* hppah-nat.c (saved_child_execd_pathname, saved_vfork_state): New.
[binutils-gdb.git] / gdb / infrun.c
1 /* Target-struct-independent code to start (run) and stop an inferior
2 process.
3
4 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
5 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software
6 Foundation, Inc.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
24
25 #include "defs.h"
26 #include "gdb_string.h"
27 #include <ctype.h>
28 #include "symtab.h"
29 #include "frame.h"
30 #include "inferior.h"
31 #include "breakpoint.h"
32 #include "gdb_wait.h"
33 #include "gdbcore.h"
34 #include "gdbcmd.h"
35 #include "cli/cli-script.h"
36 #include "target.h"
37 #include "gdbthread.h"
38 #include "annotate.h"
39 #include "symfile.h"
40 #include "top.h"
41 #include <signal.h>
42 #include "inf-loop.h"
43 #include "regcache.h"
44 #include "value.h"
45
46 /* Prototypes for local functions */
47
48 static void signals_info (char *, int);
49
50 static void handle_command (char *, int);
51
52 static void sig_print_info (enum target_signal);
53
54 static void sig_print_header (void);
55
56 static void resume_cleanups (void *);
57
58 static int hook_stop_stub (void *);
59
60 static void delete_breakpoint_current_contents (void *);
61
62 static void set_follow_fork_mode_command (char *arg, int from_tty,
63 struct cmd_list_element *c);
64
65 static int restore_selected_frame (void *);
66
67 static void build_infrun (void);
68
69 static void follow_inferior_fork (int parent_pid, int child_pid,
70 int has_forked, int has_vforked);
71
72 static void follow_fork (int parent_pid, int child_pid);
73
74 static void follow_vfork (int parent_pid, int child_pid);
75
76 static void set_schedlock_func (char *args, int from_tty,
77 struct cmd_list_element *c);
78
79 struct execution_control_state;
80
81 static int currently_stepping (struct execution_control_state *ecs);
82
83 static void xdb_handle_command (char *args, int from_tty);
84
85 void _initialize_infrun (void);
86
87 int inferior_ignoring_startup_exec_events = 0;
88 int inferior_ignoring_leading_exec_events = 0;
89
90 /* When set, stop the 'step' command if we enter a function which has
91 no line number information. The normal behavior is that we step
92 over such function. */
93 int step_stop_if_no_debug = 0;
94
95 /* In asynchronous mode, but simulating synchronous execution. */
96
97 int sync_execution = 0;
98
99 /* wait_for_inferior and normal_stop use this to notify the user
100 when the inferior stopped in a different thread than it had been
101 running in. */
102
103 static ptid_t previous_inferior_ptid;
104
105 /* This is true for configurations that may follow through execl() and
106 similar functions. At present this is only true for HP-UX native. */
107
108 #ifndef MAY_FOLLOW_EXEC
109 #define MAY_FOLLOW_EXEC (0)
110 #endif
111
112 static int may_follow_exec = MAY_FOLLOW_EXEC;
113
114 /* Dynamic function trampolines are similar to solib trampolines in that they
115 are between the caller and the callee. The difference is that when you
116 enter a dynamic trampoline, you can't determine the callee's address. Some
117 (usually complex) code needs to run in the dynamic trampoline to figure out
118 the callee's address. This macro is usually called twice. First, when we
119 enter the trampoline (looks like a normal function call at that point). It
120 should return the PC of a point within the trampoline where the callee's
121 address is known. Second, when we hit the breakpoint, this routine returns
122 the callee's address. At that point, things proceed as per a step resume
123 breakpoint. */
124
125 #ifndef DYNAMIC_TRAMPOLINE_NEXTPC
126 #define DYNAMIC_TRAMPOLINE_NEXTPC(pc) 0
127 #endif
128
129 /* If the program uses ELF-style shared libraries, then calls to
130 functions in shared libraries go through stubs, which live in a
131 table called the PLT (Procedure Linkage Table). The first time the
132 function is called, the stub sends control to the dynamic linker,
133 which looks up the function's real address, patches the stub so
134 that future calls will go directly to the function, and then passes
135 control to the function.
136
137 If we are stepping at the source level, we don't want to see any of
138 this --- we just want to skip over the stub and the dynamic linker.
139 The simple approach is to single-step until control leaves the
140 dynamic linker.
141
142 However, on some systems (e.g., Red Hat's 5.2 distribution) the
143 dynamic linker calls functions in the shared C library, so you
144 can't tell from the PC alone whether the dynamic linker is still
145 running. In this case, we use a step-resume breakpoint to get us
146 past the dynamic linker, as if we were using "next" to step over a
147 function call.
148
149 IN_SOLIB_DYNSYM_RESOLVE_CODE says whether we're in the dynamic
150 linker code or not. Normally, this means we single-step. However,
151 if SKIP_SOLIB_RESOLVER then returns non-zero, then its value is an
152 address where we can place a step-resume breakpoint to get past the
153 linker's symbol resolution function.
154
155 IN_SOLIB_DYNSYM_RESOLVE_CODE can generally be implemented in a
156 pretty portable way, by comparing the PC against the address ranges
157 of the dynamic linker's sections.
158
159 SKIP_SOLIB_RESOLVER is generally going to be system-specific, since
160 it depends on internal details of the dynamic linker. It's usually
161 not too hard to figure out where to put a breakpoint, but it
162 certainly isn't portable. SKIP_SOLIB_RESOLVER should do plenty of
163 sanity checking. If it can't figure things out, returning zero and
164 getting the (possibly confusing) stepping behavior is better than
165 signalling an error, which will obscure the change in the
166 inferior's state. */
167
168 #ifndef IN_SOLIB_DYNSYM_RESOLVE_CODE
169 #define IN_SOLIB_DYNSYM_RESOLVE_CODE(pc) 0
170 #endif
171
172 #ifndef SKIP_SOLIB_RESOLVER
173 #define SKIP_SOLIB_RESOLVER(pc) 0
174 #endif
175
176 /* This function returns TRUE if pc is the address of an instruction
177 that lies within the dynamic linker (such as the event hook, or the
178 dld itself).
179
180 This function must be used only when a dynamic linker event has
181 been caught, and the inferior is being stepped out of the hook, or
182 undefined results are guaranteed. */
183
184 #ifndef SOLIB_IN_DYNAMIC_LINKER
185 #define SOLIB_IN_DYNAMIC_LINKER(pid,pc) 0
186 #endif
187
188 /* On MIPS16, a function that returns a floating point value may call
189 a library helper function to copy the return value to a floating point
190 register. The IGNORE_HELPER_CALL macro returns non-zero if we
191 should ignore (i.e. step over) this function call. */
192 #ifndef IGNORE_HELPER_CALL
193 #define IGNORE_HELPER_CALL(pc) 0
194 #endif
195
196 /* On some systems, the PC may be left pointing at an instruction that won't
197 actually be executed. This is usually indicated by a bit in the PSW. If
198 we find ourselves in such a state, then we step the target beyond the
199 nullified instruction before returning control to the user so as to avoid
200 confusion. */
201
202 #ifndef INSTRUCTION_NULLIFIED
203 #define INSTRUCTION_NULLIFIED 0
204 #endif
205
206 /* We can't step off a permanent breakpoint in the ordinary way, because we
207 can't remove it. Instead, we have to advance the PC to the next
208 instruction. This macro should expand to a pointer to a function that
209 does that, or zero if we have no such function. If we don't have a
210 definition for it, we have to report an error. */
211 #ifndef SKIP_PERMANENT_BREAKPOINT
212 #define SKIP_PERMANENT_BREAKPOINT (default_skip_permanent_breakpoint)
213 static void
214 default_skip_permanent_breakpoint (void)
215 {
216 error ("\
217 The program is stopped at a permanent breakpoint, but GDB does not know\n\
218 how to step past a permanent breakpoint on this architecture. Try using\n\
219 a command like `return' or `jump' to continue execution.");
220 }
221 #endif
222
223
224 /* Convert the #defines into values. This is temporary until wfi control
225 flow is completely sorted out. */
226
227 #ifndef HAVE_STEPPABLE_WATCHPOINT
228 #define HAVE_STEPPABLE_WATCHPOINT 0
229 #else
230 #undef HAVE_STEPPABLE_WATCHPOINT
231 #define HAVE_STEPPABLE_WATCHPOINT 1
232 #endif
233
234 #ifndef HAVE_CONTINUABLE_WATCHPOINT
235 #define HAVE_CONTINUABLE_WATCHPOINT 0
236 #else
237 #undef HAVE_CONTINUABLE_WATCHPOINT
238 #define HAVE_CONTINUABLE_WATCHPOINT 1
239 #endif
240
241 #ifndef CANNOT_STEP_HW_WATCHPOINTS
242 #define CANNOT_STEP_HW_WATCHPOINTS 0
243 #else
244 #undef CANNOT_STEP_HW_WATCHPOINTS
245 #define CANNOT_STEP_HW_WATCHPOINTS 1
246 #endif
247
248 /* Tables of how to react to signals; the user sets them. */
249
250 static unsigned char *signal_stop;
251 static unsigned char *signal_print;
252 static unsigned char *signal_program;
253
254 #define SET_SIGS(nsigs,sigs,flags) \
255 do { \
256 int signum = (nsigs); \
257 while (signum-- > 0) \
258 if ((sigs)[signum]) \
259 (flags)[signum] = 1; \
260 } while (0)
261
262 #define UNSET_SIGS(nsigs,sigs,flags) \
263 do { \
264 int signum = (nsigs); \
265 while (signum-- > 0) \
266 if ((sigs)[signum]) \
267 (flags)[signum] = 0; \
268 } while (0)
269
270 /* Value to pass to target_resume() to cause all threads to resume */
271
272 #define RESUME_ALL (pid_to_ptid (-1))
273
274 /* Command list pointer for the "stop" placeholder. */
275
276 static struct cmd_list_element *stop_command;
277
278 /* Nonzero if breakpoints are now inserted in the inferior. */
279
280 static int breakpoints_inserted;
281
282 /* Function inferior was in as of last step command. */
283
284 static struct symbol *step_start_function;
285
286 /* Nonzero if we are expecting a trace trap and should proceed from it. */
287
288 static int trap_expected;
289
290 #ifdef SOLIB_ADD
291 /* Nonzero if we want to give control to the user when we're notified
292 of shared library events by the dynamic linker. */
293 static int stop_on_solib_events;
294 #endif
295
296 #ifdef HP_OS_BUG
297 /* Nonzero if the next time we try to continue the inferior, it will
298 step one instruction and generate a spurious trace trap.
299 This is used to compensate for a bug in HP-UX. */
300
301 static int trap_expected_after_continue;
302 #endif
303
304 /* Nonzero means expecting a trace trap
305 and should stop the inferior and return silently when it happens. */
306
307 int stop_after_trap;
308
309 /* Nonzero means expecting a trap and caller will handle it themselves.
310 It is used after attach, due to attaching to a process;
311 when running in the shell before the child program has been exec'd;
312 and when running some kinds of remote stuff (FIXME?). */
313
314 int stop_soon_quietly;
315
316 /* Nonzero if proceed is being used for a "finish" command or a similar
317 situation when stop_registers should be saved. */
318
319 int proceed_to_finish;
320
321 /* Save register contents here when about to pop a stack dummy frame,
322 if-and-only-if proceed_to_finish is set.
323 Thus this contains the return value from the called function (assuming
324 values are returned in a register). */
325
326 struct regcache *stop_registers;
327
328 /* Nonzero if program stopped due to error trying to insert breakpoints. */
329
330 static int breakpoints_failed;
331
332 /* Nonzero after stop if current stack frame should be printed. */
333
334 static int stop_print_frame;
335
336 static struct breakpoint *step_resume_breakpoint = NULL;
337 static struct breakpoint *through_sigtramp_breakpoint = NULL;
338
339 /* On some platforms (e.g., HP-UX), hardware watchpoints have bad
340 interactions with an inferior that is running a kernel function
341 (aka, a system call or "syscall"). wait_for_inferior therefore
342 may have a need to know when the inferior is in a syscall. This
343 is a count of the number of inferior threads which are known to
344 currently be running in a syscall. */
345 static int number_of_threads_in_syscalls;
346
347 /* This is a cached copy of the pid/waitstatus of the last event
348 returned by target_wait()/target_wait_hook(). This information is
349 returned by get_last_target_status(). */
350 static ptid_t target_last_wait_ptid;
351 static struct target_waitstatus target_last_waitstatus;
352
353 /* This is used to remember when a fork, vfork or exec event
354 was caught by a catchpoint, and thus the event is to be
355 followed at the next resume of the inferior, and not
356 immediately. */
357 static struct
358 {
359 enum target_waitkind kind;
360 struct
361 {
362 int parent_pid;
363 int saw_parent_fork;
364 int child_pid;
365 int saw_child_fork;
366 int saw_child_exec;
367 }
368 fork_event;
369 char *execd_pathname;
370 }
371 pending_follow;
372
373 static const char follow_fork_mode_ask[] = "ask";
374 static const char follow_fork_mode_child[] = "child";
375 static const char follow_fork_mode_parent[] = "parent";
376
377 static const char *follow_fork_mode_kind_names[] = {
378 follow_fork_mode_ask,
379 follow_fork_mode_child,
380 follow_fork_mode_parent,
381 NULL
382 };
383
384 static const char *follow_fork_mode_string = follow_fork_mode_parent;
385 \f
386
387 static void
388 follow_inferior_fork (int parent_pid, int child_pid, int has_forked,
389 int has_vforked)
390 {
391 int followed_parent = 0;
392 int followed_child = 0;
393
394 /* Which process did the user want us to follow? */
395 const char *follow_mode = follow_fork_mode_string;
396
397 /* Or, did the user not know, and want us to ask? */
398 if (follow_fork_mode_string == follow_fork_mode_ask)
399 {
400 internal_error (__FILE__, __LINE__,
401 "follow_inferior_fork: \"ask\" mode not implemented");
402 /* follow_mode = follow_fork_mode_...; */
403 }
404
405 /* If we're to be following the parent, then detach from child_pid.
406 We're already following the parent, so need do nothing explicit
407 for it. */
408 if (follow_mode == follow_fork_mode_parent)
409 {
410 followed_parent = 1;
411
412 /* We're already attached to the parent, by default. */
413
414 /* Before detaching from the child, remove all breakpoints from
415 it. (This won't actually modify the breakpoint list, but will
416 physically remove the breakpoints from the child.) */
417 detach_breakpoints (child_pid);
418 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
419 SOLIB_REMOVE_INFERIOR_HOOK (child_pid);
420 #endif
421
422 /* Detach from the child. */
423 dont_repeat ();
424
425 target_require_detach (child_pid, "", 1);
426 }
427
428 /* If we're to be following the child, then attach to it, detach
429 from inferior_ptid, and set inferior_ptid to child_pid. */
430 else if (follow_mode == follow_fork_mode_child)
431 {
432 char child_pid_spelling[100]; /* Arbitrary length. */
433
434 followed_child = 1;
435
436 /* Before detaching from the parent, detach all breakpoints from
437 the child. Note that this only works if we're following vforks
438 right away; if we've exec'd then the breakpoints are already detached
439 and the shadow contents are out of date. */
440 detach_breakpoints (child_pid);
441
442 /* Before detaching from the parent, remove all breakpoints from it. */
443 remove_breakpoints ();
444
445 /* Also reset the solib inferior hook from the parent. */
446 #ifdef SOLIB_REMOVE_INFERIOR_HOOK
447 SOLIB_REMOVE_INFERIOR_HOOK (PIDGET (inferior_ptid));
448 #endif
449
450 /* Detach from the parent. */
451 dont_repeat ();
452 target_detach (NULL, 1);
453
454 /* Attach to the child. */
455 inferior_ptid = pid_to_ptid (child_pid);
456 sprintf (child_pid_spelling, "%d", child_pid);
457 dont_repeat ();
458
459 target_require_attach (child_pid_spelling, 1);
460
461 /* Was there a step_resume breakpoint? (There was if the user
462 did a "next" at the fork() call.) If so, explicitly reset its
463 thread number.
464
465 step_resumes are a form of bp that are made to be per-thread.
466 Since we created the step_resume bp when the parent process
467 was being debugged, and now are switching to the child process,
468 from the breakpoint package's viewpoint, that's a switch of
469 "threads". We must update the bp's notion of which thread
470 it is for, or it'll be ignored when it triggers... */
471 /* As above, if we're following vforks at exec time then resetting the
472 step resume breakpoint is probably wrong. */
473 if (step_resume_breakpoint)
474 breakpoint_re_set_thread (step_resume_breakpoint);
475
476 /* Reinsert all breakpoints in the child. (The user may've set
477 breakpoints after catching the fork, in which case those
478 actually didn't get set in the child, but only in the parent.) */
479 breakpoint_re_set ();
480 insert_breakpoints ();
481 }
482
483 /* The parent and child of a vfork share the same address space.
484 Also, on some targets the order in which vfork and exec events
485 are received for parent in child requires some delicate handling
486 of the events.
487
488 For instance, on ptrace-based HPUX we receive the child's vfork
489 event first, at which time the parent has been suspended by the
490 OS and is essentially untouchable until the child's exit or second
491 exec event arrives. At that time, the parent's vfork event is
492 delivered to us, and that's when we see and decide how to follow
493 the vfork. But to get to that point, we must continue the child
494 until it execs or exits. To do that smoothly, all breakpoints
495 must be removed from the child, in case there are any set between
496 the vfork() and exec() calls. But removing them from the child
497 also removes them from the parent, due to the shared-address-space
498 nature of a vfork'd parent and child. On HPUX, therefore, we must
499 take care to restore the bp's to the parent before we continue it.
500 Else, it's likely that we may not stop in the expected place. (The
501 worst scenario is when the user tries to step over a vfork() call;
502 the step-resume bp must be restored for the step to properly stop
503 in the parent after the call completes!)
504
505 Sequence of events, as reported to gdb from HPUX:
506
507 Parent Child Action for gdb to take
508 -------------------------------------------------------
509 1 VFORK Continue child
510 2 EXEC
511 3 EXEC or EXIT
512 4 VFORK */
513 if (has_vforked)
514 {
515 target_post_follow_vfork (parent_pid,
516 followed_parent, child_pid, followed_child);
517 }
518
519 pending_follow.fork_event.saw_parent_fork = 0;
520 pending_follow.fork_event.saw_child_fork = 0;
521 }
522
523 static void
524 follow_fork (int parent_pid, int child_pid)
525 {
526 follow_inferior_fork (parent_pid, child_pid, 1, 0);
527 }
528
529
530 /* Forward declaration. */
531 static void follow_exec (int, char *);
532
533 static void
534 follow_vfork (int parent_pid, int child_pid)
535 {
536 follow_inferior_fork (parent_pid, child_pid, 0, 1);
537 }
538
539 /* EXECD_PATHNAME is assumed to be non-NULL. */
540
541 static void
542 follow_exec (int pid, char *execd_pathname)
543 {
544 int saved_pid = pid;
545 struct target_ops *tgt;
546
547 if (!may_follow_exec)
548 return;
549
550 /* This is an exec event that we actually wish to pay attention to.
551 Refresh our symbol table to the newly exec'd program, remove any
552 momentary bp's, etc.
553
554 If there are breakpoints, they aren't really inserted now,
555 since the exec() transformed our inferior into a fresh set
556 of instructions.
557
558 We want to preserve symbolic breakpoints on the list, since
559 we have hopes that they can be reset after the new a.out's
560 symbol table is read.
561
562 However, any "raw" breakpoints must be removed from the list
563 (e.g., the solib bp's), since their address is probably invalid
564 now.
565
566 And, we DON'T want to call delete_breakpoints() here, since
567 that may write the bp's "shadow contents" (the instruction
568 value that was overwritten witha TRAP instruction). Since
569 we now have a new a.out, those shadow contents aren't valid. */
570 update_breakpoints_after_exec ();
571
572 /* If there was one, it's gone now. We cannot truly step-to-next
573 statement through an exec(). */
574 step_resume_breakpoint = NULL;
575 step_range_start = 0;
576 step_range_end = 0;
577
578 /* If there was one, it's gone now. */
579 through_sigtramp_breakpoint = NULL;
580
581 /* What is this a.out's name? */
582 printf_unfiltered ("Executing new program: %s\n", execd_pathname);
583
584 /* We've followed the inferior through an exec. Therefore, the
585 inferior has essentially been killed & reborn. */
586
587 /* First collect the run target in effect. */
588 tgt = find_run_target ();
589 /* If we can't find one, things are in a very strange state... */
590 if (tgt == NULL)
591 error ("Could find run target to save before following exec");
592
593 gdb_flush (gdb_stdout);
594 target_mourn_inferior ();
595 inferior_ptid = pid_to_ptid (saved_pid);
596 /* Because mourn_inferior resets inferior_ptid. */
597 push_target (tgt);
598
599 /* That a.out is now the one to use. */
600 exec_file_attach (execd_pathname, 0);
601
602 /* And also is where symbols can be found. */
603 symbol_file_add_main (execd_pathname, 0);
604
605 /* Reset the shared library package. This ensures that we get
606 a shlib event when the child reaches "_start", at which point
607 the dld will have had a chance to initialize the child. */
608 #if defined(SOLIB_RESTART)
609 SOLIB_RESTART ();
610 #endif
611 #ifdef SOLIB_CREATE_INFERIOR_HOOK
612 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
613 #endif
614
615 /* Reinsert all breakpoints. (Those which were symbolic have
616 been reset to the proper address in the new a.out, thanks
617 to symbol_file_command...) */
618 insert_breakpoints ();
619
620 /* The next resume of this inferior should bring it to the shlib
621 startup breakpoints. (If the user had also set bp's on
622 "main" from the old (parent) process, then they'll auto-
623 matically get reset there in the new process.) */
624 }
625
626 /* Non-zero if we just simulating a single-step. This is needed
627 because we cannot remove the breakpoints in the inferior process
628 until after the `wait' in `wait_for_inferior'. */
629 static int singlestep_breakpoints_inserted_p = 0;
630 \f
631
632 /* Things to clean up if we QUIT out of resume (). */
633 /* ARGSUSED */
634 static void
635 resume_cleanups (void *ignore)
636 {
637 normal_stop ();
638 }
639
640 static const char schedlock_off[] = "off";
641 static const char schedlock_on[] = "on";
642 static const char schedlock_step[] = "step";
643 static const char *scheduler_mode = schedlock_off;
644 static const char *scheduler_enums[] = {
645 schedlock_off,
646 schedlock_on,
647 schedlock_step,
648 NULL
649 };
650
651 static void
652 set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
653 {
654 /* NOTE: cagney/2002-03-17: The add_show_from_set() function clones
655 the set command passed as a parameter. The clone operation will
656 include (BUG?) any ``set'' command callback, if present.
657 Commands like ``info set'' call all the ``show'' command
658 callbacks. Unfortunatly, for ``show'' commands cloned from
659 ``set'', this includes callbacks belonging to ``set'' commands.
660 Making this worse, this only occures if add_show_from_set() is
661 called after add_cmd_sfunc() (BUG?). */
662 if (cmd_type (c) == set_cmd)
663 if (!target_can_lock_scheduler)
664 {
665 scheduler_mode = schedlock_off;
666 error ("Target '%s' cannot support this command.", target_shortname);
667 }
668 }
669
670
671 /* Resume the inferior, but allow a QUIT. This is useful if the user
672 wants to interrupt some lengthy single-stepping operation
673 (for child processes, the SIGINT goes to the inferior, and so
674 we get a SIGINT random_signal, but for remote debugging and perhaps
675 other targets, that's not true).
676
677 STEP nonzero if we should step (zero to continue instead).
678 SIG is the signal to give the inferior (zero for none). */
679 void
680 resume (int step, enum target_signal sig)
681 {
682 int should_resume = 1;
683 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
684 QUIT;
685
686 /* FIXME: calling breakpoint_here_p (read_pc ()) three times! */
687
688
689 /* Some targets (e.g. Solaris x86) have a kernel bug when stepping
690 over an instruction that causes a page fault without triggering
691 a hardware watchpoint. The kernel properly notices that it shouldn't
692 stop, because the hardware watchpoint is not triggered, but it forgets
693 the step request and continues the program normally.
694 Work around the problem by removing hardware watchpoints if a step is
695 requested, GDB will check for a hardware watchpoint trigger after the
696 step anyway. */
697 if (CANNOT_STEP_HW_WATCHPOINTS && step && breakpoints_inserted)
698 remove_hw_watchpoints ();
699
700
701 /* Normally, by the time we reach `resume', the breakpoints are either
702 removed or inserted, as appropriate. The exception is if we're sitting
703 at a permanent breakpoint; we need to step over it, but permanent
704 breakpoints can't be removed. So we have to test for it here. */
705 if (breakpoint_here_p (read_pc ()) == permanent_breakpoint_here)
706 SKIP_PERMANENT_BREAKPOINT ();
707
708 if (SOFTWARE_SINGLE_STEP_P () && step)
709 {
710 /* Do it the hard way, w/temp breakpoints */
711 SOFTWARE_SINGLE_STEP (sig, 1 /*insert-breakpoints */ );
712 /* ...and don't ask hardware to do it. */
713 step = 0;
714 /* and do not pull these breakpoints until after a `wait' in
715 `wait_for_inferior' */
716 singlestep_breakpoints_inserted_p = 1;
717 }
718
719 /* Handle any optimized stores to the inferior NOW... */
720 #ifdef DO_DEFERRED_STORES
721 DO_DEFERRED_STORES;
722 #endif
723
724 /* If there were any forks/vforks/execs that were caught and are
725 now to be followed, then do so. */
726 switch (pending_follow.kind)
727 {
728 case (TARGET_WAITKIND_FORKED):
729 pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
730 follow_fork (PIDGET (inferior_ptid),
731 pending_follow.fork_event.child_pid);
732 break;
733
734 case (TARGET_WAITKIND_VFORKED):
735 {
736 int saw_child_exec = pending_follow.fork_event.saw_child_exec;
737
738 pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
739 follow_vfork (PIDGET (inferior_ptid),
740 pending_follow.fork_event.child_pid);
741
742 /* Did we follow the child, but not yet see the child's exec event?
743 If so, then it actually ought to be waiting for us; we respond to
744 parent vfork events. We don't actually want to resume the child
745 in this situation; we want to just get its exec event. */
746 if (!saw_child_exec &&
747 (PIDGET (inferior_ptid) == pending_follow.fork_event.child_pid))
748 should_resume = 0;
749 }
750 break;
751
752 case (TARGET_WAITKIND_EXECD):
753 /* If we saw a vfork event but couldn't follow it until we saw
754 an exec, then now might be the time! */
755 pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
756 /* follow_exec is called as soon as the exec event is seen. */
757 break;
758
759 default:
760 break;
761 }
762
763 /* Install inferior's terminal modes. */
764 target_terminal_inferior ();
765
766 if (should_resume)
767 {
768 ptid_t resume_ptid;
769
770 resume_ptid = RESUME_ALL; /* Default */
771
772 if ((step || singlestep_breakpoints_inserted_p) &&
773 !breakpoints_inserted && breakpoint_here_p (read_pc ()))
774 {
775 /* Stepping past a breakpoint without inserting breakpoints.
776 Make sure only the current thread gets to step, so that
777 other threads don't sneak past breakpoints while they are
778 not inserted. */
779
780 resume_ptid = inferior_ptid;
781 }
782
783 if ((scheduler_mode == schedlock_on) ||
784 (scheduler_mode == schedlock_step &&
785 (step || singlestep_breakpoints_inserted_p)))
786 {
787 /* User-settable 'scheduler' mode requires solo thread resume. */
788 resume_ptid = inferior_ptid;
789 }
790
791 if (CANNOT_STEP_BREAKPOINT)
792 {
793 /* Most targets can step a breakpoint instruction, thus
794 executing it normally. But if this one cannot, just
795 continue and we will hit it anyway. */
796 if (step && breakpoints_inserted && breakpoint_here_p (read_pc ()))
797 step = 0;
798 }
799 target_resume (resume_ptid, step, sig);
800 }
801
802 discard_cleanups (old_cleanups);
803 }
804 \f
805
806 /* Clear out all variables saying what to do when inferior is continued.
807 First do this, then set the ones you want, then call `proceed'. */
808
809 void
810 clear_proceed_status (void)
811 {
812 trap_expected = 0;
813 step_range_start = 0;
814 step_range_end = 0;
815 step_frame_id = null_frame_id;
816 step_over_calls = STEP_OVER_UNDEBUGGABLE;
817 stop_after_trap = 0;
818 stop_soon_quietly = 0;
819 proceed_to_finish = 0;
820 breakpoint_proceeded = 1; /* We're about to proceed... */
821
822 /* Discard any remaining commands or status from previous stop. */
823 bpstat_clear (&stop_bpstat);
824 }
825
826 /* Basic routine for continuing the program in various fashions.
827
828 ADDR is the address to resume at, or -1 for resume where stopped.
829 SIGGNAL is the signal to give it, or 0 for none,
830 or -1 for act according to how it stopped.
831 STEP is nonzero if should trap after one instruction.
832 -1 means return after that and print nothing.
833 You should probably set various step_... variables
834 before calling here, if you are stepping.
835
836 You should call clear_proceed_status before calling proceed. */
837
838 void
839 proceed (CORE_ADDR addr, enum target_signal siggnal, int step)
840 {
841 int oneproc = 0;
842
843 if (step > 0)
844 step_start_function = find_pc_function (read_pc ());
845 if (step < 0)
846 stop_after_trap = 1;
847
848 if (addr == (CORE_ADDR) -1)
849 {
850 /* If there is a breakpoint at the address we will resume at,
851 step one instruction before inserting breakpoints
852 so that we do not stop right away (and report a second
853 hit at this breakpoint). */
854
855 if (read_pc () == stop_pc && breakpoint_here_p (read_pc ()))
856 oneproc = 1;
857
858 #ifndef STEP_SKIPS_DELAY
859 #define STEP_SKIPS_DELAY(pc) (0)
860 #define STEP_SKIPS_DELAY_P (0)
861 #endif
862 /* Check breakpoint_here_p first, because breakpoint_here_p is fast
863 (it just checks internal GDB data structures) and STEP_SKIPS_DELAY
864 is slow (it needs to read memory from the target). */
865 if (STEP_SKIPS_DELAY_P
866 && breakpoint_here_p (read_pc () + 4)
867 && STEP_SKIPS_DELAY (read_pc ()))
868 oneproc = 1;
869 }
870 else
871 {
872 write_pc (addr);
873 }
874
875 #ifdef PREPARE_TO_PROCEED
876 /* In a multi-threaded task we may select another thread
877 and then continue or step.
878
879 But if the old thread was stopped at a breakpoint, it
880 will immediately cause another breakpoint stop without
881 any execution (i.e. it will report a breakpoint hit
882 incorrectly). So we must step over it first.
883
884 PREPARE_TO_PROCEED checks the current thread against the thread
885 that reported the most recent event. If a step-over is required
886 it returns TRUE and sets the current thread to the old thread. */
887 if (PREPARE_TO_PROCEED (1) && breakpoint_here_p (read_pc ()))
888 {
889 oneproc = 1;
890 }
891
892 #endif /* PREPARE_TO_PROCEED */
893
894 #ifdef HP_OS_BUG
895 if (trap_expected_after_continue)
896 {
897 /* If (step == 0), a trap will be automatically generated after
898 the first instruction is executed. Force step one
899 instruction to clear this condition. This should not occur
900 if step is nonzero, but it is harmless in that case. */
901 oneproc = 1;
902 trap_expected_after_continue = 0;
903 }
904 #endif /* HP_OS_BUG */
905
906 if (oneproc)
907 /* We will get a trace trap after one instruction.
908 Continue it automatically and insert breakpoints then. */
909 trap_expected = 1;
910 else
911 {
912 insert_breakpoints ();
913 /* If we get here there was no call to error() in
914 insert breakpoints -- so they were inserted. */
915 breakpoints_inserted = 1;
916 }
917
918 if (siggnal != TARGET_SIGNAL_DEFAULT)
919 stop_signal = siggnal;
920 /* If this signal should not be seen by program,
921 give it zero. Used for debugging signals. */
922 else if (!signal_program[stop_signal])
923 stop_signal = TARGET_SIGNAL_0;
924
925 annotate_starting ();
926
927 /* Make sure that output from GDB appears before output from the
928 inferior. */
929 gdb_flush (gdb_stdout);
930
931 /* Resume inferior. */
932 resume (oneproc || step || bpstat_should_step (), stop_signal);
933
934 /* Wait for it to stop (if not standalone)
935 and in any case decode why it stopped, and act accordingly. */
936 /* Do this only if we are not using the event loop, or if the target
937 does not support asynchronous execution. */
938 if (!event_loop_p || !target_can_async_p ())
939 {
940 wait_for_inferior ();
941 normal_stop ();
942 }
943 }
944
945 /* Record the pc and sp of the program the last time it stopped.
946 These are just used internally by wait_for_inferior, but need
947 to be preserved over calls to it and cleared when the inferior
948 is started. */
949 static CORE_ADDR prev_pc;
950 static CORE_ADDR prev_func_start;
951 static char *prev_func_name;
952 \f
953
954 /* Start remote-debugging of a machine over a serial link. */
955
956 void
957 start_remote (void)
958 {
959 init_thread_list ();
960 init_wait_for_inferior ();
961 stop_soon_quietly = 1;
962 trap_expected = 0;
963
964 /* Always go on waiting for the target, regardless of the mode. */
965 /* FIXME: cagney/1999-09-23: At present it isn't possible to
966 indicate to wait_for_inferior that a target should timeout if
967 nothing is returned (instead of just blocking). Because of this,
968 targets expecting an immediate response need to, internally, set
969 things up so that the target_wait() is forced to eventually
970 timeout. */
971 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
972 differentiate to its caller what the state of the target is after
973 the initial open has been performed. Here we're assuming that
974 the target has stopped. It should be possible to eventually have
975 target_open() return to the caller an indication that the target
976 is currently running and GDB state should be set to the same as
977 for an async run. */
978 wait_for_inferior ();
979 normal_stop ();
980 }
981
982 /* Initialize static vars when a new inferior begins. */
983
984 void
985 init_wait_for_inferior (void)
986 {
987 /* These are meaningless until the first time through wait_for_inferior. */
988 prev_pc = 0;
989 prev_func_start = 0;
990 prev_func_name = NULL;
991
992 #ifdef HP_OS_BUG
993 trap_expected_after_continue = 0;
994 #endif
995 breakpoints_inserted = 0;
996 breakpoint_init_inferior (inf_starting);
997
998 /* Don't confuse first call to proceed(). */
999 stop_signal = TARGET_SIGNAL_0;
1000
1001 /* The first resume is not following a fork/vfork/exec. */
1002 pending_follow.kind = TARGET_WAITKIND_SPURIOUS; /* I.e., none. */
1003 pending_follow.fork_event.saw_parent_fork = 0;
1004 pending_follow.fork_event.saw_child_fork = 0;
1005 pending_follow.fork_event.saw_child_exec = 0;
1006
1007 /* See wait_for_inferior's handling of SYSCALL_ENTRY/RETURN events. */
1008 number_of_threads_in_syscalls = 0;
1009
1010 clear_proceed_status ();
1011 }
1012
1013 static void
1014 delete_breakpoint_current_contents (void *arg)
1015 {
1016 struct breakpoint **breakpointp = (struct breakpoint **) arg;
1017 if (*breakpointp != NULL)
1018 {
1019 delete_breakpoint (*breakpointp);
1020 *breakpointp = NULL;
1021 }
1022 }
1023 \f
1024 /* This enum encodes possible reasons for doing a target_wait, so that
1025 wfi can call target_wait in one place. (Ultimately the call will be
1026 moved out of the infinite loop entirely.) */
1027
1028 enum infwait_states
1029 {
1030 infwait_normal_state,
1031 infwait_thread_hop_state,
1032 infwait_nullified_state,
1033 infwait_nonstep_watch_state
1034 };
1035
1036 /* Why did the inferior stop? Used to print the appropriate messages
1037 to the interface from within handle_inferior_event(). */
1038 enum inferior_stop_reason
1039 {
1040 /* We don't know why. */
1041 STOP_UNKNOWN,
1042 /* Step, next, nexti, stepi finished. */
1043 END_STEPPING_RANGE,
1044 /* Found breakpoint. */
1045 BREAKPOINT_HIT,
1046 /* Inferior terminated by signal. */
1047 SIGNAL_EXITED,
1048 /* Inferior exited. */
1049 EXITED,
1050 /* Inferior received signal, and user asked to be notified. */
1051 SIGNAL_RECEIVED
1052 };
1053
1054 /* This structure contains what used to be local variables in
1055 wait_for_inferior. Probably many of them can return to being
1056 locals in handle_inferior_event. */
1057
1058 struct execution_control_state
1059 {
1060 struct target_waitstatus ws;
1061 struct target_waitstatus *wp;
1062 int another_trap;
1063 int random_signal;
1064 CORE_ADDR stop_func_start;
1065 CORE_ADDR stop_func_end;
1066 char *stop_func_name;
1067 struct symtab_and_line sal;
1068 int remove_breakpoints_on_following_step;
1069 int current_line;
1070 struct symtab *current_symtab;
1071 int handling_longjmp; /* FIXME */
1072 ptid_t ptid;
1073 ptid_t saved_inferior_ptid;
1074 int update_step_sp;
1075 int stepping_through_solib_after_catch;
1076 bpstat stepping_through_solib_catchpoints;
1077 int enable_hw_watchpoints_after_wait;
1078 int stepping_through_sigtramp;
1079 int new_thread_event;
1080 struct target_waitstatus tmpstatus;
1081 enum infwait_states infwait_state;
1082 ptid_t waiton_ptid;
1083 int wait_some_more;
1084 };
1085
1086 void init_execution_control_state (struct execution_control_state *ecs);
1087
1088 void handle_inferior_event (struct execution_control_state *ecs);
1089
1090 static void check_sigtramp2 (struct execution_control_state *ecs);
1091 static void step_into_function (struct execution_control_state *ecs);
1092 static void step_over_function (struct execution_control_state *ecs);
1093 static void stop_stepping (struct execution_control_state *ecs);
1094 static void prepare_to_wait (struct execution_control_state *ecs);
1095 static void keep_going (struct execution_control_state *ecs);
1096 static void print_stop_reason (enum inferior_stop_reason stop_reason,
1097 int stop_info);
1098
1099 /* Wait for control to return from inferior to debugger.
1100 If inferior gets a signal, we may decide to start it up again
1101 instead of returning. That is why there is a loop in this function.
1102 When this function actually returns it means the inferior
1103 should be left stopped and GDB should read more commands. */
1104
1105 void
1106 wait_for_inferior (void)
1107 {
1108 struct cleanup *old_cleanups;
1109 struct execution_control_state ecss;
1110 struct execution_control_state *ecs;
1111
1112 old_cleanups = make_cleanup (delete_step_resume_breakpoint,
1113 &step_resume_breakpoint);
1114 make_cleanup (delete_breakpoint_current_contents,
1115 &through_sigtramp_breakpoint);
1116
1117 /* wfi still stays in a loop, so it's OK just to take the address of
1118 a local to get the ecs pointer. */
1119 ecs = &ecss;
1120
1121 /* Fill in with reasonable starting values. */
1122 init_execution_control_state (ecs);
1123
1124 /* We'll update this if & when we switch to a new thread. */
1125 previous_inferior_ptid = inferior_ptid;
1126
1127 overlay_cache_invalid = 1;
1128
1129 /* We have to invalidate the registers BEFORE calling target_wait
1130 because they can be loaded from the target while in target_wait.
1131 This makes remote debugging a bit more efficient for those
1132 targets that provide critical registers as part of their normal
1133 status mechanism. */
1134
1135 registers_changed ();
1136
1137 while (1)
1138 {
1139 if (target_wait_hook)
1140 ecs->ptid = target_wait_hook (ecs->waiton_ptid, ecs->wp);
1141 else
1142 ecs->ptid = target_wait (ecs->waiton_ptid, ecs->wp);
1143
1144 /* Now figure out what to do with the result of the result. */
1145 handle_inferior_event (ecs);
1146
1147 if (!ecs->wait_some_more)
1148 break;
1149 }
1150 do_cleanups (old_cleanups);
1151 }
1152
1153 /* Asynchronous version of wait_for_inferior. It is called by the
1154 event loop whenever a change of state is detected on the file
1155 descriptor corresponding to the target. It can be called more than
1156 once to complete a single execution command. In such cases we need
1157 to keep the state in a global variable ASYNC_ECSS. If it is the
1158 last time that this function is called for a single execution
1159 command, then report to the user that the inferior has stopped, and
1160 do the necessary cleanups. */
1161
1162 struct execution_control_state async_ecss;
1163 struct execution_control_state *async_ecs;
1164
1165 void
1166 fetch_inferior_event (void *client_data)
1167 {
1168 static struct cleanup *old_cleanups;
1169
1170 async_ecs = &async_ecss;
1171
1172 if (!async_ecs->wait_some_more)
1173 {
1174 old_cleanups = make_exec_cleanup (delete_step_resume_breakpoint,
1175 &step_resume_breakpoint);
1176 make_exec_cleanup (delete_breakpoint_current_contents,
1177 &through_sigtramp_breakpoint);
1178
1179 /* Fill in with reasonable starting values. */
1180 init_execution_control_state (async_ecs);
1181
1182 /* We'll update this if & when we switch to a new thread. */
1183 previous_inferior_ptid = inferior_ptid;
1184
1185 overlay_cache_invalid = 1;
1186
1187 /* We have to invalidate the registers BEFORE calling target_wait
1188 because they can be loaded from the target while in target_wait.
1189 This makes remote debugging a bit more efficient for those
1190 targets that provide critical registers as part of their normal
1191 status mechanism. */
1192
1193 registers_changed ();
1194 }
1195
1196 if (target_wait_hook)
1197 async_ecs->ptid =
1198 target_wait_hook (async_ecs->waiton_ptid, async_ecs->wp);
1199 else
1200 async_ecs->ptid = target_wait (async_ecs->waiton_ptid, async_ecs->wp);
1201
1202 /* Now figure out what to do with the result of the result. */
1203 handle_inferior_event (async_ecs);
1204
1205 if (!async_ecs->wait_some_more)
1206 {
1207 /* Do only the cleanups that have been added by this
1208 function. Let the continuations for the commands do the rest,
1209 if there are any. */
1210 do_exec_cleanups (old_cleanups);
1211 normal_stop ();
1212 if (step_multi && stop_step)
1213 inferior_event_handler (INF_EXEC_CONTINUE, NULL);
1214 else
1215 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
1216 }
1217 }
1218
1219 /* Prepare an execution control state for looping through a
1220 wait_for_inferior-type loop. */
1221
1222 void
1223 init_execution_control_state (struct execution_control_state *ecs)
1224 {
1225 /* ecs->another_trap? */
1226 ecs->random_signal = 0;
1227 ecs->remove_breakpoints_on_following_step = 0;
1228 ecs->handling_longjmp = 0; /* FIXME */
1229 ecs->update_step_sp = 0;
1230 ecs->stepping_through_solib_after_catch = 0;
1231 ecs->stepping_through_solib_catchpoints = NULL;
1232 ecs->enable_hw_watchpoints_after_wait = 0;
1233 ecs->stepping_through_sigtramp = 0;
1234 ecs->sal = find_pc_line (prev_pc, 0);
1235 ecs->current_line = ecs->sal.line;
1236 ecs->current_symtab = ecs->sal.symtab;
1237 ecs->infwait_state = infwait_normal_state;
1238 ecs->waiton_ptid = pid_to_ptid (-1);
1239 ecs->wp = &(ecs->ws);
1240 }
1241
1242 /* Call this function before setting step_resume_breakpoint, as a
1243 sanity check. There should never be more than one step-resume
1244 breakpoint per thread, so we should never be setting a new
1245 step_resume_breakpoint when one is already active. */
1246 static void
1247 check_for_old_step_resume_breakpoint (void)
1248 {
1249 if (step_resume_breakpoint)
1250 warning
1251 ("GDB bug: infrun.c (wait_for_inferior): dropping old step_resume breakpoint");
1252 }
1253
1254 /* Return the cached copy of the last pid/waitstatus returned by
1255 target_wait()/target_wait_hook(). The data is actually cached by
1256 handle_inferior_event(), which gets called immediately after
1257 target_wait()/target_wait_hook(). */
1258
1259 void
1260 get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
1261 {
1262 *ptidp = target_last_wait_ptid;
1263 *status = target_last_waitstatus;
1264 }
1265
1266 /* Switch thread contexts, maintaining "infrun state". */
1267
1268 static void
1269 context_switch (struct execution_control_state *ecs)
1270 {
1271 /* Caution: it may happen that the new thread (or the old one!)
1272 is not in the thread list. In this case we must not attempt
1273 to "switch context", or we run the risk that our context may
1274 be lost. This may happen as a result of the target module
1275 mishandling thread creation. */
1276
1277 if (in_thread_list (inferior_ptid) && in_thread_list (ecs->ptid))
1278 { /* Perform infrun state context switch: */
1279 /* Save infrun state for the old thread. */
1280 save_infrun_state (inferior_ptid, prev_pc,
1281 prev_func_start, prev_func_name,
1282 trap_expected, step_resume_breakpoint,
1283 through_sigtramp_breakpoint, step_range_start,
1284 step_range_end, &step_frame_id,
1285 ecs->handling_longjmp, ecs->another_trap,
1286 ecs->stepping_through_solib_after_catch,
1287 ecs->stepping_through_solib_catchpoints,
1288 ecs->stepping_through_sigtramp,
1289 ecs->current_line, ecs->current_symtab, step_sp);
1290
1291 /* Load infrun state for the new thread. */
1292 load_infrun_state (ecs->ptid, &prev_pc,
1293 &prev_func_start, &prev_func_name,
1294 &trap_expected, &step_resume_breakpoint,
1295 &through_sigtramp_breakpoint, &step_range_start,
1296 &step_range_end, &step_frame_id,
1297 &ecs->handling_longjmp, &ecs->another_trap,
1298 &ecs->stepping_through_solib_after_catch,
1299 &ecs->stepping_through_solib_catchpoints,
1300 &ecs->stepping_through_sigtramp,
1301 &ecs->current_line, &ecs->current_symtab, &step_sp);
1302 }
1303 inferior_ptid = ecs->ptid;
1304 }
1305
1306
1307 /* Given an execution control state that has been freshly filled in
1308 by an event from the inferior, figure out what it means and take
1309 appropriate action. */
1310
1311 void
1312 handle_inferior_event (struct execution_control_state *ecs)
1313 {
1314 CORE_ADDR tmp;
1315 int stepped_after_stopped_by_watchpoint;
1316 int sw_single_step_trap_p = 0;
1317
1318 /* Cache the last pid/waitstatus. */
1319 target_last_wait_ptid = ecs->ptid;
1320 target_last_waitstatus = *ecs->wp;
1321
1322 switch (ecs->infwait_state)
1323 {
1324 case infwait_thread_hop_state:
1325 /* Cancel the waiton_ptid. */
1326 ecs->waiton_ptid = pid_to_ptid (-1);
1327 /* Fall thru to the normal_state case. */
1328
1329 case infwait_normal_state:
1330 /* See comments where a TARGET_WAITKIND_SYSCALL_RETURN event
1331 is serviced in this loop, below. */
1332 if (ecs->enable_hw_watchpoints_after_wait)
1333 {
1334 TARGET_ENABLE_HW_WATCHPOINTS (PIDGET (inferior_ptid));
1335 ecs->enable_hw_watchpoints_after_wait = 0;
1336 }
1337 stepped_after_stopped_by_watchpoint = 0;
1338 break;
1339
1340 case infwait_nullified_state:
1341 break;
1342
1343 case infwait_nonstep_watch_state:
1344 insert_breakpoints ();
1345
1346 /* FIXME-maybe: is this cleaner than setting a flag? Does it
1347 handle things like signals arriving and other things happening
1348 in combination correctly? */
1349 stepped_after_stopped_by_watchpoint = 1;
1350 break;
1351 }
1352 ecs->infwait_state = infwait_normal_state;
1353
1354 flush_cached_frames ();
1355
1356 /* If it's a new process, add it to the thread database */
1357
1358 ecs->new_thread_event = (!ptid_equal (ecs->ptid, inferior_ptid)
1359 && !in_thread_list (ecs->ptid));
1360
1361 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
1362 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED && ecs->new_thread_event)
1363 {
1364 add_thread (ecs->ptid);
1365
1366 ui_out_text (uiout, "[New ");
1367 ui_out_text (uiout, target_pid_or_tid_to_str (ecs->ptid));
1368 ui_out_text (uiout, "]\n");
1369
1370 #if 0
1371 /* NOTE: This block is ONLY meant to be invoked in case of a
1372 "thread creation event"! If it is invoked for any other
1373 sort of event (such as a new thread landing on a breakpoint),
1374 the event will be discarded, which is almost certainly
1375 a bad thing!
1376
1377 To avoid this, the low-level module (eg. target_wait)
1378 should call in_thread_list and add_thread, so that the
1379 new thread is known by the time we get here. */
1380
1381 /* We may want to consider not doing a resume here in order
1382 to give the user a chance to play with the new thread.
1383 It might be good to make that a user-settable option. */
1384
1385 /* At this point, all threads are stopped (happens
1386 automatically in either the OS or the native code).
1387 Therefore we need to continue all threads in order to
1388 make progress. */
1389
1390 target_resume (RESUME_ALL, 0, TARGET_SIGNAL_0);
1391 prepare_to_wait (ecs);
1392 return;
1393 #endif
1394 }
1395
1396 switch (ecs->ws.kind)
1397 {
1398 case TARGET_WAITKIND_LOADED:
1399 /* Ignore gracefully during startup of the inferior, as it
1400 might be the shell which has just loaded some objects,
1401 otherwise add the symbols for the newly loaded objects. */
1402 #ifdef SOLIB_ADD
1403 if (!stop_soon_quietly)
1404 {
1405 /* Remove breakpoints, SOLIB_ADD might adjust
1406 breakpoint addresses via breakpoint_re_set. */
1407 if (breakpoints_inserted)
1408 remove_breakpoints ();
1409
1410 /* Check for any newly added shared libraries if we're
1411 supposed to be adding them automatically. Switch
1412 terminal for any messages produced by
1413 breakpoint_re_set. */
1414 target_terminal_ours_for_output ();
1415 SOLIB_ADD (NULL, 0, NULL, auto_solib_add);
1416 target_terminal_inferior ();
1417
1418 /* Reinsert breakpoints and continue. */
1419 if (breakpoints_inserted)
1420 insert_breakpoints ();
1421 }
1422 #endif
1423 resume (0, TARGET_SIGNAL_0);
1424 prepare_to_wait (ecs);
1425 return;
1426
1427 case TARGET_WAITKIND_SPURIOUS:
1428 resume (0, TARGET_SIGNAL_0);
1429 prepare_to_wait (ecs);
1430 return;
1431
1432 case TARGET_WAITKIND_EXITED:
1433 target_terminal_ours (); /* Must do this before mourn anyway */
1434 print_stop_reason (EXITED, ecs->ws.value.integer);
1435
1436 /* Record the exit code in the convenience variable $_exitcode, so
1437 that the user can inspect this again later. */
1438 set_internalvar (lookup_internalvar ("_exitcode"),
1439 value_from_longest (builtin_type_int,
1440 (LONGEST) ecs->ws.value.integer));
1441 gdb_flush (gdb_stdout);
1442 target_mourn_inferior ();
1443 singlestep_breakpoints_inserted_p = 0; /*SOFTWARE_SINGLE_STEP_P() */
1444 stop_print_frame = 0;
1445 stop_stepping (ecs);
1446 return;
1447
1448 case TARGET_WAITKIND_SIGNALLED:
1449 stop_print_frame = 0;
1450 stop_signal = ecs->ws.value.sig;
1451 target_terminal_ours (); /* Must do this before mourn anyway */
1452
1453 /* Note: By definition of TARGET_WAITKIND_SIGNALLED, we shouldn't
1454 reach here unless the inferior is dead. However, for years
1455 target_kill() was called here, which hints that fatal signals aren't
1456 really fatal on some systems. If that's true, then some changes
1457 may be needed. */
1458 target_mourn_inferior ();
1459
1460 print_stop_reason (SIGNAL_EXITED, stop_signal);
1461 singlestep_breakpoints_inserted_p = 0; /*SOFTWARE_SINGLE_STEP_P() */
1462 stop_stepping (ecs);
1463 return;
1464
1465 /* The following are the only cases in which we keep going;
1466 the above cases end in a continue or goto. */
1467 case TARGET_WAITKIND_FORKED:
1468 stop_signal = TARGET_SIGNAL_TRAP;
1469 pending_follow.kind = ecs->ws.kind;
1470
1471 pending_follow.fork_event.saw_child_fork = 1;
1472 pending_follow.fork_event.parent_pid = PIDGET (ecs->ptid);
1473 pending_follow.fork_event.child_pid = ecs->ws.value.related_pid;
1474
1475 stop_pc = read_pc_pid (ecs->ptid);
1476 ecs->saved_inferior_ptid = inferior_ptid;
1477 inferior_ptid = ecs->ptid;
1478 /* The second argument of bpstat_stop_status is meant to help
1479 distinguish between a breakpoint trap and a singlestep trap.
1480 This is only important on targets where DECR_PC_AFTER_BREAK
1481 is non-zero. The prev_pc test is meant to distinguish between
1482 singlestepping a trap instruction, and singlestepping thru a
1483 jump to the instruction following a trap instruction. */
1484
1485 stop_bpstat = bpstat_stop_status (&stop_pc,
1486 currently_stepping (ecs) &&
1487 prev_pc !=
1488 stop_pc - DECR_PC_AFTER_BREAK);
1489 ecs->random_signal = !bpstat_explains_signal (stop_bpstat);
1490 inferior_ptid = ecs->saved_inferior_ptid;
1491 goto process_event_stop_test;
1492
1493 /* If this a platform which doesn't allow a debugger to touch a
1494 vfork'd inferior until after it exec's, then we'd best keep
1495 our fingers entirely off the inferior, other than continuing
1496 it. This has the unfortunate side-effect that catchpoints
1497 of vforks will be ignored. But since the platform doesn't
1498 allow the inferior be touched at vfork time, there's really
1499 little choice. */
1500 case TARGET_WAITKIND_VFORKED:
1501 stop_signal = TARGET_SIGNAL_TRAP;
1502 pending_follow.kind = ecs->ws.kind;
1503
1504 /* Is this a vfork of the parent? If so, then give any
1505 vfork catchpoints a chance to trigger now. (It's
1506 dangerous to do so if the child canot be touched until
1507 it execs, and the child has not yet exec'd. We probably
1508 should warn the user to that effect when the catchpoint
1509 triggers...) */
1510 if (ptid_equal (ecs->ptid, inferior_ptid))
1511 {
1512 pending_follow.fork_event.saw_parent_fork = 1;
1513 pending_follow.fork_event.parent_pid = PIDGET (ecs->ptid);
1514 pending_follow.fork_event.child_pid = ecs->ws.value.related_pid;
1515 }
1516
1517 /* If we've seen the child's vfork event but cannot really touch
1518 the child until it execs, then we must continue the child now.
1519 Else, give any vfork catchpoints a chance to trigger now. */
1520 else
1521 {
1522 pending_follow.fork_event.saw_child_fork = 1;
1523 pending_follow.fork_event.child_pid = PIDGET (ecs->ptid);
1524 pending_follow.fork_event.parent_pid = ecs->ws.value.related_pid;
1525 target_post_startup_inferior (pid_to_ptid
1526 (pending_follow.fork_event.
1527 child_pid));
1528 }
1529
1530 stop_pc = read_pc ();
1531 /* The second argument of bpstat_stop_status is meant to help
1532 distinguish between a breakpoint trap and a singlestep trap.
1533 This is only important on targets where DECR_PC_AFTER_BREAK
1534 is non-zero. The prev_pc test is meant to distinguish between
1535 singlestepping a trap instruction, and singlestepping thru a
1536 jump to the instruction following a trap instruction. */
1537
1538 stop_bpstat = bpstat_stop_status (&stop_pc,
1539 currently_stepping (ecs) &&
1540 prev_pc !=
1541 stop_pc - DECR_PC_AFTER_BREAK);
1542 ecs->random_signal = !bpstat_explains_signal (stop_bpstat);
1543 goto process_event_stop_test;
1544
1545 case TARGET_WAITKIND_EXECD:
1546 stop_signal = TARGET_SIGNAL_TRAP;
1547
1548 /* NOTE drow/2002-12-05: This code should be pushed down into the
1549 target_wait function. Until then following vfork on HP/UX 10.20
1550 is probably broken by this. Of course, it's broken anyway. */
1551 /* Is this a target which reports multiple exec events per actual
1552 call to exec()? (HP-UX using ptrace does, for example.) If so,
1553 ignore all but the last one. Just resume the exec'r, and wait
1554 for the next exec event. */
1555 if (inferior_ignoring_leading_exec_events)
1556 {
1557 inferior_ignoring_leading_exec_events--;
1558 if (pending_follow.kind == TARGET_WAITKIND_VFORKED)
1559 ENSURE_VFORKING_PARENT_REMAINS_STOPPED (pending_follow.fork_event.
1560 parent_pid);
1561 target_resume (ecs->ptid, 0, TARGET_SIGNAL_0);
1562 prepare_to_wait (ecs);
1563 return;
1564 }
1565 inferior_ignoring_leading_exec_events =
1566 target_reported_exec_events_per_exec_call () - 1;
1567
1568 pending_follow.execd_pathname =
1569 savestring (ecs->ws.value.execd_pathname,
1570 strlen (ecs->ws.value.execd_pathname));
1571
1572 /* This causes the eventpoints and symbol table to be reset. Must
1573 do this now, before trying to determine whether to stop. */
1574 follow_exec (PIDGET (inferior_ptid), pending_follow.execd_pathname);
1575 xfree (pending_follow.execd_pathname);
1576
1577 stop_pc = read_pc_pid (ecs->ptid);
1578 ecs->saved_inferior_ptid = inferior_ptid;
1579 inferior_ptid = ecs->ptid;
1580 /* The second argument of bpstat_stop_status is meant to help
1581 distinguish between a breakpoint trap and a singlestep trap.
1582 This is only important on targets where DECR_PC_AFTER_BREAK
1583 is non-zero. The prev_pc test is meant to distinguish between
1584 singlestepping a trap instruction, and singlestepping thru a
1585 jump to the instruction following a trap instruction. */
1586
1587 stop_bpstat = bpstat_stop_status (&stop_pc,
1588 currently_stepping (ecs) &&
1589 prev_pc !=
1590 stop_pc - DECR_PC_AFTER_BREAK);
1591 ecs->random_signal = !bpstat_explains_signal (stop_bpstat);
1592 inferior_ptid = ecs->saved_inferior_ptid;
1593 goto process_event_stop_test;
1594
1595 /* These syscall events are returned on HP-UX, as part of its
1596 implementation of page-protection-based "hardware" watchpoints.
1597 HP-UX has unfortunate interactions between page-protections and
1598 some system calls. Our solution is to disable hardware watches
1599 when a system call is entered, and reenable them when the syscall
1600 completes. The downside of this is that we may miss the precise
1601 point at which a watched piece of memory is modified. "Oh well."
1602
1603 Note that we may have multiple threads running, which may each
1604 enter syscalls at roughly the same time. Since we don't have a
1605 good notion currently of whether a watched piece of memory is
1606 thread-private, we'd best not have any page-protections active
1607 when any thread is in a syscall. Thus, we only want to reenable
1608 hardware watches when no threads are in a syscall.
1609
1610 Also, be careful not to try to gather much state about a thread
1611 that's in a syscall. It's frequently a losing proposition. */
1612 case TARGET_WAITKIND_SYSCALL_ENTRY:
1613 number_of_threads_in_syscalls++;
1614 if (number_of_threads_in_syscalls == 1)
1615 {
1616 TARGET_DISABLE_HW_WATCHPOINTS (PIDGET (inferior_ptid));
1617 }
1618 resume (0, TARGET_SIGNAL_0);
1619 prepare_to_wait (ecs);
1620 return;
1621
1622 /* Before examining the threads further, step this thread to
1623 get it entirely out of the syscall. (We get notice of the
1624 event when the thread is just on the verge of exiting a
1625 syscall. Stepping one instruction seems to get it back
1626 into user code.)
1627
1628 Note that although the logical place to reenable h/w watches
1629 is here, we cannot. We cannot reenable them before stepping
1630 the thread (this causes the next wait on the thread to hang).
1631
1632 Nor can we enable them after stepping until we've done a wait.
1633 Thus, we simply set the flag ecs->enable_hw_watchpoints_after_wait
1634 here, which will be serviced immediately after the target
1635 is waited on. */
1636 case TARGET_WAITKIND_SYSCALL_RETURN:
1637 target_resume (ecs->ptid, 1, TARGET_SIGNAL_0);
1638
1639 if (number_of_threads_in_syscalls > 0)
1640 {
1641 number_of_threads_in_syscalls--;
1642 ecs->enable_hw_watchpoints_after_wait =
1643 (number_of_threads_in_syscalls == 0);
1644 }
1645 prepare_to_wait (ecs);
1646 return;
1647
1648 case TARGET_WAITKIND_STOPPED:
1649 stop_signal = ecs->ws.value.sig;
1650 break;
1651
1652 /* We had an event in the inferior, but we are not interested
1653 in handling it at this level. The lower layers have already
1654 done what needs to be done, if anything.
1655
1656 One of the possible circumstances for this is when the
1657 inferior produces output for the console. The inferior has
1658 not stopped, and we are ignoring the event. Another possible
1659 circumstance is any event which the lower level knows will be
1660 reported multiple times without an intervening resume. */
1661 case TARGET_WAITKIND_IGNORE:
1662 prepare_to_wait (ecs);
1663 return;
1664 }
1665
1666 /* We may want to consider not doing a resume here in order to give
1667 the user a chance to play with the new thread. It might be good
1668 to make that a user-settable option. */
1669
1670 /* At this point, all threads are stopped (happens automatically in
1671 either the OS or the native code). Therefore we need to continue
1672 all threads in order to make progress. */
1673 if (ecs->new_thread_event)
1674 {
1675 target_resume (RESUME_ALL, 0, TARGET_SIGNAL_0);
1676 prepare_to_wait (ecs);
1677 return;
1678 }
1679
1680 stop_pc = read_pc_pid (ecs->ptid);
1681
1682 /* See if a thread hit a thread-specific breakpoint that was meant for
1683 another thread. If so, then step that thread past the breakpoint,
1684 and continue it. */
1685
1686 if (stop_signal == TARGET_SIGNAL_TRAP)
1687 {
1688 /* Check if a regular breakpoint has been hit before checking
1689 for a potential single step breakpoint. Otherwise, GDB will
1690 not see this breakpoint hit when stepping onto breakpoints. */
1691 if (breakpoints_inserted
1692 && breakpoint_here_p (stop_pc - DECR_PC_AFTER_BREAK))
1693 {
1694 ecs->random_signal = 0;
1695 if (!breakpoint_thread_match (stop_pc - DECR_PC_AFTER_BREAK,
1696 ecs->ptid))
1697 {
1698 int remove_status;
1699
1700 /* Saw a breakpoint, but it was hit by the wrong thread.
1701 Just continue. */
1702 if (DECR_PC_AFTER_BREAK)
1703 write_pc_pid (stop_pc - DECR_PC_AFTER_BREAK, ecs->ptid);
1704
1705 remove_status = remove_breakpoints ();
1706 /* Did we fail to remove breakpoints? If so, try
1707 to set the PC past the bp. (There's at least
1708 one situation in which we can fail to remove
1709 the bp's: On HP-UX's that use ttrace, we can't
1710 change the address space of a vforking child
1711 process until the child exits (well, okay, not
1712 then either :-) or execs. */
1713 if (remove_status != 0)
1714 {
1715 /* FIXME! This is obviously non-portable! */
1716 write_pc_pid (stop_pc - DECR_PC_AFTER_BREAK + 4, ecs->ptid);
1717 /* We need to restart all the threads now,
1718 * unles we're running in scheduler-locked mode.
1719 * Use currently_stepping to determine whether to
1720 * step or continue.
1721 */
1722 /* FIXME MVS: is there any reason not to call resume()? */
1723 if (scheduler_mode == schedlock_on)
1724 target_resume (ecs->ptid,
1725 currently_stepping (ecs), TARGET_SIGNAL_0);
1726 else
1727 target_resume (RESUME_ALL,
1728 currently_stepping (ecs), TARGET_SIGNAL_0);
1729 prepare_to_wait (ecs);
1730 return;
1731 }
1732 else
1733 { /* Single step */
1734 breakpoints_inserted = 0;
1735 if (!ptid_equal (inferior_ptid, ecs->ptid))
1736 context_switch (ecs);
1737 ecs->waiton_ptid = ecs->ptid;
1738 ecs->wp = &(ecs->ws);
1739 ecs->another_trap = 1;
1740
1741 ecs->infwait_state = infwait_thread_hop_state;
1742 keep_going (ecs);
1743 registers_changed ();
1744 return;
1745 }
1746 }
1747 }
1748 else if (SOFTWARE_SINGLE_STEP_P () && singlestep_breakpoints_inserted_p)
1749 {
1750 /* Readjust the stop_pc as it is off by DECR_PC_AFTER_BREAK
1751 compared to the value it would have if the system stepping
1752 capability was used. This allows the rest of the code in
1753 this function to use this address without having to worry
1754 whether software single step is in use or not. */
1755 if (DECR_PC_AFTER_BREAK)
1756 {
1757 stop_pc -= DECR_PC_AFTER_BREAK;
1758 write_pc_pid (stop_pc, ecs->ptid);
1759 }
1760
1761 sw_single_step_trap_p = 1;
1762 ecs->random_signal = 0;
1763 }
1764 }
1765 else
1766 ecs->random_signal = 1;
1767
1768 /* See if something interesting happened to the non-current thread. If
1769 so, then switch to that thread, and eventually give control back to
1770 the user.
1771
1772 Note that if there's any kind of pending follow (i.e., of a fork,
1773 vfork or exec), we don't want to do this now. Rather, we'll let
1774 the next resume handle it. */
1775 if (!ptid_equal (ecs->ptid, inferior_ptid) &&
1776 (pending_follow.kind == TARGET_WAITKIND_SPURIOUS))
1777 {
1778 int printed = 0;
1779
1780 /* If it's a random signal for a non-current thread, notify user
1781 if he's expressed an interest. */
1782 if (ecs->random_signal && signal_print[stop_signal])
1783 {
1784 /* ??rehrauer: I don't understand the rationale for this code. If the
1785 inferior will stop as a result of this signal, then the act of handling
1786 the stop ought to print a message that's couches the stoppage in user
1787 terms, e.g., "Stopped for breakpoint/watchpoint". If the inferior
1788 won't stop as a result of the signal -- i.e., if the signal is merely
1789 a side-effect of something GDB's doing "under the covers" for the
1790 user, such as stepping threads over a breakpoint they shouldn't stop
1791 for -- then the message seems to be a serious annoyance at best.
1792
1793 For now, remove the message altogether. */
1794 #if 0
1795 printed = 1;
1796 target_terminal_ours_for_output ();
1797 printf_filtered ("\nProgram received signal %s, %s.\n",
1798 target_signal_to_name (stop_signal),
1799 target_signal_to_string (stop_signal));
1800 gdb_flush (gdb_stdout);
1801 #endif
1802 }
1803
1804 /* If it's not SIGTRAP and not a signal we want to stop for, then
1805 continue the thread. */
1806
1807 if (stop_signal != TARGET_SIGNAL_TRAP && !signal_stop[stop_signal])
1808 {
1809 if (printed)
1810 target_terminal_inferior ();
1811
1812 /* Clear the signal if it should not be passed. */
1813 if (signal_program[stop_signal] == 0)
1814 stop_signal = TARGET_SIGNAL_0;
1815
1816 target_resume (ecs->ptid, 0, stop_signal);
1817 prepare_to_wait (ecs);
1818 return;
1819 }
1820
1821 /* It's a SIGTRAP or a signal we're interested in. Switch threads,
1822 and fall into the rest of wait_for_inferior(). */
1823
1824 context_switch (ecs);
1825
1826 if (context_hook)
1827 context_hook (pid_to_thread_id (ecs->ptid));
1828
1829 flush_cached_frames ();
1830 }
1831
1832 if (SOFTWARE_SINGLE_STEP_P () && singlestep_breakpoints_inserted_p)
1833 {
1834 /* Pull the single step breakpoints out of the target. */
1835 SOFTWARE_SINGLE_STEP (0, 0);
1836 singlestep_breakpoints_inserted_p = 0;
1837 }
1838
1839 /* If PC is pointing at a nullified instruction, then step beyond
1840 it so that the user won't be confused when GDB appears to be ready
1841 to execute it. */
1842
1843 /* if (INSTRUCTION_NULLIFIED && currently_stepping (ecs)) */
1844 if (INSTRUCTION_NULLIFIED)
1845 {
1846 registers_changed ();
1847 target_resume (ecs->ptid, 1, TARGET_SIGNAL_0);
1848
1849 /* We may have received a signal that we want to pass to
1850 the inferior; therefore, we must not clobber the waitstatus
1851 in WS. */
1852
1853 ecs->infwait_state = infwait_nullified_state;
1854 ecs->waiton_ptid = ecs->ptid;
1855 ecs->wp = &(ecs->tmpstatus);
1856 prepare_to_wait (ecs);
1857 return;
1858 }
1859
1860 /* It may not be necessary to disable the watchpoint to stop over
1861 it. For example, the PA can (with some kernel cooperation)
1862 single step over a watchpoint without disabling the watchpoint. */
1863 if (HAVE_STEPPABLE_WATCHPOINT && STOPPED_BY_WATCHPOINT (ecs->ws))
1864 {
1865 resume (1, 0);
1866 prepare_to_wait (ecs);
1867 return;
1868 }
1869
1870 /* It is far more common to need to disable a watchpoint to step
1871 the inferior over it. FIXME. What else might a debug
1872 register or page protection watchpoint scheme need here? */
1873 if (HAVE_NONSTEPPABLE_WATCHPOINT && STOPPED_BY_WATCHPOINT (ecs->ws))
1874 {
1875 /* At this point, we are stopped at an instruction which has
1876 attempted to write to a piece of memory under control of
1877 a watchpoint. The instruction hasn't actually executed
1878 yet. If we were to evaluate the watchpoint expression
1879 now, we would get the old value, and therefore no change
1880 would seem to have occurred.
1881
1882 In order to make watchpoints work `right', we really need
1883 to complete the memory write, and then evaluate the
1884 watchpoint expression. The following code does that by
1885 removing the watchpoint (actually, all watchpoints and
1886 breakpoints), single-stepping the target, re-inserting
1887 watchpoints, and then falling through to let normal
1888 single-step processing handle proceed. Since this
1889 includes evaluating watchpoints, things will come to a
1890 stop in the correct manner. */
1891
1892 if (DECR_PC_AFTER_BREAK)
1893 write_pc (stop_pc - DECR_PC_AFTER_BREAK);
1894
1895 remove_breakpoints ();
1896 registers_changed ();
1897 target_resume (ecs->ptid, 1, TARGET_SIGNAL_0); /* Single step */
1898
1899 ecs->waiton_ptid = ecs->ptid;
1900 ecs->wp = &(ecs->ws);
1901 ecs->infwait_state = infwait_nonstep_watch_state;
1902 prepare_to_wait (ecs);
1903 return;
1904 }
1905
1906 /* It may be possible to simply continue after a watchpoint. */
1907 if (HAVE_CONTINUABLE_WATCHPOINT)
1908 STOPPED_BY_WATCHPOINT (ecs->ws);
1909
1910 ecs->stop_func_start = 0;
1911 ecs->stop_func_end = 0;
1912 ecs->stop_func_name = 0;
1913 /* Don't care about return value; stop_func_start and stop_func_name
1914 will both be 0 if it doesn't work. */
1915 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
1916 &ecs->stop_func_start, &ecs->stop_func_end);
1917 ecs->stop_func_start += FUNCTION_START_OFFSET;
1918 ecs->another_trap = 0;
1919 bpstat_clear (&stop_bpstat);
1920 stop_step = 0;
1921 stop_stack_dummy = 0;
1922 stop_print_frame = 1;
1923 ecs->random_signal = 0;
1924 stopped_by_random_signal = 0;
1925 breakpoints_failed = 0;
1926
1927 /* Look at the cause of the stop, and decide what to do.
1928 The alternatives are:
1929 1) break; to really stop and return to the debugger,
1930 2) drop through to start up again
1931 (set ecs->another_trap to 1 to single step once)
1932 3) set ecs->random_signal to 1, and the decision between 1 and 2
1933 will be made according to the signal handling tables. */
1934
1935 /* First, distinguish signals caused by the debugger from signals
1936 that have to do with the program's own actions.
1937 Note that breakpoint insns may cause SIGTRAP or SIGILL
1938 or SIGEMT, depending on the operating system version.
1939 Here we detect when a SIGILL or SIGEMT is really a breakpoint
1940 and change it to SIGTRAP. */
1941
1942 if (stop_signal == TARGET_SIGNAL_TRAP
1943 || (breakpoints_inserted &&
1944 (stop_signal == TARGET_SIGNAL_ILL
1945 || stop_signal == TARGET_SIGNAL_EMT)) || stop_soon_quietly)
1946 {
1947 if (stop_signal == TARGET_SIGNAL_TRAP && stop_after_trap)
1948 {
1949 stop_print_frame = 0;
1950 stop_stepping (ecs);
1951 return;
1952 }
1953 if (stop_soon_quietly)
1954 {
1955 stop_stepping (ecs);
1956 return;
1957 }
1958
1959 /* Don't even think about breakpoints
1960 if just proceeded over a breakpoint.
1961
1962 However, if we are trying to proceed over a breakpoint
1963 and end up in sigtramp, then through_sigtramp_breakpoint
1964 will be set and we should check whether we've hit the
1965 step breakpoint. */
1966 if (stop_signal == TARGET_SIGNAL_TRAP && trap_expected
1967 && through_sigtramp_breakpoint == NULL)
1968 bpstat_clear (&stop_bpstat);
1969 else
1970 {
1971 /* See if there is a breakpoint at the current PC. */
1972
1973 /* The second argument of bpstat_stop_status is meant to help
1974 distinguish between a breakpoint trap and a singlestep trap.
1975 This is only important on targets where DECR_PC_AFTER_BREAK
1976 is non-zero. The prev_pc test is meant to distinguish between
1977 singlestepping a trap instruction, and singlestepping thru a
1978 jump to the instruction following a trap instruction.
1979
1980 Therefore, pass TRUE if our reason for stopping is
1981 something other than hitting a breakpoint. We do this by
1982 checking that either: we detected earlier a software single
1983 step trap or, 1) stepping is going on and 2) we didn't hit
1984 a breakpoint in a signal handler without an intervening stop
1985 in sigtramp, which is detected by a new stack pointer value
1986 below any usual function calling stack adjustments. */
1987 stop_bpstat =
1988 bpstat_stop_status
1989 (&stop_pc,
1990 sw_single_step_trap_p
1991 || (currently_stepping (ecs)
1992 && prev_pc != stop_pc - DECR_PC_AFTER_BREAK
1993 && !(step_range_end
1994 && INNER_THAN (read_sp (), (step_sp - 16)))));
1995 /* Following in case break condition called a
1996 function. */
1997 stop_print_frame = 1;
1998 }
1999
2000 if (stop_signal == TARGET_SIGNAL_TRAP)
2001 ecs->random_signal
2002 = !(bpstat_explains_signal (stop_bpstat)
2003 || trap_expected
2004 || (!CALL_DUMMY_BREAKPOINT_OFFSET_P
2005 && DEPRECATED_PC_IN_CALL_DUMMY (stop_pc, read_sp (),
2006 get_frame_base (get_current_frame ())))
2007 || (step_range_end && step_resume_breakpoint == NULL));
2008
2009 else
2010 {
2011 ecs->random_signal = !(bpstat_explains_signal (stop_bpstat)
2012 /* End of a stack dummy. Some systems (e.g. Sony
2013 news) give another signal besides SIGTRAP, so
2014 check here as well as above. */
2015 || (!CALL_DUMMY_BREAKPOINT_OFFSET_P
2016 && DEPRECATED_PC_IN_CALL_DUMMY (stop_pc, read_sp (),
2017 get_frame_base
2018 (get_current_frame
2019 ()))));
2020 if (!ecs->random_signal)
2021 stop_signal = TARGET_SIGNAL_TRAP;
2022 }
2023 }
2024
2025 /* When we reach this point, we've pretty much decided
2026 that the reason for stopping must've been a random
2027 (unexpected) signal. */
2028
2029 else
2030 ecs->random_signal = 1;
2031 /* If a fork, vfork or exec event was seen, then there are two
2032 possible responses we can make:
2033
2034 1. If a catchpoint triggers for the event (ecs->random_signal == 0),
2035 then we must stop now and issue a prompt. We will resume
2036 the inferior when the user tells us to.
2037 2. If no catchpoint triggers for the event (ecs->random_signal == 1),
2038 then we must resume the inferior now and keep checking.
2039
2040 In either case, we must take appropriate steps to "follow" the
2041 the fork/vfork/exec when the inferior is resumed. For example,
2042 if follow-fork-mode is "child", then we must detach from the
2043 parent inferior and follow the new child inferior.
2044
2045 In either case, setting pending_follow causes the next resume()
2046 to take the appropriate following action. */
2047 process_event_stop_test:
2048 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
2049 {
2050 if (ecs->random_signal) /* I.e., no catchpoint triggered for this. */
2051 {
2052 trap_expected = 1;
2053 stop_signal = TARGET_SIGNAL_0;
2054 keep_going (ecs);
2055 return;
2056 }
2057 }
2058 else if (ecs->ws.kind == TARGET_WAITKIND_VFORKED)
2059 {
2060 if (ecs->random_signal) /* I.e., no catchpoint triggered for this. */
2061 {
2062 stop_signal = TARGET_SIGNAL_0;
2063 keep_going (ecs);
2064 return;
2065 }
2066 }
2067 else if (ecs->ws.kind == TARGET_WAITKIND_EXECD)
2068 {
2069 pending_follow.kind = ecs->ws.kind;
2070 if (ecs->random_signal) /* I.e., no catchpoint triggered for this. */
2071 {
2072 trap_expected = 1;
2073 stop_signal = TARGET_SIGNAL_0;
2074 keep_going (ecs);
2075 return;
2076 }
2077 }
2078
2079 /* For the program's own signals, act according to
2080 the signal handling tables. */
2081
2082 if (ecs->random_signal)
2083 {
2084 /* Signal not for debugging purposes. */
2085 int printed = 0;
2086
2087 stopped_by_random_signal = 1;
2088
2089 if (signal_print[stop_signal])
2090 {
2091 printed = 1;
2092 target_terminal_ours_for_output ();
2093 print_stop_reason (SIGNAL_RECEIVED, stop_signal);
2094 }
2095 if (signal_stop[stop_signal])
2096 {
2097 stop_stepping (ecs);
2098 return;
2099 }
2100 /* If not going to stop, give terminal back
2101 if we took it away. */
2102 else if (printed)
2103 target_terminal_inferior ();
2104
2105 /* Clear the signal if it should not be passed. */
2106 if (signal_program[stop_signal] == 0)
2107 stop_signal = TARGET_SIGNAL_0;
2108
2109 /* I'm not sure whether this needs to be check_sigtramp2 or
2110 whether it could/should be keep_going.
2111
2112 This used to jump to step_over_function if we are stepping,
2113 which is wrong.
2114
2115 Suppose the user does a `next' over a function call, and while
2116 that call is in progress, the inferior receives a signal for
2117 which GDB does not stop (i.e., signal_stop[SIG] is false). In
2118 that case, when we reach this point, there is already a
2119 step-resume breakpoint established, right where it should be:
2120 immediately after the function call the user is "next"-ing
2121 over. If we call step_over_function now, two bad things
2122 happen:
2123
2124 - we'll create a new breakpoint, at wherever the current
2125 frame's return address happens to be. That could be
2126 anywhere, depending on what function call happens to be on
2127 the top of the stack at that point. Point is, it's probably
2128 not where we need it.
2129
2130 - the existing step-resume breakpoint (which is at the correct
2131 address) will get orphaned: step_resume_breakpoint will point
2132 to the new breakpoint, and the old step-resume breakpoint
2133 will never be cleaned up.
2134
2135 The old behavior was meant to help HP-UX single-step out of
2136 sigtramps. It would place the new breakpoint at prev_pc, which
2137 was certainly wrong. I don't know the details there, so fixing
2138 this probably breaks that. As with anything else, it's up to
2139 the HP-UX maintainer to furnish a fix that doesn't break other
2140 platforms. --JimB, 20 May 1999 */
2141 check_sigtramp2 (ecs);
2142 keep_going (ecs);
2143 return;
2144 }
2145
2146 /* Handle cases caused by hitting a breakpoint. */
2147 {
2148 CORE_ADDR jmp_buf_pc;
2149 struct bpstat_what what;
2150
2151 what = bpstat_what (stop_bpstat);
2152
2153 if (what.call_dummy)
2154 {
2155 stop_stack_dummy = 1;
2156 #ifdef HP_OS_BUG
2157 trap_expected_after_continue = 1;
2158 #endif
2159 }
2160
2161 switch (what.main_action)
2162 {
2163 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
2164 /* If we hit the breakpoint at longjmp, disable it for the
2165 duration of this command. Then, install a temporary
2166 breakpoint at the target of the jmp_buf. */
2167 disable_longjmp_breakpoint ();
2168 remove_breakpoints ();
2169 breakpoints_inserted = 0;
2170 if (!GET_LONGJMP_TARGET_P () || !GET_LONGJMP_TARGET (&jmp_buf_pc))
2171 {
2172 keep_going (ecs);
2173 return;
2174 }
2175
2176 /* Need to blow away step-resume breakpoint, as it
2177 interferes with us */
2178 if (step_resume_breakpoint != NULL)
2179 {
2180 delete_step_resume_breakpoint (&step_resume_breakpoint);
2181 }
2182 /* Not sure whether we need to blow this away too, but probably
2183 it is like the step-resume breakpoint. */
2184 if (through_sigtramp_breakpoint != NULL)
2185 {
2186 delete_breakpoint (through_sigtramp_breakpoint);
2187 through_sigtramp_breakpoint = NULL;
2188 }
2189
2190 #if 0
2191 /* FIXME - Need to implement nested temporary breakpoints */
2192 if (step_over_calls > 0)
2193 set_longjmp_resume_breakpoint (jmp_buf_pc, get_current_frame ());
2194 else
2195 #endif /* 0 */
2196 set_longjmp_resume_breakpoint (jmp_buf_pc, NULL);
2197 ecs->handling_longjmp = 1; /* FIXME */
2198 keep_going (ecs);
2199 return;
2200
2201 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
2202 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
2203 remove_breakpoints ();
2204 breakpoints_inserted = 0;
2205 #if 0
2206 /* FIXME - Need to implement nested temporary breakpoints */
2207 if (step_over_calls
2208 && (frame_id_inner (get_frame_id (get_current_frame ()),
2209 step_frame_id)))
2210 {
2211 ecs->another_trap = 1;
2212 keep_going (ecs);
2213 return;
2214 }
2215 #endif /* 0 */
2216 disable_longjmp_breakpoint ();
2217 ecs->handling_longjmp = 0; /* FIXME */
2218 if (what.main_action == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
2219 break;
2220 /* else fallthrough */
2221
2222 case BPSTAT_WHAT_SINGLE:
2223 if (breakpoints_inserted)
2224 {
2225 remove_breakpoints ();
2226 }
2227 breakpoints_inserted = 0;
2228 ecs->another_trap = 1;
2229 /* Still need to check other stuff, at least the case
2230 where we are stepping and step out of the right range. */
2231 break;
2232
2233 case BPSTAT_WHAT_STOP_NOISY:
2234 stop_print_frame = 1;
2235
2236 /* We are about to nuke the step_resume_breakpoint and
2237 through_sigtramp_breakpoint via the cleanup chain, so
2238 no need to worry about it here. */
2239
2240 stop_stepping (ecs);
2241 return;
2242
2243 case BPSTAT_WHAT_STOP_SILENT:
2244 stop_print_frame = 0;
2245
2246 /* We are about to nuke the step_resume_breakpoint and
2247 through_sigtramp_breakpoint via the cleanup chain, so
2248 no need to worry about it here. */
2249
2250 stop_stepping (ecs);
2251 return;
2252
2253 case BPSTAT_WHAT_STEP_RESUME:
2254 /* This proably demands a more elegant solution, but, yeah
2255 right...
2256
2257 This function's use of the simple variable
2258 step_resume_breakpoint doesn't seem to accomodate
2259 simultaneously active step-resume bp's, although the
2260 breakpoint list certainly can.
2261
2262 If we reach here and step_resume_breakpoint is already
2263 NULL, then apparently we have multiple active
2264 step-resume bp's. We'll just delete the breakpoint we
2265 stopped at, and carry on.
2266
2267 Correction: what the code currently does is delete a
2268 step-resume bp, but it makes no effort to ensure that
2269 the one deleted is the one currently stopped at. MVS */
2270
2271 if (step_resume_breakpoint == NULL)
2272 {
2273 step_resume_breakpoint =
2274 bpstat_find_step_resume_breakpoint (stop_bpstat);
2275 }
2276 delete_step_resume_breakpoint (&step_resume_breakpoint);
2277 break;
2278
2279 case BPSTAT_WHAT_THROUGH_SIGTRAMP:
2280 if (through_sigtramp_breakpoint)
2281 delete_breakpoint (through_sigtramp_breakpoint);
2282 through_sigtramp_breakpoint = NULL;
2283
2284 /* If were waiting for a trap, hitting the step_resume_break
2285 doesn't count as getting it. */
2286 if (trap_expected)
2287 ecs->another_trap = 1;
2288 break;
2289
2290 case BPSTAT_WHAT_CHECK_SHLIBS:
2291 case BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK:
2292 #ifdef SOLIB_ADD
2293 {
2294 /* Remove breakpoints, we eventually want to step over the
2295 shlib event breakpoint, and SOLIB_ADD might adjust
2296 breakpoint addresses via breakpoint_re_set. */
2297 if (breakpoints_inserted)
2298 remove_breakpoints ();
2299 breakpoints_inserted = 0;
2300
2301 /* Check for any newly added shared libraries if we're
2302 supposed to be adding them automatically. Switch
2303 terminal for any messages produced by
2304 breakpoint_re_set. */
2305 target_terminal_ours_for_output ();
2306 SOLIB_ADD (NULL, 0, NULL, auto_solib_add);
2307 target_terminal_inferior ();
2308
2309 /* Try to reenable shared library breakpoints, additional
2310 code segments in shared libraries might be mapped in now. */
2311 re_enable_breakpoints_in_shlibs ();
2312
2313 /* If requested, stop when the dynamic linker notifies
2314 gdb of events. This allows the user to get control
2315 and place breakpoints in initializer routines for
2316 dynamically loaded objects (among other things). */
2317 if (stop_on_solib_events)
2318 {
2319 stop_stepping (ecs);
2320 return;
2321 }
2322
2323 /* If we stopped due to an explicit catchpoint, then the
2324 (see above) call to SOLIB_ADD pulled in any symbols
2325 from a newly-loaded library, if appropriate.
2326
2327 We do want the inferior to stop, but not where it is
2328 now, which is in the dynamic linker callback. Rather,
2329 we would like it stop in the user's program, just after
2330 the call that caused this catchpoint to trigger. That
2331 gives the user a more useful vantage from which to
2332 examine their program's state. */
2333 else if (what.main_action ==
2334 BPSTAT_WHAT_CHECK_SHLIBS_RESUME_FROM_HOOK)
2335 {
2336 /* ??rehrauer: If I could figure out how to get the
2337 right return PC from here, we could just set a temp
2338 breakpoint and resume. I'm not sure we can without
2339 cracking open the dld's shared libraries and sniffing
2340 their unwind tables and text/data ranges, and that's
2341 not a terribly portable notion.
2342
2343 Until that time, we must step the inferior out of the
2344 dld callback, and also out of the dld itself (and any
2345 code or stubs in libdld.sl, such as "shl_load" and
2346 friends) until we reach non-dld code. At that point,
2347 we can stop stepping. */
2348 bpstat_get_triggered_catchpoints (stop_bpstat,
2349 &ecs->
2350 stepping_through_solib_catchpoints);
2351 ecs->stepping_through_solib_after_catch = 1;
2352
2353 /* Be sure to lift all breakpoints, so the inferior does
2354 actually step past this point... */
2355 ecs->another_trap = 1;
2356 break;
2357 }
2358 else
2359 {
2360 /* We want to step over this breakpoint, then keep going. */
2361 ecs->another_trap = 1;
2362 break;
2363 }
2364 }
2365 #endif
2366 break;
2367
2368 case BPSTAT_WHAT_LAST:
2369 /* Not a real code, but listed here to shut up gcc -Wall. */
2370
2371 case BPSTAT_WHAT_KEEP_CHECKING:
2372 break;
2373 }
2374 }
2375
2376 /* We come here if we hit a breakpoint but should not
2377 stop for it. Possibly we also were stepping
2378 and should stop for that. So fall through and
2379 test for stepping. But, if not stepping,
2380 do not stop. */
2381
2382 /* Are we stepping to get the inferior out of the dynamic
2383 linker's hook (and possibly the dld itself) after catching
2384 a shlib event? */
2385 if (ecs->stepping_through_solib_after_catch)
2386 {
2387 #if defined(SOLIB_ADD)
2388 /* Have we reached our destination? If not, keep going. */
2389 if (SOLIB_IN_DYNAMIC_LINKER (PIDGET (ecs->ptid), stop_pc))
2390 {
2391 ecs->another_trap = 1;
2392 keep_going (ecs);
2393 return;
2394 }
2395 #endif
2396 /* Else, stop and report the catchpoint(s) whose triggering
2397 caused us to begin stepping. */
2398 ecs->stepping_through_solib_after_catch = 0;
2399 bpstat_clear (&stop_bpstat);
2400 stop_bpstat = bpstat_copy (ecs->stepping_through_solib_catchpoints);
2401 bpstat_clear (&ecs->stepping_through_solib_catchpoints);
2402 stop_print_frame = 1;
2403 stop_stepping (ecs);
2404 return;
2405 }
2406
2407 if (!CALL_DUMMY_BREAKPOINT_OFFSET_P)
2408 {
2409 /* This is the old way of detecting the end of the stack dummy.
2410 An architecture which defines CALL_DUMMY_BREAKPOINT_OFFSET gets
2411 handled above. As soon as we can test it on all of them, all
2412 architectures should define it. */
2413
2414 /* If this is the breakpoint at the end of a stack dummy,
2415 just stop silently, unless the user was doing an si/ni, in which
2416 case she'd better know what she's doing. */
2417
2418 if (CALL_DUMMY_HAS_COMPLETED (stop_pc, read_sp (),
2419 get_frame_base (get_current_frame ()))
2420 && !step_range_end)
2421 {
2422 stop_print_frame = 0;
2423 stop_stack_dummy = 1;
2424 #ifdef HP_OS_BUG
2425 trap_expected_after_continue = 1;
2426 #endif
2427 stop_stepping (ecs);
2428 return;
2429 }
2430 }
2431
2432 if (step_resume_breakpoint)
2433 {
2434 /* Having a step-resume breakpoint overrides anything
2435 else having to do with stepping commands until
2436 that breakpoint is reached. */
2437 /* I'm not sure whether this needs to be check_sigtramp2 or
2438 whether it could/should be keep_going. */
2439 check_sigtramp2 (ecs);
2440 keep_going (ecs);
2441 return;
2442 }
2443
2444 if (step_range_end == 0)
2445 {
2446 /* Likewise if we aren't even stepping. */
2447 /* I'm not sure whether this needs to be check_sigtramp2 or
2448 whether it could/should be keep_going. */
2449 check_sigtramp2 (ecs);
2450 keep_going (ecs);
2451 return;
2452 }
2453
2454 /* If stepping through a line, keep going if still within it.
2455
2456 Note that step_range_end is the address of the first instruction
2457 beyond the step range, and NOT the address of the last instruction
2458 within it! */
2459 if (stop_pc >= step_range_start && stop_pc < step_range_end)
2460 {
2461 /* We might be doing a BPSTAT_WHAT_SINGLE and getting a signal.
2462 So definately need to check for sigtramp here. */
2463 check_sigtramp2 (ecs);
2464 keep_going (ecs);
2465 return;
2466 }
2467
2468 /* We stepped out of the stepping range. */
2469
2470 /* If we are stepping at the source level and entered the runtime
2471 loader dynamic symbol resolution code, we keep on single stepping
2472 until we exit the run time loader code and reach the callee's
2473 address. */
2474 if (step_over_calls == STEP_OVER_UNDEBUGGABLE
2475 && IN_SOLIB_DYNSYM_RESOLVE_CODE (stop_pc))
2476 {
2477 CORE_ADDR pc_after_resolver = SKIP_SOLIB_RESOLVER (stop_pc);
2478
2479 if (pc_after_resolver)
2480 {
2481 /* Set up a step-resume breakpoint at the address
2482 indicated by SKIP_SOLIB_RESOLVER. */
2483 struct symtab_and_line sr_sal;
2484 init_sal (&sr_sal);
2485 sr_sal.pc = pc_after_resolver;
2486
2487 check_for_old_step_resume_breakpoint ();
2488 step_resume_breakpoint =
2489 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2490 if (breakpoints_inserted)
2491 insert_breakpoints ();
2492 }
2493
2494 keep_going (ecs);
2495 return;
2496 }
2497
2498 /* We can't update step_sp every time through the loop, because
2499 reading the stack pointer would slow down stepping too much.
2500 But we can update it every time we leave the step range. */
2501 ecs->update_step_sp = 1;
2502
2503 /* Did we just take a signal? */
2504 if (PC_IN_SIGTRAMP (stop_pc, ecs->stop_func_name)
2505 && !PC_IN_SIGTRAMP (prev_pc, prev_func_name)
2506 && INNER_THAN (read_sp (), step_sp))
2507 {
2508 /* We've just taken a signal; go until we are back to
2509 the point where we took it and one more. */
2510
2511 /* Note: The test above succeeds not only when we stepped
2512 into a signal handler, but also when we step past the last
2513 statement of a signal handler and end up in the return stub
2514 of the signal handler trampoline. To distinguish between
2515 these two cases, check that the frame is INNER_THAN the
2516 previous one below. pai/1997-09-11 */
2517
2518
2519 {
2520 struct frame_id current_frame = get_frame_id (get_current_frame ());
2521
2522 if (frame_id_inner (current_frame, step_frame_id))
2523 {
2524 /* We have just taken a signal; go until we are back to
2525 the point where we took it and one more. */
2526
2527 /* This code is needed at least in the following case:
2528 The user types "next" and then a signal arrives (before
2529 the "next" is done). */
2530
2531 /* Note that if we are stopped at a breakpoint, then we need
2532 the step_resume breakpoint to override any breakpoints at
2533 the same location, so that we will still step over the
2534 breakpoint even though the signal happened. */
2535 struct symtab_and_line sr_sal;
2536
2537 init_sal (&sr_sal);
2538 sr_sal.symtab = NULL;
2539 sr_sal.line = 0;
2540 sr_sal.pc = prev_pc;
2541 /* We could probably be setting the frame to
2542 step_frame_id; I don't think anyone thought to try it. */
2543 check_for_old_step_resume_breakpoint ();
2544 step_resume_breakpoint =
2545 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2546 if (breakpoints_inserted)
2547 insert_breakpoints ();
2548 }
2549 else
2550 {
2551 /* We just stepped out of a signal handler and into
2552 its calling trampoline.
2553
2554 Normally, we'd call step_over_function from
2555 here, but for some reason GDB can't unwind the
2556 stack correctly to find the real PC for the point
2557 user code where the signal trampoline will return
2558 -- FRAME_SAVED_PC fails, at least on HP-UX 10.20.
2559 But signal trampolines are pretty small stubs of
2560 code, anyway, so it's OK instead to just
2561 single-step out. Note: assuming such trampolines
2562 don't exhibit recursion on any platform... */
2563 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
2564 &ecs->stop_func_start,
2565 &ecs->stop_func_end);
2566 /* Readjust stepping range */
2567 step_range_start = ecs->stop_func_start;
2568 step_range_end = ecs->stop_func_end;
2569 ecs->stepping_through_sigtramp = 1;
2570 }
2571 }
2572
2573
2574 /* If this is stepi or nexti, make sure that the stepping range
2575 gets us past that instruction. */
2576 if (step_range_end == 1)
2577 /* FIXME: Does this run afoul of the code below which, if
2578 we step into the middle of a line, resets the stepping
2579 range? */
2580 step_range_end = (step_range_start = prev_pc) + 1;
2581
2582 ecs->remove_breakpoints_on_following_step = 1;
2583 keep_going (ecs);
2584 return;
2585 }
2586
2587 if (stop_pc == ecs->stop_func_start /* Quick test */
2588 || (in_prologue (stop_pc, ecs->stop_func_start) &&
2589 !IN_SOLIB_RETURN_TRAMPOLINE (stop_pc, ecs->stop_func_name))
2590 || IN_SOLIB_CALL_TRAMPOLINE (stop_pc, ecs->stop_func_name)
2591 || ecs->stop_func_name == 0)
2592 {
2593 /* It's a subroutine call. */
2594
2595 if ((step_over_calls == STEP_OVER_NONE)
2596 || ((step_range_end == 1)
2597 && in_prologue (prev_pc, ecs->stop_func_start)))
2598 {
2599 /* I presume that step_over_calls is only 0 when we're
2600 supposed to be stepping at the assembly language level
2601 ("stepi"). Just stop. */
2602 /* Also, maybe we just did a "nexti" inside a prolog,
2603 so we thought it was a subroutine call but it was not.
2604 Stop as well. FENN */
2605 stop_step = 1;
2606 print_stop_reason (END_STEPPING_RANGE, 0);
2607 stop_stepping (ecs);
2608 return;
2609 }
2610
2611 if (step_over_calls == STEP_OVER_ALL || IGNORE_HELPER_CALL (stop_pc))
2612 {
2613 /* We're doing a "next". */
2614
2615 if (PC_IN_SIGTRAMP (stop_pc, ecs->stop_func_name)
2616 && frame_id_inner (step_frame_id,
2617 frame_id_build (read_sp (), 0)))
2618 /* We stepped out of a signal handler, and into its
2619 calling trampoline. This is misdetected as a
2620 subroutine call, but stepping over the signal
2621 trampoline isn't such a bad idea. In order to do that,
2622 we have to ignore the value in step_frame_id, since
2623 that doesn't represent the frame that'll reach when we
2624 return from the signal trampoline. Otherwise we'll
2625 probably continue to the end of the program. */
2626 step_frame_id = null_frame_id;
2627
2628 step_over_function (ecs);
2629 keep_going (ecs);
2630 return;
2631 }
2632
2633 /* If we are in a function call trampoline (a stub between
2634 the calling routine and the real function), locate the real
2635 function. That's what tells us (a) whether we want to step
2636 into it at all, and (b) what prologue we want to run to
2637 the end of, if we do step into it. */
2638 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
2639 if (tmp != 0)
2640 ecs->stop_func_start = tmp;
2641 else
2642 {
2643 tmp = DYNAMIC_TRAMPOLINE_NEXTPC (stop_pc);
2644 if (tmp)
2645 {
2646 struct symtab_and_line xxx;
2647 /* Why isn't this s_a_l called "sr_sal", like all of the
2648 other s_a_l's where this code is duplicated? */
2649 init_sal (&xxx); /* initialize to zeroes */
2650 xxx.pc = tmp;
2651 xxx.section = find_pc_overlay (xxx.pc);
2652 check_for_old_step_resume_breakpoint ();
2653 step_resume_breakpoint =
2654 set_momentary_breakpoint (xxx, NULL, bp_step_resume);
2655 insert_breakpoints ();
2656 keep_going (ecs);
2657 return;
2658 }
2659 }
2660
2661 /* If we have line number information for the function we
2662 are thinking of stepping into, step into it.
2663
2664 If there are several symtabs at that PC (e.g. with include
2665 files), just want to know whether *any* of them have line
2666 numbers. find_pc_line handles this. */
2667 {
2668 struct symtab_and_line tmp_sal;
2669
2670 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
2671 if (tmp_sal.line != 0)
2672 {
2673 step_into_function (ecs);
2674 return;
2675 }
2676 }
2677
2678 /* If we have no line number and the step-stop-if-no-debug
2679 is set, we stop the step so that the user has a chance to
2680 switch in assembly mode. */
2681 if (step_over_calls == STEP_OVER_UNDEBUGGABLE && step_stop_if_no_debug)
2682 {
2683 stop_step = 1;
2684 print_stop_reason (END_STEPPING_RANGE, 0);
2685 stop_stepping (ecs);
2686 return;
2687 }
2688
2689 step_over_function (ecs);
2690 keep_going (ecs);
2691 return;
2692
2693 }
2694
2695 /* We've wandered out of the step range. */
2696
2697 ecs->sal = find_pc_line (stop_pc, 0);
2698
2699 if (step_range_end == 1)
2700 {
2701 /* It is stepi or nexti. We always want to stop stepping after
2702 one instruction. */
2703 stop_step = 1;
2704 print_stop_reason (END_STEPPING_RANGE, 0);
2705 stop_stepping (ecs);
2706 return;
2707 }
2708
2709 /* If we're in the return path from a shared library trampoline,
2710 we want to proceed through the trampoline when stepping. */
2711 if (IN_SOLIB_RETURN_TRAMPOLINE (stop_pc, ecs->stop_func_name))
2712 {
2713 CORE_ADDR tmp;
2714
2715 /* Determine where this trampoline returns. */
2716 tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
2717
2718 /* Only proceed through if we know where it's going. */
2719 if (tmp)
2720 {
2721 /* And put the step-breakpoint there and go until there. */
2722 struct symtab_and_line sr_sal;
2723
2724 init_sal (&sr_sal); /* initialize to zeroes */
2725 sr_sal.pc = tmp;
2726 sr_sal.section = find_pc_overlay (sr_sal.pc);
2727 /* Do not specify what the fp should be when we stop
2728 since on some machines the prologue
2729 is where the new fp value is established. */
2730 check_for_old_step_resume_breakpoint ();
2731 step_resume_breakpoint =
2732 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2733 if (breakpoints_inserted)
2734 insert_breakpoints ();
2735
2736 /* Restart without fiddling with the step ranges or
2737 other state. */
2738 keep_going (ecs);
2739 return;
2740 }
2741 }
2742
2743 if (ecs->sal.line == 0)
2744 {
2745 /* We have no line number information. That means to stop
2746 stepping (does this always happen right after one instruction,
2747 when we do "s" in a function with no line numbers,
2748 or can this happen as a result of a return or longjmp?). */
2749 stop_step = 1;
2750 print_stop_reason (END_STEPPING_RANGE, 0);
2751 stop_stepping (ecs);
2752 return;
2753 }
2754
2755 if ((stop_pc == ecs->sal.pc)
2756 && (ecs->current_line != ecs->sal.line
2757 || ecs->current_symtab != ecs->sal.symtab))
2758 {
2759 /* We are at the start of a different line. So stop. Note that
2760 we don't stop if we step into the middle of a different line.
2761 That is said to make things like for (;;) statements work
2762 better. */
2763 stop_step = 1;
2764 print_stop_reason (END_STEPPING_RANGE, 0);
2765 stop_stepping (ecs);
2766 return;
2767 }
2768
2769 /* We aren't done stepping.
2770
2771 Optimize by setting the stepping range to the line.
2772 (We might not be in the original line, but if we entered a
2773 new line in mid-statement, we continue stepping. This makes
2774 things like for(;;) statements work better.) */
2775
2776 if (ecs->stop_func_end && ecs->sal.end >= ecs->stop_func_end)
2777 {
2778 /* If this is the last line of the function, don't keep stepping
2779 (it would probably step us out of the function).
2780 This is particularly necessary for a one-line function,
2781 in which after skipping the prologue we better stop even though
2782 we will be in mid-line. */
2783 stop_step = 1;
2784 print_stop_reason (END_STEPPING_RANGE, 0);
2785 stop_stepping (ecs);
2786 return;
2787 }
2788 step_range_start = ecs->sal.pc;
2789 step_range_end = ecs->sal.end;
2790 step_frame_id = get_frame_id (get_current_frame ());
2791 ecs->current_line = ecs->sal.line;
2792 ecs->current_symtab = ecs->sal.symtab;
2793
2794 /* In the case where we just stepped out of a function into the
2795 middle of a line of the caller, continue stepping, but
2796 step_frame_id must be modified to current frame */
2797 {
2798 struct frame_id current_frame = get_frame_id (get_current_frame ());
2799 if (!(frame_id_inner (current_frame, step_frame_id)))
2800 step_frame_id = current_frame;
2801 }
2802
2803 keep_going (ecs);
2804 }
2805
2806 /* Are we in the middle of stepping? */
2807
2808 static int
2809 currently_stepping (struct execution_control_state *ecs)
2810 {
2811 return ((through_sigtramp_breakpoint == NULL
2812 && !ecs->handling_longjmp
2813 && ((step_range_end && step_resume_breakpoint == NULL)
2814 || trap_expected))
2815 || ecs->stepping_through_solib_after_catch
2816 || bpstat_should_step ());
2817 }
2818
2819 static void
2820 check_sigtramp2 (struct execution_control_state *ecs)
2821 {
2822 if (trap_expected
2823 && PC_IN_SIGTRAMP (stop_pc, ecs->stop_func_name)
2824 && !PC_IN_SIGTRAMP (prev_pc, prev_func_name)
2825 && INNER_THAN (read_sp (), step_sp))
2826 {
2827 /* What has happened here is that we have just stepped the
2828 inferior with a signal (because it is a signal which
2829 shouldn't make us stop), thus stepping into sigtramp.
2830
2831 So we need to set a step_resume_break_address breakpoint and
2832 continue until we hit it, and then step. FIXME: This should
2833 be more enduring than a step_resume breakpoint; we should
2834 know that we will later need to keep going rather than
2835 re-hitting the breakpoint here (see the testsuite,
2836 gdb.base/signals.exp where it says "exceedingly difficult"). */
2837
2838 struct symtab_and_line sr_sal;
2839
2840 init_sal (&sr_sal); /* initialize to zeroes */
2841 sr_sal.pc = prev_pc;
2842 sr_sal.section = find_pc_overlay (sr_sal.pc);
2843 /* We perhaps could set the frame if we kept track of what the
2844 frame corresponding to prev_pc was. But we don't, so don't. */
2845 through_sigtramp_breakpoint =
2846 set_momentary_breakpoint (sr_sal, NULL, bp_through_sigtramp);
2847 if (breakpoints_inserted)
2848 insert_breakpoints ();
2849
2850 ecs->remove_breakpoints_on_following_step = 1;
2851 ecs->another_trap = 1;
2852 }
2853 }
2854
2855 /* Subroutine call with source code we should not step over. Do step
2856 to the first line of code in it. */
2857
2858 static void
2859 step_into_function (struct execution_control_state *ecs)
2860 {
2861 struct symtab *s;
2862 struct symtab_and_line sr_sal;
2863
2864 s = find_pc_symtab (stop_pc);
2865 if (s && s->language != language_asm)
2866 ecs->stop_func_start = SKIP_PROLOGUE (ecs->stop_func_start);
2867
2868 ecs->sal = find_pc_line (ecs->stop_func_start, 0);
2869 /* Use the step_resume_break to step until the end of the prologue,
2870 even if that involves jumps (as it seems to on the vax under
2871 4.2). */
2872 /* If the prologue ends in the middle of a source line, continue to
2873 the end of that source line (if it is still within the function).
2874 Otherwise, just go to end of prologue. */
2875 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
2876 /* no, don't either. It skips any code that's legitimately on the
2877 first line. */
2878 #else
2879 if (ecs->sal.end
2880 && ecs->sal.pc != ecs->stop_func_start
2881 && ecs->sal.end < ecs->stop_func_end)
2882 ecs->stop_func_start = ecs->sal.end;
2883 #endif
2884
2885 if (ecs->stop_func_start == stop_pc)
2886 {
2887 /* We are already there: stop now. */
2888 stop_step = 1;
2889 print_stop_reason (END_STEPPING_RANGE, 0);
2890 stop_stepping (ecs);
2891 return;
2892 }
2893 else
2894 {
2895 /* Put the step-breakpoint there and go until there. */
2896 init_sal (&sr_sal); /* initialize to zeroes */
2897 sr_sal.pc = ecs->stop_func_start;
2898 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
2899 /* Do not specify what the fp should be when we stop since on
2900 some machines the prologue is where the new fp value is
2901 established. */
2902 check_for_old_step_resume_breakpoint ();
2903 step_resume_breakpoint =
2904 set_momentary_breakpoint (sr_sal, NULL, bp_step_resume);
2905 if (breakpoints_inserted)
2906 insert_breakpoints ();
2907
2908 /* And make sure stepping stops right away then. */
2909 step_range_end = step_range_start;
2910 }
2911 keep_going (ecs);
2912 }
2913
2914 /* We've just entered a callee, and we wish to resume until it returns
2915 to the caller. Setting a step_resume breakpoint on the return
2916 address will catch a return from the callee.
2917
2918 However, if the callee is recursing, we want to be careful not to
2919 catch returns of those recursive calls, but only of THIS instance
2920 of the call.
2921
2922 To do this, we set the step_resume bp's frame to our current
2923 caller's frame (step_frame_id, which is set by the "next" or
2924 "until" command, before execution begins). */
2925
2926 static void
2927 step_over_function (struct execution_control_state *ecs)
2928 {
2929 struct symtab_and_line sr_sal;
2930
2931 init_sal (&sr_sal); /* initialize to zeros */
2932 sr_sal.pc = ADDR_BITS_REMOVE (SAVED_PC_AFTER_CALL (get_current_frame ()));
2933 sr_sal.section = find_pc_overlay (sr_sal.pc);
2934
2935 check_for_old_step_resume_breakpoint ();
2936 step_resume_breakpoint =
2937 set_momentary_breakpoint (sr_sal, get_current_frame (), bp_step_resume);
2938
2939 if (frame_id_p (step_frame_id)
2940 && !IN_SOLIB_DYNSYM_RESOLVE_CODE (sr_sal.pc))
2941 /* FIXME: cagney/2002-12-01: Someone should modify the breakpoint
2942 code so that it uses a frame ID, instead of a frame address. */
2943 step_resume_breakpoint->frame = step_frame_id.base;
2944
2945 if (breakpoints_inserted)
2946 insert_breakpoints ();
2947 }
2948
2949 static void
2950 stop_stepping (struct execution_control_state *ecs)
2951 {
2952 if (target_has_execution)
2953 {
2954 /* Are we stopping for a vfork event? We only stop when we see
2955 the child's event. However, we may not yet have seen the
2956 parent's event. And, inferior_ptid is still set to the
2957 parent's pid, until we resume again and follow either the
2958 parent or child.
2959
2960 To ensure that we can really touch inferior_ptid (aka, the
2961 parent process) -- which calls to functions like read_pc
2962 implicitly do -- wait on the parent if necessary. */
2963 if ((pending_follow.kind == TARGET_WAITKIND_VFORKED)
2964 && !pending_follow.fork_event.saw_parent_fork)
2965 {
2966 ptid_t parent_ptid;
2967
2968 do
2969 {
2970 if (target_wait_hook)
2971 parent_ptid = target_wait_hook (pid_to_ptid (-1), &(ecs->ws));
2972 else
2973 parent_ptid = target_wait (pid_to_ptid (-1), &(ecs->ws));
2974 }
2975 while (!ptid_equal (parent_ptid, inferior_ptid));
2976 }
2977
2978 /* Assuming the inferior still exists, set these up for next
2979 time, just like we did above if we didn't break out of the
2980 loop. */
2981 prev_pc = read_pc ();
2982 prev_func_start = ecs->stop_func_start;
2983 prev_func_name = ecs->stop_func_name;
2984 }
2985
2986 /* Let callers know we don't want to wait for the inferior anymore. */
2987 ecs->wait_some_more = 0;
2988 }
2989
2990 /* This function handles various cases where we need to continue
2991 waiting for the inferior. */
2992 /* (Used to be the keep_going: label in the old wait_for_inferior) */
2993
2994 static void
2995 keep_going (struct execution_control_state *ecs)
2996 {
2997 /* Save the pc before execution, to compare with pc after stop. */
2998 prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
2999 prev_func_start = ecs->stop_func_start; /* Ok, since if DECR_PC_AFTER
3000 BREAK is defined, the
3001 original pc would not have
3002 been at the start of a
3003 function. */
3004 prev_func_name = ecs->stop_func_name;
3005
3006 if (ecs->update_step_sp)
3007 step_sp = read_sp ();
3008 ecs->update_step_sp = 0;
3009
3010 /* If we did not do break;, it means we should keep running the
3011 inferior and not return to debugger. */
3012
3013 if (trap_expected && stop_signal != TARGET_SIGNAL_TRAP)
3014 {
3015 /* We took a signal (which we are supposed to pass through to
3016 the inferior, else we'd have done a break above) and we
3017 haven't yet gotten our trap. Simply continue. */
3018 resume (currently_stepping (ecs), stop_signal);
3019 }
3020 else
3021 {
3022 /* Either the trap was not expected, but we are continuing
3023 anyway (the user asked that this signal be passed to the
3024 child)
3025 -- or --
3026 The signal was SIGTRAP, e.g. it was our signal, but we
3027 decided we should resume from it.
3028
3029 We're going to run this baby now!
3030
3031 Insert breakpoints now, unless we are trying to one-proceed
3032 past a breakpoint. */
3033 /* If we've just finished a special step resume and we don't
3034 want to hit a breakpoint, pull em out. */
3035 if (step_resume_breakpoint == NULL
3036 && through_sigtramp_breakpoint == NULL
3037 && ecs->remove_breakpoints_on_following_step)
3038 {
3039 ecs->remove_breakpoints_on_following_step = 0;
3040 remove_breakpoints ();
3041 breakpoints_inserted = 0;
3042 }
3043 else if (!breakpoints_inserted &&
3044 (through_sigtramp_breakpoint != NULL || !ecs->another_trap))
3045 {
3046 breakpoints_failed = insert_breakpoints ();
3047 if (breakpoints_failed)
3048 {
3049 stop_stepping (ecs);
3050 return;
3051 }
3052 breakpoints_inserted = 1;
3053 }
3054
3055 trap_expected = ecs->another_trap;
3056
3057 /* Do not deliver SIGNAL_TRAP (except when the user explicitly
3058 specifies that such a signal should be delivered to the
3059 target program).
3060
3061 Typically, this would occure when a user is debugging a
3062 target monitor on a simulator: the target monitor sets a
3063 breakpoint; the simulator encounters this break-point and
3064 halts the simulation handing control to GDB; GDB, noteing
3065 that the break-point isn't valid, returns control back to the
3066 simulator; the simulator then delivers the hardware
3067 equivalent of a SIGNAL_TRAP to the program being debugged. */
3068
3069 if (stop_signal == TARGET_SIGNAL_TRAP && !signal_program[stop_signal])
3070 stop_signal = TARGET_SIGNAL_0;
3071
3072 #ifdef SHIFT_INST_REGS
3073 /* I'm not sure when this following segment applies. I do know,
3074 now, that we shouldn't rewrite the regs when we were stopped
3075 by a random signal from the inferior process. */
3076 /* FIXME: Shouldn't this be based on the valid bit of the SXIP?
3077 (this is only used on the 88k). */
3078
3079 if (!bpstat_explains_signal (stop_bpstat)
3080 && (stop_signal != TARGET_SIGNAL_CHLD) && !stopped_by_random_signal)
3081 SHIFT_INST_REGS ();
3082 #endif /* SHIFT_INST_REGS */
3083
3084 resume (currently_stepping (ecs), stop_signal);
3085 }
3086
3087 prepare_to_wait (ecs);
3088 }
3089
3090 /* This function normally comes after a resume, before
3091 handle_inferior_event exits. It takes care of any last bits of
3092 housekeeping, and sets the all-important wait_some_more flag. */
3093
3094 static void
3095 prepare_to_wait (struct execution_control_state *ecs)
3096 {
3097 if (ecs->infwait_state == infwait_normal_state)
3098 {
3099 overlay_cache_invalid = 1;
3100
3101 /* We have to invalidate the registers BEFORE calling
3102 target_wait because they can be loaded from the target while
3103 in target_wait. This makes remote debugging a bit more
3104 efficient for those targets that provide critical registers
3105 as part of their normal status mechanism. */
3106
3107 registers_changed ();
3108 ecs->waiton_ptid = pid_to_ptid (-1);
3109 ecs->wp = &(ecs->ws);
3110 }
3111 /* This is the old end of the while loop. Let everybody know we
3112 want to wait for the inferior some more and get called again
3113 soon. */
3114 ecs->wait_some_more = 1;
3115 }
3116
3117 /* Print why the inferior has stopped. We always print something when
3118 the inferior exits, or receives a signal. The rest of the cases are
3119 dealt with later on in normal_stop() and print_it_typical(). Ideally
3120 there should be a call to this function from handle_inferior_event()
3121 each time stop_stepping() is called.*/
3122 static void
3123 print_stop_reason (enum inferior_stop_reason stop_reason, int stop_info)
3124 {
3125 switch (stop_reason)
3126 {
3127 case STOP_UNKNOWN:
3128 /* We don't deal with these cases from handle_inferior_event()
3129 yet. */
3130 break;
3131 case END_STEPPING_RANGE:
3132 /* We are done with a step/next/si/ni command. */
3133 /* For now print nothing. */
3134 /* Print a message only if not in the middle of doing a "step n"
3135 operation for n > 1 */
3136 if (!step_multi || !stop_step)
3137 if (ui_out_is_mi_like_p (uiout))
3138 ui_out_field_string (uiout, "reason", "end-stepping-range");
3139 break;
3140 case BREAKPOINT_HIT:
3141 /* We found a breakpoint. */
3142 /* For now print nothing. */
3143 break;
3144 case SIGNAL_EXITED:
3145 /* The inferior was terminated by a signal. */
3146 annotate_signalled ();
3147 if (ui_out_is_mi_like_p (uiout))
3148 ui_out_field_string (uiout, "reason", "exited-signalled");
3149 ui_out_text (uiout, "\nProgram terminated with signal ");
3150 annotate_signal_name ();
3151 ui_out_field_string (uiout, "signal-name",
3152 target_signal_to_name (stop_info));
3153 annotate_signal_name_end ();
3154 ui_out_text (uiout, ", ");
3155 annotate_signal_string ();
3156 ui_out_field_string (uiout, "signal-meaning",
3157 target_signal_to_string (stop_info));
3158 annotate_signal_string_end ();
3159 ui_out_text (uiout, ".\n");
3160 ui_out_text (uiout, "The program no longer exists.\n");
3161 break;
3162 case EXITED:
3163 /* The inferior program is finished. */
3164 annotate_exited (stop_info);
3165 if (stop_info)
3166 {
3167 if (ui_out_is_mi_like_p (uiout))
3168 ui_out_field_string (uiout, "reason", "exited");
3169 ui_out_text (uiout, "\nProgram exited with code ");
3170 ui_out_field_fmt (uiout, "exit-code", "0%o",
3171 (unsigned int) stop_info);
3172 ui_out_text (uiout, ".\n");
3173 }
3174 else
3175 {
3176 if (ui_out_is_mi_like_p (uiout))
3177 ui_out_field_string (uiout, "reason", "exited-normally");
3178 ui_out_text (uiout, "\nProgram exited normally.\n");
3179 }
3180 break;
3181 case SIGNAL_RECEIVED:
3182 /* Signal received. The signal table tells us to print about
3183 it. */
3184 annotate_signal ();
3185 ui_out_text (uiout, "\nProgram received signal ");
3186 annotate_signal_name ();
3187 if (ui_out_is_mi_like_p (uiout))
3188 ui_out_field_string (uiout, "reason", "signal-received");
3189 ui_out_field_string (uiout, "signal-name",
3190 target_signal_to_name (stop_info));
3191 annotate_signal_name_end ();
3192 ui_out_text (uiout, ", ");
3193 annotate_signal_string ();
3194 ui_out_field_string (uiout, "signal-meaning",
3195 target_signal_to_string (stop_info));
3196 annotate_signal_string_end ();
3197 ui_out_text (uiout, ".\n");
3198 break;
3199 default:
3200 internal_error (__FILE__, __LINE__,
3201 "print_stop_reason: unrecognized enum value");
3202 break;
3203 }
3204 }
3205 \f
3206
3207 /* Here to return control to GDB when the inferior stops for real.
3208 Print appropriate messages, remove breakpoints, give terminal our modes.
3209
3210 STOP_PRINT_FRAME nonzero means print the executing frame
3211 (pc, function, args, file, line number and line text).
3212 BREAKPOINTS_FAILED nonzero means stop was due to error
3213 attempting to insert breakpoints. */
3214
3215 void
3216 normal_stop (void)
3217 {
3218 /* As with the notification of thread events, we want to delay
3219 notifying the user that we've switched thread context until
3220 the inferior actually stops.
3221
3222 (Note that there's no point in saying anything if the inferior
3223 has exited!) */
3224 if (!ptid_equal (previous_inferior_ptid, inferior_ptid)
3225 && target_has_execution)
3226 {
3227 target_terminal_ours_for_output ();
3228 printf_filtered ("[Switching to %s]\n",
3229 target_pid_or_tid_to_str (inferior_ptid));
3230 previous_inferior_ptid = inferior_ptid;
3231 }
3232
3233 /* Make sure that the current_frame's pc is correct. This
3234 is a correction for setting up the frame info before doing
3235 DECR_PC_AFTER_BREAK */
3236 if (target_has_execution && get_current_frame ())
3237 (get_current_frame ())->pc = read_pc ();
3238
3239 if (target_has_execution && breakpoints_inserted)
3240 {
3241 if (remove_breakpoints ())
3242 {
3243 target_terminal_ours_for_output ();
3244 printf_filtered ("Cannot remove breakpoints because ");
3245 printf_filtered ("program is no longer writable.\n");
3246 printf_filtered ("It might be running in another process.\n");
3247 printf_filtered ("Further execution is probably impossible.\n");
3248 }
3249 }
3250 breakpoints_inserted = 0;
3251
3252 /* Delete the breakpoint we stopped at, if it wants to be deleted.
3253 Delete any breakpoint that is to be deleted at the next stop. */
3254
3255 breakpoint_auto_delete (stop_bpstat);
3256
3257 /* If an auto-display called a function and that got a signal,
3258 delete that auto-display to avoid an infinite recursion. */
3259
3260 if (stopped_by_random_signal)
3261 disable_current_display ();
3262
3263 /* Don't print a message if in the middle of doing a "step n"
3264 operation for n > 1 */
3265 if (step_multi && stop_step)
3266 goto done;
3267
3268 target_terminal_ours ();
3269
3270 /* Look up the hook_stop and run it (CLI internally handles problem
3271 of stop_command's pre-hook not existing). */
3272 if (stop_command)
3273 catch_errors (hook_stop_stub, stop_command,
3274 "Error while running hook_stop:\n", RETURN_MASK_ALL);
3275
3276 if (!target_has_stack)
3277 {
3278
3279 goto done;
3280 }
3281
3282 /* Select innermost stack frame - i.e., current frame is frame 0,
3283 and current location is based on that.
3284 Don't do this on return from a stack dummy routine,
3285 or if the program has exited. */
3286
3287 if (!stop_stack_dummy)
3288 {
3289 select_frame (get_current_frame ());
3290
3291 /* Print current location without a level number, if
3292 we have changed functions or hit a breakpoint.
3293 Print source line if we have one.
3294 bpstat_print() contains the logic deciding in detail
3295 what to print, based on the event(s) that just occurred. */
3296
3297 if (stop_print_frame && deprecated_selected_frame)
3298 {
3299 int bpstat_ret;
3300 int source_flag;
3301 int do_frame_printing = 1;
3302
3303 bpstat_ret = bpstat_print (stop_bpstat);
3304 switch (bpstat_ret)
3305 {
3306 case PRINT_UNKNOWN:
3307 /* FIXME: cagney/2002-12-01: Given that a frame ID does
3308 (or should) carry around the function and does (or
3309 should) use that when doing a frame comparison. */
3310 if (stop_step
3311 && frame_id_eq (step_frame_id,
3312 get_frame_id (get_current_frame ()))
3313 && step_start_function == find_pc_function (stop_pc))
3314 source_flag = SRC_LINE; /* finished step, just print source line */
3315 else
3316 source_flag = SRC_AND_LOC; /* print location and source line */
3317 break;
3318 case PRINT_SRC_AND_LOC:
3319 source_flag = SRC_AND_LOC; /* print location and source line */
3320 break;
3321 case PRINT_SRC_ONLY:
3322 source_flag = SRC_LINE;
3323 break;
3324 case PRINT_NOTHING:
3325 source_flag = SRC_LINE; /* something bogus */
3326 do_frame_printing = 0;
3327 break;
3328 default:
3329 internal_error (__FILE__, __LINE__, "Unknown value.");
3330 }
3331 /* For mi, have the same behavior every time we stop:
3332 print everything but the source line. */
3333 if (ui_out_is_mi_like_p (uiout))
3334 source_flag = LOC_AND_ADDRESS;
3335
3336 if (ui_out_is_mi_like_p (uiout))
3337 ui_out_field_int (uiout, "thread-id",
3338 pid_to_thread_id (inferior_ptid));
3339 /* The behavior of this routine with respect to the source
3340 flag is:
3341 SRC_LINE: Print only source line
3342 LOCATION: Print only location
3343 SRC_AND_LOC: Print location and source line */
3344 if (do_frame_printing)
3345 show_and_print_stack_frame (deprecated_selected_frame, -1, source_flag);
3346
3347 /* Display the auto-display expressions. */
3348 do_displays ();
3349 }
3350 }
3351
3352 /* Save the function value return registers, if we care.
3353 We might be about to restore their previous contents. */
3354 if (proceed_to_finish)
3355 /* NB: The copy goes through to the target picking up the value of
3356 all the registers. */
3357 regcache_cpy (stop_registers, current_regcache);
3358
3359 if (stop_stack_dummy)
3360 {
3361 /* Pop the empty frame that contains the stack dummy.
3362 POP_FRAME ends with a setting of the current frame, so we
3363 can use that next. */
3364 POP_FRAME;
3365 /* Set stop_pc to what it was before we called the function.
3366 Can't rely on restore_inferior_status because that only gets
3367 called if we don't stop in the called function. */
3368 stop_pc = read_pc ();
3369 select_frame (get_current_frame ());
3370 }
3371
3372 done:
3373 annotate_stopped ();
3374 }
3375
3376 static int
3377 hook_stop_stub (void *cmd)
3378 {
3379 execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
3380 return (0);
3381 }
3382 \f
3383 int
3384 signal_stop_state (int signo)
3385 {
3386 return signal_stop[signo];
3387 }
3388
3389 int
3390 signal_print_state (int signo)
3391 {
3392 return signal_print[signo];
3393 }
3394
3395 int
3396 signal_pass_state (int signo)
3397 {
3398 return signal_program[signo];
3399 }
3400
3401 int
3402 signal_stop_update (int signo, int state)
3403 {
3404 int ret = signal_stop[signo];
3405 signal_stop[signo] = state;
3406 return ret;
3407 }
3408
3409 int
3410 signal_print_update (int signo, int state)
3411 {
3412 int ret = signal_print[signo];
3413 signal_print[signo] = state;
3414 return ret;
3415 }
3416
3417 int
3418 signal_pass_update (int signo, int state)
3419 {
3420 int ret = signal_program[signo];
3421 signal_program[signo] = state;
3422 return ret;
3423 }
3424
3425 static void
3426 sig_print_header (void)
3427 {
3428 printf_filtered ("\
3429 Signal Stop\tPrint\tPass to program\tDescription\n");
3430 }
3431
3432 static void
3433 sig_print_info (enum target_signal oursig)
3434 {
3435 char *name = target_signal_to_name (oursig);
3436 int name_padding = 13 - strlen (name);
3437
3438 if (name_padding <= 0)
3439 name_padding = 0;
3440
3441 printf_filtered ("%s", name);
3442 printf_filtered ("%*.*s ", name_padding, name_padding, " ");
3443 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
3444 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
3445 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
3446 printf_filtered ("%s\n", target_signal_to_string (oursig));
3447 }
3448
3449 /* Specify how various signals in the inferior should be handled. */
3450
3451 static void
3452 handle_command (char *args, int from_tty)
3453 {
3454 char **argv;
3455 int digits, wordlen;
3456 int sigfirst, signum, siglast;
3457 enum target_signal oursig;
3458 int allsigs;
3459 int nsigs;
3460 unsigned char *sigs;
3461 struct cleanup *old_chain;
3462
3463 if (args == NULL)
3464 {
3465 error_no_arg ("signal to handle");
3466 }
3467
3468 /* Allocate and zero an array of flags for which signals to handle. */
3469
3470 nsigs = (int) TARGET_SIGNAL_LAST;
3471 sigs = (unsigned char *) alloca (nsigs);
3472 memset (sigs, 0, nsigs);
3473
3474 /* Break the command line up into args. */
3475
3476 argv = buildargv (args);
3477 if (argv == NULL)
3478 {
3479 nomem (0);
3480 }
3481 old_chain = make_cleanup_freeargv (argv);
3482
3483 /* Walk through the args, looking for signal oursigs, signal names, and
3484 actions. Signal numbers and signal names may be interspersed with
3485 actions, with the actions being performed for all signals cumulatively
3486 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
3487
3488 while (*argv != NULL)
3489 {
3490 wordlen = strlen (*argv);
3491 for (digits = 0; isdigit ((*argv)[digits]); digits++)
3492 {;
3493 }
3494 allsigs = 0;
3495 sigfirst = siglast = -1;
3496
3497 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
3498 {
3499 /* Apply action to all signals except those used by the
3500 debugger. Silently skip those. */
3501 allsigs = 1;
3502 sigfirst = 0;
3503 siglast = nsigs - 1;
3504 }
3505 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
3506 {
3507 SET_SIGS (nsigs, sigs, signal_stop);
3508 SET_SIGS (nsigs, sigs, signal_print);
3509 }
3510 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
3511 {
3512 UNSET_SIGS (nsigs, sigs, signal_program);
3513 }
3514 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
3515 {
3516 SET_SIGS (nsigs, sigs, signal_print);
3517 }
3518 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
3519 {
3520 SET_SIGS (nsigs, sigs, signal_program);
3521 }
3522 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
3523 {
3524 UNSET_SIGS (nsigs, sigs, signal_stop);
3525 }
3526 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
3527 {
3528 SET_SIGS (nsigs, sigs, signal_program);
3529 }
3530 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
3531 {
3532 UNSET_SIGS (nsigs, sigs, signal_print);
3533 UNSET_SIGS (nsigs, sigs, signal_stop);
3534 }
3535 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
3536 {
3537 UNSET_SIGS (nsigs, sigs, signal_program);
3538 }
3539 else if (digits > 0)
3540 {
3541 /* It is numeric. The numeric signal refers to our own
3542 internal signal numbering from target.h, not to host/target
3543 signal number. This is a feature; users really should be
3544 using symbolic names anyway, and the common ones like
3545 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
3546
3547 sigfirst = siglast = (int)
3548 target_signal_from_command (atoi (*argv));
3549 if ((*argv)[digits] == '-')
3550 {
3551 siglast = (int)
3552 target_signal_from_command (atoi ((*argv) + digits + 1));
3553 }
3554 if (sigfirst > siglast)
3555 {
3556 /* Bet he didn't figure we'd think of this case... */
3557 signum = sigfirst;
3558 sigfirst = siglast;
3559 siglast = signum;
3560 }
3561 }
3562 else
3563 {
3564 oursig = target_signal_from_name (*argv);
3565 if (oursig != TARGET_SIGNAL_UNKNOWN)
3566 {
3567 sigfirst = siglast = (int) oursig;
3568 }
3569 else
3570 {
3571 /* Not a number and not a recognized flag word => complain. */
3572 error ("Unrecognized or ambiguous flag word: \"%s\".", *argv);
3573 }
3574 }
3575
3576 /* If any signal numbers or symbol names were found, set flags for
3577 which signals to apply actions to. */
3578
3579 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
3580 {
3581 switch ((enum target_signal) signum)
3582 {
3583 case TARGET_SIGNAL_TRAP:
3584 case TARGET_SIGNAL_INT:
3585 if (!allsigs && !sigs[signum])
3586 {
3587 if (query ("%s is used by the debugger.\n\
3588 Are you sure you want to change it? ", target_signal_to_name ((enum target_signal) signum)))
3589 {
3590 sigs[signum] = 1;
3591 }
3592 else
3593 {
3594 printf_unfiltered ("Not confirmed, unchanged.\n");
3595 gdb_flush (gdb_stdout);
3596 }
3597 }
3598 break;
3599 case TARGET_SIGNAL_0:
3600 case TARGET_SIGNAL_DEFAULT:
3601 case TARGET_SIGNAL_UNKNOWN:
3602 /* Make sure that "all" doesn't print these. */
3603 break;
3604 default:
3605 sigs[signum] = 1;
3606 break;
3607 }
3608 }
3609
3610 argv++;
3611 }
3612
3613 target_notice_signals (inferior_ptid);
3614
3615 if (from_tty)
3616 {
3617 /* Show the results. */
3618 sig_print_header ();
3619 for (signum = 0; signum < nsigs; signum++)
3620 {
3621 if (sigs[signum])
3622 {
3623 sig_print_info (signum);
3624 }
3625 }
3626 }
3627
3628 do_cleanups (old_chain);
3629 }
3630
3631 static void
3632 xdb_handle_command (char *args, int from_tty)
3633 {
3634 char **argv;
3635 struct cleanup *old_chain;
3636
3637 /* Break the command line up into args. */
3638
3639 argv = buildargv (args);
3640 if (argv == NULL)
3641 {
3642 nomem (0);
3643 }
3644 old_chain = make_cleanup_freeargv (argv);
3645 if (argv[1] != (char *) NULL)
3646 {
3647 char *argBuf;
3648 int bufLen;
3649
3650 bufLen = strlen (argv[0]) + 20;
3651 argBuf = (char *) xmalloc (bufLen);
3652 if (argBuf)
3653 {
3654 int validFlag = 1;
3655 enum target_signal oursig;
3656
3657 oursig = target_signal_from_name (argv[0]);
3658 memset (argBuf, 0, bufLen);
3659 if (strcmp (argv[1], "Q") == 0)
3660 sprintf (argBuf, "%s %s", argv[0], "noprint");
3661 else
3662 {
3663 if (strcmp (argv[1], "s") == 0)
3664 {
3665 if (!signal_stop[oursig])
3666 sprintf (argBuf, "%s %s", argv[0], "stop");
3667 else
3668 sprintf (argBuf, "%s %s", argv[0], "nostop");
3669 }
3670 else if (strcmp (argv[1], "i") == 0)
3671 {
3672 if (!signal_program[oursig])
3673 sprintf (argBuf, "%s %s", argv[0], "pass");
3674 else
3675 sprintf (argBuf, "%s %s", argv[0], "nopass");
3676 }
3677 else if (strcmp (argv[1], "r") == 0)
3678 {
3679 if (!signal_print[oursig])
3680 sprintf (argBuf, "%s %s", argv[0], "print");
3681 else
3682 sprintf (argBuf, "%s %s", argv[0], "noprint");
3683 }
3684 else
3685 validFlag = 0;
3686 }
3687 if (validFlag)
3688 handle_command (argBuf, from_tty);
3689 else
3690 printf_filtered ("Invalid signal handling flag.\n");
3691 if (argBuf)
3692 xfree (argBuf);
3693 }
3694 }
3695 do_cleanups (old_chain);
3696 }
3697
3698 /* Print current contents of the tables set by the handle command.
3699 It is possible we should just be printing signals actually used
3700 by the current target (but for things to work right when switching
3701 targets, all signals should be in the signal tables). */
3702
3703 static void
3704 signals_info (char *signum_exp, int from_tty)
3705 {
3706 enum target_signal oursig;
3707 sig_print_header ();
3708
3709 if (signum_exp)
3710 {
3711 /* First see if this is a symbol name. */
3712 oursig = target_signal_from_name (signum_exp);
3713 if (oursig == TARGET_SIGNAL_UNKNOWN)
3714 {
3715 /* No, try numeric. */
3716 oursig =
3717 target_signal_from_command (parse_and_eval_long (signum_exp));
3718 }
3719 sig_print_info (oursig);
3720 return;
3721 }
3722
3723 printf_filtered ("\n");
3724 /* These ugly casts brought to you by the native VAX compiler. */
3725 for (oursig = TARGET_SIGNAL_FIRST;
3726 (int) oursig < (int) TARGET_SIGNAL_LAST;
3727 oursig = (enum target_signal) ((int) oursig + 1))
3728 {
3729 QUIT;
3730
3731 if (oursig != TARGET_SIGNAL_UNKNOWN
3732 && oursig != TARGET_SIGNAL_DEFAULT && oursig != TARGET_SIGNAL_0)
3733 sig_print_info (oursig);
3734 }
3735
3736 printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
3737 }
3738 \f
3739 struct inferior_status
3740 {
3741 enum target_signal stop_signal;
3742 CORE_ADDR stop_pc;
3743 bpstat stop_bpstat;
3744 int stop_step;
3745 int stop_stack_dummy;
3746 int stopped_by_random_signal;
3747 int trap_expected;
3748 CORE_ADDR step_range_start;
3749 CORE_ADDR step_range_end;
3750 struct frame_id step_frame_id;
3751 enum step_over_calls_kind step_over_calls;
3752 CORE_ADDR step_resume_break_address;
3753 int stop_after_trap;
3754 int stop_soon_quietly;
3755 struct regcache *stop_registers;
3756
3757 /* These are here because if call_function_by_hand has written some
3758 registers and then decides to call error(), we better not have changed
3759 any registers. */
3760 struct regcache *registers;
3761
3762 /* A frame unique identifier. */
3763 struct frame_id selected_frame_id;
3764
3765 int breakpoint_proceeded;
3766 int restore_stack_info;
3767 int proceed_to_finish;
3768 };
3769
3770 void
3771 write_inferior_status_register (struct inferior_status *inf_status, int regno,
3772 LONGEST val)
3773 {
3774 int size = REGISTER_RAW_SIZE (regno);
3775 void *buf = alloca (size);
3776 store_signed_integer (buf, size, val);
3777 regcache_raw_write (inf_status->registers, regno, buf);
3778 }
3779
3780 /* Save all of the information associated with the inferior<==>gdb
3781 connection. INF_STATUS is a pointer to a "struct inferior_status"
3782 (defined in inferior.h). */
3783
3784 struct inferior_status *
3785 save_inferior_status (int restore_stack_info)
3786 {
3787 struct inferior_status *inf_status = XMALLOC (struct inferior_status);
3788
3789 inf_status->stop_signal = stop_signal;
3790 inf_status->stop_pc = stop_pc;
3791 inf_status->stop_step = stop_step;
3792 inf_status->stop_stack_dummy = stop_stack_dummy;
3793 inf_status->stopped_by_random_signal = stopped_by_random_signal;
3794 inf_status->trap_expected = trap_expected;
3795 inf_status->step_range_start = step_range_start;
3796 inf_status->step_range_end = step_range_end;
3797 inf_status->step_frame_id = step_frame_id;
3798 inf_status->step_over_calls = step_over_calls;
3799 inf_status->stop_after_trap = stop_after_trap;
3800 inf_status->stop_soon_quietly = stop_soon_quietly;
3801 /* Save original bpstat chain here; replace it with copy of chain.
3802 If caller's caller is walking the chain, they'll be happier if we
3803 hand them back the original chain when restore_inferior_status is
3804 called. */
3805 inf_status->stop_bpstat = stop_bpstat;
3806 stop_bpstat = bpstat_copy (stop_bpstat);
3807 inf_status->breakpoint_proceeded = breakpoint_proceeded;
3808 inf_status->restore_stack_info = restore_stack_info;
3809 inf_status->proceed_to_finish = proceed_to_finish;
3810
3811 inf_status->stop_registers = regcache_dup_no_passthrough (stop_registers);
3812
3813 inf_status->registers = regcache_dup (current_regcache);
3814
3815 inf_status->selected_frame_id = get_frame_id (deprecated_selected_frame);
3816 return inf_status;
3817 }
3818
3819 static int
3820 restore_selected_frame (void *args)
3821 {
3822 struct frame_id *fid = (struct frame_id *) args;
3823 struct frame_info *frame;
3824
3825 frame = frame_find_by_id (*fid);
3826
3827 /* If inf_status->selected_frame_id is NULL, there was no previously
3828 selected frame. */
3829 if (frame == NULL)
3830 {
3831 warning ("Unable to restore previously selected frame.\n");
3832 return 0;
3833 }
3834
3835 select_frame (frame);
3836
3837 return (1);
3838 }
3839
3840 void
3841 restore_inferior_status (struct inferior_status *inf_status)
3842 {
3843 stop_signal = inf_status->stop_signal;
3844 stop_pc = inf_status->stop_pc;
3845 stop_step = inf_status->stop_step;
3846 stop_stack_dummy = inf_status->stop_stack_dummy;
3847 stopped_by_random_signal = inf_status->stopped_by_random_signal;
3848 trap_expected = inf_status->trap_expected;
3849 step_range_start = inf_status->step_range_start;
3850 step_range_end = inf_status->step_range_end;
3851 step_frame_id = inf_status->step_frame_id;
3852 step_over_calls = inf_status->step_over_calls;
3853 stop_after_trap = inf_status->stop_after_trap;
3854 stop_soon_quietly = inf_status->stop_soon_quietly;
3855 bpstat_clear (&stop_bpstat);
3856 stop_bpstat = inf_status->stop_bpstat;
3857 breakpoint_proceeded = inf_status->breakpoint_proceeded;
3858 proceed_to_finish = inf_status->proceed_to_finish;
3859
3860 /* FIXME: Is the restore of stop_registers always needed. */
3861 regcache_xfree (stop_registers);
3862 stop_registers = inf_status->stop_registers;
3863
3864 /* The inferior can be gone if the user types "print exit(0)"
3865 (and perhaps other times). */
3866 if (target_has_execution)
3867 /* NB: The register write goes through to the target. */
3868 regcache_cpy (current_regcache, inf_status->registers);
3869 regcache_xfree (inf_status->registers);
3870
3871 /* FIXME: If we are being called after stopping in a function which
3872 is called from gdb, we should not be trying to restore the
3873 selected frame; it just prints a spurious error message (The
3874 message is useful, however, in detecting bugs in gdb (like if gdb
3875 clobbers the stack)). In fact, should we be restoring the
3876 inferior status at all in that case? . */
3877
3878 if (target_has_stack && inf_status->restore_stack_info)
3879 {
3880 /* The point of catch_errors is that if the stack is clobbered,
3881 walking the stack might encounter a garbage pointer and
3882 error() trying to dereference it. */
3883 if (catch_errors
3884 (restore_selected_frame, &inf_status->selected_frame_id,
3885 "Unable to restore previously selected frame:\n",
3886 RETURN_MASK_ERROR) == 0)
3887 /* Error in restoring the selected frame. Select the innermost
3888 frame. */
3889 select_frame (get_current_frame ());
3890
3891 }
3892
3893 xfree (inf_status);
3894 }
3895
3896 static void
3897 do_restore_inferior_status_cleanup (void *sts)
3898 {
3899 restore_inferior_status (sts);
3900 }
3901
3902 struct cleanup *
3903 make_cleanup_restore_inferior_status (struct inferior_status *inf_status)
3904 {
3905 return make_cleanup (do_restore_inferior_status_cleanup, inf_status);
3906 }
3907
3908 void
3909 discard_inferior_status (struct inferior_status *inf_status)
3910 {
3911 /* See save_inferior_status for info on stop_bpstat. */
3912 bpstat_clear (&inf_status->stop_bpstat);
3913 regcache_xfree (inf_status->registers);
3914 regcache_xfree (inf_status->stop_registers);
3915 xfree (inf_status);
3916 }
3917
3918 int
3919 inferior_has_forked (int pid, int *child_pid)
3920 {
3921 struct target_waitstatus last;
3922 ptid_t last_ptid;
3923
3924 get_last_target_status (&last_ptid, &last);
3925
3926 if (last.kind != TARGET_WAITKIND_FORKED)
3927 return 0;
3928
3929 if (ptid_get_pid (last_ptid) != pid)
3930 return 0;
3931
3932 *child_pid = last.value.related_pid;
3933 return 1;
3934 }
3935
3936 int
3937 inferior_has_vforked (int pid, int *child_pid)
3938 {
3939 struct target_waitstatus last;
3940 ptid_t last_ptid;
3941
3942 get_last_target_status (&last_ptid, &last);
3943
3944 if (last.kind != TARGET_WAITKIND_VFORKED)
3945 return 0;
3946
3947 if (ptid_get_pid (last_ptid) != pid)
3948 return 0;
3949
3950 *child_pid = last.value.related_pid;
3951 return 1;
3952 }
3953
3954 int
3955 inferior_has_execd (int pid, char **execd_pathname)
3956 {
3957 struct target_waitstatus last;
3958 ptid_t last_ptid;
3959
3960 get_last_target_status (&last_ptid, &last);
3961
3962 if (last.kind != TARGET_WAITKIND_EXECD)
3963 return 0;
3964
3965 if (ptid_get_pid (last_ptid) != pid)
3966 return 0;
3967
3968 *execd_pathname = xstrdup (last.value.execd_pathname);
3969 return 1;
3970 }
3971
3972 /* Oft used ptids */
3973 ptid_t null_ptid;
3974 ptid_t minus_one_ptid;
3975
3976 /* Create a ptid given the necessary PID, LWP, and TID components. */
3977
3978 ptid_t
3979 ptid_build (int pid, long lwp, long tid)
3980 {
3981 ptid_t ptid;
3982
3983 ptid.pid = pid;
3984 ptid.lwp = lwp;
3985 ptid.tid = tid;
3986 return ptid;
3987 }
3988
3989 /* Create a ptid from just a pid. */
3990
3991 ptid_t
3992 pid_to_ptid (int pid)
3993 {
3994 return ptid_build (pid, 0, 0);
3995 }
3996
3997 /* Fetch the pid (process id) component from a ptid. */
3998
3999 int
4000 ptid_get_pid (ptid_t ptid)
4001 {
4002 return ptid.pid;
4003 }
4004
4005 /* Fetch the lwp (lightweight process) component from a ptid. */
4006
4007 long
4008 ptid_get_lwp (ptid_t ptid)
4009 {
4010 return ptid.lwp;
4011 }
4012
4013 /* Fetch the tid (thread id) component from a ptid. */
4014
4015 long
4016 ptid_get_tid (ptid_t ptid)
4017 {
4018 return ptid.tid;
4019 }
4020
4021 /* ptid_equal() is used to test equality of two ptids. */
4022
4023 int
4024 ptid_equal (ptid_t ptid1, ptid_t ptid2)
4025 {
4026 return (ptid1.pid == ptid2.pid && ptid1.lwp == ptid2.lwp
4027 && ptid1.tid == ptid2.tid);
4028 }
4029
4030 /* restore_inferior_ptid() will be used by the cleanup machinery
4031 to restore the inferior_ptid value saved in a call to
4032 save_inferior_ptid(). */
4033
4034 static void
4035 restore_inferior_ptid (void *arg)
4036 {
4037 ptid_t *saved_ptid_ptr = arg;
4038 inferior_ptid = *saved_ptid_ptr;
4039 xfree (arg);
4040 }
4041
4042 /* Save the value of inferior_ptid so that it may be restored by a
4043 later call to do_cleanups(). Returns the struct cleanup pointer
4044 needed for later doing the cleanup. */
4045
4046 struct cleanup *
4047 save_inferior_ptid (void)
4048 {
4049 ptid_t *saved_ptid_ptr;
4050
4051 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
4052 *saved_ptid_ptr = inferior_ptid;
4053 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
4054 }
4055 \f
4056
4057 static void
4058 build_infrun (void)
4059 {
4060 stop_registers = regcache_xmalloc (current_gdbarch);
4061 }
4062
4063 void
4064 _initialize_infrun (void)
4065 {
4066 register int i;
4067 register int numsigs;
4068 struct cmd_list_element *c;
4069
4070 register_gdbarch_swap (&stop_registers, sizeof (stop_registers), NULL);
4071 register_gdbarch_swap (NULL, 0, build_infrun);
4072
4073 add_info ("signals", signals_info,
4074 "What debugger does when program gets various signals.\n\
4075 Specify a signal as argument to print info on that signal only.");
4076 add_info_alias ("handle", "signals", 0);
4077
4078 add_com ("handle", class_run, handle_command,
4079 concat ("Specify how to handle a signal.\n\
4080 Args are signals and actions to apply to those signals.\n\
4081 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
4082 from 1-15 are allowed for compatibility with old versions of GDB.\n\
4083 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
4084 The special arg \"all\" is recognized to mean all signals except those\n\
4085 used by the debugger, typically SIGTRAP and SIGINT.\n", "Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
4086 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
4087 Stop means reenter debugger if this signal happens (implies print).\n\
4088 Print means print a message if this signal happens.\n\
4089 Pass means let program see this signal; otherwise program doesn't know.\n\
4090 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
4091 Pass and Stop may be combined.", NULL));
4092 if (xdb_commands)
4093 {
4094 add_com ("lz", class_info, signals_info,
4095 "What debugger does when program gets various signals.\n\
4096 Specify a signal as argument to print info on that signal only.");
4097 add_com ("z", class_run, xdb_handle_command,
4098 concat ("Specify how to handle a signal.\n\
4099 Args are signals and actions to apply to those signals.\n\
4100 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
4101 from 1-15 are allowed for compatibility with old versions of GDB.\n\
4102 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
4103 The special arg \"all\" is recognized to mean all signals except those\n\
4104 used by the debugger, typically SIGTRAP and SIGINT.\n", "Recognized actions include \"s\" (toggles between stop and nostop), \n\
4105 \"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
4106 nopass), \"Q\" (noprint)\n\
4107 Stop means reenter debugger if this signal happens (implies print).\n\
4108 Print means print a message if this signal happens.\n\
4109 Pass means let program see this signal; otherwise program doesn't know.\n\
4110 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
4111 Pass and Stop may be combined.", NULL));
4112 }
4113
4114 if (!dbx_commands)
4115 stop_command =
4116 add_cmd ("stop", class_obscure, not_just_help_class_command, "There is no `stop' command, but you can set a hook on `stop'.\n\
4117 This allows you to set a list of commands to be run each time execution\n\
4118 of the program stops.", &cmdlist);
4119
4120 numsigs = (int) TARGET_SIGNAL_LAST;
4121 signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
4122 signal_print = (unsigned char *)
4123 xmalloc (sizeof (signal_print[0]) * numsigs);
4124 signal_program = (unsigned char *)
4125 xmalloc (sizeof (signal_program[0]) * numsigs);
4126 for (i = 0; i < numsigs; i++)
4127 {
4128 signal_stop[i] = 1;
4129 signal_print[i] = 1;
4130 signal_program[i] = 1;
4131 }
4132
4133 /* Signals caused by debugger's own actions
4134 should not be given to the program afterwards. */
4135 signal_program[TARGET_SIGNAL_TRAP] = 0;
4136 signal_program[TARGET_SIGNAL_INT] = 0;
4137
4138 /* Signals that are not errors should not normally enter the debugger. */
4139 signal_stop[TARGET_SIGNAL_ALRM] = 0;
4140 signal_print[TARGET_SIGNAL_ALRM] = 0;
4141 signal_stop[TARGET_SIGNAL_VTALRM] = 0;
4142 signal_print[TARGET_SIGNAL_VTALRM] = 0;
4143 signal_stop[TARGET_SIGNAL_PROF] = 0;
4144 signal_print[TARGET_SIGNAL_PROF] = 0;
4145 signal_stop[TARGET_SIGNAL_CHLD] = 0;
4146 signal_print[TARGET_SIGNAL_CHLD] = 0;
4147 signal_stop[TARGET_SIGNAL_IO] = 0;
4148 signal_print[TARGET_SIGNAL_IO] = 0;
4149 signal_stop[TARGET_SIGNAL_POLL] = 0;
4150 signal_print[TARGET_SIGNAL_POLL] = 0;
4151 signal_stop[TARGET_SIGNAL_URG] = 0;
4152 signal_print[TARGET_SIGNAL_URG] = 0;
4153 signal_stop[TARGET_SIGNAL_WINCH] = 0;
4154 signal_print[TARGET_SIGNAL_WINCH] = 0;
4155
4156 /* These signals are used internally by user-level thread
4157 implementations. (See signal(5) on Solaris.) Like the above
4158 signals, a healthy program receives and handles them as part of
4159 its normal operation. */
4160 signal_stop[TARGET_SIGNAL_LWP] = 0;
4161 signal_print[TARGET_SIGNAL_LWP] = 0;
4162 signal_stop[TARGET_SIGNAL_WAITING] = 0;
4163 signal_print[TARGET_SIGNAL_WAITING] = 0;
4164 signal_stop[TARGET_SIGNAL_CANCEL] = 0;
4165 signal_print[TARGET_SIGNAL_CANCEL] = 0;
4166
4167 #ifdef SOLIB_ADD
4168 add_show_from_set
4169 (add_set_cmd ("stop-on-solib-events", class_support, var_zinteger,
4170 (char *) &stop_on_solib_events,
4171 "Set stopping for shared library events.\n\
4172 If nonzero, gdb will give control to the user when the dynamic linker\n\
4173 notifies gdb of shared library events. The most common event of interest\n\
4174 to the user would be loading/unloading of a new library.\n", &setlist), &showlist);
4175 #endif
4176
4177 c = add_set_enum_cmd ("follow-fork-mode",
4178 class_run,
4179 follow_fork_mode_kind_names, &follow_fork_mode_string,
4180 /* ??rehrauer: The "both" option is broken, by what may be a 10.20
4181 kernel problem. It's also not terribly useful without a GUI to
4182 help the user drive two debuggers. So for now, I'm disabling
4183 the "both" option. */
4184 /* "Set debugger response to a program call of fork \
4185 or vfork.\n\
4186 A fork or vfork creates a new process. follow-fork-mode can be:\n\
4187 parent - the original process is debugged after a fork\n\
4188 child - the new process is debugged after a fork\n\
4189 both - both the parent and child are debugged after a fork\n\
4190 ask - the debugger will ask for one of the above choices\n\
4191 For \"both\", another copy of the debugger will be started to follow\n\
4192 the new child process. The original debugger will continue to follow\n\
4193 the original parent process. To distinguish their prompts, the\n\
4194 debugger copy's prompt will be changed.\n\
4195 For \"parent\" or \"child\", the unfollowed process will run free.\n\
4196 By default, the debugger will follow the parent process.",
4197 */
4198 "Set debugger response to a program call of fork \
4199 or vfork.\n\
4200 A fork or vfork creates a new process. follow-fork-mode can be:\n\
4201 parent - the original process is debugged after a fork\n\
4202 child - the new process is debugged after a fork\n\
4203 ask - the debugger will ask for one of the above choices\n\
4204 For \"parent\" or \"child\", the unfollowed process will run free.\n\
4205 By default, the debugger will follow the parent process.", &setlist);
4206 add_show_from_set (c, &showlist);
4207
4208 c = add_set_enum_cmd ("scheduler-locking", class_run, scheduler_enums, /* array of string names */
4209 &scheduler_mode, /* current mode */
4210 "Set mode for locking scheduler during execution.\n\
4211 off == no locking (threads may preempt at any time)\n\
4212 on == full locking (no thread except the current thread may run)\n\
4213 step == scheduler locked during every single-step operation.\n\
4214 In this mode, no other thread may run during a step command.\n\
4215 Other threads may run while stepping over a function call ('next').", &setlist);
4216
4217 set_cmd_sfunc (c, set_schedlock_func); /* traps on target vector */
4218 add_show_from_set (c, &showlist);
4219
4220 c = add_set_cmd ("step-mode", class_run,
4221 var_boolean, (char *) &step_stop_if_no_debug,
4222 "Set mode of the step operation. When set, doing a step over a\n\
4223 function without debug line information will stop at the first\n\
4224 instruction of that function. Otherwise, the function is skipped and\n\
4225 the step command stops at a different source line.", &setlist);
4226 add_show_from_set (c, &showlist);
4227
4228 /* ptid initializations */
4229 null_ptid = ptid_build (0, 0, 0);
4230 minus_one_ptid = ptid_build (-1, 0, 0);
4231 inferior_ptid = null_ptid;
4232 target_last_wait_ptid = minus_one_ptid;
4233 }