blob: 1b2f751f1824efb8332925a86825f19c580c4396 [file] [log] [blame]
Chris Lattner24943d22010-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 Ingham1586d972011-12-17 01:35:57 +000019#include "lldb/Core/Value.h"
20#include "lldb/Core/ValueObjectConstResult.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Target/Process.h"
22#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000023#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Target/Target.h"
Jim Ingham43d39062011-10-15 00:57:28 +000025#include "lldb/Target/ThreadPlanStepOverRange.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026
27using namespace lldb;
28using namespace lldb_private;
29
30//----------------------------------------------------------------------
31// ThreadPlanStepOut: Step out of the current frame
32//----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000033ThreadPlanStepOut::ThreadPlanStepOut
34(
35 Thread &thread,
36 SymbolContext *context,
37 bool first_insn,
38 bool stop_others,
39 Vote stop_vote,
Greg Clayton1ebdcc72011-01-21 06:11:58 +000040 Vote run_vote,
41 uint32_t frame_idx
Chris Lattner24943d22010-06-08 16:52:24 +000042) :
Jim Ingham5a47e8b2010-06-19 04:45:32 +000043 ThreadPlan (ThreadPlan::eKindStepOut, "Step out", thread, stop_vote, run_vote),
Chris Lattner24943d22010-06-08 16:52:24 +000044 m_step_from_context (context),
45 m_step_from_insn (LLDB_INVALID_ADDRESS),
Benjamin Kramer36a08102010-07-16 12:32:33 +000046 m_return_bp_id (LLDB_INVALID_BREAK_ID),
Chris Lattner24943d22010-06-08 16:52:24 +000047 m_return_addr (LLDB_INVALID_ADDRESS),
48 m_first_insn (first_insn),
Jim Ingham43d39062011-10-15 00:57:28 +000049 m_stop_others (stop_others),
50 m_step_through_inline_plan_sp(),
Jim Ingham1586d972011-12-17 01:35:57 +000051 m_step_out_plan_sp (),
52 m_immediate_step_from_function(NULL)
Jim Ingham43d39062011-10-15 00:57:28 +000053
Chris Lattner24943d22010-06-08 16:52:24 +000054{
55 m_step_from_insn = m_thread.GetRegisterContext()->GetPC(0);
56
Greg Clayton1ebdcc72011-01-21 06:11:58 +000057 StackFrameSP return_frame_sp (m_thread.GetStackFrameAtIndex(frame_idx + 1));
Jim Ingham43d39062011-10-15 00:57:28 +000058 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (frame_idx));
59
Jim Ingham441e3b92012-03-01 00:50:50 +000060 m_step_out_to_id = return_frame_sp->GetStackID();
61 m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
62
63 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
Jim Ingham43d39062011-10-15 00:57:28 +000064
65 // If the frame directly below the one we are returning to is inlined, we have to be
66 // a little more careful. It is non-trivial to determine the real "return code address" for
67 // an inlined frame, so we have to work our way to that frame and then step out.
68 if (immediate_return_from_sp && immediate_return_from_sp->IsInlined())
Chris Lattner24943d22010-06-08 16:52:24 +000069 {
Jim Ingham43d39062011-10-15 00:57:28 +000070 if (frame_idx > 0)
71 {
72 // First queue a plan that gets us to this inlined frame, and when we get there we'll queue a second
73 // plan that walks us out of this frame.
Jim Ingham441e3b92012-03-01 00:50:50 +000074 m_step_out_plan_sp.reset (new ThreadPlanStepOut(m_thread,
75 NULL,
76 false,
77 stop_others,
78 eVoteNoOpinion,
79 eVoteNoOpinion,
80 frame_idx - 1));
Jim Ingham43d39062011-10-15 00:57:28 +000081 m_step_out_plan_sp->SetOkayToDiscard(true);
82 }
83 else
84 {
85 // If we're already at the inlined frame we're stepping through, then just do that now.
86 QueueInlinedStepPlan(false);
87 }
88
89 }
90 else if (return_frame_sp)
91 {
92 // Find the return address and set a breakpoint there:
93 // FIXME - can we do this more securely if we know first_insn?
94
Greg Claytonf4124de2012-02-21 00:09:25 +000095 m_return_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess()->GetTarget());
96 Breakpoint *return_bp = m_thread.CalculateTarget()->CreateBreakpoint (m_return_addr, true).get();
Chris Lattner24943d22010-06-08 16:52:24 +000097 if (return_bp != NULL)
98 {
99 return_bp->SetThreadID(m_thread.GetID());
100 m_return_bp_id = return_bp->GetID();
101 }
Jim Ingham1586d972011-12-17 01:35:57 +0000102
103 if (immediate_return_from_sp)
104 {
105 const SymbolContext &sc = immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
106 if (sc.function)
107 {
108 m_immediate_step_from_function = sc.function;
109 }
110 }
Chris Lattner24943d22010-06-08 16:52:24 +0000111 }
112
Jim Ingham43d39062011-10-15 00:57:28 +0000113}
114
115void
116ThreadPlanStepOut::DidPush()
117{
118 if (m_step_out_plan_sp)
119 m_thread.QueueThreadPlan(m_step_out_plan_sp, false);
120 else if (m_step_through_inline_plan_sp)
121 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000122}
123
124ThreadPlanStepOut::~ThreadPlanStepOut ()
125{
126 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
Greg Claytonf4124de2012-02-21 00:09:25 +0000127 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000128}
129
130void
131ThreadPlanStepOut::GetDescription (Stream *s, lldb::DescriptionLevel level)
132{
133 if (level == lldb::eDescriptionLevelBrief)
134 s->Printf ("step out");
135 else
136 {
Jim Ingham43d39062011-10-15 00:57:28 +0000137 if (m_step_out_plan_sp)
Jim Ingham441e3b92012-03-01 00:50:50 +0000138 s->Printf ("Stepping out to inlined frame so we can walk through it.");
Jim Ingham43d39062011-10-15 00:57:28 +0000139 else if (m_step_through_inline_plan_sp)
140 s->Printf ("Stepping out by stepping through inlined function.");
141 else
Jim Ingham441e3b92012-03-01 00:50:50 +0000142 s->Printf ("Stepping out from address 0x%llx to return address 0x%llx using breakpoint site %d",
Jim Ingham43d39062011-10-15 00:57:28 +0000143 (uint64_t)m_step_from_insn,
144 (uint64_t)m_return_addr,
Jim Ingham43d39062011-10-15 00:57:28 +0000145 m_return_bp_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000146 }
147}
148
149bool
150ThreadPlanStepOut::ValidatePlan (Stream *error)
151{
Jim Ingham43d39062011-10-15 00:57:28 +0000152 if (m_step_out_plan_sp)
153 return m_step_out_plan_sp->ValidatePlan (error);
154 else if (m_step_through_inline_plan_sp)
155 return m_step_through_inline_plan_sp->ValidatePlan (error);
156 else if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
157 {
158 error->PutCString("Could not create return address breakpoint.");
Chris Lattner24943d22010-06-08 16:52:24 +0000159 return false;
Jim Ingham43d39062011-10-15 00:57:28 +0000160 }
Chris Lattner24943d22010-06-08 16:52:24 +0000161 else
162 return true;
163}
164
165bool
166ThreadPlanStepOut::PlanExplainsStop ()
167{
Jim Ingham43d39062011-10-15 00:57:28 +0000168 // If one of our child plans just finished, then we do explain the stop.
169 if (m_step_out_plan_sp)
170 {
171 if (m_step_out_plan_sp->MischiefManaged())
172 {
173 // If this one is done, then we are all done.
Jim Ingham1586d972011-12-17 01:35:57 +0000174 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000175 SetPlanComplete();
176 return true;
177 }
178 else
179 return false;
180 }
181 else if (m_step_through_inline_plan_sp)
182 {
183 if (m_step_through_inline_plan_sp->MischiefManaged())
184 return true;
185 else
186 return false;
187 }
188
Chris Lattner24943d22010-06-08 16:52:24 +0000189 // We don't explain signals or breakpoints (breakpoints that handle stepping in or
190 // out will be handled by a child plan.
Jim Ingham43d39062011-10-15 00:57:28 +0000191
Jim Ingham6297a3a2010-10-20 00:39:53 +0000192 StopInfoSP stop_info_sp = GetPrivateStopReason();
193 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000194 {
Jim Ingham6297a3a2010-10-20 00:39:53 +0000195 StopReason reason = stop_info_sp->GetStopReason();
Chris Lattner24943d22010-06-08 16:52:24 +0000196 switch (reason)
197 {
Greg Clayton643ee732010-08-04 01:40:35 +0000198 case eStopReasonBreakpoint:
199 {
200 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
Greg Claytonf4124de2012-02-21 00:09:25 +0000201 BreakpointSiteSP site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue()));
Greg Clayton643ee732010-08-04 01:40:35 +0000202 if (site_sp && site_sp->IsBreakpointAtThisSite (m_return_bp_id))
Chris Lattner24943d22010-06-08 16:52:24 +0000203 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000204 bool done;
205
206 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
207
208 if (m_step_out_to_id == frame_zero_id)
209 done = true;
210 else if (m_step_out_to_id < frame_zero_id)
211 {
212 // Either we stepped past the breakpoint, or the stack ID calculation
213 // was incorrect and we should probably stop.
214 done = true;
215 }
216 else
217 {
218 if (m_immediate_step_from_id < frame_zero_id)
219 done = true;
220 else
221 done = false;
222 }
223
224 if (done)
Jim Ingham1586d972011-12-17 01:35:57 +0000225 {
226 CalculateReturnValue();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000227 SetPlanComplete();
Jim Ingham1586d972011-12-17 01:35:57 +0000228 }
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000229
Greg Clayton643ee732010-08-04 01:40:35 +0000230 // If there was only one owner, then we're done. But if we also hit some
231 // user breakpoint on our way out, we should mark ourselves as done, but
232 // also not claim to explain the stop, since it is more important to report
233 // the user breakpoint than the step out completion.
Chris Lattner24943d22010-06-08 16:52:24 +0000234
Greg Clayton643ee732010-08-04 01:40:35 +0000235 if (site_sp->GetNumberOfOwners() == 1)
236 return true;
237
Chris Lattner24943d22010-06-08 16:52:24 +0000238 }
Greg Clayton643ee732010-08-04 01:40:35 +0000239 return false;
240 }
241 case eStopReasonWatchpoint:
242 case eStopReasonSignal:
243 case eStopReasonException:
244 return false;
245
246 default:
247 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000248 }
249 }
250 return true;
251}
252
253bool
254ThreadPlanStepOut::ShouldStop (Event *event_ptr)
255{
Jim Ingham43d39062011-10-15 00:57:28 +0000256 if (IsPlanComplete())
Jim Ingham43d39062011-10-15 00:57:28 +0000257 return true;
Jim Ingham441e3b92012-03-01 00:50:50 +0000258
259 bool done;
260
261 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
262 if (frame_zero_id < m_step_out_to_id)
263 done = false;
264 else
265 done = true;
266
267 if (done)
Jim Ingham43d39062011-10-15 00:57:28 +0000268 {
Jim Ingham1586d972011-12-17 01:35:57 +0000269 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000270 SetPlanComplete();
271 return true;
272 }
273 else
274 {
275 if (m_step_out_plan_sp)
276 {
277 if (m_step_out_plan_sp->MischiefManaged())
278 {
279 // Now step through the inlined stack we are in:
280 if (QueueInlinedStepPlan(true))
281 {
282 return false;
283 }
284 else
285 {
Jim Ingham1586d972011-12-17 01:35:57 +0000286 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000287 SetPlanComplete ();
288 return true;
289 }
290 }
291 else
292 return m_step_out_plan_sp->ShouldStop(event_ptr);
293 }
294 else if (m_step_through_inline_plan_sp)
295 {
296 if (m_step_through_inline_plan_sp->MischiefManaged())
297 {
Jim Ingham1586d972011-12-17 01:35:57 +0000298 // We don't calculate the return value here because we don't know how to.
299 // But in case we had a return value sitting around from our process in
300 // getting here, let's clear it out.
301 m_return_valobj_sp.reset();
Jim Ingham43d39062011-10-15 00:57:28 +0000302 SetPlanComplete();
303 return true;
304 }
305 else
306 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
307 }
308 else
309 return false;
310 }
Chris Lattner24943d22010-06-08 16:52:24 +0000311}
312
313bool
314ThreadPlanStepOut::StopOthers ()
315{
316 return m_stop_others;
317}
318
319StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000320ThreadPlanStepOut::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000321{
322 return eStateRunning;
323}
324
325bool
326ThreadPlanStepOut::WillResume (StateType resume_state, bool current_plan)
327{
328 ThreadPlan::WillResume (resume_state, current_plan);
Jim Ingham43d39062011-10-15 00:57:28 +0000329 if (m_step_out_plan_sp || m_step_through_inline_plan_sp)
330 return true;
331
Chris Lattner24943d22010-06-08 16:52:24 +0000332 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
333 return false;
334
335 if (current_plan)
336 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000337 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000338 if (return_bp != NULL)
339 return_bp->SetEnabled (true);
340 }
341 return true;
342}
343
344bool
345ThreadPlanStepOut::WillStop ()
346{
Jim Ingham43d39062011-10-15 00:57:28 +0000347 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
348 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000349 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
Jim Ingham43d39062011-10-15 00:57:28 +0000350 if (return_bp != NULL)
351 return_bp->SetEnabled (false);
352 }
353
Chris Lattner24943d22010-06-08 16:52:24 +0000354 return true;
355}
356
357bool
358ThreadPlanStepOut::MischiefManaged ()
359{
Jim Ingham43d39062011-10-15 00:57:28 +0000360 if (IsPlanComplete())
Chris Lattner24943d22010-06-08 16:52:24 +0000361 {
362 // Did I reach my breakpoint? If so I'm done.
363 //
364 // I also check the stack depth, since if we've blown past the breakpoint for some
365 // reason and we're now stopping for some other reason altogether, then we're done
366 // with this step out operation.
367
Greg Claytone005f2c2010-11-06 01:53:30 +0000368 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000369 if (log)
370 log->Printf("Completed step out plan.");
Jim Ingham43d39062011-10-15 00:57:28 +0000371 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
372 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000373 m_thread.CalculateTarget()->RemoveBreakpointByID (m_return_bp_id);
Jim Ingham43d39062011-10-15 00:57:28 +0000374 m_return_bp_id = LLDB_INVALID_BREAK_ID;
375 }
376
Chris Lattner24943d22010-06-08 16:52:24 +0000377 ThreadPlan::MischiefManaged ();
378 return true;
379 }
380 else
381 {
382 return false;
383 }
384}
385
Jim Ingham43d39062011-10-15 00:57:28 +0000386bool
387ThreadPlanStepOut::QueueInlinedStepPlan (bool queue_now)
388{
389 // Now figure out the range of this inlined block, and set up a "step through range"
390 // plan for that. If we've been provided with a context, then use the block in that
391 // context.
392 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (0));
393 if (!immediate_return_from_sp)
394 return false;
395
396 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
397 if (log)
398 {
399 StreamString s;
400 immediate_return_from_sp->Dump(&s, true, false);
401 log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
402 }
403
404 Block *from_block = immediate_return_from_sp->GetFrameBlock();
405 if (from_block)
406 {
407 Block *inlined_block = from_block->GetContainingInlinedBlock();
408 if (inlined_block)
409 {
410 size_t num_ranges = inlined_block->GetNumRanges();
411 AddressRange inline_range;
412 if (inlined_block->GetRangeAtIndex(0, inline_range))
413 {
414 SymbolContext inlined_sc;
415 inlined_block->CalculateSymbolContext(&inlined_sc);
416 RunMode run_mode = m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
417 ThreadPlanStepOverRange *step_through_inline_plan_ptr = new ThreadPlanStepOverRange(m_thread,
418 inline_range,
419 inlined_sc,
420 run_mode);
421 step_through_inline_plan_ptr->SetOkayToDiscard(true);
422 StreamString errors;
423 if (!step_through_inline_plan_ptr->ValidatePlan(&errors))
424 {
425 //FIXME: Log this failure.
426 delete step_through_inline_plan_ptr;
427 return false;
428 }
429
430 for (size_t i = 1; i < num_ranges; i++)
431 {
432 if (inlined_block->GetRangeAtIndex (i, inline_range))
433 step_through_inline_plan_ptr->AddRange (inline_range);
434 }
435 m_step_through_inline_plan_sp.reset (step_through_inline_plan_ptr);
436 if (queue_now)
437 m_thread.QueueThreadPlan (m_step_through_inline_plan_sp, false);
438 return true;
439 }
440 }
441 }
442
443 return false;
444}
Jim Ingham1586d972011-12-17 01:35:57 +0000445
446void
447ThreadPlanStepOut::CalculateReturnValue ()
448{
449 if (m_return_valobj_sp)
450 return;
451
452 if (m_immediate_step_from_function != NULL)
453 {
454 Type *return_type = m_immediate_step_from_function->GetType();
455 lldb::clang_type_t return_clang_type = m_immediate_step_from_function->GetReturnClangType();
456 if (return_type && return_clang_type)
457 {
458 ClangASTType ast_type (return_type->GetClangAST(), return_clang_type);
459
Greg Claytonf4124de2012-02-21 00:09:25 +0000460 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
Jim Ingham1586d972011-12-17 01:35:57 +0000461 if (abi_sp)
462 {
463 m_return_valobj_sp = abi_sp->GetReturnValueObject(m_thread, ast_type);
464 }
465 }
466 }
467}