Add a symbol's value to the computed frag offset, rather than overwriting it.
[deliverable/binutils-gdb.git] / gdb / top.c
index 2271d2a0496ece0d30f91c412fa250489aba18f3..9747d4929127b7a3c392af4f0beceb28a0e277ea 100644 (file)
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1,6 +1,5 @@
 /* Top level stuff for GDB, the GNU debugger.
-   Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999
-   Free Software Foundation, Inc.
+   Copyright 1986-2000 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 
 #include <sys/types.h>
 
-#include "event-loop.h"
+#include <setjmp.h>
+
+#include "event-top.h"
 #include "gdb_string.h"
 #include "gdb_stat.h"
 #include <ctype.h>
+#ifdef UI_OUT
+#include "ui-out.h"
+#include "cli-out.h"
+#endif
 
 /* Prototypes for local functions */
 
@@ -287,7 +292,7 @@ char *line;
 int linesize = 100;
 
 /* Nonzero if the current command is modified by "server ".  This
-   affects things like recording into the command history, comamnds
+   affects things like recording into the command history, commands
    repeating on RETURN, etc.  This is so a user interface (emacs, GUI,
    whatever) can issue its own commands and also send along commands
    from the user, and have the user not notice that the user interface
@@ -303,7 +308,25 @@ int baud_rate = -1;
 
 /* Timeout limit for response from target. */
 
-int remote_timeout = 20;       /* Set default to 20 */
+/* The default value has been changed many times over the years.  It 
+   was originally 5 seconds.  But that was thought to be a long time 
+   to sit and wait, so it was changed to 2 seconds.  That was thought
+   to be plenty unless the connection was going through some terminal 
+   server or multiplexer or other form of hairy serial connection.
+
+   In mid-1996, remote_timeout was moved from remote.c to top.c and 
+   it began being used in other remote-* targets.  It appears that the
+   default was changed to 20 seconds at that time, perhaps because the
+   Hitachi E7000 ICE didn't always respond in a timely manner.
+
+   But if 5 seconds is a long time to sit and wait for retransmissions,
+   20 seconds is far worse.  This demonstrates the difficulty of using 
+   a single variable for all protocol timeouts.
+
+   As remote.c is used much more than remote-e7000.c, it was changed 
+   back to 2 seconds in 1999. */
+
+int remote_timeout = 2;
 
 /* Non-zero tells remote* modules to output debugging info.  */
 
@@ -374,10 +397,6 @@ int (*ui_loop_hook) PARAMS ((int));
 void (*command_loop_hook) PARAMS ((void));
 
 
-/* Called instead of fputs for all output.  */
-
-void (*fputs_unfiltered_hook) PARAMS ((const char *linebuffer, GDB_FILE * stream));
-
 /* Called from print_frame_info to list the line we stopped in.  */
 
 void (*print_frame_info_listing_hook) PARAMS ((struct symtab * s, int line,
@@ -390,10 +409,6 @@ int (*query_hook) PARAMS ((const char *, va_list));
 
 void (*warning_hook) PARAMS ((const char *, va_list));
 
-/* Called from gdb_flush to flush output.  */
-
-void (*flush_hook) PARAMS ((GDB_FILE * stream));
-
 /* These three functions support getting lines of text from the user.  They
    are used in sequence.  First readline_begin_hook is called with a text
    string that might be (for example) a message for the user to type in a
@@ -416,6 +431,12 @@ void (*create_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
 void (*delete_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
 void (*modify_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
 
+/* Called as appropriate to notify the interface that we have attached
+   to or detached from an already running process. */
+
+void (*attach_hook) PARAMS ((void));
+void (*detach_hook) PARAMS ((void));
+
 /* Called during long calculations to allow GUI to repair window damage, and to
    check for stop buttons, etc... */
 
@@ -461,16 +482,28 @@ NORETURN void (*error_hook)
 PARAMS ((void)) ATTR_NORETURN;
 \f
 
+/* Generally one should use catch_errors rather than manipulating these
+   directly.  The exception is main().  */
+#if defined(HAVE_SIGSETJMP)
+#define SIGJMP_BUF             sigjmp_buf
+#define SIGSETJMP(buf)         sigsetjmp(buf, 1)
+#define SIGLONGJMP(buf,val)    siglongjmp(buf,val)
+#else
+#define SIGJMP_BUF             jmp_buf
+#define SIGSETJMP(buf)         setjmp(buf)
+#define SIGLONGJMP(buf,val)    longjmp(buf,val)
+#endif
+
 /* Where to go for return_to_top_level (RETURN_ERROR).  */
-     SIGJMP_BUF error_return;
+static SIGJMP_BUF error_return;
 /* Where to go for return_to_top_level (RETURN_QUIT).  */
-     SIGJMP_BUF quit_return;
+static SIGJMP_BUF quit_return;
 
 /* Return for reason REASON.  This generally gets back to the command
    loop, but can be caught via catch_errors.  */
 
-     NORETURN void
-       return_to_top_level (reason)
+NORETURN void
+return_to_top_level (reason)
      enum return_reason reason;
 {
   quit_flag = 0;
@@ -482,8 +515,10 @@ PARAMS ((void)) ATTR_NORETURN;
 
   disable_current_display ();
   do_cleanups (ALL_CLEANUPS);
-  if (async_p && target_has_async)
+  if (event_loop_p && target_can_async_p () && !target_executing)
     do_exec_cleanups (ALL_CLEANUPS);
+  if (event_loop_p && sync_execution)
+    do_exec_error_cleanups (ALL_CLEANUPS);
 
   if (annotation_level > 1)
     switch (reason)
@@ -519,6 +554,25 @@ PARAMS ((void)) ATTR_NORETURN;
    catch_errors.  Note that quit should return to the command line
    fairly quickly, even if some further processing is being done.  */
 
+/* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
+   error() et.al. could maintain a set of flags that indicate the the
+   current state of each of the longjmp buffers.  This would give the
+   longjmp code the chance to detect a longjmp botch (before it gets
+   to longjmperror()).  Prior to 1999-11-05 this wasn't possible as
+   code also randomly used a SET_TOP_LEVEL macro that directly
+   initialize the longjmp buffers. */
+
+/* MAYBE: cagney/1999-11-05: Since the SET_TOP_LEVEL macro has been
+   eliminated it is now possible to use the stack to directly store
+   each longjmp buffer.  The global code would just need to update a
+   pointer (onto the stack - ulgh!?) indicating the current longjmp
+   buffers. It would certainly improve the performance of the longjmp
+   code since the memcpy's would be eliminated. */
+
+/* MAYBE: cagney/1999-11-05: Should the catch_erros and cleanups code
+   be consolidated into a single file instead of being distributed
+   between utils.c and top.c? */
+
 int
 catch_errors (func, args, errstring, mask)
      catch_errors_ftype *func;
@@ -556,6 +610,14 @@ catch_errors (func, args, errstring, mask)
       if (mask & RETURN_MASK_QUIT)
        memcpy (quit_return, tmp_jmp, sizeof (SIGJMP_BUF));
       val = (*func) (args);
+      /* FIXME: cagney/1999-11-05: A correct FUNC implementaton will
+         clean things up (restoring the cleanup chain) to the state
+         they were just prior to the call.  Technically, this means
+         that the below restore_cleanups call is redundant.
+         Unfortunatly, many FUNC's are not that well behaved.
+         restore_cleanups should either be replaced with a do_cleanups
+         call (to cover the problem) or an assertion check to detect
+         bad FUNCs code. */
     }
   else
     val = 0;
@@ -575,6 +637,41 @@ catch_errors (func, args, errstring, mask)
   return val;
 }
 
+struct captured_command_args
+  {
+    catch_command_errors_ftype *command;
+    char *arg;
+    int from_tty;
+  };
+
+static int
+do_captured_command (void *data)
+{
+  struct captured_command_args *context = data;
+  context->command (context->arg, context->from_tty);
+  /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
+     isn't needed.  Instead an assertion check could be made that
+     simply confirmed that the called function correctly cleaned up
+     after its self.  Unfortunatly, old code (prior to 1999-11-04) in
+     main.c was calling SET_TOP_LEVEL(), calling the command function,
+     and then *always* calling do_cleanups().  For the moment we
+     remain ``bug compatible'' with that old code..  */
+  do_cleanups (ALL_CLEANUPS);
+  return 1;
+}
+
+int
+catch_command_errors (catch_command_errors_ftype *command,
+                     char *arg, int from_tty, return_mask mask)
+{
+  struct captured_command_args args;
+  args.command = command;
+  args.arg = arg;
+  args.from_tty = from_tty;
+  return catch_errors (do_captured_command, &args, "", mask);
+}
+
+
 /* Handler for SIGHUP.  */
 
 #ifdef SIGHUP
@@ -681,13 +778,14 @@ gdb_init (argv0)
   initialize_targets ();       /* Setup target_terminal macros for utils.c */
   initialize_utils ();         /* Make errors and warnings possible */
   initialize_all_files ();
+  initialize_current_architecture ();
   init_main ();                        /* But that omits this file!  Do it now */
 
   /* The signal handling mechanism is different depending whether or
      not the async version is run. NOTE: in the future we plan to make
      the event loop be the default engine of gdb, and this difference
      will disappear. */
-  if (async_p)
+  if (event_loop_p)
     async_init_signals ();
   else
     init_signals ();
@@ -698,8 +796,11 @@ gdb_init (argv0)
   set_language (language_c);
   expected_language = current_language;                /* don't warn about the change.  */
 
-  /* All the interpreters should have had a look at things by now.
-     Initialize the selected interpreter. */
+#ifdef UI_OUT
+  /* Install the default UI */
+  uiout = cli_out_new (gdb_stdout);
+#endif
+
   if (init_ui_hook)
     init_ui_hook (argv0);
 }
@@ -759,11 +860,102 @@ get_command_line (type, arg)
 }
 
 /* Recursively print a command (including full control structures).  */
+#ifdef UI_OUT
+void
+print_command_lines (uiout, cmd, depth)
+     struct ui_out *uiout;
+     struct command_line *cmd;
+     unsigned int depth;
+{
+  struct command_line *list;
+
+  list = cmd;
+  while (list)
+    {
+
+      if (depth)
+       ui_out_spaces (uiout, 2 * depth);
+
+      /* A simple command, print it and continue.  */
+      if (list->control_type == simple_control)
+       {
+         ui_out_field_string (uiout, NULL, list->line);
+         ui_out_text (uiout, "\n");
+         list = list->next;
+         continue;
+       }
+
+      /* loop_continue to jump to the start of a while loop, print it
+         and continue. */
+      if (list->control_type == continue_control)
+       {
+         ui_out_field_string (uiout, NULL, "loop_continue");
+         ui_out_text (uiout, "\n");
+         list = list->next;
+         continue;
+       }
+
+      /* loop_break to break out of a while loop, print it and continue.  */
+      if (list->control_type == break_control)
+       {
+         ui_out_field_string (uiout, NULL, "loop_break");
+         ui_out_text (uiout, "\n");
+         list = list->next;
+         continue;
+       }
+
+      /* A while command.  Recursively print its subcommands and continue.  */
+      if (list->control_type == while_control)
+       {
+         ui_out_text (uiout, "while ");
+         ui_out_field_fmt (uiout, NULL, "while %s", list->line);
+         ui_out_text (uiout, "\n");
+         print_command_lines (uiout, *list->body_list, depth + 1);
+         ui_out_field_string (uiout, NULL, "end");
+         if (depth)
+           ui_out_spaces (uiout, 2 * depth);
+         ui_out_text (uiout, "end\n");
+         list = list->next;
+         continue;
+       }
+
+      /* An if command.  Recursively print both arms before continueing.  */
+      if (list->control_type == if_control)
+       {
+         ui_out_text (uiout, "if ");
+         ui_out_field_fmt (uiout, NULL, "if %s", list->line);
+         ui_out_text (uiout, "\n");
+         /* The true arm. */
+         print_command_lines (uiout, list->body_list[0], depth + 1);
+
+         /* Show the false arm if it exists.  */
+         if (list->body_count == 2)
+           {
+             if (depth)
+               ui_out_spaces (uiout, 2 * depth);
+             ui_out_field_string (uiout, NULL, "else");
+             ui_out_text (uiout, "else\n");
+             print_command_lines (uiout, list->body_list[1], depth + 1);
+           }
+
+         ui_out_field_string (uiout, NULL, "end");
+         if (depth)
+           ui_out_spaces (uiout, 2 * depth);
+         ui_out_text (uiout, "end\n");
+         list = list->next;
+         continue;
+       }
+
+      /* ignore illegal command type and try next */
+      list = list->next;
+    }                          /* while (list) */
+}
+#else
 void
 print_command_line (cmd, depth, stream)
      struct command_line *cmd;
      unsigned int depth;
-     GDB_FILE *stream;
+     struct ui_file *stream;
 {
   unsigned int i;
 
@@ -839,6 +1031,7 @@ print_command_line (cmd, depth, stream)
       fputs_filtered ("end\n", stream);
     }
 }
+#endif
 
 /* Execute the command in CMD.  */
 
@@ -1274,7 +1467,7 @@ execute_command (p, from_tty)
 
       /* If the target is running, we allow only a limited set of
          commands. */
-      if (async_p && target_has_async && target_executing)
+      if (event_loop_p && target_can_async_p () && target_executing)
        if (!strcmp (c->name, "help")
            && !strcmp (c->name, "pwd")
            && !strcmp (c->name, "show")
@@ -1438,6 +1631,41 @@ command_loop ()
     }
 }
 
+/* Read commands from `instream' and execute them until end of file or
+   error reading instream. This command loop doesnt care about any
+   such things as displaying time and space usage. If the user asks
+   for those, they won't work. */
+void
+simplified_command_loop (read_input_func, execute_command_func)
+     char *(*read_input_func) (char *);
+     void (*execute_command_func) (char *, int);
+{
+  struct cleanup *old_chain;
+  char *command;
+  int stdin_is_tty = ISATTY (stdin);
+
+  while (instream && !feof (instream))
+    {
+      quit_flag = 0;
+      if (instream == stdin && stdin_is_tty)
+       reinitialize_more_filter ();
+      old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0);
+
+      /* Get a command-line. */
+      command = (*read_input_func) (instream == stdin ?
+                                   get_prompt () : (char *) NULL);
+
+      if (command == 0)
+       return;
+
+      (*execute_command_func) (command, instream == stdin);
+
+      /* Do any commands attached to breakpoint we stopped at.  */
+      bpstat_do_actions (&stop_bpstat);
+
+      do_cleanups (old_chain);
+    }
+}
 \f
 /* Commands call this if they do not want to be repeated by null lines.  */
 
@@ -2099,7 +2327,7 @@ command_line_input (prompt_arg, repeat, annotation_suffix)
 #ifdef STOP_SIGNAL
   if (job_control)
     {
-      if (async_p)
+      if (event_loop_p)
        signal (STOP_SIGNAL, handle_stop_sig);
       else
        signal (STOP_SIGNAL, stop_sig);
@@ -2893,13 +3121,18 @@ document_command (comname, from_tty)
 /* Print the GDB banner. */
 void
 print_gdb_version (stream)
-     GDB_FILE *stream;
+     struct ui_file *stream;
 {
   /* From GNU coding standards, first line is meant to be easy for a
      program to parse, and is just canonical program name and version
      number, which starts after last space. */
 
+#ifdef UI_OUT
+  /* Print it console style until a format is defined */
+  fprintf_filtered (stream, "GNU gdb %s (UI_OUT)\n", version);
+#else
   fprintf_filtered (stream, "GNU gdb %s\n", version);
+#endif
 
   /* Second line is a copyright notice. */
 
@@ -2964,7 +3197,7 @@ get_prompt_1 (formatted_prompt)
 {
   char *local_prompt;
 
-  if (async_p)
+  if (event_loop_p)
     local_prompt = PROMPT (0);
   else
     local_prompt = gdb_prompt_string;
@@ -3200,7 +3433,7 @@ get_prompt ()
   else
     {
       /* Prompt could not be formatted.  */
-      if (async_p)
+      if (event_loop_p)
        return PROMPT (0);
       else
        return gdb_prompt_string;
@@ -3216,7 +3449,7 @@ set_prompt (s)
    if (prompt != NULL)
    free (prompt);
  */
-  if (async_p)
+  if (event_loop_p)
     PROMPT (0) = savestring (s, strlen (s));
   else
     gdb_prompt_string = savestring (s, strlen (s));
@@ -3788,7 +4021,7 @@ init_main ()
 
   /* If we are running the asynchronous version,
      we initialize the prompts differently. */
-  if (!async_p)
+  if (!event_loop_p)
     {
       gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
     }
@@ -3861,7 +4094,7 @@ until the next time it is started.", &cmdlist);
      async version is run. NOTE: this difference is going to
      disappear as we make the event loop be the default engine of
      gdb. */
-  if (!async_p)
+  if (!event_loop_p)
     {
       add_show_from_set
        (add_set_cmd ("prompt", class_support, var_string,
@@ -3937,7 +4170,7 @@ hitting return.");
   /* The set editing command is different depending whether or not the
      async version is run. NOTE: this difference is going to disappear
      as we make the event loop be the default engine of gdb. */
-  if (!async_p)
+  if (!event_loop_p)
     {
       add_show_from_set
        (add_set_cmd ("editing", class_support, var_boolean, (char *) &command_editing_p,
@@ -4061,7 +4294,7 @@ from the target.", &setlist),
      the async version is run. NOTE: this difference is going to
      disappear as we make the event loop be the default engine of
      gdb. */
-  if (!async_p)
+  if (!event_loop_p)
     {
       c = add_set_cmd ("annotate", class_obscure, var_zinteger,
                       (char *) &annotation_level, "Set annotation_level.\n\
@@ -4080,7 +4313,7 @@ from the target.", &setlist),
       add_show_from_set (c, &showlist);
       c->function.sfunc = set_async_annotation_level;
     }
-  if (async_p)
+  if (event_loop_p)
     {
       add_show_from_set
        (add_set_cmd ("exec-done-display", class_support, var_boolean, (char *) &exec_done_display_p,
This page took 0.030411 seconds and 4 git commands to generate.