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 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlanStepUntil.h"
12 #include "lldb/Breakpoint/Breakpoint.h"
13 #include "lldb/Symbol/SymbolContextScope.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Target/RegisterContext.h"
16 #include "lldb/Target/StopInfo.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Utility/Log.h"
21 using namespace lldb_private;
23 //----------------------------------------------------------------------
24 // ThreadPlanStepUntil: Run until we reach a given line number or step out of
26 //----------------------------------------------------------------------
28 ThreadPlanStepUntil::ThreadPlanStepUntil(Thread &thread,
29 lldb::addr_t *address_list,
30 size_t num_addresses, bool stop_others,
32 : ThreadPlan(ThreadPlan::eKindStepUntil, "Step until", thread,
33 eVoteNoOpinion, eVoteNoOpinion),
34 m_step_from_insn(LLDB_INVALID_ADDRESS),
35 m_return_bp_id(LLDB_INVALID_BREAK_ID),
36 m_return_addr(LLDB_INVALID_ADDRESS), m_stepped_out(false),
37 m_should_stop(false), m_ran_analyze(false), m_explains_stop(false),
38 m_until_points(), m_stop_others(stop_others) {
39 // Stash away our "until" addresses:
40 TargetSP target_sp(m_thread.CalculateTarget());
42 StackFrameSP frame_sp(m_thread.GetStackFrameAtIndex(frame_idx));
44 m_step_from_insn = frame_sp->GetStackID().GetPC();
45 lldb::user_id_t thread_id = m_thread.GetID();
47 // Find the return address and set a breakpoint there:
48 // FIXME - can we do this more securely if we know first_insn?
50 StackFrameSP return_frame_sp(m_thread.GetStackFrameAtIndex(frame_idx + 1));
51 if (return_frame_sp) {
52 // TODO: add inline functionality
53 m_return_addr = return_frame_sp->GetStackID().GetPC();
54 Breakpoint *return_bp =
55 target_sp->CreateBreakpoint(m_return_addr, true, false).get();
57 if (return_bp != nullptr) {
58 if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
59 m_could_not_resolve_hw_bp = true;
60 return_bp->SetThreadID(thread_id);
61 m_return_bp_id = return_bp->GetID();
62 return_bp->SetBreakpointKind("until-return-backstop");
66 m_stack_id = frame_sp->GetStackID();
68 // Now set breakpoints on all our return addresses:
69 for (size_t i = 0; i < num_addresses; i++) {
70 Breakpoint *until_bp =
71 target_sp->CreateBreakpoint(address_list[i], true, false).get();
72 if (until_bp != nullptr) {
73 until_bp->SetThreadID(thread_id);
74 m_until_points[address_list[i]] = until_bp->GetID();
75 until_bp->SetBreakpointKind("until-target");
77 m_until_points[address_list[i]] = LLDB_INVALID_BREAK_ID;
83 ThreadPlanStepUntil::~ThreadPlanStepUntil() { Clear(); }
85 void ThreadPlanStepUntil::Clear() {
86 TargetSP target_sp(m_thread.CalculateTarget());
88 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
89 target_sp->RemoveBreakpointByID(m_return_bp_id);
90 m_return_bp_id = LLDB_INVALID_BREAK_ID;
93 until_collection::iterator pos, end = m_until_points.end();
94 for (pos = m_until_points.begin(); pos != end; pos++) {
95 target_sp->RemoveBreakpointByID((*pos).second);
98 m_until_points.clear();
99 m_could_not_resolve_hw_bp = false;
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_could_not_resolve_hw_bp) {
133 "Could not create hardware breakpoint for thread plan.");
135 } else if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
137 error->PutCString("Could not create return breakpoint.");
140 until_collection::iterator pos, end = m_until_points.end();
141 for (pos = m_until_points.begin(); pos != end; pos++) {
142 if (!LLDB_BREAK_ID_IS_VALID((*pos).second))
149 void ThreadPlanStepUntil::AnalyzeStop() {
153 StopInfoSP stop_info_sp = GetPrivateStopInfo();
154 m_should_stop = true;
155 m_explains_stop = false;
158 StopReason reason = stop_info_sp->GetStopReason();
160 if (reason == eStopReasonBreakpoint) {
161 // If this is OUR breakpoint, we're fine, otherwise we don't know why
163 BreakpointSiteSP this_site =
164 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
165 stop_info_sp->GetValue());
167 m_explains_stop = false;
171 if (this_site->IsBreakpointAtThisSite(m_return_bp_id)) {
172 // If we are at our "step out" breakpoint, and the stack depth has
173 // shrunk, then this is indeed our stop. If the stack depth has grown,
174 // then we've hit our step out breakpoint recursively. If we are the
175 // only breakpoint at that location, then we do explain the stop, and
176 // we'll just continue. If there was another breakpoint here, then we
177 // don't explain the stop, but we won't mark ourselves Completed,
178 // because maybe that breakpoint will continue, and then we'll finish
181 StackID cur_frame_zero_id;
183 done = (m_stack_id < cur_frame_zero_id);
186 m_stepped_out = true;
189 m_should_stop = false;
191 if (this_site->GetNumberOfOwners() == 1)
192 m_explains_stop = true;
194 m_explains_stop = false;
197 // Check if we've hit one of our "until" breakpoints.
198 until_collection::iterator pos, end = m_until_points.end();
199 for (pos = m_until_points.begin(); pos != end; pos++) {
200 if (this_site->IsBreakpointAtThisSite((*pos).second)) {
201 // If we're at the right stack depth, then we're done.
204 StackID frame_zero_id =
205 m_thread.GetStackFrameAtIndex(0)->GetStackID();
207 if (frame_zero_id == m_stack_id)
209 else if (frame_zero_id < m_stack_id)
212 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
214 // But if we can't even unwind one frame we should just get out
216 if (older_frame_sp) {
217 const SymbolContext &older_context =
218 older_frame_sp->GetSymbolContext(eSymbolContextEverything);
219 SymbolContext stack_context;
220 m_stack_id.GetSymbolContextScope()->CalculateSymbolContext(
223 done = (older_context == stack_context);
231 m_should_stop = false;
233 // Otherwise we've hit this breakpoint recursively. If we're the
234 // only breakpoint here, then we do explain the stop, and we'll
235 // continue. If not then we should let higher plans handle this
237 if (this_site->GetNumberOfOwners() == 1)
238 m_explains_stop = true;
240 m_should_stop = true;
241 m_explains_stop = false;
247 // If we get here we haven't hit any of our breakpoints, so let the
248 // higher plans take care of the stop.
249 m_explains_stop = false;
251 } else if (IsUsuallyUnexplainedStopReason(reason)) {
252 m_explains_stop = false;
254 m_explains_stop = true;
259 bool ThreadPlanStepUntil::DoPlanExplainsStop(Event *event_ptr) {
260 // We don't explain signals or breakpoints (breakpoints that handle stepping
261 // in or out will be handled by a child plan.
263 return m_explains_stop;
266 bool ThreadPlanStepUntil::ShouldStop(Event *event_ptr) {
267 // If we've told our self in ExplainsStop that we plan to continue, then do
268 // so here. Otherwise, as long as this thread has stopped for a reason, we
271 StopInfoSP stop_info_sp = GetPrivateStopInfo();
272 if (!stop_info_sp || stop_info_sp->GetStopReason() == eStopReasonNone)
276 return m_should_stop;
279 bool ThreadPlanStepUntil::StopOthers() { return m_stop_others; }
281 StateType ThreadPlanStepUntil::GetPlanRunState() { return eStateRunning; }
283 bool ThreadPlanStepUntil::DoWillResume(StateType resume_state,
286 TargetSP target_sp(m_thread.CalculateTarget());
288 Breakpoint *return_bp =
289 target_sp->GetBreakpointByID(m_return_bp_id).get();
290 if (return_bp != nullptr)
291 return_bp->SetEnabled(true);
293 until_collection::iterator pos, end = m_until_points.end();
294 for (pos = m_until_points.begin(); pos != end; pos++) {
295 Breakpoint *until_bp =
296 target_sp->GetBreakpointByID((*pos).second).get();
297 if (until_bp != nullptr)
298 until_bp->SetEnabled(true);
303 m_should_stop = true;
304 m_ran_analyze = false;
305 m_explains_stop = false;
309 bool ThreadPlanStepUntil::WillStop() {
310 TargetSP target_sp(m_thread.CalculateTarget());
312 Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
313 if (return_bp != nullptr)
314 return_bp->SetEnabled(false);
316 until_collection::iterator pos, end = m_until_points.end();
317 for (pos = m_until_points.begin(); pos != end; pos++) {
318 Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get();
319 if (until_bp != nullptr)
320 until_bp->SetEnabled(false);
326 bool ThreadPlanStepUntil::MischiefManaged() {
327 // I'm letting "PlanExplainsStop" do all the work, and just reporting that
330 if (IsPlanComplete()) {
331 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
333 log->Printf("Completed step until plan.");
339 ThreadPlan::MischiefManaged();