This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: [PATCH] specify arguments to debugee from commandline (second try)


Ok, here's the latest revision of the command-line args patch.

Syntax:
gdb [various options] --args <progname> <inferior arguments>

GDB will escape the following characters within an inferior argument:
^|&#<>\"'`$*?[](); \{}
                  ^----space

One thing I'm a little confused about - I thought that GNU getopt
(and getopt_long_only) moved arguments to the beginning of argv,
but that doesn't seem to be the case.
with an  argv that looks like this: {"gdb", "ls", "--args", "-l"}
I expect it to look like this (after getopt is called):
{"gdb", "--args", "ls", "-l"}
That doesn't happen, though.  Is it being forced into compatability
mode somehow?

Anyway, that's why it's 'gdb <opts> --args <progname> ...' instead
of 'gdb <opts> <progname> --args ...'

This isn't a proper patch - there's indentation issues with some of
the old argument parsing code.  I'm leaving the indentation alone
right now so I don't obscure the actual changed code (I had to put an
'if' around all the old program-name/corearg code).

---- Begin patch ----

*** gdb/main.c	Thu Jun  7 00:56:39 2001
--- cmdline_arg_gdb/main.c	Thu Jun  7 00:58:56 2001
*************** captured_main (void *data)
*** 168,173 ****
--- 168,177 ----
  
    long time_at_startup = get_run_time ();
  
+   /* Arguments for the inferior program from the commandline. */
+   char *cmdline_inf_args = 0;
+   int cmdline_inf_args_flag = 0;
+ 
    START_PROGRESS (argv[0], 0);
  
  #ifdef MPW
*************** captured_main (void *data)
*** 281,286 ****
--- 285,291 ----
        {"windows", no_argument, &use_windows, 1},
        {"statistics", no_argument, 0, 13},
        {"write", no_argument, &write_files, 1},
+       {"args", no_argument, 0, 14},
  /* Allow machine descriptions to add more options... */
  #ifdef ADDITIONAL_OPTIONS
        ADDITIONAL_OPTIONS
*************** captured_main (void *data)
*** 297,302 ****
--- 302,314 ----
  	if (c == EOF)
  	  break;
  
+ 	/* We stop argument processing instantly if we get a -args option. */
+ 	if (c == 14)
+ 	{
+ 	  cmdline_inf_args_flag = 1;
+ 	  break;
+ 	}
+ 
  	/* Long option that takes an argument.  */
  	if (c == 0 && long_options[option_index].flag == 0)
  	  c = long_options[option_index].val;
*************** extern int gdbtk_test (char *);
*** 463,470 ****
        use_windows = 0;
  #endif
  
-     /* OK, that's all the options.  The other arguments are filenames.  */
      count = 0;
      for (; optind < argc; optind++)
        switch (++count)
  	{
--- 475,522 ----
        use_windows = 0;
  #endif
  
      count = 0;
+     /* If we have commandline arguments, then we expect the next argument
+        to be the program name, and the following arguments to be arguments
+        to the inferior process. */
+     if (cmdline_inf_args_flag)
+       {
+ 	int arg_len = 1; /* EOS */
+ 	int i;
+ 	char *cmdline_end; /* points to end of cmdline_inf_args. */
+ 
+ 	/* Run through all the arguments to determine what size buffer
+ 	   we need. */
+ 	for (i=optind+1; i<argc; i++)
+ 	  {
+ 	    arg_len += shell_escape_strlen (argv[i]);
+ 	    arg_len += 1; /* We'll put a space after the argument. */
+ 	  }
+ 	cmdline_inf_args = xmalloc (arg_len);
+ 	cmdline_inf_args[0] = 0; /* Starts out null. */
+ 	for (; optind<argc; optind++)
+ 	  {
+ 	    switch (++count)
+ 	      {
+ 	      case 1:
+ 		symarg = argv[optind];
+ 		execarg = argv[optind];
+ 		break;
+ 	      case 2:
+ 		/* Escape first string into cmdline_inf_args */
+ 		shell_escape_string (cmdline_inf_args, argv[optind]);
+ 		break;
+ 	      default:
+ 		strcat (cmdline_inf_args, " ");
+ 		cmdline_end = cmdline_inf_args + strlen (cmdline_inf_args);
+ 		shell_escape_string (cmdline_end, argv[optind]);
+ 		break;
+ 	      }
+ 	  }
+       }
+     else
+       {
+     /* OK, that's all the options.  The other arguments are filenames.  */
      for (; optind < argc; optind++)
        switch (++count)
  	{
*************** extern int gdbtk_test (char *);
*** 485,490 ****
--- 537,543 ----
      if (batch)
        quiet = 1;
    }
+   }
  
  #if defined(TUI)
    /* Should this be moved to tui-top.c:_initialize_tui()? */
*************** extern int gdbtk_test (char *);
*** 669,674 ****
--- 722,733 ----
  
    /* Read in the old history after all the command files have been read. */
    init_history ();
+ 
+   if (cmdline_inf_args_flag)
+     {
+       /* Set the inferior arguments now, so we can override the command file. */
+       set_inferior_args (cmdline_inf_args);
+     }
  
    if (batch)
      {
*** gdb/utils.c	Thu Jun  7 00:56:40 2001
--- cmdline_arg_gdb/utils.c	Thu Jun  7 00:01:05 2001
*************** address_to_host_pointer (CORE_ADDR addr)
*** 3045,3047 ****
--- 3045,3080 ----
    ADDRESS_TO_POINTER (builtin_type_ptr, &ptr, addr);
    return ptr;
  }
+ 
+ static char *shell_escape_chars = "^|&#<>\"'`$*?[](); \\{}";
+ int shell_escape_strlen (char *string)
+ {
+   int len;
+   
+   len = 0;
+   while( *string )
+     {
+       len++; /* Count each non-zero character once. */
+       if (strchr (shell_escape_chars, *string))
+ 	len++; /* If it needs to be escaped, count the '\' as well. */
+ 	  string++;
+     }
+ 
+   return len;
+ }
+ 
+ void shell_escape_string (char *dest, char *source)
+ {
+   while( *source )
+     {
+       /* If the next char. needs to be escaped... */
+       if( strchr (shell_escape_chars, *source) )
+ 	{
+ 	  *dest = '\\';  /* ...escape the next char. */
+ 	  dest++;
+ 	}
+       *dest = *source;
+       dest++;
+       source++;
+     }
+ }
*** gdb/defs.h	Thu Jun  7 00:52:40 2001
--- cmdline_arg_gdb/defs.h	Thu Jun  7 01:01:35 2001
*************** extern void init_page_info (void);
*** 519,524 ****
--- 519,526 ----
  extern CORE_ADDR host_pointer_to_address (void *ptr);
  extern void *address_to_host_pointer (CORE_ADDR addr);
  
+ extern int shell_escape_strlen (char *string);
+ extern void shell_escape_string (char *dest, char *source);
  /* From demangle.c */
  
  extern void set_demangling_style (char *);

------ End patch -------

-- 
By and large, the only skill the alchemists of Ankh-Morpork had discovered
so far was the ability to turn gold into less gold.
        -- (Terry Pratchett, Moving Pictures)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]