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