gdb/event-top.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Top level stuff for GDB, the GNU debugger.

  2.    Copyright (C) 1999-2015 Free Software Foundation, Inc.

  3.    Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "top.h"
  17. #include "inferior.h"
  18. #include "infrun.h"
  19. #include "target.h"
  20. #include "terminal.h"                /* for job_control */
  21. #include "event-loop.h"
  22. #include "event-top.h"
  23. #include "interps.h"
  24. #include <signal.h>
  25. #include "cli/cli-script.h"     /* for reset_command_nest_depth */
  26. #include "main.h"
  27. #include "gdbthread.h"
  28. #include "observer.h"
  29. #include "continuations.h"
  30. #include "gdbcmd.h"                /* for dont_repeat() */
  31. #include "annotate.h"
  32. #include "maint.h"

  33. /* readline include files.  */
  34. #include "readline/readline.h"
  35. #include "readline/history.h"

  36. /* readline defines this.  */
  37. #undef savestring

  38. static void rl_callback_read_char_wrapper (gdb_client_data client_data);
  39. static void command_line_handler (char *rl);
  40. static void change_line_handler (void);
  41. static void command_handler (char *command);
  42. static char *top_level_prompt (void);

  43. /* Signal handlers.  */
  44. #ifdef SIGQUIT
  45. static void handle_sigquit (int sig);
  46. #endif
  47. #ifdef SIGHUP
  48. static void handle_sighup (int sig);
  49. #endif
  50. static void handle_sigfpe (int sig);

  51. /* Functions to be invoked by the event loop in response to
  52.    signals.  */
  53. #if defined (SIGQUIT) || defined (SIGHUP)
  54. static void async_do_nothing (gdb_client_data);
  55. #endif
  56. #ifdef SIGHUP
  57. static void async_disconnect (gdb_client_data);
  58. #endif
  59. static void async_float_handler (gdb_client_data);
  60. #ifdef STOP_SIGNAL
  61. static void async_stop_sig (gdb_client_data);
  62. #endif
  63. static void async_sigterm_handler (gdb_client_data arg);

  64. /* Readline offers an alternate interface, via callback
  65.    functions.  These are all included in the file callback.c in the
  66.    readline distribution.  This file provides (mainly) a function, which
  67.    the event loop uses as callback (i.e. event handler) whenever an event
  68.    is detected on the standard input file descriptor.
  69.    readline_callback_read_char is called (by the GDB event loop) whenever
  70.    there is a new character ready on the input stream.  This function
  71.    incrementally builds a buffer internal to readline where it
  72.    accumulates the line read up to the point of invocation.  In the
  73.    special case in which the character read is newline, the function
  74.    invokes a GDB supplied callback routine, which does the processing of
  75.    a full command line.  This latter routine is the asynchronous analog
  76.    of the old command_line_input in gdb.  Instead of invoking (and waiting
  77.    for) readline to read the command line and pass it back to
  78.    command_loop for processing, the new command_line_handler function has
  79.    the command line already available as its parameter.  INPUT_HANDLER is
  80.    to be set to the function that readline will invoke when a complete
  81.    line of input is ready.  CALL_READLINE is to be set to the function
  82.    that readline offers as callback to the event_loop.  */

  83. void (*input_handler) (char *);
  84. void (*call_readline) (gdb_client_data);

  85. /* Important variables for the event loop.  */

  86. /* This is used to determine if GDB is using the readline library or
  87.    its own simplified form of readline.  It is used by the asynchronous
  88.    form of the set editing command.
  89.    ezannoni: as of 1999-04-29 I expect that this
  90.    variable will not be used after gdb is changed to use the event
  91.    loop as default engine, and event-top.c is merged into top.c.  */
  92. int async_command_editing_p;

  93. /* This is the annotation suffix that will be used when the
  94.    annotation_level is 2.  */
  95. char *async_annotation_suffix;

  96. /* This is used to display the notification of the completion of an
  97.    asynchronous execution command.  */
  98. int exec_done_display_p = 0;

  99. /* This is the file descriptor for the input stream that GDB uses to
  100.    read commands from.  */
  101. int input_fd;

  102. /* Used by the stdin event handler to compensate for missed stdin events.
  103.    Setting this to a non-zero value inside an stdin callback makes the callback
  104.    run again.  */
  105. int call_stdin_event_handler_again_p;

  106. /* Signal handling variables.  */
  107. /* Each of these is a pointer to a function that the event loop will
  108.    invoke if the corresponding signal has received.  The real signal
  109.    handlers mark these functions as ready to be executed and the event
  110.    loop, in a later iteration, calls them.  See the function
  111.    invoke_async_signal_handler.  */
  112. static struct async_signal_handler *sigint_token;
  113. #ifdef SIGHUP
  114. static struct async_signal_handler *sighup_token;
  115. #endif
  116. #ifdef SIGQUIT
  117. static struct async_signal_handler *sigquit_token;
  118. #endif
  119. static struct async_signal_handler *sigfpe_token;
  120. #ifdef STOP_SIGNAL
  121. static struct async_signal_handler *sigtstp_token;
  122. #endif
  123. static struct async_signal_handler *async_sigterm_token;

  124. /* Structure to save a partially entered command.  This is used when
  125.    the user types '\' at the end of a command line.  This is necessary
  126.    because each line of input is handled by a different call to
  127.    command_line_handler, and normally there is no state retained
  128.    between different calls.  */
  129. static int more_to_come = 0;

  130. struct readline_input_state
  131.   {
  132.     char *linebuffer;
  133.     char *linebuffer_ptr;
  134.   }
  135. readline_input_state;

  136. /* This hook is called by rl_callback_read_char_wrapper after each
  137.    character is processed.  */
  138. void (*after_char_processing_hook) (void);


  139. /* Wrapper function for calling into the readline library.  The event
  140.    loop expects the callback function to have a paramter, while
  141.    readline expects none.  */
  142. static void
  143. rl_callback_read_char_wrapper (gdb_client_data client_data)
  144. {
  145.   rl_callback_read_char ();
  146.   if (after_char_processing_hook)
  147.     (*after_char_processing_hook) ();
  148. }

  149. /* Initialize all the necessary variables, start the event loop,
  150.    register readline, and stdin, start the loop.  The DATA is the
  151.    interpreter data cookie, ignored for now.  */

  152. void
  153. cli_command_loop (void *data)
  154. {
  155.   display_gdb_prompt (0);

  156.   /* Now it's time to start the event loop.  */
  157.   start_event_loop ();
  158. }

  159. /* Change the function to be invoked every time there is a character
  160.    ready on stdin.  This is used when the user sets the editing off,
  161.    therefore bypassing readline, and letting gdb handle the input
  162.    itself, via gdb_readline2.  Also it is used in the opposite case in
  163.    which the user sets editing on again, by restoring readline
  164.    handling of the input.  */
  165. static void
  166. change_line_handler (void)
  167. {
  168.   /* NOTE: this operates on input_fd, not instream.  If we are reading
  169.      commands from a file, instream will point to the file.  However in
  170.      async mode, we always read commands from a file with editing
  171.      off.  This means that the 'set editing on/off' will have effect
  172.      only on the interactive session.  */

  173.   if (async_command_editing_p)
  174.     {
  175.       /* Turn on editing by using readline.  */
  176.       call_readline = rl_callback_read_char_wrapper;
  177.       input_handler = command_line_handler;
  178.     }
  179.   else
  180.     {
  181.       /* Turn off editing by using gdb_readline2.  */
  182.       gdb_rl_callback_handler_remove ();
  183.       call_readline = gdb_readline2;

  184.       /* Set up the command handler as well, in case we are called as
  185.          first thing from .gdbinit.  */
  186.       input_handler = command_line_handler;
  187.     }
  188. }

  189. /* The functions below are wrappers for rl_callback_handler_remove and
  190.    rl_callback_handler_install that keep track of whether the callback
  191.    handler is installed in readline.  This is necessary because after
  192.    handling a target event of a background execution command, we may
  193.    need to reinstall the callback handler if it was removed due to a
  194.    secondary prompt.  See gdb_readline_wrapper_line.  We don't
  195.    unconditionally install the handler for every target event because
  196.    that also clears the line buffer, thus installing it while the user
  197.    is typing would lose input.  */

  198. /* Whether we've registered a callback handler with readline.  */
  199. static int callback_handler_installed;

  200. /* See event-top.h, and above.  */

  201. void
  202. gdb_rl_callback_handler_remove (void)
  203. {
  204.   rl_callback_handler_remove ();
  205.   callback_handler_installed = 0;
  206. }

  207. /* See event-top.h, and above.  Note this wrapper doesn't have an
  208.    actual callback parameter because we always install
  209.    INPUT_HANDLER.  */

  210. void
  211. gdb_rl_callback_handler_install (const char *prompt)
  212. {
  213.   /* Calling rl_callback_handler_install resets readline's input
  214.      buffer.  Calling this when we were already processing input
  215.      therefore loses input.  */
  216.   gdb_assert (!callback_handler_installed);

  217.   rl_callback_handler_install (prompt, input_handler);
  218.   callback_handler_installed = 1;
  219. }

  220. /* See event-top.h, and above.  */

  221. void
  222. gdb_rl_callback_handler_reinstall (void)
  223. {
  224.   if (!callback_handler_installed)
  225.     {
  226.       /* Passing NULL as prompt argument tells readline to not display
  227.          a prompt.  */
  228.       gdb_rl_callback_handler_install (NULL);
  229.     }
  230. }

  231. /* Displays the prompt.  If the argument NEW_PROMPT is NULL, the
  232.    prompt that is displayed is the current top level prompt.
  233.    Otherwise, it displays whatever NEW_PROMPT is as a local/secondary
  234.    prompt.

  235.    This is used after each gdb command has completed, and in the
  236.    following cases:

  237.    1. When the user enters a command line which is ended by '\'
  238.    indicating that the command will continue on the next line.  In
  239.    that case the prompt that is displayed is the empty string.

  240.    2. When the user is entering 'commands' for a breakpoint, or
  241.    actions for a tracepoint.  In this case the prompt will be '>'

  242.    3. On prompting for pagination.  */

  243. void
  244. display_gdb_prompt (const char *new_prompt)
  245. {
  246.   char *actual_gdb_prompt = NULL;
  247.   struct cleanup *old_chain;

  248.   annotate_display_prompt ();

  249.   /* Reset the nesting depth used when trace-commands is set.  */
  250.   reset_command_nest_depth ();

  251.   old_chain = make_cleanup (free_current_contents, &actual_gdb_prompt);

  252.   /* Do not call the python hook on an explicit prompt change as
  253.      passed to this function, as this forms a secondary/local prompt,
  254.      IE, displayed but not set.  */
  255.   if (! new_prompt)
  256.     {
  257.       if (sync_execution)
  258.         {
  259.           /* This is to trick readline into not trying to display the
  260.              prompt.  Even though we display the prompt using this
  261.              function, readline still tries to do its own display if
  262.              we don't call rl_callback_handler_install and
  263.              rl_callback_handler_remove (which readline detects
  264.              because a global variable is not set).  If readline did
  265.              that, it could mess up gdb signal handlers for SIGINT.
  266.              Readline assumes that between calls to rl_set_signals and
  267.              rl_clear_signals gdb doesn't do anything with the signal
  268.              handlers.  Well, that's not the case, because when the
  269.              target executes we change the SIGINT signal handler.  If
  270.              we allowed readline to display the prompt, the signal
  271.              handler change would happen exactly between the calls to
  272.              the above two functions.  Calling
  273.              rl_callback_handler_remove(), does the job.  */

  274.           gdb_rl_callback_handler_remove ();
  275.           do_cleanups (old_chain);
  276.           return;
  277.         }
  278.       else
  279.         {
  280.           /* Display the top level prompt.  */
  281.           actual_gdb_prompt = top_level_prompt ();
  282.         }
  283.     }
  284.   else
  285.     actual_gdb_prompt = xstrdup (new_prompt);

  286.   if (async_command_editing_p)
  287.     {
  288.       gdb_rl_callback_handler_remove ();
  289.       gdb_rl_callback_handler_install (actual_gdb_prompt);
  290.     }
  291.   /* new_prompt at this point can be the top of the stack or the one
  292.      passed in.  It can't be NULL.  */
  293.   else
  294.     {
  295.       /* Don't use a _filtered function here.  It causes the assumed
  296.          character position to be off, since the newline we read from
  297.          the user is not accounted for.  */
  298.       fputs_unfiltered (actual_gdb_prompt, gdb_stdout);
  299.       gdb_flush (gdb_stdout);
  300.     }

  301.   do_cleanups (old_chain);
  302. }

  303. /* Return the top level prompt, as specified by "set prompt", possibly
  304.    overriden by the python gdb.prompt_hook hook, and then composed
  305.    with the prompt prefix and suffix (annotations).  The caller is
  306.    responsible for freeing the returned string.  */

  307. static char *
  308. top_level_prompt (void)
  309. {
  310.   char *prefix;
  311.   char *prompt = NULL;
  312.   char *suffix;
  313.   char *composed_prompt;
  314.   size_t prompt_length;

  315.   /* Give observers a chance of changing the prompt.  E.g., the python
  316.      `gdb.prompt_hook' is installed as an observer.  */
  317.   observer_notify_before_prompt (get_prompt ());

  318.   prompt = xstrdup (get_prompt ());

  319.   if (annotation_level >= 2)
  320.     {
  321.       /* Prefix needs to have new line at end.  */
  322.       prefix = (char *) alloca (strlen (async_annotation_suffix) + 10);
  323.       strcpy (prefix, "\n\032\032pre-");
  324.       strcat (prefix, async_annotation_suffix);
  325.       strcat (prefix, "\n");

  326.       /* Suffix needs to have a new line at end and \032 \032 at
  327.          beginning.  */
  328.       suffix = (char *) alloca (strlen (async_annotation_suffix) + 6);
  329.       strcpy (suffix, "\n\032\032");
  330.       strcat (suffix, async_annotation_suffix);
  331.       strcat (suffix, "\n");
  332.     }
  333.   else
  334.     {
  335.       prefix = "";
  336.       suffix = "";
  337.     }

  338.   prompt_length = strlen (prefix) + strlen (prompt) + strlen (suffix);
  339.   composed_prompt = xmalloc (prompt_length + 1);

  340.   strcpy (composed_prompt, prefix);
  341.   strcat (composed_prompt, prompt);
  342.   strcat (composed_prompt, suffix);

  343.   xfree (prompt);

  344.   return composed_prompt;
  345. }

  346. /* When there is an event ready on the stdin file desriptor, instead
  347.    of calling readline directly throught the callback function, or
  348.    instead of calling gdb_readline2, give gdb a chance to detect
  349.    errors and do something.  */
  350. void
  351. stdin_event_handler (int error, gdb_client_data client_data)
  352. {
  353.   if (error)
  354.     {
  355.       printf_unfiltered (_("error detected on stdin\n"));
  356.       delete_file_handler (input_fd);
  357.       discard_all_continuations ();
  358.       discard_all_intermediate_continuations ();
  359.       /* If stdin died, we may as well kill gdb.  */
  360.       quit_command ((char *) 0, stdin == instream);
  361.     }
  362.   else
  363.     {
  364.       do
  365.         {
  366.           call_stdin_event_handler_again_p = 0;
  367.           (*call_readline) (client_data);
  368.         } while (call_stdin_event_handler_again_p != 0);
  369.     }
  370. }

  371. /* Re-enable stdin after the end of an execution command in
  372.    synchronous mode, or after an error from the target, and we aborted
  373.    the exec operation.  */

  374. void
  375. async_enable_stdin (void)
  376. {
  377.   if (sync_execution)
  378.     {
  379.       /* See NOTE in async_disable_stdin().  */
  380.       /* FIXME: cagney/1999-09-27: Call this before clearing
  381.          sync_execution.  Current target_terminal_ours() implementations
  382.          check for sync_execution before switching the terminal.  */
  383.       target_terminal_ours ();
  384.       sync_execution = 0;
  385.     }
  386. }

  387. /* Disable reads from stdin (the console) marking the command as
  388.    synchronous.  */

  389. void
  390. async_disable_stdin (void)
  391. {
  392.   sync_execution = 1;
  393. }


  394. /* Handles a gdb command.  This function is called by
  395.    command_line_handler, which has processed one or more input lines
  396.    into COMMAND.  */
  397. /* NOTE: 1999-04-30 This is the asynchronous version of the command_loop
  398.    function.  The command_loop function will be obsolete when we
  399.    switch to use the event loop at every execution of gdb.  */
  400. static void
  401. command_handler (char *command)
  402. {
  403.   int stdin_is_tty = ISATTY (stdin);
  404.   struct cleanup *stat_chain;

  405.   clear_quit_flag ();
  406.   if (instream == stdin && stdin_is_tty)
  407.     reinitialize_more_filter ();

  408.   /* If readline returned a NULL command, it means that the connection
  409.      with the terminal is gone.  This happens at the end of a
  410.      testsuite run, after Expect has hung up but GDB is still alive.
  411.      In such a case, we just quit gdb killing the inferior program
  412.      too.  */
  413.   if (command == 0)
  414.     {
  415.       printf_unfiltered ("quit\n");
  416.       execute_command ("quit", stdin == instream);
  417.     }

  418.   stat_chain = make_command_stats_cleanup (1);

  419.   execute_command (command, instream == stdin);

  420.   /* Do any commands attached to breakpoint we stopped at.  */
  421.   bpstat_do_actions ();

  422.   do_cleanups (stat_chain);
  423. }

  424. /* Handle a complete line of input.  This is called by the callback
  425.    mechanism within the readline library.  Deal with incomplete
  426.    commands as well, by saving the partial input in a global
  427.    buffer.  */

  428. /* NOTE: 1999-04-30 This is the asynchronous version of the
  429.    command_line_input function; command_line_input will become
  430.    obsolete once we use the event loop as the default mechanism in
  431.    GDB.  */
  432. static void
  433. command_line_handler (char *rl)
  434. {
  435.   static char *linebuffer = 0;
  436.   static unsigned linelength = 0;
  437.   char *p;
  438.   char *p1;
  439.   char *nline;
  440.   int repeat = (instream == stdin);

  441.   if (annotation_level > 1 && instream == stdin)
  442.     {
  443.       printf_unfiltered (("\n\032\032post-"));
  444.       puts_unfiltered (async_annotation_suffix);
  445.       printf_unfiltered (("\n"));
  446.     }

  447.   if (linebuffer == 0)
  448.     {
  449.       linelength = 80;
  450.       linebuffer = (char *) xmalloc (linelength);
  451.       linebuffer[0] = '\0';
  452.     }

  453.   p = linebuffer;

  454.   if (more_to_come)
  455.     {
  456.       strcpy (linebuffer, readline_input_state.linebuffer);
  457.       p = readline_input_state.linebuffer_ptr;
  458.       xfree (readline_input_state.linebuffer);
  459.       more_to_come = 0;
  460.     }

  461. #ifdef STOP_SIGNAL
  462.   if (job_control)
  463.     signal (STOP_SIGNAL, handle_stop_sig);
  464. #endif

  465.   /* Make sure that all output has been output.  Some machines may let
  466.      you get away with leaving out some of the gdb_flush, but not
  467.      all.  */
  468.   wrap_here ("");
  469.   gdb_flush (gdb_stdout);
  470.   gdb_flush (gdb_stderr);

  471.   if (source_file_name != NULL)
  472.     ++source_line_number;

  473.   /* If we are in this case, then command_handler will call quit
  474.      and exit from gdb.  */
  475.   if (!rl || rl == (char *) EOF)
  476.     {
  477.       command_handler (0);
  478.       return;                        /* Lint.  */
  479.     }
  480.   if (strlen (rl) + 1 + (p - linebuffer) > linelength)
  481.     {
  482.       linelength = strlen (rl) + 1 + (p - linebuffer);
  483.       nline = (char *) xrealloc (linebuffer, linelength);
  484.       p += nline - linebuffer;
  485.       linebuffer = nline;
  486.     }
  487.   p1 = rl;
  488.   /* Copy line.  Don't copy null at end.  (Leaves line alone
  489.      if this was just a newline).  */
  490.   while (*p1)
  491.     *p++ = *p1++;

  492.   xfree (rl);                        /* Allocated in readline.  */

  493.   if (p > linebuffer && *(p - 1) == '\\')
  494.     {
  495.       *p = '\0';
  496.       p--;                        /* Put on top of '\'.  */

  497.       readline_input_state.linebuffer = xstrdup (linebuffer);
  498.       readline_input_state.linebuffer_ptr = p;

  499.       /* We will not invoke a execute_command if there is more
  500.          input expected to complete the command.  So, we need to
  501.          print an empty prompt here.  */
  502.       more_to_come = 1;
  503.       display_gdb_prompt ("");
  504.       return;
  505.     }

  506. #ifdef STOP_SIGNAL
  507.   if (job_control)
  508.     signal (STOP_SIGNAL, SIG_DFL);
  509. #endif

  510. #define SERVER_COMMAND_LENGTH 7
  511.   server_command =
  512.     (p - linebuffer > SERVER_COMMAND_LENGTH)
  513.     && strncmp (linebuffer, "server ", SERVER_COMMAND_LENGTH) == 0;
  514.   if (server_command)
  515.     {
  516.       /* Note that we don't set `line'.  Between this and the check in
  517.          dont_repeat, this insures that repeating will still do the
  518.          right thing.  */
  519.       *p = '\0';
  520.       command_handler (linebuffer + SERVER_COMMAND_LENGTH);
  521.       display_gdb_prompt (0);
  522.       return;
  523.     }

  524.   /* Do history expansion if that is wished.  */
  525.   if (history_expansion_p && instream == stdin
  526.       && ISATTY (instream))
  527.     {
  528.       char *history_value;
  529.       int expanded;

  530.       *p = '\0';                /* Insert null now.  */
  531.       expanded = history_expand (linebuffer, &history_value);
  532.       if (expanded)
  533.         {
  534.           /* Print the changes.  */
  535.           printf_unfiltered ("%s\n", history_value);

  536.           /* If there was an error, call this function again.  */
  537.           if (expanded < 0)
  538.             {
  539.               xfree (history_value);
  540.               return;
  541.             }
  542.           if (strlen (history_value) > linelength)
  543.             {
  544.               linelength = strlen (history_value) + 1;
  545.               linebuffer = (char *) xrealloc (linebuffer, linelength);
  546.             }
  547.           strcpy (linebuffer, history_value);
  548.           p = linebuffer + strlen (linebuffer);
  549.         }
  550.       xfree (history_value);
  551.     }

  552.   /* If we just got an empty line, and that is supposed to repeat the
  553.      previous command, return the value in the global buffer.  */
  554.   if (repeat && p == linebuffer && *p != '\\')
  555.     {
  556.       command_handler (saved_command_line);
  557.       display_gdb_prompt (0);
  558.       return;
  559.     }

  560.   for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
  561.   if (repeat && !*p1)
  562.     {
  563.       command_handler (saved_command_line);
  564.       display_gdb_prompt (0);
  565.       return;
  566.     }

  567.   *p = 0;

  568.   /* Add line to history if appropriate.  */
  569.   if (*linebuffer && input_from_terminal_p ())
  570.     add_history (linebuffer);

  571.   /* Note: lines consisting solely of comments are added to the command
  572.      history.  This is useful when you type a command, and then
  573.      realize you don't want to execute it quite yet.  You can comment
  574.      out the command and then later fetch it from the value history
  575.      and remove the '#'.  The kill ring is probably better, but some
  576.      people are in the habit of commenting things out.  */
  577.   if (*p1 == '#')
  578.     *p1 = '\0';                        /* Found a comment.  */

  579.   /* Save into global buffer if appropriate.  */
  580.   if (repeat)
  581.     {
  582.       if (linelength > saved_command_line_size)
  583.         {
  584.           saved_command_line = xrealloc (saved_command_line, linelength);
  585.           saved_command_line_size = linelength;
  586.         }
  587.       strcpy (saved_command_line, linebuffer);
  588.       if (!more_to_come)
  589.         {
  590.           command_handler (saved_command_line);
  591.           display_gdb_prompt (0);
  592.         }
  593.       return;
  594.     }

  595.   command_handler (linebuffer);
  596.   display_gdb_prompt (0);
  597.   return;
  598. }

  599. /* Does reading of input from terminal w/o the editing features
  600.    provided by the readline library.  */

  601. /* NOTE: 1999-04-30 Asynchronous version of gdb_readline; gdb_readline
  602.    will become obsolete when the event loop is made the default
  603.    execution for gdb.  */
  604. void
  605. gdb_readline2 (gdb_client_data client_data)
  606. {
  607.   int c;
  608.   char *result;
  609.   int input_index = 0;
  610.   int result_size = 80;
  611.   static int done_once = 0;

  612.   /* Unbuffer the input stream, so that, later on, the calls to fgetc
  613.      fetch only one char at the time from the stream.  The fgetc's will
  614.      get up to the first newline, but there may be more chars in the
  615.      stream after '\n'.  If we buffer the input and fgetc drains the
  616.      stream, getting stuff beyond the newline as well, a select, done
  617.      afterwards will not trigger.  */
  618.   if (!done_once && !ISATTY (instream))
  619.     {
  620.       setbuf (instream, NULL);
  621.       done_once = 1;
  622.     }

  623.   result = (char *) xmalloc (result_size);

  624.   /* We still need the while loop here, even though it would seem
  625.      obvious to invoke gdb_readline2 at every character entered.  If
  626.      not using the readline library, the terminal is in cooked mode,
  627.      which sends the characters all at once.  Poll will notice that the
  628.      input fd has changed state only after enter is pressed.  At this
  629.      point we still need to fetch all the chars entered.  */

  630.   while (1)
  631.     {
  632.       /* Read from stdin if we are executing a user defined command.
  633.          This is the right thing for prompt_for_continue, at least.  */
  634.       c = fgetc (instream ? instream : stdin);

  635.       if (c == EOF)
  636.         {
  637.           if (input_index > 0)
  638.             /* The last line does not end with a newline.  Return it,
  639.                and if we are called again fgetc will still return EOF
  640.                and we'll return NULL then.  */
  641.             break;
  642.           xfree (result);
  643.           (*input_handler) (0);
  644.           return;
  645.         }

  646.       if (c == '\n')
  647.         {
  648.           if (input_index > 0 && result[input_index - 1] == '\r')
  649.             input_index--;
  650.           break;
  651.         }

  652.       result[input_index++] = c;
  653.       while (input_index >= result_size)
  654.         {
  655.           result_size *= 2;
  656.           result = (char *) xrealloc (result, result_size);
  657.         }
  658.     }

  659.   result[input_index++] = '\0';
  660.   (*input_handler) (result);
  661. }


  662. /* Initialization of signal handlers and tokens.  There is a function
  663.    handle_sig* for each of the signals GDB cares about.  Specifically:
  664.    SIGINT, SIGFPE, SIGQUIT, SIGTSTP, SIGHUP, SIGWINCH.  These
  665.    functions are the actual signal handlers associated to the signals
  666.    via calls to signal().  The only job for these functions is to
  667.    enqueue the appropriate event/procedure with the event loop.  Such
  668.    procedures are the old signal handlers.  The event loop will take
  669.    care of invoking the queued procedures to perform the usual tasks
  670.    associated with the reception of the signal.  */
  671. /* NOTE: 1999-04-30 This is the asynchronous version of init_signals.
  672.    init_signals will become obsolete as we move to have to event loop
  673.    as the default for gdb.  */
  674. void
  675. async_init_signals (void)
  676. {
  677.   signal (SIGINT, handle_sigint);
  678.   sigint_token =
  679.     create_async_signal_handler (async_request_quit, NULL);
  680.   signal (SIGTERM, handle_sigterm);
  681.   async_sigterm_token
  682.     = create_async_signal_handler (async_sigterm_handler, NULL);

  683.   /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
  684.      to the inferior and breakpoints will be ignored.  */
  685. #ifdef SIGTRAP
  686.   signal (SIGTRAP, SIG_DFL);
  687. #endif

  688. #ifdef SIGQUIT
  689.   /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
  690.      passed to the inferior, which we don't want.  It would be
  691.      possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
  692.      on BSD4.3 systems using vfork, that can affect the
  693.      GDB process as well as the inferior (the signal handling tables
  694.      might be in memory, shared between the two).  Since we establish
  695.      a handler for SIGQUIT, when we call exec it will set the signal
  696.      to SIG_DFL for us.  */
  697.   signal (SIGQUIT, handle_sigquit);
  698.   sigquit_token =
  699.     create_async_signal_handler (async_do_nothing, NULL);
  700. #endif
  701. #ifdef SIGHUP
  702.   if (signal (SIGHUP, handle_sighup) != SIG_IGN)
  703.     sighup_token =
  704.       create_async_signal_handler (async_disconnect, NULL);
  705.   else
  706.     sighup_token =
  707.       create_async_signal_handler (async_do_nothing, NULL);
  708. #endif
  709.   signal (SIGFPE, handle_sigfpe);
  710.   sigfpe_token =
  711.     create_async_signal_handler (async_float_handler, NULL);

  712. #ifdef STOP_SIGNAL
  713.   sigtstp_token =
  714.     create_async_signal_handler (async_stop_sig, NULL);
  715. #endif
  716. }

  717. /* Tell the event loop what to do if SIGINT is received.
  718.    See event-signal.c.  */
  719. void
  720. handle_sigint (int sig)
  721. {
  722.   signal (sig, handle_sigint);

  723.   /* We could be running in a loop reading in symfiles or something so
  724.      it may be quite a while before we get back to the event loop.  So
  725.      set quit_flag to 1 here.  Then if QUIT is called before we get to
  726.      the event loop, we will unwind as expected.  */

  727.   set_quit_flag ();

  728.   /* If immediate_quit is set, we go ahead and process the SIGINT right
  729.      away, even if we usually would defer this to the event loop.  The
  730.      assumption here is that it is safe to process ^C immediately if
  731.      immediate_quit is set.  If we didn't, SIGINT would be really
  732.      processed only the next time through the event loop.  To get to
  733.      that point, though, the command that we want to interrupt needs to
  734.      finish first, which is unacceptable.  If immediate quit is not set,
  735.      we process SIGINT the next time through the loop, which is fine.  */
  736.   gdb_call_async_signal_handler (sigint_token, immediate_quit);
  737. }

  738. /* Handle GDB exit upon receiving SIGTERM if target_can_async_p ().  */

  739. static void
  740. async_sigterm_handler (gdb_client_data arg)
  741. {
  742.   quit_force (NULL, stdin == instream);
  743. }

  744. /* See defs.h.  */
  745. volatile int sync_quit_force_run;

  746. /* Quit GDB if SIGTERM is received.
  747.    GDB would quit anyway, but this way it will clean up properly.  */
  748. void
  749. handle_sigterm (int sig)
  750. {
  751.   signal (sig, handle_sigterm);

  752.   /* Call quit_force in a signal safe way.
  753.      quit_force itself is not signal safe.  */
  754.   if (target_can_async_p ())
  755.     mark_async_signal_handler (async_sigterm_token);
  756.   else
  757.     {
  758.       sync_quit_force_run = 1;
  759.       set_quit_flag ();
  760.     }
  761. }

  762. /* Do the quit.  All the checks have been done by the caller.  */
  763. void
  764. async_request_quit (gdb_client_data arg)
  765. {
  766.   /* If the quit_flag has gotten reset back to 0 by the time we get
  767.      back here, that means that an exception was thrown to unwind the
  768.      current command before we got back to the event loop.  So there
  769.      is no reason to call quit again here.  */

  770.   if (check_quit_flag ())
  771.     quit ();
  772. }

  773. #ifdef SIGQUIT
  774. /* Tell the event loop what to do if SIGQUIT is received.
  775.    See event-signal.c.  */
  776. static void
  777. handle_sigquit (int sig)
  778. {
  779.   mark_async_signal_handler (sigquit_token);
  780.   signal (sig, handle_sigquit);
  781. }
  782. #endif

  783. #if defined (SIGQUIT) || defined (SIGHUP)
  784. /* Called by the event loop in response to a SIGQUIT or an
  785.    ignored SIGHUP.  */
  786. static void
  787. async_do_nothing (gdb_client_data arg)
  788. {
  789.   /* Empty function body.  */
  790. }
  791. #endif

  792. #ifdef SIGHUP
  793. /* Tell the event loop what to do if SIGHUP is received.
  794.    See event-signal.c.  */
  795. static void
  796. handle_sighup (int sig)
  797. {
  798.   mark_async_signal_handler (sighup_token);
  799.   signal (sig, handle_sighup);
  800. }

  801. /* Called by the event loop to process a SIGHUP.  */
  802. static void
  803. async_disconnect (gdb_client_data arg)
  804. {
  805.   volatile struct gdb_exception exception;

  806.   TRY_CATCH (exception, RETURN_MASK_ALL)
  807.     {
  808.       quit_cover ();
  809.     }

  810.   if (exception.reason < 0)
  811.     {
  812.       fputs_filtered ("Could not kill the program being debugged",
  813.                       gdb_stderr);
  814.       exception_print (gdb_stderr, exception);
  815.     }

  816.   TRY_CATCH (exception, RETURN_MASK_ALL)
  817.     {
  818.       pop_all_targets ();
  819.     }

  820.   signal (SIGHUP, SIG_DFL);        /*FIXME: ???????????  */
  821.   raise (SIGHUP);
  822. }
  823. #endif

  824. #ifdef STOP_SIGNAL
  825. void
  826. handle_stop_sig (int sig)
  827. {
  828.   mark_async_signal_handler (sigtstp_token);
  829.   signal (sig, handle_stop_sig);
  830. }

  831. static void
  832. async_stop_sig (gdb_client_data arg)
  833. {
  834.   char *prompt = get_prompt ();

  835. #if STOP_SIGNAL == SIGTSTP
  836.   signal (SIGTSTP, SIG_DFL);
  837. #if HAVE_SIGPROCMASK
  838.   {
  839.     sigset_t zero;

  840.     sigemptyset (&zero);
  841.     sigprocmask (SIG_SETMASK, &zero, 0);
  842.   }
  843. #elif HAVE_SIGSETMASK
  844.   sigsetmask (0);
  845. #endif
  846.   raise (SIGTSTP);
  847.   signal (SIGTSTP, handle_stop_sig);
  848. #else
  849.   signal (STOP_SIGNAL, handle_stop_sig);
  850. #endif
  851.   printf_unfiltered ("%s", prompt);
  852.   gdb_flush (gdb_stdout);

  853.   /* Forget about any previous command -- null line now will do
  854.      nothing.  */
  855.   dont_repeat ();
  856. }
  857. #endif /* STOP_SIGNAL */

  858. /* Tell the event loop what to do if SIGFPE is received.
  859.    See event-signal.c.  */
  860. static void
  861. handle_sigfpe (int sig)
  862. {
  863.   mark_async_signal_handler (sigfpe_token);
  864.   signal (sig, handle_sigfpe);
  865. }

  866. /* Event loop will call this functin to process a SIGFPE.  */
  867. static void
  868. async_float_handler (gdb_client_data arg)
  869. {
  870.   /* This message is based on ANSI C, section 4.7.  Note that integer
  871.      divide by zero causes this, so "float" is a misnomer.  */
  872.   error (_("Erroneous arithmetic operation."));
  873. }


  874. /* Called by do_setshow_command.  */
  875. void
  876. set_async_editing_command (char *args, int from_tty,
  877.                            struct cmd_list_element *c)
  878. {
  879.   change_line_handler ();
  880. }

  881. /* Set things up for readline to be invoked via the alternate
  882.    interface, i.e. via a callback function (rl_callback_read_char),
  883.    and hook up instream to the event loop.  */
  884. void
  885. gdb_setup_readline (void)
  886. {
  887.   /* This function is a noop for the sync case.  The assumption is
  888.      that the sync setup is ALL done in gdb_init, and we would only
  889.      mess it up here.  The sync stuff should really go away over
  890.      time.  */
  891.   if (!batch_silent)
  892.     gdb_stdout = stdio_fileopen (stdout);
  893.   gdb_stderr = stderr_fileopen ();
  894.   gdb_stdlog = gdb_stderr/* for moment */
  895.   gdb_stdtarg = gdb_stderr; /* for moment */
  896.   gdb_stdtargerr = gdb_stderr; /* for moment */

  897.   /* If the input stream is connected to a terminal, turn on
  898.      editing.  */
  899.   if (ISATTY (instream))
  900.     {
  901.       /* Tell gdb that we will be using the readline library.  This
  902.          could be overwritten by a command in .gdbinit like 'set
  903.          editing on' or 'off'.  */
  904.       async_command_editing_p = 1;

  905.       /* When a character is detected on instream by select or poll,
  906.          readline will be invoked via this callback function.  */
  907.       call_readline = rl_callback_read_char_wrapper;
  908.     }
  909.   else
  910.     {
  911.       async_command_editing_p = 0;
  912.       call_readline = gdb_readline2;
  913.     }

  914.   /* When readline has read an end-of-line character, it passes the
  915.      complete line to gdb for processing; command_line_handler is the
  916.      function that does this.  */
  917.   input_handler = command_line_handler;

  918.   /* Tell readline to use the same input stream that gdb uses.  */
  919.   rl_instream = instream;

  920.   /* Get a file descriptor for the input stream, so that we can
  921.      register it with the event loop.  */
  922.   input_fd = fileno (instream);

  923.   /* Now we need to create the event sources for the input file
  924.      descriptor.  */
  925.   /* At this point in time, this is the only event source that we
  926.      register with the even loop.  Another source is going to be the
  927.      target program (inferior), but that must be registered only when
  928.      it actually exists (I.e. after we say 'run' or after we connect
  929.      to a remote target.  */
  930.   add_file_handler (input_fd, stdin_event_handler, 0);
  931. }

  932. /* Disable command input through the standard CLI channels.  Used in
  933.    the suspend proc for interpreters that use the standard gdb readline
  934.    interface, like the cli & the mi.  */
  935. void
  936. gdb_disable_readline (void)
  937. {
  938.   /* FIXME - It is too heavyweight to delete and remake these every
  939.      time you run an interpreter that needs readline.  It is probably
  940.      better to have the interpreters cache these, which in turn means
  941.      that this needs to be moved into interpreter specific code.  */

  942. #if 0
  943.   ui_file_delete (gdb_stdout);
  944.   ui_file_delete (gdb_stderr);
  945.   gdb_stdlog = NULL;
  946.   gdb_stdtarg = NULL;
  947.   gdb_stdtargerr = NULL;
  948. #endif

  949.   gdb_rl_callback_handler_remove ();
  950.   delete_file_handler (input_fd);
  951. }