Blob Blame History Raw
diff -Naurp insight-7.8.50.20140825.orig/gdb/exceptions.h insight-7.8.50.20140825.new/gdb/exceptions.h
--- insight-7.8.50.20140825.orig/gdb/exceptions.h	2014-07-25 15:10:37.000000000 +0200
+++ insight-7.8.50.20140825.new/gdb/exceptions.h	2014-08-25 19:11:50.701751187 +0200
@@ -100,6 +100,9 @@ enum errors {
   /* Requested feature, method, mechanism, etc. is not supported.  */
   NOT_SUPPORTED_ERROR,
 
+  /* Attempt to load a core file as executable.  */
+  IS_CORE_ERROR,
+
   /* Add more errors here.  */
   NR_ERRORS
 };
diff -Naurp insight-7.8.50.20140825.orig/gdb/exec.c insight-7.8.50.20140825.new/gdb/exec.c
--- insight-7.8.50.20140825.orig/gdb/exec.c	2014-08-25 14:46:22.000000000 +0200
+++ insight-7.8.50.20140825.new/gdb/exec.c	2014-08-25 19:11:50.703751204 +0200
@@ -35,6 +35,7 @@
 #include "progspace.h"
 #include "gdb_bfd.h"
 #include "gcore.h"
+#include "exceptions.h"
 
 #include <fcntl.h>
 #include "readline/readline.h"
@@ -222,12 +223,27 @@ exec_file_attach (const char *filename,
 
       if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
 	{
+	  int is_core;
+
+	  /* If the user accidentally did "gdb core", print a useful
+	     error message.  Check it only after bfd_object has been checked as
+	     a valid executable may get recognized for example also as
+	     "trad-core".  */
+	  is_core = bfd_check_format (exec_bfd, bfd_core);
+
 	  /* Make sure to close exec_bfd, or else "run" might try to use
 	     it.  */
 	  exec_close ();
-	  error (_("\"%s\": not in executable format: %s"),
-		 scratch_pathname,
-		 gdb_bfd_errmsg (bfd_get_error (), matching));
+
+	  if (is_core != 0)
+	    throw_error (IS_CORE_ERROR,
+		   _("\"%s\" is a core file.\n"
+		     "Please specify an executable to debug."),
+		   scratch_pathname);
+	  else
+	    error (_("\"%s\": not in executable format: %s"),
+		   scratch_pathname,
+		   gdb_bfd_errmsg (bfd_get_error (), matching));
 	}
 
       if (build_section_table (exec_bfd, &sections, &sections_end))
diff -Naurp insight-7.8.50.20140825.orig/gdb/main.c insight-7.8.50.20140825.new/gdb/main.c
--- insight-7.8.50.20140825.orig/gdb/main.c	2014-08-25 19:10:44.933194536 +0200
+++ insight-7.8.50.20140825.new/gdb/main.c	2014-08-25 19:37:06.153577650 +0200
@@ -419,6 +419,36 @@ typedef struct cmdarg {
 /* Define type VEC (cmdarg_s).  */
 DEF_VEC_O (cmdarg_s);
 
+/* Call exec_file_attach.  If it detected FILENAME is a core file call
+   core_file_command.  Print the original exec_file_attach error only if
+   core_file_command failed to find a matching executable.  */
+
+static void
+exec_or_core_file_attach (char *filename, int from_tty)
+{
+  volatile struct gdb_exception e;
+
+  gdb_assert (exec_bfd == NULL);
+
+  TRY_CATCH (e, RETURN_MASK_ALL)
+    {
+      exec_file_attach (filename, from_tty);
+    }
+  if (e.reason < 0)
+    {
+      if (e.error == IS_CORE_ERROR)
+	{
+	  core_file_command (filename, from_tty);
+
+	  /* Iff the core file found its executable suppress the error message
+	     from exec_file_attach.  */
+	  if (exec_bfd != NULL)
+	    return;
+	}
+      throw_exception (e);
+    }
+}
+
 static int
 captured_main (void *data)
 {
@@ -935,6 +965,8 @@ captured_main (void *data)
 	{
 	  symarg = argv[optind];
 	  execarg = argv[optind];
+	  if (optind + 1 == argc && corearg == NULL)
+	    corearg = argv[optind];
 	  optind++;
 	}
 
@@ -1098,11 +1130,26 @@ captured_main (void *data)
       && symarg != NULL
       && strcmp (execarg, symarg) == 0)
     {
+      int r;
+
+      /* Call exec_or_core_file_attach only if the file was specified as
+	 a command line argument (and not an a command line option).  */
+      if (corearg != NULL && strcmp (corearg, execarg) == 0)
+	{
+	  r = catch_command_errors (exec_or_core_file_attach, execarg,
+				    !batch_flag, RETURN_MASK_ALL);
+	  corearg = NULL;
+	}
+      else
+	  r = catch_command_errors_const (exec_file_attach, execarg,
+				    !batch_flag, RETURN_MASK_ALL);
+
       /* The exec file and the symbol-file are the same.  If we can't
          open it, better only print one error message.
-         catch_command_errors returns non-zero on success!  */
-      if (catch_command_errors_const (exec_file_attach, execarg,
-				      !batch_flag, RETURN_MASK_ALL))
+         catch_command_errors returns non-zero on success!
+	 Do not load EXECARG as a symbol file if it has been already processed
+	 as a core file.  */
+      if (r && core_bfd == NULL)
 	catch_command_errors_const (symbol_file_add_main, symarg,
 				    !batch_flag, RETURN_MASK_ALL);
     }