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