2012-03-08 Luis Machado <lgustavo@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / top.c
index 2b14f75c37e95500e2d8e7eeb89fd3a2718179aa..55e4504009ae6ff7cae7661c83a9a48ca1fa78d5 100644 (file)
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1,8 +1,6 @@
 /* Top level stuff for GDB, the GNU debugger.
 
-   Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
-   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
-   2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 1986-2012 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -48,6 +46,7 @@
 #include "event-loop.h"
 #include "gdbthread.h"
 #include "python/python.h"
+#include "interps.h"
 
 /* readline include files.  */
 #include "readline/readline.h"
 #include "ui-out.h"
 #include "cli-out.h"
 
+#define PROMPT(X) the_prompts.prompt_stack[the_prompts.top + X].prompt
+#define PREFIX(X) the_prompts.prompt_stack[the_prompts.top + X].prefix
+#define SUFFIX(X) the_prompts.prompt_stack[the_prompts.top + X].suffix
+
 /* Default command line prompt.  This is overriden in some configs.  */
 
 #ifndef DEFAULT_PROMPT
@@ -95,11 +98,13 @@ int use_windows = 0;
 
 extern char lang_frame_mismatch_warn[];                /* language.c */
 
-/* Flag for whether we want all the "from_tty" gubbish printed.  */
+/* Flag for whether we want to confirm potentially dangerous
+   operations.  Default is yes.  */
+
+int confirm = 1;
 
-int caution = 1;               /* Default is yes, sigh.  */
 static void
-show_caution (struct ui_file *file, int from_tty,
+show_confirm (struct ui_file *file, int from_tty,
              struct cmd_list_element *c, const char *value)
 {
   fprintf_filtered (file, _("Whether to confirm potentially "
@@ -137,8 +142,8 @@ int xgdb_verbose;
 /* Buffer used for reading command lines, and the size
    allocated for it so far.  */
 
-char *line;
-int linesize = 100;
+char *saved_command_line;
+int saved_command_line_size = 100;
 
 /* Nonzero if the current command is modified by "server ".  This
    affects things like recording into the command history, commands
@@ -283,14 +288,13 @@ void (*deprecated_context_hook) (int id);
 /* NOTE 1999-04-29: This function will be static again, once we modify
    gdb to use the event loop as the default command loop and we merge
    event-top.c into this file, top.c.  */
-/* static */ int
-quit_cover (void *s)
+/* static */ void
+quit_cover (void)
 {
-  caution = 0;                 /* Throw caution to the wind -- we're exiting.
-                                  This prevents asking the user dumb 
-                                  questions.  */
+  /* Stop asking user for confirmation --- we're exiting.  This
+     prevents asking the user dumb questions.  */
+  confirm = 0;
   quit_command ((char *) 0, 0);
-  return 0;
 }
 #endif /* defined SIGHUP */
 \f
@@ -339,10 +343,14 @@ do_chdir_cleanup (void *old_dir)
 }
 #endif
 
-void
+struct cleanup *
 prepare_execute_command (void)
 {
-  free_all_values ();
+  struct value *mark;
+  struct cleanup *cleanup;
+
+  mark = value_mark ();
+  cleanup = make_cleanup_value_free_to_mark (mark);
 
   /* With multiple threads running while the one we're examining is
      stopped, the dcache can get stale without us being able to detect
@@ -350,6 +358,49 @@ prepare_execute_command (void)
      help things like backtrace.  */
   if (non_stop)
     target_dcache_invalidate ();
+
+  return cleanup;
+}
+
+/* Tell the user if the language has changed (except first time) after
+   executing a command.  */
+
+void
+check_frame_language_change (void)
+{
+  static int warned = 0;
+
+  /* First make sure that a new frame has been selected, in case the
+     command or the hooks changed the program state.  */
+  deprecated_safe_get_selected_frame ();
+  if (current_language != expected_language)
+    {
+      if (language_mode == language_mode_auto && info_verbose)
+       {
+         language_info (1);    /* Print what changed.  */
+       }
+      warned = 0;
+    }
+
+  /* Warn the user if the working language does not match the language
+     of the current frame.  Only warn the user if we are actually
+     running the program, i.e. there is a stack.  */
+  /* FIXME: This should be cacheing the frame and only running when
+     the frame changes.  */
+
+  if (has_stack_frames ())
+    {
+      enum language flang;
+
+      flang = get_frame_language ();
+      if (!warned
+         && flang != language_unknown
+         && flang != current_language->la_language)
+       {
+         printf_filtered ("%s\n", lang_frame_mismatch_warn);
+         warned = 1;
+       }
+    }
 }
 
 /* Execute the line P as a command, in the current user context.
@@ -358,12 +409,12 @@ prepare_execute_command (void)
 void
 execute_command (char *p, int from_tty)
 {
+  struct cleanup *cleanup_if_error, *cleanup;
   struct cmd_list_element *c;
-  enum language flang;
-  static int warned = 0;
   char *line;
 
-  prepare_execute_command ();
+  cleanup_if_error = make_bpstat_clear_actions_cleanup ();
+  cleanup = prepare_execute_command ();
 
   /* Force cleanup of any alloca areas if using C alloca instead of
      a builtin alloca.  */
@@ -371,7 +422,10 @@ execute_command (char *p, int from_tty)
 
   /* This can happen when command_line_input hits end of file.  */
   if (p == NULL)
-    return;
+    {
+      do_cleanups (cleanup);
+      return;
+    }
 
   target_log_command (p);
 
@@ -416,52 +470,38 @@ execute_command (char *p, int from_tty)
       if (c->flags & DEPRECATED_WARN_USER)
        deprecated_cmd_warning (&line);
 
-      if (c->class == class_user)
+      /* c->user_commands would be NULL in the case of a python command.  */
+      if (c->class == class_user && c->user_commands)
        execute_user_command (c, arg);
       else if (c->type == set_cmd || c->type == show_cmd)
-       do_setshow_command (arg, from_tty & caution, c);
+       do_setshow_command (arg, from_tty, c);
       else if (!cmd_func_p (c))
        error (_("That is not a command, just a help topic."));
       else if (deprecated_call_command_hook)
-       deprecated_call_command_hook (c, arg, from_tty & caution);
+       deprecated_call_command_hook (c, arg, from_tty);
       else
-       cmd_func (c, arg, from_tty & caution);
-       
-      /* If this command has been post-hooked, run the hook last.  */
-      execute_cmd_post_hook (c);
-
-    }
+       cmd_func (c, arg, from_tty);
 
-  /* Tell the user if the language has changed (except first time).
-     First make sure that a new frame has been selected, in case this
-     command or the hooks changed the program state.  */
-  deprecated_safe_get_selected_frame ();
-  if (current_language != expected_language)
-    {
-      if (language_mode == language_mode_auto && info_verbose)
+      /* If the interpreter is in sync mode (we're running a user
+        command's list, running command hooks or similars), and we
+        just ran a synchronous command that started the target, wait
+        for that command to end.  */
+      if (!interpreter_async && sync_execution)
        {
-         language_info (1);    /* Print what changed.  */
+         while (gdb_do_one_event () >= 0)
+           if (!sync_execution)
+             break;
        }
-      warned = 0;
-    }
 
-  /* Warn the user if the working language does not match the
-     language of the current frame.  Only warn the user if we are
-     actually running the program, i.e. there is a stack.  */
-  /* FIXME:  This should be cacheing the frame and only running when
-     the frame changes.  */
+      /* If this command has been post-hooked, run the hook last.  */
+      execute_cmd_post_hook (c);
 
-  if (has_stack_frames ())
-    {
-      flang = get_frame_language ();
-      if (!warned
-         && flang != language_unknown
-         && flang != current_language->la_language)
-       {
-         printf_filtered ("%s\n", lang_frame_mismatch_warn);
-         warned = 1;
-       }
     }
+
+  check_frame_language_change ();
+
+  do_cleanups (cleanup);
+  discard_cleanups (cleanup_if_error);
 }
 
 /* Run execute_command for P and FROM_TTY.  Capture its output into the
@@ -479,6 +519,9 @@ execute_command_to_string (char *p, int from_tty)
      restoration callbacks.  */
   cleanup = set_batch_flag_and_make_cleanup_restore_page_info ();
 
+  make_cleanup_restore_integer (&interpreter_async);
+  interpreter_async = 0;
+
   str_file = mem_fileopen ();
 
   make_cleanup_ui_file_delete (str_file);
@@ -488,10 +531,10 @@ execute_command_to_string (char *p, int from_tty)
   make_cleanup_restore_ui_file (&gdb_stdtarg);
   make_cleanup_restore_ui_file (&gdb_stdtargerr);
 
-  if (ui_out_redirect (uiout, str_file) < 0)
+  if (ui_out_redirect (current_uiout, str_file) < 0)
     warning (_("Current output protocol does not support redirection"));
   else
-    make_cleanup_ui_out_redirect_pop (uiout);
+    make_cleanup_ui_out_redirect_pop (current_uiout);
 
   gdb_stdout = str_file;
   gdb_stderr = str_file;
@@ -533,7 +576,10 @@ command_loop (void)
                                    get_prompt () : (char *) NULL,
                                    instream == stdin, "prompt");
       if (command == 0)
-       return;
+       {
+         do_cleanups (old_chain);
+         return;
+       }
 
       make_command_stats_cleanup (1);
 
@@ -563,7 +609,7 @@ dont_repeat (void)
      thing read from stdin in line and don't want to delete it.  Null
      lines won't repeat here in any case.  */
   if (instream == stdin)
-    *line = 0;
+    *saved_command_line = 0;
 }
 
 /* Prevent dont_repeat from working, and return a cleanup that
@@ -778,8 +824,7 @@ gdb_readline_wrapper (char *prompt)
     (*after_char_processing_hook) ();
   gdb_assert (after_char_processing_hook == NULL);
 
-  /* gdb_do_one_event argument is unused.  */
-  while (gdb_do_one_event (NULL) >= 0)
+  while (gdb_do_one_event () >= 0)
     if (gdb_readline_wrapper_done)
       break;
 
@@ -1025,10 +1070,10 @@ command_line_input (char *prompt_arg, int repeat, char *annotation_suffix)
   /* If we just got an empty line, and that is supposed to repeat the
      previous command, return the value in the global buffer.  */
   if (repeat && p == linebuffer)
-    return line;
+    return saved_command_line;
   for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
   if (repeat && !*p1)
-    return line;
+    return saved_command_line;
 
   *p = 0;
 
@@ -1049,13 +1094,13 @@ command_line_input (char *prompt_arg, int repeat, char *annotation_suffix)
   /* Save into global buffer if appropriate.  */
   if (repeat)
     {
-      if (linelength > linesize)
+      if (linelength > saved_command_line_size)
        {
-         line = xrealloc (line, linelength);
-         linesize = linelength;
+         saved_command_line = xrealloc (saved_command_line, linelength);
+         saved_command_line_size = linelength;
        }
-      strcpy (line, linebuffer);
-      return line;
+      strcpy (saved_command_line, linebuffer);
+      return saved_command_line;
     }
 
   return linebuffer;
@@ -1074,7 +1119,7 @@ print_gdb_version (struct ui_file *stream)
   /* Second line is a copyright notice.  */
 
   fprintf_filtered (stream,
-                   "Copyright (C) 2011 Free Software Foundation, Inc.\n");
+                   "Copyright (C) 2012 Free Software Foundation, Inc.\n");
 
   /* Following the copyright is a brief statement that the program is
      free software, that users are free to copy and change it on
@@ -1109,23 +1154,28 @@ and \"show warranty\" for details.\n");
     }
 }
 \f
-/* get_prompt: access method for the GDB prompt string.  */
+
+/* The current top level prompt, settable with "set prompt", and/or
+   with the python `gdb.prompt_hook' hook.  */
+static char *top_prompt;
+
+/* Access method for the GDB prompt string.  */
 
 char *
 get_prompt (void)
 {
-  return PROMPT (0);
+  return top_prompt;
 }
 
+/* Set method for the GDB prompt string.  */
+
 void
-set_prompt (char *s)
+set_prompt (const char *s)
 {
-/* ??rehrauer: I don't know why this fails, since it looks as though
-   assignments to prompt are wrapped in calls to xstrdup...
-   if (prompt != NULL)
-     xfree (prompt);
- */
-  PROMPT (0) = xstrdup (s);
+  char *p = xstrdup (s);
+
+  xfree (top_prompt);
+  top_prompt = p;
 }
 \f
 
@@ -1305,8 +1355,9 @@ input_from_terminal_p (void)
 static void
 dont_repeat_command (char *ignored, int from_tty)
 {
-  *line = 0;                   /* Can't call dont_repeat here because we're 
-                                  not necessarily reading from stdin.  */
+  /* Can't call dont_repeat here because we're not necessarily reading
+     from stdin.  */
+  *saved_command_line = 0;
 }
 \f
 /* Functions to manipulate command line editing control variables.  */
@@ -1478,8 +1529,8 @@ init_history (void)
 }
 
 static void
-show_new_async_prompt (struct ui_file *file, int from_tty,
-                      struct cmd_list_element *c, const char *value)
+show_prompt (struct ui_file *file, int from_tty,
+            struct cmd_list_element *c, const char *value)
 {
   fprintf_filtered (file, _("Gdb's prompt is \"%s\".\n"), value);
 }
@@ -1511,23 +1562,13 @@ show_exec_done_display_p (struct ui_file *file, int from_tty,
 static void
 init_main (void)
 {
-  /* initialize the prompt stack to a simple "(gdb) " prompt or to
-     whatever the DEFAULT_PROMPT is.  */
-  the_prompts.top = 0;
-  PREFIX (0) = "";
-  PROMPT (0) = xstrdup (DEFAULT_PROMPT);
-  SUFFIX (0) = "";
+  /* Initialize the prompt to a simple "(gdb) " prompt or to whatever
+     the DEFAULT_PROMPT is.  */
+  set_prompt (DEFAULT_PROMPT);
+
   /* Set things up for annotation_level > 1, if the user ever decides
      to use it.  */
   async_annotation_suffix = "prompt";
-  /* Set the variable associated with the setshow prompt command.  */
-  new_async_prompt = xstrdup (PROMPT (0));
-
-  /* If gdb was started with --annotate=2, this is equivalent to the
-     user entering the command 'set annotate 2' at the gdb prompt, so
-     we need to do extra processing.  */
-  if (annotation_level > 1)
-    set_async_annotation_level (NULL, 0, NULL);
 
   /* Set the important stuff up for command editing.  */
   command_editing_p = 1;
@@ -1547,11 +1588,11 @@ init_main (void)
   rl_add_defun ("operate-and-get-next", gdb_rl_operate_and_get_next, 15);
 
   add_setshow_string_cmd ("prompt", class_support,
-                         &new_async_prompt,
+                         &top_prompt,
                          _("Set gdb's prompt"),
                          _("Show gdb's prompt"),
-                         NULL, set_async_prompt,
-                         show_new_async_prompt,
+                         NULL, NULL,
+                         show_prompt,
                          &setlist, &showlist);
 
   add_com ("dont-repeat", class_support, dont_repeat_command, _("\
@@ -1595,11 +1636,11 @@ Show the filename in which to record the command history"), _("\
                            show_history_filename,
                            &sethistlist, &showhistlist);
 
-  add_setshow_boolean_cmd ("confirm", class_support, &caution, _("\
+  add_setshow_boolean_cmd ("confirm", class_support, &confirm, _("\
 Set whether to confirm potentially dangerous operations."), _("\
 Show whether to confirm potentially dangerous operations."), NULL,
                           NULL,
-                          show_caution,
+                          show_confirm,
                           &setlist, &showlist);
 
   add_setshow_zinteger_cmd ("annotate", class_obscure, &annotation_level, _("\
@@ -1607,7 +1648,7 @@ Set annotation_level."), _("\
 Show annotation_level."), _("\
 0 == normal;     1 == fullname (for use when running under emacs)\n\
 2 == output annotated suitably for use by programs that control GDB."),
-                           set_async_annotation_level,
+                           NULL,
                            show_annotation_level,
                            &setlist, &showlist);
 
This page took 0.038365 seconds and 4 git commands to generate.