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. */
1610 displaced_step_in_progress_thread (thread_info
*thread
)
1612 gdb_assert (thread
!= nullptr);
1614 return thread
->displaced_step_state
.in_progress ();
1617 /* Return true if INF has a thread doing a displaced step. */
1620 displaced_step_in_progress (inferior
*inf
)
1622 return inf
->displaced_step_state
.in_progress_count
> 0;
1625 /* Return true if any thread is doing a displaced step. */
1628 displaced_step_in_progress_any_thread ()
1630 for (inferior
*inf
: all_non_exited_inferiors ())
1632 if (displaced_step_in_progress (inf
))
1640 infrun_inferior_exit (struct inferior
*inf
)
1642 inf
->displaced_step_state
.reset ();
1643 inf
->thread_waiting_for_vfork_done
= nullptr;
1647 infrun_inferior_execd (inferior
*exec_inf
, inferior
*follow_inf
)
1649 /* If some threads where was doing a displaced step in this inferior at the
1650 moment of the exec, they no longer exist. Even if the exec'ing thread
1651 doing a displaced step, we don't want to to any fixup nor restore displaced
1652 stepping buffer bytes. */
1653 follow_inf
->displaced_step_state
.reset ();
1655 for (thread_info
*thread
: follow_inf
->threads ())
1656 thread
->displaced_step_state
.reset ();
1658 /* Since an in-line step is done with everything else stopped, if there was
1659 one in progress at the time of the exec, it must have been the exec'ing
1661 clear_step_over_info ();
1663 follow_inf
->thread_waiting_for_vfork_done
= nullptr;
1666 /* If ON, and the architecture supports it, GDB will use displaced
1667 stepping to step over breakpoints. If OFF, or if the architecture
1668 doesn't support it, GDB will instead use the traditional
1669 hold-and-step approach. If AUTO (which is the default), GDB will
1670 decide which technique to use to step over breakpoints depending on
1671 whether the target works in a non-stop way (see use_displaced_stepping). */
1673 static enum auto_boolean can_use_displaced_stepping
= AUTO_BOOLEAN_AUTO
;
1676 show_can_use_displaced_stepping (struct ui_file
*file
, int from_tty
,
1677 struct cmd_list_element
*c
,
1680 if (can_use_displaced_stepping
== AUTO_BOOLEAN_AUTO
)
1682 _("Debugger's willingness to use displaced stepping "
1683 "to step over breakpoints is %s (currently %s).\n"),
1684 value
, target_is_non_stop_p () ? "on" : "off");
1687 _("Debugger's willingness to use displaced stepping "
1688 "to step over breakpoints is %s.\n"), value
);
1691 /* Return true if the gdbarch implements the required methods to use
1692 displaced stepping. */
1695 gdbarch_supports_displaced_stepping (gdbarch
*arch
)
1697 /* Only check for the presence of `prepare`. The gdbarch verification ensures
1698 that if `prepare` is provided, so is `finish`. */
1699 return gdbarch_displaced_step_prepare_p (arch
);
1702 /* Return non-zero if displaced stepping can/should be used to step
1703 over breakpoints of thread TP. */
1706 use_displaced_stepping (thread_info
*tp
)
1708 /* If the user disabled it explicitly, don't use displaced stepping. */
1709 if (can_use_displaced_stepping
== AUTO_BOOLEAN_FALSE
)
1712 /* If "auto", only use displaced stepping if the target operates in a non-stop
1714 if (can_use_displaced_stepping
== AUTO_BOOLEAN_AUTO
1715 && !target_is_non_stop_p ())
1718 gdbarch
*gdbarch
= get_thread_regcache (tp
)->arch ();
1720 /* If the architecture doesn't implement displaced stepping, don't use
1722 if (!gdbarch_supports_displaced_stepping (gdbarch
))
1725 /* If recording, don't use displaced stepping. */
1726 if (find_record_target () != nullptr)
1729 /* If displaced stepping failed before for this inferior, don't bother trying
1731 if (tp
->inf
->displaced_step_state
.failed_before
)
1737 /* Simple function wrapper around displaced_step_thread_state::reset. */
1740 displaced_step_reset (displaced_step_thread_state
*displaced
)
1742 displaced
->reset ();
1745 /* A cleanup that wraps displaced_step_reset. We use this instead of, say,
1746 SCOPE_EXIT, because it needs to be discardable with "cleanup.release ()". */
1748 using displaced_step_reset_cleanup
= FORWARD_SCOPE_EXIT (displaced_step_reset
);
1750 /* Prepare to single-step, using displaced stepping.
1752 Note that we cannot use displaced stepping when we have a signal to
1753 deliver. If we have a signal to deliver and an instruction to step
1754 over, then after the step, there will be no indication from the
1755 target whether the thread entered a signal handler or ignored the
1756 signal and stepped over the instruction successfully --- both cases
1757 result in a simple SIGTRAP. In the first case we mustn't do a
1758 fixup, and in the second case we must --- but we can't tell which.
1759 Comments in the code for 'random signals' in handle_inferior_event
1760 explain how we handle this case instead.
1762 Returns DISPLACED_STEP_PREPARE_STATUS_OK if preparing was successful -- this
1763 thread is going to be stepped now; DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
1764 if displaced stepping this thread got queued; or
1765 DISPLACED_STEP_PREPARE_STATUS_CANT if this instruction can't be displaced
1768 static displaced_step_prepare_status
1769 displaced_step_prepare_throw (thread_info
*tp
)
1771 regcache
*regcache
= get_thread_regcache (tp
);
1772 struct gdbarch
*gdbarch
= regcache
->arch ();
1773 displaced_step_thread_state
&disp_step_thread_state
1774 = tp
->displaced_step_state
;
1776 /* We should never reach this function if the architecture does not
1777 support displaced stepping. */
1778 gdb_assert (gdbarch_supports_displaced_stepping (gdbarch
));
1780 /* Nor if the thread isn't meant to step over a breakpoint. */
1781 gdb_assert (tp
->control
.trap_expected
);
1783 /* Disable range stepping while executing in the scratch pad. We
1784 want a single-step even if executing the displaced instruction in
1785 the scratch buffer lands within the stepping range (e.g., a
1787 tp
->control
.may_range_step
= 0;
1789 /* We are about to start a displaced step for this thread. If one is already
1790 in progress, something's wrong. */
1791 gdb_assert (!disp_step_thread_state
.in_progress ());
1793 if (tp
->inf
->displaced_step_state
.unavailable
)
1795 /* The gdbarch tells us it's not worth asking to try a prepare because
1796 it is likely that it will return unavailable, so don't bother asking. */
1798 displaced_debug_printf ("deferring step of %s",
1799 tp
->ptid
.to_string ().c_str ());
1801 global_thread_step_over_chain_enqueue (tp
);
1802 return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
;
1805 displaced_debug_printf ("displaced-stepping %s now",
1806 tp
->ptid
.to_string ().c_str ());
1808 scoped_restore_current_thread restore_thread
;
1810 switch_to_thread (tp
);
1812 CORE_ADDR original_pc
= regcache_read_pc (regcache
);
1813 CORE_ADDR displaced_pc
;
1815 /* Display the instruction we are going to displaced step. */
1816 if (debug_displaced
)
1818 string_file tmp_stream
;
1819 int dislen
= gdb_print_insn (gdbarch
, original_pc
, &tmp_stream
,
1824 gdb::byte_vector
insn_buf (dislen
);
1825 read_memory (original_pc
, insn_buf
.data (), insn_buf
.size ());
1827 std::string insn_bytes
= bytes_to_string (insn_buf
);
1829 displaced_debug_printf ("original insn %s: %s \t %s",
1830 paddress (gdbarch
, original_pc
),
1831 insn_bytes
.c_str (),
1832 tmp_stream
.string ().c_str ());
1835 displaced_debug_printf ("original insn %s: invalid length: %d",
1836 paddress (gdbarch
, original_pc
), dislen
);
1839 displaced_step_prepare_status status
1840 = gdbarch_displaced_step_prepare (gdbarch
, tp
, displaced_pc
);
1842 if (status
== DISPLACED_STEP_PREPARE_STATUS_CANT
)
1844 displaced_debug_printf ("failed to prepare (%s)",
1845 tp
->ptid
.to_string ().c_str ());
1847 return DISPLACED_STEP_PREPARE_STATUS_CANT
;
1849 else if (status
== DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
)
1851 /* Not enough displaced stepping resources available, defer this
1852 request by placing it the queue. */
1854 displaced_debug_printf ("not enough resources available, "
1855 "deferring step of %s",
1856 tp
->ptid
.to_string ().c_str ());
1858 global_thread_step_over_chain_enqueue (tp
);
1860 return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
;
1863 gdb_assert (status
== DISPLACED_STEP_PREPARE_STATUS_OK
);
1865 /* Save the information we need to fix things up if the step
1867 disp_step_thread_state
.set (gdbarch
);
1869 tp
->inf
->displaced_step_state
.in_progress_count
++;
1871 displaced_debug_printf ("prepared successfully thread=%s, "
1872 "original_pc=%s, displaced_pc=%s",
1873 tp
->ptid
.to_string ().c_str (),
1874 paddress (gdbarch
, original_pc
),
1875 paddress (gdbarch
, displaced_pc
));
1877 /* Display the new displaced instruction(s). */
1878 if (debug_displaced
)
1880 string_file tmp_stream
;
1881 CORE_ADDR addr
= displaced_pc
;
1883 /* If displaced stepping is going to use h/w single step then we know
1884 that the replacement instruction can only be a single instruction,
1885 in that case set the end address at the next byte.
1887 Otherwise the displaced stepping copy instruction routine could
1888 have generated multiple instructions, and all we know is that they
1889 must fit within the LEN bytes of the buffer. */
1891 = addr
+ (gdbarch_displaced_step_hw_singlestep (gdbarch
)
1892 ? 1 : gdbarch_displaced_step_buffer_length (gdbarch
));
1896 int dislen
= gdb_print_insn (gdbarch
, addr
, &tmp_stream
, nullptr);
1899 displaced_debug_printf
1900 ("replacement insn %s: invalid length: %d",
1901 paddress (gdbarch
, addr
), dislen
);
1905 gdb::byte_vector
insn_buf (dislen
);
1906 read_memory (addr
, insn_buf
.data (), insn_buf
.size ());
1908 std::string insn_bytes
= bytes_to_string (insn_buf
);
1909 std::string insn_str
= tmp_stream
.release ();
1910 displaced_debug_printf ("replacement insn %s: %s \t %s",
1911 paddress (gdbarch
, addr
),
1912 insn_bytes
.c_str (),
1918 return DISPLACED_STEP_PREPARE_STATUS_OK
;
1921 /* Wrapper for displaced_step_prepare_throw that disabled further
1922 attempts at displaced stepping if we get a memory error. */
1924 static displaced_step_prepare_status
1925 displaced_step_prepare (thread_info
*thread
)
1927 displaced_step_prepare_status status
1928 = DISPLACED_STEP_PREPARE_STATUS_CANT
;
1932 status
= displaced_step_prepare_throw (thread
);
1934 catch (const gdb_exception_error
&ex
)
1936 if (ex
.error
!= MEMORY_ERROR
1937 && ex
.error
!= NOT_SUPPORTED_ERROR
)
1940 infrun_debug_printf ("caught exception, disabling displaced stepping: %s",
1943 /* Be verbose if "set displaced-stepping" is "on", silent if
1945 if (can_use_displaced_stepping
== AUTO_BOOLEAN_TRUE
)
1947 warning (_("disabling displaced stepping: %s"),
1951 /* Disable further displaced stepping attempts. */
1952 thread
->inf
->displaced_step_state
.failed_before
= 1;
1958 /* If we displaced stepped an instruction successfully, adjust registers and
1959 memory to yield the same effect the instruction would have had if we had
1960 executed it at its original address, and return
1961 DISPLACED_STEP_FINISH_STATUS_OK. If the instruction didn't complete,
1962 relocate the PC and return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED.
1964 If the thread wasn't displaced stepping, return
1965 DISPLACED_STEP_FINISH_STATUS_OK as well. */
1967 static displaced_step_finish_status
1968 displaced_step_finish (thread_info
*event_thread
,
1969 const target_waitstatus
&event_status
)
1971 /* Check whether the parent is displaced stepping. */
1972 struct regcache
*regcache
= get_thread_regcache (event_thread
);
1973 struct gdbarch
*gdbarch
= regcache
->arch ();
1974 inferior
*parent_inf
= event_thread
->inf
;
1976 /* If this was a fork/vfork/clone, this event indicates that the
1977 displaced stepping of the syscall instruction has been done, so
1978 we perform cleanup for parent here. Also note that this
1979 operation also cleans up the child for vfork, because their pages
1982 /* If this is a fork (child gets its own address space copy) and
1983 some displaced step buffers were in use at the time of the fork,
1984 restore the displaced step buffer bytes in the child process.
1986 Architectures which support displaced stepping and fork events
1987 must supply an implementation of
1988 gdbarch_displaced_step_restore_all_in_ptid. This is not enforced
1989 during gdbarch validation to support architectures which support
1990 displaced stepping but not forks. */
1991 if (event_status
.kind () == TARGET_WAITKIND_FORKED
1992 && gdbarch_supports_displaced_stepping (gdbarch
))
1993 gdbarch_displaced_step_restore_all_in_ptid
1994 (gdbarch
, parent_inf
, event_status
.child_ptid ());
1996 displaced_step_thread_state
*displaced
= &event_thread
->displaced_step_state
;
1998 /* Was this thread performing a displaced step? */
1999 if (!displaced
->in_progress ())
2000 return DISPLACED_STEP_FINISH_STATUS_OK
;
2002 gdb_assert (event_thread
->inf
->displaced_step_state
.in_progress_count
> 0);
2003 event_thread
->inf
->displaced_step_state
.in_progress_count
--;
2005 /* Fixup may need to read memory/registers. Switch to the thread
2006 that we're fixing up. Also, target_stopped_by_watchpoint checks
2007 the current thread, and displaced_step_restore performs ptid-dependent
2008 memory accesses using current_inferior(). */
2009 switch_to_thread (event_thread
);
2011 displaced_step_reset_cleanup
cleanup (displaced
);
2013 /* Do the fixup, and release the resources acquired to do the displaced
2015 displaced_step_finish_status status
2016 = gdbarch_displaced_step_finish (displaced
->get_original_gdbarch (),
2017 event_thread
, event_status
);
2019 if (event_status
.kind () == TARGET_WAITKIND_FORKED
2020 || event_status
.kind () == TARGET_WAITKIND_VFORKED
2021 || event_status
.kind () == TARGET_WAITKIND_THREAD_CLONED
)
2023 /* Since the vfork/fork/clone syscall instruction was executed
2024 in the scratchpad, the child's PC is also within the
2025 scratchpad. Set the child's PC to the parent's PC value,
2026 which has already been fixed up. Note: we use the parent's
2027 aspace here, although we're touching the child, because the
2028 child hasn't been added to the inferior list yet at this
2031 struct regcache
*child_regcache
2032 = get_thread_arch_aspace_regcache (parent_inf
,
2033 event_status
.child_ptid (),
2035 parent_inf
->aspace
);
2036 /* Read PC value of parent. */
2037 CORE_ADDR parent_pc
= regcache_read_pc (regcache
);
2039 displaced_debug_printf ("write child pc from %s to %s",
2041 regcache_read_pc (child_regcache
)),
2042 paddress (gdbarch
, parent_pc
));
2044 regcache_write_pc (child_regcache
, parent_pc
);
2050 /* Data to be passed around while handling an event. This data is
2051 discarded between events. */
2052 struct execution_control_state
2054 explicit execution_control_state (thread_info
*thr
= nullptr)
2055 : ptid (thr
== nullptr ? null_ptid
: thr
->ptid
),
2060 process_stratum_target
*target
= nullptr;
2062 /* The thread that got the event, if this was a thread event; NULL
2064 struct thread_info
*event_thread
;
2066 struct target_waitstatus ws
;
2067 int stop_func_filled_in
= 0;
2068 CORE_ADDR stop_func_alt_start
= 0;
2069 CORE_ADDR stop_func_start
= 0;
2070 CORE_ADDR stop_func_end
= 0;
2071 const char *stop_func_name
= nullptr;
2072 int wait_some_more
= 0;
2074 /* True if the event thread hit the single-step breakpoint of
2075 another thread. Thus the event doesn't cause a stop, the thread
2076 needs to be single-stepped past the single-step breakpoint before
2077 we can switch back to the original stepping thread. */
2078 int hit_singlestep_breakpoint
= 0;
2081 static void keep_going_pass_signal (struct execution_control_state
*ecs
);
2082 static void prepare_to_wait (struct execution_control_state
*ecs
);
2083 static bool keep_going_stepped_thread (struct thread_info
*tp
);
2084 static step_over_what
thread_still_needs_step_over (struct thread_info
*tp
);
2086 /* Are there any pending step-over requests? If so, run all we can
2087 now and return true. Otherwise, return false. */
2090 start_step_over (void)
2092 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
2094 /* Don't start a new step-over if we already have an in-line
2095 step-over operation ongoing. */
2096 if (step_over_info_valid_p ())
2099 /* Steal the global thread step over chain. As we try to initiate displaced
2100 steps, threads will be enqueued in the global chain if no buffers are
2101 available. If we iterated on the global chain directly, we might iterate
2103 thread_step_over_list threads_to_step
2104 = std::move (global_thread_step_over_list
);
2106 infrun_debug_printf ("stealing global queue of threads to step, length = %d",
2107 thread_step_over_chain_length (threads_to_step
));
2109 bool started
= false;
2111 /* On scope exit (whatever the reason, return or exception), if there are
2112 threads left in the THREADS_TO_STEP chain, put back these threads in the
2116 if (threads_to_step
.empty ())
2117 infrun_debug_printf ("step-over queue now empty");
2120 infrun_debug_printf ("putting back %d threads to step in global queue",
2121 thread_step_over_chain_length (threads_to_step
));
2123 global_thread_step_over_chain_enqueue_chain
2124 (std::move (threads_to_step
));
2128 thread_step_over_list_safe_range range
2129 = make_thread_step_over_list_safe_range (threads_to_step
);
2131 for (thread_info
*tp
: range
)
2133 step_over_what step_what
;
2134 int must_be_in_line
;
2136 gdb_assert (!tp
->stop_requested
);
2138 if (tp
->inf
->displaced_step_state
.unavailable
)
2140 /* The arch told us to not even try preparing another displaced step
2141 for this inferior. Just leave the thread in THREADS_TO_STEP, it
2142 will get moved to the global chain on scope exit. */
2146 if (tp
->inf
->thread_waiting_for_vfork_done
!= nullptr)
2148 /* When we stop all threads, handling a vfork, any thread in the step
2149 over chain remains there. A user could also try to continue a
2150 thread stopped at a breakpoint while another thread is waiting for
2151 a vfork-done event. In any case, we don't want to start a step
2156 /* Remove thread from the THREADS_TO_STEP chain. If anything goes wrong
2157 while we try to prepare the displaced step, we don't add it back to
2158 the global step over chain. This is to avoid a thread staying in the
2159 step over chain indefinitely if something goes wrong when resuming it
2160 If the error is intermittent and it still needs a step over, it will
2161 get enqueued again when we try to resume it normally. */
2162 threads_to_step
.erase (threads_to_step
.iterator_to (*tp
));
2164 step_what
= thread_still_needs_step_over (tp
);
2165 must_be_in_line
= ((step_what
& STEP_OVER_WATCHPOINT
)
2166 || ((step_what
& STEP_OVER_BREAKPOINT
)
2167 && !use_displaced_stepping (tp
)));
2169 /* We currently stop all threads of all processes to step-over
2170 in-line. If we need to start a new in-line step-over, let
2171 any pending displaced steps finish first. */
2172 if (must_be_in_line
&& displaced_step_in_progress_any_thread ())
2174 global_thread_step_over_chain_enqueue (tp
);
2178 if (tp
->control
.trap_expected
2180 || tp
->executing ())
2182 internal_error ("[%s] has inconsistent state: "
2183 "trap_expected=%d, resumed=%d, executing=%d\n",
2184 tp
->ptid
.to_string ().c_str (),
2185 tp
->control
.trap_expected
,
2190 infrun_debug_printf ("resuming [%s] for step-over",
2191 tp
->ptid
.to_string ().c_str ());
2193 /* keep_going_pass_signal skips the step-over if the breakpoint
2194 is no longer inserted. In all-stop, we want to keep looking
2195 for a thread that needs a step-over instead of resuming TP,
2196 because we wouldn't be able to resume anything else until the
2197 target stops again. In non-stop, the resume always resumes
2198 only TP, so it's OK to let the thread resume freely. */
2199 if (!target_is_non_stop_p () && !step_what
)
2202 switch_to_thread (tp
);
2203 execution_control_state
ecs (tp
);
2204 keep_going_pass_signal (&ecs
);
2206 if (!ecs
.wait_some_more
)
2207 error (_("Command aborted."));
2209 /* If the thread's step over could not be initiated because no buffers
2210 were available, it was re-added to the global step over chain. */
2213 infrun_debug_printf ("[%s] was resumed.",
2214 tp
->ptid
.to_string ().c_str ());
2215 gdb_assert (!thread_is_in_step_over_chain (tp
));
2219 infrun_debug_printf ("[%s] was NOT resumed.",
2220 tp
->ptid
.to_string ().c_str ());
2221 gdb_assert (thread_is_in_step_over_chain (tp
));
2224 /* If we started a new in-line step-over, we're done. */
2225 if (step_over_info_valid_p ())
2227 gdb_assert (tp
->control
.trap_expected
);
2232 if (!target_is_non_stop_p ())
2234 /* On all-stop, shouldn't have resumed unless we needed a
2236 gdb_assert (tp
->control
.trap_expected
2237 || tp
->step_after_step_resume_breakpoint
);
2239 /* With remote targets (at least), in all-stop, we can't
2240 issue any further remote commands until the program stops
2246 /* Either the thread no longer needed a step-over, or a new
2247 displaced stepping sequence started. Even in the latter
2248 case, continue looking. Maybe we can also start another
2249 displaced step on a thread of other process. */
2255 /* Update global variables holding ptids to hold NEW_PTID if they were
2256 holding OLD_PTID. */
2258 infrun_thread_ptid_changed (process_stratum_target
*target
,
2259 ptid_t old_ptid
, ptid_t new_ptid
)
2261 if (inferior_ptid
== old_ptid
2262 && current_inferior ()->process_target () == target
)
2263 inferior_ptid
= new_ptid
;
2268 static const char schedlock_off
[] = "off";
2269 static const char schedlock_on
[] = "on";
2270 static const char schedlock_step
[] = "step";
2271 static const char schedlock_replay
[] = "replay";
2272 static const char *const scheduler_enums
[] = {
2279 static const char *scheduler_mode
= schedlock_replay
;
2281 show_scheduler_mode (struct ui_file
*file
, int from_tty
,
2282 struct cmd_list_element
*c
, const char *value
)
2285 _("Mode for locking scheduler "
2286 "during execution is \"%s\".\n"),
2291 set_schedlock_func (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2293 if (!target_can_lock_scheduler ())
2295 scheduler_mode
= schedlock_off
;
2296 error (_("Target '%s' cannot support this command."),
2297 target_shortname ());
2301 /* True if execution commands resume all threads of all processes by
2302 default; otherwise, resume only threads of the current inferior
2304 bool sched_multi
= false;
2306 /* Try to setup for software single stepping. Return true if target_resume()
2307 should use hardware single step.
2309 GDBARCH the current gdbarch. */
2312 maybe_software_singlestep (struct gdbarch
*gdbarch
)
2314 bool hw_step
= true;
2316 if (execution_direction
== EXEC_FORWARD
2317 && gdbarch_software_single_step_p (gdbarch
))
2318 hw_step
= !insert_single_step_breakpoints (gdbarch
);
2326 user_visible_resume_ptid (int step
)
2332 /* With non-stop mode on, threads are always handled
2334 resume_ptid
= inferior_ptid
;
2336 else if ((scheduler_mode
== schedlock_on
)
2337 || (scheduler_mode
== schedlock_step
&& step
))
2339 /* User-settable 'scheduler' mode requires solo thread
2341 resume_ptid
= inferior_ptid
;
2343 else if ((scheduler_mode
== schedlock_replay
)
2344 && target_record_will_replay (minus_one_ptid
, execution_direction
))
2346 /* User-settable 'scheduler' mode requires solo thread resume in replay
2348 resume_ptid
= inferior_ptid
;
2350 else if (!sched_multi
&& target_supports_multi_process ())
2352 /* Resume all threads of the current process (and none of other
2354 resume_ptid
= ptid_t (inferior_ptid
.pid ());
2358 /* Resume all threads of all processes. */
2359 resume_ptid
= RESUME_ALL
;
2367 process_stratum_target
*
2368 user_visible_resume_target (ptid_t resume_ptid
)
2370 return (resume_ptid
== minus_one_ptid
&& sched_multi
2372 : current_inferior ()->process_target ());
2375 /* Find a thread from the inferiors that we'll resume that is waiting
2376 for a vfork-done event. */
2378 static thread_info
*
2379 find_thread_waiting_for_vfork_done ()
2381 gdb_assert (!target_is_non_stop_p ());
2385 for (inferior
*inf
: all_non_exited_inferiors ())
2386 if (inf
->thread_waiting_for_vfork_done
!= nullptr)
2387 return inf
->thread_waiting_for_vfork_done
;
2391 inferior
*cur_inf
= current_inferior ();
2392 if (cur_inf
->thread_waiting_for_vfork_done
!= nullptr)
2393 return cur_inf
->thread_waiting_for_vfork_done
;
2398 /* Return a ptid representing the set of threads that we will resume,
2399 in the perspective of the target, assuming run control handling
2400 does not require leaving some threads stopped (e.g., stepping past
2401 breakpoint). USER_STEP indicates whether we're about to start the
2402 target for a stepping command. */
2405 internal_resume_ptid (int user_step
)
2407 /* In non-stop, we always control threads individually. Note that
2408 the target may always work in non-stop mode even with "set
2409 non-stop off", in which case user_visible_resume_ptid could
2410 return a wildcard ptid. */
2411 if (target_is_non_stop_p ())
2412 return inferior_ptid
;
2414 /* The rest of the function assumes non-stop==off and
2415 target-non-stop==off.
2417 If a thread is waiting for a vfork-done event, it means breakpoints are out
2418 for this inferior (well, program space in fact). We don't want to resume
2419 any thread other than the one waiting for vfork done, otherwise these other
2420 threads could miss breakpoints. So if a thread in the resumption set is
2421 waiting for a vfork-done event, resume only that thread.
2423 The resumption set width depends on whether schedule-multiple is on or off.
2425 Note that if the target_resume interface was more flexible, we could be
2426 smarter here when schedule-multiple is on. For example, imagine 3
2427 inferiors with 2 threads each (1.1, 1.2, 2.1, 2.2, 3.1 and 3.2). Threads
2428 2.1 and 3.2 are both waiting for a vfork-done event. Then we could ask the
2429 target(s) to resume:
2431 - All threads of inferior 1
2435 Since we don't have that flexibility (we can only pass one ptid), just
2436 resume the first thread waiting for a vfork-done event we find (e.g. thread
2438 thread_info
*thr
= find_thread_waiting_for_vfork_done ();
2441 /* If we have a thread that is waiting for a vfork-done event,
2442 then we should have switched to it earlier. Calling
2443 target_resume with thread scope is only possible when the
2444 current thread matches the thread scope. */
2445 gdb_assert (thr
->ptid
== inferior_ptid
);
2446 gdb_assert (thr
->inf
->process_target ()
2447 == inferior_thread ()->inf
->process_target ());
2451 return user_visible_resume_ptid (user_step
);
2454 /* Wrapper for target_resume, that handles infrun-specific
2458 do_target_resume (ptid_t resume_ptid
, bool step
, enum gdb_signal sig
)
2460 struct thread_info
*tp
= inferior_thread ();
2462 gdb_assert (!tp
->stop_requested
);
2464 /* Install inferior's terminal modes. */
2465 target_terminal::inferior ();
2467 /* Avoid confusing the next resume, if the next stop/resume
2468 happens to apply to another thread. */
2469 tp
->set_stop_signal (GDB_SIGNAL_0
);
2471 /* Advise target which signals may be handled silently.
2473 If we have removed breakpoints because we are stepping over one
2474 in-line (in any thread), we need to receive all signals to avoid
2475 accidentally skipping a breakpoint during execution of a signal
2478 Likewise if we're displaced stepping, otherwise a trap for a
2479 breakpoint in a signal handler might be confused with the
2480 displaced step finishing. We don't make the displaced_step_finish
2481 step distinguish the cases instead, because:
2483 - a backtrace while stopped in the signal handler would show the
2484 scratch pad as frame older than the signal handler, instead of
2485 the real mainline code.
2487 - when the thread is later resumed, the signal handler would
2488 return to the scratch pad area, which would no longer be
2490 if (step_over_info_valid_p ()
2491 || displaced_step_in_progress (tp
->inf
))
2492 target_pass_signals ({});
2494 target_pass_signals (signal_pass
);
2496 infrun_debug_printf ("resume_ptid=%s, step=%d, sig=%s",
2497 resume_ptid
.to_string ().c_str (),
2498 step
, gdb_signal_to_symbol_string (sig
));
2500 target_resume (resume_ptid
, step
, sig
);
2503 /* Resume the inferior. SIG is the signal to give the inferior
2504 (GDB_SIGNAL_0 for none). Note: don't call this directly; instead
2505 call 'resume', which handles exceptions. */
2508 resume_1 (enum gdb_signal sig
)
2510 struct regcache
*regcache
= get_current_regcache ();
2511 struct gdbarch
*gdbarch
= regcache
->arch ();
2512 struct thread_info
*tp
= inferior_thread ();
2513 const address_space
*aspace
= regcache
->aspace ();
2515 /* This represents the user's step vs continue request. When
2516 deciding whether "set scheduler-locking step" applies, it's the
2517 user's intention that counts. */
2518 const int user_step
= tp
->control
.stepping_command
;
2519 /* This represents what we'll actually request the target to do.
2520 This can decay from a step to a continue, if e.g., we need to
2521 implement single-stepping with breakpoints (software
2525 gdb_assert (!tp
->stop_requested
);
2526 gdb_assert (!thread_is_in_step_over_chain (tp
));
2528 if (tp
->has_pending_waitstatus ())
2531 ("thread %s has pending wait "
2532 "status %s (currently_stepping=%d).",
2533 tp
->ptid
.to_string ().c_str (),
2534 tp
->pending_waitstatus ().to_string ().c_str (),
2535 currently_stepping (tp
));
2537 tp
->inf
->process_target ()->threads_executing
= true;
2538 tp
->set_resumed (true);
2540 /* FIXME: What should we do if we are supposed to resume this
2541 thread with a signal? Maybe we should maintain a queue of
2542 pending signals to deliver. */
2543 if (sig
!= GDB_SIGNAL_0
)
2545 warning (_("Couldn't deliver signal %s to %s."),
2546 gdb_signal_to_name (sig
),
2547 tp
->ptid
.to_string ().c_str ());
2550 tp
->set_stop_signal (GDB_SIGNAL_0
);
2552 if (target_can_async_p ())
2554 target_async (true);
2555 /* Tell the event loop we have an event to process. */
2556 mark_async_event_handler (infrun_async_inferior_event_token
);
2561 tp
->stepped_breakpoint
= 0;
2563 /* Depends on stepped_breakpoint. */
2564 step
= currently_stepping (tp
);
2566 if (current_inferior ()->thread_waiting_for_vfork_done
!= nullptr)
2568 /* Don't try to single-step a vfork parent that is waiting for
2569 the child to get out of the shared memory region (by exec'ing
2570 or exiting). This is particularly important on software
2571 single-step archs, as the child process would trip on the
2572 software single step breakpoint inserted for the parent
2573 process. Since the parent will not actually execute any
2574 instruction until the child is out of the shared region (such
2575 are vfork's semantics), it is safe to simply continue it.
2576 Eventually, we'll see a TARGET_WAITKIND_VFORK_DONE event for
2577 the parent, and tell it to `keep_going', which automatically
2578 re-sets it stepping. */
2579 infrun_debug_printf ("resume : clear step");
2583 CORE_ADDR pc
= regcache_read_pc (regcache
);
2585 infrun_debug_printf ("step=%d, signal=%s, trap_expected=%d, "
2586 "current thread [%s] at %s",
2587 step
, gdb_signal_to_symbol_string (sig
),
2588 tp
->control
.trap_expected
,
2589 inferior_ptid
.to_string ().c_str (),
2590 paddress (gdbarch
, pc
));
2592 /* Normally, by the time we reach `resume', the breakpoints are either
2593 removed or inserted, as appropriate. The exception is if we're sitting
2594 at a permanent breakpoint; we need to step over it, but permanent
2595 breakpoints can't be removed. So we have to test for it here. */
2596 if (breakpoint_here_p (aspace
, pc
) == permanent_breakpoint_here
)
2598 if (sig
!= GDB_SIGNAL_0
)
2600 /* We have a signal to pass to the inferior. The resume
2601 may, or may not take us to the signal handler. If this
2602 is a step, we'll need to stop in the signal handler, if
2603 there's one, (if the target supports stepping into
2604 handlers), or in the next mainline instruction, if
2605 there's no handler. If this is a continue, we need to be
2606 sure to run the handler with all breakpoints inserted.
2607 In all cases, set a breakpoint at the current address
2608 (where the handler returns to), and once that breakpoint
2609 is hit, resume skipping the permanent breakpoint. If
2610 that breakpoint isn't hit, then we've stepped into the
2611 signal handler (or hit some other event). We'll delete
2612 the step-resume breakpoint then. */
2614 infrun_debug_printf ("resume: skipping permanent breakpoint, "
2615 "deliver signal first");
2617 clear_step_over_info ();
2618 tp
->control
.trap_expected
= 0;
2620 if (tp
->control
.step_resume_breakpoint
== nullptr)
2622 /* Set a "high-priority" step-resume, as we don't want
2623 user breakpoints at PC to trigger (again) when this
2625 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2626 gdb_assert (tp
->control
.step_resume_breakpoint
->first_loc ()
2629 tp
->step_after_step_resume_breakpoint
= step
;
2632 insert_breakpoints ();
2636 /* There's no signal to pass, we can go ahead and skip the
2637 permanent breakpoint manually. */
2638 infrun_debug_printf ("skipping permanent breakpoint");
2639 gdbarch_skip_permanent_breakpoint (gdbarch
, regcache
);
2640 /* Update pc to reflect the new address from which we will
2641 execute instructions. */
2642 pc
= regcache_read_pc (regcache
);
2646 /* We've already advanced the PC, so the stepping part
2647 is done. Now we need to arrange for a trap to be
2648 reported to handle_inferior_event. Set a breakpoint
2649 at the current PC, and run to it. Don't update
2650 prev_pc, because if we end in
2651 switch_back_to_stepped_thread, we want the "expected
2652 thread advanced also" branch to be taken. IOW, we
2653 don't want this thread to step further from PC
2655 gdb_assert (!step_over_info_valid_p ());
2656 insert_single_step_breakpoint (gdbarch
, aspace
, pc
);
2657 insert_breakpoints ();
2659 resume_ptid
= internal_resume_ptid (user_step
);
2660 do_target_resume (resume_ptid
, false, GDB_SIGNAL_0
);
2661 tp
->set_resumed (true);
2667 /* If we have a breakpoint to step over, make sure to do a single
2668 step only. Same if we have software watchpoints. */
2669 if (tp
->control
.trap_expected
|| bpstat_should_step ())
2670 tp
->control
.may_range_step
= 0;
2672 /* If displaced stepping is enabled, step over breakpoints by executing a
2673 copy of the instruction at a different address.
2675 We can't use displaced stepping when we have a signal to deliver;
2676 the comments for displaced_step_prepare explain why. The
2677 comments in the handle_inferior event for dealing with 'random
2678 signals' explain what we do instead.
2680 We can't use displaced stepping when we are waiting for vfork_done
2681 event, displaced stepping breaks the vfork child similarly as single
2682 step software breakpoint. */
2683 if (tp
->control
.trap_expected
2684 && use_displaced_stepping (tp
)
2685 && !step_over_info_valid_p ()
2686 && sig
== GDB_SIGNAL_0
2687 && current_inferior ()->thread_waiting_for_vfork_done
== nullptr)
2689 displaced_step_prepare_status prepare_status
2690 = displaced_step_prepare (tp
);
2692 if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
)
2694 infrun_debug_printf ("Got placed in step-over queue");
2696 tp
->control
.trap_expected
= 0;
2699 else if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_CANT
)
2701 /* Fallback to stepping over the breakpoint in-line. */
2703 if (target_is_non_stop_p ())
2704 stop_all_threads ("displaced stepping falling back on inline stepping");
2706 set_step_over_info (regcache
->aspace (),
2707 regcache_read_pc (regcache
), 0, tp
->global_num
);
2709 step
= maybe_software_singlestep (gdbarch
);
2711 insert_breakpoints ();
2713 else if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_OK
)
2715 /* Update pc to reflect the new address from which we will
2716 execute instructions due to displaced stepping. */
2717 pc
= regcache_read_pc (get_thread_regcache (tp
));
2719 step
= gdbarch_displaced_step_hw_singlestep (gdbarch
);
2722 gdb_assert_not_reached ("Invalid displaced_step_prepare_status "
2726 /* Do we need to do it the hard way, w/temp breakpoints? */
2728 step
= maybe_software_singlestep (gdbarch
);
2730 /* Currently, our software single-step implementation leads to different
2731 results than hardware single-stepping in one situation: when stepping
2732 into delivering a signal which has an associated signal handler,
2733 hardware single-step will stop at the first instruction of the handler,
2734 while software single-step will simply skip execution of the handler.
2736 For now, this difference in behavior is accepted since there is no
2737 easy way to actually implement single-stepping into a signal handler
2738 without kernel support.
2740 However, there is one scenario where this difference leads to follow-on
2741 problems: if we're stepping off a breakpoint by removing all breakpoints
2742 and then single-stepping. In this case, the software single-step
2743 behavior means that even if there is a *breakpoint* in the signal
2744 handler, GDB still would not stop.
2746 Fortunately, we can at least fix this particular issue. We detect
2747 here the case where we are about to deliver a signal while software
2748 single-stepping with breakpoints removed. In this situation, we
2749 revert the decisions to remove all breakpoints and insert single-
2750 step breakpoints, and instead we install a step-resume breakpoint
2751 at the current address, deliver the signal without stepping, and
2752 once we arrive back at the step-resume breakpoint, actually step
2753 over the breakpoint we originally wanted to step over. */
2754 if (thread_has_single_step_breakpoints_set (tp
)
2755 && sig
!= GDB_SIGNAL_0
2756 && step_over_info_valid_p ())
2758 /* If we have nested signals or a pending signal is delivered
2759 immediately after a handler returns, might already have
2760 a step-resume breakpoint set on the earlier handler. We cannot
2761 set another step-resume breakpoint; just continue on until the
2762 original breakpoint is hit. */
2763 if (tp
->control
.step_resume_breakpoint
== nullptr)
2765 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2766 tp
->step_after_step_resume_breakpoint
= 1;
2769 delete_single_step_breakpoints (tp
);
2771 clear_step_over_info ();
2772 tp
->control
.trap_expected
= 0;
2774 insert_breakpoints ();
2777 /* If STEP is set, it's a request to use hardware stepping
2778 facilities. But in that case, we should never
2779 use singlestep breakpoint. */
2780 gdb_assert (!(thread_has_single_step_breakpoints_set (tp
) && step
));
2782 /* Decide the set of threads to ask the target to resume. */
2783 if (tp
->control
.trap_expected
)
2785 /* We're allowing a thread to run past a breakpoint it has
2786 hit, either by single-stepping the thread with the breakpoint
2787 removed, or by displaced stepping, with the breakpoint inserted.
2788 In the former case, we need to single-step only this thread,
2789 and keep others stopped, as they can miss this breakpoint if
2790 allowed to run. That's not really a problem for displaced
2791 stepping, but, we still keep other threads stopped, in case
2792 another thread is also stopped for a breakpoint waiting for
2793 its turn in the displaced stepping queue. */
2794 resume_ptid
= inferior_ptid
;
2797 resume_ptid
= internal_resume_ptid (user_step
);
2799 if (execution_direction
!= EXEC_REVERSE
2800 && step
&& breakpoint_inserted_here_p (aspace
, pc
))
2802 /* There are two cases where we currently need to step a
2803 breakpoint instruction when we have a signal to deliver:
2805 - See handle_signal_stop where we handle random signals that
2806 could take out us out of the stepping range. Normally, in
2807 that case we end up continuing (instead of stepping) over the
2808 signal handler with a breakpoint at PC, but there are cases
2809 where we should _always_ single-step, even if we have a
2810 step-resume breakpoint, like when a software watchpoint is
2811 set. Assuming single-stepping and delivering a signal at the
2812 same time would takes us to the signal handler, then we could
2813 have removed the breakpoint at PC to step over it. However,
2814 some hardware step targets (like e.g., Mac OS) can't step
2815 into signal handlers, and for those, we need to leave the
2816 breakpoint at PC inserted, as otherwise if the handler
2817 recurses and executes PC again, it'll miss the breakpoint.
2818 So we leave the breakpoint inserted anyway, but we need to
2819 record that we tried to step a breakpoint instruction, so
2820 that adjust_pc_after_break doesn't end up confused.
2822 - In non-stop if we insert a breakpoint (e.g., a step-resume)
2823 in one thread after another thread that was stepping had been
2824 momentarily paused for a step-over. When we re-resume the
2825 stepping thread, it may be resumed from that address with a
2826 breakpoint that hasn't trapped yet. Seen with
2827 gdb.threads/non-stop-fair-events.exp, on targets that don't
2828 do displaced stepping. */
2830 infrun_debug_printf ("resume: [%s] stepped breakpoint",
2831 tp
->ptid
.to_string ().c_str ());
2833 tp
->stepped_breakpoint
= 1;
2835 /* Most targets can step a breakpoint instruction, thus
2836 executing it normally. But if this one cannot, just
2837 continue and we will hit it anyway. */
2838 if (gdbarch_cannot_step_breakpoint (gdbarch
))
2842 if (tp
->control
.may_range_step
)
2844 /* If we're resuming a thread with the PC out of the step
2845 range, then we're doing some nested/finer run control
2846 operation, like stepping the thread out of the dynamic
2847 linker or the displaced stepping scratch pad. We
2848 shouldn't have allowed a range step then. */
2849 gdb_assert (pc_in_thread_step_range (pc
, tp
));
2852 do_target_resume (resume_ptid
, step
, sig
);
2853 tp
->set_resumed (true);
2856 /* Resume the inferior. SIG is the signal to give the inferior
2857 (GDB_SIGNAL_0 for none). This is a wrapper around 'resume_1' that
2858 rolls back state on error. */
2861 resume (gdb_signal sig
)
2867 catch (const gdb_exception
&ex
)
2869 /* If resuming is being aborted for any reason, delete any
2870 single-step breakpoint resume_1 may have created, to avoid
2871 confusing the following resumption, and to avoid leaving
2872 single-step breakpoints perturbing other threads, in case
2873 we're running in non-stop mode. */
2874 if (inferior_ptid
!= null_ptid
)
2875 delete_single_step_breakpoints (inferior_thread ());
2885 /* Counter that tracks number of user visible stops. This can be used
2886 to tell whether a command has proceeded the inferior past the
2887 current location. This allows e.g., inferior function calls in
2888 breakpoint commands to not interrupt the command list. When the
2889 call finishes successfully, the inferior is standing at the same
2890 breakpoint as if nothing happened (and so we don't call
2892 static ULONGEST current_stop_id
;
2899 return current_stop_id
;
2902 /* Called when we report a user visible stop. */
2910 /* Clear out all variables saying what to do when inferior is continued.
2911 First do this, then set the ones you want, then call `proceed'. */
2914 clear_proceed_status_thread (struct thread_info
*tp
)
2916 infrun_debug_printf ("%s", tp
->ptid
.to_string ().c_str ());
2918 /* If we're starting a new sequence, then the previous finished
2919 single-step is no longer relevant. */
2920 if (tp
->has_pending_waitstatus ())
2922 if (tp
->stop_reason () == TARGET_STOPPED_BY_SINGLE_STEP
)
2924 infrun_debug_printf ("pending event of %s was a finished step. "
2926 tp
->ptid
.to_string ().c_str ());
2928 tp
->clear_pending_waitstatus ();
2929 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
2934 ("thread %s has pending wait status %s (currently_stepping=%d).",
2935 tp
->ptid
.to_string ().c_str (),
2936 tp
->pending_waitstatus ().to_string ().c_str (),
2937 currently_stepping (tp
));
2941 /* If this signal should not be seen by program, give it zero.
2942 Used for debugging signals. */
2943 if (!signal_pass_state (tp
->stop_signal ()))
2944 tp
->set_stop_signal (GDB_SIGNAL_0
);
2946 tp
->release_thread_fsm ();
2948 tp
->control
.trap_expected
= 0;
2949 tp
->control
.step_range_start
= 0;
2950 tp
->control
.step_range_end
= 0;
2951 tp
->control
.may_range_step
= 0;
2952 tp
->control
.step_frame_id
= null_frame_id
;
2953 tp
->control
.step_stack_frame_id
= null_frame_id
;
2954 tp
->control
.step_over_calls
= STEP_OVER_UNDEBUGGABLE
;
2955 tp
->control
.step_start_function
= nullptr;
2956 tp
->stop_requested
= 0;
2958 tp
->control
.stop_step
= 0;
2960 tp
->control
.proceed_to_finish
= 0;
2962 tp
->control
.stepping_command
= 0;
2964 /* Discard any remaining commands or status from previous stop. */
2965 bpstat_clear (&tp
->control
.stop_bpstat
);
2968 /* Notify the current interpreter and observers that the target is about to
2972 notify_about_to_proceed ()
2974 top_level_interpreter ()->on_about_to_proceed ();
2975 gdb::observers::about_to_proceed
.notify ();
2979 clear_proceed_status (int step
)
2981 /* With scheduler-locking replay, stop replaying other threads if we're
2982 not replaying the user-visible resume ptid.
2984 This is a convenience feature to not require the user to explicitly
2985 stop replaying the other threads. We're assuming that the user's
2986 intent is to resume tracing the recorded process. */
2987 if (!non_stop
&& scheduler_mode
== schedlock_replay
2988 && target_record_is_replaying (minus_one_ptid
)
2989 && !target_record_will_replay (user_visible_resume_ptid (step
),
2990 execution_direction
))
2991 target_record_stop_replaying ();
2993 if (!non_stop
&& inferior_ptid
!= null_ptid
)
2995 ptid_t resume_ptid
= user_visible_resume_ptid (step
);
2996 process_stratum_target
*resume_target
2997 = user_visible_resume_target (resume_ptid
);
2999 /* In all-stop mode, delete the per-thread status of all threads
3000 we're about to resume, implicitly and explicitly. */
3001 for (thread_info
*tp
: all_non_exited_threads (resume_target
, resume_ptid
))
3002 clear_proceed_status_thread (tp
);
3005 if (inferior_ptid
!= null_ptid
)
3007 struct inferior
*inferior
;
3011 /* If in non-stop mode, only delete the per-thread status of
3012 the current thread. */
3013 clear_proceed_status_thread (inferior_thread ());
3016 inferior
= current_inferior ();
3017 inferior
->control
.stop_soon
= NO_STOP_QUIETLY
;
3020 notify_about_to_proceed ();
3023 /* Returns true if TP is still stopped at a breakpoint that needs
3024 stepping-over in order to make progress. If the breakpoint is gone
3025 meanwhile, we can skip the whole step-over dance. */
3028 thread_still_needs_step_over_bp (struct thread_info
*tp
)
3030 if (tp
->stepping_over_breakpoint
)
3032 struct regcache
*regcache
= get_thread_regcache (tp
);
3034 if (breakpoint_here_p (regcache
->aspace (),
3035 regcache_read_pc (regcache
))
3036 == ordinary_breakpoint_here
)
3039 tp
->stepping_over_breakpoint
= 0;
3045 /* Check whether thread TP still needs to start a step-over in order
3046 to make progress when resumed. Returns an bitwise or of enum
3047 step_over_what bits, indicating what needs to be stepped over. */
3049 static step_over_what
3050 thread_still_needs_step_over (struct thread_info
*tp
)
3052 step_over_what what
= 0;
3054 if (thread_still_needs_step_over_bp (tp
))
3055 what
|= STEP_OVER_BREAKPOINT
;
3057 if (tp
->stepping_over_watchpoint
3058 && !target_have_steppable_watchpoint ())
3059 what
|= STEP_OVER_WATCHPOINT
;
3064 /* Returns true if scheduler locking applies. STEP indicates whether
3065 we're about to do a step/next-like command to a thread. */
3068 schedlock_applies (struct thread_info
*tp
)
3070 return (scheduler_mode
== schedlock_on
3071 || (scheduler_mode
== schedlock_step
3072 && tp
->control
.stepping_command
)
3073 || (scheduler_mode
== schedlock_replay
3074 && target_record_will_replay (minus_one_ptid
,
3075 execution_direction
)));
3078 /* Set process_stratum_target::COMMIT_RESUMED_STATE in all target
3079 stacks that have threads executing and don't have threads with
3083 maybe_set_commit_resumed_all_targets ()
3085 scoped_restore_current_thread restore_thread
;
3087 for (inferior
*inf
: all_non_exited_inferiors ())
3089 process_stratum_target
*proc_target
= inf
->process_target ();
3091 if (proc_target
->commit_resumed_state
)
3093 /* We already set this in a previous iteration, via another
3094 inferior sharing the process_stratum target. */
3098 /* If the target has no resumed threads, it would be useless to
3099 ask it to commit the resumed threads. */
3100 if (!proc_target
->threads_executing
)
3102 infrun_debug_printf ("not requesting commit-resumed for target "
3103 "%s, no resumed threads",
3104 proc_target
->shortname ());
3108 /* As an optimization, if a thread from this target has some
3109 status to report, handle it before requiring the target to
3110 commit its resumed threads: handling the status might lead to
3111 resuming more threads. */
3112 if (proc_target
->has_resumed_with_pending_wait_status ())
3114 infrun_debug_printf ("not requesting commit-resumed for target %s, a"
3115 " thread has a pending waitstatus",
3116 proc_target
->shortname ());
3120 switch_to_inferior_no_thread (inf
);
3122 if (target_has_pending_events ())
3124 infrun_debug_printf ("not requesting commit-resumed for target %s, "
3125 "target has pending events",
3126 proc_target
->shortname ());
3130 infrun_debug_printf ("enabling commit-resumed for target %s",
3131 proc_target
->shortname ());
3133 proc_target
->commit_resumed_state
= true;
3140 maybe_call_commit_resumed_all_targets ()
3142 scoped_restore_current_thread restore_thread
;
3144 for (inferior
*inf
: all_non_exited_inferiors ())
3146 process_stratum_target
*proc_target
= inf
->process_target ();
3148 if (!proc_target
->commit_resumed_state
)
3151 switch_to_inferior_no_thread (inf
);
3153 infrun_debug_printf ("calling commit_resumed for target %s",
3154 proc_target
->shortname());
3156 target_commit_resumed ();
3160 /* To track nesting of scoped_disable_commit_resumed objects, ensuring
3161 that only the outermost one attempts to re-enable
3163 static bool enable_commit_resumed
= true;
3167 scoped_disable_commit_resumed::scoped_disable_commit_resumed
3168 (const char *reason
)
3169 : m_reason (reason
),
3170 m_prev_enable_commit_resumed (enable_commit_resumed
)
3172 infrun_debug_printf ("reason=%s", m_reason
);
3174 enable_commit_resumed
= false;
3176 for (inferior
*inf
: all_non_exited_inferiors ())
3178 process_stratum_target
*proc_target
= inf
->process_target ();
3180 if (m_prev_enable_commit_resumed
)
3182 /* This is the outermost instance: force all
3183 COMMIT_RESUMED_STATE to false. */
3184 proc_target
->commit_resumed_state
= false;
3188 /* This is not the outermost instance, we expect
3189 COMMIT_RESUMED_STATE to have been cleared by the
3190 outermost instance. */
3191 gdb_assert (!proc_target
->commit_resumed_state
);
3199 scoped_disable_commit_resumed::reset ()
3205 infrun_debug_printf ("reason=%s", m_reason
);
3207 gdb_assert (!enable_commit_resumed
);
3209 enable_commit_resumed
= m_prev_enable_commit_resumed
;
3211 if (m_prev_enable_commit_resumed
)
3213 /* This is the outermost instance, re-enable
3214 COMMIT_RESUMED_STATE on the targets where it's possible. */
3215 maybe_set_commit_resumed_all_targets ();
3219 /* This is not the outermost instance, we expect
3220 COMMIT_RESUMED_STATE to still be false. */
3221 for (inferior
*inf
: all_non_exited_inferiors ())
3223 process_stratum_target
*proc_target
= inf
->process_target ();
3224 gdb_assert (!proc_target
->commit_resumed_state
);
3231 scoped_disable_commit_resumed::~scoped_disable_commit_resumed ()
3239 scoped_disable_commit_resumed::reset_and_commit ()
3242 maybe_call_commit_resumed_all_targets ();
3247 scoped_enable_commit_resumed::scoped_enable_commit_resumed
3248 (const char *reason
)
3249 : m_reason (reason
),
3250 m_prev_enable_commit_resumed (enable_commit_resumed
)
3252 infrun_debug_printf ("reason=%s", m_reason
);
3254 if (!enable_commit_resumed
)
3256 enable_commit_resumed
= true;
3258 /* Re-enable COMMIT_RESUMED_STATE on the targets where it's
3260 maybe_set_commit_resumed_all_targets ();
3262 maybe_call_commit_resumed_all_targets ();
3268 scoped_enable_commit_resumed::~scoped_enable_commit_resumed ()
3270 infrun_debug_printf ("reason=%s", m_reason
);
3272 gdb_assert (enable_commit_resumed
);
3274 enable_commit_resumed
= m_prev_enable_commit_resumed
;
3276 if (!enable_commit_resumed
)
3278 /* Force all COMMIT_RESUMED_STATE back to false. */
3279 for (inferior
*inf
: all_non_exited_inferiors ())
3281 process_stratum_target
*proc_target
= inf
->process_target ();
3282 proc_target
->commit_resumed_state
= false;
3287 /* Check that all the targets we're about to resume are in non-stop
3288 mode. Ideally, we'd only care whether all targets support
3289 target-async, but we're not there yet. E.g., stop_all_threads
3290 doesn't know how to handle all-stop targets. Also, the remote
3291 protocol in all-stop mode is synchronous, irrespective of
3292 target-async, which means that things like a breakpoint re-set
3293 triggered by one target would try to read memory from all targets
3297 check_multi_target_resumption (process_stratum_target
*resume_target
)
3299 if (!non_stop
&& resume_target
== nullptr)
3301 scoped_restore_current_thread restore_thread
;
3303 /* This is used to track whether we're resuming more than one
3305 process_stratum_target
*first_connection
= nullptr;
3307 /* The first inferior we see with a target that does not work in
3308 always-non-stop mode. */
3309 inferior
*first_not_non_stop
= nullptr;
3311 for (inferior
*inf
: all_non_exited_inferiors ())
3313 switch_to_inferior_no_thread (inf
);
3315 if (!target_has_execution ())
3318 process_stratum_target
*proc_target
3319 = current_inferior ()->process_target();
3321 if (!target_is_non_stop_p ())
3322 first_not_non_stop
= inf
;
3324 if (first_connection
== nullptr)
3325 first_connection
= proc_target
;
3326 else if (first_connection
!= proc_target
3327 && first_not_non_stop
!= nullptr)
3329 switch_to_inferior_no_thread (first_not_non_stop
);
3331 proc_target
= current_inferior ()->process_target();
3333 error (_("Connection %d (%s) does not support "
3334 "multi-target resumption."),
3335 proc_target
->connection_number
,
3336 make_target_connection_string (proc_target
).c_str ());
3342 /* Helper function for `proceed`. Check if thread TP is suitable for
3343 resuming, and, if it is, switch to the thread and call
3344 `keep_going_pass_signal`. If TP is not suitable for resuming then this
3345 function will just return without switching threads. */
3348 proceed_resume_thread_checked (thread_info
*tp
)
3350 if (!tp
->inf
->has_execution ())
3352 infrun_debug_printf ("[%s] target has no execution",
3353 tp
->ptid
.to_string ().c_str ());
3359 infrun_debug_printf ("[%s] resumed",
3360 tp
->ptid
.to_string ().c_str ());
3361 gdb_assert (tp
->executing () || tp
->has_pending_waitstatus ());
3365 if (thread_is_in_step_over_chain (tp
))
3367 infrun_debug_printf ("[%s] needs step-over",
3368 tp
->ptid
.to_string ().c_str ());
3372 /* When handling a vfork GDB removes all breakpoints from the program
3373 space in which the vfork is being handled. If we are following the
3374 parent then GDB will set the thread_waiting_for_vfork_done member of
3375 the parent inferior. In this case we should take care to only resume
3376 the vfork parent thread, the kernel will hold this thread suspended
3377 until the vfork child has exited or execd, at which point the parent
3378 will be resumed and a VFORK_DONE event sent to GDB. */
3379 if (tp
->inf
->thread_waiting_for_vfork_done
!= nullptr)
3381 if (target_is_non_stop_p ())
3383 /* For non-stop targets, regardless of whether GDB is using
3384 all-stop or non-stop mode, threads are controlled
3387 When a thread is handling a vfork, breakpoints are removed
3388 from the inferior (well, program space in fact), so it is
3389 critical that we don't try to resume any thread other than the
3391 if (tp
!= tp
->inf
->thread_waiting_for_vfork_done
)
3393 infrun_debug_printf ("[%s] thread %s of this inferior is "
3394 "waiting for vfork-done",
3395 tp
->ptid
.to_string ().c_str (),
3396 tp
->inf
->thread_waiting_for_vfork_done
3397 ->ptid
.to_string ().c_str ());
3403 /* For all-stop targets, when we attempt to resume the inferior,
3404 we will only resume the vfork parent thread, this is handled
3405 in internal_resume_ptid.
3407 Additionally, we will always be called with the vfork parent
3408 thread as the current thread (TP) thanks to follow_fork, as
3409 such the following assertion should hold.
3411 Beyond this there is nothing more that needs to be done
3413 gdb_assert (tp
== tp
->inf
->thread_waiting_for_vfork_done
);
3417 /* When handling a vfork GDB removes all breakpoints from the program
3418 space in which the vfork is being handled. If we are following the
3419 child then GDB will set vfork_child member of the vfork parent
3420 inferior. Once the child has either exited or execd then GDB will
3421 detach from the parent process. Until that point GDB should not
3422 resume any thread in the parent process. */
3423 if (tp
->inf
->vfork_child
!= nullptr)
3425 infrun_debug_printf ("[%s] thread is part of a vfork parent, child is %d",
3426 tp
->ptid
.to_string ().c_str (),
3427 tp
->inf
->vfork_child
->pid
);
3431 infrun_debug_printf ("resuming %s",
3432 tp
->ptid
.to_string ().c_str ());
3434 execution_control_state
ecs (tp
);
3435 switch_to_thread (tp
);
3436 keep_going_pass_signal (&ecs
);
3437 if (!ecs
.wait_some_more
)
3438 error (_("Command aborted."));
3441 /* Basic routine for continuing the program in various fashions.
3443 ADDR is the address to resume at, or -1 for resume where stopped.
3444 SIGGNAL is the signal to give it, or GDB_SIGNAL_0 for none,
3445 or GDB_SIGNAL_DEFAULT for act according to how it stopped.
3447 You should call clear_proceed_status before calling proceed. */
3450 proceed (CORE_ADDR addr
, enum gdb_signal siggnal
)
3452 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
3454 struct regcache
*regcache
;
3455 struct gdbarch
*gdbarch
;
3458 /* If we're stopped at a fork/vfork, switch to either the parent or child
3459 thread as defined by the "set follow-fork-mode" command, or, if both
3460 the parent and child are controlled by GDB, and schedule-multiple is
3461 on, follow the child. If none of the above apply then we just proceed
3462 resuming the current thread. */
3463 if (!follow_fork ())
3465 /* The target for some reason decided not to resume. */
3467 if (target_can_async_p ())
3468 inferior_event_handler (INF_EXEC_COMPLETE
);
3472 /* We'll update this if & when we switch to a new thread. */
3473 update_previous_thread ();
3475 regcache
= get_current_regcache ();
3476 gdbarch
= regcache
->arch ();
3477 const address_space
*aspace
= regcache
->aspace ();
3479 pc
= regcache_read_pc_protected (regcache
);
3481 thread_info
*cur_thr
= inferior_thread ();
3483 infrun_debug_printf ("cur_thr = %s", cur_thr
->ptid
.to_string ().c_str ());
3485 /* Fill in with reasonable starting values. */
3486 init_thread_stepping_state (cur_thr
);
3488 gdb_assert (!thread_is_in_step_over_chain (cur_thr
));
3491 = user_visible_resume_ptid (cur_thr
->control
.stepping_command
);
3492 process_stratum_target
*resume_target
3493 = user_visible_resume_target (resume_ptid
);
3495 check_multi_target_resumption (resume_target
);
3497 if (addr
== (CORE_ADDR
) -1)
3499 if (cur_thr
->stop_pc_p ()
3500 && pc
== cur_thr
->stop_pc ()
3501 && breakpoint_here_p (aspace
, pc
) == ordinary_breakpoint_here
3502 && execution_direction
!= EXEC_REVERSE
)
3503 /* There is a breakpoint at the address we will resume at,
3504 step one instruction before inserting breakpoints so that
3505 we do not stop right away (and report a second hit at this
3508 Note, we don't do this in reverse, because we won't
3509 actually be executing the breakpoint insn anyway.
3510 We'll be (un-)executing the previous instruction. */
3511 cur_thr
->stepping_over_breakpoint
= 1;
3512 else if (gdbarch_single_step_through_delay_p (gdbarch
)
3513 && gdbarch_single_step_through_delay (gdbarch
,
3514 get_current_frame ()))
3515 /* We stepped onto an instruction that needs to be stepped
3516 again before re-inserting the breakpoint, do so. */
3517 cur_thr
->stepping_over_breakpoint
= 1;
3521 regcache_write_pc (regcache
, addr
);
3524 if (siggnal
!= GDB_SIGNAL_DEFAULT
)
3525 cur_thr
->set_stop_signal (siggnal
);
3527 /* If an exception is thrown from this point on, make sure to
3528 propagate GDB's knowledge of the executing state to the
3529 frontend/user running state. */
3530 scoped_finish_thread_state
finish_state (resume_target
, resume_ptid
);
3532 /* Even if RESUME_PTID is a wildcard, and we end up resuming fewer
3533 threads (e.g., we might need to set threads stepping over
3534 breakpoints first), from the user/frontend's point of view, all
3535 threads in RESUME_PTID are now running. Unless we're calling an
3536 inferior function, as in that case we pretend the inferior
3537 doesn't run at all. */
3538 if (!cur_thr
->control
.in_infcall
)
3539 set_running (resume_target
, resume_ptid
, true);
3541 infrun_debug_printf ("addr=%s, signal=%s, resume_ptid=%s",
3542 paddress (gdbarch
, addr
),
3543 gdb_signal_to_symbol_string (siggnal
),
3544 resume_ptid
.to_string ().c_str ());
3546 annotate_starting ();
3548 /* Make sure that output from GDB appears before output from the
3550 gdb_flush (gdb_stdout
);
3552 /* Since we've marked the inferior running, give it the terminal. A
3553 QUIT/Ctrl-C from here on is forwarded to the target (which can
3554 still detect attempts to unblock a stuck connection with repeated
3555 Ctrl-C from within target_pass_ctrlc). */
3556 target_terminal::inferior ();
3558 /* In a multi-threaded task we may select another thread and
3559 then continue or step.
3561 But if a thread that we're resuming had stopped at a breakpoint,
3562 it will immediately cause another breakpoint stop without any
3563 execution (i.e. it will report a breakpoint hit incorrectly). So
3564 we must step over it first.
3566 Look for threads other than the current (TP) that reported a
3567 breakpoint hit and haven't been resumed yet since. */
3569 /* If scheduler locking applies, we can avoid iterating over all
3571 if (!non_stop
&& !schedlock_applies (cur_thr
))
3573 for (thread_info
*tp
: all_non_exited_threads (resume_target
,
3576 switch_to_thread_no_regs (tp
);
3578 /* Ignore the current thread here. It's handled
3583 if (!thread_still_needs_step_over (tp
))
3586 gdb_assert (!thread_is_in_step_over_chain (tp
));
3588 infrun_debug_printf ("need to step-over [%s] first",
3589 tp
->ptid
.to_string ().c_str ());
3591 global_thread_step_over_chain_enqueue (tp
);
3594 switch_to_thread (cur_thr
);
3597 /* Enqueue the current thread last, so that we move all other
3598 threads over their breakpoints first. */
3599 if (cur_thr
->stepping_over_breakpoint
)
3600 global_thread_step_over_chain_enqueue (cur_thr
);
3602 /* If the thread isn't started, we'll still need to set its prev_pc,
3603 so that switch_back_to_stepped_thread knows the thread hasn't
3604 advanced. Must do this before resuming any thread, as in
3605 all-stop/remote, once we resume we can't send any other packet
3606 until the target stops again. */
3607 cur_thr
->prev_pc
= regcache_read_pc_protected (regcache
);
3610 scoped_disable_commit_resumed
disable_commit_resumed ("proceeding");
3611 bool step_over_started
= start_step_over ();
3613 if (step_over_info_valid_p ())
3615 /* Either this thread started a new in-line step over, or some
3616 other thread was already doing one. In either case, don't
3617 resume anything else until the step-over is finished. */
3619 else if (step_over_started
&& !target_is_non_stop_p ())
3621 /* A new displaced stepping sequence was started. In all-stop,
3622 we can't talk to the target anymore until it next stops. */
3624 else if (!non_stop
&& target_is_non_stop_p ())
3626 INFRUN_SCOPED_DEBUG_START_END
3627 ("resuming threads, all-stop-on-top-of-non-stop");
3629 /* In all-stop, but the target is always in non-stop mode.
3630 Start all other threads that are implicitly resumed too. */
3631 for (thread_info
*tp
: all_non_exited_threads (resume_target
,
3634 switch_to_thread_no_regs (tp
);
3635 proceed_resume_thread_checked (tp
);
3639 proceed_resume_thread_checked (cur_thr
);
3641 disable_commit_resumed
.reset_and_commit ();
3644 finish_state
.release ();
3646 /* If we've switched threads above, switch back to the previously
3647 current thread. We don't want the user to see a different
3649 switch_to_thread (cur_thr
);
3651 /* Tell the event loop to wait for it to stop. If the target
3652 supports asynchronous execution, it'll do this from within
3654 if (!target_can_async_p ())
3655 mark_async_event_handler (infrun_async_inferior_event_token
);
3659 /* Start remote-debugging of a machine over a serial link. */
3662 start_remote (int from_tty
)
3664 inferior
*inf
= current_inferior ();
3665 inf
->control
.stop_soon
= STOP_QUIETLY_REMOTE
;
3667 /* Always go on waiting for the target, regardless of the mode. */
3668 /* FIXME: cagney/1999-09-23: At present it isn't possible to
3669 indicate to wait_for_inferior that a target should timeout if
3670 nothing is returned (instead of just blocking). Because of this,
3671 targets expecting an immediate response need to, internally, set
3672 things up so that the target_wait() is forced to eventually
3674 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
3675 differentiate to its caller what the state of the target is after
3676 the initial open has been performed. Here we're assuming that
3677 the target has stopped. It should be possible to eventually have
3678 target_open() return to the caller an indication that the target
3679 is currently running and GDB state should be set to the same as
3680 for an async run. */
3681 wait_for_inferior (inf
);
3683 /* Now that the inferior has stopped, do any bookkeeping like
3684 loading shared libraries. We want to do this before normal_stop,
3685 so that the displayed frame is up to date. */
3686 post_create_inferior (from_tty
);
3691 /* Initialize static vars when a new inferior begins. */
3694 init_wait_for_inferior (void)
3696 /* These are meaningless until the first time through wait_for_inferior. */
3698 breakpoint_init_inferior (inf_starting
);
3700 clear_proceed_status (0);
3702 nullify_last_target_wait_ptid ();
3704 update_previous_thread ();
3709 static void handle_inferior_event (struct execution_control_state
*ecs
);
3711 static void handle_step_into_function (struct gdbarch
*gdbarch
,
3712 struct execution_control_state
*ecs
);
3713 static void handle_step_into_function_backward (struct gdbarch
*gdbarch
,
3714 struct execution_control_state
*ecs
);
3715 static void handle_signal_stop (struct execution_control_state
*ecs
);
3716 static void check_exception_resume (struct execution_control_state
*,
3719 static void end_stepping_range (struct execution_control_state
*ecs
);
3720 static void stop_waiting (struct execution_control_state
*ecs
);
3721 static void keep_going (struct execution_control_state
*ecs
);
3722 static void process_event_stop_test (struct execution_control_state
*ecs
);
3723 static bool switch_back_to_stepped_thread (struct execution_control_state
*ecs
);
3725 /* This function is attached as a "thread_stop_requested" observer.
3726 Cleanup local state that assumed the PTID was to be resumed, and
3727 report the stop to the frontend. */
3730 infrun_thread_stop_requested (ptid_t ptid
)
3732 process_stratum_target
*curr_target
= current_inferior ()->process_target ();
3734 /* PTID was requested to stop. If the thread was already stopped,
3735 but the user/frontend doesn't know about that yet (e.g., the
3736 thread had been temporarily paused for some step-over), set up
3737 for reporting the stop now. */
3738 for (thread_info
*tp
: all_threads (curr_target
, ptid
))
3740 if (tp
->state
!= THREAD_RUNNING
)
3742 if (tp
->executing ())
3745 /* Remove matching threads from the step-over queue, so
3746 start_step_over doesn't try to resume them
3748 if (thread_is_in_step_over_chain (tp
))
3749 global_thread_step_over_chain_remove (tp
);
3751 /* If the thread is stopped, but the user/frontend doesn't
3752 know about that yet, queue a pending event, as if the
3753 thread had just stopped now. Unless the thread already had
3755 if (!tp
->has_pending_waitstatus ())
3757 target_waitstatus ws
;
3758 ws
.set_stopped (GDB_SIGNAL_0
);
3759 tp
->set_pending_waitstatus (ws
);
3762 /* Clear the inline-frame state, since we're re-processing the
3764 clear_inline_frame_state (tp
);
3766 /* If this thread was paused because some other thread was
3767 doing an inline-step over, let that finish first. Once
3768 that happens, we'll restart all threads and consume pending
3769 stop events then. */
3770 if (step_over_info_valid_p ())
3773 /* Otherwise we can process the (new) pending event now. Set
3774 it so this pending event is considered by
3776 tp
->set_resumed (true);
3780 /* Delete the step resume, single-step and longjmp/exception resume
3781 breakpoints of TP. */
3784 delete_thread_infrun_breakpoints (struct thread_info
*tp
)
3786 delete_step_resume_breakpoint (tp
);
3787 delete_exception_resume_breakpoint (tp
);
3788 delete_single_step_breakpoints (tp
);
3791 /* If the target still has execution, call FUNC for each thread that
3792 just stopped. In all-stop, that's all the non-exited threads; in
3793 non-stop, that's the current thread, only. */
3795 typedef void (*for_each_just_stopped_thread_callback_func
)
3796 (struct thread_info
*tp
);
3799 for_each_just_stopped_thread (for_each_just_stopped_thread_callback_func func
)
3801 if (!target_has_execution () || inferior_ptid
== null_ptid
)
3804 if (target_is_non_stop_p ())
3806 /* If in non-stop mode, only the current thread stopped. */
3807 func (inferior_thread ());
3811 /* In all-stop mode, all threads have stopped. */
3812 for (thread_info
*tp
: all_non_exited_threads ())
3817 /* Delete the step resume and longjmp/exception resume breakpoints of
3818 the threads that just stopped. */
3821 delete_just_stopped_threads_infrun_breakpoints (void)
3823 for_each_just_stopped_thread (delete_thread_infrun_breakpoints
);
3826 /* Delete the single-step breakpoints of the threads that just
3830 delete_just_stopped_threads_single_step_breakpoints (void)
3832 for_each_just_stopped_thread (delete_single_step_breakpoints
);
3838 print_target_wait_results (ptid_t waiton_ptid
, ptid_t result_ptid
,
3839 const struct target_waitstatus
&ws
)
3841 infrun_debug_printf ("target_wait (%s [%s], status) =",
3842 waiton_ptid
.to_string ().c_str (),
3843 target_pid_to_str (waiton_ptid
).c_str ());
3844 infrun_debug_printf (" %s [%s],",
3845 result_ptid
.to_string ().c_str (),
3846 target_pid_to_str (result_ptid
).c_str ());
3847 infrun_debug_printf (" %s", ws
.to_string ().c_str ());
3850 /* Select a thread at random, out of those which are resumed and have
3853 static struct thread_info
*
3854 random_pending_event_thread (inferior
*inf
, ptid_t waiton_ptid
)
3856 process_stratum_target
*proc_target
= inf
->process_target ();
3858 = proc_target
->random_resumed_with_pending_wait_status (inf
, waiton_ptid
);
3860 if (thread
== nullptr)
3862 infrun_debug_printf ("None found.");
3866 infrun_debug_printf ("Found %s.", thread
->ptid
.to_string ().c_str ());
3867 gdb_assert (thread
->resumed ());
3868 gdb_assert (thread
->has_pending_waitstatus ());
3873 /* Wrapper for target_wait that first checks whether threads have
3874 pending statuses to report before actually asking the target for
3875 more events. INF is the inferior we're using to call target_wait
3879 do_target_wait_1 (inferior
*inf
, ptid_t ptid
,
3880 target_waitstatus
*status
, target_wait_flags options
)
3882 struct thread_info
*tp
;
3884 /* We know that we are looking for an event in the target of inferior
3885 INF, but we don't know which thread the event might come from. As
3886 such we want to make sure that INFERIOR_PTID is reset so that none of
3887 the wait code relies on it - doing so is always a mistake. */
3888 switch_to_inferior_no_thread (inf
);
3890 /* First check if there is a resumed thread with a wait status
3892 if (ptid
== minus_one_ptid
|| ptid
.is_pid ())
3894 tp
= random_pending_event_thread (inf
, ptid
);
3898 infrun_debug_printf ("Waiting for specific thread %s.",
3899 ptid
.to_string ().c_str ());
3901 /* We have a specific thread to check. */
3902 tp
= inf
->find_thread (ptid
);
3903 gdb_assert (tp
!= nullptr);
3904 if (!tp
->has_pending_waitstatus ())
3909 && (tp
->stop_reason () == TARGET_STOPPED_BY_SW_BREAKPOINT
3910 || tp
->stop_reason () == TARGET_STOPPED_BY_HW_BREAKPOINT
))
3912 struct regcache
*regcache
= get_thread_regcache (tp
);
3913 struct gdbarch
*gdbarch
= regcache
->arch ();
3917 pc
= regcache_read_pc (regcache
);
3919 if (pc
!= tp
->stop_pc ())
3921 infrun_debug_printf ("PC of %s changed. was=%s, now=%s",
3922 tp
->ptid
.to_string ().c_str (),
3923 paddress (gdbarch
, tp
->stop_pc ()),
3924 paddress (gdbarch
, pc
));
3927 else if (!breakpoint_inserted_here_p (regcache
->aspace (), pc
))
3929 infrun_debug_printf ("previous breakpoint of %s, at %s gone",
3930 tp
->ptid
.to_string ().c_str (),
3931 paddress (gdbarch
, pc
));
3938 infrun_debug_printf ("pending event of %s cancelled.",
3939 tp
->ptid
.to_string ().c_str ());
3941 tp
->clear_pending_waitstatus ();
3942 target_waitstatus ws
;
3944 tp
->set_pending_waitstatus (ws
);
3945 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
3951 infrun_debug_printf ("Using pending wait status %s for %s.",
3952 tp
->pending_waitstatus ().to_string ().c_str (),
3953 tp
->ptid
.to_string ().c_str ());
3955 /* Now that we've selected our final event LWP, un-adjust its PC
3956 if it was a software breakpoint (and the target doesn't
3957 always adjust the PC itself). */
3958 if (tp
->stop_reason () == TARGET_STOPPED_BY_SW_BREAKPOINT
3959 && !target_supports_stopped_by_sw_breakpoint ())
3961 struct regcache
*regcache
;
3962 struct gdbarch
*gdbarch
;
3965 regcache
= get_thread_regcache (tp
);
3966 gdbarch
= regcache
->arch ();
3968 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
3973 pc
= regcache_read_pc (regcache
);
3974 regcache_write_pc (regcache
, pc
+ decr_pc
);
3978 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
3979 *status
= tp
->pending_waitstatus ();
3980 tp
->clear_pending_waitstatus ();
3982 /* Wake up the event loop again, until all pending events are
3984 if (target_is_async_p ())
3985 mark_async_event_handler (infrun_async_inferior_event_token
);
3989 /* But if we don't find one, we'll have to wait. */
3991 /* We can't ask a non-async target to do a non-blocking wait, so this will be
3993 if (!target_can_async_p ())
3994 options
&= ~TARGET_WNOHANG
;
3996 return target_wait (ptid
, status
, options
);
3999 /* Wrapper for target_wait that first checks whether threads have
4000 pending statuses to report before actually asking the target for
4001 more events. Polls for events from all inferiors/targets. */
4004 do_target_wait (execution_control_state
*ecs
, target_wait_flags options
)
4006 int num_inferiors
= 0;
4007 int random_selector
;
4009 /* For fairness, we pick the first inferior/target to poll at random
4010 out of all inferiors that may report events, and then continue
4011 polling the rest of the inferior list starting from that one in a
4012 circular fashion until the whole list is polled once. */
4014 auto inferior_matches
= [] (inferior
*inf
)
4016 return inf
->process_target () != nullptr;
4019 /* First see how many matching inferiors we have. */
4020 for (inferior
*inf
: all_inferiors ())
4021 if (inferior_matches (inf
))
4024 if (num_inferiors
== 0)
4026 ecs
->ws
.set_ignore ();
4030 /* Now randomly pick an inferior out of those that matched. */
4031 random_selector
= (int)
4032 ((num_inferiors
* (double) rand ()) / (RAND_MAX
+ 1.0));
4034 if (num_inferiors
> 1)
4035 infrun_debug_printf ("Found %d inferiors, starting at #%d",
4036 num_inferiors
, random_selector
);
4038 /* Select the Nth inferior that matched. */
4040 inferior
*selected
= nullptr;
4042 for (inferior
*inf
: all_inferiors ())
4043 if (inferior_matches (inf
))
4044 if (random_selector
-- == 0)
4050 /* Now poll for events out of each of the matching inferior's
4051 targets, starting from the selected one. */
4053 auto do_wait
= [&] (inferior
*inf
)
4055 ecs
->ptid
= do_target_wait_1 (inf
, minus_one_ptid
, &ecs
->ws
, options
);
4056 ecs
->target
= inf
->process_target ();
4057 return (ecs
->ws
.kind () != TARGET_WAITKIND_IGNORE
);
4060 /* Needed in 'all-stop + target-non-stop' mode, because we end up
4061 here spuriously after the target is all stopped and we've already
4062 reported the stop to the user, polling for events. */
4063 scoped_restore_current_thread restore_thread
;
4065 intrusive_list_iterator
<inferior
> start
4066 = inferior_list
.iterator_to (*selected
);
4068 for (intrusive_list_iterator
<inferior
> it
= start
;
4069 it
!= inferior_list
.end ();
4072 inferior
*inf
= &*it
;
4074 if (inferior_matches (inf
) && do_wait (inf
))
4078 for (intrusive_list_iterator
<inferior
> it
= inferior_list
.begin ();
4082 inferior
*inf
= &*it
;
4084 if (inferior_matches (inf
) && do_wait (inf
))
4088 ecs
->ws
.set_ignore ();
4092 /* An event reported by wait_one. */
4094 struct wait_one_event
4096 /* The target the event came out of. */
4097 process_stratum_target
*target
;
4099 /* The PTID the event was for. */
4102 /* The waitstatus. */
4103 target_waitstatus ws
;
4106 static bool handle_one (const wait_one_event
&event
);
4108 /* Prepare and stabilize the inferior for detaching it. E.g.,
4109 detaching while a thread is displaced stepping is a recipe for
4110 crashing it, as nothing would readjust the PC out of the scratch
4114 prepare_for_detach (void)
4116 struct inferior
*inf
= current_inferior ();
4117 ptid_t pid_ptid
= ptid_t (inf
->pid
);
4118 scoped_restore_current_thread restore_thread
;
4120 scoped_restore restore_detaching
= make_scoped_restore (&inf
->detaching
, true);
4122 /* Remove all threads of INF from the global step-over chain. We
4123 want to stop any ongoing step-over, not start any new one. */
4124 thread_step_over_list_safe_range range
4125 = make_thread_step_over_list_safe_range (global_thread_step_over_list
);
4127 for (thread_info
*tp
: range
)
4130 infrun_debug_printf ("removing thread %s from global step over chain",
4131 tp
->ptid
.to_string ().c_str ());
4132 global_thread_step_over_chain_remove (tp
);
4135 /* If we were already in the middle of an inline step-over, and the
4136 thread stepping belongs to the inferior we're detaching, we need
4137 to restart the threads of other inferiors. */
4138 if (step_over_info
.thread
!= -1)
4140 infrun_debug_printf ("inline step-over in-process while detaching");
4142 thread_info
*thr
= find_thread_global_id (step_over_info
.thread
);
4143 if (thr
->inf
== inf
)
4145 /* Since we removed threads of INF from the step-over chain,
4146 we know this won't start a step-over for INF. */
4147 clear_step_over_info ();
4149 if (target_is_non_stop_p ())
4151 /* Start a new step-over in another thread if there's
4152 one that needs it. */
4155 /* Restart all other threads (except the
4156 previously-stepping thread, since that one is still
4158 if (!step_over_info_valid_p ())
4159 restart_threads (thr
);
4164 if (displaced_step_in_progress (inf
))
4166 infrun_debug_printf ("displaced-stepping in-process while detaching");
4168 /* Stop threads currently displaced stepping, aborting it. */
4170 for (thread_info
*thr
: inf
->non_exited_threads ())
4172 if (thr
->displaced_step_state
.in_progress ())
4174 if (thr
->executing ())
4176 if (!thr
->stop_requested
)
4178 target_stop (thr
->ptid
);
4179 thr
->stop_requested
= true;
4183 thr
->set_resumed (false);
4187 while (displaced_step_in_progress (inf
))
4189 wait_one_event event
;
4191 event
.target
= inf
->process_target ();
4192 event
.ptid
= do_target_wait_1 (inf
, pid_ptid
, &event
.ws
, 0);
4195 print_target_wait_results (pid_ptid
, event
.ptid
, event
.ws
);
4200 /* It's OK to leave some of the threads of INF stopped, since
4201 they'll be detached shortly. */
4205 /* If all-stop, but there exists a non-stop target, stop all threads
4206 now that we're presenting the stop to the user. */
4209 stop_all_threads_if_all_stop_mode ()
4211 if (!non_stop
&& exists_non_stop_target ())
4212 stop_all_threads ("presenting stop to user in all-stop");
4215 /* Wait for control to return from inferior to debugger.
4217 If inferior gets a signal, we may decide to start it up again
4218 instead of returning. That is why there is a loop in this function.
4219 When this function actually returns it means the inferior
4220 should be left stopped and GDB should read more commands. */
4223 wait_for_inferior (inferior
*inf
)
4225 infrun_debug_printf ("wait_for_inferior ()");
4227 SCOPE_EXIT
{ delete_just_stopped_threads_infrun_breakpoints (); };
4229 /* If an error happens while handling the event, propagate GDB's
4230 knowledge of the executing state to the frontend/user running
4232 scoped_finish_thread_state finish_state
4233 (inf
->process_target (), minus_one_ptid
);
4237 execution_control_state ecs
;
4239 overlay_cache_invalid
= 1;
4241 /* Flush target cache before starting to handle each event.
4242 Target was running and cache could be stale. This is just a
4243 heuristic. Running threads may modify target memory, but we
4244 don't get any event. */
4245 target_dcache_invalidate ();
4247 ecs
.ptid
= do_target_wait_1 (inf
, minus_one_ptid
, &ecs
.ws
, 0);
4248 ecs
.target
= inf
->process_target ();
4251 print_target_wait_results (minus_one_ptid
, ecs
.ptid
, ecs
.ws
);
4253 /* Now figure out what to do with the result of the result. */
4254 handle_inferior_event (&ecs
);
4256 if (!ecs
.wait_some_more
)
4260 stop_all_threads_if_all_stop_mode ();
4262 /* No error, don't finish the state yet. */
4263 finish_state
.release ();
4266 /* Cleanup that reinstalls the readline callback handler, if the
4267 target is running in the background. If while handling the target
4268 event something triggered a secondary prompt, like e.g., a
4269 pagination prompt, we'll have removed the callback handler (see
4270 gdb_readline_wrapper_line). Need to do this as we go back to the
4271 event loop, ready to process further input. Note this has no
4272 effect if the handler hasn't actually been removed, because calling
4273 rl_callback_handler_install resets the line buffer, thus losing
4277 reinstall_readline_callback_handler_cleanup ()
4279 struct ui
*ui
= current_ui
;
4283 /* We're not going back to the top level event loop yet. Don't
4284 install the readline callback, as it'd prep the terminal,
4285 readline-style (raw, noecho) (e.g., --batch). We'll install
4286 it the next time the prompt is displayed, when we're ready
4291 if (ui
->command_editing
&& ui
->prompt_state
!= PROMPT_BLOCKED
)
4292 gdb_rl_callback_handler_reinstall ();
4295 /* Clean up the FSMs of threads that are now stopped. In non-stop,
4296 that's just the event thread. In all-stop, that's all threads. */
4299 clean_up_just_stopped_threads_fsms (struct execution_control_state
*ecs
)
4301 /* The first clean_up call below assumes the event thread is the current
4303 if (ecs
->event_thread
!= nullptr)
4304 gdb_assert (ecs
->event_thread
== inferior_thread ());
4306 if (ecs
->event_thread
!= nullptr
4307 && ecs
->event_thread
->thread_fsm () != nullptr)
4308 ecs
->event_thread
->thread_fsm ()->clean_up (ecs
->event_thread
);
4312 scoped_restore_current_thread restore_thread
;
4314 for (thread_info
*thr
: all_non_exited_threads ())
4316 if (thr
->thread_fsm () == nullptr)
4318 if (thr
== ecs
->event_thread
)
4321 switch_to_thread (thr
);
4322 thr
->thread_fsm ()->clean_up (thr
);
4327 /* Helper for all_uis_check_sync_execution_done that works on the
4331 check_curr_ui_sync_execution_done (void)
4333 struct ui
*ui
= current_ui
;
4335 if (ui
->prompt_state
== PROMPT_NEEDED
4337 && !gdb_in_secondary_prompt_p (ui
))
4339 target_terminal::ours ();
4340 top_level_interpreter ()->on_sync_execution_done ();
4341 ui
->register_file_handler ();
4348 all_uis_check_sync_execution_done (void)
4350 SWITCH_THRU_ALL_UIS ()
4352 check_curr_ui_sync_execution_done ();
4359 all_uis_on_sync_execution_starting (void)
4361 SWITCH_THRU_ALL_UIS ()
4363 if (current_ui
->prompt_state
== PROMPT_NEEDED
)
4364 async_disable_stdin ();
4368 /* A quit_handler callback installed while we're handling inferior
4372 infrun_quit_handler ()
4374 if (target_terminal::is_ours ())
4378 default_quit_handler would throw a quit in this case, but if
4379 we're handling an event while we have the terminal, it means
4380 the target is running a background execution command, and
4381 thus when users press Ctrl-C, they're wanting to interrupt
4382 whatever command they were executing in the command line.
4386 (gdb) foo bar whatever<ctrl-c>
4388 That Ctrl-C should clear the input line, not interrupt event
4389 handling if it happens that the user types Ctrl-C at just the
4392 It's as-if background event handling was handled by a
4393 separate background thread.
4395 To be clear, the Ctrl-C is not lost -- it will be processed
4396 by the next QUIT call once we're out of fetch_inferior_event
4401 if (check_quit_flag ())
4402 target_pass_ctrlc ();
4406 /* Asynchronous version of wait_for_inferior. It is called by the
4407 event loop whenever a change of state is detected on the file
4408 descriptor corresponding to the target. It can be called more than
4409 once to complete a single execution command. In such cases we need
4410 to keep the state in a global variable ECSS. If it is the last time
4411 that this function is called for a single execution command, then
4412 report to the user that the inferior has stopped, and do the
4413 necessary cleanups. */
4416 fetch_inferior_event ()
4418 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
4420 execution_control_state ecs
;
4423 /* Events are always processed with the main UI as current UI. This
4424 way, warnings, debug output, etc. are always consistently sent to
4425 the main console. */
4426 scoped_restore save_ui
= make_scoped_restore (¤t_ui
, main_ui
);
4428 /* Temporarily disable pagination. Otherwise, the user would be
4429 given an option to press 'q' to quit, which would cause an early
4430 exit and could leave GDB in a half-baked state. */
4431 scoped_restore save_pagination
4432 = make_scoped_restore (&pagination_enabled
, false);
4434 /* Install a quit handler that does nothing if we have the terminal
4435 (meaning the target is running a background execution command),
4436 so that Ctrl-C never interrupts GDB before the event is fully
4438 scoped_restore restore_quit_handler
4439 = make_scoped_restore (&quit_handler
, infrun_quit_handler
);
4441 /* Make sure a SIGINT does not interrupt an extension language while
4442 we're handling an event. That could interrupt a Python unwinder
4443 or a Python observer or some such. A Ctrl-C should either be
4444 forwarded to the inferior if the inferior has the terminal, or,
4445 if GDB has the terminal, should interrupt the command the user is
4446 typing in the CLI. */
4447 scoped_disable_cooperative_sigint_handling restore_coop_sigint
;
4449 /* End up with readline processing input, if necessary. */
4451 SCOPE_EXIT
{ reinstall_readline_callback_handler_cleanup (); };
4453 /* We're handling a live event, so make sure we're doing live
4454 debugging. If we're looking at traceframes while the target is
4455 running, we're going to need to get back to that mode after
4456 handling the event. */
4457 gdb::optional
<scoped_restore_current_traceframe
> maybe_restore_traceframe
;
4460 maybe_restore_traceframe
.emplace ();
4461 set_current_traceframe (-1);
4464 /* The user/frontend should not notice a thread switch due to
4465 internal events. Make sure we revert to the user selected
4466 thread and frame after handling the event and running any
4467 breakpoint commands. */
4468 scoped_restore_current_thread restore_thread
;
4470 overlay_cache_invalid
= 1;
4471 /* Flush target cache before starting to handle each event. Target
4472 was running and cache could be stale. This is just a heuristic.
4473 Running threads may modify target memory, but we don't get any
4475 target_dcache_invalidate ();
4477 scoped_restore save_exec_dir
4478 = make_scoped_restore (&execution_direction
,
4479 target_execution_direction ());
4481 /* Allow targets to pause their resumed threads while we handle
4483 scoped_disable_commit_resumed
disable_commit_resumed ("handling event");
4485 if (!do_target_wait (&ecs
, TARGET_WNOHANG
))
4487 infrun_debug_printf ("do_target_wait returned no event");
4488 disable_commit_resumed
.reset_and_commit ();
4492 gdb_assert (ecs
.ws
.kind () != TARGET_WAITKIND_IGNORE
);
4494 /* Switch to the inferior that generated the event, so we can do
4495 target calls. If the event was not associated to a ptid, */
4496 if (ecs
.ptid
!= null_ptid
4497 && ecs
.ptid
!= minus_one_ptid
)
4498 switch_to_inferior_no_thread (find_inferior_ptid (ecs
.target
, ecs
.ptid
));
4500 switch_to_target_no_thread (ecs
.target
);
4503 print_target_wait_results (minus_one_ptid
, ecs
.ptid
, ecs
.ws
);
4505 /* If an error happens while handling the event, propagate GDB's
4506 knowledge of the executing state to the frontend/user running
4508 ptid_t finish_ptid
= !target_is_non_stop_p () ? minus_one_ptid
: ecs
.ptid
;
4509 scoped_finish_thread_state
finish_state (ecs
.target
, finish_ptid
);
4511 /* Get executed before scoped_restore_current_thread above to apply
4512 still for the thread which has thrown the exception. */
4513 auto defer_bpstat_clear
4514 = make_scope_exit (bpstat_clear_actions
);
4515 auto defer_delete_threads
4516 = make_scope_exit (delete_just_stopped_threads_infrun_breakpoints
);
4518 int stop_id
= get_stop_id ();
4520 /* Now figure out what to do with the result of the result. */
4521 handle_inferior_event (&ecs
);
4523 if (!ecs
.wait_some_more
)
4525 struct inferior
*inf
= find_inferior_ptid (ecs
.target
, ecs
.ptid
);
4526 bool should_stop
= true;
4527 struct thread_info
*thr
= ecs
.event_thread
;
4529 delete_just_stopped_threads_infrun_breakpoints ();
4531 if (thr
!= nullptr && thr
->thread_fsm () != nullptr)
4532 should_stop
= thr
->thread_fsm ()->should_stop (thr
);
4540 bool should_notify_stop
= true;
4541 bool proceeded
= false;
4543 stop_all_threads_if_all_stop_mode ();
4545 clean_up_just_stopped_threads_fsms (&ecs
);
4547 if (stop_id
!= get_stop_id ())
4549 /* If the stop-id has changed then a stop has already been
4550 presented to the user in handle_inferior_event, this is
4551 likely a failed inferior call. As the stop has already
4552 been announced then we should not notify again.
4554 Also, if the prompt state is not PROMPT_NEEDED then GDB
4555 will not be ready for user input after this function. */
4556 should_notify_stop
= false;
4557 gdb_assert (current_ui
->prompt_state
== PROMPT_NEEDED
);
4559 else if (thr
!= nullptr && thr
->thread_fsm () != nullptr)
4561 = thr
->thread_fsm ()->should_notify_stop ();
4563 if (should_notify_stop
)
4565 /* We may not find an inferior if this was a process exit. */
4566 if (inf
== nullptr || inf
->control
.stop_soon
== NO_STOP_QUIETLY
)
4567 proceeded
= normal_stop ();
4572 inferior_event_handler (INF_EXEC_COMPLETE
);
4576 /* If we got a TARGET_WAITKIND_NO_RESUMED event, then the
4577 previously selected thread is gone. We have two
4578 choices - switch to no thread selected, or restore the
4579 previously selected thread (now exited). We chose the
4580 later, just because that's what GDB used to do. After
4581 this, "info threads" says "The current thread <Thread
4582 ID 2> has terminated." instead of "No thread
4586 && ecs
.ws
.kind () != TARGET_WAITKIND_NO_RESUMED
)
4587 restore_thread
.dont_restore ();
4591 defer_delete_threads
.release ();
4592 defer_bpstat_clear
.release ();
4594 /* No error, don't finish the thread states yet. */
4595 finish_state
.release ();
4597 disable_commit_resumed
.reset_and_commit ();
4599 /* This scope is used to ensure that readline callbacks are
4600 reinstalled here. */
4603 /* Handling this event might have caused some inferiors to become prunable.
4604 For example, the exit of an inferior that was automatically added. Try
4605 to get rid of them. Keeping those around slows down things linearly.
4607 Note that this never removes the current inferior. Therefore, call this
4608 after RESTORE_THREAD went out of scope, in case the event inferior (which was
4609 temporarily made the current inferior) is meant to be deleted.
4611 Call this before all_uis_check_sync_execution_done, so that notifications about
4612 removed inferiors appear before the prompt. */
4615 /* If a UI was in sync execution mode, and now isn't, restore its
4616 prompt (a synchronous execution command has finished, and we're
4617 ready for input). */
4618 all_uis_check_sync_execution_done ();
4621 && exec_done_display_p
4622 && (inferior_ptid
== null_ptid
4623 || inferior_thread ()->state
!= THREAD_RUNNING
))
4624 gdb_printf (_("completed.\n"));
4630 set_step_info (thread_info
*tp
, frame_info_ptr frame
,
4631 struct symtab_and_line sal
)
4633 /* This can be removed once this function no longer implicitly relies on the
4634 inferior_ptid value. */
4635 gdb_assert (inferior_ptid
== tp
->ptid
);
4637 tp
->control
.step_frame_id
= get_frame_id (frame
);
4638 tp
->control
.step_stack_frame_id
= get_stack_frame_id (frame
);
4640 tp
->current_symtab
= sal
.symtab
;
4641 tp
->current_line
= sal
.line
;
4644 ("symtab = %s, line = %d, step_frame_id = %s, step_stack_frame_id = %s",
4645 tp
->current_symtab
!= nullptr ? tp
->current_symtab
->filename
: "<null>",
4647 tp
->control
.step_frame_id
.to_string ().c_str (),
4648 tp
->control
.step_stack_frame_id
.to_string ().c_str ());
4651 /* Clear context switchable stepping state. */
4654 init_thread_stepping_state (struct thread_info
*tss
)
4656 tss
->stepped_breakpoint
= 0;
4657 tss
->stepping_over_breakpoint
= 0;
4658 tss
->stepping_over_watchpoint
= 0;
4659 tss
->step_after_step_resume_breakpoint
= 0;
4665 set_last_target_status (process_stratum_target
*target
, ptid_t ptid
,
4666 const target_waitstatus
&status
)
4668 target_last_proc_target
= target
;
4669 target_last_wait_ptid
= ptid
;
4670 target_last_waitstatus
= status
;
4676 get_last_target_status (process_stratum_target
**target
, ptid_t
*ptid
,
4677 target_waitstatus
*status
)
4679 if (target
!= nullptr)
4680 *target
= target_last_proc_target
;
4681 if (ptid
!= nullptr)
4682 *ptid
= target_last_wait_ptid
;
4683 if (status
!= nullptr)
4684 *status
= target_last_waitstatus
;
4690 nullify_last_target_wait_ptid (void)
4692 target_last_proc_target
= nullptr;
4693 target_last_wait_ptid
= minus_one_ptid
;
4694 target_last_waitstatus
= {};
4697 /* Switch thread contexts. */
4700 context_switch (execution_control_state
*ecs
)
4702 if (ecs
->ptid
!= inferior_ptid
4703 && (inferior_ptid
== null_ptid
4704 || ecs
->event_thread
!= inferior_thread ()))
4706 infrun_debug_printf ("Switching context from %s to %s",
4707 inferior_ptid
.to_string ().c_str (),
4708 ecs
->ptid
.to_string ().c_str ());
4711 switch_to_thread (ecs
->event_thread
);
4714 /* If the target can't tell whether we've hit breakpoints
4715 (target_supports_stopped_by_sw_breakpoint), and we got a SIGTRAP,
4716 check whether that could have been caused by a breakpoint. If so,
4717 adjust the PC, per gdbarch_decr_pc_after_break. */
4720 adjust_pc_after_break (struct thread_info
*thread
,
4721 const target_waitstatus
&ws
)
4723 struct regcache
*regcache
;
4724 struct gdbarch
*gdbarch
;
4725 CORE_ADDR breakpoint_pc
, decr_pc
;
4727 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
4728 we aren't, just return.
4730 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
4731 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
4732 implemented by software breakpoints should be handled through the normal
4735 NOTE drow/2004-01-31: On some targets, breakpoints may generate
4736 different signals (SIGILL or SIGEMT for instance), but it is less
4737 clear where the PC is pointing afterwards. It may not match
4738 gdbarch_decr_pc_after_break. I don't know any specific target that
4739 generates these signals at breakpoints (the code has been in GDB since at
4740 least 1992) so I can not guess how to handle them here.
4742 In earlier versions of GDB, a target with
4743 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
4744 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
4745 target with both of these set in GDB history, and it seems unlikely to be
4746 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
4748 if (ws
.kind () != TARGET_WAITKIND_STOPPED
)
4751 if (ws
.sig () != GDB_SIGNAL_TRAP
)
4754 /* In reverse execution, when a breakpoint is hit, the instruction
4755 under it has already been de-executed. The reported PC always
4756 points at the breakpoint address, so adjusting it further would
4757 be wrong. E.g., consider this case on a decr_pc_after_break == 1
4760 B1 0x08000000 : INSN1
4761 B2 0x08000001 : INSN2
4763 PC -> 0x08000003 : INSN4
4765 Say you're stopped at 0x08000003 as above. Reverse continuing
4766 from that point should hit B2 as below. Reading the PC when the
4767 SIGTRAP is reported should read 0x08000001 and INSN2 should have
4768 been de-executed already.
4770 B1 0x08000000 : INSN1
4771 B2 PC -> 0x08000001 : INSN2
4775 We can't apply the same logic as for forward execution, because
4776 we would wrongly adjust the PC to 0x08000000, since there's a
4777 breakpoint at PC - 1. We'd then report a hit on B1, although
4778 INSN1 hadn't been de-executed yet. Doing nothing is the correct
4780 if (execution_direction
== EXEC_REVERSE
)
4783 /* If the target can tell whether the thread hit a SW breakpoint,
4784 trust it. Targets that can tell also adjust the PC
4786 if (target_supports_stopped_by_sw_breakpoint ())
4789 /* Note that relying on whether a breakpoint is planted in memory to
4790 determine this can fail. E.g,. the breakpoint could have been
4791 removed since. Or the thread could have been told to step an
4792 instruction the size of a breakpoint instruction, and only
4793 _after_ was a breakpoint inserted at its address. */
4795 /* If this target does not decrement the PC after breakpoints, then
4796 we have nothing to do. */
4797 regcache
= get_thread_regcache (thread
);
4798 gdbarch
= regcache
->arch ();
4800 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
4804 const address_space
*aspace
= regcache
->aspace ();
4806 /* Find the location where (if we've hit a breakpoint) the
4807 breakpoint would be. */
4808 breakpoint_pc
= regcache_read_pc (regcache
) - decr_pc
;
4810 /* If the target can't tell whether a software breakpoint triggered,
4811 fallback to figuring it out based on breakpoints we think were
4812 inserted in the target, and on whether the thread was stepped or
4815 /* Check whether there actually is a software breakpoint inserted at
4818 If in non-stop mode, a race condition is possible where we've
4819 removed a breakpoint, but stop events for that breakpoint were
4820 already queued and arrive later. To suppress those spurious
4821 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
4822 and retire them after a number of stop events are reported. Note
4823 this is an heuristic and can thus get confused. The real fix is
4824 to get the "stopped by SW BP and needs adjustment" info out of
4825 the target/kernel (and thus never reach here; see above). */
4826 if (software_breakpoint_inserted_here_p (aspace
, breakpoint_pc
)
4827 || (target_is_non_stop_p ()
4828 && moribund_breakpoint_here_p (aspace
, breakpoint_pc
)))
4830 gdb::optional
<scoped_restore_tmpl
<int>> restore_operation_disable
;
4832 if (record_full_is_used ())
4833 restore_operation_disable
.emplace
4834 (record_full_gdb_operation_disable_set ());
4836 /* When using hardware single-step, a SIGTRAP is reported for both
4837 a completed single-step and a software breakpoint. Need to
4838 differentiate between the two, as the latter needs adjusting
4839 but the former does not.
4841 The SIGTRAP can be due to a completed hardware single-step only if
4842 - we didn't insert software single-step breakpoints
4843 - this thread is currently being stepped
4845 If any of these events did not occur, we must have stopped due
4846 to hitting a software breakpoint, and have to back up to the
4849 As a special case, we could have hardware single-stepped a
4850 software breakpoint. In this case (prev_pc == breakpoint_pc),
4851 we also need to back up to the breakpoint address. */
4853 if (thread_has_single_step_breakpoints_set (thread
)
4854 || !currently_stepping (thread
)
4855 || (thread
->stepped_breakpoint
4856 && thread
->prev_pc
== breakpoint_pc
))
4857 regcache_write_pc (regcache
, breakpoint_pc
);
4862 stepped_in_from (frame_info_ptr frame
, struct frame_id step_frame_id
)
4864 for (frame
= get_prev_frame (frame
);
4866 frame
= get_prev_frame (frame
))
4868 if (get_frame_id (frame
) == step_frame_id
)
4871 if (get_frame_type (frame
) != INLINE_FRAME
)
4878 /* Look for an inline frame that is marked for skip.
4879 If PREV_FRAME is TRUE start at the previous frame,
4880 otherwise start at the current frame. Stop at the
4881 first non-inline frame, or at the frame where the
4885 inline_frame_is_marked_for_skip (bool prev_frame
, struct thread_info
*tp
)
4887 frame_info_ptr frame
= get_current_frame ();
4890 frame
= get_prev_frame (frame
);
4892 for (; frame
!= nullptr; frame
= get_prev_frame (frame
))
4894 const char *fn
= nullptr;
4895 symtab_and_line sal
;
4898 if (get_frame_id (frame
) == tp
->control
.step_frame_id
)
4900 if (get_frame_type (frame
) != INLINE_FRAME
)
4903 sal
= find_frame_sal (frame
);
4904 sym
= get_frame_function (frame
);
4907 fn
= sym
->print_name ();
4910 && function_name_is_marked_for_skip (fn
, sal
))
4917 /* If the event thread has the stop requested flag set, pretend it
4918 stopped for a GDB_SIGNAL_0 (i.e., as if it stopped due to
4922 handle_stop_requested (struct execution_control_state
*ecs
)
4924 if (ecs
->event_thread
->stop_requested
)
4926 ecs
->ws
.set_stopped (GDB_SIGNAL_0
);
4927 handle_signal_stop (ecs
);
4933 /* Auxiliary function that handles syscall entry/return events.
4934 It returns true if the inferior should keep going (and GDB
4935 should ignore the event), or false if the event deserves to be
4939 handle_syscall_event (struct execution_control_state
*ecs
)
4941 struct regcache
*regcache
;
4944 context_switch (ecs
);
4946 regcache
= get_thread_regcache (ecs
->event_thread
);
4947 syscall_number
= ecs
->ws
.syscall_number ();
4948 ecs
->event_thread
->set_stop_pc (regcache_read_pc (regcache
));
4950 if (catch_syscall_enabled () > 0
4951 && catching_syscall_number (syscall_number
))
4953 infrun_debug_printf ("syscall number=%d", syscall_number
);
4955 ecs
->event_thread
->control
.stop_bpstat
4956 = bpstat_stop_status_nowatch (regcache
->aspace (),
4957 ecs
->event_thread
->stop_pc (),
4958 ecs
->event_thread
, ecs
->ws
);
4960 if (handle_stop_requested (ecs
))
4963 if (bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
4965 /* Catchpoint hit. */
4970 if (handle_stop_requested (ecs
))
4973 /* If no catchpoint triggered for this, then keep going. */
4979 /* Lazily fill in the execution_control_state's stop_func_* fields. */
4982 fill_in_stop_func (struct gdbarch
*gdbarch
,
4983 struct execution_control_state
*ecs
)
4985 if (!ecs
->stop_func_filled_in
)
4988 const general_symbol_info
*gsi
;
4990 /* Don't care about return value; stop_func_start and stop_func_name
4991 will both be 0 if it doesn't work. */
4992 find_pc_partial_function_sym (ecs
->event_thread
->stop_pc (),
4994 &ecs
->stop_func_start
,
4995 &ecs
->stop_func_end
,
4997 ecs
->stop_func_name
= gsi
== nullptr ? nullptr : gsi
->print_name ();
4999 /* The call to find_pc_partial_function, above, will set
5000 stop_func_start and stop_func_end to the start and end
5001 of the range containing the stop pc. If this range
5002 contains the entry pc for the block (which is always the
5003 case for contiguous blocks), advance stop_func_start past
5004 the function's start offset and entrypoint. Note that
5005 stop_func_start is NOT advanced when in a range of a
5006 non-contiguous block that does not contain the entry pc. */
5007 if (block
!= nullptr
5008 && ecs
->stop_func_start
<= block
->entry_pc ()
5009 && block
->entry_pc () < ecs
->stop_func_end
)
5011 ecs
->stop_func_start
5012 += gdbarch_deprecated_function_start_offset (gdbarch
);
5014 /* PowerPC functions have a Local Entry Point (LEP) and a Global
5015 Entry Point (GEP). There is only one Entry Point (GEP = LEP) for
5016 other architectures. */
5017 ecs
->stop_func_alt_start
= ecs
->stop_func_start
;
5019 if (gdbarch_skip_entrypoint_p (gdbarch
))
5020 ecs
->stop_func_start
5021 = gdbarch_skip_entrypoint (gdbarch
, ecs
->stop_func_start
);
5024 ecs
->stop_func_filled_in
= 1;
5029 /* Return the STOP_SOON field of the inferior pointed at by ECS. */
5031 static enum stop_kind
5032 get_inferior_stop_soon (execution_control_state
*ecs
)
5034 struct inferior
*inf
= find_inferior_ptid (ecs
->target
, ecs
->ptid
);
5036 gdb_assert (inf
!= nullptr);
5037 return inf
->control
.stop_soon
;
5040 /* Poll for one event out of the current target. Store the resulting
5041 waitstatus in WS, and return the event ptid. Does not block. */
5044 poll_one_curr_target (struct target_waitstatus
*ws
)
5048 overlay_cache_invalid
= 1;
5050 /* Flush target cache before starting to handle each event.
5051 Target was running and cache could be stale. This is just a
5052 heuristic. Running threads may modify target memory, but we
5053 don't get any event. */
5054 target_dcache_invalidate ();
5056 event_ptid
= target_wait (minus_one_ptid
, ws
, TARGET_WNOHANG
);
5059 print_target_wait_results (minus_one_ptid
, event_ptid
, *ws
);
5064 /* Wait for one event out of any target. */
5066 static wait_one_event
5071 for (inferior
*inf
: all_inferiors ())
5073 process_stratum_target
*target
= inf
->process_target ();
5074 if (target
== nullptr
5075 || !target
->is_async_p ()
5076 || !target
->threads_executing
)
5079 switch_to_inferior_no_thread (inf
);
5081 wait_one_event event
;
5082 event
.target
= target
;
5083 event
.ptid
= poll_one_curr_target (&event
.ws
);
5085 if (event
.ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
5087 /* If nothing is resumed, remove the target from the
5089 target_async (false);
5091 else if (event
.ws
.kind () != TARGET_WAITKIND_IGNORE
)
5095 /* Block waiting for some event. */
5102 for (inferior
*inf
: all_inferiors ())
5104 process_stratum_target
*target
= inf
->process_target ();
5105 if (target
== nullptr
5106 || !target
->is_async_p ()
5107 || !target
->threads_executing
)
5110 int fd
= target
->async_wait_fd ();
5111 FD_SET (fd
, &readfds
);
5118 /* No waitable targets left. All must be stopped. */
5119 target_waitstatus ws
;
5120 ws
.set_no_resumed ();
5121 return {nullptr, minus_one_ptid
, std::move (ws
)};
5126 int numfds
= interruptible_select (nfds
, &readfds
, 0, nullptr, 0);
5132 perror_with_name ("interruptible_select");
5137 /* Save the thread's event and stop reason to process it later. */
5140 save_waitstatus (struct thread_info
*tp
, const target_waitstatus
&ws
)
5142 infrun_debug_printf ("saving status %s for %s",
5143 ws
.to_string ().c_str (),
5144 tp
->ptid
.to_string ().c_str ());
5146 /* Record for later. */
5147 tp
->set_pending_waitstatus (ws
);
5149 if (ws
.kind () == TARGET_WAITKIND_STOPPED
5150 && ws
.sig () == GDB_SIGNAL_TRAP
)
5152 struct regcache
*regcache
= get_thread_regcache (tp
);
5153 const address_space
*aspace
= regcache
->aspace ();
5154 CORE_ADDR pc
= regcache_read_pc (regcache
);
5156 adjust_pc_after_break (tp
, tp
->pending_waitstatus ());
5158 scoped_restore_current_thread restore_thread
;
5159 switch_to_thread (tp
);
5161 if (target_stopped_by_watchpoint ())
5162 tp
->set_stop_reason (TARGET_STOPPED_BY_WATCHPOINT
);
5163 else if (target_supports_stopped_by_sw_breakpoint ()
5164 && target_stopped_by_sw_breakpoint ())
5165 tp
->set_stop_reason (TARGET_STOPPED_BY_SW_BREAKPOINT
);
5166 else if (target_supports_stopped_by_hw_breakpoint ()
5167 && target_stopped_by_hw_breakpoint ())
5168 tp
->set_stop_reason (TARGET_STOPPED_BY_HW_BREAKPOINT
);
5169 else if (!target_supports_stopped_by_hw_breakpoint ()
5170 && hardware_breakpoint_inserted_here_p (aspace
, pc
))
5171 tp
->set_stop_reason (TARGET_STOPPED_BY_HW_BREAKPOINT
);
5172 else if (!target_supports_stopped_by_sw_breakpoint ()
5173 && software_breakpoint_inserted_here_p (aspace
, pc
))
5174 tp
->set_stop_reason (TARGET_STOPPED_BY_SW_BREAKPOINT
);
5175 else if (!thread_has_single_step_breakpoints_set (tp
)
5176 && currently_stepping (tp
))
5177 tp
->set_stop_reason (TARGET_STOPPED_BY_SINGLE_STEP
);
5181 /* Mark the non-executing threads accordingly. In all-stop, all
5182 threads of all processes are stopped when we get any event
5183 reported. In non-stop mode, only the event thread stops. */
5186 mark_non_executing_threads (process_stratum_target
*target
,
5188 const target_waitstatus
&ws
)
5192 if (!target_is_non_stop_p ())
5193 mark_ptid
= minus_one_ptid
;
5194 else if (ws
.kind () == TARGET_WAITKIND_SIGNALLED
5195 || ws
.kind () == TARGET_WAITKIND_EXITED
)
5197 /* If we're handling a process exit in non-stop mode, even
5198 though threads haven't been deleted yet, one would think
5199 that there is nothing to do, as threads of the dead process
5200 will be soon deleted, and threads of any other process were
5201 left running. However, on some targets, threads survive a
5202 process exit event. E.g., for the "checkpoint" command,
5203 when the current checkpoint/fork exits, linux-fork.c
5204 automatically switches to another fork from within
5205 target_mourn_inferior, by associating the same
5206 inferior/thread to another fork. We haven't mourned yet at
5207 this point, but we must mark any threads left in the
5208 process as not-executing so that finish_thread_state marks
5209 them stopped (in the user's perspective) if/when we present
5210 the stop to the user. */
5211 mark_ptid
= ptid_t (event_ptid
.pid ());
5214 mark_ptid
= event_ptid
;
5216 set_executing (target
, mark_ptid
, false);
5218 /* Likewise the resumed flag. */
5219 set_resumed (target
, mark_ptid
, false);
5222 /* Handle one event after stopping threads. If the eventing thread
5223 reports back any interesting event, we leave it pending. If the
5224 eventing thread was in the middle of a displaced step, we
5225 cancel/finish it, and unless the thread's inferior is being
5226 detached, put the thread back in the step-over chain. Returns true
5227 if there are no resumed threads left in the target (thus there's no
5228 point in waiting further), false otherwise. */
5231 handle_one (const wait_one_event
&event
)
5234 ("%s %s", event
.ws
.to_string ().c_str (),
5235 event
.ptid
.to_string ().c_str ());
5237 if (event
.ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
5239 /* All resumed threads exited. */
5242 else if (event
.ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
5243 || event
.ws
.kind () == TARGET_WAITKIND_EXITED
5244 || event
.ws
.kind () == TARGET_WAITKIND_SIGNALLED
)
5246 /* One thread/process exited/signalled. */
5248 thread_info
*t
= nullptr;
5250 /* The target may have reported just a pid. If so, try
5251 the first non-exited thread. */
5252 if (event
.ptid
.is_pid ())
5254 int pid
= event
.ptid
.pid ();
5255 inferior
*inf
= find_inferior_pid (event
.target
, pid
);
5256 for (thread_info
*tp
: inf
->non_exited_threads ())
5262 /* If there is no available thread, the event would
5263 have to be appended to a per-inferior event list,
5264 which does not exist (and if it did, we'd have
5265 to adjust run control command to be able to
5266 resume such an inferior). We assert here instead
5267 of going into an infinite loop. */
5268 gdb_assert (t
!= nullptr);
5271 ("using %s", t
->ptid
.to_string ().c_str ());
5275 t
= event
.target
->find_thread (event
.ptid
);
5276 /* Check if this is the first time we see this thread.
5277 Don't bother adding if it individually exited. */
5279 && event
.ws
.kind () != TARGET_WAITKIND_THREAD_EXITED
)
5280 t
= add_thread (event
.target
, event
.ptid
);
5285 /* Set the threads as non-executing to avoid
5286 another stop attempt on them. */
5287 switch_to_thread_no_regs (t
);
5288 mark_non_executing_threads (event
.target
, event
.ptid
,
5290 save_waitstatus (t
, event
.ws
);
5291 t
->stop_requested
= false;
5296 thread_info
*t
= event
.target
->find_thread (event
.ptid
);
5298 t
= add_thread (event
.target
, event
.ptid
);
5300 t
->stop_requested
= 0;
5301 t
->set_executing (false);
5302 t
->set_resumed (false);
5303 t
->control
.may_range_step
= 0;
5305 /* This may be the first time we see the inferior report
5307 if (t
->inf
->needs_setup
)
5309 switch_to_thread_no_regs (t
);
5313 if (event
.ws
.kind () == TARGET_WAITKIND_STOPPED
5314 && event
.ws
.sig () == GDB_SIGNAL_0
)
5316 /* We caught the event that we intended to catch, so
5317 there's no event to save as pending. */
5319 if (displaced_step_finish (t
, event
.ws
)
5320 == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED
)
5322 /* Add it back to the step-over queue. */
5324 ("displaced-step of %s canceled",
5325 t
->ptid
.to_string ().c_str ());
5327 t
->control
.trap_expected
= 0;
5328 if (!t
->inf
->detaching
)
5329 global_thread_step_over_chain_enqueue (t
);
5334 struct regcache
*regcache
;
5337 ("target_wait %s, saving status for %s",
5338 event
.ws
.to_string ().c_str (),
5339 t
->ptid
.to_string ().c_str ());
5341 /* Record for later. */
5342 save_waitstatus (t
, event
.ws
);
5344 if (displaced_step_finish (t
, event
.ws
)
5345 == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED
)
5347 /* Add it back to the step-over queue. */
5348 t
->control
.trap_expected
= 0;
5349 if (!t
->inf
->detaching
)
5350 global_thread_step_over_chain_enqueue (t
);
5353 regcache
= get_thread_regcache (t
);
5354 t
->set_stop_pc (regcache_read_pc (regcache
));
5356 infrun_debug_printf ("saved stop_pc=%s for %s "
5357 "(currently_stepping=%d)",
5358 paddress (current_inferior ()->arch (),
5360 t
->ptid
.to_string ().c_str (),
5361 currently_stepping (t
));
5371 stop_all_threads (const char *reason
, inferior
*inf
)
5373 /* We may need multiple passes to discover all threads. */
5377 gdb_assert (exists_non_stop_target ());
5379 INFRUN_SCOPED_DEBUG_START_END ("reason=%s, inf=%d", reason
,
5380 inf
!= nullptr ? inf
->num
: -1);
5382 infrun_debug_show_threads ("non-exited threads",
5383 all_non_exited_threads ());
5385 scoped_restore_current_thread restore_thread
;
5387 /* Enable thread events on relevant targets. */
5388 for (auto *target
: all_non_exited_process_targets ())
5390 if (inf
!= nullptr && inf
->process_target () != target
)
5393 switch_to_target_no_thread (target
);
5394 target_thread_events (true);
5399 /* Disable thread events on relevant targets. */
5400 for (auto *target
: all_non_exited_process_targets ())
5402 if (inf
!= nullptr && inf
->process_target () != target
)
5405 switch_to_target_no_thread (target
);
5406 target_thread_events (false);
5409 /* Use debug_prefixed_printf directly to get a meaningful function
5412 debug_prefixed_printf ("infrun", "stop_all_threads", "done");
5415 /* Request threads to stop, and then wait for the stops. Because
5416 threads we already know about can spawn more threads while we're
5417 trying to stop them, and we only learn about new threads when we
5418 update the thread list, do this in a loop, and keep iterating
5419 until two passes find no threads that need to be stopped. */
5420 for (pass
= 0; pass
< 2; pass
++, iterations
++)
5422 infrun_debug_printf ("pass=%d, iterations=%d", pass
, iterations
);
5425 int waits_needed
= 0;
5427 for (auto *target
: all_non_exited_process_targets ())
5429 if (inf
!= nullptr && inf
->process_target () != target
)
5432 switch_to_target_no_thread (target
);
5433 update_thread_list ();
5436 /* Go through all threads looking for threads that we need
5437 to tell the target to stop. */
5438 for (thread_info
*t
: all_non_exited_threads ())
5440 if (inf
!= nullptr && t
->inf
!= inf
)
5443 /* For a single-target setting with an all-stop target,
5444 we would not even arrive here. For a multi-target
5445 setting, until GDB is able to handle a mixture of
5446 all-stop and non-stop targets, simply skip all-stop
5447 targets' threads. This should be fine due to the
5448 protection of 'check_multi_target_resumption'. */
5450 switch_to_thread_no_regs (t
);
5451 if (!target_is_non_stop_p ())
5454 if (t
->executing ())
5456 /* If already stopping, don't request a stop again.
5457 We just haven't seen the notification yet. */
5458 if (!t
->stop_requested
)
5460 infrun_debug_printf (" %s executing, need stop",
5461 t
->ptid
.to_string ().c_str ());
5462 target_stop (t
->ptid
);
5463 t
->stop_requested
= 1;
5467 infrun_debug_printf (" %s executing, already stopping",
5468 t
->ptid
.to_string ().c_str ());
5471 if (t
->stop_requested
)
5476 infrun_debug_printf (" %s not executing",
5477 t
->ptid
.to_string ().c_str ());
5479 /* The thread may be not executing, but still be
5480 resumed with a pending status to process. */
5481 t
->set_resumed (false);
5485 if (waits_needed
== 0)
5488 /* If we find new threads on the second iteration, restart
5489 over. We want to see two iterations in a row with all
5494 for (int i
= 0; i
< waits_needed
; i
++)
5496 wait_one_event event
= wait_one ();
5497 if (handle_one (event
))
5504 /* Handle a TARGET_WAITKIND_NO_RESUMED event. */
5507 handle_no_resumed (struct execution_control_state
*ecs
)
5509 if (target_can_async_p ())
5511 bool any_sync
= false;
5513 for (ui
*ui
: all_uis ())
5515 if (ui
->prompt_state
== PROMPT_BLOCKED
)
5523 /* There were no unwaited-for children left in the target, but,
5524 we're not synchronously waiting for events either. Just
5527 infrun_debug_printf ("TARGET_WAITKIND_NO_RESUMED (ignoring: bg)");
5528 prepare_to_wait (ecs
);
5533 /* Otherwise, if we were running a synchronous execution command, we
5534 may need to cancel it and give the user back the terminal.
5536 In non-stop mode, the target can't tell whether we've already
5537 consumed previous stop events, so it can end up sending us a
5538 no-resumed event like so:
5540 #0 - thread 1 is left stopped
5542 #1 - thread 2 is resumed and hits breakpoint
5543 -> TARGET_WAITKIND_STOPPED
5545 #2 - thread 3 is resumed and exits
5546 this is the last resumed thread, so
5547 -> TARGET_WAITKIND_NO_RESUMED
5549 #3 - gdb processes stop for thread 2 and decides to re-resume
5552 #4 - gdb processes the TARGET_WAITKIND_NO_RESUMED event.
5553 thread 2 is now resumed, so the event should be ignored.
5555 IOW, if the stop for thread 2 doesn't end a foreground command,
5556 then we need to ignore the following TARGET_WAITKIND_NO_RESUMED
5557 event. But it could be that the event meant that thread 2 itself
5558 (or whatever other thread was the last resumed thread) exited.
5560 To address this we refresh the thread list and check whether we
5561 have resumed threads _now_. In the example above, this removes
5562 thread 3 from the thread list. If thread 2 was re-resumed, we
5563 ignore this event. If we find no thread resumed, then we cancel
5564 the synchronous command and show "no unwaited-for " to the
5567 inferior
*curr_inf
= current_inferior ();
5569 scoped_restore_current_thread restore_thread
;
5570 update_thread_list ();
5574 - the current target has no thread executing, and
5575 - the current inferior is native, and
5576 - the current inferior is the one which has the terminal, and
5579 then a Ctrl-C from this point on would remain stuck in the
5580 kernel, until a thread resumes and dequeues it. That would
5581 result in the GDB CLI not reacting to Ctrl-C, not able to
5582 interrupt the program. To address this, if the current inferior
5583 no longer has any thread executing, we give the terminal to some
5584 other inferior that has at least one thread executing. */
5585 bool swap_terminal
= true;
5587 /* Whether to ignore this TARGET_WAITKIND_NO_RESUMED event, or
5588 whether to report it to the user. */
5589 bool ignore_event
= false;
5591 for (thread_info
*thread
: all_non_exited_threads ())
5593 if (swap_terminal
&& thread
->executing ())
5595 if (thread
->inf
!= curr_inf
)
5597 target_terminal::ours ();
5599 switch_to_thread (thread
);
5600 target_terminal::inferior ();
5602 swap_terminal
= false;
5605 if (!ignore_event
&& thread
->resumed ())
5607 /* Either there were no unwaited-for children left in the
5608 target at some point, but there are now, or some target
5609 other than the eventing one has unwaited-for children
5610 left. Just ignore. */
5611 infrun_debug_printf ("TARGET_WAITKIND_NO_RESUMED "
5612 "(ignoring: found resumed)");
5614 ignore_event
= true;
5617 if (ignore_event
&& !swap_terminal
)
5623 switch_to_inferior_no_thread (curr_inf
);
5624 prepare_to_wait (ecs
);
5628 /* Go ahead and report the event. */
5632 /* Given an execution control state that has been freshly filled in by
5633 an event from the inferior, figure out what it means and take
5636 The alternatives are:
5638 1) stop_waiting and return; to really stop and return to the
5641 2) keep_going and return; to wait for the next event (set
5642 ecs->event_thread->stepping_over_breakpoint to 1 to single step
5646 handle_inferior_event (struct execution_control_state
*ecs
)
5648 /* Make sure that all temporary struct value objects that were
5649 created during the handling of the event get deleted at the
5651 scoped_value_mark free_values
;
5653 infrun_debug_printf ("%s", ecs
->ws
.to_string ().c_str ());
5655 if (ecs
->ws
.kind () == TARGET_WAITKIND_IGNORE
)
5657 /* We had an event in the inferior, but we are not interested in
5658 handling it at this level. The lower layers have already
5659 done what needs to be done, if anything.
5661 One of the possible circumstances for this is when the
5662 inferior produces output for the console. The inferior has
5663 not stopped, and we are ignoring the event. Another possible
5664 circumstance is any event which the lower level knows will be
5665 reported multiple times without an intervening resume. */
5666 prepare_to_wait (ecs
);
5670 if (ecs
->ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
)
5672 prepare_to_wait (ecs
);
5676 if (ecs
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
5677 && handle_no_resumed (ecs
))
5680 /* Cache the last target/ptid/waitstatus. */
5681 set_last_target_status (ecs
->target
, ecs
->ptid
, ecs
->ws
);
5683 /* Always clear state belonging to the previous time we stopped. */
5684 stop_stack_dummy
= STOP_NONE
;
5686 if (ecs
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
5688 /* No unwaited-for children left. IOW, all resumed children
5690 stop_print_frame
= false;
5695 if (ecs
->ws
.kind () != TARGET_WAITKIND_EXITED
5696 && ecs
->ws
.kind () != TARGET_WAITKIND_SIGNALLED
)
5698 ecs
->event_thread
= ecs
->target
->find_thread (ecs
->ptid
);
5699 /* If it's a new thread, add it to the thread database. */
5700 if (ecs
->event_thread
== nullptr)
5701 ecs
->event_thread
= add_thread (ecs
->target
, ecs
->ptid
);
5703 /* Disable range stepping. If the next step request could use a
5704 range, this will be end up re-enabled then. */
5705 ecs
->event_thread
->control
.may_range_step
= 0;
5708 /* Dependent on valid ECS->EVENT_THREAD. */
5709 adjust_pc_after_break (ecs
->event_thread
, ecs
->ws
);
5711 /* Dependent on the current PC value modified by adjust_pc_after_break. */
5712 reinit_frame_cache ();
5714 breakpoint_retire_moribund ();
5716 /* First, distinguish signals caused by the debugger from signals
5717 that have to do with the program's own actions. Note that
5718 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
5719 on the operating system version. Here we detect when a SIGILL or
5720 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
5721 something similar for SIGSEGV, since a SIGSEGV will be generated
5722 when we're trying to execute a breakpoint instruction on a
5723 non-executable stack. This happens for call dummy breakpoints
5724 for architectures like SPARC that place call dummies on the
5726 if (ecs
->ws
.kind () == TARGET_WAITKIND_STOPPED
5727 && (ecs
->ws
.sig () == GDB_SIGNAL_ILL
5728 || ecs
->ws
.sig () == GDB_SIGNAL_SEGV
5729 || ecs
->ws
.sig () == GDB_SIGNAL_EMT
))
5731 struct regcache
*regcache
= get_thread_regcache (ecs
->event_thread
);
5733 if (breakpoint_inserted_here_p (regcache
->aspace (),
5734 regcache_read_pc (regcache
)))
5736 infrun_debug_printf ("Treating signal as SIGTRAP");
5737 ecs
->ws
.set_stopped (GDB_SIGNAL_TRAP
);
5741 mark_non_executing_threads (ecs
->target
, ecs
->ptid
, ecs
->ws
);
5743 switch (ecs
->ws
.kind ())
5745 case TARGET_WAITKIND_LOADED
:
5747 context_switch (ecs
);
5748 /* Ignore gracefully during startup of the inferior, as it might
5749 be the shell which has just loaded some objects, otherwise
5750 add the symbols for the newly loaded objects. Also ignore at
5751 the beginning of an attach or remote session; we will query
5752 the full list of libraries once the connection is
5755 stop_kind stop_soon
= get_inferior_stop_soon (ecs
);
5756 if (stop_soon
== NO_STOP_QUIETLY
)
5758 struct regcache
*regcache
;
5760 regcache
= get_thread_regcache (ecs
->event_thread
);
5762 handle_solib_event ();
5764 ecs
->event_thread
->set_stop_pc (regcache_read_pc (regcache
));
5765 ecs
->event_thread
->control
.stop_bpstat
5766 = bpstat_stop_status_nowatch (regcache
->aspace (),
5767 ecs
->event_thread
->stop_pc (),
5768 ecs
->event_thread
, ecs
->ws
);
5770 if (handle_stop_requested (ecs
))
5773 if (bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
5775 /* A catchpoint triggered. */
5776 process_event_stop_test (ecs
);
5780 /* If requested, stop when the dynamic linker notifies
5781 gdb of events. This allows the user to get control
5782 and place breakpoints in initializer routines for
5783 dynamically loaded objects (among other things). */
5784 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
5785 if (stop_on_solib_events
)
5787 /* Make sure we print "Stopped due to solib-event" in
5789 stop_print_frame
= true;
5796 /* If we are skipping through a shell, or through shared library
5797 loading that we aren't interested in, resume the program. If
5798 we're running the program normally, also resume. */
5799 if (stop_soon
== STOP_QUIETLY
|| stop_soon
== NO_STOP_QUIETLY
)
5801 /* Loading of shared libraries might have changed breakpoint
5802 addresses. Make sure new breakpoints are inserted. */
5803 if (stop_soon
== NO_STOP_QUIETLY
)
5804 insert_breakpoints ();
5805 resume (GDB_SIGNAL_0
);
5806 prepare_to_wait (ecs
);
5810 /* But stop if we're attaching or setting up a remote
5812 if (stop_soon
== STOP_QUIETLY_NO_SIGSTOP
5813 || stop_soon
== STOP_QUIETLY_REMOTE
)
5815 infrun_debug_printf ("quietly stopped");
5820 internal_error (_("unhandled stop_soon: %d"), (int) stop_soon
);
5823 case TARGET_WAITKIND_SPURIOUS
:
5824 if (handle_stop_requested (ecs
))
5826 context_switch (ecs
);
5827 resume (GDB_SIGNAL_0
);
5828 prepare_to_wait (ecs
);
5831 case TARGET_WAITKIND_THREAD_CREATED
:
5832 if (handle_stop_requested (ecs
))
5834 context_switch (ecs
);
5835 if (!switch_back_to_stepped_thread (ecs
))
5839 case TARGET_WAITKIND_EXITED
:
5840 case TARGET_WAITKIND_SIGNALLED
:
5842 /* Depending on the system, ecs->ptid may point to a thread or
5843 to a process. On some targets, target_mourn_inferior may
5844 need to have access to the just-exited thread. That is the
5845 case of GNU/Linux's "checkpoint" support, for example.
5846 Call the switch_to_xxx routine as appropriate. */
5847 thread_info
*thr
= ecs
->target
->find_thread (ecs
->ptid
);
5849 switch_to_thread (thr
);
5852 inferior
*inf
= find_inferior_ptid (ecs
->target
, ecs
->ptid
);
5853 switch_to_inferior_no_thread (inf
);
5856 handle_vfork_child_exec_or_exit (0);
5857 target_terminal::ours (); /* Must do this before mourn anyway. */
5859 /* Clearing any previous state of convenience variables. */
5860 clear_exit_convenience_vars ();
5862 if (ecs
->ws
.kind () == TARGET_WAITKIND_EXITED
)
5864 /* Record the exit code in the convenience variable $_exitcode, so
5865 that the user can inspect this again later. */
5866 set_internalvar_integer (lookup_internalvar ("_exitcode"),
5867 (LONGEST
) ecs
->ws
.exit_status ());
5869 /* Also record this in the inferior itself. */
5870 current_inferior ()->has_exit_code
= true;
5871 current_inferior ()->exit_code
= (LONGEST
) ecs
->ws
.exit_status ();
5873 /* Support the --return-child-result option. */
5874 return_child_result_value
= ecs
->ws
.exit_status ();
5876 interps_notify_exited (ecs
->ws
.exit_status ());
5880 struct gdbarch
*gdbarch
= current_inferior ()->arch ();
5882 if (gdbarch_gdb_signal_to_target_p (gdbarch
))
5884 /* Set the value of the internal variable $_exitsignal,
5885 which holds the signal uncaught by the inferior. */
5886 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
5887 gdbarch_gdb_signal_to_target (gdbarch
,
5892 /* We don't have access to the target's method used for
5893 converting between signal numbers (GDB's internal
5894 representation <-> target's representation).
5895 Therefore, we cannot do a good job at displaying this
5896 information to the user. It's better to just warn
5897 her about it (if infrun debugging is enabled), and
5899 infrun_debug_printf ("Cannot fill $_exitsignal with the correct "
5903 interps_notify_signal_exited (ecs
->ws
.sig ());
5906 gdb_flush (gdb_stdout
);
5907 target_mourn_inferior (inferior_ptid
);
5908 stop_print_frame
= false;
5912 case TARGET_WAITKIND_FORKED
:
5913 case TARGET_WAITKIND_VFORKED
:
5914 case TARGET_WAITKIND_THREAD_CLONED
:
5916 displaced_step_finish (ecs
->event_thread
, ecs
->ws
);
5918 /* Start a new step-over in another thread if there's one that
5922 context_switch (ecs
);
5924 /* Immediately detach breakpoints from the child before there's
5925 any chance of letting the user delete breakpoints from the
5926 breakpoint lists. If we don't do this early, it's easy to
5927 leave left over traps in the child, vis: "break foo; catch
5928 fork; c; <fork>; del; c; <child calls foo>". We only follow
5929 the fork on the last `continue', and by that time the
5930 breakpoint at "foo" is long gone from the breakpoint table.
5931 If we vforked, then we don't need to unpatch here, since both
5932 parent and child are sharing the same memory pages; we'll
5933 need to unpatch at follow/detach time instead to be certain
5934 that new breakpoints added between catchpoint hit time and
5935 vfork follow are detached. */
5936 if (ecs
->ws
.kind () == TARGET_WAITKIND_FORKED
)
5938 /* This won't actually modify the breakpoint list, but will
5939 physically remove the breakpoints from the child. */
5940 detach_breakpoints (ecs
->ws
.child_ptid ());
5943 delete_just_stopped_threads_single_step_breakpoints ();
5945 /* In case the event is caught by a catchpoint, remember that
5946 the event is to be followed at the next resume of the thread,
5947 and not immediately. */
5948 ecs
->event_thread
->pending_follow
= ecs
->ws
;
5950 ecs
->event_thread
->set_stop_pc
5951 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
5953 ecs
->event_thread
->control
.stop_bpstat
5954 = bpstat_stop_status_nowatch (get_current_regcache ()->aspace (),
5955 ecs
->event_thread
->stop_pc (),
5956 ecs
->event_thread
, ecs
->ws
);
5958 if (handle_stop_requested (ecs
))
5961 /* If no catchpoint triggered for this, then keep going. Note
5962 that we're interested in knowing the bpstat actually causes a
5963 stop, not just if it may explain the signal. Software
5964 watchpoints, for example, always appear in the bpstat. */
5965 if (!bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
5968 = (ecs
->ws
.kind () != TARGET_WAITKIND_THREAD_CLONED
5969 && follow_fork_mode_string
== follow_fork_mode_child
);
5971 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
5973 process_stratum_target
*targ
5974 = ecs
->event_thread
->inf
->process_target ();
5977 if (ecs
->ws
.kind () != TARGET_WAITKIND_THREAD_CLONED
)
5978 should_resume
= follow_fork ();
5981 should_resume
= true;
5982 inferior
*inf
= ecs
->event_thread
->inf
;
5983 inf
->top_target ()->follow_clone (ecs
->ws
.child_ptid ());
5984 ecs
->event_thread
->pending_follow
.set_spurious ();
5987 /* Note that one of these may be an invalid pointer,
5988 depending on detach_fork. */
5989 thread_info
*parent
= ecs
->event_thread
;
5990 thread_info
*child
= targ
->find_thread (ecs
->ws
.child_ptid ());
5992 /* At this point, the parent is marked running, and the
5993 child is marked stopped. */
5995 /* If not resuming the parent, mark it stopped. */
5996 if (ecs
->ws
.kind () != TARGET_WAITKIND_THREAD_CLONED
5997 && follow_child
&& !detach_fork
&& !non_stop
&& !sched_multi
)
5998 parent
->set_running (false);
6000 /* If resuming the child, mark it running. */
6001 if (ecs
->ws
.kind () == TARGET_WAITKIND_THREAD_CLONED
6002 || (follow_child
|| (!detach_fork
&& (non_stop
|| sched_multi
))))
6003 child
->set_running (true);
6005 /* In non-stop mode, also resume the other branch. */
6006 if ((ecs
->ws
.kind () == TARGET_WAITKIND_THREAD_CLONED
6007 && target_is_non_stop_p ())
6008 || (!detach_fork
&& (non_stop
6010 && target_is_non_stop_p ()))))
6013 switch_to_thread (parent
);
6015 switch_to_thread (child
);
6017 ecs
->event_thread
= inferior_thread ();
6018 ecs
->ptid
= inferior_ptid
;
6023 switch_to_thread (child
);
6025 switch_to_thread (parent
);
6027 ecs
->event_thread
= inferior_thread ();
6028 ecs
->ptid
= inferior_ptid
;
6032 /* Never call switch_back_to_stepped_thread if we are waiting for
6033 vfork-done (waiting for an external vfork child to exec or
6034 exit). We will resume only the vforking thread for the purpose
6035 of collecting the vfork-done event, and we will restart any
6036 step once the critical shared address space window is done. */
6039 && parent
->inf
->thread_waiting_for_vfork_done
!= nullptr)
6040 || !switch_back_to_stepped_thread (ecs
))
6047 process_event_stop_test (ecs
);
6050 case TARGET_WAITKIND_VFORK_DONE
:
6051 /* Done with the shared memory region. Re-insert breakpoints in
6052 the parent, and keep going. */
6054 context_switch (ecs
);
6056 handle_vfork_done (ecs
->event_thread
);
6057 gdb_assert (inferior_thread () == ecs
->event_thread
);
6059 if (handle_stop_requested (ecs
))
6062 if (!switch_back_to_stepped_thread (ecs
))
6064 gdb_assert (inferior_thread () == ecs
->event_thread
);
6065 /* This also takes care of reinserting breakpoints in the
6066 previously locked inferior. */
6071 case TARGET_WAITKIND_EXECD
:
6073 /* Note we can't read registers yet (the stop_pc), because we
6074 don't yet know the inferior's post-exec architecture.
6075 'stop_pc' is explicitly read below instead. */
6076 switch_to_thread_no_regs (ecs
->event_thread
);
6078 /* Do whatever is necessary to the parent branch of the vfork. */
6079 handle_vfork_child_exec_or_exit (1);
6081 /* This causes the eventpoints and symbol table to be reset.
6082 Must do this now, before trying to determine whether to
6084 follow_exec (inferior_ptid
, ecs
->ws
.execd_pathname ());
6086 /* In follow_exec we may have deleted the original thread and
6087 created a new one. Make sure that the event thread is the
6088 execd thread for that case (this is a nop otherwise). */
6089 ecs
->event_thread
= inferior_thread ();
6091 ecs
->event_thread
->set_stop_pc
6092 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
6094 ecs
->event_thread
->control
.stop_bpstat
6095 = bpstat_stop_status_nowatch (get_current_regcache ()->aspace (),
6096 ecs
->event_thread
->stop_pc (),
6097 ecs
->event_thread
, ecs
->ws
);
6099 if (handle_stop_requested (ecs
))
6102 /* If no catchpoint triggered for this, then keep going. */
6103 if (!bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
6105 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6109 process_event_stop_test (ecs
);
6112 /* Be careful not to try to gather much state about a thread
6113 that's in a syscall. It's frequently a losing proposition. */
6114 case TARGET_WAITKIND_SYSCALL_ENTRY
:
6115 /* Getting the current syscall number. */
6116 if (handle_syscall_event (ecs
) == 0)
6117 process_event_stop_test (ecs
);
6120 /* Before examining the threads further, step this thread to
6121 get it entirely out of the syscall. (We get notice of the
6122 event when the thread is just on the verge of exiting a
6123 syscall. Stepping one instruction seems to get it back
6125 case TARGET_WAITKIND_SYSCALL_RETURN
:
6126 if (handle_syscall_event (ecs
) == 0)
6127 process_event_stop_test (ecs
);
6130 case TARGET_WAITKIND_STOPPED
:
6131 handle_signal_stop (ecs
);
6134 case TARGET_WAITKIND_NO_HISTORY
:
6135 /* Reverse execution: target ran out of history info. */
6137 /* Switch to the stopped thread. */
6138 context_switch (ecs
);
6139 infrun_debug_printf ("stopped");
6141 delete_just_stopped_threads_single_step_breakpoints ();
6142 ecs
->event_thread
->set_stop_pc
6143 (regcache_read_pc (get_thread_regcache (inferior_thread ())));
6145 if (handle_stop_requested (ecs
))
6148 interps_notify_no_history ();
6154 /* Restart threads back to what they were trying to do back when we
6155 paused them (because of an in-line step-over or vfork, for example).
6156 The EVENT_THREAD thread is ignored (not restarted).
6158 If INF is non-nullptr, only resume threads from INF. */
6161 restart_threads (struct thread_info
*event_thread
, inferior
*inf
)
6163 INFRUN_SCOPED_DEBUG_START_END ("event_thread=%s, inf=%d",
6164 event_thread
->ptid
.to_string ().c_str (),
6165 inf
!= nullptr ? inf
->num
: -1);
6167 gdb_assert (!step_over_info_valid_p ());
6169 /* In case the instruction just stepped spawned a new thread. */
6170 update_thread_list ();
6172 for (thread_info
*tp
: all_non_exited_threads ())
6174 if (inf
!= nullptr && tp
->inf
!= inf
)
6177 if (tp
->inf
->detaching
)
6179 infrun_debug_printf ("restart threads: [%s] inferior detaching",
6180 tp
->ptid
.to_string ().c_str ());
6184 switch_to_thread_no_regs (tp
);
6186 if (tp
== event_thread
)
6188 infrun_debug_printf ("restart threads: [%s] is event thread",
6189 tp
->ptid
.to_string ().c_str ());
6193 if (!(tp
->state
== THREAD_RUNNING
|| tp
->control
.in_infcall
))
6195 infrun_debug_printf ("restart threads: [%s] not meant to be running",
6196 tp
->ptid
.to_string ().c_str ());
6202 infrun_debug_printf ("restart threads: [%s] resumed",
6203 tp
->ptid
.to_string ().c_str ());
6204 gdb_assert (tp
->executing () || tp
->has_pending_waitstatus ());
6208 if (thread_is_in_step_over_chain (tp
))
6210 infrun_debug_printf ("restart threads: [%s] needs step-over",
6211 tp
->ptid
.to_string ().c_str ());
6212 gdb_assert (!tp
->resumed ());
6217 if (tp
->has_pending_waitstatus ())
6219 infrun_debug_printf ("restart threads: [%s] has pending status",
6220 tp
->ptid
.to_string ().c_str ());
6221 tp
->set_resumed (true);
6225 gdb_assert (!tp
->stop_requested
);
6227 /* If some thread needs to start a step-over at this point, it
6228 should still be in the step-over queue, and thus skipped
6230 if (thread_still_needs_step_over (tp
))
6232 internal_error ("thread [%s] needs a step-over, but not in "
6233 "step-over queue\n",
6234 tp
->ptid
.to_string ().c_str ());
6237 if (currently_stepping (tp
))
6239 infrun_debug_printf ("restart threads: [%s] was stepping",
6240 tp
->ptid
.to_string ().c_str ());
6241 keep_going_stepped_thread (tp
);
6245 infrun_debug_printf ("restart threads: [%s] continuing",
6246 tp
->ptid
.to_string ().c_str ());
6247 execution_control_state
ecs (tp
);
6248 switch_to_thread (tp
);
6249 keep_going_pass_signal (&ecs
);
6254 /* Callback for iterate_over_threads. Find a resumed thread that has
6255 a pending waitstatus. */
6258 resumed_thread_with_pending_status (struct thread_info
*tp
,
6261 return tp
->resumed () && tp
->has_pending_waitstatus ();
6264 /* Called when we get an event that may finish an in-line or
6265 out-of-line (displaced stepping) step-over started previously.
6266 Return true if the event is processed and we should go back to the
6267 event loop; false if the caller should continue processing the
6271 finish_step_over (struct execution_control_state
*ecs
)
6273 displaced_step_finish (ecs
->event_thread
, ecs
->ws
);
6275 bool had_step_over_info
= step_over_info_valid_p ();
6277 if (had_step_over_info
)
6279 /* If we're stepping over a breakpoint with all threads locked,
6280 then only the thread that was stepped should be reporting
6282 gdb_assert (ecs
->event_thread
->control
.trap_expected
);
6284 clear_step_over_info ();
6287 if (!target_is_non_stop_p ())
6290 /* Start a new step-over in another thread if there's one that
6294 /* If we were stepping over a breakpoint before, and haven't started
6295 a new in-line step-over sequence, then restart all other threads
6296 (except the event thread). We can't do this in all-stop, as then
6297 e.g., we wouldn't be able to issue any other remote packet until
6298 these other threads stop. */
6299 if (had_step_over_info
&& !step_over_info_valid_p ())
6301 struct thread_info
*pending
;
6303 /* If we only have threads with pending statuses, the restart
6304 below won't restart any thread and so nothing re-inserts the
6305 breakpoint we just stepped over. But we need it inserted
6306 when we later process the pending events, otherwise if
6307 another thread has a pending event for this breakpoint too,
6308 we'd discard its event (because the breakpoint that
6309 originally caused the event was no longer inserted). */
6310 context_switch (ecs
);
6311 insert_breakpoints ();
6313 restart_threads (ecs
->event_thread
);
6315 /* If we have events pending, go through handle_inferior_event
6316 again, picking up a pending event at random. This avoids
6317 thread starvation. */
6319 /* But not if we just stepped over a watchpoint in order to let
6320 the instruction execute so we can evaluate its expression.
6321 The set of watchpoints that triggered is recorded in the
6322 breakpoint objects themselves (see bp->watchpoint_triggered).
6323 If we processed another event first, that other event could
6324 clobber this info. */
6325 if (ecs
->event_thread
->stepping_over_watchpoint
)
6328 pending
= iterate_over_threads (resumed_thread_with_pending_status
,
6330 if (pending
!= nullptr)
6332 struct thread_info
*tp
= ecs
->event_thread
;
6333 struct regcache
*regcache
;
6335 infrun_debug_printf ("found resumed threads with "
6336 "pending events, saving status");
6338 gdb_assert (pending
!= tp
);
6340 /* Record the event thread's event for later. */
6341 save_waitstatus (tp
, ecs
->ws
);
6342 /* This was cleared early, by handle_inferior_event. Set it
6343 so this pending event is considered by
6345 tp
->set_resumed (true);
6347 gdb_assert (!tp
->executing ());
6349 regcache
= get_thread_regcache (tp
);
6350 tp
->set_stop_pc (regcache_read_pc (regcache
));
6352 infrun_debug_printf ("saved stop_pc=%s for %s "
6353 "(currently_stepping=%d)",
6354 paddress (current_inferior ()->arch (),
6356 tp
->ptid
.to_string ().c_str (),
6357 currently_stepping (tp
));
6359 /* This in-line step-over finished; clear this so we won't
6360 start a new one. This is what handle_signal_stop would
6361 do, if we returned false. */
6362 tp
->stepping_over_breakpoint
= 0;
6364 /* Wake up the event loop again. */
6365 mark_async_event_handler (infrun_async_inferior_event_token
);
6367 prepare_to_wait (ecs
);
6378 notify_signal_received (gdb_signal sig
)
6380 interps_notify_signal_received (sig
);
6381 gdb::observers::signal_received
.notify (sig
);
6387 notify_normal_stop (bpstat
*bs
, int print_frame
)
6389 interps_notify_normal_stop (bs
, print_frame
);
6390 gdb::observers::normal_stop
.notify (bs
, print_frame
);
6395 void notify_user_selected_context_changed (user_selected_what selection
)
6397 interps_notify_user_selected_context_changed (selection
);
6398 gdb::observers::user_selected_context_changed
.notify (selection
);
6401 /* Come here when the program has stopped with a signal. */
6404 handle_signal_stop (struct execution_control_state
*ecs
)
6406 frame_info_ptr frame
;
6407 struct gdbarch
*gdbarch
;
6408 int stopped_by_watchpoint
;
6409 enum stop_kind stop_soon
;
6412 gdb_assert (ecs
->ws
.kind () == TARGET_WAITKIND_STOPPED
);
6414 ecs
->event_thread
->set_stop_signal (ecs
->ws
.sig ());
6416 /* Do we need to clean up the state of a thread that has
6417 completed a displaced single-step? (Doing so usually affects
6418 the PC, so do it here, before we set stop_pc.) */
6419 if (finish_step_over (ecs
))
6422 /* If we either finished a single-step or hit a breakpoint, but
6423 the user wanted this thread to be stopped, pretend we got a
6424 SIG0 (generic unsignaled stop). */
6425 if (ecs
->event_thread
->stop_requested
6426 && ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
)
6427 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6429 ecs
->event_thread
->set_stop_pc
6430 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
6432 context_switch (ecs
);
6434 if (deprecated_context_hook
)
6435 deprecated_context_hook (ecs
->event_thread
->global_num
);
6439 struct regcache
*regcache
= get_thread_regcache (ecs
->event_thread
);
6440 struct gdbarch
*reg_gdbarch
= regcache
->arch ();
6443 ("stop_pc=%s", paddress (reg_gdbarch
, ecs
->event_thread
->stop_pc ()));
6444 if (target_stopped_by_watchpoint ())
6448 infrun_debug_printf ("stopped by watchpoint");
6450 if (target_stopped_data_address (current_inferior ()->top_target (),
6452 infrun_debug_printf ("stopped data address=%s",
6453 paddress (reg_gdbarch
, addr
));
6455 infrun_debug_printf ("(no data address available)");
6459 /* This is originated from start_remote(), start_inferior() and
6460 shared libraries hook functions. */
6461 stop_soon
= get_inferior_stop_soon (ecs
);
6462 if (stop_soon
== STOP_QUIETLY
|| stop_soon
== STOP_QUIETLY_REMOTE
)
6464 infrun_debug_printf ("quietly stopped");
6465 stop_print_frame
= true;
6470 /* This originates from attach_command(). We need to overwrite
6471 the stop_signal here, because some kernels don't ignore a
6472 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
6473 See more comments in inferior.h. On the other hand, if we
6474 get a non-SIGSTOP, report it to the user - assume the backend
6475 will handle the SIGSTOP if it should show up later.
6477 Also consider that the attach is complete when we see a
6478 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
6479 target extended-remote report it instead of a SIGSTOP
6480 (e.g. gdbserver). We already rely on SIGTRAP being our
6481 signal, so this is no exception.
6483 Also consider that the attach is complete when we see a
6484 GDB_SIGNAL_0. In non-stop mode, GDB will explicitly tell
6485 the target to stop all threads of the inferior, in case the
6486 low level attach operation doesn't stop them implicitly. If
6487 they weren't stopped implicitly, then the stub will report a
6488 GDB_SIGNAL_0, meaning: stopped for no particular reason
6489 other than GDB's request. */
6490 if (stop_soon
== STOP_QUIETLY_NO_SIGSTOP
6491 && (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_STOP
6492 || ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6493 || ecs
->event_thread
->stop_signal () == GDB_SIGNAL_0
))
6495 stop_print_frame
= true;
6497 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6501 /* At this point, get hold of the now-current thread's frame. */
6502 frame
= get_current_frame ();
6503 gdbarch
= get_frame_arch (frame
);
6505 /* Pull the single step breakpoints out of the target. */
6506 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
)
6508 struct regcache
*regcache
;
6511 regcache
= get_thread_regcache (ecs
->event_thread
);
6512 const address_space
*aspace
= regcache
->aspace ();
6514 pc
= regcache_read_pc (regcache
);
6516 /* However, before doing so, if this single-step breakpoint was
6517 actually for another thread, set this thread up for moving
6519 if (!thread_has_single_step_breakpoint_here (ecs
->event_thread
,
6522 if (single_step_breakpoint_inserted_here_p (aspace
, pc
))
6524 infrun_debug_printf ("[%s] hit another thread's single-step "
6526 ecs
->ptid
.to_string ().c_str ());
6527 ecs
->hit_singlestep_breakpoint
= 1;
6532 infrun_debug_printf ("[%s] hit its single-step breakpoint",
6533 ecs
->ptid
.to_string ().c_str ());
6536 delete_just_stopped_threads_single_step_breakpoints ();
6538 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6539 && ecs
->event_thread
->control
.trap_expected
6540 && ecs
->event_thread
->stepping_over_watchpoint
)
6541 stopped_by_watchpoint
= 0;
6543 stopped_by_watchpoint
= watchpoints_triggered (ecs
->ws
);
6545 /* If necessary, step over this watchpoint. We'll be back to display
6547 if (stopped_by_watchpoint
6548 && (target_have_steppable_watchpoint ()
6549 || gdbarch_have_nonsteppable_watchpoint (gdbarch
)))
6551 /* At this point, we are stopped at an instruction which has
6552 attempted to write to a piece of memory under control of
6553 a watchpoint. The instruction hasn't actually executed
6554 yet. If we were to evaluate the watchpoint expression
6555 now, we would get the old value, and therefore no change
6556 would seem to have occurred.
6558 In order to make watchpoints work `right', we really need
6559 to complete the memory write, and then evaluate the
6560 watchpoint expression. We do this by single-stepping the
6563 It may not be necessary to disable the watchpoint to step over
6564 it. For example, the PA can (with some kernel cooperation)
6565 single step over a watchpoint without disabling the watchpoint.
6567 It is far more common to need to disable a watchpoint to step
6568 the inferior over it. If we have non-steppable watchpoints,
6569 we must disable the current watchpoint; it's simplest to
6570 disable all watchpoints.
6572 Any breakpoint at PC must also be stepped over -- if there's
6573 one, it will have already triggered before the watchpoint
6574 triggered, and we either already reported it to the user, or
6575 it didn't cause a stop and we called keep_going. In either
6576 case, if there was a breakpoint at PC, we must be trying to
6578 ecs
->event_thread
->stepping_over_watchpoint
= 1;
6583 ecs
->event_thread
->stepping_over_breakpoint
= 0;
6584 ecs
->event_thread
->stepping_over_watchpoint
= 0;
6585 bpstat_clear (&ecs
->event_thread
->control
.stop_bpstat
);
6586 ecs
->event_thread
->control
.stop_step
= 0;
6587 stop_print_frame
= true;
6588 stopped_by_random_signal
= 0;
6589 bpstat
*stop_chain
= nullptr;
6591 /* Hide inlined functions starting here, unless we just performed stepi or
6592 nexti. After stepi and nexti, always show the innermost frame (not any
6593 inline function call sites). */
6594 if (ecs
->event_thread
->control
.step_range_end
!= 1)
6596 const address_space
*aspace
6597 = get_thread_regcache (ecs
->event_thread
)->aspace ();
6599 /* skip_inline_frames is expensive, so we avoid it if we can
6600 determine that the address is one where functions cannot have
6601 been inlined. This improves performance with inferiors that
6602 load a lot of shared libraries, because the solib event
6603 breakpoint is defined as the address of a function (i.e. not
6604 inline). Note that we have to check the previous PC as well
6605 as the current one to catch cases when we have just
6606 single-stepped off a breakpoint prior to reinstating it.
6607 Note that we're assuming that the code we single-step to is
6608 not inline, but that's not definitive: there's nothing
6609 preventing the event breakpoint function from containing
6610 inlined code, and the single-step ending up there. If the
6611 user had set a breakpoint on that inlined code, the missing
6612 skip_inline_frames call would break things. Fortunately
6613 that's an extremely unlikely scenario. */
6614 if (!pc_at_non_inline_function (aspace
,
6615 ecs
->event_thread
->stop_pc (),
6617 && !(ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6618 && ecs
->event_thread
->control
.trap_expected
6619 && pc_at_non_inline_function (aspace
,
6620 ecs
->event_thread
->prev_pc
,
6623 stop_chain
= build_bpstat_chain (aspace
,
6624 ecs
->event_thread
->stop_pc (),
6626 skip_inline_frames (ecs
->event_thread
, stop_chain
);
6628 /* Re-fetch current thread's frame in case that invalidated
6630 frame
= get_current_frame ();
6631 gdbarch
= get_frame_arch (frame
);
6635 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6636 && ecs
->event_thread
->control
.trap_expected
6637 && gdbarch_single_step_through_delay_p (gdbarch
)
6638 && currently_stepping (ecs
->event_thread
))
6640 /* We're trying to step off a breakpoint. Turns out that we're
6641 also on an instruction that needs to be stepped multiple
6642 times before it's been fully executing. E.g., architectures
6643 with a delay slot. It needs to be stepped twice, once for
6644 the instruction and once for the delay slot. */
6645 int step_through_delay
6646 = gdbarch_single_step_through_delay (gdbarch
, frame
);
6648 if (step_through_delay
)
6649 infrun_debug_printf ("step through delay");
6651 if (ecs
->event_thread
->control
.step_range_end
== 0
6652 && step_through_delay
)
6654 /* The user issued a continue when stopped at a breakpoint.
6655 Set up for another trap and get out of here. */
6656 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6660 else if (step_through_delay
)
6662 /* The user issued a step when stopped at a breakpoint.
6663 Maybe we should stop, maybe we should not - the delay
6664 slot *might* correspond to a line of source. In any
6665 case, don't decide that here, just set
6666 ecs->stepping_over_breakpoint, making sure we
6667 single-step again before breakpoints are re-inserted. */
6668 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6672 /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
6673 handles this event. */
6674 ecs
->event_thread
->control
.stop_bpstat
6675 = bpstat_stop_status (get_current_regcache ()->aspace (),
6676 ecs
->event_thread
->stop_pc (),
6677 ecs
->event_thread
, ecs
->ws
, stop_chain
);
6679 /* Following in case break condition called a
6681 stop_print_frame
= true;
6683 /* This is where we handle "moribund" watchpoints. Unlike
6684 software breakpoints traps, hardware watchpoint traps are
6685 always distinguishable from random traps. If no high-level
6686 watchpoint is associated with the reported stop data address
6687 anymore, then the bpstat does not explain the signal ---
6688 simply make sure to ignore it if `stopped_by_watchpoint' is
6691 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6692 && !bpstat_explains_signal (ecs
->event_thread
->control
.stop_bpstat
,
6694 && stopped_by_watchpoint
)
6696 infrun_debug_printf ("no user watchpoint explains watchpoint SIGTRAP, "
6700 /* NOTE: cagney/2003-03-29: These checks for a random signal
6701 at one stage in the past included checks for an inferior
6702 function call's call dummy's return breakpoint. The original
6703 comment, that went with the test, read:
6705 ``End of a stack dummy. Some systems (e.g. Sony news) give
6706 another signal besides SIGTRAP, so check here as well as
6709 If someone ever tries to get call dummys on a
6710 non-executable stack to work (where the target would stop
6711 with something like a SIGSEGV), then those tests might need
6712 to be re-instated. Given, however, that the tests were only
6713 enabled when momentary breakpoints were not being used, I
6714 suspect that it won't be the case.
6716 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
6717 be necessary for call dummies on a non-executable stack on
6720 /* See if the breakpoints module can explain the signal. */
6722 = !bpstat_explains_signal (ecs
->event_thread
->control
.stop_bpstat
,
6723 ecs
->event_thread
->stop_signal ());
6725 /* Maybe this was a trap for a software breakpoint that has since
6727 if (random_signal
&& target_stopped_by_sw_breakpoint ())
6729 if (gdbarch_program_breakpoint_here_p (gdbarch
,
6730 ecs
->event_thread
->stop_pc ()))
6732 struct regcache
*regcache
;
6735 /* Re-adjust PC to what the program would see if GDB was not
6737 regcache
= get_thread_regcache (ecs
->event_thread
);
6738 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
6741 gdb::optional
<scoped_restore_tmpl
<int>>
6742 restore_operation_disable
;
6744 if (record_full_is_used ())
6745 restore_operation_disable
.emplace
6746 (record_full_gdb_operation_disable_set ());
6748 regcache_write_pc (regcache
,
6749 ecs
->event_thread
->stop_pc () + decr_pc
);
6754 /* A delayed software breakpoint event. Ignore the trap. */
6755 infrun_debug_printf ("delayed software breakpoint trap, ignoring");
6760 /* Maybe this was a trap for a hardware breakpoint/watchpoint that
6761 has since been removed. */
6762 if (random_signal
&& target_stopped_by_hw_breakpoint ())
6764 /* A delayed hardware breakpoint event. Ignore the trap. */
6765 infrun_debug_printf ("delayed hardware breakpoint/watchpoint "
6770 /* If not, perhaps stepping/nexting can. */
6772 random_signal
= !(ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6773 && currently_stepping (ecs
->event_thread
));
6775 /* Perhaps the thread hit a single-step breakpoint of _another_
6776 thread. Single-step breakpoints are transparent to the
6777 breakpoints module. */
6779 random_signal
= !ecs
->hit_singlestep_breakpoint
;
6781 /* No? Perhaps we got a moribund watchpoint. */
6783 random_signal
= !stopped_by_watchpoint
;
6785 /* Always stop if the user explicitly requested this thread to
6787 if (ecs
->event_thread
->stop_requested
)
6790 infrun_debug_printf ("user-requested stop");
6793 /* For the program's own signals, act according to
6794 the signal handling tables. */
6798 /* Signal not for debugging purposes. */
6799 enum gdb_signal stop_signal
= ecs
->event_thread
->stop_signal ();
6801 infrun_debug_printf ("random signal (%s)",
6802 gdb_signal_to_symbol_string (stop_signal
));
6804 stopped_by_random_signal
= 1;
6806 /* Always stop on signals if we're either just gaining control
6807 of the program, or the user explicitly requested this thread
6808 to remain stopped. */
6809 if (stop_soon
!= NO_STOP_QUIETLY
6810 || ecs
->event_thread
->stop_requested
6811 || signal_stop_state (ecs
->event_thread
->stop_signal ()))
6817 /* Notify observers the signal has "handle print" set. Note we
6818 returned early above if stopping; normal_stop handles the
6819 printing in that case. */
6820 if (signal_print
[ecs
->event_thread
->stop_signal ()])
6822 /* The signal table tells us to print about this signal. */
6823 target_terminal::ours_for_output ();
6824 notify_signal_received (ecs
->event_thread
->stop_signal ());
6825 target_terminal::inferior ();
6828 /* Clear the signal if it should not be passed. */
6829 if (signal_program
[ecs
->event_thread
->stop_signal ()] == 0)
6830 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6832 if (ecs
->event_thread
->prev_pc
== ecs
->event_thread
->stop_pc ()
6833 && ecs
->event_thread
->control
.trap_expected
6834 && ecs
->event_thread
->control
.step_resume_breakpoint
== nullptr)
6836 /* We were just starting a new sequence, attempting to
6837 single-step off of a breakpoint and expecting a SIGTRAP.
6838 Instead this signal arrives. This signal will take us out
6839 of the stepping range so GDB needs to remember to, when
6840 the signal handler returns, resume stepping off that
6842 /* To simplify things, "continue" is forced to use the same
6843 code paths as single-step - set a breakpoint at the
6844 signal return address and then, once hit, step off that
6846 infrun_debug_printf ("signal arrived while stepping over breakpoint");
6848 insert_hp_step_resume_breakpoint_at_frame (frame
);
6849 ecs
->event_thread
->step_after_step_resume_breakpoint
= 1;
6850 /* Reset trap_expected to ensure breakpoints are re-inserted. */
6851 ecs
->event_thread
->control
.trap_expected
= 0;
6853 /* If we were nexting/stepping some other thread, switch to
6854 it, so that we don't continue it, losing control. */
6855 if (!switch_back_to_stepped_thread (ecs
))
6860 if (ecs
->event_thread
->stop_signal () != GDB_SIGNAL_0
6861 && (pc_in_thread_step_range (ecs
->event_thread
->stop_pc (),
6863 || ecs
->event_thread
->control
.step_range_end
== 1)
6864 && (get_stack_frame_id (frame
)
6865 == ecs
->event_thread
->control
.step_stack_frame_id
)
6866 && ecs
->event_thread
->control
.step_resume_breakpoint
== nullptr)
6868 /* The inferior is about to take a signal that will take it
6869 out of the single step range. Set a breakpoint at the
6870 current PC (which is presumably where the signal handler
6871 will eventually return) and then allow the inferior to
6874 Note that this is only needed for a signal delivered
6875 while in the single-step range. Nested signals aren't a
6876 problem as they eventually all return. */
6877 infrun_debug_printf ("signal may take us out of single-step range");
6879 clear_step_over_info ();
6880 insert_hp_step_resume_breakpoint_at_frame (frame
);
6881 ecs
->event_thread
->step_after_step_resume_breakpoint
= 1;
6882 /* Reset trap_expected to ensure breakpoints are re-inserted. */
6883 ecs
->event_thread
->control
.trap_expected
= 0;
6888 /* Note: step_resume_breakpoint may be non-NULL. This occurs
6889 when either there's a nested signal, or when there's a
6890 pending signal enabled just as the signal handler returns
6891 (leaving the inferior at the step-resume-breakpoint without
6892 actually executing it). Either way continue until the
6893 breakpoint is really hit. */
6895 if (!switch_back_to_stepped_thread (ecs
))
6897 infrun_debug_printf ("random signal, keep going");
6904 process_event_stop_test (ecs
);
6907 /* Come here when we've got some debug event / signal we can explain
6908 (IOW, not a random signal), and test whether it should cause a
6909 stop, or whether we should resume the inferior (transparently).
6910 E.g., could be a breakpoint whose condition evaluates false; we
6911 could be still stepping within the line; etc. */
6914 process_event_stop_test (struct execution_control_state
*ecs
)
6916 struct symtab_and_line stop_pc_sal
;
6917 frame_info_ptr frame
;
6918 struct gdbarch
*gdbarch
;
6919 CORE_ADDR jmp_buf_pc
;
6920 struct bpstat_what what
;
6922 /* Handle cases caused by hitting a breakpoint. */
6924 frame
= get_current_frame ();
6925 gdbarch
= get_frame_arch (frame
);
6927 what
= bpstat_what (ecs
->event_thread
->control
.stop_bpstat
);
6929 if (what
.call_dummy
)
6931 stop_stack_dummy
= what
.call_dummy
;
6934 /* A few breakpoint types have callbacks associated (e.g.,
6935 bp_jit_event). Run them now. */
6936 bpstat_run_callbacks (ecs
->event_thread
->control
.stop_bpstat
);
6938 /* If we hit an internal event that triggers symbol changes, the
6939 current frame will be invalidated within bpstat_what (e.g., if we
6940 hit an internal solib event). Re-fetch it. */
6941 frame
= get_current_frame ();
6942 gdbarch
= get_frame_arch (frame
);
6944 switch (what
.main_action
)
6946 case BPSTAT_WHAT_SET_LONGJMP_RESUME
:
6947 /* If we hit the breakpoint at longjmp while stepping, we
6948 install a momentary breakpoint at the target of the
6951 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME");
6953 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6955 if (what
.is_longjmp
)
6957 struct value
*arg_value
;
6959 /* If we set the longjmp breakpoint via a SystemTap probe,
6960 then use it to extract the arguments. The destination PC
6961 is the third argument to the probe. */
6962 arg_value
= probe_safe_evaluate_at_pc (frame
, 2);
6965 jmp_buf_pc
= value_as_address (arg_value
);
6966 jmp_buf_pc
= gdbarch_addr_bits_remove (gdbarch
, jmp_buf_pc
);
6968 else if (!gdbarch_get_longjmp_target_p (gdbarch
)
6969 || !gdbarch_get_longjmp_target (gdbarch
,
6970 frame
, &jmp_buf_pc
))
6972 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME "
6973 "(!gdbarch_get_longjmp_target)");
6978 /* Insert a breakpoint at resume address. */
6979 insert_longjmp_resume_breakpoint (gdbarch
, jmp_buf_pc
);
6982 check_exception_resume (ecs
, frame
);
6986 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
:
6988 frame_info_ptr init_frame
;
6990 /* There are several cases to consider.
6992 1. The initiating frame no longer exists. In this case we
6993 must stop, because the exception or longjmp has gone too
6996 2. The initiating frame exists, and is the same as the
6997 current frame. We stop, because the exception or longjmp
7000 3. The initiating frame exists and is different from the
7001 current frame. This means the exception or longjmp has
7002 been caught beneath the initiating frame, so keep going.
7004 4. longjmp breakpoint has been placed just to protect
7005 against stale dummy frames and user is not interested in
7006 stopping around longjmps. */
7008 infrun_debug_printf ("BPSTAT_WHAT_CLEAR_LONGJMP_RESUME");
7010 gdb_assert (ecs
->event_thread
->control
.exception_resume_breakpoint
7012 delete_exception_resume_breakpoint (ecs
->event_thread
);
7014 if (what
.is_longjmp
)
7016 check_longjmp_breakpoint_for_call_dummy (ecs
->event_thread
);
7018 if (!frame_id_p (ecs
->event_thread
->initiating_frame
))
7026 init_frame
= frame_find_by_id (ecs
->event_thread
->initiating_frame
);
7030 struct frame_id current_id
7031 = get_frame_id (get_current_frame ());
7032 if (current_id
== ecs
->event_thread
->initiating_frame
)
7034 /* Case 2. Fall through. */
7044 /* For Cases 1 and 2, remove the step-resume breakpoint, if it
7046 delete_step_resume_breakpoint (ecs
->event_thread
);
7048 end_stepping_range (ecs
);
7052 case BPSTAT_WHAT_SINGLE
:
7053 infrun_debug_printf ("BPSTAT_WHAT_SINGLE");
7054 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7055 /* Still need to check other stuff, at least the case where we
7056 are stepping and step out of the right range. */
7059 case BPSTAT_WHAT_STEP_RESUME
:
7060 infrun_debug_printf ("BPSTAT_WHAT_STEP_RESUME");
7062 delete_step_resume_breakpoint (ecs
->event_thread
);
7063 if (ecs
->event_thread
->control
.proceed_to_finish
7064 && execution_direction
== EXEC_REVERSE
)
7066 struct thread_info
*tp
= ecs
->event_thread
;
7068 /* We are finishing a function in reverse, and just hit the
7069 step-resume breakpoint at the start address of the
7070 function, and we're almost there -- just need to back up
7071 by one more single-step, which should take us back to the
7073 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
7077 fill_in_stop_func (gdbarch
, ecs
);
7078 if (ecs
->event_thread
->stop_pc () == ecs
->stop_func_start
7079 && execution_direction
== EXEC_REVERSE
)
7081 /* We are stepping over a function call in reverse, and just
7082 hit the step-resume breakpoint at the start address of
7083 the function. Go back to single-stepping, which should
7084 take us back to the function call. */
7085 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7091 case BPSTAT_WHAT_STOP_NOISY
:
7092 infrun_debug_printf ("BPSTAT_WHAT_STOP_NOISY");
7093 stop_print_frame
= true;
7095 /* Assume the thread stopped for a breakpoint. We'll still check
7096 whether a/the breakpoint is there when the thread is next
7098 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7103 case BPSTAT_WHAT_STOP_SILENT
:
7104 infrun_debug_printf ("BPSTAT_WHAT_STOP_SILENT");
7105 stop_print_frame
= false;
7107 /* Assume the thread stopped for a breakpoint. We'll still check
7108 whether a/the breakpoint is there when the thread is next
7110 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7114 case BPSTAT_WHAT_HP_STEP_RESUME
:
7115 infrun_debug_printf ("BPSTAT_WHAT_HP_STEP_RESUME");
7117 delete_step_resume_breakpoint (ecs
->event_thread
);
7118 if (ecs
->event_thread
->step_after_step_resume_breakpoint
)
7120 /* Back when the step-resume breakpoint was inserted, we
7121 were trying to single-step off a breakpoint. Go back to
7123 ecs
->event_thread
->step_after_step_resume_breakpoint
= 0;
7124 ecs
->event_thread
->stepping_over_breakpoint
= 1;
7130 case BPSTAT_WHAT_KEEP_CHECKING
:
7134 /* If we stepped a permanent breakpoint and we had a high priority
7135 step-resume breakpoint for the address we stepped, but we didn't
7136 hit it, then we must have stepped into the signal handler. The
7137 step-resume was only necessary to catch the case of _not_
7138 stepping into the handler, so delete it, and fall through to
7139 checking whether the step finished. */
7140 if (ecs
->event_thread
->stepped_breakpoint
)
7142 struct breakpoint
*sr_bp
7143 = ecs
->event_thread
->control
.step_resume_breakpoint
;
7145 if (sr_bp
!= nullptr
7146 && sr_bp
->first_loc ().permanent
7147 && sr_bp
->type
== bp_hp_step_resume
7148 && sr_bp
->first_loc ().address
== ecs
->event_thread
->prev_pc
)
7150 infrun_debug_printf ("stepped permanent breakpoint, stopped in handler");
7151 delete_step_resume_breakpoint (ecs
->event_thread
);
7152 ecs
->event_thread
->step_after_step_resume_breakpoint
= 0;
7156 /* We come here if we hit a breakpoint but should not stop for it.
7157 Possibly we also were stepping and should stop for that. So fall
7158 through and test for stepping. But, if not stepping, do not
7161 /* In all-stop mode, if we're currently stepping but have stopped in
7162 some other thread, we need to switch back to the stepped thread. */
7163 if (switch_back_to_stepped_thread (ecs
))
7166 if (ecs
->event_thread
->control
.step_resume_breakpoint
)
7168 infrun_debug_printf ("step-resume breakpoint is inserted");
7170 /* Having a step-resume breakpoint overrides anything
7171 else having to do with stepping commands until
7172 that breakpoint is reached. */
7177 if (ecs
->event_thread
->control
.step_range_end
== 0)
7179 infrun_debug_printf ("no stepping, continue");
7180 /* Likewise if we aren't even stepping. */
7185 /* Re-fetch current thread's frame in case the code above caused
7186 the frame cache to be re-initialized, making our FRAME variable
7187 a dangling pointer. */
7188 frame
= get_current_frame ();
7189 gdbarch
= get_frame_arch (frame
);
7190 fill_in_stop_func (gdbarch
, ecs
);
7192 /* If stepping through a line, keep going if still within it.
7194 Note that step_range_end is the address of the first instruction
7195 beyond the step range, and NOT the address of the last instruction
7198 Note also that during reverse execution, we may be stepping
7199 through a function epilogue and therefore must detect when
7200 the current-frame changes in the middle of a line. */
7202 if (pc_in_thread_step_range (ecs
->event_thread
->stop_pc (),
7204 && (execution_direction
!= EXEC_REVERSE
7205 || get_frame_id (frame
) == ecs
->event_thread
->control
.step_frame_id
))
7208 ("stepping inside range [%s-%s]",
7209 paddress (gdbarch
, ecs
->event_thread
->control
.step_range_start
),
7210 paddress (gdbarch
, ecs
->event_thread
->control
.step_range_end
));
7212 /* Tentatively re-enable range stepping; `resume' disables it if
7213 necessary (e.g., if we're stepping over a breakpoint or we
7214 have software watchpoints). */
7215 ecs
->event_thread
->control
.may_range_step
= 1;
7217 /* When stepping backward, stop at beginning of line range
7218 (unless it's the function entry point, in which case
7219 keep going back to the call point). */
7220 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7221 if (stop_pc
== ecs
->event_thread
->control
.step_range_start
7222 && stop_pc
!= ecs
->stop_func_start
7223 && execution_direction
== EXEC_REVERSE
)
7224 end_stepping_range (ecs
);
7231 /* We stepped out of the stepping range. */
7233 /* If we are stepping at the source level and entered the runtime
7234 loader dynamic symbol resolution code...
7236 EXEC_FORWARD: we keep on single stepping until we exit the run
7237 time loader code and reach the callee's address.
7239 EXEC_REVERSE: we've already executed the callee (backward), and
7240 the runtime loader code is handled just like any other
7241 undebuggable function call. Now we need only keep stepping
7242 backward through the trampoline code, and that's handled further
7243 down, so there is nothing for us to do here. */
7245 if (execution_direction
!= EXEC_REVERSE
7246 && ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
7247 && in_solib_dynsym_resolve_code (ecs
->event_thread
->stop_pc ())
7248 && (ecs
->event_thread
->control
.step_start_function
== nullptr
7249 || !in_solib_dynsym_resolve_code (
7250 ecs
->event_thread
->control
.step_start_function
->value_block ()
7253 CORE_ADDR pc_after_resolver
=
7254 gdbarch_skip_solib_resolver (gdbarch
, ecs
->event_thread
->stop_pc ());
7256 infrun_debug_printf ("stepped into dynsym resolve code");
7258 if (pc_after_resolver
)
7260 /* Set up a step-resume breakpoint at the address
7261 indicated by SKIP_SOLIB_RESOLVER. */
7262 symtab_and_line sr_sal
;
7263 sr_sal
.pc
= pc_after_resolver
;
7264 sr_sal
.pspace
= get_frame_program_space (frame
);
7266 insert_step_resume_breakpoint_at_sal (gdbarch
,
7267 sr_sal
, null_frame_id
);
7274 /* Step through an indirect branch thunk. */
7275 if (ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
7276 && gdbarch_in_indirect_branch_thunk (gdbarch
,
7277 ecs
->event_thread
->stop_pc ()))
7279 infrun_debug_printf ("stepped into indirect branch thunk");
7284 if (ecs
->event_thread
->control
.step_range_end
!= 1
7285 && (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
7286 || ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
)
7287 && get_frame_type (frame
) == SIGTRAMP_FRAME
)
7289 infrun_debug_printf ("stepped into signal trampoline");
7290 /* The inferior, while doing a "step" or "next", has ended up in
7291 a signal trampoline (either by a signal being delivered or by
7292 the signal handler returning). Just single-step until the
7293 inferior leaves the trampoline (either by calling the handler
7299 /* If we're in the return path from a shared library trampoline,
7300 we want to proceed through the trampoline when stepping. */
7301 /* macro/2012-04-25: This needs to come before the subroutine
7302 call check below as on some targets return trampolines look
7303 like subroutine calls (MIPS16 return thunks). */
7304 if (gdbarch_in_solib_return_trampoline (gdbarch
,
7305 ecs
->event_thread
->stop_pc (),
7306 ecs
->stop_func_name
)
7307 && ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
)
7309 /* Determine where this trampoline returns. */
7310 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7311 CORE_ADDR real_stop_pc
7312 = gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
);
7314 infrun_debug_printf ("stepped into solib return tramp");
7316 /* Only proceed through if we know where it's going. */
7319 /* And put the step-breakpoint there and go until there. */
7320 symtab_and_line sr_sal
;
7321 sr_sal
.pc
= real_stop_pc
;
7322 sr_sal
.section
= find_pc_overlay (sr_sal
.pc
);
7323 sr_sal
.pspace
= get_frame_program_space (frame
);
7325 /* Do not specify what the fp should be when we stop since
7326 on some machines the prologue is where the new fp value
7328 insert_step_resume_breakpoint_at_sal (gdbarch
,
7329 sr_sal
, null_frame_id
);
7331 /* Restart without fiddling with the step ranges or
7338 /* Check for subroutine calls. The check for the current frame
7339 equalling the step ID is not necessary - the check of the
7340 previous frame's ID is sufficient - but it is a common case and
7341 cheaper than checking the previous frame's ID.
7343 NOTE: frame_id::operator== will never report two invalid frame IDs as
7344 being equal, so to get into this block, both the current and
7345 previous frame must have valid frame IDs. */
7346 /* The outer_frame_id check is a heuristic to detect stepping
7347 through startup code. If we step over an instruction which
7348 sets the stack pointer from an invalid value to a valid value,
7349 we may detect that as a subroutine call from the mythical
7350 "outermost" function. This could be fixed by marking
7351 outermost frames as !stack_p,code_p,special_p. Then the
7352 initial outermost frame, before sp was valid, would
7353 have code_addr == &_start. See the comment in frame_id::operator==
7356 /* We want "nexti" to step into, not over, signal handlers invoked
7357 by the kernel, therefore this subroutine check should not trigger
7358 for a signal handler invocation. On most platforms, this is already
7359 not the case, as the kernel puts a signal trampoline frame onto the
7360 stack to handle proper return after the handler, and therefore at this
7361 point, the current frame is a grandchild of the step frame, not a
7362 child. However, on some platforms, the kernel actually uses a
7363 trampoline to handle *invocation* of the handler. In that case,
7364 when executing the first instruction of the trampoline, this check
7365 would erroneously detect the trampoline invocation as a subroutine
7366 call. Fix this by checking for SIGTRAMP_FRAME. */
7367 if ((get_stack_frame_id (frame
)
7368 != ecs
->event_thread
->control
.step_stack_frame_id
)
7369 && get_frame_type (frame
) != SIGTRAMP_FRAME
7370 && ((frame_unwind_caller_id (get_current_frame ())
7371 == ecs
->event_thread
->control
.step_stack_frame_id
)
7372 && ((ecs
->event_thread
->control
.step_stack_frame_id
7374 || (ecs
->event_thread
->control
.step_start_function
7375 != find_pc_function (ecs
->event_thread
->stop_pc ())))))
7377 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7378 CORE_ADDR real_stop_pc
;
7380 infrun_debug_printf ("stepped into subroutine");
7382 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_NONE
)
7384 /* I presume that step_over_calls is only 0 when we're
7385 supposed to be stepping at the assembly language level
7386 ("stepi"). Just stop. */
7387 /* And this works the same backward as frontward. MVS */
7388 end_stepping_range (ecs
);
7392 /* Reverse stepping through solib trampolines. */
7394 if (execution_direction
== EXEC_REVERSE
7395 && ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
7396 && (gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
)
7397 || (ecs
->stop_func_start
== 0
7398 && in_solib_dynsym_resolve_code (stop_pc
))))
7400 /* Any solib trampoline code can be handled in reverse
7401 by simply continuing to single-step. We have already
7402 executed the solib function (backwards), and a few
7403 steps will take us back through the trampoline to the
7409 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
)
7411 /* We're doing a "next".
7413 Normal (forward) execution: set a breakpoint at the
7414 callee's return address (the address at which the caller
7417 Reverse (backward) execution. set the step-resume
7418 breakpoint at the start of the function that we just
7419 stepped into (backwards), and continue to there. When we
7420 get there, we'll need to single-step back to the caller. */
7422 if (execution_direction
== EXEC_REVERSE
)
7424 /* If we're already at the start of the function, we've either
7425 just stepped backward into a single instruction function,
7426 or stepped back out of a signal handler to the first instruction
7427 of the function. Just keep going, which will single-step back
7429 if (ecs
->stop_func_start
!= stop_pc
&& ecs
->stop_func_start
!= 0)
7431 /* Normal function call return (static or dynamic). */
7432 symtab_and_line sr_sal
;
7433 sr_sal
.pc
= ecs
->stop_func_start
;
7434 sr_sal
.pspace
= get_frame_program_space (frame
);
7435 insert_step_resume_breakpoint_at_sal (gdbarch
,
7436 sr_sal
, get_stack_frame_id (frame
));
7440 insert_step_resume_breakpoint_at_caller (frame
);
7446 /* If we are in a function call trampoline (a stub between the
7447 calling routine and the real function), locate the real
7448 function. That's what tells us (a) whether we want to step
7449 into it at all, and (b) what prologue we want to run to the
7450 end of, if we do step into it. */
7451 real_stop_pc
= skip_language_trampoline (frame
, stop_pc
);
7452 if (real_stop_pc
== 0)
7453 real_stop_pc
= gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
);
7454 if (real_stop_pc
!= 0)
7455 ecs
->stop_func_start
= real_stop_pc
;
7457 if (real_stop_pc
!= 0 && in_solib_dynsym_resolve_code (real_stop_pc
))
7459 symtab_and_line sr_sal
;
7460 sr_sal
.pc
= ecs
->stop_func_start
;
7461 sr_sal
.pspace
= get_frame_program_space (frame
);
7463 insert_step_resume_breakpoint_at_sal (gdbarch
,
7464 sr_sal
, null_frame_id
);
7469 /* If we have line number information for the function we are
7470 thinking of stepping into and the function isn't on the skip
7473 If there are several symtabs at that PC (e.g. with include
7474 files), just want to know whether *any* of them have line
7475 numbers. find_pc_line handles this. */
7477 struct symtab_and_line tmp_sal
;
7479 tmp_sal
= find_pc_line (ecs
->stop_func_start
, 0);
7480 if (tmp_sal
.line
!= 0
7481 && !function_name_is_marked_for_skip (ecs
->stop_func_name
,
7483 && !inline_frame_is_marked_for_skip (true, ecs
->event_thread
))
7485 if (execution_direction
== EXEC_REVERSE
)
7486 handle_step_into_function_backward (gdbarch
, ecs
);
7488 handle_step_into_function (gdbarch
, ecs
);
7493 /* If we have no line number and the step-stop-if-no-debug is
7494 set, we stop the step so that the user has a chance to switch
7495 in assembly mode. */
7496 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
7497 && step_stop_if_no_debug
)
7499 end_stepping_range (ecs
);
7503 if (execution_direction
== EXEC_REVERSE
)
7505 /* If we're already at the start of the function, we've either just
7506 stepped backward into a single instruction function without line
7507 number info, or stepped back out of a signal handler to the first
7508 instruction of the function without line number info. Just keep
7509 going, which will single-step back to the caller. */
7510 if (ecs
->stop_func_start
!= stop_pc
)
7512 /* Set a breakpoint at callee's start address.
7513 From there we can step once and be back in the caller. */
7514 symtab_and_line sr_sal
;
7515 sr_sal
.pc
= ecs
->stop_func_start
;
7516 sr_sal
.pspace
= get_frame_program_space (frame
);
7517 insert_step_resume_breakpoint_at_sal (gdbarch
,
7518 sr_sal
, null_frame_id
);
7522 /* Set a breakpoint at callee's return address (the address
7523 at which the caller will resume). */
7524 insert_step_resume_breakpoint_at_caller (frame
);
7530 /* Reverse stepping through solib trampolines. */
7532 if (execution_direction
== EXEC_REVERSE
7533 && ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
)
7535 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7537 if (gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
)
7538 || (ecs
->stop_func_start
== 0
7539 && in_solib_dynsym_resolve_code (stop_pc
)))
7541 /* Any solib trampoline code can be handled in reverse
7542 by simply continuing to single-step. We have already
7543 executed the solib function (backwards), and a few
7544 steps will take us back through the trampoline to the
7549 else if (in_solib_dynsym_resolve_code (stop_pc
))
7551 /* Stepped backward into the solib dynsym resolver.
7552 Set a breakpoint at its start and continue, then
7553 one more step will take us out. */
7554 symtab_and_line sr_sal
;
7555 sr_sal
.pc
= ecs
->stop_func_start
;
7556 sr_sal
.pspace
= get_frame_program_space (frame
);
7557 insert_step_resume_breakpoint_at_sal (gdbarch
,
7558 sr_sal
, null_frame_id
);
7564 /* This always returns the sal for the inner-most frame when we are in a
7565 stack of inlined frames, even if GDB actually believes that it is in a
7566 more outer frame. This is checked for below by calls to
7567 inline_skipped_frames. */
7568 stop_pc_sal
= find_pc_line (ecs
->event_thread
->stop_pc (), 0);
7570 /* NOTE: tausq/2004-05-24: This if block used to be done before all
7571 the trampoline processing logic, however, there are some trampolines
7572 that have no names, so we should do trampoline handling first. */
7573 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
7574 && ecs
->stop_func_name
== nullptr
7575 && stop_pc_sal
.line
== 0)
7577 infrun_debug_printf ("stepped into undebuggable function");
7579 /* The inferior just stepped into, or returned to, an
7580 undebuggable function (where there is no debugging information
7581 and no line number corresponding to the address where the
7582 inferior stopped). Since we want to skip this kind of code,
7583 we keep going until the inferior returns from this
7584 function - unless the user has asked us not to (via
7585 set step-mode) or we no longer know how to get back
7586 to the call site. */
7587 if (step_stop_if_no_debug
7588 || !frame_id_p (frame_unwind_caller_id (frame
)))
7590 /* If we have no line number and the step-stop-if-no-debug
7591 is set, we stop the step so that the user has a chance to
7592 switch in assembly mode. */
7593 end_stepping_range (ecs
);
7598 /* Set a breakpoint at callee's return address (the address
7599 at which the caller will resume). */
7600 insert_step_resume_breakpoint_at_caller (frame
);
7606 if (execution_direction
== EXEC_REVERSE
7607 && ecs
->event_thread
->control
.proceed_to_finish
7608 && ecs
->event_thread
->stop_pc () >= ecs
->stop_func_alt_start
7609 && ecs
->event_thread
->stop_pc () < ecs
->stop_func_start
)
7611 /* We are executing the reverse-finish command.
7612 If the system supports multiple entry points and we are finishing a
7613 function in reverse. If we are between the entry points single-step
7614 back to the alternate entry point. If we are at the alternate entry
7615 point -- just need to back up by one more single-step, which
7616 should take us back to the function call. */
7617 ecs
->event_thread
->control
.step_range_start
7618 = ecs
->event_thread
->control
.step_range_end
= 1;
7624 if (ecs
->event_thread
->control
.step_range_end
== 1)
7626 /* It is stepi or nexti. We always want to stop stepping after
7628 infrun_debug_printf ("stepi/nexti");
7629 end_stepping_range (ecs
);
7633 if (stop_pc_sal
.line
== 0)
7635 /* We have no line number information. That means to stop
7636 stepping (does this always happen right after one instruction,
7637 when we do "s" in a function with no line numbers,
7638 or can this happen as a result of a return or longjmp?). */
7639 infrun_debug_printf ("line number info");
7640 end_stepping_range (ecs
);
7644 /* Look for "calls" to inlined functions, part one. If the inline
7645 frame machinery detected some skipped call sites, we have entered
7646 a new inline function. */
7648 if ((get_frame_id (get_current_frame ())
7649 == ecs
->event_thread
->control
.step_frame_id
)
7650 && inline_skipped_frames (ecs
->event_thread
))
7652 infrun_debug_printf ("stepped into inlined function");
7654 symtab_and_line call_sal
= find_frame_sal (get_current_frame ());
7656 if (ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_ALL
)
7658 /* For "step", we're going to stop. But if the call site
7659 for this inlined function is on the same source line as
7660 we were previously stepping, go down into the function
7661 first. Otherwise stop at the call site. */
7663 if (call_sal
.line
== ecs
->event_thread
->current_line
7664 && call_sal
.symtab
== ecs
->event_thread
->current_symtab
)
7666 step_into_inline_frame (ecs
->event_thread
);
7667 if (inline_frame_is_marked_for_skip (false, ecs
->event_thread
))
7674 end_stepping_range (ecs
);
7679 /* For "next", we should stop at the call site if it is on a
7680 different source line. Otherwise continue through the
7681 inlined function. */
7682 if (call_sal
.line
== ecs
->event_thread
->current_line
7683 && call_sal
.symtab
== ecs
->event_thread
->current_symtab
)
7686 end_stepping_range (ecs
);
7691 /* Look for "calls" to inlined functions, part two. If we are still
7692 in the same real function we were stepping through, but we have
7693 to go further up to find the exact frame ID, we are stepping
7694 through a more inlined call beyond its call site. */
7696 if (get_frame_type (get_current_frame ()) == INLINE_FRAME
7697 && (get_frame_id (get_current_frame ())
7698 != ecs
->event_thread
->control
.step_frame_id
)
7699 && stepped_in_from (get_current_frame (),
7700 ecs
->event_thread
->control
.step_frame_id
))
7702 infrun_debug_printf ("stepping through inlined function");
7704 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
7705 || inline_frame_is_marked_for_skip (false, ecs
->event_thread
))
7708 end_stepping_range (ecs
);
7712 bool refresh_step_info
= true;
7713 if ((ecs
->event_thread
->stop_pc () == stop_pc_sal
.pc
)
7714 && (ecs
->event_thread
->current_line
!= stop_pc_sal
.line
7715 || ecs
->event_thread
->current_symtab
!= stop_pc_sal
.symtab
))
7717 /* We are at a different line. */
7719 if (stop_pc_sal
.is_stmt
)
7721 /* We are at the start of a statement.
7723 So stop. Note that we don't stop if we step into the middle of a
7724 statement. That is said to make things like for (;;) statements
7726 infrun_debug_printf ("stepped to a different line");
7727 end_stepping_range (ecs
);
7730 else if (get_frame_id (get_current_frame ())
7731 == ecs
->event_thread
->control
.step_frame_id
)
7733 /* We are not at the start of a statement, and we have not changed
7736 We ignore this line table entry, and continue stepping forward,
7737 looking for a better place to stop. */
7738 refresh_step_info
= false;
7739 infrun_debug_printf ("stepped to a different line, but "
7740 "it's not the start of a statement");
7744 /* We are not the start of a statement, and we have changed frame.
7746 We ignore this line table entry, and continue stepping forward,
7747 looking for a better place to stop. Keep refresh_step_info at
7748 true to note that the frame has changed, but ignore the line
7749 number to make sure we don't ignore a subsequent entry with the
7750 same line number. */
7751 stop_pc_sal
.line
= 0;
7752 infrun_debug_printf ("stepped to a different frame, but "
7753 "it's not the start of a statement");
7757 /* We aren't done stepping.
7759 Optimize by setting the stepping range to the line.
7760 (We might not be in the original line, but if we entered a
7761 new line in mid-statement, we continue stepping. This makes
7762 things like for(;;) statements work better.)
7764 If we entered a SAL that indicates a non-statement line table entry,
7765 then we update the stepping range, but we don't update the step info,
7766 which includes things like the line number we are stepping away from.
7767 This means we will stop when we find a line table entry that is marked
7768 as is-statement, even if it matches the non-statement one we just
7771 ecs
->event_thread
->control
.step_range_start
= stop_pc_sal
.pc
;
7772 ecs
->event_thread
->control
.step_range_end
= stop_pc_sal
.end
;
7773 ecs
->event_thread
->control
.may_range_step
= 1;
7775 ("updated step range, start = %s, end = %s, may_range_step = %d",
7776 paddress (gdbarch
, ecs
->event_thread
->control
.step_range_start
),
7777 paddress (gdbarch
, ecs
->event_thread
->control
.step_range_end
),
7778 ecs
->event_thread
->control
.may_range_step
);
7779 if (refresh_step_info
)
7780 set_step_info (ecs
->event_thread
, frame
, stop_pc_sal
);
7782 infrun_debug_printf ("keep going");
7786 static bool restart_stepped_thread (process_stratum_target
*resume_target
,
7787 ptid_t resume_ptid
);
7789 /* In all-stop mode, if we're currently stepping but have stopped in
7790 some other thread, we may need to switch back to the stepped
7791 thread. Returns true we set the inferior running, false if we left
7792 it stopped (and the event needs further processing). */
7795 switch_back_to_stepped_thread (struct execution_control_state
*ecs
)
7797 if (!target_is_non_stop_p ())
7799 /* If any thread is blocked on some internal breakpoint, and we
7800 simply need to step over that breakpoint to get it going
7801 again, do that first. */
7803 /* However, if we see an event for the stepping thread, then we
7804 know all other threads have been moved past their breakpoints
7805 already. Let the caller check whether the step is finished,
7806 etc., before deciding to move it past a breakpoint. */
7807 if (ecs
->event_thread
->control
.step_range_end
!= 0)
7810 /* Check if the current thread is blocked on an incomplete
7811 step-over, interrupted by a random signal. */
7812 if (ecs
->event_thread
->control
.trap_expected
7813 && ecs
->event_thread
->stop_signal () != GDB_SIGNAL_TRAP
)
7816 ("need to finish step-over of [%s]",
7817 ecs
->event_thread
->ptid
.to_string ().c_str ());
7822 /* Check if the current thread is blocked by a single-step
7823 breakpoint of another thread. */
7824 if (ecs
->hit_singlestep_breakpoint
)
7826 infrun_debug_printf ("need to step [%s] over single-step breakpoint",
7827 ecs
->ptid
.to_string ().c_str ());
7832 /* If this thread needs yet another step-over (e.g., stepping
7833 through a delay slot), do it first before moving on to
7835 if (thread_still_needs_step_over (ecs
->event_thread
))
7838 ("thread [%s] still needs step-over",
7839 ecs
->event_thread
->ptid
.to_string ().c_str ());
7844 /* If scheduler locking applies even if not stepping, there's no
7845 need to walk over threads. Above we've checked whether the
7846 current thread is stepping. If some other thread not the
7847 event thread is stepping, then it must be that scheduler
7848 locking is not in effect. */
7849 if (schedlock_applies (ecs
->event_thread
))
7852 /* Otherwise, we no longer expect a trap in the current thread.
7853 Clear the trap_expected flag before switching back -- this is
7854 what keep_going does as well, if we call it. */
7855 ecs
->event_thread
->control
.trap_expected
= 0;
7857 /* Likewise, clear the signal if it should not be passed. */
7858 if (!signal_program
[ecs
->event_thread
->stop_signal ()])
7859 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
7861 if (restart_stepped_thread (ecs
->target
, ecs
->ptid
))
7863 prepare_to_wait (ecs
);
7867 switch_to_thread (ecs
->event_thread
);
7873 /* Look for the thread that was stepping, and resume it.
7874 RESUME_TARGET / RESUME_PTID indicate the set of threads the caller
7875 is resuming. Return true if a thread was started, false
7879 restart_stepped_thread (process_stratum_target
*resume_target
,
7882 /* Do all pending step-overs before actually proceeding with
7884 if (start_step_over ())
7887 for (thread_info
*tp
: all_threads_safe ())
7889 if (tp
->state
== THREAD_EXITED
)
7892 if (tp
->has_pending_waitstatus ())
7895 /* Ignore threads of processes the caller is not
7898 && (tp
->inf
->process_target () != resume_target
7899 || tp
->inf
->pid
!= resume_ptid
.pid ()))
7902 if (tp
->control
.trap_expected
)
7904 infrun_debug_printf ("switching back to stepped thread (step-over)");
7906 if (keep_going_stepped_thread (tp
))
7911 for (thread_info
*tp
: all_threads_safe ())
7913 if (tp
->state
== THREAD_EXITED
)
7916 if (tp
->has_pending_waitstatus ())
7919 /* Ignore threads of processes the caller is not
7922 && (tp
->inf
->process_target () != resume_target
7923 || tp
->inf
->pid
!= resume_ptid
.pid ()))
7926 /* Did we find the stepping thread? */
7927 if (tp
->control
.step_range_end
)
7929 infrun_debug_printf ("switching back to stepped thread (stepping)");
7931 if (keep_going_stepped_thread (tp
))
7942 restart_after_all_stop_detach (process_stratum_target
*proc_target
)
7944 /* Note we don't check target_is_non_stop_p() here, because the
7945 current inferior may no longer have a process_stratum target
7946 pushed, as we just detached. */
7948 /* See if we have a THREAD_RUNNING thread that need to be
7949 re-resumed. If we have any thread that is already executing,
7950 then we don't need to resume the target -- it is already been
7951 resumed. With the remote target (in all-stop), it's even
7952 impossible to issue another resumption if the target is already
7953 resumed, until the target reports a stop. */
7954 for (thread_info
*thr
: all_threads (proc_target
))
7956 if (thr
->state
!= THREAD_RUNNING
)
7959 /* If we have any thread that is already executing, then we
7960 don't need to resume the target -- it is already been
7962 if (thr
->executing ())
7965 /* If we have a pending event to process, skip resuming the
7966 target and go straight to processing it. */
7967 if (thr
->resumed () && thr
->has_pending_waitstatus ())
7971 /* Alright, we need to re-resume the target. If a thread was
7972 stepping, we need to restart it stepping. */
7973 if (restart_stepped_thread (proc_target
, minus_one_ptid
))
7976 /* Otherwise, find the first THREAD_RUNNING thread and resume
7978 for (thread_info
*thr
: all_threads (proc_target
))
7980 if (thr
->state
!= THREAD_RUNNING
)
7983 execution_control_state
ecs (thr
);
7984 switch_to_thread (thr
);
7990 /* Set a previously stepped thread back to stepping. Returns true on
7991 success, false if the resume is not possible (e.g., the thread
7995 keep_going_stepped_thread (struct thread_info
*tp
)
7997 frame_info_ptr frame
;
7999 /* If the stepping thread exited, then don't try to switch back and
8000 resume it, which could fail in several different ways depending
8001 on the target. Instead, just keep going.
8003 We can find a stepping dead thread in the thread list in two
8006 - The target supports thread exit events, and when the target
8007 tries to delete the thread from the thread list, inferior_ptid
8008 pointed at the exiting thread. In such case, calling
8009 delete_thread does not really remove the thread from the list;
8010 instead, the thread is left listed, with 'exited' state.
8012 - The target's debug interface does not support thread exit
8013 events, and so we have no idea whatsoever if the previously
8014 stepping thread is still alive. For that reason, we need to
8015 synchronously query the target now. */
8017 if (tp
->state
== THREAD_EXITED
|| !target_thread_alive (tp
->ptid
))
8019 infrun_debug_printf ("not resuming previously stepped thread, it has "
8026 infrun_debug_printf ("resuming previously stepped thread");
8028 execution_control_state
ecs (tp
);
8029 switch_to_thread (tp
);
8031 tp
->set_stop_pc (regcache_read_pc (get_thread_regcache (tp
)));
8032 frame
= get_current_frame ();
8034 /* If the PC of the thread we were trying to single-step has
8035 changed, then that thread has trapped or been signaled, but the
8036 event has not been reported to GDB yet. Re-poll the target
8037 looking for this particular thread's event (i.e. temporarily
8038 enable schedlock) by:
8040 - setting a break at the current PC
8041 - resuming that particular thread, only (by setting trap
8044 This prevents us continuously moving the single-step breakpoint
8045 forward, one instruction at a time, overstepping. */
8047 if (tp
->stop_pc () != tp
->prev_pc
)
8051 infrun_debug_printf ("expected thread advanced also (%s -> %s)",
8052 paddress (current_inferior ()->arch (), tp
->prev_pc
),
8053 paddress (current_inferior ()->arch (),
8056 /* Clear the info of the previous step-over, as it's no longer
8057 valid (if the thread was trying to step over a breakpoint, it
8058 has already succeeded). It's what keep_going would do too,
8059 if we called it. Do this before trying to insert the sss
8060 breakpoint, otherwise if we were previously trying to step
8061 over this exact address in another thread, the breakpoint is
8063 clear_step_over_info ();
8064 tp
->control
.trap_expected
= 0;
8066 insert_single_step_breakpoint (get_frame_arch (frame
),
8067 get_frame_address_space (frame
),
8070 tp
->set_resumed (true);
8071 resume_ptid
= internal_resume_ptid (tp
->control
.stepping_command
);
8072 do_target_resume (resume_ptid
, false, GDB_SIGNAL_0
);
8076 infrun_debug_printf ("expected thread still hasn't advanced");
8078 keep_going_pass_signal (&ecs
);
8084 /* Is thread TP in the middle of (software or hardware)
8085 single-stepping? (Note the result of this function must never be
8086 passed directly as target_resume's STEP parameter.) */
8089 currently_stepping (struct thread_info
*tp
)
8091 return ((tp
->control
.step_range_end
8092 && tp
->control
.step_resume_breakpoint
== nullptr)
8093 || tp
->control
.trap_expected
8094 || tp
->stepped_breakpoint
8095 || bpstat_should_step ());
8098 /* Inferior has stepped into a subroutine call with source code that
8099 we should not step over. Do step to the first line of code in
8103 handle_step_into_function (struct gdbarch
*gdbarch
,
8104 struct execution_control_state
*ecs
)
8106 fill_in_stop_func (gdbarch
, ecs
);
8108 compunit_symtab
*cust
8109 = find_pc_compunit_symtab (ecs
->event_thread
->stop_pc ());
8110 if (cust
!= nullptr && cust
->language () != language_asm
)
8111 ecs
->stop_func_start
8112 = gdbarch_skip_prologue_noexcept (gdbarch
, ecs
->stop_func_start
);
8114 symtab_and_line stop_func_sal
= find_pc_line (ecs
->stop_func_start
, 0);
8115 /* Use the step_resume_break to step until the end of the prologue,
8116 even if that involves jumps (as it seems to on the vax under
8118 /* If the prologue ends in the middle of a source line, continue to
8119 the end of that source line (if it is still within the function).
8120 Otherwise, just go to end of prologue. */
8121 if (stop_func_sal
.end
8122 && stop_func_sal
.pc
!= ecs
->stop_func_start
8123 && stop_func_sal
.end
< ecs
->stop_func_end
)
8124 ecs
->stop_func_start
= stop_func_sal
.end
;
8126 /* Architectures which require breakpoint adjustment might not be able
8127 to place a breakpoint at the computed address. If so, the test
8128 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
8129 ecs->stop_func_start to an address at which a breakpoint may be
8130 legitimately placed.
8132 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
8133 made, GDB will enter an infinite loop when stepping through
8134 optimized code consisting of VLIW instructions which contain
8135 subinstructions corresponding to different source lines. On
8136 FR-V, it's not permitted to place a breakpoint on any but the
8137 first subinstruction of a VLIW instruction. When a breakpoint is
8138 set, GDB will adjust the breakpoint address to the beginning of
8139 the VLIW instruction. Thus, we need to make the corresponding
8140 adjustment here when computing the stop address. */
8142 if (gdbarch_adjust_breakpoint_address_p (gdbarch
))
8144 ecs
->stop_func_start
8145 = gdbarch_adjust_breakpoint_address (gdbarch
,
8146 ecs
->stop_func_start
);
8149 if (ecs
->stop_func_start
== ecs
->event_thread
->stop_pc ())
8151 /* We are already there: stop now. */
8152 end_stepping_range (ecs
);
8157 /* Put the step-breakpoint there and go until there. */
8158 symtab_and_line sr_sal
;
8159 sr_sal
.pc
= ecs
->stop_func_start
;
8160 sr_sal
.section
= find_pc_overlay (ecs
->stop_func_start
);
8161 sr_sal
.pspace
= get_frame_program_space (get_current_frame ());
8163 /* Do not specify what the fp should be when we stop since on
8164 some machines the prologue is where the new fp value is
8166 insert_step_resume_breakpoint_at_sal (gdbarch
, sr_sal
, null_frame_id
);
8168 /* And make sure stepping stops right away then. */
8169 ecs
->event_thread
->control
.step_range_end
8170 = ecs
->event_thread
->control
.step_range_start
;
8175 /* Inferior has stepped backward into a subroutine call with source
8176 code that we should not step over. Do step to the beginning of the
8177 last line of code in it. */
8180 handle_step_into_function_backward (struct gdbarch
*gdbarch
,
8181 struct execution_control_state
*ecs
)
8183 struct compunit_symtab
*cust
;
8184 struct symtab_and_line stop_func_sal
;
8186 fill_in_stop_func (gdbarch
, ecs
);
8188 cust
= find_pc_compunit_symtab (ecs
->event_thread
->stop_pc ());
8189 if (cust
!= nullptr && cust
->language () != language_asm
)
8190 ecs
->stop_func_start
8191 = gdbarch_skip_prologue_noexcept (gdbarch
, ecs
->stop_func_start
);
8193 stop_func_sal
= find_pc_line (ecs
->event_thread
->stop_pc (), 0);
8195 /* OK, we're just going to keep stepping here. */
8196 if (stop_func_sal
.pc
== ecs
->event_thread
->stop_pc ())
8198 /* We're there already. Just stop stepping now. */
8199 end_stepping_range (ecs
);
8203 /* Else just reset the step range and keep going.
8204 No step-resume breakpoint, they don't work for
8205 epilogues, which can have multiple entry paths. */
8206 ecs
->event_thread
->control
.step_range_start
= stop_func_sal
.pc
;
8207 ecs
->event_thread
->control
.step_range_end
= stop_func_sal
.end
;
8213 /* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
8214 This is used to both functions and to skip over code. */
8217 insert_step_resume_breakpoint_at_sal_1 (struct gdbarch
*gdbarch
,
8218 struct symtab_and_line sr_sal
,
8219 struct frame_id sr_id
,
8220 enum bptype sr_type
)
8222 /* There should never be more than one step-resume or longjmp-resume
8223 breakpoint per thread, so we should never be setting a new
8224 step_resume_breakpoint when one is already active. */
8225 gdb_assert (inferior_thread ()->control
.step_resume_breakpoint
== nullptr);
8226 gdb_assert (sr_type
== bp_step_resume
|| sr_type
== bp_hp_step_resume
);
8228 infrun_debug_printf ("inserting step-resume breakpoint at %s",
8229 paddress (gdbarch
, sr_sal
.pc
));
8231 inferior_thread ()->control
.step_resume_breakpoint
8232 = set_momentary_breakpoint (gdbarch
, sr_sal
, sr_id
, sr_type
).release ();
8236 insert_step_resume_breakpoint_at_sal (struct gdbarch
*gdbarch
,
8237 struct symtab_and_line sr_sal
,
8238 struct frame_id sr_id
)
8240 insert_step_resume_breakpoint_at_sal_1 (gdbarch
,
8245 /* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
8246 This is used to skip a potential signal handler.
8248 This is called with the interrupted function's frame. The signal
8249 handler, when it returns, will resume the interrupted function at
8253 insert_hp_step_resume_breakpoint_at_frame (frame_info_ptr return_frame
)
8255 gdb_assert (return_frame
!= nullptr);
8257 struct gdbarch
*gdbarch
= get_frame_arch (return_frame
);
8259 symtab_and_line sr_sal
;
8260 sr_sal
.pc
= gdbarch_addr_bits_remove (gdbarch
, get_frame_pc (return_frame
));
8261 sr_sal
.section
= find_pc_overlay (sr_sal
.pc
);
8262 sr_sal
.pspace
= get_frame_program_space (return_frame
);
8264 insert_step_resume_breakpoint_at_sal_1 (gdbarch
, sr_sal
,
8265 get_stack_frame_id (return_frame
),
8269 /* Insert a "step-resume breakpoint" at the previous frame's PC. This
8270 is used to skip a function after stepping into it (for "next" or if
8271 the called function has no debugging information).
8273 The current function has almost always been reached by single
8274 stepping a call or return instruction. NEXT_FRAME belongs to the
8275 current function, and the breakpoint will be set at the caller's
8278 This is a separate function rather than reusing
8279 insert_hp_step_resume_breakpoint_at_frame in order to avoid
8280 get_prev_frame, which may stop prematurely (see the implementation
8281 of frame_unwind_caller_id for an example). */
8284 insert_step_resume_breakpoint_at_caller (frame_info_ptr next_frame
)
8286 /* We shouldn't have gotten here if we don't know where the call site
8288 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame
)));
8290 struct gdbarch
*gdbarch
= frame_unwind_caller_arch (next_frame
);
8292 symtab_and_line sr_sal
;
8293 sr_sal
.pc
= gdbarch_addr_bits_remove (gdbarch
,
8294 frame_unwind_caller_pc (next_frame
));
8295 sr_sal
.section
= find_pc_overlay (sr_sal
.pc
);
8296 sr_sal
.pspace
= frame_unwind_program_space (next_frame
);
8298 insert_step_resume_breakpoint_at_sal (gdbarch
, sr_sal
,
8299 frame_unwind_caller_id (next_frame
));
8302 /* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
8303 new breakpoint at the target of a jmp_buf. The handling of
8304 longjmp-resume uses the same mechanisms used for handling
8305 "step-resume" breakpoints. */
8308 insert_longjmp_resume_breakpoint (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
8310 /* There should never be more than one longjmp-resume breakpoint per
8311 thread, so we should never be setting a new
8312 longjmp_resume_breakpoint when one is already active. */
8313 gdb_assert (inferior_thread ()->control
.exception_resume_breakpoint
== nullptr);
8315 infrun_debug_printf ("inserting longjmp-resume breakpoint at %s",
8316 paddress (gdbarch
, pc
));
8318 inferior_thread ()->control
.exception_resume_breakpoint
=
8319 set_momentary_breakpoint_at_pc (gdbarch
, pc
, bp_longjmp_resume
).release ();
8322 /* Insert an exception resume breakpoint. TP is the thread throwing
8323 the exception. The block B is the block of the unwinder debug hook
8324 function. FRAME is the frame corresponding to the call to this
8325 function. SYM is the symbol of the function argument holding the
8326 target PC of the exception. */
8329 insert_exception_resume_breakpoint (struct thread_info
*tp
,
8330 const struct block
*b
,
8331 frame_info_ptr frame
,
8336 struct block_symbol vsym
;
8337 struct value
*value
;
8339 struct breakpoint
*bp
;
8341 vsym
= lookup_symbol_search_name (sym
->search_name (),
8343 value
= read_var_value (vsym
.symbol
, vsym
.block
, frame
);
8344 /* If the value was optimized out, revert to the old behavior. */
8345 if (! value
->optimized_out ())
8347 handler
= value_as_address (value
);
8349 infrun_debug_printf ("exception resume at %lx",
8350 (unsigned long) handler
);
8352 /* set_momentary_breakpoint_at_pc creates a thread-specific
8353 breakpoint for the current inferior thread. */
8354 gdb_assert (tp
== inferior_thread ());
8355 bp
= set_momentary_breakpoint_at_pc (get_frame_arch (frame
),
8357 bp_exception_resume
).release ();
8359 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
8362 tp
->control
.exception_resume_breakpoint
= bp
;
8365 catch (const gdb_exception_error
&e
)
8367 /* We want to ignore errors here. */
8371 /* A helper for check_exception_resume that sets an
8372 exception-breakpoint based on a SystemTap probe. */
8375 insert_exception_resume_from_probe (struct thread_info
*tp
,
8376 const struct bound_probe
*probe
,
8377 frame_info_ptr frame
)
8379 struct value
*arg_value
;
8381 struct breakpoint
*bp
;
8383 arg_value
= probe_safe_evaluate_at_pc (frame
, 1);
8387 handler
= value_as_address (arg_value
);
8389 infrun_debug_printf ("exception resume at %s",
8390 paddress (probe
->objfile
->arch (), handler
));
8392 /* set_momentary_breakpoint_at_pc creates a thread-specific breakpoint
8393 for the current inferior thread. */
8394 gdb_assert (tp
== inferior_thread ());
8395 bp
= set_momentary_breakpoint_at_pc (get_frame_arch (frame
),
8396 handler
, bp_exception_resume
).release ();
8397 tp
->control
.exception_resume_breakpoint
= bp
;
8400 /* This is called when an exception has been intercepted. Check to
8401 see whether the exception's destination is of interest, and if so,
8402 set an exception resume breakpoint there. */
8405 check_exception_resume (struct execution_control_state
*ecs
,
8406 frame_info_ptr frame
)
8408 struct bound_probe probe
;
8409 struct symbol
*func
;
8411 /* First see if this exception unwinding breakpoint was set via a
8412 SystemTap probe point. If so, the probe has two arguments: the
8413 CFA and the HANDLER. We ignore the CFA, extract the handler, and
8414 set a breakpoint there. */
8415 probe
= find_probe_by_pc (get_frame_pc (frame
));
8418 insert_exception_resume_from_probe (ecs
->event_thread
, &probe
, frame
);
8422 func
= get_frame_function (frame
);
8428 const struct block
*b
;
8431 /* The exception breakpoint is a thread-specific breakpoint on
8432 the unwinder's debug hook, declared as:
8434 void _Unwind_DebugHook (void *cfa, void *handler);
8436 The CFA argument indicates the frame to which control is
8437 about to be transferred. HANDLER is the destination PC.
8439 We ignore the CFA and set a temporary breakpoint at HANDLER.
8440 This is not extremely efficient but it avoids issues in gdb
8441 with computing the DWARF CFA, and it also works even in weird
8442 cases such as throwing an exception from inside a signal
8445 b
= func
->value_block ();
8446 for (struct symbol
*sym
: block_iterator_range (b
))
8448 if (!sym
->is_argument ())
8455 insert_exception_resume_breakpoint (ecs
->event_thread
,
8461 catch (const gdb_exception_error
&e
)
8467 stop_waiting (struct execution_control_state
*ecs
)
8469 infrun_debug_printf ("stop_waiting");
8471 /* Let callers know we don't want to wait for the inferior anymore. */
8472 ecs
->wait_some_more
= 0;
8475 /* Like keep_going, but passes the signal to the inferior, even if the
8476 signal is set to nopass. */
8479 keep_going_pass_signal (struct execution_control_state
*ecs
)
8481 gdb_assert (ecs
->event_thread
->ptid
== inferior_ptid
);
8482 gdb_assert (!ecs
->event_thread
->resumed ());
8484 /* Save the pc before execution, to compare with pc after stop. */
8485 ecs
->event_thread
->prev_pc
8486 = regcache_read_pc_protected (get_thread_regcache (ecs
->event_thread
));
8488 if (ecs
->event_thread
->control
.trap_expected
)
8490 struct thread_info
*tp
= ecs
->event_thread
;
8492 infrun_debug_printf ("%s has trap_expected set, "
8493 "resuming to collect trap",
8494 tp
->ptid
.to_string ().c_str ());
8496 /* We haven't yet gotten our trap, and either: intercepted a
8497 non-signal event (e.g., a fork); or took a signal which we
8498 are supposed to pass through to the inferior. Simply
8500 resume (ecs
->event_thread
->stop_signal ());
8502 else if (step_over_info_valid_p ())
8504 /* Another thread is stepping over a breakpoint in-line. If
8505 this thread needs a step-over too, queue the request. In
8506 either case, this resume must be deferred for later. */
8507 struct thread_info
*tp
= ecs
->event_thread
;
8509 if (ecs
->hit_singlestep_breakpoint
8510 || thread_still_needs_step_over (tp
))
8512 infrun_debug_printf ("step-over already in progress: "
8513 "step-over for %s deferred",
8514 tp
->ptid
.to_string ().c_str ());
8515 global_thread_step_over_chain_enqueue (tp
);
8518 infrun_debug_printf ("step-over in progress: resume of %s deferred",
8519 tp
->ptid
.to_string ().c_str ());
8523 struct regcache
*regcache
= get_current_regcache ();
8526 step_over_what step_what
;
8528 /* Either the trap was not expected, but we are continuing
8529 anyway (if we got a signal, the user asked it be passed to
8532 We got our expected trap, but decided we should resume from
8535 We're going to run this baby now!
8537 Note that insert_breakpoints won't try to re-insert
8538 already inserted breakpoints. Therefore, we don't
8539 care if breakpoints were already inserted, or not. */
8541 /* If we need to step over a breakpoint, and we're not using
8542 displaced stepping to do so, insert all breakpoints
8543 (watchpoints, etc.) but the one we're stepping over, step one
8544 instruction, and then re-insert the breakpoint when that step
8547 step_what
= thread_still_needs_step_over (ecs
->event_thread
);
8549 remove_bp
= (ecs
->hit_singlestep_breakpoint
8550 || (step_what
& STEP_OVER_BREAKPOINT
));
8551 remove_wps
= (step_what
& STEP_OVER_WATCHPOINT
);
8553 /* We can't use displaced stepping if we need to step past a
8554 watchpoint. The instruction copied to the scratch pad would
8555 still trigger the watchpoint. */
8557 && (remove_wps
|| !use_displaced_stepping (ecs
->event_thread
)))
8559 set_step_over_info (regcache
->aspace (),
8560 regcache_read_pc (regcache
), remove_wps
,
8561 ecs
->event_thread
->global_num
);
8563 else if (remove_wps
)
8564 set_step_over_info (nullptr, 0, remove_wps
, -1);
8566 /* If we now need to do an in-line step-over, we need to stop
8567 all other threads. Note this must be done before
8568 insert_breakpoints below, because that removes the breakpoint
8569 we're about to step over, otherwise other threads could miss
8571 if (step_over_info_valid_p () && target_is_non_stop_p ())
8572 stop_all_threads ("starting in-line step-over");
8574 /* Stop stepping if inserting breakpoints fails. */
8577 insert_breakpoints ();
8579 catch (const gdb_exception_error
&e
)
8581 exception_print (gdb_stderr
, e
);
8583 clear_step_over_info ();
8587 ecs
->event_thread
->control
.trap_expected
= (remove_bp
|| remove_wps
);
8589 resume (ecs
->event_thread
->stop_signal ());
8592 prepare_to_wait (ecs
);
8595 /* Called when we should continue running the inferior, because the
8596 current event doesn't cause a user visible stop. This does the
8597 resuming part; waiting for the next event is done elsewhere. */
8600 keep_going (struct execution_control_state
*ecs
)
8602 if (ecs
->event_thread
->control
.trap_expected
8603 && ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
)
8604 ecs
->event_thread
->control
.trap_expected
= 0;
8606 if (!signal_program
[ecs
->event_thread
->stop_signal ()])
8607 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
8608 keep_going_pass_signal (ecs
);
8611 /* This function normally comes after a resume, before
8612 handle_inferior_event exits. It takes care of any last bits of
8613 housekeeping, and sets the all-important wait_some_more flag. */
8616 prepare_to_wait (struct execution_control_state
*ecs
)
8618 infrun_debug_printf ("prepare_to_wait");
8620 ecs
->wait_some_more
= 1;
8622 /* If the target can't async, emulate it by marking the infrun event
8623 handler such that as soon as we get back to the event-loop, we
8624 immediately end up in fetch_inferior_event again calling
8626 if (!target_can_async_p ())
8627 mark_infrun_async_event_handler ();
8630 /* We are done with the step range of a step/next/si/ni command.
8631 Called once for each n of a "step n" operation. */
8634 end_stepping_range (struct execution_control_state
*ecs
)
8636 ecs
->event_thread
->control
.stop_step
= 1;
8640 /* Several print_*_reason functions to print why the inferior has stopped.
8641 We always print something when the inferior exits, or receives a signal.
8642 The rest of the cases are dealt with later on in normal_stop and
8643 print_it_typical. Ideally there should be a call to one of these
8644 print_*_reason functions functions from handle_inferior_event each time
8645 stop_waiting is called.
8647 Note that we don't call these directly, instead we delegate that to
8648 the interpreters, through observers. Interpreters then call these
8649 with whatever uiout is right. */
8652 print_signal_exited_reason (struct ui_out
*uiout
, enum gdb_signal siggnal
)
8654 annotate_signalled ();
8655 if (uiout
->is_mi_like_p ())
8657 ("reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED
));
8658 uiout
->text ("\nProgram terminated with signal ");
8659 annotate_signal_name ();
8660 uiout
->field_string ("signal-name",
8661 gdb_signal_to_name (siggnal
));
8662 annotate_signal_name_end ();
8664 annotate_signal_string ();
8665 uiout
->field_string ("signal-meaning",
8666 gdb_signal_to_string (siggnal
));
8667 annotate_signal_string_end ();
8668 uiout
->text (".\n");
8669 uiout
->text ("The program no longer exists.\n");
8673 print_exited_reason (struct ui_out
*uiout
, int exitstatus
)
8675 struct inferior
*inf
= current_inferior ();
8676 std::string pidstr
= target_pid_to_str (ptid_t (inf
->pid
));
8678 annotate_exited (exitstatus
);
8681 if (uiout
->is_mi_like_p ())
8682 uiout
->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXITED
));
8683 std::string exit_code_str
8684 = string_printf ("0%o", (unsigned int) exitstatus
);
8685 uiout
->message ("[Inferior %s (%s) exited with code %pF]\n",
8686 plongest (inf
->num
), pidstr
.c_str (),
8687 string_field ("exit-code", exit_code_str
.c_str ()));
8691 if (uiout
->is_mi_like_p ())
8693 ("reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY
));
8694 uiout
->message ("[Inferior %s (%s) exited normally]\n",
8695 plongest (inf
->num
), pidstr
.c_str ());
8700 print_signal_received_reason (struct ui_out
*uiout
, enum gdb_signal siggnal
)
8702 struct thread_info
*thr
= inferior_thread ();
8704 infrun_debug_printf ("signal = %s", gdb_signal_to_string (siggnal
));
8708 if (uiout
->is_mi_like_p ())
8710 else if (show_thread_that_caused_stop ())
8712 uiout
->text ("\nThread ");
8713 uiout
->field_string ("thread-id", print_thread_id (thr
));
8715 const char *name
= thread_name (thr
);
8716 if (name
!= nullptr)
8718 uiout
->text (" \"");
8719 uiout
->field_string ("name", name
);
8724 uiout
->text ("\nProgram");
8726 if (siggnal
== GDB_SIGNAL_0
&& !uiout
->is_mi_like_p ())
8727 uiout
->text (" stopped");
8730 uiout
->text (" received signal ");
8731 annotate_signal_name ();
8732 if (uiout
->is_mi_like_p ())
8734 ("reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED
));
8735 uiout
->field_string ("signal-name", gdb_signal_to_name (siggnal
));
8736 annotate_signal_name_end ();
8738 annotate_signal_string ();
8739 uiout
->field_string ("signal-meaning", gdb_signal_to_string (siggnal
));
8741 struct regcache
*regcache
= get_current_regcache ();
8742 struct gdbarch
*gdbarch
= regcache
->arch ();
8743 if (gdbarch_report_signal_info_p (gdbarch
))
8744 gdbarch_report_signal_info (gdbarch
, uiout
, siggnal
);
8746 annotate_signal_string_end ();
8748 uiout
->text (".\n");
8752 print_no_history_reason (struct ui_out
*uiout
)
8754 if (uiout
->is_mi_like_p ())
8755 uiout
->field_string ("reason", async_reason_lookup (EXEC_ASYNC_NO_HISTORY
));
8757 uiout
->text ("\nNo more reverse-execution history.\n");
8760 /* Print current location without a level number, if we have changed
8761 functions or hit a breakpoint. Print source line if we have one.
8762 bpstat_print contains the logic deciding in detail what to print,
8763 based on the event(s) that just occurred. */
8766 print_stop_location (const target_waitstatus
&ws
)
8769 enum print_what source_flag
;
8770 int do_frame_printing
= 1;
8771 struct thread_info
*tp
= inferior_thread ();
8773 bpstat_ret
= bpstat_print (tp
->control
.stop_bpstat
, ws
.kind ());
8777 /* FIXME: cagney/2002-12-01: Given that a frame ID does (or
8778 should) carry around the function and does (or should) use
8779 that when doing a frame comparison. */
8780 if (tp
->control
.stop_step
8781 && (tp
->control
.step_frame_id
8782 == get_frame_id (get_current_frame ()))
8783 && (tp
->control
.step_start_function
8784 == find_pc_function (tp
->stop_pc ())))
8786 /* Finished step, just print source line. */
8787 source_flag
= SRC_LINE
;
8791 /* Print location and source line. */
8792 source_flag
= SRC_AND_LOC
;
8795 case PRINT_SRC_AND_LOC
:
8796 /* Print location and source line. */
8797 source_flag
= SRC_AND_LOC
;
8799 case PRINT_SRC_ONLY
:
8800 source_flag
= SRC_LINE
;
8803 /* Something bogus. */
8804 source_flag
= SRC_LINE
;
8805 do_frame_printing
= 0;
8808 internal_error (_("Unknown value."));
8811 /* The behavior of this routine with respect to the source
8813 SRC_LINE: Print only source line
8814 LOCATION: Print only location
8815 SRC_AND_LOC: Print location and source line. */
8816 if (do_frame_printing
)
8817 print_stack_frame (get_selected_frame (nullptr), 0, source_flag
, 1);
8823 print_stop_event (struct ui_out
*uiout
, bool displays
)
8825 struct target_waitstatus last
;
8826 struct thread_info
*tp
;
8828 get_last_target_status (nullptr, nullptr, &last
);
8831 scoped_restore save_uiout
= make_scoped_restore (¤t_uiout
, uiout
);
8833 print_stop_location (last
);
8835 /* Display the auto-display expressions. */
8840 tp
= inferior_thread ();
8841 if (tp
->thread_fsm () != nullptr
8842 && tp
->thread_fsm ()->finished_p ())
8844 struct return_value_info
*rv
;
8846 rv
= tp
->thread_fsm ()->return_value ();
8848 print_return_value (uiout
, rv
);
8855 maybe_remove_breakpoints (void)
8857 if (!breakpoints_should_be_inserted_now () && target_has_execution ())
8859 if (remove_breakpoints ())
8861 target_terminal::ours_for_output ();
8862 gdb_printf (_("Cannot remove breakpoints because "
8863 "program is no longer writable.\nFurther "
8864 "execution is probably impossible.\n"));
8869 /* The execution context that just caused a normal stop. */
8875 DISABLE_COPY_AND_ASSIGN (stop_context
);
8877 bool changed () const;
8882 /* The event PTID. */
8886 /* If stopp for a thread event, this is the thread that caused the
8888 thread_info_ref thread
;
8890 /* The inferior that caused the stop. */
8894 /* Initializes a new stop context. If stopped for a thread event, this
8895 takes a strong reference to the thread. */
8897 stop_context::stop_context ()
8899 stop_id
= get_stop_id ();
8900 ptid
= inferior_ptid
;
8901 inf_num
= current_inferior ()->num
;
8903 if (inferior_ptid
!= null_ptid
)
8905 /* Take a strong reference so that the thread can't be deleted
8907 thread
= thread_info_ref::new_reference (inferior_thread ());
8911 /* Return true if the current context no longer matches the saved stop
8915 stop_context::changed () const
8917 if (ptid
!= inferior_ptid
)
8919 if (inf_num
!= current_inferior ()->num
)
8921 if (thread
!= nullptr && thread
->state
!= THREAD_STOPPED
)
8923 if (get_stop_id () != stop_id
)
8933 struct target_waitstatus last
;
8935 get_last_target_status (nullptr, nullptr, &last
);
8939 /* If an exception is thrown from this point on, make sure to
8940 propagate GDB's knowledge of the executing state to the
8941 frontend/user running state. A QUIT is an easy exception to see
8942 here, so do this before any filtered output. */
8944 ptid_t finish_ptid
= null_ptid
;
8947 finish_ptid
= minus_one_ptid
;
8948 else if (last
.kind () == TARGET_WAITKIND_SIGNALLED
8949 || last
.kind () == TARGET_WAITKIND_EXITED
)
8951 /* On some targets, we may still have live threads in the
8952 inferior when we get a process exit event. E.g., for
8953 "checkpoint", when the current checkpoint/fork exits,
8954 linux-fork.c automatically switches to another fork from
8955 within target_mourn_inferior. */
8956 if (inferior_ptid
!= null_ptid
)
8957 finish_ptid
= ptid_t (inferior_ptid
.pid ());
8959 else if (last
.kind () != TARGET_WAITKIND_NO_RESUMED
)
8960 finish_ptid
= inferior_ptid
;
8962 gdb::optional
<scoped_finish_thread_state
> maybe_finish_thread_state
;
8963 if (finish_ptid
!= null_ptid
)
8965 maybe_finish_thread_state
.emplace
8966 (user_visible_resume_target (finish_ptid
), finish_ptid
);
8969 /* As we're presenting a stop, and potentially removing breakpoints,
8970 update the thread list so we can tell whether there are threads
8971 running on the target. With target remote, for example, we can
8972 only learn about new threads when we explicitly update the thread
8973 list. Do this before notifying the interpreters about signal
8974 stops, end of stepping ranges, etc., so that the "new thread"
8975 output is emitted before e.g., "Program received signal FOO",
8976 instead of after. */
8977 update_thread_list ();
8979 if (last
.kind () == TARGET_WAITKIND_STOPPED
&& stopped_by_random_signal
)
8980 notify_signal_received (inferior_thread ()->stop_signal ());
8982 /* As with the notification of thread events, we want to delay
8983 notifying the user that we've switched thread context until
8984 the inferior actually stops.
8986 There's no point in saying anything if the inferior has exited.
8987 Note that SIGNALLED here means "exited with a signal", not
8988 "received a signal".
8990 Also skip saying anything in non-stop mode. In that mode, as we
8991 don't want GDB to switch threads behind the user's back, to avoid
8992 races where the user is typing a command to apply to thread x,
8993 but GDB switches to thread y before the user finishes entering
8994 the command, fetch_inferior_event installs a cleanup to restore
8995 the current thread back to the thread the user had selected right
8996 after this event is handled, so we're not really switching, only
8997 informing of a stop. */
9000 if ((last
.kind () != TARGET_WAITKIND_SIGNALLED
9001 && last
.kind () != TARGET_WAITKIND_EXITED
9002 && last
.kind () != TARGET_WAITKIND_NO_RESUMED
)
9003 && target_has_execution ()
9004 && previous_thread
!= inferior_thread ())
9006 SWITCH_THRU_ALL_UIS ()
9008 target_terminal::ours_for_output ();
9009 gdb_printf (_("[Switching to %s]\n"),
9010 target_pid_to_str (inferior_ptid
).c_str ());
9011 annotate_thread_changed ();
9015 update_previous_thread ();
9018 if (last
.kind () == TARGET_WAITKIND_NO_RESUMED
)
9020 SWITCH_THRU_ALL_UIS ()
9021 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
9023 target_terminal::ours_for_output ();
9024 gdb_printf (_("No unwaited-for children left.\n"));
9028 /* Note: this depends on the update_thread_list call above. */
9029 maybe_remove_breakpoints ();
9031 /* If an auto-display called a function and that got a signal,
9032 delete that auto-display to avoid an infinite recursion. */
9034 if (stopped_by_random_signal
)
9035 disable_current_display ();
9037 SWITCH_THRU_ALL_UIS ()
9039 async_enable_stdin ();
9042 /* Let the user/frontend see the threads as stopped. */
9043 maybe_finish_thread_state
.reset ();
9045 /* Select innermost stack frame - i.e., current frame is frame 0,
9046 and current location is based on that. Handle the case where the
9047 dummy call is returning after being stopped. E.g. the dummy call
9048 previously hit a breakpoint. (If the dummy call returns
9049 normally, we won't reach here.) Do this before the stop hook is
9050 run, so that it doesn't get to see the temporary dummy frame,
9051 which is not where we'll present the stop. */
9052 if (has_stack_frames ())
9054 if (stop_stack_dummy
== STOP_STACK_DUMMY
)
9056 /* Pop the empty frame that contains the stack dummy. This
9057 also restores inferior state prior to the call (struct
9058 infcall_suspend_state). */
9059 frame_info_ptr frame
= get_current_frame ();
9061 gdb_assert (get_frame_type (frame
) == DUMMY_FRAME
);
9063 /* frame_pop calls reinit_frame_cache as the last thing it
9064 does which means there's now no selected frame. */
9067 select_frame (get_current_frame ());
9069 /* Set the current source location. */
9070 set_current_sal_from_frame (get_current_frame ());
9073 /* Look up the hook_stop and run it (CLI internally handles problem
9074 of stop_command's pre-hook not existing). */
9075 stop_context saved_context
;
9079 execute_cmd_pre_hook (stop_command
);
9081 catch (const gdb_exception_error
&ex
)
9083 exception_fprintf (gdb_stderr
, ex
,
9084 "Error while running hook_stop:\n");
9087 /* If the stop hook resumes the target, then there's no point in
9088 trying to notify about the previous stop; its context is
9089 gone. Likewise if the command switches thread or inferior --
9090 the observers would print a stop for the wrong
9092 if (saved_context
.changed ())
9095 /* Notify observers about the stop. This is where the interpreters
9096 print the stop event. */
9097 notify_normal_stop ((inferior_ptid
!= null_ptid
9098 ? inferior_thread ()->control
.stop_bpstat
9101 annotate_stopped ();
9103 if (target_has_execution ())
9105 if (last
.kind () != TARGET_WAITKIND_SIGNALLED
9106 && last
.kind () != TARGET_WAITKIND_EXITED
9107 && last
.kind () != TARGET_WAITKIND_NO_RESUMED
)
9108 /* Delete the breakpoint we stopped at, if it wants to be deleted.
9109 Delete any breakpoint that is to be deleted at the next stop. */
9110 breakpoint_auto_delete (inferior_thread ()->control
.stop_bpstat
);
9117 signal_stop_state (int signo
)
9119 return signal_stop
[signo
];
9123 signal_print_state (int signo
)
9125 return signal_print
[signo
];
9129 signal_pass_state (int signo
)
9131 return signal_program
[signo
];
9135 signal_cache_update (int signo
)
9139 for (signo
= 0; signo
< (int) GDB_SIGNAL_LAST
; signo
++)
9140 signal_cache_update (signo
);
9145 signal_pass
[signo
] = (signal_stop
[signo
] == 0
9146 && signal_print
[signo
] == 0
9147 && signal_program
[signo
] == 1
9148 && signal_catch
[signo
] == 0);
9152 signal_stop_update (int signo
, int state
)
9154 int ret
= signal_stop
[signo
];
9156 signal_stop
[signo
] = state
;
9157 signal_cache_update (signo
);
9162 signal_print_update (int signo
, int state
)
9164 int ret
= signal_print
[signo
];
9166 signal_print
[signo
] = state
;
9167 signal_cache_update (signo
);
9172 signal_pass_update (int signo
, int state
)
9174 int ret
= signal_program
[signo
];
9176 signal_program
[signo
] = state
;
9177 signal_cache_update (signo
);
9181 /* Update the global 'signal_catch' from INFO and notify the
9185 signal_catch_update (const unsigned int *info
)
9189 for (i
= 0; i
< GDB_SIGNAL_LAST
; ++i
)
9190 signal_catch
[i
] = info
[i
] > 0;
9191 signal_cache_update (-1);
9192 target_pass_signals (signal_pass
);
9196 sig_print_header (void)
9198 gdb_printf (_("Signal Stop\tPrint\tPass "
9199 "to program\tDescription\n"));
9203 sig_print_info (enum gdb_signal oursig
)
9205 const char *name
= gdb_signal_to_name (oursig
);
9206 int name_padding
= 13 - strlen (name
);
9208 if (name_padding
<= 0)
9211 gdb_printf ("%s", name
);
9212 gdb_printf ("%*.*s ", name_padding
, name_padding
, " ");
9213 gdb_printf ("%s\t", signal_stop
[oursig
] ? "Yes" : "No");
9214 gdb_printf ("%s\t", signal_print
[oursig
] ? "Yes" : "No");
9215 gdb_printf ("%s\t\t", signal_program
[oursig
] ? "Yes" : "No");
9216 gdb_printf ("%s\n", gdb_signal_to_string (oursig
));
9219 /* Specify how various signals in the inferior should be handled. */
9222 handle_command (const char *args
, int from_tty
)
9224 int digits
, wordlen
;
9225 int sigfirst
, siglast
;
9226 enum gdb_signal oursig
;
9229 if (args
== nullptr)
9231 error_no_arg (_("signal to handle"));
9234 /* Allocate and zero an array of flags for which signals to handle. */
9236 const size_t nsigs
= GDB_SIGNAL_LAST
;
9237 unsigned char sigs
[nsigs
] {};
9239 /* Break the command line up into args. */
9241 gdb_argv
built_argv (args
);
9243 /* Walk through the args, looking for signal oursigs, signal names, and
9244 actions. Signal numbers and signal names may be interspersed with
9245 actions, with the actions being performed for all signals cumulatively
9246 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
9248 for (char *arg
: built_argv
)
9250 wordlen
= strlen (arg
);
9251 for (digits
= 0; isdigit (arg
[digits
]); digits
++)
9255 sigfirst
= siglast
= -1;
9257 if (wordlen
>= 1 && !strncmp (arg
, "all", wordlen
))
9259 /* Apply action to all signals except those used by the
9260 debugger. Silently skip those. */
9263 siglast
= nsigs
- 1;
9265 else if (wordlen
>= 1 && !strncmp (arg
, "stop", wordlen
))
9267 SET_SIGS (nsigs
, sigs
, signal_stop
);
9268 SET_SIGS (nsigs
, sigs
, signal_print
);
9270 else if (wordlen
>= 1 && !strncmp (arg
, "ignore", wordlen
))
9272 UNSET_SIGS (nsigs
, sigs
, signal_program
);
9274 else if (wordlen
>= 2 && !strncmp (arg
, "print", wordlen
))
9276 SET_SIGS (nsigs
, sigs
, signal_print
);
9278 else if (wordlen
>= 2 && !strncmp (arg
, "pass", wordlen
))
9280 SET_SIGS (nsigs
, sigs
, signal_program
);
9282 else if (wordlen
>= 3 && !strncmp (arg
, "nostop", wordlen
))
9284 UNSET_SIGS (nsigs
, sigs
, signal_stop
);
9286 else if (wordlen
>= 3 && !strncmp (arg
, "noignore", wordlen
))
9288 SET_SIGS (nsigs
, sigs
, signal_program
);
9290 else if (wordlen
>= 4 && !strncmp (arg
, "noprint", wordlen
))
9292 UNSET_SIGS (nsigs
, sigs
, signal_print
);
9293 UNSET_SIGS (nsigs
, sigs
, signal_stop
);
9295 else if (wordlen
>= 4 && !strncmp (arg
, "nopass", wordlen
))
9297 UNSET_SIGS (nsigs
, sigs
, signal_program
);
9299 else if (digits
> 0)
9301 /* It is numeric. The numeric signal refers to our own
9302 internal signal numbering from target.h, not to host/target
9303 signal number. This is a feature; users really should be
9304 using symbolic names anyway, and the common ones like
9305 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
9307 sigfirst
= siglast
= (int)
9308 gdb_signal_from_command (atoi (arg
));
9309 if (arg
[digits
] == '-')
9312 gdb_signal_from_command (atoi (arg
+ digits
+ 1));
9314 if (sigfirst
> siglast
)
9316 /* Bet he didn't figure we'd think of this case... */
9317 std::swap (sigfirst
, siglast
);
9322 oursig
= gdb_signal_from_name (arg
);
9323 if (oursig
!= GDB_SIGNAL_UNKNOWN
)
9325 sigfirst
= siglast
= (int) oursig
;
9329 /* Not a number and not a recognized flag word => complain. */
9330 error (_("Unrecognized or ambiguous flag word: \"%s\"."), arg
);
9334 /* If any signal numbers or symbol names were found, set flags for
9335 which signals to apply actions to. */
9337 for (int signum
= sigfirst
; signum
>= 0 && signum
<= siglast
; signum
++)
9339 switch ((enum gdb_signal
) signum
)
9341 case GDB_SIGNAL_TRAP
:
9342 case GDB_SIGNAL_INT
:
9343 if (!allsigs
&& !sigs
[signum
])
9345 if (query (_("%s is used by the debugger.\n\
9346 Are you sure you want to change it? "),
9347 gdb_signal_to_name ((enum gdb_signal
) signum
)))
9352 gdb_printf (_("Not confirmed, unchanged.\n"));
9356 case GDB_SIGNAL_DEFAULT
:
9357 case GDB_SIGNAL_UNKNOWN
:
9358 /* Make sure that "all" doesn't print these. */
9367 for (int signum
= 0; signum
< nsigs
; signum
++)
9370 signal_cache_update (-1);
9371 target_pass_signals (signal_pass
);
9372 target_program_signals (signal_program
);
9376 /* Show the results. */
9377 sig_print_header ();
9378 for (; signum
< nsigs
; signum
++)
9380 sig_print_info ((enum gdb_signal
) signum
);
9387 /* Complete the "handle" command. */
9390 handle_completer (struct cmd_list_element
*ignore
,
9391 completion_tracker
&tracker
,
9392 const char *text
, const char *word
)
9394 static const char * const keywords
[] =
9408 signal_completer (ignore
, tracker
, text
, word
);
9409 complete_on_enum (tracker
, keywords
, word
, word
);
9413 gdb_signal_from_command (int num
)
9415 if (num
>= 1 && num
<= 15)
9416 return (enum gdb_signal
) num
;
9417 error (_("Only signals 1-15 are valid as numeric signals.\n\
9418 Use \"info signals\" for a list of symbolic signals."));
9421 /* Print current contents of the tables set by the handle command.
9422 It is possible we should just be printing signals actually used
9423 by the current target (but for things to work right when switching
9424 targets, all signals should be in the signal tables). */
9427 info_signals_command (const char *signum_exp
, int from_tty
)
9429 enum gdb_signal oursig
;
9431 sig_print_header ();
9435 /* First see if this is a symbol name. */
9436 oursig
= gdb_signal_from_name (signum_exp
);
9437 if (oursig
== GDB_SIGNAL_UNKNOWN
)
9439 /* No, try numeric. */
9441 gdb_signal_from_command (parse_and_eval_long (signum_exp
));
9443 sig_print_info (oursig
);
9448 /* These ugly casts brought to you by the native VAX compiler. */
9449 for (oursig
= GDB_SIGNAL_FIRST
;
9450 (int) oursig
< (int) GDB_SIGNAL_LAST
;
9451 oursig
= (enum gdb_signal
) ((int) oursig
+ 1))
9455 if (oursig
!= GDB_SIGNAL_UNKNOWN
9456 && oursig
!= GDB_SIGNAL_DEFAULT
&& oursig
!= GDB_SIGNAL_0
)
9457 sig_print_info (oursig
);
9460 gdb_printf (_("\nUse the \"handle\" command "
9461 "to change these tables.\n"));
9464 /* The $_siginfo convenience variable is a bit special. We don't know
9465 for sure the type of the value until we actually have a chance to
9466 fetch the data. The type can change depending on gdbarch, so it is
9467 also dependent on which thread you have selected.
9469 1. making $_siginfo be an internalvar that creates a new value on
9472 2. making the value of $_siginfo be an lval_computed value. */
9474 /* This function implements the lval_computed support for reading a
9478 siginfo_value_read (struct value
*v
)
9480 LONGEST transferred
;
9482 /* If we can access registers, so can we access $_siginfo. Likewise
9484 validate_registers_access ();
9487 target_read (current_inferior ()->top_target (),
9488 TARGET_OBJECT_SIGNAL_INFO
,
9490 v
->contents_all_raw ().data (),
9492 v
->type ()->length ());
9494 if (transferred
!= v
->type ()->length ())
9495 error (_("Unable to read siginfo"));
9498 /* This function implements the lval_computed support for writing a
9502 siginfo_value_write (struct value
*v
, struct value
*fromval
)
9504 LONGEST transferred
;
9506 /* If we can access registers, so can we access $_siginfo. Likewise
9508 validate_registers_access ();
9510 transferred
= target_write (current_inferior ()->top_target (),
9511 TARGET_OBJECT_SIGNAL_INFO
,
9513 fromval
->contents_all_raw ().data (),
9515 fromval
->type ()->length ());
9517 if (transferred
!= fromval
->type ()->length ())
9518 error (_("Unable to write siginfo"));
9521 static const struct lval_funcs siginfo_value_funcs
=
9527 /* Return a new value with the correct type for the siginfo object of
9528 the current thread using architecture GDBARCH. Return a void value
9529 if there's no object available. */
9531 static struct value
*
9532 siginfo_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
9535 if (target_has_stack ()
9536 && inferior_ptid
!= null_ptid
9537 && gdbarch_get_siginfo_type_p (gdbarch
))
9539 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9541 return value::allocate_computed (type
, &siginfo_value_funcs
, nullptr);
9544 return value::allocate (builtin_type (gdbarch
)->builtin_void
);
9548 /* infcall_suspend_state contains state about the program itself like its
9549 registers and any signal it received when it last stopped.
9550 This state must be restored regardless of how the inferior function call
9551 ends (either successfully, or after it hits a breakpoint or signal)
9552 if the program is to properly continue where it left off. */
9554 class infcall_suspend_state
9557 /* Capture state from GDBARCH, TP, and REGCACHE that must be restored
9558 once the inferior function call has finished. */
9559 infcall_suspend_state (struct gdbarch
*gdbarch
,
9560 const struct thread_info
*tp
,
9561 struct regcache
*regcache
)
9562 : m_registers (new readonly_detached_regcache (*regcache
))
9564 tp
->save_suspend_to (m_thread_suspend
);
9566 gdb::unique_xmalloc_ptr
<gdb_byte
> siginfo_data
;
9568 if (gdbarch_get_siginfo_type_p (gdbarch
))
9570 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9571 size_t len
= type
->length ();
9573 siginfo_data
.reset ((gdb_byte
*) xmalloc (len
));
9575 if (target_read (current_inferior ()->top_target (),
9576 TARGET_OBJECT_SIGNAL_INFO
, nullptr,
9577 siginfo_data
.get (), 0, len
) != len
)
9579 /* Errors ignored. */
9580 siginfo_data
.reset (nullptr);
9586 m_siginfo_gdbarch
= gdbarch
;
9587 m_siginfo_data
= std::move (siginfo_data
);
9591 /* Return a pointer to the stored register state. */
9593 readonly_detached_regcache
*registers () const
9595 return m_registers
.get ();
9598 /* Restores the stored state into GDBARCH, TP, and REGCACHE. */
9600 void restore (struct gdbarch
*gdbarch
,
9601 struct thread_info
*tp
,
9602 struct regcache
*regcache
) const
9604 tp
->restore_suspend_from (m_thread_suspend
);
9606 if (m_siginfo_gdbarch
== gdbarch
)
9608 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9610 /* Errors ignored. */
9611 target_write (current_inferior ()->top_target (),
9612 TARGET_OBJECT_SIGNAL_INFO
, nullptr,
9613 m_siginfo_data
.get (), 0, type
->length ());
9616 /* The inferior can be gone if the user types "print exit(0)"
9617 (and perhaps other times). */
9618 if (target_has_execution ())
9619 /* NB: The register write goes through to the target. */
9620 regcache
->restore (registers ());
9624 /* How the current thread stopped before the inferior function call was
9626 struct thread_suspend_state m_thread_suspend
;
9628 /* The registers before the inferior function call was executed. */
9629 std::unique_ptr
<readonly_detached_regcache
> m_registers
;
9631 /* Format of SIGINFO_DATA or NULL if it is not present. */
9632 struct gdbarch
*m_siginfo_gdbarch
= nullptr;
9634 /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
9635 gdbarch_get_siginfo_type ()->length (). For different gdbarch the
9636 content would be invalid. */
9637 gdb::unique_xmalloc_ptr
<gdb_byte
> m_siginfo_data
;
9640 infcall_suspend_state_up
9641 save_infcall_suspend_state ()
9643 struct thread_info
*tp
= inferior_thread ();
9644 struct regcache
*regcache
= get_current_regcache ();
9645 struct gdbarch
*gdbarch
= regcache
->arch ();
9647 infcall_suspend_state_up inf_state
9648 (new struct infcall_suspend_state (gdbarch
, tp
, regcache
));
9650 /* Having saved the current state, adjust the thread state, discarding
9651 any stop signal information. The stop signal is not useful when
9652 starting an inferior function call, and run_inferior_call will not use
9653 the signal due to its `proceed' call with GDB_SIGNAL_0. */
9654 tp
->set_stop_signal (GDB_SIGNAL_0
);
9659 /* Restore inferior session state to INF_STATE. */
9662 restore_infcall_suspend_state (struct infcall_suspend_state
*inf_state
)
9664 struct thread_info
*tp
= inferior_thread ();
9665 struct regcache
*regcache
= get_current_regcache ();
9666 struct gdbarch
*gdbarch
= regcache
->arch ();
9668 inf_state
->restore (gdbarch
, tp
, regcache
);
9669 discard_infcall_suspend_state (inf_state
);
9673 discard_infcall_suspend_state (struct infcall_suspend_state
*inf_state
)
9678 readonly_detached_regcache
*
9679 get_infcall_suspend_state_regcache (struct infcall_suspend_state
*inf_state
)
9681 return inf_state
->registers ();
9684 /* infcall_control_state contains state regarding gdb's control of the
9685 inferior itself like stepping control. It also contains session state like
9686 the user's currently selected frame. */
9688 struct infcall_control_state
9690 struct thread_control_state thread_control
;
9691 struct inferior_control_state inferior_control
;
9694 enum stop_stack_kind stop_stack_dummy
= STOP_NONE
;
9695 int stopped_by_random_signal
= 0;
9697 /* ID and level of the selected frame when the inferior function
9699 struct frame_id selected_frame_id
{};
9700 int selected_frame_level
= -1;
9703 /* Save all of the information associated with the inferior<==>gdb
9706 infcall_control_state_up
9707 save_infcall_control_state ()
9709 infcall_control_state_up
inf_status (new struct infcall_control_state
);
9710 struct thread_info
*tp
= inferior_thread ();
9711 struct inferior
*inf
= current_inferior ();
9713 inf_status
->thread_control
= tp
->control
;
9714 inf_status
->inferior_control
= inf
->control
;
9716 tp
->control
.step_resume_breakpoint
= nullptr;
9717 tp
->control
.exception_resume_breakpoint
= nullptr;
9719 /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
9720 chain. If caller's caller is walking the chain, they'll be happier if we
9721 hand them back the original chain when restore_infcall_control_state is
9723 tp
->control
.stop_bpstat
= bpstat_copy (tp
->control
.stop_bpstat
);
9726 inf_status
->stop_stack_dummy
= stop_stack_dummy
;
9727 inf_status
->stopped_by_random_signal
= stopped_by_random_signal
;
9729 save_selected_frame (&inf_status
->selected_frame_id
,
9730 &inf_status
->selected_frame_level
);
9735 /* Restore inferior session state to INF_STATUS. */
9738 restore_infcall_control_state (struct infcall_control_state
*inf_status
)
9740 struct thread_info
*tp
= inferior_thread ();
9741 struct inferior
*inf
= current_inferior ();
9743 if (tp
->control
.step_resume_breakpoint
)
9744 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
9746 if (tp
->control
.exception_resume_breakpoint
)
9747 tp
->control
.exception_resume_breakpoint
->disposition
9748 = disp_del_at_next_stop
;
9750 /* Handle the bpstat_copy of the chain. */
9751 bpstat_clear (&tp
->control
.stop_bpstat
);
9753 tp
->control
= inf_status
->thread_control
;
9754 inf
->control
= inf_status
->inferior_control
;
9757 stop_stack_dummy
= inf_status
->stop_stack_dummy
;
9758 stopped_by_random_signal
= inf_status
->stopped_by_random_signal
;
9760 if (target_has_stack ())
9762 restore_selected_frame (inf_status
->selected_frame_id
,
9763 inf_status
->selected_frame_level
);
9770 discard_infcall_control_state (struct infcall_control_state
*inf_status
)
9772 if (inf_status
->thread_control
.step_resume_breakpoint
)
9773 inf_status
->thread_control
.step_resume_breakpoint
->disposition
9774 = disp_del_at_next_stop
;
9776 if (inf_status
->thread_control
.exception_resume_breakpoint
)
9777 inf_status
->thread_control
.exception_resume_breakpoint
->disposition
9778 = disp_del_at_next_stop
;
9780 /* See save_infcall_control_state for info on stop_bpstat. */
9781 bpstat_clear (&inf_status
->thread_control
.stop_bpstat
);
9789 clear_exit_convenience_vars (void)
9791 clear_internalvar (lookup_internalvar ("_exitsignal"));
9792 clear_internalvar (lookup_internalvar ("_exitcode"));
9796 /* User interface for reverse debugging:
9797 Set exec-direction / show exec-direction commands
9798 (returns error unless target implements to_set_exec_direction method). */
9800 enum exec_direction_kind execution_direction
= EXEC_FORWARD
;
9801 static const char exec_forward
[] = "forward";
9802 static const char exec_reverse
[] = "reverse";
9803 static const char *exec_direction
= exec_forward
;
9804 static const char *const exec_direction_names
[] = {
9811 set_exec_direction_func (const char *args
, int from_tty
,
9812 struct cmd_list_element
*cmd
)
9814 if (target_can_execute_reverse ())
9816 if (!strcmp (exec_direction
, exec_forward
))
9817 execution_direction
= EXEC_FORWARD
;
9818 else if (!strcmp (exec_direction
, exec_reverse
))
9819 execution_direction
= EXEC_REVERSE
;
9823 exec_direction
= exec_forward
;
9824 error (_("Target does not support this operation."));
9829 show_exec_direction_func (struct ui_file
*out
, int from_tty
,
9830 struct cmd_list_element
*cmd
, const char *value
)
9832 switch (execution_direction
) {
9834 gdb_printf (out
, _("Forward.\n"));
9837 gdb_printf (out
, _("Reverse.\n"));
9840 internal_error (_("bogus execution_direction value: %d"),
9841 (int) execution_direction
);
9846 show_schedule_multiple (struct ui_file
*file
, int from_tty
,
9847 struct cmd_list_element
*c
, const char *value
)
9849 gdb_printf (file
, _("Resuming the execution of threads "
9850 "of all processes is %s.\n"), value
);
9853 /* Implementation of `siginfo' variable. */
9855 static const struct internalvar_funcs siginfo_funcs
=
9861 /* Callback for infrun's target events source. This is marked when a
9862 thread has a pending status to process. */
9865 infrun_async_inferior_event_handler (gdb_client_data data
)
9867 clear_async_event_handler (infrun_async_inferior_event_token
);
9868 inferior_event_handler (INF_REG_EVENT
);
9875 /* Verify that when two threads with the same ptid exist (from two different
9876 targets) and one of them changes ptid, we only update inferior_ptid if
9877 it is appropriate. */
9880 infrun_thread_ptid_changed ()
9882 gdbarch
*arch
= current_inferior ()->arch ();
9884 /* The thread which inferior_ptid represents changes ptid. */
9886 scoped_restore_current_pspace_and_thread restore
;
9888 scoped_mock_context
<test_target_ops
> target1 (arch
);
9889 scoped_mock_context
<test_target_ops
> target2 (arch
);
9891 ptid_t
old_ptid (111, 222);
9892 ptid_t
new_ptid (111, 333);
9894 target1
.mock_inferior
.pid
= old_ptid
.pid ();
9895 target1
.mock_thread
.ptid
= old_ptid
;
9896 target1
.mock_inferior
.ptid_thread_map
.clear ();
9897 target1
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target1
.mock_thread
;
9899 target2
.mock_inferior
.pid
= old_ptid
.pid ();
9900 target2
.mock_thread
.ptid
= old_ptid
;
9901 target2
.mock_inferior
.ptid_thread_map
.clear ();
9902 target2
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target2
.mock_thread
;
9904 auto restore_inferior_ptid
= make_scoped_restore (&inferior_ptid
, old_ptid
);
9905 set_current_inferior (&target1
.mock_inferior
);
9907 thread_change_ptid (&target1
.mock_target
, old_ptid
, new_ptid
);
9909 gdb_assert (inferior_ptid
== new_ptid
);
9912 /* A thread with the same ptid as inferior_ptid, but from another target,
9915 scoped_restore_current_pspace_and_thread restore
;
9917 scoped_mock_context
<test_target_ops
> target1 (arch
);
9918 scoped_mock_context
<test_target_ops
> target2 (arch
);
9920 ptid_t
old_ptid (111, 222);
9921 ptid_t
new_ptid (111, 333);
9923 target1
.mock_inferior
.pid
= old_ptid
.pid ();
9924 target1
.mock_thread
.ptid
= old_ptid
;
9925 target1
.mock_inferior
.ptid_thread_map
.clear ();
9926 target1
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target1
.mock_thread
;
9928 target2
.mock_inferior
.pid
= old_ptid
.pid ();
9929 target2
.mock_thread
.ptid
= old_ptid
;
9930 target2
.mock_inferior
.ptid_thread_map
.clear ();
9931 target2
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target2
.mock_thread
;
9933 auto restore_inferior_ptid
= make_scoped_restore (&inferior_ptid
, old_ptid
);
9934 set_current_inferior (&target2
.mock_inferior
);
9936 thread_change_ptid (&target1
.mock_target
, old_ptid
, new_ptid
);
9938 gdb_assert (inferior_ptid
== old_ptid
);
9942 } /* namespace selftests */
9944 #endif /* GDB_SELF_TEST */
9946 void _initialize_infrun ();
9948 _initialize_infrun ()
9950 struct cmd_list_element
*c
;
9952 /* Register extra event sources in the event loop. */
9953 infrun_async_inferior_event_token
9954 = create_async_event_handler (infrun_async_inferior_event_handler
, nullptr,
9957 cmd_list_element
*info_signals_cmd
9958 = add_info ("signals", info_signals_command
, _("\
9959 What debugger does when program gets various signals.\n\
9960 Specify a signal as argument to print info on that signal only."));
9961 add_info_alias ("handle", info_signals_cmd
, 0);
9963 c
= add_com ("handle", class_run
, handle_command
, _("\
9964 Specify how to handle signals.\n\
9965 Usage: handle SIGNAL [ACTIONS]\n\
9966 Args are signals and actions to apply to those signals.\n\
9967 If no actions are specified, the current settings for the specified signals\n\
9968 will be displayed instead.\n\
9970 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
9971 from 1-15 are allowed for compatibility with old versions of GDB.\n\
9972 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
9973 The special arg \"all\" is recognized to mean all signals except those\n\
9974 used by the debugger, typically SIGTRAP and SIGINT.\n\
9976 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
9977 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
9978 Stop means reenter debugger if this signal happens (implies print).\n\
9979 Print means print a message if this signal happens.\n\
9980 Pass means let program see this signal; otherwise program doesn't know.\n\
9981 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
9982 Pass and Stop may be combined.\n\
9984 Multiple signals may be specified. Signal numbers and signal names\n\
9985 may be interspersed with actions, with the actions being performed for\n\
9986 all signals cumulatively specified."));
9987 set_cmd_completer (c
, handle_completer
);
9989 stop_command
= add_cmd ("stop", class_obscure
,
9990 not_just_help_class_command
, _("\
9991 There is no `stop' command, but you can set a hook on `stop'.\n\
9992 This allows you to set a list of commands to be run each time execution\n\
9993 of the program stops."), &cmdlist
);
9995 add_setshow_boolean_cmd
9996 ("infrun", class_maintenance
, &debug_infrun
,
9997 _("Set inferior debugging."),
9998 _("Show inferior debugging."),
9999 _("When non-zero, inferior specific debugging is enabled."),
10000 nullptr, show_debug_infrun
, &setdebuglist
, &showdebuglist
);
10002 add_setshow_boolean_cmd ("non-stop", no_class
,
10004 Set whether gdb controls the inferior in non-stop mode."), _("\
10005 Show whether gdb controls the inferior in non-stop mode."), _("\
10006 When debugging a multi-threaded program and this setting is\n\
10007 off (the default, also called all-stop mode), when one thread stops\n\
10008 (for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
10009 all other threads in the program while you interact with the thread of\n\
10010 interest. When you continue or step a thread, you can allow the other\n\
10011 threads to run, or have them remain stopped, but while you inspect any\n\
10012 thread's state, all threads stop.\n\
10014 In non-stop mode, when one thread stops, other threads can continue\n\
10015 to run freely. You'll be able to step each thread independently,\n\
10016 leave it stopped or free to run as needed."),
10022 for (size_t i
= 0; i
< GDB_SIGNAL_LAST
; i
++)
10024 signal_stop
[i
] = 1;
10025 signal_print
[i
] = 1;
10026 signal_program
[i
] = 1;
10027 signal_catch
[i
] = 0;
10030 /* Signals caused by debugger's own actions should not be given to
10031 the program afterwards.
10033 Do not deliver GDB_SIGNAL_TRAP by default, except when the user
10034 explicitly specifies that it should be delivered to the target
10035 program. Typically, that would occur when a user is debugging a
10036 target monitor on a simulator: the target monitor sets a
10037 breakpoint; the simulator encounters this breakpoint and halts
10038 the simulation handing control to GDB; GDB, noting that the stop
10039 address doesn't map to any known breakpoint, returns control back
10040 to the simulator; the simulator then delivers the hardware
10041 equivalent of a GDB_SIGNAL_TRAP to the program being
10043 signal_program
[GDB_SIGNAL_TRAP
] = 0;
10044 signal_program
[GDB_SIGNAL_INT
] = 0;
10046 /* Signals that are not errors should not normally enter the debugger. */
10047 signal_stop
[GDB_SIGNAL_ALRM
] = 0;
10048 signal_print
[GDB_SIGNAL_ALRM
] = 0;
10049 signal_stop
[GDB_SIGNAL_VTALRM
] = 0;
10050 signal_print
[GDB_SIGNAL_VTALRM
] = 0;
10051 signal_stop
[GDB_SIGNAL_PROF
] = 0;
10052 signal_print
[GDB_SIGNAL_PROF
] = 0;
10053 signal_stop
[GDB_SIGNAL_CHLD
] = 0;
10054 signal_print
[GDB_SIGNAL_CHLD
] = 0;
10055 signal_stop
[GDB_SIGNAL_IO
] = 0;
10056 signal_print
[GDB_SIGNAL_IO
] = 0;
10057 signal_stop
[GDB_SIGNAL_POLL
] = 0;
10058 signal_print
[GDB_SIGNAL_POLL
] = 0;
10059 signal_stop
[GDB_SIGNAL_URG
] = 0;
10060 signal_print
[GDB_SIGNAL_URG
] = 0;
10061 signal_stop
[GDB_SIGNAL_WINCH
] = 0;
10062 signal_print
[GDB_SIGNAL_WINCH
] = 0;
10063 signal_stop
[GDB_SIGNAL_PRIO
] = 0;
10064 signal_print
[GDB_SIGNAL_PRIO
] = 0;
10066 /* These signals are used internally by user-level thread
10067 implementations. (See signal(5) on Solaris.) Like the above
10068 signals, a healthy program receives and handles them as part of
10069 its normal operation. */
10070 signal_stop
[GDB_SIGNAL_LWP
] = 0;
10071 signal_print
[GDB_SIGNAL_LWP
] = 0;
10072 signal_stop
[GDB_SIGNAL_WAITING
] = 0;
10073 signal_print
[GDB_SIGNAL_WAITING
] = 0;
10074 signal_stop
[GDB_SIGNAL_CANCEL
] = 0;
10075 signal_print
[GDB_SIGNAL_CANCEL
] = 0;
10076 signal_stop
[GDB_SIGNAL_LIBRT
] = 0;
10077 signal_print
[GDB_SIGNAL_LIBRT
] = 0;
10079 /* Update cached state. */
10080 signal_cache_update (-1);
10082 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support
,
10083 &stop_on_solib_events
, _("\
10084 Set stopping for shared library events."), _("\
10085 Show stopping for shared library events."), _("\
10086 If nonzero, gdb will give control to the user when the dynamic linker\n\
10087 notifies gdb of shared library events. The most common event of interest\n\
10088 to the user would be loading/unloading of a new library."),
10089 set_stop_on_solib_events
,
10090 show_stop_on_solib_events
,
10091 &setlist
, &showlist
);
10093 add_setshow_enum_cmd ("follow-fork-mode", class_run
,
10094 follow_fork_mode_kind_names
,
10095 &follow_fork_mode_string
, _("\
10096 Set debugger response to a program call of fork or vfork."), _("\
10097 Show debugger response to a program call of fork or vfork."), _("\
10098 A fork or vfork creates a new process. follow-fork-mode can be:\n\
10099 parent - the original process is debugged after a fork\n\
10100 child - the new process is debugged after a fork\n\
10101 The unfollowed process will continue to run.\n\
10102 By default, the debugger will follow the parent process."),
10104 show_follow_fork_mode_string
,
10105 &setlist
, &showlist
);
10107 add_setshow_enum_cmd ("follow-exec-mode", class_run
,
10108 follow_exec_mode_names
,
10109 &follow_exec_mode_string
, _("\
10110 Set debugger response to a program call of exec."), _("\
10111 Show debugger response to a program call of exec."), _("\
10112 An exec call replaces the program image of a process.\n\
10114 follow-exec-mode can be:\n\
10116 new - the debugger creates a new inferior and rebinds the process\n\
10117 to this new inferior. The program the process was running before\n\
10118 the exec call can be restarted afterwards by restarting the original\n\
10121 same - the debugger keeps the process bound to the same inferior.\n\
10122 The new executable image replaces the previous executable loaded in\n\
10123 the inferior. Restarting the inferior after the exec call restarts\n\
10124 the executable the process was running after the exec call.\n\
10126 By default, the debugger will use the same inferior."),
10128 show_follow_exec_mode_string
,
10129 &setlist
, &showlist
);
10131 add_setshow_enum_cmd ("scheduler-locking", class_run
,
10132 scheduler_enums
, &scheduler_mode
, _("\
10133 Set mode for locking scheduler during execution."), _("\
10134 Show mode for locking scheduler during execution."), _("\
10135 off == no locking (threads may preempt at any time)\n\
10136 on == full locking (no thread except the current thread may run)\n\
10137 This applies to both normal execution and replay mode.\n\
10138 step == scheduler locked during stepping commands (step, next, stepi, nexti).\n\
10139 In this mode, other threads may run during other commands.\n\
10140 This applies to both normal execution and replay mode.\n\
10141 replay == scheduler locked in replay mode and unlocked during normal execution."),
10142 set_schedlock_func
, /* traps on target vector */
10143 show_scheduler_mode
,
10144 &setlist
, &showlist
);
10146 add_setshow_boolean_cmd ("schedule-multiple", class_run
, &sched_multi
, _("\
10147 Set mode for resuming threads of all processes."), _("\
10148 Show mode for resuming threads of all processes."), _("\
10149 When on, execution commands (such as 'continue' or 'next') resume all\n\
10150 threads of all processes. When off (which is the default), execution\n\
10151 commands only resume the threads of the current process. The set of\n\
10152 threads that are resumed is further refined by the scheduler-locking\n\
10153 mode (see help set scheduler-locking)."),
10155 show_schedule_multiple
,
10156 &setlist
, &showlist
);
10158 add_setshow_boolean_cmd ("step-mode", class_run
, &step_stop_if_no_debug
, _("\
10159 Set mode of the step operation."), _("\
10160 Show mode of the step operation."), _("\
10161 When set, doing a step over a function without debug line information\n\
10162 will stop at the first instruction of that function. Otherwise, the\n\
10163 function is skipped and the step command stops at a different source line."),
10165 show_step_stop_if_no_debug
,
10166 &setlist
, &showlist
);
10168 add_setshow_auto_boolean_cmd ("displaced-stepping", class_run
,
10169 &can_use_displaced_stepping
, _("\
10170 Set debugger's willingness to use displaced stepping."), _("\
10171 Show debugger's willingness to use displaced stepping."), _("\
10172 If on, gdb will use displaced stepping to step over breakpoints if it is\n\
10173 supported by the target architecture. If off, gdb will not use displaced\n\
10174 stepping to step over breakpoints, even if such is supported by the target\n\
10175 architecture. If auto (which is the default), gdb will use displaced stepping\n\
10176 if the target architecture supports it and non-stop mode is active, but will not\n\
10177 use it in all-stop mode (see help set non-stop)."),
10179 show_can_use_displaced_stepping
,
10180 &setlist
, &showlist
);
10182 add_setshow_enum_cmd ("exec-direction", class_run
, exec_direction_names
,
10183 &exec_direction
, _("Set direction of execution.\n\
10184 Options are 'forward' or 'reverse'."),
10185 _("Show direction of execution (forward/reverse)."),
10186 _("Tells gdb whether to execute forward or backward."),
10187 set_exec_direction_func
, show_exec_direction_func
,
10188 &setlist
, &showlist
);
10190 /* Set/show detach-on-fork: user-settable mode. */
10192 add_setshow_boolean_cmd ("detach-on-fork", class_run
, &detach_fork
, _("\
10193 Set whether gdb will detach the child of a fork."), _("\
10194 Show whether gdb will detach the child of a fork."), _("\
10195 Tells gdb whether to detach the child of a fork."),
10196 nullptr, nullptr, &setlist
, &showlist
);
10198 /* Set/show disable address space randomization mode. */
10200 add_setshow_boolean_cmd ("disable-randomization", class_support
,
10201 &disable_randomization
, _("\
10202 Set disabling of debuggee's virtual address space randomization."), _("\
10203 Show disabling of debuggee's virtual address space randomization."), _("\
10204 When this mode is on (which is the default), randomization of the virtual\n\
10205 address space is disabled. Standalone programs run with the randomization\n\
10206 enabled by default on some platforms."),
10207 &set_disable_randomization
,
10208 &show_disable_randomization
,
10209 &setlist
, &showlist
);
10211 /* ptid initializations */
10212 inferior_ptid
= null_ptid
;
10213 target_last_wait_ptid
= minus_one_ptid
;
10215 gdb::observers::thread_ptid_changed
.attach (infrun_thread_ptid_changed
,
10217 gdb::observers::thread_stop_requested
.attach (infrun_thread_stop_requested
,
10219 gdb::observers::inferior_exit
.attach (infrun_inferior_exit
, "infrun");
10220 gdb::observers::inferior_execd
.attach (infrun_inferior_execd
, "infrun");
10222 /* Explicitly create without lookup, since that tries to create a
10223 value with a void typed value, and when we get here, gdbarch
10224 isn't initialized yet. At this point, we're quite sure there
10225 isn't another convenience variable of the same name. */
10226 create_internalvar_type_lazy ("_siginfo", &siginfo_funcs
, nullptr);
10228 add_setshow_boolean_cmd ("observer", no_class
,
10229 &observer_mode_1
, _("\
10230 Set whether gdb controls the inferior in observer mode."), _("\
10231 Show whether gdb controls the inferior in observer mode."), _("\
10232 In observer mode, GDB can get data from the inferior, but not\n\
10233 affect its execution. Registers and memory may not be changed,\n\
10234 breakpoints may not be set, and the program cannot be interrupted\n\
10237 show_observer_mode
,
10242 selftests::register_test ("infrun_thread_ptid_changed",
10243 selftests::infrun_thread_ptid_changed
);