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