Blame SOURCES/gdb-6.6-buildid-locate-core-as-arg.patch

01917d
http://sourceware.org/ml/gdb-patches/2010-01/msg00558.html
01917d
Subject: Re: [patch] print a more useful error message for "gdb core"
01917d
01917d
[ Fixed up since the mail.  ]
01917d
01917d
On Thu, 21 Jan 2010 18:17:15 +0100, Doug Evans wrote:
01917d
> Not an exhaustive list, but if we go down the path of converting "gdb
01917d
> corefile" to "gdb -c corefile", then we also need to think about "file
01917d
> corefile" being converted to "core corefile" [or "target core
01917d
> corefile", "core" is apparently deprecated in favor of "target core"]
01917d
> and "target exec corefile" -> "target core corefile".  Presumably
01917d
> "file corefile" (and "target exec corefile") would discard the
01917d
> currently selected executable.  But maybe not.  Will that be confusing
01917d
> for users?  I don't know.
01917d
01917d
While thinking about it overriding some GDB _commands_ was not my intention.
01917d
01917d
There is a general assumption if I have a shell COMMAND and some FILE I can do
01917d
$ COMMAND FILE
01917d
and COMMAND will appropriately load the FILE.
01917d
01917d
FSF GDB currently needs to specify also the executable file for core files
01917d
which already inhibits this intuitive expectation.  OTOH with the build-id
01917d
locating patch which could allow such intuitive start  notneeding the
01917d
executable file.  Still it currently did not work due to the required "-c":
01917d
$ COMMAND -c COREFILE
01917d
01917d
Entering "file", "core-file" or "attach" commands is already explicit enough
01917d
so that it IMO should do what the command name says without any
01917d
autodetections.  The second command line argument
01917d
(captured_main->pid_or_core_arg) is also autodetected (for PID or CORE) but
01917d
neither "attach" accepts a core file nor "core-file" accepts a PID.
01917d
01917d
01917d
The patch makes sense only with the build-id patchset so this is not submit
01917d
for FSF GDB inclusion yet.  I am fine with your patch (+/- Hui Zhu's pending
01917d
bfd_check_format_matches) as the patch below is its natural extension.
01917d
01917d
01917d
Sorry for the delay,
01917d
Jan
01917d
01917d
01917d
2010-01-25  Jan Kratochvil  <jan.kratochvil@redhat.com>
01917d
01917d
	* exceptions.h (enum errors <IS_CORE_ERROR>): New.
01917d
	* exec.c: Include exceptions.h.
01917d
	(exec_file_attach <bfd_core>): Call throw_error (IS_CORE_ERROR, ...).
01917d
	* main.c (exec_or_core_file_attach): New.
01917d
	(captured_main <optind < argc>): Set also corearg.
01917d
	(captured_main <strcmp (execarg, symarg) == 0>): New variable func.
01917d
	Call exec_or_core_file_attach if COREARG matches EXECARG.  Call
01917d
	symbol_file_add_main only if CORE_BFD remained NULL.
01917d
01917d
Http://sourceware.org/ml/gdb-patches/2010-01/msg00517.html
01917d
2010-01-20  Doug Evans  <dje@google.com>
01917d
01917d
	* exec.c (exec_file_attach): Print a more useful error message if the
01917d
	user did "gdb core".
01917d
01917d
Index: gdb-7.5.91.20130323/gdb/exceptions.h
01917d
===================================================================
01917d
--- gdb-7.5.91.20130323.orig/gdb/exceptions.h	2013-03-22 21:41:45.000000000 +0100
01917d
+++ gdb-7.5.91.20130323/gdb/exceptions.h	2013-03-23 19:49:05.738459185 +0100
01917d
@@ -90,6 +90,9 @@ enum errors {
01917d
      aborted as the inferior state is no longer valid.  */
01917d
   TARGET_CLOSE_ERROR,
01917d
 
01917d
+  /* Attempt to load a core file as executable.  */
01917d
+  IS_CORE_ERROR,
01917d
+
01917d
   /* Add more errors here.  */
01917d
   NR_ERRORS
01917d
 };
01917d
Index: gdb-7.5.91.20130323/gdb/exec.c
01917d
===================================================================
01917d
--- gdb-7.5.91.20130323.orig/gdb/exec.c	2013-01-31 19:37:37.000000000 +0100
01917d
+++ gdb-7.5.91.20130323/gdb/exec.c	2013-03-23 19:48:53.284575912 +0100
01917d
@@ -34,6 +34,7 @@
01917d
 #include "gdbthread.h"
01917d
 #include "progspace.h"
01917d
 #include "gdb_bfd.h"
01917d
+#include "exceptions.h"
01917d
 
01917d
 #include <fcntl.h>
01917d
 #include "readline/readline.h"
01917d
@@ -240,12 +241,27 @@ exec_file_attach (char *filename, int fr
01917d
 
01917d
       if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
01917d
 	{
01917d
+	  int is_core;
01917d
+
01917d
+	  /* If the user accidentally did "gdb core", print a useful
01917d
+	     error message.  Check it only after bfd_object has been checked as
01917d
+	     a valid executable may get recognized for example also as
01917d
+	     "trad-core".  */
01917d
+	  is_core = bfd_check_format (exec_bfd, bfd_core);
01917d
+
01917d
 	  /* Make sure to close exec_bfd, or else "run" might try to use
01917d
 	     it.  */
01917d
 	  exec_close ();
01917d
-	  error (_("\"%s\": not in executable format: %s"),
01917d
-		 scratch_pathname,
01917d
-		 gdb_bfd_errmsg (bfd_get_error (), matching));
01917d
+
01917d
+	  if (is_core != 0)
01917d
+	    throw_error (IS_CORE_ERROR,
01917d
+		   _("\"%s\" is a core file.\n"
01917d
+		     "Please specify an executable to debug."),
01917d
+		   scratch_pathname);
01917d
+	  else
01917d
+	    error (_("\"%s\": not in executable format: %s"),
01917d
+		   scratch_pathname,
01917d
+		   gdb_bfd_errmsg (bfd_get_error (), matching));
01917d
 	}
01917d
 
01917d
       /* FIXME - This should only be run for RS6000, but the ifdef is a poor
01917d
Index: gdb-7.5.91.20130323/gdb/main.c
01917d
===================================================================
01917d
--- gdb-7.5.91.20130323.orig/gdb/main.c	2013-03-23 19:48:18.000000000 +0100
01917d
+++ gdb-7.5.91.20130323/gdb/main.c	2013-03-23 19:48:53.285575901 +0100
01917d
@@ -296,6 +296,36 @@ typedef struct cmdarg {
01917d
 /* Define type VEC (cmdarg_s).  */
01917d
 DEF_VEC_O (cmdarg_s);
01917d
 
01917d
+/* Call exec_file_attach.  If it detected FILENAME is a core file call
01917d
+   core_file_command.  Print the original exec_file_attach error only if
01917d
+   core_file_command failed to find a matching executable.  */
01917d
+
01917d
+static void
01917d
+exec_or_core_file_attach (char *filename, int from_tty)
01917d
+{
01917d
+  volatile struct gdb_exception e;
01917d
+
01917d
+  gdb_assert (exec_bfd == NULL);
01917d
+
01917d
+  TRY_CATCH (e, RETURN_MASK_ALL)
01917d
+    {
01917d
+      exec_file_attach (filename, from_tty);
01917d
+    }
01917d
+  if (e.reason < 0)
01917d
+    {
01917d
+      if (e.error == IS_CORE_ERROR)
01917d
+	{
01917d
+	  core_file_command (filename, from_tty);
01917d
+
01917d
+	  /* Iff the core file found its executable suppress the error message
01917d
+	     from exec_file_attach.  */
01917d
+	  if (exec_bfd != NULL)
01917d
+	    return;
01917d
+	}
01917d
+      throw_exception (e);
01917d
+    }
01917d
+}
01917d
+
01917d
 static int
01917d
 captured_main (void *data)
01917d
 {
01917d
@@ -796,6 +826,8 @@ captured_main (void *data)
01917d
 	{
01917d
 	  symarg = argv[optind];
01917d
 	  execarg = argv[optind];
01917d
+	  if (optind + 1 == argc && corearg == NULL)
01917d
+	    corearg = argv[optind];
01917d
 	  optind++;
01917d
 	}
01917d
 
01917d
@@ -951,11 +983,25 @@ captured_main (void *data)
01917d
       && symarg != NULL
01917d
       && strcmp (execarg, symarg) == 0)
01917d
     {
01917d
+      catch_command_errors_ftype *func;
01917d
+
01917d
+      /* Call exec_or_core_file_attach only if the file was specified as
01917d
+	 a command line argument (and not an a command line option).  */
01917d
+      if (corearg != NULL && strcmp (corearg, execarg) == 0)
01917d
+	{
01917d
+	  func = exec_or_core_file_attach;
01917d
+	  corearg = NULL;
01917d
+	}
01917d
+      else
01917d
+	func = exec_file_attach;
01917d
+
01917d
       /* The exec file and the symbol-file are the same.  If we can't
01917d
          open it, better only print one error message.
01917d
-         catch_command_errors returns non-zero on success!  */
01917d
-      if (catch_command_errors (exec_file_attach, execarg,
01917d
-				!batch_flag, RETURN_MASK_ALL))
01917d
+         catch_command_errors returns non-zero on success!
01917d
+	 Do not load EXECARG as a symbol file if it has been already processed
01917d
+	 as a core file.  */
01917d
+      if (catch_command_errors (func, execarg, !batch_flag, RETURN_MASK_ALL)
01917d
+	  && core_bfd == NULL)
01917d
 	catch_command_errors (symbol_file_add_main, symarg,
01917d
 			      !batch_flag, RETURN_MASK_ALL);
01917d
     }