diff options
Diffstat (limited to 'lib/runtime_tools/src')
-rw-r--r-- | lib/runtime_tools/src/dyntrace.erl | 171 |
1 files changed, 122 insertions, 49 deletions
diff --git a/lib/runtime_tools/src/dyntrace.erl b/lib/runtime_tools/src/dyntrace.erl index 388c7679b9..b4579fd5ce 100644 --- a/lib/runtime_tools/src/dyntrace.erl +++ b/lib/runtime_tools/src/dyntrace.erl @@ -6,23 +6,22 @@ %%% work on any operating system platform where user-space DTrace/Systemtap %%% (and in the future LttNG UST) probes are supported. %%% -%%% Use the `dyntrace:init()' function to load the NIF shared library and -%%% to initialize library's private state. -%%% %%% It is recommended that you use the `dyntrace:p()' function to add %%% Dynamic trace probes to your Erlang code. This function can accept up to %%% four integer arguments and four string arguments; the integer -%%% argument(s) must come before any string argument. For example: +%%% argument(s) must come before any string argument. +%%% +%%% If using DTrace, enable the dynamic trace probe using the 'dtrace' +%%% command, for example: +%%% +%%% dtrace -s /your/path/to/lib/runtime_tools-1.8.7/examples/user-probe.d +%%% +%%% Then, back at the Erlang shell, try this example: %%% ``` %%% 1> dyntrace:put_tag("GGOOOAAALL!!!!!"). %%% true -%%% 2> dyntrace:init(). -%%% ok -%%% -%%% % % % If using dtrace, enable the Dynamic trace probe using the 'dtrace' -%%% % % % command. %%% -%%% 3> dyntrace:p(7, 8, 9, "one", "four"). +%%% 2> dyntrace:p(7, 8, 9, "one", "four"). %%% true %%% ''' %%% @@ -38,15 +37,16 @@ -export([available/0, user_trace_s1/1, % TODO: unify with pid & tag args like user_trace_i4s4 - p/0, p/1, p/2, p/3, p/4, p/5, p/6, p/7, p/8]). + p/0, p/1, p/2, p/3, p/4, p/5, p/6, p/7, p/8, + pn/1, pn/2, pn/3, pn/4, pn/5, pn/6, pn/7, pn/8, pn/9]). -export([put_tag/1, get_tag/0, get_tag_data/0, spread_tag/1, restore_tag/1]). --export([scaff/0]). % Development only -export([user_trace_i4s4/9]). % Know what you're doing! -on_load(on_load/0). -type probe_arg() :: integer() | iolist(). -type int_p_arg() :: integer() | iolist() | undef. +-type n_probe_label() :: 0..1023. %% The *_maybe() types use atom() instead of a stricter 'undef' %% because user_trace_i4s4/9 is exposed to the outside world, and @@ -115,6 +115,16 @@ user_trace_s1(_Message) -> user_trace_i4s4(_, _, _, _, _, _, _, _, _) -> erlang:nif_error(nif_not_loaded). +-spec user_trace_n(n_probe_label(), iolist(), + integer_maybe(), integer_maybe(), + integer_maybe(), integer_maybe(), + iolist_maybe(), iolist_maybe(), + iolist_maybe(), iolist_maybe()) -> + true | false | error | badarg. + +user_trace_n(_, _, _, _, _, _, _, _, _, _) -> + erlang:nif_error(nif_not_loaded). + %%% %%% Erlang support functions %%% @@ -218,6 +228,106 @@ user_trace_int(I1, I2, I3, I4, S1, S2, S3, S4) -> false end. +-spec pn(n_probe_label()) -> true | false | error | badarg. + +pn(ProbeLabel) -> + user_trace_n_int(ProbeLabel, undef, undef, undef, undef, undef, undef, undef, undef). + +-spec pn(n_probe_label(), probe_arg()) -> true | false | error | badarg. + +pn(ProbeLabel, I1) when is_integer(I1) -> + user_trace_n_int(ProbeLabel, I1, undef, undef, undef, undef, undef, undef, undef); +pn(ProbeLabel, S1) -> + user_trace_n_int(ProbeLabel, undef, undef, undef, undef, S1, undef, undef, undef). + +-spec pn(n_probe_label(), probe_arg(), probe_arg()) -> true | false | error | badarg. + +pn(ProbeLabel, I1, I2) when is_integer(I1), is_integer(I2) -> + user_trace_n_int(ProbeLabel, I1, I2, undef, undef, undef, undef, undef, undef); +pn(ProbeLabel, I1, S1) when is_integer(I1) -> + user_trace_n_int(ProbeLabel, I1, undef, undef, undef, S1, undef, undef, undef); +pn(ProbeLabel, S1, S2) -> + user_trace_n_int(ProbeLabel, undef, undef, undef, undef, S1, S2, undef, undef). + +-spec pn(n_probe_label(), probe_arg(), probe_arg(), probe_arg()) -> true | false | error | badarg. + +pn(ProbeLabel, I1, I2, I3) when is_integer(I1), is_integer(I2), is_integer(I3) -> + user_trace_n_int(ProbeLabel, I1, I2, I3, undef, undef, undef, undef, undef); +pn(ProbeLabel, I1, I2, S1) when is_integer(I1), is_integer(I2) -> + user_trace_n_int(ProbeLabel, I1, I2, undef, undef, S1, undef, undef, undef); +pn(ProbeLabel, I1, S1, S2) when is_integer(I1) -> + user_trace_n_int(ProbeLabel, I1, undef, undef, undef, S1, S2, undef, undef); +pn(ProbeLabel, S1, S2, S3) -> + user_trace_n_int(ProbeLabel, undef, undef, undef, undef, S1, S2, S3, undef). + +-spec pn(n_probe_label(), probe_arg(), probe_arg(), probe_arg(), probe_arg()) -> + true | false | error | badarg. + +pn(ProbeLabel, I1, I2, I3, I4) when is_integer(I1), is_integer(I2), is_integer(I3), is_integer(I4) -> + user_trace_n_int(ProbeLabel, I1, I2, I3, I4, undef, undef, undef, undef); +pn(ProbeLabel, I1, I2, I3, S1) when is_integer(I1), is_integer(I2), is_integer(I3) -> + user_trace_n_int(ProbeLabel, I1, I2, I3, undef, S1, undef, undef, undef); +pn(ProbeLabel, I1, I2, S1, S2) when is_integer(I1), is_integer(I2) -> + user_trace_n_int(ProbeLabel, I1, I2, undef, undef, S1, S2, undef, undef); +pn(ProbeLabel, I1, S1, S2, S3) when is_integer(I1) -> + user_trace_n_int(ProbeLabel, I1, undef, undef, undef, S1, S2, S3, undef); +pn(ProbeLabel, S1, S2, S3, S4) -> + user_trace_n_int(ProbeLabel, undef, undef, undef, undef, S1, S2, S3, S4). + +-spec pn(n_probe_label(), probe_arg(), probe_arg(), probe_arg(), probe_arg(), + probe_arg()) -> + true | false | error | badarg. + +pn(ProbeLabel, I1, I2, I3, I4, S1) when is_integer(I1), is_integer(I2), is_integer(I3), is_integer(I4) -> + user_trace_n_int(ProbeLabel, I1, I2, I3, I4, S1, undef, undef, undef); +pn(ProbeLabel, I1, I2, I3, S1, S2) when is_integer(I1), is_integer(I2), is_integer(I3) -> + user_trace_n_int(ProbeLabel, I1, I2, I3, undef, S1, S2, undef, undef); +pn(ProbeLabel, I1, I2, S1, S2, S3) when is_integer(I1), is_integer(I2) -> + user_trace_n_int(ProbeLabel, I1, I2, undef, undef, S1, S2, S3, undef); +pn(ProbeLabel, I1, S1, S2, S3, S4) when is_integer(I1) -> + user_trace_n_int(ProbeLabel, I1, undef, undef, undef, S1, S2, S3, S4). + +-spec pn(n_probe_label(), probe_arg(), probe_arg(), probe_arg(), probe_arg(), + probe_arg(), probe_arg()) -> + true | false | error | badarg. + +pn(ProbeLabel, I1, I2, I3, I4, S1, S2) when is_integer(I1), is_integer(I2), is_integer(I3), is_integer(I4) -> + user_trace_n_int(ProbeLabel, I1, I2, I3, I4, S1, S2, undef, undef); +pn(ProbeLabel, I1, I2, I3, S1, S2, S3) when is_integer(I1), is_integer(I2), is_integer(I3) -> + user_trace_n_int(ProbeLabel, I1, I2, I3, undef, S1, S2, S3, undef); +pn(ProbeLabel, I1, I2, S1, S2, S3, S4) when is_integer(I1), is_integer(I2) -> + user_trace_n_int(ProbeLabel, I1, I2, undef, undef, S1, S2, S3, S4). + +-spec pn(n_probe_label(), probe_arg(), probe_arg(), probe_arg(), probe_arg(), + probe_arg(), probe_arg(), probe_arg()) -> + true | false | error | badarg. + +pn(ProbeLabel, I1, I2, I3, I4, S1, S2, S3) when is_integer(I1), is_integer(I2), is_integer(I3), is_integer(I4) -> + user_trace_n_int(ProbeLabel, I1, I2, I3, I4, S1, S2, S3, undef); +pn(ProbeLabel, I1, I2, I3, S1, S2, S3, S4) when is_integer(I1), is_integer(I2), is_integer(I3) -> + user_trace_n_int(ProbeLabel, I1, I2, I3, undef, S1, S2, S3, S4). + +-spec pn(n_probe_label(), probe_arg(), probe_arg(), probe_arg(), probe_arg(), + probe_arg(), probe_arg(), probe_arg(), probe_arg()) -> + true | false | error | badarg. + +pn(ProbeLabel, I1, I2, I3, I4, S1, S2, S3, S4) when is_integer(I1), is_integer(I2), is_integer(I3), is_integer(I4) -> + user_trace_n_int(ProbeLabel, I1, I2, I3, I4, S1, S2, S3, S4). + +-spec user_trace_n_int(n_probe_label(), + int_p_arg(), int_p_arg(), int_p_arg(), int_p_arg(), + int_p_arg(), int_p_arg(), int_p_arg(), int_p_arg()) -> + true | false | error | badarg. + +user_trace_n_int(ProbeLabel, I1, I2, I3, I4, S1, S2, S3, S4) -> + UTag = get_tag(), + try + user_trace_n(ProbeLabel, UTag, I1, I2, I3, I4, S1, S2, S3, S4) + catch + error:nif_not_loaded -> + false + end. + -spec put_tag(undefined | iodata()) -> binary() | undefined. put_tag(Data) -> erlang:dt_put_tag(unicode:characters_to_binary(Data)). @@ -240,40 +350,3 @@ spread_tag(B) -> -spec restore_tag(true | {non_neg_integer(), binary() | []}) -> true. restore_tag(T) -> erlang:dt_restore_tag(T). - - -%% Scaffolding to write tedious code: quick brute force and not 100% correct. - -scaff_int_args(N) -> - L = lists:sublist(["I1", "I2", "I3", "I4"], N), - [string:join(L, ", ")]. - -scaff_int_guards(N) -> - L = lists:sublist(["is_integer(I1)", "is_integer(I2)", "is_integer(I3)", - "is_integer(I4)"], N), - lists:flatten(string:join(L, ", ")). - -scaff_char_args(N) -> - L = lists:sublist(["S1", "S2", "S3", "S4"], N), - [string:join(L, ", ")]. - -scaff_fill(N) -> - [string:join(lists:duplicate(N, "undef"), ", ")]. - -scaff() -> - L = [begin - IntArgs = scaff_int_args(N_int), - IntGuards = scaff_int_guards(N_int), - IntFill = scaff_fill(4 - N_int), - CharArgs = scaff_char_args(N_char), - CharFill = scaff_fill(4 - N_char), - InArgs = string:join(IntArgs ++ CharArgs, ", "), - OutArgs = string:join(IntArgs ++ IntFill ++ CharArgs ++ CharFill, - ", "), - {N_int + N_char, - lists:flatten([io_lib:format("p(~s) when ~s ->\n", - [InArgs, IntGuards]), - io_lib:format(" user_trace_int(~s);\n", [OutArgs]) - ])} - end || N_int <- [0,1,2,3,4], N_char <- [0,1,2,3,4]], - [io:format("%%~p\n~s", [N, Str]) || {N, Str} <- lists:sort(L)]. |