xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/ftrace/test.d/functions (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyunclear_trace() { # reset trace output
2*4882a593Smuzhiyun    echo > trace
3*4882a593Smuzhiyun}
4*4882a593Smuzhiyun
5*4882a593Smuzhiyundisable_tracing() { # stop trace recording
6*4882a593Smuzhiyun    echo 0 > tracing_on
7*4882a593Smuzhiyun}
8*4882a593Smuzhiyun
9*4882a593Smuzhiyunenable_tracing() { # start trace recording
10*4882a593Smuzhiyun    echo 1 > tracing_on
11*4882a593Smuzhiyun}
12*4882a593Smuzhiyun
13*4882a593Smuzhiyunreset_tracer() { # reset the current tracer
14*4882a593Smuzhiyun    echo nop > current_tracer
15*4882a593Smuzhiyun}
16*4882a593Smuzhiyun
17*4882a593Smuzhiyunreset_trigger_file() {
18*4882a593Smuzhiyun    # remove action triggers first
19*4882a593Smuzhiyun    grep -H ':on[^:]*(' $@ |
20*4882a593Smuzhiyun    while read line; do
21*4882a593Smuzhiyun        cmd=`echo $line | cut -f2- -d: | cut -f1 -d"["`
22*4882a593Smuzhiyun	file=`echo $line | cut -f1 -d:`
23*4882a593Smuzhiyun	echo "!$cmd" >> $file
24*4882a593Smuzhiyun    done
25*4882a593Smuzhiyun    grep -Hv ^# $@ |
26*4882a593Smuzhiyun    while read line; do
27*4882a593Smuzhiyun        cmd=`echo $line | cut -f2- -d: | cut -f1 -d"["`
28*4882a593Smuzhiyun	file=`echo $line | cut -f1 -d:`
29*4882a593Smuzhiyun	echo "!$cmd" > $file
30*4882a593Smuzhiyun    done
31*4882a593Smuzhiyun}
32*4882a593Smuzhiyun
33*4882a593Smuzhiyunreset_trigger() { # reset all current setting triggers
34*4882a593Smuzhiyun    if [ -d events/synthetic ]; then
35*4882a593Smuzhiyun        reset_trigger_file events/synthetic/*/trigger
36*4882a593Smuzhiyun    fi
37*4882a593Smuzhiyun    reset_trigger_file events/*/*/trigger
38*4882a593Smuzhiyun}
39*4882a593Smuzhiyun
40*4882a593Smuzhiyunreset_events_filter() { # reset all current setting filters
41*4882a593Smuzhiyun    grep -v ^none events/*/*/filter |
42*4882a593Smuzhiyun    while read line; do
43*4882a593Smuzhiyun	echo 0 > `echo $line | cut -f1 -d:`
44*4882a593Smuzhiyun    done
45*4882a593Smuzhiyun}
46*4882a593Smuzhiyun
47*4882a593Smuzhiyunreset_ftrace_filter() { # reset all triggers in set_ftrace_filter
48*4882a593Smuzhiyun    if [ ! -f set_ftrace_filter ]; then
49*4882a593Smuzhiyun      return 0
50*4882a593Smuzhiyun    fi
51*4882a593Smuzhiyun    echo > set_ftrace_filter
52*4882a593Smuzhiyun    grep -v '^#' set_ftrace_filter | while read t; do
53*4882a593Smuzhiyun	tr=`echo $t | cut -d: -f2`
54*4882a593Smuzhiyun	if [ "$tr" = "" ]; then
55*4882a593Smuzhiyun	    continue
56*4882a593Smuzhiyun	fi
57*4882a593Smuzhiyun	if ! grep -q "$t" set_ftrace_filter; then
58*4882a593Smuzhiyun		continue;
59*4882a593Smuzhiyun	fi
60*4882a593Smuzhiyun	name=`echo $t | cut -d: -f1 | cut -d' ' -f1`
61*4882a593Smuzhiyun	if [ $tr = "enable_event" -o $tr = "disable_event" ]; then
62*4882a593Smuzhiyun	    tr=`echo $t | cut -d: -f2-4`
63*4882a593Smuzhiyun	    limit=`echo $t | cut -d: -f5`
64*4882a593Smuzhiyun	else
65*4882a593Smuzhiyun	    tr=`echo $t | cut -d: -f2`
66*4882a593Smuzhiyun	    limit=`echo $t | cut -d: -f3`
67*4882a593Smuzhiyun	fi
68*4882a593Smuzhiyun	if [ "$limit" != "unlimited" ]; then
69*4882a593Smuzhiyun	    tr="$tr:$limit"
70*4882a593Smuzhiyun	fi
71*4882a593Smuzhiyun	echo "!$name:$tr" > set_ftrace_filter
72*4882a593Smuzhiyun    done
73*4882a593Smuzhiyun}
74*4882a593Smuzhiyun
75*4882a593Smuzhiyundisable_events() {
76*4882a593Smuzhiyun    echo 0 > events/enable
77*4882a593Smuzhiyun}
78*4882a593Smuzhiyun
79*4882a593Smuzhiyunclear_synthetic_events() { # reset all current synthetic events
80*4882a593Smuzhiyun    grep -v ^# synthetic_events |
81*4882a593Smuzhiyun    while read line; do
82*4882a593Smuzhiyun        echo "!$line" >> synthetic_events
83*4882a593Smuzhiyun    done
84*4882a593Smuzhiyun}
85*4882a593Smuzhiyun
86*4882a593Smuzhiyuninitialize_ftrace() { # Reset ftrace to initial-state
87*4882a593Smuzhiyun# As the initial state, ftrace will be set to nop tracer,
88*4882a593Smuzhiyun# no events, no triggers, no filters, no function filters,
89*4882a593Smuzhiyun# no probes, and tracing on.
90*4882a593Smuzhiyun    disable_tracing
91*4882a593Smuzhiyun    reset_tracer
92*4882a593Smuzhiyun    reset_trigger
93*4882a593Smuzhiyun    reset_events_filter
94*4882a593Smuzhiyun    reset_ftrace_filter
95*4882a593Smuzhiyun    disable_events
96*4882a593Smuzhiyun    [ -f set_event_pid ] && echo > set_event_pid
97*4882a593Smuzhiyun    [ -f set_ftrace_pid ] && echo > set_ftrace_pid
98*4882a593Smuzhiyun    [ -f set_ftrace_notrace ] && echo > set_ftrace_notrace
99*4882a593Smuzhiyun    [ -f set_graph_function ] && echo | tee set_graph_*
100*4882a593Smuzhiyun    [ -f stack_trace_filter ] && echo > stack_trace_filter
101*4882a593Smuzhiyun    [ -f kprobe_events ] && echo > kprobe_events
102*4882a593Smuzhiyun    [ -f uprobe_events ] && echo > uprobe_events
103*4882a593Smuzhiyun    [ -f synthetic_events ] && echo > synthetic_events
104*4882a593Smuzhiyun    [ -f snapshot ] && echo 0 > snapshot
105*4882a593Smuzhiyun    clear_trace
106*4882a593Smuzhiyun    enable_tracing
107*4882a593Smuzhiyun}
108*4882a593Smuzhiyun
109*4882a593Smuzhiyuncheck_requires() { # Check required files and tracers
110*4882a593Smuzhiyun    for i in "$@" ; do
111*4882a593Smuzhiyun        r=${i%:README}
112*4882a593Smuzhiyun        t=${i%:tracer}
113*4882a593Smuzhiyun        if [ $t != $i ]; then
114*4882a593Smuzhiyun            if ! grep -wq $t available_tracers ; then
115*4882a593Smuzhiyun                echo "Required tracer $t is not configured."
116*4882a593Smuzhiyun                exit_unsupported
117*4882a593Smuzhiyun            fi
118*4882a593Smuzhiyun        elif [ "$r" != "$i" ]; then
119*4882a593Smuzhiyun            if ! grep -Fq "$r" README ; then
120*4882a593Smuzhiyun                echo "Required feature pattern \"$r\" is not in README."
121*4882a593Smuzhiyun                exit_unsupported
122*4882a593Smuzhiyun            fi
123*4882a593Smuzhiyun        elif [ ! -e $i ]; then
124*4882a593Smuzhiyun            echo "Required feature interface $i doesn't exist."
125*4882a593Smuzhiyun            exit_unsupported
126*4882a593Smuzhiyun        fi
127*4882a593Smuzhiyun    done
128*4882a593Smuzhiyun}
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunLOCALHOST=127.0.0.1
131*4882a593Smuzhiyun
132*4882a593Smuzhiyunyield() {
133*4882a593Smuzhiyun    ping $LOCALHOST -c 1 || sleep .001 || usleep 1 || sleep 1
134*4882a593Smuzhiyun}
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun# The fork function in the kernel was renamed from "_do_fork" to
137*4882a593Smuzhiyun# "kernel_fork". As older tests should still work with older kernels
138*4882a593Smuzhiyun# as well as newer kernels, check which version of fork is used on this
139*4882a593Smuzhiyun# kernel so that the tests can use the fork function for the running kernel.
140*4882a593SmuzhiyunFUNCTION_FORK=`(if grep '\bkernel_clone\b' /proc/kallsyms > /dev/null; then
141*4882a593Smuzhiyun                echo kernel_clone; else echo '_do_fork'; fi)`
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun# Since probe event command may include backslash, explicitly use printf "%s"
144*4882a593Smuzhiyun# to NOT interpret it.
145*4882a593Smuzhiyunftrace_errlog_check() { # err-prefix command-with-error-pos-by-^ command-file
146*4882a593Smuzhiyun    pos=$(printf "%s" "${2%^*}" | wc -c) # error position
147*4882a593Smuzhiyun    command=$(printf "%s" "$2" | tr -d ^)
148*4882a593Smuzhiyun    echo "Test command: $command"
149*4882a593Smuzhiyun    echo > error_log
150*4882a593Smuzhiyun    (! printf "%s" "$command" >> "$3" ) 2> /dev/null
151*4882a593Smuzhiyun    grep "$1: error:" -A 3 error_log
152*4882a593Smuzhiyun    N=$(tail -n 1 error_log | wc -c)
153*4882a593Smuzhiyun    # "  Command: " and "^\n" => 13
154*4882a593Smuzhiyun    test $(expr 13 + $pos) -eq $N
155*4882a593Smuzhiyun}
156