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