Skip to content

Commit 2b95eb4

Browse files
committed
OP_APPLY_LAST: fix stacktrace on failure
Truncate stack only after checking module and function, thus generating a more helpful stacktrace, following what BEAM does. Signed-off-by: Paul Guyot <[email protected]>
1 parent 6425d02 commit 2b95eb4

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/libAtomVM/opcodesswitch.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5384,13 +5384,13 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
53845384
READ_ANY_XREG(function, arity + 1);
53855385
TRACE("apply_last/1, module=%lu, function=%lu arity=%i deallocate=%i\n", module, function, arity, n_words);
53865386

5387-
ctx->cp = ctx->e[n_words];
5388-
ctx->e += (n_words + 1);
5389-
53905387
if (UNLIKELY(!term_is_atom(module) || !term_is_atom(function))) {
53915388
RAISE_ERROR(BADARG_ATOM);
53925389
}
53935390

5391+
ctx->cp = ctx->e[n_words];
5392+
ctx->e += (n_words + 1);
5393+
53945394
AtomString module_name = globalcontext_atomstring_from_term(glb, module);
53955395
AtomString function_name = globalcontext_atomstring_from_term(glb, function);
53965396

tests/erlang_tests/fail_apply_last.erl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,25 @@ try_apply_last(M, F) ->
5353
try
5454
?MODULE:do_apply0(M, F)
5555
catch
56-
_Class:_Reason -> 1
56+
_Class:_Reason:Stack ->
57+
test_stacktrace(Stack, false, false, 1)
5758
end.
5859

5960
pad_some_calls() ->
6061
X = 1,
6162
Y = 2,
6263
Z = X + Y,
6364
Z.
65+
66+
% if try_apply_last/2 is in stacktrace, we also want do_apply0/2
67+
test_stacktrace([], _, _, R) ->
68+
R;
69+
test_stacktrace([{?MODULE, try_apply_last, 2, _} | T], false, DoApply0, R) ->
70+
test_stacktrace(T, true, DoApply0, R - 1);
71+
test_stacktrace([{?MODULE, do_apply0, 2, _} | T], TryApplyLast, false, R) ->
72+
test_stacktrace(T, TryApplyLast, true, R + 1);
73+
test_stacktrace([_ | T], TryApplyLast, DoApply0, R) ->
74+
test_stacktrace(T, TryApplyLast, DoApply0, R);
75+
test_stacktrace(undefined, _, _, R) ->
76+
% AVM_CREATE_STACKTRACES=off
77+
R.

0 commit comments

Comments
 (0)