GDB hooks for debugging GCC

David Malcolm dmalcolm@redhat.com
Sat Aug 3 01:48:00 GMT 2013


GDB 7.0 onwards supports hooks written in Python to improve the
quality-of-life within the debugger.  The best known are the
pretty-printing hooks [1], which we already use within libstdc++ for
printing better representations of STL containers.

I've written debug hooks for when the inferior process is *GCC*, which
I'm attaching.

Here's a debug session of cc1 using them, with some comments added by
hand:

[david@surprise gcc]$ gdb \
  -iex "add-auto-load-safe-path ." \
  --args ./cc1 foo.c -O3
(..snip gdb startup...)
Successfully loaded GDB hooks for GCC
(gdb) break expand_gimple_stmt
Breakpoint 5 at 0x68e94a: file ../../src/gcc/cfgexpand.c, line 2357.

(gdb) run
Breakpoint 5, expand_gimple_stmt (stmt=
    <gimple_assign 0x7ffff0440058>) at ../../src/gcc/cfgexpand.c:2357
2357	  location_t saved_location = input_location;

(gdb) bt
#0  expand_gimple_stmt (stmt=<gimple_assign 0x7ffff0440058>)
    at ../../src/gcc/cfgexpand.c:2357
  (note how stmt isn't just an address, it tells you the type of stmt)

#1  0x0000000000694dc6 in expand_gimple_basic_block (bb=
    <basic_block 0x7ffff041f208 (3)>, disable_tail_calls=false)
    at ../../src/gcc/cfgexpand.c:4204

  (note how the index of the basic block is given)

#2  0x0000000000696855 in gimple_expand_cfg ()
    at ../../src/gcc/cfgexpand.c:4723
#3  0x00000000009b59ad in execute_one_pass (pass=
    <opt_pass* 0x188b600 "expand"(170)>)
    at ../../src/gcc/passes.c:1995
#4  0x00000000009b5bb7 in execute_pass_list (pass=
    <opt_pass* 0x188b600 "expand"(170)>)
    at ../../src/gcc/passes.c:2047

  (note how the name and static_pass_number of the pass is given)


#5  0x00000000006c288a in expand_function (node=
    <cgraph_node* 0x7ffff0312720 "foo">)
    at ../../src/gcc/cgraphunit.c:1594

  (note how the cgraph_node* is printed with the decl name, so you can
easily figure out which function you're in)

(gdb) fin
Run till exit from #0  expand_gimple_stmt (stmt=
    <gimple_assign 0x7ffff0440058>) at ../../src/gcc/cfgexpand.c:2357
0x0000000000694dc6 in expand_gimple_basic_block (bb=
    <basic_block 0x7ffff041f208 (3)>, disable_tail_calls=false)
    at ../../src/gcc/cfgexpand.c:4204
4204		      last = expand_gimple_stmt (stmt);
Value returned is $3 = (note 9 8 10 [bb 3] NOTE_INSN_BASIC_BLOCK)

  (note how the rtx_def* is printed inline.  This last one is actually a
kludge; all the other pretty-printers work inside gdb, whereas this one
injects a call into print-rtl.c into the inferior).

Edges are printed giving the endpoints:

  (gdb) p e
  $1 = <edge 0x7ffff043f118 (ENTRY -> 6)>

Note that this doesn't eliminate the .gdbinit script that some people
use, but it's arguably far superior, in that it happens automatically,
and for all values that are printed e.g. in backtraces, viewing struct
fields, etc.

I'd like to get this into trunk.  Some remaining issues:
  * installation; currently one has to manually copy it into place, as
noted above; it would be nice to automate things so that during a build
it gets copied to cc1-gdb.py, cc1plus-gdb.py etc
  * it hardcoded values in a few places rather than correctly looking up
enums
  * the kludge in the RTX printer mentioned above.

Hope this is helpful
Dave

[1] http://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html

-------------- next part --------------
A non-text attachment was scrubbed...
Name: gdb-hooks.py
Type: text/x-python
Size: 11391 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20130803/570bf0da/attachment.py>


More information about the Gcc-patches mailing list