1*0b57cec5SDimitry Andric //===-- ThreadPlanStepUntil.cpp -------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepUntil.h"
10*0b57cec5SDimitry Andric
11*0b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
12*0b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContextScope.h"
13*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
14*0b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
15*0b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
16*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
17*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
18*0b57cec5SDimitry Andric
19*0b57cec5SDimitry Andric using namespace lldb;
20*0b57cec5SDimitry Andric using namespace lldb_private;
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric // ThreadPlanStepUntil: Run until we reach a given line number or step out of
23*0b57cec5SDimitry Andric // the current frame
24*0b57cec5SDimitry Andric
ThreadPlanStepUntil(Thread & thread,lldb::addr_t * address_list,size_t num_addresses,bool stop_others,uint32_t frame_idx)25*0b57cec5SDimitry Andric ThreadPlanStepUntil::ThreadPlanStepUntil(Thread &thread,
26*0b57cec5SDimitry Andric lldb::addr_t *address_list,
27*0b57cec5SDimitry Andric size_t num_addresses, bool stop_others,
28*0b57cec5SDimitry Andric uint32_t frame_idx)
29*0b57cec5SDimitry Andric : ThreadPlan(ThreadPlan::eKindStepUntil, "Step until", thread,
30*0b57cec5SDimitry Andric eVoteNoOpinion, eVoteNoOpinion),
31*0b57cec5SDimitry Andric m_step_from_insn(LLDB_INVALID_ADDRESS),
32*0b57cec5SDimitry Andric m_return_bp_id(LLDB_INVALID_BREAK_ID),
33*0b57cec5SDimitry Andric m_return_addr(LLDB_INVALID_ADDRESS), m_stepped_out(false),
34*0b57cec5SDimitry Andric m_should_stop(false), m_ran_analyze(false), m_explains_stop(false),
35*0b57cec5SDimitry Andric m_until_points(), m_stop_others(stop_others) {
36*0b57cec5SDimitry Andric // Stash away our "until" addresses:
37*0b57cec5SDimitry Andric TargetSP target_sp(thread.CalculateTarget());
38*0b57cec5SDimitry Andric
39*0b57cec5SDimitry Andric StackFrameSP frame_sp(thread.GetStackFrameAtIndex(frame_idx));
40*0b57cec5SDimitry Andric if (frame_sp) {
41*0b57cec5SDimitry Andric m_step_from_insn = frame_sp->GetStackID().GetPC();
42*0b57cec5SDimitry Andric
43*0b57cec5SDimitry Andric // Find the return address and set a breakpoint there:
44*0b57cec5SDimitry Andric // FIXME - can we do this more securely if we know first_insn?
45*0b57cec5SDimitry Andric
46*0b57cec5SDimitry Andric StackFrameSP return_frame_sp(thread.GetStackFrameAtIndex(frame_idx + 1));
47*0b57cec5SDimitry Andric if (return_frame_sp) {
48*0b57cec5SDimitry Andric // TODO: add inline functionality
49*0b57cec5SDimitry Andric m_return_addr = return_frame_sp->GetStackID().GetPC();
50*0b57cec5SDimitry Andric Breakpoint *return_bp =
51*0b57cec5SDimitry Andric target_sp->CreateBreakpoint(m_return_addr, true, false).get();
52*0b57cec5SDimitry Andric
53*0b57cec5SDimitry Andric if (return_bp != nullptr) {
54*0b57cec5SDimitry Andric if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
55*0b57cec5SDimitry Andric m_could_not_resolve_hw_bp = true;
56*0b57cec5SDimitry Andric return_bp->SetThreadID(m_tid);
57*0b57cec5SDimitry Andric m_return_bp_id = return_bp->GetID();
58*0b57cec5SDimitry Andric return_bp->SetBreakpointKind("until-return-backstop");
59*0b57cec5SDimitry Andric }
60*0b57cec5SDimitry Andric }
61*0b57cec5SDimitry Andric
62*0b57cec5SDimitry Andric m_stack_id = frame_sp->GetStackID();
63*0b57cec5SDimitry Andric
64*0b57cec5SDimitry Andric // Now set breakpoints on all our return addresses:
65*0b57cec5SDimitry Andric for (size_t i = 0; i < num_addresses; i++) {
66*0b57cec5SDimitry Andric Breakpoint *until_bp =
67*0b57cec5SDimitry Andric target_sp->CreateBreakpoint(address_list[i], true, false).get();
68*0b57cec5SDimitry Andric if (until_bp != nullptr) {
69*0b57cec5SDimitry Andric until_bp->SetThreadID(m_tid);
70*0b57cec5SDimitry Andric m_until_points[address_list[i]] = until_bp->GetID();
71*0b57cec5SDimitry Andric until_bp->SetBreakpointKind("until-target");
72*0b57cec5SDimitry Andric } else {
73*0b57cec5SDimitry Andric m_until_points[address_list[i]] = LLDB_INVALID_BREAK_ID;
74*0b57cec5SDimitry Andric }
75*0b57cec5SDimitry Andric }
76*0b57cec5SDimitry Andric }
77*0b57cec5SDimitry Andric }
78*0b57cec5SDimitry Andric
~ThreadPlanStepUntil()79*0b57cec5SDimitry Andric ThreadPlanStepUntil::~ThreadPlanStepUntil() { Clear(); }
80*0b57cec5SDimitry Andric
Clear()81*0b57cec5SDimitry Andric void ThreadPlanStepUntil::Clear() {
82*0b57cec5SDimitry Andric Target &target = GetTarget();
83*0b57cec5SDimitry Andric if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
84*0b57cec5SDimitry Andric target.RemoveBreakpointByID(m_return_bp_id);
85*0b57cec5SDimitry Andric m_return_bp_id = LLDB_INVALID_BREAK_ID;
86*0b57cec5SDimitry Andric }
87*0b57cec5SDimitry Andric
88*0b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
89*0b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
90*0b57cec5SDimitry Andric target.RemoveBreakpointByID((*pos).second);
91*0b57cec5SDimitry Andric }
92*0b57cec5SDimitry Andric m_until_points.clear();
93*0b57cec5SDimitry Andric m_could_not_resolve_hw_bp = false;
94*0b57cec5SDimitry Andric }
95*0b57cec5SDimitry Andric
GetDescription(Stream * s,lldb::DescriptionLevel level)96*0b57cec5SDimitry Andric void ThreadPlanStepUntil::GetDescription(Stream *s,
97*0b57cec5SDimitry Andric lldb::DescriptionLevel level) {
98*0b57cec5SDimitry Andric if (level == lldb::eDescriptionLevelBrief) {
99*0b57cec5SDimitry Andric s->Printf("step until");
100*0b57cec5SDimitry Andric if (m_stepped_out)
101*0b57cec5SDimitry Andric s->Printf(" - stepped out");
102*0b57cec5SDimitry Andric } else {
103*0b57cec5SDimitry Andric if (m_until_points.size() == 1)
104*0b57cec5SDimitry Andric s->Printf("Stepping from address 0x%" PRIx64 " until we reach 0x%" PRIx64
105*0b57cec5SDimitry Andric " using breakpoint %d",
106*0b57cec5SDimitry Andric (uint64_t)m_step_from_insn,
107*0b57cec5SDimitry Andric (uint64_t)(*m_until_points.begin()).first,
108*0b57cec5SDimitry Andric (*m_until_points.begin()).second);
109*0b57cec5SDimitry Andric else {
110*0b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
111*0b57cec5SDimitry Andric s->Printf("Stepping from address 0x%" PRIx64 " until we reach one of:",
112*0b57cec5SDimitry Andric (uint64_t)m_step_from_insn);
113*0b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
114*0b57cec5SDimitry Andric s->Printf("\n\t0x%" PRIx64 " (bp: %d)", (uint64_t)(*pos).first,
115*0b57cec5SDimitry Andric (*pos).second);
116*0b57cec5SDimitry Andric }
117*0b57cec5SDimitry Andric }
118*0b57cec5SDimitry Andric s->Printf(" stepped out address is 0x%" PRIx64 ".",
119*0b57cec5SDimitry Andric (uint64_t)m_return_addr);
120*0b57cec5SDimitry Andric }
121*0b57cec5SDimitry Andric }
122*0b57cec5SDimitry Andric
ValidatePlan(Stream * error)123*0b57cec5SDimitry Andric bool ThreadPlanStepUntil::ValidatePlan(Stream *error) {
124*0b57cec5SDimitry Andric if (m_could_not_resolve_hw_bp) {
125*0b57cec5SDimitry Andric if (error)
126*0b57cec5SDimitry Andric error->PutCString(
127*0b57cec5SDimitry Andric "Could not create hardware breakpoint for thread plan.");
128*0b57cec5SDimitry Andric return false;
129*0b57cec5SDimitry Andric } else if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
130*0b57cec5SDimitry Andric if (error)
131*0b57cec5SDimitry Andric error->PutCString("Could not create return breakpoint.");
132*0b57cec5SDimitry Andric return false;
133*0b57cec5SDimitry Andric } else {
134*0b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
135*0b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
136*0b57cec5SDimitry Andric if (!LLDB_BREAK_ID_IS_VALID((*pos).second))
137*0b57cec5SDimitry Andric return false;
138*0b57cec5SDimitry Andric }
139*0b57cec5SDimitry Andric return true;
140*0b57cec5SDimitry Andric }
141*0b57cec5SDimitry Andric }
142*0b57cec5SDimitry Andric
AnalyzeStop()143*0b57cec5SDimitry Andric void ThreadPlanStepUntil::AnalyzeStop() {
144*0b57cec5SDimitry Andric if (m_ran_analyze)
145*0b57cec5SDimitry Andric return;
146*0b57cec5SDimitry Andric
147*0b57cec5SDimitry Andric StopInfoSP stop_info_sp = GetPrivateStopInfo();
148*0b57cec5SDimitry Andric m_should_stop = true;
149*0b57cec5SDimitry Andric m_explains_stop = false;
150*0b57cec5SDimitry Andric
151*0b57cec5SDimitry Andric if (stop_info_sp) {
152*0b57cec5SDimitry Andric StopReason reason = stop_info_sp->GetStopReason();
153*0b57cec5SDimitry Andric
154*0b57cec5SDimitry Andric if (reason == eStopReasonBreakpoint) {
155*0b57cec5SDimitry Andric // If this is OUR breakpoint, we're fine, otherwise we don't know why
156*0b57cec5SDimitry Andric // this happened...
157*0b57cec5SDimitry Andric BreakpointSiteSP this_site =
158*0b57cec5SDimitry Andric m_process.GetBreakpointSiteList().FindByID(stop_info_sp->GetValue());
159*0b57cec5SDimitry Andric if (!this_site) {
160*0b57cec5SDimitry Andric m_explains_stop = false;
161*0b57cec5SDimitry Andric return;
162*0b57cec5SDimitry Andric }
163*0b57cec5SDimitry Andric
164*0b57cec5SDimitry Andric if (this_site->IsBreakpointAtThisSite(m_return_bp_id)) {
165*0b57cec5SDimitry Andric // If we are at our "step out" breakpoint, and the stack depth has
166*0b57cec5SDimitry Andric // shrunk, then this is indeed our stop. If the stack depth has grown,
167*0b57cec5SDimitry Andric // then we've hit our step out breakpoint recursively. If we are the
168*0b57cec5SDimitry Andric // only breakpoint at that location, then we do explain the stop, and
169*0b57cec5SDimitry Andric // we'll just continue. If there was another breakpoint here, then we
170*0b57cec5SDimitry Andric // don't explain the stop, but we won't mark ourselves Completed,
171*0b57cec5SDimitry Andric // because maybe that breakpoint will continue, and then we'll finish
172*0b57cec5SDimitry Andric // the "until".
173*0b57cec5SDimitry Andric bool done;
174*0b57cec5SDimitry Andric StackID cur_frame_zero_id;
175*0b57cec5SDimitry Andric
176*0b57cec5SDimitry Andric done = (m_stack_id < cur_frame_zero_id);
177*0b57cec5SDimitry Andric
178*0b57cec5SDimitry Andric if (done) {
179*0b57cec5SDimitry Andric m_stepped_out = true;
180*0b57cec5SDimitry Andric SetPlanComplete();
181*0b57cec5SDimitry Andric } else
182*0b57cec5SDimitry Andric m_should_stop = false;
183*0b57cec5SDimitry Andric
184*0b57cec5SDimitry Andric if (this_site->GetNumberOfOwners() == 1)
185*0b57cec5SDimitry Andric m_explains_stop = true;
186*0b57cec5SDimitry Andric else
187*0b57cec5SDimitry Andric m_explains_stop = false;
188*0b57cec5SDimitry Andric return;
189*0b57cec5SDimitry Andric } else {
190*0b57cec5SDimitry Andric // Check if we've hit one of our "until" breakpoints.
191*0b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
192*0b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
193*0b57cec5SDimitry Andric if (this_site->IsBreakpointAtThisSite((*pos).second)) {
194*0b57cec5SDimitry Andric // If we're at the right stack depth, then we're done.
195*0b57cec5SDimitry Andric Thread &thread = GetThread();
196*0b57cec5SDimitry Andric bool done;
197*0b57cec5SDimitry Andric StackID frame_zero_id =
198*0b57cec5SDimitry Andric thread.GetStackFrameAtIndex(0)->GetStackID();
199*0b57cec5SDimitry Andric
200*0b57cec5SDimitry Andric if (frame_zero_id == m_stack_id)
201*0b57cec5SDimitry Andric done = true;
202*0b57cec5SDimitry Andric else if (frame_zero_id < m_stack_id)
203*0b57cec5SDimitry Andric done = false;
204*0b57cec5SDimitry Andric else {
205*0b57cec5SDimitry Andric StackFrameSP older_frame_sp = thread.GetStackFrameAtIndex(1);
206*0b57cec5SDimitry Andric
207*0b57cec5SDimitry Andric // But if we can't even unwind one frame we should just get out
208*0b57cec5SDimitry Andric // of here & stop...
209*0b57cec5SDimitry Andric if (older_frame_sp) {
210*0b57cec5SDimitry Andric const SymbolContext &older_context =
211*0b57cec5SDimitry Andric older_frame_sp->GetSymbolContext(eSymbolContextEverything);
212*0b57cec5SDimitry Andric SymbolContext stack_context;
213*0b57cec5SDimitry Andric m_stack_id.GetSymbolContextScope()->CalculateSymbolContext(
214*0b57cec5SDimitry Andric &stack_context);
215*0b57cec5SDimitry Andric
216*0b57cec5SDimitry Andric done = (older_context == stack_context);
217*0b57cec5SDimitry Andric } else
218*0b57cec5SDimitry Andric done = false;
219*0b57cec5SDimitry Andric }
220*0b57cec5SDimitry Andric
221*0b57cec5SDimitry Andric if (done)
222*0b57cec5SDimitry Andric SetPlanComplete();
223*0b57cec5SDimitry Andric else
224*0b57cec5SDimitry Andric m_should_stop = false;
225*0b57cec5SDimitry Andric
226*0b57cec5SDimitry Andric // Otherwise we've hit this breakpoint recursively. If we're the
227*0b57cec5SDimitry Andric // only breakpoint here, then we do explain the stop, and we'll
228*0b57cec5SDimitry Andric // continue. If not then we should let higher plans handle this
229*0b57cec5SDimitry Andric // stop.
230*0b57cec5SDimitry Andric if (this_site->GetNumberOfOwners() == 1)
231*0b57cec5SDimitry Andric m_explains_stop = true;
232*0b57cec5SDimitry Andric else {
233*0b57cec5SDimitry Andric m_should_stop = true;
234*0b57cec5SDimitry Andric m_explains_stop = false;
235*0b57cec5SDimitry Andric }
236*0b57cec5SDimitry Andric return;
237*0b57cec5SDimitry Andric }
238*0b57cec5SDimitry Andric }
239*0b57cec5SDimitry Andric }
240*0b57cec5SDimitry Andric // If we get here we haven't hit any of our breakpoints, so let the
241*0b57cec5SDimitry Andric // higher plans take care of the stop.
242*0b57cec5SDimitry Andric m_explains_stop = false;
243*0b57cec5SDimitry Andric return;
244*0b57cec5SDimitry Andric } else if (IsUsuallyUnexplainedStopReason(reason)) {
245*0b57cec5SDimitry Andric m_explains_stop = false;
246*0b57cec5SDimitry Andric } else {
247*0b57cec5SDimitry Andric m_explains_stop = true;
248*0b57cec5SDimitry Andric }
249*0b57cec5SDimitry Andric }
250*0b57cec5SDimitry Andric }
251*0b57cec5SDimitry Andric
DoPlanExplainsStop(Event * event_ptr)252*0b57cec5SDimitry Andric bool ThreadPlanStepUntil::DoPlanExplainsStop(Event *event_ptr) {
253*0b57cec5SDimitry Andric // We don't explain signals or breakpoints (breakpoints that handle stepping
254*0b57cec5SDimitry Andric // in or out will be handled by a child plan.
255*0b57cec5SDimitry Andric AnalyzeStop();
256*0b57cec5SDimitry Andric return m_explains_stop;
257*0b57cec5SDimitry Andric }
258*0b57cec5SDimitry Andric
ShouldStop(Event * event_ptr)259*0b57cec5SDimitry Andric bool ThreadPlanStepUntil::ShouldStop(Event *event_ptr) {
260*0b57cec5SDimitry Andric // If we've told our self in ExplainsStop that we plan to continue, then do
261*0b57cec5SDimitry Andric // so here. Otherwise, as long as this thread has stopped for a reason, we
262*0b57cec5SDimitry Andric // will stop.
263*0b57cec5SDimitry Andric
264*0b57cec5SDimitry Andric StopInfoSP stop_info_sp = GetPrivateStopInfo();
265*0b57cec5SDimitry Andric if (!stop_info_sp || stop_info_sp->GetStopReason() == eStopReasonNone)
266*0b57cec5SDimitry Andric return false;
267*0b57cec5SDimitry Andric
268*0b57cec5SDimitry Andric AnalyzeStop();
269*0b57cec5SDimitry Andric return m_should_stop;
270*0b57cec5SDimitry Andric }
271*0b57cec5SDimitry Andric
StopOthers()272*0b57cec5SDimitry Andric bool ThreadPlanStepUntil::StopOthers() { return m_stop_others; }
273*0b57cec5SDimitry Andric
GetPlanRunState()274*0b57cec5SDimitry Andric StateType ThreadPlanStepUntil::GetPlanRunState() { return eStateRunning; }
275*0b57cec5SDimitry Andric
DoWillResume(StateType resume_state,bool current_plan)276*0b57cec5SDimitry Andric bool ThreadPlanStepUntil::DoWillResume(StateType resume_state,
277*0b57cec5SDimitry Andric bool current_plan) {
278*0b57cec5SDimitry Andric if (current_plan) {
279*0b57cec5SDimitry Andric Target &target = GetTarget();
280*0b57cec5SDimitry Andric Breakpoint *return_bp = target.GetBreakpointByID(m_return_bp_id).get();
281*0b57cec5SDimitry Andric if (return_bp != nullptr)
282*0b57cec5SDimitry Andric return_bp->SetEnabled(true);
283*0b57cec5SDimitry Andric
284*0b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
285*0b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
286*0b57cec5SDimitry Andric Breakpoint *until_bp = target.GetBreakpointByID((*pos).second).get();
287*0b57cec5SDimitry Andric if (until_bp != nullptr)
288*0b57cec5SDimitry Andric until_bp->SetEnabled(true);
289*0b57cec5SDimitry Andric }
290*0b57cec5SDimitry Andric }
291*0b57cec5SDimitry Andric
292*0b57cec5SDimitry Andric m_should_stop = true;
293*0b57cec5SDimitry Andric m_ran_analyze = false;
294*0b57cec5SDimitry Andric m_explains_stop = false;
295*0b57cec5SDimitry Andric return true;
296*0b57cec5SDimitry Andric }
297*0b57cec5SDimitry Andric
WillStop()298*0b57cec5SDimitry Andric bool ThreadPlanStepUntil::WillStop() {
299*0b57cec5SDimitry Andric Target &target = GetTarget();
300*0b57cec5SDimitry Andric Breakpoint *return_bp = target.GetBreakpointByID(m_return_bp_id).get();
301*0b57cec5SDimitry Andric if (return_bp != nullptr)
302*0b57cec5SDimitry Andric return_bp->SetEnabled(false);
303*0b57cec5SDimitry Andric
304*0b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
305*0b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
306*0b57cec5SDimitry Andric Breakpoint *until_bp = target.GetBreakpointByID((*pos).second).get();
307*0b57cec5SDimitry Andric if (until_bp != nullptr)
308*0b57cec5SDimitry Andric until_bp->SetEnabled(false);
309*0b57cec5SDimitry Andric }
310*0b57cec5SDimitry Andric return true;
311*0b57cec5SDimitry Andric }
312*0b57cec5SDimitry Andric
MischiefManaged()313*0b57cec5SDimitry Andric bool ThreadPlanStepUntil::MischiefManaged() {
314*0b57cec5SDimitry Andric // I'm letting "PlanExplainsStop" do all the work, and just reporting that
315*0b57cec5SDimitry Andric // here.
316*0b57cec5SDimitry Andric bool done = false;
317*0b57cec5SDimitry Andric if (IsPlanComplete()) {
318*0b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
319*0b57cec5SDimitry Andric LLDB_LOGF(log, "Completed step until plan.");
320*0b57cec5SDimitry Andric
321*0b57cec5SDimitry Andric Clear();
322*0b57cec5SDimitry Andric done = true;
323*0b57cec5SDimitry Andric }
324*0b57cec5SDimitry Andric if (done)
325*0b57cec5SDimitry Andric ThreadPlan::MischiefManaged();
326*0b57cec5SDimitry Andric
327*0b57cec5SDimitry Andric return done;
328*0b57cec5SDimitry Andric }
329*0b57cec5SDimitry Andric