A Code Monkey's Blog

Quick print: a little trick for gdb setting

Today I would like to share a little trick that I have adapted and used for a while. It is for gdb. Before your reading the rest of the post, and if you have no idea about the customization of gdb, please read here

If you use gdb frequently, chances are you have to type the command 'print' or 'p' to examine the value of variable many many times. A common case is like this: you type 'p foo' to see the value of 'foo'. It turns out that 'foo' is a pointer, and you have to type 'p *foo' again in the commandline. Or you have to type 'ptype foo' to find out the type definition of 'foo'. Tedious. This small piece of gdb macro to make your life easier. Every time you type 'p foo', you just scroll up the history and type '1', 'p foo 1' shows the dereferenced value, or typing '2' gives you the type definition of 'foo'. Copy and paste into your ~/.gdbinit

define p
 if $argc == 1
   print $arg0
 else
   if $arg1 == 1
     print *($arg0)
   else
     if $arg1 == 2
       ptype $arg0
     end
   end
 end
end
document p
   Print the variable information quickly
end