xref: /llvm-project/lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp (revision c9d645d30619ffad3e256928cb3d8cdc4c541998)
1 //===-- OperatingSystemPython.cpp --------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_DISABLE_PYTHON
10 
11 #include "OperatingSystemPython.h"
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 #include "lldb/Core/ArchSpec.h"
16 #include "lldb/Core/DataBufferHeap.h"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Interpreter/PythonDataObjects.h"
21 #include "lldb/Core/RegisterValue.h"
22 #include "lldb/Core/ValueObjectVariable.h"
23 #include "lldb/Interpreter/CommandInterpreter.h"
24 #include "lldb/Interpreter/PythonDataObjects.h"
25 #include "lldb/Symbol/ClangNamespaceDecl.h"
26 #include "lldb/Symbol/ObjectFile.h"
27 #include "lldb/Symbol/VariableList.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/StopInfo.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/ThreadList.h"
32 #include "lldb/Target/Thread.h"
33 #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
34 #include "Plugins/Process/Utility/RegisterContextMemory.h"
35 #include "Plugins/Process/Utility/ThreadMemory.h"
36 
37 using namespace lldb;
38 using namespace lldb_private;
39 
40 void
41 OperatingSystemPython::Initialize()
42 {
43     PluginManager::RegisterPlugin (GetPluginNameStatic(),
44                                    GetPluginDescriptionStatic(),
45                                    CreateInstance);
46 }
47 
48 void
49 OperatingSystemPython::Terminate()
50 {
51     PluginManager::UnregisterPlugin (CreateInstance);
52 }
53 
54 OperatingSystem *
55 OperatingSystemPython::CreateInstance (Process *process, bool force)
56 {
57     // Python OperatingSystem plug-ins must be requested by name, so force must be true
58     FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath());
59     if (python_os_plugin_spec && python_os_plugin_spec.Exists())
60     {
61         std::auto_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec));
62         if (os_ap.get() && os_ap->IsValid())
63             return os_ap.release();
64     }
65     return NULL;
66 }
67 
68 
69 const char *
70 OperatingSystemPython::GetPluginNameStatic()
71 {
72     return "python";
73 }
74 
75 const char *
76 OperatingSystemPython::GetPluginDescriptionStatic()
77 {
78     return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality.";
79 }
80 
81 
82 OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) :
83     OperatingSystem (process),
84     m_thread_list_valobj_sp (),
85     m_register_info_ap (),
86     m_interpreter (NULL),
87     m_python_object (NULL)
88 {
89     if (!process)
90         return;
91     lldb::TargetSP target_sp = process->CalculateTarget();
92     if (!target_sp)
93         return;
94     m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
95     if (m_interpreter)
96     {
97 
98         std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
99         if (!os_plugin_class_name.empty())
100         {
101             const bool init_session = false;
102             const bool allow_reload = true;
103             char python_module_path_cstr[PATH_MAX];
104             python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr));
105             Error error;
106             if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error))
107             {
108                 // Strip the ".py" extension if there is one
109                 size_t py_extension_pos = os_plugin_class_name.rfind(".py");
110                 if (py_extension_pos != std::string::npos)
111                     os_plugin_class_name.erase (py_extension_pos);
112                 // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn"
113                 os_plugin_class_name += ".OperatingSystemPlugIn";
114                 auto object_sp = m_interpreter->CreateOSPlugin(os_plugin_class_name.c_str(), process->CalculateProcess());
115                 if (object_sp)
116                     m_python_object = object_sp->GetObject();
117             }
118         }
119     }
120 }
121 
122 OperatingSystemPython::~OperatingSystemPython ()
123 {
124 }
125 
126 DynamicRegisterInfo *
127 OperatingSystemPython::GetDynamicRegisterInfo ()
128 {
129     if (m_register_info_ap.get() == NULL)
130     {
131         if (!m_interpreter || !m_python_object)
132             return NULL;
133         auto object_sp = m_interpreter->OSPlugin_QueryForRegisterInfo(m_interpreter->MakeScriptObject(m_python_object));
134         if (!object_sp)
135             return NULL;
136         PythonDataObject dictionary_data_obj((PyObject*)object_sp->GetObject());
137         PythonDataDictionary dictionary = dictionary_data_obj.GetDictionaryObject();
138         if (!dictionary)
139             return NULL;
140 
141         m_register_info_ap.reset (new DynamicRegisterInfo (dictionary));
142         assert (m_register_info_ap->GetNumRegisters() > 0);
143         assert (m_register_info_ap->GetNumRegisterSets() > 0);
144     }
145     return m_register_info_ap.get();
146 }
147 
148 //------------------------------------------------------------------
149 // PluginInterface protocol
150 //------------------------------------------------------------------
151 const char *
152 OperatingSystemPython::GetPluginName()
153 {
154     return "OperatingSystemPython";
155 }
156 
157 const char *
158 OperatingSystemPython::GetShortPluginName()
159 {
160     return GetPluginNameStatic();
161 }
162 
163 uint32_t
164 OperatingSystemPython::GetPluginVersion()
165 {
166     return 1;
167 }
168 
169 bool
170 OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
171 {
172 
173     if (!m_interpreter || !m_python_object)
174         return NULL;
175     auto object_sp = m_interpreter->OSPlugin_QueryForThreadsInfo(m_interpreter->MakeScriptObject(m_python_object));
176     if (!object_sp)
177         return NULL;
178     PythonDataObject pyobj((PyObject*)object_sp->GetObject());
179     PythonDataArray threads_array (pyobj.GetArrayObject());
180     if (threads_array)
181     {
182 //        const uint32_t num_old_threads = old_thread_list.GetSize(false);
183 //        for (uint32_t i=0; i<num_old_threads; ++i)
184 //        {
185 //            ThreadSP old_thread_sp(old_thread_list.GetThreadAtIndex(i, false));
186 //            if (old_thread_sp->GetID() < 0x10000)
187 //                new_thread_list.AddThread (old_thread_sp);
188 //        }
189 
190         PythonDataString tid_pystr("tid");
191         PythonDataString name_pystr("name");
192         PythonDataString queue_pystr("queue");
193         PythonDataString state_pystr("state");
194         PythonDataString stop_reason_pystr("stop_reason");
195 
196         const uint32_t num_threads = threads_array.GetSize();
197         for (uint32_t i=0; i<num_threads; ++i)
198         {
199             PythonDataDictionary thread_dict(threads_array.GetItemAtIndex(i).GetDictionaryObject());
200             if (thread_dict)
201             {
202                 const tid_t tid = thread_dict.GetItemForKeyAsInteger(tid_pystr, LLDB_INVALID_THREAD_ID);
203                 const char *name = thread_dict.GetItemForKeyAsString (name_pystr);
204                 const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr);
205                 //const char *state = thread_dict.GetItemForKeyAsString (state_pystr);
206                 //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr);
207 
208                 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
209                 if (!thread_sp)
210                     thread_sp.reset (new ThreadMemory (*m_process,
211                                                        tid,
212                                                        name,
213                                                        queue));
214                 new_thread_list.AddThread(thread_sp);
215 
216             }
217         }
218     }
219     else
220     {
221         new_thread_list = old_thread_list;
222     }
223     return new_thread_list.GetSize(false) > 0;
224 }
225 
226 void
227 OperatingSystemPython::ThreadWasSelected (Thread *thread)
228 {
229 }
230 
231 RegisterContextSP
232 OperatingSystemPython::CreateRegisterContextForThread (Thread *thread)
233 {
234     RegisterContextSP reg_ctx_sp;
235     if (!m_interpreter || !m_python_object || !thread)
236         return RegisterContextSP();
237     auto object_sp = m_interpreter->OSPlugin_QueryForRegisterContextData (m_interpreter->MakeScriptObject(m_python_object),
238                                                                           thread->GetID());
239 
240     if (!object_sp)
241         return RegisterContextSP();
242 
243     PythonDataString reg_context_data((PyObject*)object_sp->GetObject());
244     if (reg_context_data)
245     {
246         DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
247                                                   reg_context_data.GetSize()));
248         if (data_sp->GetByteSize())
249         {
250             RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
251             if (reg_ctx_memory)
252             {
253                 reg_ctx_sp.reset(reg_ctx_memory);
254                 reg_ctx_memory->SetAllRegisterData (data_sp);
255             }
256         }
257     }
258     return reg_ctx_sp;
259 }
260 
261 StopInfoSP
262 OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
263 {
264     // We should have gotten the thread stop info from the dictionary of data for
265     // the thread in the initial call to get_thread_info(), this should have been
266     // cached so we can return it here
267     StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
268     return stop_info_sp;
269 }
270 
271 
272 #endif // #ifndef LLDB_DISABLE_PYTHON
273