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