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