Skip to main content

How to improve debugging skills in gdb using gdbinit?

I've been using gdb for almost 12 years now.

Meanwhile, I used couple of debugging tools as well such as:
  1. Visual studio
  2. ddd (gui solution to gdb)

Visual Studio
I liked Visual studio for debugging because it is very easy to use. Some of the common reasons:
  1. We are actually able to see source code while debugging.
  2. We are able to find the values of each variable thereonly just by hovering the mouse. 

ddd
Similarly, ddd also has the similar facility i.e.
  1. One can see source code while debugging.
  2. One can use "graph display" command to see the changes in that particular memory/variable. If you need more, you can right click on that variable and select "display" option to get it tracked.
  3. You can see assembly code as well.

When comparing with Visual Studio, ddd has similar features but not very easy to use.
Furthermore, it is not a light application, debugging speed becomes slower compared to gdb.



GDB
Now, most of the linux systems don't have ddd installed in it. Many times, you are stuck with gdb because no other alternative is available to you.

But you shouldn't get worried about this because there are lot of features in gdb which we don't use and therefore we get apprehensions using this great linux tool.

In this post, I'll share with you common gdb commands which can enhance your debugging skills/speed.

Common commands of gdb:
Some frequently used commands of gdb (you can skip this section if you are not novice user of gdb):
  1. Breakpoints (b): This allows user to put breakpoint on a particular line of a file or directly to entry of a function. These are unconditional in nature.
    • Usage1: b
      • e.g. b HelloWorld::Print()
    • Usage2: b
      •  e.g. b HelloWorld.cpp:34
  2. Conditional breakpoint (cond or if): You can put a condition while setting a new breakpoint or you can put condition on existing breakpoint
    • Usage1: b if  
      • e.g. b main if arg1 == 0
    • Usage2: cond
      • e.g. cond 2 arg1 == 0
  3. Ignore (ignore): It is used in general where a particular breakpoint will be hit too many times and you already know how many times it will get hit before you actually want to debug.
    • Usage: ignore
  4. Print (p): Print any variable value
    • Usage:
  5. Commands (comm): It is used to print particular variables each time it hits a breakpoint
    • Usage: comm
      • p
      • p
      • end
  6. Watchpoints: 
    • wa
    • awa
    • rwa

If you don't know these commands, then it is best that you start using them. They are very useful and increase your debugging skills. These will help you in later section as well.

 Use .gdbinit to enhance the debuggign experience:
  1. Using user-defined functions
If you have to use common functions on variables most of the time during debugging to get valuable information. Then this trick will work for you.
e.g. If you have 2 APIs GetType and GetMetaData to get valuable information from an object:
gdb>> p GetType(variable_name)
gdb>> p GetMetaData(variable_name)

Then, you can edit .gdbinit and put following defitions:
//.gdbinit
define gt
printf "%s \n", GetType($arg0)
end
define gmd
printf "%s \n", GetMetaData($arg0)
end
Now, you can use following command on gdb prompt:
gdb>> gt variable_name      //Equivalent to calling GetType
gdb>> gmd variable_name  //Equivalent to calling GetMetaData

Comments