blob: 58507a80e601cf52f6898b880c3603866dd1ca32 [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
Sean Callanancebfad12012-03-13 16:34:56 +000060 if (!return_frame_sp || !immediate_return_from_sp)
61 return; // we can't do anything here. ValidatePlan() will return false.
62
Jim Ingham441e3b92012-03-01 00:50:50 +000063 m_step_out_to_id = return_frame_sp->GetStackID();
64 m_immediate_step_from_id = immediate_return_from_sp->GetStackID();
65
66 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
Jim Ingham43d39062011-10-15 00:57:28 +000067
68 // If the frame directly below the one we are returning to is inlined, we have to be
69 // a little more careful. It is non-trivial to determine the real "return code address" for
70 // an inlined frame, so we have to work our way to that frame and then step out.
71 if (immediate_return_from_sp && immediate_return_from_sp->IsInlined())
Chris Lattner24943d22010-06-08 16:52:24 +000072 {
Jim Ingham43d39062011-10-15 00:57:28 +000073 if (frame_idx > 0)
74 {
75 // First queue a plan that gets us to this inlined frame, and when we get there we'll queue a second
76 // plan that walks us out of this frame.
Jim Ingham441e3b92012-03-01 00:50:50 +000077 m_step_out_plan_sp.reset (new ThreadPlanStepOut(m_thread,
78 NULL,
79 false,
80 stop_others,
81 eVoteNoOpinion,
82 eVoteNoOpinion,
83 frame_idx - 1));
Jim Ingham43d39062011-10-15 00:57:28 +000084 m_step_out_plan_sp->SetOkayToDiscard(true);
85 }
86 else
87 {
88 // If we're already at the inlined frame we're stepping through, then just do that now.
89 QueueInlinedStepPlan(false);
90 }
91
92 }
93 else if (return_frame_sp)
94 {
95 // Find the return address and set a breakpoint there:
96 // FIXME - can we do this more securely if we know first_insn?
97
Greg Claytonf4124de2012-02-21 00:09:25 +000098 m_return_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess()->GetTarget());
99 Breakpoint *return_bp = m_thread.CalculateTarget()->CreateBreakpoint (m_return_addr, true).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000100 if (return_bp != NULL)
101 {
102 return_bp->SetThreadID(m_thread.GetID());
103 m_return_bp_id = return_bp->GetID();
104 }
Jim Ingham1586d972011-12-17 01:35:57 +0000105
106 if (immediate_return_from_sp)
107 {
108 const SymbolContext &sc = immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
109 if (sc.function)
110 {
111 m_immediate_step_from_function = sc.function;
112 }
113 }
Chris Lattner24943d22010-06-08 16:52:24 +0000114 }
115
Jim Ingham43d39062011-10-15 00:57:28 +0000116}
117
118void
119ThreadPlanStepOut::DidPush()
120{
121 if (m_step_out_plan_sp)
122 m_thread.QueueThreadPlan(m_step_out_plan_sp, false);
123 else if (m_step_through_inline_plan_sp)
124 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000125}
126
127ThreadPlanStepOut::~ThreadPlanStepOut ()
128{
129 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
Greg Claytonf4124de2012-02-21 00:09:25 +0000130 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000131}
132
133void
134ThreadPlanStepOut::GetDescription (Stream *s, lldb::DescriptionLevel level)
135{
136 if (level == lldb::eDescriptionLevelBrief)
137 s->Printf ("step out");
138 else
139 {
Jim Ingham43d39062011-10-15 00:57:28 +0000140 if (m_step_out_plan_sp)
Jim Ingham441e3b92012-03-01 00:50:50 +0000141 s->Printf ("Stepping out to inlined frame so we can walk through it.");
Jim Ingham43d39062011-10-15 00:57:28 +0000142 else if (m_step_through_inline_plan_sp)
143 s->Printf ("Stepping out by stepping through inlined function.");
144 else
Jim Ingham441e3b92012-03-01 00:50:50 +0000145 s->Printf ("Stepping out from address 0x%llx to return address 0x%llx using breakpoint site %d",
Jim Ingham43d39062011-10-15 00:57:28 +0000146 (uint64_t)m_step_from_insn,
147 (uint64_t)m_return_addr,
Jim Ingham43d39062011-10-15 00:57:28 +0000148 m_return_bp_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000149 }
150}
151
152bool
153ThreadPlanStepOut::ValidatePlan (Stream *error)
154{
Jim Ingham43d39062011-10-15 00:57:28 +0000155 if (m_step_out_plan_sp)
156 return m_step_out_plan_sp->ValidatePlan (error);
157 else if (m_step_through_inline_plan_sp)
158 return m_step_through_inline_plan_sp->ValidatePlan (error);
159 else if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
160 {
161 error->PutCString("Could not create return address breakpoint.");
Chris Lattner24943d22010-06-08 16:52:24 +0000162 return false;
Jim Ingham43d39062011-10-15 00:57:28 +0000163 }
Chris Lattner24943d22010-06-08 16:52:24 +0000164 else
165 return true;
166}
167
168bool
169ThreadPlanStepOut::PlanExplainsStop ()
170{
Jim Ingham43d39062011-10-15 00:57:28 +0000171 // If one of our child plans just finished, then we do explain the stop.
172 if (m_step_out_plan_sp)
173 {
174 if (m_step_out_plan_sp->MischiefManaged())
175 {
176 // If this one is done, then we are all done.
Jim Ingham1586d972011-12-17 01:35:57 +0000177 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000178 SetPlanComplete();
179 return true;
180 }
181 else
182 return false;
183 }
184 else if (m_step_through_inline_plan_sp)
185 {
186 if (m_step_through_inline_plan_sp->MischiefManaged())
187 return true;
188 else
189 return false;
190 }
191
Chris Lattner24943d22010-06-08 16:52:24 +0000192 // We don't explain signals or breakpoints (breakpoints that handle stepping in or
193 // out will be handled by a child plan.
Jim Ingham43d39062011-10-15 00:57:28 +0000194
Jim Ingham6297a3a2010-10-20 00:39:53 +0000195 StopInfoSP stop_info_sp = GetPrivateStopReason();
196 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000197 {
Jim Ingham6297a3a2010-10-20 00:39:53 +0000198 StopReason reason = stop_info_sp->GetStopReason();
Chris Lattner24943d22010-06-08 16:52:24 +0000199 switch (reason)
200 {
Greg Clayton643ee732010-08-04 01:40:35 +0000201 case eStopReasonBreakpoint:
202 {
203 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
Greg Claytonf4124de2012-02-21 00:09:25 +0000204 BreakpointSiteSP site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue()));
Greg Clayton643ee732010-08-04 01:40:35 +0000205 if (site_sp && site_sp->IsBreakpointAtThisSite (m_return_bp_id))
Chris Lattner24943d22010-06-08 16:52:24 +0000206 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000207 bool done;
208
209 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
210
211 if (m_step_out_to_id == frame_zero_id)
212 done = true;
213 else if (m_step_out_to_id < frame_zero_id)
214 {
215 // Either we stepped past the breakpoint, or the stack ID calculation
216 // was incorrect and we should probably stop.
217 done = true;
218 }
219 else
220 {
221 if (m_immediate_step_from_id < frame_zero_id)
222 done = true;
223 else
224 done = false;
225 }
226
227 if (done)
Jim Ingham1586d972011-12-17 01:35:57 +0000228 {
229 CalculateReturnValue();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000230 SetPlanComplete();
Jim Ingham1586d972011-12-17 01:35:57 +0000231 }
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000232
Greg Clayton643ee732010-08-04 01:40:35 +0000233 // If there was only one owner, then we're done. But if we also hit some
234 // user breakpoint on our way out, we should mark ourselves as done, but
235 // also not claim to explain the stop, since it is more important to report
236 // the user breakpoint than the step out completion.
Chris Lattner24943d22010-06-08 16:52:24 +0000237
Greg Clayton643ee732010-08-04 01:40:35 +0000238 if (site_sp->GetNumberOfOwners() == 1)
239 return true;
240
Chris Lattner24943d22010-06-08 16:52:24 +0000241 }
Greg Clayton643ee732010-08-04 01:40:35 +0000242 return false;
243 }
244 case eStopReasonWatchpoint:
245 case eStopReasonSignal:
246 case eStopReasonException:
247 return false;
248
249 default:
250 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000251 }
252 }
253 return true;
254}
255
256bool
257ThreadPlanStepOut::ShouldStop (Event *event_ptr)
258{
Jim Ingham43d39062011-10-15 00:57:28 +0000259 if (IsPlanComplete())
Jim Ingham43d39062011-10-15 00:57:28 +0000260 return true;
Jim Ingham441e3b92012-03-01 00:50:50 +0000261
262 bool done;
263
264 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
265 if (frame_zero_id < m_step_out_to_id)
266 done = false;
267 else
268 done = true;
269
270 if (done)
Jim Ingham43d39062011-10-15 00:57:28 +0000271 {
Jim Ingham1586d972011-12-17 01:35:57 +0000272 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000273 SetPlanComplete();
274 return true;
275 }
276 else
277 {
278 if (m_step_out_plan_sp)
279 {
280 if (m_step_out_plan_sp->MischiefManaged())
281 {
282 // Now step through the inlined stack we are in:
283 if (QueueInlinedStepPlan(true))
284 {
285 return false;
286 }
287 else
288 {
Jim Ingham1586d972011-12-17 01:35:57 +0000289 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000290 SetPlanComplete ();
291 return true;
292 }
293 }
294 else
295 return m_step_out_plan_sp->ShouldStop(event_ptr);
296 }
297 else if (m_step_through_inline_plan_sp)
298 {
299 if (m_step_through_inline_plan_sp->MischiefManaged())
300 {
Jim Ingham1586d972011-12-17 01:35:57 +0000301 // We don't calculate the return value here because we don't know how to.
302 // But in case we had a return value sitting around from our process in
303 // getting here, let's clear it out.
304 m_return_valobj_sp.reset();
Jim Ingham43d39062011-10-15 00:57:28 +0000305 SetPlanComplete();
306 return true;
307 }
308 else
309 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
310 }
311 else
312 return false;
313 }
Chris Lattner24943d22010-06-08 16:52:24 +0000314}
315
316bool
317ThreadPlanStepOut::StopOthers ()
318{
319 return m_stop_others;
320}
321
322StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000323ThreadPlanStepOut::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000324{
325 return eStateRunning;
326}
327
328bool
329ThreadPlanStepOut::WillResume (StateType resume_state, bool current_plan)
330{
331 ThreadPlan::WillResume (resume_state, current_plan);
Jim Ingham43d39062011-10-15 00:57:28 +0000332 if (m_step_out_plan_sp || m_step_through_inline_plan_sp)
333 return true;
334
Chris Lattner24943d22010-06-08 16:52:24 +0000335 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
336 return false;
337
338 if (current_plan)
339 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000340 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000341 if (return_bp != NULL)
342 return_bp->SetEnabled (true);
343 }
344 return true;
345}
346
347bool
348ThreadPlanStepOut::WillStop ()
349{
Jim Ingham43d39062011-10-15 00:57:28 +0000350 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
351 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000352 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
Jim Ingham43d39062011-10-15 00:57:28 +0000353 if (return_bp != NULL)
354 return_bp->SetEnabled (false);
355 }
356
Chris Lattner24943d22010-06-08 16:52:24 +0000357 return true;
358}
359
360bool
361ThreadPlanStepOut::MischiefManaged ()
362{
Jim Ingham43d39062011-10-15 00:57:28 +0000363 if (IsPlanComplete())
Chris Lattner24943d22010-06-08 16:52:24 +0000364 {
365 // Did I reach my breakpoint? If so I'm done.
366 //
367 // I also check the stack depth, since if we've blown past the breakpoint for some
368 // reason and we're now stopping for some other reason altogether, then we're done
369 // with this step out operation.
370
Greg Claytone005f2c2010-11-06 01:53:30 +0000371 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000372 if (log)
373 log->Printf("Completed step out plan.");
Jim Ingham43d39062011-10-15 00:57:28 +0000374 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
375 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000376 m_thread.CalculateTarget()->RemoveBreakpointByID (m_return_bp_id);
Jim Ingham43d39062011-10-15 00:57:28 +0000377 m_return_bp_id = LLDB_INVALID_BREAK_ID;
378 }
379
Chris Lattner24943d22010-06-08 16:52:24 +0000380 ThreadPlan::MischiefManaged ();
381 return true;
382 }
383 else
384 {
385 return false;
386 }
387}
388
Jim Ingham43d39062011-10-15 00:57:28 +0000389bool
390ThreadPlanStepOut::QueueInlinedStepPlan (bool queue_now)
391{
392 // Now figure out the range of this inlined block, and set up a "step through range"
393 // plan for that. If we've been provided with a context, then use the block in that
394 // context.
395 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (0));
396 if (!immediate_return_from_sp)
397 return false;
398
399 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
400 if (log)
401 {
402 StreamString s;
403 immediate_return_from_sp->Dump(&s, true, false);
404 log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
405 }
406
407 Block *from_block = immediate_return_from_sp->GetFrameBlock();
408 if (from_block)
409 {
410 Block *inlined_block = from_block->GetContainingInlinedBlock();
411 if (inlined_block)
412 {
413 size_t num_ranges = inlined_block->GetNumRanges();
414 AddressRange inline_range;
415 if (inlined_block->GetRangeAtIndex(0, inline_range))
416 {
417 SymbolContext inlined_sc;
418 inlined_block->CalculateSymbolContext(&inlined_sc);
419 RunMode run_mode = m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
420 ThreadPlanStepOverRange *step_through_inline_plan_ptr = new ThreadPlanStepOverRange(m_thread,
421 inline_range,
422 inlined_sc,
423 run_mode);
424 step_through_inline_plan_ptr->SetOkayToDiscard(true);
425 StreamString errors;
426 if (!step_through_inline_plan_ptr->ValidatePlan(&errors))
427 {
428 //FIXME: Log this failure.
429 delete step_through_inline_plan_ptr;
430 return false;
431 }
432
433 for (size_t i = 1; i < num_ranges; i++)
434 {
435 if (inlined_block->GetRangeAtIndex (i, inline_range))
436 step_through_inline_plan_ptr->AddRange (inline_range);
437 }
438 m_step_through_inline_plan_sp.reset (step_through_inline_plan_ptr);
439 if (queue_now)
440 m_thread.QueueThreadPlan (m_step_through_inline_plan_sp, false);
441 return true;
442 }
443 }
444 }
445
446 return false;
447}
Jim Ingham1586d972011-12-17 01:35:57 +0000448
449void
450ThreadPlanStepOut::CalculateReturnValue ()
451{
452 if (m_return_valobj_sp)
453 return;
454
455 if (m_immediate_step_from_function != NULL)
456 {
457 Type *return_type = m_immediate_step_from_function->GetType();
458 lldb::clang_type_t return_clang_type = m_immediate_step_from_function->GetReturnClangType();
459 if (return_type && return_clang_type)
460 {
461 ClangASTType ast_type (return_type->GetClangAST(), return_clang_type);
462
Greg Claytonf4124de2012-02-21 00:09:25 +0000463 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
Jim Ingham1586d972011-12-17 01:35:57 +0000464 if (abi_sp)
465 {
466 m_return_valobj_sp = abi_sp->GetReturnValueObject(m_thread, ast_type);
467 }
468 }
469 }
470}