functions 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. clear_trace() { # reset trace output
  2. echo > trace
  3. }
  4. disable_tracing() { # stop trace recording
  5. echo 0 > tracing_on
  6. }
  7. enable_tracing() { # start trace recording
  8. echo 1 > tracing_on
  9. }
  10. reset_tracer() { # reset the current tracer
  11. echo nop > current_tracer
  12. }
  13. reset_trigger_file() {
  14. # remove action triggers first
  15. grep -H ':on[^:]*(' $@ |
  16. while read line; do
  17. cmd=`echo $line | cut -f2- -d: | cut -f1 -d"["`
  18. file=`echo $line | cut -f1 -d:`
  19. echo "!$cmd" >> $file
  20. done
  21. grep -Hv ^# $@ |
  22. while read line; do
  23. cmd=`echo $line | cut -f2- -d: | cut -f1 -d"["`
  24. file=`echo $line | cut -f1 -d:`
  25. echo "!$cmd" > $file
  26. done
  27. }
  28. reset_trigger() { # reset all current setting triggers
  29. if [ -d events/synthetic ]; then
  30. reset_trigger_file events/synthetic/*/trigger
  31. fi
  32. reset_trigger_file events/*/*/trigger
  33. }
  34. reset_events_filter() { # reset all current setting filters
  35. grep -v ^none events/*/*/filter |
  36. while read line; do
  37. echo 0 > `echo $line | cut -f1 -d:`
  38. done
  39. }
  40. reset_ftrace_filter() { # reset all triggers in set_ftrace_filter
  41. if [ ! -f set_ftrace_filter ]; then
  42. return 0
  43. fi
  44. echo > set_ftrace_filter
  45. grep -v '^#' set_ftrace_filter | while read t; do
  46. tr=`echo $t | cut -d: -f2`
  47. if [ "$tr" = "" ]; then
  48. continue
  49. fi
  50. if ! grep -q "$t" set_ftrace_filter; then
  51. continue;
  52. fi
  53. name=`echo $t | cut -d: -f1 | cut -d' ' -f1`
  54. if [ $tr = "enable_event" -o $tr = "disable_event" ]; then
  55. tr=`echo $t | cut -d: -f2-4`
  56. limit=`echo $t | cut -d: -f5`
  57. else
  58. tr=`echo $t | cut -d: -f2`
  59. limit=`echo $t | cut -d: -f3`
  60. fi
  61. if [ "$limit" != "unlimited" ]; then
  62. tr="$tr:$limit"
  63. fi
  64. echo "!$name:$tr" > set_ftrace_filter
  65. done
  66. }
  67. disable_events() {
  68. echo 0 > events/enable
  69. }
  70. clear_synthetic_events() { # reset all current synthetic events
  71. grep -v ^# synthetic_events |
  72. while read line; do
  73. echo "!$line" >> synthetic_events
  74. done
  75. }
  76. clear_dynamic_events() { # reset all current dynamic events
  77. again=1
  78. stop=1
  79. # loop mulitple times as some events require other to be removed first
  80. while [ $again -eq 1 ]; do
  81. stop=$((stop+1))
  82. # Prevent infinite loops
  83. if [ $stop -gt 10 ]; then
  84. break;
  85. fi
  86. again=2
  87. grep -v '^#' dynamic_events|
  88. while read line; do
  89. del=`echo $line | sed -e 's/^.\([^ ]*\).*/-\1/'`
  90. if ! echo "$del" >> dynamic_events; then
  91. again=1
  92. fi
  93. done
  94. done
  95. }
  96. initialize_system() { # Reset ftrace to initial-state
  97. # As the initial state, ftrace will be set to nop tracer,
  98. # no events, no triggers, no filters, no function filters,
  99. # no probes, and tracing on.
  100. disable_tracing
  101. reset_tracer
  102. reset_trigger
  103. reset_events_filter
  104. reset_ftrace_filter
  105. disable_events
  106. clear_dynamic_events
  107. [ -f set_event_pid ] && echo > set_event_pid
  108. [ -f set_ftrace_pid ] && echo > set_ftrace_pid
  109. [ -f set_ftrace_notrace ] && echo > set_ftrace_notrace
  110. [ -f set_graph_function ] && echo | tee set_graph_*
  111. [ -f stack_trace_filter ] && echo > stack_trace_filter
  112. [ -f kprobe_events ] && echo > kprobe_events
  113. [ -f uprobe_events ] && echo > uprobe_events
  114. [ -f synthetic_events ] && echo > synthetic_events
  115. [ -f snapshot ] && echo 0 > snapshot
  116. # Stop tracing while reading the trace file by default, to prevent
  117. # the test results while checking it and to avoid taking a long time
  118. # to check the result.
  119. [ -f options/pause-on-trace ] && echo 1 > options/pause-on-trace
  120. clear_trace
  121. enable_tracing
  122. }
  123. finish_system() {
  124. initialize_system
  125. # And recover it to default.
  126. [ -f options/pause-on-trace ] && echo 0 > options/pause-on-trace
  127. }
  128. check_requires() { # Check required files and tracers
  129. for i in "$@" ; do
  130. p=${i%:program}
  131. r=${i%:README}
  132. t=${i%:tracer}
  133. if [ $p != $i ]; then
  134. if ! which $p ; then
  135. echo "Required program $p is not found."
  136. exit_unresolved
  137. fi
  138. elif [ $t != $i ]; then
  139. if ! grep -wq $t available_tracers ; then
  140. echo "Required tracer $t is not configured."
  141. exit_unsupported
  142. fi
  143. elif [ "$r" != "$i" ]; then
  144. # If this is an instance, check the top directory
  145. if echo $TRACING_DIR | grep -q "/instances/"; then
  146. test="$TRACING_DIR/../.."
  147. else
  148. test=$TRACING_DIR
  149. fi
  150. if ! grep -Fq "$r" $test/README ; then
  151. echo "Required feature pattern \"$r\" is not in README."
  152. exit_unsupported
  153. fi
  154. elif [ ! -e $i ]; then
  155. echo "Required feature interface $i doesn't exist."
  156. exit_unsupported
  157. fi
  158. done
  159. }
  160. LOCALHOST=127.0.0.1
  161. yield() {
  162. ping $LOCALHOST -c 1 || sleep .001 || usleep 1 || sleep 1
  163. }
  164. # The fork function in the kernel was renamed from "_do_fork" to
  165. # "kernel_fork". As older tests should still work with older kernels
  166. # as well as newer kernels, check which version of fork is used on this
  167. # kernel so that the tests can use the fork function for the running kernel.
  168. FUNCTION_FORK=`(if grep '\bkernel_clone\b' /proc/kallsyms > /dev/null; then
  169. echo kernel_clone; else echo '_do_fork'; fi)`
  170. # Since probe event command may include backslash, explicitly use printf "%s"
  171. # to NOT interpret it.
  172. ftrace_errlog_check() { # err-prefix command-with-error-pos-by-^ command-file
  173. pos=$(printf "%s" "${2%^*}" | wc -c) # error position
  174. command=$(printf "%s" "$2" | tr -d ^)
  175. echo "Test command: $command"
  176. echo > error_log
  177. (! printf "%s" "$command" >> "$3" ) 2> /dev/null
  178. grep "$1: error:" -A 3 error_log
  179. N=$(tail -n 1 error_log | wc -c)
  180. # " Command: " and "^\n" => 13
  181. test $(expr 13 + $pos) -eq $N
  182. }
  183. # Helper to get the tracefs mount point
  184. get_mount_point() {
  185. local mount_point=`stat -c '%m' .`
  186. # If stat -c '%m' does not work (e.g. busybox) or failed, try to use the
  187. # current working directory (which should be a tracefs) as the mount point.
  188. if [ ! -d "$mount_point" ]; then
  189. if mount | grep -qw "$PWD"; then
  190. mount_point=$PWD
  191. else
  192. # If PWD doesn't work, that is an environmental problem.
  193. exit_unresolved
  194. fi
  195. fi
  196. echo "$mount_point"
  197. }
  198. # Helper function to retrieve mount options for a given mount point
  199. get_mnt_options() {
  200. local mnt_point="$1"
  201. local opts=$(mount | grep -m1 "$mnt_point" | sed -e 's/.*(\(.*\)).*/\1/')
  202. echo "$opts"
  203. }