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
10 #include "lldb/lldb-python.h"
11
12 #ifndef LLDB_DISABLE_PYTHON
13
14 #include "OperatingSystemPython.h"
15 // C Includes
16 // C++ Includes
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"
40
41 using namespace lldb;
42 using namespace lldb_private;
43
44 void
Initialize()45 OperatingSystemPython::Initialize()
46 {
47 PluginManager::RegisterPlugin (GetPluginNameStatic(),
48 GetPluginDescriptionStatic(),
49 CreateInstance);
50 }
51
52 void
Terminate()53 OperatingSystemPython::Terminate()
54 {
55 PluginManager::UnregisterPlugin (CreateInstance);
56 }
57
58 OperatingSystem *
CreateInstance(Process * process,bool force)59 OperatingSystemPython::CreateInstance (Process *process, bool force)
60 {
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())
64 {
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();
68 }
69 return NULL;
70 }
71
72
73 ConstString
GetPluginNameStatic()74 OperatingSystemPython::GetPluginNameStatic()
75 {
76 static ConstString g_name("python");
77 return g_name;
78 }
79
80 const char *
GetPluginDescriptionStatic()81 OperatingSystemPython::GetPluginDescriptionStatic()
82 {
83 return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality.";
84 }
85
86
OperatingSystemPython(lldb_private::Process * process,const FileSpec & python_module_path)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 (),
91 m_interpreter (NULL),
92 m_python_object_sp ()
93 {
94 if (!process)
95 return;
96 TargetSP target_sp = process->CalculateTarget();
97 if (!target_sp)
98 return;
99 m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
100 if (m_interpreter)
101 {
102
103 std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
104 if (!os_plugin_class_name.empty())
105 {
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));
110 Error error;
111 if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error))
112 {
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;
122 }
123 }
124 }
125 }
126
~OperatingSystemPython()127 OperatingSystemPython::~OperatingSystemPython ()
128 {
129 }
130
131 DynamicRegisterInfo *
GetDynamicRegisterInfo()132 OperatingSystemPython::GetDynamicRegisterInfo ()
133 {
134 if (m_register_info_ap.get() == NULL)
135 {
136 if (!m_interpreter || !m_python_object_sp)
137 return NULL;
138 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OS));
139
140 if (log)
141 log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID());
142
143 PythonDictionary dictionary(m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp));
144 if (!dictionary)
145 return NULL;
146
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);
150 }
151 return m_register_info_ap.get();
152 }
153
154 //------------------------------------------------------------------
155 // PluginInterface protocol
156 //------------------------------------------------------------------
157 ConstString
GetPluginName()158 OperatingSystemPython::GetPluginName()
159 {
160 return GetPluginNameStatic();
161 }
162
163 uint32_t
GetPluginVersion()164 OperatingSystemPython::GetPluginVersion()
165 {
166 return 1;
167 }
168
169 bool
UpdateThreadList(ThreadList & old_thread_list,ThreadList & core_thread_list,ThreadList & new_thread_list)170 OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list,
171 ThreadList &core_thread_list,
172 ThreadList &new_thread_list)
173 {
174 if (!m_interpreter || !m_python_object_sp)
175 return false;
176
177 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OS));
178
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());
184
185 if (log)
186 log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID());
187
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.
190
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));
193 if (threads_list)
194 {
195 if (log)
196 {
197 StreamString strm;
198 threads_list.Dump(strm);
199 log->Printf("threads_list = %s", strm.GetString().c_str());
200 }
201 uint32_t i;
202 const uint32_t num_threads = threads_list.GetSize();
203 if (num_threads > 0)
204 {
205 for (i=0; i<num_threads; ++i)
206 {
207 PythonDictionary thread_dict(threads_list.GetItemAtIndex(i));
208 if (thread_dict)
209 {
210 ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_dict, core_thread_list, old_thread_list, NULL));
211 if (thread_sp)
212 new_thread_list.AddThread(thread_sp);
213 }
214 }
215 }
216 }
217
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;
222
223 return new_thread_list.GetSize(false) > 0;
224 }
225
226 ThreadSP
CreateThreadFromThreadInfo(PythonDictionary & thread_dict,ThreadList & core_thread_list,ThreadList & old_thread_list,bool * did_create_ptr)227 OperatingSystemPython::CreateThreadFromThreadInfo (PythonDictionary &thread_dict,
228 ThreadList &core_thread_list,
229 ThreadList &old_thread_list,
230 bool *did_create_ptr)
231 {
232 ThreadSP thread_sp;
233 if (thread_dict)
234 {
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)
238 {
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");
245
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);
252
253 // See if a thread already exists for "tid"
254 thread_sp = old_thread_list.FindThreadByID (tid, false);
255 if (thread_sp)
256 {
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))
260 {
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.
264 thread_sp.reset();
265 }
266 }
267
268 if (!thread_sp)
269 {
270 if (did_create_ptr)
271 *did_create_ptr = true;
272 thread_sp.reset (new ThreadMemory (*m_process,
273 tid,
274 name,
275 queue,
276 reg_data_addr));
277
278 }
279
280 if (core_number < core_thread_list.GetSize(false))
281 {
282 ThreadSP core_thread_sp (core_thread_list.GetThreadAtIndex(core_number, false));
283 if (core_thread_sp)
284 {
285 ThreadSP backing_core_thread_sp (core_thread_sp->GetBackingThread());
286 if (backing_core_thread_sp)
287 {
288 thread_sp->SetBackingThread(backing_core_thread_sp);
289 }
290 else
291 {
292 thread_sp->SetBackingThread(core_thread_sp);
293 }
294 }
295 }
296 }
297 }
298 return thread_sp;
299 }
300
301
302
303 void
ThreadWasSelected(Thread * thread)304 OperatingSystemPython::ThreadWasSelected (Thread *thread)
305 {
306 }
307
308 RegisterContextSP
CreateRegisterContextForThread(Thread * thread,addr_t reg_data_addr)309 OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, addr_t reg_data_addr)
310 {
311 RegisterContextSP reg_ctx_sp;
312 if (!m_interpreter || !m_python_object_sp || !thread)
313 return reg_ctx_sp;
314
315 if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
316 return reg_ctx_sp;
317
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());
323
324 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
325
326 auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure python objects stays alive
327 if (reg_data_addr != LLDB_INVALID_ADDRESS)
328 {
329 // The registers data is in contiguous memory, just create the register
330 // context using the address provided
331 if (log)
332 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context",
333 thread->GetID(),
334 thread->GetProtocolID(),
335 reg_data_addr);
336 reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr));
337 }
338 else
339 {
340 // No register data address is provided, query the python plug-in to let
341 // it make up the data as it sees fit
342 if (log)
343 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ") fetching register data from python",
344 thread->GetID(),
345 thread->GetProtocolID());
346
347 PythonString reg_context_data(m_interpreter->OSPlugin_RegisterContextData (m_python_object_sp, thread->GetID()));
348 if (reg_context_data)
349 {
350 DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
351 reg_context_data.GetSize()));
352 if (data_sp->GetByteSize())
353 {
354 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
355 if (reg_ctx_memory)
356 {
357 reg_ctx_sp.reset(reg_ctx_memory);
358 reg_ctx_memory->SetAllRegisterData (data_sp);
359 }
360 }
361 }
362 }
363 // if we still have no register data, fallback on a dummy context to avoid crashing
364 if (!reg_ctx_sp)
365 {
366 if (log)
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()));
369 }
370 return reg_ctx_sp;
371 }
372
373 StopInfoSP
CreateThreadStopReason(lldb_private::Thread * thread)374 OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
375 {
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));
380 return stop_info_sp;
381 }
382
383 lldb::ThreadSP
CreateThread(lldb::tid_t tid,addr_t context)384 OperatingSystemPython::CreateThread (lldb::tid_t tid, addr_t context)
385 {
386 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
387
388 if (log)
389 log->Printf ("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 ", context = 0x%" PRIx64 ") fetching register data from python", tid, context);
390
391 if (m_interpreter && m_python_object_sp)
392 {
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());
398
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)
402 {
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));
407 if (did_create)
408 thread_list.AddThread(thread_sp);
409 return thread_sp;
410 }
411 }
412 return ThreadSP();
413 }
414
415
416
417 #endif // #ifndef LLDB_DISABLE_PYTHON
418