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" 24b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h" 255790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h" 265790759aSEnrico Granata #include "lldb/Interpreter/PythonDataObjects.h" 27b3e77600SGreg Clayton #include "lldb/Symbol/ClangNamespaceDecl.h" 28b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h" 29b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h" 30b3e77600SGreg Clayton #include "lldb/Target/Process.h" 31b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h" 32b3e77600SGreg Clayton #include "lldb/Target/Target.h" 33b3e77600SGreg Clayton #include "lldb/Target/ThreadList.h" 34b3e77600SGreg Clayton #include "lldb/Target/Thread.h" 35b3e77600SGreg Clayton #include "Plugins/Process/Utility/DynamicRegisterInfo.h" 36b3e77600SGreg Clayton #include "Plugins/Process/Utility/RegisterContextMemory.h" 37b3e77600SGreg Clayton #include "Plugins/Process/Utility/ThreadMemory.h" 38b3e77600SGreg Clayton 39b3e77600SGreg Clayton using namespace lldb; 40b3e77600SGreg Clayton using namespace lldb_private; 41b3e77600SGreg Clayton 42b3e77600SGreg Clayton void 43b3e77600SGreg Clayton OperatingSystemPython::Initialize() 44b3e77600SGreg Clayton { 45b3e77600SGreg Clayton PluginManager::RegisterPlugin (GetPluginNameStatic(), 46b3e77600SGreg Clayton GetPluginDescriptionStatic(), 47b3e77600SGreg Clayton CreateInstance); 48b3e77600SGreg Clayton } 49b3e77600SGreg Clayton 50b3e77600SGreg Clayton void 51b3e77600SGreg Clayton OperatingSystemPython::Terminate() 52b3e77600SGreg Clayton { 53b3e77600SGreg Clayton PluginManager::UnregisterPlugin (CreateInstance); 54b3e77600SGreg Clayton } 55b3e77600SGreg Clayton 56b3e77600SGreg Clayton OperatingSystem * 57b3e77600SGreg Clayton OperatingSystemPython::CreateInstance (Process *process, bool force) 58b3e77600SGreg Clayton { 59b3e77600SGreg Clayton // Python OperatingSystem plug-ins must be requested by name, so force must be true 60c9d645d3SGreg Clayton FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath()); 61c9d645d3SGreg Clayton if (python_os_plugin_spec && python_os_plugin_spec.Exists()) 62c9d645d3SGreg Clayton { 63c9d645d3SGreg Clayton std::auto_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec)); 64c9d645d3SGreg Clayton if (os_ap.get() && os_ap->IsValid()) 65c9d645d3SGreg Clayton return os_ap.release(); 66c9d645d3SGreg Clayton } 67b3e77600SGreg Clayton return NULL; 68b3e77600SGreg Clayton } 69b3e77600SGreg Clayton 70b3e77600SGreg Clayton 71b3e77600SGreg Clayton const char * 72b3e77600SGreg Clayton OperatingSystemPython::GetPluginNameStatic() 73b3e77600SGreg Clayton { 74b3e77600SGreg Clayton return "python"; 75b3e77600SGreg Clayton } 76b3e77600SGreg Clayton 77b3e77600SGreg Clayton const char * 78b3e77600SGreg Clayton OperatingSystemPython::GetPluginDescriptionStatic() 79b3e77600SGreg Clayton { 80b3e77600SGreg Clayton return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality."; 81b3e77600SGreg Clayton } 82b3e77600SGreg Clayton 83b3e77600SGreg Clayton 84c9d645d3SGreg Clayton OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) : 85b3e77600SGreg Clayton OperatingSystem (process), 86b3e77600SGreg Clayton m_thread_list_valobj_sp (), 875790759aSEnrico Granata m_register_info_ap (), 885790759aSEnrico Granata m_interpreter (NULL), 89a4d8747dSGreg Clayton m_python_object_sp () 90b3e77600SGreg Clayton { 915790759aSEnrico Granata if (!process) 925790759aSEnrico Granata return; 93a4d8747dSGreg Clayton TargetSP target_sp = process->CalculateTarget(); 945790759aSEnrico Granata if (!target_sp) 955790759aSEnrico Granata return; 965790759aSEnrico Granata m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 975790759aSEnrico Granata if (m_interpreter) 985790759aSEnrico Granata { 992443cbd7SGreg Clayton 100c9d645d3SGreg Clayton std::string os_plugin_class_name (python_module_path.GetFilename().AsCString("")); 101c9d645d3SGreg Clayton if (!os_plugin_class_name.empty()) 102c9d645d3SGreg Clayton { 103c9d645d3SGreg Clayton const bool init_session = false; 104c9d645d3SGreg Clayton const bool allow_reload = true; 105c9d645d3SGreg Clayton char python_module_path_cstr[PATH_MAX]; 106c9d645d3SGreg Clayton python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr)); 107c9d645d3SGreg Clayton Error error; 108c9d645d3SGreg Clayton if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error)) 109c9d645d3SGreg Clayton { 110c9d645d3SGreg Clayton // Strip the ".py" extension if there is one 111c9d645d3SGreg Clayton size_t py_extension_pos = os_plugin_class_name.rfind(".py"); 112c9d645d3SGreg Clayton if (py_extension_pos != std::string::npos) 113c9d645d3SGreg Clayton os_plugin_class_name.erase (py_extension_pos); 114c9d645d3SGreg Clayton // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn" 115c9d645d3SGreg Clayton os_plugin_class_name += ".OperatingSystemPlugIn"; 116a4d8747dSGreg Clayton ScriptInterpreterObjectSP object_sp = m_interpreter->OSPlugin_CreatePluginObject(os_plugin_class_name.c_str(), process->CalculateProcess()); 117a4d8747dSGreg Clayton if (object_sp && object_sp->GetObject()) 118a4d8747dSGreg Clayton m_python_object_sp = object_sp; 119c9d645d3SGreg Clayton } 1202443cbd7SGreg Clayton } 1215790759aSEnrico Granata } 122b3e77600SGreg Clayton } 123b3e77600SGreg Clayton 124b3e77600SGreg Clayton OperatingSystemPython::~OperatingSystemPython () 125b3e77600SGreg Clayton { 126b3e77600SGreg Clayton } 127b3e77600SGreg Clayton 128b3e77600SGreg Clayton DynamicRegisterInfo * 129b3e77600SGreg Clayton OperatingSystemPython::GetDynamicRegisterInfo () 130b3e77600SGreg Clayton { 1312443cbd7SGreg Clayton if (m_register_info_ap.get() == NULL) 1322443cbd7SGreg Clayton { 133a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp) 1345790759aSEnrico Granata return NULL; 1351c22be69SGreg Clayton LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1361c22be69SGreg Clayton 1371c22be69SGreg Clayton if (log) 138d01b2953SDaniel Malea log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID()); 1391c22be69SGreg Clayton 140a4d8747dSGreg Clayton PythonDictionary dictionary(m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp)); 1415790759aSEnrico Granata if (!dictionary) 1425790759aSEnrico Granata return NULL; 143b3e77600SGreg Clayton 1442443cbd7SGreg Clayton m_register_info_ap.reset (new DynamicRegisterInfo (dictionary)); 1452443cbd7SGreg Clayton assert (m_register_info_ap->GetNumRegisters() > 0); 1462443cbd7SGreg Clayton assert (m_register_info_ap->GetNumRegisterSets() > 0); 147b3e77600SGreg Clayton } 148b3e77600SGreg Clayton return m_register_info_ap.get(); 149b3e77600SGreg Clayton } 150b3e77600SGreg Clayton 151b3e77600SGreg Clayton //------------------------------------------------------------------ 152b3e77600SGreg Clayton // PluginInterface protocol 153b3e77600SGreg Clayton //------------------------------------------------------------------ 154b3e77600SGreg Clayton const char * 155b3e77600SGreg Clayton OperatingSystemPython::GetPluginName() 156b3e77600SGreg Clayton { 157b3e77600SGreg Clayton return "OperatingSystemPython"; 158b3e77600SGreg Clayton } 159b3e77600SGreg Clayton 160b3e77600SGreg Clayton const char * 161b3e77600SGreg Clayton OperatingSystemPython::GetShortPluginName() 162b3e77600SGreg Clayton { 163b3e77600SGreg Clayton return GetPluginNameStatic(); 164b3e77600SGreg Clayton } 165b3e77600SGreg Clayton 166b3e77600SGreg Clayton uint32_t 167b3e77600SGreg Clayton OperatingSystemPython::GetPluginVersion() 168b3e77600SGreg Clayton { 169b3e77600SGreg Clayton return 1; 170b3e77600SGreg Clayton } 171b3e77600SGreg Clayton 172b3e77600SGreg Clayton bool 173b3e77600SGreg Clayton OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) 174b3e77600SGreg Clayton { 175a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp) 176a85e6b6cSDaniel Malea return false; 1771c22be69SGreg Clayton 1781c22be69SGreg Clayton LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1791c22be69SGreg Clayton 18085e276b8SJim Ingham // First thing we have to do is get the API lock, and the run lock. We're going to change the thread 18185e276b8SJim Ingham // content of the process, and we're going to use python, which requires the API lock to do it. 18285e276b8SJim 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. 18385e276b8SJim Ingham Target &target = m_process->GetTarget(); 18485e276b8SJim Ingham Mutex::Locker api_locker (target.GetAPIMutex()); 18585e276b8SJim Ingham 1861c22be69SGreg Clayton if (log) 187d01b2953SDaniel Malea log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID()); 1881c22be69SGreg Clayton 189a4d8747dSGreg Clayton PythonList threads_list(m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp)); 190a4d8747dSGreg Clayton if (threads_list) 191435ce139SGreg Clayton { 192a4d8747dSGreg Clayton const uint32_t num_threads = threads_list.GetSize(); 193435ce139SGreg Clayton for (uint32_t i=0; i<num_threads; ++i) 194435ce139SGreg Clayton { 195a4d8747dSGreg Clayton PythonDictionary thread_dict(threads_list.GetItemAtIndex(i)); 196435ce139SGreg Clayton if (thread_dict) 197435ce139SGreg Clayton { 198a4d8747dSGreg Clayton ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_dict, &old_thread_list, NULL)); 199a4d8747dSGreg Clayton if (thread_sp) 200435ce139SGreg Clayton new_thread_list.AddThread(thread_sp); 201435ce139SGreg Clayton } 202435ce139SGreg Clayton } 203435ce139SGreg Clayton } 204435ce139SGreg Clayton else 205435ce139SGreg Clayton { 206b3e77600SGreg Clayton new_thread_list = old_thread_list; 207435ce139SGreg Clayton } 208b3e77600SGreg Clayton return new_thread_list.GetSize(false) > 0; 209b3e77600SGreg Clayton } 210b3e77600SGreg Clayton 211a4d8747dSGreg Clayton ThreadSP 212a4d8747dSGreg Clayton OperatingSystemPython::CreateThreadFromThreadInfo (PythonDictionary &thread_dict, ThreadList *old_thread_list_ptr, bool *did_create_ptr) 213a4d8747dSGreg Clayton { 214a4d8747dSGreg Clayton ThreadSP thread_sp; 215a4d8747dSGreg Clayton if (thread_dict) 216a4d8747dSGreg Clayton { 217a4d8747dSGreg Clayton PythonString tid_pystr("tid"); 218*c7bece56SGreg Clayton const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID); 219*c7bece56SGreg Clayton if (tid != LLDB_INVALID_THREAD_ID) 220*c7bece56SGreg Clayton { 221a4d8747dSGreg Clayton PythonString name_pystr("name"); 222a4d8747dSGreg Clayton PythonString queue_pystr("queue"); 223a4d8747dSGreg Clayton PythonString state_pystr("state"); 224a4d8747dSGreg Clayton PythonString stop_reason_pystr("stop_reason"); 225a4d8747dSGreg Clayton PythonString reg_data_addr_pystr ("register_data_addr"); 226a4d8747dSGreg Clayton 227a4d8747dSGreg Clayton const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS); 228a4d8747dSGreg Clayton const char *name = thread_dict.GetItemForKeyAsString (name_pystr); 229a4d8747dSGreg Clayton const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr); 230a4d8747dSGreg Clayton //const char *state = thread_dict.GetItemForKeyAsString (state_pystr); 231a4d8747dSGreg Clayton //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr); 232a4d8747dSGreg Clayton 233a4d8747dSGreg Clayton if (old_thread_list_ptr) 234a4d8747dSGreg Clayton thread_sp = old_thread_list_ptr->FindThreadByID (tid, false); 235a4d8747dSGreg Clayton if (!thread_sp) 236a4d8747dSGreg Clayton { 237a4d8747dSGreg Clayton if (did_create_ptr) 238a4d8747dSGreg Clayton *did_create_ptr = true; 239a4d8747dSGreg Clayton thread_sp.reset (new ThreadMemory (*m_process, 240a4d8747dSGreg Clayton tid, 241a4d8747dSGreg Clayton name, 242a4d8747dSGreg Clayton queue, 243a4d8747dSGreg Clayton reg_data_addr)); 244a4d8747dSGreg Clayton } 245a4d8747dSGreg Clayton } 246*c7bece56SGreg Clayton } 247a4d8747dSGreg Clayton return thread_sp; 248a4d8747dSGreg Clayton } 249a4d8747dSGreg Clayton 250a4d8747dSGreg Clayton 251a4d8747dSGreg Clayton 252b3e77600SGreg Clayton void 253b3e77600SGreg Clayton OperatingSystemPython::ThreadWasSelected (Thread *thread) 254b3e77600SGreg Clayton { 255b3e77600SGreg Clayton } 256b3e77600SGreg Clayton 257b3e77600SGreg Clayton RegisterContextSP 258a4d8747dSGreg Clayton OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, addr_t reg_data_addr) 259b3e77600SGreg Clayton { 260435ce139SGreg Clayton RegisterContextSP reg_ctx_sp; 261a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp || !thread) 262c44306f7SFilipe Cabecinhas return RegisterContextSP(); 2631c22be69SGreg Clayton 26485e276b8SJim Ingham // First thing we have to do is get the API lock, and the run lock. We're going to change the thread 26585e276b8SJim Ingham // content of the process, and we're going to use python, which requires the API lock to do it. 26685e276b8SJim 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. 26785e276b8SJim Ingham Target &target = m_process->GetTarget(); 26885e276b8SJim Ingham Mutex::Locker api_locker (target.GetAPIMutex()); 26985e276b8SJim Ingham 2701c22be69SGreg Clayton LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 2711c22be69SGreg Clayton 272ead45e01SGreg Clayton if (reg_data_addr != LLDB_INVALID_ADDRESS) 273ead45e01SGreg Clayton { 274ead45e01SGreg Clayton // The registers data is in contiguous memory, just create the register 275ead45e01SGreg Clayton // context using the address provided 276ead45e01SGreg Clayton if (log) 277d01b2953SDaniel Malea log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context", thread->GetID(), reg_data_addr); 278ead45e01SGreg Clayton reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr)); 279ead45e01SGreg Clayton } 280ead45e01SGreg Clayton else 281ead45e01SGreg Clayton { 282ead45e01SGreg Clayton // No register data address is provided, query the python plug-in to let 283ead45e01SGreg Clayton // it make up the data as it sees fit 2841c22be69SGreg Clayton if (log) 285d01b2953SDaniel Malea log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") fetching register data from python", thread->GetID()); 2861c22be69SGreg Clayton 287a4d8747dSGreg Clayton PythonString reg_context_data(m_interpreter->OSPlugin_RegisterContextData (m_python_object_sp, thread->GetID())); 288435ce139SGreg Clayton if (reg_context_data) 289435ce139SGreg Clayton { 290435ce139SGreg Clayton DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(), 291435ce139SGreg Clayton reg_context_data.GetSize())); 292435ce139SGreg Clayton if (data_sp->GetByteSize()) 293435ce139SGreg Clayton { 294435ce139SGreg Clayton RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS); 295435ce139SGreg Clayton if (reg_ctx_memory) 296435ce139SGreg Clayton { 297435ce139SGreg Clayton reg_ctx_sp.reset(reg_ctx_memory); 298435ce139SGreg Clayton reg_ctx_memory->SetAllRegisterData (data_sp); 299435ce139SGreg Clayton } 300435ce139SGreg Clayton } 301435ce139SGreg Clayton } 302ead45e01SGreg Clayton } 303b3e77600SGreg Clayton return reg_ctx_sp; 304b3e77600SGreg Clayton } 305b3e77600SGreg Clayton 306b3e77600SGreg Clayton StopInfoSP 307b3e77600SGreg Clayton OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread) 308b3e77600SGreg Clayton { 309b3e77600SGreg Clayton // We should have gotten the thread stop info from the dictionary of data for 310b3e77600SGreg Clayton // the thread in the initial call to get_thread_info(), this should have been 311b3e77600SGreg Clayton // cached so we can return it here 312b3e77600SGreg Clayton StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 313b3e77600SGreg Clayton return stop_info_sp; 314b3e77600SGreg Clayton } 315b3e77600SGreg Clayton 316a4d8747dSGreg Clayton lldb::ThreadSP 317a4d8747dSGreg Clayton OperatingSystemPython::CreateThread (lldb::tid_t tid, addr_t context) 318a4d8747dSGreg Clayton { 319a4d8747dSGreg Clayton LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 320a4d8747dSGreg Clayton 321a4d8747dSGreg Clayton if (log) 322a4d8747dSGreg Clayton log->Printf ("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 ", context = 0x%" PRIx64 ") fetching register data from python", tid, context); 323a4d8747dSGreg Clayton 324a4d8747dSGreg Clayton if (m_interpreter && m_python_object_sp) 325a4d8747dSGreg Clayton { 326a4d8747dSGreg Clayton // First thing we have to do is get the API lock, and the run lock. We're going to change the thread 327a4d8747dSGreg Clayton // content of the process, and we're going to use python, which requires the API lock to do it. 328a4d8747dSGreg 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. 329a4d8747dSGreg Clayton Target &target = m_process->GetTarget(); 330a4d8747dSGreg Clayton Mutex::Locker api_locker (target.GetAPIMutex()); 331a4d8747dSGreg Clayton 332a4d8747dSGreg Clayton PythonDictionary thread_info_dict (m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context)); 333a4d8747dSGreg Clayton if (thread_info_dict) 334a4d8747dSGreg Clayton { 335a4d8747dSGreg Clayton ThreadList &thread_list = m_process->GetThreadList(); 336a4d8747dSGreg Clayton bool did_create = false; 337a4d8747dSGreg Clayton ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_info_dict, &thread_list, &did_create)); 338a4d8747dSGreg Clayton if (did_create) 339a4d8747dSGreg Clayton thread_list.AddThread(thread_sp); 340a4d8747dSGreg Clayton return thread_sp; 341a4d8747dSGreg Clayton } 342a4d8747dSGreg Clayton } 343a4d8747dSGreg Clayton return ThreadSP(); 344a4d8747dSGreg Clayton } 345a4d8747dSGreg Clayton 346a4d8747dSGreg Clayton 347b3e77600SGreg Clayton 348b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON 349