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