[Lldb/Lua] Generate Lua Bindings

This patch uses SWIG to generate the Lua bindings for the SB API. It
covers most of the API, but some methods require a type map similar to
Python.

Discussion on the mailing list:
http://lists.llvm.org/pipermail/lldb-dev/2019-December/015812.html

Differential revision: https://reviews.llvm.org/D71235
This commit is contained in:
Jonas Devlieghere 2019-12-08 15:32:57 -08:00
parent 2203089a60
commit bf03e17c57
8 changed files with 107 additions and 28 deletions

View File

@ -50,10 +50,10 @@ if (LLDB_ENABLE_PYTHON)
file(TO_CMAKE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH} LLDB_PYTHON_DEFAULT_RELATIVE_PATH)
set(LLDB_PYTHON_RELATIVE_PATH ${LLDB_PYTHON_DEFAULT_RELATIVE_PATH}
CACHE STRING "Path where Python modules are installed, relative to install prefix")
add_subdirectory(scripts)
endif ()
add_subdirectory(scripts)
# We need the headers generated by instrinsics_gen before we can compile
# any source file in LLDB as the imported Clang modules might include
# some of these generated headers. This approach is copied from Clang's main

View File

@ -27,31 +27,58 @@ else()
set(DARWIN_EXTRAS "")
endif()
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lldb.py
DEPENDS ${SWIG_SOURCES}
DEPENDS ${SWIG_INTERFACES}
DEPENDS ${SWIG_HEADERS}
COMMAND ${SWIG_EXECUTABLE}
-c++
-shadow
-python
-features autodoc
-threads
-I${LLDB_SOURCE_DIR}/include
-I${CMAKE_CURRENT_SOURCE_DIR}
-D__STDC_LIMIT_MACROS
-D__STDC_CONSTANT_MACROS
${DARWIN_EXTRAS}
-outdir ${CMAKE_CURRENT_BINARY_DIR}
-o ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
${LLDB_SOURCE_DIR}/scripts/lldb.swig
VERBATIM
COMMENT "Builds LLDB Python wrapper")
add_custom_target(swig_wrapper ALL DEPENDS
${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
${CMAKE_CURRENT_BINARY_DIR}/lldb.py
set(SWIG_COMMON_FLAGS
-c++
-features autodoc
-I${LLDB_SOURCE_DIR}/include
-I${CMAKE_CURRENT_SOURCE_DIR}
-D__STDC_LIMIT_MACROS
-D__STDC_CONSTANT_MACROS
${DARWIN_EXTRAS}
-outdir ${CMAKE_CURRENT_BINARY_DIR}
)
if (LLDB_ENABLE_PYTHON)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lldb.py
DEPENDS ${SWIG_SOURCES}
DEPENDS ${SWIG_INTERFACES}
DEPENDS ${SWIG_HEADERS}
COMMAND ${SWIG_EXECUTABLE}
${SWIG_COMMON_FLAGS}
-c++
-shadow
-python
-threads
-o ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
${LLDB_SOURCE_DIR}/scripts/lldb.swig
VERBATIM
COMMENT "Builds LLDB Python wrapper")
add_custom_target(swig_wrapper ALL DEPENDS
${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapPython.cpp
${CMAKE_CURRENT_BINARY_DIR}/lldb.py
)
endif()
if (LLDB_ENABLE_LUA)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapLua.cpp
DEPENDS ${SWIG_SOURCES}
DEPENDS ${SWIG_INTERFACES}
DEPENDS ${SWIG_HEADERS}
COMMAND ${SWIG_EXECUTABLE}
${SWIG_COMMON_FLAGS}
-lua
-w503
-o ${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapLua.cpp
${LLDB_SOURCE_DIR}/scripts/lldb_lua.swig
VERBATIM
COMMENT "Builds LLDB Lua wrapper")
add_custom_target(swig_wrapper_lua ALL DEPENDS
${CMAKE_CURRENT_BINARY_DIR}/LLDBWrapLua.cpp
)
endif()

View File

@ -0,0 +1,18 @@
/*
lldb.swig
This is the input file for SWIG, to create the appropriate C++ wrappers and
functions for various scripting languages, to enable them to call the
liblldb Script Bridge functions.
*/
%module lldb
%include "./headers.swig"
%{
using namespace lldb_private;
using namespace lldb;
%}
%include "./interfaces.swig"

View File

@ -9,6 +9,11 @@ if(LLDB_ENABLE_PYTHON)
set(lldb_python_wrapper ${lldb_scripts_dir}/LLDBWrapPython.cpp)
endif()
if(LLDB_ENABLE_LUA)
get_target_property(lldb_scripts_dir swig_wrapper_lua BINARY_DIR)
set(lldb_lua_wrapper ${lldb_scripts_dir}/LLDBWrapLua.cpp)
endif()
if(LLDB_BUILD_FRAMEWORK)
set(option_install_prefix INSTALL_PREFIX ${LLDB_FRAMEWORK_INSTALL_DIR})
set(option_framework FRAMEWORK)
@ -85,6 +90,7 @@ add_lldb_library(liblldb SHARED ${option_framework}
SBUnixSignals.cpp
SystemInitializerFull.cpp
${lldb_python_wrapper}
${lldb_lua_wrapper}
LINK_LIBS
lldbBase
@ -130,6 +136,19 @@ if(lldb_python_wrapper)
endif ()
endif()
if(lldb_lua_wrapper)
add_dependencies(liblldb swig_wrapper_lua)
target_include_directories(liblldb PRIVATE ${LUA_INCLUDE_DIR})
if (MSVC)
set_property(SOURCE ${lldb_lua_wrapper} APPEND_STRING PROPERTY COMPILE_FLAGS " /W0")
else()
set_property(SOURCE ${lldb_lua_wrapper} APPEND_STRING PROPERTY COMPILE_FLAGS " -w")
endif()
set_source_files_properties(${lldb_lua_wrapper} PROPERTIES GENERATED ON)
endif()
set_target_properties(liblldb
PROPERTIES
VERSION ${LLDB_VERSION}

View File

@ -1,3 +1,5 @@
find_package(Lua REQUIRED)
add_lldb_library(lldbPluginScriptInterpreterLua PLUGIN
Lua.cpp
ScriptInterpreterLua.cpp

View File

@ -16,11 +16,16 @@
namespace lldb_private {
extern "C" {
int luaopen_lldb(lua_State *L);
}
class Lua {
public:
Lua() : m_lua_state(luaL_newstate()) {
assert(m_lua_state);
luaL_openlibs(m_lua_state);
luaopen_lldb(m_lua_state);
}
~Lua() {

View File

@ -0,0 +1,6 @@
# REQUIRES: lua
# RUN: cat %s | %lldb --script-language lua 2>&1 | FileCheck %s
script
debugger = lldb.SBDebugger.Create()
print(string.format("debugger is valid: %s", debugger:IsValid()))
# CHECK: debugger is valid: true

View File

@ -11,6 +11,8 @@
using namespace lldb_private;
extern "C" int luaopen_lldb(lua_State *L) { return 0; }
TEST(LuaTest, RunValid) {
Lua lua;
llvm::Error error = lua.Run("foo = 1");