1951dbf50SSteven Rostedt (Red Hat)#!/bin/bash 2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 3951dbf50SSteven Rostedt (Red Hat)# 4951dbf50SSteven Rostedt (Red Hat)# Here's how to use this: 5951dbf50SSteven Rostedt (Red Hat)# 6951dbf50SSteven Rostedt (Red Hat)# This script is used to help find functions that are being traced by function 7951dbf50SSteven Rostedt (Red Hat)# tracer or function graph tracing that causes the machine to reboot, hang, or 8951dbf50SSteven Rostedt (Red Hat)# crash. Here's the steps to take. 9951dbf50SSteven Rostedt (Red Hat)# 10951dbf50SSteven Rostedt (Red Hat)# First, determine if function tracing is working with a single function: 11951dbf50SSteven Rostedt (Red Hat)# 12951dbf50SSteven Rostedt (Red Hat)# (note, if this is a problem with function_graph tracing, then simply 13951dbf50SSteven Rostedt (Red Hat)# replace "function" with "function_graph" in the following steps). 14951dbf50SSteven Rostedt (Red Hat)# 15*7ae4ba71SSteven Rostedt (Google)# # cd /sys/kernel/tracing 16951dbf50SSteven Rostedt (Red Hat)# # echo schedule > set_ftrace_filter 17951dbf50SSteven Rostedt (Red Hat)# # echo function > current_tracer 18951dbf50SSteven Rostedt (Red Hat)# 19951dbf50SSteven Rostedt (Red Hat)# If this works, then we know that something is being traced that shouldn't be. 20951dbf50SSteven Rostedt (Red Hat)# 21951dbf50SSteven Rostedt (Red Hat)# # echo nop > current_tracer 22951dbf50SSteven Rostedt (Red Hat)# 23*7ae4ba71SSteven Rostedt (Google)# Starting with v5.1 this can be done with numbers, making it much faster: 24*7ae4ba71SSteven Rostedt (Google)# 25*7ae4ba71SSteven Rostedt (Google)# The old (slow) way, for kernels before v5.1. 26*7ae4ba71SSteven Rostedt (Google)# 27*7ae4ba71SSteven Rostedt (Google)# [old-way] # cat available_filter_functions > ~/full-file 28*7ae4ba71SSteven Rostedt (Google)# 29*7ae4ba71SSteven Rostedt (Google)# [old-way] *** Note *** this process will take several minutes to update the 30*7ae4ba71SSteven Rostedt (Google)# [old-way] filters. Setting multiple functions is an O(n^2) operation, and we 31*7ae4ba71SSteven Rostedt (Google)# [old-way] are dealing with thousands of functions. So go have coffee, talk 32*7ae4ba71SSteven Rostedt (Google)# [old-way] with your coworkers, read facebook. And eventually, this operation 33*7ae4ba71SSteven Rostedt (Google)# [old-way] will end. 34*7ae4ba71SSteven Rostedt (Google)# 35*7ae4ba71SSteven Rostedt (Google)# The new way (using numbers) is an O(n) operation, and usually takes less than a second. 36*7ae4ba71SSteven Rostedt (Google)# 37*7ae4ba71SSteven Rostedt (Google)# seq `wc -l available_filter_functions | cut -d' ' -f1` > ~/full-file 38*7ae4ba71SSteven Rostedt (Google)# 39*7ae4ba71SSteven Rostedt (Google)# This will create a sequence of numbers that match the functions in 40*7ae4ba71SSteven Rostedt (Google)# available_filter_functions, and when echoing in a number into the 41*7ae4ba71SSteven Rostedt (Google)# set_ftrace_filter file, it will enable the corresponding function in 42*7ae4ba71SSteven Rostedt (Google)# O(1) time. Making enabling all functions O(n) where n is the number of 43*7ae4ba71SSteven Rostedt (Google)# functions to enable. 44*7ae4ba71SSteven Rostedt (Google)# 45*7ae4ba71SSteven Rostedt (Google)# For either the new or old way, the rest of the operations remain the same. 46*7ae4ba71SSteven Rostedt (Google)# 47951dbf50SSteven Rostedt (Red Hat)# # ftrace-bisect ~/full-file ~/test-file ~/non-test-file 48951dbf50SSteven Rostedt (Red Hat)# # cat ~/test-file > set_ftrace_filter 49951dbf50SSteven Rostedt (Red Hat)# 50951dbf50SSteven Rostedt (Red Hat)# # echo function > current_tracer 51951dbf50SSteven Rostedt (Red Hat)# 52951dbf50SSteven Rostedt (Red Hat)# If it crashes, we know that ~/test-file has a bad function. 53951dbf50SSteven Rostedt (Red Hat)# 54951dbf50SSteven Rostedt (Red Hat)# Reboot back to test kernel. 55951dbf50SSteven Rostedt (Red Hat)# 56*7ae4ba71SSteven Rostedt (Google)# # cd /sys/kernel/tracing 57951dbf50SSteven Rostedt (Red Hat)# # mv ~/test-file ~/full-file 58951dbf50SSteven Rostedt (Red Hat)# 59951dbf50SSteven Rostedt (Red Hat)# If it didn't crash. 60951dbf50SSteven Rostedt (Red Hat)# 61951dbf50SSteven Rostedt (Red Hat)# # echo nop > current_tracer 62951dbf50SSteven Rostedt (Red Hat)# # mv ~/non-test-file ~/full-file 63951dbf50SSteven Rostedt (Red Hat)# 64951dbf50SSteven Rostedt (Red Hat)# Get rid of the other test file from previous run (or save them off somewhere). 65951dbf50SSteven Rostedt (Red Hat)# # rm -f ~/test-file ~/non-test-file 66951dbf50SSteven Rostedt (Red Hat)# 67951dbf50SSteven Rostedt (Red Hat)# And start again: 68951dbf50SSteven Rostedt (Red Hat)# 69951dbf50SSteven Rostedt (Red Hat)# # ftrace-bisect ~/full-file ~/test-file ~/non-test-file 70951dbf50SSteven Rostedt (Red Hat)# 71951dbf50SSteven Rostedt (Red Hat)# The good thing is, because this cuts the number of functions in ~/test-file 72951dbf50SSteven Rostedt (Red Hat)# by half, the cat of it into set_ftrace_filter takes half as long each 73951dbf50SSteven Rostedt (Red Hat)# iteration, so don't talk so much at the water cooler the second time. 74951dbf50SSteven Rostedt (Red Hat)# 75951dbf50SSteven Rostedt (Red Hat)# Eventually, if you did this correctly, you will get down to the problem 76951dbf50SSteven Rostedt (Red Hat)# function, and all we need to do is to notrace it. 77951dbf50SSteven Rostedt (Red Hat)# 78951dbf50SSteven Rostedt (Red Hat)# The way to figure out if the problem function is bad, just do: 79951dbf50SSteven Rostedt (Red Hat)# 80951dbf50SSteven Rostedt (Red Hat)# # echo <problem-function> > set_ftrace_notrace 81951dbf50SSteven Rostedt (Red Hat)# # echo > set_ftrace_filter 82951dbf50SSteven Rostedt (Red Hat)# # echo function > current_tracer 83951dbf50SSteven Rostedt (Red Hat)# 84951dbf50SSteven Rostedt (Red Hat)# And if it doesn't crash, we are done. 85951dbf50SSteven Rostedt (Red Hat)# 86951dbf50SSteven Rostedt (Red Hat)# If it does crash, do this again (there's more than one problem function) 87951dbf50SSteven Rostedt (Red Hat)# but you need to echo the problem function(s) into set_ftrace_notrace before 88951dbf50SSteven Rostedt (Red Hat)# enabling function tracing in the above steps. Or if you can compile the 89951dbf50SSteven Rostedt (Red Hat)# kernel, annotate the problem functions with "notrace" and start again. 90951dbf50SSteven Rostedt (Red Hat)# 91951dbf50SSteven Rostedt (Red Hat) 92951dbf50SSteven Rostedt (Red Hat) 93951dbf50SSteven Rostedt (Red Hat)if [ $# -ne 3 ]; then 94951dbf50SSteven Rostedt (Red Hat) echo 'usage: ftrace-bisect full-file test-file non-test-file' 95951dbf50SSteven Rostedt (Red Hat) exit 96951dbf50SSteven Rostedt (Red Hat)fi 97951dbf50SSteven Rostedt (Red Hat) 98951dbf50SSteven Rostedt (Red Hat)full=$1 99951dbf50SSteven Rostedt (Red Hat)test=$2 100951dbf50SSteven Rostedt (Red Hat)nontest=$3 101951dbf50SSteven Rostedt (Red Hat) 102951dbf50SSteven Rostedt (Red Hat)x=`cat $full | wc -l` 103951dbf50SSteven Rostedt (Red Hat)if [ $x -eq 1 ]; then 104951dbf50SSteven Rostedt (Red Hat) echo "There's only one function left, must be the bad one" 105951dbf50SSteven Rostedt (Red Hat) cat $full 106951dbf50SSteven Rostedt (Red Hat) exit 0 107951dbf50SSteven Rostedt (Red Hat)fi 108951dbf50SSteven Rostedt (Red Hat) 109951dbf50SSteven Rostedt (Red Hat)let x=$x/2 110951dbf50SSteven Rostedt (Red Hat)let y=$x+1 111951dbf50SSteven Rostedt (Red Hat) 112951dbf50SSteven Rostedt (Red Hat)if [ ! -f $full ]; then 113951dbf50SSteven Rostedt (Red Hat) echo "$full does not exist" 114951dbf50SSteven Rostedt (Red Hat) exit 1 115951dbf50SSteven Rostedt (Red Hat)fi 116951dbf50SSteven Rostedt (Red Hat) 117951dbf50SSteven Rostedt (Red Hat)if [ -f $test ]; then 118951dbf50SSteven Rostedt (Red Hat) echo -n "$test exists, delete it? [y/N]" 119951dbf50SSteven Rostedt (Red Hat) read a 120951dbf50SSteven Rostedt (Red Hat) if [ "$a" != "y" -a "$a" != "Y" ]; then 121951dbf50SSteven Rostedt (Red Hat) exit 1 122951dbf50SSteven Rostedt (Red Hat) fi 123951dbf50SSteven Rostedt (Red Hat)fi 124951dbf50SSteven Rostedt (Red Hat) 125951dbf50SSteven Rostedt (Red Hat)if [ -f $nontest ]; then 126951dbf50SSteven Rostedt (Red Hat) echo -n "$nontest exists, delete it? [y/N]" 127951dbf50SSteven Rostedt (Red Hat) read a 128951dbf50SSteven Rostedt (Red Hat) if [ "$a" != "y" -a "$a" != "Y" ]; then 129951dbf50SSteven Rostedt (Red Hat) exit 1 130951dbf50SSteven Rostedt (Red Hat) fi 131951dbf50SSteven Rostedt (Red Hat)fi 132951dbf50SSteven Rostedt (Red Hat) 133951dbf50SSteven Rostedt (Red Hat)sed -ne "1,${x}p" $full > $test 134951dbf50SSteven Rostedt (Red Hat)sed -ne "$y,\$p" $full > $nontest 135