CST8204: Lab 3 -- gdb: Linux Debug Tool

You've been attending Design Review meeting, Code Walk-Throughs, and regularly reviewing your PDL and the design for your part of the project at your software developer employer. Your team leader suggests that you learn the use of gdb. She explains that gdb is the GNU Debugger (where GNU stands for GNU's Not Unix. Yes, really.). The gdb program is used for source-level debugging by controlling program execution, monitoring memory locations, and tracing source code statements. You start gdb by entering this at the command line prompt:

gdb your-executable-program-name

The executable file used with the debugger is created by specifying the -g compiler option on the compiler command line and linking the resulting object files to produce an executable file. This file contains debugging information, including the names of all source files that the compiler translated to create the executable. These source files can be viewed from the debugger:

gcc -ansi -pedantic -g -o myProgram myProgram.c

Note that you are omitting -Wall -Wextra -O2, since the process of optimizing makes use of a debug program quite awkward and because the increased warning levels will detect the bug. But you want to learn how gdb, remember?

Once you start gdb, you can use the built-in help for assistance. Very briefly, though, you will establish break-points (help break) at key spots in your program and use list to see the source code. You can then run the program with its arguments, and when it stops at a break-point, print or printf variables by name. You can step into a function, execute the next statement without tracing functions, or continue to the next break-point. And lots more.

Copy this test program from ~allisor/Student/misc/debug.c and debug it. Before the end of your lab, explain to the instructor where and what the problems are (if any), and suggest one or more ways to correct it. You will of course note that it compiles cleanly, but fails to execute.

#include <stdio.h>
#include <stdlib.h>

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

   /* program needs only 1 numeric argument */
   if (argc != 2)
      printf("Test program needs one number\n");
   else
   {
      /* convert ascii to integer (string to long) */
      number = strtol(argv[argc - 1], &ptr, 0);
      /* print argument value if non-zero */
      if (ptr[0] != '\0' || number == 0)
         printf("Use a proper number!\n");
      else
         printf("Last arg value is %s\n", number);
   }
   return 0;
}

Your purpose in this exercise is to learn how to use gdb, not to change the purpose of this trivial code (which is simply to print the value of the number from the last argument).

Don't attempt to just find and fix it by compiling it properly, or by inspecting the code even if you can. If you think you see one or more bugs, force yourself to use gdb to find it (or them – I won't tell!). You can check later with the full gcc warning levels or try splint, a version of lint, if you wish.