keiths / rpms / gdb

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