Skip to content

Lab #01: Debugging Using GDB

Week: 03

Objective

  • Learn how to use GDB to debug a C program.

Preliminary

GNU Debugger (GDB)

A debugger is used to analyse 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:

  • Start your program, specifying anything that might affect its behaviour.
  • Make your program stop on specified conditions.
  • Examine what has happened, when your program has stopped.
  • Change things in your program; so that you can experiment with correcting the effects of one bug and go on to learn about another.

GNU Compiler Collection (GCC)

GCC is an open source compiler system used to compile C/C++ programs: https://gcc.gnu.org/.

Preparation

You should do this before the lab.

  1. Install GDB:

    1. Ubuntu: sudo apt-get install gdb
    2. OSX: brew install gdb
    3. Windows: this takes multiple steps

      • Download Cygwin.

        Cygwin

      • Select development packages (Devel) during installation.

        Devel

      • Start Cygwin terminal from the start menu.

        Terminal

  2. Starting GDB:

    1. Type gdb in the terminal (Cygwin terminal for Windows user).

      GDB

    help command lists the help and quit command exits GDB.

Alternatively, you may also use online GDB for GDB part (with the exception on some questions, which we will highlight). Another alternative is to use the provided ReplIt on ...

Procedure

  1. Download the file lab1.c from the CS2100 website "Labs page".

  2. Compile lab1.c with gcc using the following command:

    gcc -g -o lab1 lab1.c

  3. What is the purpose of the flags "g" and "o" in GCC?

    • "g" flag: __________________________________________________

    • "o" flag: __________________________________________________

  4. Execute the program you just compiled using the command: ./lab1 (for windows type: lab1). What is the error encountered (if any!)?

    • Answer: __________________________________________________
  5. Start the GDB debugger by using the command: gdb lab1.

  6. To run the program in GDB, you can use the command run. This will run the whole program without any pause. Type run to execute the program.

  7. To get into the debug mode use the start command.

    Info
    • You can use the list command to view the source code at any point.
    • You can also use layout src and layout asm commands to view source code and assembly code in a split screen.
  8. A breakpoint is a command to put an intentional pause in the program execution to inspect the variable values and resources in the program. You can set multiple breakpoints in a program. In GDB you can put a breakpoint at any line number using the command:

    > break lineNumber

    or

    > b lineNumber

    Example: This will put a breakpoint on line 6 > break 6.

    Now if you run the program, it will pause at line 6. You can continue execution (till end or the next breakpoint) using the continue command.

    Which line(s) will you set the breakpoint(s) in?

    • Answer: __________________________________________________
  9. A step command is used to carry out step-by-step execution of the program. You can step through the program using the following command:

    > step

    This will execute only the next line of code.

    or

    > step numberOfLines

    Example: > step 3 will execute next three lines of code.

    Info
    • You can "switch on" display of the associated assembly code related to the instruction being executed using the command:

    set disassemble-next-line on

  10. At every step (or breakpoint) you can view a variable value using print command:

    > print a

    You can view all local variable values using the command:

    > info locals

    What are the values of variable c and d at the start of line 8 (i.e., before executing line 8)?

    • c = __________________________________________________

    • d = __________________________________________________

  11. You can view the register values at any step or breakpoint using this command:

    > info registers

  12. You can stop the debugging by using the stop command. To quit GDB, use the quit command.

  13. Debug and modify lab1.c to carry out four arithmetic operations (+, -, /, *) and print the days of the week. The output of the program should look as follows:

    Expected Output
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    Arithmetic operations:
    a+b = 110
    a-b = 90
    b/a = 0
    a*b = 1000
    
    Days of the week:
    Day[0] = Monday
    Day[1] = Tuesday
    Day[2] = Wednesday
    Day[3] = Thursday
    Day[4] = Friday
    Day[5] = Saturday
    Day[6] = Sunday
    

    The code lab1.c is also shown on the next page for your reference. Show your labTA the output of your corrected program.

  14. Submit this report to your labTA at the end of the lab. You do not need to submit the corrected program. You are NOT to email the report to your labTA.

Marking Scheme

Component Marks
Report 6
Correct Output 4
Total 10

Program

Using the following ReplIt, you cannot click on run directly because the file name is not main.c but lab1.c.

Lab 1

lab1.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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;
  int i;
  char *day[7] = {
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
  };

  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);

  printf("Days of the week:\n");
  for (i=0; i<8; i++) {
    printf("Day[%d] = %s\n", i, day[i]);
  }

  return 0;
}