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