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