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/Interpreter/PythonDataObjects.h" 24 #include "lldb/Core/RegisterValue.h" 25 #include "lldb/Core/ValueObjectVariable.h" 26 #include "lldb/Interpreter/CommandInterpreter.h" 27 #include "lldb/Interpreter/PythonDataObjects.h" 28 #include "lldb/Symbol/ClangNamespaceDecl.h" 29 #include "lldb/Symbol/ObjectFile.h" 30 #include "lldb/Symbol/VariableList.h" 31 #include "lldb/Target/Process.h" 32 #include "lldb/Target/StopInfo.h" 33 #include "lldb/Target/Target.h" 34 #include "lldb/Target/ThreadList.h" 35 #include "lldb/Target/Thread.h" 36 #include "Plugins/Process/Utility/DynamicRegisterInfo.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::auto_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 (NULL) 91 { 92 if (!process) 93 return; 94 lldb::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 auto object_sp = m_interpreter->CreateOSPlugin(os_plugin_class_name.c_str(), process->CalculateProcess()); 118 if (object_sp) 119 m_python_object = object_sp->GetObject(); 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) 135 return NULL; 136 LogSP 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 auto object_sp = m_interpreter->OSPlugin_QueryForRegisterInfo(m_interpreter->MakeScriptObject(m_python_object)); 142 if (!object_sp) 143 return NULL; 144 PythonDataObject dictionary_data_obj((PyObject*)object_sp->GetObject()); 145 PythonDataDictionary dictionary = dictionary_data_obj.GetDictionaryObject(); 146 if (!dictionary) 147 return NULL; 148 149 m_register_info_ap.reset (new DynamicRegisterInfo (dictionary)); 150 assert (m_register_info_ap->GetNumRegisters() > 0); 151 assert (m_register_info_ap->GetNumRegisterSets() > 0); 152 } 153 return m_register_info_ap.get(); 154 } 155 156 //------------------------------------------------------------------ 157 // PluginInterface protocol 158 //------------------------------------------------------------------ 159 const char * 160 OperatingSystemPython::GetPluginName() 161 { 162 return "OperatingSystemPython"; 163 } 164 165 const char * 166 OperatingSystemPython::GetShortPluginName() 167 { 168 return GetPluginNameStatic(); 169 } 170 171 uint32_t 172 OperatingSystemPython::GetPluginVersion() 173 { 174 return 1; 175 } 176 177 bool 178 OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) 179 { 180 if (!m_interpreter || !m_python_object) 181 return NULL; 182 183 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 184 185 if (log) 186 log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID()); 187 188 auto object_sp = m_interpreter->OSPlugin_QueryForThreadsInfo(m_interpreter->MakeScriptObject(m_python_object)); 189 if (!object_sp) 190 return NULL; 191 PythonDataObject pyobj((PyObject*)object_sp->GetObject()); 192 PythonDataArray threads_array (pyobj.GetArrayObject()); 193 if (threads_array) 194 { 195 // const uint32_t num_old_threads = old_thread_list.GetSize(false); 196 // for (uint32_t i=0; i<num_old_threads; ++i) 197 // { 198 // ThreadSP old_thread_sp(old_thread_list.GetThreadAtIndex(i, false)); 199 // if (old_thread_sp->GetID() < 0x10000) 200 // new_thread_list.AddThread (old_thread_sp); 201 // } 202 203 PythonDataString tid_pystr("tid"); 204 PythonDataString name_pystr("name"); 205 PythonDataString queue_pystr("queue"); 206 PythonDataString state_pystr("state"); 207 PythonDataString stop_reason_pystr("stop_reason"); 208 PythonDataString reg_data_addr_pystr ("register_data_addr"); 209 210 const uint32_t num_threads = threads_array.GetSize(); 211 for (uint32_t i=0; i<num_threads; ++i) 212 { 213 PythonDataDictionary thread_dict(threads_array.GetItemAtIndex(i).GetDictionaryObject()); 214 if (thread_dict) 215 { 216 const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID); 217 const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS); 218 const char *name = thread_dict.GetItemForKeyAsString (name_pystr); 219 const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr); 220 //const char *state = thread_dict.GetItemForKeyAsString (state_pystr); 221 //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr); 222 223 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false)); 224 if (!thread_sp) 225 thread_sp.reset (new ThreadMemory (*m_process, 226 tid, 227 name, 228 queue, 229 reg_data_addr)); 230 new_thread_list.AddThread(thread_sp); 231 232 } 233 } 234 } 235 else 236 { 237 new_thread_list = old_thread_list; 238 } 239 return new_thread_list.GetSize(false) > 0; 240 } 241 242 void 243 OperatingSystemPython::ThreadWasSelected (Thread *thread) 244 { 245 } 246 247 RegisterContextSP 248 OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, lldb::addr_t reg_data_addr) 249 { 250 RegisterContextSP reg_ctx_sp; 251 if (!m_interpreter || !m_python_object || !thread) 252 return RegisterContextSP(); 253 254 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 255 256 if (reg_data_addr != LLDB_INVALID_ADDRESS) 257 { 258 // The registers data is in contiguous memory, just create the register 259 // context using the address provided 260 if (log) 261 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context", thread->GetID(), reg_data_addr); 262 reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr)); 263 } 264 else 265 { 266 // No register data address is provided, query the python plug-in to let 267 // it make up the data as it sees fit 268 if (log) 269 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") fetching register data from python", thread->GetID()); 270 271 auto object_sp = m_interpreter->OSPlugin_QueryForRegisterContextData (m_interpreter->MakeScriptObject(m_python_object), 272 thread->GetID()); 273 274 if (!object_sp) 275 return RegisterContextSP(); 276 277 PythonDataString reg_context_data((PyObject*)object_sp->GetObject()); 278 if (reg_context_data) 279 { 280 DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(), 281 reg_context_data.GetSize())); 282 if (data_sp->GetByteSize()) 283 { 284 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS); 285 if (reg_ctx_memory) 286 { 287 reg_ctx_sp.reset(reg_ctx_memory); 288 reg_ctx_memory->SetAllRegisterData (data_sp); 289 } 290 } 291 } 292 } 293 return reg_ctx_sp; 294 } 295 296 StopInfoSP 297 OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread) 298 { 299 // We should have gotten the thread stop info from the dictionary of data for 300 // the thread in the initial call to get_thread_info(), this should have been 301 // cached so we can return it here 302 StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 303 return stop_info_sp; 304 } 305 306 307 #endif // #ifndef LLDB_DISABLE_PYTHON 308