1b3e77600SGreg Clayton //===-- OperatingSystemPython.cpp --------------------------------*- C++ -*-===//
2b3e77600SGreg Clayton //
3b3e77600SGreg Clayton //                     The LLVM Compiler Infrastructure
4b3e77600SGreg Clayton //
5b3e77600SGreg Clayton // This file is distributed under the University of Illinois Open Source
6b3e77600SGreg Clayton // License. See LICENSE.TXT for details.
7b3e77600SGreg Clayton //
8b3e77600SGreg Clayton //===----------------------------------------------------------------------===//
993a64300SDaniel Malea 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
12b3e77600SGreg Clayton #ifndef LLDB_DISABLE_PYTHON
13b3e77600SGreg Clayton 
14b3e77600SGreg Clayton #include "OperatingSystemPython.h"
15b3e77600SGreg Clayton // C Includes
16b3e77600SGreg Clayton // C++ Includes
17b3e77600SGreg Clayton // Other libraries and framework includes
18b3e77600SGreg Clayton #include "lldb/Core/ArchSpec.h"
19b3e77600SGreg Clayton #include "lldb/Core/DataBufferHeap.h"
205790759aSEnrico Granata #include "lldb/Core/Debugger.h"
21b3e77600SGreg Clayton #include "lldb/Core/Module.h"
22b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h"
232443cbd7SGreg Clayton #include "lldb/Interpreter/PythonDataObjects.h"
24b3e77600SGreg Clayton #include "lldb/Core/RegisterValue.h"
25b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h"
265790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
275790759aSEnrico Granata #include "lldb/Interpreter/PythonDataObjects.h"
28b3e77600SGreg Clayton #include "lldb/Symbol/ClangNamespaceDecl.h"
29b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
30b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h"
31b3e77600SGreg Clayton #include "lldb/Target/Process.h"
32b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h"
33b3e77600SGreg Clayton #include "lldb/Target/Target.h"
34b3e77600SGreg Clayton #include "lldb/Target/ThreadList.h"
35b3e77600SGreg Clayton #include "lldb/Target/Thread.h"
36b3e77600SGreg Clayton #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
37b3e77600SGreg Clayton #include "Plugins/Process/Utility/RegisterContextMemory.h"
38b3e77600SGreg Clayton #include "Plugins/Process/Utility/ThreadMemory.h"
39b3e77600SGreg Clayton 
40b3e77600SGreg Clayton using namespace lldb;
41b3e77600SGreg Clayton using namespace lldb_private;
42b3e77600SGreg Clayton 
43b3e77600SGreg Clayton void
44b3e77600SGreg Clayton OperatingSystemPython::Initialize()
45b3e77600SGreg Clayton {
46b3e77600SGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
47b3e77600SGreg Clayton                                    GetPluginDescriptionStatic(),
48b3e77600SGreg Clayton                                    CreateInstance);
49b3e77600SGreg Clayton }
50b3e77600SGreg Clayton 
51b3e77600SGreg Clayton void
52b3e77600SGreg Clayton OperatingSystemPython::Terminate()
53b3e77600SGreg Clayton {
54b3e77600SGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
55b3e77600SGreg Clayton }
56b3e77600SGreg Clayton 
57b3e77600SGreg Clayton OperatingSystem *
58b3e77600SGreg Clayton OperatingSystemPython::CreateInstance (Process *process, bool force)
59b3e77600SGreg Clayton {
60b3e77600SGreg Clayton     // Python OperatingSystem plug-ins must be requested by name, so force must be true
61c9d645d3SGreg Clayton     FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath());
62c9d645d3SGreg Clayton     if (python_os_plugin_spec && python_os_plugin_spec.Exists())
63c9d645d3SGreg Clayton     {
64c9d645d3SGreg Clayton         std::auto_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec));
65c9d645d3SGreg Clayton         if (os_ap.get() && os_ap->IsValid())
66c9d645d3SGreg Clayton             return os_ap.release();
67c9d645d3SGreg Clayton     }
68b3e77600SGreg Clayton     return NULL;
69b3e77600SGreg Clayton }
70b3e77600SGreg Clayton 
71b3e77600SGreg Clayton 
72b3e77600SGreg Clayton const char *
73b3e77600SGreg Clayton OperatingSystemPython::GetPluginNameStatic()
74b3e77600SGreg Clayton {
75b3e77600SGreg Clayton     return "python";
76b3e77600SGreg Clayton }
77b3e77600SGreg Clayton 
78b3e77600SGreg Clayton const char *
79b3e77600SGreg Clayton OperatingSystemPython::GetPluginDescriptionStatic()
80b3e77600SGreg Clayton {
81b3e77600SGreg Clayton     return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality.";
82b3e77600SGreg Clayton }
83b3e77600SGreg Clayton 
84b3e77600SGreg Clayton 
85c9d645d3SGreg Clayton OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) :
86b3e77600SGreg Clayton     OperatingSystem (process),
87b3e77600SGreg Clayton     m_thread_list_valobj_sp (),
885790759aSEnrico Granata     m_register_info_ap (),
895790759aSEnrico Granata     m_interpreter (NULL),
905790759aSEnrico Granata     m_python_object (NULL)
91b3e77600SGreg Clayton {
925790759aSEnrico Granata     if (!process)
935790759aSEnrico Granata         return;
945790759aSEnrico Granata     lldb::TargetSP target_sp = process->CalculateTarget();
955790759aSEnrico Granata     if (!target_sp)
965790759aSEnrico Granata         return;
975790759aSEnrico Granata     m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
985790759aSEnrico Granata     if (m_interpreter)
995790759aSEnrico Granata     {
1002443cbd7SGreg Clayton 
101c9d645d3SGreg Clayton         std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
102c9d645d3SGreg Clayton         if (!os_plugin_class_name.empty())
103c9d645d3SGreg Clayton         {
104c9d645d3SGreg Clayton             const bool init_session = false;
105c9d645d3SGreg Clayton             const bool allow_reload = true;
106c9d645d3SGreg Clayton             char python_module_path_cstr[PATH_MAX];
107c9d645d3SGreg Clayton             python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr));
108c9d645d3SGreg Clayton             Error error;
109c9d645d3SGreg Clayton             if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error))
110c9d645d3SGreg Clayton             {
111c9d645d3SGreg Clayton                 // Strip the ".py" extension if there is one
112c9d645d3SGreg Clayton                 size_t py_extension_pos = os_plugin_class_name.rfind(".py");
113c9d645d3SGreg Clayton                 if (py_extension_pos != std::string::npos)
114c9d645d3SGreg Clayton                     os_plugin_class_name.erase (py_extension_pos);
115c9d645d3SGreg Clayton                 // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn"
116c9d645d3SGreg Clayton                 os_plugin_class_name += ".OperatingSystemPlugIn";
117c9d645d3SGreg Clayton                 auto object_sp = m_interpreter->CreateOSPlugin(os_plugin_class_name.c_str(), process->CalculateProcess());
118c9d645d3SGreg Clayton                 if (object_sp)
119c9d645d3SGreg Clayton                     m_python_object = object_sp->GetObject();
120c9d645d3SGreg Clayton             }
1212443cbd7SGreg Clayton         }
1225790759aSEnrico Granata     }
123b3e77600SGreg Clayton }
124b3e77600SGreg Clayton 
125b3e77600SGreg Clayton OperatingSystemPython::~OperatingSystemPython ()
126b3e77600SGreg Clayton {
127b3e77600SGreg Clayton }
128b3e77600SGreg Clayton 
129b3e77600SGreg Clayton DynamicRegisterInfo *
130b3e77600SGreg Clayton OperatingSystemPython::GetDynamicRegisterInfo ()
131b3e77600SGreg Clayton {
1322443cbd7SGreg Clayton     if (m_register_info_ap.get() == NULL)
1332443cbd7SGreg Clayton     {
1345790759aSEnrico Granata         if (!m_interpreter || !m_python_object)
1355790759aSEnrico Granata             return NULL;
1361c22be69SGreg Clayton         LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1371c22be69SGreg Clayton 
1381c22be69SGreg Clayton         if (log)
139d01b2953SDaniel Malea             log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID());
1401c22be69SGreg Clayton 
1415790759aSEnrico Granata         auto object_sp = m_interpreter->OSPlugin_QueryForRegisterInfo(m_interpreter->MakeScriptObject(m_python_object));
1425790759aSEnrico Granata         if (!object_sp)
1435790759aSEnrico Granata             return NULL;
1445790759aSEnrico Granata         PythonDataObject dictionary_data_obj((PyObject*)object_sp->GetObject());
1455790759aSEnrico Granata         PythonDataDictionary dictionary = dictionary_data_obj.GetDictionaryObject();
1465790759aSEnrico Granata         if (!dictionary)
1475790759aSEnrico Granata             return NULL;
148b3e77600SGreg Clayton 
1492443cbd7SGreg Clayton         m_register_info_ap.reset (new DynamicRegisterInfo (dictionary));
1502443cbd7SGreg Clayton         assert (m_register_info_ap->GetNumRegisters() > 0);
1512443cbd7SGreg Clayton         assert (m_register_info_ap->GetNumRegisterSets() > 0);
152b3e77600SGreg Clayton     }
153b3e77600SGreg Clayton     return m_register_info_ap.get();
154b3e77600SGreg Clayton }
155b3e77600SGreg Clayton 
156b3e77600SGreg Clayton //------------------------------------------------------------------
157b3e77600SGreg Clayton // PluginInterface protocol
158b3e77600SGreg Clayton //------------------------------------------------------------------
159b3e77600SGreg Clayton const char *
160b3e77600SGreg Clayton OperatingSystemPython::GetPluginName()
161b3e77600SGreg Clayton {
162b3e77600SGreg Clayton     return "OperatingSystemPython";
163b3e77600SGreg Clayton }
164b3e77600SGreg Clayton 
165b3e77600SGreg Clayton const char *
166b3e77600SGreg Clayton OperatingSystemPython::GetShortPluginName()
167b3e77600SGreg Clayton {
168b3e77600SGreg Clayton     return GetPluginNameStatic();
169b3e77600SGreg Clayton }
170b3e77600SGreg Clayton 
171b3e77600SGreg Clayton uint32_t
172b3e77600SGreg Clayton OperatingSystemPython::GetPluginVersion()
173b3e77600SGreg Clayton {
174b3e77600SGreg Clayton     return 1;
175b3e77600SGreg Clayton }
176b3e77600SGreg Clayton 
177b3e77600SGreg Clayton bool
178b3e77600SGreg Clayton OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
179b3e77600SGreg Clayton {
1806167ab28SEnrico Granata     if (!m_interpreter || !m_python_object)
1816167ab28SEnrico Granata         return NULL;
1821c22be69SGreg Clayton 
1831c22be69SGreg Clayton     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1841c22be69SGreg Clayton 
185*85e276b8SJim Ingham     // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
186*85e276b8SJim Ingham     // content of the process, and we're going to use python, which requires the API lock to do it.
187*85e276b8SJim Ingham     // So get & hold that.  This is a recursive lock so we can grant it to any Python code called on the stack below us.
188*85e276b8SJim Ingham     Target &target = m_process->GetTarget();
189*85e276b8SJim Ingham     Mutex::Locker api_locker (target.GetAPIMutex());
190*85e276b8SJim Ingham 
1911c22be69SGreg Clayton     if (log)
192d01b2953SDaniel Malea         log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID());
1931c22be69SGreg Clayton 
1946167ab28SEnrico Granata     auto object_sp = m_interpreter->OSPlugin_QueryForThreadsInfo(m_interpreter->MakeScriptObject(m_python_object));
1956167ab28SEnrico Granata     if (!object_sp)
1966167ab28SEnrico Granata         return NULL;
197a83b6cf2SGreg Clayton     PythonDataObject pyobj((PyObject*)object_sp->GetObject());
198435ce139SGreg Clayton     PythonDataArray threads_array (pyobj.GetArrayObject());
199435ce139SGreg Clayton     if (threads_array)
200435ce139SGreg Clayton     {
201435ce139SGreg Clayton //        const uint32_t num_old_threads = old_thread_list.GetSize(false);
202435ce139SGreg Clayton //        for (uint32_t i=0; i<num_old_threads; ++i)
203b3e77600SGreg Clayton //        {
204435ce139SGreg Clayton //            ThreadSP old_thread_sp(old_thread_list.GetThreadAtIndex(i, false));
205435ce139SGreg Clayton //            if (old_thread_sp->GetID() < 0x10000)
206435ce139SGreg Clayton //                new_thread_list.AddThread (old_thread_sp);
207b3e77600SGreg Clayton //        }
208435ce139SGreg Clayton 
209435ce139SGreg Clayton         PythonDataString tid_pystr("tid");
210435ce139SGreg Clayton         PythonDataString name_pystr("name");
211435ce139SGreg Clayton         PythonDataString queue_pystr("queue");
212435ce139SGreg Clayton         PythonDataString state_pystr("state");
213435ce139SGreg Clayton         PythonDataString stop_reason_pystr("stop_reason");
214ead45e01SGreg Clayton         PythonDataString reg_data_addr_pystr ("register_data_addr");
215435ce139SGreg Clayton 
216435ce139SGreg Clayton         const uint32_t num_threads = threads_array.GetSize();
217435ce139SGreg Clayton         for (uint32_t i=0; i<num_threads; ++i)
218435ce139SGreg Clayton         {
219435ce139SGreg Clayton             PythonDataDictionary thread_dict(threads_array.GetItemAtIndex(i).GetDictionaryObject());
220435ce139SGreg Clayton             if (thread_dict)
221435ce139SGreg Clayton             {
222435ce139SGreg Clayton                 const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID);
223ead45e01SGreg Clayton                 const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS);
224435ce139SGreg Clayton                 const char *name = thread_dict.GetItemForKeyAsString (name_pystr);
225435ce139SGreg Clayton                 const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr);
226435ce139SGreg Clayton                 //const char *state = thread_dict.GetItemForKeyAsString (state_pystr);
227435ce139SGreg Clayton                 //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr);
228435ce139SGreg Clayton 
229435ce139SGreg Clayton                 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
230435ce139SGreg Clayton                 if (!thread_sp)
2314f465cffSJim Ingham                     thread_sp.reset (new ThreadMemory (*m_process,
232435ce139SGreg Clayton                                                        tid,
233435ce139SGreg Clayton                                                        name,
234ead45e01SGreg Clayton                                                        queue,
235ead45e01SGreg Clayton                                                        reg_data_addr));
236435ce139SGreg Clayton                 new_thread_list.AddThread(thread_sp);
237435ce139SGreg Clayton 
238435ce139SGreg Clayton             }
239435ce139SGreg Clayton         }
240435ce139SGreg Clayton     }
241435ce139SGreg Clayton     else
242435ce139SGreg Clayton     {
243b3e77600SGreg Clayton         new_thread_list = old_thread_list;
244435ce139SGreg Clayton     }
245b3e77600SGreg Clayton     return new_thread_list.GetSize(false) > 0;
246b3e77600SGreg Clayton }
247b3e77600SGreg Clayton 
248b3e77600SGreg Clayton void
249b3e77600SGreg Clayton OperatingSystemPython::ThreadWasSelected (Thread *thread)
250b3e77600SGreg Clayton {
251b3e77600SGreg Clayton }
252b3e77600SGreg Clayton 
253b3e77600SGreg Clayton RegisterContextSP
254ead45e01SGreg Clayton OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, lldb::addr_t reg_data_addr)
255b3e77600SGreg Clayton {
256435ce139SGreg Clayton     RegisterContextSP reg_ctx_sp;
2576167ab28SEnrico Granata     if (!m_interpreter || !m_python_object || !thread)
258c44306f7SFilipe Cabecinhas         return RegisterContextSP();
2591c22be69SGreg Clayton 
260*85e276b8SJim Ingham     // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
261*85e276b8SJim Ingham     // content of the process, and we're going to use python, which requires the API lock to do it.
262*85e276b8SJim Ingham     // So get & hold that.  This is a recursive lock so we can grant it to any Python code called on the stack below us.
263*85e276b8SJim Ingham     Target &target = m_process->GetTarget();
264*85e276b8SJim Ingham     Mutex::Locker api_locker (target.GetAPIMutex());
265*85e276b8SJim Ingham 
2661c22be69SGreg Clayton     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
2671c22be69SGreg Clayton 
268ead45e01SGreg Clayton     if (reg_data_addr != LLDB_INVALID_ADDRESS)
269ead45e01SGreg Clayton     {
270ead45e01SGreg Clayton         // The registers data is in contiguous memory, just create the register
271ead45e01SGreg Clayton         // context using the address provided
272ead45e01SGreg Clayton         if (log)
273d01b2953SDaniel Malea             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context", thread->GetID(), reg_data_addr);
274ead45e01SGreg Clayton         reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr));
275ead45e01SGreg Clayton     }
276ead45e01SGreg Clayton     else
277ead45e01SGreg Clayton     {
278ead45e01SGreg Clayton         // No register data address is provided, query the python plug-in to let
279ead45e01SGreg Clayton         // it make up the data as it sees fit
2801c22be69SGreg Clayton         if (log)
281d01b2953SDaniel Malea             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") fetching register data from python", thread->GetID());
2821c22be69SGreg Clayton 
283435ce139SGreg Clayton         auto object_sp = m_interpreter->OSPlugin_QueryForRegisterContextData (m_interpreter->MakeScriptObject(m_python_object),
2846167ab28SEnrico Granata                                                                               thread->GetID());
285435ce139SGreg Clayton 
2866167ab28SEnrico Granata         if (!object_sp)
287c44306f7SFilipe Cabecinhas             return RegisterContextSP();
2886167ab28SEnrico Granata 
289435ce139SGreg Clayton         PythonDataString reg_context_data((PyObject*)object_sp->GetObject());
290435ce139SGreg Clayton         if (reg_context_data)
291435ce139SGreg Clayton         {
292435ce139SGreg Clayton             DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
293435ce139SGreg Clayton                                                       reg_context_data.GetSize()));
294435ce139SGreg Clayton             if (data_sp->GetByteSize())
295435ce139SGreg Clayton             {
296435ce139SGreg Clayton                 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
297435ce139SGreg Clayton                 if (reg_ctx_memory)
298435ce139SGreg Clayton                 {
299435ce139SGreg Clayton                     reg_ctx_sp.reset(reg_ctx_memory);
300435ce139SGreg Clayton                     reg_ctx_memory->SetAllRegisterData (data_sp);
301435ce139SGreg Clayton                 }
302435ce139SGreg Clayton             }
303435ce139SGreg Clayton         }
304ead45e01SGreg Clayton     }
305b3e77600SGreg Clayton     return reg_ctx_sp;
306b3e77600SGreg Clayton }
307b3e77600SGreg Clayton 
308b3e77600SGreg Clayton StopInfoSP
309b3e77600SGreg Clayton OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
310b3e77600SGreg Clayton {
311b3e77600SGreg Clayton     // We should have gotten the thread stop info from the dictionary of data for
312b3e77600SGreg Clayton     // the thread in the initial call to get_thread_info(), this should have been
313b3e77600SGreg Clayton     // cached so we can return it here
314b3e77600SGreg Clayton     StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
315b3e77600SGreg Clayton     return stop_info_sp;
316b3e77600SGreg Clayton }
317b3e77600SGreg Clayton 
318b3e77600SGreg Clayton 
319b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON
320