xref: /dragonfly/contrib/gdb-7/gdb/event-top.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Top level stuff for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1999-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "top.h"
245796c8dcSSimon Schubert #include "inferior.h"
255796c8dcSSimon Schubert #include "target.h"
265796c8dcSSimon Schubert #include "terminal.h"		/* for job_control */
275796c8dcSSimon Schubert #include "event-loop.h"
285796c8dcSSimon Schubert #include "event-top.h"
295796c8dcSSimon Schubert #include "interps.h"
305796c8dcSSimon Schubert #include <signal.h>
315796c8dcSSimon Schubert #include "exceptions.h"
325796c8dcSSimon Schubert #include "cli/cli-script.h"     /* for reset_command_nest_depth */
335796c8dcSSimon Schubert #include "main.h"
345796c8dcSSimon Schubert #include "gdbthread.h"
35a45ae5f8SJohn Marino #include "observer.h"
36a45ae5f8SJohn Marino #include "continuations.h"
37c50c785cSJohn Marino #include "gdbcmd.h"		/* for dont_repeat() */
38*ef5ccd6cSJohn Marino #include "annotate.h"
395796c8dcSSimon Schubert 
40c50c785cSJohn Marino /* readline include files.  */
415796c8dcSSimon Schubert #include "readline/readline.h"
425796c8dcSSimon Schubert #include "readline/history.h"
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert /* readline defines this.  */
455796c8dcSSimon Schubert #undef savestring
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert static void rl_callback_read_char_wrapper (gdb_client_data client_data);
485796c8dcSSimon Schubert static void command_line_handler (char *rl);
495796c8dcSSimon Schubert static void change_line_handler (void);
505796c8dcSSimon Schubert static void command_handler (char *command);
51a45ae5f8SJohn Marino static char *top_level_prompt (void);
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert /* Signal handlers.  */
545796c8dcSSimon Schubert #ifdef SIGQUIT
555796c8dcSSimon Schubert static void handle_sigquit (int sig);
565796c8dcSSimon Schubert #endif
575796c8dcSSimon Schubert #ifdef SIGHUP
585796c8dcSSimon Schubert static void handle_sighup (int sig);
595796c8dcSSimon Schubert #endif
605796c8dcSSimon Schubert static void handle_sigfpe (int sig);
615796c8dcSSimon Schubert 
625796c8dcSSimon Schubert /* Functions to be invoked by the event loop in response to
635796c8dcSSimon Schubert    signals.  */
645796c8dcSSimon Schubert #if defined (SIGQUIT) || defined (SIGHUP)
655796c8dcSSimon Schubert static void async_do_nothing (gdb_client_data);
665796c8dcSSimon Schubert #endif
675796c8dcSSimon Schubert #ifdef SIGHUP
685796c8dcSSimon Schubert static void async_disconnect (gdb_client_data);
695796c8dcSSimon Schubert #endif
705796c8dcSSimon Schubert static void async_float_handler (gdb_client_data);
715796c8dcSSimon Schubert #ifdef STOP_SIGNAL
725796c8dcSSimon Schubert static void async_stop_sig (gdb_client_data);
735796c8dcSSimon Schubert #endif
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert /* Readline offers an alternate interface, via callback
765796c8dcSSimon Schubert    functions.  These are all included in the file callback.c in the
775796c8dcSSimon Schubert    readline distribution.  This file provides (mainly) a function, which
785796c8dcSSimon Schubert    the event loop uses as callback (i.e. event handler) whenever an event
795796c8dcSSimon Schubert    is detected on the standard input file descriptor.
805796c8dcSSimon Schubert    readline_callback_read_char is called (by the GDB event loop) whenever
815796c8dcSSimon Schubert    there is a new character ready on the input stream.  This function
825796c8dcSSimon Schubert    incrementally builds a buffer internal to readline where it
835796c8dcSSimon Schubert    accumulates the line read up to the point of invocation.  In the
845796c8dcSSimon Schubert    special case in which the character read is newline, the function
855796c8dcSSimon Schubert    invokes a GDB supplied callback routine, which does the processing of
865796c8dcSSimon Schubert    a full command line.  This latter routine is the asynchronous analog
875796c8dcSSimon Schubert    of the old command_line_input in gdb.  Instead of invoking (and waiting
885796c8dcSSimon Schubert    for) readline to read the command line and pass it back to
895796c8dcSSimon Schubert    command_loop for processing, the new command_line_handler function has
905796c8dcSSimon Schubert    the command line already available as its parameter.  INPUT_HANDLER is
915796c8dcSSimon Schubert    to be set to the function that readline will invoke when a complete
925796c8dcSSimon Schubert    line of input is ready.  CALL_READLINE is to be set to the function
935796c8dcSSimon Schubert    that readline offers as callback to the event_loop.  */
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert void (*input_handler) (char *);
965796c8dcSSimon Schubert void (*call_readline) (gdb_client_data);
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert /* Important variables for the event loop.  */
995796c8dcSSimon Schubert 
1005796c8dcSSimon Schubert /* This is used to determine if GDB is using the readline library or
1015796c8dcSSimon Schubert    its own simplified form of readline.  It is used by the asynchronous
1025796c8dcSSimon Schubert    form of the set editing command.
1035796c8dcSSimon Schubert    ezannoni: as of 1999-04-29 I expect that this
1045796c8dcSSimon Schubert    variable will not be used after gdb is changed to use the event
1055796c8dcSSimon Schubert    loop as default engine, and event-top.c is merged into top.c.  */
1065796c8dcSSimon Schubert int async_command_editing_p;
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert /* This is the annotation suffix that will be used when the
1095796c8dcSSimon Schubert    annotation_level is 2.  */
1105796c8dcSSimon Schubert char *async_annotation_suffix;
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert /* This is used to display the notification of the completion of an
1135796c8dcSSimon Schubert    asynchronous execution command.  */
1145796c8dcSSimon Schubert int exec_done_display_p = 0;
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert /* This is the file descriptor for the input stream that GDB uses to
1175796c8dcSSimon Schubert    read commands from.  */
1185796c8dcSSimon Schubert int input_fd;
1195796c8dcSSimon Schubert 
120c50c785cSJohn Marino /* Signal handling variables.  */
1215796c8dcSSimon Schubert /* Each of these is a pointer to a function that the event loop will
1225796c8dcSSimon Schubert    invoke if the corresponding signal has received.  The real signal
1235796c8dcSSimon Schubert    handlers mark these functions as ready to be executed and the event
1245796c8dcSSimon Schubert    loop, in a later iteration, calls them.  See the function
1255796c8dcSSimon Schubert    invoke_async_signal_handler.  */
126*ef5ccd6cSJohn Marino static struct async_signal_handler *sigint_token;
1275796c8dcSSimon Schubert #ifdef SIGHUP
128*ef5ccd6cSJohn Marino static struct async_signal_handler *sighup_token;
1295796c8dcSSimon Schubert #endif
1305796c8dcSSimon Schubert #ifdef SIGQUIT
131*ef5ccd6cSJohn Marino static struct async_signal_handler *sigquit_token;
1325796c8dcSSimon Schubert #endif
133*ef5ccd6cSJohn Marino static struct async_signal_handler *sigfpe_token;
1345796c8dcSSimon Schubert #ifdef STOP_SIGNAL
135*ef5ccd6cSJohn Marino static struct async_signal_handler *sigtstp_token;
1365796c8dcSSimon Schubert #endif
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert /* Structure to save a partially entered command.  This is used when
1395796c8dcSSimon Schubert    the user types '\' at the end of a command line.  This is necessary
1405796c8dcSSimon Schubert    because each line of input is handled by a different call to
1415796c8dcSSimon Schubert    command_line_handler, and normally there is no state retained
1425796c8dcSSimon Schubert    between different calls.  */
143a45ae5f8SJohn Marino static int more_to_come = 0;
1445796c8dcSSimon Schubert 
1455796c8dcSSimon Schubert struct readline_input_state
1465796c8dcSSimon Schubert   {
1475796c8dcSSimon Schubert     char *linebuffer;
1485796c8dcSSimon Schubert     char *linebuffer_ptr;
1495796c8dcSSimon Schubert   }
1505796c8dcSSimon Schubert readline_input_state;
1515796c8dcSSimon Schubert 
1525796c8dcSSimon Schubert /* This hook is called by rl_callback_read_char_wrapper after each
1535796c8dcSSimon Schubert    character is processed.  */
154c50c785cSJohn Marino void (*after_char_processing_hook) (void);
1555796c8dcSSimon Schubert 
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert /* Wrapper function for calling into the readline library.  The event
158c50c785cSJohn Marino    loop expects the callback function to have a paramter, while
159c50c785cSJohn Marino    readline expects none.  */
1605796c8dcSSimon Schubert static void
rl_callback_read_char_wrapper(gdb_client_data client_data)1615796c8dcSSimon Schubert rl_callback_read_char_wrapper (gdb_client_data client_data)
1625796c8dcSSimon Schubert {
1635796c8dcSSimon Schubert   rl_callback_read_char ();
1645796c8dcSSimon Schubert   if (after_char_processing_hook)
1655796c8dcSSimon Schubert     (*after_char_processing_hook) ();
1665796c8dcSSimon Schubert }
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert /* Initialize all the necessary variables, start the event loop,
1695796c8dcSSimon Schubert    register readline, and stdin, start the loop.  */
1705796c8dcSSimon Schubert void
cli_command_loop(void)1715796c8dcSSimon Schubert cli_command_loop (void)
1725796c8dcSSimon Schubert {
1735796c8dcSSimon Schubert   display_gdb_prompt (0);
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert   /* Now it's time to start the event loop.  */
1765796c8dcSSimon Schubert   start_event_loop ();
1775796c8dcSSimon Schubert }
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert /* Change the function to be invoked every time there is a character
1805796c8dcSSimon Schubert    ready on stdin.  This is used when the user sets the editing off,
1815796c8dcSSimon Schubert    therefore bypassing readline, and letting gdb handle the input
1825796c8dcSSimon Schubert    itself, via gdb_readline2.  Also it is used in the opposite case in
1835796c8dcSSimon Schubert    which the user sets editing on again, by restoring readline
1845796c8dcSSimon Schubert    handling of the input.  */
1855796c8dcSSimon Schubert static void
change_line_handler(void)1865796c8dcSSimon Schubert change_line_handler (void)
1875796c8dcSSimon Schubert {
1885796c8dcSSimon Schubert   /* NOTE: this operates on input_fd, not instream.  If we are reading
1895796c8dcSSimon Schubert      commands from a file, instream will point to the file.  However in
1905796c8dcSSimon Schubert      async mode, we always read commands from a file with editing
1915796c8dcSSimon Schubert      off.  This means that the 'set editing on/off' will have effect
1925796c8dcSSimon Schubert      only on the interactive session.  */
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert   if (async_command_editing_p)
1955796c8dcSSimon Schubert     {
1965796c8dcSSimon Schubert       /* Turn on editing by using readline.  */
1975796c8dcSSimon Schubert       call_readline = rl_callback_read_char_wrapper;
1985796c8dcSSimon Schubert       input_handler = command_line_handler;
1995796c8dcSSimon Schubert     }
2005796c8dcSSimon Schubert   else
2015796c8dcSSimon Schubert     {
2025796c8dcSSimon Schubert       /* Turn off editing by using gdb_readline2.  */
2035796c8dcSSimon Schubert       rl_callback_handler_remove ();
2045796c8dcSSimon Schubert       call_readline = gdb_readline2;
2055796c8dcSSimon Schubert 
2065796c8dcSSimon Schubert       /* Set up the command handler as well, in case we are called as
2075796c8dcSSimon Schubert          first thing from .gdbinit.  */
2085796c8dcSSimon Schubert       input_handler = command_line_handler;
2095796c8dcSSimon Schubert     }
2105796c8dcSSimon Schubert }
2115796c8dcSSimon Schubert 
212a45ae5f8SJohn Marino /* Displays the prompt.  If the argument NEW_PROMPT is NULL, the
213a45ae5f8SJohn Marino    prompt that is displayed is the current top level prompt.
214a45ae5f8SJohn Marino    Otherwise, it displays whatever NEW_PROMPT is as a local/secondary
215a45ae5f8SJohn Marino    prompt.
216a45ae5f8SJohn Marino 
217a45ae5f8SJohn Marino    This is used after each gdb command has completed, and in the
218a45ae5f8SJohn Marino    following cases:
219a45ae5f8SJohn Marino 
220c50c785cSJohn Marino    1. When the user enters a command line which is ended by '\'
221a45ae5f8SJohn Marino    indicating that the command will continue on the next line.  In
222a45ae5f8SJohn Marino    that case the prompt that is displayed is the empty string.
223a45ae5f8SJohn Marino 
2245796c8dcSSimon Schubert    2. When the user is entering 'commands' for a breakpoint, or
2255796c8dcSSimon Schubert    actions for a tracepoint.  In this case the prompt will be '>'
226a45ae5f8SJohn Marino 
227a45ae5f8SJohn Marino    3. On prompting for pagination.  */
228a45ae5f8SJohn Marino 
2295796c8dcSSimon Schubert void
display_gdb_prompt(char * new_prompt)2305796c8dcSSimon Schubert display_gdb_prompt (char *new_prompt)
2315796c8dcSSimon Schubert {
232a45ae5f8SJohn Marino   char *actual_gdb_prompt = NULL;
233a45ae5f8SJohn Marino   struct cleanup *old_chain;
2345796c8dcSSimon Schubert 
235*ef5ccd6cSJohn Marino   annotate_display_prompt ();
236*ef5ccd6cSJohn Marino 
2375796c8dcSSimon Schubert   /* Reset the nesting depth used when trace-commands is set.  */
2385796c8dcSSimon Schubert   reset_command_nest_depth ();
2395796c8dcSSimon Schubert 
2405796c8dcSSimon Schubert   /* Each interpreter has its own rules on displaying the command
2415796c8dcSSimon Schubert      prompt.  */
2425796c8dcSSimon Schubert   if (!current_interp_display_prompt_p ())
2435796c8dcSSimon Schubert     return;
2445796c8dcSSimon Schubert 
245a45ae5f8SJohn Marino   old_chain = make_cleanup (free_current_contents, &actual_gdb_prompt);
246a45ae5f8SJohn Marino 
247a45ae5f8SJohn Marino   /* Do not call the python hook on an explicit prompt change as
248a45ae5f8SJohn Marino      passed to this function, as this forms a secondary/local prompt,
249a45ae5f8SJohn Marino      IE, displayed but not set.  */
250a45ae5f8SJohn Marino   if (! new_prompt)
251a45ae5f8SJohn Marino     {
252a45ae5f8SJohn Marino       if (sync_execution)
2535796c8dcSSimon Schubert 	{
2545796c8dcSSimon Schubert 	  /* This is to trick readline into not trying to display the
2555796c8dcSSimon Schubert 	     prompt.  Even though we display the prompt using this
256a45ae5f8SJohn Marino 	     function, readline still tries to do its own display if
257a45ae5f8SJohn Marino 	     we don't call rl_callback_handler_install and
258a45ae5f8SJohn Marino 	     rl_callback_handler_remove (which readline detects
259a45ae5f8SJohn Marino 	     because a global variable is not set).  If readline did
260a45ae5f8SJohn Marino 	     that, it could mess up gdb signal handlers for SIGINT.
261a45ae5f8SJohn Marino 	     Readline assumes that between calls to rl_set_signals and
262a45ae5f8SJohn Marino 	     rl_clear_signals gdb doesn't do anything with the signal
263a45ae5f8SJohn Marino 	     handlers.  Well, that's not the case, because when the
264a45ae5f8SJohn Marino 	     target executes we change the SIGINT signal handler.  If
265a45ae5f8SJohn Marino 	     we allowed readline to display the prompt, the signal
266a45ae5f8SJohn Marino 	     handler change would happen exactly between the calls to
267a45ae5f8SJohn Marino 	     the above two functions.  Calling
268a45ae5f8SJohn Marino 	     rl_callback_handler_remove(), does the job.  */
2695796c8dcSSimon Schubert 
2705796c8dcSSimon Schubert 	  rl_callback_handler_remove ();
2715796c8dcSSimon Schubert 	  return;
2725796c8dcSSimon Schubert 	}
273a45ae5f8SJohn Marino       else
2745796c8dcSSimon Schubert 	{
275a45ae5f8SJohn Marino 	  /* Display the top level prompt.  */
276a45ae5f8SJohn Marino 	  actual_gdb_prompt = top_level_prompt ();
2775796c8dcSSimon Schubert 	}
278a45ae5f8SJohn Marino     }
279a45ae5f8SJohn Marino   else
280a45ae5f8SJohn Marino     actual_gdb_prompt = xstrdup (new_prompt);
2815796c8dcSSimon Schubert 
2825796c8dcSSimon Schubert   if (async_command_editing_p)
2835796c8dcSSimon Schubert     {
2845796c8dcSSimon Schubert       rl_callback_handler_remove ();
285a45ae5f8SJohn Marino       rl_callback_handler_install (actual_gdb_prompt, input_handler);
2865796c8dcSSimon Schubert     }
287c50c785cSJohn Marino   /* new_prompt at this point can be the top of the stack or the one
288c50c785cSJohn Marino      passed in.  It can't be NULL.  */
289c50c785cSJohn Marino   else
2905796c8dcSSimon Schubert     {
2915796c8dcSSimon Schubert       /* Don't use a _filtered function here.  It causes the assumed
2925796c8dcSSimon Schubert          character position to be off, since the newline we read from
2935796c8dcSSimon Schubert          the user is not accounted for.  */
294a45ae5f8SJohn Marino       fputs_unfiltered (actual_gdb_prompt, gdb_stdout);
2955796c8dcSSimon Schubert       gdb_flush (gdb_stdout);
2965796c8dcSSimon Schubert     }
297a45ae5f8SJohn Marino 
298a45ae5f8SJohn Marino   do_cleanups (old_chain);
2995796c8dcSSimon Schubert }
3005796c8dcSSimon Schubert 
301a45ae5f8SJohn Marino /* Return the top level prompt, as specified by "set prompt", possibly
302a45ae5f8SJohn Marino    overriden by the python gdb.prompt_hook hook, and then composed
303a45ae5f8SJohn Marino    with the prompt prefix and suffix (annotations).  The caller is
304a45ae5f8SJohn Marino    responsible for freeing the returned string.  */
3055796c8dcSSimon Schubert 
306a45ae5f8SJohn Marino static char *
top_level_prompt(void)307a45ae5f8SJohn Marino top_level_prompt (void)
3085796c8dcSSimon Schubert {
309a45ae5f8SJohn Marino   char *prefix;
310a45ae5f8SJohn Marino   char *prompt = NULL;
311a45ae5f8SJohn Marino   char *suffix;
312a45ae5f8SJohn Marino   char *composed_prompt;
313a45ae5f8SJohn Marino   size_t prompt_length;
3145796c8dcSSimon Schubert 
315a45ae5f8SJohn Marino   /* Give observers a chance of changing the prompt.  E.g., the python
316a45ae5f8SJohn Marino      `gdb.prompt_hook' is installed as an observer.  */
317a45ae5f8SJohn Marino   observer_notify_before_prompt (get_prompt ());
318a45ae5f8SJohn Marino 
319a45ae5f8SJohn Marino   prompt = xstrdup (get_prompt ());
320a45ae5f8SJohn Marino 
321a45ae5f8SJohn Marino   if (annotation_level >= 2)
3225796c8dcSSimon Schubert     {
323a45ae5f8SJohn Marino       /* Prefix needs to have new line at end.  */
3245796c8dcSSimon Schubert       prefix = (char *) alloca (strlen (async_annotation_suffix) + 10);
3255796c8dcSSimon Schubert       strcpy (prefix, "\n\032\032pre-");
3265796c8dcSSimon Schubert       strcat (prefix, async_annotation_suffix);
3275796c8dcSSimon Schubert       strcat (prefix, "\n");
3285796c8dcSSimon Schubert 
329a45ae5f8SJohn Marino       /* Suffix needs to have a new line at end and \032 \032 at
330a45ae5f8SJohn Marino 	 beginning.  */
3315796c8dcSSimon Schubert       suffix = (char *) alloca (strlen (async_annotation_suffix) + 6);
3325796c8dcSSimon Schubert       strcpy (suffix, "\n\032\032");
3335796c8dcSSimon Schubert       strcat (suffix, async_annotation_suffix);
3345796c8dcSSimon Schubert       strcat (suffix, "\n");
3355796c8dcSSimon Schubert     }
3365796c8dcSSimon Schubert   else
3375796c8dcSSimon Schubert     {
338a45ae5f8SJohn Marino       prefix = "";
339a45ae5f8SJohn Marino       suffix = "";
3405796c8dcSSimon Schubert     }
3415796c8dcSSimon Schubert 
342a45ae5f8SJohn Marino   prompt_length = strlen (prefix) + strlen (prompt) + strlen (suffix);
343a45ae5f8SJohn Marino   composed_prompt = xmalloc (prompt_length + 1);
3445796c8dcSSimon Schubert 
345a45ae5f8SJohn Marino   strcpy (composed_prompt, prefix);
346a45ae5f8SJohn Marino   strcat (composed_prompt, prompt);
347a45ae5f8SJohn Marino   strcat (composed_prompt, suffix);
3485796c8dcSSimon Schubert 
349a45ae5f8SJohn Marino   xfree (prompt);
3505796c8dcSSimon Schubert 
351a45ae5f8SJohn Marino   return composed_prompt;
3525796c8dcSSimon Schubert }
3535796c8dcSSimon Schubert 
3545796c8dcSSimon Schubert /* When there is an event ready on the stdin file desriptor, instead
3555796c8dcSSimon Schubert    of calling readline directly throught the callback function, or
3565796c8dcSSimon Schubert    instead of calling gdb_readline2, give gdb a chance to detect
3575796c8dcSSimon Schubert    errors and do something.  */
3585796c8dcSSimon Schubert void
stdin_event_handler(int error,gdb_client_data client_data)3595796c8dcSSimon Schubert stdin_event_handler (int error, gdb_client_data client_data)
3605796c8dcSSimon Schubert {
3615796c8dcSSimon Schubert   if (error)
3625796c8dcSSimon Schubert     {
3635796c8dcSSimon Schubert       printf_unfiltered (_("error detected on stdin\n"));
3645796c8dcSSimon Schubert       delete_file_handler (input_fd);
3655796c8dcSSimon Schubert       discard_all_continuations ();
3665796c8dcSSimon Schubert       discard_all_intermediate_continuations ();
3675796c8dcSSimon Schubert       /* If stdin died, we may as well kill gdb.  */
3685796c8dcSSimon Schubert       quit_command ((char *) 0, stdin == instream);
3695796c8dcSSimon Schubert     }
3705796c8dcSSimon Schubert   else
3715796c8dcSSimon Schubert     (*call_readline) (client_data);
3725796c8dcSSimon Schubert }
3735796c8dcSSimon Schubert 
3745796c8dcSSimon Schubert /* Re-enable stdin after the end of an execution command in
3755796c8dcSSimon Schubert    synchronous mode, or after an error from the target, and we aborted
3765796c8dcSSimon Schubert    the exec operation.  */
3775796c8dcSSimon Schubert 
3785796c8dcSSimon Schubert void
async_enable_stdin(void)3795796c8dcSSimon Schubert async_enable_stdin (void)
3805796c8dcSSimon Schubert {
3815796c8dcSSimon Schubert   if (sync_execution)
3825796c8dcSSimon Schubert     {
383c50c785cSJohn Marino       /* See NOTE in async_disable_stdin().  */
3845796c8dcSSimon Schubert       /* FIXME: cagney/1999-09-27: Call this before clearing
3855796c8dcSSimon Schubert 	 sync_execution.  Current target_terminal_ours() implementations
3865796c8dcSSimon Schubert 	 check for sync_execution before switching the terminal.  */
3875796c8dcSSimon Schubert       target_terminal_ours ();
3885796c8dcSSimon Schubert       sync_execution = 0;
3895796c8dcSSimon Schubert     }
3905796c8dcSSimon Schubert }
3915796c8dcSSimon Schubert 
3925796c8dcSSimon Schubert /* Disable reads from stdin (the console) marking the command as
3935796c8dcSSimon Schubert    synchronous.  */
3945796c8dcSSimon Schubert 
3955796c8dcSSimon Schubert void
async_disable_stdin(void)3965796c8dcSSimon Schubert async_disable_stdin (void)
3975796c8dcSSimon Schubert {
3985796c8dcSSimon Schubert   sync_execution = 1;
3995796c8dcSSimon Schubert }
4005796c8dcSSimon Schubert 
4015796c8dcSSimon Schubert 
4025796c8dcSSimon Schubert /* Handles a gdb command.  This function is called by
4035796c8dcSSimon Schubert    command_line_handler, which has processed one or more input lines
4045796c8dcSSimon Schubert    into COMMAND.  */
4055796c8dcSSimon Schubert /* NOTE: 1999-04-30 This is the asynchronous version of the command_loop
4065796c8dcSSimon Schubert    function.  The command_loop function will be obsolete when we
4075796c8dcSSimon Schubert    switch to use the event loop at every execution of gdb.  */
4085796c8dcSSimon Schubert static void
command_handler(char * command)4095796c8dcSSimon Schubert command_handler (char *command)
4105796c8dcSSimon Schubert {
4115796c8dcSSimon Schubert   int stdin_is_tty = ISATTY (stdin);
412cf7f2e2dSJohn Marino   struct cleanup *stat_chain;
4135796c8dcSSimon Schubert 
414*ef5ccd6cSJohn Marino   clear_quit_flag ();
4155796c8dcSSimon Schubert   if (instream == stdin && stdin_is_tty)
4165796c8dcSSimon Schubert     reinitialize_more_filter ();
4175796c8dcSSimon Schubert 
418c50c785cSJohn Marino   /* If readline returned a NULL command, it means that the connection
419c50c785cSJohn Marino      with the terminal is gone.  This happens at the end of a
420c50c785cSJohn Marino      testsuite run, after Expect has hung up but GDB is still alive.
421c50c785cSJohn Marino      In such a case, we just quit gdb killing the inferior program
422c50c785cSJohn Marino      too.  */
4235796c8dcSSimon Schubert   if (command == 0)
4245796c8dcSSimon Schubert     {
4255796c8dcSSimon Schubert       printf_unfiltered ("quit\n");
4265796c8dcSSimon Schubert       execute_command ("quit", stdin == instream);
4275796c8dcSSimon Schubert     }
4285796c8dcSSimon Schubert 
429cf7f2e2dSJohn Marino   stat_chain = make_command_stats_cleanup (1);
4305796c8dcSSimon Schubert 
4315796c8dcSSimon Schubert   execute_command (command, instream == stdin);
4325796c8dcSSimon Schubert 
4335796c8dcSSimon Schubert   /* Do any commands attached to breakpoint we stopped at.  */
4345796c8dcSSimon Schubert   bpstat_do_actions ();
4355796c8dcSSimon Schubert 
436cf7f2e2dSJohn Marino   do_cleanups (stat_chain);
4375796c8dcSSimon Schubert }
4385796c8dcSSimon Schubert 
4395796c8dcSSimon Schubert /* Handle a complete line of input.  This is called by the callback
440c50c785cSJohn Marino    mechanism within the readline library.  Deal with incomplete
441c50c785cSJohn Marino    commands as well, by saving the partial input in a global
442c50c785cSJohn Marino    buffer.  */
4435796c8dcSSimon Schubert 
4445796c8dcSSimon Schubert /* NOTE: 1999-04-30 This is the asynchronous version of the
445c50c785cSJohn Marino    command_line_input function; command_line_input will become
4465796c8dcSSimon Schubert    obsolete once we use the event loop as the default mechanism in
4475796c8dcSSimon Schubert    GDB.  */
4485796c8dcSSimon Schubert static void
command_line_handler(char * rl)4495796c8dcSSimon Schubert command_line_handler (char *rl)
4505796c8dcSSimon Schubert {
4515796c8dcSSimon Schubert   static char *linebuffer = 0;
4525796c8dcSSimon Schubert   static unsigned linelength = 0;
4535796c8dcSSimon Schubert   char *p;
4545796c8dcSSimon Schubert   char *p1;
4555796c8dcSSimon Schubert   char *nline;
4565796c8dcSSimon Schubert   int repeat = (instream == stdin);
4575796c8dcSSimon Schubert 
4585796c8dcSSimon Schubert   if (annotation_level > 1 && instream == stdin)
4595796c8dcSSimon Schubert     {
4605796c8dcSSimon Schubert       printf_unfiltered (("\n\032\032post-"));
4615796c8dcSSimon Schubert       puts_unfiltered (async_annotation_suffix);
4625796c8dcSSimon Schubert       printf_unfiltered (("\n"));
4635796c8dcSSimon Schubert     }
4645796c8dcSSimon Schubert 
4655796c8dcSSimon Schubert   if (linebuffer == 0)
4665796c8dcSSimon Schubert     {
4675796c8dcSSimon Schubert       linelength = 80;
4685796c8dcSSimon Schubert       linebuffer = (char *) xmalloc (linelength);
4695796c8dcSSimon Schubert     }
4705796c8dcSSimon Schubert 
4715796c8dcSSimon Schubert   p = linebuffer;
4725796c8dcSSimon Schubert 
4735796c8dcSSimon Schubert   if (more_to_come)
4745796c8dcSSimon Schubert     {
4755796c8dcSSimon Schubert       strcpy (linebuffer, readline_input_state.linebuffer);
4765796c8dcSSimon Schubert       p = readline_input_state.linebuffer_ptr;
4775796c8dcSSimon Schubert       xfree (readline_input_state.linebuffer);
4785796c8dcSSimon Schubert       more_to_come = 0;
4795796c8dcSSimon Schubert     }
4805796c8dcSSimon Schubert 
4815796c8dcSSimon Schubert #ifdef STOP_SIGNAL
4825796c8dcSSimon Schubert   if (job_control)
4835796c8dcSSimon Schubert     signal (STOP_SIGNAL, handle_stop_sig);
4845796c8dcSSimon Schubert #endif
4855796c8dcSSimon Schubert 
4865796c8dcSSimon Schubert   /* Make sure that all output has been output.  Some machines may let
487c50c785cSJohn Marino      you get away with leaving out some of the gdb_flush, but not
488c50c785cSJohn Marino      all.  */
4895796c8dcSSimon Schubert   wrap_here ("");
4905796c8dcSSimon Schubert   gdb_flush (gdb_stdout);
4915796c8dcSSimon Schubert   gdb_flush (gdb_stderr);
4925796c8dcSSimon Schubert 
4935796c8dcSSimon Schubert   if (source_file_name != NULL)
4945796c8dcSSimon Schubert     ++source_line_number;
4955796c8dcSSimon Schubert 
4965796c8dcSSimon Schubert   /* If we are in this case, then command_handler will call quit
4975796c8dcSSimon Schubert      and exit from gdb.  */
4985796c8dcSSimon Schubert   if (!rl || rl == (char *) EOF)
4995796c8dcSSimon Schubert     {
5005796c8dcSSimon Schubert       command_handler (0);
5015796c8dcSSimon Schubert       return;			/* Lint.  */
5025796c8dcSSimon Schubert     }
5035796c8dcSSimon Schubert   if (strlen (rl) + 1 + (p - linebuffer) > linelength)
5045796c8dcSSimon Schubert     {
5055796c8dcSSimon Schubert       linelength = strlen (rl) + 1 + (p - linebuffer);
5065796c8dcSSimon Schubert       nline = (char *) xrealloc (linebuffer, linelength);
5075796c8dcSSimon Schubert       p += nline - linebuffer;
5085796c8dcSSimon Schubert       linebuffer = nline;
5095796c8dcSSimon Schubert     }
5105796c8dcSSimon Schubert   p1 = rl;
5115796c8dcSSimon Schubert   /* Copy line.  Don't copy null at end.  (Leaves line alone
512c50c785cSJohn Marino      if this was just a newline).  */
5135796c8dcSSimon Schubert   while (*p1)
5145796c8dcSSimon Schubert     *p++ = *p1++;
5155796c8dcSSimon Schubert 
5165796c8dcSSimon Schubert   xfree (rl);			/* Allocated in readline.  */
5175796c8dcSSimon Schubert 
5185796c8dcSSimon Schubert   if (p > linebuffer && *(p - 1) == '\\')
5195796c8dcSSimon Schubert     {
5205796c8dcSSimon Schubert       *p = '\0';
5215796c8dcSSimon Schubert       p--;			/* Put on top of '\'.  */
5225796c8dcSSimon Schubert 
5235796c8dcSSimon Schubert       readline_input_state.linebuffer = xstrdup (linebuffer);
5245796c8dcSSimon Schubert       readline_input_state.linebuffer_ptr = p;
5255796c8dcSSimon Schubert 
5265796c8dcSSimon Schubert       /* We will not invoke a execute_command if there is more
5275796c8dcSSimon Schubert 	 input expected to complete the command.  So, we need to
5285796c8dcSSimon Schubert 	 print an empty prompt here.  */
5295796c8dcSSimon Schubert       more_to_come = 1;
530a45ae5f8SJohn Marino       display_gdb_prompt ("");
5315796c8dcSSimon Schubert       return;
5325796c8dcSSimon Schubert     }
5335796c8dcSSimon Schubert 
5345796c8dcSSimon Schubert #ifdef STOP_SIGNAL
5355796c8dcSSimon Schubert   if (job_control)
5365796c8dcSSimon Schubert     signal (STOP_SIGNAL, SIG_DFL);
5375796c8dcSSimon Schubert #endif
5385796c8dcSSimon Schubert 
5395796c8dcSSimon Schubert #define SERVER_COMMAND_LENGTH 7
5405796c8dcSSimon Schubert   server_command =
5415796c8dcSSimon Schubert     (p - linebuffer > SERVER_COMMAND_LENGTH)
5425796c8dcSSimon Schubert     && strncmp (linebuffer, "server ", SERVER_COMMAND_LENGTH) == 0;
5435796c8dcSSimon Schubert   if (server_command)
5445796c8dcSSimon Schubert     {
5455796c8dcSSimon Schubert       /* Note that we don't set `line'.  Between this and the check in
5465796c8dcSSimon Schubert          dont_repeat, this insures that repeating will still do the
5475796c8dcSSimon Schubert          right thing.  */
5485796c8dcSSimon Schubert       *p = '\0';
5495796c8dcSSimon Schubert       command_handler (linebuffer + SERVER_COMMAND_LENGTH);
5505796c8dcSSimon Schubert       display_gdb_prompt (0);
5515796c8dcSSimon Schubert       return;
5525796c8dcSSimon Schubert     }
5535796c8dcSSimon Schubert 
5545796c8dcSSimon Schubert   /* Do history expansion if that is wished.  */
5555796c8dcSSimon Schubert   if (history_expansion_p && instream == stdin
5565796c8dcSSimon Schubert       && ISATTY (instream))
5575796c8dcSSimon Schubert     {
5585796c8dcSSimon Schubert       char *history_value;
5595796c8dcSSimon Schubert       int expanded;
5605796c8dcSSimon Schubert 
5615796c8dcSSimon Schubert       *p = '\0';		/* Insert null now.  */
5625796c8dcSSimon Schubert       expanded = history_expand (linebuffer, &history_value);
5635796c8dcSSimon Schubert       if (expanded)
5645796c8dcSSimon Schubert 	{
5655796c8dcSSimon Schubert 	  /* Print the changes.  */
5665796c8dcSSimon Schubert 	  printf_unfiltered ("%s\n", history_value);
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert 	  /* If there was an error, call this function again.  */
5695796c8dcSSimon Schubert 	  if (expanded < 0)
5705796c8dcSSimon Schubert 	    {
5715796c8dcSSimon Schubert 	      xfree (history_value);
5725796c8dcSSimon Schubert 	      return;
5735796c8dcSSimon Schubert 	    }
5745796c8dcSSimon Schubert 	  if (strlen (history_value) > linelength)
5755796c8dcSSimon Schubert 	    {
5765796c8dcSSimon Schubert 	      linelength = strlen (history_value) + 1;
5775796c8dcSSimon Schubert 	      linebuffer = (char *) xrealloc (linebuffer, linelength);
5785796c8dcSSimon Schubert 	    }
5795796c8dcSSimon Schubert 	  strcpy (linebuffer, history_value);
5805796c8dcSSimon Schubert 	  p = linebuffer + strlen (linebuffer);
5815796c8dcSSimon Schubert 	}
5825796c8dcSSimon Schubert       xfree (history_value);
5835796c8dcSSimon Schubert     }
5845796c8dcSSimon Schubert 
585c50c785cSJohn Marino   /* If we just got an empty line, and that is supposed to repeat the
586c50c785cSJohn Marino      previous command, return the value in the global buffer.  */
5875796c8dcSSimon Schubert   if (repeat && p == linebuffer && *p != '\\')
5885796c8dcSSimon Schubert     {
589a45ae5f8SJohn Marino       command_handler (saved_command_line);
5905796c8dcSSimon Schubert       display_gdb_prompt (0);
5915796c8dcSSimon Schubert       return;
5925796c8dcSSimon Schubert     }
5935796c8dcSSimon Schubert 
5945796c8dcSSimon Schubert   for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
5955796c8dcSSimon Schubert   if (repeat && !*p1)
5965796c8dcSSimon Schubert     {
597a45ae5f8SJohn Marino       command_handler (saved_command_line);
5985796c8dcSSimon Schubert       display_gdb_prompt (0);
5995796c8dcSSimon Schubert       return;
6005796c8dcSSimon Schubert     }
6015796c8dcSSimon Schubert 
6025796c8dcSSimon Schubert   *p = 0;
6035796c8dcSSimon Schubert 
6045796c8dcSSimon Schubert   /* Add line to history if appropriate.  */
6055796c8dcSSimon Schubert   if (instream == stdin
6065796c8dcSSimon Schubert       && ISATTY (stdin) && *linebuffer)
6075796c8dcSSimon Schubert     add_history (linebuffer);
6085796c8dcSSimon Schubert 
6095796c8dcSSimon Schubert   /* Note: lines consisting solely of comments are added to the command
6105796c8dcSSimon Schubert      history.  This is useful when you type a command, and then
6115796c8dcSSimon Schubert      realize you don't want to execute it quite yet.  You can comment
6125796c8dcSSimon Schubert      out the command and then later fetch it from the value history
6135796c8dcSSimon Schubert      and remove the '#'.  The kill ring is probably better, but some
6145796c8dcSSimon Schubert      people are in the habit of commenting things out.  */
6155796c8dcSSimon Schubert   if (*p1 == '#')
6165796c8dcSSimon Schubert     *p1 = '\0';			/* Found a comment.  */
6175796c8dcSSimon Schubert 
6185796c8dcSSimon Schubert   /* Save into global buffer if appropriate.  */
6195796c8dcSSimon Schubert   if (repeat)
6205796c8dcSSimon Schubert     {
621a45ae5f8SJohn Marino       if (linelength > saved_command_line_size)
6225796c8dcSSimon Schubert 	{
623a45ae5f8SJohn Marino 	  saved_command_line = xrealloc (saved_command_line, linelength);
624a45ae5f8SJohn Marino 	  saved_command_line_size = linelength;
6255796c8dcSSimon Schubert 	}
626a45ae5f8SJohn Marino       strcpy (saved_command_line, linebuffer);
6275796c8dcSSimon Schubert       if (!more_to_come)
6285796c8dcSSimon Schubert 	{
629a45ae5f8SJohn Marino 	  command_handler (saved_command_line);
6305796c8dcSSimon Schubert 	  display_gdb_prompt (0);
6315796c8dcSSimon Schubert 	}
6325796c8dcSSimon Schubert       return;
6335796c8dcSSimon Schubert     }
6345796c8dcSSimon Schubert 
6355796c8dcSSimon Schubert   command_handler (linebuffer);
6365796c8dcSSimon Schubert   display_gdb_prompt (0);
6375796c8dcSSimon Schubert   return;
6385796c8dcSSimon Schubert }
6395796c8dcSSimon Schubert 
6405796c8dcSSimon Schubert /* Does reading of input from terminal w/o the editing features
6415796c8dcSSimon Schubert    provided by the readline library.  */
6425796c8dcSSimon Schubert 
643c50c785cSJohn Marino /* NOTE: 1999-04-30 Asynchronous version of gdb_readline; gdb_readline
6445796c8dcSSimon Schubert    will become obsolete when the event loop is made the default
6455796c8dcSSimon Schubert    execution for gdb.  */
6465796c8dcSSimon Schubert void
gdb_readline2(gdb_client_data client_data)6475796c8dcSSimon Schubert gdb_readline2 (gdb_client_data client_data)
6485796c8dcSSimon Schubert {
6495796c8dcSSimon Schubert   int c;
6505796c8dcSSimon Schubert   char *result;
6515796c8dcSSimon Schubert   int input_index = 0;
6525796c8dcSSimon Schubert   int result_size = 80;
6535796c8dcSSimon Schubert   static int done_once = 0;
6545796c8dcSSimon Schubert 
6555796c8dcSSimon Schubert   /* Unbuffer the input stream, so that, later on, the calls to fgetc
6565796c8dcSSimon Schubert      fetch only one char at the time from the stream.  The fgetc's will
6575796c8dcSSimon Schubert      get up to the first newline, but there may be more chars in the
6585796c8dcSSimon Schubert      stream after '\n'.  If we buffer the input and fgetc drains the
6595796c8dcSSimon Schubert      stream, getting stuff beyond the newline as well, a select, done
6605796c8dcSSimon Schubert      afterwards will not trigger.  */
6615796c8dcSSimon Schubert   if (!done_once && !ISATTY (instream))
6625796c8dcSSimon Schubert     {
6635796c8dcSSimon Schubert       setbuf (instream, NULL);
6645796c8dcSSimon Schubert       done_once = 1;
6655796c8dcSSimon Schubert     }
6665796c8dcSSimon Schubert 
6675796c8dcSSimon Schubert   result = (char *) xmalloc (result_size);
6685796c8dcSSimon Schubert 
6695796c8dcSSimon Schubert   /* We still need the while loop here, even though it would seem
6705796c8dcSSimon Schubert      obvious to invoke gdb_readline2 at every character entered.  If
6715796c8dcSSimon Schubert      not using the readline library, the terminal is in cooked mode,
6725796c8dcSSimon Schubert      which sends the characters all at once.  Poll will notice that the
6735796c8dcSSimon Schubert      input fd has changed state only after enter is pressed.  At this
6745796c8dcSSimon Schubert      point we still need to fetch all the chars entered.  */
6755796c8dcSSimon Schubert 
6765796c8dcSSimon Schubert   while (1)
6775796c8dcSSimon Schubert     {
6785796c8dcSSimon Schubert       /* Read from stdin if we are executing a user defined command.
6795796c8dcSSimon Schubert          This is the right thing for prompt_for_continue, at least.  */
6805796c8dcSSimon Schubert       c = fgetc (instream ? instream : stdin);
6815796c8dcSSimon Schubert 
6825796c8dcSSimon Schubert       if (c == EOF)
6835796c8dcSSimon Schubert 	{
6845796c8dcSSimon Schubert 	  if (input_index > 0)
685c50c785cSJohn Marino 	    /* The last line does not end with a newline.  Return it,
686c50c785cSJohn Marino 	       and if we are called again fgetc will still return EOF
687c50c785cSJohn Marino 	       and we'll return NULL then.  */
6885796c8dcSSimon Schubert 	    break;
6895796c8dcSSimon Schubert 	  xfree (result);
6905796c8dcSSimon Schubert 	  (*input_handler) (0);
6915796c8dcSSimon Schubert 	  return;
6925796c8dcSSimon Schubert 	}
6935796c8dcSSimon Schubert 
6945796c8dcSSimon Schubert       if (c == '\n')
6955796c8dcSSimon Schubert 	{
6965796c8dcSSimon Schubert 	  if (input_index > 0 && result[input_index - 1] == '\r')
6975796c8dcSSimon Schubert 	    input_index--;
6985796c8dcSSimon Schubert 	  break;
6995796c8dcSSimon Schubert 	}
7005796c8dcSSimon Schubert 
7015796c8dcSSimon Schubert       result[input_index++] = c;
7025796c8dcSSimon Schubert       while (input_index >= result_size)
7035796c8dcSSimon Schubert 	{
7045796c8dcSSimon Schubert 	  result_size *= 2;
7055796c8dcSSimon Schubert 	  result = (char *) xrealloc (result, result_size);
7065796c8dcSSimon Schubert 	}
7075796c8dcSSimon Schubert     }
7085796c8dcSSimon Schubert 
7095796c8dcSSimon Schubert   result[input_index++] = '\0';
7105796c8dcSSimon Schubert   (*input_handler) (result);
7115796c8dcSSimon Schubert }
7125796c8dcSSimon Schubert 
7135796c8dcSSimon Schubert 
7145796c8dcSSimon Schubert /* Initialization of signal handlers and tokens.  There is a function
7155796c8dcSSimon Schubert    handle_sig* for each of the signals GDB cares about.  Specifically:
7165796c8dcSSimon Schubert    SIGINT, SIGFPE, SIGQUIT, SIGTSTP, SIGHUP, SIGWINCH.  These
7175796c8dcSSimon Schubert    functions are the actual signal handlers associated to the signals
7185796c8dcSSimon Schubert    via calls to signal().  The only job for these functions is to
7195796c8dcSSimon Schubert    enqueue the appropriate event/procedure with the event loop.  Such
7205796c8dcSSimon Schubert    procedures are the old signal handlers.  The event loop will take
7215796c8dcSSimon Schubert    care of invoking the queued procedures to perform the usual tasks
7225796c8dcSSimon Schubert    associated with the reception of the signal.  */
7235796c8dcSSimon Schubert /* NOTE: 1999-04-30 This is the asynchronous version of init_signals.
7245796c8dcSSimon Schubert    init_signals will become obsolete as we move to have to event loop
7255796c8dcSSimon Schubert    as the default for gdb.  */
7265796c8dcSSimon Schubert void
async_init_signals(void)7275796c8dcSSimon Schubert async_init_signals (void)
7285796c8dcSSimon Schubert {
7295796c8dcSSimon Schubert   signal (SIGINT, handle_sigint);
7305796c8dcSSimon Schubert   sigint_token =
7315796c8dcSSimon Schubert     create_async_signal_handler (async_request_quit, NULL);
7325796c8dcSSimon Schubert   signal (SIGTERM, handle_sigterm);
7335796c8dcSSimon Schubert 
7345796c8dcSSimon Schubert   /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
7355796c8dcSSimon Schubert      to the inferior and breakpoints will be ignored.  */
7365796c8dcSSimon Schubert #ifdef SIGTRAP
7375796c8dcSSimon Schubert   signal (SIGTRAP, SIG_DFL);
7385796c8dcSSimon Schubert #endif
7395796c8dcSSimon Schubert 
7405796c8dcSSimon Schubert #ifdef SIGQUIT
7415796c8dcSSimon Schubert   /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
7425796c8dcSSimon Schubert      passed to the inferior, which we don't want.  It would be
7435796c8dcSSimon Schubert      possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
7445796c8dcSSimon Schubert      on BSD4.3 systems using vfork, that can affect the
7455796c8dcSSimon Schubert      GDB process as well as the inferior (the signal handling tables
7465796c8dcSSimon Schubert      might be in memory, shared between the two).  Since we establish
7475796c8dcSSimon Schubert      a handler for SIGQUIT, when we call exec it will set the signal
7485796c8dcSSimon Schubert      to SIG_DFL for us.  */
7495796c8dcSSimon Schubert   signal (SIGQUIT, handle_sigquit);
7505796c8dcSSimon Schubert   sigquit_token =
7515796c8dcSSimon Schubert     create_async_signal_handler (async_do_nothing, NULL);
7525796c8dcSSimon Schubert #endif
7535796c8dcSSimon Schubert #ifdef SIGHUP
7545796c8dcSSimon Schubert   if (signal (SIGHUP, handle_sighup) != SIG_IGN)
7555796c8dcSSimon Schubert     sighup_token =
7565796c8dcSSimon Schubert       create_async_signal_handler (async_disconnect, NULL);
7575796c8dcSSimon Schubert   else
7585796c8dcSSimon Schubert     sighup_token =
7595796c8dcSSimon Schubert       create_async_signal_handler (async_do_nothing, NULL);
7605796c8dcSSimon Schubert #endif
7615796c8dcSSimon Schubert   signal (SIGFPE, handle_sigfpe);
7625796c8dcSSimon Schubert   sigfpe_token =
7635796c8dcSSimon Schubert     create_async_signal_handler (async_float_handler, NULL);
7645796c8dcSSimon Schubert 
7655796c8dcSSimon Schubert #ifdef STOP_SIGNAL
7665796c8dcSSimon Schubert   sigtstp_token =
7675796c8dcSSimon Schubert     create_async_signal_handler (async_stop_sig, NULL);
7685796c8dcSSimon Schubert #endif
7695796c8dcSSimon Schubert 
7705796c8dcSSimon Schubert }
7715796c8dcSSimon Schubert 
7725796c8dcSSimon Schubert /* Tell the event loop what to do if SIGINT is received.
7735796c8dcSSimon Schubert    See event-signal.c.  */
7745796c8dcSSimon Schubert void
handle_sigint(int sig)7755796c8dcSSimon Schubert handle_sigint (int sig)
7765796c8dcSSimon Schubert {
7775796c8dcSSimon Schubert   signal (sig, handle_sigint);
7785796c8dcSSimon Schubert 
7795796c8dcSSimon Schubert   /* We could be running in a loop reading in symfiles or something so
7805796c8dcSSimon Schubert      it may be quite a while before we get back to the event loop.  So
7815796c8dcSSimon Schubert      set quit_flag to 1 here.  Then if QUIT is called before we get to
7825796c8dcSSimon Schubert      the event loop, we will unwind as expected.  */
7835796c8dcSSimon Schubert 
784*ef5ccd6cSJohn Marino   set_quit_flag ();
7855796c8dcSSimon Schubert 
7865796c8dcSSimon Schubert   /* If immediate_quit is set, we go ahead and process the SIGINT right
7875796c8dcSSimon Schubert      away, even if we usually would defer this to the event loop.  The
7885796c8dcSSimon Schubert      assumption here is that it is safe to process ^C immediately if
7895796c8dcSSimon Schubert      immediate_quit is set.  If we didn't, SIGINT would be really
7905796c8dcSSimon Schubert      processed only the next time through the event loop.  To get to
7915796c8dcSSimon Schubert      that point, though, the command that we want to interrupt needs to
7925796c8dcSSimon Schubert      finish first, which is unacceptable.  If immediate quit is not set,
7935796c8dcSSimon Schubert      we process SIGINT the next time through the loop, which is fine.  */
7945796c8dcSSimon Schubert   gdb_call_async_signal_handler (sigint_token, immediate_quit);
7955796c8dcSSimon Schubert }
7965796c8dcSSimon Schubert 
7975796c8dcSSimon Schubert /* Quit GDB if SIGTERM is received.
7985796c8dcSSimon Schubert    GDB would quit anyway, but this way it will clean up properly.  */
7995796c8dcSSimon Schubert void
handle_sigterm(int sig)8005796c8dcSSimon Schubert handle_sigterm (int sig)
8015796c8dcSSimon Schubert {
8025796c8dcSSimon Schubert   signal (sig, handle_sigterm);
8035796c8dcSSimon Schubert   quit_force ((char *) 0, stdin == instream);
8045796c8dcSSimon Schubert }
8055796c8dcSSimon Schubert 
8065796c8dcSSimon Schubert /* Do the quit.  All the checks have been done by the caller.  */
8075796c8dcSSimon Schubert void
async_request_quit(gdb_client_data arg)8085796c8dcSSimon Schubert async_request_quit (gdb_client_data arg)
8095796c8dcSSimon Schubert {
8105796c8dcSSimon Schubert   /* If the quit_flag has gotten reset back to 0 by the time we get
8115796c8dcSSimon Schubert      back here, that means that an exception was thrown to unwind the
8125796c8dcSSimon Schubert      current command before we got back to the event loop.  So there
813*ef5ccd6cSJohn Marino      is no reason to call quit again here.  */
8145796c8dcSSimon Schubert 
815*ef5ccd6cSJohn Marino   if (check_quit_flag ())
8165796c8dcSSimon Schubert     quit ();
8175796c8dcSSimon Schubert }
8185796c8dcSSimon Schubert 
8195796c8dcSSimon Schubert #ifdef SIGQUIT
8205796c8dcSSimon Schubert /* Tell the event loop what to do if SIGQUIT is received.
8215796c8dcSSimon Schubert    See event-signal.c.  */
8225796c8dcSSimon Schubert static void
handle_sigquit(int sig)8235796c8dcSSimon Schubert handle_sigquit (int sig)
8245796c8dcSSimon Schubert {
825*ef5ccd6cSJohn Marino   mark_async_signal_handler (sigquit_token);
8265796c8dcSSimon Schubert   signal (sig, handle_sigquit);
8275796c8dcSSimon Schubert }
8285796c8dcSSimon Schubert #endif
8295796c8dcSSimon Schubert 
8305796c8dcSSimon Schubert #if defined (SIGQUIT) || defined (SIGHUP)
8315796c8dcSSimon Schubert /* Called by the event loop in response to a SIGQUIT or an
8325796c8dcSSimon Schubert    ignored SIGHUP.  */
8335796c8dcSSimon Schubert static void
async_do_nothing(gdb_client_data arg)8345796c8dcSSimon Schubert async_do_nothing (gdb_client_data arg)
8355796c8dcSSimon Schubert {
8365796c8dcSSimon Schubert   /* Empty function body.  */
8375796c8dcSSimon Schubert }
8385796c8dcSSimon Schubert #endif
8395796c8dcSSimon Schubert 
8405796c8dcSSimon Schubert #ifdef SIGHUP
8415796c8dcSSimon Schubert /* Tell the event loop what to do if SIGHUP is received.
8425796c8dcSSimon Schubert    See event-signal.c.  */
8435796c8dcSSimon Schubert static void
handle_sighup(int sig)8445796c8dcSSimon Schubert handle_sighup (int sig)
8455796c8dcSSimon Schubert {
846*ef5ccd6cSJohn Marino   mark_async_signal_handler (sighup_token);
8475796c8dcSSimon Schubert   signal (sig, handle_sighup);
8485796c8dcSSimon Schubert }
8495796c8dcSSimon Schubert 
850c50c785cSJohn Marino /* Called by the event loop to process a SIGHUP.  */
8515796c8dcSSimon Schubert static void
async_disconnect(gdb_client_data arg)8525796c8dcSSimon Schubert async_disconnect (gdb_client_data arg)
8535796c8dcSSimon Schubert {
854a45ae5f8SJohn Marino   volatile struct gdb_exception exception;
855a45ae5f8SJohn Marino 
856a45ae5f8SJohn Marino   TRY_CATCH (exception, RETURN_MASK_ALL)
857a45ae5f8SJohn Marino     {
858a45ae5f8SJohn Marino       quit_cover ();
859a45ae5f8SJohn Marino     }
860a45ae5f8SJohn Marino 
861a45ae5f8SJohn Marino   if (exception.reason < 0)
862a45ae5f8SJohn Marino     {
863a45ae5f8SJohn Marino       fputs_filtered ("Could not kill the program being debugged",
864a45ae5f8SJohn Marino 		      gdb_stderr);
865a45ae5f8SJohn Marino       exception_print (gdb_stderr, exception);
866a45ae5f8SJohn Marino     }
867a45ae5f8SJohn Marino 
868a45ae5f8SJohn Marino   TRY_CATCH (exception, RETURN_MASK_ALL)
869a45ae5f8SJohn Marino     {
870a45ae5f8SJohn Marino       pop_all_targets (1);
871a45ae5f8SJohn Marino     }
872a45ae5f8SJohn Marino 
8735796c8dcSSimon Schubert   signal (SIGHUP, SIG_DFL);	/*FIXME: ???????????  */
8745796c8dcSSimon Schubert   raise (SIGHUP);
8755796c8dcSSimon Schubert }
8765796c8dcSSimon Schubert #endif
8775796c8dcSSimon Schubert 
8785796c8dcSSimon Schubert #ifdef STOP_SIGNAL
8795796c8dcSSimon Schubert void
handle_stop_sig(int sig)8805796c8dcSSimon Schubert handle_stop_sig (int sig)
8815796c8dcSSimon Schubert {
882*ef5ccd6cSJohn Marino   mark_async_signal_handler (sigtstp_token);
8835796c8dcSSimon Schubert   signal (sig, handle_stop_sig);
8845796c8dcSSimon Schubert }
8855796c8dcSSimon Schubert 
8865796c8dcSSimon Schubert static void
async_stop_sig(gdb_client_data arg)8875796c8dcSSimon Schubert async_stop_sig (gdb_client_data arg)
8885796c8dcSSimon Schubert {
8895796c8dcSSimon Schubert   char *prompt = get_prompt ();
890cf7f2e2dSJohn Marino 
8915796c8dcSSimon Schubert #if STOP_SIGNAL == SIGTSTP
8925796c8dcSSimon Schubert   signal (SIGTSTP, SIG_DFL);
8935796c8dcSSimon Schubert #if HAVE_SIGPROCMASK
8945796c8dcSSimon Schubert   {
8955796c8dcSSimon Schubert     sigset_t zero;
8965796c8dcSSimon Schubert 
8975796c8dcSSimon Schubert     sigemptyset (&zero);
8985796c8dcSSimon Schubert     sigprocmask (SIG_SETMASK, &zero, 0);
8995796c8dcSSimon Schubert   }
9005796c8dcSSimon Schubert #elif HAVE_SIGSETMASK
9015796c8dcSSimon Schubert   sigsetmask (0);
9025796c8dcSSimon Schubert #endif
9035796c8dcSSimon Schubert   raise (SIGTSTP);
9045796c8dcSSimon Schubert   signal (SIGTSTP, handle_stop_sig);
9055796c8dcSSimon Schubert #else
9065796c8dcSSimon Schubert   signal (STOP_SIGNAL, handle_stop_sig);
9075796c8dcSSimon Schubert #endif
9085796c8dcSSimon Schubert   printf_unfiltered ("%s", prompt);
9095796c8dcSSimon Schubert   gdb_flush (gdb_stdout);
9105796c8dcSSimon Schubert 
911c50c785cSJohn Marino   /* Forget about any previous command -- null line now will do
912c50c785cSJohn Marino      nothing.  */
9135796c8dcSSimon Schubert   dont_repeat ();
9145796c8dcSSimon Schubert }
9155796c8dcSSimon Schubert #endif /* STOP_SIGNAL */
9165796c8dcSSimon Schubert 
9175796c8dcSSimon Schubert /* Tell the event loop what to do if SIGFPE is received.
9185796c8dcSSimon Schubert    See event-signal.c.  */
9195796c8dcSSimon Schubert static void
handle_sigfpe(int sig)9205796c8dcSSimon Schubert handle_sigfpe (int sig)
9215796c8dcSSimon Schubert {
922*ef5ccd6cSJohn Marino   mark_async_signal_handler (sigfpe_token);
9235796c8dcSSimon Schubert   signal (sig, handle_sigfpe);
9245796c8dcSSimon Schubert }
9255796c8dcSSimon Schubert 
9265796c8dcSSimon Schubert /* Event loop will call this functin to process a SIGFPE.  */
9275796c8dcSSimon Schubert static void
async_float_handler(gdb_client_data arg)9285796c8dcSSimon Schubert async_float_handler (gdb_client_data arg)
9295796c8dcSSimon Schubert {
9305796c8dcSSimon Schubert   /* This message is based on ANSI C, section 4.7.  Note that integer
9315796c8dcSSimon Schubert      divide by zero causes this, so "float" is a misnomer.  */
9325796c8dcSSimon Schubert   error (_("Erroneous arithmetic operation."));
9335796c8dcSSimon Schubert }
9345796c8dcSSimon Schubert 
9355796c8dcSSimon Schubert 
9365796c8dcSSimon Schubert /* Called by do_setshow_command.  */
9375796c8dcSSimon Schubert void
set_async_editing_command(char * args,int from_tty,struct cmd_list_element * c)938c50c785cSJohn Marino set_async_editing_command (char *args, int from_tty,
939c50c785cSJohn Marino 			   struct cmd_list_element *c)
9405796c8dcSSimon Schubert {
9415796c8dcSSimon Schubert   change_line_handler ();
9425796c8dcSSimon Schubert }
9435796c8dcSSimon Schubert 
9445796c8dcSSimon Schubert /* Set things up for readline to be invoked via the alternate
9455796c8dcSSimon Schubert    interface, i.e. via a callback function (rl_callback_read_char),
9465796c8dcSSimon Schubert    and hook up instream to the event loop.  */
9475796c8dcSSimon Schubert void
gdb_setup_readline(void)9485796c8dcSSimon Schubert gdb_setup_readline (void)
9495796c8dcSSimon Schubert {
9505796c8dcSSimon Schubert   /* This function is a noop for the sync case.  The assumption is
9515796c8dcSSimon Schubert      that the sync setup is ALL done in gdb_init, and we would only
9525796c8dcSSimon Schubert      mess it up here.  The sync stuff should really go away over
9535796c8dcSSimon Schubert      time.  */
9545796c8dcSSimon Schubert   if (!batch_silent)
9555796c8dcSSimon Schubert     gdb_stdout = stdio_fileopen (stdout);
9565796c8dcSSimon Schubert   gdb_stderr = stdio_fileopen (stderr);
9575796c8dcSSimon Schubert   gdb_stdlog = gdb_stderr;  /* for moment */
9585796c8dcSSimon Schubert   gdb_stdtarg = gdb_stderr; /* for moment */
959c50c785cSJohn Marino   gdb_stdtargerr = gdb_stderr; /* for moment */
9605796c8dcSSimon Schubert 
9615796c8dcSSimon Schubert   /* If the input stream is connected to a terminal, turn on
9625796c8dcSSimon Schubert      editing.  */
9635796c8dcSSimon Schubert   if (ISATTY (instream))
9645796c8dcSSimon Schubert     {
9655796c8dcSSimon Schubert       /* Tell gdb that we will be using the readline library.  This
9665796c8dcSSimon Schubert 	 could be overwritten by a command in .gdbinit like 'set
9675796c8dcSSimon Schubert 	 editing on' or 'off'.  */
9685796c8dcSSimon Schubert       async_command_editing_p = 1;
9695796c8dcSSimon Schubert 
9705796c8dcSSimon Schubert       /* When a character is detected on instream by select or poll,
9715796c8dcSSimon Schubert 	 readline will be invoked via this callback function.  */
9725796c8dcSSimon Schubert       call_readline = rl_callback_read_char_wrapper;
9735796c8dcSSimon Schubert     }
9745796c8dcSSimon Schubert   else
9755796c8dcSSimon Schubert     {
9765796c8dcSSimon Schubert       async_command_editing_p = 0;
9775796c8dcSSimon Schubert       call_readline = gdb_readline2;
9785796c8dcSSimon Schubert     }
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert   /* When readline has read an end-of-line character, it passes the
981c50c785cSJohn Marino      complete line to gdb for processing; command_line_handler is the
9825796c8dcSSimon Schubert      function that does this.  */
9835796c8dcSSimon Schubert   input_handler = command_line_handler;
9845796c8dcSSimon Schubert 
9855796c8dcSSimon Schubert   /* Tell readline to use the same input stream that gdb uses.  */
9865796c8dcSSimon Schubert   rl_instream = instream;
9875796c8dcSSimon Schubert 
9885796c8dcSSimon Schubert   /* Get a file descriptor for the input stream, so that we can
9895796c8dcSSimon Schubert      register it with the event loop.  */
9905796c8dcSSimon Schubert   input_fd = fileno (instream);
9915796c8dcSSimon Schubert 
9925796c8dcSSimon Schubert   /* Now we need to create the event sources for the input file
9935796c8dcSSimon Schubert      descriptor.  */
9945796c8dcSSimon Schubert   /* At this point in time, this is the only event source that we
9955796c8dcSSimon Schubert      register with the even loop.  Another source is going to be the
9965796c8dcSSimon Schubert      target program (inferior), but that must be registered only when
9975796c8dcSSimon Schubert      it actually exists (I.e. after we say 'run' or after we connect
9985796c8dcSSimon Schubert      to a remote target.  */
9995796c8dcSSimon Schubert   add_file_handler (input_fd, stdin_event_handler, 0);
10005796c8dcSSimon Schubert }
10015796c8dcSSimon Schubert 
10025796c8dcSSimon Schubert /* Disable command input through the standard CLI channels.  Used in
10035796c8dcSSimon Schubert    the suspend proc for interpreters that use the standard gdb readline
10045796c8dcSSimon Schubert    interface, like the cli & the mi.  */
10055796c8dcSSimon Schubert void
gdb_disable_readline(void)10065796c8dcSSimon Schubert gdb_disable_readline (void)
10075796c8dcSSimon Schubert {
10085796c8dcSSimon Schubert   /* FIXME - It is too heavyweight to delete and remake these every
10095796c8dcSSimon Schubert      time you run an interpreter that needs readline.  It is probably
10105796c8dcSSimon Schubert      better to have the interpreters cache these, which in turn means
10115796c8dcSSimon Schubert      that this needs to be moved into interpreter specific code.  */
10125796c8dcSSimon Schubert 
10135796c8dcSSimon Schubert #if 0
10145796c8dcSSimon Schubert   ui_file_delete (gdb_stdout);
10155796c8dcSSimon Schubert   ui_file_delete (gdb_stderr);
10165796c8dcSSimon Schubert   gdb_stdlog = NULL;
10175796c8dcSSimon Schubert   gdb_stdtarg = NULL;
1018c50c785cSJohn Marino   gdb_stdtargerr = NULL;
10195796c8dcSSimon Schubert #endif
10205796c8dcSSimon Schubert 
10215796c8dcSSimon Schubert   rl_callback_handler_remove ();
10225796c8dcSSimon Schubert   delete_file_handler (input_fd);
10235796c8dcSSimon Schubert }
1024