^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/bin/bash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # Here's how to use this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # This script is used to help find functions that are being traced by function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) # tracer or function graph tracing that causes the machine to reboot, hang, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # crash. Here's the steps to take.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) # First, determine if function tracing is working with a single function:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # (note, if this is a problem with function_graph tracing, then simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) # replace "function" with "function_graph" in the following steps).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) # # cd /sys/kernel/debug/tracing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) # # echo schedule > set_ftrace_filter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) # # echo function > current_tracer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) # If this works, then we know that something is being traced that shouldn't be.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) # # echo nop > current_tracer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) # # cat available_filter_functions > ~/full-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) # # ftrace-bisect ~/full-file ~/test-file ~/non-test-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) # # cat ~/test-file > set_ftrace_filter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) # *** Note *** this will take several minutes. Setting multiple functions is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) # an O(n^2) operation, and we are dealing with thousands of functions. So go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) # have coffee, talk with your coworkers, read facebook. And eventually, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) # operation will end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) # # echo function > current_tracer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) # If it crashes, we know that ~/test-file has a bad function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) # Reboot back to test kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) # # cd /sys/kernel/debug/tracing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) # # mv ~/test-file ~/full-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) # If it didn't crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) # # echo nop > current_tracer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) # # mv ~/non-test-file ~/full-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) # Get rid of the other test file from previous run (or save them off somewhere).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) # # rm -f ~/test-file ~/non-test-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) # And start again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) # # ftrace-bisect ~/full-file ~/test-file ~/non-test-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) # The good thing is, because this cuts the number of functions in ~/test-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) # by half, the cat of it into set_ftrace_filter takes half as long each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) # iteration, so don't talk so much at the water cooler the second time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) # Eventually, if you did this correctly, you will get down to the problem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) # function, and all we need to do is to notrace it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) # The way to figure out if the problem function is bad, just do:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) # # echo <problem-function> > set_ftrace_notrace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) # # echo > set_ftrace_filter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) # # echo function > current_tracer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) # And if it doesn't crash, we are done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) # If it does crash, do this again (there's more than one problem function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) # but you need to echo the problem function(s) into set_ftrace_notrace before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) # enabling function tracing in the above steps. Or if you can compile the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) # kernel, annotate the problem functions with "notrace" and start again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if [ $# -ne 3 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) echo 'usage: ftrace-bisect full-file test-file non-test-file'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) full=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) test=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) nontest=$3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) x=`cat $full | wc -l`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if [ $x -eq 1 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) echo "There's only one function left, must be the bad one"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) cat $full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) exit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) let x=$x/2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) let y=$x+1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if [ ! -f $full ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) echo "$full does not exist"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if [ -f $test ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) echo -n "$test exists, delete it? [y/N]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) read a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if [ "$a" != "y" -a "$a" != "Y" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if [ -f $nontest ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) echo -n "$nontest exists, delete it? [y/N]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) read a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if [ "$a" != "y" -a "$a" != "Y" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) sed -ne "1,${x}p" $full > $test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) sed -ne "$y,\$p" $full > $nontest