NOTE: this website is out of date. This is the course web site from a past quarter. If you are a current student taking the course, you should visit the current class web site instead. If the current website is not yet visible by going to cs111.stanford.edu, it may be accessible by visiting this link until the new page is mounted at this address. Please be advised that courses' policies change with each new quarter and instructor, and any information on this out-of-date page may not apply to you.
Handout by John Ousterhout, with modifications by Nick Troccoli
This page contains some tips about how to use gdb. For more in-depth debugging tips, check out the CS107 debugging guide:
To start gdb, type the command
gdb prog
where prog is the name of the executable file you would like to debug.
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.
backtrace [OR] 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 if 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.