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