views:

885

answers:

2

People use gdb on and off for debugging, of course there are lots of other debugging tools across the varied OSes, with and without GUI and, maybe other fancy IDE features.

I would like to know what useful gdb scripts you have written and liked.
While, I do not mean a dump of commands in a something.gdb file that you source to pull out a bunch of data, if that made your day, go ahead and talk about it.

  • Lets think conditional processing, control loops and functions written for more elegant and refined programming to debug and, maybe even for whitebox testing
  • Things get interesting when you start debugging remote systems (say, over a serial/ethernet interface)
  • And, what if the target is a multi-processor (and, multithreaded) system

Let me put a simple case as an example...
Say,

A script that traversed serially over entries
to locate a bad entry in a large hash-table
that is implemented on an embedded platform.

That helped me debug a broken hash-table once.

+1  A: 

1. When trying to get some 3rd party closed-source DLLs working with our project under Mono, it was giving meaningless errors. Consequently, I resorted to the scripts from the Mono project.

2. I also had a project that could dump it's own information to stdout for use in GDB, so at a breakpoint, I could run the function, then cut-n-paste its output into GDB.

[Edit]

3. Most of my GCC/G++ use has been a while, but I also recall using a macro to take advantage of the fact that GDB knew the members of some opaque data I had (the library was compiled with debug). That was enormously helpful.

4. And I just found this, too. It dumps a list of objects (from a global "headMeterFix" SLL) that contain, among other things, dynamic arrays of another object type. One of the few times I've used nested loops in a macro:

define showFixes
  set $i= headMeterFix
  set $n = 0
  while ($i != 0)
    set $p = $i->resolved_list
    set $x = $i->resolved_cnt
    set $j = 0 
    printf "%08x [%d] = {", $i, $x
    printf "%3d [%3d] %08x->%08x (D/R): %3d/%-3d - %3d/%-3d {", $n, $i, $x, $i->fix, $i->depend_cnt, dynArySizeDepList($i->depend_list), $i->resolved_cnt, dynArySizeDepList($i->resolved_list)
    while ($j < $x)
       printf " %08x", $p[$j]
       set $j=$j+1
    end
    printf " }\n"
    set $i = $i->next
    set $n = $n+1
  end
end
NVRAM
The Mono gdb reference is nice -- did not know about that. Not sure if I got your second part correctly, are you describing a function that was integrated into the project build to be called from a breakpoint in GDB? that is a good trick and was useful over a slow serial line debug interface in one of my projects.
nik
W.r.t the 2nd part, it was long ago. But yes, it was source code meant only to be called while at a breakpoint in GDB. I _think_ it was an attempt to track down some stack corruption with the help of the *__builtin_frame_address(n)* -- which is a macro and can't be called from GDB.
NVRAM
+1  A: 

This script, not written by me, pretty prints STL containers, such as vector, map, etc: http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt

Totally awesome.

Artem Russakovskii
+1, this is an excellent example!
nik