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