-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetracing.sh
executable file
·131 lines (118 loc) · 2.72 KB
/
getracing.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
TRACEROOT=""
CURRENTTRACER="nop"
LATENCYENABLED=0
ENABLED=0
PID=-1
function usage () {
cat <<-EOF
Usage: $(basename $0) <tracer> [-p <pid>] [ -l] [ n of lines ]
tracer: one of the tracers:
blk function_graph mmiotrace wakeup_rt wakeup irqsoff
function sched_switch
-l
--latency: specify it to get the latency information on tracing
output
-p
--pid: specify a pid for the tracers that allow pid filtering
EOF
exit 10
} >&2
function do_error () {
printf "$@"
exit 10
} >&2
function find_debugfs () {
DEBUGFS=$(mount -t debugfs | cut -d\ -f3)
if [ ! -z $DEBUGFS ]
then
TRACEROOT="${DEBUGFS}/tracing"
else
do_error "You need the debugfs mounted somewhere\n"
fi
}
function enable_tracing () {
local ENABLERROR=0
echo 1 > $TRACEROOT/tracing_enabled
ENABLERROR=$(( ENABLERROR + $? ))
echo $CURRENTTRACER > $TRACEROOT/current_tracer
ENABLERROR=$(( ENABLERROR + $? ))
if [ $LATENCYENABLED -eq 1 ]
then
echo "latency-format" > $TRACEROOT/trace_options
ENABLERROR=$(( ENABLERROR + $? ))
else
echo "nolatency-format" > $TRACEROOT/trace_options
fi
if [ $CURRENTTRACER == "function" ]
then
sysctl kernel.ftrace_enabled=1
ENABLERROR=$(( ENABLERROR + $? ))
fi
if [ $PID -ne -1 ]
then
echo $PID > $TRACEROOT/set_ftrace_pid
ENABLERROR=$(( ENABLERROR + $? ))
fi
[ $ENABLERROR -ne 0 ] && do_error "Error enabling tracing\n"
ENABLED=1
}
function disable_tracing () {
if [ $ENABLED -eq 0 ]
then
return
fi
local DISABLERROR=0
echo 0 > $TRACEROOT/tracing_enabled
DISABLERROR=$(( DISABLERROR + $? ))
echo "nop" > $TRACEROOT/current_tracer
DISABLERROR=$(( DISABLERROR + $? ))
echo > $TRACEROOT/set_ftrace_pid
DISABLERROR=$(( DISABLERROR + $? ))
if [ $CURRENTTRACER == "function" ]
then
sysctl kernel.ftrace_enabled=0
fi
if [ $DISABLERROR -ne 0 ]
then
do_error "Something went wrong while disabling the tracing\n"
else
echo "Tracing disabled"
ENABLED=0
fi
}
function show_trace () {
cat $TRACEROOT/trace_pipe
}
# Main
# Only starts if you're root
[ $(whoami) != 'root' ] && do_error "You must be root to enable tracing\n"
# Trap Ctrl+C to correctly disable the tracing
trap disable_tracing SIGINT
# Argument parsing
if (( $# < 1 ))
then
usage
fi
while (( $# > 0 ))
do
case $1 in
function|blk|function_graph|mmiotrace|wakeup_rt|wakeup|irqsoff|function|sched_switch)
CURRENTTRACER=$1
shift
;;
-l|--latency)
LATENCYENABLED=1
shift
;;
-p|--pid)
PID=$2
shift 2
;;
esac
done
# Do the job
find_debugfs
enable_tracing
show_trace
disable_tracing