From bf8ca989d603b52d4be8c6460a2685d469a4edac Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 28 Jul 2024 00:58:00 +0200 Subject: [PATCH] Add `--show-backtrace` option to disable backtrace for even smaller binary. --- lib/std/core/builtin.c3 | 2 +- lib/std/core/env.c3 | 1 + releasenotes.md | 1 + src/build/build.h | 10 ++++++++++ src/build/build_internal.h | 5 +++++ src/build/build_options.c | 10 ++++++++-- src/build/builder.c | 4 ++++ src/build/project.c | 6 ++++++ src/compiler/compiler.c | 1 + src/compiler/semantic_analyser.c | 2 +- 10 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/std/core/builtin.c3 b/lib/std/core/builtin.c3 index ef74bbb2a..9fef9518a 100644 --- a/lib/std/core/builtin.c3 +++ b/lib/std/core/builtin.c3 @@ -683,7 +683,7 @@ fn void install_signal_handler(CInt signal, SignalFunction func) @local } // Clean this up -fn void install_signal_handlers() @init(101) @local +fn void install_signal_handlers() @init(101) @local @if(env::BACKTRACE) { install_signal_handler(libc::SIGBUS, &sig_bus_error); install_signal_handler(libc::SIGSEGV, &sig_segmentation_fault); diff --git a/lib/std/core/env.c3 b/lib/std/core/env.c3 index 79118e13d..e9e9c8fa1 100644 --- a/lib/std/core/env.c3 +++ b/lib/std/core/env.c3 @@ -128,6 +128,7 @@ const bool F128_SUPPORT = $$PLATFORM_F128_SUPPORTED; const REGISTER_SIZE = $$REGISTER_SIZE; const bool COMPILER_SAFE_MODE = $$COMPILER_SAFE_MODE; const bool DEBUG_SYMBOLS = $$DEBUG_SYMBOLS; +const bool BACKTRACE = $$BACKTRACE; const usz LLVM_VERSION = $$LLVM_VERSION; const bool BENCHMARKING = $$BENCHMARKING; const bool TESTING = $$TESTING; diff --git a/releasenotes.md b/releasenotes.md index db69e43e4..1077baeec 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -14,6 +14,7 @@ - Don't generate .o files on `compile` and `compile-run` if there is no `main`. - c3c init-lib does not create the directory with the .c3l suffix #1253 - Permit foreach values to be optional. +- Add `--show-backtrace` option to disable backtrace for even smaller binary. ### Fixes - Broken WASM library code. diff --git a/src/build/build.h b/src/build/build.h index f06e97d04..62c8e0ff3 100644 --- a/src/build/build.h +++ b/src/build/build.h @@ -192,6 +192,13 @@ typedef enum USE_STDLIB_ON = 1 } UseStdlib; +typedef enum +{ + SHOW_BACKTRACE_NOT_SET = -1, + SHOW_BACKTRACE_OFF = 0, + SHOW_BACKTRACE_ON = 1 +} ShowBacktrace; + typedef enum { SIZE_OPTIMIZATION_NOT_SET = -1, @@ -383,6 +390,7 @@ typedef struct BuildOptions_ DiagnosticsSeverity severity[DIAG_END_SENTINEL]; OptimizationSetting optsetting; DebugInfo debug_info_override; + ShowBacktrace show_backtrace; ArchOsTarget arch_os_target_override; SafetyLevel safety_level; PanicLevel panic_level; @@ -529,6 +537,7 @@ typedef struct UseStdlib use_stdlib; EmitStdlib emit_stdlib; LinkLibc link_libc; + ShowBacktrace show_backtrace; StripUnused strip_unused; DebugInfo debug_info; MergeFunctions merge_functions; @@ -603,6 +612,7 @@ static BuildTarget default_build_target = { .optsize = SIZE_OPTIMIZATION_NOT_SET, .arch_os_target = ARCH_OS_TARGET_DEFAULT, .debug_info = DEBUG_INFO_NOT_SET, + .show_backtrace = SHOW_BACKTRACE_NOT_SET, .use_stdlib = USE_STDLIB_NOT_SET, .link_libc = LINK_LIBC_NOT_SET, .emit_stdlib = EMIT_STDLIB_NOT_SET, diff --git a/src/build/build_internal.h b/src/build/build_internal.h index f0bdeaeeb..1853dbc83 100644 --- a/src/build/build_internal.h +++ b/src/build/build_internal.h @@ -81,6 +81,11 @@ static const char *optlevels[4] = { [OPTIMIZATION_AGGRESSIVE] = "max", }; +static const char *backtrace_levels[2] = { + [SHOW_BACKTRACE_OFF] = "off", + [SHOW_BACKTRACE_ON] = "on", +}; + static const char *reloc_models[5] = { [RELOC_NONE] = "none", [RELOC_SMALL_PIC] = "pic", diff --git a/src/build/build_options.c b/src/build/build_options.c index 7d4f530d5..793a53614 100644 --- a/src/build/build_options.c +++ b/src/build/build_options.c @@ -99,9 +99,9 @@ static void usage(void) PRINTF(" -O2 - Unsafe, high optimization, emit debug info."); PRINTF(" -O3 - Unsafe, high optimization, single module, emit debug info."); PRINTF(" -O4 - Unsafe, highest optimization, relaxed maths, single module, emit debug info, no panic messages."); - PRINTF(" -O5 - Unsafe, highest optimization, fast maths, single module, emit debug info, no panic messages."); + PRINTF(" -O5 - Unsafe, highest optimization, fast maths, single module, emit debug info, no panic messages, no backtrace."); PRINTF(" -Os - Unsafe, high optimization, small code, single module, no debug info, no panic messages."); - PRINTF(" -Oz - Unsafe, high optimization, tiny code, single module, no debug info, no panic messages."); + PRINTF(" -Oz - Unsafe, high optimization, tiny code, single module, no debug info, no panic messages, no backtrace."); PRINTF(" -D - Add feature flag ."); PRINTF(" -U - Remove feature flag ."); PRINTF(" --trust=