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

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