Gdb Cheat Sheet

In some situations the easiest way to track down bugs is by running the test under the gdb debugger. This page contains some tips about how to use gdb.

To start gdb, type the command

gdb prog

where prog is the name of the executable file you would like to debug. For most of the assignments in CS 111, you'll be debugging an executable named test.

Common commands

run args

Start the program running, with args as its arguments. For example, if you're debugging test, args might be a single argument containing the name of the test to run. Args can contain multiple words if needed.

break vm.cc:44

Set a breakpoint at line 44 in file vm.cc. When this line of code is about to be executed, execution will stop and control will return to gdb so you can examine the state of the program. You will probably want to set breakpoints before you invoke run.

where

Print out a stack trace showing the methods that are currently active.

print x

Print the value of variable x. You can also use C++ expressions instead of a simple variable name.

next

Execute one statement, then stop again. If the next statement is a method call, that entire method will be executed.

step

Same as next, except that with the next statement is a method call, execution will stop as soon as the method is entered, before its first statement is executed.

info break

Print out information about all of the breakpoints currently set.

delete 2

Remove breakpoint 2.

handle SIGSEGV noprint nostop pass

Normallly, when a program encounters a segmentation error, gdb stops the program and takes control so you can debug why it happened. This command tells gdb not to take control after segmentation errors, so the error will be handled in the same way it would have been handled if gdb were not running. You'll need to invoke this command for Assignments 5 and 6, since your code will need to receive segmentation faults to implement paging.