1 //===-- OperatingSystemPython.cpp --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #ifndef LLDB_DISABLE_PYTHON
14 #include "OperatingSystemPython.h"
17 // Other libraries and framework includes
18 #include "lldb/Core/ArchSpec.h"
19 #include "lldb/Core/DataBufferHeap.h"
20 #include "lldb/Core/Debugger.h"
21 #include "lldb/Core/Module.h"
22 #include "lldb/Core/PluginManager.h"
23 #include "lldb/Core/RegisterValue.h"
24 #include "lldb/Core/StreamString.h"
25 #include "lldb/Core/ValueObjectVariable.h"
26 #include "lldb/Interpreter/CommandInterpreter.h"
27 #include "lldb/Interpreter/PythonDataObjects.h"
28 #include "lldb/Symbol/ClangNamespaceDecl.h"
29 #include "lldb/Symbol/ObjectFile.h"
30 #include "lldb/Symbol/VariableList.h"
31 #include "lldb/Target/Process.h"
32 #include "lldb/Target/StopInfo.h"
33 #include "lldb/Target/Target.h"
34 #include "lldb/Target/ThreadList.h"
35 #include "lldb/Target/Thread.h"
36 #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
37 #include "Plugins/Process/Utility/RegisterContextDummy.h"
38 #include "Plugins/Process/Utility/RegisterContextMemory.h"
39 #include "Plugins/Process/Utility/ThreadMemory.h"
42 using namespace lldb_private;
45 OperatingSystemPython::Initialize()
47 PluginManager::RegisterPlugin (GetPluginNameStatic(),
48 GetPluginDescriptionStatic(),
53 OperatingSystemPython::Terminate()
55 PluginManager::UnregisterPlugin (CreateInstance);
59 OperatingSystemPython::CreateInstance (Process *process, bool force)
61 // Python OperatingSystem plug-ins must be requested by name, so force must be true
62 FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath());
63 if (python_os_plugin_spec && python_os_plugin_spec.Exists())
65 std::unique_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec));
66 if (os_ap.get() && os_ap->IsValid())
67 return os_ap.release();
74 OperatingSystemPython::GetPluginNameStatic()
76 static ConstString g_name("python");
81 OperatingSystemPython::GetPluginDescriptionStatic()
83 return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality.";
87 OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) :
88 OperatingSystem (process),
89 m_thread_list_valobj_sp (),
90 m_register_info_ap (),
96 TargetSP target_sp = process->CalculateTarget();
99 m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
103 std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
104 if (!os_plugin_class_name.empty())
106 const bool init_session = false;
107 const bool allow_reload = true;
108 char python_module_path_cstr[PATH_MAX];
109 python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr));
111 if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error))
113 // Strip the ".py" extension if there is one
114 size_t py_extension_pos = os_plugin_class_name.rfind(".py");
115 if (py_extension_pos != std::string::npos)
116 os_plugin_class_name.erase (py_extension_pos);
117 // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn"
118 os_plugin_class_name += ".OperatingSystemPlugIn";
119 ScriptInterpreterObjectSP object_sp = m_interpreter->OSPlugin_CreatePluginObject(os_plugin_class_name.c_str(), process->CalculateProcess());
120 if (object_sp && object_sp->GetObject())
121 m_python_object_sp = object_sp;
127 OperatingSystemPython::~OperatingSystemPython ()
131 DynamicRegisterInfo *
132 OperatingSystemPython::GetDynamicRegisterInfo ()
134 if (m_register_info_ap.get() == NULL)
136 if (!m_interpreter || !m_python_object_sp)
138 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OS));
141 log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID());
143 PythonDictionary dictionary(m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp));
147 m_register_info_ap.reset (new DynamicRegisterInfo (dictionary));
148 assert (m_register_info_ap->GetNumRegisters() > 0);
149 assert (m_register_info_ap->GetNumRegisterSets() > 0);
151 return m_register_info_ap.get();
154 //------------------------------------------------------------------
155 // PluginInterface protocol
156 //------------------------------------------------------------------
158 OperatingSystemPython::GetPluginName()
160 return GetPluginNameStatic();
164 OperatingSystemPython::GetPluginVersion()
170 OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list,
171 ThreadList &core_thread_list,
172 ThreadList &new_thread_list)
174 if (!m_interpreter || !m_python_object_sp)
177 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OS));
179 // First thing we have to do is get the API lock, and the run lock. We're going to change the thread
180 // content of the process, and we're going to use python, which requires the API lock to do it.
181 // So get & hold that. This is a recursive lock so we can grant it to any Python code called on the stack below us.
182 Target &target = m_process->GetTarget();
183 Mutex::Locker api_locker (target.GetAPIMutex());
186 log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID());
188 // The threads that are in "new_thread_list" upon entry are the threads from the
189 // lldb_private::Process subclass, no memory threads will be in this list.
191 auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure threads_list stays alive
192 PythonList threads_list(m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp));
198 threads_list.Dump(strm);
199 log->Printf("threads_list = %s", strm.GetString().c_str());
202 const uint32_t num_threads = threads_list.GetSize();
205 for (i=0; i<num_threads; ++i)
207 PythonDictionary thread_dict(threads_list.GetItemAtIndex(i));
210 ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_dict, core_thread_list, old_thread_list, NULL));
212 new_thread_list.AddThread(thread_sp);
218 // No new threads added from the thread info array gotten from python, just
219 // display the core threads.
220 if (new_thread_list.GetSize(false) == 0)
221 new_thread_list = core_thread_list;
223 return new_thread_list.GetSize(false) > 0;
227 OperatingSystemPython::CreateThreadFromThreadInfo (PythonDictionary &thread_dict,
228 ThreadList &core_thread_list,
229 ThreadList &old_thread_list,
230 bool *did_create_ptr)
235 PythonString tid_pystr("tid");
236 const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID);
237 if (tid != LLDB_INVALID_THREAD_ID)
239 PythonString core_pystr("core");
240 PythonString name_pystr("name");
241 PythonString queue_pystr("queue");
242 //PythonString state_pystr("state");
243 //PythonString stop_reason_pystr("stop_reason");
244 PythonString reg_data_addr_pystr ("register_data_addr");
246 const uint32_t core_number = thread_dict.GetItemForKeyAsInteger (core_pystr, UINT32_MAX);
247 const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS);
248 const char *name = thread_dict.GetItemForKeyAsString (name_pystr);
249 const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr);
250 //const char *state = thread_dict.GetItemForKeyAsString (state_pystr);
251 //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr);
253 // See if a thread already exists for "tid"
254 thread_sp = old_thread_list.FindThreadByID (tid, false);
257 // A thread already does exist for "tid", make sure it was an operating system
258 // plug-in generated thread.
259 if (!IsOperatingSystemPluginThread(thread_sp))
261 // We have thread ID overlap between the protocol threads and the
262 // operating system threads, clear the thread so we create an
263 // operating system thread for this.
271 *did_create_ptr = true;
272 thread_sp.reset (new ThreadMemory (*m_process,
280 if (core_number < core_thread_list.GetSize(false))
282 ThreadSP core_thread_sp (core_thread_list.GetThreadAtIndex(core_number, false));
285 ThreadSP backing_core_thread_sp (core_thread_sp->GetBackingThread());
286 if (backing_core_thread_sp)
288 thread_sp->SetBackingThread(backing_core_thread_sp);
292 thread_sp->SetBackingThread(core_thread_sp);
304 OperatingSystemPython::ThreadWasSelected (Thread *thread)
309 OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, addr_t reg_data_addr)
311 RegisterContextSP reg_ctx_sp;
312 if (!m_interpreter || !m_python_object_sp || !thread)
315 if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
318 // First thing we have to do is get the API lock, and the run lock. We're going to change the thread
319 // content of the process, and we're going to use python, which requires the API lock to do it.
320 // So get & hold that. This is a recursive lock so we can grant it to any Python code called on the stack below us.
321 Target &target = m_process->GetTarget();
322 Mutex::Locker api_locker (target.GetAPIMutex());
324 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
326 auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure python objects stays alive
327 if (reg_data_addr != LLDB_INVALID_ADDRESS)
329 // The registers data is in contiguous memory, just create the register
330 // context using the address provided
332 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context",
334 thread->GetProtocolID(),
336 reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr));
340 // No register data address is provided, query the python plug-in to let
341 // it make up the data as it sees fit
343 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ") fetching register data from python",
345 thread->GetProtocolID());
347 PythonString reg_context_data(m_interpreter->OSPlugin_RegisterContextData (m_python_object_sp, thread->GetID()));
348 if (reg_context_data)
350 DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
351 reg_context_data.GetSize()));
352 if (data_sp->GetByteSize())
354 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
357 reg_ctx_sp.reset(reg_ctx_memory);
358 reg_ctx_memory->SetAllRegisterData (data_sp);
363 // if we still have no register data, fallback on a dummy context to avoid crashing
367 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") forcing a dummy register context", thread->GetID());
368 reg_ctx_sp.reset(new RegisterContextDummy(*thread,0,target.GetArchitecture().GetAddressByteSize()));
374 OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
376 // We should have gotten the thread stop info from the dictionary of data for
377 // the thread in the initial call to get_thread_info(), this should have been
378 // cached so we can return it here
379 StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
384 OperatingSystemPython::CreateThread (lldb::tid_t tid, addr_t context)
386 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
389 log->Printf ("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 ", context = 0x%" PRIx64 ") fetching register data from python", tid, context);
391 if (m_interpreter && m_python_object_sp)
393 // First thing we have to do is get the API lock, and the run lock. We're going to change the thread
394 // content of the process, and we're going to use python, which requires the API lock to do it.
395 // So get & hold that. This is a recursive lock so we can grant it to any Python code called on the stack below us.
396 Target &target = m_process->GetTarget();
397 Mutex::Locker api_locker (target.GetAPIMutex());
399 auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure thread_info_dict stays alive
400 PythonDictionary thread_info_dict (m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context));
401 if (thread_info_dict)
403 ThreadList core_threads(m_process);
404 ThreadList &thread_list = m_process->GetThreadList();
405 bool did_create = false;
406 ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_info_dict, core_threads, thread_list, &did_create));
408 thread_list.AddThread(thread_sp);
417 #endif // #ifndef LLDB_DISABLE_PYTHON