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 //===----------------------------------------------------------------------===// 9b3e77600SGreg Clayton #ifndef LLDB_DISABLE_PYTHON 10b3e77600SGreg Clayton 11b3e77600SGreg Clayton #include "OperatingSystemPython.h" 12b3e77600SGreg Clayton // C Includes 13b3e77600SGreg Clayton // C++ Includes 14b3e77600SGreg Clayton // Other libraries and framework includes 15b3e77600SGreg Clayton #include "lldb/Core/ArchSpec.h" 16b3e77600SGreg Clayton #include "lldb/Core/DataBufferHeap.h" 175790759aSEnrico Granata #include "lldb/Core/Debugger.h" 18b3e77600SGreg Clayton #include "lldb/Core/Module.h" 19b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h" 202443cbd7SGreg Clayton #include "lldb/Interpreter/PythonDataObjects.h" 21b3e77600SGreg Clayton #include "lldb/Core/RegisterValue.h" 22b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h" 235790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h" 245790759aSEnrico Granata #include "lldb/Interpreter/PythonDataObjects.h" 25b3e77600SGreg Clayton #include "lldb/Symbol/ClangNamespaceDecl.h" 26b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h" 27b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h" 28b3e77600SGreg Clayton #include "lldb/Target/Process.h" 29b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h" 30b3e77600SGreg Clayton #include "lldb/Target/Target.h" 31b3e77600SGreg Clayton #include "lldb/Target/ThreadList.h" 32b3e77600SGreg Clayton #include "lldb/Target/Thread.h" 33b3e77600SGreg Clayton #include "Plugins/Process/Utility/DynamicRegisterInfo.h" 34b3e77600SGreg Clayton #include "Plugins/Process/Utility/RegisterContextMemory.h" 35b3e77600SGreg Clayton #include "Plugins/Process/Utility/ThreadMemory.h" 36b3e77600SGreg Clayton 37b3e77600SGreg Clayton using namespace lldb; 38b3e77600SGreg Clayton using namespace lldb_private; 39b3e77600SGreg Clayton 40b3e77600SGreg Clayton void 41b3e77600SGreg Clayton OperatingSystemPython::Initialize() 42b3e77600SGreg Clayton { 43b3e77600SGreg Clayton PluginManager::RegisterPlugin (GetPluginNameStatic(), 44b3e77600SGreg Clayton GetPluginDescriptionStatic(), 45b3e77600SGreg Clayton CreateInstance); 46b3e77600SGreg Clayton } 47b3e77600SGreg Clayton 48b3e77600SGreg Clayton void 49b3e77600SGreg Clayton OperatingSystemPython::Terminate() 50b3e77600SGreg Clayton { 51b3e77600SGreg Clayton PluginManager::UnregisterPlugin (CreateInstance); 52b3e77600SGreg Clayton } 53b3e77600SGreg Clayton 54b3e77600SGreg Clayton OperatingSystem * 55b3e77600SGreg Clayton OperatingSystemPython::CreateInstance (Process *process, bool force) 56b3e77600SGreg Clayton { 57b3e77600SGreg Clayton // Python OperatingSystem plug-ins must be requested by name, so force must be true 58c9d645d3SGreg Clayton FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath()); 59c9d645d3SGreg Clayton if (python_os_plugin_spec && python_os_plugin_spec.Exists()) 60c9d645d3SGreg Clayton { 61c9d645d3SGreg Clayton std::auto_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec)); 62c9d645d3SGreg Clayton if (os_ap.get() && os_ap->IsValid()) 63c9d645d3SGreg Clayton return os_ap.release(); 64c9d645d3SGreg Clayton } 65b3e77600SGreg Clayton return NULL; 66b3e77600SGreg Clayton } 67b3e77600SGreg Clayton 68b3e77600SGreg Clayton 69b3e77600SGreg Clayton const char * 70b3e77600SGreg Clayton OperatingSystemPython::GetPluginNameStatic() 71b3e77600SGreg Clayton { 72b3e77600SGreg Clayton return "python"; 73b3e77600SGreg Clayton } 74b3e77600SGreg Clayton 75b3e77600SGreg Clayton const char * 76b3e77600SGreg Clayton OperatingSystemPython::GetPluginDescriptionStatic() 77b3e77600SGreg Clayton { 78b3e77600SGreg Clayton return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality."; 79b3e77600SGreg Clayton } 80b3e77600SGreg Clayton 81b3e77600SGreg Clayton 82c9d645d3SGreg Clayton OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) : 83b3e77600SGreg Clayton OperatingSystem (process), 84b3e77600SGreg Clayton m_thread_list_valobj_sp (), 855790759aSEnrico Granata m_register_info_ap (), 865790759aSEnrico Granata m_interpreter (NULL), 875790759aSEnrico Granata m_python_object (NULL) 88b3e77600SGreg Clayton { 895790759aSEnrico Granata if (!process) 905790759aSEnrico Granata return; 915790759aSEnrico Granata lldb::TargetSP target_sp = process->CalculateTarget(); 925790759aSEnrico Granata if (!target_sp) 935790759aSEnrico Granata return; 945790759aSEnrico Granata m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 955790759aSEnrico Granata if (m_interpreter) 965790759aSEnrico Granata { 972443cbd7SGreg Clayton 98c9d645d3SGreg Clayton std::string os_plugin_class_name (python_module_path.GetFilename().AsCString("")); 99c9d645d3SGreg Clayton if (!os_plugin_class_name.empty()) 100c9d645d3SGreg Clayton { 101c9d645d3SGreg Clayton const bool init_session = false; 102c9d645d3SGreg Clayton const bool allow_reload = true; 103c9d645d3SGreg Clayton char python_module_path_cstr[PATH_MAX]; 104c9d645d3SGreg Clayton python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr)); 105c9d645d3SGreg Clayton Error error; 106c9d645d3SGreg Clayton if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error)) 107c9d645d3SGreg Clayton { 108c9d645d3SGreg Clayton // Strip the ".py" extension if there is one 109c9d645d3SGreg Clayton size_t py_extension_pos = os_plugin_class_name.rfind(".py"); 110c9d645d3SGreg Clayton if (py_extension_pos != std::string::npos) 111c9d645d3SGreg Clayton os_plugin_class_name.erase (py_extension_pos); 112c9d645d3SGreg Clayton // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn" 113c9d645d3SGreg Clayton os_plugin_class_name += ".OperatingSystemPlugIn"; 114c9d645d3SGreg Clayton auto object_sp = m_interpreter->CreateOSPlugin(os_plugin_class_name.c_str(), process->CalculateProcess()); 115c9d645d3SGreg Clayton if (object_sp) 116c9d645d3SGreg Clayton m_python_object = object_sp->GetObject(); 117c9d645d3SGreg Clayton } 1182443cbd7SGreg Clayton } 1195790759aSEnrico Granata } 120b3e77600SGreg Clayton } 121b3e77600SGreg Clayton 122b3e77600SGreg Clayton OperatingSystemPython::~OperatingSystemPython () 123b3e77600SGreg Clayton { 124b3e77600SGreg Clayton } 125b3e77600SGreg Clayton 126b3e77600SGreg Clayton DynamicRegisterInfo * 127b3e77600SGreg Clayton OperatingSystemPython::GetDynamicRegisterInfo () 128b3e77600SGreg Clayton { 1292443cbd7SGreg Clayton if (m_register_info_ap.get() == NULL) 1302443cbd7SGreg Clayton { 1315790759aSEnrico Granata if (!m_interpreter || !m_python_object) 1325790759aSEnrico Granata return NULL; 1331c22be69SGreg Clayton LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1341c22be69SGreg Clayton 1351c22be69SGreg Clayton if (log) 136*d01b2953SDaniel Malea log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID()); 1371c22be69SGreg Clayton 1385790759aSEnrico Granata auto object_sp = m_interpreter->OSPlugin_QueryForRegisterInfo(m_interpreter->MakeScriptObject(m_python_object)); 1395790759aSEnrico Granata if (!object_sp) 1405790759aSEnrico Granata return NULL; 1415790759aSEnrico Granata PythonDataObject dictionary_data_obj((PyObject*)object_sp->GetObject()); 1425790759aSEnrico Granata PythonDataDictionary dictionary = dictionary_data_obj.GetDictionaryObject(); 1435790759aSEnrico Granata if (!dictionary) 1445790759aSEnrico Granata return NULL; 145b3e77600SGreg Clayton 1462443cbd7SGreg Clayton m_register_info_ap.reset (new DynamicRegisterInfo (dictionary)); 1472443cbd7SGreg Clayton assert (m_register_info_ap->GetNumRegisters() > 0); 1482443cbd7SGreg Clayton assert (m_register_info_ap->GetNumRegisterSets() > 0); 149b3e77600SGreg Clayton } 150b3e77600SGreg Clayton return m_register_info_ap.get(); 151b3e77600SGreg Clayton } 152b3e77600SGreg Clayton 153b3e77600SGreg Clayton //------------------------------------------------------------------ 154b3e77600SGreg Clayton // PluginInterface protocol 155b3e77600SGreg Clayton //------------------------------------------------------------------ 156b3e77600SGreg Clayton const char * 157b3e77600SGreg Clayton OperatingSystemPython::GetPluginName() 158b3e77600SGreg Clayton { 159b3e77600SGreg Clayton return "OperatingSystemPython"; 160b3e77600SGreg Clayton } 161b3e77600SGreg Clayton 162b3e77600SGreg Clayton const char * 163b3e77600SGreg Clayton OperatingSystemPython::GetShortPluginName() 164b3e77600SGreg Clayton { 165b3e77600SGreg Clayton return GetPluginNameStatic(); 166b3e77600SGreg Clayton } 167b3e77600SGreg Clayton 168b3e77600SGreg Clayton uint32_t 169b3e77600SGreg Clayton OperatingSystemPython::GetPluginVersion() 170b3e77600SGreg Clayton { 171b3e77600SGreg Clayton return 1; 172b3e77600SGreg Clayton } 173b3e77600SGreg Clayton 174b3e77600SGreg Clayton bool 175b3e77600SGreg Clayton OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) 176b3e77600SGreg Clayton { 1776167ab28SEnrico Granata if (!m_interpreter || !m_python_object) 1786167ab28SEnrico Granata return NULL; 1791c22be69SGreg Clayton 1801c22be69SGreg Clayton LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1811c22be69SGreg Clayton 1821c22be69SGreg Clayton if (log) 183*d01b2953SDaniel Malea log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID()); 1841c22be69SGreg Clayton 1856167ab28SEnrico Granata auto object_sp = m_interpreter->OSPlugin_QueryForThreadsInfo(m_interpreter->MakeScriptObject(m_python_object)); 1866167ab28SEnrico Granata if (!object_sp) 1876167ab28SEnrico Granata return NULL; 188a83b6cf2SGreg Clayton PythonDataObject pyobj((PyObject*)object_sp->GetObject()); 189435ce139SGreg Clayton PythonDataArray threads_array (pyobj.GetArrayObject()); 190435ce139SGreg Clayton if (threads_array) 191435ce139SGreg Clayton { 192435ce139SGreg Clayton // const uint32_t num_old_threads = old_thread_list.GetSize(false); 193435ce139SGreg Clayton // for (uint32_t i=0; i<num_old_threads; ++i) 194b3e77600SGreg Clayton // { 195435ce139SGreg Clayton // ThreadSP old_thread_sp(old_thread_list.GetThreadAtIndex(i, false)); 196435ce139SGreg Clayton // if (old_thread_sp->GetID() < 0x10000) 197435ce139SGreg Clayton // new_thread_list.AddThread (old_thread_sp); 198b3e77600SGreg Clayton // } 199435ce139SGreg Clayton 200435ce139SGreg Clayton PythonDataString tid_pystr("tid"); 201435ce139SGreg Clayton PythonDataString name_pystr("name"); 202435ce139SGreg Clayton PythonDataString queue_pystr("queue"); 203435ce139SGreg Clayton PythonDataString state_pystr("state"); 204435ce139SGreg Clayton PythonDataString stop_reason_pystr("stop_reason"); 205ead45e01SGreg Clayton PythonDataString reg_data_addr_pystr ("register_data_addr"); 206435ce139SGreg Clayton 207435ce139SGreg Clayton const uint32_t num_threads = threads_array.GetSize(); 208435ce139SGreg Clayton for (uint32_t i=0; i<num_threads; ++i) 209435ce139SGreg Clayton { 210435ce139SGreg Clayton PythonDataDictionary thread_dict(threads_array.GetItemAtIndex(i).GetDictionaryObject()); 211435ce139SGreg Clayton if (thread_dict) 212435ce139SGreg Clayton { 213435ce139SGreg Clayton const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID); 214ead45e01SGreg Clayton const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS); 215435ce139SGreg Clayton const char *name = thread_dict.GetItemForKeyAsString (name_pystr); 216435ce139SGreg Clayton const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr); 217435ce139SGreg Clayton //const char *state = thread_dict.GetItemForKeyAsString (state_pystr); 218435ce139SGreg Clayton //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr); 219435ce139SGreg Clayton 220435ce139SGreg Clayton ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false)); 221435ce139SGreg Clayton if (!thread_sp) 2224f465cffSJim Ingham thread_sp.reset (new ThreadMemory (*m_process, 223435ce139SGreg Clayton tid, 224435ce139SGreg Clayton name, 225ead45e01SGreg Clayton queue, 226ead45e01SGreg Clayton reg_data_addr)); 227435ce139SGreg Clayton new_thread_list.AddThread(thread_sp); 228435ce139SGreg Clayton 229435ce139SGreg Clayton } 230435ce139SGreg Clayton } 231435ce139SGreg Clayton } 232435ce139SGreg Clayton else 233435ce139SGreg Clayton { 234b3e77600SGreg Clayton new_thread_list = old_thread_list; 235435ce139SGreg Clayton } 236b3e77600SGreg Clayton return new_thread_list.GetSize(false) > 0; 237b3e77600SGreg Clayton } 238b3e77600SGreg Clayton 239b3e77600SGreg Clayton void 240b3e77600SGreg Clayton OperatingSystemPython::ThreadWasSelected (Thread *thread) 241b3e77600SGreg Clayton { 242b3e77600SGreg Clayton } 243b3e77600SGreg Clayton 244b3e77600SGreg Clayton RegisterContextSP 245ead45e01SGreg Clayton OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, lldb::addr_t reg_data_addr) 246b3e77600SGreg Clayton { 247435ce139SGreg Clayton RegisterContextSP reg_ctx_sp; 2486167ab28SEnrico Granata if (!m_interpreter || !m_python_object || !thread) 249c44306f7SFilipe Cabecinhas return RegisterContextSP(); 2501c22be69SGreg Clayton 2511c22be69SGreg Clayton LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 2521c22be69SGreg Clayton 253ead45e01SGreg Clayton if (reg_data_addr != LLDB_INVALID_ADDRESS) 254ead45e01SGreg Clayton { 255ead45e01SGreg Clayton // The registers data is in contiguous memory, just create the register 256ead45e01SGreg Clayton // context using the address provided 257ead45e01SGreg Clayton if (log) 258*d01b2953SDaniel Malea log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context", thread->GetID(), reg_data_addr); 259ead45e01SGreg Clayton reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr)); 260ead45e01SGreg Clayton } 261ead45e01SGreg Clayton else 262ead45e01SGreg Clayton { 263ead45e01SGreg Clayton // No register data address is provided, query the python plug-in to let 264ead45e01SGreg Clayton // it make up the data as it sees fit 2651c22be69SGreg Clayton if (log) 266*d01b2953SDaniel Malea log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") fetching register data from python", thread->GetID()); 2671c22be69SGreg Clayton 268435ce139SGreg Clayton auto object_sp = m_interpreter->OSPlugin_QueryForRegisterContextData (m_interpreter->MakeScriptObject(m_python_object), 2696167ab28SEnrico Granata thread->GetID()); 270435ce139SGreg Clayton 2716167ab28SEnrico Granata if (!object_sp) 272c44306f7SFilipe Cabecinhas return RegisterContextSP(); 2736167ab28SEnrico Granata 274435ce139SGreg Clayton PythonDataString reg_context_data((PyObject*)object_sp->GetObject()); 275435ce139SGreg Clayton if (reg_context_data) 276435ce139SGreg Clayton { 277435ce139SGreg Clayton DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(), 278435ce139SGreg Clayton reg_context_data.GetSize())); 279435ce139SGreg Clayton if (data_sp->GetByteSize()) 280435ce139SGreg Clayton { 281435ce139SGreg Clayton RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS); 282435ce139SGreg Clayton if (reg_ctx_memory) 283435ce139SGreg Clayton { 284435ce139SGreg Clayton reg_ctx_sp.reset(reg_ctx_memory); 285435ce139SGreg Clayton reg_ctx_memory->SetAllRegisterData (data_sp); 286435ce139SGreg Clayton } 287435ce139SGreg Clayton } 288435ce139SGreg Clayton } 289ead45e01SGreg Clayton } 290b3e77600SGreg Clayton return reg_ctx_sp; 291b3e77600SGreg Clayton } 292b3e77600SGreg Clayton 293b3e77600SGreg Clayton StopInfoSP 294b3e77600SGreg Clayton OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread) 295b3e77600SGreg Clayton { 296b3e77600SGreg Clayton // We should have gotten the thread stop info from the dictionary of data for 297b3e77600SGreg Clayton // the thread in the initial call to get_thread_info(), this should have been 298b3e77600SGreg Clayton // cached so we can return it here 299b3e77600SGreg Clayton StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 300b3e77600SGreg Clayton return stop_info_sp; 301b3e77600SGreg Clayton } 302b3e77600SGreg Clayton 303b3e77600SGreg Clayton 304b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON 305