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