9#include "lldb/Host/Config.h"
44void OperatingSystemPython::Initialize() {
45 PluginManager::RegisterPlugin(GetPluginNameStatic(),
46 GetPluginDescriptionStatic(), CreateInstance,
50void OperatingSystemPython::Terminate() {
51 PluginManager::UnregisterPlugin(CreateInstance);
59 if (python_os_plugin_spec &&
60 FileSystem::Instance().Exists(python_os_plugin_spec)) {
61 std::unique_ptr<OperatingSystemPython> os_up(
62 new OperatingSystemPython(process, python_os_plugin_spec));
63 if (os_up.get() && os_up->IsValid())
64 return os_up.release();
69llvm::StringRef OperatingSystemPython::GetPluginDescriptionStatic() {
70 return "Operating system plug-in that gathers OS information from a python "
71 "class that implements the necessary OperatingSystem functionality.";
76 :
OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(),
77 m_interpreter(nullptr), m_script_object_sp() {
83 m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
87 std::string os_plugin_class_name(
89 if (os_plugin_class_name.empty())
93 char python_module_path_cstr[
PATH_MAX];
94 python_module_path.
GetPath(python_module_path_cstr,
95 sizeof(python_module_path_cstr));
97 if (!m_interpreter->LoadScriptingModule(python_module_path_cstr, options,
102 size_t py_extension_pos = os_plugin_class_name.rfind(
".py");
103 if (py_extension_pos != std::string::npos)
104 os_plugin_class_name.erase(py_extension_pos);
107 os_plugin_class_name +=
".OperatingSystemPlugIn";
109 auto operating_system_interface =
110 m_interpreter->CreateOperatingSystemInterface();
111 if (!operating_system_interface)
119 auto obj_or_err = operating_system_interface->CreatePluginObject(
120 os_plugin_class_name, exe_ctx,
nullptr);
123 llvm::consumeError(obj_or_err.takeError());
128 if (!owned_script_object_sp->IsValid())
133 m_script_object_sp = owned_script_object_sp;
134 m_operating_system_interface_sp = operating_system_interface;
137OperatingSystemPython::~OperatingSystemPython() =
default;
140 if (m_register_info_up ==
nullptr) {
141 if (!m_interpreter || !m_operating_system_interface_sp)
146 "OperatingSystemPython::GetDynamicRegisterInfo() fetching "
147 "thread register definitions from python for pid %" PRIu64,
151 m_operating_system_interface_sp->GetRegisterInfo();
155 m_register_info_up = DynamicRegisterInfo::Create(
156 *dictionary, m_process->GetTarget().GetArchitecture());
157 assert(m_register_info_up);
158 assert(m_register_info_up->GetNumRegisters() > 0);
159 assert(m_register_info_up->GetNumRegisterSets() > 0);
161 return m_register_info_up.get();
164bool OperatingSystemPython::UpdateThreadList(
ThreadList &old_thread_list,
167 if (!m_interpreter || !m_operating_system_interface_sp)
173 "OperatingSystemPython::UpdateThreadList() fetching thread "
174 "data from python for pid %" PRIu64,
181 m_operating_system_interface_sp->GetThreadInfo();
183 const uint32_t num_cores = core_thread_list.
GetSize(
false);
188 std::vector<bool> core_used_map(num_cores,
false);
192 threads_list->Dump(strm);
196 const uint32_t num_threads = threads_list->GetSize();
197 for (uint32_t i = 0; i < num_threads; ++i) {
199 threads_list->GetItemAtIndex(i);
200 if (
auto thread_dict = thread_dict_obj->GetAsDictionary()) {
201 ThreadSP thread_sp(CreateThreadFromThreadInfo(
202 *thread_dict, core_thread_list, old_thread_list, core_used_map,
213 uint32_t insert_idx = 0;
214 for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
215 if (!core_used_map[core_idx]) {
222 return new_thread_list.
GetSize(
false) > 0;
225ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
227 ThreadList &old_thread_list, std::vector<bool> &core_used_map,
228 bool *did_create_ptr) {
234 uint32_t core_number;
236 llvm::StringRef name;
237 llvm::StringRef queue;
251 if (!IsOperatingSystemPluginThread(thread_sp)) {
261 *did_create_ptr =
true;
262 thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
266 if (core_number < core_thread_list.
GetSize(
false)) {
269 if (core_thread_sp) {
272 if (core_number < core_used_map.size())
273 core_used_map[core_number] =
true;
275 ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
276 if (backing_core_thread_sp) {
277 thread_sp->SetBackingThread(backing_core_thread_sp);
279 thread_sp->SetBackingThread(core_thread_sp);
286void OperatingSystemPython::ThreadWasSelected(
Thread *thread) {}
289OperatingSystemPython::CreateRegisterContextForThread(
Thread *thread,
292 if (!m_interpreter || !m_script_object_sp || !thread)
295 if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
304 "OperatingSystemPython::CreateRegisterContextForThread (tid "
305 "= 0x%" PRIx64
", 0x%" PRIx64
", reg_data_addr = 0x%" PRIx64
306 ") creating memory register context",
308 reg_ctx_sp = std::make_shared<RegisterContextMemory>(
309 *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr);
314 "OperatingSystemPython::CreateRegisterContextForThread (tid "
315 "= 0x%" PRIx64
", 0x%" PRIx64
316 ") fetching register data from python",
319 std::optional<std::string> reg_context_data =
320 m_operating_system_interface_sp->GetRegisterContextForTID(
322 if (reg_context_data) {
323 std::string value = *reg_context_data;
325 if (data_sp->GetByteSize()) {
328 if (reg_ctx_memory) {
329 reg_ctx_sp.reset(reg_ctx_memory);
339 "OperatingSystemPython::CreateRegisterContextForThread (tid "
340 "= 0x%" PRIx64
") forcing a dummy register context",
342 Target &target = m_process->GetTarget();
343 reg_ctx_sp = std::make_shared<RegisterContextDummy>(
364 "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
365 ", context = 0x%" PRIx64
") fetching register data from python",
368 if (m_interpreter && m_script_object_sp) {
371 m_operating_system_interface_sp->CreateThread(tid, context);
373 std::vector<bool> core_used_map;
374 if (thread_info_dict) {
376 ThreadList &thread_list = m_process->GetThreadList();
377 bool did_create =
false;
379 CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
380 thread_list, core_used_map, &did_create));
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
#define LLDB_PLUGIN_DEFINE(PluginName)
void SetAllRegisterData(const lldb::DataBufferSP &data_sp)
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
A subclass of DataBuffer that stores a data buffer on the heap.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const ConstString & GetFilename() const
Filename string const get accessor.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
A plug-in interface definition class for halted OS helpers.
FileSpec GetPythonOSPluginPath() const
A plug-in interface definition class for debugging a process.
lldb::TargetSP CalculateTarget() override
const char * GetData() const
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
std::shared_ptr< Generic > GenericSP
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< Array > ArraySP
const ArchSpec & GetArchitecture() const
void AddThread(const lldb::ThreadSP &thread_sp)
void InsertThread(const lldb::ThreadSP &thread_sp, uint32_t idx)
uint32_t GetSize(bool can_update=true)
lldb::ThreadSP GetThreadAtIndex(uint32_t idx, bool can_update=true)
lldb::ThreadSP FindThreadByID(lldb::tid_t tid, bool can_update=true)
virtual lldb::user_id_t GetProtocolID() const
#define LLDB_INVALID_THREAD_ID
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
lldb::user_id_t GetID() const
Get accessor for the user ID.