blob: 534332415c2c965973b8eb23964567344ce7da2e [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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/lldb-private-log.h"
18#include "lldb/Core/Log.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000019#include "lldb/Core/Value.h"
20#include "lldb/Core/ValueObjectConstResult.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Symbol/Block.h"
22#include "lldb/Symbol/Function.h"
23#include "lldb/Symbol/Type.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000026#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Target/Target.h"
Jim Inghama5ce6c82011-10-15 00:57:28 +000028#include "lldb/Target/ThreadPlanStepOverRange.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
33//----------------------------------------------------------------------
34// ThreadPlanStepOut: Step out of the current frame
35//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036ThreadPlanStepOut::ThreadPlanStepOut
37(
38 Thread &thread,
39 SymbolContext *context,
40 bool first_insn,
41 bool stop_others,
42 Vote stop_vote,
Greg Clayton481cef22011-01-21 06:11:58 +000043 Vote run_vote,
44 uint32_t frame_idx
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045) :
Jim Inghamb01e7422010-06-19 04:45:32 +000046 ThreadPlan (ThreadPlan::eKindStepOut, "Step out", thread, stop_vote, run_vote),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047 m_step_from_context (context),
48 m_step_from_insn (LLDB_INVALID_ADDRESS),
Benjamin Kramer1ee0d4f2010-07-16 12:32:33 +000049 m_return_bp_id (LLDB_INVALID_BREAK_ID),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050 m_return_addr (LLDB_INVALID_ADDRESS),
51 m_first_insn (first_insn),
Jim Inghama5ce6c82011-10-15 00:57:28 +000052 m_stop_others (stop_others),
53 m_step_through_inline_plan_sp(),
Jim Ingham73ca05a2011-12-17 01:35:57 +000054 m_step_out_plan_sp (),
55 m_immediate_step_from_function(NULL)
Jim Inghama5ce6c82011-10-15 00:57:28 +000056
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057{
58 m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
59
Greg Clayton481cef22011-01-21 06:11:58 +000060 StackFrameSP return_frame_sp (m_thread.GetStackFrameAtIndex(frame_idx + 1));
Jim Inghama5ce6c82011-10-15 00:57:28 +000061 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (frame_idx));
62
Sean Callanan632d2f72012-03-13 16:34:56 +000063 if (!return_frame_sp || !immediate_return_from_sp)
64 return; // we can't do anything here. ValidatePlan() will return false.
65
Jim Inghamb5c0d1c2012-03-01 00:50:50 +000066 m_step_out_to_id = return_frame_sp->GetStackID();
67 m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
68
69 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
Jim Inghama5ce6c82011-10-15 00:57:28 +000070
71 // If the frame directly below the one we are returning to is inlined, we have to be
72 // a little more careful. It is non-trivial to determine the real "return code address" for
73 // an inlined frame, so we have to work our way to that frame and then step out.
74 if (immediate_return_from_sp && immediate_return_from_sp->IsInlined())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 {
Jim Inghama5ce6c82011-10-15 00:57:28 +000076 if (frame_idx > 0)
77 {
78 // First queue a plan that gets us to this inlined frame, and when we get there we'll queue a second
79 // plan that walks us out of this frame.
Jim Inghamb5c0d1c2012-03-01 00:50:50 +000080 m_step_out_plan_sp.reset (new ThreadPlanStepOut(m_thread,
81 NULL,
82 false,
83 stop_others,
84 eVoteNoOpinion,
85 eVoteNoOpinion,
86 frame_idx - 1));
Jim Inghama5ce6c82011-10-15 00:57:28 +000087 }
88 else
89 {
90 // If we're already at the inlined frame we're stepping through, then just do that now.
91 QueueInlinedStepPlan(false);
92 }
93
94 }
95 else if (return_frame_sp)
96 {
97 // Find the return address and set a breakpoint there:
98 // FIXME - can we do this more securely if we know first_insn?
99
Greg Clayton1ac04c32012-02-21 00:09:25 +0000100 m_return_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess()->GetTarget());
Sean Callanan708709c2012-07-31 22:19:25 +0000101
102 if (m_return_addr == LLDB_INVALID_ADDRESS)
103 return;
104
Greg Clayton1ac04c32012-02-21 00:09:25 +0000105 Breakpoint *return_bp = m_thread.CalculateTarget()->CreateBreakpoint (m_return_addr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106 if (return_bp != NULL)
107 {
108 return_bp->SetThreadID(m_thread.GetID());
109 m_return_bp_id = return_bp->GetID();
110 }
Jim Ingham73ca05a2011-12-17 01:35:57 +0000111
112 if (immediate_return_from_sp)
113 {
114 const SymbolContext &sc = immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
115 if (sc.function)
116 {
117 m_immediate_step_from_function = sc.function;
118 }
119 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 }
121
Jim Inghama5ce6c82011-10-15 00:57:28 +0000122}
123
124void
125ThreadPlanStepOut::DidPush()
126{
127 if (m_step_out_plan_sp)
128 m_thread.QueueThreadPlan(m_step_out_plan_sp, false);
129 else if (m_step_through_inline_plan_sp)
130 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131}
132
133ThreadPlanStepOut::~ThreadPlanStepOut ()
134{
135 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000136 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137}
138
139void
140ThreadPlanStepOut::GetDescription (Stream *s, lldb::DescriptionLevel level)
141{
142 if (level == lldb::eDescriptionLevelBrief)
143 s->Printf ("step out");
144 else
145 {
Jim Inghama5ce6c82011-10-15 00:57:28 +0000146 if (m_step_out_plan_sp)
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000147 s->Printf ("Stepping out to inlined frame so we can walk through it.");
Jim Inghama5ce6c82011-10-15 00:57:28 +0000148 else if (m_step_through_inline_plan_sp)
149 s->Printf ("Stepping out by stepping through inlined function.");
150 else
Daniel Malead01b2952012-11-29 21:49:15 +0000151 s->Printf ("Stepping out from address 0x%" PRIx64 " to return address 0x%" PRIx64 " using breakpoint site %d",
Jim Inghama5ce6c82011-10-15 00:57:28 +0000152 (uint64_t)m_step_from_insn,
153 (uint64_t)m_return_addr,
Jim Inghama5ce6c82011-10-15 00:57:28 +0000154 m_return_bp_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 }
156}
157
158bool
159ThreadPlanStepOut::ValidatePlan (Stream *error)
160{
Jim Inghama5ce6c82011-10-15 00:57:28 +0000161 if (m_step_out_plan_sp)
162 return m_step_out_plan_sp->ValidatePlan (error);
163 else if (m_step_through_inline_plan_sp)
164 return m_step_through_inline_plan_sp->ValidatePlan (error);
165 else if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
166 {
Sean Callanan708709c2012-07-31 22:19:25 +0000167 if (error)
168 error->PutCString("Could not create return address breakpoint.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169 return false;
Jim Inghama5ce6c82011-10-15 00:57:28 +0000170 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 else
172 return true;
173}
174
175bool
176ThreadPlanStepOut::PlanExplainsStop ()
177{
Jim Inghama5ce6c82011-10-15 00:57:28 +0000178 // If one of our child plans just finished, then we do explain the stop.
179 if (m_step_out_plan_sp)
180 {
181 if (m_step_out_plan_sp->MischiefManaged())
182 {
183 // If this one is done, then we are all done.
Jim Ingham73ca05a2011-12-17 01:35:57 +0000184 CalculateReturnValue();
Jim Inghama5ce6c82011-10-15 00:57:28 +0000185 SetPlanComplete();
186 return true;
187 }
188 else
189 return false;
190 }
191 else if (m_step_through_inline_plan_sp)
192 {
193 if (m_step_through_inline_plan_sp->MischiefManaged())
194 return true;
195 else
196 return false;
197 }
198
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 // We don't explain signals or breakpoints (breakpoints that handle stepping in or
200 // out will be handled by a child plan.
Jim Inghama5ce6c82011-10-15 00:57:28 +0000201
Jim Inghamb15bfc72010-10-20 00:39:53 +0000202 StopInfoSP stop_info_sp = GetPrivateStopReason();
203 if (stop_info_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 {
Jim Inghamb15bfc72010-10-20 00:39:53 +0000205 StopReason reason = stop_info_sp->GetStopReason();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206 switch (reason)
207 {
Greg Claytonf4b47e12010-08-04 01:40:35 +0000208 case eStopReasonBreakpoint:
209 {
210 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000211 BreakpointSiteSP site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue()));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000212 if (site_sp && site_sp->IsBreakpointAtThisSite (m_return_bp_id))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213 {
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000214 bool done;
215
216 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
217
218 if (m_step_out_to_id == frame_zero_id)
219 done = true;
220 else if (m_step_out_to_id < frame_zero_id)
221 {
222 // Either we stepped past the breakpoint, or the stack ID calculation
223 // was incorrect and we should probably stop.
224 done = true;
225 }
226 else
227 {
228 if (m_immediate_step_from_id < frame_zero_id)
229 done = true;
230 else
231 done = false;
232 }
233
234 if (done)
Jim Ingham73ca05a2011-12-17 01:35:57 +0000235 {
236 CalculateReturnValue();
Greg Clayton481cef22011-01-21 06:11:58 +0000237 SetPlanComplete();
Jim Ingham73ca05a2011-12-17 01:35:57 +0000238 }
Greg Clayton481cef22011-01-21 06:11:58 +0000239
Greg Claytonf4b47e12010-08-04 01:40:35 +0000240 // If there was only one owner, then we're done. But if we also hit some
241 // user breakpoint on our way out, we should mark ourselves as done, but
242 // also not claim to explain the stop, since it is more important to report
243 // the user breakpoint than the step out completion.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244
Greg Claytonf4b47e12010-08-04 01:40:35 +0000245 if (site_sp->GetNumberOfOwners() == 1)
246 return true;
247
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000249 return false;
250 }
251 case eStopReasonWatchpoint:
252 case eStopReasonSignal:
253 case eStopReasonException:
Greg Clayton90ba8112012-12-05 00:16:59 +0000254 case eStopReasonExec:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000255 return false;
256
257 default:
258 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259 }
260 }
261 return true;
262}
263
264bool
265ThreadPlanStepOut::ShouldStop (Event *event_ptr)
266{
Jim Inghama5ce6c82011-10-15 00:57:28 +0000267 if (IsPlanComplete())
Jim Inghama5ce6c82011-10-15 00:57:28 +0000268 return true;
Jim Inghamb5c0d1c2012-03-01 00:50:50 +0000269
270 bool done;
271
272 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
273 if (frame_zero_id < m_step_out_to_id)
274 done = false;
275 else
276 done = true;
277
278 if (done)
Jim Inghama5ce6c82011-10-15 00:57:28 +0000279 {
Jim Ingham73ca05a2011-12-17 01:35:57 +0000280 CalculateReturnValue();
Jim Inghama5ce6c82011-10-15 00:57:28 +0000281 SetPlanComplete();
282 return true;
283 }
284 else
285 {
286 if (m_step_out_plan_sp)
287 {
288 if (m_step_out_plan_sp->MischiefManaged())
289 {
290 // Now step through the inlined stack we are in:
291 if (QueueInlinedStepPlan(true))
292 {
293 return false;
294 }
295 else
296 {
Jim Ingham73ca05a2011-12-17 01:35:57 +0000297 CalculateReturnValue();
Jim Inghama5ce6c82011-10-15 00:57:28 +0000298 SetPlanComplete ();
299 return true;
300 }
301 }
302 else
303 return m_step_out_plan_sp->ShouldStop(event_ptr);
304 }
305 else if (m_step_through_inline_plan_sp)
306 {
307 if (m_step_through_inline_plan_sp->MischiefManaged())
308 {
Jim Ingham73ca05a2011-12-17 01:35:57 +0000309 // We don't calculate the return value here because we don't know how to.
310 // But in case we had a return value sitting around from our process in
311 // getting here, let's clear it out.
312 m_return_valobj_sp.reset();
Jim Inghama5ce6c82011-10-15 00:57:28 +0000313 SetPlanComplete();
314 return true;
315 }
316 else
317 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
318 }
319 else
320 return false;
321 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322}
323
324bool
325ThreadPlanStepOut::StopOthers ()
326{
327 return m_stop_others;
328}
329
330StateType
Jim Ingham06e827c2010-11-11 19:26:09 +0000331ThreadPlanStepOut::GetPlanRunState ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332{
333 return eStateRunning;
334}
335
336bool
337ThreadPlanStepOut::WillResume (StateType resume_state, bool current_plan)
338{
339 ThreadPlan::WillResume (resume_state, current_plan);
Jim Inghama5ce6c82011-10-15 00:57:28 +0000340 if (m_step_out_plan_sp || m_step_through_inline_plan_sp)
341 return true;
342
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
344 return false;
345
346 if (current_plan)
347 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000348 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 if (return_bp != NULL)
350 return_bp->SetEnabled (true);
351 }
352 return true;
353}
354
355bool
356ThreadPlanStepOut::WillStop ()
357{
Jim Inghama5ce6c82011-10-15 00:57:28 +0000358 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
359 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000360 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
Jim Inghama5ce6c82011-10-15 00:57:28 +0000361 if (return_bp != NULL)
362 return_bp->SetEnabled (false);
363 }
364
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 return true;
366}
367
368bool
369ThreadPlanStepOut::MischiefManaged ()
370{
Jim Inghama5ce6c82011-10-15 00:57:28 +0000371 if (IsPlanComplete())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 {
373 // Did I reach my breakpoint? If so I'm done.
374 //
375 // I also check the stack depth, since if we've blown past the breakpoint for some
376 // reason and we're now stopping for some other reason altogether, then we're done
377 // with this step out operation.
378
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000379 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380 if (log)
381 log->Printf("Completed step out plan.");
Jim Inghama5ce6c82011-10-15 00:57:28 +0000382 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
383 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000384 m_thread.CalculateTarget()->RemoveBreakpointByID (m_return_bp_id);
Jim Inghama5ce6c82011-10-15 00:57:28 +0000385 m_return_bp_id = LLDB_INVALID_BREAK_ID;
386 }
387
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388 ThreadPlan::MischiefManaged ();
389 return true;
390 }
391 else
392 {
393 return false;
394 }
395}
396
Jim Inghama5ce6c82011-10-15 00:57:28 +0000397bool
398ThreadPlanStepOut::QueueInlinedStepPlan (bool queue_now)
399{
400 // Now figure out the range of this inlined block, and set up a "step through range"
401 // plan for that. If we've been provided with a context, then use the block in that
402 // context.
403 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (0));
404 if (!immediate_return_from_sp)
405 return false;
406
407 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
408 if (log)
409 {
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 {
418 Block *inlined_block = from_block->GetContainingInlinedBlock();
419 if (inlined_block)
420 {
421 size_t num_ranges = inlined_block->GetNumRanges();
422 AddressRange inline_range;
423 if (inlined_block->GetRangeAtIndex(0, inline_range))
424 {
425 SymbolContext inlined_sc;
426 inlined_block->CalculateSymbolContext(&inlined_sc);
Jim Ingham5f1a4e12012-07-26 18:23:21 +0000427 inlined_sc.target_sp = GetTarget().shared_from_this();
Jim Inghama5ce6c82011-10-15 00:57:28 +0000428 RunMode run_mode = m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
429 ThreadPlanStepOverRange *step_through_inline_plan_ptr = new ThreadPlanStepOverRange(m_thread,
430 inline_range,
431 inlined_sc,
432 run_mode);
433 step_through_inline_plan_ptr->SetOkayToDiscard(true);
434 StreamString errors;
435 if (!step_through_inline_plan_ptr->ValidatePlan(&errors))
436 {
437 //FIXME: Log this failure.
438 delete step_through_inline_plan_ptr;
439 return false;
440 }
441
442 for (size_t i = 1; i < num_ranges; i++)
443 {
444 if (inlined_block->GetRangeAtIndex (i, inline_range))
445 step_through_inline_plan_ptr->AddRange (inline_range);
446 }
447 m_step_through_inline_plan_sp.reset (step_through_inline_plan_ptr);
448 if (queue_now)
449 m_thread.QueueThreadPlan (m_step_through_inline_plan_sp, false);
450 return true;
451 }
452 }
453 }
454
455 return false;
456}
Jim Ingham73ca05a2011-12-17 01:35:57 +0000457
458void
459ThreadPlanStepOut::CalculateReturnValue ()
460{
461 if (m_return_valobj_sp)
462 return;
463
464 if (m_immediate_step_from_function != NULL)
465 {
466 Type *return_type = m_immediate_step_from_function->GetType();
467 lldb::clang_type_t return_clang_type = m_immediate_step_from_function->GetReturnClangType();
468 if (return_type && return_clang_type)
469 {
470 ClangASTType ast_type (return_type->GetClangAST(), return_clang_type);
471
Greg Clayton1ac04c32012-02-21 00:09:25 +0000472 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
Jim Ingham73ca05a2011-12-17 01:35:57 +0000473 if (abi_sp)
474 {
475 m_return_valobj_sp = abi_sp->GetReturnValueObject(m_thread, ast_type);
476 }
477 }
478 }
479}
Jim Ingham64e7ead2012-05-03 21:19:36 +0000480
481bool
482ThreadPlanStepOut::IsPlanStale()
483{
484 // If we are still lower on the stack than the frame we are returning to, then
485 // there's something for us to do. Otherwise, we're stale.
486
487 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
488 if (frame_zero_id < m_step_out_to_id)
489 return false;
490 else
491 return true;
492}
493