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} events in
2520 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.
2527 - If we are stepping over a breakpoint out of line (displaced
2528 stepping) then we won't remove a breakpoint from the target,
2529 but, if the step spawns a new clone thread, then we will need
2530 to fixup the $pc address in the clone child too, so we need it
2533 if (step_over_info_valid_p ()
2534 || displaced_step_in_progress_thread (tp
))
2536 gdb_thread_options options
= GDB_THREAD_OPTION_CLONE
;
2537 if (target_supports_set_thread_options (options
))
2538 tp
->set_thread_options (options
);
2540 target_thread_events (true);
2543 /* If we're resuming more than one thread simultaneously, then any
2544 thread other than the leader is being set to run free. Clear any
2545 previous thread option for those threads. */
2546 if (resume_ptid
!= inferior_ptid
&& target_supports_set_thread_options (0))
2548 process_stratum_target
*resume_target
= tp
->inf
->process_target ();
2549 for (thread_info
*thr_iter
: all_non_exited_threads (resume_target
,
2552 thr_iter
->set_thread_options (0);
2555 infrun_debug_printf ("resume_ptid=%s, step=%d, sig=%s",
2556 resume_ptid
.to_string ().c_str (),
2557 step
, gdb_signal_to_symbol_string (sig
));
2559 target_resume (resume_ptid
, step
, sig
);
2562 /* Resume the inferior. SIG is the signal to give the inferior
2563 (GDB_SIGNAL_0 for none). Note: don't call this directly; instead
2564 call 'resume', which handles exceptions. */
2567 resume_1 (enum gdb_signal sig
)
2569 struct regcache
*regcache
= get_current_regcache ();
2570 struct gdbarch
*gdbarch
= regcache
->arch ();
2571 struct thread_info
*tp
= inferior_thread ();
2572 const address_space
*aspace
= regcache
->aspace ();
2574 /* This represents the user's step vs continue request. When
2575 deciding whether "set scheduler-locking step" applies, it's the
2576 user's intention that counts. */
2577 const int user_step
= tp
->control
.stepping_command
;
2578 /* This represents what we'll actually request the target to do.
2579 This can decay from a step to a continue, if e.g., we need to
2580 implement single-stepping with breakpoints (software
2584 gdb_assert (!tp
->stop_requested
);
2585 gdb_assert (!thread_is_in_step_over_chain (tp
));
2587 if (tp
->has_pending_waitstatus ())
2590 ("thread %s has pending wait "
2591 "status %s (currently_stepping=%d).",
2592 tp
->ptid
.to_string ().c_str (),
2593 tp
->pending_waitstatus ().to_string ().c_str (),
2594 currently_stepping (tp
));
2596 tp
->inf
->process_target ()->threads_executing
= true;
2597 tp
->set_resumed (true);
2599 /* FIXME: What should we do if we are supposed to resume this
2600 thread with a signal? Maybe we should maintain a queue of
2601 pending signals to deliver. */
2602 if (sig
!= GDB_SIGNAL_0
)
2604 warning (_("Couldn't deliver signal %s to %s."),
2605 gdb_signal_to_name (sig
),
2606 tp
->ptid
.to_string ().c_str ());
2609 tp
->set_stop_signal (GDB_SIGNAL_0
);
2611 if (target_can_async_p ())
2613 target_async (true);
2614 /* Tell the event loop we have an event to process. */
2615 mark_async_event_handler (infrun_async_inferior_event_token
);
2620 tp
->stepped_breakpoint
= 0;
2622 /* Depends on stepped_breakpoint. */
2623 step
= currently_stepping (tp
);
2625 if (current_inferior ()->thread_waiting_for_vfork_done
!= nullptr)
2627 /* Don't try to single-step a vfork parent that is waiting for
2628 the child to get out of the shared memory region (by exec'ing
2629 or exiting). This is particularly important on software
2630 single-step archs, as the child process would trip on the
2631 software single step breakpoint inserted for the parent
2632 process. Since the parent will not actually execute any
2633 instruction until the child is out of the shared region (such
2634 are vfork's semantics), it is safe to simply continue it.
2635 Eventually, we'll see a TARGET_WAITKIND_VFORK_DONE event for
2636 the parent, and tell it to `keep_going', which automatically
2637 re-sets it stepping. */
2638 infrun_debug_printf ("resume : clear step");
2642 CORE_ADDR pc
= regcache_read_pc (regcache
);
2644 infrun_debug_printf ("step=%d, signal=%s, trap_expected=%d, "
2645 "current thread [%s] at %s",
2646 step
, gdb_signal_to_symbol_string (sig
),
2647 tp
->control
.trap_expected
,
2648 inferior_ptid
.to_string ().c_str (),
2649 paddress (gdbarch
, pc
));
2651 /* Normally, by the time we reach `resume', the breakpoints are either
2652 removed or inserted, as appropriate. The exception is if we're sitting
2653 at a permanent breakpoint; we need to step over it, but permanent
2654 breakpoints can't be removed. So we have to test for it here. */
2655 if (breakpoint_here_p (aspace
, pc
) == permanent_breakpoint_here
)
2657 if (sig
!= GDB_SIGNAL_0
)
2659 /* We have a signal to pass to the inferior. The resume
2660 may, or may not take us to the signal handler. If this
2661 is a step, we'll need to stop in the signal handler, if
2662 there's one, (if the target supports stepping into
2663 handlers), or in the next mainline instruction, if
2664 there's no handler. If this is a continue, we need to be
2665 sure to run the handler with all breakpoints inserted.
2666 In all cases, set a breakpoint at the current address
2667 (where the handler returns to), and once that breakpoint
2668 is hit, resume skipping the permanent breakpoint. If
2669 that breakpoint isn't hit, then we've stepped into the
2670 signal handler (or hit some other event). We'll delete
2671 the step-resume breakpoint then. */
2673 infrun_debug_printf ("resume: skipping permanent breakpoint, "
2674 "deliver signal first");
2676 clear_step_over_info ();
2677 tp
->control
.trap_expected
= 0;
2679 if (tp
->control
.step_resume_breakpoint
== nullptr)
2681 /* Set a "high-priority" step-resume, as we don't want
2682 user breakpoints at PC to trigger (again) when this
2684 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2685 gdb_assert (tp
->control
.step_resume_breakpoint
->first_loc ()
2688 tp
->step_after_step_resume_breakpoint
= step
;
2691 insert_breakpoints ();
2695 /* There's no signal to pass, we can go ahead and skip the
2696 permanent breakpoint manually. */
2697 infrun_debug_printf ("skipping permanent breakpoint");
2698 gdbarch_skip_permanent_breakpoint (gdbarch
, regcache
);
2699 /* Update pc to reflect the new address from which we will
2700 execute instructions. */
2701 pc
= regcache_read_pc (regcache
);
2705 /* We've already advanced the PC, so the stepping part
2706 is done. Now we need to arrange for a trap to be
2707 reported to handle_inferior_event. Set a breakpoint
2708 at the current PC, and run to it. Don't update
2709 prev_pc, because if we end in
2710 switch_back_to_stepped_thread, we want the "expected
2711 thread advanced also" branch to be taken. IOW, we
2712 don't want this thread to step further from PC
2714 gdb_assert (!step_over_info_valid_p ());
2715 insert_single_step_breakpoint (gdbarch
, aspace
, pc
);
2716 insert_breakpoints ();
2718 resume_ptid
= internal_resume_ptid (user_step
);
2719 do_target_resume (resume_ptid
, false, GDB_SIGNAL_0
);
2720 tp
->set_resumed (true);
2726 /* If we have a breakpoint to step over, make sure to do a single
2727 step only. Same if we have software watchpoints. */
2728 if (tp
->control
.trap_expected
|| bpstat_should_step ())
2729 tp
->control
.may_range_step
= 0;
2731 /* If displaced stepping is enabled, step over breakpoints by executing a
2732 copy of the instruction at a different address.
2734 We can't use displaced stepping when we have a signal to deliver;
2735 the comments for displaced_step_prepare explain why. The
2736 comments in the handle_inferior event for dealing with 'random
2737 signals' explain what we do instead.
2739 We can't use displaced stepping when we are waiting for vfork_done
2740 event, displaced stepping breaks the vfork child similarly as single
2741 step software breakpoint. */
2742 if (tp
->control
.trap_expected
2743 && use_displaced_stepping (tp
)
2744 && !step_over_info_valid_p ()
2745 && sig
== GDB_SIGNAL_0
2746 && current_inferior ()->thread_waiting_for_vfork_done
== nullptr)
2748 displaced_step_prepare_status prepare_status
2749 = displaced_step_prepare (tp
);
2751 if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
)
2753 infrun_debug_printf ("Got placed in step-over queue");
2755 tp
->control
.trap_expected
= 0;
2758 else if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_CANT
)
2760 /* Fallback to stepping over the breakpoint in-line. */
2762 if (target_is_non_stop_p ())
2763 stop_all_threads ("displaced stepping falling back on inline stepping");
2765 set_step_over_info (regcache
->aspace (),
2766 regcache_read_pc (regcache
), 0, tp
->global_num
);
2768 step
= maybe_software_singlestep (gdbarch
);
2770 insert_breakpoints ();
2772 else if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_OK
)
2774 /* Update pc to reflect the new address from which we will
2775 execute instructions due to displaced stepping. */
2776 pc
= regcache_read_pc (get_thread_regcache (tp
));
2778 step
= gdbarch_displaced_step_hw_singlestep (gdbarch
);
2781 gdb_assert_not_reached ("Invalid displaced_step_prepare_status "
2785 /* Do we need to do it the hard way, w/temp breakpoints? */
2787 step
= maybe_software_singlestep (gdbarch
);
2789 /* Currently, our software single-step implementation leads to different
2790 results than hardware single-stepping in one situation: when stepping
2791 into delivering a signal which has an associated signal handler,
2792 hardware single-step will stop at the first instruction of the handler,
2793 while software single-step will simply skip execution of the handler.
2795 For now, this difference in behavior is accepted since there is no
2796 easy way to actually implement single-stepping into a signal handler
2797 without kernel support.
2799 However, there is one scenario where this difference leads to follow-on
2800 problems: if we're stepping off a breakpoint by removing all breakpoints
2801 and then single-stepping. In this case, the software single-step
2802 behavior means that even if there is a *breakpoint* in the signal
2803 handler, GDB still would not stop.
2805 Fortunately, we can at least fix this particular issue. We detect
2806 here the case where we are about to deliver a signal while software
2807 single-stepping with breakpoints removed. In this situation, we
2808 revert the decisions to remove all breakpoints and insert single-
2809 step breakpoints, and instead we install a step-resume breakpoint
2810 at the current address, deliver the signal without stepping, and
2811 once we arrive back at the step-resume breakpoint, actually step
2812 over the breakpoint we originally wanted to step over. */
2813 if (thread_has_single_step_breakpoints_set (tp
)
2814 && sig
!= GDB_SIGNAL_0
2815 && step_over_info_valid_p ())
2817 /* If we have nested signals or a pending signal is delivered
2818 immediately after a handler returns, might already have
2819 a step-resume breakpoint set on the earlier handler. We cannot
2820 set another step-resume breakpoint; just continue on until the
2821 original breakpoint is hit. */
2822 if (tp
->control
.step_resume_breakpoint
== nullptr)
2824 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2825 tp
->step_after_step_resume_breakpoint
= 1;
2828 delete_single_step_breakpoints (tp
);
2830 clear_step_over_info ();
2831 tp
->control
.trap_expected
= 0;
2833 insert_breakpoints ();
2836 /* If STEP is set, it's a request to use hardware stepping
2837 facilities. But in that case, we should never
2838 use singlestep breakpoint. */
2839 gdb_assert (!(thread_has_single_step_breakpoints_set (tp
) && step
));
2841 /* Decide the set of threads to ask the target to resume. */
2842 if (tp
->control
.trap_expected
)
2844 /* We're allowing a thread to run past a breakpoint it has
2845 hit, either by single-stepping the thread with the breakpoint
2846 removed, or by displaced stepping, with the breakpoint inserted.
2847 In the former case, we need to single-step only this thread,
2848 and keep others stopped, as they can miss this breakpoint if
2849 allowed to run. That's not really a problem for displaced
2850 stepping, but, we still keep other threads stopped, in case
2851 another thread is also stopped for a breakpoint waiting for
2852 its turn in the displaced stepping queue. */
2853 resume_ptid
= inferior_ptid
;
2856 resume_ptid
= internal_resume_ptid (user_step
);
2858 if (execution_direction
!= EXEC_REVERSE
2859 && step
&& breakpoint_inserted_here_p (aspace
, pc
))
2861 /* There are two cases where we currently need to step a
2862 breakpoint instruction when we have a signal to deliver:
2864 - See handle_signal_stop where we handle random signals that
2865 could take out us out of the stepping range. Normally, in
2866 that case we end up continuing (instead of stepping) over the
2867 signal handler with a breakpoint at PC, but there are cases
2868 where we should _always_ single-step, even if we have a
2869 step-resume breakpoint, like when a software watchpoint is
2870 set. Assuming single-stepping and delivering a signal at the
2871 same time would takes us to the signal handler, then we could
2872 have removed the breakpoint at PC to step over it. However,
2873 some hardware step targets (like e.g., Mac OS) can't step
2874 into signal handlers, and for those, we need to leave the
2875 breakpoint at PC inserted, as otherwise if the handler
2876 recurses and executes PC again, it'll miss the breakpoint.
2877 So we leave the breakpoint inserted anyway, but we need to
2878 record that we tried to step a breakpoint instruction, so
2879 that adjust_pc_after_break doesn't end up confused.
2881 - In non-stop if we insert a breakpoint (e.g., a step-resume)
2882 in one thread after another thread that was stepping had been
2883 momentarily paused for a step-over. When we re-resume the
2884 stepping thread, it may be resumed from that address with a
2885 breakpoint that hasn't trapped yet. Seen with
2886 gdb.threads/non-stop-fair-events.exp, on targets that don't
2887 do displaced stepping. */
2889 infrun_debug_printf ("resume: [%s] stepped breakpoint",
2890 tp
->ptid
.to_string ().c_str ());
2892 tp
->stepped_breakpoint
= 1;
2894 /* Most targets can step a breakpoint instruction, thus
2895 executing it normally. But if this one cannot, just
2896 continue and we will hit it anyway. */
2897 if (gdbarch_cannot_step_breakpoint (gdbarch
))
2901 if (tp
->control
.may_range_step
)
2903 /* If we're resuming a thread with the PC out of the step
2904 range, then we're doing some nested/finer run control
2905 operation, like stepping the thread out of the dynamic
2906 linker or the displaced stepping scratch pad. We
2907 shouldn't have allowed a range step then. */
2908 gdb_assert (pc_in_thread_step_range (pc
, tp
));
2911 do_target_resume (resume_ptid
, step
, sig
);
2912 tp
->set_resumed (true);
2915 /* Resume the inferior. SIG is the signal to give the inferior
2916 (GDB_SIGNAL_0 for none). This is a wrapper around 'resume_1' that
2917 rolls back state on error. */
2920 resume (gdb_signal sig
)
2926 catch (const gdb_exception
&ex
)
2928 /* If resuming is being aborted for any reason, delete any
2929 single-step breakpoint resume_1 may have created, to avoid
2930 confusing the following resumption, and to avoid leaving
2931 single-step breakpoints perturbing other threads, in case
2932 we're running in non-stop mode. */
2933 if (inferior_ptid
!= null_ptid
)
2934 delete_single_step_breakpoints (inferior_thread ());
2944 /* Counter that tracks number of user visible stops. This can be used
2945 to tell whether a command has proceeded the inferior past the
2946 current location. This allows e.g., inferior function calls in
2947 breakpoint commands to not interrupt the command list. When the
2948 call finishes successfully, the inferior is standing at the same
2949 breakpoint as if nothing happened (and so we don't call
2951 static ULONGEST current_stop_id
;
2958 return current_stop_id
;
2961 /* Called when we report a user visible stop. */
2969 /* Clear out all variables saying what to do when inferior is continued.
2970 First do this, then set the ones you want, then call `proceed'. */
2973 clear_proceed_status_thread (struct thread_info
*tp
)
2975 infrun_debug_printf ("%s", tp
->ptid
.to_string ().c_str ());
2977 /* If we're starting a new sequence, then the previous finished
2978 single-step is no longer relevant. */
2979 if (tp
->has_pending_waitstatus ())
2981 if (tp
->stop_reason () == TARGET_STOPPED_BY_SINGLE_STEP
)
2983 infrun_debug_printf ("pending event of %s was a finished step. "
2985 tp
->ptid
.to_string ().c_str ());
2987 tp
->clear_pending_waitstatus ();
2988 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
2993 ("thread %s has pending wait status %s (currently_stepping=%d).",
2994 tp
->ptid
.to_string ().c_str (),
2995 tp
->pending_waitstatus ().to_string ().c_str (),
2996 currently_stepping (tp
));
3000 /* If this signal should not be seen by program, give it zero.
3001 Used for debugging signals. */
3002 if (!signal_pass_state (tp
->stop_signal ()))
3003 tp
->set_stop_signal (GDB_SIGNAL_0
);
3005 tp
->release_thread_fsm ();
3007 tp
->control
.trap_expected
= 0;
3008 tp
->control
.step_range_start
= 0;
3009 tp
->control
.step_range_end
= 0;
3010 tp
->control
.may_range_step
= 0;
3011 tp
->control
.step_frame_id
= null_frame_id
;
3012 tp
->control
.step_stack_frame_id
= null_frame_id
;
3013 tp
->control
.step_over_calls
= STEP_OVER_UNDEBUGGABLE
;
3014 tp
->control
.step_start_function
= nullptr;
3015 tp
->stop_requested
= 0;
3017 tp
->control
.stop_step
= 0;
3019 tp
->control
.proceed_to_finish
= 0;
3021 tp
->control
.stepping_command
= 0;
3023 /* Discard any remaining commands or status from previous stop. */
3024 bpstat_clear (&tp
->control
.stop_bpstat
);
3027 /* Notify the current interpreter and observers that the target is about to
3031 notify_about_to_proceed ()
3033 top_level_interpreter ()->on_about_to_proceed ();
3034 gdb::observers::about_to_proceed
.notify ();
3038 clear_proceed_status (int step
)
3040 /* With scheduler-locking replay, stop replaying other threads if we're
3041 not replaying the user-visible resume ptid.
3043 This is a convenience feature to not require the user to explicitly
3044 stop replaying the other threads. We're assuming that the user's
3045 intent is to resume tracing the recorded process. */
3046 if (!non_stop
&& scheduler_mode
== schedlock_replay
3047 && target_record_is_replaying (minus_one_ptid
)
3048 && !target_record_will_replay (user_visible_resume_ptid (step
),
3049 execution_direction
))
3050 target_record_stop_replaying ();
3052 if (!non_stop
&& inferior_ptid
!= null_ptid
)
3054 ptid_t resume_ptid
= user_visible_resume_ptid (step
);
3055 process_stratum_target
*resume_target
3056 = user_visible_resume_target (resume_ptid
);
3058 /* In all-stop mode, delete the per-thread status of all threads
3059 we're about to resume, implicitly and explicitly. */
3060 for (thread_info
*tp
: all_non_exited_threads (resume_target
, resume_ptid
))
3061 clear_proceed_status_thread (tp
);
3064 if (inferior_ptid
!= null_ptid
)
3066 struct inferior
*inferior
;
3070 /* If in non-stop mode, only delete the per-thread status of
3071 the current thread. */
3072 clear_proceed_status_thread (inferior_thread ());
3075 inferior
= current_inferior ();
3076 inferior
->control
.stop_soon
= NO_STOP_QUIETLY
;
3079 notify_about_to_proceed ();
3082 /* Returns true if TP is still stopped at a breakpoint that needs
3083 stepping-over in order to make progress. If the breakpoint is gone
3084 meanwhile, we can skip the whole step-over dance. */
3087 thread_still_needs_step_over_bp (struct thread_info
*tp
)
3089 if (tp
->stepping_over_breakpoint
)
3091 struct regcache
*regcache
= get_thread_regcache (tp
);
3093 if (breakpoint_here_p (regcache
->aspace (),
3094 regcache_read_pc (regcache
))
3095 == ordinary_breakpoint_here
)
3098 tp
->stepping_over_breakpoint
= 0;
3104 /* Check whether thread TP still needs to start a step-over in order
3105 to make progress when resumed. Returns an bitwise or of enum
3106 step_over_what bits, indicating what needs to be stepped over. */
3108 static step_over_what
3109 thread_still_needs_step_over (struct thread_info
*tp
)
3111 step_over_what what
= 0;
3113 if (thread_still_needs_step_over_bp (tp
))
3114 what
|= STEP_OVER_BREAKPOINT
;
3116 if (tp
->stepping_over_watchpoint
3117 && !target_have_steppable_watchpoint ())
3118 what
|= STEP_OVER_WATCHPOINT
;
3123 /* Returns true if scheduler locking applies. STEP indicates whether
3124 we're about to do a step/next-like command to a thread. */
3127 schedlock_applies (struct thread_info
*tp
)
3129 return (scheduler_mode
== schedlock_on
3130 || (scheduler_mode
== schedlock_step
3131 && tp
->control
.stepping_command
)
3132 || (scheduler_mode
== schedlock_replay
3133 && target_record_will_replay (minus_one_ptid
,
3134 execution_direction
)));
3137 /* Set process_stratum_target::COMMIT_RESUMED_STATE in all target
3138 stacks that have threads executing and don't have threads with
3142 maybe_set_commit_resumed_all_targets ()
3144 scoped_restore_current_thread restore_thread
;
3146 for (inferior
*inf
: all_non_exited_inferiors ())
3148 process_stratum_target
*proc_target
= inf
->process_target ();
3150 if (proc_target
->commit_resumed_state
)
3152 /* We already set this in a previous iteration, via another
3153 inferior sharing the process_stratum target. */
3157 /* If the target has no resumed threads, it would be useless to
3158 ask it to commit the resumed threads. */
3159 if (!proc_target
->threads_executing
)
3161 infrun_debug_printf ("not requesting commit-resumed for target "
3162 "%s, no resumed threads",
3163 proc_target
->shortname ());
3167 /* As an optimization, if a thread from this target has some
3168 status to report, handle it before requiring the target to
3169 commit its resumed threads: handling the status might lead to
3170 resuming more threads. */
3171 if (proc_target
->has_resumed_with_pending_wait_status ())
3173 infrun_debug_printf ("not requesting commit-resumed for target %s, a"
3174 " thread has a pending waitstatus",
3175 proc_target
->shortname ());
3179 switch_to_inferior_no_thread (inf
);
3181 if (target_has_pending_events ())
3183 infrun_debug_printf ("not requesting commit-resumed for target %s, "
3184 "target has pending events",
3185 proc_target
->shortname ());
3189 infrun_debug_printf ("enabling commit-resumed for target %s",
3190 proc_target
->shortname ());
3192 proc_target
->commit_resumed_state
= true;
3199 maybe_call_commit_resumed_all_targets ()
3201 scoped_restore_current_thread restore_thread
;
3203 for (inferior
*inf
: all_non_exited_inferiors ())
3205 process_stratum_target
*proc_target
= inf
->process_target ();
3207 if (!proc_target
->commit_resumed_state
)
3210 switch_to_inferior_no_thread (inf
);
3212 infrun_debug_printf ("calling commit_resumed for target %s",
3213 proc_target
->shortname());
3215 target_commit_resumed ();
3219 /* To track nesting of scoped_disable_commit_resumed objects, ensuring
3220 that only the outermost one attempts to re-enable
3222 static bool enable_commit_resumed
= true;
3226 scoped_disable_commit_resumed::scoped_disable_commit_resumed
3227 (const char *reason
)
3228 : m_reason (reason
),
3229 m_prev_enable_commit_resumed (enable_commit_resumed
)
3231 infrun_debug_printf ("reason=%s", m_reason
);
3233 enable_commit_resumed
= false;
3235 for (inferior
*inf
: all_non_exited_inferiors ())
3237 process_stratum_target
*proc_target
= inf
->process_target ();
3239 if (m_prev_enable_commit_resumed
)
3241 /* This is the outermost instance: force all
3242 COMMIT_RESUMED_STATE to false. */
3243 proc_target
->commit_resumed_state
= false;
3247 /* This is not the outermost instance, we expect
3248 COMMIT_RESUMED_STATE to have been cleared by the
3249 outermost instance. */
3250 gdb_assert (!proc_target
->commit_resumed_state
);
3258 scoped_disable_commit_resumed::reset ()
3264 infrun_debug_printf ("reason=%s", m_reason
);
3266 gdb_assert (!enable_commit_resumed
);
3268 enable_commit_resumed
= m_prev_enable_commit_resumed
;
3270 if (m_prev_enable_commit_resumed
)
3272 /* This is the outermost instance, re-enable
3273 COMMIT_RESUMED_STATE on the targets where it's possible. */
3274 maybe_set_commit_resumed_all_targets ();
3278 /* This is not the outermost instance, we expect
3279 COMMIT_RESUMED_STATE to still be false. */
3280 for (inferior
*inf
: all_non_exited_inferiors ())
3282 process_stratum_target
*proc_target
= inf
->process_target ();
3283 gdb_assert (!proc_target
->commit_resumed_state
);
3290 scoped_disable_commit_resumed::~scoped_disable_commit_resumed ()
3298 scoped_disable_commit_resumed::reset_and_commit ()
3301 maybe_call_commit_resumed_all_targets ();
3306 scoped_enable_commit_resumed::scoped_enable_commit_resumed
3307 (const char *reason
)
3308 : m_reason (reason
),
3309 m_prev_enable_commit_resumed (enable_commit_resumed
)
3311 infrun_debug_printf ("reason=%s", m_reason
);
3313 if (!enable_commit_resumed
)
3315 enable_commit_resumed
= true;
3317 /* Re-enable COMMIT_RESUMED_STATE on the targets where it's
3319 maybe_set_commit_resumed_all_targets ();
3321 maybe_call_commit_resumed_all_targets ();
3327 scoped_enable_commit_resumed::~scoped_enable_commit_resumed ()
3329 infrun_debug_printf ("reason=%s", m_reason
);
3331 gdb_assert (enable_commit_resumed
);
3333 enable_commit_resumed
= m_prev_enable_commit_resumed
;
3335 if (!enable_commit_resumed
)
3337 /* Force all COMMIT_RESUMED_STATE back to false. */
3338 for (inferior
*inf
: all_non_exited_inferiors ())
3340 process_stratum_target
*proc_target
= inf
->process_target ();
3341 proc_target
->commit_resumed_state
= false;
3346 /* Check that all the targets we're about to resume are in non-stop
3347 mode. Ideally, we'd only care whether all targets support
3348 target-async, but we're not there yet. E.g., stop_all_threads
3349 doesn't know how to handle all-stop targets. Also, the remote
3350 protocol in all-stop mode is synchronous, irrespective of
3351 target-async, which means that things like a breakpoint re-set
3352 triggered by one target would try to read memory from all targets
3356 check_multi_target_resumption (process_stratum_target
*resume_target
)
3358 if (!non_stop
&& resume_target
== nullptr)
3360 scoped_restore_current_thread restore_thread
;
3362 /* This is used to track whether we're resuming more than one
3364 process_stratum_target
*first_connection
= nullptr;
3366 /* The first inferior we see with a target that does not work in
3367 always-non-stop mode. */
3368 inferior
*first_not_non_stop
= nullptr;
3370 for (inferior
*inf
: all_non_exited_inferiors ())
3372 switch_to_inferior_no_thread (inf
);
3374 if (!target_has_execution ())
3377 process_stratum_target
*proc_target
3378 = current_inferior ()->process_target();
3380 if (!target_is_non_stop_p ())
3381 first_not_non_stop
= inf
;
3383 if (first_connection
== nullptr)
3384 first_connection
= proc_target
;
3385 else if (first_connection
!= proc_target
3386 && first_not_non_stop
!= nullptr)
3388 switch_to_inferior_no_thread (first_not_non_stop
);
3390 proc_target
= current_inferior ()->process_target();
3392 error (_("Connection %d (%s) does not support "
3393 "multi-target resumption."),
3394 proc_target
->connection_number
,
3395 make_target_connection_string (proc_target
).c_str ());
3401 /* Helper function for `proceed`. Check if thread TP is suitable for
3402 resuming, and, if it is, switch to the thread and call
3403 `keep_going_pass_signal`. If TP is not suitable for resuming then this
3404 function will just return without switching threads. */
3407 proceed_resume_thread_checked (thread_info
*tp
)
3409 if (!tp
->inf
->has_execution ())
3411 infrun_debug_printf ("[%s] target has no execution",
3412 tp
->ptid
.to_string ().c_str ());
3418 infrun_debug_printf ("[%s] resumed",
3419 tp
->ptid
.to_string ().c_str ());
3420 gdb_assert (tp
->executing () || tp
->has_pending_waitstatus ());
3424 if (thread_is_in_step_over_chain (tp
))
3426 infrun_debug_printf ("[%s] needs step-over",
3427 tp
->ptid
.to_string ().c_str ());
3431 /* When handling a vfork GDB removes all breakpoints from the program
3432 space in which the vfork is being handled. If we are following the
3433 parent then GDB will set the thread_waiting_for_vfork_done member of
3434 the parent inferior. In this case we should take care to only resume
3435 the vfork parent thread, the kernel will hold this thread suspended
3436 until the vfork child has exited or execd, at which point the parent
3437 will be resumed and a VFORK_DONE event sent to GDB. */
3438 if (tp
->inf
->thread_waiting_for_vfork_done
!= nullptr)
3440 if (target_is_non_stop_p ())
3442 /* For non-stop targets, regardless of whether GDB is using
3443 all-stop or non-stop mode, threads are controlled
3446 When a thread is handling a vfork, breakpoints are removed
3447 from the inferior (well, program space in fact), so it is
3448 critical that we don't try to resume any thread other than the
3450 if (tp
!= tp
->inf
->thread_waiting_for_vfork_done
)
3452 infrun_debug_printf ("[%s] thread %s of this inferior is "
3453 "waiting for vfork-done",
3454 tp
->ptid
.to_string ().c_str (),
3455 tp
->inf
->thread_waiting_for_vfork_done
3456 ->ptid
.to_string ().c_str ());
3462 /* For all-stop targets, when we attempt to resume the inferior,
3463 we will only resume the vfork parent thread, this is handled
3464 in internal_resume_ptid.
3466 Additionally, we will always be called with the vfork parent
3467 thread as the current thread (TP) thanks to follow_fork, as
3468 such the following assertion should hold.
3470 Beyond this there is nothing more that needs to be done
3472 gdb_assert (tp
== tp
->inf
->thread_waiting_for_vfork_done
);
3476 /* When handling a vfork GDB removes all breakpoints from the program
3477 space in which the vfork is being handled. If we are following the
3478 child then GDB will set vfork_child member of the vfork parent
3479 inferior. Once the child has either exited or execd then GDB will
3480 detach from the parent process. Until that point GDB should not
3481 resume any thread in the parent process. */
3482 if (tp
->inf
->vfork_child
!= nullptr)
3484 infrun_debug_printf ("[%s] thread is part of a vfork parent, child is %d",
3485 tp
->ptid
.to_string ().c_str (),
3486 tp
->inf
->vfork_child
->pid
);
3490 infrun_debug_printf ("resuming %s",
3491 tp
->ptid
.to_string ().c_str ());
3493 execution_control_state
ecs (tp
);
3494 switch_to_thread (tp
);
3495 keep_going_pass_signal (&ecs
);
3496 if (!ecs
.wait_some_more
)
3497 error (_("Command aborted."));
3500 /* Basic routine for continuing the program in various fashions.
3502 ADDR is the address to resume at, or -1 for resume where stopped.
3503 SIGGNAL is the signal to give it, or GDB_SIGNAL_0 for none,
3504 or GDB_SIGNAL_DEFAULT for act according to how it stopped.
3506 You should call clear_proceed_status before calling proceed. */
3509 proceed (CORE_ADDR addr
, enum gdb_signal siggnal
)
3511 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
3513 struct regcache
*regcache
;
3514 struct gdbarch
*gdbarch
;
3517 /* If we're stopped at a fork/vfork, switch to either the parent or child
3518 thread as defined by the "set follow-fork-mode" command, or, if both
3519 the parent and child are controlled by GDB, and schedule-multiple is
3520 on, follow the child. If none of the above apply then we just proceed
3521 resuming the current thread. */
3522 if (!follow_fork ())
3524 /* The target for some reason decided not to resume. */
3526 if (target_can_async_p ())
3527 inferior_event_handler (INF_EXEC_COMPLETE
);
3531 /* We'll update this if & when we switch to a new thread. */
3532 update_previous_thread ();
3534 regcache
= get_current_regcache ();
3535 gdbarch
= regcache
->arch ();
3536 const address_space
*aspace
= regcache
->aspace ();
3538 pc
= regcache_read_pc_protected (regcache
);
3540 thread_info
*cur_thr
= inferior_thread ();
3542 infrun_debug_printf ("cur_thr = %s", cur_thr
->ptid
.to_string ().c_str ());
3544 /* Fill in with reasonable starting values. */
3545 init_thread_stepping_state (cur_thr
);
3547 gdb_assert (!thread_is_in_step_over_chain (cur_thr
));
3550 = user_visible_resume_ptid (cur_thr
->control
.stepping_command
);
3551 process_stratum_target
*resume_target
3552 = user_visible_resume_target (resume_ptid
);
3554 check_multi_target_resumption (resume_target
);
3556 if (addr
== (CORE_ADDR
) -1)
3558 if (cur_thr
->stop_pc_p ()
3559 && pc
== cur_thr
->stop_pc ()
3560 && breakpoint_here_p (aspace
, pc
) == ordinary_breakpoint_here
3561 && execution_direction
!= EXEC_REVERSE
)
3562 /* There is a breakpoint at the address we will resume at,
3563 step one instruction before inserting breakpoints so that
3564 we do not stop right away (and report a second hit at this
3567 Note, we don't do this in reverse, because we won't
3568 actually be executing the breakpoint insn anyway.
3569 We'll be (un-)executing the previous instruction. */
3570 cur_thr
->stepping_over_breakpoint
= 1;
3571 else if (gdbarch_single_step_through_delay_p (gdbarch
)
3572 && gdbarch_single_step_through_delay (gdbarch
,
3573 get_current_frame ()))
3574 /* We stepped onto an instruction that needs to be stepped
3575 again before re-inserting the breakpoint, do so. */
3576 cur_thr
->stepping_over_breakpoint
= 1;
3580 regcache_write_pc (regcache
, addr
);
3583 if (siggnal
!= GDB_SIGNAL_DEFAULT
)
3584 cur_thr
->set_stop_signal (siggnal
);
3586 /* If an exception is thrown from this point on, make sure to
3587 propagate GDB's knowledge of the executing state to the
3588 frontend/user running state. */
3589 scoped_finish_thread_state
finish_state (resume_target
, resume_ptid
);
3591 /* Even if RESUME_PTID is a wildcard, and we end up resuming fewer
3592 threads (e.g., we might need to set threads stepping over
3593 breakpoints first), from the user/frontend's point of view, all
3594 threads in RESUME_PTID are now running. Unless we're calling an
3595 inferior function, as in that case we pretend the inferior
3596 doesn't run at all. */
3597 if (!cur_thr
->control
.in_infcall
)
3598 set_running (resume_target
, resume_ptid
, true);
3600 infrun_debug_printf ("addr=%s, signal=%s, resume_ptid=%s",
3601 paddress (gdbarch
, addr
),
3602 gdb_signal_to_symbol_string (siggnal
),
3603 resume_ptid
.to_string ().c_str ());
3605 annotate_starting ();
3607 /* Make sure that output from GDB appears before output from the
3609 gdb_flush (gdb_stdout
);
3611 /* Since we've marked the inferior running, give it the terminal. A
3612 QUIT/Ctrl-C from here on is forwarded to the target (which can
3613 still detect attempts to unblock a stuck connection with repeated
3614 Ctrl-C from within target_pass_ctrlc). */
3615 target_terminal::inferior ();
3617 /* In a multi-threaded task we may select another thread and
3618 then continue or step.
3620 But if a thread that we're resuming had stopped at a breakpoint,
3621 it will immediately cause another breakpoint stop without any
3622 execution (i.e. it will report a breakpoint hit incorrectly). So
3623 we must step over it first.
3625 Look for threads other than the current (TP) that reported a
3626 breakpoint hit and haven't been resumed yet since. */
3628 /* If scheduler locking applies, we can avoid iterating over all
3630 if (!non_stop
&& !schedlock_applies (cur_thr
))
3632 for (thread_info
*tp
: all_non_exited_threads (resume_target
,
3635 switch_to_thread_no_regs (tp
);
3637 /* Ignore the current thread here. It's handled
3642 if (!thread_still_needs_step_over (tp
))
3645 gdb_assert (!thread_is_in_step_over_chain (tp
));
3647 infrun_debug_printf ("need to step-over [%s] first",
3648 tp
->ptid
.to_string ().c_str ());
3650 global_thread_step_over_chain_enqueue (tp
);
3653 switch_to_thread (cur_thr
);
3656 /* Enqueue the current thread last, so that we move all other
3657 threads over their breakpoints first. */
3658 if (cur_thr
->stepping_over_breakpoint
)
3659 global_thread_step_over_chain_enqueue (cur_thr
);
3661 /* If the thread isn't started, we'll still need to set its prev_pc,
3662 so that switch_back_to_stepped_thread knows the thread hasn't
3663 advanced. Must do this before resuming any thread, as in
3664 all-stop/remote, once we resume we can't send any other packet
3665 until the target stops again. */
3666 cur_thr
->prev_pc
= regcache_read_pc_protected (regcache
);
3669 scoped_disable_commit_resumed
disable_commit_resumed ("proceeding");
3670 bool step_over_started
= start_step_over ();
3672 if (step_over_info_valid_p ())
3674 /* Either this thread started a new in-line step over, or some
3675 other thread was already doing one. In either case, don't
3676 resume anything else until the step-over is finished. */
3678 else if (step_over_started
&& !target_is_non_stop_p ())
3680 /* A new displaced stepping sequence was started. In all-stop,
3681 we can't talk to the target anymore until it next stops. */
3683 else if (!non_stop
&& target_is_non_stop_p ())
3685 INFRUN_SCOPED_DEBUG_START_END
3686 ("resuming threads, all-stop-on-top-of-non-stop");
3688 /* In all-stop, but the target is always in non-stop mode.
3689 Start all other threads that are implicitly resumed too. */
3690 for (thread_info
*tp
: all_non_exited_threads (resume_target
,
3693 switch_to_thread_no_regs (tp
);
3694 proceed_resume_thread_checked (tp
);
3698 proceed_resume_thread_checked (cur_thr
);
3700 disable_commit_resumed
.reset_and_commit ();
3703 finish_state
.release ();
3705 /* If we've switched threads above, switch back to the previously
3706 current thread. We don't want the user to see a different
3708 switch_to_thread (cur_thr
);
3710 /* Tell the event loop to wait for it to stop. If the target
3711 supports asynchronous execution, it'll do this from within
3713 if (!target_can_async_p ())
3714 mark_async_event_handler (infrun_async_inferior_event_token
);
3718 /* Start remote-debugging of a machine over a serial link. */
3721 start_remote (int from_tty
)
3723 inferior
*inf
= current_inferior ();
3724 inf
->control
.stop_soon
= STOP_QUIETLY_REMOTE
;
3726 /* Always go on waiting for the target, regardless of the mode. */
3727 /* FIXME: cagney/1999-09-23: At present it isn't possible to
3728 indicate to wait_for_inferior that a target should timeout if
3729 nothing is returned (instead of just blocking). Because of this,
3730 targets expecting an immediate response need to, internally, set
3731 things up so that the target_wait() is forced to eventually
3733 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
3734 differentiate to its caller what the state of the target is after
3735 the initial open has been performed. Here we're assuming that
3736 the target has stopped. It should be possible to eventually have
3737 target_open() return to the caller an indication that the target
3738 is currently running and GDB state should be set to the same as
3739 for an async run. */
3740 wait_for_inferior (inf
);
3742 /* Now that the inferior has stopped, do any bookkeeping like
3743 loading shared libraries. We want to do this before normal_stop,
3744 so that the displayed frame is up to date. */
3745 post_create_inferior (from_tty
);
3750 /* Initialize static vars when a new inferior begins. */
3753 init_wait_for_inferior (void)
3755 /* These are meaningless until the first time through wait_for_inferior. */
3757 breakpoint_init_inferior (inf_starting
);
3759 clear_proceed_status (0);
3761 nullify_last_target_wait_ptid ();
3763 update_previous_thread ();
3768 static void handle_inferior_event (struct execution_control_state
*ecs
);
3770 static void handle_step_into_function (struct gdbarch
*gdbarch
,
3771 struct execution_control_state
*ecs
);
3772 static void handle_step_into_function_backward (struct gdbarch
*gdbarch
,
3773 struct execution_control_state
*ecs
);
3774 static void handle_signal_stop (struct execution_control_state
*ecs
);
3775 static void check_exception_resume (struct execution_control_state
*,
3778 static void end_stepping_range (struct execution_control_state
*ecs
);
3779 static void stop_waiting (struct execution_control_state
*ecs
);
3780 static void keep_going (struct execution_control_state
*ecs
);
3781 static void process_event_stop_test (struct execution_control_state
*ecs
);
3782 static bool switch_back_to_stepped_thread (struct execution_control_state
*ecs
);
3784 /* This function is attached as a "thread_stop_requested" observer.
3785 Cleanup local state that assumed the PTID was to be resumed, and
3786 report the stop to the frontend. */
3789 infrun_thread_stop_requested (ptid_t ptid
)
3791 process_stratum_target
*curr_target
= current_inferior ()->process_target ();
3793 /* PTID was requested to stop. If the thread was already stopped,
3794 but the user/frontend doesn't know about that yet (e.g., the
3795 thread had been temporarily paused for some step-over), set up
3796 for reporting the stop now. */
3797 for (thread_info
*tp
: all_threads (curr_target
, ptid
))
3799 if (tp
->state
!= THREAD_RUNNING
)
3801 if (tp
->executing ())
3804 /* Remove matching threads from the step-over queue, so
3805 start_step_over doesn't try to resume them
3807 if (thread_is_in_step_over_chain (tp
))
3808 global_thread_step_over_chain_remove (tp
);
3810 /* If the thread is stopped, but the user/frontend doesn't
3811 know about that yet, queue a pending event, as if the
3812 thread had just stopped now. Unless the thread already had
3814 if (!tp
->has_pending_waitstatus ())
3816 target_waitstatus ws
;
3817 ws
.set_stopped (GDB_SIGNAL_0
);
3818 tp
->set_pending_waitstatus (ws
);
3821 /* Clear the inline-frame state, since we're re-processing the
3823 clear_inline_frame_state (tp
);
3825 /* If this thread was paused because some other thread was
3826 doing an inline-step over, let that finish first. Once
3827 that happens, we'll restart all threads and consume pending
3828 stop events then. */
3829 if (step_over_info_valid_p ())
3832 /* Otherwise we can process the (new) pending event now. Set
3833 it so this pending event is considered by
3835 tp
->set_resumed (true);
3839 /* Delete the step resume, single-step and longjmp/exception resume
3840 breakpoints of TP. */
3843 delete_thread_infrun_breakpoints (struct thread_info
*tp
)
3845 delete_step_resume_breakpoint (tp
);
3846 delete_exception_resume_breakpoint (tp
);
3847 delete_single_step_breakpoints (tp
);
3850 /* If the target still has execution, call FUNC for each thread that
3851 just stopped. In all-stop, that's all the non-exited threads; in
3852 non-stop, that's the current thread, only. */
3854 typedef void (*for_each_just_stopped_thread_callback_func
)
3855 (struct thread_info
*tp
);
3858 for_each_just_stopped_thread (for_each_just_stopped_thread_callback_func func
)
3860 if (!target_has_execution () || inferior_ptid
== null_ptid
)
3863 if (target_is_non_stop_p ())
3865 /* If in non-stop mode, only the current thread stopped. */
3866 func (inferior_thread ());
3870 /* In all-stop mode, all threads have stopped. */
3871 for (thread_info
*tp
: all_non_exited_threads ())
3876 /* Delete the step resume and longjmp/exception resume breakpoints of
3877 the threads that just stopped. */
3880 delete_just_stopped_threads_infrun_breakpoints (void)
3882 for_each_just_stopped_thread (delete_thread_infrun_breakpoints
);
3885 /* Delete the single-step breakpoints of the threads that just
3889 delete_just_stopped_threads_single_step_breakpoints (void)
3891 for_each_just_stopped_thread (delete_single_step_breakpoints
);
3897 print_target_wait_results (ptid_t waiton_ptid
, ptid_t result_ptid
,
3898 const struct target_waitstatus
&ws
)
3900 infrun_debug_printf ("target_wait (%s [%s], status) =",
3901 waiton_ptid
.to_string ().c_str (),
3902 target_pid_to_str (waiton_ptid
).c_str ());
3903 infrun_debug_printf (" %s [%s],",
3904 result_ptid
.to_string ().c_str (),
3905 target_pid_to_str (result_ptid
).c_str ());
3906 infrun_debug_printf (" %s", ws
.to_string ().c_str ());
3909 /* Select a thread at random, out of those which are resumed and have
3912 static struct thread_info
*
3913 random_pending_event_thread (inferior
*inf
, ptid_t waiton_ptid
)
3915 process_stratum_target
*proc_target
= inf
->process_target ();
3917 = proc_target
->random_resumed_with_pending_wait_status (inf
, waiton_ptid
);
3919 if (thread
== nullptr)
3921 infrun_debug_printf ("None found.");
3925 infrun_debug_printf ("Found %s.", thread
->ptid
.to_string ().c_str ());
3926 gdb_assert (thread
->resumed ());
3927 gdb_assert (thread
->has_pending_waitstatus ());
3932 /* Wrapper for target_wait that first checks whether threads have
3933 pending statuses to report before actually asking the target for
3934 more events. INF is the inferior we're using to call target_wait
3938 do_target_wait_1 (inferior
*inf
, ptid_t ptid
,
3939 target_waitstatus
*status
, target_wait_flags options
)
3941 struct thread_info
*tp
;
3943 /* We know that we are looking for an event in the target of inferior
3944 INF, but we don't know which thread the event might come from. As
3945 such we want to make sure that INFERIOR_PTID is reset so that none of
3946 the wait code relies on it - doing so is always a mistake. */
3947 switch_to_inferior_no_thread (inf
);
3949 /* First check if there is a resumed thread with a wait status
3951 if (ptid
== minus_one_ptid
|| ptid
.is_pid ())
3953 tp
= random_pending_event_thread (inf
, ptid
);
3957 infrun_debug_printf ("Waiting for specific thread %s.",
3958 ptid
.to_string ().c_str ());
3960 /* We have a specific thread to check. */
3961 tp
= inf
->find_thread (ptid
);
3962 gdb_assert (tp
!= nullptr);
3963 if (!tp
->has_pending_waitstatus ())
3968 && (tp
->stop_reason () == TARGET_STOPPED_BY_SW_BREAKPOINT
3969 || tp
->stop_reason () == TARGET_STOPPED_BY_HW_BREAKPOINT
))
3971 struct regcache
*regcache
= get_thread_regcache (tp
);
3972 struct gdbarch
*gdbarch
= regcache
->arch ();
3976 pc
= regcache_read_pc (regcache
);
3978 if (pc
!= tp
->stop_pc ())
3980 infrun_debug_printf ("PC of %s changed. was=%s, now=%s",
3981 tp
->ptid
.to_string ().c_str (),
3982 paddress (gdbarch
, tp
->stop_pc ()),
3983 paddress (gdbarch
, pc
));
3986 else if (!breakpoint_inserted_here_p (regcache
->aspace (), pc
))
3988 infrun_debug_printf ("previous breakpoint of %s, at %s gone",
3989 tp
->ptid
.to_string ().c_str (),
3990 paddress (gdbarch
, pc
));
3997 infrun_debug_printf ("pending event of %s cancelled.",
3998 tp
->ptid
.to_string ().c_str ());
4000 tp
->clear_pending_waitstatus ();
4001 target_waitstatus ws
;
4003 tp
->set_pending_waitstatus (ws
);
4004 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
4010 infrun_debug_printf ("Using pending wait status %s for %s.",
4011 tp
->pending_waitstatus ().to_string ().c_str (),
4012 tp
->ptid
.to_string ().c_str ());
4014 /* Now that we've selected our final event LWP, un-adjust its PC
4015 if it was a software breakpoint (and the target doesn't
4016 always adjust the PC itself). */
4017 if (tp
->stop_reason () == TARGET_STOPPED_BY_SW_BREAKPOINT
4018 && !target_supports_stopped_by_sw_breakpoint ())
4020 struct regcache
*regcache
;
4021 struct gdbarch
*gdbarch
;
4024 regcache
= get_thread_regcache (tp
);
4025 gdbarch
= regcache
->arch ();
4027 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
4032 pc
= regcache_read_pc (regcache
);
4033 regcache_write_pc (regcache
, pc
+ decr_pc
);
4037 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
4038 *status
= tp
->pending_waitstatus ();
4039 tp
->clear_pending_waitstatus ();
4041 /* Wake up the event loop again, until all pending events are
4043 if (target_is_async_p ())
4044 mark_async_event_handler (infrun_async_inferior_event_token
);
4048 /* But if we don't find one, we'll have to wait. */
4050 /* We can't ask a non-async target to do a non-blocking wait, so this will be
4052 if (!target_can_async_p ())
4053 options
&= ~TARGET_WNOHANG
;
4055 return target_wait (ptid
, status
, options
);
4058 /* Wrapper for target_wait that first checks whether threads have
4059 pending statuses to report before actually asking the target for
4060 more events. Polls for events from all inferiors/targets. */
4063 do_target_wait (execution_control_state
*ecs
, target_wait_flags options
)
4065 int num_inferiors
= 0;
4066 int random_selector
;
4068 /* For fairness, we pick the first inferior/target to poll at random
4069 out of all inferiors that may report events, and then continue
4070 polling the rest of the inferior list starting from that one in a
4071 circular fashion until the whole list is polled once. */
4073 auto inferior_matches
= [] (inferior
*inf
)
4075 return inf
->process_target () != nullptr;
4078 /* First see how many matching inferiors we have. */
4079 for (inferior
*inf
: all_inferiors ())
4080 if (inferior_matches (inf
))
4083 if (num_inferiors
== 0)
4085 ecs
->ws
.set_ignore ();
4089 /* Now randomly pick an inferior out of those that matched. */
4090 random_selector
= (int)
4091 ((num_inferiors
* (double) rand ()) / (RAND_MAX
+ 1.0));
4093 if (num_inferiors
> 1)
4094 infrun_debug_printf ("Found %d inferiors, starting at #%d",
4095 num_inferiors
, random_selector
);
4097 /* Select the Nth inferior that matched. */
4099 inferior
*selected
= nullptr;
4101 for (inferior
*inf
: all_inferiors ())
4102 if (inferior_matches (inf
))
4103 if (random_selector
-- == 0)
4109 /* Now poll for events out of each of the matching inferior's
4110 targets, starting from the selected one. */
4112 auto do_wait
= [&] (inferior
*inf
)
4114 ecs
->ptid
= do_target_wait_1 (inf
, minus_one_ptid
, &ecs
->ws
, options
);
4115 ecs
->target
= inf
->process_target ();
4116 return (ecs
->ws
.kind () != TARGET_WAITKIND_IGNORE
);
4119 /* Needed in 'all-stop + target-non-stop' mode, because we end up
4120 here spuriously after the target is all stopped and we've already
4121 reported the stop to the user, polling for events. */
4122 scoped_restore_current_thread restore_thread
;
4124 intrusive_list_iterator
<inferior
> start
4125 = inferior_list
.iterator_to (*selected
);
4127 for (intrusive_list_iterator
<inferior
> it
= start
;
4128 it
!= inferior_list
.end ();
4131 inferior
*inf
= &*it
;
4133 if (inferior_matches (inf
) && do_wait (inf
))
4137 for (intrusive_list_iterator
<inferior
> it
= inferior_list
.begin ();
4141 inferior
*inf
= &*it
;
4143 if (inferior_matches (inf
) && do_wait (inf
))
4147 ecs
->ws
.set_ignore ();
4151 /* An event reported by wait_one. */
4153 struct wait_one_event
4155 /* The target the event came out of. */
4156 process_stratum_target
*target
;
4158 /* The PTID the event was for. */
4161 /* The waitstatus. */
4162 target_waitstatus ws
;
4165 static bool handle_one (const wait_one_event
&event
);
4167 /* Prepare and stabilize the inferior for detaching it. E.g.,
4168 detaching while a thread is displaced stepping is a recipe for
4169 crashing it, as nothing would readjust the PC out of the scratch
4173 prepare_for_detach (void)
4175 struct inferior
*inf
= current_inferior ();
4176 ptid_t pid_ptid
= ptid_t (inf
->pid
);
4177 scoped_restore_current_thread restore_thread
;
4179 scoped_restore restore_detaching
= make_scoped_restore (&inf
->detaching
, true);
4181 /* Remove all threads of INF from the global step-over chain. We
4182 want to stop any ongoing step-over, not start any new one. */
4183 thread_step_over_list_safe_range range
4184 = make_thread_step_over_list_safe_range (global_thread_step_over_list
);
4186 for (thread_info
*tp
: range
)
4189 infrun_debug_printf ("removing thread %s from global step over chain",
4190 tp
->ptid
.to_string ().c_str ());
4191 global_thread_step_over_chain_remove (tp
);
4194 /* If we were already in the middle of an inline step-over, and the
4195 thread stepping belongs to the inferior we're detaching, we need
4196 to restart the threads of other inferiors. */
4197 if (step_over_info
.thread
!= -1)
4199 infrun_debug_printf ("inline step-over in-process while detaching");
4201 thread_info
*thr
= find_thread_global_id (step_over_info
.thread
);
4202 if (thr
->inf
== inf
)
4204 /* Since we removed threads of INF from the step-over chain,
4205 we know this won't start a step-over for INF. */
4206 clear_step_over_info ();
4208 if (target_is_non_stop_p ())
4210 /* Start a new step-over in another thread if there's
4211 one that needs it. */
4214 /* Restart all other threads (except the
4215 previously-stepping thread, since that one is still
4217 if (!step_over_info_valid_p ())
4218 restart_threads (thr
);
4223 if (displaced_step_in_progress (inf
))
4225 infrun_debug_printf ("displaced-stepping in-process while detaching");
4227 /* Stop threads currently displaced stepping, aborting it. */
4229 for (thread_info
*thr
: inf
->non_exited_threads ())
4231 if (thr
->displaced_step_state
.in_progress ())
4233 if (thr
->executing ())
4235 if (!thr
->stop_requested
)
4237 target_stop (thr
->ptid
);
4238 thr
->stop_requested
= true;
4242 thr
->set_resumed (false);
4246 while (displaced_step_in_progress (inf
))
4248 wait_one_event event
;
4250 event
.target
= inf
->process_target ();
4251 event
.ptid
= do_target_wait_1 (inf
, pid_ptid
, &event
.ws
, 0);
4254 print_target_wait_results (pid_ptid
, event
.ptid
, event
.ws
);
4259 /* It's OK to leave some of the threads of INF stopped, since
4260 they'll be detached shortly. */
4264 /* If all-stop, but there exists a non-stop target, stop all threads
4265 now that we're presenting the stop to the user. */
4268 stop_all_threads_if_all_stop_mode ()
4270 if (!non_stop
&& exists_non_stop_target ())
4271 stop_all_threads ("presenting stop to user in all-stop");
4274 /* Wait for control to return from inferior to debugger.
4276 If inferior gets a signal, we may decide to start it up again
4277 instead of returning. That is why there is a loop in this function.
4278 When this function actually returns it means the inferior
4279 should be left stopped and GDB should read more commands. */
4282 wait_for_inferior (inferior
*inf
)
4284 infrun_debug_printf ("wait_for_inferior ()");
4286 SCOPE_EXIT
{ delete_just_stopped_threads_infrun_breakpoints (); };
4288 /* If an error happens while handling the event, propagate GDB's
4289 knowledge of the executing state to the frontend/user running
4291 scoped_finish_thread_state finish_state
4292 (inf
->process_target (), minus_one_ptid
);
4296 execution_control_state ecs
;
4298 overlay_cache_invalid
= 1;
4300 /* Flush target cache before starting to handle each event.
4301 Target was running and cache could be stale. This is just a
4302 heuristic. Running threads may modify target memory, but we
4303 don't get any event. */
4304 target_dcache_invalidate ();
4306 ecs
.ptid
= do_target_wait_1 (inf
, minus_one_ptid
, &ecs
.ws
, 0);
4307 ecs
.target
= inf
->process_target ();
4310 print_target_wait_results (minus_one_ptid
, ecs
.ptid
, ecs
.ws
);
4312 /* Now figure out what to do with the result of the result. */
4313 handle_inferior_event (&ecs
);
4315 if (!ecs
.wait_some_more
)
4319 stop_all_threads_if_all_stop_mode ();
4321 /* No error, don't finish the state yet. */
4322 finish_state
.release ();
4325 /* Cleanup that reinstalls the readline callback handler, if the
4326 target is running in the background. If while handling the target
4327 event something triggered a secondary prompt, like e.g., a
4328 pagination prompt, we'll have removed the callback handler (see
4329 gdb_readline_wrapper_line). Need to do this as we go back to the
4330 event loop, ready to process further input. Note this has no
4331 effect if the handler hasn't actually been removed, because calling
4332 rl_callback_handler_install resets the line buffer, thus losing
4336 reinstall_readline_callback_handler_cleanup ()
4338 struct ui
*ui
= current_ui
;
4342 /* We're not going back to the top level event loop yet. Don't
4343 install the readline callback, as it'd prep the terminal,
4344 readline-style (raw, noecho) (e.g., --batch). We'll install
4345 it the next time the prompt is displayed, when we're ready
4350 if (ui
->command_editing
&& ui
->prompt_state
!= PROMPT_BLOCKED
)
4351 gdb_rl_callback_handler_reinstall ();
4354 /* Clean up the FSMs of threads that are now stopped. In non-stop,
4355 that's just the event thread. In all-stop, that's all threads. In
4356 all-stop, threads that had a pending exit no longer have a reason
4357 to be around, as their FSMs/commands are canceled, so we delete
4358 them. This avoids "info threads" listing such threads as if they
4359 were alive (and failing to read their registers), the user being
4360 able to select and resume them (and that failing), etc. */
4363 clean_up_just_stopped_threads_fsms (struct execution_control_state
*ecs
)
4365 /* The first clean_up call below assumes the event thread is the current
4367 if (ecs
->event_thread
!= nullptr)
4368 gdb_assert (ecs
->event_thread
== inferior_thread ());
4370 if (ecs
->event_thread
!= nullptr
4371 && ecs
->event_thread
->thread_fsm () != nullptr)
4372 ecs
->event_thread
->thread_fsm ()->clean_up (ecs
->event_thread
);
4376 scoped_restore_current_thread restore_thread
;
4378 for (thread_info
*thr
: all_threads_safe ())
4380 if (thr
->state
== THREAD_EXITED
)
4383 if (thr
== ecs
->event_thread
)
4386 if (thr
->thread_fsm () != nullptr)
4388 switch_to_thread (thr
);
4389 thr
->thread_fsm ()->clean_up (thr
);
4392 /* As we are cancelling the command/FSM of this thread,
4393 whatever was the reason we needed to report a thread
4394 exited event to the user, that reason is gone. Delete
4395 the thread, so that the user doesn't see it in the thread
4396 list, the next proceed doesn't try to resume it, etc. */
4397 if (thr
->has_pending_waitstatus ()
4398 && (thr
->pending_waitstatus ().kind ()
4399 == TARGET_WAITKIND_THREAD_EXITED
))
4400 delete_thread (thr
);
4405 /* Helper for all_uis_check_sync_execution_done that works on the
4409 check_curr_ui_sync_execution_done (void)
4411 struct ui
*ui
= current_ui
;
4413 if (ui
->prompt_state
== PROMPT_NEEDED
4415 && !gdb_in_secondary_prompt_p (ui
))
4417 target_terminal::ours ();
4418 top_level_interpreter ()->on_sync_execution_done ();
4419 ui
->register_file_handler ();
4426 all_uis_check_sync_execution_done (void)
4428 SWITCH_THRU_ALL_UIS ()
4430 check_curr_ui_sync_execution_done ();
4437 all_uis_on_sync_execution_starting (void)
4439 SWITCH_THRU_ALL_UIS ()
4441 if (current_ui
->prompt_state
== PROMPT_NEEDED
)
4442 async_disable_stdin ();
4446 /* A quit_handler callback installed while we're handling inferior
4450 infrun_quit_handler ()
4452 if (target_terminal::is_ours ())
4456 default_quit_handler would throw a quit in this case, but if
4457 we're handling an event while we have the terminal, it means
4458 the target is running a background execution command, and
4459 thus when users press Ctrl-C, they're wanting to interrupt
4460 whatever command they were executing in the command line.
4464 (gdb) foo bar whatever<ctrl-c>
4466 That Ctrl-C should clear the input line, not interrupt event
4467 handling if it happens that the user types Ctrl-C at just the
4470 It's as-if background event handling was handled by a
4471 separate background thread.
4473 To be clear, the Ctrl-C is not lost -- it will be processed
4474 by the next QUIT call once we're out of fetch_inferior_event
4479 if (check_quit_flag ())
4480 target_pass_ctrlc ();
4484 /* Asynchronous version of wait_for_inferior. It is called by the
4485 event loop whenever a change of state is detected on the file
4486 descriptor corresponding to the target. It can be called more than
4487 once to complete a single execution command. In such cases we need
4488 to keep the state in a global variable ECSS. If it is the last time
4489 that this function is called for a single execution command, then
4490 report to the user that the inferior has stopped, and do the
4491 necessary cleanups. */
4494 fetch_inferior_event ()
4496 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
4498 execution_control_state ecs
;
4501 /* Events are always processed with the main UI as current UI. This
4502 way, warnings, debug output, etc. are always consistently sent to
4503 the main console. */
4504 scoped_restore save_ui
= make_scoped_restore (¤t_ui
, main_ui
);
4506 /* Temporarily disable pagination. Otherwise, the user would be
4507 given an option to press 'q' to quit, which would cause an early
4508 exit and could leave GDB in a half-baked state. */
4509 scoped_restore save_pagination
4510 = make_scoped_restore (&pagination_enabled
, false);
4512 /* Install a quit handler that does nothing if we have the terminal
4513 (meaning the target is running a background execution command),
4514 so that Ctrl-C never interrupts GDB before the event is fully
4516 scoped_restore restore_quit_handler
4517 = make_scoped_restore (&quit_handler
, infrun_quit_handler
);
4519 /* Make sure a SIGINT does not interrupt an extension language while
4520 we're handling an event. That could interrupt a Python unwinder
4521 or a Python observer or some such. A Ctrl-C should either be
4522 forwarded to the inferior if the inferior has the terminal, or,
4523 if GDB has the terminal, should interrupt the command the user is
4524 typing in the CLI. */
4525 scoped_disable_cooperative_sigint_handling restore_coop_sigint
;
4527 /* End up with readline processing input, if necessary. */
4529 SCOPE_EXIT
{ reinstall_readline_callback_handler_cleanup (); };
4531 /* We're handling a live event, so make sure we're doing live
4532 debugging. If we're looking at traceframes while the target is
4533 running, we're going to need to get back to that mode after
4534 handling the event. */
4535 gdb::optional
<scoped_restore_current_traceframe
> maybe_restore_traceframe
;
4538 maybe_restore_traceframe
.emplace ();
4539 set_current_traceframe (-1);
4542 /* The user/frontend should not notice a thread switch due to
4543 internal events. Make sure we revert to the user selected
4544 thread and frame after handling the event and running any
4545 breakpoint commands. */
4546 scoped_restore_current_thread restore_thread
;
4548 overlay_cache_invalid
= 1;
4549 /* Flush target cache before starting to handle each event. Target
4550 was running and cache could be stale. This is just a heuristic.
4551 Running threads may modify target memory, but we don't get any
4553 target_dcache_invalidate ();
4555 scoped_restore save_exec_dir
4556 = make_scoped_restore (&execution_direction
,
4557 target_execution_direction ());
4559 /* Allow targets to pause their resumed threads while we handle
4561 scoped_disable_commit_resumed
disable_commit_resumed ("handling event");
4563 if (!do_target_wait (&ecs
, TARGET_WNOHANG
))
4565 infrun_debug_printf ("do_target_wait returned no event");
4566 disable_commit_resumed
.reset_and_commit ();
4570 gdb_assert (ecs
.ws
.kind () != TARGET_WAITKIND_IGNORE
);
4572 /* Switch to the inferior that generated the event, so we can do
4573 target calls. If the event was not associated to a ptid, */
4574 if (ecs
.ptid
!= null_ptid
4575 && ecs
.ptid
!= minus_one_ptid
)
4576 switch_to_inferior_no_thread (find_inferior_ptid (ecs
.target
, ecs
.ptid
));
4578 switch_to_target_no_thread (ecs
.target
);
4581 print_target_wait_results (minus_one_ptid
, ecs
.ptid
, ecs
.ws
);
4583 /* If an error happens while handling the event, propagate GDB's
4584 knowledge of the executing state to the frontend/user running
4586 ptid_t finish_ptid
= !target_is_non_stop_p () ? minus_one_ptid
: ecs
.ptid
;
4587 scoped_finish_thread_state
finish_state (ecs
.target
, finish_ptid
);
4589 /* Get executed before scoped_restore_current_thread above to apply
4590 still for the thread which has thrown the exception. */
4591 auto defer_bpstat_clear
4592 = make_scope_exit (bpstat_clear_actions
);
4593 auto defer_delete_threads
4594 = make_scope_exit (delete_just_stopped_threads_infrun_breakpoints
);
4596 int stop_id
= get_stop_id ();
4598 /* Now figure out what to do with the result of the result. */
4599 handle_inferior_event (&ecs
);
4601 if (!ecs
.wait_some_more
)
4603 struct inferior
*inf
= find_inferior_ptid (ecs
.target
, ecs
.ptid
);
4604 bool should_stop
= true;
4605 struct thread_info
*thr
= ecs
.event_thread
;
4607 delete_just_stopped_threads_infrun_breakpoints ();
4609 if (thr
!= nullptr && thr
->thread_fsm () != nullptr)
4610 should_stop
= thr
->thread_fsm ()->should_stop (thr
);
4618 bool should_notify_stop
= true;
4619 bool proceeded
= false;
4621 stop_all_threads_if_all_stop_mode ();
4623 clean_up_just_stopped_threads_fsms (&ecs
);
4625 if (stop_id
!= get_stop_id ())
4627 /* If the stop-id has changed then a stop has already been
4628 presented to the user in handle_inferior_event, this is
4629 likely a failed inferior call. As the stop has already
4630 been announced then we should not notify again.
4632 Also, if the prompt state is not PROMPT_NEEDED then GDB
4633 will not be ready for user input after this function. */
4634 should_notify_stop
= false;
4635 gdb_assert (current_ui
->prompt_state
== PROMPT_NEEDED
);
4637 else if (thr
!= nullptr && thr
->thread_fsm () != nullptr)
4639 = thr
->thread_fsm ()->should_notify_stop ();
4641 if (should_notify_stop
)
4643 /* We may not find an inferior if this was a process exit. */
4644 if (inf
== nullptr || inf
->control
.stop_soon
== NO_STOP_QUIETLY
)
4645 proceeded
= normal_stop ();
4650 inferior_event_handler (INF_EXEC_COMPLETE
);
4654 /* If we got a TARGET_WAITKIND_NO_RESUMED event, then the
4655 previously selected thread is gone. We have two
4656 choices - switch to no thread selected, or restore the
4657 previously selected thread (now exited). We chose the
4658 later, just because that's what GDB used to do. After
4659 this, "info threads" says "The current thread <Thread
4660 ID 2> has terminated." instead of "No thread
4664 && ecs
.ws
.kind () != TARGET_WAITKIND_NO_RESUMED
)
4665 restore_thread
.dont_restore ();
4669 defer_delete_threads
.release ();
4670 defer_bpstat_clear
.release ();
4672 /* No error, don't finish the thread states yet. */
4673 finish_state
.release ();
4675 disable_commit_resumed
.reset_and_commit ();
4677 /* This scope is used to ensure that readline callbacks are
4678 reinstalled here. */
4681 /* Handling this event might have caused some inferiors to become prunable.
4682 For example, the exit of an inferior that was automatically added. Try
4683 to get rid of them. Keeping those around slows down things linearly.
4685 Note that this never removes the current inferior. Therefore, call this
4686 after RESTORE_THREAD went out of scope, in case the event inferior (which was
4687 temporarily made the current inferior) is meant to be deleted.
4689 Call this before all_uis_check_sync_execution_done, so that notifications about
4690 removed inferiors appear before the prompt. */
4693 /* If a UI was in sync execution mode, and now isn't, restore its
4694 prompt (a synchronous execution command has finished, and we're
4695 ready for input). */
4696 all_uis_check_sync_execution_done ();
4699 && exec_done_display_p
4700 && (inferior_ptid
== null_ptid
4701 || inferior_thread ()->state
!= THREAD_RUNNING
))
4702 gdb_printf (_("completed.\n"));
4708 set_step_info (thread_info
*tp
, frame_info_ptr frame
,
4709 struct symtab_and_line sal
)
4711 /* This can be removed once this function no longer implicitly relies on the
4712 inferior_ptid value. */
4713 gdb_assert (inferior_ptid
== tp
->ptid
);
4715 tp
->control
.step_frame_id
= get_frame_id (frame
);
4716 tp
->control
.step_stack_frame_id
= get_stack_frame_id (frame
);
4718 tp
->current_symtab
= sal
.symtab
;
4719 tp
->current_line
= sal
.line
;
4722 ("symtab = %s, line = %d, step_frame_id = %s, step_stack_frame_id = %s",
4723 tp
->current_symtab
!= nullptr ? tp
->current_symtab
->filename
: "<null>",
4725 tp
->control
.step_frame_id
.to_string ().c_str (),
4726 tp
->control
.step_stack_frame_id
.to_string ().c_str ());
4729 /* Clear context switchable stepping state. */
4732 init_thread_stepping_state (struct thread_info
*tss
)
4734 tss
->stepped_breakpoint
= 0;
4735 tss
->stepping_over_breakpoint
= 0;
4736 tss
->stepping_over_watchpoint
= 0;
4737 tss
->step_after_step_resume_breakpoint
= 0;
4743 set_last_target_status (process_stratum_target
*target
, ptid_t ptid
,
4744 const target_waitstatus
&status
)
4746 target_last_proc_target
= target
;
4747 target_last_wait_ptid
= ptid
;
4748 target_last_waitstatus
= status
;
4754 get_last_target_status (process_stratum_target
**target
, ptid_t
*ptid
,
4755 target_waitstatus
*status
)
4757 if (target
!= nullptr)
4758 *target
= target_last_proc_target
;
4759 if (ptid
!= nullptr)
4760 *ptid
= target_last_wait_ptid
;
4761 if (status
!= nullptr)
4762 *status
= target_last_waitstatus
;
4768 nullify_last_target_wait_ptid (void)
4770 target_last_proc_target
= nullptr;
4771 target_last_wait_ptid
= minus_one_ptid
;
4772 target_last_waitstatus
= {};
4775 /* Switch thread contexts. */
4778 context_switch (execution_control_state
*ecs
)
4780 if (ecs
->ptid
!= inferior_ptid
4781 && (inferior_ptid
== null_ptid
4782 || ecs
->event_thread
!= inferior_thread ()))
4784 infrun_debug_printf ("Switching context from %s to %s",
4785 inferior_ptid
.to_string ().c_str (),
4786 ecs
->ptid
.to_string ().c_str ());
4789 switch_to_thread (ecs
->event_thread
);
4792 /* If the target can't tell whether we've hit breakpoints
4793 (target_supports_stopped_by_sw_breakpoint), and we got a SIGTRAP,
4794 check whether that could have been caused by a breakpoint. If so,
4795 adjust the PC, per gdbarch_decr_pc_after_break. */
4798 adjust_pc_after_break (struct thread_info
*thread
,
4799 const target_waitstatus
&ws
)
4801 struct regcache
*regcache
;
4802 struct gdbarch
*gdbarch
;
4803 CORE_ADDR breakpoint_pc
, decr_pc
;
4805 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
4806 we aren't, just return.
4808 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
4809 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
4810 implemented by software breakpoints should be handled through the normal
4813 NOTE drow/2004-01-31: On some targets, breakpoints may generate
4814 different signals (SIGILL or SIGEMT for instance), but it is less
4815 clear where the PC is pointing afterwards. It may not match
4816 gdbarch_decr_pc_after_break. I don't know any specific target that
4817 generates these signals at breakpoints (the code has been in GDB since at
4818 least 1992) so I can not guess how to handle them here.
4820 In earlier versions of GDB, a target with
4821 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
4822 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
4823 target with both of these set in GDB history, and it seems unlikely to be
4824 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
4826 if (ws
.kind () != TARGET_WAITKIND_STOPPED
)
4829 if (ws
.sig () != GDB_SIGNAL_TRAP
)
4832 /* In reverse execution, when a breakpoint is hit, the instruction
4833 under it has already been de-executed. The reported PC always
4834 points at the breakpoint address, so adjusting it further would
4835 be wrong. E.g., consider this case on a decr_pc_after_break == 1
4838 B1 0x08000000 : INSN1
4839 B2 0x08000001 : INSN2
4841 PC -> 0x08000003 : INSN4
4843 Say you're stopped at 0x08000003 as above. Reverse continuing
4844 from that point should hit B2 as below. Reading the PC when the
4845 SIGTRAP is reported should read 0x08000001 and INSN2 should have
4846 been de-executed already.
4848 B1 0x08000000 : INSN1
4849 B2 PC -> 0x08000001 : INSN2
4853 We can't apply the same logic as for forward execution, because
4854 we would wrongly adjust the PC to 0x08000000, since there's a
4855 breakpoint at PC - 1. We'd then report a hit on B1, although
4856 INSN1 hadn't been de-executed yet. Doing nothing is the correct
4858 if (execution_direction
== EXEC_REVERSE
)
4861 /* If the target can tell whether the thread hit a SW breakpoint,
4862 trust it. Targets that can tell also adjust the PC
4864 if (target_supports_stopped_by_sw_breakpoint ())
4867 /* Note that relying on whether a breakpoint is planted in memory to
4868 determine this can fail. E.g,. the breakpoint could have been
4869 removed since. Or the thread could have been told to step an
4870 instruction the size of a breakpoint instruction, and only
4871 _after_ was a breakpoint inserted at its address. */
4873 /* If this target does not decrement the PC after breakpoints, then
4874 we have nothing to do. */
4875 regcache
= get_thread_regcache (thread
);
4876 gdbarch
= regcache
->arch ();
4878 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
4882 const address_space
*aspace
= regcache
->aspace ();
4884 /* Find the location where (if we've hit a breakpoint) the
4885 breakpoint would be. */
4886 breakpoint_pc
= regcache_read_pc (regcache
) - decr_pc
;
4888 /* If the target can't tell whether a software breakpoint triggered,
4889 fallback to figuring it out based on breakpoints we think were
4890 inserted in the target, and on whether the thread was stepped or
4893 /* Check whether there actually is a software breakpoint inserted at
4896 If in non-stop mode, a race condition is possible where we've
4897 removed a breakpoint, but stop events for that breakpoint were
4898 already queued and arrive later. To suppress those spurious
4899 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
4900 and retire them after a number of stop events are reported. Note
4901 this is an heuristic and can thus get confused. The real fix is
4902 to get the "stopped by SW BP and needs adjustment" info out of
4903 the target/kernel (and thus never reach here; see above). */
4904 if (software_breakpoint_inserted_here_p (aspace
, breakpoint_pc
)
4905 || (target_is_non_stop_p ()
4906 && moribund_breakpoint_here_p (aspace
, breakpoint_pc
)))
4908 gdb::optional
<scoped_restore_tmpl
<int>> restore_operation_disable
;
4910 if (record_full_is_used ())
4911 restore_operation_disable
.emplace
4912 (record_full_gdb_operation_disable_set ());
4914 /* When using hardware single-step, a SIGTRAP is reported for both
4915 a completed single-step and a software breakpoint. Need to
4916 differentiate between the two, as the latter needs adjusting
4917 but the former does not.
4919 The SIGTRAP can be due to a completed hardware single-step only if
4920 - we didn't insert software single-step breakpoints
4921 - this thread is currently being stepped
4923 If any of these events did not occur, we must have stopped due
4924 to hitting a software breakpoint, and have to back up to the
4927 As a special case, we could have hardware single-stepped a
4928 software breakpoint. In this case (prev_pc == breakpoint_pc),
4929 we also need to back up to the breakpoint address. */
4931 if (thread_has_single_step_breakpoints_set (thread
)
4932 || !currently_stepping (thread
)
4933 || (thread
->stepped_breakpoint
4934 && thread
->prev_pc
== breakpoint_pc
))
4935 regcache_write_pc (regcache
, breakpoint_pc
);
4940 stepped_in_from (frame_info_ptr frame
, struct frame_id step_frame_id
)
4942 for (frame
= get_prev_frame (frame
);
4944 frame
= get_prev_frame (frame
))
4946 if (get_frame_id (frame
) == step_frame_id
)
4949 if (get_frame_type (frame
) != INLINE_FRAME
)
4956 /* Look for an inline frame that is marked for skip.
4957 If PREV_FRAME is TRUE start at the previous frame,
4958 otherwise start at the current frame. Stop at the
4959 first non-inline frame, or at the frame where the
4963 inline_frame_is_marked_for_skip (bool prev_frame
, struct thread_info
*tp
)
4965 frame_info_ptr frame
= get_current_frame ();
4968 frame
= get_prev_frame (frame
);
4970 for (; frame
!= nullptr; frame
= get_prev_frame (frame
))
4972 const char *fn
= nullptr;
4973 symtab_and_line sal
;
4976 if (get_frame_id (frame
) == tp
->control
.step_frame_id
)
4978 if (get_frame_type (frame
) != INLINE_FRAME
)
4981 sal
= find_frame_sal (frame
);
4982 sym
= get_frame_function (frame
);
4985 fn
= sym
->print_name ();
4988 && function_name_is_marked_for_skip (fn
, sal
))
4995 /* If the event thread has the stop requested flag set, pretend it
4996 stopped for a GDB_SIGNAL_0 (i.e., as if it stopped due to
5000 handle_stop_requested (struct execution_control_state
*ecs
)
5002 if (ecs
->event_thread
->stop_requested
)
5004 ecs
->ws
.set_stopped (GDB_SIGNAL_0
);
5005 handle_signal_stop (ecs
);
5011 /* Auxiliary function that handles syscall entry/return events.
5012 It returns true if the inferior should keep going (and GDB
5013 should ignore the event), or false if the event deserves to be
5017 handle_syscall_event (struct execution_control_state
*ecs
)
5019 struct regcache
*regcache
;
5022 context_switch (ecs
);
5024 regcache
= get_thread_regcache (ecs
->event_thread
);
5025 syscall_number
= ecs
->ws
.syscall_number ();
5026 ecs
->event_thread
->set_stop_pc (regcache_read_pc (regcache
));
5028 if (catch_syscall_enabled () > 0
5029 && catching_syscall_number (syscall_number
))
5031 infrun_debug_printf ("syscall number=%d", syscall_number
);
5033 ecs
->event_thread
->control
.stop_bpstat
5034 = bpstat_stop_status_nowatch (regcache
->aspace (),
5035 ecs
->event_thread
->stop_pc (),
5036 ecs
->event_thread
, ecs
->ws
);
5038 if (handle_stop_requested (ecs
))
5041 if (bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
5043 /* Catchpoint hit. */
5048 if (handle_stop_requested (ecs
))
5051 /* If no catchpoint triggered for this, then keep going. */
5057 /* Lazily fill in the execution_control_state's stop_func_* fields. */
5060 fill_in_stop_func (struct gdbarch
*gdbarch
,
5061 struct execution_control_state
*ecs
)
5063 if (!ecs
->stop_func_filled_in
)
5066 const general_symbol_info
*gsi
;
5068 /* Don't care about return value; stop_func_start and stop_func_name
5069 will both be 0 if it doesn't work. */
5070 find_pc_partial_function_sym (ecs
->event_thread
->stop_pc (),
5072 &ecs
->stop_func_start
,
5073 &ecs
->stop_func_end
,
5075 ecs
->stop_func_name
= gsi
== nullptr ? nullptr : gsi
->print_name ();
5077 /* The call to find_pc_partial_function, above, will set
5078 stop_func_start and stop_func_end to the start and end
5079 of the range containing the stop pc. If this range
5080 contains the entry pc for the block (which is always the
5081 case for contiguous blocks), advance stop_func_start past
5082 the function's start offset and entrypoint. Note that
5083 stop_func_start is NOT advanced when in a range of a
5084 non-contiguous block that does not contain the entry pc. */
5085 if (block
!= nullptr
5086 && ecs
->stop_func_start
<= block
->entry_pc ()
5087 && block
->entry_pc () < ecs
->stop_func_end
)
5089 ecs
->stop_func_start
5090 += gdbarch_deprecated_function_start_offset (gdbarch
);
5092 /* PowerPC functions have a Local Entry Point (LEP) and a Global
5093 Entry Point (GEP). There is only one Entry Point (GEP = LEP) for
5094 other architectures. */
5095 ecs
->stop_func_alt_start
= ecs
->stop_func_start
;
5097 if (gdbarch_skip_entrypoint_p (gdbarch
))
5098 ecs
->stop_func_start
5099 = gdbarch_skip_entrypoint (gdbarch
, ecs
->stop_func_start
);
5102 ecs
->stop_func_filled_in
= 1;
5107 /* Return the STOP_SOON field of the inferior pointed at by ECS. */
5109 static enum stop_kind
5110 get_inferior_stop_soon (execution_control_state
*ecs
)
5112 struct inferior
*inf
= find_inferior_ptid (ecs
->target
, ecs
->ptid
);
5114 gdb_assert (inf
!= nullptr);
5115 return inf
->control
.stop_soon
;
5118 /* Poll for one event out of the current target. Store the resulting
5119 waitstatus in WS, and return the event ptid. Does not block. */
5122 poll_one_curr_target (struct target_waitstatus
*ws
)
5126 overlay_cache_invalid
= 1;
5128 /* Flush target cache before starting to handle each event.
5129 Target was running and cache could be stale. This is just a
5130 heuristic. Running threads may modify target memory, but we
5131 don't get any event. */
5132 target_dcache_invalidate ();
5134 event_ptid
= target_wait (minus_one_ptid
, ws
, TARGET_WNOHANG
);
5137 print_target_wait_results (minus_one_ptid
, event_ptid
, *ws
);
5142 /* Wait for one event out of any target. */
5144 static wait_one_event
5149 for (inferior
*inf
: all_inferiors ())
5151 process_stratum_target
*target
= inf
->process_target ();
5152 if (target
== nullptr
5153 || !target
->is_async_p ()
5154 || !target
->threads_executing
)
5157 switch_to_inferior_no_thread (inf
);
5159 wait_one_event event
;
5160 event
.target
= target
;
5161 event
.ptid
= poll_one_curr_target (&event
.ws
);
5163 if (event
.ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
5165 /* If nothing is resumed, remove the target from the
5167 target_async (false);
5169 else if (event
.ws
.kind () != TARGET_WAITKIND_IGNORE
)
5173 /* Block waiting for some event. */
5180 for (inferior
*inf
: all_inferiors ())
5182 process_stratum_target
*target
= inf
->process_target ();
5183 if (target
== nullptr
5184 || !target
->is_async_p ()
5185 || !target
->threads_executing
)
5188 int fd
= target
->async_wait_fd ();
5189 FD_SET (fd
, &readfds
);
5196 /* No waitable targets left. All must be stopped. */
5197 target_waitstatus ws
;
5198 ws
.set_no_resumed ();
5199 return {nullptr, minus_one_ptid
, std::move (ws
)};
5204 int numfds
= interruptible_select (nfds
, &readfds
, 0, nullptr, 0);
5210 perror_with_name ("interruptible_select");
5215 /* Save the thread's event and stop reason to process it later. */
5218 save_waitstatus (struct thread_info
*tp
, const target_waitstatus
&ws
)
5220 infrun_debug_printf ("saving status %s for %s",
5221 ws
.to_string ().c_str (),
5222 tp
->ptid
.to_string ().c_str ());
5224 /* Record for later. */
5225 tp
->set_pending_waitstatus (ws
);
5227 if (ws
.kind () == TARGET_WAITKIND_STOPPED
5228 && ws
.sig () == GDB_SIGNAL_TRAP
)
5230 struct regcache
*regcache
= get_thread_regcache (tp
);
5231 const address_space
*aspace
= regcache
->aspace ();
5232 CORE_ADDR pc
= regcache_read_pc (regcache
);
5234 adjust_pc_after_break (tp
, tp
->pending_waitstatus ());
5236 scoped_restore_current_thread restore_thread
;
5237 switch_to_thread (tp
);
5239 if (target_stopped_by_watchpoint ())
5240 tp
->set_stop_reason (TARGET_STOPPED_BY_WATCHPOINT
);
5241 else if (target_supports_stopped_by_sw_breakpoint ()
5242 && target_stopped_by_sw_breakpoint ())
5243 tp
->set_stop_reason (TARGET_STOPPED_BY_SW_BREAKPOINT
);
5244 else if (target_supports_stopped_by_hw_breakpoint ()
5245 && target_stopped_by_hw_breakpoint ())
5246 tp
->set_stop_reason (TARGET_STOPPED_BY_HW_BREAKPOINT
);
5247 else if (!target_supports_stopped_by_hw_breakpoint ()
5248 && hardware_breakpoint_inserted_here_p (aspace
, pc
))
5249 tp
->set_stop_reason (TARGET_STOPPED_BY_HW_BREAKPOINT
);
5250 else if (!target_supports_stopped_by_sw_breakpoint ()
5251 && software_breakpoint_inserted_here_p (aspace
, pc
))
5252 tp
->set_stop_reason (TARGET_STOPPED_BY_SW_BREAKPOINT
);
5253 else if (!thread_has_single_step_breakpoints_set (tp
)
5254 && currently_stepping (tp
))
5255 tp
->set_stop_reason (TARGET_STOPPED_BY_SINGLE_STEP
);
5259 /* Mark the non-executing threads accordingly. In all-stop, all
5260 threads of all processes are stopped when we get any event
5261 reported. In non-stop mode, only the event thread stops. */
5264 mark_non_executing_threads (process_stratum_target
*target
,
5266 const target_waitstatus
&ws
)
5270 if (!target_is_non_stop_p ())
5271 mark_ptid
= minus_one_ptid
;
5272 else if (ws
.kind () == TARGET_WAITKIND_SIGNALLED
5273 || ws
.kind () == TARGET_WAITKIND_EXITED
)
5275 /* If we're handling a process exit in non-stop mode, even
5276 though threads haven't been deleted yet, one would think
5277 that there is nothing to do, as threads of the dead process
5278 will be soon deleted, and threads of any other process were
5279 left running. However, on some targets, threads survive a
5280 process exit event. E.g., for the "checkpoint" command,
5281 when the current checkpoint/fork exits, linux-fork.c
5282 automatically switches to another fork from within
5283 target_mourn_inferior, by associating the same
5284 inferior/thread to another fork. We haven't mourned yet at
5285 this point, but we must mark any threads left in the
5286 process as not-executing so that finish_thread_state marks
5287 them stopped (in the user's perspective) if/when we present
5288 the stop to the user. */
5289 mark_ptid
= ptid_t (event_ptid
.pid ());
5292 mark_ptid
= event_ptid
;
5294 set_executing (target
, mark_ptid
, false);
5296 /* Likewise the resumed flag. */
5297 set_resumed (target
, mark_ptid
, false);
5300 /* Handle one event after stopping threads. If the eventing thread
5301 reports back any interesting event, we leave it pending. If the
5302 eventing thread was in the middle of a displaced step, we
5303 cancel/finish it, and unless the thread's inferior is being
5304 detached, put the thread back in the step-over chain. Returns true
5305 if there are no resumed threads left in the target (thus there's no
5306 point in waiting further), false otherwise. */
5309 handle_one (const wait_one_event
&event
)
5312 ("%s %s", event
.ws
.to_string ().c_str (),
5313 event
.ptid
.to_string ().c_str ());
5315 if (event
.ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
5317 /* All resumed threads exited. */
5320 else if (event
.ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
5321 || event
.ws
.kind () == TARGET_WAITKIND_EXITED
5322 || event
.ws
.kind () == TARGET_WAITKIND_SIGNALLED
)
5324 /* One thread/process exited/signalled. */
5326 thread_info
*t
= nullptr;
5328 /* The target may have reported just a pid. If so, try
5329 the first non-exited thread. */
5330 if (event
.ptid
.is_pid ())
5332 int pid
= event
.ptid
.pid ();
5333 inferior
*inf
= find_inferior_pid (event
.target
, pid
);
5334 for (thread_info
*tp
: inf
->non_exited_threads ())
5340 /* If there is no available thread, the event would
5341 have to be appended to a per-inferior event list,
5342 which does not exist (and if it did, we'd have
5343 to adjust run control command to be able to
5344 resume such an inferior). We assert here instead
5345 of going into an infinite loop. */
5346 gdb_assert (t
!= nullptr);
5349 ("using %s", t
->ptid
.to_string ().c_str ());
5353 t
= event
.target
->find_thread (event
.ptid
);
5354 /* Check if this is the first time we see this thread.
5355 Don't bother adding if it individually exited. */
5357 && event
.ws
.kind () != TARGET_WAITKIND_THREAD_EXITED
)
5358 t
= add_thread (event
.target
, event
.ptid
);
5363 /* Set the threads as non-executing to avoid
5364 another stop attempt on them. */
5365 switch_to_thread_no_regs (t
);
5366 mark_non_executing_threads (event
.target
, event
.ptid
,
5368 save_waitstatus (t
, event
.ws
);
5369 t
->stop_requested
= false;
5374 thread_info
*t
= event
.target
->find_thread (event
.ptid
);
5376 t
= add_thread (event
.target
, event
.ptid
);
5378 t
->stop_requested
= 0;
5379 t
->set_executing (false);
5380 t
->set_resumed (false);
5381 t
->control
.may_range_step
= 0;
5383 /* This may be the first time we see the inferior report
5385 if (t
->inf
->needs_setup
)
5387 switch_to_thread_no_regs (t
);
5391 if (event
.ws
.kind () == TARGET_WAITKIND_STOPPED
5392 && event
.ws
.sig () == GDB_SIGNAL_0
)
5394 /* We caught the event that we intended to catch, so
5395 there's no event to save as pending. */
5397 if (displaced_step_finish (t
, event
.ws
)
5398 == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED
)
5400 /* Add it back to the step-over queue. */
5402 ("displaced-step of %s canceled",
5403 t
->ptid
.to_string ().c_str ());
5405 t
->control
.trap_expected
= 0;
5406 if (!t
->inf
->detaching
)
5407 global_thread_step_over_chain_enqueue (t
);
5412 struct regcache
*regcache
;
5415 ("target_wait %s, saving status for %s",
5416 event
.ws
.to_string ().c_str (),
5417 t
->ptid
.to_string ().c_str ());
5419 /* Record for later. */
5420 save_waitstatus (t
, event
.ws
);
5422 if (displaced_step_finish (t
, event
.ws
)
5423 == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED
)
5425 /* Add it back to the step-over queue. */
5426 t
->control
.trap_expected
= 0;
5427 if (!t
->inf
->detaching
)
5428 global_thread_step_over_chain_enqueue (t
);
5431 regcache
= get_thread_regcache (t
);
5432 t
->set_stop_pc (regcache_read_pc (regcache
));
5434 infrun_debug_printf ("saved stop_pc=%s for %s "
5435 "(currently_stepping=%d)",
5436 paddress (current_inferior ()->arch (),
5438 t
->ptid
.to_string ().c_str (),
5439 currently_stepping (t
));
5449 stop_all_threads (const char *reason
, inferior
*inf
)
5451 /* We may need multiple passes to discover all threads. */
5455 gdb_assert (exists_non_stop_target ());
5457 INFRUN_SCOPED_DEBUG_START_END ("reason=%s, inf=%d", reason
,
5458 inf
!= nullptr ? inf
->num
: -1);
5460 infrun_debug_show_threads ("non-exited threads",
5461 all_non_exited_threads ());
5463 scoped_restore_current_thread restore_thread
;
5465 /* Enable thread events on relevant targets. */
5466 for (auto *target
: all_non_exited_process_targets ())
5468 if (inf
!= nullptr && inf
->process_target () != target
)
5471 switch_to_target_no_thread (target
);
5472 target_thread_events (true);
5477 /* Disable thread events on relevant targets. */
5478 for (auto *target
: all_non_exited_process_targets ())
5480 if (inf
!= nullptr && inf
->process_target () != target
)
5483 switch_to_target_no_thread (target
);
5484 target_thread_events (false);
5487 /* Use debug_prefixed_printf directly to get a meaningful function
5490 debug_prefixed_printf ("infrun", "stop_all_threads", "done");
5493 /* Request threads to stop, and then wait for the stops. Because
5494 threads we already know about can spawn more threads while we're
5495 trying to stop them, and we only learn about new threads when we
5496 update the thread list, do this in a loop, and keep iterating
5497 until two passes find no threads that need to be stopped. */
5498 for (pass
= 0; pass
< 2; pass
++, iterations
++)
5500 infrun_debug_printf ("pass=%d, iterations=%d", pass
, iterations
);
5503 int waits_needed
= 0;
5505 for (auto *target
: all_non_exited_process_targets ())
5507 if (inf
!= nullptr && inf
->process_target () != target
)
5510 switch_to_target_no_thread (target
);
5511 update_thread_list ();
5514 /* Go through all threads looking for threads that we need
5515 to tell the target to stop. */
5516 for (thread_info
*t
: all_non_exited_threads ())
5518 if (inf
!= nullptr && t
->inf
!= inf
)
5521 /* For a single-target setting with an all-stop target,
5522 we would not even arrive here. For a multi-target
5523 setting, until GDB is able to handle a mixture of
5524 all-stop and non-stop targets, simply skip all-stop
5525 targets' threads. This should be fine due to the
5526 protection of 'check_multi_target_resumption'. */
5528 switch_to_thread_no_regs (t
);
5529 if (!target_is_non_stop_p ())
5532 if (t
->executing ())
5534 /* If already stopping, don't request a stop again.
5535 We just haven't seen the notification yet. */
5536 if (!t
->stop_requested
)
5538 infrun_debug_printf (" %s executing, need stop",
5539 t
->ptid
.to_string ().c_str ());
5540 target_stop (t
->ptid
);
5541 t
->stop_requested
= 1;
5545 infrun_debug_printf (" %s executing, already stopping",
5546 t
->ptid
.to_string ().c_str ());
5549 if (t
->stop_requested
)
5554 infrun_debug_printf (" %s not executing",
5555 t
->ptid
.to_string ().c_str ());
5557 /* The thread may be not executing, but still be
5558 resumed with a pending status to process. */
5559 t
->set_resumed (false);
5563 if (waits_needed
== 0)
5566 /* If we find new threads on the second iteration, restart
5567 over. We want to see two iterations in a row with all
5572 for (int i
= 0; i
< waits_needed
; i
++)
5574 wait_one_event event
= wait_one ();
5575 if (handle_one (event
))
5582 /* Handle a TARGET_WAITKIND_NO_RESUMED event. */
5585 handle_no_resumed (struct execution_control_state
*ecs
)
5587 if (target_can_async_p ())
5589 bool any_sync
= false;
5591 for (ui
*ui
: all_uis ())
5593 if (ui
->prompt_state
== PROMPT_BLOCKED
)
5601 /* There were no unwaited-for children left in the target, but,
5602 we're not synchronously waiting for events either. Just
5605 infrun_debug_printf ("TARGET_WAITKIND_NO_RESUMED (ignoring: bg)");
5606 prepare_to_wait (ecs
);
5611 /* Otherwise, if we were running a synchronous execution command, we
5612 may need to cancel it and give the user back the terminal.
5614 In non-stop mode, the target can't tell whether we've already
5615 consumed previous stop events, so it can end up sending us a
5616 no-resumed event like so:
5618 #0 - thread 1 is left stopped
5620 #1 - thread 2 is resumed and hits breakpoint
5621 -> TARGET_WAITKIND_STOPPED
5623 #2 - thread 3 is resumed and exits
5624 this is the last resumed thread, so
5625 -> TARGET_WAITKIND_NO_RESUMED
5627 #3 - gdb processes stop for thread 2 and decides to re-resume
5630 #4 - gdb processes the TARGET_WAITKIND_NO_RESUMED event.
5631 thread 2 is now resumed, so the event should be ignored.
5633 IOW, if the stop for thread 2 doesn't end a foreground command,
5634 then we need to ignore the following TARGET_WAITKIND_NO_RESUMED
5635 event. But it could be that the event meant that thread 2 itself
5636 (or whatever other thread was the last resumed thread) exited.
5638 To address this we refresh the thread list and check whether we
5639 have resumed threads _now_. In the example above, this removes
5640 thread 3 from the thread list. If thread 2 was re-resumed, we
5641 ignore this event. If we find no thread resumed, then we cancel
5642 the synchronous command and show "no unwaited-for " to the
5645 inferior
*curr_inf
= current_inferior ();
5647 scoped_restore_current_thread restore_thread
;
5648 update_thread_list ();
5652 - the current target has no thread executing, and
5653 - the current inferior is native, and
5654 - the current inferior is the one which has the terminal, and
5657 then a Ctrl-C from this point on would remain stuck in the
5658 kernel, until a thread resumes and dequeues it. That would
5659 result in the GDB CLI not reacting to Ctrl-C, not able to
5660 interrupt the program. To address this, if the current inferior
5661 no longer has any thread executing, we give the terminal to some
5662 other inferior that has at least one thread executing. */
5663 bool swap_terminal
= true;
5665 /* Whether to ignore this TARGET_WAITKIND_NO_RESUMED event, or
5666 whether to report it to the user. */
5667 bool ignore_event
= false;
5669 for (thread_info
*thread
: all_non_exited_threads ())
5671 if (swap_terminal
&& thread
->executing ())
5673 if (thread
->inf
!= curr_inf
)
5675 target_terminal::ours ();
5677 switch_to_thread (thread
);
5678 target_terminal::inferior ();
5680 swap_terminal
= false;
5683 if (!ignore_event
&& thread
->resumed ())
5685 /* Either there were no unwaited-for children left in the
5686 target at some point, but there are now, or some target
5687 other than the eventing one has unwaited-for children
5688 left. Just ignore. */
5689 infrun_debug_printf ("TARGET_WAITKIND_NO_RESUMED "
5690 "(ignoring: found resumed)");
5692 ignore_event
= true;
5695 if (ignore_event
&& !swap_terminal
)
5701 switch_to_inferior_no_thread (curr_inf
);
5702 prepare_to_wait (ecs
);
5706 /* Go ahead and report the event. */
5710 /* Given an execution control state that has been freshly filled in by
5711 an event from the inferior, figure out what it means and take
5714 The alternatives are:
5716 1) stop_waiting and return; to really stop and return to the
5719 2) keep_going and return; to wait for the next event (set
5720 ecs->event_thread->stepping_over_breakpoint to 1 to single step
5724 handle_inferior_event (struct execution_control_state
*ecs
)
5726 /* Make sure that all temporary struct value objects that were
5727 created during the handling of the event get deleted at the
5729 scoped_value_mark free_values
;
5731 infrun_debug_printf ("%s", ecs
->ws
.to_string ().c_str ());
5733 if (ecs
->ws
.kind () == TARGET_WAITKIND_IGNORE
)
5735 /* We had an event in the inferior, but we are not interested in
5736 handling it at this level. The lower layers have already
5737 done what needs to be done, if anything.
5739 One of the possible circumstances for this is when the
5740 inferior produces output for the console. The inferior has
5741 not stopped, and we are ignoring the event. Another possible
5742 circumstance is any event which the lower level knows will be
5743 reported multiple times without an intervening resume. */
5744 prepare_to_wait (ecs
);
5748 if (ecs
->ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
)
5750 ecs
->event_thread
= ecs
->target
->find_thread (ecs
->ptid
);
5751 gdb_assert (ecs
->event_thread
!= nullptr);
5752 delete_thread (ecs
->event_thread
);
5753 prepare_to_wait (ecs
);
5757 if (ecs
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
5758 && handle_no_resumed (ecs
))
5761 /* Cache the last target/ptid/waitstatus. */
5762 set_last_target_status (ecs
->target
, ecs
->ptid
, ecs
->ws
);
5764 /* Always clear state belonging to the previous time we stopped. */
5765 stop_stack_dummy
= STOP_NONE
;
5767 if (ecs
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
5769 /* No unwaited-for children left. IOW, all resumed children
5771 stop_print_frame
= false;
5776 if (ecs
->ws
.kind () != TARGET_WAITKIND_EXITED
5777 && ecs
->ws
.kind () != TARGET_WAITKIND_SIGNALLED
)
5779 ecs
->event_thread
= ecs
->target
->find_thread (ecs
->ptid
);
5780 /* If it's a new thread, add it to the thread database. */
5781 if (ecs
->event_thread
== nullptr)
5782 ecs
->event_thread
= add_thread (ecs
->target
, ecs
->ptid
);
5784 /* Disable range stepping. If the next step request could use a
5785 range, this will be end up re-enabled then. */
5786 ecs
->event_thread
->control
.may_range_step
= 0;
5789 /* Dependent on valid ECS->EVENT_THREAD. */
5790 adjust_pc_after_break (ecs
->event_thread
, ecs
->ws
);
5792 /* Dependent on the current PC value modified by adjust_pc_after_break. */
5793 reinit_frame_cache ();
5795 breakpoint_retire_moribund ();
5797 /* First, distinguish signals caused by the debugger from signals
5798 that have to do with the program's own actions. Note that
5799 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
5800 on the operating system version. Here we detect when a SIGILL or
5801 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
5802 something similar for SIGSEGV, since a SIGSEGV will be generated
5803 when we're trying to execute a breakpoint instruction on a
5804 non-executable stack. This happens for call dummy breakpoints
5805 for architectures like SPARC that place call dummies on the
5807 if (ecs
->ws
.kind () == TARGET_WAITKIND_STOPPED
5808 && (ecs
->ws
.sig () == GDB_SIGNAL_ILL
5809 || ecs
->ws
.sig () == GDB_SIGNAL_SEGV
5810 || ecs
->ws
.sig () == GDB_SIGNAL_EMT
))
5812 struct regcache
*regcache
= get_thread_regcache (ecs
->event_thread
);
5814 if (breakpoint_inserted_here_p (regcache
->aspace (),
5815 regcache_read_pc (regcache
)))
5817 infrun_debug_printf ("Treating signal as SIGTRAP");
5818 ecs
->ws
.set_stopped (GDB_SIGNAL_TRAP
);
5822 mark_non_executing_threads (ecs
->target
, ecs
->ptid
, ecs
->ws
);
5824 switch (ecs
->ws
.kind ())
5826 case TARGET_WAITKIND_LOADED
:
5828 context_switch (ecs
);
5829 /* Ignore gracefully during startup of the inferior, as it might
5830 be the shell which has just loaded some objects, otherwise
5831 add the symbols for the newly loaded objects. Also ignore at
5832 the beginning of an attach or remote session; we will query
5833 the full list of libraries once the connection is
5836 stop_kind stop_soon
= get_inferior_stop_soon (ecs
);
5837 if (stop_soon
== NO_STOP_QUIETLY
)
5839 struct regcache
*regcache
;
5841 regcache
= get_thread_regcache (ecs
->event_thread
);
5843 handle_solib_event ();
5845 ecs
->event_thread
->set_stop_pc (regcache_read_pc (regcache
));
5846 ecs
->event_thread
->control
.stop_bpstat
5847 = bpstat_stop_status_nowatch (regcache
->aspace (),
5848 ecs
->event_thread
->stop_pc (),
5849 ecs
->event_thread
, ecs
->ws
);
5851 if (handle_stop_requested (ecs
))
5854 if (bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
5856 /* A catchpoint triggered. */
5857 process_event_stop_test (ecs
);
5861 /* If requested, stop when the dynamic linker notifies
5862 gdb of events. This allows the user to get control
5863 and place breakpoints in initializer routines for
5864 dynamically loaded objects (among other things). */
5865 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
5866 if (stop_on_solib_events
)
5868 /* Make sure we print "Stopped due to solib-event" in
5870 stop_print_frame
= true;
5877 /* If we are skipping through a shell, or through shared library
5878 loading that we aren't interested in, resume the program. If
5879 we're running the program normally, also resume. */
5880 if (stop_soon
== STOP_QUIETLY
|| stop_soon
== NO_STOP_QUIETLY
)
5882 /* Loading of shared libraries might have changed breakpoint
5883 addresses. Make sure new breakpoints are inserted. */
5884 if (stop_soon
== NO_STOP_QUIETLY
)
5885 insert_breakpoints ();
5886 resume (GDB_SIGNAL_0
);
5887 prepare_to_wait (ecs
);
5891 /* But stop if we're attaching or setting up a remote
5893 if (stop_soon
== STOP_QUIETLY_NO_SIGSTOP
5894 || stop_soon
== STOP_QUIETLY_REMOTE
)
5896 infrun_debug_printf ("quietly stopped");
5901 internal_error (_("unhandled stop_soon: %d"), (int) stop_soon
);
5904 case TARGET_WAITKIND_SPURIOUS
:
5905 if (handle_stop_requested (ecs
))
5907 context_switch (ecs
);
5908 resume (GDB_SIGNAL_0
);
5909 prepare_to_wait (ecs
);
5912 case TARGET_WAITKIND_THREAD_CREATED
:
5913 if (handle_stop_requested (ecs
))
5915 context_switch (ecs
);
5916 if (!switch_back_to_stepped_thread (ecs
))
5920 case TARGET_WAITKIND_EXITED
:
5921 case TARGET_WAITKIND_SIGNALLED
:
5923 /* Depending on the system, ecs->ptid may point to a thread or
5924 to a process. On some targets, target_mourn_inferior may
5925 need to have access to the just-exited thread. That is the
5926 case of GNU/Linux's "checkpoint" support, for example.
5927 Call the switch_to_xxx routine as appropriate. */
5928 thread_info
*thr
= ecs
->target
->find_thread (ecs
->ptid
);
5930 switch_to_thread (thr
);
5933 inferior
*inf
= find_inferior_ptid (ecs
->target
, ecs
->ptid
);
5934 switch_to_inferior_no_thread (inf
);
5937 handle_vfork_child_exec_or_exit (0);
5938 target_terminal::ours (); /* Must do this before mourn anyway. */
5940 /* Clearing any previous state of convenience variables. */
5941 clear_exit_convenience_vars ();
5943 if (ecs
->ws
.kind () == TARGET_WAITKIND_EXITED
)
5945 /* Record the exit code in the convenience variable $_exitcode, so
5946 that the user can inspect this again later. */
5947 set_internalvar_integer (lookup_internalvar ("_exitcode"),
5948 (LONGEST
) ecs
->ws
.exit_status ());
5950 /* Also record this in the inferior itself. */
5951 current_inferior ()->has_exit_code
= true;
5952 current_inferior ()->exit_code
= (LONGEST
) ecs
->ws
.exit_status ();
5954 /* Support the --return-child-result option. */
5955 return_child_result_value
= ecs
->ws
.exit_status ();
5957 interps_notify_exited (ecs
->ws
.exit_status ());
5961 struct gdbarch
*gdbarch
= current_inferior ()->arch ();
5963 if (gdbarch_gdb_signal_to_target_p (gdbarch
))
5965 /* Set the value of the internal variable $_exitsignal,
5966 which holds the signal uncaught by the inferior. */
5967 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
5968 gdbarch_gdb_signal_to_target (gdbarch
,
5973 /* We don't have access to the target's method used for
5974 converting between signal numbers (GDB's internal
5975 representation <-> target's representation).
5976 Therefore, we cannot do a good job at displaying this
5977 information to the user. It's better to just warn
5978 her about it (if infrun debugging is enabled), and
5980 infrun_debug_printf ("Cannot fill $_exitsignal with the correct "
5984 interps_notify_signal_exited (ecs
->ws
.sig ());
5987 gdb_flush (gdb_stdout
);
5988 target_mourn_inferior (inferior_ptid
);
5989 stop_print_frame
= false;
5993 case TARGET_WAITKIND_FORKED
:
5994 case TARGET_WAITKIND_VFORKED
:
5995 case TARGET_WAITKIND_THREAD_CLONED
:
5997 displaced_step_finish (ecs
->event_thread
, ecs
->ws
);
5999 /* Start a new step-over in another thread if there's one that
6003 context_switch (ecs
);
6005 /* Immediately detach breakpoints from the child before there's
6006 any chance of letting the user delete breakpoints from the
6007 breakpoint lists. If we don't do this early, it's easy to
6008 leave left over traps in the child, vis: "break foo; catch
6009 fork; c; <fork>; del; c; <child calls foo>". We only follow
6010 the fork on the last `continue', and by that time the
6011 breakpoint at "foo" is long gone from the breakpoint table.
6012 If we vforked, then we don't need to unpatch here, since both
6013 parent and child are sharing the same memory pages; we'll
6014 need to unpatch at follow/detach time instead to be certain
6015 that new breakpoints added between catchpoint hit time and
6016 vfork follow are detached. */
6017 if (ecs
->ws
.kind () == TARGET_WAITKIND_FORKED
)
6019 /* This won't actually modify the breakpoint list, but will
6020 physically remove the breakpoints from the child. */
6021 detach_breakpoints (ecs
->ws
.child_ptid ());
6024 delete_just_stopped_threads_single_step_breakpoints ();
6026 /* In case the event is caught by a catchpoint, remember that
6027 the event is to be followed at the next resume of the thread,
6028 and not immediately. */
6029 ecs
->event_thread
->pending_follow
= ecs
->ws
;
6031 ecs
->event_thread
->set_stop_pc
6032 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
6034 ecs
->event_thread
->control
.stop_bpstat
6035 = bpstat_stop_status_nowatch (get_current_regcache ()->aspace (),
6036 ecs
->event_thread
->stop_pc (),
6037 ecs
->event_thread
, ecs
->ws
);
6039 if (handle_stop_requested (ecs
))
6042 /* If no catchpoint triggered for this, then keep going. Note
6043 that we're interested in knowing the bpstat actually causes a
6044 stop, not just if it may explain the signal. Software
6045 watchpoints, for example, always appear in the bpstat. */
6046 if (!bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
6049 = (ecs
->ws
.kind () != TARGET_WAITKIND_THREAD_CLONED
6050 && follow_fork_mode_string
== follow_fork_mode_child
);
6052 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6054 process_stratum_target
*targ
6055 = ecs
->event_thread
->inf
->process_target ();
6058 if (ecs
->ws
.kind () != TARGET_WAITKIND_THREAD_CLONED
)
6059 should_resume
= follow_fork ();
6062 should_resume
= true;
6063 inferior
*inf
= ecs
->event_thread
->inf
;
6064 inf
->top_target ()->follow_clone (ecs
->ws
.child_ptid ());
6065 ecs
->event_thread
->pending_follow
.set_spurious ();
6068 /* Note that one of these may be an invalid pointer,
6069 depending on detach_fork. */
6070 thread_info
*parent
= ecs
->event_thread
;
6071 thread_info
*child
= targ
->find_thread (ecs
->ws
.child_ptid ());
6073 /* At this point, the parent is marked running, and the
6074 child is marked stopped. */
6076 /* If not resuming the parent, mark it stopped. */
6077 if (ecs
->ws
.kind () != TARGET_WAITKIND_THREAD_CLONED
6078 && follow_child
&& !detach_fork
&& !non_stop
&& !sched_multi
)
6079 parent
->set_running (false);
6081 /* If resuming the child, mark it running. */
6082 if (ecs
->ws
.kind () == TARGET_WAITKIND_THREAD_CLONED
6083 || (follow_child
|| (!detach_fork
&& (non_stop
|| sched_multi
))))
6084 child
->set_running (true);
6086 /* In non-stop mode, also resume the other branch. */
6087 if ((ecs
->ws
.kind () == TARGET_WAITKIND_THREAD_CLONED
6088 && target_is_non_stop_p ())
6089 || (!detach_fork
&& (non_stop
6091 && target_is_non_stop_p ()))))
6094 switch_to_thread (parent
);
6096 switch_to_thread (child
);
6098 ecs
->event_thread
= inferior_thread ();
6099 ecs
->ptid
= inferior_ptid
;
6104 switch_to_thread (child
);
6106 switch_to_thread (parent
);
6108 ecs
->event_thread
= inferior_thread ();
6109 ecs
->ptid
= inferior_ptid
;
6113 /* Never call switch_back_to_stepped_thread if we are waiting for
6114 vfork-done (waiting for an external vfork child to exec or
6115 exit). We will resume only the vforking thread for the purpose
6116 of collecting the vfork-done event, and we will restart any
6117 step once the critical shared address space window is done. */
6120 && parent
->inf
->thread_waiting_for_vfork_done
!= nullptr)
6121 || !switch_back_to_stepped_thread (ecs
))
6128 process_event_stop_test (ecs
);
6131 case TARGET_WAITKIND_VFORK_DONE
:
6132 /* Done with the shared memory region. Re-insert breakpoints in
6133 the parent, and keep going. */
6135 context_switch (ecs
);
6137 handle_vfork_done (ecs
->event_thread
);
6138 gdb_assert (inferior_thread () == ecs
->event_thread
);
6140 if (handle_stop_requested (ecs
))
6143 if (!switch_back_to_stepped_thread (ecs
))
6145 gdb_assert (inferior_thread () == ecs
->event_thread
);
6146 /* This also takes care of reinserting breakpoints in the
6147 previously locked inferior. */
6152 case TARGET_WAITKIND_EXECD
:
6154 /* Note we can't read registers yet (the stop_pc), because we
6155 don't yet know the inferior's post-exec architecture.
6156 'stop_pc' is explicitly read below instead. */
6157 switch_to_thread_no_regs (ecs
->event_thread
);
6159 /* Do whatever is necessary to the parent branch of the vfork. */
6160 handle_vfork_child_exec_or_exit (1);
6162 /* This causes the eventpoints and symbol table to be reset.
6163 Must do this now, before trying to determine whether to
6165 follow_exec (inferior_ptid
, ecs
->ws
.execd_pathname ());
6167 /* In follow_exec we may have deleted the original thread and
6168 created a new one. Make sure that the event thread is the
6169 execd thread for that case (this is a nop otherwise). */
6170 ecs
->event_thread
= inferior_thread ();
6172 ecs
->event_thread
->set_stop_pc
6173 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
6175 ecs
->event_thread
->control
.stop_bpstat
6176 = bpstat_stop_status_nowatch (get_current_regcache ()->aspace (),
6177 ecs
->event_thread
->stop_pc (),
6178 ecs
->event_thread
, ecs
->ws
);
6180 if (handle_stop_requested (ecs
))
6183 /* If no catchpoint triggered for this, then keep going. */
6184 if (!bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
6186 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6190 process_event_stop_test (ecs
);
6193 /* Be careful not to try to gather much state about a thread
6194 that's in a syscall. It's frequently a losing proposition. */
6195 case TARGET_WAITKIND_SYSCALL_ENTRY
:
6196 /* Getting the current syscall number. */
6197 if (handle_syscall_event (ecs
) == 0)
6198 process_event_stop_test (ecs
);
6201 /* Before examining the threads further, step this thread to
6202 get it entirely out of the syscall. (We get notice of the
6203 event when the thread is just on the verge of exiting a
6204 syscall. Stepping one instruction seems to get it back
6206 case TARGET_WAITKIND_SYSCALL_RETURN
:
6207 if (handle_syscall_event (ecs
) == 0)
6208 process_event_stop_test (ecs
);
6211 case TARGET_WAITKIND_STOPPED
:
6212 handle_signal_stop (ecs
);
6215 case TARGET_WAITKIND_NO_HISTORY
:
6216 /* Reverse execution: target ran out of history info. */
6218 /* Switch to the stopped thread. */
6219 context_switch (ecs
);
6220 infrun_debug_printf ("stopped");
6222 delete_just_stopped_threads_single_step_breakpoints ();
6223 ecs
->event_thread
->set_stop_pc
6224 (regcache_read_pc (get_thread_regcache (inferior_thread ())));
6226 if (handle_stop_requested (ecs
))
6229 interps_notify_no_history ();
6235 /* Restart threads back to what they were trying to do back when we
6236 paused them (because of an in-line step-over or vfork, for example).
6237 The EVENT_THREAD thread is ignored (not restarted).
6239 If INF is non-nullptr, only resume threads from INF. */
6242 restart_threads (struct thread_info
*event_thread
, inferior
*inf
)
6244 INFRUN_SCOPED_DEBUG_START_END ("event_thread=%s, inf=%d",
6245 event_thread
->ptid
.to_string ().c_str (),
6246 inf
!= nullptr ? inf
->num
: -1);
6248 gdb_assert (!step_over_info_valid_p ());
6250 /* In case the instruction just stepped spawned a new thread. */
6251 update_thread_list ();
6253 for (thread_info
*tp
: all_non_exited_threads ())
6255 if (inf
!= nullptr && tp
->inf
!= inf
)
6258 if (tp
->inf
->detaching
)
6260 infrun_debug_printf ("restart threads: [%s] inferior detaching",
6261 tp
->ptid
.to_string ().c_str ());
6265 switch_to_thread_no_regs (tp
);
6267 if (tp
== event_thread
)
6269 infrun_debug_printf ("restart threads: [%s] is event thread",
6270 tp
->ptid
.to_string ().c_str ());
6274 if (!(tp
->state
== THREAD_RUNNING
|| tp
->control
.in_infcall
))
6276 infrun_debug_printf ("restart threads: [%s] not meant to be running",
6277 tp
->ptid
.to_string ().c_str ());
6283 infrun_debug_printf ("restart threads: [%s] resumed",
6284 tp
->ptid
.to_string ().c_str ());
6285 gdb_assert (tp
->executing () || tp
->has_pending_waitstatus ());
6289 if (thread_is_in_step_over_chain (tp
))
6291 infrun_debug_printf ("restart threads: [%s] needs step-over",
6292 tp
->ptid
.to_string ().c_str ());
6293 gdb_assert (!tp
->resumed ());
6298 if (tp
->has_pending_waitstatus ())
6300 infrun_debug_printf ("restart threads: [%s] has pending status",
6301 tp
->ptid
.to_string ().c_str ());
6302 tp
->set_resumed (true);
6306 gdb_assert (!tp
->stop_requested
);
6308 /* If some thread needs to start a step-over at this point, it
6309 should still be in the step-over queue, and thus skipped
6311 if (thread_still_needs_step_over (tp
))
6313 internal_error ("thread [%s] needs a step-over, but not in "
6314 "step-over queue\n",
6315 tp
->ptid
.to_string ().c_str ());
6318 if (currently_stepping (tp
))
6320 infrun_debug_printf ("restart threads: [%s] was stepping",
6321 tp
->ptid
.to_string ().c_str ());
6322 keep_going_stepped_thread (tp
);
6326 infrun_debug_printf ("restart threads: [%s] continuing",
6327 tp
->ptid
.to_string ().c_str ());
6328 execution_control_state
ecs (tp
);
6329 switch_to_thread (tp
);
6330 keep_going_pass_signal (&ecs
);
6335 /* Callback for iterate_over_threads. Find a resumed thread that has
6336 a pending waitstatus. */
6339 resumed_thread_with_pending_status (struct thread_info
*tp
,
6342 return tp
->resumed () && tp
->has_pending_waitstatus ();
6345 /* Called when we get an event that may finish an in-line or
6346 out-of-line (displaced stepping) step-over started previously.
6347 Return true if the event is processed and we should go back to the
6348 event loop; false if the caller should continue processing the
6352 finish_step_over (struct execution_control_state
*ecs
)
6354 displaced_step_finish (ecs
->event_thread
, ecs
->ws
);
6356 bool had_step_over_info
= step_over_info_valid_p ();
6358 if (had_step_over_info
)
6360 /* If we're stepping over a breakpoint with all threads locked,
6361 then only the thread that was stepped should be reporting
6363 gdb_assert (ecs
->event_thread
->control
.trap_expected
);
6365 update_thread_events_after_step_over (ecs
->event_thread
);
6367 clear_step_over_info ();
6370 if (!target_is_non_stop_p ())
6373 /* Start a new step-over in another thread if there's one that
6377 /* If we were stepping over a breakpoint before, and haven't started
6378 a new in-line step-over sequence, then restart all other threads
6379 (except the event thread). We can't do this in all-stop, as then
6380 e.g., we wouldn't be able to issue any other remote packet until
6381 these other threads stop. */
6382 if (had_step_over_info
&& !step_over_info_valid_p ())
6384 struct thread_info
*pending
;
6386 /* If we only have threads with pending statuses, the restart
6387 below won't restart any thread and so nothing re-inserts the
6388 breakpoint we just stepped over. But we need it inserted
6389 when we later process the pending events, otherwise if
6390 another thread has a pending event for this breakpoint too,
6391 we'd discard its event (because the breakpoint that
6392 originally caused the event was no longer inserted). */
6393 context_switch (ecs
);
6394 insert_breakpoints ();
6396 restart_threads (ecs
->event_thread
);
6398 /* If we have events pending, go through handle_inferior_event
6399 again, picking up a pending event at random. This avoids
6400 thread starvation. */
6402 /* But not if we just stepped over a watchpoint in order to let
6403 the instruction execute so we can evaluate its expression.
6404 The set of watchpoints that triggered is recorded in the
6405 breakpoint objects themselves (see bp->watchpoint_triggered).
6406 If we processed another event first, that other event could
6407 clobber this info. */
6408 if (ecs
->event_thread
->stepping_over_watchpoint
)
6411 pending
= iterate_over_threads (resumed_thread_with_pending_status
,
6413 if (pending
!= nullptr)
6415 struct thread_info
*tp
= ecs
->event_thread
;
6416 struct regcache
*regcache
;
6418 infrun_debug_printf ("found resumed threads with "
6419 "pending events, saving status");
6421 gdb_assert (pending
!= tp
);
6423 /* Record the event thread's event for later. */
6424 save_waitstatus (tp
, ecs
->ws
);
6425 /* This was cleared early, by handle_inferior_event. Set it
6426 so this pending event is considered by
6428 tp
->set_resumed (true);
6430 gdb_assert (!tp
->executing ());
6432 regcache
= get_thread_regcache (tp
);
6433 tp
->set_stop_pc (regcache_read_pc (regcache
));
6435 infrun_debug_printf ("saved stop_pc=%s for %s "
6436 "(currently_stepping=%d)",
6437 paddress (current_inferior ()->arch (),
6439 tp
->ptid
.to_string ().c_str (),
6440 currently_stepping (tp
));
6442 /* This in-line step-over finished; clear this so we won't
6443 start a new one. This is what handle_signal_stop would
6444 do, if we returned false. */
6445 tp
->stepping_over_breakpoint
= 0;
6447 /* Wake up the event loop again. */
6448 mark_async_event_handler (infrun_async_inferior_event_token
);
6450 prepare_to_wait (ecs
);
6461 notify_signal_received (gdb_signal sig
)
6463 interps_notify_signal_received (sig
);
6464 gdb::observers::signal_received
.notify (sig
);
6470 notify_normal_stop (bpstat
*bs
, int print_frame
)
6472 interps_notify_normal_stop (bs
, print_frame
);
6473 gdb::observers::normal_stop
.notify (bs
, print_frame
);
6478 void notify_user_selected_context_changed (user_selected_what selection
)
6480 interps_notify_user_selected_context_changed (selection
);
6481 gdb::observers::user_selected_context_changed
.notify (selection
);
6484 /* Come here when the program has stopped with a signal. */
6487 handle_signal_stop (struct execution_control_state
*ecs
)
6489 frame_info_ptr frame
;
6490 struct gdbarch
*gdbarch
;
6491 int stopped_by_watchpoint
;
6492 enum stop_kind stop_soon
;
6495 gdb_assert (ecs
->ws
.kind () == TARGET_WAITKIND_STOPPED
);
6497 ecs
->event_thread
->set_stop_signal (ecs
->ws
.sig ());
6499 /* Do we need to clean up the state of a thread that has
6500 completed a displaced single-step? (Doing so usually affects
6501 the PC, so do it here, before we set stop_pc.) */
6502 if (finish_step_over (ecs
))
6505 /* If we either finished a single-step or hit a breakpoint, but
6506 the user wanted this thread to be stopped, pretend we got a
6507 SIG0 (generic unsignaled stop). */
6508 if (ecs
->event_thread
->stop_requested
6509 && ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
)
6510 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6512 ecs
->event_thread
->set_stop_pc
6513 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
6515 context_switch (ecs
);
6517 if (deprecated_context_hook
)
6518 deprecated_context_hook (ecs
->event_thread
->global_num
);
6522 struct regcache
*regcache
= get_thread_regcache (ecs
->event_thread
);
6523 struct gdbarch
*reg_gdbarch
= regcache
->arch ();
6526 ("stop_pc=%s", paddress (reg_gdbarch
, ecs
->event_thread
->stop_pc ()));
6527 if (target_stopped_by_watchpoint ())
6531 infrun_debug_printf ("stopped by watchpoint");
6533 if (target_stopped_data_address (current_inferior ()->top_target (),
6535 infrun_debug_printf ("stopped data address=%s",
6536 paddress (reg_gdbarch
, addr
));
6538 infrun_debug_printf ("(no data address available)");
6542 /* This is originated from start_remote(), start_inferior() and
6543 shared libraries hook functions. */
6544 stop_soon
= get_inferior_stop_soon (ecs
);
6545 if (stop_soon
== STOP_QUIETLY
|| stop_soon
== STOP_QUIETLY_REMOTE
)
6547 infrun_debug_printf ("quietly stopped");
6548 stop_print_frame
= true;
6553 /* This originates from attach_command(). We need to overwrite
6554 the stop_signal here, because some kernels don't ignore a
6555 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
6556 See more comments in inferior.h. On the other hand, if we
6557 get a non-SIGSTOP, report it to the user - assume the backend
6558 will handle the SIGSTOP if it should show up later.
6560 Also consider that the attach is complete when we see a
6561 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
6562 target extended-remote report it instead of a SIGSTOP
6563 (e.g. gdbserver). We already rely on SIGTRAP being our
6564 signal, so this is no exception.
6566 Also consider that the attach is complete when we see a
6567 GDB_SIGNAL_0. In non-stop mode, GDB will explicitly tell
6568 the target to stop all threads of the inferior, in case the
6569 low level attach operation doesn't stop them implicitly. If
6570 they weren't stopped implicitly, then the stub will report a
6571 GDB_SIGNAL_0, meaning: stopped for no particular reason
6572 other than GDB's request. */
6573 if (stop_soon
== STOP_QUIETLY_NO_SIGSTOP
6574 && (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_STOP
6575 || ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6576 || ecs
->event_thread
->stop_signal () == GDB_SIGNAL_0
))
6578 stop_print_frame
= true;
6580 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6584 /* At this point, get hold of the now-current thread's frame. */
6585 frame
= get_current_frame ();
6586 gdbarch
= get_frame_arch (frame
);
6588 /* Pull the single step breakpoints out of the target. */
6589 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
)
6591 struct regcache
*regcache
;
6594 regcache
= get_thread_regcache (ecs
->event_thread
);
6595 const address_space
*aspace
= regcache
->aspace ();
6597 pc
= regcache_read_pc (regcache
);
6599 /* However, before doing so, if this single-step breakpoint was
6600 actually for another thread, set this thread up for moving
6602 if (!thread_has_single_step_breakpoint_here (ecs
->event_thread
,
6605 if (single_step_breakpoint_inserted_here_p (aspace
, pc
))
6607 infrun_debug_printf ("[%s] hit another thread's single-step "
6609 ecs
->ptid
.to_string ().c_str ());
6610 ecs
->hit_singlestep_breakpoint
= 1;
6615 infrun_debug_printf ("[%s] hit its single-step breakpoint",
6616 ecs
->ptid
.to_string ().c_str ());
6619 delete_just_stopped_threads_single_step_breakpoints ();
6621 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6622 && ecs
->event_thread
->control
.trap_expected
6623 && ecs
->event_thread
->stepping_over_watchpoint
)
6624 stopped_by_watchpoint
= 0;
6626 stopped_by_watchpoint
= watchpoints_triggered (ecs
->ws
);
6628 /* If necessary, step over this watchpoint. We'll be back to display
6630 if (stopped_by_watchpoint
6631 && (target_have_steppable_watchpoint ()
6632 || gdbarch_have_nonsteppable_watchpoint (gdbarch
)))
6634 /* At this point, we are stopped at an instruction which has
6635 attempted to write to a piece of memory under control of
6636 a watchpoint. The instruction hasn't actually executed
6637 yet. If we were to evaluate the watchpoint expression
6638 now, we would get the old value, and therefore no change
6639 would seem to have occurred.
6641 In order to make watchpoints work `right', we really need
6642 to complete the memory write, and then evaluate the
6643 watchpoint expression. We do this by single-stepping the
6646 It may not be necessary to disable the watchpoint to step over
6647 it. For example, the PA can (with some kernel cooperation)
6648 single step over a watchpoint without disabling the watchpoint.
6650 It is far more common to need to disable a watchpoint to step
6651 the inferior over it. If we have non-steppable watchpoints,
6652 we must disable the current watchpoint; it's simplest to
6653 disable all watchpoints.
6655 Any breakpoint at PC must also be stepped over -- if there's
6656 one, it will have already triggered before the watchpoint
6657 triggered, and we either already reported it to the user, or
6658 it didn't cause a stop and we called keep_going. In either
6659 case, if there was a breakpoint at PC, we must be trying to
6661 ecs
->event_thread
->stepping_over_watchpoint
= 1;
6666 ecs
->event_thread
->stepping_over_breakpoint
= 0;
6667 ecs
->event_thread
->stepping_over_watchpoint
= 0;
6668 bpstat_clear (&ecs
->event_thread
->control
.stop_bpstat
);
6669 ecs
->event_thread
->control
.stop_step
= 0;
6670 stop_print_frame
= true;
6671 stopped_by_random_signal
= 0;
6672 bpstat
*stop_chain
= nullptr;
6674 /* Hide inlined functions starting here, unless we just performed stepi or
6675 nexti. After stepi and nexti, always show the innermost frame (not any
6676 inline function call sites). */
6677 if (ecs
->event_thread
->control
.step_range_end
!= 1)
6679 const address_space
*aspace
6680 = get_thread_regcache (ecs
->event_thread
)->aspace ();
6682 /* skip_inline_frames is expensive, so we avoid it if we can
6683 determine that the address is one where functions cannot have
6684 been inlined. This improves performance with inferiors that
6685 load a lot of shared libraries, because the solib event
6686 breakpoint is defined as the address of a function (i.e. not
6687 inline). Note that we have to check the previous PC as well
6688 as the current one to catch cases when we have just
6689 single-stepped off a breakpoint prior to reinstating it.
6690 Note that we're assuming that the code we single-step to is
6691 not inline, but that's not definitive: there's nothing
6692 preventing the event breakpoint function from containing
6693 inlined code, and the single-step ending up there. If the
6694 user had set a breakpoint on that inlined code, the missing
6695 skip_inline_frames call would break things. Fortunately
6696 that's an extremely unlikely scenario. */
6697 if (!pc_at_non_inline_function (aspace
,
6698 ecs
->event_thread
->stop_pc (),
6700 && !(ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6701 && ecs
->event_thread
->control
.trap_expected
6702 && pc_at_non_inline_function (aspace
,
6703 ecs
->event_thread
->prev_pc
,
6706 stop_chain
= build_bpstat_chain (aspace
,
6707 ecs
->event_thread
->stop_pc (),
6709 skip_inline_frames (ecs
->event_thread
, stop_chain
);
6711 /* Re-fetch current thread's frame in case that invalidated
6713 frame
= get_current_frame ();
6714 gdbarch
= get_frame_arch (frame
);
6718 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6719 && ecs
->event_thread
->control
.trap_expected
6720 && gdbarch_single_step_through_delay_p (gdbarch
)
6721 && currently_stepping (ecs
->event_thread
))
6723 /* We're trying to step off a breakpoint. Turns out that we're
6724 also on an instruction that needs to be stepped multiple
6725 times before it's been fully executing. E.g., architectures
6726 with a delay slot. It needs to be stepped twice, once for
6727 the instruction and once for the delay slot. */
6728 int step_through_delay
6729 = gdbarch_single_step_through_delay (gdbarch
, frame
);
6731 if (step_through_delay
)
6732 infrun_debug_printf ("step through delay");
6734 if (ecs
->event_thread
->control
.step_range_end
== 0
6735 && step_through_delay
)
6737 /* The user issued a continue when stopped at a breakpoint.
6738 Set up for another trap and get out of here. */
6739 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6743 else if (step_through_delay
)
6745 /* The user issued a step when stopped at a breakpoint.
6746 Maybe we should stop, maybe we should not - the delay
6747 slot *might* correspond to a line of source. In any
6748 case, don't decide that here, just set
6749 ecs->stepping_over_breakpoint, making sure we
6750 single-step again before breakpoints are re-inserted. */
6751 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6755 /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
6756 handles this event. */
6757 ecs
->event_thread
->control
.stop_bpstat
6758 = bpstat_stop_status (get_current_regcache ()->aspace (),
6759 ecs
->event_thread
->stop_pc (),
6760 ecs
->event_thread
, ecs
->ws
, stop_chain
);
6762 /* Following in case break condition called a
6764 stop_print_frame
= true;
6766 /* This is where we handle "moribund" watchpoints. Unlike
6767 software breakpoints traps, hardware watchpoint traps are
6768 always distinguishable from random traps. If no high-level
6769 watchpoint is associated with the reported stop data address
6770 anymore, then the bpstat does not explain the signal ---
6771 simply make sure to ignore it if `stopped_by_watchpoint' is
6774 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6775 && !bpstat_explains_signal (ecs
->event_thread
->control
.stop_bpstat
,
6777 && stopped_by_watchpoint
)
6779 infrun_debug_printf ("no user watchpoint explains watchpoint SIGTRAP, "
6783 /* NOTE: cagney/2003-03-29: These checks for a random signal
6784 at one stage in the past included checks for an inferior
6785 function call's call dummy's return breakpoint. The original
6786 comment, that went with the test, read:
6788 ``End of a stack dummy. Some systems (e.g. Sony news) give
6789 another signal besides SIGTRAP, so check here as well as
6792 If someone ever tries to get call dummys on a
6793 non-executable stack to work (where the target would stop
6794 with something like a SIGSEGV), then those tests might need
6795 to be re-instated. Given, however, that the tests were only
6796 enabled when momentary breakpoints were not being used, I
6797 suspect that it won't be the case.
6799 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
6800 be necessary for call dummies on a non-executable stack on
6803 /* See if the breakpoints module can explain the signal. */
6805 = !bpstat_explains_signal (ecs
->event_thread
->control
.stop_bpstat
,
6806 ecs
->event_thread
->stop_signal ());
6808 /* Maybe this was a trap for a software breakpoint that has since
6810 if (random_signal
&& target_stopped_by_sw_breakpoint ())
6812 if (gdbarch_program_breakpoint_here_p (gdbarch
,
6813 ecs
->event_thread
->stop_pc ()))
6815 struct regcache
*regcache
;
6818 /* Re-adjust PC to what the program would see if GDB was not
6820 regcache
= get_thread_regcache (ecs
->event_thread
);
6821 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
6824 gdb::optional
<scoped_restore_tmpl
<int>>
6825 restore_operation_disable
;
6827 if (record_full_is_used ())
6828 restore_operation_disable
.emplace
6829 (record_full_gdb_operation_disable_set ());
6831 regcache_write_pc (regcache
,
6832 ecs
->event_thread
->stop_pc () + decr_pc
);
6837 /* A delayed software breakpoint event. Ignore the trap. */
6838 infrun_debug_printf ("delayed software breakpoint trap, ignoring");
6843 /* Maybe this was a trap for a hardware breakpoint/watchpoint that
6844 has since been removed. */
6845 if (random_signal
&& target_stopped_by_hw_breakpoint ())
6847 /* A delayed hardware breakpoint event. Ignore the trap. */
6848 infrun_debug_printf ("delayed hardware breakpoint/watchpoint "
6853 /* If not, perhaps stepping/nexting can. */
6855 random_signal
= !(ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6856 && currently_stepping (ecs
->event_thread
));
6858 /* Perhaps the thread hit a single-step breakpoint of _another_
6859 thread. Single-step breakpoints are transparent to the
6860 breakpoints module. */
6862 random_signal
= !ecs
->hit_singlestep_breakpoint
;
6864 /* No? Perhaps we got a moribund watchpoint. */
6866 random_signal
= !stopped_by_watchpoint
;
6868 /* Always stop if the user explicitly requested this thread to
6870 if (ecs
->event_thread
->stop_requested
)
6873 infrun_debug_printf ("user-requested stop");
6876 /* For the program's own signals, act according to
6877 the signal handling tables. */
6881 /* Signal not for debugging purposes. */
6882 enum gdb_signal stop_signal
= ecs
->event_thread
->stop_signal ();
6884 infrun_debug_printf ("random signal (%s)",
6885 gdb_signal_to_symbol_string (stop_signal
));
6887 stopped_by_random_signal
= 1;
6889 /* Always stop on signals if we're either just gaining control
6890 of the program, or the user explicitly requested this thread
6891 to remain stopped. */
6892 if (stop_soon
!= NO_STOP_QUIETLY
6893 || ecs
->event_thread
->stop_requested
6894 || signal_stop_state (ecs
->event_thread
->stop_signal ()))
6900 /* Notify observers the signal has "handle print" set. Note we
6901 returned early above if stopping; normal_stop handles the
6902 printing in that case. */
6903 if (signal_print
[ecs
->event_thread
->stop_signal ()])
6905 /* The signal table tells us to print about this signal. */
6906 target_terminal::ours_for_output ();
6907 notify_signal_received (ecs
->event_thread
->stop_signal ());
6908 target_terminal::inferior ();
6911 /* Clear the signal if it should not be passed. */
6912 if (signal_program
[ecs
->event_thread
->stop_signal ()] == 0)
6913 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6915 if (ecs
->event_thread
->prev_pc
== ecs
->event_thread
->stop_pc ()
6916 && ecs
->event_thread
->control
.trap_expected
6917 && ecs
->event_thread
->control
.step_resume_breakpoint
== nullptr)
6919 /* We were just starting a new sequence, attempting to
6920 single-step off of a breakpoint and expecting a SIGTRAP.
6921 Instead this signal arrives. This signal will take us out
6922 of the stepping range so GDB needs to remember to, when
6923 the signal handler returns, resume stepping off that
6925 /* To simplify things, "continue" is forced to use the same
6926 code paths as single-step - set a breakpoint at the
6927 signal return address and then, once hit, step off that
6929 infrun_debug_printf ("signal arrived while stepping over breakpoint");
6931 insert_hp_step_resume_breakpoint_at_frame (frame
);
6932 ecs
->event_thread
->step_after_step_resume_breakpoint
= 1;
6933 /* Reset trap_expected to ensure breakpoints are re-inserted. */
6934 ecs
->event_thread
->control
.trap_expected
= 0;
6936 /* If we were nexting/stepping some other thread, switch to
6937 it, so that we don't continue it, losing control. */
6938 if (!switch_back_to_stepped_thread (ecs
))
6943 if (ecs
->event_thread
->stop_signal () != GDB_SIGNAL_0
6944 && (pc_in_thread_step_range (ecs
->event_thread
->stop_pc (),
6946 || ecs
->event_thread
->control
.step_range_end
== 1)
6947 && (get_stack_frame_id (frame
)
6948 == ecs
->event_thread
->control
.step_stack_frame_id
)
6949 && ecs
->event_thread
->control
.step_resume_breakpoint
== nullptr)
6951 /* The inferior is about to take a signal that will take it
6952 out of the single step range. Set a breakpoint at the
6953 current PC (which is presumably where the signal handler
6954 will eventually return) and then allow the inferior to
6957 Note that this is only needed for a signal delivered
6958 while in the single-step range. Nested signals aren't a
6959 problem as they eventually all return. */
6960 infrun_debug_printf ("signal may take us out of single-step range");
6962 clear_step_over_info ();
6963 insert_hp_step_resume_breakpoint_at_frame (frame
);
6964 ecs
->event_thread
->step_after_step_resume_breakpoint
= 1;
6965 /* Reset trap_expected to ensure breakpoints are re-inserted. */
6966 ecs
->event_thread
->control
.trap_expected
= 0;
6971 /* Note: step_resume_breakpoint may be non-NULL. This occurs
6972 when either there's a nested signal, or when there's a
6973 pending signal enabled just as the signal handler returns
6974 (leaving the inferior at the step-resume-breakpoint without
6975 actually executing it). Either way continue until the
6976 breakpoint is really hit. */
6978 if (!switch_back_to_stepped_thread (ecs
))
6980 infrun_debug_printf ("random signal, keep going");
6987 process_event_stop_test (ecs
);
6990 /* Come here when we've got some debug event / signal we can explain
6991 (IOW, not a random signal), and test whether it should cause a
6992 stop, or whether we should resume the inferior (transparently).
6993 E.g., could be a breakpoint whose condition evaluates false; we
6994 could be still stepping within the line; etc. */
6997 process_event_stop_test (struct execution_control_state
*ecs
)
6999 struct symtab_and_line stop_pc_sal
;
7000 frame_info_ptr frame
;
7001 struct gdbarch
*gdbarch
;
7002 CORE_ADDR jmp_buf_pc
;
7003 struct bpstat_what what
;
7005 /* Handle cases caused by hitting a breakpoint. */
7007 frame
= get_current_frame ();
7008 gdbarch
= get_frame_arch (frame
);
7010 what
= bpstat_what (ecs
->event_thread
->control
.stop_bpstat
);
7012 if (what
.call_dummy
)
7014 stop_stack_dummy
= what
.call_dummy
;
7017 /* A few breakpoint types have callbacks associated (e.g.,
7018 bp_jit_event). Run them now. */
7019 bpstat_run_callbacks (ecs
->event_thread
->control
.stop_bpstat
);
7021 /* If we hit an internal event that triggers symbol changes, the
7022 current frame will be invalidated within bpstat_what (e.g., if we
7023 hit an internal solib event). Re-fetch it. */
7024 frame
= get_current_frame ();
7025 gdbarch
= get_frame_arch (frame
);
7027 switch (what
.main_action
)
7029 case BPSTAT_WHAT_SET_LONGJMP_RESUME
:
7030 /* If we hit the breakpoint at longjmp while stepping, we
7031 install a momentary breakpoint at the target of the
7034 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME");
7036 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7038 if (what
.is_longjmp
)
7040 struct value
*arg_value
;
7042 /* If we set the longjmp breakpoint via a SystemTap probe,
7043 then use it to extract the arguments. The destination PC
7044 is the third argument to the probe. */
7045 arg_value
= probe_safe_evaluate_at_pc (frame
, 2);
7048 jmp_buf_pc
= value_as_address (arg_value
);
7049 jmp_buf_pc
= gdbarch_addr_bits_remove (gdbarch
, jmp_buf_pc
);
7051 else if (!gdbarch_get_longjmp_target_p (gdbarch
)
7052 || !gdbarch_get_longjmp_target (gdbarch
,
7053 frame
, &jmp_buf_pc
))
7055 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME "
7056 "(!gdbarch_get_longjmp_target)");
7061 /* Insert a breakpoint at resume address. */
7062 insert_longjmp_resume_breakpoint (gdbarch
, jmp_buf_pc
);
7065 check_exception_resume (ecs
, frame
);
7069 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
:
7071 frame_info_ptr init_frame
;
7073 /* There are several cases to consider.
7075 1. The initiating frame no longer exists. In this case we
7076 must stop, because the exception or longjmp has gone too
7079 2. The initiating frame exists, and is the same as the
7080 current frame. We stop, because the exception or longjmp
7083 3. The initiating frame exists and is different from the
7084 current frame. This means the exception or longjmp has
7085 been caught beneath the initiating frame, so keep going.
7087 4. longjmp breakpoint has been placed just to protect
7088 against stale dummy frames and user is not interested in
7089 stopping around longjmps. */
7091 infrun_debug_printf ("BPSTAT_WHAT_CLEAR_LONGJMP_RESUME");
7093 gdb_assert (ecs
->event_thread
->control
.exception_resume_breakpoint
7095 delete_exception_resume_breakpoint (ecs
->event_thread
);
7097 if (what
.is_longjmp
)
7099 check_longjmp_breakpoint_for_call_dummy (ecs
->event_thread
);
7101 if (!frame_id_p (ecs
->event_thread
->initiating_frame
))
7109 init_frame
= frame_find_by_id (ecs
->event_thread
->initiating_frame
);
7113 struct frame_id current_id
7114 = get_frame_id (get_current_frame ());
7115 if (current_id
== ecs
->event_thread
->initiating_frame
)
7117 /* Case 2. Fall through. */
7127 /* For Cases 1 and 2, remove the step-resume breakpoint, if it
7129 delete_step_resume_breakpoint (ecs
->event_thread
);
7131 end_stepping_range (ecs
);
7135 case BPSTAT_WHAT_SINGLE
:
7136 infrun_debug_printf ("BPSTAT_WHAT_SINGLE");
7137 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7138 /* Still need to check other stuff, at least the case where we
7139 are stepping and step out of the right range. */
7142 case BPSTAT_WHAT_STEP_RESUME
:
7143 infrun_debug_printf ("BPSTAT_WHAT_STEP_RESUME");
7145 delete_step_resume_breakpoint (ecs
->event_thread
);
7146 if (ecs
->event_thread
->control
.proceed_to_finish
7147 && execution_direction
== EXEC_REVERSE
)
7149 struct thread_info
*tp
= ecs
->event_thread
;
7151 /* We are finishing a function in reverse, and just hit the
7152 step-resume breakpoint at the start address of the
7153 function, and we're almost there -- just need to back up
7154 by one more single-step, which should take us back to the
7156 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
7160 fill_in_stop_func (gdbarch
, ecs
);
7161 if (ecs
->event_thread
->stop_pc () == ecs
->stop_func_start
7162 && execution_direction
== EXEC_REVERSE
)
7164 /* We are stepping over a function call in reverse, and just
7165 hit the step-resume breakpoint at the start address of
7166 the function. Go back to single-stepping, which should
7167 take us back to the function call. */
7168 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7174 case BPSTAT_WHAT_STOP_NOISY
:
7175 infrun_debug_printf ("BPSTAT_WHAT_STOP_NOISY");
7176 stop_print_frame
= true;
7178 /* Assume the thread stopped for a breakpoint. We'll still check
7179 whether a/the breakpoint is there when the thread is next
7181 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7186 case BPSTAT_WHAT_STOP_SILENT
:
7187 infrun_debug_printf ("BPSTAT_WHAT_STOP_SILENT");
7188 stop_print_frame
= false;
7190 /* Assume the thread stopped for a breakpoint. We'll still check
7191 whether a/the breakpoint is there when the thread is next
7193 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7197 case BPSTAT_WHAT_HP_STEP_RESUME
:
7198 infrun_debug_printf ("BPSTAT_WHAT_HP_STEP_RESUME");
7200 delete_step_resume_breakpoint (ecs
->event_thread
);
7201 if (ecs
->event_thread
->step_after_step_resume_breakpoint
)
7203 /* Back when the step-resume breakpoint was inserted, we
7204 were trying to single-step off a breakpoint. Go back to
7206 ecs
->event_thread
->step_after_step_resume_breakpoint
= 0;
7207 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7213 case BPSTAT_WHAT_KEEP_CHECKING
:
7217 /* If we stepped a permanent breakpoint and we had a high priority
7218 step-resume breakpoint for the address we stepped, but we didn't
7219 hit it, then we must have stepped into the signal handler. The
7220 step-resume was only necessary to catch the case of _not_
7221 stepping into the handler, so delete it, and fall through to
7222 checking whether the step finished. */
7223 if (ecs
->event_thread
->stepped_breakpoint
)
7225 struct breakpoint
*sr_bp
7226 = ecs
->event_thread
->control
.step_resume_breakpoint
;
7228 if (sr_bp
!= nullptr
7229 && sr_bp
->first_loc ().permanent
7230 && sr_bp
->type
== bp_hp_step_resume
7231 && sr_bp
->first_loc ().address
== ecs
->event_thread
->prev_pc
)
7233 infrun_debug_printf ("stepped permanent breakpoint, stopped in handler");
7234 delete_step_resume_breakpoint (ecs
->event_thread
);
7235 ecs
->event_thread
->step_after_step_resume_breakpoint
= 0;
7239 /* We come here if we hit a breakpoint but should not stop for it.
7240 Possibly we also were stepping and should stop for that. So fall
7241 through and test for stepping. But, if not stepping, do not
7244 /* In all-stop mode, if we're currently stepping but have stopped in
7245 some other thread, we need to switch back to the stepped thread. */
7246 if (switch_back_to_stepped_thread (ecs
))
7249 if (ecs
->event_thread
->control
.step_resume_breakpoint
)
7251 infrun_debug_printf ("step-resume breakpoint is inserted");
7253 /* Having a step-resume breakpoint overrides anything
7254 else having to do with stepping commands until
7255 that breakpoint is reached. */
7260 if (ecs
->event_thread
->control
.step_range_end
== 0)
7262 infrun_debug_printf ("no stepping, continue");
7263 /* Likewise if we aren't even stepping. */
7268 /* Re-fetch current thread's frame in case the code above caused
7269 the frame cache to be re-initialized, making our FRAME variable
7270 a dangling pointer. */
7271 frame
= get_current_frame ();
7272 gdbarch
= get_frame_arch (frame
);
7273 fill_in_stop_func (gdbarch
, ecs
);
7275 /* If stepping through a line, keep going if still within it.
7277 Note that step_range_end is the address of the first instruction
7278 beyond the step range, and NOT the address of the last instruction
7281 Note also that during reverse execution, we may be stepping
7282 through a function epilogue and therefore must detect when
7283 the current-frame changes in the middle of a line. */
7285 if (pc_in_thread_step_range (ecs
->event_thread
->stop_pc (),
7287 && (execution_direction
!= EXEC_REVERSE
7288 || get_frame_id (frame
) == ecs
->event_thread
->control
.step_frame_id
))
7291 ("stepping inside range [%s-%s]",
7292 paddress (gdbarch
, ecs
->event_thread
->control
.step_range_start
),
7293 paddress (gdbarch
, ecs
->event_thread
->control
.step_range_end
));
7295 /* Tentatively re-enable range stepping; `resume' disables it if
7296 necessary (e.g., if we're stepping over a breakpoint or we
7297 have software watchpoints). */
7298 ecs
->event_thread
->control
.may_range_step
= 1;
7300 /* When stepping backward, stop at beginning of line range
7301 (unless it's the function entry point, in which case
7302 keep going back to the call point). */
7303 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7304 if (stop_pc
== ecs
->event_thread
->control
.step_range_start
7305 && stop_pc
!= ecs
->stop_func_start
7306 && execution_direction
== EXEC_REVERSE
)
7307 end_stepping_range (ecs
);
7314 /* We stepped out of the stepping range. */
7316 /* If we are stepping at the source level and entered the runtime
7317 loader dynamic symbol resolution code...
7319 EXEC_FORWARD: we keep on single stepping until we exit the run
7320 time loader code and reach the callee's address.
7322 EXEC_REVERSE: we've already executed the callee (backward), and
7323 the runtime loader code is handled just like any other
7324 undebuggable function call. Now we need only keep stepping
7325 backward through the trampoline code, and that's handled further
7326 down, so there is nothing for us to do here. */
7328 if (execution_direction
!= EXEC_REVERSE
7329 && ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
7330 && in_solib_dynsym_resolve_code (ecs
->event_thread
->stop_pc ())
7331 && (ecs
->event_thread
->control
.step_start_function
== nullptr
7332 || !in_solib_dynsym_resolve_code (
7333 ecs
->event_thread
->control
.step_start_function
->value_block ()
7336 CORE_ADDR pc_after_resolver
=
7337 gdbarch_skip_solib_resolver (gdbarch
, ecs
->event_thread
->stop_pc ());
7339 infrun_debug_printf ("stepped into dynsym resolve code");
7341 if (pc_after_resolver
)
7343 /* Set up a step-resume breakpoint at the address
7344 indicated by SKIP_SOLIB_RESOLVER. */
7345 symtab_and_line sr_sal
;
7346 sr_sal
.pc
= pc_after_resolver
;
7347 sr_sal
.pspace
= get_frame_program_space (frame
);
7349 insert_step_resume_breakpoint_at_sal (gdbarch
,
7350 sr_sal
, null_frame_id
);
7357 /* Step through an indirect branch thunk. */
7358 if (ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
7359 && gdbarch_in_indirect_branch_thunk (gdbarch
,
7360 ecs
->event_thread
->stop_pc ()))
7362 infrun_debug_printf ("stepped into indirect branch thunk");
7367 if (ecs
->event_thread
->control
.step_range_end
!= 1
7368 && (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
7369 || ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
)
7370 && get_frame_type (frame
) == SIGTRAMP_FRAME
)
7372 infrun_debug_printf ("stepped into signal trampoline");
7373 /* The inferior, while doing a "step" or "next", has ended up in
7374 a signal trampoline (either by a signal being delivered or by
7375 the signal handler returning). Just single-step until the
7376 inferior leaves the trampoline (either by calling the handler
7382 /* If we're in the return path from a shared library trampoline,
7383 we want to proceed through the trampoline when stepping. */
7384 /* macro/2012-04-25: This needs to come before the subroutine
7385 call check below as on some targets return trampolines look
7386 like subroutine calls (MIPS16 return thunks). */
7387 if (gdbarch_in_solib_return_trampoline (gdbarch
,
7388 ecs
->event_thread
->stop_pc (),
7389 ecs
->stop_func_name
)
7390 && ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
)
7392 /* Determine where this trampoline returns. */
7393 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7394 CORE_ADDR real_stop_pc
7395 = gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
);
7397 infrun_debug_printf ("stepped into solib return tramp");
7399 /* Only proceed through if we know where it's going. */
7402 /* And put the step-breakpoint there and go until there. */
7403 symtab_and_line sr_sal
;
7404 sr_sal
.pc
= real_stop_pc
;
7405 sr_sal
.section
= find_pc_overlay (sr_sal
.pc
);
7406 sr_sal
.pspace
= get_frame_program_space (frame
);
7408 /* Do not specify what the fp should be when we stop since
7409 on some machines the prologue is where the new fp value
7411 insert_step_resume_breakpoint_at_sal (gdbarch
,
7412 sr_sal
, null_frame_id
);
7414 /* Restart without fiddling with the step ranges or
7421 /* Check for subroutine calls. The check for the current frame
7422 equalling the step ID is not necessary - the check of the
7423 previous frame's ID is sufficient - but it is a common case and
7424 cheaper than checking the previous frame's ID.
7426 NOTE: frame_id::operator== will never report two invalid frame IDs as
7427 being equal, so to get into this block, both the current and
7428 previous frame must have valid frame IDs. */
7429 /* The outer_frame_id check is a heuristic to detect stepping
7430 through startup code. If we step over an instruction which
7431 sets the stack pointer from an invalid value to a valid value,
7432 we may detect that as a subroutine call from the mythical
7433 "outermost" function. This could be fixed by marking
7434 outermost frames as !stack_p,code_p,special_p. Then the
7435 initial outermost frame, before sp was valid, would
7436 have code_addr == &_start. See the comment in frame_id::operator==
7439 /* We want "nexti" to step into, not over, signal handlers invoked
7440 by the kernel, therefore this subroutine check should not trigger
7441 for a signal handler invocation. On most platforms, this is already
7442 not the case, as the kernel puts a signal trampoline frame onto the
7443 stack to handle proper return after the handler, and therefore at this
7444 point, the current frame is a grandchild of the step frame, not a
7445 child. However, on some platforms, the kernel actually uses a
7446 trampoline to handle *invocation* of the handler. In that case,
7447 when executing the first instruction of the trampoline, this check
7448 would erroneously detect the trampoline invocation as a subroutine
7449 call. Fix this by checking for SIGTRAMP_FRAME. */
7450 if ((get_stack_frame_id (frame
)
7451 != ecs
->event_thread
->control
.step_stack_frame_id
)
7452 && get_frame_type (frame
) != SIGTRAMP_FRAME
7453 && ((frame_unwind_caller_id (get_current_frame ())
7454 == ecs
->event_thread
->control
.step_stack_frame_id
)
7455 && ((ecs
->event_thread
->control
.step_stack_frame_id
7457 || (ecs
->event_thread
->control
.step_start_function
7458 != find_pc_function (ecs
->event_thread
->stop_pc ())))))
7460 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7461 CORE_ADDR real_stop_pc
;
7463 infrun_debug_printf ("stepped into subroutine");
7465 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_NONE
)
7467 /* I presume that step_over_calls is only 0 when we're
7468 supposed to be stepping at the assembly language level
7469 ("stepi"). Just stop. */
7470 /* And this works the same backward as frontward. MVS */
7471 end_stepping_range (ecs
);
7475 /* Reverse stepping through solib trampolines. */
7477 if (execution_direction
== EXEC_REVERSE
7478 && ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
7479 && (gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
)
7480 || (ecs
->stop_func_start
== 0
7481 && in_solib_dynsym_resolve_code (stop_pc
))))
7483 /* Any solib trampoline code can be handled in reverse
7484 by simply continuing to single-step. We have already
7485 executed the solib function (backwards), and a few
7486 steps will take us back through the trampoline to the
7492 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
)
7494 /* We're doing a "next".
7496 Normal (forward) execution: set a breakpoint at the
7497 callee's return address (the address at which the caller
7500 Reverse (backward) execution. set the step-resume
7501 breakpoint at the start of the function that we just
7502 stepped into (backwards), and continue to there. When we
7503 get there, we'll need to single-step back to the caller. */
7505 if (execution_direction
== EXEC_REVERSE
)
7507 /* If we're already at the start of the function, we've either
7508 just stepped backward into a single instruction function,
7509 or stepped back out of a signal handler to the first instruction
7510 of the function. Just keep going, which will single-step back
7512 if (ecs
->stop_func_start
!= stop_pc
&& ecs
->stop_func_start
!= 0)
7514 /* Normal function call return (static or dynamic). */
7515 symtab_and_line sr_sal
;
7516 sr_sal
.pc
= ecs
->stop_func_start
;
7517 sr_sal
.pspace
= get_frame_program_space (frame
);
7518 insert_step_resume_breakpoint_at_sal (gdbarch
,
7519 sr_sal
, get_stack_frame_id (frame
));
7523 insert_step_resume_breakpoint_at_caller (frame
);
7529 /* If we are in a function call trampoline (a stub between the
7530 calling routine and the real function), locate the real
7531 function. That's what tells us (a) whether we want to step
7532 into it at all, and (b) what prologue we want to run to the
7533 end of, if we do step into it. */
7534 real_stop_pc
= skip_language_trampoline (frame
, stop_pc
);
7535 if (real_stop_pc
== 0)
7536 real_stop_pc
= gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
);
7537 if (real_stop_pc
!= 0)
7538 ecs
->stop_func_start
= real_stop_pc
;
7540 if (real_stop_pc
!= 0 && in_solib_dynsym_resolve_code (real_stop_pc
))
7542 symtab_and_line sr_sal
;
7543 sr_sal
.pc
= ecs
->stop_func_start
;
7544 sr_sal
.pspace
= get_frame_program_space (frame
);
7546 insert_step_resume_breakpoint_at_sal (gdbarch
,
7547 sr_sal
, null_frame_id
);
7552 /* If we have line number information for the function we are
7553 thinking of stepping into and the function isn't on the skip
7556 If there are several symtabs at that PC (e.g. with include
7557 files), just want to know whether *any* of them have line
7558 numbers. find_pc_line handles this. */
7560 struct symtab_and_line tmp_sal
;
7562 tmp_sal
= find_pc_line (ecs
->stop_func_start
, 0);
7563 if (tmp_sal
.line
!= 0
7564 && !function_name_is_marked_for_skip (ecs
->stop_func_name
,
7566 && !inline_frame_is_marked_for_skip (true, ecs
->event_thread
))
7568 if (execution_direction
== EXEC_REVERSE
)
7569 handle_step_into_function_backward (gdbarch
, ecs
);
7571 handle_step_into_function (gdbarch
, ecs
);
7576 /* If we have no line number and the step-stop-if-no-debug is
7577 set, we stop the step so that the user has a chance to switch
7578 in assembly mode. */
7579 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
7580 && step_stop_if_no_debug
)
7582 end_stepping_range (ecs
);
7586 if (execution_direction
== EXEC_REVERSE
)
7588 /* If we're already at the start of the function, we've either just
7589 stepped backward into a single instruction function without line
7590 number info, or stepped back out of a signal handler to the first
7591 instruction of the function without line number info. Just keep
7592 going, which will single-step back to the caller. */
7593 if (ecs
->stop_func_start
!= stop_pc
)
7595 /* Set a breakpoint at callee's start address.
7596 From there we can step once and be back in the caller. */
7597 symtab_and_line sr_sal
;
7598 sr_sal
.pc
= ecs
->stop_func_start
;
7599 sr_sal
.pspace
= get_frame_program_space (frame
);
7600 insert_step_resume_breakpoint_at_sal (gdbarch
,
7601 sr_sal
, null_frame_id
);
7605 /* Set a breakpoint at callee's return address (the address
7606 at which the caller will resume). */
7607 insert_step_resume_breakpoint_at_caller (frame
);
7613 /* Reverse stepping through solib trampolines. */
7615 if (execution_direction
== EXEC_REVERSE
7616 && ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
)
7618 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7620 if (gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
)
7621 || (ecs
->stop_func_start
== 0
7622 && in_solib_dynsym_resolve_code (stop_pc
)))
7624 /* Any solib trampoline code can be handled in reverse
7625 by simply continuing to single-step. We have already
7626 executed the solib function (backwards), and a few
7627 steps will take us back through the trampoline to the
7632 else if (in_solib_dynsym_resolve_code (stop_pc
))
7634 /* Stepped backward into the solib dynsym resolver.
7635 Set a breakpoint at its start and continue, then
7636 one more step will take us out. */
7637 symtab_and_line sr_sal
;
7638 sr_sal
.pc
= ecs
->stop_func_start
;
7639 sr_sal
.pspace
= get_frame_program_space (frame
);
7640 insert_step_resume_breakpoint_at_sal (gdbarch
,
7641 sr_sal
, null_frame_id
);
7647 /* This always returns the sal for the inner-most frame when we are in a
7648 stack of inlined frames, even if GDB actually believes that it is in a
7649 more outer frame. This is checked for below by calls to
7650 inline_skipped_frames. */
7651 stop_pc_sal
= find_pc_line (ecs
->event_thread
->stop_pc (), 0);
7653 /* NOTE: tausq/2004-05-24: This if block used to be done before all
7654 the trampoline processing logic, however, there are some trampolines
7655 that have no names, so we should do trampoline handling first. */
7656 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
7657 && ecs
->stop_func_name
== nullptr
7658 && stop_pc_sal
.line
== 0)
7660 infrun_debug_printf ("stepped into undebuggable function");
7662 /* The inferior just stepped into, or returned to, an
7663 undebuggable function (where there is no debugging information
7664 and no line number corresponding to the address where the
7665 inferior stopped). Since we want to skip this kind of code,
7666 we keep going until the inferior returns from this
7667 function - unless the user has asked us not to (via
7668 set step-mode) or we no longer know how to get back
7669 to the call site. */
7670 if (step_stop_if_no_debug
7671 || !frame_id_p (frame_unwind_caller_id (frame
)))
7673 /* If we have no line number and the step-stop-if-no-debug
7674 is set, we stop the step so that the user has a chance to
7675 switch in assembly mode. */
7676 end_stepping_range (ecs
);
7681 /* Set a breakpoint at callee's return address (the address
7682 at which the caller will resume). */
7683 insert_step_resume_breakpoint_at_caller (frame
);
7689 if (execution_direction
== EXEC_REVERSE
7690 && ecs
->event_thread
->control
.proceed_to_finish
7691 && ecs
->event_thread
->stop_pc () >= ecs
->stop_func_alt_start
7692 && ecs
->event_thread
->stop_pc () < ecs
->stop_func_start
)
7694 /* We are executing the reverse-finish command.
7695 If the system supports multiple entry points and we are finishing a
7696 function in reverse. If we are between the entry points single-step
7697 back to the alternate entry point. If we are at the alternate entry
7698 point -- just need to back up by one more single-step, which
7699 should take us back to the function call. */
7700 ecs
->event_thread
->control
.step_range_start
7701 = ecs
->event_thread
->control
.step_range_end
= 1;
7707 if (ecs
->event_thread
->control
.step_range_end
== 1)
7709 /* It is stepi or nexti. We always want to stop stepping after
7711 infrun_debug_printf ("stepi/nexti");
7712 end_stepping_range (ecs
);
7716 if (stop_pc_sal
.line
== 0)
7718 /* We have no line number information. That means to stop
7719 stepping (does this always happen right after one instruction,
7720 when we do "s" in a function with no line numbers,
7721 or can this happen as a result of a return or longjmp?). */
7722 infrun_debug_printf ("line number info");
7723 end_stepping_range (ecs
);
7727 /* Look for "calls" to inlined functions, part one. If the inline
7728 frame machinery detected some skipped call sites, we have entered
7729 a new inline function. */
7731 if ((get_frame_id (get_current_frame ())
7732 == ecs
->event_thread
->control
.step_frame_id
)
7733 && inline_skipped_frames (ecs
->event_thread
))
7735 infrun_debug_printf ("stepped into inlined function");
7737 symtab_and_line call_sal
= find_frame_sal (get_current_frame ());
7739 if (ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_ALL
)
7741 /* For "step", we're going to stop. But if the call site
7742 for this inlined function is on the same source line as
7743 we were previously stepping, go down into the function
7744 first. Otherwise stop at the call site. */
7746 if (call_sal
.line
== ecs
->event_thread
->current_line
7747 && call_sal
.symtab
== ecs
->event_thread
->current_symtab
)
7749 step_into_inline_frame (ecs
->event_thread
);
7750 if (inline_frame_is_marked_for_skip (false, ecs
->event_thread
))
7757 end_stepping_range (ecs
);
7762 /* For "next", we should stop at the call site if it is on a
7763 different source line. Otherwise continue through the
7764 inlined function. */
7765 if (call_sal
.line
== ecs
->event_thread
->current_line
7766 && call_sal
.symtab
== ecs
->event_thread
->current_symtab
)
7769 end_stepping_range (ecs
);
7774 /* Look for "calls" to inlined functions, part two. If we are still
7775 in the same real function we were stepping through, but we have
7776 to go further up to find the exact frame ID, we are stepping
7777 through a more inlined call beyond its call site. */
7779 if (get_frame_type (get_current_frame ()) == INLINE_FRAME
7780 && (get_frame_id (get_current_frame ())
7781 != ecs
->event_thread
->control
.step_frame_id
)
7782 && stepped_in_from (get_current_frame (),
7783 ecs
->event_thread
->control
.step_frame_id
))
7785 infrun_debug_printf ("stepping through inlined function");
7787 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
7788 || inline_frame_is_marked_for_skip (false, ecs
->event_thread
))
7791 end_stepping_range (ecs
);
7795 bool refresh_step_info
= true;
7796 if ((ecs
->event_thread
->stop_pc () == stop_pc_sal
.pc
)
7797 && (ecs
->event_thread
->current_line
!= stop_pc_sal
.line
7798 || ecs
->event_thread
->current_symtab
!= stop_pc_sal
.symtab
))
7800 /* We are at a different line. */
7802 if (stop_pc_sal
.is_stmt
)
7804 /* We are at the start of a statement.
7806 So stop. Note that we don't stop if we step into the middle of a
7807 statement. That is said to make things like for (;;) statements
7809 infrun_debug_printf ("stepped to a different line");
7810 end_stepping_range (ecs
);
7813 else if (get_frame_id (get_current_frame ())
7814 == ecs
->event_thread
->control
.step_frame_id
)
7816 /* We are not at the start of a statement, and we have not changed
7819 We ignore this line table entry, and continue stepping forward,
7820 looking for a better place to stop. */
7821 refresh_step_info
= false;
7822 infrun_debug_printf ("stepped to a different line, but "
7823 "it's not the start of a statement");
7827 /* We are not the start of a statement, and we have changed frame.
7829 We ignore this line table entry, and continue stepping forward,
7830 looking for a better place to stop. Keep refresh_step_info at
7831 true to note that the frame has changed, but ignore the line
7832 number to make sure we don't ignore a subsequent entry with the
7833 same line number. */
7834 stop_pc_sal
.line
= 0;
7835 infrun_debug_printf ("stepped to a different frame, but "
7836 "it's not the start of a statement");
7840 /* We aren't done stepping.
7842 Optimize by setting the stepping range to the line.
7843 (We might not be in the original line, but if we entered a
7844 new line in mid-statement, we continue stepping. This makes
7845 things like for(;;) statements work better.)
7847 If we entered a SAL that indicates a non-statement line table entry,
7848 then we update the stepping range, but we don't update the step info,
7849 which includes things like the line number we are stepping away from.
7850 This means we will stop when we find a line table entry that is marked
7851 as is-statement, even if it matches the non-statement one we just
7854 ecs
->event_thread
->control
.step_range_start
= stop_pc_sal
.pc
;
7855 ecs
->event_thread
->control
.step_range_end
= stop_pc_sal
.end
;
7856 ecs
->event_thread
->control
.may_range_step
= 1;
7858 ("updated step range, start = %s, end = %s, may_range_step = %d",
7859 paddress (gdbarch
, ecs
->event_thread
->control
.step_range_start
),
7860 paddress (gdbarch
, ecs
->event_thread
->control
.step_range_end
),
7861 ecs
->event_thread
->control
.may_range_step
);
7862 if (refresh_step_info
)
7863 set_step_info (ecs
->event_thread
, frame
, stop_pc_sal
);
7865 infrun_debug_printf ("keep going");
7869 static bool restart_stepped_thread (process_stratum_target
*resume_target
,
7870 ptid_t resume_ptid
);
7872 /* In all-stop mode, if we're currently stepping but have stopped in
7873 some other thread, we may need to switch back to the stepped
7874 thread. Returns true we set the inferior running, false if we left
7875 it stopped (and the event needs further processing). */
7878 switch_back_to_stepped_thread (struct execution_control_state
*ecs
)
7880 if (!target_is_non_stop_p ())
7882 /* If any thread is blocked on some internal breakpoint, and we
7883 simply need to step over that breakpoint to get it going
7884 again, do that first. */
7886 /* However, if we see an event for the stepping thread, then we
7887 know all other threads have been moved past their breakpoints
7888 already. Let the caller check whether the step is finished,
7889 etc., before deciding to move it past a breakpoint. */
7890 if (ecs
->event_thread
->control
.step_range_end
!= 0)
7893 /* Check if the current thread is blocked on an incomplete
7894 step-over, interrupted by a random signal. */
7895 if (ecs
->event_thread
->control
.trap_expected
7896 && ecs
->event_thread
->stop_signal () != GDB_SIGNAL_TRAP
)
7899 ("need to finish step-over of [%s]",
7900 ecs
->event_thread
->ptid
.to_string ().c_str ());
7905 /* Check if the current thread is blocked by a single-step
7906 breakpoint of another thread. */
7907 if (ecs
->hit_singlestep_breakpoint
)
7909 infrun_debug_printf ("need to step [%s] over single-step breakpoint",
7910 ecs
->ptid
.to_string ().c_str ());
7915 /* If this thread needs yet another step-over (e.g., stepping
7916 through a delay slot), do it first before moving on to
7918 if (thread_still_needs_step_over (ecs
->event_thread
))
7921 ("thread [%s] still needs step-over",
7922 ecs
->event_thread
->ptid
.to_string ().c_str ());
7927 /* If scheduler locking applies even if not stepping, there's no
7928 need to walk over threads. Above we've checked whether the
7929 current thread is stepping. If some other thread not the
7930 event thread is stepping, then it must be that scheduler
7931 locking is not in effect. */
7932 if (schedlock_applies (ecs
->event_thread
))
7935 /* Otherwise, we no longer expect a trap in the current thread.
7936 Clear the trap_expected flag before switching back -- this is
7937 what keep_going does as well, if we call it. */
7938 ecs
->event_thread
->control
.trap_expected
= 0;
7940 /* Likewise, clear the signal if it should not be passed. */
7941 if (!signal_program
[ecs
->event_thread
->stop_signal ()])
7942 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
7944 if (restart_stepped_thread (ecs
->target
, ecs
->ptid
))
7946 prepare_to_wait (ecs
);
7950 switch_to_thread (ecs
->event_thread
);
7956 /* Look for the thread that was stepping, and resume it.
7957 RESUME_TARGET / RESUME_PTID indicate the set of threads the caller
7958 is resuming. Return true if a thread was started, false
7962 restart_stepped_thread (process_stratum_target
*resume_target
,
7965 /* Do all pending step-overs before actually proceeding with
7967 if (start_step_over ())
7970 for (thread_info
*tp
: all_threads_safe ())
7972 if (tp
->state
== THREAD_EXITED
)
7975 if (tp
->has_pending_waitstatus ())
7978 /* Ignore threads of processes the caller is not
7981 && (tp
->inf
->process_target () != resume_target
7982 || tp
->inf
->pid
!= resume_ptid
.pid ()))
7985 if (tp
->control
.trap_expected
)
7987 infrun_debug_printf ("switching back to stepped thread (step-over)");
7989 if (keep_going_stepped_thread (tp
))
7994 for (thread_info
*tp
: all_threads_safe ())
7996 if (tp
->state
== THREAD_EXITED
)
7999 if (tp
->has_pending_waitstatus ())
8002 /* Ignore threads of processes the caller is not
8005 && (tp
->inf
->process_target () != resume_target
8006 || tp
->inf
->pid
!= resume_ptid
.pid ()))
8009 /* Did we find the stepping thread? */
8010 if (tp
->control
.step_range_end
)
8012 infrun_debug_printf ("switching back to stepped thread (stepping)");
8014 if (keep_going_stepped_thread (tp
))
8025 restart_after_all_stop_detach (process_stratum_target
*proc_target
)
8027 /* Note we don't check target_is_non_stop_p() here, because the
8028 current inferior may no longer have a process_stratum target
8029 pushed, as we just detached. */
8031 /* See if we have a THREAD_RUNNING thread that need to be
8032 re-resumed. If we have any thread that is already executing,
8033 then we don't need to resume the target -- it is already been
8034 resumed. With the remote target (in all-stop), it's even
8035 impossible to issue another resumption if the target is already
8036 resumed, until the target reports a stop. */
8037 for (thread_info
*thr
: all_threads (proc_target
))
8039 if (thr
->state
!= THREAD_RUNNING
)
8042 /* If we have any thread that is already executing, then we
8043 don't need to resume the target -- it is already been
8045 if (thr
->executing ())
8048 /* If we have a pending event to process, skip resuming the
8049 target and go straight to processing it. */
8050 if (thr
->resumed () && thr
->has_pending_waitstatus ())
8054 /* Alright, we need to re-resume the target. If a thread was
8055 stepping, we need to restart it stepping. */
8056 if (restart_stepped_thread (proc_target
, minus_one_ptid
))
8059 /* Otherwise, find the first THREAD_RUNNING thread and resume
8061 for (thread_info
*thr
: all_threads (proc_target
))
8063 if (thr
->state
!= THREAD_RUNNING
)
8066 execution_control_state
ecs (thr
);
8067 switch_to_thread (thr
);
8073 /* Set a previously stepped thread back to stepping. Returns true on
8074 success, false if the resume is not possible (e.g., the thread
8078 keep_going_stepped_thread (struct thread_info
*tp
)
8080 frame_info_ptr frame
;
8082 /* If the stepping thread exited, then don't try to switch back and
8083 resume it, which could fail in several different ways depending
8084 on the target. Instead, just keep going.
8086 We can find a stepping dead thread in the thread list in two
8089 - The target supports thread exit events, and when the target
8090 tries to delete the thread from the thread list, inferior_ptid
8091 pointed at the exiting thread. In such case, calling
8092 delete_thread does not really remove the thread from the list;
8093 instead, the thread is left listed, with 'exited' state.
8095 - The target's debug interface does not support thread exit
8096 events, and so we have no idea whatsoever if the previously
8097 stepping thread is still alive. For that reason, we need to
8098 synchronously query the target now. */
8100 if (tp
->state
== THREAD_EXITED
|| !target_thread_alive (tp
->ptid
))
8102 infrun_debug_printf ("not resuming previously stepped thread, it has "
8109 infrun_debug_printf ("resuming previously stepped thread");
8111 execution_control_state
ecs (tp
);
8112 switch_to_thread (tp
);
8114 tp
->set_stop_pc (regcache_read_pc (get_thread_regcache (tp
)));
8115 frame
= get_current_frame ();
8117 /* If the PC of the thread we were trying to single-step has
8118 changed, then that thread has trapped or been signaled, but the
8119 event has not been reported to GDB yet. Re-poll the target
8120 looking for this particular thread's event (i.e. temporarily
8121 enable schedlock) by:
8123 - setting a break at the current PC
8124 - resuming that particular thread, only (by setting trap
8127 This prevents us continuously moving the single-step breakpoint
8128 forward, one instruction at a time, overstepping. */
8130 if (tp
->stop_pc () != tp
->prev_pc
)
8134 infrun_debug_printf ("expected thread advanced also (%s -> %s)",
8135 paddress (current_inferior ()->arch (), tp
->prev_pc
),
8136 paddress (current_inferior ()->arch (),
8139 /* Clear the info of the previous step-over, as it's no longer
8140 valid (if the thread was trying to step over a breakpoint, it
8141 has already succeeded). It's what keep_going would do too,
8142 if we called it. Do this before trying to insert the sss
8143 breakpoint, otherwise if we were previously trying to step
8144 over this exact address in another thread, the breakpoint is
8146 clear_step_over_info ();
8147 tp
->control
.trap_expected
= 0;
8149 insert_single_step_breakpoint (get_frame_arch (frame
),
8150 get_frame_address_space (frame
),
8153 tp
->set_resumed (true);
8154 resume_ptid
= internal_resume_ptid (tp
->control
.stepping_command
);
8155 do_target_resume (resume_ptid
, false, GDB_SIGNAL_0
);
8159 infrun_debug_printf ("expected thread still hasn't advanced");
8161 keep_going_pass_signal (&ecs
);
8167 /* Is thread TP in the middle of (software or hardware)
8168 single-stepping? (Note the result of this function must never be
8169 passed directly as target_resume's STEP parameter.) */
8172 currently_stepping (struct thread_info
*tp
)
8174 return ((tp
->control
.step_range_end
8175 && tp
->control
.step_resume_breakpoint
== nullptr)
8176 || tp
->control
.trap_expected
8177 || tp
->stepped_breakpoint
8178 || bpstat_should_step ());
8181 /* Inferior has stepped into a subroutine call with source code that
8182 we should not step over. Do step to the first line of code in
8186 handle_step_into_function (struct gdbarch
*gdbarch
,
8187 struct execution_control_state
*ecs
)
8189 fill_in_stop_func (gdbarch
, ecs
);
8191 compunit_symtab
*cust
8192 = find_pc_compunit_symtab (ecs
->event_thread
->stop_pc ());
8193 if (cust
!= nullptr && cust
->language () != language_asm
)
8194 ecs
->stop_func_start
8195 = gdbarch_skip_prologue_noexcept (gdbarch
, ecs
->stop_func_start
);
8197 symtab_and_line stop_func_sal
= find_pc_line (ecs
->stop_func_start
, 0);
8198 /* Use the step_resume_break to step until the end of the prologue,
8199 even if that involves jumps (as it seems to on the vax under
8201 /* If the prologue ends in the middle of a source line, continue to
8202 the end of that source line (if it is still within the function).
8203 Otherwise, just go to end of prologue. */
8204 if (stop_func_sal
.end
8205 && stop_func_sal
.pc
!= ecs
->stop_func_start
8206 && stop_func_sal
.end
< ecs
->stop_func_end
)
8207 ecs
->stop_func_start
= stop_func_sal
.end
;
8209 /* Architectures which require breakpoint adjustment might not be able
8210 to place a breakpoint at the computed address. If so, the test
8211 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
8212 ecs->stop_func_start to an address at which a breakpoint may be
8213 legitimately placed.
8215 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
8216 made, GDB will enter an infinite loop when stepping through
8217 optimized code consisting of VLIW instructions which contain
8218 subinstructions corresponding to different source lines. On
8219 FR-V, it's not permitted to place a breakpoint on any but the
8220 first subinstruction of a VLIW instruction. When a breakpoint is
8221 set, GDB will adjust the breakpoint address to the beginning of
8222 the VLIW instruction. Thus, we need to make the corresponding
8223 adjustment here when computing the stop address. */
8225 if (gdbarch_adjust_breakpoint_address_p (gdbarch
))
8227 ecs
->stop_func_start
8228 = gdbarch_adjust_breakpoint_address (gdbarch
,
8229 ecs
->stop_func_start
);
8232 if (ecs
->stop_func_start
== ecs
->event_thread
->stop_pc ())
8234 /* We are already there: stop now. */
8235 end_stepping_range (ecs
);
8240 /* Put the step-breakpoint there and go until there. */
8241 symtab_and_line sr_sal
;
8242 sr_sal
.pc
= ecs
->stop_func_start
;
8243 sr_sal
.section
= find_pc_overlay (ecs
->stop_func_start
);
8244 sr_sal
.pspace
= get_frame_program_space (get_current_frame ());
8246 /* Do not specify what the fp should be when we stop since on
8247 some machines the prologue is where the new fp value is
8249 insert_step_resume_breakpoint_at_sal (gdbarch
, sr_sal
, null_frame_id
);
8251 /* And make sure stepping stops right away then. */
8252 ecs
->event_thread
->control
.step_range_end
8253 = ecs
->event_thread
->control
.step_range_start
;
8258 /* Inferior has stepped backward into a subroutine call with source
8259 code that we should not step over. Do step to the beginning of the
8260 last line of code in it. */
8263 handle_step_into_function_backward (struct gdbarch
*gdbarch
,
8264 struct execution_control_state
*ecs
)
8266 struct compunit_symtab
*cust
;
8267 struct symtab_and_line stop_func_sal
;
8269 fill_in_stop_func (gdbarch
, ecs
);
8271 cust
= find_pc_compunit_symtab (ecs
->event_thread
->stop_pc ());
8272 if (cust
!= nullptr && cust
->language () != language_asm
)
8273 ecs
->stop_func_start
8274 = gdbarch_skip_prologue_noexcept (gdbarch
, ecs
->stop_func_start
);
8276 stop_func_sal
= find_pc_line (ecs
->event_thread
->stop_pc (), 0);
8278 /* OK, we're just going to keep stepping here. */
8279 if (stop_func_sal
.pc
== ecs
->event_thread
->stop_pc ())
8281 /* We're there already. Just stop stepping now. */
8282 end_stepping_range (ecs
);
8286 /* Else just reset the step range and keep going.
8287 No step-resume breakpoint, they don't work for
8288 epilogues, which can have multiple entry paths. */
8289 ecs
->event_thread
->control
.step_range_start
= stop_func_sal
.pc
;
8290 ecs
->event_thread
->control
.step_range_end
= stop_func_sal
.end
;
8296 /* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
8297 This is used to both functions and to skip over code. */
8300 insert_step_resume_breakpoint_at_sal_1 (struct gdbarch
*gdbarch
,
8301 struct symtab_and_line sr_sal
,
8302 struct frame_id sr_id
,
8303 enum bptype sr_type
)
8305 /* There should never be more than one step-resume or longjmp-resume
8306 breakpoint per thread, so we should never be setting a new
8307 step_resume_breakpoint when one is already active. */
8308 gdb_assert (inferior_thread ()->control
.step_resume_breakpoint
== nullptr);
8309 gdb_assert (sr_type
== bp_step_resume
|| sr_type
== bp_hp_step_resume
);
8311 infrun_debug_printf ("inserting step-resume breakpoint at %s",
8312 paddress (gdbarch
, sr_sal
.pc
));
8314 inferior_thread ()->control
.step_resume_breakpoint
8315 = set_momentary_breakpoint (gdbarch
, sr_sal
, sr_id
, sr_type
).release ();
8319 insert_step_resume_breakpoint_at_sal (struct gdbarch
*gdbarch
,
8320 struct symtab_and_line sr_sal
,
8321 struct frame_id sr_id
)
8323 insert_step_resume_breakpoint_at_sal_1 (gdbarch
,
8328 /* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
8329 This is used to skip a potential signal handler.
8331 This is called with the interrupted function's frame. The signal
8332 handler, when it returns, will resume the interrupted function at
8336 insert_hp_step_resume_breakpoint_at_frame (frame_info_ptr return_frame
)
8338 gdb_assert (return_frame
!= nullptr);
8340 struct gdbarch
*gdbarch
= get_frame_arch (return_frame
);
8342 symtab_and_line sr_sal
;
8343 sr_sal
.pc
= gdbarch_addr_bits_remove (gdbarch
, get_frame_pc (return_frame
));
8344 sr_sal
.section
= find_pc_overlay (sr_sal
.pc
);
8345 sr_sal
.pspace
= get_frame_program_space (return_frame
);
8347 insert_step_resume_breakpoint_at_sal_1 (gdbarch
, sr_sal
,
8348 get_stack_frame_id (return_frame
),
8352 /* Insert a "step-resume breakpoint" at the previous frame's PC. This
8353 is used to skip a function after stepping into it (for "next" or if
8354 the called function has no debugging information).
8356 The current function has almost always been reached by single
8357 stepping a call or return instruction. NEXT_FRAME belongs to the
8358 current function, and the breakpoint will be set at the caller's
8361 This is a separate function rather than reusing
8362 insert_hp_step_resume_breakpoint_at_frame in order to avoid
8363 get_prev_frame, which may stop prematurely (see the implementation
8364 of frame_unwind_caller_id for an example). */
8367 insert_step_resume_breakpoint_at_caller (frame_info_ptr next_frame
)
8369 /* We shouldn't have gotten here if we don't know where the call site
8371 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame
)));
8373 struct gdbarch
*gdbarch
= frame_unwind_caller_arch (next_frame
);
8375 symtab_and_line sr_sal
;
8376 sr_sal
.pc
= gdbarch_addr_bits_remove (gdbarch
,
8377 frame_unwind_caller_pc (next_frame
));
8378 sr_sal
.section
= find_pc_overlay (sr_sal
.pc
);
8379 sr_sal
.pspace
= frame_unwind_program_space (next_frame
);
8381 insert_step_resume_breakpoint_at_sal (gdbarch
, sr_sal
,
8382 frame_unwind_caller_id (next_frame
));
8385 /* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
8386 new breakpoint at the target of a jmp_buf. The handling of
8387 longjmp-resume uses the same mechanisms used for handling
8388 "step-resume" breakpoints. */
8391 insert_longjmp_resume_breakpoint (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
8393 /* There should never be more than one longjmp-resume breakpoint per
8394 thread, so we should never be setting a new
8395 longjmp_resume_breakpoint when one is already active. */
8396 gdb_assert (inferior_thread ()->control
.exception_resume_breakpoint
== nullptr);
8398 infrun_debug_printf ("inserting longjmp-resume breakpoint at %s",
8399 paddress (gdbarch
, pc
));
8401 inferior_thread ()->control
.exception_resume_breakpoint
=
8402 set_momentary_breakpoint_at_pc (gdbarch
, pc
, bp_longjmp_resume
).release ();
8405 /* Insert an exception resume breakpoint. TP is the thread throwing
8406 the exception. The block B is the block of the unwinder debug hook
8407 function. FRAME is the frame corresponding to the call to this
8408 function. SYM is the symbol of the function argument holding the
8409 target PC of the exception. */
8412 insert_exception_resume_breakpoint (struct thread_info
*tp
,
8413 const struct block
*b
,
8414 frame_info_ptr frame
,
8419 struct block_symbol vsym
;
8420 struct value
*value
;
8422 struct breakpoint
*bp
;
8424 vsym
= lookup_symbol_search_name (sym
->search_name (),
8426 value
= read_var_value (vsym
.symbol
, vsym
.block
, frame
);
8427 /* If the value was optimized out, revert to the old behavior. */
8428 if (! value
->optimized_out ())
8430 handler
= value_as_address (value
);
8432 infrun_debug_printf ("exception resume at %lx",
8433 (unsigned long) handler
);
8435 /* set_momentary_breakpoint_at_pc creates a thread-specific
8436 breakpoint for the current inferior thread. */
8437 gdb_assert (tp
== inferior_thread ());
8438 bp
= set_momentary_breakpoint_at_pc (get_frame_arch (frame
),
8440 bp_exception_resume
).release ();
8442 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
8445 tp
->control
.exception_resume_breakpoint
= bp
;
8448 catch (const gdb_exception_error
&e
)
8450 /* We want to ignore errors here. */
8454 /* A helper for check_exception_resume that sets an
8455 exception-breakpoint based on a SystemTap probe. */
8458 insert_exception_resume_from_probe (struct thread_info
*tp
,
8459 const struct bound_probe
*probe
,
8460 frame_info_ptr frame
)
8462 struct value
*arg_value
;
8464 struct breakpoint
*bp
;
8466 arg_value
= probe_safe_evaluate_at_pc (frame
, 1);
8470 handler
= value_as_address (arg_value
);
8472 infrun_debug_printf ("exception resume at %s",
8473 paddress (probe
->objfile
->arch (), handler
));
8475 /* set_momentary_breakpoint_at_pc creates a thread-specific breakpoint
8476 for the current inferior thread. */
8477 gdb_assert (tp
== inferior_thread ());
8478 bp
= set_momentary_breakpoint_at_pc (get_frame_arch (frame
),
8479 handler
, bp_exception_resume
).release ();
8480 tp
->control
.exception_resume_breakpoint
= bp
;
8483 /* This is called when an exception has been intercepted. Check to
8484 see whether the exception's destination is of interest, and if so,
8485 set an exception resume breakpoint there. */
8488 check_exception_resume (struct execution_control_state
*ecs
,
8489 frame_info_ptr frame
)
8491 struct bound_probe probe
;
8492 struct symbol
*func
;
8494 /* First see if this exception unwinding breakpoint was set via a
8495 SystemTap probe point. If so, the probe has two arguments: the
8496 CFA and the HANDLER. We ignore the CFA, extract the handler, and
8497 set a breakpoint there. */
8498 probe
= find_probe_by_pc (get_frame_pc (frame
));
8501 insert_exception_resume_from_probe (ecs
->event_thread
, &probe
, frame
);
8505 func
= get_frame_function (frame
);
8511 const struct block
*b
;
8514 /* The exception breakpoint is a thread-specific breakpoint on
8515 the unwinder's debug hook, declared as:
8517 void _Unwind_DebugHook (void *cfa, void *handler);
8519 The CFA argument indicates the frame to which control is
8520 about to be transferred. HANDLER is the destination PC.
8522 We ignore the CFA and set a temporary breakpoint at HANDLER.
8523 This is not extremely efficient but it avoids issues in gdb
8524 with computing the DWARF CFA, and it also works even in weird
8525 cases such as throwing an exception from inside a signal
8528 b
= func
->value_block ();
8529 for (struct symbol
*sym
: block_iterator_range (b
))
8531 if (!sym
->is_argument ())
8538 insert_exception_resume_breakpoint (ecs
->event_thread
,
8544 catch (const gdb_exception_error
&e
)
8550 stop_waiting (struct execution_control_state
*ecs
)
8552 infrun_debug_printf ("stop_waiting");
8554 /* Let callers know we don't want to wait for the inferior anymore. */
8555 ecs
->wait_some_more
= 0;
8558 /* Like keep_going, but passes the signal to the inferior, even if the
8559 signal is set to nopass. */
8562 keep_going_pass_signal (struct execution_control_state
*ecs
)
8564 gdb_assert (ecs
->event_thread
->ptid
== inferior_ptid
);
8565 gdb_assert (!ecs
->event_thread
->resumed ());
8567 /* Save the pc before execution, to compare with pc after stop. */
8568 ecs
->event_thread
->prev_pc
8569 = regcache_read_pc_protected (get_thread_regcache (ecs
->event_thread
));
8571 if (ecs
->event_thread
->control
.trap_expected
)
8573 struct thread_info
*tp
= ecs
->event_thread
;
8575 infrun_debug_printf ("%s has trap_expected set, "
8576 "resuming to collect trap",
8577 tp
->ptid
.to_string ().c_str ());
8579 /* We haven't yet gotten our trap, and either: intercepted a
8580 non-signal event (e.g., a fork); or took a signal which we
8581 are supposed to pass through to the inferior. Simply
8583 resume (ecs
->event_thread
->stop_signal ());
8585 else if (step_over_info_valid_p ())
8587 /* Another thread is stepping over a breakpoint in-line. If
8588 this thread needs a step-over too, queue the request. In
8589 either case, this resume must be deferred for later. */
8590 struct thread_info
*tp
= ecs
->event_thread
;
8592 if (ecs
->hit_singlestep_breakpoint
8593 || thread_still_needs_step_over (tp
))
8595 infrun_debug_printf ("step-over already in progress: "
8596 "step-over for %s deferred",
8597 tp
->ptid
.to_string ().c_str ());
8598 global_thread_step_over_chain_enqueue (tp
);
8601 infrun_debug_printf ("step-over in progress: resume of %s deferred",
8602 tp
->ptid
.to_string ().c_str ());
8606 struct regcache
*regcache
= get_current_regcache ();
8609 step_over_what step_what
;
8611 /* Either the trap was not expected, but we are continuing
8612 anyway (if we got a signal, the user asked it be passed to
8615 We got our expected trap, but decided we should resume from
8618 We're going to run this baby now!
8620 Note that insert_breakpoints won't try to re-insert
8621 already inserted breakpoints. Therefore, we don't
8622 care if breakpoints were already inserted, or not. */
8624 /* If we need to step over a breakpoint, and we're not using
8625 displaced stepping to do so, insert all breakpoints
8626 (watchpoints, etc.) but the one we're stepping over, step one
8627 instruction, and then re-insert the breakpoint when that step
8630 step_what
= thread_still_needs_step_over (ecs
->event_thread
);
8632 remove_bp
= (ecs
->hit_singlestep_breakpoint
8633 || (step_what
& STEP_OVER_BREAKPOINT
));
8634 remove_wps
= (step_what
& STEP_OVER_WATCHPOINT
);
8636 /* We can't use displaced stepping if we need to step past a
8637 watchpoint. The instruction copied to the scratch pad would
8638 still trigger the watchpoint. */
8640 && (remove_wps
|| !use_displaced_stepping (ecs
->event_thread
)))
8642 set_step_over_info (regcache
->aspace (),
8643 regcache_read_pc (regcache
), remove_wps
,
8644 ecs
->event_thread
->global_num
);
8646 else if (remove_wps
)
8647 set_step_over_info (nullptr, 0, remove_wps
, -1);
8649 /* If we now need to do an in-line step-over, we need to stop
8650 all other threads. Note this must be done before
8651 insert_breakpoints below, because that removes the breakpoint
8652 we're about to step over, otherwise other threads could miss
8654 if (step_over_info_valid_p () && target_is_non_stop_p ())
8655 stop_all_threads ("starting in-line step-over");
8657 /* Stop stepping if inserting breakpoints fails. */
8660 insert_breakpoints ();
8662 catch (const gdb_exception_error
&e
)
8664 exception_print (gdb_stderr
, e
);
8666 clear_step_over_info ();
8670 ecs
->event_thread
->control
.trap_expected
= (remove_bp
|| remove_wps
);
8672 resume (ecs
->event_thread
->stop_signal ());
8675 prepare_to_wait (ecs
);
8678 /* Called when we should continue running the inferior, because the
8679 current event doesn't cause a user visible stop. This does the
8680 resuming part; waiting for the next event is done elsewhere. */
8683 keep_going (struct execution_control_state
*ecs
)
8685 if (ecs
->event_thread
->control
.trap_expected
8686 && ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
)
8687 ecs
->event_thread
->control
.trap_expected
= 0;
8689 if (!signal_program
[ecs
->event_thread
->stop_signal ()])
8690 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
8691 keep_going_pass_signal (ecs
);
8694 /* This function normally comes after a resume, before
8695 handle_inferior_event exits. It takes care of any last bits of
8696 housekeeping, and sets the all-important wait_some_more flag. */
8699 prepare_to_wait (struct execution_control_state
*ecs
)
8701 infrun_debug_printf ("prepare_to_wait");
8703 ecs
->wait_some_more
= 1;
8705 /* If the target can't async, emulate it by marking the infrun event
8706 handler such that as soon as we get back to the event-loop, we
8707 immediately end up in fetch_inferior_event again calling
8709 if (!target_can_async_p ())
8710 mark_infrun_async_event_handler ();
8713 /* We are done with the step range of a step/next/si/ni command.
8714 Called once for each n of a "step n" operation. */
8717 end_stepping_range (struct execution_control_state
*ecs
)
8719 ecs
->event_thread
->control
.stop_step
= 1;
8723 /* Several print_*_reason functions to print why the inferior has stopped.
8724 We always print something when the inferior exits, or receives a signal.
8725 The rest of the cases are dealt with later on in normal_stop and
8726 print_it_typical. Ideally there should be a call to one of these
8727 print_*_reason functions functions from handle_inferior_event each time
8728 stop_waiting is called.
8730 Note that we don't call these directly, instead we delegate that to
8731 the interpreters, through observers. Interpreters then call these
8732 with whatever uiout is right. */
8735 print_signal_exited_reason (struct ui_out
*uiout
, enum gdb_signal siggnal
)
8737 annotate_signalled ();
8738 if (uiout
->is_mi_like_p ())
8740 ("reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED
));
8741 uiout
->text ("\nProgram terminated with signal ");
8742 annotate_signal_name ();
8743 uiout
->field_string ("signal-name",
8744 gdb_signal_to_name (siggnal
));
8745 annotate_signal_name_end ();
8747 annotate_signal_string ();
8748 uiout
->field_string ("signal-meaning",
8749 gdb_signal_to_string (siggnal
));
8750 annotate_signal_string_end ();
8751 uiout
->text (".\n");
8752 uiout
->text ("The program no longer exists.\n");
8756 print_exited_reason (struct ui_out
*uiout
, int exitstatus
)
8758 struct inferior
*inf
= current_inferior ();
8759 std::string pidstr
= target_pid_to_str (ptid_t (inf
->pid
));
8761 annotate_exited (exitstatus
);
8764 if (uiout
->is_mi_like_p ())
8765 uiout
->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXITED
));
8766 std::string exit_code_str
8767 = string_printf ("0%o", (unsigned int) exitstatus
);
8768 uiout
->message ("[Inferior %s (%s) exited with code %pF]\n",
8769 plongest (inf
->num
), pidstr
.c_str (),
8770 string_field ("exit-code", exit_code_str
.c_str ()));
8774 if (uiout
->is_mi_like_p ())
8776 ("reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY
));
8777 uiout
->message ("[Inferior %s (%s) exited normally]\n",
8778 plongest (inf
->num
), pidstr
.c_str ());
8783 print_signal_received_reason (struct ui_out
*uiout
, enum gdb_signal siggnal
)
8785 struct thread_info
*thr
= inferior_thread ();
8787 infrun_debug_printf ("signal = %s", gdb_signal_to_string (siggnal
));
8791 if (uiout
->is_mi_like_p ())
8793 else if (show_thread_that_caused_stop ())
8795 uiout
->text ("\nThread ");
8796 uiout
->field_string ("thread-id", print_thread_id (thr
));
8798 const char *name
= thread_name (thr
);
8799 if (name
!= nullptr)
8801 uiout
->text (" \"");
8802 uiout
->field_string ("name", name
);
8807 uiout
->text ("\nProgram");
8809 if (siggnal
== GDB_SIGNAL_0
&& !uiout
->is_mi_like_p ())
8810 uiout
->text (" stopped");
8813 uiout
->text (" received signal ");
8814 annotate_signal_name ();
8815 if (uiout
->is_mi_like_p ())
8817 ("reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED
));
8818 uiout
->field_string ("signal-name", gdb_signal_to_name (siggnal
));
8819 annotate_signal_name_end ();
8821 annotate_signal_string ();
8822 uiout
->field_string ("signal-meaning", gdb_signal_to_string (siggnal
));
8824 struct regcache
*regcache
= get_current_regcache ();
8825 struct gdbarch
*gdbarch
= regcache
->arch ();
8826 if (gdbarch_report_signal_info_p (gdbarch
))
8827 gdbarch_report_signal_info (gdbarch
, uiout
, siggnal
);
8829 annotate_signal_string_end ();
8831 uiout
->text (".\n");
8835 print_no_history_reason (struct ui_out
*uiout
)
8837 if (uiout
->is_mi_like_p ())
8838 uiout
->field_string ("reason", async_reason_lookup (EXEC_ASYNC_NO_HISTORY
));
8840 uiout
->text ("\nNo more reverse-execution history.\n");
8843 /* Print current location without a level number, if we have changed
8844 functions or hit a breakpoint. Print source line if we have one.
8845 bpstat_print contains the logic deciding in detail what to print,
8846 based on the event(s) that just occurred. */
8849 print_stop_location (const target_waitstatus
&ws
)
8852 enum print_what source_flag
;
8853 int do_frame_printing
= 1;
8854 struct thread_info
*tp
= inferior_thread ();
8856 bpstat_ret
= bpstat_print (tp
->control
.stop_bpstat
, ws
.kind ());
8860 /* FIXME: cagney/2002-12-01: Given that a frame ID does (or
8861 should) carry around the function and does (or should) use
8862 that when doing a frame comparison. */
8863 if (tp
->control
.stop_step
8864 && (tp
->control
.step_frame_id
8865 == get_frame_id (get_current_frame ()))
8866 && (tp
->control
.step_start_function
8867 == find_pc_function (tp
->stop_pc ())))
8869 /* Finished step, just print source line. */
8870 source_flag
= SRC_LINE
;
8874 /* Print location and source line. */
8875 source_flag
= SRC_AND_LOC
;
8878 case PRINT_SRC_AND_LOC
:
8879 /* Print location and source line. */
8880 source_flag
= SRC_AND_LOC
;
8882 case PRINT_SRC_ONLY
:
8883 source_flag
= SRC_LINE
;
8886 /* Something bogus. */
8887 source_flag
= SRC_LINE
;
8888 do_frame_printing
= 0;
8891 internal_error (_("Unknown value."));
8894 /* The behavior of this routine with respect to the source
8896 SRC_LINE: Print only source line
8897 LOCATION: Print only location
8898 SRC_AND_LOC: Print location and source line. */
8899 if (do_frame_printing
)
8900 print_stack_frame (get_selected_frame (nullptr), 0, source_flag
, 1);
8906 print_stop_event (struct ui_out
*uiout
, bool displays
)
8908 struct target_waitstatus last
;
8909 struct thread_info
*tp
;
8911 get_last_target_status (nullptr, nullptr, &last
);
8914 scoped_restore save_uiout
= make_scoped_restore (¤t_uiout
, uiout
);
8916 print_stop_location (last
);
8918 /* Display the auto-display expressions. */
8923 tp
= inferior_thread ();
8924 if (tp
->thread_fsm () != nullptr
8925 && tp
->thread_fsm ()->finished_p ())
8927 struct return_value_info
*rv
;
8929 rv
= tp
->thread_fsm ()->return_value ();
8931 print_return_value (uiout
, rv
);
8938 maybe_remove_breakpoints (void)
8940 if (!breakpoints_should_be_inserted_now () && target_has_execution ())
8942 if (remove_breakpoints ())
8944 target_terminal::ours_for_output ();
8945 gdb_printf (_("Cannot remove breakpoints because "
8946 "program is no longer writable.\nFurther "
8947 "execution is probably impossible.\n"));
8952 /* The execution context that just caused a normal stop. */
8958 DISABLE_COPY_AND_ASSIGN (stop_context
);
8960 bool changed () const;
8965 /* The event PTID. */
8969 /* If stopp for a thread event, this is the thread that caused the
8971 thread_info_ref thread
;
8973 /* The inferior that caused the stop. */
8977 /* Initializes a new stop context. If stopped for a thread event, this
8978 takes a strong reference to the thread. */
8980 stop_context::stop_context ()
8982 stop_id
= get_stop_id ();
8983 ptid
= inferior_ptid
;
8984 inf_num
= current_inferior ()->num
;
8986 if (inferior_ptid
!= null_ptid
)
8988 /* Take a strong reference so that the thread can't be deleted
8990 thread
= thread_info_ref::new_reference (inferior_thread ());
8994 /* Return true if the current context no longer matches the saved stop
8998 stop_context::changed () const
9000 if (ptid
!= inferior_ptid
)
9002 if (inf_num
!= current_inferior ()->num
)
9004 if (thread
!= nullptr && thread
->state
!= THREAD_STOPPED
)
9006 if (get_stop_id () != stop_id
)
9016 struct target_waitstatus last
;
9018 get_last_target_status (nullptr, nullptr, &last
);
9022 /* If an exception is thrown from this point on, make sure to
9023 propagate GDB's knowledge of the executing state to the
9024 frontend/user running state. A QUIT is an easy exception to see
9025 here, so do this before any filtered output. */
9027 ptid_t finish_ptid
= null_ptid
;
9030 finish_ptid
= minus_one_ptid
;
9031 else if (last
.kind () == TARGET_WAITKIND_SIGNALLED
9032 || last
.kind () == TARGET_WAITKIND_EXITED
)
9034 /* On some targets, we may still have live threads in the
9035 inferior when we get a process exit event. E.g., for
9036 "checkpoint", when the current checkpoint/fork exits,
9037 linux-fork.c automatically switches to another fork from
9038 within target_mourn_inferior. */
9039 if (inferior_ptid
!= null_ptid
)
9040 finish_ptid
= ptid_t (inferior_ptid
.pid ());
9042 else if (last
.kind () != TARGET_WAITKIND_NO_RESUMED
)
9043 finish_ptid
= inferior_ptid
;
9045 gdb::optional
<scoped_finish_thread_state
> maybe_finish_thread_state
;
9046 if (finish_ptid
!= null_ptid
)
9048 maybe_finish_thread_state
.emplace
9049 (user_visible_resume_target (finish_ptid
), finish_ptid
);
9052 /* As we're presenting a stop, and potentially removing breakpoints,
9053 update the thread list so we can tell whether there are threads
9054 running on the target. With target remote, for example, we can
9055 only learn about new threads when we explicitly update the thread
9056 list. Do this before notifying the interpreters about signal
9057 stops, end of stepping ranges, etc., so that the "new thread"
9058 output is emitted before e.g., "Program received signal FOO",
9059 instead of after. */
9060 update_thread_list ();
9062 if (last
.kind () == TARGET_WAITKIND_STOPPED
&& stopped_by_random_signal
)
9063 notify_signal_received (inferior_thread ()->stop_signal ());
9065 /* As with the notification of thread events, we want to delay
9066 notifying the user that we've switched thread context until
9067 the inferior actually stops.
9069 There's no point in saying anything if the inferior has exited.
9070 Note that SIGNALLED here means "exited with a signal", not
9071 "received a signal".
9073 Also skip saying anything in non-stop mode. In that mode, as we
9074 don't want GDB to switch threads behind the user's back, to avoid
9075 races where the user is typing a command to apply to thread x,
9076 but GDB switches to thread y before the user finishes entering
9077 the command, fetch_inferior_event installs a cleanup to restore
9078 the current thread back to the thread the user had selected right
9079 after this event is handled, so we're not really switching, only
9080 informing of a stop. */
9083 if ((last
.kind () != TARGET_WAITKIND_SIGNALLED
9084 && last
.kind () != TARGET_WAITKIND_EXITED
9085 && last
.kind () != TARGET_WAITKIND_NO_RESUMED
)
9086 && target_has_execution ()
9087 && previous_thread
!= inferior_thread ())
9089 SWITCH_THRU_ALL_UIS ()
9091 target_terminal::ours_for_output ();
9092 gdb_printf (_("[Switching to %s]\n"),
9093 target_pid_to_str (inferior_ptid
).c_str ());
9094 annotate_thread_changed ();
9098 update_previous_thread ();
9101 if (last
.kind () == TARGET_WAITKIND_NO_RESUMED
)
9103 SWITCH_THRU_ALL_UIS ()
9104 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
9106 target_terminal::ours_for_output ();
9107 gdb_printf (_("No unwaited-for children left.\n"));
9111 /* Note: this depends on the update_thread_list call above. */
9112 maybe_remove_breakpoints ();
9114 /* If an auto-display called a function and that got a signal,
9115 delete that auto-display to avoid an infinite recursion. */
9117 if (stopped_by_random_signal
)
9118 disable_current_display ();
9120 SWITCH_THRU_ALL_UIS ()
9122 async_enable_stdin ();
9125 /* Let the user/frontend see the threads as stopped. */
9126 maybe_finish_thread_state
.reset ();
9128 /* Select innermost stack frame - i.e., current frame is frame 0,
9129 and current location is based on that. Handle the case where the
9130 dummy call is returning after being stopped. E.g. the dummy call
9131 previously hit a breakpoint. (If the dummy call returns
9132 normally, we won't reach here.) Do this before the stop hook is
9133 run, so that it doesn't get to see the temporary dummy frame,
9134 which is not where we'll present the stop. */
9135 if (has_stack_frames ())
9137 if (stop_stack_dummy
== STOP_STACK_DUMMY
)
9139 /* Pop the empty frame that contains the stack dummy. This
9140 also restores inferior state prior to the call (struct
9141 infcall_suspend_state). */
9142 frame_info_ptr frame
= get_current_frame ();
9144 gdb_assert (get_frame_type (frame
) == DUMMY_FRAME
);
9146 /* frame_pop calls reinit_frame_cache as the last thing it
9147 does which means there's now no selected frame. */
9150 select_frame (get_current_frame ());
9152 /* Set the current source location. */
9153 set_current_sal_from_frame (get_current_frame ());
9156 /* Look up the hook_stop and run it (CLI internally handles problem
9157 of stop_command's pre-hook not existing). */
9158 stop_context saved_context
;
9162 execute_cmd_pre_hook (stop_command
);
9164 catch (const gdb_exception_error
&ex
)
9166 exception_fprintf (gdb_stderr
, ex
,
9167 "Error while running hook_stop:\n");
9170 /* If the stop hook resumes the target, then there's no point in
9171 trying to notify about the previous stop; its context is
9172 gone. Likewise if the command switches thread or inferior --
9173 the observers would print a stop for the wrong
9175 if (saved_context
.changed ())
9178 /* Notify observers about the stop. This is where the interpreters
9179 print the stop event. */
9180 notify_normal_stop ((inferior_ptid
!= null_ptid
9181 ? inferior_thread ()->control
.stop_bpstat
9184 annotate_stopped ();
9186 if (target_has_execution ())
9188 if (last
.kind () != TARGET_WAITKIND_SIGNALLED
9189 && last
.kind () != TARGET_WAITKIND_EXITED
9190 && last
.kind () != TARGET_WAITKIND_NO_RESUMED
)
9191 /* Delete the breakpoint we stopped at, if it wants to be deleted.
9192 Delete any breakpoint that is to be deleted at the next stop. */
9193 breakpoint_auto_delete (inferior_thread ()->control
.stop_bpstat
);
9200 signal_stop_state (int signo
)
9202 return signal_stop
[signo
];
9206 signal_print_state (int signo
)
9208 return signal_print
[signo
];
9212 signal_pass_state (int signo
)
9214 return signal_program
[signo
];
9218 signal_cache_update (int signo
)
9222 for (signo
= 0; signo
< (int) GDB_SIGNAL_LAST
; signo
++)
9223 signal_cache_update (signo
);
9228 signal_pass
[signo
] = (signal_stop
[signo
] == 0
9229 && signal_print
[signo
] == 0
9230 && signal_program
[signo
] == 1
9231 && signal_catch
[signo
] == 0);
9235 signal_stop_update (int signo
, int state
)
9237 int ret
= signal_stop
[signo
];
9239 signal_stop
[signo
] = state
;
9240 signal_cache_update (signo
);
9245 signal_print_update (int signo
, int state
)
9247 int ret
= signal_print
[signo
];
9249 signal_print
[signo
] = state
;
9250 signal_cache_update (signo
);
9255 signal_pass_update (int signo
, int state
)
9257 int ret
= signal_program
[signo
];
9259 signal_program
[signo
] = state
;
9260 signal_cache_update (signo
);
9264 /* Update the global 'signal_catch' from INFO and notify the
9268 signal_catch_update (const unsigned int *info
)
9272 for (i
= 0; i
< GDB_SIGNAL_LAST
; ++i
)
9273 signal_catch
[i
] = info
[i
] > 0;
9274 signal_cache_update (-1);
9275 target_pass_signals (signal_pass
);
9279 sig_print_header (void)
9281 gdb_printf (_("Signal Stop\tPrint\tPass "
9282 "to program\tDescription\n"));
9286 sig_print_info (enum gdb_signal oursig
)
9288 const char *name
= gdb_signal_to_name (oursig
);
9289 int name_padding
= 13 - strlen (name
);
9291 if (name_padding
<= 0)
9294 gdb_printf ("%s", name
);
9295 gdb_printf ("%*.*s ", name_padding
, name_padding
, " ");
9296 gdb_printf ("%s\t", signal_stop
[oursig
] ? "Yes" : "No");
9297 gdb_printf ("%s\t", signal_print
[oursig
] ? "Yes" : "No");
9298 gdb_printf ("%s\t\t", signal_program
[oursig
] ? "Yes" : "No");
9299 gdb_printf ("%s\n", gdb_signal_to_string (oursig
));
9302 /* Specify how various signals in the inferior should be handled. */
9305 handle_command (const char *args
, int from_tty
)
9307 int digits
, wordlen
;
9308 int sigfirst
, siglast
;
9309 enum gdb_signal oursig
;
9312 if (args
== nullptr)
9314 error_no_arg (_("signal to handle"));
9317 /* Allocate and zero an array of flags for which signals to handle. */
9319 const size_t nsigs
= GDB_SIGNAL_LAST
;
9320 unsigned char sigs
[nsigs
] {};
9322 /* Break the command line up into args. */
9324 gdb_argv
built_argv (args
);
9326 /* Walk through the args, looking for signal oursigs, signal names, and
9327 actions. Signal numbers and signal names may be interspersed with
9328 actions, with the actions being performed for all signals cumulatively
9329 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
9331 for (char *arg
: built_argv
)
9333 wordlen
= strlen (arg
);
9334 for (digits
= 0; isdigit (arg
[digits
]); digits
++)
9338 sigfirst
= siglast
= -1;
9340 if (wordlen
>= 1 && !strncmp (arg
, "all", wordlen
))
9342 /* Apply action to all signals except those used by the
9343 debugger. Silently skip those. */
9346 siglast
= nsigs
- 1;
9348 else if (wordlen
>= 1 && !strncmp (arg
, "stop", wordlen
))
9350 SET_SIGS (nsigs
, sigs
, signal_stop
);
9351 SET_SIGS (nsigs
, sigs
, signal_print
);
9353 else if (wordlen
>= 1 && !strncmp (arg
, "ignore", wordlen
))
9355 UNSET_SIGS (nsigs
, sigs
, signal_program
);
9357 else if (wordlen
>= 2 && !strncmp (arg
, "print", wordlen
))
9359 SET_SIGS (nsigs
, sigs
, signal_print
);
9361 else if (wordlen
>= 2 && !strncmp (arg
, "pass", wordlen
))
9363 SET_SIGS (nsigs
, sigs
, signal_program
);
9365 else if (wordlen
>= 3 && !strncmp (arg
, "nostop", wordlen
))
9367 UNSET_SIGS (nsigs
, sigs
, signal_stop
);
9369 else if (wordlen
>= 3 && !strncmp (arg
, "noignore", wordlen
))
9371 SET_SIGS (nsigs
, sigs
, signal_program
);
9373 else if (wordlen
>= 4 && !strncmp (arg
, "noprint", wordlen
))
9375 UNSET_SIGS (nsigs
, sigs
, signal_print
);
9376 UNSET_SIGS (nsigs
, sigs
, signal_stop
);
9378 else if (wordlen
>= 4 && !strncmp (arg
, "nopass", wordlen
))
9380 UNSET_SIGS (nsigs
, sigs
, signal_program
);
9382 else if (digits
> 0)
9384 /* It is numeric. The numeric signal refers to our own
9385 internal signal numbering from target.h, not to host/target
9386 signal number. This is a feature; users really should be
9387 using symbolic names anyway, and the common ones like
9388 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
9390 sigfirst
= siglast
= (int)
9391 gdb_signal_from_command (atoi (arg
));
9392 if (arg
[digits
] == '-')
9395 gdb_signal_from_command (atoi (arg
+ digits
+ 1));
9397 if (sigfirst
> siglast
)
9399 /* Bet he didn't figure we'd think of this case... */
9400 std::swap (sigfirst
, siglast
);
9405 oursig
= gdb_signal_from_name (arg
);
9406 if (oursig
!= GDB_SIGNAL_UNKNOWN
)
9408 sigfirst
= siglast
= (int) oursig
;
9412 /* Not a number and not a recognized flag word => complain. */
9413 error (_("Unrecognized or ambiguous flag word: \"%s\"."), arg
);
9417 /* If any signal numbers or symbol names were found, set flags for
9418 which signals to apply actions to. */
9420 for (int signum
= sigfirst
; signum
>= 0 && signum
<= siglast
; signum
++)
9422 switch ((enum gdb_signal
) signum
)
9424 case GDB_SIGNAL_TRAP
:
9425 case GDB_SIGNAL_INT
:
9426 if (!allsigs
&& !sigs
[signum
])
9428 if (query (_("%s is used by the debugger.\n\
9429 Are you sure you want to change it? "),
9430 gdb_signal_to_name ((enum gdb_signal
) signum
)))
9435 gdb_printf (_("Not confirmed, unchanged.\n"));
9439 case GDB_SIGNAL_DEFAULT
:
9440 case GDB_SIGNAL_UNKNOWN
:
9441 /* Make sure that "all" doesn't print these. */
9450 for (int signum
= 0; signum
< nsigs
; signum
++)
9453 signal_cache_update (-1);
9454 target_pass_signals (signal_pass
);
9455 target_program_signals (signal_program
);
9459 /* Show the results. */
9460 sig_print_header ();
9461 for (; signum
< nsigs
; signum
++)
9463 sig_print_info ((enum gdb_signal
) signum
);
9470 /* Complete the "handle" command. */
9473 handle_completer (struct cmd_list_element
*ignore
,
9474 completion_tracker
&tracker
,
9475 const char *text
, const char *word
)
9477 static const char * const keywords
[] =
9491 signal_completer (ignore
, tracker
, text
, word
);
9492 complete_on_enum (tracker
, keywords
, word
, word
);
9496 gdb_signal_from_command (int num
)
9498 if (num
>= 1 && num
<= 15)
9499 return (enum gdb_signal
) num
;
9500 error (_("Only signals 1-15 are valid as numeric signals.\n\
9501 Use \"info signals\" for a list of symbolic signals."));
9504 /* Print current contents of the tables set by the handle command.
9505 It is possible we should just be printing signals actually used
9506 by the current target (but for things to work right when switching
9507 targets, all signals should be in the signal tables). */
9510 info_signals_command (const char *signum_exp
, int from_tty
)
9512 enum gdb_signal oursig
;
9514 sig_print_header ();
9518 /* First see if this is a symbol name. */
9519 oursig
= gdb_signal_from_name (signum_exp
);
9520 if (oursig
== GDB_SIGNAL_UNKNOWN
)
9522 /* No, try numeric. */
9524 gdb_signal_from_command (parse_and_eval_long (signum_exp
));
9526 sig_print_info (oursig
);
9531 /* These ugly casts brought to you by the native VAX compiler. */
9532 for (oursig
= GDB_SIGNAL_FIRST
;
9533 (int) oursig
< (int) GDB_SIGNAL_LAST
;
9534 oursig
= (enum gdb_signal
) ((int) oursig
+ 1))
9538 if (oursig
!= GDB_SIGNAL_UNKNOWN
9539 && oursig
!= GDB_SIGNAL_DEFAULT
&& oursig
!= GDB_SIGNAL_0
)
9540 sig_print_info (oursig
);
9543 gdb_printf (_("\nUse the \"handle\" command "
9544 "to change these tables.\n"));
9547 /* The $_siginfo convenience variable is a bit special. We don't know
9548 for sure the type of the value until we actually have a chance to
9549 fetch the data. The type can change depending on gdbarch, so it is
9550 also dependent on which thread you have selected.
9552 1. making $_siginfo be an internalvar that creates a new value on
9555 2. making the value of $_siginfo be an lval_computed value. */
9557 /* This function implements the lval_computed support for reading a
9561 siginfo_value_read (struct value
*v
)
9563 LONGEST transferred
;
9565 /* If we can access registers, so can we access $_siginfo. Likewise
9567 validate_registers_access ();
9570 target_read (current_inferior ()->top_target (),
9571 TARGET_OBJECT_SIGNAL_INFO
,
9573 v
->contents_all_raw ().data (),
9575 v
->type ()->length ());
9577 if (transferred
!= v
->type ()->length ())
9578 error (_("Unable to read siginfo"));
9581 /* This function implements the lval_computed support for writing a
9585 siginfo_value_write (struct value
*v
, struct value
*fromval
)
9587 LONGEST transferred
;
9589 /* If we can access registers, so can we access $_siginfo. Likewise
9591 validate_registers_access ();
9593 transferred
= target_write (current_inferior ()->top_target (),
9594 TARGET_OBJECT_SIGNAL_INFO
,
9596 fromval
->contents_all_raw ().data (),
9598 fromval
->type ()->length ());
9600 if (transferred
!= fromval
->type ()->length ())
9601 error (_("Unable to write siginfo"));
9604 static const struct lval_funcs siginfo_value_funcs
=
9610 /* Return a new value with the correct type for the siginfo object of
9611 the current thread using architecture GDBARCH. Return a void value
9612 if there's no object available. */
9614 static struct value
*
9615 siginfo_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
9618 if (target_has_stack ()
9619 && inferior_ptid
!= null_ptid
9620 && gdbarch_get_siginfo_type_p (gdbarch
))
9622 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9624 return value::allocate_computed (type
, &siginfo_value_funcs
, nullptr);
9627 return value::allocate (builtin_type (gdbarch
)->builtin_void
);
9631 /* infcall_suspend_state contains state about the program itself like its
9632 registers and any signal it received when it last stopped.
9633 This state must be restored regardless of how the inferior function call
9634 ends (either successfully, or after it hits a breakpoint or signal)
9635 if the program is to properly continue where it left off. */
9637 class infcall_suspend_state
9640 /* Capture state from GDBARCH, TP, and REGCACHE that must be restored
9641 once the inferior function call has finished. */
9642 infcall_suspend_state (struct gdbarch
*gdbarch
,
9643 const struct thread_info
*tp
,
9644 struct regcache
*regcache
)
9645 : m_registers (new readonly_detached_regcache (*regcache
))
9647 tp
->save_suspend_to (m_thread_suspend
);
9649 gdb::unique_xmalloc_ptr
<gdb_byte
> siginfo_data
;
9651 if (gdbarch_get_siginfo_type_p (gdbarch
))
9653 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9654 size_t len
= type
->length ();
9656 siginfo_data
.reset ((gdb_byte
*) xmalloc (len
));
9658 if (target_read (current_inferior ()->top_target (),
9659 TARGET_OBJECT_SIGNAL_INFO
, nullptr,
9660 siginfo_data
.get (), 0, len
) != len
)
9662 /* Errors ignored. */
9663 siginfo_data
.reset (nullptr);
9669 m_siginfo_gdbarch
= gdbarch
;
9670 m_siginfo_data
= std::move (siginfo_data
);
9674 /* Return a pointer to the stored register state. */
9676 readonly_detached_regcache
*registers () const
9678 return m_registers
.get ();
9681 /* Restores the stored state into GDBARCH, TP, and REGCACHE. */
9683 void restore (struct gdbarch
*gdbarch
,
9684 struct thread_info
*tp
,
9685 struct regcache
*regcache
) const
9687 tp
->restore_suspend_from (m_thread_suspend
);
9689 if (m_siginfo_gdbarch
== gdbarch
)
9691 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9693 /* Errors ignored. */
9694 target_write (current_inferior ()->top_target (),
9695 TARGET_OBJECT_SIGNAL_INFO
, nullptr,
9696 m_siginfo_data
.get (), 0, type
->length ());
9699 /* The inferior can be gone if the user types "print exit(0)"
9700 (and perhaps other times). */
9701 if (target_has_execution ())
9702 /* NB: The register write goes through to the target. */
9703 regcache
->restore (registers ());
9707 /* How the current thread stopped before the inferior function call was
9709 struct thread_suspend_state m_thread_suspend
;
9711 /* The registers before the inferior function call was executed. */
9712 std::unique_ptr
<readonly_detached_regcache
> m_registers
;
9714 /* Format of SIGINFO_DATA or NULL if it is not present. */
9715 struct gdbarch
*m_siginfo_gdbarch
= nullptr;
9717 /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
9718 gdbarch_get_siginfo_type ()->length (). For different gdbarch the
9719 content would be invalid. */
9720 gdb::unique_xmalloc_ptr
<gdb_byte
> m_siginfo_data
;
9723 infcall_suspend_state_up
9724 save_infcall_suspend_state ()
9726 struct thread_info
*tp
= inferior_thread ();
9727 struct regcache
*regcache
= get_current_regcache ();
9728 struct gdbarch
*gdbarch
= regcache
->arch ();
9730 infcall_suspend_state_up inf_state
9731 (new struct infcall_suspend_state (gdbarch
, tp
, regcache
));
9733 /* Having saved the current state, adjust the thread state, discarding
9734 any stop signal information. The stop signal is not useful when
9735 starting an inferior function call, and run_inferior_call will not use
9736 the signal due to its `proceed' call with GDB_SIGNAL_0. */
9737 tp
->set_stop_signal (GDB_SIGNAL_0
);
9742 /* Restore inferior session state to INF_STATE. */
9745 restore_infcall_suspend_state (struct infcall_suspend_state
*inf_state
)
9747 struct thread_info
*tp
= inferior_thread ();
9748 struct regcache
*regcache
= get_current_regcache ();
9749 struct gdbarch
*gdbarch
= regcache
->arch ();
9751 inf_state
->restore (gdbarch
, tp
, regcache
);
9752 discard_infcall_suspend_state (inf_state
);
9756 discard_infcall_suspend_state (struct infcall_suspend_state
*inf_state
)
9761 readonly_detached_regcache
*
9762 get_infcall_suspend_state_regcache (struct infcall_suspend_state
*inf_state
)
9764 return inf_state
->registers ();
9767 /* infcall_control_state contains state regarding gdb's control of the
9768 inferior itself like stepping control. It also contains session state like
9769 the user's currently selected frame. */
9771 struct infcall_control_state
9773 struct thread_control_state thread_control
;
9774 struct inferior_control_state inferior_control
;
9777 enum stop_stack_kind stop_stack_dummy
= STOP_NONE
;
9778 int stopped_by_random_signal
= 0;
9780 /* ID and level of the selected frame when the inferior function
9782 struct frame_id selected_frame_id
{};
9783 int selected_frame_level
= -1;
9786 /* Save all of the information associated with the inferior<==>gdb
9789 infcall_control_state_up
9790 save_infcall_control_state ()
9792 infcall_control_state_up
inf_status (new struct infcall_control_state
);
9793 struct thread_info
*tp
= inferior_thread ();
9794 struct inferior
*inf
= current_inferior ();
9796 inf_status
->thread_control
= tp
->control
;
9797 inf_status
->inferior_control
= inf
->control
;
9799 tp
->control
.step_resume_breakpoint
= nullptr;
9800 tp
->control
.exception_resume_breakpoint
= nullptr;
9802 /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
9803 chain. If caller's caller is walking the chain, they'll be happier if we
9804 hand them back the original chain when restore_infcall_control_state is
9806 tp
->control
.stop_bpstat
= bpstat_copy (tp
->control
.stop_bpstat
);
9809 inf_status
->stop_stack_dummy
= stop_stack_dummy
;
9810 inf_status
->stopped_by_random_signal
= stopped_by_random_signal
;
9812 save_selected_frame (&inf_status
->selected_frame_id
,
9813 &inf_status
->selected_frame_level
);
9818 /* Restore inferior session state to INF_STATUS. */
9821 restore_infcall_control_state (struct infcall_control_state
*inf_status
)
9823 struct thread_info
*tp
= inferior_thread ();
9824 struct inferior
*inf
= current_inferior ();
9826 if (tp
->control
.step_resume_breakpoint
)
9827 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
9829 if (tp
->control
.exception_resume_breakpoint
)
9830 tp
->control
.exception_resume_breakpoint
->disposition
9831 = disp_del_at_next_stop
;
9833 /* Handle the bpstat_copy of the chain. */
9834 bpstat_clear (&tp
->control
.stop_bpstat
);
9836 tp
->control
= inf_status
->thread_control
;
9837 inf
->control
= inf_status
->inferior_control
;
9840 stop_stack_dummy
= inf_status
->stop_stack_dummy
;
9841 stopped_by_random_signal
= inf_status
->stopped_by_random_signal
;
9843 if (target_has_stack ())
9845 restore_selected_frame (inf_status
->selected_frame_id
,
9846 inf_status
->selected_frame_level
);
9853 discard_infcall_control_state (struct infcall_control_state
*inf_status
)
9855 if (inf_status
->thread_control
.step_resume_breakpoint
)
9856 inf_status
->thread_control
.step_resume_breakpoint
->disposition
9857 = disp_del_at_next_stop
;
9859 if (inf_status
->thread_control
.exception_resume_breakpoint
)
9860 inf_status
->thread_control
.exception_resume_breakpoint
->disposition
9861 = disp_del_at_next_stop
;
9863 /* See save_infcall_control_state for info on stop_bpstat. */
9864 bpstat_clear (&inf_status
->thread_control
.stop_bpstat
);
9872 clear_exit_convenience_vars (void)
9874 clear_internalvar (lookup_internalvar ("_exitsignal"));
9875 clear_internalvar (lookup_internalvar ("_exitcode"));
9879 /* User interface for reverse debugging:
9880 Set exec-direction / show exec-direction commands
9881 (returns error unless target implements to_set_exec_direction method). */
9883 enum exec_direction_kind execution_direction
= EXEC_FORWARD
;
9884 static const char exec_forward
[] = "forward";
9885 static const char exec_reverse
[] = "reverse";
9886 static const char *exec_direction
= exec_forward
;
9887 static const char *const exec_direction_names
[] = {
9894 set_exec_direction_func (const char *args
, int from_tty
,
9895 struct cmd_list_element
*cmd
)
9897 if (target_can_execute_reverse ())
9899 if (!strcmp (exec_direction
, exec_forward
))
9900 execution_direction
= EXEC_FORWARD
;
9901 else if (!strcmp (exec_direction
, exec_reverse
))
9902 execution_direction
= EXEC_REVERSE
;
9906 exec_direction
= exec_forward
;
9907 error (_("Target does not support this operation."));
9912 show_exec_direction_func (struct ui_file
*out
, int from_tty
,
9913 struct cmd_list_element
*cmd
, const char *value
)
9915 switch (execution_direction
) {
9917 gdb_printf (out
, _("Forward.\n"));
9920 gdb_printf (out
, _("Reverse.\n"));
9923 internal_error (_("bogus execution_direction value: %d"),
9924 (int) execution_direction
);
9929 show_schedule_multiple (struct ui_file
*file
, int from_tty
,
9930 struct cmd_list_element
*c
, const char *value
)
9932 gdb_printf (file
, _("Resuming the execution of threads "
9933 "of all processes is %s.\n"), value
);
9936 /* Implementation of `siginfo' variable. */
9938 static const struct internalvar_funcs siginfo_funcs
=
9944 /* Callback for infrun's target events source. This is marked when a
9945 thread has a pending status to process. */
9948 infrun_async_inferior_event_handler (gdb_client_data data
)
9950 clear_async_event_handler (infrun_async_inferior_event_token
);
9951 inferior_event_handler (INF_REG_EVENT
);
9958 /* Verify that when two threads with the same ptid exist (from two different
9959 targets) and one of them changes ptid, we only update inferior_ptid if
9960 it is appropriate. */
9963 infrun_thread_ptid_changed ()
9965 gdbarch
*arch
= current_inferior ()->arch ();
9967 /* The thread which inferior_ptid represents changes ptid. */
9969 scoped_restore_current_pspace_and_thread restore
;
9971 scoped_mock_context
<test_target_ops
> target1 (arch
);
9972 scoped_mock_context
<test_target_ops
> target2 (arch
);
9974 ptid_t
old_ptid (111, 222);
9975 ptid_t
new_ptid (111, 333);
9977 target1
.mock_inferior
.pid
= old_ptid
.pid ();
9978 target1
.mock_thread
.ptid
= old_ptid
;
9979 target1
.mock_inferior
.ptid_thread_map
.clear ();
9980 target1
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target1
.mock_thread
;
9982 target2
.mock_inferior
.pid
= old_ptid
.pid ();
9983 target2
.mock_thread
.ptid
= old_ptid
;
9984 target2
.mock_inferior
.ptid_thread_map
.clear ();
9985 target2
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target2
.mock_thread
;
9987 auto restore_inferior_ptid
= make_scoped_restore (&inferior_ptid
, old_ptid
);
9988 set_current_inferior (&target1
.mock_inferior
);
9990 thread_change_ptid (&target1
.mock_target
, old_ptid
, new_ptid
);
9992 gdb_assert (inferior_ptid
== new_ptid
);
9995 /* A thread with the same ptid as inferior_ptid, but from another target,
9998 scoped_restore_current_pspace_and_thread restore
;
10000 scoped_mock_context
<test_target_ops
> target1 (arch
);
10001 scoped_mock_context
<test_target_ops
> target2 (arch
);
10003 ptid_t
old_ptid (111, 222);
10004 ptid_t
new_ptid (111, 333);
10006 target1
.mock_inferior
.pid
= old_ptid
.pid ();
10007 target1
.mock_thread
.ptid
= old_ptid
;
10008 target1
.mock_inferior
.ptid_thread_map
.clear ();
10009 target1
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target1
.mock_thread
;
10011 target2
.mock_inferior
.pid
= old_ptid
.pid ();
10012 target2
.mock_thread
.ptid
= old_ptid
;
10013 target2
.mock_inferior
.ptid_thread_map
.clear ();
10014 target2
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target2
.mock_thread
;
10016 auto restore_inferior_ptid
= make_scoped_restore (&inferior_ptid
, old_ptid
);
10017 set_current_inferior (&target2
.mock_inferior
);
10019 thread_change_ptid (&target1
.mock_target
, old_ptid
, new_ptid
);
10021 gdb_assert (inferior_ptid
== old_ptid
);
10025 } /* namespace selftests */
10027 #endif /* GDB_SELF_TEST */
10029 void _initialize_infrun ();
10031 _initialize_infrun ()
10033 struct cmd_list_element
*c
;
10035 /* Register extra event sources in the event loop. */
10036 infrun_async_inferior_event_token
10037 = create_async_event_handler (infrun_async_inferior_event_handler
, nullptr,
10040 cmd_list_element
*info_signals_cmd
10041 = add_info ("signals", info_signals_command
, _("\
10042 What debugger does when program gets various signals.\n\
10043 Specify a signal as argument to print info on that signal only."));
10044 add_info_alias ("handle", info_signals_cmd
, 0);
10046 c
= add_com ("handle", class_run
, handle_command
, _("\
10047 Specify how to handle signals.\n\
10048 Usage: handle SIGNAL [ACTIONS]\n\
10049 Args are signals and actions to apply to those signals.\n\
10050 If no actions are specified, the current settings for the specified signals\n\
10051 will be displayed instead.\n\
10053 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
10054 from 1-15 are allowed for compatibility with old versions of GDB.\n\
10055 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
10056 The special arg \"all\" is recognized to mean all signals except those\n\
10057 used by the debugger, typically SIGTRAP and SIGINT.\n\
10059 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
10060 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
10061 Stop means reenter debugger if this signal happens (implies print).\n\
10062 Print means print a message if this signal happens.\n\
10063 Pass means let program see this signal; otherwise program doesn't know.\n\
10064 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
10065 Pass and Stop may be combined.\n\
10067 Multiple signals may be specified. Signal numbers and signal names\n\
10068 may be interspersed with actions, with the actions being performed for\n\
10069 all signals cumulatively specified."));
10070 set_cmd_completer (c
, handle_completer
);
10072 stop_command
= add_cmd ("stop", class_obscure
,
10073 not_just_help_class_command
, _("\
10074 There is no `stop' command, but you can set a hook on `stop'.\n\
10075 This allows you to set a list of commands to be run each time execution\n\
10076 of the program stops."), &cmdlist
);
10078 add_setshow_boolean_cmd
10079 ("infrun", class_maintenance
, &debug_infrun
,
10080 _("Set inferior debugging."),
10081 _("Show inferior debugging."),
10082 _("When non-zero, inferior specific debugging is enabled."),
10083 nullptr, show_debug_infrun
, &setdebuglist
, &showdebuglist
);
10085 add_setshow_boolean_cmd ("non-stop", no_class
,
10087 Set whether gdb controls the inferior in non-stop mode."), _("\
10088 Show whether gdb controls the inferior in non-stop mode."), _("\
10089 When debugging a multi-threaded program and this setting is\n\
10090 off (the default, also called all-stop mode), when one thread stops\n\
10091 (for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
10092 all other threads in the program while you interact with the thread of\n\
10093 interest. When you continue or step a thread, you can allow the other\n\
10094 threads to run, or have them remain stopped, but while you inspect any\n\
10095 thread's state, all threads stop.\n\
10097 In non-stop mode, when one thread stops, other threads can continue\n\
10098 to run freely. You'll be able to step each thread independently,\n\
10099 leave it stopped or free to run as needed."),
10105 for (size_t i
= 0; i
< GDB_SIGNAL_LAST
; i
++)
10107 signal_stop
[i
] = 1;
10108 signal_print
[i
] = 1;
10109 signal_program
[i
] = 1;
10110 signal_catch
[i
] = 0;
10113 /* Signals caused by debugger's own actions should not be given to
10114 the program afterwards.
10116 Do not deliver GDB_SIGNAL_TRAP by default, except when the user
10117 explicitly specifies that it should be delivered to the target
10118 program. Typically, that would occur when a user is debugging a
10119 target monitor on a simulator: the target monitor sets a
10120 breakpoint; the simulator encounters this breakpoint and halts
10121 the simulation handing control to GDB; GDB, noting that the stop
10122 address doesn't map to any known breakpoint, returns control back
10123 to the simulator; the simulator then delivers the hardware
10124 equivalent of a GDB_SIGNAL_TRAP to the program being
10126 signal_program
[GDB_SIGNAL_TRAP
] = 0;
10127 signal_program
[GDB_SIGNAL_INT
] = 0;
10129 /* Signals that are not errors should not normally enter the debugger. */
10130 signal_stop
[GDB_SIGNAL_ALRM
] = 0;
10131 signal_print
[GDB_SIGNAL_ALRM
] = 0;
10132 signal_stop
[GDB_SIGNAL_VTALRM
] = 0;
10133 signal_print
[GDB_SIGNAL_VTALRM
] = 0;
10134 signal_stop
[GDB_SIGNAL_PROF
] = 0;
10135 signal_print
[GDB_SIGNAL_PROF
] = 0;
10136 signal_stop
[GDB_SIGNAL_CHLD
] = 0;
10137 signal_print
[GDB_SIGNAL_CHLD
] = 0;
10138 signal_stop
[GDB_SIGNAL_IO
] = 0;
10139 signal_print
[GDB_SIGNAL_IO
] = 0;
10140 signal_stop
[GDB_SIGNAL_POLL
] = 0;
10141 signal_print
[GDB_SIGNAL_POLL
] = 0;
10142 signal_stop
[GDB_SIGNAL_URG
] = 0;
10143 signal_print
[GDB_SIGNAL_URG
] = 0;
10144 signal_stop
[GDB_SIGNAL_WINCH
] = 0;
10145 signal_print
[GDB_SIGNAL_WINCH
] = 0;
10146 signal_stop
[GDB_SIGNAL_PRIO
] = 0;
10147 signal_print
[GDB_SIGNAL_PRIO
] = 0;
10149 /* These signals are used internally by user-level thread
10150 implementations. (See signal(5) on Solaris.) Like the above
10151 signals, a healthy program receives and handles them as part of
10152 its normal operation. */
10153 signal_stop
[GDB_SIGNAL_LWP
] = 0;
10154 signal_print
[GDB_SIGNAL_LWP
] = 0;
10155 signal_stop
[GDB_SIGNAL_WAITING
] = 0;
10156 signal_print
[GDB_SIGNAL_WAITING
] = 0;
10157 signal_stop
[GDB_SIGNAL_CANCEL
] = 0;
10158 signal_print
[GDB_SIGNAL_CANCEL
] = 0;
10159 signal_stop
[GDB_SIGNAL_LIBRT
] = 0;
10160 signal_print
[GDB_SIGNAL_LIBRT
] = 0;
10162 /* Update cached state. */
10163 signal_cache_update (-1);
10165 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support
,
10166 &stop_on_solib_events
, _("\
10167 Set stopping for shared library events."), _("\
10168 Show stopping for shared library events."), _("\
10169 If nonzero, gdb will give control to the user when the dynamic linker\n\
10170 notifies gdb of shared library events. The most common event of interest\n\
10171 to the user would be loading/unloading of a new library."),
10172 set_stop_on_solib_events
,
10173 show_stop_on_solib_events
,
10174 &setlist
, &showlist
);
10176 add_setshow_enum_cmd ("follow-fork-mode", class_run
,
10177 follow_fork_mode_kind_names
,
10178 &follow_fork_mode_string
, _("\
10179 Set debugger response to a program call of fork or vfork."), _("\
10180 Show debugger response to a program call of fork or vfork."), _("\
10181 A fork or vfork creates a new process. follow-fork-mode can be:\n\
10182 parent - the original process is debugged after a fork\n\
10183 child - the new process is debugged after a fork\n\
10184 The unfollowed process will continue to run.\n\
10185 By default, the debugger will follow the parent process."),
10187 show_follow_fork_mode_string
,
10188 &setlist
, &showlist
);
10190 add_setshow_enum_cmd ("follow-exec-mode", class_run
,
10191 follow_exec_mode_names
,
10192 &follow_exec_mode_string
, _("\
10193 Set debugger response to a program call of exec."), _("\
10194 Show debugger response to a program call of exec."), _("\
10195 An exec call replaces the program image of a process.\n\
10197 follow-exec-mode can be:\n\
10199 new - the debugger creates a new inferior and rebinds the process\n\
10200 to this new inferior. The program the process was running before\n\
10201 the exec call can be restarted afterwards by restarting the original\n\
10204 same - the debugger keeps the process bound to the same inferior.\n\
10205 The new executable image replaces the previous executable loaded in\n\
10206 the inferior. Restarting the inferior after the exec call restarts\n\
10207 the executable the process was running after the exec call.\n\
10209 By default, the debugger will use the same inferior."),
10211 show_follow_exec_mode_string
,
10212 &setlist
, &showlist
);
10214 add_setshow_enum_cmd ("scheduler-locking", class_run
,
10215 scheduler_enums
, &scheduler_mode
, _("\
10216 Set mode for locking scheduler during execution."), _("\
10217 Show mode for locking scheduler during execution."), _("\
10218 off == no locking (threads may preempt at any time)\n\
10219 on == full locking (no thread except the current thread may run)\n\
10220 This applies to both normal execution and replay mode.\n\
10221 step == scheduler locked during stepping commands (step, next, stepi, nexti).\n\
10222 In this mode, other threads may run during other commands.\n\
10223 This applies to both normal execution and replay mode.\n\
10224 replay == scheduler locked in replay mode and unlocked during normal execution."),
10225 set_schedlock_func
, /* traps on target vector */
10226 show_scheduler_mode
,
10227 &setlist
, &showlist
);
10229 add_setshow_boolean_cmd ("schedule-multiple", class_run
, &sched_multi
, _("\
10230 Set mode for resuming threads of all processes."), _("\
10231 Show mode for resuming threads of all processes."), _("\
10232 When on, execution commands (such as 'continue' or 'next') resume all\n\
10233 threads of all processes. When off (which is the default), execution\n\
10234 commands only resume the threads of the current process. The set of\n\
10235 threads that are resumed is further refined by the scheduler-locking\n\
10236 mode (see help set scheduler-locking)."),
10238 show_schedule_multiple
,
10239 &setlist
, &showlist
);
10241 add_setshow_boolean_cmd ("step-mode", class_run
, &step_stop_if_no_debug
, _("\
10242 Set mode of the step operation."), _("\
10243 Show mode of the step operation."), _("\
10244 When set, doing a step over a function without debug line information\n\
10245 will stop at the first instruction of that function. Otherwise, the\n\
10246 function is skipped and the step command stops at a different source line."),
10248 show_step_stop_if_no_debug
,
10249 &setlist
, &showlist
);
10251 add_setshow_auto_boolean_cmd ("displaced-stepping", class_run
,
10252 &can_use_displaced_stepping
, _("\
10253 Set debugger's willingness to use displaced stepping."), _("\
10254 Show debugger's willingness to use displaced stepping."), _("\
10255 If on, gdb will use displaced stepping to step over breakpoints if it is\n\
10256 supported by the target architecture. If off, gdb will not use displaced\n\
10257 stepping to step over breakpoints, even if such is supported by the target\n\
10258 architecture. If auto (which is the default), gdb will use displaced stepping\n\
10259 if the target architecture supports it and non-stop mode is active, but will not\n\
10260 use it in all-stop mode (see help set non-stop)."),
10262 show_can_use_displaced_stepping
,
10263 &setlist
, &showlist
);
10265 add_setshow_enum_cmd ("exec-direction", class_run
, exec_direction_names
,
10266 &exec_direction
, _("Set direction of execution.\n\
10267 Options are 'forward' or 'reverse'."),
10268 _("Show direction of execution (forward/reverse)."),
10269 _("Tells gdb whether to execute forward or backward."),
10270 set_exec_direction_func
, show_exec_direction_func
,
10271 &setlist
, &showlist
);
10273 /* Set/show detach-on-fork: user-settable mode. */
10275 add_setshow_boolean_cmd ("detach-on-fork", class_run
, &detach_fork
, _("\
10276 Set whether gdb will detach the child of a fork."), _("\
10277 Show whether gdb will detach the child of a fork."), _("\
10278 Tells gdb whether to detach the child of a fork."),
10279 nullptr, nullptr, &setlist
, &showlist
);
10281 /* Set/show disable address space randomization mode. */
10283 add_setshow_boolean_cmd ("disable-randomization", class_support
,
10284 &disable_randomization
, _("\
10285 Set disabling of debuggee's virtual address space randomization."), _("\
10286 Show disabling of debuggee's virtual address space randomization."), _("\
10287 When this mode is on (which is the default), randomization of the virtual\n\
10288 address space is disabled. Standalone programs run with the randomization\n\
10289 enabled by default on some platforms."),
10290 &set_disable_randomization
,
10291 &show_disable_randomization
,
10292 &setlist
, &showlist
);
10294 /* ptid initializations */
10295 inferior_ptid
= null_ptid
;
10296 target_last_wait_ptid
= minus_one_ptid
;
10298 gdb::observers::thread_ptid_changed
.attach (infrun_thread_ptid_changed
,
10300 gdb::observers::thread_stop_requested
.attach (infrun_thread_stop_requested
,
10302 gdb::observers::inferior_exit
.attach (infrun_inferior_exit
, "infrun");
10303 gdb::observers::inferior_execd
.attach (infrun_inferior_execd
, "infrun");
10305 /* Explicitly create without lookup, since that tries to create a
10306 value with a void typed value, and when we get here, gdbarch
10307 isn't initialized yet. At this point, we're quite sure there
10308 isn't another convenience variable of the same name. */
10309 create_internalvar_type_lazy ("_siginfo", &siginfo_funcs
, nullptr);
10311 add_setshow_boolean_cmd ("observer", no_class
,
10312 &observer_mode_1
, _("\
10313 Set whether gdb controls the inferior in observer mode."), _("\
10314 Show whether gdb controls the inferior in observer mode."), _("\
10315 In observer mode, GDB can get data from the inferior, but not\n\
10316 affect its execution. Registers and memory may not be changed,\n\
10317 breakpoints may not be set, and the program cannot be interrupted\n\
10320 show_observer_mode
,
10325 selftests::register_test ("infrun_thread_ptid_changed",
10326 selftests::infrun_thread_ptid_changed
);