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