Skip to content

Commit 01df099

Browse files
committed
Improve tracing raises
1 parent 4e68318 commit 01df099

File tree

8 files changed

+39
-6
lines changed

8 files changed

+39
-6
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ find_package(Elixir)
2929
find_package(Gleam)
3030

3131
set(SANITIZER "address" CACHE STRING "Enable Address Sanitizer") # Include address, memory, or undefined sanitizer
32+
option(AVM_ENABLE_TRACE "Enable opcode-level tracing" OFF)
33+
option(AVM_ENABLE_TRACE_RAISE "Enable tracing raised errors" OFF)
3234

3335
option(AVM_DISABLE_FP "Disable floating point support." OFF)
3436
option(AVM_DISABLE_SMP "Disable SMP." OFF)
@@ -41,7 +43,7 @@ option(AVM_RELEASE "Build an AtomVM release" OFF)
4143
option(AVM_CREATE_STACKTRACES "Create stacktraces" ON)
4244
option(AVM_BUILD_RUNTIME_ONLY "Only build the AtomVM runtime" OFF)
4345
option(COVERAGE "Build for code coverage" OFF)
44-
option(AVM_PRINT_PROCESS_CRASH_DUMPS "Print crash reports when processes die with non-standard reasons" ON)
46+
option(AVM_PRINT_PROCESS_CRASH_DUMPS "Print crash reports when processes die with non-standard reasons" OFF)
4547

4648
# JIT & execution of precompiled code
4749
if(NOT Erlang_VERSION VERSION_GREATER_EQUAL "23")

src/libAtomVM/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ elseif(SANITIZER STREQUAL "undefined")
162162
target_link_options(libAtomVM PUBLIC -fsanitize=undefined)
163163
endif()
164164

165+
if (AVM_ENABLE_TRACE)
166+
target_compile_definitions(libAtomVM PUBLIC ENABLE_TRACE)
167+
endif()
168+
169+
if (AVM_ENABLE_TRACE_RAISE)
170+
target_compile_definitions(libAtomVM PUBLIC ENABLE_TRACE_RAISE)
171+
endif()
172+
165173
target_link_libraries(libAtomVM PUBLIC m)
166174
include(CheckCSourceCompiles)
167175
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")

src/libAtomVM/bif.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@
4444
#pragma GCC diagnostic pop
4545

4646
#define RAISE_ERROR(error_type_atom) \
47+
TRACE_RAISE; \
4748
ctx->x[0] = ERROR_ATOM; \
4849
ctx->x[1] = (error_type_atom); \
4950
return term_invalid_term();
5051

5152
#define RAISE_ERROR_BIF(fail_label, error_type_atom) \
53+
TRACE_RAISE; \
5254
if (fail_label == 0) { \
5355
ctx->x[0] = ERROR_ATOM; \
5456
ctx->x[1] = (error_type_atom); \

src/libAtomVM/nifs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4128,7 +4128,7 @@ static term nif_erlang_fun_info_2(Context *ctx, int argc, term argv[])
41284128
RAISE_ERROR(BADARG_ATOM);
41294129
}
41304130

4131-
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2), 2, (term[]){ key, value }, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
4131+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2), 2, (term[]) { key, value }, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
41324132
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
41334133
}
41344134
term fun_info_tuple = term_alloc_tuple(2, &ctx->heap);
@@ -5694,8 +5694,8 @@ static term nif_erlang_nif_error(Context *ctx, int argc, term argv[])
56945694
UNUSED(argc);
56955695
UNUSED(argv);
56965696

5697-
fprintf(stderr, "Nif not found, aborting\n");
5698-
AVM_ABORT();
5697+
fprintf(stderr, "Nif not found\n");
5698+
RAISE_ERROR(NIF_NOT_FOUND_ERROR_ATOM);
56995699
}
57005700

57015701
#ifndef AVM_NO_JIT

src/libAtomVM/nifs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929
#include "atom.h"
3030
#include "context.h"
3131
#include "exportedfunction.h"
32+
#include "trace.h"
3233

3334
#ifdef __cplusplus
3435
extern "C" {
3536
#endif
3637

3738
#define VALIDATE_VALUE(value, verify_function) \
3839
if (UNLIKELY(!verify_function((value)))) { \
40+
TRACE_RAISE; \
3941
argv[0] = ERROR_ATOM; \
4042
argv[1] = BADARG_ATOM; \
4143
return term_invalid_term(); \
@@ -44,6 +46,7 @@ extern "C" {
4446
#define RAISE_ERROR(error_type_atom) \
4547
ctx->x[0] = ERROR_ATOM; \
4648
ctx->x[1] = (error_type_atom); \
49+
TRACE_RAISE; \
4750
return term_invalid_term();
4851

4952
const struct Nif *nifs_get(const char *mfa);

src/libAtomVM/opcodesswitch.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ extern "C" {
8484
#undef RAISE_ERROR
8585
#endif
8686
#define RAISE_ERROR(error_type_atom) \
87+
TRACE_RAISE; \
8788
SET_ERROR(error_type_atom) \
8889
goto handle_error;
8990

@@ -7551,6 +7552,15 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
75517552

75527553
handle_error:
75537554
{
7555+
if (x_regs[0] == ERROR_ATOM && x_regs[1] == NIF_NOT_FOUND_ERROR_ATOM) {
7556+
fprintf(stderr, "nif_not_found_error raised, printing crash dump and aborting\n");
7557+
context_dump(ctx);
7558+
AVM_ABORT();
7559+
}
7560+
#ifdef ENABLE_TRACE_RAISE
7561+
fprintf(stderr, "TRACE RAISE: raise detected, dumping process context\n");
7562+
context_dump(ctx);
7563+
#endif
75547564
int target_label = context_get_catch_label(ctx, &mod);
75557565
if (target_label) {
75567566
#if AVM_NO_JIT

src/libAtomVM/popcorn/popcorn_atoms.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ X(TRIM_ATOM, "\x4", "trim")
22
X(TRIM_ALL_ATOM, "\x8", "trim_all")
33
X(STDOUT_ATOM, "\x6", "stdout")
44
X(STDERR_ATOM, "\x6", "stderr")
5+
X(NIF_NOT_FOUND_ERROR_ATOM, "\x13", "nif_not_found_error")

src/libAtomVM/trace.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,24 @@
2020

2121
#ifndef _TRACE_H_
2222
#define _TRACE_H_
23-
2423
#ifndef TRACE
2524
#ifdef ENABLE_TRACE
26-
#define TRACE printf
25+
#define TRACE(args...) fprintf(stderr, args)
2726
#define DEBUG_FAIL_NULL(expr) assert((expr) != NULL)
2827
#else
2928
#define TRACE(...)
3029
#define DEBUG_FAIL_NULL(expr)
3130
#endif
3231
#endif
3332

33+
#ifndef TRACE_RAISE
34+
#ifdef ENABLE_TRACE_RAISE
35+
#define TRACE_RAISE fprintf(stderr, "TRACE RAISE: raise from C at %s:%u\n", __FILE_NAME__, __LINE__)
36+
#else
37+
#define TRACE_RAISE
38+
#endif
39+
#endif
40+
3441
#define USED_BY_TRACE(x) \
3542
(void) (x)
3643

0 commit comments

Comments
 (0)