Skip to content

Commit 7adba97

Browse files
committed
Return incomplete ranges as well, and make this configurable
1 parent 319a940 commit 7adba97

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/tr.erl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,17 @@
138138
%% Source of traces: an ETS table or a list of traces. Default: `tab()'.
139139

140140
-type range_options() :: #{tab => tr_source(),
141-
max_depth => limit()}.
141+
max_depth => limit(),
142+
output => range_output()}.
142143
%% Options for trace ranges.
143144
%%
144145
%% Optional `limit' is the maximum depth of calls in the returned ranges.
145146
%% All traces (including messages) exceeding that depth are skipped.
146147

148+
-type range_output() :: complete | incomplete | all.
149+
%% Which ranges to return. Incomplete ranges are missing at least one return.
150+
%% By default, all ranges are returned.
151+
147152
-type tb_options() :: #{tab => tr_source(),
148153
output => tb_output(),
149154
format => tb_format(),
@@ -424,10 +429,21 @@ ranges(PredF) ->
424429
-spec ranges(pred(), range_options()) -> [[tr()]].
425430
ranges(PredF, Options) when is_map(Options) ->
426431
Tab = maps:get(tab, Options, tab()),
432+
Output = maps:get(output, Options, all),
427433
InitialState = maps:merge(#{traces => [], pid_states => #{}, max_depth => infinity}, Options),
428-
#{traces := Traces} = foldl(fun(T, S) -> range_step(PredF, T, S) end, InitialState, Tab),
434+
FinalState = foldl(fun(T, S) -> range_step(PredF, T, S) end, InitialState, Tab),
435+
complete_ranges(FinalState, Output) ++ incomplete_ranges(FinalState, Output).
436+
437+
complete_ranges(#{}, incomplete) ->
438+
[];
439+
complete_ranges(#{traces := Traces}, Output) when Output =:= all; Output =:= complete ->
429440
lists:reverse(Traces).
430441

442+
incomplete_ranges(#{}, complete) ->
443+
[];
444+
incomplete_ranges(#{pid_states := States}, Output) when Output =:= all; Output =:= incomplete ->
445+
lists:sort([Range || #{trace := Range} <- maps:values(States)]).
446+
431447
%% @doc Prints sorted call time statistics for the selected traces from `tab()'.
432448
%%
433449
%% The statistics are sorted according to `t:acc_time()', descending.

test/tr_SUITE.erl

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ groups() ->
4444
{range, [ranges,
4545
ranges_max_depth,
4646
range,
47-
ranges_with_messages]},
47+
ranges_with_messages,
48+
incomplete_ranges]},
4849
{traceback, [single_tb,
4950
single_tb_with_messages,
5051
tb,
@@ -243,6 +244,37 @@ ranges_with_messages(_Config) ->
243244
Range1 = [hd(Traces), lists:last(Traces)],
244245
[Range1] = tr:ranges(fun(#tr{}) -> true end, #{max_depth => 1}).
245246

247+
incomplete_ranges(_Config) ->
248+
Self = self(),
249+
tr:trace(#{modules => [MFA = {?MODULE, wait_and_reply, 1}]}),
250+
Pid1 = spawn_link(?MODULE, wait_and_reply, [self()]),
251+
Pid2 = spawn_link(?MODULE, wait_and_reply, [self()]),
252+
receive {started, Pid1} -> ok end,
253+
receive {started, Pid2} -> ok end,
254+
Pid2 ! reply,
255+
receive {finished, Pid2} -> ok end,
256+
257+
%% Pid1 finished, but Pid1 didn't
258+
wait_for_traces(3),
259+
tr:stop_tracing(),
260+
Pid1 ! reply,
261+
receive {finished, Pid1} -> ok end,
262+
263+
[#tr{index = 1, pid = Pid1, event = call, mfa = MFA, data = [Self]},
264+
#tr{index = 2, pid = Pid2, event = call, mfa = MFA, data = [Self]},
265+
#tr{index = 3, pid = Pid2, event = return, mfa = MFA, data = {finished, Pid2}}] =
266+
[T1, T2, T3] = tr:select(),
267+
268+
%% Ranges with missing returns are included at the end
269+
[[T2, T3], [T1]] = tr:ranges(fun(#tr{}) -> true end),
270+
[[T2, T3], [T1]] = tr:ranges(fun(#tr{}) -> true end, #{output => all}),
271+
272+
%% Skip ranges with missing returns
273+
[[T2, T3]] = tr:ranges(fun(#tr{}) -> true end, #{output => complete}),
274+
275+
%% Skip ranges with missing returns
276+
[[T1]] = tr:ranges(fun(#tr{}) -> true end, #{output => incomplete}).
277+
246278
do(_Config) ->
247279
tr:trace([{?MODULE, fib, 1}]),
248280
?MODULE:fib(2),

0 commit comments

Comments
 (0)