10 Things You Can Only Do With GDB

Debugging is a complex task. Most of the work time, developers spend on debugging and it is important to be familiar with many debugging tools

In Linux, the native debugger is GDB and it is command line based and looks ugly and primitive. Many developers , especially those who moved from windows and worked with tools like visual studio, don’t give GDB a chance.

GDB is command line based and you need to learn the commands and it looks like debugging in the 80’s but there are some tasks you can only do with GDB

We will use the following code:

 

1 – Calling functions while debugging

While GDB stops on a breakpoint or stepping the code you can call a function with or without parameters.

 

2 – Watch Points

One of the great features GDB has is the ability to break while something is changed. It can be a variable, an address or a register:

Watch variable change:

 

Watch address change

We first print the array address :

then ask GDB to stop while the content in that address changed

 

Watch register change

The same can be done with register.

 

3 – Debugging Preprocessor Macros

Debugging macros is hard, It is preferred to work with inline function ,

While compiling the code, you can use -E to see only preprocessor output. You can ask GDB to expand a macro to see how it is handled on runtime:

 

4 – Inspecting the code without running

If you write fault handlers in your code and the program crashed, the faulty address is printed to the log:

You can load the program to GDB without running it:

And ask the debugger to list the faulty address:

This is very useful if the program is running on a different platform (i.e embedded system) , you can use the cross debugger (arm-linux-gdb)

 

5 – Inspecting core dumps

While the program crashed , the system can dump its state to a core dump file if you run:

Run the program, it will crash and create a core file

Load the program with the core dump to GDB:

You can see the problem and now you can inspect the state at the crashing moment:

backtrace:

registers:

stack (display 20 words from the stack pointer address)

and more

 

6 – Automation

GDB support automation. For example , you can ask GDB to do something on a breakpoint hit:

You can define it in .gdbinit file (in your debugging folder) and GDB loads and execute it automatically

.gdbinit file:

While running the debugger the file is executed

 

7 – GDB Macros

With .gdbinit file you can also define a new commands (macros) for your debugging sessions. For example if you write the following to .gdbinit:

It defines 2 new macros you can use – pdb , irg

 

8 – Define GDB variables

You can define variables on your debugging session and use it on other commands:

 

9 – Tips For Debugging Multiple Processes and Threads

While debugging a process that call fork(2) system call, you can choose if you want to continue with the child or the parent

You can also tell the debugger to detach the other process.

If you are debugging a multithreaded application, while a breakpoint hits, the debugger will freeze all the threads. You can tell the debugger to let the other threads continue run using:

 

10 – Integrating with IDE

The Last reason to use GDB is the ability to integrate it with some front ends. First , you can run gdb with -tui option to see the sources while running and also some other windows in text mode:

You can also find some front ends that give you the GDB command line so you can use both options (command line and GUI). You can find a list here

 

Last note

We are developers, we are using the keyboard all the time, learning GDB commands takes 30 minutes and after using a cheat sheet for a while, you won’t need the reference anymore

 

 

 

 

 

Tagged ,

5 thoughts on “10 Things You Can Only Do With GDB

  1. […] via 10 Things You Can Only Do With GDB – Developers Area […]

  2. You need to compile with “gcc -g3” to use macro expansion.

  3. In 5 it is arm-linux-gdb, no?

    1. can be any gdb (local or remote)

  4. Very good post! I recently adopted gdb as my main debugger and yours tips were useful, specially about functions and multiple processes. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *