#!/bin/bash # # reset-ftrace - reset state of ftrace, disabling all tracing. # Written for Linux ftrace. # # This may only be of use to ftrace hackers who, in the process of developing # ftrace software, often get the subsystem into a partially active state, and # would like a quick way to reset state. Check the end of this script for the # actually files reset, and add more if you need. # # USAGE: ./reset-ftrace [-fhq] # # REQUIREMENTS: FTRACE CONFIG. # # From perf-tools: https://github.com/brendangregg/perf-tools # # See the reset-ftrace(8) man page (in perf-tools) for more info. # # COPYRIGHT: Copyright (c) 2014 Brendan Gregg. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # (http://www.gnu.org/copyleft/gpl.html) # # 20-Jul-2014 Brendan Gregg Created this. setglobal tracing = '/sys/kernel/debug/tracing' setglobal flock = '/var/tmp/.ftrace-lock' setglobal opt_force = '0'; setglobal opt_quiet = '0' proc usage { cat << """ >&2 USAGE: reset-ftrace [-fhq] -f # force: delete ftrace lock file -q # quiet: reset, but say nothing -h # this usage message eg, reset-ftrace # disable active ftrace session """ > !2 USAGE: reset-ftrace [-fhq] -f # force: delete ftrace lock file -q # quiet: reset, but say nothing -h # this usage message eg, reset-ftrace # disable active ftrace session END exit } proc warn { if ! eval @Argv { echo >&2 "WARNING: command failed \"$ifsjoin(Argv)> !2 "WARNING: command failed \"$@\"" } } proc die { echo >&2 @Argv> !2 "$@" exit 1 } proc vecho { sh-expr ' opt_quiet ' && return echo @Argv } # write to file proc writefile { setglobal file = $1 setglobal string = $2 # optional if [[ ! -w $file ]] { echo >&2 "WARNING: file $file not writable/exists. Skipping.> !2 "WARNING: file $file not writable/exists. Skipping." return } vecho "$file, before:" sh-expr ' ! opt_quiet ' && cat -n $file warn "echo $string > $file" vecho "$file, after:" sh-expr ' ! opt_quiet ' && cat -n $file vecho } ### process options while getopts fhq opt { match $opt { with f setglobal opt_force = '1' with q setglobal opt_quiet = '1' with h|? usage } } shift $shExpr(' $OPTIND - 1 ') ### ftrace lock if [[ -e $flock ]] { if sh-expr ' opt_force ' { warn rm $flock } else { echo -e >&2 "ERROR: ftrace lock ($flock) exists. It shows" \ "ftrace may be in use by PID $[cat $flock].\nDouble check" \ "to see if that PID is still active. If not, consider" \ "using -f to force a reset. Exiting.> !2 "ERROR: ftrace lock ($flock) exists. It shows" \ "ftrace may be in use by PID $(cat $flock).\nDouble check" \ "to see if that PID is still active. If not, consider" \ "using -f to force a reset. Exiting." exit 1 } } ### reset ftrace state vecho "Reseting ftrace state..." vecho cd $tracing || die "ERROR: accessing tracing. Root user? Kernel has FTRACE?" writefile current_tracer nop writefile set_ftrace_filter writefile set_graph_function writefile set_ftrace_pid writefile events/enable 0 writefile tracing_thresh 0 writefile kprobe_events writefile tracing_on 1 vecho "Done."