This is the mail archive of the gdb-patches@sourceware.org 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]
Other format: [Raw text]

--pid and --core


Hi all,

I was looking through --pid and attach mingw PRs in the GNATS,
and noticed an annoying issue.

If one specifies an explicit PID to attach to, failing to
attach, gdb will try to open a core file of the same name.

The attach fail -> open core is there to support
invoking gdb as:

gdb program pid

or:

gdb program core

Since, if the user specifies gdb program 3333, gdb can't
know if 3333 is a pid or a core file named "3333".

So for, so good. Now,

If the user specifies,

gdb program --pid=3333,

gdb has no business trying to look for a core file named 3333.

The patch avoids these confusing messages [1] when the user specifies
an invalid or non-existing pid:

Before:

    >./gdb --pid=3333
    Attaching to process 3333
    ptrace: No such process.
    /home/pedro/gdb/build/3333: No such file or directory.  <==== [1]
    (gdb)

After:

    >./gdb --pid=3333
    Attaching to process 3333
    ptrace: No such process.
    (gdb)

The patch also catches invalid simultaneous --pid,--core uses,

And these weirdnesses:

Before:

  >gdb/gdb gdb/gdb --pid=3333 4444 5555
  Excess command line arguments ignored. (5555)
  Attaching to program: /home/pedro/gdb/build/gdb/gdb, process 4444
  ptrace: No such process.
  /home/pedro/gdb/build/4444: No such file or directory.

After:

  >gdb/gdb gdb/gdb --pid=3333 4444 5555
  Excess command line arguments ignored. (4444 ...)
  Attaching to program: /home/pedro/gdb/build/gdb/gdb, process 3333
  ptrace: No such process.

Tested on i686-pc-linux-gnu, no regressions.

OK ?


2007-12-28  Pedro Alves  <pedro_alves@portugalmail.pt>

	* main.c (captured_main): Error out if --core and --pid options
	were issued simultaneously.  If an explicit pid option was passed,
	don't fallback to core file.  Detect extra arguments better in the
	presence of explicit pid or core arguments.

---
 gdb/main.c |   76 ++++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 28 deletions(-)

Index: src/gdb/main.c
===================================================================
--- src.orig/gdb/main.c	2008-01-02 17:58:39.000000000 +0000
+++ src/gdb/main.c	2008-01-03 16:43:03.000000000 +0000
@@ -127,7 +127,9 @@ captured_main (void *data)
   /* Pointers to various arguments from command line.  */
   char *symarg = NULL;
   char *execarg = NULL;
+  char *pidarg = NULL;
   char *corearg = NULL;
+  char *pid_or_core_arg = NULL;
   char *cdarg = NULL;
   char *ttyarg = NULL;
 
@@ -435,8 +437,7 @@ captured_main (void *data)
 	    corearg = optarg;
 	    break;
 	  case 'p':
-	    /* "corearg" is shared by "--core" and "--pid" */
-	    corearg = optarg;
+	    pidarg = optarg;
 	    break;
 	  case 'x':
 	    cmdarg[ncmd].type = CMDARG_FILE;
@@ -575,23 +576,32 @@ extern int gdbtk_test (char *);
 	/* OK, that's all the options.  The other arguments are filenames.  */
 	count = 0;
 	for (; optind < argc; optind++)
-	  switch (++count)
-	    {
-	    case 1:
-	      symarg = argv[optind];
-	      execarg = argv[optind];
-	      break;
-	    case 2:
-	      /* The documentation says this can be a "ProcID" as well. 
-	         We will try it as both a corefile and a pid.  */
-	      corearg = argv[optind];
-	      break;
-	    case 3:
-	      fprintf_unfiltered (gdb_stderr,
-				  _("Excess command line arguments ignored. (%s%s)\n"),
-				  argv[optind], (optind == argc - 1) ? "" : " ...");
-	      break;
-	    }
+	  {
+	    ++count;
+	    if (count == 1)
+	      {
+		symarg = argv[optind];
+		execarg = argv[optind];
+	      }
+	    else if (count > 2
+		     /* If we have a --pid or a --core argument,
+			this argument don't make sense.  */
+		     || pidarg != NULL
+		     || corearg != NULL)
+	      {
+		fprintf_unfiltered (gdb_stderr, _("\
+Excess command line arguments ignored. (%s%s)\n"),
+				    argv[optind],
+				    (optind == argc - 1) ? "" : " ...");
+		optind = argc;
+		break;
+	      }
+	    else
+	      /* The documentation says this can be a "ProcID" as
+		 well.  We will try it later as both a corefile and a
+		 pid.  */
+	      pid_or_core_arg = argv[optind];
+	  }
       }
     if (batch)
       quiet = 1;
@@ -733,21 +743,31 @@ extern int gdbtk_test (char *);
 	catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL);
     }
 
+  if (corearg && pidarg)
+    error (_("\
+Can't attach to process and specify a core file at the same time."));
+
   if (corearg != NULL)
-    {
-      /* corearg may be either a corefile or a pid.
-	 If its first character is a digit, try attach first
-	 and then corefile.  Otherwise try corefile first. */
+    catch_command_errors (core_file_command, corearg,
+			  !batch, RETURN_MASK_ALL);
+  else if (pidarg != NULL)
+    catch_command_errors (attach_command, pidarg,
+			  !batch, RETURN_MASK_ALL);
+  else if (pid_or_core_arg)
+    {
+      /* The user specified 'gdb program pid' or gdb program core'.
+	 If pid_or_core_arg's first character is a digit, try attach
+	 first and then corefile.  Otherwise try just corefile.  */
 
-      if (isdigit (corearg[0]))
+      if (isdigit (pid_or_core_arg[0]))
 	{
-	  if (catch_command_errors (attach_command, corearg, 
+	  if (catch_command_errors (attach_command, pid_or_core_arg,
 				    !batch, RETURN_MASK_ALL) == 0)
-	    catch_command_errors (core_file_command, corearg, 
+	    catch_command_errors (core_file_command, pid_or_core_arg,
 				  !batch, RETURN_MASK_ALL);
 	}
-      else /* Can't be a pid, better be a corefile. */
-	catch_command_errors (core_file_command, corearg, 
+      else /* Can't be a pid, better be a corefile.  */
+	catch_command_errors (core_file_command, pid_or_core_arg,
 			      !batch, RETURN_MASK_ALL);
     }
 


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