1 //===-- OperatingSystemPython.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 "lldb/Host/Config.h"
10 
11 #if LLDB_ENABLE_PYTHON
12 
13 #include "OperatingSystemPython.h"
14 
15 #include "Plugins/Process/Utility/RegisterContextDummy.h"
16 #include "Plugins/Process/Utility/RegisterContextMemory.h"
17 #include "Plugins/Process/Utility/ThreadMemory.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Core/ValueObjectVariable.h"
22 #include "lldb/Interpreter/CommandInterpreter.h"
23 #include "lldb/Interpreter/ScriptInterpreter.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Symbol/VariableList.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/StopInfo.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/Thread.h"
30 #include "lldb/Target/ThreadList.h"
31 #include "lldb/Utility/DataBufferHeap.h"
32 #include "lldb/Utility/LLDBLog.h"
33 #include "lldb/Utility/RegisterValue.h"
34 #include "lldb/Utility/StreamString.h"
35 #include "lldb/Utility/StructuredData.h"
36 
37 #include <memory>
38 
39 using namespace lldb;
40 using namespace lldb_private;
41 
42 LLDB_PLUGIN_DEFINE(OperatingSystemPython)
43 
44 void OperatingSystemPython::Initialize() {
45   PluginManager::RegisterPlugin(GetPluginNameStatic(),
46                                 GetPluginDescriptionStatic(), CreateInstance,
47                                 nullptr);
48 }
49 
50 void OperatingSystemPython::Terminate() {
51   PluginManager::UnregisterPlugin(CreateInstance);
52 }
53 
54 OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
55                                                        bool force) {
56   // Python OperatingSystem plug-ins must be requested by name, so force must
57   // be true
58   FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
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();
65   }
66   return nullptr;
67 }
68 
69 llvm::StringRef OperatingSystemPython::GetPluginDescriptionStatic() {
70   return "Operating system plug-in that gathers OS information from a python "
71          "class that implements the necessary OperatingSystem functionality.";
72 }
73 
74 OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
75                                              const FileSpec &python_module_path)
76     : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(),
77       m_interpreter(nullptr), m_python_object_sp() {
78   if (!process)
79     return;
80   TargetSP target_sp = process->CalculateTarget();
81   if (!target_sp)
82     return;
83   m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
84   if (m_interpreter) {
85 
86     std::string os_plugin_class_name(
87         python_module_path.GetFilename().AsCString(""));
88     if (!os_plugin_class_name.empty()) {
89       LoadScriptOptions options;
90       char python_module_path_cstr[PATH_MAX];
91       python_module_path.GetPath(python_module_path_cstr,
92                                  sizeof(python_module_path_cstr));
93       Status error;
94       if (m_interpreter->LoadScriptingModule(python_module_path_cstr, options,
95                                              error)) {
96         // Strip the ".py" extension if there is one
97         size_t py_extension_pos = os_plugin_class_name.rfind(".py");
98         if (py_extension_pos != std::string::npos)
99           os_plugin_class_name.erase(py_extension_pos);
100         // Add ".OperatingSystemPlugIn" to the module name to get a string like
101         // "modulename.OperatingSystemPlugIn"
102         os_plugin_class_name += ".OperatingSystemPlugIn";
103         StructuredData::ObjectSP object_sp =
104             m_interpreter->OSPlugin_CreatePluginObject(
105                 os_plugin_class_name.c_str(), process->CalculateProcess());
106         if (object_sp && object_sp->IsValid())
107           m_python_object_sp = object_sp;
108       }
109     }
110   }
111 }
112 
113 OperatingSystemPython::~OperatingSystemPython() = default;
114 
115 DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
116   if (m_register_info_up == nullptr) {
117     if (!m_interpreter || !m_python_object_sp)
118       return nullptr;
119     Log *log = GetLog(LLDBLog::OS);
120 
121     LLDB_LOGF(log,
122               "OperatingSystemPython::GetDynamicRegisterInfo() fetching "
123               "thread register definitions from python for pid %" PRIu64,
124               m_process->GetID());
125 
126     StructuredData::DictionarySP dictionary =
127         m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp);
128     if (!dictionary)
129       return nullptr;
130 
131     m_register_info_up = std::make_unique<DynamicRegisterInfo>(
132         *dictionary, m_process->GetTarget().GetArchitecture());
133     assert(m_register_info_up->GetNumRegisters() > 0);
134     assert(m_register_info_up->GetNumRegisterSets() > 0);
135   }
136   return m_register_info_up.get();
137 }
138 
139 bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
140                                              ThreadList &core_thread_list,
141                                              ThreadList &new_thread_list) {
142   if (!m_interpreter || !m_python_object_sp)
143     return false;
144 
145   Log *log = GetLog(LLDBLog::OS);
146 
147   // First thing we have to do is to try to get the API lock, and the
148   // interpreter lock. We're going to change the thread content of the process,
149   // and we're going to use python, which requires the API lock to do it. We
150   // need the interpreter lock to make sure thread_info_dict stays alive.
151   //
152   // If someone already has the API lock, that is ok, we just want to avoid
153   // external code from making new API calls while this call is happening.
154   //
155   // This is a recursive lock so we can grant it to any Python code called on
156   // the stack below us.
157   Target &target = m_process->GetTarget();
158   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
159                                                   std::defer_lock);
160   (void)api_lock.try_lock(); // See above.
161   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
162 
163   LLDB_LOGF(log,
164             "OperatingSystemPython::UpdateThreadList() fetching thread "
165             "data from python for pid %" PRIu64,
166             m_process->GetID());
167 
168   // The threads that are in "core_thread_list" upon entry are the threads from
169   // the lldb_private::Process subclass, no memory threads will be in this
170   // list.
171   StructuredData::ArraySP threads_list =
172       m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp);
173 
174   const uint32_t num_cores = core_thread_list.GetSize(false);
175 
176   // Make a map so we can keep track of which cores were used from the
177   // core_thread list. Any real threads/cores that weren't used should later be
178   // put back into the "new_thread_list".
179   std::vector<bool> core_used_map(num_cores, false);
180   if (threads_list) {
181     if (log) {
182       StreamString strm;
183       threads_list->Dump(strm);
184       LLDB_LOGF(log, "threads_list = %s", strm.GetData());
185     }
186 
187     const uint32_t num_threads = threads_list->GetSize();
188     for (uint32_t i = 0; i < num_threads; ++i) {
189       StructuredData::ObjectSP thread_dict_obj =
190           threads_list->GetItemAtIndex(i);
191       if (auto thread_dict = thread_dict_obj->GetAsDictionary()) {
192         ThreadSP thread_sp(CreateThreadFromThreadInfo(
193             *thread_dict, core_thread_list, old_thread_list, core_used_map,
194             nullptr));
195         if (thread_sp)
196           new_thread_list.AddThread(thread_sp);
197       }
198     }
199   }
200 
201   // Any real core threads that didn't end up backing a memory thread should
202   // still be in the main thread list, and they should be inserted at the
203   // beginning of the list
204   uint32_t insert_idx = 0;
205   for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
206     if (!core_used_map[core_idx]) {
207       new_thread_list.InsertThread(
208           core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
209       ++insert_idx;
210     }
211   }
212 
213   return new_thread_list.GetSize(false) > 0;
214 }
215 
216 ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
217     StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
218     ThreadList &old_thread_list, std::vector<bool> &core_used_map,
219     bool *did_create_ptr) {
220   ThreadSP thread_sp;
221   tid_t tid = LLDB_INVALID_THREAD_ID;
222   if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
223     return ThreadSP();
224 
225   uint32_t core_number;
226   addr_t reg_data_addr;
227   llvm::StringRef name;
228   llvm::StringRef queue;
229 
230   thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
231   thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
232                                       LLDB_INVALID_ADDRESS);
233   thread_dict.GetValueForKeyAsString("name", name);
234   thread_dict.GetValueForKeyAsString("queue", queue);
235 
236   // See if a thread already exists for "tid"
237   thread_sp = old_thread_list.FindThreadByID(tid, false);
238   if (thread_sp) {
239     // A thread already does exist for "tid", make sure it was an operating
240     // system
241     // plug-in generated thread.
242     if (!IsOperatingSystemPluginThread(thread_sp)) {
243       // We have thread ID overlap between the protocol threads and the
244       // operating system threads, clear the thread so we create an operating
245       // system thread for this.
246       thread_sp.reset();
247     }
248   }
249 
250   if (!thread_sp) {
251     if (did_create_ptr)
252       *did_create_ptr = true;
253     thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
254                                                reg_data_addr);
255   }
256 
257   if (core_number < core_thread_list.GetSize(false)) {
258     ThreadSP core_thread_sp(
259         core_thread_list.GetThreadAtIndex(core_number, false));
260     if (core_thread_sp) {
261       // Keep track of which cores were set as the backing thread for memory
262       // threads...
263       if (core_number < core_used_map.size())
264         core_used_map[core_number] = true;
265 
266       ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
267       if (backing_core_thread_sp) {
268         thread_sp->SetBackingThread(backing_core_thread_sp);
269       } else {
270         thread_sp->SetBackingThread(core_thread_sp);
271       }
272     }
273   }
274   return thread_sp;
275 }
276 
277 void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
278 
279 RegisterContextSP
280 OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
281                                                       addr_t reg_data_addr) {
282   RegisterContextSP reg_ctx_sp;
283   if (!m_interpreter || !m_python_object_sp || !thread)
284     return reg_ctx_sp;
285 
286   if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
287     return reg_ctx_sp;
288 
289   // First thing we have to do is to try to get the API lock, and the
290   // interpreter lock. We're going to change the thread content of the process,
291   // and we're going to use python, which requires the API lock to do it. We
292   // need the interpreter lock to make sure thread_info_dict stays alive.
293   //
294   // If someone already has the API lock, that is ok, we just want to avoid
295   // external code from making new API calls while this call is happening.
296   //
297   // This is a recursive lock so we can grant it to any Python code called on
298   // the stack below us.
299   Target &target = m_process->GetTarget();
300   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
301                                                   std::defer_lock);
302   (void)api_lock.try_lock(); // See above.
303   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
304 
305   Log *log = GetLog(LLDBLog::Thread);
306 
307   if (reg_data_addr != LLDB_INVALID_ADDRESS) {
308     // The registers data is in contiguous memory, just create the register
309     // context using the address provided
310     LLDB_LOGF(log,
311               "OperatingSystemPython::CreateRegisterContextForThread (tid "
312               "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
313               ") creating memory register context",
314               thread->GetID(), thread->GetProtocolID(), reg_data_addr);
315     reg_ctx_sp = std::make_shared<RegisterContextMemory>(
316         *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr);
317   } else {
318     // No register data address is provided, query the python plug-in to let it
319     // make up the data as it sees fit
320     LLDB_LOGF(log,
321               "OperatingSystemPython::CreateRegisterContextForThread (tid "
322               "= 0x%" PRIx64 ", 0x%" PRIx64
323               ") fetching register data from python",
324               thread->GetID(), thread->GetProtocolID());
325 
326     StructuredData::StringSP reg_context_data =
327         m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp,
328                                                     thread->GetID());
329     if (reg_context_data) {
330       std::string value = std::string(reg_context_data->GetValue());
331       DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
332       if (data_sp->GetByteSize()) {
333         RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
334             *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
335         if (reg_ctx_memory) {
336           reg_ctx_sp.reset(reg_ctx_memory);
337           reg_ctx_memory->SetAllRegisterData(data_sp);
338         }
339       }
340     }
341   }
342   // if we still have no register data, fallback on a dummy context to avoid
343   // crashing
344   if (!reg_ctx_sp) {
345     LLDB_LOGF(log,
346               "OperatingSystemPython::CreateRegisterContextForThread (tid "
347               "= 0x%" PRIx64 ") forcing a dummy register context",
348               thread->GetID());
349     reg_ctx_sp = std::make_shared<RegisterContextDummy>(
350         *thread, 0, target.GetArchitecture().GetAddressByteSize());
351   }
352   return reg_ctx_sp;
353 }
354 
355 StopInfoSP
356 OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
357   // We should have gotten the thread stop info from the dictionary of data for
358   // the thread in the initial call to get_thread_info(), this should have been
359   // cached so we can return it here
360   StopInfoSP
361       stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
362   return stop_info_sp;
363 }
364 
365 lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
366                                                    addr_t context) {
367   Log *log = GetLog(LLDBLog::Thread);
368 
369   LLDB_LOGF(log,
370             "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
371             ", context = 0x%" PRIx64 ") fetching register data from python",
372             tid, context);
373 
374   if (m_interpreter && m_python_object_sp) {
375     // First thing we have to do is to try to get the API lock, and the
376     // interpreter lock. We're going to change the thread content of the
377     // process, and we're going to use python, which requires the API lock to
378     // do it. We need the interpreter lock to make sure thread_info_dict stays
379     // alive.
380     //
381     // If someone already has the API lock, that is ok, we just want to avoid
382     // external code from making new API calls while this call is happening.
383     //
384     // This is a recursive lock so we can grant it to any Python code called on
385     // the stack below us.
386     Target &target = m_process->GetTarget();
387     std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
388                                                     std::defer_lock);
389     (void)api_lock.try_lock(); // See above.
390     auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
391 
392     StructuredData::DictionarySP thread_info_dict =
393         m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
394     std::vector<bool> core_used_map;
395     if (thread_info_dict) {
396       ThreadList core_threads(m_process);
397       ThreadList &thread_list = m_process->GetThreadList();
398       bool did_create = false;
399       ThreadSP thread_sp(
400           CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
401                                      thread_list, core_used_map, &did_create));
402       if (did_create)
403         thread_list.AddThread(thread_sp);
404       return thread_sp;
405     }
406   }
407   return ThreadSP();
408 }
409 
410 #endif // #if LLDB_ENABLE_PYTHON
411