ftrace-bisect.sh 3.1 KB

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