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