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

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