• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# reset-trace - reset state of tracing, disabling all tracing.
4#               Written for Linux.
5#
6# If a bcc tool crashed and you suspect tracing is partially enabled, you
7# can use this tool to reset the state of tracing, disabling anything still
8# enabled. Only use this tool in the case of error, and, consider filing a
9# bcc ticket so we can fix the error.
10#
11# bcc-used tracing facilities are reset. Other tracing facilities (ftrace) are
12# checked, and if not in an expected state, a note is printed. All tracing
13# files can be reset with -F for force, but this will interfere with any other
14# running tracing sessions (eg, ftrace).
15#
16# USAGE: ./reset-trace [-Fhqv]
17#
18# REQUIREMENTS: debugfs mounted on /sys/kernel/debug
19#
20# COPYRIGHT: Copyright (c) 2016 Brendan Gregg.
21# Licensed under the Apache License, Version 2.0 (the "License")
22#
23# 20-Jul-2014	Brendan Gregg	Created this.
24# 18-Oct-2016      "      "     Updated for bcc use.
25
26tracing=/sys/kernel/debug/tracing
27opt_force=0; opt_verbose=0; opt_quiet=0
28
29function usage {
30	cat <<-END >&2
31	USAGE: reset-trace [-Fhqv]
32	                 -F             # force: reset all tracing files
33	                 -v             # verbose: print details while working
34	                 -h             # this usage message
35	                 -q             # quiet: no output
36	  eg,
37	       reset-trace              # disable semi-enabled tracing
38END
39	exit
40}
41
42function die {
43	echo >&2 "$@"
44	exit 1
45}
46
47function vecho {
48	(( ! opt_verbose )) && return
49	echo "$@"
50}
51
52function writefile {
53	file=$1
54	write=$2
55	if [[ ! -w $file ]]; then
56		echo >&2 "WARNING: file $file not writable/exists. Skipping."
57		return
58	fi
59
60	vecho "Checking $PWD/$file"
61        contents=$(grep -v '^#' $file)
62	if [[ "$contents" != "$expected" ]]; then
63		(( ! opt_quiet )) && echo "Needed to reset $PWD/$file"
64		vecho "$file, before (line enumerated):"
65		(( opt_verbose )) && cat -nv $file
66		cmd="echo $write > $file"
67		if ! eval "$cmd"; then
68			echo >&2 "WARNING: command failed \"$cmd\"." \
69			    "bcc still running? Continuing."
70		fi
71		vecho "$file, after (line enumerated):"
72		(( opt_verbose )) && cat -nv $file
73		vecho
74	fi
75}
76
77# only write when force is used
78function checkfile {
79	file=$1
80	write=$2
81	expected=$3
82	if [[ ! -e $file ]]; then
83		echo >&2 "WARNING: file $file doesn't exist. Skipping."
84		return
85	fi
86	if (( opt_force )); then
87		writefile $file $write
88		return
89	fi
90	(( opt_quiet )) && return
91
92	vecho "Checking $PWD/$file"
93        contents=$(grep -v '^#' $file)
94	if [[ "$contents" != "$expected" ]]; then
95		echo "Noticed unrelated tracing file $PWD/$file isn't set as" \
96		    "expected. Not reseting (-F to force, -v for verbose)."
97		vecho "Contents of $file is (line enumerated):"
98		(( opt_verbose )) && cat -nv $file
99		vecho "Expected \"$expected\"."
100	fi
101}
102
103### process options
104while getopts Fhqv opt
105do
106	case $opt in
107	F)	opt_force=1 ;;
108	q)	opt_quiet=1 ;;
109	v)	opt_verbose=1 ;;
110	h|?)	usage ;;
111	esac
112done
113shift $(( $OPTIND - 1 ))
114
115### reset tracing state
116vecho "Reseting tracing state..."
117vecho
118cd $tracing || die "ERROR: accessing tracing. Root user? /sys/kernel/debug?"
119
120# files bcc uses
121writefile kprobe_events "" ""
122writefile uprobe_events "" ""
123writefile trace "" ""         # clears trace_pipe
124
125# non-bcc files
126checkfile current_tracer nop nop
127checkfile set_ftrace_filter "" ""
128checkfile set_graph_function "" ""
129checkfile set_ftrace_pid "" "no pid"
130checkfile events/enable 0 0
131checkfile tracing_thresh 0 0
132checkfile tracing_on 1 1
133
134vecho
135vecho "Done."
136