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