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 10 #include "lldb/lldb-python.h" 11 12 #ifndef LLDB_DISABLE_PYTHON 13 14 #include "OperatingSystemPython.h" 15 // C Includes 16 // C++ Includes 17 // Other libraries and framework includes 18 #include "lldb/Core/ArchSpec.h" 19 #include "lldb/Core/DataBufferHeap.h" 20 #include "lldb/Core/Debugger.h" 21 #include "lldb/Core/Module.h" 22 #include "lldb/Core/PluginManager.h" 23 #include "lldb/Core/RegisterValue.h" 24 #include "lldb/Core/ValueObjectVariable.h" 25 #include "lldb/Interpreter/CommandInterpreter.h" 26 #include "lldb/Interpreter/PythonDataObjects.h" 27 #include "lldb/Symbol/ClangNamespaceDecl.h" 28 #include "lldb/Symbol/ObjectFile.h" 29 #include "lldb/Symbol/VariableList.h" 30 #include "lldb/Target/Process.h" 31 #include "lldb/Target/StopInfo.h" 32 #include "lldb/Target/Target.h" 33 #include "lldb/Target/ThreadList.h" 34 #include "lldb/Target/Thread.h" 35 #include "Plugins/Process/Utility/DynamicRegisterInfo.h" 36 #include "Plugins/Process/Utility/RegisterContextMemory.h" 37 #include "Plugins/Process/Utility/ThreadMemory.h" 38 39 using namespace lldb; 40 using namespace lldb_private; 41 42 void 43 OperatingSystemPython::Initialize() 44 { 45 PluginManager::RegisterPlugin (GetPluginNameStatic(), 46 GetPluginDescriptionStatic(), 47 CreateInstance); 48 } 49 50 void 51 OperatingSystemPython::Terminate() 52 { 53 PluginManager::UnregisterPlugin (CreateInstance); 54 } 55 56 OperatingSystem * 57 OperatingSystemPython::CreateInstance (Process *process, bool force) 58 { 59 // Python OperatingSystem plug-ins must be requested by name, so force must be true 60 FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath()); 61 if (python_os_plugin_spec && python_os_plugin_spec.Exists()) 62 { 63 std::auto_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec)); 64 if (os_ap.get() && os_ap->IsValid()) 65 return os_ap.release(); 66 } 67 return NULL; 68 } 69 70 71 const char * 72 OperatingSystemPython::GetPluginNameStatic() 73 { 74 return "python"; 75 } 76 77 const char * 78 OperatingSystemPython::GetPluginDescriptionStatic() 79 { 80 return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality."; 81 } 82 83 84 OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) : 85 OperatingSystem (process), 86 m_thread_list_valobj_sp (), 87 m_register_info_ap (), 88 m_interpreter (NULL), 89 m_python_object_sp () 90 { 91 if (!process) 92 return; 93 TargetSP target_sp = process->CalculateTarget(); 94 if (!target_sp) 95 return; 96 m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 97 if (m_interpreter) 98 { 99 100 std::string os_plugin_class_name (python_module_path.GetFilename().AsCString("")); 101 if (!os_plugin_class_name.empty()) 102 { 103 const bool init_session = false; 104 const bool allow_reload = true; 105 char python_module_path_cstr[PATH_MAX]; 106 python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr)); 107 Error error; 108 if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error)) 109 { 110 // Strip the ".py" extension if there is one 111 size_t py_extension_pos = os_plugin_class_name.rfind(".py"); 112 if (py_extension_pos != std::string::npos) 113 os_plugin_class_name.erase (py_extension_pos); 114 // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn" 115 os_plugin_class_name += ".OperatingSystemPlugIn"; 116 ScriptInterpreterObjectSP object_sp = m_interpreter->OSPlugin_CreatePluginObject(os_plugin_class_name.c_str(), process->CalculateProcess()); 117 if (object_sp && object_sp->GetObject()) 118 m_python_object_sp = object_sp; 119 } 120 } 121 } 122 } 123 124 OperatingSystemPython::~OperatingSystemPython () 125 { 126 } 127 128 DynamicRegisterInfo * 129 OperatingSystemPython::GetDynamicRegisterInfo () 130 { 131 if (m_register_info_ap.get() == NULL) 132 { 133 if (!m_interpreter || !m_python_object_sp) 134 return NULL; 135 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 136 137 if (log) 138 log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID()); 139 140 PythonDictionary dictionary(m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp)); 141 if (!dictionary) 142 return NULL; 143 144 m_register_info_ap.reset (new DynamicRegisterInfo (dictionary)); 145 assert (m_register_info_ap->GetNumRegisters() > 0); 146 assert (m_register_info_ap->GetNumRegisterSets() > 0); 147 } 148 return m_register_info_ap.get(); 149 } 150 151 //------------------------------------------------------------------ 152 // PluginInterface protocol 153 //------------------------------------------------------------------ 154 const char * 155 OperatingSystemPython::GetPluginName() 156 { 157 return "OperatingSystemPython"; 158 } 159 160 const char * 161 OperatingSystemPython::GetShortPluginName() 162 { 163 return GetPluginNameStatic(); 164 } 165 166 uint32_t 167 OperatingSystemPython::GetPluginVersion() 168 { 169 return 1; 170 } 171 172 bool 173 OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) 174 { 175 if (!m_interpreter || !m_python_object_sp) 176 return false; 177 178 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 179 180 // First thing we have to do is get the API lock, and the run lock. We're going to change the thread 181 // content of the process, and we're going to use python, which requires the API lock to do it. 182 // So get & hold that. This is a recursive lock so we can grant it to any Python code called on the stack below us. 183 Target &target = m_process->GetTarget(); 184 Mutex::Locker api_locker (target.GetAPIMutex()); 185 186 if (log) 187 log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID()); 188 189 // The threads that are in "new_thread_list" upon entry are the threads from the 190 // lldb_private::Process subclass, no memory threads will be in this list. 191 192 auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure threads_list stays alive 193 PythonList threads_list(m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp)); 194 if (threads_list) 195 { 196 ThreadList core_thread_list(new_thread_list); 197 198 threads_list.Dump(); // REMOVE THIS 199 200 uint32_t i; 201 const uint32_t num_threads = threads_list.GetSize(); 202 for (i=0; i<num_threads; ++i) 203 { 204 PythonDictionary thread_dict(threads_list.GetItemAtIndex(i)); 205 if (thread_dict) 206 { 207 if (thread_dict.GetItemForKey("core")) 208 { 209 // We have some threads that are saying they are on a "core", which means 210 // they map the threads that are gotten from the lldb_private::Process subclass 211 // so clear the new threads list so the core threads don't show up 212 new_thread_list.Clear(); 213 break; 214 } 215 } 216 } 217 for (i=0; i<num_threads; ++i) 218 { 219 PythonDictionary thread_dict(threads_list.GetItemAtIndex(i)); 220 if (thread_dict) 221 { 222 ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_dict, core_thread_list, old_thread_list, NULL)); 223 if (thread_sp) 224 new_thread_list.AddThread(thread_sp); 225 } 226 } 227 } 228 else 229 { 230 new_thread_list = old_thread_list; 231 } 232 return new_thread_list.GetSize(false) > 0; 233 } 234 235 ThreadSP 236 OperatingSystemPython::CreateThreadFromThreadInfo (PythonDictionary &thread_dict, 237 ThreadList &core_thread_list, 238 ThreadList &old_thread_list, 239 bool *did_create_ptr) 240 { 241 ThreadSP thread_sp; 242 if (thread_dict) 243 { 244 PythonString tid_pystr("tid"); 245 const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID); 246 if (tid != LLDB_INVALID_THREAD_ID) 247 { 248 PythonString core_pystr("core"); 249 PythonString name_pystr("name"); 250 PythonString queue_pystr("queue"); 251 PythonString state_pystr("state"); 252 PythonString stop_reason_pystr("stop_reason"); 253 PythonString reg_data_addr_pystr ("register_data_addr"); 254 255 const uint32_t core_number = thread_dict.GetItemForKeyAsInteger (core_pystr, UINT32_MAX); 256 const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS); 257 const char *name = thread_dict.GetItemForKeyAsString (name_pystr); 258 const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr); 259 //const char *state = thread_dict.GetItemForKeyAsString (state_pystr); 260 //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr); 261 262 thread_sp = old_thread_list.FindThreadByID (tid, false); 263 if (!thread_sp) 264 { 265 if (did_create_ptr) 266 *did_create_ptr = true; 267 thread_sp.reset (new ThreadMemory (*m_process, 268 tid, 269 name, 270 queue, 271 reg_data_addr)); 272 273 } 274 275 if (core_number < core_thread_list.GetSize(false)) 276 { 277 thread_sp->SetBackingThread(core_thread_list.GetThreadAtIndex(core_number, false)); 278 } 279 } 280 } 281 return thread_sp; 282 } 283 284 285 286 void 287 OperatingSystemPython::ThreadWasSelected (Thread *thread) 288 { 289 } 290 291 RegisterContextSP 292 OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, addr_t reg_data_addr) 293 { 294 RegisterContextSP reg_ctx_sp; 295 if (!m_interpreter || !m_python_object_sp || !thread) 296 return RegisterContextSP(); 297 298 // First thing we have to do is get the API lock, and the run lock. We're going to change the thread 299 // content of the process, and we're going to use python, which requires the API lock to do it. 300 // So get & hold that. This is a recursive lock so we can grant it to any Python code called on the stack below us. 301 Target &target = m_process->GetTarget(); 302 Mutex::Locker api_locker (target.GetAPIMutex()); 303 304 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 305 306 auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure python objects stays alive 307 if (reg_data_addr != LLDB_INVALID_ADDRESS) 308 { 309 // The registers data is in contiguous memory, just create the register 310 // context using the address provided 311 if (log) 312 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context", thread->GetID(), reg_data_addr); 313 reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr)); 314 } 315 else 316 { 317 // No register data address is provided, query the python plug-in to let 318 // it make up the data as it sees fit 319 if (log) 320 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") fetching register data from python", thread->GetID()); 321 322 PythonString reg_context_data(m_interpreter->OSPlugin_RegisterContextData (m_python_object_sp, thread->GetID())); 323 if (reg_context_data) 324 { 325 DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(), 326 reg_context_data.GetSize())); 327 if (data_sp->GetByteSize()) 328 { 329 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS); 330 if (reg_ctx_memory) 331 { 332 reg_ctx_sp.reset(reg_ctx_memory); 333 reg_ctx_memory->SetAllRegisterData (data_sp); 334 } 335 } 336 } 337 } 338 return reg_ctx_sp; 339 } 340 341 StopInfoSP 342 OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread) 343 { 344 // We should have gotten the thread stop info from the dictionary of data for 345 // the thread in the initial call to get_thread_info(), this should have been 346 // cached so we can return it here 347 StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 348 return stop_info_sp; 349 } 350 351 lldb::ThreadSP 352 OperatingSystemPython::CreateThread (lldb::tid_t tid, addr_t context) 353 { 354 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 355 356 if (log) 357 log->Printf ("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 ", context = 0x%" PRIx64 ") fetching register data from python", tid, context); 358 359 if (m_interpreter && m_python_object_sp) 360 { 361 // First thing we have to do is get the API lock, and the run lock. We're going to change the thread 362 // content of the process, and we're going to use python, which requires the API lock to do it. 363 // So get & hold that. This is a recursive lock so we can grant it to any Python code called on the stack below us. 364 Target &target = m_process->GetTarget(); 365 Mutex::Locker api_locker (target.GetAPIMutex()); 366 367 auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure thread_info_dict stays alive 368 PythonDictionary thread_info_dict (m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context)); 369 if (thread_info_dict) 370 { 371 ThreadList core_threads(m_process); 372 ThreadList &thread_list = m_process->GetThreadList(); 373 bool did_create = false; 374 ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_info_dict, core_threads, thread_list, &did_create)); 375 if (did_create) 376 thread_list.AddThread(thread_sp); 377 return thread_sp; 378 } 379 } 380 return ThreadSP(); 381 } 382 383 384 385 #endif // #ifndef LLDB_DISABLE_PYTHON 386