Merge remote-tracking branches 'asoc/topic/ac97', 'asoc/topic/ac97-mfd', 'asoc/topic...
[sfrench/cifs-2.6.git] / tools / testing / selftests / ftrace / test.d / ftrace / func-filter-pid.tc
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0
3 # description: ftrace - function pid filters
4
5 # Make sure that function pid matching filter works.
6 # Also test it on an instance directory
7
8 if ! grep -q function available_tracers; then
9     echo "no function tracer configured"
10     exit_unsupported
11 fi
12
13 if [ ! -f set_ftrace_pid ]; then
14     echo "set_ftrace_pid not found? Is function tracer not set?"
15     exit_unsupported
16 fi
17
18 if [ ! -f set_ftrace_filter ]; then
19     echo "set_ftrace_filter not found? Is function tracer not set?"
20     exit_unsupported
21 fi
22
23 do_function_fork=1
24
25 if [ ! -f options/function-fork ]; then
26     do_function_fork=0
27     echo "no option for function-fork found. Option will not be tested."
28 fi
29
30 read PID _ < /proc/self/stat
31
32 if [ $do_function_fork -eq 1 ]; then
33     # default value of function-fork option
34     orig_value=`grep function-fork trace_options`
35 fi
36
37 do_reset() {
38     reset_tracer
39     clear_trace
40     enable_tracing
41     echo > set_ftrace_filter
42     echo > set_ftrace_pid
43
44     if [ $do_function_fork -eq 0 ]; then
45         return
46     fi
47
48     echo $orig_value > trace_options
49 }
50
51 fail() { # msg
52     do_reset
53     echo $1
54     exit $FAIL
55 }
56
57 yield() {
58     ping localhost -c 1 || sleep .001 || usleep 1 || sleep 1
59 }
60
61 do_test() {
62     disable_tracing
63
64     echo do_execve* > set_ftrace_filter
65     echo *do_fork >> set_ftrace_filter
66
67     echo $PID > set_ftrace_pid
68     echo function > current_tracer
69
70     if [ $do_function_fork -eq 1 ]; then
71         # don't allow children to be traced
72         echo nofunction-fork > trace_options
73     fi
74
75     enable_tracing
76     yield
77
78     count_pid=`cat trace | grep -v ^# | grep $PID | wc -l`
79     count_other=`cat trace | grep -v ^# | grep -v $PID | wc -l`
80
81     # count_other should be 0
82     if [ $count_pid -eq 0 -o $count_other -ne 0 ]; then
83         fail "PID filtering not working?"
84     fi
85
86     disable_tracing
87     clear_trace
88
89     if [ $do_function_fork -eq 0 ]; then
90         return
91     fi
92
93     # allow children to be traced
94     echo function-fork > trace_options
95
96     enable_tracing
97     yield
98
99     count_pid=`cat trace | grep -v ^# | grep $PID | wc -l`
100     count_other=`cat trace | grep -v ^# | grep -v $PID | wc -l`
101
102     # count_other should NOT be 0
103     if [ $count_pid -eq 0 -o $count_other -eq 0 ]; then
104         fail "PID filtering not following fork?"
105     fi
106 }
107
108 do_test
109
110 mkdir instances/foo
111 cd instances/foo
112 do_test
113 cd ../../
114 rmdir instances/foo
115
116 do_reset
117
118 exit 0