Skip to content

[lldb] Convert script native types to StructuredData counterpart #7095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lldb/bindings/headers.swig
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "lldb/API/SBQueue.h"
#include "lldb/API/SBQueueItem.h"
#include "lldb/API/SBReproducer.h"
#include "lldb/API/SBScriptObject.h"
#include "lldb/API/SBSection.h"
#include "lldb/API/SBSourceManager.h"
#include "lldb/API/SBStream.h"
Expand Down
2 changes: 1 addition & 1 deletion lldb/bindings/interface/SBProcess.i
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public:
%feature("autodoc", "
Returns the implementation object of the process plugin if available. None
otherwise.") GetScriptedImplementation;
ScriptedObject
lldb::SBScriptObject
GetScriptedImplementation();

%feature("autodoc", "
Expand Down
51 changes: 51 additions & 0 deletions lldb/bindings/interface/SBScriptObject.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===-- SWIG Interface for SBScriptObject -----------------------*- 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
//
//===----------------------------------------------------------------------===//

namespace lldb {

class SBScriptObject
{
public:
SBScriptObject(const ScriptObjectPtr ptr, lldb::ScriptLanguage lang);

SBScriptObject(const lldb::SBScriptObject &rhs);

~SBScriptObject();

const lldb::SBScriptObject &operator=(const lldb::SBScriptObject &rhs);

explicit operator bool() const;

bool operator!=(const SBScriptObject &rhs) const;

bool IsValid() const;

lldb::ScriptObjectPtr GetPointer() const;

lldb::ScriptLanguage GetLanguage() const;

#ifdef SWIGPYTHON
// operator== is a free function, which swig does not handle, so we inject
// our own equality operator here
%pythoncode%{
def __eq__(self, other):
return not self.__ne__(other)
%}
#endif


#ifdef SWIGPYTHON
%pythoncode %{
ptr = property(GetPointer, None, doc='''A read only property that returns the underlying script object.''')
lang = property(GetLanguage, None, doc='''A read only property that returns the script language associated with with this script object.''')
%}
#endif

};

}
5 changes: 5 additions & 0 deletions lldb/bindings/interface/SBStructuredData.i
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ This class wraps the event type generated by StructuredData features."

SBStructuredData(const lldb::EventSP &event_sp);

SBStructuredData(const lldb::SBScriptObject obj,
const lldb::SBDebugger &debugger);

~SBStructuredData();

bool
Expand Down Expand Up @@ -50,6 +53,8 @@ This class wraps the event type generated by StructuredData features."

size_t GetStringValue(char *dst, size_t dst_len) const;

lldb::SBScriptObject GetGenericValue() const;

lldb::SBError
GetAsJSON(lldb::SBStream &stream) const;

Expand Down
1 change: 1 addition & 0 deletions lldb/bindings/interfaces.swig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
%include "./interface/SBQueue.i"
%include "./interface/SBQueueItem.i"
%include "./interface/SBReproducer.i"
%include "./interface/SBScriptObject.i"
%include "./interface/SBSection.i"
%include "./interface/SBSourceManager.i"
%include "./interface/SBStream.i"
Expand Down
73 changes: 71 additions & 2 deletions lldb/bindings/python/python-typemaps.swig
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,78 @@
free((char *) $1);
}

%typemap(out) lldb::ScriptedObject {
%typecheck(SWIG_TYPECHECK_POINTER) lldb::ScriptObjectPtr {
PythonObject obj(PyRefType::Borrowed, $input);
if (!obj.IsValid()) {
PyErr_Clear();
$1 = 0;
} else {
$1 = 1;
}
}

%typemap(in) lldb::ScriptObjectPtr {
if ($input == Py_None) {
$1 = nullptr;
} else {
PythonObject obj(PyRefType::Borrowed, $input);
if (!obj.IsValid()) {
PyErr_SetString(PyExc_TypeError, "Script object is not valid");
SWIG_fail;
}

auto lldb_module = PythonModule::Import("lldb");
if (!lldb_module) {
std::string err_msg = llvm::toString(lldb_module.takeError());
PyErr_SetString(PyExc_TypeError, err_msg.c_str());
SWIG_fail;
}

auto sb_structured_data_class = lldb_module.get().Get("SBStructuredData");
if (!sb_structured_data_class) {
std::string err_msg = llvm::toString(sb_structured_data_class.takeError());
PyErr_SetString(PyExc_TypeError, err_msg.c_str());
SWIG_fail;
}

if (obj.IsInstance(sb_structured_data_class.get())) {
$1 = obj.get();
} else {
auto type = obj.GetType();
if (!type) {
std::string err_msg = llvm::toString(type.takeError());
PyErr_SetString(PyExc_TypeError, err_msg.c_str());
SWIG_fail;
}

auto type_name = As<std::string>(type.get().GetAttribute("__name__"));
if (!type_name) {
std::string err_msg = llvm::toString(type_name.takeError());
PyErr_SetString(PyExc_TypeError, err_msg.c_str());
SWIG_fail;
}

if (llvm::StringRef(type_name.get()).startswith("SB")) {
std::string error_msg = "Input type is invalid: " + type_name.get();
PyErr_SetString(PyExc_TypeError, error_msg.c_str());
SWIG_fail;
} else {
$1 = obj.get();
}
}
}
}

%typemap(out) lldb::ScriptObjectPtr {
$result = (PyObject*) $1;
if (!$result)
$result = Py_None;
Py_INCREF($result);
}

%typemap(out) lldb::SBScriptObject {
$result = nullptr;
if (const void* impl = $1)
if (const void* impl = $1.GetPointer())
$result = (PyObject*) impl;
if (!$result) {
$result = Py_None;
Expand Down
Loading