GDB Programming

Using GDB to get stack traces at particular program points

A while back I wrote how I sometimes use Valgrind to print a stack trace every time a particular program point is reached. I just learned how to do likewise with GDB. Here’s an example session that illustrates what to do.

(gdb) break je_chunk_alloc_mmap     # set a breakpoint
Breakpoint 1 at 0x1aa32c0
(gdb) commands                      # enter breakpoint command sequence
Type commands for breakpoint(s) 1, one per line.
End with a line saying just "end".
>bt                                 # print stack trace
>c                                  # continue execution
>end                                # end command sequence
(gdb) set pagination off            # don't pause when the screen fills up
(gdb) set logging on                # copy output to a file
Copying output to gdb.txt.

This is better than the Valgrind technique because (a) it doesn’t require modifying the source code, and (b) programs tend to run faster under GDB than they do under Valgrind.

Thanks to Mike Hommey for helping with this.

2 replies on “Using GDB to get stack traces at particular program points”

I learned just last week that you may do this in LLDB as well:

(lldb) br com add
Enter your debugger command(s). Type ‘DONE’ to end.
> bt
> c

If you are using WinDbg, you can do it on the same line when you set the breakpoint. Commands are quoted and separated by semicolons:
bp mozglue!je_chunk_alloc_mmap “k; g”

Comments are closed.