Your cart is currently empty!
GNU Debugger (GDB) https://www.gnu.org/software/gdb/ A debugger is used to analyze program execution in a step-by-step and detailed manner. It is used to find bugs in a program. Using a debugger, we can execute a program partially and view the status of the variables and resources being used the program to identify any discrepancies. GDB is…
GNU Debugger (GDB) https://www.gnu.org/software/gdb/
A debugger is used to analyze program execution in a step-by-step and detailed manner. It is used to find bugs in a program. Using a debugger, we can execute a program partially and view the status of the variables and resources being used the program to identify any discrepancies. GDB is an open source, freely available debugger which can be used for multiple languages.
GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:
GNU Compiler Collection (GCC)
GCC is an open source compiler system used to compile C/C++ programs: https://gcc.gnu.org/
Objective:
You will learn how to use GDB to debug a C program.
Procedure:
Answer: ______________________________________________________
> break lineNumber
or
> b lineNumber
Example: This will put a breakpoint on line 6 > break 6
Now if you run the program using the start command, it will pause at line 6. You can continue execution (till end or the next breakpoint) using the continue command.
> step
This will execute only the next line of code
or
> step numberOfLines
E.g. > step 3 will execute next three lines of code
> print a
You can view all local variable values using the command:
> info locals
What are the values of variables c and d at the start of line 8 (before executing line 8)?
Answers: _______________________________________________________________
> info registers
The code lab1.c is also shown on the next page for your reference.
Show your labTA the output of your corrected program.
Marking Scheme: Report – 6 marks; Correct output – 4 marks; Total: 10 marks.
Program lab1a.c
#include <stdio.h>
int main(void){ int a = 10; int b = 10; int c = a+b; int d = a-b; int e = a/d; int f = a*b;
printf(“Arithmetic operations:\n”); printf(“a+b = %d\n”, c); printf(“a-b = %d\n”, d); printf(“b/a = %d\n”, e); printf(“a*b = %d\n\n”, f);
return 0; }
|
Program lab1b.c
#include <stdio.h>
#define MAX 10
int readArray(int [], int); void printArray(int [], int); void reverseArray(int [], int);
int main(void) { int array[MAX], numElements;
numElements = readArray(array, MAX); reverseArray(array, numElements); printArray(array, numElements);
return 0; }
int readArray(int arr[], int limit) { int i, input;
printf(“Enter up to %d integers, terminating with a negative integer.\n”, limit); i = 0; scanf(“%d”, &input); while (input >= 0) { arr[i] = input; i++; scanf(“%d”, &input); } return i; }
void reverseArray(int arr[], size_t size) { // complete the function body }
void printArray(int arr[], int size) { int i;
for (i=0; i<size; i++) { printf(“%d “, arr[i]); } printf(“\n”); }
|