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