Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 colortest.c and have the executable stored in testcolor, use the following: gcc -Wall -g -o testcolor colortest.c Once you have an executable file (in this case prog.o) you can use gdb to debug it. First you must launch the debugger. To launch the debugger you type gdb For the above program, this will be gdb testcolor gdb

2 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 testcolor using the command./testcolor colordata1.txt you would could use the following command valgrind --leak-check=yes./testcolor colordata1.txt For more information about valgrind, refer to the man page: man valgrind Valgrind

3 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

4 (gdb) b main Breakpoint 1 at 0x804833b: file p18.c, line 11. To start the program use the run command: (gdb) run Starting program: /home/rlowe/cs2100/examples/p18 When the program reaches a breakpoint gdb will tell you and display the next line of code to be executed. Breakpoint 1, main () at p18.c:11 11 printf("val of ptr is %p \n", ptr); Using gdb to find the point of failure

5 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

6 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 p18.c:13 13 *ptr = 99; Using gdb to find the point of failure

7 The where command can show you where the failure occurred (along with a complete function activation trace. (gdb) where #0 0x08048357 in main () at p18.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

8 Use the q (quit) command to terminate gdb (gdb) quit The program is running. Exit anyway? (y or n) y home/rlowe/cs2100/examples ==> Using gdb to find the point of failure

9 Common gdb commands CommandDescription helpList gdb command topics. help topic-classesList gdb command within class. help commandCommand description. apropos search-word Search for commands and command topics containing search-word. info args i args List program command line arguments info breakpoints Print info about breakpoints and watchpoints info breakPrint breakpoint numbers. info break breakpoint- number Print info about specific breakpoint. info watchpointsPrint info about watchpoints

10 Common gdb commands tbreakTemporary break. Break once only. Break is then removed. See "break" above for options. watch conditionSuspend processing when condition is met. i.e. x > 5 clear clear function clear line-number Delete breakpoints as identified by command option. delete d Delete all breakpoints, watchpoints, or catchpoints. delete breakpoint-number delete range Delete the breakpoints, watchpoints, or catchpoints of the breakpoint ranges specified as arguments. disable breakpoint- number-or-range enable breakpoint- number-or-range Does not delete breakpoints. Just enables/disables them. Example: Show breakpoints: info break Disable: disable 2-9

11 Common gdb commands Break and Watch break funtion-name break line-number break ClassName::functionName Suspend program at specified function of line number. break +offset break -offset Set a breakpoint specified number of lines forward or back from the position at which execution stopped. break filename:functionDon't specify path, just the file name and function name. break filename:line-numberDon't specify path, just the file name and line number. break Directory/Path/filename.cpp:62 break line-number if conditionWhere condition is an expression. i.e. x > 5 Suspend when boolean expression is true.

12 Common gdb commands enable breakpoint-number once Enables once continue c Continue executing until next break point/watchpoint. continue numberContinue but ignore current breakpoint number times. Usefull for breakpoints within a loop. finishContinue to end of function. Line Execution step s step number-of-steps-to- perform Step to next line of code. Will step into a function. next n next number Execute next line of code. Will not enter functions.

13 Common gdb commands until until line-number Continue processing until you reacha aspecified line number. Also: function name, address, filename:function or filename:line-number. whereShows current line number and which function you are in. Source Code list l list line-number list function list - list start#,end# list filename:function List source code. set listsize count show listsize Number of lines listed when list command given.

14 Common gdb commands Examine Variables print variable-name p variable-name p file-name::variable-name p 'file-name'::variable-name Print value stored in variable. Start and Stop run r run command-line- arguments run outfile Start program execution from the beginning of the program. The command break main will get you started. Also allows basic I/O redirection. continue c Continue execution to next break point. kill k Stop program execution. quit q Exit GDB debugger.

15 gdb – Quick reference guid run -- run the program run args-- run program with command line args. break function-- set breakpoint at function entry break linenum -- set breakpoint at line break … if cond-- set breakpoint; break if condition clear funct-- remove breakpoint at function entry delete bnum-- delete breakpoint bnum disable bnum-- disable breakpoint bnum enable bnum-- enable breakpoint bnum condition bnum-- set conditions for breakpoint bnum commands bnum-- set commands for breakpoint bnum cont -- continue execution to next break point

16 gdb – Quick reference guid next -- step next source level statement or function nexti -- step next machine instruction or function step -- step next source level statement stepi -- step next machine instruction print expr -- print value of expression info data -- information about break, display, registers, functions, variables list -- list ten source lines where -- show call stack kill -- stop program execution quit -- exit gdb Note: pressing Enter repeats the last command.


Download ppt "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."

Similar presentations


Ads by Google