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

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