*** empty log message ***
[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 3
6aba47ca 4 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
9b254dd1
DJ
5 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
6 2008 Free Software 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
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
c5aa993b 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 20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
22
23#include "defs.h"
24#include "gdb_string.h"
25#include <ctype.h>
26#include "symtab.h"
27#include "frame.h"
28#include "inferior.h"
60250e8b 29#include "exceptions.h"
c906108c 30#include "breakpoint.h"
03f2053f 31#include "gdb_wait.h"
c906108c
SS
32#include "gdbcore.h"
33#include "gdbcmd.h"
210661e7 34#include "cli/cli-script.h"
c906108c
SS
35#include "target.h"
36#include "gdbthread.h"
37#include "annotate.h"
1adeb98a 38#include "symfile.h"
7a292a7a 39#include "top.h"
c906108c 40#include <signal.h>
2acceee2 41#include "inf-loop.h"
4e052eda 42#include "regcache.h"
fd0407d6 43#include "value.h"
06600e06 44#include "observer.h"
f636b87d 45#include "language.h"
a77053c2 46#include "solib.h"
f17517ea 47#include "main.h"
a77053c2 48
9f976b41 49#include "gdb_assert.h"
034dad6f 50#include "mi/mi-common.h"
4f8d22e3 51#include "event-top.h"
c906108c
SS
52
53/* Prototypes for local functions */
54
96baa820 55static void signals_info (char *, int);
c906108c 56
96baa820 57static void handle_command (char *, int);
c906108c 58
96baa820 59static void sig_print_info (enum target_signal);
c906108c 60
96baa820 61static void sig_print_header (void);
c906108c 62
74b7792f 63static void resume_cleanups (void *);
c906108c 64
96baa820 65static int hook_stop_stub (void *);
c906108c 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
4e1c45ea 76static int currently_stepping (struct thread_info *tp);
96baa820 77
a7212384
UW
78static int currently_stepping_callback (struct thread_info *tp, void *data);
79
96baa820
JM
80static void xdb_handle_command (char *args, int from_tty);
81
6a6b96b9 82static int prepare_to_proceed (int);
ea67f13b 83
96baa820 84void _initialize_infrun (void);
43ff13b4 85
5fbbeb29
CF
86/* When set, stop the 'step' command if we enter a function which has
87 no line number information. The normal behavior is that we step
88 over such function. */
89int step_stop_if_no_debug = 0;
920d2a44
AC
90static void
91show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
92 struct cmd_list_element *c, const char *value)
93{
94 fprintf_filtered (file, _("Mode of the step operation is %s.\n"), value);
95}
5fbbeb29 96
43ff13b4 97/* In asynchronous mode, but simulating synchronous execution. */
96baa820 98
43ff13b4
JM
99int sync_execution = 0;
100
c906108c
SS
101/* wait_for_inferior and normal_stop use this to notify the user
102 when the inferior stopped in a different thread than it had been
96baa820
JM
103 running in. */
104
39f77062 105static ptid_t previous_inferior_ptid;
7a292a7a 106
237fc4c9
PA
107int debug_displaced = 0;
108static void
109show_debug_displaced (struct ui_file *file, int from_tty,
110 struct cmd_list_element *c, const char *value)
111{
112 fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
113}
114
527159b7 115static int debug_infrun = 0;
920d2a44
AC
116static void
117show_debug_infrun (struct ui_file *file, int from_tty,
118 struct cmd_list_element *c, const char *value)
119{
120 fprintf_filtered (file, _("Inferior debugging is %s.\n"), value);
121}
527159b7 122
d4f3574e
SS
123/* If the program uses ELF-style shared libraries, then calls to
124 functions in shared libraries go through stubs, which live in a
125 table called the PLT (Procedure Linkage Table). The first time the
126 function is called, the stub sends control to the dynamic linker,
127 which looks up the function's real address, patches the stub so
128 that future calls will go directly to the function, and then passes
129 control to the function.
130
131 If we are stepping at the source level, we don't want to see any of
132 this --- we just want to skip over the stub and the dynamic linker.
133 The simple approach is to single-step until control leaves the
134 dynamic linker.
135
ca557f44
AC
136 However, on some systems (e.g., Red Hat's 5.2 distribution) the
137 dynamic linker calls functions in the shared C library, so you
138 can't tell from the PC alone whether the dynamic linker is still
139 running. In this case, we use a step-resume breakpoint to get us
140 past the dynamic linker, as if we were using "next" to step over a
141 function call.
d4f3574e 142
cfd8ab24 143 in_solib_dynsym_resolve_code() says whether we're in the dynamic
d4f3574e
SS
144 linker code or not. Normally, this means we single-step. However,
145 if SKIP_SOLIB_RESOLVER then returns non-zero, then its value is an
146 address where we can place a step-resume breakpoint to get past the
147 linker's symbol resolution function.
148
cfd8ab24 149 in_solib_dynsym_resolve_code() can generally be implemented in a
d4f3574e
SS
150 pretty portable way, by comparing the PC against the address ranges
151 of the dynamic linker's sections.
152
153 SKIP_SOLIB_RESOLVER is generally going to be system-specific, since
154 it depends on internal details of the dynamic linker. It's usually
155 not too hard to figure out where to put a breakpoint, but it
156 certainly isn't portable. SKIP_SOLIB_RESOLVER should do plenty of
157 sanity checking. If it can't figure things out, returning zero and
158 getting the (possibly confusing) stepping behavior is better than
159 signalling an error, which will obscure the change in the
160 inferior's state. */
c906108c 161
c906108c
SS
162/* This function returns TRUE if pc is the address of an instruction
163 that lies within the dynamic linker (such as the event hook, or the
164 dld itself).
165
166 This function must be used only when a dynamic linker event has
167 been caught, and the inferior is being stepped out of the hook, or
168 undefined results are guaranteed. */
169
170#ifndef SOLIB_IN_DYNAMIC_LINKER
171#define SOLIB_IN_DYNAMIC_LINKER(pid,pc) 0
172#endif
173
c2c6d25f 174
7a292a7a
SS
175/* Convert the #defines into values. This is temporary until wfi control
176 flow is completely sorted out. */
177
692590c1
MS
178#ifndef CANNOT_STEP_HW_WATCHPOINTS
179#define CANNOT_STEP_HW_WATCHPOINTS 0
180#else
181#undef CANNOT_STEP_HW_WATCHPOINTS
182#define CANNOT_STEP_HW_WATCHPOINTS 1
183#endif
184
c906108c
SS
185/* Tables of how to react to signals; the user sets them. */
186
187static unsigned char *signal_stop;
188static unsigned char *signal_print;
189static unsigned char *signal_program;
190
191#define SET_SIGS(nsigs,sigs,flags) \
192 do { \
193 int signum = (nsigs); \
194 while (signum-- > 0) \
195 if ((sigs)[signum]) \
196 (flags)[signum] = 1; \
197 } while (0)
198
199#define UNSET_SIGS(nsigs,sigs,flags) \
200 do { \
201 int signum = (nsigs); \
202 while (signum-- > 0) \
203 if ((sigs)[signum]) \
204 (flags)[signum] = 0; \
205 } while (0)
206
39f77062
KB
207/* Value to pass to target_resume() to cause all threads to resume */
208
209#define RESUME_ALL (pid_to_ptid (-1))
c906108c
SS
210
211/* Command list pointer for the "stop" placeholder. */
212
213static struct cmd_list_element *stop_command;
214
c906108c
SS
215/* Function inferior was in as of last step command. */
216
217static struct symbol *step_start_function;
218
c906108c
SS
219/* Nonzero if we want to give control to the user when we're notified
220 of shared library events by the dynamic linker. */
221static int stop_on_solib_events;
920d2a44
AC
222static void
223show_stop_on_solib_events (struct ui_file *file, int from_tty,
224 struct cmd_list_element *c, const char *value)
225{
226 fprintf_filtered (file, _("Stopping for shared library events is %s.\n"),
227 value);
228}
c906108c 229
c906108c
SS
230/* Nonzero means expecting a trace trap
231 and should stop the inferior and return silently when it happens. */
232
233int stop_after_trap;
234
642fd101
DE
235/* Save register contents here when executing a "finish" command or are
236 about to pop a stack dummy frame, if-and-only-if proceed_to_finish is set.
c906108c
SS
237 Thus this contains the return value from the called function (assuming
238 values are returned in a register). */
239
72cec141 240struct regcache *stop_registers;
c906108c 241
c906108c
SS
242/* Nonzero after stop if current stack frame should be printed. */
243
244static int stop_print_frame;
245
e02bc4cc 246/* This is a cached copy of the pid/waitstatus of the last event
9a4105ab
AC
247 returned by target_wait()/deprecated_target_wait_hook(). This
248 information is returned by get_last_target_status(). */
39f77062 249static ptid_t target_last_wait_ptid;
e02bc4cc
DS
250static struct target_waitstatus target_last_waitstatus;
251
0d1e5fa7
PA
252static void context_switch (ptid_t ptid);
253
4e1c45ea 254void init_thread_stepping_state (struct thread_info *tss);
0d1e5fa7
PA
255
256void init_infwait_state (void);
a474d7c2 257
c906108c
SS
258/* This is used to remember when a fork, vfork or exec event
259 was caught by a catchpoint, and thus the event is to be
260 followed at the next resume of the inferior, and not
261 immediately. */
262static struct
488f131b
JB
263{
264 enum target_waitkind kind;
265 struct
c906108c 266 {
3a3e9ee3
PA
267 ptid_t parent_pid;
268 ptid_t child_pid;
c906108c 269 }
488f131b
JB
270 fork_event;
271 char *execd_pathname;
272}
c906108c
SS
273pending_follow;
274
53904c9e
AC
275static const char follow_fork_mode_child[] = "child";
276static const char follow_fork_mode_parent[] = "parent";
277
488f131b 278static const char *follow_fork_mode_kind_names[] = {
53904c9e
AC
279 follow_fork_mode_child,
280 follow_fork_mode_parent,
281 NULL
ef346e04 282};
c906108c 283
53904c9e 284static const char *follow_fork_mode_string = follow_fork_mode_parent;
920d2a44
AC
285static void
286show_follow_fork_mode_string (struct ui_file *file, int from_tty,
287 struct cmd_list_element *c, const char *value)
288{
289 fprintf_filtered (file, _("\
290Debugger response to a program call of fork or vfork is \"%s\".\n"),
291 value);
292}
c906108c
SS
293\f
294
6604731b 295static int
4ef3f3be 296follow_fork (void)
c906108c 297{
ea1dd7bc 298 int follow_child = (follow_fork_mode_string == follow_fork_mode_child);
c906108c 299
6604731b 300 return target_follow_fork (follow_child);
c906108c
SS
301}
302
6604731b
DJ
303void
304follow_inferior_reset_breakpoints (void)
c906108c 305{
4e1c45ea
PA
306 struct thread_info *tp = inferior_thread ();
307
6604731b
DJ
308 /* Was there a step_resume breakpoint? (There was if the user
309 did a "next" at the fork() call.) If so, explicitly reset its
310 thread number.
311
312 step_resumes are a form of bp that are made to be per-thread.
313 Since we created the step_resume bp when the parent process
314 was being debugged, and now are switching to the child process,
315 from the breakpoint package's viewpoint, that's a switch of
316 "threads". We must update the bp's notion of which thread
317 it is for, or it'll be ignored when it triggers. */
318
4e1c45ea
PA
319 if (tp->step_resume_breakpoint)
320 breakpoint_re_set_thread (tp->step_resume_breakpoint);
6604731b
DJ
321
322 /* Reinsert all breakpoints in the child. The user may have set
323 breakpoints after catching the fork, in which case those
324 were never set in the child, but only in the parent. This makes
325 sure the inserted breakpoints match the breakpoint list. */
326
327 breakpoint_re_set ();
328 insert_breakpoints ();
c906108c 329}
c906108c 330
1adeb98a
FN
331/* EXECD_PATHNAME is assumed to be non-NULL. */
332
c906108c 333static void
3a3e9ee3 334follow_exec (ptid_t pid, char *execd_pathname)
c906108c 335{
7a292a7a 336 struct target_ops *tgt;
4e1c45ea 337 struct thread_info *th = inferior_thread ();
7a292a7a 338
c906108c
SS
339 /* This is an exec event that we actually wish to pay attention to.
340 Refresh our symbol table to the newly exec'd program, remove any
341 momentary bp's, etc.
342
343 If there are breakpoints, they aren't really inserted now,
344 since the exec() transformed our inferior into a fresh set
345 of instructions.
346
347 We want to preserve symbolic breakpoints on the list, since
348 we have hopes that they can be reset after the new a.out's
349 symbol table is read.
350
351 However, any "raw" breakpoints must be removed from the list
352 (e.g., the solib bp's), since their address is probably invalid
353 now.
354
355 And, we DON'T want to call delete_breakpoints() here, since
356 that may write the bp's "shadow contents" (the instruction
357 value that was overwritten witha TRAP instruction). Since
358 we now have a new a.out, those shadow contents aren't valid. */
359 update_breakpoints_after_exec ();
360
361 /* If there was one, it's gone now. We cannot truly step-to-next
362 statement through an exec(). */
4e1c45ea
PA
363 th->step_resume_breakpoint = NULL;
364 th->step_range_start = 0;
365 th->step_range_end = 0;
c906108c 366
c906108c 367 /* What is this a.out's name? */
a3f17187 368 printf_unfiltered (_("Executing new program: %s\n"), execd_pathname);
c906108c
SS
369
370 /* We've followed the inferior through an exec. Therefore, the
371 inferior has essentially been killed & reborn. */
7a292a7a 372
c906108c 373 gdb_flush (gdb_stdout);
6ca15a4b
PA
374
375 breakpoint_init_inferior (inf_execd);
e85a822c
DJ
376
377 if (gdb_sysroot && *gdb_sysroot)
378 {
379 char *name = alloca (strlen (gdb_sysroot)
380 + strlen (execd_pathname)
381 + 1);
382 strcpy (name, gdb_sysroot);
383 strcat (name, execd_pathname);
384 execd_pathname = name;
385 }
c906108c
SS
386
387 /* That a.out is now the one to use. */
388 exec_file_attach (execd_pathname, 0);
389
cce9b6bf
PA
390 /* Reset the shared library package. This ensures that we get a
391 shlib event when the child reaches "_start", at which point the
392 dld will have had a chance to initialize the child. */
393 /* Also, loading a symbol file below may trigger symbol lookups, and
394 we don't want those to be satisfied by the libraries of the
395 previous incarnation of this process. */
396 no_shared_libraries (NULL, 0);
397
398 /* Load the main file's symbols. */
1adeb98a 399 symbol_file_add_main (execd_pathname, 0);
c906108c 400
7a292a7a 401#ifdef SOLIB_CREATE_INFERIOR_HOOK
39f77062 402 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
a77053c2
MK
403#else
404 solib_create_inferior_hook ();
7a292a7a 405#endif
c906108c
SS
406
407 /* Reinsert all breakpoints. (Those which were symbolic have
408 been reset to the proper address in the new a.out, thanks
409 to symbol_file_command...) */
410 insert_breakpoints ();
411
412 /* The next resume of this inferior should bring it to the shlib
413 startup breakpoints. (If the user had also set bp's on
414 "main" from the old (parent) process, then they'll auto-
415 matically get reset there in the new process.) */
c906108c
SS
416}
417
418/* Non-zero if we just simulating a single-step. This is needed
419 because we cannot remove the breakpoints in the inferior process
420 until after the `wait' in `wait_for_inferior'. */
421static int singlestep_breakpoints_inserted_p = 0;
9f976b41
DJ
422
423/* The thread we inserted single-step breakpoints for. */
424static ptid_t singlestep_ptid;
425
fd48f117
DJ
426/* PC when we started this single-step. */
427static CORE_ADDR singlestep_pc;
428
9f976b41
DJ
429/* If another thread hit the singlestep breakpoint, we save the original
430 thread here so that we can resume single-stepping it later. */
431static ptid_t saved_singlestep_ptid;
432static int stepping_past_singlestep_breakpoint;
6a6b96b9 433
ca67fcb8
VP
434/* If not equal to null_ptid, this means that after stepping over breakpoint
435 is finished, we need to switch to deferred_step_ptid, and step it.
436
437 The use case is when one thread has hit a breakpoint, and then the user
438 has switched to another thread and issued 'step'. We need to step over
439 breakpoint in the thread which hit the breakpoint, but then continue
440 stepping the thread user has selected. */
441static ptid_t deferred_step_ptid;
c906108c 442\f
237fc4c9
PA
443/* Displaced stepping. */
444
445/* In non-stop debugging mode, we must take special care to manage
446 breakpoints properly; in particular, the traditional strategy for
447 stepping a thread past a breakpoint it has hit is unsuitable.
448 'Displaced stepping' is a tactic for stepping one thread past a
449 breakpoint it has hit while ensuring that other threads running
450 concurrently will hit the breakpoint as they should.
451
452 The traditional way to step a thread T off a breakpoint in a
453 multi-threaded program in all-stop mode is as follows:
454
455 a0) Initially, all threads are stopped, and breakpoints are not
456 inserted.
457 a1) We single-step T, leaving breakpoints uninserted.
458 a2) We insert breakpoints, and resume all threads.
459
460 In non-stop debugging, however, this strategy is unsuitable: we
461 don't want to have to stop all threads in the system in order to
462 continue or step T past a breakpoint. Instead, we use displaced
463 stepping:
464
465 n0) Initially, T is stopped, other threads are running, and
466 breakpoints are inserted.
467 n1) We copy the instruction "under" the breakpoint to a separate
468 location, outside the main code stream, making any adjustments
469 to the instruction, register, and memory state as directed by
470 T's architecture.
471 n2) We single-step T over the instruction at its new location.
472 n3) We adjust the resulting register and memory state as directed
473 by T's architecture. This includes resetting T's PC to point
474 back into the main instruction stream.
475 n4) We resume T.
476
477 This approach depends on the following gdbarch methods:
478
479 - gdbarch_max_insn_length and gdbarch_displaced_step_location
480 indicate where to copy the instruction, and how much space must
481 be reserved there. We use these in step n1.
482
483 - gdbarch_displaced_step_copy_insn copies a instruction to a new
484 address, and makes any necessary adjustments to the instruction,
485 register contents, and memory. We use this in step n1.
486
487 - gdbarch_displaced_step_fixup adjusts registers and memory after
488 we have successfuly single-stepped the instruction, to yield the
489 same effect the instruction would have had if we had executed it
490 at its original address. We use this in step n3.
491
492 - gdbarch_displaced_step_free_closure provides cleanup.
493
494 The gdbarch_displaced_step_copy_insn and
495 gdbarch_displaced_step_fixup functions must be written so that
496 copying an instruction with gdbarch_displaced_step_copy_insn,
497 single-stepping across the copied instruction, and then applying
498 gdbarch_displaced_insn_fixup should have the same effects on the
499 thread's memory and registers as stepping the instruction in place
500 would have. Exactly which responsibilities fall to the copy and
501 which fall to the fixup is up to the author of those functions.
502
503 See the comments in gdbarch.sh for details.
504
505 Note that displaced stepping and software single-step cannot
506 currently be used in combination, although with some care I think
507 they could be made to. Software single-step works by placing
508 breakpoints on all possible subsequent instructions; if the
509 displaced instruction is a PC-relative jump, those breakpoints
510 could fall in very strange places --- on pages that aren't
511 executable, or at addresses that are not proper instruction
512 boundaries. (We do generally let other threads run while we wait
513 to hit the software single-step breakpoint, and they might
514 encounter such a corrupted instruction.) One way to work around
515 this would be to have gdbarch_displaced_step_copy_insn fully
516 simulate the effect of PC-relative instructions (and return NULL)
517 on architectures that use software single-stepping.
518
519 In non-stop mode, we can have independent and simultaneous step
520 requests, so more than one thread may need to simultaneously step
521 over a breakpoint. The current implementation assumes there is
522 only one scratch space per process. In this case, we have to
523 serialize access to the scratch space. If thread A wants to step
524 over a breakpoint, but we are currently waiting for some other
525 thread to complete a displaced step, we leave thread A stopped and
526 place it in the displaced_step_request_queue. Whenever a displaced
527 step finishes, we pick the next thread in the queue and start a new
528 displaced step operation on it. See displaced_step_prepare and
529 displaced_step_fixup for details. */
530
531/* If this is not null_ptid, this is the thread carrying out a
532 displaced single-step. This thread's state will require fixing up
533 once it has completed its step. */
534static ptid_t displaced_step_ptid;
535
536struct displaced_step_request
537{
538 ptid_t ptid;
539 struct displaced_step_request *next;
540};
541
542/* A queue of pending displaced stepping requests. */
543struct displaced_step_request *displaced_step_request_queue;
544
545/* The architecture the thread had when we stepped it. */
546static struct gdbarch *displaced_step_gdbarch;
547
548/* The closure provided gdbarch_displaced_step_copy_insn, to be used
549 for post-step cleanup. */
550static struct displaced_step_closure *displaced_step_closure;
551
552/* The address of the original instruction, and the copy we made. */
553static CORE_ADDR displaced_step_original, displaced_step_copy;
554
555/* Saved contents of copy area. */
556static gdb_byte *displaced_step_saved_copy;
557
fff08868
HZ
558/* Enum strings for "set|show displaced-stepping". */
559
560static const char can_use_displaced_stepping_auto[] = "auto";
561static const char can_use_displaced_stepping_on[] = "on";
562static const char can_use_displaced_stepping_off[] = "off";
563static const char *can_use_displaced_stepping_enum[] =
564{
565 can_use_displaced_stepping_auto,
566 can_use_displaced_stepping_on,
567 can_use_displaced_stepping_off,
568 NULL,
569};
570
571/* If ON, and the architecture supports it, GDB will use displaced
572 stepping to step over breakpoints. If OFF, or if the architecture
573 doesn't support it, GDB will instead use the traditional
574 hold-and-step approach. If AUTO (which is the default), GDB will
575 decide which technique to use to step over breakpoints depending on
576 which of all-stop or non-stop mode is active --- displaced stepping
577 in non-stop mode; hold-and-step in all-stop mode. */
578
579static const char *can_use_displaced_stepping =
580 can_use_displaced_stepping_auto;
581
237fc4c9
PA
582static void
583show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
584 struct cmd_list_element *c,
585 const char *value)
586{
fff08868
HZ
587 if (can_use_displaced_stepping == can_use_displaced_stepping_auto)
588 fprintf_filtered (file, _("\
589Debugger's willingness to use displaced stepping to step over \
590breakpoints is %s (currently %s).\n"),
591 value, non_stop ? "on" : "off");
592 else
593 fprintf_filtered (file, _("\
594Debugger's willingness to use displaced stepping to step over \
595breakpoints is %s.\n"), value);
237fc4c9
PA
596}
597
fff08868
HZ
598/* Return non-zero if displaced stepping can/should be used to step
599 over breakpoints. */
600
237fc4c9
PA
601static int
602use_displaced_stepping (struct gdbarch *gdbarch)
603{
fff08868
HZ
604 return (((can_use_displaced_stepping == can_use_displaced_stepping_auto
605 && non_stop)
606 || can_use_displaced_stepping == can_use_displaced_stepping_on)
237fc4c9
PA
607 && gdbarch_displaced_step_copy_insn_p (gdbarch));
608}
609
610/* Clean out any stray displaced stepping state. */
611static void
612displaced_step_clear (void)
613{
614 /* Indicate that there is no cleanup pending. */
615 displaced_step_ptid = null_ptid;
616
617 if (displaced_step_closure)
618 {
619 gdbarch_displaced_step_free_closure (displaced_step_gdbarch,
620 displaced_step_closure);
621 displaced_step_closure = NULL;
622 }
623}
624
625static void
626cleanup_displaced_step_closure (void *ptr)
627{
628 struct displaced_step_closure *closure = ptr;
629
630 gdbarch_displaced_step_free_closure (current_gdbarch, closure);
631}
632
633/* Dump LEN bytes at BUF in hex to FILE, followed by a newline. */
634void
635displaced_step_dump_bytes (struct ui_file *file,
636 const gdb_byte *buf,
637 size_t len)
638{
639 int i;
640
641 for (i = 0; i < len; i++)
642 fprintf_unfiltered (file, "%02x ", buf[i]);
643 fputs_unfiltered ("\n", file);
644}
645
646/* Prepare to single-step, using displaced stepping.
647
648 Note that we cannot use displaced stepping when we have a signal to
649 deliver. If we have a signal to deliver and an instruction to step
650 over, then after the step, there will be no indication from the
651 target whether the thread entered a signal handler or ignored the
652 signal and stepped over the instruction successfully --- both cases
653 result in a simple SIGTRAP. In the first case we mustn't do a
654 fixup, and in the second case we must --- but we can't tell which.
655 Comments in the code for 'random signals' in handle_inferior_event
656 explain how we handle this case instead.
657
658 Returns 1 if preparing was successful -- this thread is going to be
659 stepped now; or 0 if displaced stepping this thread got queued. */
660static int
661displaced_step_prepare (ptid_t ptid)
662{
ad53cd71 663 struct cleanup *old_cleanups, *ignore_cleanups;
237fc4c9
PA
664 struct regcache *regcache = get_thread_regcache (ptid);
665 struct gdbarch *gdbarch = get_regcache_arch (regcache);
666 CORE_ADDR original, copy;
667 ULONGEST len;
668 struct displaced_step_closure *closure;
669
670 /* We should never reach this function if the architecture does not
671 support displaced stepping. */
672 gdb_assert (gdbarch_displaced_step_copy_insn_p (gdbarch));
673
674 /* For the first cut, we're displaced stepping one thread at a
675 time. */
676
677 if (!ptid_equal (displaced_step_ptid, null_ptid))
678 {
679 /* Already waiting for a displaced step to finish. Defer this
680 request and place in queue. */
681 struct displaced_step_request *req, *new_req;
682
683 if (debug_displaced)
684 fprintf_unfiltered (gdb_stdlog,
685 "displaced: defering step of %s\n",
686 target_pid_to_str (ptid));
687
688 new_req = xmalloc (sizeof (*new_req));
689 new_req->ptid = ptid;
690 new_req->next = NULL;
691
692 if (displaced_step_request_queue)
693 {
694 for (req = displaced_step_request_queue;
695 req && req->next;
696 req = req->next)
697 ;
698 req->next = new_req;
699 }
700 else
701 displaced_step_request_queue = new_req;
702
703 return 0;
704 }
705 else
706 {
707 if (debug_displaced)
708 fprintf_unfiltered (gdb_stdlog,
709 "displaced: stepping %s now\n",
710 target_pid_to_str (ptid));
711 }
712
713 displaced_step_clear ();
714
ad53cd71
PA
715 old_cleanups = save_inferior_ptid ();
716 inferior_ptid = ptid;
717
515630c5 718 original = regcache_read_pc (regcache);
237fc4c9
PA
719
720 copy = gdbarch_displaced_step_location (gdbarch);
721 len = gdbarch_max_insn_length (gdbarch);
722
723 /* Save the original contents of the copy area. */
724 displaced_step_saved_copy = xmalloc (len);
ad53cd71
PA
725 ignore_cleanups = make_cleanup (free_current_contents,
726 &displaced_step_saved_copy);
237fc4c9
PA
727 read_memory (copy, displaced_step_saved_copy, len);
728 if (debug_displaced)
729 {
730 fprintf_unfiltered (gdb_stdlog, "displaced: saved 0x%s: ",
731 paddr_nz (copy));
732 displaced_step_dump_bytes (gdb_stdlog, displaced_step_saved_copy, len);
733 };
734
735 closure = gdbarch_displaced_step_copy_insn (gdbarch,
ad53cd71 736 original, copy, regcache);
237fc4c9
PA
737
738 /* We don't support the fully-simulated case at present. */
739 gdb_assert (closure);
740
741 make_cleanup (cleanup_displaced_step_closure, closure);
742
743 /* Resume execution at the copy. */
515630c5 744 regcache_write_pc (regcache, copy);
237fc4c9 745
ad53cd71
PA
746 discard_cleanups (ignore_cleanups);
747
748 do_cleanups (old_cleanups);
237fc4c9
PA
749
750 if (debug_displaced)
751 fprintf_unfiltered (gdb_stdlog, "displaced: displaced pc to 0x%s\n",
ad53cd71 752 paddr_nz (copy));
237fc4c9
PA
753
754 /* Save the information we need to fix things up if the step
755 succeeds. */
756 displaced_step_ptid = ptid;
757 displaced_step_gdbarch = gdbarch;
758 displaced_step_closure = closure;
759 displaced_step_original = original;
760 displaced_step_copy = copy;
761 return 1;
762}
763
764static void
765displaced_step_clear_cleanup (void *ignore)
766{
767 displaced_step_clear ();
768}
769
770static void
771write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
772{
773 struct cleanup *ptid_cleanup = save_inferior_ptid ();
774 inferior_ptid = ptid;
775 write_memory (memaddr, myaddr, len);
776 do_cleanups (ptid_cleanup);
777}
778
779static void
780displaced_step_fixup (ptid_t event_ptid, enum target_signal signal)
781{
782 struct cleanup *old_cleanups;
783
784 /* Was this event for the pid we displaced? */
785 if (ptid_equal (displaced_step_ptid, null_ptid)
786 || ! ptid_equal (displaced_step_ptid, event_ptid))
787 return;
788
789 old_cleanups = make_cleanup (displaced_step_clear_cleanup, 0);
790
791 /* Restore the contents of the copy area. */
792 {
793 ULONGEST len = gdbarch_max_insn_length (displaced_step_gdbarch);
794 write_memory_ptid (displaced_step_ptid, displaced_step_copy,
795 displaced_step_saved_copy, len);
796 if (debug_displaced)
797 fprintf_unfiltered (gdb_stdlog, "displaced: restored 0x%s\n",
798 paddr_nz (displaced_step_copy));
799 }
800
801 /* Did the instruction complete successfully? */
802 if (signal == TARGET_SIGNAL_TRAP)
803 {
804 /* Fix up the resulting state. */
805 gdbarch_displaced_step_fixup (displaced_step_gdbarch,
806 displaced_step_closure,
807 displaced_step_original,
808 displaced_step_copy,
809 get_thread_regcache (displaced_step_ptid));
810 }
811 else
812 {
813 /* Since the instruction didn't complete, all we can do is
814 relocate the PC. */
515630c5
UW
815 struct regcache *regcache = get_thread_regcache (event_ptid);
816 CORE_ADDR pc = regcache_read_pc (regcache);
237fc4c9 817 pc = displaced_step_original + (pc - displaced_step_copy);
515630c5 818 regcache_write_pc (regcache, pc);
237fc4c9
PA
819 }
820
821 do_cleanups (old_cleanups);
822
1c5cfe86
PA
823 displaced_step_ptid = null_ptid;
824
237fc4c9
PA
825 /* Are there any pending displaced stepping requests? If so, run
826 one now. */
1c5cfe86 827 while (displaced_step_request_queue)
237fc4c9
PA
828 {
829 struct displaced_step_request *head;
830 ptid_t ptid;
1c5cfe86 831 CORE_ADDR actual_pc;
237fc4c9
PA
832
833 head = displaced_step_request_queue;
834 ptid = head->ptid;
835 displaced_step_request_queue = head->next;
836 xfree (head);
837
ad53cd71
PA
838 context_switch (ptid);
839
1c5cfe86
PA
840 actual_pc = read_pc ();
841
842 if (breakpoint_here_p (actual_pc))
ad53cd71 843 {
1c5cfe86
PA
844 if (debug_displaced)
845 fprintf_unfiltered (gdb_stdlog,
846 "displaced: stepping queued %s now\n",
847 target_pid_to_str (ptid));
848
849 displaced_step_prepare (ptid);
850
851 if (debug_displaced)
852 {
853 gdb_byte buf[4];
854
855 fprintf_unfiltered (gdb_stdlog, "displaced: run 0x%s: ",
856 paddr_nz (actual_pc));
857 read_memory (actual_pc, buf, sizeof (buf));
858 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
859 }
860
861 target_resume (ptid, 1, TARGET_SIGNAL_0);
862
863 /* Done, we're stepping a thread. */
864 break;
ad53cd71 865 }
1c5cfe86
PA
866 else
867 {
868 int step;
869 struct thread_info *tp = inferior_thread ();
870
871 /* The breakpoint we were sitting under has since been
872 removed. */
873 tp->trap_expected = 0;
874
875 /* Go back to what we were trying to do. */
876 step = currently_stepping (tp);
ad53cd71 877
1c5cfe86
PA
878 if (debug_displaced)
879 fprintf_unfiltered (gdb_stdlog, "breakpoint is gone %s: step(%d)\n",
880 target_pid_to_str (tp->ptid), step);
881
882 target_resume (ptid, step, TARGET_SIGNAL_0);
883 tp->stop_signal = TARGET_SIGNAL_0;
884
885 /* This request was discarded. See if there's any other
886 thread waiting for its turn. */
887 }
237fc4c9
PA
888 }
889}
890
5231c1fd
PA
891/* Update global variables holding ptids to hold NEW_PTID if they were
892 holding OLD_PTID. */
893static void
894infrun_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
895{
896 struct displaced_step_request *it;
897
898 if (ptid_equal (inferior_ptid, old_ptid))
899 inferior_ptid = new_ptid;
900
901 if (ptid_equal (singlestep_ptid, old_ptid))
902 singlestep_ptid = new_ptid;
903
904 if (ptid_equal (displaced_step_ptid, old_ptid))
905 displaced_step_ptid = new_ptid;
906
907 if (ptid_equal (deferred_step_ptid, old_ptid))
908 deferred_step_ptid = new_ptid;
909
910 for (it = displaced_step_request_queue; it; it = it->next)
911 if (ptid_equal (it->ptid, old_ptid))
912 it->ptid = new_ptid;
913}
914
237fc4c9
PA
915\f
916/* Resuming. */
c906108c
SS
917
918/* Things to clean up if we QUIT out of resume (). */
c906108c 919static void
74b7792f 920resume_cleanups (void *ignore)
c906108c
SS
921{
922 normal_stop ();
923}
924
53904c9e
AC
925static const char schedlock_off[] = "off";
926static const char schedlock_on[] = "on";
927static const char schedlock_step[] = "step";
488f131b 928static const char *scheduler_enums[] = {
ef346e04
AC
929 schedlock_off,
930 schedlock_on,
931 schedlock_step,
932 NULL
933};
920d2a44
AC
934static const char *scheduler_mode = schedlock_off;
935static void
936show_scheduler_mode (struct ui_file *file, int from_tty,
937 struct cmd_list_element *c, const char *value)
938{
939 fprintf_filtered (file, _("\
940Mode for locking scheduler during execution is \"%s\".\n"),
941 value);
942}
c906108c
SS
943
944static void
96baa820 945set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
c906108c 946{
eefe576e
AC
947 if (!target_can_lock_scheduler)
948 {
949 scheduler_mode = schedlock_off;
950 error (_("Target '%s' cannot support this command."), target_shortname);
951 }
c906108c
SS
952}
953
954
955/* Resume the inferior, but allow a QUIT. This is useful if the user
956 wants to interrupt some lengthy single-stepping operation
957 (for child processes, the SIGINT goes to the inferior, and so
958 we get a SIGINT random_signal, but for remote debugging and perhaps
959 other targets, that's not true).
960
961 STEP nonzero if we should step (zero to continue instead).
962 SIG is the signal to give the inferior (zero for none). */
963void
96baa820 964resume (int step, enum target_signal sig)
c906108c
SS
965{
966 int should_resume = 1;
74b7792f 967 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
c7e8a53c
PA
968
969 /* Note that these must be reset if we follow a fork below. */
515630c5
UW
970 struct regcache *regcache = get_current_regcache ();
971 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4e1c45ea 972 struct thread_info *tp = inferior_thread ();
515630c5 973 CORE_ADDR pc = regcache_read_pc (regcache);
c7e8a53c 974
c906108c
SS
975 QUIT;
976
527159b7 977 if (debug_infrun)
237fc4c9
PA
978 fprintf_unfiltered (gdb_stdlog,
979 "infrun: resume (step=%d, signal=%d), "
4e1c45ea
PA
980 "trap_expected=%d\n",
981 step, sig, tp->trap_expected);
c906108c 982
692590c1
MS
983 /* Some targets (e.g. Solaris x86) have a kernel bug when stepping
984 over an instruction that causes a page fault without triggering
985 a hardware watchpoint. The kernel properly notices that it shouldn't
986 stop, because the hardware watchpoint is not triggered, but it forgets
987 the step request and continues the program normally.
988 Work around the problem by removing hardware watchpoints if a step is
989 requested, GDB will check for a hardware watchpoint trigger after the
990 step anyway. */
c36b740a 991 if (CANNOT_STEP_HW_WATCHPOINTS && step)
692590c1 992 remove_hw_watchpoints ();
488f131b 993
692590c1 994
c2c6d25f
JM
995 /* Normally, by the time we reach `resume', the breakpoints are either
996 removed or inserted, as appropriate. The exception is if we're sitting
997 at a permanent breakpoint; we need to step over it, but permanent
998 breakpoints can't be removed. So we have to test for it here. */
237fc4c9 999 if (breakpoint_here_p (pc) == permanent_breakpoint_here)
6d350bb5 1000 {
515630c5
UW
1001 if (gdbarch_skip_permanent_breakpoint_p (gdbarch))
1002 gdbarch_skip_permanent_breakpoint (gdbarch, regcache);
6d350bb5
UW
1003 else
1004 error (_("\
1005The program is stopped at a permanent breakpoint, but GDB does not know\n\
1006how to step past a permanent breakpoint on this architecture. Try using\n\
1007a command like `return' or `jump' to continue execution."));
1008 }
c2c6d25f 1009
237fc4c9
PA
1010 /* If enabled, step over breakpoints by executing a copy of the
1011 instruction at a different address.
1012
1013 We can't use displaced stepping when we have a signal to deliver;
1014 the comments for displaced_step_prepare explain why. The
1015 comments in the handle_inferior event for dealing with 'random
1016 signals' explain what we do instead. */
515630c5 1017 if (use_displaced_stepping (gdbarch)
4e1c45ea 1018 && tp->trap_expected
237fc4c9
PA
1019 && sig == TARGET_SIGNAL_0)
1020 {
1021 if (!displaced_step_prepare (inferior_ptid))
d56b7306
VP
1022 {
1023 /* Got placed in displaced stepping queue. Will be resumed
1024 later when all the currently queued displaced stepping
7f7efbd9
VP
1025 requests finish. The thread is not executing at this point,
1026 and the call to set_executing will be made later. But we
1027 need to call set_running here, since from frontend point of view,
1028 the thread is running. */
1029 set_running (inferior_ptid, 1);
d56b7306
VP
1030 discard_cleanups (old_cleanups);
1031 return;
1032 }
237fc4c9
PA
1033 }
1034
515630c5 1035 if (step && gdbarch_software_single_step_p (gdbarch))
c906108c
SS
1036 {
1037 /* Do it the hard way, w/temp breakpoints */
515630c5 1038 if (gdbarch_software_single_step (gdbarch, get_current_frame ()))
e6590a1b
UW
1039 {
1040 /* ...and don't ask hardware to do it. */
1041 step = 0;
1042 /* and do not pull these breakpoints until after a `wait' in
1043 `wait_for_inferior' */
1044 singlestep_breakpoints_inserted_p = 1;
1045 singlestep_ptid = inferior_ptid;
237fc4c9 1046 singlestep_pc = pc;
e6590a1b 1047 }
c906108c
SS
1048 }
1049
c906108c 1050 /* If there were any forks/vforks/execs that were caught and are
6604731b 1051 now to be followed, then do so. */
c906108c
SS
1052 switch (pending_follow.kind)
1053 {
6604731b
DJ
1054 case TARGET_WAITKIND_FORKED:
1055 case TARGET_WAITKIND_VFORKED:
c906108c 1056 pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
6604731b
DJ
1057 if (follow_fork ())
1058 should_resume = 0;
607cecd2
PA
1059
1060 /* Following a child fork will change our notion of current
1061 thread. */
1062 tp = inferior_thread ();
c7e8a53c
PA
1063 regcache = get_current_regcache ();
1064 gdbarch = get_regcache_arch (regcache);
1065 pc = regcache_read_pc (regcache);
c906108c
SS
1066 break;
1067
6604731b 1068 case TARGET_WAITKIND_EXECD:
c906108c 1069 /* follow_exec is called as soon as the exec event is seen. */
6604731b 1070 pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
c906108c
SS
1071 break;
1072
1073 default:
1074 break;
1075 }
c906108c
SS
1076
1077 /* Install inferior's terminal modes. */
1078 target_terminal_inferior ();
1079
1080 if (should_resume)
1081 {
39f77062 1082 ptid_t resume_ptid;
dfcd3bfb 1083
488f131b 1084 resume_ptid = RESUME_ALL; /* Default */
ef5cf84e 1085
cd76b0b7
VP
1086 /* If STEP is set, it's a request to use hardware stepping
1087 facilities. But in that case, we should never
1088 use singlestep breakpoint. */
1089 gdb_assert (!(singlestep_breakpoints_inserted_p && step));
1090
1091 if (singlestep_breakpoints_inserted_p
1092 && stepping_past_singlestep_breakpoint)
c906108c 1093 {
cd76b0b7
VP
1094 /* The situation here is as follows. In thread T1 we wanted to
1095 single-step. Lacking hardware single-stepping we've
1096 set breakpoint at the PC of the next instruction -- call it
1097 P. After resuming, we've hit that breakpoint in thread T2.
1098 Now we've removed original breakpoint, inserted breakpoint
1099 at P+1, and try to step to advance T2 past breakpoint.
1100 We need to step only T2, as if T1 is allowed to freely run,
1101 it can run past P, and if other threads are allowed to run,
1102 they can hit breakpoint at P+1, and nested hits of single-step
1103 breakpoints is not something we'd want -- that's complicated
1104 to support, and has no value. */
1105 resume_ptid = inferior_ptid;
1106 }
c906108c 1107
e842223a 1108 if ((step || singlestep_breakpoints_inserted_p)
4e1c45ea 1109 && tp->trap_expected)
cd76b0b7 1110 {
74960c60
VP
1111 /* We're allowing a thread to run past a breakpoint it has
1112 hit, by single-stepping the thread with the breakpoint
1113 removed. In which case, we need to single-step only this
1114 thread, and keep others stopped, as they can miss this
1115 breakpoint if allowed to run.
1116
1117 The current code actually removes all breakpoints when
1118 doing this, not just the one being stepped over, so if we
1119 let other threads run, we can actually miss any
1120 breakpoint, not just the one at PC. */
ef5cf84e 1121 resume_ptid = inferior_ptid;
c906108c 1122 }
ef5cf84e 1123
94cc34af
PA
1124 if (non_stop)
1125 {
1126 /* With non-stop mode on, threads are always handled
1127 individually. */
1128 resume_ptid = inferior_ptid;
1129 }
1130 else if ((scheduler_mode == schedlock_on)
1131 || (scheduler_mode == schedlock_step
1132 && (step || singlestep_breakpoints_inserted_p)))
c906108c 1133 {
ef5cf84e 1134 /* User-settable 'scheduler' mode requires solo thread resume. */
488f131b 1135 resume_ptid = inferior_ptid;
c906108c 1136 }
ef5cf84e 1137
515630c5 1138 if (gdbarch_cannot_step_breakpoint (gdbarch))
c4ed33b9
AC
1139 {
1140 /* Most targets can step a breakpoint instruction, thus
1141 executing it normally. But if this one cannot, just
1142 continue and we will hit it anyway. */
237fc4c9 1143 if (step && breakpoint_inserted_here_p (pc))
c4ed33b9
AC
1144 step = 0;
1145 }
237fc4c9
PA
1146
1147 if (debug_displaced
515630c5 1148 && use_displaced_stepping (gdbarch)
4e1c45ea 1149 && tp->trap_expected)
237fc4c9 1150 {
515630c5
UW
1151 struct regcache *resume_regcache = get_thread_regcache (resume_ptid);
1152 CORE_ADDR actual_pc = regcache_read_pc (resume_regcache);
237fc4c9
PA
1153 gdb_byte buf[4];
1154
1155 fprintf_unfiltered (gdb_stdlog, "displaced: run 0x%s: ",
1156 paddr_nz (actual_pc));
1157 read_memory (actual_pc, buf, sizeof (buf));
1158 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1159 }
1160
2020b7ab
PA
1161 /* Avoid confusing the next resume, if the next stop/resume
1162 happens to apply to another thread. */
1163 tp->stop_signal = TARGET_SIGNAL_0;
607cecd2
PA
1164
1165 target_resume (resume_ptid, step, sig);
c906108c
SS
1166 }
1167
1168 discard_cleanups (old_cleanups);
1169}
1170\f
237fc4c9 1171/* Proceeding. */
c906108c
SS
1172
1173/* Clear out all variables saying what to do when inferior is continued.
1174 First do this, then set the ones you want, then call `proceed'. */
1175
a7212384
UW
1176static void
1177clear_proceed_status_thread (struct thread_info *tp)
c906108c 1178{
a7212384
UW
1179 if (debug_infrun)
1180 fprintf_unfiltered (gdb_stdlog,
1181 "infrun: clear_proceed_status_thread (%s)\n",
1182 target_pid_to_str (tp->ptid));
d6b48e9c 1183
a7212384
UW
1184 tp->trap_expected = 0;
1185 tp->step_range_start = 0;
1186 tp->step_range_end = 0;
1187 tp->step_frame_id = null_frame_id;
1188 tp->step_over_calls = STEP_OVER_UNDEBUGGABLE;
1189 tp->stop_requested = 0;
4e1c45ea 1190
a7212384 1191 tp->stop_step = 0;
32400beb 1192
a7212384 1193 tp->proceed_to_finish = 0;
414c69f7 1194
a7212384
UW
1195 /* Discard any remaining commands or status from previous stop. */
1196 bpstat_clear (&tp->stop_bpstat);
1197}
32400beb 1198
a7212384
UW
1199static int
1200clear_proceed_status_callback (struct thread_info *tp, void *data)
1201{
1202 if (is_exited (tp->ptid))
1203 return 0;
d6b48e9c 1204
a7212384
UW
1205 clear_proceed_status_thread (tp);
1206 return 0;
1207}
1208
1209void
1210clear_proceed_status (void)
1211{
1212 if (!ptid_equal (inferior_ptid, null_ptid))
1213 {
1214 struct inferior *inferior;
1215
1216 if (non_stop)
1217 {
1218 /* If in non-stop mode, only delete the per-thread status
1219 of the current thread. */
1220 clear_proceed_status_thread (inferior_thread ());
1221 }
1222 else
1223 {
1224 /* In all-stop mode, delete the per-thread status of
1225 *all* threads. */
1226 iterate_over_threads (clear_proceed_status_callback, NULL);
1227 }
1228
d6b48e9c
PA
1229 inferior = current_inferior ();
1230 inferior->stop_soon = NO_STOP_QUIETLY;
4e1c45ea
PA
1231 }
1232
c906108c 1233 stop_after_trap = 0;
c906108c
SS
1234 breakpoint_proceeded = 1; /* We're about to proceed... */
1235
d5c31457
UW
1236 if (stop_registers)
1237 {
1238 regcache_xfree (stop_registers);
1239 stop_registers = NULL;
1240 }
c906108c
SS
1241}
1242
ea67f13b
DJ
1243/* This should be suitable for any targets that support threads. */
1244
1245static int
6a6b96b9 1246prepare_to_proceed (int step)
ea67f13b
DJ
1247{
1248 ptid_t wait_ptid;
1249 struct target_waitstatus wait_status;
1250
1251 /* Get the last target status returned by target_wait(). */
1252 get_last_target_status (&wait_ptid, &wait_status);
1253
6a6b96b9 1254 /* Make sure we were stopped at a breakpoint. */
ea67f13b 1255 if (wait_status.kind != TARGET_WAITKIND_STOPPED
6a6b96b9 1256 || wait_status.value.sig != TARGET_SIGNAL_TRAP)
ea67f13b
DJ
1257 {
1258 return 0;
1259 }
1260
6a6b96b9 1261 /* Switched over from WAIT_PID. */
ea67f13b 1262 if (!ptid_equal (wait_ptid, minus_one_ptid)
515630c5 1263 && !ptid_equal (inferior_ptid, wait_ptid))
ea67f13b 1264 {
515630c5
UW
1265 struct regcache *regcache = get_thread_regcache (wait_ptid);
1266
1267 if (breakpoint_here_p (regcache_read_pc (regcache)))
ea67f13b 1268 {
515630c5
UW
1269 /* If stepping, remember current thread to switch back to. */
1270 if (step)
1271 deferred_step_ptid = inferior_ptid;
ea67f13b 1272
515630c5
UW
1273 /* Switch back to WAIT_PID thread. */
1274 switch_to_thread (wait_ptid);
6a6b96b9 1275
515630c5
UW
1276 /* We return 1 to indicate that there is a breakpoint here,
1277 so we need to step over it before continuing to avoid
1278 hitting it straight away. */
1279 return 1;
1280 }
ea67f13b
DJ
1281 }
1282
1283 return 0;
ea67f13b 1284}
e4846b08 1285
c906108c
SS
1286/* Basic routine for continuing the program in various fashions.
1287
1288 ADDR is the address to resume at, or -1 for resume where stopped.
1289 SIGGNAL is the signal to give it, or 0 for none,
c5aa993b 1290 or -1 for act according to how it stopped.
c906108c 1291 STEP is nonzero if should trap after one instruction.
c5aa993b
JM
1292 -1 means return after that and print nothing.
1293 You should probably set various step_... variables
1294 before calling here, if you are stepping.
c906108c
SS
1295
1296 You should call clear_proceed_status before calling proceed. */
1297
1298void
96baa820 1299proceed (CORE_ADDR addr, enum target_signal siggnal, int step)
c906108c 1300{
515630c5
UW
1301 struct regcache *regcache = get_current_regcache ();
1302 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4e1c45ea 1303 struct thread_info *tp;
515630c5 1304 CORE_ADDR pc = regcache_read_pc (regcache);
c906108c 1305 int oneproc = 0;
2020b7ab 1306 enum target_signal stop_signal;
c906108c
SS
1307
1308 if (step > 0)
515630c5 1309 step_start_function = find_pc_function (pc);
c906108c
SS
1310 if (step < 0)
1311 stop_after_trap = 1;
1312
2acceee2 1313 if (addr == (CORE_ADDR) -1)
c906108c 1314 {
b2175913
MS
1315 if (pc == stop_pc && breakpoint_here_p (pc)
1316 && execution_direction != EXEC_REVERSE)
3352ef37
AC
1317 /* There is a breakpoint at the address we will resume at,
1318 step one instruction before inserting breakpoints so that
1319 we do not stop right away (and report a second hit at this
b2175913
MS
1320 breakpoint).
1321
1322 Note, we don't do this in reverse, because we won't
1323 actually be executing the breakpoint insn anyway.
1324 We'll be (un-)executing the previous instruction. */
1325
c906108c 1326 oneproc = 1;
515630c5
UW
1327 else if (gdbarch_single_step_through_delay_p (gdbarch)
1328 && gdbarch_single_step_through_delay (gdbarch,
1329 get_current_frame ()))
3352ef37
AC
1330 /* We stepped onto an instruction that needs to be stepped
1331 again before re-inserting the breakpoint, do so. */
c906108c
SS
1332 oneproc = 1;
1333 }
1334 else
1335 {
515630c5 1336 regcache_write_pc (regcache, addr);
c906108c
SS
1337 }
1338
527159b7 1339 if (debug_infrun)
8a9de0e4
AC
1340 fprintf_unfiltered (gdb_stdlog,
1341 "infrun: proceed (addr=0x%s, signal=%d, step=%d)\n",
1342 paddr_nz (addr), siggnal, step);
527159b7 1343
94cc34af
PA
1344 if (non_stop)
1345 /* In non-stop, each thread is handled individually. The context
1346 must already be set to the right thread here. */
1347 ;
1348 else
1349 {
1350 /* In a multi-threaded task we may select another thread and
1351 then continue or step.
c906108c 1352
94cc34af
PA
1353 But if the old thread was stopped at a breakpoint, it will
1354 immediately cause another breakpoint stop without any
1355 execution (i.e. it will report a breakpoint hit incorrectly).
1356 So we must step over it first.
c906108c 1357
94cc34af
PA
1358 prepare_to_proceed checks the current thread against the
1359 thread that reported the most recent event. If a step-over
1360 is required it returns TRUE and sets the current thread to
1361 the old thread. */
1362 if (prepare_to_proceed (step))
1363 oneproc = 1;
1364 }
c906108c 1365
4e1c45ea
PA
1366 /* prepare_to_proceed may change the current thread. */
1367 tp = inferior_thread ();
1368
c906108c 1369 if (oneproc)
74960c60 1370 {
4e1c45ea 1371 tp->trap_expected = 1;
237fc4c9
PA
1372 /* If displaced stepping is enabled, we can step over the
1373 breakpoint without hitting it, so leave all breakpoints
1374 inserted. Otherwise we need to disable all breakpoints, step
1375 one instruction, and then re-add them when that step is
1376 finished. */
515630c5 1377 if (!use_displaced_stepping (gdbarch))
237fc4c9 1378 remove_breakpoints ();
74960c60 1379 }
237fc4c9
PA
1380
1381 /* We can insert breakpoints if we're not trying to step over one,
1382 or if we are stepping over one but we're using displaced stepping
1383 to do so. */
4e1c45ea 1384 if (! tp->trap_expected || use_displaced_stepping (gdbarch))
c36b740a 1385 insert_breakpoints ();
c906108c 1386
2020b7ab
PA
1387 if (!non_stop)
1388 {
1389 /* Pass the last stop signal to the thread we're resuming,
1390 irrespective of whether the current thread is the thread that
1391 got the last event or not. This was historically GDB's
1392 behaviour before keeping a stop_signal per thread. */
1393
1394 struct thread_info *last_thread;
1395 ptid_t last_ptid;
1396 struct target_waitstatus last_status;
1397
1398 get_last_target_status (&last_ptid, &last_status);
1399 if (!ptid_equal (inferior_ptid, last_ptid)
1400 && !ptid_equal (last_ptid, null_ptid)
1401 && !ptid_equal (last_ptid, minus_one_ptid))
1402 {
1403 last_thread = find_thread_pid (last_ptid);
1404 if (last_thread)
1405 {
1406 tp->stop_signal = last_thread->stop_signal;
1407 last_thread->stop_signal = TARGET_SIGNAL_0;
1408 }
1409 }
1410 }
1411
c906108c 1412 if (siggnal != TARGET_SIGNAL_DEFAULT)
2020b7ab 1413 tp->stop_signal = siggnal;
c906108c
SS
1414 /* If this signal should not be seen by program,
1415 give it zero. Used for debugging signals. */
2020b7ab
PA
1416 else if (!signal_program[tp->stop_signal])
1417 tp->stop_signal = TARGET_SIGNAL_0;
c906108c
SS
1418
1419 annotate_starting ();
1420
1421 /* Make sure that output from GDB appears before output from the
1422 inferior. */
1423 gdb_flush (gdb_stdout);
1424
e4846b08
JJ
1425 /* Refresh prev_pc value just prior to resuming. This used to be
1426 done in stop_stepping, however, setting prev_pc there did not handle
1427 scenarios such as inferior function calls or returning from
1428 a function via the return command. In those cases, the prev_pc
1429 value was not set properly for subsequent commands. The prev_pc value
1430 is used to initialize the starting line number in the ecs. With an
1431 invalid value, the gdb next command ends up stopping at the position
1432 represented by the next line table entry past our start position.
1433 On platforms that generate one line table entry per line, this
1434 is not a problem. However, on the ia64, the compiler generates
1435 extraneous line table entries that do not increase the line number.
1436 When we issue the gdb next command on the ia64 after an inferior call
1437 or a return command, we often end up a few instructions forward, still
1438 within the original line we started.
1439
1440 An attempt was made to have init_execution_control_state () refresh
1441 the prev_pc value before calculating the line number. This approach
1442 did not work because on platforms that use ptrace, the pc register
1443 cannot be read unless the inferior is stopped. At that point, we
515630c5 1444 are not guaranteed the inferior is stopped and so the regcache_read_pc ()
e4846b08 1445 call can fail. Setting the prev_pc value here ensures the value is
8fb3e588 1446 updated correctly when the inferior is stopped. */
4e1c45ea 1447 tp->prev_pc = regcache_read_pc (get_current_regcache ());
e4846b08 1448
59f0d5d9 1449 /* Fill in with reasonable starting values. */
4e1c45ea 1450 init_thread_stepping_state (tp);
59f0d5d9 1451
59f0d5d9
PA
1452 /* Reset to normal state. */
1453 init_infwait_state ();
1454
c906108c 1455 /* Resume inferior. */
2020b7ab 1456 resume (oneproc || step || bpstat_should_step (), tp->stop_signal);
c906108c
SS
1457
1458 /* Wait for it to stop (if not standalone)
1459 and in any case decode why it stopped, and act accordingly. */
43ff13b4
JM
1460 /* Do this only if we are not using the event loop, or if the target
1461 does not support asynchronous execution. */
362646f5 1462 if (!target_can_async_p ())
43ff13b4 1463 {
ae123ec6 1464 wait_for_inferior (0);
43ff13b4
JM
1465 normal_stop ();
1466 }
c906108c 1467}
c906108c
SS
1468\f
1469
1470/* Start remote-debugging of a machine over a serial link. */
96baa820 1471
c906108c 1472void
8621d6a9 1473start_remote (int from_tty)
c906108c 1474{
d6b48e9c 1475 struct inferior *inferior;
c906108c 1476 init_wait_for_inferior ();
d6b48e9c
PA
1477
1478 inferior = current_inferior ();
1479 inferior->stop_soon = STOP_QUIETLY_REMOTE;
43ff13b4 1480
6426a772
JM
1481 /* Always go on waiting for the target, regardless of the mode. */
1482 /* FIXME: cagney/1999-09-23: At present it isn't possible to
7e73cedf 1483 indicate to wait_for_inferior that a target should timeout if
6426a772
JM
1484 nothing is returned (instead of just blocking). Because of this,
1485 targets expecting an immediate response need to, internally, set
1486 things up so that the target_wait() is forced to eventually
1487 timeout. */
1488 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
1489 differentiate to its caller what the state of the target is after
1490 the initial open has been performed. Here we're assuming that
1491 the target has stopped. It should be possible to eventually have
1492 target_open() return to the caller an indication that the target
1493 is currently running and GDB state should be set to the same as
1494 for an async run. */
ae123ec6 1495 wait_for_inferior (0);
8621d6a9
DJ
1496
1497 /* Now that the inferior has stopped, do any bookkeeping like
1498 loading shared libraries. We want to do this before normal_stop,
1499 so that the displayed frame is up to date. */
1500 post_create_inferior (&current_target, from_tty);
1501
6426a772 1502 normal_stop ();
c906108c
SS
1503}
1504
1505/* Initialize static vars when a new inferior begins. */
1506
1507void
96baa820 1508init_wait_for_inferior (void)
c906108c
SS
1509{
1510 /* These are meaningless until the first time through wait_for_inferior. */
c906108c 1511
c906108c
SS
1512 breakpoint_init_inferior (inf_starting);
1513
c906108c
SS
1514 /* The first resume is not following a fork/vfork/exec. */
1515 pending_follow.kind = TARGET_WAITKIND_SPURIOUS; /* I.e., none. */
c906108c 1516
c906108c 1517 clear_proceed_status ();
9f976b41
DJ
1518
1519 stepping_past_singlestep_breakpoint = 0;
ca67fcb8 1520 deferred_step_ptid = null_ptid;
ca005067
DJ
1521
1522 target_last_wait_ptid = minus_one_ptid;
237fc4c9 1523
0d1e5fa7
PA
1524 previous_inferior_ptid = null_ptid;
1525 init_infwait_state ();
1526
237fc4c9 1527 displaced_step_clear ();
c906108c 1528}
237fc4c9 1529
c906108c 1530\f
b83266a0
SS
1531/* This enum encodes possible reasons for doing a target_wait, so that
1532 wfi can call target_wait in one place. (Ultimately the call will be
1533 moved out of the infinite loop entirely.) */
1534
c5aa993b
JM
1535enum infwait_states
1536{
cd0fc7c3
SS
1537 infwait_normal_state,
1538 infwait_thread_hop_state,
d983da9c 1539 infwait_step_watch_state,
cd0fc7c3 1540 infwait_nonstep_watch_state
b83266a0
SS
1541};
1542
11cf8741
JM
1543/* Why did the inferior stop? Used to print the appropriate messages
1544 to the interface from within handle_inferior_event(). */
1545enum inferior_stop_reason
1546{
11cf8741
JM
1547 /* Step, next, nexti, stepi finished. */
1548 END_STEPPING_RANGE,
11cf8741
JM
1549 /* Inferior terminated by signal. */
1550 SIGNAL_EXITED,
1551 /* Inferior exited. */
1552 EXITED,
1553 /* Inferior received signal, and user asked to be notified. */
b2175913
MS
1554 SIGNAL_RECEIVED,
1555 /* Reverse execution -- target ran out of history info. */
1556 NO_HISTORY
11cf8741
JM
1557};
1558
0d1e5fa7
PA
1559/* The PTID we'll do a target_wait on.*/
1560ptid_t waiton_ptid;
1561
1562/* Current inferior wait state. */
1563enum infwait_states infwait_state;
cd0fc7c3 1564
0d1e5fa7
PA
1565/* Data to be passed around while handling an event. This data is
1566 discarded between events. */
c5aa993b 1567struct execution_control_state
488f131b 1568{
0d1e5fa7 1569 ptid_t ptid;
4e1c45ea
PA
1570 /* The thread that got the event, if this was a thread event; NULL
1571 otherwise. */
1572 struct thread_info *event_thread;
1573
488f131b 1574 struct target_waitstatus ws;
488f131b
JB
1575 int random_signal;
1576 CORE_ADDR stop_func_start;
1577 CORE_ADDR stop_func_end;
1578 char *stop_func_name;
488f131b 1579 int new_thread_event;
488f131b
JB
1580 int wait_some_more;
1581};
1582
1583void init_execution_control_state (struct execution_control_state *ecs);
1584
1585void handle_inferior_event (struct execution_control_state *ecs);
cd0fc7c3 1586
b2175913
MS
1587static void handle_step_into_function (struct execution_control_state *ecs);
1588static void handle_step_into_function_backward (struct execution_control_state *ecs);
44cbf7b5 1589static void insert_step_resume_breakpoint_at_frame (struct frame_info *step_frame);
14e60db5 1590static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
44cbf7b5
AC
1591static void insert_step_resume_breakpoint_at_sal (struct symtab_and_line sr_sal,
1592 struct frame_id sr_id);
611c83ae
PA
1593static void insert_longjmp_resume_breakpoint (CORE_ADDR);
1594
104c1213
JM
1595static void stop_stepping (struct execution_control_state *ecs);
1596static void prepare_to_wait (struct execution_control_state *ecs);
d4f3574e 1597static void keep_going (struct execution_control_state *ecs);
488f131b
JB
1598static void print_stop_reason (enum inferior_stop_reason stop_reason,
1599 int stop_info);
104c1213 1600
252fbfc8
PA
1601/* Callback for iterate over threads. If the thread is stopped, but
1602 the user/frontend doesn't know about that yet, go through
1603 normal_stop, as if the thread had just stopped now. ARG points at
1604 a ptid. If PTID is MINUS_ONE_PTID, applies to all threads. If
1605 ptid_is_pid(PTID) is true, applies to all threads of the process
1606 pointed at by PTID. Otherwise, apply only to the thread pointed by
1607 PTID. */
1608
1609static int
1610infrun_thread_stop_requested_callback (struct thread_info *info, void *arg)
1611{
1612 ptid_t ptid = * (ptid_t *) arg;
1613
1614 if ((ptid_equal (info->ptid, ptid)
1615 || ptid_equal (minus_one_ptid, ptid)
1616 || (ptid_is_pid (ptid)
1617 && ptid_get_pid (ptid) == ptid_get_pid (info->ptid)))
1618 && is_running (info->ptid)
1619 && !is_executing (info->ptid))
1620 {
1621 struct cleanup *old_chain;
1622 struct execution_control_state ecss;
1623 struct execution_control_state *ecs = &ecss;
1624
1625 memset (ecs, 0, sizeof (*ecs));
1626
1627 old_chain = make_cleanup_restore_current_thread ();
1628
1629 switch_to_thread (info->ptid);
1630
1631 /* Go through handle_inferior_event/normal_stop, so we always
1632 have consistent output as if the stop event had been
1633 reported. */
1634 ecs->ptid = info->ptid;
1635 ecs->event_thread = find_thread_pid (info->ptid);
1636 ecs->ws.kind = TARGET_WAITKIND_STOPPED;
1637 ecs->ws.value.sig = TARGET_SIGNAL_0;
1638
1639 handle_inferior_event (ecs);
1640
1641 if (!ecs->wait_some_more)
1642 {
1643 struct thread_info *tp;
1644
1645 normal_stop ();
1646
1647 /* Finish off the continuations. The continations
1648 themselves are responsible for realising the thread
1649 didn't finish what it was supposed to do. */
1650 tp = inferior_thread ();
1651 do_all_intermediate_continuations_thread (tp);
1652 do_all_continuations_thread (tp);
1653 }
1654
1655 do_cleanups (old_chain);
1656 }
1657
1658 return 0;
1659}
1660
1661/* This function is attached as a "thread_stop_requested" observer.
1662 Cleanup local state that assumed the PTID was to be resumed, and
1663 report the stop to the frontend. */
1664
1665void
1666infrun_thread_stop_requested (ptid_t ptid)
1667{
1668 struct displaced_step_request *it, *next, *prev = NULL;
1669
1670 /* PTID was requested to stop. Remove it from the displaced
1671 stepping queue, so we don't try to resume it automatically. */
1672 for (it = displaced_step_request_queue; it; it = next)
1673 {
1674 next = it->next;
1675
1676 if (ptid_equal (it->ptid, ptid)
1677 || ptid_equal (minus_one_ptid, ptid)
1678 || (ptid_is_pid (ptid)
1679 && ptid_get_pid (ptid) == ptid_get_pid (it->ptid)))
1680 {
1681 if (displaced_step_request_queue == it)
1682 displaced_step_request_queue = it->next;
1683 else
1684 prev->next = it->next;
1685
1686 xfree (it);
1687 }
1688 else
1689 prev = it;
1690 }
1691
1692 iterate_over_threads (infrun_thread_stop_requested_callback, &ptid);
1693}
1694
4e1c45ea
PA
1695/* Callback for iterate_over_threads. */
1696
1697static int
1698delete_step_resume_breakpoint_callback (struct thread_info *info, void *data)
1699{
1700 if (is_exited (info->ptid))
1701 return 0;
1702
1703 delete_step_resume_breakpoint (info);
1704 return 0;
1705}
1706
1707/* In all-stop, delete the step resume breakpoint of any thread that
1708 had one. In non-stop, delete the step resume breakpoint of the
1709 thread that just stopped. */
1710
1711static void
1712delete_step_thread_step_resume_breakpoint (void)
1713{
1714 if (!target_has_execution
1715 || ptid_equal (inferior_ptid, null_ptid))
1716 /* If the inferior has exited, we have already deleted the step
1717 resume breakpoints out of GDB's lists. */
1718 return;
1719
1720 if (non_stop)
1721 {
1722 /* If in non-stop mode, only delete the step-resume or
1723 longjmp-resume breakpoint of the thread that just stopped
1724 stepping. */
1725 struct thread_info *tp = inferior_thread ();
1726 delete_step_resume_breakpoint (tp);
1727 }
1728 else
1729 /* In all-stop mode, delete all step-resume and longjmp-resume
1730 breakpoints of any thread that had them. */
1731 iterate_over_threads (delete_step_resume_breakpoint_callback, NULL);
1732}
1733
1734/* A cleanup wrapper. */
1735
1736static void
1737delete_step_thread_step_resume_breakpoint_cleanup (void *arg)
1738{
1739 delete_step_thread_step_resume_breakpoint ();
1740}
1741
cd0fc7c3 1742/* Wait for control to return from inferior to debugger.
ae123ec6
JB
1743
1744 If TREAT_EXEC_AS_SIGTRAP is non-zero, then handle EXEC signals
1745 as if they were SIGTRAP signals. This can be useful during
1746 the startup sequence on some targets such as HP/UX, where
1747 we receive an EXEC event instead of the expected SIGTRAP.
1748
cd0fc7c3
SS
1749 If inferior gets a signal, we may decide to start it up again
1750 instead of returning. That is why there is a loop in this function.
1751 When this function actually returns it means the inferior
1752 should be left stopped and GDB should read more commands. */
1753
1754void
ae123ec6 1755wait_for_inferior (int treat_exec_as_sigtrap)
cd0fc7c3
SS
1756{
1757 struct cleanup *old_cleanups;
0d1e5fa7 1758 struct execution_control_state ecss;
cd0fc7c3 1759 struct execution_control_state *ecs;
c906108c 1760
527159b7 1761 if (debug_infrun)
ae123ec6
JB
1762 fprintf_unfiltered
1763 (gdb_stdlog, "infrun: wait_for_inferior (treat_exec_as_sigtrap=%d)\n",
1764 treat_exec_as_sigtrap);
527159b7 1765
4e1c45ea
PA
1766 old_cleanups =
1767 make_cleanup (delete_step_thread_step_resume_breakpoint_cleanup, NULL);
cd0fc7c3 1768
cd0fc7c3 1769 ecs = &ecss;
0d1e5fa7
PA
1770 memset (ecs, 0, sizeof (*ecs));
1771
cd0fc7c3
SS
1772 overlay_cache_invalid = 1;
1773
e0bb1c1c
PA
1774 /* We'll update this if & when we switch to a new thread. */
1775 previous_inferior_ptid = inferior_ptid;
1776
cd0fc7c3
SS
1777 /* We have to invalidate the registers BEFORE calling target_wait
1778 because they can be loaded from the target while in target_wait.
1779 This makes remote debugging a bit more efficient for those
1780 targets that provide critical registers as part of their normal
1781 status mechanism. */
1782
1783 registers_changed ();
b83266a0 1784
c906108c
SS
1785 while (1)
1786 {
9a4105ab 1787 if (deprecated_target_wait_hook)
0d1e5fa7 1788 ecs->ptid = deprecated_target_wait_hook (waiton_ptid, &ecs->ws);
cd0fc7c3 1789 else
0d1e5fa7 1790 ecs->ptid = target_wait (waiton_ptid, &ecs->ws);
c906108c 1791
ae123ec6
JB
1792 if (treat_exec_as_sigtrap && ecs->ws.kind == TARGET_WAITKIND_EXECD)
1793 {
1794 xfree (ecs->ws.value.execd_pathname);
1795 ecs->ws.kind = TARGET_WAITKIND_STOPPED;
1796 ecs->ws.value.sig = TARGET_SIGNAL_TRAP;
1797 }
1798
cd0fc7c3
SS
1799 /* Now figure out what to do with the result of the result. */
1800 handle_inferior_event (ecs);
c906108c 1801
cd0fc7c3
SS
1802 if (!ecs->wait_some_more)
1803 break;
1804 }
4e1c45ea 1805
cd0fc7c3
SS
1806 do_cleanups (old_cleanups);
1807}
c906108c 1808
43ff13b4
JM
1809/* Asynchronous version of wait_for_inferior. It is called by the
1810 event loop whenever a change of state is detected on the file
1811 descriptor corresponding to the target. It can be called more than
1812 once to complete a single execution command. In such cases we need
a474d7c2
PA
1813 to keep the state in a global variable ECSS. If it is the last time
1814 that this function is called for a single execution command, then
1815 report to the user that the inferior has stopped, and do the
1816 necessary cleanups. */
43ff13b4
JM
1817
1818void
fba45db2 1819fetch_inferior_event (void *client_data)
43ff13b4 1820{
0d1e5fa7 1821 struct execution_control_state ecss;
a474d7c2 1822 struct execution_control_state *ecs = &ecss;
4f8d22e3
PA
1823 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
1824 int was_sync = sync_execution;
43ff13b4 1825
0d1e5fa7
PA
1826 memset (ecs, 0, sizeof (*ecs));
1827
59f0d5d9 1828 overlay_cache_invalid = 1;
43ff13b4 1829
e0bb1c1c
PA
1830 /* We can only rely on wait_for_more being correct before handling
1831 the event in all-stop, but previous_inferior_ptid isn't used in
1832 non-stop. */
1833 if (!ecs->wait_some_more)
1834 /* We'll update this if & when we switch to a new thread. */
1835 previous_inferior_ptid = inferior_ptid;
1836
4f8d22e3
PA
1837 if (non_stop)
1838 /* In non-stop mode, the user/frontend should not notice a thread
1839 switch due to internal events. Make sure we reverse to the
1840 user selected thread and frame after handling the event and
1841 running any breakpoint commands. */
1842 make_cleanup_restore_current_thread ();
1843
59f0d5d9
PA
1844 /* We have to invalidate the registers BEFORE calling target_wait
1845 because they can be loaded from the target while in target_wait.
1846 This makes remote debugging a bit more efficient for those
1847 targets that provide critical registers as part of their normal
1848 status mechanism. */
43ff13b4 1849
59f0d5d9 1850 registers_changed ();
43ff13b4 1851
9a4105ab 1852 if (deprecated_target_wait_hook)
a474d7c2 1853 ecs->ptid =
0d1e5fa7 1854 deprecated_target_wait_hook (waiton_ptid, &ecs->ws);
43ff13b4 1855 else
0d1e5fa7 1856 ecs->ptid = target_wait (waiton_ptid, &ecs->ws);
43ff13b4 1857
94cc34af
PA
1858 if (non_stop
1859 && ecs->ws.kind != TARGET_WAITKIND_IGNORE
1860 && ecs->ws.kind != TARGET_WAITKIND_EXITED
1861 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
1862 /* In non-stop mode, each thread is handled individually. Switch
1863 early, so the global state is set correctly for this
1864 thread. */
1865 context_switch (ecs->ptid);
1866
43ff13b4 1867 /* Now figure out what to do with the result of the result. */
a474d7c2 1868 handle_inferior_event (ecs);
43ff13b4 1869
a474d7c2 1870 if (!ecs->wait_some_more)
43ff13b4 1871 {
d6b48e9c
PA
1872 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
1873
4e1c45ea 1874 delete_step_thread_step_resume_breakpoint ();
f107f563 1875
d6b48e9c
PA
1876 /* We may not find an inferior if this was a process exit. */
1877 if (inf == NULL || inf->stop_soon == NO_STOP_QUIETLY)
83c265ab
PA
1878 normal_stop ();
1879
af679fd0
PA
1880 if (target_has_execution
1881 && ecs->ws.kind != TARGET_WAITKIND_EXITED
1882 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
1883 && ecs->event_thread->step_multi
414c69f7 1884 && ecs->event_thread->stop_step)
c2d11a7d
JM
1885 inferior_event_handler (INF_EXEC_CONTINUE, NULL);
1886 else
1887 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
43ff13b4 1888 }
4f8d22e3
PA
1889
1890 /* Revert thread and frame. */
1891 do_cleanups (old_chain);
1892
1893 /* If the inferior was in sync execution mode, and now isn't,
1894 restore the prompt. */
1895 if (was_sync && !sync_execution)
1896 display_gdb_prompt (0);
43ff13b4
JM
1897}
1898
cd0fc7c3
SS
1899/* Prepare an execution control state for looping through a
1900 wait_for_inferior-type loop. */
1901
1902void
96baa820 1903init_execution_control_state (struct execution_control_state *ecs)
cd0fc7c3
SS
1904{
1905 ecs->random_signal = 0;
0d1e5fa7
PA
1906}
1907
1908/* Clear context switchable stepping state. */
1909
1910void
4e1c45ea 1911init_thread_stepping_state (struct thread_info *tss)
0d1e5fa7 1912{
2afb61aa
PA
1913 struct symtab_and_line sal;
1914
0d1e5fa7
PA
1915 tss->stepping_over_breakpoint = 0;
1916 tss->step_after_step_resume_breakpoint = 0;
1917 tss->stepping_through_solib_after_catch = 0;
1918 tss->stepping_through_solib_catchpoints = NULL;
2afb61aa 1919
4e1c45ea 1920 sal = find_pc_line (tss->prev_pc, 0);
2afb61aa
PA
1921 tss->current_line = sal.line;
1922 tss->current_symtab = sal.symtab;
cd0fc7c3
SS
1923}
1924
e02bc4cc 1925/* Return the cached copy of the last pid/waitstatus returned by
9a4105ab
AC
1926 target_wait()/deprecated_target_wait_hook(). The data is actually
1927 cached by handle_inferior_event(), which gets called immediately
1928 after target_wait()/deprecated_target_wait_hook(). */
e02bc4cc
DS
1929
1930void
488f131b 1931get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
e02bc4cc 1932{
39f77062 1933 *ptidp = target_last_wait_ptid;
e02bc4cc
DS
1934 *status = target_last_waitstatus;
1935}
1936
ac264b3b
MS
1937void
1938nullify_last_target_wait_ptid (void)
1939{
1940 target_last_wait_ptid = minus_one_ptid;
1941}
1942
dcf4fbde 1943/* Switch thread contexts. */
dd80620e
MS
1944
1945static void
0d1e5fa7 1946context_switch (ptid_t ptid)
dd80620e 1947{
fd48f117
DJ
1948 if (debug_infrun)
1949 {
1950 fprintf_unfiltered (gdb_stdlog, "infrun: Switching context from %s ",
1951 target_pid_to_str (inferior_ptid));
1952 fprintf_unfiltered (gdb_stdlog, "to %s\n",
0d1e5fa7 1953 target_pid_to_str (ptid));
fd48f117
DJ
1954 }
1955
0d1e5fa7 1956 switch_to_thread (ptid);
dd80620e
MS
1957}
1958
4fa8626c
DJ
1959static void
1960adjust_pc_after_break (struct execution_control_state *ecs)
1961{
24a73cce
UW
1962 struct regcache *regcache;
1963 struct gdbarch *gdbarch;
8aad930b 1964 CORE_ADDR breakpoint_pc;
4fa8626c 1965
4fa8626c
DJ
1966 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
1967 we aren't, just return.
9709f61c
DJ
1968
1969 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
b798847d
UW
1970 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
1971 implemented by software breakpoints should be handled through the normal
1972 breakpoint layer.
8fb3e588 1973
4fa8626c
DJ
1974 NOTE drow/2004-01-31: On some targets, breakpoints may generate
1975 different signals (SIGILL or SIGEMT for instance), but it is less
1976 clear where the PC is pointing afterwards. It may not match
b798847d
UW
1977 gdbarch_decr_pc_after_break. I don't know any specific target that
1978 generates these signals at breakpoints (the code has been in GDB since at
1979 least 1992) so I can not guess how to handle them here.
8fb3e588 1980
e6cf7916
UW
1981 In earlier versions of GDB, a target with
1982 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
b798847d
UW
1983 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
1984 target with both of these set in GDB history, and it seems unlikely to be
1985 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
4fa8626c
DJ
1986
1987 if (ecs->ws.kind != TARGET_WAITKIND_STOPPED)
1988 return;
1989
1990 if (ecs->ws.value.sig != TARGET_SIGNAL_TRAP)
1991 return;
1992
4058b839
PA
1993 /* In reverse execution, when a breakpoint is hit, the instruction
1994 under it has already been de-executed. The reported PC always
1995 points at the breakpoint address, so adjusting it further would
1996 be wrong. E.g., consider this case on a decr_pc_after_break == 1
1997 architecture:
1998
1999 B1 0x08000000 : INSN1
2000 B2 0x08000001 : INSN2
2001 0x08000002 : INSN3
2002 PC -> 0x08000003 : INSN4
2003
2004 Say you're stopped at 0x08000003 as above. Reverse continuing
2005 from that point should hit B2 as below. Reading the PC when the
2006 SIGTRAP is reported should read 0x08000001 and INSN2 should have
2007 been de-executed already.
2008
2009 B1 0x08000000 : INSN1
2010 B2 PC -> 0x08000001 : INSN2
2011 0x08000002 : INSN3
2012 0x08000003 : INSN4
2013
2014 We can't apply the same logic as for forward execution, because
2015 we would wrongly adjust the PC to 0x08000000, since there's a
2016 breakpoint at PC - 1. We'd then report a hit on B1, although
2017 INSN1 hadn't been de-executed yet. Doing nothing is the correct
2018 behaviour. */
2019 if (execution_direction == EXEC_REVERSE)
2020 return;
2021
24a73cce
UW
2022 /* If this target does not decrement the PC after breakpoints, then
2023 we have nothing to do. */
2024 regcache = get_thread_regcache (ecs->ptid);
2025 gdbarch = get_regcache_arch (regcache);
2026 if (gdbarch_decr_pc_after_break (gdbarch) == 0)
2027 return;
2028
8aad930b
AC
2029 /* Find the location where (if we've hit a breakpoint) the
2030 breakpoint would be. */
515630c5
UW
2031 breakpoint_pc = regcache_read_pc (regcache)
2032 - gdbarch_decr_pc_after_break (gdbarch);
8aad930b 2033
1c5cfe86
PA
2034 /* Check whether there actually is a software breakpoint inserted at
2035 that location.
2036
2037 If in non-stop mode, a race condition is possible where we've
2038 removed a breakpoint, but stop events for that breakpoint were
2039 already queued and arrive later. To suppress those spurious
2040 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
2041 and retire them after a number of stop events are reported. */
2042 if (software_breakpoint_inserted_here_p (breakpoint_pc)
2043 || (non_stop && moribund_breakpoint_here_p (breakpoint_pc)))
8aad930b 2044 {
1c0fdd0e
UW
2045 /* When using hardware single-step, a SIGTRAP is reported for both
2046 a completed single-step and a software breakpoint. Need to
2047 differentiate between the two, as the latter needs adjusting
2048 but the former does not.
2049
2050 The SIGTRAP can be due to a completed hardware single-step only if
2051 - we didn't insert software single-step breakpoints
2052 - the thread to be examined is still the current thread
2053 - this thread is currently being stepped
2054
2055 If any of these events did not occur, we must have stopped due
2056 to hitting a software breakpoint, and have to back up to the
2057 breakpoint address.
2058
2059 As a special case, we could have hardware single-stepped a
2060 software breakpoint. In this case (prev_pc == breakpoint_pc),
2061 we also need to back up to the breakpoint address. */
2062
2063 if (singlestep_breakpoints_inserted_p
2064 || !ptid_equal (ecs->ptid, inferior_ptid)
4e1c45ea
PA
2065 || !currently_stepping (ecs->event_thread)
2066 || ecs->event_thread->prev_pc == breakpoint_pc)
515630c5 2067 regcache_write_pc (regcache, breakpoint_pc);
8aad930b 2068 }
4fa8626c
DJ
2069}
2070
0d1e5fa7
PA
2071void
2072init_infwait_state (void)
2073{
2074 waiton_ptid = pid_to_ptid (-1);
2075 infwait_state = infwait_normal_state;
2076}
2077
94cc34af
PA
2078void
2079error_is_running (void)
2080{
2081 error (_("\
2082Cannot execute this command while the selected thread is running."));
2083}
2084
2085void
2086ensure_not_running (void)
2087{
2088 if (is_running (inferior_ptid))
2089 error_is_running ();
2090}
2091
cd0fc7c3
SS
2092/* Given an execution control state that has been freshly filled in
2093 by an event from the inferior, figure out what it means and take
2094 appropriate action. */
c906108c 2095
cd0fc7c3 2096void
96baa820 2097handle_inferior_event (struct execution_control_state *ecs)
cd0fc7c3 2098{
c8edd8b4 2099 int sw_single_step_trap_p = 0;
d983da9c
DJ
2100 int stopped_by_watchpoint;
2101 int stepped_after_stopped_by_watchpoint = 0;
2afb61aa 2102 struct symtab_and_line stop_pc_sal;
d6b48e9c
PA
2103 enum stop_kind stop_soon;
2104
2105 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
2106 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
2107 && ecs->ws.kind != TARGET_WAITKIND_IGNORE)
2108 {
2109 struct inferior *inf = find_inferior_pid (ptid_get_pid (ecs->ptid));
2110 gdb_assert (inf);
2111 stop_soon = inf->stop_soon;
2112 }
2113 else
2114 stop_soon = NO_STOP_QUIETLY;
cd0fc7c3 2115
e02bc4cc 2116 /* Cache the last pid/waitstatus. */
39f77062 2117 target_last_wait_ptid = ecs->ptid;
0d1e5fa7 2118 target_last_waitstatus = ecs->ws;
e02bc4cc 2119
ca005067
DJ
2120 /* Always clear state belonging to the previous time we stopped. */
2121 stop_stack_dummy = 0;
2122
8c90c137
LM
2123 /* If it's a new process, add it to the thread database */
2124
2125 ecs->new_thread_event = (!ptid_equal (ecs->ptid, inferior_ptid)
2126 && !ptid_equal (ecs->ptid, minus_one_ptid)
2127 && !in_thread_list (ecs->ptid));
2128
2129 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
2130 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED && ecs->new_thread_event)
2131 add_thread (ecs->ptid);
2132
88ed393a
JK
2133 ecs->event_thread = find_thread_pid (ecs->ptid);
2134
2135 /* Dependent on valid ECS->EVENT_THREAD. */
2136 adjust_pc_after_break (ecs);
2137
2138 /* Dependent on the current PC value modified by adjust_pc_after_break. */
2139 reinit_frame_cache ();
2140
8c90c137
LM
2141 if (ecs->ws.kind != TARGET_WAITKIND_IGNORE)
2142 {
1c5cfe86
PA
2143 breakpoint_retire_moribund ();
2144
8c90c137
LM
2145 /* Mark the non-executing threads accordingly. */
2146 if (!non_stop
2147 || ecs->ws.kind == TARGET_WAITKIND_EXITED
2148 || ecs->ws.kind == TARGET_WAITKIND_SIGNALLED)
2149 set_executing (pid_to_ptid (-1), 0);
2150 else
2151 set_executing (ecs->ptid, 0);
2152 }
2153
0d1e5fa7 2154 switch (infwait_state)
488f131b
JB
2155 {
2156 case infwait_thread_hop_state:
527159b7 2157 if (debug_infrun)
8a9de0e4 2158 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_thread_hop_state\n");
488f131b 2159 /* Cancel the waiton_ptid. */
0d1e5fa7 2160 waiton_ptid = pid_to_ptid (-1);
65e82032 2161 break;
b83266a0 2162
488f131b 2163 case infwait_normal_state:
527159b7 2164 if (debug_infrun)
8a9de0e4 2165 fprintf_unfiltered (gdb_stdlog, "infrun: infwait_normal_state\n");
d983da9c
DJ
2166 break;
2167
2168 case infwait_step_watch_state:
2169 if (debug_infrun)
2170 fprintf_unfiltered (gdb_stdlog,
2171 "infrun: infwait_step_watch_state\n");
2172
2173 stepped_after_stopped_by_watchpoint = 1;
488f131b 2174 break;
b83266a0 2175
488f131b 2176 case infwait_nonstep_watch_state:
527159b7 2177 if (debug_infrun)
8a9de0e4
AC
2178 fprintf_unfiltered (gdb_stdlog,
2179 "infrun: infwait_nonstep_watch_state\n");
488f131b 2180 insert_breakpoints ();
c906108c 2181
488f131b
JB
2182 /* FIXME-maybe: is this cleaner than setting a flag? Does it
2183 handle things like signals arriving and other things happening
2184 in combination correctly? */
2185 stepped_after_stopped_by_watchpoint = 1;
2186 break;
65e82032
AC
2187
2188 default:
e2e0b3e5 2189 internal_error (__FILE__, __LINE__, _("bad switch"));
488f131b 2190 }
0d1e5fa7 2191 infwait_state = infwait_normal_state;
c906108c 2192
488f131b
JB
2193 switch (ecs->ws.kind)
2194 {
2195 case TARGET_WAITKIND_LOADED:
527159b7 2196 if (debug_infrun)
8a9de0e4 2197 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_LOADED\n");
b0f4b84b
DJ
2198 /* Ignore gracefully during startup of the inferior, as it might
2199 be the shell which has just loaded some objects, otherwise
2200 add the symbols for the newly loaded objects. Also ignore at
2201 the beginning of an attach or remote session; we will query
2202 the full list of libraries once the connection is
2203 established. */
c0236d92 2204 if (stop_soon == NO_STOP_QUIETLY)
488f131b 2205 {
488f131b
JB
2206 /* Check for any newly added shared libraries if we're
2207 supposed to be adding them automatically. Switch
2208 terminal for any messages produced by
2209 breakpoint_re_set. */
2210 target_terminal_ours_for_output ();
aff6338a 2211 /* NOTE: cagney/2003-11-25: Make certain that the target
8fb3e588
AC
2212 stack's section table is kept up-to-date. Architectures,
2213 (e.g., PPC64), use the section table to perform
2214 operations such as address => section name and hence
2215 require the table to contain all sections (including
2216 those found in shared libraries). */
aff6338a 2217 /* NOTE: cagney/2003-11-25: Pass current_target and not
8fb3e588
AC
2218 exec_ops to SOLIB_ADD. This is because current GDB is
2219 only tooled to propagate section_table changes out from
2220 the "current_target" (see target_resize_to_sections), and
2221 not up from the exec stratum. This, of course, isn't
2222 right. "infrun.c" should only interact with the
2223 exec/process stratum, instead relying on the target stack
2224 to propagate relevant changes (stop, section table
2225 changed, ...) up to other layers. */
b0f4b84b 2226#ifdef SOLIB_ADD
aff6338a 2227 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
b0f4b84b
DJ
2228#else
2229 solib_add (NULL, 0, &current_target, auto_solib_add);
2230#endif
488f131b
JB
2231 target_terminal_inferior ();
2232
b0f4b84b
DJ
2233 /* If requested, stop when the dynamic linker notifies
2234 gdb of events. This allows the user to get control
2235 and place breakpoints in initializer routines for
2236 dynamically loaded objects (among other things). */
2237 if (stop_on_solib_events)
2238 {
2239 stop_stepping (ecs);
2240 return;
2241 }
2242
2243 /* NOTE drow/2007-05-11: This might be a good place to check
2244 for "catch load". */
488f131b 2245 }
b0f4b84b
DJ
2246
2247 /* If we are skipping through a shell, or through shared library
2248 loading that we aren't interested in, resume the program. If
2249 we're running the program normally, also resume. But stop if
2250 we're attaching or setting up a remote connection. */
2251 if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
2252 {
74960c60
VP
2253 /* Loading of shared libraries might have changed breakpoint
2254 addresses. Make sure new breakpoints are inserted. */
0b02b92d
UW
2255 if (stop_soon == NO_STOP_QUIETLY
2256 && !breakpoints_always_inserted_mode ())
74960c60 2257 insert_breakpoints ();
b0f4b84b
DJ
2258 resume (0, TARGET_SIGNAL_0);
2259 prepare_to_wait (ecs);
2260 return;
2261 }
2262
2263 break;
c5aa993b 2264
488f131b 2265 case TARGET_WAITKIND_SPURIOUS:
527159b7 2266 if (debug_infrun)
8a9de0e4 2267 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SPURIOUS\n");
488f131b
JB
2268 resume (0, TARGET_SIGNAL_0);
2269 prepare_to_wait (ecs);
2270 return;
c5aa993b 2271
488f131b 2272 case TARGET_WAITKIND_EXITED:
527159b7 2273 if (debug_infrun)
8a9de0e4 2274 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXITED\n");
488f131b
JB
2275 target_terminal_ours (); /* Must do this before mourn anyway */
2276 print_stop_reason (EXITED, ecs->ws.value.integer);
2277
2278 /* Record the exit code in the convenience variable $_exitcode, so
2279 that the user can inspect this again later. */
2280 set_internalvar (lookup_internalvar ("_exitcode"),
8b9b9e1a 2281 value_from_longest (builtin_type_int32,
488f131b
JB
2282 (LONGEST) ecs->ws.value.integer));
2283 gdb_flush (gdb_stdout);
2284 target_mourn_inferior ();
1c0fdd0e 2285 singlestep_breakpoints_inserted_p = 0;
488f131b
JB
2286 stop_print_frame = 0;
2287 stop_stepping (ecs);
2288 return;
c5aa993b 2289
488f131b 2290 case TARGET_WAITKIND_SIGNALLED:
527159b7 2291 if (debug_infrun)
8a9de0e4 2292 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SIGNALLED\n");
488f131b 2293 stop_print_frame = 0;
488f131b 2294 target_terminal_ours (); /* Must do this before mourn anyway */
c5aa993b 2295
488f131b
JB
2296 /* Note: By definition of TARGET_WAITKIND_SIGNALLED, we shouldn't
2297 reach here unless the inferior is dead. However, for years
2298 target_kill() was called here, which hints that fatal signals aren't
2299 really fatal on some systems. If that's true, then some changes
2300 may be needed. */
2301 target_mourn_inferior ();
c906108c 2302
2020b7ab 2303 print_stop_reason (SIGNAL_EXITED, ecs->ws.value.sig);
1c0fdd0e 2304 singlestep_breakpoints_inserted_p = 0;
488f131b
JB
2305 stop_stepping (ecs);
2306 return;
c906108c 2307
488f131b
JB
2308 /* The following are the only cases in which we keep going;
2309 the above cases end in a continue or goto. */
2310 case TARGET_WAITKIND_FORKED:
deb3b17b 2311 case TARGET_WAITKIND_VFORKED:
527159b7 2312 if (debug_infrun)
8a9de0e4 2313 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\n");
488f131b
JB
2314 pending_follow.kind = ecs->ws.kind;
2315
3a3e9ee3 2316 pending_follow.fork_event.parent_pid = ecs->ptid;
8e7d2c16 2317 pending_follow.fork_event.child_pid = ecs->ws.value.related_pid;
c906108c 2318
5a2901d9
DJ
2319 if (!ptid_equal (ecs->ptid, inferior_ptid))
2320 {
0d1e5fa7 2321 context_switch (ecs->ptid);
35f196d9 2322 reinit_frame_cache ();
5a2901d9
DJ
2323 }
2324
488f131b 2325 stop_pc = read_pc ();
675bf4cb 2326
347bddb7 2327 ecs->event_thread->stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
675bf4cb 2328
347bddb7 2329 ecs->random_signal = !bpstat_explains_signal (ecs->event_thread->stop_bpstat);
04e68871
DJ
2330
2331 /* If no catchpoint triggered for this, then keep going. */
2332 if (ecs->random_signal)
2333 {
2020b7ab 2334 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
04e68871
DJ
2335 keep_going (ecs);
2336 return;
2337 }
2020b7ab 2338 ecs->event_thread->stop_signal = TARGET_SIGNAL_TRAP;
488f131b
JB
2339 goto process_event_stop_test;
2340
2341 case TARGET_WAITKIND_EXECD:
527159b7 2342 if (debug_infrun)
fc5261f2 2343 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
488f131b
JB
2344 pending_follow.execd_pathname =
2345 savestring (ecs->ws.value.execd_pathname,
2346 strlen (ecs->ws.value.execd_pathname));
2347
5a2901d9
DJ
2348 if (!ptid_equal (ecs->ptid, inferior_ptid))
2349 {
0d1e5fa7 2350 context_switch (ecs->ptid);
35f196d9 2351 reinit_frame_cache ();
5a2901d9
DJ
2352 }
2353
795e548f
PA
2354 stop_pc = read_pc ();
2355
2356 /* This causes the eventpoints and symbol table to be reset.
2357 Must do this now, before trying to determine whether to
2358 stop. */
2359 follow_exec (inferior_ptid, pending_follow.execd_pathname);
2360 xfree (pending_follow.execd_pathname);
2361
2362 ecs->event_thread->stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
2363 ecs->random_signal = !bpstat_explains_signal (ecs->event_thread->stop_bpstat);
2364
04e68871
DJ
2365 /* If no catchpoint triggered for this, then keep going. */
2366 if (ecs->random_signal)
2367 {
2020b7ab 2368 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
04e68871
DJ
2369 keep_going (ecs);
2370 return;
2371 }
2020b7ab 2372 ecs->event_thread->stop_signal = TARGET_SIGNAL_TRAP;
488f131b
JB
2373 goto process_event_stop_test;
2374
b4dc5ffa
MK
2375 /* Be careful not to try to gather much state about a thread
2376 that's in a syscall. It's frequently a losing proposition. */
488f131b 2377 case TARGET_WAITKIND_SYSCALL_ENTRY:
527159b7 2378 if (debug_infrun)
8a9de0e4 2379 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SYSCALL_ENTRY\n");
488f131b
JB
2380 resume (0, TARGET_SIGNAL_0);
2381 prepare_to_wait (ecs);
2382 return;
c906108c 2383
488f131b
JB
2384 /* Before examining the threads further, step this thread to
2385 get it entirely out of the syscall. (We get notice of the
2386 event when the thread is just on the verge of exiting a
2387 syscall. Stepping one instruction seems to get it back
b4dc5ffa 2388 into user code.) */
488f131b 2389 case TARGET_WAITKIND_SYSCALL_RETURN:
527159b7 2390 if (debug_infrun)
8a9de0e4 2391 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SYSCALL_RETURN\n");
488f131b 2392 target_resume (ecs->ptid, 1, TARGET_SIGNAL_0);
488f131b
JB
2393 prepare_to_wait (ecs);
2394 return;
c906108c 2395
488f131b 2396 case TARGET_WAITKIND_STOPPED:
527159b7 2397 if (debug_infrun)
8a9de0e4 2398 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_STOPPED\n");
2020b7ab 2399 ecs->event_thread->stop_signal = ecs->ws.value.sig;
488f131b 2400 break;
c906108c 2401
b2175913
MS
2402 case TARGET_WAITKIND_NO_HISTORY:
2403 /* Reverse execution: target ran out of history info. */
40e12b06 2404 stop_pc = read_pc ();
b2175913
MS
2405 print_stop_reason (NO_HISTORY, 0);
2406 stop_stepping (ecs);
2407 return;
2408
488f131b
JB
2409 /* We had an event in the inferior, but we are not interested
2410 in handling it at this level. The lower layers have already
8e7d2c16 2411 done what needs to be done, if anything.
8fb3e588
AC
2412
2413 One of the possible circumstances for this is when the
2414 inferior produces output for the console. The inferior has
2415 not stopped, and we are ignoring the event. Another possible
2416 circumstance is any event which the lower level knows will be
2417 reported multiple times without an intervening resume. */
488f131b 2418 case TARGET_WAITKIND_IGNORE:
527159b7 2419 if (debug_infrun)
8a9de0e4 2420 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_IGNORE\n");
8e7d2c16 2421 prepare_to_wait (ecs);
488f131b
JB
2422 return;
2423 }
c906108c 2424
488f131b
JB
2425 if (ecs->new_thread_event)
2426 {
94cc34af
PA
2427 if (non_stop)
2428 /* Non-stop assumes that the target handles adding new threads
2429 to the thread list. */
2430 internal_error (__FILE__, __LINE__, "\
2431targets should add new threads to the thread list themselves in non-stop mode.");
2432
2433 /* We may want to consider not doing a resume here in order to
2434 give the user a chance to play with the new thread. It might
2435 be good to make that a user-settable option. */
2436
2437 /* At this point, all threads are stopped (happens automatically
2438 in either the OS or the native code). Therefore we need to
2439 continue all threads in order to make progress. */
2440
488f131b
JB
2441 target_resume (RESUME_ALL, 0, TARGET_SIGNAL_0);
2442 prepare_to_wait (ecs);
2443 return;
2444 }
c906108c 2445
2020b7ab 2446 if (ecs->ws.kind == TARGET_WAITKIND_STOPPED)
252fbfc8
PA
2447 {
2448 /* Do we need to clean up the state of a thread that has
2449 completed a displaced single-step? (Doing so usually affects
2450 the PC, so do it here, before we set stop_pc.) */
2451 displaced_step_fixup (ecs->ptid, ecs->event_thread->stop_signal);
2452
2453 /* If we either finished a single-step or hit a breakpoint, but
2454 the user wanted this thread to be stopped, pretend we got a
2455 SIG0 (generic unsignaled stop). */
2456
2457 if (ecs->event_thread->stop_requested
2458 && ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP)
2459 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
2460 }
237fc4c9 2461
515630c5 2462 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
488f131b 2463
527159b7 2464 if (debug_infrun)
237fc4c9
PA
2465 {
2466 fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = 0x%s\n",
2467 paddr_nz (stop_pc));
2468 if (STOPPED_BY_WATCHPOINT (&ecs->ws))
2469 {
2470 CORE_ADDR addr;
2471 fprintf_unfiltered (gdb_stdlog, "infrun: stopped by watchpoint\n");
2472
2473 if (target_stopped_data_address (&current_target, &addr))
2474 fprintf_unfiltered (gdb_stdlog,
2475 "infrun: stopped data address = 0x%s\n",
2476 paddr_nz (addr));
2477 else
2478 fprintf_unfiltered (gdb_stdlog,
2479 "infrun: (no data address available)\n");
2480 }
2481 }
527159b7 2482
9f976b41
DJ
2483 if (stepping_past_singlestep_breakpoint)
2484 {
1c0fdd0e 2485 gdb_assert (singlestep_breakpoints_inserted_p);
9f976b41
DJ
2486 gdb_assert (ptid_equal (singlestep_ptid, ecs->ptid));
2487 gdb_assert (!ptid_equal (singlestep_ptid, saved_singlestep_ptid));
2488
2489 stepping_past_singlestep_breakpoint = 0;
2490
2491 /* We've either finished single-stepping past the single-step
8fb3e588
AC
2492 breakpoint, or stopped for some other reason. It would be nice if
2493 we could tell, but we can't reliably. */
2020b7ab 2494 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP)
8fb3e588 2495 {
527159b7 2496 if (debug_infrun)
8a9de0e4 2497 fprintf_unfiltered (gdb_stdlog, "infrun: stepping_past_singlestep_breakpoint\n");
9f976b41 2498 /* Pull the single step breakpoints out of the target. */
e0cd558a 2499 remove_single_step_breakpoints ();
9f976b41
DJ
2500 singlestep_breakpoints_inserted_p = 0;
2501
2502 ecs->random_signal = 0;
2503
0d1e5fa7 2504 context_switch (saved_singlestep_ptid);
9a4105ab
AC
2505 if (deprecated_context_hook)
2506 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
9f976b41
DJ
2507
2508 resume (1, TARGET_SIGNAL_0);
2509 prepare_to_wait (ecs);
2510 return;
2511 }
2512 }
2513
2514 stepping_past_singlestep_breakpoint = 0;
2515
ca67fcb8 2516 if (!ptid_equal (deferred_step_ptid, null_ptid))
6a6b96b9 2517 {
94cc34af
PA
2518 /* In non-stop mode, there's never a deferred_step_ptid set. */
2519 gdb_assert (!non_stop);
2520
6a6b96b9
UW
2521 /* If we stopped for some other reason than single-stepping, ignore
2522 the fact that we were supposed to switch back. */
2020b7ab 2523 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP)
6a6b96b9 2524 {
4e1c45ea
PA
2525 struct thread_info *tp;
2526
6a6b96b9
UW
2527 if (debug_infrun)
2528 fprintf_unfiltered (gdb_stdlog,
ca67fcb8 2529 "infrun: handling deferred step\n");
6a6b96b9
UW
2530
2531 /* Pull the single step breakpoints out of the target. */
2532 if (singlestep_breakpoints_inserted_p)
2533 {
2534 remove_single_step_breakpoints ();
2535 singlestep_breakpoints_inserted_p = 0;
2536 }
2537
2538 /* Note: We do not call context_switch at this point, as the
2539 context is already set up for stepping the original thread. */
ca67fcb8
VP
2540 switch_to_thread (deferred_step_ptid);
2541 deferred_step_ptid = null_ptid;
6a6b96b9
UW
2542 /* Suppress spurious "Switching to ..." message. */
2543 previous_inferior_ptid = inferior_ptid;
2544
2545 resume (1, TARGET_SIGNAL_0);
2546 prepare_to_wait (ecs);
2547 return;
2548 }
ca67fcb8
VP
2549
2550 deferred_step_ptid = null_ptid;
6a6b96b9
UW
2551 }
2552
488f131b
JB
2553 /* See if a thread hit a thread-specific breakpoint that was meant for
2554 another thread. If so, then step that thread past the breakpoint,
2555 and continue it. */
2556
2020b7ab 2557 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP)
488f131b 2558 {
9f976b41
DJ
2559 int thread_hop_needed = 0;
2560
f8d40ec8
JB
2561 /* Check if a regular breakpoint has been hit before checking
2562 for a potential single step breakpoint. Otherwise, GDB will
2563 not see this breakpoint hit when stepping onto breakpoints. */
c36b740a 2564 if (regular_breakpoint_inserted_here_p (stop_pc))
488f131b 2565 {
c5aa993b 2566 ecs->random_signal = 0;
4fa8626c 2567 if (!breakpoint_thread_match (stop_pc, ecs->ptid))
9f976b41
DJ
2568 thread_hop_needed = 1;
2569 }
1c0fdd0e 2570 else if (singlestep_breakpoints_inserted_p)
9f976b41 2571 {
fd48f117
DJ
2572 /* We have not context switched yet, so this should be true
2573 no matter which thread hit the singlestep breakpoint. */
2574 gdb_assert (ptid_equal (inferior_ptid, singlestep_ptid));
2575 if (debug_infrun)
2576 fprintf_unfiltered (gdb_stdlog, "infrun: software single step "
2577 "trap for %s\n",
2578 target_pid_to_str (ecs->ptid));
2579
9f976b41
DJ
2580 ecs->random_signal = 0;
2581 /* The call to in_thread_list is necessary because PTIDs sometimes
2582 change when we go from single-threaded to multi-threaded. If
2583 the singlestep_ptid is still in the list, assume that it is
2584 really different from ecs->ptid. */
2585 if (!ptid_equal (singlestep_ptid, ecs->ptid)
2586 && in_thread_list (singlestep_ptid))
2587 {
fd48f117
DJ
2588 /* If the PC of the thread we were trying to single-step
2589 has changed, discard this event (which we were going
2590 to ignore anyway), and pretend we saw that thread
2591 trap. This prevents us continuously moving the
2592 single-step breakpoint forward, one instruction at a
2593 time. If the PC has changed, then the thread we were
2594 trying to single-step has trapped or been signalled,
2595 but the event has not been reported to GDB yet.
2596
2597 There might be some cases where this loses signal
2598 information, if a signal has arrived at exactly the
2599 same time that the PC changed, but this is the best
2600 we can do with the information available. Perhaps we
2601 should arrange to report all events for all threads
2602 when they stop, or to re-poll the remote looking for
2603 this particular thread (i.e. temporarily enable
2604 schedlock). */
515630c5
UW
2605
2606 CORE_ADDR new_singlestep_pc
2607 = regcache_read_pc (get_thread_regcache (singlestep_ptid));
2608
2609 if (new_singlestep_pc != singlestep_pc)
fd48f117 2610 {
2020b7ab
PA
2611 enum target_signal stop_signal;
2612
fd48f117
DJ
2613 if (debug_infrun)
2614 fprintf_unfiltered (gdb_stdlog, "infrun: unexpected thread,"
2615 " but expected thread advanced also\n");
2616
2617 /* The current context still belongs to
2618 singlestep_ptid. Don't swap here, since that's
2619 the context we want to use. Just fudge our
2620 state and continue. */
2020b7ab
PA
2621 stop_signal = ecs->event_thread->stop_signal;
2622 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
fd48f117 2623 ecs->ptid = singlestep_ptid;
4e1c45ea 2624 ecs->event_thread = find_thread_pid (ecs->ptid);
2020b7ab 2625 ecs->event_thread->stop_signal = stop_signal;
515630c5 2626 stop_pc = new_singlestep_pc;
fd48f117
DJ
2627 }
2628 else
2629 {
2630 if (debug_infrun)
2631 fprintf_unfiltered (gdb_stdlog,
2632 "infrun: unexpected thread\n");
2633
2634 thread_hop_needed = 1;
2635 stepping_past_singlestep_breakpoint = 1;
2636 saved_singlestep_ptid = singlestep_ptid;
2637 }
9f976b41
DJ
2638 }
2639 }
2640
2641 if (thread_hop_needed)
8fb3e588 2642 {
237fc4c9 2643 int remove_status = 0;
8fb3e588 2644
527159b7 2645 if (debug_infrun)
8a9de0e4 2646 fprintf_unfiltered (gdb_stdlog, "infrun: thread_hop_needed\n");
527159b7 2647
8fb3e588
AC
2648 /* Saw a breakpoint, but it was hit by the wrong thread.
2649 Just continue. */
2650
1c0fdd0e 2651 if (singlestep_breakpoints_inserted_p)
488f131b 2652 {
8fb3e588 2653 /* Pull the single step breakpoints out of the target. */
e0cd558a 2654 remove_single_step_breakpoints ();
8fb3e588
AC
2655 singlestep_breakpoints_inserted_p = 0;
2656 }
2657
237fc4c9
PA
2658 /* If the arch can displace step, don't remove the
2659 breakpoints. */
2660 if (!use_displaced_stepping (current_gdbarch))
2661 remove_status = remove_breakpoints ();
2662
8fb3e588
AC
2663 /* Did we fail to remove breakpoints? If so, try
2664 to set the PC past the bp. (There's at least
2665 one situation in which we can fail to remove
2666 the bp's: On HP-UX's that use ttrace, we can't
2667 change the address space of a vforking child
2668 process until the child exits (well, okay, not
2669 then either :-) or execs. */
2670 if (remove_status != 0)
9d9cd7ac 2671 error (_("Cannot step over breakpoint hit in wrong thread"));
8fb3e588
AC
2672 else
2673 { /* Single step */
8fb3e588 2674 if (!ptid_equal (inferior_ptid, ecs->ptid))
0d1e5fa7
PA
2675 context_switch (ecs->ptid);
2676
94cc34af
PA
2677 if (!non_stop)
2678 {
2679 /* Only need to require the next event from this
2680 thread in all-stop mode. */
2681 waiton_ptid = ecs->ptid;
2682 infwait_state = infwait_thread_hop_state;
2683 }
8fb3e588 2684
4e1c45ea 2685 ecs->event_thread->stepping_over_breakpoint = 1;
8fb3e588
AC
2686 keep_going (ecs);
2687 registers_changed ();
2688 return;
2689 }
488f131b 2690 }
1c0fdd0e 2691 else if (singlestep_breakpoints_inserted_p)
8fb3e588
AC
2692 {
2693 sw_single_step_trap_p = 1;
2694 ecs->random_signal = 0;
2695 }
488f131b
JB
2696 }
2697 else
2698 ecs->random_signal = 1;
c906108c 2699
488f131b 2700 /* See if something interesting happened to the non-current thread. If
b40c7d58
DJ
2701 so, then switch to that thread. */
2702 if (!ptid_equal (ecs->ptid, inferior_ptid))
488f131b 2703 {
527159b7 2704 if (debug_infrun)
8a9de0e4 2705 fprintf_unfiltered (gdb_stdlog, "infrun: context switch\n");
527159b7 2706
0d1e5fa7 2707 context_switch (ecs->ptid);
c5aa993b 2708
9a4105ab
AC
2709 if (deprecated_context_hook)
2710 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
488f131b 2711 }
c906108c 2712
1c0fdd0e 2713 if (singlestep_breakpoints_inserted_p)
488f131b
JB
2714 {
2715 /* Pull the single step breakpoints out of the target. */
e0cd558a 2716 remove_single_step_breakpoints ();
488f131b
JB
2717 singlestep_breakpoints_inserted_p = 0;
2718 }
c906108c 2719
d983da9c
DJ
2720 if (stepped_after_stopped_by_watchpoint)
2721 stopped_by_watchpoint = 0;
2722 else
2723 stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
2724
2725 /* If necessary, step over this watchpoint. We'll be back to display
2726 it in a moment. */
2727 if (stopped_by_watchpoint
2728 && (HAVE_STEPPABLE_WATCHPOINT
2729 || gdbarch_have_nonsteppable_watchpoint (current_gdbarch)))
488f131b 2730 {
488f131b
JB
2731 /* At this point, we are stopped at an instruction which has
2732 attempted to write to a piece of memory under control of
2733 a watchpoint. The instruction hasn't actually executed
2734 yet. If we were to evaluate the watchpoint expression
2735 now, we would get the old value, and therefore no change
2736 would seem to have occurred.
2737
2738 In order to make watchpoints work `right', we really need
2739 to complete the memory write, and then evaluate the
d983da9c
DJ
2740 watchpoint expression. We do this by single-stepping the
2741 target.
2742
2743 It may not be necessary to disable the watchpoint to stop over
2744 it. For example, the PA can (with some kernel cooperation)
2745 single step over a watchpoint without disabling the watchpoint.
2746
2747 It is far more common to need to disable a watchpoint to step
2748 the inferior over it. If we have non-steppable watchpoints,
2749 we must disable the current watchpoint; it's simplest to
2750 disable all watchpoints and breakpoints. */
2751
2752 if (!HAVE_STEPPABLE_WATCHPOINT)
2753 remove_breakpoints ();
488f131b
JB
2754 registers_changed ();
2755 target_resume (ecs->ptid, 1, TARGET_SIGNAL_0); /* Single step */
0d1e5fa7 2756 waiton_ptid = ecs->ptid;
d983da9c 2757 if (HAVE_STEPPABLE_WATCHPOINT)
0d1e5fa7 2758 infwait_state = infwait_step_watch_state;
d983da9c 2759 else
0d1e5fa7 2760 infwait_state = infwait_nonstep_watch_state;
488f131b
JB
2761 prepare_to_wait (ecs);
2762 return;
2763 }
2764
488f131b
JB
2765 ecs->stop_func_start = 0;
2766 ecs->stop_func_end = 0;
2767 ecs->stop_func_name = 0;
2768 /* Don't care about return value; stop_func_start and stop_func_name
2769 will both be 0 if it doesn't work. */
2770 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
2771 &ecs->stop_func_start, &ecs->stop_func_end);
cbf3b44a
UW
2772 ecs->stop_func_start
2773 += gdbarch_deprecated_function_start_offset (current_gdbarch);
4e1c45ea 2774 ecs->event_thread->stepping_over_breakpoint = 0;
347bddb7 2775 bpstat_clear (&ecs->event_thread->stop_bpstat);
414c69f7 2776 ecs->event_thread->stop_step = 0;
488f131b
JB
2777 stop_print_frame = 1;
2778 ecs->random_signal = 0;
2779 stopped_by_random_signal = 0;
488f131b 2780
2020b7ab 2781 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP
4e1c45ea 2782 && ecs->event_thread->trap_expected
3352ef37 2783 && gdbarch_single_step_through_delay_p (current_gdbarch)
4e1c45ea 2784 && currently_stepping (ecs->event_thread))
3352ef37 2785 {
b50d7442 2786 /* We're trying to step off a breakpoint. Turns out that we're
3352ef37
AC
2787 also on an instruction that needs to be stepped multiple
2788 times before it's been fully executing. E.g., architectures
2789 with a delay slot. It needs to be stepped twice, once for
2790 the instruction and once for the delay slot. */
2791 int step_through_delay
2792 = gdbarch_single_step_through_delay (current_gdbarch,
2793 get_current_frame ());
527159b7 2794 if (debug_infrun && step_through_delay)
8a9de0e4 2795 fprintf_unfiltered (gdb_stdlog, "infrun: step through delay\n");
4e1c45ea 2796 if (ecs->event_thread->step_range_end == 0 && step_through_delay)
3352ef37
AC
2797 {
2798 /* The user issued a continue when stopped at a breakpoint.
2799 Set up for another trap and get out of here. */
4e1c45ea 2800 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
2801 keep_going (ecs);
2802 return;
2803 }
2804 else if (step_through_delay)
2805 {
2806 /* The user issued a step when stopped at a breakpoint.
2807 Maybe we should stop, maybe we should not - the delay
2808 slot *might* correspond to a line of source. In any
ca67fcb8
VP
2809 case, don't decide that here, just set
2810 ecs->stepping_over_breakpoint, making sure we
2811 single-step again before breakpoints are re-inserted. */
4e1c45ea 2812 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
2813 }
2814 }
2815
488f131b
JB
2816 /* Look at the cause of the stop, and decide what to do.
2817 The alternatives are:
0d1e5fa7
PA
2818 1) stop_stepping and return; to really stop and return to the debugger,
2819 2) keep_going and return to start up again
4e1c45ea 2820 (set ecs->event_thread->stepping_over_breakpoint to 1 to single step once)
488f131b
JB
2821 3) set ecs->random_signal to 1, and the decision between 1 and 2
2822 will be made according to the signal handling tables. */
2823
2824 /* First, distinguish signals caused by the debugger from signals
03cebad2
MK
2825 that have to do with the program's own actions. Note that
2826 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
2827 on the operating system version. Here we detect when a SIGILL or
2828 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
2829 something similar for SIGSEGV, since a SIGSEGV will be generated
2830 when we're trying to execute a breakpoint instruction on a
2831 non-executable stack. This happens for call dummy breakpoints
2832 for architectures like SPARC that place call dummies on the
237fc4c9 2833 stack.
488f131b 2834
237fc4c9
PA
2835 If we're doing a displaced step past a breakpoint, then the
2836 breakpoint is always inserted at the original instruction;
2837 non-standard signals can't be explained by the breakpoint. */
2020b7ab 2838 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP
4e1c45ea 2839 || (! ecs->event_thread->trap_expected
237fc4c9 2840 && breakpoint_inserted_here_p (stop_pc)
2020b7ab
PA
2841 && (ecs->event_thread->stop_signal == TARGET_SIGNAL_ILL
2842 || ecs->event_thread->stop_signal == TARGET_SIGNAL_SEGV
2843 || ecs->event_thread->stop_signal == TARGET_SIGNAL_EMT))
b0f4b84b
DJ
2844 || stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_NO_SIGSTOP
2845 || stop_soon == STOP_QUIETLY_REMOTE)
488f131b 2846 {
2020b7ab 2847 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP && stop_after_trap)
488f131b 2848 {
527159b7 2849 if (debug_infrun)
8a9de0e4 2850 fprintf_unfiltered (gdb_stdlog, "infrun: stopped\n");
488f131b
JB
2851 stop_print_frame = 0;
2852 stop_stepping (ecs);
2853 return;
2854 }
c54cfec8
EZ
2855
2856 /* This is originated from start_remote(), start_inferior() and
2857 shared libraries hook functions. */
b0f4b84b 2858 if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
488f131b 2859 {
527159b7 2860 if (debug_infrun)
8a9de0e4 2861 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
488f131b
JB
2862 stop_stepping (ecs);
2863 return;
2864 }
2865
c54cfec8 2866 /* This originates from attach_command(). We need to overwrite
a0d21d28
PA
2867 the stop_signal here, because some kernels don't ignore a
2868 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
2869 See more comments in inferior.h. On the other hand, if we
a0ef4274 2870 get a non-SIGSTOP, report it to the user - assume the backend
a0d21d28
PA
2871 will handle the SIGSTOP if it should show up later.
2872
2873 Also consider that the attach is complete when we see a
2874 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
2875 target extended-remote report it instead of a SIGSTOP
2876 (e.g. gdbserver). We already rely on SIGTRAP being our
e0ba6746
PA
2877 signal, so this is no exception.
2878
2879 Also consider that the attach is complete when we see a
2880 TARGET_SIGNAL_0. In non-stop mode, GDB will explicitly tell
2881 the target to stop all threads of the inferior, in case the
2882 low level attach operation doesn't stop them implicitly. If
2883 they weren't stopped implicitly, then the stub will report a
2884 TARGET_SIGNAL_0, meaning: stopped for no particular reason
2885 other than GDB's request. */
a0ef4274 2886 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
2020b7ab 2887 && (ecs->event_thread->stop_signal == TARGET_SIGNAL_STOP
e0ba6746
PA
2888 || ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP
2889 || ecs->event_thread->stop_signal == TARGET_SIGNAL_0))
c54cfec8
EZ
2890 {
2891 stop_stepping (ecs);
2020b7ab 2892 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
c54cfec8
EZ
2893 return;
2894 }
2895
fba57f8f 2896 /* See if there is a breakpoint at the current PC. */
347bddb7 2897 ecs->event_thread->stop_bpstat = bpstat_stop_status (stop_pc, ecs->ptid);
fba57f8f
VP
2898
2899 /* Following in case break condition called a
2900 function. */
2901 stop_print_frame = 1;
488f131b 2902
73dd234f 2903 /* NOTE: cagney/2003-03-29: These two checks for a random signal
8fb3e588
AC
2904 at one stage in the past included checks for an inferior
2905 function call's call dummy's return breakpoint. The original
2906 comment, that went with the test, read:
73dd234f 2907
8fb3e588
AC
2908 ``End of a stack dummy. Some systems (e.g. Sony news) give
2909 another signal besides SIGTRAP, so check here as well as
2910 above.''
73dd234f 2911
8002d778 2912 If someone ever tries to get call dummys on a
73dd234f 2913 non-executable stack to work (where the target would stop
03cebad2
MK
2914 with something like a SIGSEGV), then those tests might need
2915 to be re-instated. Given, however, that the tests were only
73dd234f 2916 enabled when momentary breakpoints were not being used, I
03cebad2
MK
2917 suspect that it won't be the case.
2918
8fb3e588
AC
2919 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
2920 be necessary for call dummies on a non-executable stack on
2921 SPARC. */
73dd234f 2922
2020b7ab 2923 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP)
488f131b 2924 ecs->random_signal
347bddb7 2925 = !(bpstat_explains_signal (ecs->event_thread->stop_bpstat)
4e1c45ea
PA
2926 || ecs->event_thread->trap_expected
2927 || (ecs->event_thread->step_range_end
2928 && ecs->event_thread->step_resume_breakpoint == NULL));
488f131b
JB
2929 else
2930 {
347bddb7 2931 ecs->random_signal = !bpstat_explains_signal (ecs->event_thread->stop_bpstat);
488f131b 2932 if (!ecs->random_signal)
2020b7ab 2933 ecs->event_thread->stop_signal = TARGET_SIGNAL_TRAP;
488f131b
JB
2934 }
2935 }
2936
2937 /* When we reach this point, we've pretty much decided
2938 that the reason for stopping must've been a random
2939 (unexpected) signal. */
2940
2941 else
2942 ecs->random_signal = 1;
488f131b 2943
04e68871 2944process_event_stop_test:
488f131b
JB
2945 /* For the program's own signals, act according to
2946 the signal handling tables. */
2947
2948 if (ecs->random_signal)
2949 {
2950 /* Signal not for debugging purposes. */
2951 int printed = 0;
2952
527159b7 2953 if (debug_infrun)
2020b7ab
PA
2954 fprintf_unfiltered (gdb_stdlog, "infrun: random signal %d\n",
2955 ecs->event_thread->stop_signal);
527159b7 2956
488f131b
JB
2957 stopped_by_random_signal = 1;
2958
2020b7ab 2959 if (signal_print[ecs->event_thread->stop_signal])
488f131b
JB
2960 {
2961 printed = 1;
2962 target_terminal_ours_for_output ();
2020b7ab 2963 print_stop_reason (SIGNAL_RECEIVED, ecs->event_thread->stop_signal);
488f131b 2964 }
252fbfc8
PA
2965 /* Always stop on signals if we're either just gaining control
2966 of the program, or the user explicitly requested this thread
2967 to remain stopped. */
d6b48e9c 2968 if (stop_soon != NO_STOP_QUIETLY
252fbfc8 2969 || ecs->event_thread->stop_requested
d6b48e9c 2970 || signal_stop_state (ecs->event_thread->stop_signal))
488f131b
JB
2971 {
2972 stop_stepping (ecs);
2973 return;
2974 }
2975 /* If not going to stop, give terminal back
2976 if we took it away. */
2977 else if (printed)
2978 target_terminal_inferior ();
2979
2980 /* Clear the signal if it should not be passed. */
2020b7ab
PA
2981 if (signal_program[ecs->event_thread->stop_signal] == 0)
2982 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
488f131b 2983
4e1c45ea
PA
2984 if (ecs->event_thread->prev_pc == read_pc ()
2985 && ecs->event_thread->trap_expected
2986 && ecs->event_thread->step_resume_breakpoint == NULL)
68f53502
AC
2987 {
2988 /* We were just starting a new sequence, attempting to
2989 single-step off of a breakpoint and expecting a SIGTRAP.
237fc4c9 2990 Instead this signal arrives. This signal will take us out
68f53502
AC
2991 of the stepping range so GDB needs to remember to, when
2992 the signal handler returns, resume stepping off that
2993 breakpoint. */
2994 /* To simplify things, "continue" is forced to use the same
2995 code paths as single-step - set a breakpoint at the
2996 signal return address and then, once hit, step off that
2997 breakpoint. */
237fc4c9
PA
2998 if (debug_infrun)
2999 fprintf_unfiltered (gdb_stdlog,
3000 "infrun: signal arrived while stepping over "
3001 "breakpoint\n");
d3169d93 3002
44cbf7b5 3003 insert_step_resume_breakpoint_at_frame (get_current_frame ());
4e1c45ea 3004 ecs->event_thread->step_after_step_resume_breakpoint = 1;
9d799f85
AC
3005 keep_going (ecs);
3006 return;
68f53502 3007 }
9d799f85 3008
4e1c45ea 3009 if (ecs->event_thread->step_range_end != 0
2020b7ab 3010 && ecs->event_thread->stop_signal != TARGET_SIGNAL_0
4e1c45ea
PA
3011 && (ecs->event_thread->step_range_start <= stop_pc
3012 && stop_pc < ecs->event_thread->step_range_end)
9d799f85 3013 && frame_id_eq (get_frame_id (get_current_frame ()),
4e1c45ea
PA
3014 ecs->event_thread->step_frame_id)
3015 && ecs->event_thread->step_resume_breakpoint == NULL)
d303a6c7
AC
3016 {
3017 /* The inferior is about to take a signal that will take it
3018 out of the single step range. Set a breakpoint at the
3019 current PC (which is presumably where the signal handler
3020 will eventually return) and then allow the inferior to
3021 run free.
3022
3023 Note that this is only needed for a signal delivered
3024 while in the single-step range. Nested signals aren't a
3025 problem as they eventually all return. */
237fc4c9
PA
3026 if (debug_infrun)
3027 fprintf_unfiltered (gdb_stdlog,
3028 "infrun: signal may take us out of "
3029 "single-step range\n");
3030
44cbf7b5 3031 insert_step_resume_breakpoint_at_frame (get_current_frame ());
9d799f85
AC
3032 keep_going (ecs);
3033 return;
d303a6c7 3034 }
9d799f85
AC
3035
3036 /* Note: step_resume_breakpoint may be non-NULL. This occures
3037 when either there's a nested signal, or when there's a
3038 pending signal enabled just as the signal handler returns
3039 (leaving the inferior at the step-resume-breakpoint without
3040 actually executing it). Either way continue until the
3041 breakpoint is really hit. */
488f131b
JB
3042 keep_going (ecs);
3043 return;
3044 }
3045
3046 /* Handle cases caused by hitting a breakpoint. */
3047 {
3048 CORE_ADDR jmp_buf_pc;
3049 struct bpstat_what what;
3050
347bddb7 3051 what = bpstat_what (ecs->event_thread->stop_bpstat);
488f131b
JB
3052
3053 if (what.call_dummy)
3054 {
3055 stop_stack_dummy = 1;
c5aa993b 3056 }
c906108c 3057
488f131b 3058 switch (what.main_action)
c5aa993b 3059 {
488f131b 3060 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
611c83ae
PA
3061 /* If we hit the breakpoint at longjmp while stepping, we
3062 install a momentary breakpoint at the target of the
3063 jmp_buf. */
3064
3065 if (debug_infrun)
3066 fprintf_unfiltered (gdb_stdlog,
3067 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME\n");
3068
4e1c45ea 3069 ecs->event_thread->stepping_over_breakpoint = 1;
611c83ae 3070
91104499 3071 if (!gdbarch_get_longjmp_target_p (current_gdbarch)
60ade65d
UW
3072 || !gdbarch_get_longjmp_target (current_gdbarch,
3073 get_current_frame (), &jmp_buf_pc))
c5aa993b 3074 {
611c83ae
PA
3075 if (debug_infrun)
3076 fprintf_unfiltered (gdb_stdlog, "\
3077infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME (!gdbarch_get_longjmp_target)\n");
488f131b 3078 keep_going (ecs);
104c1213 3079 return;
c5aa993b 3080 }
488f131b 3081
611c83ae
PA
3082 /* We're going to replace the current step-resume breakpoint
3083 with a longjmp-resume breakpoint. */
4e1c45ea 3084 delete_step_resume_breakpoint (ecs->event_thread);
611c83ae
PA
3085
3086 /* Insert a breakpoint at resume address. */
3087 insert_longjmp_resume_breakpoint (jmp_buf_pc);
c906108c 3088
488f131b
JB
3089 keep_going (ecs);
3090 return;
c906108c 3091
488f131b 3092 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
527159b7 3093 if (debug_infrun)
611c83ae
PA
3094 fprintf_unfiltered (gdb_stdlog,
3095 "infrun: BPSTAT_WHAT_CLEAR_LONGJMP_RESUME\n");
3096
4e1c45ea
PA
3097 gdb_assert (ecs->event_thread->step_resume_breakpoint != NULL);
3098 delete_step_resume_breakpoint (ecs->event_thread);
611c83ae 3099
414c69f7 3100 ecs->event_thread->stop_step = 1;
611c83ae
PA
3101 print_stop_reason (END_STEPPING_RANGE, 0);
3102 stop_stepping (ecs);
3103 return;
488f131b
JB
3104
3105 case BPSTAT_WHAT_SINGLE:
527159b7 3106 if (debug_infrun)
8802d8ed 3107 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SINGLE\n");
4e1c45ea 3108 ecs->event_thread->stepping_over_breakpoint = 1;
488f131b
JB
3109 /* Still need to check other stuff, at least the case
3110 where we are stepping and step out of the right range. */
3111 break;
c906108c 3112
488f131b 3113 case BPSTAT_WHAT_STOP_NOISY:
527159b7 3114 if (debug_infrun)
8802d8ed 3115 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_NOISY\n");
488f131b 3116 stop_print_frame = 1;
c906108c 3117
d303a6c7
AC
3118 /* We are about to nuke the step_resume_breakpointt via the
3119 cleanup chain, so no need to worry about it here. */
c5aa993b 3120
488f131b
JB
3121 stop_stepping (ecs);
3122 return;
c5aa993b 3123
488f131b 3124 case BPSTAT_WHAT_STOP_SILENT:
527159b7 3125 if (debug_infrun)
8802d8ed 3126 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_SILENT\n");
488f131b 3127 stop_print_frame = 0;
c5aa993b 3128
d303a6c7
AC
3129 /* We are about to nuke the step_resume_breakpoin via the
3130 cleanup chain, so no need to worry about it here. */
c5aa993b 3131
488f131b 3132 stop_stepping (ecs);
e441088d 3133 return;
c5aa993b 3134
488f131b 3135 case BPSTAT_WHAT_STEP_RESUME:
527159b7 3136 if (debug_infrun)
8802d8ed 3137 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STEP_RESUME\n");
527159b7 3138
4e1c45ea
PA
3139 delete_step_resume_breakpoint (ecs->event_thread);
3140 if (ecs->event_thread->step_after_step_resume_breakpoint)
68f53502
AC
3141 {
3142 /* Back when the step-resume breakpoint was inserted, we
3143 were trying to single-step off a breakpoint. Go back
3144 to doing that. */
4e1c45ea
PA
3145 ecs->event_thread->step_after_step_resume_breakpoint = 0;
3146 ecs->event_thread->stepping_over_breakpoint = 1;
68f53502
AC
3147 keep_going (ecs);
3148 return;
3149 }
b2175913
MS
3150 if (stop_pc == ecs->stop_func_start
3151 && execution_direction == EXEC_REVERSE)
3152 {
3153 /* We are stepping over a function call in reverse, and
3154 just hit the step-resume breakpoint at the start
3155 address of the function. Go back to single-stepping,
3156 which should take us back to the function call. */
3157 ecs->event_thread->stepping_over_breakpoint = 1;
3158 keep_going (ecs);
3159 return;
3160 }
488f131b
JB
3161 break;
3162
488f131b 3163 case BPSTAT_WHAT_CHECK_SHLIBS:
c906108c 3164 {
527159b7 3165 if (debug_infrun)
8802d8ed 3166 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_CHECK_SHLIBS\n");
488f131b
JB
3167
3168 /* Check for any newly added shared libraries if we're
3169 supposed to be adding them automatically. Switch
3170 terminal for any messages produced by
3171 breakpoint_re_set. */
3172 target_terminal_ours_for_output ();
aff6338a 3173 /* NOTE: cagney/2003-11-25: Make certain that the target
8fb3e588
AC
3174 stack's section table is kept up-to-date. Architectures,
3175 (e.g., PPC64), use the section table to perform
3176 operations such as address => section name and hence
3177 require the table to contain all sections (including
3178 those found in shared libraries). */
aff6338a 3179 /* NOTE: cagney/2003-11-25: Pass current_target and not
8fb3e588
AC
3180 exec_ops to SOLIB_ADD. This is because current GDB is
3181 only tooled to propagate section_table changes out from
3182 the "current_target" (see target_resize_to_sections), and
3183 not up from the exec stratum. This, of course, isn't
3184 right. "infrun.c" should only interact with the
3185 exec/process stratum, instead relying on the target stack
3186 to propagate relevant changes (stop, section table
3187 changed, ...) up to other layers. */
a77053c2 3188#ifdef SOLIB_ADD
aff6338a 3189 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
a77053c2
MK
3190#else
3191 solib_add (NULL, 0, &current_target, auto_solib_add);
3192#endif
488f131b
JB
3193 target_terminal_inferior ();
3194
488f131b
JB
3195 /* If requested, stop when the dynamic linker notifies
3196 gdb of events. This allows the user to get control
3197 and place breakpoints in initializer routines for
3198 dynamically loaded objects (among other things). */
877522db 3199 if (stop_on_solib_events || stop_stack_dummy)
d4f3574e 3200 {
488f131b 3201 stop_stepping (ecs);
d4f3574e
SS
3202 return;
3203 }
c5aa993b 3204 else
c5aa993b 3205 {
488f131b 3206 /* We want to step over this breakpoint, then keep going. */
4e1c45ea 3207 ecs->event_thread->stepping_over_breakpoint = 1;
488f131b 3208 break;
c5aa993b 3209 }
488f131b 3210 }
488f131b 3211 break;
c906108c 3212
488f131b
JB
3213 case BPSTAT_WHAT_LAST:
3214 /* Not a real code, but listed here to shut up gcc -Wall. */
c906108c 3215
488f131b
JB
3216 case BPSTAT_WHAT_KEEP_CHECKING:
3217 break;
3218 }
3219 }
c906108c 3220
488f131b
JB
3221 /* We come here if we hit a breakpoint but should not
3222 stop for it. Possibly we also were stepping
3223 and should stop for that. So fall through and
3224 test for stepping. But, if not stepping,
3225 do not stop. */
c906108c 3226
a7212384
UW
3227 /* In all-stop mode, if we're currently stepping but have stopped in
3228 some other thread, we need to switch back to the stepped thread. */
3229 if (!non_stop)
3230 {
3231 struct thread_info *tp;
3232 tp = iterate_over_threads (currently_stepping_callback,
3233 ecs->event_thread);
3234 if (tp)
3235 {
3236 /* However, if the current thread is blocked on some internal
3237 breakpoint, and we simply need to step over that breakpoint
3238 to get it going again, do that first. */
3239 if ((ecs->event_thread->trap_expected
3240 && ecs->event_thread->stop_signal != TARGET_SIGNAL_TRAP)
3241 || ecs->event_thread->stepping_over_breakpoint)
3242 {
3243 keep_going (ecs);
3244 return;
3245 }
3246
3247 /* Otherwise, we no longer expect a trap in the current thread.
3248 Clear the trap_expected flag before switching back -- this is
3249 what keep_going would do as well, if we called it. */
3250 ecs->event_thread->trap_expected = 0;
3251
3252 if (debug_infrun)
3253 fprintf_unfiltered (gdb_stdlog,
3254 "infrun: switching back to stepped thread\n");
3255
3256 ecs->event_thread = tp;
3257 ecs->ptid = tp->ptid;
3258 context_switch (ecs->ptid);
3259 keep_going (ecs);
3260 return;
3261 }
3262 }
3263
9d1ff73f
MS
3264 /* Are we stepping to get the inferior out of the dynamic linker's
3265 hook (and possibly the dld itself) after catching a shlib
3266 event? */
4e1c45ea 3267 if (ecs->event_thread->stepping_through_solib_after_catch)
488f131b
JB
3268 {
3269#if defined(SOLIB_ADD)
3270 /* Have we reached our destination? If not, keep going. */
3271 if (SOLIB_IN_DYNAMIC_LINKER (PIDGET (ecs->ptid), stop_pc))
3272 {
527159b7 3273 if (debug_infrun)
8a9de0e4 3274 fprintf_unfiltered (gdb_stdlog, "infrun: stepping in dynamic linker\n");
4e1c45ea 3275 ecs->event_thread->stepping_over_breakpoint = 1;
488f131b 3276 keep_going (ecs);
104c1213 3277 return;
488f131b
JB
3278 }
3279#endif
527159b7 3280 if (debug_infrun)
8a9de0e4 3281 fprintf_unfiltered (gdb_stdlog, "infrun: step past dynamic linker\n");
488f131b
JB
3282 /* Else, stop and report the catchpoint(s) whose triggering
3283 caused us to begin stepping. */
4e1c45ea 3284 ecs->event_thread->stepping_through_solib_after_catch = 0;
347bddb7
PA
3285 bpstat_clear (&ecs->event_thread->stop_bpstat);
3286 ecs->event_thread->stop_bpstat
3287 = bpstat_copy (ecs->event_thread->stepping_through_solib_catchpoints);
4e1c45ea 3288 bpstat_clear (&ecs->event_thread->stepping_through_solib_catchpoints);
488f131b
JB
3289 stop_print_frame = 1;
3290 stop_stepping (ecs);
3291 return;
3292 }
c906108c 3293
4e1c45ea 3294 if (ecs->event_thread->step_resume_breakpoint)
488f131b 3295 {
527159b7 3296 if (debug_infrun)
d3169d93
DJ
3297 fprintf_unfiltered (gdb_stdlog,
3298 "infrun: step-resume breakpoint is inserted\n");
527159b7 3299
488f131b
JB
3300 /* Having a step-resume breakpoint overrides anything
3301 else having to do with stepping commands until
3302 that breakpoint is reached. */
488f131b
JB
3303 keep_going (ecs);
3304 return;
3305 }
c5aa993b 3306
4e1c45ea 3307 if (ecs->event_thread->step_range_end == 0)
488f131b 3308 {
527159b7 3309 if (debug_infrun)
8a9de0e4 3310 fprintf_unfiltered (gdb_stdlog, "infrun: no stepping, continue\n");
488f131b 3311 /* Likewise if we aren't even stepping. */
488f131b
JB
3312 keep_going (ecs);
3313 return;
3314 }
c5aa993b 3315
488f131b 3316 /* If stepping through a line, keep going if still within it.
c906108c 3317
488f131b
JB
3318 Note that step_range_end is the address of the first instruction
3319 beyond the step range, and NOT the address of the last instruction
3320 within it! */
4e1c45ea
PA
3321 if (stop_pc >= ecs->event_thread->step_range_start
3322 && stop_pc < ecs->event_thread->step_range_end)
488f131b 3323 {
527159b7 3324 if (debug_infrun)
b2175913 3325 fprintf_unfiltered (gdb_stdlog, "infrun: stepping inside range [0x%s-0x%s]\n",
4e1c45ea
PA
3326 paddr_nz (ecs->event_thread->step_range_start),
3327 paddr_nz (ecs->event_thread->step_range_end));
b2175913
MS
3328
3329 /* When stepping backward, stop at beginning of line range
3330 (unless it's the function entry point, in which case
3331 keep going back to the call point). */
3332 if (stop_pc == ecs->event_thread->step_range_start
3333 && stop_pc != ecs->stop_func_start
3334 && execution_direction == EXEC_REVERSE)
3335 {
3336 ecs->event_thread->stop_step = 1;
3337 print_stop_reason (END_STEPPING_RANGE, 0);
3338 stop_stepping (ecs);
3339 }
3340 else
3341 keep_going (ecs);
3342
488f131b
JB
3343 return;
3344 }
c5aa993b 3345
488f131b 3346 /* We stepped out of the stepping range. */
c906108c 3347
488f131b
JB
3348 /* If we are stepping at the source level and entered the runtime
3349 loader dynamic symbol resolution code, we keep on single stepping
3350 until we exit the run time loader code and reach the callee's
3351 address. */
078130d0 3352 if (ecs->event_thread->step_over_calls == STEP_OVER_UNDEBUGGABLE
cfd8ab24 3353 && in_solib_dynsym_resolve_code (stop_pc))
488f131b 3354 {
4c8c40e6
MK
3355 CORE_ADDR pc_after_resolver =
3356 gdbarch_skip_solib_resolver (current_gdbarch, stop_pc);
c906108c 3357
527159b7 3358 if (debug_infrun)
8a9de0e4 3359 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into dynsym resolve code\n");
527159b7 3360
488f131b
JB
3361 if (pc_after_resolver)
3362 {
3363 /* Set up a step-resume breakpoint at the address
3364 indicated by SKIP_SOLIB_RESOLVER. */
3365 struct symtab_and_line sr_sal;
fe39c653 3366 init_sal (&sr_sal);
488f131b
JB
3367 sr_sal.pc = pc_after_resolver;
3368
44cbf7b5 3369 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
c5aa993b 3370 }
c906108c 3371
488f131b
JB
3372 keep_going (ecs);
3373 return;
3374 }
c906108c 3375
4e1c45ea 3376 if (ecs->event_thread->step_range_end != 1
078130d0
PA
3377 && (ecs->event_thread->step_over_calls == STEP_OVER_UNDEBUGGABLE
3378 || ecs->event_thread->step_over_calls == STEP_OVER_ALL)
42edda50 3379 && get_frame_type (get_current_frame ()) == SIGTRAMP_FRAME)
488f131b 3380 {
527159b7 3381 if (debug_infrun)
8a9de0e4 3382 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into signal trampoline\n");
42edda50 3383 /* The inferior, while doing a "step" or "next", has ended up in
8fb3e588
AC
3384 a signal trampoline (either by a signal being delivered or by
3385 the signal handler returning). Just single-step until the
3386 inferior leaves the trampoline (either by calling the handler
3387 or returning). */
488f131b
JB
3388 keep_going (ecs);
3389 return;
3390 }
c906108c 3391
c17eaafe
DJ
3392 /* Check for subroutine calls. The check for the current frame
3393 equalling the step ID is not necessary - the check of the
3394 previous frame's ID is sufficient - but it is a common case and
3395 cheaper than checking the previous frame's ID.
14e60db5
DJ
3396
3397 NOTE: frame_id_eq will never report two invalid frame IDs as
3398 being equal, so to get into this block, both the current and
3399 previous frame must have valid frame IDs. */
4e1c45ea
PA
3400 if (!frame_id_eq (get_frame_id (get_current_frame ()),
3401 ecs->event_thread->step_frame_id)
b2175913
MS
3402 && (frame_id_eq (frame_unwind_id (get_current_frame ()),
3403 ecs->event_thread->step_frame_id)
3404 || execution_direction == EXEC_REVERSE))
488f131b 3405 {
95918acb 3406 CORE_ADDR real_stop_pc;
8fb3e588 3407
527159b7 3408 if (debug_infrun)
8a9de0e4 3409 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into subroutine\n");
527159b7 3410
078130d0 3411 if ((ecs->event_thread->step_over_calls == STEP_OVER_NONE)
4e1c45ea
PA
3412 || ((ecs->event_thread->step_range_end == 1)
3413 && in_prologue (ecs->event_thread->prev_pc,
3414 ecs->stop_func_start)))
95918acb
AC
3415 {
3416 /* I presume that step_over_calls is only 0 when we're
3417 supposed to be stepping at the assembly language level
3418 ("stepi"). Just stop. */
3419 /* Also, maybe we just did a "nexti" inside a prolog, so we
3420 thought it was a subroutine call but it was not. Stop as
3421 well. FENN */
414c69f7 3422 ecs->event_thread->stop_step = 1;
95918acb
AC
3423 print_stop_reason (END_STEPPING_RANGE, 0);
3424 stop_stepping (ecs);
3425 return;
3426 }
8fb3e588 3427
078130d0 3428 if (ecs->event_thread->step_over_calls == STEP_OVER_ALL)
8567c30f 3429 {
b2175913
MS
3430 /* We're doing a "next".
3431
3432 Normal (forward) execution: set a breakpoint at the
3433 callee's return address (the address at which the caller
3434 will resume).
3435
3436 Reverse (backward) execution. set the step-resume
3437 breakpoint at the start of the function that we just
3438 stepped into (backwards), and continue to there. When we
6130d0b7 3439 get there, we'll need to single-step back to the caller. */
b2175913
MS
3440
3441 if (execution_direction == EXEC_REVERSE)
3442 {
3443 struct symtab_and_line sr_sal;
3067f6e5
MS
3444
3445 if (ecs->stop_func_start == 0
3446 && in_solib_dynsym_resolve_code (stop_pc))
3447 {
3448 /* Stepped into runtime loader dynamic symbol
3449 resolution code. Since we're in reverse,
3450 we have already backed up through the runtime
3451 loader and the dynamic function. This is just
3452 the trampoline (jump table).
3453
3454 Just keep stepping, we'll soon be home.
3455 */
3456 keep_going (ecs);
3457 return;
3458 }
3459 /* Normal (staticly linked) function call return. */
b2175913
MS
3460 init_sal (&sr_sal);
3461 sr_sal.pc = ecs->stop_func_start;
3462 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
3463 }
3464 else
3465 insert_step_resume_breakpoint_at_caller (get_current_frame ());
3466
8567c30f
AC
3467 keep_going (ecs);
3468 return;
3469 }
a53c66de 3470
95918acb 3471 /* If we are in a function call trampoline (a stub between the
8fb3e588
AC
3472 calling routine and the real function), locate the real
3473 function. That's what tells us (a) whether we want to step
3474 into it at all, and (b) what prologue we want to run to the
3475 end of, if we do step into it. */
52f729a7 3476 real_stop_pc = skip_language_trampoline (get_current_frame (), stop_pc);
95918acb 3477 if (real_stop_pc == 0)
52f729a7
UW
3478 real_stop_pc = gdbarch_skip_trampoline_code
3479 (current_gdbarch, get_current_frame (), stop_pc);
95918acb
AC
3480 if (real_stop_pc != 0)
3481 ecs->stop_func_start = real_stop_pc;
8fb3e588 3482
db5f024e 3483 if (real_stop_pc != 0 && in_solib_dynsym_resolve_code (real_stop_pc))
1b2bfbb9
RC
3484 {
3485 struct symtab_and_line sr_sal;
3486 init_sal (&sr_sal);
3487 sr_sal.pc = ecs->stop_func_start;
3488
44cbf7b5 3489 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
8fb3e588
AC
3490 keep_going (ecs);
3491 return;
1b2bfbb9
RC
3492 }
3493
95918acb 3494 /* If we have line number information for the function we are
8fb3e588 3495 thinking of stepping into, step into it.
95918acb 3496
8fb3e588
AC
3497 If there are several symtabs at that PC (e.g. with include
3498 files), just want to know whether *any* of them have line
3499 numbers. find_pc_line handles this. */
95918acb
AC
3500 {
3501 struct symtab_and_line tmp_sal;
8fb3e588 3502
95918acb
AC
3503 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
3504 if (tmp_sal.line != 0)
3505 {
b2175913
MS
3506 if (execution_direction == EXEC_REVERSE)
3507 handle_step_into_function_backward (ecs);
3508 else
3509 handle_step_into_function (ecs);
95918acb
AC
3510 return;
3511 }
3512 }
3513
3514 /* If we have no line number and the step-stop-if-no-debug is
8fb3e588
AC
3515 set, we stop the step so that the user has a chance to switch
3516 in assembly mode. */
078130d0
PA
3517 if (ecs->event_thread->step_over_calls == STEP_OVER_UNDEBUGGABLE
3518 && step_stop_if_no_debug)
95918acb 3519 {
414c69f7 3520 ecs->event_thread->stop_step = 1;
95918acb
AC
3521 print_stop_reason (END_STEPPING_RANGE, 0);
3522 stop_stepping (ecs);
3523 return;
3524 }
3525
b2175913
MS
3526 if (execution_direction == EXEC_REVERSE)
3527 {
3528 /* Set a breakpoint at callee's start address.
3529 From there we can step once and be back in the caller. */
3530 struct symtab_and_line sr_sal;
3531 init_sal (&sr_sal);
3532 sr_sal.pc = ecs->stop_func_start;
3533 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
3534 }
3535 else
3536 /* Set a breakpoint at callee's return address (the address
3537 at which the caller will resume). */
3538 insert_step_resume_breakpoint_at_caller (get_current_frame ());
3539
95918acb 3540 keep_going (ecs);
488f131b 3541 return;
488f131b 3542 }
c906108c 3543
488f131b
JB
3544 /* If we're in the return path from a shared library trampoline,
3545 we want to proceed through the trampoline when stepping. */
e76f05fa
UW
3546 if (gdbarch_in_solib_return_trampoline (current_gdbarch,
3547 stop_pc, ecs->stop_func_name))
488f131b 3548 {
488f131b 3549 /* Determine where this trampoline returns. */
52f729a7
UW
3550 CORE_ADDR real_stop_pc;
3551 real_stop_pc = gdbarch_skip_trampoline_code
3552 (current_gdbarch, get_current_frame (), stop_pc);
c906108c 3553
527159b7 3554 if (debug_infrun)
8a9de0e4 3555 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into solib return tramp\n");
527159b7 3556
488f131b 3557 /* Only proceed through if we know where it's going. */
d764a824 3558 if (real_stop_pc)
488f131b
JB
3559 {
3560 /* And put the step-breakpoint there and go until there. */
3561 struct symtab_and_line sr_sal;
3562
fe39c653 3563 init_sal (&sr_sal); /* initialize to zeroes */
d764a824 3564 sr_sal.pc = real_stop_pc;
488f131b 3565 sr_sal.section = find_pc_overlay (sr_sal.pc);
44cbf7b5
AC
3566
3567 /* Do not specify what the fp should be when we stop since
3568 on some machines the prologue is where the new fp value
3569 is established. */
3570 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
c906108c 3571
488f131b
JB
3572 /* Restart without fiddling with the step ranges or
3573 other state. */
3574 keep_going (ecs);
3575 return;
3576 }
3577 }
c906108c 3578
2afb61aa 3579 stop_pc_sal = find_pc_line (stop_pc, 0);
7ed0fe66 3580
1b2bfbb9
RC
3581 /* NOTE: tausq/2004-05-24: This if block used to be done before all
3582 the trampoline processing logic, however, there are some trampolines
3583 that have no names, so we should do trampoline handling first. */
078130d0 3584 if (ecs->event_thread->step_over_calls == STEP_OVER_UNDEBUGGABLE
7ed0fe66 3585 && ecs->stop_func_name == NULL
2afb61aa 3586 && stop_pc_sal.line == 0)
1b2bfbb9 3587 {
527159b7 3588 if (debug_infrun)
8a9de0e4 3589 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into undebuggable function\n");
527159b7 3590
1b2bfbb9 3591 /* The inferior just stepped into, or returned to, an
7ed0fe66
DJ
3592 undebuggable function (where there is no debugging information
3593 and no line number corresponding to the address where the
1b2bfbb9
RC
3594 inferior stopped). Since we want to skip this kind of code,
3595 we keep going until the inferior returns from this
14e60db5
DJ
3596 function - unless the user has asked us not to (via
3597 set step-mode) or we no longer know how to get back
3598 to the call site. */
3599 if (step_stop_if_no_debug
eb2f4a08 3600 || !frame_id_p (frame_unwind_id (get_current_frame ())))
1b2bfbb9
RC
3601 {
3602 /* If we have no line number and the step-stop-if-no-debug
3603 is set, we stop the step so that the user has a chance to
3604 switch in assembly mode. */
414c69f7 3605 ecs->event_thread->stop_step = 1;
1b2bfbb9
RC
3606 print_stop_reason (END_STEPPING_RANGE, 0);
3607 stop_stepping (ecs);
3608 return;
3609 }
3610 else
3611 {
3612 /* Set a breakpoint at callee's return address (the address
3613 at which the caller will resume). */
14e60db5 3614 insert_step_resume_breakpoint_at_caller (get_current_frame ());
1b2bfbb9
RC
3615 keep_going (ecs);
3616 return;
3617 }
3618 }
3619
4e1c45ea 3620 if (ecs->event_thread->step_range_end == 1)
1b2bfbb9
RC
3621 {
3622 /* It is stepi or nexti. We always want to stop stepping after
3623 one instruction. */
527159b7 3624 if (debug_infrun)
8a9de0e4 3625 fprintf_unfiltered (gdb_stdlog, "infrun: stepi/nexti\n");
414c69f7 3626 ecs->event_thread->stop_step = 1;
1b2bfbb9
RC
3627 print_stop_reason (END_STEPPING_RANGE, 0);
3628 stop_stepping (ecs);
3629 return;
3630 }
3631
2afb61aa 3632 if (stop_pc_sal.line == 0)
488f131b
JB
3633 {
3634 /* We have no line number information. That means to stop
3635 stepping (does this always happen right after one instruction,
3636 when we do "s" in a function with no line numbers,
3637 or can this happen as a result of a return or longjmp?). */
527159b7 3638 if (debug_infrun)
8a9de0e4 3639 fprintf_unfiltered (gdb_stdlog, "infrun: no line number info\n");
414c69f7 3640 ecs->event_thread->stop_step = 1;
488f131b
JB
3641 print_stop_reason (END_STEPPING_RANGE, 0);
3642 stop_stepping (ecs);
3643 return;
3644 }
c906108c 3645
2afb61aa 3646 if ((stop_pc == stop_pc_sal.pc)
4e1c45ea
PA
3647 && (ecs->event_thread->current_line != stop_pc_sal.line
3648 || ecs->event_thread->current_symtab != stop_pc_sal.symtab))
488f131b
JB
3649 {
3650 /* We are at the start of a different line. So stop. Note that
3651 we don't stop if we step into the middle of a different line.
3652 That is said to make things like for (;;) statements work
3653 better. */
527159b7 3654 if (debug_infrun)
8a9de0e4 3655 fprintf_unfiltered (gdb_stdlog, "infrun: stepped to a different line\n");
414c69f7 3656 ecs->event_thread->stop_step = 1;
488f131b
JB
3657 print_stop_reason (END_STEPPING_RANGE, 0);
3658 stop_stepping (ecs);
3659 return;
3660 }
c906108c 3661
488f131b 3662 /* We aren't done stepping.
c906108c 3663
488f131b
JB
3664 Optimize by setting the stepping range to the line.
3665 (We might not be in the original line, but if we entered a
3666 new line in mid-statement, we continue stepping. This makes
3667 things like for(;;) statements work better.) */
c906108c 3668
4e1c45ea
PA
3669 ecs->event_thread->step_range_start = stop_pc_sal.pc;
3670 ecs->event_thread->step_range_end = stop_pc_sal.end;
3671 ecs->event_thread->step_frame_id = get_frame_id (get_current_frame ());
3672 ecs->event_thread->current_line = stop_pc_sal.line;
3673 ecs->event_thread->current_symtab = stop_pc_sal.symtab;
488f131b 3674
527159b7 3675 if (debug_infrun)
8a9de0e4 3676 fprintf_unfiltered (gdb_stdlog, "infrun: keep going\n");
488f131b 3677 keep_going (ecs);
104c1213
JM
3678}
3679
3680/* Are we in the middle of stepping? */
3681
a7212384
UW
3682static int
3683currently_stepping_thread (struct thread_info *tp)
3684{
3685 return (tp->step_range_end && tp->step_resume_breakpoint == NULL)
3686 || tp->trap_expected
3687 || tp->stepping_through_solib_after_catch;
3688}
3689
3690static int
3691currently_stepping_callback (struct thread_info *tp, void *data)
3692{
3693 /* Return true if any thread *but* the one passed in "data" is
3694 in the middle of stepping. */
3695 return tp != data && currently_stepping_thread (tp);
3696}
3697
104c1213 3698static int
4e1c45ea 3699currently_stepping (struct thread_info *tp)
104c1213 3700{
a7212384 3701 return currently_stepping_thread (tp) || bpstat_should_step ();
104c1213 3702}
c906108c 3703
b2175913
MS
3704/* Inferior has stepped into a subroutine call with source code that
3705 we should not step over. Do step to the first line of code in
3706 it. */
c2c6d25f
JM
3707
3708static void
b2175913 3709handle_step_into_function (struct execution_control_state *ecs)
c2c6d25f
JM
3710{
3711 struct symtab *s;
2afb61aa 3712 struct symtab_and_line stop_func_sal, sr_sal;
c2c6d25f
JM
3713
3714 s = find_pc_symtab (stop_pc);
3715 if (s && s->language != language_asm)
b2175913
MS
3716 ecs->stop_func_start = gdbarch_skip_prologue (current_gdbarch,
3717 ecs->stop_func_start);
c2c6d25f 3718
2afb61aa 3719 stop_func_sal = find_pc_line (ecs->stop_func_start, 0);
c2c6d25f
JM
3720 /* Use the step_resume_break to step until the end of the prologue,
3721 even if that involves jumps (as it seems to on the vax under
3722 4.2). */
3723 /* If the prologue ends in the middle of a source line, continue to
3724 the end of that source line (if it is still within the function).
3725 Otherwise, just go to end of prologue. */
2afb61aa
PA
3726 if (stop_func_sal.end
3727 && stop_func_sal.pc != ecs->stop_func_start
3728 && stop_func_sal.end < ecs->stop_func_end)
3729 ecs->stop_func_start = stop_func_sal.end;
c2c6d25f 3730
2dbd5e30
KB
3731 /* Architectures which require breakpoint adjustment might not be able
3732 to place a breakpoint at the computed address. If so, the test
3733 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
3734 ecs->stop_func_start to an address at which a breakpoint may be
3735 legitimately placed.
8fb3e588 3736
2dbd5e30
KB
3737 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
3738 made, GDB will enter an infinite loop when stepping through
3739 optimized code consisting of VLIW instructions which contain
3740 subinstructions corresponding to different source lines. On
3741 FR-V, it's not permitted to place a breakpoint on any but the
3742 first subinstruction of a VLIW instruction. When a breakpoint is
3743 set, GDB will adjust the breakpoint address to the beginning of
3744 the VLIW instruction. Thus, we need to make the corresponding
3745 adjustment here when computing the stop address. */
8fb3e588 3746
2dbd5e30
KB
3747 if (gdbarch_adjust_breakpoint_address_p (current_gdbarch))
3748 {
3749 ecs->stop_func_start
3750 = gdbarch_adjust_breakpoint_address (current_gdbarch,
8fb3e588 3751 ecs->stop_func_start);
2dbd5e30
KB
3752 }
3753
c2c6d25f
JM
3754 if (ecs->stop_func_start == stop_pc)
3755 {
3756 /* We are already there: stop now. */
414c69f7 3757 ecs->event_thread->stop_step = 1;
488f131b 3758 print_stop_reason (END_STEPPING_RANGE, 0);
c2c6d25f
JM
3759 stop_stepping (ecs);
3760 return;
3761 }
3762 else
3763 {
3764 /* Put the step-breakpoint there and go until there. */
fe39c653 3765 init_sal (&sr_sal); /* initialize to zeroes */
c2c6d25f
JM
3766 sr_sal.pc = ecs->stop_func_start;
3767 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
44cbf7b5 3768
c2c6d25f 3769 /* Do not specify what the fp should be when we stop since on
488f131b
JB
3770 some machines the prologue is where the new fp value is
3771 established. */
44cbf7b5 3772 insert_step_resume_breakpoint_at_sal (sr_sal, null_frame_id);
c2c6d25f
JM
3773
3774 /* And make sure stepping stops right away then. */
4e1c45ea 3775 ecs->event_thread->step_range_end = ecs->event_thread->step_range_start;
c2c6d25f
JM
3776 }
3777 keep_going (ecs);
3778}
d4f3574e 3779
b2175913
MS
3780/* Inferior has stepped backward into a subroutine call with source
3781 code that we should not step over. Do step to the beginning of the
3782 last line of code in it. */
3783
3784static void
3785handle_step_into_function_backward (struct execution_control_state *ecs)
3786{
3787 struct symtab *s;
3788 struct symtab_and_line stop_func_sal, sr_sal;
3789
3790 s = find_pc_symtab (stop_pc);
3791 if (s && s->language != language_asm)
3792 ecs->stop_func_start = gdbarch_skip_prologue (current_gdbarch,
3793 ecs->stop_func_start);
3794
3795 stop_func_sal = find_pc_line (stop_pc, 0);
3796
3797 /* OK, we're just going to keep stepping here. */
3798 if (stop_func_sal.pc == stop_pc)
3799 {
3800 /* We're there already. Just stop stepping now. */
3801 ecs->event_thread->stop_step = 1;
3802 print_stop_reason (END_STEPPING_RANGE, 0);
3803 stop_stepping (ecs);
3804 }
3805 else
3806 {
3807 /* Else just reset the step range and keep going.
3808 No step-resume breakpoint, they don't work for
3809 epilogues, which can have multiple entry paths. */
3810 ecs->event_thread->step_range_start = stop_func_sal.pc;
3811 ecs->event_thread->step_range_end = stop_func_sal.end;
3812 keep_going (ecs);
3813 }
3814 return;
3815}
3816
d3169d93 3817/* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
44cbf7b5
AC
3818 This is used to both functions and to skip over code. */
3819
3820static void
3821insert_step_resume_breakpoint_at_sal (struct symtab_and_line sr_sal,
3822 struct frame_id sr_id)
3823{
611c83ae
PA
3824 /* There should never be more than one step-resume or longjmp-resume
3825 breakpoint per thread, so we should never be setting a new
44cbf7b5 3826 step_resume_breakpoint when one is already active. */
4e1c45ea 3827 gdb_assert (inferior_thread ()->step_resume_breakpoint == NULL);
d3169d93
DJ
3828
3829 if (debug_infrun)
3830 fprintf_unfiltered (gdb_stdlog,
3831 "infrun: inserting step-resume breakpoint at 0x%s\n",
3832 paddr_nz (sr_sal.pc));
3833
4e1c45ea
PA
3834 inferior_thread ()->step_resume_breakpoint
3835 = set_momentary_breakpoint (sr_sal, sr_id, bp_step_resume);
44cbf7b5 3836}
7ce450bd 3837
d3169d93 3838/* Insert a "step-resume breakpoint" at RETURN_FRAME.pc. This is used
14e60db5 3839 to skip a potential signal handler.
7ce450bd 3840
14e60db5
DJ
3841 This is called with the interrupted function's frame. The signal
3842 handler, when it returns, will resume the interrupted function at
3843 RETURN_FRAME.pc. */
d303a6c7
AC
3844
3845static void
44cbf7b5 3846insert_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
d303a6c7
AC
3847{
3848 struct symtab_and_line sr_sal;
3849
f4c1edd8 3850 gdb_assert (return_frame != NULL);
d303a6c7
AC
3851 init_sal (&sr_sal); /* initialize to zeros */
3852
bf6ae464
UW
3853 sr_sal.pc = gdbarch_addr_bits_remove
3854 (current_gdbarch, get_frame_pc (return_frame));
d303a6c7
AC
3855 sr_sal.section = find_pc_overlay (sr_sal.pc);
3856
44cbf7b5 3857 insert_step_resume_breakpoint_at_sal (sr_sal, get_frame_id (return_frame));
d303a6c7
AC
3858}
3859
14e60db5
DJ
3860/* Similar to insert_step_resume_breakpoint_at_frame, except
3861 but a breakpoint at the previous frame's PC. This is used to
3862 skip a function after stepping into it (for "next" or if the called
3863 function has no debugging information).
3864
3865 The current function has almost always been reached by single
3866 stepping a call or return instruction. NEXT_FRAME belongs to the
3867 current function, and the breakpoint will be set at the caller's
3868 resume address.
3869
3870 This is a separate function rather than reusing
3871 insert_step_resume_breakpoint_at_frame in order to avoid
3872 get_prev_frame, which may stop prematurely (see the implementation
eb2f4a08 3873 of frame_unwind_id for an example). */
14e60db5
DJ
3874
3875static void
3876insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
3877{
3878 struct symtab_and_line sr_sal;
3879
3880 /* We shouldn't have gotten here if we don't know where the call site
3881 is. */
eb2f4a08 3882 gdb_assert (frame_id_p (frame_unwind_id (next_frame)));
14e60db5
DJ
3883
3884 init_sal (&sr_sal); /* initialize to zeros */
3885
bf6ae464 3886 sr_sal.pc = gdbarch_addr_bits_remove
eb2f4a08 3887 (current_gdbarch, frame_pc_unwind (next_frame));
14e60db5
DJ
3888 sr_sal.section = find_pc_overlay (sr_sal.pc);
3889
eb2f4a08 3890 insert_step_resume_breakpoint_at_sal (sr_sal, frame_unwind_id (next_frame));
14e60db5
DJ
3891}
3892
611c83ae
PA
3893/* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
3894 new breakpoint at the target of a jmp_buf. The handling of
3895 longjmp-resume uses the same mechanisms used for handling
3896 "step-resume" breakpoints. */
3897
3898static void
3899insert_longjmp_resume_breakpoint (CORE_ADDR pc)
3900{
3901 /* There should never be more than one step-resume or longjmp-resume
3902 breakpoint per thread, so we should never be setting a new
3903 longjmp_resume_breakpoint when one is already active. */
4e1c45ea 3904 gdb_assert (inferior_thread ()->step_resume_breakpoint == NULL);
611c83ae
PA
3905
3906 if (debug_infrun)
3907 fprintf_unfiltered (gdb_stdlog,
3908 "infrun: inserting longjmp-resume breakpoint at 0x%s\n",
3909 paddr_nz (pc));
3910
4e1c45ea 3911 inferior_thread ()->step_resume_breakpoint =
611c83ae
PA
3912 set_momentary_breakpoint_at_pc (pc, bp_longjmp_resume);
3913}
3914
104c1213
JM
3915static void
3916stop_stepping (struct execution_control_state *ecs)
3917{
527159b7 3918 if (debug_infrun)
8a9de0e4 3919 fprintf_unfiltered (gdb_stdlog, "infrun: stop_stepping\n");
527159b7 3920
cd0fc7c3
SS
3921 /* Let callers know we don't want to wait for the inferior anymore. */
3922 ecs->wait_some_more = 0;
3923}
3924
d4f3574e
SS
3925/* This function handles various cases where we need to continue
3926 waiting for the inferior. */
3927/* (Used to be the keep_going: label in the old wait_for_inferior) */
3928
3929static void
3930keep_going (struct execution_control_state *ecs)
3931{
d4f3574e 3932 /* Save the pc before execution, to compare with pc after stop. */
4e1c45ea 3933 ecs->event_thread->prev_pc = read_pc (); /* Might have been DECR_AFTER_BREAK */
d4f3574e 3934
d4f3574e
SS
3935 /* If we did not do break;, it means we should keep running the
3936 inferior and not return to debugger. */
3937
2020b7ab
PA
3938 if (ecs->event_thread->trap_expected
3939 && ecs->event_thread->stop_signal != TARGET_SIGNAL_TRAP)
d4f3574e
SS
3940 {
3941 /* We took a signal (which we are supposed to pass through to
4e1c45ea
PA
3942 the inferior, else we'd not get here) and we haven't yet
3943 gotten our trap. Simply continue. */
2020b7ab
PA
3944 resume (currently_stepping (ecs->event_thread),
3945 ecs->event_thread->stop_signal);
d4f3574e
SS
3946 }
3947 else
3948 {
3949 /* Either the trap was not expected, but we are continuing
488f131b
JB
3950 anyway (the user asked that this signal be passed to the
3951 child)
3952 -- or --
3953 The signal was SIGTRAP, e.g. it was our signal, but we
3954 decided we should resume from it.
d4f3574e 3955
c36b740a 3956 We're going to run this baby now!
d4f3574e 3957
c36b740a
VP
3958 Note that insert_breakpoints won't try to re-insert
3959 already inserted breakpoints. Therefore, we don't
3960 care if breakpoints were already inserted, or not. */
3961
4e1c45ea 3962 if (ecs->event_thread->stepping_over_breakpoint)
45e8c884 3963 {
237fc4c9
PA
3964 if (! use_displaced_stepping (current_gdbarch))
3965 /* Since we can't do a displaced step, we have to remove
3966 the breakpoint while we step it. To keep things
3967 simple, we remove them all. */
3968 remove_breakpoints ();
45e8c884
VP
3969 }
3970 else
d4f3574e 3971 {
e236ba44 3972 struct gdb_exception e;
569631c6
UW
3973 /* Stop stepping when inserting breakpoints
3974 has failed. */
e236ba44
VP
3975 TRY_CATCH (e, RETURN_MASK_ERROR)
3976 {
3977 insert_breakpoints ();
3978 }
3979 if (e.reason < 0)
d4f3574e
SS
3980 {
3981 stop_stepping (ecs);
3982 return;
3983 }
d4f3574e
SS
3984 }
3985
4e1c45ea 3986 ecs->event_thread->trap_expected = ecs->event_thread->stepping_over_breakpoint;
d4f3574e
SS
3987
3988 /* Do not deliver SIGNAL_TRAP (except when the user explicitly
488f131b
JB
3989 specifies that such a signal should be delivered to the
3990 target program).
3991
3992 Typically, this would occure when a user is debugging a
3993 target monitor on a simulator: the target monitor sets a
3994 breakpoint; the simulator encounters this break-point and
3995 halts the simulation handing control to GDB; GDB, noteing
3996 that the break-point isn't valid, returns control back to the
3997 simulator; the simulator then delivers the hardware
3998 equivalent of a SIGNAL_TRAP to the program being debugged. */
3999
2020b7ab
PA
4000 if (ecs->event_thread->stop_signal == TARGET_SIGNAL_TRAP
4001 && !signal_program[ecs->event_thread->stop_signal])
4002 ecs->event_thread->stop_signal = TARGET_SIGNAL_0;
d4f3574e 4003
2020b7ab
PA
4004 resume (currently_stepping (ecs->event_thread),
4005 ecs->event_thread->stop_signal);
d4f3574e
SS
4006 }
4007
488f131b 4008 prepare_to_wait (ecs);
d4f3574e
SS
4009}
4010
104c1213
JM
4011/* This function normally comes after a resume, before
4012 handle_inferior_event exits. It takes care of any last bits of
4013 housekeeping, and sets the all-important wait_some_more flag. */
cd0fc7c3 4014
104c1213
JM
4015static void
4016prepare_to_wait (struct execution_control_state *ecs)
cd0fc7c3 4017{
527159b7 4018 if (debug_infrun)
8a9de0e4 4019 fprintf_unfiltered (gdb_stdlog, "infrun: prepare_to_wait\n");
0d1e5fa7 4020 if (infwait_state == infwait_normal_state)
104c1213
JM
4021 {
4022 overlay_cache_invalid = 1;
4023
4024 /* We have to invalidate the registers BEFORE calling
488f131b
JB
4025 target_wait because they can be loaded from the target while
4026 in target_wait. This makes remote debugging a bit more
4027 efficient for those targets that provide critical registers
4028 as part of their normal status mechanism. */
104c1213
JM
4029
4030 registers_changed ();
0d1e5fa7 4031 waiton_ptid = pid_to_ptid (-1);
104c1213
JM
4032 }
4033 /* This is the old end of the while loop. Let everybody know we
4034 want to wait for the inferior some more and get called again
4035 soon. */
4036 ecs->wait_some_more = 1;
c906108c 4037}
11cf8741
JM
4038
4039/* Print why the inferior has stopped. We always print something when
4040 the inferior exits, or receives a signal. The rest of the cases are
4041 dealt with later on in normal_stop() and print_it_typical(). Ideally
4042 there should be a call to this function from handle_inferior_event()
4043 each time stop_stepping() is called.*/
4044static void
4045print_stop_reason (enum inferior_stop_reason stop_reason, int stop_info)
4046{
4047 switch (stop_reason)
4048 {
11cf8741
JM
4049 case END_STEPPING_RANGE:
4050 /* We are done with a step/next/si/ni command. */
4051 /* For now print nothing. */
fb40c209 4052 /* Print a message only if not in the middle of doing a "step n"
488f131b 4053 operation for n > 1 */
414c69f7
PA
4054 if (!inferior_thread ()->step_multi
4055 || !inferior_thread ()->stop_step)
9dc5e2a9 4056 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4057 ui_out_field_string
4058 (uiout, "reason",
4059 async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
11cf8741 4060 break;
11cf8741
JM
4061 case SIGNAL_EXITED:
4062 /* The inferior was terminated by a signal. */
8b93c638 4063 annotate_signalled ();
9dc5e2a9 4064 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4065 ui_out_field_string
4066 (uiout, "reason",
4067 async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
8b93c638
JM
4068 ui_out_text (uiout, "\nProgram terminated with signal ");
4069 annotate_signal_name ();
488f131b
JB
4070 ui_out_field_string (uiout, "signal-name",
4071 target_signal_to_name (stop_info));
8b93c638
JM
4072 annotate_signal_name_end ();
4073 ui_out_text (uiout, ", ");
4074 annotate_signal_string ();
488f131b
JB
4075 ui_out_field_string (uiout, "signal-meaning",
4076 target_signal_to_string (stop_info));
8b93c638
JM
4077 annotate_signal_string_end ();
4078 ui_out_text (uiout, ".\n");
4079 ui_out_text (uiout, "The program no longer exists.\n");
11cf8741
JM
4080 break;
4081 case EXITED:
4082 /* The inferior program is finished. */
8b93c638
JM
4083 annotate_exited (stop_info);
4084 if (stop_info)
4085 {
9dc5e2a9 4086 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4087 ui_out_field_string (uiout, "reason",
4088 async_reason_lookup (EXEC_ASYNC_EXITED));
8b93c638 4089 ui_out_text (uiout, "\nProgram exited with code ");
488f131b
JB
4090 ui_out_field_fmt (uiout, "exit-code", "0%o",
4091 (unsigned int) stop_info);
8b93c638
JM
4092 ui_out_text (uiout, ".\n");
4093 }
4094 else
4095 {
9dc5e2a9 4096 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4097 ui_out_field_string
4098 (uiout, "reason",
4099 async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
8b93c638
JM
4100 ui_out_text (uiout, "\nProgram exited normally.\n");
4101 }
f17517ea
AS
4102 /* Support the --return-child-result option. */
4103 return_child_result_value = stop_info;
11cf8741
JM
4104 break;
4105 case SIGNAL_RECEIVED:
252fbfc8
PA
4106 /* Signal received. The signal table tells us to print about
4107 it. */
8b93c638 4108 annotate_signal ();
252fbfc8
PA
4109
4110 if (stop_info == TARGET_SIGNAL_0 && !ui_out_is_mi_like_p (uiout))
4111 {
4112 struct thread_info *t = inferior_thread ();
4113
4114 ui_out_text (uiout, "\n[");
4115 ui_out_field_string (uiout, "thread-name",
4116 target_pid_to_str (t->ptid));
4117 ui_out_field_fmt (uiout, "thread-id", "] #%d", t->num);
4118 ui_out_text (uiout, " stopped");
4119 }
4120 else
4121 {
4122 ui_out_text (uiout, "\nProgram received signal ");
4123 annotate_signal_name ();
4124 if (ui_out_is_mi_like_p (uiout))
4125 ui_out_field_string
4126 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
4127 ui_out_field_string (uiout, "signal-name",
4128 target_signal_to_name (stop_info));
4129 annotate_signal_name_end ();
4130 ui_out_text (uiout, ", ");
4131 annotate_signal_string ();
4132 ui_out_field_string (uiout, "signal-meaning",
4133 target_signal_to_string (stop_info));
4134 annotate_signal_string_end ();
4135 }
8b93c638 4136 ui_out_text (uiout, ".\n");
11cf8741 4137 break;
b2175913
MS
4138 case NO_HISTORY:
4139 /* Reverse execution: target ran out of history info. */
4140 ui_out_text (uiout, "\nNo more reverse-execution history.\n");
4141 break;
11cf8741 4142 default:
8e65ff28 4143 internal_error (__FILE__, __LINE__,
e2e0b3e5 4144 _("print_stop_reason: unrecognized enum value"));
11cf8741
JM
4145 break;
4146 }
4147}
c906108c 4148\f
43ff13b4 4149
c906108c
SS
4150/* Here to return control to GDB when the inferior stops for real.
4151 Print appropriate messages, remove breakpoints, give terminal our modes.
4152
4153 STOP_PRINT_FRAME nonzero means print the executing frame
4154 (pc, function, args, file, line number and line text).
4155 BREAKPOINTS_FAILED nonzero means stop was due to error
4156 attempting to insert breakpoints. */
4157
4158void
96baa820 4159normal_stop (void)
c906108c 4160{
73b65bb0
DJ
4161 struct target_waitstatus last;
4162 ptid_t last_ptid;
4163
4164 get_last_target_status (&last_ptid, &last);
4165
4f8d22e3
PA
4166 /* In non-stop mode, we don't want GDB to switch threads behind the
4167 user's back, to avoid races where the user is typing a command to
4168 apply to thread x, but GDB switches to thread y before the user
4169 finishes entering the command. */
4170
c906108c
SS
4171 /* As with the notification of thread events, we want to delay
4172 notifying the user that we've switched thread context until
4173 the inferior actually stops.
4174
73b65bb0
DJ
4175 There's no point in saying anything if the inferior has exited.
4176 Note that SIGNALLED here means "exited with a signal", not
4177 "received a signal". */
4f8d22e3
PA
4178 if (!non_stop
4179 && !ptid_equal (previous_inferior_ptid, inferior_ptid)
73b65bb0
DJ
4180 && target_has_execution
4181 && last.kind != TARGET_WAITKIND_SIGNALLED
4182 && last.kind != TARGET_WAITKIND_EXITED)
c906108c
SS
4183 {
4184 target_terminal_ours_for_output ();
a3f17187 4185 printf_filtered (_("[Switching to %s]\n"),
c95310c6 4186 target_pid_to_str (inferior_ptid));
b8fa951a 4187 annotate_thread_changed ();
39f77062 4188 previous_inferior_ptid = inferior_ptid;
c906108c 4189 }
c906108c 4190
4fa8626c 4191 /* NOTE drow/2004-01-17: Is this still necessary? */
c906108c
SS
4192 /* Make sure that the current_frame's pc is correct. This
4193 is a correction for setting up the frame info before doing
b798847d 4194 gdbarch_decr_pc_after_break */
b87efeee
AC
4195 if (target_has_execution)
4196 /* FIXME: cagney/2002-12-06: Has the PC changed? Thanks to
b798847d 4197 gdbarch_decr_pc_after_break, the program counter can change. Ask the
b87efeee 4198 frame code to check for this and sort out any resultant mess.
b798847d 4199 gdbarch_decr_pc_after_break needs to just go away. */
2f107107 4200 deprecated_update_frame_pc_hack (get_current_frame (), read_pc ());
c906108c 4201
74960c60 4202 if (!breakpoints_always_inserted_mode () && target_has_execution)
c906108c
SS
4203 {
4204 if (remove_breakpoints ())
4205 {
4206 target_terminal_ours_for_output ();
a3f17187
AC
4207 printf_filtered (_("\
4208Cannot remove breakpoints because program is no longer writable.\n\
4209It might be running in another process.\n\
4210Further execution is probably impossible.\n"));
c906108c
SS
4211 }
4212 }
c906108c 4213
c906108c
SS
4214 /* If an auto-display called a function and that got a signal,
4215 delete that auto-display to avoid an infinite recursion. */
4216
4217 if (stopped_by_random_signal)
4218 disable_current_display ();
4219
4220 /* Don't print a message if in the middle of doing a "step n"
4221 operation for n > 1 */
af679fd0
PA
4222 if (target_has_execution
4223 && last.kind != TARGET_WAITKIND_SIGNALLED
4224 && last.kind != TARGET_WAITKIND_EXITED
4225 && inferior_thread ()->step_multi
414c69f7 4226 && inferior_thread ()->stop_step)
c906108c
SS
4227 goto done;
4228
4229 target_terminal_ours ();
4230
7abfe014
DJ
4231 /* Set the current source location. This will also happen if we
4232 display the frame below, but the current SAL will be incorrect
4233 during a user hook-stop function. */
4234 if (target_has_stack && !stop_stack_dummy)
4235 set_current_sal_from_frame (get_current_frame (), 1);
4236
c906108c 4237 if (!target_has_stack)
d51fd4c8 4238 goto done;
c906108c 4239
32400beb
PA
4240 if (last.kind == TARGET_WAITKIND_SIGNALLED
4241 || last.kind == TARGET_WAITKIND_EXITED)
4242 goto done;
4243
c906108c
SS
4244 /* Select innermost stack frame - i.e., current frame is frame 0,
4245 and current location is based on that.
4246 Don't do this on return from a stack dummy routine,
4247 or if the program has exited. */
4248
4249 if (!stop_stack_dummy)
4250 {
0f7d239c 4251 select_frame (get_current_frame ());
c906108c
SS
4252
4253 /* Print current location without a level number, if
c5aa993b
JM
4254 we have changed functions or hit a breakpoint.
4255 Print source line if we have one.
4256 bpstat_print() contains the logic deciding in detail
4257 what to print, based on the event(s) that just occurred. */
c906108c 4258
d01a8610
AS
4259 /* If --batch-silent is enabled then there's no need to print the current
4260 source location, and to try risks causing an error message about
4261 missing source files. */
4262 if (stop_print_frame && !batch_silent)
c906108c
SS
4263 {
4264 int bpstat_ret;
4265 int source_flag;
917317f4 4266 int do_frame_printing = 1;
347bddb7 4267 struct thread_info *tp = inferior_thread ();
c906108c 4268
347bddb7 4269 bpstat_ret = bpstat_print (tp->stop_bpstat);
917317f4
JM
4270 switch (bpstat_ret)
4271 {
4272 case PRINT_UNKNOWN:
b0f4b84b
DJ
4273 /* If we had hit a shared library event breakpoint,
4274 bpstat_print would print out this message. If we hit
4275 an OS-level shared library event, do the same
4276 thing. */
4277 if (last.kind == TARGET_WAITKIND_LOADED)
4278 {
4279 printf_filtered (_("Stopped due to shared library event\n"));
4280 source_flag = SRC_LINE; /* something bogus */
4281 do_frame_printing = 0;
4282 break;
4283 }
4284
aa0cd9c1 4285 /* FIXME: cagney/2002-12-01: Given that a frame ID does
8fb3e588
AC
4286 (or should) carry around the function and does (or
4287 should) use that when doing a frame comparison. */
414c69f7 4288 if (tp->stop_step
347bddb7 4289 && frame_id_eq (tp->step_frame_id,
aa0cd9c1 4290 get_frame_id (get_current_frame ()))
917317f4 4291 && step_start_function == find_pc_function (stop_pc))
488f131b 4292 source_flag = SRC_LINE; /* finished step, just print source line */
917317f4 4293 else
488f131b 4294 source_flag = SRC_AND_LOC; /* print location and source line */
917317f4
JM
4295 break;
4296 case PRINT_SRC_AND_LOC:
488f131b 4297 source_flag = SRC_AND_LOC; /* print location and source line */
917317f4
JM
4298 break;
4299 case PRINT_SRC_ONLY:
c5394b80 4300 source_flag = SRC_LINE;
917317f4
JM
4301 break;
4302 case PRINT_NOTHING:
488f131b 4303 source_flag = SRC_LINE; /* something bogus */
917317f4
JM
4304 do_frame_printing = 0;
4305 break;
4306 default:
e2e0b3e5 4307 internal_error (__FILE__, __LINE__, _("Unknown value."));
917317f4 4308 }
c906108c 4309
9dc5e2a9 4310 if (ui_out_is_mi_like_p (uiout))
b1a268e5
VP
4311 {
4312
4313 ui_out_field_int (uiout, "thread-id",
4314 pid_to_thread_id (inferior_ptid));
4315 if (non_stop)
4316 {
4317 struct cleanup *back_to = make_cleanup_ui_out_list_begin_end
4318 (uiout, "stopped-threads");
4319 ui_out_field_int (uiout, NULL,
4320 pid_to_thread_id (inferior_ptid));
4321 do_cleanups (back_to);
4322 }
4323 else
4324 ui_out_field_string (uiout, "stopped-threads", "all");
4325 }
c906108c
SS
4326 /* The behavior of this routine with respect to the source
4327 flag is:
c5394b80
JM
4328 SRC_LINE: Print only source line
4329 LOCATION: Print only location
4330 SRC_AND_LOC: Print location and source line */
917317f4 4331 if (do_frame_printing)
b04f3ab4 4332 print_stack_frame (get_selected_frame (NULL), 0, source_flag);
c906108c
SS
4333
4334 /* Display the auto-display expressions. */
4335 do_displays ();
4336 }
4337 }
4338
4339 /* Save the function value return registers, if we care.
4340 We might be about to restore their previous contents. */
32400beb 4341 if (inferior_thread ()->proceed_to_finish)
d5c31457
UW
4342 {
4343 /* This should not be necessary. */
4344 if (stop_registers)
4345 regcache_xfree (stop_registers);
4346
4347 /* NB: The copy goes through to the target picking up the value of
4348 all the registers. */
4349 stop_registers = regcache_dup (get_current_regcache ());
4350 }
c906108c
SS
4351
4352 if (stop_stack_dummy)
4353 {
dbe9fe58
AC
4354 /* Pop the empty frame that contains the stack dummy. POP_FRAME
4355 ends with a setting of the current frame, so we can use that
4356 next. */
4357 frame_pop (get_current_frame ());
c906108c 4358 /* Set stop_pc to what it was before we called the function.
c5aa993b
JM
4359 Can't rely on restore_inferior_status because that only gets
4360 called if we don't stop in the called function. */
c906108c 4361 stop_pc = read_pc ();
0f7d239c 4362 select_frame (get_current_frame ());
c906108c
SS
4363 }
4364
c906108c
SS
4365done:
4366 annotate_stopped ();
af679fd0
PA
4367 if (!suppress_stop_observer
4368 && !(target_has_execution
4369 && last.kind != TARGET_WAITKIND_SIGNALLED
4370 && last.kind != TARGET_WAITKIND_EXITED
4371 && inferior_thread ()->step_multi))
347bddb7
PA
4372 {
4373 if (!ptid_equal (inferior_ptid, null_ptid))
4374 observer_notify_normal_stop (inferior_thread ()->stop_bpstat);
4375 else
4376 observer_notify_normal_stop (NULL);
4377 }
94cc34af
PA
4378 if (target_has_execution
4379 && last.kind != TARGET_WAITKIND_SIGNALLED
4380 && last.kind != TARGET_WAITKIND_EXITED)
4381 {
347bddb7
PA
4382 /* Delete the breakpoint we stopped at, if it wants to be deleted.
4383 Delete any breakpoint that is to be deleted at the next stop. */
4384 breakpoint_auto_delete (inferior_thread ()->stop_bpstat);
4385
94cc34af
PA
4386 if (!non_stop)
4387 set_running (pid_to_ptid (-1), 0);
4388 else
4389 set_running (inferior_ptid, 0);
4390 }
d51fd4c8
PA
4391
4392 /* Look up the hook_stop and run it (CLI internally handles problem
4393 of stop_command's pre-hook not existing). */
4394 if (stop_command)
4395 catch_errors (hook_stop_stub, stop_command,
4396 "Error while running hook_stop:\n", RETURN_MASK_ALL);
4397
c906108c
SS
4398}
4399
4400static int
96baa820 4401hook_stop_stub (void *cmd)
c906108c 4402{
5913bcb0 4403 execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
c906108c
SS
4404 return (0);
4405}
4406\f
c5aa993b 4407int
96baa820 4408signal_stop_state (int signo)
c906108c 4409{
d6b48e9c 4410 return signal_stop[signo];
c906108c
SS
4411}
4412
c5aa993b 4413int
96baa820 4414signal_print_state (int signo)
c906108c
SS
4415{
4416 return signal_print[signo];
4417}
4418
c5aa993b 4419int
96baa820 4420signal_pass_state (int signo)
c906108c
SS
4421{
4422 return signal_program[signo];
4423}
4424
488f131b 4425int
7bda5e4a 4426signal_stop_update (int signo, int state)
d4f3574e
SS
4427{
4428 int ret = signal_stop[signo];
4429 signal_stop[signo] = state;
4430 return ret;
4431}
4432
488f131b 4433int
7bda5e4a 4434signal_print_update (int signo, int state)
d4f3574e
SS
4435{
4436 int ret = signal_print[signo];
4437 signal_print[signo] = state;
4438 return ret;
4439}
4440
488f131b 4441int
7bda5e4a 4442signal_pass_update (int signo, int state)
d4f3574e
SS
4443{
4444 int ret = signal_program[signo];
4445 signal_program[signo] = state;
4446 return ret;
4447}
4448
c906108c 4449static void
96baa820 4450sig_print_header (void)
c906108c 4451{
a3f17187
AC
4452 printf_filtered (_("\
4453Signal Stop\tPrint\tPass to program\tDescription\n"));
c906108c
SS
4454}
4455
4456static void
96baa820 4457sig_print_info (enum target_signal oursig)
c906108c
SS
4458{
4459 char *name = target_signal_to_name (oursig);
4460 int name_padding = 13 - strlen (name);
96baa820 4461
c906108c
SS
4462 if (name_padding <= 0)
4463 name_padding = 0;
4464
4465 printf_filtered ("%s", name);
488f131b 4466 printf_filtered ("%*.*s ", name_padding, name_padding, " ");
c906108c
SS
4467 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
4468 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
4469 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
4470 printf_filtered ("%s\n", target_signal_to_string (oursig));
4471}
4472
4473/* Specify how various signals in the inferior should be handled. */
4474
4475static void
96baa820 4476handle_command (char *args, int from_tty)
c906108c
SS
4477{
4478 char **argv;
4479 int digits, wordlen;
4480 int sigfirst, signum, siglast;
4481 enum target_signal oursig;
4482 int allsigs;
4483 int nsigs;
4484 unsigned char *sigs;
4485 struct cleanup *old_chain;
4486
4487 if (args == NULL)
4488 {
e2e0b3e5 4489 error_no_arg (_("signal to handle"));
c906108c
SS
4490 }
4491
4492 /* Allocate and zero an array of flags for which signals to handle. */
4493
4494 nsigs = (int) TARGET_SIGNAL_LAST;
4495 sigs = (unsigned char *) alloca (nsigs);
4496 memset (sigs, 0, nsigs);
4497
4498 /* Break the command line up into args. */
4499
d1a41061 4500 argv = gdb_buildargv (args);
7a292a7a 4501 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
4502
4503 /* Walk through the args, looking for signal oursigs, signal names, and
4504 actions. Signal numbers and signal names may be interspersed with
4505 actions, with the actions being performed for all signals cumulatively
4506 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
4507
4508 while (*argv != NULL)
4509 {
4510 wordlen = strlen (*argv);
4511 for (digits = 0; isdigit ((*argv)[digits]); digits++)
4512 {;
4513 }
4514 allsigs = 0;
4515 sigfirst = siglast = -1;
4516
4517 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
4518 {
4519 /* Apply action to all signals except those used by the
4520 debugger. Silently skip those. */
4521 allsigs = 1;
4522 sigfirst = 0;
4523 siglast = nsigs - 1;
4524 }
4525 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
4526 {
4527 SET_SIGS (nsigs, sigs, signal_stop);
4528 SET_SIGS (nsigs, sigs, signal_print);
4529 }
4530 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
4531 {
4532 UNSET_SIGS (nsigs, sigs, signal_program);
4533 }
4534 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
4535 {
4536 SET_SIGS (nsigs, sigs, signal_print);
4537 }
4538 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
4539 {
4540 SET_SIGS (nsigs, sigs, signal_program);
4541 }
4542 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
4543 {
4544 UNSET_SIGS (nsigs, sigs, signal_stop);
4545 }
4546 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
4547 {
4548 SET_SIGS (nsigs, sigs, signal_program);
4549 }
4550 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
4551 {
4552 UNSET_SIGS (nsigs, sigs, signal_print);
4553 UNSET_SIGS (nsigs, sigs, signal_stop);
4554 }
4555 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
4556 {
4557 UNSET_SIGS (nsigs, sigs, signal_program);
4558 }
4559 else if (digits > 0)
4560 {
4561 /* It is numeric. The numeric signal refers to our own
4562 internal signal numbering from target.h, not to host/target
4563 signal number. This is a feature; users really should be
4564 using symbolic names anyway, and the common ones like
4565 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
4566
4567 sigfirst = siglast = (int)
4568 target_signal_from_command (atoi (*argv));
4569 if ((*argv)[digits] == '-')
4570 {
4571 siglast = (int)
4572 target_signal_from_command (atoi ((*argv) + digits + 1));
4573 }
4574 if (sigfirst > siglast)
4575 {
4576 /* Bet he didn't figure we'd think of this case... */
4577 signum = sigfirst;
4578 sigfirst = siglast;
4579 siglast = signum;
4580 }
4581 }
4582 else
4583 {
4584 oursig = target_signal_from_name (*argv);
4585 if (oursig != TARGET_SIGNAL_UNKNOWN)
4586 {
4587 sigfirst = siglast = (int) oursig;
4588 }
4589 else
4590 {
4591 /* Not a number and not a recognized flag word => complain. */
8a3fe4f8 4592 error (_("Unrecognized or ambiguous flag word: \"%s\"."), *argv);
c906108c
SS
4593 }
4594 }
4595
4596 /* If any signal numbers or symbol names were found, set flags for
c5aa993b 4597 which signals to apply actions to. */
c906108c
SS
4598
4599 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
4600 {
4601 switch ((enum target_signal) signum)
4602 {
4603 case TARGET_SIGNAL_TRAP:
4604 case TARGET_SIGNAL_INT:
4605 if (!allsigs && !sigs[signum])
4606 {
4607 if (query ("%s is used by the debugger.\n\
488f131b 4608Are you sure you want to change it? ", target_signal_to_name ((enum target_signal) signum)))
c906108c
SS
4609 {
4610 sigs[signum] = 1;
4611 }
4612 else
4613 {
a3f17187 4614 printf_unfiltered (_("Not confirmed, unchanged.\n"));
c906108c
SS
4615 gdb_flush (gdb_stdout);
4616 }
4617 }
4618 break;
4619 case TARGET_SIGNAL_0:
4620 case TARGET_SIGNAL_DEFAULT:
4621 case TARGET_SIGNAL_UNKNOWN:
4622 /* Make sure that "all" doesn't print these. */
4623 break;
4624 default:
4625 sigs[signum] = 1;
4626 break;
4627 }
4628 }
4629
4630 argv++;
4631 }
4632
39f77062 4633 target_notice_signals (inferior_ptid);
c906108c
SS
4634
4635 if (from_tty)
4636 {
4637 /* Show the results. */
4638 sig_print_header ();
4639 for (signum = 0; signum < nsigs; signum++)
4640 {
4641 if (sigs[signum])
4642 {
4643 sig_print_info (signum);
4644 }
4645 }
4646 }
4647
4648 do_cleanups (old_chain);
4649}
4650
4651static void
96baa820 4652xdb_handle_command (char *args, int from_tty)
c906108c
SS
4653{
4654 char **argv;
4655 struct cleanup *old_chain;
4656
d1a41061
PP
4657 if (args == NULL)
4658 error_no_arg (_("xdb command"));
4659
c906108c
SS
4660 /* Break the command line up into args. */
4661
d1a41061 4662 argv = gdb_buildargv (args);
7a292a7a 4663 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
4664 if (argv[1] != (char *) NULL)
4665 {
4666 char *argBuf;
4667 int bufLen;
4668
4669 bufLen = strlen (argv[0]) + 20;
4670 argBuf = (char *) xmalloc (bufLen);
4671 if (argBuf)
4672 {
4673 int validFlag = 1;
4674 enum target_signal oursig;
4675
4676 oursig = target_signal_from_name (argv[0]);
4677 memset (argBuf, 0, bufLen);
4678 if (strcmp (argv[1], "Q") == 0)
4679 sprintf (argBuf, "%s %s", argv[0], "noprint");
4680 else
4681 {
4682 if (strcmp (argv[1], "s") == 0)
4683 {
4684 if (!signal_stop[oursig])
4685 sprintf (argBuf, "%s %s", argv[0], "stop");
4686 else
4687 sprintf (argBuf, "%s %s", argv[0], "nostop");
4688 }
4689 else if (strcmp (argv[1], "i") == 0)
4690 {
4691 if (!signal_program[oursig])
4692 sprintf (argBuf, "%s %s", argv[0], "pass");
4693 else
4694 sprintf (argBuf, "%s %s", argv[0], "nopass");
4695 }
4696 else if (strcmp (argv[1], "r") == 0)
4697 {
4698 if (!signal_print[oursig])
4699 sprintf (argBuf, "%s %s", argv[0], "print");
4700 else
4701 sprintf (argBuf, "%s %s", argv[0], "noprint");
4702 }
4703 else
4704 validFlag = 0;
4705 }
4706 if (validFlag)
4707 handle_command (argBuf, from_tty);
4708 else
a3f17187 4709 printf_filtered (_("Invalid signal handling flag.\n"));
c906108c 4710 if (argBuf)
b8c9b27d 4711 xfree (argBuf);
c906108c
SS
4712 }
4713 }
4714 do_cleanups (old_chain);
4715}
4716
4717/* Print current contents of the tables set by the handle command.
4718 It is possible we should just be printing signals actually used
4719 by the current target (but for things to work right when switching
4720 targets, all signals should be in the signal tables). */
4721
4722static void
96baa820 4723signals_info (char *signum_exp, int from_tty)
c906108c
SS
4724{
4725 enum target_signal oursig;
4726 sig_print_header ();
4727
4728 if (signum_exp)
4729 {
4730 /* First see if this is a symbol name. */
4731 oursig = target_signal_from_name (signum_exp);
4732 if (oursig == TARGET_SIGNAL_UNKNOWN)
4733 {
4734 /* No, try numeric. */
4735 oursig =
bb518678 4736 target_signal_from_command (parse_and_eval_long (signum_exp));
c906108c
SS
4737 }
4738 sig_print_info (oursig);
4739 return;
4740 }
4741
4742 printf_filtered ("\n");
4743 /* These ugly casts brought to you by the native VAX compiler. */
4744 for (oursig = TARGET_SIGNAL_FIRST;
4745 (int) oursig < (int) TARGET_SIGNAL_LAST;
4746 oursig = (enum target_signal) ((int) oursig + 1))
4747 {
4748 QUIT;
4749
4750 if (oursig != TARGET_SIGNAL_UNKNOWN
488f131b 4751 && oursig != TARGET_SIGNAL_DEFAULT && oursig != TARGET_SIGNAL_0)
c906108c
SS
4752 sig_print_info (oursig);
4753 }
4754
a3f17187 4755 printf_filtered (_("\nUse the \"handle\" command to change these tables.\n"));
c906108c
SS
4756}
4757\f
7a292a7a
SS
4758struct inferior_status
4759{
4760 enum target_signal stop_signal;
4761 CORE_ADDR stop_pc;
4762 bpstat stop_bpstat;
4763 int stop_step;
4764 int stop_stack_dummy;
4765 int stopped_by_random_signal;
ca67fcb8 4766 int stepping_over_breakpoint;
7a292a7a
SS
4767 CORE_ADDR step_range_start;
4768 CORE_ADDR step_range_end;
aa0cd9c1 4769 struct frame_id step_frame_id;
5fbbeb29 4770 enum step_over_calls_kind step_over_calls;
7a292a7a
SS
4771 CORE_ADDR step_resume_break_address;
4772 int stop_after_trap;
c0236d92 4773 int stop_soon;
7a292a7a
SS
4774
4775 /* These are here because if call_function_by_hand has written some
4776 registers and then decides to call error(), we better not have changed
4777 any registers. */
72cec141 4778 struct regcache *registers;
7a292a7a 4779
101dcfbe
AC
4780 /* A frame unique identifier. */
4781 struct frame_id selected_frame_id;
4782
7a292a7a
SS
4783 int breakpoint_proceeded;
4784 int restore_stack_info;
4785 int proceed_to_finish;
4786};
4787
c906108c
SS
4788/* Save all of the information associated with the inferior<==>gdb
4789 connection. INF_STATUS is a pointer to a "struct inferior_status"
4790 (defined in inferior.h). */
4791
7a292a7a 4792struct inferior_status *
96baa820 4793save_inferior_status (int restore_stack_info)
c906108c 4794{
72cec141 4795 struct inferior_status *inf_status = XMALLOC (struct inferior_status);
4e1c45ea 4796 struct thread_info *tp = inferior_thread ();
d6b48e9c 4797 struct inferior *inf = current_inferior ();
7a292a7a 4798
2020b7ab 4799 inf_status->stop_signal = tp->stop_signal;
c906108c 4800 inf_status->stop_pc = stop_pc;
414c69f7 4801 inf_status->stop_step = tp->stop_step;
c906108c
SS
4802 inf_status->stop_stack_dummy = stop_stack_dummy;
4803 inf_status->stopped_by_random_signal = stopped_by_random_signal;
4e1c45ea
PA
4804 inf_status->stepping_over_breakpoint = tp->trap_expected;
4805 inf_status->step_range_start = tp->step_range_start;
4806 inf_status->step_range_end = tp->step_range_end;
4807 inf_status->step_frame_id = tp->step_frame_id;
078130d0 4808 inf_status->step_over_calls = tp->step_over_calls;
c906108c 4809 inf_status->stop_after_trap = stop_after_trap;
d6b48e9c 4810 inf_status->stop_soon = inf->stop_soon;
c906108c
SS
4811 /* Save original bpstat chain here; replace it with copy of chain.
4812 If caller's caller is walking the chain, they'll be happier if we
7a292a7a
SS
4813 hand them back the original chain when restore_inferior_status is
4814 called. */
347bddb7
PA
4815 inf_status->stop_bpstat = tp->stop_bpstat;
4816 tp->stop_bpstat = bpstat_copy (tp->stop_bpstat);
c906108c
SS
4817 inf_status->breakpoint_proceeded = breakpoint_proceeded;
4818 inf_status->restore_stack_info = restore_stack_info;
32400beb 4819 inf_status->proceed_to_finish = tp->proceed_to_finish;
c5aa993b 4820
594f7785 4821 inf_status->registers = regcache_dup (get_current_regcache ());
c906108c 4822
206415a3 4823 inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
7a292a7a 4824 return inf_status;
c906108c
SS
4825}
4826
c906108c 4827static int
96baa820 4828restore_selected_frame (void *args)
c906108c 4829{
488f131b 4830 struct frame_id *fid = (struct frame_id *) args;
c906108c 4831 struct frame_info *frame;
c906108c 4832
101dcfbe 4833 frame = frame_find_by_id (*fid);
c906108c 4834
aa0cd9c1
AC
4835 /* If inf_status->selected_frame_id is NULL, there was no previously
4836 selected frame. */
101dcfbe 4837 if (frame == NULL)
c906108c 4838 {
8a3fe4f8 4839 warning (_("Unable to restore previously selected frame."));
c906108c
SS
4840 return 0;
4841 }
4842
0f7d239c 4843 select_frame (frame);
c906108c
SS
4844
4845 return (1);
4846}
4847
4848void
96baa820 4849restore_inferior_status (struct inferior_status *inf_status)
c906108c 4850{
4e1c45ea 4851 struct thread_info *tp = inferior_thread ();
d6b48e9c 4852 struct inferior *inf = current_inferior ();
4e1c45ea 4853
2020b7ab 4854 tp->stop_signal = inf_status->stop_signal;
c906108c 4855 stop_pc = inf_status->stop_pc;
414c69f7 4856 tp->stop_step = inf_status->stop_step;
c906108c
SS
4857 stop_stack_dummy = inf_status->stop_stack_dummy;
4858 stopped_by_random_signal = inf_status->stopped_by_random_signal;
4e1c45ea
PA
4859 tp->trap_expected = inf_status->stepping_over_breakpoint;
4860 tp->step_range_start = inf_status->step_range_start;
4861 tp->step_range_end = inf_status->step_range_end;
4862 tp->step_frame_id = inf_status->step_frame_id;
078130d0 4863 tp->step_over_calls = inf_status->step_over_calls;
c906108c 4864 stop_after_trap = inf_status->stop_after_trap;
d6b48e9c 4865 inf->stop_soon = inf_status->stop_soon;
347bddb7
PA
4866 bpstat_clear (&tp->stop_bpstat);
4867 tp->stop_bpstat = inf_status->stop_bpstat;
c906108c 4868 breakpoint_proceeded = inf_status->breakpoint_proceeded;
32400beb 4869 tp->proceed_to_finish = inf_status->proceed_to_finish;
c906108c 4870
c906108c
SS
4871 /* The inferior can be gone if the user types "print exit(0)"
4872 (and perhaps other times). */
4873 if (target_has_execution)
72cec141 4874 /* NB: The register write goes through to the target. */
594f7785 4875 regcache_cpy (get_current_regcache (), inf_status->registers);
72cec141 4876 regcache_xfree (inf_status->registers);
c906108c 4877
c906108c
SS
4878 /* FIXME: If we are being called after stopping in a function which
4879 is called from gdb, we should not be trying to restore the
4880 selected frame; it just prints a spurious error message (The
4881 message is useful, however, in detecting bugs in gdb (like if gdb
4882 clobbers the stack)). In fact, should we be restoring the
4883 inferior status at all in that case? . */
4884
4885 if (target_has_stack && inf_status->restore_stack_info)
4886 {
c906108c 4887 /* The point of catch_errors is that if the stack is clobbered,
101dcfbe
AC
4888 walking the stack might encounter a garbage pointer and
4889 error() trying to dereference it. */
488f131b
JB
4890 if (catch_errors
4891 (restore_selected_frame, &inf_status->selected_frame_id,
4892 "Unable to restore previously selected frame:\n",
4893 RETURN_MASK_ERROR) == 0)
c906108c
SS
4894 /* Error in restoring the selected frame. Select the innermost
4895 frame. */
0f7d239c 4896 select_frame (get_current_frame ());
c906108c
SS
4897
4898 }
c906108c 4899
72cec141 4900 xfree (inf_status);
7a292a7a 4901}
c906108c 4902
74b7792f
AC
4903static void
4904do_restore_inferior_status_cleanup (void *sts)
4905{
4906 restore_inferior_status (sts);
4907}
4908
4909struct cleanup *
4910make_cleanup_restore_inferior_status (struct inferior_status *inf_status)
4911{
4912 return make_cleanup (do_restore_inferior_status_cleanup, inf_status);
4913}
4914
c906108c 4915void
96baa820 4916discard_inferior_status (struct inferior_status *inf_status)
7a292a7a
SS
4917{
4918 /* See save_inferior_status for info on stop_bpstat. */
4919 bpstat_clear (&inf_status->stop_bpstat);
72cec141 4920 regcache_xfree (inf_status->registers);
72cec141 4921 xfree (inf_status);
7a292a7a
SS
4922}
4923
47932f85 4924int
3a3e9ee3 4925inferior_has_forked (ptid_t pid, ptid_t *child_pid)
47932f85
DJ
4926{
4927 struct target_waitstatus last;
4928 ptid_t last_ptid;
4929
4930 get_last_target_status (&last_ptid, &last);
4931
4932 if (last.kind != TARGET_WAITKIND_FORKED)
4933 return 0;
4934
3a3e9ee3 4935 if (!ptid_equal (last_ptid, pid))
47932f85
DJ
4936 return 0;
4937
4938 *child_pid = last.value.related_pid;
4939 return 1;
4940}
4941
4942int
3a3e9ee3 4943inferior_has_vforked (ptid_t pid, ptid_t *child_pid)
47932f85
DJ
4944{
4945 struct target_waitstatus last;
4946 ptid_t last_ptid;
4947
4948 get_last_target_status (&last_ptid, &last);
4949
4950 if (last.kind != TARGET_WAITKIND_VFORKED)
4951 return 0;
4952
3a3e9ee3 4953 if (!ptid_equal (last_ptid, pid))
47932f85
DJ
4954 return 0;
4955
4956 *child_pid = last.value.related_pid;
4957 return 1;
4958}
4959
4960int
3a3e9ee3 4961inferior_has_execd (ptid_t pid, char **execd_pathname)
47932f85
DJ
4962{
4963 struct target_waitstatus last;
4964 ptid_t last_ptid;
4965
4966 get_last_target_status (&last_ptid, &last);
4967
4968 if (last.kind != TARGET_WAITKIND_EXECD)
4969 return 0;
4970
3a3e9ee3 4971 if (!ptid_equal (last_ptid, pid))
47932f85
DJ
4972 return 0;
4973
4974 *execd_pathname = xstrdup (last.value.execd_pathname);
4975 return 1;
4976}
4977
ca6724c1
KB
4978/* Oft used ptids */
4979ptid_t null_ptid;
4980ptid_t minus_one_ptid;
4981
4982/* Create a ptid given the necessary PID, LWP, and TID components. */
488f131b 4983
ca6724c1
KB
4984ptid_t
4985ptid_build (int pid, long lwp, long tid)
4986{
4987 ptid_t ptid;
4988
4989 ptid.pid = pid;
4990 ptid.lwp = lwp;
4991 ptid.tid = tid;
4992 return ptid;
4993}
4994
4995/* Create a ptid from just a pid. */
4996
4997ptid_t
4998pid_to_ptid (int pid)
4999{
5000 return ptid_build (pid, 0, 0);
5001}
5002
5003/* Fetch the pid (process id) component from a ptid. */
5004
5005int
5006ptid_get_pid (ptid_t ptid)
5007{
5008 return ptid.pid;
5009}
5010
5011/* Fetch the lwp (lightweight process) component from a ptid. */
5012
5013long
5014ptid_get_lwp (ptid_t ptid)
5015{
5016 return ptid.lwp;
5017}
5018
5019/* Fetch the tid (thread id) component from a ptid. */
5020
5021long
5022ptid_get_tid (ptid_t ptid)
5023{
5024 return ptid.tid;
5025}
5026
5027/* ptid_equal() is used to test equality of two ptids. */
5028
5029int
5030ptid_equal (ptid_t ptid1, ptid_t ptid2)
5031{
5032 return (ptid1.pid == ptid2.pid && ptid1.lwp == ptid2.lwp
488f131b 5033 && ptid1.tid == ptid2.tid);
ca6724c1
KB
5034}
5035
252fbfc8
PA
5036/* Returns true if PTID represents a process. */
5037
5038int
5039ptid_is_pid (ptid_t ptid)
5040{
5041 if (ptid_equal (minus_one_ptid, ptid))
5042 return 0;
5043 if (ptid_equal (null_ptid, ptid))
5044 return 0;
5045
5046 return (ptid_get_lwp (ptid) == 0 && ptid_get_tid (ptid) == 0);
5047}
5048
ca6724c1
KB
5049/* restore_inferior_ptid() will be used by the cleanup machinery
5050 to restore the inferior_ptid value saved in a call to
5051 save_inferior_ptid(). */
ce696e05
KB
5052
5053static void
5054restore_inferior_ptid (void *arg)
5055{
5056 ptid_t *saved_ptid_ptr = arg;
5057 inferior_ptid = *saved_ptid_ptr;
5058 xfree (arg);
5059}
5060
5061/* Save the value of inferior_ptid so that it may be restored by a
5062 later call to do_cleanups(). Returns the struct cleanup pointer
5063 needed for later doing the cleanup. */
5064
5065struct cleanup *
5066save_inferior_ptid (void)
5067{
5068 ptid_t *saved_ptid_ptr;
5069
5070 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
5071 *saved_ptid_ptr = inferior_ptid;
5072 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
5073}
c5aa993b 5074\f
488f131b 5075
b2175913
MS
5076/* User interface for reverse debugging:
5077 Set exec-direction / show exec-direction commands
5078 (returns error unless target implements to_set_exec_direction method). */
5079
5080enum exec_direction_kind execution_direction = EXEC_FORWARD;
5081static const char exec_forward[] = "forward";
5082static const char exec_reverse[] = "reverse";
5083static const char *exec_direction = exec_forward;
5084static const char *exec_direction_names[] = {
5085 exec_forward,
5086 exec_reverse,
5087 NULL
5088};
5089
5090static void
5091set_exec_direction_func (char *args, int from_tty,
5092 struct cmd_list_element *cmd)
5093{
5094 if (target_can_execute_reverse)
5095 {
5096 if (!strcmp (exec_direction, exec_forward))
5097 execution_direction = EXEC_FORWARD;
5098 else if (!strcmp (exec_direction, exec_reverse))
5099 execution_direction = EXEC_REVERSE;
5100 }
5101}
5102
5103static void
5104show_exec_direction_func (struct ui_file *out, int from_tty,
5105 struct cmd_list_element *cmd, const char *value)
5106{
5107 switch (execution_direction) {
5108 case EXEC_FORWARD:
5109 fprintf_filtered (out, _("Forward.\n"));
5110 break;
5111 case EXEC_REVERSE:
5112 fprintf_filtered (out, _("Reverse.\n"));
5113 break;
5114 case EXEC_ERROR:
5115 default:
5116 fprintf_filtered (out,
5117 _("Forward (target `%s' does not support exec-direction).\n"),
5118 target_shortname);
5119 break;
5120 }
5121}
5122
5123/* User interface for non-stop mode. */
5124
ad52ddc6
PA
5125int non_stop = 0;
5126static int non_stop_1 = 0;
5127
5128static void
5129set_non_stop (char *args, int from_tty,
5130 struct cmd_list_element *c)
5131{
5132 if (target_has_execution)
5133 {
5134 non_stop_1 = non_stop;
5135 error (_("Cannot change this setting while the inferior is running."));
5136 }
5137
5138 non_stop = non_stop_1;
5139}
5140
5141static void
5142show_non_stop (struct ui_file *file, int from_tty,
5143 struct cmd_list_element *c, const char *value)
5144{
5145 fprintf_filtered (file,
5146 _("Controlling the inferior in non-stop mode is %s.\n"),
5147 value);
5148}
5149
5150
c906108c 5151void
96baa820 5152_initialize_infrun (void)
c906108c 5153{
52f0bd74
AC
5154 int i;
5155 int numsigs;
c906108c
SS
5156 struct cmd_list_element *c;
5157
1bedd215
AC
5158 add_info ("signals", signals_info, _("\
5159What debugger does when program gets various signals.\n\
5160Specify a signal as argument to print info on that signal only."));
c906108c
SS
5161 add_info_alias ("handle", "signals", 0);
5162
1bedd215
AC
5163 add_com ("handle", class_run, handle_command, _("\
5164Specify how to handle a signal.\n\
c906108c
SS
5165Args are signals and actions to apply to those signals.\n\
5166Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
5167from 1-15 are allowed for compatibility with old versions of GDB.\n\
5168Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
5169The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215
AC
5170used by the debugger, typically SIGTRAP and SIGINT.\n\
5171Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
c906108c
SS
5172\"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
5173Stop means reenter debugger if this signal happens (implies print).\n\
5174Print means print a message if this signal happens.\n\
5175Pass means let program see this signal; otherwise program doesn't know.\n\
5176Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1bedd215 5177Pass and Stop may be combined."));
c906108c
SS
5178 if (xdb_commands)
5179 {
1bedd215
AC
5180 add_com ("lz", class_info, signals_info, _("\
5181What debugger does when program gets various signals.\n\
5182Specify a signal as argument to print info on that signal only."));
5183 add_com ("z", class_run, xdb_handle_command, _("\
5184Specify how to handle a signal.\n\
c906108c
SS
5185Args are signals and actions to apply to those signals.\n\
5186Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
5187from 1-15 are allowed for compatibility with old versions of GDB.\n\
5188Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
5189The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215
AC
5190used by the debugger, typically SIGTRAP and SIGINT.\n\
5191Recognized actions include \"s\" (toggles between stop and nostop), \n\
c906108c
SS
5192\"r\" (toggles between print and noprint), \"i\" (toggles between pass and \
5193nopass), \"Q\" (noprint)\n\
5194Stop means reenter debugger if this signal happens (implies print).\n\
5195Print means print a message if this signal happens.\n\
5196Pass means let program see this signal; otherwise program doesn't know.\n\
5197Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
1bedd215 5198Pass and Stop may be combined."));
c906108c
SS
5199 }
5200
5201 if (!dbx_commands)
1a966eab
AC
5202 stop_command = add_cmd ("stop", class_obscure,
5203 not_just_help_class_command, _("\
5204There is no `stop' command, but you can set a hook on `stop'.\n\
c906108c 5205This allows you to set a list of commands to be run each time execution\n\
1a966eab 5206of the program stops."), &cmdlist);
c906108c 5207
85c07804
AC
5208 add_setshow_zinteger_cmd ("infrun", class_maintenance, &debug_infrun, _("\
5209Set inferior debugging."), _("\
5210Show inferior debugging."), _("\
5211When non-zero, inferior specific debugging is enabled."),
5212 NULL,
920d2a44 5213 show_debug_infrun,
85c07804 5214 &setdebuglist, &showdebuglist);
527159b7 5215
237fc4c9
PA
5216 add_setshow_boolean_cmd ("displaced", class_maintenance, &debug_displaced, _("\
5217Set displaced stepping debugging."), _("\
5218Show displaced stepping debugging."), _("\
5219When non-zero, displaced stepping specific debugging is enabled."),
5220 NULL,
5221 show_debug_displaced,
5222 &setdebuglist, &showdebuglist);
5223
ad52ddc6
PA
5224 add_setshow_boolean_cmd ("non-stop", no_class,
5225 &non_stop_1, _("\
5226Set whether gdb controls the inferior in non-stop mode."), _("\
5227Show whether gdb controls the inferior in non-stop mode."), _("\
5228When debugging a multi-threaded program and this setting is\n\
5229off (the default, also called all-stop mode), when one thread stops\n\
5230(for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
5231all other threads in the program while you interact with the thread of\n\
5232interest. When you continue or step a thread, you can allow the other\n\
5233threads to run, or have them remain stopped, but while you inspect any\n\
5234thread's state, all threads stop.\n\
5235\n\
5236In non-stop mode, when one thread stops, other threads can continue\n\
5237to run freely. You'll be able to step each thread independently,\n\
5238leave it stopped or free to run as needed."),
5239 set_non_stop,
5240 show_non_stop,
5241 &setlist,
5242 &showlist);
5243
c906108c 5244 numsigs = (int) TARGET_SIGNAL_LAST;
488f131b 5245 signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
c906108c
SS
5246 signal_print = (unsigned char *)
5247 xmalloc (sizeof (signal_print[0]) * numsigs);
5248 signal_program = (unsigned char *)
5249 xmalloc (sizeof (signal_program[0]) * numsigs);
5250 for (i = 0; i < numsigs; i++)
5251 {
5252 signal_stop[i] = 1;
5253 signal_print[i] = 1;
5254 signal_program[i] = 1;
5255 }
5256
5257 /* Signals caused by debugger's own actions
5258 should not be given to the program afterwards. */
5259 signal_program[TARGET_SIGNAL_TRAP] = 0;
5260 signal_program[TARGET_SIGNAL_INT] = 0;
5261
5262 /* Signals that are not errors should not normally enter the debugger. */
5263 signal_stop[TARGET_SIGNAL_ALRM] = 0;
5264 signal_print[TARGET_SIGNAL_ALRM] = 0;
5265 signal_stop[TARGET_SIGNAL_VTALRM] = 0;
5266 signal_print[TARGET_SIGNAL_VTALRM] = 0;
5267 signal_stop[TARGET_SIGNAL_PROF] = 0;
5268 signal_print[TARGET_SIGNAL_PROF] = 0;
5269 signal_stop[TARGET_SIGNAL_CHLD] = 0;
5270 signal_print[TARGET_SIGNAL_CHLD] = 0;
5271 signal_stop[TARGET_SIGNAL_IO] = 0;
5272 signal_print[TARGET_SIGNAL_IO] = 0;
5273 signal_stop[TARGET_SIGNAL_POLL] = 0;
5274 signal_print[TARGET_SIGNAL_POLL] = 0;
5275 signal_stop[TARGET_SIGNAL_URG] = 0;
5276 signal_print[TARGET_SIGNAL_URG] = 0;
5277 signal_stop[TARGET_SIGNAL_WINCH] = 0;
5278 signal_print[TARGET_SIGNAL_WINCH] = 0;
5279
cd0fc7c3
SS
5280 /* These signals are used internally by user-level thread
5281 implementations. (See signal(5) on Solaris.) Like the above
5282 signals, a healthy program receives and handles them as part of
5283 its normal operation. */
5284 signal_stop[TARGET_SIGNAL_LWP] = 0;
5285 signal_print[TARGET_SIGNAL_LWP] = 0;
5286 signal_stop[TARGET_SIGNAL_WAITING] = 0;
5287 signal_print[TARGET_SIGNAL_WAITING] = 0;
5288 signal_stop[TARGET_SIGNAL_CANCEL] = 0;
5289 signal_print[TARGET_SIGNAL_CANCEL] = 0;
5290
85c07804
AC
5291 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
5292 &stop_on_solib_events, _("\
5293Set stopping for shared library events."), _("\
5294Show stopping for shared library events."), _("\
c906108c
SS
5295If nonzero, gdb will give control to the user when the dynamic linker\n\
5296notifies gdb of shared library events. The most common event of interest\n\
85c07804
AC
5297to the user would be loading/unloading of a new library."),
5298 NULL,
920d2a44 5299 show_stop_on_solib_events,
85c07804 5300 &setlist, &showlist);
c906108c 5301
7ab04401
AC
5302 add_setshow_enum_cmd ("follow-fork-mode", class_run,
5303 follow_fork_mode_kind_names,
5304 &follow_fork_mode_string, _("\
5305Set debugger response to a program call of fork or vfork."), _("\
5306Show debugger response to a program call of fork or vfork."), _("\
c906108c
SS
5307A fork or vfork creates a new process. follow-fork-mode can be:\n\
5308 parent - the original process is debugged after a fork\n\
5309 child - the new process is debugged after a fork\n\
ea1dd7bc 5310The unfollowed process will continue to run.\n\
7ab04401
AC
5311By default, the debugger will follow the parent process."),
5312 NULL,
920d2a44 5313 show_follow_fork_mode_string,
7ab04401
AC
5314 &setlist, &showlist);
5315
5316 add_setshow_enum_cmd ("scheduler-locking", class_run,
5317 scheduler_enums, &scheduler_mode, _("\
5318Set mode for locking scheduler during execution."), _("\
5319Show mode for locking scheduler during execution."), _("\
c906108c
SS
5320off == no locking (threads may preempt at any time)\n\
5321on == full locking (no thread except the current thread may run)\n\
5322step == scheduler locked during every single-step operation.\n\
5323 In this mode, no other thread may run during a step command.\n\
7ab04401
AC
5324 Other threads may run while stepping over a function call ('next')."),
5325 set_schedlock_func, /* traps on target vector */
920d2a44 5326 show_scheduler_mode,
7ab04401 5327 &setlist, &showlist);
5fbbeb29 5328
5bf193a2
AC
5329 add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
5330Set mode of the step operation."), _("\
5331Show mode of the step operation."), _("\
5332When set, doing a step over a function without debug line information\n\
5333will stop at the first instruction of that function. Otherwise, the\n\
5334function is skipped and the step command stops at a different source line."),
5335 NULL,
920d2a44 5336 show_step_stop_if_no_debug,
5bf193a2 5337 &setlist, &showlist);
ca6724c1 5338
fff08868
HZ
5339 add_setshow_enum_cmd ("displaced-stepping", class_run,
5340 can_use_displaced_stepping_enum,
5341 &can_use_displaced_stepping, _("\
237fc4c9
PA
5342Set debugger's willingness to use displaced stepping."), _("\
5343Show debugger's willingness to use displaced stepping."), _("\
fff08868
HZ
5344If on, gdb will use displaced stepping to step over breakpoints if it is\n\
5345supported by the target architecture. If off, gdb will not use displaced\n\
5346stepping to step over breakpoints, even if such is supported by the target\n\
5347architecture. If auto (which is the default), gdb will use displaced stepping\n\
5348if the target architecture supports it and non-stop mode is active, but will not\n\
5349use it in all-stop mode (see help set non-stop)."),
5350 NULL,
5351 show_can_use_displaced_stepping,
5352 &setlist, &showlist);
237fc4c9 5353
b2175913
MS
5354 add_setshow_enum_cmd ("exec-direction", class_run, exec_direction_names,
5355 &exec_direction, _("Set direction of execution.\n\
5356Options are 'forward' or 'reverse'."),
5357 _("Show direction of execution (forward/reverse)."),
5358 _("Tells gdb whether to execute forward or backward."),
5359 set_exec_direction_func, show_exec_direction_func,
5360 &setlist, &showlist);
5361
ca6724c1
KB
5362 /* ptid initializations */
5363 null_ptid = ptid_build (0, 0, 0);
5364 minus_one_ptid = ptid_build (-1, 0, 0);
5365 inferior_ptid = null_ptid;
5366 target_last_wait_ptid = minus_one_ptid;
237fc4c9 5367 displaced_step_ptid = null_ptid;
5231c1fd
PA
5368
5369 observer_attach_thread_ptid_changed (infrun_thread_ptid_changed);
252fbfc8 5370 observer_attach_thread_stop_requested (infrun_thread_stop_requested);
c906108c 5371}
This page took 1.054862 seconds and 4 git commands to generate.