GNU Debugger (GDB) Quick Notes

GDB (GNU DeBugger) is a powerful commandline debugging tool for programs written in C, C++, and other languages, allowing developers to run programs step-by-step, set breakpoints, inspect variables, view the call stack, and modify program behavior during execution to identify and fix bugs efficiently.


First install GDB:

sudo apt install gdb

Consider this code snippet:

#include <stdio.h>

int main()
{
    long double start, end, step;

    printf("Enter the start value: ");
    scanf("%Lf", &start);

    printf("Enter the end value: ");
    scanf("%Lf", &end);

    printf("Enter the step value: ");
    scanf("%Lf", &step);

    while (start != end)
    {
        printf("Value: %Lf\n", start);
        start += step;
    }
}

Compile the program with gcc main.c -o main.out , and run with ./main.out.

When we run the program with the following output:

Enter the start value: 1
Enter the end value: 5
Enter the step value: 0.2

The program will not end because of the floating point error. We can use the GDB debugger to find the root cause of the problem.

To do this, we can compile the program using gcc with the information needed for debugging, which includes the mapping between the source code and the binary. Because of this extra information, this compiled binary will be larger than the normal binary file.

To compile:

gcc main.c -o main.out -g

Flag -g helps to compile with debugging information.

To invoke the GDB with the binary file:

gdb main.out

This will open-up the GDB commandline debugger. The commands below that starts with (gdb) are commands entered in the commandline debugger.

We can see the source-code using the command (gdb) list . This will print the entire source code mapped in the main.out. In some cases this can also help to find the line number of the source code to set the break-points later.

Use the command, (gdb) break 17 to create a breakpoint on the line number 16 (In our example case, just after where the while loops begins).

The command (gdb) deassemble main will print the assembly code of the main function (not the entire program in the main.c file).

The command (gdb) run now runs the program and break at the set breakpoint and let us analyse the memory and individual variables.

(gdb) print <variable> will print the value of the variable in the breakpoint. For example, in our case, (gdb) print start will print the start value in the while loop and we can use (gdb) continue to continue the program until it meets the next breakpoint. We can pause and see the value of the variable start when it is supposed to have 5 but you can see it is actually not 5 but 4.99999999999999999957 which makes the while loop condition to be true all the time and the loop continue infinitely. This concluded the loss of precission error in floating point numbers is in the program.

There are few other useful GDB command below:

  1. run arg1 arg2 .... : We can run the program using this command and also pass any number of optional arguments that the program might require.

  2. quit : Quit the debugger.
  3. break : Set breaking points in the program
    1. break main : Create a breaking point at the start of the main function.
    2. break 42 : Create a breaking point at the line number 42.
    3. break file.c:15 : Create a breaking point at line 15 of file.c
  4. info breakpoints : Get the details about the points set in the program.
  5. delete 2 : Delete the 2nd breaking point (breaking point number can be retrived from info command)
  6. watch var : Watch a variable and stop when its value changes.
  7. continue ****(or c): Continue execution until the next breakpoint or end.
  8. step (or s): Step into a function call.
  9. next (or n): Step over a function call.
  10. finish: Run until the current function returns.
  11. until 50: Run until line number 50.
  12. print x : Print the value of a variable.
  13. display x : Automatically print a variable’s value every time it changes.
  14. info locals: Show local variables in the current stack frame.
  15. info args: Show function arguments in the current stack frame.
  16. backtrace (or bt): Show the call stack.
  17. frame 2 (or f): Move to a specific stack frame.
  18. list (or l): Show source code near the current execution point.
  19. list
  20. list 10 : List around line 10
  21. list main : List the main function
  22. condition: Set a condition on a breakpoint.
  23. condition 2 x > 5 : Set a breakpoint number 2 only when variable value x is greater than 5
  24. set var x = 42 : Modify a variable or memory.