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