Getting started with GDB

In this article, I am going to help you set up the debugger and use that in your machine. This is the second article in this series. In the first article, I have written about approaches to debug your c/c++ application.

GDB For You

The debugger I use and will write here about is GDB. Why GDB?

GNU Debugger(GDB) supports a wide variety of platforms including ARM, x86, AVR, Motorola, etc. It also has various language supports including C/C++, Go, Java, and even Python.

Setting up the GDB

Installing GDB on Linux is not difficult at all. I have an Ubuntu machine and I just need to type these magic words in the terminal to get GDB for my machine.

sudo apt-get update

sudo apt-get install gdb

Pretty easy right? Yeah for Ubuntu, that’s about it. Also, if you are using Linux, you probably have gdb in you. 

For Windows though, install the Mingw. It does have the gdb you needed for your application. 

Let’s use it

Open the command prompt or terminal in your machine and navigate it to your Project. Compile it with the flag -g. For example:

gcc -g main.c -o main.o

If it doesn’t have any compile-time error, it will compile and generate the .o file needed to debug the application. The -g flag is used to make the debug build of the program. In the terminal, type:

gdb main.o

This will load “main.o” file in the gdb terminal. Now, you can do a bunch of things with it. But let’s just take a step back and understand more with an example. You can find the example after the cheat sheet.


debug code in C

Example Code

Let’s just say that I want to write a code for factorial. Here is the sample code for that. Now, as far as I can see this code has two problems. I want to debug that.

#include <stdio.h>
#include <stdint.h>

int multiply(int a, int b) {
    return a*b;
int factorial(int a) {
    int product = 0;
    for( int i = a ; i > 0; i--) {
        product *= multiply(i,i--);
    return product;

int main(void) {
    int a = 5;
    int c = factorial(a);
    printf("Factorial of A:%d is :%d\n",a, c);


Let’s Debug it

Now, I want to know why I am getting the answer ‘0’ every time with different values of a. For debugging this, I want to stop the program at the start and the end of the factorial function. I will put the breakpoint at the factorial function.

b factorial

As soon as you run the program by typing “run” in the gdb terminal, you can see the program stops at the function factorial. From here, you can do many things. You can jump to the next instruction by typing “next” in the terminal. The next command “steps-over” the function that may come in the way. For example in the above code, the next command will not go inside the multiply function. For stepping inside the function you should use the “step” command. If you want to see the value of any variable(should be in the scope), you can see by typing- print “variable”. For example, I want to see the value of the product variable, I should type “print product“. Now, I can see what’s wrong with my program. The initial value of the product variable is ‘0’. So, anything that multiplies with it becomes ‘0’. Now, can you find another mistake in the code?

There are several other commands as well. For example, you want to continue the code, you can press “c”. You want to see the call stack of the code, you can type “bt” in the terminal. You want to see the information about breakpoints, type “info b”. You can delete the breakpoint as well, just type “delete <num>” num here means which breakpoint. There are still many commands that are there. Even I don’t know all of them. I suggest you guys play and explore with these commands.


You can configure the gdb with your IDE. The IDE that I use is VS-Code and you can configure the gdb in it. You can find the instruction here.

This will make your life much easier than before.

I think that’s it for this article if you have any problem or feedback regarding this article or want to appreciate the article(highly recommended). You can comment in the comment section.

2 thoughts on “Getting started with GDB”

Leave a Comment

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

Share This