GDB (API)
/home/stan/gdb/src/gdb/event-top.c
Go to the documentation of this file.
00001 /* Top level stuff for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1999-2013 Free Software Foundation, Inc.
00004 
00005    Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include "top.h"
00024 #include "inferior.h"
00025 #include "target.h"
00026 #include "terminal.h"           /* for job_control */
00027 #include "event-loop.h"
00028 #include "event-top.h"
00029 #include "interps.h"
00030 #include <signal.h>
00031 #include "exceptions.h"
00032 #include "cli/cli-script.h"     /* for reset_command_nest_depth */
00033 #include "main.h"
00034 #include "gdbthread.h"
00035 #include "observer.h"
00036 #include "continuations.h"
00037 #include "gdbcmd.h"             /* for dont_repeat() */
00038 #include "annotate.h"
00039 #include "maint.h"
00040 
00041 /* readline include files.  */
00042 #include "readline/readline.h"
00043 #include "readline/history.h"
00044 
00045 /* readline defines this.  */
00046 #undef savestring
00047 
00048 static void rl_callback_read_char_wrapper (gdb_client_data client_data);
00049 static void command_line_handler (char *rl);
00050 static void change_line_handler (void);
00051 static void command_handler (char *command);
00052 static char *top_level_prompt (void);
00053 
00054 /* Signal handlers.  */
00055 #ifdef SIGQUIT
00056 static void handle_sigquit (int sig);
00057 #endif
00058 #ifdef SIGHUP
00059 static void handle_sighup (int sig);
00060 #endif
00061 static void handle_sigfpe (int sig);
00062 
00063 /* Functions to be invoked by the event loop in response to
00064    signals.  */
00065 #if defined (SIGQUIT) || defined (SIGHUP)
00066 static void async_do_nothing (gdb_client_data);
00067 #endif
00068 #ifdef SIGHUP
00069 static void async_disconnect (gdb_client_data);
00070 #endif
00071 static void async_float_handler (gdb_client_data);
00072 #ifdef STOP_SIGNAL
00073 static void async_stop_sig (gdb_client_data);
00074 #endif
00075 
00076 /* Readline offers an alternate interface, via callback
00077    functions.  These are all included in the file callback.c in the
00078    readline distribution.  This file provides (mainly) a function, which
00079    the event loop uses as callback (i.e. event handler) whenever an event
00080    is detected on the standard input file descriptor.
00081    readline_callback_read_char is called (by the GDB event loop) whenever
00082    there is a new character ready on the input stream.  This function
00083    incrementally builds a buffer internal to readline where it
00084    accumulates the line read up to the point of invocation.  In the
00085    special case in which the character read is newline, the function
00086    invokes a GDB supplied callback routine, which does the processing of
00087    a full command line.  This latter routine is the asynchronous analog
00088    of the old command_line_input in gdb.  Instead of invoking (and waiting
00089    for) readline to read the command line and pass it back to
00090    command_loop for processing, the new command_line_handler function has
00091    the command line already available as its parameter.  INPUT_HANDLER is
00092    to be set to the function that readline will invoke when a complete
00093    line of input is ready.  CALL_READLINE is to be set to the function
00094    that readline offers as callback to the event_loop.  */
00095 
00096 void (*input_handler) (char *);
00097 void (*call_readline) (gdb_client_data);
00098 
00099 /* Important variables for the event loop.  */
00100 
00101 /* This is used to determine if GDB is using the readline library or
00102    its own simplified form of readline.  It is used by the asynchronous
00103    form of the set editing command.
00104    ezannoni: as of 1999-04-29 I expect that this
00105    variable will not be used after gdb is changed to use the event
00106    loop as default engine, and event-top.c is merged into top.c.  */
00107 int async_command_editing_p;
00108 
00109 /* This is the annotation suffix that will be used when the
00110    annotation_level is 2.  */
00111 char *async_annotation_suffix;
00112 
00113 /* This is used to display the notification of the completion of an
00114    asynchronous execution command.  */
00115 int exec_done_display_p = 0;
00116 
00117 /* This is the file descriptor for the input stream that GDB uses to
00118    read commands from.  */
00119 int input_fd;
00120 
00121 /* Signal handling variables.  */
00122 /* Each of these is a pointer to a function that the event loop will
00123    invoke if the corresponding signal has received.  The real signal
00124    handlers mark these functions as ready to be executed and the event
00125    loop, in a later iteration, calls them.  See the function
00126    invoke_async_signal_handler.  */
00127 static struct async_signal_handler *sigint_token;
00128 #ifdef SIGHUP
00129 static struct async_signal_handler *sighup_token;
00130 #endif
00131 #ifdef SIGQUIT
00132 static struct async_signal_handler *sigquit_token;
00133 #endif
00134 static struct async_signal_handler *sigfpe_token;
00135 #ifdef STOP_SIGNAL
00136 static struct async_signal_handler *sigtstp_token;
00137 #endif
00138 
00139 /* Structure to save a partially entered command.  This is used when
00140    the user types '\' at the end of a command line.  This is necessary
00141    because each line of input is handled by a different call to
00142    command_line_handler, and normally there is no state retained
00143    between different calls.  */
00144 static int more_to_come = 0;
00145 
00146 struct readline_input_state
00147   {
00148     char *linebuffer;
00149     char *linebuffer_ptr;
00150   }
00151 readline_input_state;
00152 
00153 /* This hook is called by rl_callback_read_char_wrapper after each
00154    character is processed.  */
00155 void (*after_char_processing_hook) (void);
00156 
00157 
00158 /* Wrapper function for calling into the readline library.  The event
00159    loop expects the callback function to have a paramter, while
00160    readline expects none.  */
00161 static void
00162 rl_callback_read_char_wrapper (gdb_client_data client_data)
00163 {
00164   rl_callback_read_char ();
00165   if (after_char_processing_hook)
00166     (*after_char_processing_hook) ();
00167 }
00168 
00169 /* Initialize all the necessary variables, start the event loop,
00170    register readline, and stdin, start the loop.  The DATA is the
00171    interpreter data cookie, ignored for now.  */
00172 
00173 void
00174 cli_command_loop (void *data)
00175 {
00176   display_gdb_prompt (0);
00177 
00178   /* Now it's time to start the event loop.  */
00179   start_event_loop ();
00180 }
00181 
00182 /* Change the function to be invoked every time there is a character
00183    ready on stdin.  This is used when the user sets the editing off,
00184    therefore bypassing readline, and letting gdb handle the input
00185    itself, via gdb_readline2.  Also it is used in the opposite case in
00186    which the user sets editing on again, by restoring readline
00187    handling of the input.  */
00188 static void
00189 change_line_handler (void)
00190 {
00191   /* NOTE: this operates on input_fd, not instream.  If we are reading
00192      commands from a file, instream will point to the file.  However in
00193      async mode, we always read commands from a file with editing
00194      off.  This means that the 'set editing on/off' will have effect
00195      only on the interactive session.  */
00196 
00197   if (async_command_editing_p)
00198     {
00199       /* Turn on editing by using readline.  */
00200       call_readline = rl_callback_read_char_wrapper;
00201       input_handler = command_line_handler;
00202     }
00203   else
00204     {
00205       /* Turn off editing by using gdb_readline2.  */
00206       rl_callback_handler_remove ();
00207       call_readline = gdb_readline2;
00208 
00209       /* Set up the command handler as well, in case we are called as
00210          first thing from .gdbinit.  */
00211       input_handler = command_line_handler;
00212     }
00213 }
00214 
00215 /* Displays the prompt.  If the argument NEW_PROMPT is NULL, the
00216    prompt that is displayed is the current top level prompt.
00217    Otherwise, it displays whatever NEW_PROMPT is as a local/secondary
00218    prompt.
00219 
00220    This is used after each gdb command has completed, and in the
00221    following cases:
00222 
00223    1. When the user enters a command line which is ended by '\'
00224    indicating that the command will continue on the next line.  In
00225    that case the prompt that is displayed is the empty string.
00226 
00227    2. When the user is entering 'commands' for a breakpoint, or
00228    actions for a tracepoint.  In this case the prompt will be '>'
00229 
00230    3. On prompting for pagination.  */
00231 
00232 void
00233 display_gdb_prompt (char *new_prompt)
00234 {
00235   char *actual_gdb_prompt = NULL;
00236   struct cleanup *old_chain;
00237 
00238   annotate_display_prompt ();
00239 
00240   /* Reset the nesting depth used when trace-commands is set.  */
00241   reset_command_nest_depth ();
00242 
00243   /* Each interpreter has its own rules on displaying the command
00244      prompt.  */
00245   if (!current_interp_display_prompt_p ())
00246     return;
00247 
00248   old_chain = make_cleanup (free_current_contents, &actual_gdb_prompt);
00249 
00250   /* Do not call the python hook on an explicit prompt change as
00251      passed to this function, as this forms a secondary/local prompt,
00252      IE, displayed but not set.  */
00253   if (! new_prompt)
00254     {
00255       if (sync_execution)
00256         {
00257           /* This is to trick readline into not trying to display the
00258              prompt.  Even though we display the prompt using this
00259              function, readline still tries to do its own display if
00260              we don't call rl_callback_handler_install and
00261              rl_callback_handler_remove (which readline detects
00262              because a global variable is not set).  If readline did
00263              that, it could mess up gdb signal handlers for SIGINT.
00264              Readline assumes that between calls to rl_set_signals and
00265              rl_clear_signals gdb doesn't do anything with the signal
00266              handlers.  Well, that's not the case, because when the
00267              target executes we change the SIGINT signal handler.  If
00268              we allowed readline to display the prompt, the signal
00269              handler change would happen exactly between the calls to
00270              the above two functions.  Calling
00271              rl_callback_handler_remove(), does the job.  */
00272 
00273           rl_callback_handler_remove ();
00274           do_cleanups (old_chain);
00275           return;
00276         }
00277       else
00278         {
00279           /* Display the top level prompt.  */
00280           actual_gdb_prompt = top_level_prompt ();
00281         }
00282     }
00283   else
00284     actual_gdb_prompt = xstrdup (new_prompt);
00285 
00286   if (async_command_editing_p)
00287     {
00288       rl_callback_handler_remove ();
00289       rl_callback_handler_install (actual_gdb_prompt, input_handler);
00290     }
00291   /* new_prompt at this point can be the top of the stack or the one
00292      passed in.  It can't be NULL.  */
00293   else
00294     {
00295       /* Don't use a _filtered function here.  It causes the assumed
00296          character position to be off, since the newline we read from
00297          the user is not accounted for.  */
00298       fputs_unfiltered (actual_gdb_prompt, gdb_stdout);
00299       gdb_flush (gdb_stdout);
00300     }
00301 
00302   do_cleanups (old_chain);
00303 }
00304 
00305 /* Return the top level prompt, as specified by "set prompt", possibly
00306    overriden by the python gdb.prompt_hook hook, and then composed
00307    with the prompt prefix and suffix (annotations).  The caller is
00308    responsible for freeing the returned string.  */
00309 
00310 static char *
00311 top_level_prompt (void)
00312 {
00313   char *prefix;
00314   char *prompt = NULL;
00315   char *suffix;
00316   char *composed_prompt;
00317   size_t prompt_length;
00318 
00319   /* Give observers a chance of changing the prompt.  E.g., the python
00320      `gdb.prompt_hook' is installed as an observer.  */
00321   observer_notify_before_prompt (get_prompt ());
00322 
00323   prompt = xstrdup (get_prompt ());
00324 
00325   if (annotation_level >= 2)
00326     {
00327       /* Prefix needs to have new line at end.  */
00328       prefix = (char *) alloca (strlen (async_annotation_suffix) + 10);
00329       strcpy (prefix, "\n\032\032pre-");
00330       strcat (prefix, async_annotation_suffix);
00331       strcat (prefix, "\n");
00332 
00333       /* Suffix needs to have a new line at end and \032 \032 at
00334          beginning.  */
00335       suffix = (char *) alloca (strlen (async_annotation_suffix) + 6);
00336       strcpy (suffix, "\n\032\032");
00337       strcat (suffix, async_annotation_suffix);
00338       strcat (suffix, "\n");
00339     }
00340   else
00341     {
00342       prefix = "";
00343       suffix = "";
00344     }
00345 
00346   prompt_length = strlen (prefix) + strlen (prompt) + strlen (suffix);
00347   composed_prompt = xmalloc (prompt_length + 1);
00348 
00349   strcpy (composed_prompt, prefix);
00350   strcat (composed_prompt, prompt);
00351   strcat (composed_prompt, suffix);
00352 
00353   xfree (prompt);
00354 
00355   return composed_prompt;
00356 }
00357 
00358 /* When there is an event ready on the stdin file desriptor, instead
00359    of calling readline directly throught the callback function, or
00360    instead of calling gdb_readline2, give gdb a chance to detect
00361    errors and do something.  */
00362 void
00363 stdin_event_handler (int error, gdb_client_data client_data)
00364 {
00365   if (error)
00366     {
00367       printf_unfiltered (_("error detected on stdin\n"));
00368       delete_file_handler (input_fd);
00369       discard_all_continuations ();
00370       discard_all_intermediate_continuations ();
00371       /* If stdin died, we may as well kill gdb.  */
00372       quit_command ((char *) 0, stdin == instream);
00373     }
00374   else
00375     (*call_readline) (client_data);
00376 }
00377 
00378 /* Re-enable stdin after the end of an execution command in
00379    synchronous mode, or after an error from the target, and we aborted
00380    the exec operation.  */
00381 
00382 void
00383 async_enable_stdin (void)
00384 {
00385   if (sync_execution)
00386     {
00387       /* See NOTE in async_disable_stdin().  */
00388       /* FIXME: cagney/1999-09-27: Call this before clearing
00389          sync_execution.  Current target_terminal_ours() implementations
00390          check for sync_execution before switching the terminal.  */
00391       target_terminal_ours ();
00392       sync_execution = 0;
00393     }
00394 }
00395 
00396 /* Disable reads from stdin (the console) marking the command as
00397    synchronous.  */
00398 
00399 void
00400 async_disable_stdin (void)
00401 {
00402   sync_execution = 1;
00403 }
00404 
00405 
00406 /* Handles a gdb command.  This function is called by
00407    command_line_handler, which has processed one or more input lines
00408    into COMMAND.  */
00409 /* NOTE: 1999-04-30 This is the asynchronous version of the command_loop
00410    function.  The command_loop function will be obsolete when we
00411    switch to use the event loop at every execution of gdb.  */
00412 static void
00413 command_handler (char *command)
00414 {
00415   int stdin_is_tty = ISATTY (stdin);
00416   struct cleanup *stat_chain;
00417 
00418   clear_quit_flag ();
00419   if (instream == stdin && stdin_is_tty)
00420     reinitialize_more_filter ();
00421 
00422   /* If readline returned a NULL command, it means that the connection
00423      with the terminal is gone.  This happens at the end of a
00424      testsuite run, after Expect has hung up but GDB is still alive.
00425      In such a case, we just quit gdb killing the inferior program
00426      too.  */
00427   if (command == 0)
00428     {
00429       printf_unfiltered ("quit\n");
00430       execute_command ("quit", stdin == instream);
00431     }
00432 
00433   stat_chain = make_command_stats_cleanup (1);
00434 
00435   execute_command (command, instream == stdin);
00436 
00437   /* Do any commands attached to breakpoint we stopped at.  */
00438   bpstat_do_actions ();
00439 
00440   do_cleanups (stat_chain);
00441 }
00442 
00443 /* Handle a complete line of input.  This is called by the callback
00444    mechanism within the readline library.  Deal with incomplete
00445    commands as well, by saving the partial input in a global
00446    buffer.  */
00447 
00448 /* NOTE: 1999-04-30 This is the asynchronous version of the
00449    command_line_input function; command_line_input will become
00450    obsolete once we use the event loop as the default mechanism in
00451    GDB.  */
00452 static void
00453 command_line_handler (char *rl)
00454 {
00455   static char *linebuffer = 0;
00456   static unsigned linelength = 0;
00457   char *p;
00458   char *p1;
00459   char *nline;
00460   int repeat = (instream == stdin);
00461 
00462   if (annotation_level > 1 && instream == stdin)
00463     {
00464       printf_unfiltered (("\n\032\032post-"));
00465       puts_unfiltered (async_annotation_suffix);
00466       printf_unfiltered (("\n"));
00467     }
00468 
00469   if (linebuffer == 0)
00470     {
00471       linelength = 80;
00472       linebuffer = (char *) xmalloc (linelength);
00473     }
00474 
00475   p = linebuffer;
00476 
00477   if (more_to_come)
00478     {
00479       strcpy (linebuffer, readline_input_state.linebuffer);
00480       p = readline_input_state.linebuffer_ptr;
00481       xfree (readline_input_state.linebuffer);
00482       more_to_come = 0;
00483     }
00484 
00485 #ifdef STOP_SIGNAL
00486   if (job_control)
00487     signal (STOP_SIGNAL, handle_stop_sig);
00488 #endif
00489 
00490   /* Make sure that all output has been output.  Some machines may let
00491      you get away with leaving out some of the gdb_flush, but not
00492      all.  */
00493   wrap_here ("");
00494   gdb_flush (gdb_stdout);
00495   gdb_flush (gdb_stderr);
00496 
00497   if (source_file_name != NULL)
00498     ++source_line_number;
00499 
00500   /* If we are in this case, then command_handler will call quit 
00501      and exit from gdb.  */
00502   if (!rl || rl == (char *) EOF)
00503     {
00504       command_handler (0);
00505       return;                   /* Lint.  */
00506     }
00507   if (strlen (rl) + 1 + (p - linebuffer) > linelength)
00508     {
00509       linelength = strlen (rl) + 1 + (p - linebuffer);
00510       nline = (char *) xrealloc (linebuffer, linelength);
00511       p += nline - linebuffer;
00512       linebuffer = nline;
00513     }
00514   p1 = rl;
00515   /* Copy line.  Don't copy null at end.  (Leaves line alone
00516      if this was just a newline).  */
00517   while (*p1)
00518     *p++ = *p1++;
00519 
00520   xfree (rl);                   /* Allocated in readline.  */
00521 
00522   if (p > linebuffer && *(p - 1) == '\\')
00523     {
00524       *p = '\0';
00525       p--;                      /* Put on top of '\'.  */
00526 
00527       readline_input_state.linebuffer = xstrdup (linebuffer);
00528       readline_input_state.linebuffer_ptr = p;
00529 
00530       /* We will not invoke a execute_command if there is more
00531          input expected to complete the command.  So, we need to
00532          print an empty prompt here.  */
00533       more_to_come = 1;
00534       display_gdb_prompt ("");
00535       return;
00536     }
00537 
00538 #ifdef STOP_SIGNAL
00539   if (job_control)
00540     signal (STOP_SIGNAL, SIG_DFL);
00541 #endif
00542 
00543 #define SERVER_COMMAND_LENGTH 7
00544   server_command =
00545     (p - linebuffer > SERVER_COMMAND_LENGTH)
00546     && strncmp (linebuffer, "server ", SERVER_COMMAND_LENGTH) == 0;
00547   if (server_command)
00548     {
00549       /* Note that we don't set `line'.  Between this and the check in
00550          dont_repeat, this insures that repeating will still do the
00551          right thing.  */
00552       *p = '\0';
00553       command_handler (linebuffer + SERVER_COMMAND_LENGTH);
00554       display_gdb_prompt (0);
00555       return;
00556     }
00557 
00558   /* Do history expansion if that is wished.  */
00559   if (history_expansion_p && instream == stdin
00560       && ISATTY (instream))
00561     {
00562       char *history_value;
00563       int expanded;
00564 
00565       *p = '\0';                /* Insert null now.  */
00566       expanded = history_expand (linebuffer, &history_value);
00567       if (expanded)
00568         {
00569           /* Print the changes.  */
00570           printf_unfiltered ("%s\n", history_value);
00571 
00572           /* If there was an error, call this function again.  */
00573           if (expanded < 0)
00574             {
00575               xfree (history_value);
00576               return;
00577             }
00578           if (strlen (history_value) > linelength)
00579             {
00580               linelength = strlen (history_value) + 1;
00581               linebuffer = (char *) xrealloc (linebuffer, linelength);
00582             }
00583           strcpy (linebuffer, history_value);
00584           p = linebuffer + strlen (linebuffer);
00585         }
00586       xfree (history_value);
00587     }
00588 
00589   /* If we just got an empty line, and that is supposed to repeat the
00590      previous command, return the value in the global buffer.  */
00591   if (repeat && p == linebuffer && *p != '\\')
00592     {
00593       command_handler (saved_command_line);
00594       display_gdb_prompt (0);
00595       return;
00596     }
00597 
00598   for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
00599   if (repeat && !*p1)
00600     {
00601       command_handler (saved_command_line);
00602       display_gdb_prompt (0);
00603       return;
00604     }
00605 
00606   *p = 0;
00607 
00608   /* Add line to history if appropriate.  */
00609   if (instream == stdin
00610       && ISATTY (stdin) && *linebuffer)
00611     add_history (linebuffer);
00612 
00613   /* Note: lines consisting solely of comments are added to the command
00614      history.  This is useful when you type a command, and then
00615      realize you don't want to execute it quite yet.  You can comment
00616      out the command and then later fetch it from the value history
00617      and remove the '#'.  The kill ring is probably better, but some
00618      people are in the habit of commenting things out.  */
00619   if (*p1 == '#')
00620     *p1 = '\0';                 /* Found a comment.  */
00621 
00622   /* Save into global buffer if appropriate.  */
00623   if (repeat)
00624     {
00625       if (linelength > saved_command_line_size)
00626         {
00627           saved_command_line = xrealloc (saved_command_line, linelength);
00628           saved_command_line_size = linelength;
00629         }
00630       strcpy (saved_command_line, linebuffer);
00631       if (!more_to_come)
00632         {
00633           command_handler (saved_command_line);
00634           display_gdb_prompt (0);
00635         }
00636       return;
00637     }
00638 
00639   command_handler (linebuffer);
00640   display_gdb_prompt (0);
00641   return;
00642 }
00643 
00644 /* Does reading of input from terminal w/o the editing features
00645    provided by the readline library.  */
00646 
00647 /* NOTE: 1999-04-30 Asynchronous version of gdb_readline; gdb_readline
00648    will become obsolete when the event loop is made the default
00649    execution for gdb.  */
00650 void
00651 gdb_readline2 (gdb_client_data client_data)
00652 {
00653   int c;
00654   char *result;
00655   int input_index = 0;
00656   int result_size = 80;
00657   static int done_once = 0;
00658 
00659   /* Unbuffer the input stream, so that, later on, the calls to fgetc
00660      fetch only one char at the time from the stream.  The fgetc's will
00661      get up to the first newline, but there may be more chars in the
00662      stream after '\n'.  If we buffer the input and fgetc drains the
00663      stream, getting stuff beyond the newline as well, a select, done
00664      afterwards will not trigger.  */
00665   if (!done_once && !ISATTY (instream))
00666     {
00667       setbuf (instream, NULL);
00668       done_once = 1;
00669     }
00670 
00671   result = (char *) xmalloc (result_size);
00672 
00673   /* We still need the while loop here, even though it would seem
00674      obvious to invoke gdb_readline2 at every character entered.  If
00675      not using the readline library, the terminal is in cooked mode,
00676      which sends the characters all at once.  Poll will notice that the
00677      input fd has changed state only after enter is pressed.  At this
00678      point we still need to fetch all the chars entered.  */
00679 
00680   while (1)
00681     {
00682       /* Read from stdin if we are executing a user defined command.
00683          This is the right thing for prompt_for_continue, at least.  */
00684       c = fgetc (instream ? instream : stdin);
00685 
00686       if (c == EOF)
00687         {
00688           if (input_index > 0)
00689             /* The last line does not end with a newline.  Return it,
00690                and if we are called again fgetc will still return EOF
00691                and we'll return NULL then.  */
00692             break;
00693           xfree (result);
00694           (*input_handler) (0);
00695           return;
00696         }
00697 
00698       if (c == '\n')
00699         {
00700           if (input_index > 0 && result[input_index - 1] == '\r')
00701             input_index--;
00702           break;
00703         }
00704 
00705       result[input_index++] = c;
00706       while (input_index >= result_size)
00707         {
00708           result_size *= 2;
00709           result = (char *) xrealloc (result, result_size);
00710         }
00711     }
00712 
00713   result[input_index++] = '\0';
00714   (*input_handler) (result);
00715 }
00716 
00717 
00718 /* Initialization of signal handlers and tokens.  There is a function
00719    handle_sig* for each of the signals GDB cares about.  Specifically:
00720    SIGINT, SIGFPE, SIGQUIT, SIGTSTP, SIGHUP, SIGWINCH.  These
00721    functions are the actual signal handlers associated to the signals
00722    via calls to signal().  The only job for these functions is to
00723    enqueue the appropriate event/procedure with the event loop.  Such
00724    procedures are the old signal handlers.  The event loop will take
00725    care of invoking the queued procedures to perform the usual tasks
00726    associated with the reception of the signal.  */
00727 /* NOTE: 1999-04-30 This is the asynchronous version of init_signals.
00728    init_signals will become obsolete as we move to have to event loop
00729    as the default for gdb.  */
00730 void
00731 async_init_signals (void)
00732 {
00733   signal (SIGINT, handle_sigint);
00734   sigint_token =
00735     create_async_signal_handler (async_request_quit, NULL);
00736   signal (SIGTERM, handle_sigterm);
00737 
00738   /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
00739      to the inferior and breakpoints will be ignored.  */
00740 #ifdef SIGTRAP
00741   signal (SIGTRAP, SIG_DFL);
00742 #endif
00743 
00744 #ifdef SIGQUIT
00745   /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
00746      passed to the inferior, which we don't want.  It would be
00747      possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
00748      on BSD4.3 systems using vfork, that can affect the
00749      GDB process as well as the inferior (the signal handling tables
00750      might be in memory, shared between the two).  Since we establish
00751      a handler for SIGQUIT, when we call exec it will set the signal
00752      to SIG_DFL for us.  */
00753   signal (SIGQUIT, handle_sigquit);
00754   sigquit_token =
00755     create_async_signal_handler (async_do_nothing, NULL);
00756 #endif
00757 #ifdef SIGHUP
00758   if (signal (SIGHUP, handle_sighup) != SIG_IGN)
00759     sighup_token =
00760       create_async_signal_handler (async_disconnect, NULL);
00761   else
00762     sighup_token =
00763       create_async_signal_handler (async_do_nothing, NULL);
00764 #endif
00765   signal (SIGFPE, handle_sigfpe);
00766   sigfpe_token =
00767     create_async_signal_handler (async_float_handler, NULL);
00768 
00769 #ifdef STOP_SIGNAL
00770   sigtstp_token =
00771     create_async_signal_handler (async_stop_sig, NULL);
00772 #endif
00773 
00774 }
00775 
00776 /* Tell the event loop what to do if SIGINT is received.
00777    See event-signal.c.  */
00778 void
00779 handle_sigint (int sig)
00780 {
00781   signal (sig, handle_sigint);
00782 
00783   /* We could be running in a loop reading in symfiles or something so
00784      it may be quite a while before we get back to the event loop.  So
00785      set quit_flag to 1 here.  Then if QUIT is called before we get to
00786      the event loop, we will unwind as expected.  */
00787 
00788   set_quit_flag ();
00789 
00790   /* If immediate_quit is set, we go ahead and process the SIGINT right
00791      away, even if we usually would defer this to the event loop.  The
00792      assumption here is that it is safe to process ^C immediately if
00793      immediate_quit is set.  If we didn't, SIGINT would be really
00794      processed only the next time through the event loop.  To get to
00795      that point, though, the command that we want to interrupt needs to
00796      finish first, which is unacceptable.  If immediate quit is not set,
00797      we process SIGINT the next time through the loop, which is fine.  */
00798   gdb_call_async_signal_handler (sigint_token, immediate_quit);
00799 }
00800 
00801 /* Quit GDB if SIGTERM is received.
00802    GDB would quit anyway, but this way it will clean up properly.  */
00803 void
00804 handle_sigterm (int sig)
00805 {
00806   signal (sig, handle_sigterm);
00807   quit_force ((char *) 0, stdin == instream);
00808 }
00809 
00810 /* Do the quit.  All the checks have been done by the caller.  */
00811 void
00812 async_request_quit (gdb_client_data arg)
00813 {
00814   /* If the quit_flag has gotten reset back to 0 by the time we get
00815      back here, that means that an exception was thrown to unwind the
00816      current command before we got back to the event loop.  So there
00817      is no reason to call quit again here.  */
00818 
00819   if (check_quit_flag ())
00820     quit ();
00821 }
00822 
00823 #ifdef SIGQUIT
00824 /* Tell the event loop what to do if SIGQUIT is received.
00825    See event-signal.c.  */
00826 static void
00827 handle_sigquit (int sig)
00828 {
00829   mark_async_signal_handler (sigquit_token);
00830   signal (sig, handle_sigquit);
00831 }
00832 #endif
00833 
00834 #if defined (SIGQUIT) || defined (SIGHUP)
00835 /* Called by the event loop in response to a SIGQUIT or an
00836    ignored SIGHUP.  */
00837 static void
00838 async_do_nothing (gdb_client_data arg)
00839 {
00840   /* Empty function body.  */
00841 }
00842 #endif
00843 
00844 #ifdef SIGHUP
00845 /* Tell the event loop what to do if SIGHUP is received.
00846    See event-signal.c.  */
00847 static void
00848 handle_sighup (int sig)
00849 {
00850   mark_async_signal_handler (sighup_token);
00851   signal (sig, handle_sighup);
00852 }
00853 
00854 /* Called by the event loop to process a SIGHUP.  */
00855 static void
00856 async_disconnect (gdb_client_data arg)
00857 {
00858   volatile struct gdb_exception exception;
00859 
00860   TRY_CATCH (exception, RETURN_MASK_ALL)
00861     {
00862       quit_cover ();
00863     }
00864 
00865   if (exception.reason < 0)
00866     {
00867       fputs_filtered ("Could not kill the program being debugged",
00868                       gdb_stderr);
00869       exception_print (gdb_stderr, exception);
00870     }
00871 
00872   TRY_CATCH (exception, RETURN_MASK_ALL)
00873     {
00874       pop_all_targets ();
00875     }
00876 
00877   signal (SIGHUP, SIG_DFL);     /*FIXME: ???????????  */
00878   raise (SIGHUP);
00879 }
00880 #endif
00881 
00882 #ifdef STOP_SIGNAL
00883 void
00884 handle_stop_sig (int sig)
00885 {
00886   mark_async_signal_handler (sigtstp_token);
00887   signal (sig, handle_stop_sig);
00888 }
00889 
00890 static void
00891 async_stop_sig (gdb_client_data arg)
00892 {
00893   char *prompt = get_prompt ();
00894 
00895 #if STOP_SIGNAL == SIGTSTP
00896   signal (SIGTSTP, SIG_DFL);
00897 #if HAVE_SIGPROCMASK
00898   {
00899     sigset_t zero;
00900 
00901     sigemptyset (&zero);
00902     sigprocmask (SIG_SETMASK, &zero, 0);
00903   }
00904 #elif HAVE_SIGSETMASK
00905   sigsetmask (0);
00906 #endif
00907   raise (SIGTSTP);
00908   signal (SIGTSTP, handle_stop_sig);
00909 #else
00910   signal (STOP_SIGNAL, handle_stop_sig);
00911 #endif
00912   printf_unfiltered ("%s", prompt);
00913   gdb_flush (gdb_stdout);
00914 
00915   /* Forget about any previous command -- null line now will do
00916      nothing.  */
00917   dont_repeat ();
00918 }
00919 #endif /* STOP_SIGNAL */
00920 
00921 /* Tell the event loop what to do if SIGFPE is received.
00922    See event-signal.c.  */
00923 static void
00924 handle_sigfpe (int sig)
00925 {
00926   mark_async_signal_handler (sigfpe_token);
00927   signal (sig, handle_sigfpe);
00928 }
00929 
00930 /* Event loop will call this functin to process a SIGFPE.  */
00931 static void
00932 async_float_handler (gdb_client_data arg)
00933 {
00934   /* This message is based on ANSI C, section 4.7.  Note that integer
00935      divide by zero causes this, so "float" is a misnomer.  */
00936   error (_("Erroneous arithmetic operation."));
00937 }
00938 
00939 
00940 /* Called by do_setshow_command.  */
00941 void
00942 set_async_editing_command (char *args, int from_tty,
00943                            struct cmd_list_element *c)
00944 {
00945   change_line_handler ();
00946 }
00947 
00948 /* Set things up for readline to be invoked via the alternate
00949    interface, i.e. via a callback function (rl_callback_read_char),
00950    and hook up instream to the event loop.  */
00951 void
00952 gdb_setup_readline (void)
00953 {
00954   /* This function is a noop for the sync case.  The assumption is
00955      that the sync setup is ALL done in gdb_init, and we would only
00956      mess it up here.  The sync stuff should really go away over
00957      time.  */
00958   if (!batch_silent)
00959     gdb_stdout = stdio_fileopen (stdout);
00960   gdb_stderr = stderr_fileopen ();
00961   gdb_stdlog = gdb_stderr;  /* for moment */
00962   gdb_stdtarg = gdb_stderr; /* for moment */
00963   gdb_stdtargerr = gdb_stderr; /* for moment */
00964 
00965   /* If the input stream is connected to a terminal, turn on
00966      editing.  */
00967   if (ISATTY (instream))
00968     {
00969       /* Tell gdb that we will be using the readline library.  This
00970          could be overwritten by a command in .gdbinit like 'set
00971          editing on' or 'off'.  */
00972       async_command_editing_p = 1;
00973           
00974       /* When a character is detected on instream by select or poll,
00975          readline will be invoked via this callback function.  */
00976       call_readline = rl_callback_read_char_wrapper;
00977     }
00978   else
00979     {
00980       async_command_editing_p = 0;
00981       call_readline = gdb_readline2;
00982     }
00983   
00984   /* When readline has read an end-of-line character, it passes the
00985      complete line to gdb for processing; command_line_handler is the
00986      function that does this.  */
00987   input_handler = command_line_handler;
00988       
00989   /* Tell readline to use the same input stream that gdb uses.  */
00990   rl_instream = instream;
00991 
00992   /* Get a file descriptor for the input stream, so that we can
00993      register it with the event loop.  */
00994   input_fd = fileno (instream);
00995 
00996   /* Now we need to create the event sources for the input file
00997      descriptor.  */
00998   /* At this point in time, this is the only event source that we
00999      register with the even loop.  Another source is going to be the
01000      target program (inferior), but that must be registered only when
01001      it actually exists (I.e. after we say 'run' or after we connect
01002      to a remote target.  */
01003   add_file_handler (input_fd, stdin_event_handler, 0);
01004 }
01005 
01006 /* Disable command input through the standard CLI channels.  Used in
01007    the suspend proc for interpreters that use the standard gdb readline
01008    interface, like the cli & the mi.  */
01009 void
01010 gdb_disable_readline (void)
01011 {
01012   /* FIXME - It is too heavyweight to delete and remake these every
01013      time you run an interpreter that needs readline.  It is probably
01014      better to have the interpreters cache these, which in turn means
01015      that this needs to be moved into interpreter specific code.  */
01016 
01017 #if 0
01018   ui_file_delete (gdb_stdout);
01019   ui_file_delete (gdb_stderr);
01020   gdb_stdlog = NULL;
01021   gdb_stdtarg = NULL;
01022   gdb_stdtargerr = NULL;
01023 #endif
01024 
01025   rl_callback_handler_remove ();
01026   delete_file_handler (input_fd);
01027 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines