GDB Tutorial: Essential Debugging Tips For Using GDB

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 to help beginners and professional C/C++ programmers.

GDB tutorial to help beginners and professional C/C++ programmers.

Hence, 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 step in which you’ll prepare your program for the debugging.

GDB tutorial Tip (1) – Compile your 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.

GDB tutorial Tip (2) – Start you program with GDB.

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

#Program with no argument.

Simply open the terminal window and run the following command.

#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.

A little note for you.

When you start GDB, it throws a lot of information on the console about itself. And then expect you to type its commands. You can always hide the extra output by starting it with the <–silent> option.

GDB tutorial Tip (3) – Display source code in the 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.

Please note.

The gdb debugger normally shows a few lines back of the point you requested. It’s just to give you an added eyeshot.

GDB tutorial Tip (4) – Setting break points into your code.

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.

#Standard break points.

Break into a line or a Function.

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

Break into a Function in a given file.

Break on to a line in a given file.

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.

Break after a condition.

#Advance break point conditions.

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

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

How to print backtrace after the break point?

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.

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.

How to print the line number in GDB while debugging?

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

GDB tutorial Tip (5) – Monitor variables while debugging.

Print standard variable (int, char etc.)

Print structure variable.

Print pointer variable.

Print a Macro.

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

Print an Array.

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

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 tutorial Tip (6) – 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.

<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.

GDB tutorial Tip (7) – Ignoring break points.

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.

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

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.

GDB tutorial Tip (8) – Concluding GDB Commands.

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.

Quitting from the GDB debugger.

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


Final Words.

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!



  1. Imraan Reply
  2. Jenkins Reply
  3. Manju Reply

Leave a Reply