Skip to content

Reland "[lldb][api-test] Add API test for SBCommandInterpreter::Comm… #8884

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
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
18 changes: 17 additions & 1 deletion lldb/bindings/python/python-typemaps.swig
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
free($1);
}


// For Log::LogOutputCallback
%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) {
if (!($input == Py_None ||
Expand Down Expand Up @@ -476,6 +475,23 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
$1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input));
}

%typemap(in) (lldb::CommandOverrideCallback callback, void *baton) {
if (!($input == Py_None ||
PyCallable_Check(reinterpret_cast<PyObject *>($input)))) {
PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
SWIG_fail;
}

// Don't lose the callback reference.
Py_INCREF($input);
$1 = LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback;
$2 = $input;
}
%typemap(typecheck) (lldb::CommandOverrideCallback callback, void *baton) {
$1 = $input == Py_None;
$1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input));
}

%typemap(in) lldb::FileSP {
PythonFile py_file(PyRefType::Borrowed, $input);
if (!py_file) {
Expand Down
22 changes: 22 additions & 0 deletions lldb/bindings/python/python-wrapper.swig
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,28 @@ static void LLDBSwigPythonCallPythonSBDebuggerTerminateCallback(lldb::user_id_t
}
}

static bool LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback(void *baton, const char **argv) {
bool ret_val = false;
if (baton != Py_None) {
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
// Create a PyList of items since we're going to pass it to the callback as a tuple
// of arguments.
PyObject *py_argv = PyList_New(0);
for (const char **arg = argv; arg && *arg; arg++) {
std::string arg_string = *arg;
PyObject *py_string = PyUnicode_FromStringAndSize(arg_string.c_str(), arg_string.size());
PyList_Append(py_argv, py_string);
}

PyObject *result = PyObject_CallObject(
reinterpret_cast<PyObject *>(baton), PyList_AsTuple(py_argv));
ret_val = result ? PyObject_IsTrue(result) : false;
Py_XDECREF(result);
SWIG_PYTHON_THREAD_END_BLOCK;
}
return ret_val;
}

static SBError LLDBSwigPythonCallLocateModuleCallback(
void *callback_baton, const SBModuleSpec &module_spec_sb,
SBFileSpec &module_file_spec_sb, SBFileSpec &symbol_file_spec_sb) {
Expand Down
75 changes: 75 additions & 0 deletions lldb/bindings/python/static-binding/LLDBWrapPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5947,6 +5947,28 @@ static void LLDBSwigPythonCallPythonSBDebuggerTerminateCallback(lldb::user_id_t
}
}

static bool LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback(void *baton, const char **argv) {
bool ret_val = false;
if (baton != Py_None) {
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
// Create a PyList of items since we're going to pass it to the callback as a tuple
// of arguments.
PyObject *py_argv = PyList_New(0);
for (const char **arg = argv; arg && *arg; arg++) {
std::string arg_string = *arg;
PyObject *py_string = PyUnicode_FromStringAndSize(arg_string.c_str(), arg_string.size());
PyList_Append(py_argv, py_string);
}

PyObject *result = PyObject_CallObject(
reinterpret_cast<PyObject *>(baton), PyList_AsTuple(py_argv));
ret_val = result ? PyObject_IsTrue(result) : false;
Py_XDECREF(result);
SWIG_PYTHON_THREAD_END_BLOCK;
}
return ret_val;
}

static SBError LLDBSwigPythonCallLocateModuleCallback(
void *callback_baton, const SBModuleSpec &module_spec_sb,
SBFileSpec &module_file_spec_sb, SBFileSpec &symbol_file_spec_sb) {
Expand Down Expand Up @@ -16511,6 +16533,58 @@ SWIGINTERN PyObject *_wrap_SBCommandInterpreter_InterruptCommand(PyObject *self,
}


SWIGINTERN PyObject *_wrap_SBCommandInterpreter_SetCommandOverrideCallback(PyObject *self, PyObject *args) {
PyObject *resultobj = 0;
lldb::SBCommandInterpreter *arg1 = (lldb::SBCommandInterpreter *) 0 ;
char *arg2 = (char *) 0 ;
lldb::CommandOverrideCallback arg3 = (lldb::CommandOverrideCallback) 0 ;
void *arg4 = (void *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
int res2 ;
char *buf2 = 0 ;
int alloc2 = 0 ;
PyObject *swig_obj[3] ;
bool result;

(void)self;
if (!SWIG_Python_UnpackTuple(args, "SBCommandInterpreter_SetCommandOverrideCallback", 3, 3, swig_obj)) SWIG_fail;
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_lldb__SBCommandInterpreter, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "SBCommandInterpreter_SetCommandOverrideCallback" "', argument " "1"" of type '" "lldb::SBCommandInterpreter *""'");
}
arg1 = reinterpret_cast< lldb::SBCommandInterpreter * >(argp1);
res2 = SWIG_AsCharPtrAndSize(swig_obj[1], &buf2, NULL, &alloc2);
if (!SWIG_IsOK(res2)) {
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "SBCommandInterpreter_SetCommandOverrideCallback" "', argument " "2"" of type '" "char const *""'");
}
arg2 = reinterpret_cast< char * >(buf2);
{
if (!(swig_obj[2] == Py_None ||
PyCallable_Check(reinterpret_cast<PyObject *>(swig_obj[2])))) {
PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
SWIG_fail;
}

// Don't lose the callback reference.
Py_INCREF(swig_obj[2]);
arg3 = LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback;
arg4 = swig_obj[2];
}
{
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
result = (bool)(arg1)->SetCommandOverrideCallback((char const *)arg2,arg3,arg4);
SWIG_PYTHON_THREAD_END_ALLOW;
}
resultobj = SWIG_From_bool(static_cast< bool >(result));
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
return resultobj;
fail:
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
return NULL;
}


SWIGINTERN PyObject *_wrap_SBCommandInterpreter_IsActive(PyObject *self, PyObject *args) {
PyObject *resultobj = 0;
lldb::SBCommandInterpreter *arg1 = (lldb::SBCommandInterpreter *) 0 ;
Expand Down Expand Up @@ -92259,6 +92333,7 @@ static PyMethodDef SwigMethods[] = {
{ "SBCommandInterpreter_HandleCompletionWithDescriptions", _wrap_SBCommandInterpreter_HandleCompletionWithDescriptions, METH_VARARGS, "SBCommandInterpreter_HandleCompletionWithDescriptions(SBCommandInterpreter self, char const * current_line, uint32_t cursor_pos, int match_start_point, int max_return_elements, SBStringList matches, SBStringList descriptions) -> int"},
{ "SBCommandInterpreter_WasInterrupted", _wrap_SBCommandInterpreter_WasInterrupted, METH_O, "SBCommandInterpreter_WasInterrupted(SBCommandInterpreter self) -> bool"},
{ "SBCommandInterpreter_InterruptCommand", _wrap_SBCommandInterpreter_InterruptCommand, METH_O, "SBCommandInterpreter_InterruptCommand(SBCommandInterpreter self) -> bool"},
{ "SBCommandInterpreter_SetCommandOverrideCallback", _wrap_SBCommandInterpreter_SetCommandOverrideCallback, METH_VARARGS, "SBCommandInterpreter_SetCommandOverrideCallback(SBCommandInterpreter self, char const * command_name, lldb::CommandOverrideCallback callback) -> bool"},
{ "SBCommandInterpreter_IsActive", _wrap_SBCommandInterpreter_IsActive, METH_O, "SBCommandInterpreter_IsActive(SBCommandInterpreter self) -> bool"},
{ "SBCommandInterpreter_GetIOHandlerControlSequence", _wrap_SBCommandInterpreter_GetIOHandlerControlSequence, METH_VARARGS, "SBCommandInterpreter_GetIOHandlerControlSequence(SBCommandInterpreter self, char ch) -> char const *"},
{ "SBCommandInterpreter_GetPromptOnQuit", _wrap_SBCommandInterpreter_GetPromptOnQuit, METH_O, "SBCommandInterpreter_GetPromptOnQuit(SBCommandInterpreter self) -> bool"},
Expand Down
4 changes: 4 additions & 0 deletions lldb/bindings/python/static-binding/lldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3392,6 +3392,10 @@ def InterruptCommand(self):
r"""InterruptCommand(SBCommandInterpreter self) -> bool"""
return _lldb.SBCommandInterpreter_InterruptCommand(self)

def SetCommandOverrideCallback(self, command_name, callback):
r"""SetCommandOverrideCallback(SBCommandInterpreter self, char const * command_name, lldb::CommandOverrideCallback callback) -> bool"""
return _lldb.SBCommandInterpreter_SetCommandOverrideCallback(self, command_name, callback)

def IsActive(self):
r"""IsActive(SBCommandInterpreter self) -> bool"""
return _lldb.SBCommandInterpreter_IsActive(self)
Expand Down
2 changes: 0 additions & 2 deletions lldb/include/lldb/API/SBCommandInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,9 @@ class SBCommandInterpreter {
// Catch commands before they execute by registering a callback that will get
// called when the command gets executed. This allows GUI or command line
// interfaces to intercept a command and stop it from happening
#ifndef SWIG
bool SetCommandOverrideCallback(const char *command_name,
lldb::CommandOverrideCallback callback,
void *baton);
#endif

/// Return true if the command interpreter is the active IO handler.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class CommandOverrideCallback(TestBase):
def setUp(self):
TestBase.setUp(self)
self.line = line_number("main.c", "Hello world.")

def test_command_override_callback(self):
self.build()
exe = self.getBuildArtifact("a.out")

target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)

ci = self.dbg.GetCommandInterpreter()
self.assertTrue(ci, VALID_COMMAND_INTERPRETER)

command_arg = ""

def foo(*command_args):
nonlocal command_arg
command_arg = command_args[0]

self.assertTrue(ci.SetCommandOverrideCallback("breakpoint set", foo))
self.expect("breakpoint set -n main")
self.assertTrue(command_arg == "breakpoint")