1 /* Target-struct-independent code to start (run) and stop an inferior
4 Copyright (C) 1986-2023 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
22 #include "displaced-stepping.h"
28 #include "breakpoint.h"
32 #include "target-connection.h"
33 #include "gdbthread.h"
41 #include "observable.h"
46 #include "mi/mi-common.h"
47 #include "event-top.h"
49 #include "record-full.h"
50 #include "inline-frame.h"
52 #include "tracepoint.h"
56 #include "completer.h"
57 #include "target-descriptions.h"
58 #include "target-dcache.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"
81 /* Prototypes for local functions */
83 static void sig_print_info (enum gdb_signal
);
85 static void sig_print_header (void);
87 static void follow_inferior_reset_breakpoints (void);
89 static bool currently_stepping (struct thread_info
*tp
);
91 static void insert_hp_step_resume_breakpoint_at_frame (frame_info_ptr
);
93 static void insert_step_resume_breakpoint_at_caller (frame_info_ptr
);
95 static void insert_longjmp_resume_breakpoint (struct gdbarch
*, CORE_ADDR
);
97 static bool maybe_software_singlestep (struct gdbarch
*gdbarch
);
99 static void resume (gdb_signal sig
);
101 static void wait_for_inferior (inferior
*inf
);
103 static void restart_threads (struct thread_info
*event_thread
,
104 inferior
*inf
= nullptr);
106 static bool start_step_over (void);
108 static bool step_over_info_valid_p (void);
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
;
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;
121 infrun_async (int enable
)
123 if (infrun_is_async
!= enable
)
125 infrun_is_async
= enable
;
127 infrun_debug_printf ("enable=%d", enable
);
130 mark_async_event_handler (infrun_async_inferior_event_token
);
132 clear_async_event_handler (infrun_async_inferior_event_token
);
139 mark_infrun_async_event_handler (void)
141 mark_async_event_handler (infrun_async_inferior_event_token
);
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;
149 show_step_stop_if_no_debug (struct ui_file
*file
, int from_tty
,
150 struct cmd_list_element
*c
, const char *value
)
152 gdb_printf (file
, _("Mode of the step operation is %s.\n"), value
);
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
159 static thread_info_ref previous_thread
;
164 update_previous_thread ()
166 if (inferior_ptid
== null_ptid
)
167 previous_thread
= nullptr;
169 previous_thread
= thread_info_ref::new_reference (inferior_thread ());
175 get_previous_thread ()
177 return previous_thread
.get ();
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'
185 static bool detach_fork
= true;
187 bool debug_infrun
= false;
189 show_debug_infrun (struct ui_file
*file
, int from_tty
,
190 struct cmd_list_element
*c
, const char *value
)
192 gdb_printf (file
, _("Inferior debugging is %s.\n"), value
);
195 /* Support for disabling address space randomization. */
197 bool disable_randomization
= true;
200 show_disable_randomization (struct ui_file
*file
, int from_tty
,
201 struct cmd_list_element
*c
, const char *value
)
203 if (target_supports_disable_randomization ())
205 _("Disabling randomization of debuggee's "
206 "virtual address space is %s.\n"),
209 gdb_puts (_("Disabling randomization of debuggee's "
210 "virtual address space is unsupported on\n"
211 "this platform.\n"), file
);
215 set_disable_randomization (const char *args
, int from_tty
,
216 struct cmd_list_element
*c
)
218 if (!target_supports_disable_randomization ())
219 error (_("Disabling randomization of debuggee's "
220 "virtual address space is unsupported on\n"
224 /* User interface for non-stop mode. */
226 bool non_stop
= false;
227 static bool non_stop_1
= false;
230 set_non_stop (const char *args
, int from_tty
,
231 struct cmd_list_element
*c
)
233 if (target_has_execution ())
235 non_stop_1
= non_stop
;
236 error (_("Cannot change this setting while the inferior is running."));
239 non_stop
= non_stop_1
;
243 show_non_stop (struct ui_file
*file
, int from_tty
,
244 struct cmd_list_element
*c
, const char *value
)
247 _("Controlling the inferior in non-stop mode is %s.\n"),
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. */
255 static bool observer_mode
= false;
256 static bool observer_mode_1
= false;
259 set_observer_mode (const char *args
, int from_tty
,
260 struct cmd_list_element
*c
)
262 if (target_has_execution ())
264 observer_mode_1
= observer_mode
;
265 error (_("Cannot change this setting while the inferior is running."));
268 observer_mode
= observer_mode_1
;
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. */
277 may_insert_fast_tracepoints
= true;
278 may_stop
= !observer_mode
;
279 update_target_permissions ();
281 /* Going *into* observer mode we must force non-stop, then
282 going out we leave it that way. */
285 pagination_enabled
= false;
286 non_stop
= non_stop_1
= true;
290 gdb_printf (_("Observer mode is now %s.\n"),
291 (observer_mode
? "on" : "off"));
295 show_observer_mode (struct ui_file
*file
, int from_tty
,
296 struct cmd_list_element
*c
, const char *value
)
298 gdb_printf (file
, _("Observer mode is %s.\n"), value
);
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. */
308 update_observer_mode (void)
310 bool newval
= (!may_insert_breakpoints
311 && !may_insert_tracepoints
312 && may_insert_fast_tracepoints
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"));
321 observer_mode
= observer_mode_1
= newval
;
324 /* Tables of how to react to signals; the user sets them. */
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
];
330 /* Table of signals that are registered with "catch signal". A
331 non-zero entry indicates that the signal is caught by some "catch
333 static unsigned char signal_catch
[GDB_SIGNAL_LAST
];
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
];
340 #define SET_SIGS(nsigs,sigs,flags) \
342 int signum = (nsigs); \
343 while (signum-- > 0) \
344 if ((sigs)[signum]) \
345 (flags)[signum] = 1; \
348 #define UNSET_SIGS(nsigs,sigs,flags) \
350 int signum = (nsigs); \
351 while (signum-- > 0) \
352 if ((sigs)[signum]) \
353 (flags)[signum] = 0; \
356 /* Update the target's copy of SIGNAL_PROGRAM. The sole purpose of
357 this function is to avoid exporting `signal_program'. */
360 update_signals_program_target (void)
362 target_program_signals (signal_program
);
365 /* Value to pass to target_resume() to cause all threads to resume. */
367 #define RESUME_ALL minus_one_ptid
369 /* Command list pointer for the "stop" placeholder. */
371 static struct cmd_list_element
*stop_command
;
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
;
377 /* Enable or disable optional shared library event breakpoints
378 as appropriate when the above flag is changed. */
381 set_stop_on_solib_events (const char *args
,
382 int from_tty
, struct cmd_list_element
*c
)
384 update_solib_breakpoints ();
388 show_stop_on_solib_events (struct ui_file
*file
, int from_tty
,
389 struct cmd_list_element
*c
, const char *value
)
391 gdb_printf (file
, _("Stopping for shared library events is %s.\n"),
395 /* True after stop if current stack frame should be printed. */
397 static bool stop_print_frame
;
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
;
406 void init_thread_stepping_state (struct thread_info
*tss
);
408 static const char follow_fork_mode_child
[] = "child";
409 static const char follow_fork_mode_parent
[] = "parent";
411 static const char *const follow_fork_mode_kind_names
[] = {
412 follow_fork_mode_child
,
413 follow_fork_mode_parent
,
417 static const char *follow_fork_mode_string
= follow_fork_mode_parent
;
419 show_follow_fork_mode_string (struct ui_file
*file
, int from_tty
,
420 struct cmd_list_element
*c
, const char *value
)
423 _("Debugger response to a program "
424 "call of fork or vfork is \"%s\".\n"),
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. */
436 follow_fork_inferior (bool follow_child
, bool detach_fork
)
438 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
440 infrun_debug_printf ("follow_child = %d, detach_fork = %d",
441 follow_child
, detach_fork
);
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 ();
451 && !non_stop
/* Non-stop always resumes both branches. */
452 && current_ui
->prompt_state
== PROMPT_BLOCKED
453 && !(follow_child
|| detach_fork
|| sched_multi
))
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"));
467 inferior
*parent_inf
= current_inferior ();
468 inferior
*child_inf
= nullptr;
470 gdb_assert (parent_inf
->thread_waiting_for_vfork_done
== nullptr);
474 /* Detach new forked process? */
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. */
486 /* Keep breakpoints list in sync. */
487 remove_breakpoints_inf (current_inferior ());
490 if (print_inferior_events
)
492 /* Ensure that we have a process ptid. */
493 ptid_t process_ptid
= ptid_t (child_ptid
.pid ());
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 ());
503 /* Add process to GDB's tables. */
504 child_inf
= add_inferior (child_ptid
.pid ());
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
;
511 child_inf
->symfile_flags
= SYMFILE_NO_READ
;
513 /* If this is a vfork child, then the address-space is
514 shared with the parent. */
517 child_inf
->pspace
= parent_inf
->pspace
;
518 child_inf
->aspace
= parent_inf
->aspace
;
520 exec_on_vfork (child_inf
);
522 /* The parent will be frozen until the child is done
523 with the shared region. Keep track of the
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;
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
);
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
;
553 ("parent_inf->thread_waiting_for_vfork_done == %s",
554 (parent_inf
->thread_waiting_for_vfork_done
== nullptr
556 : (parent_inf
->thread_waiting_for_vfork_done
557 ->ptid
.to_string ().c_str ())));
562 /* Follow the child. */
564 if (print_inferior_events
)
566 std::string parent_pid
= target_pid_to_str (parent_ptid
);
567 std::string child_pid
= target_pid_to_str (child_ptid
);
569 target_terminal::ours_for_output ();
570 gdb_printf (_("[Attaching after %s %s to child %s]\n"),
572 has_vforked
? "vfork" : "fork",
576 /* Add the new inferior first, so that the target_detach below
577 doesn't unpush the target. */
579 child_inf
= add_inferior (child_ptid
.pid ());
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
;
588 /* If this is a vfork child, then the address-space is shared
590 child_inf
->aspace
= parent_inf
->aspace
;
591 child_inf
->pspace
= parent_inf
->pspace
;
593 exec_on_vfork (child_inf
);
595 else if (detach_fork
)
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").
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
607 remove_breakpoints_inf (parent_inf
);
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
);
615 /* The parent inferior is still the current one, so keep things
617 set_current_program_space (parent_inf
->pspace
);
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
);
629 gdb_assert (current_inferior () == parent_inf
);
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).
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
638 target_follow_fork (child_inf
, child_ptid
, fork_kind
, follow_child
,
641 gdb::observers::inferior_forked
.notify (parent_inf
, child_inf
, fork_kind
);
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
);
647 /* If there is a child inferior, target_follow_fork must have created a thread
649 if (child_inf
!= nullptr)
650 gdb_assert (!child_inf
->thread_list
.empty ());
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
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
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 ();
669 /* Detach the parent if needed. */
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). */
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
;
691 else if (detach_fork
)
693 if (print_inferior_events
)
695 /* Ensure that we have a process ptid. */
696 ptid_t process_ptid
= ptid_t (parent_ptid
.pid ());
698 target_terminal::ours_for_output ();
699 gdb_printf (_("[Detaching after fork from "
701 target_pid_to_str (process_ptid
).c_str ());
704 target_detach (parent_inf
, 0);
708 /* If we ended up creating a new inferior, call post_create_inferior to inform
709 the various subcomponents. */
710 if (child_inf
!= nullptr)
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
;
716 if (!follow_child
&& !sched_multi
)
717 maybe_restore
.emplace ();
719 switch_to_thread (*child_inf
->threads ().begin ());
720 post_create_inferior (0);
726 /* Set the last target status as TP having stopped. */
729 set_last_target_status_stopped (thread_info
*tp
)
731 set_last_target_status (tp
->inf
->process_target (), tp
->ptid
,
732 target_waitstatus
{}.set_stopped (GDB_SIGNAL_0
));
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. */
742 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
744 bool follow_child
= (follow_fork_mode_string
== follow_fork_mode_child
);
745 bool should_resume
= true;
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 };
761 thread_info
*cur_thr
= inferior_thread ();
764 = user_visible_resume_ptid (cur_thr
->control
.stepping_command
);
765 process_stratum_target
*resume_target
766 = user_visible_resume_target (resume_ptid
);
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
,
779 /* follow_fork_inferior clears tp->pending_follow, and below
780 we'll need the value after the follow_fork_inferior
782 target_waitkind kind
= tp
->pending_follow
.kind ();
784 if (kind
!= TARGET_WAITKIND_SPURIOUS
)
786 infrun_debug_printf ("need to follow-fork [%s] first",
787 tp
->ptid
.to_string ().c_str ());
789 switch_to_thread (tp
);
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. */
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,
800 should_resume
= false;
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
))
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
);
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
822 if (kind
!= TARGET_WAITKIND_VFORKED
)
824 gdb_assert (kind
== TARGET_WAITKIND_FORKED
);
825 switch_to_thread (cur_thr
);
834 thread_info
*tp
= inferior_thread ();
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 ())
840 case TARGET_WAITKIND_FORKED
:
841 case TARGET_WAITKIND_VFORKED
:
843 ptid_t parent
, child
;
844 std::unique_ptr
<struct thread_fsm
> thread_fsm
;
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
)
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 ();
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
);
873 parent
= inferior_ptid
;
874 child
= tp
->pending_follow
.child_ptid ();
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
);
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
886 if (follow_fork_inferior (follow_child
, detach_fork
))
888 /* Target refused to follow, or there's some other reason
889 we shouldn't resume. */
894 /* If we followed the child, switch to it... */
897 tp
= parent_targ
->find_thread (child
);
898 switch_to_thread (tp
);
900 /* ... and preserve the stepping state, in case the
901 user was stepping over the fork call. */
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
));
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."));
927 /* Reset breakpoints in the child as appropriate. */
928 follow_inferior_reset_breakpoints ();
933 case TARGET_WAITKIND_SPURIOUS
:
934 /* Nothing to follow. */
937 internal_error ("Unexpected pending_follow.kind %d\n",
938 tp
->pending_follow
.kind ());
943 set_last_target_status_stopped (tp
);
944 return should_resume
;
948 follow_inferior_reset_breakpoints (void)
950 struct thread_info
*tp
= inferior_thread ();
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
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. */
965 if (tp
->control
.step_resume_breakpoint
)
967 breakpoint_re_set_thread (tp
->control
.step_resume_breakpoint
);
968 tp
->control
.step_resume_breakpoint
->first_loc ().enabled
= 1;
971 /* Treat exception_resume breakpoints like step_resume breakpoints. */
972 if (tp
->control
.exception_resume_breakpoint
)
974 breakpoint_re_set_thread (tp
->control
.exception_resume_breakpoint
);
975 tp
->control
.exception_resume_breakpoint
->first_loc ().enabled
= 1;
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. */
983 breakpoint_re_set ();
984 insert_breakpoints ();
987 /* The child has exited or execed: resume THREAD, a thread of the parent,
988 if it was meant to be executing. */
991 proceed_after_vfork_done (thread_info
*thread
)
993 if (thread
->state
== THREAD_RUNNING
994 && !thread
->executing ()
995 && !thread
->stop_requested
996 && thread
->stop_signal () == GDB_SIGNAL_0
)
998 infrun_debug_printf ("resuming vfork parent thread %s",
999 thread
->ptid
.to_string ().c_str ());
1001 switch_to_thread (thread
);
1002 clear_proceed_status (0);
1003 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
1007 /* Called whenever we notice an exec or exit event, to handle
1008 detaching or resuming a vfork parent. */
1011 handle_vfork_child_exec_or_exit (int exec
)
1013 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
1015 struct inferior
*inf
= current_inferior ();
1017 if (inf
->vfork_parent
)
1019 inferior
*resume_parent
= nullptr;
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;
1027 /* If the user wanted to detach from the parent, now is the
1029 if (vfork_parent
->pending_detach
)
1031 struct program_space
*pspace
;
1032 struct address_space
*aspace
;
1034 /* follow-fork child, detach-on-fork on. */
1036 vfork_parent
->pending_detach
= false;
1038 scoped_restore_current_pspace_and_thread restore_thread
;
1040 /* We're letting loose of the parent. */
1041 thread_info
*tp
= any_live_thread_of_inferior (vfork_parent
);
1042 switch_to_thread (tp
);
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
1056 pspace
= inf
->pspace
;
1057 aspace
= inf
->aspace
;
1058 inf
->aspace
= nullptr;
1059 inf
->pspace
= nullptr;
1061 if (print_inferior_events
)
1064 = target_pid_to_str (ptid_t (vfork_parent
->pid
));
1066 target_terminal::ours_for_output ();
1070 gdb_printf (_("[Detaching vfork parent %s "
1071 "after child exec]\n"), pidstr
.c_str ());
1075 gdb_printf (_("[Detaching vfork parent %s "
1076 "after child exit]\n"), pidstr
.c_str ());
1080 target_detach (vfork_parent
, 0);
1083 inf
->pspace
= pspace
;
1084 inf
->aspace
= aspace
;
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
);
1095 resume_parent
= vfork_parent
;
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
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 ();
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
);
1121 resume_parent
= vfork_parent
;
1124 gdb_assert (current_program_space
== inf
->pspace
);
1126 if (non_stop
&& resume_parent
!= nullptr)
1128 /* If the user wanted the parent to be running, let it go
1130 scoped_restore_current_thread restore_thread
;
1132 infrun_debug_printf ("resuming vfork parent process %d",
1133 resume_parent
->pid
);
1135 for (thread_info
*thread
: resume_parent
->threads ())
1136 proceed_after_vfork_done (thread
);
1141 /* Handle TARGET_WAITKIND_VFORK_DONE. */
1144 handle_vfork_done (thread_info
*event_thread
)
1146 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
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.
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)
1159 infrun_debug_printf ("not waiting for a vfork-done event");
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
);
1170 event_thread
->inf
->thread_waiting_for_vfork_done
= nullptr;
1171 event_thread
->inf
->pspace
->breakpoints_not_allowed
= 0;
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 ())
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
;
1182 insert_breakpoints ();
1185 if (!step_over_info_valid_p ())
1186 restart_threads (event_thread
, event_thread
->inf
);
1190 /* Enum strings for "set|show follow-exec-mode". */
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
[] =
1196 follow_exec_mode_new
,
1197 follow_exec_mode_same
,
1201 static const char *follow_exec_mode_string
= follow_exec_mode_same
;
1203 show_follow_exec_mode_string (struct ui_file
*file
, int from_tty
,
1204 struct cmd_list_element
*c
, const char *value
)
1206 gdb_printf (file
, _("Follow exec mode is \"%s\".\n"), value
);
1209 /* EXEC_FILE_TARGET is assumed to be non-NULL. */
1212 follow_exec (ptid_t ptid
, const char *exec_file_target
)
1214 int pid
= ptid
.pid ();
1215 ptid_t process_ptid
;
1217 /* Switch terminal for any messages produced e.g. by
1218 breakpoint_re_set. */
1219 target_terminal::ours_for_output ();
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.
1225 If there are breakpoints, they aren't really inserted now,
1226 since the exec() transformed our inferior into a fresh set
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.
1233 However, any "raw" breakpoints must be removed from the list
1234 (e.g., the solib bp's), since their address is probably invalid
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. */
1242 mark_breakpoints_out ();
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
1261 for (thread_info
*th
: all_threads_safe ())
1262 if (th
->ptid
.pid () == pid
&& th
->ptid
!= ptid
)
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;
1276 /* The user may have had the main thread held stopped in the
1277 previous image (e.g., schedlock on, or non-stop). Release
1279 th
->stop_requested
= 0;
1281 update_breakpoints_after_exec ();
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 (),
1289 /* We've followed the inferior through an exec. Therefore, the
1290 inferior has essentially been killed & reborn. */
1292 breakpoint_init_inferior (inf_execd
);
1294 gdb::unique_xmalloc_ptr
<char> exec_file_host
1295 = exec_file_find (exec_file_target
, nullptr);
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\"?"),
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);
1314 inferior
*execing_inferior
= current_inferior ();
1315 inferior
*following_inferior
;
1317 if (follow_exec_mode_string
== follow_exec_mode_new
)
1319 /* The user wants to keep the old inferior and program spaces
1320 around. Create a new fresh one, and switch to it. */
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 ();
1328 swap_terminal_info (following_inferior
, execing_inferior
);
1329 exit_inferior (execing_inferior
);
1331 following_inferior
->pid
= pid
;
1335 /* follow-exec-mode is "same", we continue execution in the execing
1337 following_inferior
= execing_inferior
;
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
1345 target_clear_description ();
1348 target_follow_exec (following_inferior
, ptid
, exec_file_target
);
1350 gdb_assert (current_inferior () == following_inferior
);
1351 gdb_assert (current_program_space
== following_inferior
->pspace
);
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
);
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
1367 target_find_description ();
1369 gdb::observers::inferior_execd
.notify (execing_inferior
, following_inferior
);
1371 breakpoint_re_set ();
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 ();
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.). */
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
;
1393 /* Bit flags indicating what the thread needs to step over. */
1395 enum step_over_what_flag
1397 /* Step over a breakpoint. */
1398 STEP_OVER_BREAKPOINT
= 1,
1400 /* Step past a non-continuable watchpoint, in order to let the
1401 instruction execute so we can evaluate the watchpoint
1403 STEP_OVER_WATCHPOINT
= 2
1405 DEF_ENUM_FLAGS_TYPE (enum step_over_what_flag
, step_over_what
);
1407 /* Info about an instruction that is being stepped over. */
1409 struct step_over_info
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
1415 const address_space
*aspace
= nullptr;
1416 CORE_ADDR address
= 0;
1418 /* The instruction being stepped over triggers a nonsteppable
1419 watchpoint. If true, we'll skip inserting watchpoints. */
1420 int nonsteppable_watchpoint_p
= 0;
1422 /* The thread's global number. */
1426 /* The step-over info of the location that is being stepped over.
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).
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
1450 static struct step_over_info step_over_info
;
1452 /* Record the address of the breakpoint/instruction we're currently
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. */
1458 set_step_over_info (const address_space
*aspace
, CORE_ADDR address
,
1459 int nonsteppable_watchpoint_p
,
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
;
1468 /* Called when we're not longer stepping over a breakpoint / an
1469 instruction, so all breakpoints are free to be (re)inserted. */
1472 clear_step_over_info (void)
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;
1484 stepping_past_instruction_at (struct address_space
*aspace
,
1487 return (step_over_info
.aspace
!= nullptr
1488 && breakpoint_address_match (aspace
, address
,
1489 step_over_info
.aspace
,
1490 step_over_info
.address
));
1496 thread_is_stepping_over_breakpoint (int thread
)
1498 return (step_over_info
.thread
!= -1
1499 && thread
== step_over_info
.thread
);
1505 stepping_past_nonsteppable_watchpoint (void)
1507 return step_over_info
.nonsteppable_watchpoint_p
;
1510 /* Returns true if step-over info is valid. */
1513 step_over_info_valid_p (void)
1515 return (step_over_info
.aspace
!= nullptr
1516 || stepping_past_nonsteppable_watchpoint ());
1520 /* Displaced stepping. */
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.
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:
1532 a0) Initially, all threads are stopped, and breakpoints are not
1534 a1) We single-step T, leaving breakpoints uninserted.
1535 a2) We insert breakpoints, and resume all threads.
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
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
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.
1554 This approach depends on the following gdbarch methods:
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.
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.
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.
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.
1578 See the comments in gdbarch.sh for details.
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.
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. */
1606 /* Return true if THREAD is doing a displaced step. */
1609 displaced_step_in_progress_thread (thread_info
*thread
)
1611 gdb_assert (thread
!= nullptr);
1613 return thread
->displaced_step_state
.in_progress ();
1616 /* Return true if INF has a thread doing a displaced step. */
1619 displaced_step_in_progress (inferior
*inf
)
1621 return inf
->displaced_step_state
.in_progress_count
> 0;
1624 /* Return true if any thread is doing a displaced step. */
1627 displaced_step_in_progress_any_thread ()
1629 for (inferior
*inf
: all_non_exited_inferiors ())
1631 if (displaced_step_in_progress (inf
))
1639 infrun_inferior_exit (struct inferior
*inf
)
1641 inf
->displaced_step_state
.reset ();
1642 inf
->thread_waiting_for_vfork_done
= nullptr;
1646 infrun_inferior_execd (inferior
*exec_inf
, inferior
*follow_inf
)
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 ();
1654 for (thread_info
*thread
: follow_inf
->threads ())
1655 thread
->displaced_step_state
.reset ();
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
1660 clear_step_over_info ();
1662 follow_inf
->thread_waiting_for_vfork_done
= nullptr;
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). */
1672 static enum auto_boolean can_use_displaced_stepping
= AUTO_BOOLEAN_AUTO
;
1675 show_can_use_displaced_stepping (struct ui_file
*file
, int from_tty
,
1676 struct cmd_list_element
*c
,
1679 if (can_use_displaced_stepping
== AUTO_BOOLEAN_AUTO
)
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");
1686 _("Debugger's willingness to use displaced stepping "
1687 "to step over breakpoints is %s.\n"), value
);
1690 /* Return true if the gdbarch implements the required methods to use
1691 displaced stepping. */
1694 gdbarch_supports_displaced_stepping (gdbarch
*arch
)
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
);
1701 /* Return non-zero if displaced stepping can/should be used to step
1702 over breakpoints of thread TP. */
1705 use_displaced_stepping (thread_info
*tp
)
1707 /* If the user disabled it explicitly, don't use displaced stepping. */
1708 if (can_use_displaced_stepping
== AUTO_BOOLEAN_FALSE
)
1711 /* If "auto", only use displaced stepping if the target operates in a non-stop
1713 if (can_use_displaced_stepping
== AUTO_BOOLEAN_AUTO
1714 && !target_is_non_stop_p ())
1717 gdbarch
*gdbarch
= get_thread_regcache (tp
)->arch ();
1719 /* If the architecture doesn't implement displaced stepping, don't use
1721 if (!gdbarch_supports_displaced_stepping (gdbarch
))
1724 /* If recording, don't use displaced stepping. */
1725 if (find_record_target () != nullptr)
1728 /* If displaced stepping failed before for this inferior, don't bother trying
1730 if (tp
->inf
->displaced_step_state
.failed_before
)
1736 /* Simple function wrapper around displaced_step_thread_state::reset. */
1739 displaced_step_reset (displaced_step_thread_state
*displaced
)
1741 displaced
->reset ();
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 ()". */
1747 using displaced_step_reset_cleanup
= FORWARD_SCOPE_EXIT (displaced_step_reset
);
1749 /* Prepare to single-step, using displaced stepping.
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.
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
1767 static displaced_step_prepare_status
1768 displaced_step_prepare_throw (thread_info
*tp
)
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
;
1775 /* We should never reach this function if the architecture does not
1776 support displaced stepping. */
1777 gdb_assert (gdbarch_supports_displaced_stepping (gdbarch
));
1779 /* Nor if the thread isn't meant to step over a breakpoint. */
1780 gdb_assert (tp
->control
.trap_expected
);
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
1786 tp
->control
.may_range_step
= 0;
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 ());
1792 if (tp
->inf
->displaced_step_state
.unavailable
)
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. */
1797 displaced_debug_printf ("deferring step of %s",
1798 tp
->ptid
.to_string ().c_str ());
1800 global_thread_step_over_chain_enqueue (tp
);
1801 return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
;
1804 displaced_debug_printf ("displaced-stepping %s now",
1805 tp
->ptid
.to_string ().c_str ());
1807 scoped_restore_current_thread restore_thread
;
1809 switch_to_thread (tp
);
1811 CORE_ADDR original_pc
= regcache_read_pc (regcache
);
1812 CORE_ADDR displaced_pc
;
1814 /* Display the instruction we are going to displaced step. */
1815 if (debug_displaced
)
1817 string_file tmp_stream
;
1818 int dislen
= gdb_print_insn (gdbarch
, original_pc
, &tmp_stream
,
1823 gdb::byte_vector
insn_buf (dislen
);
1824 read_memory (original_pc
, insn_buf
.data (), insn_buf
.size ());
1826 std::string insn_bytes
= bytes_to_string (insn_buf
);
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 ());
1834 displaced_debug_printf ("original insn %s: invalid length: %d",
1835 paddress (gdbarch
, original_pc
), dislen
);
1838 displaced_step_prepare_status status
1839 = gdbarch_displaced_step_prepare (gdbarch
, tp
, displaced_pc
);
1841 if (status
== DISPLACED_STEP_PREPARE_STATUS_CANT
)
1843 displaced_debug_printf ("failed to prepare (%s)",
1844 tp
->ptid
.to_string ().c_str ());
1846 return DISPLACED_STEP_PREPARE_STATUS_CANT
;
1848 else if (status
== DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
)
1850 /* Not enough displaced stepping resources available, defer this
1851 request by placing it the queue. */
1853 displaced_debug_printf ("not enough resources available, "
1854 "deferring step of %s",
1855 tp
->ptid
.to_string ().c_str ());
1857 global_thread_step_over_chain_enqueue (tp
);
1859 return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
;
1862 gdb_assert (status
== DISPLACED_STEP_PREPARE_STATUS_OK
);
1864 /* Save the information we need to fix things up if the step
1866 disp_step_thread_state
.set (gdbarch
);
1868 tp
->inf
->displaced_step_state
.in_progress_count
++;
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
));
1876 /* Display the new displaced instruction(s). */
1877 if (debug_displaced
)
1879 string_file tmp_stream
;
1880 CORE_ADDR addr
= displaced_pc
;
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.
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. */
1890 = addr
+ (gdbarch_displaced_step_hw_singlestep (gdbarch
)
1891 ? 1 : gdbarch_displaced_step_buffer_length (gdbarch
));
1895 int dislen
= gdb_print_insn (gdbarch
, addr
, &tmp_stream
, nullptr);
1898 displaced_debug_printf
1899 ("replacement insn %s: invalid length: %d",
1900 paddress (gdbarch
, addr
), dislen
);
1904 gdb::byte_vector
insn_buf (dislen
);
1905 read_memory (addr
, insn_buf
.data (), insn_buf
.size ());
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 (),
1917 return DISPLACED_STEP_PREPARE_STATUS_OK
;
1920 /* Wrapper for displaced_step_prepare_throw that disabled further
1921 attempts at displaced stepping if we get a memory error. */
1923 static displaced_step_prepare_status
1924 displaced_step_prepare (thread_info
*thread
)
1926 displaced_step_prepare_status status
1927 = DISPLACED_STEP_PREPARE_STATUS_CANT
;
1931 status
= displaced_step_prepare_throw (thread
);
1933 catch (const gdb_exception_error
&ex
)
1935 if (ex
.error
!= MEMORY_ERROR
1936 && ex
.error
!= NOT_SUPPORTED_ERROR
)
1939 infrun_debug_printf ("caught exception, disabling displaced stepping: %s",
1942 /* Be verbose if "set displaced-stepping" is "on", silent if
1944 if (can_use_displaced_stepping
== AUTO_BOOLEAN_TRUE
)
1946 warning (_("disabling displaced stepping: %s"),
1950 /* Disable further displaced stepping attempts. */
1951 thread
->inf
->displaced_step_state
.failed_before
= 1;
1957 /* Maybe disable thread-{cloned,created,exited} event reporting after
1958 a step-over (either in-line or displaced) finishes. */
1961 update_thread_events_after_step_over (thread_info
*event_thread
)
1963 if (target_supports_set_thread_options (0))
1965 /* We can control per-thread options. Disable events for the
1967 event_thread
->set_thread_options (0);
1971 /* We can only control the target-wide target_thread_events
1972 setting. Disable it, but only if other threads don't need it
1974 if (!displaced_step_in_progress_any_thread ())
1975 target_thread_events (false);
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.
1985 If the thread wasn't displaced stepping, return
1986 DISPLACED_STEP_FINISH_STATUS_OK as well. */
1988 static displaced_step_finish_status
1989 displaced_step_finish (thread_info
*event_thread
,
1990 const target_waitstatus
&event_status
)
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
;
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
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.
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 ());
2017 displaced_step_thread_state
*displaced
= &event_thread
->displaced_step_state
;
2019 /* Was this thread performing a displaced step? */
2020 if (!displaced
->in_progress ())
2021 return DISPLACED_STEP_FINISH_STATUS_OK
;
2023 update_thread_events_after_step_over (event_thread
);
2025 gdb_assert (event_thread
->inf
->displaced_step_state
.in_progress_count
> 0);
2026 event_thread
->inf
->displaced_step_state
.in_progress_count
--;
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
);
2034 displaced_step_reset_cleanup
cleanup (displaced
);
2036 /* Do the fixup, and release the resources acquired to do the displaced
2038 displaced_step_finish_status status
2039 = gdbarch_displaced_step_finish (displaced
->get_original_gdbarch (),
2040 event_thread
, event_status
);
2042 if (event_status
.kind () == TARGET_WAITKIND_FORKED
2043 || event_status
.kind () == TARGET_WAITKIND_VFORKED
2044 || event_status
.kind () == TARGET_WAITKIND_THREAD_CLONED
)
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
2054 struct regcache
*child_regcache
2055 = get_thread_arch_aspace_regcache (parent_inf
,
2056 event_status
.child_ptid (),
2058 parent_inf
->aspace
);
2059 /* Read PC value of parent. */
2060 CORE_ADDR parent_pc
= regcache_read_pc (regcache
);
2062 displaced_debug_printf ("write child pc from %s to %s",
2064 regcache_read_pc (child_regcache
)),
2065 paddress (gdbarch
, parent_pc
));
2067 regcache_write_pc (child_regcache
, parent_pc
);
2073 /* Data to be passed around while handling an event. This data is
2074 discarded between events. */
2075 struct execution_control_state
2077 explicit execution_control_state (thread_info
*thr
= nullptr)
2078 : ptid (thr
== nullptr ? null_ptid
: thr
->ptid
),
2083 process_stratum_target
*target
= nullptr;
2085 /* The thread that got the event, if this was a thread event; NULL
2087 struct thread_info
*event_thread
;
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;
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;
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
);
2109 /* Are there any pending step-over requests? If so, run all we can
2110 now and return true. Otherwise, return false. */
2113 start_step_over (void)
2115 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
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 ())
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
2126 thread_step_over_list threads_to_step
2127 = std::move (global_thread_step_over_list
);
2129 infrun_debug_printf ("stealing global queue of threads to step, length = %d",
2130 thread_step_over_chain_length (threads_to_step
));
2132 bool started
= false;
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
2139 if (threads_to_step
.empty ())
2140 infrun_debug_printf ("step-over queue now empty");
2143 infrun_debug_printf ("putting back %d threads to step in global queue",
2144 thread_step_over_chain_length (threads_to_step
));
2146 global_thread_step_over_chain_enqueue_chain
2147 (std::move (threads_to_step
));
2151 thread_step_over_list_safe_range range
2152 = make_thread_step_over_list_safe_range (threads_to_step
);
2154 for (thread_info
*tp
: range
)
2156 step_over_what step_what
;
2157 int must_be_in_line
;
2159 gdb_assert (!tp
->stop_requested
);
2161 if (tp
->inf
->displaced_step_state
.unavailable
)
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. */
2169 if (tp
->inf
->thread_waiting_for_vfork_done
!= nullptr)
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
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
));
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
)));
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 ())
2197 global_thread_step_over_chain_enqueue (tp
);
2201 if (tp
->control
.trap_expected
2203 || tp
->executing ())
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
,
2213 infrun_debug_printf ("resuming [%s] for step-over",
2214 tp
->ptid
.to_string ().c_str ());
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
)
2225 switch_to_thread (tp
);
2226 execution_control_state
ecs (tp
);
2227 keep_going_pass_signal (&ecs
);
2229 if (!ecs
.wait_some_more
)
2230 error (_("Command aborted."));
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. */
2236 infrun_debug_printf ("[%s] was resumed.",
2237 tp
->ptid
.to_string ().c_str ());
2238 gdb_assert (!thread_is_in_step_over_chain (tp
));
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
));
2247 /* If we started a new in-line step-over, we're done. */
2248 if (step_over_info_valid_p ())
2250 gdb_assert (tp
->control
.trap_expected
);
2255 if (!target_is_non_stop_p ())
2257 /* On all-stop, shouldn't have resumed unless we needed a
2259 gdb_assert (tp
->control
.trap_expected
2260 || tp
->step_after_step_resume_breakpoint
);
2262 /* With remote targets (at least), in all-stop, we can't
2263 issue any further remote commands until the program stops
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. */
2278 /* Update global variables holding ptids to hold NEW_PTID if they were
2279 holding OLD_PTID. */
2281 infrun_thread_ptid_changed (process_stratum_target
*target
,
2282 ptid_t old_ptid
, ptid_t new_ptid
)
2284 if (inferior_ptid
== old_ptid
2285 && current_inferior ()->process_target () == target
)
2286 inferior_ptid
= new_ptid
;
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
[] = {
2302 static const char *scheduler_mode
= schedlock_replay
;
2304 show_scheduler_mode (struct ui_file
*file
, int from_tty
,
2305 struct cmd_list_element
*c
, const char *value
)
2308 _("Mode for locking scheduler "
2309 "during execution is \"%s\".\n"),
2314 set_schedlock_func (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2316 if (!target_can_lock_scheduler ())
2318 scheduler_mode
= schedlock_off
;
2319 error (_("Target '%s' cannot support this command."),
2320 target_shortname ());
2324 /* True if execution commands resume all threads of all processes by
2325 default; otherwise, resume only threads of the current inferior
2327 bool sched_multi
= false;
2329 /* Try to setup for software single stepping. Return true if target_resume()
2330 should use hardware single step.
2332 GDBARCH the current gdbarch. */
2335 maybe_software_singlestep (struct gdbarch
*gdbarch
)
2337 bool hw_step
= true;
2339 if (execution_direction
== EXEC_FORWARD
2340 && gdbarch_software_single_step_p (gdbarch
))
2341 hw_step
= !insert_single_step_breakpoints (gdbarch
);
2349 user_visible_resume_ptid (int step
)
2355 /* With non-stop mode on, threads are always handled
2357 resume_ptid
= inferior_ptid
;
2359 else if ((scheduler_mode
== schedlock_on
)
2360 || (scheduler_mode
== schedlock_step
&& step
))
2362 /* User-settable 'scheduler' mode requires solo thread
2364 resume_ptid
= inferior_ptid
;
2366 else if ((scheduler_mode
== schedlock_replay
)
2367 && target_record_will_replay (minus_one_ptid
, execution_direction
))
2369 /* User-settable 'scheduler' mode requires solo thread resume in replay
2371 resume_ptid
= inferior_ptid
;
2373 else if (!sched_multi
&& target_supports_multi_process ())
2375 /* Resume all threads of the current process (and none of other
2377 resume_ptid
= ptid_t (inferior_ptid
.pid ());
2381 /* Resume all threads of all processes. */
2382 resume_ptid
= RESUME_ALL
;
2390 process_stratum_target
*
2391 user_visible_resume_target (ptid_t resume_ptid
)
2393 return (resume_ptid
== minus_one_ptid
&& sched_multi
2395 : current_inferior ()->process_target ());
2398 /* Find a thread from the inferiors that we'll resume that is waiting
2399 for a vfork-done event. */
2401 static thread_info
*
2402 find_thread_waiting_for_vfork_done ()
2404 gdb_assert (!target_is_non_stop_p ());
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
;
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
;
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. */
2428 internal_resume_ptid (int user_step
)
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
;
2437 /* The rest of the function assumes non-stop==off and
2438 target-non-stop==off.
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.
2446 The resumption set width depends on whether schedule-multiple is on or off.
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:
2454 - All threads of inferior 1
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
2461 thread_info
*thr
= find_thread_waiting_for_vfork_done ();
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 ());
2474 return user_visible_resume_ptid (user_step
);
2477 /* Wrapper for target_resume, that handles infrun-specific
2481 do_target_resume (ptid_t resume_ptid
, bool step
, enum gdb_signal sig
)
2483 struct thread_info
*tp
= inferior_thread ();
2485 gdb_assert (!tp
->stop_requested
);
2487 /* Install inferior's terminal modes. */
2488 target_terminal::inferior ();
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
);
2494 /* Advise target which signals may be handled silently.
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
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:
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.
2510 - when the thread is later resumed, the signal handler would
2511 return to the scratch pad area, which would no longer be
2513 if (step_over_info_valid_p ()
2514 || displaced_step_in_progress (tp
->inf
))
2515 target_pass_signals ({});
2517 target_pass_signals (signal_pass
);
2519 /* Request that the target report thread-{created,cloned,exited}
2520 events in the following situations:
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.
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
2537 if (step_over_info_valid_p ()
2538 || displaced_step_in_progress_thread (tp
))
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
);
2545 target_thread_events (true);
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))
2553 process_stratum_target
*resume_target
= tp
->inf
->process_target ();
2554 for (thread_info
*thr_iter
: all_non_exited_threads (resume_target
,
2557 thr_iter
->set_thread_options (0);
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
));
2564 target_resume (resume_ptid
, step
, sig
);
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. */
2572 resume_1 (enum gdb_signal sig
)
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 ();
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
2589 gdb_assert (!tp
->stop_requested
);
2590 gdb_assert (!thread_is_in_step_over_chain (tp
));
2592 if (tp
->has_pending_waitstatus ())
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
));
2601 tp
->inf
->process_target ()->threads_executing
= true;
2602 tp
->set_resumed (true);
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
)
2609 warning (_("Couldn't deliver signal %s to %s."),
2610 gdb_signal_to_name (sig
),
2611 tp
->ptid
.to_string ().c_str ());
2614 tp
->set_stop_signal (GDB_SIGNAL_0
);
2616 if (target_can_async_p ())
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
);
2625 tp
->stepped_breakpoint
= 0;
2627 /* Depends on stepped_breakpoint. */
2628 step
= currently_stepping (tp
);
2630 if (current_inferior ()->thread_waiting_for_vfork_done
!= nullptr)
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");
2647 CORE_ADDR pc
= regcache_read_pc (regcache
);
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
));
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
)
2662 if (sig
!= GDB_SIGNAL_0
)
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. */
2678 infrun_debug_printf ("resume: skipping permanent breakpoint, "
2679 "deliver signal first");
2681 clear_step_over_info ();
2682 tp
->control
.trap_expected
= 0;
2684 if (tp
->control
.step_resume_breakpoint
== nullptr)
2686 /* Set a "high-priority" step-resume, as we don't want
2687 user breakpoints at PC to trigger (again) when this
2689 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2690 gdb_assert (tp
->control
.step_resume_breakpoint
->first_loc ()
2693 tp
->step_after_step_resume_breakpoint
= step
;
2696 insert_breakpoints ();
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
);
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
2719 gdb_assert (!step_over_info_valid_p ());
2720 insert_single_step_breakpoint (gdbarch
, aspace
, pc
);
2721 insert_breakpoints ();
2723 resume_ptid
= internal_resume_ptid (user_step
);
2724 do_target_resume (resume_ptid
, false, GDB_SIGNAL_0
);
2725 tp
->set_resumed (true);
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;
2736 /* If displaced stepping is enabled, step over breakpoints by executing a
2737 copy of the instruction at a different address.
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.
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)
2753 displaced_step_prepare_status prepare_status
2754 = displaced_step_prepare (tp
);
2756 if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
)
2758 infrun_debug_printf ("Got placed in step-over queue");
2760 tp
->control
.trap_expected
= 0;
2763 else if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_CANT
)
2765 /* Fallback to stepping over the breakpoint in-line. */
2767 if (target_is_non_stop_p ())
2768 stop_all_threads ("displaced stepping falling back on inline stepping");
2770 set_step_over_info (regcache
->aspace (),
2771 regcache_read_pc (regcache
), 0, tp
->global_num
);
2773 step
= maybe_software_singlestep (gdbarch
);
2775 insert_breakpoints ();
2777 else if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_OK
)
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
));
2783 step
= gdbarch_displaced_step_hw_singlestep (gdbarch
);
2786 gdb_assert_not_reached ("Invalid displaced_step_prepare_status "
2790 /* Do we need to do it the hard way, w/temp breakpoints? */
2792 step
= maybe_software_singlestep (gdbarch
);
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.
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.
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.
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 ())
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)
2829 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2830 tp
->step_after_step_resume_breakpoint
= 1;
2833 delete_single_step_breakpoints (tp
);
2835 clear_step_over_info ();
2836 tp
->control
.trap_expected
= 0;
2838 insert_breakpoints ();
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
));
2846 /* Decide the set of threads to ask the target to resume. */
2847 if (tp
->control
.trap_expected
)
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
;
2861 resume_ptid
= internal_resume_ptid (user_step
);
2863 if (execution_direction
!= EXEC_REVERSE
2864 && step
&& breakpoint_inserted_here_p (aspace
, pc
))
2866 /* There are two cases where we currently need to step a
2867 breakpoint instruction when we have a signal to deliver:
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.
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. */
2894 infrun_debug_printf ("resume: [%s] stepped breakpoint",
2895 tp
->ptid
.to_string ().c_str ());
2897 tp
->stepped_breakpoint
= 1;
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
))
2906 if (tp
->control
.may_range_step
)
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
));
2916 do_target_resume (resume_ptid
, step
, sig
);
2917 tp
->set_resumed (true);
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. */
2925 resume (gdb_signal sig
)
2931 catch (const gdb_exception
&ex
)
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 ());
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
2956 static ULONGEST current_stop_id
;
2963 return current_stop_id
;
2966 /* Called when we report a user visible stop. */
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'. */
2978 clear_proceed_status_thread (struct thread_info
*tp
)
2980 infrun_debug_printf ("%s", tp
->ptid
.to_string ().c_str ());
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 ())
2986 if (tp
->stop_reason () == TARGET_STOPPED_BY_SINGLE_STEP
)
2988 infrun_debug_printf ("pending event of %s was a finished step. "
2990 tp
->ptid
.to_string ().c_str ());
2992 tp
->clear_pending_waitstatus ();
2993 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
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
));
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
);
3010 tp
->release_thread_fsm ();
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;
3022 tp
->control
.stop_step
= 0;
3024 tp
->control
.proceed_to_finish
= 0;
3026 tp
->control
.stepping_command
= 0;
3028 /* Discard any remaining commands or status from previous stop. */
3029 bpstat_clear (&tp
->control
.stop_bpstat
);
3032 /* Notify the current interpreter and observers that the target is about to
3036 notify_about_to_proceed ()
3038 top_level_interpreter ()->on_about_to_proceed ();
3039 gdb::observers::about_to_proceed
.notify ();
3043 clear_proceed_status (int step
)
3045 /* With scheduler-locking replay, stop replaying other threads if we're
3046 not replaying the user-visible resume ptid.
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 ();
3057 if (!non_stop
&& inferior_ptid
!= null_ptid
)
3059 ptid_t resume_ptid
= user_visible_resume_ptid (step
);
3060 process_stratum_target
*resume_target
3061 = user_visible_resume_target (resume_ptid
);
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
);
3069 if (inferior_ptid
!= null_ptid
)
3071 struct inferior
*inferior
;
3075 /* If in non-stop mode, only delete the per-thread status of
3076 the current thread. */
3077 clear_proceed_status_thread (inferior_thread ());
3080 inferior
= current_inferior ();
3081 inferior
->control
.stop_soon
= NO_STOP_QUIETLY
;
3084 notify_about_to_proceed ();
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. */
3092 thread_still_needs_step_over_bp (struct thread_info
*tp
)
3094 if (tp
->stepping_over_breakpoint
)
3096 struct regcache
*regcache
= get_thread_regcache (tp
);
3098 if (breakpoint_here_p (regcache
->aspace (),
3099 regcache_read_pc (regcache
))
3100 == ordinary_breakpoint_here
)
3103 tp
->stepping_over_breakpoint
= 0;
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. */
3113 static step_over_what
3114 thread_still_needs_step_over (struct thread_info
*tp
)
3116 step_over_what what
= 0;
3118 if (thread_still_needs_step_over_bp (tp
))
3119 what
|= STEP_OVER_BREAKPOINT
;
3121 if (tp
->stepping_over_watchpoint
3122 && !target_have_steppable_watchpoint ())
3123 what
|= STEP_OVER_WATCHPOINT
;
3128 /* Returns true if scheduler locking applies. STEP indicates whether
3129 we're about to do a step/next-like command to a thread. */
3132 schedlock_applies (struct thread_info
*tp
)
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
)));
3142 /* Set process_stratum_target::COMMIT_RESUMED_STATE in all target
3143 stacks that have threads executing and don't have threads with
3147 maybe_set_commit_resumed_all_targets ()
3149 scoped_restore_current_thread restore_thread
;
3151 for (inferior
*inf
: all_non_exited_inferiors ())
3153 process_stratum_target
*proc_target
= inf
->process_target ();
3155 if (proc_target
->commit_resumed_state
)
3157 /* We already set this in a previous iteration, via another
3158 inferior sharing the process_stratum target. */
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
)
3166 infrun_debug_printf ("not requesting commit-resumed for target "
3167 "%s, no resumed threads",
3168 proc_target
->shortname ());
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 ())
3178 infrun_debug_printf ("not requesting commit-resumed for target %s, a"
3179 " thread has a pending waitstatus",
3180 proc_target
->shortname ());
3184 switch_to_inferior_no_thread (inf
);
3186 if (target_has_pending_events ())
3188 infrun_debug_printf ("not requesting commit-resumed for target %s, "
3189 "target has pending events",
3190 proc_target
->shortname ());
3194 infrun_debug_printf ("enabling commit-resumed for target %s",
3195 proc_target
->shortname ());
3197 proc_target
->commit_resumed_state
= true;
3204 maybe_call_commit_resumed_all_targets ()
3206 scoped_restore_current_thread restore_thread
;
3208 for (inferior
*inf
: all_non_exited_inferiors ())
3210 process_stratum_target
*proc_target
= inf
->process_target ();
3212 if (!proc_target
->commit_resumed_state
)
3215 switch_to_inferior_no_thread (inf
);
3217 infrun_debug_printf ("calling commit_resumed for target %s",
3218 proc_target
->shortname());
3220 target_commit_resumed ();
3224 /* To track nesting of scoped_disable_commit_resumed objects, ensuring
3225 that only the outermost one attempts to re-enable
3227 static bool enable_commit_resumed
= true;
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
)
3236 infrun_debug_printf ("reason=%s", m_reason
);
3238 enable_commit_resumed
= false;
3240 for (inferior
*inf
: all_non_exited_inferiors ())
3242 process_stratum_target
*proc_target
= inf
->process_target ();
3244 if (m_prev_enable_commit_resumed
)
3246 /* This is the outermost instance: force all
3247 COMMIT_RESUMED_STATE to false. */
3248 proc_target
->commit_resumed_state
= false;
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
);
3263 scoped_disable_commit_resumed::reset ()
3269 infrun_debug_printf ("reason=%s", m_reason
);
3271 gdb_assert (!enable_commit_resumed
);
3273 enable_commit_resumed
= m_prev_enable_commit_resumed
;
3275 if (m_prev_enable_commit_resumed
)
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 ();
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 ())
3287 process_stratum_target
*proc_target
= inf
->process_target ();
3288 gdb_assert (!proc_target
->commit_resumed_state
);
3295 scoped_disable_commit_resumed::~scoped_disable_commit_resumed ()
3303 scoped_disable_commit_resumed::reset_and_commit ()
3306 maybe_call_commit_resumed_all_targets ();
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
)
3316 infrun_debug_printf ("reason=%s", m_reason
);
3318 if (!enable_commit_resumed
)
3320 enable_commit_resumed
= true;
3322 /* Re-enable COMMIT_RESUMED_STATE on the targets where it's
3324 maybe_set_commit_resumed_all_targets ();
3326 maybe_call_commit_resumed_all_targets ();
3332 scoped_enable_commit_resumed::~scoped_enable_commit_resumed ()
3334 infrun_debug_printf ("reason=%s", m_reason
);
3336 gdb_assert (enable_commit_resumed
);
3338 enable_commit_resumed
= m_prev_enable_commit_resumed
;
3340 if (!enable_commit_resumed
)
3342 /* Force all COMMIT_RESUMED_STATE back to false. */
3343 for (inferior
*inf
: all_non_exited_inferiors ())
3345 process_stratum_target
*proc_target
= inf
->process_target ();
3346 proc_target
->commit_resumed_state
= false;
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
3361 check_multi_target_resumption (process_stratum_target
*resume_target
)
3363 if (!non_stop
&& resume_target
== nullptr)
3365 scoped_restore_current_thread restore_thread
;
3367 /* This is used to track whether we're resuming more than one
3369 process_stratum_target
*first_connection
= nullptr;
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;
3375 for (inferior
*inf
: all_non_exited_inferiors ())
3377 switch_to_inferior_no_thread (inf
);
3379 if (!target_has_execution ())
3382 process_stratum_target
*proc_target
3383 = current_inferior ()->process_target();
3385 if (!target_is_non_stop_p ())
3386 first_not_non_stop
= inf
;
3388 if (first_connection
== nullptr)
3389 first_connection
= proc_target
;
3390 else if (first_connection
!= proc_target
3391 && first_not_non_stop
!= nullptr)
3393 switch_to_inferior_no_thread (first_not_non_stop
);
3395 proc_target
= current_inferior ()->process_target();
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 ());
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. */
3412 proceed_resume_thread_checked (thread_info
*tp
)
3414 if (!tp
->inf
->has_execution ())
3416 infrun_debug_printf ("[%s] target has no execution",
3417 tp
->ptid
.to_string ().c_str ());
3423 infrun_debug_printf ("[%s] resumed",
3424 tp
->ptid
.to_string ().c_str ());
3425 gdb_assert (tp
->executing () || tp
->has_pending_waitstatus ());
3429 if (thread_is_in_step_over_chain (tp
))
3431 infrun_debug_printf ("[%s] needs step-over",
3432 tp
->ptid
.to_string ().c_str ());
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)
3445 if (target_is_non_stop_p ())
3447 /* For non-stop targets, regardless of whether GDB is using
3448 all-stop or non-stop mode, threads are controlled
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
3455 if (tp
!= tp
->inf
->thread_waiting_for_vfork_done
)
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 ());
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.
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.
3475 Beyond this there is nothing more that needs to be done
3477 gdb_assert (tp
== tp
->inf
->thread_waiting_for_vfork_done
);
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)
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
);
3495 infrun_debug_printf ("resuming %s",
3496 tp
->ptid
.to_string ().c_str ());
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."));
3505 /* Basic routine for continuing the program in various fashions.
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.
3511 You should call clear_proceed_status before calling proceed. */
3514 proceed (CORE_ADDR addr
, enum gdb_signal siggnal
)
3516 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
3518 struct regcache
*regcache
;
3519 struct gdbarch
*gdbarch
;
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 ())
3529 /* The target for some reason decided not to resume. */
3531 if (target_can_async_p ())
3532 inferior_event_handler (INF_EXEC_COMPLETE
);
3536 /* We'll update this if & when we switch to a new thread. */
3537 update_previous_thread ();
3539 regcache
= get_current_regcache ();
3540 gdbarch
= regcache
->arch ();
3541 const address_space
*aspace
= regcache
->aspace ();
3543 pc
= regcache_read_pc_protected (regcache
);
3545 thread_info
*cur_thr
= inferior_thread ();
3547 infrun_debug_printf ("cur_thr = %s", cur_thr
->ptid
.to_string ().c_str ());
3549 /* Fill in with reasonable starting values. */
3550 init_thread_stepping_state (cur_thr
);
3552 gdb_assert (!thread_is_in_step_over_chain (cur_thr
));
3555 = user_visible_resume_ptid (cur_thr
->control
.stepping_command
);
3556 process_stratum_target
*resume_target
3557 = user_visible_resume_target (resume_ptid
);
3559 check_multi_target_resumption (resume_target
);
3561 if (addr
== (CORE_ADDR
) -1)
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
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;
3585 regcache_write_pc (regcache
, addr
);
3588 if (siggnal
!= GDB_SIGNAL_DEFAULT
)
3589 cur_thr
->set_stop_signal (siggnal
);
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
);
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);
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 ());
3610 annotate_starting ();
3612 /* Make sure that output from GDB appears before output from the
3614 gdb_flush (gdb_stdout
);
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 ();
3622 /* In a multi-threaded task we may select another thread and
3623 then continue or step.
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.
3630 Look for threads other than the current (TP) that reported a
3631 breakpoint hit and haven't been resumed yet since. */
3633 /* If scheduler locking applies, we can avoid iterating over all
3635 if (!non_stop
&& !schedlock_applies (cur_thr
))
3637 for (thread_info
*tp
: all_non_exited_threads (resume_target
,
3640 switch_to_thread_no_regs (tp
);
3642 /* Ignore the current thread here. It's handled
3647 if (!thread_still_needs_step_over (tp
))
3650 gdb_assert (!thread_is_in_step_over_chain (tp
));
3652 infrun_debug_printf ("need to step-over [%s] first",
3653 tp
->ptid
.to_string ().c_str ());
3655 global_thread_step_over_chain_enqueue (tp
);
3658 switch_to_thread (cur_thr
);
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
);
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
);
3674 scoped_disable_commit_resumed
disable_commit_resumed ("proceeding");
3675 bool step_over_started
= start_step_over ();
3677 if (step_over_info_valid_p ())
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. */
3683 else if (step_over_started
&& !target_is_non_stop_p ())
3685 /* A new displaced stepping sequence was started. In all-stop,
3686 we can't talk to the target anymore until it next stops. */
3688 else if (!non_stop
&& target_is_non_stop_p ())
3690 INFRUN_SCOPED_DEBUG_START_END
3691 ("resuming threads, all-stop-on-top-of-non-stop");
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
,
3698 switch_to_thread_no_regs (tp
);
3699 proceed_resume_thread_checked (tp
);
3703 proceed_resume_thread_checked (cur_thr
);
3705 disable_commit_resumed
.reset_and_commit ();
3708 finish_state
.release ();
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
3713 switch_to_thread (cur_thr
);
3715 /* Tell the event loop to wait for it to stop. If the target
3716 supports asynchronous execution, it'll do this from within
3718 if (!target_can_async_p ())
3719 mark_async_event_handler (infrun_async_inferior_event_token
);
3723 /* Start remote-debugging of a machine over a serial link. */
3726 start_remote (int from_tty
)
3728 inferior
*inf
= current_inferior ();
3729 inf
->control
.stop_soon
= STOP_QUIETLY_REMOTE
;
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
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
);
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
);
3755 /* Initialize static vars when a new inferior begins. */
3758 init_wait_for_inferior (void)
3760 /* These are meaningless until the first time through wait_for_inferior. */
3762 breakpoint_init_inferior (inf_starting
);
3764 clear_proceed_status (0);
3766 nullify_last_target_wait_ptid ();
3768 update_previous_thread ();
3773 static void handle_inferior_event (struct execution_control_state
*ecs
);
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
*,
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
);
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. */
3794 infrun_thread_stop_requested (ptid_t ptid
)
3796 process_stratum_target
*curr_target
= current_inferior ()->process_target ();
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
))
3804 if (tp
->state
!= THREAD_RUNNING
)
3806 if (tp
->executing ())
3809 /* Remove matching threads from the step-over queue, so
3810 start_step_over doesn't try to resume them
3812 if (thread_is_in_step_over_chain (tp
))
3813 global_thread_step_over_chain_remove (tp
);
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
3819 if (!tp
->has_pending_waitstatus ())
3821 target_waitstatus ws
;
3822 ws
.set_stopped (GDB_SIGNAL_0
);
3823 tp
->set_pending_waitstatus (ws
);
3826 /* Clear the inline-frame state, since we're re-processing the
3828 clear_inline_frame_state (tp
);
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 ())
3837 /* Otherwise we can process the (new) pending event now. Set
3838 it so this pending event is considered by
3840 tp
->set_resumed (true);
3844 /* Delete the step resume, single-step and longjmp/exception resume
3845 breakpoints of TP. */
3848 delete_thread_infrun_breakpoints (struct thread_info
*tp
)
3850 delete_step_resume_breakpoint (tp
);
3851 delete_exception_resume_breakpoint (tp
);
3852 delete_single_step_breakpoints (tp
);
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. */
3859 typedef void (*for_each_just_stopped_thread_callback_func
)
3860 (struct thread_info
*tp
);
3863 for_each_just_stopped_thread (for_each_just_stopped_thread_callback_func func
)
3865 if (!target_has_execution () || inferior_ptid
== null_ptid
)
3868 if (target_is_non_stop_p ())
3870 /* If in non-stop mode, only the current thread stopped. */
3871 func (inferior_thread ());
3875 /* In all-stop mode, all threads have stopped. */
3876 for (thread_info
*tp
: all_non_exited_threads ())
3881 /* Delete the step resume and longjmp/exception resume breakpoints of
3882 the threads that just stopped. */
3885 delete_just_stopped_threads_infrun_breakpoints (void)
3887 for_each_just_stopped_thread (delete_thread_infrun_breakpoints
);
3890 /* Delete the single-step breakpoints of the threads that just
3894 delete_just_stopped_threads_single_step_breakpoints (void)
3896 for_each_just_stopped_thread (delete_single_step_breakpoints
);
3902 print_target_wait_results (ptid_t waiton_ptid
, ptid_t result_ptid
,
3903 const struct target_waitstatus
&ws
)
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 ());
3914 /* Select a thread at random, out of those which are resumed and have
3917 static struct thread_info
*
3918 random_pending_event_thread (inferior
*inf
, ptid_t waiton_ptid
)
3920 process_stratum_target
*proc_target
= inf
->process_target ();
3922 = proc_target
->random_resumed_with_pending_wait_status (inf
, waiton_ptid
);
3924 if (thread
== nullptr)
3926 infrun_debug_printf ("None found.");
3930 infrun_debug_printf ("Found %s.", thread
->ptid
.to_string ().c_str ());
3931 gdb_assert (thread
->resumed ());
3932 gdb_assert (thread
->has_pending_waitstatus ());
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
3943 do_target_wait_1 (inferior
*inf
, ptid_t ptid
,
3944 target_waitstatus
*status
, target_wait_flags options
)
3946 struct thread_info
*tp
;
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
);
3954 /* First check if there is a resumed thread with a wait status
3956 if (ptid
== minus_one_ptid
|| ptid
.is_pid ())
3958 tp
= random_pending_event_thread (inf
, ptid
);
3962 infrun_debug_printf ("Waiting for specific thread %s.",
3963 ptid
.to_string ().c_str ());
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 ())
3973 && (tp
->stop_reason () == TARGET_STOPPED_BY_SW_BREAKPOINT
3974 || tp
->stop_reason () == TARGET_STOPPED_BY_HW_BREAKPOINT
))
3976 struct regcache
*regcache
= get_thread_regcache (tp
);
3977 struct gdbarch
*gdbarch
= regcache
->arch ();
3981 pc
= regcache_read_pc (regcache
);
3983 if (pc
!= tp
->stop_pc ())
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
));
3991 else if (!breakpoint_inserted_here_p (regcache
->aspace (), pc
))
3993 infrun_debug_printf ("previous breakpoint of %s, at %s gone",
3994 tp
->ptid
.to_string ().c_str (),
3995 paddress (gdbarch
, pc
));
4002 infrun_debug_printf ("pending event of %s cancelled.",
4003 tp
->ptid
.to_string ().c_str ());
4005 tp
->clear_pending_waitstatus ();
4006 target_waitstatus ws
;
4008 tp
->set_pending_waitstatus (ws
);
4009 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
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 ());
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 ())
4025 struct regcache
*regcache
;
4026 struct gdbarch
*gdbarch
;
4029 regcache
= get_thread_regcache (tp
);
4030 gdbarch
= regcache
->arch ();
4032 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
4037 pc
= regcache_read_pc (regcache
);
4038 regcache_write_pc (regcache
, pc
+ decr_pc
);
4042 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
4043 *status
= tp
->pending_waitstatus ();
4044 tp
->clear_pending_waitstatus ();
4046 /* Wake up the event loop again, until all pending events are
4048 if (target_is_async_p ())
4049 mark_async_event_handler (infrun_async_inferior_event_token
);
4053 /* But if we don't find one, we'll have to wait. */
4055 /* We can't ask a non-async target to do a non-blocking wait, so this will be
4057 if (!target_can_async_p ())
4058 options
&= ~TARGET_WNOHANG
;
4060 return target_wait (ptid
, status
, options
);
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. */
4068 do_target_wait (execution_control_state
*ecs
, target_wait_flags options
)
4070 int num_inferiors
= 0;
4071 int random_selector
;
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. */
4078 auto inferior_matches
= [] (inferior
*inf
)
4080 return inf
->process_target () != nullptr;
4083 /* First see how many matching inferiors we have. */
4084 for (inferior
*inf
: all_inferiors ())
4085 if (inferior_matches (inf
))
4088 if (num_inferiors
== 0)
4090 ecs
->ws
.set_ignore ();
4094 /* Now randomly pick an inferior out of those that matched. */
4095 random_selector
= (int)
4096 ((num_inferiors
* (double) rand ()) / (RAND_MAX
+ 1.0));
4098 if (num_inferiors
> 1)
4099 infrun_debug_printf ("Found %d inferiors, starting at #%d",
4100 num_inferiors
, random_selector
);
4102 /* Select the Nth inferior that matched. */
4104 inferior
*selected
= nullptr;
4106 for (inferior
*inf
: all_inferiors ())
4107 if (inferior_matches (inf
))
4108 if (random_selector
-- == 0)
4114 /* Now poll for events out of each of the matching inferior's
4115 targets, starting from the selected one. */
4117 auto do_wait
= [&] (inferior
*inf
)
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
);
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
;
4129 intrusive_list_iterator
<inferior
> start
4130 = inferior_list
.iterator_to (*selected
);
4132 for (intrusive_list_iterator
<inferior
> it
= start
;
4133 it
!= inferior_list
.end ();
4136 inferior
*inf
= &*it
;
4138 if (inferior_matches (inf
) && do_wait (inf
))
4142 for (intrusive_list_iterator
<inferior
> it
= inferior_list
.begin ();
4146 inferior
*inf
= &*it
;
4148 if (inferior_matches (inf
) && do_wait (inf
))
4152 ecs
->ws
.set_ignore ();
4156 /* An event reported by wait_one. */
4158 struct wait_one_event
4160 /* The target the event came out of. */
4161 process_stratum_target
*target
;
4163 /* The PTID the event was for. */
4166 /* The waitstatus. */
4167 target_waitstatus ws
;
4170 static bool handle_one (const wait_one_event
&event
);
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
4178 prepare_for_detach (void)
4180 struct inferior
*inf
= current_inferior ();
4181 ptid_t pid_ptid
= ptid_t (inf
->pid
);
4182 scoped_restore_current_thread restore_thread
;
4184 scoped_restore restore_detaching
= make_scoped_restore (&inf
->detaching
, true);
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
);
4191 for (thread_info
*tp
: range
)
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
);
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)
4204 infrun_debug_printf ("inline step-over in-process while detaching");
4206 thread_info
*thr
= find_thread_global_id (step_over_info
.thread
);
4207 if (thr
->inf
== inf
)
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 ();
4213 if (target_is_non_stop_p ())
4215 /* Start a new step-over in another thread if there's
4216 one that needs it. */
4219 /* Restart all other threads (except the
4220 previously-stepping thread, since that one is still
4222 if (!step_over_info_valid_p ())
4223 restart_threads (thr
);
4228 if (displaced_step_in_progress (inf
))
4230 infrun_debug_printf ("displaced-stepping in-process while detaching");
4232 /* Stop threads currently displaced stepping, aborting it. */
4234 for (thread_info
*thr
: inf
->non_exited_threads ())
4236 if (thr
->displaced_step_state
.in_progress ())
4238 if (thr
->executing ())
4240 if (!thr
->stop_requested
)
4242 target_stop (thr
->ptid
);
4243 thr
->stop_requested
= true;
4247 thr
->set_resumed (false);
4251 while (displaced_step_in_progress (inf
))
4253 wait_one_event event
;
4255 event
.target
= inf
->process_target ();
4256 event
.ptid
= do_target_wait_1 (inf
, pid_ptid
, &event
.ws
, 0);
4259 print_target_wait_results (pid_ptid
, event
.ptid
, event
.ws
);
4264 /* It's OK to leave some of the threads of INF stopped, since
4265 they'll be detached shortly. */
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. */
4273 stop_all_threads_if_all_stop_mode ()
4275 if (!non_stop
&& exists_non_stop_target ())
4276 stop_all_threads ("presenting stop to user in all-stop");
4279 /* Wait for control to return from inferior to debugger.
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. */
4287 wait_for_inferior (inferior
*inf
)
4289 infrun_debug_printf ("wait_for_inferior ()");
4291 SCOPE_EXIT
{ delete_just_stopped_threads_infrun_breakpoints (); };
4293 /* If an error happens while handling the event, propagate GDB's
4294 knowledge of the executing state to the frontend/user running
4296 scoped_finish_thread_state finish_state
4297 (inf
->process_target (), minus_one_ptid
);
4301 execution_control_state ecs
;
4303 overlay_cache_invalid
= 1;
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 ();
4311 ecs
.ptid
= do_target_wait_1 (inf
, minus_one_ptid
, &ecs
.ws
, 0);
4312 ecs
.target
= inf
->process_target ();
4315 print_target_wait_results (minus_one_ptid
, ecs
.ptid
, ecs
.ws
);
4317 /* Now figure out what to do with the result of the result. */
4318 handle_inferior_event (&ecs
);
4320 if (!ecs
.wait_some_more
)
4324 stop_all_threads_if_all_stop_mode ();
4326 /* No error, don't finish the state yet. */
4327 finish_state
.release ();
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
4341 reinstall_readline_callback_handler_cleanup ()
4343 struct ui
*ui
= current_ui
;
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
4355 if (ui
->command_editing
&& ui
->prompt_state
!= PROMPT_BLOCKED
)
4356 gdb_rl_callback_handler_reinstall ();
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. */
4368 clean_up_just_stopped_threads_fsms (struct execution_control_state
*ecs
)
4370 /* The first clean_up call below assumes the event thread is the current
4372 if (ecs
->event_thread
!= nullptr)
4373 gdb_assert (ecs
->event_thread
== inferior_thread ());
4375 if (ecs
->event_thread
!= nullptr
4376 && ecs
->event_thread
->thread_fsm () != nullptr)
4377 ecs
->event_thread
->thread_fsm ()->clean_up (ecs
->event_thread
);
4381 scoped_restore_current_thread restore_thread
;
4383 for (thread_info
*thr
: all_threads_safe ())
4385 if (thr
->state
== THREAD_EXITED
)
4388 if (thr
== ecs
->event_thread
)
4391 if (thr
->thread_fsm () != nullptr)
4393 switch_to_thread (thr
);
4394 thr
->thread_fsm ()->clean_up (thr
);
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
);
4410 /* Helper for all_uis_check_sync_execution_done that works on the
4414 check_curr_ui_sync_execution_done (void)
4416 struct ui
*ui
= current_ui
;
4418 if (ui
->prompt_state
== PROMPT_NEEDED
4420 && !gdb_in_secondary_prompt_p (ui
))
4422 target_terminal::ours ();
4423 top_level_interpreter ()->on_sync_execution_done ();
4424 ui
->register_file_handler ();
4431 all_uis_check_sync_execution_done (void)
4433 SWITCH_THRU_ALL_UIS ()
4435 check_curr_ui_sync_execution_done ();
4442 all_uis_on_sync_execution_starting (void)
4444 SWITCH_THRU_ALL_UIS ()
4446 if (current_ui
->prompt_state
== PROMPT_NEEDED
)
4447 async_disable_stdin ();
4451 /* A quit_handler callback installed while we're handling inferior
4455 infrun_quit_handler ()
4457 if (target_terminal::is_ours ())
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.
4469 (gdb) foo bar whatever<ctrl-c>
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
4475 It's as-if background event handling was handled by a
4476 separate background thread.
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
4484 if (check_quit_flag ())
4485 target_pass_ctrlc ();
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. */
4499 fetch_inferior_event ()
4501 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
4503 execution_control_state ecs
;
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 (¤t_ui
, main_ui
);
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);
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
4521 scoped_restore restore_quit_handler
4522 = make_scoped_restore (&quit_handler
, infrun_quit_handler
);
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
;
4532 /* End up with readline processing input, if necessary. */
4534 SCOPE_EXIT
{ reinstall_readline_callback_handler_cleanup (); };
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
;
4543 maybe_restore_traceframe
.emplace ();
4544 set_current_traceframe (-1);
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
;
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
4558 target_dcache_invalidate ();
4560 scoped_restore save_exec_dir
4561 = make_scoped_restore (&execution_direction
,
4562 target_execution_direction ());
4564 /* Allow targets to pause their resumed threads while we handle
4566 scoped_disable_commit_resumed
disable_commit_resumed ("handling event");
4568 if (!do_target_wait (&ecs
, TARGET_WNOHANG
))
4570 infrun_debug_printf ("do_target_wait returned no event");
4571 disable_commit_resumed
.reset_and_commit ();
4575 gdb_assert (ecs
.ws
.kind () != TARGET_WAITKIND_IGNORE
);
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
));
4583 switch_to_target_no_thread (ecs
.target
);
4586 print_target_wait_results (minus_one_ptid
, ecs
.ptid
, ecs
.ws
);
4588 /* If an error happens while handling the event, propagate GDB's
4589 knowledge of the executing state to the frontend/user running
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
);
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
);
4601 int stop_id
= get_stop_id ();
4603 /* Now figure out what to do with the result of the result. */
4604 handle_inferior_event (&ecs
);
4606 if (!ecs
.wait_some_more
)
4608 struct inferior
*inf
= find_inferior_ptid (ecs
.target
, ecs
.ptid
);
4609 bool should_stop
= true;
4610 struct thread_info
*thr
= ecs
.event_thread
;
4612 delete_just_stopped_threads_infrun_breakpoints ();
4614 if (thr
!= nullptr && thr
->thread_fsm () != nullptr)
4615 should_stop
= thr
->thread_fsm ()->should_stop (thr
);
4623 bool should_notify_stop
= true;
4624 bool proceeded
= false;
4626 stop_all_threads_if_all_stop_mode ();
4628 clean_up_just_stopped_threads_fsms (&ecs
);
4630 if (stop_id
!= get_stop_id ())
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.
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
);
4642 else if (thr
!= nullptr && thr
->thread_fsm () != nullptr)
4644 = thr
->thread_fsm ()->should_notify_stop ();
4646 if (should_notify_stop
)
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 ();
4655 inferior_event_handler (INF_EXEC_COMPLETE
);
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
4669 && ecs
.ws
.kind () != TARGET_WAITKIND_NO_RESUMED
)
4670 restore_thread
.dont_restore ();
4674 defer_delete_threads
.release ();
4675 defer_bpstat_clear
.release ();
4677 /* No error, don't finish the thread states yet. */
4678 finish_state
.release ();
4680 disable_commit_resumed
.reset_and_commit ();
4682 /* This scope is used to ensure that readline callbacks are
4683 reinstalled here. */
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.
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.
4694 Call this before all_uis_check_sync_execution_done, so that notifications about
4695 removed inferiors appear before the prompt. */
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 ();
4704 && exec_done_display_p
4705 && (inferior_ptid
== null_ptid
4706 || inferior_thread ()->state
!= THREAD_RUNNING
))
4707 gdb_printf (_("completed.\n"));
4713 set_step_info (thread_info
*tp
, frame_info_ptr frame
,
4714 struct symtab_and_line sal
)
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
);
4720 tp
->control
.step_frame_id
= get_frame_id (frame
);
4721 tp
->control
.step_stack_frame_id
= get_stack_frame_id (frame
);
4723 tp
->current_symtab
= sal
.symtab
;
4724 tp
->current_line
= sal
.line
;
4727 ("symtab = %s, line = %d, step_frame_id = %s, step_stack_frame_id = %s",
4728 tp
->current_symtab
!= nullptr ? tp
->current_symtab
->filename
: "<null>",
4730 tp
->control
.step_frame_id
.to_string ().c_str (),
4731 tp
->control
.step_stack_frame_id
.to_string ().c_str ());
4734 /* Clear context switchable stepping state. */
4737 init_thread_stepping_state (struct thread_info
*tss
)
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;
4748 set_last_target_status (process_stratum_target
*target
, ptid_t ptid
,
4749 const target_waitstatus
&status
)
4751 target_last_proc_target
= target
;
4752 target_last_wait_ptid
= ptid
;
4753 target_last_waitstatus
= status
;
4759 get_last_target_status (process_stratum_target
**target
, ptid_t
*ptid
,
4760 target_waitstatus
*status
)
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
;
4773 nullify_last_target_wait_ptid (void)
4775 target_last_proc_target
= nullptr;
4776 target_last_wait_ptid
= minus_one_ptid
;
4777 target_last_waitstatus
= {};
4780 /* Switch thread contexts. */
4783 context_switch (execution_control_state
*ecs
)
4785 if (ecs
->ptid
!= inferior_ptid
4786 && (inferior_ptid
== null_ptid
4787 || ecs
->event_thread
!= inferior_thread ()))
4789 infrun_debug_printf ("Switching context from %s to %s",
4790 inferior_ptid
.to_string ().c_str (),
4791 ecs
->ptid
.to_string ().c_str ());
4794 switch_to_thread (ecs
->event_thread
);
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. */
4803 adjust_pc_after_break (struct thread_info
*thread
,
4804 const target_waitstatus
&ws
)
4806 struct regcache
*regcache
;
4807 struct gdbarch
*gdbarch
;
4808 CORE_ADDR breakpoint_pc
, decr_pc
;
4810 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
4811 we aren't, just return.
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
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.
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. */
4831 if (ws
.kind () != TARGET_WAITKIND_STOPPED
)
4834 if (ws
.sig () != GDB_SIGNAL_TRAP
)
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
4843 B1 0x08000000 : INSN1
4844 B2 0x08000001 : INSN2
4846 PC -> 0x08000003 : INSN4
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.
4853 B1 0x08000000 : INSN1
4854 B2 PC -> 0x08000001 : INSN2
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
4863 if (execution_direction
== EXEC_REVERSE
)
4866 /* If the target can tell whether the thread hit a SW breakpoint,
4867 trust it. Targets that can tell also adjust the PC
4869 if (target_supports_stopped_by_sw_breakpoint ())
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. */
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 ();
4883 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
4887 const address_space
*aspace
= regcache
->aspace ();
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
;
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
4898 /* Check whether there actually is a software breakpoint inserted at
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
)))
4913 gdb::optional
<scoped_restore_tmpl
<int>> restore_operation_disable
;
4915 if (record_full_is_used ())
4916 restore_operation_disable
.emplace
4917 (record_full_gdb_operation_disable_set ());
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.
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
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
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. */
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
);
4945 stepped_in_from (frame_info_ptr frame
, struct frame_id step_frame_id
)
4947 for (frame
= get_prev_frame (frame
);
4949 frame
= get_prev_frame (frame
))
4951 if (get_frame_id (frame
) == step_frame_id
)
4954 if (get_frame_type (frame
) != INLINE_FRAME
)
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
4968 inline_frame_is_marked_for_skip (bool prev_frame
, struct thread_info
*tp
)
4970 frame_info_ptr frame
= get_current_frame ();
4973 frame
= get_prev_frame (frame
);
4975 for (; frame
!= nullptr; frame
= get_prev_frame (frame
))
4977 const char *fn
= nullptr;
4978 symtab_and_line sal
;
4981 if (get_frame_id (frame
) == tp
->control
.step_frame_id
)
4983 if (get_frame_type (frame
) != INLINE_FRAME
)
4986 sal
= find_frame_sal (frame
);
4987 sym
= get_frame_function (frame
);
4990 fn
= sym
->print_name ();
4993 && function_name_is_marked_for_skip (fn
, sal
))
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
5005 handle_stop_requested (struct execution_control_state
*ecs
)
5007 if (ecs
->event_thread
->stop_requested
)
5009 ecs
->ws
.set_stopped (GDB_SIGNAL_0
);
5010 handle_signal_stop (ecs
);
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
5022 handle_syscall_event (struct execution_control_state
*ecs
)
5024 struct regcache
*regcache
;
5027 context_switch (ecs
);
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
));
5033 if (catch_syscall_enabled () > 0
5034 && catching_syscall_number (syscall_number
))
5036 infrun_debug_printf ("syscall number=%d", syscall_number
);
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
);
5043 if (handle_stop_requested (ecs
))
5046 if (bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
5048 /* Catchpoint hit. */
5053 if (handle_stop_requested (ecs
))
5056 /* If no catchpoint triggered for this, then keep going. */
5062 /* Lazily fill in the execution_control_state's stop_func_* fields. */
5065 fill_in_stop_func (struct gdbarch
*gdbarch
,
5066 struct execution_control_state
*ecs
)
5068 if (!ecs
->stop_func_filled_in
)
5071 const general_symbol_info
*gsi
;
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 (),
5077 &ecs
->stop_func_start
,
5078 &ecs
->stop_func_end
,
5080 ecs
->stop_func_name
= gsi
== nullptr ? nullptr : gsi
->print_name ();
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
)
5094 ecs
->stop_func_start
5095 += gdbarch_deprecated_function_start_offset (gdbarch
);
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
;
5102 if (gdbarch_skip_entrypoint_p (gdbarch
))
5103 ecs
->stop_func_start
5104 = gdbarch_skip_entrypoint (gdbarch
, ecs
->stop_func_start
);
5107 ecs
->stop_func_filled_in
= 1;
5112 /* Return the STOP_SOON field of the inferior pointed at by ECS. */
5114 static enum stop_kind
5115 get_inferior_stop_soon (execution_control_state
*ecs
)
5117 struct inferior
*inf
= find_inferior_ptid (ecs
->target
, ecs
->ptid
);
5119 gdb_assert (inf
!= nullptr);
5120 return inf
->control
.stop_soon
;
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. */
5127 poll_one_curr_target (struct target_waitstatus
*ws
)
5131 overlay_cache_invalid
= 1;
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 ();
5139 event_ptid
= target_wait (minus_one_ptid
, ws
, TARGET_WNOHANG
);
5142 print_target_wait_results (minus_one_ptid
, event_ptid
, *ws
);
5147 /* Wait for one event out of any target. */
5149 static wait_one_event
5154 for (inferior
*inf
: all_inferiors ())
5156 process_stratum_target
*target
= inf
->process_target ();
5157 if (target
== nullptr
5158 || !target
->is_async_p ()
5159 || !target
->threads_executing
)
5162 switch_to_inferior_no_thread (inf
);
5164 wait_one_event event
;
5165 event
.target
= target
;
5166 event
.ptid
= poll_one_curr_target (&event
.ws
);
5168 if (event
.ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
5170 /* If nothing is resumed, remove the target from the
5172 target_async (false);
5174 else if (event
.ws
.kind () != TARGET_WAITKIND_IGNORE
)
5178 /* Block waiting for some event. */
5185 for (inferior
*inf
: all_inferiors ())
5187 process_stratum_target
*target
= inf
->process_target ();
5188 if (target
== nullptr
5189 || !target
->is_async_p ()
5190 || !target
->threads_executing
)
5193 int fd
= target
->async_wait_fd ();
5194 FD_SET (fd
, &readfds
);
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
)};
5209 int numfds
= interruptible_select (nfds
, &readfds
, 0, nullptr, 0);
5215 perror_with_name ("interruptible_select");
5220 /* Save the thread's event and stop reason to process it later. */
5223 save_waitstatus (struct thread_info
*tp
, const target_waitstatus
&ws
)
5225 infrun_debug_printf ("saving status %s for %s",
5226 ws
.to_string ().c_str (),
5227 tp
->ptid
.to_string ().c_str ());
5229 /* Record for later. */
5230 tp
->set_pending_waitstatus (ws
);
5232 if (ws
.kind () == TARGET_WAITKIND_STOPPED
5233 && ws
.sig () == GDB_SIGNAL_TRAP
)
5235 struct regcache
*regcache
= get_thread_regcache (tp
);
5236 const address_space
*aspace
= regcache
->aspace ();
5237 CORE_ADDR pc
= regcache_read_pc (regcache
);
5239 adjust_pc_after_break (tp
, tp
->pending_waitstatus ());
5241 scoped_restore_current_thread restore_thread
;
5242 switch_to_thread (tp
);
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
);
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. */
5269 mark_non_executing_threads (process_stratum_target
*target
,
5271 const target_waitstatus
&ws
)
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
)
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 ());
5297 mark_ptid
= event_ptid
;
5299 set_executing (target
, mark_ptid
, false);
5301 /* Likewise the resumed flag. */
5302 set_resumed (target
, mark_ptid
, false);
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. */
5314 handle_one (const wait_one_event
&event
)
5317 ("%s %s", event
.ws
.to_string ().c_str (),
5318 event
.ptid
.to_string ().c_str ());
5320 if (event
.ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
5322 /* All resumed threads exited. */
5325 else if (event
.ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
5326 || event
.ws
.kind () == TARGET_WAITKIND_EXITED
5327 || event
.ws
.kind () == TARGET_WAITKIND_SIGNALLED
)
5329 /* One thread/process exited/signalled. */
5331 thread_info
*t
= nullptr;
5333 /* The target may have reported just a pid. If so, try
5334 the first non-exited thread. */
5335 if (event
.ptid
.is_pid ())
5337 int pid
= event
.ptid
.pid ();
5338 inferior
*inf
= find_inferior_pid (event
.target
, pid
);
5339 for (thread_info
*tp
: inf
->non_exited_threads ())
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);
5354 ("using %s", t
->ptid
.to_string ().c_str ());
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. */
5362 && event
.ws
.kind () != TARGET_WAITKIND_THREAD_EXITED
)
5363 t
= add_thread (event
.target
, event
.ptid
);
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
,
5373 save_waitstatus (t
, event
.ws
);
5374 t
->stop_requested
= false;
5379 thread_info
*t
= event
.target
->find_thread (event
.ptid
);
5381 t
= add_thread (event
.target
, event
.ptid
);
5383 t
->stop_requested
= 0;
5384 t
->set_executing (false);
5385 t
->set_resumed (false);
5386 t
->control
.may_range_step
= 0;
5388 /* This may be the first time we see the inferior report
5390 if (t
->inf
->needs_setup
)
5392 switch_to_thread_no_regs (t
);
5396 if (event
.ws
.kind () == TARGET_WAITKIND_STOPPED
5397 && event
.ws
.sig () == GDB_SIGNAL_0
)
5399 /* We caught the event that we intended to catch, so
5400 there's no event to save as pending. */
5402 if (displaced_step_finish (t
, event
.ws
)
5403 == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED
)
5405 /* Add it back to the step-over queue. */
5407 ("displaced-step of %s canceled",
5408 t
->ptid
.to_string ().c_str ());
5410 t
->control
.trap_expected
= 0;
5411 if (!t
->inf
->detaching
)
5412 global_thread_step_over_chain_enqueue (t
);
5417 struct regcache
*regcache
;
5420 ("target_wait %s, saving status for %s",
5421 event
.ws
.to_string ().c_str (),
5422 t
->ptid
.to_string ().c_str ());
5424 /* Record for later. */
5425 save_waitstatus (t
, event
.ws
);
5427 if (displaced_step_finish (t
, event
.ws
)
5428 == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED
)
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
);
5436 regcache
= get_thread_regcache (t
);
5437 t
->set_stop_pc (regcache_read_pc (regcache
));
5439 infrun_debug_printf ("saved stop_pc=%s for %s "
5440 "(currently_stepping=%d)",
5441 paddress (current_inferior ()->arch (),
5443 t
->ptid
.to_string ().c_str (),
5444 currently_stepping (t
));
5454 stop_all_threads (const char *reason
, inferior
*inf
)
5456 /* We may need multiple passes to discover all threads. */
5460 gdb_assert (exists_non_stop_target ());
5462 INFRUN_SCOPED_DEBUG_START_END ("reason=%s, inf=%d", reason
,
5463 inf
!= nullptr ? inf
->num
: -1);
5465 infrun_debug_show_threads ("non-exited threads",
5466 all_non_exited_threads ());
5468 scoped_restore_current_thread restore_thread
;
5470 /* Enable thread events on relevant targets. */
5471 for (auto *target
: all_non_exited_process_targets ())
5473 if (inf
!= nullptr && inf
->process_target () != target
)
5476 switch_to_target_no_thread (target
);
5477 target_thread_events (true);
5482 /* Disable thread events on relevant targets. */
5483 for (auto *target
: all_non_exited_process_targets ())
5485 if (inf
!= nullptr && inf
->process_target () != target
)
5488 switch_to_target_no_thread (target
);
5489 target_thread_events (false);
5492 /* Use debug_prefixed_printf directly to get a meaningful function
5495 debug_prefixed_printf ("infrun", "stop_all_threads", "done");
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
++)
5505 infrun_debug_printf ("pass=%d, iterations=%d", pass
, iterations
);
5508 int waits_needed
= 0;
5510 for (auto *target
: all_non_exited_process_targets ())
5512 if (inf
!= nullptr && inf
->process_target () != target
)
5515 switch_to_target_no_thread (target
);
5516 update_thread_list ();
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 ())
5523 if (inf
!= nullptr && t
->inf
!= inf
)
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'. */
5533 switch_to_thread_no_regs (t
);
5534 if (!target_is_non_stop_p ())
5537 if (t
->executing ())
5539 /* If already stopping, don't request a stop again.
5540 We just haven't seen the notification yet. */
5541 if (!t
->stop_requested
)
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;
5550 infrun_debug_printf (" %s executing, already stopping",
5551 t
->ptid
.to_string ().c_str ());
5554 if (t
->stop_requested
)
5559 infrun_debug_printf (" %s not executing",
5560 t
->ptid
.to_string ().c_str ());
5562 /* The thread may be not executing, but still be
5563 resumed with a pending status to process. */
5564 t
->set_resumed (false);
5568 if (waits_needed
== 0)
5571 /* If we find new threads on the second iteration, restart
5572 over. We want to see two iterations in a row with all
5577 for (int i
= 0; i
< waits_needed
; i
++)
5579 wait_one_event event
= wait_one ();
5580 if (handle_one (event
))
5587 /* Handle a TARGET_WAITKIND_NO_RESUMED event. */
5590 handle_no_resumed (struct execution_control_state
*ecs
)
5592 if (target_can_async_p ())
5594 bool any_sync
= false;
5596 for (ui
*ui
: all_uis ())
5598 if (ui
->prompt_state
== PROMPT_BLOCKED
)
5606 /* There were no unwaited-for children left in the target, but,
5607 we're not synchronously waiting for events either. Just
5610 infrun_debug_printf ("TARGET_WAITKIND_NO_RESUMED (ignoring: bg)");
5611 prepare_to_wait (ecs
);
5616 /* Otherwise, if we were running a synchronous execution command, we
5617 may need to cancel it and give the user back the terminal.
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:
5623 #0 - thread 1 is left stopped
5625 #1 - thread 2 is resumed and hits breakpoint
5626 -> TARGET_WAITKIND_STOPPED
5628 #2 - thread 3 is resumed and exits
5629 this is the last resumed thread, so
5630 -> TARGET_WAITKIND_NO_RESUMED
5632 #3 - gdb processes stop for thread 2 and decides to re-resume
5635 #4 - gdb processes the TARGET_WAITKIND_NO_RESUMED event.
5636 thread 2 is now resumed, so the event should be ignored.
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.
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
5650 inferior
*curr_inf
= current_inferior ();
5652 scoped_restore_current_thread restore_thread
;
5653 update_thread_list ();
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
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;
5670 /* Whether to ignore this TARGET_WAITKIND_NO_RESUMED event, or
5671 whether to report it to the user. */
5672 bool ignore_event
= false;
5674 for (thread_info
*thread
: all_non_exited_threads ())
5676 if (swap_terminal
&& thread
->executing ())
5678 if (thread
->inf
!= curr_inf
)
5680 target_terminal::ours ();
5682 switch_to_thread (thread
);
5683 target_terminal::inferior ();
5685 swap_terminal
= false;
5688 if (!ignore_event
&& thread
->resumed ())
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)");
5697 ignore_event
= true;
5700 if (ignore_event
&& !swap_terminal
)
5706 switch_to_inferior_no_thread (curr_inf
);
5707 prepare_to_wait (ecs
);
5711 /* Go ahead and report the event. */
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
5719 The alternatives are:
5721 1) stop_waiting and return; to really stop and return to the
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
5729 handle_inferior_event (struct execution_control_state
*ecs
)
5731 /* Make sure that all temporary struct value objects that were
5732 created during the handling of the event get deleted at the
5734 scoped_value_mark free_values
;
5736 infrun_debug_printf ("%s", ecs
->ws
.to_string ().c_str ());
5738 if (ecs
->ws
.kind () == TARGET_WAITKIND_IGNORE
)
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.
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
);
5753 if (ecs
->ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
)
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
);
5762 if (ecs
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
5763 && handle_no_resumed (ecs
))
5766 /* Cache the last target/ptid/waitstatus. */
5767 set_last_target_status (ecs
->target
, ecs
->ptid
, ecs
->ws
);
5769 /* Always clear state belonging to the previous time we stopped. */
5770 stop_stack_dummy
= STOP_NONE
;
5772 if (ecs
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
5774 /* No unwaited-for children left. IOW, all resumed children
5776 stop_print_frame
= false;
5781 if (ecs
->ws
.kind () != TARGET_WAITKIND_EXITED
5782 && ecs
->ws
.kind () != TARGET_WAITKIND_SIGNALLED
)
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
);
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;
5794 /* Dependent on valid ECS->EVENT_THREAD. */
5795 adjust_pc_after_break (ecs
->event_thread
, ecs
->ws
);
5797 /* Dependent on the current PC value modified by adjust_pc_after_break. */
5798 reinit_frame_cache ();
5800 breakpoint_retire_moribund ();
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
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
))
5817 struct regcache
*regcache
= get_thread_regcache (ecs
->event_thread
);
5819 if (breakpoint_inserted_here_p (regcache
->aspace (),
5820 regcache_read_pc (regcache
)))
5822 infrun_debug_printf ("Treating signal as SIGTRAP");
5823 ecs
->ws
.set_stopped (GDB_SIGNAL_TRAP
);
5827 mark_non_executing_threads (ecs
->target
, ecs
->ptid
, ecs
->ws
);
5829 switch (ecs
->ws
.kind ())
5831 case TARGET_WAITKIND_LOADED
:
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
5841 stop_kind stop_soon
= get_inferior_stop_soon (ecs
);
5842 if (stop_soon
== NO_STOP_QUIETLY
)
5844 struct regcache
*regcache
;
5846 regcache
= get_thread_regcache (ecs
->event_thread
);
5848 handle_solib_event ();
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
);
5856 if (handle_stop_requested (ecs
))
5859 if (bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
5861 /* A catchpoint triggered. */
5862 process_event_stop_test (ecs
);
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
)
5873 /* Make sure we print "Stopped due to solib-event" in
5875 stop_print_frame
= true;
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
)
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
);
5896 /* But stop if we're attaching or setting up a remote
5898 if (stop_soon
== STOP_QUIETLY_NO_SIGSTOP
5899 || stop_soon
== STOP_QUIETLY_REMOTE
)
5901 infrun_debug_printf ("quietly stopped");
5906 internal_error (_("unhandled stop_soon: %d"), (int) stop_soon
);
5909 case TARGET_WAITKIND_SPURIOUS
:
5910 if (handle_stop_requested (ecs
))
5912 context_switch (ecs
);
5913 resume (GDB_SIGNAL_0
);
5914 prepare_to_wait (ecs
);
5917 case TARGET_WAITKIND_THREAD_CREATED
:
5918 if (handle_stop_requested (ecs
))
5920 context_switch (ecs
);
5921 if (!switch_back_to_stepped_thread (ecs
))
5925 case TARGET_WAITKIND_EXITED
:
5926 case TARGET_WAITKIND_SIGNALLED
:
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
);
5935 switch_to_thread (thr
);
5938 inferior
*inf
= find_inferior_ptid (ecs
->target
, ecs
->ptid
);
5939 switch_to_inferior_no_thread (inf
);
5942 handle_vfork_child_exec_or_exit (0);
5943 target_terminal::ours (); /* Must do this before mourn anyway. */
5945 /* Clearing any previous state of convenience variables. */
5946 clear_exit_convenience_vars ();
5948 if (ecs
->ws
.kind () == TARGET_WAITKIND_EXITED
)
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 ());
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 ();
5959 /* Support the --return-child-result option. */
5960 return_child_result_value
= ecs
->ws
.exit_status ();
5962 interps_notify_exited (ecs
->ws
.exit_status ());
5966 struct gdbarch
*gdbarch
= current_inferior ()->arch ();
5968 if (gdbarch_gdb_signal_to_target_p (gdbarch
))
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
,
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
5985 infrun_debug_printf ("Cannot fill $_exitsignal with the correct "
5989 interps_notify_signal_exited (ecs
->ws
.sig ());
5992 gdb_flush (gdb_stdout
);
5993 target_mourn_inferior (inferior_ptid
);
5994 stop_print_frame
= false;
5998 case TARGET_WAITKIND_FORKED
:
5999 case TARGET_WAITKIND_VFORKED
:
6000 case TARGET_WAITKIND_THREAD_CLONED
:
6002 displaced_step_finish (ecs
->event_thread
, ecs
->ws
);
6004 /* Start a new step-over in another thread if there's one that
6008 context_switch (ecs
);
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
)
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 ());
6029 delete_just_stopped_threads_single_step_breakpoints ();
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
;
6036 ecs
->event_thread
->set_stop_pc
6037 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
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
);
6044 if (handle_stop_requested (ecs
))
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
))
6054 = (ecs
->ws
.kind () != TARGET_WAITKIND_THREAD_CLONED
6055 && follow_fork_mode_string
== follow_fork_mode_child
);
6057 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6059 process_stratum_target
*targ
6060 = ecs
->event_thread
->inf
->process_target ();
6063 if (ecs
->ws
.kind () != TARGET_WAITKIND_THREAD_CLONED
)
6064 should_resume
= follow_fork ();
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 ();
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 ());
6078 /* At this point, the parent is marked running, and the
6079 child is marked stopped. */
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);
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);
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
6096 && target_is_non_stop_p ()))))
6099 switch_to_thread (parent
);
6101 switch_to_thread (child
);
6103 ecs
->event_thread
= inferior_thread ();
6104 ecs
->ptid
= inferior_ptid
;
6109 switch_to_thread (child
);
6111 switch_to_thread (parent
);
6113 ecs
->event_thread
= inferior_thread ();
6114 ecs
->ptid
= inferior_ptid
;
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. */
6125 && parent
->inf
->thread_waiting_for_vfork_done
!= nullptr)
6126 || !switch_back_to_stepped_thread (ecs
))
6133 process_event_stop_test (ecs
);
6136 case TARGET_WAITKIND_VFORK_DONE
:
6137 /* Done with the shared memory region. Re-insert breakpoints in
6138 the parent, and keep going. */
6140 context_switch (ecs
);
6142 handle_vfork_done (ecs
->event_thread
);
6143 gdb_assert (inferior_thread () == ecs
->event_thread
);
6145 if (handle_stop_requested (ecs
))
6148 if (!switch_back_to_stepped_thread (ecs
))
6150 gdb_assert (inferior_thread () == ecs
->event_thread
);
6151 /* This also takes care of reinserting breakpoints in the
6152 previously locked inferior. */
6157 case TARGET_WAITKIND_EXECD
:
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
);
6164 /* Do whatever is necessary to the parent branch of the vfork. */
6165 handle_vfork_child_exec_or_exit (1);
6167 /* This causes the eventpoints and symbol table to be reset.
6168 Must do this now, before trying to determine whether to
6170 follow_exec (inferior_ptid
, ecs
->ws
.execd_pathname ());
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 ();
6177 ecs
->event_thread
->set_stop_pc
6178 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
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
);
6185 if (handle_stop_requested (ecs
))
6188 /* If no catchpoint triggered for this, then keep going. */
6189 if (!bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
6191 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6195 process_event_stop_test (ecs
);
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
);
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
6211 case TARGET_WAITKIND_SYSCALL_RETURN
:
6212 if (handle_syscall_event (ecs
) == 0)
6213 process_event_stop_test (ecs
);
6216 case TARGET_WAITKIND_STOPPED
:
6217 handle_signal_stop (ecs
);
6220 case TARGET_WAITKIND_NO_HISTORY
:
6221 /* Reverse execution: target ran out of history info. */
6223 /* Switch to the stopped thread. */
6224 context_switch (ecs
);
6225 infrun_debug_printf ("stopped");
6227 delete_just_stopped_threads_single_step_breakpoints ();
6228 ecs
->event_thread
->set_stop_pc
6229 (regcache_read_pc (get_thread_regcache (inferior_thread ())));
6231 if (handle_stop_requested (ecs
))
6234 interps_notify_no_history ();
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).
6244 If INF is non-nullptr, only resume threads from INF. */
6247 restart_threads (struct thread_info
*event_thread
, inferior
*inf
)
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);
6253 gdb_assert (!step_over_info_valid_p ());
6255 /* In case the instruction just stepped spawned a new thread. */
6256 update_thread_list ();
6258 for (thread_info
*tp
: all_non_exited_threads ())
6260 if (inf
!= nullptr && tp
->inf
!= inf
)
6263 if (tp
->inf
->detaching
)
6265 infrun_debug_printf ("restart threads: [%s] inferior detaching",
6266 tp
->ptid
.to_string ().c_str ());
6270 switch_to_thread_no_regs (tp
);
6272 if (tp
== event_thread
)
6274 infrun_debug_printf ("restart threads: [%s] is event thread",
6275 tp
->ptid
.to_string ().c_str ());
6279 if (!(tp
->state
== THREAD_RUNNING
|| tp
->control
.in_infcall
))
6281 infrun_debug_printf ("restart threads: [%s] not meant to be running",
6282 tp
->ptid
.to_string ().c_str ());
6288 infrun_debug_printf ("restart threads: [%s] resumed",
6289 tp
->ptid
.to_string ().c_str ());
6290 gdb_assert (tp
->executing () || tp
->has_pending_waitstatus ());
6294 if (thread_is_in_step_over_chain (tp
))
6296 infrun_debug_printf ("restart threads: [%s] needs step-over",
6297 tp
->ptid
.to_string ().c_str ());
6298 gdb_assert (!tp
->resumed ());
6303 if (tp
->has_pending_waitstatus ())
6305 infrun_debug_printf ("restart threads: [%s] has pending status",
6306 tp
->ptid
.to_string ().c_str ());
6307 tp
->set_resumed (true);
6311 gdb_assert (!tp
->stop_requested
);
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
6316 if (thread_still_needs_step_over (tp
))
6318 internal_error ("thread [%s] needs a step-over, but not in "
6319 "step-over queue\n",
6320 tp
->ptid
.to_string ().c_str ());
6323 if (currently_stepping (tp
))
6325 infrun_debug_printf ("restart threads: [%s] was stepping",
6326 tp
->ptid
.to_string ().c_str ());
6327 keep_going_stepped_thread (tp
);
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
);
6340 /* Callback for iterate_over_threads. Find a resumed thread that has
6341 a pending waitstatus. */
6344 resumed_thread_with_pending_status (struct thread_info
*tp
,
6347 return tp
->resumed () && tp
->has_pending_waitstatus ();
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
6357 finish_step_over (struct execution_control_state
*ecs
)
6359 displaced_step_finish (ecs
->event_thread
, ecs
->ws
);
6361 bool had_step_over_info
= step_over_info_valid_p ();
6363 if (had_step_over_info
)
6365 /* If we're stepping over a breakpoint with all threads locked,
6366 then only the thread that was stepped should be reporting
6368 gdb_assert (ecs
->event_thread
->control
.trap_expected
);
6370 update_thread_events_after_step_over (ecs
->event_thread
);
6372 clear_step_over_info ();
6375 if (!target_is_non_stop_p ())
6378 /* Start a new step-over in another thread if there's one that
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 ())
6389 struct thread_info
*pending
;
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 ();
6401 restart_threads (ecs
->event_thread
);
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. */
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
)
6416 pending
= iterate_over_threads (resumed_thread_with_pending_status
,
6418 if (pending
!= nullptr)
6420 struct thread_info
*tp
= ecs
->event_thread
;
6421 struct regcache
*regcache
;
6423 infrun_debug_printf ("found resumed threads with "
6424 "pending events, saving status");
6426 gdb_assert (pending
!= tp
);
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
6433 tp
->set_resumed (true);
6435 gdb_assert (!tp
->executing ());
6437 regcache
= get_thread_regcache (tp
);
6438 tp
->set_stop_pc (regcache_read_pc (regcache
));
6440 infrun_debug_printf ("saved stop_pc=%s for %s "
6441 "(currently_stepping=%d)",
6442 paddress (current_inferior ()->arch (),
6444 tp
->ptid
.to_string ().c_str (),
6445 currently_stepping (tp
));
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;
6452 /* Wake up the event loop again. */
6453 mark_async_event_handler (infrun_async_inferior_event_token
);
6455 prepare_to_wait (ecs
);
6466 notify_signal_received (gdb_signal sig
)
6468 interps_notify_signal_received (sig
);
6469 gdb::observers::signal_received
.notify (sig
);
6475 notify_normal_stop (bpstat
*bs
, int print_frame
)
6477 interps_notify_normal_stop (bs
, print_frame
);
6478 gdb::observers::normal_stop
.notify (bs
, print_frame
);
6483 void notify_user_selected_context_changed (user_selected_what selection
)
6485 interps_notify_user_selected_context_changed (selection
);
6486 gdb::observers::user_selected_context_changed
.notify (selection
);
6489 /* Come here when the program has stopped with a signal. */
6492 handle_signal_stop (struct execution_control_state
*ecs
)
6494 frame_info_ptr frame
;
6495 struct gdbarch
*gdbarch
;
6496 int stopped_by_watchpoint
;
6497 enum stop_kind stop_soon
;
6500 gdb_assert (ecs
->ws
.kind () == TARGET_WAITKIND_STOPPED
);
6502 ecs
->event_thread
->set_stop_signal (ecs
->ws
.sig ());
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
))
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
);
6517 ecs
->event_thread
->set_stop_pc
6518 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
6520 context_switch (ecs
);
6522 if (deprecated_context_hook
)
6523 deprecated_context_hook (ecs
->event_thread
->global_num
);
6527 struct regcache
*regcache
= get_thread_regcache (ecs
->event_thread
);
6528 struct gdbarch
*reg_gdbarch
= regcache
->arch ();
6531 ("stop_pc=%s", paddress (reg_gdbarch
, ecs
->event_thread
->stop_pc ()));
6532 if (target_stopped_by_watchpoint ())
6536 infrun_debug_printf ("stopped by watchpoint");
6538 if (target_stopped_data_address (current_inferior ()->top_target (),
6540 infrun_debug_printf ("stopped data address=%s",
6541 paddress (reg_gdbarch
, addr
));
6543 infrun_debug_printf ("(no data address available)");
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
)
6552 infrun_debug_printf ("quietly stopped");
6553 stop_print_frame
= true;
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.
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.
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
))
6583 stop_print_frame
= true;
6585 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6589 /* At this point, get hold of the now-current thread's frame. */
6590 frame
= get_current_frame ();
6591 gdbarch
= get_frame_arch (frame
);
6593 /* Pull the single step breakpoints out of the target. */
6594 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
)
6596 struct regcache
*regcache
;
6599 regcache
= get_thread_regcache (ecs
->event_thread
);
6600 const address_space
*aspace
= regcache
->aspace ();
6602 pc
= regcache_read_pc (regcache
);
6604 /* However, before doing so, if this single-step breakpoint was
6605 actually for another thread, set this thread up for moving
6607 if (!thread_has_single_step_breakpoint_here (ecs
->event_thread
,
6610 if (single_step_breakpoint_inserted_here_p (aspace
, pc
))
6612 infrun_debug_printf ("[%s] hit another thread's single-step "
6614 ecs
->ptid
.to_string ().c_str ());
6615 ecs
->hit_singlestep_breakpoint
= 1;
6620 infrun_debug_printf ("[%s] hit its single-step breakpoint",
6621 ecs
->ptid
.to_string ().c_str ());
6624 delete_just_stopped_threads_single_step_breakpoints ();
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;
6631 stopped_by_watchpoint
= watchpoints_triggered (ecs
->ws
);
6633 /* If necessary, step over this watchpoint. We'll be back to display
6635 if (stopped_by_watchpoint
6636 && (target_have_steppable_watchpoint ()
6637 || gdbarch_have_nonsteppable_watchpoint (gdbarch
)))
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.
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
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.
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.
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
6666 ecs
->event_thread
->stepping_over_watchpoint
= 1;
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;
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)
6684 const address_space
*aspace
6685 = get_thread_regcache (ecs
->event_thread
)->aspace ();
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 (),
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
,
6711 stop_chain
= build_bpstat_chain (aspace
,
6712 ecs
->event_thread
->stop_pc (),
6714 skip_inline_frames (ecs
->event_thread
, stop_chain
);
6716 /* Re-fetch current thread's frame in case that invalidated
6718 frame
= get_current_frame ();
6719 gdbarch
= get_frame_arch (frame
);
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
))
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
);
6736 if (step_through_delay
)
6737 infrun_debug_printf ("step through delay");
6739 if (ecs
->event_thread
->control
.step_range_end
== 0
6740 && step_through_delay
)
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;
6748 else if (step_through_delay
)
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;
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
);
6767 /* Following in case break condition called a
6769 stop_print_frame
= true;
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
6779 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6780 && !bpstat_explains_signal (ecs
->event_thread
->control
.stop_bpstat
,
6782 && stopped_by_watchpoint
)
6784 infrun_debug_printf ("no user watchpoint explains watchpoint SIGTRAP, "
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:
6793 ``End of a stack dummy. Some systems (e.g. Sony news) give
6794 another signal besides SIGTRAP, so check here as well as
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.
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
6808 /* See if the breakpoints module can explain the signal. */
6810 = !bpstat_explains_signal (ecs
->event_thread
->control
.stop_bpstat
,
6811 ecs
->event_thread
->stop_signal ());
6813 /* Maybe this was a trap for a software breakpoint that has since
6815 if (random_signal
&& target_stopped_by_sw_breakpoint ())
6817 if (gdbarch_program_breakpoint_here_p (gdbarch
,
6818 ecs
->event_thread
->stop_pc ()))
6820 struct regcache
*regcache
;
6823 /* Re-adjust PC to what the program would see if GDB was not
6825 regcache
= get_thread_regcache (ecs
->event_thread
);
6826 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
6829 gdb::optional
<scoped_restore_tmpl
<int>>
6830 restore_operation_disable
;
6832 if (record_full_is_used ())
6833 restore_operation_disable
.emplace
6834 (record_full_gdb_operation_disable_set ());
6836 regcache_write_pc (regcache
,
6837 ecs
->event_thread
->stop_pc () + decr_pc
);
6842 /* A delayed software breakpoint event. Ignore the trap. */
6843 infrun_debug_printf ("delayed software breakpoint trap, ignoring");
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 ())
6852 /* A delayed hardware breakpoint event. Ignore the trap. */
6853 infrun_debug_printf ("delayed hardware breakpoint/watchpoint "
6858 /* If not, perhaps stepping/nexting can. */
6860 random_signal
= !(ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6861 && currently_stepping (ecs
->event_thread
));
6863 /* Perhaps the thread hit a single-step breakpoint of _another_
6864 thread. Single-step breakpoints are transparent to the
6865 breakpoints module. */
6867 random_signal
= !ecs
->hit_singlestep_breakpoint
;
6869 /* No? Perhaps we got a moribund watchpoint. */
6871 random_signal
= !stopped_by_watchpoint
;
6873 /* Always stop if the user explicitly requested this thread to
6875 if (ecs
->event_thread
->stop_requested
)
6878 infrun_debug_printf ("user-requested stop");
6881 /* For the program's own signals, act according to
6882 the signal handling tables. */
6886 /* Signal not for debugging purposes. */
6887 enum gdb_signal stop_signal
= ecs
->event_thread
->stop_signal ();
6889 infrun_debug_printf ("random signal (%s)",
6890 gdb_signal_to_symbol_string (stop_signal
));
6892 stopped_by_random_signal
= 1;
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 ()))
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 ()])
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 ();
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
);
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)
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
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
6934 infrun_debug_printf ("signal arrived while stepping over breakpoint");
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;
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
))
6948 if (ecs
->event_thread
->stop_signal () != GDB_SIGNAL_0
6949 && (pc_in_thread_step_range (ecs
->event_thread
->stop_pc (),
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)
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
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");
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;
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. */
6983 if (!switch_back_to_stepped_thread (ecs
))
6985 infrun_debug_printf ("random signal, keep going");
6992 process_event_stop_test (ecs
);
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. */
7002 process_event_stop_test (struct execution_control_state
*ecs
)
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
;
7010 /* Handle cases caused by hitting a breakpoint. */
7012 frame
= get_current_frame ();
7013 gdbarch
= get_frame_arch (frame
);
7015 what
= bpstat_what (ecs
->event_thread
->control
.stop_bpstat
);
7017 if (what
.call_dummy
)
7019 stop_stack_dummy
= what
.call_dummy
;
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
);
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
);
7032 switch (what
.main_action
)
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
7039 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME");
7041 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7043 if (what
.is_longjmp
)
7045 struct value
*arg_value
;
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);
7053 jmp_buf_pc
= value_as_address (arg_value
);
7054 jmp_buf_pc
= gdbarch_addr_bits_remove (gdbarch
, jmp_buf_pc
);
7056 else if (!gdbarch_get_longjmp_target_p (gdbarch
)
7057 || !gdbarch_get_longjmp_target (gdbarch
,
7058 frame
, &jmp_buf_pc
))
7060 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME "
7061 "(!gdbarch_get_longjmp_target)");
7066 /* Insert a breakpoint at resume address. */
7067 insert_longjmp_resume_breakpoint (gdbarch
, jmp_buf_pc
);
7070 check_exception_resume (ecs
, frame
);
7074 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
:
7076 frame_info_ptr init_frame
;
7078 /* There are several cases to consider.
7080 1. The initiating frame no longer exists. In this case we
7081 must stop, because the exception or longjmp has gone too
7084 2. The initiating frame exists, and is the same as the
7085 current frame. We stop, because the exception or longjmp
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.
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. */
7096 infrun_debug_printf ("BPSTAT_WHAT_CLEAR_LONGJMP_RESUME");
7098 gdb_assert (ecs
->event_thread
->control
.exception_resume_breakpoint
7100 delete_exception_resume_breakpoint (ecs
->event_thread
);
7102 if (what
.is_longjmp
)
7104 check_longjmp_breakpoint_for_call_dummy (ecs
->event_thread
);
7106 if (!frame_id_p (ecs
->event_thread
->initiating_frame
))
7114 init_frame
= frame_find_by_id (ecs
->event_thread
->initiating_frame
);
7118 struct frame_id current_id
7119 = get_frame_id (get_current_frame ());
7120 if (current_id
== ecs
->event_thread
->initiating_frame
)
7122 /* Case 2. Fall through. */
7132 /* For Cases 1 and 2, remove the step-resume breakpoint, if it
7134 delete_step_resume_breakpoint (ecs
->event_thread
);
7136 end_stepping_range (ecs
);
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. */
7147 case BPSTAT_WHAT_STEP_RESUME
:
7148 infrun_debug_printf ("BPSTAT_WHAT_STEP_RESUME");
7150 delete_step_resume_breakpoint (ecs
->event_thread
);
7151 if (ecs
->event_thread
->control
.proceed_to_finish
7152 && execution_direction
== EXEC_REVERSE
)
7154 struct thread_info
*tp
= ecs
->event_thread
;
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
7161 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
7165 fill_in_stop_func (gdbarch
, ecs
);
7166 if (ecs
->event_thread
->stop_pc () == ecs
->stop_func_start
7167 && execution_direction
== EXEC_REVERSE
)
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;
7179 case BPSTAT_WHAT_STOP_NOISY
:
7180 infrun_debug_printf ("BPSTAT_WHAT_STOP_NOISY");
7181 stop_print_frame
= true;
7183 /* Assume the thread stopped for a breakpoint. We'll still check
7184 whether a/the breakpoint is there when the thread is next
7186 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7191 case BPSTAT_WHAT_STOP_SILENT
:
7192 infrun_debug_printf ("BPSTAT_WHAT_STOP_SILENT");
7193 stop_print_frame
= false;
7195 /* Assume the thread stopped for a breakpoint. We'll still check
7196 whether a/the breakpoint is there when the thread is next
7198 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7202 case BPSTAT_WHAT_HP_STEP_RESUME
:
7203 infrun_debug_printf ("BPSTAT_WHAT_HP_STEP_RESUME");
7205 delete_step_resume_breakpoint (ecs
->event_thread
);
7206 if (ecs
->event_thread
->step_after_step_resume_breakpoint
)
7208 /* Back when the step-resume breakpoint was inserted, we
7209 were trying to single-step off a breakpoint. Go back to
7211 ecs
->event_thread
->step_after_step_resume_breakpoint
= 0;
7212 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7218 case BPSTAT_WHAT_KEEP_CHECKING
:
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
)
7230 struct breakpoint
*sr_bp
7231 = ecs
->event_thread
->control
.step_resume_breakpoint
;
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
)
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;
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
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
))
7254 if (ecs
->event_thread
->control
.step_resume_breakpoint
)
7256 infrun_debug_printf ("step-resume breakpoint is inserted");
7258 /* Having a step-resume breakpoint overrides anything
7259 else having to do with stepping commands until
7260 that breakpoint is reached. */
7265 if (ecs
->event_thread
->control
.step_range_end
== 0)
7267 infrun_debug_printf ("no stepping, continue");
7268 /* Likewise if we aren't even stepping. */
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
);
7280 /* If stepping through a line, keep going if still within it.
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
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. */
7290 if (pc_in_thread_step_range (ecs
->event_thread
->stop_pc (),
7292 && (execution_direction
!= EXEC_REVERSE
7293 || get_frame_id (frame
) == ecs
->event_thread
->control
.step_frame_id
))
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
));
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;
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
);
7319 /* We stepped out of the stepping range. */
7321 /* If we are stepping at the source level and entered the runtime
7322 loader dynamic symbol resolution code...
7324 EXEC_FORWARD: we keep on single stepping until we exit the run
7325 time loader code and reach the callee's address.
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. */
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 ()
7341 CORE_ADDR pc_after_resolver
=
7342 gdbarch_skip_solib_resolver (gdbarch
, ecs
->event_thread
->stop_pc ());
7344 infrun_debug_printf ("stepped into dynsym resolve code");
7346 if (pc_after_resolver
)
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
);
7354 insert_step_resume_breakpoint_at_sal (gdbarch
,
7355 sr_sal
, null_frame_id
);
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 ()))
7367 infrun_debug_printf ("stepped into indirect branch thunk");
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
)
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
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
)
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
);
7402 infrun_debug_printf ("stepped into solib return tramp");
7404 /* Only proceed through if we know where it's going. */
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
);
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
7416 insert_step_resume_breakpoint_at_sal (gdbarch
,
7417 sr_sal
, null_frame_id
);
7419 /* Restart without fiddling with the step ranges or
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.
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==
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
7462 || (ecs
->event_thread
->control
.step_start_function
7463 != find_pc_function (ecs
->event_thread
->stop_pc ())))))
7465 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7466 CORE_ADDR real_stop_pc
;
7468 infrun_debug_printf ("stepped into subroutine");
7470 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_NONE
)
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
);
7480 /* Reverse stepping through solib trampolines. */
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
))))
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
7497 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
)
7499 /* We're doing a "next".
7501 Normal (forward) execution: set a breakpoint at the
7502 callee's return address (the address at which the caller
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. */
7510 if (execution_direction
== EXEC_REVERSE
)
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
7517 if (ecs
->stop_func_start
!= stop_pc
&& ecs
->stop_func_start
!= 0)
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
));
7528 insert_step_resume_breakpoint_at_caller (frame
);
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
;
7545 if (real_stop_pc
!= 0 && in_solib_dynsym_resolve_code (real_stop_pc
))
7547 symtab_and_line sr_sal
;
7548 sr_sal
.pc
= ecs
->stop_func_start
;
7549 sr_sal
.pspace
= get_frame_program_space (frame
);
7551 insert_step_resume_breakpoint_at_sal (gdbarch
,
7552 sr_sal
, null_frame_id
);
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
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. */
7565 struct symtab_and_line tmp_sal
;
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
,
7571 && !inline_frame_is_marked_for_skip (true, ecs
->event_thread
))
7573 if (execution_direction
== EXEC_REVERSE
)
7574 handle_step_into_function_backward (gdbarch
, ecs
);
7576 handle_step_into_function (gdbarch
, ecs
);
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
)
7587 end_stepping_range (ecs
);
7591 if (execution_direction
== EXEC_REVERSE
)
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
)
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
);
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
);
7618 /* Reverse stepping through solib trampolines. */
7620 if (execution_direction
== EXEC_REVERSE
7621 && ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
)
7623 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7625 if (gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
)
7626 || (ecs
->stop_func_start
== 0
7627 && in_solib_dynsym_resolve_code (stop_pc
)))
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
7637 else if (in_solib_dynsym_resolve_code (stop_pc
))
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
);
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);
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)
7665 infrun_debug_printf ("stepped into undebuggable function");
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
)))
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
);
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
);
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
)
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;
7712 if (ecs
->event_thread
->control
.step_range_end
== 1)
7714 /* It is stepi or nexti. We always want to stop stepping after
7716 infrun_debug_printf ("stepi/nexti");
7717 end_stepping_range (ecs
);
7721 if (stop_pc_sal
.line
== 0)
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
);
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. */
7736 if ((get_frame_id (get_current_frame ())
7737 == ecs
->event_thread
->control
.step_frame_id
)
7738 && inline_skipped_frames (ecs
->event_thread
))
7740 infrun_debug_printf ("stepped into inlined function");
7742 symtab_and_line call_sal
= find_frame_sal (get_current_frame ());
7744 if (ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_ALL
)
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. */
7751 if (call_sal
.line
== ecs
->event_thread
->current_line
7752 && call_sal
.symtab
== ecs
->event_thread
->current_symtab
)
7754 step_into_inline_frame (ecs
->event_thread
);
7755 if (inline_frame_is_marked_for_skip (false, ecs
->event_thread
))
7762 end_stepping_range (ecs
);
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
)
7774 end_stepping_range (ecs
);
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. */
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
))
7790 infrun_debug_printf ("stepping through inlined function");
7792 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
7793 || inline_frame_is_marked_for_skip (false, ecs
->event_thread
))
7796 end_stepping_range (ecs
);
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
))
7805 /* We are at a different line. */
7807 if (stop_pc_sal
.is_stmt
)
7809 /* We are at the start of a statement.
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
7814 infrun_debug_printf ("stepped to a different line");
7815 end_stepping_range (ecs
);
7818 else if (get_frame_id (get_current_frame ())
7819 == ecs
->event_thread
->control
.step_frame_id
)
7821 /* We are not at the start of a statement, and we have not changed
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");
7832 /* We are not the start of a statement, and we have changed frame.
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");
7845 /* We aren't done stepping.
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.)
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
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;
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
);
7870 infrun_debug_printf ("keep going");
7874 static bool restart_stepped_thread (process_stratum_target
*resume_target
,
7875 ptid_t resume_ptid
);
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). */
7883 switch_back_to_stepped_thread (struct execution_control_state
*ecs
)
7885 if (!target_is_non_stop_p ())
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. */
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)
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
)
7904 ("need to finish step-over of [%s]",
7905 ecs
->event_thread
->ptid
.to_string ().c_str ());
7910 /* Check if the current thread is blocked by a single-step
7911 breakpoint of another thread. */
7912 if (ecs
->hit_singlestep_breakpoint
)
7914 infrun_debug_printf ("need to step [%s] over single-step breakpoint",
7915 ecs
->ptid
.to_string ().c_str ());
7920 /* If this thread needs yet another step-over (e.g., stepping
7921 through a delay slot), do it first before moving on to
7923 if (thread_still_needs_step_over (ecs
->event_thread
))
7926 ("thread [%s] still needs step-over",
7927 ecs
->event_thread
->ptid
.to_string ().c_str ());
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
))
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;
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
);
7949 if (restart_stepped_thread (ecs
->target
, ecs
->ptid
))
7951 prepare_to_wait (ecs
);
7955 switch_to_thread (ecs
->event_thread
);
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
7967 restart_stepped_thread (process_stratum_target
*resume_target
,
7970 /* Do all pending step-overs before actually proceeding with
7972 if (start_step_over ())
7975 for (thread_info
*tp
: all_threads_safe ())
7977 if (tp
->state
== THREAD_EXITED
)
7980 if (tp
->has_pending_waitstatus ())
7983 /* Ignore threads of processes the caller is not
7986 && (tp
->inf
->process_target () != resume_target
7987 || tp
->inf
->pid
!= resume_ptid
.pid ()))
7990 if (tp
->control
.trap_expected
)
7992 infrun_debug_printf ("switching back to stepped thread (step-over)");
7994 if (keep_going_stepped_thread (tp
))
7999 for (thread_info
*tp
: all_threads_safe ())
8001 if (tp
->state
== THREAD_EXITED
)
8004 if (tp
->has_pending_waitstatus ())
8007 /* Ignore threads of processes the caller is not
8010 && (tp
->inf
->process_target () != resume_target
8011 || tp
->inf
->pid
!= resume_ptid
.pid ()))
8014 /* Did we find the stepping thread? */
8015 if (tp
->control
.step_range_end
)
8017 infrun_debug_printf ("switching back to stepped thread (stepping)");
8019 if (keep_going_stepped_thread (tp
))
8030 restart_after_all_stop_detach (process_stratum_target
*proc_target
)
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. */
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
))
8044 if (thr
->state
!= THREAD_RUNNING
)
8047 /* If we have any thread that is already executing, then we
8048 don't need to resume the target -- it is already been
8050 if (thr
->executing ())
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 ())
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
))
8064 /* Otherwise, find the first THREAD_RUNNING thread and resume
8066 for (thread_info
*thr
: all_threads (proc_target
))
8068 if (thr
->state
!= THREAD_RUNNING
)
8071 execution_control_state
ecs (thr
);
8072 switch_to_thread (thr
);
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
8083 keep_going_stepped_thread (struct thread_info
*tp
)
8085 frame_info_ptr frame
;
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.
8091 We can find a stepping dead thread in the thread list in two
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.
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. */
8105 if (tp
->state
== THREAD_EXITED
|| !target_thread_alive (tp
->ptid
))
8107 infrun_debug_printf ("not resuming previously stepped thread, it has "
8114 infrun_debug_printf ("resuming previously stepped thread");
8116 execution_control_state
ecs (tp
);
8117 switch_to_thread (tp
);
8119 tp
->set_stop_pc (regcache_read_pc (get_thread_regcache (tp
)));
8120 frame
= get_current_frame ();
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:
8128 - setting a break at the current PC
8129 - resuming that particular thread, only (by setting trap
8132 This prevents us continuously moving the single-step breakpoint
8133 forward, one instruction at a time, overstepping. */
8135 if (tp
->stop_pc () != tp
->prev_pc
)
8139 infrun_debug_printf ("expected thread advanced also (%s -> %s)",
8140 paddress (current_inferior ()->arch (), tp
->prev_pc
),
8141 paddress (current_inferior ()->arch (),
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
8151 clear_step_over_info ();
8152 tp
->control
.trap_expected
= 0;
8154 insert_single_step_breakpoint (get_frame_arch (frame
),
8155 get_frame_address_space (frame
),
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
);
8164 infrun_debug_printf ("expected thread still hasn't advanced");
8166 keep_going_pass_signal (&ecs
);
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.) */
8177 currently_stepping (struct thread_info
*tp
)
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 ());
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
8191 handle_step_into_function (struct gdbarch
*gdbarch
,
8192 struct execution_control_state
*ecs
)
8194 fill_in_stop_func (gdbarch
, ecs
);
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
);
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
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
;
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.
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. */
8230 if (gdbarch_adjust_breakpoint_address_p (gdbarch
))
8232 ecs
->stop_func_start
8233 = gdbarch_adjust_breakpoint_address (gdbarch
,
8234 ecs
->stop_func_start
);
8237 if (ecs
->stop_func_start
== ecs
->event_thread
->stop_pc ())
8239 /* We are already there: stop now. */
8240 end_stepping_range (ecs
);
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 ());
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
8254 insert_step_resume_breakpoint_at_sal (gdbarch
, sr_sal
, null_frame_id
);
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
;
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. */
8268 handle_step_into_function_backward (struct gdbarch
*gdbarch
,
8269 struct execution_control_state
*ecs
)
8271 struct compunit_symtab
*cust
;
8272 struct symtab_and_line stop_func_sal
;
8274 fill_in_stop_func (gdbarch
, ecs
);
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
);
8281 stop_func_sal
= find_pc_line (ecs
->event_thread
->stop_pc (), 0);
8283 /* OK, we're just going to keep stepping here. */
8284 if (stop_func_sal
.pc
== ecs
->event_thread
->stop_pc ())
8286 /* We're there already. Just stop stepping now. */
8287 end_stepping_range (ecs
);
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
;
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. */
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
)
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
);
8316 infrun_debug_printf ("inserting step-resume breakpoint at %s",
8317 paddress (gdbarch
, sr_sal
.pc
));
8319 inferior_thread ()->control
.step_resume_breakpoint
8320 = set_momentary_breakpoint (gdbarch
, sr_sal
, sr_id
, sr_type
).release ();
8324 insert_step_resume_breakpoint_at_sal (struct gdbarch
*gdbarch
,
8325 struct symtab_and_line sr_sal
,
8326 struct frame_id sr_id
)
8328 insert_step_resume_breakpoint_at_sal_1 (gdbarch
,
8333 /* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
8334 This is used to skip a potential signal handler.
8336 This is called with the interrupted function's frame. The signal
8337 handler, when it returns, will resume the interrupted function at
8341 insert_hp_step_resume_breakpoint_at_frame (frame_info_ptr return_frame
)
8343 gdb_assert (return_frame
!= nullptr);
8345 struct gdbarch
*gdbarch
= get_frame_arch (return_frame
);
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
);
8352 insert_step_resume_breakpoint_at_sal_1 (gdbarch
, sr_sal
,
8353 get_stack_frame_id (return_frame
),
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).
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
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). */
8372 insert_step_resume_breakpoint_at_caller (frame_info_ptr next_frame
)
8374 /* We shouldn't have gotten here if we don't know where the call site
8376 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame
)));
8378 struct gdbarch
*gdbarch
= frame_unwind_caller_arch (next_frame
);
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
);
8386 insert_step_resume_breakpoint_at_sal (gdbarch
, sr_sal
,
8387 frame_unwind_caller_id (next_frame
));
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. */
8396 insert_longjmp_resume_breakpoint (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
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);
8403 infrun_debug_printf ("inserting longjmp-resume breakpoint at %s",
8404 paddress (gdbarch
, pc
));
8406 inferior_thread ()->control
.exception_resume_breakpoint
=
8407 set_momentary_breakpoint_at_pc (gdbarch
, pc
, bp_longjmp_resume
).release ();
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. */
8417 insert_exception_resume_breakpoint (struct thread_info
*tp
,
8418 const struct block
*b
,
8419 frame_info_ptr frame
,
8424 struct block_symbol vsym
;
8425 struct value
*value
;
8427 struct breakpoint
*bp
;
8429 vsym
= lookup_symbol_search_name (sym
->search_name (),
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 ())
8435 handler
= value_as_address (value
);
8437 infrun_debug_printf ("exception resume at %lx",
8438 (unsigned long) handler
);
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
),
8445 bp_exception_resume
).release ();
8447 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
8450 tp
->control
.exception_resume_breakpoint
= bp
;
8453 catch (const gdb_exception_error
&e
)
8455 /* We want to ignore errors here. */
8459 /* A helper for check_exception_resume that sets an
8460 exception-breakpoint based on a SystemTap probe. */
8463 insert_exception_resume_from_probe (struct thread_info
*tp
,
8464 const struct bound_probe
*probe
,
8465 frame_info_ptr frame
)
8467 struct value
*arg_value
;
8469 struct breakpoint
*bp
;
8471 arg_value
= probe_safe_evaluate_at_pc (frame
, 1);
8475 handler
= value_as_address (arg_value
);
8477 infrun_debug_printf ("exception resume at %s",
8478 paddress (probe
->objfile
->arch (), handler
));
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
;
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. */
8493 check_exception_resume (struct execution_control_state
*ecs
,
8494 frame_info_ptr frame
)
8496 struct bound_probe probe
;
8497 struct symbol
*func
;
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
));
8506 insert_exception_resume_from_probe (ecs
->event_thread
, &probe
, frame
);
8510 func
= get_frame_function (frame
);
8516 const struct block
*b
;
8519 /* The exception breakpoint is a thread-specific breakpoint on
8520 the unwinder's debug hook, declared as:
8522 void _Unwind_DebugHook (void *cfa, void *handler);
8524 The CFA argument indicates the frame to which control is
8525 about to be transferred. HANDLER is the destination PC.
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
8533 b
= func
->value_block ();
8534 for (struct symbol
*sym
: block_iterator_range (b
))
8536 if (!sym
->is_argument ())
8543 insert_exception_resume_breakpoint (ecs
->event_thread
,
8549 catch (const gdb_exception_error
&e
)
8555 stop_waiting (struct execution_control_state
*ecs
)
8557 infrun_debug_printf ("stop_waiting");
8559 /* Let callers know we don't want to wait for the inferior anymore. */
8560 ecs
->wait_some_more
= 0;
8563 /* Like keep_going, but passes the signal to the inferior, even if the
8564 signal is set to nopass. */
8567 keep_going_pass_signal (struct execution_control_state
*ecs
)
8569 gdb_assert (ecs
->event_thread
->ptid
== inferior_ptid
);
8570 gdb_assert (!ecs
->event_thread
->resumed ());
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
));
8576 if (ecs
->event_thread
->control
.trap_expected
)
8578 struct thread_info
*tp
= ecs
->event_thread
;
8580 infrun_debug_printf ("%s has trap_expected set, "
8581 "resuming to collect trap",
8582 tp
->ptid
.to_string ().c_str ());
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
8588 resume (ecs
->event_thread
->stop_signal ());
8590 else if (step_over_info_valid_p ())
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
;
8597 if (ecs
->hit_singlestep_breakpoint
8598 || thread_still_needs_step_over (tp
))
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
);
8606 infrun_debug_printf ("step-over in progress: resume of %s deferred",
8607 tp
->ptid
.to_string ().c_str ());
8611 struct regcache
*regcache
= get_current_regcache ();
8614 step_over_what step_what
;
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
8620 We got our expected trap, but decided we should resume from
8623 We're going to run this baby now!
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. */
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
8635 step_what
= thread_still_needs_step_over (ecs
->event_thread
);
8637 remove_bp
= (ecs
->hit_singlestep_breakpoint
8638 || (step_what
& STEP_OVER_BREAKPOINT
));
8639 remove_wps
= (step_what
& STEP_OVER_WATCHPOINT
);
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. */
8645 && (remove_wps
|| !use_displaced_stepping (ecs
->event_thread
)))
8647 set_step_over_info (regcache
->aspace (),
8648 regcache_read_pc (regcache
), remove_wps
,
8649 ecs
->event_thread
->global_num
);
8651 else if (remove_wps
)
8652 set_step_over_info (nullptr, 0, remove_wps
, -1);
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
8659 if (step_over_info_valid_p () && target_is_non_stop_p ())
8660 stop_all_threads ("starting in-line step-over");
8662 /* Stop stepping if inserting breakpoints fails. */
8665 insert_breakpoints ();
8667 catch (const gdb_exception_error
&e
)
8669 exception_print (gdb_stderr
, e
);
8671 clear_step_over_info ();
8675 ecs
->event_thread
->control
.trap_expected
= (remove_bp
|| remove_wps
);
8677 resume (ecs
->event_thread
->stop_signal ());
8680 prepare_to_wait (ecs
);
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. */
8688 keep_going (struct execution_control_state
*ecs
)
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;
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
);
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. */
8704 prepare_to_wait (struct execution_control_state
*ecs
)
8706 infrun_debug_printf ("prepare_to_wait");
8708 ecs
->wait_some_more
= 1;
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
8714 if (!target_can_async_p ())
8715 mark_infrun_async_event_handler ();
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. */
8722 end_stepping_range (struct execution_control_state
*ecs
)
8724 ecs
->event_thread
->control
.stop_step
= 1;
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.
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. */
8740 print_signal_exited_reason (struct ui_out
*uiout
, enum gdb_signal siggnal
)
8742 annotate_signalled ();
8743 if (uiout
->is_mi_like_p ())
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 ();
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");
8761 print_exited_reason (struct ui_out
*uiout
, int exitstatus
)
8763 struct inferior
*inf
= current_inferior ();
8764 std::string pidstr
= target_pid_to_str (ptid_t (inf
->pid
));
8766 annotate_exited (exitstatus
);
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 ()));
8779 if (uiout
->is_mi_like_p ())
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 ());
8788 print_signal_received_reason (struct ui_out
*uiout
, enum gdb_signal siggnal
)
8790 struct thread_info
*thr
= inferior_thread ();
8792 infrun_debug_printf ("signal = %s", gdb_signal_to_string (siggnal
));
8796 if (uiout
->is_mi_like_p ())
8798 else if (show_thread_that_caused_stop ())
8800 uiout
->text ("\nThread ");
8801 uiout
->field_string ("thread-id", print_thread_id (thr
));
8803 const char *name
= thread_name (thr
);
8804 if (name
!= nullptr)
8806 uiout
->text (" \"");
8807 uiout
->field_string ("name", name
);
8812 uiout
->text ("\nProgram");
8814 if (siggnal
== GDB_SIGNAL_0
&& !uiout
->is_mi_like_p ())
8815 uiout
->text (" stopped");
8818 uiout
->text (" received signal ");
8819 annotate_signal_name ();
8820 if (uiout
->is_mi_like_p ())
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 ();
8826 annotate_signal_string ();
8827 uiout
->field_string ("signal-meaning", gdb_signal_to_string (siggnal
));
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
);
8834 annotate_signal_string_end ();
8836 uiout
->text (".\n");
8840 print_no_history_reason (struct ui_out
*uiout
)
8842 if (uiout
->is_mi_like_p ())
8843 uiout
->field_string ("reason", async_reason_lookup (EXEC_ASYNC_NO_HISTORY
));
8845 uiout
->text ("\nNo more reverse-execution history.\n");
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. */
8854 print_stop_location (const target_waitstatus
&ws
)
8857 enum print_what source_flag
;
8858 int do_frame_printing
= 1;
8859 struct thread_info
*tp
= inferior_thread ();
8861 bpstat_ret
= bpstat_print (tp
->control
.stop_bpstat
, ws
.kind ());
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 ())))
8874 /* Finished step, just print source line. */
8875 source_flag
= SRC_LINE
;
8879 /* Print location and source line. */
8880 source_flag
= SRC_AND_LOC
;
8883 case PRINT_SRC_AND_LOC
:
8884 /* Print location and source line. */
8885 source_flag
= SRC_AND_LOC
;
8887 case PRINT_SRC_ONLY
:
8888 source_flag
= SRC_LINE
;
8891 /* Something bogus. */
8892 source_flag
= SRC_LINE
;
8893 do_frame_printing
= 0;
8896 internal_error (_("Unknown value."));
8899 /* The behavior of this routine with respect to the source
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);
8911 print_stop_event (struct ui_out
*uiout
, bool displays
)
8913 struct target_waitstatus last
;
8914 struct thread_info
*tp
;
8916 get_last_target_status (nullptr, nullptr, &last
);
8919 scoped_restore save_uiout
= make_scoped_restore (¤t_uiout
, uiout
);
8921 print_stop_location (last
);
8923 /* Display the auto-display expressions. */
8928 tp
= inferior_thread ();
8929 if (tp
->thread_fsm () != nullptr
8930 && tp
->thread_fsm ()->finished_p ())
8932 struct return_value_info
*rv
;
8934 rv
= tp
->thread_fsm ()->return_value ();
8936 print_return_value (uiout
, rv
);
8943 maybe_remove_breakpoints (void)
8945 if (!breakpoints_should_be_inserted_now () && target_has_execution ())
8947 if (remove_breakpoints ())
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"));
8957 /* The execution context that just caused a normal stop. */
8963 DISABLE_COPY_AND_ASSIGN (stop_context
);
8965 bool changed () const;
8970 /* The event PTID. */
8974 /* If stopp for a thread event, this is the thread that caused the
8976 thread_info_ref thread
;
8978 /* The inferior that caused the stop. */
8982 /* Initializes a new stop context. If stopped for a thread event, this
8983 takes a strong reference to the thread. */
8985 stop_context::stop_context ()
8987 stop_id
= get_stop_id ();
8988 ptid
= inferior_ptid
;
8989 inf_num
= current_inferior ()->num
;
8991 if (inferior_ptid
!= null_ptid
)
8993 /* Take a strong reference so that the thread can't be deleted
8995 thread
= thread_info_ref::new_reference (inferior_thread ());
8999 /* Return true if the current context no longer matches the saved stop
9003 stop_context::changed () const
9005 if (ptid
!= inferior_ptid
)
9007 if (inf_num
!= current_inferior ()->num
)
9009 if (thread
!= nullptr && thread
->state
!= THREAD_STOPPED
)
9011 if (get_stop_id () != stop_id
)
9021 struct target_waitstatus last
;
9023 get_last_target_status (nullptr, nullptr, &last
);
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. */
9032 ptid_t finish_ptid
= null_ptid
;
9035 finish_ptid
= minus_one_ptid
;
9036 else if (last
.kind () == TARGET_WAITKIND_SIGNALLED
9037 || last
.kind () == TARGET_WAITKIND_EXITED
)
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 ());
9047 else if (last
.kind () != TARGET_WAITKIND_NO_RESUMED
)
9048 finish_ptid
= inferior_ptid
;
9050 gdb::optional
<scoped_finish_thread_state
> maybe_finish_thread_state
;
9051 if (finish_ptid
!= null_ptid
)
9053 maybe_finish_thread_state
.emplace
9054 (user_visible_resume_target (finish_ptid
), finish_ptid
);
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 ();
9067 if (last
.kind () == TARGET_WAITKIND_STOPPED
&& stopped_by_random_signal
)
9068 notify_signal_received (inferior_thread ()->stop_signal ());
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.
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".
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. */
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 ())
9094 SWITCH_THRU_ALL_UIS ()
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 ();
9103 update_previous_thread ();
9106 if (last
.kind () == TARGET_WAITKIND_NO_RESUMED
)
9108 SWITCH_THRU_ALL_UIS ()
9109 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
9111 target_terminal::ours_for_output ();
9112 gdb_printf (_("No unwaited-for children left.\n"));
9116 /* Note: this depends on the update_thread_list call above. */
9117 maybe_remove_breakpoints ();
9119 /* If an auto-display called a function and that got a signal,
9120 delete that auto-display to avoid an infinite recursion. */
9122 if (stopped_by_random_signal
)
9123 disable_current_display ();
9125 SWITCH_THRU_ALL_UIS ()
9127 async_enable_stdin ();
9130 /* Let the user/frontend see the threads as stopped. */
9131 maybe_finish_thread_state
.reset ();
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 ())
9142 if (stop_stack_dummy
== STOP_STACK_DUMMY
)
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 ();
9149 gdb_assert (get_frame_type (frame
) == DUMMY_FRAME
);
9151 /* frame_pop calls reinit_frame_cache as the last thing it
9152 does which means there's now no selected frame. */
9155 select_frame (get_current_frame ());
9157 /* Set the current source location. */
9158 set_current_sal_from_frame (get_current_frame ());
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
;
9167 execute_cmd_pre_hook (stop_command
);
9169 catch (const gdb_exception_error
&ex
)
9171 exception_fprintf (gdb_stderr
, ex
,
9172 "Error while running hook_stop:\n");
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
9180 if (saved_context
.changed ())
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
9189 annotate_stopped ();
9191 if (target_has_execution ())
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
);
9205 signal_stop_state (int signo
)
9207 return signal_stop
[signo
];
9211 signal_print_state (int signo
)
9213 return signal_print
[signo
];
9217 signal_pass_state (int signo
)
9219 return signal_program
[signo
];
9223 signal_cache_update (int signo
)
9227 for (signo
= 0; signo
< (int) GDB_SIGNAL_LAST
; signo
++)
9228 signal_cache_update (signo
);
9233 signal_pass
[signo
] = (signal_stop
[signo
] == 0
9234 && signal_print
[signo
] == 0
9235 && signal_program
[signo
] == 1
9236 && signal_catch
[signo
] == 0);
9240 signal_stop_update (int signo
, int state
)
9242 int ret
= signal_stop
[signo
];
9244 signal_stop
[signo
] = state
;
9245 signal_cache_update (signo
);
9250 signal_print_update (int signo
, int state
)
9252 int ret
= signal_print
[signo
];
9254 signal_print
[signo
] = state
;
9255 signal_cache_update (signo
);
9260 signal_pass_update (int signo
, int state
)
9262 int ret
= signal_program
[signo
];
9264 signal_program
[signo
] = state
;
9265 signal_cache_update (signo
);
9269 /* Update the global 'signal_catch' from INFO and notify the
9273 signal_catch_update (const unsigned int *info
)
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
);
9284 sig_print_header (void)
9286 gdb_printf (_("Signal Stop\tPrint\tPass "
9287 "to program\tDescription\n"));
9291 sig_print_info (enum gdb_signal oursig
)
9293 const char *name
= gdb_signal_to_name (oursig
);
9294 int name_padding
= 13 - strlen (name
);
9296 if (name_padding
<= 0)
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
));
9307 /* Specify how various signals in the inferior should be handled. */
9310 handle_command (const char *args
, int from_tty
)
9312 int digits
, wordlen
;
9313 int sigfirst
, siglast
;
9314 enum gdb_signal oursig
;
9317 if (args
== nullptr)
9319 error_no_arg (_("signal to handle"));
9322 /* Allocate and zero an array of flags for which signals to handle. */
9324 const size_t nsigs
= GDB_SIGNAL_LAST
;
9325 unsigned char sigs
[nsigs
] {};
9327 /* Break the command line up into args. */
9329 gdb_argv
built_argv (args
);
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>. */
9336 for (char *arg
: built_argv
)
9338 wordlen
= strlen (arg
);
9339 for (digits
= 0; isdigit (arg
[digits
]); digits
++)
9343 sigfirst
= siglast
= -1;
9345 if (wordlen
>= 1 && !strncmp (arg
, "all", wordlen
))
9347 /* Apply action to all signals except those used by the
9348 debugger. Silently skip those. */
9351 siglast
= nsigs
- 1;
9353 else if (wordlen
>= 1 && !strncmp (arg
, "stop", wordlen
))
9355 SET_SIGS (nsigs
, sigs
, signal_stop
);
9356 SET_SIGS (nsigs
, sigs
, signal_print
);
9358 else if (wordlen
>= 1 && !strncmp (arg
, "ignore", wordlen
))
9360 UNSET_SIGS (nsigs
, sigs
, signal_program
);
9362 else if (wordlen
>= 2 && !strncmp (arg
, "print", wordlen
))
9364 SET_SIGS (nsigs
, sigs
, signal_print
);
9366 else if (wordlen
>= 2 && !strncmp (arg
, "pass", wordlen
))
9368 SET_SIGS (nsigs
, sigs
, signal_program
);
9370 else if (wordlen
>= 3 && !strncmp (arg
, "nostop", wordlen
))
9372 UNSET_SIGS (nsigs
, sigs
, signal_stop
);
9374 else if (wordlen
>= 3 && !strncmp (arg
, "noignore", wordlen
))
9376 SET_SIGS (nsigs
, sigs
, signal_program
);
9378 else if (wordlen
>= 4 && !strncmp (arg
, "noprint", wordlen
))
9380 UNSET_SIGS (nsigs
, sigs
, signal_print
);
9381 UNSET_SIGS (nsigs
, sigs
, signal_stop
);
9383 else if (wordlen
>= 4 && !strncmp (arg
, "nopass", wordlen
))
9385 UNSET_SIGS (nsigs
, sigs
, signal_program
);
9387 else if (digits
> 0)
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. */
9395 sigfirst
= siglast
= (int)
9396 gdb_signal_from_command (atoi (arg
));
9397 if (arg
[digits
] == '-')
9400 gdb_signal_from_command (atoi (arg
+ digits
+ 1));
9402 if (sigfirst
> siglast
)
9404 /* Bet he didn't figure we'd think of this case... */
9405 std::swap (sigfirst
, siglast
);
9410 oursig
= gdb_signal_from_name (arg
);
9411 if (oursig
!= GDB_SIGNAL_UNKNOWN
)
9413 sigfirst
= siglast
= (int) oursig
;
9417 /* Not a number and not a recognized flag word => complain. */
9418 error (_("Unrecognized or ambiguous flag word: \"%s\"."), arg
);
9422 /* If any signal numbers or symbol names were found, set flags for
9423 which signals to apply actions to. */
9425 for (int signum
= sigfirst
; signum
>= 0 && signum
<= siglast
; signum
++)
9427 switch ((enum gdb_signal
) signum
)
9429 case GDB_SIGNAL_TRAP
:
9430 case GDB_SIGNAL_INT
:
9431 if (!allsigs
&& !sigs
[signum
])
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
)))
9440 gdb_printf (_("Not confirmed, unchanged.\n"));
9444 case GDB_SIGNAL_DEFAULT
:
9445 case GDB_SIGNAL_UNKNOWN
:
9446 /* Make sure that "all" doesn't print these. */
9455 for (int signum
= 0; signum
< nsigs
; signum
++)
9458 signal_cache_update (-1);
9459 target_pass_signals (signal_pass
);
9460 target_program_signals (signal_program
);
9464 /* Show the results. */
9465 sig_print_header ();
9466 for (; signum
< nsigs
; signum
++)
9468 sig_print_info ((enum gdb_signal
) signum
);
9475 /* Complete the "handle" command. */
9478 handle_completer (struct cmd_list_element
*ignore
,
9479 completion_tracker
&tracker
,
9480 const char *text
, const char *word
)
9482 static const char * const keywords
[] =
9496 signal_completer (ignore
, tracker
, text
, word
);
9497 complete_on_enum (tracker
, keywords
, word
, word
);
9501 gdb_signal_from_command (int num
)
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."));
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). */
9515 info_signals_command (const char *signum_exp
, int from_tty
)
9517 enum gdb_signal oursig
;
9519 sig_print_header ();
9523 /* First see if this is a symbol name. */
9524 oursig
= gdb_signal_from_name (signum_exp
);
9525 if (oursig
== GDB_SIGNAL_UNKNOWN
)
9527 /* No, try numeric. */
9529 gdb_signal_from_command (parse_and_eval_long (signum_exp
));
9531 sig_print_info (oursig
);
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))
9543 if (oursig
!= GDB_SIGNAL_UNKNOWN
9544 && oursig
!= GDB_SIGNAL_DEFAULT
&& oursig
!= GDB_SIGNAL_0
)
9545 sig_print_info (oursig
);
9548 gdb_printf (_("\nUse the \"handle\" command "
9549 "to change these tables.\n"));
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.
9557 1. making $_siginfo be an internalvar that creates a new value on
9560 2. making the value of $_siginfo be an lval_computed value. */
9562 /* This function implements the lval_computed support for reading a
9566 siginfo_value_read (struct value
*v
)
9568 LONGEST transferred
;
9570 /* If we can access registers, so can we access $_siginfo. Likewise
9572 validate_registers_access ();
9575 target_read (current_inferior ()->top_target (),
9576 TARGET_OBJECT_SIGNAL_INFO
,
9578 v
->contents_all_raw ().data (),
9580 v
->type ()->length ());
9582 if (transferred
!= v
->type ()->length ())
9583 error (_("Unable to read siginfo"));
9586 /* This function implements the lval_computed support for writing a
9590 siginfo_value_write (struct value
*v
, struct value
*fromval
)
9592 LONGEST transferred
;
9594 /* If we can access registers, so can we access $_siginfo. Likewise
9596 validate_registers_access ();
9598 transferred
= target_write (current_inferior ()->top_target (),
9599 TARGET_OBJECT_SIGNAL_INFO
,
9601 fromval
->contents_all_raw ().data (),
9603 fromval
->type ()->length ());
9605 if (transferred
!= fromval
->type ()->length ())
9606 error (_("Unable to write siginfo"));
9609 static const struct lval_funcs siginfo_value_funcs
=
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. */
9619 static struct value
*
9620 siginfo_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
9623 if (target_has_stack ()
9624 && inferior_ptid
!= null_ptid
9625 && gdbarch_get_siginfo_type_p (gdbarch
))
9627 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9629 return value::allocate_computed (type
, &siginfo_value_funcs
, nullptr);
9632 return value::allocate (builtin_type (gdbarch
)->builtin_void
);
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. */
9642 class infcall_suspend_state
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
))
9652 tp
->save_suspend_to (m_thread_suspend
);
9654 gdb::unique_xmalloc_ptr
<gdb_byte
> siginfo_data
;
9656 if (gdbarch_get_siginfo_type_p (gdbarch
))
9658 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9659 size_t len
= type
->length ();
9661 siginfo_data
.reset ((gdb_byte
*) xmalloc (len
));
9663 if (target_read (current_inferior ()->top_target (),
9664 TARGET_OBJECT_SIGNAL_INFO
, nullptr,
9665 siginfo_data
.get (), 0, len
) != len
)
9667 /* Errors ignored. */
9668 siginfo_data
.reset (nullptr);
9674 m_siginfo_gdbarch
= gdbarch
;
9675 m_siginfo_data
= std::move (siginfo_data
);
9679 /* Return a pointer to the stored register state. */
9681 readonly_detached_regcache
*registers () const
9683 return m_registers
.get ();
9686 /* Restores the stored state into GDBARCH, TP, and REGCACHE. */
9688 void restore (struct gdbarch
*gdbarch
,
9689 struct thread_info
*tp
,
9690 struct regcache
*regcache
) const
9692 tp
->restore_suspend_from (m_thread_suspend
);
9694 if (m_siginfo_gdbarch
== gdbarch
)
9696 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9698 /* Errors ignored. */
9699 target_write (current_inferior ()->top_target (),
9700 TARGET_OBJECT_SIGNAL_INFO
, nullptr,
9701 m_siginfo_data
.get (), 0, type
->length ());
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 ());
9712 /* How the current thread stopped before the inferior function call was
9714 struct thread_suspend_state m_thread_suspend
;
9716 /* The registers before the inferior function call was executed. */
9717 std::unique_ptr
<readonly_detached_regcache
> m_registers
;
9719 /* Format of SIGINFO_DATA or NULL if it is not present. */
9720 struct gdbarch
*m_siginfo_gdbarch
= nullptr;
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
;
9728 infcall_suspend_state_up
9729 save_infcall_suspend_state ()
9731 struct thread_info
*tp
= inferior_thread ();
9732 struct regcache
*regcache
= get_current_regcache ();
9733 struct gdbarch
*gdbarch
= regcache
->arch ();
9735 infcall_suspend_state_up inf_state
9736 (new struct infcall_suspend_state (gdbarch
, tp
, regcache
));
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
);
9747 /* Restore inferior session state to INF_STATE. */
9750 restore_infcall_suspend_state (struct infcall_suspend_state
*inf_state
)
9752 struct thread_info
*tp
= inferior_thread ();
9753 struct regcache
*regcache
= get_current_regcache ();
9754 struct gdbarch
*gdbarch
= regcache
->arch ();
9756 inf_state
->restore (gdbarch
, tp
, regcache
);
9757 discard_infcall_suspend_state (inf_state
);
9761 discard_infcall_suspend_state (struct infcall_suspend_state
*inf_state
)
9766 readonly_detached_regcache
*
9767 get_infcall_suspend_state_regcache (struct infcall_suspend_state
*inf_state
)
9769 return inf_state
->registers ();
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. */
9776 struct infcall_control_state
9778 struct thread_control_state thread_control
;
9779 struct inferior_control_state inferior_control
;
9782 enum stop_stack_kind stop_stack_dummy
= STOP_NONE
;
9783 int stopped_by_random_signal
= 0;
9785 /* ID and level of the selected frame when the inferior function
9787 struct frame_id selected_frame_id
{};
9788 int selected_frame_level
= -1;
9791 /* Save all of the information associated with the inferior<==>gdb
9794 infcall_control_state_up
9795 save_infcall_control_state ()
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 ();
9801 inf_status
->thread_control
= tp
->control
;
9802 inf_status
->inferior_control
= inf
->control
;
9804 tp
->control
.step_resume_breakpoint
= nullptr;
9805 tp
->control
.exception_resume_breakpoint
= nullptr;
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
9811 tp
->control
.stop_bpstat
= bpstat_copy (tp
->control
.stop_bpstat
);
9814 inf_status
->stop_stack_dummy
= stop_stack_dummy
;
9815 inf_status
->stopped_by_random_signal
= stopped_by_random_signal
;
9817 save_selected_frame (&inf_status
->selected_frame_id
,
9818 &inf_status
->selected_frame_level
);
9823 /* Restore inferior session state to INF_STATUS. */
9826 restore_infcall_control_state (struct infcall_control_state
*inf_status
)
9828 struct thread_info
*tp
= inferior_thread ();
9829 struct inferior
*inf
= current_inferior ();
9831 if (tp
->control
.step_resume_breakpoint
)
9832 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
9834 if (tp
->control
.exception_resume_breakpoint
)
9835 tp
->control
.exception_resume_breakpoint
->disposition
9836 = disp_del_at_next_stop
;
9838 /* Handle the bpstat_copy of the chain. */
9839 bpstat_clear (&tp
->control
.stop_bpstat
);
9841 tp
->control
= inf_status
->thread_control
;
9842 inf
->control
= inf_status
->inferior_control
;
9845 stop_stack_dummy
= inf_status
->stop_stack_dummy
;
9846 stopped_by_random_signal
= inf_status
->stopped_by_random_signal
;
9848 if (target_has_stack ())
9850 restore_selected_frame (inf_status
->selected_frame_id
,
9851 inf_status
->selected_frame_level
);
9858 discard_infcall_control_state (struct infcall_control_state
*inf_status
)
9860 if (inf_status
->thread_control
.step_resume_breakpoint
)
9861 inf_status
->thread_control
.step_resume_breakpoint
->disposition
9862 = disp_del_at_next_stop
;
9864 if (inf_status
->thread_control
.exception_resume_breakpoint
)
9865 inf_status
->thread_control
.exception_resume_breakpoint
->disposition
9866 = disp_del_at_next_stop
;
9868 /* See save_infcall_control_state for info on stop_bpstat. */
9869 bpstat_clear (&inf_status
->thread_control
.stop_bpstat
);
9877 clear_exit_convenience_vars (void)
9879 clear_internalvar (lookup_internalvar ("_exitsignal"));
9880 clear_internalvar (lookup_internalvar ("_exitcode"));
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). */
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
[] = {
9899 set_exec_direction_func (const char *args
, int from_tty
,
9900 struct cmd_list_element
*cmd
)
9902 if (target_can_execute_reverse ())
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
;
9911 exec_direction
= exec_forward
;
9912 error (_("Target does not support this operation."));
9917 show_exec_direction_func (struct ui_file
*out
, int from_tty
,
9918 struct cmd_list_element
*cmd
, const char *value
)
9920 switch (execution_direction
) {
9922 gdb_printf (out
, _("Forward.\n"));
9925 gdb_printf (out
, _("Reverse.\n"));
9928 internal_error (_("bogus execution_direction value: %d"),
9929 (int) execution_direction
);
9934 show_schedule_multiple (struct ui_file
*file
, int from_tty
,
9935 struct cmd_list_element
*c
, const char *value
)
9937 gdb_printf (file
, _("Resuming the execution of threads "
9938 "of all processes is %s.\n"), value
);
9941 /* Implementation of `siginfo' variable. */
9943 static const struct internalvar_funcs siginfo_funcs
=
9949 /* Callback for infrun's target events source. This is marked when a
9950 thread has a pending status to process. */
9953 infrun_async_inferior_event_handler (gdb_client_data data
)
9955 clear_async_event_handler (infrun_async_inferior_event_token
);
9956 inferior_event_handler (INF_REG_EVENT
);
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. */
9968 infrun_thread_ptid_changed ()
9970 gdbarch
*arch
= current_inferior ()->arch ();
9972 /* The thread which inferior_ptid represents changes ptid. */
9974 scoped_restore_current_pspace_and_thread restore
;
9976 scoped_mock_context
<test_target_ops
> target1 (arch
);
9977 scoped_mock_context
<test_target_ops
> target2 (arch
);
9979 ptid_t
old_ptid (111, 222);
9980 ptid_t
new_ptid (111, 333);
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
;
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
;
9992 auto restore_inferior_ptid
= make_scoped_restore (&inferior_ptid
, old_ptid
);
9993 set_current_inferior (&target1
.mock_inferior
);
9995 thread_change_ptid (&target1
.mock_target
, old_ptid
, new_ptid
);
9997 gdb_assert (inferior_ptid
== new_ptid
);
10000 /* A thread with the same ptid as inferior_ptid, but from another target,
10003 scoped_restore_current_pspace_and_thread restore
;
10005 scoped_mock_context
<test_target_ops
> target1 (arch
);
10006 scoped_mock_context
<test_target_ops
> target2 (arch
);
10008 ptid_t
old_ptid (111, 222);
10009 ptid_t
new_ptid (111, 333);
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
;
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
;
10021 auto restore_inferior_ptid
= make_scoped_restore (&inferior_ptid
, old_ptid
);
10022 set_current_inferior (&target2
.mock_inferior
);
10024 thread_change_ptid (&target1
.mock_target
, old_ptid
, new_ptid
);
10026 gdb_assert (inferior_ptid
== old_ptid
);
10030 } /* namespace selftests */
10032 #endif /* GDB_SELF_TEST */
10034 void _initialize_infrun ();
10036 _initialize_infrun ()
10038 struct cmd_list_element
*c
;
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,
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);
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\
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\
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\
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
);
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
);
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
);
10090 add_setshow_boolean_cmd ("non-stop", no_class
,
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\
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."),
10110 for (size_t i
= 0; i
< GDB_SIGNAL_LAST
; i
++)
10112 signal_stop
[i
] = 1;
10113 signal_print
[i
] = 1;
10114 signal_program
[i
] = 1;
10115 signal_catch
[i
] = 0;
10118 /* Signals caused by debugger's own actions should not be given to
10119 the program afterwards.
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
10131 signal_program
[GDB_SIGNAL_TRAP
] = 0;
10132 signal_program
[GDB_SIGNAL_INT
] = 0;
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;
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;
10167 /* Update cached state. */
10168 signal_cache_update (-1);
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
);
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."),
10192 show_follow_fork_mode_string
,
10193 &setlist
, &showlist
);
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\
10202 follow-exec-mode can be:\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\
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\
10214 By default, the debugger will use the same inferior."),
10216 show_follow_exec_mode_string
,
10217 &setlist
, &showlist
);
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
);
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)."),
10243 show_schedule_multiple
,
10244 &setlist
, &showlist
);
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."),
10253 show_step_stop_if_no_debug
,
10254 &setlist
, &showlist
);
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)."),
10267 show_can_use_displaced_stepping
,
10268 &setlist
, &showlist
);
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
);
10278 /* Set/show detach-on-fork: user-settable mode. */
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
);
10286 /* Set/show disable address space randomization mode. */
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
);
10299 /* ptid initializations */
10300 inferior_ptid
= null_ptid
;
10301 target_last_wait_ptid
= minus_one_ptid
;
10303 gdb::observers::thread_ptid_changed
.attach (infrun_thread_ptid_changed
,
10305 gdb::observers::thread_stop_requested
.attach (infrun_thread_stop_requested
,
10307 gdb::observers::inferior_exit
.attach (infrun_inferior_exit
, "infrun");
10308 gdb::observers::inferior_execd
.attach (infrun_inferior_execd
, "infrun");
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);
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\
10325 show_observer_mode
,
10330 selftests::register_test ("infrun_thread_ptid_changed",
10331 selftests::infrun_thread_ptid_changed
);