* target.h: Add enum target_waitkind, enum target_signal, and
[deliverable/binutils-gdb.git] / gdb / infrun.c
1 /* Target-struct-independent code to start (run) and stop an inferior process.
2 Copyright 1986, 1987, 1988, 1989, 1991, 1992, 1993
3 Free Software Foundation, Inc.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include <string.h>
23 #include <ctype.h>
24 #include "symtab.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "breakpoint.h"
28 #include "wait.h"
29 #include "gdbcore.h"
30 #include "gdbcmd.h"
31 #include "target.h"
32 #include "thread.h"
33
34 #include <signal.h>
35
36 /* unistd.h is needed to #define X_OK */
37 #ifdef USG
38 #include <unistd.h>
39 #else
40 #include <sys/file.h>
41 #endif
42
43 /* Prototypes for local functions */
44
45 static void
46 signals_info PARAMS ((char *, int));
47
48 static void
49 handle_command PARAMS ((char *, int));
50
51 static void sig_print_info PARAMS ((enum target_signal));
52
53 static void
54 sig_print_header PARAMS ((void));
55
56 static void
57 resume_cleanups PARAMS ((int));
58
59 static int
60 hook_stop_stub PARAMS ((char *));
61
62 /* GET_LONGJMP_TARGET returns the PC at which longjmp() will resume the
63 program. It needs to examine the jmp_buf argument and extract the PC
64 from it. The return value is non-zero on success, zero otherwise. */
65 #ifndef GET_LONGJMP_TARGET
66 #define GET_LONGJMP_TARGET(PC_ADDR) 0
67 #endif
68
69
70 /* Some machines have trampoline code that sits between function callers
71 and the actual functions themselves. If this machine doesn't have
72 such things, disable their processing. */
73 #ifndef SKIP_TRAMPOLINE_CODE
74 #define SKIP_TRAMPOLINE_CODE(pc) 0
75 #endif
76
77 /* On Irix 5, some function calls automatically skip the first few
78 instructions, so we need a more complicated test to see if we are
79 the start of a function. */
80 #ifndef AT_FUNCTION_START
81 #define AT_FUNCTION_START(pc,func_name,func_addr) 0
82 #endif
83
84 /* For SVR4 shared libraries, each call goes through a small piece of
85 trampoline code in the ".init" section. IN_SOLIB_TRAMPOLINE evaluates
86 to nonzero if we are current stopped in one of these. */
87 #ifndef IN_SOLIB_TRAMPOLINE
88 #define IN_SOLIB_TRAMPOLINE(pc,name) 0
89 #endif
90
91 /* On some systems, the PC may be left pointing at an instruction that won't
92 actually be executed. This is usually indicated by a bit in the PSW. If
93 we find ourselves in such a state, then we step the target beyond the
94 nullified instruction before returning control to the user so as to avoid
95 confusion. */
96
97 #ifndef INSTRUCTION_NULLIFIED
98 #define INSTRUCTION_NULLIFIED 0
99 #endif
100
101 /* Tables of how to react to signals; the user sets them. */
102
103 static unsigned char *signal_stop;
104 static unsigned char *signal_print;
105 static unsigned char *signal_program;
106
107 #define SET_SIGS(nsigs,sigs,flags) \
108 do { \
109 int signum = (nsigs); \
110 while (signum-- > 0) \
111 if ((sigs)[signum]) \
112 (flags)[signum] = 1; \
113 } while (0)
114
115 #define UNSET_SIGS(nsigs,sigs,flags) \
116 do { \
117 int signum = (nsigs); \
118 while (signum-- > 0) \
119 if ((sigs)[signum]) \
120 (flags)[signum] = 0; \
121 } while (0)
122
123
124 /* Command list pointer for the "stop" placeholder. */
125
126 static struct cmd_list_element *stop_command;
127
128 /* Nonzero if breakpoints are now inserted in the inferior. */
129
130 static int breakpoints_inserted;
131
132 /* Function inferior was in as of last step command. */
133
134 static struct symbol *step_start_function;
135
136 /* Nonzero if we are expecting a trace trap and should proceed from it. */
137
138 static int trap_expected;
139
140 /* Nonzero if the next time we try to continue the inferior, it will
141 step one instruction and generate a spurious trace trap.
142 This is used to compensate for a bug in HP-UX. */
143
144 static int trap_expected_after_continue;
145
146 /* Nonzero means expecting a trace trap
147 and should stop the inferior and return silently when it happens. */
148
149 int stop_after_trap;
150
151 /* Nonzero means expecting a trap and caller will handle it themselves.
152 It is used after attach, due to attaching to a process;
153 when running in the shell before the child program has been exec'd;
154 and when running some kinds of remote stuff (FIXME?). */
155
156 int stop_soon_quietly;
157
158 /* Nonzero if proceed is being used for a "finish" command or a similar
159 situation when stop_registers should be saved. */
160
161 int proceed_to_finish;
162
163 /* Save register contents here when about to pop a stack dummy frame,
164 if-and-only-if proceed_to_finish is set.
165 Thus this contains the return value from the called function (assuming
166 values are returned in a register). */
167
168 char stop_registers[REGISTER_BYTES];
169
170 /* Nonzero if program stopped due to error trying to insert breakpoints. */
171
172 static int breakpoints_failed;
173
174 /* Nonzero after stop if current stack frame should be printed. */
175
176 static int stop_print_frame;
177
178 #ifdef NO_SINGLE_STEP
179 extern int one_stepped; /* From machine dependent code */
180 extern void single_step (); /* Same. */
181 #endif /* NO_SINGLE_STEP */
182
183 \f
184 /* Things to clean up if we QUIT out of resume (). */
185 /* ARGSUSED */
186 static void
187 resume_cleanups (arg)
188 int arg;
189 {
190 normal_stop ();
191 }
192
193 /* Resume the inferior, but allow a QUIT. This is useful if the user
194 wants to interrupt some lengthy single-stepping operation
195 (for child processes, the SIGINT goes to the inferior, and so
196 we get a SIGINT random_signal, but for remote debugging and perhaps
197 other targets, that's not true).
198
199 STEP nonzero if we should step (zero to continue instead).
200 SIG is the signal to give the inferior (zero for none). */
201 void
202 resume (step, sig)
203 int step;
204 enum target_signal sig;
205 {
206 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
207 QUIT;
208
209 #ifdef CANNOT_STEP_BREAKPOINT
210 /* Most targets can step a breakpoint instruction, thus executing it
211 normally. But if this one cannot, just continue and we will hit
212 it anyway. */
213 if (step && breakpoints_inserted && breakpoint_here_p (read_pc ()))
214 step = 0;
215 #endif
216
217 #ifdef NO_SINGLE_STEP
218 if (step) {
219 single_step(sig); /* Do it the hard way, w/temp breakpoints */
220 step = 0; /* ...and don't ask hardware to do it. */
221 }
222 #endif
223
224 /* Handle any optimized stores to the inferior NOW... */
225 #ifdef DO_DEFERRED_STORES
226 DO_DEFERRED_STORES;
227 #endif
228
229 /* Install inferior's terminal modes. */
230 target_terminal_inferior ();
231
232 target_resume (-1, step, sig);
233 discard_cleanups (old_cleanups);
234 }
235
236 \f
237 /* Clear out all variables saying what to do when inferior is continued.
238 First do this, then set the ones you want, then call `proceed'. */
239
240 void
241 clear_proceed_status ()
242 {
243 trap_expected = 0;
244 step_range_start = 0;
245 step_range_end = 0;
246 step_frame_address = 0;
247 step_over_calls = -1;
248 stop_after_trap = 0;
249 stop_soon_quietly = 0;
250 proceed_to_finish = 0;
251 breakpoint_proceeded = 1; /* We're about to proceed... */
252
253 /* Discard any remaining commands or status from previous stop. */
254 bpstat_clear (&stop_bpstat);
255 }
256
257 /* Basic routine for continuing the program in various fashions.
258
259 ADDR is the address to resume at, or -1 for resume where stopped.
260 SIGGNAL is the signal to give it, or 0 for none,
261 or -1 for act according to how it stopped.
262 STEP is nonzero if should trap after one instruction.
263 -1 means return after that and print nothing.
264 You should probably set various step_... variables
265 before calling here, if you are stepping.
266
267 You should call clear_proceed_status before calling proceed. */
268
269 void
270 proceed (addr, siggnal, step)
271 CORE_ADDR addr;
272 enum target_signal siggnal;
273 int step;
274 {
275 int oneproc = 0;
276
277 if (step > 0)
278 step_start_function = find_pc_function (read_pc ());
279 if (step < 0)
280 stop_after_trap = 1;
281
282 if (addr == (CORE_ADDR)-1)
283 {
284 /* If there is a breakpoint at the address we will resume at,
285 step one instruction before inserting breakpoints
286 so that we do not stop right away. */
287
288 if (breakpoint_here_p (read_pc ()))
289 oneproc = 1;
290 }
291 else
292 write_pc (addr);
293
294 if (trap_expected_after_continue)
295 {
296 /* If (step == 0), a trap will be automatically generated after
297 the first instruction is executed. Force step one
298 instruction to clear this condition. This should not occur
299 if step is nonzero, but it is harmless in that case. */
300 oneproc = 1;
301 trap_expected_after_continue = 0;
302 }
303
304 if (oneproc)
305 /* We will get a trace trap after one instruction.
306 Continue it automatically and insert breakpoints then. */
307 trap_expected = 1;
308 else
309 {
310 int temp = insert_breakpoints ();
311 if (temp)
312 {
313 print_sys_errmsg ("ptrace", temp);
314 error ("Cannot insert breakpoints.\n\
315 The same program may be running in another process.");
316 }
317 breakpoints_inserted = 1;
318 }
319
320 if (siggnal >= 0)
321 stop_signal = siggnal;
322 /* If this signal should not be seen by program,
323 give it zero. Used for debugging signals. */
324 else if (!signal_program[stop_signal])
325 stop_signal= 0;
326
327 /* Resume inferior. */
328 resume (oneproc || step || bpstat_should_step (), stop_signal);
329
330 /* Wait for it to stop (if not standalone)
331 and in any case decode why it stopped, and act accordingly. */
332
333 wait_for_inferior ();
334 normal_stop ();
335 }
336
337 /* Record the pc and sp of the program the last time it stopped.
338 These are just used internally by wait_for_inferior, but need
339 to be preserved over calls to it and cleared when the inferior
340 is started. */
341 static CORE_ADDR prev_pc;
342 static CORE_ADDR prev_sp;
343 static CORE_ADDR prev_func_start;
344 static char *prev_func_name;
345
346 \f
347 /* Start remote-debugging of a machine over a serial link. */
348
349 void
350 start_remote ()
351 {
352 init_wait_for_inferior ();
353 clear_proceed_status ();
354 stop_soon_quietly = 1;
355 trap_expected = 0;
356 wait_for_inferior ();
357 normal_stop ();
358 }
359
360 /* Initialize static vars when a new inferior begins. */
361
362 void
363 init_wait_for_inferior ()
364 {
365 /* These are meaningless until the first time through wait_for_inferior. */
366 prev_pc = 0;
367 prev_sp = 0;
368 prev_func_start = 0;
369 prev_func_name = NULL;
370
371 trap_expected_after_continue = 0;
372 breakpoints_inserted = 0;
373 breakpoint_init_inferior ();
374
375 /* Don't confuse first call to proceed(). */
376 stop_signal = TARGET_SIGNAL_0;
377 }
378
379 static void
380 delete_breakpoint_current_contents (arg)
381 PTR arg;
382 {
383 struct breakpoint **breakpointp = (struct breakpoint **)arg;
384 if (*breakpointp != NULL)
385 delete_breakpoint (*breakpointp);
386 }
387 \f
388 /* Wait for control to return from inferior to debugger.
389 If inferior gets a signal, we may decide to start it up again
390 instead of returning. That is why there is a loop in this function.
391 When this function actually returns it means the inferior
392 should be left stopped and GDB should read more commands. */
393
394 void
395 wait_for_inferior ()
396 {
397 struct cleanup *old_cleanups;
398 struct target_waitstatus w;
399 int another_trap;
400 int random_signal;
401 CORE_ADDR stop_sp = 0;
402 CORE_ADDR stop_func_start;
403 CORE_ADDR stop_func_end;
404 char *stop_func_name;
405 CORE_ADDR prologue_pc = 0, tmp;
406 struct symtab_and_line sal;
407 int remove_breakpoints_on_following_step = 0;
408 int current_line;
409 struct symtab *current_symtab;
410 int handling_longjmp = 0; /* FIXME */
411 struct breakpoint *step_resume_breakpoint = NULL;
412 int pid;
413
414 old_cleanups = make_cleanup (delete_breakpoint_current_contents,
415 &step_resume_breakpoint);
416 sal = find_pc_line(prev_pc, 0);
417 current_line = sal.line;
418 current_symtab = sal.symtab;
419
420 /* Are we stepping? */
421 #define CURRENTLY_STEPPING() ((step_resume_breakpoint == NULL \
422 && !handling_longjmp \
423 && (step_range_end \
424 || trap_expected)) \
425 || bpstat_should_step ())
426
427 while (1)
428 {
429 /* Clean up saved state that will become invalid. */
430 flush_cached_frames ();
431 registers_changed ();
432
433 pid = target_wait (-1, &w);
434
435 #ifdef SIGTRAP_STOP_AFTER_LOAD
436
437 /* Somebody called load(2), and it gave us a "trap signal after load".
438 Ignore it gracefully. */
439
440 SIGTRAP_STOP_AFTER_LOAD (w);
441 #endif
442
443 /* See if the process still exists; clean up if it doesn't. */
444 if (w.kind == TARGET_WAITKIND_EXITED)
445 {
446 target_terminal_ours (); /* Must do this before mourn anyway */
447 if (w.value.integer)
448 printf_filtered ("\nProgram exited with code 0%o.\n",
449 (unsigned int)w.value.integer);
450 else
451 if (!batch_mode())
452 printf_filtered ("\nProgram exited normally.\n");
453 gdb_flush (gdb_stdout);
454 target_mourn_inferior ();
455 #ifdef NO_SINGLE_STEP
456 one_stepped = 0;
457 #endif
458 stop_print_frame = 0;
459 break;
460 }
461 else if (w.kind == TARGET_WAITKIND_SIGNALLED)
462 {
463 char *signame;
464
465 stop_print_frame = 0;
466 stop_signal = w.value.sig;
467 target_terminal_ours (); /* Must do this before mourn anyway */
468 target_kill (); /* kill mourns as well */
469 printf_filtered ("\nProgram terminated with signal %s, %s.\n",
470 target_signal_to_name (stop_signal),
471 target_signal_to_string (stop_signal));
472
473 printf_filtered ("The program no longer exists.\n");
474 gdb_flush (gdb_stdout);
475 #ifdef NO_SINGLE_STEP
476 one_stepped = 0;
477 #endif
478 break;
479 }
480
481 stop_signal = w.value.sig;
482
483 if (pid != inferior_pid)
484 {
485 int save_pid = inferior_pid;
486
487 inferior_pid = pid; /* Setup for target memory/regs */
488 registers_changed ();
489 stop_pc = read_pc ();
490 inferior_pid = save_pid;
491 registers_changed ();
492 }
493 else
494 stop_pc = read_pc ();
495
496 if (stop_signal == TARGET_SIGNAL_TRAP
497 && breakpoint_here_p (stop_pc - DECR_PC_AFTER_BREAK))
498 {
499 if (!breakpoint_thread_match (stop_pc - DECR_PC_AFTER_BREAK, pid))
500 {
501 /* Saw a breakpoint, but it was hit by the wrong thread. Just continue. */
502 if (breakpoints_inserted)
503 {
504 if (pid != inferior_pid)
505 {
506 int save_pid = inferior_pid;
507
508 inferior_pid = pid;
509 registers_changed ();
510 write_pc (stop_pc - DECR_PC_AFTER_BREAK);
511 inferior_pid = save_pid;
512 registers_changed ();
513 }
514 else
515 write_pc (stop_pc - DECR_PC_AFTER_BREAK);
516
517 remove_breakpoints ();
518 target_resume (pid, 1, TARGET_SIGNAL_0); /* Single step */
519 /* FIXME: What if a signal arrives instead of the single-step
520 happening? */
521 target_wait (pid, &w);
522 insert_breakpoints ();
523 }
524 target_resume (-1, 0, TARGET_SIGNAL_0);
525 continue;
526 }
527 else
528 if (pid != inferior_pid)
529 goto switch_thread;
530 }
531
532 if (pid != inferior_pid)
533 {
534 int printed = 0;
535
536 if (!in_thread_list (pid))
537 {
538 fprintf_unfiltered (gdb_stderr, "[New %s]\n", target_pid_to_str (pid));
539 add_thread (pid);
540
541 target_resume (-1, 0, TARGET_SIGNAL_0);
542 continue;
543 }
544 else
545 {
546 if (signal_print[stop_signal])
547 {
548 char *signame;
549
550 printed = 1;
551 target_terminal_ours_for_output ();
552 printf_filtered ("\nProgram received signal %s, %s.\n",
553 target_signal_to_name (stop_signal),
554 target_signal_to_string (stop_signal));
555 gdb_flush (gdb_stdout);
556 }
557
558 if (stop_signal == TARGET_SIGNAL_TRAP
559 || signal_stop[stop_signal])
560 {
561 switch_thread:
562 inferior_pid = pid;
563 printf_filtered ("[Switching to %s]\n", target_pid_to_str (pid));
564
565 flush_cached_frames ();
566 registers_changed ();
567 trap_expected = 0;
568 if (step_resume_breakpoint)
569 {
570 delete_breakpoint (step_resume_breakpoint);
571 step_resume_breakpoint = NULL;
572 }
573 prev_pc = 0;
574 prev_sp = 0;
575 prev_func_name = NULL;
576 step_range_start = 0;
577 step_range_end = 0;
578 step_frame_address = 0;
579 handling_longjmp = 0;
580 another_trap = 0;
581 }
582 else
583 {
584 if (printed)
585 target_terminal_inferior ();
586
587 /* Clear the signal if it should not be passed. */
588 if (signal_program[stop_signal] == 0)
589 stop_signal = TARGET_SIGNAL_0;
590
591 target_resume (pid, 0, stop_signal);
592 continue;
593 }
594 }
595 }
596
597 #ifdef NO_SINGLE_STEP
598 if (one_stepped)
599 single_step (0); /* This actually cleans up the ss */
600 #endif /* NO_SINGLE_STEP */
601
602 /* If PC is pointing at a nullified instruction, then step beyond it so that
603 the user won't be confused when GDB appears to be ready to execute it. */
604
605 if (INSTRUCTION_NULLIFIED)
606 {
607 resume (1, 0);
608 continue;
609 }
610
611 set_current_frame ( create_new_frame (read_fp (), stop_pc));
612
613 stop_frame_address = FRAME_FP (get_current_frame ());
614 stop_sp = read_sp ();
615 stop_func_start = 0;
616 stop_func_name = 0;
617 /* Don't care about return value; stop_func_start and stop_func_name
618 will both be 0 if it doesn't work. */
619 find_pc_partial_function (stop_pc, &stop_func_name, &stop_func_start,
620 &stop_func_end);
621 stop_func_start += FUNCTION_START_OFFSET;
622 another_trap = 0;
623 bpstat_clear (&stop_bpstat);
624 stop_step = 0;
625 stop_stack_dummy = 0;
626 stop_print_frame = 1;
627 random_signal = 0;
628 stopped_by_random_signal = 0;
629 breakpoints_failed = 0;
630
631 /* Look at the cause of the stop, and decide what to do.
632 The alternatives are:
633 1) break; to really stop and return to the debugger,
634 2) drop through to start up again
635 (set another_trap to 1 to single step once)
636 3) set random_signal to 1, and the decision between 1 and 2
637 will be made according to the signal handling tables. */
638
639 /* First, distinguish signals caused by the debugger from signals
640 that have to do with the program's own actions.
641 Note that breakpoint insns may cause SIGTRAP or SIGILL
642 or SIGEMT, depending on the operating system version.
643 Here we detect when a SIGILL or SIGEMT is really a breakpoint
644 and change it to SIGTRAP. */
645
646 if (stop_signal == TARGET_SIGNAL_TRAP
647 || (breakpoints_inserted &&
648 (stop_signal == TARGET_SIGNAL_ILL
649 || stop_signal == TARGET_SIGNAL_EMT
650 ))
651 || stop_soon_quietly)
652 {
653 if (stop_signal == TARGET_SIGNAL_TRAP && stop_after_trap)
654 {
655 stop_print_frame = 0;
656 break;
657 }
658 if (stop_soon_quietly)
659 break;
660
661 /* Don't even think about breakpoints
662 if just proceeded over a breakpoint.
663
664 However, if we are trying to proceed over a breakpoint
665 and end up in sigtramp, then step_resume_breakpoint
666 will be set and we should check whether we've hit the
667 step breakpoint. */
668 if (stop_signal == TARGET_SIGNAL_TRAP && trap_expected
669 && step_resume_breakpoint == NULL)
670 bpstat_clear (&stop_bpstat);
671 else
672 {
673 /* See if there is a breakpoint at the current PC. */
674 stop_bpstat = bpstat_stop_status
675 (&stop_pc, stop_frame_address,
676 #if DECR_PC_AFTER_BREAK
677 /* Notice the case of stepping through a jump
678 that lands just after a breakpoint.
679 Don't confuse that with hitting the breakpoint.
680 What we check for is that 1) stepping is going on
681 and 2) the pc before the last insn does not match
682 the address of the breakpoint before the current pc. */
683 (prev_pc != stop_pc - DECR_PC_AFTER_BREAK
684 && CURRENTLY_STEPPING ())
685 #else /* DECR_PC_AFTER_BREAK zero */
686 0
687 #endif /* DECR_PC_AFTER_BREAK zero */
688 );
689 /* Following in case break condition called a
690 function. */
691 stop_print_frame = 1;
692 }
693
694 if (stop_signal == TARGET_SIGNAL_TRAP)
695 random_signal
696 = !(bpstat_explains_signal (stop_bpstat)
697 || trap_expected
698 #ifndef CALL_DUMMY_BREAKPOINT_OFFSET
699 || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
700 #endif /* No CALL_DUMMY_BREAKPOINT_OFFSET. */
701 || (step_range_end && step_resume_breakpoint == NULL));
702 else
703 {
704 random_signal
705 = !(bpstat_explains_signal (stop_bpstat)
706 /* End of a stack dummy. Some systems (e.g. Sony
707 news) give another signal besides SIGTRAP,
708 so check here as well as above. */
709 #ifndef CALL_DUMMY_BREAKPOINT_OFFSET
710 || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
711 #endif /* No CALL_DUMMY_BREAKPOINT_OFFSET. */
712 );
713 if (!random_signal)
714 stop_signal = TARGET_SIGNAL_TRAP;
715 }
716 }
717 else
718 random_signal = 1;
719
720 /* For the program's own signals, act according to
721 the signal handling tables. */
722
723 if (random_signal)
724 {
725 /* Signal not for debugging purposes. */
726 int printed = 0;
727
728 stopped_by_random_signal = 1;
729
730 if (signal_print[stop_signal])
731 {
732 char *signame;
733 printed = 1;
734 target_terminal_ours_for_output ();
735 printf_filtered ("\nProgram received signal %s, %s.\n",
736 target_signal_to_name (stop_signal),
737 target_signal_to_string (stop_signal));
738 gdb_flush (gdb_stdout);
739 }
740 if (signal_stop[stop_signal])
741 break;
742 /* If not going to stop, give terminal back
743 if we took it away. */
744 else if (printed)
745 target_terminal_inferior ();
746
747 /* Clear the signal if it should not be passed. */
748 if (signal_program[stop_signal] == 0)
749 stop_signal = TARGET_SIGNAL_0;
750
751 /* I'm not sure whether this needs to be check_sigtramp2 or
752 whether it could/should be keep_going. */
753 goto check_sigtramp2;
754 }
755
756 /* Handle cases caused by hitting a breakpoint. */
757 {
758 CORE_ADDR jmp_buf_pc;
759 struct bpstat_what what;
760
761 what = bpstat_what (stop_bpstat);
762
763 if (what.call_dummy)
764 {
765 stop_stack_dummy = 1;
766 #ifdef HP_OS_BUG
767 trap_expected_after_continue = 1;
768 #endif
769 }
770
771 switch (what.main_action)
772 {
773 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
774 /* If we hit the breakpoint at longjmp, disable it for the
775 duration of this command. Then, install a temporary
776 breakpoint at the target of the jmp_buf. */
777 disable_longjmp_breakpoint();
778 remove_breakpoints ();
779 breakpoints_inserted = 0;
780 if (!GET_LONGJMP_TARGET(&jmp_buf_pc)) goto keep_going;
781
782 /* Need to blow away step-resume breakpoint, as it
783 interferes with us */
784 if (step_resume_breakpoint != NULL)
785 {
786 delete_breakpoint (step_resume_breakpoint);
787 step_resume_breakpoint = NULL;
788 what.step_resume = 0;
789 }
790
791 #if 0
792 /* FIXME - Need to implement nested temporary breakpoints */
793 if (step_over_calls > 0)
794 set_longjmp_resume_breakpoint(jmp_buf_pc,
795 get_current_frame());
796 else
797 #endif /* 0 */
798 set_longjmp_resume_breakpoint(jmp_buf_pc, NULL);
799 handling_longjmp = 1; /* FIXME */
800 goto keep_going;
801
802 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
803 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
804 remove_breakpoints ();
805 breakpoints_inserted = 0;
806 #if 0
807 /* FIXME - Need to implement nested temporary breakpoints */
808 if (step_over_calls
809 && (stop_frame_address
810 INNER_THAN step_frame_address))
811 {
812 another_trap = 1;
813 goto keep_going;
814 }
815 #endif /* 0 */
816 disable_longjmp_breakpoint();
817 handling_longjmp = 0; /* FIXME */
818 if (what.main_action == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
819 break;
820 /* else fallthrough */
821
822 case BPSTAT_WHAT_SINGLE:
823 if (breakpoints_inserted)
824 remove_breakpoints ();
825 breakpoints_inserted = 0;
826 another_trap = 1;
827 /* Still need to check other stuff, at least the case
828 where we are stepping and step out of the right range. */
829 break;
830
831 case BPSTAT_WHAT_STOP_NOISY:
832 stop_print_frame = 1;
833 /* We are about to nuke the step_resume_breakpoint via the
834 cleanup chain, so no need to worry about it here. */
835 goto stop_stepping;
836
837 case BPSTAT_WHAT_STOP_SILENT:
838 stop_print_frame = 0;
839 /* We are about to nuke the step_resume_breakpoint via the
840 cleanup chain, so no need to worry about it here. */
841 goto stop_stepping;
842
843 case BPSTAT_WHAT_LAST:
844 /* Not a real code, but listed here to shut up gcc -Wall. */
845
846 case BPSTAT_WHAT_KEEP_CHECKING:
847 break;
848 }
849
850 if (what.step_resume)
851 {
852 delete_breakpoint (step_resume_breakpoint);
853 step_resume_breakpoint = NULL;
854
855 /* If were waiting for a trap, hitting the step_resume_break
856 doesn't count as getting it. */
857 if (trap_expected)
858 another_trap = 1;
859 }
860 }
861
862 /* We come here if we hit a breakpoint but should not
863 stop for it. Possibly we also were stepping
864 and should stop for that. So fall through and
865 test for stepping. But, if not stepping,
866 do not stop. */
867
868 #ifndef CALL_DUMMY_BREAKPOINT_OFFSET
869 /* This is the old way of detecting the end of the stack dummy.
870 An architecture which defines CALL_DUMMY_BREAKPOINT_OFFSET gets
871 handled above. As soon as we can test it on all of them, all
872 architectures should define it. */
873
874 /* If this is the breakpoint at the end of a stack dummy,
875 just stop silently, unless the user was doing an si/ni, in which
876 case she'd better know what she's doing. */
877
878 if (PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
879 && !step_range_end)
880 {
881 stop_print_frame = 0;
882 stop_stack_dummy = 1;
883 #ifdef HP_OS_BUG
884 trap_expected_after_continue = 1;
885 #endif
886 break;
887 }
888 #endif /* No CALL_DUMMY_BREAKPOINT_OFFSET. */
889
890 if (step_resume_breakpoint)
891 /* Having a step-resume breakpoint overrides anything
892 else having to do with stepping commands until
893 that breakpoint is reached. */
894 /* I suspect this could/should be keep_going, because if the
895 check_sigtramp2 check succeeds, then it will put in another
896 step_resume_breakpoint, and we aren't (yet) prepared to nest
897 them. */
898 goto check_sigtramp2;
899
900 if (step_range_end == 0)
901 /* Likewise if we aren't even stepping. */
902 /* I'm not sure whether this needs to be check_sigtramp2 or
903 whether it could/should be keep_going. */
904 goto check_sigtramp2;
905
906 /* If stepping through a line, keep going if still within it. */
907 if (stop_pc >= step_range_start
908 && stop_pc < step_range_end
909 /* The step range might include the start of the
910 function, so if we are at the start of the
911 step range and either the stack or frame pointers
912 just changed, we've stepped outside */
913 && !(stop_pc == step_range_start
914 && stop_frame_address
915 && (stop_sp INNER_THAN prev_sp
916 || stop_frame_address != step_frame_address)))
917 {
918 /* We might be doing a BPSTAT_WHAT_SINGLE and getting a signal.
919 So definately need to check for sigtramp here. */
920 goto check_sigtramp2;
921 }
922
923 /* We stepped out of the stepping range. See if that was due
924 to a subroutine call that we should proceed to the end of. */
925
926 /* Did we just take a signal? */
927 if (IN_SIGTRAMP (stop_pc, stop_func_name)
928 && !IN_SIGTRAMP (prev_pc, prev_func_name))
929 {
930 /* This code is needed at least in the following case:
931 The user types "next" and then a signal arrives (before
932 the "next" is done). */
933 /* We've just taken a signal; go until we are back to
934 the point where we took it and one more. */
935 {
936 struct symtab_and_line sr_sal;
937
938 sr_sal.pc = prev_pc;
939 sr_sal.symtab = NULL;
940 sr_sal.line = 0;
941 step_resume_breakpoint =
942 set_momentary_breakpoint (sr_sal, get_current_frame (),
943 bp_step_resume);
944 if (breakpoints_inserted)
945 insert_breakpoints ();
946 }
947
948 /* If this is stepi or nexti, make sure that the stepping range
949 gets us past that instruction. */
950 if (step_range_end == 1)
951 /* FIXME: Does this run afoul of the code below which, if
952 we step into the middle of a line, resets the stepping
953 range? */
954 step_range_end = (step_range_start = prev_pc) + 1;
955
956 remove_breakpoints_on_following_step = 1;
957 goto keep_going;
958 }
959
960 if (stop_func_start)
961 {
962 /* Do this after the IN_SIGTRAMP check; it might give
963 an error. */
964 prologue_pc = stop_func_start;
965 SKIP_PROLOGUE (prologue_pc);
966 }
967
968 if ((/* Might be a non-recursive call. If the symbols are missing
969 enough that stop_func_start == prev_func_start even though
970 they are really two functions, we will treat some calls as
971 jumps. */
972 stop_func_start != prev_func_start
973
974 /* Might be a recursive call if either we have a prologue
975 or the call instruction itself saves the PC on the stack. */
976 || prologue_pc != stop_func_start
977 || stop_sp != prev_sp)
978 && (/* PC is completely out of bounds of any known objfiles. Treat
979 like a subroutine call. */
980 ! stop_func_start
981
982 /* If we do a call, we will be at the start of a function... */
983 || stop_pc == stop_func_start
984 #if 0
985 /* Should be taken care of by the stop_pc < prologue_pc check
986 below. Also, on irix5 where this checks for stop_pc
987 equal to stop_func_start plus 12, it would seem to be
988 wrong for a function with a 4 byte prologue, and an 8 byte
989 call; a "return" could end up at stop_func_start+12. */
990
991 || AT_FUNCTION_START (stop_pc, stop_func_name, stop_func_start)
992 #endif
993
994 /* ...except on the Alpha with -O (and also Irix 5 and
995 perhaps others), in which we might call the address
996 after the load of gp. Since prologues don't contain
997 calls, we can't return to within one, and we don't
998 jump back into them, so this check is OK. */
999
1000 || stop_pc < prologue_pc
1001
1002 /* If we end up in certain places, it means we did a subroutine
1003 call. I'm not completely sure this is necessary now that we
1004 have the above checks with stop_func_start (and now that
1005 find_pc_partial_function is pickier). */
1006 || IN_SOLIB_TRAMPOLINE (stop_pc, stop_func_name)
1007
1008 /* If none of the above apply, it is a jump within a function,
1009 or a return from a subroutine. The other case is longjmp,
1010 which can no longer happen here as long as the
1011 handling_longjmp stuff is working. */
1012 ))
1013 {
1014 /* It's a subroutine call. */
1015
1016 if (step_over_calls == 0)
1017 {
1018 /* I presume that step_over_calls is only 0 when we're
1019 supposed to be stepping at the assembly language level
1020 ("stepi"). Just stop. */
1021 stop_step = 1;
1022 break;
1023 }
1024
1025 if (step_over_calls > 0)
1026 /* We're doing a "next". */
1027 goto step_over_function;
1028
1029 /* If we are in a function call trampoline (a stub between
1030 the calling routine and the real function), locate the real
1031 function. That's what tells us (a) whether we want to step
1032 into it at all, and (b) what prologue we want to run to
1033 the end of, if we do step into it. */
1034 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
1035 if (tmp != 0)
1036 stop_func_start = tmp;
1037
1038 /* If we have line number information for the function we
1039 are thinking of stepping into, step into it.
1040
1041 If there are several symtabs at that PC (e.g. with include
1042 files), just want to know whether *any* of them have line
1043 numbers. find_pc_line handles this. */
1044 {
1045 struct symtab_and_line tmp_sal;
1046
1047 tmp_sal = find_pc_line (stop_func_start, 0);
1048 if (tmp_sal.line != 0)
1049 goto step_into_function;
1050 }
1051
1052 step_over_function:
1053 /* A subroutine call has happened. */
1054 {
1055 /* Set a special breakpoint after the return */
1056 struct symtab_and_line sr_sal;
1057 sr_sal.pc =
1058 ADDR_BITS_REMOVE
1059 (SAVED_PC_AFTER_CALL (get_current_frame ()));
1060 sr_sal.symtab = NULL;
1061 sr_sal.line = 0;
1062 step_resume_breakpoint =
1063 set_momentary_breakpoint (sr_sal, get_current_frame (),
1064 bp_step_resume);
1065 if (breakpoints_inserted)
1066 insert_breakpoints ();
1067 }
1068 goto keep_going;
1069
1070 step_into_function:
1071 /* Subroutine call with source code we should not step over.
1072 Do step to the first line of code in it. */
1073 SKIP_PROLOGUE (stop_func_start);
1074 sal = find_pc_line (stop_func_start, 0);
1075 /* Use the step_resume_break to step until
1076 the end of the prologue, even if that involves jumps
1077 (as it seems to on the vax under 4.2). */
1078 /* If the prologue ends in the middle of a source line,
1079 continue to the end of that source line (if it is still
1080 within the function). Otherwise, just go to end of prologue. */
1081 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1082 /* no, don't either. It skips any code that's
1083 legitimately on the first line. */
1084 #else
1085 if (sal.end && sal.pc != stop_func_start && sal.end < stop_func_end)
1086 stop_func_start = sal.end;
1087 #endif
1088
1089 if (stop_func_start == stop_pc)
1090 {
1091 /* We are already there: stop now. */
1092 stop_step = 1;
1093 break;
1094 }
1095 else
1096 /* Put the step-breakpoint there and go until there. */
1097 {
1098 struct symtab_and_line sr_sal;
1099
1100 sr_sal.pc = stop_func_start;
1101 sr_sal.symtab = NULL;
1102 sr_sal.line = 0;
1103 /* Do not specify what the fp should be when we stop
1104 since on some machines the prologue
1105 is where the new fp value is established. */
1106 step_resume_breakpoint =
1107 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
1108 if (breakpoints_inserted)
1109 insert_breakpoints ();
1110
1111 /* And make sure stepping stops right away then. */
1112 step_range_end = step_range_start;
1113 }
1114 goto keep_going;
1115 }
1116
1117 /* We've wandered out of the step range. */
1118
1119 sal = find_pc_line(stop_pc, 0);
1120
1121 if (step_range_end == 1)
1122 {
1123 /* It is stepi or nexti. We always want to stop stepping after
1124 one instruction. */
1125 stop_step = 1;
1126 break;
1127 }
1128
1129 if (sal.line == 0)
1130 {
1131 /* We have no line number information. That means to stop
1132 stepping (does this always happen right after one instruction,
1133 when we do "s" in a function with no line numbers,
1134 or can this happen as a result of a return or longjmp?). */
1135 stop_step = 1;
1136 break;
1137 }
1138
1139 if (stop_pc == sal.pc
1140 && (current_line != sal.line || current_symtab != sal.symtab))
1141 {
1142 /* We are at the start of a different line. So stop. Note that
1143 we don't stop if we step into the middle of a different line.
1144 That is said to make things like for (;;) statements work
1145 better. */
1146 stop_step = 1;
1147 break;
1148 }
1149
1150 /* We aren't done stepping.
1151
1152 Optimize by setting the stepping range to the line.
1153 (We might not be in the original line, but if we entered a
1154 new line in mid-statement, we continue stepping. This makes
1155 things like for(;;) statements work better.) */
1156
1157 if (stop_func_end && sal.end >= stop_func_end)
1158 {
1159 /* If this is the last line of the function, don't keep stepping
1160 (it would probably step us out of the function).
1161 This is particularly necessary for a one-line function,
1162 in which after skipping the prologue we better stop even though
1163 we will be in mid-line. */
1164 stop_step = 1;
1165 break;
1166 }
1167 step_range_start = sal.pc;
1168 step_range_end = sal.end;
1169 goto keep_going;
1170
1171 check_sigtramp2:
1172 if (trap_expected
1173 && IN_SIGTRAMP (stop_pc, stop_func_name)
1174 && !IN_SIGTRAMP (prev_pc, prev_func_name))
1175 {
1176 /* What has happened here is that we have just stepped the inferior
1177 with a signal (because it is a signal which shouldn't make
1178 us stop), thus stepping into sigtramp.
1179
1180 So we need to set a step_resume_break_address breakpoint
1181 and continue until we hit it, and then step. FIXME: This should
1182 be more enduring than a step_resume breakpoint; we should know
1183 that we will later need to keep going rather than re-hitting
1184 the breakpoint here (see testsuite/gdb.t06/signals.exp where
1185 it says "exceedingly difficult"). */
1186 struct symtab_and_line sr_sal;
1187
1188 sr_sal.pc = prev_pc;
1189 sr_sal.symtab = NULL;
1190 sr_sal.line = 0;
1191 step_resume_breakpoint =
1192 set_momentary_breakpoint (sr_sal, get_current_frame (),
1193 bp_step_resume);
1194 if (breakpoints_inserted)
1195 insert_breakpoints ();
1196
1197 remove_breakpoints_on_following_step = 1;
1198 another_trap = 1;
1199 }
1200
1201 keep_going:
1202 /* Come to this label when you need to resume the inferior.
1203 It's really much cleaner to do a goto than a maze of if-else
1204 conditions. */
1205
1206 /* Save the pc before execution, to compare with pc after stop. */
1207 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
1208 prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
1209 BREAK is defined, the
1210 original pc would not have
1211 been at the start of a
1212 function. */
1213 prev_func_name = stop_func_name;
1214 prev_sp = stop_sp;
1215
1216 /* If we did not do break;, it means we should keep
1217 running the inferior and not return to debugger. */
1218
1219 if (trap_expected && stop_signal != TARGET_SIGNAL_TRAP)
1220 {
1221 /* We took a signal (which we are supposed to pass through to
1222 the inferior, else we'd have done a break above) and we
1223 haven't yet gotten our trap. Simply continue. */
1224 resume (CURRENTLY_STEPPING (), stop_signal);
1225 }
1226 else
1227 {
1228 /* Either the trap was not expected, but we are continuing
1229 anyway (the user asked that this signal be passed to the
1230 child)
1231 -- or --
1232 The signal was SIGTRAP, e.g. it was our signal, but we
1233 decided we should resume from it.
1234
1235 We're going to run this baby now!
1236
1237 Insert breakpoints now, unless we are trying
1238 to one-proceed past a breakpoint. */
1239 /* If we've just finished a special step resume and we don't
1240 want to hit a breakpoint, pull em out. */
1241 if (step_resume_breakpoint == NULL &&
1242 remove_breakpoints_on_following_step)
1243 {
1244 remove_breakpoints_on_following_step = 0;
1245 remove_breakpoints ();
1246 breakpoints_inserted = 0;
1247 }
1248 else if (!breakpoints_inserted &&
1249 (step_resume_breakpoint != NULL || !another_trap))
1250 {
1251 breakpoints_failed = insert_breakpoints ();
1252 if (breakpoints_failed)
1253 break;
1254 breakpoints_inserted = 1;
1255 }
1256
1257 trap_expected = another_trap;
1258
1259 if (stop_signal == TARGET_SIGNAL_TRAP)
1260 stop_signal = TARGET_SIGNAL_0;
1261
1262 #ifdef SHIFT_INST_REGS
1263 /* I'm not sure when this following segment applies. I do know, now,
1264 that we shouldn't rewrite the regs when we were stopped by a
1265 random signal from the inferior process. */
1266 /* FIXME: Shouldn't this be based on the valid bit of the SXIP?
1267 (this is only used on the 88k). */
1268
1269 if (!bpstat_explains_signal (stop_bpstat)
1270 && (stop_signal != TARGET_SIGNAL_CHLD)
1271 && !stopped_by_random_signal)
1272 SHIFT_INST_REGS();
1273 #endif /* SHIFT_INST_REGS */
1274
1275 resume (CURRENTLY_STEPPING (), stop_signal);
1276 }
1277 }
1278
1279 stop_stepping:
1280 if (target_has_execution)
1281 {
1282 /* Assuming the inferior still exists, set these up for next
1283 time, just like we did above if we didn't break out of the
1284 loop. */
1285 prev_pc = read_pc ();
1286 prev_func_start = stop_func_start;
1287 prev_func_name = stop_func_name;
1288 prev_sp = stop_sp;
1289 }
1290 do_cleanups (old_cleanups);
1291 }
1292 \f
1293 /* Here to return control to GDB when the inferior stops for real.
1294 Print appropriate messages, remove breakpoints, give terminal our modes.
1295
1296 STOP_PRINT_FRAME nonzero means print the executing frame
1297 (pc, function, args, file, line number and line text).
1298 BREAKPOINTS_FAILED nonzero means stop was due to error
1299 attempting to insert breakpoints. */
1300
1301 void
1302 normal_stop ()
1303 {
1304 /* Make sure that the current_frame's pc is correct. This
1305 is a correction for setting up the frame info before doing
1306 DECR_PC_AFTER_BREAK */
1307 if (target_has_execution && get_current_frame())
1308 (get_current_frame ())->pc = read_pc ();
1309
1310 if (breakpoints_failed)
1311 {
1312 target_terminal_ours_for_output ();
1313 print_sys_errmsg ("ptrace", breakpoints_failed);
1314 printf_filtered ("Stopped; cannot insert breakpoints.\n\
1315 The same program may be running in another process.\n");
1316 }
1317
1318 if (target_has_execution && breakpoints_inserted)
1319 if (remove_breakpoints ())
1320 {
1321 target_terminal_ours_for_output ();
1322 printf_filtered ("Cannot remove breakpoints because program is no longer writable.\n\
1323 It might be running in another process.\n\
1324 Further execution is probably impossible.\n");
1325 }
1326
1327 breakpoints_inserted = 0;
1328
1329 /* Delete the breakpoint we stopped at, if it wants to be deleted.
1330 Delete any breakpoint that is to be deleted at the next stop. */
1331
1332 breakpoint_auto_delete (stop_bpstat);
1333
1334 /* If an auto-display called a function and that got a signal,
1335 delete that auto-display to avoid an infinite recursion. */
1336
1337 if (stopped_by_random_signal)
1338 disable_current_display ();
1339
1340 if (step_multi && stop_step)
1341 return;
1342
1343 target_terminal_ours ();
1344
1345 /* Look up the hook_stop and run it if it exists. */
1346
1347 if (stop_command->hook)
1348 {
1349 catch_errors (hook_stop_stub, (char *)stop_command->hook,
1350 "Error while running hook_stop:\n", RETURN_MASK_ALL);
1351 }
1352
1353 if (!target_has_stack)
1354 return;
1355
1356 /* Select innermost stack frame except on return from a stack dummy routine,
1357 or if the program has exited. Print it without a level number if
1358 we have changed functions or hit a breakpoint. Print source line
1359 if we have one. */
1360 if (!stop_stack_dummy)
1361 {
1362 select_frame (get_current_frame (), 0);
1363
1364 if (stop_print_frame)
1365 {
1366 int source_only;
1367
1368 source_only = bpstat_print (stop_bpstat);
1369 source_only = source_only ||
1370 ( stop_step
1371 && step_frame_address == stop_frame_address
1372 && step_start_function == find_pc_function (stop_pc));
1373
1374 print_stack_frame (selected_frame, -1, source_only? -1: 1);
1375
1376 /* Display the auto-display expressions. */
1377 do_displays ();
1378 }
1379 }
1380
1381 /* Save the function value return registers, if we care.
1382 We might be about to restore their previous contents. */
1383 if (proceed_to_finish)
1384 read_register_bytes (0, stop_registers, REGISTER_BYTES);
1385
1386 if (stop_stack_dummy)
1387 {
1388 /* Pop the empty frame that contains the stack dummy.
1389 POP_FRAME ends with a setting of the current frame, so we
1390 can use that next. */
1391 POP_FRAME;
1392 select_frame (get_current_frame (), 0);
1393 }
1394 }
1395
1396 static int
1397 hook_stop_stub (cmd)
1398 char *cmd;
1399 {
1400 execute_user_command ((struct cmd_list_element *)cmd, 0);
1401 return (0);
1402 }
1403 \f
1404 int signal_stop_state (signo)
1405 int signo;
1406 {
1407 return signal_stop[signo];
1408 }
1409
1410 int signal_print_state (signo)
1411 int signo;
1412 {
1413 return signal_print[signo];
1414 }
1415
1416 int signal_pass_state (signo)
1417 int signo;
1418 {
1419 return signal_program[signo];
1420 }
1421
1422 static void
1423 sig_print_header ()
1424 {
1425 printf_filtered ("\
1426 Signal Stop\tPrint\tPass to program\tDescription\n");
1427 }
1428
1429 static void
1430 sig_print_info (oursig)
1431 enum target_signal oursig;
1432 {
1433 char *name = target_signal_to_name (oursig);
1434 printf_filtered ("%s", name);
1435 printf_filtered ("%*.*s ", 13 - strlen (name), 13 - strlen (name),
1436 " ");
1437 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
1438 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
1439 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
1440 printf_filtered ("%s\n", target_signal_to_string (oursig));
1441 }
1442
1443 /* Specify how various signals in the inferior should be handled. */
1444
1445 static void
1446 handle_command (args, from_tty)
1447 char *args;
1448 int from_tty;
1449 {
1450 char **argv;
1451 int digits, wordlen;
1452 int sigfirst, signum, siglast;
1453 enum target_signal oursig;
1454 int allsigs;
1455 int nsigs;
1456 unsigned char *sigs;
1457 struct cleanup *old_chain;
1458
1459 if (args == NULL)
1460 {
1461 error_no_arg ("signal to handle");
1462 }
1463
1464 /* Allocate and zero an array of flags for which signals to handle. */
1465
1466 nsigs = (int)TARGET_SIGNAL_LAST;
1467 sigs = (unsigned char *) alloca (nsigs);
1468 memset (sigs, 0, nsigs);
1469
1470 /* Break the command line up into args. */
1471
1472 argv = buildargv (args);
1473 if (argv == NULL)
1474 {
1475 nomem (0);
1476 }
1477 old_chain = make_cleanup (freeargv, (char *) argv);
1478
1479 /* Walk through the args, looking for signal oursigs, signal names, and
1480 actions. Signal numbers and signal names may be interspersed with
1481 actions, with the actions being performed for all signals cumulatively
1482 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
1483
1484 while (*argv != NULL)
1485 {
1486 wordlen = strlen (*argv);
1487 for (digits = 0; isdigit ((*argv)[digits]); digits++) {;}
1488 allsigs = 0;
1489 sigfirst = siglast = -1;
1490
1491 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
1492 {
1493 /* Apply action to all signals except those used by the
1494 debugger. Silently skip those. */
1495 allsigs = 1;
1496 sigfirst = 0;
1497 siglast = nsigs - 1;
1498 }
1499 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
1500 {
1501 SET_SIGS (nsigs, sigs, signal_stop);
1502 SET_SIGS (nsigs, sigs, signal_print);
1503 }
1504 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
1505 {
1506 UNSET_SIGS (nsigs, sigs, signal_program);
1507 }
1508 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
1509 {
1510 SET_SIGS (nsigs, sigs, signal_print);
1511 }
1512 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
1513 {
1514 SET_SIGS (nsigs, sigs, signal_program);
1515 }
1516 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
1517 {
1518 UNSET_SIGS (nsigs, sigs, signal_stop);
1519 }
1520 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
1521 {
1522 SET_SIGS (nsigs, sigs, signal_program);
1523 }
1524 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
1525 {
1526 UNSET_SIGS (nsigs, sigs, signal_print);
1527 UNSET_SIGS (nsigs, sigs, signal_stop);
1528 }
1529 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
1530 {
1531 UNSET_SIGS (nsigs, sigs, signal_program);
1532 }
1533 else if (digits > 0)
1534 {
1535 /* It is numeric. The numeric signal refers to our own internal
1536 signal numbering from target.h, not to host/target signal number.
1537 This is a feature; users really should be using symbolic names
1538 anyway, and the common ones like SIGHUP, SIGINT, SIGALRM, etc.
1539 will work right anyway. */
1540
1541 sigfirst = siglast = atoi (*argv);
1542 if ((*argv)[digits] == '-')
1543 {
1544 siglast = atoi ((*argv) + digits + 1);
1545 }
1546 if (sigfirst > siglast)
1547 {
1548 /* Bet he didn't figure we'd think of this case... */
1549 signum = sigfirst;
1550 sigfirst = siglast;
1551 siglast = signum;
1552 }
1553 if (sigfirst < 0 || sigfirst >= nsigs)
1554 {
1555 error ("Signal %d not in range 0-%d", sigfirst, nsigs - 1);
1556 }
1557 if (siglast < 0 || siglast >= nsigs)
1558 {
1559 error ("Signal %d not in range 0-%d", siglast, nsigs - 1);
1560 }
1561 }
1562 else if ((oursig = target_signal_from_name (*argv))
1563 != TARGET_SIGNAL_UNKNOWN)
1564 {
1565 sigfirst = siglast = (int)oursig;
1566 }
1567 else
1568 {
1569 /* Not a number and not a recognized flag word => complain. */
1570 error ("Unrecognized or ambiguous flag word: \"%s\".", *argv);
1571 }
1572
1573 /* If any signal numbers or symbol names were found, set flags for
1574 which signals to apply actions to. */
1575
1576 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
1577 {
1578 switch ((enum target_signal)signum)
1579 {
1580 case TARGET_SIGNAL_TRAP:
1581 case TARGET_SIGNAL_INT:
1582 if (!allsigs && !sigs[signum])
1583 {
1584 if (query ("%s is used by the debugger.\n\
1585 Are you sure you want to change it? ",
1586 target_signal_to_name
1587 ((enum target_signal)signum)))
1588 {
1589 sigs[signum] = 1;
1590 }
1591 else
1592 {
1593 printf_unfiltered ("Not confirmed, unchanged.\n");
1594 gdb_flush (gdb_stdout);
1595 }
1596 }
1597 break;
1598 default:
1599 sigs[signum] = 1;
1600 break;
1601 }
1602 }
1603
1604 argv++;
1605 }
1606
1607 target_notice_signals(inferior_pid);
1608
1609 if (from_tty)
1610 {
1611 /* Show the results. */
1612 sig_print_header ();
1613 for (signum = 0; signum < nsigs; signum++)
1614 {
1615 if (sigs[signum])
1616 {
1617 sig_print_info (signum);
1618 }
1619 }
1620 }
1621
1622 do_cleanups (old_chain);
1623 }
1624
1625 /* Print current contents of the tables set by the handle command.
1626 It is possible we should just be printing signals actually used
1627 by the current target (but for things to work right when switching
1628 targets, all signals should be in the signal tables). */
1629
1630 static void
1631 signals_info (signum_exp, from_tty)
1632 char *signum_exp;
1633 int from_tty;
1634 {
1635 enum target_signal oursig;
1636 sig_print_header ();
1637
1638 if (signum_exp)
1639 {
1640 /* First see if this is a symbol name. */
1641 oursig = target_signal_from_name (signum_exp);
1642 if (oursig == TARGET_SIGNAL_UNKNOWN)
1643 {
1644 /* Nope, maybe it's an address which evaluates to a signal
1645 number. */
1646 /* The numeric signal refers to our own internal
1647 signal numbering from target.h, not to host/target signal number.
1648 This is a feature; users really should be using symbolic names
1649 anyway, and the common ones like SIGHUP, SIGINT, SIGALRM, etc.
1650 will work right anyway. */
1651 int i = parse_and_eval_address (signum_exp);
1652 if (i >= (int)TARGET_SIGNAL_LAST
1653 || i < 0
1654 || i == TARGET_SIGNAL_UNKNOWN)
1655 error ("Signal number out of bounds.");
1656 oursig = (enum target_signal)i;
1657 }
1658 sig_print_info (oursig);
1659 return;
1660 }
1661
1662 printf_filtered ("\n");
1663 for (oursig = 0; oursig < TARGET_SIGNAL_LAST; ++oursig)
1664 {
1665 QUIT;
1666
1667 if (oursig != TARGET_SIGNAL_UNKNOWN)
1668 sig_print_info (oursig);
1669 }
1670
1671 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
1672 }
1673 \f
1674 /* Save all of the information associated with the inferior<==>gdb
1675 connection. INF_STATUS is a pointer to a "struct inferior_status"
1676 (defined in inferior.h). */
1677
1678 void
1679 save_inferior_status (inf_status, restore_stack_info)
1680 struct inferior_status *inf_status;
1681 int restore_stack_info;
1682 {
1683 inf_status->stop_signal = stop_signal;
1684 inf_status->stop_pc = stop_pc;
1685 inf_status->stop_frame_address = stop_frame_address;
1686 inf_status->stop_step = stop_step;
1687 inf_status->stop_stack_dummy = stop_stack_dummy;
1688 inf_status->stopped_by_random_signal = stopped_by_random_signal;
1689 inf_status->trap_expected = trap_expected;
1690 inf_status->step_range_start = step_range_start;
1691 inf_status->step_range_end = step_range_end;
1692 inf_status->step_frame_address = step_frame_address;
1693 inf_status->step_over_calls = step_over_calls;
1694 inf_status->stop_after_trap = stop_after_trap;
1695 inf_status->stop_soon_quietly = stop_soon_quietly;
1696 /* Save original bpstat chain here; replace it with copy of chain.
1697 If caller's caller is walking the chain, they'll be happier if we
1698 hand them back the original chain when restore_i_s is called. */
1699 inf_status->stop_bpstat = stop_bpstat;
1700 stop_bpstat = bpstat_copy (stop_bpstat);
1701 inf_status->breakpoint_proceeded = breakpoint_proceeded;
1702 inf_status->restore_stack_info = restore_stack_info;
1703 inf_status->proceed_to_finish = proceed_to_finish;
1704
1705 memcpy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
1706
1707 read_register_bytes (0, inf_status->registers, REGISTER_BYTES);
1708
1709 record_selected_frame (&(inf_status->selected_frame_address),
1710 &(inf_status->selected_level));
1711 return;
1712 }
1713
1714 struct restore_selected_frame_args {
1715 FRAME_ADDR frame_address;
1716 int level;
1717 };
1718
1719 static int restore_selected_frame PARAMS ((char *));
1720
1721 /* Restore the selected frame. args is really a struct
1722 restore_selected_frame_args * (declared as char * for catch_errors)
1723 telling us what frame to restore. Returns 1 for success, or 0 for
1724 failure. An error message will have been printed on error. */
1725 static int
1726 restore_selected_frame (args)
1727 char *args;
1728 {
1729 struct restore_selected_frame_args *fr =
1730 (struct restore_selected_frame_args *) args;
1731 FRAME fid;
1732 int level = fr->level;
1733
1734 fid = find_relative_frame (get_current_frame (), &level);
1735
1736 /* If inf_status->selected_frame_address is NULL, there was no
1737 previously selected frame. */
1738 if (fid == 0 ||
1739 FRAME_FP (fid) != fr->frame_address ||
1740 level != 0)
1741 {
1742 warning ("Unable to restore previously selected frame.\n");
1743 return 0;
1744 }
1745 select_frame (fid, fr->level);
1746 return(1);
1747 }
1748
1749 void
1750 restore_inferior_status (inf_status)
1751 struct inferior_status *inf_status;
1752 {
1753 stop_signal = inf_status->stop_signal;
1754 stop_pc = inf_status->stop_pc;
1755 stop_frame_address = inf_status->stop_frame_address;
1756 stop_step = inf_status->stop_step;
1757 stop_stack_dummy = inf_status->stop_stack_dummy;
1758 stopped_by_random_signal = inf_status->stopped_by_random_signal;
1759 trap_expected = inf_status->trap_expected;
1760 step_range_start = inf_status->step_range_start;
1761 step_range_end = inf_status->step_range_end;
1762 step_frame_address = inf_status->step_frame_address;
1763 step_over_calls = inf_status->step_over_calls;
1764 stop_after_trap = inf_status->stop_after_trap;
1765 stop_soon_quietly = inf_status->stop_soon_quietly;
1766 bpstat_clear (&stop_bpstat);
1767 stop_bpstat = inf_status->stop_bpstat;
1768 breakpoint_proceeded = inf_status->breakpoint_proceeded;
1769 proceed_to_finish = inf_status->proceed_to_finish;
1770
1771 memcpy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
1772
1773 /* The inferior can be gone if the user types "print exit(0)"
1774 (and perhaps other times). */
1775 if (target_has_execution)
1776 write_register_bytes (0, inf_status->registers, REGISTER_BYTES);
1777
1778 /* The inferior can be gone if the user types "print exit(0)"
1779 (and perhaps other times). */
1780
1781 /* FIXME: If we are being called after stopping in a function which
1782 is called from gdb, we should not be trying to restore the
1783 selected frame; it just prints a spurious error message (The
1784 message is useful, however, in detecting bugs in gdb (like if gdb
1785 clobbers the stack)). In fact, should we be restoring the
1786 inferior status at all in that case? . */
1787
1788 if (target_has_stack && inf_status->restore_stack_info)
1789 {
1790 struct restore_selected_frame_args fr;
1791 fr.level = inf_status->selected_level;
1792 fr.frame_address = inf_status->selected_frame_address;
1793 /* The point of catch_errors is that if the stack is clobbered,
1794 walking the stack might encounter a garbage pointer and error()
1795 trying to dereference it. */
1796 if (catch_errors (restore_selected_frame, &fr,
1797 "Unable to restore previously selected frame:\n",
1798 RETURN_MASK_ERROR) == 0)
1799 /* Error in restoring the selected frame. Select the innermost
1800 frame. */
1801 select_frame (get_current_frame (), 0);
1802 }
1803 }
1804
1805 \f
1806 void
1807 _initialize_infrun ()
1808 {
1809 register int i;
1810 register int numsigs;
1811
1812 add_info ("signals", signals_info,
1813 "What debugger does when program gets various signals.\n\
1814 Specify a signal number as argument to print info on that signal only.");
1815 add_info_alias ("handle", "signals", 0);
1816
1817 add_com ("handle", class_run, handle_command,
1818 "Specify how to handle a signal.\n\
1819 Args are signal numbers and actions to apply to those signals.\n\
1820 Signal numbers may be numeric (ex. 11) or symbolic (ex. SIGSEGV).\n\
1821 Numeric ranges may be specified with the form LOW-HIGH (ex. 14-21).\n\
1822 The special arg \"all\" is recognized to mean all signals except those\n\
1823 used by the debugger, typically SIGTRAP and SIGINT.\n\
1824 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
1825 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
1826 Stop means reenter debugger if this signal happens (implies print).\n\
1827 Print means print a message if this signal happens.\n\
1828 Pass means let program see this signal; otherwise program doesn't know.\n\
1829 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1830 Pass and Stop may be combined.");
1831
1832 stop_command = add_cmd ("stop", class_obscure, not_just_help_class_command,
1833 "There is no `stop' command, but you can set a hook on `stop'.\n\
1834 This allows you to set a list of commands to be run each time execution\n\
1835 of the program stops.", &cmdlist);
1836
1837 numsigs = (int)TARGET_SIGNAL_LAST;
1838 signal_stop = (unsigned char *)
1839 xmalloc (sizeof (signal_stop[0]) * numsigs);
1840 signal_print = (unsigned char *)
1841 xmalloc (sizeof (signal_print[0]) * numsigs);
1842 signal_program = (unsigned char *)
1843 xmalloc (sizeof (signal_program[0]) * numsigs);
1844 for (i = 0; i < numsigs; i++)
1845 {
1846 signal_stop[i] = 1;
1847 signal_print[i] = 1;
1848 signal_program[i] = 1;
1849 }
1850
1851 /* Signals caused by debugger's own actions
1852 should not be given to the program afterwards. */
1853 signal_program[TARGET_SIGNAL_TRAP] = 0;
1854 signal_program[TARGET_SIGNAL_INT] = 0;
1855
1856 /* Signals that are not errors should not normally enter the debugger. */
1857 signal_stop[TARGET_SIGNAL_ALRM] = 0;
1858 signal_print[TARGET_SIGNAL_ALRM] = 0;
1859 signal_stop[TARGET_SIGNAL_VTALRM] = 0;
1860 signal_print[TARGET_SIGNAL_VTALRM] = 0;
1861 signal_stop[TARGET_SIGNAL_PROF] = 0;
1862 signal_print[TARGET_SIGNAL_PROF] = 0;
1863 signal_stop[TARGET_SIGNAL_CHLD] = 0;
1864 signal_print[TARGET_SIGNAL_CHLD] = 0;
1865 signal_stop[TARGET_SIGNAL_IO] = 0;
1866 signal_print[TARGET_SIGNAL_IO] = 0;
1867 signal_stop[TARGET_SIGNAL_URG] = 0;
1868 signal_print[TARGET_SIGNAL_URG] = 0;
1869 }
This page took 0.067115 seconds and 5 git commands to generate.