blob: 44ede3ad700f436fca4a3a6b6664e14f51123f1f [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());
Sean Callananf6d5fea2012-07-31 22:19:25 +000098
99 if (m_return_addr == LLDB_INVALID_ADDRESS)
100 return;
101
Greg Claytonf4124de2012-02-21 00:09:25 +0000102 Breakpoint *return_bp = m_thread.CalculateTarget()->CreateBreakpoint (m_return_addr, true).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000103 if (return_bp != NULL)
104 {
105 return_bp->SetThreadID(m_thread.GetID());
106 m_return_bp_id = return_bp->GetID();
107 }
Jim Ingham1586d972011-12-17 01:35:57 +0000108
109 if (immediate_return_from_sp)
110 {
111 const SymbolContext &sc = immediate_return_from_sp->GetSymbolContext(eSymbolContextFunction);
112 if (sc.function)
113 {
114 m_immediate_step_from_function = sc.function;
115 }
116 }
Chris Lattner24943d22010-06-08 16:52:24 +0000117 }
118
Jim Ingham43d39062011-10-15 00:57:28 +0000119}
120
121void
122ThreadPlanStepOut::DidPush()
123{
124 if (m_step_out_plan_sp)
125 m_thread.QueueThreadPlan(m_step_out_plan_sp, false);
126 else if (m_step_through_inline_plan_sp)
127 m_thread.QueueThreadPlan(m_step_through_inline_plan_sp, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000128}
129
130ThreadPlanStepOut::~ThreadPlanStepOut ()
131{
132 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
Greg Claytonf4124de2012-02-21 00:09:25 +0000133 m_thread.CalculateTarget()->RemoveBreakpointByID(m_return_bp_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000134}
135
136void
137ThreadPlanStepOut::GetDescription (Stream *s, lldb::DescriptionLevel level)
138{
139 if (level == lldb::eDescriptionLevelBrief)
140 s->Printf ("step out");
141 else
142 {
Jim Ingham43d39062011-10-15 00:57:28 +0000143 if (m_step_out_plan_sp)
Jim Ingham441e3b92012-03-01 00:50:50 +0000144 s->Printf ("Stepping out to inlined frame so we can walk through it.");
Jim Ingham43d39062011-10-15 00:57:28 +0000145 else if (m_step_through_inline_plan_sp)
146 s->Printf ("Stepping out by stepping through inlined function.");
147 else
Jim Ingham441e3b92012-03-01 00:50:50 +0000148 s->Printf ("Stepping out from address 0x%llx to return address 0x%llx using breakpoint site %d",
Jim Ingham43d39062011-10-15 00:57:28 +0000149 (uint64_t)m_step_from_insn,
150 (uint64_t)m_return_addr,
Jim Ingham43d39062011-10-15 00:57:28 +0000151 m_return_bp_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000152 }
153}
154
155bool
156ThreadPlanStepOut::ValidatePlan (Stream *error)
157{
Jim Ingham43d39062011-10-15 00:57:28 +0000158 if (m_step_out_plan_sp)
159 return m_step_out_plan_sp->ValidatePlan (error);
160 else if (m_step_through_inline_plan_sp)
161 return m_step_through_inline_plan_sp->ValidatePlan (error);
162 else if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
163 {
Sean Callananf6d5fea2012-07-31 22:19:25 +0000164 if (error)
165 error->PutCString("Could not create return address breakpoint.");
Chris Lattner24943d22010-06-08 16:52:24 +0000166 return false;
Jim Ingham43d39062011-10-15 00:57:28 +0000167 }
Chris Lattner24943d22010-06-08 16:52:24 +0000168 else
169 return true;
170}
171
172bool
173ThreadPlanStepOut::PlanExplainsStop ()
174{
Jim Ingham43d39062011-10-15 00:57:28 +0000175 // If one of our child plans just finished, then we do explain the stop.
176 if (m_step_out_plan_sp)
177 {
178 if (m_step_out_plan_sp->MischiefManaged())
179 {
180 // If this one is done, then we are all done.
Jim Ingham1586d972011-12-17 01:35:57 +0000181 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000182 SetPlanComplete();
183 return true;
184 }
185 else
186 return false;
187 }
188 else if (m_step_through_inline_plan_sp)
189 {
190 if (m_step_through_inline_plan_sp->MischiefManaged())
191 return true;
192 else
193 return false;
194 }
195
Chris Lattner24943d22010-06-08 16:52:24 +0000196 // We don't explain signals or breakpoints (breakpoints that handle stepping in or
197 // out will be handled by a child plan.
Jim Ingham43d39062011-10-15 00:57:28 +0000198
Jim Ingham6297a3a2010-10-20 00:39:53 +0000199 StopInfoSP stop_info_sp = GetPrivateStopReason();
200 if (stop_info_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000201 {
Jim Ingham6297a3a2010-10-20 00:39:53 +0000202 StopReason reason = stop_info_sp->GetStopReason();
Chris Lattner24943d22010-06-08 16:52:24 +0000203 switch (reason)
204 {
Greg Clayton643ee732010-08-04 01:40:35 +0000205 case eStopReasonBreakpoint:
206 {
207 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
Greg Claytonf4124de2012-02-21 00:09:25 +0000208 BreakpointSiteSP site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue()));
Greg Clayton643ee732010-08-04 01:40:35 +0000209 if (site_sp && site_sp->IsBreakpointAtThisSite (m_return_bp_id))
Chris Lattner24943d22010-06-08 16:52:24 +0000210 {
Jim Ingham441e3b92012-03-01 00:50:50 +0000211 bool done;
212
213 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
214
215 if (m_step_out_to_id == frame_zero_id)
216 done = true;
217 else if (m_step_out_to_id < frame_zero_id)
218 {
219 // Either we stepped past the breakpoint, or the stack ID calculation
220 // was incorrect and we should probably stop.
221 done = true;
222 }
223 else
224 {
225 if (m_immediate_step_from_id < frame_zero_id)
226 done = true;
227 else
228 done = false;
229 }
230
231 if (done)
Jim Ingham1586d972011-12-17 01:35:57 +0000232 {
233 CalculateReturnValue();
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000234 SetPlanComplete();
Jim Ingham1586d972011-12-17 01:35:57 +0000235 }
Greg Clayton1ebdcc72011-01-21 06:11:58 +0000236
Greg Clayton643ee732010-08-04 01:40:35 +0000237 // If there was only one owner, then we're done. But if we also hit some
238 // user breakpoint on our way out, we should mark ourselves as done, but
239 // also not claim to explain the stop, since it is more important to report
240 // the user breakpoint than the step out completion.
Chris Lattner24943d22010-06-08 16:52:24 +0000241
Greg Clayton643ee732010-08-04 01:40:35 +0000242 if (site_sp->GetNumberOfOwners() == 1)
243 return true;
244
Chris Lattner24943d22010-06-08 16:52:24 +0000245 }
Greg Clayton643ee732010-08-04 01:40:35 +0000246 return false;
247 }
248 case eStopReasonWatchpoint:
249 case eStopReasonSignal:
250 case eStopReasonException:
251 return false;
252
253 default:
254 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000255 }
256 }
257 return true;
258}
259
260bool
261ThreadPlanStepOut::ShouldStop (Event *event_ptr)
262{
Jim Ingham43d39062011-10-15 00:57:28 +0000263 if (IsPlanComplete())
Jim Ingham43d39062011-10-15 00:57:28 +0000264 return true;
Jim Ingham441e3b92012-03-01 00:50:50 +0000265
266 bool done;
267
268 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
269 if (frame_zero_id < m_step_out_to_id)
270 done = false;
271 else
272 done = true;
273
274 if (done)
Jim Ingham43d39062011-10-15 00:57:28 +0000275 {
Jim Ingham1586d972011-12-17 01:35:57 +0000276 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000277 SetPlanComplete();
278 return true;
279 }
280 else
281 {
282 if (m_step_out_plan_sp)
283 {
284 if (m_step_out_plan_sp->MischiefManaged())
285 {
286 // Now step through the inlined stack we are in:
287 if (QueueInlinedStepPlan(true))
288 {
289 return false;
290 }
291 else
292 {
Jim Ingham1586d972011-12-17 01:35:57 +0000293 CalculateReturnValue();
Jim Ingham43d39062011-10-15 00:57:28 +0000294 SetPlanComplete ();
295 return true;
296 }
297 }
298 else
299 return m_step_out_plan_sp->ShouldStop(event_ptr);
300 }
301 else if (m_step_through_inline_plan_sp)
302 {
303 if (m_step_through_inline_plan_sp->MischiefManaged())
304 {
Jim Ingham1586d972011-12-17 01:35:57 +0000305 // We don't calculate the return value here because we don't know how to.
306 // But in case we had a return value sitting around from our process in
307 // getting here, let's clear it out.
308 m_return_valobj_sp.reset();
Jim Ingham43d39062011-10-15 00:57:28 +0000309 SetPlanComplete();
310 return true;
311 }
312 else
313 return m_step_through_inline_plan_sp->ShouldStop(event_ptr);
314 }
315 else
316 return false;
317 }
Chris Lattner24943d22010-06-08 16:52:24 +0000318}
319
320bool
321ThreadPlanStepOut::StopOthers ()
322{
323 return m_stop_others;
324}
325
326StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000327ThreadPlanStepOut::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000328{
329 return eStateRunning;
330}
331
332bool
333ThreadPlanStepOut::WillResume (StateType resume_state, bool current_plan)
334{
335 ThreadPlan::WillResume (resume_state, current_plan);
Jim Ingham43d39062011-10-15 00:57:28 +0000336 if (m_step_out_plan_sp || m_step_through_inline_plan_sp)
337 return true;
338
Chris Lattner24943d22010-06-08 16:52:24 +0000339 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
340 return false;
341
342 if (current_plan)
343 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000344 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000345 if (return_bp != NULL)
346 return_bp->SetEnabled (true);
347 }
348 return true;
349}
350
351bool
352ThreadPlanStepOut::WillStop ()
353{
Jim Ingham43d39062011-10-15 00:57:28 +0000354 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
355 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000356 Breakpoint *return_bp = m_thread.CalculateTarget()->GetBreakpointByID(m_return_bp_id).get();
Jim Ingham43d39062011-10-15 00:57:28 +0000357 if (return_bp != NULL)
358 return_bp->SetEnabled (false);
359 }
360
Chris Lattner24943d22010-06-08 16:52:24 +0000361 return true;
362}
363
364bool
365ThreadPlanStepOut::MischiefManaged ()
366{
Jim Ingham43d39062011-10-15 00:57:28 +0000367 if (IsPlanComplete())
Chris Lattner24943d22010-06-08 16:52:24 +0000368 {
369 // Did I reach my breakpoint? If so I'm done.
370 //
371 // I also check the stack depth, since if we've blown past the breakpoint for some
372 // reason and we're now stopping for some other reason altogether, then we're done
373 // with this step out operation.
374
Greg Claytone005f2c2010-11-06 01:53:30 +0000375 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000376 if (log)
377 log->Printf("Completed step out plan.");
Jim Ingham43d39062011-10-15 00:57:28 +0000378 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
379 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000380 m_thread.CalculateTarget()->RemoveBreakpointByID (m_return_bp_id);
Jim Ingham43d39062011-10-15 00:57:28 +0000381 m_return_bp_id = LLDB_INVALID_BREAK_ID;
382 }
383
Chris Lattner24943d22010-06-08 16:52:24 +0000384 ThreadPlan::MischiefManaged ();
385 return true;
386 }
387 else
388 {
389 return false;
390 }
391}
392
Jim Ingham43d39062011-10-15 00:57:28 +0000393bool
394ThreadPlanStepOut::QueueInlinedStepPlan (bool queue_now)
395{
396 // Now figure out the range of this inlined block, and set up a "step through range"
397 // plan for that. If we've been provided with a context, then use the block in that
398 // context.
399 StackFrameSP immediate_return_from_sp (m_thread.GetStackFrameAtIndex (0));
400 if (!immediate_return_from_sp)
401 return false;
402
403 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
404 if (log)
405 {
406 StreamString s;
407 immediate_return_from_sp->Dump(&s, true, false);
408 log->Printf("Queuing inlined frame to step past: %s.", s.GetData());
409 }
410
411 Block *from_block = immediate_return_from_sp->GetFrameBlock();
412 if (from_block)
413 {
414 Block *inlined_block = from_block->GetContainingInlinedBlock();
415 if (inlined_block)
416 {
417 size_t num_ranges = inlined_block->GetNumRanges();
418 AddressRange inline_range;
419 if (inlined_block->GetRangeAtIndex(0, inline_range))
420 {
421 SymbolContext inlined_sc;
422 inlined_block->CalculateSymbolContext(&inlined_sc);
Jim Inghamc3ba2af2012-07-26 18:23:21 +0000423 inlined_sc.target_sp = GetTarget().shared_from_this();
Jim Ingham43d39062011-10-15 00:57:28 +0000424 RunMode run_mode = m_stop_others ? lldb::eOnlyThisThread : lldb::eAllThreads;
425 ThreadPlanStepOverRange *step_through_inline_plan_ptr = new ThreadPlanStepOverRange(m_thread,
426 inline_range,
427 inlined_sc,
428 run_mode);
429 step_through_inline_plan_ptr->SetOkayToDiscard(true);
430 StreamString errors;
431 if (!step_through_inline_plan_ptr->ValidatePlan(&errors))
432 {
433 //FIXME: Log this failure.
434 delete step_through_inline_plan_ptr;
435 return false;
436 }
437
438 for (size_t i = 1; i < num_ranges; i++)
439 {
440 if (inlined_block->GetRangeAtIndex (i, inline_range))
441 step_through_inline_plan_ptr->AddRange (inline_range);
442 }
443 m_step_through_inline_plan_sp.reset (step_through_inline_plan_ptr);
444 if (queue_now)
445 m_thread.QueueThreadPlan (m_step_through_inline_plan_sp, false);
446 return true;
447 }
448 }
449 }
450
451 return false;
452}
Jim Ingham1586d972011-12-17 01:35:57 +0000453
454void
455ThreadPlanStepOut::CalculateReturnValue ()
456{
457 if (m_return_valobj_sp)
458 return;
459
460 if (m_immediate_step_from_function != NULL)
461 {
462 Type *return_type = m_immediate_step_from_function->GetType();
463 lldb::clang_type_t return_clang_type = m_immediate_step_from_function->GetReturnClangType();
464 if (return_type && return_clang_type)
465 {
466 ClangASTType ast_type (return_type->GetClangAST(), return_clang_type);
467
Greg Claytonf4124de2012-02-21 00:09:25 +0000468 lldb::ABISP abi_sp = m_thread.GetProcess()->GetABI();
Jim Ingham1586d972011-12-17 01:35:57 +0000469 if (abi_sp)
470 {
471 m_return_valobj_sp = abi_sp->GetReturnValueObject(m_thread, ast_type);
472 }
473 }
474 }
475}
Jim Ingham88e3de22012-05-03 21:19:36 +0000476
477bool
478ThreadPlanStepOut::IsPlanStale()
479{
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 if (frame_zero_id < m_step_out_to_id)
485 return false;
486 else
487 return true;
488}
489