COMP XXXXXXXXXXTutorial 8 SpecificationCOMP 2401 − "Introduction to Systems Programming"Tutorial 8 - Debugging with GDBTutorial 8Debugging with GDBLearning ObjectivesAfter this tutorial,...

complete this tutorial


COMP 2401 - Tutorial 8 Specification COMP 2401 − "Introduction to Systems Programming" Tutorial 8 - Debugging with GDB Tutorial 8 Debugging with GDB Learning Objectives After this tutorial, you will be able to: ● Use GDB to help debug your programs by ○ Setting breakpoints ○ Printing variable values at points during execution ○ Navigating the function call stack at points during execution Tutorial In order to receive full marks, you must complete everything in the “Debugging” section. Part marks may be provided if good progress was made toward completing the tutorial, at TA discretion. Note that a large portion of this tutorial is about learning to use GDB. You are advised to follow along with this section to better prepare you for your remaining course work, as well as to help you more quickly fix the bugs in examples 1-6. If you are already comfortable using the GDB tool, skim the “Introduction to GDB” notes to see if there is anything new to learn before continuing to fix the bugs in examples 1-6. Introduction to GDB GDB (the GNU Project Debugger) is a debugger that allows you to see what is happening inside a program while it executes. It can be used for debugging programs written in different languages including C and C++. GDB can start a program, specifying information that might be needed for running the program. It can also stop the program at a specific line and on specific conditions. When the program stops, GDB lets you examine the running program by printing the value of the variables inside the program. In this tutorial, you will see how to do the above tasks. Then you are asked to use GDB and find errors in some examples and fix them. 1. Download the file T08.tar.bz2 from the tutorial page on Brightspace. Extract and read through the tutorial files. 2. Create the example1 executable using the Makefile and then run gdb with this executable: make example1 gdb ./example1 The gdb program is now running and it should look something like: ~/T08$ gdb ./example1 GNU gdb (Ubuntu 7.11.1 0ubuntu1~16.5) 7.11.1 Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64 linux gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./example1...done. (gdb) COMP 2401 Tutorial #8 1 COMP 2401 − "Introduction to Systems Programming" Tutorial 8 - Debugging with GDB 3. Your program is now loaded into gdb and ready to be run (executed). The list command in gdb will give you a partial listing of the source code. Try typing: (gdb) list The (gdb) above denotes the gdb prompt (you do not type this). It should look something like: (gdb) list 1 #include 2 #include 3 4 void printBinary(int); 5 int power(int, int); 6 7 int main() { 8 int n; 9 printf("Enter an integer number:"); 10 scanf("%d", &n); (gdb) The list command shows about 10 consecutive lines of code. Notice that it didn't start at the beginning of the code. You can specify where in the code you want to list by giving a line number or a function name (only if you used g when compiling). 4. Try the following commands: (gdb) list 20 (gdb) list printBinary When you use the list command, it shows about 10 lines of code centred around the part you specify. Note: gdb lets you be lazy. It allows you to use prefixes of commands, as long as the prefix uniquely specifies that command. For example, since there is only one command starting with the letter L, which is list, you can use l, li, lis and list to invoke the list command. So, all of the following are the same: (gdb) l (gdb) list (gdb) lis 5. Now let’s execute the program by giving gdb the command run. It executes your program as if it were being executed from the shell. When asked for input, you type the input in. When there is output printed out, it shows up on the screen. For example, running example1 looks like: (gdb) run Starting program: /home/student/T08/example1 Enter an integer number: You can also pass command line arguments and do input/output redirection when executing a program in gdb . This is especially useful when (re)typing in input is tedious... COMP 2401 Tutorial #8 2 COMP 2401 − "Introduction to Systems Programming" Tutorial 8 - Debugging with GDB For example, (using the in.txt file) (gdb) run < in.txt ok, you now know how to list your code and how to run it. let's look at our first bug in the code. try : (gdb) run starting program: /home/student/t08/example1 enter an integer number: 123 after entering 123 we would see: enter an integer number:123 decimal = 123 program received signal sigfpe, arithmetic exception. 0x0000000000400733 in printbinary (n=123) at example1.c:31 31 printf("%d", n % pow); (gdb) 6. now, what does this mean? it tells us that we have an arithmetic exception. it tells us that it occurred inside the function printbinary and it tells us the value of the input parameter (n=123). it also tells us what line the code crashed on (line 31 of the file example1.c) and then shows us that line. to see the code around it, try: (gdb) list 31 7. we can also probe the "state" of the program when the code crashed. the print command will display the current value of a variable. for example, try: (gdb) print pow (gdb) print n the print command also lets us print expressions. for example, we can look inside arrays, ask for memory locations, and dereference pointers. try the following: (gdb) print n*pow (gdb) print (n+10)/(pow+1) (gdb) print &n notice that whenever you print something, the line starts with $n for some integer n. this is a label for the value of the expression that was printed and can be used to access this value later. for example, try: (gdb) print $4 this is the value displayed the 5th time you called print (counting starts at zero). comp 2401 tutorial #8 3 comp 2401 − "introduction to systems programming" tutorial 8 - debugging with gdb 8. hopefully you can see what the problem with the code is already. but most bugs will not be so simple. what can we do to try and find more difficult bugs. we can look at the function call stack and see what it looked like when the crash occurred. to view the function call stack, we use the "backtrace" command, which in our case should look something like: (gdb) backtrace #0 0x0000000000400733 in printbinary (n=123) at example1.c:31 #1 0x00000000004006af in main () at example1.c:13 (gdb) 9. we can move into any stack frame that we wish, with the frame command, to see what the state is. for example, we can move into stack frame 1 (which is the main() function). (gdb) frame 1 #1 0x00000000004006af in main () at example1.c:13 13 printbinary(n); (gdb) list 8 int n; 9 printf("enter an integer number:"); 10 scanf("%d", &n); 11 printf("decimal = %d\n", n); 12 printf("binary = 0b"); 13 printbinary(n); 14 printf("\n"); 15 return 0; 16 } 17 (gdb) this tells us that we are currently (executing) at line 13 of the main() function. we can use the print command to view any variables in this current stack frame. that is, we can ask what the values of all the local variables in this function are (even though the execution of the program is not really in this stack frame). to get back to where we started from, (the top of the function call stack) we use: (gdb) frame 0 10. now let's see how we can stop our program before it crashes (where the exception happens), look at the state, and then step through the program (to hopefully find our bug). the break command tells gdb to stop the program when it gets to a certain line (or reaches a function). it specifies a breakpoint for the code. for example, we know that our code crashed on line 31. we can set a breakpoint on this line as follows: (gdb) break 31 breakpoint 1 at 0x40072f: file example1.c, line 31. (gdb) comp 2401 tutorial #8 4 comp 2401 − "introduction to systems programming" tutorial 8 - debugging with gdb notice that it gives a number to the breakpoint (1 in this case). now run the program and again give 123 as input integer number. you should see something like: (gdb) run starting program: /home/student/t08/example1 enter an integer number:123 decimal = 123 breakpoint 1, printbinary (n=123) at example1.c:31 31 printf("%d", n % pow); (gdb) 11. at this point, you can probe the state of the program. (use print to see what some values are, for example). after looking at the state of the program, we can now use continue, step, or next to move on and resume execution of the program. each of these does something different. (gdb) continue will continue the execution of the code until it reaches another breakpoint, crashes, or the program ends. (gdb) step will execute the next line of code, and then stop. if the previous line of code was a function call, step takes you inside the new function and stops at the first line of code of the new function. (gdb) next will execute the next line of code inside the same function. if the previous line of code was a function call, it executes the entire function and brings you to the next line of code after the function returns. 12. try these three different ways of continuing execution when your program is using example1 with 123 as the input number. to see the difference between step and next, run example1 with a breakpoint at line 13 (where printbinary has been called). when gdb stops at line 13, execute step and then use list to see what the current line is. try the same again, but this time execute next when you reach line 13 and then again see the current line by list. 13. more about break. you can set a temporary breakpoint with tbreak. (gdb) tbreak 31 will set a breakpoint at line 31 and will stop execution the first (and only the first) time the code reaches this line. you can list all the breakpoints you have set with: (gdb) info breakpoints you can disable any breakpoint with the disable command. for example, to disable breakpoint 2, you would use: (gdb) disable 2 comp 2401 tutorial #8 5 comp 2401 − "introduction to systems programming" tutorial 8 - debugging with gdb for example, try: (gdb) break 31 (gdb) break 10 (gdb) tbreak 33 (gdb) info breakpoints (gdb) disable 2 (gdb) info breakpoints sometimes you might want to skip a breakpoint one more time. (gdb) ignore 1 3 tells gdb to ignore breakpoint number 1 a total of 3 times. tutorial problems: debugging 1. make example1. find and fix any bugs. 2. make example2. find and fix any bugs. 3. make example3. find and fix any bugs. 4. make example4. find and fix any bugs. 5. make example5. find and fix any bugs. 6. make example6. find and fix any bugs. exercise these exercises are optional, but are provided to give you some additional practice working with common operations that you will encounter while using pointers that might be tricky when you first work with them. it is a bit unrelated to the tutorial, but these are some tricky pointers, and so you can try using gdb to help you accomplish these tasks. use the following structure and implement a binary search tree. assume that the head of the list will be a treenodetype pointer. typedef struct treenode { int data; struct treenode *right, *left; } treenodetype; you should write insert, delete, search, and print functions to support the bst. comp 2401 tutorial #8 6 http://en.wikipedia.org/wiki/binary_search_tree comp 2401 − "introduction to systems programming" tutorial 8 - debugging with gdb saving and submission in-person attendance: submission is optional, as you can be checked off in class, but you must still complete the required portion of the tutorial for marks. asynchronous: if you are completing the work on your own at home, make sure to follow the submission requirements. 1. create an archive to submit your files, and include all of the files that you worked with in the tutorial. a. they may be submitted as a .tar, .tar.gz, bz2, or .tgz file b. make sure to include all of the code needed to run your program 2. for full marks you should have completed all problems in the tutorial section (and they should be seen upon running the program). 3. for part-marks you should have attempted to complete most of the tutorial. grades for part-marks will be at ta discretion. comp 2401 tutorial #8 7 in.txt="" ok,="" you="" now="" know="" how="" to="" list="" your="" code="" and="" how="" to="" run="" it.="" let's="" look="" at="" our="" first="" bug="" in="" the="" code.="" try="" :="" (gdb)="" run="" starting="" program:="" home/student/t08/example1="" enter="" an="" integer="" number:="" 123="" after="" entering="" 123="" we="" would="" see:="" enter="" an="" integer="" number:123="" decimal="123" program="" received="" signal="" sigfpe,="" arithmetic="" exception.="" 0x0000000000400733="" in="" printbinary="" (n="123)" at="" example1.c:31="" 31="" printf("%d",="" n="" %="" pow);="" (gdb)="" 6.="" now,="" what="" does="" this="" mean?="" it="" tells="" us="" that="" we="" have="" an="" arithmetic="" exception.="" it="" tells="" us="" that="" it="" occurred="" inside="" the="" function="" printbinary="" and="" it="" tells="" us="" the="" value="" of="" the="" input="" parameter="" (n="123)." it="" also="" tells="" us="" what="" line="" the="" code="" crashed="" on="" (line="" 31="" of="" the="" file="" example1.c)="" and="" then="" shows="" us="" that="" line.="" to="" see="" the="" code="" around="" it,="" try:="" (gdb)="" list="" 31="" 7.="" we="" can="" also="" probe="" the="" "state"="" of="" the="" program="" when="" the="" code="" crashed.="" the="" print="" command="" will="" display="" the="" current="" value="" of="" a="" variable.="" for="" example,="" try:="" (gdb)="" print="" pow="" (gdb)="" print="" n="" the="" print="" command="" also="" lets="" us="" print="" expressions.="" for="" example,="" we="" can="" look="" inside="" arrays,="" ask="" for="" memory="" locations,="" and="" dereference="" pointers.="" try="" the="" following:="" (gdb)="" print="" n*pow="" (gdb)="" print="" (n+10)/(pow+1)="" (gdb)="" print="" &n="" notice="" that="" whenever="" you="" print="" something,="" the="" line="" starts="" with="" $n="" for="" some="" integer="" n.="" this="" is="" a="" label="" for="" the="" value="" of="" the="" expression="" that="" was="" printed="" and="" can="" be="" used="" to="" access="" this="" value="" later.="" for="" example,="" try:="" (gdb)="" print="" $4="" this="" is="" the="" value="" displayed="" the="" 5th="" time="" you="" called="" print="" (counting="" starts="" at="" zero).="" comp="" 2401="" tutorial="" #8="" 3="" comp="" 2401="" −="" "introduction="" to="" systems="" programming"="" tutorial="" 8="" -="" debugging="" with="" gdb="" 8.="" hopefully="" you="" can="" see="" what="" the="" problem="" with="" the="" code="" is="" already.="" but="" most="" bugs="" will="" not="" be="" so="" simple.="" what="" can="" we="" do="" to="" try="" and="" find="" more="" difficult="" bugs.="" we="" can="" look="" at="" the="" function="" call="" stack="" and="" see="" what="" it="" looked="" like="" when="" the="" crash="" occurred.="" to="" view="" the="" function="" call="" stack,="" we="" use="" the="" "backtrace"="" command,="" which="" in="" our="" case="" should="" look="" something="" like:="" (gdb)="" backtrace="" #0="" 0x0000000000400733="" in="" printbinary="" (n="123)" at="" example1.c:31="" #1="" 0x00000000004006af="" in="" main="" ()="" at="" example1.c:13="" (gdb)="" 9.="" we="" can="" move="" into="" any="" stack="" frame="" that="" we="" wish,="" with="" the="" frame="" command,="" to="" see="" what="" the="" state="" is.="" for="" example,="" we="" can="" move="" into="" stack="" frame="" 1="" (which="" is="" the="" main()="" function).="" (gdb)="" frame="" 1="" #1="" 0x00000000004006af="" in="" main="" ()="" at="" example1.c:13="" 13="" printbinary(n);="" (gdb)="" list="" 8="" int="" n;="" 9="" printf("enter="" an="" integer="" number:");="" 10="" scanf("%d",="" &n);="" 11="" printf("decimal="%d\n"," n);="" 12="" printf("binary="0b");" 13="" printbinary(n);="" 14="" printf("\n");="" 15="" return="" 0;="" 16="" }="" 17="" (gdb)="" this="" tells="" us="" that="" we="" are="" currently="" (executing)="" at="" line="" 13="" of="" the="" main()="" function.="" we="" can="" use="" the="" print="" command="" to="" view="" any="" variables="" in="" this="" current="" stack="" frame.="" that="" is,="" we="" can="" ask="" what="" the="" values="" of="" all="" the="" local="" variables="" in="" this="" function="" are="" (even="" though="" the="" execution="" of="" the="" program="" is="" not="" really="" in="" this="" stack="" frame).="" to="" get="" back="" to="" where="" we="" started="" from,="" (the="" top="" of="" the="" function="" call="" stack)="" we="" use:="" (gdb)="" frame="" 0="" 10.="" now="" let's="" see="" how="" we="" can="" stop="" our="" program="" before="" it="" crashes="" (where="" the="" exception="" happens),="" look="" at="" the="" state,="" and="" then="" step="" through="" the="" program="" (to="" hopefully="" find="" our="" bug).="" the="" break="" command="" tells="" gdb="" to="" stop="" the="" program="" when="" it="" gets="" to="" a="" certain="" line="" (or="" reaches="" a="" function).="" it="" specifies="" a="" breakpoint="" for="" the="" code.="" for="" example,="" we="" know="" that="" our="" code="" crashed="" on="" line="" 31.="" we="" can="" set="" a="" breakpoint="" on="" this="" line="" as="" follows:="" (gdb)="" break="" 31="" breakpoint="" 1="" at="" 0x40072f:="" file="" example1.c,="" line="" 31.="" (gdb)="" comp="" 2401="" tutorial="" #8="" 4="" comp="" 2401="" −="" "introduction="" to="" systems="" programming"="" tutorial="" 8="" -="" debugging="" with="" gdb="" notice="" that="" it="" gives="" a="" number="" to="" the="" breakpoint="" (1="" in="" this="" case).="" now="" run="" the="" program="" and="" again="" give="" 123="" as="" input="" integer="" number.="" you="" should="" see="" something="" like:="" (gdb)="" run="" starting="" program:="" home/student/t08/example1="" enter="" an="" integer="" number:123="" decimal="123" breakpoint="" 1,="" printbinary="" (n="123)" at="" example1.c:31="" 31="" printf("%d",="" n="" %="" pow);="" (gdb)="" 11.="" at="" this="" point,="" you="" can="" probe="" the="" state="" of="" the="" program.="" (use="" print="" to="" see="" what="" some="" values="" are,="" for="" example).="" after="" looking="" at="" the="" state="" of="" the="" program,="" we="" can="" now="" use="" continue,="" step,="" or="" next="" to="" move="" on="" and="" resume="" execution="" of="" the="" program.="" each="" of="" these="" does="" something="" different.="" (gdb)="" continue="" will="" continue="" the="" execution="" of="" the="" code="" until="" it="" reaches="" another="" breakpoint,="" crashes,="" or="" the="" program="" ends.="" (gdb)="" step="" will="" execute="" the="" next="" line="" of="" code,="" and="" then="" stop.="" if="" the="" previous="" line="" of="" code="" was="" a="" function="" call,="" step="" takes="" you="" inside="" the="" new="" function="" and="" stops="" at="" the="" first="" line="" of="" code="" of="" the="" new="" function.="" (gdb)="" next="" will="" execute="" the="" next="" line="" of="" code="" inside="" the="" same="" function.="" if="" the="" previous="" line="" of="" code="" was="" a="" function="" call,="" it="" executes="" the="" entire="" function="" and="" brings="" you="" to="" the="" next="" line="" of="" code="" after="" the="" function="" returns.="" 12.="" try="" these="" three="" different="" ways="" of="" continuing="" execution="" when="" your="" program="" is="" using="" example1="" with="" 123="" as="" the="" input="" number.="" to="" see="" the="" difference="" between="" step="" and="" next,="" run="" example1="" with="" a="" breakpoint="" at="" line="" 13="" (where="" printbinary="" has="" been="" called).="" when="" gdb="" stops="" at="" line="" 13,="" execute="" step="" and="" then="" use="" list="" to="" see="" what="" the="" current="" line="" is.="" try="" the="" same="" again,="" but="" this="" time="" execute="" next="" when="" you="" reach="" line="" 13="" and="" then="" again="" see="" the="" current="" line="" by="" list.="" 13.="" more="" about="" break.="" you="" can="" set="" a="" temporary="" breakpoint="" with="" tbreak.="" (gdb)="" tbreak="" 31="" will="" set="" a="" breakpoint="" at="" line="" 31="" and="" will="" stop="" execution="" the="" first="" (and="" only="" the="" first)="" time="" the="" code="" reaches="" this="" line.="" you="" can="" list="" all="" the="" breakpoints="" you="" have="" set="" with:="" (gdb)="" info="" breakpoints="" you="" can="" disable="" any="" breakpoint="" with="" the="" disable="" command.="" for="" example,="" to="" disable="" breakpoint="" 2,="" you="" would="" use:="" (gdb)="" disable="" 2="" comp="" 2401="" tutorial="" #8="" 5="" comp="" 2401="" −="" "introduction="" to="" systems="" programming"="" tutorial="" 8="" -="" debugging="" with="" gdb="" for="" example,="" try:="" (gdb)="" break="" 31="" (gdb)="" break="" 10="" (gdb)="" tbreak="" 33="" (gdb)="" info="" breakpoints="" (gdb)="" disable="" 2="" (gdb)="" info="" breakpoints="" sometimes="" you="" might="" want="" to="" skip="" a="" breakpoint="" one="" more="" time.="" (gdb)="" ignore="" 1="" 3="" tells="" gdb="" to="" ignore="" breakpoint="" number="" 1="" a="" total="" of="" 3="" times.="" tutorial="" problems:="" debugging="" 1.="" make="" example1.="" find="" and="" fix="" any="" bugs.="" 2.="" make="" example2.="" find="" and="" fix="" any="" bugs.="" 3.="" make="" example3.="" find="" and="" fix="" any="" bugs.="" 4.="" make="" example4.="" find="" and="" fix="" any="" bugs.="" 5.="" make="" example5.="" find="" and="" fix="" any="" bugs.="" 6.="" make="" example6.="" find="" and="" fix="" any="" bugs.="" exercise="" these="" exercises="" are="" optional,="" but="" are="" provided="" to="" give="" you="" some="" additional="" practice="" working="" with="" common="" operations="" that="" you="" will="" encounter="" while="" using="" pointers="" that="" might="" be="" tricky="" when="" you="" first="" work="" with="" them.="" it="" is="" a="" bit="" unrelated="" to="" the="" tutorial,="" but="" these="" are="" some="" tricky="" pointers,="" and="" so="" you="" can="" try="" using="" gdb="" to="" help="" you="" accomplish="" these="" tasks.="" use="" the="" following="" structure="" and="" implement="" a="" binary="" search="" tree.="" assume="" that="" the="" head="" of="" the="" list="" will="" be="" a="" treenodetype="" pointer.="" typedef="" struct="" treenode="" {="" int="" data;="" struct="" treenode="" *right,="" *left;="" }="" treenodetype;="" you="" should="" write="" insert,="" delete,="" search,="" and="" print="" functions="" to="" support="" the="" bst.="" comp="" 2401="" tutorial="" #8="" 6="" http://en.wikipedia.org/wiki/binary_search_tree="" comp="" 2401="" −="" "introduction="" to="" systems="" programming"="" tutorial="" 8="" -="" debugging="" with="" gdb="" saving="" and="" submission="" in-person="" attendance:="" submission="" is="" optional,="" as="" you="" can="" be="" checked="" off="" in="" class,="" but="" you="" must="" still="" complete="" the="" required="" portion="" of="" the="" tutorial="" for="" marks.="" asynchronous:="" if="" you="" are="" completing="" the="" work="" on="" your="" own="" at="" home,="" make="" sure="" to="" follow="" the="" submission="" requirements.="" 1.="" create="" an="" archive="" to="" submit="" your="" files,="" and="" include="" all="" of="" the="" files="" that="" you="" worked="" with="" in="" the="" tutorial.="" a.="" they="" may="" be="" submitted="" as="" a="" .tar,="" .tar.gz,="" bz2,="" or="" .tgz="" file="" b.="" make="" sure="" to="" include="" all="" of="" the="" code="" needed="" to="" run="" your="" program="" 2.="" for="" full="" marks="" you="" should="" have="" completed="" all="" problems="" in="" the="" tutorial="" section="" (and="" they="" should="" be="" seen="" upon="" running="" the="" program).="" 3.="" for="" part-marks="" you="" should="" have="" attempted="" to="" complete="" most="" of="" the="" tutorial.="" grades="" for="" part-marks="" will="" be="" at="" ta="" discretion.="" comp="" 2401="" tutorial="" #8="">
Nov 29, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here