1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
//===-- OperatingSystemPython.cpp --------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#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"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Interpreter/PythonDataObjects.h"
#include "lldb/Core/RegisterValue.h"
#include "lldb/Core/ValueObjectVariable.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/PythonDataObjects.h"
#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
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();
}
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.";
}
OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) :
OperatingSystem (process),
m_thread_list_valobj_sp (),
m_register_info_ap (),
m_interpreter (NULL),
m_python_object (NULL)
{
if (!process)
return;
lldb::TargetSP target_sp = process->CalculateTarget();
if (!target_sp)
return;
m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
if (m_interpreter)
{
std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
if (!os_plugin_class_name.empty())
{
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();
}
}
}
}
OperatingSystemPython::~OperatingSystemPython ()
{
}
DynamicRegisterInfo *
OperatingSystemPython::GetDynamicRegisterInfo ()
{
if (m_register_info_ap.get() == NULL)
{
if (!m_interpreter || !m_python_object)
return NULL;
LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
if (log)
log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %llu", m_process->GetID());
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);
}
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)
{
if (!m_interpreter || !m_python_object)
return NULL;
LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
if (log)
log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %llu", m_process->GetID());
auto object_sp = m_interpreter->OSPlugin_QueryForThreadsInfo(m_interpreter->MakeScriptObject(m_python_object));
if (!object_sp)
return NULL;
PythonDataObject pyobj((PyObject*)object_sp->GetObject());
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");
PythonDataString reg_data_addr_pystr ("register_data_addr");
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)
{
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);
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)
thread_sp.reset (new ThreadMemory (*m_process,
tid,
name,
queue,
reg_data_addr));
new_thread_list.AddThread(thread_sp);
}
}
}
else
{
new_thread_list = old_thread_list;
}
return new_thread_list.GetSize(false) > 0;
}
void
OperatingSystemPython::ThreadWasSelected (Thread *thread)
{
}
RegisterContextSP
OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, lldb::addr_t reg_data_addr)
{
RegisterContextSP reg_ctx_sp;
if (!m_interpreter || !m_python_object || !thread)
return RegisterContextSP();
LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
if (reg_data_addr != LLDB_INVALID_ADDRESS)
{
// The registers data is in contiguous memory, just create the register
// context using the address provided
if (log)
log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%llx, reg_data_addr = 0x%llx) creating memory register context", thread->GetID(), reg_data_addr);
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)
log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%llx) fetching register data from python", thread->GetID());
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)
{
DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
reg_context_data.GetSize()));
if (data_sp->GetByteSize())
{
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);
}
}
}
}
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
|