1check_filter_file() { # check filter file introduced by dynamic ftrace
2    if [ ! -f "$1" ]; then
3        echo "$1 not found? Is dynamic ftrace not set?"
4        exit_unsupported
5    fi
6}
7
8clear_trace() { # reset trace output
9    echo > trace
10}
11
12disable_tracing() { # stop trace recording
13    echo 0 > tracing_on
14}
15
16enable_tracing() { # start trace recording
17    echo 1 > tracing_on
18}
19
20reset_tracer() { # reset the current tracer
21    echo nop > current_tracer
22}
23
24reset_trigger_file() {
25    # remove action triggers first
26    grep -H ':on[^:]*(' $@ |
27    while read line; do
28        cmd=`echo $line | cut -f2- -d: | cut -f1 -d"["`
29	file=`echo $line | cut -f1 -d:`
30	echo "!$cmd" >> $file
31    done
32    grep -Hv ^# $@ |
33    while read line; do
34        cmd=`echo $line | cut -f2- -d: | cut -f1 -d"["`
35	file=`echo $line | cut -f1 -d:`
36	echo "!$cmd" > $file
37    done
38}
39
40reset_trigger() { # reset all current setting triggers
41    if [ -d events/synthetic ]; then
42        reset_trigger_file events/synthetic/*/trigger
43    fi
44    reset_trigger_file events/*/*/trigger
45}
46
47reset_events_filter() { # reset all current setting filters
48    grep -v ^none events/*/*/filter |
49    while read line; do
50	echo 0 > `echo $line | cut -f1 -d:`
51    done
52}
53
54reset_ftrace_filter() { # reset all triggers in set_ftrace_filter
55    if [ ! -f set_ftrace_filter ]; then
56      return 0
57    fi
58    echo > set_ftrace_filter
59    grep -v '^#' set_ftrace_filter | while read t; do
60	tr=`echo $t | cut -d: -f2`
61	if [ "$tr" = "" ]; then
62	    continue
63	fi
64	if ! grep -q "$t" set_ftrace_filter; then
65		continue;
66	fi
67	name=`echo $t | cut -d: -f1 | cut -d' ' -f1`
68	if [ $tr = "enable_event" -o $tr = "disable_event" ]; then
69	    tr=`echo $t | cut -d: -f2-4`
70	    limit=`echo $t | cut -d: -f5`
71	else
72	    tr=`echo $t | cut -d: -f2`
73	    limit=`echo $t | cut -d: -f3`
74	fi
75	if [ "$limit" != "unlimited" ]; then
76	    tr="$tr:$limit"
77	fi
78	echo "!$name:$tr" > set_ftrace_filter
79    done
80}
81
82disable_events() {
83    echo 0 > events/enable
84}
85
86clear_synthetic_events() { # reset all current synthetic events
87    grep -v ^# synthetic_events |
88    while read line; do
89        echo "!$line" >> synthetic_events
90    done
91}
92
93initialize_ftrace() { # Reset ftrace to initial-state
94# As the initial state, ftrace will be set to nop tracer,
95# no events, no triggers, no filters, no function filters,
96# no probes, and tracing on.
97    disable_tracing
98    reset_tracer
99    reset_trigger
100    reset_events_filter
101    reset_ftrace_filter
102    disable_events
103    [ -f set_event_pid ] && echo > set_event_pid
104    [ -f set_ftrace_pid ] && echo > set_ftrace_pid
105    [ -f set_ftrace_notrace ] && echo > set_ftrace_notrace
106    [ -f set_graph_function ] && echo | tee set_graph_*
107    [ -f stack_trace_filter ] && echo > stack_trace_filter
108    [ -f kprobe_events ] && echo > kprobe_events
109    [ -f uprobe_events ] && echo > uprobe_events
110    [ -f synthetic_events ] && echo > synthetic_events
111    [ -f snapshot ] && echo 0 > snapshot
112    clear_trace
113    enable_tracing
114}
115
116LOCALHOST=127.0.0.1
117
118yield() {
119    ping $LOCALHOST -c 1 || sleep .001 || usleep 1 || sleep 1
120}
121
122ftrace_errlog_check() { # err-prefix command-with-error-pos-by-^ command-file
123    pos=$(echo -n "${2%^*}" | wc -c) # error position
124    command=$(echo "$2" | tr -d ^)
125    echo "Test command: $command"
126    echo > error_log
127    (! echo "$command" >> "$3" ) 2> /dev/null
128    grep "$1: error:" -A 3 error_log
129    N=$(tail -n 1 error_log | wc -c)
130    # "  Command: " and "^\n" => 13
131    test $(expr 13 + $pos) -eq $N
132}
133