blob: 2581fc7b522f3f3742f28b68397d7eff9a72e861 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ThreadPlanStepUntil.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//===----------------------------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00009
10// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000014#include "lldb/Target/ThreadPlanStepUntil.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Breakpoint/Breakpoint.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Log.h"
17#include "lldb/Target/Process.h"
18#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000019#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Target/Target.h"
21
22using namespace lldb;
23using namespace lldb_private;
24
25//----------------------------------------------------------------------
26// ThreadPlanStepUntil: Run until we reach a given line number or step out of the current frame
27//----------------------------------------------------------------------
28
29ThreadPlanStepUntil::ThreadPlanStepUntil
30(
31 Thread &thread,
32 lldb::addr_t *address_list,
33 size_t num_addresses,
Greg Clayton481cef22011-01-21 06:11:58 +000034 bool stop_others,
35 uint32_t frame_idx
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036) :
Jim Inghamb01e7422010-06-19 04:45:32 +000037 ThreadPlan (ThreadPlan::eKindStepUntil, "Step until", thread, eVoteNoOpinion, eVoteNoOpinion),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038 m_step_from_insn (LLDB_INVALID_ADDRESS),
Jim Inghamb5c0d1c2012-03-01 00:50:50 +000039 m_return_bp_id (LLDB_INVALID_BREAK_ID),
Greg Claytonc982c762010-07-09 20:39:50 +000040 m_return_addr (LLDB_INVALID_ADDRESS),
Jim Inghamb5c0d1c2012-03-01 00:50:50 +000041 m_stepped_out (false),
42 m_should_stop (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043 m_ran_analyze (false),
Jim Inghamb5c0d1c2012-03-01 00:50:50 +000044 m_explains_stop (false),
45 m_until_points (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046 m_stop_others (stop_others)
47{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048 // Stash away our "until" addresses:
Greg Clayton1ac04c32012-02-21 00:09:25 +000049 TargetSP target_sp (m_thread.CalculateTarget());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
Jason Molendab57e4a12013-11-04 09:33:30 +000051 StackFrameSP frame_sp (m_thread.GetStackFrameAtIndex (frame_idx));
Greg Clayton481cef22011-01-21 06:11:58 +000052 if (frame_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053 {
Greg Clayton481cef22011-01-21 06:11:58 +000054 m_step_from_insn = frame_sp->GetStackID().GetPC();
55 lldb::user_id_t thread_id = m_thread.GetID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
Greg Clayton481cef22011-01-21 06:11:58 +000057 // Find the return address and set a breakpoint there:
58 // FIXME - can we do this more securely if we know first_insn?
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Jason Molendab57e4a12013-11-04 09:33:30 +000060 StackFrameSP return_frame_sp (m_thread.GetStackFrameAtIndex(frame_idx + 1));
Greg Clayton481cef22011-01-21 06:11:58 +000061 if (return_frame_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 {
Greg Clayton481cef22011-01-21 06:11:58 +000063 // TODO: add inline functionality
64 m_return_addr = return_frame_sp->GetStackID().GetPC();
Greg Claytoneb023e72013-10-11 19:48:25 +000065 Breakpoint *return_bp = target_sp->CreateBreakpoint (m_return_addr, true, false).get();
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000066 if (return_bp != nullptr)
Greg Clayton481cef22011-01-21 06:11:58 +000067 {
68 return_bp->SetThreadID(thread_id);
69 m_return_bp_id = return_bp->GetID();
Jim Ingham29950772013-01-26 02:19:28 +000070 return_bp->SetBreakpointKind ("until-return-backstop");
Greg Clayton481cef22011-01-21 06:11:58 +000071 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 }
Greg Clayton481cef22011-01-21 06:11:58 +000073
Jim Ingham76447852014-08-11 23:57:43 +000074 m_stack_id = frame_sp->GetStackID();
Greg Clayton481cef22011-01-21 06:11:58 +000075
76 // Now set breakpoints on all our return addresses:
Andy Gibbsa297a972013-06-19 19:04:53 +000077 for (size_t i = 0; i < num_addresses; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078 {
Greg Claytoneb023e72013-10-11 19:48:25 +000079 Breakpoint *until_bp = target_sp->CreateBreakpoint (address_list[i], true, false).get();
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000080 if (until_bp != nullptr)
Greg Clayton481cef22011-01-21 06:11:58 +000081 {
82 until_bp->SetThreadID(thread_id);
83 m_until_points[address_list[i]] = until_bp->GetID();
Jim Ingham29950772013-01-26 02:19:28 +000084 until_bp->SetBreakpointKind("until-target");
Greg Clayton481cef22011-01-21 06:11:58 +000085 }
86 else
87 {
88 m_until_points[address_list[i]] = LLDB_INVALID_BREAK_ID;
89 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090 }
91 }
92}
93
94ThreadPlanStepUntil::~ThreadPlanStepUntil ()
95{
96 Clear();
97}
98
99void
100ThreadPlanStepUntil::Clear()
101{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000102 TargetSP target_sp (m_thread.CalculateTarget());
103 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000105 if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
106 {
107 target_sp->RemoveBreakpointByID(m_return_bp_id);
108 m_return_bp_id = LLDB_INVALID_BREAK_ID;
109 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110
Greg Clayton1ac04c32012-02-21 00:09:25 +0000111 until_collection::iterator pos, end = m_until_points.end();
112 for (pos = m_until_points.begin(); pos != end; pos++)
113 {
114 target_sp->RemoveBreakpointByID((*pos).second);
115 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 }
117 m_until_points.clear();
118}
119
120void
121ThreadPlanStepUntil::GetDescription (Stream *s, lldb::DescriptionLevel level)
122{
123 if (level == lldb::eDescriptionLevelBrief)
124 {
125 s->Printf ("step until");
126 if (m_stepped_out)
127 s->Printf (" - stepped out");
128 }
129 else
130 {
131 if (m_until_points.size() == 1)
Daniel Malead01b2952012-11-29 21:49:15 +0000132 s->Printf ("Stepping from address 0x%" PRIx64 " until we reach 0x%" PRIx64 " using breakpoint %d",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133 (uint64_t)m_step_from_insn,
134 (uint64_t) (*m_until_points.begin()).first,
135 (*m_until_points.begin()).second);
136 else
137 {
138 until_collection::iterator pos, end = m_until_points.end();
Daniel Malead01b2952012-11-29 21:49:15 +0000139 s->Printf ("Stepping from address 0x%" PRIx64 " until we reach one of:",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 (uint64_t)m_step_from_insn);
141 for (pos = m_until_points.begin(); pos != end; pos++)
142 {
Daniel Malead01b2952012-11-29 21:49:15 +0000143 s->Printf ("\n\t0x%" PRIx64 " (bp: %d)", (uint64_t) (*pos).first, (*pos).second);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 }
145 }
Daniel Malead01b2952012-11-29 21:49:15 +0000146 s->Printf(" stepped out address is 0x%" PRIx64 ".", (uint64_t) m_return_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 }
148}
149
150bool
151ThreadPlanStepUntil::ValidatePlan (Stream *error)
152{
153 if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
154 return false;
155 else
156 {
157 until_collection::iterator pos, end = m_until_points.end();
158 for (pos = m_until_points.begin(); pos != end; pos++)
159 {
160 if (!LLDB_BREAK_ID_IS_VALID ((*pos).second))
161 return false;
162 }
163 return true;
164 }
165}
166
167void
168ThreadPlanStepUntil::AnalyzeStop()
169{
170 if (m_ran_analyze)
171 return;
172
Jim Ingham60c41182013-06-04 01:40:51 +0000173 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174 m_should_stop = true;
175 m_explains_stop = false;
176
Jim Inghamb15bfc72010-10-20 00:39:53 +0000177 if (stop_info_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 {
Jim Inghamb15bfc72010-10-20 00:39:53 +0000179 StopReason reason = stop_info_sp->GetStopReason();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180
Jim Ingham9b03fa02015-07-23 19:55:02 +0000181 if (reason == eStopReasonBreakpoint)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 {
Jim Ingham9b03fa02015-07-23 19:55:02 +0000183 // If this is OUR breakpoint, we're fine, otherwise we don't know why this happened...
184 BreakpointSiteSP this_site = m_thread.GetProcess()->GetBreakpointSiteList().FindByID (stop_info_sp->GetValue());
185 if (!this_site)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 m_explains_stop = false;
188 return;
189 }
Jim Ingham9b03fa02015-07-23 19:55:02 +0000190
191 if (this_site->IsBreakpointAtThisSite (m_return_bp_id))
192 {
193 // If we are at our "step out" breakpoint, and the stack depth has shrunk, then
194 // this is indeed our stop.
195 // If the stack depth has grown, then we've hit our step out breakpoint recursively.
196 // If we are the only breakpoint at that location, then we do explain the stop, and
197 // we'll just continue.
198 // If there was another breakpoint here, then we don't explain the stop, but we won't
199 // mark ourselves Completed, because maybe that breakpoint will continue, and then
200 // we'll finish the "until".
201 bool done;
202 StackID cur_frame_zero_id;
203
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000204 done = (m_stack_id < cur_frame_zero_id);
Jim Ingham9b03fa02015-07-23 19:55:02 +0000205
206 if (done)
207 {
208 m_stepped_out = true;
209 SetPlanComplete();
210 }
211 else
212 m_should_stop = false;
213
214 if (this_site->GetNumberOfOwners() == 1)
215 m_explains_stop = true;
216 else
217 m_explains_stop = false;
218 return;
219 }
220 else
221 {
222 // Check if we've hit one of our "until" breakpoints.
223 until_collection::iterator pos, end = m_until_points.end();
224 for (pos = m_until_points.begin(); pos != end; pos++)
225 {
226 if (this_site->IsBreakpointAtThisSite ((*pos).second))
227 {
228 // If we're at the right stack depth, then we're done.
229
230 bool done;
231 StackID frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
232
233 if (frame_zero_id == m_stack_id)
234 done = true;
235 else if (frame_zero_id < m_stack_id)
236 done = false;
237 else
238 {
239 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
240
241 // But if we can't even unwind one frame we should just get out of here & stop...
242 if (older_frame_sp)
243 {
244 const SymbolContext &older_context
245 = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
246 SymbolContext stack_context;
247 m_stack_id.GetSymbolContextScope()->CalculateSymbolContext(&stack_context);
248
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000249 done = (older_context == stack_context);
Jim Ingham9b03fa02015-07-23 19:55:02 +0000250 }
251 else
252 done = false;
253 }
254
255 if (done)
256 SetPlanComplete();
257 else
258 m_should_stop = false;
259
260 // Otherwise we've hit this breakpoint recursively. If we're the
261 // only breakpoint here, then we do explain the stop, and we'll continue.
262 // If not then we should let higher plans handle this stop.
263 if (this_site->GetNumberOfOwners() == 1)
264 m_explains_stop = true;
265 else
266 {
267 m_should_stop = true;
268 m_explains_stop = false;
269 }
270 return;
271 }
272 }
273 }
274 // If we get here we haven't hit any of our breakpoints, so let the higher
275 // plans take care of the stop.
276 m_explains_stop = false;
277 return;
278 }
279 else if (IsUsuallyUnexplainedStopReason(reason))
280 {
281 m_explains_stop = false;
282 }
283 else
284 {
285 m_explains_stop = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 }
287 }
288}
289
290bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000291ThreadPlanStepUntil::DoPlanExplainsStop (Event *event_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292{
293 // We don't explain signals or breakpoints (breakpoints that handle stepping in or
294 // out will be handled by a child plan.
295 AnalyzeStop();
296 return m_explains_stop;
297}
298
299bool
300ThreadPlanStepUntil::ShouldStop (Event *event_ptr)
301{
302 // If we've told our self in ExplainsStop that we plan to continue, then
303 // do so here. Otherwise, as long as this thread has stopped for a reason,
304 // we will stop.
305
Jim Ingham60c41182013-06-04 01:40:51 +0000306 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
Sean Callanan9a028512012-08-09 00:50:26 +0000307 if (!stop_info_sp || stop_info_sp->GetStopReason() == eStopReasonNone)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 return false;
309
310 AnalyzeStop();
311 return m_should_stop;
312}
313
314bool
315ThreadPlanStepUntil::StopOthers ()
316{
317 return m_stop_others;
318}
319
320StateType
Jim Ingham06e827c2010-11-11 19:26:09 +0000321ThreadPlanStepUntil::GetPlanRunState ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322{
323 return eStateRunning;
324}
325
326bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000327ThreadPlanStepUntil::DoWillResume (StateType resume_state, bool current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329 if (current_plan)
330 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000331 TargetSP target_sp (m_thread.CalculateTarget());
332 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000334 Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000335 if (return_bp != nullptr)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000336 return_bp->SetEnabled (true);
337
338 until_collection::iterator pos, end = m_until_points.end();
339 for (pos = m_until_points.begin(); pos != end; pos++)
340 {
341 Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get();
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000342 if (until_bp != nullptr)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000343 until_bp->SetEnabled (true);
344 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345 }
346 }
347
348 m_should_stop = true;
349 m_ran_analyze = false;
350 m_explains_stop = false;
351 return true;
352}
353
354bool
355ThreadPlanStepUntil::WillStop ()
356{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000357 TargetSP target_sp (m_thread.CalculateTarget());
358 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000360 Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get();
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000361 if (return_bp != nullptr)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000362 return_bp->SetEnabled (false);
363
364 until_collection::iterator pos, end = m_until_points.end();
365 for (pos = m_until_points.begin(); pos != end; pos++)
366 {
367 Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get();
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000368 if (until_bp != nullptr)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000369 until_bp->SetEnabled (false);
370 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 }
372 return true;
373}
374
375bool
376ThreadPlanStepUntil::MischiefManaged ()
377{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 // I'm letting "PlanExplainsStop" do all the work, and just reporting that here.
379 bool done = false;
380 if (IsPlanComplete())
381 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000382 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 if (log)
384 log->Printf("Completed step until plan.");
385
386 Clear();
387 done = true;
388 }
389 if (done)
390 ThreadPlan::MischiefManaged ();
391
392 return done;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393}