LLDB  mainline
ScriptedThread.cpp
Go to the documentation of this file.
1 //===-- ScriptedThread.cpp ------------------------------------------------===//
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 "ScriptedThread.h"
10 
13 #include "lldb/Target/Process.h"
15 #include "lldb/Target/StopInfo.h"
16 #include "lldb/Target/Unwind.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/Logging.h"
20 
21 #include <memory>
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 void ScriptedThread::CheckInterpreterAndScriptObject() const {
27  lldbassert(m_script_object_sp && "Invalid Script Object.");
28  lldbassert(GetInterface() && "Invalid Scripted Thread Interface.");
29 }
30 
31 ScriptedThread::ScriptedThread(ScriptedProcess &process, Status &error)
32  : Thread(process, LLDB_INVALID_THREAD_ID), m_scripted_process(process) {
33  if (!process.IsValid()) {
34  error.SetErrorString("Invalid scripted process");
35  return;
36  }
37 
39 
40  auto scripted_thread_interface = GetInterface();
41  if (!scripted_thread_interface) {
42  error.SetErrorString("Failed to get scripted thread interface.");
43  return;
44  }
45 
46  llvm::Optional<std::string> class_name =
48  if (!class_name || class_name->empty()) {
49  error.SetErrorString("Failed to get scripted thread class name.");
50  return;
51  }
52 
53  ExecutionContext exe_ctx(process);
54 
55  StructuredData::GenericSP object_sp =
56  scripted_thread_interface->CreatePluginObject(
57  class_name->c_str(), exe_ctx,
59  if (!object_sp || !object_sp->IsValid()) {
60  error.SetErrorString("Failed to create valid script object");
61  return;
62  }
63 
64  m_script_object_sp = object_sp;
65 
66  SetID(scripted_thread_interface->GetThreadID());
67 }
68 
70 
71 const char *ScriptedThread::GetName() {
73  llvm::Optional<std::string> thread_name = GetInterface()->GetName();
74  if (!thread_name)
75  return nullptr;
76  return ConstString(thread_name->c_str()).AsCString();
77 }
78 
81  llvm::Optional<std::string> queue_name = GetInterface()->GetQueue();
82  if (!queue_name)
83  return nullptr;
84  return ConstString(queue_name->c_str()).AsCString();
85 }
86 
88 
90 
92  if (!m_reg_context_sp)
94  return m_reg_context_sp;
95 }
96 
97 RegisterContextSP
99  const uint32_t concrete_frame_idx =
100  frame ? frame->GetConcreteFrameIndex() : 0;
101 
102  if (concrete_frame_idx)
104 
105  lldb::RegisterContextSP reg_ctx_sp;
106  Status error;
107 
108  llvm::Optional<std::string> reg_data = GetInterface()->GetRegisterContext();
109  if (!reg_data)
110  return GetInterface()->ErrorWithMessage<lldb::RegisterContextSP>(
111  LLVM_PRETTY_FUNCTION, "Failed to get scripted thread registers data.",
113 
114  DataBufferSP data_sp(
115  std::make_shared<DataBufferHeap>(reg_data->c_str(), reg_data->size()));
116 
117  if (!data_sp->GetByteSize())
118  return GetInterface()->ErrorWithMessage<lldb::RegisterContextSP>(
119  LLVM_PRETTY_FUNCTION, "Failed to copy raw registers data.", error,
121 
122  std::shared_ptr<RegisterContextMemory> reg_ctx_memory =
123  std::make_shared<RegisterContextMemory>(
125  if (!reg_ctx_memory)
126  return GetInterface()->ErrorWithMessage<lldb::RegisterContextSP>(
127  LLVM_PRETTY_FUNCTION, "Failed to create a register context.", error,
129 
130  reg_ctx_memory->SetAllRegisterData(data_sp);
131  m_reg_context_sp = reg_ctx_memory;
132 
133  return m_reg_context_sp;
134 }
135 
137  StructuredData::DictionarySP dict_sp = GetInterface()->GetStopReason();
138 
139  Status error;
140  lldb::StopInfoSP stop_info_sp;
141  lldb::StopReason stop_reason_type;
142 
143  if (!dict_sp->GetValueForKeyAsInteger("type", stop_reason_type))
144  return GetInterface()->ErrorWithMessage<bool>(
145  LLVM_PRETTY_FUNCTION,
146  "Couldn't find value for key 'type' in stop reason dictionary.", error,
148 
149  StructuredData::Dictionary *data_dict;
150  if (!dict_sp->GetValueForKeyAsDictionary("data", data_dict))
151  return GetInterface()->ErrorWithMessage<bool>(
152  LLVM_PRETTY_FUNCTION,
153  "Couldn't find value for key 'type' in stop reason dictionary.", error,
155 
156  switch (stop_reason_type) {
158  break;
160  lldb::break_id_t break_id;
161  data_dict->GetValueForKeyAsInteger("break_id", break_id,
163  stop_info_sp =
165  } break;
167  int signal;
168  llvm::StringRef description;
169  data_dict->GetValueForKeyAsInteger("signal", signal,
171  data_dict->GetValueForKeyAsString("desc", description);
172  stop_info_sp =
173  StopInfo::CreateStopReasonWithSignal(*this, signal, description.data());
174  } break;
175  default:
176  return GetInterface()->ErrorWithMessage<bool>(
177  LLVM_PRETTY_FUNCTION,
178  llvm::Twine("Unsupported stop reason type (" +
179  llvm::Twine(stop_reason_type) + llvm::Twine(")."))
180  .str(),
182  }
183 
184  SetStopInfo(stop_info_sp);
185  return true;
186 }
187 
189  GetRegisterContext()->InvalidateIfNeeded(/*force=*/false);
190 }
191 
192 lldb::ScriptedThreadInterfaceSP ScriptedThread::GetInterface() const {
194 }
195 
196 std::shared_ptr<DynamicRegisterInfo> ScriptedThread::GetDynamicRegisterInfo() {
198 
199  if (!m_register_info_sp) {
200  StructuredData::DictionarySP reg_info = GetInterface()->GetRegisterInfo();
201  if (!reg_info)
202  return nullptr;
203 
204  m_register_info_sp = std::make_shared<DynamicRegisterInfo>(
206  assert(m_register_info_sp->GetNumRegisters() > 0);
207  assert(m_register_info_sp->GetNumRegisterSets() > 0);
208  }
209 
210  return m_register_info_sp;
211 }
lldb_private::Process::IsValid
bool IsValid() const
Return whether this object is valid (i.e.
Definition: Process.h:573
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb_private::StructuredData::Dictionary
Definition: StructuredData.h:352
lldb_private::ScriptedThread::CheckInterpreterAndScriptObject
void CheckInterpreterAndScriptObject() const
Definition: ScriptedThread.cpp:26
lldb_private::ScriptedThread::CalculateStopInfo
bool CalculateStopInfo() override
Definition: ScriptedThread.cpp:136
lldb_private::ScriptedThread::m_register_info_sp
std::shared_ptr< DynamicRegisterInfo > m_register_info_sp
Definition: ScriptedThread.h:62
lldb_private::StructuredData::GenericSP
std::shared_ptr< Generic > GenericSP
Definition: StructuredData.h:68
lldb_private::Thread::GetUnwinder
virtual Unwind & GetUnwinder()
Definition: Thread.cpp:1861
lldb_private::ScriptedProcess::GetInterface
ScriptedProcessInterface & GetInterface() const
Definition: ScriptedProcess.cpp:338
lldb_private::ScriptedProcessInterface::GetScriptedThreadPluginName
virtual llvm::Optional< std::string > GetScriptedThreadPluginName()
Definition: ScriptedProcessInterface.h:63
lldb_private::Thread::ClearStackFrames
virtual void ClearStackFrames()
Definition: Thread.cpp:1417
Unwind.h
lldb_private::Process::GetTarget
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1209
lldb_private::StopInfo::CreateStopReasonWithSignal
static lldb::StopInfoSP CreateStopReasonWithSignal(Thread &thread, int signo, const char *description=nullptr)
Definition: StopInfo.cpp:1273
lldb::StopReason
StopReason
Thread stop reasons.
Definition: lldb-enumerations.h:239
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:193
lldb_private::UserID::SetID
void SetID(lldb::user_id_t uid)
Set accessor for the user ID.
Definition: UserID.h:53
lldb_private::ScriptedThread::GetName
const char * GetName() override
Definition: ScriptedThread.cpp:71
lldb_private::ScriptedThread::~ScriptedThread
~ScriptedThread() override
Definition: ScriptedThread.cpp:69
Process.h
lldb_private::ScriptedThread::ClearStackFrames
void ClearStackFrames() override
Definition: ScriptedThread.cpp:89
lldb_private::ScriptedThread::RefreshStateAfterStop
void RefreshStateAfterStop() override
Definition: ScriptedThread.cpp:188
lldb_private::StackFrame::GetConcreteFrameIndex
uint32_t GetConcreteFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList, not counting inlined frame...
Definition: StackFrame.h:407
lldb_private::Thread::DestroyThread
virtual void DestroyThread()
Definition: Thread.cpp:247
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
LLDB_INVALID_SIGNAL_NUMBER
#define LLDB_INVALID_SIGNAL_NUMBER
Definition: lldb-defines.h:96
lldb_private::ScriptedThread::CreateRegisterContextForFrame
lldb::RegisterContextSP CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override
Definition: ScriptedThread.cpp:98
Log.h
lldb_private::Thread
Definition: Thread.h:60
lldb_private::ScriptedThread::GetRegisterContext
lldb::RegisterContextSP GetRegisterContext() override
Definition: ScriptedThread.cpp:91
lldb::eStopReasonNone
@ eStopReasonNone
Definition: lldb-enumerations.h:241
lldb::break_id_t
int32_t break_id_t
Definition: lldb-types.h:88
OperatingSystem.h
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::ScriptedThread::m_script_object_sp
lldb_private::StructuredData::ObjectSP m_script_object_sp
Definition: ScriptedThread.h:63
lldb_private::Thread::SetStopInfo
void SetStopInfo(const lldb::StopInfoSP &stop_info_sp)
Definition: Thread.cpp:433
lldb_private::StructuredData::DictionarySP
std::shared_ptr< Dictionary > DictionarySP
Definition: StructuredData.h:67
LLDB_INVALID_BREAK_ID
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:49
lldb_private::Unwind::CreateRegisterContextForFrame
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition: Unwind.h:56
lldbassert
#define lldbassert(x)
Definition: LLDBAssert.h:15
lldb_private::ScriptedProcess::m_scripted_process_info
const ScriptedProcessInfo m_scripted_process_info
Definition: ScriptedProcess.h:111
lldb::eStopReasonSignal
@ eStopReasonSignal
Definition: lldb-enumerations.h:245
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:967
lldb_private::Status
Definition: Status.h:44
lldb_private::Thread::m_reg_context_sp
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread's current register state.
Definition: Thread.h:1248
uint32_t
lldb_private::ScriptedThread::GetInterface
lldb::ScriptedThreadInterfaceSP GetInterface() const
Definition: ScriptedThread.cpp:192
lldb_private::ScriptedThread::m_scripted_process
const ScriptedProcess & m_scripted_process
Definition: ScriptedThread.h:61
lldb_private::ScriptedProcess
Definition: ScriptedProcess.h:22
ScriptedThread.h
lldb_private::ScriptedProcessInterface::GetScriptedThreadInterface
virtual lldb::ScriptedThreadInterfaceSP GetScriptedThreadInterface()
Definition: ScriptedProcessInterface.h:69
StopInfo.h
lldb::eStopReasonBreakpoint
@ eStopReasonBreakpoint
Definition: lldb-enumerations.h:243
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
RegisterContextThreadMemory.h
lldb_private::ScriptedThread::WillResume
void WillResume(lldb::StateType resume_state) override
Definition: ScriptedThread.cpp:87
lldb_private::ScriptedThread::GetDynamicRegisterInfo
std::shared_ptr< DynamicRegisterInfo > GetDynamicRegisterInfo()
Definition: ScriptedThread.cpp:196
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Logging.h
LLDB_INVALID_THREAD_ID
#define LLDB_INVALID_THREAD_ID
Definition: lldb-defines.h:94
lldb_private::ScriptedProcess::ScriptedProcessInfo::GetArgsSP
StructuredData::DictionarySP GetArgsSP() const
Definition: ScriptedProcess.h:32
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:73
lldb_private::StopInfo::CreateStopReasonWithBreakpointSiteID
static lldb::StopInfoSP CreateStopReasonWithBreakpointSiteID(Thread &thread, lldb::break_id_t break_id)
Definition: StopInfo.cpp:1256
lldb_private::ScriptedThread::GetQueueName
const char * GetQueueName() override
Retrieve the Queue name for the queue currently using this Thread.
Definition: ScriptedThread.cpp:79
lldb_private::StackFrame
Definition: StackFrame.h:40
lldb_private::ScriptedProcess::CheckInterpreterAndScriptObject
void CheckInterpreterAndScriptObject() const
Definition: ScriptedProcess.cpp:47
lldb
Definition: SBAddress.h:15
LIBLLDB_LOG_THREAD
#define LIBLLDB_LOG_THREAD
Definition: Logging.h:16
RegisterContext.h
DataBufferHeap.h