This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset-trace.sh
executable file
·135 lines (122 loc) · 3.41 KB
/
reset-trace.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
132
133
134
135
#!/bin/bash
#
# reset-trace - reset state of tracing, disabling all tracing.
# Written for Linux.
#
# If a bcc tool crashed and you suspect tracing is partially enabled, you
# can use this tool to reset the state of tracing, disabling anything still
# enabled. Only use this tool in the case of error, and, consider filing a
# bcc ticket so we can fix the error.
#
# bcc-used tracing facilities are reset. Other tracing facilities (ftrace) are
# checked, and if not in an expected state, a note is printed. All tracing
# files can be reset with -F for force, but this will interfere with any other
# running tracing sessions (eg, ftrace).
#
# USAGE: ./reset-trace [-Fhqv]
#
# REQUIREMENTS: debugfs mounted on /sys/kernel/debug
#
# COPYRIGHT: Copyright (c) 2016 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 20-Jul-2014 Brendan Gregg Created this.
# 18-Oct-2016 " " Updated for bcc use.
tracing=/sys/kernel/debug/tracing
opt_force=0; opt_verbose=0; opt_quiet=0
function usage {
cat <<-END >&2
USAGE: reset-trace [-Fhqv]
-F # force: reset all tracing files
-v # verbose: print details while working
-h # this usage message
-q # quiet: no output
eg,
reset-trace # disable semi-enabled tracing
END
exit
}
function die {
echo >&2 "$@"
exit 1
}
function vecho {
(( ! opt_verbose )) && return
echo "$@"
}
function writefile {
file=$1
write=$2
if [[ ! -w $file ]]; then
echo >&2 "WARNING: file $file not writable/exists. Skipping."
return
fi
vecho "Checking $PWD/$file"
contents=$(grep -v '^#' $file)
if [[ "$contents" != "$expected" ]]; then
(( ! opt_quiet )) && echo "Needed to reset $PWD/$file"
vecho "$file, before (line enumerated):"
(( opt_verbose )) && cat -nv $file
cmd="echo $write > $file"
if ! eval "$cmd"; then
echo >&2 "WARNING: command failed \"$cmd\"." \
"bcc still running? Continuing."
fi
vecho "$file, after (line enumerated):"
(( opt_verbose )) && cat -nv $file
vecho
fi
}
# only write when force is used
function checkfile {
file=$1
write=$2
expected=$3
if [[ ! -e $file ]]; then
echo >&2 "WARNING: file $file doesn't exist. Skipping."
return
fi
if (( opt_force )); then
writefile $file $write
return
fi
(( opt_quiet )) && return
vecho "Checking $PWD/$file"
contents=$(grep -v '^#' $file)
if [[ "$contents" != "$expected" ]]; then
echo "Noticed unrelated tracing file $PWD/$file isn't set as" \
"expected. Not reseting (-F to force, -v for verbose)."
vecho "Contents of $file is (line enumerated):"
(( opt_verbose )) && cat -nv $file
vecho "Expected \"$expected\"."
fi
}
### process options
while getopts Fhqv opt
do
case $opt in
F) opt_force=1 ;;
q) opt_quiet=1 ;;
v) opt_verbose=1 ;;
h|?) usage ;;
esac
done
shift $(( $OPTIND - 1 ))
### reset tracing state
vecho "Reseting tracing state..."
vecho
cd $tracing || die "ERROR: accessing tracing. Root user? /sys/kernel/debug?"
# files bcc uses
writefile kprobe_events "" ""
writefile uprobe_events "" ""
writefile trace "" "" # clears trace_pipe
# non-bcc files
checkfile current_tracer nop nop
checkfile set_ftrace_filter "" ""
checkfile set_graph_function "" ""
checkfile set_ftrace_pid "" "no pid"
checkfile events/enable 0 0
checkfile tracing_thresh 0 0
checkfile tracing_on 1 1
vecho
vecho "Done."