gdb is the gnu debugger on our cs machines. gdb is most effective when it is debugging a program...
TRANSCRIPT
![Page 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/1.jpg)
• 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 <executable>
• For the above program, this will be
gdb testcolor
gdb
![Page 2: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/2.jpg)
• 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
![Page 3: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/3.jpg)
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
![Page 4: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/4.jpg)
(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:1111 printf("val of ptr is %p \n", ptr);
Using gdb to find the point of failure
![Page 5: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/5.jpg)
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) nextval of ptr is (nil)13 *ptr = 99;
Using gdb to find the point of failure
![Page 6: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/6.jpg)
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:1313 *ptr = 99;
Using gdb to find the point of failure
![Page 7: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/7.jpg)
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 *ptrCannot access memory at address 0x0
Using gdb to find the point of failure
![Page 8: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/8.jpg)
Use the q (quit) command to terminate gdb
(gdb) quit
The program is running. Exit anyway? (y or n) yhome/rlowe/cs2100/examples ==>
Using gdb to find the point of failure
![Page 9: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/9.jpg)
Common gdb commands
Command Description
help List gdb command topics.
help topic-classes List gdb command within class.
help command Command description.
apropos search-wordSearch for commands and command topics containing search-word.
info argsi args List program command line arguments
info breakpointsPrint info about breakpoints and watchpoints
info break Print breakpoint numbers.
info break breakpoint-number Print info about specific breakpoint.
info watchpoints Print info about watchpoints
![Page 10: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/10.jpg)
Common gdb commandstbreak Temporary break. Break once only.
Break is then removed. See "break" above for options.
watch condition Suspend processing when condition is met. i.e. x > 5
clearclear functionclear line-number
Delete breakpoints as identified by command option.
deleted
Delete all breakpoints, watchpoints, or catchpoints.
delete breakpoint-numberdelete range
Delete the breakpoints, watchpoints, or catchpoints of the breakpoint ranges specified as arguments.
disable breakpoint-number-or-rangeenable breakpoint-number-or-range
Does not delete breakpoints. Just enables/disables them.Example:Show breakpoints: info breakDisable: disable 2-9
![Page 11: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/11.jpg)
Common gdb commandsBreak and Watch
break funtion-namebreak line-numberbreak ClassName::functionName
Suspend program at specified function of line number.
break +offsetbreak -offset
Set a breakpoint specified number of lines forward or back from the position at which execution stopped.
break filename:function Don't specify path, just the file name and function name.
break filename:line-number Don't specify path, just the file name and line number.break Directory/Path/filename.cpp:62
break line-number if condition Where condition is an expression. i.e. x > 5Suspend when boolean expression is true.
![Page 12: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/12.jpg)
Common gdb commandsenable breakpoint-number once
Enables once
continuec
Continue executing until next break point/watchpoint.
continue number Continue but ignore current breakpoint number times. Usefull for breakpoints within a loop.
finish Continue to end of function.
Line Execution
stepsstep number-of-steps-to-perform
Step to next line of code. Will step into a function.
nextnnext number
Execute next line of code. Will not enter functions.
![Page 13: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/13.jpg)
Common gdb commands
untiluntil line-number
Continue processing until you reacha aspecified line number. Also: function name, address, filename:function or filename:line-number.
where Shows current line number and which function you are in.
Source Code
listllist line-numberlist functionlist -list start#,end#list filename:function
List source code.
set listsize countshow listsize
Number of lines listed when list command given.
![Page 14: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/14.jpg)
Common gdb commands
Examine Variables print variable-namep variable-namep file-name::variable-namep 'file-name'::variable-name
Print value stored in variable.
Start and Stop runrrun command-line-argumentsrun < infile > outfile
Start program execution from the beginning of the program. The command break main will get you started. Also allows basic I/O redirection.
continuec
Continue execution to next break point.
killk
Stop program execution.
quitq
Exit GDB debugger.
![Page 15: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/15.jpg)
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
![Page 16: 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](https://reader036.vdocuments.mx/reader036/viewer/2022072011/56649de45503460f94adb3a3/html5/thumbnails/16.jpg)
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.