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