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

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