1ac7ddfbfSEd Maste //===-- OperatingSystemPython.cpp --------------------------------*- C++-*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
10ac7ddfbfSEd Maste #ifndef LLDB_DISABLE_PYTHON
11ac7ddfbfSEd Maste
12ac7ddfbfSEd Maste #include "OperatingSystemPython.h"
13435933ddSDimitry Andric #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
14435933ddSDimitry Andric #include "Plugins/Process/Utility/RegisterContextDummy.h"
15435933ddSDimitry Andric #include "Plugins/Process/Utility/RegisterContextMemory.h"
16435933ddSDimitry Andric #include "Plugins/Process/Utility/ThreadMemory.h"
17ac7ddfbfSEd Maste #include "lldb/Core/Debugger.h"
18ac7ddfbfSEd Maste #include "lldb/Core/Module.h"
19ac7ddfbfSEd Maste #include "lldb/Core/PluginManager.h"
20ac7ddfbfSEd Maste #include "lldb/Core/ValueObjectVariable.h"
21ac7ddfbfSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
221c3bbb01SEd Maste #include "lldb/Interpreter/ScriptInterpreter.h"
23ac7ddfbfSEd Maste #include "lldb/Symbol/ObjectFile.h"
24ac7ddfbfSEd Maste #include "lldb/Symbol/VariableList.h"
25ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
26ac7ddfbfSEd Maste #include "lldb/Target/StopInfo.h"
27ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
28ac7ddfbfSEd Maste #include "lldb/Target/Thread.h"
29435933ddSDimitry Andric #include "lldb/Target/ThreadList.h"
30f678e45dSDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
31*b5893f02SDimitry Andric #include "lldb/Utility/RegisterValue.h"
32f678e45dSDimitry Andric #include "lldb/Utility/StreamString.h"
33a580b014SDimitry Andric #include "lldb/Utility/StructuredData.h"
34ac7ddfbfSEd Maste
35ac7ddfbfSEd Maste using namespace lldb;
36ac7ddfbfSEd Maste using namespace lldb_private;
37ac7ddfbfSEd Maste
Initialize()38435933ddSDimitry Andric void OperatingSystemPython::Initialize() {
39435933ddSDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(),
40435933ddSDimitry Andric GetPluginDescriptionStatic(), CreateInstance,
41435933ddSDimitry Andric nullptr);
42ac7ddfbfSEd Maste }
43ac7ddfbfSEd Maste
Terminate()44435933ddSDimitry Andric void OperatingSystemPython::Terminate() {
45ac7ddfbfSEd Maste PluginManager::UnregisterPlugin(CreateInstance);
46ac7ddfbfSEd Maste }
47ac7ddfbfSEd Maste
CreateInstance(Process * process,bool force)48435933ddSDimitry Andric OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
49435933ddSDimitry Andric bool force) {
504ba319b5SDimitry Andric // Python OperatingSystem plug-ins must be requested by name, so force must
514ba319b5SDimitry Andric // be true
52ac7ddfbfSEd Maste FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
53*b5893f02SDimitry Andric if (python_os_plugin_spec &&
54*b5893f02SDimitry Andric FileSystem::Instance().Exists(python_os_plugin_spec)) {
55435933ddSDimitry Andric std::unique_ptr<OperatingSystemPython> os_ap(
56435933ddSDimitry Andric new OperatingSystemPython(process, python_os_plugin_spec));
57ac7ddfbfSEd Maste if (os_ap.get() && os_ap->IsValid())
58ac7ddfbfSEd Maste return os_ap.release();
59ac7ddfbfSEd Maste }
60ac7ddfbfSEd Maste return NULL;
61ac7ddfbfSEd Maste }
62ac7ddfbfSEd Maste
GetPluginNameStatic()63435933ddSDimitry Andric ConstString OperatingSystemPython::GetPluginNameStatic() {
64ac7ddfbfSEd Maste static ConstString g_name("python");
65ac7ddfbfSEd Maste return g_name;
66ac7ddfbfSEd Maste }
67ac7ddfbfSEd Maste
GetPluginDescriptionStatic()68435933ddSDimitry Andric const char *OperatingSystemPython::GetPluginDescriptionStatic() {
69435933ddSDimitry Andric return "Operating system plug-in that gathers OS information from a python "
70435933ddSDimitry Andric "class that implements the necessary OperatingSystem functionality.";
71ac7ddfbfSEd Maste }
72ac7ddfbfSEd Maste
OperatingSystemPython(lldb_private::Process * process,const FileSpec & python_module_path)73435933ddSDimitry Andric OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
74435933ddSDimitry Andric const FileSpec &python_module_path)
75435933ddSDimitry Andric : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_ap(),
76435933ddSDimitry Andric m_interpreter(NULL), m_python_object_sp() {
77ac7ddfbfSEd Maste if (!process)
78ac7ddfbfSEd Maste return;
79ac7ddfbfSEd Maste TargetSP target_sp = process->CalculateTarget();
80ac7ddfbfSEd Maste if (!target_sp)
81ac7ddfbfSEd Maste return;
82435933ddSDimitry Andric m_interpreter =
83435933ddSDimitry Andric target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
84435933ddSDimitry Andric if (m_interpreter) {
85ac7ddfbfSEd Maste
86435933ddSDimitry Andric std::string os_plugin_class_name(
87435933ddSDimitry Andric python_module_path.GetFilename().AsCString(""));
88435933ddSDimitry Andric if (!os_plugin_class_name.empty()) {
89ac7ddfbfSEd Maste const bool init_session = false;
90ac7ddfbfSEd Maste const bool allow_reload = true;
91ac7ddfbfSEd Maste char python_module_path_cstr[PATH_MAX];
92435933ddSDimitry Andric python_module_path.GetPath(python_module_path_cstr,
93435933ddSDimitry Andric sizeof(python_module_path_cstr));
945517e702SDimitry Andric Status error;
95435933ddSDimitry Andric if (m_interpreter->LoadScriptingModule(
96435933ddSDimitry Andric python_module_path_cstr, allow_reload, init_session, error)) {
97ac7ddfbfSEd Maste // Strip the ".py" extension if there is one
98ac7ddfbfSEd Maste size_t py_extension_pos = os_plugin_class_name.rfind(".py");
99ac7ddfbfSEd Maste if (py_extension_pos != std::string::npos)
100ac7ddfbfSEd Maste os_plugin_class_name.erase(py_extension_pos);
101435933ddSDimitry Andric // Add ".OperatingSystemPlugIn" to the module name to get a string like
102435933ddSDimitry Andric // "modulename.OperatingSystemPlugIn"
103ac7ddfbfSEd Maste os_plugin_class_name += ".OperatingSystemPlugIn";
1041c3bbb01SEd Maste StructuredData::ObjectSP object_sp =
105435933ddSDimitry Andric m_interpreter->OSPlugin_CreatePluginObject(
106435933ddSDimitry Andric os_plugin_class_name.c_str(), process->CalculateProcess());
1071c3bbb01SEd Maste if (object_sp && object_sp->IsValid())
108ac7ddfbfSEd Maste m_python_object_sp = object_sp;
109ac7ddfbfSEd Maste }
110ac7ddfbfSEd Maste }
111ac7ddfbfSEd Maste }
112ac7ddfbfSEd Maste }
113ac7ddfbfSEd Maste
~OperatingSystemPython()114435933ddSDimitry Andric OperatingSystemPython::~OperatingSystemPython() {}
115ac7ddfbfSEd Maste
GetDynamicRegisterInfo()116435933ddSDimitry Andric DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
117435933ddSDimitry Andric if (m_register_info_ap.get() == NULL) {
118ac7ddfbfSEd Maste if (!m_interpreter || !m_python_object_sp)
119ac7ddfbfSEd Maste return NULL;
120ac7ddfbfSEd Maste Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
121ac7ddfbfSEd Maste
122ac7ddfbfSEd Maste if (log)
123435933ddSDimitry Andric log->Printf("OperatingSystemPython::GetDynamicRegisterInfo() fetching "
124435933ddSDimitry Andric "thread register definitions from python for pid %" PRIu64,
125435933ddSDimitry Andric m_process->GetID());
126ac7ddfbfSEd Maste
127435933ddSDimitry Andric StructuredData::DictionarySP dictionary =
128435933ddSDimitry Andric m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp);
129ac7ddfbfSEd Maste if (!dictionary)
130ac7ddfbfSEd Maste return NULL;
131ac7ddfbfSEd Maste
132435933ddSDimitry Andric m_register_info_ap.reset(new DynamicRegisterInfo(
133435933ddSDimitry Andric *dictionary, m_process->GetTarget().GetArchitecture()));
134ac7ddfbfSEd Maste assert(m_register_info_ap->GetNumRegisters() > 0);
135ac7ddfbfSEd Maste assert(m_register_info_ap->GetNumRegisterSets() > 0);
136ac7ddfbfSEd Maste }
137ac7ddfbfSEd Maste return m_register_info_ap.get();
138ac7ddfbfSEd Maste }
139ac7ddfbfSEd Maste
140ac7ddfbfSEd Maste //------------------------------------------------------------------
141ac7ddfbfSEd Maste // PluginInterface protocol
142ac7ddfbfSEd Maste //------------------------------------------------------------------
GetPluginName()143435933ddSDimitry Andric ConstString OperatingSystemPython::GetPluginName() {
144ac7ddfbfSEd Maste return GetPluginNameStatic();
145ac7ddfbfSEd Maste }
146ac7ddfbfSEd Maste
GetPluginVersion()147435933ddSDimitry Andric uint32_t OperatingSystemPython::GetPluginVersion() { return 1; }
148ac7ddfbfSEd Maste
UpdateThreadList(ThreadList & old_thread_list,ThreadList & core_thread_list,ThreadList & new_thread_list)149435933ddSDimitry Andric bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
150ac7ddfbfSEd Maste ThreadList &core_thread_list,
151435933ddSDimitry Andric ThreadList &new_thread_list) {
152ac7ddfbfSEd Maste if (!m_interpreter || !m_python_object_sp)
153ac7ddfbfSEd Maste return false;
154ac7ddfbfSEd Maste
155ac7ddfbfSEd Maste Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
156ac7ddfbfSEd Maste
1574ba319b5SDimitry Andric // First thing we have to do is to try to get the API lock, and the
1584ba319b5SDimitry Andric // interpreter lock. We're going to change the thread content of the process,
1594ba319b5SDimitry Andric // and we're going to use python, which requires the API lock to do it. We
1604ba319b5SDimitry Andric // need the interpreter lock to make sure thread_info_dict stays alive.
1611c3bbb01SEd Maste //
1621c3bbb01SEd Maste // If someone already has the API lock, that is ok, we just want to avoid
1631c3bbb01SEd Maste // external code from making new API calls while this call is happening.
1641c3bbb01SEd Maste //
1651c3bbb01SEd Maste // This is a recursive lock so we can grant it to any Python code called on
1661c3bbb01SEd Maste // the stack below us.
167ac7ddfbfSEd Maste Target &target = m_process->GetTarget();
1684ba319b5SDimitry Andric std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
169435933ddSDimitry Andric std::defer_lock);
1704ba319b5SDimitry Andric api_lock.try_lock();
1714ba319b5SDimitry Andric auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
172ac7ddfbfSEd Maste
173ac7ddfbfSEd Maste if (log)
174435933ddSDimitry Andric log->Printf("OperatingSystemPython::UpdateThreadList() fetching thread "
175435933ddSDimitry Andric "data from python for pid %" PRIu64,
176435933ddSDimitry Andric m_process->GetID());
177ac7ddfbfSEd Maste
178435933ddSDimitry Andric // The threads that are in "new_thread_list" upon entry are the threads from
1794ba319b5SDimitry Andric // the lldb_private::Process subclass, no memory threads will be in this
1804ba319b5SDimitry Andric // list.
181435933ddSDimitry Andric StructuredData::ArraySP threads_list =
182435933ddSDimitry Andric m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp);
18312b93ac6SEd Maste
18412b93ac6SEd Maste const uint32_t num_cores = core_thread_list.GetSize(false);
18512b93ac6SEd Maste
18612b93ac6SEd Maste // Make a map so we can keep track of which cores were used from the
1874ba319b5SDimitry Andric // core_thread list. Any real threads/cores that weren't used should later be
1884ba319b5SDimitry Andric // put back into the "new_thread_list".
18912b93ac6SEd Maste std::vector<bool> core_used_map(num_cores, false);
190435933ddSDimitry Andric if (threads_list) {
191435933ddSDimitry Andric if (log) {
192ac7ddfbfSEd Maste StreamString strm;
1931c3bbb01SEd Maste threads_list->Dump(strm);
194435933ddSDimitry Andric log->Printf("threads_list = %s", strm.GetData());
195ac7ddfbfSEd Maste }
1961c3bbb01SEd Maste
1971c3bbb01SEd Maste const uint32_t num_threads = threads_list->GetSize();
198435933ddSDimitry Andric for (uint32_t i = 0; i < num_threads; ++i) {
199435933ddSDimitry Andric StructuredData::ObjectSP thread_dict_obj =
200435933ddSDimitry Andric threads_list->GetItemAtIndex(i);
201435933ddSDimitry Andric if (auto thread_dict = thread_dict_obj->GetAsDictionary()) {
202435933ddSDimitry Andric ThreadSP thread_sp(
203435933ddSDimitry Andric CreateThreadFromThreadInfo(*thread_dict, core_thread_list,
204435933ddSDimitry Andric old_thread_list, core_used_map, NULL));
205ac7ddfbfSEd Maste if (thread_sp)
206ac7ddfbfSEd Maste new_thread_list.AddThread(thread_sp);
207ac7ddfbfSEd Maste }
208ac7ddfbfSEd Maste }
209ac7ddfbfSEd Maste }
210ac7ddfbfSEd Maste
21112b93ac6SEd Maste // Any real core threads that didn't end up backing a memory thread should
212435933ddSDimitry Andric // still be in the main thread list, and they should be inserted at the
2134ba319b5SDimitry Andric // beginning of the list
21412b93ac6SEd Maste uint32_t insert_idx = 0;
215435933ddSDimitry Andric for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
216*b5893f02SDimitry Andric if (!core_used_map[core_idx]) {
217435933ddSDimitry Andric new_thread_list.InsertThread(
218435933ddSDimitry Andric core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
21912b93ac6SEd Maste ++insert_idx;
22012b93ac6SEd Maste }
22112b93ac6SEd Maste }
222ac7ddfbfSEd Maste
223ac7ddfbfSEd Maste return new_thread_list.GetSize(false) > 0;
224ac7ddfbfSEd Maste }
225ac7ddfbfSEd Maste
CreateThreadFromThreadInfo(StructuredData::Dictionary & thread_dict,ThreadList & core_thread_list,ThreadList & old_thread_list,std::vector<bool> & core_used_map,bool * did_create_ptr)226435933ddSDimitry Andric ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
227435933ddSDimitry Andric StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
228435933ddSDimitry Andric ThreadList &old_thread_list, std::vector<bool> &core_used_map,
229435933ddSDimitry Andric bool *did_create_ptr) {
230ac7ddfbfSEd Maste ThreadSP thread_sp;
2311c3bbb01SEd Maste tid_t tid = LLDB_INVALID_THREAD_ID;
2321c3bbb01SEd Maste if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
2331c3bbb01SEd Maste return ThreadSP();
234ac7ddfbfSEd Maste
2351c3bbb01SEd Maste uint32_t core_number;
2361c3bbb01SEd Maste addr_t reg_data_addr;
2375517e702SDimitry Andric llvm::StringRef name;
2385517e702SDimitry Andric llvm::StringRef queue;
2391c3bbb01SEd Maste
2401c3bbb01SEd Maste thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
241435933ddSDimitry Andric thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
242435933ddSDimitry Andric LLDB_INVALID_ADDRESS);
2431c3bbb01SEd Maste thread_dict.GetValueForKeyAsString("name", name);
2441c3bbb01SEd Maste thread_dict.GetValueForKeyAsString("queue", queue);
245ac7ddfbfSEd Maste
246ac7ddfbfSEd Maste // See if a thread already exists for "tid"
247ac7ddfbfSEd Maste thread_sp = old_thread_list.FindThreadByID(tid, false);
248435933ddSDimitry Andric if (thread_sp) {
249435933ddSDimitry Andric // A thread already does exist for "tid", make sure it was an operating
250435933ddSDimitry Andric // system
251ac7ddfbfSEd Maste // plug-in generated thread.
252435933ddSDimitry Andric if (!IsOperatingSystemPluginThread(thread_sp)) {
253ac7ddfbfSEd Maste // We have thread ID overlap between the protocol threads and the
2544ba319b5SDimitry Andric // operating system threads, clear the thread so we create an operating
2554ba319b5SDimitry Andric // system thread for this.
256ac7ddfbfSEd Maste thread_sp.reset();
257ac7ddfbfSEd Maste }
258ac7ddfbfSEd Maste }
259ac7ddfbfSEd Maste
260435933ddSDimitry Andric if (!thread_sp) {
261ac7ddfbfSEd Maste if (did_create_ptr)
262ac7ddfbfSEd Maste *did_create_ptr = true;
2635517e702SDimitry Andric thread_sp.reset(
2645517e702SDimitry Andric new ThreadMemory(*m_process, tid, name, queue, reg_data_addr));
265ac7ddfbfSEd Maste }
266ac7ddfbfSEd Maste
267435933ddSDimitry Andric if (core_number < core_thread_list.GetSize(false)) {
268435933ddSDimitry Andric ThreadSP core_thread_sp(
269435933ddSDimitry Andric core_thread_list.GetThreadAtIndex(core_number, false));
270435933ddSDimitry Andric if (core_thread_sp) {
271435933ddSDimitry Andric // Keep track of which cores were set as the backing thread for memory
272435933ddSDimitry Andric // threads...
27312b93ac6SEd Maste if (core_number < core_used_map.size())
27412b93ac6SEd Maste core_used_map[core_number] = true;
27512b93ac6SEd Maste
276ac7ddfbfSEd Maste ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
277435933ddSDimitry Andric if (backing_core_thread_sp) {
278ac7ddfbfSEd Maste thread_sp->SetBackingThread(backing_core_thread_sp);
279435933ddSDimitry Andric } else {
280ac7ddfbfSEd Maste thread_sp->SetBackingThread(core_thread_sp);
281ac7ddfbfSEd Maste }
282ac7ddfbfSEd Maste }
283ac7ddfbfSEd Maste }
284ac7ddfbfSEd Maste return thread_sp;
285ac7ddfbfSEd Maste }
286ac7ddfbfSEd Maste
ThreadWasSelected(Thread * thread)287435933ddSDimitry Andric void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
288ac7ddfbfSEd Maste
289ac7ddfbfSEd Maste RegisterContextSP
CreateRegisterContextForThread(Thread * thread,addr_t reg_data_addr)290435933ddSDimitry Andric OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
291435933ddSDimitry Andric addr_t reg_data_addr) {
292ac7ddfbfSEd Maste RegisterContextSP reg_ctx_sp;
293ac7ddfbfSEd Maste if (!m_interpreter || !m_python_object_sp || !thread)
294ac7ddfbfSEd Maste return reg_ctx_sp;
295ac7ddfbfSEd Maste
296ac7ddfbfSEd Maste if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
297ac7ddfbfSEd Maste return reg_ctx_sp;
298ac7ddfbfSEd Maste
2994ba319b5SDimitry Andric // First thing we have to do is to try to get the API lock, and the
3004ba319b5SDimitry Andric // interpreter lock. We're going to change the thread content of the process,
3014ba319b5SDimitry Andric // and we're going to use python, which requires the API lock to do it. We
3024ba319b5SDimitry Andric // need the interpreter lock to make sure thread_info_dict stays alive.
3034ba319b5SDimitry Andric //
3044ba319b5SDimitry Andric // If someone already has the API lock, that is ok, we just want to avoid
3054ba319b5SDimitry Andric // external code from making new API calls while this call is happening.
3064ba319b5SDimitry Andric //
3074ba319b5SDimitry Andric // This is a recursive lock so we can grant it to any Python code called on
3084ba319b5SDimitry Andric // the stack below us.
309ac7ddfbfSEd Maste Target &target = m_process->GetTarget();
3104ba319b5SDimitry Andric std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
3114ba319b5SDimitry Andric std::defer_lock);
3124ba319b5SDimitry Andric api_lock.try_lock();
3134ba319b5SDimitry Andric auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
314ac7ddfbfSEd Maste
315ac7ddfbfSEd Maste Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
316ac7ddfbfSEd Maste
317435933ddSDimitry Andric if (reg_data_addr != LLDB_INVALID_ADDRESS) {
318ac7ddfbfSEd Maste // The registers data is in contiguous memory, just create the register
319ac7ddfbfSEd Maste // context using the address provided
320ac7ddfbfSEd Maste if (log)
321435933ddSDimitry Andric log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid "
322435933ddSDimitry Andric "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
323435933ddSDimitry Andric ") creating memory register context",
324435933ddSDimitry Andric thread->GetID(), thread->GetProtocolID(), reg_data_addr);
325435933ddSDimitry Andric reg_ctx_sp.reset(new RegisterContextMemory(
326435933ddSDimitry Andric *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr));
327435933ddSDimitry Andric } else {
3284ba319b5SDimitry Andric // No register data address is provided, query the python plug-in to let it
3294ba319b5SDimitry Andric // make up the data as it sees fit
330ac7ddfbfSEd Maste if (log)
331435933ddSDimitry Andric log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid "
332435933ddSDimitry Andric "= 0x%" PRIx64 ", 0x%" PRIx64
333435933ddSDimitry Andric ") fetching register data from python",
334435933ddSDimitry Andric thread->GetID(), thread->GetProtocolID());
335ac7ddfbfSEd Maste
336435933ddSDimitry Andric StructuredData::StringSP reg_context_data =
337435933ddSDimitry Andric m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp,
338435933ddSDimitry Andric thread->GetID());
339435933ddSDimitry Andric if (reg_context_data) {
3401c3bbb01SEd Maste std::string value = reg_context_data->GetValue();
3411c3bbb01SEd Maste DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
342435933ddSDimitry Andric if (data_sp->GetByteSize()) {
343435933ddSDimitry Andric RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
344435933ddSDimitry Andric *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
345435933ddSDimitry Andric if (reg_ctx_memory) {
346ac7ddfbfSEd Maste reg_ctx_sp.reset(reg_ctx_memory);
347ac7ddfbfSEd Maste reg_ctx_memory->SetAllRegisterData(data_sp);
348ac7ddfbfSEd Maste }
349ac7ddfbfSEd Maste }
350ac7ddfbfSEd Maste }
351ac7ddfbfSEd Maste }
352435933ddSDimitry Andric // if we still have no register data, fallback on a dummy context to avoid
353435933ddSDimitry Andric // crashing
354435933ddSDimitry Andric if (!reg_ctx_sp) {
355ac7ddfbfSEd Maste if (log)
356435933ddSDimitry Andric log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid "
357435933ddSDimitry Andric "= 0x%" PRIx64 ") forcing a dummy register context",
358435933ddSDimitry Andric thread->GetID());
359435933ddSDimitry Andric reg_ctx_sp.reset(new RegisterContextDummy(
360435933ddSDimitry Andric *thread, 0, target.GetArchitecture().GetAddressByteSize()));
361ac7ddfbfSEd Maste }
362ac7ddfbfSEd Maste return reg_ctx_sp;
363ac7ddfbfSEd Maste }
364ac7ddfbfSEd Maste
365ac7ddfbfSEd Maste StopInfoSP
CreateThreadStopReason(lldb_private::Thread * thread)366435933ddSDimitry Andric OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
367ac7ddfbfSEd Maste // We should have gotten the thread stop info from the dictionary of data for
368ac7ddfbfSEd Maste // the thread in the initial call to get_thread_info(), this should have been
369ac7ddfbfSEd Maste // cached so we can return it here
370435933ddSDimitry Andric StopInfoSP
371435933ddSDimitry Andric stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
372ac7ddfbfSEd Maste return stop_info_sp;
373ac7ddfbfSEd Maste }
374ac7ddfbfSEd Maste
CreateThread(lldb::tid_t tid,addr_t context)375435933ddSDimitry Andric lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
376435933ddSDimitry Andric addr_t context) {
377ac7ddfbfSEd Maste Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
378ac7ddfbfSEd Maste
379ac7ddfbfSEd Maste if (log)
380435933ddSDimitry Andric log->Printf("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
381435933ddSDimitry Andric ", context = 0x%" PRIx64 ") fetching register data from python",
382435933ddSDimitry Andric tid, context);
383ac7ddfbfSEd Maste
384435933ddSDimitry Andric if (m_interpreter && m_python_object_sp) {
3854ba319b5SDimitry Andric // First thing we have to do is to try to get the API lock, and the
3864ba319b5SDimitry Andric // interpreter lock. We're going to change the thread content of the
3874ba319b5SDimitry Andric // process, and we're going to use python, which requires the API lock to
3884ba319b5SDimitry Andric // do it. We need the interpreter lock to make sure thread_info_dict stays
3894ba319b5SDimitry Andric // alive.
3904ba319b5SDimitry Andric //
3914ba319b5SDimitry Andric // If someone already has the API lock, that is ok, we just want to avoid
3924ba319b5SDimitry Andric // external code from making new API calls while this call is happening.
3934ba319b5SDimitry Andric //
3944ba319b5SDimitry Andric // This is a recursive lock so we can grant it to any Python code called on
3954ba319b5SDimitry Andric // the stack below us.
396ac7ddfbfSEd Maste Target &target = m_process->GetTarget();
3974ba319b5SDimitry Andric std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
3984ba319b5SDimitry Andric std::defer_lock);
3994ba319b5SDimitry Andric api_lock.try_lock();
4004ba319b5SDimitry Andric auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
401ac7ddfbfSEd Maste
402435933ddSDimitry Andric StructuredData::DictionarySP thread_info_dict =
403435933ddSDimitry Andric m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
40412b93ac6SEd Maste std::vector<bool> core_used_map;
405435933ddSDimitry Andric if (thread_info_dict) {
406ac7ddfbfSEd Maste ThreadList core_threads(m_process);
407ac7ddfbfSEd Maste ThreadList &thread_list = m_process->GetThreadList();
408ac7ddfbfSEd Maste bool did_create = false;
409435933ddSDimitry Andric ThreadSP thread_sp(
410435933ddSDimitry Andric CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
411435933ddSDimitry Andric thread_list, core_used_map, &did_create));
412ac7ddfbfSEd Maste if (did_create)
413ac7ddfbfSEd Maste thread_list.AddThread(thread_sp);
414ac7ddfbfSEd Maste return thread_sp;
415ac7ddfbfSEd Maste }
416ac7ddfbfSEd Maste }
417ac7ddfbfSEd Maste return ThreadSP();
418ac7ddfbfSEd Maste }
419ac7ddfbfSEd Maste
420ac7ddfbfSEd Maste #endif // #ifndef LLDB_DISABLE_PYTHON
421