@@ -74,47 +74,71 @@ llvm::StringRef OperatingSystemPython::GetPluginDescriptionStatic() {
74
74
OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process,
75
75
const FileSpec &python_module_path)
76
76
: OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(),
77
- m_interpreter(nullptr ), m_python_object_sp () {
77
+ m_interpreter(nullptr ), m_script_object_sp () {
78
78
if (!process)
79
79
return ;
80
80
TargetSP target_sp = process->CalculateTarget ();
81
81
if (!target_sp)
82
82
return ;
83
83
m_interpreter = target_sp->GetDebugger ().GetScriptInterpreter ();
84
- if (m_interpreter) {
85
-
86
- std::string os_plugin_class_name (
87
- python_module_path.GetFilename ().AsCString (" " ));
88
- if (!os_plugin_class_name.empty ()) {
89
- LoadScriptOptions options;
90
- char python_module_path_cstr[PATH_MAX];
91
- python_module_path.GetPath (python_module_path_cstr,
92
- sizeof (python_module_path_cstr));
93
- Status error;
94
- if (m_interpreter->LoadScriptingModule (python_module_path_cstr, options,
95
- error)) {
96
- // Strip the ".py" extension if there is one
97
- size_t py_extension_pos = os_plugin_class_name.rfind (" .py" );
98
- if (py_extension_pos != std::string::npos)
99
- os_plugin_class_name.erase (py_extension_pos);
100
- // Add ".OperatingSystemPlugIn" to the module name to get a string like
101
- // "modulename.OperatingSystemPlugIn"
102
- os_plugin_class_name += " .OperatingSystemPlugIn" ;
103
- StructuredData::ObjectSP object_sp =
104
- m_interpreter->OSPlugin_CreatePluginObject (
105
- os_plugin_class_name.c_str (), process->CalculateProcess ());
106
- if (object_sp && object_sp->IsValid ())
107
- m_python_object_sp = object_sp;
108
- }
109
- }
84
+ if (!m_interpreter)
85
+ return ;
86
+
87
+ std::string os_plugin_class_name (
88
+ python_module_path.GetFilename ().AsCString (" " ));
89
+ if (os_plugin_class_name.empty ())
90
+ return ;
91
+
92
+ LoadScriptOptions options;
93
+ char python_module_path_cstr[PATH_MAX];
94
+ python_module_path.GetPath (python_module_path_cstr,
95
+ sizeof (python_module_path_cstr));
96
+ Status error;
97
+ if (!m_interpreter->LoadScriptingModule (python_module_path_cstr, options,
98
+ error))
99
+ return ;
100
+
101
+ // Strip the ".py" extension if there is one
102
+ size_t py_extension_pos = os_plugin_class_name.rfind (" .py" );
103
+ if (py_extension_pos != std::string::npos)
104
+ os_plugin_class_name.erase (py_extension_pos);
105
+ // Add ".OperatingSystemPlugIn" to the module name to get a string like
106
+ // "modulename.OperatingSystemPlugIn"
107
+ os_plugin_class_name += " .OperatingSystemPlugIn" ;
108
+
109
+ auto operating_system_interface =
110
+ m_interpreter->CreateOperatingSystemInterface ();
111
+ if (!operating_system_interface)
112
+ // FIXME: We should pass an Status& to raise the error to the user.
113
+ // return llvm::createStringError(
114
+ // llvm::inconvertibleErrorCode(),
115
+ // "Failed to create scripted thread interface.");
116
+ return ;
117
+
118
+ ExecutionContext exe_ctx (process);
119
+ auto obj_or_err = operating_system_interface->CreatePluginObject (
120
+ os_plugin_class_name, exe_ctx, nullptr );
121
+
122
+ if (!obj_or_err) {
123
+ llvm::consumeError (obj_or_err.takeError ());
124
+ return ;
110
125
}
126
+
127
+ StructuredData::GenericSP owned_script_object_sp = *obj_or_err;
128
+ if (!owned_script_object_sp->IsValid ())
129
+ // return llvm::createStringError(llvm::inconvertibleErrorCode(),
130
+ // "Created script object is invalid.");
131
+ return ;
132
+
133
+ m_script_object_sp = owned_script_object_sp;
134
+ m_operating_system_interface_sp = operating_system_interface;
111
135
}
112
136
113
137
OperatingSystemPython::~OperatingSystemPython () = default ;
114
138
115
139
DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo () {
116
140
if (m_register_info_up == nullptr ) {
117
- if (!m_interpreter || !m_python_object_sp )
141
+ if (!m_interpreter || !m_operating_system_interface_sp )
118
142
return nullptr ;
119
143
Log *log = GetLog (LLDBLog::OS);
120
144
@@ -124,7 +148,7 @@ DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
124
148
m_process->GetID ());
125
149
126
150
StructuredData::DictionarySP dictionary =
127
- m_interpreter-> OSPlugin_RegisterInfo (m_python_object_sp );
151
+ m_operating_system_interface_sp-> GetRegisterInfo ( );
128
152
if (!dictionary)
129
153
return nullptr ;
130
154
@@ -140,27 +164,11 @@ DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
140
164
bool OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list,
141
165
ThreadList &core_thread_list,
142
166
ThreadList &new_thread_list) {
143
- if (!m_interpreter || !m_python_object_sp )
167
+ if (!m_interpreter || !m_operating_system_interface_sp )
144
168
return false ;
145
169
146
170
Log *log = GetLog (LLDBLog::OS);
147
171
148
- // First thing we have to do is to try to get the API lock, and the
149
- // interpreter lock. We're going to change the thread content of the process,
150
- // and we're going to use python, which requires the API lock to do it. We
151
- // need the interpreter lock to make sure thread_info_dict stays alive.
152
- //
153
- // If someone already has the API lock, that is ok, we just want to avoid
154
- // external code from making new API calls while this call is happening.
155
- //
156
- // This is a recursive lock so we can grant it to any Python code called on
157
- // the stack below us.
158
- Target &target = m_process->GetTarget ();
159
- std::unique_lock<std::recursive_mutex> api_lock (target.GetAPIMutex (),
160
- std::defer_lock);
161
- (void )api_lock.try_lock (); // See above.
162
- auto interpreter_lock = m_interpreter->AcquireInterpreterLock ();
163
-
164
172
LLDB_LOGF (log,
165
173
" OperatingSystemPython::UpdateThreadList() fetching thread "
166
174
" data from python for pid %" PRIu64,
@@ -170,7 +178,7 @@ bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
170
178
// the lldb_private::Process subclass, no memory threads will be in this
171
179
// list.
172
180
StructuredData::ArraySP threads_list =
173
- m_interpreter-> OSPlugin_ThreadsInfo (m_python_object_sp );
181
+ m_operating_system_interface_sp-> GetThreadInfo ( );
174
182
175
183
const uint32_t num_cores = core_thread_list.GetSize (false );
176
184
@@ -281,28 +289,12 @@ RegisterContextSP
281
289
OperatingSystemPython::CreateRegisterContextForThread (Thread *thread,
282
290
addr_t reg_data_addr) {
283
291
RegisterContextSP reg_ctx_sp;
284
- if (!m_interpreter || !m_python_object_sp || !thread)
292
+ if (!m_interpreter || !m_script_object_sp || !thread)
285
293
return reg_ctx_sp;
286
294
287
295
if (!IsOperatingSystemPluginThread (thread->shared_from_this ()))
288
296
return reg_ctx_sp;
289
297
290
- // First thing we have to do is to try to get the API lock, and the
291
- // interpreter lock. We're going to change the thread content of the process,
292
- // and we're going to use python, which requires the API lock to do it. We
293
- // need the interpreter lock to make sure thread_info_dict stays alive.
294
- //
295
- // If someone already has the API lock, that is ok, we just want to avoid
296
- // external code from making new API calls while this call is happening.
297
- //
298
- // This is a recursive lock so we can grant it to any Python code called on
299
- // the stack below us.
300
- Target &target = m_process->GetTarget ();
301
- std::unique_lock<std::recursive_mutex> api_lock (target.GetAPIMutex (),
302
- std::defer_lock);
303
- (void )api_lock.try_lock (); // See above.
304
- auto interpreter_lock = m_interpreter->AcquireInterpreterLock ();
305
-
306
298
Log *log = GetLog (LLDBLog::Thread);
307
299
308
300
if (reg_data_addr != LLDB_INVALID_ADDRESS) {
@@ -324,11 +316,11 @@ OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
324
316
" ) fetching register data from python" ,
325
317
thread->GetID (), thread->GetProtocolID ());
326
318
327
- StructuredData::StringSP reg_context_data =
328
- m_interpreter-> OSPlugin_RegisterContextData (m_python_object_sp,
329
- thread->GetID ());
319
+ std::optional<std::string> reg_context_data =
320
+ m_operating_system_interface_sp-> GetRegisterContextForTID (
321
+ thread->GetID ());
330
322
if (reg_context_data) {
331
- std::string value = std::string ( reg_context_data-> GetValue ()) ;
323
+ std::string value = * reg_context_data;
332
324
DataBufferSP data_sp (new DataBufferHeap (value.c_str (), value.length ()));
333
325
if (data_sp->GetByteSize ()) {
334
326
RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (
@@ -347,6 +339,7 @@ OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
347
339
" OperatingSystemPython::CreateRegisterContextForThread (tid "
348
340
" = 0x%" PRIx64 " ) forcing a dummy register context" ,
349
341
thread->GetID ());
342
+ Target &target = m_process->GetTarget ();
350
343
reg_ctx_sp = std::make_shared<RegisterContextDummy>(
351
344
*thread, 0 , target.GetArchitecture ().GetAddressByteSize ());
352
345
}
@@ -372,26 +365,11 @@ lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
372
365
" , context = 0x%" PRIx64 " ) fetching register data from python" ,
373
366
tid, context);
374
367
375
- if (m_interpreter && m_python_object_sp) {
376
- // First thing we have to do is to try to get the API lock, and the
377
- // interpreter lock. We're going to change the thread content of the
378
- // process, and we're going to use python, which requires the API lock to
379
- // do it. We need the interpreter lock to make sure thread_info_dict stays
380
- // alive.
381
- //
382
- // If someone already has the API lock, that is ok, we just want to avoid
383
- // external code from making new API calls while this call is happening.
384
- //
385
- // This is a recursive lock so we can grant it to any Python code called on
386
- // the stack below us.
387
- Target &target = m_process->GetTarget ();
388
- std::unique_lock<std::recursive_mutex> api_lock (target.GetAPIMutex (),
389
- std::defer_lock);
390
- (void )api_lock.try_lock (); // See above.
391
- auto interpreter_lock = m_interpreter->AcquireInterpreterLock ();
368
+ if (m_interpreter && m_script_object_sp) {
392
369
393
370
StructuredData::DictionarySP thread_info_dict =
394
- m_interpreter->OSPlugin_CreateThread (m_python_object_sp, tid, context);
371
+ m_operating_system_interface_sp->CreateThread (tid, context);
372
+
395
373
std::vector<bool > core_used_map;
396
374
if (thread_info_dict) {
397
375
ThreadList core_threads (m_process);
0 commit comments