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