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

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