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