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 //===----------------------------------------------------------------------===//
9b3e77600SGreg Clayton #ifndef LLDB_DISABLE_PYTHON
10b3e77600SGreg Clayton 
11b3e77600SGreg Clayton #include "OperatingSystemPython.h"
12b3e77600SGreg Clayton // C Includes
13b3e77600SGreg Clayton // C++ Includes
14b3e77600SGreg Clayton // Other libraries and framework includes
15b3e77600SGreg Clayton #include "lldb/Core/ArchSpec.h"
16b3e77600SGreg Clayton #include "lldb/Core/DataBufferHeap.h"
175790759aSEnrico Granata #include "lldb/Core/Debugger.h"
18b3e77600SGreg Clayton #include "lldb/Core/Module.h"
19b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h"
202443cbd7SGreg Clayton #include "lldb/Interpreter/PythonDataObjects.h"
21b3e77600SGreg Clayton #include "lldb/Core/RegisterValue.h"
22b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h"
235790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
245790759aSEnrico Granata #include "lldb/Interpreter/PythonDataObjects.h"
25b3e77600SGreg Clayton #include "lldb/Symbol/ClangNamespaceDecl.h"
26b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
27b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h"
28b3e77600SGreg Clayton #include "lldb/Target/Process.h"
29b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h"
30b3e77600SGreg Clayton #include "lldb/Target/Target.h"
31b3e77600SGreg Clayton #include "lldb/Target/ThreadList.h"
32b3e77600SGreg Clayton #include "lldb/Target/Thread.h"
33b3e77600SGreg Clayton #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
34b3e77600SGreg Clayton #include "Plugins/Process/Utility/RegisterContextMemory.h"
35b3e77600SGreg Clayton #include "Plugins/Process/Utility/ThreadMemory.h"
36b3e77600SGreg Clayton 
37b3e77600SGreg Clayton using namespace lldb;
38b3e77600SGreg Clayton using namespace lldb_private;
39b3e77600SGreg Clayton 
40b3e77600SGreg Clayton void
41b3e77600SGreg Clayton OperatingSystemPython::Initialize()
42b3e77600SGreg Clayton {
43b3e77600SGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
44b3e77600SGreg Clayton                                    GetPluginDescriptionStatic(),
45b3e77600SGreg Clayton                                    CreateInstance);
46b3e77600SGreg Clayton }
47b3e77600SGreg Clayton 
48b3e77600SGreg Clayton void
49b3e77600SGreg Clayton OperatingSystemPython::Terminate()
50b3e77600SGreg Clayton {
51b3e77600SGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
52b3e77600SGreg Clayton }
53b3e77600SGreg Clayton 
54b3e77600SGreg Clayton OperatingSystem *
55b3e77600SGreg Clayton OperatingSystemPython::CreateInstance (Process *process, bool force)
56b3e77600SGreg Clayton {
57b3e77600SGreg Clayton     // Python OperatingSystem plug-ins must be requested by name, so force must be true
58c9d645d3SGreg Clayton     FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath());
59c9d645d3SGreg Clayton     if (python_os_plugin_spec && python_os_plugin_spec.Exists())
60c9d645d3SGreg Clayton     {
61c9d645d3SGreg Clayton         std::auto_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec));
62c9d645d3SGreg Clayton         if (os_ap.get() && os_ap->IsValid())
63c9d645d3SGreg Clayton             return os_ap.release();
64c9d645d3SGreg Clayton     }
65b3e77600SGreg Clayton     return NULL;
66b3e77600SGreg Clayton }
67b3e77600SGreg Clayton 
68b3e77600SGreg Clayton 
69b3e77600SGreg Clayton const char *
70b3e77600SGreg Clayton OperatingSystemPython::GetPluginNameStatic()
71b3e77600SGreg Clayton {
72b3e77600SGreg Clayton     return "python";
73b3e77600SGreg Clayton }
74b3e77600SGreg Clayton 
75b3e77600SGreg Clayton const char *
76b3e77600SGreg Clayton OperatingSystemPython::GetPluginDescriptionStatic()
77b3e77600SGreg Clayton {
78b3e77600SGreg Clayton     return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality.";
79b3e77600SGreg Clayton }
80b3e77600SGreg Clayton 
81b3e77600SGreg Clayton 
82c9d645d3SGreg Clayton OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) :
83b3e77600SGreg Clayton     OperatingSystem (process),
84b3e77600SGreg Clayton     m_thread_list_valobj_sp (),
855790759aSEnrico Granata     m_register_info_ap (),
865790759aSEnrico Granata     m_interpreter (NULL),
875790759aSEnrico Granata     m_python_object (NULL)
88b3e77600SGreg Clayton {
895790759aSEnrico Granata     if (!process)
905790759aSEnrico Granata         return;
915790759aSEnrico Granata     lldb::TargetSP target_sp = process->CalculateTarget();
925790759aSEnrico Granata     if (!target_sp)
935790759aSEnrico Granata         return;
945790759aSEnrico Granata     m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
955790759aSEnrico Granata     if (m_interpreter)
965790759aSEnrico Granata     {
972443cbd7SGreg Clayton 
98c9d645d3SGreg Clayton         std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
99c9d645d3SGreg Clayton         if (!os_plugin_class_name.empty())
100c9d645d3SGreg Clayton         {
101c9d645d3SGreg Clayton             const bool init_session = false;
102c9d645d3SGreg Clayton             const bool allow_reload = true;
103c9d645d3SGreg Clayton             char python_module_path_cstr[PATH_MAX];
104c9d645d3SGreg Clayton             python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr));
105c9d645d3SGreg Clayton             Error error;
106c9d645d3SGreg Clayton             if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error))
107c9d645d3SGreg Clayton             {
108c9d645d3SGreg Clayton                 // Strip the ".py" extension if there is one
109c9d645d3SGreg Clayton                 size_t py_extension_pos = os_plugin_class_name.rfind(".py");
110c9d645d3SGreg Clayton                 if (py_extension_pos != std::string::npos)
111c9d645d3SGreg Clayton                     os_plugin_class_name.erase (py_extension_pos);
112c9d645d3SGreg Clayton                 // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn"
113c9d645d3SGreg Clayton                 os_plugin_class_name += ".OperatingSystemPlugIn";
114c9d645d3SGreg Clayton                 auto object_sp = m_interpreter->CreateOSPlugin(os_plugin_class_name.c_str(), process->CalculateProcess());
115c9d645d3SGreg Clayton                 if (object_sp)
116c9d645d3SGreg Clayton                     m_python_object = object_sp->GetObject();
117c9d645d3SGreg Clayton             }
1182443cbd7SGreg Clayton         }
1195790759aSEnrico Granata     }
120b3e77600SGreg Clayton }
121b3e77600SGreg Clayton 
122b3e77600SGreg Clayton OperatingSystemPython::~OperatingSystemPython ()
123b3e77600SGreg Clayton {
124b3e77600SGreg Clayton }
125b3e77600SGreg Clayton 
126b3e77600SGreg Clayton DynamicRegisterInfo *
127b3e77600SGreg Clayton OperatingSystemPython::GetDynamicRegisterInfo ()
128b3e77600SGreg Clayton {
1292443cbd7SGreg Clayton     if (m_register_info_ap.get() == NULL)
1302443cbd7SGreg Clayton     {
1315790759aSEnrico Granata         if (!m_interpreter || !m_python_object)
1325790759aSEnrico Granata             return NULL;
133*1c22be69SGreg Clayton         LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
134*1c22be69SGreg Clayton 
135*1c22be69SGreg Clayton         if (log)
136*1c22be69SGreg Clayton             log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %llu", m_process->GetID());
137*1c22be69SGreg Clayton 
1385790759aSEnrico Granata         auto object_sp = m_interpreter->OSPlugin_QueryForRegisterInfo(m_interpreter->MakeScriptObject(m_python_object));
1395790759aSEnrico Granata         if (!object_sp)
1405790759aSEnrico Granata             return NULL;
1415790759aSEnrico Granata         PythonDataObject dictionary_data_obj((PyObject*)object_sp->GetObject());
1425790759aSEnrico Granata         PythonDataDictionary dictionary = dictionary_data_obj.GetDictionaryObject();
1435790759aSEnrico Granata         if (!dictionary)
1445790759aSEnrico Granata             return NULL;
145b3e77600SGreg Clayton 
1462443cbd7SGreg Clayton         m_register_info_ap.reset (new DynamicRegisterInfo (dictionary));
1472443cbd7SGreg Clayton         assert (m_register_info_ap->GetNumRegisters() > 0);
1482443cbd7SGreg Clayton         assert (m_register_info_ap->GetNumRegisterSets() > 0);
149b3e77600SGreg Clayton     }
150b3e77600SGreg Clayton     return m_register_info_ap.get();
151b3e77600SGreg Clayton }
152b3e77600SGreg Clayton 
153b3e77600SGreg Clayton //------------------------------------------------------------------
154b3e77600SGreg Clayton // PluginInterface protocol
155b3e77600SGreg Clayton //------------------------------------------------------------------
156b3e77600SGreg Clayton const char *
157b3e77600SGreg Clayton OperatingSystemPython::GetPluginName()
158b3e77600SGreg Clayton {
159b3e77600SGreg Clayton     return "OperatingSystemPython";
160b3e77600SGreg Clayton }
161b3e77600SGreg Clayton 
162b3e77600SGreg Clayton const char *
163b3e77600SGreg Clayton OperatingSystemPython::GetShortPluginName()
164b3e77600SGreg Clayton {
165b3e77600SGreg Clayton     return GetPluginNameStatic();
166b3e77600SGreg Clayton }
167b3e77600SGreg Clayton 
168b3e77600SGreg Clayton uint32_t
169b3e77600SGreg Clayton OperatingSystemPython::GetPluginVersion()
170b3e77600SGreg Clayton {
171b3e77600SGreg Clayton     return 1;
172b3e77600SGreg Clayton }
173b3e77600SGreg Clayton 
174b3e77600SGreg Clayton bool
175b3e77600SGreg Clayton OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
176b3e77600SGreg Clayton {
1776167ab28SEnrico Granata     if (!m_interpreter || !m_python_object)
1786167ab28SEnrico Granata         return NULL;
179*1c22be69SGreg Clayton 
180*1c22be69SGreg Clayton     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
181*1c22be69SGreg Clayton 
182*1c22be69SGreg Clayton     if (log)
183*1c22be69SGreg Clayton         log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %llu", m_process->GetID());
184*1c22be69SGreg Clayton 
1856167ab28SEnrico Granata     auto object_sp = m_interpreter->OSPlugin_QueryForThreadsInfo(m_interpreter->MakeScriptObject(m_python_object));
1866167ab28SEnrico Granata     if (!object_sp)
1876167ab28SEnrico Granata         return NULL;
188a83b6cf2SGreg Clayton     PythonDataObject pyobj((PyObject*)object_sp->GetObject());
189435ce139SGreg Clayton     PythonDataArray threads_array (pyobj.GetArrayObject());
190435ce139SGreg Clayton     if (threads_array)
191435ce139SGreg Clayton     {
192435ce139SGreg Clayton //        const uint32_t num_old_threads = old_thread_list.GetSize(false);
193435ce139SGreg Clayton //        for (uint32_t i=0; i<num_old_threads; ++i)
194b3e77600SGreg Clayton //        {
195435ce139SGreg Clayton //            ThreadSP old_thread_sp(old_thread_list.GetThreadAtIndex(i, false));
196435ce139SGreg Clayton //            if (old_thread_sp->GetID() < 0x10000)
197435ce139SGreg Clayton //                new_thread_list.AddThread (old_thread_sp);
198b3e77600SGreg Clayton //        }
199435ce139SGreg Clayton 
200435ce139SGreg Clayton         PythonDataString tid_pystr("tid");
201435ce139SGreg Clayton         PythonDataString name_pystr("name");
202435ce139SGreg Clayton         PythonDataString queue_pystr("queue");
203435ce139SGreg Clayton         PythonDataString state_pystr("state");
204435ce139SGreg Clayton         PythonDataString stop_reason_pystr("stop_reason");
205435ce139SGreg Clayton 
206435ce139SGreg Clayton         const uint32_t num_threads = threads_array.GetSize();
207435ce139SGreg Clayton         for (uint32_t i=0; i<num_threads; ++i)
208435ce139SGreg Clayton         {
209435ce139SGreg Clayton             PythonDataDictionary thread_dict(threads_array.GetItemAtIndex(i).GetDictionaryObject());
210435ce139SGreg Clayton             if (thread_dict)
211435ce139SGreg Clayton             {
212435ce139SGreg Clayton                 const tid_t tid = thread_dict.GetItemForKeyAsInteger(tid_pystr, LLDB_INVALID_THREAD_ID);
213435ce139SGreg Clayton                 const char *name = thread_dict.GetItemForKeyAsString (name_pystr);
214435ce139SGreg Clayton                 const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr);
215435ce139SGreg Clayton                 //const char *state = thread_dict.GetItemForKeyAsString (state_pystr);
216435ce139SGreg Clayton                 //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr);
217435ce139SGreg Clayton 
218435ce139SGreg Clayton                 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
219435ce139SGreg Clayton                 if (!thread_sp)
2204f465cffSJim Ingham                     thread_sp.reset (new ThreadMemory (*m_process,
221435ce139SGreg Clayton                                                        tid,
222435ce139SGreg Clayton                                                        name,
223435ce139SGreg Clayton                                                        queue));
224435ce139SGreg Clayton                 new_thread_list.AddThread(thread_sp);
225435ce139SGreg Clayton 
226435ce139SGreg Clayton             }
227435ce139SGreg Clayton         }
228435ce139SGreg Clayton     }
229435ce139SGreg Clayton     else
230435ce139SGreg Clayton     {
231b3e77600SGreg Clayton         new_thread_list = old_thread_list;
232435ce139SGreg Clayton     }
233b3e77600SGreg Clayton     return new_thread_list.GetSize(false) > 0;
234b3e77600SGreg Clayton }
235b3e77600SGreg Clayton 
236b3e77600SGreg Clayton void
237b3e77600SGreg Clayton OperatingSystemPython::ThreadWasSelected (Thread *thread)
238b3e77600SGreg Clayton {
239b3e77600SGreg Clayton }
240b3e77600SGreg Clayton 
241b3e77600SGreg Clayton RegisterContextSP
242b3e77600SGreg Clayton OperatingSystemPython::CreateRegisterContextForThread (Thread *thread)
243b3e77600SGreg Clayton {
244435ce139SGreg Clayton     RegisterContextSP reg_ctx_sp;
2456167ab28SEnrico Granata     if (!m_interpreter || !m_python_object || !thread)
246c44306f7SFilipe Cabecinhas         return RegisterContextSP();
247*1c22be69SGreg Clayton 
248*1c22be69SGreg Clayton     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
249*1c22be69SGreg Clayton 
250*1c22be69SGreg Clayton     if (log)
251*1c22be69SGreg Clayton         log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%llx) fetching register data from python", thread->GetID());
252*1c22be69SGreg Clayton 
253435ce139SGreg Clayton     auto object_sp = m_interpreter->OSPlugin_QueryForRegisterContextData (m_interpreter->MakeScriptObject(m_python_object),
2546167ab28SEnrico Granata                                                                           thread->GetID());
255435ce139SGreg Clayton 
2566167ab28SEnrico Granata     if (!object_sp)
257c44306f7SFilipe Cabecinhas         return RegisterContextSP();
2586167ab28SEnrico Granata 
259435ce139SGreg Clayton     PythonDataString reg_context_data((PyObject*)object_sp->GetObject());
260435ce139SGreg Clayton     if (reg_context_data)
261435ce139SGreg Clayton     {
262435ce139SGreg Clayton         DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
263435ce139SGreg Clayton                                                   reg_context_data.GetSize()));
264435ce139SGreg Clayton         if (data_sp->GetByteSize())
265435ce139SGreg Clayton         {
266435ce139SGreg Clayton             RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
267435ce139SGreg Clayton             if (reg_ctx_memory)
268435ce139SGreg Clayton             {
269435ce139SGreg Clayton                 reg_ctx_sp.reset(reg_ctx_memory);
270435ce139SGreg Clayton                 reg_ctx_memory->SetAllRegisterData (data_sp);
271435ce139SGreg Clayton             }
272435ce139SGreg Clayton         }
273435ce139SGreg Clayton     }
274b3e77600SGreg Clayton     return reg_ctx_sp;
275b3e77600SGreg Clayton }
276b3e77600SGreg Clayton 
277b3e77600SGreg Clayton StopInfoSP
278b3e77600SGreg Clayton OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
279b3e77600SGreg Clayton {
280b3e77600SGreg Clayton     // We should have gotten the thread stop info from the dictionary of data for
281b3e77600SGreg Clayton     // the thread in the initial call to get_thread_info(), this should have been
282b3e77600SGreg Clayton     // cached so we can return it here
283b3e77600SGreg Clayton     StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
284b3e77600SGreg Clayton     return stop_info_sp;
285b3e77600SGreg Clayton }
286b3e77600SGreg Clayton 
287b3e77600SGreg Clayton 
288b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON
289