1 //===-- ThreadPlanStepOut.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/ThreadPlanStepOut.h"
11 #include "lldb/Breakpoint/Breakpoint.h"
12 #include "lldb/Core/Value.h"
13 #include "lldb/Core/ValueObjectConstResult.h"
14 #include "lldb/Symbol/Block.h"
15 #include "lldb/Symbol/Function.h"
16 #include "lldb/Symbol/Symbol.h"
17 #include "lldb/Symbol/Type.h"
18 #include "lldb/Target/ABI.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/RegisterContext.h"
21 #include "lldb/Target/StopInfo.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/ThreadPlanStepOverRange.h"
24 #include "lldb/Target/ThreadPlanStepThrough.h"
25 #include "lldb/Utility/Log.h"
26
27 using namespace lldb;
28 using namespace lldb_private;
29
30 uint32_t ThreadPlanStepOut::s_default_flag_values = 0;
31
32 //----------------------------------------------------------------------
33 // ThreadPlanStepOut: Step out of the current frame
34 //----------------------------------------------------------------------
ThreadPlanStepOut(Thread & thread,SymbolContext * context,bool first_insn,bool stop_others,Vote stop_vote,Vote run_vote,uint32_t frame_idx,LazyBool step_out_avoids_code_without_debug_info,bool continue_to_next_branch,bool gather_return_value)35 ThreadPlanStepOut::ThreadPlanStepOut(
36 Thread &thread, SymbolContext *context, bool first_insn, bool stop_others,
37 Vote stop_vote, Vote run_vote, uint32_t frame_idx,
38 LazyBool step_out_avoids_code_without_debug_info,
39 bool continue_to_next_branch, bool gather_return_value)
40 : ThreadPlan(ThreadPlan::eKindStepOut, "Step out", thread, stop_vote,
41 run_vote),
42 ThreadPlanShouldStopHere(this), m_step_from_insn(LLDB_INVALID_ADDRESS),
43 m_return_bp_id(LLDB_INVALID_BREAK_ID),
44 m_return_addr(LLDB_INVALID_ADDRESS), m_stop_others(stop_others),
45 m_immediate_step_from_function(nullptr),
46 m_calculate_return_value(gather_return_value) {
47 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
48 SetFlagsToDefault();
49 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
50
51 m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
52
53 uint32_t return_frame_index = frame_idx + 1;
54 StackFrameSP return_frame_sp(
55 m_thread.GetStackFrameAtIndex(return_frame_index));
56 StackFrameSP immediate_return_from_sp(
57 m_thread.GetStackFrameAtIndex(frame_idx));
58
59 if (!return_frame_sp || !immediate_return_from_sp)
60 return; // we can't do anything here. ValidatePlan() will return false.
61
62 // While stepping out, behave as-if artificial frames are not present.
63 while (return_frame_sp->IsArtificial()) {
64 m_stepped_past_frames.push_back(return_frame_sp);
65
66 ++return_frame_index;
67 return_frame_sp = m_thread.GetStackFrameAtIndex(return_frame_index);
68
69 // We never expect to see an artificial frame without a regular ancestor.
70 // If this happens, log the issue and defensively refuse to step out.
71 if (!return_frame_sp) {
72 LLDB_LOG(log, "Can't step out of frame with artificial ancestors");
73 return;
74 }
75 }
76
77 m_step_out_to_id = return_frame_sp->GetStackID();
78 m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
79
80 // If the frame directly below the one we are returning to is inlined, we
81 // have to be a little more careful. It is non-trivial to determine the real
82 // "return code address" for an inlined frame, so we have to work our way to
83 // that frame and then step out.
84 if (immediate_return_from_sp->IsInlined()) {
85 if (frame_idx > 0) {
86 // First queue a plan that gets us to this inlined frame, and when we get
87 // there we'll queue a second plan that walks us out of this frame.
88 m_step_out_to_inline_plan_sp.reset(new ThreadPlanStepOut(
89 m_thread, nullptr, false, stop_others, eVoteNoOpinion, eVoteNoOpinion,
90 frame_idx - 1, eLazyBoolNo, continue_to_next_branch));
91 static_cast<ThreadPlanStepOut *>(m_step_out_to_inline_plan_sp.get())
92 ->SetShouldStopHereCallbacks(nullptr, nullptr);
93 m_step_out_to_inline_plan_sp->SetPrivate(true);
94 } else {
95 // If we're already at the inlined frame we're stepping through, then
96 // just do that now.
97 QueueInlinedStepPlan(false);
98 }
99 } else {
100 // Find the return address and set a breakpoint there:
101 // FIXME - can we do this more securely if we know first_insn?
102
103 Address return_address(return_frame_sp->GetFrameCodeAddress());
104 if (continue_to_next_branch) {
105 SymbolContext return_address_sc;
106 AddressRange range;
107 Address return_address_decr_pc = return_address;
108 if (return_address_decr_pc.GetOffset() > 0)
109 return_address_decr_pc.Slide(-1);
110
111 return_address_decr_pc.CalculateSymbolContext(
112 &return_address_sc, lldb::eSymbolContextLineEntry);
113 if (return_address_sc.line_entry.IsValid()) {
114 range =
115 return_address_sc.line_entry.GetSameLineContiguousAddressRange();
116 if (range.GetByteSize() > 0) {
117 return_address =
118 m_thread.GetProcess()->AdvanceAddressToNextBranchInstruction(
119 return_address, range);
120 }
121 }
122 }
123 m_return_addr =
124 return_address.GetLoadAddress(&m_thread.GetProcess()->GetTarget());
125
126 if (m_return_addr == LLDB_INVALID_ADDRESS)
127 return;
128
129 Breakpoint *return_bp = m_thread.CalculateTarget()
130 ->CreateBreakpoint(m_return_addr, true, false)
131 .get();
132
133 if (return_bp != nullptr) {
134 if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
135 m_could_not_resolve_hw_bp = true;
136 return_bp->SetThreadID(m_thread.GetID());
137 m_return_bp_id = return_bp->GetID();
138 return_bp->SetBreakpointKind("step-out");
139 }
140
141 if (immediate_return_from_sp) {
142 const SymbolContext &sc =
143 immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
144 if (sc.function) {
145 m_immediate_step_from_function = sc.function;
146 }
147 }
148 }
149 }
150
SetupAvoidNoDebug(LazyBool step_out_avoids_code_without_debug_info)151 void ThreadPlanStepOut::SetupAvoidNoDebug(
152 LazyBool step_out_avoids_code_without_debug_info) {
153 bool avoid_nodebug = true;
154 switch (step_out_avoids_code_without_debug_info) {
155 case eLazyBoolYes:
156 avoid_nodebug = true;
157 break;
158 case eLazyBoolNo:
159 avoid_nodebug = false;
160 break;
161 case eLazyBoolCalculate:
162 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
163 break;
164 }
165 if (avoid_nodebug)
166 GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
167 else
168 GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
169 }
170
DidPush()171 void ThreadPlanStepOut::DidPush() {
172 if (m_step_out_to_inline_plan_sp)
173 m_thread.QueueThreadPlan(m_step_out_to_inline_plan_sp, false);
174 else if (m_step_through_inline_plan_sp)
175 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
176 }
177
~ThreadPlanStepOut()178 ThreadPlanStepOut::~ThreadPlanStepOut() {
179 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
180 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
181 }
182
GetDescription(Stream * s,lldb::DescriptionLevel level)183 void ThreadPlanStepOut::GetDescription(Stream *s,
184 lldb::DescriptionLevel level) {
185 if (level == lldb::eDescriptionLevelBrief)
186 s->Printf("step out");
187 else {
188 if (m_step_out_to_inline_plan_sp)
189 s->Printf("Stepping out to inlined frame so we can walk through it.");
190 else if (m_step_through_inline_plan_sp)
191 s->Printf("Stepping out by stepping through inlined function.");
192 else {
193 s->Printf("Stepping out from ");
194 Address tmp_address;
195 if (tmp_address.SetLoadAddress(m_step_from_insn, &GetTarget())) {
196 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
197 Address::DumpStyleLoadAddress);
198 } else {
199 s->Printf("address 0x%" PRIx64 "", (uint64_t)m_step_from_insn);
200 }
201
202 // FIXME: find some useful way to present the m_return_id, since there may
203 // be multiple copies of the
204 // same function on the stack.
205
206 s->Printf(" returning to frame at ");
207 if (tmp_address.SetLoadAddress(m_return_addr, &GetTarget())) {
208 tmp_address.Dump(s, &GetThread(), Address::DumpStyleResolvedDescription,
209 Address::DumpStyleLoadAddress);
210 } else {
211 s->Printf("address 0x%" PRIx64 "", (uint64_t)m_return_addr);
212 }
213
214 if (level == eDescriptionLevelVerbose)
215 s->Printf(" using breakpoint site %d", m_return_bp_id);
216 }
217 }
218
219 s->Printf("\n");
220 for (StackFrameSP frame_sp : m_stepped_past_frames) {
221 s->Printf("Stepped out past: ");
222 frame_sp->DumpUsingSettingsFormat(s);
223 }
224 }
225
ValidatePlan(Stream * error)226 bool ThreadPlanStepOut::ValidatePlan(Stream *error) {
227 if (m_step_out_to_inline_plan_sp)
228 return m_step_out_to_inline_plan_sp->ValidatePlan(error);
229
230 if (m_step_through_inline_plan_sp)
231 return m_step_through_inline_plan_sp->ValidatePlan(error);
232
233 if (m_could_not_resolve_hw_bp) {
234 if (error)
235 error->PutCString(
236 "Could not create hardware breakpoint for thread plan.");
237 return false;
238 }
239
240 if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
241 if (error)
242 error->PutCString("Could not create return address breakpoint.");
243 return false;
244 }
245
246 return true;
247 }
248
DoPlanExplainsStop(Event * event_ptr)249 bool ThreadPlanStepOut::DoPlanExplainsStop(Event *event_ptr) {
250 // If the step out plan is done, then we just need to step through the
251 // inlined frame.
252 if (m_step_out_to_inline_plan_sp) {
253 return m_step_out_to_inline_plan_sp->MischiefManaged();
254 } else if (m_step_through_inline_plan_sp) {
255 if (m_step_through_inline_plan_sp->MischiefManaged()) {
256 CalculateReturnValue();
257 SetPlanComplete();
258 return true;
259 } else
260 return false;
261 } else if (m_step_out_further_plan_sp) {
262 return m_step_out_further_plan_sp->MischiefManaged();
263 }
264
265 // We don't explain signals or breakpoints (breakpoints that handle stepping
266 // in or out will be handled by a child plan.
267
268 StopInfoSP stop_info_sp = GetPrivateStopInfo();
269 if (stop_info_sp) {
270 StopReason reason = stop_info_sp->GetStopReason();
271 if (reason == eStopReasonBreakpoint) {
272 // If this is OUR breakpoint, we're fine, otherwise we don't know why
273 // this happened...
274 BreakpointSiteSP site_sp(
275 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(
276 stop_info_sp->GetValue()));
277 if (site_sp && site_sp->IsBreakpointAtThisSite(m_return_bp_id)) {
278 bool done;
279
280 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
281
282 if (m_step_out_to_id == frame_zero_id)
283 done = true;
284 else if (m_step_out_to_id < frame_zero_id) {
285 // Either we stepped past the breakpoint, or the stack ID calculation
286 // was incorrect and we should probably stop.
287 done = true;
288 } else {
289 done = (m_immediate_step_from_id < frame_zero_id);
290 }
291
292 if (done) {
293 if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
294 CalculateReturnValue();
295 SetPlanComplete();
296 }
297 }
298
299 // If there was only one owner, then we're done. But if we also hit
300 // some user breakpoint on our way out, we should mark ourselves as
301 // done, but also not claim to explain the stop, since it is more
302 // important to report the user breakpoint than the step out
303 // completion.
304
305 if (site_sp->GetNumberOfOwners() == 1)
306 return true;
307 }
308 return false;
309 } else if (IsUsuallyUnexplainedStopReason(reason))
310 return false;
311 else
312 return true;
313 }
314 return true;
315 }
316
ShouldStop(Event * event_ptr)317 bool ThreadPlanStepOut::ShouldStop(Event *event_ptr) {
318 if (IsPlanComplete())
319 return true;
320
321 bool done = false;
322 if (m_step_out_to_inline_plan_sp) {
323 if (m_step_out_to_inline_plan_sp->MischiefManaged()) {
324 // Now step through the inlined stack we are in:
325 if (QueueInlinedStepPlan(true)) {
326 // If we can't queue a plan to do this, then just call ourselves done.
327 m_step_out_to_inline_plan_sp.reset();
328 SetPlanComplete(false);
329 return true;
330 } else
331 done = true;
332 } else
333 return m_step_out_to_inline_plan_sp->ShouldStop(event_ptr);
334 } else if (m_step_through_inline_plan_sp) {
335 if (m_step_through_inline_plan_sp->MischiefManaged())
336 done = true;
337 else
338 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
339 } else if (m_step_out_further_plan_sp) {
340 if (m_step_out_further_plan_sp->MischiefManaged())
341 m_step_out_further_plan_sp.reset();
342 else
343 return m_step_out_further_plan_sp->ShouldStop(event_ptr);
344 }
345
346 if (!done) {
347 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
348 done = !(frame_zero_id < m_step_out_to_id);
349 }
350
351 // The normal step out computations think we are done, so all we need to do
352 // is consult the ShouldStopHere, and we are done.
353
354 if (done) {
355 if (InvokeShouldStopHereCallback(eFrameCompareOlder, m_status)) {
356 CalculateReturnValue();
357 SetPlanComplete();
358 } else {
359 m_step_out_further_plan_sp =
360 QueueStepOutFromHerePlan(m_flags, eFrameCompareOlder, m_status);
361 done = false;
362 }
363 }
364
365 return done;
366 }
367
StopOthers()368 bool ThreadPlanStepOut::StopOthers() { return m_stop_others; }
369
GetPlanRunState()370 StateType ThreadPlanStepOut::GetPlanRunState() { return eStateRunning; }
371
DoWillResume(StateType resume_state,bool current_plan)372 bool ThreadPlanStepOut::DoWillResume(StateType resume_state,
373 bool current_plan) {
374 if (m_step_out_to_inline_plan_sp || m_step_through_inline_plan_sp)
375 return true;
376
377 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
378 return false;
379
380 if (current_plan) {
381 Breakpoint *return_bp =
382 m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
383 if (return_bp != nullptr)
384 return_bp->SetEnabled(true);
385 }
386 return true;
387 }
388
WillStop()389 bool ThreadPlanStepOut::WillStop() {
390 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
391 Breakpoint *return_bp =
392 m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
393 if (return_bp != nullptr)
394 return_bp->SetEnabled(false);
395 }
396
397 return true;
398 }
399
MischiefManaged()400 bool ThreadPlanStepOut::MischiefManaged() {
401 if (IsPlanComplete()) {
402 // Did I reach my breakpoint? If so I'm done.
403 //
404 // I also check the stack depth, since if we've blown past the breakpoint
405 // for some
406 // reason and we're now stopping for some other reason altogether, then
407 // we're done with this step out operation.
408
409 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
410 if (log)
411 log->Printf("Completed step out plan.");
412 if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
413 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
414 m_return_bp_id = LLDB_INVALID_BREAK_ID;
415 }
416
417 ThreadPlan::MischiefManaged();
418 return true;
419 } else {
420 return false;
421 }
422 }
423
QueueInlinedStepPlan(bool queue_now)424 bool ThreadPlanStepOut::QueueInlinedStepPlan(bool queue_now) {
425 // Now figure out the range of this inlined block, and set up a "step through
426 // range" plan for that. If we've been provided with a context, then use the
427 // block in that context.
428 StackFrameSP immediate_return_from_sp(m_thread.GetStackFrameAtIndex(0));
429 if (!immediate_return_from_sp)
430 return false;
431
432 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
433 if (log) {
434 StreamString s;
435 immediate_return_from_sp->Dump(&s, true, false);
436 log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
437 }
438
439 Block *from_block = immediate_return_from_sp->GetFrameBlock();
440 if (from_block) {
441 Block *inlined_block = from_block->GetContainingInlinedBlock();
442 if (inlined_block) {
443 size_t num_ranges = inlined_block->GetNumRanges();
444 AddressRange inline_range;
445 if (inlined_block->GetRangeAtIndex(0, inline_range)) {
446 SymbolContext inlined_sc;
447 inlined_block->CalculateSymbolContext(&inlined_sc);
448 inlined_sc.target_sp = GetTarget().shared_from_this();
449 RunMode run_mode =
450 m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
451 const LazyBool avoid_no_debug = eLazyBoolNo;
452
453 m_step_through_inline_plan_sp.reset(new ThreadPlanStepOverRange(
454 m_thread, inline_range, inlined_sc, run_mode, avoid_no_debug));
455 ThreadPlanStepOverRange *step_through_inline_plan_ptr =
456 static_cast<ThreadPlanStepOverRange *>(
457 m_step_through_inline_plan_sp.get());
458 m_step_through_inline_plan_sp->SetPrivate(true);
459
460 step_through_inline_plan_ptr->SetOkayToDiscard(true);
461 StreamString errors;
462 if (!step_through_inline_plan_ptr->ValidatePlan(&errors)) {
463 // FIXME: Log this failure.
464 delete step_through_inline_plan_ptr;
465 return false;
466 }
467
468 for (size_t i = 1; i < num_ranges; i++) {
469 if (inlined_block->GetRangeAtIndex(i, inline_range))
470 step_through_inline_plan_ptr->AddRange(inline_range);
471 }
472
473 if (queue_now)
474 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
475 return true;
476 }
477 }
478 }
479
480 return false;
481 }
482
CalculateReturnValue()483 void ThreadPlanStepOut::CalculateReturnValue() {
484 if (m_return_valobj_sp)
485 return;
486
487 if (!m_calculate_return_value)
488 return;
489
490 if (m_immediate_step_from_function != nullptr) {
491 CompilerType return_compiler_type =
492 m_immediate_step_from_function->GetCompilerType()
493 .GetFunctionReturnType();
494 if (return_compiler_type) {
495 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
496 if (abi_sp)
497 m_return_valobj_sp =
498 abi_sp->GetReturnValueObject(m_thread, return_compiler_type);
499 }
500 }
501 }
502
IsPlanStale()503 bool ThreadPlanStepOut::IsPlanStale() {
504 // If we are still lower on the stack than the frame we are returning to,
505 // then there's something for us to do. Otherwise, we're stale.
506
507 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
508 return !(frame_zero_id < m_step_out_to_id);
509 }
510