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 //===----------------------------------------------------------------------===//
993a64300SDaniel Malea 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel 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"
23b3e77600SGreg Clayton #include "lldb/Core/RegisterValue.h"
2462f80036SGreg Clayton #include "lldb/Core/StreamString.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"
37cbd79b6cSEnrico Granata #include "Plugins/Process/Utility/RegisterContextDummy.h"
38b3e77600SGreg Clayton #include "Plugins/Process/Utility/RegisterContextMemory.h"
39b3e77600SGreg Clayton #include "Plugins/Process/Utility/ThreadMemory.h"
40b3e77600SGreg Clayton 
41b3e77600SGreg Clayton using namespace lldb;
42b3e77600SGreg Clayton using namespace lldb_private;
43b3e77600SGreg Clayton 
44b3e77600SGreg Clayton void
45b3e77600SGreg Clayton OperatingSystemPython::Initialize()
46b3e77600SGreg Clayton {
47b3e77600SGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
48b3e77600SGreg Clayton                                    GetPluginDescriptionStatic(),
49b3e77600SGreg Clayton                                    CreateInstance);
50b3e77600SGreg Clayton }
51b3e77600SGreg Clayton 
52b3e77600SGreg Clayton void
53b3e77600SGreg Clayton OperatingSystemPython::Terminate()
54b3e77600SGreg Clayton {
55b3e77600SGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
56b3e77600SGreg Clayton }
57b3e77600SGreg Clayton 
58b3e77600SGreg Clayton OperatingSystem *
59b3e77600SGreg Clayton OperatingSystemPython::CreateInstance (Process *process, bool force)
60b3e77600SGreg Clayton {
61b3e77600SGreg Clayton     // Python OperatingSystem plug-ins must be requested by name, so force must be true
62c9d645d3SGreg Clayton     FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath());
63c9d645d3SGreg Clayton     if (python_os_plugin_spec && python_os_plugin_spec.Exists())
64c9d645d3SGreg Clayton     {
657b0992d9SGreg Clayton         std::unique_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec));
66c9d645d3SGreg Clayton         if (os_ap.get() && os_ap->IsValid())
67c9d645d3SGreg Clayton             return os_ap.release();
68c9d645d3SGreg Clayton     }
69b3e77600SGreg Clayton     return NULL;
70b3e77600SGreg Clayton }
71b3e77600SGreg Clayton 
72b3e77600SGreg Clayton 
7357abc5d6SGreg Clayton ConstString
74b3e77600SGreg Clayton OperatingSystemPython::GetPluginNameStatic()
75b3e77600SGreg Clayton {
7657abc5d6SGreg Clayton     static ConstString g_name("python");
7757abc5d6SGreg Clayton     return g_name;
78b3e77600SGreg Clayton }
79b3e77600SGreg Clayton 
80b3e77600SGreg Clayton const char *
81b3e77600SGreg Clayton OperatingSystemPython::GetPluginDescriptionStatic()
82b3e77600SGreg Clayton {
83b3e77600SGreg Clayton     return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality.";
84b3e77600SGreg Clayton }
85b3e77600SGreg Clayton 
86b3e77600SGreg Clayton 
87c9d645d3SGreg Clayton OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) :
88b3e77600SGreg Clayton     OperatingSystem (process),
89b3e77600SGreg Clayton     m_thread_list_valobj_sp (),
905790759aSEnrico Granata     m_register_info_ap (),
915790759aSEnrico Granata     m_interpreter (NULL),
92a4d8747dSGreg Clayton     m_python_object_sp ()
93b3e77600SGreg Clayton {
945790759aSEnrico Granata     if (!process)
955790759aSEnrico Granata         return;
96a4d8747dSGreg Clayton     TargetSP target_sp = process->CalculateTarget();
975790759aSEnrico Granata     if (!target_sp)
985790759aSEnrico Granata         return;
995790759aSEnrico Granata     m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
1005790759aSEnrico Granata     if (m_interpreter)
1015790759aSEnrico Granata     {
1022443cbd7SGreg Clayton 
103c9d645d3SGreg Clayton         std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
104c9d645d3SGreg Clayton         if (!os_plugin_class_name.empty())
105c9d645d3SGreg Clayton         {
106c9d645d3SGreg Clayton             const bool init_session = false;
107c9d645d3SGreg Clayton             const bool allow_reload = true;
108c9d645d3SGreg Clayton             char python_module_path_cstr[PATH_MAX];
109c9d645d3SGreg Clayton             python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr));
110c9d645d3SGreg Clayton             Error error;
111c9d645d3SGreg Clayton             if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error))
112c9d645d3SGreg Clayton             {
113c9d645d3SGreg Clayton                 // Strip the ".py" extension if there is one
114c9d645d3SGreg Clayton                 size_t py_extension_pos = os_plugin_class_name.rfind(".py");
115c9d645d3SGreg Clayton                 if (py_extension_pos != std::string::npos)
116c9d645d3SGreg Clayton                     os_plugin_class_name.erase (py_extension_pos);
117c9d645d3SGreg Clayton                 // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn"
118c9d645d3SGreg Clayton                 os_plugin_class_name += ".OperatingSystemPlugIn";
119a4d8747dSGreg Clayton                 ScriptInterpreterObjectSP object_sp = m_interpreter->OSPlugin_CreatePluginObject(os_plugin_class_name.c_str(), process->CalculateProcess());
120a4d8747dSGreg Clayton                 if (object_sp && object_sp->GetObject())
121a4d8747dSGreg Clayton                     m_python_object_sp = object_sp;
122c9d645d3SGreg Clayton             }
1232443cbd7SGreg Clayton         }
1245790759aSEnrico Granata     }
125b3e77600SGreg Clayton }
126b3e77600SGreg Clayton 
127b3e77600SGreg Clayton OperatingSystemPython::~OperatingSystemPython ()
128b3e77600SGreg Clayton {
129b3e77600SGreg Clayton }
130b3e77600SGreg Clayton 
131b3e77600SGreg Clayton DynamicRegisterInfo *
132b3e77600SGreg Clayton OperatingSystemPython::GetDynamicRegisterInfo ()
133b3e77600SGreg Clayton {
1342443cbd7SGreg Clayton     if (m_register_info_ap.get() == NULL)
1352443cbd7SGreg Clayton     {
136a4d8747dSGreg Clayton         if (!m_interpreter || !m_python_object_sp)
1375790759aSEnrico Granata             return NULL;
13862f80036SGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OS));
1391c22be69SGreg Clayton 
1401c22be69SGreg Clayton         if (log)
141d01b2953SDaniel Malea             log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID());
1421c22be69SGreg Clayton 
143a4d8747dSGreg Clayton         PythonDictionary dictionary(m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp));
1445790759aSEnrico Granata         if (!dictionary)
1455790759aSEnrico Granata             return NULL;
146b3e77600SGreg Clayton 
147312bcbe8SGreg Clayton         m_register_info_ap.reset (new DynamicRegisterInfo (dictionary, m_process->GetTarget().GetArchitecture().GetByteOrder()));
1482443cbd7SGreg Clayton         assert (m_register_info_ap->GetNumRegisters() > 0);
1492443cbd7SGreg Clayton         assert (m_register_info_ap->GetNumRegisterSets() > 0);
150b3e77600SGreg Clayton     }
151b3e77600SGreg Clayton     return m_register_info_ap.get();
152b3e77600SGreg Clayton }
153b3e77600SGreg Clayton 
154b3e77600SGreg Clayton //------------------------------------------------------------------
155b3e77600SGreg Clayton // PluginInterface protocol
156b3e77600SGreg Clayton //------------------------------------------------------------------
15757abc5d6SGreg Clayton ConstString
158b3e77600SGreg Clayton OperatingSystemPython::GetPluginName()
159b3e77600SGreg Clayton {
160b3e77600SGreg Clayton     return GetPluginNameStatic();
161b3e77600SGreg Clayton }
162b3e77600SGreg Clayton 
163b3e77600SGreg Clayton uint32_t
164b3e77600SGreg Clayton OperatingSystemPython::GetPluginVersion()
165b3e77600SGreg Clayton {
166b3e77600SGreg Clayton     return 1;
167b3e77600SGreg Clayton }
168b3e77600SGreg Clayton 
169b3e77600SGreg Clayton bool
170ba4e61d3SAndrew Kaylor OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list,
171ba4e61d3SAndrew Kaylor                                          ThreadList &core_thread_list,
172ba4e61d3SAndrew Kaylor                                          ThreadList &new_thread_list)
173b3e77600SGreg Clayton {
174a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp)
175a85e6b6cSDaniel Malea         return false;
1761c22be69SGreg Clayton 
17762f80036SGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OS));
1781c22be69SGreg Clayton 
17985e276b8SJim Ingham     // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
18085e276b8SJim Ingham     // content of the process, and we're going to use python, which requires the API lock to do it.
18185e276b8SJim Ingham     // So get & hold that.  This is a recursive lock so we can grant it to any Python code called on the stack below us.
18285e276b8SJim Ingham     Target &target = m_process->GetTarget();
18385e276b8SJim Ingham     Mutex::Locker api_locker (target.GetAPIMutex());
18485e276b8SJim Ingham 
1851c22be69SGreg Clayton     if (log)
186d01b2953SDaniel Malea         log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID());
1871c22be69SGreg Clayton 
188b3ae8761SGreg Clayton     // The threads that are in "new_thread_list" upon entry are the threads from the
189b3ae8761SGreg Clayton     // lldb_private::Process subclass, no memory threads will be in this list.
190b3ae8761SGreg Clayton 
191198125a8SEnrico Granata     auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure threads_list stays alive
192a4d8747dSGreg Clayton     PythonList threads_list(m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp));
193*e98008ccSGreg Clayton 
194*e98008ccSGreg Clayton     const uint32_t num_cores = core_thread_list.GetSize(false);
195*e98008ccSGreg Clayton 
196*e98008ccSGreg Clayton     // Make a map so we can keep track of which cores were used from the
197*e98008ccSGreg Clayton     // core_thread list. Any real threads/cores that weren't used should
198*e98008ccSGreg Clayton     // later be put back into the "new_thread_list".
199*e98008ccSGreg Clayton     std::vector<bool> core_used_map(num_cores, false);
200a4d8747dSGreg Clayton     if (threads_list)
201435ce139SGreg Clayton     {
20262f80036SGreg Clayton         if (log)
20362f80036SGreg Clayton         {
20462f80036SGreg Clayton             StreamString strm;
20562f80036SGreg Clayton             threads_list.Dump(strm);
20662f80036SGreg Clayton             log->Printf("threads_list = %s", strm.GetString().c_str());
20762f80036SGreg Clayton         }
208b3ae8761SGreg Clayton         uint32_t i;
209a4d8747dSGreg Clayton         const uint32_t num_threads = threads_list.GetSize();
2106e0ff1a3SGreg Clayton         if (num_threads > 0)
2116e0ff1a3SGreg Clayton         {
212b3ae8761SGreg Clayton             for (i=0; i<num_threads; ++i)
213435ce139SGreg Clayton             {
214a4d8747dSGreg Clayton                 PythonDictionary thread_dict(threads_list.GetItemAtIndex(i));
215435ce139SGreg Clayton                 if (thread_dict)
216435ce139SGreg Clayton                 {
217*e98008ccSGreg Clayton                     ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_dict, core_thread_list, old_thread_list, core_used_map, NULL));
218a4d8747dSGreg Clayton                     if (thread_sp)
219435ce139SGreg Clayton                         new_thread_list.AddThread(thread_sp);
220435ce139SGreg Clayton                 }
221435ce139SGreg Clayton             }
222435ce139SGreg Clayton         }
223435ce139SGreg Clayton     }
2246e0ff1a3SGreg Clayton 
225*e98008ccSGreg Clayton     // Any real core threads that didn't end up backing a memory thread should
226*e98008ccSGreg Clayton     // still be in the main thread list, and they should be inserted at the beginning
227*e98008ccSGreg Clayton     // of the list
228*e98008ccSGreg Clayton     uint32_t insert_idx = 0;
229*e98008ccSGreg Clayton     for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx)
230*e98008ccSGreg Clayton     {
231*e98008ccSGreg Clayton         if (core_used_map[core_idx] == false)
232*e98008ccSGreg Clayton         {
233*e98008ccSGreg Clayton             new_thread_list.InsertThread (core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
234*e98008ccSGreg Clayton             ++insert_idx;
235*e98008ccSGreg Clayton         }
236*e98008ccSGreg Clayton     }
2376e0ff1a3SGreg Clayton 
238b3e77600SGreg Clayton     return new_thread_list.GetSize(false) > 0;
239b3e77600SGreg Clayton }
240b3e77600SGreg Clayton 
241a4d8747dSGreg Clayton ThreadSP
242b3ae8761SGreg Clayton OperatingSystemPython::CreateThreadFromThreadInfo (PythonDictionary &thread_dict,
243b3ae8761SGreg Clayton                                                    ThreadList &core_thread_list,
244b3ae8761SGreg Clayton                                                    ThreadList &old_thread_list,
245*e98008ccSGreg Clayton                                                    std::vector<bool> &core_used_map,
246b3ae8761SGreg Clayton                                                    bool *did_create_ptr)
247a4d8747dSGreg Clayton {
248a4d8747dSGreg Clayton     ThreadSP thread_sp;
249a4d8747dSGreg Clayton     if (thread_dict)
250a4d8747dSGreg Clayton     {
251a4d8747dSGreg Clayton         PythonString tid_pystr("tid");
252c7bece56SGreg Clayton         const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID);
253c7bece56SGreg Clayton         if (tid != LLDB_INVALID_THREAD_ID)
254c7bece56SGreg Clayton         {
255b3ae8761SGreg Clayton             PythonString core_pystr("core");
256a4d8747dSGreg Clayton             PythonString name_pystr("name");
257a4d8747dSGreg Clayton             PythonString queue_pystr("queue");
258160c9d81SGreg Clayton             //PythonString state_pystr("state");
259160c9d81SGreg Clayton             //PythonString stop_reason_pystr("stop_reason");
260a4d8747dSGreg Clayton             PythonString reg_data_addr_pystr ("register_data_addr");
261a4d8747dSGreg Clayton 
262b3ae8761SGreg Clayton             const uint32_t core_number = thread_dict.GetItemForKeyAsInteger (core_pystr, UINT32_MAX);
263a4d8747dSGreg Clayton             const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS);
264a4d8747dSGreg Clayton             const char *name = thread_dict.GetItemForKeyAsString (name_pystr);
265a4d8747dSGreg Clayton             const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr);
266a4d8747dSGreg Clayton             //const char *state = thread_dict.GetItemForKeyAsString (state_pystr);
267a4d8747dSGreg Clayton             //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr);
268a4d8747dSGreg Clayton 
269160c9d81SGreg Clayton             // See if a thread already exists for "tid"
270b3ae8761SGreg Clayton             thread_sp = old_thread_list.FindThreadByID (tid, false);
271160c9d81SGreg Clayton             if (thread_sp)
272160c9d81SGreg Clayton             {
273160c9d81SGreg Clayton                 // A thread already does exist for "tid", make sure it was an operating system
274160c9d81SGreg Clayton                 // plug-in generated thread.
275160c9d81SGreg Clayton                 if (!IsOperatingSystemPluginThread(thread_sp))
276160c9d81SGreg Clayton                 {
277160c9d81SGreg Clayton                     // We have thread ID overlap between the protocol threads and the
278160c9d81SGreg Clayton                     // operating system threads, clear the thread so we create an
279160c9d81SGreg Clayton                     // operating system thread for this.
280160c9d81SGreg Clayton                     thread_sp.reset();
281160c9d81SGreg Clayton                 }
282160c9d81SGreg Clayton             }
283160c9d81SGreg Clayton 
284a4d8747dSGreg Clayton             if (!thread_sp)
285a4d8747dSGreg Clayton             {
286a4d8747dSGreg Clayton                 if (did_create_ptr)
287a4d8747dSGreg Clayton                     *did_create_ptr = true;
288a4d8747dSGreg Clayton                 thread_sp.reset (new ThreadMemory (*m_process,
289a4d8747dSGreg Clayton                                                    tid,
290a4d8747dSGreg Clayton                                                    name,
291a4d8747dSGreg Clayton                                                    queue,
292a4d8747dSGreg Clayton                                                    reg_data_addr));
293b3ae8761SGreg Clayton 
294b3ae8761SGreg Clayton             }
295b3ae8761SGreg Clayton 
296b3ae8761SGreg Clayton             if (core_number < core_thread_list.GetSize(false))
297b3ae8761SGreg Clayton             {
298160c9d81SGreg Clayton                 ThreadSP core_thread_sp (core_thread_list.GetThreadAtIndex(core_number, false));
299160c9d81SGreg Clayton                 if (core_thread_sp)
300160c9d81SGreg Clayton                 {
301*e98008ccSGreg Clayton                     // Keep track of which cores were set as the backing thread for memory threads...
302*e98008ccSGreg Clayton                     if (core_number < core_used_map.size())
303*e98008ccSGreg Clayton                         core_used_map[core_number] = true;
304*e98008ccSGreg Clayton 
305160c9d81SGreg Clayton                     ThreadSP backing_core_thread_sp (core_thread_sp->GetBackingThread());
306160c9d81SGreg Clayton                     if (backing_core_thread_sp)
307160c9d81SGreg Clayton                     {
308160c9d81SGreg Clayton                         thread_sp->SetBackingThread(backing_core_thread_sp);
309160c9d81SGreg Clayton                     }
310160c9d81SGreg Clayton                     else
311160c9d81SGreg Clayton                     {
312160c9d81SGreg Clayton                         thread_sp->SetBackingThread(core_thread_sp);
313160c9d81SGreg Clayton                     }
314160c9d81SGreg Clayton                 }
315a4d8747dSGreg Clayton             }
316a4d8747dSGreg Clayton         }
317c7bece56SGreg Clayton     }
318a4d8747dSGreg Clayton     return thread_sp;
319a4d8747dSGreg Clayton }
320a4d8747dSGreg Clayton 
321a4d8747dSGreg Clayton 
322a4d8747dSGreg Clayton 
323b3e77600SGreg Clayton void
324b3e77600SGreg Clayton OperatingSystemPython::ThreadWasSelected (Thread *thread)
325b3e77600SGreg Clayton {
326b3e77600SGreg Clayton }
327b3e77600SGreg Clayton 
328b3e77600SGreg Clayton RegisterContextSP
329a4d8747dSGreg Clayton OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, addr_t reg_data_addr)
330b3e77600SGreg Clayton {
331435ce139SGreg Clayton     RegisterContextSP reg_ctx_sp;
332a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp || !thread)
333160c9d81SGreg Clayton         return reg_ctx_sp;
334160c9d81SGreg Clayton 
335160c9d81SGreg Clayton     if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
336160c9d81SGreg Clayton         return reg_ctx_sp;
3371c22be69SGreg Clayton 
33885e276b8SJim Ingham     // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
33985e276b8SJim Ingham     // content of the process, and we're going to use python, which requires the API lock to do it.
34085e276b8SJim Ingham     // So get & hold that.  This is a recursive lock so we can grant it to any Python code called on the stack below us.
34185e276b8SJim Ingham     Target &target = m_process->GetTarget();
34285e276b8SJim Ingham     Mutex::Locker api_locker (target.GetAPIMutex());
34385e276b8SJim Ingham 
3445160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
3451c22be69SGreg Clayton 
346198125a8SEnrico Granata     auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure python objects stays alive
347ead45e01SGreg Clayton     if (reg_data_addr != LLDB_INVALID_ADDRESS)
348ead45e01SGreg Clayton     {
349ead45e01SGreg Clayton         // The registers data is in contiguous memory, just create the register
350ead45e01SGreg Clayton         // context using the address provided
351ead45e01SGreg Clayton         if (log)
352160c9d81SGreg Clayton             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context",
353160c9d81SGreg Clayton                          thread->GetID(),
354160c9d81SGreg Clayton                          thread->GetProtocolID(),
355160c9d81SGreg Clayton                          reg_data_addr);
356ead45e01SGreg Clayton         reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr));
357ead45e01SGreg Clayton     }
358ead45e01SGreg Clayton     else
359ead45e01SGreg Clayton     {
360ead45e01SGreg Clayton         // No register data address is provided, query the python plug-in to let
361ead45e01SGreg Clayton         // it make up the data as it sees fit
3621c22be69SGreg Clayton         if (log)
363160c9d81SGreg Clayton             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ") fetching register data from python",
364160c9d81SGreg Clayton                          thread->GetID(),
365160c9d81SGreg Clayton                          thread->GetProtocolID());
3661c22be69SGreg Clayton 
367a4d8747dSGreg Clayton         PythonString reg_context_data(m_interpreter->OSPlugin_RegisterContextData (m_python_object_sp, thread->GetID()));
368435ce139SGreg Clayton         if (reg_context_data)
369435ce139SGreg Clayton         {
370435ce139SGreg Clayton             DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
371435ce139SGreg Clayton                                                       reg_context_data.GetSize()));
372435ce139SGreg Clayton             if (data_sp->GetByteSize())
373435ce139SGreg Clayton             {
374435ce139SGreg Clayton                 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
375435ce139SGreg Clayton                 if (reg_ctx_memory)
376435ce139SGreg Clayton                 {
377435ce139SGreg Clayton                     reg_ctx_sp.reset(reg_ctx_memory);
378435ce139SGreg Clayton                     reg_ctx_memory->SetAllRegisterData (data_sp);
379435ce139SGreg Clayton                 }
380435ce139SGreg Clayton             }
381435ce139SGreg Clayton         }
382ead45e01SGreg Clayton     }
383cbd79b6cSEnrico Granata     // if we still have no register data, fallback on a dummy context to avoid crashing
384cbd79b6cSEnrico Granata     if (!reg_ctx_sp)
385cbd79b6cSEnrico Granata     {
386cbd79b6cSEnrico Granata         if (log)
387cbd79b6cSEnrico Granata             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") forcing a dummy register context", thread->GetID());
388cbd79b6cSEnrico Granata         reg_ctx_sp.reset(new RegisterContextDummy(*thread,0,target.GetArchitecture().GetAddressByteSize()));
389cbd79b6cSEnrico Granata     }
390b3e77600SGreg Clayton     return reg_ctx_sp;
391b3e77600SGreg Clayton }
392b3e77600SGreg Clayton 
393b3e77600SGreg Clayton StopInfoSP
394b3e77600SGreg Clayton OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
395b3e77600SGreg Clayton {
396b3e77600SGreg Clayton     // We should have gotten the thread stop info from the dictionary of data for
397b3e77600SGreg Clayton     // the thread in the initial call to get_thread_info(), this should have been
398b3e77600SGreg Clayton     // cached so we can return it here
399b3e77600SGreg Clayton     StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
400b3e77600SGreg Clayton     return stop_info_sp;
401b3e77600SGreg Clayton }
402b3e77600SGreg Clayton 
403a4d8747dSGreg Clayton lldb::ThreadSP
404a4d8747dSGreg Clayton OperatingSystemPython::CreateThread (lldb::tid_t tid, addr_t context)
405a4d8747dSGreg Clayton {
4065160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
407a4d8747dSGreg Clayton 
408a4d8747dSGreg Clayton     if (log)
409a4d8747dSGreg Clayton         log->Printf ("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 ", context = 0x%" PRIx64 ") fetching register data from python", tid, context);
410a4d8747dSGreg Clayton 
411a4d8747dSGreg Clayton     if (m_interpreter && m_python_object_sp)
412a4d8747dSGreg Clayton     {
413a4d8747dSGreg Clayton         // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
414a4d8747dSGreg Clayton         // content of the process, and we're going to use python, which requires the API lock to do it.
415a4d8747dSGreg Clayton         // So get & hold that.  This is a recursive lock so we can grant it to any Python code called on the stack below us.
416a4d8747dSGreg Clayton         Target &target = m_process->GetTarget();
417a4d8747dSGreg Clayton         Mutex::Locker api_locker (target.GetAPIMutex());
418a4d8747dSGreg Clayton 
419198125a8SEnrico Granata         auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure thread_info_dict stays alive
420a4d8747dSGreg Clayton         PythonDictionary thread_info_dict (m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context));
421*e98008ccSGreg Clayton         std::vector<bool> core_used_map;
422a4d8747dSGreg Clayton         if (thread_info_dict)
423a4d8747dSGreg Clayton         {
424b3ae8761SGreg Clayton             ThreadList core_threads(m_process);
425a4d8747dSGreg Clayton             ThreadList &thread_list = m_process->GetThreadList();
426a4d8747dSGreg Clayton             bool did_create = false;
427*e98008ccSGreg Clayton             ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_info_dict, core_threads, thread_list, core_used_map, &did_create));
428a4d8747dSGreg Clayton             if (did_create)
429a4d8747dSGreg Clayton                 thread_list.AddThread(thread_sp);
430a4d8747dSGreg Clayton             return thread_sp;
431a4d8747dSGreg Clayton         }
432a4d8747dSGreg Clayton     }
433a4d8747dSGreg Clayton     return ThreadSP();
434a4d8747dSGreg Clayton }
435a4d8747dSGreg Clayton 
436a4d8747dSGreg Clayton 
437b3e77600SGreg Clayton 
438b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON
439