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 32f92b2
Index: gdb-7.9.50.20150531/gdb/exec.c
dd46ae6
===================================================================
Jan Kratochvil 32f92b2
--- gdb-7.9.50.20150531.orig/gdb/exec.c	2015-05-31 03:48:29.000000000 +0200
Jan Kratochvil 32f92b2
+++ gdb-7.9.50.20150531/gdb/exec.c	2015-05-31 20:07:35.092878685 +0200
Jan Kratochvil eb6cb2d
@@ -35,6 +35,7 @@
b469073
 #include "progspace.h"
Jan Kratochvil 11eae30
 #include "gdb_bfd.h"
Jan Kratochvil eb6cb2d
 #include "gcore.h"
b469073
+#include "exceptions.h"
b469073
 
b469073
 #include <fcntl.h>
b469073
 #include "readline/readline.h"
Jan Kratochvil 32f92b2
@@ -298,12 +299,27 @@ exec_file_attach (const char *filename,
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))
Jan Kratochvil 32f92b2
Index: gdb-7.9.50.20150531/gdb/main.c
dd46ae6
===================================================================
Jan Kratochvil 32f92b2
--- gdb-7.9.50.20150531.orig/gdb/main.c	2015-05-31 20:07:34.183872824 +0200
Jan Kratochvil 32f92b2
+++ gdb-7.9.50.20150531/gdb/main.c	2015-05-31 20:10:05.095845935 +0200
Jan Kratochvil 32f92b2
@@ -435,6 +435,37 @@ typedef struct cmdarg {
Jan Kratochvil f8eee05
 /* Define type VEC (cmdarg_s).  */
Jan Kratochvil f8eee05
 DEF_VEC_O (cmdarg_s);
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
+
b469073
 static int
b469073
 captured_main (void *data)
b469073
 {
Jan Kratochvil 32f92b2
@@ -923,6 +954,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 32f92b2
@@ -1080,11 +1113,25 @@ captured_main (void *data)
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!  */
Jan Kratochvil 2f7f533
-      if (catch_command_errors_const (exec_file_attach, execarg,
Jan Kratochvil 32f92b2
-				      !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.  */
Jan Kratochvil 32f92b2
+      if (catch_command_errors_const (func, execarg, !batch_flag)
b469073
+	  && core_bfd == NULL)
Jan Kratochvil 2c55a54
 	catch_command_errors_const (symbol_file_add_main, symarg,
Jan Kratochvil 32f92b2
 				    !batch_flag);
b469073
     }
Jan Kratochvil 32f92b2
Index: gdb-7.9.50.20150531/gdb/common/common-exceptions.h
Jan Kratochvil 2f7f533
===================================================================
Jan Kratochvil 32f92b2
--- gdb-7.9.50.20150531.orig/gdb/common/common-exceptions.h	2015-05-31 03:48:29.000000000 +0200
Jan Kratochvil 32f92b2
+++ gdb-7.9.50.20150531/gdb/common/common-exceptions.h	2015-05-31 20:07:35.093878692 +0200
Jan Kratochvil 32f92b2
@@ -105,6 +105,9 @@ enum errors {
Jan Kratochvil 32f92b2
      "_ERROR" is appended to the name.  */
Jan Kratochvil 32f92b2
   MAX_COMPLETIONS_REACHED_ERROR,
Jan Kratochvil 2f7f533
 
Jan Kratochvil 2f7f533
+  /* Attempt to load a core file as executable.  */
Jan Kratochvil 2f7f533
+  IS_CORE_ERROR,
Jan Kratochvil 2f7f533
+
Jan Kratochvil 2f7f533
   /* Add more errors here.  */
Jan Kratochvil 2f7f533
   NR_ERRORS
Jan Kratochvil 2f7f533
 };