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