9#include "lldb/Host/Config.h"
36#include "llvm/Support/FormatVariadic.h"
45void OperatingSystemPython::Initialize() {
47 GetPluginDescriptionStatic(), CreateInstance,
51void OperatingSystemPython::Terminate() {
60 if (python_os_plugin_spec &&
62 std::unique_ptr<OperatingSystemPython> os_up(
63 new OperatingSystemPython(process, python_os_plugin_spec));
64 if (os_up.get() && os_up->IsValid())
65 return os_up.release();
70llvm::StringRef OperatingSystemPython::GetPluginDescriptionStatic() {
71 return "Operating system plug-in that gathers OS information from a python "
72 "class that implements the necessary OperatingSystem functionality.";
78 m_interpreter(nullptr), m_script_object_sp() {
84 m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
88 std::string os_plugin_class_name(python_module_path.GetFilename());
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)
120 auto obj_or_err = operating_system_interface->CreatePluginObject(
121 scripted_metadata, exe_ctx,
nullptr);
124 std::string msg = llvm::toString(obj_or_err.takeError());
127 llvm::formatv(
"failed to create OperatingSystemPython: {0}", msg)
134 if (!owned_script_object_sp->IsValid())
139 m_script_object_sp = owned_script_object_sp;
140 m_operating_system_interface_sp = operating_system_interface;
143OperatingSystemPython::~OperatingSystemPython() =
default;
146 if (!m_interpreter || !m_operating_system_interface_sp)
152 "OperatingSystemPython::GetDynamicRegisterInfo() fetching "
153 "thread register definitions from python for pid %" PRIu64,
157 m_operating_system_interface_sp->GetRegisterInfo();
162 *dictionary, m_process->GetTarget().GetArchitecture());
163 assert(register_info_sp);
164 assert(register_info_sp->GetNumRegisters() > 0);
165 assert(register_info_sp->GetNumRegisterSets() > 0);
167 return register_info_sp;
170bool OperatingSystemPython::UpdateThreadList(
ThreadList &old_thread_list,
173 if (!m_interpreter || !m_operating_system_interface_sp)
179 "OperatingSystemPython::UpdateThreadList() fetching thread "
180 "data from python for pid %" PRIu64,
187 m_operating_system_interface_sp->GetThreadInfo();
189 const uint32_t num_cores = core_thread_list.
GetSize(
false);
194 std::vector<bool> core_used_map(num_cores,
false);
198 threads_list->Dump(strm);
202 const uint32_t num_threads = threads_list->GetSize();
203 for (uint32_t i = 0; i < num_threads; ++i) {
205 threads_list->GetItemAtIndex(i);
206 if (
auto thread_dict = thread_dict_obj->GetAsDictionary()) {
207 ThreadSP thread_sp(CreateThreadFromThreadInfo(
208 *thread_dict, core_thread_list, old_thread_list, core_used_map,
219 uint32_t insert_idx = 0;
220 for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
221 if (!core_used_map[core_idx]) {
228 return new_thread_list.
GetSize(
false) > 0;
231ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
233 ThreadList &old_thread_list, std::vector<bool> &core_used_map,
234 bool *did_create_ptr) {
240 uint32_t core_number;
242 llvm::StringRef name;
243 llvm::StringRef queue;
257 if (!IsOperatingSystemPluginThread(thread_sp)) {
267 *did_create_ptr =
true;
268 thread_sp = std::make_shared<ThreadMemoryProvidingNameAndQueue>(
269 *m_process, tid, name, queue, reg_data_addr);
272 if (core_number < core_thread_list.
GetSize(
false)) {
275 if (core_thread_sp) {
278 if (core_number < core_used_map.size())
279 core_used_map[core_number] =
true;
281 ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
282 if (backing_core_thread_sp) {
283 thread_sp->SetBackingThread(backing_core_thread_sp);
285 thread_sp->SetBackingThread(core_thread_sp);
292void OperatingSystemPython::ThreadWasSelected(
Thread *thread) {}
295OperatingSystemPython::CreateRegisterContextForThread(
Thread *thread,
298 if (!m_interpreter || !m_script_object_sp || !thread)
301 if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
310 "OperatingSystemPython::CreateRegisterContextForThread (tid "
311 "= 0x%" PRIx64
", 0x%" PRIx64
", reg_data_addr = 0x%" PRIx64
312 ") creating memory register context",
313 thread->GetID(), thread->GetProtocolID(), reg_data_addr);
314 reg_ctx_sp = std::make_shared<RegisterContextMemory>(
315 *thread, 0, GetDynamicRegisterInfo(), reg_data_addr);
320 "OperatingSystemPython::CreateRegisterContextForThread (tid "
321 "= 0x%" PRIx64
", 0x%" PRIx64
322 ") fetching register data from python",
323 thread->GetID(), thread->GetProtocolID());
325 std::optional<std::string> reg_context_data =
326 m_operating_system_interface_sp->GetRegisterContextForTID(
328 if (reg_context_data) {
329 std::string value = *reg_context_data;
331 if (data_sp->GetByteSize()) {
334 if (reg_ctx_memory) {
335 reg_ctx_sp.reset(reg_ctx_memory);
345 "OperatingSystemPython::CreateRegisterContextForThread (tid "
346 "= 0x%" PRIx64
") forcing a dummy register context",
348 Target &target = m_process->GetTarget();
349 reg_ctx_sp = std::make_shared<RegisterContextDummy>(
370 "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
371 ", context = 0x%" PRIx64
") fetching register data from python",
374 if (m_interpreter && m_script_object_sp) {
377 m_operating_system_interface_sp->CreateThread(tid, context);
379 std::vector<bool> core_used_map;
380 if (thread_info_dict) {
382 ThreadList &thread_list = m_process->GetThreadList();
383 bool did_create =
false;
385 CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
386 thread_list, core_used_map, &did_create));
395bool OperatingSystemPython::DoesPluginReportAllThreads() {
397 if (std::optional<bool> plugin_answer =
398 m_operating_system_interface_sp->DoesPluginReportAllThreads())
399 return *plugin_answer;
400 return m_process->GetOSPluginReportsAllThreads();
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.
A subclass of DataBuffer that stores a data buffer on the heap.
static void ReportError(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report error events.
static std::unique_ptr< DynamicRegisterInfo > Create(const StructuredData::Dictionary &dict, const ArchSpec &arch)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
static FileSystem & Instance()
A plug-in interface definition class for halted OS helpers.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
FileSpec GetPythonOSPluginPath() const
A plug-in interface definition class for debugging a process.
lldb::TargetSP CalculateTarget() override
Target & GetTarget()
Get the target object pointer for this module.
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
Debugger & GetDebugger() const
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)
#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::DynamicRegisterInfo > DynamicRegisterInfoSP
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::RegisterContext > RegisterContextSP
lldb::user_id_t GetID() const
Get accessor for the user ID.