Sonoma State University
Department of Computer Science
CS-210: Introduction to Unix
Exercise 12: GNU Debugger

Objective

In this exercise you will run the GNU debugger (gdb) on buggy C programs to gain experience in diagnosing how and why compiled C programs fail.

For questions one through four, please download then untar/uncompress the following file in your home directory on Blue:

  • cd ~/cs210_exercises/cs210_exercise_12
  • wget www.robertjamesbruce.com/programming_exercises/cs210/spring_2026/cs210_exercise_12.tgz
  • tar xzf cs210_exercise_12.tgz

Question 1 (accessing unallocated memory):

  1. Enter the question 1 directory:
    cd question_1
  2. The program q1.c is comprised of the following:
    #include <stdio.h>
    #include <stdlib.h>

    int main (void)
    {
      char *buffer;

      buffer[0] = 'X';
      buffer[1] = '\0';
      printf ("%s\n", buffer);
      return (EXIT_FAILURE);
    }
  3. Build q1.c by typing the following command in your shell:
    make
  4. Run the compiled q1 program by typing the following command in your shell:
    ./q1
  5. You should see the following error message output to the screen in your shell:
    Program received signal SIGSEGV, Segmentation fault.
    Obviously there is something wrong with the program. The GNU debugger can help you diagnose what went wrong.
  6. Run the compiled q1 program under the GNU debugger (gdb):
    gdb ./q1
  7. Enter the following command in the gdb prompt:
    run
  8. Enter the following command in the gdb prompt:
    backtrace
  9. What is the the output from the GNU debugger when issuing the "backtrace" command? You can cut-and-paste the output into a text file.
  10. Enter the following command in the gdb prompt:
    x buffer
  11. What is the the output from the GNU debugger when issuing the "x buffer" command? You can cut-and-paste the output into a text file.
  12. Enter the following command in the gdb prompt:
    quit
    Note: This will exit from your current debugging session.

Question 2 (accessing unallocated memory):

  1. Enter the question 2 directory:
    cd question_2
  2. The program q2.c is comprised of the following:
    #include <stdio.h>
    #include <stdlib.h>

    int main (int argc, char *argv[])
    {
      int i;

      for (i = 1; i <= 10; i++)
      {
        printf ("%s\n", argv[i]);
      }
      return (EXIT_FAILURE);
    }
  3. Build q2.c by typing the following command in your shell:
    make
  4. Run the compiled q2 program by typing the following command in your shell:
    ./q2 bork1 bork2 bork3 bork4 bork5 bork6 bork7 bork8 bork9 bork10
    Note: So far everything looks fine. It looks like the q2 program displays the input strings from the command line (e.g. "bork1", "bork2", etc.)
  5. Without entering any command line input strings, run the q2 program again by typing the following command in your shell:
    ./q2
  6. You should see the following error message output to the screen in your shell:
    Segmentation fault (core dumped)
    Obviously there is something wrong with the program. The GNU debugger can help you diagnose what went wrong.
  7. Run the compiled q2 program under the GNU debugger (gdb):
    gdb ./q2
  8. Enter the following command in the gdb prompt:
    break 12
    Note: this sets a breakpoint at line 12 in the source code (i.e. the print statement). When the program runs it will stop before running the print statement.
  9. What is the the output from the GNU debugger when issuing the "break 12" command? You can cut-and-paste the output into a text file.
  10. Enter the following command in the gdb prompt:
    run foo bar
    Note: This will run q2 program with two input strings called "foo" and "bar"
  11. What is the the output from the GNU debugger when issuing the "run foo bar" command? You can cut-and-paste the output into a text file.
  12. Enter the following command in the gdb prompt:
    print argc
    Note: This will display the contents of the argc variable (i.e. the number of arguments passed into the command line).
  13. What is the the output from the GNU debugger when issuing the "print argc" command? You can cut-and-paste the output into a text file.
  14. Enter the following command in the gdb prompt:
    print i
    Note: This will display the contents of the integer variable i.
  15. What is the the output from the GNU debugger when issuing the "print i" command? You can cut-and-paste the output into a text file.
  16. Enter the following command in the gdb prompt:
    print argv[1]
    Note: This will display the contents of the string array argv, element 1.
  17. What is the the output from the GNU debugger when issuing the "print argv[1]" command? You can cut-and-paste the output into a text file.
  18. Enter the following command in the gdb prompt:
    print argv[2]
    Note: This will display the contents of the string array argv, element 2.
  19. What is the the output from the GNU debugger when issuing the "print argv[2]" command? You can cut-and-paste the output into a text file.
  20. Enter the following command in the gdb prompt:
    print argv[3]
    Note: This will display the contents of the string array argv, element 3.
  21. What is the the output from the GNU debugger when issuing the "print argv[3]" command? You can cut-and-paste the output into a text file.
  22. Enter the following command in the gdb prompt:
    print argv[0]
    Note: This will display the contents of the string array argv, element 0.
  23. What is the the output from the GNU debugger when issuing the "print argv[0]" command? You can cut-and-paste the output into a text file.
  24. Enter the following command in the gdb prompt:
    quit
    Note: This will exit from your current debugging session.

Question 3 (smashing the stack):

  1. Enter the question 3 directory:
    cd question_3
  2. The program q3.c is comprised of the following:
    #include <stdio.h>
    #include <stdlib.h>

    int main (void)
    {
      char buffer[5];
      buffer[0] = 'O';
      buffer[1] = 'o';
      buffer[2] = 'p';
      buffer[3] = 's';
      buffer[4] = '!';
      buffer[5] = '\n';
      buffer[6] = '\0';
      printf ("%s", buffer);
      return (EXIT_FAILURE);
    }
  3. Build q3.c by typing the following command in your shell:
    make
  4. Run the compiled q3 program by typing the following command in your shell:
    ./q3
  5. You should see the following error message output to the screen in your shell:
    *** stack smashing detected ***: terminated
    Aborted (core dumped)
    Obviously there is something wrong with the program. The GNU debugger can help you diagnose what went wrong.
  6. Run the compiled q3 program under the GNU debugger (gdb):
    gdb ./q3
  7. Enter the following command in the gdb prompt:
    break 15
  8. Enter the following command in the gdb prompt:
    run
  9. Enter the following command in the gdb prompt:
    ptype buffer
  10. What is the the output from the GNU debugger when issuing the "ptype buffer" command? You can cut-and-paste the output into a text file.
  11. Enter the following command in the gdb prompt:
    print buffer
  12. What is the the output from the GNU debugger when issuing the "print buffer" command? You can cut-and-paste the output into a text file.
  13. Enter the following command in the gdb prompt:
    continue
  14. What is the the output from the GNU debugger when issuing the "continue" command? You can cut-and-paste the output into a text file.
  15. Enter the following command in the gdb prompt:
    quit

Question 4 (buffer overflow):

  1. Enter the question 4 directory:
    cd question_4
  2. The program q4.c is comprised of the following:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main (int argc, char *argv[])
    {
      char *buffer;

      buffer = (char *) malloc(sizeof(char)*5);
      if (buffer != NULL)
      {
        strcpy (buffer, argv[1]);
        printf ("%s\n", buffer);
        free (buffer);
      }
      return (EXIT_FAILURE);
    }
  3. Build q4.c by typing the following command in your shell:
    make
  4. Run the compiled q4 program by typing the following command in your shell:
    ./q4 01234
    Notice this program just echos the string "01234".
  5. Run the compiled q4 program under the GNU debugger (gdb):
    gdb ./q4
  6. Enter the following command in the gdb prompt:
    run 012345678901234567890123456789
  7. What is the the output from the GNU debugger when issuing the "run 012345678901234567890123456789" command? You can cut-and-paste the output into a text file.
  8. Enter the following command in the gdb prompt:
    quit

Submission Instructions:

Please submit your answers to Canvas for the questions above.