1 //===-- ThreadPlanStepUntil.cpp ---------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Target/ThreadPlanStepUntil.h"
11
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"
19
20 using namespace lldb;
21 using namespace lldb_private;
22
23 //----------------------------------------------------------------------
24 // ThreadPlanStepUntil: Run until we reach a given line number or step out of
25 // the current frame
26 //----------------------------------------------------------------------
27
ThreadPlanStepUntil(Thread & thread,lldb::addr_t * address_list,size_t num_addresses,bool stop_others,uint32_t frame_idx)28 ThreadPlanStepUntil::ThreadPlanStepUntil(Thread &thread,
29 lldb::addr_t *address_list,
30 size_t num_addresses, bool stop_others,
31 uint32_t frame_idx)
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());
41
42 StackFrameSP frame_sp(m_thread.GetStackFrameAtIndex(frame_idx));
43 if (frame_sp) {
44 m_step_from_insn = frame_sp->GetStackID().GetPC();
45 lldb::user_id_t thread_id = m_thread.GetID();
46
47 // Find the return address and set a breakpoint there:
48 // FIXME - can we do this more securely if we know first_insn?
49
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();
56
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");
63 }
64 }
65
66 m_stack_id = frame_sp->GetStackID();
67
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");
76 } else {
77 m_until_points[address_list[i]] = LLDB_INVALID_BREAK_ID;
78 }
79 }
80 }
81 }
82
~ThreadPlanStepUntil()83 ThreadPlanStepUntil::~ThreadPlanStepUntil() { Clear(); }
84
Clear()85 void ThreadPlanStepUntil::Clear() {
86 TargetSP target_sp(m_thread.CalculateTarget());
87 if (target_sp) {
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;
91 }
92
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);
96 }
97 }
98 m_until_points.clear();
99 m_could_not_resolve_hw_bp = false;
100 }
101
GetDescription(Stream * s,lldb::DescriptionLevel level)102 void ThreadPlanStepUntil::GetDescription(Stream *s,
103 lldb::DescriptionLevel level) {
104 if (level == lldb::eDescriptionLevelBrief) {
105 s->Printf("step until");
106 if (m_stepped_out)
107 s->Printf(" - stepped out");
108 } else {
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);
115 else {
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,
121 (*pos).second);
122 }
123 }
124 s->Printf(" stepped out address is 0x%" PRIx64 ".",
125 (uint64_t)m_return_addr);
126 }
127 }
128
ValidatePlan(Stream * error)129 bool ThreadPlanStepUntil::ValidatePlan(Stream *error) {
130 if (m_could_not_resolve_hw_bp) {
131 if (error)
132 error->PutCString(
133 "Could not create hardware breakpoint for thread plan.");
134 return false;
135 } else if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
136 if (error)
137 error->PutCString("Could not create return breakpoint.");
138 return false;
139 } else {
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))
143 return false;
144 }
145 return true;
146 }
147 }
148
AnalyzeStop()149 void ThreadPlanStepUntil::AnalyzeStop() {
150 if (m_ran_analyze)
151 return;
152
153 StopInfoSP stop_info_sp = GetPrivateStopInfo();
154 m_should_stop = true;
155 m_explains_stop = false;
156
157 if (stop_info_sp) {
158 StopReason reason = stop_info_sp->GetStopReason();
159
160 if (reason == eStopReasonBreakpoint) {
161 // If this is OUR breakpoint, we're fine, otherwise we don't know why
162 // this happened...
163 BreakpointSiteSP this_site =
164 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
165 stop_info_sp->GetValue());
166 if (!this_site) {
167 m_explains_stop = false;
168 return;
169 }
170
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
179 // the "until".
180 bool done;
181 StackID cur_frame_zero_id;
182
183 done = (m_stack_id < cur_frame_zero_id);
184
185 if (done) {
186 m_stepped_out = true;
187 SetPlanComplete();
188 } else
189 m_should_stop = false;
190
191 if (this_site->GetNumberOfOwners() == 1)
192 m_explains_stop = true;
193 else
194 m_explains_stop = false;
195 return;
196 } else {
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.
202
203 bool done;
204 StackID frame_zero_id =
205 m_thread.GetStackFrameAtIndex(0)->GetStackID();
206
207 if (frame_zero_id == m_stack_id)
208 done = true;
209 else if (frame_zero_id < m_stack_id)
210 done = false;
211 else {
212 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
213
214 // But if we can't even unwind one frame we should just get out
215 // of here & stop...
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(
221 &stack_context);
222
223 done = (older_context == stack_context);
224 } else
225 done = false;
226 }
227
228 if (done)
229 SetPlanComplete();
230 else
231 m_should_stop = false;
232
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
236 // stop.
237 if (this_site->GetNumberOfOwners() == 1)
238 m_explains_stop = true;
239 else {
240 m_should_stop = true;
241 m_explains_stop = false;
242 }
243 return;
244 }
245 }
246 }
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;
250 return;
251 } else if (IsUsuallyUnexplainedStopReason(reason)) {
252 m_explains_stop = false;
253 } else {
254 m_explains_stop = true;
255 }
256 }
257 }
258
DoPlanExplainsStop(Event * event_ptr)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.
262 AnalyzeStop();
263 return m_explains_stop;
264 }
265
ShouldStop(Event * event_ptr)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
269 // will stop.
270
271 StopInfoSP stop_info_sp = GetPrivateStopInfo();
272 if (!stop_info_sp || stop_info_sp->GetStopReason() == eStopReasonNone)
273 return false;
274
275 AnalyzeStop();
276 return m_should_stop;
277 }
278
StopOthers()279 bool ThreadPlanStepUntil::StopOthers() { return m_stop_others; }
280
GetPlanRunState()281 StateType ThreadPlanStepUntil::GetPlanRunState() { return eStateRunning; }
282
DoWillResume(StateType resume_state,bool current_plan)283 bool ThreadPlanStepUntil::DoWillResume(StateType resume_state,
284 bool current_plan) {
285 if (current_plan) {
286 TargetSP target_sp(m_thread.CalculateTarget());
287 if (target_sp) {
288 Breakpoint *return_bp =
289 target_sp->GetBreakpointByID(m_return_bp_id).get();
290 if (return_bp != nullptr)
291 return_bp->SetEnabled(true);
292
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);
299 }
300 }
301 }
302
303 m_should_stop = true;
304 m_ran_analyze = false;
305 m_explains_stop = false;
306 return true;
307 }
308
WillStop()309 bool ThreadPlanStepUntil::WillStop() {
310 TargetSP target_sp(m_thread.CalculateTarget());
311 if (target_sp) {
312 Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
313 if (return_bp != nullptr)
314 return_bp->SetEnabled(false);
315
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);
321 }
322 }
323 return true;
324 }
325
MischiefManaged()326 bool ThreadPlanStepUntil::MischiefManaged() {
327 // I'm letting "PlanExplainsStop" do all the work, and just reporting that
328 // here.
329 bool done = false;
330 if (IsPlanComplete()) {
331 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
332 if (log)
333 log->Printf("Completed step until plan.");
334
335 Clear();
336 done = true;
337 }
338 if (done)
339 ThreadPlan::MischiefManaged();
340
341 return done;
342 }
343