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