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