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