Skip to content

Commit 3a6b33d

Browse files
authored
feat(bb): runtime stack traces in debug mode (#16590)
Until the next rebuild of the base image, this means you do need to do: ``` sudo apt-get install libdw-dev libelf-dev ``` on ubuntu (e.g. our mainframe accounts) to be able to build debug or debug-fast. but now the recommended flow is to always use debug-fast, and you'll always have a detailed stack trace
1 parent 2cce3d0 commit 3a6b33d

File tree

9 files changed

+53
-14
lines changed

9 files changed

+53
-14
lines changed

barretenberg/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ If you are in a scenario where you have a failing call to check_circuit and wish
326326

327327
Usage instructions:
328328

329-
- On ubuntu (or our mainframe accounts) use `sudo apt-get install libdw-dev libelf-dev libbackward-cpp-dev` to support trace printing
329+
- On ubuntu (or our mainframe accounts) use `sudo apt-get install libdw-dev libelf-dev` to support trace printing
330330
- Use `cmake --preset debug-fast-circuit-check-traces` and `cmake --build --preset debug-fast-circuit-check-traces` to enable the backward-cpp dependency through the CHECK_CIRCUIT_STACKTRACES CMake variable.
331331
- Run any case where you have a failing check_circuit call, you will now have a stack trace illuminating where this constraint was added in code.
332332

barretenberg/cpp/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ option(ENABLE_ASAN "Address sanitizer for debugging tricky memory corruption" OF
3030
option(ENABLE_HEAVY_TESTS "Enable heavy tests when collecting coverage" OFF)
3131
# Note: Must do 'sudo apt-get install libdw-dev' or equivalent
3232
option(CHECK_CIRCUIT_STACKTRACES "Enable (slow) stack traces for check circuit" OFF)
33+
option(ENABLE_STACKTRACES "Enable stack traces on assertion" OFF)
3334
option(ENABLE_TRACY "Enable low-medium overhead profiling for memory and performance with tracy" OFF)
3435
option(ENABLE_PIC "Builds with position independent code" OFF)
3536
option(SYNTAX_ONLY "only check syntax (-fsyntax-only)" OFF)
@@ -44,7 +45,9 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "a
4445
endif()
4546

4647
if(CHECK_CIRCUIT_STACKTRACES)
47-
add_compile_options(-DCHECK_CIRCUIT_STACKTRACES)
48+
add_compile_options(-DCHECK_CIRCUIT_STACKTRACES -DSTACKTRACES)
49+
elseif(ENABLE_STACKTRACES)
50+
add_compile_options(-DSTACKTRACES)
4851
endif()
4952

5053
if(ENABLE_TRACY OR ENABLE_TRACY_TIME_INSTRUMENTED)

barretenberg/cpp/CMakePresets.json

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@
126126
"cacheVariables": {
127127
"CMAKE_BUILD_TYPE": "Debug",
128128
"ENABLE_ASAN": "OFF",
129-
"DISABLE_ASM": "ON"
129+
"DISABLE_ASM": "ON",
130+
"ENABLE_STACKTRACES": "ON"
130131
}
131132
},
132133
{
@@ -193,6 +194,9 @@
193194
"CFLAGS": "-O2 -gdwarf",
194195
"CXXFLAGS": "-O2 -gdwarf-4",
195196
"LDFLAGS": "-O2 -gdwarf-4"
197+
},
198+
"cacheVariables": {
199+
"ENABLE_STACKTRACES": "ON"
196200
}
197201
},
198202
{
@@ -206,13 +210,25 @@
206210
}
207211
},
208212
{
209-
"name": "debug-no-avm",
213+
"name": "debug-fast-no-avm",
210214
"displayName": "Optimized debug build with Clang-20 (no AVM)",
211215
"description": "Build with globally installed Clang-20 in debug mode excluding the Aztec VM",
216+
"inherits": "debug-fast",
217+
"binaryDir": "build-debug-fast-no-avm",
218+
"cacheVariables": {
219+
"DISABLE_AZTEC_VM": "ON",
220+
"ENABLE_STACKTRACES": "ON"
221+
}
222+
},
223+
{
224+
"name": "debug-no-avm",
225+
"displayName": "Debug build with Clang-20 (no AVM)",
226+
"description": "Build with globally installed Clang-20 in debug mode excluding the Aztec VM",
212227
"inherits": "debug",
213228
"binaryDir": "build-debug-no-avm",
214229
"cacheVariables": {
215-
"DISABLE_AZTEC_VM": "ON"
230+
"DISABLE_AZTEC_VM": "ON",
231+
"ENABLE_STACKTRACES": "ON"
216232
}
217233
},
218234
{
@@ -234,7 +250,8 @@
234250
"cacheVariables": {
235251
"ENABLE_ASAN": "ON",
236252
"DISABLE_AZTEC_VM": "ON",
237-
"DISABLE_ASM": "ON"
253+
"DISABLE_ASM": "ON",
254+
"ENABLE_STACKTRACES": "OFF"
238255
}
239256
},
240257
{
@@ -245,7 +262,8 @@
245262
"binaryDir": "build-asan",
246263
"cacheVariables": {
247264
"ENABLE_ASAN": "ON",
248-
"DISABLE_ASM": "ON"
265+
"DISABLE_ASM": "ON",
266+
"ENABLE_STACKTRACES": "OFF"
249267
}
250268
},
251269
{
@@ -503,6 +521,11 @@
503521
"inherits": "default",
504522
"configurePreset": "clang20-no-avm"
505523
},
524+
{
525+
"name": "debug-fast-no-avm",
526+
"inherits": "default",
527+
"configurePreset": "debug-fast-no-avm"
528+
},
506529
{
507530
"name": "debug-no-avm",
508531
"inherits": "default",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if(CHECK_CIRCUIT_STACKTRACES)
1+
if(CHECK_CIRCUIT_STACKTRACES OR ENABLE_STACKTRACES)
22
include(FetchContent)
33

44
# Also requires one of: libbfd (gnu binutils), libdwarf, libdw (elfutils)
@@ -8,4 +8,4 @@ if(CHECK_CIRCUIT_STACKTRACES)
88
SYSTEM # optional, the Backward include directory will be treated as system directory
99
)
1010
FetchContent_MakeAvailable(backward)
11-
endif()
11+
endif()

barretenberg/cpp/cmake/module.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function(barretenberg_module MODULE_NAME)
5757
${TBB_IMPORTED_TARGETS}
5858
)
5959

60-
if(CHECK_CIRCUIT_STACKTRACES)
60+
if(CHECK_CIRCUIT_STACKTRACES OR ENABLE_STACKTRACES)
6161
target_link_libraries(
6262
${MODULE_NAME}_objects
6363
PUBLIC
@@ -108,7 +108,7 @@ function(barretenberg_module MODULE_NAME)
108108
)
109109
list(APPEND exe_targets ${MODULE_NAME}_tests)
110110

111-
if(CHECK_CIRCUIT_STACKTRACES)
111+
if(CHECK_CIRCUIT_STACKTRACES OR ENABLE_STACKTRACES)
112112
target_link_libraries(
113113
${MODULE_NAME}_test_objects
114114
PUBLIC
@@ -236,7 +236,7 @@ function(barretenberg_module MODULE_NAME)
236236
${TRACY_LIBS}
237237
${TBB_IMPORTED_TARGETS}
238238
)
239-
if(CHECK_CIRCUIT_STACKTRACES)
239+
if(CHECK_CIRCUIT_STACKTRACES OR ENABLE_STACKTRACES)
240240
target_link_libraries(
241241
${BENCHMARK_NAME}_bench_objects
242242
PUBLIC

barretenberg/cpp/src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ if(WASM)
252252
DEPENDS ${CMAKE_BINARY_DIR}/bin/barretenberg.wasm.gz
253253
)
254254

255-
if(CHECK_CIRCUIT_STACKTRACES)
255+
if(CHECK_CIRCUIT_STACKTRACES OR ENABLE_STACKTRACES)
256256
target_link_libraries(
257257
barretenberg.wasm
258258
PUBLIC

barretenberg/cpp/src/barretenberg/bb/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if (NOT(FUZZING))
2323
bb-cli-lib
2424
tracy_mem
2525
)
26-
if(CHECK_CIRCUIT_STACKTRACES)
26+
if(CHECK_CIRCUIT_STACKTRACES OR ENABLE_STACKTRACES)
2727
target_link_libraries(
2828
bb
2929
PUBLIC

barretenberg/cpp/src/barretenberg/env/throw_or_abort_impl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "barretenberg/common/log.hpp"
22
#include <stdexcept>
3+
#ifdef STACKTRACES
4+
#include <backward.hpp>
5+
#endif
36

47
inline void abort_with_message [[noreturn]] (std::string const& err)
58
{
@@ -10,6 +13,13 @@ inline void abort_with_message [[noreturn]] (std::string const& err)
1013
// Native implementation of throw_or_abort
1114
extern "C" void throw_or_abort_impl [[noreturn]] (const char* err)
1215
{
16+
17+
#ifdef STACKTRACES
18+
// Use backward library to print stack trace
19+
backward::StackTrace trace;
20+
trace.load_here(32);
21+
backward::Printer{}.print(trace);
22+
#endif
1323
#ifndef BB_NO_EXCEPTIONS
1424
throw std::runtime_error(err);
1525
#else

build-images/src/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ RUN apt update && \
127127
libc++-dev \
128128
libomp-dev \
129129
doxygen \
130+
# C++ stack trace support
131+
libdw-dev \
132+
libelf-dev \
130133
# Node
131134
# WARNING: Need to downgrade to this version in the basebox below as well.
132135
nodejs=22.16.0-1nodesource1 \

0 commit comments

Comments
 (0)