1696bd635SAlexander Shaposhnikov //===-- SBThreadPlan.cpp ----------------------------------------*- C++ -*-===//
22bdbfd50SJim Ingham //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62bdbfd50SJim Ingham //
72bdbfd50SJim Ingham //===----------------------------------------------------------------------===//
82bdbfd50SJim Ingham 
92bdbfd50SJim Ingham #include "lldb/API/SBThread.h"
102bdbfd50SJim Ingham 
112bdbfd50SJim Ingham #include "lldb/API/SBFileSpec.h"
122bdbfd50SJim Ingham #include "lldb/API/SBStream.h"
13b9c1b51eSKate Stone #include "lldb/API/SBSymbolContext.h"
142bdbfd50SJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
152bdbfd50SJim Ingham #include "lldb/Core/Debugger.h"
162bdbfd50SJim Ingham #include "lldb/Core/StreamFile.h"
172bdbfd50SJim Ingham #include "lldb/Interpreter/CommandInterpreter.h"
18b9c1b51eSKate Stone #include "lldb/Symbol/CompileUnit.h"
19b9c1b51eSKate Stone #include "lldb/Symbol/SymbolContext.h"
202bdbfd50SJim Ingham #include "lldb/Target/Process.h"
212bdbfd50SJim Ingham #include "lldb/Target/Queue.h"
222bdbfd50SJim Ingham #include "lldb/Target/StopInfo.h"
23b9c1b51eSKate Stone #include "lldb/Target/SystemRuntime.h"
242bdbfd50SJim Ingham #include "lldb/Target/Target.h"
25b9c1b51eSKate Stone #include "lldb/Target/Thread.h"
26b9c1b51eSKate Stone #include "lldb/Target/ThreadPlan.h"
272bdbfd50SJim Ingham #include "lldb/Target/ThreadPlanPython.h"
28b9c1b51eSKate Stone #include "lldb/Target/ThreadPlanStepInRange.h"
292bdbfd50SJim Ingham #include "lldb/Target/ThreadPlanStepInstruction.h"
302bdbfd50SJim Ingham #include "lldb/Target/ThreadPlanStepOut.h"
312bdbfd50SJim Ingham #include "lldb/Target/ThreadPlanStepRange.h"
32d821c997SPavel Labath #include "lldb/Utility/State.h"
33bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
34f2a8bccfSPavel Labath #include "lldb/Utility/StructuredData.h"
352bdbfd50SJim Ingham 
362bdbfd50SJim Ingham #include "lldb/API/SBAddress.h"
372bdbfd50SJim Ingham #include "lldb/API/SBDebugger.h"
382bdbfd50SJim Ingham #include "lldb/API/SBEvent.h"
392bdbfd50SJim Ingham #include "lldb/API/SBFrame.h"
402bdbfd50SJim Ingham #include "lldb/API/SBProcess.h"
412bdbfd50SJim Ingham #include "lldb/API/SBThreadPlan.h"
422bdbfd50SJim Ingham #include "lldb/API/SBValue.h"
432bdbfd50SJim Ingham 
44*796ac80bSJonas Devlieghere #include <memory>
45*796ac80bSJonas Devlieghere 
462bdbfd50SJim Ingham using namespace lldb;
472bdbfd50SJim Ingham using namespace lldb_private;
482bdbfd50SJim Ingham 
492bdbfd50SJim Ingham //----------------------------------------------------------------------
502bdbfd50SJim Ingham // Constructors
512bdbfd50SJim Ingham //----------------------------------------------------------------------
52b9c1b51eSKate Stone SBThreadPlan::SBThreadPlan() {}
532bdbfd50SJim Ingham 
54b9c1b51eSKate Stone SBThreadPlan::SBThreadPlan(const ThreadPlanSP &lldb_object_sp)
55b9c1b51eSKate Stone     : m_opaque_sp(lldb_object_sp) {}
562bdbfd50SJim Ingham 
57b9c1b51eSKate Stone SBThreadPlan::SBThreadPlan(const SBThreadPlan &rhs)
58b9c1b51eSKate Stone     : m_opaque_sp(rhs.m_opaque_sp) {}
592bdbfd50SJim Ingham 
60b9c1b51eSKate Stone SBThreadPlan::SBThreadPlan(lldb::SBThread &sb_thread, const char *class_name) {
612bdbfd50SJim Ingham   Thread *thread = sb_thread.get();
622bdbfd50SJim Ingham   if (thread)
63*796ac80bSJonas Devlieghere     m_opaque_sp = std::make_shared<ThreadPlanPython>(*thread, class_name);
642bdbfd50SJim Ingham }
652bdbfd50SJim Ingham 
662bdbfd50SJim Ingham //----------------------------------------------------------------------
672bdbfd50SJim Ingham // Assignment operator
682bdbfd50SJim Ingham //----------------------------------------------------------------------
692bdbfd50SJim Ingham 
70b9c1b51eSKate Stone const lldb::SBThreadPlan &SBThreadPlan::operator=(const SBThreadPlan &rhs) {
712bdbfd50SJim Ingham   if (this != &rhs)
722bdbfd50SJim Ingham     m_opaque_sp = rhs.m_opaque_sp;
732bdbfd50SJim Ingham   return *this;
742bdbfd50SJim Ingham }
752bdbfd50SJim Ingham //----------------------------------------------------------------------
762bdbfd50SJim Ingham // Destructor
772bdbfd50SJim Ingham //----------------------------------------------------------------------
78b9c1b51eSKate Stone SBThreadPlan::~SBThreadPlan() {}
792bdbfd50SJim Ingham 
80b9c1b51eSKate Stone lldb_private::ThreadPlan *SBThreadPlan::get() { return m_opaque_sp.get(); }
812bdbfd50SJim Ingham 
82b9c1b51eSKate Stone bool SBThreadPlan::IsValid() const { return m_opaque_sp.get() != NULL; }
832bdbfd50SJim Ingham 
84b9c1b51eSKate Stone void SBThreadPlan::Clear() { m_opaque_sp.reset(); }
852bdbfd50SJim Ingham 
86b9c1b51eSKate Stone lldb::StopReason SBThreadPlan::GetStopReason() { return eStopReasonNone; }
872bdbfd50SJim Ingham 
88b9c1b51eSKate Stone size_t SBThreadPlan::GetStopReasonDataCount() { return 0; }
892bdbfd50SJim Ingham 
90b9c1b51eSKate Stone uint64_t SBThreadPlan::GetStopReasonDataAtIndex(uint32_t idx) { return 0; }
912bdbfd50SJim Ingham 
92b9c1b51eSKate Stone SBThread SBThreadPlan::GetThread() const {
93b9c1b51eSKate Stone   if (m_opaque_sp) {
942bdbfd50SJim Ingham     return SBThread(m_opaque_sp->GetThread().shared_from_this());
95b9c1b51eSKate Stone   } else
962bdbfd50SJim Ingham     return SBThread();
972bdbfd50SJim Ingham }
982bdbfd50SJim Ingham 
99b9c1b51eSKate Stone bool SBThreadPlan::GetDescription(lldb::SBStream &description) const {
100b9c1b51eSKate Stone   if (m_opaque_sp) {
1012bdbfd50SJim Ingham     m_opaque_sp->GetDescription(description.get(), eDescriptionLevelFull);
102b9c1b51eSKate Stone   } else {
1032bdbfd50SJim Ingham     description.Printf("Empty SBThreadPlan");
1042bdbfd50SJim Ingham   }
1052bdbfd50SJim Ingham   return true;
1062bdbfd50SJim Ingham }
1072bdbfd50SJim Ingham 
108b9c1b51eSKate Stone void SBThreadPlan::SetThreadPlan(const ThreadPlanSP &lldb_object_sp) {
1092bdbfd50SJim Ingham   m_opaque_sp = lldb_object_sp;
1102bdbfd50SJim Ingham }
1112bdbfd50SJim Ingham 
112b9c1b51eSKate Stone void SBThreadPlan::SetPlanComplete(bool success) {
1132bdbfd50SJim Ingham   if (m_opaque_sp)
1142bdbfd50SJim Ingham     m_opaque_sp->SetPlanComplete(success);
1152bdbfd50SJim Ingham }
1162bdbfd50SJim Ingham 
117b9c1b51eSKate Stone bool SBThreadPlan::IsPlanComplete() {
1182bdbfd50SJim Ingham   if (m_opaque_sp)
1192bdbfd50SJim Ingham     return m_opaque_sp->IsPlanComplete();
1202bdbfd50SJim Ingham   else
1212bdbfd50SJim Ingham     return true;
1222bdbfd50SJim Ingham }
1232bdbfd50SJim Ingham 
124b9c1b51eSKate Stone bool SBThreadPlan::IsPlanStale() {
125c915a7d2SJim Ingham   if (m_opaque_sp)
126c915a7d2SJim Ingham     return m_opaque_sp->IsPlanStale();
127c915a7d2SJim Ingham   else
128c915a7d2SJim Ingham     return true;
129c915a7d2SJim Ingham }
130c915a7d2SJim Ingham 
131b9c1b51eSKate Stone bool SBThreadPlan::IsValid() {
1322bdbfd50SJim Ingham   if (m_opaque_sp)
1332bdbfd50SJim Ingham     return m_opaque_sp->ValidatePlan(nullptr);
1342bdbfd50SJim Ingham   else
1352bdbfd50SJim Ingham     return false;
1362bdbfd50SJim Ingham }
1372bdbfd50SJim Ingham 
138b9c1b51eSKate Stone // This section allows an SBThreadPlan to push another of the common types of
139b9c1b51eSKate Stone // plans...
1402bdbfd50SJim Ingham //
141b9c1b51eSKate Stone // FIXME, you should only be able to queue thread plans from inside the methods
14205097246SAdrian Prantl // of a Scripted Thread Plan.  Need a way to enforce that.
1432bdbfd50SJim Ingham 
1442bdbfd50SJim Ingham SBThreadPlan
1452bdbfd50SJim Ingham SBThreadPlan::QueueThreadPlanForStepOverRange(SBAddress &sb_start_address,
146b9c1b51eSKate Stone                                               lldb::addr_t size) {
147e103ae92SJonas Devlieghere   SBError error;
148e103ae92SJonas Devlieghere   return QueueThreadPlanForStepOverRange(sb_start_address, size, error);
149e103ae92SJonas Devlieghere }
150e103ae92SJonas Devlieghere 
151e103ae92SJonas Devlieghere SBThreadPlan SBThreadPlan::QueueThreadPlanForStepOverRange(
152e103ae92SJonas Devlieghere     SBAddress &sb_start_address, lldb::addr_t size, SBError &error) {
153b9c1b51eSKate Stone   if (m_opaque_sp) {
1542bdbfd50SJim Ingham     Address *start_address = sb_start_address.get();
155b9c1b51eSKate Stone     if (!start_address) {
1562bdbfd50SJim Ingham       return SBThreadPlan();
1572bdbfd50SJim Ingham     }
1582bdbfd50SJim Ingham 
1592bdbfd50SJim Ingham     AddressRange range(*start_address, size);
1602bdbfd50SJim Ingham     SymbolContext sc;
1612bdbfd50SJim Ingham     start_address->CalculateSymbolContext(&sc);
162e103ae92SJonas Devlieghere     Status plan_status;
163e103ae92SJonas Devlieghere 
164e103ae92SJonas Devlieghere     SBThreadPlan plan =
165e103ae92SJonas Devlieghere         SBThreadPlan(m_opaque_sp->GetThread().QueueThreadPlanForStepOverRange(
166e103ae92SJonas Devlieghere             false, range, sc, eAllThreads, plan_status));
167e103ae92SJonas Devlieghere 
168e103ae92SJonas Devlieghere     if (plan_status.Fail())
169e103ae92SJonas Devlieghere       error.SetErrorString(plan_status.AsCString());
170e103ae92SJonas Devlieghere 
171e103ae92SJonas Devlieghere     return plan;
172b9c1b51eSKate Stone   } else {
1732bdbfd50SJim Ingham     return SBThreadPlan();
1742bdbfd50SJim Ingham   }
1752bdbfd50SJim Ingham }
1762bdbfd50SJim Ingham 
1772bdbfd50SJim Ingham SBThreadPlan
1782bdbfd50SJim Ingham SBThreadPlan::QueueThreadPlanForStepInRange(SBAddress &sb_start_address,
179b9c1b51eSKate Stone                                             lldb::addr_t size) {
180e103ae92SJonas Devlieghere   SBError error;
181e103ae92SJonas Devlieghere   return QueueThreadPlanForStepInRange(sb_start_address, size, error);
182e103ae92SJonas Devlieghere }
183e103ae92SJonas Devlieghere 
184e103ae92SJonas Devlieghere SBThreadPlan
185e103ae92SJonas Devlieghere SBThreadPlan::QueueThreadPlanForStepInRange(SBAddress &sb_start_address,
186e103ae92SJonas Devlieghere                                             lldb::addr_t size, SBError &error) {
187b9c1b51eSKate Stone   if (m_opaque_sp) {
1882bdbfd50SJim Ingham     Address *start_address = sb_start_address.get();
189b9c1b51eSKate Stone     if (!start_address) {
1902bdbfd50SJim Ingham       return SBThreadPlan();
1912bdbfd50SJim Ingham     }
1922bdbfd50SJim Ingham 
1932bdbfd50SJim Ingham     AddressRange range(*start_address, size);
1942bdbfd50SJim Ingham     SymbolContext sc;
1952bdbfd50SJim Ingham     start_address->CalculateSymbolContext(&sc);
196e103ae92SJonas Devlieghere 
197e103ae92SJonas Devlieghere     Status plan_status;
198e103ae92SJonas Devlieghere     SBThreadPlan plan =
199e103ae92SJonas Devlieghere         SBThreadPlan(m_opaque_sp->GetThread().QueueThreadPlanForStepInRange(
200e103ae92SJonas Devlieghere             false, range, sc, NULL, eAllThreads, plan_status));
201e103ae92SJonas Devlieghere 
202e103ae92SJonas Devlieghere     if (plan_status.Fail())
203e103ae92SJonas Devlieghere       error.SetErrorString(plan_status.AsCString());
204e103ae92SJonas Devlieghere 
205e103ae92SJonas Devlieghere     return plan;
206b9c1b51eSKate Stone   } else {
2072bdbfd50SJim Ingham     return SBThreadPlan();
2082bdbfd50SJim Ingham   }
2092bdbfd50SJim Ingham }
2102bdbfd50SJim Ingham 
2112bdbfd50SJim Ingham SBThreadPlan
212b9c1b51eSKate Stone SBThreadPlan::QueueThreadPlanForStepOut(uint32_t frame_idx_to_step_to,
213b9c1b51eSKate Stone                                         bool first_insn) {
214e103ae92SJonas Devlieghere   SBError error;
215e103ae92SJonas Devlieghere   return QueueThreadPlanForStepOut(frame_idx_to_step_to, first_insn, error);
216e103ae92SJonas Devlieghere }
217e103ae92SJonas Devlieghere 
218e103ae92SJonas Devlieghere SBThreadPlan
219e103ae92SJonas Devlieghere SBThreadPlan::QueueThreadPlanForStepOut(uint32_t frame_idx_to_step_to,
220e103ae92SJonas Devlieghere                                         bool first_insn, SBError &error) {
221b9c1b51eSKate Stone   if (m_opaque_sp) {
2222bdbfd50SJim Ingham     SymbolContext sc;
223b9c1b51eSKate Stone     sc = m_opaque_sp->GetThread().GetStackFrameAtIndex(0)->GetSymbolContext(
224b9c1b51eSKate Stone         lldb::eSymbolContextEverything);
225e103ae92SJonas Devlieghere 
226e103ae92SJonas Devlieghere     Status plan_status;
227e103ae92SJonas Devlieghere     SBThreadPlan plan =
228e103ae92SJonas Devlieghere         SBThreadPlan(m_opaque_sp->GetThread().QueueThreadPlanForStepOut(
229b9c1b51eSKate Stone             false, &sc, first_insn, false, eVoteYes, eVoteNoOpinion,
230e103ae92SJonas Devlieghere             frame_idx_to_step_to, plan_status));
231e103ae92SJonas Devlieghere 
232e103ae92SJonas Devlieghere     if (plan_status.Fail())
233e103ae92SJonas Devlieghere       error.SetErrorString(plan_status.AsCString());
234e103ae92SJonas Devlieghere 
235e103ae92SJonas Devlieghere     return plan;
236b9c1b51eSKate Stone   } else {
2372bdbfd50SJim Ingham     return SBThreadPlan();
2382bdbfd50SJim Ingham   }
2392bdbfd50SJim Ingham }
2402bdbfd50SJim Ingham 
2412bdbfd50SJim Ingham SBThreadPlan
242b9c1b51eSKate Stone SBThreadPlan::QueueThreadPlanForRunToAddress(SBAddress sb_address) {
243e103ae92SJonas Devlieghere   SBError error;
244e103ae92SJonas Devlieghere   return QueueThreadPlanForRunToAddress(sb_address, error);
245e103ae92SJonas Devlieghere }
246e103ae92SJonas Devlieghere 
247e103ae92SJonas Devlieghere SBThreadPlan SBThreadPlan::QueueThreadPlanForRunToAddress(SBAddress sb_address,
248e103ae92SJonas Devlieghere                                                           SBError &error) {
249b9c1b51eSKate Stone   if (m_opaque_sp) {
2502bdbfd50SJim Ingham     Address *address = sb_address.get();
2512bdbfd50SJim Ingham     if (!address)
2522bdbfd50SJim Ingham       return SBThreadPlan();
2532bdbfd50SJim Ingham 
254e103ae92SJonas Devlieghere     Status plan_status;
255e103ae92SJonas Devlieghere     SBThreadPlan plan =
256e103ae92SJonas Devlieghere         SBThreadPlan(m_opaque_sp->GetThread().QueueThreadPlanForRunToAddress(
257e103ae92SJonas Devlieghere             false, *address, false, plan_status));
258e103ae92SJonas Devlieghere 
259e103ae92SJonas Devlieghere     if (plan_status.Fail())
260e103ae92SJonas Devlieghere       error.SetErrorString(plan_status.AsCString());
261e103ae92SJonas Devlieghere 
262e103ae92SJonas Devlieghere     return plan;
263b9c1b51eSKate Stone   } else {
2642bdbfd50SJim Ingham     return SBThreadPlan();
2652bdbfd50SJim Ingham   }
2662bdbfd50SJim Ingham }
267c1c0fac7SAleksandr Urakov 
268c1c0fac7SAleksandr Urakov SBThreadPlan
269c1c0fac7SAleksandr Urakov SBThreadPlan::QueueThreadPlanForStepScripted(const char *script_class_name) {
270e103ae92SJonas Devlieghere   SBError error;
271e103ae92SJonas Devlieghere   return QueueThreadPlanForStepScripted(script_class_name, error);
272e103ae92SJonas Devlieghere }
273e103ae92SJonas Devlieghere 
274e103ae92SJonas Devlieghere SBThreadPlan
275e103ae92SJonas Devlieghere SBThreadPlan::QueueThreadPlanForStepScripted(const char *script_class_name,
276e103ae92SJonas Devlieghere                                              SBError &error) {
277c1c0fac7SAleksandr Urakov   if (m_opaque_sp) {
278e103ae92SJonas Devlieghere     Status plan_status;
279e103ae92SJonas Devlieghere     SBThreadPlan plan =
280e103ae92SJonas Devlieghere         SBThreadPlan(m_opaque_sp->GetThread().QueueThreadPlanForStepScripted(
281e103ae92SJonas Devlieghere             false, script_class_name, false, plan_status));
282e103ae92SJonas Devlieghere 
283e103ae92SJonas Devlieghere     if (plan_status.Fail())
284e103ae92SJonas Devlieghere       error.SetErrorString(plan_status.AsCString());
285e103ae92SJonas Devlieghere 
286e103ae92SJonas Devlieghere     return plan;
287c1c0fac7SAleksandr Urakov   } else {
288c1c0fac7SAleksandr Urakov     return SBThreadPlan();
289c1c0fac7SAleksandr Urakov   }
290c1c0fac7SAleksandr Urakov }
291