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