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