Question: home / study / engineering / computer science / computer science questions and answers / how do you set a break point in gdb c

home / study / engineering / computer science / computer science questions and answers / how do you set a break point in gdb c programming for the function read_values? this is what ...

Question: How do you set a break point in gdb C programming for the function read_values? This is what i ha...

How do you set a break point in gdb C programming for the function read_values? This is what i have..

Recall what causes segmentation fault and bus errors from lecture. Common cause is an invalid pointer or address that is being dereferenced by the C program. Use the program average.c from the assignment page for this exercise. The program is intended to find the average of all the numbers inputted by the user. Currently, it has a bus error if you input more than one number. Load average.c into GDB with all the appropriate information and run it. GDB will trap on the segmentation fault and give you back the prompt. First find where the program execution ended by using backtrace(bt as shortcut) which will print out a stack trace. Find the exact line that caused the segmentation fault. Answer the following questions:

1.What line caused the segmentation fault?

2.How do you fix the line so it works properly?

You can recompile the code and run the program again. The program now reads all the input values but the average calculated is still incorrect. Use GDB to fix the program by looking at the output of read_values. To do this, either set a breakpoint using the line number or set a breakpoint in the read_values function. Then continue executing to the end of the function and view the values being returned.

 home / study / engineering / computer science / computer science I added pointers to sum but the average still does not pop up it comes out as zero. I added *sum and changed input to have have a value of -1

progr amming 1s the bestlL progr amming Tanguage 1s the betl programming 1S Caverage.cx punishment.c CappendTest.c IntegerClass.py .. the best! C programming is the best [Inferior 1 (process 28) exited normally 1 #include (gdb) q root@DESKTOP-77MI8N4:/mnt/c/Users/ TPA Labl 2.docx appendTest.c average.c punishment test a.out root@DESKTOP-77MI8N4:/mnt/c/Users/ average g root@DESKTOP-77MI8N4:/mnt/c/Users/ GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.8.20188409-git Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or 1ater 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-1inux-gnu" . Type "show configuration" for configuration details For bug reporting instructions, please see: http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: http://www.gnu.org/software/gdb/documentation/> For help, type "help" Type "apropos word" to search for commands related to "word" Reading symbols from average...done (gdb) run Starting program: /mnt/c/Users//Desktop/CSE31/Lab_2/average Enter input values (enter to finish): 4 Desktop/CSE31/Lab_2ft dir Read a set of values From the user Store the sum in the sum variable and return the number of averagO pointers.c punishment.c /Desktop/CSE 3 1 /Lab-24 gcc average . c -o 7 int read_values (double sum)( /Desktop/CSE31/Lab-2# gdb average int values-e,input-; 10 printf( "Enter input values (enter 8 to finish): ); scanf("d" ,&input); 12 while(input!-0) values++ sum input scanf"%d ,&input); 14 15 16 17 18 return values 20 int main) double sum-0 int values values read_values (sum); printf("Average : return 23 24 25 26 27 28 %g " , sum/values); 7 Average: e [Inferior 1 (process 40) exited normally]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!