OperatingSystemPython.cpp revision e15e58facd4814a2be1cc1aa385e9f9125b92993
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
41using namespace lldb;
42using namespace lldb_private;
43
44void
45OperatingSystemPython::Initialize()
46{
47    PluginManager::RegisterPlugin (GetPluginNameStatic(),
48                                   GetPluginDescriptionStatic(),
49                                   CreateInstance);
50}
51
52void
53OperatingSystemPython::Terminate()
54{
55    PluginManager::UnregisterPlugin (CreateInstance);
56}
57
58OperatingSystem *
59OperatingSystemPython::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
73ConstString
74OperatingSystemPython::GetPluginNameStatic()
75{
76    static ConstString g_name("python");
77    return g_name;
78}
79
80const char *
81OperatingSystemPython::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
87OperatingSystemPython::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
127OperatingSystemPython::~OperatingSystemPython ()
128{
129}
130
131DynamicRegisterInfo *
132OperatingSystemPython::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//------------------------------------------------------------------
157ConstString
158OperatingSystemPython::GetPluginName()
159{
160    return GetPluginNameStatic();
161}
162
163uint32_t
164OperatingSystemPython::GetPluginVersion()
165{
166    return 1;
167}
168
169bool
170OperatingSystemPython::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                    if (thread_dict.GetItemForKey("core"))
211                    {
212                        // We have some threads that are saying they are on a "core", which means
213                        // they map the threads that are gotten from the lldb_private::Process subclass
214                        // so clear the new threads list so the core threads don't show up
215                        new_thread_list.Clear();
216                        break;
217                    }
218                }
219            }
220            for (i=0; i<num_threads; ++i)
221            {
222                PythonDictionary thread_dict(threads_list.GetItemAtIndex(i));
223                if (thread_dict)
224                {
225                    ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_dict, core_thread_list, old_thread_list, NULL));
226                    if (thread_sp)
227                        new_thread_list.AddThread(thread_sp);
228                }
229            }
230        }
231    }
232
233    if (new_thread_list.GetSize(false) == 0)
234        new_thread_list = old_thread_list;
235
236    return new_thread_list.GetSize(false) > 0;
237}
238
239ThreadSP
240OperatingSystemPython::CreateThreadFromThreadInfo (PythonDictionary &thread_dict,
241                                                   ThreadList &core_thread_list,
242                                                   ThreadList &old_thread_list,
243                                                   bool *did_create_ptr)
244{
245    ThreadSP thread_sp;
246    if (thread_dict)
247    {
248        PythonString tid_pystr("tid");
249        const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID);
250        if (tid != LLDB_INVALID_THREAD_ID)
251        {
252            PythonString core_pystr("core");
253            PythonString name_pystr("name");
254            PythonString queue_pystr("queue");
255            //PythonString state_pystr("state");
256            //PythonString stop_reason_pystr("stop_reason");
257            PythonString reg_data_addr_pystr ("register_data_addr");
258
259            const uint32_t core_number = thread_dict.GetItemForKeyAsInteger (core_pystr, UINT32_MAX);
260            const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS);
261            const char *name = thread_dict.GetItemForKeyAsString (name_pystr);
262            const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr);
263            //const char *state = thread_dict.GetItemForKeyAsString (state_pystr);
264            //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr);
265
266            // See if a thread already exists for "tid"
267            thread_sp = old_thread_list.FindThreadByID (tid, false);
268            if (thread_sp)
269            {
270                // A thread already does exist for "tid", make sure it was an operating system
271                // plug-in generated thread.
272                if (!IsOperatingSystemPluginThread(thread_sp))
273                {
274                    // We have thread ID overlap between the protocol threads and the
275                    // operating system threads, clear the thread so we create an
276                    // operating system thread for this.
277                    thread_sp.reset();
278                }
279            }
280
281            if (!thread_sp)
282            {
283                if (did_create_ptr)
284                    *did_create_ptr = true;
285                thread_sp.reset (new ThreadMemory (*m_process,
286                                                   tid,
287                                                   name,
288                                                   queue,
289                                                   reg_data_addr));
290
291            }
292
293            if (core_number < core_thread_list.GetSize(false))
294            {
295                ThreadSP core_thread_sp (core_thread_list.GetThreadAtIndex(core_number, false));
296                if (core_thread_sp)
297                {
298                    ThreadSP backing_core_thread_sp (core_thread_sp->GetBackingThread());
299                    if (backing_core_thread_sp)
300                    {
301                        thread_sp->SetBackingThread(backing_core_thread_sp);
302                    }
303                    else
304                    {
305                        thread_sp->SetBackingThread(core_thread_sp);
306                    }
307                }
308            }
309        }
310    }
311    return thread_sp;
312}
313
314
315
316void
317OperatingSystemPython::ThreadWasSelected (Thread *thread)
318{
319}
320
321RegisterContextSP
322OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, addr_t reg_data_addr)
323{
324    RegisterContextSP reg_ctx_sp;
325    if (!m_interpreter || !m_python_object_sp || !thread)
326        return reg_ctx_sp;
327
328    if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
329        return reg_ctx_sp;
330
331    // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
332    // content of the process, and we're going to use python, which requires the API lock to do it.
333    // So get & hold that.  This is a recursive lock so we can grant it to any Python code called on the stack below us.
334    Target &target = m_process->GetTarget();
335    Mutex::Locker api_locker (target.GetAPIMutex());
336
337    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
338
339    auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure python objects stays alive
340    if (reg_data_addr != LLDB_INVALID_ADDRESS)
341    {
342        // The registers data is in contiguous memory, just create the register
343        // context using the address provided
344        if (log)
345            log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context",
346                         thread->GetID(),
347                         thread->GetProtocolID(),
348                         reg_data_addr);
349        reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr));
350    }
351    else
352    {
353        // No register data address is provided, query the python plug-in to let
354        // it make up the data as it sees fit
355        if (log)
356            log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ") fetching register data from python",
357                         thread->GetID(),
358                         thread->GetProtocolID());
359
360        PythonString reg_context_data(m_interpreter->OSPlugin_RegisterContextData (m_python_object_sp, thread->GetID()));
361        if (reg_context_data)
362        {
363            DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
364                                                      reg_context_data.GetSize()));
365            if (data_sp->GetByteSize())
366            {
367                RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
368                if (reg_ctx_memory)
369                {
370                    reg_ctx_sp.reset(reg_ctx_memory);
371                    reg_ctx_memory->SetAllRegisterData (data_sp);
372                }
373            }
374        }
375    }
376    // if we still have no register data, fallback on a dummy context to avoid crashing
377    if (!reg_ctx_sp)
378    {
379        if (log)
380            log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") forcing a dummy register context", thread->GetID());
381        reg_ctx_sp.reset(new RegisterContextDummy(*thread,0,target.GetArchitecture().GetAddressByteSize()));
382    }
383    return reg_ctx_sp;
384}
385
386StopInfoSP
387OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
388{
389    // We should have gotten the thread stop info from the dictionary of data for
390    // the thread in the initial call to get_thread_info(), this should have been
391    // cached so we can return it here
392    StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
393    return stop_info_sp;
394}
395
396lldb::ThreadSP
397OperatingSystemPython::CreateThread (lldb::tid_t tid, addr_t context)
398{
399    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
400
401    if (log)
402        log->Printf ("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 ", context = 0x%" PRIx64 ") fetching register data from python", tid, context);
403
404    if (m_interpreter && m_python_object_sp)
405    {
406        // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
407        // content of the process, and we're going to use python, which requires the API lock to do it.
408        // So get & hold that.  This is a recursive lock so we can grant it to any Python code called on the stack below us.
409        Target &target = m_process->GetTarget();
410        Mutex::Locker api_locker (target.GetAPIMutex());
411
412        auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure thread_info_dict stays alive
413        PythonDictionary thread_info_dict (m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context));
414        if (thread_info_dict)
415        {
416            ThreadList core_threads(m_process);
417            ThreadList &thread_list = m_process->GetThreadList();
418            bool did_create = false;
419            ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_info_dict, core_threads, thread_list, &did_create));
420            if (did_create)
421                thread_list.AddThread(thread_sp);
422            return thread_sp;
423        }
424    }
425    return ThreadSP();
426}
427
428
429
430#endif // #ifndef LLDB_DISABLE_PYTHON
431