1 //===-- ThreadPlanStepUntil.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 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/ThreadPlanStepUntil.h"
16 #include "lldb/Breakpoint/Breakpoint.h"
17 #include "lldb/Symbol/SymbolContextScope.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/RegisterContext.h"
20 #include "lldb/Target/StopInfo.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/Log.h"
25 using namespace lldb_private;
27 //----------------------------------------------------------------------
28 // ThreadPlanStepUntil: Run until we reach a given line number or step out of
30 //----------------------------------------------------------------------
32 ThreadPlanStepUntil::ThreadPlanStepUntil(Thread &thread,
33 lldb::addr_t *address_list,
34 size_t num_addresses, bool stop_others,
36 : ThreadPlan(ThreadPlan::eKindStepUntil, "Step until", thread,
37 eVoteNoOpinion, eVoteNoOpinion),
38 m_step_from_insn(LLDB_INVALID_ADDRESS),
39 m_return_bp_id(LLDB_INVALID_BREAK_ID),
40 m_return_addr(LLDB_INVALID_ADDRESS), m_stepped_out(false),
41 m_should_stop(false), m_ran_analyze(false), m_explains_stop(false),
42 m_until_points(), m_stop_others(stop_others) {
43 // Stash away our "until" addresses:
44 TargetSP target_sp(m_thread.CalculateTarget());
46 StackFrameSP frame_sp(m_thread.GetStackFrameAtIndex(frame_idx));
48 m_step_from_insn = frame_sp->GetStackID().GetPC();
49 lldb::user_id_t thread_id = m_thread.GetID();
51 // Find the return address and set a breakpoint there:
52 // FIXME - can we do this more securely if we know first_insn?
54 StackFrameSP return_frame_sp(m_thread.GetStackFrameAtIndex(frame_idx + 1));
55 if (return_frame_sp) {
56 // TODO: add inline functionality
57 m_return_addr = return_frame_sp->GetStackID().GetPC();
58 Breakpoint *return_bp =
59 target_sp->CreateBreakpoint(m_return_addr, true, false).get();
60 if (return_bp != nullptr) {
61 return_bp->SetThreadID(thread_id);
62 m_return_bp_id = return_bp->GetID();
63 return_bp->SetBreakpointKind("until-return-backstop");
67 m_stack_id = frame_sp->GetStackID();
69 // Now set breakpoints on all our return addresses:
70 for (size_t i = 0; i < num_addresses; i++) {
71 Breakpoint *until_bp =
72 target_sp->CreateBreakpoint(address_list[i], true, false).get();
73 if (until_bp != nullptr) {
74 until_bp->SetThreadID(thread_id);
75 m_until_points[address_list[i]] = until_bp->GetID();
76 until_bp->SetBreakpointKind("until-target");
78 m_until_points[address_list[i]] = LLDB_INVALID_BREAK_ID;
84 ThreadPlanStepUntil::~ThreadPlanStepUntil() { Clear(); }
86 void ThreadPlanStepUntil::Clear() {
87 TargetSP target_sp(m_thread.CalculateTarget());
89 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
90 target_sp->RemoveBreakpointByID(m_return_bp_id);
91 m_return_bp_id = LLDB_INVALID_BREAK_ID;
94 until_collection::iterator pos, end = m_until_points.end();
95 for (pos = m_until_points.begin(); pos != end; pos++) {
96 target_sp->RemoveBreakpointByID((*pos).second);
99 m_until_points.clear();
102 void ThreadPlanStepUntil::GetDescription(Stream *s,
103 lldb::DescriptionLevel level) {
104 if (level == lldb::eDescriptionLevelBrief) {
105 s->Printf("step until");
107 s->Printf(" - stepped out");
109 if (m_until_points.size() == 1)
110 s->Printf("Stepping from address 0x%" PRIx64 " until we reach 0x%" PRIx64
111 " using breakpoint %d",
112 (uint64_t)m_step_from_insn,
113 (uint64_t)(*m_until_points.begin()).first,
114 (*m_until_points.begin()).second);
116 until_collection::iterator pos, end = m_until_points.end();
117 s->Printf("Stepping from address 0x%" PRIx64 " until we reach one of:",
118 (uint64_t)m_step_from_insn);
119 for (pos = m_until_points.begin(); pos != end; pos++) {
120 s->Printf("\n\t0x%" PRIx64 " (bp: %d)", (uint64_t)(*pos).first,
124 s->Printf(" stepped out address is 0x%" PRIx64 ".",
125 (uint64_t)m_return_addr);
129 bool ThreadPlanStepUntil::ValidatePlan(Stream *error) {
130 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
133 until_collection::iterator pos, end = m_until_points.end();
134 for (pos = m_until_points.begin(); pos != end; pos++) {
135 if (!LLDB_BREAK_ID_IS_VALID((*pos).second))
142 void ThreadPlanStepUntil::AnalyzeStop() {
146 StopInfoSP stop_info_sp = GetPrivateStopInfo();
147 m_should_stop = true;
148 m_explains_stop = false;
151 StopReason reason = stop_info_sp->GetStopReason();
153 if (reason == eStopReasonBreakpoint) {
154 // If this is OUR breakpoint, we're fine, otherwise we don't know why
156 BreakpointSiteSP this_site =
157 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
158 stop_info_sp->GetValue());
160 m_explains_stop = false;
164 if (this_site->IsBreakpointAtThisSite(m_return_bp_id)) {
165 // If we are at our "step out" breakpoint, and the stack depth has
166 // shrunk, then this is indeed our stop. If the stack depth has grown,
167 // then we've hit our step out breakpoint recursively. If we are the
168 // only breakpoint at that location, then we do explain the stop, and
169 // we'll just continue. If there was another breakpoint here, then we
170 // don't explain the stop, but we won't mark ourselves Completed,
171 // because maybe that breakpoint will continue, and then we'll finish
174 StackID cur_frame_zero_id;
176 done = (m_stack_id < cur_frame_zero_id);
179 m_stepped_out = true;
182 m_should_stop = false;
184 if (this_site->GetNumberOfOwners() == 1)
185 m_explains_stop = true;
187 m_explains_stop = false;
190 // Check if we've hit one of our "until" breakpoints.
191 until_collection::iterator pos, end = m_until_points.end();
192 for (pos = m_until_points.begin(); pos != end; pos++) {
193 if (this_site->IsBreakpointAtThisSite((*pos).second)) {
194 // If we're at the right stack depth, then we're done.
197 StackID frame_zero_id =
198 m_thread.GetStackFrameAtIndex(0)->GetStackID();
200 if (frame_zero_id == m_stack_id)
202 else if (frame_zero_id < m_stack_id)
205 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
207 // But if we can't even unwind one frame we should just get out
209 if (older_frame_sp) {
210 const SymbolContext &older_context =
211 older_frame_sp->GetSymbolContext(eSymbolContextEverything);
212 SymbolContext stack_context;
213 m_stack_id.GetSymbolContextScope()->CalculateSymbolContext(
216 done = (older_context == stack_context);
224 m_should_stop = false;
226 // Otherwise we've hit this breakpoint recursively. If we're the
227 // only breakpoint here, then we do explain the stop, and we'll
228 // continue. If not then we should let higher plans handle this
230 if (this_site->GetNumberOfOwners() == 1)
231 m_explains_stop = true;
233 m_should_stop = true;
234 m_explains_stop = false;
240 // If we get here we haven't hit any of our breakpoints, so let the
241 // higher plans take care of the stop.
242 m_explains_stop = false;
244 } else if (IsUsuallyUnexplainedStopReason(reason)) {
245 m_explains_stop = false;
247 m_explains_stop = true;
252 bool ThreadPlanStepUntil::DoPlanExplainsStop(Event *event_ptr) {
253 // We don't explain signals or breakpoints (breakpoints that handle stepping
254 // in or out will be handled by a child plan.
256 return m_explains_stop;
259 bool ThreadPlanStepUntil::ShouldStop(Event *event_ptr) {
260 // If we've told our self in ExplainsStop that we plan to continue, then do
261 // so here. Otherwise, as long as this thread has stopped for a reason, we
264 StopInfoSP stop_info_sp = GetPrivateStopInfo();
265 if (!stop_info_sp || stop_info_sp->GetStopReason() == eStopReasonNone)
269 return m_should_stop;
272 bool ThreadPlanStepUntil::StopOthers() { return m_stop_others; }
274 StateType ThreadPlanStepUntil::GetPlanRunState() { return eStateRunning; }
276 bool ThreadPlanStepUntil::DoWillResume(StateType resume_state,
279 TargetSP target_sp(m_thread.CalculateTarget());
281 Breakpoint *return_bp =
282 target_sp->GetBreakpointByID(m_return_bp_id).get();
283 if (return_bp != nullptr)
284 return_bp->SetEnabled(true);
286 until_collection::iterator pos, end = m_until_points.end();
287 for (pos = m_until_points.begin(); pos != end; pos++) {
288 Breakpoint *until_bp =
289 target_sp->GetBreakpointByID((*pos).second).get();
290 if (until_bp != nullptr)
291 until_bp->SetEnabled(true);
296 m_should_stop = true;
297 m_ran_analyze = false;
298 m_explains_stop = false;
302 bool ThreadPlanStepUntil::WillStop() {
303 TargetSP target_sp(m_thread.CalculateTarget());
305 Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
306 if (return_bp != nullptr)
307 return_bp->SetEnabled(false);
309 until_collection::iterator pos, end = m_until_points.end();
310 for (pos = m_until_points.begin(); pos != end; pos++) {
311 Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get();
312 if (until_bp != nullptr)
313 until_bp->SetEnabled(false);
319 bool ThreadPlanStepUntil::MischiefManaged() {
320 // I'm letting "PlanExplainsStop" do all the work, and just reporting that
323 if (IsPlanComplete()) {
324 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
326 log->Printf("Completed step until plan.");
332 ThreadPlan::MischiefManaged();