|
| 1 | +%% EXPECT: stacktrace_order_ok |
| 2 | +%% Stacktraces identify the raising function before its non-tail callers, |
| 3 | +%% including re-raises and {Reason, Stack} exit reasons. Run on Nera and BEAM. |
| 4 | +-module(stacktrace_order). |
| 5 | +-export([test/0, init/1]). |
| 6 | + |
| 7 | +test() -> |
| 8 | + {outer, {middle, ok}} = outer(ok), |
| 9 | + lists:foreach(fun check_caught/1, [error, throw, exit, badmatch]), |
| 10 | + lists:foreach(fun check_reraised/1, [error, throw, exit, badmatch]), |
| 11 | + ok = check_exit_reason(), |
| 12 | + ok = check_failed_start(), |
| 13 | + io:format("stacktrace_order_ok~n"), |
| 14 | + ok. |
| 15 | + |
| 16 | +check_caught(Mode) -> |
| 17 | + {Class, Reason, Stack} = captured(Mode), |
| 18 | + {Class, Reason} = expected(Mode), |
| 19 | + ok = check_order(Stack), |
| 20 | + io:format("caught ~p: ~p~n", [Mode, first_calls(Stack)]). |
| 21 | + |
| 22 | +captured(Mode) -> |
| 23 | + try outer(Mode) |
| 24 | + catch Class:Reason:Stack -> {Class, Reason, Stack} |
| 25 | + end. |
| 26 | + |
| 27 | +%% Keep each call non-tail: BEAM may omit tail callers from a stacktrace. |
| 28 | +outer(Mode) -> {outer, middle(Mode)}. |
| 29 | +middle(Mode) -> {middle, inner(Mode)}. |
| 30 | +inner(error) -> erlang:error(boom); |
| 31 | +inner(throw) -> throw(boom); |
| 32 | +inner(exit) -> exit(boom); |
| 33 | +inner(badmatch) -> 1 = id(2), ok; |
| 34 | +inner(ok) -> ok. |
| 35 | + |
| 36 | +expected(error) -> {error, boom}; |
| 37 | +expected(throw) -> {throw, boom}; |
| 38 | +expected(exit) -> {exit, boom}; |
| 39 | +expected(badmatch) -> {error, {badmatch, 2}}. |
| 40 | + |
| 41 | +check_order(Stack) -> |
| 42 | + [{?MODULE, inner, 1, _}, {?MODULE, middle, 1, _}, |
| 43 | + {?MODULE, outer, 1, _} | _] = Stack, |
| 44 | + ok. |
| 45 | + |
| 46 | +first_calls(Stack) -> |
| 47 | + [{M, F, A} || {M, F, A, _} <- lists:sublist(Stack, 3)]. |
| 48 | + |
| 49 | +check_reraised(Mode) -> |
| 50 | + {Class, Reason, Original} = captured(Mode), |
| 51 | + %% Also supply a shortened, real trace: raise/3 must keep list order, |
| 52 | + %% rather than capture the re-raising function's current call stack. |
| 53 | + lists:foreach(fun(Stack) -> |
| 54 | + Raised = try erlang:raise(Class, Reason, Stack) |
| 55 | + catch Class:Reason:Caught -> Caught |
| 56 | + end, |
| 57 | + Stack = Raised, |
| 58 | + ok = check_order(Raised) |
| 59 | + end, [Original, lists:sublist(Original, 3)]), |
| 60 | + io:format("reraised ~p: ~p~n", [Mode, first_calls(Original)]). |
| 61 | + |
| 62 | +check_exit_reason() -> |
| 63 | + {Pid, Ref} = spawn_monitor(fun() -> |
| 64 | + {error, Reason, Stack} = captured(error), |
| 65 | + exit({Reason, Stack}) |
| 66 | + end), |
| 67 | + receive |
| 68 | + {'DOWN', Ref, process, Pid, {boom, Stack}} -> |
| 69 | + ok = check_order(Stack), |
| 70 | + io:format("exit reason: ~p~n", [first_calls(Stack)]) |
| 71 | + after 5000 -> error(missing_exit_reason) |
| 72 | + end. |
| 73 | + |
| 74 | +check_failed_start() -> |
| 75 | + Previous = process_flag(trap_exit, true), |
| 76 | + {error, {boom, Stack}} = gen_server:start_link(?MODULE, [], []), |
| 77 | + process_flag(trap_exit, Previous), |
| 78 | + ok = check_order(Stack), |
| 79 | + io:format("failed start: ~p~n", [first_calls(Stack)]). |
| 80 | + |
| 81 | +init([]) -> {ok, outer(error)}. |
| 82 | +id(X) -> X. |
0 commit comments