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