GDB Cheat Sheet

By Spencer Davis

GDB is a debugging program that will save your life in this class and beyond. This file aims to make it more accessible for beginner’s use.

Why Use GDB?

There are essentially two methods of debugging you’ll use in this class: GDB and couts. If you’re anything like me, the first time you run your program, it’s probably going to segfault. If and when it does that, you’re gonna have to backtrace it with GDB anyway. From there, you may as well test your variable values from that line instead of placing the couts, recompiling, and rerunning. Obviously this doesn’t replace couts entirely, but it’s a tool you need to be comfortable using.

GDB Commands

Most commands have 2 ways of running them. These will be shown by the slash. I.e. run/r means you can use either run or r.

Basic Examplee

# start GDB using an executable you compiled
#   use the run shorthand r to run the program with commandline arguments
#   use the backtrace shorthand bt to see where the segfault occured
#   use the breakpoint shorthand b to set a breakpoint at line 132
#   use the run command again to run the program again

$ gdb the_rani
(gdb) r input.txt output.txt

Program received signal SIGSEGV, Segmentation fault.
(gdb) bt
#0  0x000 in TheRani::some_function (this=xxxx, x=2, n=0, subject_history=xxxxx,
    temp_history=xxx, query_count=2) at the_rani.cpp:194
#1  0x000 in TheRani::execute (this=xxxx, line=...) at the_rani.cpp:132
#2  0x000 in TheRani::main (this=xxxxx) at the_rani.cpp:60
#3  0x000 in main (argc=3, argv=xxxxxx) at the_rani.cpp:281
(gdb) b 132
(gdb) r input.txt output.txt
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Advanced Example

# this is #5 from Lab 2
#   break right before the first control flow statement
#   continue until you get to the bugged output (i.e. the fourth line of output)
#   step through the code, making sure everything is working logically
#   print out any variables that are relevant to the current line
#   on line 115, despite protector->power > invader->power, protectorLost is true
#   this shows what the error was, a mistake in < > signs in control flow

$ gdb game_student2
(gdb) break 110
Breakpoint 1 at 0x401683: file game_of_pointers_student2.cpp, line 110.
(gdb) r input3.txt output3-stu2.txt
Starting program: /home/cs104/labs/lab2/game_student2 input3.txt output3-stu2.txt

Breakpoint 1, Skirmish (protectors=0x61a0a0, invaders=0x61a220, n=2, m=3, 
    skirmishLine=1, numReserves=@0x7fffffffd970: 1, outputFile=...)
    at game_of_pointers_student2.cpp:110
110	    bool protectorLost = false;
(gdb) c
Continuing.

Breakpoint 1, Skirmish (protectors=0x61a0a0, invaders=0x61a220, n=2, m=3, 
    skirmishLine=1, numReserves=@0x7fffffffd970: 1, outputFile=...)
    at game_of_pointers_student2.cpp:110
110	    bool protectorLost = false;
(gdb) c
Continuing.

Breakpoint 1, Skirmish (protectors=0x61a0a0, invaders=0x61a220, n=2, m=3, 
    skirmishLine=1, numReserves=@0x7fffffffd970: 0, outputFile=...)
    at game_of_pointers_student2.cpp:110
110	    bool protectorLost = false;
(gdb) c
Continuing.

Breakpoint 1, Skirmish (protectors=0x61a0a0, invaders=0x61a220, n=2, m=3, 
    skirmishLine=1, numReserves=@0x7fffffffd970: 0, outputFile=...)
    at game_of_pointers_student2.cpp:110
110	    bool protectorLost = false;
(gdb) n
111	    if (invader->weapon == protector->weapon)
(gdb) print invader->weapon
$3 = "axe"
(gdb) print protector->weapon
$4 = "axe"
(gdb) n
113	      if (invader->power < protector->power)
(gdb) print invader->power
$5 = 30
(gdb) print protector->power
$6 = 100
(gdb) n
115	        protectorLost = true;
(gdb) c