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