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