blob: 9b0e32040080846e39d3af07b0ab8e8fc937e687 [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 }
85 else
86 {
87 // If we're already at the inlined frame we're stepping through, then just do that now.
88 QueueInlinedStepPlan(false);
89 }
90
91 }
92 else if (return_frame_sp)
93 {
94 // Find the return address and set a breakpoint there:
95 // FIXME - can we do this more securely if we know first_insn?
96
Greg Claytonf4124de2012-02-21 00:09:25 +000097 m_return_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess()->GetTarget());
98 Breakpoint *return_bp = m_thread.CalculateTarget()->CreateBreakpoint (m_return_addr, true).get();
Chris Lattner24943d22010-06-08 16:52:24 +000099 if (return_bp != NULL)
100 {
101 return_bp->SetThreadID(m_thread.GetID());
102 m_return_bp_id = return_bp->GetID();
103 }
Jim Ingham1586d972011-12-17 01:35:57 +0000104
105 if (immediate_return_from_sp)
106 {
107 const SymbolContext &sc = immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
108 if (sc.function)
109 {
110 m_immediate_step_from_function = sc.function;
111 }
112 }
Chris Lattner24943d22010-06-08 16:52:24 +0000113 }
114
Jim Ingham43d39062011-10-15 00:57:28 +0000115}
116
117void
118ThreadPlanStepOut::DidPush()
119{
120 if (m_step_out_plan_sp)
121 m_thread.QueueThreadPlan(m_step_out_plan_sp, false);
122 else if (m_step_through_inline_plan_sp)
123 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000124}
125
126ThreadPlanStepOut::~ThreadPlanStepOut ()
127{
128 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
Greg Claytonf4124de2012-02-21 00:09:25 +0000129 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000130}
131
132void
133ThreadPlanStepOut::GetDescription (Stream *s, lldb::DescriptionLevel level)
134{
135 if (level == lldb::eDescriptionLevelBrief)
136 s->Printf ("step out");
137 else
138 {
Jim Ingham43d39062011-10-15 00:57:28 +0000139 if (m_step_out_plan_sp)
Jim Ingham441e3b92012-03-01 00:50:50 +0000140 s->Printf ("Stepping out to inlined frame so we can walk through it.");
Jim Ingham43d39062011-10-15 00:57:28 +0000141 else if (m_step_through_inline_plan_sp)
142 s->Printf ("Stepping out by stepping through inlined function.");
143 else
Jim Ingham441e3b92012-03-01 00:50:50 +0000144 s->Printf ("Stepping out from address 0x%llx to return address 0x%llx using breakpoint site %d",
Jim Ingham43d39062011-10-15 00:57:28 +0000145 (uint64_t)m_step_from_insn,
146 (uint64_t)m_return_addr,
Jim Ingham43d39062011-10-15 00:57:28 +0000147 m_return_bp_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000148 }
149}
150
151bool
152ThreadPlanStepOut::ValidatePlan (Stream *error)
153{
Jim Ingham43d39062011-10-15 00:57:28 +0000154 if (m_step_out_plan_sp)
155 return m_step_out_plan_sp->ValidatePlan (error);
156 else if (m_step_through_inline_plan_sp)
157 return m_step_through_inline_plan_sp->ValidatePlan (error);
158 else if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
159 {
160 error->PutCString("Could not create return address breakpoint.");
Chris Lattner24943d22010-06-08 16:52:24 +0000161 return false;
Jim Ingham43d39062011-10-15 00:57:28 +0000162 }
Chris Lattner24943d22010-06-08 16:52:24 +0000163 else
164 return true;
165}
166
167bool
168ThreadPlanStepOut::PlanExplainsStop ()
169{
Jim Ingham43d39062011-10-15 00:57:28 +0000170 // If one of our child plans just finished, then we do explain the stop.
171 if (m_step_out_plan_sp)
172 {
173 if (m_step_out_plan_sp->MischiefManaged())
174 {
175 // If this one is done, then we are all done.
Jim Ingham1586d972011-12-17 01:35:57 +0000176 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000177 SetPlanComplete();
178 return true;
179 }
180 else
181 return false;
182 }
183 else if (m_step_through_inline_plan_sp)
184 {
185 if (m_step_through_inline_plan_sp->MischiefManaged())
186 return true;
187 else
188 return false;
189 }
190
Chris Lattner24943d22010-06-08 16:52:24 +0000191 // We don't explain signals or breakpoints (breakpoints that handle stepping in or
192 // out will be handled by a child plan.
Jim Ingham43d39062011-10-15 00:57:28 +0000193
Jim Ingham6297a3a2010-10-20 00:39:53 +0000194 StopInfoSP stop_info_sp = GetPrivateStopReason();
195 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000196 {
Jim Ingham6297a3a2010-10-20 00:39:53 +0000197 StopReason reason = stop_info_sp->GetStopReason();
Chris Lattner24943d22010-06-08 16:52:24 +0000198 switch (reason)
199 {
Greg Clayton643ee732010-08-04 01:40:35 +0000200 case eStopReasonBreakpoint:
201 {
202 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
Greg Claytonf4124de2012-02-21 00:09:25 +0000203 BreakpointSiteSP site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue()));
Greg Clayton643ee732010-08-04 01:40:35 +0000204 if (site_sp && site_sp->IsBreakpointAtThisSite (m_return_bp_id))
Chris Lattner24943d22010-06-08 16:52:24 +0000205 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000206 bool done;
207
208 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
209
210 if (m_step_out_to_id == frame_zero_id)
211 done = true;
212 else if (m_step_out_to_id < frame_zero_id)
213 {
214 // Either we stepped past the breakpoint, or the stack ID calculation
215 // was incorrect and we should probably stop.
216 done = true;
217 }
218 else
219 {
220 if (m_immediate_step_from_id < frame_zero_id)
221 done = true;
222 else
223 done = false;
224 }
225
226 if (done)
Jim Ingham1586d972011-12-17 01:35:57 +0000227 {
228 CalculateReturnValue();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000229 SetPlanComplete();
Jim Ingham1586d972011-12-17 01:35:57 +0000230 }
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000231
Greg Clayton643ee732010-08-04 01:40:35 +0000232 // If there was only one owner, then we're done. But if we also hit some
233 // user breakpoint on our way out, we should mark ourselves as done, but
234 // also not claim to explain the stop, since it is more important to report
235 // the user breakpoint than the step out completion.
Chris Lattner24943d22010-06-08 16:52:24 +0000236
Greg Clayton643ee732010-08-04 01:40:35 +0000237 if (site_sp->GetNumberOfOwners() == 1)
238 return true;
239
Chris Lattner24943d22010-06-08 16:52:24 +0000240 }
Greg Clayton643ee732010-08-04 01:40:35 +0000241 return false;
242 }
243 case eStopReasonWatchpoint:
244 case eStopReasonSignal:
245 case eStopReasonException:
246 return false;
247
248 default:
249 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000250 }
251 }
252 return true;
253}
254
255bool
256ThreadPlanStepOut::ShouldStop (Event *event_ptr)
257{
Jim Ingham43d39062011-10-15 00:57:28 +0000258 if (IsPlanComplete())
Jim Ingham43d39062011-10-15 00:57:28 +0000259 return true;
Jim Ingham441e3b92012-03-01 00:50:50 +0000260
261 bool done;
262
263 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
264 if (frame_zero_id < m_step_out_to_id)
265 done = false;
266 else
267 done = true;
268
269 if (done)
Jim Ingham43d39062011-10-15 00:57:28 +0000270 {
Jim Ingham1586d972011-12-17 01:35:57 +0000271 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000272 SetPlanComplete();
273 return true;
274 }
275 else
276 {
277 if (m_step_out_plan_sp)
278 {
279 if (m_step_out_plan_sp->MischiefManaged())
280 {
281 // Now step through the inlined stack we are in:
282 if (QueueInlinedStepPlan(true))
283 {
284 return false;
285 }
286 else
287 {
Jim Ingham1586d972011-12-17 01:35:57 +0000288 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000289 SetPlanComplete ();
290 return true;
291 }
292 }
293 else
294 return m_step_out_plan_sp->ShouldStop(event_ptr);
295 }
296 else if (m_step_through_inline_plan_sp)
297 {
298 if (m_step_through_inline_plan_sp->MischiefManaged())
299 {
Jim Ingham1586d972011-12-17 01:35:57 +0000300 // We don't calculate the return value here because we don't know how to.
301 // But in case we had a return value sitting around from our process in
302 // getting here, let's clear it out.
303 m_return_valobj_sp.reset();
Jim Ingham43d39062011-10-15 00:57:28 +0000304 SetPlanComplete();
305 return true;
306 }
307 else
308 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
309 }
310 else
311 return false;
312 }
Chris Lattner24943d22010-06-08 16:52:24 +0000313}
314
315bool
316ThreadPlanStepOut::StopOthers ()
317{
318 return m_stop_others;
319}
320
321StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000322ThreadPlanStepOut::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000323{
324 return eStateRunning;
325}
326
327bool
328ThreadPlanStepOut::WillResume (StateType resume_state, bool current_plan)
329{
330 ThreadPlan::WillResume (resume_state, current_plan);
Jim Ingham43d39062011-10-15 00:57:28 +0000331 if (m_step_out_plan_sp || m_step_through_inline_plan_sp)
332 return true;
333
Chris Lattner24943d22010-06-08 16:52:24 +0000334 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
335 return false;
336
337 if (current_plan)
338 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000339 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000340 if (return_bp != NULL)
341 return_bp->SetEnabled (true);
342 }
343 return true;
344}
345
346bool
347ThreadPlanStepOut::WillStop ()
348{
Jim Ingham43d39062011-10-15 00:57:28 +0000349 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
350 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000351 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
Jim Ingham43d39062011-10-15 00:57:28 +0000352 if (return_bp != NULL)
353 return_bp->SetEnabled (false);
354 }
355
Chris Lattner24943d22010-06-08 16:52:24 +0000356 return true;
357}
358
359bool
360ThreadPlanStepOut::MischiefManaged ()
361{
Jim Ingham43d39062011-10-15 00:57:28 +0000362 if (IsPlanComplete())
Chris Lattner24943d22010-06-08 16:52:24 +0000363 {
364 // Did I reach my breakpoint? If so I'm done.
365 //
366 // I also check the stack depth, since if we've blown past the breakpoint for some
367 // reason and we're now stopping for some other reason altogether, then we're done
368 // with this step out operation.
369
Greg Claytone005f2c2010-11-06 01:53:30 +0000370 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000371 if (log)
372 log->Printf("Completed step out plan.");
Jim Ingham43d39062011-10-15 00:57:28 +0000373 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
374 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000375 m_thread.CalculateTarget()->RemoveBreakpointByID (m_return_bp_id);
Jim Ingham43d39062011-10-15 00:57:28 +0000376 m_return_bp_id = LLDB_INVALID_BREAK_ID;
377 }
378
Chris Lattner24943d22010-06-08 16:52:24 +0000379 ThreadPlan::MischiefManaged ();
380 return true;
381 }
382 else
383 {
384 return false;
385 }
386}
387
Jim Ingham43d39062011-10-15 00:57:28 +0000388bool
389ThreadPlanStepOut::QueueInlinedStepPlan (bool queue_now)
390{
391 // Now figure out the range of this inlined block, and set up a "step through range"
392 // plan for that. If we've been provided with a context, then use the block in that
393 // context.
394 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (0));
395 if (!immediate_return_from_sp)
396 return false;
397
398 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
399 if (log)
400 {
401 StreamString s;
402 immediate_return_from_sp->Dump(&s, true, false);
403 log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
404 }
405
406 Block *from_block = immediate_return_from_sp->GetFrameBlock();
407 if (from_block)
408 {
409 Block *inlined_block = from_block->GetContainingInlinedBlock();
410 if (inlined_block)
411 {
412 size_t num_ranges = inlined_block->GetNumRanges();
413 AddressRange inline_range;
414 if (inlined_block->GetRangeAtIndex(0, inline_range))
415 {
416 SymbolContext inlined_sc;
417 inlined_block->CalculateSymbolContext(&inlined_sc);
Jim Inghamc3ba2af2012-07-26 18:23:21 +0000418 inlined_sc.target_sp = GetTarget().shared_from_this();
Jim Ingham43d39062011-10-15 00:57:28 +0000419 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}
Jim Ingham88e3de22012-05-03 21:19:36 +0000471
472bool
473ThreadPlanStepOut::IsPlanStale()
474{
475 // If we are still lower on the stack than the frame we are returning to, then
476 // there's something for us to do. Otherwise, we're stale.
477
478 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
479 if (frame_zero_id < m_step_out_to_id)
480 return false;
481 else
482 return true;
483}
484