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