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

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