aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp
blob: ce262c930f8b749d7a9a9abca9dbae1c330dd4dd (plain) (blame)
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
305
306
//===-- ScriptedProcessPythonInterface.cpp --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/Config.h"
#include "lldb/lldb-enumerations.h"

#if LLDB_ENABLE_PYTHON

// LLDB Python header must be included first
#include "lldb-python.h"

#include "SWIGPythonBridge.h"
#include "ScriptInterpreterPythonImpl.h"
#include "ScriptedProcessPythonInterface.h"

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::python;
using Locker = ScriptInterpreterPythonImpl::Locker;

StructuredData::GenericSP ScriptedProcessPythonInterface::CreatePluginObject(
    const llvm::StringRef class_name, lldb::TargetSP target_sp,
    StructuredData::DictionarySP args_sp) {
  if (class_name.empty())
    return {};

  std::string error_string;
  StructuredDataImpl *args_impl = nullptr;
  if (args_sp) {
    args_impl = new StructuredDataImpl();
    args_impl->SetObjectSP(args_sp);
  }

  void *ret_val;

  {

    Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
                   Locker::FreeLock);

    ret_val = LLDBSwigPythonCreateScriptedProcess(
        class_name.str().c_str(), m_interpreter.GetDictionaryName(), target_sp,
        args_impl, error_string);
  }

  m_object_instance_sp =
      StructuredData::GenericSP(new StructuredPythonObject(ret_val));

  return m_object_instance_sp;
}

Status ScriptedProcessPythonInterface::Launch() {
  return GetStatusFromMethod("launch");
}

Status ScriptedProcessPythonInterface::Resume() {
  return GetStatusFromMethod("resume");
}

bool ScriptedProcessPythonInterface::ShouldStop() {
  llvm::Optional<unsigned long long> should_stop =
      GetGenericInteger("should_stop");

  if (!should_stop)
    return false;

  return static_cast<bool>(*should_stop);
}

Status ScriptedProcessPythonInterface::Stop() {
  return GetStatusFromMethod("stop");
}

Status ScriptedProcessPythonInterface::GetStatusFromMethod(
    llvm::StringRef method_name) {
  Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
                 Locker::FreeLock);

  if (!m_object_instance_sp)
    return Status("Python object ill-formed.");

  if (!m_object_instance_sp)
    return Status("Cannot convert Python object to StructuredData::Generic.");
  PythonObject implementor(PyRefType::Borrowed,
                           (PyObject *)m_object_instance_sp->GetValue());

  if (!implementor.IsAllocated())
    return Status("Python implementor not allocated.");

  PythonObject pmeth(
      PyRefType::Owned,
      PyObject_GetAttrString(implementor.get(), method_name.str().c_str()));

  if (PyErr_Occurred())
    PyErr_Clear();

  if (!pmeth.IsAllocated())
    return Status("Python method not allocated.");

  if (PyCallable_Check(pmeth.get()) == 0) {
    if (PyErr_Occurred())
      PyErr_Clear();
    return Status("Python method not callable.");
  }

  if (PyErr_Occurred())
    PyErr_Clear();

  PythonObject py_return(PyRefType::Owned,
                         PyObject_CallMethod(implementor.get(),
                                             method_name.str().c_str(),
                                             nullptr));

  if (PyErr_Occurred()) {
    PyErr_Print();
    PyErr_Clear();
    return Status("Python method could not be called.");
  }

  if (PyObject *py_ret_ptr = py_return.get()) {
    lldb::SBError *sb_error =
        (lldb::SBError *)LLDBSWIGPython_CastPyObjectToSBError(py_ret_ptr);

    if (!sb_error)
      return Status("Couldn't cast lldb::SBError to lldb::Status.");

    Status status = m_interpreter.GetStatusFromSBError(*sb_error);

    if (status.Fail())
      return Status("error: %s", status.AsCString());

    return status;
  }

  return Status("Returned object is null.");
}

llvm::Optional<unsigned long long>
ScriptedProcessPythonInterface::GetGenericInteger(llvm::StringRef method_name) {
  Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
                 Locker::FreeLock);

  if (!m_object_instance_sp)
    return llvm::None;

  if (!m_object_instance_sp)
    return llvm::None;
  PythonObject implementor(PyRefType::Borrowed,
                           (PyObject *)m_object_instance_sp->GetValue());

  if (!implementor.IsAllocated())
    return llvm::None;

  PythonObject pmeth(
      PyRefType::Owned,
      PyObject_GetAttrString(implementor.get(), method_name.str().c_str()));

  if (PyErr_Occurred())
    PyErr_Clear();

  if (!pmeth.IsAllocated())
    return llvm::None;

  if (PyCallable_Check(pmeth.get()) == 0) {
    if (PyErr_Occurred())
      PyErr_Clear();
    return llvm::None;
  }

  if (PyErr_Occurred())
    PyErr_Clear();

  PythonObject py_return(PyRefType::Owned,
                         PyObject_CallMethod(implementor.get(),
                                             method_name.str().c_str(),
                                             nullptr));

  if (PyErr_Occurred()) {
    PyErr_Print();
    PyErr_Clear();
  }

  if (!py_return.get())
    return llvm::None;

  llvm::Expected<unsigned long long> size = py_return.AsUnsignedLongLong();
  // FIXME: Handle error.
  if (!size)
    return llvm::None;

  return *size;
}

lldb::MemoryRegionInfoSP
ScriptedProcessPythonInterface::GetMemoryRegionContainingAddress(
    lldb::addr_t address) {
  // TODO: Implement
  return nullptr;
}

StructuredData::DictionarySP
ScriptedProcessPythonInterface::GetThreadWithID(lldb::tid_t tid) {
  // TODO: Implement
  return nullptr;
}

StructuredData::DictionarySP
ScriptedProcessPythonInterface::GetRegistersForThread(lldb::tid_t tid) {
  // TODO: Implement
  return nullptr;
}

lldb::DataExtractorSP ScriptedProcessPythonInterface::ReadMemoryAtAddress(
    lldb::addr_t address, size_t size, Status &error) {
  Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
                 Locker::FreeLock);

  auto error_with_message = [&error](llvm::StringRef message) {
    error.SetErrorString(message);
    return nullptr;
  };

  static char callee_name[] = "read_memory_at_address";
  std::string param_format = GetPythonValueFormatString(address);
  param_format += GetPythonValueFormatString(size);

  if (!m_object_instance_sp)
    return error_with_message("Python object ill-formed.");

  if (!m_object_instance_sp)
    return error_with_message("Python method not callable.");

  PythonObject implementor(PyRefType::Borrowed,
                           (PyObject *)m_object_instance_sp->GetValue());

  if (!implementor.IsAllocated())
    return error_with_message("Python implementor not allocated.");

  PythonObject pmeth(PyRefType::Owned,
                     PyObject_GetAttrString(implementor.get(), callee_name));

  if (PyErr_Occurred())
    PyErr_Clear();

  if (!pmeth.IsAllocated())
    return error_with_message("Python method not allocated.");

  if (PyCallable_Check(pmeth.get()) == 0) {
    if (PyErr_Occurred())
      PyErr_Clear();
    return error_with_message("Python method not callable.");
  }

  if (PyErr_Occurred())
    PyErr_Clear();

  PythonObject py_return(PyRefType::Owned,
                         PyObject_CallMethod(implementor.get(), callee_name,
                                             param_format.c_str(), address,
                                             size));

  if (PyErr_Occurred()) {
    PyErr_Print();
    PyErr_Clear();
    return error_with_message("Python method could not be called.");
  }

  if (PyObject *py_ret_ptr = py_return.get()) {
    lldb::SBData *sb_data =
        (lldb::SBData *)LLDBSWIGPython_CastPyObjectToSBData(py_ret_ptr);

    if (!sb_data)
      return error_with_message(
          "Couldn't cast lldb::SBData to lldb::DataExtractor.");

    return m_interpreter.GetDataExtractorFromSBData(*sb_data);
  }

  return error_with_message("Returned object is null.");
}

StructuredData::DictionarySP ScriptedProcessPythonInterface::GetLoadedImages() {
  // TODO: Implement
  return nullptr;
}

lldb::pid_t ScriptedProcessPythonInterface::GetProcessID() {
  llvm::Optional<unsigned long long> pid = GetGenericInteger("get_process_id");
  return (!pid) ? LLDB_INVALID_PROCESS_ID : *pid;
}

bool ScriptedProcessPythonInterface::IsAlive() {
  llvm::Optional<unsigned long long> is_alive = GetGenericInteger("is_alive");

  if (!is_alive)
    return false;

  return static_cast<bool>(*is_alive);
}

#endif