gdb: fix handling of vfork by multi-threaded program (follow-fork-mode=parent, detach...
[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)
5785 keep_going (ecs);
5786 else
22bcd14b 5787 stop_waiting (ecs);
04e68871
DJ
5788 return;
5789 }
94c57d6a
PA
5790 process_event_stop_test (ecs);
5791 return;
488f131b 5792
6c95b8df
PA
5793 case TARGET_WAITKIND_VFORK_DONE:
5794 /* Done with the shared memory region. Re-insert breakpoints in
5795 the parent, and keep going. */
5796
00431a78 5797 context_switch (ecs);
6c95b8df 5798
8cc8b4da
SM
5799 handle_vfork_done (ecs->event_thread);
5800 gdb_assert (inferior_thread () == ecs->event_thread);
c65d6b55
PA
5801
5802 if (handle_stop_requested (ecs))
5803 return;
5804
6c95b8df
PA
5805 /* This also takes care of reinserting breakpoints in the
5806 previously locked inferior. */
5807 keep_going (ecs);
5808 return;
5809
488f131b 5810 case TARGET_WAITKIND_EXECD:
488f131b 5811
cbd2b4e3
PA
5812 /* Note we can't read registers yet (the stop_pc), because we
5813 don't yet know the inferior's post-exec architecture.
5814 'stop_pc' is explicitly read below instead. */
00431a78 5815 switch_to_thread_no_regs (ecs->event_thread);
5a2901d9 5816
6c95b8df
PA
5817 /* Do whatever is necessary to the parent branch of the vfork. */
5818 handle_vfork_child_exec_or_exit (1);
5819
795e548f 5820 /* This causes the eventpoints and symbol table to be reset.
dda83cd7
SM
5821 Must do this now, before trying to determine whether to
5822 stop. */
71b43ef8 5823 follow_exec (inferior_ptid, ecs->ws.value.execd_pathname);
795e548f 5824
17d8546e
DB
5825 /* In follow_exec we may have deleted the original thread and
5826 created a new one. Make sure that the event thread is the
5827 execd thread for that case (this is a nop otherwise). */
5828 ecs->event_thread = inferior_thread ();
5829
f2ffa92b
PA
5830 ecs->event_thread->suspend.stop_pc
5831 = regcache_read_pc (get_thread_regcache (ecs->event_thread));
ecdc3a72 5832
16c381f0 5833 ecs->event_thread->control.stop_bpstat
a01bda52 5834 = bpstat_stop_status (get_current_regcache ()->aspace (),
f2ffa92b
PA
5835 ecs->event_thread->suspend.stop_pc,
5836 ecs->event_thread, &ecs->ws);
795e548f 5837
71b43ef8
PA
5838 /* Note that this may be referenced from inside
5839 bpstat_stop_status above, through inferior_has_execd. */
5840 xfree (ecs->ws.value.execd_pathname);
5841 ecs->ws.value.execd_pathname = NULL;
5842
c65d6b55
PA
5843 if (handle_stop_requested (ecs))
5844 return;
5845
04e68871 5846 /* If no catchpoint triggered for this, then keep going. */
ce12b012 5847 if (!bpstat_causes_stop (ecs->event_thread->control.stop_bpstat))
04e68871 5848 {
a493e3e2 5849 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
04e68871
DJ
5850 keep_going (ecs);
5851 return;
5852 }
94c57d6a
PA
5853 process_event_stop_test (ecs);
5854 return;
488f131b 5855
b4dc5ffa 5856 /* Be careful not to try to gather much state about a thread
dda83cd7 5857 that's in a syscall. It's frequently a losing proposition. */
488f131b 5858 case TARGET_WAITKIND_SYSCALL_ENTRY:
1777feb0 5859 /* Getting the current syscall number. */
94c57d6a
PA
5860 if (handle_syscall_event (ecs) == 0)
5861 process_event_stop_test (ecs);
5862 return;
c906108c 5863
488f131b 5864 /* Before examining the threads further, step this thread to
dda83cd7
SM
5865 get it entirely out of the syscall. (We get notice of the
5866 event when the thread is just on the verge of exiting a
5867 syscall. Stepping one instruction seems to get it back
5868 into user code.) */
488f131b 5869 case TARGET_WAITKIND_SYSCALL_RETURN:
94c57d6a
PA
5870 if (handle_syscall_event (ecs) == 0)
5871 process_event_stop_test (ecs);
5872 return;
c906108c 5873
488f131b 5874 case TARGET_WAITKIND_STOPPED:
4f5d7f63
PA
5875 handle_signal_stop (ecs);
5876 return;
c906108c 5877
b2175913
MS
5878 case TARGET_WAITKIND_NO_HISTORY:
5879 /* Reverse execution: target ran out of history info. */
eab402df 5880
d1988021 5881 /* Switch to the stopped thread. */
00431a78 5882 context_switch (ecs);
1eb8556f 5883 infrun_debug_printf ("stopped");
d1988021 5884
34b7e8a6 5885 delete_just_stopped_threads_single_step_breakpoints ();
f2ffa92b
PA
5886 ecs->event_thread->suspend.stop_pc
5887 = regcache_read_pc (get_thread_regcache (inferior_thread ()));
c65d6b55
PA
5888
5889 if (handle_stop_requested (ecs))
5890 return;
5891
76727919 5892 gdb::observers::no_history.notify ();
22bcd14b 5893 stop_waiting (ecs);
b2175913 5894 return;
488f131b 5895 }
4f5d7f63
PA
5896}
5897
372316f1 5898/* Restart threads back to what they were trying to do back when we
8631fd9b
SM
5899 paused them (because of an in-line step-over or vfork, for example).
5900 The EVENT_THREAD thread is ignored (not restarted).
5901
5902 If INF is non-nullptr, only resume threads from INF. */
4d9d9d04
PA
5903
5904static void
8631fd9b 5905restart_threads (struct thread_info *event_thread, inferior *inf)
372316f1 5906{
8631fd9b
SM
5907 INFRUN_SCOPED_DEBUG_START_END ("event_thread=%s, inf=%d",
5908 event_thread->ptid.to_string ().c_str (),
5909 inf != nullptr ? inf->num : -1);
5910
372316f1
PA
5911 /* In case the instruction just stepped spawned a new thread. */
5912 update_thread_list ();
5913
08036331 5914 for (thread_info *tp : all_non_exited_threads ())
372316f1 5915 {
8631fd9b
SM
5916 if (inf != nullptr && tp->inf != inf)
5917 continue;
5918
ac7d717c
PA
5919 if (tp->inf->detaching)
5920 {
5921 infrun_debug_printf ("restart threads: [%s] inferior detaching",
5922 target_pid_to_str (tp->ptid).c_str ());
5923 continue;
5924 }
5925
f3f8ece4
PA
5926 switch_to_thread_no_regs (tp);
5927
372316f1
PA
5928 if (tp == event_thread)
5929 {
1eb8556f
SM
5930 infrun_debug_printf ("restart threads: [%s] is event thread",
5931 target_pid_to_str (tp->ptid).c_str ());
372316f1
PA
5932 continue;
5933 }
5934
5935 if (!(tp->state == THREAD_RUNNING || tp->control.in_infcall))
5936 {
1eb8556f
SM
5937 infrun_debug_printf ("restart threads: [%s] not meant to be running",
5938 target_pid_to_str (tp->ptid).c_str ());
372316f1
PA
5939 continue;
5940 }
5941
5942 if (tp->resumed)
5943 {
1eb8556f
SM
5944 infrun_debug_printf ("restart threads: [%s] resumed",
5945 target_pid_to_str (tp->ptid).c_str ());
372316f1
PA
5946 gdb_assert (tp->executing || tp->suspend.waitstatus_pending_p);
5947 continue;
5948 }
5949
5950 if (thread_is_in_step_over_chain (tp))
5951 {
1eb8556f
SM
5952 infrun_debug_printf ("restart threads: [%s] needs step-over",
5953 target_pid_to_str (tp->ptid).c_str ());
372316f1
PA
5954 gdb_assert (!tp->resumed);
5955 continue;
5956 }
5957
5958
5959 if (tp->suspend.waitstatus_pending_p)
5960 {
1eb8556f
SM
5961 infrun_debug_printf ("restart threads: [%s] has pending status",
5962 target_pid_to_str (tp->ptid).c_str ());
719546c4 5963 tp->resumed = true;
372316f1
PA
5964 continue;
5965 }
5966
c65d6b55
PA
5967 gdb_assert (!tp->stop_requested);
5968
372316f1
PA
5969 /* If some thread needs to start a step-over at this point, it
5970 should still be in the step-over queue, and thus skipped
5971 above. */
5972 if (thread_still_needs_step_over (tp))
5973 {
5974 internal_error (__FILE__, __LINE__,
5975 "thread [%s] needs a step-over, but not in "
5976 "step-over queue\n",
a068643d 5977 target_pid_to_str (tp->ptid).c_str ());
372316f1
PA
5978 }
5979
5980 if (currently_stepping (tp))
5981 {
1eb8556f
SM
5982 infrun_debug_printf ("restart threads: [%s] was stepping",
5983 target_pid_to_str (tp->ptid).c_str ());
372316f1
PA
5984 keep_going_stepped_thread (tp);
5985 }
5986 else
5987 {
5988 struct execution_control_state ecss;
5989 struct execution_control_state *ecs = &ecss;
5990
1eb8556f
SM
5991 infrun_debug_printf ("restart threads: [%s] continuing",
5992 target_pid_to_str (tp->ptid).c_str ());
372316f1 5993 reset_ecs (ecs, tp);
00431a78 5994 switch_to_thread (tp);
372316f1
PA
5995 keep_going_pass_signal (ecs);
5996 }
5997 }
5998}
5999
6000/* Callback for iterate_over_threads. Find a resumed thread that has
6001 a pending waitstatus. */
6002
6003static int
6004resumed_thread_with_pending_status (struct thread_info *tp,
6005 void *arg)
6006{
6007 return (tp->resumed
6008 && tp->suspend.waitstatus_pending_p);
6009}
6010
6011/* Called when we get an event that may finish an in-line or
6012 out-of-line (displaced stepping) step-over started previously.
6013 Return true if the event is processed and we should go back to the
6014 event loop; false if the caller should continue processing the
6015 event. */
6016
6017static int
4d9d9d04
PA
6018finish_step_over (struct execution_control_state *ecs)
6019{
7def77a1
SM
6020 displaced_step_finish (ecs->event_thread,
6021 ecs->event_thread->suspend.stop_signal);
4d9d9d04 6022
c4464ade 6023 bool had_step_over_info = step_over_info_valid_p ();
372316f1
PA
6024
6025 if (had_step_over_info)
4d9d9d04
PA
6026 {
6027 /* If we're stepping over a breakpoint with all threads locked,
6028 then only the thread that was stepped should be reporting
6029 back an event. */
6030 gdb_assert (ecs->event_thread->control.trap_expected);
6031
c65d6b55 6032 clear_step_over_info ();
4d9d9d04
PA
6033 }
6034
fbea99ea 6035 if (!target_is_non_stop_p ())
372316f1 6036 return 0;
4d9d9d04
PA
6037
6038 /* Start a new step-over in another thread if there's one that
6039 needs it. */
6040 start_step_over ();
372316f1
PA
6041
6042 /* If we were stepping over a breakpoint before, and haven't started
6043 a new in-line step-over sequence, then restart all other threads
6044 (except the event thread). We can't do this in all-stop, as then
6045 e.g., we wouldn't be able to issue any other remote packet until
6046 these other threads stop. */
6047 if (had_step_over_info && !step_over_info_valid_p ())
6048 {
6049 struct thread_info *pending;
6050
6051 /* If we only have threads with pending statuses, the restart
6052 below won't restart any thread and so nothing re-inserts the
6053 breakpoint we just stepped over. But we need it inserted
6054 when we later process the pending events, otherwise if
6055 another thread has a pending event for this breakpoint too,
6056 we'd discard its event (because the breakpoint that
6057 originally caused the event was no longer inserted). */
00431a78 6058 context_switch (ecs);
372316f1
PA
6059 insert_breakpoints ();
6060
6061 restart_threads (ecs->event_thread);
6062
6063 /* If we have events pending, go through handle_inferior_event
6064 again, picking up a pending event at random. This avoids
6065 thread starvation. */
6066
6067 /* But not if we just stepped over a watchpoint in order to let
6068 the instruction execute so we can evaluate its expression.
6069 The set of watchpoints that triggered is recorded in the
6070 breakpoint objects themselves (see bp->watchpoint_triggered).
6071 If we processed another event first, that other event could
6072 clobber this info. */
6073 if (ecs->event_thread->stepping_over_watchpoint)
6074 return 0;
6075
6076 pending = iterate_over_threads (resumed_thread_with_pending_status,
6077 NULL);
6078 if (pending != NULL)
6079 {
6080 struct thread_info *tp = ecs->event_thread;
6081 struct regcache *regcache;
6082
1eb8556f
SM
6083 infrun_debug_printf ("found resumed threads with "
6084 "pending events, saving status");
372316f1
PA
6085
6086 gdb_assert (pending != tp);
6087
6088 /* Record the event thread's event for later. */
6089 save_waitstatus (tp, &ecs->ws);
6090 /* This was cleared early, by handle_inferior_event. Set it
6091 so this pending event is considered by
6092 do_target_wait. */
719546c4 6093 tp->resumed = true;
372316f1
PA
6094
6095 gdb_assert (!tp->executing);
6096
00431a78 6097 regcache = get_thread_regcache (tp);
372316f1
PA
6098 tp->suspend.stop_pc = regcache_read_pc (regcache);
6099
1eb8556f
SM
6100 infrun_debug_printf ("saved stop_pc=%s for %s "
6101 "(currently_stepping=%d)",
6102 paddress (target_gdbarch (),
dda83cd7 6103 tp->suspend.stop_pc),
1eb8556f
SM
6104 target_pid_to_str (tp->ptid).c_str (),
6105 currently_stepping (tp));
372316f1
PA
6106
6107 /* This in-line step-over finished; clear this so we won't
6108 start a new one. This is what handle_signal_stop would
6109 do, if we returned false. */
6110 tp->stepping_over_breakpoint = 0;
6111
6112 /* Wake up the event loop again. */
6113 mark_async_event_handler (infrun_async_inferior_event_token);
6114
6115 prepare_to_wait (ecs);
6116 return 1;
6117 }
6118 }
6119
6120 return 0;
4d9d9d04
PA
6121}
6122
4f5d7f63
PA
6123/* Come here when the program has stopped with a signal. */
6124
6125static void
6126handle_signal_stop (struct execution_control_state *ecs)
6127{
6128 struct frame_info *frame;
6129 struct gdbarch *gdbarch;
6130 int stopped_by_watchpoint;
6131 enum stop_kind stop_soon;
6132 int random_signal;
c906108c 6133
f0407826
DE
6134 gdb_assert (ecs->ws.kind == TARGET_WAITKIND_STOPPED);
6135
c65d6b55
PA
6136 ecs->event_thread->suspend.stop_signal = ecs->ws.value.sig;
6137
f0407826
DE
6138 /* Do we need to clean up the state of a thread that has
6139 completed a displaced single-step? (Doing so usually affects
6140 the PC, so do it here, before we set stop_pc.) */
372316f1
PA
6141 if (finish_step_over (ecs))
6142 return;
f0407826
DE
6143
6144 /* If we either finished a single-step or hit a breakpoint, but
6145 the user wanted this thread to be stopped, pretend we got a
6146 SIG0 (generic unsignaled stop). */
6147 if (ecs->event_thread->stop_requested
6148 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
6149 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
237fc4c9 6150
f2ffa92b
PA
6151 ecs->event_thread->suspend.stop_pc
6152 = regcache_read_pc (get_thread_regcache (ecs->event_thread));
488f131b 6153
2ab76a18
PA
6154 context_switch (ecs);
6155
6156 if (deprecated_context_hook)
6157 deprecated_context_hook (ecs->event_thread->global_num);
6158
527159b7 6159 if (debug_infrun)
237fc4c9 6160 {
00431a78 6161 struct regcache *regcache = get_thread_regcache (ecs->event_thread);
b926417a 6162 struct gdbarch *reg_gdbarch = regcache->arch ();
7f82dfc7 6163
1eb8556f
SM
6164 infrun_debug_printf ("stop_pc=%s",
6165 paddress (reg_gdbarch,
6166 ecs->event_thread->suspend.stop_pc));
d92524f1 6167 if (target_stopped_by_watchpoint ())
237fc4c9 6168 {
dda83cd7 6169 CORE_ADDR addr;
abbb1732 6170
1eb8556f 6171 infrun_debug_printf ("stopped by watchpoint");
237fc4c9 6172
328d42d8
SM
6173 if (target_stopped_data_address (current_inferior ()->top_target (),
6174 &addr))
1eb8556f 6175 infrun_debug_printf ("stopped data address=%s",
dda83cd7
SM
6176 paddress (reg_gdbarch, addr));
6177 else
1eb8556f 6178 infrun_debug_printf ("(no data address available)");
237fc4c9
PA
6179 }
6180 }
527159b7 6181
36fa8042
PA
6182 /* This is originated from start_remote(), start_inferior() and
6183 shared libraries hook functions. */
00431a78 6184 stop_soon = get_inferior_stop_soon (ecs);
36fa8042
PA
6185 if (stop_soon == STOP_QUIETLY || stop_soon == STOP_QUIETLY_REMOTE)
6186 {
1eb8556f 6187 infrun_debug_printf ("quietly stopped");
c4464ade 6188 stop_print_frame = true;
22bcd14b 6189 stop_waiting (ecs);
36fa8042
PA
6190 return;
6191 }
6192
36fa8042
PA
6193 /* This originates from attach_command(). We need to overwrite
6194 the stop_signal here, because some kernels don't ignore a
6195 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
6196 See more comments in inferior.h. On the other hand, if we
6197 get a non-SIGSTOP, report it to the user - assume the backend
6198 will handle the SIGSTOP if it should show up later.
6199
6200 Also consider that the attach is complete when we see a
6201 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
6202 target extended-remote report it instead of a SIGSTOP
6203 (e.g. gdbserver). We already rely on SIGTRAP being our
6204 signal, so this is no exception.
6205
6206 Also consider that the attach is complete when we see a
6207 GDB_SIGNAL_0. In non-stop mode, GDB will explicitly tell
6208 the target to stop all threads of the inferior, in case the
6209 low level attach operation doesn't stop them implicitly. If
6210 they weren't stopped implicitly, then the stub will report a
6211 GDB_SIGNAL_0, meaning: stopped for no particular reason
6212 other than GDB's request. */
6213 if (stop_soon == STOP_QUIETLY_NO_SIGSTOP
6214 && (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_STOP
6215 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
6216 || ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_0))
6217 {
c4464ade 6218 stop_print_frame = true;
22bcd14b 6219 stop_waiting (ecs);
36fa8042
PA
6220 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
6221 return;
6222 }
6223
568d6575
UW
6224 /* At this point, get hold of the now-current thread's frame. */
6225 frame = get_current_frame ();
6226 gdbarch = get_frame_arch (frame);
6227
2adfaa28 6228 /* Pull the single step breakpoints out of the target. */
af48d08f 6229 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
488f131b 6230 {
af48d08f 6231 struct regcache *regcache;
af48d08f 6232 CORE_ADDR pc;
2adfaa28 6233
00431a78 6234 regcache = get_thread_regcache (ecs->event_thread);
8b86c959
YQ
6235 const address_space *aspace = regcache->aspace ();
6236
af48d08f 6237 pc = regcache_read_pc (regcache);
34b7e8a6 6238
af48d08f
PA
6239 /* However, before doing so, if this single-step breakpoint was
6240 actually for another thread, set this thread up for moving
6241 past it. */
6242 if (!thread_has_single_step_breakpoint_here (ecs->event_thread,
6243 aspace, pc))
6244 {
6245 if (single_step_breakpoint_inserted_here_p (aspace, pc))
2adfaa28 6246 {
1eb8556f
SM
6247 infrun_debug_printf ("[%s] hit another thread's single-step "
6248 "breakpoint",
6249 target_pid_to_str (ecs->ptid).c_str ());
af48d08f
PA
6250 ecs->hit_singlestep_breakpoint = 1;
6251 }
6252 }
6253 else
6254 {
1eb8556f
SM
6255 infrun_debug_printf ("[%s] hit its single-step breakpoint",
6256 target_pid_to_str (ecs->ptid).c_str ());
2adfaa28 6257 }
488f131b 6258 }
af48d08f 6259 delete_just_stopped_threads_single_step_breakpoints ();
c906108c 6260
963f9c80
PA
6261 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
6262 && ecs->event_thread->control.trap_expected
6263 && ecs->event_thread->stepping_over_watchpoint)
d983da9c
DJ
6264 stopped_by_watchpoint = 0;
6265 else
6266 stopped_by_watchpoint = watchpoints_triggered (&ecs->ws);
6267
6268 /* If necessary, step over this watchpoint. We'll be back to display
6269 it in a moment. */
6270 if (stopped_by_watchpoint
9aed480c 6271 && (target_have_steppable_watchpoint ()
568d6575 6272 || gdbarch_have_nonsteppable_watchpoint (gdbarch)))
488f131b 6273 {
488f131b 6274 /* At this point, we are stopped at an instruction which has
dda83cd7
SM
6275 attempted to write to a piece of memory under control of
6276 a watchpoint. The instruction hasn't actually executed
6277 yet. If we were to evaluate the watchpoint expression
6278 now, we would get the old value, and therefore no change
6279 would seem to have occurred.
6280
6281 In order to make watchpoints work `right', we really need
6282 to complete the memory write, and then evaluate the
6283 watchpoint expression. We do this by single-stepping the
d983da9c
DJ
6284 target.
6285
7f89fd65 6286 It may not be necessary to disable the watchpoint to step over
d983da9c
DJ
6287 it. For example, the PA can (with some kernel cooperation)
6288 single step over a watchpoint without disabling the watchpoint.
6289
6290 It is far more common to need to disable a watchpoint to step
6291 the inferior over it. If we have non-steppable watchpoints,
6292 we must disable the current watchpoint; it's simplest to
963f9c80
PA
6293 disable all watchpoints.
6294
6295 Any breakpoint at PC must also be stepped over -- if there's
6296 one, it will have already triggered before the watchpoint
6297 triggered, and we either already reported it to the user, or
6298 it didn't cause a stop and we called keep_going. In either
6299 case, if there was a breakpoint at PC, we must be trying to
6300 step past it. */
6301 ecs->event_thread->stepping_over_watchpoint = 1;
6302 keep_going (ecs);
488f131b
JB
6303 return;
6304 }
6305
4e1c45ea 6306 ecs->event_thread->stepping_over_breakpoint = 0;
963f9c80 6307 ecs->event_thread->stepping_over_watchpoint = 0;
16c381f0
JK
6308 bpstat_clear (&ecs->event_thread->control.stop_bpstat);
6309 ecs->event_thread->control.stop_step = 0;
c4464ade 6310 stop_print_frame = true;
488f131b 6311 stopped_by_random_signal = 0;
ddfe970e 6312 bpstat stop_chain = NULL;
488f131b 6313
edb3359d
DJ
6314 /* Hide inlined functions starting here, unless we just performed stepi or
6315 nexti. After stepi and nexti, always show the innermost frame (not any
6316 inline function call sites). */
16c381f0 6317 if (ecs->event_thread->control.step_range_end != 1)
0574c78f 6318 {
00431a78
PA
6319 const address_space *aspace
6320 = get_thread_regcache (ecs->event_thread)->aspace ();
0574c78f
GB
6321
6322 /* skip_inline_frames is expensive, so we avoid it if we can
6323 determine that the address is one where functions cannot have
6324 been inlined. This improves performance with inferiors that
6325 load a lot of shared libraries, because the solib event
6326 breakpoint is defined as the address of a function (i.e. not
6327 inline). Note that we have to check the previous PC as well
6328 as the current one to catch cases when we have just
6329 single-stepped off a breakpoint prior to reinstating it.
6330 Note that we're assuming that the code we single-step to is
6331 not inline, but that's not definitive: there's nothing
6332 preventing the event breakpoint function from containing
6333 inlined code, and the single-step ending up there. If the
6334 user had set a breakpoint on that inlined code, the missing
6335 skip_inline_frames call would break things. Fortunately
6336 that's an extremely unlikely scenario. */
f2ffa92b
PA
6337 if (!pc_at_non_inline_function (aspace,
6338 ecs->event_thread->suspend.stop_pc,
6339 &ecs->ws)
a210c238
MR
6340 && !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
6341 && ecs->event_thread->control.trap_expected
6342 && pc_at_non_inline_function (aspace,
6343 ecs->event_thread->prev_pc,
09ac7c10 6344 &ecs->ws)))
1c5a993e 6345 {
f2ffa92b
PA
6346 stop_chain = build_bpstat_chain (aspace,
6347 ecs->event_thread->suspend.stop_pc,
6348 &ecs->ws);
00431a78 6349 skip_inline_frames (ecs->event_thread, stop_chain);
1c5a993e
MR
6350
6351 /* Re-fetch current thread's frame in case that invalidated
6352 the frame cache. */
6353 frame = get_current_frame ();
6354 gdbarch = get_frame_arch (frame);
6355 }
0574c78f 6356 }
edb3359d 6357
a493e3e2 6358 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
16c381f0 6359 && ecs->event_thread->control.trap_expected
568d6575 6360 && gdbarch_single_step_through_delay_p (gdbarch)
4e1c45ea 6361 && currently_stepping (ecs->event_thread))
3352ef37 6362 {
b50d7442 6363 /* We're trying to step off a breakpoint. Turns out that we're
3352ef37 6364 also on an instruction that needs to be stepped multiple
1777feb0 6365 times before it's been fully executing. E.g., architectures
3352ef37
AC
6366 with a delay slot. It needs to be stepped twice, once for
6367 the instruction and once for the delay slot. */
6368 int step_through_delay
568d6575 6369 = gdbarch_single_step_through_delay (gdbarch, frame);
abbb1732 6370
1eb8556f
SM
6371 if (step_through_delay)
6372 infrun_debug_printf ("step through delay");
6373
16c381f0
JK
6374 if (ecs->event_thread->control.step_range_end == 0
6375 && step_through_delay)
3352ef37
AC
6376 {
6377 /* The user issued a continue when stopped at a breakpoint.
6378 Set up for another trap and get out of here. */
dda83cd7
SM
6379 ecs->event_thread->stepping_over_breakpoint = 1;
6380 keep_going (ecs);
6381 return;
3352ef37
AC
6382 }
6383 else if (step_through_delay)
6384 {
6385 /* The user issued a step when stopped at a breakpoint.
6386 Maybe we should stop, maybe we should not - the delay
6387 slot *might* correspond to a line of source. In any
ca67fcb8
VP
6388 case, don't decide that here, just set
6389 ecs->stepping_over_breakpoint, making sure we
6390 single-step again before breakpoints are re-inserted. */
4e1c45ea 6391 ecs->event_thread->stepping_over_breakpoint = 1;
3352ef37
AC
6392 }
6393 }
6394
ab04a2af
TT
6395 /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
6396 handles this event. */
6397 ecs->event_thread->control.stop_bpstat
a01bda52 6398 = bpstat_stop_status (get_current_regcache ()->aspace (),
f2ffa92b
PA
6399 ecs->event_thread->suspend.stop_pc,
6400 ecs->event_thread, &ecs->ws, stop_chain);
db82e815 6401
ab04a2af
TT
6402 /* Following in case break condition called a
6403 function. */
c4464ade 6404 stop_print_frame = true;
73dd234f 6405
ab04a2af
TT
6406 /* This is where we handle "moribund" watchpoints. Unlike
6407 software breakpoints traps, hardware watchpoint traps are
6408 always distinguishable from random traps. If no high-level
6409 watchpoint is associated with the reported stop data address
6410 anymore, then the bpstat does not explain the signal ---
6411 simply make sure to ignore it if `stopped_by_watchpoint' is
6412 set. */
6413
1eb8556f 6414 if (ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
47591c29 6415 && !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
427cd150 6416 GDB_SIGNAL_TRAP)
ab04a2af 6417 && stopped_by_watchpoint)
1eb8556f
SM
6418 {
6419 infrun_debug_printf ("no user watchpoint explains watchpoint SIGTRAP, "
6420 "ignoring");
6421 }
73dd234f 6422
bac7d97b 6423 /* NOTE: cagney/2003-03-29: These checks for a random signal
ab04a2af
TT
6424 at one stage in the past included checks for an inferior
6425 function call's call dummy's return breakpoint. The original
6426 comment, that went with the test, read:
03cebad2 6427
ab04a2af
TT
6428 ``End of a stack dummy. Some systems (e.g. Sony news) give
6429 another signal besides SIGTRAP, so check here as well as
6430 above.''
73dd234f 6431
ab04a2af
TT
6432 If someone ever tries to get call dummys on a
6433 non-executable stack to work (where the target would stop
6434 with something like a SIGSEGV), then those tests might need
6435 to be re-instated. Given, however, that the tests were only
6436 enabled when momentary breakpoints were not being used, I
6437 suspect that it won't be the case.
488f131b 6438
ab04a2af
TT
6439 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
6440 be necessary for call dummies on a non-executable stack on
6441 SPARC. */
488f131b 6442
bac7d97b 6443 /* See if the breakpoints module can explain the signal. */
47591c29
PA
6444 random_signal
6445 = !bpstat_explains_signal (ecs->event_thread->control.stop_bpstat,
6446 ecs->event_thread->suspend.stop_signal);
bac7d97b 6447
1cf4d951
PA
6448 /* Maybe this was a trap for a software breakpoint that has since
6449 been removed. */
6450 if (random_signal && target_stopped_by_sw_breakpoint ())
6451 {
5133a315
LM
6452 if (gdbarch_program_breakpoint_here_p (gdbarch,
6453 ecs->event_thread->suspend.stop_pc))
1cf4d951
PA
6454 {
6455 struct regcache *regcache;
6456 int decr_pc;
6457
6458 /* Re-adjust PC to what the program would see if GDB was not
6459 debugging it. */
00431a78 6460 regcache = get_thread_regcache (ecs->event_thread);
527a273a 6461 decr_pc = gdbarch_decr_pc_after_break (gdbarch);
1cf4d951
PA
6462 if (decr_pc != 0)
6463 {
07036511
TT
6464 gdb::optional<scoped_restore_tmpl<int>>
6465 restore_operation_disable;
1cf4d951
PA
6466
6467 if (record_full_is_used ())
07036511
TT
6468 restore_operation_disable.emplace
6469 (record_full_gdb_operation_disable_set ());
1cf4d951 6470
f2ffa92b
PA
6471 regcache_write_pc (regcache,
6472 ecs->event_thread->suspend.stop_pc + decr_pc);
1cf4d951
PA
6473 }
6474 }
6475 else
6476 {
6477 /* A delayed software breakpoint event. Ignore the trap. */
1eb8556f 6478 infrun_debug_printf ("delayed software breakpoint trap, ignoring");
1cf4d951
PA
6479 random_signal = 0;
6480 }
6481 }
6482
6483 /* Maybe this was a trap for a hardware breakpoint/watchpoint that
6484 has since been removed. */
6485 if (random_signal && target_stopped_by_hw_breakpoint ())
6486 {
6487 /* A delayed hardware breakpoint event. Ignore the trap. */
1eb8556f
SM
6488 infrun_debug_printf ("delayed hardware breakpoint/watchpoint "
6489 "trap, ignoring");
1cf4d951
PA
6490 random_signal = 0;
6491 }
6492
bac7d97b
PA
6493 /* If not, perhaps stepping/nexting can. */
6494 if (random_signal)
6495 random_signal = !(ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP
6496 && currently_stepping (ecs->event_thread));
ab04a2af 6497
2adfaa28
PA
6498 /* Perhaps the thread hit a single-step breakpoint of _another_
6499 thread. Single-step breakpoints are transparent to the
6500 breakpoints module. */
6501 if (random_signal)
6502 random_signal = !ecs->hit_singlestep_breakpoint;
6503
bac7d97b
PA
6504 /* No? Perhaps we got a moribund watchpoint. */
6505 if (random_signal)
6506 random_signal = !stopped_by_watchpoint;
ab04a2af 6507
c65d6b55
PA
6508 /* Always stop if the user explicitly requested this thread to
6509 remain stopped. */
6510 if (ecs->event_thread->stop_requested)
6511 {
6512 random_signal = 1;
1eb8556f 6513 infrun_debug_printf ("user-requested stop");
c65d6b55
PA
6514 }
6515
488f131b
JB
6516 /* For the program's own signals, act according to
6517 the signal handling tables. */
6518
ce12b012 6519 if (random_signal)
488f131b
JB
6520 {
6521 /* Signal not for debugging purposes. */
c9737c08 6522 enum gdb_signal stop_signal = ecs->event_thread->suspend.stop_signal;
488f131b 6523
1eb8556f
SM
6524 infrun_debug_printf ("random signal (%s)",
6525 gdb_signal_to_symbol_string (stop_signal));
527159b7 6526
488f131b
JB
6527 stopped_by_random_signal = 1;
6528
252fbfc8
PA
6529 /* Always stop on signals if we're either just gaining control
6530 of the program, or the user explicitly requested this thread
6531 to remain stopped. */
d6b48e9c 6532 if (stop_soon != NO_STOP_QUIETLY
252fbfc8 6533 || ecs->event_thread->stop_requested
8ff53139 6534 || signal_stop_state (ecs->event_thread->suspend.stop_signal))
488f131b 6535 {
22bcd14b 6536 stop_waiting (ecs);
488f131b
JB
6537 return;
6538 }
b57bacec
PA
6539
6540 /* Notify observers the signal has "handle print" set. Note we
6541 returned early above if stopping; normal_stop handles the
6542 printing in that case. */
6543 if (signal_print[ecs->event_thread->suspend.stop_signal])
6544 {
6545 /* The signal table tells us to print about this signal. */
223ffa71 6546 target_terminal::ours_for_output ();
76727919 6547 gdb::observers::signal_received.notify (ecs->event_thread->suspend.stop_signal);
223ffa71 6548 target_terminal::inferior ();
b57bacec 6549 }
488f131b
JB
6550
6551 /* Clear the signal if it should not be passed. */
16c381f0 6552 if (signal_program[ecs->event_thread->suspend.stop_signal] == 0)
a493e3e2 6553 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
488f131b 6554
f2ffa92b 6555 if (ecs->event_thread->prev_pc == ecs->event_thread->suspend.stop_pc
16c381f0 6556 && ecs->event_thread->control.trap_expected
8358c15c 6557 && ecs->event_thread->control.step_resume_breakpoint == NULL)
68f53502
AC
6558 {
6559 /* We were just starting a new sequence, attempting to
6560 single-step off of a breakpoint and expecting a SIGTRAP.
237fc4c9 6561 Instead this signal arrives. This signal will take us out
68f53502
AC
6562 of the stepping range so GDB needs to remember to, when
6563 the signal handler returns, resume stepping off that
6564 breakpoint. */
6565 /* To simplify things, "continue" is forced to use the same
6566 code paths as single-step - set a breakpoint at the
6567 signal return address and then, once hit, step off that
6568 breakpoint. */
1eb8556f 6569 infrun_debug_printf ("signal arrived while stepping over breakpoint");
d3169d93 6570
2c03e5be 6571 insert_hp_step_resume_breakpoint_at_frame (frame);
4e1c45ea 6572 ecs->event_thread->step_after_step_resume_breakpoint = 1;
2455069d
UW
6573 /* Reset trap_expected to ensure breakpoints are re-inserted. */
6574 ecs->event_thread->control.trap_expected = 0;
d137e6dc
PA
6575
6576 /* If we were nexting/stepping some other thread, switch to
6577 it, so that we don't continue it, losing control. */
6578 if (!switch_back_to_stepped_thread (ecs))
6579 keep_going (ecs);
9d799f85 6580 return;
68f53502 6581 }
9d799f85 6582
e5f8a7cc 6583 if (ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_0
f2ffa92b
PA
6584 && (pc_in_thread_step_range (ecs->event_thread->suspend.stop_pc,
6585 ecs->event_thread)
e5f8a7cc 6586 || ecs->event_thread->control.step_range_end == 1)
edb3359d 6587 && frame_id_eq (get_stack_frame_id (frame),
16c381f0 6588 ecs->event_thread->control.step_stack_frame_id)
8358c15c 6589 && ecs->event_thread->control.step_resume_breakpoint == NULL)
d303a6c7
AC
6590 {
6591 /* The inferior is about to take a signal that will take it
6592 out of the single step range. Set a breakpoint at the
6593 current PC (which is presumably where the signal handler
6594 will eventually return) and then allow the inferior to
6595 run free.
6596
6597 Note that this is only needed for a signal delivered
6598 while in the single-step range. Nested signals aren't a
6599 problem as they eventually all return. */
1eb8556f 6600 infrun_debug_printf ("signal may take us out of single-step range");
237fc4c9 6601
372316f1 6602 clear_step_over_info ();
2c03e5be 6603 insert_hp_step_resume_breakpoint_at_frame (frame);
e5f8a7cc 6604 ecs->event_thread->step_after_step_resume_breakpoint = 1;
2455069d
UW
6605 /* Reset trap_expected to ensure breakpoints are re-inserted. */
6606 ecs->event_thread->control.trap_expected = 0;
9d799f85
AC
6607 keep_going (ecs);
6608 return;
d303a6c7 6609 }
9d799f85 6610
85102364 6611 /* Note: step_resume_breakpoint may be non-NULL. This occurs
9d799f85
AC
6612 when either there's a nested signal, or when there's a
6613 pending signal enabled just as the signal handler returns
6614 (leaving the inferior at the step-resume-breakpoint without
6615 actually executing it). Either way continue until the
6616 breakpoint is really hit. */
c447ac0b
PA
6617
6618 if (!switch_back_to_stepped_thread (ecs))
6619 {
1eb8556f 6620 infrun_debug_printf ("random signal, keep going");
c447ac0b
PA
6621
6622 keep_going (ecs);
6623 }
6624 return;
488f131b 6625 }
94c57d6a
PA
6626
6627 process_event_stop_test (ecs);
6628}
6629
6630/* Come here when we've got some debug event / signal we can explain
6631 (IOW, not a random signal), and test whether it should cause a
6632 stop, or whether we should resume the inferior (transparently).
6633 E.g., could be a breakpoint whose condition evaluates false; we
6634 could be still stepping within the line; etc. */
6635
6636static void
6637process_event_stop_test (struct execution_control_state *ecs)
6638{
6639 struct symtab_and_line stop_pc_sal;
6640 struct frame_info *frame;
6641 struct gdbarch *gdbarch;
cdaa5b73
PA
6642 CORE_ADDR jmp_buf_pc;
6643 struct bpstat_what what;
94c57d6a 6644
cdaa5b73 6645 /* Handle cases caused by hitting a breakpoint. */
611c83ae 6646
cdaa5b73
PA
6647 frame = get_current_frame ();
6648 gdbarch = get_frame_arch (frame);
fcf3daef 6649
cdaa5b73 6650 what = bpstat_what (ecs->event_thread->control.stop_bpstat);
611c83ae 6651
cdaa5b73
PA
6652 if (what.call_dummy)
6653 {
6654 stop_stack_dummy = what.call_dummy;
6655 }
186c406b 6656
243a9253
PA
6657 /* A few breakpoint types have callbacks associated (e.g.,
6658 bp_jit_event). Run them now. */
6659 bpstat_run_callbacks (ecs->event_thread->control.stop_bpstat);
6660
cdaa5b73
PA
6661 /* If we hit an internal event that triggers symbol changes, the
6662 current frame will be invalidated within bpstat_what (e.g., if we
6663 hit an internal solib event). Re-fetch it. */
6664 frame = get_current_frame ();
6665 gdbarch = get_frame_arch (frame);
e2e4d78b 6666
cdaa5b73
PA
6667 switch (what.main_action)
6668 {
6669 case BPSTAT_WHAT_SET_LONGJMP_RESUME:
6670 /* If we hit the breakpoint at longjmp while stepping, we
6671 install a momentary breakpoint at the target of the
6672 jmp_buf. */
186c406b 6673
1eb8556f 6674 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME");
186c406b 6675
cdaa5b73 6676 ecs->event_thread->stepping_over_breakpoint = 1;
611c83ae 6677
cdaa5b73
PA
6678 if (what.is_longjmp)
6679 {
6680 struct value *arg_value;
6681
6682 /* If we set the longjmp breakpoint via a SystemTap probe,
6683 then use it to extract the arguments. The destination PC
6684 is the third argument to the probe. */
6685 arg_value = probe_safe_evaluate_at_pc (frame, 2);
6686 if (arg_value)
8fa0c4f8
AA
6687 {
6688 jmp_buf_pc = value_as_address (arg_value);
6689 jmp_buf_pc = gdbarch_addr_bits_remove (gdbarch, jmp_buf_pc);
6690 }
cdaa5b73
PA
6691 else if (!gdbarch_get_longjmp_target_p (gdbarch)
6692 || !gdbarch_get_longjmp_target (gdbarch,
6693 frame, &jmp_buf_pc))
e2e4d78b 6694 {
1eb8556f
SM
6695 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME "
6696 "(!gdbarch_get_longjmp_target)");
cdaa5b73
PA
6697 keep_going (ecs);
6698 return;
e2e4d78b 6699 }
e2e4d78b 6700
cdaa5b73
PA
6701 /* Insert a breakpoint at resume address. */
6702 insert_longjmp_resume_breakpoint (gdbarch, jmp_buf_pc);
6703 }
6704 else
6705 check_exception_resume (ecs, frame);
6706 keep_going (ecs);
6707 return;
e81a37f7 6708
cdaa5b73
PA
6709 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
6710 {
6711 struct frame_info *init_frame;
e81a37f7 6712
cdaa5b73 6713 /* There are several cases to consider.
c906108c 6714
cdaa5b73
PA
6715 1. The initiating frame no longer exists. In this case we
6716 must stop, because the exception or longjmp has gone too
6717 far.
2c03e5be 6718
cdaa5b73
PA
6719 2. The initiating frame exists, and is the same as the
6720 current frame. We stop, because the exception or longjmp
6721 has been caught.
2c03e5be 6722
cdaa5b73
PA
6723 3. The initiating frame exists and is different from the
6724 current frame. This means the exception or longjmp has
6725 been caught beneath the initiating frame, so keep going.
c906108c 6726
cdaa5b73
PA
6727 4. longjmp breakpoint has been placed just to protect
6728 against stale dummy frames and user is not interested in
6729 stopping around longjmps. */
c5aa993b 6730
1eb8556f 6731 infrun_debug_printf ("BPSTAT_WHAT_CLEAR_LONGJMP_RESUME");
c5aa993b 6732
cdaa5b73
PA
6733 gdb_assert (ecs->event_thread->control.exception_resume_breakpoint
6734 != NULL);
6735 delete_exception_resume_breakpoint (ecs->event_thread);
c5aa993b 6736
cdaa5b73
PA
6737 if (what.is_longjmp)
6738 {
b67a2c6f 6739 check_longjmp_breakpoint_for_call_dummy (ecs->event_thread);
c5aa993b 6740
cdaa5b73 6741 if (!frame_id_p (ecs->event_thread->initiating_frame))
e5ef252a 6742 {
cdaa5b73
PA
6743 /* Case 4. */
6744 keep_going (ecs);
6745 return;
e5ef252a 6746 }
cdaa5b73 6747 }
c5aa993b 6748
cdaa5b73 6749 init_frame = frame_find_by_id (ecs->event_thread->initiating_frame);
527159b7 6750
cdaa5b73
PA
6751 if (init_frame)
6752 {
6753 struct frame_id current_id
6754 = get_frame_id (get_current_frame ());
6755 if (frame_id_eq (current_id,
6756 ecs->event_thread->initiating_frame))
6757 {
6758 /* Case 2. Fall through. */
6759 }
6760 else
6761 {
6762 /* Case 3. */
6763 keep_going (ecs);
6764 return;
6765 }
68f53502 6766 }
488f131b 6767
cdaa5b73
PA
6768 /* For Cases 1 and 2, remove the step-resume breakpoint, if it
6769 exists. */
6770 delete_step_resume_breakpoint (ecs->event_thread);
e5ef252a 6771
bdc36728 6772 end_stepping_range (ecs);
cdaa5b73
PA
6773 }
6774 return;
e5ef252a 6775
cdaa5b73 6776 case BPSTAT_WHAT_SINGLE:
1eb8556f 6777 infrun_debug_printf ("BPSTAT_WHAT_SINGLE");
cdaa5b73
PA
6778 ecs->event_thread->stepping_over_breakpoint = 1;
6779 /* Still need to check other stuff, at least the case where we
6780 are stepping and step out of the right range. */
6781 break;
e5ef252a 6782
cdaa5b73 6783 case BPSTAT_WHAT_STEP_RESUME:
1eb8556f 6784 infrun_debug_printf ("BPSTAT_WHAT_STEP_RESUME");
e5ef252a 6785
cdaa5b73
PA
6786 delete_step_resume_breakpoint (ecs->event_thread);
6787 if (ecs->event_thread->control.proceed_to_finish
6788 && execution_direction == EXEC_REVERSE)
6789 {
6790 struct thread_info *tp = ecs->event_thread;
6791
6792 /* We are finishing a function in reverse, and just hit the
6793 step-resume breakpoint at the start address of the
6794 function, and we're almost there -- just need to back up
6795 by one more single-step, which should take us back to the
6796 function call. */
6797 tp->control.step_range_start = tp->control.step_range_end = 1;
6798 keep_going (ecs);
e5ef252a 6799 return;
cdaa5b73
PA
6800 }
6801 fill_in_stop_func (gdbarch, ecs);
f2ffa92b 6802 if (ecs->event_thread->suspend.stop_pc == ecs->stop_func_start
cdaa5b73
PA
6803 && execution_direction == EXEC_REVERSE)
6804 {
6805 /* We are stepping over a function call in reverse, and just
6806 hit the step-resume breakpoint at the start address of
6807 the function. Go back to single-stepping, which should
6808 take us back to the function call. */
6809 ecs->event_thread->stepping_over_breakpoint = 1;
6810 keep_going (ecs);
6811 return;
6812 }
6813 break;
e5ef252a 6814
cdaa5b73 6815 case BPSTAT_WHAT_STOP_NOISY:
1eb8556f 6816 infrun_debug_printf ("BPSTAT_WHAT_STOP_NOISY");
c4464ade 6817 stop_print_frame = true;
e5ef252a 6818
33bf4c5c 6819 /* Assume the thread stopped for a breakpoint. We'll still check
99619bea
PA
6820 whether a/the breakpoint is there when the thread is next
6821 resumed. */
6822 ecs->event_thread->stepping_over_breakpoint = 1;
e5ef252a 6823
22bcd14b 6824 stop_waiting (ecs);
cdaa5b73 6825 return;
e5ef252a 6826
cdaa5b73 6827 case BPSTAT_WHAT_STOP_SILENT:
1eb8556f 6828 infrun_debug_printf ("BPSTAT_WHAT_STOP_SILENT");
c4464ade 6829 stop_print_frame = false;
e5ef252a 6830
33bf4c5c 6831 /* Assume the thread stopped for a breakpoint. We'll still check
99619bea
PA
6832 whether a/the breakpoint is there when the thread is next
6833 resumed. */
6834 ecs->event_thread->stepping_over_breakpoint = 1;
22bcd14b 6835 stop_waiting (ecs);
cdaa5b73
PA
6836 return;
6837
6838 case BPSTAT_WHAT_HP_STEP_RESUME:
1eb8556f 6839 infrun_debug_printf ("BPSTAT_WHAT_HP_STEP_RESUME");
cdaa5b73
PA
6840
6841 delete_step_resume_breakpoint (ecs->event_thread);
6842 if (ecs->event_thread->step_after_step_resume_breakpoint)
6843 {
6844 /* Back when the step-resume breakpoint was inserted, we
6845 were trying to single-step off a breakpoint. Go back to
6846 doing that. */
6847 ecs->event_thread->step_after_step_resume_breakpoint = 0;
6848 ecs->event_thread->stepping_over_breakpoint = 1;
6849 keep_going (ecs);
6850 return;
e5ef252a 6851 }
cdaa5b73
PA
6852 break;
6853
6854 case BPSTAT_WHAT_KEEP_CHECKING:
6855 break;
e5ef252a 6856 }
c906108c 6857
af48d08f
PA
6858 /* If we stepped a permanent breakpoint and we had a high priority
6859 step-resume breakpoint for the address we stepped, but we didn't
6860 hit it, then we must have stepped into the signal handler. The
6861 step-resume was only necessary to catch the case of _not_
6862 stepping into the handler, so delete it, and fall through to
6863 checking whether the step finished. */
6864 if (ecs->event_thread->stepped_breakpoint)
6865 {
6866 struct breakpoint *sr_bp
6867 = ecs->event_thread->control.step_resume_breakpoint;
6868
8d707a12
PA
6869 if (sr_bp != NULL
6870 && sr_bp->loc->permanent
af48d08f
PA
6871 && sr_bp->type == bp_hp_step_resume
6872 && sr_bp->loc->address == ecs->event_thread->prev_pc)
6873 {
1eb8556f 6874 infrun_debug_printf ("stepped permanent breakpoint, stopped in handler");
af48d08f
PA
6875 delete_step_resume_breakpoint (ecs->event_thread);
6876 ecs->event_thread->step_after_step_resume_breakpoint = 0;
6877 }
6878 }
6879
cdaa5b73
PA
6880 /* We come here if we hit a breakpoint but should not stop for it.
6881 Possibly we also were stepping and should stop for that. So fall
6882 through and test for stepping. But, if not stepping, do not
6883 stop. */
c906108c 6884
a7212384
UW
6885 /* In all-stop mode, if we're currently stepping but have stopped in
6886 some other thread, we need to switch back to the stepped thread. */
c447ac0b
PA
6887 if (switch_back_to_stepped_thread (ecs))
6888 return;
776f04fa 6889
8358c15c 6890 if (ecs->event_thread->control.step_resume_breakpoint)
488f131b 6891 {
1eb8556f 6892 infrun_debug_printf ("step-resume breakpoint is inserted");
527159b7 6893
488f131b 6894 /* Having a step-resume breakpoint overrides anything
dda83cd7
SM
6895 else having to do with stepping commands until
6896 that breakpoint is reached. */
488f131b
JB
6897 keep_going (ecs);
6898 return;
6899 }
c5aa993b 6900
16c381f0 6901 if (ecs->event_thread->control.step_range_end == 0)
488f131b 6902 {
1eb8556f 6903 infrun_debug_printf ("no stepping, continue");
488f131b 6904 /* Likewise if we aren't even stepping. */
488f131b
JB
6905 keep_going (ecs);
6906 return;
6907 }
c5aa993b 6908
4b7703ad
JB
6909 /* Re-fetch current thread's frame in case the code above caused
6910 the frame cache to be re-initialized, making our FRAME variable
6911 a dangling pointer. */
6912 frame = get_current_frame ();
628fe4e4 6913 gdbarch = get_frame_arch (frame);
7e324e48 6914 fill_in_stop_func (gdbarch, ecs);
4b7703ad 6915
488f131b 6916 /* If stepping through a line, keep going if still within it.
c906108c 6917
488f131b
JB
6918 Note that step_range_end is the address of the first instruction
6919 beyond the step range, and NOT the address of the last instruction
31410e84
MS
6920 within it!
6921
6922 Note also that during reverse execution, we may be stepping
6923 through a function epilogue and therefore must detect when
6924 the current-frame changes in the middle of a line. */
6925
f2ffa92b
PA
6926 if (pc_in_thread_step_range (ecs->event_thread->suspend.stop_pc,
6927 ecs->event_thread)
31410e84 6928 && (execution_direction != EXEC_REVERSE
388a8562 6929 || frame_id_eq (get_frame_id (frame),
16c381f0 6930 ecs->event_thread->control.step_frame_id)))
488f131b 6931 {
1eb8556f
SM
6932 infrun_debug_printf
6933 ("stepping inside range [%s-%s]",
6934 paddress (gdbarch, ecs->event_thread->control.step_range_start),
6935 paddress (gdbarch, ecs->event_thread->control.step_range_end));
b2175913 6936
c1e36e3e
PA
6937 /* Tentatively re-enable range stepping; `resume' disables it if
6938 necessary (e.g., if we're stepping over a breakpoint or we
6939 have software watchpoints). */
6940 ecs->event_thread->control.may_range_step = 1;
6941
b2175913
MS
6942 /* When stepping backward, stop at beginning of line range
6943 (unless it's the function entry point, in which case
6944 keep going back to the call point). */
f2ffa92b 6945 CORE_ADDR stop_pc = ecs->event_thread->suspend.stop_pc;
16c381f0 6946 if (stop_pc == ecs->event_thread->control.step_range_start
b2175913
MS
6947 && stop_pc != ecs->stop_func_start
6948 && execution_direction == EXEC_REVERSE)
bdc36728 6949 end_stepping_range (ecs);
b2175913
MS
6950 else
6951 keep_going (ecs);
6952
488f131b
JB
6953 return;
6954 }
c5aa993b 6955
488f131b 6956 /* We stepped out of the stepping range. */
c906108c 6957
488f131b 6958 /* If we are stepping at the source level and entered the runtime
388a8562
MS
6959 loader dynamic symbol resolution code...
6960
6961 EXEC_FORWARD: we keep on single stepping until we exit the run
6962 time loader code and reach the callee's address.
6963
6964 EXEC_REVERSE: we've already executed the callee (backward), and
6965 the runtime loader code is handled just like any other
6966 undebuggable function call. Now we need only keep stepping
6967 backward through the trampoline code, and that's handled further
6968 down, so there is nothing for us to do here. */
6969
6970 if (execution_direction != EXEC_REVERSE
16c381f0 6971 && ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
f2ffa92b 6972 && in_solib_dynsym_resolve_code (ecs->event_thread->suspend.stop_pc))
488f131b 6973 {
4c8c40e6 6974 CORE_ADDR pc_after_resolver =
f2ffa92b
PA
6975 gdbarch_skip_solib_resolver (gdbarch,
6976 ecs->event_thread->suspend.stop_pc);
c906108c 6977
1eb8556f 6978 infrun_debug_printf ("stepped into dynsym resolve code");
527159b7 6979
488f131b
JB
6980 if (pc_after_resolver)
6981 {
6982 /* Set up a step-resume breakpoint at the address
6983 indicated by SKIP_SOLIB_RESOLVER. */
51abb421 6984 symtab_and_line sr_sal;
488f131b 6985 sr_sal.pc = pc_after_resolver;
6c95b8df 6986 sr_sal.pspace = get_frame_program_space (frame);
488f131b 6987
a6d9a66e
UW
6988 insert_step_resume_breakpoint_at_sal (gdbarch,
6989 sr_sal, null_frame_id);
c5aa993b 6990 }
c906108c 6991
488f131b
JB
6992 keep_going (ecs);
6993 return;
6994 }
c906108c 6995
1d509aa6
MM
6996 /* Step through an indirect branch thunk. */
6997 if (ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
f2ffa92b
PA
6998 && gdbarch_in_indirect_branch_thunk (gdbarch,
6999 ecs->event_thread->suspend.stop_pc))
1d509aa6 7000 {
1eb8556f 7001 infrun_debug_printf ("stepped into indirect branch thunk");
1d509aa6
MM
7002 keep_going (ecs);
7003 return;
7004 }
7005
16c381f0
JK
7006 if (ecs->event_thread->control.step_range_end != 1
7007 && (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
7008 || ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
568d6575 7009 && get_frame_type (frame) == SIGTRAMP_FRAME)
488f131b 7010 {
1eb8556f 7011 infrun_debug_printf ("stepped into signal trampoline");
42edda50 7012 /* The inferior, while doing a "step" or "next", has ended up in
dda83cd7
SM
7013 a signal trampoline (either by a signal being delivered or by
7014 the signal handler returning). Just single-step until the
7015 inferior leaves the trampoline (either by calling the handler
7016 or returning). */
488f131b
JB
7017 keep_going (ecs);
7018 return;
7019 }
c906108c 7020
14132e89
MR
7021 /* If we're in the return path from a shared library trampoline,
7022 we want to proceed through the trampoline when stepping. */
7023 /* macro/2012-04-25: This needs to come before the subroutine
7024 call check below as on some targets return trampolines look
7025 like subroutine calls (MIPS16 return thunks). */
7026 if (gdbarch_in_solib_return_trampoline (gdbarch,
f2ffa92b
PA
7027 ecs->event_thread->suspend.stop_pc,
7028 ecs->stop_func_name)
14132e89
MR
7029 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
7030 {
7031 /* Determine where this trampoline returns. */
f2ffa92b
PA
7032 CORE_ADDR stop_pc = ecs->event_thread->suspend.stop_pc;
7033 CORE_ADDR real_stop_pc
7034 = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
14132e89 7035
1eb8556f 7036 infrun_debug_printf ("stepped into solib return tramp");
14132e89
MR
7037
7038 /* Only proceed through if we know where it's going. */
7039 if (real_stop_pc)
7040 {
7041 /* And put the step-breakpoint there and go until there. */
51abb421 7042 symtab_and_line sr_sal;
14132e89
MR
7043 sr_sal.pc = real_stop_pc;
7044 sr_sal.section = find_pc_overlay (sr_sal.pc);
7045 sr_sal.pspace = get_frame_program_space (frame);
7046
7047 /* Do not specify what the fp should be when we stop since
7048 on some machines the prologue is where the new fp value
7049 is established. */
7050 insert_step_resume_breakpoint_at_sal (gdbarch,
7051 sr_sal, null_frame_id);
7052
7053 /* Restart without fiddling with the step ranges or
7054 other state. */
7055 keep_going (ecs);
7056 return;
7057 }
7058 }
7059
c17eaafe
DJ
7060 /* Check for subroutine calls. The check for the current frame
7061 equalling the step ID is not necessary - the check of the
7062 previous frame's ID is sufficient - but it is a common case and
7063 cheaper than checking the previous frame's ID.
14e60db5
DJ
7064
7065 NOTE: frame_id_eq will never report two invalid frame IDs as
7066 being equal, so to get into this block, both the current and
7067 previous frame must have valid frame IDs. */
005ca36a
JB
7068 /* The outer_frame_id check is a heuristic to detect stepping
7069 through startup code. If we step over an instruction which
7070 sets the stack pointer from an invalid value to a valid value,
7071 we may detect that as a subroutine call from the mythical
7072 "outermost" function. This could be fixed by marking
7073 outermost frames as !stack_p,code_p,special_p. Then the
7074 initial outermost frame, before sp was valid, would
ce6cca6d 7075 have code_addr == &_start. See the comment in frame_id_eq
005ca36a 7076 for more. */
edb3359d 7077 if (!frame_id_eq (get_stack_frame_id (frame),
16c381f0 7078 ecs->event_thread->control.step_stack_frame_id)
005ca36a 7079 && (frame_id_eq (frame_unwind_caller_id (get_current_frame ()),
16c381f0
JK
7080 ecs->event_thread->control.step_stack_frame_id)
7081 && (!frame_id_eq (ecs->event_thread->control.step_stack_frame_id,
005ca36a 7082 outer_frame_id)
885eeb5b 7083 || (ecs->event_thread->control.step_start_function
f2ffa92b 7084 != find_pc_function (ecs->event_thread->suspend.stop_pc)))))
488f131b 7085 {
f2ffa92b 7086 CORE_ADDR stop_pc = ecs->event_thread->suspend.stop_pc;
95918acb 7087 CORE_ADDR real_stop_pc;
8fb3e588 7088
1eb8556f 7089 infrun_debug_printf ("stepped into subroutine");
527159b7 7090
b7a084be 7091 if (ecs->event_thread->control.step_over_calls == STEP_OVER_NONE)
95918acb
AC
7092 {
7093 /* I presume that step_over_calls is only 0 when we're
7094 supposed to be stepping at the assembly language level
7095 ("stepi"). Just stop. */
388a8562 7096 /* And this works the same backward as frontward. MVS */
bdc36728 7097 end_stepping_range (ecs);
95918acb
AC
7098 return;
7099 }
8fb3e588 7100
388a8562
MS
7101 /* Reverse stepping through solib trampolines. */
7102
7103 if (execution_direction == EXEC_REVERSE
16c381f0 7104 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
388a8562
MS
7105 && (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
7106 || (ecs->stop_func_start == 0
7107 && in_solib_dynsym_resolve_code (stop_pc))))
7108 {
7109 /* Any solib trampoline code can be handled in reverse
7110 by simply continuing to single-step. We have already
7111 executed the solib function (backwards), and a few
7112 steps will take us back through the trampoline to the
7113 caller. */
7114 keep_going (ecs);
7115 return;
7116 }
7117
16c381f0 7118 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL)
8567c30f 7119 {
b2175913
MS
7120 /* We're doing a "next".
7121
7122 Normal (forward) execution: set a breakpoint at the
7123 callee's return address (the address at which the caller
7124 will resume).
7125
7126 Reverse (backward) execution. set the step-resume
7127 breakpoint at the start of the function that we just
7128 stepped into (backwards), and continue to there. When we
6130d0b7 7129 get there, we'll need to single-step back to the caller. */
b2175913
MS
7130
7131 if (execution_direction == EXEC_REVERSE)
7132 {
acf9414f
JK
7133 /* If we're already at the start of the function, we've either
7134 just stepped backward into a single instruction function,
7135 or stepped back out of a signal handler to the first instruction
7136 of the function. Just keep going, which will single-step back
7137 to the caller. */
58c48e72 7138 if (ecs->stop_func_start != stop_pc && ecs->stop_func_start != 0)
acf9414f 7139 {
acf9414f 7140 /* Normal function call return (static or dynamic). */
51abb421 7141 symtab_and_line sr_sal;
acf9414f
JK
7142 sr_sal.pc = ecs->stop_func_start;
7143 sr_sal.pspace = get_frame_program_space (frame);
7144 insert_step_resume_breakpoint_at_sal (gdbarch,
7145 sr_sal, null_frame_id);
7146 }
b2175913
MS
7147 }
7148 else
568d6575 7149 insert_step_resume_breakpoint_at_caller (frame);
b2175913 7150
8567c30f
AC
7151 keep_going (ecs);
7152 return;
7153 }
a53c66de 7154
95918acb 7155 /* If we are in a function call trampoline (a stub between the
dda83cd7
SM
7156 calling routine and the real function), locate the real
7157 function. That's what tells us (a) whether we want to step
7158 into it at all, and (b) what prologue we want to run to the
7159 end of, if we do step into it. */
568d6575 7160 real_stop_pc = skip_language_trampoline (frame, stop_pc);
95918acb 7161 if (real_stop_pc == 0)
568d6575 7162 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
95918acb
AC
7163 if (real_stop_pc != 0)
7164 ecs->stop_func_start = real_stop_pc;
8fb3e588 7165
db5f024e 7166 if (real_stop_pc != 0 && in_solib_dynsym_resolve_code (real_stop_pc))
1b2bfbb9 7167 {
51abb421 7168 symtab_and_line sr_sal;
1b2bfbb9 7169 sr_sal.pc = ecs->stop_func_start;
6c95b8df 7170 sr_sal.pspace = get_frame_program_space (frame);
1b2bfbb9 7171
a6d9a66e
UW
7172 insert_step_resume_breakpoint_at_sal (gdbarch,
7173 sr_sal, null_frame_id);
8fb3e588
AC
7174 keep_going (ecs);
7175 return;
1b2bfbb9
RC
7176 }
7177
95918acb 7178 /* If we have line number information for the function we are
1bfeeb0f
JL
7179 thinking of stepping into and the function isn't on the skip
7180 list, step into it.
95918acb 7181
dda83cd7
SM
7182 If there are several symtabs at that PC (e.g. with include
7183 files), just want to know whether *any* of them have line
7184 numbers. find_pc_line handles this. */
95918acb
AC
7185 {
7186 struct symtab_and_line tmp_sal;
8fb3e588 7187
95918acb 7188 tmp_sal = find_pc_line (ecs->stop_func_start, 0);
2b914b52 7189 if (tmp_sal.line != 0
85817405 7190 && !function_name_is_marked_for_skip (ecs->stop_func_name,
4a4c04f1
BE
7191 tmp_sal)
7192 && !inline_frame_is_marked_for_skip (true, ecs->event_thread))
95918acb 7193 {
b2175913 7194 if (execution_direction == EXEC_REVERSE)
568d6575 7195 handle_step_into_function_backward (gdbarch, ecs);
b2175913 7196 else
568d6575 7197 handle_step_into_function (gdbarch, ecs);
95918acb
AC
7198 return;
7199 }
7200 }
7201
7202 /* If we have no line number and the step-stop-if-no-debug is
dda83cd7
SM
7203 set, we stop the step so that the user has a chance to switch
7204 in assembly mode. */
16c381f0 7205 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
078130d0 7206 && step_stop_if_no_debug)
95918acb 7207 {
bdc36728 7208 end_stepping_range (ecs);
95918acb
AC
7209 return;
7210 }
7211
b2175913
MS
7212 if (execution_direction == EXEC_REVERSE)
7213 {
acf9414f
JK
7214 /* If we're already at the start of the function, we've either just
7215 stepped backward into a single instruction function without line
7216 number info, or stepped back out of a signal handler to the first
7217 instruction of the function without line number info. Just keep
7218 going, which will single-step back to the caller. */
7219 if (ecs->stop_func_start != stop_pc)
7220 {
7221 /* Set a breakpoint at callee's start address.
7222 From there we can step once and be back in the caller. */
51abb421 7223 symtab_and_line sr_sal;
acf9414f
JK
7224 sr_sal.pc = ecs->stop_func_start;
7225 sr_sal.pspace = get_frame_program_space (frame);
7226 insert_step_resume_breakpoint_at_sal (gdbarch,
7227 sr_sal, null_frame_id);
7228 }
b2175913
MS
7229 }
7230 else
7231 /* Set a breakpoint at callee's return address (the address
7232 at which the caller will resume). */
568d6575 7233 insert_step_resume_breakpoint_at_caller (frame);
b2175913 7234
95918acb 7235 keep_going (ecs);
488f131b 7236 return;
488f131b 7237 }
c906108c 7238
fdd654f3
MS
7239 /* Reverse stepping through solib trampolines. */
7240
7241 if (execution_direction == EXEC_REVERSE
16c381f0 7242 && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
fdd654f3 7243 {
f2ffa92b
PA
7244 CORE_ADDR stop_pc = ecs->event_thread->suspend.stop_pc;
7245
fdd654f3
MS
7246 if (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
7247 || (ecs->stop_func_start == 0
7248 && in_solib_dynsym_resolve_code (stop_pc)))
7249 {
7250 /* Any solib trampoline code can be handled in reverse
7251 by simply continuing to single-step. We have already
7252 executed the solib function (backwards), and a few
7253 steps will take us back through the trampoline to the
7254 caller. */
7255 keep_going (ecs);
7256 return;
7257 }
7258 else if (in_solib_dynsym_resolve_code (stop_pc))
7259 {
7260 /* Stepped backward into the solib dynsym resolver.
7261 Set a breakpoint at its start and continue, then
7262 one more step will take us out. */
51abb421 7263 symtab_and_line sr_sal;
fdd654f3 7264 sr_sal.pc = ecs->stop_func_start;
9d1807c3 7265 sr_sal.pspace = get_frame_program_space (frame);
fdd654f3
MS
7266 insert_step_resume_breakpoint_at_sal (gdbarch,
7267 sr_sal, null_frame_id);
7268 keep_going (ecs);
7269 return;
7270 }
7271 }
7272
8c95582d
AB
7273 /* This always returns the sal for the inner-most frame when we are in a
7274 stack of inlined frames, even if GDB actually believes that it is in a
7275 more outer frame. This is checked for below by calls to
7276 inline_skipped_frames. */
f2ffa92b 7277 stop_pc_sal = find_pc_line (ecs->event_thread->suspend.stop_pc, 0);
7ed0fe66 7278
1b2bfbb9
RC
7279 /* NOTE: tausq/2004-05-24: This if block used to be done before all
7280 the trampoline processing logic, however, there are some trampolines
7281 that have no names, so we should do trampoline handling first. */
16c381f0 7282 if (ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
7ed0fe66 7283 && ecs->stop_func_name == NULL
2afb61aa 7284 && stop_pc_sal.line == 0)
1b2bfbb9 7285 {
1eb8556f 7286 infrun_debug_printf ("stepped into undebuggable function");
527159b7 7287
1b2bfbb9 7288 /* The inferior just stepped into, or returned to, an
dda83cd7
SM
7289 undebuggable function (where there is no debugging information
7290 and no line number corresponding to the address where the
7291 inferior stopped). Since we want to skip this kind of code,
7292 we keep going until the inferior returns from this
7293 function - unless the user has asked us not to (via
7294 set step-mode) or we no longer know how to get back
7295 to the call site. */
14e60db5 7296 if (step_stop_if_no_debug
c7ce8faa 7297 || !frame_id_p (frame_unwind_caller_id (frame)))
1b2bfbb9
RC
7298 {
7299 /* If we have no line number and the step-stop-if-no-debug
7300 is set, we stop the step so that the user has a chance to
7301 switch in assembly mode. */
bdc36728 7302 end_stepping_range (ecs);
1b2bfbb9
RC
7303 return;
7304 }
7305 else
7306 {
7307 /* Set a breakpoint at callee's return address (the address
7308 at which the caller will resume). */
568d6575 7309 insert_step_resume_breakpoint_at_caller (frame);
1b2bfbb9
RC
7310 keep_going (ecs);
7311 return;
7312 }
7313 }
7314
16c381f0 7315 if (ecs->event_thread->control.step_range_end == 1)
1b2bfbb9
RC
7316 {
7317 /* It is stepi or nexti. We always want to stop stepping after
dda83cd7 7318 one instruction. */
1eb8556f 7319 infrun_debug_printf ("stepi/nexti");
bdc36728 7320 end_stepping_range (ecs);
1b2bfbb9
RC
7321 return;
7322 }
7323
2afb61aa 7324 if (stop_pc_sal.line == 0)
488f131b
JB
7325 {
7326 /* We have no line number information. That means to stop
dda83cd7
SM
7327 stepping (does this always happen right after one instruction,
7328 when we do "s" in a function with no line numbers,
7329 or can this happen as a result of a return or longjmp?). */
1eb8556f 7330 infrun_debug_printf ("line number info");
bdc36728 7331 end_stepping_range (ecs);
488f131b
JB
7332 return;
7333 }
c906108c 7334
edb3359d
DJ
7335 /* Look for "calls" to inlined functions, part one. If the inline
7336 frame machinery detected some skipped call sites, we have entered
7337 a new inline function. */
7338
7339 if (frame_id_eq (get_frame_id (get_current_frame ()),
16c381f0 7340 ecs->event_thread->control.step_frame_id)
00431a78 7341 && inline_skipped_frames (ecs->event_thread))
edb3359d 7342 {
1eb8556f 7343 infrun_debug_printf ("stepped into inlined function");
edb3359d 7344
51abb421 7345 symtab_and_line call_sal = find_frame_sal (get_current_frame ());
edb3359d 7346
16c381f0 7347 if (ecs->event_thread->control.step_over_calls != STEP_OVER_ALL)
edb3359d
DJ
7348 {
7349 /* For "step", we're going to stop. But if the call site
7350 for this inlined function is on the same source line as
7351 we were previously stepping, go down into the function
7352 first. Otherwise stop at the call site. */
7353
7354 if (call_sal.line == ecs->event_thread->current_line
7355 && call_sal.symtab == ecs->event_thread->current_symtab)
4a4c04f1
BE
7356 {
7357 step_into_inline_frame (ecs->event_thread);
7358 if (inline_frame_is_marked_for_skip (false, ecs->event_thread))
7359 {
7360 keep_going (ecs);
7361 return;
7362 }
7363 }
edb3359d 7364
bdc36728 7365 end_stepping_range (ecs);
edb3359d
DJ
7366 return;
7367 }
7368 else
7369 {
7370 /* For "next", we should stop at the call site if it is on a
7371 different source line. Otherwise continue through the
7372 inlined function. */
7373 if (call_sal.line == ecs->event_thread->current_line
7374 && call_sal.symtab == ecs->event_thread->current_symtab)
7375 keep_going (ecs);
7376 else
bdc36728 7377 end_stepping_range (ecs);
edb3359d
DJ
7378 return;
7379 }
7380 }
7381
7382 /* Look for "calls" to inlined functions, part two. If we are still
7383 in the same real function we were stepping through, but we have
7384 to go further up to find the exact frame ID, we are stepping
7385 through a more inlined call beyond its call site. */
7386
7387 if (get_frame_type (get_current_frame ()) == INLINE_FRAME
7388 && !frame_id_eq (get_frame_id (get_current_frame ()),
16c381f0 7389 ecs->event_thread->control.step_frame_id)
edb3359d 7390 && stepped_in_from (get_current_frame (),
16c381f0 7391 ecs->event_thread->control.step_frame_id))
edb3359d 7392 {
1eb8556f 7393 infrun_debug_printf ("stepping through inlined function");
edb3359d 7394
4a4c04f1
BE
7395 if (ecs->event_thread->control.step_over_calls == STEP_OVER_ALL
7396 || inline_frame_is_marked_for_skip (false, ecs->event_thread))
edb3359d
DJ
7397 keep_going (ecs);
7398 else
bdc36728 7399 end_stepping_range (ecs);
edb3359d
DJ
7400 return;
7401 }
7402
8c95582d 7403 bool refresh_step_info = true;
f2ffa92b 7404 if ((ecs->event_thread->suspend.stop_pc == stop_pc_sal.pc)
4e1c45ea 7405 && (ecs->event_thread->current_line != stop_pc_sal.line
24b21115 7406 || ecs->event_thread->current_symtab != stop_pc_sal.symtab))
488f131b 7407 {
ebde6f2d
TV
7408 /* We are at a different line. */
7409
8c95582d
AB
7410 if (stop_pc_sal.is_stmt)
7411 {
ebde6f2d
TV
7412 /* We are at the start of a statement.
7413
7414 So stop. Note that we don't stop if we step into the middle of a
7415 statement. That is said to make things like for (;;) statements
7416 work better. */
1eb8556f 7417 infrun_debug_printf ("stepped to a different line");
8c95582d
AB
7418 end_stepping_range (ecs);
7419 return;
7420 }
7421 else if (frame_id_eq (get_frame_id (get_current_frame ()),
ebde6f2d 7422 ecs->event_thread->control.step_frame_id))
8c95582d 7423 {
ebde6f2d
TV
7424 /* We are not at the start of a statement, and we have not changed
7425 frame.
7426
7427 We ignore this line table entry, and continue stepping forward,
8c95582d
AB
7428 looking for a better place to stop. */
7429 refresh_step_info = false;
1eb8556f
SM
7430 infrun_debug_printf ("stepped to a different line, but "
7431 "it's not the start of a statement");
8c95582d 7432 }
ebde6f2d
TV
7433 else
7434 {
7435 /* We are not the start of a statement, and we have changed frame.
7436
7437 We ignore this line table entry, and continue stepping forward,
7438 looking for a better place to stop. Keep refresh_step_info at
7439 true to note that the frame has changed, but ignore the line
7440 number to make sure we don't ignore a subsequent entry with the
7441 same line number. */
7442 stop_pc_sal.line = 0;
7443 infrun_debug_printf ("stepped to a different frame, but "
7444 "it's not the start of a statement");
7445 }
488f131b 7446 }
c906108c 7447
488f131b 7448 /* We aren't done stepping.
c906108c 7449
488f131b
JB
7450 Optimize by setting the stepping range to the line.
7451 (We might not be in the original line, but if we entered a
7452 new line in mid-statement, we continue stepping. This makes
8c95582d
AB
7453 things like for(;;) statements work better.)
7454
7455 If we entered a SAL that indicates a non-statement line table entry,
7456 then we update the stepping range, but we don't update the step info,
7457 which includes things like the line number we are stepping away from.
7458 This means we will stop when we find a line table entry that is marked
7459 as is-statement, even if it matches the non-statement one we just
7460 stepped into. */
c906108c 7461
16c381f0
JK
7462 ecs->event_thread->control.step_range_start = stop_pc_sal.pc;
7463 ecs->event_thread->control.step_range_end = stop_pc_sal.end;
c1e36e3e 7464 ecs->event_thread->control.may_range_step = 1;
8c95582d
AB
7465 if (refresh_step_info)
7466 set_step_info (ecs->event_thread, frame, stop_pc_sal);
488f131b 7467
1eb8556f 7468 infrun_debug_printf ("keep going");
488f131b 7469 keep_going (ecs);
104c1213
JM
7470}
7471
408f6686
PA
7472static bool restart_stepped_thread (process_stratum_target *resume_target,
7473 ptid_t resume_ptid);
7474
c447ac0b
PA
7475/* In all-stop mode, if we're currently stepping but have stopped in
7476 some other thread, we may need to switch back to the stepped
7477 thread. Returns true we set the inferior running, false if we left
7478 it stopped (and the event needs further processing). */
7479
c4464ade 7480static bool
c447ac0b
PA
7481switch_back_to_stepped_thread (struct execution_control_state *ecs)
7482{
fbea99ea 7483 if (!target_is_non_stop_p ())
c447ac0b 7484 {
99619bea
PA
7485 /* If any thread is blocked on some internal breakpoint, and we
7486 simply need to step over that breakpoint to get it going
7487 again, do that first. */
7488
7489 /* However, if we see an event for the stepping thread, then we
7490 know all other threads have been moved past their breakpoints
7491 already. Let the caller check whether the step is finished,
7492 etc., before deciding to move it past a breakpoint. */
7493 if (ecs->event_thread->control.step_range_end != 0)
c4464ade 7494 return false;
99619bea
PA
7495
7496 /* Check if the current thread is blocked on an incomplete
7497 step-over, interrupted by a random signal. */
7498 if (ecs->event_thread->control.trap_expected
7499 && ecs->event_thread->suspend.stop_signal != GDB_SIGNAL_TRAP)
c447ac0b 7500 {
1eb8556f
SM
7501 infrun_debug_printf
7502 ("need to finish step-over of [%s]",
7503 target_pid_to_str (ecs->event_thread->ptid).c_str ());
99619bea 7504 keep_going (ecs);
c4464ade 7505 return true;
99619bea 7506 }
2adfaa28 7507
99619bea
PA
7508 /* Check if the current thread is blocked by a single-step
7509 breakpoint of another thread. */
7510 if (ecs->hit_singlestep_breakpoint)
7511 {
1eb8556f
SM
7512 infrun_debug_printf ("need to step [%s] over single-step breakpoint",
7513 target_pid_to_str (ecs->ptid).c_str ());
99619bea 7514 keep_going (ecs);
c4464ade 7515 return true;
99619bea
PA
7516 }
7517
4d9d9d04
PA
7518 /* If this thread needs yet another step-over (e.g., stepping
7519 through a delay slot), do it first before moving on to
7520 another thread. */
7521 if (thread_still_needs_step_over (ecs->event_thread))
7522 {
1eb8556f
SM
7523 infrun_debug_printf
7524 ("thread [%s] still needs step-over",
7525 target_pid_to_str (ecs->event_thread->ptid).c_str ());
4d9d9d04 7526 keep_going (ecs);
c4464ade 7527 return true;
4d9d9d04 7528 }
70509625 7529
483805cf
PA
7530 /* If scheduler locking applies even if not stepping, there's no
7531 need to walk over threads. Above we've checked whether the
7532 current thread is stepping. If some other thread not the
7533 event thread is stepping, then it must be that scheduler
7534 locking is not in effect. */
856e7dd6 7535 if (schedlock_applies (ecs->event_thread))
c4464ade 7536 return false;
483805cf 7537
4d9d9d04
PA
7538 /* Otherwise, we no longer expect a trap in the current thread.
7539 Clear the trap_expected flag before switching back -- this is
7540 what keep_going does as well, if we call it. */
7541 ecs->event_thread->control.trap_expected = 0;
7542
7543 /* Likewise, clear the signal if it should not be passed. */
7544 if (!signal_program[ecs->event_thread->suspend.stop_signal])
7545 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
7546
408f6686 7547 if (restart_stepped_thread (ecs->target, ecs->ptid))
4d9d9d04
PA
7548 {
7549 prepare_to_wait (ecs);
c4464ade 7550 return true;
4d9d9d04
PA
7551 }
7552
408f6686
PA
7553 switch_to_thread (ecs->event_thread);
7554 }
4d9d9d04 7555
408f6686
PA
7556 return false;
7557}
f3f8ece4 7558
408f6686
PA
7559/* Look for the thread that was stepping, and resume it.
7560 RESUME_TARGET / RESUME_PTID indicate the set of threads the caller
7561 is resuming. Return true if a thread was started, false
7562 otherwise. */
483805cf 7563
408f6686
PA
7564static bool
7565restart_stepped_thread (process_stratum_target *resume_target,
7566 ptid_t resume_ptid)
7567{
7568 /* Do all pending step-overs before actually proceeding with
7569 step/next/etc. */
7570 if (start_step_over ())
7571 return true;
483805cf 7572
408f6686
PA
7573 for (thread_info *tp : all_threads_safe ())
7574 {
7575 if (tp->state == THREAD_EXITED)
7576 continue;
7577
7578 if (tp->suspend.waitstatus_pending_p)
7579 continue;
483805cf 7580
408f6686
PA
7581 /* Ignore threads of processes the caller is not
7582 resuming. */
7583 if (!sched_multi
7584 && (tp->inf->process_target () != resume_target
7585 || tp->inf->pid != resume_ptid.pid ()))
7586 continue;
483805cf 7587
408f6686
PA
7588 if (tp->control.trap_expected)
7589 {
7590 infrun_debug_printf ("switching back to stepped thread (step-over)");
483805cf 7591
408f6686
PA
7592 if (keep_going_stepped_thread (tp))
7593 return true;
99619bea 7594 }
408f6686
PA
7595 }
7596
7597 for (thread_info *tp : all_threads_safe ())
7598 {
7599 if (tp->state == THREAD_EXITED)
7600 continue;
7601
7602 if (tp->suspend.waitstatus_pending_p)
7603 continue;
99619bea 7604
408f6686
PA
7605 /* Ignore threads of processes the caller is not
7606 resuming. */
7607 if (!sched_multi
7608 && (tp->inf->process_target () != resume_target
7609 || tp->inf->pid != resume_ptid.pid ()))
7610 continue;
7611
7612 /* Did we find the stepping thread? */
7613 if (tp->control.step_range_end)
99619bea 7614 {
408f6686 7615 infrun_debug_printf ("switching back to stepped thread (stepping)");
c447ac0b 7616
408f6686
PA
7617 if (keep_going_stepped_thread (tp))
7618 return true;
2ac7589c
PA
7619 }
7620 }
2adfaa28 7621
c4464ade 7622 return false;
2ac7589c 7623}
2adfaa28 7624
408f6686
PA
7625/* See infrun.h. */
7626
7627void
7628restart_after_all_stop_detach (process_stratum_target *proc_target)
7629{
7630 /* Note we don't check target_is_non_stop_p() here, because the
7631 current inferior may no longer have a process_stratum target
7632 pushed, as we just detached. */
7633
7634 /* See if we have a THREAD_RUNNING thread that need to be
7635 re-resumed. If we have any thread that is already executing,
7636 then we don't need to resume the target -- it is already been
7637 resumed. With the remote target (in all-stop), it's even
7638 impossible to issue another resumption if the target is already
7639 resumed, until the target reports a stop. */
7640 for (thread_info *thr : all_threads (proc_target))
7641 {
7642 if (thr->state != THREAD_RUNNING)
7643 continue;
7644
7645 /* If we have any thread that is already executing, then we
7646 don't need to resume the target -- it is already been
7647 resumed. */
7648 if (thr->executing)
7649 return;
7650
7651 /* If we have a pending event to process, skip resuming the
7652 target and go straight to processing it. */
7653 if (thr->resumed && thr->suspend.waitstatus_pending_p)
7654 return;
7655 }
7656
7657 /* Alright, we need to re-resume the target. If a thread was
7658 stepping, we need to restart it stepping. */
7659 if (restart_stepped_thread (proc_target, minus_one_ptid))
7660 return;
7661
7662 /* Otherwise, find the first THREAD_RUNNING thread and resume
7663 it. */
7664 for (thread_info *thr : all_threads (proc_target))
7665 {
7666 if (thr->state != THREAD_RUNNING)
7667 continue;
7668
7669 execution_control_state ecs;
7670 reset_ecs (&ecs, thr);
7671 switch_to_thread (thr);
7672 keep_going (&ecs);
7673 return;
7674 }
7675}
7676
2ac7589c
PA
7677/* Set a previously stepped thread back to stepping. Returns true on
7678 success, false if the resume is not possible (e.g., the thread
7679 vanished). */
7680
c4464ade 7681static bool
2ac7589c
PA
7682keep_going_stepped_thread (struct thread_info *tp)
7683{
7684 struct frame_info *frame;
2ac7589c
PA
7685 struct execution_control_state ecss;
7686 struct execution_control_state *ecs = &ecss;
2adfaa28 7687
2ac7589c
PA
7688 /* If the stepping thread exited, then don't try to switch back and
7689 resume it, which could fail in several different ways depending
7690 on the target. Instead, just keep going.
2adfaa28 7691
2ac7589c
PA
7692 We can find a stepping dead thread in the thread list in two
7693 cases:
2adfaa28 7694
2ac7589c
PA
7695 - The target supports thread exit events, and when the target
7696 tries to delete the thread from the thread list, inferior_ptid
7697 pointed at the exiting thread. In such case, calling
7698 delete_thread does not really remove the thread from the list;
7699 instead, the thread is left listed, with 'exited' state.
64ce06e4 7700
2ac7589c
PA
7701 - The target's debug interface does not support thread exit
7702 events, and so we have no idea whatsoever if the previously
7703 stepping thread is still alive. For that reason, we need to
7704 synchronously query the target now. */
2adfaa28 7705
00431a78 7706 if (tp->state == THREAD_EXITED || !target_thread_alive (tp->ptid))
2ac7589c 7707 {
1eb8556f
SM
7708 infrun_debug_printf ("not resuming previously stepped thread, it has "
7709 "vanished");
2ac7589c 7710
00431a78 7711 delete_thread (tp);
c4464ade 7712 return false;
c447ac0b 7713 }
2ac7589c 7714
1eb8556f 7715 infrun_debug_printf ("resuming previously stepped thread");
2ac7589c
PA
7716
7717 reset_ecs (ecs, tp);
00431a78 7718 switch_to_thread (tp);
2ac7589c 7719
f2ffa92b 7720 tp->suspend.stop_pc = regcache_read_pc (get_thread_regcache (tp));
2ac7589c 7721 frame = get_current_frame ();
2ac7589c
PA
7722
7723 /* If the PC of the thread we were trying to single-step has
7724 changed, then that thread has trapped or been signaled, but the
7725 event has not been reported to GDB yet. Re-poll the target
7726 looking for this particular thread's event (i.e. temporarily
7727 enable schedlock) by:
7728
7729 - setting a break at the current PC
7730 - resuming that particular thread, only (by setting trap
7731 expected)
7732
7733 This prevents us continuously moving the single-step breakpoint
7734 forward, one instruction at a time, overstepping. */
7735
f2ffa92b 7736 if (tp->suspend.stop_pc != tp->prev_pc)
2ac7589c
PA
7737 {
7738 ptid_t resume_ptid;
7739
1eb8556f
SM
7740 infrun_debug_printf ("expected thread advanced also (%s -> %s)",
7741 paddress (target_gdbarch (), tp->prev_pc),
7742 paddress (target_gdbarch (), tp->suspend.stop_pc));
2ac7589c
PA
7743
7744 /* Clear the info of the previous step-over, as it's no longer
7745 valid (if the thread was trying to step over a breakpoint, it
7746 has already succeeded). It's what keep_going would do too,
7747 if we called it. Do this before trying to insert the sss
7748 breakpoint, otherwise if we were previously trying to step
7749 over this exact address in another thread, the breakpoint is
7750 skipped. */
7751 clear_step_over_info ();
7752 tp->control.trap_expected = 0;
7753
7754 insert_single_step_breakpoint (get_frame_arch (frame),
7755 get_frame_address_space (frame),
f2ffa92b 7756 tp->suspend.stop_pc);
2ac7589c 7757
719546c4 7758 tp->resumed = true;
fbea99ea 7759 resume_ptid = internal_resume_ptid (tp->control.stepping_command);
c4464ade 7760 do_target_resume (resume_ptid, false, GDB_SIGNAL_0);
2ac7589c
PA
7761 }
7762 else
7763 {
1eb8556f 7764 infrun_debug_printf ("expected thread still hasn't advanced");
2ac7589c
PA
7765
7766 keep_going_pass_signal (ecs);
7767 }
c4464ade
SM
7768
7769 return true;
c447ac0b
PA
7770}
7771
8b061563
PA
7772/* Is thread TP in the middle of (software or hardware)
7773 single-stepping? (Note the result of this function must never be
7774 passed directly as target_resume's STEP parameter.) */
104c1213 7775
c4464ade 7776static bool
b3444185 7777currently_stepping (struct thread_info *tp)
a7212384 7778{
8358c15c
JK
7779 return ((tp->control.step_range_end
7780 && tp->control.step_resume_breakpoint == NULL)
7781 || tp->control.trap_expected
af48d08f 7782 || tp->stepped_breakpoint
8358c15c 7783 || bpstat_should_step ());
a7212384
UW
7784}
7785
b2175913
MS
7786/* Inferior has stepped into a subroutine call with source code that
7787 we should not step over. Do step to the first line of code in
7788 it. */
c2c6d25f
JM
7789
7790static void
568d6575
UW
7791handle_step_into_function (struct gdbarch *gdbarch,
7792 struct execution_control_state *ecs)
c2c6d25f 7793{
7e324e48
GB
7794 fill_in_stop_func (gdbarch, ecs);
7795
f2ffa92b
PA
7796 compunit_symtab *cust
7797 = find_pc_compunit_symtab (ecs->event_thread->suspend.stop_pc);
43f3e411 7798 if (cust != NULL && compunit_language (cust) != language_asm)
46a62268
YQ
7799 ecs->stop_func_start
7800 = gdbarch_skip_prologue_noexcept (gdbarch, ecs->stop_func_start);
c2c6d25f 7801
51abb421 7802 symtab_and_line stop_func_sal = find_pc_line (ecs->stop_func_start, 0);
c2c6d25f
JM
7803 /* Use the step_resume_break to step until the end of the prologue,
7804 even if that involves jumps (as it seems to on the vax under
7805 4.2). */
7806 /* If the prologue ends in the middle of a source line, continue to
7807 the end of that source line (if it is still within the function).
7808 Otherwise, just go to end of prologue. */
2afb61aa
PA
7809 if (stop_func_sal.end
7810 && stop_func_sal.pc != ecs->stop_func_start
7811 && stop_func_sal.end < ecs->stop_func_end)
7812 ecs->stop_func_start = stop_func_sal.end;
c2c6d25f 7813
2dbd5e30
KB
7814 /* Architectures which require breakpoint adjustment might not be able
7815 to place a breakpoint at the computed address. If so, the test
7816 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
7817 ecs->stop_func_start to an address at which a breakpoint may be
7818 legitimately placed.
8fb3e588 7819
2dbd5e30
KB
7820 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
7821 made, GDB will enter an infinite loop when stepping through
7822 optimized code consisting of VLIW instructions which contain
7823 subinstructions corresponding to different source lines. On
7824 FR-V, it's not permitted to place a breakpoint on any but the
7825 first subinstruction of a VLIW instruction. When a breakpoint is
7826 set, GDB will adjust the breakpoint address to the beginning of
7827 the VLIW instruction. Thus, we need to make the corresponding
7828 adjustment here when computing the stop address. */
8fb3e588 7829
568d6575 7830 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
2dbd5e30
KB
7831 {
7832 ecs->stop_func_start
568d6575 7833 = gdbarch_adjust_breakpoint_address (gdbarch,
8fb3e588 7834 ecs->stop_func_start);
2dbd5e30
KB
7835 }
7836
f2ffa92b 7837 if (ecs->stop_func_start == ecs->event_thread->suspend.stop_pc)
c2c6d25f
JM
7838 {
7839 /* We are already there: stop now. */
bdc36728 7840 end_stepping_range (ecs);
c2c6d25f
JM
7841 return;
7842 }
7843 else
7844 {
7845 /* Put the step-breakpoint there and go until there. */
51abb421 7846 symtab_and_line sr_sal;
c2c6d25f
JM
7847 sr_sal.pc = ecs->stop_func_start;
7848 sr_sal.section = find_pc_overlay (ecs->stop_func_start);
6c95b8df 7849 sr_sal.pspace = get_frame_program_space (get_current_frame ());
44cbf7b5 7850
c2c6d25f 7851 /* Do not specify what the fp should be when we stop since on
dda83cd7
SM
7852 some machines the prologue is where the new fp value is
7853 established. */
a6d9a66e 7854 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal, null_frame_id);
c2c6d25f
JM
7855
7856 /* And make sure stepping stops right away then. */
16c381f0 7857 ecs->event_thread->control.step_range_end
dda83cd7 7858 = ecs->event_thread->control.step_range_start;
c2c6d25f
JM
7859 }
7860 keep_going (ecs);
7861}
d4f3574e 7862
b2175913
MS
7863/* Inferior has stepped backward into a subroutine call with source
7864 code that we should not step over. Do step to the beginning of the
7865 last line of code in it. */
7866
7867static void
568d6575
UW
7868handle_step_into_function_backward (struct gdbarch *gdbarch,
7869 struct execution_control_state *ecs)
b2175913 7870{
43f3e411 7871 struct compunit_symtab *cust;
167e4384 7872 struct symtab_and_line stop_func_sal;
b2175913 7873
7e324e48
GB
7874 fill_in_stop_func (gdbarch, ecs);
7875
f2ffa92b 7876 cust = find_pc_compunit_symtab (ecs->event_thread->suspend.stop_pc);
43f3e411 7877 if (cust != NULL && compunit_language (cust) != language_asm)
46a62268
YQ
7878 ecs->stop_func_start
7879 = gdbarch_skip_prologue_noexcept (gdbarch, ecs->stop_func_start);
b2175913 7880
f2ffa92b 7881 stop_func_sal = find_pc_line (ecs->event_thread->suspend.stop_pc, 0);
b2175913
MS
7882
7883 /* OK, we're just going to keep stepping here. */
f2ffa92b 7884 if (stop_func_sal.pc == ecs->event_thread->suspend.stop_pc)
b2175913
MS
7885 {
7886 /* We're there already. Just stop stepping now. */
bdc36728 7887 end_stepping_range (ecs);
b2175913
MS
7888 }
7889 else
7890 {
7891 /* Else just reset the step range and keep going.
7892 No step-resume breakpoint, they don't work for
7893 epilogues, which can have multiple entry paths. */
16c381f0
JK
7894 ecs->event_thread->control.step_range_start = stop_func_sal.pc;
7895 ecs->event_thread->control.step_range_end = stop_func_sal.end;
b2175913
MS
7896 keep_going (ecs);
7897 }
7898 return;
7899}
7900
d3169d93 7901/* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
44cbf7b5
AC
7902 This is used to both functions and to skip over code. */
7903
7904static void
2c03e5be
PA
7905insert_step_resume_breakpoint_at_sal_1 (struct gdbarch *gdbarch,
7906 struct symtab_and_line sr_sal,
7907 struct frame_id sr_id,
7908 enum bptype sr_type)
44cbf7b5 7909{
611c83ae
PA
7910 /* There should never be more than one step-resume or longjmp-resume
7911 breakpoint per thread, so we should never be setting a new
44cbf7b5 7912 step_resume_breakpoint when one is already active. */
8358c15c 7913 gdb_assert (inferior_thread ()->control.step_resume_breakpoint == NULL);
2c03e5be 7914 gdb_assert (sr_type == bp_step_resume || sr_type == bp_hp_step_resume);
d3169d93 7915
1eb8556f
SM
7916 infrun_debug_printf ("inserting step-resume breakpoint at %s",
7917 paddress (gdbarch, sr_sal.pc));
d3169d93 7918
8358c15c 7919 inferior_thread ()->control.step_resume_breakpoint
454dafbd 7920 = set_momentary_breakpoint (gdbarch, sr_sal, sr_id, sr_type).release ();
2c03e5be
PA
7921}
7922
9da8c2a0 7923void
2c03e5be
PA
7924insert_step_resume_breakpoint_at_sal (struct gdbarch *gdbarch,
7925 struct symtab_and_line sr_sal,
7926 struct frame_id sr_id)
7927{
7928 insert_step_resume_breakpoint_at_sal_1 (gdbarch,
7929 sr_sal, sr_id,
7930 bp_step_resume);
44cbf7b5 7931}
7ce450bd 7932
2c03e5be
PA
7933/* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
7934 This is used to skip a potential signal handler.
7ce450bd 7935
14e60db5
DJ
7936 This is called with the interrupted function's frame. The signal
7937 handler, when it returns, will resume the interrupted function at
7938 RETURN_FRAME.pc. */
d303a6c7
AC
7939
7940static void
2c03e5be 7941insert_hp_step_resume_breakpoint_at_frame (struct frame_info *return_frame)
d303a6c7 7942{
f4c1edd8 7943 gdb_assert (return_frame != NULL);
d303a6c7 7944
51abb421
PA
7945 struct gdbarch *gdbarch = get_frame_arch (return_frame);
7946
7947 symtab_and_line sr_sal;
568d6575 7948 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch, get_frame_pc (return_frame));
d303a6c7 7949 sr_sal.section = find_pc_overlay (sr_sal.pc);
6c95b8df 7950 sr_sal.pspace = get_frame_program_space (return_frame);
d303a6c7 7951
2c03e5be
PA
7952 insert_step_resume_breakpoint_at_sal_1 (gdbarch, sr_sal,
7953 get_stack_frame_id (return_frame),
7954 bp_hp_step_resume);
d303a6c7
AC
7955}
7956
2c03e5be
PA
7957/* Insert a "step-resume breakpoint" at the previous frame's PC. This
7958 is used to skip a function after stepping into it (for "next" or if
7959 the called function has no debugging information).
14e60db5
DJ
7960
7961 The current function has almost always been reached by single
7962 stepping a call or return instruction. NEXT_FRAME belongs to the
7963 current function, and the breakpoint will be set at the caller's
7964 resume address.
7965
7966 This is a separate function rather than reusing
2c03e5be 7967 insert_hp_step_resume_breakpoint_at_frame in order to avoid
14e60db5 7968 get_prev_frame, which may stop prematurely (see the implementation
c7ce8faa 7969 of frame_unwind_caller_id for an example). */
14e60db5
DJ
7970
7971static void
7972insert_step_resume_breakpoint_at_caller (struct frame_info *next_frame)
7973{
14e60db5
DJ
7974 /* We shouldn't have gotten here if we don't know where the call site
7975 is. */
c7ce8faa 7976 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame)));
14e60db5 7977
51abb421 7978 struct gdbarch *gdbarch = frame_unwind_caller_arch (next_frame);
14e60db5 7979
51abb421 7980 symtab_and_line sr_sal;
c7ce8faa
DJ
7981 sr_sal.pc = gdbarch_addr_bits_remove (gdbarch,
7982 frame_unwind_caller_pc (next_frame));
14e60db5 7983 sr_sal.section = find_pc_overlay (sr_sal.pc);
6c95b8df 7984 sr_sal.pspace = frame_unwind_program_space (next_frame);
14e60db5 7985
a6d9a66e 7986 insert_step_resume_breakpoint_at_sal (gdbarch, sr_sal,
c7ce8faa 7987 frame_unwind_caller_id (next_frame));
14e60db5
DJ
7988}
7989
611c83ae
PA
7990/* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
7991 new breakpoint at the target of a jmp_buf. The handling of
7992 longjmp-resume uses the same mechanisms used for handling
7993 "step-resume" breakpoints. */
7994
7995static void
a6d9a66e 7996insert_longjmp_resume_breakpoint (struct gdbarch *gdbarch, CORE_ADDR pc)
611c83ae 7997{
e81a37f7
TT
7998 /* There should never be more than one longjmp-resume breakpoint per
7999 thread, so we should never be setting a new
611c83ae 8000 longjmp_resume_breakpoint when one is already active. */
e81a37f7 8001 gdb_assert (inferior_thread ()->control.exception_resume_breakpoint == NULL);
611c83ae 8002
1eb8556f
SM
8003 infrun_debug_printf ("inserting longjmp-resume breakpoint at %s",
8004 paddress (gdbarch, pc));
611c83ae 8005
e81a37f7 8006 inferior_thread ()->control.exception_resume_breakpoint =
454dafbd 8007 set_momentary_breakpoint_at_pc (gdbarch, pc, bp_longjmp_resume).release ();
611c83ae
PA
8008}
8009
186c406b
TT
8010/* Insert an exception resume breakpoint. TP is the thread throwing
8011 the exception. The block B is the block of the unwinder debug hook
8012 function. FRAME is the frame corresponding to the call to this
8013 function. SYM is the symbol of the function argument holding the
8014 target PC of the exception. */
8015
8016static void
8017insert_exception_resume_breakpoint (struct thread_info *tp,
3977b71f 8018 const struct block *b,
186c406b
TT
8019 struct frame_info *frame,
8020 struct symbol *sym)
8021{
a70b8144 8022 try
186c406b 8023 {
63e43d3a 8024 struct block_symbol vsym;
186c406b
TT
8025 struct value *value;
8026 CORE_ADDR handler;
8027 struct breakpoint *bp;
8028
987012b8 8029 vsym = lookup_symbol_search_name (sym->search_name (),
de63c46b 8030 b, VAR_DOMAIN);
63e43d3a 8031 value = read_var_value (vsym.symbol, vsym.block, frame);
186c406b
TT
8032 /* If the value was optimized out, revert to the old behavior. */
8033 if (! value_optimized_out (value))
8034 {
8035 handler = value_as_address (value);
8036
1eb8556f
SM
8037 infrun_debug_printf ("exception resume at %lx",
8038 (unsigned long) handler);
186c406b
TT
8039
8040 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
454dafbd
TT
8041 handler,
8042 bp_exception_resume).release ();
c70a6932
JK
8043
8044 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
8045 frame = NULL;
8046
5d5658a1 8047 bp->thread = tp->global_num;
186c406b
TT
8048 inferior_thread ()->control.exception_resume_breakpoint = bp;
8049 }
8050 }
230d2906 8051 catch (const gdb_exception_error &e)
492d29ea
PA
8052 {
8053 /* We want to ignore errors here. */
8054 }
186c406b
TT
8055}
8056
28106bc2
SDJ
8057/* A helper for check_exception_resume that sets an
8058 exception-breakpoint based on a SystemTap probe. */
8059
8060static void
8061insert_exception_resume_from_probe (struct thread_info *tp,
729662a5 8062 const struct bound_probe *probe,
28106bc2
SDJ
8063 struct frame_info *frame)
8064{
8065 struct value *arg_value;
8066 CORE_ADDR handler;
8067 struct breakpoint *bp;
8068
8069 arg_value = probe_safe_evaluate_at_pc (frame, 1);
8070 if (!arg_value)
8071 return;
8072
8073 handler = value_as_address (arg_value);
8074
1eb8556f
SM
8075 infrun_debug_printf ("exception resume at %s",
8076 paddress (probe->objfile->arch (), handler));
28106bc2
SDJ
8077
8078 bp = set_momentary_breakpoint_at_pc (get_frame_arch (frame),
454dafbd 8079 handler, bp_exception_resume).release ();
5d5658a1 8080 bp->thread = tp->global_num;
28106bc2
SDJ
8081 inferior_thread ()->control.exception_resume_breakpoint = bp;
8082}
8083
186c406b
TT
8084/* This is called when an exception has been intercepted. Check to
8085 see whether the exception's destination is of interest, and if so,
8086 set an exception resume breakpoint there. */
8087
8088static void
8089check_exception_resume (struct execution_control_state *ecs,
28106bc2 8090 struct frame_info *frame)
186c406b 8091{
729662a5 8092 struct bound_probe probe;
28106bc2
SDJ
8093 struct symbol *func;
8094
8095 /* First see if this exception unwinding breakpoint was set via a
8096 SystemTap probe point. If so, the probe has two arguments: the
8097 CFA and the HANDLER. We ignore the CFA, extract the handler, and
8098 set a breakpoint there. */
6bac7473 8099 probe = find_probe_by_pc (get_frame_pc (frame));
935676c9 8100 if (probe.prob)
28106bc2 8101 {
729662a5 8102 insert_exception_resume_from_probe (ecs->event_thread, &probe, frame);
28106bc2
SDJ
8103 return;
8104 }
8105
8106 func = get_frame_function (frame);
8107 if (!func)
8108 return;
186c406b 8109
a70b8144 8110 try
186c406b 8111 {
3977b71f 8112 const struct block *b;
8157b174 8113 struct block_iterator iter;
186c406b
TT
8114 struct symbol *sym;
8115 int argno = 0;
8116
8117 /* The exception breakpoint is a thread-specific breakpoint on
8118 the unwinder's debug hook, declared as:
8119
8120 void _Unwind_DebugHook (void *cfa, void *handler);
8121
8122 The CFA argument indicates the frame to which control is
8123 about to be transferred. HANDLER is the destination PC.
8124
8125 We ignore the CFA and set a temporary breakpoint at HANDLER.
8126 This is not extremely efficient but it avoids issues in gdb
8127 with computing the DWARF CFA, and it also works even in weird
8128 cases such as throwing an exception from inside a signal
8129 handler. */
8130
8131 b = SYMBOL_BLOCK_VALUE (func);
8132 ALL_BLOCK_SYMBOLS (b, iter, sym)
8133 {
8134 if (!SYMBOL_IS_ARGUMENT (sym))
8135 continue;
8136
8137 if (argno == 0)
8138 ++argno;
8139 else
8140 {
8141 insert_exception_resume_breakpoint (ecs->event_thread,
8142 b, frame, sym);
8143 break;
8144 }
8145 }
8146 }
230d2906 8147 catch (const gdb_exception_error &e)
492d29ea
PA
8148 {
8149 }
186c406b
TT
8150}
8151
104c1213 8152static void
22bcd14b 8153stop_waiting (struct execution_control_state *ecs)
104c1213 8154{
1eb8556f 8155 infrun_debug_printf ("stop_waiting");
527159b7 8156
cd0fc7c3
SS
8157 /* Let callers know we don't want to wait for the inferior anymore. */
8158 ecs->wait_some_more = 0;
fbea99ea 8159
53cccef1 8160 /* If all-stop, but there exists a non-stop target, stop all
fbea99ea 8161 threads now that we're presenting the stop to the user. */
53cccef1 8162 if (!non_stop && exists_non_stop_target ())
2bf06f96 8163 stop_all_threads ("presenting stop to user in all-stop");
cd0fc7c3
SS
8164}
8165
4d9d9d04
PA
8166/* Like keep_going, but passes the signal to the inferior, even if the
8167 signal is set to nopass. */
d4f3574e
SS
8168
8169static void
4d9d9d04 8170keep_going_pass_signal (struct execution_control_state *ecs)
d4f3574e 8171{
d7e15655 8172 gdb_assert (ecs->event_thread->ptid == inferior_ptid);
372316f1 8173 gdb_assert (!ecs->event_thread->resumed);
4d9d9d04 8174
d4f3574e 8175 /* Save the pc before execution, to compare with pc after stop. */
fb14de7b 8176 ecs->event_thread->prev_pc
fc75c28b 8177 = regcache_read_pc_protected (get_thread_regcache (ecs->event_thread));
d4f3574e 8178
4d9d9d04 8179 if (ecs->event_thread->control.trap_expected)
d4f3574e 8180 {
4d9d9d04
PA
8181 struct thread_info *tp = ecs->event_thread;
8182
1eb8556f
SM
8183 infrun_debug_printf ("%s has trap_expected set, "
8184 "resuming to collect trap",
8185 target_pid_to_str (tp->ptid).c_str ());
4d9d9d04 8186
a9ba6bae
PA
8187 /* We haven't yet gotten our trap, and either: intercepted a
8188 non-signal event (e.g., a fork); or took a signal which we
8189 are supposed to pass through to the inferior. Simply
8190 continue. */
64ce06e4 8191 resume (ecs->event_thread->suspend.stop_signal);
d4f3574e 8192 }
372316f1
PA
8193 else if (step_over_info_valid_p ())
8194 {
8195 /* Another thread is stepping over a breakpoint in-line. If
8196 this thread needs a step-over too, queue the request. In
8197 either case, this resume must be deferred for later. */
8198 struct thread_info *tp = ecs->event_thread;
8199
8200 if (ecs->hit_singlestep_breakpoint
8201 || thread_still_needs_step_over (tp))
8202 {
1eb8556f
SM
8203 infrun_debug_printf ("step-over already in progress: "
8204 "step-over for %s deferred",
8205 target_pid_to_str (tp->ptid).c_str ());
28d5518b 8206 global_thread_step_over_chain_enqueue (tp);
372316f1
PA
8207 }
8208 else
8209 {
1eb8556f
SM
8210 infrun_debug_printf ("step-over in progress: resume of %s deferred",
8211 target_pid_to_str (tp->ptid).c_str ());
372316f1 8212 }
372316f1 8213 }
d4f3574e
SS
8214 else
8215 {
31e77af2 8216 struct regcache *regcache = get_current_regcache ();
963f9c80
PA
8217 int remove_bp;
8218 int remove_wps;
8d297bbf 8219 step_over_what step_what;
31e77af2 8220
d4f3574e 8221 /* Either the trap was not expected, but we are continuing
a9ba6bae
PA
8222 anyway (if we got a signal, the user asked it be passed to
8223 the child)
8224 -- or --
8225 We got our expected trap, but decided we should resume from
8226 it.
d4f3574e 8227
a9ba6bae 8228 We're going to run this baby now!
d4f3574e 8229
c36b740a
VP
8230 Note that insert_breakpoints won't try to re-insert
8231 already inserted breakpoints. Therefore, we don't
8232 care if breakpoints were already inserted, or not. */
a9ba6bae 8233
31e77af2
PA
8234 /* If we need to step over a breakpoint, and we're not using
8235 displaced stepping to do so, insert all breakpoints
8236 (watchpoints, etc.) but the one we're stepping over, step one
8237 instruction, and then re-insert the breakpoint when that step
8238 is finished. */
963f9c80 8239
6c4cfb24
PA
8240 step_what = thread_still_needs_step_over (ecs->event_thread);
8241
963f9c80 8242 remove_bp = (ecs->hit_singlestep_breakpoint
6c4cfb24
PA
8243 || (step_what & STEP_OVER_BREAKPOINT));
8244 remove_wps = (step_what & STEP_OVER_WATCHPOINT);
963f9c80 8245
cb71640d
PA
8246 /* We can't use displaced stepping if we need to step past a
8247 watchpoint. The instruction copied to the scratch pad would
8248 still trigger the watchpoint. */
8249 if (remove_bp
3fc8eb30 8250 && (remove_wps || !use_displaced_stepping (ecs->event_thread)))
45e8c884 8251 {
a01bda52 8252 set_step_over_info (regcache->aspace (),
21edc42f
YQ
8253 regcache_read_pc (regcache), remove_wps,
8254 ecs->event_thread->global_num);
45e8c884 8255 }
963f9c80 8256 else if (remove_wps)
21edc42f 8257 set_step_over_info (NULL, 0, remove_wps, -1);
372316f1
PA
8258
8259 /* If we now need to do an in-line step-over, we need to stop
8260 all other threads. Note this must be done before
8261 insert_breakpoints below, because that removes the breakpoint
8262 we're about to step over, otherwise other threads could miss
8263 it. */
fbea99ea 8264 if (step_over_info_valid_p () && target_is_non_stop_p ())
2bf06f96 8265 stop_all_threads ("starting in-line step-over");
abbb1732 8266
31e77af2 8267 /* Stop stepping if inserting breakpoints fails. */
a70b8144 8268 try
31e77af2
PA
8269 {
8270 insert_breakpoints ();
8271 }
230d2906 8272 catch (const gdb_exception_error &e)
31e77af2
PA
8273 {
8274 exception_print (gdb_stderr, e);
22bcd14b 8275 stop_waiting (ecs);
bdf2a94a 8276 clear_step_over_info ();
31e77af2 8277 return;
d4f3574e
SS
8278 }
8279
963f9c80 8280 ecs->event_thread->control.trap_expected = (remove_bp || remove_wps);
d4f3574e 8281
64ce06e4 8282 resume (ecs->event_thread->suspend.stop_signal);
d4f3574e
SS
8283 }
8284
488f131b 8285 prepare_to_wait (ecs);
d4f3574e
SS
8286}
8287
4d9d9d04
PA
8288/* Called when we should continue running the inferior, because the
8289 current event doesn't cause a user visible stop. This does the
8290 resuming part; waiting for the next event is done elsewhere. */
8291
8292static void
8293keep_going (struct execution_control_state *ecs)
8294{
8295 if (ecs->event_thread->control.trap_expected
8296 && ecs->event_thread->suspend.stop_signal == GDB_SIGNAL_TRAP)
8297 ecs->event_thread->control.trap_expected = 0;
8298
8299 if (!signal_program[ecs->event_thread->suspend.stop_signal])
8300 ecs->event_thread->suspend.stop_signal = GDB_SIGNAL_0;
8301 keep_going_pass_signal (ecs);
8302}
8303
104c1213
JM
8304/* This function normally comes after a resume, before
8305 handle_inferior_event exits. It takes care of any last bits of
8306 housekeeping, and sets the all-important wait_some_more flag. */
cd0fc7c3 8307
104c1213
JM
8308static void
8309prepare_to_wait (struct execution_control_state *ecs)
cd0fc7c3 8310{
1eb8556f 8311 infrun_debug_printf ("prepare_to_wait");
104c1213 8312
104c1213 8313 ecs->wait_some_more = 1;
0b333c5e 8314
42bd97a6
PA
8315 /* If the target can't async, emulate it by marking the infrun event
8316 handler such that as soon as we get back to the event-loop, we
8317 immediately end up in fetch_inferior_event again calling
8318 target_wait. */
8319 if (!target_can_async_p ())
0b333c5e 8320 mark_infrun_async_event_handler ();
c906108c 8321}
11cf8741 8322
fd664c91 8323/* We are done with the step range of a step/next/si/ni command.
b57bacec 8324 Called once for each n of a "step n" operation. */
fd664c91
PA
8325
8326static void
bdc36728 8327end_stepping_range (struct execution_control_state *ecs)
fd664c91 8328{
bdc36728 8329 ecs->event_thread->control.stop_step = 1;
bdc36728 8330 stop_waiting (ecs);
fd664c91
PA
8331}
8332
33d62d64
JK
8333/* Several print_*_reason functions to print why the inferior has stopped.
8334 We always print something when the inferior exits, or receives a signal.
8335 The rest of the cases are dealt with later on in normal_stop and
8336 print_it_typical. Ideally there should be a call to one of these
8337 print_*_reason functions functions from handle_inferior_event each time
22bcd14b 8338 stop_waiting is called.
33d62d64 8339
fd664c91
PA
8340 Note that we don't call these directly, instead we delegate that to
8341 the interpreters, through observers. Interpreters then call these
8342 with whatever uiout is right. */
33d62d64 8343
fd664c91
PA
8344void
8345print_end_stepping_range_reason (struct ui_out *uiout)
33d62d64 8346{
fd664c91 8347 /* For CLI-like interpreters, print nothing. */
33d62d64 8348
112e8700 8349 if (uiout->is_mi_like_p ())
fd664c91 8350 {
112e8700 8351 uiout->field_string ("reason",
fd664c91
PA
8352 async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE));
8353 }
8354}
33d62d64 8355
fd664c91
PA
8356void
8357print_signal_exited_reason (struct ui_out *uiout, enum gdb_signal siggnal)
11cf8741 8358{
33d62d64 8359 annotate_signalled ();
112e8700
SM
8360 if (uiout->is_mi_like_p ())
8361 uiout->field_string
8362 ("reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED));
8363 uiout->text ("\nProgram terminated with signal ");
33d62d64 8364 annotate_signal_name ();
112e8700 8365 uiout->field_string ("signal-name",
2ea28649 8366 gdb_signal_to_name (siggnal));
33d62d64 8367 annotate_signal_name_end ();
112e8700 8368 uiout->text (", ");
33d62d64 8369 annotate_signal_string ();
112e8700 8370 uiout->field_string ("signal-meaning",
2ea28649 8371 gdb_signal_to_string (siggnal));
33d62d64 8372 annotate_signal_string_end ();
112e8700
SM
8373 uiout->text (".\n");
8374 uiout->text ("The program no longer exists.\n");
33d62d64
JK
8375}
8376
fd664c91
PA
8377void
8378print_exited_reason (struct ui_out *uiout, int exitstatus)
33d62d64 8379{
fda326dd 8380 struct inferior *inf = current_inferior ();
a068643d 8381 std::string pidstr = target_pid_to_str (ptid_t (inf->pid));
fda326dd 8382
33d62d64
JK
8383 annotate_exited (exitstatus);
8384 if (exitstatus)
8385 {
112e8700
SM
8386 if (uiout->is_mi_like_p ())
8387 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXITED));
6a831f06
PA
8388 std::string exit_code_str
8389 = string_printf ("0%o", (unsigned int) exitstatus);
8390 uiout->message ("[Inferior %s (%s) exited with code %pF]\n",
8391 plongest (inf->num), pidstr.c_str (),
8392 string_field ("exit-code", exit_code_str.c_str ()));
33d62d64
JK
8393 }
8394 else
11cf8741 8395 {
112e8700
SM
8396 if (uiout->is_mi_like_p ())
8397 uiout->field_string
8398 ("reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY));
6a831f06
PA
8399 uiout->message ("[Inferior %s (%s) exited normally]\n",
8400 plongest (inf->num), pidstr.c_str ());
33d62d64 8401 }
33d62d64
JK
8402}
8403
fd664c91
PA
8404void
8405print_signal_received_reason (struct ui_out *uiout, enum gdb_signal siggnal)
33d62d64 8406{
f303dbd6
PA
8407 struct thread_info *thr = inferior_thread ();
8408
33d62d64
JK
8409 annotate_signal ();
8410
112e8700 8411 if (uiout->is_mi_like_p ())
f303dbd6
PA
8412 ;
8413 else if (show_thread_that_caused_stop ())
33d62d64 8414 {
f303dbd6 8415 const char *name;
33d62d64 8416
112e8700 8417 uiout->text ("\nThread ");
33eca680 8418 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
8419
8420 name = thr->name != NULL ? thr->name : target_thread_name (thr);
8421 if (name != NULL)
8422 {
112e8700 8423 uiout->text (" \"");
33eca680 8424 uiout->field_string ("name", name);
112e8700 8425 uiout->text ("\"");
f303dbd6 8426 }
33d62d64 8427 }
f303dbd6 8428 else
112e8700 8429 uiout->text ("\nProgram");
f303dbd6 8430
112e8700
SM
8431 if (siggnal == GDB_SIGNAL_0 && !uiout->is_mi_like_p ())
8432 uiout->text (" stopped");
33d62d64
JK
8433 else
8434 {
112e8700 8435 uiout->text (" received signal ");
8b93c638 8436 annotate_signal_name ();
112e8700
SM
8437 if (uiout->is_mi_like_p ())
8438 uiout->field_string
8439 ("reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED));
8440 uiout->field_string ("signal-name", gdb_signal_to_name (siggnal));
8b93c638 8441 annotate_signal_name_end ();
112e8700 8442 uiout->text (", ");
8b93c638 8443 annotate_signal_string ();
112e8700 8444 uiout->field_string ("signal-meaning", gdb_signal_to_string (siggnal));
012b3a21 8445
272bb05c
JB
8446 struct regcache *regcache = get_current_regcache ();
8447 struct gdbarch *gdbarch = regcache->arch ();
8448 if (gdbarch_report_signal_info_p (gdbarch))
8449 gdbarch_report_signal_info (gdbarch, uiout, siggnal);
8450
8b93c638 8451 annotate_signal_string_end ();
33d62d64 8452 }
112e8700 8453 uiout->text (".\n");
33d62d64 8454}
252fbfc8 8455
fd664c91
PA
8456void
8457print_no_history_reason (struct ui_out *uiout)
33d62d64 8458{
112e8700 8459 uiout->text ("\nNo more reverse-execution history.\n");
11cf8741 8460}
43ff13b4 8461
0c7e1a46
PA
8462/* Print current location without a level number, if we have changed
8463 functions or hit a breakpoint. Print source line if we have one.
8464 bpstat_print contains the logic deciding in detail what to print,
8465 based on the event(s) that just occurred. */
8466
243a9253
PA
8467static void
8468print_stop_location (struct target_waitstatus *ws)
0c7e1a46
PA
8469{
8470 int bpstat_ret;
f486487f 8471 enum print_what source_flag;
0c7e1a46
PA
8472 int do_frame_printing = 1;
8473 struct thread_info *tp = inferior_thread ();
8474
8475 bpstat_ret = bpstat_print (tp->control.stop_bpstat, ws->kind);
8476 switch (bpstat_ret)
8477 {
8478 case PRINT_UNKNOWN:
8479 /* FIXME: cagney/2002-12-01: Given that a frame ID does (or
8480 should) carry around the function and does (or should) use
8481 that when doing a frame comparison. */
8482 if (tp->control.stop_step
8483 && frame_id_eq (tp->control.step_frame_id,
8484 get_frame_id (get_current_frame ()))
f2ffa92b
PA
8485 && (tp->control.step_start_function
8486 == find_pc_function (tp->suspend.stop_pc)))
0c7e1a46
PA
8487 {
8488 /* Finished step, just print source line. */
8489 source_flag = SRC_LINE;
8490 }
8491 else
8492 {
8493 /* Print location and source line. */
8494 source_flag = SRC_AND_LOC;
8495 }
8496 break;
8497 case PRINT_SRC_AND_LOC:
8498 /* Print location and source line. */
8499 source_flag = SRC_AND_LOC;
8500 break;
8501 case PRINT_SRC_ONLY:
8502 source_flag = SRC_LINE;
8503 break;
8504 case PRINT_NOTHING:
8505 /* Something bogus. */
8506 source_flag = SRC_LINE;
8507 do_frame_printing = 0;
8508 break;
8509 default:
8510 internal_error (__FILE__, __LINE__, _("Unknown value."));
8511 }
8512
8513 /* The behavior of this routine with respect to the source
8514 flag is:
8515 SRC_LINE: Print only source line
8516 LOCATION: Print only location
8517 SRC_AND_LOC: Print location and source line. */
8518 if (do_frame_printing)
8519 print_stack_frame (get_selected_frame (NULL), 0, source_flag, 1);
243a9253
PA
8520}
8521
243a9253
PA
8522/* See infrun.h. */
8523
8524void
4c7d57e7 8525print_stop_event (struct ui_out *uiout, bool displays)
243a9253 8526{
243a9253 8527 struct target_waitstatus last;
243a9253
PA
8528 struct thread_info *tp;
8529
5b6d1e4f 8530 get_last_target_status (nullptr, nullptr, &last);
243a9253 8531
67ad9399
TT
8532 {
8533 scoped_restore save_uiout = make_scoped_restore (&current_uiout, uiout);
0c7e1a46 8534
67ad9399 8535 print_stop_location (&last);
243a9253 8536
67ad9399 8537 /* Display the auto-display expressions. */
4c7d57e7
TT
8538 if (displays)
8539 do_displays ();
67ad9399 8540 }
243a9253
PA
8541
8542 tp = inferior_thread ();
8543 if (tp->thread_fsm != NULL
46e3ed7f 8544 && tp->thread_fsm->finished_p ())
243a9253
PA
8545 {
8546 struct return_value_info *rv;
8547
46e3ed7f 8548 rv = tp->thread_fsm->return_value ();
243a9253
PA
8549 if (rv != NULL)
8550 print_return_value (uiout, rv);
8551 }
0c7e1a46
PA
8552}
8553
388a7084
PA
8554/* See infrun.h. */
8555
8556void
8557maybe_remove_breakpoints (void)
8558{
55f6301a 8559 if (!breakpoints_should_be_inserted_now () && target_has_execution ())
388a7084
PA
8560 {
8561 if (remove_breakpoints ())
8562 {
223ffa71 8563 target_terminal::ours_for_output ();
388a7084
PA
8564 printf_filtered (_("Cannot remove breakpoints because "
8565 "program is no longer writable.\nFurther "
8566 "execution is probably impossible.\n"));
8567 }
8568 }
8569}
8570
4c2f2a79
PA
8571/* The execution context that just caused a normal stop. */
8572
8573struct stop_context
8574{
2d844eaf 8575 stop_context ();
2d844eaf
TT
8576
8577 DISABLE_COPY_AND_ASSIGN (stop_context);
8578
8579 bool changed () const;
8580
4c2f2a79
PA
8581 /* The stop ID. */
8582 ULONGEST stop_id;
c906108c 8583
4c2f2a79 8584 /* The event PTID. */
c906108c 8585
4c2f2a79
PA
8586 ptid_t ptid;
8587
8588 /* If stopp for a thread event, this is the thread that caused the
8589 stop. */
d634cd0b 8590 thread_info_ref thread;
4c2f2a79
PA
8591
8592 /* The inferior that caused the stop. */
8593 int inf_num;
8594};
8595
2d844eaf 8596/* Initializes a new stop context. If stopped for a thread event, this
4c2f2a79
PA
8597 takes a strong reference to the thread. */
8598
2d844eaf 8599stop_context::stop_context ()
4c2f2a79 8600{
2d844eaf
TT
8601 stop_id = get_stop_id ();
8602 ptid = inferior_ptid;
8603 inf_num = current_inferior ()->num;
4c2f2a79 8604
d7e15655 8605 if (inferior_ptid != null_ptid)
4c2f2a79
PA
8606 {
8607 /* Take a strong reference so that the thread can't be deleted
8608 yet. */
d634cd0b 8609 thread = thread_info_ref::new_reference (inferior_thread ());
4c2f2a79 8610 }
4c2f2a79
PA
8611}
8612
8613/* Return true if the current context no longer matches the saved stop
8614 context. */
8615
2d844eaf
TT
8616bool
8617stop_context::changed () const
8618{
8619 if (ptid != inferior_ptid)
8620 return true;
8621 if (inf_num != current_inferior ()->num)
8622 return true;
8623 if (thread != NULL && thread->state != THREAD_STOPPED)
8624 return true;
8625 if (get_stop_id () != stop_id)
8626 return true;
8627 return false;
4c2f2a79
PA
8628}
8629
8630/* See infrun.h. */
8631
8632int
96baa820 8633normal_stop (void)
c906108c 8634{
73b65bb0 8635 struct target_waitstatus last;
73b65bb0 8636
5b6d1e4f 8637 get_last_target_status (nullptr, nullptr, &last);
73b65bb0 8638
4c2f2a79
PA
8639 new_stop_id ();
8640
29f49a6a
PA
8641 /* If an exception is thrown from this point on, make sure to
8642 propagate GDB's knowledge of the executing state to the
8643 frontend/user running state. A QUIT is an easy exception to see
8644 here, so do this before any filtered output. */
731f534f 8645
5b6d1e4f 8646 ptid_t finish_ptid = null_ptid;
731f534f 8647
c35b1492 8648 if (!non_stop)
5b6d1e4f 8649 finish_ptid = minus_one_ptid;
e1316e60
PA
8650 else if (last.kind == TARGET_WAITKIND_SIGNALLED
8651 || last.kind == TARGET_WAITKIND_EXITED)
8652 {
8653 /* On some targets, we may still have live threads in the
8654 inferior when we get a process exit event. E.g., for
8655 "checkpoint", when the current checkpoint/fork exits,
8656 linux-fork.c automatically switches to another fork from
8657 within target_mourn_inferior. */
731f534f 8658 if (inferior_ptid != null_ptid)
5b6d1e4f 8659 finish_ptid = ptid_t (inferior_ptid.pid ());
e1316e60
PA
8660 }
8661 else if (last.kind != TARGET_WAITKIND_NO_RESUMED)
5b6d1e4f
PA
8662 finish_ptid = inferior_ptid;
8663
8664 gdb::optional<scoped_finish_thread_state> maybe_finish_thread_state;
8665 if (finish_ptid != null_ptid)
8666 {
8667 maybe_finish_thread_state.emplace
8668 (user_visible_resume_target (finish_ptid), finish_ptid);
8669 }
29f49a6a 8670
b57bacec
PA
8671 /* As we're presenting a stop, and potentially removing breakpoints,
8672 update the thread list so we can tell whether there are threads
8673 running on the target. With target remote, for example, we can
8674 only learn about new threads when we explicitly update the thread
8675 list. Do this before notifying the interpreters about signal
8676 stops, end of stepping ranges, etc., so that the "new thread"
8677 output is emitted before e.g., "Program received signal FOO",
8678 instead of after. */
8679 update_thread_list ();
8680
8681 if (last.kind == TARGET_WAITKIND_STOPPED && stopped_by_random_signal)
76727919 8682 gdb::observers::signal_received.notify (inferior_thread ()->suspend.stop_signal);
b57bacec 8683
c906108c
SS
8684 /* As with the notification of thread events, we want to delay
8685 notifying the user that we've switched thread context until
8686 the inferior actually stops.
8687
73b65bb0
DJ
8688 There's no point in saying anything if the inferior has exited.
8689 Note that SIGNALLED here means "exited with a signal", not
b65dc60b
PA
8690 "received a signal".
8691
8692 Also skip saying anything in non-stop mode. In that mode, as we
8693 don't want GDB to switch threads behind the user's back, to avoid
8694 races where the user is typing a command to apply to thread x,
8695 but GDB switches to thread y before the user finishes entering
8696 the command, fetch_inferior_event installs a cleanup to restore
8697 the current thread back to the thread the user had selected right
8698 after this event is handled, so we're not really switching, only
8699 informing of a stop. */
4f8d22e3 8700 if (!non_stop
731f534f 8701 && previous_inferior_ptid != inferior_ptid
55f6301a 8702 && target_has_execution ()
73b65bb0 8703 && last.kind != TARGET_WAITKIND_SIGNALLED
0e5bf2a8
PA
8704 && last.kind != TARGET_WAITKIND_EXITED
8705 && last.kind != TARGET_WAITKIND_NO_RESUMED)
c906108c 8706 {
0e454242 8707 SWITCH_THRU_ALL_UIS ()
3b12939d 8708 {
223ffa71 8709 target_terminal::ours_for_output ();
3b12939d 8710 printf_filtered (_("[Switching to %s]\n"),
a068643d 8711 target_pid_to_str (inferior_ptid).c_str ());
3b12939d
PA
8712 annotate_thread_changed ();
8713 }
39f77062 8714 previous_inferior_ptid = inferior_ptid;
c906108c 8715 }
c906108c 8716
0e5bf2a8
PA
8717 if (last.kind == TARGET_WAITKIND_NO_RESUMED)
8718 {
0e454242 8719 SWITCH_THRU_ALL_UIS ()
3b12939d
PA
8720 if (current_ui->prompt_state == PROMPT_BLOCKED)
8721 {
223ffa71 8722 target_terminal::ours_for_output ();
3b12939d
PA
8723 printf_filtered (_("No unwaited-for children left.\n"));
8724 }
0e5bf2a8
PA
8725 }
8726
b57bacec 8727 /* Note: this depends on the update_thread_list call above. */
388a7084 8728 maybe_remove_breakpoints ();
c906108c 8729
c906108c
SS
8730 /* If an auto-display called a function and that got a signal,
8731 delete that auto-display to avoid an infinite recursion. */
8732
8733 if (stopped_by_random_signal)
8734 disable_current_display ();
8735
0e454242 8736 SWITCH_THRU_ALL_UIS ()
3b12939d
PA
8737 {
8738 async_enable_stdin ();
8739 }
c906108c 8740
388a7084 8741 /* Let the user/frontend see the threads as stopped. */
731f534f 8742 maybe_finish_thread_state.reset ();
388a7084
PA
8743
8744 /* Select innermost stack frame - i.e., current frame is frame 0,
8745 and current location is based on that. Handle the case where the
8746 dummy call is returning after being stopped. E.g. the dummy call
8747 previously hit a breakpoint. (If the dummy call returns
8748 normally, we won't reach here.) Do this before the stop hook is
8749 run, so that it doesn't get to see the temporary dummy frame,
8750 which is not where we'll present the stop. */
8751 if (has_stack_frames ())
8752 {
8753 if (stop_stack_dummy == STOP_STACK_DUMMY)
8754 {
8755 /* Pop the empty frame that contains the stack dummy. This
8756 also restores inferior state prior to the call (struct
8757 infcall_suspend_state). */
8758 struct frame_info *frame = get_current_frame ();
8759
8760 gdb_assert (get_frame_type (frame) == DUMMY_FRAME);
8761 frame_pop (frame);
8762 /* frame_pop calls reinit_frame_cache as the last thing it
8763 does which means there's now no selected frame. */
8764 }
8765
8766 select_frame (get_current_frame ());
8767
8768 /* Set the current source location. */
8769 set_current_sal_from_frame (get_current_frame ());
8770 }
dd7e2d2b
PA
8771
8772 /* Look up the hook_stop and run it (CLI internally handles problem
8773 of stop_command's pre-hook not existing). */
4c2f2a79
PA
8774 if (stop_command != NULL)
8775 {
2d844eaf 8776 stop_context saved_context;
4c2f2a79 8777
a70b8144 8778 try
bf469271
PA
8779 {
8780 execute_cmd_pre_hook (stop_command);
8781 }
230d2906 8782 catch (const gdb_exception &ex)
bf469271
PA
8783 {
8784 exception_fprintf (gdb_stderr, ex,
8785 "Error while running hook_stop:\n");
8786 }
4c2f2a79
PA
8787
8788 /* If the stop hook resumes the target, then there's no point in
8789 trying to notify about the previous stop; its context is
8790 gone. Likewise if the command switches thread or inferior --
8791 the observers would print a stop for the wrong
8792 thread/inferior. */
2d844eaf
TT
8793 if (saved_context.changed ())
8794 return 1;
4c2f2a79 8795 }
dd7e2d2b 8796
388a7084
PA
8797 /* Notify observers about the stop. This is where the interpreters
8798 print the stop event. */
d7e15655 8799 if (inferior_ptid != null_ptid)
76727919 8800 gdb::observers::normal_stop.notify (inferior_thread ()->control.stop_bpstat,
24a7f1b5 8801 stop_print_frame);
388a7084 8802 else
76727919 8803 gdb::observers::normal_stop.notify (NULL, stop_print_frame);
347bddb7 8804
243a9253
PA
8805 annotate_stopped ();
8806
55f6301a 8807 if (target_has_execution ())
48844aa6
PA
8808 {
8809 if (last.kind != TARGET_WAITKIND_SIGNALLED
fe726667
PA
8810 && last.kind != TARGET_WAITKIND_EXITED
8811 && last.kind != TARGET_WAITKIND_NO_RESUMED)
48844aa6
PA
8812 /* Delete the breakpoint we stopped at, if it wants to be deleted.
8813 Delete any breakpoint that is to be deleted at the next stop. */
16c381f0 8814 breakpoint_auto_delete (inferior_thread ()->control.stop_bpstat);
94cc34af 8815 }
6c95b8df
PA
8816
8817 /* Try to get rid of automatically added inferiors that are no
8818 longer needed. Keeping those around slows down things linearly.
8819 Note that this never removes the current inferior. */
8820 prune_inferiors ();
4c2f2a79
PA
8821
8822 return 0;
c906108c 8823}
c906108c 8824\f
c5aa993b 8825int
96baa820 8826signal_stop_state (int signo)
c906108c 8827{
d6b48e9c 8828 return signal_stop[signo];
c906108c
SS
8829}
8830
c5aa993b 8831int
96baa820 8832signal_print_state (int signo)
c906108c
SS
8833{
8834 return signal_print[signo];
8835}
8836
c5aa993b 8837int
96baa820 8838signal_pass_state (int signo)
c906108c
SS
8839{
8840 return signal_program[signo];
8841}
8842
2455069d
UW
8843static void
8844signal_cache_update (int signo)
8845{
8846 if (signo == -1)
8847 {
a493e3e2 8848 for (signo = 0; signo < (int) GDB_SIGNAL_LAST; signo++)
2455069d
UW
8849 signal_cache_update (signo);
8850
8851 return;
8852 }
8853
8854 signal_pass[signo] = (signal_stop[signo] == 0
8855 && signal_print[signo] == 0
ab04a2af
TT
8856 && signal_program[signo] == 1
8857 && signal_catch[signo] == 0);
2455069d
UW
8858}
8859
488f131b 8860int
7bda5e4a 8861signal_stop_update (int signo, int state)
d4f3574e
SS
8862{
8863 int ret = signal_stop[signo];
abbb1732 8864
d4f3574e 8865 signal_stop[signo] = state;
2455069d 8866 signal_cache_update (signo);
d4f3574e
SS
8867 return ret;
8868}
8869
488f131b 8870int
7bda5e4a 8871signal_print_update (int signo, int state)
d4f3574e
SS
8872{
8873 int ret = signal_print[signo];
abbb1732 8874
d4f3574e 8875 signal_print[signo] = state;
2455069d 8876 signal_cache_update (signo);
d4f3574e
SS
8877 return ret;
8878}
8879
488f131b 8880int
7bda5e4a 8881signal_pass_update (int signo, int state)
d4f3574e
SS
8882{
8883 int ret = signal_program[signo];
abbb1732 8884
d4f3574e 8885 signal_program[signo] = state;
2455069d 8886 signal_cache_update (signo);
d4f3574e
SS
8887 return ret;
8888}
8889
ab04a2af
TT
8890/* Update the global 'signal_catch' from INFO and notify the
8891 target. */
8892
8893void
8894signal_catch_update (const unsigned int *info)
8895{
8896 int i;
8897
8898 for (i = 0; i < GDB_SIGNAL_LAST; ++i)
8899 signal_catch[i] = info[i] > 0;
8900 signal_cache_update (-1);
adc6a863 8901 target_pass_signals (signal_pass);
ab04a2af
TT
8902}
8903
c906108c 8904static void
96baa820 8905sig_print_header (void)
c906108c 8906{
3e43a32a
MS
8907 printf_filtered (_("Signal Stop\tPrint\tPass "
8908 "to program\tDescription\n"));
c906108c
SS
8909}
8910
8911static void
2ea28649 8912sig_print_info (enum gdb_signal oursig)
c906108c 8913{
2ea28649 8914 const char *name = gdb_signal_to_name (oursig);
c906108c 8915 int name_padding = 13 - strlen (name);
96baa820 8916
c906108c
SS
8917 if (name_padding <= 0)
8918 name_padding = 0;
8919
8920 printf_filtered ("%s", name);
488f131b 8921 printf_filtered ("%*.*s ", name_padding, name_padding, " ");
c906108c
SS
8922 printf_filtered ("%s\t", signal_stop[oursig] ? "Yes" : "No");
8923 printf_filtered ("%s\t", signal_print[oursig] ? "Yes" : "No");
8924 printf_filtered ("%s\t\t", signal_program[oursig] ? "Yes" : "No");
2ea28649 8925 printf_filtered ("%s\n", gdb_signal_to_string (oursig));
c906108c
SS
8926}
8927
8928/* Specify how various signals in the inferior should be handled. */
8929
8930static void
0b39b52e 8931handle_command (const char *args, int from_tty)
c906108c 8932{
c906108c 8933 int digits, wordlen;
b926417a 8934 int sigfirst, siglast;
2ea28649 8935 enum gdb_signal oursig;
c906108c 8936 int allsigs;
c906108c
SS
8937
8938 if (args == NULL)
8939 {
e2e0b3e5 8940 error_no_arg (_("signal to handle"));
c906108c
SS
8941 }
8942
1777feb0 8943 /* Allocate and zero an array of flags for which signals to handle. */
c906108c 8944
adc6a863
PA
8945 const size_t nsigs = GDB_SIGNAL_LAST;
8946 unsigned char sigs[nsigs] {};
c906108c 8947
1777feb0 8948 /* Break the command line up into args. */
c906108c 8949
773a1edc 8950 gdb_argv built_argv (args);
c906108c
SS
8951
8952 /* Walk through the args, looking for signal oursigs, signal names, and
8953 actions. Signal numbers and signal names may be interspersed with
8954 actions, with the actions being performed for all signals cumulatively
1777feb0 8955 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
c906108c 8956
773a1edc 8957 for (char *arg : built_argv)
c906108c 8958 {
773a1edc
TT
8959 wordlen = strlen (arg);
8960 for (digits = 0; isdigit (arg[digits]); digits++)
c906108c
SS
8961 {;
8962 }
8963 allsigs = 0;
8964 sigfirst = siglast = -1;
8965
773a1edc 8966 if (wordlen >= 1 && !strncmp (arg, "all", wordlen))
c906108c
SS
8967 {
8968 /* Apply action to all signals except those used by the
1777feb0 8969 debugger. Silently skip those. */
c906108c
SS
8970 allsigs = 1;
8971 sigfirst = 0;
8972 siglast = nsigs - 1;
8973 }
773a1edc 8974 else if (wordlen >= 1 && !strncmp (arg, "stop", wordlen))
c906108c
SS
8975 {
8976 SET_SIGS (nsigs, sigs, signal_stop);
8977 SET_SIGS (nsigs, sigs, signal_print);
8978 }
773a1edc 8979 else if (wordlen >= 1 && !strncmp (arg, "ignore", wordlen))
c906108c
SS
8980 {
8981 UNSET_SIGS (nsigs, sigs, signal_program);
8982 }
773a1edc 8983 else if (wordlen >= 2 && !strncmp (arg, "print", wordlen))
c906108c
SS
8984 {
8985 SET_SIGS (nsigs, sigs, signal_print);
8986 }
773a1edc 8987 else if (wordlen >= 2 && !strncmp (arg, "pass", wordlen))
c906108c
SS
8988 {
8989 SET_SIGS (nsigs, sigs, signal_program);
8990 }
773a1edc 8991 else if (wordlen >= 3 && !strncmp (arg, "nostop", wordlen))
c906108c
SS
8992 {
8993 UNSET_SIGS (nsigs, sigs, signal_stop);
8994 }
773a1edc 8995 else if (wordlen >= 3 && !strncmp (arg, "noignore", wordlen))
c906108c
SS
8996 {
8997 SET_SIGS (nsigs, sigs, signal_program);
8998 }
773a1edc 8999 else if (wordlen >= 4 && !strncmp (arg, "noprint", wordlen))
c906108c
SS
9000 {
9001 UNSET_SIGS (nsigs, sigs, signal_print);
9002 UNSET_SIGS (nsigs, sigs, signal_stop);
9003 }
773a1edc 9004 else if (wordlen >= 4 && !strncmp (arg, "nopass", wordlen))
c906108c
SS
9005 {
9006 UNSET_SIGS (nsigs, sigs, signal_program);
9007 }
9008 else if (digits > 0)
9009 {
9010 /* It is numeric. The numeric signal refers to our own
9011 internal signal numbering from target.h, not to host/target
9012 signal number. This is a feature; users really should be
9013 using symbolic names anyway, and the common ones like
9014 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
9015
9016 sigfirst = siglast = (int)
773a1edc
TT
9017 gdb_signal_from_command (atoi (arg));
9018 if (arg[digits] == '-')
c906108c
SS
9019 {
9020 siglast = (int)
773a1edc 9021 gdb_signal_from_command (atoi (arg + digits + 1));
c906108c
SS
9022 }
9023 if (sigfirst > siglast)
9024 {
1777feb0 9025 /* Bet he didn't figure we'd think of this case... */
b926417a 9026 std::swap (sigfirst, siglast);
c906108c
SS
9027 }
9028 }
9029 else
9030 {
773a1edc 9031 oursig = gdb_signal_from_name (arg);
a493e3e2 9032 if (oursig != GDB_SIGNAL_UNKNOWN)
c906108c
SS
9033 {
9034 sigfirst = siglast = (int) oursig;
9035 }
9036 else
9037 {
9038 /* Not a number and not a recognized flag word => complain. */
773a1edc 9039 error (_("Unrecognized or ambiguous flag word: \"%s\"."), arg);
c906108c
SS
9040 }
9041 }
9042
9043 /* If any signal numbers or symbol names were found, set flags for
dda83cd7 9044 which signals to apply actions to. */
c906108c 9045
b926417a 9046 for (int signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
c906108c 9047 {
2ea28649 9048 switch ((enum gdb_signal) signum)
c906108c 9049 {
a493e3e2
PA
9050 case GDB_SIGNAL_TRAP:
9051 case GDB_SIGNAL_INT:
c906108c
SS
9052 if (!allsigs && !sigs[signum])
9053 {
9e2f0ad4 9054 if (query (_("%s is used by the debugger.\n\
3e43a32a 9055Are you sure you want to change it? "),
2ea28649 9056 gdb_signal_to_name ((enum gdb_signal) signum)))
c906108c
SS
9057 {
9058 sigs[signum] = 1;
9059 }
9060 else
c119e040 9061 printf_unfiltered (_("Not confirmed, unchanged.\n"));
c906108c
SS
9062 }
9063 break;
a493e3e2
PA
9064 case GDB_SIGNAL_0:
9065 case GDB_SIGNAL_DEFAULT:
9066 case GDB_SIGNAL_UNKNOWN:
c906108c
SS
9067 /* Make sure that "all" doesn't print these. */
9068 break;
9069 default:
9070 sigs[signum] = 1;
9071 break;
9072 }
9073 }
c906108c
SS
9074 }
9075
b926417a 9076 for (int signum = 0; signum < nsigs; signum++)
3a031f65
PA
9077 if (sigs[signum])
9078 {
2455069d 9079 signal_cache_update (-1);
adc6a863
PA
9080 target_pass_signals (signal_pass);
9081 target_program_signals (signal_program);
c906108c 9082
3a031f65
PA
9083 if (from_tty)
9084 {
9085 /* Show the results. */
9086 sig_print_header ();
9087 for (; signum < nsigs; signum++)
9088 if (sigs[signum])
aead7601 9089 sig_print_info ((enum gdb_signal) signum);
3a031f65
PA
9090 }
9091
9092 break;
9093 }
c906108c
SS
9094}
9095
de0bea00
MF
9096/* Complete the "handle" command. */
9097
eb3ff9a5 9098static void
de0bea00 9099handle_completer (struct cmd_list_element *ignore,
eb3ff9a5 9100 completion_tracker &tracker,
6f937416 9101 const char *text, const char *word)
de0bea00 9102{
de0bea00
MF
9103 static const char * const keywords[] =
9104 {
9105 "all",
9106 "stop",
9107 "ignore",
9108 "print",
9109 "pass",
9110 "nostop",
9111 "noignore",
9112 "noprint",
9113 "nopass",
9114 NULL,
9115 };
9116
eb3ff9a5
PA
9117 signal_completer (ignore, tracker, text, word);
9118 complete_on_enum (tracker, keywords, word, word);
de0bea00
MF
9119}
9120
2ea28649
PA
9121enum gdb_signal
9122gdb_signal_from_command (int num)
ed01b82c
PA
9123{
9124 if (num >= 1 && num <= 15)
2ea28649 9125 return (enum gdb_signal) num;
ed01b82c
PA
9126 error (_("Only signals 1-15 are valid as numeric signals.\n\
9127Use \"info signals\" for a list of symbolic signals."));
9128}
9129
c906108c
SS
9130/* Print current contents of the tables set by the handle command.
9131 It is possible we should just be printing signals actually used
9132 by the current target (but for things to work right when switching
9133 targets, all signals should be in the signal tables). */
9134
9135static void
1d12d88f 9136info_signals_command (const char *signum_exp, int from_tty)
c906108c 9137{
2ea28649 9138 enum gdb_signal oursig;
abbb1732 9139
c906108c
SS
9140 sig_print_header ();
9141
9142 if (signum_exp)
9143 {
9144 /* First see if this is a symbol name. */
2ea28649 9145 oursig = gdb_signal_from_name (signum_exp);
a493e3e2 9146 if (oursig == GDB_SIGNAL_UNKNOWN)
c906108c
SS
9147 {
9148 /* No, try numeric. */
9149 oursig =
2ea28649 9150 gdb_signal_from_command (parse_and_eval_long (signum_exp));
c906108c
SS
9151 }
9152 sig_print_info (oursig);
9153 return;
9154 }
9155
9156 printf_filtered ("\n");
9157 /* These ugly casts brought to you by the native VAX compiler. */
a493e3e2
PA
9158 for (oursig = GDB_SIGNAL_FIRST;
9159 (int) oursig < (int) GDB_SIGNAL_LAST;
2ea28649 9160 oursig = (enum gdb_signal) ((int) oursig + 1))
c906108c
SS
9161 {
9162 QUIT;
9163
a493e3e2
PA
9164 if (oursig != GDB_SIGNAL_UNKNOWN
9165 && oursig != GDB_SIGNAL_DEFAULT && oursig != GDB_SIGNAL_0)
c906108c
SS
9166 sig_print_info (oursig);
9167 }
9168
3e43a32a
MS
9169 printf_filtered (_("\nUse the \"handle\" command "
9170 "to change these tables.\n"));
c906108c 9171}
4aa995e1
PA
9172
9173/* The $_siginfo convenience variable is a bit special. We don't know
9174 for sure the type of the value until we actually have a chance to
7a9dd1b2 9175 fetch the data. The type can change depending on gdbarch, so it is
4aa995e1
PA
9176 also dependent on which thread you have selected.
9177
9178 1. making $_siginfo be an internalvar that creates a new value on
9179 access.
9180
9181 2. making the value of $_siginfo be an lval_computed value. */
9182
9183/* This function implements the lval_computed support for reading a
9184 $_siginfo value. */
9185
9186static void
9187siginfo_value_read (struct value *v)
9188{
9189 LONGEST transferred;
9190
a911d87a
PA
9191 /* If we can access registers, so can we access $_siginfo. Likewise
9192 vice versa. */
9193 validate_registers_access ();
c709acd1 9194
4aa995e1 9195 transferred =
328d42d8
SM
9196 target_read (current_inferior ()->top_target (),
9197 TARGET_OBJECT_SIGNAL_INFO,
4aa995e1
PA
9198 NULL,
9199 value_contents_all_raw (v),
9200 value_offset (v),
9201 TYPE_LENGTH (value_type (v)));
9202
9203 if (transferred != TYPE_LENGTH (value_type (v)))
9204 error (_("Unable to read siginfo"));
9205}
9206
9207/* This function implements the lval_computed support for writing a
9208 $_siginfo value. */
9209
9210static void
9211siginfo_value_write (struct value *v, struct value *fromval)
9212{
9213 LONGEST transferred;
9214
a911d87a
PA
9215 /* If we can access registers, so can we access $_siginfo. Likewise
9216 vice versa. */
9217 validate_registers_access ();
c709acd1 9218
328d42d8 9219 transferred = target_write (current_inferior ()->top_target (),
4aa995e1
PA
9220 TARGET_OBJECT_SIGNAL_INFO,
9221 NULL,
9222 value_contents_all_raw (fromval),
9223 value_offset (v),
9224 TYPE_LENGTH (value_type (fromval)));
9225
9226 if (transferred != TYPE_LENGTH (value_type (fromval)))
9227 error (_("Unable to write siginfo"));
9228}
9229
c8f2448a 9230static const struct lval_funcs siginfo_value_funcs =
4aa995e1
PA
9231 {
9232 siginfo_value_read,
9233 siginfo_value_write
9234 };
9235
9236/* Return a new value with the correct type for the siginfo object of
78267919
UW
9237 the current thread using architecture GDBARCH. Return a void value
9238 if there's no object available. */
4aa995e1 9239
2c0b251b 9240static struct value *
22d2b532
SDJ
9241siginfo_make_value (struct gdbarch *gdbarch, struct internalvar *var,
9242 void *ignore)
4aa995e1 9243{
841de120 9244 if (target_has_stack ()
d7e15655 9245 && inferior_ptid != null_ptid
78267919 9246 && gdbarch_get_siginfo_type_p (gdbarch))
4aa995e1 9247 {
78267919 9248 struct type *type = gdbarch_get_siginfo_type (gdbarch);
abbb1732 9249
78267919 9250 return allocate_computed_value (type, &siginfo_value_funcs, NULL);
4aa995e1
PA
9251 }
9252
78267919 9253 return allocate_value (builtin_type (gdbarch)->builtin_void);
4aa995e1
PA
9254}
9255
c906108c 9256\f
16c381f0
JK
9257/* infcall_suspend_state contains state about the program itself like its
9258 registers and any signal it received when it last stopped.
9259 This state must be restored regardless of how the inferior function call
9260 ends (either successfully, or after it hits a breakpoint or signal)
9261 if the program is to properly continue where it left off. */
9262
6bf78e29 9263class infcall_suspend_state
7a292a7a 9264{
6bf78e29
AB
9265public:
9266 /* Capture state from GDBARCH, TP, and REGCACHE that must be restored
9267 once the inferior function call has finished. */
9268 infcall_suspend_state (struct gdbarch *gdbarch,
dda83cd7
SM
9269 const struct thread_info *tp,
9270 struct regcache *regcache)
6bf78e29
AB
9271 : m_thread_suspend (tp->suspend),
9272 m_registers (new readonly_detached_regcache (*regcache))
9273 {
9274 gdb::unique_xmalloc_ptr<gdb_byte> siginfo_data;
9275
9276 if (gdbarch_get_siginfo_type_p (gdbarch))
9277 {
dda83cd7
SM
9278 struct type *type = gdbarch_get_siginfo_type (gdbarch);
9279 size_t len = TYPE_LENGTH (type);
6bf78e29 9280
dda83cd7 9281 siginfo_data.reset ((gdb_byte *) xmalloc (len));
6bf78e29 9282
328d42d8
SM
9283 if (target_read (current_inferior ()->top_target (),
9284 TARGET_OBJECT_SIGNAL_INFO, NULL,
dda83cd7
SM
9285 siginfo_data.get (), 0, len) != len)
9286 {
9287 /* Errors ignored. */
9288 siginfo_data.reset (nullptr);
9289 }
6bf78e29
AB
9290 }
9291
9292 if (siginfo_data)
9293 {
dda83cd7
SM
9294 m_siginfo_gdbarch = gdbarch;
9295 m_siginfo_data = std::move (siginfo_data);
6bf78e29
AB
9296 }
9297 }
9298
9299 /* Return a pointer to the stored register state. */
16c381f0 9300
6bf78e29
AB
9301 readonly_detached_regcache *registers () const
9302 {
9303 return m_registers.get ();
9304 }
9305
9306 /* Restores the stored state into GDBARCH, TP, and REGCACHE. */
9307
9308 void restore (struct gdbarch *gdbarch,
dda83cd7
SM
9309 struct thread_info *tp,
9310 struct regcache *regcache) const
6bf78e29
AB
9311 {
9312 tp->suspend = m_thread_suspend;
9313
9314 if (m_siginfo_gdbarch == gdbarch)
9315 {
dda83cd7 9316 struct type *type = gdbarch_get_siginfo_type (gdbarch);
6bf78e29 9317
dda83cd7 9318 /* Errors ignored. */
328d42d8
SM
9319 target_write (current_inferior ()->top_target (),
9320 TARGET_OBJECT_SIGNAL_INFO, NULL,
dda83cd7 9321 m_siginfo_data.get (), 0, TYPE_LENGTH (type));
6bf78e29
AB
9322 }
9323
9324 /* The inferior can be gone if the user types "print exit(0)"
9325 (and perhaps other times). */
55f6301a 9326 if (target_has_execution ())
6bf78e29
AB
9327 /* NB: The register write goes through to the target. */
9328 regcache->restore (registers ());
9329 }
9330
9331private:
9332 /* How the current thread stopped before the inferior function call was
9333 executed. */
9334 struct thread_suspend_state m_thread_suspend;
9335
9336 /* The registers before the inferior function call was executed. */
9337 std::unique_ptr<readonly_detached_regcache> m_registers;
1736ad11 9338
35515841 9339 /* Format of SIGINFO_DATA or NULL if it is not present. */
6bf78e29 9340 struct gdbarch *m_siginfo_gdbarch = nullptr;
1736ad11
JK
9341
9342 /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
9343 TYPE_LENGTH (gdbarch_get_siginfo_type ()). For different gdbarch the
9344 content would be invalid. */
6bf78e29 9345 gdb::unique_xmalloc_ptr<gdb_byte> m_siginfo_data;
b89667eb
DE
9346};
9347
cb524840
TT
9348infcall_suspend_state_up
9349save_infcall_suspend_state ()
b89667eb 9350{
b89667eb 9351 struct thread_info *tp = inferior_thread ();
1736ad11 9352 struct regcache *regcache = get_current_regcache ();
ac7936df 9353 struct gdbarch *gdbarch = regcache->arch ();
1736ad11 9354
6bf78e29
AB
9355 infcall_suspend_state_up inf_state
9356 (new struct infcall_suspend_state (gdbarch, tp, regcache));
1736ad11 9357
6bf78e29
AB
9358 /* Having saved the current state, adjust the thread state, discarding
9359 any stop signal information. The stop signal is not useful when
9360 starting an inferior function call, and run_inferior_call will not use
9361 the signal due to its `proceed' call with GDB_SIGNAL_0. */
a493e3e2 9362 tp->suspend.stop_signal = GDB_SIGNAL_0;
35515841 9363
b89667eb
DE
9364 return inf_state;
9365}
9366
9367/* Restore inferior session state to INF_STATE. */
9368
9369void
16c381f0 9370restore_infcall_suspend_state (struct infcall_suspend_state *inf_state)
b89667eb
DE
9371{
9372 struct thread_info *tp = inferior_thread ();
1736ad11 9373 struct regcache *regcache = get_current_regcache ();
ac7936df 9374 struct gdbarch *gdbarch = regcache->arch ();
b89667eb 9375
6bf78e29 9376 inf_state->restore (gdbarch, tp, regcache);
16c381f0 9377 discard_infcall_suspend_state (inf_state);
b89667eb
DE
9378}
9379
b89667eb 9380void
16c381f0 9381discard_infcall_suspend_state (struct infcall_suspend_state *inf_state)
b89667eb 9382{
dd848631 9383 delete inf_state;
b89667eb
DE
9384}
9385
daf6667d 9386readonly_detached_regcache *
16c381f0 9387get_infcall_suspend_state_regcache (struct infcall_suspend_state *inf_state)
b89667eb 9388{
6bf78e29 9389 return inf_state->registers ();
b89667eb
DE
9390}
9391
16c381f0
JK
9392/* infcall_control_state contains state regarding gdb's control of the
9393 inferior itself like stepping control. It also contains session state like
9394 the user's currently selected frame. */
b89667eb 9395
16c381f0 9396struct infcall_control_state
b89667eb 9397{
16c381f0
JK
9398 struct thread_control_state thread_control;
9399 struct inferior_control_state inferior_control;
d82142e2
JK
9400
9401 /* Other fields: */
ee841dd8
TT
9402 enum stop_stack_kind stop_stack_dummy = STOP_NONE;
9403 int stopped_by_random_signal = 0;
7a292a7a 9404
79952e69
PA
9405 /* ID and level of the selected frame when the inferior function
9406 call was made. */
ee841dd8 9407 struct frame_id selected_frame_id {};
79952e69 9408 int selected_frame_level = -1;
7a292a7a
SS
9409};
9410
c906108c 9411/* Save all of the information associated with the inferior<==>gdb
b89667eb 9412 connection. */
c906108c 9413
cb524840
TT
9414infcall_control_state_up
9415save_infcall_control_state ()
c906108c 9416{
cb524840 9417 infcall_control_state_up inf_status (new struct infcall_control_state);
4e1c45ea 9418 struct thread_info *tp = inferior_thread ();
d6b48e9c 9419 struct inferior *inf = current_inferior ();
7a292a7a 9420
16c381f0
JK
9421 inf_status->thread_control = tp->control;
9422 inf_status->inferior_control = inf->control;
d82142e2 9423
8358c15c 9424 tp->control.step_resume_breakpoint = NULL;
5b79abe7 9425 tp->control.exception_resume_breakpoint = NULL;
8358c15c 9426
16c381f0
JK
9427 /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
9428 chain. If caller's caller is walking the chain, they'll be happier if we
9429 hand them back the original chain when restore_infcall_control_state is
9430 called. */
9431 tp->control.stop_bpstat = bpstat_copy (tp->control.stop_bpstat);
d82142e2
JK
9432
9433 /* Other fields: */
9434 inf_status->stop_stack_dummy = stop_stack_dummy;
9435 inf_status->stopped_by_random_signal = stopped_by_random_signal;
c5aa993b 9436
79952e69
PA
9437 save_selected_frame (&inf_status->selected_frame_id,
9438 &inf_status->selected_frame_level);
b89667eb 9439
7a292a7a 9440 return inf_status;
c906108c
SS
9441}
9442
b89667eb
DE
9443/* Restore inferior session state to INF_STATUS. */
9444
c906108c 9445void
16c381f0 9446restore_infcall_control_state (struct infcall_control_state *inf_status)
c906108c 9447{
4e1c45ea 9448 struct thread_info *tp = inferior_thread ();
d6b48e9c 9449 struct inferior *inf = current_inferior ();
4e1c45ea 9450
8358c15c
JK
9451 if (tp->control.step_resume_breakpoint)
9452 tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
9453
5b79abe7
TT
9454 if (tp->control.exception_resume_breakpoint)
9455 tp->control.exception_resume_breakpoint->disposition
9456 = disp_del_at_next_stop;
9457
d82142e2 9458 /* Handle the bpstat_copy of the chain. */
16c381f0 9459 bpstat_clear (&tp->control.stop_bpstat);
d82142e2 9460
16c381f0
JK
9461 tp->control = inf_status->thread_control;
9462 inf->control = inf_status->inferior_control;
d82142e2
JK
9463
9464 /* Other fields: */
9465 stop_stack_dummy = inf_status->stop_stack_dummy;
9466 stopped_by_random_signal = inf_status->stopped_by_random_signal;
c906108c 9467
841de120 9468 if (target_has_stack ())
c906108c 9469 {
79952e69
PA
9470 restore_selected_frame (inf_status->selected_frame_id,
9471 inf_status->selected_frame_level);
c906108c 9472 }
c906108c 9473
ee841dd8 9474 delete inf_status;
7a292a7a 9475}
c906108c
SS
9476
9477void
16c381f0 9478discard_infcall_control_state (struct infcall_control_state *inf_status)
7a292a7a 9479{
8358c15c
JK
9480 if (inf_status->thread_control.step_resume_breakpoint)
9481 inf_status->thread_control.step_resume_breakpoint->disposition
9482 = disp_del_at_next_stop;
9483
5b79abe7
TT
9484 if (inf_status->thread_control.exception_resume_breakpoint)
9485 inf_status->thread_control.exception_resume_breakpoint->disposition
9486 = disp_del_at_next_stop;
9487
1777feb0 9488 /* See save_infcall_control_state for info on stop_bpstat. */
16c381f0 9489 bpstat_clear (&inf_status->thread_control.stop_bpstat);
8358c15c 9490
ee841dd8 9491 delete inf_status;
7a292a7a 9492}
b89667eb 9493\f
7f89fd65 9494/* See infrun.h. */
0c557179
SDJ
9495
9496void
9497clear_exit_convenience_vars (void)
9498{
9499 clear_internalvar (lookup_internalvar ("_exitsignal"));
9500 clear_internalvar (lookup_internalvar ("_exitcode"));
9501}
c5aa993b 9502\f
488f131b 9503
b2175913
MS
9504/* User interface for reverse debugging:
9505 Set exec-direction / show exec-direction commands
9506 (returns error unless target implements to_set_exec_direction method). */
9507
170742de 9508enum exec_direction_kind execution_direction = EXEC_FORWARD;
b2175913
MS
9509static const char exec_forward[] = "forward";
9510static const char exec_reverse[] = "reverse";
9511static const char *exec_direction = exec_forward;
40478521 9512static const char *const exec_direction_names[] = {
b2175913
MS
9513 exec_forward,
9514 exec_reverse,
9515 NULL
9516};
9517
9518static void
eb4c3f4a 9519set_exec_direction_func (const char *args, int from_tty,
b2175913
MS
9520 struct cmd_list_element *cmd)
9521{
05374cfd 9522 if (target_can_execute_reverse ())
b2175913
MS
9523 {
9524 if (!strcmp (exec_direction, exec_forward))
9525 execution_direction = EXEC_FORWARD;
9526 else if (!strcmp (exec_direction, exec_reverse))
9527 execution_direction = EXEC_REVERSE;
9528 }
8bbed405
MS
9529 else
9530 {
9531 exec_direction = exec_forward;
9532 error (_("Target does not support this operation."));
9533 }
b2175913
MS
9534}
9535
9536static void
9537show_exec_direction_func (struct ui_file *out, int from_tty,
9538 struct cmd_list_element *cmd, const char *value)
9539{
9540 switch (execution_direction) {
9541 case EXEC_FORWARD:
9542 fprintf_filtered (out, _("Forward.\n"));
9543 break;
9544 case EXEC_REVERSE:
9545 fprintf_filtered (out, _("Reverse.\n"));
9546 break;
b2175913 9547 default:
d8b34453
PA
9548 internal_error (__FILE__, __LINE__,
9549 _("bogus execution_direction value: %d"),
9550 (int) execution_direction);
b2175913
MS
9551 }
9552}
9553
d4db2f36
PA
9554static void
9555show_schedule_multiple (struct ui_file *file, int from_tty,
9556 struct cmd_list_element *c, const char *value)
9557{
3e43a32a
MS
9558 fprintf_filtered (file, _("Resuming the execution of threads "
9559 "of all processes is %s.\n"), value);
d4db2f36 9560}
ad52ddc6 9561
22d2b532
SDJ
9562/* Implementation of `siginfo' variable. */
9563
9564static const struct internalvar_funcs siginfo_funcs =
9565{
9566 siginfo_make_value,
9567 NULL,
9568 NULL
9569};
9570
372316f1
PA
9571/* Callback for infrun's target events source. This is marked when a
9572 thread has a pending status to process. */
9573
9574static void
9575infrun_async_inferior_event_handler (gdb_client_data data)
9576{
6b36ddeb 9577 clear_async_event_handler (infrun_async_inferior_event_token);
b1a35af2 9578 inferior_event_handler (INF_REG_EVENT);
372316f1
PA
9579}
9580
8087c3fa 9581#if GDB_SELF_TEST
b161a60d
SM
9582namespace selftests
9583{
9584
9585/* Verify that when two threads with the same ptid exist (from two different
9586 targets) and one of them changes ptid, we only update inferior_ptid if
9587 it is appropriate. */
9588
9589static void
9590infrun_thread_ptid_changed ()
9591{
9592 gdbarch *arch = current_inferior ()->gdbarch;
9593
9594 /* The thread which inferior_ptid represents changes ptid. */
9595 {
9596 scoped_restore_current_pspace_and_thread restore;
9597
9598 scoped_mock_context<test_target_ops> target1 (arch);
9599 scoped_mock_context<test_target_ops> target2 (arch);
9600 target2.mock_inferior.next = &target1.mock_inferior;
9601
9602 ptid_t old_ptid (111, 222);
9603 ptid_t new_ptid (111, 333);
9604
9605 target1.mock_inferior.pid = old_ptid.pid ();
9606 target1.mock_thread.ptid = old_ptid;
9607 target2.mock_inferior.pid = old_ptid.pid ();
9608 target2.mock_thread.ptid = old_ptid;
9609
9610 auto restore_inferior_ptid = make_scoped_restore (&inferior_ptid, old_ptid);
9611 set_current_inferior (&target1.mock_inferior);
9612
9613 thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
9614
9615 gdb_assert (inferior_ptid == new_ptid);
9616 }
9617
9618 /* A thread with the same ptid as inferior_ptid, but from another target,
9619 changes ptid. */
9620 {
9621 scoped_restore_current_pspace_and_thread restore;
9622
9623 scoped_mock_context<test_target_ops> target1 (arch);
9624 scoped_mock_context<test_target_ops> target2 (arch);
9625 target2.mock_inferior.next = &target1.mock_inferior;
9626
9627 ptid_t old_ptid (111, 222);
9628 ptid_t new_ptid (111, 333);
9629
9630 target1.mock_inferior.pid = old_ptid.pid ();
9631 target1.mock_thread.ptid = old_ptid;
9632 target2.mock_inferior.pid = old_ptid.pid ();
9633 target2.mock_thread.ptid = old_ptid;
9634
9635 auto restore_inferior_ptid = make_scoped_restore (&inferior_ptid, old_ptid);
9636 set_current_inferior (&target2.mock_inferior);
9637
9638 thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
9639
9640 gdb_assert (inferior_ptid == old_ptid);
9641 }
9642}
9643
9644} /* namespace selftests */
9645
8087c3fa
JB
9646#endif /* GDB_SELF_TEST */
9647
6c265988 9648void _initialize_infrun ();
c906108c 9649void
6c265988 9650_initialize_infrun ()
c906108c 9651{
de0bea00 9652 struct cmd_list_element *c;
c906108c 9653
372316f1
PA
9654 /* Register extra event sources in the event loop. */
9655 infrun_async_inferior_event_token
db20ebdf
SM
9656 = create_async_event_handler (infrun_async_inferior_event_handler, NULL,
9657 "infrun");
372316f1 9658
e0f25bd9
SM
9659 cmd_list_element *info_signals_cmd
9660 = add_info ("signals", info_signals_command, _("\
1bedd215
AC
9661What debugger does when program gets various signals.\n\
9662Specify a signal as argument to print info on that signal only."));
e0f25bd9 9663 add_info_alias ("handle", info_signals_cmd, 0);
c906108c 9664
de0bea00 9665 c = add_com ("handle", class_run, handle_command, _("\
dfbd5e7b 9666Specify how to handle signals.\n\
486c7739 9667Usage: handle SIGNAL [ACTIONS]\n\
c906108c 9668Args are signals and actions to apply to those signals.\n\
dfbd5e7b 9669If no actions are specified, the current settings for the specified signals\n\
486c7739
MF
9670will be displayed instead.\n\
9671\n\
c906108c
SS
9672Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
9673from 1-15 are allowed for compatibility with old versions of GDB.\n\
9674Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
9675The special arg \"all\" is recognized to mean all signals except those\n\
1bedd215 9676used by the debugger, typically SIGTRAP and SIGINT.\n\
486c7739 9677\n\
1bedd215 9678Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
c906108c
SS
9679\"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
9680Stop means reenter debugger if this signal happens (implies print).\n\
9681Print means print a message if this signal happens.\n\
9682Pass means let program see this signal; otherwise program doesn't know.\n\
9683Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
dfbd5e7b
PA
9684Pass and Stop may be combined.\n\
9685\n\
9686Multiple signals may be specified. Signal numbers and signal names\n\
9687may be interspersed with actions, with the actions being performed for\n\
9688all signals cumulatively specified."));
de0bea00 9689 set_cmd_completer (c, handle_completer);
486c7739 9690
c906108c 9691 if (!dbx_commands)
1a966eab
AC
9692 stop_command = add_cmd ("stop", class_obscure,
9693 not_just_help_class_command, _("\
9694There is no `stop' command, but you can set a hook on `stop'.\n\
c906108c 9695This allows you to set a list of commands to be run each time execution\n\
1a966eab 9696of the program stops."), &cmdlist);
c906108c 9697
94ba44a6
SM
9698 add_setshow_boolean_cmd
9699 ("infrun", class_maintenance, &debug_infrun,
9700 _("Set inferior debugging."),
9701 _("Show inferior debugging."),
9702 _("When non-zero, inferior specific debugging is enabled."),
9703 NULL, show_debug_infrun, &setdebuglist, &showdebuglist);
527159b7 9704
ad52ddc6
PA
9705 add_setshow_boolean_cmd ("non-stop", no_class,
9706 &non_stop_1, _("\
9707Set whether gdb controls the inferior in non-stop mode."), _("\
9708Show whether gdb controls the inferior in non-stop mode."), _("\
9709When debugging a multi-threaded program and this setting is\n\
9710off (the default, also called all-stop mode), when one thread stops\n\
9711(for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
9712all other threads in the program while you interact with the thread of\n\
9713interest. When you continue or step a thread, you can allow the other\n\
9714threads to run, or have them remain stopped, but while you inspect any\n\
9715thread's state, all threads stop.\n\
9716\n\
9717In non-stop mode, when one thread stops, other threads can continue\n\
9718to run freely. You'll be able to step each thread independently,\n\
9719leave it stopped or free to run as needed."),
9720 set_non_stop,
9721 show_non_stop,
9722 &setlist,
9723 &showlist);
9724
adc6a863 9725 for (size_t i = 0; i < GDB_SIGNAL_LAST; i++)
c906108c
SS
9726 {
9727 signal_stop[i] = 1;
9728 signal_print[i] = 1;
9729 signal_program[i] = 1;
ab04a2af 9730 signal_catch[i] = 0;
c906108c
SS
9731 }
9732
4d9d9d04
PA
9733 /* Signals caused by debugger's own actions should not be given to
9734 the program afterwards.
9735
9736 Do not deliver GDB_SIGNAL_TRAP by default, except when the user
9737 explicitly specifies that it should be delivered to the target
9738 program. Typically, that would occur when a user is debugging a
9739 target monitor on a simulator: the target monitor sets a
9740 breakpoint; the simulator encounters this breakpoint and halts
9741 the simulation handing control to GDB; GDB, noting that the stop
9742 address doesn't map to any known breakpoint, returns control back
9743 to the simulator; the simulator then delivers the hardware
9744 equivalent of a GDB_SIGNAL_TRAP to the program being
9745 debugged. */
a493e3e2
PA
9746 signal_program[GDB_SIGNAL_TRAP] = 0;
9747 signal_program[GDB_SIGNAL_INT] = 0;
c906108c
SS
9748
9749 /* Signals that are not errors should not normally enter the debugger. */
a493e3e2
PA
9750 signal_stop[GDB_SIGNAL_ALRM] = 0;
9751 signal_print[GDB_SIGNAL_ALRM] = 0;
9752 signal_stop[GDB_SIGNAL_VTALRM] = 0;
9753 signal_print[GDB_SIGNAL_VTALRM] = 0;
9754 signal_stop[GDB_SIGNAL_PROF] = 0;
9755 signal_print[GDB_SIGNAL_PROF] = 0;
9756 signal_stop[GDB_SIGNAL_CHLD] = 0;
9757 signal_print[GDB_SIGNAL_CHLD] = 0;
9758 signal_stop[GDB_SIGNAL_IO] = 0;
9759 signal_print[GDB_SIGNAL_IO] = 0;
9760 signal_stop[GDB_SIGNAL_POLL] = 0;
9761 signal_print[GDB_SIGNAL_POLL] = 0;
9762 signal_stop[GDB_SIGNAL_URG] = 0;
9763 signal_print[GDB_SIGNAL_URG] = 0;
9764 signal_stop[GDB_SIGNAL_WINCH] = 0;
9765 signal_print[GDB_SIGNAL_WINCH] = 0;
9766 signal_stop[GDB_SIGNAL_PRIO] = 0;
9767 signal_print[GDB_SIGNAL_PRIO] = 0;
c906108c 9768
cd0fc7c3
SS
9769 /* These signals are used internally by user-level thread
9770 implementations. (See signal(5) on Solaris.) Like the above
9771 signals, a healthy program receives and handles them as part of
9772 its normal operation. */
a493e3e2
PA
9773 signal_stop[GDB_SIGNAL_LWP] = 0;
9774 signal_print[GDB_SIGNAL_LWP] = 0;
9775 signal_stop[GDB_SIGNAL_WAITING] = 0;
9776 signal_print[GDB_SIGNAL_WAITING] = 0;
9777 signal_stop[GDB_SIGNAL_CANCEL] = 0;
9778 signal_print[GDB_SIGNAL_CANCEL] = 0;
bc7b765a
JB
9779 signal_stop[GDB_SIGNAL_LIBRT] = 0;
9780 signal_print[GDB_SIGNAL_LIBRT] = 0;
cd0fc7c3 9781
2455069d
UW
9782 /* Update cached state. */
9783 signal_cache_update (-1);
9784
85c07804
AC
9785 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support,
9786 &stop_on_solib_events, _("\
9787Set stopping for shared library events."), _("\
9788Show stopping for shared library events."), _("\
c906108c
SS
9789If nonzero, gdb will give control to the user when the dynamic linker\n\
9790notifies gdb of shared library events. The most common event of interest\n\
85c07804 9791to the user would be loading/unloading of a new library."),
f9e14852 9792 set_stop_on_solib_events,
920d2a44 9793 show_stop_on_solib_events,
85c07804 9794 &setlist, &showlist);
c906108c 9795
7ab04401
AC
9796 add_setshow_enum_cmd ("follow-fork-mode", class_run,
9797 follow_fork_mode_kind_names,
9798 &follow_fork_mode_string, _("\
9799Set debugger response to a program call of fork or vfork."), _("\
9800Show debugger response to a program call of fork or vfork."), _("\
c906108c
SS
9801A fork or vfork creates a new process. follow-fork-mode can be:\n\
9802 parent - the original process is debugged after a fork\n\
9803 child - the new process is debugged after a fork\n\
ea1dd7bc 9804The unfollowed process will continue to run.\n\
7ab04401
AC
9805By default, the debugger will follow the parent process."),
9806 NULL,
920d2a44 9807 show_follow_fork_mode_string,
7ab04401
AC
9808 &setlist, &showlist);
9809
6c95b8df
PA
9810 add_setshow_enum_cmd ("follow-exec-mode", class_run,
9811 follow_exec_mode_names,
9812 &follow_exec_mode_string, _("\
9813Set debugger response to a program call of exec."), _("\
9814Show debugger response to a program call of exec."), _("\
9815An exec call replaces the program image of a process.\n\
9816\n\
9817follow-exec-mode can be:\n\
9818\n\
cce7e648 9819 new - the debugger creates a new inferior and rebinds the process\n\
6c95b8df
PA
9820to this new inferior. The program the process was running before\n\
9821the exec call can be restarted afterwards by restarting the original\n\
9822inferior.\n\
9823\n\
9824 same - the debugger keeps the process bound to the same inferior.\n\
9825The new executable image replaces the previous executable loaded in\n\
9826the inferior. Restarting the inferior after the exec call restarts\n\
9827the executable the process was running after the exec call.\n\
9828\n\
9829By default, the debugger will use the same inferior."),
9830 NULL,
9831 show_follow_exec_mode_string,
9832 &setlist, &showlist);
9833
7ab04401
AC
9834 add_setshow_enum_cmd ("scheduler-locking", class_run,
9835 scheduler_enums, &scheduler_mode, _("\
9836Set mode for locking scheduler during execution."), _("\
9837Show mode for locking scheduler during execution."), _("\
f2665db5
MM
9838off == no locking (threads may preempt at any time)\n\
9839on == full locking (no thread except the current thread may run)\n\
dda83cd7 9840 This applies to both normal execution and replay mode.\n\
f2665db5 9841step == scheduler locked during stepping commands (step, next, stepi, nexti).\n\
dda83cd7
SM
9842 In this mode, other threads may run during other commands.\n\
9843 This applies to both normal execution and replay mode.\n\
f2665db5 9844replay == scheduler locked in replay mode and unlocked during normal execution."),
7ab04401 9845 set_schedlock_func, /* traps on target vector */
920d2a44 9846 show_scheduler_mode,
7ab04401 9847 &setlist, &showlist);
5fbbeb29 9848
d4db2f36
PA
9849 add_setshow_boolean_cmd ("schedule-multiple", class_run, &sched_multi, _("\
9850Set mode for resuming threads of all processes."), _("\
9851Show mode for resuming threads of all processes."), _("\
9852When on, execution commands (such as 'continue' or 'next') resume all\n\
9853threads of all processes. When off (which is the default), execution\n\
9854commands only resume the threads of the current process. The set of\n\
9855threads that are resumed is further refined by the scheduler-locking\n\
9856mode (see help set scheduler-locking)."),
9857 NULL,
9858 show_schedule_multiple,
9859 &setlist, &showlist);
9860
5bf193a2
AC
9861 add_setshow_boolean_cmd ("step-mode", class_run, &step_stop_if_no_debug, _("\
9862Set mode of the step operation."), _("\
9863Show mode of the step operation."), _("\
9864When set, doing a step over a function without debug line information\n\
9865will stop at the first instruction of that function. Otherwise, the\n\
9866function is skipped and the step command stops at a different source line."),
9867 NULL,
920d2a44 9868 show_step_stop_if_no_debug,
5bf193a2 9869 &setlist, &showlist);
ca6724c1 9870
72d0e2c5
YQ
9871 add_setshow_auto_boolean_cmd ("displaced-stepping", class_run,
9872 &can_use_displaced_stepping, _("\
237fc4c9
PA
9873Set debugger's willingness to use displaced stepping."), _("\
9874Show debugger's willingness to use displaced stepping."), _("\
fff08868
HZ
9875If on, gdb will use displaced stepping to step over breakpoints if it is\n\
9876supported by the target architecture. If off, gdb will not use displaced\n\
9877stepping to step over breakpoints, even if such is supported by the target\n\
9878architecture. If auto (which is the default), gdb will use displaced stepping\n\
9879if the target architecture supports it and non-stop mode is active, but will not\n\
9880use it in all-stop mode (see help set non-stop)."),
72d0e2c5
YQ
9881 NULL,
9882 show_can_use_displaced_stepping,
9883 &setlist, &showlist);
237fc4c9 9884
b2175913
MS
9885 add_setshow_enum_cmd ("exec-direction", class_run, exec_direction_names,
9886 &exec_direction, _("Set direction of execution.\n\
9887Options are 'forward' or 'reverse'."),
9888 _("Show direction of execution (forward/reverse)."),
9889 _("Tells gdb whether to execute forward or backward."),
9890 set_exec_direction_func, show_exec_direction_func,
9891 &setlist, &showlist);
9892
6c95b8df
PA
9893 /* Set/show detach-on-fork: user-settable mode. */
9894
9895 add_setshow_boolean_cmd ("detach-on-fork", class_run, &detach_fork, _("\
9896Set whether gdb will detach the child of a fork."), _("\
9897Show whether gdb will detach the child of a fork."), _("\
9898Tells gdb whether to detach the child of a fork."),
9899 NULL, NULL, &setlist, &showlist);
9900
03583c20
UW
9901 /* Set/show disable address space randomization mode. */
9902
9903 add_setshow_boolean_cmd ("disable-randomization", class_support,
9904 &disable_randomization, _("\
9905Set disabling of debuggee's virtual address space randomization."), _("\
9906Show disabling of debuggee's virtual address space randomization."), _("\
9907When this mode is on (which is the default), randomization of the virtual\n\
9908address space is disabled. Standalone programs run with the randomization\n\
9909enabled by default on some platforms."),
9910 &set_disable_randomization,
9911 &show_disable_randomization,
9912 &setlist, &showlist);
9913
ca6724c1 9914 /* ptid initializations */
ca6724c1
KB
9915 inferior_ptid = null_ptid;
9916 target_last_wait_ptid = minus_one_ptid;
5231c1fd 9917
c90e7d63
SM
9918 gdb::observers::thread_ptid_changed.attach (infrun_thread_ptid_changed,
9919 "infrun");
9920 gdb::observers::thread_stop_requested.attach (infrun_thread_stop_requested,
9921 "infrun");
9922 gdb::observers::thread_exit.attach (infrun_thread_thread_exit, "infrun");
9923 gdb::observers::inferior_exit.attach (infrun_inferior_exit, "infrun");
9924 gdb::observers::inferior_execd.attach (infrun_inferior_execd, "infrun");
4aa995e1
PA
9925
9926 /* Explicitly create without lookup, since that tries to create a
9927 value with a void typed value, and when we get here, gdbarch
9928 isn't initialized yet. At this point, we're quite sure there
9929 isn't another convenience variable of the same name. */
22d2b532 9930 create_internalvar_type_lazy ("_siginfo", &siginfo_funcs, NULL);
d914c394
SS
9931
9932 add_setshow_boolean_cmd ("observer", no_class,
9933 &observer_mode_1, _("\
9934Set whether gdb controls the inferior in observer mode."), _("\
9935Show whether gdb controls the inferior in observer mode."), _("\
9936In observer mode, GDB can get data from the inferior, but not\n\
9937affect its execution. Registers and memory may not be changed,\n\
9938breakpoints may not be set, and the program cannot be interrupted\n\
9939or signalled."),
9940 set_observer_mode,
9941 show_observer_mode,
9942 &setlist,
9943 &showlist);
b161a60d
SM
9944
9945#if GDB_SELF_TEST
9946 selftests::register_test ("infrun_thread_ptid_changed",
9947 selftests::infrun_thread_ptid_changed);
9948#endif
c906108c 9949}
This page took 3.135397 seconds and 4 git commands to generate.