1. Engineering
  2. Computer Science
  3. starter code include ltstdiohgt void swapint int int main ...

Question: starter code include ltstdiohgt void swapint int int main ...

Question details

8. Problem E Pass-by-value, and trace a program with debugger 8.1 Specification In this exercise you will practice tracing/debugging a program using a software tool called debugger, rather than using print statements. The key technique of debugging a program is to examine the values of variables during program execution. With a debugger, you can do this by setting several breakpoints in the program. The program will pause execution at the breakpoints and you can then view the current values of the variables You will use a GNU debugger call gdb. It is a command line based debugger but also comes with a simple text-based gui (tui) To debug a C program using gdb, you need to compile the program with -g flag 8.2 Implementation Download the program swap.c, and compile using gcc -g swap.c. Then invoke gdb by issuing gdb -tui a.out. A window with two panels will appear. The upper panel displays the source code and the lower panel allows you to enter commands. Maximize the terminal and use arrow keys to scroll the upper panel so you can see the whole source code First we want to examine the values of variables mainA and mainB after initialization. So we set a breakpoint at the beginning of line 11 (before line 11 is executed) by issuing break 11 Observe that a B+ symbol appears on the left of line 11. We want to trace the values of variables x and y defined in function swap, both before and after swapping. So we set breakpoints at (the beginning of) line 18 and line 21. Finally we set a breakpoint at line 12 so that we can trace the value of mainA and mainB after the function call When the program pauses at a breakpoint, you can view the current values of variables with the print or display or even printf command8.3 Sample inout/output red 64 % gcc -Wall -g swap, c red 65 gdb-tui a. out Reading symbols from a.out. . .done (gdb) break 11 Breakpoint 1 at 0x400488: file swap.c, line 11 (gdb) break 18 Breakpoint 2 at 0x4004a3: file swap.c, line 18 (gdb) break 21 Breakpoint 3 at 0x4004b5: file swap.c, line 21. (gdb) break 12 Breakpoint 4 at 0x400497: file swap.c, line 12 (gdb) run Starting program: /eecs/home/huiwang/a.out /run the program until the first breakpoint. Notice the > sign on the left of the upper pane Breakpoint 1, main ) at swap.c:11 (gdb) display mainA mainA? (gdb) display mainB mainB ? (gdb) continue Continuing What do you get for mainA and mainB? /*continue execution to the next breakpoint. Notice the position of >sign / Breakpoint 2, swap (gdb) display x x? (gdb) displayY (x-1, y-20000) at swap.c:18 What do you get for x and y? (gdb) display mainA What do you get for mainA and (gdb) display mainB mainB, and why? (gdb) continue Continuing Breakpoint 3, swap (x-20000, y-1) at swap.c:21 (gdb) display x What do you get for x and y? Are they swapped? (gdb) display y (gdb) continue Continuing Breakpoint 4, main at swap.c:12 (gdb) display mainA mainA = ? (gdb) display mainB mainB? (gdb) display x What do you get for mainA and mainB? Are they swapped? What do you get here, and why? (gdb) displayY (gdb) quit 10 8.4 Submission Write your answers into a text file, and submit it. Or submit a snapshot of your gdb session

starter Code:

#include <stdio.h>


void swap(int, int);


int main( ) {
  int mainA, mainB;
  mainA = 1;
  mainB = 20000;
  swap(mainA, mainB);
  return 0;
}


void swap(int x, int y)
{
   int temp = x;
   x = y;
   y = temp;
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution