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