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