b469073
http://sourceware.org/ml/gdb-patches/2010-01/msg00558.html
b469073
Subject: Re: [patch] print a more useful error message for "gdb core"
b469073
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
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
b469073
Sorry for the delay,
b469073
Jan
b469073
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".
b469073
Jan Kratochvil 254f0e9
Index: gdb-7.4.50.20111218/gdb/exceptions.h
dd46ae6
===================================================================
Jan Kratochvil 254f0e9
--- gdb-7.4.50.20111218.orig/gdb/exceptions.h	2011-10-09 21:21:38.000000000 +0200
Jan Kratochvil 254f0e9
+++ gdb-7.4.50.20111218/gdb/exceptions.h	2011-12-19 01:41:20.900509347 +0100
Jan Kratochvil 254f0e9
@@ -88,6 +88,9 @@ enum errors {
Jan Kratochvil 254f0e9
   /* DW_OP_GNU_entry_value resolving failed.  */
Jan Kratochvil 254f0e9
   NO_ENTRY_VALUE_ERROR,
b469073
 
b469073
+  /* Attempt to load a core file as executable.  */
b469073
+  IS_CORE_ERROR,
b469073
+
b469073
   /* Add more errors here.  */
b469073
   NR_ERRORS
b469073
 };
Jan Kratochvil 254f0e9
Index: gdb-7.4.50.20111218/gdb/exec.c
dd46ae6
===================================================================
Jan Kratochvil 254f0e9
--- gdb-7.4.50.20111218.orig/gdb/exec.c	2011-03-23 19:23:54.000000000 +0100
Jan Kratochvil 254f0e9
+++ gdb-7.4.50.20111218/gdb/exec.c	2011-12-19 01:41:04.863568846 +0100
Jan Kratochvil 254f0e9
@@ -35,6 +35,7 @@
ee681d3
 #include "arch-utils.h"
b469073
 #include "gdbthread.h"
b469073
 #include "progspace.h"
b469073
+#include "exceptions.h"
b469073
 
b469073
 #include <fcntl.h>
b469073
 #include "readline/readline.h"
Jan Kratochvil 254f0e9
@@ -254,12 +255,27 @@ exec_file_attach (char *filename, int fr
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
 
ee681d3
       /* FIXME - This should only be run for RS6000, but the ifdef is a poor
Jan Kratochvil 254f0e9
Index: gdb-7.4.50.20111218/gdb/main.c
dd46ae6
===================================================================
Jan Kratochvil 254f0e9
--- gdb-7.4.50.20111218.orig/gdb/main.c	2011-12-19 00:28:01.000000000 +0100
Jan Kratochvil 254f0e9
+++ gdb-7.4.50.20111218/gdb/main.c	2011-12-19 01:41:04.863568846 +0100
Jan Kratochvil 254f0e9
@@ -248,6 +248,36 @@ captured_command_loop (void *data)
b469073
   return 1;
b469073
 }
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
b469073
+exec_or_core_file_attach (char *filename, int from_tty)
b469073
+{
b469073
+  volatile struct gdb_exception e;
b469073
+
b469073
+  gdb_assert (exec_bfd == NULL);
b469073
+
b469073
+  TRY_CATCH (e, RETURN_MASK_ALL)
b469073
+    {
b469073
+      exec_file_attach (filename, from_tty);
b469073
+    }
b469073
+  if (e.reason < 0)
b469073
+    {
b469073
+      if (e.error == IS_CORE_ERROR)
b469073
+	{
b469073
+	  core_file_command (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
+    }
b469073
+}
b469073
+
b469073
 static int
b469073
 captured_main (void *data)
b469073
 {
Jan Kratochvil 254f0e9
@@ -704,6 +734,8 @@ captured_main (void *data)
b469073
 	{
b469073
 	  symarg = argv[optind];
b469073
 	  execarg = argv[optind];
b469073
+	  if (optind + 1 == argc && corearg == NULL)
b469073
+	    corearg = argv[optind];
b469073
 	  optind++;
b469073
 	}
b469073
 
Jan Kratochvil 254f0e9
@@ -845,11 +877,25 @@ captured_main (void *data)
b469073
       && symarg != NULL
b469073
       && strcmp (execarg, symarg) == 0)
b469073
     {
b469073
+      catch_command_errors_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!  */
Jan Kratochvil 33ff709
-      if (catch_command_errors (exec_file_attach, execarg,
Jan Kratochvil 33ff709
-				!batch_flag, RETURN_MASK_ALL))
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.  */
dd46ae6
+      if (catch_command_errors (func, execarg, !batch_flag, RETURN_MASK_ALL)
b469073
+	  && core_bfd == NULL)
Jan Kratochvil 33ff709
 	catch_command_errors (symbol_file_add_main, symarg,
Jan Kratochvil 33ff709
 			      !batch_flag, RETURN_MASK_ALL);
b469073
     }