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

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