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

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