Skip to content

Commit 8d196f6

Browse files
committedSep 4, 2017
functrace: Make -p switch respect all the process' threads
Signed-off-by: Sasha Goldshtein <[email protected]>
1 parent cf7532a commit 8d196f6

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed
 

‎examples/functrace_example.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,12 @@ and events were missed.
325325
Use -h to print the USAGE message:
326326

327327
# ./functrace -h
328-
USAGE: functrace [-hH] [-p PID] [-d secs] funcstring
328+
USAGE: functrace [-hH] [-p PID] [-L TID] [-d secs] funcstring
329329
-d seconds # trace duration, and use buffers
330330
-h # this usage message
331331
-H # include column headers
332332
-p PID # trace when this pid is on-CPU
333+
-L TID # trace when this thread is on-CPU
333334
eg,
334335
functrace do_nanosleep # trace the do_nanosleep() function
335336
functrace '*sleep' # trace functions ending in "sleep"

‎kernel/functrace

+22-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
# This is a proof of concept using Linux ftrace capabilities on older kernels.
77
#
8-
# USAGE: functrace [-hH] [-p PID] [-d secs] funcstring
8+
# USAGE: functrace [-hH] [-p PID] [-L TID] [-d secs] funcstring
99
# eg,
1010
# functrace '*sleep' # trace all functions ending in "sleep"
1111
#
@@ -59,16 +59,18 @@
5959
### default variables
6060
tracing=/sys/kernel/debug/tracing
6161
flock=/var/tmp/.ftrace-lock
62-
opt_duration=0; duration=; opt_pid=0; pid=; pidtext=; opt_headers=0
62+
opt_duration=0; duration=; opt_pid=0; pid=; opt_tid=0; tid=; pidtext=
63+
opt_headers=0
6364
trap ':' INT QUIT TERM PIPE HUP # sends execution to end tracing section
6465

6566
function usage {
6667
cat <<-END >&2
67-
USAGE: functrace [-hH] [-p PID] [-d secs] funcstring
68+
USAGE: functrace [-hH] [-p PID] [-L TID] [-d secs] funcstring
6869
-d seconds # trace duration, and use buffers
6970
-h # this usage message
7071
-H # include column headers
7172
-p PID # trace when this pid is on-CPU
73+
-L TID # trace when this thread is on-CPU
7274
eg,
7375
functrace do_nanosleep # trace the do_nanosleep() function
7476
functrace '*sleep' # trace functions ending in "sleep"
@@ -93,7 +95,7 @@ function end {
9395
echo "Ending tracing..." 2>/dev/null
9496
cd $tracing
9597
warn "echo nop > current_tracer"
96-
(( opt_pid )) && warn "echo > set_ftrace_pid"
98+
(( opt_pid || opt_tid )) && warn "echo > set_ftrace_pid"
9799
warn "echo > set_ftrace_filter"
98100
warn "echo > trace"
99101
(( wroteflock )) && warn "rm $flock"
@@ -113,11 +115,12 @@ function edie {
113115
}
114116

115117
### process options
116-
while getopts d:hHp: opt
118+
while getopts d:hHp:L: opt
117119
do
118120
case $opt in
119121
d) opt_duration=1; duration=$OPTARG ;;
120122
p) opt_pid=1; pid=$OPTARG ;;
123+
L) opt_tid=1; tid=$OPTARG ;;
121124
H) opt_headers=1; ;;
122125
h|?) usage ;;
123126
esac
@@ -126,8 +129,10 @@ shift $(( $OPTIND - 1 ))
126129

127130
### option logic
128131
(( $# == 0 )) && usage
132+
(( opt_pid && opt_tid )) && edie "ERROR: You can use -p or -L but not both."
129133
funcs="$1"
130134
(( opt_pid )) && pidtext=" for PID $pid"
135+
(( opt_tid )) && pidtext=" for TID $pid"
131136
if (( opt_duration )); then
132137
echo "Tracing \"$funcs\"$pidtext for $duration seconds..."
133138
else
@@ -148,9 +153,18 @@ sysctl -q kernel.ftrace_enabled=1 # doesn't set exit status
148153
read mode < current_tracer
149154
[[ "$mode" != "nop" ]] && edie "ERROR: ftrace active (current_tracer=$mode)"
150155
if (( opt_pid )); then
151-
if ! echo $pid > set_ftrace_pid; then
152-
edie "ERROR: setting -p $pid (PID exist?). Exiting."
153-
fi
156+
echo > set_ftrace_pid
157+
# ftrace expects kernel pids, which are thread ids
158+
for tid in /proc/$pid/task/*; do
159+
if ! echo ${tid##*/} >> set_ftrace_pid; then
160+
edie "ERROR: setting -p $pid (PID exist?). Exiting."
161+
fi
162+
done
163+
fi
164+
if (( opt_tid )); then
165+
if ! echo $tid > set_ftrace_pid; then
166+
edie "ERROR: setting -L $tid (TID exist?). Exiting."
167+
fi
154168
fi
155169
if ! echo "$funcs" > set_ftrace_filter; then
156170
edie "ERROR: enabling \"$funcs\". Exiting."

‎man/man8/functrace.8

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
functrace \- trace kernel function calls matching specified wildcards. Uses Linux ftrace.
44
.SH SYNOPSIS
55
.B functrace
6-
[\-hH] [\-p PID] [\-d secs] funcstring
6+
[\-hH] [\-p PID] [\-L TID] [\-d secs] funcstring
77
.SH DESCRIPTION
88
This tool provides a quick way to capture the execution of kernel functions,
99
showing basic details including as the process ID, timestamp, and calling
@@ -45,6 +45,9 @@ Print column headers.
4545
\-p PID
4646
Only trace kernel functions when this process ID is on-CPU.
4747
.TP
48+
\-L TID
49+
Only trace kernel functions when this thread ID is on-CPU.
50+
.TP
4851
funcstring
4952
A function name to trace, which may include file glob style wildcards ("*") at
5053
the beginning or ending of a string only. Eg, "vfs*" means match "vfs" followed

0 commit comments

Comments
 (0)
Please sign in to comment.