Debugging with GDB Manual The GNU Source-Level Debugger (769148-001, March 2014)
Setting Display width
We now tell GDB to use a narrower display width than usual, so that examples fit in this manual.
((gdb)) set width 70
We need to see how the m4 built-in changequote works. Having looked at the source, we
know the relevant subroutine is m4_changequote, so we set a breakpoint there with the GDB
break command.
Setting Breakpoints
Here we describe how to set a breakpoint.
((gdb)) break m4 changequote
Breakpoint 1 at 0x62f4: file builtin.c, line 879.
Running the executable under GDB
Using the run command, we start m4 under GDB control. As long as the control does not reach
the m4_changequote subroutine, the program runs as usual.
((gdb)) run
Starting program: /work/Editorial/gdb/gnu/m4/m4
define(foo,0000)
foo
0000
To trigger the breakpoint, we call changequote. GDB suspends execution of m4, displaying
information about the context where it stops.
changequote(<QUOTE>,<UNQUOTE>)
Breakpoint 1, m4_changequote (argc=3, argv=0x33c70)
at builtin.c:879
879 if (bad_argc(TOKEN_DATA_TEXT(argv[0]),argc,1,3))
Stepping to the next line in the source program
Now we use the command n (next) to advance execution to the next line of the current function.
((gdb)) n
882 set_quotes((argc >= 2) ? TOKEN_DATA_TEXT(argv[1])\
: nil,
Stepping into a subroutine
The set_quotes looks like a promising subroutine. We can go into it by using the command s
(step) instead of next. step goes to the next line to be executed in any subroutine, so it steps
into set_quotes.
((gdb)) s
set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>")
at input.c:530
530 if (lquote != def_lquote)
Examining the Stack
The display that shows the subroutine where m4 is now suspended (and its arguments) is called a
stack frame display. It shows a summary of the stack. We can use the backtrace command
(which can also be spelled bt), to see where we are in the stack as a whole: the backtrace
command displays a stack frame for each active subroutine.
((gdb)) bt
#0 set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>")
at input.c:530
16 A Sample GDB Session