Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@codesourcery.com>
To: gdb-patches@sourceware.org
Subject: --pid and --core
Date: Thu, 03 Jan 2008 16:56:00 -0000	[thread overview]
Message-ID: <477D122A.4060405@codesourcery.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1572 bytes --]

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 ?



[-- Attachment #2: pid_core_split.diff --]
[-- Type: text/x-diff, Size: 4149 bytes --]

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);
     }
 


             reply	other threads:[~2008-01-03 16:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-03 16:56 Pedro Alves [this message]
2008-01-04  5:03 ` Joel Brobecker
2008-01-04 12:20   ` Pedro Alves
2008-01-04 15:41     ` Joel Brobecker
2008-01-05 12:47     ` Eli Zaretskii
2008-01-05 12:46 ` Eli Zaretskii
2008-01-05 13:09   ` Eli Zaretskii
2008-01-05 17:26     ` Pedro Alves
2008-01-05 17:36       ` Eli Zaretskii
2008-01-05 21:58         ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=477D122A.4060405@codesourcery.com \
    --to=pedro@codesourcery.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox