1 //===-- OperatingSystemPython.cpp --------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #ifndef LLDB_DISABLE_PYTHON 10 11 #include "OperatingSystemPython.h" 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 #include "lldb/Core/ArchSpec.h" 16 #include "lldb/Core/DataBufferHeap.h" 17 #include "lldb/Core/Debugger.h" 18 #include "lldb/Core/Module.h" 19 #include "lldb/Core/PluginManager.h" 20 #include "lldb/Interpreter/PythonDataObjects.h" 21 #include "lldb/Core/RegisterValue.h" 22 #include "lldb/Core/ValueObjectVariable.h" 23 #include "lldb/Interpreter/CommandInterpreter.h" 24 #include "lldb/Interpreter/PythonDataObjects.h" 25 #include "lldb/Symbol/ClangNamespaceDecl.h" 26 #include "lldb/Symbol/ObjectFile.h" 27 #include "lldb/Symbol/VariableList.h" 28 #include "lldb/Target/Process.h" 29 #include "lldb/Target/StopInfo.h" 30 #include "lldb/Target/Target.h" 31 #include "lldb/Target/ThreadList.h" 32 #include "lldb/Target/Thread.h" 33 #include "Plugins/Process/Utility/DynamicRegisterInfo.h" 34 #include "Plugins/Process/Utility/RegisterContextMemory.h" 35 #include "Plugins/Process/Utility/ThreadMemory.h" 36 37 using namespace lldb; 38 using namespace lldb_private; 39 40 void 41 OperatingSystemPython::Initialize() 42 { 43 PluginManager::RegisterPlugin (GetPluginNameStatic(), 44 GetPluginDescriptionStatic(), 45 CreateInstance); 46 } 47 48 void 49 OperatingSystemPython::Terminate() 50 { 51 PluginManager::UnregisterPlugin (CreateInstance); 52 } 53 54 OperatingSystem * 55 OperatingSystemPython::CreateInstance (Process *process, bool force) 56 { 57 // Python OperatingSystem plug-ins must be requested by name, so force must be true 58 FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath()); 59 if (python_os_plugin_spec && python_os_plugin_spec.Exists()) 60 { 61 std::auto_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec)); 62 if (os_ap.get() && os_ap->IsValid()) 63 return os_ap.release(); 64 } 65 return NULL; 66 } 67 68 69 const char * 70 OperatingSystemPython::GetPluginNameStatic() 71 { 72 return "python"; 73 } 74 75 const char * 76 OperatingSystemPython::GetPluginDescriptionStatic() 77 { 78 return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality."; 79 } 80 81 82 OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) : 83 OperatingSystem (process), 84 m_thread_list_valobj_sp (), 85 m_register_info_ap (), 86 m_interpreter (NULL), 87 m_python_object (NULL) 88 { 89 if (!process) 90 return; 91 lldb::TargetSP target_sp = process->CalculateTarget(); 92 if (!target_sp) 93 return; 94 m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 95 if (m_interpreter) 96 { 97 98 std::string os_plugin_class_name (python_module_path.GetFilename().AsCString("")); 99 if (!os_plugin_class_name.empty()) 100 { 101 const bool init_session = false; 102 const bool allow_reload = true; 103 char python_module_path_cstr[PATH_MAX]; 104 python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr)); 105 Error error; 106 if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error)) 107 { 108 // Strip the ".py" extension if there is one 109 size_t py_extension_pos = os_plugin_class_name.rfind(".py"); 110 if (py_extension_pos != std::string::npos) 111 os_plugin_class_name.erase (py_extension_pos); 112 // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn" 113 os_plugin_class_name += ".OperatingSystemPlugIn"; 114 auto object_sp = m_interpreter->CreateOSPlugin(os_plugin_class_name.c_str(), process->CalculateProcess()); 115 if (object_sp) 116 m_python_object = object_sp->GetObject(); 117 } 118 } 119 } 120 } 121 122 OperatingSystemPython::~OperatingSystemPython () 123 { 124 } 125 126 DynamicRegisterInfo * 127 OperatingSystemPython::GetDynamicRegisterInfo () 128 { 129 if (m_register_info_ap.get() == NULL) 130 { 131 if (!m_interpreter || !m_python_object) 132 return NULL; 133 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 134 135 if (log) 136 log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID()); 137 138 auto object_sp = m_interpreter->OSPlugin_QueryForRegisterInfo(m_interpreter->MakeScriptObject(m_python_object)); 139 if (!object_sp) 140 return NULL; 141 PythonDataObject dictionary_data_obj((PyObject*)object_sp->GetObject()); 142 PythonDataDictionary dictionary = dictionary_data_obj.GetDictionaryObject(); 143 if (!dictionary) 144 return NULL; 145 146 m_register_info_ap.reset (new DynamicRegisterInfo (dictionary)); 147 assert (m_register_info_ap->GetNumRegisters() > 0); 148 assert (m_register_info_ap->GetNumRegisterSets() > 0); 149 } 150 return m_register_info_ap.get(); 151 } 152 153 //------------------------------------------------------------------ 154 // PluginInterface protocol 155 //------------------------------------------------------------------ 156 const char * 157 OperatingSystemPython::GetPluginName() 158 { 159 return "OperatingSystemPython"; 160 } 161 162 const char * 163 OperatingSystemPython::GetShortPluginName() 164 { 165 return GetPluginNameStatic(); 166 } 167 168 uint32_t 169 OperatingSystemPython::GetPluginVersion() 170 { 171 return 1; 172 } 173 174 bool 175 OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) 176 { 177 if (!m_interpreter || !m_python_object) 178 return NULL; 179 180 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 181 182 if (log) 183 log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID()); 184 185 auto object_sp = m_interpreter->OSPlugin_QueryForThreadsInfo(m_interpreter->MakeScriptObject(m_python_object)); 186 if (!object_sp) 187 return NULL; 188 PythonDataObject pyobj((PyObject*)object_sp->GetObject()); 189 PythonDataArray threads_array (pyobj.GetArrayObject()); 190 if (threads_array) 191 { 192 // const uint32_t num_old_threads = old_thread_list.GetSize(false); 193 // for (uint32_t i=0; i<num_old_threads; ++i) 194 // { 195 // ThreadSP old_thread_sp(old_thread_list.GetThreadAtIndex(i, false)); 196 // if (old_thread_sp->GetID() < 0x10000) 197 // new_thread_list.AddThread (old_thread_sp); 198 // } 199 200 PythonDataString tid_pystr("tid"); 201 PythonDataString name_pystr("name"); 202 PythonDataString queue_pystr("queue"); 203 PythonDataString state_pystr("state"); 204 PythonDataString stop_reason_pystr("stop_reason"); 205 PythonDataString reg_data_addr_pystr ("register_data_addr"); 206 207 const uint32_t num_threads = threads_array.GetSize(); 208 for (uint32_t i=0; i<num_threads; ++i) 209 { 210 PythonDataDictionary thread_dict(threads_array.GetItemAtIndex(i).GetDictionaryObject()); 211 if (thread_dict) 212 { 213 const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID); 214 const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS); 215 const char *name = thread_dict.GetItemForKeyAsString (name_pystr); 216 const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr); 217 //const char *state = thread_dict.GetItemForKeyAsString (state_pystr); 218 //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr); 219 220 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false)); 221 if (!thread_sp) 222 thread_sp.reset (new ThreadMemory (*m_process, 223 tid, 224 name, 225 queue, 226 reg_data_addr)); 227 new_thread_list.AddThread(thread_sp); 228 229 } 230 } 231 } 232 else 233 { 234 new_thread_list = old_thread_list; 235 } 236 return new_thread_list.GetSize(false) > 0; 237 } 238 239 void 240 OperatingSystemPython::ThreadWasSelected (Thread *thread) 241 { 242 } 243 244 RegisterContextSP 245 OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, lldb::addr_t reg_data_addr) 246 { 247 RegisterContextSP reg_ctx_sp; 248 if (!m_interpreter || !m_python_object || !thread) 249 return RegisterContextSP(); 250 251 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 252 253 if (reg_data_addr != LLDB_INVALID_ADDRESS) 254 { 255 // The registers data is in contiguous memory, just create the register 256 // context using the address provided 257 if (log) 258 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context", thread->GetID(), reg_data_addr); 259 reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr)); 260 } 261 else 262 { 263 // No register data address is provided, query the python plug-in to let 264 // it make up the data as it sees fit 265 if (log) 266 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") fetching register data from python", thread->GetID()); 267 268 auto object_sp = m_interpreter->OSPlugin_QueryForRegisterContextData (m_interpreter->MakeScriptObject(m_python_object), 269 thread->GetID()); 270 271 if (!object_sp) 272 return RegisterContextSP(); 273 274 PythonDataString reg_context_data((PyObject*)object_sp->GetObject()); 275 if (reg_context_data) 276 { 277 DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(), 278 reg_context_data.GetSize())); 279 if (data_sp->GetByteSize()) 280 { 281 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS); 282 if (reg_ctx_memory) 283 { 284 reg_ctx_sp.reset(reg_ctx_memory); 285 reg_ctx_memory->SetAllRegisterData (data_sp); 286 } 287 } 288 } 289 } 290 return reg_ctx_sp; 291 } 292 293 StopInfoSP 294 OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread) 295 { 296 // We should have gotten the thread stop info from the dictionary of data for 297 // the thread in the initial call to get_thread_info(), this should have been 298 // cached so we can return it here 299 StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 300 return stop_info_sp; 301 } 302 303 304 #endif // #ifndef LLDB_DISABLE_PYTHON 305