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