xref: /openbsd-src/gnu/usr.bin/binutils/gdb/event-top.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
1b725ae77Skettenis /* Top level stuff for GDB, the GNU debugger.
2b725ae77Skettenis    Copyright 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
3b725ae77Skettenis    Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
4b725ae77Skettenis 
5b725ae77Skettenis    This file is part of GDB.
6b725ae77Skettenis 
7b725ae77Skettenis    This program is free software; you can redistribute it and/or modify
8b725ae77Skettenis    it under the terms of the GNU General Public License as published by
9b725ae77Skettenis    the Free Software Foundation; either version 2 of the License, or
10b725ae77Skettenis    (at your option) any later version.
11b725ae77Skettenis 
12b725ae77Skettenis    This program is distributed in the hope that it will be useful,
13b725ae77Skettenis    but WITHOUT ANY WARRANTY; without even the implied warranty of
14b725ae77Skettenis    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15b725ae77Skettenis    GNU General Public License for more details.
16b725ae77Skettenis 
17b725ae77Skettenis    You should have received a copy of the GNU General Public License
18b725ae77Skettenis    along with this program; if not, write to the Free Software
19b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
20b725ae77Skettenis    Boston, MA 02111-1307, USA. */
21b725ae77Skettenis 
22b725ae77Skettenis #include "defs.h"
23b725ae77Skettenis #include "top.h"
24b725ae77Skettenis #include "inferior.h"
25b725ae77Skettenis #include "target.h"
26b725ae77Skettenis #include "terminal.h"		/* for job_control */
27b725ae77Skettenis #include "event-loop.h"
28b725ae77Skettenis #include "event-top.h"
29b725ae77Skettenis #include "interps.h"
30b725ae77Skettenis #include <signal.h>
31b725ae77Skettenis 
32b725ae77Skettenis /* For dont_repeat() */
33b725ae77Skettenis #include "gdbcmd.h"
34b725ae77Skettenis 
35b725ae77Skettenis /* readline include files */
36b725ae77Skettenis #include "readline/readline.h"
37b725ae77Skettenis #include "readline/history.h"
38b725ae77Skettenis 
39b725ae77Skettenis /* readline defines this.  */
40b725ae77Skettenis #undef savestring
41b725ae77Skettenis 
42b725ae77Skettenis static void rl_callback_read_char_wrapper (gdb_client_data client_data);
43b725ae77Skettenis static void command_line_handler (char *rl);
44b725ae77Skettenis static void command_line_handler_continuation (struct continuation_arg *arg);
45b725ae77Skettenis static void change_line_handler (void);
46b725ae77Skettenis static void change_annotation_level (void);
47b725ae77Skettenis static void command_handler (char *command);
48b725ae77Skettenis static void async_do_nothing (gdb_client_data arg);
49b725ae77Skettenis static void async_disconnect (gdb_client_data arg);
50b725ae77Skettenis static void async_stop_sig (gdb_client_data arg);
51b725ae77Skettenis static void async_float_handler (gdb_client_data arg);
52b725ae77Skettenis 
53b725ae77Skettenis /* Signal handlers. */
54b725ae77Skettenis static void handle_sigquit (int sig);
55b725ae77Skettenis static void handle_sighup (int sig);
56b725ae77Skettenis static void handle_sigfpe (int sig);
57b725ae77Skettenis #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
58b725ae77Skettenis static void handle_sigwinch (int sig);
59b725ae77Skettenis #endif
60b725ae77Skettenis 
61b725ae77Skettenis /* Functions to be invoked by the event loop in response to
62b725ae77Skettenis    signals. */
63b725ae77Skettenis static void async_do_nothing (gdb_client_data);
64b725ae77Skettenis static void async_disconnect (gdb_client_data);
65b725ae77Skettenis static void async_float_handler (gdb_client_data);
66b725ae77Skettenis static void async_stop_sig (gdb_client_data);
67b725ae77Skettenis 
68b725ae77Skettenis /* Readline offers an alternate interface, via callback
69b725ae77Skettenis    functions. These are all included in the file callback.c in the
70b725ae77Skettenis    readline distribution.  This file provides (mainly) a function, which
71b725ae77Skettenis    the event loop uses as callback (i.e. event handler) whenever an event
72b725ae77Skettenis    is detected on the standard input file descriptor.
73b725ae77Skettenis    readline_callback_read_char is called (by the GDB event loop) whenever
74b725ae77Skettenis    there is a new character ready on the input stream. This function
75b725ae77Skettenis    incrementally builds a buffer internal to readline where it
76b725ae77Skettenis    accumulates the line read up to the point of invocation.  In the
77b725ae77Skettenis    special case in which the character read is newline, the function
78b725ae77Skettenis    invokes a GDB supplied callback routine, which does the processing of
79b725ae77Skettenis    a full command line.  This latter routine is the asynchronous analog
80b725ae77Skettenis    of the old command_line_input in gdb. Instead of invoking (and waiting
81b725ae77Skettenis    for) readline to read the command line and pass it back to
82b725ae77Skettenis    command_loop for processing, the new command_line_handler function has
83b725ae77Skettenis    the command line already available as its parameter.  INPUT_HANDLER is
84b725ae77Skettenis    to be set to the function that readline will invoke when a complete
85b725ae77Skettenis    line of input is ready.  CALL_READLINE is to be set to the function
86b725ae77Skettenis    that readline offers as callback to the event_loop. */
87b725ae77Skettenis 
88b725ae77Skettenis void (*input_handler) (char *);
89b725ae77Skettenis void (*call_readline) (gdb_client_data);
90b725ae77Skettenis 
91b725ae77Skettenis /* Important variables for the event loop. */
92b725ae77Skettenis 
93b725ae77Skettenis /* This is used to determine if GDB is using the readline library or
94b725ae77Skettenis    its own simplified form of readline. It is used by the asynchronous
95b725ae77Skettenis    form of the set editing command.
96b725ae77Skettenis    ezannoni: as of 1999-04-29 I expect that this
97b725ae77Skettenis    variable will not be used after gdb is changed to use the event
98b725ae77Skettenis    loop as default engine, and event-top.c is merged into top.c. */
99b725ae77Skettenis int async_command_editing_p;
100b725ae77Skettenis 
101b725ae77Skettenis /* This variable contains the new prompt that the user sets with the
102b725ae77Skettenis    set prompt command. */
103b725ae77Skettenis char *new_async_prompt;
104b725ae77Skettenis 
105b725ae77Skettenis /* This is the annotation suffix that will be used when the
106b725ae77Skettenis    annotation_level is 2. */
107b725ae77Skettenis char *async_annotation_suffix;
108b725ae77Skettenis 
109b725ae77Skettenis /* This is used to display the notification of the completion of an
110b725ae77Skettenis    asynchronous execution command. */
111b725ae77Skettenis int exec_done_display_p = 0;
112b725ae77Skettenis 
113b725ae77Skettenis /* This is the file descriptor for the input stream that GDB uses to
114b725ae77Skettenis    read commands from. */
115b725ae77Skettenis int input_fd;
116b725ae77Skettenis 
117b725ae77Skettenis /* This is the prompt stack. Prompts will be pushed on the stack as
118b725ae77Skettenis    needed by the different 'kinds' of user inputs GDB is asking
119b725ae77Skettenis    for. See event-loop.h. */
120b725ae77Skettenis struct prompts the_prompts;
121b725ae77Skettenis 
122b725ae77Skettenis /* signal handling variables */
123b725ae77Skettenis /* Each of these is a pointer to a function that the event loop will
124b725ae77Skettenis    invoke if the corresponding signal has received. The real signal
125b725ae77Skettenis    handlers mark these functions as ready to be executed and the event
126b725ae77Skettenis    loop, in a later iteration, calls them. See the function
127b725ae77Skettenis    invoke_async_signal_handler. */
128b725ae77Skettenis void *sigint_token;
129b725ae77Skettenis #ifdef SIGHUP
130b725ae77Skettenis void *sighup_token;
131b725ae77Skettenis #endif
132b725ae77Skettenis void *sigquit_token;
133b725ae77Skettenis void *sigfpe_token;
134b725ae77Skettenis #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
135b725ae77Skettenis void *sigwinch_token;
136b725ae77Skettenis #endif
137b725ae77Skettenis #ifdef STOP_SIGNAL
138b725ae77Skettenis void *sigtstp_token;
139b725ae77Skettenis #endif
140b725ae77Skettenis 
141b725ae77Skettenis /* Structure to save a partially entered command.  This is used when
142b725ae77Skettenis    the user types '\' at the end of a command line. This is necessary
143b725ae77Skettenis    because each line of input is handled by a different call to
144b725ae77Skettenis    command_line_handler, and normally there is no state retained
145b725ae77Skettenis    between different calls. */
146b725ae77Skettenis int more_to_come = 0;
147b725ae77Skettenis 
148b725ae77Skettenis struct readline_input_state
149b725ae77Skettenis   {
150b725ae77Skettenis     char *linebuffer;
151b725ae77Skettenis     char *linebuffer_ptr;
152b725ae77Skettenis   }
153b725ae77Skettenis readline_input_state;
154b725ae77Skettenis 
155b725ae77Skettenis /* This hook is called by rl_callback_read_char_wrapper after each
156b725ae77Skettenis    character is processed.  */
157b725ae77Skettenis void (*after_char_processing_hook) ();
158b725ae77Skettenis 
159b725ae77Skettenis 
160b725ae77Skettenis /* Wrapper function for calling into the readline library. The event
161b725ae77Skettenis    loop expects the callback function to have a paramter, while readline
162b725ae77Skettenis    expects none. */
163b725ae77Skettenis static void
rl_callback_read_char_wrapper(gdb_client_data client_data)164b725ae77Skettenis rl_callback_read_char_wrapper (gdb_client_data client_data)
165b725ae77Skettenis {
166b725ae77Skettenis   rl_callback_read_char ();
167b725ae77Skettenis   if (after_char_processing_hook)
168b725ae77Skettenis     (*after_char_processing_hook) ();
169b725ae77Skettenis }
170b725ae77Skettenis 
171b725ae77Skettenis /* Initialize all the necessary variables, start the event loop,
172b725ae77Skettenis    register readline, and stdin, start the loop. */
173b725ae77Skettenis void
cli_command_loop(void)174b725ae77Skettenis cli_command_loop (void)
175b725ae77Skettenis {
176b725ae77Skettenis   int length;
177b725ae77Skettenis   char *a_prompt;
178b725ae77Skettenis   char *gdb_prompt = get_prompt ();
179b725ae77Skettenis 
180b725ae77Skettenis   /* If we are using readline, set things up and display the first
181b725ae77Skettenis      prompt, otherwise just print the prompt. */
182b725ae77Skettenis   if (async_command_editing_p)
183b725ae77Skettenis     {
184b725ae77Skettenis       /* Tell readline what the prompt to display is and what function it
185b725ae77Skettenis          will need to call after a whole line is read. This also displays
186b725ae77Skettenis          the first prompt. */
187b725ae77Skettenis       length = strlen (PREFIX (0)) + strlen (gdb_prompt) + strlen (SUFFIX (0)) + 1;
188b725ae77Skettenis       a_prompt = (char *) xmalloc (length);
189b725ae77Skettenis       strcpy (a_prompt, PREFIX (0));
190b725ae77Skettenis       strcat (a_prompt, gdb_prompt);
191b725ae77Skettenis       strcat (a_prompt, SUFFIX (0));
192b725ae77Skettenis       rl_callback_handler_install (a_prompt, input_handler);
193b725ae77Skettenis     }
194b725ae77Skettenis   else
195b725ae77Skettenis     display_gdb_prompt (0);
196b725ae77Skettenis 
197b725ae77Skettenis   /* Now it's time to start the event loop. */
198b725ae77Skettenis   start_event_loop ();
199b725ae77Skettenis }
200b725ae77Skettenis 
201b725ae77Skettenis /* Change the function to be invoked every time there is a character
202b725ae77Skettenis    ready on stdin. This is used when the user sets the editing off,
203b725ae77Skettenis    therefore bypassing readline, and letting gdb handle the input
204b725ae77Skettenis    itself, via gdb_readline2. Also it is used in the opposite case in
205b725ae77Skettenis    which the user sets editing on again, by restoring readline
206b725ae77Skettenis    handling of the input. */
207b725ae77Skettenis static void
change_line_handler(void)208b725ae77Skettenis change_line_handler (void)
209b725ae77Skettenis {
210b725ae77Skettenis   /* NOTE: this operates on input_fd, not instream. If we are reading
211b725ae77Skettenis      commands from a file, instream will point to the file. However in
212b725ae77Skettenis      async mode, we always read commands from a file with editing
213b725ae77Skettenis      off. This means that the 'set editing on/off' will have effect
214b725ae77Skettenis      only on the interactive session. */
215b725ae77Skettenis 
216b725ae77Skettenis   if (async_command_editing_p)
217b725ae77Skettenis     {
218b725ae77Skettenis       /* Turn on editing by using readline. */
219b725ae77Skettenis       call_readline = rl_callback_read_char_wrapper;
220b725ae77Skettenis       input_handler = command_line_handler;
221b725ae77Skettenis     }
222b725ae77Skettenis   else
223b725ae77Skettenis     {
224b725ae77Skettenis       /* Turn off editing by using gdb_readline2. */
225b725ae77Skettenis       rl_callback_handler_remove ();
226b725ae77Skettenis       call_readline = gdb_readline2;
227b725ae77Skettenis 
228b725ae77Skettenis       /* Set up the command handler as well, in case we are called as
229b725ae77Skettenis          first thing from .gdbinit. */
230b725ae77Skettenis       input_handler = command_line_handler;
231b725ae77Skettenis     }
232b725ae77Skettenis }
233b725ae77Skettenis 
234b725ae77Skettenis /* Displays the prompt. The prompt that is displayed is the current
235b725ae77Skettenis    top of the prompt stack, if the argument NEW_PROMPT is
236b725ae77Skettenis    0. Otherwise, it displays whatever NEW_PROMPT is. This is used
237b725ae77Skettenis    after each gdb command has completed, and in the following cases:
238b725ae77Skettenis    1. when the user enters a command line which is ended by '\'
239b725ae77Skettenis    indicating that the command will continue on the next line.
240b725ae77Skettenis    In that case the prompt that is displayed is the empty string.
241b725ae77Skettenis    2. When the user is entering 'commands' for a breakpoint, or
242b725ae77Skettenis    actions for a tracepoint. In this case the prompt will be '>'
243b725ae77Skettenis    3. Other????
244b725ae77Skettenis    FIXME: 2. & 3. not implemented yet for async. */
245b725ae77Skettenis void
display_gdb_prompt(char * new_prompt)246b725ae77Skettenis display_gdb_prompt (char *new_prompt)
247b725ae77Skettenis {
248b725ae77Skettenis   int prompt_length = 0;
249b725ae77Skettenis   char *gdb_prompt = get_prompt ();
250b725ae77Skettenis 
251b725ae77Skettenis   /* Each interpreter has its own rules on displaying the command
252b725ae77Skettenis      prompt.  */
253b725ae77Skettenis   if (!current_interp_display_prompt_p ())
254b725ae77Skettenis     return;
255b725ae77Skettenis 
256b725ae77Skettenis   if (target_executing && sync_execution)
257b725ae77Skettenis     {
258b725ae77Skettenis       /* This is to trick readline into not trying to display the
259b725ae77Skettenis          prompt.  Even though we display the prompt using this
260b725ae77Skettenis          function, readline still tries to do its own display if we
261b725ae77Skettenis          don't call rl_callback_handler_install and
262b725ae77Skettenis          rl_callback_handler_remove (which readline detects because a
263b725ae77Skettenis          global variable is not set). If readline did that, it could
264b725ae77Skettenis          mess up gdb signal handlers for SIGINT.  Readline assumes
265b725ae77Skettenis          that between calls to rl_set_signals and rl_clear_signals gdb
266b725ae77Skettenis          doesn't do anything with the signal handlers. Well, that's
267b725ae77Skettenis          not the case, because when the target executes we change the
268b725ae77Skettenis          SIGINT signal handler. If we allowed readline to display the
269b725ae77Skettenis          prompt, the signal handler change would happen exactly
270b725ae77Skettenis          between the calls to the above two functions.
271b725ae77Skettenis          Calling rl_callback_handler_remove(), does the job. */
272b725ae77Skettenis 
273b725ae77Skettenis       rl_callback_handler_remove ();
274b725ae77Skettenis       return;
275b725ae77Skettenis     }
276b725ae77Skettenis 
277b725ae77Skettenis   if (!new_prompt)
278b725ae77Skettenis     {
279b725ae77Skettenis       /* Just use the top of the prompt stack. */
280b725ae77Skettenis       prompt_length = strlen (PREFIX (0)) +
281b725ae77Skettenis 	strlen (SUFFIX (0)) +
282b725ae77Skettenis 	strlen (gdb_prompt) + 1;
283b725ae77Skettenis 
284b725ae77Skettenis       new_prompt = (char *) alloca (prompt_length);
285b725ae77Skettenis 
286b725ae77Skettenis       /* Prefix needs to have new line at end. */
287b725ae77Skettenis       strcpy (new_prompt, PREFIX (0));
288b725ae77Skettenis       strcat (new_prompt, gdb_prompt);
289b725ae77Skettenis       /* Suffix needs to have a new line at end and \032 \032 at
290b725ae77Skettenis          beginning. */
291b725ae77Skettenis       strcat (new_prompt, SUFFIX (0));
292b725ae77Skettenis     }
293b725ae77Skettenis 
294b725ae77Skettenis   if (async_command_editing_p)
295b725ae77Skettenis     {
296b725ae77Skettenis       rl_callback_handler_remove ();
297b725ae77Skettenis       rl_callback_handler_install (new_prompt, input_handler);
298b725ae77Skettenis     }
299b725ae77Skettenis   /* new_prompt at this point can be the top of the stack or the one passed in */
300b725ae77Skettenis   else if (new_prompt)
301b725ae77Skettenis     {
302b725ae77Skettenis       /* Don't use a _filtered function here.  It causes the assumed
303b725ae77Skettenis          character position to be off, since the newline we read from
304b725ae77Skettenis          the user is not accounted for.  */
305b725ae77Skettenis       fputs_unfiltered (new_prompt, gdb_stdout);
306b725ae77Skettenis       gdb_flush (gdb_stdout);
307b725ae77Skettenis     }
308b725ae77Skettenis }
309b725ae77Skettenis 
310b725ae77Skettenis /* Used when the user requests a different annotation level, with
311b725ae77Skettenis    'set annotate'. It pushes a new prompt (with prefix and suffix) on top
312b725ae77Skettenis    of the prompt stack, if the annotation level desired is 2, otherwise
313b725ae77Skettenis    it pops the top of the prompt stack when we want the annotation level
314b725ae77Skettenis    to be the normal ones (1 or 0). */
315b725ae77Skettenis static void
change_annotation_level(void)316b725ae77Skettenis change_annotation_level (void)
317b725ae77Skettenis {
318b725ae77Skettenis   char *prefix, *suffix;
319b725ae77Skettenis 
320b725ae77Skettenis   if (!PREFIX (0) || !PROMPT (0) || !SUFFIX (0))
321b725ae77Skettenis     {
322b725ae77Skettenis       /* The prompt stack has not been initialized to "", we are
323b725ae77Skettenis          using gdb w/o the --async switch */
324b725ae77Skettenis       warning ("Command has same effect as set annotate");
325b725ae77Skettenis       return;
326b725ae77Skettenis     }
327b725ae77Skettenis 
328b725ae77Skettenis   if (annotation_level > 1)
329b725ae77Skettenis     {
330b725ae77Skettenis       if (!strcmp (PREFIX (0), "") && !strcmp (SUFFIX (0), ""))
331b725ae77Skettenis 	{
332b725ae77Skettenis 	  /* Push a new prompt if the previous annotation_level was not >1. */
333b725ae77Skettenis 	  prefix = (char *) alloca (strlen (async_annotation_suffix) + 10);
334b725ae77Skettenis 	  strcpy (prefix, "\n\032\032pre-");
335b725ae77Skettenis 	  strcat (prefix, async_annotation_suffix);
336b725ae77Skettenis 	  strcat (prefix, "\n");
337b725ae77Skettenis 
338b725ae77Skettenis 	  suffix = (char *) alloca (strlen (async_annotation_suffix) + 6);
339b725ae77Skettenis 	  strcpy (suffix, "\n\032\032");
340b725ae77Skettenis 	  strcat (suffix, async_annotation_suffix);
341b725ae77Skettenis 	  strcat (suffix, "\n");
342b725ae77Skettenis 
343b725ae77Skettenis 	  push_prompt (prefix, (char *) 0, suffix);
344b725ae77Skettenis 	}
345b725ae77Skettenis     }
346b725ae77Skettenis   else
347b725ae77Skettenis     {
348b725ae77Skettenis       if (strcmp (PREFIX (0), "") && strcmp (SUFFIX (0), ""))
349b725ae77Skettenis 	{
350b725ae77Skettenis 	  /* Pop the top of the stack, we are going back to annotation < 1. */
351b725ae77Skettenis 	  pop_prompt ();
352b725ae77Skettenis 	}
353b725ae77Skettenis     }
354b725ae77Skettenis }
355b725ae77Skettenis 
356b725ae77Skettenis /* Pushes a new prompt on the prompt stack. Each prompt has three
357b725ae77Skettenis    parts: prefix, prompt, suffix. Usually prefix and suffix are empty
358b725ae77Skettenis    strings, except when the annotation level is 2. Memory is allocated
359b725ae77Skettenis    within savestring for the new prompt. */
360b725ae77Skettenis void
push_prompt(char * prefix,char * prompt,char * suffix)361b725ae77Skettenis push_prompt (char *prefix, char *prompt, char *suffix)
362b725ae77Skettenis {
363b725ae77Skettenis   the_prompts.top++;
364b725ae77Skettenis   PREFIX (0) = savestring (prefix, strlen (prefix));
365b725ae77Skettenis 
366b725ae77Skettenis   /* Note that this function is used by the set annotate 2
367b725ae77Skettenis      command. This is why we take care of saving the old prompt
368b725ae77Skettenis      in case a new one is not specified. */
369b725ae77Skettenis   if (prompt)
370b725ae77Skettenis     PROMPT (0) = savestring (prompt, strlen (prompt));
371b725ae77Skettenis   else
372b725ae77Skettenis     PROMPT (0) = savestring (PROMPT (-1), strlen (PROMPT (-1)));
373b725ae77Skettenis 
374b725ae77Skettenis   SUFFIX (0) = savestring (suffix, strlen (suffix));
375b725ae77Skettenis }
376b725ae77Skettenis 
377b725ae77Skettenis /* Pops the top of the prompt stack, and frees the memory allocated for it. */
378b725ae77Skettenis void
pop_prompt(void)379b725ae77Skettenis pop_prompt (void)
380b725ae77Skettenis {
381b725ae77Skettenis   /* If we are not during a 'synchronous' execution command, in which
382b725ae77Skettenis      case, the top prompt would be empty. */
383b725ae77Skettenis   if (strcmp (PROMPT (0), ""))
384b725ae77Skettenis     /* This is for the case in which the prompt is set while the
385b725ae77Skettenis        annotation level is 2. The top prompt will be changed, but when
386b725ae77Skettenis        we return to annotation level < 2, we want that new prompt to be
387b725ae77Skettenis        in effect, until the user does another 'set prompt'. */
388b725ae77Skettenis     if (strcmp (PROMPT (0), PROMPT (-1)))
389b725ae77Skettenis       {
390b725ae77Skettenis 	xfree (PROMPT (-1));
391b725ae77Skettenis 	PROMPT (-1) = savestring (PROMPT (0), strlen (PROMPT (0)));
392b725ae77Skettenis       }
393b725ae77Skettenis 
394b725ae77Skettenis   xfree (PREFIX (0));
395b725ae77Skettenis   xfree (PROMPT (0));
396b725ae77Skettenis   xfree (SUFFIX (0));
397b725ae77Skettenis   the_prompts.top--;
398b725ae77Skettenis }
399b725ae77Skettenis 
400b725ae77Skettenis /* When there is an event ready on the stdin file desriptor, instead
401b725ae77Skettenis    of calling readline directly throught the callback function, or
402b725ae77Skettenis    instead of calling gdb_readline2, give gdb a chance to detect
403b725ae77Skettenis    errors and do something. */
404b725ae77Skettenis void
stdin_event_handler(int error,gdb_client_data client_data)405b725ae77Skettenis stdin_event_handler (int error, gdb_client_data client_data)
406b725ae77Skettenis {
407b725ae77Skettenis   if (error)
408b725ae77Skettenis     {
409b725ae77Skettenis       printf_unfiltered ("error detected on stdin\n");
410b725ae77Skettenis       delete_file_handler (input_fd);
411b725ae77Skettenis       discard_all_continuations ();
412b725ae77Skettenis       /* If stdin died, we may as well kill gdb. */
413b725ae77Skettenis       quit_command ((char *) 0, stdin == instream);
414b725ae77Skettenis     }
415b725ae77Skettenis   else
416b725ae77Skettenis     (*call_readline) (client_data);
417b725ae77Skettenis }
418b725ae77Skettenis 
419b725ae77Skettenis /* Re-enable stdin after the end of an execution command in
420b725ae77Skettenis    synchronous mode, or after an error from the target, and we aborted
421b725ae77Skettenis    the exec operation. */
422b725ae77Skettenis 
423b725ae77Skettenis void
async_enable_stdin(void * dummy)424b725ae77Skettenis async_enable_stdin (void *dummy)
425b725ae77Skettenis {
426b725ae77Skettenis   /* See NOTE in async_disable_stdin() */
427b725ae77Skettenis   /* FIXME: cagney/1999-09-27: Call this before clearing
428b725ae77Skettenis      sync_execution.  Current target_terminal_ours() implementations
429b725ae77Skettenis      check for sync_execution before switching the terminal. */
430b725ae77Skettenis   target_terminal_ours ();
431b725ae77Skettenis   pop_prompt ();
432b725ae77Skettenis   sync_execution = 0;
433b725ae77Skettenis }
434b725ae77Skettenis 
435b725ae77Skettenis /* Disable reads from stdin (the console) marking the command as
436b725ae77Skettenis    synchronous. */
437b725ae77Skettenis 
438b725ae77Skettenis void
async_disable_stdin(void)439b725ae77Skettenis async_disable_stdin (void)
440b725ae77Skettenis {
441b725ae77Skettenis   sync_execution = 1;
442b725ae77Skettenis   push_prompt ("", "", "");
443b725ae77Skettenis   /* FIXME: cagney/1999-09-27: At present this call is technically
444b725ae77Skettenis      redundant since infcmd.c and infrun.c both already call
445b725ae77Skettenis      target_terminal_inferior().  As the terminal handling (in
446b725ae77Skettenis      sync/async mode) is refined, the duplicate calls can be
447b725ae77Skettenis      eliminated (Here or in infcmd.c/infrun.c). */
448b725ae77Skettenis   target_terminal_inferior ();
449b725ae77Skettenis   /* Add the reinstate of stdin to the list of cleanups to be done
450b725ae77Skettenis      in case the target errors out and dies. These cleanups are also
451b725ae77Skettenis      done in case of normal successful termination of the execution
452b725ae77Skettenis      command, by complete_execution(). */
453b725ae77Skettenis   make_exec_error_cleanup (async_enable_stdin, NULL);
454b725ae77Skettenis }
455b725ae77Skettenis 
456b725ae77Skettenis 
457b725ae77Skettenis /* Handles a gdb command. This function is called by
458b725ae77Skettenis    command_line_handler, which has processed one or more input lines
459b725ae77Skettenis    into COMMAND. */
460b725ae77Skettenis /* NOTE: 1999-04-30 This is the asynchronous version of the command_loop
461b725ae77Skettenis    function.  The command_loop function will be obsolete when we
462b725ae77Skettenis    switch to use the event loop at every execution of gdb. */
463b725ae77Skettenis static void
command_handler(char * command)464b725ae77Skettenis command_handler (char *command)
465b725ae77Skettenis {
466b725ae77Skettenis   struct cleanup *old_chain;
467b725ae77Skettenis   int stdin_is_tty = ISATTY (stdin);
468b725ae77Skettenis   struct continuation_arg *arg1;
469b725ae77Skettenis   struct continuation_arg *arg2;
470b725ae77Skettenis   long time_at_cmd_start;
471b725ae77Skettenis #ifdef HAVE_SBRK
472b725ae77Skettenis   long space_at_cmd_start = 0;
473b725ae77Skettenis #endif
474b725ae77Skettenis   extern int display_time;
475b725ae77Skettenis   extern int display_space;
476b725ae77Skettenis 
477b725ae77Skettenis   quit_flag = 0;
478b725ae77Skettenis   if (instream == stdin && stdin_is_tty)
479b725ae77Skettenis     reinitialize_more_filter ();
480b725ae77Skettenis   old_chain = make_cleanup (null_cleanup, 0);
481b725ae77Skettenis 
482b725ae77Skettenis   /* If readline returned a NULL command, it means that the
483b725ae77Skettenis      connection with the terminal is gone. This happens at the
484b725ae77Skettenis      end of a testsuite run, after Expect has hung up
485b725ae77Skettenis      but GDB is still alive. In such a case, we just quit gdb
486b725ae77Skettenis      killing the inferior program too. */
487b725ae77Skettenis   if (command == 0)
488b725ae77Skettenis     quit_command ((char *) 0, stdin == instream);
489b725ae77Skettenis 
490b725ae77Skettenis   time_at_cmd_start = get_run_time ();
491b725ae77Skettenis 
492b725ae77Skettenis   if (display_space)
493b725ae77Skettenis     {
494b725ae77Skettenis #ifdef HAVE_SBRK
495b725ae77Skettenis       char *lim = (char *) sbrk (0);
496b725ae77Skettenis       space_at_cmd_start = lim - lim_at_start;
497b725ae77Skettenis #endif
498b725ae77Skettenis     }
499b725ae77Skettenis 
500b725ae77Skettenis   execute_command (command, instream == stdin);
501b725ae77Skettenis 
502b725ae77Skettenis   /* Set things up for this function to be compete later, once the
503b725ae77Skettenis      execution has completed, if we are doing an execution command,
504b725ae77Skettenis      otherwise, just go ahead and finish. */
505b725ae77Skettenis   if (target_can_async_p () && target_executing)
506b725ae77Skettenis     {
507b725ae77Skettenis       arg1 =
508b725ae77Skettenis 	(struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
509b725ae77Skettenis       arg2 =
510b725ae77Skettenis 	(struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
511b725ae77Skettenis       arg1->next = arg2;
512b725ae77Skettenis       arg2->next = NULL;
513b725ae77Skettenis       arg1->data.longint = time_at_cmd_start;
514b725ae77Skettenis #ifdef HAVE_SBRK
515b725ae77Skettenis       arg2->data.longint = space_at_cmd_start;
516b725ae77Skettenis #endif
517b725ae77Skettenis       add_continuation (command_line_handler_continuation, arg1);
518b725ae77Skettenis     }
519b725ae77Skettenis 
520b725ae77Skettenis   /* Do any commands attached to breakpoint we stopped at. Only if we
521b725ae77Skettenis      are always running synchronously. Or if we have just executed a
522b725ae77Skettenis      command that doesn't start the target. */
523b725ae77Skettenis   if (!target_can_async_p () || !target_executing)
524b725ae77Skettenis     {
525b725ae77Skettenis       bpstat_do_actions (&stop_bpstat);
526b725ae77Skettenis       do_cleanups (old_chain);
527b725ae77Skettenis 
528b725ae77Skettenis       if (display_time)
529b725ae77Skettenis 	{
530b725ae77Skettenis 	  long cmd_time = get_run_time () - time_at_cmd_start;
531b725ae77Skettenis 
532b725ae77Skettenis 	  printf_unfiltered ("Command execution time: %ld.%06ld\n",
533b725ae77Skettenis 			     cmd_time / 1000000, cmd_time % 1000000);
534b725ae77Skettenis 	}
535b725ae77Skettenis 
536b725ae77Skettenis       if (display_space)
537b725ae77Skettenis 	{
538b725ae77Skettenis #ifdef HAVE_SBRK
539b725ae77Skettenis 	  char *lim = (char *) sbrk (0);
540b725ae77Skettenis 	  long space_now = lim - lim_at_start;
541b725ae77Skettenis 	  long space_diff = space_now - space_at_cmd_start;
542b725ae77Skettenis 
543b725ae77Skettenis 	  printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
544b725ae77Skettenis 			     space_now,
545b725ae77Skettenis 			     (space_diff >= 0 ? '+' : '-'),
546b725ae77Skettenis 			     space_diff);
547b725ae77Skettenis #endif
548b725ae77Skettenis 	}
549b725ae77Skettenis     }
550b725ae77Skettenis }
551b725ae77Skettenis 
552b725ae77Skettenis /* Do any commands attached to breakpoint we stopped at. Only if we
553b725ae77Skettenis    are always running synchronously. Or if we have just executed a
554b725ae77Skettenis    command that doesn't start the target. */
555b725ae77Skettenis void
command_line_handler_continuation(struct continuation_arg * arg)556b725ae77Skettenis command_line_handler_continuation (struct continuation_arg *arg)
557b725ae77Skettenis {
558b725ae77Skettenis   extern int display_time;
559b725ae77Skettenis   extern int display_space;
560b725ae77Skettenis 
561b725ae77Skettenis   long time_at_cmd_start  = arg->data.longint;
562b725ae77Skettenis   long space_at_cmd_start = arg->next->data.longint;
563b725ae77Skettenis 
564b725ae77Skettenis   bpstat_do_actions (&stop_bpstat);
565b725ae77Skettenis   /*do_cleanups (old_chain); *//*?????FIXME????? */
566b725ae77Skettenis 
567b725ae77Skettenis   if (display_time)
568b725ae77Skettenis     {
569b725ae77Skettenis       long cmd_time = get_run_time () - time_at_cmd_start;
570b725ae77Skettenis 
571b725ae77Skettenis       printf_unfiltered ("Command execution time: %ld.%06ld\n",
572b725ae77Skettenis 			 cmd_time / 1000000, cmd_time % 1000000);
573b725ae77Skettenis     }
574b725ae77Skettenis   if (display_space)
575b725ae77Skettenis     {
576b725ae77Skettenis #ifdef HAVE_SBRK
577b725ae77Skettenis       char *lim = (char *) sbrk (0);
578b725ae77Skettenis       long space_now = lim - lim_at_start;
579b725ae77Skettenis       long space_diff = space_now - space_at_cmd_start;
580b725ae77Skettenis 
581b725ae77Skettenis       printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
582b725ae77Skettenis 			 space_now,
583b725ae77Skettenis 			 (space_diff >= 0 ? '+' : '-'),
584b725ae77Skettenis 			 space_diff);
585b725ae77Skettenis #endif
586b725ae77Skettenis     }
587b725ae77Skettenis }
588b725ae77Skettenis 
589b725ae77Skettenis /* Handle a complete line of input. This is called by the callback
590b725ae77Skettenis    mechanism within the readline library.  Deal with incomplete commands
591b725ae77Skettenis    as well, by saving the partial input in a global buffer.  */
592b725ae77Skettenis 
593b725ae77Skettenis /* NOTE: 1999-04-30 This is the asynchronous version of the
594b725ae77Skettenis    command_line_input function. command_line_input will become
595b725ae77Skettenis    obsolete once we use the event loop as the default mechanism in
596b725ae77Skettenis    GDB. */
597b725ae77Skettenis static void
command_line_handler(char * rl)598b725ae77Skettenis command_line_handler (char *rl)
599b725ae77Skettenis {
600b725ae77Skettenis   static char *linebuffer = 0;
601b725ae77Skettenis   static unsigned linelength = 0;
602b725ae77Skettenis   char *p;
603b725ae77Skettenis   char *p1;
604b725ae77Skettenis   extern char *line;
605b725ae77Skettenis   extern int linesize;
606b725ae77Skettenis   char *nline;
607b725ae77Skettenis   char got_eof = 0;
608b725ae77Skettenis 
609b725ae77Skettenis 
610b725ae77Skettenis   int repeat = (instream == stdin);
611b725ae77Skettenis 
612b725ae77Skettenis   if (annotation_level > 1 && instream == stdin)
613b725ae77Skettenis     {
614b725ae77Skettenis       printf_unfiltered ("\n\032\032post-");
615b725ae77Skettenis       puts_unfiltered (async_annotation_suffix);
616b725ae77Skettenis       printf_unfiltered ("\n");
617b725ae77Skettenis     }
618b725ae77Skettenis 
619b725ae77Skettenis   if (linebuffer == 0)
620b725ae77Skettenis     {
621b725ae77Skettenis       linelength = 80;
622b725ae77Skettenis       linebuffer = (char *) xmalloc (linelength);
623b725ae77Skettenis     }
624b725ae77Skettenis 
625b725ae77Skettenis   p = linebuffer;
626b725ae77Skettenis 
627b725ae77Skettenis   if (more_to_come)
628b725ae77Skettenis     {
629b725ae77Skettenis       strcpy (linebuffer, readline_input_state.linebuffer);
630b725ae77Skettenis       p = readline_input_state.linebuffer_ptr;
631b725ae77Skettenis       xfree (readline_input_state.linebuffer);
632b725ae77Skettenis       more_to_come = 0;
633b725ae77Skettenis       pop_prompt ();
634b725ae77Skettenis     }
635b725ae77Skettenis 
636b725ae77Skettenis #ifdef STOP_SIGNAL
637b725ae77Skettenis   if (job_control)
638b725ae77Skettenis     signal (STOP_SIGNAL, handle_stop_sig);
639b725ae77Skettenis #endif
640b725ae77Skettenis 
641b725ae77Skettenis   /* Make sure that all output has been output.  Some machines may let
642b725ae77Skettenis      you get away with leaving out some of the gdb_flush, but not all.  */
643b725ae77Skettenis   wrap_here ("");
644b725ae77Skettenis   gdb_flush (gdb_stdout);
645b725ae77Skettenis   gdb_flush (gdb_stderr);
646b725ae77Skettenis 
647b725ae77Skettenis   if (source_file_name != NULL)
648b725ae77Skettenis     {
649b725ae77Skettenis       ++source_line_number;
650b725ae77Skettenis       sprintf (source_error,
651b725ae77Skettenis 	       "%s%s:%d: Error in sourced command file:\n",
652b725ae77Skettenis 	       source_pre_error,
653b725ae77Skettenis 	       source_file_name,
654b725ae77Skettenis 	       source_line_number);
655b725ae77Skettenis       error_pre_print = source_error;
656b725ae77Skettenis     }
657b725ae77Skettenis 
658b725ae77Skettenis   /* If we are in this case, then command_handler will call quit
659b725ae77Skettenis      and exit from gdb. */
660b725ae77Skettenis   if (!rl || rl == (char *) EOF)
661b725ae77Skettenis     {
662b725ae77Skettenis       got_eof = 1;
663b725ae77Skettenis       command_handler (0);
664b725ae77Skettenis     }
665b725ae77Skettenis   if (strlen (rl) + 1 + (p - linebuffer) > linelength)
666b725ae77Skettenis     {
667b725ae77Skettenis       linelength = strlen (rl) + 1 + (p - linebuffer);
668b725ae77Skettenis       nline = (char *) xrealloc (linebuffer, linelength);
669b725ae77Skettenis       p += nline - linebuffer;
670b725ae77Skettenis       linebuffer = nline;
671b725ae77Skettenis     }
672b725ae77Skettenis   p1 = rl;
673b725ae77Skettenis   /* Copy line.  Don't copy null at end.  (Leaves line alone
674b725ae77Skettenis      if this was just a newline)  */
675b725ae77Skettenis   while (*p1)
676b725ae77Skettenis     *p++ = *p1++;
677b725ae77Skettenis 
678b725ae77Skettenis   xfree (rl);			/* Allocated in readline.  */
679b725ae77Skettenis 
680b725ae77Skettenis   if (p > linebuffer && *(p - 1) == '\\')
681b725ae77Skettenis     {
682b725ae77Skettenis       p--;			/* Put on top of '\'.  */
683b725ae77Skettenis 
684b725ae77Skettenis       readline_input_state.linebuffer = savestring (linebuffer,
685b725ae77Skettenis 						    strlen (linebuffer));
686b725ae77Skettenis       readline_input_state.linebuffer_ptr = p;
687b725ae77Skettenis 
688b725ae77Skettenis       /* We will not invoke a execute_command if there is more
689b725ae77Skettenis 	 input expected to complete the command. So, we need to
690b725ae77Skettenis 	 print an empty prompt here. */
691b725ae77Skettenis       more_to_come = 1;
692b725ae77Skettenis       push_prompt ("", "", "");
693b725ae77Skettenis       display_gdb_prompt (0);
694b725ae77Skettenis       return;
695b725ae77Skettenis     }
696b725ae77Skettenis 
697b725ae77Skettenis #ifdef STOP_SIGNAL
698b725ae77Skettenis   if (job_control)
699b725ae77Skettenis     signal (STOP_SIGNAL, SIG_DFL);
700b725ae77Skettenis #endif
701b725ae77Skettenis 
702b725ae77Skettenis #define SERVER_COMMAND_LENGTH 7
703b725ae77Skettenis   server_command =
704b725ae77Skettenis     (p - linebuffer > SERVER_COMMAND_LENGTH)
705b725ae77Skettenis     && strncmp (linebuffer, "server ", SERVER_COMMAND_LENGTH) == 0;
706b725ae77Skettenis   if (server_command)
707b725ae77Skettenis     {
708b725ae77Skettenis       /* Note that we don't set `line'.  Between this and the check in
709b725ae77Skettenis          dont_repeat, this insures that repeating will still do the
710b725ae77Skettenis          right thing.  */
711b725ae77Skettenis       *p = '\0';
712b725ae77Skettenis       command_handler (linebuffer + SERVER_COMMAND_LENGTH);
713b725ae77Skettenis       display_gdb_prompt (0);
714b725ae77Skettenis       return;
715b725ae77Skettenis     }
716b725ae77Skettenis 
717b725ae77Skettenis   /* Do history expansion if that is wished.  */
718b725ae77Skettenis   if (history_expansion_p && instream == stdin
719b725ae77Skettenis       && ISATTY (instream))
720b725ae77Skettenis     {
721b725ae77Skettenis       char *history_value;
722b725ae77Skettenis       int expanded;
723b725ae77Skettenis 
724b725ae77Skettenis       *p = '\0';		/* Insert null now.  */
725b725ae77Skettenis       expanded = history_expand (linebuffer, &history_value);
726b725ae77Skettenis       if (expanded)
727b725ae77Skettenis 	{
728b725ae77Skettenis 	  /* Print the changes.  */
729b725ae77Skettenis 	  printf_unfiltered ("%s\n", history_value);
730b725ae77Skettenis 
731b725ae77Skettenis 	  /* If there was an error, call this function again.  */
732b725ae77Skettenis 	  if (expanded < 0)
733b725ae77Skettenis 	    {
734b725ae77Skettenis 	      xfree (history_value);
735b725ae77Skettenis 	      return;
736b725ae77Skettenis 	    }
737b725ae77Skettenis 	  if (strlen (history_value) > linelength)
738b725ae77Skettenis 	    {
739b725ae77Skettenis 	      linelength = strlen (history_value) + 1;
740b725ae77Skettenis 	      linebuffer = (char *) xrealloc (linebuffer, linelength);
741b725ae77Skettenis 	    }
742b725ae77Skettenis 	  strcpy (linebuffer, history_value);
743b725ae77Skettenis 	  p = linebuffer + strlen (linebuffer);
744b725ae77Skettenis 	  xfree (history_value);
745b725ae77Skettenis 	}
746b725ae77Skettenis     }
747b725ae77Skettenis 
748b725ae77Skettenis   /* If we just got an empty line, and that is supposed
749b725ae77Skettenis      to repeat the previous command, return the value in the
750b725ae77Skettenis      global buffer.  */
751b725ae77Skettenis   if (repeat && p == linebuffer && *p != '\\')
752b725ae77Skettenis     {
753b725ae77Skettenis       command_handler (line);
754b725ae77Skettenis       display_gdb_prompt (0);
755b725ae77Skettenis       return;
756b725ae77Skettenis     }
757b725ae77Skettenis 
758b725ae77Skettenis   for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
759b725ae77Skettenis   if (repeat && !*p1)
760b725ae77Skettenis     {
761b725ae77Skettenis       command_handler (line);
762b725ae77Skettenis       display_gdb_prompt (0);
763b725ae77Skettenis       return;
764b725ae77Skettenis     }
765b725ae77Skettenis 
766b725ae77Skettenis   *p = 0;
767b725ae77Skettenis 
768b725ae77Skettenis   /* Add line to history if appropriate.  */
769b725ae77Skettenis   if (instream == stdin
770b725ae77Skettenis       && ISATTY (stdin) && *linebuffer)
771b725ae77Skettenis     add_history (linebuffer);
772b725ae77Skettenis 
773b725ae77Skettenis   /* Note: lines consisting solely of comments are added to the command
774b725ae77Skettenis      history.  This is useful when you type a command, and then
775b725ae77Skettenis      realize you don't want to execute it quite yet.  You can comment
776b725ae77Skettenis      out the command and then later fetch it from the value history
777b725ae77Skettenis      and remove the '#'.  The kill ring is probably better, but some
778b725ae77Skettenis      people are in the habit of commenting things out.  */
779b725ae77Skettenis   if (*p1 == '#')
780b725ae77Skettenis     *p1 = '\0';			/* Found a comment. */
781b725ae77Skettenis 
782b725ae77Skettenis   /* Save into global buffer if appropriate.  */
783b725ae77Skettenis   if (repeat)
784b725ae77Skettenis     {
785b725ae77Skettenis       if (linelength > linesize)
786b725ae77Skettenis 	{
787b725ae77Skettenis 	  line = xrealloc (line, linelength);
788b725ae77Skettenis 	  linesize = linelength;
789b725ae77Skettenis 	}
790b725ae77Skettenis       strcpy (line, linebuffer);
791b725ae77Skettenis       if (!more_to_come)
792b725ae77Skettenis 	{
793b725ae77Skettenis 	  command_handler (line);
794b725ae77Skettenis 	  display_gdb_prompt (0);
795b725ae77Skettenis 	}
796b725ae77Skettenis       return;
797b725ae77Skettenis     }
798b725ae77Skettenis 
799b725ae77Skettenis   command_handler (linebuffer);
800b725ae77Skettenis   display_gdb_prompt (0);
801b725ae77Skettenis   return;
802b725ae77Skettenis }
803b725ae77Skettenis 
804b725ae77Skettenis /* Does reading of input from terminal w/o the editing features
805b725ae77Skettenis    provided by the readline library. */
806b725ae77Skettenis 
807b725ae77Skettenis /* NOTE: 1999-04-30 Asynchronous version of gdb_readline. gdb_readline
808b725ae77Skettenis    will become obsolete when the event loop is made the default
809b725ae77Skettenis    execution for gdb. */
810b725ae77Skettenis void
gdb_readline2(gdb_client_data client_data)811b725ae77Skettenis gdb_readline2 (gdb_client_data client_data)
812b725ae77Skettenis {
813b725ae77Skettenis   int c;
814b725ae77Skettenis   char *result;
815b725ae77Skettenis   int input_index = 0;
816b725ae77Skettenis   int result_size = 80;
817b725ae77Skettenis   static int done_once = 0;
818b725ae77Skettenis 
819b725ae77Skettenis   /* Unbuffer the input stream, so that, later on, the calls to fgetc
820b725ae77Skettenis      fetch only one char at the time from the stream. The fgetc's will
821b725ae77Skettenis      get up to the first newline, but there may be more chars in the
822b725ae77Skettenis      stream after '\n'. If we buffer the input and fgetc drains the
823b725ae77Skettenis      stream, getting stuff beyond the newline as well, a select, done
824b725ae77Skettenis      afterwards will not trigger. */
825b725ae77Skettenis   if (!done_once && !ISATTY (instream))
826b725ae77Skettenis     {
827b725ae77Skettenis       setbuf (instream, NULL);
828b725ae77Skettenis       done_once = 1;
829b725ae77Skettenis     }
830b725ae77Skettenis 
831b725ae77Skettenis   result = (char *) xmalloc (result_size);
832b725ae77Skettenis 
833b725ae77Skettenis   /* We still need the while loop here, even though it would seem
834b725ae77Skettenis      obvious to invoke gdb_readline2 at every character entered.  If
835b725ae77Skettenis      not using the readline library, the terminal is in cooked mode,
836b725ae77Skettenis      which sends the characters all at once. Poll will notice that the
837b725ae77Skettenis      input fd has changed state only after enter is pressed. At this
838b725ae77Skettenis      point we still need to fetch all the chars entered. */
839b725ae77Skettenis 
840b725ae77Skettenis   while (1)
841b725ae77Skettenis     {
842b725ae77Skettenis       /* Read from stdin if we are executing a user defined command.
843b725ae77Skettenis          This is the right thing for prompt_for_continue, at least.  */
844b725ae77Skettenis       c = fgetc (instream ? instream : stdin);
845b725ae77Skettenis 
846b725ae77Skettenis       if (c == EOF)
847b725ae77Skettenis 	{
848b725ae77Skettenis 	  if (input_index > 0)
849b725ae77Skettenis 	    /* The last line does not end with a newline.  Return it, and
850b725ae77Skettenis 	       if we are called again fgetc will still return EOF and
851b725ae77Skettenis 	       we'll return NULL then.  */
852b725ae77Skettenis 	    break;
853b725ae77Skettenis 	  xfree (result);
854b725ae77Skettenis 	  (*input_handler) (0);
855b725ae77Skettenis 	}
856b725ae77Skettenis 
857b725ae77Skettenis       if (c == '\n')
858b725ae77Skettenis 	{
859b725ae77Skettenis 	  if (input_index > 0 && result[input_index - 1] == '\r')
860b725ae77Skettenis 	    input_index--;
861b725ae77Skettenis 	  break;
862b725ae77Skettenis 	}
863b725ae77Skettenis 
864b725ae77Skettenis       result[input_index++] = c;
865b725ae77Skettenis       while (input_index >= result_size)
866b725ae77Skettenis 	{
867b725ae77Skettenis 	  result_size *= 2;
868b725ae77Skettenis 	  result = (char *) xrealloc (result, result_size);
869b725ae77Skettenis 	}
870b725ae77Skettenis     }
871b725ae77Skettenis 
872b725ae77Skettenis   result[input_index++] = '\0';
873b725ae77Skettenis   (*input_handler) (result);
874b725ae77Skettenis }
875b725ae77Skettenis 
876b725ae77Skettenis 
877b725ae77Skettenis /* Initialization of signal handlers and tokens.  There is a function
878b725ae77Skettenis    handle_sig* for each of the signals GDB cares about. Specifically:
879b725ae77Skettenis    SIGINT, SIGFPE, SIGQUIT, SIGTSTP, SIGHUP, SIGWINCH.  These
880b725ae77Skettenis    functions are the actual signal handlers associated to the signals
881b725ae77Skettenis    via calls to signal().  The only job for these functions is to
882b725ae77Skettenis    enqueue the appropriate event/procedure with the event loop.  Such
883b725ae77Skettenis    procedures are the old signal handlers. The event loop will take
884b725ae77Skettenis    care of invoking the queued procedures to perform the usual tasks
885b725ae77Skettenis    associated with the reception of the signal. */
886b725ae77Skettenis /* NOTE: 1999-04-30 This is the asynchronous version of init_signals.
887b725ae77Skettenis    init_signals will become obsolete as we move to have to event loop
888b725ae77Skettenis    as the default for gdb. */
889b725ae77Skettenis void
async_init_signals(void)890b725ae77Skettenis async_init_signals (void)
891b725ae77Skettenis {
892b725ae77Skettenis   signal (SIGINT, handle_sigint);
893b725ae77Skettenis   sigint_token =
894b725ae77Skettenis     create_async_signal_handler (async_request_quit, NULL);
895b725ae77Skettenis 
896b725ae77Skettenis   /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
897b725ae77Skettenis      to the inferior and breakpoints will be ignored.  */
898b725ae77Skettenis #ifdef SIGTRAP
899b725ae77Skettenis   signal (SIGTRAP, SIG_DFL);
900b725ae77Skettenis #endif
901b725ae77Skettenis 
902b725ae77Skettenis   /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
903b725ae77Skettenis      passed to the inferior, which we don't want.  It would be
904b725ae77Skettenis      possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
905b725ae77Skettenis      on BSD4.3 systems using vfork, that can affect the
906b725ae77Skettenis      GDB process as well as the inferior (the signal handling tables
907b725ae77Skettenis      might be in memory, shared between the two).  Since we establish
908b725ae77Skettenis      a handler for SIGQUIT, when we call exec it will set the signal
909b725ae77Skettenis      to SIG_DFL for us.  */
910b725ae77Skettenis   signal (SIGQUIT, handle_sigquit);
911b725ae77Skettenis   sigquit_token =
912b725ae77Skettenis     create_async_signal_handler (async_do_nothing, NULL);
913b725ae77Skettenis #ifdef SIGHUP
914b725ae77Skettenis   if (signal (SIGHUP, handle_sighup) != SIG_IGN)
915b725ae77Skettenis     sighup_token =
916b725ae77Skettenis       create_async_signal_handler (async_disconnect, NULL);
917b725ae77Skettenis   else
918b725ae77Skettenis     sighup_token =
919b725ae77Skettenis       create_async_signal_handler (async_do_nothing, NULL);
920b725ae77Skettenis #endif
921b725ae77Skettenis   signal (SIGFPE, handle_sigfpe);
922b725ae77Skettenis   sigfpe_token =
923b725ae77Skettenis     create_async_signal_handler (async_float_handler, NULL);
924b725ae77Skettenis 
925b725ae77Skettenis #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
926b725ae77Skettenis   signal (SIGWINCH, handle_sigwinch);
927b725ae77Skettenis   sigwinch_token =
928b725ae77Skettenis     create_async_signal_handler (SIGWINCH_HANDLER, NULL);
929b725ae77Skettenis #endif
930b725ae77Skettenis #ifdef STOP_SIGNAL
931b725ae77Skettenis   sigtstp_token =
932b725ae77Skettenis     create_async_signal_handler (async_stop_sig, NULL);
933b725ae77Skettenis #endif
934b725ae77Skettenis 
935b725ae77Skettenis }
936b725ae77Skettenis 
937b725ae77Skettenis void
mark_async_signal_handler_wrapper(void * token)938b725ae77Skettenis mark_async_signal_handler_wrapper (void *token)
939b725ae77Skettenis {
940b725ae77Skettenis   mark_async_signal_handler ((struct async_signal_handler *) token);
941b725ae77Skettenis }
942b725ae77Skettenis 
943b725ae77Skettenis /* Tell the event loop what to do if SIGINT is received.
944b725ae77Skettenis    See event-signal.c. */
945b725ae77Skettenis void
handle_sigint(int sig)946b725ae77Skettenis handle_sigint (int sig)
947b725ae77Skettenis {
948b725ae77Skettenis   signal (sig, handle_sigint);
949b725ae77Skettenis 
950b725ae77Skettenis   /* If immediate_quit is set, we go ahead and process the SIGINT right
951b725ae77Skettenis      away, even if we usually would defer this to the event loop. The
952b725ae77Skettenis      assumption here is that it is safe to process ^C immediately if
953b725ae77Skettenis      immediate_quit is set. If we didn't, SIGINT would be really
954b725ae77Skettenis      processed only the next time through the event loop.  To get to
955b725ae77Skettenis      that point, though, the command that we want to interrupt needs to
956b725ae77Skettenis      finish first, which is unacceptable. */
957b725ae77Skettenis   if (immediate_quit)
958b725ae77Skettenis     async_request_quit (0);
959b725ae77Skettenis   else
960b725ae77Skettenis     /* If immediate quit is not set, we process SIGINT the next time
961b725ae77Skettenis        through the loop, which is fine. */
962b725ae77Skettenis     mark_async_signal_handler_wrapper (sigint_token);
963b725ae77Skettenis }
964b725ae77Skettenis 
965b725ae77Skettenis /* Do the quit. All the checks have been done by the caller. */
966b725ae77Skettenis void
async_request_quit(gdb_client_data arg)967b725ae77Skettenis async_request_quit (gdb_client_data arg)
968b725ae77Skettenis {
969b725ae77Skettenis   quit_flag = 1;
970b725ae77Skettenis   quit ();
971b725ae77Skettenis }
972b725ae77Skettenis 
973b725ae77Skettenis /* Tell the event loop what to do if SIGQUIT is received.
974b725ae77Skettenis    See event-signal.c. */
975b725ae77Skettenis static void
handle_sigquit(int sig)976b725ae77Skettenis handle_sigquit (int sig)
977b725ae77Skettenis {
978b725ae77Skettenis   mark_async_signal_handler_wrapper (sigquit_token);
979b725ae77Skettenis   signal (sig, handle_sigquit);
980b725ae77Skettenis }
981b725ae77Skettenis 
982b725ae77Skettenis /* Called by the event loop in response to a SIGQUIT. */
983b725ae77Skettenis static void
async_do_nothing(gdb_client_data arg)984b725ae77Skettenis async_do_nothing (gdb_client_data arg)
985b725ae77Skettenis {
986b725ae77Skettenis   /* Empty function body. */
987b725ae77Skettenis }
988b725ae77Skettenis 
989b725ae77Skettenis #ifdef SIGHUP
990b725ae77Skettenis /* Tell the event loop what to do if SIGHUP is received.
991b725ae77Skettenis    See event-signal.c. */
992b725ae77Skettenis static void
handle_sighup(int sig)993b725ae77Skettenis handle_sighup (int sig)
994b725ae77Skettenis {
995b725ae77Skettenis   mark_async_signal_handler_wrapper (sighup_token);
996b725ae77Skettenis   signal (sig, handle_sighup);
997b725ae77Skettenis }
998b725ae77Skettenis 
999b725ae77Skettenis /* Called by the event loop to process a SIGHUP */
1000b725ae77Skettenis static void
async_disconnect(gdb_client_data arg)1001b725ae77Skettenis async_disconnect (gdb_client_data arg)
1002b725ae77Skettenis {
1003b725ae77Skettenis   catch_errors (quit_cover, NULL,
1004b725ae77Skettenis 		"Could not kill the program being debugged",
1005b725ae77Skettenis 		RETURN_MASK_ALL);
1006b725ae77Skettenis   signal (SIGHUP, SIG_DFL);	/*FIXME: ??????????? */
1007b725ae77Skettenis   kill (getpid (), SIGHUP);
1008b725ae77Skettenis }
1009b725ae77Skettenis #endif
1010b725ae77Skettenis 
1011b725ae77Skettenis #ifdef STOP_SIGNAL
1012b725ae77Skettenis void
handle_stop_sig(int sig)1013b725ae77Skettenis handle_stop_sig (int sig)
1014b725ae77Skettenis {
1015b725ae77Skettenis   mark_async_signal_handler_wrapper (sigtstp_token);
1016b725ae77Skettenis   signal (sig, handle_stop_sig);
1017b725ae77Skettenis }
1018b725ae77Skettenis 
1019b725ae77Skettenis static void
async_stop_sig(gdb_client_data arg)1020b725ae77Skettenis async_stop_sig (gdb_client_data arg)
1021b725ae77Skettenis {
1022b725ae77Skettenis   char *prompt = get_prompt ();
1023b725ae77Skettenis #if STOP_SIGNAL == SIGTSTP
1024b725ae77Skettenis   signal (SIGTSTP, SIG_DFL);
1025b725ae77Skettenis #if HAVE_SIGPROCMASK
1026b725ae77Skettenis   {
1027b725ae77Skettenis     sigset_t zero;
1028b725ae77Skettenis 
1029b725ae77Skettenis     sigemptyset (&zero);
1030b725ae77Skettenis     sigprocmask (SIG_SETMASK, &zero, 0);
1031b725ae77Skettenis   }
1032b725ae77Skettenis #elif HAVE_SIGSETMASK
1033b725ae77Skettenis   sigsetmask (0);
1034b725ae77Skettenis #endif
1035b725ae77Skettenis   kill (getpid (), SIGTSTP);
1036b725ae77Skettenis   signal (SIGTSTP, handle_stop_sig);
1037b725ae77Skettenis #else
1038b725ae77Skettenis   signal (STOP_SIGNAL, handle_stop_sig);
1039b725ae77Skettenis #endif
1040b725ae77Skettenis   printf_unfiltered ("%s", prompt);
1041b725ae77Skettenis   gdb_flush (gdb_stdout);
1042b725ae77Skettenis 
1043b725ae77Skettenis   /* Forget about any previous command -- null line now will do nothing.  */
1044b725ae77Skettenis   dont_repeat ();
1045b725ae77Skettenis }
1046b725ae77Skettenis #endif /* STOP_SIGNAL */
1047b725ae77Skettenis 
1048b725ae77Skettenis /* Tell the event loop what to do if SIGFPE is received.
1049b725ae77Skettenis    See event-signal.c. */
1050b725ae77Skettenis static void
handle_sigfpe(int sig)1051b725ae77Skettenis handle_sigfpe (int sig)
1052b725ae77Skettenis {
1053b725ae77Skettenis   mark_async_signal_handler_wrapper (sigfpe_token);
1054b725ae77Skettenis   signal (sig, handle_sigfpe);
1055b725ae77Skettenis }
1056b725ae77Skettenis 
1057b725ae77Skettenis /* Event loop will call this functin to process a SIGFPE. */
1058b725ae77Skettenis static void
async_float_handler(gdb_client_data arg)1059b725ae77Skettenis async_float_handler (gdb_client_data arg)
1060b725ae77Skettenis {
1061b725ae77Skettenis   /* This message is based on ANSI C, section 4.7. Note that integer
1062b725ae77Skettenis      divide by zero causes this, so "float" is a misnomer. */
1063b725ae77Skettenis   error ("Erroneous arithmetic operation.");
1064b725ae77Skettenis }
1065b725ae77Skettenis 
1066b725ae77Skettenis /* Tell the event loop what to do if SIGWINCH is received.
1067b725ae77Skettenis    See event-signal.c. */
1068b725ae77Skettenis #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1069b725ae77Skettenis static void
handle_sigwinch(int sig)1070b725ae77Skettenis handle_sigwinch (int sig)
1071b725ae77Skettenis {
1072b725ae77Skettenis   mark_async_signal_handler_wrapper (sigwinch_token);
1073b725ae77Skettenis   signal (sig, handle_sigwinch);
1074b725ae77Skettenis }
1075b725ae77Skettenis #endif
1076b725ae77Skettenis 
1077b725ae77Skettenis 
1078b725ae77Skettenis /* Called by do_setshow_command.  */
1079b725ae77Skettenis void
set_async_editing_command(char * args,int from_tty,struct cmd_list_element * c)1080b725ae77Skettenis set_async_editing_command (char *args, int from_tty, struct cmd_list_element *c)
1081b725ae77Skettenis {
1082b725ae77Skettenis   change_line_handler ();
1083b725ae77Skettenis }
1084b725ae77Skettenis 
1085b725ae77Skettenis /* Called by do_setshow_command.  */
1086b725ae77Skettenis void
set_async_annotation_level(char * args,int from_tty,struct cmd_list_element * c)1087b725ae77Skettenis set_async_annotation_level (char *args, int from_tty, struct cmd_list_element *c)
1088b725ae77Skettenis {
1089b725ae77Skettenis   change_annotation_level ();
1090b725ae77Skettenis }
1091b725ae77Skettenis 
1092b725ae77Skettenis /* Called by do_setshow_command.  */
1093b725ae77Skettenis void
set_async_prompt(char * args,int from_tty,struct cmd_list_element * c)1094b725ae77Skettenis set_async_prompt (char *args, int from_tty, struct cmd_list_element *c)
1095b725ae77Skettenis {
1096b725ae77Skettenis   PROMPT (0) = savestring (new_async_prompt, strlen (new_async_prompt));
1097b725ae77Skettenis }
1098b725ae77Skettenis 
1099b725ae77Skettenis /* Set things up for readline to be invoked via the alternate
1100b725ae77Skettenis    interface, i.e. via a callback function (rl_callback_read_char),
1101b725ae77Skettenis    and hook up instream to the event loop. */
1102b725ae77Skettenis void
gdb_setup_readline(void)1103b725ae77Skettenis gdb_setup_readline (void)
1104b725ae77Skettenis {
1105*11efff7fSkettenis   /* This function is a noop for the sync case.  The assumption is
1106*11efff7fSkettenis      that the sync setup is ALL done in gdb_init, and we would only
1107*11efff7fSkettenis      mess it up here.  The sync stuff should really go away over
1108*11efff7fSkettenis      time.  */
1109b725ae77Skettenis 
1110b725ae77Skettenis   gdb_stdout = stdio_fileopen (stdout);
1111b725ae77Skettenis   gdb_stderr = stdio_fileopen (stderr);
1112b725ae77Skettenis   gdb_stdlog = gdb_stderr;  /* for moment */
1113b725ae77Skettenis   gdb_stdtarg = gdb_stderr; /* for moment */
1114b725ae77Skettenis 
1115b725ae77Skettenis   /* If the input stream is connected to a terminal, turn on
1116b725ae77Skettenis      editing.  */
1117b725ae77Skettenis   if (ISATTY (instream))
1118b725ae77Skettenis     {
1119b725ae77Skettenis       /* Tell gdb that we will be using the readline library. This
1120b725ae77Skettenis 	 could be overwritten by a command in .gdbinit like 'set
1121b725ae77Skettenis 	 editing on' or 'off'.  */
1122b725ae77Skettenis       async_command_editing_p = 1;
1123b725ae77Skettenis 
1124*11efff7fSkettenis       /* When a character is detected on instream by select or poll,
1125*11efff7fSkettenis 	 readline will be invoked via this callback function.  */
1126b725ae77Skettenis       call_readline = rl_callback_read_char_wrapper;
1127b725ae77Skettenis     }
1128b725ae77Skettenis   else
1129b725ae77Skettenis     {
1130b725ae77Skettenis       async_command_editing_p = 0;
1131b725ae77Skettenis       call_readline = gdb_readline2;
1132b725ae77Skettenis     }
1133b725ae77Skettenis 
1134*11efff7fSkettenis   /* When readline has read an end-of-line character, it passes the
1135*11efff7fSkettenis      complete line to gdb for processing. command_line_handler is the
1136*11efff7fSkettenis      function that does this.  */
1137b725ae77Skettenis   input_handler = command_line_handler;
1138b725ae77Skettenis 
1139b725ae77Skettenis   /* Tell readline to use the same input stream that gdb uses. */
1140b725ae77Skettenis   rl_instream = instream;
1141b725ae77Skettenis 
1142b725ae77Skettenis   /* Get a file descriptor for the input stream, so that we can
1143b725ae77Skettenis      register it with the event loop.  */
1144b725ae77Skettenis   input_fd = fileno (instream);
1145b725ae77Skettenis 
1146b725ae77Skettenis   /* Now we need to create the event sources for the input file
1147b725ae77Skettenis      descriptor.  */
1148b725ae77Skettenis   /* At this point in time, this is the only event source that we
1149*11efff7fSkettenis      register with the even loop. Another source is going to be the
1150*11efff7fSkettenis      target program (inferior), but that must be registered only when
1151*11efff7fSkettenis      it actually exists (I.e. after we say 'run' or after we connect
1152*11efff7fSkettenis      to a remote target.  */
1153b725ae77Skettenis   add_file_handler (input_fd, stdin_event_handler, 0);
1154b725ae77Skettenis }
1155b725ae77Skettenis 
1156b725ae77Skettenis /* Disable command input through the standard CLI channels.  Used in
1157b725ae77Skettenis    the suspend proc for interpreters that use the standard gdb readline
1158b725ae77Skettenis    interface, like the cli & the mi.  */
1159b725ae77Skettenis void
gdb_disable_readline(void)1160b725ae77Skettenis gdb_disable_readline (void)
1161b725ae77Skettenis {
1162*11efff7fSkettenis   /* FIXME - It is too heavyweight to delete and remake these every
1163*11efff7fSkettenis      time you run an interpreter that needs readline.  It is probably
1164*11efff7fSkettenis      better to have the interpreters cache these, which in turn means
1165*11efff7fSkettenis      that this needs to be moved into interpreter specific code.  */
1166b725ae77Skettenis 
1167b725ae77Skettenis #if 0
1168b725ae77Skettenis   ui_file_delete (gdb_stdout);
1169b725ae77Skettenis   ui_file_delete (gdb_stderr);
1170b725ae77Skettenis   gdb_stdlog = NULL;
1171b725ae77Skettenis   gdb_stdtarg = NULL;
1172b725ae77Skettenis #endif
1173b725ae77Skettenis 
1174b725ae77Skettenis   rl_callback_handler_remove ();
1175b725ae77Skettenis   delete_file_handler (input_fd);
1176b725ae77Skettenis }
1177