2012-08-24 05:17:11 +08:00
|
|
|
//===-- OperatingSystemPython.cpp --------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-12-05 08:20:57 +08:00
|
|
|
|
|
|
|
#include "lldb/lldb-python.h"
|
|
|
|
|
2012-08-24 05:17:11 +08:00
|
|
|
#ifndef LLDB_DISABLE_PYTHON
|
|
|
|
|
|
|
|
#include "OperatingSystemPython.h"
|
|
|
|
// C Includes
|
|
|
|
// C++ Includes
|
|
|
|
// Other libraries and framework includes
|
|
|
|
#include "lldb/Core/ArchSpec.h"
|
|
|
|
#include "lldb/Core/DataBufferHeap.h"
|
2012-08-24 08:30:47 +08:00
|
|
|
#include "lldb/Core/Debugger.h"
|
2012-08-24 05:17:11 +08:00
|
|
|
#include "lldb/Core/Module.h"
|
|
|
|
#include "lldb/Core/PluginManager.h"
|
2012-08-24 09:42:50 +08:00
|
|
|
#include "lldb/Interpreter/PythonDataObjects.h"
|
2012-08-24 05:17:11 +08:00
|
|
|
#include "lldb/Core/RegisterValue.h"
|
|
|
|
#include "lldb/Core/ValueObjectVariable.h"
|
2012-08-24 08:30:47 +08:00
|
|
|
#include "lldb/Interpreter/CommandInterpreter.h"
|
|
|
|
#include "lldb/Interpreter/PythonDataObjects.h"
|
2012-08-24 05:17:11 +08:00
|
|
|
#include "lldb/Symbol/ClangNamespaceDecl.h"
|
|
|
|
#include "lldb/Symbol/ObjectFile.h"
|
|
|
|
#include "lldb/Symbol/VariableList.h"
|
|
|
|
#include "lldb/Target/Process.h"
|
|
|
|
#include "lldb/Target/StopInfo.h"
|
|
|
|
#include "lldb/Target/Target.h"
|
|
|
|
#include "lldb/Target/ThreadList.h"
|
|
|
|
#include "lldb/Target/Thread.h"
|
|
|
|
#include "Plugins/Process/Utility/DynamicRegisterInfo.h"
|
|
|
|
#include "Plugins/Process/Utility/RegisterContextMemory.h"
|
|
|
|
#include "Plugins/Process/Utility/ThreadMemory.h"
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
void
|
|
|
|
OperatingSystemPython::Initialize()
|
|
|
|
{
|
|
|
|
PluginManager::RegisterPlugin (GetPluginNameStatic(),
|
|
|
|
GetPluginDescriptionStatic(),
|
|
|
|
CreateInstance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OperatingSystemPython::Terminate()
|
|
|
|
{
|
|
|
|
PluginManager::UnregisterPlugin (CreateInstance);
|
|
|
|
}
|
|
|
|
|
|
|
|
OperatingSystem *
|
|
|
|
OperatingSystemPython::CreateInstance (Process *process, bool force)
|
|
|
|
{
|
|
|
|
// Python OperatingSystem plug-ins must be requested by name, so force must be true
|
2012-10-19 06:40:37 +08:00
|
|
|
FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath());
|
|
|
|
if (python_os_plugin_spec && python_os_plugin_spec.Exists())
|
|
|
|
{
|
|
|
|
std::auto_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec));
|
|
|
|
if (os_ap.get() && os_ap->IsValid())
|
|
|
|
return os_ap.release();
|
|
|
|
}
|
2012-08-24 05:17:11 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
OperatingSystemPython::GetPluginNameStatic()
|
|
|
|
{
|
|
|
|
return "python";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
OperatingSystemPython::GetPluginDescriptionStatic()
|
|
|
|
{
|
|
|
|
return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality.";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-19 06:40:37 +08:00
|
|
|
OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) :
|
2012-08-24 05:17:11 +08:00
|
|
|
OperatingSystem (process),
|
|
|
|
m_thread_list_valobj_sp (),
|
2012-08-24 08:30:47 +08:00
|
|
|
m_register_info_ap (),
|
2012-10-19 06:40:37 +08:00
|
|
|
m_interpreter (NULL),
|
|
|
|
m_python_object (NULL)
|
2012-08-24 05:17:11 +08:00
|
|
|
{
|
2012-08-24 08:30:47 +08:00
|
|
|
if (!process)
|
|
|
|
return;
|
|
|
|
lldb::TargetSP target_sp = process->CalculateTarget();
|
|
|
|
if (!target_sp)
|
|
|
|
return;
|
|
|
|
m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
|
|
|
|
if (m_interpreter)
|
|
|
|
{
|
2012-10-19 06:40:37 +08:00
|
|
|
|
|
|
|
std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
|
|
|
|
if (!os_plugin_class_name.empty())
|
2012-08-24 09:42:50 +08:00
|
|
|
{
|
2012-10-19 06:40:37 +08:00
|
|
|
const bool init_session = false;
|
|
|
|
const bool allow_reload = true;
|
|
|
|
char python_module_path_cstr[PATH_MAX];
|
|
|
|
python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr));
|
|
|
|
Error error;
|
|
|
|
if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error))
|
|
|
|
{
|
|
|
|
// Strip the ".py" extension if there is one
|
|
|
|
size_t py_extension_pos = os_plugin_class_name.rfind(".py");
|
|
|
|
if (py_extension_pos != std::string::npos)
|
|
|
|
os_plugin_class_name.erase (py_extension_pos);
|
|
|
|
// Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn"
|
|
|
|
os_plugin_class_name += ".OperatingSystemPlugIn";
|
|
|
|
auto object_sp = m_interpreter->CreateOSPlugin(os_plugin_class_name.c_str(), process->CalculateProcess());
|
|
|
|
if (object_sp)
|
|
|
|
m_python_object = object_sp->GetObject();
|
|
|
|
}
|
2012-08-24 09:42:50 +08:00
|
|
|
}
|
2012-08-24 08:30:47 +08:00
|
|
|
}
|
2012-08-24 05:17:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OperatingSystemPython::~OperatingSystemPython ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicRegisterInfo *
|
|
|
|
OperatingSystemPython::GetDynamicRegisterInfo ()
|
|
|
|
{
|
2012-08-24 09:42:50 +08:00
|
|
|
if (m_register_info_ap.get() == NULL)
|
2012-08-24 05:17:11 +08:00
|
|
|
{
|
2012-08-24 09:42:50 +08:00
|
|
|
if (!m_interpreter || !m_python_object)
|
|
|
|
return NULL;
|
2012-10-25 02:39:14 +08:00
|
|
|
LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
|
|
|
|
|
|
|
|
if (log)
|
2012-11-30 05:49:15 +08:00
|
|
|
log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID());
|
2012-10-25 02:39:14 +08:00
|
|
|
|
2012-08-24 09:42:50 +08:00
|
|
|
auto object_sp = m_interpreter->OSPlugin_QueryForRegisterInfo(m_interpreter->MakeScriptObject(m_python_object));
|
|
|
|
if (!object_sp)
|
|
|
|
return NULL;
|
|
|
|
PythonDataObject dictionary_data_obj((PyObject*)object_sp->GetObject());
|
|
|
|
PythonDataDictionary dictionary = dictionary_data_obj.GetDictionaryObject();
|
|
|
|
if (!dictionary)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
m_register_info_ap.reset (new DynamicRegisterInfo (dictionary));
|
|
|
|
assert (m_register_info_ap->GetNumRegisters() > 0);
|
|
|
|
assert (m_register_info_ap->GetNumRegisterSets() > 0);
|
2012-08-24 05:17:11 +08:00
|
|
|
}
|
|
|
|
return m_register_info_ap.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// PluginInterface protocol
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
const char *
|
|
|
|
OperatingSystemPython::GetPluginName()
|
|
|
|
{
|
|
|
|
return "OperatingSystemPython";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
OperatingSystemPython::GetShortPluginName()
|
|
|
|
{
|
|
|
|
return GetPluginNameStatic();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
OperatingSystemPython::GetPluginVersion()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
|
|
|
|
{
|
2012-08-24 08:51:29 +08:00
|
|
|
if (!m_interpreter || !m_python_object)
|
|
|
|
return NULL;
|
2012-10-25 02:39:14 +08:00
|
|
|
|
|
|
|
LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
|
|
|
|
|
2012-12-08 01:42:15 +08:00
|
|
|
// First thing we have to do is get the API lock, and the run lock. We're going to change the thread
|
|
|
|
// content of the process, and we're going to use python, which requires the API lock to do it.
|
|
|
|
// So get & hold that. This is a recursive lock so we can grant it to any Python code called on the stack below us.
|
|
|
|
Target &target = m_process->GetTarget();
|
|
|
|
Mutex::Locker api_locker (target.GetAPIMutex());
|
|
|
|
|
2012-10-25 02:39:14 +08:00
|
|
|
if (log)
|
2012-11-30 05:49:15 +08:00
|
|
|
log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID());
|
2012-10-25 02:39:14 +08:00
|
|
|
|
2012-08-24 08:51:29 +08:00
|
|
|
auto object_sp = m_interpreter->OSPlugin_QueryForThreadsInfo(m_interpreter->MakeScriptObject(m_python_object));
|
|
|
|
if (!object_sp)
|
|
|
|
return NULL;
|
2012-08-24 10:01:39 +08:00
|
|
|
PythonDataObject pyobj((PyObject*)object_sp->GetObject());
|
2012-08-24 13:45:15 +08:00
|
|
|
PythonDataArray threads_array (pyobj.GetArrayObject());
|
|
|
|
if (threads_array)
|
|
|
|
{
|
|
|
|
// const uint32_t num_old_threads = old_thread_list.GetSize(false);
|
|
|
|
// for (uint32_t i=0; i<num_old_threads; ++i)
|
|
|
|
// {
|
|
|
|
// ThreadSP old_thread_sp(old_thread_list.GetThreadAtIndex(i, false));
|
|
|
|
// if (old_thread_sp->GetID() < 0x10000)
|
|
|
|
// new_thread_list.AddThread (old_thread_sp);
|
|
|
|
// }
|
|
|
|
|
|
|
|
PythonDataString tid_pystr("tid");
|
|
|
|
PythonDataString name_pystr("name");
|
|
|
|
PythonDataString queue_pystr("queue");
|
|
|
|
PythonDataString state_pystr("state");
|
|
|
|
PythonDataString stop_reason_pystr("stop_reason");
|
2012-10-26 01:56:31 +08:00
|
|
|
PythonDataString reg_data_addr_pystr ("register_data_addr");
|
2012-08-24 13:45:15 +08:00
|
|
|
|
|
|
|
const uint32_t num_threads = threads_array.GetSize();
|
|
|
|
for (uint32_t i=0; i<num_threads; ++i)
|
|
|
|
{
|
|
|
|
PythonDataDictionary thread_dict(threads_array.GetItemAtIndex(i).GetDictionaryObject());
|
|
|
|
if (thread_dict)
|
|
|
|
{
|
2012-10-26 01:56:31 +08:00
|
|
|
const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID);
|
|
|
|
const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS);
|
2012-08-24 13:45:15 +08:00
|
|
|
const char *name = thread_dict.GetItemForKeyAsString (name_pystr);
|
|
|
|
const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr);
|
|
|
|
//const char *state = thread_dict.GetItemForKeyAsString (state_pystr);
|
|
|
|
//const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr);
|
|
|
|
|
|
|
|
ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false));
|
|
|
|
if (!thread_sp)
|
2012-10-11 02:32:14 +08:00
|
|
|
thread_sp.reset (new ThreadMemory (*m_process,
|
2012-08-24 13:45:15 +08:00
|
|
|
tid,
|
|
|
|
name,
|
2012-10-26 01:56:31 +08:00
|
|
|
queue,
|
|
|
|
reg_data_addr));
|
2012-08-24 13:45:15 +08:00
|
|
|
new_thread_list.AddThread(thread_sp);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_thread_list = old_thread_list;
|
|
|
|
}
|
2012-08-24 05:17:11 +08:00
|
|
|
return new_thread_list.GetSize(false) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OperatingSystemPython::ThreadWasSelected (Thread *thread)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RegisterContextSP
|
2012-10-26 01:56:31 +08:00
|
|
|
OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, lldb::addr_t reg_data_addr)
|
2012-08-24 05:17:11 +08:00
|
|
|
{
|
2012-08-24 13:45:15 +08:00
|
|
|
RegisterContextSP reg_ctx_sp;
|
2012-08-24 08:51:29 +08:00
|
|
|
if (!m_interpreter || !m_python_object || !thread)
|
2012-08-28 21:59:38 +08:00
|
|
|
return RegisterContextSP();
|
2012-10-25 02:39:14 +08:00
|
|
|
|
2012-12-08 01:42:15 +08:00
|
|
|
// First thing we have to do is get the API lock, and the run lock. We're going to change the thread
|
|
|
|
// content of the process, and we're going to use python, which requires the API lock to do it.
|
|
|
|
// So get & hold that. This is a recursive lock so we can grant it to any Python code called on the stack below us.
|
|
|
|
Target &target = m_process->GetTarget();
|
|
|
|
Mutex::Locker api_locker (target.GetAPIMutex());
|
|
|
|
|
2012-10-25 02:39:14 +08:00
|
|
|
LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
|
|
|
|
|
2012-10-26 01:56:31 +08:00
|
|
|
if (reg_data_addr != LLDB_INVALID_ADDRESS)
|
2012-08-24 13:45:15 +08:00
|
|
|
{
|
2012-10-26 01:56:31 +08:00
|
|
|
// The registers data is in contiguous memory, just create the register
|
|
|
|
// context using the address provided
|
|
|
|
if (log)
|
2012-11-30 05:49:15 +08:00
|
|
|
log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context", thread->GetID(), reg_data_addr);
|
2012-10-26 01:56:31 +08:00
|
|
|
reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No register data address is provided, query the python plug-in to let
|
|
|
|
// it make up the data as it sees fit
|
|
|
|
if (log)
|
2012-11-30 05:49:15 +08:00
|
|
|
log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") fetching register data from python", thread->GetID());
|
2012-10-26 01:56:31 +08:00
|
|
|
|
|
|
|
auto object_sp = m_interpreter->OSPlugin_QueryForRegisterContextData (m_interpreter->MakeScriptObject(m_python_object),
|
|
|
|
thread->GetID());
|
|
|
|
|
|
|
|
if (!object_sp)
|
|
|
|
return RegisterContextSP();
|
|
|
|
|
|
|
|
PythonDataString reg_context_data((PyObject*)object_sp->GetObject());
|
|
|
|
if (reg_context_data)
|
2012-08-24 13:45:15 +08:00
|
|
|
{
|
2012-10-26 01:56:31 +08:00
|
|
|
DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
|
|
|
|
reg_context_data.GetSize()));
|
|
|
|
if (data_sp->GetByteSize())
|
2012-08-24 13:45:15 +08:00
|
|
|
{
|
2012-10-26 01:56:31 +08:00
|
|
|
RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
|
|
|
|
if (reg_ctx_memory)
|
|
|
|
{
|
|
|
|
reg_ctx_sp.reset(reg_ctx_memory);
|
|
|
|
reg_ctx_memory->SetAllRegisterData (data_sp);
|
|
|
|
}
|
2012-08-24 13:45:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-24 05:17:11 +08:00
|
|
|
return reg_ctx_sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
StopInfoSP
|
|
|
|
OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
|
|
|
|
{
|
|
|
|
// We should have gotten the thread stop info from the dictionary of data for
|
|
|
|
// the thread in the initial call to get_thread_info(), this should have been
|
|
|
|
// cached so we can return it here
|
|
|
|
StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
|
|
|
|
return stop_info_sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif // #ifndef LLDB_DISABLE_PYTHON
|