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

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