1 //===-- ThreadPlanStepThrough.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlanStepThrough.h"
11 #include "lldb/Breakpoint/Breakpoint.h"
12 #include "lldb/Target/CPPLanguageRuntime.h"
13 #include "lldb/Target/DynamicLoader.h"
14 #include "lldb/Target/ObjCLanguageRuntime.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/RegisterContext.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/Stream.h"
22 using namespace lldb_private;
24 //----------------------------------------------------------------------
25 // ThreadPlanStepThrough: If the current instruction is a trampoline, step
26 // through it If it is the beginning of the prologue of a function, step
27 // through that as well.
28 // FIXME: At present only handles DYLD trampolines.
29 //----------------------------------------------------------------------
31 ThreadPlanStepThrough::ThreadPlanStepThrough(Thread &thread,
34 : ThreadPlan(ThreadPlan::eKindStepThrough,
35 "Step through trampolines and prologues", thread,
36 eVoteNoOpinion, eVoteNoOpinion),
37 m_start_address(0), m_backstop_bkpt_id(LLDB_INVALID_BREAK_ID),
38 m_backstop_addr(LLDB_INVALID_ADDRESS), m_return_stack_id(m_stack_id),
39 m_stop_others(stop_others) {
40 LookForPlanToStepThroughFromCurrentPC();
42 // If we don't get a valid step through plan, don't bother to set up a
45 m_start_address = GetThread().GetRegisterContext()->GetPC(0);
47 // We are going to return back to the concrete frame 1, we might pass by
48 // some inlined code that we're in the middle of by doing this, but it's
49 // easier than trying to figure out where the inlined code might return to.
51 StackFrameSP return_frame_sp = m_thread.GetFrameWithStackID(m_stack_id);
53 if (return_frame_sp) {
54 m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(
55 m_thread.CalculateTarget().get());
56 Breakpoint *return_bp =
59 .CreateBreakpoint(m_backstop_addr, true, false)
62 if (return_bp != nullptr) {
63 if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
64 m_could_not_resolve_hw_bp = true;
65 return_bp->SetThreadID(m_thread.GetID());
66 m_backstop_bkpt_id = return_bp->GetID();
67 return_bp->SetBreakpointKind("step-through-backstop");
69 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
71 log->Printf("Setting backstop breakpoint %d at address: 0x%" PRIx64,
72 m_backstop_bkpt_id, m_backstop_addr);
78 ThreadPlanStepThrough::~ThreadPlanStepThrough() { ClearBackstopBreakpoint(); }
80 void ThreadPlanStepThrough::DidPush() {
82 PushPlan(m_sub_plan_sp);
85 void ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC() {
86 DynamicLoader *loader = m_thread.GetProcess()->GetDynamicLoader();
89 loader->GetStepThroughTrampolinePlan(m_thread, m_stop_others);
91 // If that didn't come up with anything, try the ObjC runtime plugin:
92 if (!m_sub_plan_sp.get()) {
93 ObjCLanguageRuntime *objc_runtime =
94 m_thread.GetProcess()->GetObjCLanguageRuntime();
97 objc_runtime->GetStepThroughTrampolinePlan(m_thread, m_stop_others);
99 CPPLanguageRuntime *cpp_runtime =
100 m_thread.GetProcess()->GetCPPLanguageRuntime();
102 // If the ObjC runtime did not provide us with a step though plan then if we
103 // have it check the C++ runtime for a step though plan.
104 if (!m_sub_plan_sp.get() && cpp_runtime)
106 cpp_runtime->GetStepThroughTrampolinePlan(m_thread, m_stop_others);
109 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
111 lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0);
114 m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
115 log->Printf("Found step through plan from 0x%" PRIx64 ": %s",
116 current_address, s.GetData());
118 log->Printf("Couldn't find step through plan from address 0x%" PRIx64 ".",
124 void ThreadPlanStepThrough::GetDescription(Stream *s,
125 lldb::DescriptionLevel level) {
126 if (level == lldb::eDescriptionLevelBrief)
127 s->Printf("Step through");
129 s->PutCString("Stepping through trampoline code from: ");
130 s->Address(m_start_address, sizeof(addr_t));
131 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
132 s->Printf(" with backstop breakpoint ID: %d at address: ",
134 s->Address(m_backstop_addr, sizeof(addr_t));
136 s->PutCString(" unable to set a backstop breakpoint.");
140 bool ThreadPlanStepThrough::ValidatePlan(Stream *error) {
141 if (m_could_not_resolve_hw_bp) {
144 "Could not create hardware breakpoint for thread plan.");
148 if (m_backstop_bkpt_id == LLDB_INVALID_BREAK_ID) {
150 error->PutCString("Could not create backstop breakpoint.");
154 if (!m_sub_plan_sp.get()) {
156 error->PutCString("Does not have a subplan.");
163 bool ThreadPlanStepThrough::DoPlanExplainsStop(Event *event_ptr) {
164 // If we have a sub-plan, it will have been asked first if we explain the
165 // stop, and we won't get asked. The only time we would be the one directly
166 // asked this question is if we hit our backstop breakpoint.
168 return HitOurBackstopBreakpoint();
171 bool ThreadPlanStepThrough::ShouldStop(Event *event_ptr) {
172 // If we've already marked ourselves done, then we're done...
173 if (IsPlanComplete())
176 // First, did we hit the backstop breakpoint?
177 if (HitOurBackstopBreakpoint()) {
178 SetPlanComplete(true);
182 // If we don't have a sub-plan, then we're also done (can't see how we would
183 // ever get here without a plan, but just in case.
185 if (!m_sub_plan_sp) {
190 // If the current sub plan is not done, we don't want to stop. Actually, we
191 // probably won't ever get here in this state, since we generally won't get
192 // asked any questions if out current sub-plan is not done...
193 if (!m_sub_plan_sp->IsPlanComplete())
196 // If our current sub plan failed, then let's just run to our backstop. If
197 // we can't do that then just stop.
198 if (!m_sub_plan_sp->PlanSucceeded()) {
199 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
200 m_sub_plan_sp.reset();
203 SetPlanComplete(false);
208 // Next see if there is a specific step through plan at our current pc (these
209 // might chain, for instance stepping through a dylib trampoline to the objc
210 // dispatch function...)
211 LookForPlanToStepThroughFromCurrentPC();
213 PushPlan(m_sub_plan_sp);
221 bool ThreadPlanStepThrough::StopOthers() { return m_stop_others; }
223 StateType ThreadPlanStepThrough::GetPlanRunState() { return eStateRunning; }
225 bool ThreadPlanStepThrough::DoWillResume(StateType resume_state,
230 bool ThreadPlanStepThrough::WillStop() { return true; }
232 void ThreadPlanStepThrough::ClearBackstopBreakpoint() {
233 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
234 m_thread.GetProcess()->GetTarget().RemoveBreakpointByID(m_backstop_bkpt_id);
235 m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
236 m_could_not_resolve_hw_bp = false;
240 bool ThreadPlanStepThrough::MischiefManaged() {
241 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
243 if (!IsPlanComplete()) {
247 log->Printf("Completed step through step plan.");
249 ClearBackstopBreakpoint();
250 ThreadPlan::MischiefManaged();
255 bool ThreadPlanStepThrough::HitOurBackstopBreakpoint() {
256 StopInfoSP stop_info_sp(m_thread.GetStopInfo());
257 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
258 break_id_t stop_value = (break_id_t)stop_info_sp->GetValue();
259 BreakpointSiteSP cur_site_sp =
260 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(stop_value);
262 cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) {
263 StackID cur_frame_zero_id =
264 m_thread.GetStackFrameAtIndex(0)->GetStackID();
266 if (cur_frame_zero_id == m_return_stack_id) {
267 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
269 log->PutCString("ThreadPlanStepThrough hit backstop breakpoint.");