150 likes | 350 Views
Debugging with gdb. Eileen Kraemer CSCI 6950 – Research Methods in CS August 31, 2009. What does a debugger do?. Allows you to see what is going on "inside" a program while it executes AND/ OR what that program was doing at the moment it crashed. GDB functions.
E N D
Debugging with gdb Eileen Kraemer CSCI 6950 – Research Methods in CS August 31, 2009
What does a debugger do? • Allows you to see • what is going on "inside" a program while it executes • AND/ OR • what that program was doing at the moment it crashed.
GDB functions • GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act: • * Start your program, specifying anything that might affect its behavior. • * Make your program stop on specified conditions. • * Examine what has happened, when your program has stopped. • * Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
Preparing to debug • In order to debug a program effectively, you need to generate debugging information when you compile it. This debugging information is stored in the object file; it describes the data type of each variable or function and the correspondence between source line numbers and addresses in the executable code. • To request debugging information, specify the `-g' option when you run the compiler. Specifying –ggdb will provide additional info.
Gdb commands • Running/Resuming Execution • Stopping Execution • Examining the Data and Source Code • Examining the Stack
Running/Resuming Execution • run • Start your program • Step [count] (s) • Continue running until control reaches another source line • next [count] (n) • Continue running until next source line in current stack frame • Continue (c) • Resume where you left off • finish • Continue running until the current stack frame returns
Stopping execution • break – set breakpoints • more on next slide … • watch EXPR • stop execution whenever the value of an expression changes • info • Print a table of all breakpoints and watchpoints set and not deleted • clear • delete breakpoints by location • delete • delete breakpoints by argument
break • break FUNCTION • Set breakpoint at entry to FUNCTION. • break LINENUM • Set breakpoint at LINENUM in current source file. • break FILENAME:LINENUM • Set a breakpoint at LINENUM in source file FILENAME. • break FILENAME:FUNCTION • Set breakpoint at entry to FUNCTION in FILENAME. • break ... if COND • Set breakpoint with condition COND; evaluate the expression COND each time the breakpoint is reached, and stop only if the value is nonzero--that is, if COND evaluates as true. ... stands for one of the possible arguments described above (or no argument) specifying where to break.
Examining Data and Source Code • print [exp] (p) • exp = a source language expression • display EXP (disp) • add EXP to the list of things that display whenever your program stops • list (l) • list LINENUM • list FUNCTION • list • list -
Examining the stack • backtrace • where