1 /* Target-struct-independent code to start (run) and stop an inferior
4 Copyright (C) 1986-2022 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"
40 #include "observable.h"
45 #include "mi/mi-common.h"
46 #include "event-top.h"
48 #include "record-full.h"
49 #include "inline-frame.h"
51 #include "tracepoint.h"
55 #include "completer.h"
56 #include "target-descriptions.h"
57 #include "target-dcache.h"
60 #include "gdbsupport/event-loop.h"
61 #include "thread-fsm.h"
62 #include "gdbsupport/enum-flags.h"
63 #include "progspace-and-thread.h"
64 #include "gdbsupport/gdb_optional.h"
65 #include "arch-utils.h"
66 #include "gdbsupport/scope-exit.h"
67 #include "gdbsupport/forward-scope-exit.h"
68 #include "gdbsupport/gdb_select.h"
69 #include <unordered_map>
70 #include "async-event.h"
71 #include "gdbsupport/selftest.h"
72 #include "scoped-mock-context.h"
73 #include "test-target.h"
74 #include "gdbsupport/common-debug.h"
75 #include "gdbsupport/buildargv.h"
77 /* Prototypes for local functions */
79 static void sig_print_info (enum gdb_signal
);
81 static void sig_print_header (void);
83 static void follow_inferior_reset_breakpoints (void);
85 static bool currently_stepping (struct thread_info
*tp
);
87 static void insert_hp_step_resume_breakpoint_at_frame (struct frame_info
*);
89 static void insert_step_resume_breakpoint_at_caller (struct frame_info
*);
91 static void insert_longjmp_resume_breakpoint (struct gdbarch
*, CORE_ADDR
);
93 static bool maybe_software_singlestep (struct gdbarch
*gdbarch
);
95 static void resume (gdb_signal sig
);
97 static void wait_for_inferior (inferior
*inf
);
99 /* Asynchronous signal handler registered as event loop source for
100 when we have pending events ready to be passed to the core. */
101 static struct async_event_handler
*infrun_async_inferior_event_token
;
103 /* Stores whether infrun_async was previously enabled or disabled.
104 Starts off as -1, indicating "never enabled/disabled". */
105 static int infrun_is_async
= -1;
110 infrun_async (int enable
)
112 if (infrun_is_async
!= enable
)
114 infrun_is_async
= enable
;
116 infrun_debug_printf ("enable=%d", enable
);
119 mark_async_event_handler (infrun_async_inferior_event_token
);
121 clear_async_event_handler (infrun_async_inferior_event_token
);
128 mark_infrun_async_event_handler (void)
130 mark_async_event_handler (infrun_async_inferior_event_token
);
133 /* When set, stop the 'step' command if we enter a function which has
134 no line number information. The normal behavior is that we step
135 over such function. */
136 bool step_stop_if_no_debug
= false;
138 show_step_stop_if_no_debug (struct ui_file
*file
, int from_tty
,
139 struct cmd_list_element
*c
, const char *value
)
141 fprintf_filtered (file
, _("Mode of the step operation is %s.\n"), value
);
144 /* proceed and normal_stop use this to notify the user when the
145 inferior stopped in a different thread than it had been running
148 static ptid_t previous_inferior_ptid
;
150 /* If set (default for legacy reasons), when following a fork, GDB
151 will detach from one of the fork branches, child or parent.
152 Exactly which branch is detached depends on 'set follow-fork-mode'
155 static bool detach_fork
= true;
157 bool debug_infrun
= false;
159 show_debug_infrun (struct ui_file
*file
, int from_tty
,
160 struct cmd_list_element
*c
, const char *value
)
162 fprintf_filtered (file
, _("Inferior debugging is %s.\n"), value
);
165 /* Support for disabling address space randomization. */
167 bool disable_randomization
= true;
170 show_disable_randomization (struct ui_file
*file
, int from_tty
,
171 struct cmd_list_element
*c
, const char *value
)
173 if (target_supports_disable_randomization ())
174 fprintf_filtered (file
,
175 _("Disabling randomization of debuggee's "
176 "virtual address space is %s.\n"),
179 fputs_filtered (_("Disabling randomization of debuggee's "
180 "virtual address space is unsupported on\n"
181 "this platform.\n"), file
);
185 set_disable_randomization (const char *args
, int from_tty
,
186 struct cmd_list_element
*c
)
188 if (!target_supports_disable_randomization ())
189 error (_("Disabling randomization of debuggee's "
190 "virtual address space is unsupported on\n"
194 /* User interface for non-stop mode. */
196 bool non_stop
= false;
197 static bool non_stop_1
= false;
200 set_non_stop (const char *args
, int from_tty
,
201 struct cmd_list_element
*c
)
203 if (target_has_execution ())
205 non_stop_1
= non_stop
;
206 error (_("Cannot change this setting while the inferior is running."));
209 non_stop
= non_stop_1
;
213 show_non_stop (struct ui_file
*file
, int from_tty
,
214 struct cmd_list_element
*c
, const char *value
)
216 fprintf_filtered (file
,
217 _("Controlling the inferior in non-stop mode is %s.\n"),
221 /* "Observer mode" is somewhat like a more extreme version of
222 non-stop, in which all GDB operations that might affect the
223 target's execution have been disabled. */
225 static bool observer_mode
= false;
226 static bool observer_mode_1
= false;
229 set_observer_mode (const char *args
, int from_tty
,
230 struct cmd_list_element
*c
)
232 if (target_has_execution ())
234 observer_mode_1
= observer_mode
;
235 error (_("Cannot change this setting while the inferior is running."));
238 observer_mode
= observer_mode_1
;
240 may_write_registers
= !observer_mode
;
241 may_write_memory
= !observer_mode
;
242 may_insert_breakpoints
= !observer_mode
;
243 may_insert_tracepoints
= !observer_mode
;
244 /* We can insert fast tracepoints in or out of observer mode,
245 but enable them if we're going into this mode. */
247 may_insert_fast_tracepoints
= true;
248 may_stop
= !observer_mode
;
249 update_target_permissions ();
251 /* Going *into* observer mode we must force non-stop, then
252 going out we leave it that way. */
255 pagination_enabled
= 0;
256 non_stop
= non_stop_1
= true;
260 printf_filtered (_("Observer mode is now %s.\n"),
261 (observer_mode
? "on" : "off"));
265 show_observer_mode (struct ui_file
*file
, int from_tty
,
266 struct cmd_list_element
*c
, const char *value
)
268 fprintf_filtered (file
, _("Observer mode is %s.\n"), value
);
271 /* This updates the value of observer mode based on changes in
272 permissions. Note that we are deliberately ignoring the values of
273 may-write-registers and may-write-memory, since the user may have
274 reason to enable these during a session, for instance to turn on a
275 debugging-related global. */
278 update_observer_mode (void)
280 bool newval
= (!may_insert_breakpoints
281 && !may_insert_tracepoints
282 && may_insert_fast_tracepoints
286 /* Let the user know if things change. */
287 if (newval
!= observer_mode
)
288 printf_filtered (_("Observer mode is now %s.\n"),
289 (newval
? "on" : "off"));
291 observer_mode
= observer_mode_1
= newval
;
294 /* Tables of how to react to signals; the user sets them. */
296 static unsigned char signal_stop
[GDB_SIGNAL_LAST
];
297 static unsigned char signal_print
[GDB_SIGNAL_LAST
];
298 static unsigned char signal_program
[GDB_SIGNAL_LAST
];
300 /* Table of signals that are registered with "catch signal". A
301 non-zero entry indicates that the signal is caught by some "catch
303 static unsigned char signal_catch
[GDB_SIGNAL_LAST
];
305 /* Table of signals that the target may silently handle.
306 This is automatically determined from the flags above,
307 and simply cached here. */
308 static unsigned char signal_pass
[GDB_SIGNAL_LAST
];
310 #define SET_SIGS(nsigs,sigs,flags) \
312 int signum = (nsigs); \
313 while (signum-- > 0) \
314 if ((sigs)[signum]) \
315 (flags)[signum] = 1; \
318 #define UNSET_SIGS(nsigs,sigs,flags) \
320 int signum = (nsigs); \
321 while (signum-- > 0) \
322 if ((sigs)[signum]) \
323 (flags)[signum] = 0; \
326 /* Update the target's copy of SIGNAL_PROGRAM. The sole purpose of
327 this function is to avoid exporting `signal_program'. */
330 update_signals_program_target (void)
332 target_program_signals (signal_program
);
335 /* Value to pass to target_resume() to cause all threads to resume. */
337 #define RESUME_ALL minus_one_ptid
339 /* Command list pointer for the "stop" placeholder. */
341 static struct cmd_list_element
*stop_command
;
343 /* Nonzero if we want to give control to the user when we're notified
344 of shared library events by the dynamic linker. */
345 int stop_on_solib_events
;
347 /* Enable or disable optional shared library event breakpoints
348 as appropriate when the above flag is changed. */
351 set_stop_on_solib_events (const char *args
,
352 int from_tty
, struct cmd_list_element
*c
)
354 update_solib_breakpoints ();
358 show_stop_on_solib_events (struct ui_file
*file
, int from_tty
,
359 struct cmd_list_element
*c
, const char *value
)
361 fprintf_filtered (file
, _("Stopping for shared library events is %s.\n"),
365 /* True after stop if current stack frame should be printed. */
367 static bool stop_print_frame
;
369 /* This is a cached copy of the target/ptid/waitstatus of the last
370 event returned by target_wait()/deprecated_target_wait_hook().
371 This information is returned by get_last_target_status(). */
372 static process_stratum_target
*target_last_proc_target
;
373 static ptid_t target_last_wait_ptid
;
374 static struct target_waitstatus target_last_waitstatus
;
376 void init_thread_stepping_state (struct thread_info
*tss
);
378 static const char follow_fork_mode_child
[] = "child";
379 static const char follow_fork_mode_parent
[] = "parent";
381 static const char *const follow_fork_mode_kind_names
[] = {
382 follow_fork_mode_child
,
383 follow_fork_mode_parent
,
387 static const char *follow_fork_mode_string
= follow_fork_mode_parent
;
389 show_follow_fork_mode_string (struct ui_file
*file
, int from_tty
,
390 struct cmd_list_element
*c
, const char *value
)
392 fprintf_filtered (file
,
393 _("Debugger response to a program "
394 "call of fork or vfork is \"%s\".\n"),
399 /* Handle changes to the inferior list based on the type of fork,
400 which process is being followed, and whether the other process
401 should be detached. On entry inferior_ptid must be the ptid of
402 the fork parent. At return inferior_ptid is the ptid of the
403 followed inferior. */
406 follow_fork_inferior (bool follow_child
, bool detach_fork
)
408 target_waitkind fork_kind
= inferior_thread ()->pending_follow
.kind ();
409 gdb_assert (fork_kind
== TARGET_WAITKIND_FORKED
410 || fork_kind
== TARGET_WAITKIND_VFORKED
);
411 bool has_vforked
= fork_kind
== TARGET_WAITKIND_VFORKED
;
412 ptid_t parent_ptid
= inferior_ptid
;
413 ptid_t child_ptid
= inferior_thread ()->pending_follow
.child_ptid ();
416 && !non_stop
/* Non-stop always resumes both branches. */
417 && current_ui
->prompt_state
== PROMPT_BLOCKED
418 && !(follow_child
|| detach_fork
|| sched_multi
))
420 /* The parent stays blocked inside the vfork syscall until the
421 child execs or exits. If we don't let the child run, then
422 the parent stays blocked. If we're telling the parent to run
423 in the foreground, the user will not be able to ctrl-c to get
424 back the terminal, effectively hanging the debug session. */
425 fprintf_filtered (gdb_stderr
, _("\
426 Can not resume the parent process over vfork in the foreground while\n\
427 holding the child stopped. Try \"set detach-on-fork\" or \
428 \"set schedule-multiple\".\n"));
432 inferior
*parent_inf
= current_inferior ();
433 inferior
*child_inf
= nullptr;
437 /* Detach new forked process? */
440 /* Before detaching from the child, remove all breakpoints
441 from it. If we forked, then this has already been taken
442 care of by infrun.c. If we vforked however, any
443 breakpoint inserted in the parent is visible in the
444 child, even those added while stopped in a vfork
445 catchpoint. This will remove the breakpoints from the
446 parent also, but they'll be reinserted below. */
449 /* Keep breakpoints list in sync. */
450 remove_breakpoints_inf (current_inferior ());
453 if (print_inferior_events
)
455 /* Ensure that we have a process ptid. */
456 ptid_t process_ptid
= ptid_t (child_ptid
.pid ());
458 target_terminal::ours_for_output ();
459 printf_filtered (_("[Detaching after %s from child %s]\n"),
460 has_vforked
? "vfork" : "fork",
461 target_pid_to_str (process_ptid
).c_str ());
466 /* Add process to GDB's tables. */
467 child_inf
= add_inferior (child_ptid
.pid ());
469 child_inf
->attach_flag
= parent_inf
->attach_flag
;
470 copy_terminal_info (child_inf
, parent_inf
);
471 child_inf
->gdbarch
= parent_inf
->gdbarch
;
472 copy_inferior_target_desc_info (child_inf
, parent_inf
);
474 child_inf
->symfile_flags
= SYMFILE_NO_READ
;
476 /* If this is a vfork child, then the address-space is
477 shared with the parent. */
480 child_inf
->pspace
= parent_inf
->pspace
;
481 child_inf
->aspace
= parent_inf
->aspace
;
483 exec_on_vfork (child_inf
);
485 /* The parent will be frozen until the child is done
486 with the shared region. Keep track of the
488 child_inf
->vfork_parent
= parent_inf
;
489 child_inf
->pending_detach
= 0;
490 parent_inf
->vfork_child
= child_inf
;
491 parent_inf
->pending_detach
= 0;
495 child_inf
->aspace
= new_address_space ();
496 child_inf
->pspace
= new program_space (child_inf
->aspace
);
497 child_inf
->removable
= 1;
498 clone_program_space (child_inf
->pspace
, parent_inf
->pspace
);
504 /* If we detached from the child, then we have to be careful
505 to not insert breakpoints in the parent until the child
506 is done with the shared memory region. However, if we're
507 staying attached to the child, then we can and should
508 insert breakpoints, so that we can debug it. A
509 subsequent child exec or exit is enough to know when does
510 the child stops using the parent's address space. */
511 parent_inf
->waiting_for_vfork_done
= detach_fork
;
512 parent_inf
->pspace
->breakpoints_not_allowed
= detach_fork
;
517 /* Follow the child. */
519 if (print_inferior_events
)
521 std::string parent_pid
= target_pid_to_str (parent_ptid
);
522 std::string child_pid
= target_pid_to_str (child_ptid
);
524 target_terminal::ours_for_output ();
525 printf_filtered (_("[Attaching after %s %s to child %s]\n"),
527 has_vforked
? "vfork" : "fork",
531 /* Add the new inferior first, so that the target_detach below
532 doesn't unpush the target. */
534 child_inf
= add_inferior (child_ptid
.pid ());
536 child_inf
->attach_flag
= parent_inf
->attach_flag
;
537 copy_terminal_info (child_inf
, parent_inf
);
538 child_inf
->gdbarch
= parent_inf
->gdbarch
;
539 copy_inferior_target_desc_info (child_inf
, parent_inf
);
543 /* If this is a vfork child, then the address-space is shared
545 child_inf
->aspace
= parent_inf
->aspace
;
546 child_inf
->pspace
= parent_inf
->pspace
;
548 exec_on_vfork (child_inf
);
550 else if (detach_fork
)
552 /* We follow the child and detach from the parent: move the parent's
553 program space to the child. This simplifies some things, like
554 doing "next" over fork() and landing on the expected line in the
555 child (note, that is broken with "set detach-on-fork off").
557 Before assigning brand new spaces for the parent, remove
558 breakpoints from it: because the new pspace won't match
559 currently inserted locations, the normal detach procedure
560 wouldn't remove them, and we would leave them inserted when
562 remove_breakpoints_inf (parent_inf
);
564 child_inf
->aspace
= parent_inf
->aspace
;
565 child_inf
->pspace
= parent_inf
->pspace
;
566 parent_inf
->aspace
= new_address_space ();
567 parent_inf
->pspace
= new program_space (parent_inf
->aspace
);
568 clone_program_space (parent_inf
->pspace
, child_inf
->pspace
);
570 /* The parent inferior is still the current one, so keep things
572 set_current_program_space (parent_inf
->pspace
);
576 child_inf
->aspace
= new_address_space ();
577 child_inf
->pspace
= new program_space (child_inf
->aspace
);
578 child_inf
->removable
= 1;
579 child_inf
->symfile_flags
= SYMFILE_NO_READ
;
580 clone_program_space (child_inf
->pspace
, parent_inf
->pspace
);
584 gdb_assert (current_inferior () == parent_inf
);
586 /* If we are setting up an inferior for the child, target_follow_fork is
587 responsible for pushing the appropriate targets on the new inferior's
588 target stack and adding the initial thread (with ptid CHILD_PTID).
590 If we are not setting up an inferior for the child (because following
591 the parent and detach_fork is true), it is responsible for detaching
593 target_follow_fork (child_inf
, child_ptid
, fork_kind
, follow_child
,
596 /* target_follow_fork must leave the parent as the current inferior. If we
597 want to follow the child, we make it the current one below. */
598 gdb_assert (current_inferior () == parent_inf
);
600 /* If there is a child inferior, target_follow_fork must have created a thread
602 if (child_inf
!= nullptr)
603 gdb_assert (!child_inf
->thread_list
.empty ());
605 /* Clear the parent thread's pending follow field. Do this before calling
606 target_detach, so that the target can differentiate the two following
609 - We continue past a fork with "follow-fork-mode == child" &&
610 "detach-on-fork on", and therefore detach the parent. In that
611 case the target should not detach the fork child.
612 - We run to a fork catchpoint and the user types "detach". In that
613 case, the target should detach the fork child in addition to the
616 The former case will have pending_follow cleared, the later will have
617 pending_follow set. */
618 thread_info
*parent_thread
= find_thread_ptid (parent_inf
, parent_ptid
);
619 gdb_assert (parent_thread
!= nullptr);
620 parent_thread
->pending_follow
.set_spurious ();
622 /* Detach the parent if needed. */
625 /* If we're vforking, we want to hold on to the parent until
626 the child exits or execs. At child exec or exit time we
627 can remove the old breakpoints from the parent and detach
628 or resume debugging it. Otherwise, detach the parent now;
629 we'll want to reuse it's program/address spaces, but we
630 can't set them to the child before removing breakpoints
631 from the parent, otherwise, the breakpoints module could
632 decide to remove breakpoints from the wrong process (since
633 they'd be assigned to the same address space). */
637 gdb_assert (child_inf
->vfork_parent
== NULL
);
638 gdb_assert (parent_inf
->vfork_child
== NULL
);
639 child_inf
->vfork_parent
= parent_inf
;
640 child_inf
->pending_detach
= 0;
641 parent_inf
->vfork_child
= child_inf
;
642 parent_inf
->pending_detach
= detach_fork
;
643 parent_inf
->waiting_for_vfork_done
= 0;
645 else if (detach_fork
)
647 if (print_inferior_events
)
649 /* Ensure that we have a process ptid. */
650 ptid_t process_ptid
= ptid_t (parent_ptid
.pid ());
652 target_terminal::ours_for_output ();
653 printf_filtered (_("[Detaching after fork from "
655 target_pid_to_str (process_ptid
).c_str ());
658 target_detach (parent_inf
, 0);
662 /* If we ended up creating a new inferior, call post_create_inferior to inform
663 the various subcomponents. */
664 if (child_inf
!= nullptr)
666 /* If FOLLOW_CHILD, we leave CHILD_INF as the current inferior
667 (do not restore the parent as the current inferior). */
668 gdb::optional
<scoped_restore_current_thread
> maybe_restore
;
671 maybe_restore
.emplace ();
673 switch_to_thread (*child_inf
->threads ().begin ());
674 post_create_inferior (0);
680 /* Tell the target to follow the fork we're stopped at. Returns true
681 if the inferior should be resumed; false, if the target for some
682 reason decided it's best not to resume. */
687 bool follow_child
= (follow_fork_mode_string
== follow_fork_mode_child
);
688 bool should_resume
= true;
690 /* Copy user stepping state to the new inferior thread. FIXME: the
691 followed fork child thread should have a copy of most of the
692 parent thread structure's run control related fields, not just these.
693 Initialized to avoid "may be used uninitialized" warnings from gcc. */
694 struct breakpoint
*step_resume_breakpoint
= NULL
;
695 struct breakpoint
*exception_resume_breakpoint
= NULL
;
696 CORE_ADDR step_range_start
= 0;
697 CORE_ADDR step_range_end
= 0;
698 int current_line
= 0;
699 symtab
*current_symtab
= NULL
;
700 struct frame_id step_frame_id
= { 0 };
701 struct thread_fsm
*thread_fsm
= NULL
;
705 process_stratum_target
*wait_target
;
707 struct target_waitstatus wait_status
;
709 /* Get the last target status returned by target_wait(). */
710 get_last_target_status (&wait_target
, &wait_ptid
, &wait_status
);
712 /* If not stopped at a fork event, then there's nothing else to
714 if (wait_status
.kind () != TARGET_WAITKIND_FORKED
715 && wait_status
.kind () != TARGET_WAITKIND_VFORKED
)
718 /* Check if we switched over from WAIT_PTID, since the event was
720 if (wait_ptid
!= minus_one_ptid
721 && (current_inferior ()->process_target () != wait_target
722 || inferior_ptid
!= wait_ptid
))
724 /* We did. Switch back to WAIT_PTID thread, to tell the
725 target to follow it (in either direction). We'll
726 afterwards refuse to resume, and inform the user what
728 thread_info
*wait_thread
= find_thread_ptid (wait_target
, wait_ptid
);
729 switch_to_thread (wait_thread
);
730 should_resume
= false;
734 thread_info
*tp
= inferior_thread ();
736 /* If there were any forks/vforks that were caught and are now to be
737 followed, then do so now. */
738 switch (tp
->pending_follow
.kind ())
740 case TARGET_WAITKIND_FORKED
:
741 case TARGET_WAITKIND_VFORKED
:
743 ptid_t parent
, child
;
745 /* If the user did a next/step, etc, over a fork call,
746 preserve the stepping state in the fork child. */
747 if (follow_child
&& should_resume
)
749 step_resume_breakpoint
= clone_momentary_breakpoint
750 (tp
->control
.step_resume_breakpoint
);
751 step_range_start
= tp
->control
.step_range_start
;
752 step_range_end
= tp
->control
.step_range_end
;
753 current_line
= tp
->current_line
;
754 current_symtab
= tp
->current_symtab
;
755 step_frame_id
= tp
->control
.step_frame_id
;
756 exception_resume_breakpoint
757 = clone_momentary_breakpoint (tp
->control
.exception_resume_breakpoint
);
758 thread_fsm
= tp
->thread_fsm
;
760 /* For now, delete the parent's sr breakpoint, otherwise,
761 parent/child sr breakpoints are considered duplicates,
762 and the child version will not be installed. Remove
763 this when the breakpoints module becomes aware of
764 inferiors and address spaces. */
765 delete_step_resume_breakpoint (tp
);
766 tp
->control
.step_range_start
= 0;
767 tp
->control
.step_range_end
= 0;
768 tp
->control
.step_frame_id
= null_frame_id
;
769 delete_exception_resume_breakpoint (tp
);
770 tp
->thread_fsm
= NULL
;
773 parent
= inferior_ptid
;
774 child
= tp
->pending_follow
.child_ptid ();
776 process_stratum_target
*parent_targ
= tp
->inf
->process_target ();
777 /* Set up inferior(s) as specified by the caller, and tell the
778 target to do whatever is necessary to follow either parent
780 if (follow_fork_inferior (follow_child
, detach_fork
))
782 /* Target refused to follow, or there's some other reason
783 we shouldn't resume. */
788 /* This makes sure we don't try to apply the "Switched
789 over from WAIT_PID" logic above. */
790 nullify_last_target_wait_ptid ();
792 /* If we followed the child, switch to it... */
795 thread_info
*child_thr
= find_thread_ptid (parent_targ
, child
);
796 switch_to_thread (child_thr
);
798 /* ... and preserve the stepping state, in case the
799 user was stepping over the fork call. */
802 tp
= inferior_thread ();
803 tp
->control
.step_resume_breakpoint
804 = step_resume_breakpoint
;
805 tp
->control
.step_range_start
= step_range_start
;
806 tp
->control
.step_range_end
= step_range_end
;
807 tp
->current_line
= current_line
;
808 tp
->current_symtab
= current_symtab
;
809 tp
->control
.step_frame_id
= step_frame_id
;
810 tp
->control
.exception_resume_breakpoint
811 = exception_resume_breakpoint
;
812 tp
->thread_fsm
= thread_fsm
;
816 /* If we get here, it was because we're trying to
817 resume from a fork catchpoint, but, the user
818 has switched threads away from the thread that
819 forked. In that case, the resume command
820 issued is most likely not applicable to the
821 child, so just warn, and refuse to resume. */
822 warning (_("Not resuming: switched threads "
823 "before following fork child."));
826 /* Reset breakpoints in the child as appropriate. */
827 follow_inferior_reset_breakpoints ();
832 case TARGET_WAITKIND_SPURIOUS
:
833 /* Nothing to follow. */
836 internal_error (__FILE__
, __LINE__
,
837 "Unexpected pending_follow.kind %d\n",
838 tp
->pending_follow
.kind ());
842 return should_resume
;
846 follow_inferior_reset_breakpoints (void)
848 struct thread_info
*tp
= inferior_thread ();
850 /* Was there a step_resume breakpoint? (There was if the user
851 did a "next" at the fork() call.) If so, explicitly reset its
852 thread number. Cloned step_resume breakpoints are disabled on
853 creation, so enable it here now that it is associated with the
856 step_resumes are a form of bp that are made to be per-thread.
857 Since we created the step_resume bp when the parent process
858 was being debugged, and now are switching to the child process,
859 from the breakpoint package's viewpoint, that's a switch of
860 "threads". We must update the bp's notion of which thread
861 it is for, or it'll be ignored when it triggers. */
863 if (tp
->control
.step_resume_breakpoint
)
865 breakpoint_re_set_thread (tp
->control
.step_resume_breakpoint
);
866 tp
->control
.step_resume_breakpoint
->loc
->enabled
= 1;
869 /* Treat exception_resume breakpoints like step_resume breakpoints. */
870 if (tp
->control
.exception_resume_breakpoint
)
872 breakpoint_re_set_thread (tp
->control
.exception_resume_breakpoint
);
873 tp
->control
.exception_resume_breakpoint
->loc
->enabled
= 1;
876 /* Reinsert all breakpoints in the child. The user may have set
877 breakpoints after catching the fork, in which case those
878 were never set in the child, but only in the parent. This makes
879 sure the inserted breakpoints match the breakpoint list. */
881 breakpoint_re_set ();
882 insert_breakpoints ();
885 /* The child has exited or execed: resume THREAD, a thread of the parent,
886 if it was meant to be executing. */
889 proceed_after_vfork_done (thread_info
*thread
)
891 if (thread
->state
== THREAD_RUNNING
892 && !thread
->executing ()
893 && !thread
->stop_requested
894 && thread
->stop_signal () == GDB_SIGNAL_0
)
896 infrun_debug_printf ("resuming vfork parent thread %s",
897 thread
->ptid
.to_string ().c_str ());
899 switch_to_thread (thread
);
900 clear_proceed_status (0);
901 proceed ((CORE_ADDR
) -1, GDB_SIGNAL_DEFAULT
);
905 /* Called whenever we notice an exec or exit event, to handle
906 detaching or resuming a vfork parent. */
909 handle_vfork_child_exec_or_exit (int exec
)
911 struct inferior
*inf
= current_inferior ();
913 if (inf
->vfork_parent
)
915 inferior
*resume_parent
= nullptr;
917 /* This exec or exit marks the end of the shared memory region
918 between the parent and the child. Break the bonds. */
919 inferior
*vfork_parent
= inf
->vfork_parent
;
920 inf
->vfork_parent
->vfork_child
= NULL
;
921 inf
->vfork_parent
= NULL
;
923 /* If the user wanted to detach from the parent, now is the
925 if (vfork_parent
->pending_detach
)
927 struct program_space
*pspace
;
928 struct address_space
*aspace
;
930 /* follow-fork child, detach-on-fork on. */
932 vfork_parent
->pending_detach
= 0;
934 scoped_restore_current_pspace_and_thread restore_thread
;
936 /* We're letting loose of the parent. */
937 thread_info
*tp
= any_live_thread_of_inferior (vfork_parent
);
938 switch_to_thread (tp
);
940 /* We're about to detach from the parent, which implicitly
941 removes breakpoints from its address space. There's a
942 catch here: we want to reuse the spaces for the child,
943 but, parent/child are still sharing the pspace at this
944 point, although the exec in reality makes the kernel give
945 the child a fresh set of new pages. The problem here is
946 that the breakpoints module being unaware of this, would
947 likely chose the child process to write to the parent
948 address space. Swapping the child temporarily away from
949 the spaces has the desired effect. Yes, this is "sort
952 pspace
= inf
->pspace
;
953 aspace
= inf
->aspace
;
957 if (print_inferior_events
)
960 = target_pid_to_str (ptid_t (vfork_parent
->pid
));
962 target_terminal::ours_for_output ();
966 printf_filtered (_("[Detaching vfork parent %s "
967 "after child exec]\n"), pidstr
.c_str ());
971 printf_filtered (_("[Detaching vfork parent %s "
972 "after child exit]\n"), pidstr
.c_str ());
976 target_detach (vfork_parent
, 0);
979 inf
->pspace
= pspace
;
980 inf
->aspace
= aspace
;
984 /* We're staying attached to the parent, so, really give the
985 child a new address space. */
986 inf
->pspace
= new program_space (maybe_new_address_space ());
987 inf
->aspace
= inf
->pspace
->aspace
;
989 set_current_program_space (inf
->pspace
);
991 resume_parent
= vfork_parent
;
995 /* If this is a vfork child exiting, then the pspace and
996 aspaces were shared with the parent. Since we're
997 reporting the process exit, we'll be mourning all that is
998 found in the address space, and switching to null_ptid,
999 preparing to start a new inferior. But, since we don't
1000 want to clobber the parent's address/program spaces, we
1001 go ahead and create a new one for this exiting
1004 /* Switch to no-thread while running clone_program_space, so
1005 that clone_program_space doesn't want to read the
1006 selected frame of a dead process. */
1007 scoped_restore_current_thread restore_thread
;
1008 switch_to_no_thread ();
1010 inf
->pspace
= new program_space (maybe_new_address_space ());
1011 inf
->aspace
= inf
->pspace
->aspace
;
1012 set_current_program_space (inf
->pspace
);
1014 inf
->symfile_flags
= SYMFILE_NO_READ
;
1015 clone_program_space (inf
->pspace
, vfork_parent
->pspace
);
1017 resume_parent
= vfork_parent
;
1020 gdb_assert (current_program_space
== inf
->pspace
);
1022 if (non_stop
&& resume_parent
!= nullptr)
1024 /* If the user wanted the parent to be running, let it go
1026 scoped_restore_current_thread restore_thread
;
1028 infrun_debug_printf ("resuming vfork parent process %d",
1029 resume_parent
->pid
);
1031 for (thread_info
*thread
: resume_parent
->threads ())
1032 proceed_after_vfork_done (thread
);
1037 /* Enum strings for "set|show follow-exec-mode". */
1039 static const char follow_exec_mode_new
[] = "new";
1040 static const char follow_exec_mode_same
[] = "same";
1041 static const char *const follow_exec_mode_names
[] =
1043 follow_exec_mode_new
,
1044 follow_exec_mode_same
,
1048 static const char *follow_exec_mode_string
= follow_exec_mode_same
;
1050 show_follow_exec_mode_string (struct ui_file
*file
, int from_tty
,
1051 struct cmd_list_element
*c
, const char *value
)
1053 fprintf_filtered (file
, _("Follow exec mode is \"%s\".\n"), value
);
1056 /* EXEC_FILE_TARGET is assumed to be non-NULL. */
1059 follow_exec (ptid_t ptid
, const char *exec_file_target
)
1061 int pid
= ptid
.pid ();
1062 ptid_t process_ptid
;
1064 /* Switch terminal for any messages produced e.g. by
1065 breakpoint_re_set. */
1066 target_terminal::ours_for_output ();
1068 /* This is an exec event that we actually wish to pay attention to.
1069 Refresh our symbol table to the newly exec'd program, remove any
1070 momentary bp's, etc.
1072 If there are breakpoints, they aren't really inserted now,
1073 since the exec() transformed our inferior into a fresh set
1076 We want to preserve symbolic breakpoints on the list, since
1077 we have hopes that they can be reset after the new a.out's
1078 symbol table is read.
1080 However, any "raw" breakpoints must be removed from the list
1081 (e.g., the solib bp's), since their address is probably invalid
1084 And, we DON'T want to call delete_breakpoints() here, since
1085 that may write the bp's "shadow contents" (the instruction
1086 value that was overwritten with a TRAP instruction). Since
1087 we now have a new a.out, those shadow contents aren't valid. */
1089 mark_breakpoints_out ();
1091 /* The target reports the exec event to the main thread, even if
1092 some other thread does the exec, and even if the main thread was
1093 stopped or already gone. We may still have non-leader threads of
1094 the process on our list. E.g., on targets that don't have thread
1095 exit events (like remote); or on native Linux in non-stop mode if
1096 there were only two threads in the inferior and the non-leader
1097 one is the one that execs (and nothing forces an update of the
1098 thread list up to here). When debugging remotely, it's best to
1099 avoid extra traffic, when possible, so avoid syncing the thread
1100 list with the target, and instead go ahead and delete all threads
1101 of the process but one that reported the event. Note this must
1102 be done before calling update_breakpoints_after_exec, as
1103 otherwise clearing the threads' resources would reference stale
1104 thread breakpoints -- it may have been one of these threads that
1105 stepped across the exec. We could just clear their stepping
1106 states, but as long as we're iterating, might as well delete
1107 them. Deleting them now rather than at the next user-visible
1108 stop provides a nicer sequence of events for user and MI
1110 for (thread_info
*th
: all_threads_safe ())
1111 if (th
->ptid
.pid () == pid
&& th
->ptid
!= ptid
)
1114 /* We also need to clear any left over stale state for the
1115 leader/event thread. E.g., if there was any step-resume
1116 breakpoint or similar, it's gone now. We cannot truly
1117 step-to-next statement through an exec(). */
1118 thread_info
*th
= inferior_thread ();
1119 th
->control
.step_resume_breakpoint
= NULL
;
1120 th
->control
.exception_resume_breakpoint
= NULL
;
1121 th
->control
.single_step_breakpoints
= NULL
;
1122 th
->control
.step_range_start
= 0;
1123 th
->control
.step_range_end
= 0;
1125 /* The user may have had the main thread held stopped in the
1126 previous image (e.g., schedlock on, or non-stop). Release
1128 th
->stop_requested
= 0;
1130 update_breakpoints_after_exec ();
1132 /* What is this a.out's name? */
1133 process_ptid
= ptid_t (pid
);
1134 printf_unfiltered (_("%s is executing new program: %s\n"),
1135 target_pid_to_str (process_ptid
).c_str (),
1138 /* We've followed the inferior through an exec. Therefore, the
1139 inferior has essentially been killed & reborn. */
1141 breakpoint_init_inferior (inf_execd
);
1143 gdb::unique_xmalloc_ptr
<char> exec_file_host
1144 = exec_file_find (exec_file_target
, NULL
);
1146 /* If we were unable to map the executable target pathname onto a host
1147 pathname, tell the user that. Otherwise GDB's subsequent behavior
1148 is confusing. Maybe it would even be better to stop at this point
1149 so that the user can specify a file manually before continuing. */
1150 if (exec_file_host
== NULL
)
1151 warning (_("Could not load symbols for executable %s.\n"
1152 "Do you need \"set sysroot\"?"),
1155 /* Reset the shared library package. This ensures that we get a
1156 shlib event when the child reaches "_start", at which point the
1157 dld will have had a chance to initialize the child. */
1158 /* Also, loading a symbol file below may trigger symbol lookups, and
1159 we don't want those to be satisfied by the libraries of the
1160 previous incarnation of this process. */
1161 no_shared_libraries (NULL
, 0);
1163 struct inferior
*inf
= current_inferior ();
1165 if (follow_exec_mode_string
== follow_exec_mode_new
)
1167 /* The user wants to keep the old inferior and program spaces
1168 around. Create a new fresh one, and switch to it. */
1170 /* Do exit processing for the original inferior before setting the new
1171 inferior's pid. Having two inferiors with the same pid would confuse
1172 find_inferior_p(t)id. Transfer the terminal state and info from the
1173 old to the new inferior. */
1174 inferior
*new_inferior
= add_inferior_with_spaces ();
1176 swap_terminal_info (new_inferior
, inf
);
1177 exit_inferior_silent (inf
);
1179 new_inferior
->pid
= pid
;
1180 target_follow_exec (new_inferior
, ptid
, exec_file_target
);
1182 /* We continue with the new inferior. */
1187 /* The old description may no longer be fit for the new image.
1188 E.g, a 64-bit process exec'ed a 32-bit process. Clear the
1189 old description; we'll read a new one below. No need to do
1190 this on "follow-exec-mode new", as the old inferior stays
1191 around (its description is later cleared/refetched on
1193 target_clear_description ();
1194 target_follow_exec (inf
, ptid
, exec_file_target
);
1197 gdb_assert (current_inferior () == inf
);
1198 gdb_assert (current_program_space
== inf
->pspace
);
1200 /* Attempt to open the exec file. SYMFILE_DEFER_BP_RESET is used
1201 because the proper displacement for a PIE (Position Independent
1202 Executable) main symbol file will only be computed by
1203 solib_create_inferior_hook below. breakpoint_re_set would fail
1204 to insert the breakpoints with the zero displacement. */
1205 try_open_exec_file (exec_file_host
.get (), inf
, SYMFILE_DEFER_BP_RESET
);
1207 /* If the target can specify a description, read it. Must do this
1208 after flipping to the new executable (because the target supplied
1209 description must be compatible with the executable's
1210 architecture, and the old executable may e.g., be 32-bit, while
1211 the new one 64-bit), and before anything involving memory or
1213 target_find_description ();
1215 gdb::observers::inferior_execd
.notify (inf
);
1217 breakpoint_re_set ();
1219 /* Reinsert all breakpoints. (Those which were symbolic have
1220 been reset to the proper address in the new a.out, thanks
1221 to symbol_file_command...). */
1222 insert_breakpoints ();
1224 /* The next resume of this inferior should bring it to the shlib
1225 startup breakpoints. (If the user had also set bp's on
1226 "main" from the old (parent) process, then they'll auto-
1227 matically get reset there in the new process.). */
1230 /* The chain of threads that need to do a step-over operation to get
1231 past e.g., a breakpoint. What technique is used to step over the
1232 breakpoint/watchpoint does not matter -- all threads end up in the
1233 same queue, to maintain rough temporal order of execution, in order
1234 to avoid starvation, otherwise, we could e.g., find ourselves
1235 constantly stepping the same couple threads past their breakpoints
1236 over and over, if the single-step finish fast enough. */
1237 thread_step_over_list global_thread_step_over_list
;
1239 /* Bit flags indicating what the thread needs to step over. */
1241 enum step_over_what_flag
1243 /* Step over a breakpoint. */
1244 STEP_OVER_BREAKPOINT
= 1,
1246 /* Step past a non-continuable watchpoint, in order to let the
1247 instruction execute so we can evaluate the watchpoint
1249 STEP_OVER_WATCHPOINT
= 2
1251 DEF_ENUM_FLAGS_TYPE (enum step_over_what_flag
, step_over_what
);
1253 /* Info about an instruction that is being stepped over. */
1255 struct step_over_info
1257 /* If we're stepping past a breakpoint, this is the address space
1258 and address of the instruction the breakpoint is set at. We'll
1259 skip inserting all breakpoints here. Valid iff ASPACE is
1261 const address_space
*aspace
= nullptr;
1262 CORE_ADDR address
= 0;
1264 /* The instruction being stepped over triggers a nonsteppable
1265 watchpoint. If true, we'll skip inserting watchpoints. */
1266 int nonsteppable_watchpoint_p
= 0;
1268 /* The thread's global number. */
1272 /* The step-over info of the location that is being stepped over.
1274 Note that with async/breakpoint always-inserted mode, a user might
1275 set a new breakpoint/watchpoint/etc. exactly while a breakpoint is
1276 being stepped over. As setting a new breakpoint inserts all
1277 breakpoints, we need to make sure the breakpoint being stepped over
1278 isn't inserted then. We do that by only clearing the step-over
1279 info when the step-over is actually finished (or aborted).
1281 Presently GDB can only step over one breakpoint at any given time.
1282 Given threads that can't run code in the same address space as the
1283 breakpoint's can't really miss the breakpoint, GDB could be taught
1284 to step-over at most one breakpoint per address space (so this info
1285 could move to the address space object if/when GDB is extended).
1286 The set of breakpoints being stepped over will normally be much
1287 smaller than the set of all breakpoints, so a flag in the
1288 breakpoint location structure would be wasteful. A separate list
1289 also saves complexity and run-time, as otherwise we'd have to go
1290 through all breakpoint locations clearing their flag whenever we
1291 start a new sequence. Similar considerations weigh against storing
1292 this info in the thread object. Plus, not all step overs actually
1293 have breakpoint locations -- e.g., stepping past a single-step
1294 breakpoint, or stepping to complete a non-continuable
1296 static struct step_over_info step_over_info
;
1298 /* Record the address of the breakpoint/instruction we're currently
1300 N.B. We record the aspace and address now, instead of say just the thread,
1301 because when we need the info later the thread may be running. */
1304 set_step_over_info (const address_space
*aspace
, CORE_ADDR address
,
1305 int nonsteppable_watchpoint_p
,
1308 step_over_info
.aspace
= aspace
;
1309 step_over_info
.address
= address
;
1310 step_over_info
.nonsteppable_watchpoint_p
= nonsteppable_watchpoint_p
;
1311 step_over_info
.thread
= thread
;
1314 /* Called when we're not longer stepping over a breakpoint / an
1315 instruction, so all breakpoints are free to be (re)inserted. */
1318 clear_step_over_info (void)
1320 infrun_debug_printf ("clearing step over info");
1321 step_over_info
.aspace
= NULL
;
1322 step_over_info
.address
= 0;
1323 step_over_info
.nonsteppable_watchpoint_p
= 0;
1324 step_over_info
.thread
= -1;
1330 stepping_past_instruction_at (struct address_space
*aspace
,
1333 return (step_over_info
.aspace
!= NULL
1334 && breakpoint_address_match (aspace
, address
,
1335 step_over_info
.aspace
,
1336 step_over_info
.address
));
1342 thread_is_stepping_over_breakpoint (int thread
)
1344 return (step_over_info
.thread
!= -1
1345 && thread
== step_over_info
.thread
);
1351 stepping_past_nonsteppable_watchpoint (void)
1353 return step_over_info
.nonsteppable_watchpoint_p
;
1356 /* Returns true if step-over info is valid. */
1359 step_over_info_valid_p (void)
1361 return (step_over_info
.aspace
!= NULL
1362 || stepping_past_nonsteppable_watchpoint ());
1366 /* Displaced stepping. */
1368 /* In non-stop debugging mode, we must take special care to manage
1369 breakpoints properly; in particular, the traditional strategy for
1370 stepping a thread past a breakpoint it has hit is unsuitable.
1371 'Displaced stepping' is a tactic for stepping one thread past a
1372 breakpoint it has hit while ensuring that other threads running
1373 concurrently will hit the breakpoint as they should.
1375 The traditional way to step a thread T off a breakpoint in a
1376 multi-threaded program in all-stop mode is as follows:
1378 a0) Initially, all threads are stopped, and breakpoints are not
1380 a1) We single-step T, leaving breakpoints uninserted.
1381 a2) We insert breakpoints, and resume all threads.
1383 In non-stop debugging, however, this strategy is unsuitable: we
1384 don't want to have to stop all threads in the system in order to
1385 continue or step T past a breakpoint. Instead, we use displaced
1388 n0) Initially, T is stopped, other threads are running, and
1389 breakpoints are inserted.
1390 n1) We copy the instruction "under" the breakpoint to a separate
1391 location, outside the main code stream, making any adjustments
1392 to the instruction, register, and memory state as directed by
1394 n2) We single-step T over the instruction at its new location.
1395 n3) We adjust the resulting register and memory state as directed
1396 by T's architecture. This includes resetting T's PC to point
1397 back into the main instruction stream.
1400 This approach depends on the following gdbarch methods:
1402 - gdbarch_max_insn_length and gdbarch_displaced_step_location
1403 indicate where to copy the instruction, and how much space must
1404 be reserved there. We use these in step n1.
1406 - gdbarch_displaced_step_copy_insn copies a instruction to a new
1407 address, and makes any necessary adjustments to the instruction,
1408 register contents, and memory. We use this in step n1.
1410 - gdbarch_displaced_step_fixup adjusts registers and memory after
1411 we have successfully single-stepped the instruction, to yield the
1412 same effect the instruction would have had if we had executed it
1413 at its original address. We use this in step n3.
1415 The gdbarch_displaced_step_copy_insn and
1416 gdbarch_displaced_step_fixup functions must be written so that
1417 copying an instruction with gdbarch_displaced_step_copy_insn,
1418 single-stepping across the copied instruction, and then applying
1419 gdbarch_displaced_insn_fixup should have the same effects on the
1420 thread's memory and registers as stepping the instruction in place
1421 would have. Exactly which responsibilities fall to the copy and
1422 which fall to the fixup is up to the author of those functions.
1424 See the comments in gdbarch.sh for details.
1426 Note that displaced stepping and software single-step cannot
1427 currently be used in combination, although with some care I think
1428 they could be made to. Software single-step works by placing
1429 breakpoints on all possible subsequent instructions; if the
1430 displaced instruction is a PC-relative jump, those breakpoints
1431 could fall in very strange places --- on pages that aren't
1432 executable, or at addresses that are not proper instruction
1433 boundaries. (We do generally let other threads run while we wait
1434 to hit the software single-step breakpoint, and they might
1435 encounter such a corrupted instruction.) One way to work around
1436 this would be to have gdbarch_displaced_step_copy_insn fully
1437 simulate the effect of PC-relative instructions (and return NULL)
1438 on architectures that use software single-stepping.
1440 In non-stop mode, we can have independent and simultaneous step
1441 requests, so more than one thread may need to simultaneously step
1442 over a breakpoint. The current implementation assumes there is
1443 only one scratch space per process. In this case, we have to
1444 serialize access to the scratch space. If thread A wants to step
1445 over a breakpoint, but we are currently waiting for some other
1446 thread to complete a displaced step, we leave thread A stopped and
1447 place it in the displaced_step_request_queue. Whenever a displaced
1448 step finishes, we pick the next thread in the queue and start a new
1449 displaced step operation on it. See displaced_step_prepare and
1450 displaced_step_finish for details. */
1452 /* Return true if THREAD is doing a displaced step. */
1455 displaced_step_in_progress_thread (thread_info
*thread
)
1457 gdb_assert (thread
!= NULL
);
1459 return thread
->displaced_step_state
.in_progress ();
1462 /* Return true if INF has a thread doing a displaced step. */
1465 displaced_step_in_progress (inferior
*inf
)
1467 return inf
->displaced_step_state
.in_progress_count
> 0;
1470 /* Return true if any thread is doing a displaced step. */
1473 displaced_step_in_progress_any_thread ()
1475 for (inferior
*inf
: all_non_exited_inferiors ())
1477 if (displaced_step_in_progress (inf
))
1485 infrun_inferior_exit (struct inferior
*inf
)
1487 inf
->displaced_step_state
.reset ();
1491 infrun_inferior_execd (inferior
*inf
)
1493 /* If some threads where was doing a displaced step in this inferior at the
1494 moment of the exec, they no longer exist. Even if the exec'ing thread
1495 doing a displaced step, we don't want to to any fixup nor restore displaced
1496 stepping buffer bytes. */
1497 inf
->displaced_step_state
.reset ();
1499 for (thread_info
*thread
: inf
->threads ())
1500 thread
->displaced_step_state
.reset ();
1502 /* Since an in-line step is done with everything else stopped, if there was
1503 one in progress at the time of the exec, it must have been the exec'ing
1505 clear_step_over_info ();
1508 /* If ON, and the architecture supports it, GDB will use displaced
1509 stepping to step over breakpoints. If OFF, or if the architecture
1510 doesn't support it, GDB will instead use the traditional
1511 hold-and-step approach. If AUTO (which is the default), GDB will
1512 decide which technique to use to step over breakpoints depending on
1513 whether the target works in a non-stop way (see use_displaced_stepping). */
1515 static enum auto_boolean can_use_displaced_stepping
= AUTO_BOOLEAN_AUTO
;
1518 show_can_use_displaced_stepping (struct ui_file
*file
, int from_tty
,
1519 struct cmd_list_element
*c
,
1522 if (can_use_displaced_stepping
== AUTO_BOOLEAN_AUTO
)
1523 fprintf_filtered (file
,
1524 _("Debugger's willingness to use displaced stepping "
1525 "to step over breakpoints is %s (currently %s).\n"),
1526 value
, target_is_non_stop_p () ? "on" : "off");
1528 fprintf_filtered (file
,
1529 _("Debugger's willingness to use displaced stepping "
1530 "to step over breakpoints is %s.\n"), value
);
1533 /* Return true if the gdbarch implements the required methods to use
1534 displaced stepping. */
1537 gdbarch_supports_displaced_stepping (gdbarch
*arch
)
1539 /* Only check for the presence of `prepare`. The gdbarch verification ensures
1540 that if `prepare` is provided, so is `finish`. */
1541 return gdbarch_displaced_step_prepare_p (arch
);
1544 /* Return non-zero if displaced stepping can/should be used to step
1545 over breakpoints of thread TP. */
1548 use_displaced_stepping (thread_info
*tp
)
1550 /* If the user disabled it explicitly, don't use displaced stepping. */
1551 if (can_use_displaced_stepping
== AUTO_BOOLEAN_FALSE
)
1554 /* If "auto", only use displaced stepping if the target operates in a non-stop
1556 if (can_use_displaced_stepping
== AUTO_BOOLEAN_AUTO
1557 && !target_is_non_stop_p ())
1560 gdbarch
*gdbarch
= get_thread_regcache (tp
)->arch ();
1562 /* If the architecture doesn't implement displaced stepping, don't use
1564 if (!gdbarch_supports_displaced_stepping (gdbarch
))
1567 /* If recording, don't use displaced stepping. */
1568 if (find_record_target () != nullptr)
1571 /* If displaced stepping failed before for this inferior, don't bother trying
1573 if (tp
->inf
->displaced_step_state
.failed_before
)
1579 /* Simple function wrapper around displaced_step_thread_state::reset. */
1582 displaced_step_reset (displaced_step_thread_state
*displaced
)
1584 displaced
->reset ();
1587 /* A cleanup that wraps displaced_step_reset. We use this instead of, say,
1588 SCOPE_EXIT, because it needs to be discardable with "cleanup.release ()". */
1590 using displaced_step_reset_cleanup
= FORWARD_SCOPE_EXIT (displaced_step_reset
);
1595 displaced_step_dump_bytes (const gdb_byte
*buf
, size_t len
)
1599 for (size_t i
= 0; i
< len
; i
++)
1602 ret
+= string_printf ("%02x", buf
[i
]);
1604 ret
+= string_printf (" %02x", buf
[i
]);
1610 /* Prepare to single-step, using displaced stepping.
1612 Note that we cannot use displaced stepping when we have a signal to
1613 deliver. If we have a signal to deliver and an instruction to step
1614 over, then after the step, there will be no indication from the
1615 target whether the thread entered a signal handler or ignored the
1616 signal and stepped over the instruction successfully --- both cases
1617 result in a simple SIGTRAP. In the first case we mustn't do a
1618 fixup, and in the second case we must --- but we can't tell which.
1619 Comments in the code for 'random signals' in handle_inferior_event
1620 explain how we handle this case instead.
1622 Returns DISPLACED_STEP_PREPARE_STATUS_OK if preparing was successful -- this
1623 thread is going to be stepped now; DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
1624 if displaced stepping this thread got queued; or
1625 DISPLACED_STEP_PREPARE_STATUS_CANT if this instruction can't be displaced
1628 static displaced_step_prepare_status
1629 displaced_step_prepare_throw (thread_info
*tp
)
1631 regcache
*regcache
= get_thread_regcache (tp
);
1632 struct gdbarch
*gdbarch
= regcache
->arch ();
1633 displaced_step_thread_state
&disp_step_thread_state
1634 = tp
->displaced_step_state
;
1636 /* We should never reach this function if the architecture does not
1637 support displaced stepping. */
1638 gdb_assert (gdbarch_supports_displaced_stepping (gdbarch
));
1640 /* Nor if the thread isn't meant to step over a breakpoint. */
1641 gdb_assert (tp
->control
.trap_expected
);
1643 /* Disable range stepping while executing in the scratch pad. We
1644 want a single-step even if executing the displaced instruction in
1645 the scratch buffer lands within the stepping range (e.g., a
1647 tp
->control
.may_range_step
= 0;
1649 /* We are about to start a displaced step for this thread. If one is already
1650 in progress, something's wrong. */
1651 gdb_assert (!disp_step_thread_state
.in_progress ());
1653 if (tp
->inf
->displaced_step_state
.unavailable
)
1655 /* The gdbarch tells us it's not worth asking to try a prepare because
1656 it is likely that it will return unavailable, so don't bother asking. */
1658 displaced_debug_printf ("deferring step of %s",
1659 tp
->ptid
.to_string ().c_str ());
1661 global_thread_step_over_chain_enqueue (tp
);
1662 return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
;
1665 displaced_debug_printf ("displaced-stepping %s now",
1666 tp
->ptid
.to_string ().c_str ());
1668 scoped_restore_current_thread restore_thread
;
1670 switch_to_thread (tp
);
1672 CORE_ADDR original_pc
= regcache_read_pc (regcache
);
1673 CORE_ADDR displaced_pc
;
1675 displaced_step_prepare_status status
1676 = gdbarch_displaced_step_prepare (gdbarch
, tp
, displaced_pc
);
1678 if (status
== DISPLACED_STEP_PREPARE_STATUS_CANT
)
1680 displaced_debug_printf ("failed to prepare (%s)",
1681 tp
->ptid
.to_string ().c_str ());
1683 return DISPLACED_STEP_PREPARE_STATUS_CANT
;
1685 else if (status
== DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
)
1687 /* Not enough displaced stepping resources available, defer this
1688 request by placing it the queue. */
1690 displaced_debug_printf ("not enough resources available, "
1691 "deferring step of %s",
1692 tp
->ptid
.to_string ().c_str ());
1694 global_thread_step_over_chain_enqueue (tp
);
1696 return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
;
1699 gdb_assert (status
== DISPLACED_STEP_PREPARE_STATUS_OK
);
1701 /* Save the information we need to fix things up if the step
1703 disp_step_thread_state
.set (gdbarch
);
1705 tp
->inf
->displaced_step_state
.in_progress_count
++;
1707 displaced_debug_printf ("prepared successfully thread=%s, "
1708 "original_pc=%s, displaced_pc=%s",
1709 tp
->ptid
.to_string ().c_str (),
1710 paddress (gdbarch
, original_pc
),
1711 paddress (gdbarch
, displaced_pc
));
1713 return DISPLACED_STEP_PREPARE_STATUS_OK
;
1716 /* Wrapper for displaced_step_prepare_throw that disabled further
1717 attempts at displaced stepping if we get a memory error. */
1719 static displaced_step_prepare_status
1720 displaced_step_prepare (thread_info
*thread
)
1722 displaced_step_prepare_status status
1723 = DISPLACED_STEP_PREPARE_STATUS_CANT
;
1727 status
= displaced_step_prepare_throw (thread
);
1729 catch (const gdb_exception_error
&ex
)
1731 if (ex
.error
!= MEMORY_ERROR
1732 && ex
.error
!= NOT_SUPPORTED_ERROR
)
1735 infrun_debug_printf ("caught exception, disabling displaced stepping: %s",
1738 /* Be verbose if "set displaced-stepping" is "on", silent if
1740 if (can_use_displaced_stepping
== AUTO_BOOLEAN_TRUE
)
1742 warning (_("disabling displaced stepping: %s"),
1746 /* Disable further displaced stepping attempts. */
1747 thread
->inf
->displaced_step_state
.failed_before
= 1;
1753 /* If we displaced stepped an instruction successfully, adjust registers and
1754 memory to yield the same effect the instruction would have had if we had
1755 executed it at its original address, and return
1756 DISPLACED_STEP_FINISH_STATUS_OK. If the instruction didn't complete,
1757 relocate the PC and return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED.
1759 If the thread wasn't displaced stepping, return
1760 DISPLACED_STEP_FINISH_STATUS_OK as well. */
1762 static displaced_step_finish_status
1763 displaced_step_finish (thread_info
*event_thread
, enum gdb_signal signal
)
1765 displaced_step_thread_state
*displaced
= &event_thread
->displaced_step_state
;
1767 /* Was this thread performing a displaced step? */
1768 if (!displaced
->in_progress ())
1769 return DISPLACED_STEP_FINISH_STATUS_OK
;
1771 gdb_assert (event_thread
->inf
->displaced_step_state
.in_progress_count
> 0);
1772 event_thread
->inf
->displaced_step_state
.in_progress_count
--;
1774 /* Fixup may need to read memory/registers. Switch to the thread
1775 that we're fixing up. Also, target_stopped_by_watchpoint checks
1776 the current thread, and displaced_step_restore performs ptid-dependent
1777 memory accesses using current_inferior(). */
1778 switch_to_thread (event_thread
);
1780 displaced_step_reset_cleanup
cleanup (displaced
);
1782 /* Do the fixup, and release the resources acquired to do the displaced
1784 return gdbarch_displaced_step_finish (displaced
->get_original_gdbarch (),
1785 event_thread
, signal
);
1788 /* Data to be passed around while handling an event. This data is
1789 discarded between events. */
1790 struct execution_control_state
1792 execution_control_state ()
1799 this->target
= nullptr;
1800 this->ptid
= null_ptid
;
1801 this->event_thread
= nullptr;
1802 ws
= target_waitstatus ();
1803 stop_func_filled_in
= 0;
1804 stop_func_start
= 0;
1806 stop_func_name
= nullptr;
1808 hit_singlestep_breakpoint
= 0;
1811 process_stratum_target
*target
;
1813 /* The thread that got the event, if this was a thread event; NULL
1815 struct thread_info
*event_thread
;
1817 struct target_waitstatus ws
;
1818 int stop_func_filled_in
;
1819 CORE_ADDR stop_func_start
;
1820 CORE_ADDR stop_func_end
;
1821 const char *stop_func_name
;
1824 /* True if the event thread hit the single-step breakpoint of
1825 another thread. Thus the event doesn't cause a stop, the thread
1826 needs to be single-stepped past the single-step breakpoint before
1827 we can switch back to the original stepping thread. */
1828 int hit_singlestep_breakpoint
;
1831 /* Clear ECS and set it to point at TP. */
1834 reset_ecs (struct execution_control_state
*ecs
, struct thread_info
*tp
)
1837 ecs
->event_thread
= tp
;
1838 ecs
->ptid
= tp
->ptid
;
1841 static void keep_going_pass_signal (struct execution_control_state
*ecs
);
1842 static void prepare_to_wait (struct execution_control_state
*ecs
);
1843 static bool keep_going_stepped_thread (struct thread_info
*tp
);
1844 static step_over_what
thread_still_needs_step_over (struct thread_info
*tp
);
1846 /* Are there any pending step-over requests? If so, run all we can
1847 now and return true. Otherwise, return false. */
1850 start_step_over (void)
1852 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
1854 /* Don't start a new step-over if we already have an in-line
1855 step-over operation ongoing. */
1856 if (step_over_info_valid_p ())
1859 /* Steal the global thread step over chain. As we try to initiate displaced
1860 steps, threads will be enqueued in the global chain if no buffers are
1861 available. If we iterated on the global chain directly, we might iterate
1863 thread_step_over_list threads_to_step
1864 = std::move (global_thread_step_over_list
);
1866 infrun_debug_printf ("stealing global queue of threads to step, length = %d",
1867 thread_step_over_chain_length (threads_to_step
));
1869 bool started
= false;
1871 /* On scope exit (whatever the reason, return or exception), if there are
1872 threads left in the THREADS_TO_STEP chain, put back these threads in the
1876 if (threads_to_step
.empty ())
1877 infrun_debug_printf ("step-over queue now empty");
1880 infrun_debug_printf ("putting back %d threads to step in global queue",
1881 thread_step_over_chain_length (threads_to_step
));
1883 global_thread_step_over_chain_enqueue_chain
1884 (std::move (threads_to_step
));
1888 thread_step_over_list_safe_range range
1889 = make_thread_step_over_list_safe_range (threads_to_step
);
1891 for (thread_info
*tp
: range
)
1893 struct execution_control_state ecss
;
1894 struct execution_control_state
*ecs
= &ecss
;
1895 step_over_what step_what
;
1896 int must_be_in_line
;
1898 gdb_assert (!tp
->stop_requested
);
1900 if (tp
->inf
->displaced_step_state
.unavailable
)
1902 /* The arch told us to not even try preparing another displaced step
1903 for this inferior. Just leave the thread in THREADS_TO_STEP, it
1904 will get moved to the global chain on scope exit. */
1908 /* Remove thread from the THREADS_TO_STEP chain. If anything goes wrong
1909 while we try to prepare the displaced step, we don't add it back to
1910 the global step over chain. This is to avoid a thread staying in the
1911 step over chain indefinitely if something goes wrong when resuming it
1912 If the error is intermittent and it still needs a step over, it will
1913 get enqueued again when we try to resume it normally. */
1914 threads_to_step
.erase (threads_to_step
.iterator_to (*tp
));
1916 step_what
= thread_still_needs_step_over (tp
);
1917 must_be_in_line
= ((step_what
& STEP_OVER_WATCHPOINT
)
1918 || ((step_what
& STEP_OVER_BREAKPOINT
)
1919 && !use_displaced_stepping (tp
)));
1921 /* We currently stop all threads of all processes to step-over
1922 in-line. If we need to start a new in-line step-over, let
1923 any pending displaced steps finish first. */
1924 if (must_be_in_line
&& displaced_step_in_progress_any_thread ())
1926 global_thread_step_over_chain_enqueue (tp
);
1930 if (tp
->control
.trap_expected
1932 || tp
->executing ())
1934 internal_error (__FILE__
, __LINE__
,
1935 "[%s] has inconsistent state: "
1936 "trap_expected=%d, resumed=%d, executing=%d\n",
1937 tp
->ptid
.to_string ().c_str (),
1938 tp
->control
.trap_expected
,
1943 infrun_debug_printf ("resuming [%s] for step-over",
1944 tp
->ptid
.to_string ().c_str ());
1946 /* keep_going_pass_signal skips the step-over if the breakpoint
1947 is no longer inserted. In all-stop, we want to keep looking
1948 for a thread that needs a step-over instead of resuming TP,
1949 because we wouldn't be able to resume anything else until the
1950 target stops again. In non-stop, the resume always resumes
1951 only TP, so it's OK to let the thread resume freely. */
1952 if (!target_is_non_stop_p () && !step_what
)
1955 switch_to_thread (tp
);
1956 reset_ecs (ecs
, tp
);
1957 keep_going_pass_signal (ecs
);
1959 if (!ecs
->wait_some_more
)
1960 error (_("Command aborted."));
1962 /* If the thread's step over could not be initiated because no buffers
1963 were available, it was re-added to the global step over chain. */
1966 infrun_debug_printf ("[%s] was resumed.",
1967 tp
->ptid
.to_string ().c_str ());
1968 gdb_assert (!thread_is_in_step_over_chain (tp
));
1972 infrun_debug_printf ("[%s] was NOT resumed.",
1973 tp
->ptid
.to_string ().c_str ());
1974 gdb_assert (thread_is_in_step_over_chain (tp
));
1977 /* If we started a new in-line step-over, we're done. */
1978 if (step_over_info_valid_p ())
1980 gdb_assert (tp
->control
.trap_expected
);
1985 if (!target_is_non_stop_p ())
1987 /* On all-stop, shouldn't have resumed unless we needed a
1989 gdb_assert (tp
->control
.trap_expected
1990 || tp
->step_after_step_resume_breakpoint
);
1992 /* With remote targets (at least), in all-stop, we can't
1993 issue any further remote commands until the program stops
1999 /* Either the thread no longer needed a step-over, or a new
2000 displaced stepping sequence started. Even in the latter
2001 case, continue looking. Maybe we can also start another
2002 displaced step on a thread of other process. */
2008 /* Update global variables holding ptids to hold NEW_PTID if they were
2009 holding OLD_PTID. */
2011 infrun_thread_ptid_changed (process_stratum_target
*target
,
2012 ptid_t old_ptid
, ptid_t new_ptid
)
2014 if (inferior_ptid
== old_ptid
2015 && current_inferior ()->process_target () == target
)
2016 inferior_ptid
= new_ptid
;
2021 static const char schedlock_off
[] = "off";
2022 static const char schedlock_on
[] = "on";
2023 static const char schedlock_step
[] = "step";
2024 static const char schedlock_replay
[] = "replay";
2025 static const char *const scheduler_enums
[] = {
2032 static const char *scheduler_mode
= schedlock_replay
;
2034 show_scheduler_mode (struct ui_file
*file
, int from_tty
,
2035 struct cmd_list_element
*c
, const char *value
)
2037 fprintf_filtered (file
,
2038 _("Mode for locking scheduler "
2039 "during execution is \"%s\".\n"),
2044 set_schedlock_func (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2046 if (!target_can_lock_scheduler ())
2048 scheduler_mode
= schedlock_off
;
2049 error (_("Target '%s' cannot support this command."),
2050 target_shortname ());
2054 /* True if execution commands resume all threads of all processes by
2055 default; otherwise, resume only threads of the current inferior
2057 bool sched_multi
= false;
2059 /* Try to setup for software single stepping. Return true if target_resume()
2060 should use hardware single step.
2062 GDBARCH the current gdbarch. */
2065 maybe_software_singlestep (struct gdbarch
*gdbarch
)
2067 bool hw_step
= true;
2069 if (execution_direction
== EXEC_FORWARD
2070 && gdbarch_software_single_step_p (gdbarch
))
2071 hw_step
= !insert_single_step_breakpoints (gdbarch
);
2079 user_visible_resume_ptid (int step
)
2085 /* With non-stop mode on, threads are always handled
2087 resume_ptid
= inferior_ptid
;
2089 else if ((scheduler_mode
== schedlock_on
)
2090 || (scheduler_mode
== schedlock_step
&& step
))
2092 /* User-settable 'scheduler' mode requires solo thread
2094 resume_ptid
= inferior_ptid
;
2096 else if ((scheduler_mode
== schedlock_replay
)
2097 && target_record_will_replay (minus_one_ptid
, execution_direction
))
2099 /* User-settable 'scheduler' mode requires solo thread resume in replay
2101 resume_ptid
= inferior_ptid
;
2103 else if (!sched_multi
&& target_supports_multi_process ())
2105 /* Resume all threads of the current process (and none of other
2107 resume_ptid
= ptid_t (inferior_ptid
.pid ());
2111 /* Resume all threads of all processes. */
2112 resume_ptid
= RESUME_ALL
;
2120 process_stratum_target
*
2121 user_visible_resume_target (ptid_t resume_ptid
)
2123 return (resume_ptid
== minus_one_ptid
&& sched_multi
2125 : current_inferior ()->process_target ());
2128 /* Return a ptid representing the set of threads that we will resume,
2129 in the perspective of the target, assuming run control handling
2130 does not require leaving some threads stopped (e.g., stepping past
2131 breakpoint). USER_STEP indicates whether we're about to start the
2132 target for a stepping command. */
2135 internal_resume_ptid (int user_step
)
2137 /* In non-stop, we always control threads individually. Note that
2138 the target may always work in non-stop mode even with "set
2139 non-stop off", in which case user_visible_resume_ptid could
2140 return a wildcard ptid. */
2141 if (target_is_non_stop_p ())
2142 return inferior_ptid
;
2144 return user_visible_resume_ptid (user_step
);
2147 /* Wrapper for target_resume, that handles infrun-specific
2151 do_target_resume (ptid_t resume_ptid
, bool step
, enum gdb_signal sig
)
2153 struct thread_info
*tp
= inferior_thread ();
2155 gdb_assert (!tp
->stop_requested
);
2157 /* Install inferior's terminal modes. */
2158 target_terminal::inferior ();
2160 /* Avoid confusing the next resume, if the next stop/resume
2161 happens to apply to another thread. */
2162 tp
->set_stop_signal (GDB_SIGNAL_0
);
2164 /* Advise target which signals may be handled silently.
2166 If we have removed breakpoints because we are stepping over one
2167 in-line (in any thread), we need to receive all signals to avoid
2168 accidentally skipping a breakpoint during execution of a signal
2171 Likewise if we're displaced stepping, otherwise a trap for a
2172 breakpoint in a signal handler might be confused with the
2173 displaced step finishing. We don't make the displaced_step_finish
2174 step distinguish the cases instead, because:
2176 - a backtrace while stopped in the signal handler would show the
2177 scratch pad as frame older than the signal handler, instead of
2178 the real mainline code.
2180 - when the thread is later resumed, the signal handler would
2181 return to the scratch pad area, which would no longer be
2183 if (step_over_info_valid_p ()
2184 || displaced_step_in_progress (tp
->inf
))
2185 target_pass_signals ({});
2187 target_pass_signals (signal_pass
);
2189 target_resume (resume_ptid
, step
, sig
);
2191 if (target_can_async_p ())
2195 /* Resume the inferior. SIG is the signal to give the inferior
2196 (GDB_SIGNAL_0 for none). Note: don't call this directly; instead
2197 call 'resume', which handles exceptions. */
2200 resume_1 (enum gdb_signal sig
)
2202 struct regcache
*regcache
= get_current_regcache ();
2203 struct gdbarch
*gdbarch
= regcache
->arch ();
2204 struct thread_info
*tp
= inferior_thread ();
2205 const address_space
*aspace
= regcache
->aspace ();
2207 /* This represents the user's step vs continue request. When
2208 deciding whether "set scheduler-locking step" applies, it's the
2209 user's intention that counts. */
2210 const int user_step
= tp
->control
.stepping_command
;
2211 /* This represents what we'll actually request the target to do.
2212 This can decay from a step to a continue, if e.g., we need to
2213 implement single-stepping with breakpoints (software
2217 gdb_assert (!tp
->stop_requested
);
2218 gdb_assert (!thread_is_in_step_over_chain (tp
));
2220 if (tp
->has_pending_waitstatus ())
2223 ("thread %s has pending wait "
2224 "status %s (currently_stepping=%d).",
2225 tp
->ptid
.to_string ().c_str (),
2226 tp
->pending_waitstatus ().to_string ().c_str (),
2227 currently_stepping (tp
));
2229 tp
->inf
->process_target ()->threads_executing
= true;
2230 tp
->set_resumed (true);
2232 /* FIXME: What should we do if we are supposed to resume this
2233 thread with a signal? Maybe we should maintain a queue of
2234 pending signals to deliver. */
2235 if (sig
!= GDB_SIGNAL_0
)
2237 warning (_("Couldn't deliver signal %s to %s."),
2238 gdb_signal_to_name (sig
),
2239 tp
->ptid
.to_string ().c_str ());
2242 tp
->set_stop_signal (GDB_SIGNAL_0
);
2244 if (target_can_async_p ())
2247 /* Tell the event loop we have an event to process. */
2248 mark_async_event_handler (infrun_async_inferior_event_token
);
2253 tp
->stepped_breakpoint
= 0;
2255 /* Depends on stepped_breakpoint. */
2256 step
= currently_stepping (tp
);
2258 if (current_inferior ()->waiting_for_vfork_done
)
2260 /* Don't try to single-step a vfork parent that is waiting for
2261 the child to get out of the shared memory region (by exec'ing
2262 or exiting). This is particularly important on software
2263 single-step archs, as the child process would trip on the
2264 software single step breakpoint inserted for the parent
2265 process. Since the parent will not actually execute any
2266 instruction until the child is out of the shared region (such
2267 are vfork's semantics), it is safe to simply continue it.
2268 Eventually, we'll see a TARGET_WAITKIND_VFORK_DONE event for
2269 the parent, and tell it to `keep_going', which automatically
2270 re-sets it stepping. */
2271 infrun_debug_printf ("resume : clear step");
2275 CORE_ADDR pc
= regcache_read_pc (regcache
);
2277 infrun_debug_printf ("step=%d, signal=%s, trap_expected=%d, "
2278 "current thread [%s] at %s",
2279 step
, gdb_signal_to_symbol_string (sig
),
2280 tp
->control
.trap_expected
,
2281 inferior_ptid
.to_string ().c_str (),
2282 paddress (gdbarch
, pc
));
2284 /* Normally, by the time we reach `resume', the breakpoints are either
2285 removed or inserted, as appropriate. The exception is if we're sitting
2286 at a permanent breakpoint; we need to step over it, but permanent
2287 breakpoints can't be removed. So we have to test for it here. */
2288 if (breakpoint_here_p (aspace
, pc
) == permanent_breakpoint_here
)
2290 if (sig
!= GDB_SIGNAL_0
)
2292 /* We have a signal to pass to the inferior. The resume
2293 may, or may not take us to the signal handler. If this
2294 is a step, we'll need to stop in the signal handler, if
2295 there's one, (if the target supports stepping into
2296 handlers), or in the next mainline instruction, if
2297 there's no handler. If this is a continue, we need to be
2298 sure to run the handler with all breakpoints inserted.
2299 In all cases, set a breakpoint at the current address
2300 (where the handler returns to), and once that breakpoint
2301 is hit, resume skipping the permanent breakpoint. If
2302 that breakpoint isn't hit, then we've stepped into the
2303 signal handler (or hit some other event). We'll delete
2304 the step-resume breakpoint then. */
2306 infrun_debug_printf ("resume: skipping permanent breakpoint, "
2307 "deliver signal first");
2309 clear_step_over_info ();
2310 tp
->control
.trap_expected
= 0;
2312 if (tp
->control
.step_resume_breakpoint
== NULL
)
2314 /* Set a "high-priority" step-resume, as we don't want
2315 user breakpoints at PC to trigger (again) when this
2317 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2318 gdb_assert (tp
->control
.step_resume_breakpoint
->loc
->permanent
);
2320 tp
->step_after_step_resume_breakpoint
= step
;
2323 insert_breakpoints ();
2327 /* There's no signal to pass, we can go ahead and skip the
2328 permanent breakpoint manually. */
2329 infrun_debug_printf ("skipping permanent breakpoint");
2330 gdbarch_skip_permanent_breakpoint (gdbarch
, regcache
);
2331 /* Update pc to reflect the new address from which we will
2332 execute instructions. */
2333 pc
= regcache_read_pc (regcache
);
2337 /* We've already advanced the PC, so the stepping part
2338 is done. Now we need to arrange for a trap to be
2339 reported to handle_inferior_event. Set a breakpoint
2340 at the current PC, and run to it. Don't update
2341 prev_pc, because if we end in
2342 switch_back_to_stepped_thread, we want the "expected
2343 thread advanced also" branch to be taken. IOW, we
2344 don't want this thread to step further from PC
2346 gdb_assert (!step_over_info_valid_p ());
2347 insert_single_step_breakpoint (gdbarch
, aspace
, pc
);
2348 insert_breakpoints ();
2350 resume_ptid
= internal_resume_ptid (user_step
);
2351 do_target_resume (resume_ptid
, false, GDB_SIGNAL_0
);
2352 tp
->set_resumed (true);
2358 /* If we have a breakpoint to step over, make sure to do a single
2359 step only. Same if we have software watchpoints. */
2360 if (tp
->control
.trap_expected
|| bpstat_should_step ())
2361 tp
->control
.may_range_step
= 0;
2363 /* If displaced stepping is enabled, step over breakpoints by executing a
2364 copy of the instruction at a different address.
2366 We can't use displaced stepping when we have a signal to deliver;
2367 the comments for displaced_step_prepare explain why. The
2368 comments in the handle_inferior event for dealing with 'random
2369 signals' explain what we do instead.
2371 We can't use displaced stepping when we are waiting for vfork_done
2372 event, displaced stepping breaks the vfork child similarly as single
2373 step software breakpoint. */
2374 if (tp
->control
.trap_expected
2375 && use_displaced_stepping (tp
)
2376 && !step_over_info_valid_p ()
2377 && sig
== GDB_SIGNAL_0
2378 && !current_inferior ()->waiting_for_vfork_done
)
2380 displaced_step_prepare_status prepare_status
2381 = displaced_step_prepare (tp
);
2383 if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
)
2385 infrun_debug_printf ("Got placed in step-over queue");
2387 tp
->control
.trap_expected
= 0;
2390 else if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_CANT
)
2392 /* Fallback to stepping over the breakpoint in-line. */
2394 if (target_is_non_stop_p ())
2395 stop_all_threads ();
2397 set_step_over_info (regcache
->aspace (),
2398 regcache_read_pc (regcache
), 0, tp
->global_num
);
2400 step
= maybe_software_singlestep (gdbarch
);
2402 insert_breakpoints ();
2404 else if (prepare_status
== DISPLACED_STEP_PREPARE_STATUS_OK
)
2406 /* Update pc to reflect the new address from which we will
2407 execute instructions due to displaced stepping. */
2408 pc
= regcache_read_pc (get_thread_regcache (tp
));
2410 step
= gdbarch_displaced_step_hw_singlestep (gdbarch
);
2413 gdb_assert_not_reached ("Invalid displaced_step_prepare_status "
2417 /* Do we need to do it the hard way, w/temp breakpoints? */
2419 step
= maybe_software_singlestep (gdbarch
);
2421 /* Currently, our software single-step implementation leads to different
2422 results than hardware single-stepping in one situation: when stepping
2423 into delivering a signal which has an associated signal handler,
2424 hardware single-step will stop at the first instruction of the handler,
2425 while software single-step will simply skip execution of the handler.
2427 For now, this difference in behavior is accepted since there is no
2428 easy way to actually implement single-stepping into a signal handler
2429 without kernel support.
2431 However, there is one scenario where this difference leads to follow-on
2432 problems: if we're stepping off a breakpoint by removing all breakpoints
2433 and then single-stepping. In this case, the software single-step
2434 behavior means that even if there is a *breakpoint* in the signal
2435 handler, GDB still would not stop.
2437 Fortunately, we can at least fix this particular issue. We detect
2438 here the case where we are about to deliver a signal while software
2439 single-stepping with breakpoints removed. In this situation, we
2440 revert the decisions to remove all breakpoints and insert single-
2441 step breakpoints, and instead we install a step-resume breakpoint
2442 at the current address, deliver the signal without stepping, and
2443 once we arrive back at the step-resume breakpoint, actually step
2444 over the breakpoint we originally wanted to step over. */
2445 if (thread_has_single_step_breakpoints_set (tp
)
2446 && sig
!= GDB_SIGNAL_0
2447 && step_over_info_valid_p ())
2449 /* If we have nested signals or a pending signal is delivered
2450 immediately after a handler returns, might already have
2451 a step-resume breakpoint set on the earlier handler. We cannot
2452 set another step-resume breakpoint; just continue on until the
2453 original breakpoint is hit. */
2454 if (tp
->control
.step_resume_breakpoint
== NULL
)
2456 insert_hp_step_resume_breakpoint_at_frame (get_current_frame ());
2457 tp
->step_after_step_resume_breakpoint
= 1;
2460 delete_single_step_breakpoints (tp
);
2462 clear_step_over_info ();
2463 tp
->control
.trap_expected
= 0;
2465 insert_breakpoints ();
2468 /* If STEP is set, it's a request to use hardware stepping
2469 facilities. But in that case, we should never
2470 use singlestep breakpoint. */
2471 gdb_assert (!(thread_has_single_step_breakpoints_set (tp
) && step
));
2473 /* Decide the set of threads to ask the target to resume. */
2474 if (tp
->control
.trap_expected
)
2476 /* We're allowing a thread to run past a breakpoint it has
2477 hit, either by single-stepping the thread with the breakpoint
2478 removed, or by displaced stepping, with the breakpoint inserted.
2479 In the former case, we need to single-step only this thread,
2480 and keep others stopped, as they can miss this breakpoint if
2481 allowed to run. That's not really a problem for displaced
2482 stepping, but, we still keep other threads stopped, in case
2483 another thread is also stopped for a breakpoint waiting for
2484 its turn in the displaced stepping queue. */
2485 resume_ptid
= inferior_ptid
;
2488 resume_ptid
= internal_resume_ptid (user_step
);
2490 if (execution_direction
!= EXEC_REVERSE
2491 && step
&& breakpoint_inserted_here_p (aspace
, pc
))
2493 /* There are two cases where we currently need to step a
2494 breakpoint instruction when we have a signal to deliver:
2496 - See handle_signal_stop where we handle random signals that
2497 could take out us out of the stepping range. Normally, in
2498 that case we end up continuing (instead of stepping) over the
2499 signal handler with a breakpoint at PC, but there are cases
2500 where we should _always_ single-step, even if we have a
2501 step-resume breakpoint, like when a software watchpoint is
2502 set. Assuming single-stepping and delivering a signal at the
2503 same time would takes us to the signal handler, then we could
2504 have removed the breakpoint at PC to step over it. However,
2505 some hardware step targets (like e.g., Mac OS) can't step
2506 into signal handlers, and for those, we need to leave the
2507 breakpoint at PC inserted, as otherwise if the handler
2508 recurses and executes PC again, it'll miss the breakpoint.
2509 So we leave the breakpoint inserted anyway, but we need to
2510 record that we tried to step a breakpoint instruction, so
2511 that adjust_pc_after_break doesn't end up confused.
2513 - In non-stop if we insert a breakpoint (e.g., a step-resume)
2514 in one thread after another thread that was stepping had been
2515 momentarily paused for a step-over. When we re-resume the
2516 stepping thread, it may be resumed from that address with a
2517 breakpoint that hasn't trapped yet. Seen with
2518 gdb.threads/non-stop-fair-events.exp, on targets that don't
2519 do displaced stepping. */
2521 infrun_debug_printf ("resume: [%s] stepped breakpoint",
2522 tp
->ptid
.to_string ().c_str ());
2524 tp
->stepped_breakpoint
= 1;
2526 /* Most targets can step a breakpoint instruction, thus
2527 executing it normally. But if this one cannot, just
2528 continue and we will hit it anyway. */
2529 if (gdbarch_cannot_step_breakpoint (gdbarch
))
2534 && tp
->control
.trap_expected
2535 && use_displaced_stepping (tp
)
2536 && !step_over_info_valid_p ())
2538 struct regcache
*resume_regcache
= get_thread_regcache (tp
);
2539 struct gdbarch
*resume_gdbarch
= resume_regcache
->arch ();
2540 CORE_ADDR actual_pc
= regcache_read_pc (resume_regcache
);
2543 read_memory (actual_pc
, buf
, sizeof (buf
));
2544 displaced_debug_printf ("run %s: %s",
2545 paddress (resume_gdbarch
, actual_pc
),
2546 displaced_step_dump_bytes
2547 (buf
, sizeof (buf
)).c_str ());
2550 if (tp
->control
.may_range_step
)
2552 /* If we're resuming a thread with the PC out of the step
2553 range, then we're doing some nested/finer run control
2554 operation, like stepping the thread out of the dynamic
2555 linker or the displaced stepping scratch pad. We
2556 shouldn't have allowed a range step then. */
2557 gdb_assert (pc_in_thread_step_range (pc
, tp
));
2560 do_target_resume (resume_ptid
, step
, sig
);
2561 tp
->set_resumed (true);
2564 /* Resume the inferior. SIG is the signal to give the inferior
2565 (GDB_SIGNAL_0 for none). This is a wrapper around 'resume_1' that
2566 rolls back state on error. */
2569 resume (gdb_signal sig
)
2575 catch (const gdb_exception
&ex
)
2577 /* If resuming is being aborted for any reason, delete any
2578 single-step breakpoint resume_1 may have created, to avoid
2579 confusing the following resumption, and to avoid leaving
2580 single-step breakpoints perturbing other threads, in case
2581 we're running in non-stop mode. */
2582 if (inferior_ptid
!= null_ptid
)
2583 delete_single_step_breakpoints (inferior_thread ());
2593 /* Counter that tracks number of user visible stops. This can be used
2594 to tell whether a command has proceeded the inferior past the
2595 current location. This allows e.g., inferior function calls in
2596 breakpoint commands to not interrupt the command list. When the
2597 call finishes successfully, the inferior is standing at the same
2598 breakpoint as if nothing happened (and so we don't call
2600 static ULONGEST current_stop_id
;
2607 return current_stop_id
;
2610 /* Called when we report a user visible stop. */
2618 /* Clear out all variables saying what to do when inferior is continued.
2619 First do this, then set the ones you want, then call `proceed'. */
2622 clear_proceed_status_thread (struct thread_info
*tp
)
2624 infrun_debug_printf ("%s", tp
->ptid
.to_string ().c_str ());
2626 /* If we're starting a new sequence, then the previous finished
2627 single-step is no longer relevant. */
2628 if (tp
->has_pending_waitstatus ())
2630 if (tp
->stop_reason () == TARGET_STOPPED_BY_SINGLE_STEP
)
2632 infrun_debug_printf ("pending event of %s was a finished step. "
2634 tp
->ptid
.to_string ().c_str ());
2636 tp
->clear_pending_waitstatus ();
2637 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
2642 ("thread %s has pending wait status %s (currently_stepping=%d).",
2643 tp
->ptid
.to_string ().c_str (),
2644 tp
->pending_waitstatus ().to_string ().c_str (),
2645 currently_stepping (tp
));
2649 /* If this signal should not be seen by program, give it zero.
2650 Used for debugging signals. */
2651 if (!signal_pass_state (tp
->stop_signal ()))
2652 tp
->set_stop_signal (GDB_SIGNAL_0
);
2654 delete tp
->thread_fsm
;
2655 tp
->thread_fsm
= NULL
;
2657 tp
->control
.trap_expected
= 0;
2658 tp
->control
.step_range_start
= 0;
2659 tp
->control
.step_range_end
= 0;
2660 tp
->control
.may_range_step
= 0;
2661 tp
->control
.step_frame_id
= null_frame_id
;
2662 tp
->control
.step_stack_frame_id
= null_frame_id
;
2663 tp
->control
.step_over_calls
= STEP_OVER_UNDEBUGGABLE
;
2664 tp
->control
.step_start_function
= NULL
;
2665 tp
->stop_requested
= 0;
2667 tp
->control
.stop_step
= 0;
2669 tp
->control
.proceed_to_finish
= 0;
2671 tp
->control
.stepping_command
= 0;
2673 /* Discard any remaining commands or status from previous stop. */
2674 bpstat_clear (&tp
->control
.stop_bpstat
);
2678 clear_proceed_status (int step
)
2680 /* With scheduler-locking replay, stop replaying other threads if we're
2681 not replaying the user-visible resume ptid.
2683 This is a convenience feature to not require the user to explicitly
2684 stop replaying the other threads. We're assuming that the user's
2685 intent is to resume tracing the recorded process. */
2686 if (!non_stop
&& scheduler_mode
== schedlock_replay
2687 && target_record_is_replaying (minus_one_ptid
)
2688 && !target_record_will_replay (user_visible_resume_ptid (step
),
2689 execution_direction
))
2690 target_record_stop_replaying ();
2692 if (!non_stop
&& inferior_ptid
!= null_ptid
)
2694 ptid_t resume_ptid
= user_visible_resume_ptid (step
);
2695 process_stratum_target
*resume_target
2696 = user_visible_resume_target (resume_ptid
);
2698 /* In all-stop mode, delete the per-thread status of all threads
2699 we're about to resume, implicitly and explicitly. */
2700 for (thread_info
*tp
: all_non_exited_threads (resume_target
, resume_ptid
))
2701 clear_proceed_status_thread (tp
);
2704 if (inferior_ptid
!= null_ptid
)
2706 struct inferior
*inferior
;
2710 /* If in non-stop mode, only delete the per-thread status of
2711 the current thread. */
2712 clear_proceed_status_thread (inferior_thread ());
2715 inferior
= current_inferior ();
2716 inferior
->control
.stop_soon
= NO_STOP_QUIETLY
;
2719 gdb::observers::about_to_proceed
.notify ();
2722 /* Returns true if TP is still stopped at a breakpoint that needs
2723 stepping-over in order to make progress. If the breakpoint is gone
2724 meanwhile, we can skip the whole step-over dance. */
2727 thread_still_needs_step_over_bp (struct thread_info
*tp
)
2729 if (tp
->stepping_over_breakpoint
)
2731 struct regcache
*regcache
= get_thread_regcache (tp
);
2733 if (breakpoint_here_p (regcache
->aspace (),
2734 regcache_read_pc (regcache
))
2735 == ordinary_breakpoint_here
)
2738 tp
->stepping_over_breakpoint
= 0;
2744 /* Check whether thread TP still needs to start a step-over in order
2745 to make progress when resumed. Returns an bitwise or of enum
2746 step_over_what bits, indicating what needs to be stepped over. */
2748 static step_over_what
2749 thread_still_needs_step_over (struct thread_info
*tp
)
2751 step_over_what what
= 0;
2753 if (thread_still_needs_step_over_bp (tp
))
2754 what
|= STEP_OVER_BREAKPOINT
;
2756 if (tp
->stepping_over_watchpoint
2757 && !target_have_steppable_watchpoint ())
2758 what
|= STEP_OVER_WATCHPOINT
;
2763 /* Returns true if scheduler locking applies. STEP indicates whether
2764 we're about to do a step/next-like command to a thread. */
2767 schedlock_applies (struct thread_info
*tp
)
2769 return (scheduler_mode
== schedlock_on
2770 || (scheduler_mode
== schedlock_step
2771 && tp
->control
.stepping_command
)
2772 || (scheduler_mode
== schedlock_replay
2773 && target_record_will_replay (minus_one_ptid
,
2774 execution_direction
)));
2777 /* Set process_stratum_target::COMMIT_RESUMED_STATE in all target
2778 stacks that have threads executing and don't have threads with
2782 maybe_set_commit_resumed_all_targets ()
2784 scoped_restore_current_thread restore_thread
;
2786 for (inferior
*inf
: all_non_exited_inferiors ())
2788 process_stratum_target
*proc_target
= inf
->process_target ();
2790 if (proc_target
->commit_resumed_state
)
2792 /* We already set this in a previous iteration, via another
2793 inferior sharing the process_stratum target. */
2797 /* If the target has no resumed threads, it would be useless to
2798 ask it to commit the resumed threads. */
2799 if (!proc_target
->threads_executing
)
2801 infrun_debug_printf ("not requesting commit-resumed for target "
2802 "%s, no resumed threads",
2803 proc_target
->shortname ());
2807 /* As an optimization, if a thread from this target has some
2808 status to report, handle it before requiring the target to
2809 commit its resumed threads: handling the status might lead to
2810 resuming more threads. */
2811 if (proc_target
->has_resumed_with_pending_wait_status ())
2813 infrun_debug_printf ("not requesting commit-resumed for target %s, a"
2814 " thread has a pending waitstatus",
2815 proc_target
->shortname ());
2819 switch_to_inferior_no_thread (inf
);
2821 if (target_has_pending_events ())
2823 infrun_debug_printf ("not requesting commit-resumed for target %s, "
2824 "target has pending events",
2825 proc_target
->shortname ());
2829 infrun_debug_printf ("enabling commit-resumed for target %s",
2830 proc_target
->shortname ());
2832 proc_target
->commit_resumed_state
= true;
2839 maybe_call_commit_resumed_all_targets ()
2841 scoped_restore_current_thread restore_thread
;
2843 for (inferior
*inf
: all_non_exited_inferiors ())
2845 process_stratum_target
*proc_target
= inf
->process_target ();
2847 if (!proc_target
->commit_resumed_state
)
2850 switch_to_inferior_no_thread (inf
);
2852 infrun_debug_printf ("calling commit_resumed for target %s",
2853 proc_target
->shortname());
2855 target_commit_resumed ();
2859 /* To track nesting of scoped_disable_commit_resumed objects, ensuring
2860 that only the outermost one attempts to re-enable
2862 static bool enable_commit_resumed
= true;
2866 scoped_disable_commit_resumed::scoped_disable_commit_resumed
2867 (const char *reason
)
2868 : m_reason (reason
),
2869 m_prev_enable_commit_resumed (enable_commit_resumed
)
2871 infrun_debug_printf ("reason=%s", m_reason
);
2873 enable_commit_resumed
= false;
2875 for (inferior
*inf
: all_non_exited_inferiors ())
2877 process_stratum_target
*proc_target
= inf
->process_target ();
2879 if (m_prev_enable_commit_resumed
)
2881 /* This is the outermost instance: force all
2882 COMMIT_RESUMED_STATE to false. */
2883 proc_target
->commit_resumed_state
= false;
2887 /* This is not the outermost instance, we expect
2888 COMMIT_RESUMED_STATE to have been cleared by the
2889 outermost instance. */
2890 gdb_assert (!proc_target
->commit_resumed_state
);
2898 scoped_disable_commit_resumed::reset ()
2904 infrun_debug_printf ("reason=%s", m_reason
);
2906 gdb_assert (!enable_commit_resumed
);
2908 enable_commit_resumed
= m_prev_enable_commit_resumed
;
2910 if (m_prev_enable_commit_resumed
)
2912 /* This is the outermost instance, re-enable
2913 COMMIT_RESUMED_STATE on the targets where it's possible. */
2914 maybe_set_commit_resumed_all_targets ();
2918 /* This is not the outermost instance, we expect
2919 COMMIT_RESUMED_STATE to still be false. */
2920 for (inferior
*inf
: all_non_exited_inferiors ())
2922 process_stratum_target
*proc_target
= inf
->process_target ();
2923 gdb_assert (!proc_target
->commit_resumed_state
);
2930 scoped_disable_commit_resumed::~scoped_disable_commit_resumed ()
2938 scoped_disable_commit_resumed::reset_and_commit ()
2941 maybe_call_commit_resumed_all_targets ();
2946 scoped_enable_commit_resumed::scoped_enable_commit_resumed
2947 (const char *reason
)
2948 : m_reason (reason
),
2949 m_prev_enable_commit_resumed (enable_commit_resumed
)
2951 infrun_debug_printf ("reason=%s", m_reason
);
2953 if (!enable_commit_resumed
)
2955 enable_commit_resumed
= true;
2957 /* Re-enable COMMIT_RESUMED_STATE on the targets where it's
2959 maybe_set_commit_resumed_all_targets ();
2961 maybe_call_commit_resumed_all_targets ();
2967 scoped_enable_commit_resumed::~scoped_enable_commit_resumed ()
2969 infrun_debug_printf ("reason=%s", m_reason
);
2971 gdb_assert (enable_commit_resumed
);
2973 enable_commit_resumed
= m_prev_enable_commit_resumed
;
2975 if (!enable_commit_resumed
)
2977 /* Force all COMMIT_RESUMED_STATE back to false. */
2978 for (inferior
*inf
: all_non_exited_inferiors ())
2980 process_stratum_target
*proc_target
= inf
->process_target ();
2981 proc_target
->commit_resumed_state
= false;
2986 /* Check that all the targets we're about to resume are in non-stop
2987 mode. Ideally, we'd only care whether all targets support
2988 target-async, but we're not there yet. E.g., stop_all_threads
2989 doesn't know how to handle all-stop targets. Also, the remote
2990 protocol in all-stop mode is synchronous, irrespective of
2991 target-async, which means that things like a breakpoint re-set
2992 triggered by one target would try to read memory from all targets
2996 check_multi_target_resumption (process_stratum_target
*resume_target
)
2998 if (!non_stop
&& resume_target
== nullptr)
3000 scoped_restore_current_thread restore_thread
;
3002 /* This is used to track whether we're resuming more than one
3004 process_stratum_target
*first_connection
= nullptr;
3006 /* The first inferior we see with a target that does not work in
3007 always-non-stop mode. */
3008 inferior
*first_not_non_stop
= nullptr;
3010 for (inferior
*inf
: all_non_exited_inferiors ())
3012 switch_to_inferior_no_thread (inf
);
3014 if (!target_has_execution ())
3017 process_stratum_target
*proc_target
3018 = current_inferior ()->process_target();
3020 if (!target_is_non_stop_p ())
3021 first_not_non_stop
= inf
;
3023 if (first_connection
== nullptr)
3024 first_connection
= proc_target
;
3025 else if (first_connection
!= proc_target
3026 && first_not_non_stop
!= nullptr)
3028 switch_to_inferior_no_thread (first_not_non_stop
);
3030 proc_target
= current_inferior ()->process_target();
3032 error (_("Connection %d (%s) does not support "
3033 "multi-target resumption."),
3034 proc_target
->connection_number
,
3035 make_target_connection_string (proc_target
).c_str ());
3041 /* Basic routine for continuing the program in various fashions.
3043 ADDR is the address to resume at, or -1 for resume where stopped.
3044 SIGGNAL is the signal to give it, or GDB_SIGNAL_0 for none,
3045 or GDB_SIGNAL_DEFAULT for act according to how it stopped.
3047 You should call clear_proceed_status before calling proceed. */
3050 proceed (CORE_ADDR addr
, enum gdb_signal siggnal
)
3052 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
3054 struct regcache
*regcache
;
3055 struct gdbarch
*gdbarch
;
3057 struct execution_control_state ecss
;
3058 struct execution_control_state
*ecs
= &ecss
;
3060 /* If we're stopped at a fork/vfork, follow the branch set by the
3061 "set follow-fork-mode" command; otherwise, we'll just proceed
3062 resuming the current thread. */
3063 if (!follow_fork ())
3065 /* The target for some reason decided not to resume. */
3067 if (target_can_async_p ())
3068 inferior_event_handler (INF_EXEC_COMPLETE
);
3072 /* We'll update this if & when we switch to a new thread. */
3073 previous_inferior_ptid
= inferior_ptid
;
3075 regcache
= get_current_regcache ();
3076 gdbarch
= regcache
->arch ();
3077 const address_space
*aspace
= regcache
->aspace ();
3079 pc
= regcache_read_pc_protected (regcache
);
3081 thread_info
*cur_thr
= inferior_thread ();
3083 /* Fill in with reasonable starting values. */
3084 init_thread_stepping_state (cur_thr
);
3086 gdb_assert (!thread_is_in_step_over_chain (cur_thr
));
3089 = user_visible_resume_ptid (cur_thr
->control
.stepping_command
);
3090 process_stratum_target
*resume_target
3091 = user_visible_resume_target (resume_ptid
);
3093 check_multi_target_resumption (resume_target
);
3095 if (addr
== (CORE_ADDR
) -1)
3097 if (cur_thr
->stop_pc_p ()
3098 && pc
== cur_thr
->stop_pc ()
3099 && breakpoint_here_p (aspace
, pc
) == ordinary_breakpoint_here
3100 && execution_direction
!= EXEC_REVERSE
)
3101 /* There is a breakpoint at the address we will resume at,
3102 step one instruction before inserting breakpoints so that
3103 we do not stop right away (and report a second hit at this
3106 Note, we don't do this in reverse, because we won't
3107 actually be executing the breakpoint insn anyway.
3108 We'll be (un-)executing the previous instruction. */
3109 cur_thr
->stepping_over_breakpoint
= 1;
3110 else if (gdbarch_single_step_through_delay_p (gdbarch
)
3111 && gdbarch_single_step_through_delay (gdbarch
,
3112 get_current_frame ()))
3113 /* We stepped onto an instruction that needs to be stepped
3114 again before re-inserting the breakpoint, do so. */
3115 cur_thr
->stepping_over_breakpoint
= 1;
3119 regcache_write_pc (regcache
, addr
);
3122 if (siggnal
!= GDB_SIGNAL_DEFAULT
)
3123 cur_thr
->set_stop_signal (siggnal
);
3125 /* If an exception is thrown from this point on, make sure to
3126 propagate GDB's knowledge of the executing state to the
3127 frontend/user running state. */
3128 scoped_finish_thread_state
finish_state (resume_target
, resume_ptid
);
3130 /* Even if RESUME_PTID is a wildcard, and we end up resuming fewer
3131 threads (e.g., we might need to set threads stepping over
3132 breakpoints first), from the user/frontend's point of view, all
3133 threads in RESUME_PTID are now running. Unless we're calling an
3134 inferior function, as in that case we pretend the inferior
3135 doesn't run at all. */
3136 if (!cur_thr
->control
.in_infcall
)
3137 set_running (resume_target
, resume_ptid
, true);
3139 infrun_debug_printf ("addr=%s, signal=%s", paddress (gdbarch
, addr
),
3140 gdb_signal_to_symbol_string (siggnal
));
3142 annotate_starting ();
3144 /* Make sure that output from GDB appears before output from the
3146 gdb_flush (gdb_stdout
);
3148 /* Since we've marked the inferior running, give it the terminal. A
3149 QUIT/Ctrl-C from here on is forwarded to the target (which can
3150 still detect attempts to unblock a stuck connection with repeated
3151 Ctrl-C from within target_pass_ctrlc). */
3152 target_terminal::inferior ();
3154 /* In a multi-threaded task we may select another thread and
3155 then continue or step.
3157 But if a thread that we're resuming had stopped at a breakpoint,
3158 it will immediately cause another breakpoint stop without any
3159 execution (i.e. it will report a breakpoint hit incorrectly). So
3160 we must step over it first.
3162 Look for threads other than the current (TP) that reported a
3163 breakpoint hit and haven't been resumed yet since. */
3165 /* If scheduler locking applies, we can avoid iterating over all
3167 if (!non_stop
&& !schedlock_applies (cur_thr
))
3169 for (thread_info
*tp
: all_non_exited_threads (resume_target
,
3172 switch_to_thread_no_regs (tp
);
3174 /* Ignore the current thread here. It's handled
3179 if (!thread_still_needs_step_over (tp
))
3182 gdb_assert (!thread_is_in_step_over_chain (tp
));
3184 infrun_debug_printf ("need to step-over [%s] first",
3185 tp
->ptid
.to_string ().c_str ());
3187 global_thread_step_over_chain_enqueue (tp
);
3190 switch_to_thread (cur_thr
);
3193 /* Enqueue the current thread last, so that we move all other
3194 threads over their breakpoints first. */
3195 if (cur_thr
->stepping_over_breakpoint
)
3196 global_thread_step_over_chain_enqueue (cur_thr
);
3198 /* If the thread isn't started, we'll still need to set its prev_pc,
3199 so that switch_back_to_stepped_thread knows the thread hasn't
3200 advanced. Must do this before resuming any thread, as in
3201 all-stop/remote, once we resume we can't send any other packet
3202 until the target stops again. */
3203 cur_thr
->prev_pc
= regcache_read_pc_protected (regcache
);
3206 scoped_disable_commit_resumed
disable_commit_resumed ("proceeding");
3207 bool step_over_started
= start_step_over ();
3209 if (step_over_info_valid_p ())
3211 /* Either this thread started a new in-line step over, or some
3212 other thread was already doing one. In either case, don't
3213 resume anything else until the step-over is finished. */
3215 else if (step_over_started
&& !target_is_non_stop_p ())
3217 /* A new displaced stepping sequence was started. In all-stop,
3218 we can't talk to the target anymore until it next stops. */
3220 else if (!non_stop
&& target_is_non_stop_p ())
3222 INFRUN_SCOPED_DEBUG_START_END
3223 ("resuming threads, all-stop-on-top-of-non-stop");
3225 /* In all-stop, but the target is always in non-stop mode.
3226 Start all other threads that are implicitly resumed too. */
3227 for (thread_info
*tp
: all_non_exited_threads (resume_target
,
3230 switch_to_thread_no_regs (tp
);
3232 if (!tp
->inf
->has_execution ())
3234 infrun_debug_printf ("[%s] target has no execution",
3235 tp
->ptid
.to_string ().c_str ());
3241 infrun_debug_printf ("[%s] resumed",
3242 tp
->ptid
.to_string ().c_str ());
3243 gdb_assert (tp
->executing () || tp
->has_pending_waitstatus ());
3247 if (thread_is_in_step_over_chain (tp
))
3249 infrun_debug_printf ("[%s] needs step-over",
3250 tp
->ptid
.to_string ().c_str ());
3254 infrun_debug_printf ("resuming %s",
3255 tp
->ptid
.to_string ().c_str ());
3257 reset_ecs (ecs
, tp
);
3258 switch_to_thread (tp
);
3259 keep_going_pass_signal (ecs
);
3260 if (!ecs
->wait_some_more
)
3261 error (_("Command aborted."));
3264 else if (!cur_thr
->resumed () && !thread_is_in_step_over_chain (cur_thr
))
3266 /* The thread wasn't started, and isn't queued, run it now. */
3267 reset_ecs (ecs
, cur_thr
);
3268 switch_to_thread (cur_thr
);
3269 keep_going_pass_signal (ecs
);
3270 if (!ecs
->wait_some_more
)
3271 error (_("Command aborted."));
3274 disable_commit_resumed
.reset_and_commit ();
3277 finish_state
.release ();
3279 /* If we've switched threads above, switch back to the previously
3280 current thread. We don't want the user to see a different
3282 switch_to_thread (cur_thr
);
3284 /* Tell the event loop to wait for it to stop. If the target
3285 supports asynchronous execution, it'll do this from within
3287 if (!target_can_async_p ())
3288 mark_async_event_handler (infrun_async_inferior_event_token
);
3292 /* Start remote-debugging of a machine over a serial link. */
3295 start_remote (int from_tty
)
3297 inferior
*inf
= current_inferior ();
3298 inf
->control
.stop_soon
= STOP_QUIETLY_REMOTE
;
3300 /* Always go on waiting for the target, regardless of the mode. */
3301 /* FIXME: cagney/1999-09-23: At present it isn't possible to
3302 indicate to wait_for_inferior that a target should timeout if
3303 nothing is returned (instead of just blocking). Because of this,
3304 targets expecting an immediate response need to, internally, set
3305 things up so that the target_wait() is forced to eventually
3307 /* FIXME: cagney/1999-09-24: It isn't possible for target_open() to
3308 differentiate to its caller what the state of the target is after
3309 the initial open has been performed. Here we're assuming that
3310 the target has stopped. It should be possible to eventually have
3311 target_open() return to the caller an indication that the target
3312 is currently running and GDB state should be set to the same as
3313 for an async run. */
3314 wait_for_inferior (inf
);
3316 /* Now that the inferior has stopped, do any bookkeeping like
3317 loading shared libraries. We want to do this before normal_stop,
3318 so that the displayed frame is up to date. */
3319 post_create_inferior (from_tty
);
3324 /* Initialize static vars when a new inferior begins. */
3327 init_wait_for_inferior (void)
3329 /* These are meaningless until the first time through wait_for_inferior. */
3331 breakpoint_init_inferior (inf_starting
);
3333 clear_proceed_status (0);
3335 nullify_last_target_wait_ptid ();
3337 previous_inferior_ptid
= inferior_ptid
;
3342 static void handle_inferior_event (struct execution_control_state
*ecs
);
3344 static void handle_step_into_function (struct gdbarch
*gdbarch
,
3345 struct execution_control_state
*ecs
);
3346 static void handle_step_into_function_backward (struct gdbarch
*gdbarch
,
3347 struct execution_control_state
*ecs
);
3348 static void handle_signal_stop (struct execution_control_state
*ecs
);
3349 static void check_exception_resume (struct execution_control_state
*,
3350 struct frame_info
*);
3352 static void end_stepping_range (struct execution_control_state
*ecs
);
3353 static void stop_waiting (struct execution_control_state
*ecs
);
3354 static void keep_going (struct execution_control_state
*ecs
);
3355 static void process_event_stop_test (struct execution_control_state
*ecs
);
3356 static bool switch_back_to_stepped_thread (struct execution_control_state
*ecs
);
3358 /* This function is attached as a "thread_stop_requested" observer.
3359 Cleanup local state that assumed the PTID was to be resumed, and
3360 report the stop to the frontend. */
3363 infrun_thread_stop_requested (ptid_t ptid
)
3365 process_stratum_target
*curr_target
= current_inferior ()->process_target ();
3367 /* PTID was requested to stop. If the thread was already stopped,
3368 but the user/frontend doesn't know about that yet (e.g., the
3369 thread had been temporarily paused for some step-over), set up
3370 for reporting the stop now. */
3371 for (thread_info
*tp
: all_threads (curr_target
, ptid
))
3373 if (tp
->state
!= THREAD_RUNNING
)
3375 if (tp
->executing ())
3378 /* Remove matching threads from the step-over queue, so
3379 start_step_over doesn't try to resume them
3381 if (thread_is_in_step_over_chain (tp
))
3382 global_thread_step_over_chain_remove (tp
);
3384 /* If the thread is stopped, but the user/frontend doesn't
3385 know about that yet, queue a pending event, as if the
3386 thread had just stopped now. Unless the thread already had
3388 if (!tp
->has_pending_waitstatus ())
3390 target_waitstatus ws
;
3391 ws
.set_stopped (GDB_SIGNAL_0
);
3392 tp
->set_pending_waitstatus (ws
);
3395 /* Clear the inline-frame state, since we're re-processing the
3397 clear_inline_frame_state (tp
);
3399 /* If this thread was paused because some other thread was
3400 doing an inline-step over, let that finish first. Once
3401 that happens, we'll restart all threads and consume pending
3402 stop events then. */
3403 if (step_over_info_valid_p ())
3406 /* Otherwise we can process the (new) pending event now. Set
3407 it so this pending event is considered by
3409 tp
->set_resumed (true);
3414 infrun_thread_thread_exit (struct thread_info
*tp
, int silent
)
3416 if (target_last_proc_target
== tp
->inf
->process_target ()
3417 && target_last_wait_ptid
== tp
->ptid
)
3418 nullify_last_target_wait_ptid ();
3421 /* Delete the step resume, single-step and longjmp/exception resume
3422 breakpoints of TP. */
3425 delete_thread_infrun_breakpoints (struct thread_info
*tp
)
3427 delete_step_resume_breakpoint (tp
);
3428 delete_exception_resume_breakpoint (tp
);
3429 delete_single_step_breakpoints (tp
);
3432 /* If the target still has execution, call FUNC for each thread that
3433 just stopped. In all-stop, that's all the non-exited threads; in
3434 non-stop, that's the current thread, only. */
3436 typedef void (*for_each_just_stopped_thread_callback_func
)
3437 (struct thread_info
*tp
);
3440 for_each_just_stopped_thread (for_each_just_stopped_thread_callback_func func
)
3442 if (!target_has_execution () || inferior_ptid
== null_ptid
)
3445 if (target_is_non_stop_p ())
3447 /* If in non-stop mode, only the current thread stopped. */
3448 func (inferior_thread ());
3452 /* In all-stop mode, all threads have stopped. */
3453 for (thread_info
*tp
: all_non_exited_threads ())
3458 /* Delete the step resume and longjmp/exception resume breakpoints of
3459 the threads that just stopped. */
3462 delete_just_stopped_threads_infrun_breakpoints (void)
3464 for_each_just_stopped_thread (delete_thread_infrun_breakpoints
);
3467 /* Delete the single-step breakpoints of the threads that just
3471 delete_just_stopped_threads_single_step_breakpoints (void)
3473 for_each_just_stopped_thread (delete_single_step_breakpoints
);
3479 print_target_wait_results (ptid_t waiton_ptid
, ptid_t result_ptid
,
3480 const struct target_waitstatus
&ws
)
3482 infrun_debug_printf ("target_wait (%s [%s], status) =",
3483 waiton_ptid
.to_string ().c_str (),
3484 target_pid_to_str (waiton_ptid
).c_str ());
3485 infrun_debug_printf (" %s [%s],",
3486 result_ptid
.to_string ().c_str (),
3487 target_pid_to_str (result_ptid
).c_str ());
3488 infrun_debug_printf (" %s", ws
.to_string ().c_str ());
3491 /* Select a thread at random, out of those which are resumed and have
3494 static struct thread_info
*
3495 random_pending_event_thread (inferior
*inf
, ptid_t waiton_ptid
)
3497 process_stratum_target
*proc_target
= inf
->process_target ();
3499 = proc_target
->random_resumed_with_pending_wait_status (inf
, waiton_ptid
);
3501 if (thread
== nullptr)
3503 infrun_debug_printf ("None found.");
3507 infrun_debug_printf ("Found %s.", thread
->ptid
.to_string ().c_str ());
3508 gdb_assert (thread
->resumed ());
3509 gdb_assert (thread
->has_pending_waitstatus ());
3514 /* Wrapper for target_wait that first checks whether threads have
3515 pending statuses to report before actually asking the target for
3516 more events. INF is the inferior we're using to call target_wait
3520 do_target_wait_1 (inferior
*inf
, ptid_t ptid
,
3521 target_waitstatus
*status
, target_wait_flags options
)
3524 struct thread_info
*tp
;
3526 /* We know that we are looking for an event in the target of inferior
3527 INF, but we don't know which thread the event might come from. As
3528 such we want to make sure that INFERIOR_PTID is reset so that none of
3529 the wait code relies on it - doing so is always a mistake. */
3530 switch_to_inferior_no_thread (inf
);
3532 /* First check if there is a resumed thread with a wait status
3534 if (ptid
== minus_one_ptid
|| ptid
.is_pid ())
3536 tp
= random_pending_event_thread (inf
, ptid
);
3540 infrun_debug_printf ("Waiting for specific thread %s.",
3541 ptid
.to_string ().c_str ());
3543 /* We have a specific thread to check. */
3544 tp
= find_thread_ptid (inf
, ptid
);
3545 gdb_assert (tp
!= NULL
);
3546 if (!tp
->has_pending_waitstatus ())
3551 && (tp
->stop_reason () == TARGET_STOPPED_BY_SW_BREAKPOINT
3552 || tp
->stop_reason () == TARGET_STOPPED_BY_HW_BREAKPOINT
))
3554 struct regcache
*regcache
= get_thread_regcache (tp
);
3555 struct gdbarch
*gdbarch
= regcache
->arch ();
3559 pc
= regcache_read_pc (regcache
);
3561 if (pc
!= tp
->stop_pc ())
3563 infrun_debug_printf ("PC of %s changed. was=%s, now=%s",
3564 tp
->ptid
.to_string ().c_str (),
3565 paddress (gdbarch
, tp
->stop_pc ()),
3566 paddress (gdbarch
, pc
));
3569 else if (!breakpoint_inserted_here_p (regcache
->aspace (), pc
))
3571 infrun_debug_printf ("previous breakpoint of %s, at %s gone",
3572 tp
->ptid
.to_string ().c_str (),
3573 paddress (gdbarch
, pc
));
3580 infrun_debug_printf ("pending event of %s cancelled.",
3581 tp
->ptid
.to_string ().c_str ());
3583 tp
->clear_pending_waitstatus ();
3584 target_waitstatus ws
;
3586 tp
->set_pending_waitstatus (ws
);
3587 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
3593 infrun_debug_printf ("Using pending wait status %s for %s.",
3594 tp
->pending_waitstatus ().to_string ().c_str (),
3595 tp
->ptid
.to_string ().c_str ());
3597 /* Now that we've selected our final event LWP, un-adjust its PC
3598 if it was a software breakpoint (and the target doesn't
3599 always adjust the PC itself). */
3600 if (tp
->stop_reason () == TARGET_STOPPED_BY_SW_BREAKPOINT
3601 && !target_supports_stopped_by_sw_breakpoint ())
3603 struct regcache
*regcache
;
3604 struct gdbarch
*gdbarch
;
3607 regcache
= get_thread_regcache (tp
);
3608 gdbarch
= regcache
->arch ();
3610 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
3615 pc
= regcache_read_pc (regcache
);
3616 regcache_write_pc (regcache
, pc
+ decr_pc
);
3620 tp
->set_stop_reason (TARGET_STOPPED_BY_NO_REASON
);
3621 *status
= tp
->pending_waitstatus ();
3622 tp
->clear_pending_waitstatus ();
3624 /* Wake up the event loop again, until all pending events are
3626 if (target_is_async_p ())
3627 mark_async_event_handler (infrun_async_inferior_event_token
);
3631 /* But if we don't find one, we'll have to wait. */
3633 /* We can't ask a non-async target to do a non-blocking wait, so this will be
3635 if (!target_can_async_p ())
3636 options
&= ~TARGET_WNOHANG
;
3638 if (deprecated_target_wait_hook
)
3639 event_ptid
= deprecated_target_wait_hook (ptid
, status
, options
);
3641 event_ptid
= target_wait (ptid
, status
, options
);
3646 /* Wrapper for target_wait that first checks whether threads have
3647 pending statuses to report before actually asking the target for
3648 more events. Polls for events from all inferiors/targets. */
3651 do_target_wait (execution_control_state
*ecs
, target_wait_flags options
)
3653 int num_inferiors
= 0;
3654 int random_selector
;
3656 /* For fairness, we pick the first inferior/target to poll at random
3657 out of all inferiors that may report events, and then continue
3658 polling the rest of the inferior list starting from that one in a
3659 circular fashion until the whole list is polled once. */
3661 auto inferior_matches
= [] (inferior
*inf
)
3663 return inf
->process_target () != nullptr;
3666 /* First see how many matching inferiors we have. */
3667 for (inferior
*inf
: all_inferiors ())
3668 if (inferior_matches (inf
))
3671 if (num_inferiors
== 0)
3673 ecs
->ws
.set_ignore ();
3677 /* Now randomly pick an inferior out of those that matched. */
3678 random_selector
= (int)
3679 ((num_inferiors
* (double) rand ()) / (RAND_MAX
+ 1.0));
3681 if (num_inferiors
> 1)
3682 infrun_debug_printf ("Found %d inferiors, starting at #%d",
3683 num_inferiors
, random_selector
);
3685 /* Select the Nth inferior that matched. */
3687 inferior
*selected
= nullptr;
3689 for (inferior
*inf
: all_inferiors ())
3690 if (inferior_matches (inf
))
3691 if (random_selector
-- == 0)
3697 /* Now poll for events out of each of the matching inferior's
3698 targets, starting from the selected one. */
3700 auto do_wait
= [&] (inferior
*inf
)
3702 ecs
->ptid
= do_target_wait_1 (inf
, minus_one_ptid
, &ecs
->ws
, options
);
3703 ecs
->target
= inf
->process_target ();
3704 return (ecs
->ws
.kind () != TARGET_WAITKIND_IGNORE
);
3707 /* Needed in 'all-stop + target-non-stop' mode, because we end up
3708 here spuriously after the target is all stopped and we've already
3709 reported the stop to the user, polling for events. */
3710 scoped_restore_current_thread restore_thread
;
3712 intrusive_list_iterator
<inferior
> start
3713 = inferior_list
.iterator_to (*selected
);
3715 for (intrusive_list_iterator
<inferior
> it
= start
;
3716 it
!= inferior_list
.end ();
3719 inferior
*inf
= &*it
;
3721 if (inferior_matches (inf
) && do_wait (inf
))
3725 for (intrusive_list_iterator
<inferior
> it
= inferior_list
.begin ();
3729 inferior
*inf
= &*it
;
3731 if (inferior_matches (inf
) && do_wait (inf
))
3735 ecs
->ws
.set_ignore ();
3739 /* An event reported by wait_one. */
3741 struct wait_one_event
3743 /* The target the event came out of. */
3744 process_stratum_target
*target
;
3746 /* The PTID the event was for. */
3749 /* The waitstatus. */
3750 target_waitstatus ws
;
3753 static bool handle_one (const wait_one_event
&event
);
3754 static void restart_threads (struct thread_info
*event_thread
);
3756 /* Prepare and stabilize the inferior for detaching it. E.g.,
3757 detaching while a thread is displaced stepping is a recipe for
3758 crashing it, as nothing would readjust the PC out of the scratch
3762 prepare_for_detach (void)
3764 struct inferior
*inf
= current_inferior ();
3765 ptid_t pid_ptid
= ptid_t (inf
->pid
);
3766 scoped_restore_current_thread restore_thread
;
3768 scoped_restore restore_detaching
= make_scoped_restore (&inf
->detaching
, true);
3770 /* Remove all threads of INF from the global step-over chain. We
3771 want to stop any ongoing step-over, not start any new one. */
3772 thread_step_over_list_safe_range range
3773 = make_thread_step_over_list_safe_range (global_thread_step_over_list
);
3775 for (thread_info
*tp
: range
)
3778 infrun_debug_printf ("removing thread %s from global step over chain",
3779 tp
->ptid
.to_string ().c_str ());
3780 global_thread_step_over_chain_remove (tp
);
3783 /* If we were already in the middle of an inline step-over, and the
3784 thread stepping belongs to the inferior we're detaching, we need
3785 to restart the threads of other inferiors. */
3786 if (step_over_info
.thread
!= -1)
3788 infrun_debug_printf ("inline step-over in-process while detaching");
3790 thread_info
*thr
= find_thread_global_id (step_over_info
.thread
);
3791 if (thr
->inf
== inf
)
3793 /* Since we removed threads of INF from the step-over chain,
3794 we know this won't start a step-over for INF. */
3795 clear_step_over_info ();
3797 if (target_is_non_stop_p ())
3799 /* Start a new step-over in another thread if there's
3800 one that needs it. */
3803 /* Restart all other threads (except the
3804 previously-stepping thread, since that one is still
3806 if (!step_over_info_valid_p ())
3807 restart_threads (thr
);
3812 if (displaced_step_in_progress (inf
))
3814 infrun_debug_printf ("displaced-stepping in-process while detaching");
3816 /* Stop threads currently displaced stepping, aborting it. */
3818 for (thread_info
*thr
: inf
->non_exited_threads ())
3820 if (thr
->displaced_step_state
.in_progress ())
3822 if (thr
->executing ())
3824 if (!thr
->stop_requested
)
3826 target_stop (thr
->ptid
);
3827 thr
->stop_requested
= true;
3831 thr
->set_resumed (false);
3835 while (displaced_step_in_progress (inf
))
3837 wait_one_event event
;
3839 event
.target
= inf
->process_target ();
3840 event
.ptid
= do_target_wait_1 (inf
, pid_ptid
, &event
.ws
, 0);
3843 print_target_wait_results (pid_ptid
, event
.ptid
, event
.ws
);
3848 /* It's OK to leave some of the threads of INF stopped, since
3849 they'll be detached shortly. */
3853 /* Wait for control to return from inferior to debugger.
3855 If inferior gets a signal, we may decide to start it up again
3856 instead of returning. That is why there is a loop in this function.
3857 When this function actually returns it means the inferior
3858 should be left stopped and GDB should read more commands. */
3861 wait_for_inferior (inferior
*inf
)
3863 infrun_debug_printf ("wait_for_inferior ()");
3865 SCOPE_EXIT
{ delete_just_stopped_threads_infrun_breakpoints (); };
3867 /* If an error happens while handling the event, propagate GDB's
3868 knowledge of the executing state to the frontend/user running
3870 scoped_finish_thread_state finish_state
3871 (inf
->process_target (), minus_one_ptid
);
3875 struct execution_control_state ecss
;
3876 struct execution_control_state
*ecs
= &ecss
;
3878 overlay_cache_invalid
= 1;
3880 /* Flush target cache before starting to handle each event.
3881 Target was running and cache could be stale. This is just a
3882 heuristic. Running threads may modify target memory, but we
3883 don't get any event. */
3884 target_dcache_invalidate ();
3886 ecs
->ptid
= do_target_wait_1 (inf
, minus_one_ptid
, &ecs
->ws
, 0);
3887 ecs
->target
= inf
->process_target ();
3890 print_target_wait_results (minus_one_ptid
, ecs
->ptid
, ecs
->ws
);
3892 /* Now figure out what to do with the result of the result. */
3893 handle_inferior_event (ecs
);
3895 if (!ecs
->wait_some_more
)
3899 /* No error, don't finish the state yet. */
3900 finish_state
.release ();
3903 /* Cleanup that reinstalls the readline callback handler, if the
3904 target is running in the background. If while handling the target
3905 event something triggered a secondary prompt, like e.g., a
3906 pagination prompt, we'll have removed the callback handler (see
3907 gdb_readline_wrapper_line). Need to do this as we go back to the
3908 event loop, ready to process further input. Note this has no
3909 effect if the handler hasn't actually been removed, because calling
3910 rl_callback_handler_install resets the line buffer, thus losing
3914 reinstall_readline_callback_handler_cleanup ()
3916 struct ui
*ui
= current_ui
;
3920 /* We're not going back to the top level event loop yet. Don't
3921 install the readline callback, as it'd prep the terminal,
3922 readline-style (raw, noecho) (e.g., --batch). We'll install
3923 it the next time the prompt is displayed, when we're ready
3928 if (ui
->command_editing
&& ui
->prompt_state
!= PROMPT_BLOCKED
)
3929 gdb_rl_callback_handler_reinstall ();
3932 /* Clean up the FSMs of threads that are now stopped. In non-stop,
3933 that's just the event thread. In all-stop, that's all threads. */
3936 clean_up_just_stopped_threads_fsms (struct execution_control_state
*ecs
)
3938 if (ecs
->event_thread
!= NULL
3939 && ecs
->event_thread
->thread_fsm
!= NULL
)
3940 ecs
->event_thread
->thread_fsm
->clean_up (ecs
->event_thread
);
3944 for (thread_info
*thr
: all_non_exited_threads ())
3946 if (thr
->thread_fsm
== NULL
)
3948 if (thr
== ecs
->event_thread
)
3951 switch_to_thread (thr
);
3952 thr
->thread_fsm
->clean_up (thr
);
3955 if (ecs
->event_thread
!= NULL
)
3956 switch_to_thread (ecs
->event_thread
);
3960 /* Helper for all_uis_check_sync_execution_done that works on the
3964 check_curr_ui_sync_execution_done (void)
3966 struct ui
*ui
= current_ui
;
3968 if (ui
->prompt_state
== PROMPT_NEEDED
3970 && !gdb_in_secondary_prompt_p (ui
))
3972 target_terminal::ours ();
3973 gdb::observers::sync_execution_done
.notify ();
3974 ui_register_input_event_handler (ui
);
3981 all_uis_check_sync_execution_done (void)
3983 SWITCH_THRU_ALL_UIS ()
3985 check_curr_ui_sync_execution_done ();
3992 all_uis_on_sync_execution_starting (void)
3994 SWITCH_THRU_ALL_UIS ()
3996 if (current_ui
->prompt_state
== PROMPT_NEEDED
)
3997 async_disable_stdin ();
4001 /* Asynchronous version of wait_for_inferior. It is called by the
4002 event loop whenever a change of state is detected on the file
4003 descriptor corresponding to the target. It can be called more than
4004 once to complete a single execution command. In such cases we need
4005 to keep the state in a global variable ECSS. If it is the last time
4006 that this function is called for a single execution command, then
4007 report to the user that the inferior has stopped, and do the
4008 necessary cleanups. */
4011 fetch_inferior_event ()
4013 INFRUN_SCOPED_DEBUG_ENTER_EXIT
;
4015 struct execution_control_state ecss
;
4016 struct execution_control_state
*ecs
= &ecss
;
4019 /* Events are always processed with the main UI as current UI. This
4020 way, warnings, debug output, etc. are always consistently sent to
4021 the main console. */
4022 scoped_restore save_ui
= make_scoped_restore (¤t_ui
, main_ui
);
4024 /* Temporarily disable pagination. Otherwise, the user would be
4025 given an option to press 'q' to quit, which would cause an early
4026 exit and could leave GDB in a half-baked state. */
4027 scoped_restore save_pagination
4028 = make_scoped_restore (&pagination_enabled
, false);
4030 /* End up with readline processing input, if necessary. */
4032 SCOPE_EXIT
{ reinstall_readline_callback_handler_cleanup (); };
4034 /* We're handling a live event, so make sure we're doing live
4035 debugging. If we're looking at traceframes while the target is
4036 running, we're going to need to get back to that mode after
4037 handling the event. */
4038 gdb::optional
<scoped_restore_current_traceframe
> maybe_restore_traceframe
;
4041 maybe_restore_traceframe
.emplace ();
4042 set_current_traceframe (-1);
4045 /* The user/frontend should not notice a thread switch due to
4046 internal events. Make sure we revert to the user selected
4047 thread and frame after handling the event and running any
4048 breakpoint commands. */
4049 scoped_restore_current_thread restore_thread
;
4051 overlay_cache_invalid
= 1;
4052 /* Flush target cache before starting to handle each event. Target
4053 was running and cache could be stale. This is just a heuristic.
4054 Running threads may modify target memory, but we don't get any
4056 target_dcache_invalidate ();
4058 scoped_restore save_exec_dir
4059 = make_scoped_restore (&execution_direction
,
4060 target_execution_direction ());
4062 /* Allow targets to pause their resumed threads while we handle
4064 scoped_disable_commit_resumed
disable_commit_resumed ("handling event");
4066 if (!do_target_wait (ecs
, TARGET_WNOHANG
))
4068 infrun_debug_printf ("do_target_wait returned no event");
4069 disable_commit_resumed
.reset_and_commit ();
4073 gdb_assert (ecs
->ws
.kind () != TARGET_WAITKIND_IGNORE
);
4075 /* Switch to the target that generated the event, so we can do
4077 switch_to_target_no_thread (ecs
->target
);
4080 print_target_wait_results (minus_one_ptid
, ecs
->ptid
, ecs
->ws
);
4082 /* If an error happens while handling the event, propagate GDB's
4083 knowledge of the executing state to the frontend/user running
4085 ptid_t finish_ptid
= !target_is_non_stop_p () ? minus_one_ptid
: ecs
->ptid
;
4086 scoped_finish_thread_state
finish_state (ecs
->target
, finish_ptid
);
4088 /* Get executed before scoped_restore_current_thread above to apply
4089 still for the thread which has thrown the exception. */
4090 auto defer_bpstat_clear
4091 = make_scope_exit (bpstat_clear_actions
);
4092 auto defer_delete_threads
4093 = make_scope_exit (delete_just_stopped_threads_infrun_breakpoints
);
4095 /* Now figure out what to do with the result of the result. */
4096 handle_inferior_event (ecs
);
4098 if (!ecs
->wait_some_more
)
4100 struct inferior
*inf
= find_inferior_ptid (ecs
->target
, ecs
->ptid
);
4101 bool should_stop
= true;
4102 struct thread_info
*thr
= ecs
->event_thread
;
4104 delete_just_stopped_threads_infrun_breakpoints ();
4108 struct thread_fsm
*thread_fsm
= thr
->thread_fsm
;
4110 if (thread_fsm
!= NULL
)
4111 should_stop
= thread_fsm
->should_stop (thr
);
4120 bool should_notify_stop
= true;
4123 clean_up_just_stopped_threads_fsms (ecs
);
4125 if (thr
!= NULL
&& thr
->thread_fsm
!= NULL
)
4126 should_notify_stop
= thr
->thread_fsm
->should_notify_stop ();
4128 if (should_notify_stop
)
4130 /* We may not find an inferior if this was a process exit. */
4131 if (inf
== NULL
|| inf
->control
.stop_soon
== NO_STOP_QUIETLY
)
4132 proceeded
= normal_stop ();
4137 inferior_event_handler (INF_EXEC_COMPLETE
);
4141 /* If we got a TARGET_WAITKIND_NO_RESUMED event, then the
4142 previously selected thread is gone. We have two
4143 choices - switch to no thread selected, or restore the
4144 previously selected thread (now exited). We chose the
4145 later, just because that's what GDB used to do. After
4146 this, "info threads" says "The current thread <Thread
4147 ID 2> has terminated." instead of "No thread
4151 && ecs
->ws
.kind () != TARGET_WAITKIND_NO_RESUMED
)
4152 restore_thread
.dont_restore ();
4156 defer_delete_threads
.release ();
4157 defer_bpstat_clear
.release ();
4159 /* No error, don't finish the thread states yet. */
4160 finish_state
.release ();
4162 disable_commit_resumed
.reset_and_commit ();
4164 /* This scope is used to ensure that readline callbacks are
4165 reinstalled here. */
4168 /* If a UI was in sync execution mode, and now isn't, restore its
4169 prompt (a synchronous execution command has finished, and we're
4170 ready for input). */
4171 all_uis_check_sync_execution_done ();
4174 && exec_done_display_p
4175 && (inferior_ptid
== null_ptid
4176 || inferior_thread ()->state
!= THREAD_RUNNING
))
4177 printf_unfiltered (_("completed.\n"));
4183 set_step_info (thread_info
*tp
, struct frame_info
*frame
,
4184 struct symtab_and_line sal
)
4186 /* This can be removed once this function no longer implicitly relies on the
4187 inferior_ptid value. */
4188 gdb_assert (inferior_ptid
== tp
->ptid
);
4190 tp
->control
.step_frame_id
= get_frame_id (frame
);
4191 tp
->control
.step_stack_frame_id
= get_stack_frame_id (frame
);
4193 tp
->current_symtab
= sal
.symtab
;
4194 tp
->current_line
= sal
.line
;
4197 /* Clear context switchable stepping state. */
4200 init_thread_stepping_state (struct thread_info
*tss
)
4202 tss
->stepped_breakpoint
= 0;
4203 tss
->stepping_over_breakpoint
= 0;
4204 tss
->stepping_over_watchpoint
= 0;
4205 tss
->step_after_step_resume_breakpoint
= 0;
4211 set_last_target_status (process_stratum_target
*target
, ptid_t ptid
,
4212 const target_waitstatus
&status
)
4214 target_last_proc_target
= target
;
4215 target_last_wait_ptid
= ptid
;
4216 target_last_waitstatus
= status
;
4222 get_last_target_status (process_stratum_target
**target
, ptid_t
*ptid
,
4223 target_waitstatus
*status
)
4225 if (target
!= nullptr)
4226 *target
= target_last_proc_target
;
4227 if (ptid
!= nullptr)
4228 *ptid
= target_last_wait_ptid
;
4229 if (status
!= nullptr)
4230 *status
= target_last_waitstatus
;
4236 nullify_last_target_wait_ptid (void)
4238 target_last_proc_target
= nullptr;
4239 target_last_wait_ptid
= minus_one_ptid
;
4240 target_last_waitstatus
= {};
4243 /* Switch thread contexts. */
4246 context_switch (execution_control_state
*ecs
)
4248 if (ecs
->ptid
!= inferior_ptid
4249 && (inferior_ptid
== null_ptid
4250 || ecs
->event_thread
!= inferior_thread ()))
4252 infrun_debug_printf ("Switching context from %s to %s",
4253 inferior_ptid
.to_string ().c_str (),
4254 ecs
->ptid
.to_string ().c_str ());
4257 switch_to_thread (ecs
->event_thread
);
4260 /* If the target can't tell whether we've hit breakpoints
4261 (target_supports_stopped_by_sw_breakpoint), and we got a SIGTRAP,
4262 check whether that could have been caused by a breakpoint. If so,
4263 adjust the PC, per gdbarch_decr_pc_after_break. */
4266 adjust_pc_after_break (struct thread_info
*thread
,
4267 const target_waitstatus
&ws
)
4269 struct regcache
*regcache
;
4270 struct gdbarch
*gdbarch
;
4271 CORE_ADDR breakpoint_pc
, decr_pc
;
4273 /* If we've hit a breakpoint, we'll normally be stopped with SIGTRAP. If
4274 we aren't, just return.
4276 We assume that waitkinds other than TARGET_WAITKIND_STOPPED are not
4277 affected by gdbarch_decr_pc_after_break. Other waitkinds which are
4278 implemented by software breakpoints should be handled through the normal
4281 NOTE drow/2004-01-31: On some targets, breakpoints may generate
4282 different signals (SIGILL or SIGEMT for instance), but it is less
4283 clear where the PC is pointing afterwards. It may not match
4284 gdbarch_decr_pc_after_break. I don't know any specific target that
4285 generates these signals at breakpoints (the code has been in GDB since at
4286 least 1992) so I can not guess how to handle them here.
4288 In earlier versions of GDB, a target with
4289 gdbarch_have_nonsteppable_watchpoint would have the PC after hitting a
4290 watchpoint affected by gdbarch_decr_pc_after_break. I haven't found any
4291 target with both of these set in GDB history, and it seems unlikely to be
4292 correct, so gdbarch_have_nonsteppable_watchpoint is not checked here. */
4294 if (ws
.kind () != TARGET_WAITKIND_STOPPED
)
4297 if (ws
.sig () != GDB_SIGNAL_TRAP
)
4300 /* In reverse execution, when a breakpoint is hit, the instruction
4301 under it has already been de-executed. The reported PC always
4302 points at the breakpoint address, so adjusting it further would
4303 be wrong. E.g., consider this case on a decr_pc_after_break == 1
4306 B1 0x08000000 : INSN1
4307 B2 0x08000001 : INSN2
4309 PC -> 0x08000003 : INSN4
4311 Say you're stopped at 0x08000003 as above. Reverse continuing
4312 from that point should hit B2 as below. Reading the PC when the
4313 SIGTRAP is reported should read 0x08000001 and INSN2 should have
4314 been de-executed already.
4316 B1 0x08000000 : INSN1
4317 B2 PC -> 0x08000001 : INSN2
4321 We can't apply the same logic as for forward execution, because
4322 we would wrongly adjust the PC to 0x08000000, since there's a
4323 breakpoint at PC - 1. We'd then report a hit on B1, although
4324 INSN1 hadn't been de-executed yet. Doing nothing is the correct
4326 if (execution_direction
== EXEC_REVERSE
)
4329 /* If the target can tell whether the thread hit a SW breakpoint,
4330 trust it. Targets that can tell also adjust the PC
4332 if (target_supports_stopped_by_sw_breakpoint ())
4335 /* Note that relying on whether a breakpoint is planted in memory to
4336 determine this can fail. E.g,. the breakpoint could have been
4337 removed since. Or the thread could have been told to step an
4338 instruction the size of a breakpoint instruction, and only
4339 _after_ was a breakpoint inserted at its address. */
4341 /* If this target does not decrement the PC after breakpoints, then
4342 we have nothing to do. */
4343 regcache
= get_thread_regcache (thread
);
4344 gdbarch
= regcache
->arch ();
4346 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
4350 const address_space
*aspace
= regcache
->aspace ();
4352 /* Find the location where (if we've hit a breakpoint) the
4353 breakpoint would be. */
4354 breakpoint_pc
= regcache_read_pc (regcache
) - decr_pc
;
4356 /* If the target can't tell whether a software breakpoint triggered,
4357 fallback to figuring it out based on breakpoints we think were
4358 inserted in the target, and on whether the thread was stepped or
4361 /* Check whether there actually is a software breakpoint inserted at
4364 If in non-stop mode, a race condition is possible where we've
4365 removed a breakpoint, but stop events for that breakpoint were
4366 already queued and arrive later. To suppress those spurious
4367 SIGTRAPs, we keep a list of such breakpoint locations for a bit,
4368 and retire them after a number of stop events are reported. Note
4369 this is an heuristic and can thus get confused. The real fix is
4370 to get the "stopped by SW BP and needs adjustment" info out of
4371 the target/kernel (and thus never reach here; see above). */
4372 if (software_breakpoint_inserted_here_p (aspace
, breakpoint_pc
)
4373 || (target_is_non_stop_p ()
4374 && moribund_breakpoint_here_p (aspace
, breakpoint_pc
)))
4376 gdb::optional
<scoped_restore_tmpl
<int>> restore_operation_disable
;
4378 if (record_full_is_used ())
4379 restore_operation_disable
.emplace
4380 (record_full_gdb_operation_disable_set ());
4382 /* When using hardware single-step, a SIGTRAP is reported for both
4383 a completed single-step and a software breakpoint. Need to
4384 differentiate between the two, as the latter needs adjusting
4385 but the former does not.
4387 The SIGTRAP can be due to a completed hardware single-step only if
4388 - we didn't insert software single-step breakpoints
4389 - this thread is currently being stepped
4391 If any of these events did not occur, we must have stopped due
4392 to hitting a software breakpoint, and have to back up to the
4395 As a special case, we could have hardware single-stepped a
4396 software breakpoint. In this case (prev_pc == breakpoint_pc),
4397 we also need to back up to the breakpoint address. */
4399 if (thread_has_single_step_breakpoints_set (thread
)
4400 || !currently_stepping (thread
)
4401 || (thread
->stepped_breakpoint
4402 && thread
->prev_pc
== breakpoint_pc
))
4403 regcache_write_pc (regcache
, breakpoint_pc
);
4408 stepped_in_from (struct frame_info
*frame
, struct frame_id step_frame_id
)
4410 for (frame
= get_prev_frame (frame
);
4412 frame
= get_prev_frame (frame
))
4414 if (frame_id_eq (get_frame_id (frame
), step_frame_id
))
4417 if (get_frame_type (frame
) != INLINE_FRAME
)
4424 /* Look for an inline frame that is marked for skip.
4425 If PREV_FRAME is TRUE start at the previous frame,
4426 otherwise start at the current frame. Stop at the
4427 first non-inline frame, or at the frame where the
4431 inline_frame_is_marked_for_skip (bool prev_frame
, struct thread_info
*tp
)
4433 struct frame_info
*frame
= get_current_frame ();
4436 frame
= get_prev_frame (frame
);
4438 for (; frame
!= NULL
; frame
= get_prev_frame (frame
))
4440 const char *fn
= NULL
;
4441 symtab_and_line sal
;
4444 if (frame_id_eq (get_frame_id (frame
), tp
->control
.step_frame_id
))
4446 if (get_frame_type (frame
) != INLINE_FRAME
)
4449 sal
= find_frame_sal (frame
);
4450 sym
= get_frame_function (frame
);
4453 fn
= sym
->print_name ();
4456 && function_name_is_marked_for_skip (fn
, sal
))
4463 /* If the event thread has the stop requested flag set, pretend it
4464 stopped for a GDB_SIGNAL_0 (i.e., as if it stopped due to
4468 handle_stop_requested (struct execution_control_state
*ecs
)
4470 if (ecs
->event_thread
->stop_requested
)
4472 ecs
->ws
.set_stopped (GDB_SIGNAL_0
);
4473 handle_signal_stop (ecs
);
4479 /* Auxiliary function that handles syscall entry/return events.
4480 It returns true if the inferior should keep going (and GDB
4481 should ignore the event), or false if the event deserves to be
4485 handle_syscall_event (struct execution_control_state
*ecs
)
4487 struct regcache
*regcache
;
4490 context_switch (ecs
);
4492 regcache
= get_thread_regcache (ecs
->event_thread
);
4493 syscall_number
= ecs
->ws
.syscall_number ();
4494 ecs
->event_thread
->set_stop_pc (regcache_read_pc (regcache
));
4496 if (catch_syscall_enabled () > 0
4497 && catching_syscall_number (syscall_number
))
4499 infrun_debug_printf ("syscall number=%d", syscall_number
);
4501 ecs
->event_thread
->control
.stop_bpstat
4502 = bpstat_stop_status (regcache
->aspace (),
4503 ecs
->event_thread
->stop_pc (),
4504 ecs
->event_thread
, ecs
->ws
);
4506 if (handle_stop_requested (ecs
))
4509 if (bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
4511 /* Catchpoint hit. */
4516 if (handle_stop_requested (ecs
))
4519 /* If no catchpoint triggered for this, then keep going. */
4525 /* Lazily fill in the execution_control_state's stop_func_* fields. */
4528 fill_in_stop_func (struct gdbarch
*gdbarch
,
4529 struct execution_control_state
*ecs
)
4531 if (!ecs
->stop_func_filled_in
)
4534 const general_symbol_info
*gsi
;
4536 /* Don't care about return value; stop_func_start and stop_func_name
4537 will both be 0 if it doesn't work. */
4538 find_pc_partial_function_sym (ecs
->event_thread
->stop_pc (),
4540 &ecs
->stop_func_start
,
4541 &ecs
->stop_func_end
,
4543 ecs
->stop_func_name
= gsi
== nullptr ? nullptr : gsi
->print_name ();
4545 /* The call to find_pc_partial_function, above, will set
4546 stop_func_start and stop_func_end to the start and end
4547 of the range containing the stop pc. If this range
4548 contains the entry pc for the block (which is always the
4549 case for contiguous blocks), advance stop_func_start past
4550 the function's start offset and entrypoint. Note that
4551 stop_func_start is NOT advanced when in a range of a
4552 non-contiguous block that does not contain the entry pc. */
4553 if (block
!= nullptr
4554 && ecs
->stop_func_start
<= BLOCK_ENTRY_PC (block
)
4555 && BLOCK_ENTRY_PC (block
) < ecs
->stop_func_end
)
4557 ecs
->stop_func_start
4558 += gdbarch_deprecated_function_start_offset (gdbarch
);
4560 if (gdbarch_skip_entrypoint_p (gdbarch
))
4561 ecs
->stop_func_start
4562 = gdbarch_skip_entrypoint (gdbarch
, ecs
->stop_func_start
);
4565 ecs
->stop_func_filled_in
= 1;
4570 /* Return the STOP_SOON field of the inferior pointed at by ECS. */
4572 static enum stop_kind
4573 get_inferior_stop_soon (execution_control_state
*ecs
)
4575 struct inferior
*inf
= find_inferior_ptid (ecs
->target
, ecs
->ptid
);
4577 gdb_assert (inf
!= NULL
);
4578 return inf
->control
.stop_soon
;
4581 /* Poll for one event out of the current target. Store the resulting
4582 waitstatus in WS, and return the event ptid. Does not block. */
4585 poll_one_curr_target (struct target_waitstatus
*ws
)
4589 overlay_cache_invalid
= 1;
4591 /* Flush target cache before starting to handle each event.
4592 Target was running and cache could be stale. This is just a
4593 heuristic. Running threads may modify target memory, but we
4594 don't get any event. */
4595 target_dcache_invalidate ();
4597 if (deprecated_target_wait_hook
)
4598 event_ptid
= deprecated_target_wait_hook (minus_one_ptid
, ws
, TARGET_WNOHANG
);
4600 event_ptid
= target_wait (minus_one_ptid
, ws
, TARGET_WNOHANG
);
4603 print_target_wait_results (minus_one_ptid
, event_ptid
, *ws
);
4608 /* Wait for one event out of any target. */
4610 static wait_one_event
4615 for (inferior
*inf
: all_inferiors ())
4617 process_stratum_target
*target
= inf
->process_target ();
4619 || !target
->is_async_p ()
4620 || !target
->threads_executing
)
4623 switch_to_inferior_no_thread (inf
);
4625 wait_one_event event
;
4626 event
.target
= target
;
4627 event
.ptid
= poll_one_curr_target (&event
.ws
);
4629 if (event
.ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
4631 /* If nothing is resumed, remove the target from the
4635 else if (event
.ws
.kind () != TARGET_WAITKIND_IGNORE
)
4639 /* Block waiting for some event. */
4646 for (inferior
*inf
: all_inferiors ())
4648 process_stratum_target
*target
= inf
->process_target ();
4650 || !target
->is_async_p ()
4651 || !target
->threads_executing
)
4654 int fd
= target
->async_wait_fd ();
4655 FD_SET (fd
, &readfds
);
4662 /* No waitable targets left. All must be stopped. */
4663 target_waitstatus ws
;
4664 ws
.set_no_resumed ();
4665 return {NULL
, minus_one_ptid
, std::move (ws
)};
4670 int numfds
= interruptible_select (nfds
, &readfds
, 0, NULL
, 0);
4676 perror_with_name ("interruptible_select");
4681 /* Save the thread's event and stop reason to process it later. */
4684 save_waitstatus (struct thread_info
*tp
, const target_waitstatus
&ws
)
4686 infrun_debug_printf ("saving status %s for %s",
4687 ws
.to_string ().c_str (),
4688 tp
->ptid
.to_string ().c_str ());
4690 /* Record for later. */
4691 tp
->set_pending_waitstatus (ws
);
4693 if (ws
.kind () == TARGET_WAITKIND_STOPPED
4694 && ws
.sig () == GDB_SIGNAL_TRAP
)
4696 struct regcache
*regcache
= get_thread_regcache (tp
);
4697 const address_space
*aspace
= regcache
->aspace ();
4698 CORE_ADDR pc
= regcache_read_pc (regcache
);
4700 adjust_pc_after_break (tp
, tp
->pending_waitstatus ());
4702 scoped_restore_current_thread restore_thread
;
4703 switch_to_thread (tp
);
4705 if (target_stopped_by_watchpoint ())
4706 tp
->set_stop_reason (TARGET_STOPPED_BY_WATCHPOINT
);
4707 else if (target_supports_stopped_by_sw_breakpoint ()
4708 && target_stopped_by_sw_breakpoint ())
4709 tp
->set_stop_reason (TARGET_STOPPED_BY_SW_BREAKPOINT
);
4710 else if (target_supports_stopped_by_hw_breakpoint ()
4711 && target_stopped_by_hw_breakpoint ())
4712 tp
->set_stop_reason (TARGET_STOPPED_BY_HW_BREAKPOINT
);
4713 else if (!target_supports_stopped_by_hw_breakpoint ()
4714 && hardware_breakpoint_inserted_here_p (aspace
, pc
))
4715 tp
->set_stop_reason (TARGET_STOPPED_BY_HW_BREAKPOINT
);
4716 else if (!target_supports_stopped_by_sw_breakpoint ()
4717 && software_breakpoint_inserted_here_p (aspace
, pc
))
4718 tp
->set_stop_reason (TARGET_STOPPED_BY_SW_BREAKPOINT
);
4719 else if (!thread_has_single_step_breakpoints_set (tp
)
4720 && currently_stepping (tp
))
4721 tp
->set_stop_reason (TARGET_STOPPED_BY_SINGLE_STEP
);
4725 /* Mark the non-executing threads accordingly. In all-stop, all
4726 threads of all processes are stopped when we get any event
4727 reported. In non-stop mode, only the event thread stops. */
4730 mark_non_executing_threads (process_stratum_target
*target
,
4732 const target_waitstatus
&ws
)
4736 if (!target_is_non_stop_p ())
4737 mark_ptid
= minus_one_ptid
;
4738 else if (ws
.kind () == TARGET_WAITKIND_SIGNALLED
4739 || ws
.kind () == TARGET_WAITKIND_EXITED
)
4741 /* If we're handling a process exit in non-stop mode, even
4742 though threads haven't been deleted yet, one would think
4743 that there is nothing to do, as threads of the dead process
4744 will be soon deleted, and threads of any other process were
4745 left running. However, on some targets, threads survive a
4746 process exit event. E.g., for the "checkpoint" command,
4747 when the current checkpoint/fork exits, linux-fork.c
4748 automatically switches to another fork from within
4749 target_mourn_inferior, by associating the same
4750 inferior/thread to another fork. We haven't mourned yet at
4751 this point, but we must mark any threads left in the
4752 process as not-executing so that finish_thread_state marks
4753 them stopped (in the user's perspective) if/when we present
4754 the stop to the user. */
4755 mark_ptid
= ptid_t (event_ptid
.pid ());
4758 mark_ptid
= event_ptid
;
4760 set_executing (target
, mark_ptid
, false);
4762 /* Likewise the resumed flag. */
4763 set_resumed (target
, mark_ptid
, false);
4766 /* Handle one event after stopping threads. If the eventing thread
4767 reports back any interesting event, we leave it pending. If the
4768 eventing thread was in the middle of a displaced step, we
4769 cancel/finish it, and unless the thread's inferior is being
4770 detached, put the thread back in the step-over chain. Returns true
4771 if there are no resumed threads left in the target (thus there's no
4772 point in waiting further), false otherwise. */
4775 handle_one (const wait_one_event
&event
)
4778 ("%s %s", event
.ws
.to_string ().c_str (),
4779 event
.ptid
.to_string ().c_str ());
4781 if (event
.ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
4783 /* All resumed threads exited. */
4786 else if (event
.ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
4787 || event
.ws
.kind () == TARGET_WAITKIND_EXITED
4788 || event
.ws
.kind () == TARGET_WAITKIND_SIGNALLED
)
4790 /* One thread/process exited/signalled. */
4792 thread_info
*t
= nullptr;
4794 /* The target may have reported just a pid. If so, try
4795 the first non-exited thread. */
4796 if (event
.ptid
.is_pid ())
4798 int pid
= event
.ptid
.pid ();
4799 inferior
*inf
= find_inferior_pid (event
.target
, pid
);
4800 for (thread_info
*tp
: inf
->non_exited_threads ())
4806 /* If there is no available thread, the event would
4807 have to be appended to a per-inferior event list,
4808 which does not exist (and if it did, we'd have
4809 to adjust run control command to be able to
4810 resume such an inferior). We assert here instead
4811 of going into an infinite loop. */
4812 gdb_assert (t
!= nullptr);
4815 ("using %s", t
->ptid
.to_string ().c_str ());
4819 t
= find_thread_ptid (event
.target
, event
.ptid
);
4820 /* Check if this is the first time we see this thread.
4821 Don't bother adding if it individually exited. */
4823 && event
.ws
.kind () != TARGET_WAITKIND_THREAD_EXITED
)
4824 t
= add_thread (event
.target
, event
.ptid
);
4829 /* Set the threads as non-executing to avoid
4830 another stop attempt on them. */
4831 switch_to_thread_no_regs (t
);
4832 mark_non_executing_threads (event
.target
, event
.ptid
,
4834 save_waitstatus (t
, event
.ws
);
4835 t
->stop_requested
= false;
4840 thread_info
*t
= find_thread_ptid (event
.target
, event
.ptid
);
4842 t
= add_thread (event
.target
, event
.ptid
);
4844 t
->stop_requested
= 0;
4845 t
->set_executing (false);
4846 t
->set_resumed (false);
4847 t
->control
.may_range_step
= 0;
4849 /* This may be the first time we see the inferior report
4851 inferior
*inf
= find_inferior_ptid (event
.target
, event
.ptid
);
4852 if (inf
->needs_setup
)
4854 switch_to_thread_no_regs (t
);
4858 if (event
.ws
.kind () == TARGET_WAITKIND_STOPPED
4859 && event
.ws
.sig () == GDB_SIGNAL_0
)
4861 /* We caught the event that we intended to catch, so
4862 there's no event to save as pending. */
4864 if (displaced_step_finish (t
, GDB_SIGNAL_0
)
4865 == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED
)
4867 /* Add it back to the step-over queue. */
4869 ("displaced-step of %s canceled",
4870 t
->ptid
.to_string ().c_str ());
4872 t
->control
.trap_expected
= 0;
4873 if (!t
->inf
->detaching
)
4874 global_thread_step_over_chain_enqueue (t
);
4879 enum gdb_signal sig
;
4880 struct regcache
*regcache
;
4883 ("target_wait %s, saving status for %s",
4884 event
.ws
.to_string ().c_str (),
4885 t
->ptid
.to_string ().c_str ());
4887 /* Record for later. */
4888 save_waitstatus (t
, event
.ws
);
4890 sig
= (event
.ws
.kind () == TARGET_WAITKIND_STOPPED
4891 ? event
.ws
.sig () : GDB_SIGNAL_0
);
4893 if (displaced_step_finish (t
, sig
)
4894 == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED
)
4896 /* Add it back to the step-over queue. */
4897 t
->control
.trap_expected
= 0;
4898 if (!t
->inf
->detaching
)
4899 global_thread_step_over_chain_enqueue (t
);
4902 regcache
= get_thread_regcache (t
);
4903 t
->set_stop_pc (regcache_read_pc (regcache
));
4905 infrun_debug_printf ("saved stop_pc=%s for %s "
4906 "(currently_stepping=%d)",
4907 paddress (target_gdbarch (), t
->stop_pc ()),
4908 t
->ptid
.to_string ().c_str (),
4909 currently_stepping (t
));
4919 stop_all_threads (void)
4921 /* We may need multiple passes to discover all threads. */
4925 gdb_assert (exists_non_stop_target ());
4927 infrun_debug_printf ("starting");
4929 scoped_restore_current_thread restore_thread
;
4931 /* Enable thread events of all targets. */
4932 for (auto *target
: all_non_exited_process_targets ())
4934 switch_to_target_no_thread (target
);
4935 target_thread_events (true);
4940 /* Disable thread events of all targets. */
4941 for (auto *target
: all_non_exited_process_targets ())
4943 switch_to_target_no_thread (target
);
4944 target_thread_events (false);
4947 /* Use debug_prefixed_printf directly to get a meaningful function
4950 debug_prefixed_printf ("infrun", "stop_all_threads", "done");
4953 /* Request threads to stop, and then wait for the stops. Because
4954 threads we already know about can spawn more threads while we're
4955 trying to stop them, and we only learn about new threads when we
4956 update the thread list, do this in a loop, and keep iterating
4957 until two passes find no threads that need to be stopped. */
4958 for (pass
= 0; pass
< 2; pass
++, iterations
++)
4960 infrun_debug_printf ("pass=%d, iterations=%d", pass
, iterations
);
4963 int waits_needed
= 0;
4965 for (auto *target
: all_non_exited_process_targets ())
4967 switch_to_target_no_thread (target
);
4968 update_thread_list ();
4971 /* Go through all threads looking for threads that we need
4972 to tell the target to stop. */
4973 for (thread_info
*t
: all_non_exited_threads ())
4975 /* For a single-target setting with an all-stop target,
4976 we would not even arrive here. For a multi-target
4977 setting, until GDB is able to handle a mixture of
4978 all-stop and non-stop targets, simply skip all-stop
4979 targets' threads. This should be fine due to the
4980 protection of 'check_multi_target_resumption'. */
4982 switch_to_thread_no_regs (t
);
4983 if (!target_is_non_stop_p ())
4986 if (t
->executing ())
4988 /* If already stopping, don't request a stop again.
4989 We just haven't seen the notification yet. */
4990 if (!t
->stop_requested
)
4992 infrun_debug_printf (" %s executing, need stop",
4993 t
->ptid
.to_string ().c_str ());
4994 target_stop (t
->ptid
);
4995 t
->stop_requested
= 1;
4999 infrun_debug_printf (" %s executing, already stopping",
5000 t
->ptid
.to_string ().c_str ());
5003 if (t
->stop_requested
)
5008 infrun_debug_printf (" %s not executing",
5009 t
->ptid
.to_string ().c_str ());
5011 /* The thread may be not executing, but still be
5012 resumed with a pending status to process. */
5013 t
->set_resumed (false);
5017 if (waits_needed
== 0)
5020 /* If we find new threads on the second iteration, restart
5021 over. We want to see two iterations in a row with all
5026 for (int i
= 0; i
< waits_needed
; i
++)
5028 wait_one_event event
= wait_one ();
5029 if (handle_one (event
))
5036 /* Handle a TARGET_WAITKIND_NO_RESUMED event. */
5039 handle_no_resumed (struct execution_control_state
*ecs
)
5041 if (target_can_async_p ())
5043 bool any_sync
= false;
5045 for (ui
*ui
: all_uis ())
5047 if (ui
->prompt_state
== PROMPT_BLOCKED
)
5055 /* There were no unwaited-for children left in the target, but,
5056 we're not synchronously waiting for events either. Just
5059 infrun_debug_printf ("TARGET_WAITKIND_NO_RESUMED (ignoring: bg)");
5060 prepare_to_wait (ecs
);
5065 /* Otherwise, if we were running a synchronous execution command, we
5066 may need to cancel it and give the user back the terminal.
5068 In non-stop mode, the target can't tell whether we've already
5069 consumed previous stop events, so it can end up sending us a
5070 no-resumed event like so:
5072 #0 - thread 1 is left stopped
5074 #1 - thread 2 is resumed and hits breakpoint
5075 -> TARGET_WAITKIND_STOPPED
5077 #2 - thread 3 is resumed and exits
5078 this is the last resumed thread, so
5079 -> TARGET_WAITKIND_NO_RESUMED
5081 #3 - gdb processes stop for thread 2 and decides to re-resume
5084 #4 - gdb processes the TARGET_WAITKIND_NO_RESUMED event.
5085 thread 2 is now resumed, so the event should be ignored.
5087 IOW, if the stop for thread 2 doesn't end a foreground command,
5088 then we need to ignore the following TARGET_WAITKIND_NO_RESUMED
5089 event. But it could be that the event meant that thread 2 itself
5090 (or whatever other thread was the last resumed thread) exited.
5092 To address this we refresh the thread list and check whether we
5093 have resumed threads _now_. In the example above, this removes
5094 thread 3 from the thread list. If thread 2 was re-resumed, we
5095 ignore this event. If we find no thread resumed, then we cancel
5096 the synchronous command and show "no unwaited-for " to the
5099 inferior
*curr_inf
= current_inferior ();
5101 scoped_restore_current_thread restore_thread
;
5103 for (auto *target
: all_non_exited_process_targets ())
5105 switch_to_target_no_thread (target
);
5106 update_thread_list ();
5111 - the current target has no thread executing, and
5112 - the current inferior is native, and
5113 - the current inferior is the one which has the terminal, and
5116 then a Ctrl-C from this point on would remain stuck in the
5117 kernel, until a thread resumes and dequeues it. That would
5118 result in the GDB CLI not reacting to Ctrl-C, not able to
5119 interrupt the program. To address this, if the current inferior
5120 no longer has any thread executing, we give the terminal to some
5121 other inferior that has at least one thread executing. */
5122 bool swap_terminal
= true;
5124 /* Whether to ignore this TARGET_WAITKIND_NO_RESUMED event, or
5125 whether to report it to the user. */
5126 bool ignore_event
= false;
5128 for (thread_info
*thread
: all_non_exited_threads ())
5130 if (swap_terminal
&& thread
->executing ())
5132 if (thread
->inf
!= curr_inf
)
5134 target_terminal::ours ();
5136 switch_to_thread (thread
);
5137 target_terminal::inferior ();
5139 swap_terminal
= false;
5142 if (!ignore_event
&& thread
->resumed ())
5144 /* Either there were no unwaited-for children left in the
5145 target at some point, but there are now, or some target
5146 other than the eventing one has unwaited-for children
5147 left. Just ignore. */
5148 infrun_debug_printf ("TARGET_WAITKIND_NO_RESUMED "
5149 "(ignoring: found resumed)");
5151 ignore_event
= true;
5154 if (ignore_event
&& !swap_terminal
)
5160 switch_to_inferior_no_thread (curr_inf
);
5161 prepare_to_wait (ecs
);
5165 /* Go ahead and report the event. */
5169 /* Given an execution control state that has been freshly filled in by
5170 an event from the inferior, figure out what it means and take
5173 The alternatives are:
5175 1) stop_waiting and return; to really stop and return to the
5178 2) keep_going and return; to wait for the next event (set
5179 ecs->event_thread->stepping_over_breakpoint to 1 to single step
5183 handle_inferior_event (struct execution_control_state
*ecs
)
5185 /* Make sure that all temporary struct value objects that were
5186 created during the handling of the event get deleted at the
5188 scoped_value_mark free_values
;
5190 infrun_debug_printf ("%s", ecs
->ws
.to_string ().c_str ());
5192 if (ecs
->ws
.kind () == TARGET_WAITKIND_IGNORE
)
5194 /* We had an event in the inferior, but we are not interested in
5195 handling it at this level. The lower layers have already
5196 done what needs to be done, if anything.
5198 One of the possible circumstances for this is when the
5199 inferior produces output for the console. The inferior has
5200 not stopped, and we are ignoring the event. Another possible
5201 circumstance is any event which the lower level knows will be
5202 reported multiple times without an intervening resume. */
5203 prepare_to_wait (ecs
);
5207 if (ecs
->ws
.kind () == TARGET_WAITKIND_THREAD_EXITED
)
5209 prepare_to_wait (ecs
);
5213 if (ecs
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
5214 && handle_no_resumed (ecs
))
5217 /* Cache the last target/ptid/waitstatus. */
5218 set_last_target_status (ecs
->target
, ecs
->ptid
, ecs
->ws
);
5220 /* Always clear state belonging to the previous time we stopped. */
5221 stop_stack_dummy
= STOP_NONE
;
5223 if (ecs
->ws
.kind () == TARGET_WAITKIND_NO_RESUMED
)
5225 /* No unwaited-for children left. IOW, all resumed children
5227 stop_print_frame
= false;
5232 if (ecs
->ws
.kind () != TARGET_WAITKIND_EXITED
5233 && ecs
->ws
.kind () != TARGET_WAITKIND_SIGNALLED
)
5235 ecs
->event_thread
= find_thread_ptid (ecs
->target
, ecs
->ptid
);
5236 /* If it's a new thread, add it to the thread database. */
5237 if (ecs
->event_thread
== NULL
)
5238 ecs
->event_thread
= add_thread (ecs
->target
, ecs
->ptid
);
5240 /* Disable range stepping. If the next step request could use a
5241 range, this will be end up re-enabled then. */
5242 ecs
->event_thread
->control
.may_range_step
= 0;
5245 /* Dependent on valid ECS->EVENT_THREAD. */
5246 adjust_pc_after_break (ecs
->event_thread
, ecs
->ws
);
5248 /* Dependent on the current PC value modified by adjust_pc_after_break. */
5249 reinit_frame_cache ();
5251 breakpoint_retire_moribund ();
5253 /* First, distinguish signals caused by the debugger from signals
5254 that have to do with the program's own actions. Note that
5255 breakpoint insns may cause SIGTRAP or SIGILL or SIGEMT, depending
5256 on the operating system version. Here we detect when a SIGILL or
5257 SIGEMT is really a breakpoint and change it to SIGTRAP. We do
5258 something similar for SIGSEGV, since a SIGSEGV will be generated
5259 when we're trying to execute a breakpoint instruction on a
5260 non-executable stack. This happens for call dummy breakpoints
5261 for architectures like SPARC that place call dummies on the
5263 if (ecs
->ws
.kind () == TARGET_WAITKIND_STOPPED
5264 && (ecs
->ws
.sig () == GDB_SIGNAL_ILL
5265 || ecs
->ws
.sig () == GDB_SIGNAL_SEGV
5266 || ecs
->ws
.sig () == GDB_SIGNAL_EMT
))
5268 struct regcache
*regcache
= get_thread_regcache (ecs
->event_thread
);
5270 if (breakpoint_inserted_here_p (regcache
->aspace (),
5271 regcache_read_pc (regcache
)))
5273 infrun_debug_printf ("Treating signal as SIGTRAP");
5274 ecs
->ws
.set_stopped (GDB_SIGNAL_TRAP
);
5278 mark_non_executing_threads (ecs
->target
, ecs
->ptid
, ecs
->ws
);
5280 switch (ecs
->ws
.kind ())
5282 case TARGET_WAITKIND_LOADED
:
5284 context_switch (ecs
);
5285 /* Ignore gracefully during startup of the inferior, as it might
5286 be the shell which has just loaded some objects, otherwise
5287 add the symbols for the newly loaded objects. Also ignore at
5288 the beginning of an attach or remote session; we will query
5289 the full list of libraries once the connection is
5292 stop_kind stop_soon
= get_inferior_stop_soon (ecs
);
5293 if (stop_soon
== NO_STOP_QUIETLY
)
5295 struct regcache
*regcache
;
5297 regcache
= get_thread_regcache (ecs
->event_thread
);
5299 handle_solib_event ();
5301 ecs
->event_thread
->set_stop_pc (regcache_read_pc (regcache
));
5302 ecs
->event_thread
->control
.stop_bpstat
5303 = bpstat_stop_status (regcache
->aspace (),
5304 ecs
->event_thread
->stop_pc (),
5305 ecs
->event_thread
, ecs
->ws
);
5307 if (handle_stop_requested (ecs
))
5310 if (bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
5312 /* A catchpoint triggered. */
5313 process_event_stop_test (ecs
);
5317 /* If requested, stop when the dynamic linker notifies
5318 gdb of events. This allows the user to get control
5319 and place breakpoints in initializer routines for
5320 dynamically loaded objects (among other things). */
5321 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
5322 if (stop_on_solib_events
)
5324 /* Make sure we print "Stopped due to solib-event" in
5326 stop_print_frame
= true;
5333 /* If we are skipping through a shell, or through shared library
5334 loading that we aren't interested in, resume the program. If
5335 we're running the program normally, also resume. */
5336 if (stop_soon
== STOP_QUIETLY
|| stop_soon
== NO_STOP_QUIETLY
)
5338 /* Loading of shared libraries might have changed breakpoint
5339 addresses. Make sure new breakpoints are inserted. */
5340 if (stop_soon
== NO_STOP_QUIETLY
)
5341 insert_breakpoints ();
5342 resume (GDB_SIGNAL_0
);
5343 prepare_to_wait (ecs
);
5347 /* But stop if we're attaching or setting up a remote
5349 if (stop_soon
== STOP_QUIETLY_NO_SIGSTOP
5350 || stop_soon
== STOP_QUIETLY_REMOTE
)
5352 infrun_debug_printf ("quietly stopped");
5357 internal_error (__FILE__
, __LINE__
,
5358 _("unhandled stop_soon: %d"), (int) stop_soon
);
5361 case TARGET_WAITKIND_SPURIOUS
:
5362 if (handle_stop_requested (ecs
))
5364 context_switch (ecs
);
5365 resume (GDB_SIGNAL_0
);
5366 prepare_to_wait (ecs
);
5369 case TARGET_WAITKIND_THREAD_CREATED
:
5370 if (handle_stop_requested (ecs
))
5372 context_switch (ecs
);
5373 if (!switch_back_to_stepped_thread (ecs
))
5377 case TARGET_WAITKIND_EXITED
:
5378 case TARGET_WAITKIND_SIGNALLED
:
5380 /* Depending on the system, ecs->ptid may point to a thread or
5381 to a process. On some targets, target_mourn_inferior may
5382 need to have access to the just-exited thread. That is the
5383 case of GNU/Linux's "checkpoint" support, for example.
5384 Call the switch_to_xxx routine as appropriate. */
5385 thread_info
*thr
= find_thread_ptid (ecs
->target
, ecs
->ptid
);
5387 switch_to_thread (thr
);
5390 inferior
*inf
= find_inferior_ptid (ecs
->target
, ecs
->ptid
);
5391 switch_to_inferior_no_thread (inf
);
5394 handle_vfork_child_exec_or_exit (0);
5395 target_terminal::ours (); /* Must do this before mourn anyway. */
5397 /* Clearing any previous state of convenience variables. */
5398 clear_exit_convenience_vars ();
5400 if (ecs
->ws
.kind () == TARGET_WAITKIND_EXITED
)
5402 /* Record the exit code in the convenience variable $_exitcode, so
5403 that the user can inspect this again later. */
5404 set_internalvar_integer (lookup_internalvar ("_exitcode"),
5405 (LONGEST
) ecs
->ws
.exit_status ());
5407 /* Also record this in the inferior itself. */
5408 current_inferior ()->has_exit_code
= 1;
5409 current_inferior ()->exit_code
= (LONGEST
) ecs
->ws
.exit_status ();
5411 /* Support the --return-child-result option. */
5412 return_child_result_value
= ecs
->ws
.exit_status ();
5414 gdb::observers::exited
.notify (ecs
->ws
.exit_status ());
5418 struct gdbarch
*gdbarch
= current_inferior ()->gdbarch
;
5420 if (gdbarch_gdb_signal_to_target_p (gdbarch
))
5422 /* Set the value of the internal variable $_exitsignal,
5423 which holds the signal uncaught by the inferior. */
5424 set_internalvar_integer (lookup_internalvar ("_exitsignal"),
5425 gdbarch_gdb_signal_to_target (gdbarch
,
5430 /* We don't have access to the target's method used for
5431 converting between signal numbers (GDB's internal
5432 representation <-> target's representation).
5433 Therefore, we cannot do a good job at displaying this
5434 information to the user. It's better to just warn
5435 her about it (if infrun debugging is enabled), and
5437 infrun_debug_printf ("Cannot fill $_exitsignal with the correct "
5441 gdb::observers::signal_exited
.notify (ecs
->ws
.sig ());
5444 gdb_flush (gdb_stdout
);
5445 target_mourn_inferior (inferior_ptid
);
5446 stop_print_frame
= false;
5450 case TARGET_WAITKIND_FORKED
:
5451 case TARGET_WAITKIND_VFORKED
:
5452 /* Check whether the inferior is displaced stepping. */
5454 struct regcache
*regcache
= get_thread_regcache (ecs
->event_thread
);
5455 struct gdbarch
*gdbarch
= regcache
->arch ();
5456 inferior
*parent_inf
= find_inferior_ptid (ecs
->target
, ecs
->ptid
);
5458 /* If this is a fork (child gets its own address space copy)
5459 and some displaced step buffers were in use at the time of
5460 the fork, restore the displaced step buffer bytes in the
5463 Architectures which support displaced stepping and fork
5464 events must supply an implementation of
5465 gdbarch_displaced_step_restore_all_in_ptid. This is not
5466 enforced during gdbarch validation to support architectures
5467 which support displaced stepping but not forks. */
5468 if (ecs
->ws
.kind () == TARGET_WAITKIND_FORKED
5469 && gdbarch_supports_displaced_stepping (gdbarch
))
5470 gdbarch_displaced_step_restore_all_in_ptid
5471 (gdbarch
, parent_inf
, ecs
->ws
.child_ptid ());
5473 /* If displaced stepping is supported, and thread ecs->ptid is
5474 displaced stepping. */
5475 if (displaced_step_in_progress_thread (ecs
->event_thread
))
5477 struct regcache
*child_regcache
;
5478 CORE_ADDR parent_pc
;
5480 /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
5481 indicating that the displaced stepping of syscall instruction
5482 has been done. Perform cleanup for parent process here. Note
5483 that this operation also cleans up the child process for vfork,
5484 because their pages are shared. */
5485 displaced_step_finish (ecs
->event_thread
, GDB_SIGNAL_TRAP
);
5486 /* Start a new step-over in another thread if there's one
5490 /* Since the vfork/fork syscall instruction was executed in the scratchpad,
5491 the child's PC is also within the scratchpad. Set the child's PC
5492 to the parent's PC value, which has already been fixed up.
5493 FIXME: we use the parent's aspace here, although we're touching
5494 the child, because the child hasn't been added to the inferior
5495 list yet at this point. */
5498 = get_thread_arch_aspace_regcache (parent_inf
->process_target (),
5499 ecs
->ws
.child_ptid (),
5501 parent_inf
->aspace
);
5502 /* Read PC value of parent process. */
5503 parent_pc
= regcache_read_pc (regcache
);
5505 displaced_debug_printf ("write child pc from %s to %s",
5507 regcache_read_pc (child_regcache
)),
5508 paddress (gdbarch
, parent_pc
));
5510 regcache_write_pc (child_regcache
, parent_pc
);
5514 context_switch (ecs
);
5516 /* Immediately detach breakpoints from the child before there's
5517 any chance of letting the user delete breakpoints from the
5518 breakpoint lists. If we don't do this early, it's easy to
5519 leave left over traps in the child, vis: "break foo; catch
5520 fork; c; <fork>; del; c; <child calls foo>". We only follow
5521 the fork on the last `continue', and by that time the
5522 breakpoint at "foo" is long gone from the breakpoint table.
5523 If we vforked, then we don't need to unpatch here, since both
5524 parent and child are sharing the same memory pages; we'll
5525 need to unpatch at follow/detach time instead to be certain
5526 that new breakpoints added between catchpoint hit time and
5527 vfork follow are detached. */
5528 if (ecs
->ws
.kind () != TARGET_WAITKIND_VFORKED
)
5530 /* This won't actually modify the breakpoint list, but will
5531 physically remove the breakpoints from the child. */
5532 detach_breakpoints (ecs
->ws
.child_ptid ());
5535 delete_just_stopped_threads_single_step_breakpoints ();
5537 /* In case the event is caught by a catchpoint, remember that
5538 the event is to be followed at the next resume of the thread,
5539 and not immediately. */
5540 ecs
->event_thread
->pending_follow
= ecs
->ws
;
5542 ecs
->event_thread
->set_stop_pc
5543 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
5545 ecs
->event_thread
->control
.stop_bpstat
5546 = bpstat_stop_status (get_current_regcache ()->aspace (),
5547 ecs
->event_thread
->stop_pc (),
5548 ecs
->event_thread
, ecs
->ws
);
5550 if (handle_stop_requested (ecs
))
5553 /* If no catchpoint triggered for this, then keep going. Note
5554 that we're interested in knowing the bpstat actually causes a
5555 stop, not just if it may explain the signal. Software
5556 watchpoints, for example, always appear in the bpstat. */
5557 if (!bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
5560 = (follow_fork_mode_string
== follow_fork_mode_child
);
5562 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
5564 process_stratum_target
*targ
5565 = ecs
->event_thread
->inf
->process_target ();
5567 bool should_resume
= follow_fork ();
5569 /* Note that one of these may be an invalid pointer,
5570 depending on detach_fork. */
5571 thread_info
*parent
= ecs
->event_thread
;
5572 thread_info
*child
= find_thread_ptid (targ
, ecs
->ws
.child_ptid ());
5574 /* At this point, the parent is marked running, and the
5575 child is marked stopped. */
5577 /* If not resuming the parent, mark it stopped. */
5578 if (follow_child
&& !detach_fork
&& !non_stop
&& !sched_multi
)
5579 parent
->set_running (false);
5581 /* If resuming the child, mark it running. */
5582 if (follow_child
|| (!detach_fork
&& (non_stop
|| sched_multi
)))
5583 child
->set_running (true);
5585 /* In non-stop mode, also resume the other branch. */
5586 if (!detach_fork
&& (non_stop
5587 || (sched_multi
&& target_is_non_stop_p ())))
5590 switch_to_thread (parent
);
5592 switch_to_thread (child
);
5594 ecs
->event_thread
= inferior_thread ();
5595 ecs
->ptid
= inferior_ptid
;
5600 switch_to_thread (child
);
5602 switch_to_thread (parent
);
5604 ecs
->event_thread
= inferior_thread ();
5605 ecs
->ptid
= inferior_ptid
;
5613 process_event_stop_test (ecs
);
5616 case TARGET_WAITKIND_VFORK_DONE
:
5617 /* Done with the shared memory region. Re-insert breakpoints in
5618 the parent, and keep going. */
5620 context_switch (ecs
);
5622 current_inferior ()->waiting_for_vfork_done
= 0;
5623 current_inferior ()->pspace
->breakpoints_not_allowed
= 0;
5625 if (handle_stop_requested (ecs
))
5628 /* This also takes care of reinserting breakpoints in the
5629 previously locked inferior. */
5633 case TARGET_WAITKIND_EXECD
:
5635 /* Note we can't read registers yet (the stop_pc), because we
5636 don't yet know the inferior's post-exec architecture.
5637 'stop_pc' is explicitly read below instead. */
5638 switch_to_thread_no_regs (ecs
->event_thread
);
5640 /* Do whatever is necessary to the parent branch of the vfork. */
5641 handle_vfork_child_exec_or_exit (1);
5643 /* This causes the eventpoints and symbol table to be reset.
5644 Must do this now, before trying to determine whether to
5646 follow_exec (inferior_ptid
, ecs
->ws
.execd_pathname ());
5648 /* In follow_exec we may have deleted the original thread and
5649 created a new one. Make sure that the event thread is the
5650 execd thread for that case (this is a nop otherwise). */
5651 ecs
->event_thread
= inferior_thread ();
5653 ecs
->event_thread
->set_stop_pc
5654 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
5656 ecs
->event_thread
->control
.stop_bpstat
5657 = bpstat_stop_status (get_current_regcache ()->aspace (),
5658 ecs
->event_thread
->stop_pc (),
5659 ecs
->event_thread
, ecs
->ws
);
5661 if (handle_stop_requested (ecs
))
5664 /* If no catchpoint triggered for this, then keep going. */
5665 if (!bpstat_causes_stop (ecs
->event_thread
->control
.stop_bpstat
))
5667 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
5671 process_event_stop_test (ecs
);
5674 /* Be careful not to try to gather much state about a thread
5675 that's in a syscall. It's frequently a losing proposition. */
5676 case TARGET_WAITKIND_SYSCALL_ENTRY
:
5677 /* Getting the current syscall number. */
5678 if (handle_syscall_event (ecs
) == 0)
5679 process_event_stop_test (ecs
);
5682 /* Before examining the threads further, step this thread to
5683 get it entirely out of the syscall. (We get notice of the
5684 event when the thread is just on the verge of exiting a
5685 syscall. Stepping one instruction seems to get it back
5687 case TARGET_WAITKIND_SYSCALL_RETURN
:
5688 if (handle_syscall_event (ecs
) == 0)
5689 process_event_stop_test (ecs
);
5692 case TARGET_WAITKIND_STOPPED
:
5693 handle_signal_stop (ecs
);
5696 case TARGET_WAITKIND_NO_HISTORY
:
5697 /* Reverse execution: target ran out of history info. */
5699 /* Switch to the stopped thread. */
5700 context_switch (ecs
);
5701 infrun_debug_printf ("stopped");
5703 delete_just_stopped_threads_single_step_breakpoints ();
5704 ecs
->event_thread
->set_stop_pc
5705 (regcache_read_pc (get_thread_regcache (inferior_thread ())));
5707 if (handle_stop_requested (ecs
))
5710 gdb::observers::no_history
.notify ();
5716 /* Restart threads back to what they were trying to do back when we
5717 paused them for an in-line step-over. The EVENT_THREAD thread is
5721 restart_threads (struct thread_info
*event_thread
)
5723 /* In case the instruction just stepped spawned a new thread. */
5724 update_thread_list ();
5726 for (thread_info
*tp
: all_non_exited_threads ())
5728 if (tp
->inf
->detaching
)
5730 infrun_debug_printf ("restart threads: [%s] inferior detaching",
5731 tp
->ptid
.to_string ().c_str ());
5735 switch_to_thread_no_regs (tp
);
5737 if (tp
== event_thread
)
5739 infrun_debug_printf ("restart threads: [%s] is event thread",
5740 tp
->ptid
.to_string ().c_str ());
5744 if (!(tp
->state
== THREAD_RUNNING
|| tp
->control
.in_infcall
))
5746 infrun_debug_printf ("restart threads: [%s] not meant to be running",
5747 tp
->ptid
.to_string ().c_str ());
5753 infrun_debug_printf ("restart threads: [%s] resumed",
5754 tp
->ptid
.to_string ().c_str ());
5755 gdb_assert (tp
->executing () || tp
->has_pending_waitstatus ());
5759 if (thread_is_in_step_over_chain (tp
))
5761 infrun_debug_printf ("restart threads: [%s] needs step-over",
5762 tp
->ptid
.to_string ().c_str ());
5763 gdb_assert (!tp
->resumed ());
5768 if (tp
->has_pending_waitstatus ())
5770 infrun_debug_printf ("restart threads: [%s] has pending status",
5771 tp
->ptid
.to_string ().c_str ());
5772 tp
->set_resumed (true);
5776 gdb_assert (!tp
->stop_requested
);
5778 /* If some thread needs to start a step-over at this point, it
5779 should still be in the step-over queue, and thus skipped
5781 if (thread_still_needs_step_over (tp
))
5783 internal_error (__FILE__
, __LINE__
,
5784 "thread [%s] needs a step-over, but not in "
5785 "step-over queue\n",
5786 tp
->ptid
.to_string ().c_str ());
5789 if (currently_stepping (tp
))
5791 infrun_debug_printf ("restart threads: [%s] was stepping",
5792 tp
->ptid
.to_string ().c_str ());
5793 keep_going_stepped_thread (tp
);
5797 struct execution_control_state ecss
;
5798 struct execution_control_state
*ecs
= &ecss
;
5800 infrun_debug_printf ("restart threads: [%s] continuing",
5801 tp
->ptid
.to_string ().c_str ());
5802 reset_ecs (ecs
, tp
);
5803 switch_to_thread (tp
);
5804 keep_going_pass_signal (ecs
);
5809 /* Callback for iterate_over_threads. Find a resumed thread that has
5810 a pending waitstatus. */
5813 resumed_thread_with_pending_status (struct thread_info
*tp
,
5816 return tp
->resumed () && tp
->has_pending_waitstatus ();
5819 /* Called when we get an event that may finish an in-line or
5820 out-of-line (displaced stepping) step-over started previously.
5821 Return true if the event is processed and we should go back to the
5822 event loop; false if the caller should continue processing the
5826 finish_step_over (struct execution_control_state
*ecs
)
5828 displaced_step_finish (ecs
->event_thread
, ecs
->event_thread
->stop_signal ());
5830 bool had_step_over_info
= step_over_info_valid_p ();
5832 if (had_step_over_info
)
5834 /* If we're stepping over a breakpoint with all threads locked,
5835 then only the thread that was stepped should be reporting
5837 gdb_assert (ecs
->event_thread
->control
.trap_expected
);
5839 clear_step_over_info ();
5842 if (!target_is_non_stop_p ())
5845 /* Start a new step-over in another thread if there's one that
5849 /* If we were stepping over a breakpoint before, and haven't started
5850 a new in-line step-over sequence, then restart all other threads
5851 (except the event thread). We can't do this in all-stop, as then
5852 e.g., we wouldn't be able to issue any other remote packet until
5853 these other threads stop. */
5854 if (had_step_over_info
&& !step_over_info_valid_p ())
5856 struct thread_info
*pending
;
5858 /* If we only have threads with pending statuses, the restart
5859 below won't restart any thread and so nothing re-inserts the
5860 breakpoint we just stepped over. But we need it inserted
5861 when we later process the pending events, otherwise if
5862 another thread has a pending event for this breakpoint too,
5863 we'd discard its event (because the breakpoint that
5864 originally caused the event was no longer inserted). */
5865 context_switch (ecs
);
5866 insert_breakpoints ();
5868 restart_threads (ecs
->event_thread
);
5870 /* If we have events pending, go through handle_inferior_event
5871 again, picking up a pending event at random. This avoids
5872 thread starvation. */
5874 /* But not if we just stepped over a watchpoint in order to let
5875 the instruction execute so we can evaluate its expression.
5876 The set of watchpoints that triggered is recorded in the
5877 breakpoint objects themselves (see bp->watchpoint_triggered).
5878 If we processed another event first, that other event could
5879 clobber this info. */
5880 if (ecs
->event_thread
->stepping_over_watchpoint
)
5883 pending
= iterate_over_threads (resumed_thread_with_pending_status
,
5885 if (pending
!= NULL
)
5887 struct thread_info
*tp
= ecs
->event_thread
;
5888 struct regcache
*regcache
;
5890 infrun_debug_printf ("found resumed threads with "
5891 "pending events, saving status");
5893 gdb_assert (pending
!= tp
);
5895 /* Record the event thread's event for later. */
5896 save_waitstatus (tp
, ecs
->ws
);
5897 /* This was cleared early, by handle_inferior_event. Set it
5898 so this pending event is considered by
5900 tp
->set_resumed (true);
5902 gdb_assert (!tp
->executing ());
5904 regcache
= get_thread_regcache (tp
);
5905 tp
->set_stop_pc (regcache_read_pc (regcache
));
5907 infrun_debug_printf ("saved stop_pc=%s for %s "
5908 "(currently_stepping=%d)",
5909 paddress (target_gdbarch (), tp
->stop_pc ()),
5910 tp
->ptid
.to_string ().c_str (),
5911 currently_stepping (tp
));
5913 /* This in-line step-over finished; clear this so we won't
5914 start a new one. This is what handle_signal_stop would
5915 do, if we returned false. */
5916 tp
->stepping_over_breakpoint
= 0;
5918 /* Wake up the event loop again. */
5919 mark_async_event_handler (infrun_async_inferior_event_token
);
5921 prepare_to_wait (ecs
);
5929 /* Come here when the program has stopped with a signal. */
5932 handle_signal_stop (struct execution_control_state
*ecs
)
5934 struct frame_info
*frame
;
5935 struct gdbarch
*gdbarch
;
5936 int stopped_by_watchpoint
;
5937 enum stop_kind stop_soon
;
5940 gdb_assert (ecs
->ws
.kind () == TARGET_WAITKIND_STOPPED
);
5942 ecs
->event_thread
->set_stop_signal (ecs
->ws
.sig ());
5944 /* Do we need to clean up the state of a thread that has
5945 completed a displaced single-step? (Doing so usually affects
5946 the PC, so do it here, before we set stop_pc.) */
5947 if (finish_step_over (ecs
))
5950 /* If we either finished a single-step or hit a breakpoint, but
5951 the user wanted this thread to be stopped, pretend we got a
5952 SIG0 (generic unsignaled stop). */
5953 if (ecs
->event_thread
->stop_requested
5954 && ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
)
5955 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
5957 ecs
->event_thread
->set_stop_pc
5958 (regcache_read_pc (get_thread_regcache (ecs
->event_thread
)));
5960 context_switch (ecs
);
5962 if (deprecated_context_hook
)
5963 deprecated_context_hook (ecs
->event_thread
->global_num
);
5967 struct regcache
*regcache
= get_thread_regcache (ecs
->event_thread
);
5968 struct gdbarch
*reg_gdbarch
= regcache
->arch ();
5971 ("stop_pc=%s", paddress (reg_gdbarch
, ecs
->event_thread
->stop_pc ()));
5972 if (target_stopped_by_watchpoint ())
5976 infrun_debug_printf ("stopped by watchpoint");
5978 if (target_stopped_data_address (current_inferior ()->top_target (),
5980 infrun_debug_printf ("stopped data address=%s",
5981 paddress (reg_gdbarch
, addr
));
5983 infrun_debug_printf ("(no data address available)");
5987 /* This is originated from start_remote(), start_inferior() and
5988 shared libraries hook functions. */
5989 stop_soon
= get_inferior_stop_soon (ecs
);
5990 if (stop_soon
== STOP_QUIETLY
|| stop_soon
== STOP_QUIETLY_REMOTE
)
5992 infrun_debug_printf ("quietly stopped");
5993 stop_print_frame
= true;
5998 /* This originates from attach_command(). We need to overwrite
5999 the stop_signal here, because some kernels don't ignore a
6000 SIGSTOP in a subsequent ptrace(PTRACE_CONT,SIGSTOP) call.
6001 See more comments in inferior.h. On the other hand, if we
6002 get a non-SIGSTOP, report it to the user - assume the backend
6003 will handle the SIGSTOP if it should show up later.
6005 Also consider that the attach is complete when we see a
6006 SIGTRAP. Some systems (e.g. Windows), and stubs supporting
6007 target extended-remote report it instead of a SIGSTOP
6008 (e.g. gdbserver). We already rely on SIGTRAP being our
6009 signal, so this is no exception.
6011 Also consider that the attach is complete when we see a
6012 GDB_SIGNAL_0. In non-stop mode, GDB will explicitly tell
6013 the target to stop all threads of the inferior, in case the
6014 low level attach operation doesn't stop them implicitly. If
6015 they weren't stopped implicitly, then the stub will report a
6016 GDB_SIGNAL_0, meaning: stopped for no particular reason
6017 other than GDB's request. */
6018 if (stop_soon
== STOP_QUIETLY_NO_SIGSTOP
6019 && (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_STOP
6020 || ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6021 || ecs
->event_thread
->stop_signal () == GDB_SIGNAL_0
))
6023 stop_print_frame
= true;
6025 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6029 /* At this point, get hold of the now-current thread's frame. */
6030 frame
= get_current_frame ();
6031 gdbarch
= get_frame_arch (frame
);
6033 /* Pull the single step breakpoints out of the target. */
6034 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
)
6036 struct regcache
*regcache
;
6039 regcache
= get_thread_regcache (ecs
->event_thread
);
6040 const address_space
*aspace
= regcache
->aspace ();
6042 pc
= regcache_read_pc (regcache
);
6044 /* However, before doing so, if this single-step breakpoint was
6045 actually for another thread, set this thread up for moving
6047 if (!thread_has_single_step_breakpoint_here (ecs
->event_thread
,
6050 if (single_step_breakpoint_inserted_here_p (aspace
, pc
))
6052 infrun_debug_printf ("[%s] hit another thread's single-step "
6054 ecs
->ptid
.to_string ().c_str ());
6055 ecs
->hit_singlestep_breakpoint
= 1;
6060 infrun_debug_printf ("[%s] hit its single-step breakpoint",
6061 ecs
->ptid
.to_string ().c_str ());
6064 delete_just_stopped_threads_single_step_breakpoints ();
6066 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6067 && ecs
->event_thread
->control
.trap_expected
6068 && ecs
->event_thread
->stepping_over_watchpoint
)
6069 stopped_by_watchpoint
= 0;
6071 stopped_by_watchpoint
= watchpoints_triggered (ecs
->ws
);
6073 /* If necessary, step over this watchpoint. We'll be back to display
6075 if (stopped_by_watchpoint
6076 && (target_have_steppable_watchpoint ()
6077 || gdbarch_have_nonsteppable_watchpoint (gdbarch
)))
6079 /* At this point, we are stopped at an instruction which has
6080 attempted to write to a piece of memory under control of
6081 a watchpoint. The instruction hasn't actually executed
6082 yet. If we were to evaluate the watchpoint expression
6083 now, we would get the old value, and therefore no change
6084 would seem to have occurred.
6086 In order to make watchpoints work `right', we really need
6087 to complete the memory write, and then evaluate the
6088 watchpoint expression. We do this by single-stepping the
6091 It may not be necessary to disable the watchpoint to step over
6092 it. For example, the PA can (with some kernel cooperation)
6093 single step over a watchpoint without disabling the watchpoint.
6095 It is far more common to need to disable a watchpoint to step
6096 the inferior over it. If we have non-steppable watchpoints,
6097 we must disable the current watchpoint; it's simplest to
6098 disable all watchpoints.
6100 Any breakpoint at PC must also be stepped over -- if there's
6101 one, it will have already triggered before the watchpoint
6102 triggered, and we either already reported it to the user, or
6103 it didn't cause a stop and we called keep_going. In either
6104 case, if there was a breakpoint at PC, we must be trying to
6106 ecs
->event_thread
->stepping_over_watchpoint
= 1;
6111 ecs
->event_thread
->stepping_over_breakpoint
= 0;
6112 ecs
->event_thread
->stepping_over_watchpoint
= 0;
6113 bpstat_clear (&ecs
->event_thread
->control
.stop_bpstat
);
6114 ecs
->event_thread
->control
.stop_step
= 0;
6115 stop_print_frame
= true;
6116 stopped_by_random_signal
= 0;
6117 bpstat
*stop_chain
= nullptr;
6119 /* Hide inlined functions starting here, unless we just performed stepi or
6120 nexti. After stepi and nexti, always show the innermost frame (not any
6121 inline function call sites). */
6122 if (ecs
->event_thread
->control
.step_range_end
!= 1)
6124 const address_space
*aspace
6125 = get_thread_regcache (ecs
->event_thread
)->aspace ();
6127 /* skip_inline_frames is expensive, so we avoid it if we can
6128 determine that the address is one where functions cannot have
6129 been inlined. This improves performance with inferiors that
6130 load a lot of shared libraries, because the solib event
6131 breakpoint is defined as the address of a function (i.e. not
6132 inline). Note that we have to check the previous PC as well
6133 as the current one to catch cases when we have just
6134 single-stepped off a breakpoint prior to reinstating it.
6135 Note that we're assuming that the code we single-step to is
6136 not inline, but that's not definitive: there's nothing
6137 preventing the event breakpoint function from containing
6138 inlined code, and the single-step ending up there. If the
6139 user had set a breakpoint on that inlined code, the missing
6140 skip_inline_frames call would break things. Fortunately
6141 that's an extremely unlikely scenario. */
6142 if (!pc_at_non_inline_function (aspace
,
6143 ecs
->event_thread
->stop_pc (),
6145 && !(ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6146 && ecs
->event_thread
->control
.trap_expected
6147 && pc_at_non_inline_function (aspace
,
6148 ecs
->event_thread
->prev_pc
,
6151 stop_chain
= build_bpstat_chain (aspace
,
6152 ecs
->event_thread
->stop_pc (),
6154 skip_inline_frames (ecs
->event_thread
, stop_chain
);
6156 /* Re-fetch current thread's frame in case that invalidated
6158 frame
= get_current_frame ();
6159 gdbarch
= get_frame_arch (frame
);
6163 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6164 && ecs
->event_thread
->control
.trap_expected
6165 && gdbarch_single_step_through_delay_p (gdbarch
)
6166 && currently_stepping (ecs
->event_thread
))
6168 /* We're trying to step off a breakpoint. Turns out that we're
6169 also on an instruction that needs to be stepped multiple
6170 times before it's been fully executing. E.g., architectures
6171 with a delay slot. It needs to be stepped twice, once for
6172 the instruction and once for the delay slot. */
6173 int step_through_delay
6174 = gdbarch_single_step_through_delay (gdbarch
, frame
);
6176 if (step_through_delay
)
6177 infrun_debug_printf ("step through delay");
6179 if (ecs
->event_thread
->control
.step_range_end
== 0
6180 && step_through_delay
)
6182 /* The user issued a continue when stopped at a breakpoint.
6183 Set up for another trap and get out of here. */
6184 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6188 else if (step_through_delay
)
6190 /* The user issued a step when stopped at a breakpoint.
6191 Maybe we should stop, maybe we should not - the delay
6192 slot *might* correspond to a line of source. In any
6193 case, don't decide that here, just set
6194 ecs->stepping_over_breakpoint, making sure we
6195 single-step again before breakpoints are re-inserted. */
6196 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6200 /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
6201 handles this event. */
6202 ecs
->event_thread
->control
.stop_bpstat
6203 = bpstat_stop_status (get_current_regcache ()->aspace (),
6204 ecs
->event_thread
->stop_pc (),
6205 ecs
->event_thread
, ecs
->ws
, stop_chain
);
6207 /* Following in case break condition called a
6209 stop_print_frame
= true;
6211 /* This is where we handle "moribund" watchpoints. Unlike
6212 software breakpoints traps, hardware watchpoint traps are
6213 always distinguishable from random traps. If no high-level
6214 watchpoint is associated with the reported stop data address
6215 anymore, then the bpstat does not explain the signal ---
6216 simply make sure to ignore it if `stopped_by_watchpoint' is
6219 if (ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6220 && !bpstat_explains_signal (ecs
->event_thread
->control
.stop_bpstat
,
6222 && stopped_by_watchpoint
)
6224 infrun_debug_printf ("no user watchpoint explains watchpoint SIGTRAP, "
6228 /* NOTE: cagney/2003-03-29: These checks for a random signal
6229 at one stage in the past included checks for an inferior
6230 function call's call dummy's return breakpoint. The original
6231 comment, that went with the test, read:
6233 ``End of a stack dummy. Some systems (e.g. Sony news) give
6234 another signal besides SIGTRAP, so check here as well as
6237 If someone ever tries to get call dummys on a
6238 non-executable stack to work (where the target would stop
6239 with something like a SIGSEGV), then those tests might need
6240 to be re-instated. Given, however, that the tests were only
6241 enabled when momentary breakpoints were not being used, I
6242 suspect that it won't be the case.
6244 NOTE: kettenis/2004-02-05: Indeed such checks don't seem to
6245 be necessary for call dummies on a non-executable stack on
6248 /* See if the breakpoints module can explain the signal. */
6250 = !bpstat_explains_signal (ecs
->event_thread
->control
.stop_bpstat
,
6251 ecs
->event_thread
->stop_signal ());
6253 /* Maybe this was a trap for a software breakpoint that has since
6255 if (random_signal
&& target_stopped_by_sw_breakpoint ())
6257 if (gdbarch_program_breakpoint_here_p (gdbarch
,
6258 ecs
->event_thread
->stop_pc ()))
6260 struct regcache
*regcache
;
6263 /* Re-adjust PC to what the program would see if GDB was not
6265 regcache
= get_thread_regcache (ecs
->event_thread
);
6266 decr_pc
= gdbarch_decr_pc_after_break (gdbarch
);
6269 gdb::optional
<scoped_restore_tmpl
<int>>
6270 restore_operation_disable
;
6272 if (record_full_is_used ())
6273 restore_operation_disable
.emplace
6274 (record_full_gdb_operation_disable_set ());
6276 regcache_write_pc (regcache
,
6277 ecs
->event_thread
->stop_pc () + decr_pc
);
6282 /* A delayed software breakpoint event. Ignore the trap. */
6283 infrun_debug_printf ("delayed software breakpoint trap, ignoring");
6288 /* Maybe this was a trap for a hardware breakpoint/watchpoint that
6289 has since been removed. */
6290 if (random_signal
&& target_stopped_by_hw_breakpoint ())
6292 /* A delayed hardware breakpoint event. Ignore the trap. */
6293 infrun_debug_printf ("delayed hardware breakpoint/watchpoint "
6298 /* If not, perhaps stepping/nexting can. */
6300 random_signal
= !(ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
6301 && currently_stepping (ecs
->event_thread
));
6303 /* Perhaps the thread hit a single-step breakpoint of _another_
6304 thread. Single-step breakpoints are transparent to the
6305 breakpoints module. */
6307 random_signal
= !ecs
->hit_singlestep_breakpoint
;
6309 /* No? Perhaps we got a moribund watchpoint. */
6311 random_signal
= !stopped_by_watchpoint
;
6313 /* Always stop if the user explicitly requested this thread to
6315 if (ecs
->event_thread
->stop_requested
)
6318 infrun_debug_printf ("user-requested stop");
6321 /* For the program's own signals, act according to
6322 the signal handling tables. */
6326 /* Signal not for debugging purposes. */
6327 enum gdb_signal stop_signal
= ecs
->event_thread
->stop_signal ();
6329 infrun_debug_printf ("random signal (%s)",
6330 gdb_signal_to_symbol_string (stop_signal
));
6332 stopped_by_random_signal
= 1;
6334 /* Always stop on signals if we're either just gaining control
6335 of the program, or the user explicitly requested this thread
6336 to remain stopped. */
6337 if (stop_soon
!= NO_STOP_QUIETLY
6338 || ecs
->event_thread
->stop_requested
6339 || signal_stop_state (ecs
->event_thread
->stop_signal ()))
6345 /* Notify observers the signal has "handle print" set. Note we
6346 returned early above if stopping; normal_stop handles the
6347 printing in that case. */
6348 if (signal_print
[ecs
->event_thread
->stop_signal ()])
6350 /* The signal table tells us to print about this signal. */
6351 target_terminal::ours_for_output ();
6352 gdb::observers::signal_received
.notify (ecs
->event_thread
->stop_signal ());
6353 target_terminal::inferior ();
6356 /* Clear the signal if it should not be passed. */
6357 if (signal_program
[ecs
->event_thread
->stop_signal ()] == 0)
6358 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
6360 if (ecs
->event_thread
->prev_pc
== ecs
->event_thread
->stop_pc ()
6361 && ecs
->event_thread
->control
.trap_expected
6362 && ecs
->event_thread
->control
.step_resume_breakpoint
== NULL
)
6364 /* We were just starting a new sequence, attempting to
6365 single-step off of a breakpoint and expecting a SIGTRAP.
6366 Instead this signal arrives. This signal will take us out
6367 of the stepping range so GDB needs to remember to, when
6368 the signal handler returns, resume stepping off that
6370 /* To simplify things, "continue" is forced to use the same
6371 code paths as single-step - set a breakpoint at the
6372 signal return address and then, once hit, step off that
6374 infrun_debug_printf ("signal arrived while stepping over breakpoint");
6376 insert_hp_step_resume_breakpoint_at_frame (frame
);
6377 ecs
->event_thread
->step_after_step_resume_breakpoint
= 1;
6378 /* Reset trap_expected to ensure breakpoints are re-inserted. */
6379 ecs
->event_thread
->control
.trap_expected
= 0;
6381 /* If we were nexting/stepping some other thread, switch to
6382 it, so that we don't continue it, losing control. */
6383 if (!switch_back_to_stepped_thread (ecs
))
6388 if (ecs
->event_thread
->stop_signal () != GDB_SIGNAL_0
6389 && (pc_in_thread_step_range (ecs
->event_thread
->stop_pc (),
6391 || ecs
->event_thread
->control
.step_range_end
== 1)
6392 && frame_id_eq (get_stack_frame_id (frame
),
6393 ecs
->event_thread
->control
.step_stack_frame_id
)
6394 && ecs
->event_thread
->control
.step_resume_breakpoint
== NULL
)
6396 /* The inferior is about to take a signal that will take it
6397 out of the single step range. Set a breakpoint at the
6398 current PC (which is presumably where the signal handler
6399 will eventually return) and then allow the inferior to
6402 Note that this is only needed for a signal delivered
6403 while in the single-step range. Nested signals aren't a
6404 problem as they eventually all return. */
6405 infrun_debug_printf ("signal may take us out of single-step range");
6407 clear_step_over_info ();
6408 insert_hp_step_resume_breakpoint_at_frame (frame
);
6409 ecs
->event_thread
->step_after_step_resume_breakpoint
= 1;
6410 /* Reset trap_expected to ensure breakpoints are re-inserted. */
6411 ecs
->event_thread
->control
.trap_expected
= 0;
6416 /* Note: step_resume_breakpoint may be non-NULL. This occurs
6417 when either there's a nested signal, or when there's a
6418 pending signal enabled just as the signal handler returns
6419 (leaving the inferior at the step-resume-breakpoint without
6420 actually executing it). Either way continue until the
6421 breakpoint is really hit. */
6423 if (!switch_back_to_stepped_thread (ecs
))
6425 infrun_debug_printf ("random signal, keep going");
6432 process_event_stop_test (ecs
);
6435 /* Come here when we've got some debug event / signal we can explain
6436 (IOW, not a random signal), and test whether it should cause a
6437 stop, or whether we should resume the inferior (transparently).
6438 E.g., could be a breakpoint whose condition evaluates false; we
6439 could be still stepping within the line; etc. */
6442 process_event_stop_test (struct execution_control_state
*ecs
)
6444 struct symtab_and_line stop_pc_sal
;
6445 struct frame_info
*frame
;
6446 struct gdbarch
*gdbarch
;
6447 CORE_ADDR jmp_buf_pc
;
6448 struct bpstat_what what
;
6450 /* Handle cases caused by hitting a breakpoint. */
6452 frame
= get_current_frame ();
6453 gdbarch
= get_frame_arch (frame
);
6455 what
= bpstat_what (ecs
->event_thread
->control
.stop_bpstat
);
6457 if (what
.call_dummy
)
6459 stop_stack_dummy
= what
.call_dummy
;
6462 /* A few breakpoint types have callbacks associated (e.g.,
6463 bp_jit_event). Run them now. */
6464 bpstat_run_callbacks (ecs
->event_thread
->control
.stop_bpstat
);
6466 /* If we hit an internal event that triggers symbol changes, the
6467 current frame will be invalidated within bpstat_what (e.g., if we
6468 hit an internal solib event). Re-fetch it. */
6469 frame
= get_current_frame ();
6470 gdbarch
= get_frame_arch (frame
);
6472 switch (what
.main_action
)
6474 case BPSTAT_WHAT_SET_LONGJMP_RESUME
:
6475 /* If we hit the breakpoint at longjmp while stepping, we
6476 install a momentary breakpoint at the target of the
6479 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME");
6481 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6483 if (what
.is_longjmp
)
6485 struct value
*arg_value
;
6487 /* If we set the longjmp breakpoint via a SystemTap probe,
6488 then use it to extract the arguments. The destination PC
6489 is the third argument to the probe. */
6490 arg_value
= probe_safe_evaluate_at_pc (frame
, 2);
6493 jmp_buf_pc
= value_as_address (arg_value
);
6494 jmp_buf_pc
= gdbarch_addr_bits_remove (gdbarch
, jmp_buf_pc
);
6496 else if (!gdbarch_get_longjmp_target_p (gdbarch
)
6497 || !gdbarch_get_longjmp_target (gdbarch
,
6498 frame
, &jmp_buf_pc
))
6500 infrun_debug_printf ("BPSTAT_WHAT_SET_LONGJMP_RESUME "
6501 "(!gdbarch_get_longjmp_target)");
6506 /* Insert a breakpoint at resume address. */
6507 insert_longjmp_resume_breakpoint (gdbarch
, jmp_buf_pc
);
6510 check_exception_resume (ecs
, frame
);
6514 case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME
:
6516 struct frame_info
*init_frame
;
6518 /* There are several cases to consider.
6520 1. The initiating frame no longer exists. In this case we
6521 must stop, because the exception or longjmp has gone too
6524 2. The initiating frame exists, and is the same as the
6525 current frame. We stop, because the exception or longjmp
6528 3. The initiating frame exists and is different from the
6529 current frame. This means the exception or longjmp has
6530 been caught beneath the initiating frame, so keep going.
6532 4. longjmp breakpoint has been placed just to protect
6533 against stale dummy frames and user is not interested in
6534 stopping around longjmps. */
6536 infrun_debug_printf ("BPSTAT_WHAT_CLEAR_LONGJMP_RESUME");
6538 gdb_assert (ecs
->event_thread
->control
.exception_resume_breakpoint
6540 delete_exception_resume_breakpoint (ecs
->event_thread
);
6542 if (what
.is_longjmp
)
6544 check_longjmp_breakpoint_for_call_dummy (ecs
->event_thread
);
6546 if (!frame_id_p (ecs
->event_thread
->initiating_frame
))
6554 init_frame
= frame_find_by_id (ecs
->event_thread
->initiating_frame
);
6558 struct frame_id current_id
6559 = get_frame_id (get_current_frame ());
6560 if (frame_id_eq (current_id
,
6561 ecs
->event_thread
->initiating_frame
))
6563 /* Case 2. Fall through. */
6573 /* For Cases 1 and 2, remove the step-resume breakpoint, if it
6575 delete_step_resume_breakpoint (ecs
->event_thread
);
6577 end_stepping_range (ecs
);
6581 case BPSTAT_WHAT_SINGLE
:
6582 infrun_debug_printf ("BPSTAT_WHAT_SINGLE");
6583 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6584 /* Still need to check other stuff, at least the case where we
6585 are stepping and step out of the right range. */
6588 case BPSTAT_WHAT_STEP_RESUME
:
6589 infrun_debug_printf ("BPSTAT_WHAT_STEP_RESUME");
6591 delete_step_resume_breakpoint (ecs
->event_thread
);
6592 if (ecs
->event_thread
->control
.proceed_to_finish
6593 && execution_direction
== EXEC_REVERSE
)
6595 struct thread_info
*tp
= ecs
->event_thread
;
6597 /* We are finishing a function in reverse, and just hit the
6598 step-resume breakpoint at the start address of the
6599 function, and we're almost there -- just need to back up
6600 by one more single-step, which should take us back to the
6602 tp
->control
.step_range_start
= tp
->control
.step_range_end
= 1;
6606 fill_in_stop_func (gdbarch
, ecs
);
6607 if (ecs
->event_thread
->stop_pc () == ecs
->stop_func_start
6608 && execution_direction
== EXEC_REVERSE
)
6610 /* We are stepping over a function call in reverse, and just
6611 hit the step-resume breakpoint at the start address of
6612 the function. Go back to single-stepping, which should
6613 take us back to the function call. */
6614 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6620 case BPSTAT_WHAT_STOP_NOISY
:
6621 infrun_debug_printf ("BPSTAT_WHAT_STOP_NOISY");
6622 stop_print_frame
= true;
6624 /* Assume the thread stopped for a breakpoint. We'll still check
6625 whether a/the breakpoint is there when the thread is next
6627 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6632 case BPSTAT_WHAT_STOP_SILENT
:
6633 infrun_debug_printf ("BPSTAT_WHAT_STOP_SILENT");
6634 stop_print_frame
= false;
6636 /* Assume the thread stopped for a breakpoint. We'll still check
6637 whether a/the breakpoint is there when the thread is next
6639 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6643 case BPSTAT_WHAT_HP_STEP_RESUME
:
6644 infrun_debug_printf ("BPSTAT_WHAT_HP_STEP_RESUME");
6646 delete_step_resume_breakpoint (ecs
->event_thread
);
6647 if (ecs
->event_thread
->step_after_step_resume_breakpoint
)
6649 /* Back when the step-resume breakpoint was inserted, we
6650 were trying to single-step off a breakpoint. Go back to
6652 ecs
->event_thread
->step_after_step_resume_breakpoint
= 0;
6653 ecs
->event_thread
->stepping_over_breakpoint
= 1;
6659 case BPSTAT_WHAT_KEEP_CHECKING
:
6663 /* If we stepped a permanent breakpoint and we had a high priority
6664 step-resume breakpoint for the address we stepped, but we didn't
6665 hit it, then we must have stepped into the signal handler. The
6666 step-resume was only necessary to catch the case of _not_
6667 stepping into the handler, so delete it, and fall through to
6668 checking whether the step finished. */
6669 if (ecs
->event_thread
->stepped_breakpoint
)
6671 struct breakpoint
*sr_bp
6672 = ecs
->event_thread
->control
.step_resume_breakpoint
;
6675 && sr_bp
->loc
->permanent
6676 && sr_bp
->type
== bp_hp_step_resume
6677 && sr_bp
->loc
->address
== ecs
->event_thread
->prev_pc
)
6679 infrun_debug_printf ("stepped permanent breakpoint, stopped in handler");
6680 delete_step_resume_breakpoint (ecs
->event_thread
);
6681 ecs
->event_thread
->step_after_step_resume_breakpoint
= 0;
6685 /* We come here if we hit a breakpoint but should not stop for it.
6686 Possibly we also were stepping and should stop for that. So fall
6687 through and test for stepping. But, if not stepping, do not
6690 /* In all-stop mode, if we're currently stepping but have stopped in
6691 some other thread, we need to switch back to the stepped thread. */
6692 if (switch_back_to_stepped_thread (ecs
))
6695 if (ecs
->event_thread
->control
.step_resume_breakpoint
)
6697 infrun_debug_printf ("step-resume breakpoint is inserted");
6699 /* Having a step-resume breakpoint overrides anything
6700 else having to do with stepping commands until
6701 that breakpoint is reached. */
6706 if (ecs
->event_thread
->control
.step_range_end
== 0)
6708 infrun_debug_printf ("no stepping, continue");
6709 /* Likewise if we aren't even stepping. */
6714 /* Re-fetch current thread's frame in case the code above caused
6715 the frame cache to be re-initialized, making our FRAME variable
6716 a dangling pointer. */
6717 frame
= get_current_frame ();
6718 gdbarch
= get_frame_arch (frame
);
6719 fill_in_stop_func (gdbarch
, ecs
);
6721 /* If stepping through a line, keep going if still within it.
6723 Note that step_range_end is the address of the first instruction
6724 beyond the step range, and NOT the address of the last instruction
6727 Note also that during reverse execution, we may be stepping
6728 through a function epilogue and therefore must detect when
6729 the current-frame changes in the middle of a line. */
6731 if (pc_in_thread_step_range (ecs
->event_thread
->stop_pc (),
6733 && (execution_direction
!= EXEC_REVERSE
6734 || frame_id_eq (get_frame_id (frame
),
6735 ecs
->event_thread
->control
.step_frame_id
)))
6738 ("stepping inside range [%s-%s]",
6739 paddress (gdbarch
, ecs
->event_thread
->control
.step_range_start
),
6740 paddress (gdbarch
, ecs
->event_thread
->control
.step_range_end
));
6742 /* Tentatively re-enable range stepping; `resume' disables it if
6743 necessary (e.g., if we're stepping over a breakpoint or we
6744 have software watchpoints). */
6745 ecs
->event_thread
->control
.may_range_step
= 1;
6747 /* When stepping backward, stop at beginning of line range
6748 (unless it's the function entry point, in which case
6749 keep going back to the call point). */
6750 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
6751 if (stop_pc
== ecs
->event_thread
->control
.step_range_start
6752 && stop_pc
!= ecs
->stop_func_start
6753 && execution_direction
== EXEC_REVERSE
)
6754 end_stepping_range (ecs
);
6761 /* We stepped out of the stepping range. */
6763 /* If we are stepping at the source level and entered the runtime
6764 loader dynamic symbol resolution code...
6766 EXEC_FORWARD: we keep on single stepping until we exit the run
6767 time loader code and reach the callee's address.
6769 EXEC_REVERSE: we've already executed the callee (backward), and
6770 the runtime loader code is handled just like any other
6771 undebuggable function call. Now we need only keep stepping
6772 backward through the trampoline code, and that's handled further
6773 down, so there is nothing for us to do here. */
6775 if (execution_direction
!= EXEC_REVERSE
6776 && ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
6777 && in_solib_dynsym_resolve_code (ecs
->event_thread
->stop_pc ()))
6779 CORE_ADDR pc_after_resolver
=
6780 gdbarch_skip_solib_resolver (gdbarch
, ecs
->event_thread
->stop_pc ());
6782 infrun_debug_printf ("stepped into dynsym resolve code");
6784 if (pc_after_resolver
)
6786 /* Set up a step-resume breakpoint at the address
6787 indicated by SKIP_SOLIB_RESOLVER. */
6788 symtab_and_line sr_sal
;
6789 sr_sal
.pc
= pc_after_resolver
;
6790 sr_sal
.pspace
= get_frame_program_space (frame
);
6792 insert_step_resume_breakpoint_at_sal (gdbarch
,
6793 sr_sal
, null_frame_id
);
6800 /* Step through an indirect branch thunk. */
6801 if (ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
6802 && gdbarch_in_indirect_branch_thunk (gdbarch
,
6803 ecs
->event_thread
->stop_pc ()))
6805 infrun_debug_printf ("stepped into indirect branch thunk");
6810 if (ecs
->event_thread
->control
.step_range_end
!= 1
6811 && (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
6812 || ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
)
6813 && get_frame_type (frame
) == SIGTRAMP_FRAME
)
6815 infrun_debug_printf ("stepped into signal trampoline");
6816 /* The inferior, while doing a "step" or "next", has ended up in
6817 a signal trampoline (either by a signal being delivered or by
6818 the signal handler returning). Just single-step until the
6819 inferior leaves the trampoline (either by calling the handler
6825 /* If we're in the return path from a shared library trampoline,
6826 we want to proceed through the trampoline when stepping. */
6827 /* macro/2012-04-25: This needs to come before the subroutine
6828 call check below as on some targets return trampolines look
6829 like subroutine calls (MIPS16 return thunks). */
6830 if (gdbarch_in_solib_return_trampoline (gdbarch
,
6831 ecs
->event_thread
->stop_pc (),
6832 ecs
->stop_func_name
)
6833 && ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
)
6835 /* Determine where this trampoline returns. */
6836 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
6837 CORE_ADDR real_stop_pc
6838 = gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
);
6840 infrun_debug_printf ("stepped into solib return tramp");
6842 /* Only proceed through if we know where it's going. */
6845 /* And put the step-breakpoint there and go until there. */
6846 symtab_and_line sr_sal
;
6847 sr_sal
.pc
= real_stop_pc
;
6848 sr_sal
.section
= find_pc_overlay (sr_sal
.pc
);
6849 sr_sal
.pspace
= get_frame_program_space (frame
);
6851 /* Do not specify what the fp should be when we stop since
6852 on some machines the prologue is where the new fp value
6854 insert_step_resume_breakpoint_at_sal (gdbarch
,
6855 sr_sal
, null_frame_id
);
6857 /* Restart without fiddling with the step ranges or
6864 /* Check for subroutine calls. The check for the current frame
6865 equalling the step ID is not necessary - the check of the
6866 previous frame's ID is sufficient - but it is a common case and
6867 cheaper than checking the previous frame's ID.
6869 NOTE: frame_id_eq will never report two invalid frame IDs as
6870 being equal, so to get into this block, both the current and
6871 previous frame must have valid frame IDs. */
6872 /* The outer_frame_id check is a heuristic to detect stepping
6873 through startup code. If we step over an instruction which
6874 sets the stack pointer from an invalid value to a valid value,
6875 we may detect that as a subroutine call from the mythical
6876 "outermost" function. This could be fixed by marking
6877 outermost frames as !stack_p,code_p,special_p. Then the
6878 initial outermost frame, before sp was valid, would
6879 have code_addr == &_start. See the comment in frame_id_eq
6881 if (!frame_id_eq (get_stack_frame_id (frame
),
6882 ecs
->event_thread
->control
.step_stack_frame_id
)
6883 && (frame_id_eq (frame_unwind_caller_id (get_current_frame ()),
6884 ecs
->event_thread
->control
.step_stack_frame_id
)
6885 && (!frame_id_eq (ecs
->event_thread
->control
.step_stack_frame_id
,
6887 || (ecs
->event_thread
->control
.step_start_function
6888 != find_pc_function (ecs
->event_thread
->stop_pc ())))))
6890 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
6891 CORE_ADDR real_stop_pc
;
6893 infrun_debug_printf ("stepped into subroutine");
6895 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_NONE
)
6897 /* I presume that step_over_calls is only 0 when we're
6898 supposed to be stepping at the assembly language level
6899 ("stepi"). Just stop. */
6900 /* And this works the same backward as frontward. MVS */
6901 end_stepping_range (ecs
);
6905 /* Reverse stepping through solib trampolines. */
6907 if (execution_direction
== EXEC_REVERSE
6908 && ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
6909 && (gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
)
6910 || (ecs
->stop_func_start
== 0
6911 && in_solib_dynsym_resolve_code (stop_pc
))))
6913 /* Any solib trampoline code can be handled in reverse
6914 by simply continuing to single-step. We have already
6915 executed the solib function (backwards), and a few
6916 steps will take us back through the trampoline to the
6922 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
)
6924 /* We're doing a "next".
6926 Normal (forward) execution: set a breakpoint at the
6927 callee's return address (the address at which the caller
6930 Reverse (backward) execution. set the step-resume
6931 breakpoint at the start of the function that we just
6932 stepped into (backwards), and continue to there. When we
6933 get there, we'll need to single-step back to the caller. */
6935 if (execution_direction
== EXEC_REVERSE
)
6937 /* If we're already at the start of the function, we've either
6938 just stepped backward into a single instruction function,
6939 or stepped back out of a signal handler to the first instruction
6940 of the function. Just keep going, which will single-step back
6942 if (ecs
->stop_func_start
!= stop_pc
&& ecs
->stop_func_start
!= 0)
6944 /* Normal function call return (static or dynamic). */
6945 symtab_and_line sr_sal
;
6946 sr_sal
.pc
= ecs
->stop_func_start
;
6947 sr_sal
.pspace
= get_frame_program_space (frame
);
6948 insert_step_resume_breakpoint_at_sal (gdbarch
,
6949 sr_sal
, null_frame_id
);
6953 insert_step_resume_breakpoint_at_caller (frame
);
6959 /* If we are in a function call trampoline (a stub between the
6960 calling routine and the real function), locate the real
6961 function. That's what tells us (a) whether we want to step
6962 into it at all, and (b) what prologue we want to run to the
6963 end of, if we do step into it. */
6964 real_stop_pc
= skip_language_trampoline (frame
, stop_pc
);
6965 if (real_stop_pc
== 0)
6966 real_stop_pc
= gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
);
6967 if (real_stop_pc
!= 0)
6968 ecs
->stop_func_start
= real_stop_pc
;
6970 if (real_stop_pc
!= 0 && in_solib_dynsym_resolve_code (real_stop_pc
))
6972 symtab_and_line sr_sal
;
6973 sr_sal
.pc
= ecs
->stop_func_start
;
6974 sr_sal
.pspace
= get_frame_program_space (frame
);
6976 insert_step_resume_breakpoint_at_sal (gdbarch
,
6977 sr_sal
, null_frame_id
);
6982 /* If we have line number information for the function we are
6983 thinking of stepping into and the function isn't on the skip
6986 If there are several symtabs at that PC (e.g. with include
6987 files), just want to know whether *any* of them have line
6988 numbers. find_pc_line handles this. */
6990 struct symtab_and_line tmp_sal
;
6992 tmp_sal
= find_pc_line (ecs
->stop_func_start
, 0);
6993 if (tmp_sal
.line
!= 0
6994 && !function_name_is_marked_for_skip (ecs
->stop_func_name
,
6996 && !inline_frame_is_marked_for_skip (true, ecs
->event_thread
))
6998 if (execution_direction
== EXEC_REVERSE
)
6999 handle_step_into_function_backward (gdbarch
, ecs
);
7001 handle_step_into_function (gdbarch
, ecs
);
7006 /* If we have no line number and the step-stop-if-no-debug is
7007 set, we stop the step so that the user has a chance to switch
7008 in assembly mode. */
7009 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
7010 && step_stop_if_no_debug
)
7012 end_stepping_range (ecs
);
7016 if (execution_direction
== EXEC_REVERSE
)
7018 /* If we're already at the start of the function, we've either just
7019 stepped backward into a single instruction function without line
7020 number info, or stepped back out of a signal handler to the first
7021 instruction of the function without line number info. Just keep
7022 going, which will single-step back to the caller. */
7023 if (ecs
->stop_func_start
!= stop_pc
)
7025 /* Set a breakpoint at callee's start address.
7026 From there we can step once and be back in the caller. */
7027 symtab_and_line sr_sal
;
7028 sr_sal
.pc
= ecs
->stop_func_start
;
7029 sr_sal
.pspace
= get_frame_program_space (frame
);
7030 insert_step_resume_breakpoint_at_sal (gdbarch
,
7031 sr_sal
, null_frame_id
);
7035 /* Set a breakpoint at callee's return address (the address
7036 at which the caller will resume). */
7037 insert_step_resume_breakpoint_at_caller (frame
);
7043 /* Reverse stepping through solib trampolines. */
7045 if (execution_direction
== EXEC_REVERSE
7046 && ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_NONE
)
7048 CORE_ADDR stop_pc
= ecs
->event_thread
->stop_pc ();
7050 if (gdbarch_skip_trampoline_code (gdbarch
, frame
, stop_pc
)
7051 || (ecs
->stop_func_start
== 0
7052 && in_solib_dynsym_resolve_code (stop_pc
)))
7054 /* Any solib trampoline code can be handled in reverse
7055 by simply continuing to single-step. We have already
7056 executed the solib function (backwards), and a few
7057 steps will take us back through the trampoline to the
7062 else if (in_solib_dynsym_resolve_code (stop_pc
))
7064 /* Stepped backward into the solib dynsym resolver.
7065 Set a breakpoint at its start and continue, then
7066 one more step will take us out. */
7067 symtab_and_line sr_sal
;
7068 sr_sal
.pc
= ecs
->stop_func_start
;
7069 sr_sal
.pspace
= get_frame_program_space (frame
);
7070 insert_step_resume_breakpoint_at_sal (gdbarch
,
7071 sr_sal
, null_frame_id
);
7077 /* This always returns the sal for the inner-most frame when we are in a
7078 stack of inlined frames, even if GDB actually believes that it is in a
7079 more outer frame. This is checked for below by calls to
7080 inline_skipped_frames. */
7081 stop_pc_sal
= find_pc_line (ecs
->event_thread
->stop_pc (), 0);
7083 /* NOTE: tausq/2004-05-24: This if block used to be done before all
7084 the trampoline processing logic, however, there are some trampolines
7085 that have no names, so we should do trampoline handling first. */
7086 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_UNDEBUGGABLE
7087 && ecs
->stop_func_name
== NULL
7088 && stop_pc_sal
.line
== 0)
7090 infrun_debug_printf ("stepped into undebuggable function");
7092 /* The inferior just stepped into, or returned to, an
7093 undebuggable function (where there is no debugging information
7094 and no line number corresponding to the address where the
7095 inferior stopped). Since we want to skip this kind of code,
7096 we keep going until the inferior returns from this
7097 function - unless the user has asked us not to (via
7098 set step-mode) or we no longer know how to get back
7099 to the call site. */
7100 if (step_stop_if_no_debug
7101 || !frame_id_p (frame_unwind_caller_id (frame
)))
7103 /* If we have no line number and the step-stop-if-no-debug
7104 is set, we stop the step so that the user has a chance to
7105 switch in assembly mode. */
7106 end_stepping_range (ecs
);
7111 /* Set a breakpoint at callee's return address (the address
7112 at which the caller will resume). */
7113 insert_step_resume_breakpoint_at_caller (frame
);
7119 if (ecs
->event_thread
->control
.step_range_end
== 1)
7121 /* It is stepi or nexti. We always want to stop stepping after
7123 infrun_debug_printf ("stepi/nexti");
7124 end_stepping_range (ecs
);
7128 if (stop_pc_sal
.line
== 0)
7130 /* We have no line number information. That means to stop
7131 stepping (does this always happen right after one instruction,
7132 when we do "s" in a function with no line numbers,
7133 or can this happen as a result of a return or longjmp?). */
7134 infrun_debug_printf ("line number info");
7135 end_stepping_range (ecs
);
7139 /* Look for "calls" to inlined functions, part one. If the inline
7140 frame machinery detected some skipped call sites, we have entered
7141 a new inline function. */
7143 if (frame_id_eq (get_frame_id (get_current_frame ()),
7144 ecs
->event_thread
->control
.step_frame_id
)
7145 && inline_skipped_frames (ecs
->event_thread
))
7147 infrun_debug_printf ("stepped into inlined function");
7149 symtab_and_line call_sal
= find_frame_sal (get_current_frame ());
7151 if (ecs
->event_thread
->control
.step_over_calls
!= STEP_OVER_ALL
)
7153 /* For "step", we're going to stop. But if the call site
7154 for this inlined function is on the same source line as
7155 we were previously stepping, go down into the function
7156 first. Otherwise stop at the call site. */
7158 if (call_sal
.line
== ecs
->event_thread
->current_line
7159 && call_sal
.symtab
== ecs
->event_thread
->current_symtab
)
7161 step_into_inline_frame (ecs
->event_thread
);
7162 if (inline_frame_is_marked_for_skip (false, ecs
->event_thread
))
7169 end_stepping_range (ecs
);
7174 /* For "next", we should stop at the call site if it is on a
7175 different source line. Otherwise continue through the
7176 inlined function. */
7177 if (call_sal
.line
== ecs
->event_thread
->current_line
7178 && call_sal
.symtab
== ecs
->event_thread
->current_symtab
)
7181 end_stepping_range (ecs
);
7186 /* Look for "calls" to inlined functions, part two. If we are still
7187 in the same real function we were stepping through, but we have
7188 to go further up to find the exact frame ID, we are stepping
7189 through a more inlined call beyond its call site. */
7191 if (get_frame_type (get_current_frame ()) == INLINE_FRAME
7192 && !frame_id_eq (get_frame_id (get_current_frame ()),
7193 ecs
->event_thread
->control
.step_frame_id
)
7194 && stepped_in_from (get_current_frame (),
7195 ecs
->event_thread
->control
.step_frame_id
))
7197 infrun_debug_printf ("stepping through inlined function");
7199 if (ecs
->event_thread
->control
.step_over_calls
== STEP_OVER_ALL
7200 || inline_frame_is_marked_for_skip (false, ecs
->event_thread
))
7203 end_stepping_range (ecs
);
7207 bool refresh_step_info
= true;
7208 if ((ecs
->event_thread
->stop_pc () == stop_pc_sal
.pc
)
7209 && (ecs
->event_thread
->current_line
!= stop_pc_sal
.line
7210 || ecs
->event_thread
->current_symtab
!= stop_pc_sal
.symtab
))
7212 /* We are at a different line. */
7214 if (stop_pc_sal
.is_stmt
)
7216 /* We are at the start of a statement.
7218 So stop. Note that we don't stop if we step into the middle of a
7219 statement. That is said to make things like for (;;) statements
7221 infrun_debug_printf ("stepped to a different line");
7222 end_stepping_range (ecs
);
7225 else if (frame_id_eq (get_frame_id (get_current_frame ()),
7226 ecs
->event_thread
->control
.step_frame_id
))
7228 /* We are not at the start of a statement, and we have not changed
7231 We ignore this line table entry, and continue stepping forward,
7232 looking for a better place to stop. */
7233 refresh_step_info
= false;
7234 infrun_debug_printf ("stepped to a different line, but "
7235 "it's not the start of a statement");
7239 /* We are not the start of a statement, and we have changed frame.
7241 We ignore this line table entry, and continue stepping forward,
7242 looking for a better place to stop. Keep refresh_step_info at
7243 true to note that the frame has changed, but ignore the line
7244 number to make sure we don't ignore a subsequent entry with the
7245 same line number. */
7246 stop_pc_sal
.line
= 0;
7247 infrun_debug_printf ("stepped to a different frame, but "
7248 "it's not the start of a statement");
7252 /* We aren't done stepping.
7254 Optimize by setting the stepping range to the line.
7255 (We might not be in the original line, but if we entered a
7256 new line in mid-statement, we continue stepping. This makes
7257 things like for(;;) statements work better.)
7259 If we entered a SAL that indicates a non-statement line table entry,
7260 then we update the stepping range, but we don't update the step info,
7261 which includes things like the line number we are stepping away from.
7262 This means we will stop when we find a line table entry that is marked
7263 as is-statement, even if it matches the non-statement one we just
7266 ecs
->event_thread
->control
.step_range_start
= stop_pc_sal
.pc
;
7267 ecs
->event_thread
->control
.step_range_end
= stop_pc_sal
.end
;
7268 ecs
->event_thread
->control
.may_range_step
= 1;
7269 if (refresh_step_info
)
7270 set_step_info (ecs
->event_thread
, frame
, stop_pc_sal
);
7272 infrun_debug_printf ("keep going");
7276 static bool restart_stepped_thread (process_stratum_target
*resume_target
,
7277 ptid_t resume_ptid
);
7279 /* In all-stop mode, if we're currently stepping but have stopped in
7280 some other thread, we may need to switch back to the stepped
7281 thread. Returns true we set the inferior running, false if we left
7282 it stopped (and the event needs further processing). */
7285 switch_back_to_stepped_thread (struct execution_control_state
*ecs
)
7287 if (!target_is_non_stop_p ())
7289 /* If any thread is blocked on some internal breakpoint, and we
7290 simply need to step over that breakpoint to get it going
7291 again, do that first. */
7293 /* However, if we see an event for the stepping thread, then we
7294 know all other threads have been moved past their breakpoints
7295 already. Let the caller check whether the step is finished,
7296 etc., before deciding to move it past a breakpoint. */
7297 if (ecs
->event_thread
->control
.step_range_end
!= 0)
7300 /* Check if the current thread is blocked on an incomplete
7301 step-over, interrupted by a random signal. */
7302 if (ecs
->event_thread
->control
.trap_expected
7303 && ecs
->event_thread
->stop_signal () != GDB_SIGNAL_TRAP
)
7306 ("need to finish step-over of [%s]",
7307 ecs
->event_thread
->ptid
.to_string ().c_str ());
7312 /* Check if the current thread is blocked by a single-step
7313 breakpoint of another thread. */
7314 if (ecs
->hit_singlestep_breakpoint
)
7316 infrun_debug_printf ("need to step [%s] over single-step breakpoint",
7317 ecs
->ptid
.to_string ().c_str ());
7322 /* If this thread needs yet another step-over (e.g., stepping
7323 through a delay slot), do it first before moving on to
7325 if (thread_still_needs_step_over (ecs
->event_thread
))
7328 ("thread [%s] still needs step-over",
7329 ecs
->event_thread
->ptid
.to_string ().c_str ());
7334 /* If scheduler locking applies even if not stepping, there's no
7335 need to walk over threads. Above we've checked whether the
7336 current thread is stepping. If some other thread not the
7337 event thread is stepping, then it must be that scheduler
7338 locking is not in effect. */
7339 if (schedlock_applies (ecs
->event_thread
))
7342 /* Otherwise, we no longer expect a trap in the current thread.
7343 Clear the trap_expected flag before switching back -- this is
7344 what keep_going does as well, if we call it. */
7345 ecs
->event_thread
->control
.trap_expected
= 0;
7347 /* Likewise, clear the signal if it should not be passed. */
7348 if (!signal_program
[ecs
->event_thread
->stop_signal ()])
7349 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
7351 if (restart_stepped_thread (ecs
->target
, ecs
->ptid
))
7353 prepare_to_wait (ecs
);
7357 switch_to_thread (ecs
->event_thread
);
7363 /* Look for the thread that was stepping, and resume it.
7364 RESUME_TARGET / RESUME_PTID indicate the set of threads the caller
7365 is resuming. Return true if a thread was started, false
7369 restart_stepped_thread (process_stratum_target
*resume_target
,
7372 /* Do all pending step-overs before actually proceeding with
7374 if (start_step_over ())
7377 for (thread_info
*tp
: all_threads_safe ())
7379 if (tp
->state
== THREAD_EXITED
)
7382 if (tp
->has_pending_waitstatus ())
7385 /* Ignore threads of processes the caller is not
7388 && (tp
->inf
->process_target () != resume_target
7389 || tp
->inf
->pid
!= resume_ptid
.pid ()))
7392 if (tp
->control
.trap_expected
)
7394 infrun_debug_printf ("switching back to stepped thread (step-over)");
7396 if (keep_going_stepped_thread (tp
))
7401 for (thread_info
*tp
: all_threads_safe ())
7403 if (tp
->state
== THREAD_EXITED
)
7406 if (tp
->has_pending_waitstatus ())
7409 /* Ignore threads of processes the caller is not
7412 && (tp
->inf
->process_target () != resume_target
7413 || tp
->inf
->pid
!= resume_ptid
.pid ()))
7416 /* Did we find the stepping thread? */
7417 if (tp
->control
.step_range_end
)
7419 infrun_debug_printf ("switching back to stepped thread (stepping)");
7421 if (keep_going_stepped_thread (tp
))
7432 restart_after_all_stop_detach (process_stratum_target
*proc_target
)
7434 /* Note we don't check target_is_non_stop_p() here, because the
7435 current inferior may no longer have a process_stratum target
7436 pushed, as we just detached. */
7438 /* See if we have a THREAD_RUNNING thread that need to be
7439 re-resumed. If we have any thread that is already executing,
7440 then we don't need to resume the target -- it is already been
7441 resumed. With the remote target (in all-stop), it's even
7442 impossible to issue another resumption if the target is already
7443 resumed, until the target reports a stop. */
7444 for (thread_info
*thr
: all_threads (proc_target
))
7446 if (thr
->state
!= THREAD_RUNNING
)
7449 /* If we have any thread that is already executing, then we
7450 don't need to resume the target -- it is already been
7452 if (thr
->executing ())
7455 /* If we have a pending event to process, skip resuming the
7456 target and go straight to processing it. */
7457 if (thr
->resumed () && thr
->has_pending_waitstatus ())
7461 /* Alright, we need to re-resume the target. If a thread was
7462 stepping, we need to restart it stepping. */
7463 if (restart_stepped_thread (proc_target
, minus_one_ptid
))
7466 /* Otherwise, find the first THREAD_RUNNING thread and resume
7468 for (thread_info
*thr
: all_threads (proc_target
))
7470 if (thr
->state
!= THREAD_RUNNING
)
7473 execution_control_state ecs
;
7474 reset_ecs (&ecs
, thr
);
7475 switch_to_thread (thr
);
7481 /* Set a previously stepped thread back to stepping. Returns true on
7482 success, false if the resume is not possible (e.g., the thread
7486 keep_going_stepped_thread (struct thread_info
*tp
)
7488 struct frame_info
*frame
;
7489 struct execution_control_state ecss
;
7490 struct execution_control_state
*ecs
= &ecss
;
7492 /* If the stepping thread exited, then don't try to switch back and
7493 resume it, which could fail in several different ways depending
7494 on the target. Instead, just keep going.
7496 We can find a stepping dead thread in the thread list in two
7499 - The target supports thread exit events, and when the target
7500 tries to delete the thread from the thread list, inferior_ptid
7501 pointed at the exiting thread. In such case, calling
7502 delete_thread does not really remove the thread from the list;
7503 instead, the thread is left listed, with 'exited' state.
7505 - The target's debug interface does not support thread exit
7506 events, and so we have no idea whatsoever if the previously
7507 stepping thread is still alive. For that reason, we need to
7508 synchronously query the target now. */
7510 if (tp
->state
== THREAD_EXITED
|| !target_thread_alive (tp
->ptid
))
7512 infrun_debug_printf ("not resuming previously stepped thread, it has "
7519 infrun_debug_printf ("resuming previously stepped thread");
7521 reset_ecs (ecs
, tp
);
7522 switch_to_thread (tp
);
7524 tp
->set_stop_pc (regcache_read_pc (get_thread_regcache (tp
)));
7525 frame
= get_current_frame ();
7527 /* If the PC of the thread we were trying to single-step has
7528 changed, then that thread has trapped or been signaled, but the
7529 event has not been reported to GDB yet. Re-poll the target
7530 looking for this particular thread's event (i.e. temporarily
7531 enable schedlock) by:
7533 - setting a break at the current PC
7534 - resuming that particular thread, only (by setting trap
7537 This prevents us continuously moving the single-step breakpoint
7538 forward, one instruction at a time, overstepping. */
7540 if (tp
->stop_pc () != tp
->prev_pc
)
7544 infrun_debug_printf ("expected thread advanced also (%s -> %s)",
7545 paddress (target_gdbarch (), tp
->prev_pc
),
7546 paddress (target_gdbarch (), tp
->stop_pc ()));
7548 /* Clear the info of the previous step-over, as it's no longer
7549 valid (if the thread was trying to step over a breakpoint, it
7550 has already succeeded). It's what keep_going would do too,
7551 if we called it. Do this before trying to insert the sss
7552 breakpoint, otherwise if we were previously trying to step
7553 over this exact address in another thread, the breakpoint is
7555 clear_step_over_info ();
7556 tp
->control
.trap_expected
= 0;
7558 insert_single_step_breakpoint (get_frame_arch (frame
),
7559 get_frame_address_space (frame
),
7562 tp
->set_resumed (true);
7563 resume_ptid
= internal_resume_ptid (tp
->control
.stepping_command
);
7564 do_target_resume (resume_ptid
, false, GDB_SIGNAL_0
);
7568 infrun_debug_printf ("expected thread still hasn't advanced");
7570 keep_going_pass_signal (ecs
);
7576 /* Is thread TP in the middle of (software or hardware)
7577 single-stepping? (Note the result of this function must never be
7578 passed directly as target_resume's STEP parameter.) */
7581 currently_stepping (struct thread_info
*tp
)
7583 return ((tp
->control
.step_range_end
7584 && tp
->control
.step_resume_breakpoint
== NULL
)
7585 || tp
->control
.trap_expected
7586 || tp
->stepped_breakpoint
7587 || bpstat_should_step ());
7590 /* Inferior has stepped into a subroutine call with source code that
7591 we should not step over. Do step to the first line of code in
7595 handle_step_into_function (struct gdbarch
*gdbarch
,
7596 struct execution_control_state
*ecs
)
7598 fill_in_stop_func (gdbarch
, ecs
);
7600 compunit_symtab
*cust
7601 = find_pc_compunit_symtab (ecs
->event_thread
->stop_pc ());
7602 if (cust
!= NULL
&& compunit_language (cust
) != language_asm
)
7603 ecs
->stop_func_start
7604 = gdbarch_skip_prologue_noexcept (gdbarch
, ecs
->stop_func_start
);
7606 symtab_and_line stop_func_sal
= find_pc_line (ecs
->stop_func_start
, 0);
7607 /* Use the step_resume_break to step until the end of the prologue,
7608 even if that involves jumps (as it seems to on the vax under
7610 /* If the prologue ends in the middle of a source line, continue to
7611 the end of that source line (if it is still within the function).
7612 Otherwise, just go to end of prologue. */
7613 if (stop_func_sal
.end
7614 && stop_func_sal
.pc
!= ecs
->stop_func_start
7615 && stop_func_sal
.end
< ecs
->stop_func_end
)
7616 ecs
->stop_func_start
= stop_func_sal
.end
;
7618 /* Architectures which require breakpoint adjustment might not be able
7619 to place a breakpoint at the computed address. If so, the test
7620 ``ecs->stop_func_start == stop_pc'' will never succeed. Adjust
7621 ecs->stop_func_start to an address at which a breakpoint may be
7622 legitimately placed.
7624 Note: kevinb/2004-01-19: On FR-V, if this adjustment is not
7625 made, GDB will enter an infinite loop when stepping through
7626 optimized code consisting of VLIW instructions which contain
7627 subinstructions corresponding to different source lines. On
7628 FR-V, it's not permitted to place a breakpoint on any but the
7629 first subinstruction of a VLIW instruction. When a breakpoint is
7630 set, GDB will adjust the breakpoint address to the beginning of
7631 the VLIW instruction. Thus, we need to make the corresponding
7632 adjustment here when computing the stop address. */
7634 if (gdbarch_adjust_breakpoint_address_p (gdbarch
))
7636 ecs
->stop_func_start
7637 = gdbarch_adjust_breakpoint_address (gdbarch
,
7638 ecs
->stop_func_start
);
7641 if (ecs
->stop_func_start
== ecs
->event_thread
->stop_pc ())
7643 /* We are already there: stop now. */
7644 end_stepping_range (ecs
);
7649 /* Put the step-breakpoint there and go until there. */
7650 symtab_and_line sr_sal
;
7651 sr_sal
.pc
= ecs
->stop_func_start
;
7652 sr_sal
.section
= find_pc_overlay (ecs
->stop_func_start
);
7653 sr_sal
.pspace
= get_frame_program_space (get_current_frame ());
7655 /* Do not specify what the fp should be when we stop since on
7656 some machines the prologue is where the new fp value is
7658 insert_step_resume_breakpoint_at_sal (gdbarch
, sr_sal
, null_frame_id
);
7660 /* And make sure stepping stops right away then. */
7661 ecs
->event_thread
->control
.step_range_end
7662 = ecs
->event_thread
->control
.step_range_start
;
7667 /* Inferior has stepped backward into a subroutine call with source
7668 code that we should not step over. Do step to the beginning of the
7669 last line of code in it. */
7672 handle_step_into_function_backward (struct gdbarch
*gdbarch
,
7673 struct execution_control_state
*ecs
)
7675 struct compunit_symtab
*cust
;
7676 struct symtab_and_line stop_func_sal
;
7678 fill_in_stop_func (gdbarch
, ecs
);
7680 cust
= find_pc_compunit_symtab (ecs
->event_thread
->stop_pc ());
7681 if (cust
!= NULL
&& compunit_language (cust
) != language_asm
)
7682 ecs
->stop_func_start
7683 = gdbarch_skip_prologue_noexcept (gdbarch
, ecs
->stop_func_start
);
7685 stop_func_sal
= find_pc_line (ecs
->event_thread
->stop_pc (), 0);
7687 /* OK, we're just going to keep stepping here. */
7688 if (stop_func_sal
.pc
== ecs
->event_thread
->stop_pc ())
7690 /* We're there already. Just stop stepping now. */
7691 end_stepping_range (ecs
);
7695 /* Else just reset the step range and keep going.
7696 No step-resume breakpoint, they don't work for
7697 epilogues, which can have multiple entry paths. */
7698 ecs
->event_thread
->control
.step_range_start
= stop_func_sal
.pc
;
7699 ecs
->event_thread
->control
.step_range_end
= stop_func_sal
.end
;
7705 /* Insert a "step-resume breakpoint" at SR_SAL with frame ID SR_ID.
7706 This is used to both functions and to skip over code. */
7709 insert_step_resume_breakpoint_at_sal_1 (struct gdbarch
*gdbarch
,
7710 struct symtab_and_line sr_sal
,
7711 struct frame_id sr_id
,
7712 enum bptype sr_type
)
7714 /* There should never be more than one step-resume or longjmp-resume
7715 breakpoint per thread, so we should never be setting a new
7716 step_resume_breakpoint when one is already active. */
7717 gdb_assert (inferior_thread ()->control
.step_resume_breakpoint
== NULL
);
7718 gdb_assert (sr_type
== bp_step_resume
|| sr_type
== bp_hp_step_resume
);
7720 infrun_debug_printf ("inserting step-resume breakpoint at %s",
7721 paddress (gdbarch
, sr_sal
.pc
));
7723 inferior_thread ()->control
.step_resume_breakpoint
7724 = set_momentary_breakpoint (gdbarch
, sr_sal
, sr_id
, sr_type
).release ();
7728 insert_step_resume_breakpoint_at_sal (struct gdbarch
*gdbarch
,
7729 struct symtab_and_line sr_sal
,
7730 struct frame_id sr_id
)
7732 insert_step_resume_breakpoint_at_sal_1 (gdbarch
,
7737 /* Insert a "high-priority step-resume breakpoint" at RETURN_FRAME.pc.
7738 This is used to skip a potential signal handler.
7740 This is called with the interrupted function's frame. The signal
7741 handler, when it returns, will resume the interrupted function at
7745 insert_hp_step_resume_breakpoint_at_frame (struct frame_info
*return_frame
)
7747 gdb_assert (return_frame
!= NULL
);
7749 struct gdbarch
*gdbarch
= get_frame_arch (return_frame
);
7751 symtab_and_line sr_sal
;
7752 sr_sal
.pc
= gdbarch_addr_bits_remove (gdbarch
, get_frame_pc (return_frame
));
7753 sr_sal
.section
= find_pc_overlay (sr_sal
.pc
);
7754 sr_sal
.pspace
= get_frame_program_space (return_frame
);
7756 insert_step_resume_breakpoint_at_sal_1 (gdbarch
, sr_sal
,
7757 get_stack_frame_id (return_frame
),
7761 /* Insert a "step-resume breakpoint" at the previous frame's PC. This
7762 is used to skip a function after stepping into it (for "next" or if
7763 the called function has no debugging information).
7765 The current function has almost always been reached by single
7766 stepping a call or return instruction. NEXT_FRAME belongs to the
7767 current function, and the breakpoint will be set at the caller's
7770 This is a separate function rather than reusing
7771 insert_hp_step_resume_breakpoint_at_frame in order to avoid
7772 get_prev_frame, which may stop prematurely (see the implementation
7773 of frame_unwind_caller_id for an example). */
7776 insert_step_resume_breakpoint_at_caller (struct frame_info
*next_frame
)
7778 /* We shouldn't have gotten here if we don't know where the call site
7780 gdb_assert (frame_id_p (frame_unwind_caller_id (next_frame
)));
7782 struct gdbarch
*gdbarch
= frame_unwind_caller_arch (next_frame
);
7784 symtab_and_line sr_sal
;
7785 sr_sal
.pc
= gdbarch_addr_bits_remove (gdbarch
,
7786 frame_unwind_caller_pc (next_frame
));
7787 sr_sal
.section
= find_pc_overlay (sr_sal
.pc
);
7788 sr_sal
.pspace
= frame_unwind_program_space (next_frame
);
7790 insert_step_resume_breakpoint_at_sal (gdbarch
, sr_sal
,
7791 frame_unwind_caller_id (next_frame
));
7794 /* Insert a "longjmp-resume" breakpoint at PC. This is used to set a
7795 new breakpoint at the target of a jmp_buf. The handling of
7796 longjmp-resume uses the same mechanisms used for handling
7797 "step-resume" breakpoints. */
7800 insert_longjmp_resume_breakpoint (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
7802 /* There should never be more than one longjmp-resume breakpoint per
7803 thread, so we should never be setting a new
7804 longjmp_resume_breakpoint when one is already active. */
7805 gdb_assert (inferior_thread ()->control
.exception_resume_breakpoint
== NULL
);
7807 infrun_debug_printf ("inserting longjmp-resume breakpoint at %s",
7808 paddress (gdbarch
, pc
));
7810 inferior_thread ()->control
.exception_resume_breakpoint
=
7811 set_momentary_breakpoint_at_pc (gdbarch
, pc
, bp_longjmp_resume
).release ();
7814 /* Insert an exception resume breakpoint. TP is the thread throwing
7815 the exception. The block B is the block of the unwinder debug hook
7816 function. FRAME is the frame corresponding to the call to this
7817 function. SYM is the symbol of the function argument holding the
7818 target PC of the exception. */
7821 insert_exception_resume_breakpoint (struct thread_info
*tp
,
7822 const struct block
*b
,
7823 struct frame_info
*frame
,
7828 struct block_symbol vsym
;
7829 struct value
*value
;
7831 struct breakpoint
*bp
;
7833 vsym
= lookup_symbol_search_name (sym
->search_name (),
7835 value
= read_var_value (vsym
.symbol
, vsym
.block
, frame
);
7836 /* If the value was optimized out, revert to the old behavior. */
7837 if (! value_optimized_out (value
))
7839 handler
= value_as_address (value
);
7841 infrun_debug_printf ("exception resume at %lx",
7842 (unsigned long) handler
);
7844 bp
= set_momentary_breakpoint_at_pc (get_frame_arch (frame
),
7846 bp_exception_resume
).release ();
7848 /* set_momentary_breakpoint_at_pc invalidates FRAME. */
7851 bp
->thread
= tp
->global_num
;
7852 inferior_thread ()->control
.exception_resume_breakpoint
= bp
;
7855 catch (const gdb_exception_error
&e
)
7857 /* We want to ignore errors here. */
7861 /* A helper for check_exception_resume that sets an
7862 exception-breakpoint based on a SystemTap probe. */
7865 insert_exception_resume_from_probe (struct thread_info
*tp
,
7866 const struct bound_probe
*probe
,
7867 struct frame_info
*frame
)
7869 struct value
*arg_value
;
7871 struct breakpoint
*bp
;
7873 arg_value
= probe_safe_evaluate_at_pc (frame
, 1);
7877 handler
= value_as_address (arg_value
);
7879 infrun_debug_printf ("exception resume at %s",
7880 paddress (probe
->objfile
->arch (), handler
));
7882 bp
= set_momentary_breakpoint_at_pc (get_frame_arch (frame
),
7883 handler
, bp_exception_resume
).release ();
7884 bp
->thread
= tp
->global_num
;
7885 inferior_thread ()->control
.exception_resume_breakpoint
= bp
;
7888 /* This is called when an exception has been intercepted. Check to
7889 see whether the exception's destination is of interest, and if so,
7890 set an exception resume breakpoint there. */
7893 check_exception_resume (struct execution_control_state
*ecs
,
7894 struct frame_info
*frame
)
7896 struct bound_probe probe
;
7897 struct symbol
*func
;
7899 /* First see if this exception unwinding breakpoint was set via a
7900 SystemTap probe point. If so, the probe has two arguments: the
7901 CFA and the HANDLER. We ignore the CFA, extract the handler, and
7902 set a breakpoint there. */
7903 probe
= find_probe_by_pc (get_frame_pc (frame
));
7906 insert_exception_resume_from_probe (ecs
->event_thread
, &probe
, frame
);
7910 func
= get_frame_function (frame
);
7916 const struct block
*b
;
7917 struct block_iterator iter
;
7921 /* The exception breakpoint is a thread-specific breakpoint on
7922 the unwinder's debug hook, declared as:
7924 void _Unwind_DebugHook (void *cfa, void *handler);
7926 The CFA argument indicates the frame to which control is
7927 about to be transferred. HANDLER is the destination PC.
7929 We ignore the CFA and set a temporary breakpoint at HANDLER.
7930 This is not extremely efficient but it avoids issues in gdb
7931 with computing the DWARF CFA, and it also works even in weird
7932 cases such as throwing an exception from inside a signal
7935 b
= SYMBOL_BLOCK_VALUE (func
);
7936 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
7938 if (!SYMBOL_IS_ARGUMENT (sym
))
7945 insert_exception_resume_breakpoint (ecs
->event_thread
,
7951 catch (const gdb_exception_error
&e
)
7957 stop_waiting (struct execution_control_state
*ecs
)
7959 infrun_debug_printf ("stop_waiting");
7961 /* Let callers know we don't want to wait for the inferior anymore. */
7962 ecs
->wait_some_more
= 0;
7964 /* If all-stop, but there exists a non-stop target, stop all
7965 threads now that we're presenting the stop to the user. */
7966 if (!non_stop
&& exists_non_stop_target ())
7967 stop_all_threads ();
7970 /* Like keep_going, but passes the signal to the inferior, even if the
7971 signal is set to nopass. */
7974 keep_going_pass_signal (struct execution_control_state
*ecs
)
7976 gdb_assert (ecs
->event_thread
->ptid
== inferior_ptid
);
7977 gdb_assert (!ecs
->event_thread
->resumed ());
7979 /* Save the pc before execution, to compare with pc after stop. */
7980 ecs
->event_thread
->prev_pc
7981 = regcache_read_pc_protected (get_thread_regcache (ecs
->event_thread
));
7983 if (ecs
->event_thread
->control
.trap_expected
)
7985 struct thread_info
*tp
= ecs
->event_thread
;
7987 infrun_debug_printf ("%s has trap_expected set, "
7988 "resuming to collect trap",
7989 tp
->ptid
.to_string ().c_str ());
7991 /* We haven't yet gotten our trap, and either: intercepted a
7992 non-signal event (e.g., a fork); or took a signal which we
7993 are supposed to pass through to the inferior. Simply
7995 resume (ecs
->event_thread
->stop_signal ());
7997 else if (step_over_info_valid_p ())
7999 /* Another thread is stepping over a breakpoint in-line. If
8000 this thread needs a step-over too, queue the request. In
8001 either case, this resume must be deferred for later. */
8002 struct thread_info
*tp
= ecs
->event_thread
;
8004 if (ecs
->hit_singlestep_breakpoint
8005 || thread_still_needs_step_over (tp
))
8007 infrun_debug_printf ("step-over already in progress: "
8008 "step-over for %s deferred",
8009 tp
->ptid
.to_string ().c_str ());
8010 global_thread_step_over_chain_enqueue (tp
);
8013 infrun_debug_printf ("step-over in progress: resume of %s deferred",
8014 tp
->ptid
.to_string ().c_str ());
8018 struct regcache
*regcache
= get_current_regcache ();
8021 step_over_what step_what
;
8023 /* Either the trap was not expected, but we are continuing
8024 anyway (if we got a signal, the user asked it be passed to
8027 We got our expected trap, but decided we should resume from
8030 We're going to run this baby now!
8032 Note that insert_breakpoints won't try to re-insert
8033 already inserted breakpoints. Therefore, we don't
8034 care if breakpoints were already inserted, or not. */
8036 /* If we need to step over a breakpoint, and we're not using
8037 displaced stepping to do so, insert all breakpoints
8038 (watchpoints, etc.) but the one we're stepping over, step one
8039 instruction, and then re-insert the breakpoint when that step
8042 step_what
= thread_still_needs_step_over (ecs
->event_thread
);
8044 remove_bp
= (ecs
->hit_singlestep_breakpoint
8045 || (step_what
& STEP_OVER_BREAKPOINT
));
8046 remove_wps
= (step_what
& STEP_OVER_WATCHPOINT
);
8048 /* We can't use displaced stepping if we need to step past a
8049 watchpoint. The instruction copied to the scratch pad would
8050 still trigger the watchpoint. */
8052 && (remove_wps
|| !use_displaced_stepping (ecs
->event_thread
)))
8054 set_step_over_info (regcache
->aspace (),
8055 regcache_read_pc (regcache
), remove_wps
,
8056 ecs
->event_thread
->global_num
);
8058 else if (remove_wps
)
8059 set_step_over_info (NULL
, 0, remove_wps
, -1);
8061 /* If we now need to do an in-line step-over, we need to stop
8062 all other threads. Note this must be done before
8063 insert_breakpoints below, because that removes the breakpoint
8064 we're about to step over, otherwise other threads could miss
8066 if (step_over_info_valid_p () && target_is_non_stop_p ())
8067 stop_all_threads ();
8069 /* Stop stepping if inserting breakpoints fails. */
8072 insert_breakpoints ();
8074 catch (const gdb_exception_error
&e
)
8076 exception_print (gdb_stderr
, e
);
8078 clear_step_over_info ();
8082 ecs
->event_thread
->control
.trap_expected
= (remove_bp
|| remove_wps
);
8084 resume (ecs
->event_thread
->stop_signal ());
8087 prepare_to_wait (ecs
);
8090 /* Called when we should continue running the inferior, because the
8091 current event doesn't cause a user visible stop. This does the
8092 resuming part; waiting for the next event is done elsewhere. */
8095 keep_going (struct execution_control_state
*ecs
)
8097 if (ecs
->event_thread
->control
.trap_expected
8098 && ecs
->event_thread
->stop_signal () == GDB_SIGNAL_TRAP
)
8099 ecs
->event_thread
->control
.trap_expected
= 0;
8101 if (!signal_program
[ecs
->event_thread
->stop_signal ()])
8102 ecs
->event_thread
->set_stop_signal (GDB_SIGNAL_0
);
8103 keep_going_pass_signal (ecs
);
8106 /* This function normally comes after a resume, before
8107 handle_inferior_event exits. It takes care of any last bits of
8108 housekeeping, and sets the all-important wait_some_more flag. */
8111 prepare_to_wait (struct execution_control_state
*ecs
)
8113 infrun_debug_printf ("prepare_to_wait");
8115 ecs
->wait_some_more
= 1;
8117 /* If the target can't async, emulate it by marking the infrun event
8118 handler such that as soon as we get back to the event-loop, we
8119 immediately end up in fetch_inferior_event again calling
8121 if (!target_can_async_p ())
8122 mark_infrun_async_event_handler ();
8125 /* We are done with the step range of a step/next/si/ni command.
8126 Called once for each n of a "step n" operation. */
8129 end_stepping_range (struct execution_control_state
*ecs
)
8131 ecs
->event_thread
->control
.stop_step
= 1;
8135 /* Several print_*_reason functions to print why the inferior has stopped.
8136 We always print something when the inferior exits, or receives a signal.
8137 The rest of the cases are dealt with later on in normal_stop and
8138 print_it_typical. Ideally there should be a call to one of these
8139 print_*_reason functions functions from handle_inferior_event each time
8140 stop_waiting is called.
8142 Note that we don't call these directly, instead we delegate that to
8143 the interpreters, through observers. Interpreters then call these
8144 with whatever uiout is right. */
8147 print_end_stepping_range_reason (struct ui_out
*uiout
)
8149 /* For CLI-like interpreters, print nothing. */
8151 if (uiout
->is_mi_like_p ())
8153 uiout
->field_string ("reason",
8154 async_reason_lookup (EXEC_ASYNC_END_STEPPING_RANGE
));
8159 print_signal_exited_reason (struct ui_out
*uiout
, enum gdb_signal siggnal
)
8161 annotate_signalled ();
8162 if (uiout
->is_mi_like_p ())
8164 ("reason", async_reason_lookup (EXEC_ASYNC_EXITED_SIGNALLED
));
8165 uiout
->text ("\nProgram terminated with signal ");
8166 annotate_signal_name ();
8167 uiout
->field_string ("signal-name",
8168 gdb_signal_to_name (siggnal
));
8169 annotate_signal_name_end ();
8171 annotate_signal_string ();
8172 uiout
->field_string ("signal-meaning",
8173 gdb_signal_to_string (siggnal
));
8174 annotate_signal_string_end ();
8175 uiout
->text (".\n");
8176 uiout
->text ("The program no longer exists.\n");
8180 print_exited_reason (struct ui_out
*uiout
, int exitstatus
)
8182 struct inferior
*inf
= current_inferior ();
8183 std::string pidstr
= target_pid_to_str (ptid_t (inf
->pid
));
8185 annotate_exited (exitstatus
);
8188 if (uiout
->is_mi_like_p ())
8189 uiout
->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXITED
));
8190 std::string exit_code_str
8191 = string_printf ("0%o", (unsigned int) exitstatus
);
8192 uiout
->message ("[Inferior %s (%s) exited with code %pF]\n",
8193 plongest (inf
->num
), pidstr
.c_str (),
8194 string_field ("exit-code", exit_code_str
.c_str ()));
8198 if (uiout
->is_mi_like_p ())
8200 ("reason", async_reason_lookup (EXEC_ASYNC_EXITED_NORMALLY
));
8201 uiout
->message ("[Inferior %s (%s) exited normally]\n",
8202 plongest (inf
->num
), pidstr
.c_str ());
8207 print_signal_received_reason (struct ui_out
*uiout
, enum gdb_signal siggnal
)
8209 struct thread_info
*thr
= inferior_thread ();
8213 if (uiout
->is_mi_like_p ())
8215 else if (show_thread_that_caused_stop ())
8217 uiout
->text ("\nThread ");
8218 uiout
->field_string ("thread-id", print_thread_id (thr
));
8220 const char *name
= thread_name (thr
);
8223 uiout
->text (" \"");
8224 uiout
->field_string ("name", name
);
8229 uiout
->text ("\nProgram");
8231 if (siggnal
== GDB_SIGNAL_0
&& !uiout
->is_mi_like_p ())
8232 uiout
->text (" stopped");
8235 uiout
->text (" received signal ");
8236 annotate_signal_name ();
8237 if (uiout
->is_mi_like_p ())
8239 ("reason", async_reason_lookup (EXEC_ASYNC_SIGNAL_RECEIVED
));
8240 uiout
->field_string ("signal-name", gdb_signal_to_name (siggnal
));
8241 annotate_signal_name_end ();
8243 annotate_signal_string ();
8244 uiout
->field_string ("signal-meaning", gdb_signal_to_string (siggnal
));
8246 struct regcache
*regcache
= get_current_regcache ();
8247 struct gdbarch
*gdbarch
= regcache
->arch ();
8248 if (gdbarch_report_signal_info_p (gdbarch
))
8249 gdbarch_report_signal_info (gdbarch
, uiout
, siggnal
);
8251 annotate_signal_string_end ();
8253 uiout
->text (".\n");
8257 print_no_history_reason (struct ui_out
*uiout
)
8259 uiout
->text ("\nNo more reverse-execution history.\n");
8262 /* Print current location without a level number, if we have changed
8263 functions or hit a breakpoint. Print source line if we have one.
8264 bpstat_print contains the logic deciding in detail what to print,
8265 based on the event(s) that just occurred. */
8268 print_stop_location (const target_waitstatus
&ws
)
8271 enum print_what source_flag
;
8272 int do_frame_printing
= 1;
8273 struct thread_info
*tp
= inferior_thread ();
8275 bpstat_ret
= bpstat_print (tp
->control
.stop_bpstat
, ws
.kind ());
8279 /* FIXME: cagney/2002-12-01: Given that a frame ID does (or
8280 should) carry around the function and does (or should) use
8281 that when doing a frame comparison. */
8282 if (tp
->control
.stop_step
8283 && frame_id_eq (tp
->control
.step_frame_id
,
8284 get_frame_id (get_current_frame ()))
8285 && (tp
->control
.step_start_function
8286 == find_pc_function (tp
->stop_pc ())))
8288 /* Finished step, just print source line. */
8289 source_flag
= SRC_LINE
;
8293 /* Print location and source line. */
8294 source_flag
= SRC_AND_LOC
;
8297 case PRINT_SRC_AND_LOC
:
8298 /* Print location and source line. */
8299 source_flag
= SRC_AND_LOC
;
8301 case PRINT_SRC_ONLY
:
8302 source_flag
= SRC_LINE
;
8305 /* Something bogus. */
8306 source_flag
= SRC_LINE
;
8307 do_frame_printing
= 0;
8310 internal_error (__FILE__
, __LINE__
, _("Unknown value."));
8313 /* The behavior of this routine with respect to the source
8315 SRC_LINE: Print only source line
8316 LOCATION: Print only location
8317 SRC_AND_LOC: Print location and source line. */
8318 if (do_frame_printing
)
8319 print_stack_frame (get_selected_frame (NULL
), 0, source_flag
, 1);
8325 print_stop_event (struct ui_out
*uiout
, bool displays
)
8327 struct target_waitstatus last
;
8328 struct thread_info
*tp
;
8330 get_last_target_status (nullptr, nullptr, &last
);
8333 scoped_restore save_uiout
= make_scoped_restore (¤t_uiout
, uiout
);
8335 print_stop_location (last
);
8337 /* Display the auto-display expressions. */
8342 tp
= inferior_thread ();
8343 if (tp
->thread_fsm
!= NULL
8344 && tp
->thread_fsm
->finished_p ())
8346 struct return_value_info
*rv
;
8348 rv
= tp
->thread_fsm
->return_value ();
8350 print_return_value (uiout
, rv
);
8357 maybe_remove_breakpoints (void)
8359 if (!breakpoints_should_be_inserted_now () && target_has_execution ())
8361 if (remove_breakpoints ())
8363 target_terminal::ours_for_output ();
8364 printf_filtered (_("Cannot remove breakpoints because "
8365 "program is no longer writable.\nFurther "
8366 "execution is probably impossible.\n"));
8371 /* The execution context that just caused a normal stop. */
8377 DISABLE_COPY_AND_ASSIGN (stop_context
);
8379 bool changed () const;
8384 /* The event PTID. */
8388 /* If stopp for a thread event, this is the thread that caused the
8390 thread_info_ref thread
;
8392 /* The inferior that caused the stop. */
8396 /* Initializes a new stop context. If stopped for a thread event, this
8397 takes a strong reference to the thread. */
8399 stop_context::stop_context ()
8401 stop_id
= get_stop_id ();
8402 ptid
= inferior_ptid
;
8403 inf_num
= current_inferior ()->num
;
8405 if (inferior_ptid
!= null_ptid
)
8407 /* Take a strong reference so that the thread can't be deleted
8409 thread
= thread_info_ref::new_reference (inferior_thread ());
8413 /* Return true if the current context no longer matches the saved stop
8417 stop_context::changed () const
8419 if (ptid
!= inferior_ptid
)
8421 if (inf_num
!= current_inferior ()->num
)
8423 if (thread
!= NULL
&& thread
->state
!= THREAD_STOPPED
)
8425 if (get_stop_id () != stop_id
)
8435 struct target_waitstatus last
;
8437 get_last_target_status (nullptr, nullptr, &last
);
8441 /* If an exception is thrown from this point on, make sure to
8442 propagate GDB's knowledge of the executing state to the
8443 frontend/user running state. A QUIT is an easy exception to see
8444 here, so do this before any filtered output. */
8446 ptid_t finish_ptid
= null_ptid
;
8449 finish_ptid
= minus_one_ptid
;
8450 else if (last
.kind () == TARGET_WAITKIND_SIGNALLED
8451 || last
.kind () == TARGET_WAITKIND_EXITED
)
8453 /* On some targets, we may still have live threads in the
8454 inferior when we get a process exit event. E.g., for
8455 "checkpoint", when the current checkpoint/fork exits,
8456 linux-fork.c automatically switches to another fork from
8457 within target_mourn_inferior. */
8458 if (inferior_ptid
!= null_ptid
)
8459 finish_ptid
= ptid_t (inferior_ptid
.pid ());
8461 else if (last
.kind () != TARGET_WAITKIND_NO_RESUMED
)
8462 finish_ptid
= inferior_ptid
;
8464 gdb::optional
<scoped_finish_thread_state
> maybe_finish_thread_state
;
8465 if (finish_ptid
!= null_ptid
)
8467 maybe_finish_thread_state
.emplace
8468 (user_visible_resume_target (finish_ptid
), finish_ptid
);
8471 /* As we're presenting a stop, and potentially removing breakpoints,
8472 update the thread list so we can tell whether there are threads
8473 running on the target. With target remote, for example, we can
8474 only learn about new threads when we explicitly update the thread
8475 list. Do this before notifying the interpreters about signal
8476 stops, end of stepping ranges, etc., so that the "new thread"
8477 output is emitted before e.g., "Program received signal FOO",
8478 instead of after. */
8479 update_thread_list ();
8481 if (last
.kind () == TARGET_WAITKIND_STOPPED
&& stopped_by_random_signal
)
8482 gdb::observers::signal_received
.notify (inferior_thread ()->stop_signal ());
8484 /* As with the notification of thread events, we want to delay
8485 notifying the user that we've switched thread context until
8486 the inferior actually stops.
8488 There's no point in saying anything if the inferior has exited.
8489 Note that SIGNALLED here means "exited with a signal", not
8490 "received a signal".
8492 Also skip saying anything in non-stop mode. In that mode, as we
8493 don't want GDB to switch threads behind the user's back, to avoid
8494 races where the user is typing a command to apply to thread x,
8495 but GDB switches to thread y before the user finishes entering
8496 the command, fetch_inferior_event installs a cleanup to restore
8497 the current thread back to the thread the user had selected right
8498 after this event is handled, so we're not really switching, only
8499 informing of a stop. */
8501 && previous_inferior_ptid
!= inferior_ptid
8502 && target_has_execution ()
8503 && last
.kind () != TARGET_WAITKIND_SIGNALLED
8504 && last
.kind () != TARGET_WAITKIND_EXITED
8505 && last
.kind () != TARGET_WAITKIND_NO_RESUMED
)
8507 SWITCH_THRU_ALL_UIS ()
8509 target_terminal::ours_for_output ();
8510 printf_filtered (_("[Switching to %s]\n"),
8511 target_pid_to_str (inferior_ptid
).c_str ());
8512 annotate_thread_changed ();
8514 previous_inferior_ptid
= inferior_ptid
;
8517 if (last
.kind () == TARGET_WAITKIND_NO_RESUMED
)
8519 SWITCH_THRU_ALL_UIS ()
8520 if (current_ui
->prompt_state
== PROMPT_BLOCKED
)
8522 target_terminal::ours_for_output ();
8523 printf_filtered (_("No unwaited-for children left.\n"));
8527 /* Note: this depends on the update_thread_list call above. */
8528 maybe_remove_breakpoints ();
8530 /* If an auto-display called a function and that got a signal,
8531 delete that auto-display to avoid an infinite recursion. */
8533 if (stopped_by_random_signal
)
8534 disable_current_display ();
8536 SWITCH_THRU_ALL_UIS ()
8538 async_enable_stdin ();
8541 /* Let the user/frontend see the threads as stopped. */
8542 maybe_finish_thread_state
.reset ();
8544 /* Select innermost stack frame - i.e., current frame is frame 0,
8545 and current location is based on that. Handle the case where the
8546 dummy call is returning after being stopped. E.g. the dummy call
8547 previously hit a breakpoint. (If the dummy call returns
8548 normally, we won't reach here.) Do this before the stop hook is
8549 run, so that it doesn't get to see the temporary dummy frame,
8550 which is not where we'll present the stop. */
8551 if (has_stack_frames ())
8553 if (stop_stack_dummy
== STOP_STACK_DUMMY
)
8555 /* Pop the empty frame that contains the stack dummy. This
8556 also restores inferior state prior to the call (struct
8557 infcall_suspend_state). */
8558 struct frame_info
*frame
= get_current_frame ();
8560 gdb_assert (get_frame_type (frame
) == DUMMY_FRAME
);
8562 /* frame_pop calls reinit_frame_cache as the last thing it
8563 does which means there's now no selected frame. */
8566 select_frame (get_current_frame ());
8568 /* Set the current source location. */
8569 set_current_sal_from_frame (get_current_frame ());
8572 /* Look up the hook_stop and run it (CLI internally handles problem
8573 of stop_command's pre-hook not existing). */
8574 if (stop_command
!= NULL
)
8576 stop_context saved_context
;
8580 execute_cmd_pre_hook (stop_command
);
8582 catch (const gdb_exception
&ex
)
8584 exception_fprintf (gdb_stderr
, ex
,
8585 "Error while running hook_stop:\n");
8588 /* If the stop hook resumes the target, then there's no point in
8589 trying to notify about the previous stop; its context is
8590 gone. Likewise if the command switches thread or inferior --
8591 the observers would print a stop for the wrong
8593 if (saved_context
.changed ())
8597 /* Notify observers about the stop. This is where the interpreters
8598 print the stop event. */
8599 if (inferior_ptid
!= null_ptid
)
8600 gdb::observers::normal_stop
.notify (inferior_thread ()->control
.stop_bpstat
,
8603 gdb::observers::normal_stop
.notify (NULL
, stop_print_frame
);
8605 annotate_stopped ();
8607 if (target_has_execution ())
8609 if (last
.kind () != TARGET_WAITKIND_SIGNALLED
8610 && last
.kind () != TARGET_WAITKIND_EXITED
8611 && last
.kind () != TARGET_WAITKIND_NO_RESUMED
)
8612 /* Delete the breakpoint we stopped at, if it wants to be deleted.
8613 Delete any breakpoint that is to be deleted at the next stop. */
8614 breakpoint_auto_delete (inferior_thread ()->control
.stop_bpstat
);
8617 /* Try to get rid of automatically added inferiors that are no
8618 longer needed. Keeping those around slows down things linearly.
8619 Note that this never removes the current inferior. */
8626 signal_stop_state (int signo
)
8628 return signal_stop
[signo
];
8632 signal_print_state (int signo
)
8634 return signal_print
[signo
];
8638 signal_pass_state (int signo
)
8640 return signal_program
[signo
];
8644 signal_cache_update (int signo
)
8648 for (signo
= 0; signo
< (int) GDB_SIGNAL_LAST
; signo
++)
8649 signal_cache_update (signo
);
8654 signal_pass
[signo
] = (signal_stop
[signo
] == 0
8655 && signal_print
[signo
] == 0
8656 && signal_program
[signo
] == 1
8657 && signal_catch
[signo
] == 0);
8661 signal_stop_update (int signo
, int state
)
8663 int ret
= signal_stop
[signo
];
8665 signal_stop
[signo
] = state
;
8666 signal_cache_update (signo
);
8671 signal_print_update (int signo
, int state
)
8673 int ret
= signal_print
[signo
];
8675 signal_print
[signo
] = state
;
8676 signal_cache_update (signo
);
8681 signal_pass_update (int signo
, int state
)
8683 int ret
= signal_program
[signo
];
8685 signal_program
[signo
] = state
;
8686 signal_cache_update (signo
);
8690 /* Update the global 'signal_catch' from INFO and notify the
8694 signal_catch_update (const unsigned int *info
)
8698 for (i
= 0; i
< GDB_SIGNAL_LAST
; ++i
)
8699 signal_catch
[i
] = info
[i
] > 0;
8700 signal_cache_update (-1);
8701 target_pass_signals (signal_pass
);
8705 sig_print_header (void)
8707 printf_filtered (_("Signal Stop\tPrint\tPass "
8708 "to program\tDescription\n"));
8712 sig_print_info (enum gdb_signal oursig
)
8714 const char *name
= gdb_signal_to_name (oursig
);
8715 int name_padding
= 13 - strlen (name
);
8717 if (name_padding
<= 0)
8720 printf_filtered ("%s", name
);
8721 printf_filtered ("%*.*s ", name_padding
, name_padding
, " ");
8722 printf_filtered ("%s\t", signal_stop
[oursig
] ? "Yes" : "No");
8723 printf_filtered ("%s\t", signal_print
[oursig
] ? "Yes" : "No");
8724 printf_filtered ("%s\t\t", signal_program
[oursig
] ? "Yes" : "No");
8725 printf_filtered ("%s\n", gdb_signal_to_string (oursig
));
8728 /* Specify how various signals in the inferior should be handled. */
8731 handle_command (const char *args
, int from_tty
)
8733 int digits
, wordlen
;
8734 int sigfirst
, siglast
;
8735 enum gdb_signal oursig
;
8740 error_no_arg (_("signal to handle"));
8743 /* Allocate and zero an array of flags for which signals to handle. */
8745 const size_t nsigs
= GDB_SIGNAL_LAST
;
8746 unsigned char sigs
[nsigs
] {};
8748 /* Break the command line up into args. */
8750 gdb_argv
built_argv (args
);
8752 /* Walk through the args, looking for signal oursigs, signal names, and
8753 actions. Signal numbers and signal names may be interspersed with
8754 actions, with the actions being performed for all signals cumulatively
8755 specified. Signal ranges can be specified as <LOW>-<HIGH>. */
8757 for (char *arg
: built_argv
)
8759 wordlen
= strlen (arg
);
8760 for (digits
= 0; isdigit (arg
[digits
]); digits
++)
8764 sigfirst
= siglast
= -1;
8766 if (wordlen
>= 1 && !strncmp (arg
, "all", wordlen
))
8768 /* Apply action to all signals except those used by the
8769 debugger. Silently skip those. */
8772 siglast
= nsigs
- 1;
8774 else if (wordlen
>= 1 && !strncmp (arg
, "stop", wordlen
))
8776 SET_SIGS (nsigs
, sigs
, signal_stop
);
8777 SET_SIGS (nsigs
, sigs
, signal_print
);
8779 else if (wordlen
>= 1 && !strncmp (arg
, "ignore", wordlen
))
8781 UNSET_SIGS (nsigs
, sigs
, signal_program
);
8783 else if (wordlen
>= 2 && !strncmp (arg
, "print", wordlen
))
8785 SET_SIGS (nsigs
, sigs
, signal_print
);
8787 else if (wordlen
>= 2 && !strncmp (arg
, "pass", wordlen
))
8789 SET_SIGS (nsigs
, sigs
, signal_program
);
8791 else if (wordlen
>= 3 && !strncmp (arg
, "nostop", wordlen
))
8793 UNSET_SIGS (nsigs
, sigs
, signal_stop
);
8795 else if (wordlen
>= 3 && !strncmp (arg
, "noignore", wordlen
))
8797 SET_SIGS (nsigs
, sigs
, signal_program
);
8799 else if (wordlen
>= 4 && !strncmp (arg
, "noprint", wordlen
))
8801 UNSET_SIGS (nsigs
, sigs
, signal_print
);
8802 UNSET_SIGS (nsigs
, sigs
, signal_stop
);
8804 else if (wordlen
>= 4 && !strncmp (arg
, "nopass", wordlen
))
8806 UNSET_SIGS (nsigs
, sigs
, signal_program
);
8808 else if (digits
> 0)
8810 /* It is numeric. The numeric signal refers to our own
8811 internal signal numbering from target.h, not to host/target
8812 signal number. This is a feature; users really should be
8813 using symbolic names anyway, and the common ones like
8814 SIGHUP, SIGINT, SIGALRM, etc. will work right anyway. */
8816 sigfirst
= siglast
= (int)
8817 gdb_signal_from_command (atoi (arg
));
8818 if (arg
[digits
] == '-')
8821 gdb_signal_from_command (atoi (arg
+ digits
+ 1));
8823 if (sigfirst
> siglast
)
8825 /* Bet he didn't figure we'd think of this case... */
8826 std::swap (sigfirst
, siglast
);
8831 oursig
= gdb_signal_from_name (arg
);
8832 if (oursig
!= GDB_SIGNAL_UNKNOWN
)
8834 sigfirst
= siglast
= (int) oursig
;
8838 /* Not a number and not a recognized flag word => complain. */
8839 error (_("Unrecognized or ambiguous flag word: \"%s\"."), arg
);
8843 /* If any signal numbers or symbol names were found, set flags for
8844 which signals to apply actions to. */
8846 for (int signum
= sigfirst
; signum
>= 0 && signum
<= siglast
; signum
++)
8848 switch ((enum gdb_signal
) signum
)
8850 case GDB_SIGNAL_TRAP
:
8851 case GDB_SIGNAL_INT
:
8852 if (!allsigs
&& !sigs
[signum
])
8854 if (query (_("%s is used by the debugger.\n\
8855 Are you sure you want to change it? "),
8856 gdb_signal_to_name ((enum gdb_signal
) signum
)))
8861 printf_unfiltered (_("Not confirmed, unchanged.\n"));
8865 case GDB_SIGNAL_DEFAULT
:
8866 case GDB_SIGNAL_UNKNOWN
:
8867 /* Make sure that "all" doesn't print these. */
8876 for (int signum
= 0; signum
< nsigs
; signum
++)
8879 signal_cache_update (-1);
8880 target_pass_signals (signal_pass
);
8881 target_program_signals (signal_program
);
8885 /* Show the results. */
8886 sig_print_header ();
8887 for (; signum
< nsigs
; signum
++)
8889 sig_print_info ((enum gdb_signal
) signum
);
8896 /* Complete the "handle" command. */
8899 handle_completer (struct cmd_list_element
*ignore
,
8900 completion_tracker
&tracker
,
8901 const char *text
, const char *word
)
8903 static const char * const keywords
[] =
8917 signal_completer (ignore
, tracker
, text
, word
);
8918 complete_on_enum (tracker
, keywords
, word
, word
);
8922 gdb_signal_from_command (int num
)
8924 if (num
>= 1 && num
<= 15)
8925 return (enum gdb_signal
) num
;
8926 error (_("Only signals 1-15 are valid as numeric signals.\n\
8927 Use \"info signals\" for a list of symbolic signals."));
8930 /* Print current contents of the tables set by the handle command.
8931 It is possible we should just be printing signals actually used
8932 by the current target (but for things to work right when switching
8933 targets, all signals should be in the signal tables). */
8936 info_signals_command (const char *signum_exp
, int from_tty
)
8938 enum gdb_signal oursig
;
8940 sig_print_header ();
8944 /* First see if this is a symbol name. */
8945 oursig
= gdb_signal_from_name (signum_exp
);
8946 if (oursig
== GDB_SIGNAL_UNKNOWN
)
8948 /* No, try numeric. */
8950 gdb_signal_from_command (parse_and_eval_long (signum_exp
));
8952 sig_print_info (oursig
);
8956 printf_filtered ("\n");
8957 /* These ugly casts brought to you by the native VAX compiler. */
8958 for (oursig
= GDB_SIGNAL_FIRST
;
8959 (int) oursig
< (int) GDB_SIGNAL_LAST
;
8960 oursig
= (enum gdb_signal
) ((int) oursig
+ 1))
8964 if (oursig
!= GDB_SIGNAL_UNKNOWN
8965 && oursig
!= GDB_SIGNAL_DEFAULT
&& oursig
!= GDB_SIGNAL_0
)
8966 sig_print_info (oursig
);
8969 printf_filtered (_("\nUse the \"handle\" command "
8970 "to change these tables.\n"));
8973 /* The $_siginfo convenience variable is a bit special. We don't know
8974 for sure the type of the value until we actually have a chance to
8975 fetch the data. The type can change depending on gdbarch, so it is
8976 also dependent on which thread you have selected.
8978 1. making $_siginfo be an internalvar that creates a new value on
8981 2. making the value of $_siginfo be an lval_computed value. */
8983 /* This function implements the lval_computed support for reading a
8987 siginfo_value_read (struct value
*v
)
8989 LONGEST transferred
;
8991 /* If we can access registers, so can we access $_siginfo. Likewise
8993 validate_registers_access ();
8996 target_read (current_inferior ()->top_target (),
8997 TARGET_OBJECT_SIGNAL_INFO
,
8999 value_contents_all_raw (v
).data (),
9001 TYPE_LENGTH (value_type (v
)));
9003 if (transferred
!= TYPE_LENGTH (value_type (v
)))
9004 error (_("Unable to read siginfo"));
9007 /* This function implements the lval_computed support for writing a
9011 siginfo_value_write (struct value
*v
, struct value
*fromval
)
9013 LONGEST transferred
;
9015 /* If we can access registers, so can we access $_siginfo. Likewise
9017 validate_registers_access ();
9019 transferred
= target_write (current_inferior ()->top_target (),
9020 TARGET_OBJECT_SIGNAL_INFO
,
9022 value_contents_all_raw (fromval
).data (),
9024 TYPE_LENGTH (value_type (fromval
)));
9026 if (transferred
!= TYPE_LENGTH (value_type (fromval
)))
9027 error (_("Unable to write siginfo"));
9030 static const struct lval_funcs siginfo_value_funcs
=
9036 /* Return a new value with the correct type for the siginfo object of
9037 the current thread using architecture GDBARCH. Return a void value
9038 if there's no object available. */
9040 static struct value
*
9041 siginfo_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
9044 if (target_has_stack ()
9045 && inferior_ptid
!= null_ptid
9046 && gdbarch_get_siginfo_type_p (gdbarch
))
9048 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9050 return allocate_computed_value (type
, &siginfo_value_funcs
, NULL
);
9053 return allocate_value (builtin_type (gdbarch
)->builtin_void
);
9057 /* infcall_suspend_state contains state about the program itself like its
9058 registers and any signal it received when it last stopped.
9059 This state must be restored regardless of how the inferior function call
9060 ends (either successfully, or after it hits a breakpoint or signal)
9061 if the program is to properly continue where it left off. */
9063 class infcall_suspend_state
9066 /* Capture state from GDBARCH, TP, and REGCACHE that must be restored
9067 once the inferior function call has finished. */
9068 infcall_suspend_state (struct gdbarch
*gdbarch
,
9069 const struct thread_info
*tp
,
9070 struct regcache
*regcache
)
9071 : m_registers (new readonly_detached_regcache (*regcache
))
9073 tp
->save_suspend_to (m_thread_suspend
);
9075 gdb::unique_xmalloc_ptr
<gdb_byte
> siginfo_data
;
9077 if (gdbarch_get_siginfo_type_p (gdbarch
))
9079 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9080 size_t len
= TYPE_LENGTH (type
);
9082 siginfo_data
.reset ((gdb_byte
*) xmalloc (len
));
9084 if (target_read (current_inferior ()->top_target (),
9085 TARGET_OBJECT_SIGNAL_INFO
, NULL
,
9086 siginfo_data
.get (), 0, len
) != len
)
9088 /* Errors ignored. */
9089 siginfo_data
.reset (nullptr);
9095 m_siginfo_gdbarch
= gdbarch
;
9096 m_siginfo_data
= std::move (siginfo_data
);
9100 /* Return a pointer to the stored register state. */
9102 readonly_detached_regcache
*registers () const
9104 return m_registers
.get ();
9107 /* Restores the stored state into GDBARCH, TP, and REGCACHE. */
9109 void restore (struct gdbarch
*gdbarch
,
9110 struct thread_info
*tp
,
9111 struct regcache
*regcache
) const
9113 tp
->restore_suspend_from (m_thread_suspend
);
9115 if (m_siginfo_gdbarch
== gdbarch
)
9117 struct type
*type
= gdbarch_get_siginfo_type (gdbarch
);
9119 /* Errors ignored. */
9120 target_write (current_inferior ()->top_target (),
9121 TARGET_OBJECT_SIGNAL_INFO
, NULL
,
9122 m_siginfo_data
.get (), 0, TYPE_LENGTH (type
));
9125 /* The inferior can be gone if the user types "print exit(0)"
9126 (and perhaps other times). */
9127 if (target_has_execution ())
9128 /* NB: The register write goes through to the target. */
9129 regcache
->restore (registers ());
9133 /* How the current thread stopped before the inferior function call was
9135 struct thread_suspend_state m_thread_suspend
;
9137 /* The registers before the inferior function call was executed. */
9138 std::unique_ptr
<readonly_detached_regcache
> m_registers
;
9140 /* Format of SIGINFO_DATA or NULL if it is not present. */
9141 struct gdbarch
*m_siginfo_gdbarch
= nullptr;
9143 /* The inferior format depends on SIGINFO_GDBARCH and it has a length of
9144 TYPE_LENGTH (gdbarch_get_siginfo_type ()). For different gdbarch the
9145 content would be invalid. */
9146 gdb::unique_xmalloc_ptr
<gdb_byte
> m_siginfo_data
;
9149 infcall_suspend_state_up
9150 save_infcall_suspend_state ()
9152 struct thread_info
*tp
= inferior_thread ();
9153 struct regcache
*regcache
= get_current_regcache ();
9154 struct gdbarch
*gdbarch
= regcache
->arch ();
9156 infcall_suspend_state_up inf_state
9157 (new struct infcall_suspend_state (gdbarch
, tp
, regcache
));
9159 /* Having saved the current state, adjust the thread state, discarding
9160 any stop signal information. The stop signal is not useful when
9161 starting an inferior function call, and run_inferior_call will not use
9162 the signal due to its `proceed' call with GDB_SIGNAL_0. */
9163 tp
->set_stop_signal (GDB_SIGNAL_0
);
9168 /* Restore inferior session state to INF_STATE. */
9171 restore_infcall_suspend_state (struct infcall_suspend_state
*inf_state
)
9173 struct thread_info
*tp
= inferior_thread ();
9174 struct regcache
*regcache
= get_current_regcache ();
9175 struct gdbarch
*gdbarch
= regcache
->arch ();
9177 inf_state
->restore (gdbarch
, tp
, regcache
);
9178 discard_infcall_suspend_state (inf_state
);
9182 discard_infcall_suspend_state (struct infcall_suspend_state
*inf_state
)
9187 readonly_detached_regcache
*
9188 get_infcall_suspend_state_regcache (struct infcall_suspend_state
*inf_state
)
9190 return inf_state
->registers ();
9193 /* infcall_control_state contains state regarding gdb's control of the
9194 inferior itself like stepping control. It also contains session state like
9195 the user's currently selected frame. */
9197 struct infcall_control_state
9199 struct thread_control_state thread_control
;
9200 struct inferior_control_state inferior_control
;
9203 enum stop_stack_kind stop_stack_dummy
= STOP_NONE
;
9204 int stopped_by_random_signal
= 0;
9206 /* ID and level of the selected frame when the inferior function
9208 struct frame_id selected_frame_id
{};
9209 int selected_frame_level
= -1;
9212 /* Save all of the information associated with the inferior<==>gdb
9215 infcall_control_state_up
9216 save_infcall_control_state ()
9218 infcall_control_state_up
inf_status (new struct infcall_control_state
);
9219 struct thread_info
*tp
= inferior_thread ();
9220 struct inferior
*inf
= current_inferior ();
9222 inf_status
->thread_control
= tp
->control
;
9223 inf_status
->inferior_control
= inf
->control
;
9225 tp
->control
.step_resume_breakpoint
= NULL
;
9226 tp
->control
.exception_resume_breakpoint
= NULL
;
9228 /* Save original bpstat chain to INF_STATUS; replace it in TP with copy of
9229 chain. If caller's caller is walking the chain, they'll be happier if we
9230 hand them back the original chain when restore_infcall_control_state is
9232 tp
->control
.stop_bpstat
= bpstat_copy (tp
->control
.stop_bpstat
);
9235 inf_status
->stop_stack_dummy
= stop_stack_dummy
;
9236 inf_status
->stopped_by_random_signal
= stopped_by_random_signal
;
9238 save_selected_frame (&inf_status
->selected_frame_id
,
9239 &inf_status
->selected_frame_level
);
9244 /* Restore inferior session state to INF_STATUS. */
9247 restore_infcall_control_state (struct infcall_control_state
*inf_status
)
9249 struct thread_info
*tp
= inferior_thread ();
9250 struct inferior
*inf
= current_inferior ();
9252 if (tp
->control
.step_resume_breakpoint
)
9253 tp
->control
.step_resume_breakpoint
->disposition
= disp_del_at_next_stop
;
9255 if (tp
->control
.exception_resume_breakpoint
)
9256 tp
->control
.exception_resume_breakpoint
->disposition
9257 = disp_del_at_next_stop
;
9259 /* Handle the bpstat_copy of the chain. */
9260 bpstat_clear (&tp
->control
.stop_bpstat
);
9262 tp
->control
= inf_status
->thread_control
;
9263 inf
->control
= inf_status
->inferior_control
;
9266 stop_stack_dummy
= inf_status
->stop_stack_dummy
;
9267 stopped_by_random_signal
= inf_status
->stopped_by_random_signal
;
9269 if (target_has_stack ())
9271 restore_selected_frame (inf_status
->selected_frame_id
,
9272 inf_status
->selected_frame_level
);
9279 discard_infcall_control_state (struct infcall_control_state
*inf_status
)
9281 if (inf_status
->thread_control
.step_resume_breakpoint
)
9282 inf_status
->thread_control
.step_resume_breakpoint
->disposition
9283 = disp_del_at_next_stop
;
9285 if (inf_status
->thread_control
.exception_resume_breakpoint
)
9286 inf_status
->thread_control
.exception_resume_breakpoint
->disposition
9287 = disp_del_at_next_stop
;
9289 /* See save_infcall_control_state for info on stop_bpstat. */
9290 bpstat_clear (&inf_status
->thread_control
.stop_bpstat
);
9298 clear_exit_convenience_vars (void)
9300 clear_internalvar (lookup_internalvar ("_exitsignal"));
9301 clear_internalvar (lookup_internalvar ("_exitcode"));
9305 /* User interface for reverse debugging:
9306 Set exec-direction / show exec-direction commands
9307 (returns error unless target implements to_set_exec_direction method). */
9309 enum exec_direction_kind execution_direction
= EXEC_FORWARD
;
9310 static const char exec_forward
[] = "forward";
9311 static const char exec_reverse
[] = "reverse";
9312 static const char *exec_direction
= exec_forward
;
9313 static const char *const exec_direction_names
[] = {
9320 set_exec_direction_func (const char *args
, int from_tty
,
9321 struct cmd_list_element
*cmd
)
9323 if (target_can_execute_reverse ())
9325 if (!strcmp (exec_direction
, exec_forward
))
9326 execution_direction
= EXEC_FORWARD
;
9327 else if (!strcmp (exec_direction
, exec_reverse
))
9328 execution_direction
= EXEC_REVERSE
;
9332 exec_direction
= exec_forward
;
9333 error (_("Target does not support this operation."));
9338 show_exec_direction_func (struct ui_file
*out
, int from_tty
,
9339 struct cmd_list_element
*cmd
, const char *value
)
9341 switch (execution_direction
) {
9343 fprintf_filtered (out
, _("Forward.\n"));
9346 fprintf_filtered (out
, _("Reverse.\n"));
9349 internal_error (__FILE__
, __LINE__
,
9350 _("bogus execution_direction value: %d"),
9351 (int) execution_direction
);
9356 show_schedule_multiple (struct ui_file
*file
, int from_tty
,
9357 struct cmd_list_element
*c
, const char *value
)
9359 fprintf_filtered (file
, _("Resuming the execution of threads "
9360 "of all processes is %s.\n"), value
);
9363 /* Implementation of `siginfo' variable. */
9365 static const struct internalvar_funcs siginfo_funcs
=
9372 /* Callback for infrun's target events source. This is marked when a
9373 thread has a pending status to process. */
9376 infrun_async_inferior_event_handler (gdb_client_data data
)
9378 clear_async_event_handler (infrun_async_inferior_event_token
);
9379 inferior_event_handler (INF_REG_EVENT
);
9386 /* Verify that when two threads with the same ptid exist (from two different
9387 targets) and one of them changes ptid, we only update inferior_ptid if
9388 it is appropriate. */
9391 infrun_thread_ptid_changed ()
9393 gdbarch
*arch
= current_inferior ()->gdbarch
;
9395 /* The thread which inferior_ptid represents changes ptid. */
9397 scoped_restore_current_pspace_and_thread restore
;
9399 scoped_mock_context
<test_target_ops
> target1 (arch
);
9400 scoped_mock_context
<test_target_ops
> target2 (arch
);
9402 ptid_t
old_ptid (111, 222);
9403 ptid_t
new_ptid (111, 333);
9405 target1
.mock_inferior
.pid
= old_ptid
.pid ();
9406 target1
.mock_thread
.ptid
= old_ptid
;
9407 target1
.mock_inferior
.ptid_thread_map
.clear ();
9408 target1
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target1
.mock_thread
;
9410 target2
.mock_inferior
.pid
= old_ptid
.pid ();
9411 target2
.mock_thread
.ptid
= old_ptid
;
9412 target2
.mock_inferior
.ptid_thread_map
.clear ();
9413 target2
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target2
.mock_thread
;
9415 auto restore_inferior_ptid
= make_scoped_restore (&inferior_ptid
, old_ptid
);
9416 set_current_inferior (&target1
.mock_inferior
);
9418 thread_change_ptid (&target1
.mock_target
, old_ptid
, new_ptid
);
9420 gdb_assert (inferior_ptid
== new_ptid
);
9423 /* A thread with the same ptid as inferior_ptid, but from another target,
9426 scoped_restore_current_pspace_and_thread restore
;
9428 scoped_mock_context
<test_target_ops
> target1 (arch
);
9429 scoped_mock_context
<test_target_ops
> target2 (arch
);
9431 ptid_t
old_ptid (111, 222);
9432 ptid_t
new_ptid (111, 333);
9434 target1
.mock_inferior
.pid
= old_ptid
.pid ();
9435 target1
.mock_thread
.ptid
= old_ptid
;
9436 target1
.mock_inferior
.ptid_thread_map
.clear ();
9437 target1
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target1
.mock_thread
;
9439 target2
.mock_inferior
.pid
= old_ptid
.pid ();
9440 target2
.mock_thread
.ptid
= old_ptid
;
9441 target2
.mock_inferior
.ptid_thread_map
.clear ();
9442 target2
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target2
.mock_thread
;
9444 auto restore_inferior_ptid
= make_scoped_restore (&inferior_ptid
, old_ptid
);
9445 set_current_inferior (&target2
.mock_inferior
);
9447 thread_change_ptid (&target1
.mock_target
, old_ptid
, new_ptid
);
9449 gdb_assert (inferior_ptid
== old_ptid
);
9453 } /* namespace selftests */
9455 #endif /* GDB_SELF_TEST */
9457 void _initialize_infrun ();
9459 _initialize_infrun ()
9461 struct cmd_list_element
*c
;
9463 /* Register extra event sources in the event loop. */
9464 infrun_async_inferior_event_token
9465 = create_async_event_handler (infrun_async_inferior_event_handler
, NULL
,
9468 cmd_list_element
*info_signals_cmd
9469 = add_info ("signals", info_signals_command
, _("\
9470 What debugger does when program gets various signals.\n\
9471 Specify a signal as argument to print info on that signal only."));
9472 add_info_alias ("handle", info_signals_cmd
, 0);
9474 c
= add_com ("handle", class_run
, handle_command
, _("\
9475 Specify how to handle signals.\n\
9476 Usage: handle SIGNAL [ACTIONS]\n\
9477 Args are signals and actions to apply to those signals.\n\
9478 If no actions are specified, the current settings for the specified signals\n\
9479 will be displayed instead.\n\
9481 Symbolic signals (e.g. SIGSEGV) are recommended but numeric signals\n\
9482 from 1-15 are allowed for compatibility with old versions of GDB.\n\
9483 Numeric ranges may be specified with the form LOW-HIGH (e.g. 1-5).\n\
9484 The special arg \"all\" is recognized to mean all signals except those\n\
9485 used by the debugger, typically SIGTRAP and SIGINT.\n\
9487 Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
9488 \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
9489 Stop means reenter debugger if this signal happens (implies print).\n\
9490 Print means print a message if this signal happens.\n\
9491 Pass means let program see this signal; otherwise program doesn't know.\n\
9492 Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
9493 Pass and Stop may be combined.\n\
9495 Multiple signals may be specified. Signal numbers and signal names\n\
9496 may be interspersed with actions, with the actions being performed for\n\
9497 all signals cumulatively specified."));
9498 set_cmd_completer (c
, handle_completer
);
9501 stop_command
= add_cmd ("stop", class_obscure
,
9502 not_just_help_class_command
, _("\
9503 There is no `stop' command, but you can set a hook on `stop'.\n\
9504 This allows you to set a list of commands to be run each time execution\n\
9505 of the program stops."), &cmdlist
);
9507 add_setshow_boolean_cmd
9508 ("infrun", class_maintenance
, &debug_infrun
,
9509 _("Set inferior debugging."),
9510 _("Show inferior debugging."),
9511 _("When non-zero, inferior specific debugging is enabled."),
9512 NULL
, show_debug_infrun
, &setdebuglist
, &showdebuglist
);
9514 add_setshow_boolean_cmd ("non-stop", no_class
,
9516 Set whether gdb controls the inferior in non-stop mode."), _("\
9517 Show whether gdb controls the inferior in non-stop mode."), _("\
9518 When debugging a multi-threaded program and this setting is\n\
9519 off (the default, also called all-stop mode), when one thread stops\n\
9520 (for a breakpoint, watchpoint, exception, or similar events), GDB stops\n\
9521 all other threads in the program while you interact with the thread of\n\
9522 interest. When you continue or step a thread, you can allow the other\n\
9523 threads to run, or have them remain stopped, but while you inspect any\n\
9524 thread's state, all threads stop.\n\
9526 In non-stop mode, when one thread stops, other threads can continue\n\
9527 to run freely. You'll be able to step each thread independently,\n\
9528 leave it stopped or free to run as needed."),
9534 for (size_t i
= 0; i
< GDB_SIGNAL_LAST
; i
++)
9537 signal_print
[i
] = 1;
9538 signal_program
[i
] = 1;
9539 signal_catch
[i
] = 0;
9542 /* Signals caused by debugger's own actions should not be given to
9543 the program afterwards.
9545 Do not deliver GDB_SIGNAL_TRAP by default, except when the user
9546 explicitly specifies that it should be delivered to the target
9547 program. Typically, that would occur when a user is debugging a
9548 target monitor on a simulator: the target monitor sets a
9549 breakpoint; the simulator encounters this breakpoint and halts
9550 the simulation handing control to GDB; GDB, noting that the stop
9551 address doesn't map to any known breakpoint, returns control back
9552 to the simulator; the simulator then delivers the hardware
9553 equivalent of a GDB_SIGNAL_TRAP to the program being
9555 signal_program
[GDB_SIGNAL_TRAP
] = 0;
9556 signal_program
[GDB_SIGNAL_INT
] = 0;
9558 /* Signals that are not errors should not normally enter the debugger. */
9559 signal_stop
[GDB_SIGNAL_ALRM
] = 0;
9560 signal_print
[GDB_SIGNAL_ALRM
] = 0;
9561 signal_stop
[GDB_SIGNAL_VTALRM
] = 0;
9562 signal_print
[GDB_SIGNAL_VTALRM
] = 0;
9563 signal_stop
[GDB_SIGNAL_PROF
] = 0;
9564 signal_print
[GDB_SIGNAL_PROF
] = 0;
9565 signal_stop
[GDB_SIGNAL_CHLD
] = 0;
9566 signal_print
[GDB_SIGNAL_CHLD
] = 0;
9567 signal_stop
[GDB_SIGNAL_IO
] = 0;
9568 signal_print
[GDB_SIGNAL_IO
] = 0;
9569 signal_stop
[GDB_SIGNAL_POLL
] = 0;
9570 signal_print
[GDB_SIGNAL_POLL
] = 0;
9571 signal_stop
[GDB_SIGNAL_URG
] = 0;
9572 signal_print
[GDB_SIGNAL_URG
] = 0;
9573 signal_stop
[GDB_SIGNAL_WINCH
] = 0;
9574 signal_print
[GDB_SIGNAL_WINCH
] = 0;
9575 signal_stop
[GDB_SIGNAL_PRIO
] = 0;
9576 signal_print
[GDB_SIGNAL_PRIO
] = 0;
9578 /* These signals are used internally by user-level thread
9579 implementations. (See signal(5) on Solaris.) Like the above
9580 signals, a healthy program receives and handles them as part of
9581 its normal operation. */
9582 signal_stop
[GDB_SIGNAL_LWP
] = 0;
9583 signal_print
[GDB_SIGNAL_LWP
] = 0;
9584 signal_stop
[GDB_SIGNAL_WAITING
] = 0;
9585 signal_print
[GDB_SIGNAL_WAITING
] = 0;
9586 signal_stop
[GDB_SIGNAL_CANCEL
] = 0;
9587 signal_print
[GDB_SIGNAL_CANCEL
] = 0;
9588 signal_stop
[GDB_SIGNAL_LIBRT
] = 0;
9589 signal_print
[GDB_SIGNAL_LIBRT
] = 0;
9591 /* Update cached state. */
9592 signal_cache_update (-1);
9594 add_setshow_zinteger_cmd ("stop-on-solib-events", class_support
,
9595 &stop_on_solib_events
, _("\
9596 Set stopping for shared library events."), _("\
9597 Show stopping for shared library events."), _("\
9598 If nonzero, gdb will give control to the user when the dynamic linker\n\
9599 notifies gdb of shared library events. The most common event of interest\n\
9600 to the user would be loading/unloading of a new library."),
9601 set_stop_on_solib_events
,
9602 show_stop_on_solib_events
,
9603 &setlist
, &showlist
);
9605 add_setshow_enum_cmd ("follow-fork-mode", class_run
,
9606 follow_fork_mode_kind_names
,
9607 &follow_fork_mode_string
, _("\
9608 Set debugger response to a program call of fork or vfork."), _("\
9609 Show debugger response to a program call of fork or vfork."), _("\
9610 A fork or vfork creates a new process. follow-fork-mode can be:\n\
9611 parent - the original process is debugged after a fork\n\
9612 child - the new process is debugged after a fork\n\
9613 The unfollowed process will continue to run.\n\
9614 By default, the debugger will follow the parent process."),
9616 show_follow_fork_mode_string
,
9617 &setlist
, &showlist
);
9619 add_setshow_enum_cmd ("follow-exec-mode", class_run
,
9620 follow_exec_mode_names
,
9621 &follow_exec_mode_string
, _("\
9622 Set debugger response to a program call of exec."), _("\
9623 Show debugger response to a program call of exec."), _("\
9624 An exec call replaces the program image of a process.\n\
9626 follow-exec-mode can be:\n\
9628 new - the debugger creates a new inferior and rebinds the process\n\
9629 to this new inferior. The program the process was running before\n\
9630 the exec call can be restarted afterwards by restarting the original\n\
9633 same - the debugger keeps the process bound to the same inferior.\n\
9634 The new executable image replaces the previous executable loaded in\n\
9635 the inferior. Restarting the inferior after the exec call restarts\n\
9636 the executable the process was running after the exec call.\n\
9638 By default, the debugger will use the same inferior."),
9640 show_follow_exec_mode_string
,
9641 &setlist
, &showlist
);
9643 add_setshow_enum_cmd ("scheduler-locking", class_run
,
9644 scheduler_enums
, &scheduler_mode
, _("\
9645 Set mode for locking scheduler during execution."), _("\
9646 Show mode for locking scheduler during execution."), _("\
9647 off == no locking (threads may preempt at any time)\n\
9648 on == full locking (no thread except the current thread may run)\n\
9649 This applies to both normal execution and replay mode.\n\
9650 step == scheduler locked during stepping commands (step, next, stepi, nexti).\n\
9651 In this mode, other threads may run during other commands.\n\
9652 This applies to both normal execution and replay mode.\n\
9653 replay == scheduler locked in replay mode and unlocked during normal execution."),
9654 set_schedlock_func
, /* traps on target vector */
9655 show_scheduler_mode
,
9656 &setlist
, &showlist
);
9658 add_setshow_boolean_cmd ("schedule-multiple", class_run
, &sched_multi
, _("\
9659 Set mode for resuming threads of all processes."), _("\
9660 Show mode for resuming threads of all processes."), _("\
9661 When on, execution commands (such as 'continue' or 'next') resume all\n\
9662 threads of all processes. When off (which is the default), execution\n\
9663 commands only resume the threads of the current process. The set of\n\
9664 threads that are resumed is further refined by the scheduler-locking\n\
9665 mode (see help set scheduler-locking)."),
9667 show_schedule_multiple
,
9668 &setlist
, &showlist
);
9670 add_setshow_boolean_cmd ("step-mode", class_run
, &step_stop_if_no_debug
, _("\
9671 Set mode of the step operation."), _("\
9672 Show mode of the step operation."), _("\
9673 When set, doing a step over a function without debug line information\n\
9674 will stop at the first instruction of that function. Otherwise, the\n\
9675 function is skipped and the step command stops at a different source line."),
9677 show_step_stop_if_no_debug
,
9678 &setlist
, &showlist
);
9680 add_setshow_auto_boolean_cmd ("displaced-stepping", class_run
,
9681 &can_use_displaced_stepping
, _("\
9682 Set debugger's willingness to use displaced stepping."), _("\
9683 Show debugger's willingness to use displaced stepping."), _("\
9684 If on, gdb will use displaced stepping to step over breakpoints if it is\n\
9685 supported by the target architecture. If off, gdb will not use displaced\n\
9686 stepping to step over breakpoints, even if such is supported by the target\n\
9687 architecture. If auto (which is the default), gdb will use displaced stepping\n\
9688 if the target architecture supports it and non-stop mode is active, but will not\n\
9689 use it in all-stop mode (see help set non-stop)."),
9691 show_can_use_displaced_stepping
,
9692 &setlist
, &showlist
);
9694 add_setshow_enum_cmd ("exec-direction", class_run
, exec_direction_names
,
9695 &exec_direction
, _("Set direction of execution.\n\
9696 Options are 'forward' or 'reverse'."),
9697 _("Show direction of execution (forward/reverse)."),
9698 _("Tells gdb whether to execute forward or backward."),
9699 set_exec_direction_func
, show_exec_direction_func
,
9700 &setlist
, &showlist
);
9702 /* Set/show detach-on-fork: user-settable mode. */
9704 add_setshow_boolean_cmd ("detach-on-fork", class_run
, &detach_fork
, _("\
9705 Set whether gdb will detach the child of a fork."), _("\
9706 Show whether gdb will detach the child of a fork."), _("\
9707 Tells gdb whether to detach the child of a fork."),
9708 NULL
, NULL
, &setlist
, &showlist
);
9710 /* Set/show disable address space randomization mode. */
9712 add_setshow_boolean_cmd ("disable-randomization", class_support
,
9713 &disable_randomization
, _("\
9714 Set disabling of debuggee's virtual address space randomization."), _("\
9715 Show disabling of debuggee's virtual address space randomization."), _("\
9716 When this mode is on (which is the default), randomization of the virtual\n\
9717 address space is disabled. Standalone programs run with the randomization\n\
9718 enabled by default on some platforms."),
9719 &set_disable_randomization
,
9720 &show_disable_randomization
,
9721 &setlist
, &showlist
);
9723 /* ptid initializations */
9724 inferior_ptid
= null_ptid
;
9725 target_last_wait_ptid
= minus_one_ptid
;
9727 gdb::observers::thread_ptid_changed
.attach (infrun_thread_ptid_changed
,
9729 gdb::observers::thread_stop_requested
.attach (infrun_thread_stop_requested
,
9731 gdb::observers::thread_exit
.attach (infrun_thread_thread_exit
, "infrun");
9732 gdb::observers::inferior_exit
.attach (infrun_inferior_exit
, "infrun");
9733 gdb::observers::inferior_execd
.attach (infrun_inferior_execd
, "infrun");
9735 /* Explicitly create without lookup, since that tries to create a
9736 value with a void typed value, and when we get here, gdbarch
9737 isn't initialized yet. At this point, we're quite sure there
9738 isn't another convenience variable of the same name. */
9739 create_internalvar_type_lazy ("_siginfo", &siginfo_funcs
, NULL
);
9741 add_setshow_boolean_cmd ("observer", no_class
,
9742 &observer_mode_1
, _("\
9743 Set whether gdb controls the inferior in observer mode."), _("\
9744 Show whether gdb controls the inferior in observer mode."), _("\
9745 In observer mode, GDB can get data from the inferior, but not\n\
9746 affect its execution. Registers and memory may not be changed,\n\
9747 breakpoints may not be set, and the program cannot be interrupted\n\
9755 selftests::register_test ("infrun_thread_ptid_changed",
9756 selftests::infrun_thread_ptid_changed
);