GDB needs a --cmdline option

Mo DeJong
Sun May 7 16:01:00 GMT 2000

Hi all.

I have been running into a problem with gdb that it seems
would be nicely solved with a new command line option --cmdline.

What is the problem you ask? Well, I am using a makefile
that does checks for various things and passes a bunch
of files to an executable based on the results of
earlier parts of the makefile. The makefile uses shell
pattern expansion to gather the command line arguments.

(this is a typical cmd line that gets run)

gcj -C *.java gnu/gcj/convert/*.java gnu/gcj/*.java

Now if there is a problem with the gcj executable,
I would want to use gdb to debug it. The problem is
I want to debug it in the context of the Makefile run,
not just by itself on the command line.

So what is needed? I suggest that a --cmdline option
like the following would make gdb a lot easier to use
in this scenario. I would simply change the line in
the makefile that ran the tool to this:

gdb gcj --cmdline "-C *.java gnu/gcj/convert/*.java gnu/gcj/*.java"

So I went off and hacked something together to do
that. The patch for gdb from the sourceware CVS is
appended to this email.

So question 1 is, do other folks think this --cmdline
option is a good idea?

If you answered yes on question 1, then what could be
done to improve the patch so that it is up to
gdb standards? I heard from a couple of folks that
it might not be a good idea to clobber an existing
.gdbcmdline file (if it existed) and that it might
be better to remove the file after it was sourced.

Any comments?

Mo Dejong
Red Hat Inc.

Index: main.c
RCS file: /cvs/src/src/gdb/main.c,v
retrieving revision 1.2
diff -u -r1.2 main.c
--- main.c      2000/02/23 00:25:42     1.2
+++ main.c      2000/05/07 02:03:59
@@ -164,6 +164,10 @@
   register int i;
+  /* Used by the --cmdline option */
+  struct ui_file * cmdline_file;
+  char * cmdline_filename;
   long time_at_startup = get_run_time ();
   START_PROGRESS (argv[0], 0);
@@ -257,6 +261,7 @@
       {"e", required_argument, 0, 'e'},
       {"core", required_argument, 0, 'c'},
       {"c", required_argument, 0, 'c'},
+      {"cmdline", required_argument, 0, 14},
       {"command", required_argument, 0, 'x'},
       {"version", no_argument, &print_version, 1},
       {"x", required_argument, 0, 'x'},
@@ -337,6 +342,22 @@
          case 'c':
            corearg = optarg;
+         case 14:
+           /* Provide a handy shortcut for the --command-FILE option.
+              We simply write out a file that sets the args and calls run.
+              You can then invoke gdb --cmdline "*.txt" prog from the 
shell */
+           cmdline_filename = ".gdbcmdline";
+           cmdline_file = gdb_fopen(cmdline_filename, "w");
+           fputs_unfiltered("set args ", cmdline_file);
+           fputs_unfiltered(optarg, cmdline_file);
+           fputs_unfiltered("\nrun\n", cmdline_file);
+           gdb_flush (cmdline_file);
+           ui_file_delete (cmdline_file); /* deletes the object not the 
file */
+           /* Now we pretend to be like --command by passing the file name
+              for the tmp file we just created into the next switch 
block */
+           optarg = cmdline_filename;
          case 'x':
            cmdarg[ncmd++] = optarg;
            if (ncmd >= cmdsize)
@@ -782,6 +803,7 @@
   -b BAUDRATE        Set serial port baud rate used for remote debugging.\n\
   --batch            Exit after processing options.\n\
   --cd=DIR           Change current directory to DIR.\n\
+  --cmdline=args     Run executable-file with args.\n\
   --command=FILE     Execute GDB commands from FILE.\n\
   --core=COREFILE    Analyze the core dump COREFILE.\n\
 ", stream);

More information about the Gdb-patches mailing list