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 %llu", 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 %llu", 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 206 const uint32_t num_threads = threads_array.GetSize(); 207 for (uint32_t i=0; i<num_threads; ++i) 208 { 209 PythonDataDictionary thread_dict(threads_array.GetItemAtIndex(i).GetDictionaryObject()); 210 if (thread_dict) 211 { 212 const tid_t tid = thread_dict.GetItemForKeyAsInteger(tid_pystr, LLDB_INVALID_THREAD_ID); 213 const char *name = thread_dict.GetItemForKeyAsString (name_pystr); 214 const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr); 215 //const char *state = thread_dict.GetItemForKeyAsString (state_pystr); 216 //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr); 217 218 ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false)); 219 if (!thread_sp) 220 thread_sp.reset (new ThreadMemory (*m_process, 221 tid, 222 name, 223 queue)); 224 new_thread_list.AddThread(thread_sp); 225 226 } 227 } 228 } 229 else 230 { 231 new_thread_list = old_thread_list; 232 } 233 return new_thread_list.GetSize(false) > 0; 234 } 235 236 void 237 OperatingSystemPython::ThreadWasSelected (Thread *thread) 238 { 239 } 240 241 RegisterContextSP 242 OperatingSystemPython::CreateRegisterContextForThread (Thread *thread) 243 { 244 RegisterContextSP reg_ctx_sp; 245 if (!m_interpreter || !m_python_object || !thread) 246 return RegisterContextSP(); 247 248 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 249 250 if (log) 251 log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%llx) fetching register data from python", thread->GetID()); 252 253 auto object_sp = m_interpreter->OSPlugin_QueryForRegisterContextData (m_interpreter->MakeScriptObject(m_python_object), 254 thread->GetID()); 255 256 if (!object_sp) 257 return RegisterContextSP(); 258 259 PythonDataString reg_context_data((PyObject*)object_sp->GetObject()); 260 if (reg_context_data) 261 { 262 DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(), 263 reg_context_data.GetSize())); 264 if (data_sp->GetByteSize()) 265 { 266 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS); 267 if (reg_ctx_memory) 268 { 269 reg_ctx_sp.reset(reg_ctx_memory); 270 reg_ctx_memory->SetAllRegisterData (data_sp); 271 } 272 } 273 } 274 return reg_ctx_sp; 275 } 276 277 StopInfoSP 278 OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread) 279 { 280 // We should have gotten the thread stop info from the dictionary of data for 281 // the thread in the initial call to get_thread_info(), this should have been 282 // cached so we can return it here 283 StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 284 return stop_info_sp; 285 } 286 287 288 #endif // #ifndef LLDB_DISABLE_PYTHON 289