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 //===----------------------------------------------------------------------===//
9*93a64300SDaniel Malea 
10*93a64300SDaniel Malea #include "lldb/lldb-python.h"
11*93a64300SDaniel 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 
1851c22be69SGreg Clayton     if (log)
186d01b2953SDaniel Malea         log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID());
1871c22be69SGreg Clayton 
1886167ab28SEnrico Granata     auto object_sp = m_interpreter->OSPlugin_QueryForThreadsInfo(m_interpreter->MakeScriptObject(m_python_object));
1896167ab28SEnrico Granata     if (!object_sp)
1906167ab28SEnrico Granata         return NULL;
191a83b6cf2SGreg Clayton     PythonDataObject pyobj((PyObject*)object_sp->GetObject());
192435ce139SGreg Clayton     PythonDataArray threads_array (pyobj.GetArrayObject());
193435ce139SGreg Clayton     if (threads_array)
194435ce139SGreg Clayton     {
195435ce139SGreg Clayton //        const uint32_t num_old_threads = old_thread_list.GetSize(false);
196435ce139SGreg Clayton //        for (uint32_t i=0; i<num_old_threads; ++i)
197b3e77600SGreg Clayton //        {
198435ce139SGreg Clayton //            ThreadSP old_thread_sp(old_thread_list.GetThreadAtIndex(i, false));
199435ce139SGreg Clayton //            if (old_thread_sp->GetID() < 0x10000)
200435ce139SGreg Clayton //                new_thread_list.AddThread (old_thread_sp);
201b3e77600SGreg Clayton //        }
202435ce139SGreg Clayton 
203435ce139SGreg Clayton         PythonDataString tid_pystr("tid");
204435ce139SGreg Clayton         PythonDataString name_pystr("name");
205435ce139SGreg Clayton         PythonDataString queue_pystr("queue");
206435ce139SGreg Clayton         PythonDataString state_pystr("state");
207435ce139SGreg Clayton         PythonDataString stop_reason_pystr("stop_reason");
208ead45e01SGreg Clayton         PythonDataString reg_data_addr_pystr ("register_data_addr");
209435ce139SGreg Clayton 
210435ce139SGreg Clayton         const uint32_t num_threads = threads_array.GetSize();
211435ce139SGreg Clayton         for (uint32_t i=0; i<num_threads; ++i)
212435ce139SGreg Clayton         {
213435ce139SGreg Clayton             PythonDataDictionary thread_dict(threads_array.GetItemAtIndex(i).GetDictionaryObject());
214435ce139SGreg Clayton             if (thread_dict)
215435ce139SGreg Clayton             {
216435ce139SGreg Clayton                 const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID);
217ead45e01SGreg Clayton                 const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS);
218435ce139SGreg Clayton                 const char *name = thread_dict.GetItemForKeyAsString (name_pystr);
219435ce139SGreg Clayton                 const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr);
220435ce139SGreg Clayton                 //const char *state = thread_dict.GetItemForKeyAsString (state_pystr);
221435ce139SGreg Clayton                 //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr);
222435ce139SGreg Clayton 
223435ce139SGreg Clayton                 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
224435ce139SGreg Clayton                 if (!thread_sp)
2254f465cffSJim Ingham                     thread_sp.reset (new ThreadMemory (*m_process,
226435ce139SGreg Clayton                                                        tid,
227435ce139SGreg Clayton                                                        name,
228ead45e01SGreg Clayton                                                        queue,
229ead45e01SGreg Clayton                                                        reg_data_addr));
230435ce139SGreg Clayton                 new_thread_list.AddThread(thread_sp);
231435ce139SGreg Clayton 
232435ce139SGreg Clayton             }
233435ce139SGreg Clayton         }
234435ce139SGreg Clayton     }
235435ce139SGreg Clayton     else
236435ce139SGreg Clayton     {
237b3e77600SGreg Clayton         new_thread_list = old_thread_list;
238435ce139SGreg Clayton     }
239b3e77600SGreg Clayton     return new_thread_list.GetSize(false) > 0;
240b3e77600SGreg Clayton }
241b3e77600SGreg Clayton 
242b3e77600SGreg Clayton void
243b3e77600SGreg Clayton OperatingSystemPython::ThreadWasSelected (Thread *thread)
244b3e77600SGreg Clayton {
245b3e77600SGreg Clayton }
246b3e77600SGreg Clayton 
247b3e77600SGreg Clayton RegisterContextSP
248ead45e01SGreg Clayton OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, lldb::addr_t reg_data_addr)
249b3e77600SGreg Clayton {
250435ce139SGreg Clayton     RegisterContextSP reg_ctx_sp;
2516167ab28SEnrico Granata     if (!m_interpreter || !m_python_object || !thread)
252c44306f7SFilipe Cabecinhas         return RegisterContextSP();
2531c22be69SGreg Clayton 
2541c22be69SGreg Clayton     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
2551c22be69SGreg Clayton 
256ead45e01SGreg Clayton     if (reg_data_addr != LLDB_INVALID_ADDRESS)
257ead45e01SGreg Clayton     {
258ead45e01SGreg Clayton         // The registers data is in contiguous memory, just create the register
259ead45e01SGreg Clayton         // context using the address provided
260ead45e01SGreg Clayton         if (log)
261d01b2953SDaniel Malea             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context", thread->GetID(), reg_data_addr);
262ead45e01SGreg Clayton         reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr));
263ead45e01SGreg Clayton     }
264ead45e01SGreg Clayton     else
265ead45e01SGreg Clayton     {
266ead45e01SGreg Clayton         // No register data address is provided, query the python plug-in to let
267ead45e01SGreg Clayton         // it make up the data as it sees fit
2681c22be69SGreg Clayton         if (log)
269d01b2953SDaniel Malea             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") fetching register data from python", thread->GetID());
2701c22be69SGreg Clayton 
271435ce139SGreg Clayton         auto object_sp = m_interpreter->OSPlugin_QueryForRegisterContextData (m_interpreter->MakeScriptObject(m_python_object),
2726167ab28SEnrico Granata                                                                               thread->GetID());
273435ce139SGreg Clayton 
2746167ab28SEnrico Granata         if (!object_sp)
275c44306f7SFilipe Cabecinhas             return RegisterContextSP();
2766167ab28SEnrico Granata 
277435ce139SGreg Clayton         PythonDataString reg_context_data((PyObject*)object_sp->GetObject());
278435ce139SGreg Clayton         if (reg_context_data)
279435ce139SGreg Clayton         {
280435ce139SGreg Clayton             DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
281435ce139SGreg Clayton                                                       reg_context_data.GetSize()));
282435ce139SGreg Clayton             if (data_sp->GetByteSize())
283435ce139SGreg Clayton             {
284435ce139SGreg Clayton                 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
285435ce139SGreg Clayton                 if (reg_ctx_memory)
286435ce139SGreg Clayton                 {
287435ce139SGreg Clayton                     reg_ctx_sp.reset(reg_ctx_memory);
288435ce139SGreg Clayton                     reg_ctx_memory->SetAllRegisterData (data_sp);
289435ce139SGreg Clayton                 }
290435ce139SGreg Clayton             }
291435ce139SGreg Clayton         }
292ead45e01SGreg Clayton     }
293b3e77600SGreg Clayton     return reg_ctx_sp;
294b3e77600SGreg Clayton }
295b3e77600SGreg Clayton 
296b3e77600SGreg Clayton StopInfoSP
297b3e77600SGreg Clayton OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
298b3e77600SGreg Clayton {
299b3e77600SGreg Clayton     // We should have gotten the thread stop info from the dictionary of data for
300b3e77600SGreg Clayton     // the thread in the initial call to get_thread_info(), this should have been
301b3e77600SGreg Clayton     // cached so we can return it here
302b3e77600SGreg Clayton     StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
303b3e77600SGreg Clayton     return stop_info_sp;
304b3e77600SGreg Clayton }
305b3e77600SGreg Clayton 
306b3e77600SGreg Clayton 
307b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON
308