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/lldb-private-log.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/RegisterContext.h"
25 #include "lldb/Target/StopInfo.h"
26 #include "lldb/Target/Target.h"
29 using namespace lldb_private;
31 //----------------------------------------------------------------------
32 // ThreadPlanStepUntil: Run until we reach a given line number or step out of the current frame
33 //----------------------------------------------------------------------
35 ThreadPlanStepUntil::ThreadPlanStepUntil
38 lldb::addr_t *address_list,
43 ThreadPlan (ThreadPlan::eKindStepUntil, "Step until", thread, eVoteNoOpinion, eVoteNoOpinion),
44 m_step_from_insn (LLDB_INVALID_ADDRESS),
45 m_return_bp_id (LLDB_INVALID_BREAK_ID),
46 m_return_addr (LLDB_INVALID_ADDRESS),
47 m_stepped_out (false),
48 m_should_stop (false),
49 m_ran_analyze (false),
50 m_explains_stop (false),
52 m_stop_others (stop_others)
54 // Stash away our "until" addresses:
55 TargetSP target_sp (m_thread.CalculateTarget());
57 StackFrameSP frame_sp (m_thread.GetStackFrameAtIndex (frame_idx));
60 m_step_from_insn = frame_sp->GetStackID().GetPC();
61 lldb::user_id_t thread_id = m_thread.GetID();
63 // Find the return address and set a breakpoint there:
64 // FIXME - can we do this more securely if we know first_insn?
66 StackFrameSP return_frame_sp (m_thread.GetStackFrameAtIndex(frame_idx + 1));
69 // TODO: add inline functionality
70 m_return_addr = return_frame_sp->GetStackID().GetPC();
71 Breakpoint *return_bp = target_sp->CreateBreakpoint (m_return_addr, true).get();
72 if (return_bp != NULL)
74 return_bp->SetThreadID(thread_id);
75 m_return_bp_id = return_bp->GetID();
76 return_bp->SetBreakpointKind ("until-return-backstop");
80 m_stack_id = m_thread.GetStackFrameAtIndex(frame_idx)->GetStackID();
82 // Now set breakpoints on all our return addresses:
83 for (size_t i = 0; i < num_addresses; i++)
85 Breakpoint *until_bp = target_sp->CreateBreakpoint (address_list[i], true).get();
88 until_bp->SetThreadID(thread_id);
89 m_until_points[address_list[i]] = until_bp->GetID();
90 until_bp->SetBreakpointKind("until-target");
94 m_until_points[address_list[i]] = LLDB_INVALID_BREAK_ID;
100 ThreadPlanStepUntil::~ThreadPlanStepUntil ()
106 ThreadPlanStepUntil::Clear()
108 TargetSP target_sp (m_thread.CalculateTarget());
111 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
113 target_sp->RemoveBreakpointByID(m_return_bp_id);
114 m_return_bp_id = LLDB_INVALID_BREAK_ID;
117 until_collection::iterator pos, end = m_until_points.end();
118 for (pos = m_until_points.begin(); pos != end; pos++)
120 target_sp->RemoveBreakpointByID((*pos).second);
123 m_until_points.clear();
127 ThreadPlanStepUntil::GetDescription (Stream *s, lldb::DescriptionLevel level)
129 if (level == lldb::eDescriptionLevelBrief)
131 s->Printf ("step until");
133 s->Printf (" - stepped out");
137 if (m_until_points.size() == 1)
138 s->Printf ("Stepping from address 0x%" PRIx64 " until we reach 0x%" PRIx64 " using breakpoint %d",
139 (uint64_t)m_step_from_insn,
140 (uint64_t) (*m_until_points.begin()).first,
141 (*m_until_points.begin()).second);
144 until_collection::iterator pos, end = m_until_points.end();
145 s->Printf ("Stepping from address 0x%" PRIx64 " until we reach one of:",
146 (uint64_t)m_step_from_insn);
147 for (pos = m_until_points.begin(); pos != end; pos++)
149 s->Printf ("\n\t0x%" PRIx64 " (bp: %d)", (uint64_t) (*pos).first, (*pos).second);
152 s->Printf(" stepped out address is 0x%" PRIx64 ".", (uint64_t) m_return_addr);
157 ThreadPlanStepUntil::ValidatePlan (Stream *error)
159 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
163 until_collection::iterator pos, end = m_until_points.end();
164 for (pos = m_until_points.begin(); pos != end; pos++)
166 if (!LLDB_BREAK_ID_IS_VALID ((*pos).second))
174 ThreadPlanStepUntil::AnalyzeStop()
179 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
180 m_should_stop = true;
181 m_explains_stop = false;
185 StopReason reason = stop_info_sp->GetStopReason();
189 case eStopReasonBreakpoint:
191 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
192 BreakpointSiteSP this_site = m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue());
195 m_explains_stop = false;
199 if (this_site->IsBreakpointAtThisSite (m_return_bp_id))
201 // If we are at our "step out" breakpoint, and the stack depth has shrunk, then
202 // this is indeed our stop.
203 // If the stack depth has grown, then we've hit our step out breakpoint recursively.
204 // If we are the only breakpoint at that location, then we do explain the stop, and
205 // we'll just continue.
206 // If there was another breakpoint here, then we don't explain the stop, but we won't
207 // mark ourselves Completed, because maybe that breakpoint will continue, and then
208 // we'll finish the "until".
210 StackID cur_frame_zero_id;
212 if (m_stack_id < cur_frame_zero_id)
219 m_stepped_out = true;
223 m_should_stop = false;
225 if (this_site->GetNumberOfOwners() == 1)
226 m_explains_stop = true;
228 m_explains_stop = false;
233 // Check if we've hit one of our "until" breakpoints.
234 until_collection::iterator pos, end = m_until_points.end();
235 for (pos = m_until_points.begin(); pos != end; pos++)
237 if (this_site->IsBreakpointAtThisSite ((*pos).second))
239 // If we're at the right stack depth, then we're done.
242 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
244 if (frame_zero_id == m_stack_id)
246 else if (frame_zero_id < m_stack_id)
250 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
252 // But if we can't even unwind one frame we should just get out of here & stop...
255 const SymbolContext &older_context
256 = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
257 SymbolContext stack_context;
258 m_stack_id.GetSymbolContextScope()->CalculateSymbolContext(&stack_context);
260 if (older_context == stack_context)
272 m_should_stop = false;
274 // Otherwise we've hit this breakpoint recursively. If we're the
275 // only breakpoint here, then we do explain the stop, and we'll continue.
276 // If not then we should let higher plans handle this stop.
277 if (this_site->GetNumberOfOwners() == 1)
278 m_explains_stop = true;
281 m_should_stop = true;
282 m_explains_stop = false;
288 // If we get here we haven't hit any of our breakpoints, so let the higher
289 // plans take care of the stop.
290 m_explains_stop = false;
293 case eStopReasonWatchpoint:
294 case eStopReasonSignal:
295 case eStopReasonException:
296 case eStopReasonExec:
297 case eStopReasonThreadExiting:
298 m_explains_stop = false;
301 m_explains_stop = true;
308 ThreadPlanStepUntil::DoPlanExplainsStop (Event *event_ptr)
310 // We don't explain signals or breakpoints (breakpoints that handle stepping in or
311 // out will be handled by a child plan.
313 return m_explains_stop;
317 ThreadPlanStepUntil::ShouldStop (Event *event_ptr)
319 // If we've told our self in ExplainsStop that we plan to continue, then
320 // do so here. Otherwise, as long as this thread has stopped for a reason,
323 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
324 if (!stop_info_sp || stop_info_sp->GetStopReason() == eStopReasonNone)
328 return m_should_stop;
332 ThreadPlanStepUntil::StopOthers ()
334 return m_stop_others;
338 ThreadPlanStepUntil::GetPlanRunState ()
340 return eStateRunning;
344 ThreadPlanStepUntil::DoWillResume (StateType resume_state, bool current_plan)
348 TargetSP target_sp (m_thread.CalculateTarget());
351 Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
352 if (return_bp != NULL)
353 return_bp->SetEnabled (true);
355 until_collection::iterator pos, end = m_until_points.end();
356 for (pos = m_until_points.begin(); pos != end; pos++)
358 Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get();
359 if (until_bp != NULL)
360 until_bp->SetEnabled (true);
365 m_should_stop = true;
366 m_ran_analyze = false;
367 m_explains_stop = false;
372 ThreadPlanStepUntil::WillStop ()
374 TargetSP target_sp (m_thread.CalculateTarget());
377 Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
378 if (return_bp != NULL)
379 return_bp->SetEnabled (false);
381 until_collection::iterator pos, end = m_until_points.end();
382 for (pos = m_until_points.begin(); pos != end; pos++)
384 Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get();
385 if (until_bp != NULL)
386 until_bp->SetEnabled (false);
393 ThreadPlanStepUntil::MischiefManaged ()
396 // I'm letting "PlanExplainsStop" do all the work, and just reporting that here.
398 if (IsPlanComplete())
400 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
402 log->Printf("Completed step until plan.");
408 ThreadPlan::MischiefManaged ();