blob: 21e1eeb8d3cbed5fa6dbd05489d50a6580f9ad58 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ThreadPlanStepThrough.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/ThreadPlanStepThrough.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/lldb-private-log.h"
17#include "lldb/Core/Log.h"
18#include "lldb/Core/Stream.h"
19#include "lldb/Target/DynamicLoader.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000020#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Target/Process.h"
22#include "lldb/Target/RegisterContext.h"
Jim Inghamad382c52011-12-03 01:52:59 +000023#include "lldb/Target/Target.h"
24#include "lldb/Breakpoint/Breakpoint.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
28
29//----------------------------------------------------------------------
30// ThreadPlanStepThrough: If the current instruction is a trampoline, step through it
31// If it is the beginning of the prologue of a function, step through that as well.
32// FIXME: At present only handles DYLD trampolines.
33//----------------------------------------------------------------------
34
Jim Ingham038fa8e2012-05-10 01:35:39 +000035ThreadPlanStepThrough::ThreadPlanStepThrough (Thread &thread, StackID &m_stack_id, bool stop_others) :
Jim Ingham5a47e8b2010-06-19 04:45:32 +000036 ThreadPlan (ThreadPlan::eKindStepThrough, "Step through trampolines and prologues", thread, eVoteNoOpinion, eVoteNoOpinion),
Chris Lattner24943d22010-06-08 16:52:24 +000037 m_start_address (0),
Jim Inghamad382c52011-12-03 01:52:59 +000038 m_backstop_bkpt_id (LLDB_INVALID_BREAK_ID),
39 m_backstop_addr(LLDB_INVALID_ADDRESS),
Jim Ingham038fa8e2012-05-10 01:35:39 +000040 m_return_stack_id (m_stack_id),
Chris Lattner24943d22010-06-08 16:52:24 +000041 m_stop_others (stop_others)
42{
Jim Inghamad382c52011-12-03 01:52:59 +000043
44 LookForPlanToStepThroughFromCurrentPC();
45
46 // If we don't get a valid step through plan, don't bother to set up a backstop.
47 if (m_sub_plan_sp)
48 {
49 m_start_address = GetThread().GetRegisterContext()->GetPC(0);
Jim Ingham41ce4f12012-03-01 20:01:22 +000050
Jim Inghamad382c52011-12-03 01:52:59 +000051 // We are going to return back to the concrete frame 1, we might pass by some inlined code that we're in
52 // the middle of by doing this, but it's easier than trying to figure out where the inlined code might return to.
53
Jim Ingham038fa8e2012-05-10 01:35:39 +000054 StackFrameSP return_frame_sp = m_thread.GetFrameWithStackID (m_stack_id);
Jim Inghamad382c52011-12-03 01:52:59 +000055
56 if (return_frame_sp)
57 {
Greg Claytonf4124de2012-02-21 00:09:25 +000058 m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(m_thread.CalculateTarget().get());
59 Breakpoint *return_bp = m_thread.GetProcess()->GetTarget().CreateBreakpoint (m_backstop_addr, true).get();
Jim Inghamad382c52011-12-03 01:52:59 +000060 if (return_bp != NULL)
61 {
62 return_bp->SetThreadID(m_thread.GetID());
63 m_backstop_bkpt_id = return_bp->GetID();
64 }
65 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
66 if (log)
67 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +000068 log->Printf ("Setting backstop breakpoint %d at address: 0x%" PRIx64, m_backstop_bkpt_id, m_backstop_addr);
Jim Inghamad382c52011-12-03 01:52:59 +000069 }
70 }
71 }
Chris Lattner24943d22010-06-08 16:52:24 +000072}
73
74ThreadPlanStepThrough::~ThreadPlanStepThrough ()
75{
Jim Ingham038fa8e2012-05-10 01:35:39 +000076 ClearBackstopBreakpoint ();
Jim Inghamad382c52011-12-03 01:52:59 +000077}
78
79void
80ThreadPlanStepThrough::DidPush ()
81{
82 if (m_sub_plan_sp)
83 PushPlan(m_sub_plan_sp);
84}
85
86void
87ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC()
88{
Greg Claytonf4124de2012-02-21 00:09:25 +000089 m_sub_plan_sp = m_thread.GetProcess()->GetDynamicLoader()->GetStepThroughTrampolinePlan (m_thread, m_stop_others);
Jim Inghamad382c52011-12-03 01:52:59 +000090 // If that didn't come up with anything, try the ObjC runtime plugin:
91 if (!m_sub_plan_sp.get())
92 {
Greg Claytonf4124de2012-02-21 00:09:25 +000093 ObjCLanguageRuntime *objc_runtime = m_thread.GetProcess()->GetObjCLanguageRuntime();
Jim Inghamad382c52011-12-03 01:52:59 +000094 if (objc_runtime)
95 m_sub_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (m_thread, m_stop_others);
96 }
97
98 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
99 if (log)
100 {
101 lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0);
102 if (m_sub_plan_sp)
103 {
104 StreamString s;
105 m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000106 log->Printf ("Found step through plan from 0x%" PRIx64 ": %s", current_address, s.GetData());
Jim Inghamad382c52011-12-03 01:52:59 +0000107 }
108 else
109 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000110 log->Printf ("Couldn't find step through plan from address 0x%" PRIx64 ".", current_address);
Jim Inghamad382c52011-12-03 01:52:59 +0000111 }
112 }
Chris Lattner24943d22010-06-08 16:52:24 +0000113}
114
115void
116ThreadPlanStepThrough::GetDescription (Stream *s, lldb::DescriptionLevel level)
117{
118 if (level == lldb::eDescriptionLevelBrief)
119 s->Printf ("Step through");
120 else
121 {
Jim Inghamad382c52011-12-03 01:52:59 +0000122 s->PutCString ("Stepping through trampoline code from: ");
Chris Lattner24943d22010-06-08 16:52:24 +0000123 s->Address(m_start_address, sizeof (addr_t));
Jim Inghamad382c52011-12-03 01:52:59 +0000124 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID)
125 {
126 s->Printf (" with backstop breakpoint id: %d at address: ", m_backstop_bkpt_id);
127 s->Address (m_backstop_addr, sizeof (addr_t));
128 }
129 else
130 s->PutCString (" unable to set a backstop breakpoint.");
Chris Lattner24943d22010-06-08 16:52:24 +0000131 }
132}
133
134bool
135ThreadPlanStepThrough::ValidatePlan (Stream *error)
136{
Jim Inghamad382c52011-12-03 01:52:59 +0000137 return m_sub_plan_sp.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000138}
139
140bool
141ThreadPlanStepThrough::PlanExplainsStop ()
142{
Jim Inghamad382c52011-12-03 01:52:59 +0000143 // If we have a sub-plan, it will have been asked first if we explain the stop, and
144 // we won't get asked. The only time we would be the one directly asked this question
145 // is if we hit our backstop breakpoint.
146
147 if (HitOurBackstopBreakpoint())
148 return true;
149 else
150 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000151}
152
153bool
154ThreadPlanStepThrough::ShouldStop (Event *event_ptr)
155{
Jim Inghamad382c52011-12-03 01:52:59 +0000156 // If we've already marked ourselves done, then we're done...
157 if (IsPlanComplete())
158 return true;
159
Jim Ingham038fa8e2012-05-10 01:35:39 +0000160 // First, did we hit the backstop breakpoint?
161 if (HitOurBackstopBreakpoint())
162 {
163 SetPlanComplete(false);
164 return true;
165 }
166
Jim Inghamad382c52011-12-03 01:52:59 +0000167 // If we don't have a sub-plan, then we're also done (can't see how we would ever get here
168 // without a plan, but just in case.
169
170 if (!m_sub_plan_sp)
171 {
172 SetPlanComplete();
173 return true;
174 }
175
Jim Inghamad382c52011-12-03 01:52:59 +0000176 // If the current sub plan is not done, we don't want to stop. Actually, we probably won't
177 // ever get here in this state, since we generally won't get asked any questions if out
178 // current sub-plan is not done...
179 if (!m_sub_plan_sp->IsPlanComplete())
Jim Ingham038fa8e2012-05-10 01:35:39 +0000180 return false;
181
182 // If our current sub plan failed, then let's just run to our backstop. If we can't do that then just stop.
183 if (!m_sub_plan_sp->PlanSucceeded())
184 {
185 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID)
186 {
187 m_sub_plan_sp.reset();
Jim Inghamad382c52011-12-03 01:52:59 +0000188 return false;
Jim Ingham038fa8e2012-05-10 01:35:39 +0000189 }
190 else
191 {
192 SetPlanComplete(false);
193 return true;
194 }
195 }
196
Jim Inghamad382c52011-12-03 01:52:59 +0000197 // Next see if there is a specific step through plan at our current pc (these might
198 // chain, for instance stepping through a dylib trampoline to the objc dispatch function...)
199 LookForPlanToStepThroughFromCurrentPC();
200 if (m_sub_plan_sp)
201 {
202 PushPlan (m_sub_plan_sp);
203 return false;
204 }
205 else
206 {
207 SetPlanComplete();
208 return true;
209 }
Chris Lattner24943d22010-06-08 16:52:24 +0000210}
211
212bool
213ThreadPlanStepThrough::StopOthers ()
214{
215 return m_stop_others;
216}
217
218StateType
Jim Ingham745ac7a2010-11-11 19:26:09 +0000219ThreadPlanStepThrough::GetPlanRunState ()
Chris Lattner24943d22010-06-08 16:52:24 +0000220{
Jim Ingham038fa8e2012-05-10 01:35:39 +0000221 return eStateRunning;
Chris Lattner24943d22010-06-08 16:52:24 +0000222}
223
224bool
225ThreadPlanStepThrough::WillResume (StateType resume_state, bool current_plan)
226{
227 ThreadPlan::WillResume(resume_state, current_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000228 return true;
229}
230
231bool
232ThreadPlanStepThrough::WillStop ()
233{
234 return true;
235}
236
Jim Ingham038fa8e2012-05-10 01:35:39 +0000237void
238ThreadPlanStepThrough::ClearBackstopBreakpoint ()
239{
240 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID)
241 {
242 m_thread.GetProcess()->GetTarget().RemoveBreakpointByID (m_backstop_bkpt_id);
243 m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
244 }
245}
246
Chris Lattner24943d22010-06-08 16:52:24 +0000247bool
248ThreadPlanStepThrough::MischiefManaged ()
249{
Greg Claytone005f2c2010-11-06 01:53:30 +0000250 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000251
Jim Inghamad382c52011-12-03 01:52:59 +0000252 if (!IsPlanComplete())
Chris Lattner24943d22010-06-08 16:52:24 +0000253 {
Chris Lattner24943d22010-06-08 16:52:24 +0000254 return false;
255 }
256 else
257 {
258 if (log)
259 log->Printf("Completed step through step plan.");
Jim Ingham038fa8e2012-05-10 01:35:39 +0000260
261 ClearBackstopBreakpoint ();
Chris Lattner24943d22010-06-08 16:52:24 +0000262 ThreadPlan::MischiefManaged ();
263 return true;
264 }
265}
266
267bool
Jim Inghamad382c52011-12-03 01:52:59 +0000268ThreadPlanStepThrough::HitOurBackstopBreakpoint()
Chris Lattner24943d22010-06-08 16:52:24 +0000269{
Jim Inghamad382c52011-12-03 01:52:59 +0000270 StopInfoSP stop_info_sp(m_thread.GetStopInfo());
271 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
272 {
273 break_id_t stop_value = (break_id_t) stop_info_sp->GetValue();
Greg Claytonf4124de2012-02-21 00:09:25 +0000274 BreakpointSiteSP cur_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(stop_value);
Jim Inghamad382c52011-12-03 01:52:59 +0000275 if (cur_site_sp && cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id))
276 {
Jim Ingham41ce4f12012-03-01 20:01:22 +0000277 StackID cur_frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
278
Jim Ingham038fa8e2012-05-10 01:35:39 +0000279 if (cur_frame_zero_id == m_return_stack_id)
Jim Inghamad382c52011-12-03 01:52:59 +0000280 {
281 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
282 if (log)
283 log->PutCString ("ThreadPlanStepThrough hit backstop breakpoint.");
284 return true;
285 }
286 }
287 }
288 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000289}
290