142Chapter5 • Working with GNU Debugger
gram. Let us look at the following program that prints two command line arguments. The
program prints a message if exactly two command line arguments are not specified. Note that
this may not be the best program for this purpose and is used only to demonstrate a debugging
process. The program name is
arg.c
and it is listed below.
#include
#include
main (int argc, char **argv)
{
if (argc != 3) {
printf("You have to use two command line arguments\n");
exit(-1);
}
printf("This program prints two command line arguments\n");
printf("The first argument is : %s\n", argv[1]);
printf("The second argument is : %s\n", argv[2]);
}
You have to create an executable file,
arg
, from
arg.c
source code file. If you run this
program in
gdb
using the
run
command, it complains about command line arguments and ter-
minates as expected. This is shown below.
[rr@conformix 5]$ gdb arg
GNU gdb 5.0rh-5 Red Hat Linux 7.1
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public
License, and you are
welcome to change it and/or distribute copies of it under
certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty"
for details.
This GDB was configured as "i386-redhat-linux"...
(gdb) run
Starting program: /home/rr/5/arg
You have to use two command line arguments
Program exited with code 0377.
(gdb)
Note that no
break
point is set as we are not interested in stepping through the program
line by line at this point.
The following lines of another
gdb
session output show that you can specify command
line arguments with the
run
command. Any arguments for the
run
command are considered as
program arguments. These arguments are “
test1
” and “
test2
” and are printed on the screen
when you run the program.
Next Page >>
<< Previous Page
Back to the Table of Contents