Fix normal_stop latent bug
[deliverable/binutils-gdb.git] / gdb / main.c
index 77d2bf7467cdd614968616b2feacf141c9cef000..97ffc3fba482abdf8acca6d336b8fbe15eb3cbc7 100644 (file)
@@ -1,6 +1,6 @@
 /* Top level stuff for GDB, the GNU debugger.
 
-   Copyright (C) 1986-2018 Free Software Foundation, Inc.
+   Copyright (C) 1986-2019 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "maint.h"
 
 #include "filenames.h"
-#include "filestuff.h"
+#include "common/filestuff.h"
 #include <signal.h>
 #include "event-top.h"
 #include "infrun.h"
-#include "signals-state-save-restore.h"
+#include "common/signals-state-save-restore.h"
 #include <vector>
+#include "common/pathstuff.h"
+#include "cli/cli-style.h"
 
 /* The selected interpreter.  This will be used as a set command
    variable, so it should always be malloc'ed - since
@@ -207,7 +209,7 @@ get_init_files (const char **system_gdbinit,
   if (!initialized)
     {
       struct stat homebuf, cwdbuf, s;
-      char *homedir;
+      const char *homedir;
 
       if (SYSTEM_GDBINIT[0])
        {
@@ -328,14 +330,6 @@ captured_command_loop ()
   /* Now it's time to start the event loop.  */
   start_event_loop ();
 
-  /* FIXME: cagney/1999-11-05: A correct command_loop() implementaton
-     would clean things up (restoring the cleanup chain) to the state
-     they were just prior to the call.  Technically, this means that
-     the do_cleanups() below is redundant.  Unfortunately, many FUNCs
-     are not that well behaved.  do_cleanups should either be replaced
-     with a do_cleanups call (to cover the problem) or an assertion
-     check to detect bad FUNCs code.  */
-  do_cleanups (all_cleanups ());
   /* If the command_loop returned, normally (rather than threw an
      error) we try to quit.  If the quit is aborted, our caller
      catches the signal and restarts the command loop.  */
@@ -484,9 +478,9 @@ captured_main_1 (struct captured_main_args *context)
 
   int i;
   int save_auto_load;
-  struct objfile *objfile;
+  int ret = 1;
 
-#ifdef HAVE_SBRK
+#ifdef HAVE_USEFUL_SBRK
   /* Set this before constructing scoped_command_stats.  */
   lim_at_start = (char *) sbrk (0);
 #endif
@@ -504,9 +498,7 @@ captured_main_1 (struct captured_main_args *context)
   textdomain (PACKAGE);
 #endif
 
-  bfd_init ();
   notice_open_fds ();
-  save_original_signals_state ();
 
   saved_command_line = (char *) xstrdup ("");
 
@@ -516,12 +508,17 @@ captured_main_1 (struct captured_main_args *context)
   setvbuf (stderr, NULL, _IONBF, BUFSIZ);
 #endif
 
+  /* Note: `error' cannot be called before this point, because the
+     caller will crash when trying to print the exception.  */
   main_ui = new ui (stdin, stdout, stderr);
   current_ui = main_ui;
 
   gdb_stdtargerr = gdb_stderr; /* for moment */
   gdb_stdtargin = gdb_stdin;   /* for moment */
 
+  if (bfd_init () != BFD_INIT_MAGIC)
+    error (_("fatal error: libbfd ABI mismatch"));
+
 #ifdef __MINGW32__
   /* On Windows, argv[0] is not necessarily set to absolute form when
      GDB is found along PATH, without which relocation doesn't work.  */
@@ -800,28 +797,28 @@ captured_main_1 (struct captured_main_args *context)
            break;
          case 'b':
            {
-             int i;
+             int rate;
              char *p;
 
-             i = strtol (optarg, &p, 0);
-             if (i == 0 && p == optarg)
+             rate = strtol (optarg, &p, 0);
+             if (rate == 0 && p == optarg)
                warning (_("could not set baud rate to `%s'."),
                         optarg);
              else
-               baud_rate = i;
+               baud_rate = rate;
            }
             break;
          case 'l':
            {
-             int i;
+             int timeout;
              char *p;
 
-             i = strtol (optarg, &p, 0);
-             if (i == 0 && p == optarg)
+             timeout = strtol (optarg, &p, 0);
+             if (timeout == 0 && p == optarg)
                warning (_("could not set timeout limit to `%s'."),
                         optarg);
              else
-               remote_timeout = i;
+               remote_timeout = timeout;
            }
            break;
 
@@ -846,9 +843,16 @@ captured_main_1 (struct captured_main_args *context)
       }
 
     if (batch_flag)
-      quiet = 1;
+      {
+       quiet = 1;
+
+       /* Disable all output styling when running in batch mode.  */
+       cli_styling = 0;
+      }
   }
 
+  save_original_signals_state (quiet);
+
   /* Try to set up an alternate signal stack for SIGSEGV handlers.  */
   setup_alternate_signal_stack ();
 
@@ -916,7 +920,7 @@ captured_main_1 (struct captured_main_args *context)
 
   if (print_version)
     {
-      print_gdb_version (gdb_stdout);
+      print_gdb_version (gdb_stdout, false);
       wrap_here ("");
       printf_filtered ("\n");
       exit (0);
@@ -945,7 +949,7 @@ captured_main_1 (struct captured_main_args *context)
     {
       /* Print all the junk at the top, with trailing "..." if we are
          about to read a symbol file (possibly slowly).  */
-      print_gdb_version (gdb_stdout);
+      print_gdb_version (gdb_stdout, true);
       if (symarg)
        printf_filtered ("..");
       wrap_here ("");
@@ -966,7 +970,7 @@ captured_main_1 (struct captured_main_args *context)
     {
       /* Print all the junk at the top, with trailing "..." if we are
          about to read a symbol file (possibly slowly).  */
-      print_gdb_version (gdb_stdout);
+      print_gdb_version (gdb_stdout, true);
       if (symarg)
        printf_filtered ("..");
       wrap_here ("");
@@ -984,7 +988,7 @@ captured_main_1 (struct captured_main_args *context)
      processed; it sets global parameters, which are independent of
      what file you are debugging or what directory you are in.  */
   if (system_gdbinit && !inhibit_gdbinit)
-    catch_command_errors (source_script, system_gdbinit, 0);
+    ret = catch_command_errors (source_script, system_gdbinit, 0);
 
   /* Read and execute $HOME/.gdbinit file, if it exists.  This is done
      *before* all the command line arguments are processed; it sets
@@ -992,7 +996,7 @@ captured_main_1 (struct captured_main_args *context)
      debugging or what directory you are in.  */
 
   if (home_gdbinit && !inhibit_gdbinit && !inhibit_home_gdbinit)
-    catch_command_errors (source_script, home_gdbinit, 0);
+    ret = catch_command_errors (source_script, home_gdbinit, 0);
 
   /* Process '-ix' and '-iex' options early.  */
   for (i = 0; i < cmdarg_vec.size (); i++)
@@ -1002,12 +1006,12 @@ captured_main_1 (struct captured_main_args *context)
       switch (cmdarg_p.type)
        {
        case CMDARG_INIT_FILE:
-         catch_command_errors (source_script, cmdarg_p.string,
-                               !batch_flag);
+         ret = catch_command_errors (source_script, cmdarg_p.string,
+                                     !batch_flag);
          break;
        case CMDARG_INIT_COMMAND:
-         catch_command_errors (execute_command, cmdarg_p.string,
-                               !batch_flag);
+         ret = catch_command_errors (execute_command, cmdarg_p.string,
+                                     !batch_flag);
          break;
        }
     }
@@ -1015,11 +1019,11 @@ captured_main_1 (struct captured_main_args *context)
   /* Now perform all the actions indicated by the arguments.  */
   if (cdarg != NULL)
     {
-      catch_command_errors (cd_command, cdarg, 0);
+      ret = catch_command_errors (cd_command, cdarg, 0);
     }
 
   for (i = 0; i < dirarg.size (); i++)
-    catch_command_errors (directory_switch, dirarg[i], 0);
+    ret = catch_command_errors (directory_switch, dirarg[i], 0);
 
   /* Skip auto-loading section-specified scripts until we've sourced
      local_gdbinit (which is often used to augment the source search
@@ -1034,19 +1038,20 @@ captured_main_1 (struct captured_main_args *context)
       /* 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 (exec_file_attach, execarg,
-                               !batch_flag))
-       catch_command_errors (symbol_file_add_main_adapter, symarg,
-                             !batch_flag);
+      ret = catch_command_errors (exec_file_attach, execarg,
+                                 !batch_flag);
+      if (ret != 0)
+       ret = catch_command_errors (symbol_file_add_main_adapter,
+                                   symarg, !batch_flag);
     }
   else
     {
       if (execarg != NULL)
-       catch_command_errors (exec_file_attach, execarg,
-                             !batch_flag);
+       ret = catch_command_errors (exec_file_attach, execarg,
+                                   !batch_flag);
       if (symarg != NULL)
-       catch_command_errors (symbol_file_add_main_adapter, symarg,
-                             !batch_flag);
+       ret = catch_command_errors (symbol_file_add_main_adapter,
+                                   symarg, !batch_flag);
     }
 
   if (corearg && pidarg)
@@ -1054,9 +1059,14 @@ captured_main_1 (struct captured_main_args *context)
             "a core file at the same time."));
 
   if (corearg != NULL)
-    catch_command_errors (core_file_command, corearg, !batch_flag);
+    {
+      ret = catch_command_errors (core_file_command, corearg,
+                                 !batch_flag);
+    }
   else if (pidarg != NULL)
-    catch_command_errors (attach_command, pidarg, !batch_flag);
+    {
+      ret = catch_command_errors (attach_command, pidarg, !batch_flag);
+    }
   else if (pid_or_core_arg)
     {
       /* The user specified 'gdb program pid' or gdb program core'.
@@ -1065,14 +1075,20 @@ captured_main_1 (struct captured_main_args *context)
 
       if (isdigit (pid_or_core_arg[0]))
        {
-         if (catch_command_errors (attach_command, pid_or_core_arg,
-                                   !batch_flag) == 0)
-           catch_command_errors (core_file_command, pid_or_core_arg,
-                                 !batch_flag);
+         ret = catch_command_errors (attach_command, pid_or_core_arg,
+                                     !batch_flag);
+         if (ret == 0)
+           ret = catch_command_errors (core_file_command,
+                                       pid_or_core_arg,
+                                       !batch_flag);
+       }
+      else
+       {
+         /* Can't be a pid, better be a corefile.  */
+         ret = catch_command_errors (core_file_command,
+                                     pid_or_core_arg,
+                                     !batch_flag);
        }
-      else /* Can't be a pid, better be a corefile.  */
-       catch_command_errors (core_file_command, pid_or_core_arg,
-                             !batch_flag);
     }
 
   if (ttyarg != NULL)
@@ -1096,7 +1112,7 @@ captured_main_1 (struct captured_main_args *context)
        {
          auto_load_local_gdbinit_loaded = 1;
 
-         catch_command_errors (source_script, local_gdbinit, 0);
+         ret = catch_command_errors (source_script, local_gdbinit, 0);
        }
     }
 
@@ -1105,7 +1121,7 @@ captured_main_1 (struct captured_main_args *context)
      We wait until now because it is common to add to the source search
      path in local_gdbinit.  */
   global_auto_load = save_auto_load;
-  ALL_OBJFILES (objfile)
+  for (objfile *objfile : current_program_space->objfiles ())
     load_auto_scripts_for_objfile (objfile);
 
   /* Process '-x' and '-ex' options.  */
@@ -1116,12 +1132,12 @@ captured_main_1 (struct captured_main_args *context)
       switch (cmdarg_p.type)
        {
        case CMDARG_FILE:
-         catch_command_errors (source_script, cmdarg_p.string,
-                               !batch_flag);
+         ret = catch_command_errors (source_script, cmdarg_p.string,
+                                     !batch_flag);
          break;
        case CMDARG_COMMAND:
-         catch_command_errors (execute_command, cmdarg_p.string,
-                               !batch_flag);
+         ret = catch_command_errors (execute_command, cmdarg_p.string,
+                                     !batch_flag);
          break;
        }
     }
@@ -1132,8 +1148,11 @@ captured_main_1 (struct captured_main_args *context)
 
   if (batch_flag)
     {
+      int error_status = EXIT_FAILURE;
+      int *exit_arg = ret == 0 ? &error_status : NULL;
+
       /* We have hit the end of the batch file.  */
-      quit_force (NULL, 0);
+      quit_force (exit_arg, 0);
     }
 }
 
This page took 0.031241 seconds and 4 git commands to generate.