0 < n <= 20The program then reads in n integers and saves them in an array. You may assume that the number of elements in the array will not exceed 20. Name your program lab3.c.
help help list display ten more lines list <line number> display ten lines centered around <line number> list - display ten lines previous to the lines just printed print <variable name> print the value of a variable break <function name> set a breakpoint at the start of a function break <line number> set a breakpoint at the specified line cont continue execution after a breakpoint clear <function name> remove all breakpoints from function run run program with optional arguments step resume execution for just a single statement quit exit the debugger backtrace shows layers of functions you are intoA breakpoint is just what the name implies - a point at which you break out of the currently-executing program and are brought into the debugger. You can then check out the state of various variables to make sure they contain the values they should contain at that point. If you reach a breakpoint and you find something amiss, you know that there is a bug in your program in one of the instructions that executed prior to the breakpoint. If, on the other hand, everything looks OK, you can narrow down your search for bugs in the instructions that execute after the breakpoint. Loops are usually good places to put breakpoints; start by placing a breakpoint immediately before a loop and immediately after. If it looks like there may be a problem inside a loop, placing a breakpoint within the loop allows you to stop after each iteration and make sure the loop control variables and other variables contain the correct values.
When you want to use the debugger, you must first tell the compiler to prepare the program for debugging. So recompile your program with the command gcc -Wall -g lab3.c. The -g switch tells the compiler to add information about line numbers, variable names and locations, and other stuff to the object file so that the debugger can find its way around the program. Start the debugger by typing gdb a.out. You'll see a welcoming message followed by the debugger's prompt:
(gdb)
list
This will display 10 lines around the next line to be executed, in this case, the function main..
list 4, 8
will list lines 4-8 inclusive. You can use the command help list to discover more listing options. Now run your program by typing the command:
run
You will see that the program runs normally, just as if you had tried to run it in a command shell. Type help run for more information.
Set a breakpoint at your return 0 statement. To do this, type
break x
where x is the line number of your return statement. Use run to execute your program again. This time execution stops at the breakpoint. Now that the program is suspended, you can poke around your program to make sure everything looks OK.
Use the print command to verify that the array contains the values you expect it to contain. If your array name is myArray, you can display the array's values this way:
print myArray
or, to display a single array item,
print myArray[0]
Finally, continue the execution of your program after the breakpoint with the continue command:
cont
break largestItem
This will cause the program to be suspended whenever the function largestItem is called. Use run to run your program. When the program stops at the breakpoint, single-step through the next line by typing
step
This causes execution for just one instruction, and the next line to be executed is displayed (not the line you just executed). Use step a few more times, verifying the contents of variables with the print command as you go. It can take a lot of stepping to get through a loop; set a breakpoint at the return statement in your function, and cont execution. Convince yourself that your program is working correctly, making multiple runs on various sets of input. If you are running out of time, go to step 8. If you have at least 15 minutes left, proceed to step 7.
See you next week!