GDB Manual


What is GBD?

  1. GDB stands for GNU Debugger.


  2. Before you can debug, you must compile your C/C++ program with the -g flag:
  3. gcc -g main.c   -o main   # for C
    g++ -g main.cpp -o main   # for C++
    

  4. Start GBD (it does not start your program automatically):

    gdb ./main
    gdb --args ./main arg1 arg2


  5. Run GDB with -tui will start it looking like GUI in the terminal.
    In some terminals you can also call set tui mouse on to activate mouse support.


Running the program (run)

  1. Basic syntax:

    (gdb) run [arguments]


  2. It will start the program from beginning.


  3. If you have loaded your program with arguments, you don't have to do it again here.



Breakpoints (break)

  1. A breakpoint tells GDB to pause your program at a specific place so you can inspect state (variables, stack, memory) before continuing.


  2. Set a breakpoint:


  3. Temporary breakpoint (auto-delete on first hit): use tbreak instead of break.


  4. Inspect breakpoints: info break or info breakpoints


  5. Enable / Disable / Delete breakpoints:


  6. Clear breakpoints at a location (without knowing breakpoint ID):

    (gdb) clear foo.c:42

    (gdb) clear my_func


  7. Conditional breakpoint: break foo if i > 10 && p != NULL


  8. Ignore first 10 hits of the breakpoint 5: ignore 5 10



Stepping through code (next and step)

  1. step [n]: To execute one line of code, if the current line calls a function, stops at the first line inside it.
    With n, it repeats that n times.


  2. next [n]: To execute one line of code, executing any called functions completely.
    With n, it repeats that n times.


  3. stepi [n]: executes exactly one machine instruction and then stops.


  4. nexti [n]: executes one machine instruction but if that instruction is a call, GDB runs the entire call and stops at the instruction after the call.



Inspecting variables and memory (print)

  1. To print a value:
    (gdb) print i
    (gdb) print &i
    (gdb) print a + b
    (gdb) print *ptr
    (gdb) print arr[3]
    (gdb) print *(struct node*)ptr
    (gdb) print ((int*)buf)[3]
    
    (gdb) p /x i                # hex
    (gdb) p /d i                # signed dec
    (gdb) p /u i                # unsigned dec
    (gdb) p /t i                # binary
    (gdb) p /c ch               # char
    


  2. Pretty Printer: set print pretty on


  3. Display local variables or arguments:
  4. (gdb) info locals       # Shows all local variables in the current stack frame.
    (gdb) info args         # Lists all function arguments for the current stack frame.
    


  5. Print information about registers:
  6. (gdb) info registers
    (gdb) print $pc, print $sp, print $fp     # architecture-specific names valid
    


  7. Modify during debug (BE CAREFUL):
  8. (gdb) set var i = 42
    (gdb) set {int}0x7fffffffde20 = 123
    


  9. Raw memory inspection: x/[COUNT][FORMAT][SIZE] <ADDRESS>




Resume from breakpoint (continue)

  1. It tells GDB to resume the execution of your program from its current stop point (where it's paused)
    until the next stop condition is met.


  2. continue: resumes until any stop condition.


  3. continue N: resumes after ignoring N breakpoints.



Backtrace and frames (backtrace)





Watchpoints (watch)





Inspecting registers and assembly (info)





Catchpoints