* gc++filt.m4: New file.
[deliverable/binutils-gdb.git] / gdb / event-top.c
index 635ce0fdc5de3fdd2f7d435211e7681d375f30e4..0fc7f899a0e7b809cc9ca9eefa8807326b7ca460 100644 (file)
@@ -1,7 +1,7 @@
 /* Top level stuff for GDB, the GNU debugger.
 
-   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005 Free Software
-   Foundation, Inc.
+   Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2007, 2008, 2009, 2010
+   Free Software Foundation, Inc.
 
    Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
 
@@ -9,7 +9,7 @@
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -18,9 +18,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "defs.h"
 #include "top.h"
@@ -32,6 +30,9 @@
 #include "interps.h"
 #include <signal.h>
 #include "exceptions.h"
+#include "cli/cli-script.h"     /* for reset_command_nest_depth */
+#include "main.h"
+#include "gdbthread.h"
 
 /* For dont_repeat() */
 #include "gdbcmd.h"
@@ -45,7 +46,6 @@
 
 static void rl_callback_read_char_wrapper (gdb_client_data client_data);
 static void command_line_handler (char *rl);
-static void command_line_handler_continuation (struct continuation_arg *arg);
 static void change_line_handler (void);
 static void change_annotation_level (void);
 static void command_handler (char *command);
@@ -185,19 +185,20 @@ rl_callback_read_char_wrapper (gdb_client_data client_data)
 void
 cli_command_loop (void)
 {
-  int length;
-  char *a_prompt;
-  char *gdb_prompt = get_prompt ();
-
   /* If we are using readline, set things up and display the first
      prompt, otherwise just print the prompt. */
   if (async_command_editing_p)
     {
+      int length;
+      char *a_prompt;
+      char *gdb_prompt = get_prompt ();
+
       /* Tell readline what the prompt to display is and what function it
          will need to call after a whole line is read. This also displays
          the first prompt. */
-      length = strlen (PREFIX (0)) + strlen (gdb_prompt) + strlen (SUFFIX (0)) + 1;
-      a_prompt = (char *) xmalloc (length);
+      length = strlen (PREFIX (0)) 
+       + strlen (gdb_prompt) + strlen (SUFFIX (0)) + 1;
+      a_prompt = (char *) alloca (length);
       strcpy (a_prompt, PREFIX (0));
       strcat (a_prompt, gdb_prompt);
       strcat (a_prompt, SUFFIX (0));
@@ -260,12 +261,15 @@ display_gdb_prompt (char *new_prompt)
   int prompt_length = 0;
   char *gdb_prompt = get_prompt ();
 
+  /* Reset the nesting depth used when trace-commands is set.  */
+  reset_command_nest_depth ();
+
   /* Each interpreter has its own rules on displaying the command
      prompt.  */
   if (!current_interp_display_prompt_p ())
     return;
 
-  if (target_executing && sync_execution)
+  if (sync_execution && is_running (inferior_ptid))
     {
       /* This is to trick readline into not trying to display the
          prompt.  Even though we display the prompt using this
@@ -368,22 +372,22 @@ change_annotation_level (void)
 /* Pushes a new prompt on the prompt stack. Each prompt has three
    parts: prefix, prompt, suffix. Usually prefix and suffix are empty
    strings, except when the annotation level is 2. Memory is allocated
-   within savestring for the new prompt. */
+   within xstrdup for the new prompt. */
 void
 push_prompt (char *prefix, char *prompt, char *suffix)
 {
   the_prompts.top++;
-  PREFIX (0) = savestring (prefix, strlen (prefix));
+  PREFIX (0) = xstrdup (prefix);
 
   /* Note that this function is used by the set annotate 2
      command. This is why we take care of saving the old prompt
      in case a new one is not specified. */
   if (prompt)
-    PROMPT (0) = savestring (prompt, strlen (prompt));
+    PROMPT (0) = xstrdup (prompt);
   else
-    PROMPT (0) = savestring (PROMPT (-1), strlen (PROMPT (-1)));
+    PROMPT (0) = xstrdup (PROMPT (-1));
 
-  SUFFIX (0) = savestring (suffix, strlen (suffix));
+  SUFFIX (0) = xstrdup (suffix);
 }
 
 /* Pops the top of the prompt stack, and frees the memory allocated for it. */
@@ -400,7 +404,7 @@ pop_prompt (void)
     if (strcmp (PROMPT (0), PROMPT (-1)))
       {
        xfree (PROMPT (-1));
-       PROMPT (-1) = savestring (PROMPT (0), strlen (PROMPT (0)));
+       PROMPT (-1) = xstrdup (PROMPT (0));
       }
 
   xfree (PREFIX (0));
@@ -421,6 +425,7 @@ stdin_event_handler (int error, gdb_client_data client_data)
       printf_unfiltered (_("error detected on stdin\n"));
       delete_file_handler (input_fd);
       discard_all_continuations ();
+      discard_all_intermediate_continuations ();
       /* If stdin died, we may as well kill gdb. */
       quit_command ((char *) 0, stdin == instream);
     }
@@ -433,15 +438,18 @@ stdin_event_handler (int error, gdb_client_data client_data)
    the exec operation. */
 
 void
-async_enable_stdin (void *dummy)
+async_enable_stdin (void)
 {
-  /* See NOTE in async_disable_stdin() */
-  /* FIXME: cagney/1999-09-27: Call this before clearing
-     sync_execution.  Current target_terminal_ours() implementations
-     check for sync_execution before switching the terminal. */
-  target_terminal_ours ();
-  pop_prompt ();
-  sync_execution = 0;
+  if (sync_execution)
+    {
+      /* See NOTE in async_disable_stdin() */
+      /* FIXME: cagney/1999-09-27: Call this before clearing
+        sync_execution.  Current target_terminal_ours() implementations
+        check for sync_execution before switching the terminal. */
+      target_terminal_ours ();
+      pop_prompt ();
+      sync_execution = 0;
+    }
 }
 
 /* Disable reads from stdin (the console) marking the command as
@@ -450,19 +458,11 @@ async_enable_stdin (void *dummy)
 void
 async_disable_stdin (void)
 {
-  sync_execution = 1;
-  push_prompt ("", "", "");
-  /* FIXME: cagney/1999-09-27: At present this call is technically
-     redundant since infcmd.c and infrun.c both already call
-     target_terminal_inferior().  As the terminal handling (in
-     sync/async mode) is refined, the duplicate calls can be
-     eliminated (Here or in infcmd.c/infrun.c). */
-  target_terminal_inferior ();
-  /* Add the reinstate of stdin to the list of cleanups to be done
-     in case the target errors out and dies. These cleanups are also
-     done in case of normal successful termination of the execution
-     command, by complete_execution(). */
-  make_exec_error_cleanup (async_enable_stdin, NULL);
+  if (!sync_execution)
+    {
+      sync_execution = 1;
+      push_prompt ("", "", "");
+    }
 }
 \f
 
@@ -475,21 +475,12 @@ async_disable_stdin (void)
 static void
 command_handler (char *command)
 {
-  struct cleanup *old_chain;
   int stdin_is_tty = ISATTY (stdin);
-  struct continuation_arg *arg1;
-  struct continuation_arg *arg2;
-  long time_at_cmd_start;
-#ifdef HAVE_SBRK
-  long space_at_cmd_start = 0;
-#endif
-  extern int display_time;
-  extern int display_space;
+  struct cleanup *stat_chain;
 
   quit_flag = 0;
   if (instream == stdin && stdin_is_tty)
     reinitialize_more_filter ();
-  old_chain = make_cleanup (null_cleanup, 0);
 
   /* If readline returned a NULL command, it means that the 
      connection with the terminal is gone. This happens at the
@@ -497,105 +488,19 @@ command_handler (char *command)
      but GDB is still alive. In such a case, we just quit gdb
      killing the inferior program too. */
   if (command == 0)
-    quit_command ((char *) 0, stdin == instream);
-
-  time_at_cmd_start = get_run_time ();
-
-  if (display_space)
     {
-#ifdef HAVE_SBRK
-      char *lim = (char *) sbrk (0);
-      space_at_cmd_start = lim - lim_at_start;
-#endif
-    }
-
-  execute_command (command, instream == stdin);
-
-  /* Set things up for this function to be compete later, once the
-     execution has completed, if we are doing an execution command,
-     otherwise, just go ahead and finish. */
-  if (target_can_async_p () && target_executing)
-    {
-      arg1 =
-       (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
-      arg2 =
-       (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
-      arg1->next = arg2;
-      arg2->next = NULL;
-      arg1->data.longint = time_at_cmd_start;
-#ifdef HAVE_SBRK
-      arg2->data.longint = space_at_cmd_start;
-#endif
-      add_continuation (command_line_handler_continuation, arg1);
-    }
-
-  /* Do any commands attached to breakpoint we stopped at. Only if we
-     are always running synchronously. Or if we have just executed a
-     command that doesn't start the target. */
-  if (!target_can_async_p () || !target_executing)
-    {
-      bpstat_do_actions (&stop_bpstat);
-      do_cleanups (old_chain);
-
-      if (display_time)
-       {
-         long cmd_time = get_run_time () - time_at_cmd_start;
-
-         printf_unfiltered (_("Command execution time: %ld.%06ld\n"),
-                            cmd_time / 1000000, cmd_time % 1000000);
-       }
-
-      if (display_space)
-       {
-#ifdef HAVE_SBRK
-         char *lim = (char *) sbrk (0);
-         long space_now = lim - lim_at_start;
-         long space_diff = space_now - space_at_cmd_start;
-
-         printf_unfiltered (_("Space used: %ld (%c%ld for this command)\n"),
-                            space_now,
-                            (space_diff >= 0 ? '+' : '-'),
-                            space_diff);
-#endif
-       }
+      printf_unfiltered ("quit\n");
+      execute_command ("quit", stdin == instream);
     }
-}
 
-/* Do any commands attached to breakpoint we stopped at. Only if we
-   are always running synchronously. Or if we have just executed a
-   command that doesn't start the target. */
-void
-command_line_handler_continuation (struct continuation_arg *arg)
-{
-  extern int display_time;
-  extern int display_space;
-
-  long time_at_cmd_start  = arg->data.longint;
-  long space_at_cmd_start = arg->next->data.longint;
+  stat_chain = make_command_stats_cleanup (1);
 
-  bpstat_do_actions (&stop_bpstat);
-  /*do_cleanups (old_chain); *//*?????FIXME????? */
+  execute_command (command, instream == stdin);
 
-  if (display_time)
-    {
-      long cmd_time = get_run_time () - time_at_cmd_start;
+  /* Do any commands attached to breakpoint we stopped at.  */
+  bpstat_do_actions ();
 
-      printf_unfiltered (_("Command execution time: %ld.%06ld\n"),
-                        cmd_time / 1000000, cmd_time % 1000000);
-    }
-  if (display_space)
-    {
-#ifdef HAVE_SBRK
-      char *lim = (char *) sbrk (0);
-      long space_now = lim - lim_at_start;
-      long space_diff = space_now - space_at_cmd_start;
-
-      printf_unfiltered (_("Space used: %ld (%c%ld for this command)\n"),
-                        space_now,
-                        (space_diff >= 0 ? '+' : '-'),
-                        space_diff);
-#endif
-    }
+  do_cleanups (stat_chain);
 }
 
 /* Handle a complete line of input. This is called by the callback
@@ -618,7 +523,6 @@ command_line_handler (char *rl)
   char *nline;
   char got_eof = 0;
 
-
   int repeat = (instream == stdin);
 
   if (annotation_level > 1 && instream == stdin)
@@ -665,6 +569,7 @@ command_line_handler (char *rl)
     {
       got_eof = 1;
       command_handler (0);
+      return;                  /* Lint. */
     }
   if (strlen (rl) + 1 + (p - linebuffer) > linelength)
     {
@@ -683,10 +588,10 @@ command_line_handler (char *rl)
 
   if (p > linebuffer && *(p - 1) == '\\')
     {
+      *p = '\0';
       p--;                     /* Put on top of '\'.  */
 
-      readline_input_state.linebuffer = savestring (linebuffer,
-                                                   strlen (linebuffer));
+      readline_input_state.linebuffer = xstrdup (linebuffer);
       readline_input_state.linebuffer_ptr = p;
 
       /* We will not invoke a execute_command if there is more
@@ -745,8 +650,8 @@ command_line_handler (char *rl)
            }
          strcpy (linebuffer, history_value);
          p = linebuffer + strlen (linebuffer);
-         xfree (history_value);
        }
+      xfree (history_value);
     }
 
   /* If we just got an empty line, and that is supposed
@@ -856,6 +761,7 @@ gdb_readline2 (gdb_client_data client_data)
            break;
          xfree (result);
          (*input_handler) (0);
+         return;
        }
 
       if (c == '\n')
@@ -954,19 +860,22 @@ handle_sigint (int sig)
 {
   signal (sig, handle_sigint);
 
+  /* We could be running in a loop reading in symfiles or something so
+     it may be quite a while before we get back to the event loop.  So
+     set quit_flag to 1 here. Then if QUIT is called before we get to
+     the event loop, we will unwind as expected.  */
+
+  quit_flag = 1;
+
   /* If immediate_quit is set, we go ahead and process the SIGINT right
      away, even if we usually would defer this to the event loop. The
      assumption here is that it is safe to process ^C immediately if
      immediate_quit is set. If we didn't, SIGINT would be really
      processed only the next time through the event loop.  To get to
      that point, though, the command that we want to interrupt needs to
-     finish first, which is unacceptable. */
-  if (immediate_quit)
-    async_request_quit (0);
-  else
-    /* If immediate quit is not set, we process SIGINT the next time
-       through the loop, which is fine. */
-    mark_async_signal_handler_wrapper (sigint_token);
+     finish first, which is unacceptable.  If immediate quit is not set,
+     we process SIGINT the next time through the loop, which is fine. */
+  gdb_call_async_signal_handler (sigint_token, immediate_quit);
 }
 
 /* Quit GDB if SIGTERM is received.
@@ -982,8 +891,14 @@ handle_sigterm (int sig)
 void
 async_request_quit (gdb_client_data arg)
 {
-  quit_flag = 1;
-  quit ();
+  /* If the quit_flag has gotten reset back to 0 by the time we get
+     back here, that means that an exception was thrown to unwind the
+     current command before we got back to the event loop.  So there
+     is no reason to call quit again here, unless immediate_quit is
+     set.*/
+
+  if (quit_flag || immediate_quit)
+    quit ();
 }
 
 #ifdef SIGQUIT
@@ -1025,7 +940,7 @@ async_disconnect (gdb_client_data arg)
                "Could not kill the program being debugged",
                RETURN_MASK_ALL);
   signal (SIGHUP, SIG_DFL);    /*FIXME: ??????????? */
-  kill (getpid (), SIGHUP);
+  raise (SIGHUP);
 }
 #endif
 
@@ -1041,6 +956,7 @@ static void
 async_stop_sig (gdb_client_data arg)
 {
   char *prompt = get_prompt ();
+
 #if STOP_SIGNAL == SIGTSTP
   signal (SIGTSTP, SIG_DFL);
 #if HAVE_SIGPROCMASK
@@ -1053,7 +969,7 @@ async_stop_sig (gdb_client_data arg)
 #elif HAVE_SIGSETMASK
   sigsetmask (0);
 #endif
-  kill (getpid (), SIGTSTP);
+  raise (SIGTSTP);
   signal (SIGTSTP, handle_stop_sig);
 #else
   signal (STOP_SIGNAL, handle_stop_sig);
@@ -1114,7 +1030,7 @@ set_async_annotation_level (char *args, int from_tty, struct cmd_list_element *c
 void
 set_async_prompt (char *args, int from_tty, struct cmd_list_element *c)
 {
-  PROMPT (0) = savestring (new_async_prompt, strlen (new_async_prompt));
+  PROMPT (0) = xstrdup (new_async_prompt);
 }
 
 /* Set things up for readline to be invoked via the alternate
@@ -1127,8 +1043,6 @@ gdb_setup_readline (void)
      that the sync setup is ALL done in gdb_init, and we would only
      mess it up here.  The sync stuff should really go away over
      time.  */
-  extern int batch_silent;
-
   if (!batch_silent)
     gdb_stdout = stdio_fileopen (stdout);
   gdb_stderr = stdio_fileopen (stderr);
This page took 0.030379 seconds and 4 git commands to generate.