From 7579fea0ddff6ca010be3292518d37004f34b0ec Mon Sep 17 00:00:00 2001 From: Sasha Goldshtein Date: Mon, 4 Sep 2017 11:06:39 -0400 Subject: [PATCH] tpoint: Make -p switch respect all process' threads This makes -p pass all the process thread ids to the ftrace common_pid filters. Previously, only the main thread would be traced for each process. The -L switch now works for only a single, specified thread. Signed-off-by: Sasha Goldshtein --- examples/tpoint_example.txt | 5 +++-- man/man8/tpoint.8 | 5 ++++- system/tpoint | 27 +++++++++++++++++++-------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/examples/tpoint_example.txt b/examples/tpoint_example.txt index 7dba97c..fecdba2 100644 --- a/examples/tpoint_example.txt +++ b/examples/tpoint_example.txt @@ -184,10 +184,11 @@ making it a read: Use -h to print the USAGE message: # ./tpoint -h -USAGE: tpoint [-hHsv] [-d secs] [-p PID] tracepoint [filter] +USAGE: tpoint [-hHsv] [-d secs] [-p PID] [-L TID] tracepoint [filter] tpoint -l -d seconds # trace duration, and use buffers - -p PID # PID to match on I/O issue + -p PID # PID to match on events + -L TID # thread id to match on events -v # view format file (don't trace) -H # include column headers -l # list all tracepoints diff --git a/man/man8/tpoint.8 b/man/man8/tpoint.8 index d0a95e2..a3e8c93 100644 --- a/man/man8/tpoint.8 +++ b/man/man8/tpoint.8 @@ -3,7 +3,7 @@ tpoint \- trace a given tracepoint. Static tracing. Uses Linux ftrace. .SH SYNOPSIS .B tpoint -[\-hHsv] [\-d secs] [\-p PID] tracepoint [filter] +[\-hHsv] [\-d secs] [\-p PID] [\-L TID] tracepoint [filter] .B tpoint \-l @@ -53,6 +53,9 @@ for use in a custom filter. \-p PID Only trace kernel functions when this process ID is on-CPU. .TP +\-L TID +Only trace kernel functions when this thread ID is on-CPU. +.TP tracepoint A tracepoint name. Eg, block:block_rq_issue. See the EXAMPLES section. .TP diff --git a/system/tpoint b/system/tpoint index 9cd7916..dbc8572 100755 --- a/system/tpoint +++ b/system/tpoint @@ -8,7 +8,7 @@ # printing live tracepoint events only. Wildcards are currently not supported. # If this is insufficient for any reason, use the perf command instead. # -# USAGE: ./tpoint [-hHsv] [-d secs] [-p pid] tracepoint [filter] +# USAGE: ./tpoint [-hHsv] [-d secs] [-p pid] [-L tid] tracepoint [filter] # ./tpoint -l # # Run "tpoint -h" for full usage. @@ -53,16 +53,17 @@ ### default variables tracing=/sys/kernel/debug/tracing flock=/var/tmp/.ftrace-lock; wroteflock=0 -opt_duration=0; duration=; opt_pid=0; pid=; opt_filter=0; filter= -opt_view=0; opt_headers=0; opt_stack=0; dmesg=2 +opt_duration=0; duration=; opt_pid=0; pid=; opt_tid=0; tid= +opt_filter=0; filter=; opt_view=0; opt_headers=0; opt_stack=0; dmesg=2 trap ':' INT QUIT TERM PIPE HUP # sends execution to end tracing section function usage { cat <<-END >&2 - USAGE: tpoint [-hHsv] [-d secs] [-p PID] tracepoint [filter] + USAGE: tpoint [-hHsv] [-d secs] [-p PID] [-L TID] tracepoint [filter] tpoint -l -d seconds # trace duration, and use buffers - -p PID # PID to match on I/O issue + -p PID # PID to match on event + -L TID # thread id to match on event -v # view format file (don't trace) -H # include column headers -l # list all tracepoints @@ -117,11 +118,12 @@ function edie { } ### process options -while getopts d:hHlp:sv opt +while getopts d:hHlp:L:sv opt do case $opt in d) opt_duration=1; duration=$OPTARG ;; p) opt_pid=1; pid=$OPTARG ;; + L) opt_tid=1; tid=$OPTARG ;; H) opt_headers=1 ;; l) opt_list=1 ;; s) opt_stack=1 ;; @@ -141,12 +143,21 @@ if (( !opt_list )); then fi ### option logic -(( opt_pid && opt_filter )) && die "ERROR: use either -p or -f." +(( opt_pid + opt_filter + opt_tid > 1 )) && \ + die "ERROR: use at most one of -p, -L, or filter." (( opt_duration && opt_view )) && die "ERROR: use either -d or -v." if (( opt_pid )); then # convert to filter opt_filter=1 - filter="common_pid == $pid" + # ftrace common_pid is thread id from user's perspective + for tid in /proc/$pid/task/*; do + filter="$filter || common_pid == ${tid##*/}" + done + filter=${filter:3} # trim leading ' || ' (four characters) +fi +if (( opt_tid )); then + opt_filter=1 + filter="common_pid == $tid" fi if (( !opt_view && !opt_list )); then if (( opt_duration )); then