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