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

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