Skip to content

Commit c60a3ef

Browse files
committed
engine: Add LRDB debugging support
- Add slimmed down LRDB library to the engine/vendor/lrdb directory. - It can be compiled in or out via option (in by default) - The command-line flags are --debug-lua and --debug-lua-port
1 parent d182d03 commit c60a3ef

27 files changed

+3401
-4
lines changed

NOTICE.md

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ The following third-party libraries are included in the Cloe repository:
7070
- Website: https://github.com/kikito/inspect.lua
7171
- Source: engine/lua/inspect.lua
7272

73+
- LRDB
74+
- License: BSL-1.0
75+
- License-Source: https://www.boost.org/LICENSE_1_0.txt
76+
- Website: https://github.com/satoren/LRDB
77+
- Source: engine/vendor/lrdb
78+
7379
- Lust
7480
- License: MIT
7581
- License-Source: https://raw.githubusercontent.com/bjornbytes/lust/master/LICENSE

conanfile.py

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Cloe(ConanFile):
5050
"fPIC": [True, False],
5151
"fable_allow_comments": [True, False],
5252
"engine_server": [True, False],
53+
"engine_lrdb": [True, False],
5354
"with_esmini": [True, False],
5455
"with_vtd": [True, False],
5556
}
@@ -58,6 +59,7 @@ class Cloe(ConanFile):
5859
"fPIC": True,
5960
"fable_allow_comments": True,
6061
"engine_server": True,
62+
"engine_lrdb": True,
6163
"with_esmini": True,
6264
"with_vtd": False,
6365
}
@@ -134,6 +136,7 @@ def generate(self):
134136
tc.cache_variables["CLOE_VERSION"] = self.version
135137
tc.cache_variables["CLOE_VERSION_U32"] = version_u32
136138
tc.cache_variables["CLOE_ENGINE_WITH_SERVER"] = self.options.engine_server
139+
tc.cache_variables["CLOE_ENGINE_WITH_LRDB"] = self.options.engine_lrdb
137140
tc.cache_variables["CLOE_WITH_ESMINI"] = self.options.with_esmini
138141
tc.cache_variables["CLOE_WITH_VTD"] = self.options.with_vtd
139142
tc.generate()

docs/reference/lua-initialization.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ by a Lua file: `cloe-engine run simulation.lua`
1212

1313
- Lua package path (`--lua-path`, `CLOE_LUA_PATH`)
1414
- Disable system packages (`--no-system-lua`)
15+
- Enable LRDB Lua debugger (`--debug-lua`)
1516
- Cloe plugins (`--plugin-path`, `CLOE_PLUGIN_PATH`)
1617

1718
2. Initialize Cloe Stack
@@ -25,11 +26,13 @@ by a Lua file: `cloe-engine run simulation.lua`
2526
- Expose Cloe API via `cloe` Lua table
2627
- Load Cloe Lua runtime (located in the package `lib/cloe/lua` directory)
2728

28-
4. Source input files
29+
4. Start LRDB Lua debugger (Optional)
30+
31+
5. Source input files
2932

3033
- Files ending with `.lua` are merged as Lua
3134
- Other files are read as JSON
3235

33-
5. Start simulation
36+
6. Start simulation
3437

3538
- Schedule triggers pending from the Lua script

engine/CMakeLists.txt

+12-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ set_target_properties(cloe-enginelib PROPERTIES
123123
target_compile_definitions(cloe-enginelib
124124
PUBLIC
125125
SOL_ALL_SAFETIES_ON=1
126+
LRDB_USE_BOOST_ASIO=1
126127
CLOE_ENGINE_VERSION="${CLOE_ENGINE_VERSION}"
127128
CLOE_ENGINE_TIMESTAMP="${CLOE_ENGINE_TIMESTAMP}"
128129
PROJECT_SOURCE_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}\"
@@ -155,6 +156,16 @@ else()
155156
target_compile_definitions(cloe-enginelib PUBLIC CLOE_ENGINE_WITH_SERVER=0)
156157
endif()
157158

159+
option(CLOE_ENGINE_WITH_LRDB "Enable LRDB Lua Debugger?" ON)
160+
if(CLOE_ENGINE_WITH_LRDB)
161+
add_subdirectory(vendor/lrdb)
162+
target_sources(cloe-enginelib PRIVATE src/lua_debugger.cpp)
163+
target_link_libraries(cloe-enginelib PRIVATE lrdb::lrdb)
164+
target_compile_definitions(cloe-enginelib PUBLIC CLOE_ENGINE_WITH_LRDB=1)
165+
else()
166+
target_compile_definitions(cloe-enginelib PUBLIC CLOE_ENGINE_WITH_LRDB=0)
167+
endif()
168+
158169
if(BUILD_TESTING)
159170
message(STATUS "Building test-enginelib executable.")
160171
add_executable(test-enginelib
@@ -176,7 +187,7 @@ if(BUILD_TESTING)
176187
endif()
177188

178189
# Executable ---------------------------------------------------------
179-
message(STATUS "Building cloe-engine executable [with server=${CLOE_ENGINE_WITH_SERVER}].")
190+
message(STATUS "Building cloe-engine executable [with server=${CLOE_ENGINE_WITH_SERVER}, lrdb=${CLOE_ENGINE_WITH_LRDB}].")
180191
add_subdirectory(vendor/linenoise)
181192
add_executable(cloe-engine
182193
src/main.cpp

engine/conanfile.py

+5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ class CloeEngine(ConanFile):
2121
# server dependencies are incompatible with your target system.
2222
"server": [True, False],
2323

24+
# Whether the LRDB integration is compiled and built into the Cloe engine.
25+
"lrdb": [True, False],
26+
2427
# Make the compiler be strict and pedantic.
2528
# Disable if you upgrade compilers and run into new warnings preventing
2629
# the build from completing. May be removed in the future.
2730
"pedantic": [True, False],
2831
}
2932
default_options = {
33+
"lrdb": True,
3034
"server": True,
3135
"pedantic": True,
3236

@@ -72,6 +76,7 @@ def generate(self):
7276
tc.cache_variables["CMAKE_EXPORT_COMPILE_COMMANDS"] = True
7377
tc.cache_variables["CLOE_PROJECT_VERSION"] = self.version
7478
tc.cache_variables["CLOE_ENGINE_WITH_SERVER"] = self.options.server
79+
tc.cache_variables["CLOE_ENGINE_WITH_LRDB"] = self.options.lrdb
7580
tc.cache_variables["TargetLintingExtended"] = self.options.pedantic
7681
tc.generate()
7782

engine/lua/cloe-engine/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ local engine = {
5656
["cloe-stackfile-4.1"] = true,
5757

5858
["cloe-server"] = false,
59+
["cloe-lrdb"] = false,
5960
},
6061

6162
--- @type table Lua table dumped as JSON report at end of simulation.

engine/src/lua_debugger.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2023 Robert Bosch GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
/**
19+
* \file stack_lua.cpp
20+
*/
21+
22+
#include "lua_setup.hpp"
23+
24+
#include <lrdb/server.hpp> // lrdb::server
25+
#include <sol/state_view.hpp> // for state_view
26+
27+
namespace cloe {
28+
29+
void start_lua_debugger(sol::state& lua, int listen_port) {
30+
static lrdb::server debug_server(listen_port);
31+
debug_server.reset(lua.lua_state());
32+
}
33+
34+
} // namespace cloe

engine/src/lua_setup.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ void register_cloe_engine(sol::state_view& lua, Stack& stack) {
198198
"cloe-stackfile-4.1", true,
199199

200200
// Server enabled:
201-
"cloe-server", CLOE_ENGINE_WITH_SERVER != 0
201+
"cloe-server", CLOE_ENGINE_WITH_SERVER != 0,
202+
"cloe-lrdb", CLOE_ENGINE_WITH_LRDB != 0
202203
);
203204
// clang-format on
204205

engine/src/lua_setup.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ struct LuaOptions {
5858
*/
5959
sol::state new_lua(const LuaOptions& opt, Stack& s);
6060

61+
#if CLOE_ENGINE_WITH_LRDB
62+
/**
63+
* Start Lua debugger server on port.
64+
*
65+
* \param lua
66+
* \param listen_port
67+
*/
68+
void start_lua_debugger(sol::state& lua, int listen_port);
69+
#endif
70+
6171
/**
6272
* Merge the provided Lua file into the existing `Stack`, respecting `StackOptions`.
6373
*

engine/src/main.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ int main(int argc, char** argv) {
7979
run->add_flag("--require-success,!--no-require-success", run_options.require_success,
8080
"Require simulation success")
8181
->envname("CLOE_REQUIRE_SUCCESS");
82+
run->add_flag("--debug-lua", run_options.debug_lua,
83+
"Debug the Lua simulation");
84+
run->add_option("--debug-lua-port", run_options.debug_lua_port,
85+
"Port to listen on for debugger to attach to")
86+
->envname("CLOE_DEBUG_LUA_PORT");
8287
run->add_option("files", run_files, "Files to merge into a single stackfile")->required();
8388

8489
// One of the above subcommands must be used.

engine/src/main_commands.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ struct RunOptions {
7575
bool write_output = true;
7676
bool require_success = false;
7777
bool report_progress = true;
78+
79+
bool debug_lua = false;
80+
int debug_lua_port = 21110;
7881
};
7982

8083
int run(const RunOptions& opt, const std::vector<std::string>& filepaths);

engine/src/main_run.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ int run(const RunOptions& opt, const std::vector<std::string>& filepaths) {
6464
// Load the stack file:
6565
cloe::Stack stack = cloe::new_stack(opt.stack_options);
6666
sol::state lua = cloe::new_lua(opt.lua_options, stack);
67+
#if CLOE_ENGINE_WITH_LRDB
68+
if (opt.debug_lua) {
69+
log->info("Lua debugger listening at port: {}", opt.debug_lua_port);
70+
cloe::start_lua_debugger(lua, opt.debug_lua_port);
71+
}
72+
#else
73+
if (opt.debug_lua) {
74+
log->error("Lua debugger feature not available.");
75+
}
76+
#endif
6777
try {
6878
cloe::conclude_error(*opt.stack_options.error, [&]() {
6979
for (const auto& file : filepaths) {

engine/src/main_version.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int version(const VersionOptions& opt) {
4545
{"stack", CLOE_STACK_VERSION}, // from "stack.hpp"
4646
{"plugin_manifest", CLOE_PLUGIN_MANIFEST_VERSION}, // from <cloe/plugin.hpp>
4747
{"feature_server", CLOE_ENGINE_WITH_SERVER != 0}, // from CMakeLists.txt
48+
{"feature_lrdb", CLOE_ENGINE_WITH_LRDB != 0}, // from CMakeLists.txt
4849
};
4950

5051
if (opt.output_json) {

engine/vendor/lrdb/CMakeLists.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
2+
3+
project(LRDB LANGUAGES CXX)
4+
5+
add_library(lrdb INTERFACE)
6+
add_library(lrdb::lrdb ALIAS lrdb)
7+
target_include_directories(lrdb
8+
INTERFACE
9+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
10+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/picojson>"
11+
)
12+
target_link_libraries(lrdb
13+
INTERFACE
14+
lua::lua
15+
)
16+
set_target_properties(lrdb PROPERTIES
17+
CXX_STANDARD 17
18+
CXX_STANDARD_REQUIRED ON
19+
)

engine/vendor/lrdb/LICENSE_1_0.txt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Boost Software License - Version 1.0 - August 17th, 2003
2+
3+
Permission is hereby granted, free of charge, to any person or organization
4+
obtaining a copy of the software and accompanying documentation covered by
5+
this license (the "Software") to use, reproduce, display, distribute,
6+
execute, and transmit the Software, and to prepare derivative works of the
7+
Software, and to permit third-parties to whom the Software is furnished to
8+
do so, all subject to the following:
9+
10+
The copyright notices in the Software and this entire statement, including
11+
the above license grant, this restriction and the following disclaimer,
12+
must be included in all copies of the Software, in whole or in part, and
13+
all derivative works of the Software, unless such copies or derivative
14+
works are solely in the form of machine-executable object code generated by
15+
a source language processor.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20+
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21+
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
DEALINGS IN THE SOFTWARE.

engine/vendor/lrdb/NOTICE

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
LRDB Modifications
2+
==================
3+
4+
The LRDB library is sourced from the GitHub repository below:
5+
6+
- License: BSL-1.0
7+
- License-Source: https://www.boost.org/LICENSE_1_0.txt
8+
- Website: https://github.com/satoren/LRDB
9+
10+
The source code has been modified in following ways:
11+
12+
- Remove files not relevant to our use (e.g. test, node, cmake).
13+
- Replace include/lrdb/debugger.hpp implementation of is_file_path_match.
14+
- Replace CMakeLists.txt with a simplified version.

0 commit comments

Comments
 (0)