Question
/* File is average.c * * Purpose: * test the function average() * */ #include stdio.h /* computes the average of two numbers * *
/* File is average.c * * Purpose: * test the function average() * */
#include "stdio.h"
/* computes the average of two numbers * * * * input: * x - the first number * y - the last number * * output: * average - the average of the two numbers * * return: * none * * *
*/
int average(int x, int y, int *average) {
int sum; int i;
sum = 0; // compute the sum sum = x; for (i = 0; i
// compute the average *average = sum /2; return (0); }
int main(int argc, char **argv)
{ int x; int y; int result;
printf("Testing the average function ");
// test 1 x = 5; y = 7; printf("test 1: testing average(%d, %d) answer should be 6 ",x,y); average(x,y, &result); printf("average(%d, %d)=%d ",x, y, result);
// test 2 x = 5; y = 4; printf("test 2: testing average(%d, %d) answer should be 4.5 ",x,y); average(x,y, &result); printf("average(%d, %d)=%d ",x, y, result);
return(0); }
1. Compile the code and create an executable file average gcc -o average average.c Fix any errors that may arise Run the code in GDB using gdb average You will get the gdb copyright notice and it will end with a prompt informing us that GDB is ready for execution. 2. 3. 4. 5. Try the list command to show the code (gdb) list A message will appear saying there is no symbol table. Quit GDB by typing quit (or q for short) 6. 7. The problem is that a debugging program such as GDB requires additional information in order to allow the program to navigate through the code. The additional information is termed a symbol table ( Executable Machine Code Code File (average.c) Compiler Symbol Table Figure 1: compiler creating code file and symbol table This symbol table is created by the compiler by using the -g option. For example gcc -g-o average average.c The result is a larger executable code size. Below you can see the difference between compiling the code with the -g option and without. Compiling the file average.c without the -g option results in an executable size of 8448 bytes and in code size of 12536 when compiling with the -g option. gcc -o average average.c l-la average rwxr-xr-x 1 student student 8448 Sep 24 14:05 average* gcc -g-o average average.c >l-la average -rwxr-xr-x 1 student student 12536 Sep 24 14:05 average* 2.2 Navigating the source code Commands used in this part of the tutorial: start, next, list, continue In this part of the tutorial you will navigate the code. Here you will start inspecting the code line by line 1. Begin the gdb execution by using the start command - gdb will stop at the fist line of code in the 2. 3. function main) Execute the next line of code by typing next (or n for short) Note that at anytime during the execution you can look at the code this can be done by Type list (or I for short) to see the lines of code. Typing /again will present the next few lines. Note that if you wish to see other lines of code or go back you can select a line number and the code around this line number will be presented. Try the following to see the code around line 60 list 60 a. b. 4. 5. 6. Repeat the command next until you reached the code line average(x, y, &result); Type next again-note that the function the computes the average was skipped Type continue to terminate the execution of the program 2.3 Exploring the function average() Commands used in the part of the tutorial: step, finish Here you will explore the function average() by entering it. 1. Repeat steps 1-4 of Section 2.2 2. This time instead of skipping the function average) we will enter it by typing step You should see the parameters and the values that are passed to the function average(). 3. Type next 4. Type next several times until you are in the middle of the for loop (line 39) 5 Instead of typing next, next... until the code reaches the end of the line, one can leave the function by typing finish Type c to finish the program execution. 6. 2.4 Managing breakpoints Commands used in the part of the tutorial: breakpoint, info, delete Breakpoints are commands that instruct the debugger to stop at particular lines of code while the code is running. Namely, instead of walking through the code one line at a time one can tell the debugger to execute the code until it reaches the first break point. Breakpoints can be set by defining lines of code or by functions. 2.4.1 Setting breakpoints: 1. Type break main. This will set a breakpoint at the first line of the function main). 2. Run the program by typing r. The program will stop at the first line of main 3. Type list 56 to see the program around code line 56. Inspect the code 4. Type break 56 to set up a break point at line 56. 5. Type continue or c to continue the execution. The program should stop at line 56. 6. Type c again. The program should terminate. Managing breakpoints 7. To navigate directly to the function average one can set a breakpoint at the beginning of the function. This is done by typing break average Start the program again by typing r 8. The program stops at the first breakpoint that we set and not at the function average ). We will remedy this by disabling some of the breakpoints. First we need to see which breakpoints are set. 9. Type info breakpoints. This will list all the breakpoints. There should be three breakpoints-the 10. Type disable 1 2 to disable breakpoints 1 and 2. (your numbers may be different so use those 11. Type r to restart the program. You will be asked to confirm it. The program should stop at the 12. To enable the breakpoint at line 56 use the command enable. first two are in main and one in average. instead) beginning of the function average () List the breakpoints using info breakpoints Identify the number of the breakpoint at line 56. Assuming that the breakpoint number is 2 then Enable the breakpoint using enable 2 a. b. c. 13. Test the code by typing r Deleting breakpoints Deleting breakpoints is done using the command delete and the breakpoint number 14. List the breakpoints using info breakpoints (assuming that the breakpoint at average) is the breakpoint number 3) 15. Type delete 3 the breakpoint at average() 16. Run the code using r. The program should stop at line 56 17. Type c and the program should terminate 18. Type delete to delete all breakpointsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started