Debugging and Performance

Brandon Rozek

June 15, 2019

I’ve come to like the GNU Debugger (GDB) and the perf tool recently. This post will be a short summary of the various interesting commands you can use.

GNU Debugger

To attach gbd to an existing process do the following

gdb -p pid_of_process

Otherwise you can start a new application

gdb name_of_executable

Once it loads it will bring you into it’s own REPL environment. Usually once this comes up I find it useful to add breakpoints to the program. You can either do it by function name or by line number.

(gdb) break FunctionName
(gdb) break code.cpp:81

If you just started a new application you can begin running the program with whatever arguments you wish

(gdb) run -arg1 -arg2

If you have attached to a process then you can continue its execution.

(gdb) continue

Breakpoints

If you have set a breakpoint, it will stop the processes’ execution when it lands on the breakpoint. From here, we can take a look at what’s on the stack, print variables, and do whatever other debugging we wish.

Print variables on stack:

(gdb) info locals

Print a specific variable:

(gdb) print variable_name

Show backtrace:

(gdb) bt

Continue on with program execution:

(gdb) continue

Perf

I haven’t played with perf as much but one thing I found that was cool was the perf top command. This command gives you samples of which function calls keeps the program the most busy.

To attach to a process and show samples:

perf top -p pid -K