Embed the pending step-over chain in thread_info objects
[deliverable/binutils-gdb.git] / gdb / infrun.c
CommitLineData
ca557f44
AC
1/* Target-struct-independent code to start (run) and stop an inferior
2 process.
8926118c 3
32d0add0 4 Copyright (C) 1986-2015 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
45741a9c 22#include "infrun.h"
c906108c
SS
23#include <ctype.h>
24#include "symtab.h"
25#include "frame.h"
26#include "inferior.h"
27#include "breakpoint.h"
03f2053f 28#include "gdb_wait.h"
c906108c
SS
29#include "gdbcore.h"
30#include "gdbcmd.h"
210661e7 31#include "cli/cli-script.h"
c906108c
SS
32#include "target.h"
33#include "gdbthread.h"
34#include "annotate.h"
1adeb98a 35#include "symfile.h"
7a292a7a 36#include "top.h"
c906108c 37#include <signal.h>
2acceee2 38#include "inf-loop.h"
4e052eda 39#include "regcache.h"
fd0407d6 40#include "value.h"
06600e06 41#include "observer.h"
f636b87d 42#include "language.h"
a77053c2 43#include "solib.h"
f17517ea 44#include "main.h"
186c406b
TT
45#include "dictionary.h"
46#include "block.h"
034dad6f 47#include "mi/mi-common.h"
4f8d22e3 48#include "event-top.h"
96429cc8 49#include "record.h"
d02ed0bb 50#include "record-full.h"
edb3359d 51#include "inline-frame.h"
4efc6507 52#include "jit.h"
06cd862c 53#include "tracepoint.h"
be34f849 54#include "continuations.h"
b4a14fd0 55#include "interps.h"
1bfeeb0f 56#include "skip.h"
28106bc2
SDJ
57#include "probe.h"
58#include "objfiles.h"
de0bea00 59#include "completer.h"
9107fc8d 60#include "target-descriptions.h"
f15cb84a 61#include "target-dcache.h"
d83ad864 62#include "terminal.h"
ff862be4 63#include "solist.h"
c906108c
SS
64
65/* Prototypes for local functions */
66
96baa820 67static void signals_info (char *, int);
c906108c 68
96baa820 69static void handle_command (char *, int);
c906108c 70
2ea28649 71static void sig_print_info (enum gdb_signal);
c906108c 72
96baa820 73static void sig_print_header (void);
c906108c 74
74b7792f 75static void resume_cleanups (void *);
c906108c 76
96baa820 77static int hook_stop_stub (void *);
c906108c 78
96baa820
JM
79static int restore_selected_frame (void *);
80
4ef3f3be 81static int follow_fork (void);
96baa820 82
d83ad864
DB
83static int follow_fork_inferior (int follow_child, int detach_fork);
84
85static void follow_inferior_reset_breakpoints (void);
86
96baa820 87static void set_schedlock_func (char *args, int from_tty,
488f131b 88 struct cmd_list_element *c);
96baa820 89
a289b8f6
JK
90static int currently_stepping (struct thread_info *tp);
91
96baa820 92void _initialize_infrun (void);
43ff13b4 93
e58b0e63
PA
94void nullify_last_target_wait_ptid (void);
95
2c03e5be 96static void insert_hp_step_resume_breakpoint_at_frame (struct frame_info *);
2484c66b
UW
97
98static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
99
2484c66b
UW
100static void insert_longjmp_resume_breakpoint (struct gdbarch *, CORE_ADDR);
101
8550d3b3
YQ
102static int maybe_software_singlestep (struct gdbarch *gdbarch, CORE_ADDR pc);
103
5fbbeb29
CF
104/* When set, stop the 'step' command if we enter a function which has
105 no line number information. The normal behavior is that we step
106 over such function. */
107int step_stop_if_no_debug = 0;
920d2a44
AC
108static void
109show_step_stop_if_no_debug (struct ui_file *file, int from_tty,
110 struct cmd_list_element *c, const char *value)
111{
112 fprintf_filtered (file, _("Mode of the step operation is %s.\n"), value);
113}
5fbbeb29 114
1777feb0 115/* In asynchronous mode, but simulating synchronous execution. */
96baa820 116
43ff13b4
JM
117int sync_execution = 0;
118
b9f437de
PA
119/* proceed and normal_stop use this to notify the user when the
120 inferior stopped in a different thread than it had been running
121 in. */
96baa820 122
39f77062 123static ptid_t previous_inferior_ptid;
7a292a7a 124
07107ca6
LM
125/* If set (default for legacy reasons), when following a fork, GDB
126 will detach from one of the fork branches, child or parent.
127 Exactly which branch is detached depends on 'set follow-fork-mode'
128 setting. */
129
130static int detach_fork = 1;
6c95b8df 131
237fc4c9
PA
132int debug_displaced = 0;
133static void
134show_debug_displaced (struct ui_file *file, int from_tty,
135 struct cmd_list_element *c, const char *value)
136{
137 fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
138}
139
ccce17b0 140unsigned int debug_infrun = 0;
920d2a44
AC
141static void
142show_debug_infrun (struct ui_file *file, int from_tty,
143 struct cmd_list_element *c, const char *value)
144{
145 fprintf_filtered (file, _("Inferior debugging is %s.\n"), value);
146}
527159b7 147
03583c20
UW
148
149/* Support for disabling address space randomization. */
150
151int disable_randomization = 1;
152
153static void
154show_disable_randomization (struct ui_file *file, int from_tty,
155 struct cmd_list_element *c, const char *value)
156{
157 if (target_supports_disable_randomization ())
158 fprintf_filtered (file,
159 _("Disabling randomization of debuggee's "
160 "virtual address space is %s.\n"),
161 value);
162 else
163 fputs_filtered (_("Disabling randomization of debuggee's "
164 "virtual address space is unsupported on\n"
165 "this platform.\n"), file);
166}
167
168static void
169set_disable_randomization (char *args, int from_tty,
170 struct cmd_list_element *c)
171{
172 if (!target_supports_disable_randomization ())
173 error (_("Disabling randomization of debuggee's "
174 "virtual address space is unsupported on\n"
175 "this platform."));
176}
177
d32dc48e
PA
178/* User interface for non-stop mode. */
179
180int non_stop = 0;
181static int non_stop_1 = 0;
182
183static void
184set_non_stop (char *args, int from_tty,
185 struct cmd_list_element *c)
186{
187 if (target_has_execution)
188 {
189 non_stop_1 = non_stop;
190 error (_("Cannot change this setting while the inferior is running."));
191 }
192
193 non_stop = non_stop_1;
194}
195
196static void
197show_non_stop (struct ui_file *file, int from_tty,
198 struct cmd_list_element *c, const char *value)
199{
200 fprintf_filtered (file,
201 _("Controlling the inferior in non-stop mode is %s.\n"),
202 value);
203}
204
d914c394
SS
205/* "Observer mode" is somewhat like a more extreme version of
206 non-stop, in which all GDB operations that might affect the
207 target's execution have been disabled. */
208
d914c394
SS
209int observer_mode = 0;
210static int observer_mode_1 = 0;
211
212static void
213set_observer_mode (char *args, int from_tty,
214 struct cmd_list_element *c)
215{
d914c394
SS
216 if (target_has_execution)
217 {
218 observer_mode_1 = observer_mode;
219 error (_("Cannot change this setting while the inferior is running."));
220 }
221
222 observer_mode = observer_mode_1;
223
224 may_write_registers = !observer_mode;
225 may_write_memory = !observer_mode;
226 may_insert_breakpoints = !observer_mode;
227 may_insert_tracepoints = !observer_mode;
228 /* We can insert fast tracepoints in or out of observer mode,
229 but enable them if we're going into this mode. */
230 if (observer_mode)
231 may_insert_fast_tracepoints = 1;
232 may_stop = !observer_mode;
233 update_target_permissions ();
234
235 /* Going *into* observer mode we must force non-stop, then
236 going out we leave it that way. */
237 if (observer_mode)
238 {
d914c394
SS
239 pagination_enabled = 0;
240 non_stop = non_stop_1 = 1;
241 }
242
243 if (from_tty)
244 printf_filtered (_("Observer mode is now %s.\n"),
245 (observer_mode ? "on" : "off"));
246}
247
248static void
249show_observer_mode (struct ui_file *file, int from_tty,
250 struct cmd_list_element *c, const char *value)
251{
252 fprintf_filtered (file, _("Observer mode is %s.\n"), value);
253}
254
255/* This updates the value of observer mode based on changes in
256 permissions. Note that we are deliberately ignoring the values of
257 may-write-registers and may-write-memory, since the user may have
258 reason to enable these during a session, for instance to turn on a
259 debugging-related global. */
260
261void
262update_observer_mode (void)
263{
264 int newval;
265
266 newval = (!may_insert_breakpoints
267 && !may_insert_tracepoints
268 && may_insert_fast_tracepoints
269 && !may_stop
270 && non_stop);
271
272 /* Let the user know if things change. */
273 if (newval != observer_mode)
274 printf_filtered (_("Observer mode is now %s.\n"),
275 (newval ? "on" : "off"));
276
277 observer_mode = observer_mode_1 = newval;
278}
c2c6d25f 279
c906108c
SS
280/* Tables of how to react to signals; the user sets them. */
281
282static unsigned char *signal_stop;
283static unsigned char *signal_print;
284static unsigned char *signal_program;
285
ab04a2af
TT
286/* Table of signals that are registered with "catch signal". A
287 non-zero entry indicates that the signal is caught by some "catch
288 signal" command. This has size GDB_SIGNAL_LAST, to accommodate all
289 signals. */
290static unsigned char *signal_catch;
291
2455069d
UW
292/* Table of signals that the target may silently handle.
293 This is automatically determined from the flags above,
294 and simply cached here. */
295static unsigned char *signal_pass;
296
c906108c
SS
297#define SET_SIGS(nsigs,sigs,flags) \
298 do { \
299 int signum = (nsigs); \
300 while (signum-- > 0) \
301 if ((sigs)[signum]) \
302 (flags)[signum] = 1; \
303 } while (0)
304
305#define UNSET_SIGS(nsigs,sigs,flags) \
306 do { \
307 int signum = (nsigs); \
308 while (signum-- > 0) \
309 if ((sigs)[signum]) \
310 (flags)[signum] = 0; \
311 } while (0)
312
9b224c5e
PA
313/* Update the target's copy of SIGNAL_PROGRAM. The sole purpose of
314 this function is to avoid exporting `signal_program'. */
315
316void
317update_signals_program_target (void)
318{
a493e3e2 319 target_program_signals ((int) GDB_SIGNAL_LAST, signal_program);
9b224c5e
PA
320}
321
1777feb0 322/* Value to pass to target_resume() to cause all threads to resume. */
39f77062 323
edb3359d 324#define RESUME_ALL minus_one_ptid
c906108c
SS
325
326/* Command list pointer for the "stop" placeholder. */
327
328static struct cmd_list_element *stop_command;
329
c906108c
SS
330/* Nonzero if we want to give control to the user when we're notified
331 of shared library events by the dynamic linker. */
628fe4e4 332int stop_on_solib_events;
f9e14852
GB
333
334/* Enable or disable optional shared library event breakpoints
335 as appropriate when the above flag is changed. */
336
337static void
338set_stop_on_solib_events (char *args, int from_tty, struct cmd_list_element *c)
339{
340 update_solib_breakpoints ();
341}
342
920d2a44
AC
343static void
344show_stop_on_solib_events (struct ui_file *file, int from_tty,
345 struct cmd_list_element *c, const char *value)
346{
347 fprintf_filtered (file, _("Stopping for shared library events is %s.\n"),
348 value);
349}
c906108c 350
c906108c
SS
351/* Nonzero means expecting a trace trap
352 and should stop the inferior and return silently when it happens. */
353
354int stop_after_trap;
355
c906108c
SS
356/* Nonzero after stop if current stack frame should be printed. */
357
358static int stop_print_frame;
359
e02bc4cc 360/* This is a cached copy of the pid/waitstatus of the last event
9a4105ab
AC
361 returned by target_wait()/deprecated_target_wait_hook(). This
362 information is returned by get_last_target_status(). */
39f77062 363static ptid_t target_last_wait_ptid;
e02bc4cc
DS
364static struct target_waitstatus target_last_waitstatus;
365
0d1e5fa7
PA
366static void context_switch (ptid_t ptid);
367
4e1c45ea 368void init_thread_stepping_state (struct thread_info *tss);
0d1e5fa7 369
53904c9e
AC
370static const char follow_fork_mode_child[] = "child";
371static const char follow_fork_mode_parent[] = "parent";
372
40478521 373static const char *const follow_fork_mode_kind_names[] = {
53904c9e
AC
374 follow_fork_mode_child,
375 follow_fork_mode_parent,
376 NULL
ef346e04 377};
c906108c 378
53904c9e 379static const char *follow_fork_mode_string = follow_fork_mode_parent;
920d2a44
AC
380static void
381show_follow_fork_mode_string (struct ui_file *file, int from_tty,
382 struct cmd_list_element *c, const char *value)
383{
3e43a32a
MS
384 fprintf_filtered (file,
385 _("Debugger response to a program "
386 "call of fork or vfork is \"%s\".\n"),
920d2a44
AC
387 value);
388}
c906108c
SS
389\f
390
d83ad864
DB
391/* Handle changes to the inferior list based on the type of fork,
392 which process is being followed, and whether the other process
393 should be detached. On entry inferior_ptid must be the ptid of
394 the fork parent. At return inferior_ptid is the ptid of the
395 followed inferior. */
396
397static int
398follow_fork_inferior (int follow_child, int detach_fork)
399{
400 int has_vforked;
79639e11 401 ptid_t parent_ptid, child_ptid;
d83ad864
DB
402
403 has_vforked = (inferior_thread ()->pending_follow.kind
404 == TARGET_WAITKIND_VFORKED);
79639e11
PA
405 parent_ptid = inferior_ptid;
406 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
d83ad864
DB
407
408 if (has_vforked
409 && !non_stop /* Non-stop always resumes both branches. */
410 && (!target_is_async_p () || sync_execution)
411 && !(follow_child || detach_fork || sched_multi))
412 {
413 /* The parent stays blocked inside the vfork syscall until the
414 child execs or exits. If we don't let the child run, then
415 the parent stays blocked. If we're telling the parent to run
416 in the foreground, the user will not be able to ctrl-c to get
417 back the terminal, effectively hanging the debug session. */
418 fprintf_filtered (gdb_stderr, _("\
419Can not resume the parent process over vfork in the foreground while\n\
420holding the child stopped. Try \"set detach-on-fork\" or \
421\"set schedule-multiple\".\n"));
422 /* FIXME output string > 80 columns. */
423 return 1;
424 }
425
426 if (!follow_child)
427 {
428 /* Detach new forked process? */
429 if (detach_fork)
430 {
431 struct cleanup *old_chain;
432
433 /* Before detaching from the child, remove all breakpoints
434 from it. If we forked, then this has already been taken
435 care of by infrun.c. If we vforked however, any
436 breakpoint inserted in the parent is visible in the
437 child, even those added while stopped in a vfork
438 catchpoint. This will remove the breakpoints from the
439 parent also, but they'll be reinserted below. */
440 if (has_vforked)
441 {
442 /* Keep breakpoints list in sync. */
443 remove_breakpoints_pid (ptid_get_pid (inferior_ptid));
444 }
445
446 if (info_verbose || debug_infrun)
447 {
8dd06f7a
DB
448 /* Ensure that we have a process ptid. */
449 ptid_t process_ptid = pid_to_ptid (ptid_get_pid (child_ptid));
450
6f259a23 451 target_terminal_ours_for_output ();
d83ad864 452 fprintf_filtered (gdb_stdlog,
79639e11 453 _("Detaching after %s from child %s.\n"),
6f259a23 454 has_vforked ? "vfork" : "fork",
8dd06f7a 455 target_pid_to_str (process_ptid));
d83ad864
DB
456 }
457 }
458 else
459 {
460 struct inferior *parent_inf, *child_inf;
461 struct cleanup *old_chain;
462
463 /* Add process to GDB's tables. */
79639e11 464 child_inf = add_inferior (ptid_get_pid (child_ptid));
d83ad864
DB
465
466 parent_inf = current_inferior ();
467 child_inf->attach_flag = parent_inf->attach_flag;
468 copy_terminal_info (child_inf, parent_inf);
469 child_inf->gdbarch = parent_inf->gdbarch;
470 copy_inferior_target_desc_info (child_inf, parent_inf);
471
472 old_chain = save_inferior_ptid ();
473 save_current_program_space ();
474
79639e11 475 inferior_ptid = child_ptid;
d83ad864
DB
476 add_thread (inferior_ptid);
477 child_inf->symfile_flags = SYMFILE_NO_READ;
478
479 /* If this is a vfork child, then the address-space is
480 shared with the parent. */
481 if (has_vforked)
482 {
483 child_inf->pspace = parent_inf->pspace;
484 child_inf->aspace = parent_inf->aspace;
485
486 /* The parent will be frozen until the child is done
487 with the shared region. Keep track of the
488 parent. */
489 child_inf->vfork_parent = parent_inf;
490 child_inf->pending_detach = 0;
491 parent_inf->vfork_child = child_inf;
492 parent_inf->pending_detach = 0;
493 }
494 else
495 {
496 child_inf->aspace = new_address_space ();
497 child_inf->pspace = add_program_space (child_inf->aspace);
498 child_inf->removable = 1;
499 set_current_program_space (child_inf->pspace);
500 clone_program_space (child_inf->pspace, parent_inf->pspace);
501
502 /* Let the shared library layer (e.g., solib-svr4) learn
503 about this new process, relocate the cloned exec, pull
504 in shared libraries, and install the solib event
505 breakpoint. If a "cloned-VM" event was propagated
506 better throughout the core, this wouldn't be
507 required. */
508 solib_create_inferior_hook (0);
509 }
510
511 do_cleanups (old_chain);
512 }
513
514 if (has_vforked)
515 {
516 struct inferior *parent_inf;
517
518 parent_inf = current_inferior ();
519
520 /* If we detached from the child, then we have to be careful
521 to not insert breakpoints in the parent until the child
522 is done with the shared memory region. However, if we're
523 staying attached to the child, then we can and should
524 insert breakpoints, so that we can debug it. A
525 subsequent child exec or exit is enough to know when does
526 the child stops using the parent's address space. */
527 parent_inf->waiting_for_vfork_done = detach_fork;
528 parent_inf->pspace->breakpoints_not_allowed = detach_fork;
529 }
530 }
531 else
532 {
533 /* Follow the child. */
534 struct inferior *parent_inf, *child_inf;
535 struct program_space *parent_pspace;
536
537 if (info_verbose || debug_infrun)
538 {
6f259a23
DB
539 target_terminal_ours_for_output ();
540 fprintf_filtered (gdb_stdlog,
79639e11
PA
541 _("Attaching after %s %s to child %s.\n"),
542 target_pid_to_str (parent_ptid),
6f259a23 543 has_vforked ? "vfork" : "fork",
79639e11 544 target_pid_to_str (child_ptid));
d83ad864
DB
545 }
546
547 /* Add the new inferior first, so that the target_detach below
548 doesn't unpush the target. */
549
79639e11 550 child_inf = add_inferior (ptid_get_pid (child_ptid));
d83ad864
DB
551
552 parent_inf = current_inferior ();
553 child_inf->attach_flag = parent_inf->attach_flag;
554 copy_terminal_info (child_inf, parent_inf);
555 child_inf->gdbarch = parent_inf->gdbarch;
556 copy_inferior_target_desc_info (child_inf, parent_inf);
557
558 parent_pspace = parent_inf->pspace;
559
560 /* If we're vforking, we want to hold on to the parent until the
561 child exits or execs. At child exec or exit time we can
562 remove the old breakpoints from the parent and detach or
563 resume debugging it. Otherwise, detach the parent now; we'll
564 want to reuse it's program/address spaces, but we can't set
565 them to the child before removing breakpoints from the
566 parent, otherwise, the breakpoints module could decide to
567 remove breakpoints from the wrong process (since they'd be
568 assigned to the same address space). */
569
570 if (has_vforked)
571 {
572 gdb_assert (child_inf->vfork_parent == NULL);
573 gdb_assert (parent_inf->vfork_child == NULL);
574 child_inf->vfork_parent = parent_inf;
575 child_inf->pending_detach = 0;
576 parent_inf->vfork_child = child_inf;
577 parent_inf->pending_detach = detach_fork;
578 parent_inf->waiting_for_vfork_done = 0;
579 }
580 else if (detach_fork)
6f259a23
DB
581 {
582 if (info_verbose || debug_infrun)
583 {
8dd06f7a
DB
584 /* Ensure that we have a process ptid. */
585 ptid_t process_ptid = pid_to_ptid (ptid_get_pid (child_ptid));
586
6f259a23
DB
587 target_terminal_ours_for_output ();
588 fprintf_filtered (gdb_stdlog,
589 _("Detaching after fork from "
79639e11 590 "child %s.\n"),
8dd06f7a 591 target_pid_to_str (process_ptid));
6f259a23
DB
592 }
593
594 target_detach (NULL, 0);
595 }
d83ad864
DB
596
597 /* Note that the detach above makes PARENT_INF dangling. */
598
599 /* Add the child thread to the appropriate lists, and switch to
600 this new thread, before cloning the program space, and
601 informing the solib layer about this new process. */
602
79639e11 603 inferior_ptid = child_ptid;
d83ad864
DB
604 add_thread (inferior_ptid);
605
606 /* If this is a vfork child, then the address-space is shared
607 with the parent. If we detached from the parent, then we can
608 reuse the parent's program/address spaces. */
609 if (has_vforked || detach_fork)
610 {
611 child_inf->pspace = parent_pspace;
612 child_inf->aspace = child_inf->pspace->aspace;
613 }
614 else
615 {
616 child_inf->aspace = new_address_space ();
617 child_inf->pspace = add_program_space (child_inf->aspace);
618 child_inf->removable = 1;
619 child_inf->symfile_flags = SYMFILE_NO_READ;
620 set_current_program_space (child_inf->pspace);
621 clone_program_space (child_inf->pspace, parent_pspace);
622
623 /* Let the shared library layer (e.g., solib-svr4) learn
624 about this new process, relocate the cloned exec, pull in
625 shared libraries, and install the solib event breakpoint.
626 If a "cloned-VM" event was propagated better throughout
627 the core, this wouldn't be required. */
628 solib_create_inferior_hook (0);
629 }
630 }
631
632 return target_follow_fork (follow_child, detach_fork);
633}
634
e58b0e63
PA
635/* Tell the target to follow the fork we're stopped at. Returns true
636 if the inferior should be resumed; false, if the target for some
637 reason decided it's best not to resume. */
638
6604731b 639static int
4ef3f3be 640follow_fork (void)
c906108c 641{
ea1dd7bc 642 int follow_child = (follow_fork_mode_string == follow_fork_mode_child);
e58b0e63
PA
643 int should_resume = 1;
644 struct thread_info *tp;
645
646 /* Copy user stepping state to the new inferior thread. FIXME: the
647 followed fork child thread should have a copy of most of the
4e3990f4
DE
648 parent thread structure's run control related fields, not just these.
649 Initialized to avoid "may be used uninitialized" warnings from gcc. */
650 struct breakpoint *step_resume_breakpoint = NULL;
186c406b 651 struct breakpoint *exception_resume_breakpoint = NULL;
4e3990f4
DE
652 CORE_ADDR step_range_start = 0;
653 CORE_ADDR step_range_end = 0;
654 struct frame_id step_frame_id = { 0 };
17b2616c 655 struct interp *command_interp = NULL;
e58b0e63
PA
656
657 if (!non_stop)
658 {
659 ptid_t wait_ptid;
660 struct target_waitstatus wait_status;
661
662 /* Get the last target status returned by target_wait(). */
663 get_last_target_status (&wait_ptid, &wait_status);
664
665 /* If not stopped at a fork event, then there's nothing else to
666 do. */
667 if (wait_status.kind != TARGET_WAITKIND_FORKED
668 && wait_status.kind != TARGET_WAITKIND_VFORKED)
669 return 1;
670
671 /* Check if we switched over from WAIT_PTID, since the event was
672 reported. */
673 if (!ptid_equal (wait_ptid, minus_one_ptid)
674 && !ptid_equal (inferior_ptid, wait_ptid))
675 {
676 /* We did. Switch back to WAIT_PTID thread, to tell the
677 target to follow it (in either direction). We'll
678 afterwards refuse to resume, and inform the user what
679 happened. */
680 switch_to_thread (wait_ptid);
681 should_resume = 0;
682 }
683 }
684
685 tp = inferior_thread ();
686
687 /* If there were any forks/vforks that were caught and are now to be
688 followed, then do so now. */
689 switch (tp->pending_follow.kind)
690 {
691 case TARGET_WAITKIND_FORKED:
692 case TARGET_WAITKIND_VFORKED:
693 {
694 ptid_t parent, child;
695
696 /* If the user did a next/step, etc, over a fork call,
697 preserve the stepping state in the fork child. */
698 if (follow_child && should_resume)
699 {
8358c15c
JK
700 step_resume_breakpoint = clone_momentary_breakpoint
701 (tp->control.step_resume_breakpoint);
16c381f0
JK
702 step_range_start = tp->control.step_range_start;
703 step_range_end = tp->control.step_range_end;
704 step_frame_id = tp->control.step_frame_id;
186c406b
TT
705 exception_resume_breakpoint
706 = clone_momentary_breakpoint (tp->control.exception_resume_breakpoint);
17b2616c 707 command_interp = tp->control.command_interp;
e58b0e63
PA
708
709 /* For now, delete the parent's sr breakpoint, otherwise,
710 parent/child sr breakpoints are considered duplicates,
711 and the child version will not be installed. Remove
712 this when the breakpoints module becomes aware of
713 inferiors and address spaces. */
714 delete_step_resume_breakpoint (tp);
16c381f0
JK
715 tp->control.step_range_start = 0;
716 tp->control.step_range_end = 0;
717 tp->control.step_frame_id = null_frame_id;
186c406b 718 delete_exception_resume_breakpoint (tp);
17b2616c 719 tp->control.command_interp = NULL;
e58b0e63
PA
720 }
721
722 parent = inferior_ptid;
723 child = tp->pending_follow.value.related_pid;
724
d83ad864
DB
725 /* Set up inferior(s) as specified by the caller, and tell the
726 target to do whatever is necessary to follow either parent
727 or child. */
728 if (follow_fork_inferior (follow_child, detach_fork))
e58b0e63
PA
729 {
730 /* Target refused to follow, or there's some other reason
731 we shouldn't resume. */
732 should_resume = 0;
733 }
734 else
735 {
736 /* This pending follow fork event is now handled, one way
737 or another. The previous selected thread may be gone
738 from the lists by now, but if it is still around, need
739 to clear the pending follow request. */
e09875d4 740 tp = find_thread_ptid (parent);
e58b0e63
PA
741 if (tp)
742 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
743
744 /* This makes sure we don't try to apply the "Switched
745 over from WAIT_PID" logic above. */
746 nullify_last_target_wait_ptid ();
747
1777feb0 748 /* If we followed the child, switch to it... */
e58b0e63
PA
749 if (follow_child)
750 {
751 switch_to_thread (child);
752
753 /* ... and preserve the stepping state, in case the
754 user was stepping over the fork call. */
755 if (should_resume)
756 {
757 tp = inferior_thread ();
8358c15c
JK
758 tp->control.step_resume_breakpoint
759 = step_resume_breakpoint;
16c381f0
JK
760 tp->control.step_range_start = step_range_start;
761 tp->control.step_range_end = step_range_end;
762 tp->control.step_frame_id = step_frame_id;
186c406b
TT
763 tp->control.exception_resume_breakpoint
764 = exception_resume_breakpoint;
17b2616c 765 tp->control.command_interp = command_interp;
e58b0e63
PA
766 }
767 else
768 {
769 /* If we get here, it was because we're trying to
770 resume from a fork catchpoint, but, the user
771 has switched threads away from the thread that
772 forked. In that case, the resume command
773 issued is most likely not applicable to the
774 child, so just warn, and refuse to resume. */
3e43a32a
MS
775 warning (_("Not resuming: switched threads "
776 "before following fork child.\n"));
e58b0e63
PA
777 }
778
779 /* Reset breakpoints in the child as appropriate. */
780 follow_inferior_reset_breakpoints ();
781 }
782 else
783 switch_to_thread (parent);
784 }
785 }
786 break;
787 case TARGET_WAITKIND_SPURIOUS:
788 /* Nothing to follow. */
789 break;
790 default:
791 internal_error (__FILE__, __LINE__,
792 "Unexpected pending_follow.kind %d\n",
793 tp->pending_follow.kind);
794 break;
795 }
c906108c 796
e58b0e63 797 return should_resume;
c906108c
SS
798}
799
d83ad864 800static void
6604731b 801follow_inferior_reset_breakpoints (void)
c906108c 802{
4e1c45ea
PA
803 struct thread_info *tp = inferior_thread ();
804
6604731b
DJ
805 /* Was there a step_resume breakpoint? (There was if the user
806 did a "next" at the fork() call.) If so, explicitly reset its
a1aa2221
LM
807 thread number. Cloned step_resume breakpoints are disabled on
808 creation, so enable it here now that it is associated with the
809 correct thread.
6604731b
DJ
810
811 step_resumes are a form of bp that are made to be per-thread.
812 Since we created the step_resume bp when the parent process
813 was being debugged, and now are switching to the child process,
814 from the breakpoint package's viewpoint, that's a switch of
815 "threads". We must update the bp's notion of which thread
816 it is for, or it'll be ignored when it triggers. */
817
8358c15c 818 if (tp->control.step_resume_breakpoint)
a1aa2221
LM
819 {
820 breakpoint_re_set_thread (tp->control.step_resume_breakpoint);
821 tp->control.step_resume_breakpoint->loc->enabled = 1;
822 }
6604731b 823
a1aa2221 824 /* Treat exception_resume breakpoints like step_resume breakpoints. */
186c406b 825 if (tp->control.exception_resume_breakpoint)
a1aa2221
LM
826 {
827 breakpoint_re_set_thread (tp->control.exception_resume_breakpoint);
828 tp->control.exception_resume_breakpoint->loc->enabled = 1;
829 }
186c406b 830
6604731b
DJ
831 /* Reinsert all breakpoints in the child. The user may have set
832 breakpoints after catching the fork, in which case those
833 were never set in the child, but only in the parent. This makes
834 sure the inserted breakpoints match the breakpoint list. */
835
836 breakpoint_re_set ();
837 insert_breakpoints ();
c906108c 838}
c906108c 839
6c95b8df
PA
840/* The child has exited or execed: resume threads of the parent the
841 user wanted to be executing. */
842
843static int
844proceed_after_vfork_done (struct thread_info *thread,
845 void *arg)
846{
847 int pid = * (int *) arg;
848
849 if (ptid_get_pid (thread->ptid) == pid
850 && is_running (thread->ptid)
851 && !is_executing (thread->ptid)
852 && !thread->stop_requested
a493e3e2 853 && thread->suspend.stop_signal == GDB_SIGNAL_0)
6c95b8df
PA
854 {
855 if (debug_infrun)
856 fprintf_unfiltered (gdb_stdlog,
857 "infrun: resuming vfork parent thread %s\n",
858 target_pid_to_str (thread->ptid));
859
860 switch_to_thread (thread->ptid);
70509625 861 clear_proceed_status (0);
64ce06e4 862 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
6c95b8df
PA
863 }
864
865 return 0;
866}
867
868/* Called whenever we notice an exec or exit event, to handle
869 detaching or resuming a vfork parent. */
870
871static void
872handle_vfork_child_exec_or_exit (int exec)
873{
874 struct inferior *inf = current_inferior ();
875
876 if (inf->vfork_parent)
877 {
878 int resume_parent = -1;
879
880 /* This exec or exit marks the end of the shared memory region
881 between the parent and the child. If the user wanted to
882 detach from the parent, now is the time. */
883
884 if (inf->vfork_parent->pending_detach)
885 {
886 struct thread_info *tp;
887 struct cleanup *old_chain;
888 struct program_space *pspace;
889 struct address_space *aspace;
890
1777feb0 891 /* follow-fork child, detach-on-fork on. */
6c95b8df 892
68c9da30
PA
893 inf->vfork_parent->pending_detach = 0;
894
f50f4e56
PA
895 if (!exec)
896 {
897 /* If we're handling a child exit, then inferior_ptid
898 points at the inferior's pid, not to a thread. */
899 old_chain = save_inferior_ptid ();
900 save_current_program_space ();
901 save_current_inferior ();
902 }
903 else
904 old_chain = save_current_space_and_thread ();
6c95b8df
PA
905
906 /* We're letting loose of the parent. */
907 tp = any_live_thread_of_process (inf->vfork_parent->pid);
908 switch_to_thread (tp->ptid);
909
910 /* We're about to detach from the parent, which implicitly
911 removes breakpoints from its address space. There's a
912 catch here: we want to reuse the spaces for the child,
913 but, parent/child are still sharing the pspace at this
914 point, although the exec in reality makes the kernel give
915 the child a fresh set of new pages. The problem here is
916 that the breakpoints module being unaware of this, would
917 likely chose the child process to write to the parent
918 address space. Swapping the child temporarily away from
919 the spaces has the desired effect. Yes, this is "sort
920 of" a hack. */
921
922 pspace = inf->pspace;
923 aspace = inf->aspace;
924 inf->aspace = NULL;
925 inf->pspace = NULL;
926
927 if (debug_infrun || info_verbose)
928 {
6f259a23 929 target_terminal_ours_for_output ();
6c95b8df
PA
930
931 if (exec)
6f259a23
DB
932 {
933 fprintf_filtered (gdb_stdlog,
934 _("Detaching vfork parent process "
935 "%d after child exec.\n"),
936 inf->vfork_parent->pid);
937 }
6c95b8df 938 else
6f259a23
DB
939 {
940 fprintf_filtered (gdb_stdlog,
941 _("Detaching vfork parent process "
942 "%d after child exit.\n"),
943 inf->vfork_parent->pid);
944 }
6c95b8df
PA
945 }
946
947 target_detach (NULL, 0);
948
949 /* Put it back. */
950 inf->pspace = pspace;
951 inf->aspace = aspace;
952
953 do_cleanups (old_chain);
954 }
955 else if (exec)
956 {
957 /* We're staying attached to the parent, so, really give the
958 child a new address space. */
959 inf->pspace = add_program_space (maybe_new_address_space ());
960 inf->aspace = inf->pspace->aspace;
961 inf->removable = 1;
962 set_current_program_space (inf->pspace);
963
964 resume_parent = inf->vfork_parent->pid;
965
966 /* Break the bonds. */
967 inf->vfork_parent->vfork_child = NULL;
968 }
969 else
970 {
971 struct cleanup *old_chain;
972 struct program_space *pspace;
973
974 /* If this is a vfork child exiting, then the pspace and
975 aspaces were shared with the parent. Since we're
976 reporting the process exit, we'll be mourning all that is
977 found in the address space, and switching to null_ptid,
978 preparing to start a new inferior. But, since we don't
979 want to clobber the parent's address/program spaces, we
980 go ahead and create a new one for this exiting
981 inferior. */
982
983 /* Switch to null_ptid, so that clone_program_space doesn't want
984 to read the selected frame of a dead process. */
985 old_chain = save_inferior_ptid ();
986 inferior_ptid = null_ptid;
987
988 /* This inferior is dead, so avoid giving the breakpoints
989 module the option to write through to it (cloning a
990 program space resets breakpoints). */
991 inf->aspace = NULL;
992 inf->pspace = NULL;
993 pspace = add_program_space (maybe_new_address_space ());
994 set_current_program_space (pspace);
995 inf->removable = 1;
7dcd53a0 996 inf->symfile_flags = SYMFILE_NO_READ;
6c95b8df
PA
997 clone_program_space (pspace, inf->vfork_parent->pspace);
998 inf->pspace = pspace;
999 inf->aspace = pspace->aspace;
1000
1001 /* Put back inferior_ptid. We'll continue mourning this
1777feb0 1002 inferior. */
6c95b8df
PA
1003 do_cleanups (old_chain);
1004
1005 resume_parent = inf->vfork_parent->pid;
1006 /* Break the bonds. */
1007 inf->vfork_parent->vfork_child = NULL;
1008 }
1009
1010 inf->vfork_parent = NULL;
1011
1012 gdb_assert (current_program_space == inf->pspace);
1013
1014 if (non_stop && resume_parent != -1)
1015 {
1016 /* If the user wanted the parent to be running, let it go
1017 free now. */
1018 struct cleanup *old_chain = make_cleanup_restore_current_thread ();
1019
1020 if (debug_infrun)
3e43a32a
MS
1021 fprintf_unfiltered (gdb_stdlog,
1022 "infrun: resuming vfork parent process %d\n",
6c95b8df
PA
1023 resume_parent);
1024
1025 iterate_over_threads (proceed_after_vfork_done, &resume_parent);
1026
1027 do_cleanups (old_chain);
1028 }
1029 }
1030}
1031
eb6c553b 1032/* Enum strings for "set|show follow-exec-mode". */
6c95b8df
PA
1033
1034static const char follow_exec_mode_new[] = "new";
1035static const char follow_exec_mode_same[] = "same";
40478521 1036static const char *const follow_exec_mode_names[] =
6c95b8df
PA
1037{
1038 follow_exec_mode_new,
1039 follow_exec_mode_same,
1040 NULL,
1041};
1042
1043static const char *follow_exec_mode_string = follow_exec_mode_same;
1044static void
1045show_follow_exec_mode_string (struct ui_file *file, int from_tty,
1046 struct cmd_list_element *c, const char *value)
1047{
1048 fprintf_filtered (file, _("Follow exec mode is \"%s\".\n"), value);
1049}
1050
1777feb0 1051/* EXECD_PATHNAME is assumed to be non-NULL. */
1adeb98a 1052
c906108c 1053static void
95e50b27 1054follow_exec (ptid_t ptid, char *execd_pathname)
c906108c 1055{
95e50b27 1056 struct thread_info *th, *tmp;
6c95b8df 1057 struct inferior *inf = current_inferior ();
95e50b27 1058 int pid = ptid_get_pid (ptid);
7a292a7a 1059
c906108c
SS
1060 /* This is an exec event that we actually wish to pay attention to.
1061 Refresh our symbol table to the newly exec'd program, remove any
1062 momentary bp's, etc.
1063
1064 If there are breakpoints, they aren't really inserted now,
1065 since the exec() transformed our inferior into a fresh set
1066 of instructions.
1067
1068 We want to preserve symbolic breakpoints on the list, since
1069 we have hopes that they can be reset after the new a.out's
1070 symbol table is read.
1071
1072 However, any "raw" breakpoints must be removed from the list
1073 (e.g., the solib bp's), since their address is probably invalid
1074 now.
1075
1076 And, we DON'T want to call delete_breakpoints() here, since
1077 that may write the bp's "shadow contents" (the instruction
1078 value that was overwritten witha TRAP instruction). Since
1777feb0 1079 we now have a new a.out, those shadow contents aren't valid. */
6c95b8df
PA
1080
1081 mark_breakpoints_out ();
1082
95e50b27
PA
1083 /* The target reports the exec event to the main thread, even if
1084 some other thread does the exec, and even if the main thread was
1085 stopped or already gone. We may still have non-leader threads of
1086 the process on our list. E.g., on targets that don't have thread
1087 exit events (like remote); or on native Linux in non-stop mode if
1088 there were only two threads in the inferior and the non-leader
1089 one is the one that execs (and nothing forces an update of the
1090 thread list up to here). When debugging remotely, it's best to
1091 avoid extra traffic, when possible, so avoid syncing the thread
1092 list with the target, and instead go ahead and delete all threads
1093 of the process but one that reported the event. Note this must
1094 be done before calling update_breakpoints_after_exec, as
1095 otherwise clearing the threads' resources would reference stale
1096 thread breakpoints -- it may have been one of these threads that
1097 stepped across the exec. We could just clear their stepping
1098 states, but as long as we're iterating, might as well delete
1099 them. Deleting them now rather than at the next user-visible
1100 stop provides a nicer sequence of events for user and MI
1101 notifications. */
8a06aea7 1102 ALL_THREADS_SAFE (th, tmp)
95e50b27
PA
1103 if (ptid_get_pid (th->ptid) == pid && !ptid_equal (th->ptid, ptid))
1104 delete_thread (th->ptid);
1105
1106 /* We also need to clear any left over stale state for the
1107 leader/event thread. E.g., if there was any step-resume
1108 breakpoint or similar, it's gone now. We cannot truly
1109 step-to-next statement through an exec(). */
1110 th = inferior_thread ();
8358c15c 1111 th->control.step_resume_breakpoint = NULL;
186c406b 1112 th->control.exception_resume_breakpoint = NULL;
34b7e8a6 1113 th->control.single_step_breakpoints = NULL;
16c381f0
JK
1114 th->control.step_range_start = 0;
1115 th->control.step_range_end = 0;
c906108c 1116
95e50b27
PA
1117 /* The user may have had the main thread held stopped in the
1118 previous image (e.g., schedlock on, or non-stop). Release
1119 it now. */
a75724bc
PA
1120 th->stop_requested = 0;
1121
95e50b27
PA
1122 update_breakpoints_after_exec ();
1123
1777feb0 1124 /* What is this a.out's name? */
6c95b8df
PA
1125 printf_unfiltered (_("%s is executing new program: %s\n"),
1126 target_pid_to_str (inferior_ptid),
1127 execd_pathname);
c906108c
SS
1128
1129 /* We've followed the inferior through an exec. Therefore, the
1777feb0 1130 inferior has essentially been killed & reborn. */
7a292a7a 1131
c906108c 1132 gdb_flush (gdb_stdout);
6ca15a4b
PA
1133
1134 breakpoint_init_inferior (inf_execd);
e85a822c 1135
a3be80c3 1136 if (*gdb_sysroot != '\0')
e85a822c 1137 {
998d2a3e 1138 char *name = exec_file_find (execd_pathname, NULL);
ff862be4
GB
1139
1140 execd_pathname = alloca (strlen (name) + 1);
1141 strcpy (execd_pathname, name);
1142 xfree (name);
e85a822c 1143 }
c906108c 1144
cce9b6bf
PA
1145 /* Reset the shared library package. This ensures that we get a
1146 shlib event when the child reaches "_start", at which point the
1147 dld will have had a chance to initialize the child. */
1148 /* Also, loading a symbol file below may trigger symbol lookups, and
1149 we don't want those to be satisfied by the libraries of the
1150 previous incarnation of this process. */
1151 no_shared_libraries (NULL, 0);
1152
6c95b8df
PA
1153 if (follow_exec_mode_string == follow_exec_mode_new)
1154 {
1155 struct program_space *pspace;
6c95b8df
PA
1156
1157 /* The user wants to keep the old inferior and program spaces
1158 around. Create a new fresh one, and switch to it. */
1159
1160 inf = add_inferior (current_inferior ()->pid);
1161 pspace = add_program_space (maybe_new_address_space ());
1162 inf->pspace = pspace;
1163 inf->aspace = pspace->aspace;
1164
1165 exit_inferior_num_silent (current_inferior ()->num);
1166
1167 set_current_inferior (inf);
1168 set_current_program_space (pspace);
1169 }
9107fc8d
PA
1170 else
1171 {
1172 /* The old description may no longer be fit for the new image.
1173 E.g, a 64-bit process exec'ed a 32-bit process. Clear the
1174 old description; we'll read a new one below. No need to do
1175 this on "follow-exec-mode new", as the old inferior stays
1176 around (its description is later cleared/refetched on
1177 restart). */
1178 target_clear_description ();
1179 }
6c95b8df
PA
1180
1181 gdb_assert (current_program_space == inf->pspace);
1182
1777feb0 1183 /* That a.out is now the one to use. */
6c95b8df
PA
1184 exec_file_attach (execd_pathname, 0);
1185
c1e56572
JK
1186 /* SYMFILE_DEFER_BP_RESET is used as the proper displacement for PIE
1187 (Position Independent Executable) main symbol file will get applied by
1188 solib_create_inferior_hook below. breakpoint_re_set would fail to insert
1189 the breakpoints with the zero displacement. */
1190
7dcd53a0
TT
1191 symbol_file_add (execd_pathname,
1192 (inf->symfile_flags
1193 | SYMFILE_MAINLINE | SYMFILE_DEFER_BP_RESET),
c1e56572
JK
1194 NULL, 0);
1195
7dcd53a0
TT
1196 if ((inf->symfile_flags & SYMFILE_NO_READ) == 0)
1197 set_initial_language ();
c906108c 1198
9107fc8d
PA
1199 /* If the target can specify a description, read it. Must do this
1200 after flipping to the new executable (because the target supplied
1201 description must be compatible with the executable's
1202 architecture, and the old executable may e.g., be 32-bit, while
1203 the new one 64-bit), and before anything involving memory or
1204 registers. */
1205 target_find_description ();
1206
268a4a75 1207 solib_create_inferior_hook (0);
c906108c 1208
4efc6507
DE
1209 jit_inferior_created_hook ();
1210
c1e56572
JK
1211 breakpoint_re_set ();
1212
c906108c
SS
1213 /* Reinsert all breakpoints. (Those which were symbolic have
1214 been reset to the proper address in the new a.out, thanks
1777feb0 1215 to symbol_file_command...). */
c906108c
SS
1216 insert_breakpoints ();
1217
1218 /* The next resume of this inferior should bring it to the shlib
1219 startup breakpoints. (If the user had also set bp's on
1220 "main" from the old (parent) process, then they'll auto-
1777feb0 1221 matically get reset there in the new process.). */
c906108c
SS
1222}
1223
c2829269
PA
1224/* The queue of threads that need to do a step-over operation to get
1225 past e.g., a breakpoint. What technique is used to step over the
1226 breakpoint/watchpoint does not matter -- all threads end up in the
1227 same queue, to maintain rough temporal order of execution, in order
1228 to avoid starvation, otherwise, we could e.g., find ourselves
1229 constantly stepping the same couple threads past their breakpoints
1230 over and over, if the single-step finish fast enough. */
1231struct thread_info *step_over_queue_head;
1232
6c4cfb24
PA
1233/* Bit flags indicating what the thread needs to step over. */
1234
1235enum step_over_what
1236 {
1237 /* Step over a breakpoint. */
1238 STEP_OVER_BREAKPOINT = 1,
1239
1240 /* Step past a non-continuable watchpoint, in order to let the
1241 instruction execute so we can evaluate the watchpoint
1242 expression. */
1243 STEP_OVER_WATCHPOINT = 2
1244 };
1245
963f9c80 1246/* Info about an instruction that is being stepped over. */
31e77af2
PA
1247
1248struct step_over_info
1249{
963f9c80
PA
1250 /* If we're stepping past a breakpoint, this is the address space
1251 and address of the instruction the breakpoint is set at. We'll
1252 skip inserting all breakpoints here. Valid iff ASPACE is
1253 non-NULL. */
31e77af2 1254 struct address_space *aspace;
31e77af2 1255 CORE_ADDR address;
963f9c80
PA
1256
1257 /* The instruction being stepped over triggers a nonsteppable
1258 watchpoint. If true, we'll skip inserting watchpoints. */
1259 int nonsteppable_watchpoint_p;
31e77af2
PA
1260};
1261
1262/* The step-over info of the location that is being stepped over.
1263
1264 Note that with async/breakpoint always-inserted mode, a user might
1265 set a new breakpoint/watchpoint/etc. exactly while a breakpoint is
1266 being stepped over. As setting a new breakpoint inserts all
1267 breakpoints, we need to make sure the breakpoint being stepped over
1268 isn't inserted then. We do that by only clearing the step-over
1269 info when the step-over is actually finished (or aborted).
1270
1271 Presently GDB can only step over one breakpoint at any given time.
1272 Given threads that can't run code in the same address space as the
1273 breakpoint's can't really miss the breakpoint, GDB could be taught
1274 to step-over at most one breakpoint per address space (so this info
1275 could move to the address space object if/when GDB is extended).
1276 The set of breakpoints being stepped over will normally be much
1277 smaller than the set of all breakpoints, so a flag in the
1278 breakpoint location structure would be wasteful. A separate list
1279 also saves complexity and run-time, as otherwise we'd have to go
1280 through all breakpoint locations clearing their flag whenever we
1281 start a new sequence. Similar considerations weigh against storing
1282 this info in the thread object. Plus, not all step overs actually
1283 have breakpoint locations -- e.g., stepping past a single-step
1284 breakpoint, or stepping to complete a non-continuable
1285 watchpoint. */
1286static struct step_over_info step_over_info;
1287
1288/* Record the address of the breakpoint/instruction we're currently
1289 stepping over. */
1290
1291static void
963f9c80
PA
1292set_step_over_info (struct address_space *aspace, CORE_ADDR address,
1293 int nonsteppable_watchpoint_p)
31e77af2
PA
1294{
1295 step_over_info.aspace = aspace;
1296 step_over_info.address = address;
963f9c80 1297 step_over_info.nonsteppable_watchpoint_p = nonsteppable_watchpoint_p;
31e77af2
PA
1298}
1299
1300/* Called when we're not longer stepping over a breakpoint / an
1301 instruction, so all breakpoints are free to be (re)inserted. */
1302
1303static void
1304clear_step_over_info (void)
1305{
1306 step_over_info.aspace = NULL;
1307 step_over_info.address = 0;
963f9c80 1308 step_over_info.nonsteppable_watchpoint_p = 0;
31e77af2
PA
1309}
1310
7f89fd65 1311/* See infrun.h. */
31e77af2
PA
1312
1313int
1314stepping_past_instruction_at (struct address_space *aspace,
1315 CORE_ADDR address)
1316{
1317 return (step_over_info.aspace != NULL
1318 && breakpoint_address_match (aspace, address,
1319 step_over_info.aspace,
1320 step_over_info.address));
1321}
1322
963f9c80
PA
1323/* See infrun.h. */
1324
1325int
1326stepping_past_nonsteppable_watchpoint (void)
1327{
1328 return step_over_info.nonsteppable_watchpoint_p;
1329}
1330
6cc83d2a
PA
1331/* Returns true if step-over info is valid. */
1332
1333static int
1334step_over_info_valid_p (void)
1335{
963f9c80
PA
1336 return (step_over_info.aspace != NULL
1337 || stepping_past_nonsteppable_watchpoint ());
6cc83d2a
PA
1338}
1339
c906108c 1340\f
237fc4c9
PA
1341/* Displaced stepping. */
1342
1343/* In non-stop debugging mode, we must take special care to manage
1344 breakpoints properly; in particular, the traditional strategy for
1345 stepping a thread past a breakpoint it has hit is unsuitable.
1346 'Displaced stepping' is a tactic for stepping one thread past a
1347 breakpoint it has hit while ensuring that other threads running
1348 concurrently will hit the breakpoint as they should.
1349
1350 The traditional way to step a thread T off a breakpoint in a
1351 multi-threaded program in all-stop mode is as follows:
1352
1353 a0) Initially, all threads are stopped, and breakpoints are not
1354 inserted.
1355 a1) We single-step T, leaving breakpoints uninserted.
1356 a2) We insert breakpoints, and resume all threads.
1357
1358 In non-stop debugging, however, this strategy is unsuitable: we
1359 don't want to have to stop all threads in the system in order to
1360 continue or step T past a breakpoint. Instead, we use displaced
1361 stepping:
1362
1363 n0) Initially, T is stopped, other threads are running, and
1364 breakpoints are inserted.
1365 n1) We copy the instruction "under" the breakpoint to a separate
1366 location, outside the main code stream, making any adjustments
1367 to the instruction, register, and memory state as directed by
1368 T's architecture.
1369 n2) We single-step T over the instruction at its new location.
1370 n3) We adjust the resulting register and memory state as directed
1371 by T's architecture. This includes resetting T's PC to point
1372 back into the main instruction stream.
1373 n4) We resume T.
1374
1375 This approach depends on the following gdbarch methods:
1376
1377 - gdbarch_max_insn_length and gdbarch_displaced_step_location
1378 indicate where to copy the instruction, and how much space must
1379 be reserved there. We use these in step n1.
1380
1381 - gdbarch_displaced_step_copy_insn copies a instruction to a new
1382 address, and makes any necessary adjustments to the instruction,
1383 register contents, and memory. We use this in step n1.
1384
1385 - gdbarch_displaced_step_fixup adjusts registers and memory after
1386 we have successfuly single-stepped the instruction, to yield the
1387 same effect the instruction would have had if we had executed it
1388 at its original address. We use this in step n3.
1389
1390 - gdbarch_displaced_step_free_closure provides cleanup.
1391
1392 The gdbarch_displaced_step_copy_insn and
1393 gdbarch_displaced_step_fixup functions must be written so that
1394 copying an instruction with gdbarch_displaced_step_copy_insn,
1395 single-stepping across the copied instruction, and then applying
1396 gdbarch_displaced_insn_fixup should have the same effects on the
1397 thread's memory and registers as stepping the instruction in place
1398 would have. Exactly which responsibilities fall to the copy and
1399 which fall to the fixup is up to the author of those functions.
1400
1401 See the comments in gdbarch.sh for details.
1402
1403 Note that displaced stepping and software single-step cannot
1404 currently be used in combination, although with some care I think
1405 they could be made to. Software single-step works by placing
1406 breakpoints on all possible subsequent instructions; if the
1407 displaced instruction is a PC-relative jump, those breakpoints
1408 could fall in very strange places --- on pages that aren't
1409 executable, or at addresses that are not proper instruction
1410 boundaries. (We do generally let other threads run while we wait
1411 to hit the software single-step breakpoint, and they might
1412 encounter such a corrupted instruction.) One way to work around
1413 this would be to have gdbarch_displaced_step_copy_insn fully
1414 simulate the effect of PC-relative instructions (and return NULL)
1415 on architectures that use software single-stepping.
1416
1417 In non-stop mode, we can have independent and simultaneous step
1418 requests, so more than one thread may need to simultaneously step
1419 over a breakpoint. The current implementation assumes there is
1420 only one scratch space per process. In this case, we have to
1421 serialize access to the scratch space. If thread A wants to step
1422 over a breakpoint, but we are currently waiting for some other
1423 thread to complete a displaced step, we leave thread A stopped and
1424 place it in the displaced_step_request_queue. Whenever a displaced
1425 step finishes, we pick the next thread in the queue and start a new
1426 displaced step operation on it. See displaced_step_prepare and
1427 displaced_step_fixup for details. */
1428
fc1cf338
PA
1429/* Per-inferior displaced stepping state. */
1430struct displaced_step_inferior_state
1431{
1432 /* Pointer to next in linked list. */
1433 struct displaced_step_inferior_state *next;
1434
1435 /* The process this displaced step state refers to. */
1436 int pid;
1437
fc1cf338
PA
1438 /* If this is not null_ptid, this is the thread carrying out a
1439 displaced single-step in process PID. This thread's state will
1440 require fixing up once it has completed its step. */
1441 ptid_t step_ptid;
1442
1443 /* The architecture the thread had when we stepped it. */
1444 struct gdbarch *step_gdbarch;
1445
1446 /* The closure provided gdbarch_displaced_step_copy_insn, to be used
1447 for post-step cleanup. */
1448 struct displaced_step_closure *step_closure;
1449
1450 /* The address of the original instruction, and the copy we
1451 made. */
1452 CORE_ADDR step_original, step_copy;
1453
1454 /* Saved contents of copy area. */
1455 gdb_byte *step_saved_copy;
1456};
1457
1458/* The list of states of processes involved in displaced stepping
1459 presently. */
1460static struct displaced_step_inferior_state *displaced_step_inferior_states;
1461
1462/* Get the displaced stepping state of process PID. */
1463
1464static struct displaced_step_inferior_state *
1465get_displaced_stepping_state (int pid)
1466{
1467 struct displaced_step_inferior_state *state;
1468
1469 for (state = displaced_step_inferior_states;
1470 state != NULL;
1471 state = state->next)
1472 if (state->pid == pid)
1473 return state;
1474
1475 return NULL;
1476}
1477
8f572e5c
PA
1478/* Return true if process PID has a thread doing a displaced step. */
1479
1480static int
1481displaced_step_in_progress (int pid)
1482{
1483 struct displaced_step_inferior_state *displaced;
1484
1485 displaced = get_displaced_stepping_state (pid);
1486 if (displaced != NULL && !ptid_equal (displaced->step_ptid, null_ptid))
1487 return 1;
1488
1489 return 0;
1490}
1491
fc1cf338
PA
1492/* Add a new displaced stepping state for process PID to the displaced
1493 stepping state list, or return a pointer to an already existing
1494 entry, if it already exists. Never returns NULL. */
1495
1496static struct displaced_step_inferior_state *
1497add_displaced_stepping_state (int pid)
1498{
1499 struct displaced_step_inferior_state *state;
1500
1501 for (state = displaced_step_inferior_states;
1502 state != NULL;
1503 state = state->next)
1504 if (state->pid == pid)
1505 return state;
237fc4c9 1506
fc1cf338
PA
1507 state = xcalloc (1, sizeof (*state));
1508 state->pid = pid;
1509 state->next = displaced_step_inferior_states;
1510 displaced_step_inferior_states = state;
237fc4c9 1511
fc1cf338
PA
1512 return state;
1513}
1514
a42244db
YQ
1515/* If inferior is in displaced stepping, and ADDR equals to starting address
1516 of copy area, return corresponding displaced_step_closure. Otherwise,
1517 return NULL. */
1518
1519struct displaced_step_closure*
1520get_displaced_step_closure_by_addr (CORE_ADDR addr)
1521{
1522 struct displaced_step_inferior_state *displaced
1523 = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
1524
1525 /* If checking the mode of displaced instruction in copy area. */
1526 if (displaced && !ptid_equal (displaced->step_ptid, null_ptid)
1527 && (displaced->step_copy == addr))
1528 return displaced->step_closure;
1529
1530 return NULL;
1531}
1532
fc1cf338 1533/* Remove the displaced stepping state of process PID. */
237fc4c9 1534
fc1cf338
PA
1535static void
1536remove_displaced_stepping_state (int pid)
1537{
1538 struct displaced_step_inferior_state *it, **prev_next_p;
237fc4c9 1539
fc1cf338
PA
1540 gdb_assert (pid != 0);
1541
1542 it = displaced_step_inferior_states;
1543 prev_next_p = &displaced_step_inferior_states;
1544 while (it)
1545 {
1546 if (it->pid == pid)
1547 {
1548 *prev_next_p = it->next;
1549 xfree (it);
1550 return;
1551 }
1552
1553 prev_next_p = &it->next;
1554 it = *prev_next_p;
1555 }
1556}
1557
1558static void
1559infrun_inferior_exit (struct inferior *inf)
1560{
1561 remove_displaced_stepping_state (inf->pid);
1562}
237fc4c9 1563
fff08868
HZ
1564/* If ON, and the architecture supports it, GDB will use displaced
1565 stepping to step over breakpoints. If OFF, or if the architecture
1566 doesn't support it, GDB will instead use the traditional
1567 hold-and-step approach. If AUTO (which is the default), GDB will
1568 decide which technique to use to step over breakpoints depending on
1569 which of all-stop or non-stop mode is active --- displaced stepping
1570 in non-stop mode; hold-and-step in all-stop mode. */
1571
72d0e2c5 1572static enum auto_boolean can_use_displaced_stepping = AUTO_BOOLEAN_AUTO;
fff08868 1573
237fc4c9
PA
1574static void
1575show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
1576 struct cmd_list_element *c,
1577 const char *value)
1578{
72d0e2c5 1579 if (can_use_displaced_stepping == AUTO_BOOLEAN_AUTO)
3e43a32a
MS
1580 fprintf_filtered (file,
1581 _("Debugger's willingness to use displaced stepping "
1582 "to step over breakpoints is %s (currently %s).\n"),
fff08868
HZ
1583 value, non_stop ? "on" : "off");
1584 else
3e43a32a
MS
1585 fprintf_filtered (file,
1586 _("Debugger's willingness to use displaced stepping "
1587 "to step over breakpoints is %s.\n"), value);
237fc4c9
PA
1588}
1589
fff08868
HZ
1590/* Return non-zero if displaced stepping can/should be used to step
1591 over breakpoints. */
1592
237fc4c9
PA
1593static int
1594use_displaced_stepping (struct gdbarch *gdbarch)
1595{
72d0e2c5
YQ
1596 return (((can_use_displaced_stepping == AUTO_BOOLEAN_AUTO && non_stop)
1597 || can_use_displaced_stepping == AUTO_BOOLEAN_TRUE)
96429cc8 1598 && gdbarch_displaced_step_copy_insn_p (gdbarch)
8213266a 1599 && find_record_target () == NULL);
237fc4c9
PA
1600}
1601
1602/* Clean out any stray displaced stepping state. */
1603static void
fc1cf338 1604displaced_step_clear (struct displaced_step_inferior_state *displaced)
237fc4c9
PA
1605{
1606 /* Indicate that there is no cleanup pending. */
fc1cf338 1607 displaced->step_ptid = null_ptid;
237fc4c9 1608
fc1cf338 1609 if (displaced->step_closure)
237fc4c9 1610 {
fc1cf338
PA
1611 gdbarch_displaced_step_free_closure (displaced->step_gdbarch,
1612 displaced->step_closure);
1613 displaced->step_closure = NULL;
237fc4c9
PA
1614 }
1615}
1616
1617static void
fc1cf338 1618displaced_step_clear_cleanup (void *arg)
237fc4c9 1619{
fc1cf338
PA
1620 struct displaced_step_inferior_state *state = arg;
1621
1622 displaced_step_clear (state);
237fc4c9
PA
1623}
1624
1625/* Dump LEN bytes at BUF in hex to FILE, followed by a newline. */
1626void
1627displaced_step_dump_bytes (struct ui_file *file,
1628 const gdb_byte *buf,
1629 size_t len)
1630{
1631 int i;
1632
1633 for (i = 0; i < len; i++)
1634 fprintf_unfiltered (file, "%02x ", buf[i]);
1635 fputs_unfiltered ("\n", file);
1636}
1637
1638/* Prepare to single-step, using displaced stepping.
1639
1640 Note that we cannot use displaced stepping when we have a signal to
1641 deliver. If we have a signal to deliver and an instruction to step
1642 over, then after the step, there will be no indication from the
1643 target whether the thread entered a signal handler or ignored the
1644 signal and stepped over the instruction successfully --- both cases
1645 result in a simple SIGTRAP. In the first case we mustn't do a
1646 fixup, and in the second case we must --- but we can't tell which.
1647 Comments in the code for 'random signals' in handle_inferior_event
1648 explain how we handle this case instead.
1649
1650 Returns 1 if preparing was successful -- this thread is going to be
1651 stepped now; or 0 if displaced stepping this thread got queued. */
1652static int
1653displaced_step_prepare (ptid_t ptid)
1654{
ad53cd71 1655 struct cleanup *old_cleanups, *ignore_cleanups;
c1e36e3e 1656 struct thread_info *tp = find_thread_ptid (ptid);
237fc4c9
PA
1657 struct regcache *regcache = get_thread_regcache (ptid);
1658 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1659 CORE_ADDR original, copy;
1660 ULONGEST len;
1661 struct displaced_step_closure *closure;
fc1cf338 1662 struct displaced_step_inferior_state *displaced;
9e529e1d 1663 int status;
237fc4c9
PA
1664
1665 /* We should never reach this function if the architecture does not
1666 support displaced stepping. */
1667 gdb_assert (gdbarch_displaced_step_copy_insn_p (gdbarch));
1668
c2829269
PA
1669 /* Nor if the thread isn't meant to step over a breakpoint. */
1670 gdb_assert (tp->control.trap_expected);
1671
c1e36e3e
PA
1672 /* Disable range stepping while executing in the scratch pad. We
1673 want a single-step even if executing the displaced instruction in
1674 the scratch buffer lands within the stepping range (e.g., a
1675 jump/branch). */
1676 tp->control.may_range_step = 0;
1677
fc1cf338
PA
1678 /* We have to displaced step one thread at a time, as we only have
1679 access to a single scratch space per inferior. */
237fc4c9 1680
fc1cf338
PA
1681 displaced = add_displaced_stepping_state (ptid_get_pid (ptid));
1682
1683 if (!ptid_equal (displaced->step_ptid, null_ptid))
237fc4c9
PA
1684 {
1685 /* Already waiting for a displaced step to finish. Defer this
1686 request and place in queue. */
237fc4c9
PA
1687
1688 if (debug_displaced)
1689 fprintf_unfiltered (gdb_stdlog,
c2829269 1690 "displaced: deferring step of %s\n",
237fc4c9
PA
1691 target_pid_to_str (ptid));
1692
c2829269 1693 thread_step_over_chain_enqueue (tp);
237fc4c9
PA
1694 return 0;
1695 }
1696 else
1697 {
1698 if (debug_displaced)
1699 fprintf_unfiltered (gdb_stdlog,
1700 "displaced: stepping %s now\n",
1701 target_pid_to_str (ptid));
1702 }
1703
fc1cf338 1704 displaced_step_clear (displaced);
237fc4c9 1705
ad53cd71
PA
1706 old_cleanups = save_inferior_ptid ();
1707 inferior_ptid = ptid;
1708
515630c5 1709 original = regcache_read_pc (regcache);
237fc4c9
PA
1710
1711 copy = gdbarch_displaced_step_location (gdbarch);
1712 len = gdbarch_max_insn_length (gdbarch);
1713
1714 /* Save the original contents of the copy area. */
fc1cf338 1715 displaced->step_saved_copy = xmalloc (len);
ad53cd71 1716 ignore_cleanups = make_cleanup (free_current_contents,
fc1cf338 1717 &displaced->step_saved_copy);
9e529e1d
JK
1718 status = target_read_memory (copy, displaced->step_saved_copy, len);
1719 if (status != 0)
1720 throw_error (MEMORY_ERROR,
1721 _("Error accessing memory address %s (%s) for "
1722 "displaced-stepping scratch space."),
1723 paddress (gdbarch, copy), safe_strerror (status));
237fc4c9
PA
1724 if (debug_displaced)
1725 {
5af949e3
UW
1726 fprintf_unfiltered (gdb_stdlog, "displaced: saved %s: ",
1727 paddress (gdbarch, copy));
fc1cf338
PA
1728 displaced_step_dump_bytes (gdb_stdlog,
1729 displaced->step_saved_copy,
1730 len);
237fc4c9
PA
1731 };
1732
1733 closure = gdbarch_displaced_step_copy_insn (gdbarch,
ad53cd71 1734 original, copy, regcache);
237fc4c9
PA
1735
1736 /* We don't support the fully-simulated case at present. */
1737 gdb_assert (closure);
1738
9f5a595d
UW
1739 /* Save the information we need to fix things up if the step
1740 succeeds. */
fc1cf338
PA
1741 displaced->step_ptid = ptid;
1742 displaced->step_gdbarch = gdbarch;
1743 displaced->step_closure = closure;
1744 displaced->step_original = original;
1745 displaced->step_copy = copy;
9f5a595d 1746
fc1cf338 1747 make_cleanup (displaced_step_clear_cleanup, displaced);
237fc4c9
PA
1748
1749 /* Resume execution at the copy. */
515630c5 1750 regcache_write_pc (regcache, copy);
237fc4c9 1751
ad53cd71
PA
1752 discard_cleanups (ignore_cleanups);
1753
1754 do_cleanups (old_cleanups);
237fc4c9
PA
1755
1756 if (debug_displaced)
5af949e3
UW
1757 fprintf_unfiltered (gdb_stdlog, "displaced: displaced pc to %s\n",
1758 paddress (gdbarch, copy));
237fc4c9 1759
237fc4c9
PA
1760 return 1;
1761}
1762
237fc4c9 1763static void
3e43a32a
MS
1764write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
1765 const gdb_byte *myaddr, int len)
237fc4c9
PA
1766{
1767 struct cleanup *ptid_cleanup = save_inferior_ptid ();
abbb1732 1768
237fc4c9
PA
1769 inferior_ptid = ptid;
1770 write_memory (memaddr, myaddr, len);
1771 do_cleanups (ptid_cleanup);
1772}
1773
e2d96639
YQ
1774/* Restore the contents of the copy area for thread PTID. */
1775
1776static void
1777displaced_step_restore (struct displaced_step_inferior_state *displaced,
1778 ptid_t ptid)
1779{
1780 ULONGEST len = gdbarch_max_insn_length (displaced->step_gdbarch);
1781
1782 write_memory_ptid (ptid, displaced->step_copy,
1783 displaced->step_saved_copy, len);
1784 if (debug_displaced)
1785 fprintf_unfiltered (gdb_stdlog, "displaced: restored %s %s\n",
1786 target_pid_to_str (ptid),
1787 paddress (displaced->step_gdbarch,
1788 displaced->step_copy));
1789}
1790
237fc4c9 1791static void
2ea28649 1792displaced_step_fixup (ptid_t event_ptid, enum gdb_signal signal)
237fc4c9
PA
1793{
1794 struct cleanup *old_cleanups;
fc1cf338
PA
1795 struct displaced_step_inferior_state *displaced
1796 = get_displaced_stepping_state (ptid_get_pid (event_ptid));
1797
1798 /* Was any thread of this process doing a displaced step? */
1799 if (displaced == NULL)
1800 return;
237fc4c9
PA
1801
1802 /* Was this event for the pid we displaced? */
fc1cf338
PA
1803 if (ptid_equal (displaced->step_ptid, null_ptid)
1804 || ! ptid_equal (displaced->step_ptid, event_ptid))
237fc4c9
PA
1805 return;
1806
fc1cf338 1807 old_cleanups = make_cleanup (displaced_step_clear_cleanup, displaced);
237fc4c9 1808
e2d96639 1809 displaced_step_restore (displaced, displaced->step_ptid);
237fc4c9 1810
cb71640d
PA
1811 /* Fixup may need to read memory/registers. Switch to the thread
1812 that we're fixing up. Also, target_stopped_by_watchpoint checks
1813 the current thread. */
1814 switch_to_thread (event_ptid);
1815
237fc4c9 1816 /* Did the instruction complete successfully? */
cb71640d
PA
1817 if (signal == GDB_SIGNAL_TRAP
1818 && !(target_stopped_by_watchpoint ()
1819 && (gdbarch_have_nonsteppable_watchpoint (displaced->step_gdbarch)
1820 || target_have_steppable_watchpoint)))
237fc4c9
PA
1821 {
1822 /* Fix up the resulting state. */
fc1cf338
PA
1823 gdbarch_displaced_step_fixup (displaced->step_gdbarch,
1824 displaced->step_closure,
1825 displaced->step_original,
1826 displaced->step_copy,
1827 get_thread_regcache (displaced->step_ptid));
237fc4c9
PA
1828 }
1829 else
1830 {
1831 /* Since the instruction didn't complete, all we can do is
1832 relocate the PC. */
515630c5
UW
1833 struct regcache *regcache = get_thread_regcache (event_ptid);
1834 CORE_ADDR pc = regcache_read_pc (regcache);
abbb1732 1835
fc1cf338 1836 pc = displaced->step_original + (pc - displaced->step_copy);
515630c5 1837 regcache_write_pc (regcache, pc);
237fc4c9
PA
1838 }
1839
1840 do_cleanups (old_cleanups);
1841
fc1cf338 1842 displaced->step_ptid = null_ptid;
c2829269 1843}
1c5cfe86 1844
c2829269
PA
1845/* Are there any pending step-over requests? If so, run all we can
1846 now. */
1847
1848static void
1849start_step_over (void)
1850{
1851 struct thread_info *tp, *next;
1852
1853 for (tp = step_over_queue_head; tp != NULL; tp = next)
237fc4c9 1854 {
237fc4c9 1855 ptid_t ptid;
c2829269 1856 struct displaced_step_inferior_state *displaced;
5af949e3 1857 struct regcache *regcache;
929dfd4f 1858 struct gdbarch *gdbarch;
1c5cfe86 1859 CORE_ADDR actual_pc;
6c95b8df 1860 struct address_space *aspace;
c2829269
PA
1861 struct inferior *inf = find_inferior_ptid (tp->ptid);
1862
1863 next = thread_step_over_chain_next (tp);
237fc4c9 1864
c2829269 1865 displaced = get_displaced_stepping_state (inf->pid);
237fc4c9 1866
c2829269
PA
1867 /* If this inferior already has a displaced step in process,
1868 don't start a new one. */
1869 if (!ptid_equal (displaced->step_ptid, null_ptid))
1870 continue;
1871
1872 thread_step_over_chain_remove (tp);
1873
1874 if (step_over_queue_head == NULL)
1875 {
1876 if (debug_infrun)
1877 fprintf_unfiltered (gdb_stdlog,
1878 "infrun: step-over queue now empty\n");
1879 }
1880
1881 ptid = tp->ptid;
ad53cd71
PA
1882 context_switch (ptid);
1883
5af949e3
UW
1884 regcache = get_thread_regcache (ptid);
1885 actual_pc = regcache_read_pc (regcache);
6c95b8df 1886 aspace = get_regcache_aspace (regcache);
8550d3b3 1887 gdbarch = get_regcache_arch (regcache);
1c5cfe86 1888
6c95b8df 1889 if (breakpoint_here_p (aspace, actual_pc))
ad53cd71 1890 {
1c5cfe86
PA
1891 if (debug_displaced)
1892 fprintf_unfiltered (gdb_stdlog,
1893 "displaced: stepping queued %s now\n",
1894 target_pid_to_str (ptid));
1895
1896 displaced_step_prepare (ptid);
1897
1898 if (debug_displaced)
1899 {
929dfd4f 1900 CORE_ADDR actual_pc = regcache_read_pc (regcache);
1c5cfe86
PA
1901 gdb_byte buf[4];
1902
5af949e3
UW
1903 fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
1904 paddress (gdbarch, actual_pc));
1c5cfe86
PA
1905 read_memory (actual_pc, buf, sizeof (buf));
1906 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
1907 }
1908
fc1cf338
PA
1909 if (gdbarch_displaced_step_hw_singlestep (gdbarch,
1910 displaced->step_closure))
a493e3e2 1911 target_resume (ptid, 1, GDB_SIGNAL_0);
99e40580 1912 else
a493e3e2 1913 target_resume (ptid, 0, GDB_SIGNAL_0);
1c5cfe86
PA
1914
1915 /* Done, we're stepping a thread. */
ad53cd71 1916 }
1c5cfe86
PA
1917 else
1918 {
1919 int step;
1920 struct thread_info *tp = inferior_thread ();
1921
1922 /* The breakpoint we were sitting under has since been
1923 removed. */
16c381f0 1924 tp->control.trap_expected = 0;
1c5cfe86
PA
1925
1926 /* Go back to what we were trying to do. */
1927 step = currently_stepping (tp);
ad53cd71 1928
8550d3b3
YQ
1929 if (step)
1930 step = maybe_software_singlestep (gdbarch, actual_pc);
1931
1c5cfe86 1932 if (debug_displaced)
3e43a32a 1933 fprintf_unfiltered (gdb_stdlog,
27d2932e 1934 "displaced: breakpoint is gone: %s, step(%d)\n",
1c5cfe86
PA
1935 target_pid_to_str (tp->ptid), step);
1936
a493e3e2
PA
1937 target_resume (ptid, step, GDB_SIGNAL_0);
1938 tp->suspend.stop_signal = GDB_SIGNAL_0;
1c5cfe86
PA
1939
1940 /* This request was discarded. See if there's any other
1941 thread waiting for its turn. */
1942 }
c2829269
PA
1943
1944 /* A new displaced stepping sequence started. Maybe we can
1945 start a displaced step on a thread of other process.
1946 Continue looking. */
237fc4c9
PA
1947 }
1948}
1949
5231c1fd
PA
1950/* Update global variables holding ptids to hold NEW_PTID if they were
1951 holding OLD_PTID. */
1952static void
1953infrun_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1954{
1955 struct displaced_step_request *it;
fc1cf338 1956 struct displaced_step_inferior_state *displaced;
5231c1fd
PA
1957
1958 if (ptid_equal (inferior_ptid, old_ptid))
1959 inferior_ptid = new_ptid;
1960
fc1cf338
PA
1961 for (displaced = displaced_step_inferior_states;
1962 displaced;
1963 displaced = displaced->next)
1964 {
1965 if (ptid_equal (displaced->step_ptid, old_ptid))
1966 displaced->step_ptid = new_ptid;
fc1cf338 1967 }
5231c1fd
PA
1968}
1969
237fc4c9
PA
1970\f
1971/* Resuming. */
c906108c
SS
1972
1973/* Things to clean up if we QUIT out of resume (). */
c906108c 1974static void
74b7792f 1975resume_cleanups (void *ignore)
c906108c 1976{
34b7e8a6
PA
1977 if (!ptid_equal (inferior_ptid, null_ptid))
1978 delete_single_step_breakpoints (inferior_thread ());
7c16b83e 1979
c906108c
SS
1980 normal_stop ();
1981}
1982
53904c9e
AC
1983static const char schedlock_off[] = "off";
1984static const char schedlock_on[] = "on";
1985static const char schedlock_step[] = "step";
40478521 1986static const char *const scheduler_enums[] = {
ef346e04
AC
1987 schedlock_off,
1988 schedlock_on,
1989 schedlock_step,
1990 NULL
1991};
920d2a44
AC
1992static const char *scheduler_mode = schedlock_off;
1993static void
1994show_scheduler_mode (struct ui_file *file, int from_tty,
1995 struct cmd_list_element *c, const char *value)
1996{
3e43a32a
MS
1997 fprintf_filtered (file,
1998 _("Mode for locking scheduler "
1999 "during execution is \"%s\".\n"),
920d2a44
AC
2000 value);
2001}
c906108c
SS
2002
2003static void
96baa820 2004set_schedlock_func (char *args, int from_tty, struct cmd_list_element *c)
c906108c 2005{
eefe576e
AC
2006 if (!target_can_lock_scheduler)
2007 {
2008 scheduler_mode = schedlock_off;
2009 error (_("Target '%s' cannot support this command."), target_shortname);
2010 }
c906108c
SS
2011}
2012
d4db2f36
PA
2013/* True if execution commands resume all threads of all processes by
2014 default; otherwise, resume only threads of the current inferior
2015 process. */
2016int sched_multi = 0;
2017
2facfe5c
DD
2018/* Try to setup for software single stepping over the specified location.
2019 Return 1 if target_resume() should use hardware single step.
2020
2021 GDBARCH the current gdbarch.
2022 PC the location to step over. */
2023
2024static int
2025maybe_software_singlestep (struct gdbarch *gdbarch, CORE_ADDR pc)
2026{
2027 int hw_step = 1;
2028
f02253f1
HZ
2029 if (execution_direction == EXEC_FORWARD
2030 && gdbarch_software_single_step_p (gdbarch)
99e40580 2031 && gdbarch_software_single_step (gdbarch, get_current_frame ()))
2facfe5c 2032 {
99e40580 2033 hw_step = 0;
2facfe5c
DD
2034 }
2035 return hw_step;
2036}
c906108c 2037
f3263aa4
PA
2038/* See infrun.h. */
2039
09cee04b
PA
2040ptid_t
2041user_visible_resume_ptid (int step)
2042{
f3263aa4 2043 ptid_t resume_ptid;
09cee04b 2044
09cee04b
PA
2045 if (non_stop)
2046 {
2047 /* With non-stop mode on, threads are always handled
2048 individually. */
2049 resume_ptid = inferior_ptid;
2050 }
2051 else if ((scheduler_mode == schedlock_on)
03d46957 2052 || (scheduler_mode == schedlock_step && step))
09cee04b 2053 {
f3263aa4
PA
2054 /* User-settable 'scheduler' mode requires solo thread
2055 resume. */
09cee04b
PA
2056 resume_ptid = inferior_ptid;
2057 }
f3263aa4
PA
2058 else if (!sched_multi && target_supports_multi_process ())
2059 {
2060 /* Resume all threads of the current process (and none of other
2061 processes). */
2062 resume_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
2063 }
2064 else
2065 {
2066 /* Resume all threads of all processes. */
2067 resume_ptid = RESUME_ALL;
2068 }
09cee04b
PA
2069
2070 return resume_ptid;
2071}
2072
64ce06e4
PA
2073/* Wrapper for target_resume, that handles infrun-specific
2074 bookkeeping. */
2075
2076static void
2077do_target_resume (ptid_t resume_ptid, int step, enum gdb_signal sig)
2078{
2079 struct thread_info *tp = inferior_thread ();
2080
2081 /* Install inferior's terminal modes. */
2082 target_terminal_inferior ();
2083
2084 /* Avoid confusing the next resume, if the next stop/resume
2085 happens to apply to another thread. */
2086 tp->suspend.stop_signal = GDB_SIGNAL_0;
2087
8f572e5c
PA
2088 /* Advise target which signals may be handled silently.
2089
2090 If we have removed breakpoints because we are stepping over one
2091 in-line (in any thread), we need to receive all signals to avoid
2092 accidentally skipping a breakpoint during execution of a signal
2093 handler.
2094
2095 Likewise if we're displaced stepping, otherwise a trap for a
2096 breakpoint in a signal handler might be confused with the
2097 displaced step finishing. We don't make the displaced_step_fixup
2098 step distinguish the cases instead, because:
2099
2100 - a backtrace while stopped in the signal handler would show the
2101 scratch pad as frame older than the signal handler, instead of
2102 the real mainline code.
2103
2104 - when the thread is later resumed, the signal handler would
2105 return to the scratch pad area, which would no longer be
2106 valid. */
2107 if (step_over_info_valid_p ()
2108 || displaced_step_in_progress (ptid_get_pid (tp->ptid)))
64ce06e4
PA
2109 target_pass_signals (0, NULL);
2110 else
2111 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
2112
2113 target_resume (resume_ptid, step, sig);
2114}
2115
c906108c
SS
2116/* Resume the inferior, but allow a QUIT. This is useful if the user
2117 wants to interrupt some lengthy single-stepping operation
2118 (for child processes, the SIGINT goes to the inferior, and so
2119 we get a SIGINT random_signal, but for remote debugging and perhaps
2120 other targets, that's not true).
2121
c906108c
SS
2122 SIG is the signal to give the inferior (zero for none). */
2123void
64ce06e4 2124resume (enum gdb_signal sig)
c906108c 2125{
74b7792f 2126 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
515630c5
UW
2127 struct regcache *regcache = get_current_regcache ();
2128 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4e1c45ea 2129 struct thread_info *tp = inferior_thread ();
515630c5 2130 CORE_ADDR pc = regcache_read_pc (regcache);
6c95b8df 2131 struct address_space *aspace = get_regcache_aspace (regcache);
b0f16a3e 2132 ptid_t resume_ptid;
856e7dd6
PA
2133 /* This represents the user's step vs continue request. When
2134 deciding whether "set scheduler-locking step" applies, it's the
2135 user's intention that counts. */
2136 const int user_step = tp->control.stepping_command;
64ce06e4
PA
2137 /* This represents what we'll actually request the target to do.
2138 This can decay from a step to a continue, if e.g., we need to
2139 implement single-stepping with breakpoints (software
2140 single-step). */
6b403daa 2141 int step;
c7e8a53c 2142
af48d08f
PA
2143 tp->stepped_breakpoint = 0;
2144
c2829269
PA
2145 gdb_assert (!thread_is_in_step_over_chain (tp));
2146
c906108c
SS
2147 QUIT;
2148
6b403daa
PA
2149 /* Depends on stepped_breakpoint. */
2150 step = currently_stepping (tp);
2151
74609e71
YQ
2152 if (current_inferior ()->waiting_for_vfork_done)
2153 {
48f9886d
PA
2154 /* Don't try to single-step a vfork parent that is waiting for
2155 the child to get out of the shared memory region (by exec'ing
2156 or exiting). This is particularly important on software
2157 single-step archs, as the child process would trip on the
2158 software single step breakpoint inserted for the parent
2159 process. Since the parent will not actually execute any
2160 instruction until the child is out of the shared region (such
2161 are vfork's semantics), it is safe to simply continue it.
2162 Eventually, we'll see a TARGET_WAITKIND_VFORK_DONE event for
2163 the parent, and tell it to `keep_going', which automatically
2164 re-sets it stepping. */
74609e71
YQ
2165 if (debug_infrun)
2166 fprintf_unfiltered (gdb_stdlog,
2167 "infrun: resume : clear step\n");
a09dd441 2168 step = 0;
74609e71
YQ
2169 }
2170
527159b7 2171 if (debug_infrun)
237fc4c9 2172 fprintf_unfiltered (gdb_stdlog,
c9737c08 2173 "infrun: resume (step=%d, signal=%s), "
0d9a9a5f 2174 "trap_expected=%d, current thread [%s] at %s\n",
c9737c08
PA
2175 step, gdb_signal_to_symbol_string (sig),
2176 tp->control.trap_expected,
0d9a9a5f
PA
2177 target_pid_to_str (inferior_ptid),
2178 paddress (gdbarch, pc));
c906108c 2179
c2c6d25f
JM
2180 /* Normally, by the time we reach `resume', the breakpoints are either
2181 removed or inserted, as appropriate. The exception is if we're sitting
2182 at a permanent breakpoint; we need to step over it, but permanent
2183 breakpoints can't be removed. So we have to test for it here. */
6c95b8df 2184 if (breakpoint_here_p (aspace, pc) == permanent_breakpoint_here)
6d350bb5 2185 {
af48d08f
PA
2186 if (sig != GDB_SIGNAL_0)
2187 {
2188 /* We have a signal to pass to the inferior. The resume
2189 may, or may not take us to the signal handler. If this
2190 is a step, we'll need to stop in the signal handler, if
2191 there's one, (if the target supports stepping into
2192 handlers), or in the next mainline instruction, if
2193 there's no handler. If this is a continue, we need to be
2194 sure to run the handler with all breakpoints inserted.
2195 In all cases, set a breakpoint at the current address
2196 (where the handler returns to), and once that breakpoint
2197 is hit, resume skipping the permanent breakpoint. If
2198 that breakpoint isn't hit, then we've stepped into the
2199 signal handler (or hit some other event). We'll delete
2200 the step-resume breakpoint then. */
2201
2202 if (debug_infrun)
2203 fprintf_unfiltered (gdb_stdlog,
2204 "infrun: resume: skipping permanent breakpoint, "
2205 "deliver signal first\n");
2206
2207 clear_step_over_info ();
2208 tp->control.trap_expected = 0;
2209
2210 if (tp->control.step_resume_breakpoint == NULL)
2211 {
2212 /* Set a "high-priority" step-resume, as we don't want
2213 user breakpoints at PC to trigger (again) when this
2214 hits. */
2215 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2216 gdb_assert (tp->control.step_resume_breakpoint->loc->permanent);
2217
2218 tp->step_after_step_resume_breakpoint = step;
2219 }
2220
2221 insert_breakpoints ();
2222 }
2223 else
2224 {
2225 /* There's no signal to pass, we can go ahead and skip the
2226 permanent breakpoint manually. */
2227 if (debug_infrun)
2228 fprintf_unfiltered (gdb_stdlog,
2229 "infrun: resume: skipping permanent breakpoint\n");
2230 gdbarch_skip_permanent_breakpoint (gdbarch, regcache);
2231 /* Update pc to reflect the new address from which we will
2232 execute instructions. */
2233 pc = regcache_read_pc (regcache);
2234
2235 if (step)
2236 {
2237 /* We've already advanced the PC, so the stepping part
2238 is done. Now we need to arrange for a trap to be
2239 reported to handle_inferior_event. Set a breakpoint
2240 at the current PC, and run to it. Don't update
2241 prev_pc, because if we end in
44a1ee51
PA
2242 switch_back_to_stepped_thread, we want the "expected
2243 thread advanced also" branch to be taken. IOW, we
2244 don't want this thread to step further from PC
af48d08f 2245 (overstep). */
1ac806b8 2246 gdb_assert (!step_over_info_valid_p ());
af48d08f
PA
2247 insert_single_step_breakpoint (gdbarch, aspace, pc);
2248 insert_breakpoints ();
2249
856e7dd6 2250 resume_ptid = user_visible_resume_ptid (user_step);
1ac806b8 2251 do_target_resume (resume_ptid, 0, GDB_SIGNAL_0);
af48d08f
PA
2252 discard_cleanups (old_cleanups);
2253 return;
2254 }
2255 }
6d350bb5 2256 }
c2c6d25f 2257
c1e36e3e
PA
2258 /* If we have a breakpoint to step over, make sure to do a single
2259 step only. Same if we have software watchpoints. */
2260 if (tp->control.trap_expected || bpstat_should_step ())
2261 tp->control.may_range_step = 0;
2262
237fc4c9
PA
2263 /* If enabled, step over breakpoints by executing a copy of the
2264 instruction at a different address.
2265
2266 We can't use displaced stepping when we have a signal to deliver;
2267 the comments for displaced_step_prepare explain why. The
2268 comments in the handle_inferior event for dealing with 'random
74609e71
YQ
2269 signals' explain what we do instead.
2270
2271 We can't use displaced stepping when we are waiting for vfork_done
2272 event, displaced stepping breaks the vfork child similarly as single
2273 step software breakpoint. */
515630c5 2274 if (use_displaced_stepping (gdbarch)
36728e82 2275 && tp->control.trap_expected
cb71640d 2276 && !step_over_info_valid_p ()
a493e3e2 2277 && sig == GDB_SIGNAL_0
74609e71 2278 && !current_inferior ()->waiting_for_vfork_done)
237fc4c9 2279 {
fc1cf338
PA
2280 struct displaced_step_inferior_state *displaced;
2281
237fc4c9 2282 if (!displaced_step_prepare (inferior_ptid))
d56b7306
VP
2283 {
2284 /* Got placed in displaced stepping queue. Will be resumed
2285 later when all the currently queued displaced stepping
251bde03
PA
2286 requests finish. The thread is not executing at this
2287 point, and the call to set_executing will be made later.
2288 But we need to call set_running here, since from the
28bf096c
PA
2289 user/frontend's point of view, threads were set running. */
2290 set_running (user_visible_resume_ptid (user_step), 1);
d56b7306
VP
2291 discard_cleanups (old_cleanups);
2292 return;
2293 }
99e40580 2294
ca7781d2
LM
2295 /* Update pc to reflect the new address from which we will execute
2296 instructions due to displaced stepping. */
2297 pc = regcache_read_pc (get_thread_regcache (inferior_ptid));
2298
fc1cf338 2299 displaced = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
a09dd441
PA
2300 step = gdbarch_displaced_step_hw_singlestep (gdbarch,
2301 displaced->step_closure);
237fc4c9
PA
2302 }
2303
2facfe5c 2304 /* Do we need to do it the hard way, w/temp breakpoints? */
99e40580 2305 else if (step)
2facfe5c 2306 step = maybe_software_singlestep (gdbarch, pc);
c906108c 2307
30852783
UW
2308 /* Currently, our software single-step implementation leads to different
2309 results than hardware single-stepping in one situation: when stepping
2310 into delivering a signal which has an associated signal handler,
2311 hardware single-step will stop at the first instruction of the handler,
2312 while software single-step will simply skip execution of the handler.
2313
2314 For now, this difference in behavior is accepted since there is no
2315 easy way to actually implement single-stepping into a signal handler
2316 without kernel support.
2317
2318 However, there is one scenario where this difference leads to follow-on
2319 problems: if we're stepping off a breakpoint by removing all breakpoints
2320 and then single-stepping. In this case, the software single-step
2321 behavior means that even if there is a *breakpoint* in the signal
2322 handler, GDB still would not stop.
2323
2324 Fortunately, we can at least fix this particular issue. We detect
2325 here the case where we are about to deliver a signal while software
2326 single-stepping with breakpoints removed. In this situation, we
2327 revert the decisions to remove all breakpoints and insert single-
2328 step breakpoints, and instead we install a step-resume breakpoint
2329 at the current address, deliver the signal without stepping, and
2330 once we arrive back at the step-resume breakpoint, actually step
2331 over the breakpoint we originally wanted to step over. */
34b7e8a6 2332 if (thread_has_single_step_breakpoints_set (tp)
6cc83d2a
PA
2333 && sig != GDB_SIGNAL_0
2334 && step_over_info_valid_p ())
30852783
UW
2335 {
2336 /* If we have nested signals or a pending signal is delivered
2337 immediately after a handler returns, might might already have
2338 a step-resume breakpoint set on the earlier handler. We cannot
2339 set another step-resume breakpoint; just continue on until the
2340 original breakpoint is hit. */
2341 if (tp->control.step_resume_breakpoint == NULL)
2342 {
2c03e5be 2343 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
30852783
UW
2344 tp->step_after_step_resume_breakpoint = 1;
2345 }
2346
34b7e8a6 2347 delete_single_step_breakpoints (tp);
30852783 2348
31e77af2 2349 clear_step_over_info ();
30852783 2350 tp->control.trap_expected = 0;
31e77af2
PA
2351
2352 insert_breakpoints ();
30852783
UW
2353 }
2354
b0f16a3e
SM
2355 /* If STEP is set, it's a request to use hardware stepping
2356 facilities. But in that case, we should never
2357 use singlestep breakpoint. */
34b7e8a6 2358 gdb_assert (!(thread_has_single_step_breakpoints_set (tp) && step));
dfcd3bfb 2359
b0f16a3e
SM
2360 /* Decide the set of threads to ask the target to resume. Start
2361 by assuming everything will be resumed, than narrow the set
2362 by applying increasingly restricting conditions. */
856e7dd6 2363 resume_ptid = user_visible_resume_ptid (user_step);
cd76b0b7 2364
251bde03
PA
2365 /* Even if RESUME_PTID is a wildcard, and we end up resuming less
2366 (e.g., we might need to step over a breakpoint), from the
2367 user/frontend's point of view, all threads in RESUME_PTID are now
28bf096c
PA
2368 running. */
2369 set_running (resume_ptid, 1);
251bde03 2370
b0f16a3e 2371 /* Maybe resume a single thread after all. */
34b7e8a6 2372 if ((step || thread_has_single_step_breakpoints_set (tp))
b0f16a3e
SM
2373 && tp->control.trap_expected)
2374 {
2375 /* We're allowing a thread to run past a breakpoint it has
2376 hit, by single-stepping the thread with the breakpoint
2377 removed. In which case, we need to single-step only this
2378 thread, and keep others stopped, as they can miss this
2379 breakpoint if allowed to run. */
2380 resume_ptid = inferior_ptid;
2381 }
d4db2f36 2382
7f5ef605
PA
2383 if (execution_direction != EXEC_REVERSE
2384 && step && breakpoint_inserted_here_p (aspace, pc))
b0f16a3e 2385 {
7f5ef605
PA
2386 /* The only case we currently need to step a breakpoint
2387 instruction is when we have a signal to deliver. See
2388 handle_signal_stop where we handle random signals that could
2389 take out us out of the stepping range. Normally, in that
2390 case we end up continuing (instead of stepping) over the
2391 signal handler with a breakpoint at PC, but there are cases
2392 where we should _always_ single-step, even if we have a
2393 step-resume breakpoint, like when a software watchpoint is
2394 set. Assuming single-stepping and delivering a signal at the
2395 same time would takes us to the signal handler, then we could
2396 have removed the breakpoint at PC to step over it. However,
2397 some hardware step targets (like e.g., Mac OS) can't step
2398 into signal handlers, and for those, we need to leave the
2399 breakpoint at PC inserted, as otherwise if the handler
2400 recurses and executes PC again, it'll miss the breakpoint.
2401 So we leave the breakpoint inserted anyway, but we need to
2402 record that we tried to step a breakpoint instruction, so
2403 that adjust_pc_after_break doesn't end up confused. */
2404 gdb_assert (sig != GDB_SIGNAL_0);
2405
2406 tp->stepped_breakpoint = 1;
2407
b0f16a3e
SM
2408 /* Most targets can step a breakpoint instruction, thus
2409 executing it normally. But if this one cannot, just
2410 continue and we will hit it anyway. */
7f5ef605 2411 if (gdbarch_cannot_step_breakpoint (gdbarch))
b0f16a3e
SM
2412 step = 0;
2413 }
ef5cf84e 2414
b0f16a3e
SM
2415 if (debug_displaced
2416 && use_displaced_stepping (gdbarch)
cb71640d
PA
2417 && tp->control.trap_expected
2418 && !step_over_info_valid_p ())
b0f16a3e 2419 {
d9b67d9f 2420 struct regcache *resume_regcache = get_thread_regcache (tp->ptid);
b0f16a3e
SM
2421 struct gdbarch *resume_gdbarch = get_regcache_arch (resume_regcache);
2422 CORE_ADDR actual_pc = regcache_read_pc (resume_regcache);
2423 gdb_byte buf[4];
2424
2425 fprintf_unfiltered (gdb_stdlog, "displaced: run %s: ",
2426 paddress (resume_gdbarch, actual_pc));
2427 read_memory (actual_pc, buf, sizeof (buf));
2428 displaced_step_dump_bytes (gdb_stdlog, buf, sizeof (buf));
2429 }
237fc4c9 2430
b0f16a3e
SM
2431 if (tp->control.may_range_step)
2432 {
2433 /* If we're resuming a thread with the PC out of the step
2434 range, then we're doing some nested/finer run control
2435 operation, like stepping the thread out of the dynamic
2436 linker or the displaced stepping scratch pad. We
2437 shouldn't have allowed a range step then. */
2438 gdb_assert (pc_in_thread_step_range (pc, tp));
2439 }
c1e36e3e 2440
64ce06e4 2441 do_target_resume (resume_ptid, step, sig);
c906108c
SS
2442 discard_cleanups (old_cleanups);
2443}
2444\f
237fc4c9 2445/* Proceeding. */
c906108c
SS
2446
2447/* Clear out all variables saying what to do when inferior is continued.
2448 First do this, then set the ones you want, then call `proceed'. */
2449
a7212384
UW
2450static void
2451clear_proceed_status_thread (struct thread_info *tp)
c906108c 2452{
a7212384
UW
2453 if (debug_infrun)
2454 fprintf_unfiltered (gdb_stdlog,
2455 "infrun: clear_proceed_status_thread (%s)\n",
2456 target_pid_to_str (tp->ptid));
d6b48e9c 2457
70509625
PA
2458 /* If this signal should not be seen by program, give it zero.
2459 Used for debugging signals. */
2460 if (!signal_pass_state (tp->suspend.stop_signal))
2461 tp->suspend.stop_signal = GDB_SIGNAL_0;
2462
16c381f0
JK
2463 tp->control.trap_expected = 0;
2464 tp->control.step_range_start = 0;
2465 tp->control.step_range_end = 0;
c1e36e3e 2466 tp->control.may_range_step = 0;
16c381f0
JK
2467 tp->control.step_frame_id = null_frame_id;
2468 tp->control.step_stack_frame_id = null_frame_id;
2469 tp->control.step_over_calls = STEP_OVER_UNDEBUGGABLE;
885eeb5b 2470 tp->control.step_start_function = NULL;
a7212384 2471 tp->stop_requested = 0;
4e1c45ea 2472
16c381f0 2473 tp->control.stop_step = 0;
32400beb 2474
16c381f0 2475 tp->control.proceed_to_finish = 0;
414c69f7 2476
17b2616c 2477 tp->control.command_interp = NULL;
856e7dd6 2478 tp->control.stepping_command = 0;
17b2616c 2479
a7212384 2480 /* Discard any remaining commands or status from previous stop. */
16c381f0 2481 bpstat_clear (&tp->control.stop_bpstat);
a7212384 2482}
32400beb 2483
a7212384 2484void
70509625 2485clear_proceed_status (int step)
a7212384 2486{
6c95b8df
PA
2487 if (!non_stop)
2488 {
70509625
PA
2489 struct thread_info *tp;
2490 ptid_t resume_ptid;
2491
2492 resume_ptid = user_visible_resume_ptid (step);
2493
2494 /* In all-stop mode, delete the per-thread status of all threads
2495 we're about to resume, implicitly and explicitly. */
2496 ALL_NON_EXITED_THREADS (tp)
2497 {
2498 if (!ptid_match (tp->ptid, resume_ptid))
2499 continue;
2500 clear_proceed_status_thread (tp);
2501 }
6c95b8df
PA
2502 }
2503
a7212384
UW
2504 if (!ptid_equal (inferior_ptid, null_ptid))
2505 {
2506 struct inferior *inferior;
2507
2508 if (non_stop)
2509 {
6c95b8df
PA
2510 /* If in non-stop mode, only delete the per-thread status of
2511 the current thread. */
a7212384
UW
2512 clear_proceed_status_thread (inferior_thread ());
2513 }
6c95b8df 2514
d6b48e9c 2515 inferior = current_inferior ();
16c381f0 2516 inferior->control.stop_soon = NO_STOP_QUIETLY;
4e1c45ea
PA
2517 }
2518
c906108c 2519 stop_after_trap = 0;
f3b1572e 2520
31e77af2
PA
2521 clear_step_over_info ();
2522
f3b1572e 2523 observer_notify_about_to_proceed ();
c906108c
SS
2524}
2525
99619bea
PA
2526/* Returns true if TP is still stopped at a breakpoint that needs
2527 stepping-over in order to make progress. If the breakpoint is gone
2528 meanwhile, we can skip the whole step-over dance. */
ea67f13b
DJ
2529
2530static int
6c4cfb24 2531thread_still_needs_step_over_bp (struct thread_info *tp)
99619bea
PA
2532{
2533 if (tp->stepping_over_breakpoint)
2534 {
2535 struct regcache *regcache = get_thread_regcache (tp->ptid);
2536
2537 if (breakpoint_here_p (get_regcache_aspace (regcache),
af48d08f
PA
2538 regcache_read_pc (regcache))
2539 == ordinary_breakpoint_here)
99619bea
PA
2540 return 1;
2541
2542 tp->stepping_over_breakpoint = 0;
2543 }
2544
2545 return 0;
2546}
2547
6c4cfb24
PA
2548/* Check whether thread TP still needs to start a step-over in order
2549 to make progress when resumed. Returns an bitwise or of enum
2550 step_over_what bits, indicating what needs to be stepped over. */
2551
2552static int
2553thread_still_needs_step_over (struct thread_info *tp)
2554{
2555 struct inferior *inf = find_inferior_ptid (tp->ptid);
2556 int what = 0;
2557
2558 if (thread_still_needs_step_over_bp (tp))
2559 what |= STEP_OVER_BREAKPOINT;
2560
2561 if (tp->stepping_over_watchpoint
2562 && !target_have_steppable_watchpoint)
2563 what |= STEP_OVER_WATCHPOINT;
2564
2565 return what;
2566}
2567
483805cf
PA
2568/* Returns true if scheduler locking applies. STEP indicates whether
2569 we're about to do a step/next-like command to a thread. */
2570
2571static int
856e7dd6 2572schedlock_applies (struct thread_info *tp)
483805cf
PA
2573{
2574 return (scheduler_mode == schedlock_on
2575 || (scheduler_mode == schedlock_step
856e7dd6 2576 && tp->control.stepping_command));
483805cf
PA
2577}
2578
99619bea
PA
2579/* Look a thread other than EXCEPT that has previously reported a
2580 breakpoint event, and thus needs a step-over in order to make
856e7dd6 2581 progress. Returns NULL is none is found. */
99619bea
PA
2582
2583static struct thread_info *
856e7dd6 2584find_thread_needs_step_over (struct thread_info *except)
ea67f13b 2585{
99619bea 2586 struct thread_info *tp, *current;
5a437975
DE
2587
2588 /* With non-stop mode on, threads are always handled individually. */
2589 gdb_assert (! non_stop);
ea67f13b 2590
99619bea 2591 current = inferior_thread ();
d4db2f36 2592
99619bea
PA
2593 /* If scheduler locking applies, we can avoid iterating over all
2594 threads. */
856e7dd6 2595 if (schedlock_applies (except))
ea67f13b 2596 {
99619bea
PA
2597 if (except != current
2598 && thread_still_needs_step_over (current))
2599 return current;
515630c5 2600
99619bea
PA
2601 return NULL;
2602 }
0d9a9a5f 2603
034f788c 2604 ALL_NON_EXITED_THREADS (tp)
99619bea
PA
2605 {
2606 /* Ignore the EXCEPT thread. */
2607 if (tp == except)
2608 continue;
2609 /* Ignore threads of processes we're not resuming. */
2610 if (!sched_multi
2611 && ptid_get_pid (tp->ptid) != ptid_get_pid (inferior_ptid))
2612 continue;
2613
2614 if (thread_still_needs_step_over (tp))
2615 return tp;
ea67f13b
DJ
2616 }
2617
99619bea 2618 return NULL;
ea67f13b 2619}
e4846b08 2620
c906108c
SS
2621/* Basic routine for continuing the program in various fashions.
2622
2623 ADDR is the address to resume at, or -1 for resume where stopped.
2624 SIGGNAL is the signal to give it, or 0 for none,
c5aa993b 2625 or -1 for act according to how it stopped.
c906108c 2626 STEP is nonzero if should trap after one instruction.
c5aa993b
JM
2627 -1 means return after that and print nothing.
2628 You should probably set various step_... variables
2629 before calling here, if you are stepping.
c906108c
SS
2630
2631 You should call clear_proceed_status before calling proceed. */
2632
2633void
64ce06e4 2634proceed (CORE_ADDR addr, enum gdb_signal siggnal)
c906108c 2635{
e58b0e63
PA
2636 struct regcache *regcache;
2637 struct gdbarch *gdbarch;
4e1c45ea 2638 struct thread_info *tp;
e58b0e63 2639 CORE_ADDR pc;
6c95b8df 2640 struct address_space *aspace;
c906108c 2641
e58b0e63
PA
2642 /* If we're stopped at a fork/vfork, follow the branch set by the
2643 "set follow-fork-mode" command; otherwise, we'll just proceed
2644 resuming the current thread. */
2645 if (!follow_fork ())
2646 {
2647 /* The target for some reason decided not to resume. */
2648 normal_stop ();
f148b27e
PA
2649 if (target_can_async_p ())
2650 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
e58b0e63
PA
2651 return;
2652 }
2653
842951eb
PA
2654 /* We'll update this if & when we switch to a new thread. */
2655 previous_inferior_ptid = inferior_ptid;
2656
e58b0e63
PA
2657 regcache = get_current_regcache ();
2658 gdbarch = get_regcache_arch (regcache);
6c95b8df 2659 aspace = get_regcache_aspace (regcache);
e58b0e63 2660 pc = regcache_read_pc (regcache);
2adfaa28 2661 tp = inferior_thread ();
e58b0e63 2662
99619bea
PA
2663 /* Fill in with reasonable starting values. */
2664 init_thread_stepping_state (tp);
2665
c2829269
PA
2666 gdb_assert (!thread_is_in_step_over_chain (tp));
2667
2acceee2 2668 if (addr == (CORE_ADDR) -1)
c906108c 2669 {
af48d08f
PA
2670 if (pc == stop_pc
2671 && breakpoint_here_p (aspace, pc) == ordinary_breakpoint_here
b2175913 2672 && execution_direction != EXEC_REVERSE)
3352ef37
AC
2673 /* There is a breakpoint at the address we will resume at,
2674 step one instruction before inserting breakpoints so that
2675 we do not stop right away (and report a second hit at this
b2175913
MS
2676 breakpoint).
2677
2678 Note, we don't do this in reverse, because we won't
2679 actually be executing the breakpoint insn anyway.
2680 We'll be (un-)executing the previous instruction. */
99619bea 2681 tp->stepping_over_breakpoint = 1;
515630c5
UW
2682 else if (gdbarch_single_step_through_delay_p (gdbarch)
2683 && gdbarch_single_step_through_delay (gdbarch,
2684 get_current_frame ()))
3352ef37
AC
2685 /* We stepped onto an instruction that needs to be stepped
2686 again before re-inserting the breakpoint, do so. */
99619bea 2687 tp->stepping_over_breakpoint = 1;
c906108c
SS
2688 }
2689 else
2690 {
515630c5 2691 regcache_write_pc (regcache, addr);
c906108c
SS
2692 }
2693
70509625
PA
2694 if (siggnal != GDB_SIGNAL_DEFAULT)
2695 tp->suspend.stop_signal = siggnal;
2696
17b2616c
PA
2697 /* Record the interpreter that issued the execution command that
2698 caused this thread to resume. If the top level interpreter is
2699 MI/async, and the execution command was a CLI command
2700 (next/step/etc.), we'll want to print stop event output to the MI
2701 console channel (the stepped-to line, etc.), as if the user
2702 entered the execution command on a real GDB console. */
2703 inferior_thread ()->control.command_interp = command_interp ();
2704
527159b7 2705 if (debug_infrun)
8a9de0e4 2706 fprintf_unfiltered (gdb_stdlog,
64ce06e4 2707 "infrun: proceed (addr=%s, signal=%s)\n",
c9737c08 2708 paddress (gdbarch, addr),
64ce06e4 2709 gdb_signal_to_symbol_string (siggnal));
527159b7 2710
94cc34af
PA
2711 if (non_stop)
2712 /* In non-stop, each thread is handled individually. The context
2713 must already be set to the right thread here. */
2714 ;
2715 else
2716 {
99619bea
PA
2717 struct thread_info *step_over;
2718
94cc34af
PA
2719 /* In a multi-threaded task we may select another thread and
2720 then continue or step.
c906108c 2721
94cc34af
PA
2722 But if the old thread was stopped at a breakpoint, it will
2723 immediately cause another breakpoint stop without any
2724 execution (i.e. it will report a breakpoint hit incorrectly).
2725 So we must step over it first.
c906108c 2726
99619bea
PA
2727 Look for a thread other than the current (TP) that reported a
2728 breakpoint hit and hasn't been resumed yet since. */
856e7dd6 2729 step_over = find_thread_needs_step_over (tp);
99619bea 2730 if (step_over != NULL)
2adfaa28 2731 {
99619bea
PA
2732 if (debug_infrun)
2733 fprintf_unfiltered (gdb_stdlog,
2734 "infrun: need to step-over [%s] first\n",
2735 target_pid_to_str (step_over->ptid));
2736
2737 /* Store the prev_pc for the stepping thread too, needed by
44a1ee51 2738 switch_back_to_stepped_thread. */
99619bea
PA
2739 tp->prev_pc = regcache_read_pc (get_current_regcache ());
2740 switch_to_thread (step_over->ptid);
2741 tp = step_over;
2adfaa28 2742 }
94cc34af 2743 }
c906108c 2744
31e77af2
PA
2745 /* If we need to step over a breakpoint, and we're not using
2746 displaced stepping to do so, insert all breakpoints (watchpoints,
2747 etc.) but the one we're stepping over, step one instruction, and
2748 then re-insert the breakpoint when that step is finished. */
99619bea 2749 if (tp->stepping_over_breakpoint && !use_displaced_stepping (gdbarch))
30852783 2750 {
31e77af2
PA
2751 struct regcache *regcache = get_current_regcache ();
2752
2753 set_step_over_info (get_regcache_aspace (regcache),
963f9c80 2754 regcache_read_pc (regcache), 0);
30852783 2755 }
31e77af2
PA
2756 else
2757 clear_step_over_info ();
30852783 2758
31e77af2 2759 insert_breakpoints ();
30852783 2760
99619bea
PA
2761 tp->control.trap_expected = tp->stepping_over_breakpoint;
2762
c906108c
SS
2763 annotate_starting ();
2764
2765 /* Make sure that output from GDB appears before output from the
2766 inferior. */
2767 gdb_flush (gdb_stdout);
2768
e4846b08 2769 /* Refresh prev_pc value just prior to resuming. This used to be
22bcd14b 2770 done in stop_waiting, however, setting prev_pc there did not handle
e4846b08
JJ
2771 scenarios such as inferior function calls or returning from
2772 a function via the return command. In those cases, the prev_pc
2773 value was not set properly for subsequent commands. The prev_pc value
2774 is used to initialize the starting line number in the ecs. With an
2775 invalid value, the gdb next command ends up stopping at the position
2776 represented by the next line table entry past our start position.
2777 On platforms that generate one line table entry per line, this
2778 is not a problem. However, on the ia64, the compiler generates
2779 extraneous line table entries that do not increase the line number.
2780 When we issue the gdb next command on the ia64 after an inferior call
2781 or a return command, we often end up a few instructions forward, still
2782 within the original line we started.
2783
d5cd6034
JB
2784 An attempt was made to refresh the prev_pc at the same time the
2785 execution_control_state is initialized (for instance, just before
2786 waiting for an inferior event). But this approach did not work
2787 because of platforms that use ptrace, where the pc register cannot
2788 be read unless the inferior is stopped. At that point, we are not
2789 guaranteed the inferior is stopped and so the regcache_read_pc() call
2790 can fail. Setting the prev_pc value here ensures the value is updated
2791 correctly when the inferior is stopped. */
4e1c45ea 2792 tp->prev_pc = regcache_read_pc (get_current_regcache ());
e4846b08 2793
c906108c 2794 /* Resume inferior. */
64ce06e4 2795 resume (tp->suspend.stop_signal);
c906108c
SS
2796
2797 /* Wait for it to stop (if not standalone)
2798 and in any case decode why it stopped, and act accordingly. */
43ff13b4 2799 /* Do this only if we are not using the event loop, or if the target
1777feb0 2800 does not support asynchronous execution. */
362646f5 2801 if (!target_can_async_p ())
43ff13b4 2802 {
e4c8541f 2803 wait_for_inferior ();
43ff13b4
JM
2804 normal_stop ();
2805 }
c906108c 2806}
c906108c
SS
2807\f
2808
2809/* Start remote-debugging of a machine over a serial link. */
96baa820 2810
c906108c 2811void
8621d6a9 2812start_remote (int from_tty)
c906108c 2813{
d6b48e9c 2814 struct inferior *inferior;
d6b48e9c
PA
2815
2816 inferior = current_inferior ();
16c381f0 2817 inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
43ff13b4 2818
1777feb0 2819 /* Always go on waiting for the target, regardless of the mode. */
6426a772 2820 /* FIXME: cagney/1999-09-23: At present it isn't possible to
7e73cedf 2821 indicate to wait_for_inferior that a target should timeout if
6426a772
JM
2822 nothing is returned (instead of just blocking). Because of this,
2823 targets expecting an immediate response need to, internally, set
2824 things up so that the target_wait() is forced to eventually
1777feb0 2825 timeout. */
6426a772
JM
2826 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
2827 differentiate to its caller what the state of the target is after
2828 the initial open has been performed. Here we're assuming that
2829 the target has stopped. It should be possible to eventually have
2830 target_open() return to the caller an indication that the target
2831 is currently running and GDB state should be set to the same as
1777feb0 2832 for an async run. */
e4c8541f 2833 wait_for_inferior ();
8621d6a9
DJ
2834
2835 /* Now that the inferior has stopped, do any bookkeeping like
2836 loading shared libraries. We want to do this before normal_stop,
2837 so that the displayed frame is up to date. */
2838 post_create_inferior (&current_target, from_tty);
2839
6426a772 2840 normal_stop ();
c906108c
SS
2841}
2842
2843/* Initialize static vars when a new inferior begins. */
2844
2845void
96baa820 2846init_wait_for_inferior (void)
c906108c
SS
2847{
2848 /* These are meaningless until the first time through wait_for_inferior. */
c906108c 2849
c906108c
SS
2850 breakpoint_init_inferior (inf_starting);
2851
70509625 2852 clear_proceed_status (0);
9f976b41 2853
ca005067 2854 target_last_wait_ptid = minus_one_ptid;
237fc4c9 2855
842951eb 2856 previous_inferior_ptid = inferior_ptid;
0d1e5fa7 2857
edb3359d
DJ
2858 /* Discard any skipped inlined frames. */
2859 clear_inline_frame_state (minus_one_ptid);
c906108c 2860}
237fc4c9 2861
c906108c 2862\f
0d1e5fa7
PA
2863/* Data to be passed around while handling an event. This data is
2864 discarded between events. */
c5aa993b 2865struct execution_control_state
488f131b 2866{
0d1e5fa7 2867 ptid_t ptid;
4e1c45ea
PA
2868 /* The thread that got the event, if this was a thread event; NULL
2869 otherwise. */
2870 struct thread_info *event_thread;
2871
488f131b 2872 struct target_waitstatus ws;
7e324e48 2873 int stop_func_filled_in;
488f131b
JB
2874 CORE_ADDR stop_func_start;
2875 CORE_ADDR stop_func_end;
2c02bd72 2876 const char *stop_func_name;
488f131b 2877 int wait_some_more;
4f5d7f63 2878
2adfaa28
PA
2879 /* True if the event thread hit the single-step breakpoint of
2880 another thread. Thus the event doesn't cause a stop, the thread
2881 needs to be single-stepped past the single-step breakpoint before
2882 we can switch back to the original stepping thread. */
2883 int hit_singlestep_breakpoint;
488f131b
JB
2884};
2885
ec9499be 2886static void handle_inferior_event (struct execution_control_state *ecs);
cd0fc7c3 2887
568d6575
UW
2888static void handle_step_into_function (struct gdbarch *gdbarch,
2889 struct execution_control_state *ecs);
2890static void handle_step_into_function_backward (struct gdbarch *gdbarch,
2891 struct execution_control_state *ecs);
4f5d7f63 2892static void handle_signal_stop (struct execution_control_state *ecs);
186c406b 2893static void check_exception_resume (struct execution_control_state *,
28106bc2 2894 struct frame_info *);
611c83ae 2895
bdc36728 2896static void end_stepping_range (struct execution_control_state *ecs);
22bcd14b 2897static void stop_waiting (struct execution_control_state *ecs);
104c1213 2898static void prepare_to_wait (struct execution_control_state *ecs);
d4f3574e 2899static void keep_going (struct execution_control_state *ecs);
94c57d6a 2900static void process_event_stop_test (struct execution_control_state *ecs);
c447ac0b 2901static int switch_back_to_stepped_thread (struct execution_control_state *ecs);
104c1213 2902
252fbfc8
PA
2903/* Callback for iterate over threads. If the thread is stopped, but
2904 the user/frontend doesn't know about that yet, go through
2905 normal_stop, as if the thread had just stopped now. ARG points at
2906 a ptid. If PTID is MINUS_ONE_PTID, applies to all threads. If
2907 ptid_is_pid(PTID) is true, applies to all threads of the process
2908 pointed at by PTID. Otherwise, apply only to the thread pointed by
2909 PTID. */
2910
2911static int
2912infrun_thread_stop_requested_callback (struct thread_info *info, void *arg)
2913{
2914 ptid_t ptid = * (ptid_t *) arg;
2915
2916 if ((ptid_equal (info->ptid, ptid)
2917 || ptid_equal (minus_one_ptid, ptid)
2918 || (ptid_is_pid (ptid)
2919 && ptid_get_pid (ptid) == ptid_get_pid (info->ptid)))
2920 && is_running (info->ptid)
2921 && !is_executing (info->ptid))
2922 {
2923 struct cleanup *old_chain;
2924 struct execution_control_state ecss;
2925 struct execution_control_state *ecs = &ecss;
2926
2927 memset (ecs, 0, sizeof (*ecs));
2928
2929 old_chain = make_cleanup_restore_current_thread ();
2930
f15cb84a
YQ
2931 overlay_cache_invalid = 1;
2932 /* Flush target cache before starting to handle each event.
2933 Target was running and cache could be stale. This is just a
2934 heuristic. Running threads may modify target memory, but we
2935 don't get any event. */
2936 target_dcache_invalidate ();
2937
252fbfc8
PA
2938 /* Go through handle_inferior_event/normal_stop, so we always
2939 have consistent output as if the stop event had been
2940 reported. */
2941 ecs->ptid = info->ptid;
e09875d4 2942 ecs->event_thread = find_thread_ptid (info->ptid);
252fbfc8 2943 ecs->ws.kind = TARGET_WAITKIND_STOPPED;
a493e3e2 2944 ecs->ws.value.sig = GDB_SIGNAL_0;
252fbfc8
PA
2945
2946 handle_inferior_event (ecs);
2947
2948 if (!ecs->wait_some_more)
2949 {
2950 struct thread_info *tp;
2951
2952 normal_stop ();
2953
fa4cd53f 2954 /* Finish off the continuations. */
252fbfc8 2955 tp = inferior_thread ();
fa4cd53f
PA
2956 do_all_intermediate_continuations_thread (tp, 1);
2957 do_all_continuations_thread (tp, 1);
252fbfc8
PA
2958 }
2959
2960 do_cleanups (old_chain);
2961 }
2962
2963 return 0;
2964}
2965
2966/* This function is attached as a "thread_stop_requested" observer.
2967 Cleanup local state that assumed the PTID was to be resumed, and
2968 report the stop to the frontend. */
2969
2c0b251b 2970static void
252fbfc8
PA
2971infrun_thread_stop_requested (ptid_t ptid)
2972{
c2829269 2973 struct thread_info *tp;
252fbfc8 2974
c2829269
PA
2975 /* PTID was requested to stop. Remove matching threads from the
2976 step-over queue, so we don't try to resume them
2977 automatically. */
2978 ALL_NON_EXITED_THREADS (tp)
2979 if (ptid_match (tp->ptid, ptid))
2980 {
2981 if (thread_is_in_step_over_chain (tp))
2982 thread_step_over_chain_remove (tp);
2983 }
252fbfc8
PA
2984
2985 iterate_over_threads (infrun_thread_stop_requested_callback, &ptid);
2986}
2987
a07daef3
PA
2988static void
2989infrun_thread_thread_exit (struct thread_info *tp, int silent)
2990{
2991 if (ptid_equal (target_last_wait_ptid, tp->ptid))
2992 nullify_last_target_wait_ptid ();
2993}
2994
0cbcdb96
PA
2995/* Delete the step resume, single-step and longjmp/exception resume
2996 breakpoints of TP. */
4e1c45ea 2997
0cbcdb96
PA
2998static void
2999delete_thread_infrun_breakpoints (struct thread_info *tp)
4e1c45ea 3000{
0cbcdb96
PA
3001 delete_step_resume_breakpoint (tp);
3002 delete_exception_resume_breakpoint (tp);
34b7e8a6 3003 delete_single_step_breakpoints (tp);
4e1c45ea
PA
3004}
3005
0cbcdb96
PA
3006/* If the target still has execution, call FUNC for each thread that
3007 just stopped. In all-stop, that's all the non-exited threads; in
3008 non-stop, that's the current thread, only. */
3009
3010typedef void (*for_each_just_stopped_thread_callback_func)
3011 (struct thread_info *tp);
4e1c45ea
PA
3012
3013static void
0cbcdb96 3014for_each_just_stopped_thread (for_each_just_stopped_thread_callback_func func)
4e1c45ea 3015{
0cbcdb96 3016 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
4e1c45ea
PA
3017 return;
3018
3019 if (non_stop)
3020 {
0cbcdb96
PA
3021 /* If in non-stop mode, only the current thread stopped. */
3022 func (inferior_thread ());
4e1c45ea
PA
3023 }
3024 else
0cbcdb96
PA
3025 {
3026 struct thread_info *tp;
3027
3028 /* In all-stop mode, all threads have stopped. */
3029 ALL_NON_EXITED_THREADS (tp)
3030 {
3031 func (tp);
3032 }
3033 }
3034}
3035
3036/* Delete the step resume and longjmp/exception resume breakpoints of
3037 the threads that just stopped. */
3038
3039static void
3040delete_just_stopped_threads_infrun_breakpoints (void)
3041{
3042 for_each_just_stopped_thread (delete_thread_infrun_breakpoints);
34b7e8a6
PA
3043}
3044
3045/* Delete the single-step breakpoints of the threads that just
3046 stopped. */
7c16b83e 3047
34b7e8a6
PA
3048static void
3049delete_just_stopped_threads_single_step_breakpoints (void)
3050{
3051 for_each_just_stopped_thread (delete_single_step_breakpoints);
4e1c45ea
PA
3052}
3053
1777feb0 3054/* A cleanup wrapper. */
4e1c45ea
PA
3055
3056static void
0cbcdb96 3057delete_just_stopped_threads_infrun_breakpoints_cleanup (void *arg)
4e1c45ea 3058{
0cbcdb96 3059 delete_just_stopped_threads_infrun_breakpoints ();
4e1c45ea
PA
3060}
3061
223698f8
DE
3062/* Pretty print the results of target_wait, for debugging purposes. */
3063
3064static void
3065print_target_wait_results (ptid_t waiton_ptid, ptid_t result_ptid,
3066 const struct target_waitstatus *ws)
3067{
3068 char *status_string = target_waitstatus_to_string (ws);
3069 struct ui_file *tmp_stream = mem_fileopen ();
3070 char *text;
223698f8
DE
3071
3072 /* The text is split over several lines because it was getting too long.
3073 Call fprintf_unfiltered (gdb_stdlog) once so that the text is still
3074 output as a unit; we want only one timestamp printed if debug_timestamp
3075 is set. */
3076
3077 fprintf_unfiltered (tmp_stream,
1176ecec
PA
3078 "infrun: target_wait (%d.%ld.%ld",
3079 ptid_get_pid (waiton_ptid),
3080 ptid_get_lwp (waiton_ptid),
3081 ptid_get_tid (waiton_ptid));
dfd4cc63 3082 if (ptid_get_pid (waiton_ptid) != -1)
223698f8
DE
3083 fprintf_unfiltered (tmp_stream,
3084 " [%s]", target_pid_to_str (waiton_ptid));
3085 fprintf_unfiltered (tmp_stream, ", status) =\n");
3086 fprintf_unfiltered (tmp_stream,
1176ecec 3087 "infrun: %d.%ld.%ld [%s],\n",
dfd4cc63 3088 ptid_get_pid (result_ptid),
1176ecec
PA
3089 ptid_get_lwp (result_ptid),
3090 ptid_get_tid (result_ptid),
dfd4cc63 3091 target_pid_to_str (result_ptid));
223698f8
DE
3092 fprintf_unfiltered (tmp_stream,
3093 "infrun: %s\n",
3094 status_string);
3095
759ef836 3096 text = ui_file_xstrdup (tmp_stream, NULL);
223698f8
DE
3097
3098 /* This uses %s in part to handle %'s in the text, but also to avoid
3099 a gcc error: the format attribute requires a string literal. */
3100 fprintf_unfiltered (gdb_stdlog, "%s", text);
3101
3102 xfree (status_string);
3103 xfree (text);
3104 ui_file_delete (tmp_stream);
3105}
3106
24291992
PA
3107/* Prepare and stabilize the inferior for detaching it. E.g.,
3108 detaching while a thread is displaced stepping is a recipe for
3109 crashing it, as nothing would readjust the PC out of the scratch
3110 pad. */
3111
3112void
3113prepare_for_detach (void)
3114{
3115 struct inferior *inf = current_inferior ();
3116 ptid_t pid_ptid = pid_to_ptid (inf->pid);
3117 struct cleanup *old_chain_1;
3118 struct displaced_step_inferior_state *displaced;
3119
3120 displaced = get_displaced_stepping_state (inf->pid);
3121
3122 /* Is any thread of this process displaced stepping? If not,
3123 there's nothing else to do. */
3124 if (displaced == NULL || ptid_equal (displaced->step_ptid, null_ptid))
3125 return;
3126
3127 if (debug_infrun)
3128 fprintf_unfiltered (gdb_stdlog,
3129 "displaced-stepping in-process while detaching");
3130
3131 old_chain_1 = make_cleanup_restore_integer (&inf->detaching);
3132 inf->detaching = 1;
3133
3134 while (!ptid_equal (displaced->step_ptid, null_ptid))
3135 {
3136 struct cleanup *old_chain_2;
3137 struct execution_control_state ecss;
3138 struct execution_control_state *ecs;
3139
3140 ecs = &ecss;
3141 memset (ecs, 0, sizeof (*ecs));
3142
3143 overlay_cache_invalid = 1;
f15cb84a
YQ
3144 /* Flush target cache before starting to handle each event.
3145 Target was running and cache could be stale. This is just a
3146 heuristic. Running threads may modify target memory, but we
3147 don't get any event. */
3148 target_dcache_invalidate ();
24291992 3149
24291992
PA
3150 if (deprecated_target_wait_hook)
3151 ecs->ptid = deprecated_target_wait_hook (pid_ptid, &ecs->ws, 0);
3152 else
3153 ecs->ptid = target_wait (pid_ptid, &ecs->ws, 0);
3154
3155 if (debug_infrun)
3156 print_target_wait_results (pid_ptid, ecs->ptid, &ecs->ws);
3157
3158 /* If an error happens while handling the event, propagate GDB's
3159 knowledge of the executing state to the frontend/user running
3160 state. */
3e43a32a
MS
3161 old_chain_2 = make_cleanup (finish_thread_state_cleanup,
3162 &minus_one_ptid);
24291992
PA
3163
3164 /* Now figure out what to do with the result of the result. */
3165 handle_inferior_event (ecs);
3166
3167 /* No error, don't finish the state yet. */
3168 discard_cleanups (old_chain_2);
3169
3170 /* Breakpoints and watchpoints are not installed on the target
3171 at this point, and signals are passed directly to the
3172 inferior, so this must mean the process is gone. */
3173 if (!ecs->wait_some_more)
3174 {
3175 discard_cleanups (old_chain_1);
3176 error (_("Program exited while detaching"));
3177 }
3178 }
3179
3180 discard_cleanups (old_chain_1);
3181}
3182
cd0fc7c3 3183/* Wait for control to return from inferior to debugger.
ae123ec6 3184
cd0fc7c3
SS
3185 If inferior gets a signal, we may decide to start it up again
3186 instead of returning. That is why there is a loop in this function.
3187 When this function actually returns it means the inferior
3188 should be left stopped and GDB should read more commands. */
3189
3190void
e4c8541f 3191wait_for_inferior (void)
cd0fc7c3
SS
3192{
3193 struct cleanup *old_cleanups;
e6f5c25b 3194 struct cleanup *thread_state_chain;
c906108c 3195
527159b7 3196 if (debug_infrun)
ae123ec6 3197 fprintf_unfiltered
e4c8541f 3198 (gdb_stdlog, "infrun: wait_for_inferior ()\n");
527159b7 3199
0cbcdb96
PA
3200 old_cleanups
3201 = make_cleanup (delete_just_stopped_threads_infrun_breakpoints_cleanup,
3202 NULL);
cd0fc7c3 3203
e6f5c25b
PA
3204 /* If an error happens while handling the event, propagate GDB's
3205 knowledge of the executing state to the frontend/user running
3206 state. */
3207 thread_state_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
3208
c906108c
SS
3209 while (1)
3210 {
ae25568b
PA
3211 struct execution_control_state ecss;
3212 struct execution_control_state *ecs = &ecss;
963f9c80 3213 ptid_t waiton_ptid = minus_one_ptid;
29f49a6a 3214
ae25568b
PA
3215 memset (ecs, 0, sizeof (*ecs));
3216
ec9499be 3217 overlay_cache_invalid = 1;
ec9499be 3218
f15cb84a
YQ
3219 /* Flush target cache before starting to handle each event.
3220 Target was running and cache could be stale. This is just a
3221 heuristic. Running threads may modify target memory, but we
3222 don't get any event. */
3223 target_dcache_invalidate ();
3224
9a4105ab 3225 if (deprecated_target_wait_hook)
47608cb1 3226 ecs->ptid = deprecated_target_wait_hook (waiton_ptid, &ecs->ws, 0);
cd0fc7c3 3227 else
47608cb1 3228 ecs->ptid = target_wait (waiton_ptid, &ecs->ws, 0);
c906108c 3229
f00150c9 3230 if (debug_infrun)
223698f8 3231 print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
f00150c9 3232
cd0fc7c3
SS
3233 /* Now figure out what to do with the result of the result. */
3234 handle_inferior_event (ecs);
c906108c 3235
cd0fc7c3
SS
3236 if (!ecs->wait_some_more)
3237 break;
3238 }
4e1c45ea 3239
e6f5c25b
PA
3240 /* No error, don't finish the state yet. */
3241 discard_cleanups (thread_state_chain);
3242
cd0fc7c3
SS
3243 do_cleanups (old_cleanups);
3244}
c906108c 3245
d3d4baed
PA
3246/* Cleanup that reinstalls the readline callback handler, if the
3247 target is running in the background. If while handling the target
3248 event something triggered a secondary prompt, like e.g., a
3249 pagination prompt, we'll have removed the callback handler (see
3250 gdb_readline_wrapper_line). Need to do this as we go back to the
3251 event loop, ready to process further input. Note this has no
3252 effect if the handler hasn't actually been removed, because calling
3253 rl_callback_handler_install resets the line buffer, thus losing
3254 input. */
3255
3256static void
3257reinstall_readline_callback_handler_cleanup (void *arg)
3258{
6c400b59
PA
3259 if (!interpreter_async)
3260 {
3261 /* We're not going back to the top level event loop yet. Don't
3262 install the readline callback, as it'd prep the terminal,
3263 readline-style (raw, noecho) (e.g., --batch). We'll install
3264 it the next time the prompt is displayed, when we're ready
3265 for input. */
3266 return;
3267 }
3268
d3d4baed
PA
3269 if (async_command_editing_p && !sync_execution)
3270 gdb_rl_callback_handler_reinstall ();
3271}
3272
1777feb0 3273/* Asynchronous version of wait_for_inferior. It is called by the
43ff13b4 3274 event loop whenever a change of state is detected on the file
1777feb0
MS
3275 descriptor corresponding to the target. It can be called more than
3276 once to complete a single execution command. In such cases we need
3277 to keep the state in a global variable ECSS. If it is the last time
a474d7c2
PA
3278 that this function is called for a single execution command, then
3279 report to the user that the inferior has stopped, and do the
1777feb0 3280 necessary cleanups. */
43ff13b4
JM
3281
3282void
fba45db2 3283fetch_inferior_event (void *client_data)
43ff13b4 3284{
0d1e5fa7 3285 struct execution_control_state ecss;
a474d7c2 3286 struct execution_control_state *ecs = &ecss;
4f8d22e3 3287 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
29f49a6a 3288 struct cleanup *ts_old_chain;
4f8d22e3 3289 int was_sync = sync_execution;
0f641c01 3290 int cmd_done = 0;
963f9c80 3291 ptid_t waiton_ptid = minus_one_ptid;
43ff13b4 3292
0d1e5fa7
PA
3293 memset (ecs, 0, sizeof (*ecs));
3294
d3d4baed
PA
3295 /* End up with readline processing input, if necessary. */
3296 make_cleanup (reinstall_readline_callback_handler_cleanup, NULL);
3297
c5187ac6
PA
3298 /* We're handling a live event, so make sure we're doing live
3299 debugging. If we're looking at traceframes while the target is
3300 running, we're going to need to get back to that mode after
3301 handling the event. */
3302 if (non_stop)
3303 {
3304 make_cleanup_restore_current_traceframe ();
e6e4e701 3305 set_current_traceframe (-1);
c5187ac6
PA
3306 }
3307
4f8d22e3
PA
3308 if (non_stop)
3309 /* In non-stop mode, the user/frontend should not notice a thread
3310 switch due to internal events. Make sure we reverse to the
3311 user selected thread and frame after handling the event and
3312 running any breakpoint commands. */
3313 make_cleanup_restore_current_thread ();
3314
ec9499be 3315 overlay_cache_invalid = 1;
f15cb84a
YQ
3316 /* Flush target cache before starting to handle each event. Target
3317 was running and cache could be stale. This is just a heuristic.
3318 Running threads may modify target memory, but we don't get any
3319 event. */
3320 target_dcache_invalidate ();
3dd5b83d 3321
32231432
PA
3322 make_cleanup_restore_integer (&execution_direction);
3323 execution_direction = target_execution_direction ();
3324
9a4105ab 3325 if (deprecated_target_wait_hook)
a474d7c2 3326 ecs->ptid =
47608cb1 3327 deprecated_target_wait_hook (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
43ff13b4 3328 else
47608cb1 3329 ecs->ptid = target_wait (waiton_ptid, &ecs->ws, TARGET_WNOHANG);
43ff13b4 3330
f00150c9 3331 if (debug_infrun)
223698f8 3332 print_target_wait_results (waiton_ptid, ecs->ptid, &ecs->ws);
f00150c9 3333
29f49a6a
PA
3334 /* If an error happens while handling the event, propagate GDB's
3335 knowledge of the executing state to the frontend/user running
3336 state. */
3337 if (!non_stop)
3338 ts_old_chain = make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
3339 else
3340 ts_old_chain = make_cleanup (finish_thread_state_cleanup, &ecs->ptid);
3341
353d1d73
JK
3342 /* Get executed before make_cleanup_restore_current_thread above to apply
3343 still for the thread which has thrown the exception. */
3344 make_bpstat_clear_actions_cleanup ();
3345
7c16b83e
PA
3346 make_cleanup (delete_just_stopped_threads_infrun_breakpoints_cleanup, NULL);
3347
43ff13b4 3348 /* Now figure out what to do with the result of the result. */
a474d7c2 3349 handle_inferior_event (ecs);
43ff13b4 3350
a474d7c2 3351 if (!ecs->wait_some_more)
43ff13b4 3352 {
c9657e70 3353 struct inferior *inf = find_inferior_ptid (ecs->ptid);
d6b48e9c 3354
0cbcdb96 3355 delete_just_stopped_threads_infrun_breakpoints ();
f107f563 3356
d6b48e9c 3357 /* We may not find an inferior if this was a process exit. */
16c381f0 3358 if (inf == NULL || inf->control.stop_soon == NO_STOP_QUIETLY)
83c265ab
PA
3359 normal_stop ();
3360
af679fd0 3361 if (target_has_execution
0e5bf2a8 3362 && ecs->ws.kind != TARGET_WAITKIND_NO_RESUMED
af679fd0
PA
3363 && ecs->ws.kind != TARGET_WAITKIND_EXITED
3364 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED
3365 && ecs->event_thread->step_multi
16c381f0 3366 && ecs->event_thread->control.stop_step)
c2d11a7d
JM
3367 inferior_event_handler (INF_EXEC_CONTINUE, NULL);
3368 else
0f641c01
PA
3369 {
3370 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
3371 cmd_done = 1;
3372 }
43ff13b4 3373 }
4f8d22e3 3374
29f49a6a
PA
3375 /* No error, don't finish the thread states yet. */
3376 discard_cleanups (ts_old_chain);
3377
4f8d22e3
PA
3378 /* Revert thread and frame. */
3379 do_cleanups (old_chain);
3380
3381 /* If the inferior was in sync execution mode, and now isn't,
0f641c01
PA
3382 restore the prompt (a synchronous execution command has finished,
3383 and we're ready for input). */
b4a14fd0 3384 if (interpreter_async && was_sync && !sync_execution)
92bcb5f9 3385 observer_notify_sync_execution_done ();
0f641c01
PA
3386
3387 if (cmd_done
3388 && !was_sync
3389 && exec_done_display_p
3390 && (ptid_equal (inferior_ptid, null_ptid)
3391 || !is_running (inferior_ptid)))
3392 printf_unfiltered (_("completed.\n"));
43ff13b4
JM
3393}
3394
edb3359d
DJ
3395/* Record the frame and location we're currently stepping through. */
3396void
3397set_step_info (struct frame_info *frame, struct symtab_and_line sal)
3398{
3399 struct thread_info *tp = inferior_thread ();
3400
16c381f0
JK
3401 tp->control.step_frame_id = get_frame_id (frame);
3402 tp->control.step_stack_frame_id = get_stack_frame_id (frame);
edb3359d
DJ
3403
3404 tp->current_symtab = sal.symtab;
3405 tp->current_line = sal.line;
3406}
3407
0d1e5fa7
PA
3408/* Clear context switchable stepping state. */
3409
3410void
4e1c45ea 3411init_thread_stepping_state (struct thread_info *tss)
0d1e5fa7 3412{
7f5ef605 3413 tss->stepped_breakpoint = 0;
0d1e5fa7 3414 tss->stepping_over_breakpoint = 0;
963f9c80 3415 tss->stepping_over_watchpoint = 0;
0d1e5fa7 3416 tss->step_after_step_resume_breakpoint = 0;
cd0fc7c3
SS
3417}
3418
c32c64b7
DE
3419/* Set the cached copy of the last ptid/waitstatus. */
3420
3421static void
3422set_last_target_status (ptid_t ptid, struct target_waitstatus status)
3423{
3424 target_last_wait_ptid = ptid;
3425 target_last_waitstatus = status;
3426}
3427
e02bc4cc 3428/* Return the cached copy of the last pid/waitstatus returned by
9a4105ab
AC
3429 target_wait()/deprecated_target_wait_hook(). The data is actually
3430 cached by handle_inferior_event(), which gets called immediately
3431 after target_wait()/deprecated_target_wait_hook(). */
e02bc4cc
DS
3432
3433void
488f131b 3434get_last_target_status (ptid_t *ptidp, struct target_waitstatus *status)
e02bc4cc 3435{
39f77062 3436 *ptidp = target_last_wait_ptid;
e02bc4cc
DS
3437 *status = target_last_waitstatus;
3438}
3439
ac264b3b
MS
3440void
3441nullify_last_target_wait_ptid (void)
3442{
3443 target_last_wait_ptid = minus_one_ptid;
3444}
3445
dcf4fbde 3446/* Switch thread contexts. */
dd80620e
MS
3447
3448static void
0d1e5fa7 3449context_switch (ptid_t ptid)
dd80620e 3450{
4b51d87b 3451 if (debug_infrun && !ptid_equal (ptid, inferior_ptid))
fd48f117
DJ
3452 {
3453 fprintf_unfiltered (gdb_stdlog, "infrun: Switching context from %s ",
3454 target_pid_to_str (inferior_ptid));
3455 fprintf_unfiltered (gdb_stdlog, "to %s\n",
0d1e5fa7 3456 target_pid_to_str (ptid));
fd48f117
DJ
3457 }
3458
0d1e5fa7 3459 switch_to_thread (ptid);
dd80620e
MS
3460}
3461
d8dd4d5f
PA
3462/* If the target can't tell whether we've hit breakpoints
3463 (target_supports_stopped_by_sw_breakpoint), and we got a SIGTRAP,
3464 check whether that could have been caused by a breakpoint. If so,
3465 adjust the PC, per gdbarch_decr_pc_after_break. */
3466
4fa8626c 3467static void
d8dd4d5f
PA
3468adjust_pc_after_break (struct thread_info *thread,
3469 struct target_waitstatus *ws)
4fa8626c 3470{
24a73cce
UW
3471 struct regcache *regcache;
3472 struct gdbarch *gdbarch;
6c95b8df 3473 struct address_space *aspace;
118e6252 3474 CORE_ADDR breakpoint_pc, decr_pc;
4fa8626c 3475
4fa8626c
DJ
3476 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
3477 we aren't, just return.
9709f61c
DJ
3478
3479 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
b798847d
UW
3480 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
3481 implemented by software breakpoints should be handled through the normal
3482 breakpoint layer.
8fb3e588 3483
4fa8626c
DJ
3484 NOTE drow/2004-01-31: On some targets, breakpoints may generate
3485 different signals (SIGILL or SIGEMT for instance), but it is less
3486 clear where the PC is pointing afterwards. It may not match
b798847d
UW
3487 gdbarch_decr_pc_after_break. I don't know any specific target that
3488 generates these signals at breakpoints (the code has been in GDB since at
3489 least 1992) so I can not guess how to handle them here.
8fb3e588 3490
e6cf7916
UW
3491 In earlier versions of GDB, a target with
3492 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
b798847d
UW
3493 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
3494 target with both of these set in GDB history, and it seems unlikely to be
3495 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
4fa8626c 3496
d8dd4d5f 3497 if (ws->kind != TARGET_WAITKIND_STOPPED)
4fa8626c
DJ
3498 return;
3499
d8dd4d5f 3500 if (ws->value.sig != GDB_SIGNAL_TRAP)
4fa8626c
DJ
3501 return;
3502
4058b839
PA
3503 /* In reverse execution, when a breakpoint is hit, the instruction
3504 under it has already been de-executed. The reported PC always
3505 points at the breakpoint address, so adjusting it further would
3506 be wrong. E.g., consider this case on a decr_pc_after_break == 1
3507 architecture:
3508
3509 B1 0x08000000 : INSN1
3510 B2 0x08000001 : INSN2
3511 0x08000002 : INSN3
3512 PC -> 0x08000003 : INSN4
3513
3514 Say you're stopped at 0x08000003 as above. Reverse continuing
3515 from that point should hit B2 as below. Reading the PC when the
3516 SIGTRAP is reported should read 0x08000001 and INSN2 should have
3517 been de-executed already.
3518
3519 B1 0x08000000 : INSN1
3520 B2 PC -> 0x08000001 : INSN2
3521 0x08000002 : INSN3
3522 0x08000003 : INSN4
3523
3524 We can't apply the same logic as for forward execution, because
3525 we would wrongly adjust the PC to 0x08000000, since there's a
3526 breakpoint at PC - 1. We'd then report a hit on B1, although
3527 INSN1 hadn't been de-executed yet. Doing nothing is the correct
3528 behaviour. */
3529 if (execution_direction == EXEC_REVERSE)
3530 return;
3531
1cf4d951
PA
3532 /* If the target can tell whether the thread hit a SW breakpoint,
3533 trust it. Targets that can tell also adjust the PC
3534 themselves. */
3535 if (target_supports_stopped_by_sw_breakpoint ())
3536 return;
3537
3538 /* Note that relying on whether a breakpoint is planted in memory to
3539 determine this can fail. E.g,. the breakpoint could have been
3540 removed since. Or the thread could have been told to step an
3541 instruction the size of a breakpoint instruction, and only
3542 _after_ was a breakpoint inserted at its address. */
3543
24a73cce
UW
3544 /* If this target does not decrement the PC after breakpoints, then
3545 we have nothing to do. */
d8dd4d5f 3546 regcache = get_thread_regcache (thread->ptid);
24a73cce 3547 gdbarch = get_regcache_arch (regcache);
118e6252 3548
527a273a 3549 decr_pc = gdbarch_decr_pc_after_break (gdbarch);
118e6252 3550 if (decr_pc == 0)
24a73cce
UW
3551 return;
3552
6c95b8df
PA
3553 aspace = get_regcache_aspace (regcache);
3554
8aad930b
AC
3555 /* Find the location where (if we've hit a breakpoint) the
3556 breakpoint would be. */
118e6252 3557 breakpoint_pc = regcache_read_pc (regcache) - decr_pc;
8aad930b 3558
1cf4d951
PA
3559 /* If the target can't tell whether a software breakpoint triggered,
3560 fallback to figuring it out based on breakpoints we think were
3561 inserted in the target, and on whether the thread was stepped or
3562 continued. */
3563
1c5cfe86
PA
3564 /* Check whether there actually is a software breakpoint inserted at
3565 that location.
3566
3567 If in non-stop mode, a race condition is possible where we've
3568 removed a breakpoint, but stop events for that breakpoint were
3569 already queued and arrive later. To suppress those spurious
3570 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
1cf4d951
PA
3571 and retire them after a number of stop events are reported. Note
3572 this is an heuristic and can thus get confused. The real fix is
3573 to get the "stopped by SW BP and needs adjustment" info out of
3574 the target/kernel (and thus never reach here; see above). */
6c95b8df
PA
3575 if (software_breakpoint_inserted_here_p (aspace, breakpoint_pc)
3576 || (non_stop && moribund_breakpoint_here_p (aspace, breakpoint_pc)))
8aad930b 3577 {
77f9e713 3578 struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
abbb1732 3579
8213266a 3580 if (record_full_is_used ())
77f9e713 3581 record_full_gdb_operation_disable_set ();
96429cc8 3582
1c0fdd0e
UW
3583 /* When using hardware single-step, a SIGTRAP is reported for both
3584 a completed single-step and a software breakpoint. Need to
3585 differentiate between the two, as the latter needs adjusting
3586 but the former does not.
3587
3588 The SIGTRAP can be due to a completed hardware single-step only if
3589 - we didn't insert software single-step breakpoints
1c0fdd0e
UW
3590 - this thread is currently being stepped
3591
3592 If any of these events did not occur, we must have stopped due
3593 to hitting a software breakpoint, and have to back up to the
3594 breakpoint address.
3595
3596 As a special case, we could have hardware single-stepped a
3597 software breakpoint. In this case (prev_pc == breakpoint_pc),
3598 we also need to back up to the breakpoint address. */
3599
d8dd4d5f
PA
3600 if (thread_has_single_step_breakpoints_set (thread)
3601 || !currently_stepping (thread)
3602 || (thread->stepped_breakpoint
3603 && thread->prev_pc == breakpoint_pc))
515630c5 3604 regcache_write_pc (regcache, breakpoint_pc);
96429cc8 3605
77f9e713 3606 do_cleanups (old_cleanups);
8aad930b 3607 }
4fa8626c
DJ
3608}
3609
edb3359d
DJ
3610static int
3611stepped_in_from (struct frame_info *frame, struct frame_id step_frame_id)
3612{
3613 for (frame = get_prev_frame (frame);
3614 frame != NULL;
3615 frame = get_prev_frame (frame))
3616 {
3617 if (frame_id_eq (get_frame_id (frame), step_frame_id))
3618 return 1;
3619 if (get_frame_type (frame) != INLINE_FRAME)
3620 break;
3621 }
3622
3623 return 0;
3624}
3625
a96d9b2e
SDJ
3626/* Auxiliary function that handles syscall entry/return events.
3627 It returns 1 if the inferior should keep going (and GDB
3628 should ignore the event), or 0 if the event deserves to be
3629 processed. */
ca2163eb 3630
a96d9b2e 3631static int
ca2163eb 3632handle_syscall_event (struct execution_control_state *ecs)
a96d9b2e 3633{
ca2163eb 3634 struct regcache *regcache;
ca2163eb
PA
3635 int syscall_number;
3636
3637 if (!ptid_equal (ecs->ptid, inferior_ptid))
3638 context_switch (ecs->ptid);
3639
3640 regcache = get_thread_regcache (ecs->ptid);
f90263c1 3641 syscall_number = ecs->ws.value.syscall_number;
ca2163eb
PA
3642 stop_pc = regcache_read_pc (regcache);
3643
a96d9b2e
SDJ
3644 if (catch_syscall_enabled () > 0
3645 && catching_syscall_number (syscall_number) > 0)
3646 {
3647 if (debug_infrun)
3648 fprintf_unfiltered (gdb_stdlog, "infrun: syscall number = '%d'\n",
3649 syscall_number);
a96d9b2e 3650
16c381f0 3651 ecs->event_thread->control.stop_bpstat
6c95b8df 3652 = bpstat_stop_status (get_regcache_aspace (regcache),
09ac7c10 3653 stop_pc, ecs->ptid, &ecs->ws);
ab04a2af 3654
ce12b012 3655 if (bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
ca2163eb
PA
3656 {
3657 /* Catchpoint hit. */
ca2163eb
PA
3658 return 0;
3659 }
a96d9b2e 3660 }
ca2163eb
PA
3661
3662 /* If no catchpoint triggered for this, then keep going. */
ca2163eb
PA
3663 keep_going (ecs);
3664 return 1;
a96d9b2e
SDJ
3665}
3666
7e324e48
GB
3667/* Lazily fill in the execution_control_state's stop_func_* fields. */
3668
3669static void
3670fill_in_stop_func (struct gdbarch *gdbarch,
3671 struct execution_control_state *ecs)
3672{
3673 if (!ecs->stop_func_filled_in)
3674 {
3675 /* Don't care about return value; stop_func_start and stop_func_name
3676 will both be 0 if it doesn't work. */
3677 find_pc_partial_function (stop_pc, &ecs->stop_func_name,
3678 &ecs->stop_func_start, &ecs->stop_func_end);
3679 ecs->stop_func_start
3680 += gdbarch_deprecated_function_start_offset (gdbarch);
3681
591a12a1
UW
3682 if (gdbarch_skip_entrypoint_p (gdbarch))
3683 ecs->stop_func_start = gdbarch_skip_entrypoint (gdbarch,
3684 ecs->stop_func_start);
3685
7e324e48
GB
3686 ecs->stop_func_filled_in = 1;
3687 }
3688}
3689
4f5d7f63
PA
3690
3691/* Return the STOP_SOON field of the inferior pointed at by PTID. */
3692
3693static enum stop_kind
3694get_inferior_stop_soon (ptid_t ptid)
3695{
c9657e70 3696 struct inferior *inf = find_inferior_ptid (ptid);
4f5d7f63
PA
3697
3698 gdb_assert (inf != NULL);
3699 return inf->control.stop_soon;
3700}
3701
05ba8510
PA
3702/* Given an execution control state that has been freshly filled in by
3703 an event from the inferior, figure out what it means and take
3704 appropriate action.
3705
3706 The alternatives are:
3707
22bcd14b 3708 1) stop_waiting and return; to really stop and return to the
05ba8510
PA
3709 debugger.
3710
3711 2) keep_going and return; to wait for the next event (set
3712 ecs->event_thread->stepping_over_breakpoint to 1 to single step
3713 once). */
c906108c 3714
ec9499be 3715static void
0b6e5e10 3716handle_inferior_event_1 (struct execution_control_state *ecs)
cd0fc7c3 3717{
d6b48e9c
PA
3718 enum stop_kind stop_soon;
3719
28736962
PA
3720 if (ecs->ws.kind == TARGET_WAITKIND_IGNORE)
3721 {
3722 /* We had an event in the inferior, but we are not interested in
3723 handling it at this level. The lower layers have already
3724 done what needs to be done, if anything.
3725
3726 One of the possible circumstances for this is when the
3727 inferior produces output for the console. The inferior has
3728 not stopped, and we are ignoring the event. Another possible
3729 circumstance is any event which the lower level knows will be
3730 reported multiple times without an intervening resume. */
3731 if (debug_infrun)
3732 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_IGNORE\n");
3733 prepare_to_wait (ecs);
3734 return;
3735 }
3736
0e5bf2a8
PA
3737 if (ecs->ws.kind == TARGET_WAITKIND_NO_RESUMED
3738 && target_can_async_p () && !sync_execution)
3739 {
3740 /* There were no unwaited-for children left in the target, but,
3741 we're not synchronously waiting for events either. Just
3742 ignore. Otherwise, if we were running a synchronous
3743 execution command, we need to cancel it and give the user
3744 back the terminal. */
3745 if (debug_infrun)
3746 fprintf_unfiltered (gdb_stdlog,
3747 "infrun: TARGET_WAITKIND_NO_RESUMED (ignoring)\n");
3748 prepare_to_wait (ecs);
3749 return;
3750 }
3751
1777feb0 3752 /* Cache the last pid/waitstatus. */
c32c64b7 3753 set_last_target_status (ecs->ptid, ecs->ws);
e02bc4cc 3754
ca005067 3755 /* Always clear state belonging to the previous time we stopped. */
aa7d318d 3756 stop_stack_dummy = STOP_NONE;
ca005067 3757
0e5bf2a8
PA
3758 if (ecs->ws.kind == TARGET_WAITKIND_NO_RESUMED)
3759 {
3760 /* No unwaited-for children left. IOW, all resumed children
3761 have exited. */
3762 if (debug_infrun)
3763 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_NO_RESUMED\n");
3764
3765 stop_print_frame = 0;
22bcd14b 3766 stop_waiting (ecs);
0e5bf2a8
PA
3767 return;
3768 }
3769
8c90c137 3770 if (ecs->ws.kind != TARGET_WAITKIND_EXITED
64776a0b 3771 && ecs->ws.kind != TARGET_WAITKIND_SIGNALLED)
359f5fe6
PA
3772 {
3773 ecs->event_thread = find_thread_ptid (ecs->ptid);
3774 /* If it's a new thread, add it to the thread database. */
3775 if (ecs->event_thread == NULL)
3776 ecs->event_thread = add_thread (ecs->ptid);
c1e36e3e
PA
3777
3778 /* Disable range stepping. If the next step request could use a
3779 range, this will be end up re-enabled then. */
3780 ecs->event_thread->control.may_range_step = 0;
359f5fe6 3781 }
88ed393a
JK
3782
3783 /* Dependent on valid ECS->EVENT_THREAD. */
d8dd4d5f 3784 adjust_pc_after_break (ecs->event_thread, &ecs->ws);
88ed393a
JK
3785
3786 /* Dependent on the current PC value modified by adjust_pc_after_break. */
3787 reinit_frame_cache ();
3788
28736962
PA
3789 breakpoint_retire_moribund ();
3790
2b009048
DJ
3791 /* First, distinguish signals caused by the debugger from signals
3792 that have to do with the program's own actions. Note that
3793 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
3794 on the operating system version. Here we detect when a SIGILL or
3795 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
3796 something similar for SIGSEGV, since a SIGSEGV will be generated
3797 when we're trying to execute a breakpoint instruction on a
3798 non-executable stack. This happens for call dummy breakpoints
3799 for architectures like SPARC that place call dummies on the
3800 stack. */
2b009048 3801 if (ecs->ws.kind == TARGET_WAITKIND_STOPPED
a493e3e2
PA
3802 && (ecs->ws.value.sig == GDB_SIGNAL_ILL
3803 || ecs->ws.value.sig == GDB_SIGNAL_SEGV
3804 || ecs->ws.value.sig == GDB_SIGNAL_EMT))
2b009048 3805 {
de0a0249
UW
3806 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3807
3808 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
3809 regcache_read_pc (regcache)))
3810 {
3811 if (debug_infrun)
3812 fprintf_unfiltered (gdb_stdlog,
3813 "infrun: Treating signal as SIGTRAP\n");
a493e3e2 3814 ecs->ws.value.sig = GDB_SIGNAL_TRAP;
de0a0249 3815 }
2b009048
DJ
3816 }
3817
28736962
PA
3818 /* Mark the non-executing threads accordingly. In all-stop, all
3819 threads of all processes are stopped when we get any event
e1316e60 3820 reported. In non-stop mode, only the event thread stops. */
28736962
PA
3821 if (!non_stop)
3822 set_executing (minus_one_ptid, 0);
e1316e60
PA
3823 else if (ecs->ws.kind == TARGET_WAITKIND_SIGNALLED
3824 || ecs->ws.kind == TARGET_WAITKIND_EXITED)
3825 {
3826 ptid_t pid_ptid;
3827
3828 /* If we're handling a process exit in non-stop mode, even
3829 though threads haven't been deleted yet, one would think that
3830 there is nothing to do, as threads of the dead process will
3831 be soon deleted, and threads of any other process were left
3832 running. However, on some targets, threads survive a process
3833 exit event. E.g., for the "checkpoint" command, when the
3834 current checkpoint/fork exits, linux-fork.c automatically
3835 switches to another fork from within target_mourn_inferior,
3836 by associating the same inferior/thread to another fork. We
3837 haven't mourned yet at this point, but we must mark any
3838 threads left in the process as not-executing so that
3839 finish_thread_state marks them stopped (in the user's
3840 perspective) if/when we present the stop to the user. */
3841 pid_ptid = pid_to_ptid (ptid_get_pid (ecs->ptid));
3842 set_executing (pid_ptid, 0);
3843 }
3844 else
7aee8dc2 3845 set_executing (ecs->ptid, 0);
8c90c137 3846
488f131b
JB
3847 switch (ecs->ws.kind)
3848 {
3849 case TARGET_WAITKIND_LOADED:
527159b7 3850 if (debug_infrun)
8a9de0e4 3851 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_LOADED\n");
5c09a2c5
PA
3852 if (!ptid_equal (ecs->ptid, inferior_ptid))
3853 context_switch (ecs->ptid);
b0f4b84b
DJ
3854 /* Ignore gracefully during startup of the inferior, as it might
3855 be the shell which has just loaded some objects, otherwise
3856 add the symbols for the newly loaded objects. Also ignore at
3857 the beginning of an attach or remote session; we will query
3858 the full list of libraries once the connection is
3859 established. */
4f5d7f63
PA
3860
3861 stop_soon = get_inferior_stop_soon (ecs->ptid);
c0236d92 3862 if (stop_soon == NO_STOP_QUIETLY)
488f131b 3863 {
edcc5120
TT
3864 struct regcache *regcache;
3865
edcc5120
TT
3866 regcache = get_thread_regcache (ecs->ptid);
3867
3868 handle_solib_event ();
3869
3870 ecs->event_thread->control.stop_bpstat
3871 = bpstat_stop_status (get_regcache_aspace (regcache),
3872 stop_pc, ecs->ptid, &ecs->ws);
ab04a2af 3873
ce12b012 3874 if (bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
edcc5120
TT
3875 {
3876 /* A catchpoint triggered. */
94c57d6a
PA
3877 process_event_stop_test (ecs);
3878 return;
edcc5120 3879 }
488f131b 3880
b0f4b84b
DJ
3881 /* If requested, stop when the dynamic linker notifies
3882 gdb of events. This allows the user to get control
3883 and place breakpoints in initializer routines for
3884 dynamically loaded objects (among other things). */
a493e3e2 3885 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
b0f4b84b
DJ
3886 if (stop_on_solib_events)
3887 {
55409f9d
DJ
3888 /* Make sure we print "Stopped due to solib-event" in
3889 normal_stop. */
3890 stop_print_frame = 1;
3891
22bcd14b 3892 stop_waiting (ecs);
b0f4b84b
DJ
3893 return;
3894 }
488f131b 3895 }
b0f4b84b
DJ
3896
3897 /* If we are skipping through a shell, or through shared library
3898 loading that we aren't interested in, resume the program. If
5c09a2c5 3899 we're running the program normally, also resume. */
b0f4b84b
DJ
3900 if (stop_soon == STOP_QUIETLY || stop_soon == NO_STOP_QUIETLY)
3901 {
74960c60
VP
3902 /* Loading of shared libraries might have changed breakpoint
3903 addresses. Make sure new breakpoints are inserted. */
a25a5a45 3904 if (stop_soon == NO_STOP_QUIETLY)
74960c60 3905 insert_breakpoints ();
64ce06e4 3906 resume (GDB_SIGNAL_0);
b0f4b84b
DJ
3907 prepare_to_wait (ecs);
3908 return;
3909 }
3910
5c09a2c5
PA
3911 /* But stop if we're attaching or setting up a remote
3912 connection. */
3913 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
3914 || stop_soon == STOP_QUIETLY_REMOTE)
3915 {
3916 if (debug_infrun)
3917 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
22bcd14b 3918 stop_waiting (ecs);
5c09a2c5
PA
3919 return;
3920 }
3921
3922 internal_error (__FILE__, __LINE__,
3923 _("unhandled stop_soon: %d"), (int) stop_soon);
c5aa993b 3924
488f131b 3925 case TARGET_WAITKIND_SPURIOUS:
527159b7 3926 if (debug_infrun)
8a9de0e4 3927 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_SPURIOUS\n");
64776a0b 3928 if (!ptid_equal (ecs->ptid, inferior_ptid))
8b3ee56d 3929 context_switch (ecs->ptid);
64ce06e4 3930 resume (GDB_SIGNAL_0);
488f131b
JB
3931 prepare_to_wait (ecs);
3932 return;
c5aa993b 3933
488f131b 3934 case TARGET_WAITKIND_EXITED:
940c3c06 3935 case TARGET_WAITKIND_SIGNALLED:
527159b7 3936 if (debug_infrun)
940c3c06
PA
3937 {
3938 if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
3939 fprintf_unfiltered (gdb_stdlog,
3940 "infrun: TARGET_WAITKIND_EXITED\n");
3941 else
3942 fprintf_unfiltered (gdb_stdlog,
3943 "infrun: TARGET_WAITKIND_SIGNALLED\n");
3944 }
3945
fb66883a 3946 inferior_ptid = ecs->ptid;
c9657e70 3947 set_current_inferior (find_inferior_ptid (ecs->ptid));
6c95b8df
PA
3948 set_current_program_space (current_inferior ()->pspace);
3949 handle_vfork_child_exec_or_exit (0);
1777feb0 3950 target_terminal_ours (); /* Must do this before mourn anyway. */
488f131b 3951
0c557179
SDJ
3952 /* Clearing any previous state of convenience variables. */
3953 clear_exit_convenience_vars ();
3954
940c3c06
PA
3955 if (ecs->ws.kind == TARGET_WAITKIND_EXITED)
3956 {
3957 /* Record the exit code in the convenience variable $_exitcode, so
3958 that the user can inspect this again later. */
3959 set_internalvar_integer (lookup_internalvar ("_exitcode"),
3960 (LONGEST) ecs->ws.value.integer);
3961
3962 /* Also record this in the inferior itself. */
3963 current_inferior ()->has_exit_code = 1;
3964 current_inferior ()->exit_code = (LONGEST) ecs->ws.value.integer;
8cf64490 3965
98eb56a4
PA
3966 /* Support the --return-child-result option. */
3967 return_child_result_value = ecs->ws.value.integer;
3968
fd664c91 3969 observer_notify_exited (ecs->ws.value.integer);
940c3c06
PA
3970 }
3971 else
0c557179
SDJ
3972 {
3973 struct regcache *regcache = get_thread_regcache (ecs->ptid);
3974 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3975
3976 if (gdbarch_gdb_signal_to_target_p (gdbarch))
3977 {
3978 /* Set the value of the internal variable $_exitsignal,
3979 which holds the signal uncaught by the inferior. */
3980 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
3981 gdbarch_gdb_signal_to_target (gdbarch,
3982 ecs->ws.value.sig));
3983 }
3984 else
3985 {
3986 /* We don't have access to the target's method used for
3987 converting between signal numbers (GDB's internal
3988 representation <-> target's representation).
3989 Therefore, we cannot do a good job at displaying this
3990 information to the user. It's better to just warn
3991 her about it (if infrun debugging is enabled), and
3992 give up. */
3993 if (debug_infrun)
3994 fprintf_filtered (gdb_stdlog, _("\
3995Cannot fill $_exitsignal with the correct signal number.\n"));
3996 }
3997
fd664c91 3998 observer_notify_signal_exited (ecs->ws.value.sig);
0c557179 3999 }
8cf64490 4000
488f131b
JB
4001 gdb_flush (gdb_stdout);
4002 target_mourn_inferior ();
488f131b 4003 stop_print_frame = 0;
22bcd14b 4004 stop_waiting (ecs);
488f131b 4005 return;
c5aa993b 4006
488f131b 4007 /* The following are the only cases in which we keep going;
1777feb0 4008 the above cases end in a continue or goto. */
488f131b 4009 case TARGET_WAITKIND_FORKED:
deb3b17b 4010 case TARGET_WAITKIND_VFORKED:
527159b7 4011 if (debug_infrun)
fed708ed
PA
4012 {
4013 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
4014 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_FORKED\n");
4015 else
4016 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_VFORKED\n");
4017 }
c906108c 4018
e2d96639
YQ
4019 /* Check whether the inferior is displaced stepping. */
4020 {
4021 struct regcache *regcache = get_thread_regcache (ecs->ptid);
4022 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4023 struct displaced_step_inferior_state *displaced
4024 = get_displaced_stepping_state (ptid_get_pid (ecs->ptid));
4025
4026 /* If checking displaced stepping is supported, and thread
4027 ecs->ptid is displaced stepping. */
4028 if (displaced && ptid_equal (displaced->step_ptid, ecs->ptid))
4029 {
4030 struct inferior *parent_inf
c9657e70 4031 = find_inferior_ptid (ecs->ptid);
e2d96639
YQ
4032 struct regcache *child_regcache;
4033 CORE_ADDR parent_pc;
4034
4035 /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
4036 indicating that the displaced stepping of syscall instruction
4037 has been done. Perform cleanup for parent process here. Note
4038 that this operation also cleans up the child process for vfork,
4039 because their pages are shared. */
a493e3e2 4040 displaced_step_fixup (ecs->ptid, GDB_SIGNAL_TRAP);
c2829269
PA
4041 /* Start a new step-over in another thread if there's one
4042 that needs it. */
4043 start_step_over ();
e2d96639
YQ
4044
4045 if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
4046 {
4047 /* Restore scratch pad for child process. */
4048 displaced_step_restore (displaced, ecs->ws.value.related_pid);
4049 }
4050
4051 /* Since the vfork/fork syscall instruction was executed in the scratchpad,
4052 the child's PC is also within the scratchpad. Set the child's PC
4053 to the parent's PC value, which has already been fixed up.
4054 FIXME: we use the parent's aspace here, although we're touching
4055 the child, because the child hasn't been added to the inferior
4056 list yet at this point. */
4057
4058 child_regcache
4059 = get_thread_arch_aspace_regcache (ecs->ws.value.related_pid,
4060 gdbarch,
4061 parent_inf->aspace);
4062 /* Read PC value of parent process. */
4063 parent_pc = regcache_read_pc (regcache);
4064
4065 if (debug_displaced)
4066 fprintf_unfiltered (gdb_stdlog,
4067 "displaced: write child pc from %s to %s\n",
4068 paddress (gdbarch,
4069 regcache_read_pc (child_regcache)),
4070 paddress (gdbarch, parent_pc));
4071
4072 regcache_write_pc (child_regcache, parent_pc);
4073 }
4074 }
4075
5a2901d9 4076 if (!ptid_equal (ecs->ptid, inferior_ptid))
c3a01a22 4077 context_switch (ecs->ptid);
5a2901d9 4078
b242c3c2
PA
4079 /* Immediately detach breakpoints from the child before there's
4080 any chance of letting the user delete breakpoints from the
4081 breakpoint lists. If we don't do this early, it's easy to
4082 leave left over traps in the child, vis: "break foo; catch
4083 fork; c; <fork>; del; c; <child calls foo>". We only follow
4084 the fork on the last `continue', and by that time the
4085 breakpoint at "foo" is long gone from the breakpoint table.
4086 If we vforked, then we don't need to unpatch here, since both
4087 parent and child are sharing the same memory pages; we'll
4088 need to unpatch at follow/detach time instead to be certain
4089 that new breakpoints added between catchpoint hit time and
4090 vfork follow are detached. */
4091 if (ecs->ws.kind != TARGET_WAITKIND_VFORKED)
4092 {
b242c3c2
PA
4093 /* This won't actually modify the breakpoint list, but will
4094 physically remove the breakpoints from the child. */
d80ee84f 4095 detach_breakpoints (ecs->ws.value.related_pid);
b242c3c2
PA
4096 }
4097
34b7e8a6 4098 delete_just_stopped_threads_single_step_breakpoints ();
d03285ec 4099
e58b0e63
PA
4100 /* In case the event is caught by a catchpoint, remember that
4101 the event is to be followed at the next resume of the thread,
4102 and not immediately. */
4103 ecs->event_thread->pending_follow = ecs->ws;
4104
fb14de7b 4105 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
675bf4cb 4106
16c381f0 4107 ecs->event_thread->control.stop_bpstat
6c95b8df 4108 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
09ac7c10 4109 stop_pc, ecs->ptid, &ecs->ws);
675bf4cb 4110
ce12b012
PA
4111 /* If no catchpoint triggered for this, then keep going. Note
4112 that we're interested in knowing the bpstat actually causes a
4113 stop, not just if it may explain the signal. Software
4114 watchpoints, for example, always appear in the bpstat. */
4115 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
04e68871 4116 {
6c95b8df
PA
4117 ptid_t parent;
4118 ptid_t child;
e58b0e63 4119 int should_resume;
3e43a32a
MS
4120 int follow_child
4121 = (follow_fork_mode_string == follow_fork_mode_child);
e58b0e63 4122
a493e3e2 4123 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
e58b0e63
PA
4124
4125 should_resume = follow_fork ();
4126
6c95b8df
PA
4127 parent = ecs->ptid;
4128 child = ecs->ws.value.related_pid;
4129
4130 /* In non-stop mode, also resume the other branch. */
4131 if (non_stop && !detach_fork)
4132 {
4133 if (follow_child)
4134 switch_to_thread (parent);
4135 else
4136 switch_to_thread (child);
4137
4138 ecs->event_thread = inferior_thread ();
4139 ecs->ptid = inferior_ptid;
4140 keep_going (ecs);
4141 }
4142
4143 if (follow_child)
4144 switch_to_thread (child);
4145 else
4146 switch_to_thread (parent);
4147
e58b0e63
PA
4148 ecs->event_thread = inferior_thread ();
4149 ecs->ptid = inferior_ptid;
4150
4151 if (should_resume)
4152 keep_going (ecs);
4153 else
22bcd14b 4154 stop_waiting (ecs);
04e68871
DJ
4155 return;
4156 }
94c57d6a
PA
4157 process_event_stop_test (ecs);
4158 return;
488f131b 4159
6c95b8df
PA
4160 case TARGET_WAITKIND_VFORK_DONE:
4161 /* Done with the shared memory region. Re-insert breakpoints in
4162 the parent, and keep going. */
4163
4164 if (debug_infrun)
3e43a32a
MS
4165 fprintf_unfiltered (gdb_stdlog,
4166 "infrun: TARGET_WAITKIND_VFORK_DONE\n");
6c95b8df
PA
4167
4168 if (!ptid_equal (ecs->ptid, inferior_ptid))
4169 context_switch (ecs->ptid);
4170
4171 current_inferior ()->waiting_for_vfork_done = 0;
56710373 4172 current_inferior ()->pspace->breakpoints_not_allowed = 0;
6c95b8df
PA
4173 /* This also takes care of reinserting breakpoints in the
4174 previously locked inferior. */
4175 keep_going (ecs);
4176 return;
4177
488f131b 4178 case TARGET_WAITKIND_EXECD:
527159b7 4179 if (debug_infrun)
fc5261f2 4180 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_EXECD\n");
488f131b 4181
5a2901d9 4182 if (!ptid_equal (ecs->ptid, inferior_ptid))
c3a01a22 4183 context_switch (ecs->ptid);
5a2901d9 4184
fb14de7b 4185 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
795e548f 4186
6c95b8df
PA
4187 /* Do whatever is necessary to the parent branch of the vfork. */
4188 handle_vfork_child_exec_or_exit (1);
4189
795e548f
PA
4190 /* This causes the eventpoints and symbol table to be reset.
4191 Must do this now, before trying to determine whether to
4192 stop. */
71b43ef8 4193 follow_exec (inferior_ptid, ecs->ws.value.execd_pathname);
795e548f 4194
16c381f0 4195 ecs->event_thread->control.stop_bpstat
6c95b8df 4196 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
09ac7c10 4197 stop_pc, ecs->ptid, &ecs->ws);
795e548f 4198
71b43ef8
PA
4199 /* Note that this may be referenced from inside
4200 bpstat_stop_status above, through inferior_has_execd. */
4201 xfree (ecs->ws.value.execd_pathname);
4202 ecs->ws.value.execd_pathname = NULL;
4203
04e68871 4204 /* If no catchpoint triggered for this, then keep going. */
ce12b012 4205 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
04e68871 4206 {
a493e3e2 4207 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
04e68871
DJ
4208 keep_going (ecs);
4209 return;
4210 }
94c57d6a
PA
4211 process_event_stop_test (ecs);
4212 return;
488f131b 4213
b4dc5ffa
MK
4214 /* Be careful not to try to gather much state about a thread
4215 that's in a syscall. It's frequently a losing proposition. */
488f131b 4216 case TARGET_WAITKIND_SYSCALL_ENTRY:
527159b7 4217 if (debug_infrun)
3e43a32a
MS
4218 fprintf_unfiltered (gdb_stdlog,
4219 "infrun: TARGET_WAITKIND_SYSCALL_ENTRY\n");
1777feb0 4220 /* Getting the current syscall number. */
94c57d6a
PA
4221 if (handle_syscall_event (ecs) == 0)
4222 process_event_stop_test (ecs);
4223 return;
c906108c 4224
488f131b
JB
4225 /* Before examining the threads further, step this thread to
4226 get it entirely out of the syscall. (We get notice of the
4227 event when the thread is just on the verge of exiting a
4228 syscall. Stepping one instruction seems to get it back
b4dc5ffa 4229 into user code.) */
488f131b 4230 case TARGET_WAITKIND_SYSCALL_RETURN:
527159b7 4231 if (debug_infrun)
3e43a32a
MS
4232 fprintf_unfiltered (gdb_stdlog,
4233 "infrun: TARGET_WAITKIND_SYSCALL_RETURN\n");
94c57d6a
PA
4234 if (handle_syscall_event (ecs) == 0)
4235 process_event_stop_test (ecs);
4236 return;
c906108c 4237
488f131b 4238 case TARGET_WAITKIND_STOPPED:
527159b7 4239 if (debug_infrun)
8a9de0e4 4240 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_STOPPED\n");
16c381f0 4241 ecs->event_thread->suspend.stop_signal = ecs->ws.value.sig;
4f5d7f63
PA
4242 handle_signal_stop (ecs);
4243 return;
c906108c 4244
b2175913 4245 case TARGET_WAITKIND_NO_HISTORY:
4b4e080e
PA
4246 if (debug_infrun)
4247 fprintf_unfiltered (gdb_stdlog, "infrun: TARGET_WAITKIND_NO_HISTORY\n");
b2175913 4248 /* Reverse execution: target ran out of history info. */
eab402df 4249
34b7e8a6 4250 delete_just_stopped_threads_single_step_breakpoints ();
fb14de7b 4251 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
fd664c91 4252 observer_notify_no_history ();
22bcd14b 4253 stop_waiting (ecs);
b2175913 4254 return;
488f131b 4255 }
4f5d7f63
PA
4256}
4257
0b6e5e10
JB
4258/* A wrapper around handle_inferior_event_1, which also makes sure
4259 that all temporary struct value objects that were created during
4260 the handling of the event get deleted at the end. */
4261
4262static void
4263handle_inferior_event (struct execution_control_state *ecs)
4264{
4265 struct value *mark = value_mark ();
4266
4267 handle_inferior_event_1 (ecs);
4268 /* Purge all temporary values created during the event handling,
4269 as it could be a long time before we return to the command level
4270 where such values would otherwise be purged. */
4271 value_free_to_mark (mark);
4272}
4273
4f5d7f63
PA
4274/* Come here when the program has stopped with a signal. */
4275
4276static void
4277handle_signal_stop (struct execution_control_state *ecs)
4278{
4279 struct frame_info *frame;
4280 struct gdbarch *gdbarch;
4281 int stopped_by_watchpoint;
4282 enum stop_kind stop_soon;
4283 int random_signal;
c906108c 4284
f0407826
DE
4285 gdb_assert (ecs->ws.kind == TARGET_WAITKIND_STOPPED);
4286
4287 /* Do we need to clean up the state of a thread that has
4288 completed a displaced single-step? (Doing so usually affects
4289 the PC, so do it here, before we set stop_pc.) */
4290 displaced_step_fixup (ecs->ptid,
4291 ecs->event_thread->suspend.stop_signal);
c2829269 4292 start_step_over ();
f0407826
DE
4293
4294 /* If we either finished a single-step or hit a breakpoint, but
4295 the user wanted this thread to be stopped, pretend we got a
4296 SIG0 (generic unsignaled stop). */
4297 if (ecs->event_thread->stop_requested
4298 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
4299 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
237fc4c9 4300
515630c5 4301 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
488f131b 4302
527159b7 4303 if (debug_infrun)
237fc4c9 4304 {
5af949e3
UW
4305 struct regcache *regcache = get_thread_regcache (ecs->ptid);
4306 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7f82dfc7
JK
4307 struct cleanup *old_chain = save_inferior_ptid ();
4308
4309 inferior_ptid = ecs->ptid;
5af949e3
UW
4310
4311 fprintf_unfiltered (gdb_stdlog, "infrun: stop_pc = %s\n",
4312 paddress (gdbarch, stop_pc));
d92524f1 4313 if (target_stopped_by_watchpoint ())
237fc4c9
PA
4314 {
4315 CORE_ADDR addr;
abbb1732 4316
237fc4c9
PA
4317 fprintf_unfiltered (gdb_stdlog, "infrun: stopped by watchpoint\n");
4318
4319 if (target_stopped_data_address (&current_target, &addr))
4320 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
4321 "infrun: stopped data address = %s\n",
4322 paddress (gdbarch, addr));
237fc4c9
PA
4323 else
4324 fprintf_unfiltered (gdb_stdlog,
4325 "infrun: (no data address available)\n");
4326 }
7f82dfc7
JK
4327
4328 do_cleanups (old_chain);
237fc4c9 4329 }
527159b7 4330
36fa8042
PA
4331 /* This is originated from start_remote(), start_inferior() and
4332 shared libraries hook functions. */
4333 stop_soon = get_inferior_stop_soon (ecs->ptid);
4334 if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
4335 {
4336 if (!ptid_equal (ecs->ptid, inferior_ptid))
4337 context_switch (ecs->ptid);
4338 if (debug_infrun)
4339 fprintf_unfiltered (gdb_stdlog, "infrun: quietly stopped\n");
4340 stop_print_frame = 1;
22bcd14b 4341 stop_waiting (ecs);
36fa8042
PA
4342 return;
4343 }
4344
4345 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4346 && stop_after_trap)
4347 {
4348 if (!ptid_equal (ecs->ptid, inferior_ptid))
4349 context_switch (ecs->ptid);
4350 if (debug_infrun)
4351 fprintf_unfiltered (gdb_stdlog, "infrun: stopped\n");
4352 stop_print_frame = 0;
22bcd14b 4353 stop_waiting (ecs);
36fa8042
PA
4354 return;
4355 }
4356
4357 /* This originates from attach_command(). We need to overwrite
4358 the stop_signal here, because some kernels don't ignore a
4359 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
4360 See more comments in inferior.h. On the other hand, if we
4361 get a non-SIGSTOP, report it to the user - assume the backend
4362 will handle the SIGSTOP if it should show up later.
4363
4364 Also consider that the attach is complete when we see a
4365 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
4366 target extended-remote report it instead of a SIGSTOP
4367 (e.g. gdbserver). We already rely on SIGTRAP being our
4368 signal, so this is no exception.
4369
4370 Also consider that the attach is complete when we see a
4371 GDB_SIGNAL_0. In non-stop mode, GDB will explicitly tell
4372 the target to stop all threads of the inferior, in case the
4373 low level attach operation doesn't stop them implicitly. If
4374 they weren't stopped implicitly, then the stub will report a
4375 GDB_SIGNAL_0, meaning: stopped for no particular reason
4376 other than GDB's request. */
4377 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
4378 && (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_STOP
4379 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4380 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_0))
4381 {
4382 stop_print_frame = 1;
22bcd14b 4383 stop_waiting (ecs);
36fa8042
PA
4384 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
4385 return;
4386 }
4387
488f131b 4388 /* See if something interesting happened to the non-current thread. If
b40c7d58
DJ
4389 so, then switch to that thread. */
4390 if (!ptid_equal (ecs->ptid, inferior_ptid))
488f131b 4391 {
527159b7 4392 if (debug_infrun)
8a9de0e4 4393 fprintf_unfiltered (gdb_stdlog, "infrun: context switch\n");
527159b7 4394
0d1e5fa7 4395 context_switch (ecs->ptid);
c5aa993b 4396
9a4105ab
AC
4397 if (deprecated_context_hook)
4398 deprecated_context_hook (pid_to_thread_id (ecs->ptid));
488f131b 4399 }
c906108c 4400
568d6575
UW
4401 /* At this point, get hold of the now-current thread's frame. */
4402 frame = get_current_frame ();
4403 gdbarch = get_frame_arch (frame);
4404
2adfaa28 4405 /* Pull the single step breakpoints out of the target. */
af48d08f 4406 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
488f131b 4407 {
af48d08f
PA
4408 struct regcache *regcache;
4409 struct address_space *aspace;
4410 CORE_ADDR pc;
2adfaa28 4411
af48d08f
PA
4412 regcache = get_thread_regcache (ecs->ptid);
4413 aspace = get_regcache_aspace (regcache);
4414 pc = regcache_read_pc (regcache);
34b7e8a6 4415
af48d08f
PA
4416 /* However, before doing so, if this single-step breakpoint was
4417 actually for another thread, set this thread up for moving
4418 past it. */
4419 if (!thread_has_single_step_breakpoint_here (ecs->event_thread,
4420 aspace, pc))
4421 {
4422 if (single_step_breakpoint_inserted_here_p (aspace, pc))
2adfaa28
PA
4423 {
4424 if (debug_infrun)
4425 {
4426 fprintf_unfiltered (gdb_stdlog,
af48d08f 4427 "infrun: [%s] hit another thread's "
34b7e8a6
PA
4428 "single-step breakpoint\n",
4429 target_pid_to_str (ecs->ptid));
2adfaa28 4430 }
af48d08f
PA
4431 ecs->hit_singlestep_breakpoint = 1;
4432 }
4433 }
4434 else
4435 {
4436 if (debug_infrun)
4437 {
4438 fprintf_unfiltered (gdb_stdlog,
4439 "infrun: [%s] hit its "
4440 "single-step breakpoint\n",
4441 target_pid_to_str (ecs->ptid));
2adfaa28
PA
4442 }
4443 }
488f131b 4444 }
af48d08f 4445 delete_just_stopped_threads_single_step_breakpoints ();
c906108c 4446
963f9c80
PA
4447 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4448 && ecs->event_thread->control.trap_expected
4449 && ecs->event_thread->stepping_over_watchpoint)
d983da9c
DJ
4450 stopped_by_watchpoint = 0;
4451 else
4452 stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
4453
4454 /* If necessary, step over this watchpoint. We'll be back to display
4455 it in a moment. */
4456 if (stopped_by_watchpoint
d92524f1 4457 && (target_have_steppable_watchpoint
568d6575 4458 || gdbarch_have_nonsteppable_watchpoint (gdbarch)))
488f131b 4459 {
488f131b
JB
4460 /* At this point, we are stopped at an instruction which has
4461 attempted to write to a piece of memory under control of
4462 a watchpoint. The instruction hasn't actually executed
4463 yet. If we were to evaluate the watchpoint expression
4464 now, we would get the old value, and therefore no change
4465 would seem to have occurred.
4466
4467 In order to make watchpoints work `right', we really need
4468 to complete the memory write, and then evaluate the
d983da9c
DJ
4469 watchpoint expression. We do this by single-stepping the
4470 target.
4471
7f89fd65 4472 It may not be necessary to disable the watchpoint to step over
d983da9c
DJ
4473 it. For example, the PA can (with some kernel cooperation)
4474 single step over a watchpoint without disabling the watchpoint.
4475
4476 It is far more common to need to disable a watchpoint to step
4477 the inferior over it. If we have non-steppable watchpoints,
4478 we must disable the current watchpoint; it's simplest to
963f9c80
PA
4479 disable all watchpoints.
4480
4481 Any breakpoint at PC must also be stepped over -- if there's
4482 one, it will have already triggered before the watchpoint
4483 triggered, and we either already reported it to the user, or
4484 it didn't cause a stop and we called keep_going. In either
4485 case, if there was a breakpoint at PC, we must be trying to
4486 step past it. */
4487 ecs->event_thread->stepping_over_watchpoint = 1;
4488 keep_going (ecs);
488f131b
JB
4489 return;
4490 }
4491
4e1c45ea 4492 ecs->event_thread->stepping_over_breakpoint = 0;
963f9c80 4493 ecs->event_thread->stepping_over_watchpoint = 0;
16c381f0
JK
4494 bpstat_clear (&ecs->event_thread->control.stop_bpstat);
4495 ecs->event_thread->control.stop_step = 0;
488f131b 4496 stop_print_frame = 1;
488f131b 4497 stopped_by_random_signal = 0;
488f131b 4498
edb3359d
DJ
4499 /* Hide inlined functions starting here, unless we just performed stepi or
4500 nexti. After stepi and nexti, always show the innermost frame (not any
4501 inline function call sites). */
16c381f0 4502 if (ecs->event_thread->control.step_range_end != 1)
0574c78f
GB
4503 {
4504 struct address_space *aspace =
4505 get_regcache_aspace (get_thread_regcache (ecs->ptid));
4506
4507 /* skip_inline_frames is expensive, so we avoid it if we can
4508 determine that the address is one where functions cannot have
4509 been inlined. This improves performance with inferiors that
4510 load a lot of shared libraries, because the solib event
4511 breakpoint is defined as the address of a function (i.e. not
4512 inline). Note that we have to check the previous PC as well
4513 as the current one to catch cases when we have just
4514 single-stepped off a breakpoint prior to reinstating it.
4515 Note that we're assuming that the code we single-step to is
4516 not inline, but that's not definitive: there's nothing
4517 preventing the event breakpoint function from containing
4518 inlined code, and the single-step ending up there. If the
4519 user had set a breakpoint on that inlined code, the missing
4520 skip_inline_frames call would break things. Fortunately
4521 that's an extremely unlikely scenario. */
09ac7c10 4522 if (!pc_at_non_inline_function (aspace, stop_pc, &ecs->ws)
a210c238
MR
4523 && !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4524 && ecs->event_thread->control.trap_expected
4525 && pc_at_non_inline_function (aspace,
4526 ecs->event_thread->prev_pc,
09ac7c10 4527 &ecs->ws)))
1c5a993e
MR
4528 {
4529 skip_inline_frames (ecs->ptid);
4530
4531 /* Re-fetch current thread's frame in case that invalidated
4532 the frame cache. */
4533 frame = get_current_frame ();
4534 gdbarch = get_frame_arch (frame);
4535 }
0574c78f 4536 }
edb3359d 4537
a493e3e2 4538 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
16c381f0 4539 && ecs->event_thread->control.trap_expected
568d6575 4540 && gdbarch_single_step_through_delay_p (gdbarch)
4e1c45ea 4541 && currently_stepping (ecs->event_thread))
3352ef37 4542 {
b50d7442 4543 /* We're trying to step off a breakpoint. Turns out that we're
3352ef37 4544 also on an instruction that needs to be stepped multiple
1777feb0 4545 times before it's been fully executing. E.g., architectures
3352ef37
AC
4546 with a delay slot. It needs to be stepped twice, once for
4547 the instruction and once for the delay slot. */
4548 int step_through_delay
568d6575 4549 = gdbarch_single_step_through_delay (gdbarch, frame);
abbb1732 4550
527159b7 4551 if (debug_infrun && step_through_delay)
8a9de0e4 4552 fprintf_unfiltered (gdb_stdlog, "infrun: step through delay\n");
16c381f0
JK
4553 if (ecs->event_thread->control.step_range_end == 0
4554 && step_through_delay)
3352ef37
AC
4555 {
4556 /* The user issued a continue when stopped at a breakpoint.
4557 Set up for another trap and get out of here. */
4e1c45ea 4558 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
4559 keep_going (ecs);
4560 return;
4561 }
4562 else if (step_through_delay)
4563 {
4564 /* The user issued a step when stopped at a breakpoint.
4565 Maybe we should stop, maybe we should not - the delay
4566 slot *might* correspond to a line of source. In any
ca67fcb8
VP
4567 case, don't decide that here, just set
4568 ecs->stepping_over_breakpoint, making sure we
4569 single-step again before breakpoints are re-inserted. */
4e1c45ea 4570 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
4571 }
4572 }
4573
ab04a2af
TT
4574 /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
4575 handles this event. */
4576 ecs->event_thread->control.stop_bpstat
4577 = bpstat_stop_status (get_regcache_aspace (get_current_regcache ()),
4578 stop_pc, ecs->ptid, &ecs->ws);
db82e815 4579
ab04a2af
TT
4580 /* Following in case break condition called a
4581 function. */
4582 stop_print_frame = 1;
73dd234f 4583
ab04a2af
TT
4584 /* This is where we handle "moribund" watchpoints. Unlike
4585 software breakpoints traps, hardware watchpoint traps are
4586 always distinguishable from random traps. If no high-level
4587 watchpoint is associated with the reported stop data address
4588 anymore, then the bpstat does not explain the signal ---
4589 simply make sure to ignore it if `stopped_by_watchpoint' is
4590 set. */
4591
4592 if (debug_infrun
4593 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
47591c29 4594 && !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
427cd150 4595 GDB_SIGNAL_TRAP)
ab04a2af
TT
4596 && stopped_by_watchpoint)
4597 fprintf_unfiltered (gdb_stdlog,
4598 "infrun: no user watchpoint explains "
4599 "watchpoint SIGTRAP, ignoring\n");
73dd234f 4600
bac7d97b 4601 /* NOTE: cagney/2003-03-29: These checks for a random signal
ab04a2af
TT
4602 at one stage in the past included checks for an inferior
4603 function call's call dummy's return breakpoint. The original
4604 comment, that went with the test, read:
03cebad2 4605
ab04a2af
TT
4606 ``End of a stack dummy. Some systems (e.g. Sony news) give
4607 another signal besides SIGTRAP, so check here as well as
4608 above.''
73dd234f 4609
ab04a2af
TT
4610 If someone ever tries to get call dummys on a
4611 non-executable stack to work (where the target would stop
4612 with something like a SIGSEGV), then those tests might need
4613 to be re-instated. Given, however, that the tests were only
4614 enabled when momentary breakpoints were not being used, I
4615 suspect that it won't be the case.
488f131b 4616
ab04a2af
TT
4617 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
4618 be necessary for call dummies on a non-executable stack on
4619 SPARC. */
488f131b 4620
bac7d97b 4621 /* See if the breakpoints module can explain the signal. */
47591c29
PA
4622 random_signal
4623 = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
4624 ecs->event_thread->suspend.stop_signal);
bac7d97b 4625
1cf4d951
PA
4626 /* Maybe this was a trap for a software breakpoint that has since
4627 been removed. */
4628 if (random_signal && target_stopped_by_sw_breakpoint ())
4629 {
4630 if (program_breakpoint_here_p (gdbarch, stop_pc))
4631 {
4632 struct regcache *regcache;
4633 int decr_pc;
4634
4635 /* Re-adjust PC to what the program would see if GDB was not
4636 debugging it. */
4637 regcache = get_thread_regcache (ecs->event_thread->ptid);
527a273a 4638 decr_pc = gdbarch_decr_pc_after_break (gdbarch);
1cf4d951
PA
4639 if (decr_pc != 0)
4640 {
4641 struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
4642
4643 if (record_full_is_used ())
4644 record_full_gdb_operation_disable_set ();
4645
4646 regcache_write_pc (regcache, stop_pc + decr_pc);
4647
4648 do_cleanups (old_cleanups);
4649 }
4650 }
4651 else
4652 {
4653 /* A delayed software breakpoint event. Ignore the trap. */
4654 if (debug_infrun)
4655 fprintf_unfiltered (gdb_stdlog,
4656 "infrun: delayed software breakpoint "
4657 "trap, ignoring\n");
4658 random_signal = 0;
4659 }
4660 }
4661
4662 /* Maybe this was a trap for a hardware breakpoint/watchpoint that
4663 has since been removed. */
4664 if (random_signal && target_stopped_by_hw_breakpoint ())
4665 {
4666 /* A delayed hardware breakpoint event. Ignore the trap. */
4667 if (debug_infrun)
4668 fprintf_unfiltered (gdb_stdlog,
4669 "infrun: delayed hardware breakpoint/watchpoint "
4670 "trap, ignoring\n");
4671 random_signal = 0;
4672 }
4673
bac7d97b
PA
4674 /* If not, perhaps stepping/nexting can. */
4675 if (random_signal)
4676 random_signal = !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
4677 && currently_stepping (ecs->event_thread));
ab04a2af 4678
2adfaa28
PA
4679 /* Perhaps the thread hit a single-step breakpoint of _another_
4680 thread. Single-step breakpoints are transparent to the
4681 breakpoints module. */
4682 if (random_signal)
4683 random_signal = !ecs->hit_singlestep_breakpoint;
4684
bac7d97b
PA
4685 /* No? Perhaps we got a moribund watchpoint. */
4686 if (random_signal)
4687 random_signal = !stopped_by_watchpoint;
ab04a2af 4688
488f131b
JB
4689 /* For the program's own signals, act according to
4690 the signal handling tables. */
4691
ce12b012 4692 if (random_signal)
488f131b
JB
4693 {
4694 /* Signal not for debugging purposes. */
c9657e70 4695 struct inferior *inf = find_inferior_ptid (ecs->ptid);
c9737c08 4696 enum gdb_signal stop_signal = ecs->event_thread->suspend.stop_signal;
488f131b 4697
527159b7 4698 if (debug_infrun)
c9737c08
PA
4699 fprintf_unfiltered (gdb_stdlog, "infrun: random signal (%s)\n",
4700 gdb_signal_to_symbol_string (stop_signal));
527159b7 4701
488f131b
JB
4702 stopped_by_random_signal = 1;
4703
252fbfc8
PA
4704 /* Always stop on signals if we're either just gaining control
4705 of the program, or the user explicitly requested this thread
4706 to remain stopped. */
d6b48e9c 4707 if (stop_soon != NO_STOP_QUIETLY
252fbfc8 4708 || ecs->event_thread->stop_requested
24291992 4709 || (!inf->detaching
16c381f0 4710 && signal_stop_state (ecs->event_thread->suspend.stop_signal)))
488f131b 4711 {
22bcd14b 4712 stop_waiting (ecs);
488f131b
JB
4713 return;
4714 }
b57bacec
PA
4715
4716 /* Notify observers the signal has "handle print" set. Note we
4717 returned early above if stopping; normal_stop handles the
4718 printing in that case. */
4719 if (signal_print[ecs->event_thread->suspend.stop_signal])
4720 {
4721 /* The signal table tells us to print about this signal. */
4722 target_terminal_ours_for_output ();
4723 observer_notify_signal_received (ecs->event_thread->suspend.stop_signal);
4724 target_terminal_inferior ();
4725 }
488f131b
JB
4726
4727 /* Clear the signal if it should not be passed. */
16c381f0 4728 if (signal_program[ecs->event_thread->suspend.stop_signal] == 0)
a493e3e2 4729 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
488f131b 4730
fb14de7b 4731 if (ecs->event_thread->prev_pc == stop_pc
16c381f0 4732 && ecs->event_thread->control.trap_expected
8358c15c 4733 && ecs->event_thread->control.step_resume_breakpoint == NULL)
68f53502
AC
4734 {
4735 /* We were just starting a new sequence, attempting to
4736 single-step off of a breakpoint and expecting a SIGTRAP.
237fc4c9 4737 Instead this signal arrives. This signal will take us out
68f53502
AC
4738 of the stepping range so GDB needs to remember to, when
4739 the signal handler returns, resume stepping off that
4740 breakpoint. */
4741 /* To simplify things, "continue" is forced to use the same
4742 code paths as single-step - set a breakpoint at the
4743 signal return address and then, once hit, step off that
4744 breakpoint. */
237fc4c9
PA
4745 if (debug_infrun)
4746 fprintf_unfiltered (gdb_stdlog,
4747 "infrun: signal arrived while stepping over "
4748 "breakpoint\n");
d3169d93 4749
2c03e5be 4750 insert_hp_step_resume_breakpoint_at_frame (frame);
4e1c45ea 4751 ecs->event_thread->step_after_step_resume_breakpoint = 1;
2455069d
UW
4752 /* Reset trap_expected to ensure breakpoints are re-inserted. */
4753 ecs->event_thread->control.trap_expected = 0;
d137e6dc
PA
4754
4755 /* If we were nexting/stepping some other thread, switch to
4756 it, so that we don't continue it, losing control. */
4757 if (!switch_back_to_stepped_thread (ecs))
4758 keep_going (ecs);
9d799f85 4759 return;
68f53502 4760 }
9d799f85 4761
e5f8a7cc
PA
4762 if (ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_0
4763 && (pc_in_thread_step_range (stop_pc, ecs->event_thread)
4764 || ecs->event_thread->control.step_range_end == 1)
edb3359d 4765 && frame_id_eq (get_stack_frame_id (frame),
16c381f0 4766 ecs->event_thread->control.step_stack_frame_id)
8358c15c 4767 && ecs->event_thread->control.step_resume_breakpoint == NULL)
d303a6c7
AC
4768 {
4769 /* The inferior is about to take a signal that will take it
4770 out of the single step range. Set a breakpoint at the
4771 current PC (which is presumably where the signal handler
4772 will eventually return) and then allow the inferior to
4773 run free.
4774
4775 Note that this is only needed for a signal delivered
4776 while in the single-step range. Nested signals aren't a
4777 problem as they eventually all return. */
237fc4c9
PA
4778 if (debug_infrun)
4779 fprintf_unfiltered (gdb_stdlog,
4780 "infrun: signal may take us out of "
4781 "single-step range\n");
4782
2c03e5be 4783 insert_hp_step_resume_breakpoint_at_frame (frame);
e5f8a7cc 4784 ecs->event_thread->step_after_step_resume_breakpoint = 1;
2455069d
UW
4785 /* Reset trap_expected to ensure breakpoints are re-inserted. */
4786 ecs->event_thread->control.trap_expected = 0;
9d799f85
AC
4787 keep_going (ecs);
4788 return;
d303a6c7 4789 }
9d799f85
AC
4790
4791 /* Note: step_resume_breakpoint may be non-NULL. This occures
4792 when either there's a nested signal, or when there's a
4793 pending signal enabled just as the signal handler returns
4794 (leaving the inferior at the step-resume-breakpoint without
4795 actually executing it). Either way continue until the
4796 breakpoint is really hit. */
c447ac0b
PA
4797
4798 if (!switch_back_to_stepped_thread (ecs))
4799 {
4800 if (debug_infrun)
4801 fprintf_unfiltered (gdb_stdlog,
4802 "infrun: random signal, keep going\n");
4803
4804 keep_going (ecs);
4805 }
4806 return;
488f131b 4807 }
94c57d6a
PA
4808
4809 process_event_stop_test (ecs);
4810}
4811
4812/* Come here when we've got some debug event / signal we can explain
4813 (IOW, not a random signal), and test whether it should cause a
4814 stop, or whether we should resume the inferior (transparently).
4815 E.g., could be a breakpoint whose condition evaluates false; we
4816 could be still stepping within the line; etc. */
4817
4818static void
4819process_event_stop_test (struct execution_control_state *ecs)
4820{
4821 struct symtab_and_line stop_pc_sal;
4822 struct frame_info *frame;
4823 struct gdbarch *gdbarch;
cdaa5b73
PA
4824 CORE_ADDR jmp_buf_pc;
4825 struct bpstat_what what;
94c57d6a 4826
cdaa5b73 4827 /* Handle cases caused by hitting a breakpoint. */
611c83ae 4828
cdaa5b73
PA
4829 frame = get_current_frame ();
4830 gdbarch = get_frame_arch (frame);
fcf3daef 4831
cdaa5b73 4832 what = bpstat_what (ecs->event_thread->control.stop_bpstat);
611c83ae 4833
cdaa5b73
PA
4834 if (what.call_dummy)
4835 {
4836 stop_stack_dummy = what.call_dummy;
4837 }
186c406b 4838
cdaa5b73
PA
4839 /* If we hit an internal event that triggers symbol changes, the
4840 current frame will be invalidated within bpstat_what (e.g., if we
4841 hit an internal solib event). Re-fetch it. */
4842 frame = get_current_frame ();
4843 gdbarch = get_frame_arch (frame);
e2e4d78b 4844
cdaa5b73
PA
4845 switch (what.main_action)
4846 {
4847 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
4848 /* If we hit the breakpoint at longjmp while stepping, we
4849 install a momentary breakpoint at the target of the
4850 jmp_buf. */
186c406b 4851
cdaa5b73
PA
4852 if (debug_infrun)
4853 fprintf_unfiltered (gdb_stdlog,
4854 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME\n");
186c406b 4855
cdaa5b73 4856 ecs->event_thread->stepping_over_breakpoint = 1;
611c83ae 4857
cdaa5b73
PA
4858 if (what.is_longjmp)
4859 {
4860 struct value *arg_value;
4861
4862 /* If we set the longjmp breakpoint via a SystemTap probe,
4863 then use it to extract the arguments. The destination PC
4864 is the third argument to the probe. */
4865 arg_value = probe_safe_evaluate_at_pc (frame, 2);
4866 if (arg_value)
8fa0c4f8
AA
4867 {
4868 jmp_buf_pc = value_as_address (arg_value);
4869 jmp_buf_pc = gdbarch_addr_bits_remove (gdbarch, jmp_buf_pc);
4870 }
cdaa5b73
PA
4871 else if (!gdbarch_get_longjmp_target_p (gdbarch)
4872 || !gdbarch_get_longjmp_target (gdbarch,
4873 frame, &jmp_buf_pc))
e2e4d78b 4874 {
cdaa5b73
PA
4875 if (debug_infrun)
4876 fprintf_unfiltered (gdb_stdlog,
4877 "infrun: BPSTAT_WHAT_SET_LONGJMP_RESUME "
4878 "(!gdbarch_get_longjmp_target)\n");
4879 keep_going (ecs);
4880 return;
e2e4d78b 4881 }
e2e4d78b 4882
cdaa5b73
PA
4883 /* Insert a breakpoint at resume address. */
4884 insert_longjmp_resume_breakpoint (gdbarch, jmp_buf_pc);
4885 }
4886 else
4887 check_exception_resume (ecs, frame);
4888 keep_going (ecs);
4889 return;
e81a37f7 4890
cdaa5b73
PA
4891 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
4892 {
4893 struct frame_info *init_frame;
e81a37f7 4894
cdaa5b73 4895 /* There are several cases to consider.
c906108c 4896
cdaa5b73
PA
4897 1. The initiating frame no longer exists. In this case we
4898 must stop, because the exception or longjmp has gone too
4899 far.
2c03e5be 4900
cdaa5b73
PA
4901 2. The initiating frame exists, and is the same as the
4902 current frame. We stop, because the exception or longjmp
4903 has been caught.
2c03e5be 4904
cdaa5b73
PA
4905 3. The initiating frame exists and is different from the
4906 current frame. This means the exception or longjmp has
4907 been caught beneath the initiating frame, so keep going.
c906108c 4908
cdaa5b73
PA
4909 4. longjmp breakpoint has been placed just to protect
4910 against stale dummy frames and user is not interested in
4911 stopping around longjmps. */
c5aa993b 4912
cdaa5b73
PA
4913 if (debug_infrun)
4914 fprintf_unfiltered (gdb_stdlog,
4915 "infrun: BPSTAT_WHAT_CLEAR_LONGJMP_RESUME\n");
c5aa993b 4916
cdaa5b73
PA
4917 gdb_assert (ecs->event_thread->control.exception_resume_breakpoint
4918 != NULL);
4919 delete_exception_resume_breakpoint (ecs->event_thread);
c5aa993b 4920
cdaa5b73
PA
4921 if (what.is_longjmp)
4922 {
b67a2c6f 4923 check_longjmp_breakpoint_for_call_dummy (ecs->event_thread);
c5aa993b 4924
cdaa5b73 4925 if (!frame_id_p (ecs->event_thread->initiating_frame))
e5ef252a 4926 {
cdaa5b73
PA
4927 /* Case 4. */
4928 keep_going (ecs);
4929 return;
e5ef252a 4930 }
cdaa5b73 4931 }
c5aa993b 4932
cdaa5b73 4933 init_frame = frame_find_by_id (ecs->event_thread->initiating_frame);
527159b7 4934
cdaa5b73
PA
4935 if (init_frame)
4936 {
4937 struct frame_id current_id
4938 = get_frame_id (get_current_frame ());
4939 if (frame_id_eq (current_id,
4940 ecs->event_thread->initiating_frame))
4941 {
4942 /* Case 2. Fall through. */
4943 }
4944 else
4945 {
4946 /* Case 3. */
4947 keep_going (ecs);
4948 return;
4949 }
68f53502 4950 }
488f131b 4951
cdaa5b73
PA
4952 /* For Cases 1 and 2, remove the step-resume breakpoint, if it
4953 exists. */
4954 delete_step_resume_breakpoint (ecs->event_thread);
e5ef252a 4955
bdc36728 4956 end_stepping_range (ecs);
cdaa5b73
PA
4957 }
4958 return;
e5ef252a 4959
cdaa5b73
PA
4960 case BPSTAT_WHAT_SINGLE:
4961 if (debug_infrun)
4962 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_SINGLE\n");
4963 ecs->event_thread->stepping_over_breakpoint = 1;
4964 /* Still need to check other stuff, at least the case where we
4965 are stepping and step out of the right range. */
4966 break;
e5ef252a 4967
cdaa5b73
PA
4968 case BPSTAT_WHAT_STEP_RESUME:
4969 if (debug_infrun)
4970 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STEP_RESUME\n");
e5ef252a 4971
cdaa5b73
PA
4972 delete_step_resume_breakpoint (ecs->event_thread);
4973 if (ecs->event_thread->control.proceed_to_finish
4974 && execution_direction == EXEC_REVERSE)
4975 {
4976 struct thread_info *tp = ecs->event_thread;
4977
4978 /* We are finishing a function in reverse, and just hit the
4979 step-resume breakpoint at the start address of the
4980 function, and we're almost there -- just need to back up
4981 by one more single-step, which should take us back to the
4982 function call. */
4983 tp->control.step_range_start = tp->control.step_range_end = 1;
4984 keep_going (ecs);
e5ef252a 4985 return;
cdaa5b73
PA
4986 }
4987 fill_in_stop_func (gdbarch, ecs);
4988 if (stop_pc == ecs->stop_func_start
4989 && execution_direction == EXEC_REVERSE)
4990 {
4991 /* We are stepping over a function call in reverse, and just
4992 hit the step-resume breakpoint at the start address of
4993 the function. Go back to single-stepping, which should
4994 take us back to the function call. */
4995 ecs->event_thread->stepping_over_breakpoint = 1;
4996 keep_going (ecs);
4997 return;
4998 }
4999 break;
e5ef252a 5000
cdaa5b73
PA
5001 case BPSTAT_WHAT_STOP_NOISY:
5002 if (debug_infrun)
5003 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_NOISY\n");
5004 stop_print_frame = 1;
e5ef252a 5005
99619bea
PA
5006 /* Assume the thread stopped for a breapoint. We'll still check
5007 whether a/the breakpoint is there when the thread is next
5008 resumed. */
5009 ecs->event_thread->stepping_over_breakpoint = 1;
e5ef252a 5010
22bcd14b 5011 stop_waiting (ecs);
cdaa5b73 5012 return;
e5ef252a 5013
cdaa5b73
PA
5014 case BPSTAT_WHAT_STOP_SILENT:
5015 if (debug_infrun)
5016 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_STOP_SILENT\n");
5017 stop_print_frame = 0;
e5ef252a 5018
99619bea
PA
5019 /* Assume the thread stopped for a breapoint. We'll still check
5020 whether a/the breakpoint is there when the thread is next
5021 resumed. */
5022 ecs->event_thread->stepping_over_breakpoint = 1;
22bcd14b 5023 stop_waiting (ecs);
cdaa5b73
PA
5024 return;
5025
5026 case BPSTAT_WHAT_HP_STEP_RESUME:
5027 if (debug_infrun)
5028 fprintf_unfiltered (gdb_stdlog, "infrun: BPSTAT_WHAT_HP_STEP_RESUME\n");
5029
5030 delete_step_resume_breakpoint (ecs->event_thread);
5031 if (ecs->event_thread->step_after_step_resume_breakpoint)
5032 {
5033 /* Back when the step-resume breakpoint was inserted, we
5034 were trying to single-step off a breakpoint. Go back to
5035 doing that. */
5036 ecs->event_thread->step_after_step_resume_breakpoint = 0;
5037 ecs->event_thread->stepping_over_breakpoint = 1;
5038 keep_going (ecs);
5039 return;
e5ef252a 5040 }
cdaa5b73
PA
5041 break;
5042
5043 case BPSTAT_WHAT_KEEP_CHECKING:
5044 break;
e5ef252a 5045 }
c906108c 5046
af48d08f
PA
5047 /* If we stepped a permanent breakpoint and we had a high priority
5048 step-resume breakpoint for the address we stepped, but we didn't
5049 hit it, then we must have stepped into the signal handler. The
5050 step-resume was only necessary to catch the case of _not_
5051 stepping into the handler, so delete it, and fall through to
5052 checking whether the step finished. */
5053 if (ecs->event_thread->stepped_breakpoint)
5054 {
5055 struct breakpoint *sr_bp
5056 = ecs->event_thread->control.step_resume_breakpoint;
5057
8d707a12
PA
5058 if (sr_bp != NULL
5059 && sr_bp->loc->permanent
af48d08f
PA
5060 && sr_bp->type == bp_hp_step_resume
5061 && sr_bp->loc->address == ecs->event_thread->prev_pc)
5062 {
5063 if (debug_infrun)
5064 fprintf_unfiltered (gdb_stdlog,
5065 "infrun: stepped permanent breakpoint, stopped in "
5066 "handler\n");
5067 delete_step_resume_breakpoint (ecs->event_thread);
5068 ecs->event_thread->step_after_step_resume_breakpoint = 0;
5069 }
5070 }
5071
cdaa5b73
PA
5072 /* We come here if we hit a breakpoint but should not stop for it.
5073 Possibly we also were stepping and should stop for that. So fall
5074 through and test for stepping. But, if not stepping, do not
5075 stop. */
c906108c 5076
a7212384
UW
5077 /* In all-stop mode, if we're currently stepping but have stopped in
5078 some other thread, we need to switch back to the stepped thread. */
c447ac0b
PA
5079 if (switch_back_to_stepped_thread (ecs))
5080 return;
776f04fa 5081
8358c15c 5082 if (ecs->event_thread->control.step_resume_breakpoint)
488f131b 5083 {
527159b7 5084 if (debug_infrun)
d3169d93
DJ
5085 fprintf_unfiltered (gdb_stdlog,
5086 "infrun: step-resume breakpoint is inserted\n");
527159b7 5087
488f131b
JB
5088 /* Having a step-resume breakpoint overrides anything
5089 else having to do with stepping commands until
5090 that breakpoint is reached. */
488f131b
JB
5091 keep_going (ecs);
5092 return;
5093 }
c5aa993b 5094
16c381f0 5095 if (ecs->event_thread->control.step_range_end == 0)
488f131b 5096 {
527159b7 5097 if (debug_infrun)
8a9de0e4 5098 fprintf_unfiltered (gdb_stdlog, "infrun: no stepping, continue\n");
488f131b 5099 /* Likewise if we aren't even stepping. */
488f131b
JB
5100 keep_going (ecs);
5101 return;
5102 }
c5aa993b 5103
4b7703ad
JB
5104 /* Re-fetch current thread's frame in case the code above caused
5105 the frame cache to be re-initialized, making our FRAME variable
5106 a dangling pointer. */
5107 frame = get_current_frame ();
628fe4e4 5108 gdbarch = get_frame_arch (frame);
7e324e48 5109 fill_in_stop_func (gdbarch, ecs);
4b7703ad 5110
488f131b 5111 /* If stepping through a line, keep going if still within it.
c906108c 5112
488f131b
JB
5113 Note that step_range_end is the address of the first instruction
5114 beyond the step range, and NOT the address of the last instruction
31410e84
MS
5115 within it!
5116
5117 Note also that during reverse execution, we may be stepping
5118 through a function epilogue and therefore must detect when
5119 the current-frame changes in the middle of a line. */
5120
ce4c476a 5121 if (pc_in_thread_step_range (stop_pc, ecs->event_thread)
31410e84 5122 && (execution_direction != EXEC_REVERSE
388a8562 5123 || frame_id_eq (get_frame_id (frame),
16c381f0 5124 ecs->event_thread->control.step_frame_id)))
488f131b 5125 {
527159b7 5126 if (debug_infrun)
5af949e3
UW
5127 fprintf_unfiltered
5128 (gdb_stdlog, "infrun: stepping inside range [%s-%s]\n",
16c381f0
JK
5129 paddress (gdbarch, ecs->event_thread->control.step_range_start),
5130 paddress (gdbarch, ecs->event_thread->control.step_range_end));
b2175913 5131
c1e36e3e
PA
5132 /* Tentatively re-enable range stepping; `resume' disables it if
5133 necessary (e.g., if we're stepping over a breakpoint or we
5134 have software watchpoints). */
5135 ecs->event_thread->control.may_range_step = 1;
5136
b2175913
MS
5137 /* When stepping backward, stop at beginning of line range
5138 (unless it's the function entry point, in which case
5139 keep going back to the call point). */
16c381f0 5140 if (stop_pc == ecs->event_thread->control.step_range_start
b2175913
MS
5141 && stop_pc != ecs->stop_func_start
5142 && execution_direction == EXEC_REVERSE)
bdc36728 5143 end_stepping_range (ecs);
b2175913
MS
5144 else
5145 keep_going (ecs);
5146
488f131b
JB
5147 return;
5148 }
c5aa993b 5149
488f131b 5150 /* We stepped out of the stepping range. */
c906108c 5151
488f131b 5152 /* If we are stepping at the source level and entered the runtime
388a8562
MS
5153 loader dynamic symbol resolution code...
5154
5155 EXEC_FORWARD: we keep on single stepping until we exit the run
5156 time loader code and reach the callee's address.
5157
5158 EXEC_REVERSE: we've already executed the callee (backward), and
5159 the runtime loader code is handled just like any other
5160 undebuggable function call. Now we need only keep stepping
5161 backward through the trampoline code, and that's handled further
5162 down, so there is nothing for us to do here. */
5163
5164 if (execution_direction != EXEC_REVERSE
16c381f0 5165 && ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
cfd8ab24 5166 && in_solib_dynsym_resolve_code (stop_pc))
488f131b 5167 {
4c8c40e6 5168 CORE_ADDR pc_after_resolver =
568d6575 5169 gdbarch_skip_solib_resolver (gdbarch, stop_pc);
c906108c 5170
527159b7 5171 if (debug_infrun)
3e43a32a
MS
5172 fprintf_unfiltered (gdb_stdlog,
5173 "infrun: stepped into dynsym resolve code\n");
527159b7 5174
488f131b
JB
5175 if (pc_after_resolver)
5176 {
5177 /* Set up a step-resume breakpoint at the address
5178 indicated by SKIP_SOLIB_RESOLVER. */
5179 struct symtab_and_line sr_sal;
abbb1732 5180
fe39c653 5181 init_sal (&sr_sal);
488f131b 5182 sr_sal.pc = pc_after_resolver;
6c95b8df 5183 sr_sal.pspace = get_frame_program_space (frame);
488f131b 5184
a6d9a66e
UW
5185 insert_step_resume_breakpoint_at_sal (gdbarch,
5186 sr_sal, null_frame_id);
c5aa993b 5187 }
c906108c 5188
488f131b
JB
5189 keep_going (ecs);
5190 return;
5191 }
c906108c 5192
16c381f0
JK
5193 if (ecs->event_thread->control.step_range_end != 1
5194 && (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
5195 || ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
568d6575 5196 && get_frame_type (frame) == SIGTRAMP_FRAME)
488f131b 5197 {
527159b7 5198 if (debug_infrun)
3e43a32a
MS
5199 fprintf_unfiltered (gdb_stdlog,
5200 "infrun: stepped into signal trampoline\n");
42edda50 5201 /* The inferior, while doing a "step" or "next", has ended up in
8fb3e588
AC
5202 a signal trampoline (either by a signal being delivered or by
5203 the signal handler returning). Just single-step until the
5204 inferior leaves the trampoline (either by calling the handler
5205 or returning). */
488f131b
JB
5206 keep_going (ecs);
5207 return;
5208 }
c906108c 5209
14132e89
MR
5210 /* If we're in the return path from a shared library trampoline,
5211 we want to proceed through the trampoline when stepping. */
5212 /* macro/2012-04-25: This needs to come before the subroutine
5213 call check below as on some targets return trampolines look
5214 like subroutine calls (MIPS16 return thunks). */
5215 if (gdbarch_in_solib_return_trampoline (gdbarch,
5216 stop_pc, ecs->stop_func_name)
5217 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
5218 {
5219 /* Determine where this trampoline returns. */
5220 CORE_ADDR real_stop_pc;
5221
5222 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
5223
5224 if (debug_infrun)
5225 fprintf_unfiltered (gdb_stdlog,
5226 "infrun: stepped into solib return tramp\n");
5227
5228 /* Only proceed through if we know where it's going. */
5229 if (real_stop_pc)
5230 {
5231 /* And put the step-breakpoint there and go until there. */
5232 struct symtab_and_line sr_sal;
5233
5234 init_sal (&sr_sal); /* initialize to zeroes */
5235 sr_sal.pc = real_stop_pc;
5236 sr_sal.section = find_pc_overlay (sr_sal.pc);
5237 sr_sal.pspace = get_frame_program_space (frame);
5238
5239 /* Do not specify what the fp should be when we stop since
5240 on some machines the prologue is where the new fp value
5241 is established. */
5242 insert_step_resume_breakpoint_at_sal (gdbarch,
5243 sr_sal, null_frame_id);
5244
5245 /* Restart without fiddling with the step ranges or
5246 other state. */
5247 keep_going (ecs);
5248 return;
5249 }
5250 }
5251
c17eaafe
DJ
5252 /* Check for subroutine calls. The check for the current frame
5253 equalling the step ID is not necessary - the check of the
5254 previous frame's ID is sufficient - but it is a common case and
5255 cheaper than checking the previous frame's ID.
14e60db5
DJ
5256
5257 NOTE: frame_id_eq will never report two invalid frame IDs as
5258 being equal, so to get into this block, both the current and
5259 previous frame must have valid frame IDs. */
005ca36a
JB
5260 /* The outer_frame_id check is a heuristic to detect stepping
5261 through startup code. If we step over an instruction which
5262 sets the stack pointer from an invalid value to a valid value,
5263 we may detect that as a subroutine call from the mythical
5264 "outermost" function. This could be fixed by marking
5265 outermost frames as !stack_p,code_p,special_p. Then the
5266 initial outermost frame, before sp was valid, would
ce6cca6d 5267 have code_addr == &_start. See the comment in frame_id_eq
005ca36a 5268 for more. */
edb3359d 5269 if (!frame_id_eq (get_stack_frame_id (frame),
16c381f0 5270 ecs->event_thread->control.step_stack_frame_id)
005ca36a 5271 && (frame_id_eq (frame_unwind_caller_id (get_current_frame ()),
16c381f0
JK
5272 ecs->event_thread->control.step_stack_frame_id)
5273 && (!frame_id_eq (ecs->event_thread->control.step_stack_frame_id,
005ca36a 5274 outer_frame_id)
885eeb5b
PA
5275 || (ecs->event_thread->control.step_start_function
5276 != find_pc_function (stop_pc)))))
488f131b 5277 {
95918acb 5278 CORE_ADDR real_stop_pc;
8fb3e588 5279
527159b7 5280 if (debug_infrun)
8a9de0e4 5281 fprintf_unfiltered (gdb_stdlog, "infrun: stepped into subroutine\n");
527159b7 5282
b7a084be 5283 if (ecs->event_thread->control.step_over_calls == STEP_OVER_NONE)
95918acb
AC
5284 {
5285 /* I presume that step_over_calls is only 0 when we're
5286 supposed to be stepping at the assembly language level
5287 ("stepi"). Just stop. */
388a8562 5288 /* And this works the same backward as frontward. MVS */
bdc36728 5289 end_stepping_range (ecs);
95918acb
AC
5290 return;
5291 }
8fb3e588 5292
388a8562
MS
5293 /* Reverse stepping through solib trampolines. */
5294
5295 if (execution_direction == EXEC_REVERSE
16c381f0 5296 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
388a8562
MS
5297 && (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
5298 || (ecs->stop_func_start == 0
5299 && in_solib_dynsym_resolve_code (stop_pc))))
5300 {
5301 /* Any solib trampoline code can be handled in reverse
5302 by simply continuing to single-step. We have already
5303 executed the solib function (backwards), and a few
5304 steps will take us back through the trampoline to the
5305 caller. */
5306 keep_going (ecs);
5307 return;
5308 }
5309
16c381f0 5310 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
8567c30f 5311 {
b2175913
MS
5312 /* We're doing a "next".
5313
5314 Normal (forward) execution: set a breakpoint at the
5315 callee's return address (the address at which the caller
5316 will resume).
5317
5318 Reverse (backward) execution. set the step-resume
5319 breakpoint at the start of the function that we just
5320 stepped into (backwards), and continue to there. When we
6130d0b7 5321 get there, we'll need to single-step back to the caller. */
b2175913
MS
5322
5323 if (execution_direction == EXEC_REVERSE)
5324 {
acf9414f
JK
5325 /* If we're already at the start of the function, we've either
5326 just stepped backward into a single instruction function,
5327 or stepped back out of a signal handler to the first instruction
5328 of the function. Just keep going, which will single-step back
5329 to the caller. */
58c48e72 5330 if (ecs->stop_func_start != stop_pc && ecs->stop_func_start != 0)
acf9414f
JK
5331 {
5332 struct symtab_and_line sr_sal;
5333
5334 /* Normal function call return (static or dynamic). */
5335 init_sal (&sr_sal);
5336 sr_sal.pc = ecs->stop_func_start;
5337 sr_sal.pspace = get_frame_program_space (frame);
5338 insert_step_resume_breakpoint_at_sal (gdbarch,
5339 sr_sal, null_frame_id);
5340 }
b2175913
MS
5341 }
5342 else
568d6575 5343 insert_step_resume_breakpoint_at_caller (frame);
b2175913 5344
8567c30f
AC
5345 keep_going (ecs);
5346 return;
5347 }
a53c66de 5348
95918acb 5349 /* If we are in a function call trampoline (a stub between the
8fb3e588
AC
5350 calling routine and the real function), locate the real
5351 function. That's what tells us (a) whether we want to step
5352 into it at all, and (b) what prologue we want to run to the
5353 end of, if we do step into it. */
568d6575 5354 real_stop_pc = skip_language_trampoline (frame, stop_pc);
95918acb 5355 if (real_stop_pc == 0)
568d6575 5356 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
95918acb
AC
5357 if (real_stop_pc != 0)
5358 ecs->stop_func_start = real_stop_pc;
8fb3e588 5359
db5f024e 5360 if (real_stop_pc != 0 && in_solib_dynsym_resolve_code (real_stop_pc))
1b2bfbb9
RC
5361 {
5362 struct symtab_and_line sr_sal;
abbb1732 5363
1b2bfbb9
RC
5364 init_sal (&sr_sal);
5365 sr_sal.pc = ecs->stop_func_start;
6c95b8df 5366 sr_sal.pspace = get_frame_program_space (frame);
1b2bfbb9 5367
a6d9a66e
UW
5368 insert_step_resume_breakpoint_at_sal (gdbarch,
5369 sr_sal, null_frame_id);
8fb3e588
AC
5370 keep_going (ecs);
5371 return;
1b2bfbb9
RC
5372 }
5373
95918acb 5374 /* If we have line number information for the function we are
1bfeeb0f
JL
5375 thinking of stepping into and the function isn't on the skip
5376 list, step into it.
95918acb 5377
8fb3e588
AC
5378 If there are several symtabs at that PC (e.g. with include
5379 files), just want to know whether *any* of them have line
5380 numbers. find_pc_line handles this. */
95918acb
AC
5381 {
5382 struct symtab_and_line tmp_sal;
8fb3e588 5383
95918acb 5384 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
2b914b52 5385 if (tmp_sal.line != 0
85817405
JK
5386 && !function_name_is_marked_for_skip (ecs->stop_func_name,
5387 &tmp_sal))
95918acb 5388 {
b2175913 5389 if (execution_direction == EXEC_REVERSE)
568d6575 5390 handle_step_into_function_backward (gdbarch, ecs);
b2175913 5391 else
568d6575 5392 handle_step_into_function (gdbarch, ecs);
95918acb
AC
5393 return;
5394 }
5395 }
5396
5397 /* If we have no line number and the step-stop-if-no-debug is
8fb3e588
AC
5398 set, we stop the step so that the user has a chance to switch
5399 in assembly mode. */
16c381f0 5400 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
078130d0 5401 && step_stop_if_no_debug)
95918acb 5402 {
bdc36728 5403 end_stepping_range (ecs);
95918acb
AC
5404 return;
5405 }
5406
b2175913
MS
5407 if (execution_direction == EXEC_REVERSE)
5408 {
acf9414f
JK
5409 /* If we're already at the start of the function, we've either just
5410 stepped backward into a single instruction function without line
5411 number info, or stepped back out of a signal handler to the first
5412 instruction of the function without line number info. Just keep
5413 going, which will single-step back to the caller. */
5414 if (ecs->stop_func_start != stop_pc)
5415 {
5416 /* Set a breakpoint at callee's start address.
5417 From there we can step once and be back in the caller. */
5418 struct symtab_and_line sr_sal;
abbb1732 5419
acf9414f
JK
5420 init_sal (&sr_sal);
5421 sr_sal.pc = ecs->stop_func_start;
5422 sr_sal.pspace = get_frame_program_space (frame);
5423 insert_step_resume_breakpoint_at_sal (gdbarch,
5424 sr_sal, null_frame_id);
5425 }
b2175913
MS
5426 }
5427 else
5428 /* Set a breakpoint at callee's return address (the address
5429 at which the caller will resume). */
568d6575 5430 insert_step_resume_breakpoint_at_caller (frame);
b2175913 5431
95918acb 5432 keep_going (ecs);
488f131b 5433 return;
488f131b 5434 }
c906108c 5435
fdd654f3
MS
5436 /* Reverse stepping through solib trampolines. */
5437
5438 if (execution_direction == EXEC_REVERSE
16c381f0 5439 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
fdd654f3
MS
5440 {
5441 if (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
5442 || (ecs->stop_func_start == 0
5443 && in_solib_dynsym_resolve_code (stop_pc)))
5444 {
5445 /* Any solib trampoline code can be handled in reverse
5446 by simply continuing to single-step. We have already
5447 executed the solib function (backwards), and a few
5448 steps will take us back through the trampoline to the
5449 caller. */
5450 keep_going (ecs);
5451 return;
5452 }
5453 else if (in_solib_dynsym_resolve_code (stop_pc))
5454 {
5455 /* Stepped backward into the solib dynsym resolver.
5456 Set a breakpoint at its start and continue, then
5457 one more step will take us out. */
5458 struct symtab_and_line sr_sal;
abbb1732 5459
fdd654f3
MS
5460 init_sal (&sr_sal);
5461 sr_sal.pc = ecs->stop_func_start;
9d1807c3 5462 sr_sal.pspace = get_frame_program_space (frame);
fdd654f3
MS
5463 insert_step_resume_breakpoint_at_sal (gdbarch,
5464 sr_sal, null_frame_id);
5465 keep_going (ecs);
5466 return;
5467 }
5468 }
5469
2afb61aa 5470 stop_pc_sal = find_pc_line (stop_pc, 0);
7ed0fe66 5471
1b2bfbb9
RC
5472 /* NOTE: tausq/2004-05-24: This if block used to be done before all
5473 the trampoline processing logic, however, there are some trampolines
5474 that have no names, so we should do trampoline handling first. */
16c381f0 5475 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
7ed0fe66 5476 && ecs->stop_func_name == NULL
2afb61aa 5477 && stop_pc_sal.line == 0)
1b2bfbb9 5478 {
527159b7 5479 if (debug_infrun)
3e43a32a
MS
5480 fprintf_unfiltered (gdb_stdlog,
5481 "infrun: stepped into undebuggable function\n");
527159b7 5482
1b2bfbb9 5483 /* The inferior just stepped into, or returned to, an
7ed0fe66
DJ
5484 undebuggable function (where there is no debugging information
5485 and no line number corresponding to the address where the
1b2bfbb9
RC
5486 inferior stopped). Since we want to skip this kind of code,
5487 we keep going until the inferior returns from this
14e60db5
DJ
5488 function - unless the user has asked us not to (via
5489 set step-mode) or we no longer know how to get back
5490 to the call site. */
5491 if (step_stop_if_no_debug
c7ce8faa 5492 || !frame_id_p (frame_unwind_caller_id (frame)))
1b2bfbb9
RC
5493 {
5494 /* If we have no line number and the step-stop-if-no-debug
5495 is set, we stop the step so that the user has a chance to
5496 switch in assembly mode. */
bdc36728 5497 end_stepping_range (ecs);
1b2bfbb9
RC
5498 return;
5499 }
5500 else
5501 {
5502 /* Set a breakpoint at callee's return address (the address
5503 at which the caller will resume). */
568d6575 5504 insert_step_resume_breakpoint_at_caller (frame);
1b2bfbb9
RC
5505 keep_going (ecs);
5506 return;
5507 }
5508 }
5509
16c381f0 5510 if (ecs->event_thread->control.step_range_end == 1)
1b2bfbb9
RC
5511 {
5512 /* It is stepi or nexti. We always want to stop stepping after
5513 one instruction. */
527159b7 5514 if (debug_infrun)
8a9de0e4 5515 fprintf_unfiltered (gdb_stdlog, "infrun: stepi/nexti\n");
bdc36728 5516 end_stepping_range (ecs);
1b2bfbb9
RC
5517 return;
5518 }
5519
2afb61aa 5520 if (stop_pc_sal.line == 0)
488f131b
JB
5521 {
5522 /* We have no line number information. That means to stop
5523 stepping (does this always happen right after one instruction,
5524 when we do "s" in a function with no line numbers,
5525 or can this happen as a result of a return or longjmp?). */
527159b7 5526 if (debug_infrun)
8a9de0e4 5527 fprintf_unfiltered (gdb_stdlog, "infrun: no line number info\n");
bdc36728 5528 end_stepping_range (ecs);
488f131b
JB
5529 return;
5530 }
c906108c 5531
edb3359d
DJ
5532 /* Look for "calls" to inlined functions, part one. If the inline
5533 frame machinery detected some skipped call sites, we have entered
5534 a new inline function. */
5535
5536 if (frame_id_eq (get_frame_id (get_current_frame ()),
16c381f0 5537 ecs->event_thread->control.step_frame_id)
edb3359d
DJ
5538 && inline_skipped_frames (ecs->ptid))
5539 {
5540 struct symtab_and_line call_sal;
5541
5542 if (debug_infrun)
5543 fprintf_unfiltered (gdb_stdlog,
5544 "infrun: stepped into inlined function\n");
5545
5546 find_frame_sal (get_current_frame (), &call_sal);
5547
16c381f0 5548 if (ecs->event_thread->control.step_over_calls != STEP_OVER_ALL)
edb3359d
DJ
5549 {
5550 /* For "step", we're going to stop. But if the call site
5551 for this inlined function is on the same source line as
5552 we were previously stepping, go down into the function
5553 first. Otherwise stop at the call site. */
5554
5555 if (call_sal.line == ecs->event_thread->current_line
5556 && call_sal.symtab == ecs->event_thread->current_symtab)
5557 step_into_inline_frame (ecs->ptid);
5558
bdc36728 5559 end_stepping_range (ecs);
edb3359d
DJ
5560 return;
5561 }
5562 else
5563 {
5564 /* For "next", we should stop at the call site if it is on a
5565 different source line. Otherwise continue through the
5566 inlined function. */
5567 if (call_sal.line == ecs->event_thread->current_line
5568 && call_sal.symtab == ecs->event_thread->current_symtab)
5569 keep_going (ecs);
5570 else
bdc36728 5571 end_stepping_range (ecs);
edb3359d
DJ
5572 return;
5573 }
5574 }
5575
5576 /* Look for "calls" to inlined functions, part two. If we are still
5577 in the same real function we were stepping through, but we have
5578 to go further up to find the exact frame ID, we are stepping
5579 through a more inlined call beyond its call site. */
5580
5581 if (get_frame_type (get_current_frame ()) == INLINE_FRAME
5582 && !frame_id_eq (get_frame_id (get_current_frame ()),
16c381f0 5583 ecs->event_thread->control.step_frame_id)
edb3359d 5584 && stepped_in_from (get_current_frame (),
16c381f0 5585 ecs->event_thread->control.step_frame_id))
edb3359d
DJ
5586 {
5587 if (debug_infrun)
5588 fprintf_unfiltered (gdb_stdlog,
5589 "infrun: stepping through inlined function\n");
5590
16c381f0 5591 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
edb3359d
DJ
5592 keep_going (ecs);
5593 else
bdc36728 5594 end_stepping_range (ecs);
edb3359d
DJ
5595 return;
5596 }
5597
2afb61aa 5598 if ((stop_pc == stop_pc_sal.pc)
4e1c45ea
PA
5599 && (ecs->event_thread->current_line != stop_pc_sal.line
5600 || ecs->event_thread->current_symtab != stop_pc_sal.symtab))
488f131b
JB
5601 {
5602 /* We are at the start of a different line. So stop. Note that
5603 we don't stop if we step into the middle of a different line.
5604 That is said to make things like for (;;) statements work
5605 better. */
527159b7 5606 if (debug_infrun)
3e43a32a
MS
5607 fprintf_unfiltered (gdb_stdlog,
5608 "infrun: stepped to a different line\n");
bdc36728 5609 end_stepping_range (ecs);
488f131b
JB
5610 return;
5611 }
c906108c 5612
488f131b 5613 /* We aren't done stepping.
c906108c 5614
488f131b
JB
5615 Optimize by setting the stepping range to the line.
5616 (We might not be in the original line, but if we entered a
5617 new line in mid-statement, we continue stepping. This makes
5618 things like for(;;) statements work better.) */
c906108c 5619
16c381f0
JK
5620 ecs->event_thread->control.step_range_start = stop_pc_sal.pc;
5621 ecs->event_thread->control.step_range_end = stop_pc_sal.end;
c1e36e3e 5622 ecs->event_thread->control.may_range_step = 1;
edb3359d 5623 set_step_info (frame, stop_pc_sal);
488f131b 5624
527159b7 5625 if (debug_infrun)
8a9de0e4 5626 fprintf_unfiltered (gdb_stdlog, "infrun: keep going\n");
488f131b 5627 keep_going (ecs);
104c1213
JM
5628}
5629
c447ac0b
PA
5630/* In all-stop mode, if we're currently stepping but have stopped in
5631 some other thread, we may need to switch back to the stepped
5632 thread. Returns true we set the inferior running, false if we left
5633 it stopped (and the event needs further processing). */
5634
5635static int
5636switch_back_to_stepped_thread (struct execution_control_state *ecs)
5637{
5638 if (!non_stop)
5639 {
5640 struct thread_info *tp;
99619bea 5641 struct thread_info *stepping_thread;
483805cf 5642 struct thread_info *step_over;
99619bea
PA
5643
5644 /* If any thread is blocked on some internal breakpoint, and we
5645 simply need to step over that breakpoint to get it going
5646 again, do that first. */
5647
5648 /* However, if we see an event for the stepping thread, then we
5649 know all other threads have been moved past their breakpoints
5650 already. Let the caller check whether the step is finished,
5651 etc., before deciding to move it past a breakpoint. */
5652 if (ecs->event_thread->control.step_range_end != 0)
5653 return 0;
5654
5655 /* Check if the current thread is blocked on an incomplete
5656 step-over, interrupted by a random signal. */
5657 if (ecs->event_thread->control.trap_expected
5658 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
c447ac0b 5659 {
99619bea
PA
5660 if (debug_infrun)
5661 {
5662 fprintf_unfiltered (gdb_stdlog,
5663 "infrun: need to finish step-over of [%s]\n",
5664 target_pid_to_str (ecs->event_thread->ptid));
5665 }
5666 keep_going (ecs);
5667 return 1;
5668 }
2adfaa28 5669
99619bea
PA
5670 /* Check if the current thread is blocked by a single-step
5671 breakpoint of another thread. */
5672 if (ecs->hit_singlestep_breakpoint)
5673 {
5674 if (debug_infrun)
5675 {
5676 fprintf_unfiltered (gdb_stdlog,
5677 "infrun: need to step [%s] over single-step "
5678 "breakpoint\n",
5679 target_pid_to_str (ecs->ptid));
5680 }
5681 keep_going (ecs);
5682 return 1;
5683 }
5684
483805cf
PA
5685 /* Otherwise, we no longer expect a trap in the current thread.
5686 Clear the trap_expected flag before switching back -- this is
5687 what keep_going does as well, if we call it. */
5688 ecs->event_thread->control.trap_expected = 0;
5689
70509625
PA
5690 /* Likewise, clear the signal if it should not be passed. */
5691 if (!signal_program[ecs->event_thread->suspend.stop_signal])
5692 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
5693
483805cf
PA
5694 /* If scheduler locking applies even if not stepping, there's no
5695 need to walk over threads. Above we've checked whether the
5696 current thread is stepping. If some other thread not the
5697 event thread is stepping, then it must be that scheduler
5698 locking is not in effect. */
856e7dd6 5699 if (schedlock_applies (ecs->event_thread))
483805cf
PA
5700 return 0;
5701
5702 /* Look for the stepping/nexting thread, and check if any other
5703 thread other than the stepping thread needs to start a
5704 step-over. Do all step-overs before actually proceeding with
5705 step/next/etc. */
5706 stepping_thread = NULL;
5707 step_over = NULL;
034f788c 5708 ALL_NON_EXITED_THREADS (tp)
483805cf
PA
5709 {
5710 /* Ignore threads of processes we're not resuming. */
5711 if (!sched_multi
5712 && ptid_get_pid (tp->ptid) != ptid_get_pid (inferior_ptid))
5713 continue;
5714
5715 /* When stepping over a breakpoint, we lock all threads
5716 except the one that needs to move past the breakpoint.
5717 If a non-event thread has this set, the "incomplete
5718 step-over" check above should have caught it earlier. */
5719 gdb_assert (!tp->control.trap_expected);
5720
5721 /* Did we find the stepping thread? */
5722 if (tp->control.step_range_end)
5723 {
5724 /* Yep. There should only one though. */
5725 gdb_assert (stepping_thread == NULL);
5726
5727 /* The event thread is handled at the top, before we
5728 enter this loop. */
5729 gdb_assert (tp != ecs->event_thread);
5730
5731 /* If some thread other than the event thread is
5732 stepping, then scheduler locking can't be in effect,
5733 otherwise we wouldn't have resumed the current event
5734 thread in the first place. */
856e7dd6 5735 gdb_assert (!schedlock_applies (tp));
483805cf
PA
5736
5737 stepping_thread = tp;
5738 }
5739 else if (thread_still_needs_step_over (tp))
5740 {
5741 step_over = tp;
5742
5743 /* At the top we've returned early if the event thread
5744 is stepping. If some other thread not the event
5745 thread is stepping, then scheduler locking can't be
5746 in effect, and we can resume this thread. No need to
5747 keep looking for the stepping thread then. */
5748 break;
5749 }
5750 }
99619bea 5751
483805cf 5752 if (step_over != NULL)
99619bea 5753 {
483805cf 5754 tp = step_over;
99619bea 5755 if (debug_infrun)
c447ac0b 5756 {
99619bea
PA
5757 fprintf_unfiltered (gdb_stdlog,
5758 "infrun: need to step-over [%s]\n",
5759 target_pid_to_str (tp->ptid));
c447ac0b
PA
5760 }
5761
483805cf 5762 /* Only the stepping thread should have this set. */
99619bea
PA
5763 gdb_assert (tp->control.step_range_end == 0);
5764
99619bea
PA
5765 ecs->ptid = tp->ptid;
5766 ecs->event_thread = tp;
5767 switch_to_thread (ecs->ptid);
5768 keep_going (ecs);
5769 return 1;
5770 }
5771
483805cf 5772 if (stepping_thread != NULL)
99619bea
PA
5773 {
5774 struct frame_info *frame;
5775 struct gdbarch *gdbarch;
5776
483805cf
PA
5777 tp = stepping_thread;
5778
c447ac0b
PA
5779 /* If the stepping thread exited, then don't try to switch
5780 back and resume it, which could fail in several different
5781 ways depending on the target. Instead, just keep going.
5782
5783 We can find a stepping dead thread in the thread list in
5784 two cases:
5785
5786 - The target supports thread exit events, and when the
5787 target tries to delete the thread from the thread list,
5788 inferior_ptid pointed at the exiting thread. In such
5789 case, calling delete_thread does not really remove the
5790 thread from the list; instead, the thread is left listed,
5791 with 'exited' state.
5792
5793 - The target's debug interface does not support thread
5794 exit events, and so we have no idea whatsoever if the
5795 previously stepping thread is still alive. For that
5796 reason, we need to synchronously query the target
5797 now. */
5798 if (is_exited (tp->ptid)
5799 || !target_thread_alive (tp->ptid))
5800 {
5801 if (debug_infrun)
5802 fprintf_unfiltered (gdb_stdlog,
5803 "infrun: not switching back to "
5804 "stepped thread, it has vanished\n");
5805
5806 delete_thread (tp->ptid);
5807 keep_going (ecs);
5808 return 1;
5809 }
5810
c447ac0b
PA
5811 if (debug_infrun)
5812 fprintf_unfiltered (gdb_stdlog,
5813 "infrun: switching back to stepped thread\n");
5814
5815 ecs->event_thread = tp;
5816 ecs->ptid = tp->ptid;
5817 context_switch (ecs->ptid);
2adfaa28
PA
5818
5819 stop_pc = regcache_read_pc (get_thread_regcache (ecs->ptid));
5820 frame = get_current_frame ();
5821 gdbarch = get_frame_arch (frame);
5822
5823 /* If the PC of the thread we were trying to single-step has
99619bea
PA
5824 changed, then that thread has trapped or been signaled,
5825 but the event has not been reported to GDB yet. Re-poll
5826 the target looking for this particular thread's event
5827 (i.e. temporarily enable schedlock) by:
2adfaa28
PA
5828
5829 - setting a break at the current PC
5830 - resuming that particular thread, only (by setting
5831 trap expected)
5832
5833 This prevents us continuously moving the single-step
5834 breakpoint forward, one instruction at a time,
5835 overstepping. */
5836
af48d08f 5837 if (stop_pc != tp->prev_pc)
2adfaa28 5838 {
64ce06e4
PA
5839 ptid_t resume_ptid;
5840
2adfaa28
PA
5841 if (debug_infrun)
5842 fprintf_unfiltered (gdb_stdlog,
5843 "infrun: expected thread advanced also\n");
5844
7c16b83e
PA
5845 /* Clear the info of the previous step-over, as it's no
5846 longer valid. It's what keep_going would do too, if
5847 we called it. Must do this before trying to insert
5848 the sss breakpoint, otherwise if we were previously
5849 trying to step over this exact address in another
5850 thread, the breakpoint ends up not installed. */
5851 clear_step_over_info ();
5852
2adfaa28
PA
5853 insert_single_step_breakpoint (get_frame_arch (frame),
5854 get_frame_address_space (frame),
5855 stop_pc);
2adfaa28 5856
64ce06e4
PA
5857 resume_ptid = user_visible_resume_ptid (tp->control.stepping_command);
5858 do_target_resume (resume_ptid,
5859 currently_stepping (tp), GDB_SIGNAL_0);
2adfaa28
PA
5860 prepare_to_wait (ecs);
5861 }
5862 else
5863 {
5864 if (debug_infrun)
5865 fprintf_unfiltered (gdb_stdlog,
5866 "infrun: expected thread still "
5867 "hasn't advanced\n");
5868 keep_going (ecs);
5869 }
5870
c447ac0b
PA
5871 return 1;
5872 }
5873 }
5874 return 0;
5875}
5876
b3444185 5877/* Is thread TP in the middle of single-stepping? */
104c1213 5878
a289b8f6 5879static int
b3444185 5880currently_stepping (struct thread_info *tp)
a7212384 5881{
8358c15c
JK
5882 return ((tp->control.step_range_end
5883 && tp->control.step_resume_breakpoint == NULL)
5884 || tp->control.trap_expected
af48d08f 5885 || tp->stepped_breakpoint
8358c15c 5886 || bpstat_should_step ());
a7212384
UW
5887}
5888
b2175913
MS
5889/* Inferior has stepped into a subroutine call with source code that
5890 we should not step over. Do step to the first line of code in
5891 it. */
c2c6d25f
JM
5892
5893static void
568d6575
UW
5894handle_step_into_function (struct gdbarch *gdbarch,
5895 struct execution_control_state *ecs)
c2c6d25f 5896{
43f3e411 5897 struct compunit_symtab *cust;
2afb61aa 5898 struct symtab_and_line stop_func_sal, sr_sal;
c2c6d25f 5899
7e324e48
GB
5900 fill_in_stop_func (gdbarch, ecs);
5901
43f3e411
DE
5902 cust = find_pc_compunit_symtab (stop_pc);
5903 if (cust != NULL && compunit_language (cust) != language_asm)
568d6575 5904 ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
b2175913 5905 ecs->stop_func_start);
c2c6d25f 5906
2afb61aa 5907 stop_func_sal = find_pc_line (ecs->stop_func_start, 0);
c2c6d25f
JM
5908 /* Use the step_resume_break to step until the end of the prologue,
5909 even if that involves jumps (as it seems to on the vax under
5910 4.2). */
5911 /* If the prologue ends in the middle of a source line, continue to
5912 the end of that source line (if it is still within the function).
5913 Otherwise, just go to end of prologue. */
2afb61aa
PA
5914 if (stop_func_sal.end
5915 && stop_func_sal.pc != ecs->stop_func_start
5916 && stop_func_sal.end < ecs->stop_func_end)
5917 ecs->stop_func_start = stop_func_sal.end;
c2c6d25f 5918
2dbd5e30
KB
5919 /* Architectures which require breakpoint adjustment might not be able
5920 to place a breakpoint at the computed address. If so, the test
5921 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
5922 ecs->stop_func_start to an address at which a breakpoint may be
5923 legitimately placed.
8fb3e588 5924
2dbd5e30
KB
5925 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
5926 made, GDB will enter an infinite loop when stepping through
5927 optimized code consisting of VLIW instructions which contain
5928 subinstructions corresponding to different source lines. On
5929 FR-V, it's not permitted to place a breakpoint on any but the
5930 first subinstruction of a VLIW instruction. When a breakpoint is
5931 set, GDB will adjust the breakpoint address to the beginning of
5932 the VLIW instruction. Thus, we need to make the corresponding
5933 adjustment here when computing the stop address. */
8fb3e588 5934
568d6575 5935 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
2dbd5e30
KB
5936 {
5937 ecs->stop_func_start
568d6575 5938 = gdbarch_adjust_breakpoint_address (gdbarch,
8fb3e588 5939 ecs->stop_func_start);
2dbd5e30
KB
5940 }
5941
c2c6d25f
JM
5942 if (ecs->stop_func_start == stop_pc)
5943 {
5944 /* We are already there: stop now. */
bdc36728 5945 end_stepping_range (ecs);
c2c6d25f
JM
5946 return;
5947 }
5948 else
5949 {
5950 /* Put the step-breakpoint there and go until there. */
fe39c653 5951 init_sal (&sr_sal); /* initialize to zeroes */
c2c6d25f
JM
5952 sr_sal.pc = ecs->stop_func_start;
5953 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
6c95b8df 5954 sr_sal.pspace = get_frame_program_space (get_current_frame ());
44cbf7b5 5955
c2c6d25f 5956 /* Do not specify what the fp should be when we stop since on
488f131b
JB
5957 some machines the prologue is where the new fp value is
5958 established. */
a6d9a66e 5959 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal, null_frame_id);
c2c6d25f
JM
5960
5961 /* And make sure stepping stops right away then. */
16c381f0
JK
5962 ecs->event_thread->control.step_range_end
5963 = ecs->event_thread->control.step_range_start;
c2c6d25f
JM
5964 }
5965 keep_going (ecs);
5966}
d4f3574e 5967
b2175913
MS
5968/* Inferior has stepped backward into a subroutine call with source
5969 code that we should not step over. Do step to the beginning of the
5970 last line of code in it. */
5971
5972static void
568d6575
UW
5973handle_step_into_function_backward (struct gdbarch *gdbarch,
5974 struct execution_control_state *ecs)
b2175913 5975{
43f3e411 5976 struct compunit_symtab *cust;
167e4384 5977 struct symtab_and_line stop_func_sal;
b2175913 5978
7e324e48
GB
5979 fill_in_stop_func (gdbarch, ecs);
5980
43f3e411
DE
5981 cust = find_pc_compunit_symtab (stop_pc);
5982 if (cust != NULL && compunit_language (cust) != language_asm)
568d6575 5983 ecs->stop_func_start = gdbarch_skip_prologue (gdbarch,
b2175913
MS
5984 ecs->stop_func_start);
5985
5986 stop_func_sal = find_pc_line (stop_pc, 0);
5987
5988 /* OK, we're just going to keep stepping here. */
5989 if (stop_func_sal.pc == stop_pc)
5990 {
5991 /* We're there already. Just stop stepping now. */
bdc36728 5992 end_stepping_range (ecs);
b2175913
MS
5993 }
5994 else
5995 {
5996 /* Else just reset the step range and keep going.
5997 No step-resume breakpoint, they don't work for
5998 epilogues, which can have multiple entry paths. */
16c381f0
JK
5999 ecs->event_thread->control.step_range_start = stop_func_sal.pc;
6000 ecs->event_thread->control.step_range_end = stop_func_sal.end;
b2175913
MS
6001 keep_going (ecs);
6002 }
6003 return;
6004}
6005
d3169d93 6006/* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
44cbf7b5
AC
6007 This is used to both functions and to skip over code. */
6008
6009static void
2c03e5be
PA
6010insert_step_resume_breakpoint_at_sal_1 (struct gdbarch *gdbarch,
6011 struct symtab_and_line sr_sal,
6012 struct frame_id sr_id,
6013 enum bptype sr_type)
44cbf7b5 6014{
611c83ae
PA
6015 /* There should never be more than one step-resume or longjmp-resume
6016 breakpoint per thread, so we should never be setting a new
44cbf7b5 6017 step_resume_breakpoint when one is already active. */
8358c15c 6018 gdb_assert (inferior_thread ()->control.step_resume_breakpoint == NULL);
2c03e5be 6019 gdb_assert (sr_type == bp_step_resume || sr_type == bp_hp_step_resume);
d3169d93
DJ
6020
6021 if (debug_infrun)
6022 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
6023 "infrun: inserting step-resume breakpoint at %s\n",
6024 paddress (gdbarch, sr_sal.pc));
d3169d93 6025
8358c15c 6026 inferior_thread ()->control.step_resume_breakpoint
2c03e5be
PA
6027 = set_momentary_breakpoint (gdbarch, sr_sal, sr_id, sr_type);
6028}
6029
9da8c2a0 6030void
2c03e5be
PA
6031insert_step_resume_breakpoint_at_sal (struct gdbarch *gdbarch,
6032 struct symtab_and_line sr_sal,
6033 struct frame_id sr_id)
6034{
6035 insert_step_resume_breakpoint_at_sal_1 (gdbarch,
6036 sr_sal, sr_id,
6037 bp_step_resume);
44cbf7b5 6038}
7ce450bd 6039
2c03e5be
PA
6040/* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
6041 This is used to skip a potential signal handler.
7ce450bd 6042
14e60db5
DJ
6043 This is called with the interrupted function's frame. The signal
6044 handler, when it returns, will resume the interrupted function at
6045 RETURN_FRAME.pc. */
d303a6c7
AC
6046
6047static void
2c03e5be 6048insert_hp_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
d303a6c7
AC
6049{
6050 struct symtab_and_line sr_sal;
a6d9a66e 6051 struct gdbarch *gdbarch;
d303a6c7 6052
f4c1edd8 6053 gdb_assert (return_frame != NULL);
d303a6c7
AC
6054 init_sal (&sr_sal); /* initialize to zeros */
6055
a6d9a66e 6056 gdbarch = get_frame_arch (return_frame);
568d6575 6057 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch, get_frame_pc (return_frame));
d303a6c7 6058 sr_sal.section = find_pc_overlay (sr_sal.pc);
6c95b8df 6059 sr_sal.pspace = get_frame_program_space (return_frame);
d303a6c7 6060
2c03e5be
PA
6061 insert_step_resume_breakpoint_at_sal_1 (gdbarch, sr_sal,
6062 get_stack_frame_id (return_frame),
6063 bp_hp_step_resume);
d303a6c7
AC
6064}
6065
2c03e5be
PA
6066/* Insert a "step-resume breakpoint" at the previous frame's PC. This
6067 is used to skip a function after stepping into it (for "next" or if
6068 the called function has no debugging information).
14e60db5
DJ
6069
6070 The current function has almost always been reached by single
6071 stepping a call or return instruction. NEXT_FRAME belongs to the
6072 current function, and the breakpoint will be set at the caller's
6073 resume address.
6074
6075 This is a separate function rather than reusing
2c03e5be 6076 insert_hp_step_resume_breakpoint_at_frame in order to avoid
14e60db5 6077 get_prev_frame, which may stop prematurely (see the implementation
c7ce8faa 6078 of frame_unwind_caller_id for an example). */
14e60db5
DJ
6079
6080static void
6081insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
6082{
6083 struct symtab_and_line sr_sal;
a6d9a66e 6084 struct gdbarch *gdbarch;
14e60db5
DJ
6085
6086 /* We shouldn't have gotten here if we don't know where the call site
6087 is. */
c7ce8faa 6088 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame)));
14e60db5
DJ
6089
6090 init_sal (&sr_sal); /* initialize to zeros */
6091
a6d9a66e 6092 gdbarch = frame_unwind_caller_arch (next_frame);
c7ce8faa
DJ
6093 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch,
6094 frame_unwind_caller_pc (next_frame));
14e60db5 6095 sr_sal.section = find_pc_overlay (sr_sal.pc);
6c95b8df 6096 sr_sal.pspace = frame_unwind_program_space (next_frame);
14e60db5 6097
a6d9a66e 6098 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal,
c7ce8faa 6099 frame_unwind_caller_id (next_frame));
14e60db5
DJ
6100}
6101
611c83ae
PA
6102/* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
6103 new breakpoint at the target of a jmp_buf. The handling of
6104 longjmp-resume uses the same mechanisms used for handling
6105 "step-resume" breakpoints. */
6106
6107static void
a6d9a66e 6108insert_longjmp_resume_breakpoint (struct gdbarch *gdbarch, CORE_ADDR pc)
611c83ae 6109{
e81a37f7
TT
6110 /* There should never be more than one longjmp-resume breakpoint per
6111 thread, so we should never be setting a new
611c83ae 6112 longjmp_resume_breakpoint when one is already active. */
e81a37f7 6113 gdb_assert (inferior_thread ()->control.exception_resume_breakpoint == NULL);
611c83ae
PA
6114
6115 if (debug_infrun)
6116 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
6117 "infrun: inserting longjmp-resume breakpoint at %s\n",
6118 paddress (gdbarch, pc));
611c83ae 6119
e81a37f7 6120 inferior_thread ()->control.exception_resume_breakpoint =
a6d9a66e 6121 set_momentary_breakpoint_at_pc (gdbarch, pc, bp_longjmp_resume);
611c83ae
PA
6122}
6123
186c406b
TT
6124/* Insert an exception resume breakpoint. TP is the thread throwing
6125 the exception. The block B is the block of the unwinder debug hook
6126 function. FRAME is the frame corresponding to the call to this
6127 function. SYM is the symbol of the function argument holding the
6128 target PC of the exception. */
6129
6130static void
6131insert_exception_resume_breakpoint (struct thread_info *tp,
3977b71f 6132 const struct block *b,
186c406b
TT
6133 struct frame_info *frame,
6134 struct symbol *sym)
6135{
492d29ea 6136 TRY
186c406b
TT
6137 {
6138 struct symbol *vsym;
6139 struct value *value;
6140 CORE_ADDR handler;
6141 struct breakpoint *bp;
6142
d12307c1
PMR
6143 vsym = lookup_symbol (SYMBOL_LINKAGE_NAME (sym), b, VAR_DOMAIN,
6144 NULL).symbol;
186c406b
TT
6145 value = read_var_value (vsym, frame);
6146 /* If the value was optimized out, revert to the old behavior. */
6147 if (! value_optimized_out (value))
6148 {
6149 handler = value_as_address (value);
6150
6151 if (debug_infrun)
6152 fprintf_unfiltered (gdb_stdlog,
6153 "infrun: exception resume at %lx\n",
6154 (unsigned long) handler);
6155
6156 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
6157 handler, bp_exception_resume);
c70a6932
JK
6158
6159 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
6160 frame = NULL;
6161
186c406b
TT
6162 bp->thread = tp->num;
6163 inferior_thread ()->control.exception_resume_breakpoint = bp;
6164 }
6165 }
492d29ea
PA
6166 CATCH (e, RETURN_MASK_ERROR)
6167 {
6168 /* We want to ignore errors here. */
6169 }
6170 END_CATCH
186c406b
TT
6171}
6172
28106bc2
SDJ
6173/* A helper for check_exception_resume that sets an
6174 exception-breakpoint based on a SystemTap probe. */
6175
6176static void
6177insert_exception_resume_from_probe (struct thread_info *tp,
729662a5 6178 const struct bound_probe *probe,
28106bc2
SDJ
6179 struct frame_info *frame)
6180{
6181 struct value *arg_value;
6182 CORE_ADDR handler;
6183 struct breakpoint *bp;
6184
6185 arg_value = probe_safe_evaluate_at_pc (frame, 1);
6186 if (!arg_value)
6187 return;
6188
6189 handler = value_as_address (arg_value);
6190
6191 if (debug_infrun)
6192 fprintf_unfiltered (gdb_stdlog,
6193 "infrun: exception resume at %s\n",
6bac7473 6194 paddress (get_objfile_arch (probe->objfile),
28106bc2
SDJ
6195 handler));
6196
6197 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
6198 handler, bp_exception_resume);
6199 bp->thread = tp->num;
6200 inferior_thread ()->control.exception_resume_breakpoint = bp;
6201}
6202
186c406b
TT
6203/* This is called when an exception has been intercepted. Check to
6204 see whether the exception's destination is of interest, and if so,
6205 set an exception resume breakpoint there. */
6206
6207static void
6208check_exception_resume (struct execution_control_state *ecs,
28106bc2 6209 struct frame_info *frame)
186c406b 6210{
729662a5 6211 struct bound_probe probe;
28106bc2
SDJ
6212 struct symbol *func;
6213
6214 /* First see if this exception unwinding breakpoint was set via a
6215 SystemTap probe point. If so, the probe has two arguments: the
6216 CFA and the HANDLER. We ignore the CFA, extract the handler, and
6217 set a breakpoint there. */
6bac7473 6218 probe = find_probe_by_pc (get_frame_pc (frame));
729662a5 6219 if (probe.probe)
28106bc2 6220 {
729662a5 6221 insert_exception_resume_from_probe (ecs->event_thread, &probe, frame);
28106bc2
SDJ
6222 return;
6223 }
6224
6225 func = get_frame_function (frame);
6226 if (!func)
6227 return;
186c406b 6228
492d29ea 6229 TRY
186c406b 6230 {
3977b71f 6231 const struct block *b;
8157b174 6232 struct block_iterator iter;
186c406b
TT
6233 struct symbol *sym;
6234 int argno = 0;
6235
6236 /* The exception breakpoint is a thread-specific breakpoint on
6237 the unwinder's debug hook, declared as:
6238
6239 void _Unwind_DebugHook (void *cfa, void *handler);
6240
6241 The CFA argument indicates the frame to which control is
6242 about to be transferred. HANDLER is the destination PC.
6243
6244 We ignore the CFA and set a temporary breakpoint at HANDLER.
6245 This is not extremely efficient but it avoids issues in gdb
6246 with computing the DWARF CFA, and it also works even in weird
6247 cases such as throwing an exception from inside a signal
6248 handler. */
6249
6250 b = SYMBOL_BLOCK_VALUE (func);
6251 ALL_BLOCK_SYMBOLS (b, iter, sym)
6252 {
6253 if (!SYMBOL_IS_ARGUMENT (sym))
6254 continue;
6255
6256 if (argno == 0)
6257 ++argno;
6258 else
6259 {
6260 insert_exception_resume_breakpoint (ecs->event_thread,
6261 b, frame, sym);
6262 break;
6263 }
6264 }
6265 }
492d29ea
PA
6266 CATCH (e, RETURN_MASK_ERROR)
6267 {
6268 }
6269 END_CATCH
186c406b
TT
6270}
6271
104c1213 6272static void
22bcd14b 6273stop_waiting (struct execution_control_state *ecs)
104c1213 6274{
527159b7 6275 if (debug_infrun)
22bcd14b 6276 fprintf_unfiltered (gdb_stdlog, "infrun: stop_waiting\n");
527159b7 6277
31e77af2
PA
6278 clear_step_over_info ();
6279
cd0fc7c3
SS
6280 /* Let callers know we don't want to wait for the inferior anymore. */
6281 ecs->wait_some_more = 0;
6282}
6283
a9ba6bae
PA
6284/* Called when we should continue running the inferior, because the
6285 current event doesn't cause a user visible stop. This does the
6286 resuming part; waiting for the next event is done elsewhere. */
d4f3574e
SS
6287
6288static void
6289keep_going (struct execution_control_state *ecs)
6290{
c4dbc9af
PA
6291 /* Make sure normal_stop is called if we get a QUIT handled before
6292 reaching resume. */
6293 struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
6294
d4f3574e 6295 /* Save the pc before execution, to compare with pc after stop. */
fb14de7b
UW
6296 ecs->event_thread->prev_pc
6297 = regcache_read_pc (get_thread_regcache (ecs->ptid));
d4f3574e 6298
16c381f0 6299 if (ecs->event_thread->control.trap_expected
a493e3e2 6300 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
d4f3574e 6301 {
a9ba6bae
PA
6302 /* We haven't yet gotten our trap, and either: intercepted a
6303 non-signal event (e.g., a fork); or took a signal which we
6304 are supposed to pass through to the inferior. Simply
6305 continue. */
c4dbc9af 6306 discard_cleanups (old_cleanups);
64ce06e4 6307 resume (ecs->event_thread->suspend.stop_signal);
d4f3574e
SS
6308 }
6309 else
6310 {
31e77af2 6311 struct regcache *regcache = get_current_regcache ();
963f9c80
PA
6312 int remove_bp;
6313 int remove_wps;
6c4cfb24 6314 enum step_over_what step_what;
31e77af2 6315
d4f3574e 6316 /* Either the trap was not expected, but we are continuing
a9ba6bae
PA
6317 anyway (if we got a signal, the user asked it be passed to
6318 the child)
6319 -- or --
6320 We got our expected trap, but decided we should resume from
6321 it.
d4f3574e 6322
a9ba6bae 6323 We're going to run this baby now!
d4f3574e 6324
c36b740a
VP
6325 Note that insert_breakpoints won't try to re-insert
6326 already inserted breakpoints. Therefore, we don't
6327 care if breakpoints were already inserted, or not. */
a9ba6bae 6328
31e77af2
PA
6329 /* If we need to step over a breakpoint, and we're not using
6330 displaced stepping to do so, insert all breakpoints
6331 (watchpoints, etc.) but the one we're stepping over, step one
6332 instruction, and then re-insert the breakpoint when that step
6333 is finished. */
963f9c80 6334
6c4cfb24
PA
6335 step_what = thread_still_needs_step_over (ecs->event_thread);
6336
963f9c80 6337 remove_bp = (ecs->hit_singlestep_breakpoint
6c4cfb24
PA
6338 || (step_what & STEP_OVER_BREAKPOINT));
6339 remove_wps = (step_what & STEP_OVER_WATCHPOINT);
963f9c80 6340
cb71640d
PA
6341 /* We can't use displaced stepping if we need to step past a
6342 watchpoint. The instruction copied to the scratch pad would
6343 still trigger the watchpoint. */
6344 if (remove_bp
6345 && (remove_wps
6346 || !use_displaced_stepping (get_regcache_arch (regcache))))
45e8c884 6347 {
31e77af2 6348 set_step_over_info (get_regcache_aspace (regcache),
963f9c80 6349 regcache_read_pc (regcache), remove_wps);
45e8c884 6350 }
963f9c80
PA
6351 else if (remove_wps)
6352 set_step_over_info (NULL, 0, remove_wps);
45e8c884 6353 else
31e77af2 6354 clear_step_over_info ();
abbb1732 6355
31e77af2 6356 /* Stop stepping if inserting breakpoints fails. */
492d29ea 6357 TRY
31e77af2
PA
6358 {
6359 insert_breakpoints ();
6360 }
492d29ea 6361 CATCH (e, RETURN_MASK_ERROR)
31e77af2
PA
6362 {
6363 exception_print (gdb_stderr, e);
22bcd14b 6364 stop_waiting (ecs);
de1fe8c8 6365 discard_cleanups (old_cleanups);
31e77af2 6366 return;
d4f3574e 6367 }
492d29ea 6368 END_CATCH
d4f3574e 6369
963f9c80 6370 ecs->event_thread->control.trap_expected = (remove_bp || remove_wps);
d4f3574e 6371
a9ba6bae
PA
6372 /* Do not deliver GDB_SIGNAL_TRAP (except when the user
6373 explicitly specifies that such a signal should be delivered
6374 to the target program). Typically, that would occur when a
6375 user is debugging a target monitor on a simulator: the target
6376 monitor sets a breakpoint; the simulator encounters this
6377 breakpoint and halts the simulation handing control to GDB;
6378 GDB, noting that the stop address doesn't map to any known
6379 breakpoint, returns control back to the simulator; the
6380 simulator then delivers the hardware equivalent of a
6381 GDB_SIGNAL_TRAP to the program being debugged. */
a493e3e2 6382 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
16c381f0 6383 && !signal_program[ecs->event_thread->suspend.stop_signal])
a493e3e2 6384 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
d4f3574e 6385
c4dbc9af 6386 discard_cleanups (old_cleanups);
64ce06e4 6387 resume (ecs->event_thread->suspend.stop_signal);
d4f3574e
SS
6388 }
6389
488f131b 6390 prepare_to_wait (ecs);
d4f3574e
SS
6391}
6392
104c1213
JM
6393/* This function normally comes after a resume, before
6394 handle_inferior_event exits. It takes care of any last bits of
6395 housekeeping, and sets the all-important wait_some_more flag. */
cd0fc7c3 6396
104c1213
JM
6397static void
6398prepare_to_wait (struct execution_control_state *ecs)
cd0fc7c3 6399{
527159b7 6400 if (debug_infrun)
8a9de0e4 6401 fprintf_unfiltered (gdb_stdlog, "infrun: prepare_to_wait\n");
104c1213 6402
104c1213
JM
6403 /* This is the old end of the while loop. Let everybody know we
6404 want to wait for the inferior some more and get called again
6405 soon. */
6406 ecs->wait_some_more = 1;
c906108c 6407}
11cf8741 6408
fd664c91 6409/* We are done with the step range of a step/next/si/ni command.
b57bacec 6410 Called once for each n of a "step n" operation. */
fd664c91
PA
6411
6412static void
bdc36728 6413end_stepping_range (struct execution_control_state *ecs)
fd664c91 6414{
bdc36728 6415 ecs->event_thread->control.stop_step = 1;
bdc36728 6416 stop_waiting (ecs);
fd664c91
PA
6417}
6418
33d62d64
JK
6419/* Several print_*_reason functions to print why the inferior has stopped.
6420 We always print something when the inferior exits, or receives a signal.
6421 The rest of the cases are dealt with later on in normal_stop and
6422 print_it_typical. Ideally there should be a call to one of these
6423 print_*_reason functions functions from handle_inferior_event each time
22bcd14b 6424 stop_waiting is called.
33d62d64 6425
fd664c91
PA
6426 Note that we don't call these directly, instead we delegate that to
6427 the interpreters, through observers. Interpreters then call these
6428 with whatever uiout is right. */
33d62d64 6429
fd664c91
PA
6430void
6431print_end_stepping_range_reason (struct ui_out *uiout)
33d62d64 6432{
fd664c91 6433 /* For CLI-like interpreters, print nothing. */
33d62d64 6434
fd664c91
PA
6435 if (ui_out_is_mi_like_p (uiout))
6436 {
6437 ui_out_field_string (uiout, "reason",
6438 async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
6439 }
6440}
33d62d64 6441
fd664c91
PA
6442void
6443print_signal_exited_reason (struct ui_out *uiout, enum gdb_signal siggnal)
11cf8741 6444{
33d62d64
JK
6445 annotate_signalled ();
6446 if (ui_out_is_mi_like_p (uiout))
6447 ui_out_field_string
6448 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
6449 ui_out_text (uiout, "\nProgram terminated with signal ");
6450 annotate_signal_name ();
6451 ui_out_field_string (uiout, "signal-name",
2ea28649 6452 gdb_signal_to_name (siggnal));
33d62d64
JK
6453 annotate_signal_name_end ();
6454 ui_out_text (uiout, ", ");
6455 annotate_signal_string ();
6456 ui_out_field_string (uiout, "signal-meaning",
2ea28649 6457 gdb_signal_to_string (siggnal));
33d62d64
JK
6458 annotate_signal_string_end ();
6459 ui_out_text (uiout, ".\n");
6460 ui_out_text (uiout, "The program no longer exists.\n");
6461}
6462
fd664c91
PA
6463void
6464print_exited_reason (struct ui_out *uiout, int exitstatus)
33d62d64 6465{
fda326dd
TT
6466 struct inferior *inf = current_inferior ();
6467 const char *pidstr = target_pid_to_str (pid_to_ptid (inf->pid));
6468
33d62d64
JK
6469 annotate_exited (exitstatus);
6470 if (exitstatus)
6471 {
6472 if (ui_out_is_mi_like_p (uiout))
6473 ui_out_field_string (uiout, "reason",
6474 async_reason_lookup (EXEC_ASYNC_EXITED));
fda326dd
TT
6475 ui_out_text (uiout, "[Inferior ");
6476 ui_out_text (uiout, plongest (inf->num));
6477 ui_out_text (uiout, " (");
6478 ui_out_text (uiout, pidstr);
6479 ui_out_text (uiout, ") exited with code ");
33d62d64 6480 ui_out_field_fmt (uiout, "exit-code", "0%o", (unsigned int) exitstatus);
fda326dd 6481 ui_out_text (uiout, "]\n");
33d62d64
JK
6482 }
6483 else
11cf8741 6484 {
9dc5e2a9 6485 if (ui_out_is_mi_like_p (uiout))
034dad6f 6486 ui_out_field_string
33d62d64 6487 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
fda326dd
TT
6488 ui_out_text (uiout, "[Inferior ");
6489 ui_out_text (uiout, plongest (inf->num));
6490 ui_out_text (uiout, " (");
6491 ui_out_text (uiout, pidstr);
6492 ui_out_text (uiout, ") exited normally]\n");
33d62d64 6493 }
33d62d64
JK
6494}
6495
fd664c91
PA
6496void
6497print_signal_received_reason (struct ui_out *uiout, enum gdb_signal siggnal)
33d62d64
JK
6498{
6499 annotate_signal ();
6500
a493e3e2 6501 if (siggnal == GDB_SIGNAL_0 && !ui_out_is_mi_like_p (uiout))
33d62d64
JK
6502 {
6503 struct thread_info *t = inferior_thread ();
6504
6505 ui_out_text (uiout, "\n[");
6506 ui_out_field_string (uiout, "thread-name",
6507 target_pid_to_str (t->ptid));
6508 ui_out_field_fmt (uiout, "thread-id", "] #%d", t->num);
6509 ui_out_text (uiout, " stopped");
6510 }
6511 else
6512 {
6513 ui_out_text (uiout, "\nProgram received signal ");
8b93c638 6514 annotate_signal_name ();
33d62d64
JK
6515 if (ui_out_is_mi_like_p (uiout))
6516 ui_out_field_string
6517 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
488f131b 6518 ui_out_field_string (uiout, "signal-name",
2ea28649 6519 gdb_signal_to_name (siggnal));
8b93c638
JM
6520 annotate_signal_name_end ();
6521 ui_out_text (uiout, ", ");
6522 annotate_signal_string ();
488f131b 6523 ui_out_field_string (uiout, "signal-meaning",
2ea28649 6524 gdb_signal_to_string (siggnal));
8b93c638 6525 annotate_signal_string_end ();
33d62d64
JK
6526 }
6527 ui_out_text (uiout, ".\n");
6528}
252fbfc8 6529
fd664c91
PA
6530void
6531print_no_history_reason (struct ui_out *uiout)
33d62d64 6532{
fd664c91 6533 ui_out_text (uiout, "\nNo more reverse-execution history.\n");
11cf8741 6534}
43ff13b4 6535
0c7e1a46
PA
6536/* Print current location without a level number, if we have changed
6537 functions or hit a breakpoint. Print source line if we have one.
6538 bpstat_print contains the logic deciding in detail what to print,
6539 based on the event(s) that just occurred. */
6540
6541void
6542print_stop_event (struct target_waitstatus *ws)
6543{
6544 int bpstat_ret;
f486487f 6545 enum print_what source_flag;
0c7e1a46
PA
6546 int do_frame_printing = 1;
6547 struct thread_info *tp = inferior_thread ();
6548
6549 bpstat_ret = bpstat_print (tp->control.stop_bpstat, ws->kind);
6550 switch (bpstat_ret)
6551 {
6552 case PRINT_UNKNOWN:
6553 /* FIXME: cagney/2002-12-01: Given that a frame ID does (or
6554 should) carry around the function and does (or should) use
6555 that when doing a frame comparison. */
6556 if (tp->control.stop_step
6557 && frame_id_eq (tp->control.step_frame_id,
6558 get_frame_id (get_current_frame ()))
885eeb5b 6559 && tp->control.step_start_function == find_pc_function (stop_pc))
0c7e1a46
PA
6560 {
6561 /* Finished step, just print source line. */
6562 source_flag = SRC_LINE;
6563 }
6564 else
6565 {
6566 /* Print location and source line. */
6567 source_flag = SRC_AND_LOC;
6568 }
6569 break;
6570 case PRINT_SRC_AND_LOC:
6571 /* Print location and source line. */
6572 source_flag = SRC_AND_LOC;
6573 break;
6574 case PRINT_SRC_ONLY:
6575 source_flag = SRC_LINE;
6576 break;
6577 case PRINT_NOTHING:
6578 /* Something bogus. */
6579 source_flag = SRC_LINE;
6580 do_frame_printing = 0;
6581 break;
6582 default:
6583 internal_error (__FILE__, __LINE__, _("Unknown value."));
6584 }
6585
6586 /* The behavior of this routine with respect to the source
6587 flag is:
6588 SRC_LINE: Print only source line
6589 LOCATION: Print only location
6590 SRC_AND_LOC: Print location and source line. */
6591 if (do_frame_printing)
6592 print_stack_frame (get_selected_frame (NULL), 0, source_flag, 1);
6593
6594 /* Display the auto-display expressions. */
6595 do_displays ();
6596}
6597
c906108c
SS
6598/* Here to return control to GDB when the inferior stops for real.
6599 Print appropriate messages, remove breakpoints, give terminal our modes.
6600
6601 STOP_PRINT_FRAME nonzero means print the executing frame
6602 (pc, function, args, file, line number and line text).
6603 BREAKPOINTS_FAILED nonzero means stop was due to error
6604 attempting to insert breakpoints. */
6605
6606void
96baa820 6607normal_stop (void)
c906108c 6608{
73b65bb0
DJ
6609 struct target_waitstatus last;
6610 ptid_t last_ptid;
29f49a6a 6611 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
e1316e60 6612 ptid_t pid_ptid;
73b65bb0
DJ
6613
6614 get_last_target_status (&last_ptid, &last);
6615
29f49a6a
PA
6616 /* If an exception is thrown from this point on, make sure to
6617 propagate GDB's knowledge of the executing state to the
6618 frontend/user running state. A QUIT is an easy exception to see
6619 here, so do this before any filtered output. */
c35b1492
PA
6620 if (!non_stop)
6621 make_cleanup (finish_thread_state_cleanup, &minus_one_ptid);
e1316e60
PA
6622 else if (last.kind == TARGET_WAITKIND_SIGNALLED
6623 || last.kind == TARGET_WAITKIND_EXITED)
6624 {
6625 /* On some targets, we may still have live threads in the
6626 inferior when we get a process exit event. E.g., for
6627 "checkpoint", when the current checkpoint/fork exits,
6628 linux-fork.c automatically switches to another fork from
6629 within target_mourn_inferior. */
6630 if (!ptid_equal (inferior_ptid, null_ptid))
6631 {
6632 pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
6633 make_cleanup (finish_thread_state_cleanup, &pid_ptid);
6634 }
6635 }
6636 else if (last.kind != TARGET_WAITKIND_NO_RESUMED)
c35b1492 6637 make_cleanup (finish_thread_state_cleanup, &inferior_ptid);
29f49a6a 6638
b57bacec
PA
6639 /* As we're presenting a stop, and potentially removing breakpoints,
6640 update the thread list so we can tell whether there are threads
6641 running on the target. With target remote, for example, we can
6642 only learn about new threads when we explicitly update the thread
6643 list. Do this before notifying the interpreters about signal
6644 stops, end of stepping ranges, etc., so that the "new thread"
6645 output is emitted before e.g., "Program received signal FOO",
6646 instead of after. */
6647 update_thread_list ();
6648
6649 if (last.kind == TARGET_WAITKIND_STOPPED && stopped_by_random_signal)
6650 observer_notify_signal_received (inferior_thread ()->suspend.stop_signal);
6651
c906108c
SS
6652 /* As with the notification of thread events, we want to delay
6653 notifying the user that we've switched thread context until
6654 the inferior actually stops.
6655
73b65bb0
DJ
6656 There's no point in saying anything if the inferior has exited.
6657 Note that SIGNALLED here means "exited with a signal", not
b65dc60b
PA
6658 "received a signal".
6659
6660 Also skip saying anything in non-stop mode. In that mode, as we
6661 don't want GDB to switch threads behind the user's back, to avoid
6662 races where the user is typing a command to apply to thread x,
6663 but GDB switches to thread y before the user finishes entering
6664 the command, fetch_inferior_event installs a cleanup to restore
6665 the current thread back to the thread the user had selected right
6666 after this event is handled, so we're not really switching, only
6667 informing of a stop. */
4f8d22e3
PA
6668 if (!non_stop
6669 && !ptid_equal (previous_inferior_ptid, inferior_ptid)
73b65bb0
DJ
6670 && target_has_execution
6671 && last.kind != TARGET_WAITKIND_SIGNALLED
0e5bf2a8
PA
6672 && last.kind != TARGET_WAITKIND_EXITED
6673 && last.kind != TARGET_WAITKIND_NO_RESUMED)
c906108c
SS
6674 {
6675 target_terminal_ours_for_output ();
a3f17187 6676 printf_filtered (_("[Switching to %s]\n"),
c95310c6 6677 target_pid_to_str (inferior_ptid));
b8fa951a 6678 annotate_thread_changed ();
39f77062 6679 previous_inferior_ptid = inferior_ptid;
c906108c 6680 }
c906108c 6681
0e5bf2a8
PA
6682 if (last.kind == TARGET_WAITKIND_NO_RESUMED)
6683 {
6684 gdb_assert (sync_execution || !target_can_async_p ());
6685
6686 target_terminal_ours_for_output ();
6687 printf_filtered (_("No unwaited-for children left.\n"));
6688 }
6689
b57bacec 6690 /* Note: this depends on the update_thread_list call above. */
a25a5a45 6691 if (!breakpoints_should_be_inserted_now () && target_has_execution)
c906108c
SS
6692 {
6693 if (remove_breakpoints ())
6694 {
6695 target_terminal_ours_for_output ();
3e43a32a
MS
6696 printf_filtered (_("Cannot remove breakpoints because "
6697 "program is no longer writable.\nFurther "
6698 "execution is probably impossible.\n"));
c906108c
SS
6699 }
6700 }
c906108c 6701
c906108c
SS
6702 /* If an auto-display called a function and that got a signal,
6703 delete that auto-display to avoid an infinite recursion. */
6704
6705 if (stopped_by_random_signal)
6706 disable_current_display ();
6707
b57bacec 6708 /* Notify observers if we finished a "step"-like command, etc. */
af679fd0
PA
6709 if (target_has_execution
6710 && last.kind != TARGET_WAITKIND_SIGNALLED
6711 && last.kind != TARGET_WAITKIND_EXITED
16c381f0 6712 && inferior_thread ()->control.stop_step)
b57bacec 6713 {
31cc0b80 6714 /* But not if in the middle of doing a "step n" operation for
b57bacec
PA
6715 n > 1 */
6716 if (inferior_thread ()->step_multi)
6717 goto done;
6718
6719 observer_notify_end_stepping_range ();
6720 }
c906108c
SS
6721
6722 target_terminal_ours ();
0f641c01 6723 async_enable_stdin ();
c906108c 6724
7abfe014
DJ
6725 /* Set the current source location. This will also happen if we
6726 display the frame below, but the current SAL will be incorrect
6727 during a user hook-stop function. */
d729566a 6728 if (has_stack_frames () && !stop_stack_dummy)
5166082f 6729 set_current_sal_from_frame (get_current_frame ());
7abfe014 6730
28bf096c
PA
6731 /* Let the user/frontend see the threads as stopped, but defer to
6732 call_function_by_hand if the thread finished an infcall
6733 successfully. We may be e.g., evaluating a breakpoint condition.
6734 In that case, the thread had state THREAD_RUNNING before the
6735 infcall, and shall remain marked running, all without informing
6736 the user/frontend about state transition changes. */
6737 if (target_has_execution
6738 && inferior_thread ()->control.in_infcall
6739 && stop_stack_dummy == STOP_STACK_DUMMY)
251bde03
PA
6740 discard_cleanups (old_chain);
6741 else
6742 do_cleanups (old_chain);
dd7e2d2b
PA
6743
6744 /* Look up the hook_stop and run it (CLI internally handles problem
6745 of stop_command's pre-hook not existing). */
6746 if (stop_command)
6747 catch_errors (hook_stop_stub, stop_command,
6748 "Error while running hook_stop:\n", RETURN_MASK_ALL);
6749
d729566a 6750 if (!has_stack_frames ())
d51fd4c8 6751 goto done;
c906108c 6752
32400beb
PA
6753 if (last.kind == TARGET_WAITKIND_SIGNALLED
6754 || last.kind == TARGET_WAITKIND_EXITED)
6755 goto done;
6756
c906108c
SS
6757 /* Select innermost stack frame - i.e., current frame is frame 0,
6758 and current location is based on that.
6759 Don't do this on return from a stack dummy routine,
1777feb0 6760 or if the program has exited. */
c906108c
SS
6761
6762 if (!stop_stack_dummy)
6763 {
0f7d239c 6764 select_frame (get_current_frame ());
c906108c 6765
d01a8610
AS
6766 /* If --batch-silent is enabled then there's no need to print the current
6767 source location, and to try risks causing an error message about
6768 missing source files. */
6769 if (stop_print_frame && !batch_silent)
0c7e1a46 6770 print_stop_event (&last);
c906108c
SS
6771 }
6772
aa7d318d 6773 if (stop_stack_dummy == STOP_STACK_DUMMY)
c906108c 6774 {
b89667eb
DE
6775 /* Pop the empty frame that contains the stack dummy.
6776 This also restores inferior state prior to the call
16c381f0 6777 (struct infcall_suspend_state). */
b89667eb 6778 struct frame_info *frame = get_current_frame ();
abbb1732 6779
b89667eb
DE
6780 gdb_assert (get_frame_type (frame) == DUMMY_FRAME);
6781 frame_pop (frame);
3e43a32a
MS
6782 /* frame_pop() calls reinit_frame_cache as the last thing it
6783 does which means there's currently no selected frame. We
6784 don't need to re-establish a selected frame if the dummy call
6785 returns normally, that will be done by
6786 restore_infcall_control_state. However, we do have to handle
6787 the case where the dummy call is returning after being
6788 stopped (e.g. the dummy call previously hit a breakpoint).
6789 We can't know which case we have so just always re-establish
6790 a selected frame here. */
0f7d239c 6791 select_frame (get_current_frame ());
c906108c
SS
6792 }
6793
c906108c
SS
6794done:
6795 annotate_stopped ();
41d2bdb4
PA
6796
6797 /* Suppress the stop observer if we're in the middle of:
6798
6799 - a step n (n > 1), as there still more steps to be done.
6800
6801 - a "finish" command, as the observer will be called in
6802 finish_command_continuation, so it can include the inferior
6803 function's return value.
6804
6805 - calling an inferior function, as we pretend we inferior didn't
6806 run at all. The return value of the call is handled by the
6807 expression evaluator, through call_function_by_hand. */
6808
6809 if (!target_has_execution
6810 || last.kind == TARGET_WAITKIND_SIGNALLED
6811 || last.kind == TARGET_WAITKIND_EXITED
0e5bf2a8 6812 || last.kind == TARGET_WAITKIND_NO_RESUMED
2ca0b532
PA
6813 || (!(inferior_thread ()->step_multi
6814 && inferior_thread ()->control.stop_step)
16c381f0
JK
6815 && !(inferior_thread ()->control.stop_bpstat
6816 && inferior_thread ()->control.proceed_to_finish)
6817 && !inferior_thread ()->control.in_infcall))
347bddb7
PA
6818 {
6819 if (!ptid_equal (inferior_ptid, null_ptid))
16c381f0 6820 observer_notify_normal_stop (inferior_thread ()->control.stop_bpstat,
1d33d6ba 6821 stop_print_frame);
347bddb7 6822 else
1d33d6ba 6823 observer_notify_normal_stop (NULL, stop_print_frame);
347bddb7 6824 }
347bddb7 6825
48844aa6
PA
6826 if (target_has_execution)
6827 {
6828 if (last.kind != TARGET_WAITKIND_SIGNALLED
6829 && last.kind != TARGET_WAITKIND_EXITED)
6830 /* Delete the breakpoint we stopped at, if it wants to be deleted.
6831 Delete any breakpoint that is to be deleted at the next stop. */
16c381f0 6832 breakpoint_auto_delete (inferior_thread ()->control.stop_bpstat);
94cc34af 6833 }
6c95b8df
PA
6834
6835 /* Try to get rid of automatically added inferiors that are no
6836 longer needed. Keeping those around slows down things linearly.
6837 Note that this never removes the current inferior. */
6838 prune_inferiors ();
c906108c
SS
6839}
6840
6841static int
96baa820 6842hook_stop_stub (void *cmd)
c906108c 6843{
5913bcb0 6844 execute_cmd_pre_hook ((struct cmd_list_element *) cmd);
c906108c
SS
6845 return (0);
6846}
6847\f
c5aa993b 6848int
96baa820 6849signal_stop_state (int signo)
c906108c 6850{
d6b48e9c 6851 return signal_stop[signo];
c906108c
SS
6852}
6853
c5aa993b 6854int
96baa820 6855signal_print_state (int signo)
c906108c
SS
6856{
6857 return signal_print[signo];
6858}
6859
c5aa993b 6860int
96baa820 6861signal_pass_state (int signo)
c906108c
SS
6862{
6863 return signal_program[signo];
6864}
6865
2455069d
UW
6866static void
6867signal_cache_update (int signo)
6868{
6869 if (signo == -1)
6870 {
a493e3e2 6871 for (signo = 0; signo < (int) GDB_SIGNAL_LAST; signo++)
2455069d
UW
6872 signal_cache_update (signo);
6873
6874 return;
6875 }
6876
6877 signal_pass[signo] = (signal_stop[signo] == 0
6878 && signal_print[signo] == 0
ab04a2af
TT
6879 && signal_program[signo] == 1
6880 && signal_catch[signo] == 0);
2455069d
UW
6881}
6882
488f131b 6883int
7bda5e4a 6884signal_stop_update (int signo, int state)
d4f3574e
SS
6885{
6886 int ret = signal_stop[signo];
abbb1732 6887
d4f3574e 6888 signal_stop[signo] = state;
2455069d 6889 signal_cache_update (signo);
d4f3574e
SS
6890 return ret;
6891}
6892
488f131b 6893int
7bda5e4a 6894signal_print_update (int signo, int state)
d4f3574e
SS
6895{
6896 int ret = signal_print[signo];
abbb1732 6897
d4f3574e 6898 signal_print[signo] = state;
2455069d 6899 signal_cache_update (signo);
d4f3574e
SS
6900 return ret;
6901}
6902
488f131b 6903int
7bda5e4a 6904signal_pass_update (int signo, int state)
d4f3574e
SS
6905{
6906 int ret = signal_program[signo];
abbb1732 6907
d4f3574e 6908 signal_program[signo] = state;
2455069d 6909 signal_cache_update (signo);
d4f3574e
SS
6910 return ret;
6911}
6912
ab04a2af
TT
6913/* Update the global 'signal_catch' from INFO and notify the
6914 target. */
6915
6916void
6917signal_catch_update (const unsigned int *info)
6918{
6919 int i;
6920
6921 for (i = 0; i < GDB_SIGNAL_LAST; ++i)
6922 signal_catch[i] = info[i] > 0;
6923 signal_cache_update (-1);
6924 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
6925}
6926
c906108c 6927static void
96baa820 6928sig_print_header (void)
c906108c 6929{
3e43a32a
MS
6930 printf_filtered (_("Signal Stop\tPrint\tPass "
6931 "to program\tDescription\n"));
c906108c
SS
6932}
6933
6934static void
2ea28649 6935sig_print_info (enum gdb_signal oursig)
c906108c 6936{
2ea28649 6937 const char *name = gdb_signal_to_name (oursig);
c906108c 6938 int name_padding = 13 - strlen (name);
96baa820 6939
c906108c
SS
6940 if (name_padding <= 0)
6941 name_padding = 0;
6942
6943 printf_filtered ("%s", name);
488f131b 6944 printf_filtered ("%*.*s ", name_padding, name_padding, " ");
c906108c
SS
6945 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
6946 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
6947 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
2ea28649 6948 printf_filtered ("%s\n", gdb_signal_to_string (oursig));
c906108c
SS
6949}
6950
6951/* Specify how various signals in the inferior should be handled. */
6952
6953static void
96baa820 6954handle_command (char *args, int from_tty)
c906108c
SS
6955{
6956 char **argv;
6957 int digits, wordlen;
6958 int sigfirst, signum, siglast;
2ea28649 6959 enum gdb_signal oursig;
c906108c
SS
6960 int allsigs;
6961 int nsigs;
6962 unsigned char *sigs;
6963 struct cleanup *old_chain;
6964
6965 if (args == NULL)
6966 {
e2e0b3e5 6967 error_no_arg (_("signal to handle"));
c906108c
SS
6968 }
6969
1777feb0 6970 /* Allocate and zero an array of flags for which signals to handle. */
c906108c 6971
a493e3e2 6972 nsigs = (int) GDB_SIGNAL_LAST;
c906108c
SS
6973 sigs = (unsigned char *) alloca (nsigs);
6974 memset (sigs, 0, nsigs);
6975
1777feb0 6976 /* Break the command line up into args. */
c906108c 6977
d1a41061 6978 argv = gdb_buildargv (args);
7a292a7a 6979 old_chain = make_cleanup_freeargv (argv);
c906108c
SS
6980
6981 /* Walk through the args, looking for signal oursigs, signal names, and
6982 actions. Signal numbers and signal names may be interspersed with
6983 actions, with the actions being performed for all signals cumulatively
1777feb0 6984 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
c906108c
SS
6985
6986 while (*argv != NULL)
6987 {
6988 wordlen = strlen (*argv);
6989 for (digits = 0; isdigit ((*argv)[digits]); digits++)
6990 {;
6991 }
6992 allsigs = 0;
6993 sigfirst = siglast = -1;
6994
6995 if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
6996 {
6997 /* Apply action to all signals except those used by the
1777feb0 6998 debugger. Silently skip those. */
c906108c
SS
6999 allsigs = 1;
7000 sigfirst = 0;
7001 siglast = nsigs - 1;
7002 }
7003 else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
7004 {
7005 SET_SIGS (nsigs, sigs, signal_stop);
7006 SET_SIGS (nsigs, sigs, signal_print);
7007 }
7008 else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
7009 {
7010 UNSET_SIGS (nsigs, sigs, signal_program);
7011 }
7012 else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
7013 {
7014 SET_SIGS (nsigs, sigs, signal_print);
7015 }
7016 else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
7017 {
7018 SET_SIGS (nsigs, sigs, signal_program);
7019 }
7020 else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
7021 {
7022 UNSET_SIGS (nsigs, sigs, signal_stop);
7023 }
7024 else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
7025 {
7026 SET_SIGS (nsigs, sigs, signal_program);
7027 }
7028 else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
7029 {
7030 UNSET_SIGS (nsigs, sigs, signal_print);
7031 UNSET_SIGS (nsigs, sigs, signal_stop);
7032 }
7033 else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
7034 {
7035 UNSET_SIGS (nsigs, sigs, signal_program);
7036 }
7037 else if (digits > 0)
7038 {
7039 /* It is numeric. The numeric signal refers to our own
7040 internal signal numbering from target.h, not to host/target
7041 signal number. This is a feature; users really should be
7042 using symbolic names anyway, and the common ones like
7043 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
7044
7045 sigfirst = siglast = (int)
2ea28649 7046 gdb_signal_from_command (atoi (*argv));
c906108c
SS
7047 if ((*argv)[digits] == '-')
7048 {
7049 siglast = (int)
2ea28649 7050 gdb_signal_from_command (atoi ((*argv) + digits + 1));
c906108c
SS
7051 }
7052 if (sigfirst > siglast)
7053 {
1777feb0 7054 /* Bet he didn't figure we'd think of this case... */
c906108c
SS
7055 signum = sigfirst;
7056 sigfirst = siglast;
7057 siglast = signum;
7058 }
7059 }
7060 else
7061 {
2ea28649 7062 oursig = gdb_signal_from_name (*argv);
a493e3e2 7063 if (oursig != GDB_SIGNAL_UNKNOWN)
c906108c
SS
7064 {
7065 sigfirst = siglast = (int) oursig;
7066 }
7067 else
7068 {
7069 /* Not a number and not a recognized flag word => complain. */
8a3fe4f8 7070 error (_("Unrecognized or ambiguous flag word: \"%s\"."), *argv);
c906108c
SS
7071 }
7072 }
7073
7074 /* If any signal numbers or symbol names were found, set flags for
1777feb0 7075 which signals to apply actions to. */
c906108c
SS
7076
7077 for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
7078 {
2ea28649 7079 switch ((enum gdb_signal) signum)
c906108c 7080 {
a493e3e2
PA
7081 case GDB_SIGNAL_TRAP:
7082 case GDB_SIGNAL_INT:
c906108c
SS
7083 if (!allsigs && !sigs[signum])
7084 {
9e2f0ad4 7085 if (query (_("%s is used by the debugger.\n\
3e43a32a 7086Are you sure you want to change it? "),
2ea28649 7087 gdb_signal_to_name ((enum gdb_signal) signum)))
c906108c
SS
7088 {
7089 sigs[signum] = 1;
7090 }
7091 else
7092 {
a3f17187 7093 printf_unfiltered (_("Not confirmed, unchanged.\n"));
c906108c
SS
7094 gdb_flush (gdb_stdout);
7095 }
7096 }
7097 break;
a493e3e2
PA
7098 case GDB_SIGNAL_0:
7099 case GDB_SIGNAL_DEFAULT:
7100 case GDB_SIGNAL_UNKNOWN:
c906108c
SS
7101 /* Make sure that "all" doesn't print these. */
7102 break;
7103 default:
7104 sigs[signum] = 1;
7105 break;
7106 }
7107 }
7108
7109 argv++;
7110 }
7111
3a031f65
PA
7112 for (signum = 0; signum < nsigs; signum++)
7113 if (sigs[signum])
7114 {
2455069d 7115 signal_cache_update (-1);
a493e3e2
PA
7116 target_pass_signals ((int) GDB_SIGNAL_LAST, signal_pass);
7117 target_program_signals ((int) GDB_SIGNAL_LAST, signal_program);
c906108c 7118
3a031f65
PA
7119 if (from_tty)
7120 {
7121 /* Show the results. */
7122 sig_print_header ();
7123 for (; signum < nsigs; signum++)
7124 if (sigs[signum])
aead7601 7125 sig_print_info ((enum gdb_signal) signum);
3a031f65
PA
7126 }
7127
7128 break;
7129 }
c906108c
SS
7130
7131 do_cleanups (old_chain);
7132}
7133
de0bea00
MF
7134/* Complete the "handle" command. */
7135
7136static VEC (char_ptr) *
7137handle_completer (struct cmd_list_element *ignore,
6f937416 7138 const char *text, const char *word)
de0bea00
MF
7139{
7140 VEC (char_ptr) *vec_signals, *vec_keywords, *return_val;
7141 static const char * const keywords[] =
7142 {
7143 "all",
7144 "stop",
7145 "ignore",
7146 "print",
7147 "pass",
7148 "nostop",
7149 "noignore",
7150 "noprint",
7151 "nopass",
7152 NULL,
7153 };
7154
7155 vec_signals = signal_completer (ignore, text, word);
7156 vec_keywords = complete_on_enum (keywords, word, word);
7157
7158 return_val = VEC_merge (char_ptr, vec_signals, vec_keywords);
7159 VEC_free (char_ptr, vec_signals);
7160 VEC_free (char_ptr, vec_keywords);
7161 return return_val;
7162}
7163
2ea28649
PA
7164enum gdb_signal
7165gdb_signal_from_command (int num)
ed01b82c
PA
7166{
7167 if (num >= 1 && num <= 15)
2ea28649 7168 return (enum gdb_signal) num;
ed01b82c
PA
7169 error (_("Only signals 1-15 are valid as numeric signals.\n\
7170Use \"info signals\" for a list of symbolic signals."));
7171}
7172
c906108c
SS
7173/* Print current contents of the tables set by the handle command.
7174 It is possible we should just be printing signals actually used
7175 by the current target (but for things to work right when switching
7176 targets, all signals should be in the signal tables). */
7177
7178static void
96baa820 7179signals_info (char *signum_exp, int from_tty)
c906108c 7180{
2ea28649 7181 enum gdb_signal oursig;
abbb1732 7182
c906108c
SS
7183 sig_print_header ();
7184
7185 if (signum_exp)
7186 {
7187 /* First see if this is a symbol name. */
2ea28649 7188 oursig = gdb_signal_from_name (signum_exp);
a493e3e2 7189 if (oursig == GDB_SIGNAL_UNKNOWN)
c906108c
SS
7190 {
7191 /* No, try numeric. */
7192 oursig =
2ea28649 7193 gdb_signal_from_command (parse_and_eval_long (signum_exp));
c906108c
SS
7194 }
7195 sig_print_info (oursig);
7196 return;
7197 }
7198
7199 printf_filtered ("\n");
7200 /* These ugly casts brought to you by the native VAX compiler. */
a493e3e2
PA
7201 for (oursig = GDB_SIGNAL_FIRST;
7202 (int) oursig < (int) GDB_SIGNAL_LAST;
2ea28649 7203 oursig = (enum gdb_signal) ((int) oursig + 1))
c906108c
SS
7204 {
7205 QUIT;
7206
a493e3e2
PA
7207 if (oursig != GDB_SIGNAL_UNKNOWN
7208 && oursig != GDB_SIGNAL_DEFAULT && oursig != GDB_SIGNAL_0)
c906108c
SS
7209 sig_print_info (oursig);
7210 }
7211
3e43a32a
MS
7212 printf_filtered (_("\nUse the \"handle\" command "
7213 "to change these tables.\n"));
c906108c 7214}
4aa995e1 7215
c709acd1
PA
7216/* Check if it makes sense to read $_siginfo from the current thread
7217 at this point. If not, throw an error. */
7218
7219static void
7220validate_siginfo_access (void)
7221{
7222 /* No current inferior, no siginfo. */
7223 if (ptid_equal (inferior_ptid, null_ptid))
7224 error (_("No thread selected."));
7225
7226 /* Don't try to read from a dead thread. */
7227 if (is_exited (inferior_ptid))
7228 error (_("The current thread has terminated"));
7229
7230 /* ... or from a spinning thread. */
7231 if (is_running (inferior_ptid))
7232 error (_("Selected thread is running."));
7233}
7234
4aa995e1
PA
7235/* The $_siginfo convenience variable is a bit special. We don't know
7236 for sure the type of the value until we actually have a chance to
7a9dd1b2 7237 fetch the data. The type can change depending on gdbarch, so it is
4aa995e1
PA
7238 also dependent on which thread you have selected.
7239
7240 1. making $_siginfo be an internalvar that creates a new value on
7241 access.
7242
7243 2. making the value of $_siginfo be an lval_computed value. */
7244
7245/* This function implements the lval_computed support for reading a
7246 $_siginfo value. */
7247
7248static void
7249siginfo_value_read (struct value *v)
7250{
7251 LONGEST transferred;
7252
c709acd1
PA
7253 validate_siginfo_access ();
7254
4aa995e1
PA
7255 transferred =
7256 target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO,
7257 NULL,
7258 value_contents_all_raw (v),
7259 value_offset (v),
7260 TYPE_LENGTH (value_type (v)));
7261
7262 if (transferred != TYPE_LENGTH (value_type (v)))
7263 error (_("Unable to read siginfo"));
7264}
7265
7266/* This function implements the lval_computed support for writing a
7267 $_siginfo value. */
7268
7269static void
7270siginfo_value_write (struct value *v, struct value *fromval)
7271{
7272 LONGEST transferred;
7273
c709acd1
PA
7274 validate_siginfo_access ();
7275
4aa995e1
PA
7276 transferred = target_write (&current_target,
7277 TARGET_OBJECT_SIGNAL_INFO,
7278 NULL,
7279 value_contents_all_raw (fromval),
7280 value_offset (v),
7281 TYPE_LENGTH (value_type (fromval)));
7282
7283 if (transferred != TYPE_LENGTH (value_type (fromval)))
7284 error (_("Unable to write siginfo"));
7285}
7286
c8f2448a 7287static const struct lval_funcs siginfo_value_funcs =
4aa995e1
PA
7288 {
7289 siginfo_value_read,
7290 siginfo_value_write
7291 };
7292
7293/* Return a new value with the correct type for the siginfo object of
78267919
UW
7294 the current thread using architecture GDBARCH. Return a void value
7295 if there's no object available. */
4aa995e1 7296
2c0b251b 7297static struct value *
22d2b532
SDJ
7298siginfo_make_value (struct gdbarch *gdbarch, struct internalvar *var,
7299 void *ignore)
4aa995e1 7300{
4aa995e1 7301 if (target_has_stack
78267919
UW
7302 && !ptid_equal (inferior_ptid, null_ptid)
7303 && gdbarch_get_siginfo_type_p (gdbarch))
4aa995e1 7304 {
78267919 7305 struct type *type = gdbarch_get_siginfo_type (gdbarch);
abbb1732 7306
78267919 7307 return allocate_computed_value (type, &siginfo_value_funcs, NULL);
4aa995e1
PA
7308 }
7309
78267919 7310 return allocate_value (builtin_type (gdbarch)->builtin_void);
4aa995e1
PA
7311}
7312
c906108c 7313\f
16c381f0
JK
7314/* infcall_suspend_state contains state about the program itself like its
7315 registers and any signal it received when it last stopped.
7316 This state must be restored regardless of how the inferior function call
7317 ends (either successfully, or after it hits a breakpoint or signal)
7318 if the program is to properly continue where it left off. */
7319
7320struct infcall_suspend_state
7a292a7a 7321{
16c381f0 7322 struct thread_suspend_state thread_suspend;
16c381f0
JK
7323
7324 /* Other fields: */
7a292a7a 7325 CORE_ADDR stop_pc;
b89667eb 7326 struct regcache *registers;
1736ad11 7327
35515841 7328 /* Format of SIGINFO_DATA or NULL if it is not present. */
1736ad11
JK
7329 struct gdbarch *siginfo_gdbarch;
7330
7331 /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
7332 TYPE_LENGTH (gdbarch_get_siginfo_type ()). For different gdbarch the
7333 content would be invalid. */
7334 gdb_byte *siginfo_data;
b89667eb
DE
7335};
7336
16c381f0
JK
7337struct infcall_suspend_state *
7338save_infcall_suspend_state (void)
b89667eb 7339{
16c381f0 7340 struct infcall_suspend_state *inf_state;
b89667eb 7341 struct thread_info *tp = inferior_thread ();
1736ad11
JK
7342 struct regcache *regcache = get_current_regcache ();
7343 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7344 gdb_byte *siginfo_data = NULL;
7345
7346 if (gdbarch_get_siginfo_type_p (gdbarch))
7347 {
7348 struct type *type = gdbarch_get_siginfo_type (gdbarch);
7349 size_t len = TYPE_LENGTH (type);
7350 struct cleanup *back_to;
7351
7352 siginfo_data = xmalloc (len);
7353 back_to = make_cleanup (xfree, siginfo_data);
7354
7355 if (target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
7356 siginfo_data, 0, len) == len)
7357 discard_cleanups (back_to);
7358 else
7359 {
7360 /* Errors ignored. */
7361 do_cleanups (back_to);
7362 siginfo_data = NULL;
7363 }
7364 }
7365
41bf6aca 7366 inf_state = XCNEW (struct infcall_suspend_state);
1736ad11
JK
7367
7368 if (siginfo_data)
7369 {
7370 inf_state->siginfo_gdbarch = gdbarch;
7371 inf_state->siginfo_data = siginfo_data;
7372 }
b89667eb 7373
16c381f0 7374 inf_state->thread_suspend = tp->suspend;
16c381f0 7375
35515841 7376 /* run_inferior_call will not use the signal due to its `proceed' call with
a493e3e2
PA
7377 GDB_SIGNAL_0 anyway. */
7378 tp->suspend.stop_signal = GDB_SIGNAL_0;
35515841 7379
b89667eb
DE
7380 inf_state->stop_pc = stop_pc;
7381
1736ad11 7382 inf_state->registers = regcache_dup (regcache);
b89667eb
DE
7383
7384 return inf_state;
7385}
7386
7387/* Restore inferior session state to INF_STATE. */
7388
7389void
16c381f0 7390restore_infcall_suspend_state (struct infcall_suspend_state *inf_state)
b89667eb
DE
7391{
7392 struct thread_info *tp = inferior_thread ();
1736ad11
JK
7393 struct regcache *regcache = get_current_regcache ();
7394 struct gdbarch *gdbarch = get_regcache_arch (regcache);
b89667eb 7395
16c381f0 7396 tp->suspend = inf_state->thread_suspend;
16c381f0 7397
b89667eb
DE
7398 stop_pc = inf_state->stop_pc;
7399
1736ad11
JK
7400 if (inf_state->siginfo_gdbarch == gdbarch)
7401 {
7402 struct type *type = gdbarch_get_siginfo_type (gdbarch);
1736ad11
JK
7403
7404 /* Errors ignored. */
7405 target_write (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
6acef6cd 7406 inf_state->siginfo_data, 0, TYPE_LENGTH (type));
1736ad11
JK
7407 }
7408
b89667eb
DE
7409 /* The inferior can be gone if the user types "print exit(0)"
7410 (and perhaps other times). */
7411 if (target_has_execution)
7412 /* NB: The register write goes through to the target. */
1736ad11 7413 regcache_cpy (regcache, inf_state->registers);
803b5f95 7414
16c381f0 7415 discard_infcall_suspend_state (inf_state);
b89667eb
DE
7416}
7417
7418static void
16c381f0 7419do_restore_infcall_suspend_state_cleanup (void *state)
b89667eb 7420{
16c381f0 7421 restore_infcall_suspend_state (state);
b89667eb
DE
7422}
7423
7424struct cleanup *
16c381f0
JK
7425make_cleanup_restore_infcall_suspend_state
7426 (struct infcall_suspend_state *inf_state)
b89667eb 7427{
16c381f0 7428 return make_cleanup (do_restore_infcall_suspend_state_cleanup, inf_state);
b89667eb
DE
7429}
7430
7431void
16c381f0 7432discard_infcall_suspend_state (struct infcall_suspend_state *inf_state)
b89667eb
DE
7433{
7434 regcache_xfree (inf_state->registers);
803b5f95 7435 xfree (inf_state->siginfo_data);
b89667eb
DE
7436 xfree (inf_state);
7437}
7438
7439struct regcache *
16c381f0 7440get_infcall_suspend_state_regcache (struct infcall_suspend_state *inf_state)
b89667eb
DE
7441{
7442 return inf_state->registers;
7443}
7444
16c381f0
JK
7445/* infcall_control_state contains state regarding gdb's control of the
7446 inferior itself like stepping control. It also contains session state like
7447 the user's currently selected frame. */
b89667eb 7448
16c381f0 7449struct infcall_control_state
b89667eb 7450{
16c381f0
JK
7451 struct thread_control_state thread_control;
7452 struct inferior_control_state inferior_control;
d82142e2
JK
7453
7454 /* Other fields: */
7455 enum stop_stack_kind stop_stack_dummy;
7456 int stopped_by_random_signal;
7a292a7a 7457 int stop_after_trap;
7a292a7a 7458
b89667eb 7459 /* ID if the selected frame when the inferior function call was made. */
101dcfbe 7460 struct frame_id selected_frame_id;
7a292a7a
SS
7461};
7462
c906108c 7463/* Save all of the information associated with the inferior<==>gdb
b89667eb 7464 connection. */
c906108c 7465
16c381f0
JK
7466struct infcall_control_state *
7467save_infcall_control_state (void)
c906108c 7468{
16c381f0 7469 struct infcall_control_state *inf_status = xmalloc (sizeof (*inf_status));
4e1c45ea 7470 struct thread_info *tp = inferior_thread ();
d6b48e9c 7471 struct inferior *inf = current_inferior ();
7a292a7a 7472
16c381f0
JK
7473 inf_status->thread_control = tp->control;
7474 inf_status->inferior_control = inf->control;
d82142e2 7475
8358c15c 7476 tp->control.step_resume_breakpoint = NULL;
5b79abe7 7477 tp->control.exception_resume_breakpoint = NULL;
8358c15c 7478
16c381f0
JK
7479 /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
7480 chain. If caller's caller is walking the chain, they'll be happier if we
7481 hand them back the original chain when restore_infcall_control_state is
7482 called. */
7483 tp->control.stop_bpstat = bpstat_copy (tp->control.stop_bpstat);
d82142e2
JK
7484
7485 /* Other fields: */
7486 inf_status->stop_stack_dummy = stop_stack_dummy;
7487 inf_status->stopped_by_random_signal = stopped_by_random_signal;
7488 inf_status->stop_after_trap = stop_after_trap;
c5aa993b 7489
206415a3 7490 inf_status->selected_frame_id = get_frame_id (get_selected_frame (NULL));
b89667eb 7491
7a292a7a 7492 return inf_status;
c906108c
SS
7493}
7494
c906108c 7495static int
96baa820 7496restore_selected_frame (void *args)
c906108c 7497{
488f131b 7498 struct frame_id *fid = (struct frame_id *) args;
c906108c 7499 struct frame_info *frame;
c906108c 7500
101dcfbe 7501 frame = frame_find_by_id (*fid);
c906108c 7502
aa0cd9c1
AC
7503 /* If inf_status->selected_frame_id is NULL, there was no previously
7504 selected frame. */
101dcfbe 7505 if (frame == NULL)
c906108c 7506 {
8a3fe4f8 7507 warning (_("Unable to restore previously selected frame."));
c906108c
SS
7508 return 0;
7509 }
7510
0f7d239c 7511 select_frame (frame);
c906108c
SS
7512
7513 return (1);
7514}
7515
b89667eb
DE
7516/* Restore inferior session state to INF_STATUS. */
7517
c906108c 7518void
16c381f0 7519restore_infcall_control_state (struct infcall_control_state *inf_status)
c906108c 7520{
4e1c45ea 7521 struct thread_info *tp = inferior_thread ();
d6b48e9c 7522 struct inferior *inf = current_inferior ();
4e1c45ea 7523
8358c15c
JK
7524 if (tp->control.step_resume_breakpoint)
7525 tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
7526
5b79abe7
TT
7527 if (tp->control.exception_resume_breakpoint)
7528 tp->control.exception_resume_breakpoint->disposition
7529 = disp_del_at_next_stop;
7530
d82142e2 7531 /* Handle the bpstat_copy of the chain. */
16c381f0 7532 bpstat_clear (&tp->control.stop_bpstat);
d82142e2 7533
16c381f0
JK
7534 tp->control = inf_status->thread_control;
7535 inf->control = inf_status->inferior_control;
d82142e2
JK
7536
7537 /* Other fields: */
7538 stop_stack_dummy = inf_status->stop_stack_dummy;
7539 stopped_by_random_signal = inf_status->stopped_by_random_signal;
7540 stop_after_trap = inf_status->stop_after_trap;
c906108c 7541
b89667eb 7542 if (target_has_stack)
c906108c 7543 {
c906108c 7544 /* The point of catch_errors is that if the stack is clobbered,
101dcfbe
AC
7545 walking the stack might encounter a garbage pointer and
7546 error() trying to dereference it. */
488f131b
JB
7547 if (catch_errors
7548 (restore_selected_frame, &inf_status->selected_frame_id,
7549 "Unable to restore previously selected frame:\n",
7550 RETURN_MASK_ERROR) == 0)
c906108c
SS
7551 /* Error in restoring the selected frame. Select the innermost
7552 frame. */
0f7d239c 7553 select_frame (get_current_frame ());
c906108c 7554 }
c906108c 7555
72cec141 7556 xfree (inf_status);
7a292a7a 7557}
c906108c 7558
74b7792f 7559static void
16c381f0 7560do_restore_infcall_control_state_cleanup (void *sts)
74b7792f 7561{
16c381f0 7562 restore_infcall_control_state (sts);
74b7792f
AC
7563}
7564
7565struct cleanup *
16c381f0
JK
7566make_cleanup_restore_infcall_control_state
7567 (struct infcall_control_state *inf_status)
74b7792f 7568{
16c381f0 7569 return make_cleanup (do_restore_infcall_control_state_cleanup, inf_status);
74b7792f
AC
7570}
7571
c906108c 7572void
16c381f0 7573discard_infcall_control_state (struct infcall_control_state *inf_status)
7a292a7a 7574{
8358c15c
JK
7575 if (inf_status->thread_control.step_resume_breakpoint)
7576 inf_status->thread_control.step_resume_breakpoint->disposition
7577 = disp_del_at_next_stop;
7578
5b79abe7
TT
7579 if (inf_status->thread_control.exception_resume_breakpoint)
7580 inf_status->thread_control.exception_resume_breakpoint->disposition
7581 = disp_del_at_next_stop;
7582
1777feb0 7583 /* See save_infcall_control_state for info on stop_bpstat. */
16c381f0 7584 bpstat_clear (&inf_status->thread_control.stop_bpstat);
8358c15c 7585
72cec141 7586 xfree (inf_status);
7a292a7a 7587}
b89667eb 7588\f
ca6724c1
KB
7589/* restore_inferior_ptid() will be used by the cleanup machinery
7590 to restore the inferior_ptid value saved in a call to
7591 save_inferior_ptid(). */
ce696e05
KB
7592
7593static void
7594restore_inferior_ptid (void *arg)
7595{
7596 ptid_t *saved_ptid_ptr = arg;
abbb1732 7597
ce696e05
KB
7598 inferior_ptid = *saved_ptid_ptr;
7599 xfree (arg);
7600}
7601
7602/* Save the value of inferior_ptid so that it may be restored by a
7603 later call to do_cleanups(). Returns the struct cleanup pointer
7604 needed for later doing the cleanup. */
7605
7606struct cleanup *
7607save_inferior_ptid (void)
7608{
7609 ptid_t *saved_ptid_ptr;
7610
7611 saved_ptid_ptr = xmalloc (sizeof (ptid_t));
7612 *saved_ptid_ptr = inferior_ptid;
7613 return make_cleanup (restore_inferior_ptid, saved_ptid_ptr);
7614}
0c557179 7615
7f89fd65 7616/* See infrun.h. */
0c557179
SDJ
7617
7618void
7619clear_exit_convenience_vars (void)
7620{
7621 clear_internalvar (lookup_internalvar ("_exitsignal"));
7622 clear_internalvar (lookup_internalvar ("_exitcode"));
7623}
c5aa993b 7624\f
488f131b 7625
b2175913
MS
7626/* User interface for reverse debugging:
7627 Set exec-direction / show exec-direction commands
7628 (returns error unless target implements to_set_exec_direction method). */
7629
32231432 7630int execution_direction = EXEC_FORWARD;
b2175913
MS
7631static const char exec_forward[] = "forward";
7632static const char exec_reverse[] = "reverse";
7633static const char *exec_direction = exec_forward;
40478521 7634static const char *const exec_direction_names[] = {
b2175913
MS
7635 exec_forward,
7636 exec_reverse,
7637 NULL
7638};
7639
7640static void
7641set_exec_direction_func (char *args, int from_tty,
7642 struct cmd_list_element *cmd)
7643{
7644 if (target_can_execute_reverse)
7645 {
7646 if (!strcmp (exec_direction, exec_forward))
7647 execution_direction = EXEC_FORWARD;
7648 else if (!strcmp (exec_direction, exec_reverse))
7649 execution_direction = EXEC_REVERSE;
7650 }
8bbed405
MS
7651 else
7652 {
7653 exec_direction = exec_forward;
7654 error (_("Target does not support this operation."));
7655 }
b2175913
MS
7656}
7657
7658static void
7659show_exec_direction_func (struct ui_file *out, int from_tty,
7660 struct cmd_list_element *cmd, const char *value)
7661{
7662 switch (execution_direction) {
7663 case EXEC_FORWARD:
7664 fprintf_filtered (out, _("Forward.\n"));
7665 break;
7666 case EXEC_REVERSE:
7667 fprintf_filtered (out, _("Reverse.\n"));
7668 break;
b2175913 7669 default:
d8b34453
PA
7670 internal_error (__FILE__, __LINE__,
7671 _("bogus execution_direction value: %d"),
7672 (int) execution_direction);
b2175913
MS
7673 }
7674}
7675
d4db2f36
PA
7676static void
7677show_schedule_multiple (struct ui_file *file, int from_tty,
7678 struct cmd_list_element *c, const char *value)
7679{
3e43a32a
MS
7680 fprintf_filtered (file, _("Resuming the execution of threads "
7681 "of all processes is %s.\n"), value);
d4db2f36 7682}
ad52ddc6 7683
22d2b532
SDJ
7684/* Implementation of `siginfo' variable. */
7685
7686static const struct internalvar_funcs siginfo_funcs =
7687{
7688 siginfo_make_value,
7689 NULL,
7690 NULL
7691};
7692
c906108c 7693void
96baa820 7694_initialize_infrun (void)
c906108c 7695{
52f0bd74
AC
7696 int i;
7697 int numsigs;
de0bea00 7698 struct cmd_list_element *c;
c906108c 7699
1bedd215
AC
7700 add_info ("signals", signals_info, _("\
7701What debugger does when program gets various signals.\n\
7702Specify a signal as argument to print info on that signal only."));
c906108c
SS
7703 add_info_alias ("handle", "signals", 0);
7704
de0bea00 7705 c = add_com ("handle", class_run, handle_command, _("\
dfbd5e7b 7706Specify how to handle signals.\n\
486c7739 7707Usage: handle SIGNAL [ACTIONS]\n\
c906108c 7708Args are signals and actions to apply to those signals.\n\
dfbd5e7b 7709If no actions are specified, the current settings for the specified signals\n\
486c7739
MF
7710will be displayed instead.\n\
7711\n\
c906108c
SS
7712Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
7713from 1-15 are allowed for compatibility with old versions of GDB.\n\
7714Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
7715The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215 7716used by the debugger, typically SIGTRAP and SIGINT.\n\
486c7739 7717\n\
1bedd215 7718Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
c906108c
SS
7719\"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
7720Stop means reenter debugger if this signal happens (implies print).\n\
7721Print means print a message if this signal happens.\n\
7722Pass means let program see this signal; otherwise program doesn't know.\n\
7723Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
dfbd5e7b
PA
7724Pass and Stop may be combined.\n\
7725\n\
7726Multiple signals may be specified. Signal numbers and signal names\n\
7727may be interspersed with actions, with the actions being performed for\n\
7728all signals cumulatively specified."));
de0bea00 7729 set_cmd_completer (c, handle_completer);
486c7739 7730
c906108c 7731 if (!dbx_commands)
1a966eab
AC
7732 stop_command = add_cmd ("stop", class_obscure,
7733 not_just_help_class_command, _("\
7734There is no `stop' command, but you can set a hook on `stop'.\n\
c906108c 7735This allows you to set a list of commands to be run each time execution\n\
1a966eab 7736of the program stops."), &cmdlist);
c906108c 7737
ccce17b0 7738 add_setshow_zuinteger_cmd ("infrun", class_maintenance, &debug_infrun, _("\
85c07804
AC
7739Set inferior debugging."), _("\
7740Show inferior debugging."), _("\
7741When non-zero, inferior specific debugging is enabled."),
ccce17b0
YQ
7742 NULL,
7743 show_debug_infrun,
7744 &setdebuglist, &showdebuglist);
527159b7 7745
3e43a32a
MS
7746 add_setshow_boolean_cmd ("displaced", class_maintenance,
7747 &debug_displaced, _("\
237fc4c9
PA
7748Set displaced stepping debugging."), _("\
7749Show displaced stepping debugging."), _("\
7750When non-zero, displaced stepping specific debugging is enabled."),
7751 NULL,
7752 show_debug_displaced,
7753 &setdebuglist, &showdebuglist);
7754
ad52ddc6
PA
7755 add_setshow_boolean_cmd ("non-stop", no_class,
7756 &non_stop_1, _("\
7757Set whether gdb controls the inferior in non-stop mode."), _("\
7758Show whether gdb controls the inferior in non-stop mode."), _("\
7759When debugging a multi-threaded program and this setting is\n\
7760off (the default, also called all-stop mode), when one thread stops\n\
7761(for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
7762all other threads in the program while you interact with the thread of\n\
7763interest. When you continue or step a thread, you can allow the other\n\
7764threads to run, or have them remain stopped, but while you inspect any\n\
7765thread's state, all threads stop.\n\
7766\n\
7767In non-stop mode, when one thread stops, other threads can continue\n\
7768to run freely. You'll be able to step each thread independently,\n\
7769leave it stopped or free to run as needed."),
7770 set_non_stop,
7771 show_non_stop,
7772 &setlist,
7773 &showlist);
7774
a493e3e2 7775 numsigs = (int) GDB_SIGNAL_LAST;
488f131b 7776 signal_stop = (unsigned char *) xmalloc (sizeof (signal_stop[0]) * numsigs);
c906108c
SS
7777 signal_print = (unsigned char *)
7778 xmalloc (sizeof (signal_print[0]) * numsigs);
7779 signal_program = (unsigned char *)
7780 xmalloc (sizeof (signal_program[0]) * numsigs);
ab04a2af
TT
7781 signal_catch = (unsigned char *)
7782 xmalloc (sizeof (signal_catch[0]) * numsigs);
2455069d 7783 signal_pass = (unsigned char *)
4395285e 7784 xmalloc (sizeof (signal_pass[0]) * numsigs);
c906108c
SS
7785 for (i = 0; i < numsigs; i++)
7786 {
7787 signal_stop[i] = 1;
7788 signal_print[i] = 1;
7789 signal_program[i] = 1;
ab04a2af 7790 signal_catch[i] = 0;
c906108c
SS
7791 }
7792
7793 /* Signals caused by debugger's own actions
7794 should not be given to the program afterwards. */
a493e3e2
PA
7795 signal_program[GDB_SIGNAL_TRAP] = 0;
7796 signal_program[GDB_SIGNAL_INT] = 0;
c906108c
SS
7797
7798 /* Signals that are not errors should not normally enter the debugger. */
a493e3e2
PA
7799 signal_stop[GDB_SIGNAL_ALRM] = 0;
7800 signal_print[GDB_SIGNAL_ALRM] = 0;
7801 signal_stop[GDB_SIGNAL_VTALRM] = 0;
7802 signal_print[GDB_SIGNAL_VTALRM] = 0;
7803 signal_stop[GDB_SIGNAL_PROF] = 0;
7804 signal_print[GDB_SIGNAL_PROF] = 0;
7805 signal_stop[GDB_SIGNAL_CHLD] = 0;
7806 signal_print[GDB_SIGNAL_CHLD] = 0;
7807 signal_stop[GDB_SIGNAL_IO] = 0;
7808 signal_print[GDB_SIGNAL_IO] = 0;
7809 signal_stop[GDB_SIGNAL_POLL] = 0;
7810 signal_print[GDB_SIGNAL_POLL] = 0;
7811 signal_stop[GDB_SIGNAL_URG] = 0;
7812 signal_print[GDB_SIGNAL_URG] = 0;
7813 signal_stop[GDB_SIGNAL_WINCH] = 0;
7814 signal_print[GDB_SIGNAL_WINCH] = 0;
7815 signal_stop[GDB_SIGNAL_PRIO] = 0;
7816 signal_print[GDB_SIGNAL_PRIO] = 0;
c906108c 7817
cd0fc7c3
SS
7818 /* These signals are used internally by user-level thread
7819 implementations. (See signal(5) on Solaris.) Like the above
7820 signals, a healthy program receives and handles them as part of
7821 its normal operation. */
a493e3e2
PA
7822 signal_stop[GDB_SIGNAL_LWP] = 0;
7823 signal_print[GDB_SIGNAL_LWP] = 0;
7824 signal_stop[GDB_SIGNAL_WAITING] = 0;
7825 signal_print[GDB_SIGNAL_WAITING] = 0;
7826 signal_stop[GDB_SIGNAL_CANCEL] = 0;
7827 signal_print[GDB_SIGNAL_CANCEL] = 0;
cd0fc7c3 7828
2455069d
UW
7829 /* Update cached state. */
7830 signal_cache_update (-1);
7831
85c07804
AC
7832 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
7833 &stop_on_solib_events, _("\
7834Set stopping for shared library events."), _("\
7835Show stopping for shared library events."), _("\
c906108c
SS
7836If nonzero, gdb will give control to the user when the dynamic linker\n\
7837notifies gdb of shared library events. The most common event of interest\n\
85c07804 7838to the user would be loading/unloading of a new library."),
f9e14852 7839 set_stop_on_solib_events,
920d2a44 7840 show_stop_on_solib_events,
85c07804 7841 &setlist, &showlist);
c906108c 7842
7ab04401
AC
7843 add_setshow_enum_cmd ("follow-fork-mode", class_run,
7844 follow_fork_mode_kind_names,
7845 &follow_fork_mode_string, _("\
7846Set debugger response to a program call of fork or vfork."), _("\
7847Show debugger response to a program call of fork or vfork."), _("\
c906108c
SS
7848A fork or vfork creates a new process. follow-fork-mode can be:\n\
7849 parent - the original process is debugged after a fork\n\
7850 child - the new process is debugged after a fork\n\
ea1dd7bc 7851The unfollowed process will continue to run.\n\
7ab04401
AC
7852By default, the debugger will follow the parent process."),
7853 NULL,
920d2a44 7854 show_follow_fork_mode_string,
7ab04401
AC
7855 &setlist, &showlist);
7856
6c95b8df
PA
7857 add_setshow_enum_cmd ("follow-exec-mode", class_run,
7858 follow_exec_mode_names,
7859 &follow_exec_mode_string, _("\
7860Set debugger response to a program call of exec."), _("\
7861Show debugger response to a program call of exec."), _("\
7862An exec call replaces the program image of a process.\n\
7863\n\
7864follow-exec-mode can be:\n\
7865\n\
cce7e648 7866 new - the debugger creates a new inferior and rebinds the process\n\
6c95b8df
PA
7867to this new inferior. The program the process was running before\n\
7868the exec call can be restarted afterwards by restarting the original\n\
7869inferior.\n\
7870\n\
7871 same - the debugger keeps the process bound to the same inferior.\n\
7872The new executable image replaces the previous executable loaded in\n\
7873the inferior. Restarting the inferior after the exec call restarts\n\
7874the executable the process was running after the exec call.\n\
7875\n\
7876By default, the debugger will use the same inferior."),
7877 NULL,
7878 show_follow_exec_mode_string,
7879 &setlist, &showlist);
7880
7ab04401
AC
7881 add_setshow_enum_cmd ("scheduler-locking", class_run,
7882 scheduler_enums, &scheduler_mode, _("\
7883Set mode for locking scheduler during execution."), _("\
7884Show mode for locking scheduler during execution."), _("\
c906108c
SS
7885off == no locking (threads may preempt at any time)\n\
7886on == full locking (no thread except the current thread may run)\n\
856e7dd6
PA
7887step == scheduler locked during stepping commands (step, next, stepi, nexti).\n\
7888 In this mode, other threads may run during other commands."),
7ab04401 7889 set_schedlock_func, /* traps on target vector */
920d2a44 7890 show_scheduler_mode,
7ab04401 7891 &setlist, &showlist);
5fbbeb29 7892
d4db2f36
PA
7893 add_setshow_boolean_cmd ("schedule-multiple", class_run, &sched_multi, _("\
7894Set mode for resuming threads of all processes."), _("\
7895Show mode for resuming threads of all processes."), _("\
7896When on, execution commands (such as 'continue' or 'next') resume all\n\
7897threads of all processes. When off (which is the default), execution\n\
7898commands only resume the threads of the current process. The set of\n\
7899threads that are resumed is further refined by the scheduler-locking\n\
7900mode (see help set scheduler-locking)."),
7901 NULL,
7902 show_schedule_multiple,
7903 &setlist, &showlist);
7904
5bf193a2
AC
7905 add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
7906Set mode of the step operation."), _("\
7907Show mode of the step operation."), _("\
7908When set, doing a step over a function without debug line information\n\
7909will stop at the first instruction of that function. Otherwise, the\n\
7910function is skipped and the step command stops at a different source line."),
7911 NULL,
920d2a44 7912 show_step_stop_if_no_debug,
5bf193a2 7913 &setlist, &showlist);
ca6724c1 7914
72d0e2c5
YQ
7915 add_setshow_auto_boolean_cmd ("displaced-stepping", class_run,
7916 &can_use_displaced_stepping, _("\
237fc4c9
PA
7917Set debugger's willingness to use displaced stepping."), _("\
7918Show debugger's willingness to use displaced stepping."), _("\
fff08868
HZ
7919If on, gdb will use displaced stepping to step over breakpoints if it is\n\
7920supported by the target architecture. If off, gdb will not use displaced\n\
7921stepping to step over breakpoints, even if such is supported by the target\n\
7922architecture. If auto (which is the default), gdb will use displaced stepping\n\
7923if the target architecture supports it and non-stop mode is active, but will not\n\
7924use it in all-stop mode (see help set non-stop)."),
72d0e2c5
YQ
7925 NULL,
7926 show_can_use_displaced_stepping,
7927 &setlist, &showlist);
237fc4c9 7928
b2175913
MS
7929 add_setshow_enum_cmd ("exec-direction", class_run, exec_direction_names,
7930 &exec_direction, _("Set direction of execution.\n\
7931Options are 'forward' or 'reverse'."),
7932 _("Show direction of execution (forward/reverse)."),
7933 _("Tells gdb whether to execute forward or backward."),
7934 set_exec_direction_func, show_exec_direction_func,
7935 &setlist, &showlist);
7936
6c95b8df
PA
7937 /* Set/show detach-on-fork: user-settable mode. */
7938
7939 add_setshow_boolean_cmd ("detach-on-fork", class_run, &detach_fork, _("\
7940Set whether gdb will detach the child of a fork."), _("\
7941Show whether gdb will detach the child of a fork."), _("\
7942Tells gdb whether to detach the child of a fork."),
7943 NULL, NULL, &setlist, &showlist);
7944
03583c20
UW
7945 /* Set/show disable address space randomization mode. */
7946
7947 add_setshow_boolean_cmd ("disable-randomization", class_support,
7948 &disable_randomization, _("\
7949Set disabling of debuggee's virtual address space randomization."), _("\
7950Show disabling of debuggee's virtual address space randomization."), _("\
7951When this mode is on (which is the default), randomization of the virtual\n\
7952address space is disabled. Standalone programs run with the randomization\n\
7953enabled by default on some platforms."),
7954 &set_disable_randomization,
7955 &show_disable_randomization,
7956 &setlist, &showlist);
7957
ca6724c1 7958 /* ptid initializations */
ca6724c1
KB
7959 inferior_ptid = null_ptid;
7960 target_last_wait_ptid = minus_one_ptid;
5231c1fd
PA
7961
7962 observer_attach_thread_ptid_changed (infrun_thread_ptid_changed);
252fbfc8 7963 observer_attach_thread_stop_requested (infrun_thread_stop_requested);
a07daef3 7964 observer_attach_thread_exit (infrun_thread_thread_exit);
fc1cf338 7965 observer_attach_inferior_exit (infrun_inferior_exit);
4aa995e1
PA
7966
7967 /* Explicitly create without lookup, since that tries to create a
7968 value with a void typed value, and when we get here, gdbarch
7969 isn't initialized yet. At this point, we're quite sure there
7970 isn't another convenience variable of the same name. */
22d2b532 7971 create_internalvar_type_lazy ("_siginfo", &siginfo_funcs, NULL);
d914c394
SS
7972
7973 add_setshow_boolean_cmd ("observer", no_class,
7974 &observer_mode_1, _("\
7975Set whether gdb controls the inferior in observer mode."), _("\
7976Show whether gdb controls the inferior in observer mode."), _("\
7977In observer mode, GDB can get data from the inferior, but not\n\
7978affect its execution. Registers and memory may not be changed,\n\
7979breakpoints may not be set, and the program cannot be interrupted\n\
7980or signalled."),
7981 set_observer_mode,
7982 show_observer_mode,
7983 &setlist,
7984 &showlist);
c906108c 7985}
This page took 2.742302 seconds and 4 git commands to generate.