Skip to content

Commit

Permalink
Work around OTP-21 deprecation of get_stacktrace()
Browse files Browse the repository at this point in the history
Based off a macro by @okeuday at erlang/otp#1783
  • Loading branch information
ferd committed May 3, 2018
1 parent fb87ed5 commit 5f9b429
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 30 deletions.
1 change: 1 addition & 0 deletions bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ additional_defines() ->
[{d, D} || {Re, D} <- [{"^[0-9]+", namespaced_types},
{"^R1[4|5]", deprecated_crypto},
{"^2", unicode_str},
{"^(R|1|20)", fun_stacktrace},
{"^((1[8|9])|2)", rand_module}],
is_otp_release(Re)].

Expand Down
2 changes: 2 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
{erl_opts, [{platform_define, "^[0-9]+", namespaced_types},
{platform_define, "^(19|2)", rand_only},
{platform_define, "^2", unicode_str},
{platform_define, "^(R|1|20)", fun_stacktrace},
warnings_as_errors]}.

%% Use OTP 18+ when dialyzing rebar3
Expand Down Expand Up @@ -59,6 +60,7 @@
{platform_define, "^R1[4|5]", deprecated_crypto},
{platform_define, "^((1[8|9])|2)", rand_module},
{platform_define, "^2", unicode_str},
{platform_define, "^(R|1|20)", fun_stacktrace},
no_debug_info,
warnings_as_errors]},
{deps, []}, {plugins, []}]},
Expand Down
6 changes: 6 additions & 0 deletions src/rebar.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
-type rebar_set() :: set().
-endif.

-ifdef(fun_stacktrace).
-define(WITH_STACKTRACE(T, R, S), T:R -> S = erlang:get_stacktrace(),).
-else.
-define(WITH_STACKTRACE(T, R, S), T:R:S ->).
-endif.

-define(GRAPH_VSN, 2).
-type v() :: {digraph:vertex(), term()} | 'false'.
-type e() :: {digraph:vertex(), digraph:vertex()}.
Expand Down
21 changes: 10 additions & 11 deletions src/rebar3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ main(Args) ->
{ok, _State} ->
erlang:halt(0);
Error ->
handle_error(Error)
handle_error(Error, [])
catch
_:Error ->
handle_error(Error)
?WITH_STACKTRACE(_,Error,Stacktrace)
handle_error(Error, Stacktrace)
end.

%% @doc Erlang-API entry point
Expand Down Expand Up @@ -299,29 +299,28 @@ global_option_spec_list() ->

%% @private translate unhandled errors and internal return codes into proper
%% erroneous program exits.
-spec handle_error(term()) -> no_return().
handle_error(rebar_abort) ->
-spec handle_error(term(), term()) -> no_return().
handle_error(rebar_abort, _) ->
erlang:halt(1);
handle_error({error, rebar_abort}) ->
handle_error({error, rebar_abort}, _) ->
erlang:halt(1);
handle_error({error, {Module, Reason}}) ->
handle_error({error, {Module, Reason}}, Stacktrace) ->
case code:which(Module) of
non_existing ->
?CRASHDUMP("~p: ~p~n~p~n~n", [Module, Reason, erlang:get_stacktrace()]),
?CRASHDUMP("~p: ~p~n~p~n~n", [Module, Reason, Stacktrace]),
?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to stacktrace or consult rebar3.crashdump", []),
?DEBUG("Uncaught error: ~p ~p", [Module, Reason]),
?INFO("When submitting a bug report, please include the output of `rebar3 report \"your command\"`", []);
_ ->
?ERROR("~ts", [Module:format_error(Reason)])
end,
erlang:halt(1);
handle_error({error, Error}) when is_list(Error) ->
handle_error({error, Error}, _) when is_list(Error) ->
?ERROR("~ts", [Error]),
erlang:halt(1);
handle_error(Error) ->
handle_error(Error, StackTrace) ->
%% Nothing should percolate up from rebar_core;
%% Dump this error to console
StackTrace = erlang:get_stacktrace(),
?CRASHDUMP("Error: ~p~n~p~n~n", [Error, StackTrace]),
?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace or consult rebar3.crashdump", []),
?DEBUG("Uncaught error: ~p", [Error]),
Expand Down
4 changes: 2 additions & 2 deletions src/rebar_agent.erl
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ run(Namespace, Command, StrArgs, RState, Cwd) ->
{{error, cwd_changed}, RState}
end
catch
Type:Reason ->
?DEBUG("Agent Stacktrace: ~p", [erlang:get_stacktrace()]),
?WITH_STACKTRACE(Type, Reason, Stacktrace)
?DEBUG("Agent Stacktrace: ~p", [Stacktrace]),
{{error, {Type, Reason}}, RState}
end.

Expand Down
3 changes: 1 addition & 2 deletions src/rebar_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ do([ProviderName | Rest], State) ->
{error, Error} ->
{error, Error}
catch
error:undef ->
Stack = erlang:get_stacktrace(),
?WITH_STACKTRACE(error,undef,Stack)
case Stack of
[{ProviderName, do, [_], _}|_] ->
%% This should really only happen if a plugin provider doesn't export do/1
Expand Down
4 changes: 2 additions & 2 deletions src/rebar_dialyzer_format.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ format_warning_(Opts, Warning = {_Tag, {SrcFile, Line}, Msg}, {_LastFile, Acc})
String = message_to_string(Msg),
{SrcFile, [lists:flatten(fmt("~n~ts~n~!c~4w~!!: ~ts", [F, Line, String])) | Acc]}
catch
Error:Reason ->
?WITH_STACKTRACE(Error, Reason, Stacktrace)
?DEBUG("Failed to pretty format warning: ~p:~p~n~p",
[Error, Reason, erlang:get_stacktrace()]),
[Error, Reason, Stacktrace]),
{SrcFile, [dialyzer:format_warning(Warning, fullpath) | Acc]}
end.

Expand Down
4 changes: 2 additions & 2 deletions src/rebar_fetch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ download_source(AppDir, Source, State) ->
Error ->
throw(?PRV_ERROR(Error))
catch
C:T ->
?DEBUG("rebar_fetch exception ~p ~p ~p", [C, T, erlang:get_stacktrace()]),
?WITH_STACKTRACE(C,T,S)
?DEBUG("rebar_fetch exception ~p ~p ~p", [C, T, S]),
throw(?PRV_ERROR({fetch_fail, Source}))
end.

Expand Down
4 changes: 2 additions & 2 deletions src/rebar_plugins.erl
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ handle_plugin(Profile, Plugin, State, Upgrade) ->

{plugin_providers(Plugin), State4}
catch
C:T ->
?DEBUG("~p ~p ~p", [C, T, erlang:get_stacktrace()]),
?WITH_STACKTRACE(C,T,S)
?DEBUG("~p ~p ~p", [C, T, S]),
?WARN("Plugin ~p not available. It will not be used.", [Plugin]),
{[], State}
end.
Expand Down
8 changes: 4 additions & 4 deletions src/rebar_prv_shell.erl
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ rewrite_leaders(OldUser, NewUser) ->
%% reset the tty handler once more for remote shells
error_logger:swap_handler(tty)
catch
E:R -> % may fail with custom loggers
?DEBUG("Logger changes failed for ~p:~p (~p)", [E,R,erlang:get_stacktrace()]),
?WITH_STACKTRACE(E,R,S) % may fail with custom loggers
?DEBUG("Logger changes failed for ~p:~p (~p)", [E,R,S]),
hope_for_best
end.

Expand All @@ -235,9 +235,9 @@ maybe_run_script(State) ->
File = filename:absname(RelFile),
try run_script_file(File)
catch
C:E ->
?WITH_STACKTRACE(C,E,S)
?ABORT("Couldn't run shell escript ~p - ~p:~p~nStack: ~p",
[File, C, E, erlang:get_stacktrace()])
[File, C, E, S])
end
end.

Expand Down
4 changes: 2 additions & 2 deletions src/rebar_prv_update.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ do(State) ->
?PRV_ERROR({package_parse_cdn, CDN})
end
catch
_E:C ->
?DEBUG("Error creating package index: ~p ~p", [C, erlang:get_stacktrace()]),
?WITH_STACKTRACE(_E, C, S)
?DEBUG("Error creating package index: ~p ~p", [C, S]),
throw(?PRV_ERROR(package_index_write))
end.

Expand Down
6 changes: 3 additions & 3 deletions src/rebar_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,9 @@ create_logic_providers(ProviderModules, State0) ->
end
end, State0, ProviderModules)
catch
C:T ->
?DEBUG("~p: ~p ~p", [C, T, erlang:get_stacktrace()]),
?CRASHDUMP("~p: ~p~n~p~n~n~p", [C, T, erlang:get_stacktrace(), State0]),
?WITH_STACKTRACE(C,T,S)
?DEBUG("~p: ~p ~p", [C, T, S]),
?CRASHDUMP("~p: ~p~n~p~n~n~p", [C, T, S, State0]),
throw({error, "Failed creating providers. Run with DEBUG=1 for stacktrace or consult rebar3.crashdump."})
end.

Expand Down

0 comments on commit 5f9b429

Please sign in to comment.