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 //===----------------------------------------------------------------------===//
14 #include "lldb/Target/ThreadPlanStepUntil.h"
18 // Other libraries and framework includes
20 #include "lldb/Breakpoint/Breakpoint.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/RegisterContext.h"
24 #include "lldb/Target/StopInfo.h"
25 #include "lldb/Target/Target.h"
28 using namespace lldb_private;
30 //----------------------------------------------------------------------
31 // ThreadPlanStepUntil: Run until we reach a given line number or step out of the current frame
32 //----------------------------------------------------------------------
34 ThreadPlanStepUntil::ThreadPlanStepUntil
37 lldb::addr_t *address_list,
42 ThreadPlan (ThreadPlan::eKindStepUntil, "Step until", thread, eVoteNoOpinion, eVoteNoOpinion),
43 m_step_from_insn (LLDB_INVALID_ADDRESS),
44 m_return_bp_id (LLDB_INVALID_BREAK_ID),
45 m_return_addr (LLDB_INVALID_ADDRESS),
46 m_stepped_out (false),
47 m_should_stop (false),
48 m_ran_analyze (false),
49 m_explains_stop (false),
51 m_stop_others (stop_others)
53 // Stash away our "until" addresses:
54 TargetSP target_sp (m_thread.CalculateTarget());
56 StackFrameSP frame_sp (m_thread.GetStackFrameAtIndex (frame_idx));
59 m_step_from_insn = frame_sp->GetStackID().GetPC();
60 lldb::user_id_t thread_id = m_thread.GetID();
62 // Find the return address and set a breakpoint there:
63 // FIXME - can we do this more securely if we know first_insn?
65 StackFrameSP return_frame_sp (m_thread.GetStackFrameAtIndex(frame_idx + 1));
68 // TODO: add inline functionality
69 m_return_addr = return_frame_sp->GetStackID().GetPC();
70 Breakpoint *return_bp = target_sp->CreateBreakpoint (m_return_addr, true, false).get();
71 if (return_bp != NULL)
73 return_bp->SetThreadID(thread_id);
74 m_return_bp_id = return_bp->GetID();
75 return_bp->SetBreakpointKind ("until-return-backstop");
79 m_stack_id = frame_sp->GetStackID();
81 // Now set breakpoints on all our return addresses:
82 for (size_t i = 0; i < num_addresses; i++)
84 Breakpoint *until_bp = target_sp->CreateBreakpoint (address_list[i], true, false).get();
87 until_bp->SetThreadID(thread_id);
88 m_until_points[address_list[i]] = until_bp->GetID();
89 until_bp->SetBreakpointKind("until-target");
93 m_until_points[address_list[i]] = LLDB_INVALID_BREAK_ID;
99 ThreadPlanStepUntil::~ThreadPlanStepUntil ()
105 ThreadPlanStepUntil::Clear()
107 TargetSP target_sp (m_thread.CalculateTarget());
110 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
112 target_sp->RemoveBreakpointByID(m_return_bp_id);
113 m_return_bp_id = LLDB_INVALID_BREAK_ID;
116 until_collection::iterator pos, end = m_until_points.end();
117 for (pos = m_until_points.begin(); pos != end; pos++)
119 target_sp->RemoveBreakpointByID((*pos).second);
122 m_until_points.clear();
126 ThreadPlanStepUntil::GetDescription (Stream *s, lldb::DescriptionLevel level)
128 if (level == lldb::eDescriptionLevelBrief)
130 s->Printf ("step until");
132 s->Printf (" - stepped out");
136 if (m_until_points.size() == 1)
137 s->Printf ("Stepping from address 0x%" PRIx64 " until we reach 0x%" PRIx64 " using breakpoint %d",
138 (uint64_t)m_step_from_insn,
139 (uint64_t) (*m_until_points.begin()).first,
140 (*m_until_points.begin()).second);
143 until_collection::iterator pos, end = m_until_points.end();
144 s->Printf ("Stepping from address 0x%" PRIx64 " until we reach one of:",
145 (uint64_t)m_step_from_insn);
146 for (pos = m_until_points.begin(); pos != end; pos++)
148 s->Printf ("\n\t0x%" PRIx64 " (bp: %d)", (uint64_t) (*pos).first, (*pos).second);
151 s->Printf(" stepped out address is 0x%" PRIx64 ".", (uint64_t) m_return_addr);
156 ThreadPlanStepUntil::ValidatePlan (Stream *error)
158 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
162 until_collection::iterator pos, end = m_until_points.end();
163 for (pos = m_until_points.begin(); pos != end; pos++)
165 if (!LLDB_BREAK_ID_IS_VALID ((*pos).second))
173 ThreadPlanStepUntil::AnalyzeStop()
178 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
179 m_should_stop = true;
180 m_explains_stop = false;
184 StopReason reason = stop_info_sp->GetStopReason();
188 case eStopReasonBreakpoint:
190 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
191 BreakpointSiteSP this_site = m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue());
194 m_explains_stop = false;
198 if (this_site->IsBreakpointAtThisSite (m_return_bp_id))
200 // If we are at our "step out" breakpoint, and the stack depth has shrunk, then
201 // this is indeed our stop.
202 // If the stack depth has grown, then we've hit our step out breakpoint recursively.
203 // If we are the only breakpoint at that location, then we do explain the stop, and
204 // we'll just continue.
205 // If there was another breakpoint here, then we don't explain the stop, but we won't
206 // mark ourselves Completed, because maybe that breakpoint will continue, and then
207 // we'll finish the "until".
209 StackID cur_frame_zero_id;
211 if (m_stack_id < cur_frame_zero_id)
218 m_stepped_out = true;
222 m_should_stop = false;
224 if (this_site->GetNumberOfOwners() == 1)
225 m_explains_stop = true;
227 m_explains_stop = false;
232 // Check if we've hit one of our "until" breakpoints.
233 until_collection::iterator pos, end = m_until_points.end();
234 for (pos = m_until_points.begin(); pos != end; pos++)
236 if (this_site->IsBreakpointAtThisSite ((*pos).second))
238 // If we're at the right stack depth, then we're done.
241 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
243 if (frame_zero_id == m_stack_id)
245 else if (frame_zero_id < m_stack_id)
249 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
251 // But if we can't even unwind one frame we should just get out of here & stop...
254 const SymbolContext &older_context
255 = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
256 SymbolContext stack_context;
257 m_stack_id.GetSymbolContextScope()->CalculateSymbolContext(&stack_context);
259 if (older_context == stack_context)
271 m_should_stop = false;
273 // Otherwise we've hit this breakpoint recursively. If we're the
274 // only breakpoint here, then we do explain the stop, and we'll continue.
275 // If not then we should let higher plans handle this stop.
276 if (this_site->GetNumberOfOwners() == 1)
277 m_explains_stop = true;
280 m_should_stop = true;
281 m_explains_stop = false;
287 // If we get here we haven't hit any of our breakpoints, so let the higher
288 // plans take care of the stop.
289 m_explains_stop = false;
292 case eStopReasonWatchpoint:
293 case eStopReasonSignal:
294 case eStopReasonException:
295 case eStopReasonExec:
296 case eStopReasonThreadExiting:
297 m_explains_stop = false;
300 m_explains_stop = true;
307 ThreadPlanStepUntil::DoPlanExplainsStop (Event *event_ptr)
309 // We don't explain signals or breakpoints (breakpoints that handle stepping in or
310 // out will be handled by a child plan.
312 return m_explains_stop;
316 ThreadPlanStepUntil::ShouldStop (Event *event_ptr)
318 // If we've told our self in ExplainsStop that we plan to continue, then
319 // do so here. Otherwise, as long as this thread has stopped for a reason,
322 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
323 if (!stop_info_sp || stop_info_sp->GetStopReason() == eStopReasonNone)
327 return m_should_stop;
331 ThreadPlanStepUntil::StopOthers ()
333 return m_stop_others;
337 ThreadPlanStepUntil::GetPlanRunState ()
339 return eStateRunning;
343 ThreadPlanStepUntil::DoWillResume (StateType resume_state, bool current_plan)
347 TargetSP target_sp (m_thread.CalculateTarget());
350 Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
351 if (return_bp != NULL)
352 return_bp->SetEnabled (true);
354 until_collection::iterator pos, end = m_until_points.end();
355 for (pos = m_until_points.begin(); pos != end; pos++)
357 Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get();
358 if (until_bp != NULL)
359 until_bp->SetEnabled (true);
364 m_should_stop = true;
365 m_ran_analyze = false;
366 m_explains_stop = false;
371 ThreadPlanStepUntil::WillStop ()
373 TargetSP target_sp (m_thread.CalculateTarget());
376 Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
377 if (return_bp != NULL)
378 return_bp->SetEnabled (false);
380 until_collection::iterator pos, end = m_until_points.end();
381 for (pos = m_until_points.begin(); pos != end; pos++)
383 Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get();
384 if (until_bp != NULL)
385 until_bp->SetEnabled (false);
392 ThreadPlanStepUntil::MischiefManaged ()
395 // I'm letting "PlanExplainsStop" do all the work, and just reporting that here.
397 if (IsPlanComplete())
399 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
401 log->Printf("Completed step until plan.");
407 ThreadPlan::MischiefManaged ();