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