GDB Tutorial: Advanced Debugging Tips For C/C++ Programmers

Ask from any core C/C++ programmer or a white box tester, what tool they like to use for debugging the code? Most of them would vote for GDB debugger or the GNU debugger. So here we bring a GDB tutorial that lists essential GDB commands and some exclusive tips to boost your debugging skills. The full form of GDB is the GNU Project debugger. And it’s the most important debugging tool for software geeks that work on UNIX and Linux-based systems.

GDB debugger was originally developed by Richard Stallman in 1986 while he was working on his GNU project. It’s an extremely versatile and pliable tool. Mastering it would save you significant time in debugging the complex code issues. In this GDB tutorial, we’ll share all the basic to advance debugging tips that every C/C++ programmer can use.

Some of you may have several questions about GDB debugger at the back of your mind like “What is GDB or why is it used for or how to use GDB for debugging?”. But before we answer you, let’s see what Brian Kernighan has said about the debugging.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”

GDB Tutorial: Essential Debugging Tips.

GDB Tutorial - Advanced Debugging Tips

GDB Tutorial – Advanced Debugging.

A great debugging tool is one of the most critical assets of any programmer’s armory. It allows you to see what is going on “inside” another program at run-time. In this GDB tutorial, you’ll get to learn things like how to pause the code execution, inspect the state of any variable on the stack and so on. You’ll see how to use GDB for stepping through the source code and watch the changes to a variable.

And probably, you would like a number of other advantages of the GDB debugger that we’ve covered in this GDB tutorial. Also, we’ll lay out 100% practical ways that you can directly apply in your work environment.

So, better bookmark this GDB tutorial so that you can also refer it anytime while debugging the production code.

We’ll now start with the first steps in which you’ll prepare your program for the debugging.

1. Compile a Program with Debugging Symbols.

This is the first step which you must complete before you start using GDB to debug your code. Please compile the code using the GDB options as given in the below example.

$ gcc -ggdb -Wall -o gdbtest gdbtest.c

# -g: This option adds debugging info in the operating system's native format.
   e.g. stabs, COFF, XCOFF, or DWARF.

# -ggdb: It produces debugging information compatible for use by GDB.
   i.e. the most expressive format available.
   e.g. DWARF 2, stabs, or the native format if none is available.

# -Wall: It enables all warnings in the code.

2. Two Ways to Start a Program with GDB.

There are multiple ways you can start your program with GDB which we’ve mentioned below.

Run a program without any argument.

Simply open the terminal window and run the following command.

$ gdb program

(gdb) run (r as shortcut key)

Run a program with arguments.

There are two ways you can feed arguments to your program in the GDB debugger. Please follow the steps given in the below code snippet.

  $ gdb --args program arg1 arg2 ... argN  
  (gdb) r

  $ gdb program  
  (gdb) r arg1 arg2 ... argN

#Note - Run GDB with <--silent> option to hide the extra output it emits on the console.

3. How to Print Source Code in GDB Console?

If your program hits a break point, then you may want to look at the source code around it. In such a case, use the <l (or list)> command which prints <ten lines> of source code at a time.

You can also pass the list command <a line number> or <a function name> to tell GDB where to start.

Display lines after a line number.Display lines after a function.
(gdb) list 12
7         int status = 0;
9         if (!isNumber(*ptr))
10           return -1;
11        else
12            while (isNumber(*p))
13                status += *p - '0';
14        return status;
15    }
(gdb) list CheckValidEmail
14        return result;
15     }
17     void
18     CheckValidEmail(char* eMail)
19     {
20         int rc = isValidEmail(eMail);
21         if (rc &lt; 0)
22         fputs("Invalid eMail\n", stderr);
23     }

💡 Note – GDB debugger usually shows a few lines back of the point you requested.

4. Six GDB Commands to Set up Breakpoints.

Probably it’s a great idea to outline different ways of setting break points in GDB debugger. It’s because a smart break-point can help you quickly find bugs in the source code.

1. Break into a line or a Function.

(gdb) break (b as shortcut) linenum
# Note: Break will take place at line <linenum> in the current source file.
# The current file is the last file whose code appeared in the debug console.
(gdb) b function

2. Break into a line which is relative to the current line.

(gdb) b +linenum

3. Break into a Function in a given file.

(gdb) b filename:function

4. Break on to a line in a given file.

(gdb) b filename:linenum

5. Break upon matching memory address.

If you have a program without debug symbols then you can’t use any of the above option. Instead, the gdb allows you to specify a break point for memory addresses.

(gdb) b *(memory address)

6. Break after a condition.

(gdb) b <...> if condition

# Note:
#1: The symbol <...> implies that you can use any form of breakpoints.
#2: Some of them you've already seen in the previous tips.
    Example: break linenum if variable==1

5. Four GDB Commands to Print Debug Info.

Command to execute after the program hits a break-point.

With the help of the <command> keyword, you can set multiple commands to run every time a break point occurs. See the below code snippet for clarity.

(gdb) b CheckValidEmail
Breakpoint 1 at 0x8049d87: file ../../test/testgdb.c, line 107.
(gdb) command 1
# Note:
#1: 1 is the breakpoint number.
#2: Here you can specify set of commands to execute.
#3: To close the command block, use the "end" keyword.
>print port
>print IPAddr
>print User
>print Pwd

1. How to print backtrace after the break point?

(gdb) backtrace (or bt as shortcut)
# OR
(gdb) info stack

# Note: This option will display a chain of functions (on the output console).

2. How to execute a function to the end after a break point?

You can issue the <fin> command in side GDB. It’ll run through the entire function.

(gdb) fin
#It'll execute to the point where function returns.

3. How to print the current stack of the executing program?

You can call the <where> command which will return the trace along with the line number.

(gdb) where
#Tell you the point where the program died.

4. How to print the line number in GDB while debugging?

It’s the <frame> gdb command that will return the line number.

(gdb) frame
 #0  0x0807826e in main () at test.c:18
 18        if(is_exist(list, 10) != 0 ) {

6. Six GDB Commands to Trace Variables.

1. Print standard variable (int, char etc.)

(gdb) p <<variable>>

2. Print structure variable.

(gdb) p <<structure>>

3. Print pointer variable.

(gdb) p <<*ptr>>

4. Print a Macro.

Printing a macro requires that you first compile your program with an extra option. Use the <-ggdb3> flag.

(gdb) p/x DBG_FLAG
$1 = 0x00

# Note: The x in the p/x would cause to print output in hex format.

5. Print an Array.

We’ll use an example to show how to print an array. Please see below code snippet.

Example: If you have defined an integer array as int arr[5] = {1, 2, 3, 4, 5};
Then you can use - (gdb) p arr
Output would be $1 = {1, 2, 3, 4, 5}

But the above method would not help if we have a large array of integers say 100 and the need is to print the integers between (96-100) range.
For this purpose, you can use following method which works in all cases but is a little complex in nature.
e.g. (gdb) p *&arr[96]@5

6. Add Watchers.

Adding watch points is same like telling the debugger to give a dynamic analysis of changes to the variables. And, it’s easy to add a watch point in your code.

(gdb) watch <<variable>>

7. How to Continue, Step or Next Operations?

After you’ve finished examining the variables, the next thing you will be doing is executing the code. So to simply resume the execution, issue the <c (or continue)> command. And your code will continue running until it hits an another break point.

Breakpoint 2, CheckValidEmail(p=0xbffff260 "") at testgdb.c:23
23      int result = isValidEmail(ptr);
(gdb) n
24      if (result < 0)

<c> to continue execution.

If your program hits a break point, then use this command to resume its execution.

<n> for executing the next line.

After a break point, press <n> to execute the next line of code. It’ll run the entire line irrespective of it is a function call or a basic assignment.

<s> to step into the.

If a program is in the halt state, then use <s> to step into any function call rather executing them completely.

8. How to Skip/Ignore Breakpoints?

While you are running through a loop in your code and wouldn’t want to pause for every break, then <ignore> command can help. Here is how you can skip a break point the number of times you want.

First, check the index of the break point which you want to ignore. Use the <info breakpoints> command.

(gdb) break test.cpp:18
Breakpoint 1 at 0x8005cd: file test.cpp, line 18.
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000008005cd in main() at test.cpp:18

Then, run the following command in the GDB debugger. Say, we want to ignore the break for 1000 times.

(gdb) ignore 1 1000
Will ignore next 1000 crossings of breakpoint 1.
(gdb) run
Starting program: /home/sample/src/test
Program received signal SIGSEGV, Segmentation fault.
0x00000000008005cd in main () at test.cpp:18
18            i = *ptr++;
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000008005cd in main() at test.cpp:18
    breakpoint already hit 10 times
    ignore next 990 hits

In the above example, we set the ignore limit to 1000 but the program crashed after 10th iteration. So you should revise the ignore limit to 9 and step in to debug the condition which is leading to the crash.

This is how the <gdb ignore> command helps in isolating issues.

9. How to Remove Breakpoints & Quit from GDB?

Finally, let’s end this GDB tutorial with the following two commands.

Deleting a break point.

The option <d> is the GDB shortcut for deleting any break point.

(gdb) d <<breakpoint num>>

Quitting from the GDB debugger.

Use <q> or the <quit> command to exit from the GDB debugger.

(gdb) q

Summary – GDB Tutorial [Advanced Debugging Tips].

We wish that you would have learned many key things from this amazing GDB tutorial. And would have acquired enough knowledge about GDB to counter most common issues in your source code.

If you wish to get access to more GDB resources, then please explore the GNU website. And for your information, GDB itself comes with a built-in help system. So just type help in the <gdb> console. And you will see a lot of options which you can explore further.

To find out more on GDB, you can also run the command “info gdb” from the terminal window.

Happy Bug Hunting!