Skip to content

Commit c8e8d05

Browse files
committed
Simplify stacktrace_build
Signed-off-by: Davide Bettio <[email protected]>
1 parent 80b2513 commit c8e8d05

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

src/libAtomVM/context.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ COLD_FUNC void context_dump(Context *ctx)
11631163
fprintf(stderr, "\n");
11641164

11651165
fprintf(stderr, "\nStacktrace:\n");
1166-
term_display(stderr, stacktrace_build(ctx, &ctx->exception_stacktrace, 3), ctx);
1166+
term_display(stderr, stacktrace_build(ctx), ctx);
11671167
fprintf(stderr, "\n\n");
11681168

11691169
{

src/libAtomVM/opcodesswitch.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4063,8 +4063,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
40634063
break;
40644064

40654065
case ERROR_ATOM_INDEX: {
4066-
x_regs[2] = ctx->exception_stacktrace;
4067-
x_regs[2] = stacktrace_build(ctx, &x_regs[2], 3);
4066+
x_regs[2] = stacktrace_build(ctx);
40684067
// MEMORY_CAN_SHRINK because catch_end is classified as gc in beam_ssa_codegen.erl
40694068
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2) * 2, 2, x_regs + 1, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
40704069
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
@@ -6574,8 +6573,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
65746573
TRACE("build_stacktrace/0\n");
65756574

65766575
#ifdef IMPL_EXECUTE_LOOP
6577-
x_regs[0] = ctx->exception_stacktrace;
6578-
x_regs[0] = stacktrace_build(ctx, &x_regs[0], 1);
6576+
x_regs[0] = stacktrace_build(ctx);
65796577
#endif
65806578
break;
65816579
}

src/libAtomVM/stacktrace.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ term stacktrace_create_raw(Context *ctx, Module *mod, int current_offset, term e
3333
return exception_class;
3434
}
3535

36-
term stacktrace_build(Context *ctx, term *stack_info, uint32_t live)
36+
term stacktrace_build(Context *ctx)
3737
{
3838
UNUSED(ctx);
3939
UNUSED(stack_info);
@@ -257,38 +257,41 @@ static term find_path_created(const void *key, struct ModulePathPair *module_pat
257257
return term_invalid_term();
258258
}
259259

260-
term stacktrace_build(Context *ctx, term *stack_info, uint32_t live)
260+
term stacktrace_build(Context *ctx)
261261
{
262262
GlobalContext *glb = ctx->global;
263263

264-
if (*stack_info == OUT_OF_MEMORY_ATOM) {
265-
return *stack_info;
264+
term stack_info = ctx->exception_stacktrace;
265+
266+
if (stack_info == OUT_OF_MEMORY_ATOM) {
267+
return stack_info;
266268
}
267-
if (!term_is_tuple(*stack_info)) {
269+
if (!term_is_tuple(stack_info)) {
268270
return UNDEFINED_ATOM;
269271
}
270272

271-
int num_frames = term_to_int(term_get_tuple_element(*stack_info, 0));
272-
int num_aux_terms = term_to_int(term_get_tuple_element(*stack_info, 1));
273-
int filename_lens = term_to_int(term_get_tuple_element(*stack_info, 2));
274-
int num_mods = term_to_int(term_get_tuple_element(*stack_info, 3));
273+
int num_frames = term_to_int(term_get_tuple_element(stack_info, 0));
274+
int num_aux_terms = term_to_int(term_get_tuple_element(stack_info, 1));
275+
int filename_lens = term_to_int(term_get_tuple_element(stack_info, 2));
276+
int num_mods = term_to_int(term_get_tuple_element(stack_info, 3));
275277

276278
struct ModulePathPair *module_paths = malloc(num_mods * sizeof(struct ModulePathPair));
277279
if (IS_NULL_PTR(module_paths)) {
278280
fprintf(stderr, "Unable to allocate space for module paths. Returning raw stacktrace.\n");
279-
return *stack_info;
281+
return stack_info;
280282
}
281283

282284
//
283285
// [{module, function, arity, [{file, string()}, {line, int}]}, ...]
284286
//
285287
size_t requested_size = (TUPLE_SIZE(4) + 2) * num_frames + num_aux_terms * (4 + 2 * TUPLE_SIZE(2)) + 2 * filename_lens;
286-
if (UNLIKELY(memory_ensure_free_with_roots(ctx, requested_size, live, ctx->x, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
288+
if (UNLIKELY(memory_ensure_free_opt(ctx, requested_size, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
287289
free(module_paths);
288290
return OUT_OF_MEMORY_ATOM;
289291
}
292+
stack_info = ctx->exception_stacktrace;
290293

291-
term raw_stacktrace = term_get_tuple_element(*stack_info, 4);
294+
term raw_stacktrace = term_get_tuple_element(stack_info, 4);
292295

293296
term stacktrace = term_nil();
294297
term el = raw_stacktrace;

src/libAtomVM/stacktrace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ term stacktrace_create_raw(Context *ctx, Module *mod, int current_offset, term e
3737
* @param live number of x registers to preserve, which should include stack_info
3838
* @return the built stack trace
3939
*/
40-
term stacktrace_build(Context *ctx, term *stack_info, uint32_t live);
40+
term stacktrace_build(Context *ctx);
4141
term stacktrace_exception_class(term stack_info);
4242

4343
#ifdef __cplusplus

0 commit comments

Comments
 (0)