1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3# description: ftrace - function pid filters
4# requires: set_ftrace_pid set_ftrace_filter function:tracer
5# flags: instance
6
7# Make sure that function pid matching filter works.
8# Also test it on an instance directory
9
10do_function_fork=1
11
12if [ ! -f options/function-fork ]; then
13    do_function_fork=0
14    echo "no option for function-fork found. Option will not be tested."
15fi
16
17read PID _ < /proc/self/stat
18
19if [ $do_function_fork -eq 1 ]; then
20    # default value of function-fork option
21    orig_value=`grep function-fork trace_options`
22fi
23
24do_reset() {
25    if [ $do_function_fork -eq 0 ]; then
26	return
27    fi
28
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_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_other should be 0
59    if [ $count_pid -eq 0 -o $count_other -ne 0 ]; then
60	fail "PID filtering not working?"
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    enable_tracing
74    yield
75
76    count_pid=`cat trace | grep -v ^# | grep $PID | wc -l`
77    count_other=`cat trace | grep -v ^# | grep -v $PID | wc -l`
78
79    # count_other should NOT be 0
80    if [ $count_pid -eq 0 -o $count_other -eq 0 ]; then
81	fail "PID filtering not following fork?"
82    fi
83}
84
85do_test
86do_reset
87
88exit 0
89