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