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