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