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