aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2019-12-20 19:53:05 +0000
committerDimitry Andric <dim@FreeBSD.org>2019-12-20 19:53:05 +0000
commit0b57cec536236d46e3dba9bd041533462f33dbb7 (patch)
tree56229dbdbbf76d18580f72f789003db17246c8d9 /contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python
parent718ef55ec7785aae63f98f8ca05dc07ed399c16d (diff)
Notes
Diffstat (limited to 'contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python')
-rw-r--r--contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp1052
-rw-r--r--contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h479
-rw-r--r--contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.cpp169
-rw-r--r--contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.h56
-rw-r--r--contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp3259
-rw-r--r--contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h57
-rw-r--r--contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h473
-rw-r--r--contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h45
8 files changed, 0 insertions, 5590 deletions
diff --git a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
deleted file mode 100644
index 29dd037efd86..000000000000
--- a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ /dev/null
@@ -1,1052 +0,0 @@
-//===-- PythonDataObjects.cpp -----------------------------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifdef LLDB_DISABLE_PYTHON
-
-// Python is disabled in this build
-
-#else
-
-#include "PythonDataObjects.h"
-#include "ScriptInterpreterPython.h"
-
-#include "lldb/Host/File.h"
-#include "lldb/Host/FileSystem.h"
-#include "lldb/Interpreter/ScriptInterpreter.h"
-#include "lldb/Utility/Stream.h"
-
-#include "llvm/ADT/StringSwitch.h"
-#include "llvm/Support/ConvertUTF.h"
-#include "llvm/Support/Errno.h"
-
-#include <stdio.h>
-
-using namespace lldb_private;
-using namespace lldb;
-
-void StructuredPythonObject::Dump(Stream &s, bool pretty_print) const {
- s << "Python Obj: 0x" << GetValue();
-}
-
-// PythonObject
-
-void PythonObject::Dump(Stream &strm) const {
- if (m_py_obj) {
- FILE *file = llvm::sys::RetryAfterSignal(nullptr, ::tmpfile);
- if (file) {
- ::PyObject_Print(m_py_obj, file, 0);
- const long length = ftell(file);
- if (length) {
- ::rewind(file);
- std::vector<char> file_contents(length, '\0');
- const size_t length_read =
- ::fread(file_contents.data(), 1, file_contents.size(), file);
- if (length_read > 0)
- strm.Write(file_contents.data(), length_read);
- }
- ::fclose(file);
- }
- } else
- strm.PutCString("NULL");
-}
-
-PyObjectType PythonObject::GetObjectType() const {
- if (!IsAllocated())
- return PyObjectType::None;
-
- if (PythonModule::Check(m_py_obj))
- return PyObjectType::Module;
- if (PythonList::Check(m_py_obj))
- return PyObjectType::List;
- if (PythonTuple::Check(m_py_obj))
- return PyObjectType::Tuple;
- if (PythonDictionary::Check(m_py_obj))
- return PyObjectType::Dictionary;
- if (PythonString::Check(m_py_obj))
- return PyObjectType::String;
-#if PY_MAJOR_VERSION >= 3
- if (PythonBytes::Check(m_py_obj))
- return PyObjectType::Bytes;
-#endif
- if (PythonByteArray::Check(m_py_obj))
- return PyObjectType::ByteArray;
- if (PythonBoolean::Check(m_py_obj))
- return PyObjectType::Boolean;
- if (PythonInteger::Check(m_py_obj))
- return PyObjectType::Integer;
- if (PythonFile::Check(m_py_obj))
- return PyObjectType::File;
- if (PythonCallable::Check(m_py_obj))
- return PyObjectType::Callable;
- return PyObjectType::Unknown;
-}
-
-PythonString PythonObject::Repr() const {
- if (!m_py_obj)
- return PythonString();
- PyObject *repr = PyObject_Repr(m_py_obj);
- if (!repr)
- return PythonString();
- return PythonString(PyRefType::Owned, repr);
-}
-
-PythonString PythonObject::Str() const {
- if (!m_py_obj)
- return PythonString();
- PyObject *str = PyObject_Str(m_py_obj);
- if (!str)
- return PythonString();
- return PythonString(PyRefType::Owned, str);
-}
-
-PythonObject
-PythonObject::ResolveNameWithDictionary(llvm::StringRef name,
- const PythonDictionary &dict) {
- size_t dot_pos = name.find('.');
- llvm::StringRef piece = name.substr(0, dot_pos);
- PythonObject result = dict.GetItemForKey(PythonString(piece));
- if (dot_pos == llvm::StringRef::npos) {
- // There was no dot, we're done.
- return result;
- }
-
- // There was a dot. The remaining portion of the name should be looked up in
- // the context of the object that was found in the dictionary.
- return result.ResolveName(name.substr(dot_pos + 1));
-}
-
-PythonObject PythonObject::ResolveName(llvm::StringRef name) const {
- // Resolve the name in the context of the specified object. If, for example,
- // `this` refers to a PyModule, then this will look for `name` in this
- // module. If `this` refers to a PyType, then it will resolve `name` as an
- // attribute of that type. If `this` refers to an instance of an object,
- // then it will resolve `name` as the value of the specified field.
- //
- // This function handles dotted names so that, for example, if `m_py_obj`
- // refers to the `sys` module, and `name` == "path.append", then it will find
- // the function `sys.path.append`.
-
- size_t dot_pos = name.find('.');
- if (dot_pos == llvm::StringRef::npos) {
- // No dots in the name, we should be able to find the value immediately as
- // an attribute of `m_py_obj`.
- return GetAttributeValue(name);
- }
-
- // Look up the first piece of the name, and resolve the rest as a child of
- // that.
- PythonObject parent = ResolveName(name.substr(0, dot_pos));
- if (!parent.IsAllocated())
- return PythonObject();
-
- // Tail recursion.. should be optimized by the compiler
- return parent.ResolveName(name.substr(dot_pos + 1));
-}
-
-bool PythonObject::HasAttribute(llvm::StringRef attr) const {
- if (!IsValid())
- return false;
- PythonString py_attr(attr);
- return !!PyObject_HasAttr(m_py_obj, py_attr.get());
-}
-
-PythonObject PythonObject::GetAttributeValue(llvm::StringRef attr) const {
- if (!IsValid())
- return PythonObject();
-
- PythonString py_attr(attr);
- if (!PyObject_HasAttr(m_py_obj, py_attr.get()))
- return PythonObject();
-
- return PythonObject(PyRefType::Owned,
- PyObject_GetAttr(m_py_obj, py_attr.get()));
-}
-
-bool PythonObject::IsNone() const { return m_py_obj == Py_None; }
-
-bool PythonObject::IsValid() const { return m_py_obj != nullptr; }
-
-bool PythonObject::IsAllocated() const { return IsValid() && !IsNone(); }
-
-StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
- switch (GetObjectType()) {
- case PyObjectType::Dictionary:
- return PythonDictionary(PyRefType::Borrowed, m_py_obj)
- .CreateStructuredDictionary();
- case PyObjectType::Boolean:
- return PythonBoolean(PyRefType::Borrowed, m_py_obj)
- .CreateStructuredBoolean();
- case PyObjectType::Integer:
- return PythonInteger(PyRefType::Borrowed, m_py_obj)
- .CreateStructuredInteger();
- case PyObjectType::List:
- return PythonList(PyRefType::Borrowed, m_py_obj).CreateStructuredArray();
- case PyObjectType::String:
- return PythonString(PyRefType::Borrowed, m_py_obj).CreateStructuredString();
- case PyObjectType::Bytes:
- return PythonBytes(PyRefType::Borrowed, m_py_obj).CreateStructuredString();
- case PyObjectType::ByteArray:
- return PythonByteArray(PyRefType::Borrowed, m_py_obj)
- .CreateStructuredString();
- case PyObjectType::None:
- return StructuredData::ObjectSP();
- default:
- return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj));
- }
-}
-
-// PythonString
-PythonBytes::PythonBytes() : PythonObject() {}
-
-PythonBytes::PythonBytes(llvm::ArrayRef<uint8_t> bytes) : PythonObject() {
- SetBytes(bytes);
-}
-
-PythonBytes::PythonBytes(const uint8_t *bytes, size_t length) : PythonObject() {
- SetBytes(llvm::ArrayRef<uint8_t>(bytes, length));
-}
-
-PythonBytes::PythonBytes(PyRefType type, PyObject *py_obj) : PythonObject() {
- Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
-}
-
-PythonBytes::~PythonBytes() {}
-
-bool PythonBytes::Check(PyObject *py_obj) {
- if (!py_obj)
- return false;
- return PyBytes_Check(py_obj);
-}
-
-void PythonBytes::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonBytes::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-llvm::ArrayRef<uint8_t> PythonBytes::GetBytes() const {
- if (!IsValid())
- return llvm::ArrayRef<uint8_t>();
-
- Py_ssize_t size;
- char *c;
-
- PyBytes_AsStringAndSize(m_py_obj, &c, &size);
- return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size);
-}
-
-size_t PythonBytes::GetSize() const {
- if (!IsValid())
- return 0;
- return PyBytes_Size(m_py_obj);
-}
-
-void PythonBytes::SetBytes(llvm::ArrayRef<uint8_t> bytes) {
- const char *data = reinterpret_cast<const char *>(bytes.data());
- PyObject *py_bytes = PyBytes_FromStringAndSize(data, bytes.size());
- PythonObject::Reset(PyRefType::Owned, py_bytes);
-}
-
-StructuredData::StringSP PythonBytes::CreateStructuredString() const {
- StructuredData::StringSP result(new StructuredData::String);
- Py_ssize_t size;
- char *c;
- PyBytes_AsStringAndSize(m_py_obj, &c, &size);
- result->SetValue(std::string(c, size));
- return result;
-}
-
-PythonByteArray::PythonByteArray(llvm::ArrayRef<uint8_t> bytes)
- : PythonByteArray(bytes.data(), bytes.size()) {}
-
-PythonByteArray::PythonByteArray(const uint8_t *bytes, size_t length) {
- const char *str = reinterpret_cast<const char *>(bytes);
- Reset(PyRefType::Owned, PyByteArray_FromStringAndSize(str, length));
-}
-
-PythonByteArray::PythonByteArray(PyRefType type, PyObject *o) {
- Reset(type, o);
-}
-
-PythonByteArray::PythonByteArray(const PythonBytes &object)
- : PythonObject(object) {}
-
-PythonByteArray::~PythonByteArray() {}
-
-bool PythonByteArray::Check(PyObject *py_obj) {
- if (!py_obj)
- return false;
- return PyByteArray_Check(py_obj);
-}
-
-void PythonByteArray::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonByteArray::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-llvm::ArrayRef<uint8_t> PythonByteArray::GetBytes() const {
- if (!IsValid())
- return llvm::ArrayRef<uint8_t>();
-
- char *c = PyByteArray_AsString(m_py_obj);
- size_t size = GetSize();
- return llvm::ArrayRef<uint8_t>(reinterpret_cast<uint8_t *>(c), size);
-}
-
-size_t PythonByteArray::GetSize() const {
- if (!IsValid())
- return 0;
-
- return PyByteArray_Size(m_py_obj);
-}
-
-StructuredData::StringSP PythonByteArray::CreateStructuredString() const {
- StructuredData::StringSP result(new StructuredData::String);
- llvm::ArrayRef<uint8_t> bytes = GetBytes();
- const char *str = reinterpret_cast<const char *>(bytes.data());
- result->SetValue(std::string(str, bytes.size()));
- return result;
-}
-
-// PythonString
-
-PythonString::PythonString(PyRefType type, PyObject *py_obj) : PythonObject() {
- Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a string
-}
-
-PythonString::PythonString(llvm::StringRef string) : PythonObject() {
- SetString(string);
-}
-
-PythonString::PythonString(const char *string) : PythonObject() {
- SetString(llvm::StringRef(string));
-}
-
-PythonString::PythonString() : PythonObject() {}
-
-PythonString::~PythonString() {}
-
-bool PythonString::Check(PyObject *py_obj) {
- if (!py_obj)
- return false;
-
- if (PyUnicode_Check(py_obj))
- return true;
-#if PY_MAJOR_VERSION < 3
- if (PyString_Check(py_obj))
- return true;
-#endif
- return false;
-}
-
-void PythonString::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonString::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-#if PY_MAJOR_VERSION < 3
- // In Python 2, Don't store PyUnicode objects directly, because we need
- // access to their underlying character buffers which Python 2 doesn't
- // provide.
- if (PyUnicode_Check(py_obj))
- result.Reset(PyRefType::Owned, PyUnicode_AsUTF8String(result.get()));
-#endif
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-llvm::StringRef PythonString::GetString() const {
- if (!IsValid())
- return llvm::StringRef();
-
- Py_ssize_t size;
- const char *data;
-
-#if PY_MAJOR_VERSION >= 3
- data = PyUnicode_AsUTF8AndSize(m_py_obj, &size);
-#else
- char *c;
- PyString_AsStringAndSize(m_py_obj, &c, &size);
- data = c;
-#endif
- return llvm::StringRef(data, size);
-}
-
-size_t PythonString::GetSize() const {
- if (IsValid()) {
-#if PY_MAJOR_VERSION >= 3
- return PyUnicode_GetSize(m_py_obj);
-#else
- return PyString_Size(m_py_obj);
-#endif
- }
- return 0;
-}
-
-void PythonString::SetString(llvm::StringRef string) {
-#if PY_MAJOR_VERSION >= 3
- PyObject *unicode = PyUnicode_FromStringAndSize(string.data(), string.size());
- PythonObject::Reset(PyRefType::Owned, unicode);
-#else
- PyObject *str = PyString_FromStringAndSize(string.data(), string.size());
- PythonObject::Reset(PyRefType::Owned, str);
-#endif
-}
-
-StructuredData::StringSP PythonString::CreateStructuredString() const {
- StructuredData::StringSP result(new StructuredData::String);
- result->SetValue(GetString());
- return result;
-}
-
-// PythonInteger
-
-PythonInteger::PythonInteger() : PythonObject() {}
-
-PythonInteger::PythonInteger(PyRefType type, PyObject *py_obj)
- : PythonObject() {
- Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a integer type
-}
-
-PythonInteger::PythonInteger(int64_t value) : PythonObject() {
- SetInteger(value);
-}
-
-PythonInteger::~PythonInteger() {}
-
-bool PythonInteger::Check(PyObject *py_obj) {
- if (!py_obj)
- return false;
-
-#if PY_MAJOR_VERSION >= 3
- // Python 3 does not have PyInt_Check. There is only one type of integral
- // value, long.
- return PyLong_Check(py_obj);
-#else
- return PyLong_Check(py_obj) || PyInt_Check(py_obj);
-#endif
-}
-
-void PythonInteger::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonInteger::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-
-#if PY_MAJOR_VERSION < 3
- // Always store this as a PyLong, which makes interoperability between Python
- // 2.x and Python 3.x easier. This is only necessary in 2.x, since 3.x
- // doesn't even have a PyInt.
- if (PyInt_Check(py_obj)) {
- // Since we converted the original object to a different type, the new
- // object is an owned object regardless of the ownership semantics
- // requested by the user.
- result.Reset(PyRefType::Owned, PyLong_FromLongLong(PyInt_AsLong(py_obj)));
- }
-#endif
-
- assert(PyLong_Check(result.get()) &&
- "Couldn't get a PyLong from this PyObject");
-
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-int64_t PythonInteger::GetInteger() const {
- if (m_py_obj) {
- assert(PyLong_Check(m_py_obj) &&
- "PythonInteger::GetInteger has a PyObject that isn't a PyLong");
-
- int overflow = 0;
- int64_t result = PyLong_AsLongLongAndOverflow(m_py_obj, &overflow);
- if (overflow != 0) {
- // We got an integer that overflows, like 18446744072853913392L we can't
- // use PyLong_AsLongLong() as it will return 0xffffffffffffffff. If we
- // use the unsigned long long it will work as expected.
- const uint64_t uval = PyLong_AsUnsignedLongLong(m_py_obj);
- result = static_cast<int64_t>(uval);
- }
- return result;
- }
- return UINT64_MAX;
-}
-
-void PythonInteger::SetInteger(int64_t value) {
- PythonObject::Reset(PyRefType::Owned, PyLong_FromLongLong(value));
-}
-
-StructuredData::IntegerSP PythonInteger::CreateStructuredInteger() const {
- StructuredData::IntegerSP result(new StructuredData::Integer);
- result->SetValue(GetInteger());
- return result;
-}
-
-// PythonBoolean
-
-PythonBoolean::PythonBoolean(PyRefType type, PyObject *py_obj)
- : PythonObject() {
- Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a boolean type
-}
-
-PythonBoolean::PythonBoolean(bool value) {
- SetValue(value);
-}
-
-bool PythonBoolean::Check(PyObject *py_obj) {
- return py_obj ? PyBool_Check(py_obj) : false;
-}
-
-void PythonBoolean::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonBoolean::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-bool PythonBoolean::GetValue() const {
- return m_py_obj ? PyObject_IsTrue(m_py_obj) : false;
-}
-
-void PythonBoolean::SetValue(bool value) {
- PythonObject::Reset(PyRefType::Owned, PyBool_FromLong(value));
-}
-
-StructuredData::BooleanSP PythonBoolean::CreateStructuredBoolean() const {
- StructuredData::BooleanSP result(new StructuredData::Boolean);
- result->SetValue(GetValue());
- return result;
-}
-
-// PythonList
-
-PythonList::PythonList(PyInitialValue value) : PythonObject() {
- if (value == PyInitialValue::Empty)
- Reset(PyRefType::Owned, PyList_New(0));
-}
-
-PythonList::PythonList(int list_size) : PythonObject() {
- Reset(PyRefType::Owned, PyList_New(list_size));
-}
-
-PythonList::PythonList(PyRefType type, PyObject *py_obj) : PythonObject() {
- Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a list
-}
-
-PythonList::~PythonList() {}
-
-bool PythonList::Check(PyObject *py_obj) {
- if (!py_obj)
- return false;
- return PyList_Check(py_obj);
-}
-
-void PythonList::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonList::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-uint32_t PythonList::GetSize() const {
- if (IsValid())
- return PyList_GET_SIZE(m_py_obj);
- return 0;
-}
-
-PythonObject PythonList::GetItemAtIndex(uint32_t index) const {
- if (IsValid())
- return PythonObject(PyRefType::Borrowed, PyList_GetItem(m_py_obj, index));
- return PythonObject();
-}
-
-void PythonList::SetItemAtIndex(uint32_t index, const PythonObject &object) {
- if (IsAllocated() && object.IsValid()) {
- // PyList_SetItem is documented to "steal" a reference, so we need to
- // convert it to an owned reference by incrementing it.
- Py_INCREF(object.get());
- PyList_SetItem(m_py_obj, index, object.get());
- }
-}
-
-void PythonList::AppendItem(const PythonObject &object) {
- if (IsAllocated() && object.IsValid()) {
- // `PyList_Append` does *not* steal a reference, so do not call `Py_INCREF`
- // here like we do with `PyList_SetItem`.
- PyList_Append(m_py_obj, object.get());
- }
-}
-
-StructuredData::ArraySP PythonList::CreateStructuredArray() const {
- StructuredData::ArraySP result(new StructuredData::Array);
- uint32_t count = GetSize();
- for (uint32_t i = 0; i < count; ++i) {
- PythonObject obj = GetItemAtIndex(i);
- result->AddItem(obj.CreateStructuredObject());
- }
- return result;
-}
-
-// PythonTuple
-
-PythonTuple::PythonTuple(PyInitialValue value) : PythonObject() {
- if (value == PyInitialValue::Empty)
- Reset(PyRefType::Owned, PyTuple_New(0));
-}
-
-PythonTuple::PythonTuple(int tuple_size) : PythonObject() {
- Reset(PyRefType::Owned, PyTuple_New(tuple_size));
-}
-
-PythonTuple::PythonTuple(PyRefType type, PyObject *py_obj) : PythonObject() {
- Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a tuple
-}
-
-PythonTuple::PythonTuple(std::initializer_list<PythonObject> objects) {
- m_py_obj = PyTuple_New(objects.size());
-
- uint32_t idx = 0;
- for (auto object : objects) {
- if (object.IsValid())
- SetItemAtIndex(idx, object);
- idx++;
- }
-}
-
-PythonTuple::PythonTuple(std::initializer_list<PyObject *> objects) {
- m_py_obj = PyTuple_New(objects.size());
-
- uint32_t idx = 0;
- for (auto py_object : objects) {
- PythonObject object(PyRefType::Borrowed, py_object);
- if (object.IsValid())
- SetItemAtIndex(idx, object);
- idx++;
- }
-}
-
-PythonTuple::~PythonTuple() {}
-
-bool PythonTuple::Check(PyObject *py_obj) {
- if (!py_obj)
- return false;
- return PyTuple_Check(py_obj);
-}
-
-void PythonTuple::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonTuple::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-uint32_t PythonTuple::GetSize() const {
- if (IsValid())
- return PyTuple_GET_SIZE(m_py_obj);
- return 0;
-}
-
-PythonObject PythonTuple::GetItemAtIndex(uint32_t index) const {
- if (IsValid())
- return PythonObject(PyRefType::Borrowed, PyTuple_GetItem(m_py_obj, index));
- return PythonObject();
-}
-
-void PythonTuple::SetItemAtIndex(uint32_t index, const PythonObject &object) {
- if (IsAllocated() && object.IsValid()) {
- // PyTuple_SetItem is documented to "steal" a reference, so we need to
- // convert it to an owned reference by incrementing it.
- Py_INCREF(object.get());
- PyTuple_SetItem(m_py_obj, index, object.get());
- }
-}
-
-StructuredData::ArraySP PythonTuple::CreateStructuredArray() const {
- StructuredData::ArraySP result(new StructuredData::Array);
- uint32_t count = GetSize();
- for (uint32_t i = 0; i < count; ++i) {
- PythonObject obj = GetItemAtIndex(i);
- result->AddItem(obj.CreateStructuredObject());
- }
- return result;
-}
-
-// PythonDictionary
-
-PythonDictionary::PythonDictionary(PyInitialValue value) : PythonObject() {
- if (value == PyInitialValue::Empty)
- Reset(PyRefType::Owned, PyDict_New());
-}
-
-PythonDictionary::PythonDictionary(PyRefType type, PyObject *py_obj)
- : PythonObject() {
- Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a dictionary
-}
-
-PythonDictionary::~PythonDictionary() {}
-
-bool PythonDictionary::Check(PyObject *py_obj) {
- if (!py_obj)
- return false;
-
- return PyDict_Check(py_obj);
-}
-
-void PythonDictionary::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonDictionary::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-uint32_t PythonDictionary::GetSize() const {
- if (IsValid())
- return PyDict_Size(m_py_obj);
- return 0;
-}
-
-PythonList PythonDictionary::GetKeys() const {
- if (IsValid())
- return PythonList(PyRefType::Owned, PyDict_Keys(m_py_obj));
- return PythonList(PyInitialValue::Invalid);
-}
-
-PythonObject PythonDictionary::GetItemForKey(const PythonObject &key) const {
- if (IsAllocated() && key.IsValid())
- return PythonObject(PyRefType::Borrowed,
- PyDict_GetItem(m_py_obj, key.get()));
- return PythonObject();
-}
-
-void PythonDictionary::SetItemForKey(const PythonObject &key,
- const PythonObject &value) {
- if (IsAllocated() && key.IsValid() && value.IsValid())
- PyDict_SetItem(m_py_obj, key.get(), value.get());
-}
-
-StructuredData::DictionarySP
-PythonDictionary::CreateStructuredDictionary() const {
- StructuredData::DictionarySP result(new StructuredData::Dictionary);
- PythonList keys(GetKeys());
- uint32_t num_keys = keys.GetSize();
- for (uint32_t i = 0; i < num_keys; ++i) {
- PythonObject key = keys.GetItemAtIndex(i);
- PythonObject value = GetItemForKey(key);
- StructuredData::ObjectSP structured_value = value.CreateStructuredObject();
- result->AddItem(key.Str().GetString(), structured_value);
- }
- return result;
-}
-
-PythonModule::PythonModule() : PythonObject() {}
-
-PythonModule::PythonModule(PyRefType type, PyObject *py_obj) {
- Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a module
-}
-
-PythonModule::~PythonModule() {}
-
-PythonModule PythonModule::BuiltinsModule() {
-#if PY_MAJOR_VERSION >= 3
- return AddModule("builtins");
-#else
- return AddModule("__builtin__");
-#endif
-}
-
-PythonModule PythonModule::MainModule() { return AddModule("__main__"); }
-
-PythonModule PythonModule::AddModule(llvm::StringRef module) {
- std::string str = module.str();
- return PythonModule(PyRefType::Borrowed, PyImport_AddModule(str.c_str()));
-}
-
-PythonModule PythonModule::ImportModule(llvm::StringRef module) {
- std::string str = module.str();
- return PythonModule(PyRefType::Owned, PyImport_ImportModule(str.c_str()));
-}
-
-bool PythonModule::Check(PyObject *py_obj) {
- if (!py_obj)
- return false;
-
- return PyModule_Check(py_obj);
-}
-
-void PythonModule::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonModule::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-PythonDictionary PythonModule::GetDictionary() const {
- return PythonDictionary(PyRefType::Borrowed, PyModule_GetDict(m_py_obj));
-}
-
-PythonCallable::PythonCallable() : PythonObject() {}
-
-PythonCallable::PythonCallable(PyRefType type, PyObject *py_obj) {
- Reset(type, py_obj); // Use "Reset()" to ensure that py_obj is a callable
-}
-
-PythonCallable::~PythonCallable() {}
-
-bool PythonCallable::Check(PyObject *py_obj) {
- if (!py_obj)
- return false;
-
- return PyCallable_Check(py_obj);
-}
-
-void PythonCallable::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonCallable::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-PythonCallable::ArgInfo PythonCallable::GetNumArguments() const {
- ArgInfo result = {0, false, false, false};
- if (!IsValid())
- return result;
-
- PyObject *py_func_obj = m_py_obj;
- if (PyMethod_Check(py_func_obj)) {
- py_func_obj = PyMethod_GET_FUNCTION(py_func_obj);
- PythonObject im_self = GetAttributeValue("im_self");
- if (im_self.IsValid() && !im_self.IsNone())
- result.is_bound_method = true;
- } else {
- // see if this is a callable object with an __call__ method
- if (!PyFunction_Check(py_func_obj)) {
- PythonObject __call__ = GetAttributeValue("__call__");
- if (__call__.IsValid()) {
- auto __callable__ = __call__.AsType<PythonCallable>();
- if (__callable__.IsValid()) {
- py_func_obj = PyMethod_GET_FUNCTION(__callable__.get());
- PythonObject im_self = GetAttributeValue("im_self");
- if (im_self.IsValid() && !im_self.IsNone())
- result.is_bound_method = true;
- }
- }
- }
- }
-
- if (!py_func_obj)
- return result;
-
- PyCodeObject *code = (PyCodeObject *)PyFunction_GET_CODE(py_func_obj);
- if (!code)
- return result;
-
- result.count = code->co_argcount;
- result.has_varargs = !!(code->co_flags & CO_VARARGS);
- result.has_kwargs = !!(code->co_flags & CO_VARKEYWORDS);
- return result;
-}
-
-PythonObject PythonCallable::operator()() {
- return PythonObject(PyRefType::Owned, PyObject_CallObject(m_py_obj, nullptr));
-}
-
-PythonObject PythonCallable::
-operator()(std::initializer_list<PyObject *> args) {
- PythonTuple arg_tuple(args);
- return PythonObject(PyRefType::Owned,
- PyObject_CallObject(m_py_obj, arg_tuple.get()));
-}
-
-PythonObject PythonCallable::
-operator()(std::initializer_list<PythonObject> args) {
- PythonTuple arg_tuple(args);
- return PythonObject(PyRefType::Owned,
- PyObject_CallObject(m_py_obj, arg_tuple.get()));
-}
-
-PythonFile::PythonFile() : PythonObject() {}
-
-PythonFile::PythonFile(File &file, const char *mode) { Reset(file, mode); }
-
-PythonFile::PythonFile(const char *path, const char *mode) {
- lldb_private::File file;
- FileSystem::Instance().Open(file, FileSpec(path), GetOptionsFromMode(mode));
- Reset(file, mode);
-}
-
-PythonFile::PythonFile(PyRefType type, PyObject *o) { Reset(type, o); }
-
-PythonFile::~PythonFile() {}
-
-bool PythonFile::Check(PyObject *py_obj) {
-#if PY_MAJOR_VERSION < 3
- return PyFile_Check(py_obj);
-#else
- // In Python 3, there is no `PyFile_Check`, and in fact PyFile is not even a
- // first-class object type anymore. `PyFile_FromFd` is just a thin wrapper
- // over `io.open()`, which returns some object derived from `io.IOBase`. As a
- // result, the only way to detect a file in Python 3 is to check whether it
- // inherits from `io.IOBase`. Since it is possible for non-files to also
- // inherit from `io.IOBase`, we additionally verify that it has the `fileno`
- // attribute, which should guarantee that it is backed by the file system.
- PythonObject io_module(PyRefType::Owned, PyImport_ImportModule("io"));
- PythonDictionary io_dict(PyRefType::Borrowed,
- PyModule_GetDict(io_module.get()));
- PythonObject io_base_class = io_dict.GetItemForKey(PythonString("IOBase"));
-
- PythonObject object_type(PyRefType::Owned, PyObject_Type(py_obj));
-
- if (1 != PyObject_IsSubclass(object_type.get(), io_base_class.get()))
- return false;
- if (!object_type.HasAttribute("fileno"))
- return false;
-
- return true;
-#endif
-}
-
-void PythonFile::Reset(PyRefType type, PyObject *py_obj) {
- // Grab the desired reference type so that if we end up rejecting `py_obj` it
- // still gets decremented if necessary.
- PythonObject result(type, py_obj);
-
- if (!PythonFile::Check(py_obj)) {
- PythonObject::Reset();
- return;
- }
-
- // Calling PythonObject::Reset(const PythonObject&) will lead to stack
- // overflow since it calls back into the virtual implementation.
- PythonObject::Reset(PyRefType::Borrowed, result.get());
-}
-
-void PythonFile::Reset(File &file, const char *mode) {
- if (!file.IsValid()) {
- Reset();
- return;
- }
-
- char *cmode = const_cast<char *>(mode);
-#if PY_MAJOR_VERSION >= 3
- Reset(PyRefType::Owned, PyFile_FromFd(file.GetDescriptor(), nullptr, cmode,
- -1, nullptr, "ignore", nullptr, 0));
-#else
- // Read through the Python source, doesn't seem to modify these strings
- Reset(PyRefType::Owned,
- PyFile_FromFile(file.GetStream(), const_cast<char *>(""), cmode,
- nullptr));
-#endif
-}
-
-uint32_t PythonFile::GetOptionsFromMode(llvm::StringRef mode) {
- if (mode.empty())
- return 0;
-
- return llvm::StringSwitch<uint32_t>(mode.str())
- .Case("r", File::eOpenOptionRead)
- .Case("w", File::eOpenOptionWrite)
- .Case("a", File::eOpenOptionWrite | File::eOpenOptionAppend |
- File::eOpenOptionCanCreate)
- .Case("r+", File::eOpenOptionRead | File::eOpenOptionWrite)
- .Case("w+", File::eOpenOptionRead | File::eOpenOptionWrite |
- File::eOpenOptionCanCreate | File::eOpenOptionTruncate)
- .Case("a+", File::eOpenOptionRead | File::eOpenOptionWrite |
- File::eOpenOptionAppend | File::eOpenOptionCanCreate)
- .Default(0);
-}
-
-bool PythonFile::GetUnderlyingFile(File &file) const {
- if (!IsValid())
- return false;
-
- file.Close();
- // We don't own the file descriptor returned by this function, make sure the
- // File object knows about that.
- file.SetDescriptor(PyObject_AsFileDescriptor(m_py_obj), false);
- PythonString py_mode = GetAttributeValue("mode").AsType<PythonString>();
- file.SetOptions(PythonFile::GetOptionsFromMode(py_mode.GetString()));
- return file.IsValid();
-}
-
-#endif
diff --git a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h b/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
deleted file mode 100644
index 049ce90bb1b1..000000000000
--- a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
+++ /dev/null
@@ -1,479 +0,0 @@
-//===-- PythonDataObjects.h--------------------------------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
-#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
-
-#ifndef LLDB_DISABLE_PYTHON
-
-// LLDB Python header must be included first
-#include "lldb-python.h"
-
-#include "lldb/Host/File.h"
-#include "lldb/Utility/StructuredData.h"
-
-#include "llvm/ADT/ArrayRef.h"
-
-namespace lldb_private {
-
-class PythonBytes;
-class PythonString;
-class PythonList;
-class PythonDictionary;
-class PythonInteger;
-
-class StructuredPythonObject : public StructuredData::Generic {
-public:
- StructuredPythonObject() : StructuredData::Generic() {}
-
- StructuredPythonObject(void *obj) : StructuredData::Generic(obj) {
- Py_XINCREF(GetValue());
- }
-
- ~StructuredPythonObject() override {
- if (Py_IsInitialized())
- Py_XDECREF(GetValue());
- SetValue(nullptr);
- }
-
- bool IsValid() const override { return GetValue() && GetValue() != Py_None; }
-
- void Dump(Stream &s, bool pretty_print = true) const override;
-
-private:
- DISALLOW_COPY_AND_ASSIGN(StructuredPythonObject);
-};
-
-enum class PyObjectType {
- Unknown,
- None,
- Boolean,
- Integer,
- Dictionary,
- List,
- String,
- Bytes,
- ByteArray,
- Module,
- Callable,
- Tuple,
- File
-};
-
-enum class PyRefType {
- Borrowed, // We are not given ownership of the incoming PyObject.
- // We cannot safely hold it without calling Py_INCREF.
- Owned // We have ownership of the incoming PyObject. We should
- // not call Py_INCREF.
-};
-
-enum class PyInitialValue { Invalid, Empty };
-
-class PythonObject {
-public:
- PythonObject() : m_py_obj(nullptr) {}
-
- PythonObject(PyRefType type, PyObject *py_obj) : m_py_obj(nullptr) {
- Reset(type, py_obj);
- }
-
- PythonObject(const PythonObject &rhs) : m_py_obj(nullptr) { Reset(rhs); }
-
- virtual ~PythonObject() { Reset(); }
-
- void Reset() {
- // Avoid calling the virtual method since it's not necessary
- // to actually validate the type of the PyObject if we're
- // just setting to null.
- if (Py_IsInitialized())
- Py_XDECREF(m_py_obj);
- m_py_obj = nullptr;
- }
-
- void Reset(const PythonObject &rhs) {
- // Avoid calling the virtual method if it's not necessary
- // to actually validate the type of the PyObject.
- if (!rhs.IsValid())
- Reset();
- else
- Reset(PyRefType::Borrowed, rhs.m_py_obj);
- }
-
- // PythonObject is implicitly convertible to PyObject *, which will call the
- // wrong overload. We want to explicitly disallow this, since a PyObject
- // *always* owns its reference. Therefore the overload which takes a
- // PyRefType doesn't make sense, and the copy constructor should be used.
- void Reset(PyRefType type, const PythonObject &ref) = delete;
-
- virtual void Reset(PyRefType type, PyObject *py_obj) {
- if (py_obj == m_py_obj)
- return;
-
- if (Py_IsInitialized())
- Py_XDECREF(m_py_obj);
-
- m_py_obj = py_obj;
-
- // If this is a borrowed reference, we need to convert it to
- // an owned reference by incrementing it. If it is an owned
- // reference (for example the caller allocated it with PyDict_New()
- // then we must *not* increment it.
- if (Py_IsInitialized() && type == PyRefType::Borrowed)
- Py_XINCREF(m_py_obj);
- }
-
- void Dump() const {
- if (m_py_obj)
- _PyObject_Dump(m_py_obj);
- else
- puts("NULL");
- }
-
- void Dump(Stream &strm) const;
-
- PyObject *get() const { return m_py_obj; }
-
- PyObject *release() {
- PyObject *result = m_py_obj;
- m_py_obj = nullptr;
- return result;
- }
-
- PythonObject &operator=(const PythonObject &other) {
- Reset(PyRefType::Borrowed, other.get());
- return *this;
- }
-
- PyObjectType GetObjectType() const;
-
- PythonString Repr() const;
-
- PythonString Str() const;
-
- static PythonObject ResolveNameWithDictionary(llvm::StringRef name,
- const PythonDictionary &dict);
-
- template <typename T>
- static T ResolveNameWithDictionary(llvm::StringRef name,
- const PythonDictionary &dict) {
- return ResolveNameWithDictionary(name, dict).AsType<T>();
- }
-
- PythonObject ResolveName(llvm::StringRef name) const;
-
- template <typename T> T ResolveName(llvm::StringRef name) const {
- return ResolveName(name).AsType<T>();
- }
-
- bool HasAttribute(llvm::StringRef attribute) const;
-
- PythonObject GetAttributeValue(llvm::StringRef attribute) const;
-
- bool IsValid() const;
-
- bool IsAllocated() const;
-
- bool IsNone() const;
-
- template <typename T> T AsType() const {
- if (!T::Check(m_py_obj))
- return T();
- return T(PyRefType::Borrowed, m_py_obj);
- }
-
- StructuredData::ObjectSP CreateStructuredObject() const;
-
-protected:
- PyObject *m_py_obj;
-};
-
-class PythonBytes : public PythonObject {
-public:
- PythonBytes();
- explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes);
- PythonBytes(const uint8_t *bytes, size_t length);
- PythonBytes(PyRefType type, PyObject *o);
-
- ~PythonBytes() override;
-
- static bool Check(PyObject *py_obj);
-
- // Bring in the no-argument base class version
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
-
- llvm::ArrayRef<uint8_t> GetBytes() const;
-
- size_t GetSize() const;
-
- void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
-
- StructuredData::StringSP CreateStructuredString() const;
-};
-
-class PythonByteArray : public PythonObject {
-public:
- PythonByteArray();
- explicit PythonByteArray(llvm::ArrayRef<uint8_t> bytes);
- PythonByteArray(const uint8_t *bytes, size_t length);
- PythonByteArray(PyRefType type, PyObject *o);
- PythonByteArray(const PythonBytes &object);
-
- ~PythonByteArray() override;
-
- static bool Check(PyObject *py_obj);
-
- // Bring in the no-argument base class version
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
-
- llvm::ArrayRef<uint8_t> GetBytes() const;
-
- size_t GetSize() const;
-
- void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
-
- StructuredData::StringSP CreateStructuredString() const;
-};
-
-class PythonString : public PythonObject {
-public:
- PythonString();
- explicit PythonString(llvm::StringRef string);
- explicit PythonString(const char *string);
- PythonString(PyRefType type, PyObject *o);
-
- ~PythonString() override;
-
- static bool Check(PyObject *py_obj);
-
- // Bring in the no-argument base class version
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
-
- llvm::StringRef GetString() const;
-
- size_t GetSize() const;
-
- void SetString(llvm::StringRef string);
-
- StructuredData::StringSP CreateStructuredString() const;
-};
-
-class PythonInteger : public PythonObject {
-public:
- PythonInteger();
- explicit PythonInteger(int64_t value);
- PythonInteger(PyRefType type, PyObject *o);
-
- ~PythonInteger() override;
-
- static bool Check(PyObject *py_obj);
-
- // Bring in the no-argument base class version
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
-
- int64_t GetInteger() const;
-
- void SetInteger(int64_t value);
-
- StructuredData::IntegerSP CreateStructuredInteger() const;
-};
-
-class PythonBoolean : public PythonObject {
-public:
- PythonBoolean() = default;
- explicit PythonBoolean(bool value);
- PythonBoolean(PyRefType type, PyObject *o);
-
- ~PythonBoolean() override = default;
-
- static bool Check(PyObject *py_obj);
-
- // Bring in the no-argument base class version
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
-
- bool GetValue() const;
-
- void SetValue(bool value);
-
- StructuredData::BooleanSP CreateStructuredBoolean() const;
-};
-
-class PythonList : public PythonObject {
-public:
- PythonList() {}
- explicit PythonList(PyInitialValue value);
- explicit PythonList(int list_size);
- PythonList(PyRefType type, PyObject *o);
-
- ~PythonList() override;
-
- static bool Check(PyObject *py_obj);
-
- // Bring in the no-argument base class version
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
-
- uint32_t GetSize() const;
-
- PythonObject GetItemAtIndex(uint32_t index) const;
-
- void SetItemAtIndex(uint32_t index, const PythonObject &object);
-
- void AppendItem(const PythonObject &object);
-
- StructuredData::ArraySP CreateStructuredArray() const;
-};
-
-class PythonTuple : public PythonObject {
-public:
- PythonTuple() {}
- explicit PythonTuple(PyInitialValue value);
- explicit PythonTuple(int tuple_size);
- PythonTuple(PyRefType type, PyObject *o);
- PythonTuple(std::initializer_list<PythonObject> objects);
- PythonTuple(std::initializer_list<PyObject *> objects);
-
- ~PythonTuple() override;
-
- static bool Check(PyObject *py_obj);
-
- // Bring in the no-argument base class version
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
-
- uint32_t GetSize() const;
-
- PythonObject GetItemAtIndex(uint32_t index) const;
-
- void SetItemAtIndex(uint32_t index, const PythonObject &object);
-
- StructuredData::ArraySP CreateStructuredArray() const;
-};
-
-class PythonDictionary : public PythonObject {
-public:
- PythonDictionary() {}
- explicit PythonDictionary(PyInitialValue value);
- PythonDictionary(PyRefType type, PyObject *o);
-
- ~PythonDictionary() override;
-
- static bool Check(PyObject *py_obj);
-
- // Bring in the no-argument base class version
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
-
- uint32_t GetSize() const;
-
- PythonList GetKeys() const;
-
- PythonObject GetItemForKey(const PythonObject &key) const;
- void SetItemForKey(const PythonObject &key, const PythonObject &value);
-
- StructuredData::DictionarySP CreateStructuredDictionary() const;
-};
-
-class PythonModule : public PythonObject {
-public:
- PythonModule();
- PythonModule(PyRefType type, PyObject *o);
-
- ~PythonModule() override;
-
- static bool Check(PyObject *py_obj);
-
- static PythonModule BuiltinsModule();
-
- static PythonModule MainModule();
-
- static PythonModule AddModule(llvm::StringRef module);
-
- static PythonModule ImportModule(llvm::StringRef module);
-
- // Bring in the no-argument base class version
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
-
- PythonDictionary GetDictionary() const;
-};
-
-class PythonCallable : public PythonObject {
-public:
- struct ArgInfo {
- size_t count;
- bool is_bound_method : 1;
- bool has_varargs : 1;
- bool has_kwargs : 1;
- };
-
- PythonCallable();
- PythonCallable(PyRefType type, PyObject *o);
-
- ~PythonCallable() override;
-
- static bool Check(PyObject *py_obj);
-
- // Bring in the no-argument base class version
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
-
- ArgInfo GetNumArguments() const;
-
- PythonObject operator()();
-
- PythonObject operator()(std::initializer_list<PyObject *> args);
-
- PythonObject operator()(std::initializer_list<PythonObject> args);
-
- template <typename Arg, typename... Args>
- PythonObject operator()(const Arg &arg, Args... args) {
- return operator()({arg, args...});
- }
-};
-
-class PythonFile : public PythonObject {
-public:
- PythonFile();
- PythonFile(File &file, const char *mode);
- PythonFile(const char *path, const char *mode);
- PythonFile(PyRefType type, PyObject *o);
-
- ~PythonFile() override;
-
- static bool Check(PyObject *py_obj);
-
- using PythonObject::Reset;
-
- void Reset(PyRefType type, PyObject *py_obj) override;
- void Reset(File &file, const char *mode);
-
- static uint32_t GetOptionsFromMode(llvm::StringRef mode);
-
- bool GetUnderlyingFile(File &file) const;
-};
-
-} // namespace lldb_private
-
-#endif
-
-#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
diff --git a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.cpp b/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.cpp
deleted file mode 100644
index c9d834ce6868..000000000000
--- a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.cpp
+++ /dev/null
@@ -1,169 +0,0 @@
-//===-- PythonExceptionState.cpp --------------------------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLDB_DISABLE_PYTHON
-
-// LLDB Python header must be included first
-#include "lldb-python.h"
-
-#include "PythonExceptionState.h"
-
-#include "llvm/ADT/StringRef.h"
-#include "llvm/Support/raw_ostream.h"
-
-using namespace lldb_private;
-
-PythonExceptionState::PythonExceptionState(bool restore_on_exit)
- : m_restore_on_exit(restore_on_exit) {
- Acquire(restore_on_exit);
-}
-
-PythonExceptionState::~PythonExceptionState() {
- if (m_restore_on_exit)
- Restore();
-}
-
-void PythonExceptionState::Acquire(bool restore_on_exit) {
- // If a state is already acquired, the user needs to decide whether they want
- // to discard or restore it. Don't allow the potential silent loss of a
- // valid state.
- assert(!IsError());
-
- if (!HasErrorOccurred())
- return;
-
- PyObject *py_type = nullptr;
- PyObject *py_value = nullptr;
- PyObject *py_traceback = nullptr;
- PyErr_Fetch(&py_type, &py_value, &py_traceback);
- // PyErr_Fetch clears the error flag.
- assert(!HasErrorOccurred());
-
- // Ownership of the objects returned by `PyErr_Fetch` is transferred to us.
- m_type.Reset(PyRefType::Owned, py_type);
- m_value.Reset(PyRefType::Owned, py_value);
- m_traceback.Reset(PyRefType::Owned, py_traceback);
- m_restore_on_exit = restore_on_exit;
-}
-
-void PythonExceptionState::Restore() {
- if (m_type.IsValid()) {
- // The documentation for PyErr_Restore says "Do not pass a null type and
- // non-null value or traceback. So only restore if type was non-null to
- // begin with. In this case we're passing ownership back to Python so
- // release them all.
- PyErr_Restore(m_type.release(), m_value.release(), m_traceback.release());
- }
-
- // After we restore, we should not hold onto the exception state. Demand
- // that it be re-acquired.
- Discard();
-}
-
-void PythonExceptionState::Discard() {
- m_type.Reset();
- m_value.Reset();
- m_traceback.Reset();
-}
-
-void PythonExceptionState::Reset() {
- if (m_restore_on_exit)
- Restore();
- else
- Discard();
-}
-
-bool PythonExceptionState::HasErrorOccurred() { return PyErr_Occurred(); }
-
-bool PythonExceptionState::IsError() const {
- return m_type.IsValid() || m_value.IsValid() || m_traceback.IsValid();
-}
-
-PythonObject PythonExceptionState::GetType() const { return m_type; }
-
-PythonObject PythonExceptionState::GetValue() const { return m_value; }
-
-PythonObject PythonExceptionState::GetTraceback() const { return m_traceback; }
-
-std::string PythonExceptionState::Format() const {
- // Don't allow this function to modify the error state.
- PythonExceptionState state(true);
-
- std::string backtrace = ReadBacktrace();
- if (!IsError())
- return std::string();
-
- // It's possible that ReadPythonBacktrace generated another exception. If
- // this happens we have to clear the exception, because otherwise
- // PyObject_Str() will assert below. That's why we needed to do the save /
- // restore at the beginning of this function.
- PythonExceptionState bt_error_state(false);
-
- std::string error_string;
- llvm::raw_string_ostream error_stream(error_string);
- error_stream << m_value.Str().GetString() << "\n";
-
- if (!bt_error_state.IsError()) {
- // If we were able to read the backtrace, just append it.
- error_stream << backtrace << "\n";
- } else {
- // Otherwise, append some information about why we were unable to obtain
- // the backtrace.
- PythonString bt_error = bt_error_state.GetValue().Str();
- error_stream << "An error occurred while retrieving the backtrace: "
- << bt_error.GetString() << "\n";
- }
- return error_stream.str();
-}
-
-std::string PythonExceptionState::ReadBacktrace() const {
- std::string retval("backtrace unavailable");
-
- auto traceback_module = PythonModule::ImportModule("traceback");
-#if PY_MAJOR_VERSION >= 3
- auto stringIO_module = PythonModule::ImportModule("io");
-#else
- auto stringIO_module = PythonModule::ImportModule("StringIO");
-#endif
- if (!m_traceback.IsAllocated())
- return retval;
-
- if (!traceback_module.IsAllocated() || !stringIO_module.IsAllocated())
- return retval;
-
- auto stringIO_builder =
- stringIO_module.ResolveName<PythonCallable>("StringIO");
- if (!stringIO_builder.IsAllocated())
- return retval;
-
- auto stringIO_buffer = stringIO_builder();
- if (!stringIO_buffer.IsAllocated())
- return retval;
-
- auto printTB = traceback_module.ResolveName<PythonCallable>("print_tb");
- if (!printTB.IsAllocated())
- return retval;
-
- auto printTB_result =
- printTB(m_traceback.get(), Py_None, stringIO_buffer.get());
- auto stringIO_getvalue =
- stringIO_buffer.ResolveName<PythonCallable>("getvalue");
- if (!stringIO_getvalue.IsAllocated())
- return retval;
-
- auto printTB_string = stringIO_getvalue().AsType<PythonString>();
- if (!printTB_string.IsAllocated())
- return retval;
-
- llvm::StringRef string_data(printTB_string.GetString());
- retval.assign(string_data.data(), string_data.size());
-
- return retval;
-}
-
-#endif
diff --git a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.h b/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.h
deleted file mode 100644
index 3a88aa037776..000000000000
--- a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/PythonExceptionState.h
+++ /dev/null
@@ -1,56 +0,0 @@
-//===-- PythonExceptionState.h ----------------------------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONEXCEPTIONSTATE_H
-#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONEXCEPTIONSTATE_H
-
-#ifndef LLDB_DISABLE_PYTHON
-
-#include "PythonDataObjects.h"
-
-namespace lldb_private {
-
-class PythonExceptionState {
-public:
- explicit PythonExceptionState(bool restore_on_exit);
- ~PythonExceptionState();
-
- void Acquire(bool restore_on_exit);
-
- void Restore();
-
- void Discard();
-
- void Reset();
-
- static bool HasErrorOccurred();
-
- bool IsError() const;
-
- PythonObject GetType() const;
-
- PythonObject GetValue() const;
-
- PythonObject GetTraceback() const;
-
- std::string Format() const;
-
-private:
- std::string ReadBacktrace() const;
-
- bool m_restore_on_exit;
-
- PythonObject m_type;
- PythonObject m_value;
- PythonObject m_traceback;
-};
-}
-
-#endif
-
-#endif
diff --git a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
deleted file mode 100644
index 2d2b68ceaaa6..000000000000
--- a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ /dev/null
@@ -1,3259 +0,0 @@
-//===-- ScriptInterpreterPython.cpp -----------------------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifdef LLDB_DISABLE_PYTHON
-
-// Python is disabled in this build
-
-#else
-
-// LLDB Python header must be included first
-#include "lldb-python.h"
-
-#include "PythonDataObjects.h"
-#include "PythonExceptionState.h"
-#include "ScriptInterpreterPythonImpl.h"
-
-#include "lldb/API/SBFrame.h"
-#include "lldb/API/SBValue.h"
-#include "lldb/Breakpoint/StoppointCallbackContext.h"
-#include "lldb/Breakpoint/WatchpointOptions.h"
-#include "lldb/Core/Communication.h"
-#include "lldb/Core/Debugger.h"
-#include "lldb/Core/PluginManager.h"
-#include "lldb/Core/ValueObject.h"
-#include "lldb/DataFormatters/TypeSummary.h"
-#include "lldb/Host/ConnectionFileDescriptor.h"
-#include "lldb/Host/FileSystem.h"
-#include "lldb/Host/HostInfo.h"
-#include "lldb/Host/Pipe.h"
-#include "lldb/Interpreter/CommandInterpreter.h"
-#include "lldb/Interpreter/CommandReturnObject.h"
-#include "lldb/Target/Thread.h"
-#include "lldb/Target/ThreadPlan.h"
-#include "lldb/Utility/Timer.h"
-
-#if defined(_WIN32)
-#include "lldb/Host/windows/ConnectionGenericFileWindows.h"
-#endif
-
-#include "llvm/ADT/STLExtras.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/Support/FileSystem.h"
-
-#include <memory>
-#include <mutex>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string>
-
-using namespace lldb;
-using namespace lldb_private;
-
-// Defined in the SWIG source file
-#if PY_MAJOR_VERSION >= 3
-extern "C" PyObject *PyInit__lldb(void);
-
-#define LLDBSwigPyInit PyInit__lldb
-
-#else
-extern "C" void init_lldb(void);
-
-#define LLDBSwigPyInit init_lldb
-#endif
-
-// These prototypes are the Pythonic implementations of the required callbacks.
-// Although these are scripting-language specific, their definition depends on
-// the public API.
-extern "C" bool LLDBSwigPythonBreakpointCallbackFunction(
- const char *python_function_name, const char *session_dictionary_name,
- const lldb::StackFrameSP &sb_frame,
- const lldb::BreakpointLocationSP &sb_bp_loc);
-
-extern "C" bool LLDBSwigPythonWatchpointCallbackFunction(
- const char *python_function_name, const char *session_dictionary_name,
- const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp);
-
-extern "C" bool LLDBSwigPythonCallTypeScript(
- const char *python_function_name, void *session_dictionary,
- const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
- const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval);
-
-extern "C" void *
-LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
- const char *session_dictionary_name,
- const lldb::ValueObjectSP &valobj_sp);
-
-extern "C" void *
-LLDBSwigPythonCreateCommandObject(const char *python_class_name,
- const char *session_dictionary_name,
- const lldb::DebuggerSP debugger_sp);
-
-extern "C" void *LLDBSwigPythonCreateScriptedThreadPlan(
- const char *python_class_name, const char *session_dictionary_name,
- const lldb::ThreadPlanSP &thread_plan_sp);
-
-extern "C" bool LLDBSWIGPythonCallThreadPlan(void *implementor,
- const char *method_name,
- Event *event_sp, bool &got_error);
-
-extern "C" void *LLDBSwigPythonCreateScriptedBreakpointResolver(
- const char *python_class_name, const char *session_dictionary_name,
- lldb_private::StructuredDataImpl *args, lldb::BreakpointSP &bkpt_sp);
-
-extern "C" unsigned int
-LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name,
- lldb_private::SymbolContext *sym_ctx);
-
-extern "C" size_t LLDBSwigPython_CalculateNumChildren(void *implementor,
- uint32_t max);
-
-extern "C" void *LLDBSwigPython_GetChildAtIndex(void *implementor,
- uint32_t idx);
-
-extern "C" int LLDBSwigPython_GetIndexOfChildWithName(void *implementor,
- const char *child_name);
-
-extern "C" void *LLDBSWIGPython_CastPyObjectToSBValue(void *data);
-
-extern lldb::ValueObjectSP
-LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data);
-
-extern "C" bool LLDBSwigPython_UpdateSynthProviderInstance(void *implementor);
-
-extern "C" bool
-LLDBSwigPython_MightHaveChildrenSynthProviderInstance(void *implementor);
-
-extern "C" void *
-LLDBSwigPython_GetValueSynthProviderInstance(void *implementor);
-
-extern "C" bool
-LLDBSwigPythonCallCommand(const char *python_function_name,
- const char *session_dictionary_name,
- lldb::DebuggerSP &debugger, const char *args,
- lldb_private::CommandReturnObject &cmd_retobj,
- lldb::ExecutionContextRefSP exe_ctx_ref_sp);
-
-extern "C" bool
-LLDBSwigPythonCallCommandObject(void *implementor, lldb::DebuggerSP &debugger,
- const char *args,
- lldb_private::CommandReturnObject &cmd_retobj,
- lldb::ExecutionContextRefSP exe_ctx_ref_sp);
-
-extern "C" bool
-LLDBSwigPythonCallModuleInit(const char *python_module_name,
- const char *session_dictionary_name,
- lldb::DebuggerSP &debugger);
-
-extern "C" void *
-LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
- const char *session_dictionary_name,
- const lldb::ProcessSP &process_sp);
-
-extern "C" void *
-LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
- const char *session_dictionary_name);
-
-extern "C" void *
-LLDBSwigPython_GetRecognizedArguments(void *implementor,
- const lldb::StackFrameSP &frame_sp);
-
-extern "C" bool LLDBSWIGPythonRunScriptKeywordProcess(
- const char *python_function_name, const char *session_dictionary_name,
- lldb::ProcessSP &process, std::string &output);
-
-extern "C" bool LLDBSWIGPythonRunScriptKeywordThread(
- const char *python_function_name, const char *session_dictionary_name,
- lldb::ThreadSP &thread, std::string &output);
-
-extern "C" bool LLDBSWIGPythonRunScriptKeywordTarget(
- const char *python_function_name, const char *session_dictionary_name,
- lldb::TargetSP &target, std::string &output);
-
-extern "C" bool LLDBSWIGPythonRunScriptKeywordFrame(
- const char *python_function_name, const char *session_dictionary_name,
- lldb::StackFrameSP &frame, std::string &output);
-
-extern "C" bool LLDBSWIGPythonRunScriptKeywordValue(
- const char *python_function_name, const char *session_dictionary_name,
- lldb::ValueObjectSP &value, std::string &output);
-
-extern "C" void *
-LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
- const lldb::TargetSP &target_sp);
-
-static bool g_initialized = false;
-
-namespace {
-
-// Initializing Python is not a straightforward process. We cannot control
-// what external code may have done before getting to this point in LLDB,
-// including potentially having already initialized Python, so we need to do a
-// lot of work to ensure that the existing state of the system is maintained
-// across our initialization. We do this by using an RAII pattern where we
-// save off initial state at the beginning, and restore it at the end
-struct InitializePythonRAII {
-public:
- InitializePythonRAII()
- : m_gil_state(PyGILState_UNLOCKED), m_was_already_initialized(false) {
- // Python will muck with STDIN terminal state, so save off any current TTY
- // settings so we can restore them.
- m_stdin_tty_state.Save(STDIN_FILENO, false);
-
- InitializePythonHome();
-
- // Register _lldb as a built-in module.
- PyImport_AppendInittab("_lldb", LLDBSwigPyInit);
-
-// Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
-// calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you
-// call `PyEval_InitThreads` first, and >= 3.2 requires that you call it last.
-#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || (PY_MAJOR_VERSION > 3)
- Py_InitializeEx(0);
- InitializeThreadsPrivate();
-#else
- InitializeThreadsPrivate();
- Py_InitializeEx(0);
-#endif
- }
-
- ~InitializePythonRAII() {
- if (m_was_already_initialized) {
- Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT));
- LLDB_LOGV(log, "Releasing PyGILState. Returning to state = {0}locked",
- m_gil_state == PyGILState_UNLOCKED ? "un" : "");
- PyGILState_Release(m_gil_state);
- } else {
- // We initialized the threads in this function, just unlock the GIL.
- PyEval_SaveThread();
- }
-
- m_stdin_tty_state.Restore();
- }
-
-private:
- void InitializePythonHome() {
-#if defined(LLDB_PYTHON_HOME)
-#if PY_MAJOR_VERSION >= 3
- size_t size = 0;
- static wchar_t *g_python_home = Py_DecodeLocale(LLDB_PYTHON_HOME, &size);
-#else
- static char g_python_home[] = LLDB_PYTHON_HOME;
-#endif
- Py_SetPythonHome(g_python_home);
-#else
-#if defined(__APPLE__) && PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 7
- // For Darwin, the only Python version supported is the one shipped in the
- // OS OS and linked with lldb. Other installation of Python may have higher
- // priorities in the path, overriding PYTHONHOME and causing
- // problems/incompatibilities. In order to avoid confusion, always hardcode
- // the PythonHome to be right, as it's not going to change.
- static char path[] =
- "/System/Library/Frameworks/Python.framework/Versions/2.7";
- Py_SetPythonHome(path);
-#endif
-#endif
- }
-
- void InitializeThreadsPrivate() {
-// Since Python 3.7 `Py_Initialize` calls `PyEval_InitThreads` inside itself,
-// so there is no way to determine whether the embedded interpreter
-// was already initialized by some external code. `PyEval_ThreadsInitialized`
-// would always return `true` and `PyGILState_Ensure/Release` flow would be
-// executed instead of unlocking GIL with `PyEval_SaveThread`. When
-// an another thread calls `PyGILState_Ensure` it would get stuck in deadlock.
-#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 7) || (PY_MAJOR_VERSION > 3)
- // The only case we should go further and acquire the GIL: it is unlocked.
- if (PyGILState_Check())
- return;
-#endif
-
- if (PyEval_ThreadsInitialized()) {
- Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT));
-
- m_was_already_initialized = true;
- m_gil_state = PyGILState_Ensure();
- LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked\n",
- m_gil_state == PyGILState_UNLOCKED ? "un" : "");
- return;
- }
-
- // InitThreads acquires the GIL if it hasn't been called before.
- PyEval_InitThreads();
- }
-
- TerminalState m_stdin_tty_state;
- PyGILState_STATE m_gil_state;
- bool m_was_already_initialized;
-};
-} // namespace
-
-void ScriptInterpreterPython::ComputePythonDirForApple(
- llvm::SmallVectorImpl<char> &path) {
- auto style = llvm::sys::path::Style::posix;
-
- llvm::StringRef path_ref(path.begin(), path.size());
- auto rbegin = llvm::sys::path::rbegin(path_ref, style);
- auto rend = llvm::sys::path::rend(path_ref);
- auto framework = std::find(rbegin, rend, "LLDB.framework");
- if (framework == rend) {
- ComputePythonDirForPosix(path);
- return;
- }
- path.resize(framework - rend);
- llvm::sys::path::append(path, style, "LLDB.framework", "Resources", "Python");
-}
-
-void ScriptInterpreterPython::ComputePythonDirForPosix(
- llvm::SmallVectorImpl<char> &path) {
- auto style = llvm::sys::path::Style::posix;
-#if defined(LLDB_PYTHON_RELATIVE_LIBDIR)
- // Build the path by backing out of the lib dir, then building with whatever
- // the real python interpreter uses. (e.g. lib for most, lib64 on RHEL
- // x86_64).
- llvm::sys::path::remove_filename(path, style);
- llvm::sys::path::append(path, style, LLDB_PYTHON_RELATIVE_LIBDIR);
-#else
- llvm::sys::path::append(path, style,
- "python" + llvm::Twine(PY_MAJOR_VERSION) + "." +
- llvm::Twine(PY_MINOR_VERSION),
- "site-packages");
-#endif
-}
-
-void ScriptInterpreterPython::ComputePythonDirForWindows(
- llvm::SmallVectorImpl<char> &path) {
- auto style = llvm::sys::path::Style::windows;
- llvm::sys::path::remove_filename(path, style);
- llvm::sys::path::append(path, style, "lib", "site-packages");
-
- // This will be injected directly through FileSpec.GetDirectory().SetString(),
- // so we need to normalize manually.
- std::replace(path.begin(), path.end(), '\\', '/');
-}
-
-FileSpec ScriptInterpreterPython::GetPythonDir() {
- static FileSpec g_spec = []() {
- FileSpec spec = HostInfo::GetShlibDir();
- if (!spec)
- return FileSpec();
- llvm::SmallString<64> path;
- spec.GetPath(path);
-
-#if defined(__APPLE__)
- ComputePythonDirForApple(path);
-#elif defined(_WIN32)
- ComputePythonDirForWindows(path);
-#else
- ComputePythonDirForPosix(path);
-#endif
- spec.GetDirectory().SetString(path);
- return spec;
- }();
- return g_spec;
-}
-
-lldb_private::ConstString ScriptInterpreterPython::GetPluginNameStatic() {
- static ConstString g_name("script-python");
- return g_name;
-}
-
-const char *ScriptInterpreterPython::GetPluginDescriptionStatic() {
- return "Embedded Python interpreter";
-}
-
-void ScriptInterpreterPython::Initialize() {
- static llvm::once_flag g_once_flag;
-
- llvm::call_once(g_once_flag, []() {
- PluginManager::RegisterPlugin(GetPluginNameStatic(),
- GetPluginDescriptionStatic(),
- lldb::eScriptLanguagePython,
- ScriptInterpreterPythonImpl::CreateInstance);
- });
-}
-
-void ScriptInterpreterPython::Terminate() {}
-
-ScriptInterpreterPythonImpl::Locker::Locker(
- ScriptInterpreterPythonImpl *py_interpreter, uint16_t on_entry,
- uint16_t on_leave, FILE *in, FILE *out, FILE *err)
- : ScriptInterpreterLocker(),
- m_teardown_session((on_leave & TearDownSession) == TearDownSession),
- m_python_interpreter(py_interpreter) {
- DoAcquireLock();
- if ((on_entry & InitSession) == InitSession) {
- if (!DoInitSession(on_entry, in, out, err)) {
- // Don't teardown the session if we didn't init it.
- m_teardown_session = false;
- }
- }
-}
-
-bool ScriptInterpreterPythonImpl::Locker::DoAcquireLock() {
- Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT));
- m_GILState = PyGILState_Ensure();
- LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked",
- m_GILState == PyGILState_UNLOCKED ? "un" : "");
-
- // we need to save the thread state when we first start the command because
- // we might decide to interrupt it while some action is taking place outside
- // of Python (e.g. printing to screen, waiting for the network, ...) in that
- // case, _PyThreadState_Current will be NULL - and we would be unable to set
- // the asynchronous exception - not a desirable situation
- m_python_interpreter->SetThreadState(PyThreadState_Get());
- m_python_interpreter->IncrementLockCount();
- return true;
-}
-
-bool ScriptInterpreterPythonImpl::Locker::DoInitSession(uint16_t on_entry_flags,
- FILE *in, FILE *out,
- FILE *err) {
- if (!m_python_interpreter)
- return false;
- return m_python_interpreter->EnterSession(on_entry_flags, in, out, err);
-}
-
-bool ScriptInterpreterPythonImpl::Locker::DoFreeLock() {
- Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT));
- LLDB_LOGV(log, "Releasing PyGILState. Returning to state = {0}locked",
- m_GILState == PyGILState_UNLOCKED ? "un" : "");
- PyGILState_Release(m_GILState);
- m_python_interpreter->DecrementLockCount();
- return true;
-}
-
-bool ScriptInterpreterPythonImpl::Locker::DoTearDownSession() {
- if (!m_python_interpreter)
- return false;
- m_python_interpreter->LeaveSession();
- return true;
-}
-
-ScriptInterpreterPythonImpl::Locker::~Locker() {
- if (m_teardown_session)
- DoTearDownSession();
- DoFreeLock();
-}
-
-ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger)
- : ScriptInterpreterPython(debugger), m_saved_stdin(), m_saved_stdout(),
- m_saved_stderr(), m_main_module(),
- m_session_dict(PyInitialValue::Invalid),
- m_sys_module_dict(PyInitialValue::Invalid), m_run_one_line_function(),
- m_run_one_line_str_global(),
- m_dictionary_name(m_debugger.GetInstanceName().AsCString()),
- m_terminal_state(), m_active_io_handler(eIOHandlerNone),
- m_session_is_active(false), m_pty_slave_is_open(false),
- m_valid_session(true), m_lock_count(0), m_command_thread_state(nullptr) {
- InitializePrivate();
-
- m_dictionary_name.append("_dict");
- StreamString run_string;
- run_string.Printf("%s = dict()", m_dictionary_name.c_str());
-
- Locker locker(this, Locker::AcquireLock, Locker::FreeAcquiredLock);
- PyRun_SimpleString(run_string.GetData());
-
- run_string.Clear();
- run_string.Printf(
- "run_one_line (%s, 'import copy, keyword, os, re, sys, uuid, lldb')",
- m_dictionary_name.c_str());
- PyRun_SimpleString(run_string.GetData());
-
- // Reloading modules requires a different syntax in Python 2 and Python 3.
- // This provides a consistent syntax no matter what version of Python.
- run_string.Clear();
- run_string.Printf("run_one_line (%s, 'from six.moves import reload_module')",
- m_dictionary_name.c_str());
- PyRun_SimpleString(run_string.GetData());
-
- // WARNING: temporary code that loads Cocoa formatters - this should be done
- // on a per-platform basis rather than loading the whole set and letting the
- // individual formatter classes exploit APIs to check whether they can/cannot
- // do their task
- run_string.Clear();
- run_string.Printf(
- "run_one_line (%s, 'import lldb.formatters, lldb.formatters.cpp, pydoc')",
- m_dictionary_name.c_str());
- PyRun_SimpleString(run_string.GetData());
- run_string.Clear();
-
- run_string.Printf("run_one_line (%s, 'import lldb.embedded_interpreter; from "
- "lldb.embedded_interpreter import run_python_interpreter; "
- "from lldb.embedded_interpreter import run_one_line')",
- m_dictionary_name.c_str());
- PyRun_SimpleString(run_string.GetData());
- run_string.Clear();
-
- run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64
- "; pydoc.pager = pydoc.plainpager')",
- m_dictionary_name.c_str(), m_debugger.GetID());
- PyRun_SimpleString(run_string.GetData());
-}
-
-ScriptInterpreterPythonImpl::~ScriptInterpreterPythonImpl() {
- // the session dictionary may hold objects with complex state which means
- // that they may need to be torn down with some level of smarts and that, in
- // turn, requires a valid thread state force Python to procure itself such a
- // thread state, nuke the session dictionary and then release it for others
- // to use and proceed with the rest of the shutdown
- auto gil_state = PyGILState_Ensure();
- m_session_dict.Reset();
- PyGILState_Release(gil_state);
-}
-
-lldb_private::ConstString ScriptInterpreterPythonImpl::GetPluginName() {
- return GetPluginNameStatic();
-}
-
-uint32_t ScriptInterpreterPythonImpl::GetPluginVersion() { return 1; }
-
-void ScriptInterpreterPythonImpl::IOHandlerActivated(IOHandler &io_handler,
- bool interactive) {
- const char *instructions = nullptr;
-
- switch (m_active_io_handler) {
- case eIOHandlerNone:
- break;
- case eIOHandlerBreakpoint:
- instructions = R"(Enter your Python command(s). Type 'DONE' to end.
-def function (frame, bp_loc, internal_dict):
- """frame: the lldb.SBFrame for the location at which you stopped
- bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information
- internal_dict: an LLDB support object not to be used"""
-)";
- break;
- case eIOHandlerWatchpoint:
- instructions = "Enter your Python command(s). Type 'DONE' to end.\n";
- break;
- }
-
- if (instructions) {
- StreamFileSP output_sp(io_handler.GetOutputStreamFile());
- if (output_sp && interactive) {
- output_sp->PutCString(instructions);
- output_sp->Flush();
- }
- }
-}
-
-void ScriptInterpreterPythonImpl::IOHandlerInputComplete(IOHandler &io_handler,
- std::string &data) {
- io_handler.SetIsDone(true);
- bool batch_mode = m_debugger.GetCommandInterpreter().GetBatchCommandMode();
-
- switch (m_active_io_handler) {
- case eIOHandlerNone:
- break;
- case eIOHandlerBreakpoint: {
- std::vector<BreakpointOptions *> *bp_options_vec =
- (std::vector<BreakpointOptions *> *)io_handler.GetUserData();
- for (auto bp_options : *bp_options_vec) {
- if (!bp_options)
- continue;
-
- auto data_up = llvm::make_unique<CommandDataPython>();
- if (!data_up)
- break;
- data_up->user_source.SplitIntoLines(data);
-
- if (GenerateBreakpointCommandCallbackData(data_up->user_source,
- data_up->script_source)
- .Success()) {
- auto baton_sp = std::make_shared<BreakpointOptions::CommandBaton>(
- std::move(data_up));
- bp_options->SetCallback(
- ScriptInterpreterPythonImpl::BreakpointCallbackFunction, baton_sp);
- } else if (!batch_mode) {
- StreamFileSP error_sp = io_handler.GetErrorStreamFile();
- if (error_sp) {
- error_sp->Printf("Warning: No command attached to breakpoint.\n");
- error_sp->Flush();
- }
- }
- }
- m_active_io_handler = eIOHandlerNone;
- } break;
- case eIOHandlerWatchpoint: {
- WatchpointOptions *wp_options =
- (WatchpointOptions *)io_handler.GetUserData();
- auto data_up = llvm::make_unique<WatchpointOptions::CommandData>();
- data_up->user_source.SplitIntoLines(data);
-
- if (GenerateWatchpointCommandCallbackData(data_up->user_source,
- data_up->script_source)) {
- auto baton_sp =
- std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
- wp_options->SetCallback(
- ScriptInterpreterPythonImpl::WatchpointCallbackFunction, baton_sp);
- } else if (!batch_mode) {
- StreamFileSP error_sp = io_handler.GetErrorStreamFile();
- if (error_sp) {
- error_sp->Printf("Warning: No command attached to breakpoint.\n");
- error_sp->Flush();
- }
- }
- m_active_io_handler = eIOHandlerNone;
- } break;
- }
-}
-
-lldb::ScriptInterpreterSP
-ScriptInterpreterPythonImpl::CreateInstance(Debugger &debugger) {
- return std::make_shared<ScriptInterpreterPythonImpl>(debugger);
-}
-
-void ScriptInterpreterPythonImpl::ResetOutputFileHandle(FILE *fh) {}
-
-void ScriptInterpreterPythonImpl::SaveTerminalState(int fd) {
- // Python mucks with the terminal state of STDIN. If we can possibly avoid
- // this by setting the file handles up correctly prior to entering the
- // interpreter we should. For now we save and restore the terminal state on
- // the input file handle.
- m_terminal_state.Save(fd, false);
-}
-
-void ScriptInterpreterPythonImpl::RestoreTerminalState() {
- // Python mucks with the terminal state of STDIN. If we can possibly avoid
- // this by setting the file handles up correctly prior to entering the
- // interpreter we should. For now we save and restore the terminal state on
- // the input file handle.
- m_terminal_state.Restore();
-}
-
-void ScriptInterpreterPythonImpl::LeaveSession() {
- Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT));
- if (log)
- log->PutCString("ScriptInterpreterPythonImpl::LeaveSession()");
-
- // checking that we have a valid thread state - since we use our own
- // threading and locking in some (rare) cases during cleanup Python may end
- // up believing we have no thread state and PyImport_AddModule will crash if
- // that is the case - since that seems to only happen when destroying the
- // SBDebugger, we can make do without clearing up stdout and stderr
-
- // rdar://problem/11292882
- // When the current thread state is NULL, PyThreadState_Get() issues a fatal
- // error.
- if (PyThreadState_GetDict()) {
- PythonDictionary &sys_module_dict = GetSysModuleDictionary();
- if (sys_module_dict.IsValid()) {
- if (m_saved_stdin.IsValid()) {
- sys_module_dict.SetItemForKey(PythonString("stdin"), m_saved_stdin);
- m_saved_stdin.Reset();
- }
- if (m_saved_stdout.IsValid()) {
- sys_module_dict.SetItemForKey(PythonString("stdout"), m_saved_stdout);
- m_saved_stdout.Reset();
- }
- if (m_saved_stderr.IsValid()) {
- sys_module_dict.SetItemForKey(PythonString("stderr"), m_saved_stderr);
- m_saved_stderr.Reset();
- }
- }
- }
-
- m_session_is_active = false;
-}
-
-bool ScriptInterpreterPythonImpl::SetStdHandle(File &file, const char *py_name,
- PythonFile &save_file,
- const char *mode) {
- if (file.IsValid()) {
- // Flush the file before giving it to python to avoid interleaved output.
- file.Flush();
-
- PythonDictionary &sys_module_dict = GetSysModuleDictionary();
-
- save_file = sys_module_dict.GetItemForKey(PythonString(py_name))
- .AsType<PythonFile>();
-
- PythonFile new_file(file, mode);
- sys_module_dict.SetItemForKey(PythonString(py_name), new_file);
- return true;
- } else
- save_file.Reset();
- return false;
-}
-
-bool ScriptInterpreterPythonImpl::EnterSession(uint16_t on_entry_flags,
- FILE *in, FILE *out, FILE *err) {
- // If we have already entered the session, without having officially 'left'
- // it, then there is no need to 'enter' it again.
- Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT));
- if (m_session_is_active) {
- if (log)
- log->Printf(
- "ScriptInterpreterPythonImpl::EnterSession(on_entry_flags=0x%" PRIx16
- ") session is already active, returning without doing anything",
- on_entry_flags);
- return false;
- }
-
- if (log)
- log->Printf(
- "ScriptInterpreterPythonImpl::EnterSession(on_entry_flags=0x%" PRIx16
- ")",
- on_entry_flags);
-
- m_session_is_active = true;
-
- StreamString run_string;
-
- if (on_entry_flags & Locker::InitGlobals) {
- run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64,
- m_dictionary_name.c_str(), m_debugger.GetID());
- run_string.Printf(
- "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")",
- m_debugger.GetID());
- run_string.PutCString("; lldb.target = lldb.debugger.GetSelectedTarget()");
- run_string.PutCString("; lldb.process = lldb.target.GetProcess()");
- run_string.PutCString("; lldb.thread = lldb.process.GetSelectedThread ()");
- run_string.PutCString("; lldb.frame = lldb.thread.GetSelectedFrame ()");
- run_string.PutCString("')");
- } else {
- // If we aren't initing the globals, we should still always set the
- // debugger (since that is always unique.)
- run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64,
- m_dictionary_name.c_str(), m_debugger.GetID());
- run_string.Printf(
- "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")",
- m_debugger.GetID());
- run_string.PutCString("')");
- }
-
- PyRun_SimpleString(run_string.GetData());
- run_string.Clear();
-
- PythonDictionary &sys_module_dict = GetSysModuleDictionary();
- if (sys_module_dict.IsValid()) {
- File in_file(in, false);
- File out_file(out, false);
- File err_file(err, false);
-
- lldb::StreamFileSP in_sp;
- lldb::StreamFileSP out_sp;
- lldb::StreamFileSP err_sp;
- if (!in_file.IsValid() || !out_file.IsValid() || !err_file.IsValid())
- m_debugger.AdoptTopIOHandlerFilesIfInvalid(in_sp, out_sp, err_sp);
-
- if (on_entry_flags & Locker::NoSTDIN) {
- m_saved_stdin.Reset();
- } else {
- if (!SetStdHandle(in_file, "stdin", m_saved_stdin, "r")) {
- if (in_sp)
- SetStdHandle(in_sp->GetFile(), "stdin", m_saved_stdin, "r");
- }
- }
-
- if (!SetStdHandle(out_file, "stdout", m_saved_stdout, "w")) {
- if (out_sp)
- SetStdHandle(out_sp->GetFile(), "stdout", m_saved_stdout, "w");
- }
-
- if (!SetStdHandle(err_file, "stderr", m_saved_stderr, "w")) {
- if (err_sp)
- SetStdHandle(err_sp->GetFile(), "stderr", m_saved_stderr, "w");
- }
- }
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- return true;
-}
-
-PythonObject &ScriptInterpreterPythonImpl::GetMainModule() {
- if (!m_main_module.IsValid())
- m_main_module.Reset(PyRefType::Borrowed, PyImport_AddModule("__main__"));
- return m_main_module;
-}
-
-PythonDictionary &ScriptInterpreterPythonImpl::GetSessionDictionary() {
- if (m_session_dict.IsValid())
- return m_session_dict;
-
- PythonObject &main_module = GetMainModule();
- if (!main_module.IsValid())
- return m_session_dict;
-
- PythonDictionary main_dict(PyRefType::Borrowed,
- PyModule_GetDict(main_module.get()));
- if (!main_dict.IsValid())
- return m_session_dict;
-
- PythonObject item = main_dict.GetItemForKey(PythonString(m_dictionary_name));
- m_session_dict.Reset(PyRefType::Borrowed, item.get());
- return m_session_dict;
-}
-
-PythonDictionary &ScriptInterpreterPythonImpl::GetSysModuleDictionary() {
- if (m_sys_module_dict.IsValid())
- return m_sys_module_dict;
-
- PythonObject sys_module(PyRefType::Borrowed, PyImport_AddModule("sys"));
- if (sys_module.IsValid())
- m_sys_module_dict.Reset(PyRefType::Borrowed,
- PyModule_GetDict(sys_module.get()));
- return m_sys_module_dict;
-}
-
-static std::string GenerateUniqueName(const char *base_name_wanted,
- uint32_t &functions_counter,
- const void *name_token = nullptr) {
- StreamString sstr;
-
- if (!base_name_wanted)
- return std::string();
-
- if (!name_token)
- sstr.Printf("%s_%d", base_name_wanted, functions_counter++);
- else
- sstr.Printf("%s_%p", base_name_wanted, name_token);
-
- return sstr.GetString();
-}
-
-bool ScriptInterpreterPythonImpl::GetEmbeddedInterpreterModuleObjects() {
- if (m_run_one_line_function.IsValid())
- return true;
-
- PythonObject module(PyRefType::Borrowed,
- PyImport_AddModule("lldb.embedded_interpreter"));
- if (!module.IsValid())
- return false;
-
- PythonDictionary module_dict(PyRefType::Borrowed,
- PyModule_GetDict(module.get()));
- if (!module_dict.IsValid())
- return false;
-
- m_run_one_line_function =
- module_dict.GetItemForKey(PythonString("run_one_line"));
- m_run_one_line_str_global =
- module_dict.GetItemForKey(PythonString("g_run_one_line_str"));
- return m_run_one_line_function.IsValid();
-}
-
-static void ReadThreadBytesReceived(void *baton, const void *src,
- size_t src_len) {
- if (src && src_len) {
- Stream *strm = (Stream *)baton;
- strm->Write(src, src_len);
- strm->Flush();
- }
-}
-
-bool ScriptInterpreterPythonImpl::ExecuteOneLine(
- llvm::StringRef command, CommandReturnObject *result,
- const ExecuteScriptOptions &options) {
- std::string command_str = command.str();
-
- if (!m_valid_session)
- return false;
-
- if (!command.empty()) {
- // We want to call run_one_line, passing in the dictionary and the command
- // string. We cannot do this through PyRun_SimpleString here because the
- // command string may contain escaped characters, and putting it inside
- // another string to pass to PyRun_SimpleString messes up the escaping. So
- // we use the following more complicated method to pass the command string
- // directly down to Python.
- Debugger &debugger = m_debugger;
-
- StreamFileSP input_file_sp;
- StreamFileSP output_file_sp;
- StreamFileSP error_file_sp;
- Communication output_comm(
- "lldb.ScriptInterpreterPythonImpl.ExecuteOneLine.comm");
- bool join_read_thread = false;
- if (options.GetEnableIO()) {
- if (result) {
- input_file_sp = debugger.GetInputFile();
- // Set output to a temporary file so we can forward the results on to
- // the result object
-
- Pipe pipe;
- Status pipe_result = pipe.CreateNew(false);
- if (pipe_result.Success()) {
-#if defined(_WIN32)
- lldb::file_t read_file = pipe.GetReadNativeHandle();
- pipe.ReleaseReadFileDescriptor();
- std::unique_ptr<ConnectionGenericFile> conn_up(
- new ConnectionGenericFile(read_file, true));
-#else
- std::unique_ptr<ConnectionFileDescriptor> conn_up(
- new ConnectionFileDescriptor(pipe.ReleaseReadFileDescriptor(),
- true));
-#endif
- if (conn_up->IsConnected()) {
- output_comm.SetConnection(conn_up.release());
- output_comm.SetReadThreadBytesReceivedCallback(
- ReadThreadBytesReceived, &result->GetOutputStream());
- output_comm.StartReadThread();
- join_read_thread = true;
- FILE *outfile_handle =
- fdopen(pipe.ReleaseWriteFileDescriptor(), "w");
- output_file_sp = std::make_shared<StreamFile>(outfile_handle, true);
- error_file_sp = output_file_sp;
- if (outfile_handle)
- ::setbuf(outfile_handle, nullptr);
-
- result->SetImmediateOutputFile(
- debugger.GetOutputFile()->GetFile().GetStream());
- result->SetImmediateErrorFile(
- debugger.GetErrorFile()->GetFile().GetStream());
- }
- }
- }
- if (!input_file_sp || !output_file_sp || !error_file_sp)
- debugger.AdoptTopIOHandlerFilesIfInvalid(input_file_sp, output_file_sp,
- error_file_sp);
- } else {
- input_file_sp = std::make_shared<StreamFile>();
- FileSystem::Instance().Open(input_file_sp->GetFile(),
- FileSpec(FileSystem::DEV_NULL),
- File::eOpenOptionRead);
-
- output_file_sp = std::make_shared<StreamFile>();
- FileSystem::Instance().Open(output_file_sp->GetFile(),
- FileSpec(FileSystem::DEV_NULL),
- File::eOpenOptionWrite);
-
- error_file_sp = output_file_sp;
- }
-
- FILE *in_file = input_file_sp->GetFile().GetStream();
- FILE *out_file = output_file_sp->GetFile().GetStream();
- FILE *err_file = error_file_sp->GetFile().GetStream();
- bool success = false;
- {
- // WARNING! It's imperative that this RAII scope be as tight as
- // possible. In particular, the scope must end *before* we try to join
- // the read thread. The reason for this is that a pre-requisite for
- // joining the read thread is that we close the write handle (to break
- // the pipe and cause it to wake up and exit). But acquiring the GIL as
- // below will redirect Python's stdio to use this same handle. If we
- // close the handle while Python is still using it, bad things will
- // happen.
- Locker locker(
- this,
- Locker::AcquireLock | Locker::InitSession |
- (options.GetSetLLDBGlobals() ? Locker::InitGlobals : 0) |
- ((result && result->GetInteractive()) ? 0 : Locker::NoSTDIN),
- Locker::FreeAcquiredLock | Locker::TearDownSession, in_file, out_file,
- err_file);
-
- // Find the correct script interpreter dictionary in the main module.
- PythonDictionary &session_dict = GetSessionDictionary();
- if (session_dict.IsValid()) {
- if (GetEmbeddedInterpreterModuleObjects()) {
- if (PyCallable_Check(m_run_one_line_function.get())) {
- PythonObject pargs(
- PyRefType::Owned,
- Py_BuildValue("(Os)", session_dict.get(), command_str.c_str()));
- if (pargs.IsValid()) {
- PythonObject return_value(
- PyRefType::Owned,
- PyObject_CallObject(m_run_one_line_function.get(),
- pargs.get()));
- if (return_value.IsValid())
- success = true;
- else if (options.GetMaskoutErrors() && PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
- }
- }
- }
- }
-
- // Flush our output and error file handles
- ::fflush(out_file);
- if (out_file != err_file)
- ::fflush(err_file);
- }
-
- if (join_read_thread) {
- // Close the write end of the pipe since we are done with our one line
- // script. This should cause the read thread that output_comm is using to
- // exit
- output_file_sp->GetFile().Close();
- // The close above should cause this thread to exit when it gets to the
- // end of file, so let it get all its data
- output_comm.JoinReadThread();
- // Now we can close the read end of the pipe
- output_comm.Disconnect();
- }
-
- if (success)
- return true;
-
- // The one-liner failed. Append the error message.
- if (result) {
- result->AppendErrorWithFormat(
- "python failed attempting to evaluate '%s'\n", command_str.c_str());
- }
- return false;
- }
-
- if (result)
- result->AppendError("empty command passed to python\n");
- return false;
-}
-
-void ScriptInterpreterPythonImpl::ExecuteInterpreterLoop() {
- static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
- Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
-
- Debugger &debugger = m_debugger;
-
- // At the moment, the only time the debugger does not have an input file
- // handle is when this is called directly from Python, in which case it is
- // both dangerous and unnecessary (not to mention confusing) to try to embed
- // a running interpreter loop inside the already running Python interpreter
- // loop, so we won't do it.
-
- if (!debugger.GetInputFile()->GetFile().IsValid())
- return;
-
- IOHandlerSP io_handler_sp(new IOHandlerPythonInterpreter(debugger, this));
- if (io_handler_sp) {
- debugger.PushIOHandler(io_handler_sp);
- }
-}
-
-bool ScriptInterpreterPythonImpl::Interrupt() {
- Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT));
-
- if (IsExecutingPython()) {
- PyThreadState *state = PyThreadState_GET();
- if (!state)
- state = GetThreadState();
- if (state) {
- long tid = state->thread_id;
- PyThreadState_Swap(state);
- int num_threads = PyThreadState_SetAsyncExc(tid, PyExc_KeyboardInterrupt);
- if (log)
- log->Printf("ScriptInterpreterPythonImpl::Interrupt() sending "
- "PyExc_KeyboardInterrupt (tid = %li, num_threads = %i)...",
- tid, num_threads);
- return true;
- }
- }
- if (log)
- log->Printf(
- "ScriptInterpreterPythonImpl::Interrupt() python code not running, "
- "can't interrupt");
- return false;
-}
-bool ScriptInterpreterPythonImpl::ExecuteOneLineWithReturn(
- llvm::StringRef in_string, ScriptInterpreter::ScriptReturnType return_type,
- void *ret_value, const ExecuteScriptOptions &options) {
-
- Locker locker(this,
- Locker::AcquireLock | Locker::InitSession |
- (options.GetSetLLDBGlobals() ? Locker::InitGlobals : 0) |
- Locker::NoSTDIN,
- Locker::FreeAcquiredLock | Locker::TearDownSession);
-
- PythonObject py_return;
- PythonObject &main_module = GetMainModule();
- PythonDictionary globals(PyRefType::Borrowed,
- PyModule_GetDict(main_module.get()));
- PythonObject py_error;
- bool ret_success = false;
- int success;
-
- PythonDictionary locals = GetSessionDictionary();
-
- if (!locals.IsValid()) {
- locals.Reset(
- PyRefType::Owned,
- PyObject_GetAttrString(globals.get(), m_dictionary_name.c_str()));
- }
-
- if (!locals.IsValid())
- locals = globals;
-
- py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
- if (py_error.IsValid())
- PyErr_Clear();
-
- std::string as_string = in_string.str();
- { // scope for PythonInputReaderManager
- // PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
- py_return.Reset(PyRefType::Owned,
- PyRun_String(as_string.c_str(), Py_eval_input,
- globals.get(), locals.get()));
- if (!py_return.IsValid()) {
- py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
- if (py_error.IsValid())
- PyErr_Clear();
-
- py_return.Reset(PyRefType::Owned,
- PyRun_String(as_string.c_str(), Py_single_input,
- globals.get(), locals.get()));
- }
- }
-
- if (py_return.IsValid()) {
- switch (return_type) {
- case eScriptReturnTypeCharPtr: // "char *"
- {
- const char format[3] = "s#";
- success = PyArg_Parse(py_return.get(), format, (char **)ret_value);
- break;
- }
- case eScriptReturnTypeCharStrOrNone: // char* or NULL if py_return ==
- // Py_None
- {
- const char format[3] = "z";
- success = PyArg_Parse(py_return.get(), format, (char **)ret_value);
- break;
- }
- case eScriptReturnTypeBool: {
- const char format[2] = "b";
- success = PyArg_Parse(py_return.get(), format, (bool *)ret_value);
- break;
- }
- case eScriptReturnTypeShortInt: {
- const char format[2] = "h";
- success = PyArg_Parse(py_return.get(), format, (short *)ret_value);
- break;
- }
- case eScriptReturnTypeShortIntUnsigned: {
- const char format[2] = "H";
- success =
- PyArg_Parse(py_return.get(), format, (unsigned short *)ret_value);
- break;
- }
- case eScriptReturnTypeInt: {
- const char format[2] = "i";
- success = PyArg_Parse(py_return.get(), format, (int *)ret_value);
- break;
- }
- case eScriptReturnTypeIntUnsigned: {
- const char format[2] = "I";
- success = PyArg_Parse(py_return.get(), format, (unsigned int *)ret_value);
- break;
- }
- case eScriptReturnTypeLongInt: {
- const char format[2] = "l";
- success = PyArg_Parse(py_return.get(), format, (long *)ret_value);
- break;
- }
- case eScriptReturnTypeLongIntUnsigned: {
- const char format[2] = "k";
- success =
- PyArg_Parse(py_return.get(), format, (unsigned long *)ret_value);
- break;
- }
- case eScriptReturnTypeLongLong: {
- const char format[2] = "L";
- success = PyArg_Parse(py_return.get(), format, (long long *)ret_value);
- break;
- }
- case eScriptReturnTypeLongLongUnsigned: {
- const char format[2] = "K";
- success =
- PyArg_Parse(py_return.get(), format, (unsigned long long *)ret_value);
- break;
- }
- case eScriptReturnTypeFloat: {
- const char format[2] = "f";
- success = PyArg_Parse(py_return.get(), format, (float *)ret_value);
- break;
- }
- case eScriptReturnTypeDouble: {
- const char format[2] = "d";
- success = PyArg_Parse(py_return.get(), format, (double *)ret_value);
- break;
- }
- case eScriptReturnTypeChar: {
- const char format[2] = "c";
- success = PyArg_Parse(py_return.get(), format, (char *)ret_value);
- break;
- }
- case eScriptReturnTypeOpaqueObject: {
- success = true;
- PyObject *saved_value = py_return.get();
- Py_XINCREF(saved_value);
- *((PyObject **)ret_value) = saved_value;
- break;
- }
- }
-
- ret_success = success;
- }
-
- py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
- if (py_error.IsValid()) {
- ret_success = false;
- if (options.GetMaskoutErrors()) {
- if (PyErr_GivenExceptionMatches(py_error.get(), PyExc_SyntaxError))
- PyErr_Print();
- PyErr_Clear();
- }
- }
-
- return ret_success;
-}
-
-Status ScriptInterpreterPythonImpl::ExecuteMultipleLines(
- const char *in_string, const ExecuteScriptOptions &options) {
- Status error;
-
- Locker locker(this,
- Locker::AcquireLock | Locker::InitSession |
- (options.GetSetLLDBGlobals() ? Locker::InitGlobals : 0) |
- Locker::NoSTDIN,
- Locker::FreeAcquiredLock | Locker::TearDownSession);
-
- PythonObject return_value;
- PythonObject &main_module = GetMainModule();
- PythonDictionary globals(PyRefType::Borrowed,
- PyModule_GetDict(main_module.get()));
- PythonObject py_error;
-
- PythonDictionary locals = GetSessionDictionary();
-
- if (!locals.IsValid())
- locals.Reset(
- PyRefType::Owned,
- PyObject_GetAttrString(globals.get(), m_dictionary_name.c_str()));
-
- if (!locals.IsValid())
- locals = globals;
-
- py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
- if (py_error.IsValid())
- PyErr_Clear();
-
- if (in_string != nullptr) {
- PythonObject code_object;
- code_object.Reset(PyRefType::Owned,
- Py_CompileString(in_string, "temp.py", Py_file_input));
-
- if (code_object.IsValid()) {
-// In Python 2.x, PyEval_EvalCode takes a PyCodeObject, but in Python 3.x, it
-// takes a PyObject. They are convertible (hence the function
-// PyCode_Check(PyObject*), so we have to do the cast for Python 2.x
-#if PY_MAJOR_VERSION >= 3
- PyObject *py_code_obj = code_object.get();
-#else
- PyCodeObject *py_code_obj =
- reinterpret_cast<PyCodeObject *>(code_object.get());
-#endif
- return_value.Reset(
- PyRefType::Owned,
- PyEval_EvalCode(py_code_obj, globals.get(), locals.get()));
- }
- }
-
- PythonExceptionState exception_state(!options.GetMaskoutErrors());
- if (exception_state.IsError())
- error.SetErrorString(exception_state.Format().c_str());
-
- return error;
-}
-
-void ScriptInterpreterPythonImpl::CollectDataForBreakpointCommandCallback(
- std::vector<BreakpointOptions *> &bp_options_vec,
- CommandReturnObject &result) {
- m_active_io_handler = eIOHandlerBreakpoint;
- m_debugger.GetCommandInterpreter().GetPythonCommandsFromIOHandler(
- " ", *this, true, &bp_options_vec);
-}
-
-void ScriptInterpreterPythonImpl::CollectDataForWatchpointCommandCallback(
- WatchpointOptions *wp_options, CommandReturnObject &result) {
- m_active_io_handler = eIOHandlerWatchpoint;
- m_debugger.GetCommandInterpreter().GetPythonCommandsFromIOHandler(
- " ", *this, true, wp_options);
-}
-
-void ScriptInterpreterPythonImpl::SetBreakpointCommandCallbackFunction(
- BreakpointOptions *bp_options, const char *function_name) {
- // For now just cons up a oneliner that calls the provided function.
- std::string oneliner("return ");
- oneliner += function_name;
- oneliner += "(frame, bp_loc, internal_dict)";
- m_debugger.GetScriptInterpreter()->SetBreakpointCommandCallback(
- bp_options, oneliner.c_str());
-}
-
-Status ScriptInterpreterPythonImpl::SetBreakpointCommandCallback(
- BreakpointOptions *bp_options,
- std::unique_ptr<BreakpointOptions::CommandData> &cmd_data_up) {
- Status error;
- error = GenerateBreakpointCommandCallbackData(cmd_data_up->user_source,
- cmd_data_up->script_source);
- if (error.Fail()) {
- return error;
- }
- auto baton_sp =
- std::make_shared<BreakpointOptions::CommandBaton>(std::move(cmd_data_up));
- bp_options->SetCallback(
- ScriptInterpreterPythonImpl::BreakpointCallbackFunction, baton_sp);
- return error;
-}
-
-// Set a Python one-liner as the callback for the breakpoint.
-Status ScriptInterpreterPythonImpl::SetBreakpointCommandCallback(
- BreakpointOptions *bp_options, const char *command_body_text) {
- auto data_up = llvm::make_unique<CommandDataPython>();
-
- // Split the command_body_text into lines, and pass that to
- // GenerateBreakpointCommandCallbackData. That will wrap the body in an
- // auto-generated function, and return the function name in script_source.
- // That is what the callback will actually invoke.
-
- data_up->user_source.SplitIntoLines(command_body_text);
- Status error = GenerateBreakpointCommandCallbackData(data_up->user_source,
- data_up->script_source);
- if (error.Success()) {
- auto baton_sp =
- std::make_shared<BreakpointOptions::CommandBaton>(std::move(data_up));
- bp_options->SetCallback(
- ScriptInterpreterPythonImpl::BreakpointCallbackFunction, baton_sp);
- return error;
- } else
- return error;
-}
-
-// Set a Python one-liner as the callback for the watchpoint.
-void ScriptInterpreterPythonImpl::SetWatchpointCommandCallback(
- WatchpointOptions *wp_options, const char *oneliner) {
- auto data_up = llvm::make_unique<WatchpointOptions::CommandData>();
-
- // It's necessary to set both user_source and script_source to the oneliner.
- // The former is used to generate callback description (as in watchpoint
- // command list) while the latter is used for Python to interpret during the
- // actual callback.
-
- data_up->user_source.AppendString(oneliner);
- data_up->script_source.assign(oneliner);
-
- if (GenerateWatchpointCommandCallbackData(data_up->user_source,
- data_up->script_source)) {
- auto baton_sp =
- std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
- wp_options->SetCallback(
- ScriptInterpreterPythonImpl::WatchpointCallbackFunction, baton_sp);
- }
-
- return;
-}
-
-Status ScriptInterpreterPythonImpl::ExportFunctionDefinitionToInterpreter(
- StringList &function_def) {
- // Convert StringList to one long, newline delimited, const char *.
- std::string function_def_string(function_def.CopyList());
-
- Status error = ExecuteMultipleLines(
- function_def_string.c_str(),
- ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false));
- return error;
-}
-
-Status ScriptInterpreterPythonImpl::GenerateFunction(const char *signature,
- const StringList &input) {
- Status error;
- int num_lines = input.GetSize();
- if (num_lines == 0) {
- error.SetErrorString("No input data.");
- return error;
- }
-
- if (!signature || *signature == 0) {
- error.SetErrorString("No output function name.");
- return error;
- }
-
- StreamString sstr;
- StringList auto_generated_function;
- auto_generated_function.AppendString(signature);
- auto_generated_function.AppendString(
- " global_dict = globals()"); // Grab the global dictionary
- auto_generated_function.AppendString(
- " new_keys = internal_dict.keys()"); // Make a list of keys in the
- // session dict
- auto_generated_function.AppendString(
- " old_keys = global_dict.keys()"); // Save list of keys in global dict
- auto_generated_function.AppendString(
- " global_dict.update (internal_dict)"); // Add the session dictionary
- // to the
- // global dictionary.
-
- // Wrap everything up inside the function, increasing the indentation.
-
- auto_generated_function.AppendString(" if True:");
- for (int i = 0; i < num_lines; ++i) {
- sstr.Clear();
- sstr.Printf(" %s", input.GetStringAtIndex(i));
- auto_generated_function.AppendString(sstr.GetData());
- }
- auto_generated_function.AppendString(
- " for key in new_keys:"); // Iterate over all the keys from session
- // dict
- auto_generated_function.AppendString(
- " internal_dict[key] = global_dict[key]"); // Update session dict
- // values
- auto_generated_function.AppendString(
- " if key not in old_keys:"); // If key was not originally in
- // global dict
- auto_generated_function.AppendString(
- " del global_dict[key]"); // ...then remove key/value from
- // global dict
-
- // Verify that the results are valid Python.
-
- error = ExportFunctionDefinitionToInterpreter(auto_generated_function);
-
- return error;
-}
-
-bool ScriptInterpreterPythonImpl::GenerateTypeScriptFunction(
- StringList &user_input, std::string &output, const void *name_token) {
- static uint32_t num_created_functions = 0;
- user_input.RemoveBlankLines();
- StreamString sstr;
-
- // Check to see if we have any data; if not, just return.
- if (user_input.GetSize() == 0)
- return false;
-
- // Take what the user wrote, wrap it all up inside one big auto-generated
- // Python function, passing in the ValueObject as parameter to the function.
-
- std::string auto_generated_function_name(
- GenerateUniqueName("lldb_autogen_python_type_print_func",
- num_created_functions, name_token));
- sstr.Printf("def %s (valobj, internal_dict):",
- auto_generated_function_name.c_str());
-
- if (!GenerateFunction(sstr.GetData(), user_input).Success())
- return false;
-
- // Store the name of the auto-generated function to be called.
- output.assign(auto_generated_function_name);
- return true;
-}
-
-bool ScriptInterpreterPythonImpl::GenerateScriptAliasFunction(
- StringList &user_input, std::string &output) {
- static uint32_t num_created_functions = 0;
- user_input.RemoveBlankLines();
- StreamString sstr;
-
- // Check to see if we have any data; if not, just return.
- if (user_input.GetSize() == 0)
- return false;
-
- std::string auto_generated_function_name(GenerateUniqueName(
- "lldb_autogen_python_cmd_alias_func", num_created_functions));
-
- sstr.Printf("def %s (debugger, args, result, internal_dict):",
- auto_generated_function_name.c_str());
-
- if (!GenerateFunction(sstr.GetData(), user_input).Success())
- return false;
-
- // Store the name of the auto-generated function to be called.
- output.assign(auto_generated_function_name);
- return true;
-}
-
-bool ScriptInterpreterPythonImpl::GenerateTypeSynthClass(
- StringList &user_input, std::string &output, const void *name_token) {
- static uint32_t num_created_classes = 0;
- user_input.RemoveBlankLines();
- int num_lines = user_input.GetSize();
- StreamString sstr;
-
- // Check to see if we have any data; if not, just return.
- if (user_input.GetSize() == 0)
- return false;
-
- // Wrap all user input into a Python class
-
- std::string auto_generated_class_name(GenerateUniqueName(
- "lldb_autogen_python_type_synth_class", num_created_classes, name_token));
-
- StringList auto_generated_class;
-
- // Create the function name & definition string.
-
- sstr.Printf("class %s:", auto_generated_class_name.c_str());
- auto_generated_class.AppendString(sstr.GetString());
-
- // Wrap everything up inside the class, increasing the indentation. we don't
- // need to play any fancy indentation tricks here because there is no
- // surrounding code whose indentation we need to honor
- for (int i = 0; i < num_lines; ++i) {
- sstr.Clear();
- sstr.Printf(" %s", user_input.GetStringAtIndex(i));
- auto_generated_class.AppendString(sstr.GetString());
- }
-
- // Verify that the results are valid Python. (even though the method is
- // ExportFunctionDefinitionToInterpreter, a class will actually be exported)
- // (TODO: rename that method to ExportDefinitionToInterpreter)
- if (!ExportFunctionDefinitionToInterpreter(auto_generated_class).Success())
- return false;
-
- // Store the name of the auto-generated class
-
- output.assign(auto_generated_class_name);
- return true;
-}
-
-StructuredData::GenericSP
-ScriptInterpreterPythonImpl::CreateFrameRecognizer(const char *class_name) {
- if (class_name == nullptr || class_name[0] == '\0')
- return StructuredData::GenericSP();
-
- void *ret_val;
-
- {
- Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN,
- Locker::FreeLock);
- ret_val = LLDBSWIGPython_CreateFrameRecognizer(class_name,
- m_dictionary_name.c_str());
- }
-
- return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
-}
-
-lldb::ValueObjectListSP ScriptInterpreterPythonImpl::GetRecognizedArguments(
- const StructuredData::ObjectSP &os_plugin_object_sp,
- lldb::StackFrameSP frame_sp) {
- Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
-
- if (!os_plugin_object_sp)
- return ValueObjectListSP();
-
- StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
- if (!generic)
- return nullptr;
-
- PythonObject implementor(PyRefType::Borrowed,
- (PyObject *)generic->GetValue());
-
- if (!implementor.IsAllocated())
- return ValueObjectListSP();
-
- PythonObject py_return(PyRefType::Owned,
- (PyObject *)LLDBSwigPython_GetRecognizedArguments(
- implementor.get(), frame_sp));
-
- // if it fails, print the error but otherwise go on
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
- if (py_return.get()) {
- PythonList result_list(PyRefType::Borrowed, py_return.get());
- ValueObjectListSP result = ValueObjectListSP(new ValueObjectList());
- for (size_t i = 0; i < result_list.GetSize(); i++) {
- PyObject *item = result_list.GetItemAtIndex(i).get();
- lldb::SBValue *sb_value_ptr =
- (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(item);
- auto valobj_sp = LLDBSWIGPython_GetValueObjectSPFromSBValue(sb_value_ptr);
- if (valobj_sp)
- result->Append(valobj_sp);
- }
- return result;
- }
- return ValueObjectListSP();
-}
-
-StructuredData::GenericSP
-ScriptInterpreterPythonImpl::OSPlugin_CreatePluginObject(
- const char *class_name, lldb::ProcessSP process_sp) {
- if (class_name == nullptr || class_name[0] == '\0')
- return StructuredData::GenericSP();
-
- if (!process_sp)
- return StructuredData::GenericSP();
-
- void *ret_val;
-
- {
- Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN,
- Locker::FreeLock);
- ret_val = LLDBSWIGPythonCreateOSPlugin(
- class_name, m_dictionary_name.c_str(), process_sp);
- }
-
- return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
-}
-
-StructuredData::DictionarySP ScriptInterpreterPythonImpl::OSPlugin_RegisterInfo(
- StructuredData::ObjectSP os_plugin_object_sp) {
- Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
-
- static char callee_name[] = "get_register_info";
-
- if (!os_plugin_object_sp)
- return StructuredData::DictionarySP();
-
- StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
- if (!generic)
- return nullptr;
-
- PythonObject implementor(PyRefType::Borrowed,
- (PyObject *)generic->GetValue());
-
- if (!implementor.IsAllocated())
- return StructuredData::DictionarySP();
-
- PythonObject pmeth(PyRefType::Owned,
- PyObject_GetAttrString(implementor.get(), callee_name));
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- if (!pmeth.IsAllocated())
- return StructuredData::DictionarySP();
-
- if (PyCallable_Check(pmeth.get()) == 0) {
- if (PyErr_Occurred())
- PyErr_Clear();
-
- return StructuredData::DictionarySP();
- }
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- // right now we know this function exists and is callable..
- PythonObject py_return(
- PyRefType::Owned,
- PyObject_CallMethod(implementor.get(), callee_name, nullptr));
-
- // if it fails, print the error but otherwise go on
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
- if (py_return.get()) {
- PythonDictionary result_dict(PyRefType::Borrowed, py_return.get());
- return result_dict.CreateStructuredDictionary();
- }
- return StructuredData::DictionarySP();
-}
-
-StructuredData::ArraySP ScriptInterpreterPythonImpl::OSPlugin_ThreadsInfo(
- StructuredData::ObjectSP os_plugin_object_sp) {
- Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
-
- static char callee_name[] = "get_thread_info";
-
- if (!os_plugin_object_sp)
- return StructuredData::ArraySP();
-
- StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
- if (!generic)
- return nullptr;
-
- PythonObject implementor(PyRefType::Borrowed,
- (PyObject *)generic->GetValue());
-
- if (!implementor.IsAllocated())
- return StructuredData::ArraySP();
-
- PythonObject pmeth(PyRefType::Owned,
- PyObject_GetAttrString(implementor.get(), callee_name));
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- if (!pmeth.IsAllocated())
- return StructuredData::ArraySP();
-
- if (PyCallable_Check(pmeth.get()) == 0) {
- if (PyErr_Occurred())
- PyErr_Clear();
-
- return StructuredData::ArraySP();
- }
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- // right now we know this function exists and is callable..
- PythonObject py_return(
- PyRefType::Owned,
- PyObject_CallMethod(implementor.get(), callee_name, nullptr));
-
- // if it fails, print the error but otherwise go on
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
-
- if (py_return.get()) {
- PythonList result_list(PyRefType::Borrowed, py_return.get());
- return result_list.CreateStructuredArray();
- }
- return StructuredData::ArraySP();
-}
-
-// GetPythonValueFormatString provides a system independent type safe way to
-// convert a variable's type into a python value format. Python value formats
-// are defined in terms of builtin C types and could change from system to as
-// the underlying typedef for uint* types, size_t, off_t and other values
-// change.
-
-template <typename T> const char *GetPythonValueFormatString(T t);
-template <> const char *GetPythonValueFormatString(char *) { return "s"; }
-template <> const char *GetPythonValueFormatString(char) { return "b"; }
-template <> const char *GetPythonValueFormatString(unsigned char) {
- return "B";
-}
-template <> const char *GetPythonValueFormatString(short) { return "h"; }
-template <> const char *GetPythonValueFormatString(unsigned short) {
- return "H";
-}
-template <> const char *GetPythonValueFormatString(int) { return "i"; }
-template <> const char *GetPythonValueFormatString(unsigned int) { return "I"; }
-template <> const char *GetPythonValueFormatString(long) { return "l"; }
-template <> const char *GetPythonValueFormatString(unsigned long) {
- return "k";
-}
-template <> const char *GetPythonValueFormatString(long long) { return "L"; }
-template <> const char *GetPythonValueFormatString(unsigned long long) {
- return "K";
-}
-template <> const char *GetPythonValueFormatString(float t) { return "f"; }
-template <> const char *GetPythonValueFormatString(double t) { return "d"; }
-
-StructuredData::StringSP
-ScriptInterpreterPythonImpl::OSPlugin_RegisterContextData(
- StructuredData::ObjectSP os_plugin_object_sp, lldb::tid_t tid) {
- Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
-
- static char callee_name[] = "get_register_data";
- static char *param_format =
- const_cast<char *>(GetPythonValueFormatString(tid));
-
- if (!os_plugin_object_sp)
- return StructuredData::StringSP();
-
- StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
- if (!generic)
- return nullptr;
- PythonObject implementor(PyRefType::Borrowed,
- (PyObject *)generic->GetValue());
-
- if (!implementor.IsAllocated())
- return StructuredData::StringSP();
-
- PythonObject pmeth(PyRefType::Owned,
- PyObject_GetAttrString(implementor.get(), callee_name));
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- if (!pmeth.IsAllocated())
- return StructuredData::StringSP();
-
- if (PyCallable_Check(pmeth.get()) == 0) {
- if (PyErr_Occurred())
- PyErr_Clear();
- return StructuredData::StringSP();
- }
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- // right now we know this function exists and is callable..
- PythonObject py_return(
- PyRefType::Owned,
- PyObject_CallMethod(implementor.get(), callee_name, param_format, tid));
-
- // if it fails, print the error but otherwise go on
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
-
- if (py_return.get()) {
- PythonBytes result(PyRefType::Borrowed, py_return.get());
- return result.CreateStructuredString();
- }
- return StructuredData::StringSP();
-}
-
-StructuredData::DictionarySP ScriptInterpreterPythonImpl::OSPlugin_CreateThread(
- StructuredData::ObjectSP os_plugin_object_sp, lldb::tid_t tid,
- lldb::addr_t context) {
- Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
-
- static char callee_name[] = "create_thread";
- std::string param_format;
- param_format += GetPythonValueFormatString(tid);
- param_format += GetPythonValueFormatString(context);
-
- if (!os_plugin_object_sp)
- return StructuredData::DictionarySP();
-
- StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
- if (!generic)
- return nullptr;
-
- PythonObject implementor(PyRefType::Borrowed,
- (PyObject *)generic->GetValue());
-
- if (!implementor.IsAllocated())
- return StructuredData::DictionarySP();
-
- PythonObject pmeth(PyRefType::Owned,
- PyObject_GetAttrString(implementor.get(), callee_name));
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- if (!pmeth.IsAllocated())
- return StructuredData::DictionarySP();
-
- if (PyCallable_Check(pmeth.get()) == 0) {
- if (PyErr_Occurred())
- PyErr_Clear();
- return StructuredData::DictionarySP();
- }
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- // right now we know this function exists and is callable..
- PythonObject py_return(PyRefType::Owned,
- PyObject_CallMethod(implementor.get(), callee_name,
- &param_format[0], tid, context));
-
- // if it fails, print the error but otherwise go on
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
-
- if (py_return.get()) {
- PythonDictionary result_dict(PyRefType::Borrowed, py_return.get());
- return result_dict.CreateStructuredDictionary();
- }
- return StructuredData::DictionarySP();
-}
-
-StructuredData::ObjectSP ScriptInterpreterPythonImpl::CreateScriptedThreadPlan(
- const char *class_name, lldb::ThreadPlanSP thread_plan_sp) {
- if (class_name == nullptr || class_name[0] == '\0')
- return StructuredData::ObjectSP();
-
- if (!thread_plan_sp.get())
- return StructuredData::ObjectSP();
-
- Debugger &debugger = thread_plan_sp->GetTarget().GetDebugger();
- ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
- ScriptInterpreterPythonImpl *python_interpreter =
- static_cast<ScriptInterpreterPythonImpl *>(script_interpreter);
-
- if (!script_interpreter)
- return StructuredData::ObjectSP();
-
- void *ret_val;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
-
- ret_val = LLDBSwigPythonCreateScriptedThreadPlan(
- class_name, python_interpreter->m_dictionary_name.c_str(),
- thread_plan_sp);
- }
-
- return StructuredData::ObjectSP(new StructuredPythonObject(ret_val));
-}
-
-bool ScriptInterpreterPythonImpl::ScriptedThreadPlanExplainsStop(
- StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error) {
- bool explains_stop = true;
- StructuredData::Generic *generic = nullptr;
- if (implementor_sp)
- generic = implementor_sp->GetAsGeneric();
- if (generic) {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- explains_stop = LLDBSWIGPythonCallThreadPlan(
- generic->GetValue(), "explains_stop", event, script_error);
- if (script_error)
- return true;
- }
- return explains_stop;
-}
-
-bool ScriptInterpreterPythonImpl::ScriptedThreadPlanShouldStop(
- StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error) {
- bool should_stop = true;
- StructuredData::Generic *generic = nullptr;
- if (implementor_sp)
- generic = implementor_sp->GetAsGeneric();
- if (generic) {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- should_stop = LLDBSWIGPythonCallThreadPlan(
- generic->GetValue(), "should_stop", event, script_error);
- if (script_error)
- return true;
- }
- return should_stop;
-}
-
-bool ScriptInterpreterPythonImpl::ScriptedThreadPlanIsStale(
- StructuredData::ObjectSP implementor_sp, bool &script_error) {
- bool is_stale = true;
- StructuredData::Generic *generic = nullptr;
- if (implementor_sp)
- generic = implementor_sp->GetAsGeneric();
- if (generic) {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- is_stale = LLDBSWIGPythonCallThreadPlan(generic->GetValue(), "is_stale",
- nullptr, script_error);
- if (script_error)
- return true;
- }
- return is_stale;
-}
-
-lldb::StateType ScriptInterpreterPythonImpl::ScriptedThreadPlanGetRunState(
- StructuredData::ObjectSP implementor_sp, bool &script_error) {
- bool should_step = false;
- StructuredData::Generic *generic = nullptr;
- if (implementor_sp)
- generic = implementor_sp->GetAsGeneric();
- if (generic) {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- should_step = LLDBSWIGPythonCallThreadPlan(
- generic->GetValue(), "should_step", nullptr, script_error);
- if (script_error)
- should_step = true;
- }
- if (should_step)
- return lldb::eStateStepping;
- else
- return lldb::eStateRunning;
-}
-
-StructuredData::GenericSP
-ScriptInterpreterPythonImpl::CreateScriptedBreakpointResolver(
- const char *class_name, StructuredDataImpl *args_data,
- lldb::BreakpointSP &bkpt_sp) {
-
- if (class_name == nullptr || class_name[0] == '\0')
- return StructuredData::GenericSP();
-
- if (!bkpt_sp.get())
- return StructuredData::GenericSP();
-
- Debugger &debugger = bkpt_sp->GetTarget().GetDebugger();
- ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
- ScriptInterpreterPythonImpl *python_interpreter =
- static_cast<ScriptInterpreterPythonImpl *>(script_interpreter);
-
- if (!script_interpreter)
- return StructuredData::GenericSP();
-
- void *ret_val;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
-
- ret_val = LLDBSwigPythonCreateScriptedBreakpointResolver(
- class_name, python_interpreter->m_dictionary_name.c_str(), args_data,
- bkpt_sp);
- }
-
- return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
-}
-
-bool ScriptInterpreterPythonImpl::ScriptedBreakpointResolverSearchCallback(
- StructuredData::GenericSP implementor_sp, SymbolContext *sym_ctx) {
- bool should_continue = false;
-
- if (implementor_sp) {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- should_continue = LLDBSwigPythonCallBreakpointResolver(
- implementor_sp->GetValue(), "__callback__", sym_ctx);
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
- }
- return should_continue;
-}
-
-lldb::SearchDepth
-ScriptInterpreterPythonImpl::ScriptedBreakpointResolverSearchDepth(
- StructuredData::GenericSP implementor_sp) {
- int depth_as_int = lldb::eSearchDepthModule;
- if (implementor_sp) {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- depth_as_int = LLDBSwigPythonCallBreakpointResolver(
- implementor_sp->GetValue(), "__get_depth__", nullptr);
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
- }
- if (depth_as_int == lldb::eSearchDepthInvalid)
- return lldb::eSearchDepthModule;
-
- if (depth_as_int <= lldb::kLastSearchDepthKind)
- return (lldb::SearchDepth)depth_as_int;
- else
- return lldb::eSearchDepthModule;
-}
-
-StructuredData::ObjectSP
-ScriptInterpreterPythonImpl::LoadPluginModule(const FileSpec &file_spec,
- lldb_private::Status &error) {
- if (!FileSystem::Instance().Exists(file_spec)) {
- error.SetErrorString("no such file");
- return StructuredData::ObjectSP();
- }
-
- StructuredData::ObjectSP module_sp;
-
- if (LoadScriptingModule(file_spec.GetPath().c_str(), true, true, error,
- &module_sp))
- return module_sp;
-
- return StructuredData::ObjectSP();
-}
-
-StructuredData::DictionarySP ScriptInterpreterPythonImpl::GetDynamicSettings(
- StructuredData::ObjectSP plugin_module_sp, Target *target,
- const char *setting_name, lldb_private::Status &error) {
- if (!plugin_module_sp || !target || !setting_name || !setting_name[0])
- return StructuredData::DictionarySP();
- StructuredData::Generic *generic = plugin_module_sp->GetAsGeneric();
- if (!generic)
- return StructuredData::DictionarySP();
-
- PythonObject reply_pyobj;
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- TargetSP target_sp(target->shared_from_this());
- reply_pyobj.Reset(PyRefType::Owned,
- (PyObject *)LLDBSWIGPython_GetDynamicSetting(
- generic->GetValue(), setting_name, target_sp));
-
- PythonDictionary py_dict(PyRefType::Borrowed, reply_pyobj.get());
- return py_dict.CreateStructuredDictionary();
-}
-
-StructuredData::ObjectSP
-ScriptInterpreterPythonImpl::CreateSyntheticScriptedProvider(
- const char *class_name, lldb::ValueObjectSP valobj) {
- if (class_name == nullptr || class_name[0] == '\0')
- return StructuredData::ObjectSP();
-
- if (!valobj.get())
- return StructuredData::ObjectSP();
-
- ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
- Target *target = exe_ctx.GetTargetPtr();
-
- if (!target)
- return StructuredData::ObjectSP();
-
- Debugger &debugger = target->GetDebugger();
- ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
- ScriptInterpreterPythonImpl *python_interpreter =
- (ScriptInterpreterPythonImpl *)script_interpreter;
-
- if (!script_interpreter)
- return StructuredData::ObjectSP();
-
- void *ret_val = nullptr;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val = LLDBSwigPythonCreateSyntheticProvider(
- class_name, python_interpreter->m_dictionary_name.c_str(), valobj);
- }
-
- return StructuredData::ObjectSP(new StructuredPythonObject(ret_val));
-}
-
-StructuredData::GenericSP
-ScriptInterpreterPythonImpl::CreateScriptCommandObject(const char *class_name) {
- DebuggerSP debugger_sp(m_debugger.shared_from_this());
-
- if (class_name == nullptr || class_name[0] == '\0')
- return StructuredData::GenericSP();
-
- if (!debugger_sp.get())
- return StructuredData::GenericSP();
-
- void *ret_val;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val = LLDBSwigPythonCreateCommandObject(
- class_name, m_dictionary_name.c_str(), debugger_sp);
- }
-
- return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
-}
-
-bool ScriptInterpreterPythonImpl::GenerateTypeScriptFunction(
- const char *oneliner, std::string &output, const void *name_token) {
- StringList input;
- input.SplitIntoLines(oneliner, strlen(oneliner));
- return GenerateTypeScriptFunction(input, output, name_token);
-}
-
-bool ScriptInterpreterPythonImpl::GenerateTypeSynthClass(
- const char *oneliner, std::string &output, const void *name_token) {
- StringList input;
- input.SplitIntoLines(oneliner, strlen(oneliner));
- return GenerateTypeSynthClass(input, output, name_token);
-}
-
-Status ScriptInterpreterPythonImpl::GenerateBreakpointCommandCallbackData(
- StringList &user_input, std::string &output) {
- static uint32_t num_created_functions = 0;
- user_input.RemoveBlankLines();
- StreamString sstr;
- Status error;
- if (user_input.GetSize() == 0) {
- error.SetErrorString("No input data.");
- return error;
- }
-
- std::string auto_generated_function_name(GenerateUniqueName(
- "lldb_autogen_python_bp_callback_func_", num_created_functions));
- sstr.Printf("def %s (frame, bp_loc, internal_dict):",
- auto_generated_function_name.c_str());
-
- error = GenerateFunction(sstr.GetData(), user_input);
- if (!error.Success())
- return error;
-
- // Store the name of the auto-generated function to be called.
- output.assign(auto_generated_function_name);
- return error;
-}
-
-bool ScriptInterpreterPythonImpl::GenerateWatchpointCommandCallbackData(
- StringList &user_input, std::string &output) {
- static uint32_t num_created_functions = 0;
- user_input.RemoveBlankLines();
- StreamString sstr;
-
- if (user_input.GetSize() == 0)
- return false;
-
- std::string auto_generated_function_name(GenerateUniqueName(
- "lldb_autogen_python_wp_callback_func_", num_created_functions));
- sstr.Printf("def %s (frame, wp, internal_dict):",
- auto_generated_function_name.c_str());
-
- if (!GenerateFunction(sstr.GetData(), user_input).Success())
- return false;
-
- // Store the name of the auto-generated function to be called.
- output.assign(auto_generated_function_name);
- return true;
-}
-
-bool ScriptInterpreterPythonImpl::GetScriptedSummary(
- const char *python_function_name, lldb::ValueObjectSP valobj,
- StructuredData::ObjectSP &callee_wrapper_sp,
- const TypeSummaryOptions &options, std::string &retval) {
-
- static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
- Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
-
- if (!valobj.get()) {
- retval.assign("<no object>");
- return false;
- }
-
- void *old_callee = nullptr;
- StructuredData::Generic *generic = nullptr;
- if (callee_wrapper_sp) {
- generic = callee_wrapper_sp->GetAsGeneric();
- if (generic)
- old_callee = generic->GetValue();
- }
- void *new_callee = old_callee;
-
- bool ret_val;
- if (python_function_name && *python_function_name) {
- {
- Locker py_lock(this, Locker::AcquireLock | Locker::InitSession |
- Locker::NoSTDIN);
- {
- TypeSummaryOptionsSP options_sp(new TypeSummaryOptions(options));
-
- static Timer::Category func_cat("LLDBSwigPythonCallTypeScript");
- Timer scoped_timer(func_cat, "LLDBSwigPythonCallTypeScript");
- ret_val = LLDBSwigPythonCallTypeScript(
- python_function_name, GetSessionDictionary().get(), valobj,
- &new_callee, options_sp, retval);
- }
- }
- } else {
- retval.assign("<no function name>");
- return false;
- }
-
- if (new_callee && old_callee != new_callee)
- callee_wrapper_sp = std::make_shared<StructuredPythonObject>(new_callee);
-
- return ret_val;
-}
-
-void ScriptInterpreterPythonImpl::Clear() {
- // Release any global variables that might have strong references to
- // LLDB objects when clearing the python script interpreter.
- Locker locker(this, Locker::AcquireLock, Locker::FreeAcquiredLock);
-
- // This may be called as part of Py_Finalize. In that case the modules are
- // destroyed in random order and we can't guarantee that we can access these.
- if (Py_IsInitialized())
- PyRun_SimpleString("lldb.debugger = None; lldb.target = None; lldb.process "
- "= None; lldb.thread = None; lldb.frame = None");
-}
-
-bool ScriptInterpreterPythonImpl::BreakpointCallbackFunction(
- void *baton, StoppointCallbackContext *context, user_id_t break_id,
- user_id_t break_loc_id) {
- CommandDataPython *bp_option_data = (CommandDataPython *)baton;
- const char *python_function_name = bp_option_data->script_source.c_str();
-
- if (!context)
- return true;
-
- ExecutionContext exe_ctx(context->exe_ctx_ref);
- Target *target = exe_ctx.GetTargetPtr();
-
- if (!target)
- return true;
-
- Debugger &debugger = target->GetDebugger();
- ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
- ScriptInterpreterPythonImpl *python_interpreter =
- (ScriptInterpreterPythonImpl *)script_interpreter;
-
- if (!script_interpreter)
- return true;
-
- if (python_function_name && python_function_name[0]) {
- const StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
- BreakpointSP breakpoint_sp = target->GetBreakpointByID(break_id);
- if (breakpoint_sp) {
- const BreakpointLocationSP bp_loc_sp(
- breakpoint_sp->FindLocationByID(break_loc_id));
-
- if (stop_frame_sp && bp_loc_sp) {
- bool ret_val = true;
- {
- Locker py_lock(python_interpreter, Locker::AcquireLock |
- Locker::InitSession |
- Locker::NoSTDIN);
- ret_val = LLDBSwigPythonBreakpointCallbackFunction(
- python_function_name,
- python_interpreter->m_dictionary_name.c_str(), stop_frame_sp,
- bp_loc_sp);
- }
- return ret_val;
- }
- }
- }
- // We currently always true so we stop in case anything goes wrong when
- // trying to call the script function
- return true;
-}
-
-bool ScriptInterpreterPythonImpl::WatchpointCallbackFunction(
- void *baton, StoppointCallbackContext *context, user_id_t watch_id) {
- WatchpointOptions::CommandData *wp_option_data =
- (WatchpointOptions::CommandData *)baton;
- const char *python_function_name = wp_option_data->script_source.c_str();
-
- if (!context)
- return true;
-
- ExecutionContext exe_ctx(context->exe_ctx_ref);
- Target *target = exe_ctx.GetTargetPtr();
-
- if (!target)
- return true;
-
- Debugger &debugger = target->GetDebugger();
- ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
- ScriptInterpreterPythonImpl *python_interpreter =
- (ScriptInterpreterPythonImpl *)script_interpreter;
-
- if (!script_interpreter)
- return true;
-
- if (python_function_name && python_function_name[0]) {
- const StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
- WatchpointSP wp_sp = target->GetWatchpointList().FindByID(watch_id);
- if (wp_sp) {
- if (stop_frame_sp && wp_sp) {
- bool ret_val = true;
- {
- Locker py_lock(python_interpreter, Locker::AcquireLock |
- Locker::InitSession |
- Locker::NoSTDIN);
- ret_val = LLDBSwigPythonWatchpointCallbackFunction(
- python_function_name,
- python_interpreter->m_dictionary_name.c_str(), stop_frame_sp,
- wp_sp);
- }
- return ret_val;
- }
- }
- }
- // We currently always true so we stop in case anything goes wrong when
- // trying to call the script function
- return true;
-}
-
-size_t ScriptInterpreterPythonImpl::CalculateNumChildren(
- const StructuredData::ObjectSP &implementor_sp, uint32_t max) {
- if (!implementor_sp)
- return 0;
- StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
- if (!generic)
- return 0;
- void *implementor = generic->GetValue();
- if (!implementor)
- return 0;
-
- size_t ret_val = 0;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val = LLDBSwigPython_CalculateNumChildren(implementor, max);
- }
-
- return ret_val;
-}
-
-lldb::ValueObjectSP ScriptInterpreterPythonImpl::GetChildAtIndex(
- const StructuredData::ObjectSP &implementor_sp, uint32_t idx) {
- if (!implementor_sp)
- return lldb::ValueObjectSP();
-
- StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
- if (!generic)
- return lldb::ValueObjectSP();
- void *implementor = generic->GetValue();
- if (!implementor)
- return lldb::ValueObjectSP();
-
- lldb::ValueObjectSP ret_val;
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- void *child_ptr = LLDBSwigPython_GetChildAtIndex(implementor, idx);
- if (child_ptr != nullptr && child_ptr != Py_None) {
- lldb::SBValue *sb_value_ptr =
- (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(child_ptr);
- if (sb_value_ptr == nullptr)
- Py_XDECREF(child_ptr);
- else
- ret_val = LLDBSWIGPython_GetValueObjectSPFromSBValue(sb_value_ptr);
- } else {
- Py_XDECREF(child_ptr);
- }
- }
-
- return ret_val;
-}
-
-int ScriptInterpreterPythonImpl::GetIndexOfChildWithName(
- const StructuredData::ObjectSP &implementor_sp, const char *child_name) {
- if (!implementor_sp)
- return UINT32_MAX;
-
- StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
- if (!generic)
- return UINT32_MAX;
- void *implementor = generic->GetValue();
- if (!implementor)
- return UINT32_MAX;
-
- int ret_val = UINT32_MAX;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val = LLDBSwigPython_GetIndexOfChildWithName(implementor, child_name);
- }
-
- return ret_val;
-}
-
-bool ScriptInterpreterPythonImpl::UpdateSynthProviderInstance(
- const StructuredData::ObjectSP &implementor_sp) {
- bool ret_val = false;
-
- if (!implementor_sp)
- return ret_val;
-
- StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
- if (!generic)
- return ret_val;
- void *implementor = generic->GetValue();
- if (!implementor)
- return ret_val;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val = LLDBSwigPython_UpdateSynthProviderInstance(implementor);
- }
-
- return ret_val;
-}
-
-bool ScriptInterpreterPythonImpl::MightHaveChildrenSynthProviderInstance(
- const StructuredData::ObjectSP &implementor_sp) {
- bool ret_val = false;
-
- if (!implementor_sp)
- return ret_val;
-
- StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
- if (!generic)
- return ret_val;
- void *implementor = generic->GetValue();
- if (!implementor)
- return ret_val;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val =
- LLDBSwigPython_MightHaveChildrenSynthProviderInstance(implementor);
- }
-
- return ret_val;
-}
-
-lldb::ValueObjectSP ScriptInterpreterPythonImpl::GetSyntheticValue(
- const StructuredData::ObjectSP &implementor_sp) {
- lldb::ValueObjectSP ret_val(nullptr);
-
- if (!implementor_sp)
- return ret_val;
-
- StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
- if (!generic)
- return ret_val;
- void *implementor = generic->GetValue();
- if (!implementor)
- return ret_val;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- void *child_ptr = LLDBSwigPython_GetValueSynthProviderInstance(implementor);
- if (child_ptr != nullptr && child_ptr != Py_None) {
- lldb::SBValue *sb_value_ptr =
- (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(child_ptr);
- if (sb_value_ptr == nullptr)
- Py_XDECREF(child_ptr);
- else
- ret_val = LLDBSWIGPython_GetValueObjectSPFromSBValue(sb_value_ptr);
- } else {
- Py_XDECREF(child_ptr);
- }
- }
-
- return ret_val;
-}
-
-ConstString ScriptInterpreterPythonImpl::GetSyntheticTypeName(
- const StructuredData::ObjectSP &implementor_sp) {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
-
- static char callee_name[] = "get_type_name";
-
- ConstString ret_val;
- bool got_string = false;
- std::string buffer;
-
- if (!implementor_sp)
- return ret_val;
-
- StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
- if (!generic)
- return ret_val;
- PythonObject implementor(PyRefType::Borrowed,
- (PyObject *)generic->GetValue());
- if (!implementor.IsAllocated())
- return ret_val;
-
- PythonObject pmeth(PyRefType::Owned,
- PyObject_GetAttrString(implementor.get(), callee_name));
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- if (!pmeth.IsAllocated())
- return ret_val;
-
- if (PyCallable_Check(pmeth.get()) == 0) {
- if (PyErr_Occurred())
- PyErr_Clear();
- return ret_val;
- }
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- // right now we know this function exists and is callable..
- PythonObject py_return(
- PyRefType::Owned,
- PyObject_CallMethod(implementor.get(), callee_name, nullptr));
-
- // if it fails, print the error but otherwise go on
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
-
- if (py_return.IsAllocated() && PythonString::Check(py_return.get())) {
- PythonString py_string(PyRefType::Borrowed, py_return.get());
- llvm::StringRef return_data(py_string.GetString());
- if (!return_data.empty()) {
- buffer.assign(return_data.data(), return_data.size());
- got_string = true;
- }
- }
-
- if (got_string)
- ret_val.SetCStringWithLength(buffer.c_str(), buffer.size());
-
- return ret_val;
-}
-
-bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
- const char *impl_function, Process *process, std::string &output,
- Status &error) {
- bool ret_val;
- if (!process) {
- error.SetErrorString("no process");
- return false;
- }
- if (!impl_function || !impl_function[0]) {
- error.SetErrorString("no function to execute");
- return false;
- }
-
- {
- ProcessSP process_sp(process->shared_from_this());
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val = LLDBSWIGPythonRunScriptKeywordProcess(
- impl_function, m_dictionary_name.c_str(), process_sp, output);
- if (!ret_val)
- error.SetErrorString("python script evaluation failed");
- }
- return ret_val;
-}
-
-bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
- const char *impl_function, Thread *thread, std::string &output,
- Status &error) {
- bool ret_val;
- if (!thread) {
- error.SetErrorString("no thread");
- return false;
- }
- if (!impl_function || !impl_function[0]) {
- error.SetErrorString("no function to execute");
- return false;
- }
-
- {
- ThreadSP thread_sp(thread->shared_from_this());
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val = LLDBSWIGPythonRunScriptKeywordThread(
- impl_function, m_dictionary_name.c_str(), thread_sp, output);
- if (!ret_val)
- error.SetErrorString("python script evaluation failed");
- }
- return ret_val;
-}
-
-bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
- const char *impl_function, Target *target, std::string &output,
- Status &error) {
- bool ret_val;
- if (!target) {
- error.SetErrorString("no thread");
- return false;
- }
- if (!impl_function || !impl_function[0]) {
- error.SetErrorString("no function to execute");
- return false;
- }
-
- {
- TargetSP target_sp(target->shared_from_this());
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val = LLDBSWIGPythonRunScriptKeywordTarget(
- impl_function, m_dictionary_name.c_str(), target_sp, output);
- if (!ret_val)
- error.SetErrorString("python script evaluation failed");
- }
- return ret_val;
-}
-
-bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
- const char *impl_function, StackFrame *frame, std::string &output,
- Status &error) {
- bool ret_val;
- if (!frame) {
- error.SetErrorString("no frame");
- return false;
- }
- if (!impl_function || !impl_function[0]) {
- error.SetErrorString("no function to execute");
- return false;
- }
-
- {
- StackFrameSP frame_sp(frame->shared_from_this());
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val = LLDBSWIGPythonRunScriptKeywordFrame(
- impl_function, m_dictionary_name.c_str(), frame_sp, output);
- if (!ret_val)
- error.SetErrorString("python script evaluation failed");
- }
- return ret_val;
-}
-
-bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
- const char *impl_function, ValueObject *value, std::string &output,
- Status &error) {
- bool ret_val;
- if (!value) {
- error.SetErrorString("no value");
- return false;
- }
- if (!impl_function || !impl_function[0]) {
- error.SetErrorString("no function to execute");
- return false;
- }
-
- {
- ValueObjectSP value_sp(value->GetSP());
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
- ret_val = LLDBSWIGPythonRunScriptKeywordValue(
- impl_function, m_dictionary_name.c_str(), value_sp, output);
- if (!ret_val)
- error.SetErrorString("python script evaluation failed");
- }
- return ret_val;
-}
-
-uint64_t replace_all(std::string &str, const std::string &oldStr,
- const std::string &newStr) {
- size_t pos = 0;
- uint64_t matches = 0;
- while ((pos = str.find(oldStr, pos)) != std::string::npos) {
- matches++;
- str.replace(pos, oldStr.length(), newStr);
- pos += newStr.length();
- }
- return matches;
-}
-
-bool ScriptInterpreterPythonImpl::LoadScriptingModule(
- const char *pathname, bool can_reload, bool init_session,
- lldb_private::Status &error, StructuredData::ObjectSP *module_sp) {
- if (!pathname || !pathname[0]) {
- error.SetErrorString("invalid pathname");
- return false;
- }
-
- lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();
-
- {
- FileSpec target_file(pathname);
- FileSystem::Instance().Resolve(target_file);
- std::string basename(target_file.GetFilename().GetCString());
-
- StreamString command_stream;
-
- // Before executing Python code, lock the GIL.
- Locker py_lock(this,
- Locker::AcquireLock |
- (init_session ? Locker::InitSession : 0) |
- Locker::NoSTDIN,
- Locker::FreeAcquiredLock |
- (init_session ? Locker::TearDownSession : 0));
- namespace fs = llvm::sys::fs;
- fs::file_status st;
- std::error_code ec = status(target_file.GetPath(), st);
-
- if (ec || st.type() == fs::file_type::status_error ||
- st.type() == fs::file_type::type_unknown ||
- st.type() == fs::file_type::file_not_found) {
- // if not a valid file of any sort, check if it might be a filename still
- // dot can't be used but / and \ can, and if either is found, reject
- if (strchr(pathname, '\\') || strchr(pathname, '/')) {
- error.SetErrorString("invalid pathname");
- return false;
- }
- basename = pathname; // not a filename, probably a package of some sort,
- // let it go through
- } else if (is_directory(st) || is_regular_file(st)) {
- if (target_file.GetDirectory().IsEmpty()) {
- error.SetErrorString("invalid directory name");
- return false;
- }
-
- std::string directory = target_file.GetDirectory().GetCString();
- replace_all(directory, "\\", "\\\\");
- replace_all(directory, "'", "\\'");
-
- // now make sure that Python has "directory" in the search path
- StreamString command_stream;
- command_stream.Printf("if not (sys.path.__contains__('%s')):\n "
- "sys.path.insert(1,'%s');\n\n",
- directory.c_str(), directory.c_str());
- bool syspath_retval =
- ExecuteMultipleLines(command_stream.GetData(),
- ScriptInterpreter::ExecuteScriptOptions()
- .SetEnableIO(false)
- .SetSetLLDBGlobals(false))
- .Success();
- if (!syspath_retval) {
- error.SetErrorString("Python sys.path handling failed");
- return false;
- }
-
- // strip .py or .pyc extension
- ConstString extension = target_file.GetFileNameExtension();
- if (extension) {
- if (llvm::StringRef(extension.GetCString()) == ".py")
- basename.resize(basename.length() - 3);
- else if (llvm::StringRef(extension.GetCString()) == ".pyc")
- basename.resize(basename.length() - 4);
- }
- } else {
- error.SetErrorString("no known way to import this module specification");
- return false;
- }
-
- // check if the module is already import-ed
- command_stream.Clear();
- command_stream.Printf("sys.modules.__contains__('%s')", basename.c_str());
- bool does_contain = false;
- // this call will succeed if the module was ever imported in any Debugger
- // in the lifetime of the process in which this LLDB framework is living
- bool was_imported_globally =
- (ExecuteOneLineWithReturn(
- command_stream.GetData(),
- ScriptInterpreterPythonImpl::eScriptReturnTypeBool, &does_contain,
- ScriptInterpreter::ExecuteScriptOptions()
- .SetEnableIO(false)
- .SetSetLLDBGlobals(false)) &&
- does_contain);
- // this call will fail if the module was not imported in this Debugger
- // before
- command_stream.Clear();
- command_stream.Printf("sys.getrefcount(%s)", basename.c_str());
- bool was_imported_locally = GetSessionDictionary()
- .GetItemForKey(PythonString(basename))
- .IsAllocated();
-
- bool was_imported = (was_imported_globally || was_imported_locally);
-
- if (was_imported && !can_reload) {
- error.SetErrorString("module already imported");
- return false;
- }
-
- // now actually do the import
- command_stream.Clear();
-
- if (was_imported) {
- if (!was_imported_locally)
- command_stream.Printf("import %s ; reload_module(%s)", basename.c_str(),
- basename.c_str());
- else
- command_stream.Printf("reload_module(%s)", basename.c_str());
- } else
- command_stream.Printf("import %s", basename.c_str());
-
- error = ExecuteMultipleLines(command_stream.GetData(),
- ScriptInterpreter::ExecuteScriptOptions()
- .SetEnableIO(false)
- .SetSetLLDBGlobals(false));
- if (error.Fail())
- return false;
-
- // if we are here, everything worked
- // call __lldb_init_module(debugger,dict)
- if (!LLDBSwigPythonCallModuleInit(basename.c_str(),
- m_dictionary_name.c_str(), debugger_sp)) {
- error.SetErrorString("calling __lldb_init_module failed");
- return false;
- }
-
- if (module_sp) {
- // everything went just great, now set the module object
- command_stream.Clear();
- command_stream.Printf("%s", basename.c_str());
- void *module_pyobj = nullptr;
- if (ExecuteOneLineWithReturn(
- command_stream.GetData(),
- ScriptInterpreter::eScriptReturnTypeOpaqueObject,
- &module_pyobj) &&
- module_pyobj)
- *module_sp = std::make_shared<StructuredPythonObject>(module_pyobj);
- }
-
- return true;
- }
-}
-
-bool ScriptInterpreterPythonImpl::IsReservedWord(const char *word) {
- if (!word || !word[0])
- return false;
-
- llvm::StringRef word_sr(word);
-
- // filter out a few characters that would just confuse us and that are
- // clearly not keyword material anyway
- if (word_sr.find('"') != llvm::StringRef::npos ||
- word_sr.find('\'') != llvm::StringRef::npos)
- return false;
-
- StreamString command_stream;
- command_stream.Printf("keyword.iskeyword('%s')", word);
- bool result;
- ExecuteScriptOptions options;
- options.SetEnableIO(false);
- options.SetMaskoutErrors(true);
- options.SetSetLLDBGlobals(false);
- if (ExecuteOneLineWithReturn(command_stream.GetData(),
- ScriptInterpreter::eScriptReturnTypeBool,
- &result, options))
- return result;
- return false;
-}
-
-ScriptInterpreterPythonImpl::SynchronicityHandler::SynchronicityHandler(
- lldb::DebuggerSP debugger_sp, ScriptedCommandSynchronicity synchro)
- : m_debugger_sp(debugger_sp), m_synch_wanted(synchro),
- m_old_asynch(debugger_sp->GetAsyncExecution()) {
- if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous)
- m_debugger_sp->SetAsyncExecution(false);
- else if (m_synch_wanted == eScriptedCommandSynchronicityAsynchronous)
- m_debugger_sp->SetAsyncExecution(true);
-}
-
-ScriptInterpreterPythonImpl::SynchronicityHandler::~SynchronicityHandler() {
- if (m_synch_wanted != eScriptedCommandSynchronicityCurrentValue)
- m_debugger_sp->SetAsyncExecution(m_old_asynch);
-}
-
-bool ScriptInterpreterPythonImpl::RunScriptBasedCommand(
- const char *impl_function, llvm::StringRef args,
- ScriptedCommandSynchronicity synchronicity,
- lldb_private::CommandReturnObject &cmd_retobj, Status &error,
- const lldb_private::ExecutionContext &exe_ctx) {
- if (!impl_function) {
- error.SetErrorString("no function to execute");
- return false;
- }
-
- lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();
- lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx));
-
- if (!debugger_sp.get()) {
- error.SetErrorString("invalid Debugger pointer");
- return false;
- }
-
- bool ret_val = false;
-
- std::string err_msg;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession |
- (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN),
- Locker::FreeLock | Locker::TearDownSession);
-
- SynchronicityHandler synch_handler(debugger_sp, synchronicity);
-
- std::string args_str = args.str();
- ret_val = LLDBSwigPythonCallCommand(
- impl_function, m_dictionary_name.c_str(), debugger_sp, args_str.c_str(),
- cmd_retobj, exe_ctx_ref_sp);
- }
-
- if (!ret_val)
- error.SetErrorString("unable to execute script function");
- else
- error.Clear();
-
- return ret_val;
-}
-
-bool ScriptInterpreterPythonImpl::RunScriptBasedCommand(
- StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
- ScriptedCommandSynchronicity synchronicity,
- lldb_private::CommandReturnObject &cmd_retobj, Status &error,
- const lldb_private::ExecutionContext &exe_ctx) {
- if (!impl_obj_sp || !impl_obj_sp->IsValid()) {
- error.SetErrorString("no function to execute");
- return false;
- }
-
- lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();
- lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx));
-
- if (!debugger_sp.get()) {
- error.SetErrorString("invalid Debugger pointer");
- return false;
- }
-
- bool ret_val = false;
-
- std::string err_msg;
-
- {
- Locker py_lock(this,
- Locker::AcquireLock | Locker::InitSession |
- (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN),
- Locker::FreeLock | Locker::TearDownSession);
-
- SynchronicityHandler synch_handler(debugger_sp, synchronicity);
-
- std::string args_str = args.str();
- ret_val = LLDBSwigPythonCallCommandObject(impl_obj_sp->GetValue(),
- debugger_sp, args_str.c_str(),
- cmd_retobj, exe_ctx_ref_sp);
- }
-
- if (!ret_val)
- error.SetErrorString("unable to execute script function");
- else
- error.Clear();
-
- return ret_val;
-}
-
-// in Python, a special attribute __doc__ contains the docstring for an object
-// (function, method, class, ...) if any is defined Otherwise, the attribute's
-// value is None
-bool ScriptInterpreterPythonImpl::GetDocumentationForItem(const char *item,
- std::string &dest) {
- dest.clear();
- if (!item || !*item)
- return false;
- std::string command(item);
- command += ".__doc__";
-
- char *result_ptr = nullptr; // Python is going to point this to valid data if
- // ExecuteOneLineWithReturn returns successfully
-
- if (ExecuteOneLineWithReturn(
- command.c_str(), ScriptInterpreter::eScriptReturnTypeCharStrOrNone,
- &result_ptr,
- ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false))) {
- if (result_ptr)
- dest.assign(result_ptr);
- return true;
- } else {
- StreamString str_stream;
- str_stream.Printf(
- "Function %s was not found. Containing module might be missing.", item);
- dest = str_stream.GetString();
- return false;
- }
-}
-
-bool ScriptInterpreterPythonImpl::GetShortHelpForCommandObject(
- StructuredData::GenericSP cmd_obj_sp, std::string &dest) {
- bool got_string = false;
- dest.clear();
-
- Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
-
- static char callee_name[] = "get_short_help";
-
- if (!cmd_obj_sp)
- return false;
-
- PythonObject implementor(PyRefType::Borrowed,
- (PyObject *)cmd_obj_sp->GetValue());
-
- if (!implementor.IsAllocated())
- return false;
-
- PythonObject pmeth(PyRefType::Owned,
- PyObject_GetAttrString(implementor.get(), callee_name));
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- if (!pmeth.IsAllocated())
- return false;
-
- if (PyCallable_Check(pmeth.get()) == 0) {
- if (PyErr_Occurred())
- PyErr_Clear();
- return false;
- }
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- // right now we know this function exists and is callable..
- PythonObject py_return(
- PyRefType::Owned,
- PyObject_CallMethod(implementor.get(), callee_name, nullptr));
-
- // if it fails, print the error but otherwise go on
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
-
- if (py_return.IsAllocated() && PythonString::Check(py_return.get())) {
- PythonString py_string(PyRefType::Borrowed, py_return.get());
- llvm::StringRef return_data(py_string.GetString());
- dest.assign(return_data.data(), return_data.size());
- got_string = true;
- }
- return got_string;
-}
-
-uint32_t ScriptInterpreterPythonImpl::GetFlagsForCommandObject(
- StructuredData::GenericSP cmd_obj_sp) {
- uint32_t result = 0;
-
- Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
-
- static char callee_name[] = "get_flags";
-
- if (!cmd_obj_sp)
- return result;
-
- PythonObject implementor(PyRefType::Borrowed,
- (PyObject *)cmd_obj_sp->GetValue());
-
- if (!implementor.IsAllocated())
- return result;
-
- PythonObject pmeth(PyRefType::Owned,
- PyObject_GetAttrString(implementor.get(), callee_name));
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- if (!pmeth.IsAllocated())
- return result;
-
- if (PyCallable_Check(pmeth.get()) == 0) {
- if (PyErr_Occurred())
- PyErr_Clear();
- return result;
- }
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- // right now we know this function exists and is callable..
- PythonObject py_return(
- PyRefType::Owned,
- PyObject_CallMethod(implementor.get(), callee_name, nullptr));
-
- // if it fails, print the error but otherwise go on
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
-
- if (py_return.IsAllocated() && PythonInteger::Check(py_return.get())) {
- PythonInteger int_value(PyRefType::Borrowed, py_return.get());
- result = int_value.GetInteger();
- }
-
- return result;
-}
-
-bool ScriptInterpreterPythonImpl::GetLongHelpForCommandObject(
- StructuredData::GenericSP cmd_obj_sp, std::string &dest) {
- bool got_string = false;
- dest.clear();
-
- Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
-
- static char callee_name[] = "get_long_help";
-
- if (!cmd_obj_sp)
- return false;
-
- PythonObject implementor(PyRefType::Borrowed,
- (PyObject *)cmd_obj_sp->GetValue());
-
- if (!implementor.IsAllocated())
- return false;
-
- PythonObject pmeth(PyRefType::Owned,
- PyObject_GetAttrString(implementor.get(), callee_name));
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- if (!pmeth.IsAllocated())
- return false;
-
- if (PyCallable_Check(pmeth.get()) == 0) {
- if (PyErr_Occurred())
- PyErr_Clear();
-
- return false;
- }
-
- if (PyErr_Occurred())
- PyErr_Clear();
-
- // right now we know this function exists and is callable..
- PythonObject py_return(
- PyRefType::Owned,
- PyObject_CallMethod(implementor.get(), callee_name, nullptr));
-
- // if it fails, print the error but otherwise go on
- if (PyErr_Occurred()) {
- PyErr_Print();
- PyErr_Clear();
- }
-
- if (py_return.IsAllocated() && PythonString::Check(py_return.get())) {
- PythonString str(PyRefType::Borrowed, py_return.get());
- llvm::StringRef str_data(str.GetString());
- dest.assign(str_data.data(), str_data.size());
- got_string = true;
- }
-
- return got_string;
-}
-
-std::unique_ptr<ScriptInterpreterLocker>
-ScriptInterpreterPythonImpl::AcquireInterpreterLock() {
- std::unique_ptr<ScriptInterpreterLocker> py_lock(new Locker(
- this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN,
- Locker::FreeLock | Locker::TearDownSession));
- return py_lock;
-}
-
-void ScriptInterpreterPythonImpl::InitializePrivate() {
- if (g_initialized)
- return;
-
- g_initialized = true;
-
- static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
- Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
-
- // RAII-based initialization which correctly handles multiple-initialization,
- // version- specific differences among Python 2 and Python 3, and saving and
- // restoring various other pieces of state that can get mucked with during
- // initialization.
- InitializePythonRAII initialize_guard;
-
- LLDBSwigPyInit();
-
- // Update the path python uses to search for modules to include the current
- // directory.
-
- PyRun_SimpleString("import sys");
- AddToSysPath(AddLocation::End, ".");
-
- // Don't denormalize paths when calling file_spec.GetPath(). On platforms
- // that use a backslash as the path separator, this will result in executing
- // python code containing paths with unescaped backslashes. But Python also
- // accepts forward slashes, so to make life easier we just use that.
- if (FileSpec file_spec = GetPythonDir())
- AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
- if (FileSpec file_spec = HostInfo::GetShlibDir())
- AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
-
- PyRun_SimpleString("sys.dont_write_bytecode = 1; import "
- "lldb.embedded_interpreter; from "
- "lldb.embedded_interpreter import run_python_interpreter; "
- "from lldb.embedded_interpreter import run_one_line");
-}
-
-void ScriptInterpreterPythonImpl::AddToSysPath(AddLocation location,
- std::string path) {
- std::string path_copy;
-
- std::string statement;
- if (location == AddLocation::Beginning) {
- statement.assign("sys.path.insert(0,\"");
- statement.append(path);
- statement.append("\")");
- } else {
- statement.assign("sys.path.append(\"");
- statement.append(path);
- statement.append("\")");
- }
- PyRun_SimpleString(statement.c_str());
-}
-
-// We are intentionally NOT calling Py_Finalize here (this would be the logical
-// place to call it). Calling Py_Finalize here causes test suite runs to seg
-// fault: The test suite runs in Python. It registers SBDebugger::Terminate to
-// be called 'at_exit'. When the test suite Python harness finishes up, it
-// calls Py_Finalize, which calls all the 'at_exit' registered functions.
-// SBDebugger::Terminate calls Debugger::Terminate, which calls lldb::Terminate,
-// which calls ScriptInterpreter::Terminate, which calls
-// ScriptInterpreterPythonImpl::Terminate. So if we call Py_Finalize here, we
-// end up with Py_Finalize being called from within Py_Finalize, which results
-// in a seg fault. Since this function only gets called when lldb is shutting
-// down and going away anyway, the fact that we don't actually call Py_Finalize
-// should not cause any problems (everything should shut down/go away anyway
-// when the process exits).
-//
-// void ScriptInterpreterPythonImpl::Terminate() { Py_Finalize (); }
-
-#endif // LLDB_DISABLE_PYTHON
diff --git a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h b/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h
deleted file mode 100644
index 24941ec77452..000000000000
--- a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h
+++ /dev/null
@@ -1,57 +0,0 @@
-//===-- ScriptInterpreterPython.h -------------------------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H
-#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H
-
-#ifdef LLDB_DISABLE_PYTHON
-
-// Python is disabled in this build
-
-#else
-
-#include "lldb/Breakpoint/BreakpointOptions.h"
-#include "lldb/Core/IOHandler.h"
-#include "lldb/Interpreter/ScriptInterpreter.h"
-#include "lldb/lldb-private.h"
-
-#include <memory>
-#include <string>
-#include <vector>
-
-namespace lldb_private {
-/// Abstract interface for the Python script interpreter.
-class ScriptInterpreterPython : public ScriptInterpreter,
- public IOHandlerDelegateMultiline {
-public:
- class CommandDataPython : public BreakpointOptions::CommandData {
- public:
- CommandDataPython() : BreakpointOptions::CommandData() {
- interpreter = lldb::eScriptLanguagePython;
- }
- };
-
- ScriptInterpreterPython(Debugger &debugger)
- : ScriptInterpreter(debugger, lldb::eScriptLanguagePython),
- IOHandlerDelegateMultiline("DONE") {}
-
- static void Initialize();
- static void Terminate();
- static lldb_private::ConstString GetPluginNameStatic();
- static const char *GetPluginDescriptionStatic();
- static FileSpec GetPythonDir();
-
-protected:
- static void ComputePythonDirForApple(llvm::SmallVectorImpl<char> &path);
- static void ComputePythonDirForPosix(llvm::SmallVectorImpl<char> &path);
- static void ComputePythonDirForWindows(llvm::SmallVectorImpl<char> &path);
-};
-} // namespace lldb_private
-
-#endif // LLDB_DISABLE_PYTHON
-#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SCRIPTINTERPRETERPYTHON_H
diff --git a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h b/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
deleted file mode 100644
index a9993c4068a2..000000000000
--- a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
+++ /dev/null
@@ -1,473 +0,0 @@
-//===-- ScriptInterpreterPythonImpl.h ---------------------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifdef LLDB_DISABLE_PYTHON
-
-// Python is disabled in this build
-
-#else
-
-#include "lldb-python.h"
-
-#include "PythonDataObjects.h"
-#include "ScriptInterpreterPython.h"
-
-#include "lldb/Host/Terminal.h"
-#include "lldb/Utility/StreamString.h"
-
-#include "llvm/ADT/STLExtras.h"
-#include "llvm/ADT/StringRef.h"
-
-namespace lldb_private {
-class IOHandlerPythonInterpreter;
-class ScriptInterpreterPythonImpl : public ScriptInterpreterPython {
-public:
- friend class IOHandlerPythonInterpreter;
-
- ScriptInterpreterPythonImpl(Debugger &debugger);
-
- ~ScriptInterpreterPythonImpl() override;
-
- bool Interrupt() override;
-
- bool ExecuteOneLine(
- llvm::StringRef command, CommandReturnObject *result,
- const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
-
- void ExecuteInterpreterLoop() override;
-
- bool ExecuteOneLineWithReturn(
- llvm::StringRef in_string,
- ScriptInterpreter::ScriptReturnType return_type, void *ret_value,
- const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
-
- lldb_private::Status ExecuteMultipleLines(
- const char *in_string,
- const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
-
- Status
- ExportFunctionDefinitionToInterpreter(StringList &function_def) override;
-
- bool GenerateTypeScriptFunction(StringList &input, std::string &output,
- const void *name_token = nullptr) override;
-
- bool GenerateTypeSynthClass(StringList &input, std::string &output,
- const void *name_token = nullptr) override;
-
- bool GenerateTypeSynthClass(const char *oneliner, std::string &output,
- const void *name_token = nullptr) override;
-
- // use this if the function code is just a one-liner script
- bool GenerateTypeScriptFunction(const char *oneliner, std::string &output,
- const void *name_token = nullptr) override;
-
- bool GenerateScriptAliasFunction(StringList &input,
- std::string &output) override;
-
- StructuredData::ObjectSP
- CreateSyntheticScriptedProvider(const char *class_name,
- lldb::ValueObjectSP valobj) override;
-
- StructuredData::GenericSP
- CreateScriptCommandObject(const char *class_name) override;
-
- StructuredData::ObjectSP
- CreateScriptedThreadPlan(const char *class_name,
- lldb::ThreadPlanSP thread_plan) override;
-
- bool ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp,
- Event *event,
- bool &script_error) override;
-
- bool ScriptedThreadPlanShouldStop(StructuredData::ObjectSP implementor_sp,
- Event *event, bool &script_error) override;
-
- bool ScriptedThreadPlanIsStale(StructuredData::ObjectSP implementor_sp,
- bool &script_error) override;
-
- lldb::StateType
- ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp,
- bool &script_error) override;
-
- StructuredData::GenericSP
- CreateScriptedBreakpointResolver(const char *class_name,
- StructuredDataImpl *args_data,
- lldb::BreakpointSP &bkpt_sp) override;
- bool ScriptedBreakpointResolverSearchCallback(
- StructuredData::GenericSP implementor_sp,
- SymbolContext *sym_ctx) override;
-
- lldb::SearchDepth ScriptedBreakpointResolverSearchDepth(
- StructuredData::GenericSP implementor_sp) override;
-
- StructuredData::GenericSP
- CreateFrameRecognizer(const char *class_name) override;
-
- lldb::ValueObjectListSP
- GetRecognizedArguments(const StructuredData::ObjectSP &implementor,
- lldb::StackFrameSP frame_sp) override;
-
- StructuredData::GenericSP
- OSPlugin_CreatePluginObject(const char *class_name,
- lldb::ProcessSP process_sp) override;
-
- StructuredData::DictionarySP
- OSPlugin_RegisterInfo(StructuredData::ObjectSP os_plugin_object_sp) override;
-
- StructuredData::ArraySP
- OSPlugin_ThreadsInfo(StructuredData::ObjectSP os_plugin_object_sp) override;
-
- StructuredData::StringSP
- OSPlugin_RegisterContextData(StructuredData::ObjectSP os_plugin_object_sp,
- lldb::tid_t thread_id) override;
-
- StructuredData::DictionarySP
- OSPlugin_CreateThread(StructuredData::ObjectSP os_plugin_object_sp,
- lldb::tid_t tid, lldb::addr_t context) override;
-
- StructuredData::ObjectSP
- LoadPluginModule(const FileSpec &file_spec,
- lldb_private::Status &error) override;
-
- StructuredData::DictionarySP
- GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target,
- const char *setting_name,
- lldb_private::Status &error) override;
-
- size_t CalculateNumChildren(const StructuredData::ObjectSP &implementor,
- uint32_t max) override;
-
- lldb::ValueObjectSP
- GetChildAtIndex(const StructuredData::ObjectSP &implementor,
- uint32_t idx) override;
-
- int GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor,
- const char *child_name) override;
-
- bool UpdateSynthProviderInstance(
- const StructuredData::ObjectSP &implementor) override;
-
- bool MightHaveChildrenSynthProviderInstance(
- const StructuredData::ObjectSP &implementor) override;
-
- lldb::ValueObjectSP
- GetSyntheticValue(const StructuredData::ObjectSP &implementor) override;
-
- ConstString
- GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override;
-
- bool
- RunScriptBasedCommand(const char *impl_function, llvm::StringRef args,
- ScriptedCommandSynchronicity synchronicity,
- lldb_private::CommandReturnObject &cmd_retobj,
- Status &error,
- const lldb_private::ExecutionContext &exe_ctx) override;
-
- bool RunScriptBasedCommand(
- StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
- ScriptedCommandSynchronicity synchronicity,
- lldb_private::CommandReturnObject &cmd_retobj, Status &error,
- const lldb_private::ExecutionContext &exe_ctx) override;
-
- Status GenerateFunction(const char *signature,
- const StringList &input) override;
-
- Status GenerateBreakpointCommandCallbackData(StringList &input,
- std::string &output) override;
-
- bool GenerateWatchpointCommandCallbackData(StringList &input,
- std::string &output) override;
-
- bool GetScriptedSummary(const char *function_name, lldb::ValueObjectSP valobj,
- StructuredData::ObjectSP &callee_wrapper_sp,
- const TypeSummaryOptions &options,
- std::string &retval) override;
-
- void Clear() override;
-
- bool GetDocumentationForItem(const char *item, std::string &dest) override;
-
- bool GetShortHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp,
- std::string &dest) override;
-
- uint32_t
- GetFlagsForCommandObject(StructuredData::GenericSP cmd_obj_sp) override;
-
- bool GetLongHelpForCommandObject(StructuredData::GenericSP cmd_obj_sp,
- std::string &dest) override;
-
- bool CheckObjectExists(const char *name) override {
- if (!name || !name[0])
- return false;
- std::string temp;
- return GetDocumentationForItem(name, temp);
- }
-
- bool RunScriptFormatKeyword(const char *impl_function, Process *process,
- std::string &output, Status &error) override;
-
- bool RunScriptFormatKeyword(const char *impl_function, Thread *thread,
- std::string &output, Status &error) override;
-
- bool RunScriptFormatKeyword(const char *impl_function, Target *target,
- std::string &output, Status &error) override;
-
- bool RunScriptFormatKeyword(const char *impl_function, StackFrame *frame,
- std::string &output, Status &error) override;
-
- bool RunScriptFormatKeyword(const char *impl_function, ValueObject *value,
- std::string &output, Status &error) override;
-
- bool
- LoadScriptingModule(const char *filename, bool can_reload, bool init_session,
- lldb_private::Status &error,
- StructuredData::ObjectSP *module_sp = nullptr) override;
-
- bool IsReservedWord(const char *word) override;
-
- std::unique_ptr<ScriptInterpreterLocker> AcquireInterpreterLock() override;
-
- void CollectDataForBreakpointCommandCallback(
- std::vector<BreakpointOptions *> &bp_options_vec,
- CommandReturnObject &result) override;
-
- void
- CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options,
- CommandReturnObject &result) override;
-
- /// Set the callback body text into the callback for the breakpoint.
- Status SetBreakpointCommandCallback(BreakpointOptions *bp_options,
- const char *callback_body) override;
-
- void SetBreakpointCommandCallbackFunction(BreakpointOptions *bp_options,
- const char *function_name) override;
-
- /// This one is for deserialization:
- Status SetBreakpointCommandCallback(
- BreakpointOptions *bp_options,
- std::unique_ptr<BreakpointOptions::CommandData> &data_up) override;
-
- /// Set a one-liner as the callback for the watchpoint.
- void SetWatchpointCommandCallback(WatchpointOptions *wp_options,
- const char *oneliner) override;
-
- void ResetOutputFileHandle(FILE *new_fh) override;
-
- const char *GetDictionaryName() { return m_dictionary_name.c_str(); }
-
- PyThreadState *GetThreadState() { return m_command_thread_state; }
-
- void SetThreadState(PyThreadState *s) {
- if (s)
- m_command_thread_state = s;
- }
-
- // IOHandlerDelegate
- void IOHandlerActivated(IOHandler &io_handler, bool interactive) override;
-
- void IOHandlerInputComplete(IOHandler &io_handler,
- std::string &data) override;
-
- static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger);
-
- // PluginInterface protocol
- lldb_private::ConstString GetPluginName() override;
-
- uint32_t GetPluginVersion() override;
-
- class Locker : public ScriptInterpreterLocker {
- public:
- enum OnEntry {
- AcquireLock = 0x0001,
- InitSession = 0x0002,
- InitGlobals = 0x0004,
- NoSTDIN = 0x0008
- };
-
- enum OnLeave {
- FreeLock = 0x0001,
- FreeAcquiredLock = 0x0002, // do not free the lock if we already held it
- // when calling constructor
- TearDownSession = 0x0004
- };
-
- Locker(ScriptInterpreterPythonImpl *py_interpreter = nullptr,
- uint16_t on_entry = AcquireLock | InitSession,
- uint16_t on_leave = FreeLock | TearDownSession, FILE *in = nullptr,
- FILE *out = nullptr, FILE *err = nullptr);
-
- ~Locker() override;
-
- private:
- bool DoAcquireLock();
-
- bool DoInitSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err);
-
- bool DoFreeLock();
-
- bool DoTearDownSession();
-
- bool m_teardown_session;
- ScriptInterpreterPythonImpl *m_python_interpreter;
- // FILE* m_tmp_fh;
- PyGILState_STATE m_GILState;
- };
-
- static bool BreakpointCallbackFunction(void *baton,
- StoppointCallbackContext *context,
- lldb::user_id_t break_id,
- lldb::user_id_t break_loc_id);
- static bool WatchpointCallbackFunction(void *baton,
- StoppointCallbackContext *context,
- lldb::user_id_t watch_id);
- static void InitializePrivate();
-
- class SynchronicityHandler {
- private:
- lldb::DebuggerSP m_debugger_sp;
- ScriptedCommandSynchronicity m_synch_wanted;
- bool m_old_asynch;
-
- public:
- SynchronicityHandler(lldb::DebuggerSP, ScriptedCommandSynchronicity);
-
- ~SynchronicityHandler();
- };
-
- enum class AddLocation { Beginning, End };
-
- static void AddToSysPath(AddLocation location, std::string path);
-
- bool EnterSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err);
-
- void LeaveSession();
-
- void SaveTerminalState(int fd);
-
- void RestoreTerminalState();
-
- uint32_t IsExecutingPython() const { return m_lock_count > 0; }
-
- uint32_t IncrementLockCount() { return ++m_lock_count; }
-
- uint32_t DecrementLockCount() {
- if (m_lock_count > 0)
- --m_lock_count;
- return m_lock_count;
- }
-
- enum ActiveIOHandler {
- eIOHandlerNone,
- eIOHandlerBreakpoint,
- eIOHandlerWatchpoint
- };
-
- PythonObject &GetMainModule();
-
- PythonDictionary &GetSessionDictionary();
-
- PythonDictionary &GetSysModuleDictionary();
-
- bool GetEmbeddedInterpreterModuleObjects();
-
- bool SetStdHandle(File &file, const char *py_name, PythonFile &save_file,
- const char *mode);
-
- PythonFile m_saved_stdin;
- PythonFile m_saved_stdout;
- PythonFile m_saved_stderr;
- PythonObject m_main_module;
- PythonDictionary m_session_dict;
- PythonDictionary m_sys_module_dict;
- PythonObject m_run_one_line_function;
- PythonObject m_run_one_line_str_global;
- std::string m_dictionary_name;
- TerminalState m_terminal_state;
- ActiveIOHandler m_active_io_handler;
- bool m_session_is_active;
- bool m_pty_slave_is_open;
- bool m_valid_session;
- uint32_t m_lock_count;
- PyThreadState *m_command_thread_state;
-};
-
-class IOHandlerPythonInterpreter : public IOHandler {
-public:
- IOHandlerPythonInterpreter(Debugger &debugger,
- ScriptInterpreterPythonImpl *python)
- : IOHandler(debugger, IOHandler::Type::PythonInterpreter),
- m_python(python) {}
-
- ~IOHandlerPythonInterpreter() override {}
-
- ConstString GetControlSequence(char ch) override {
- if (ch == 'd')
- return ConstString("quit()\n");
- return ConstString();
- }
-
- void Run() override {
- if (m_python) {
- int stdin_fd = GetInputFD();
- if (stdin_fd >= 0) {
- Terminal terminal(stdin_fd);
- TerminalState terminal_state;
- const bool is_a_tty = terminal.IsATerminal();
-
- if (is_a_tty) {
- terminal_state.Save(stdin_fd, false);
- terminal.SetCanonical(false);
- terminal.SetEcho(true);
- }
-
- ScriptInterpreterPythonImpl::Locker locker(
- m_python,
- ScriptInterpreterPythonImpl::Locker::AcquireLock |
- ScriptInterpreterPythonImpl::Locker::InitSession |
- ScriptInterpreterPythonImpl::Locker::InitGlobals,
- ScriptInterpreterPythonImpl::Locker::FreeAcquiredLock |
- ScriptInterpreterPythonImpl::Locker::TearDownSession);
-
- // The following call drops into the embedded interpreter loop and
- // stays there until the user chooses to exit from the Python
- // interpreter. This embedded interpreter will, as any Python code that
- // performs I/O, unlock the GIL before a system call that can hang, and
- // lock it when the syscall has returned.
-
- // We need to surround the call to the embedded interpreter with calls
- // to PyGILState_Ensure and PyGILState_Release (using the Locker
- // above). This is because Python has a global lock which must be held
- // whenever we want to touch any Python objects. Otherwise, if the user
- // calls Python code, the interpreter state will be off, and things
- // could hang (it's happened before).
-
- StreamString run_string;
- run_string.Printf("run_python_interpreter (%s)",
- m_python->GetDictionaryName());
- PyRun_SimpleString(run_string.GetData());
-
- if (is_a_tty)
- terminal_state.Restore();
- }
- }
- SetIsDone(true);
- }
-
- void Cancel() override {}
-
- bool Interrupt() override { return m_python->Interrupt(); }
-
- void GotEOF() override {}
-
-protected:
- ScriptInterpreterPythonImpl *m_python;
-};
-
-} // namespace lldb_private
-
-#endif
diff --git a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h b/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h
deleted file mode 100644
index 884514da9924..000000000000
--- a/contrib/llvm/tools/lldb/source/Plugins/ScriptInterpreter/Python/lldb-python.h
+++ /dev/null
@@ -1,45 +0,0 @@
-//===-- lldb-python.h -------------------------------------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_LLDB_PYTHON_H
-#define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_LLDB_PYTHON_H
-
-// Python.h needs to be included before any system headers in order to avoid
-// redefinition of macros
-
-#ifdef LLDB_DISABLE_PYTHON
-// Python is disabled in this build
-#else
-#include "llvm/Support/Compiler.h"
-#if defined(_WIN32)
-// If anyone #includes Host/PosixApi.h later, it will try to typedef pid_t. We
-// need to ensure this doesn't happen. At the same time, Python.h will also try
-// to redefine a bunch of stuff that PosixApi.h defines. So define it all now
-// so that PosixApi.h doesn't redefine it.
-#define NO_PID_T
-#endif
-#if defined(__linux__)
-// features.h will define _POSIX_C_SOURCE if _GNU_SOURCE is defined. This value
-// may be different from the value that Python defines it to be which results
-// in a warning. Undefine _POSIX_C_SOURCE before including Python.h The same
-// holds for _XOPEN_SOURCE.
-#undef _POSIX_C_SOURCE
-#undef _XOPEN_SOURCE
-#endif
-
-// Include locale before Python so _PY_PORT_CTYPE_UTF8_ISSUE doesn't cause
-// macro redefinitions.
-#if defined(__APPLE__)
-#include <locale>
-#endif
-
-// Include python for non windows machines
-#include <Python.h>
-#endif // LLDB_DISABLE_PYTHON
-
-#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_LLDB_PYTHON_H