110 likes | 265 Views
Gdb tutorial. Vishnu Nath. What is gdb ?. “GNU Debugger” Great with several languages, including C and C++ Allows inspection of program during execution Segmentation fault and other errors may be much easier to find. Usage. Requires gcc compiler with the –g flag set. Syntax:
E N D
Gdb tutorial Vishnu Nath
What is gdb? • “GNU Debugger” • Great with several languages, including C and C++ • Allows inspection of program during execution • Segmentation fault and other errors may be much easier to find
Usage • Requires gcccompiler with the –g flag set. • Syntax: g++ [other flags] –g <source files> -o <output files> • Example: g++ -Wall –Werror –ansi –g program1.c –o program
Startup functions in gdb • run – begin execution of program • kill – stop execution of program • quit – exit gdb • help – get description of every command in gdb
Simple program – no error int main() { int x = 30, y = 20; x = y; return 0; }
More functions • list – prints out lines of code above and below the line the program is stopped at • print – examine the value of the variable • set – set the value of the variable • call – call any function linked to program. This includes user and library functions. • finish – finish function execution and return to caller.
About function stepping • next void foo() { … } void foo2() { … } • step void foo() { … }
Call Stack • Place to find stack frames that control program flow. • Look at call stack to understand how the program is running. • Function – backtrace • frame - Change stack frame Example: (gdb) frame 7
Breakpoints • Way of telling the debugger that user would like to stop program at certain lines of code. • User can have program stop during specific function calls. • Once stopped, user can examine memory and check values of all variables, examine stack and step through program.
Breakpoints’ functions • break – set a breakpoint at a particular line Example: break 32 • break can be used to set a breakpoint for a function Example: break foo_func • tbreak – temporary break. Stops program only once. • Disable – remove particular breakpoint Example – disable 2 • info breakpoints – list of breakpoints and info
Examine Memory • Use the ‘x’ command to examine memory. • Syntax: x/FMT ADDRESS Example: char* s = “Hello World”; Examine as string: (gdb) x/s s => Examine as character: (gdb) x/c s =>