gdb gdb is the GNU debugger on our CS machines. gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With gcc and g++, this is accomplished using the -g option, for example, to compile prog.c and have the executable stored in testprog, use the following: gcc -Wall -g -o testprog prog.c Once you have an executable file (in this case testprog), you can use gdb to debug it. First you must launch the debugger. To launch the debugger you type gdb <executable> For the above program, this will be gdb testprog
Valgrind Valgrind is another suite of tools for debugging and profiling your programs. First compile your program using the -g option as above, then launch valgrind: valgrind [valgrind-options] [your-prog] [your-prog-options] For example, if you execute testprog using the command./testprog progdata1.txt you could use the following command valgrind --leak-check=yes./testprog progdata1.txt For more information about valgrind, refer to the man page: man valgrind
Using gdb to find the point of failure The gdb debugger is a handy tool for identifying the location at which a program failed. At the (gdb) prompt you will usually want to tell the debugger to halt the program when it reaches the start of the main() function. The b command is short for breakpoint and tells the debugger where to stop. After a function is entered, source code line numbers can be used to specify breakpoints.
Using gdb to find the point of failure (gdb) b main Breakpoint 1 at 0x804833b: file prog.c, line 11. To start the program use the run command: (gdb) run Starting program: /home/rlowe/cs1070/examples/ When the program reaches a breakpoint gdb will tell you and display the next line of code to be executed. Breakpoint 1, main () at prog.c:11 11 printf("val of ptr is %p \n", ptr);
Using gdb to find the point of failure Use the next command to execute a single source statement. The next command will treat a function call as a single statement and not single step into the function being called. If you want to single step through the function use the step command to step into it. The output of the printf() is intermixed with gdb's output and is shown in blue below. (gdb) next val of ptr is (nil) 13 *ptr = 99;
Using gdb to find the point of failure Saying next again will cause the program to execute the flawed assignment. gdb will show you the line that caused the error (line # 13). (gdb) next Program received signal SIGSEGV, Segmentation fault. 0x08048357 in main () at prog.c:13 13 *ptr = 99;
Using gdb to find the point of failure The where command can show you where the failure occurred (along with a complete function activation trace. (gdb) where #0 0x08048357 in main () at prog.c:13 #1 0x40049917 in libc_start_main () from /lib/libc.so.6 (gdb) To print the value of a variable use the print command. (gdb) print ptr $1 = (int *) 0x0 Attempting to print what ptr points to reaffirms what the problem is: (gdb) print *ptr Cannot access memory at address 0x0
Using gdb to find the point of failure Use the q (quit) command to terminate gdb (gdb) quit The program is running. Exit anyway? (y or n) y home/rlowe/cs2100/examples ==>
Common gdb commands Command Description help help topic-classes help command apropos search-word info args i args info breakpoints info break info break breakpointnumber info watchpoints List gdb command topics. List gdb command within class. Command description. Search for commands and command topics containing search-word. List program command line arguments Print info about breakpoints and watchpoints Print breakpoint numbers. Print info about specific breakpoint. Print info about watchpoints
Common gdb commands tbreak watch condition clear clear function clear line-number delete d delete breakpoint-number delete range disable breakpointnumber-or-range enable breakpoint-numberor-range Temporary break. Break once only. Break is then removed. See "break" above for options. Suspend processing when condition is met. i.e. x > 5 Delete breakpoints as identified by command option. Delete all breakpoints, watchpoints, or catchpoints. Delete the breakpoints, watchpoints, or catchpoints of the breakpoint ranges specified as arguments. Does not delete breakpoints. Just enables/disables them. Example: Show breakpoints: info break Disable: disable 2-9
Common gdb commands Break and Watch break funtion-name break line-number break ClassName::functionNam e break +offset break -offset break filename:function break filename:linenumber break line-number if condition Suspend program at specified function of line number. Set a breakpoint specified number of lines forward or back from the position at which execution stopped. Don't specify path, just the file name and function name. Don't specify path, just the file name and line number. break Directory/Path/filename.cpp:62 Where condition is an expression. i.e. x > 5 Suspend when boolean expression is true.
Common gdb commands enable breakpoint-number once continue c continue number finish Enables once Continue executing until next break point/watchpoint. Continue but ignore current breakpoint number times. Usefull for breakpoints within a loop. Continue to end of function. Line Execution step s step number-of-steps-toperform next n next number Step to next line of code. Will step into a function. Execute next line of code. Will not enter functions.
Common gdb commands until until line-number where Source Code list l list line-number list function list - list start#,end# list filename:function set listsize count show listsize Continue processing until you reacha aspecified line number. Also: function name, address, filename:function or filename:line-number. Shows current line number and which function you are in. List source code. Number of lines listed when list command given.
Common gdb commands Examine Variables print variable-name p variable-name p file-name::variable-name p 'file-name'::variable-name Start and Stop run r run command-linearguments run < infile > outfile continue c kill k quit q Print value stored in variable. Start program execution from the beginning of the program. The command break main will get you started. Also allows basic I/O redirection. Continue execution to next break point. Stop program execution. Exit GDB debugger.
gdb Quick reference guid run run args break function break linenum break if cond clear funct delete bnum disable bnum enable bnum condition bnum commands bnum cont -- run the program -- run program with command line args. -- set breakpoint at function entry -- set breakpoint at line -- set breakpoint; break if condition -- remove breakpoint at function entry -- delete breakpoint bnum -- disable breakpoint bnum -- enable breakpoint bnum -- set conditions for breakpoint bnum -- set commands for breakpoint bnum -- continue execution to next break point
gdb Quick reference guid next nexti step stepi print expr info dat list where kill quit -- step next source level statement or function -- step next machine instruction or function -- step next source level statement -- step next machine instruction -- print value of expression -- information about break, display, registers, functions, variables -- list ten source lines -- show call stack -- stop program execution -- exit gdb Note: pressing Enter repeats the last command.