180814287SRaphael Isemann //===-- OperatingSystemPython.cpp -----------------------------------------===//
2b3e77600SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b3e77600SGreg Clayton //
7b3e77600SGreg Clayton //===----------------------------------------------------------------------===//
893a64300SDaniel Malea
959998b7bSJonas Devlieghere #include "lldb/Host/Config.h"
1059998b7bSJonas Devlieghere
114e26cf2cSJonas Devlieghere #if LLDB_ENABLE_PYTHON
12b3e77600SGreg Clayton
13b3e77600SGreg Clayton #include "OperatingSystemPython.h"
14796ac80bSJonas Devlieghere
15b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextDummy.h"
16b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextMemory.h"
17b9c1b51eSKate Stone #include "Plugins/Process/Utility/ThreadMemory.h"
185790759aSEnrico Granata #include "lldb/Core/Debugger.h"
19b3e77600SGreg Clayton #include "lldb/Core/Module.h"
20b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h"
21b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h"
225790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
230641ca1aSZachary Turner #include "lldb/Interpreter/ScriptInterpreter.h"
24b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
25b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h"
26b3e77600SGreg Clayton #include "lldb/Target/Process.h"
27b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h"
28b3e77600SGreg Clayton #include "lldb/Target/Target.h"
29b3e77600SGreg Clayton #include "lldb/Target/Thread.h"
30b9c1b51eSKate Stone #include "lldb/Target/ThreadList.h"
31666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
32*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
33d821c997SPavel Labath #include "lldb/Utility/RegisterValue.h"
34bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
35f2a8bccfSPavel Labath #include "lldb/Utility/StructuredData.h"
36b3e77600SGreg Clayton
37796ac80bSJonas Devlieghere #include <memory>
38796ac80bSJonas Devlieghere
39b3e77600SGreg Clayton using namespace lldb;
40b3e77600SGreg Clayton using namespace lldb_private;
41b3e77600SGreg Clayton
LLDB_PLUGIN_DEFINE(OperatingSystemPython)42bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(OperatingSystemPython)
43fbb4d1e4SJonas Devlieghere
44b9c1b51eSKate Stone void OperatingSystemPython::Initialize() {
45b9c1b51eSKate Stone PluginManager::RegisterPlugin(GetPluginNameStatic(),
46b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance,
47b9c1b51eSKate Stone nullptr);
48b3e77600SGreg Clayton }
49b3e77600SGreg Clayton
Terminate()50b9c1b51eSKate Stone void OperatingSystemPython::Terminate() {
51b3e77600SGreg Clayton PluginManager::UnregisterPlugin(CreateInstance);
52b3e77600SGreg Clayton }
53b3e77600SGreg Clayton
CreateInstance(Process * process,bool force)54b9c1b51eSKate Stone OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
55b9c1b51eSKate Stone bool force) {
5605097246SAdrian Prantl // Python OperatingSystem plug-ins must be requested by name, so force must
5705097246SAdrian Prantl // be true
58c9d645d3SGreg Clayton FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
59dbd7fabaSJonas Devlieghere if (python_os_plugin_spec &&
60dbd7fabaSJonas Devlieghere FileSystem::Instance().Exists(python_os_plugin_spec)) {
61d5b44036SJonas Devlieghere std::unique_ptr<OperatingSystemPython> os_up(
62b9c1b51eSKate Stone new OperatingSystemPython(process, python_os_plugin_spec));
63d5b44036SJonas Devlieghere if (os_up.get() && os_up->IsValid())
64d5b44036SJonas Devlieghere return os_up.release();
65c9d645d3SGreg Clayton }
66248a1305SKonrad Kleine return nullptr;
67b3e77600SGreg Clayton }
68b3e77600SGreg Clayton
GetPluginDescriptionStatic()692ace1e57SPavel Labath llvm::StringRef OperatingSystemPython::GetPluginDescriptionStatic() {
70b9c1b51eSKate Stone return "Operating system plug-in that gathers OS information from a python "
71b9c1b51eSKate Stone "class that implements the necessary OperatingSystem functionality.";
72b3e77600SGreg Clayton }
73b3e77600SGreg Clayton
OperatingSystemPython(lldb_private::Process * process,const FileSpec & python_module_path)74b9c1b51eSKate Stone OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
75b9c1b51eSKate Stone const FileSpec &python_module_path)
76d5b44036SJonas Devlieghere : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(),
77248a1305SKonrad Kleine m_interpreter(nullptr), m_python_object_sp() {
785790759aSEnrico Granata if (!process)
795790759aSEnrico Granata return;
80a4d8747dSGreg Clayton TargetSP target_sp = process->CalculateTarget();
815790759aSEnrico Granata if (!target_sp)
825790759aSEnrico Granata return;
832b29b432SJonas Devlieghere m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
84b9c1b51eSKate Stone if (m_interpreter) {
852443cbd7SGreg Clayton
86b9c1b51eSKate Stone std::string os_plugin_class_name(
87b9c1b51eSKate Stone python_module_path.GetFilename().AsCString(""));
88b9c1b51eSKate Stone if (!os_plugin_class_name.empty()) {
89f9517353SJonas Devlieghere LoadScriptOptions options;
90c9d645d3SGreg Clayton char python_module_path_cstr[PATH_MAX];
91b9c1b51eSKate Stone python_module_path.GetPath(python_module_path_cstr,
92b9c1b51eSKate Stone sizeof(python_module_path_cstr));
9397206d57SZachary Turner Status error;
94f9517353SJonas Devlieghere if (m_interpreter->LoadScriptingModule(python_module_path_cstr, options,
95f9517353SJonas Devlieghere error)) {
96c9d645d3SGreg Clayton // Strip the ".py" extension if there is one
97c9d645d3SGreg Clayton size_t py_extension_pos = os_plugin_class_name.rfind(".py");
98c9d645d3SGreg Clayton if (py_extension_pos != std::string::npos)
99c9d645d3SGreg Clayton os_plugin_class_name.erase(py_extension_pos);
100b9c1b51eSKate Stone // Add ".OperatingSystemPlugIn" to the module name to get a string like
101b9c1b51eSKate Stone // "modulename.OperatingSystemPlugIn"
102c9d645d3SGreg Clayton os_plugin_class_name += ".OperatingSystemPlugIn";
1030641ca1aSZachary Turner StructuredData::ObjectSP object_sp =
104b9c1b51eSKate Stone m_interpreter->OSPlugin_CreatePluginObject(
105b9c1b51eSKate Stone os_plugin_class_name.c_str(), process->CalculateProcess());
1060641ca1aSZachary Turner if (object_sp && object_sp->IsValid())
107a4d8747dSGreg Clayton m_python_object_sp = object_sp;
108c9d645d3SGreg Clayton }
1092443cbd7SGreg Clayton }
1105790759aSEnrico Granata }
111b3e77600SGreg Clayton }
112b3e77600SGreg Clayton
113fd2433e1SJonas Devlieghere OperatingSystemPython::~OperatingSystemPython() = default;
114b3e77600SGreg Clayton
GetDynamicRegisterInfo()115b9c1b51eSKate Stone DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
116248a1305SKonrad Kleine if (m_register_info_up == nullptr) {
117a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp)
118248a1305SKonrad Kleine return nullptr;
119a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::OS);
1201c22be69SGreg Clayton
12163e5fb76SJonas Devlieghere LLDB_LOGF(log,
12263e5fb76SJonas Devlieghere "OperatingSystemPython::GetDynamicRegisterInfo() fetching "
123b9c1b51eSKate Stone "thread register definitions from python for pid %" PRIu64,
124b9c1b51eSKate Stone m_process->GetID());
1251c22be69SGreg Clayton
126b9c1b51eSKate Stone StructuredData::DictionarySP dictionary =
127b9c1b51eSKate Stone m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp);
1285790759aSEnrico Granata if (!dictionary)
129248a1305SKonrad Kleine return nullptr;
130b3e77600SGreg Clayton
131d2ec9184SJonas Devlieghere m_register_info_up = std::make_unique<DynamicRegisterInfo>(
132d2ec9184SJonas Devlieghere *dictionary, m_process->GetTarget().GetArchitecture());
133d5b44036SJonas Devlieghere assert(m_register_info_up->GetNumRegisters() > 0);
134d5b44036SJonas Devlieghere assert(m_register_info_up->GetNumRegisterSets() > 0);
135b3e77600SGreg Clayton }
136d5b44036SJonas Devlieghere return m_register_info_up.get();
137b3e77600SGreg Clayton }
138b3e77600SGreg Clayton
UpdateThreadList(ThreadList & old_thread_list,ThreadList & core_thread_list,ThreadList & new_thread_list)139b9c1b51eSKate Stone bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
140ba4e61d3SAndrew Kaylor ThreadList &core_thread_list,
141b9c1b51eSKate Stone ThreadList &new_thread_list) {
142a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp)
143a85e6b6cSDaniel Malea return false;
1441c22be69SGreg Clayton
145a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::OS);
1461c22be69SGreg Clayton
14785a5ec51SJonas Devlieghere // First thing we have to do is to try to get the API lock, and the
14885a5ec51SJonas Devlieghere // interpreter lock. We're going to change the thread content of the process,
14985a5ec51SJonas Devlieghere // and we're going to use python, which requires the API lock to do it. We
15085a5ec51SJonas Devlieghere // need the interpreter lock to make sure thread_info_dict stays alive.
151ab745c2aSGreg Clayton //
152ab745c2aSGreg Clayton // If someone already has the API lock, that is ok, we just want to avoid
153ab745c2aSGreg Clayton // external code from making new API calls while this call is happening.
154ab745c2aSGreg Clayton //
155ab745c2aSGreg Clayton // This is a recursive lock so we can grant it to any Python code called on
156ab745c2aSGreg Clayton // the stack below us.
15785e276b8SJim Ingham Target &target = m_process->GetTarget();
15885a5ec51SJonas Devlieghere std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
159b9c1b51eSKate Stone std::defer_lock);
1604d23764dSReid Kleckner (void)api_lock.try_lock(); // See above.
16185a5ec51SJonas Devlieghere auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
16285e276b8SJim Ingham
16363e5fb76SJonas Devlieghere LLDB_LOGF(log,
16463e5fb76SJonas Devlieghere "OperatingSystemPython::UpdateThreadList() fetching thread "
165b9c1b51eSKate Stone "data from python for pid %" PRIu64,
166b9c1b51eSKate Stone m_process->GetID());
1671c22be69SGreg Clayton
1688240b0d7SJim Ingham // The threads that are in "core_thread_list" upon entry are the threads from
16985a5ec51SJonas Devlieghere // the lldb_private::Process subclass, no memory threads will be in this
17085a5ec51SJonas Devlieghere // list.
171b9c1b51eSKate Stone StructuredData::ArraySP threads_list =
172b9c1b51eSKate Stone m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp);
173e98008ccSGreg Clayton
174e98008ccSGreg Clayton const uint32_t num_cores = core_thread_list.GetSize(false);
175e98008ccSGreg Clayton
176e98008ccSGreg Clayton // Make a map so we can keep track of which cores were used from the
17705097246SAdrian Prantl // core_thread list. Any real threads/cores that weren't used should later be
17805097246SAdrian Prantl // put back into the "new_thread_list".
179e98008ccSGreg Clayton std::vector<bool> core_used_map(num_cores, false);
180b9c1b51eSKate Stone if (threads_list) {
181b9c1b51eSKate Stone if (log) {
18262f80036SGreg Clayton StreamString strm;
1830641ca1aSZachary Turner threads_list->Dump(strm);
18463e5fb76SJonas Devlieghere LLDB_LOGF(log, "threads_list = %s", strm.GetData());
18562f80036SGreg Clayton }
1860641ca1aSZachary Turner
1870641ca1aSZachary Turner const uint32_t num_threads = threads_list->GetSize();
188b9c1b51eSKate Stone for (uint32_t i = 0; i < num_threads; ++i) {
189b9c1b51eSKate Stone StructuredData::ObjectSP thread_dict_obj =
190b9c1b51eSKate Stone threads_list->GetItemAtIndex(i);
191b9c1b51eSKate Stone if (auto thread_dict = thread_dict_obj->GetAsDictionary()) {
192248a1305SKonrad Kleine ThreadSP thread_sp(CreateThreadFromThreadInfo(
193248a1305SKonrad Kleine *thread_dict, core_thread_list, old_thread_list, core_used_map,
194248a1305SKonrad Kleine nullptr));
195a4d8747dSGreg Clayton if (thread_sp)
196435ce139SGreg Clayton new_thread_list.AddThread(thread_sp);
197435ce139SGreg Clayton }
198435ce139SGreg Clayton }
199435ce139SGreg Clayton }
2006e0ff1a3SGreg Clayton
201e98008ccSGreg Clayton // Any real core threads that didn't end up backing a memory thread should
202b9c1b51eSKate Stone // still be in the main thread list, and they should be inserted at the
20305097246SAdrian Prantl // beginning of the list
204e98008ccSGreg Clayton uint32_t insert_idx = 0;
205b9c1b51eSKate Stone for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
206a6682a41SJonas Devlieghere if (!core_used_map[core_idx]) {
207b9c1b51eSKate Stone new_thread_list.InsertThread(
208b9c1b51eSKate Stone core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
209e98008ccSGreg Clayton ++insert_idx;
210e98008ccSGreg Clayton }
211e98008ccSGreg Clayton }
2126e0ff1a3SGreg Clayton
213b3e77600SGreg Clayton return new_thread_list.GetSize(false) > 0;
214b3e77600SGreg Clayton }
215b3e77600SGreg Clayton
CreateThreadFromThreadInfo(StructuredData::Dictionary & thread_dict,ThreadList & core_thread_list,ThreadList & old_thread_list,std::vector<bool> & core_used_map,bool * did_create_ptr)216b9c1b51eSKate Stone ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
217b9c1b51eSKate Stone StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
218b9c1b51eSKate Stone ThreadList &old_thread_list, std::vector<bool> &core_used_map,
219b9c1b51eSKate Stone bool *did_create_ptr) {
220a4d8747dSGreg Clayton ThreadSP thread_sp;
2210641ca1aSZachary Turner tid_t tid = LLDB_INVALID_THREAD_ID;
2220641ca1aSZachary Turner if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
2230641ca1aSZachary Turner return ThreadSP();
224a4d8747dSGreg Clayton
2250641ca1aSZachary Turner uint32_t core_number;
2260641ca1aSZachary Turner addr_t reg_data_addr;
2272833321fSZachary Turner llvm::StringRef name;
2282833321fSZachary Turner llvm::StringRef queue;
2290641ca1aSZachary Turner
2300641ca1aSZachary Turner thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
231b9c1b51eSKate Stone thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
232b9c1b51eSKate Stone LLDB_INVALID_ADDRESS);
2330641ca1aSZachary Turner thread_dict.GetValueForKeyAsString("name", name);
2340641ca1aSZachary Turner thread_dict.GetValueForKeyAsString("queue", queue);
235a4d8747dSGreg Clayton
236160c9d81SGreg Clayton // See if a thread already exists for "tid"
237b3ae8761SGreg Clayton thread_sp = old_thread_list.FindThreadByID(tid, false);
238b9c1b51eSKate Stone if (thread_sp) {
239b9c1b51eSKate Stone // A thread already does exist for "tid", make sure it was an operating
240b9c1b51eSKate Stone // system
241160c9d81SGreg Clayton // plug-in generated thread.
242b9c1b51eSKate Stone if (!IsOperatingSystemPluginThread(thread_sp)) {
243160c9d81SGreg Clayton // We have thread ID overlap between the protocol threads and the
24405097246SAdrian Prantl // operating system threads, clear the thread so we create an operating
24505097246SAdrian Prantl // system thread for this.
246160c9d81SGreg Clayton thread_sp.reset();
247160c9d81SGreg Clayton }
248160c9d81SGreg Clayton }
249160c9d81SGreg Clayton
250b9c1b51eSKate Stone if (!thread_sp) {
251a4d8747dSGreg Clayton if (did_create_ptr)
252a4d8747dSGreg Clayton *did_create_ptr = true;
253796ac80bSJonas Devlieghere thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
254796ac80bSJonas Devlieghere reg_data_addr);
255b3ae8761SGreg Clayton }
256b3ae8761SGreg Clayton
257b9c1b51eSKate Stone if (core_number < core_thread_list.GetSize(false)) {
258b9c1b51eSKate Stone ThreadSP core_thread_sp(
259b9c1b51eSKate Stone core_thread_list.GetThreadAtIndex(core_number, false));
260b9c1b51eSKate Stone if (core_thread_sp) {
261b9c1b51eSKate Stone // Keep track of which cores were set as the backing thread for memory
262b9c1b51eSKate Stone // threads...
263e98008ccSGreg Clayton if (core_number < core_used_map.size())
264e98008ccSGreg Clayton core_used_map[core_number] = true;
265e98008ccSGreg Clayton
266160c9d81SGreg Clayton ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
267b9c1b51eSKate Stone if (backing_core_thread_sp) {
268160c9d81SGreg Clayton thread_sp->SetBackingThread(backing_core_thread_sp);
269b9c1b51eSKate Stone } else {
270160c9d81SGreg Clayton thread_sp->SetBackingThread(core_thread_sp);
271160c9d81SGreg Clayton }
272160c9d81SGreg Clayton }
273a4d8747dSGreg Clayton }
274a4d8747dSGreg Clayton return thread_sp;
275a4d8747dSGreg Clayton }
276a4d8747dSGreg Clayton
ThreadWasSelected(Thread * thread)277b9c1b51eSKate Stone void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
278b3e77600SGreg Clayton
279b3e77600SGreg Clayton RegisterContextSP
CreateRegisterContextForThread(Thread * thread,addr_t reg_data_addr)280b9c1b51eSKate Stone OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
281b9c1b51eSKate Stone addr_t reg_data_addr) {
282435ce139SGreg Clayton RegisterContextSP reg_ctx_sp;
283a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp || !thread)
284160c9d81SGreg Clayton return reg_ctx_sp;
285160c9d81SGreg Clayton
286160c9d81SGreg Clayton if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
287160c9d81SGreg Clayton return reg_ctx_sp;
2881c22be69SGreg Clayton
28985a5ec51SJonas Devlieghere // First thing we have to do is to try to get the API lock, and the
29085a5ec51SJonas Devlieghere // interpreter lock. We're going to change the thread content of the process,
29185a5ec51SJonas Devlieghere // and we're going to use python, which requires the API lock to do it. We
29285a5ec51SJonas Devlieghere // need the interpreter lock to make sure thread_info_dict stays alive.
29385a5ec51SJonas Devlieghere //
29485a5ec51SJonas Devlieghere // If someone already has the API lock, that is ok, we just want to avoid
29585a5ec51SJonas Devlieghere // external code from making new API calls while this call is happening.
29685a5ec51SJonas Devlieghere //
29785a5ec51SJonas Devlieghere // This is a recursive lock so we can grant it to any Python code called on
29885a5ec51SJonas Devlieghere // the stack below us.
29985e276b8SJim Ingham Target &target = m_process->GetTarget();
30085a5ec51SJonas Devlieghere std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
30185a5ec51SJonas Devlieghere std::defer_lock);
3024d23764dSReid Kleckner (void)api_lock.try_lock(); // See above.
30385a5ec51SJonas Devlieghere auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
30485e276b8SJim Ingham
305a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Thread);
3061c22be69SGreg Clayton
307b9c1b51eSKate Stone if (reg_data_addr != LLDB_INVALID_ADDRESS) {
308ead45e01SGreg Clayton // The registers data is in contiguous memory, just create the register
309ead45e01SGreg Clayton // context using the address provided
31063e5fb76SJonas Devlieghere LLDB_LOGF(log,
31163e5fb76SJonas Devlieghere "OperatingSystemPython::CreateRegisterContextForThread (tid "
312b9c1b51eSKate Stone "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
313b9c1b51eSKate Stone ") creating memory register context",
314b9c1b51eSKate Stone thread->GetID(), thread->GetProtocolID(), reg_data_addr);
315796ac80bSJonas Devlieghere reg_ctx_sp = std::make_shared<RegisterContextMemory>(
316796ac80bSJonas Devlieghere *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr);
317b9c1b51eSKate Stone } else {
31805097246SAdrian Prantl // No register data address is provided, query the python plug-in to let it
31905097246SAdrian Prantl // make up the data as it sees fit
32063e5fb76SJonas Devlieghere LLDB_LOGF(log,
32163e5fb76SJonas Devlieghere "OperatingSystemPython::CreateRegisterContextForThread (tid "
322b9c1b51eSKate Stone "= 0x%" PRIx64 ", 0x%" PRIx64
323b9c1b51eSKate Stone ") fetching register data from python",
324b9c1b51eSKate Stone thread->GetID(), thread->GetProtocolID());
3251c22be69SGreg Clayton
326b9c1b51eSKate Stone StructuredData::StringSP reg_context_data =
327b9c1b51eSKate Stone m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp,
328b9c1b51eSKate Stone thread->GetID());
329b9c1b51eSKate Stone if (reg_context_data) {
330adcd0268SBenjamin Kramer std::string value = std::string(reg_context_data->GetValue());
3310641ca1aSZachary Turner DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
332b9c1b51eSKate Stone if (data_sp->GetByteSize()) {
333b9c1b51eSKate Stone RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
334b9c1b51eSKate Stone *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
335b9c1b51eSKate Stone if (reg_ctx_memory) {
336435ce139SGreg Clayton reg_ctx_sp.reset(reg_ctx_memory);
337435ce139SGreg Clayton reg_ctx_memory->SetAllRegisterData(data_sp);
338435ce139SGreg Clayton }
339435ce139SGreg Clayton }
340435ce139SGreg Clayton }
341ead45e01SGreg Clayton }
342b9c1b51eSKate Stone // if we still have no register data, fallback on a dummy context to avoid
343b9c1b51eSKate Stone // crashing
344b9c1b51eSKate Stone if (!reg_ctx_sp) {
34563e5fb76SJonas Devlieghere LLDB_LOGF(log,
34663e5fb76SJonas Devlieghere "OperatingSystemPython::CreateRegisterContextForThread (tid "
347b9c1b51eSKate Stone "= 0x%" PRIx64 ") forcing a dummy register context",
348b9c1b51eSKate Stone thread->GetID());
349796ac80bSJonas Devlieghere reg_ctx_sp = std::make_shared<RegisterContextDummy>(
350796ac80bSJonas Devlieghere *thread, 0, target.GetArchitecture().GetAddressByteSize());
351cbd79b6cSEnrico Granata }
352b3e77600SGreg Clayton return reg_ctx_sp;
353b3e77600SGreg Clayton }
354b3e77600SGreg Clayton
355b3e77600SGreg Clayton StopInfoSP
CreateThreadStopReason(lldb_private::Thread * thread)356b9c1b51eSKate Stone OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
357b3e77600SGreg Clayton // We should have gotten the thread stop info from the dictionary of data for
358b3e77600SGreg Clayton // the thread in the initial call to get_thread_info(), this should have been
359b3e77600SGreg Clayton // cached so we can return it here
360b9c1b51eSKate Stone StopInfoSP
361b9c1b51eSKate Stone stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
362b3e77600SGreg Clayton return stop_info_sp;
363b3e77600SGreg Clayton }
364b3e77600SGreg Clayton
CreateThread(lldb::tid_t tid,addr_t context)365b9c1b51eSKate Stone lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
366b9c1b51eSKate Stone addr_t context) {
367a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Thread);
368a4d8747dSGreg Clayton
36963e5fb76SJonas Devlieghere LLDB_LOGF(log,
37063e5fb76SJonas Devlieghere "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
371b9c1b51eSKate Stone ", context = 0x%" PRIx64 ") fetching register data from python",
372b9c1b51eSKate Stone tid, context);
373a4d8747dSGreg Clayton
374b9c1b51eSKate Stone if (m_interpreter && m_python_object_sp) {
37585a5ec51SJonas Devlieghere // First thing we have to do is to try to get the API lock, and the
37685a5ec51SJonas Devlieghere // interpreter lock. We're going to change the thread content of the
37785a5ec51SJonas Devlieghere // process, and we're going to use python, which requires the API lock to
37885a5ec51SJonas Devlieghere // do it. We need the interpreter lock to make sure thread_info_dict stays
37985a5ec51SJonas Devlieghere // alive.
38085a5ec51SJonas Devlieghere //
38185a5ec51SJonas Devlieghere // If someone already has the API lock, that is ok, we just want to avoid
38285a5ec51SJonas Devlieghere // external code from making new API calls while this call is happening.
38385a5ec51SJonas Devlieghere //
38485a5ec51SJonas Devlieghere // This is a recursive lock so we can grant it to any Python code called on
38585a5ec51SJonas Devlieghere // the stack below us.
386a4d8747dSGreg Clayton Target &target = m_process->GetTarget();
38785a5ec51SJonas Devlieghere std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
38885a5ec51SJonas Devlieghere std::defer_lock);
3894d23764dSReid Kleckner (void)api_lock.try_lock(); // See above.
39085a5ec51SJonas Devlieghere auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
391a4d8747dSGreg Clayton
392b9c1b51eSKate Stone StructuredData::DictionarySP thread_info_dict =
393b9c1b51eSKate Stone m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
394e98008ccSGreg Clayton std::vector<bool> core_used_map;
395b9c1b51eSKate Stone if (thread_info_dict) {
396b3ae8761SGreg Clayton ThreadList core_threads(m_process);
397a4d8747dSGreg Clayton ThreadList &thread_list = m_process->GetThreadList();
398a4d8747dSGreg Clayton bool did_create = false;
399b9c1b51eSKate Stone ThreadSP thread_sp(
400b9c1b51eSKate Stone CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
401b9c1b51eSKate Stone thread_list, core_used_map, &did_create));
402a4d8747dSGreg Clayton if (did_create)
403a4d8747dSGreg Clayton thread_list.AddThread(thread_sp);
404a4d8747dSGreg Clayton return thread_sp;
405a4d8747dSGreg Clayton }
406a4d8747dSGreg Clayton }
407a4d8747dSGreg Clayton return ThreadSP();
408a4d8747dSGreg Clayton }
409a4d8747dSGreg Clayton
4104e26cf2cSJonas Devlieghere #endif // #if LLDB_ENABLE_PYTHON
411