Skip to content
Permalink
Browse files
fix code layout
  • Loading branch information
bartekgorny committed Aug 21, 2018
1 parent 61a0aff commit 6973be4f4088322ccd4cec14d0ee66c77b3dfa6e
Showing 4 changed files with 75 additions and 49 deletions.
@@ -1,5 +1,5 @@
{profiles, [
{test, [
{erl_opts, [nowarn_export_all]}
{erl_ops, [nowarn_expor_all, {d, 'TEST'}]}
]}
]}.
@@ -13,9 +13,7 @@
term_to_port/1,
time_map/5, time_fold/6,
scheduler_usage_diff/2,
maybe_kill/1,
sublist_top_n_attrs/2,
format_trace_output/1, format_trace_output/2]).
sublist_top_n_attrs/2]).
%% private exports
-export([binary_memory/1]).

@@ -247,34 +245,6 @@ sublist_top_n_attrs(List, Len) ->
binary_memory(Bins) ->
lists:foldl(fun({_,Mem,_}, Tot) -> Mem+Tot end, 0, Bins).

%% @doc formats call arguments and return values - most types are just printed out, except for
%% tuples recognised as records, which mimic the source code syntax
%% @end
format_trace_output(Args) ->
format_trace_output(recon_rec:is_active(), Args).

format_trace_output(true, Args) when is_tuple(Args) ->
recon_rec:format_tuple(Args);
format_trace_output(true, Args) when is_list(Args) ->
case io_lib:printable_list(Args) of
true -> io_lib:format("~p", [Args]);
false ->
L = lists:map(fun(A) -> format_trace_output(true, A) end, Args),
"[" ++ string:join(L, ", ") ++ "]"
end;
format_trace_output(_, Args) ->
io_lib:format("~p", [Args]).

maybe_kill(Name) ->
case whereis(Name) of
undefined ->
ok;
Pid ->
unlink(Pid),
exit(Pid, kill),
wait_for_death(Pid, Name)
end.

%%%%%%%%%%%%%%%
%%% PRIVATE %%%
%%%%%%%%%%%%%%%
@@ -312,12 +282,4 @@ merge_pairs([]) -> [];
merge_pairs([H]) -> H;
merge_pairs([A, B|T]) -> merge(merge(A, B), merge_pairs(T)).

wait_for_death(Pid, Name) ->
case is_process_alive(Pid) orelse whereis(Name) =:= Pid of
true ->
timer:sleep(10),
wait_for_death(Pid, Name);
false ->
ok
end.

@@ -17,7 +17,9 @@
-export([is_active/0]).
-export([import/1, format_tuple/1, clear/1, clear/0, list/0, get_list/0, limit/3]).

-export([lookup_record/2]). %% for testing
-ifdef(TEST).
-export([lookup_record/2]).
-endif.

% basic types
-type field() :: atom().
@@ -56,7 +58,7 @@ clear(Module) ->

%% @doc remove all imported definitions, destroy the table, clean up
clear() ->
recon_lib:maybe_kill(recon_ets),
maybe_kill(recon_ets),
ok.

%% @doc prints out all "known" (imported) record definitions and their limit settings.
@@ -192,7 +194,7 @@ format_tuple(Name, Rec) when is_atom(Name) ->
[RecDef] -> format_record(Rec, RecDef);
_ ->
List = tuple_to_list(Rec),
["{", lists:join(", ", [recon_lib:format_trace_output(true, El) || El <- List]), "}"]
["{", lists:join(", ", [recon_trace:format_trace_output(true, El) || El <- List]), "}"]
end;
format_tuple(_, Tuple) ->
format_default(Tuple).
@@ -215,11 +217,35 @@ format_record(Rec, {{Name, Arity}, Fields, _, Limits}) ->
end.

format_kv(Key, Val) ->
[recon_lib:format_trace_output(true, Key), "=", recon_lib:format_trace_output(true, Val)].
[recon_trace:format_trace_output(true, Key), "=", recon_trace:format_trace_output(true, Val)].

apply_limits(List, none) -> List;
apply_limits(_List, all) -> [];
apply_limits(List, Field) when is_atom(Field) ->
[{Field, proplists:get_value(Field, List)}, {more, '...'}];
apply_limits(List, Limits) ->
lists:filter(fun({K, _}) -> lists:member(K, Limits) end, List) ++ [{more, '...'}].

%%%%%%%%%%%%%%%
%%% HELPERS %%%
%%%%%%%%%%%%%%%

maybe_kill(Name) ->
case whereis(Name) of
undefined ->
ok;
Pid ->
unlink(Pid),
exit(Pid, kill),
wait_for_death(Pid, Name)
end.

wait_for_death(Pid, Name) ->
case is_process_alive(Pid) orelse whereis(Name) =:= Pid of
true ->
timer:sleep(10),
wait_for_death(Pid, Name);
false ->
ok
end.

@@ -178,7 +178,7 @@
-export([format/1]).

%% Internal exports
-export([count_tracer/1, rate_tracer/2, formatter/5]).
-export([count_tracer/1, rate_tracer/2, formatter/5, format_trace_output/2]).

-type matchspec() :: [{[term()], [term()], [term()]}].
-type shellfun() :: fun((_) -> term()).
@@ -219,8 +219,8 @@ clear() ->
erlang:trace(all, false, [all]),
erlang:trace_pattern({'_','_','_'}, false, [local,meta,call_count,call_time]),
erlang:trace_pattern({'_','_','_'}, false, []), % unsets global
recon_lib:maybe_kill(recon_trace_tracer),
recon_lib:maybe_kill(recon_trace_formatter),
maybe_kill(recon_trace_tracer),
maybe_kill(recon_trace_formatter),
ok.

%% @equiv calls({Mod, Fun, Args}, Max, [])
@@ -523,7 +523,7 @@ format(TraceMsg) ->
{" '--> ~p:~p/~p", [M,F,Arity]};
%% {trace, Pid, return_from, {M, F, Arity}, ReturnValue}
{return_from, [{M,F,Arity}, Return]} ->
{"~p:~p/~p --> ~s", [M,F,Arity, recon_lib:format_trace_output(Return)]};
{"~p:~p/~p --> ~s", [M,F,Arity, format_trace_output(Return)]};
%% {trace, Pid, exception_from, {M, F, Arity}, {Class, Value}}
{exception_from, [{M,F,Arity}, {Class,Val}]} ->
{"~p:~p/~p ~p ~p", [M,F,Arity, Class, Val]};
@@ -601,12 +601,50 @@ format_args(Arity) when is_integer(Arity) ->
["/", integer_to_list(Arity)];
format_args(Args) when is_list(Args) ->
Active = recon_rec:is_active(),
["(", lists:join(", ", [recon_lib:format_trace_output(Active, Arg) || Arg <- Args]), ")"].
["(", lists:join(", ", [format_trace_output(Active, Arg) || Arg <- Args]), ")"].


%% @doc formats call arguments and return values - most types are just printed out, except for
%% tuples recognised as records, which mimic the source code syntax
%% @end
format_trace_output(Args) ->
format_trace_output(recon_rec:is_active(), Args).

format_trace_output(true, Args) when is_tuple(Args) ->
recon_rec:format_tuple(Args);
format_trace_output(true, Args) when is_list(Args) ->
case io_lib:printable_list(Args) of
true -> io_lib:format("~p", [Args]);
false ->
L = lists:map(fun(A) -> format_trace_output(true, A) end, Args),
"[" ++ string:join(L, ", ") ++ "]"
end;
format_trace_output(_, Args) ->
io_lib:format("~p", [Args]).

%%%%%%%%%%%%%%%
%%% HELPERS %%%
%%%%%%%%%%%%%%%

maybe_kill(Name) ->
case whereis(Name) of
undefined ->
ok;
Pid ->
unlink(Pid),
exit(Pid, kill),
wait_for_death(Pid, Name)
end.

wait_for_death(Pid, Name) ->
case is_process_alive(Pid) orelse whereis(Name) =:= Pid of
true ->
timer:sleep(10),
wait_for_death(Pid, Name);
false ->
ok
end.

%% Borrowed from dbg
fun_to_ms(ShellFun) when is_function(ShellFun) ->
case erl_eval:fun_data(ShellFun) of

0 comments on commit 6973be4

Please sign in to comment.