Skip to content

Commit c462803

Browse files
committed
Merge pull request #1758 from pguyot/w29/fix-apply-last-stacktrace
OP_APPLY_LAST: fix stacktrace on failure These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 93c6a97 + 2b95eb4 commit c462803

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
@@ -5382,13 +5382,13 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
53825382
READ_ANY_XREG(function, arity + 1);
53835383
TRACE("apply_last/1, module=%lu, function=%lu arity=%i deallocate=%i\n", module, function, arity, n_words);
53845384

5385-
ctx->cp = ctx->e[n_words];
5386-
ctx->e += (n_words + 1);
5387-
53885385
if (UNLIKELY(!term_is_atom(module) || !term_is_atom(function))) {
53895386
RAISE_ERROR(BADARG_ATOM);
53905387
}
53915388

5389+
ctx->cp = ctx->e[n_words];
5390+
ctx->e += (n_words + 1);
5391+
53925392
AtomString module_name = globalcontext_atomstring_from_term(glb, module);
53935393
AtomString function_name = globalcontext_atomstring_from_term(glb, function);
53945394

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)