Add a symbol's value to the computed frag offset, rather than overwriting it.
[deliverable/binutils-gdb.git] / gdb / top.c
index 525e0a3fe3701987a8f1fbf5a9660d277a35afa9..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.
 
@@ -34,6 +33,7 @@
 #include "terminal.h"          /* For job_control.  */
 #include "annotate.h"
 #include "top.h"
+#include "version.h"
 
 /* readline include files */
 #include <readline/readline.h>
 #undef savestring
 
 #include <sys/types.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 
-#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 */
 
@@ -152,10 +155,10 @@ static void disconnect PARAMS ((int));
 
 static void source_cleanup PARAMS ((FILE *));
 
-/* If this definition isn't overridden by the header files, assume
-   that isatty and fileno exist on this system.  */
-#ifndef ISATTY
-#define ISATTY(FP)     (isatty (fileno (FP)))
+/* Default command line prompt.  This is overriden in some configs. */
+
+#ifndef DEFAULT_PROMPT
+#define DEFAULT_PROMPT "(gdb) "
 #endif
 
 /* Initialization file name for gdb.  This is overridden in some configs.  */
@@ -172,18 +175,6 @@ int inhibit_gdbinit = 0;
 
 int use_windows = 1;
 
-/* Version number of GDB, as a string.  */
-
-extern char *version;
-
-/* Canonical host name as a string. */
-
-extern char *host_name;
-
-/* Canonical target name as a string. */
-
-extern char *target_name;
-
 extern char lang_frame_mismatch_warn[];                /* language.c */
 
 /* Flag for whether we want all the "from_tty" gubbish printed.  */
@@ -301,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
@@ -317,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.  */
 
@@ -388,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,
@@ -404,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
@@ -430,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... */
 
@@ -459,6 +466,11 @@ int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus * status));
 void (*call_command_hook) PARAMS ((struct cmd_list_element * c, char *cmd,
                                   int from_tty));
 
+/* Called after a `set' command has finished.  Is only run if the
+   `set' command succeeded.  */
+
+void (*set_hook) PARAMS ((struct cmd_list_element *c));
+
 /* Called when the current thread changes.  Argument is thread id.  */
 
 void (*context_hook) PARAMS ((int id));
@@ -470,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;
@@ -491,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)
@@ -528,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;
@@ -565,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;
@@ -584,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
@@ -680,17 +768,24 @@ gdb_init (argv0)
   getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
   current_directory = gdb_dirbuf;
 
+#ifdef __MSDOS__
+  /* Make sure we return to the original directory upon exit, come
+     what may, since the OS doesn't do that for us.  */
+  make_final_cleanup ((make_cleanup_func) chdir, strsave (current_directory));
+#endif
+
   init_cmd_lists ();           /* This needs to be done first */
   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 ();
@@ -701,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);
 }
@@ -762,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;
 
@@ -842,6 +1031,7 @@ print_command_line (cmd, depth, stream)
       fputs_filtered ("end\n", stream);
     }
 }
+#endif
 
 /* Execute the command in CMD.  */
 
@@ -1039,7 +1229,7 @@ arg_cleanup ()
 {
   struct user_args *oargs = user_args;
   if (!user_args)
-    fatal ("Internal error, arg_cleanup called with no user args.\n");
+    internal_error ("Internal error, arg_cleanup called with no user args.\n");
 
   user_args = user_args->next;
   free (oargs);
@@ -1277,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")
@@ -1441,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.  */
 
@@ -2102,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);
@@ -2896,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. */
 
@@ -2967,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;
@@ -3170,7 +3400,7 @@ get_prompt_1 (formatted_prompt)
                    else
                      {
                        if (available >= 16 /*? */ )    /* overflow protect */
-                         sprintf (outp, "%d", (long) longval);
+                         sprintf (outp, "%ld", (long) longval);
                      }
                    break;
                  }
@@ -3203,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;
@@ -3219,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));
@@ -3362,13 +3592,32 @@ cd_command (dir, from_tty)
   if (chdir (dir) < 0)
     perror_with_name (dir);
 
+#if defined(_WIN32) || defined(__MSDOS__)
+  /* There's too much mess with DOSish names like "d:", "d:.",
+     "d:./foo" etc.  Instead of having lots of special #ifdef'ed code,
+     simply get the canonicalized name of the current directory.  */
+  dir = getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
+#endif
+
   len = strlen (dir);
-  dir = savestring (dir, len - (len > 1 && SLASH_P (dir[len - 1])));
+  if (SLASH_P (dir[len-1]))
+    {
+      /* Remove the trailing slash unless this is a root directory
+        (including a drive letter on non-Unix systems).  */
+      if (!(len == 1) /* "/" */
+#if defined(_WIN32) || defined(__MSDOS__)
+         && !(!SLASH_P (*dir) && ROOTED_P (dir) && len <= 3) /* "d:/" */
+#endif
+         )
+       len--;
+    }
+
+  dir = savestring (dir, len);
   if (ROOTED_P (dir))
     current_directory = dir;
   else
     {
-      if (SLASH_P (current_directory[0]) && current_directory[1] == '\0')
+      if (SLASH_P (current_directory[strlen (current_directory) - 1]))
        current_directory = concat (current_directory, dir, NULL);
       else
        current_directory = concat (current_directory, SLASH_STRING, dir, NULL);
@@ -3755,7 +4004,12 @@ init_history ()
       /* We include the current directory so that if the user changes
          directories the file written will be the same as the one
          that was read.  */
+#ifdef __MSDOS__
+    /* No leading dots in file names are allowed on MSDOS.  */
+    history_filename = concat (current_directory, "/_gdb_history", NULL);
+#else
       history_filename = concat (current_directory, "/.gdb_history", NULL);
+#endif
     }
   read_history (history_filename);
 }
@@ -3767,25 +4021,17 @@ init_main ()
 
   /* If we are running the asynchronous version,
      we initialize the prompts differently. */
-  if (!async_p)
+  if (!event_loop_p)
     {
-#ifdef DEFAULT_PROMPT
       gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
-#else
-      gdb_prompt_string = savestring ("(gdb) ", 6);
-#endif
     }
   else
     {
       /* initialize the prompt stack to a simple "(gdb) " prompt or to
-         whatever the DEFULAT_PROMPT is. */
+         whatever the DEFAULT_PROMPT is. */
       the_prompts.top = 0;
       PREFIX (0) = "";
-#ifdef DEFAULT_PROMPT
       PROMPT (0) = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
-#else
-      PROMPT (0) = savestring ("(gdb) ", 6);
-#endif
       SUFFIX (0) = "";
       /* Set things up for annotation_level > 1, if the user ever decides
          to use it. */
@@ -3848,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,
@@ -3924,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,
@@ -4048,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\
@@ -4067,4 +4313,12 @@ from the target.", &setlist),
       add_show_from_set (c, &showlist);
       c->function.sfunc = set_async_annotation_level;
     }
+  if (event_loop_p)
+    {
+      add_show_from_set
+       (add_set_cmd ("exec-done-display", class_support, var_boolean, (char *) &exec_done_display_p,
+                     "Set notification of completion for asynchronous execution commands.\n\
+Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist),
+        &showlist);
+    }
 }
This page took 0.033278 seconds and 4 git commands to generate.