1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3# description: ftrace - function pid notrace filters
4# requires: set_ftrace_notrace_pid set_ftrace_filter function:tracer
5# flags: instance
6
7# Make sure that function pid matching filter with notrace works.
8
9do_function_fork=1
10
11if [ ! -f options/function-fork ]; then
12    do_function_fork=0
13    echo "no option for function-fork found. Option will not be tested."
14fi
15
16read PID _ < /proc/self/stat
17
18if [ $do_function_fork -eq 1 ]; then
19    # default value of function-fork option
20    orig_value=`grep function-fork trace_options`
21fi
22
23do_reset() {
24    if [ $do_function_fork -eq 0 ]; then
25	return
26    fi
27
28    echo > set_ftrace_notrace_pid
29    echo $orig_value > trace_options
30}
31
32fail() { # msg
33    do_reset
34    echo $1
35    exit_fail
36}
37
38do_test() {
39    disable_tracing
40
41    echo do_execve* > set_ftrace_filter
42    echo $FUNCTION_FORK >> set_ftrace_filter
43
44    echo $PID > set_ftrace_notrace_pid
45    echo function > current_tracer
46
47    if [ $do_function_fork -eq 1 ]; then
48	# don't allow children to be traced
49	echo nofunction-fork > trace_options
50    fi
51
52    enable_tracing
53    yield
54
55    count_pid=`cat trace | grep -v ^# | grep $PID | wc -l`
56    count_other=`cat trace | grep -v ^# | grep -v $PID | wc -l`
57
58    # count_pid should be 0
59    if [ $count_pid -ne 0 -o $count_other -eq 0 ]; then
60	fail "PID filtering not working? traced task = $count_pid; other tasks = $count_other "
61    fi
62
63    disable_tracing
64    clear_trace
65
66    if [ $do_function_fork -eq 0 ]; then
67	return
68    fi
69
70    # allow children to be traced
71    echo function-fork > trace_options
72
73    # With pid in both set_ftrace_pid and set_ftrace_notrace_pid
74    # there should not be any tasks traced.
75
76    echo $PID > set_ftrace_pid
77
78    enable_tracing
79    yield
80
81    count_pid=`cat trace | grep -v ^# | grep $PID | wc -l`
82    count_other=`cat trace | grep -v ^# | grep -v $PID | wc -l`
83
84    # both should be zero
85    if [ $count_pid -ne 0 -o $count_other -ne 0 ]; then
86	fail "PID filtering not following fork? traced task = $count_pid; other tasks = $count_other "
87    fi
88}
89
90do_test
91
92do_reset
93
94exit 0
95