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