From c0e6907e50921af1bdb911143b105bb7bc206f4e Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Fri, 4 Jun 2021 17:54:28 +0800 Subject: [PATCH] =?UTF-8?q?ft:=20=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c_src/tracer/tpTracerNif.c | 84 ++++++++++++++++++++++++++------------ rebar.config | 8 ++-- src/eTpf.erl | 9 ++-- 3 files changed, 67 insertions(+), 34 deletions(-) diff --git a/c_src/tracer/tpTracerNif.c b/c_src/tracer/tpTracerNif.c index a0a2523..6d60345 100644 --- a/c_src/tracer/tpTracerNif.c +++ b/c_src/tracer/tpTracerNif.c @@ -71,16 +71,23 @@ static void unload(ErlNifEnv *env, void *priv_data) { // enabled(TraceTag, TracerState, Tracee) static ERL_NIF_TERM enabled(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { - int arity; - const ERL_NIF_TERM *tuple; - ErlNifPid tracer_pid; + /* Only generate trace for when tracer != tracee */ + // enif_fprintf(stdout, "IMY************enabled 000:"); + // for (int i = 0; i <= argc - 1; i++) { + // if (i != 10) { + // enif_fprintf(stdout, " %d %T", i, argv[i]); + // } + // } + // enif_fprintf(stdout, "\n"); - if (!enif_get_tuple(env, argv[1], &arity, &tuple)) { - return atom_remove; + if (enif_is_identical(argv[1], argv[2])) { + return atom_discard; } + ErlNifPid tracer_pid; + // Disable the trace when one of the tracers is not a local process. - if (!enif_get_local_pid(env, tuple[0], &tracer_pid)) + if (!enif_get_local_pid(env, argv[1], &tracer_pid)) return atom_remove; // Disable the trace when one of the tracers is not alive. @@ -91,22 +98,38 @@ static ERL_NIF_TERM enabled(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) } static ERL_NIF_TERM enabled_call(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { - // We always want both call and return_to. + // We want both call and return_to when tracer != tracee. + // enif_fprintf(stdout, "IMY************enabled_call 000:"); + // for (int i = 0; i <= argc - 1; i++) { + // if (i != 10) { + // enif_fprintf(stdout, " %d %T", i, argv[i]); + // } + // } + // enif_fprintf(stdout, "\n"); + + if (enif_is_identical(argv[1], argv[2])) { + return atom_discard; + } + return atom_trace; } static ERL_NIF_TERM enabled_procs(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { - int arity; - const ERL_NIF_TERM *tuple; + // int arity; + // const ERL_NIF_TERM *tuple; - if (!enif_get_tuple(env, argv[1], &arity, &tuple)) { - return atom_remove; - } + // if (!enif_get_tuple(env, argv[1], &arity, &tuple)) { + // return atom_remove; + // } - // We only want the spawn and exit events when 'profile' mode - // is enabled. Technically we only care about exits for callgrind, - // but spawn is cheap to keep and useful for message profilers. - if (enif_is_identical(atom_profile, tuple[1]) && !(enif_is_identical(atom_spawn, argv[0]) || enif_is_identical(atom_exit, argv[0]))) { + // // We only want the spawn and exit events when 'profile' mode + // // is enabled. Technically we only care about exits for callgrind, + // // but spawn is cheap to keep and useful for message profilers. + // if (enif_is_identical(atom_profile, tuple[1]) && !(enif_is_identical(atom_spawn, argv[0]) || enif_is_identical(atom_exit, argv[0]))) { + // return atom_discard; + // } + + if (enif_is_identical(argv[1], argv[2])) { return atom_discard; } @@ -114,22 +137,34 @@ static ERL_NIF_TERM enabled_procs(ErlNifEnv *env, int argc, const ERL_NIF_TERM a } static ERL_NIF_TERM enabled_send(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { - // We always want both send and send_to_non_existing_process. + // We want both send and send_to_non_existing_process when tracer != tracee. + if (enif_is_identical(argv[1], argv[2])) { + return atom_discard; + } return atom_trace; } static ERL_NIF_TERM enabled_receive(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { - // We always want receive. + // We want receive when tracer != tracee. + if (enif_is_identical(argv[1], argv[2])) { + return atom_discard; + } return atom_trace; } static ERL_NIF_TERM enabled_running_procs(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { - // We always want both in and out. + // We want both in and out tracer != tracee. + if (enif_is_identical(argv[1], argv[2])) { + return atom_discard; + } return atom_trace; } static ERL_NIF_TERM enabled_garbage_collection(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { - // We always want both gc_minor_start, gc_max_heap_size, and gc_minor_end. + // We want both gc_minor_start, gc_max_heap_size, and gc_minor_end tracer != tracee. + if (enif_is_identical(argv[1], argv[2])) { + return atom_discard; + } return atom_trace; } @@ -144,16 +179,11 @@ static ERL_NIF_TERM trace(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { // } // enif_fprintf(stdout, "\n"); - int arity, has_extra, has_mspec; - const ERL_NIF_TERM *tuple; + int has_extra, has_mspec; ErlNifPid tracer_pid; - if (!enif_get_tuple(env, argv[1], &arity, &tuple)) { - return atom_ok; - } - // Disable the trace when one of the tracers is not a local process. - if (!enif_get_local_pid(env, tuple[0], &tracer_pid)) + if (!enif_get_local_pid(env, argv[1], &tracer_pid)) return atom_ok; ERL_NIF_TERM ts, extra, mspec, msg; diff --git a/rebar.config b/rebar.config index 55b25b3..28a19f4 100644 --- a/rebar.config +++ b/rebar.config @@ -10,7 +10,7 @@ {apps, [eTpf]} ]}. -% {pre_hooks, -% [{"", compile, "escript c_src/eNpc compile"}]}. -% {post_hooks, -% [{"", clean, "escript c_src/eNpc clean"}]}. +{pre_hooks, + [{"", compile, "escript c_src/eNpc compile"}]}. +{post_hooks, + [{"", clean, "escript c_src/eNpc clean"}]}. diff --git a/src/eTpf.erl b/src/eTpf.erl index 0e89982..7a769a9 100644 --- a/src/eTpf.erl +++ b/src/eTpf.erl @@ -3,7 +3,8 @@ -include("eTpf.hrl"). -export([ - trace/1 + start/0 + , trace/1 , trace/2 , trace/3 , trace/4 @@ -12,6 +13,8 @@ ]). +start() -> + trace([{scope, [all]}]). -spec trace(userInput()) -> ok. trace(Input) -> @@ -41,11 +44,11 @@ do_trace(InputList, TracerMod, TracerOpts, TraceOpts) -> type => worker }), - Mode = maps:get(mode, TraceOpts, trace), + _Mode = maps:get(mode, TraceOpts, trace), Tem0InputList = flattenInput(InputList, []), Tem1InputList = ensurePattern(Tem0InputList), LastInputList = ensureScope(Tem1InputList), - traceInput(LastInputList, {TracerPid, Mode}, TraceOpts), + traceInput(LastInputList, TracerPid, TraceOpts), ok. flattenInput([], Acc) ->