From 88d0b71d58cba45cdbf051f80e2c7c1940f96c75 Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Sat, 1 Sep 2018 19:37:26 +0300 Subject: [PATCH] Get stacktrace out of lager context Calling erlang:get_stacktrace() inside lager functions produces stacktraces of the logging function itself, not the function which has failed. --- src/ejabberd_acme.erl | 21 ++++++++++++++------- src/ejabberd_hooks.erl | 3 ++- src/ejabberd_local.erl | 3 ++- src/ejabberd_router.erl | 3 ++- src/ejabberd_router_sql.erl | 3 ++- src/ejabberd_s2s.erl | 3 ++- src/ejabberd_sm.erl | 3 ++- src/gen_iq_handler.erl | 3 ++- src/mod_http_api.erl | 12 +++++++----- src/mod_muc_room.erl | 4 ++-- src/mod_roster.erl | 3 ++- 11 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/ejabberd_acme.erl b/src/ejabberd_acme.erl index 14c0ad9ad..6c503f205 100644 --- a/src/ejabberd_acme.erl +++ b/src/ejabberd_acme.erl @@ -151,7 +151,8 @@ get_certificates(Domains) -> throw:Throw -> Throw; E:R -> - ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), + St = erlang:get_stacktrace(), + ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]), {error, get_certificates} end; false -> @@ -243,7 +244,8 @@ get_certificate(CAUrl, DomainName, PrivateKey) -> throw:Throw -> Throw; E:R -> - ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), + St = erlang:get_stacktrace(), + ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]), {error, DomainName, get_certificate} end. @@ -382,7 +384,8 @@ renew_certificates() -> throw:Throw -> Throw; E:R -> - ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), + St = erlang:get_stacktrace(), + ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]), {error, get_certificates} end. @@ -447,7 +450,8 @@ list_certificates(Verbose) -> throw:Throw -> Throw; E:R -> - ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), + St = erlang:get_stacktrace(), + ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]), {error, list_certificates} end; false -> @@ -489,7 +493,8 @@ format_certificate(DataCert, Verbose) -> end catch E:R -> - ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), + St = erlang:get_stacktrace(), + ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]), fail_format_certificate(DomainName) end. @@ -614,7 +619,8 @@ revoke_certificates(DomainOrFile) -> throw:Throw -> Throw; E:R -> - ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), + St = erlang:get_stacktrace(), + ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]), {error, revoke_certificate} end. @@ -1118,7 +1124,8 @@ save_certificate({ok, DomainName, Cert}) -> throw:Throw -> Throw; E:R -> - ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), + St = erlang:get_stacktrace(), + ?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]), {error, DomainName, saving} end. diff --git a/src/ejabberd_hooks.erl b/src/ejabberd_hooks.erl index 2c992089b..bc67b4c67 100644 --- a/src/ejabberd_hooks.erl +++ b/src/ejabberd_hooks.erl @@ -380,11 +380,12 @@ safe_apply(Hook, Module, Function, Args) -> apply(Module, Function, Args) end catch E:R when E /= exit; R /= normal -> + St = get_stacktrace(), ?ERROR_MSG("Hook ~p crashed when running ~p:~p/~p:~n" "** Reason = ~p~n" "** Arguments = ~p", [Hook, Module, Function, length(Args), - {E, R, get_stacktrace()}, Args]), + {E, R, St}, Args]), 'EXIT' end. diff --git a/src/ejabberd_local.erl b/src/ejabberd_local.erl index 37087526b..d9ef97129 100644 --- a/src/ejabberd_local.erl +++ b/src/ejabberd_local.erl @@ -71,8 +71,9 @@ start_link() -> route(Packet) -> try do_route(Packet) catch E:R -> + St = erlang:get_stacktrace(), ?ERROR_MSG("failed to route packet:~n~s~nReason = ~p", - [xmpp:pp(Packet), {E, {R, erlang:get_stacktrace()}}]) + [xmpp:pp(Packet), {E, {R, St}}]) end. -spec route_iq(iq(), function()) -> ok. diff --git a/src/ejabberd_router.erl b/src/ejabberd_router.erl index b668f2b37..cf4bd8e42 100644 --- a/src/ejabberd_router.erl +++ b/src/ejabberd_router.erl @@ -91,8 +91,9 @@ start_link() -> route(Packet) -> try do_route(Packet) catch E:R -> + St = erlang:get_stacktrace(), ?ERROR_MSG("failed to route packet:~n~s~nReason = ~p", - [xmpp:pp(Packet), {E, {R, erlang:get_stacktrace()}}]) + [xmpp:pp(Packet), {E, {R, St}}]) end. -spec route(jid(), jid(), xmlel() | stanza()) -> ok. diff --git a/src/ejabberd_router_sql.erl b/src/ejabberd_router_sql.erl index b7017d528..edf06dfe0 100644 --- a/src/ejabberd_router_sql.erl +++ b/src/ejabberd_router_sql.erl @@ -122,10 +122,11 @@ row_to_route(Domain, {ServerHost, NodeS, PidS, LocalHintS} = Row) -> catch _:{bad_node, _} -> []; E:R -> + St = erlang:get_stacktrace(), ?ERROR_MSG("failed to decode row from 'route' table:~n" "Row = ~p~n" "Domain = ~s~n" "Reason = ~p", - [Row, Domain, {E, {R, erlang:get_stacktrace()}}]), + [Row, Domain, {E, {R, St}}]), [] end. diff --git a/src/ejabberd_s2s.erl b/src/ejabberd_s2s.erl index 7b043d125..74234e26e 100644 --- a/src/ejabberd_s2s.erl +++ b/src/ejabberd_s2s.erl @@ -95,8 +95,9 @@ start_link() -> route(Packet) -> try do_route(Packet) catch E:R -> + St = erlang:get_stacktrace(), ?ERROR_MSG("failed to route packet:~n~s~nReason = ~p", - [xmpp:pp(Packet), {E, {R, erlang:get_stacktrace()}}]) + [xmpp:pp(Packet), {E, {R, St}}]) end. clean_temporarily_blocked_table() -> diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index f83ead7ff..9e9a2d191 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -142,9 +142,10 @@ route(Packet) -> Packet1 -> try do_route(Packet1), ok catch E:R -> + St = erlang:get_stacktrace(), ?ERROR_MSG("failed to route packet:~n~s~nReason = ~p", [xmpp:pp(Packet1), - {E, {R, erlang:get_stacktrace()}}]) + {E, {R, St}}]) end end. diff --git a/src/gen_iq_handler.erl b/src/gen_iq_handler.erl index 636a94ec9..49d1d8e3e 100644 --- a/src/gen_iq_handler.erl +++ b/src/gen_iq_handler.erl @@ -114,8 +114,9 @@ process_iq(_Host, Module, Function, IQ) -> ignore -> ok catch E:R -> + St = erlang:get_stacktrace(), ?ERROR_MSG("failed to process iq:~n~s~nReason = ~p", - [xmpp:pp(IQ), {E, {R, erlang:get_stacktrace()}}]), + [xmpp:pp(IQ), {E, {R, St}}]), Txt = <<"Module failed to handle the query">>, Err = xmpp:err_internal_server_error(Txt, IQ#iq.lang), ejabberd_router:route_error(IQ, Err) diff --git a/src/mod_http_api.erl b/src/mod_http_api.erl index e451b82c8..84866d4d0 100644 --- a/src/mod_http_api.erl +++ b/src/mod_http_api.erl @@ -193,7 +193,8 @@ process([Call], #request{method = 'POST', data = Data, ip = IPPort} = Req) -> ?DEBUG("Bad Request: ~p", [_Err]), badrequest_response(<<"Invalid JSON input">>); _:_Error -> - ?DEBUG("Bad Request: ~p ~p", [_Error, erlang:get_stacktrace()]), + St = erlang:get_stacktrace(), + ?DEBUG("Bad Request: ~p ~p", [_Error, St]), badrequest_response() end; process([Call], #request{method = 'GET', q = Data, ip = {IP, _}} = Req) -> @@ -210,9 +211,9 @@ process([Call], #request{method = 'GET', q = Data, ip = {IP, _}} = Req) -> throw:{error, unknown_command} -> json_format({404, 44, <<"Command not found.">>}); _:_Error -> - - ?DEBUG("Bad Request: ~p ~p", [_Error, erlang:get_stacktrace()]), - badrequest_response() + St = erlang:get_stacktrace(), + ?DEBUG("Bad Request: ~p ~p", [_Error, St]), + badrequest_response() end; process([_Call], #request{method = 'OPTIONS', data = <<>>}) -> {200, ?OPTIONS_HEADER, []}; @@ -314,7 +315,8 @@ handle(Call, Auth, Args, Version) when is_atom(Call), is_list(Args) -> throw:Msg when is_list(Msg); is_binary(Msg) -> {400, iolist_to_binary(Msg)}; _Error -> - ?ERROR_MSG("REST API Error: ~p ~p", [_Error, erlang:get_stacktrace()]), + St = erlang:get_stacktrace(), + ?ERROR_MSG("REST API Error: ~p ~p", [_Error, St]), {500, <<"internal_error">>} end; {error, Msg} -> diff --git a/src/mod_muc_room.erl b/src/mod_muc_room.erl index 88bf4f099..136637ee3 100644 --- a/src/mod_muc_room.erl +++ b/src/mod_muc_room.erl @@ -2815,9 +2815,9 @@ process_item_change(Item, SD, UJID) -> undefined -> <<"">> end, + St = erlang:get_stacktrace(), ?ERROR_MSG("failed to set item ~p~s: ~p", - [Item, FromSuffix, - {E, {R, erlang:get_stacktrace()}}]), + [Item, FromSuffix, {E, {R, St}}]), {error, xmpp:err_internal_server_error()} end. diff --git a/src/mod_roster.erl b/src/mod_roster.erl index 5c0d3eeca..1f42b69e0 100644 --- a/src/mod_roster.erl +++ b/src/mod_roster.erl @@ -321,8 +321,9 @@ process_iq_get(#iq{to = To, lang = Lang, ver = Version} end) catch E:R -> + St = erlang:get_stacktrace(), ?ERROR_MSG("failed to process roster get for ~s: ~p", - [jid:encode(To), {E, {R, erlang:get_stacktrace()}}]), + [jid:encode(To), {E, {R, St}}]), Txt = <<"Roster module has failed">>, xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang)) end. -- 2.40.0