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