1 //===-- SBThreadPlan.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBThread.h"
10 
11 #include "lldb/API/SBFileSpec.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/API/SBSymbolContext.h"
14 #include "lldb/Breakpoint/BreakpointLocation.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/StreamFile.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Symbol/CompileUnit.h"
19 #include "lldb/Symbol/SymbolContext.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Queue.h"
22 #include "lldb/Target/StopInfo.h"
23 #include "lldb/Target/SystemRuntime.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26 #include "lldb/Target/ThreadPlan.h"
27 #include "lldb/Target/ThreadPlanPython.h"
28 #include "lldb/Target/ThreadPlanStepInRange.h"
29 #include "lldb/Target/ThreadPlanStepInstruction.h"
30 #include "lldb/Target/ThreadPlanStepOut.h"
31 #include "lldb/Target/ThreadPlanStepRange.h"
32 #include "lldb/Utility/State.h"
33 #include "lldb/Utility/Stream.h"
34 #include "lldb/Utility/StructuredData.h"
35 
36 #include "lldb/API/SBAddress.h"
37 #include "lldb/API/SBDebugger.h"
38 #include "lldb/API/SBEvent.h"
39 #include "lldb/API/SBFrame.h"
40 #include "lldb/API/SBProcess.h"
41 #include "lldb/API/SBThreadPlan.h"
42 #include "lldb/API/SBValue.h"
43 
44 #include <memory>
45 
46 using namespace lldb;
47 using namespace lldb_private;
48 
49 //----------------------------------------------------------------------
50 // Constructors
51 //----------------------------------------------------------------------
52 SBThreadPlan::SBThreadPlan() {}
53 
54 SBThreadPlan::SBThreadPlan(const ThreadPlanSP &lldb_object_sp)
55     : m_opaque_sp(lldb_object_sp) {}
56 
57 SBThreadPlan::SBThreadPlan(const SBThreadPlan &rhs)
58     : m_opaque_sp(rhs.m_opaque_sp) {}
59 
60 SBThreadPlan::SBThreadPlan(lldb::SBThread &sb_thread, const char *class_name) {
61   Thread *thread = sb_thread.get();
62   if (thread)
63     m_opaque_sp = std::make_shared<ThreadPlanPython>(*thread, class_name);
64 }
65 
66 //----------------------------------------------------------------------
67 // Assignment operator
68 //----------------------------------------------------------------------
69 
70 const lldb::SBThreadPlan &SBThreadPlan::operator=(const SBThreadPlan &rhs) {
71   if (this != &rhs)
72     m_opaque_sp = rhs.m_opaque_sp;
73   return *this;
74 }
75 //----------------------------------------------------------------------
76 // Destructor
77 //----------------------------------------------------------------------
78 SBThreadPlan::~SBThreadPlan() {}
79 
80 lldb_private::ThreadPlan *SBThreadPlan::get() { return m_opaque_sp.get(); }
81 
82 bool SBThreadPlan::IsValid() const { return m_opaque_sp.get() != NULL; }
83 
84 void SBThreadPlan::Clear() { m_opaque_sp.reset(); }
85 
86 lldb::StopReason SBThreadPlan::GetStopReason() { return eStopReasonNone; }
87 
88 size_t SBThreadPlan::GetStopReasonDataCount() { return 0; }
89 
90 uint64_t SBThreadPlan::GetStopReasonDataAtIndex(uint32_t idx) { return 0; }
91 
92 SBThread SBThreadPlan::GetThread() const {
93   if (m_opaque_sp) {
94     return SBThread(m_opaque_sp->GetThread().shared_from_this());
95   } else
96     return SBThread();
97 }
98 
99 bool SBThreadPlan::GetDescription(lldb::SBStream &description) const {
100   if (m_opaque_sp) {
101     m_opaque_sp->GetDescription(description.get(), eDescriptionLevelFull);
102   } else {
103     description.Printf("Empty SBThreadPlan");
104   }
105   return true;
106 }
107 
108 void SBThreadPlan::SetThreadPlan(const ThreadPlanSP &lldb_object_sp) {
109   m_opaque_sp = lldb_object_sp;
110 }
111 
112 void SBThreadPlan::SetPlanComplete(bool success) {
113   if (m_opaque_sp)
114     m_opaque_sp->SetPlanComplete(success);
115 }
116 
117 bool SBThreadPlan::IsPlanComplete() {
118   if (m_opaque_sp)
119     return m_opaque_sp->IsPlanComplete();
120   else
121     return true;
122 }
123 
124 bool SBThreadPlan::IsPlanStale() {
125   if (m_opaque_sp)
126     return m_opaque_sp->IsPlanStale();
127   else
128     return true;
129 }
130 
131 bool SBThreadPlan::IsValid() {
132   if (m_opaque_sp)
133     return m_opaque_sp->ValidatePlan(nullptr);
134   else
135     return false;
136 }
137 
138 // This section allows an SBThreadPlan to push another of the common types of
139 // plans...
140 //
141 // FIXME, you should only be able to queue thread plans from inside the methods
142 // of a Scripted Thread Plan.  Need a way to enforce that.
143 
144 SBThreadPlan
145 SBThreadPlan::QueueThreadPlanForStepOverRange(SBAddress &sb_start_address,
146                                               lldb::addr_t size) {
147   SBError error;
148   return QueueThreadPlanForStepOverRange(sb_start_address, size, error);
149 }
150 
151 SBThreadPlan SBThreadPlan::QueueThreadPlanForStepOverRange(
152     SBAddress &sb_start_address, lldb::addr_t size, SBError &error) {
153   if (m_opaque_sp) {
154     Address *start_address = sb_start_address.get();
155     if (!start_address) {
156       return SBThreadPlan();
157     }
158 
159     AddressRange range(*start_address, size);
160     SymbolContext sc;
161     start_address->CalculateSymbolContext(&sc);
162     Status plan_status;
163 
164     SBThreadPlan plan =
165         SBThreadPlan(m_opaque_sp->GetThread().QueueThreadPlanForStepOverRange(
166             false, range, sc, eAllThreads, plan_status));
167 
168     if (plan_status.Fail())
169       error.SetErrorString(plan_status.AsCString());
170 
171     return plan;
172   } else {
173     return SBThreadPlan();
174   }
175 }
176 
177 SBThreadPlan
178 SBThreadPlan::QueueThreadPlanForStepInRange(SBAddress &sb_start_address,
179                                             lldb::addr_t size) {
180   SBError error;
181   return QueueThreadPlanForStepInRange(sb_start_address, size, error);
182 }
183 
184 SBThreadPlan
185 SBThreadPlan::QueueThreadPlanForStepInRange(SBAddress &sb_start_address,
186                                             lldb::addr_t size, SBError &error) {
187   if (m_opaque_sp) {
188     Address *start_address = sb_start_address.get();
189     if (!start_address) {
190       return SBThreadPlan();
191     }
192 
193     AddressRange range(*start_address, size);
194     SymbolContext sc;
195     start_address->CalculateSymbolContext(&sc);
196 
197     Status plan_status;
198     SBThreadPlan plan =
199         SBThreadPlan(m_opaque_sp->GetThread().QueueThreadPlanForStepInRange(
200             false, range, sc, NULL, eAllThreads, plan_status));
201 
202     if (plan_status.Fail())
203       error.SetErrorString(plan_status.AsCString());
204 
205     return plan;
206   } else {
207     return SBThreadPlan();
208   }
209 }
210 
211 SBThreadPlan
212 SBThreadPlan::QueueThreadPlanForStepOut(uint32_t frame_idx_to_step_to,
213                                         bool first_insn) {
214   SBError error;
215   return QueueThreadPlanForStepOut(frame_idx_to_step_to, first_insn, error);
216 }
217 
218 SBThreadPlan
219 SBThreadPlan::QueueThreadPlanForStepOut(uint32_t frame_idx_to_step_to,
220                                         bool first_insn, SBError &error) {
221   if (m_opaque_sp) {
222     SymbolContext sc;
223     sc = m_opaque_sp->GetThread().GetStackFrameAtIndex(0)->GetSymbolContext(
224         lldb::eSymbolContextEverything);
225 
226     Status plan_status;
227     SBThreadPlan plan =
228         SBThreadPlan(m_opaque_sp->GetThread().QueueThreadPlanForStepOut(
229             false, &sc, first_insn, false, eVoteYes, eVoteNoOpinion,
230             frame_idx_to_step_to, plan_status));
231 
232     if (plan_status.Fail())
233       error.SetErrorString(plan_status.AsCString());
234 
235     return plan;
236   } else {
237     return SBThreadPlan();
238   }
239 }
240 
241 SBThreadPlan
242 SBThreadPlan::QueueThreadPlanForRunToAddress(SBAddress sb_address) {
243   SBError error;
244   return QueueThreadPlanForRunToAddress(sb_address, error);
245 }
246 
247 SBThreadPlan SBThreadPlan::QueueThreadPlanForRunToAddress(SBAddress sb_address,
248                                                           SBError &error) {
249   if (m_opaque_sp) {
250     Address *address = sb_address.get();
251     if (!address)
252       return SBThreadPlan();
253 
254     Status plan_status;
255     SBThreadPlan plan =
256         SBThreadPlan(m_opaque_sp->GetThread().QueueThreadPlanForRunToAddress(
257             false, *address, false, plan_status));
258 
259     if (plan_status.Fail())
260       error.SetErrorString(plan_status.AsCString());
261 
262     return plan;
263   } else {
264     return SBThreadPlan();
265   }
266 }
267 
268 SBThreadPlan
269 SBThreadPlan::QueueThreadPlanForStepScripted(const char *script_class_name) {
270   SBError error;
271   return QueueThreadPlanForStepScripted(script_class_name, error);
272 }
273 
274 SBThreadPlan
275 SBThreadPlan::QueueThreadPlanForStepScripted(const char *script_class_name,
276                                              SBError &error) {
277   if (m_opaque_sp) {
278     Status plan_status;
279     SBThreadPlan plan =
280         SBThreadPlan(m_opaque_sp->GetThread().QueueThreadPlanForStepScripted(
281             false, script_class_name, false, plan_status));
282 
283     if (plan_status.Fail())
284       error.SetErrorString(plan_status.AsCString());
285 
286     return plan;
287   } else {
288     return SBThreadPlan();
289   }
290 }
291