1# SPDX-License-Identifier: GPL-2.0-only
2
3clear_trace() { # reset trace output
4    echo > trace
5}
6
7disable_tracing() { # stop trace recording
8    echo 0 > tracing_on
9}
10
11enable_tracing() { # start trace recording
12    echo 1 > tracing_on
13}
14
15reset_tracer() { # reset the current tracer
16    echo nop > current_tracer
17}
18
19reset_trigger_file() {
20    # remove action triggers first
21    grep -H ':on[^:]*(' $@ |
22    while read line; do
23        cmd=`echo $line | cut -f2- -d: | cut -f1 -d"["`
24	file=`echo $line | cut -f1 -d:`
25	echo "!$cmd" >> $file
26    done
27    grep -Hv ^# $@ |
28    while read line; do
29        cmd=`echo $line | cut -f2- -d: | cut -f1 -d"["`
30	file=`echo $line | cut -f1 -d:`
31	echo "!$cmd" > $file
32    done
33}
34
35reset_trigger() { # reset all current setting triggers
36    if [ -d events/synthetic ]; then
37        reset_trigger_file events/synthetic/*/trigger
38    fi
39    reset_trigger_file events/*/*/trigger
40}
41
42reset_events_filter() { # reset all current setting filters
43    grep -v ^none events/*/*/filter |
44    while read line; do
45	echo 0 > `echo $line | cut -f1 -d:`
46    done
47}
48
49reset_ftrace_filter() { # reset all triggers in set_ftrace_filter
50    if [ ! -f set_ftrace_filter ]; then
51      return 0
52    fi
53    echo > set_ftrace_filter
54    grep -v '^#' set_ftrace_filter | while read t; do
55	tr=`echo $t | cut -d: -f2`
56	if [ "$tr" = "" ]; then
57	    continue
58	fi
59	if ! grep -q "$t" set_ftrace_filter; then
60		continue;
61	fi
62	name=`echo $t | cut -d: -f1 | cut -d' ' -f1`
63	if [ $tr = "enable_event" -o $tr = "disable_event" ]; then
64	    tr=`echo $t | cut -d: -f2-4`
65	    limit=`echo $t | cut -d: -f5`
66	else
67	    tr=`echo $t | cut -d: -f2`
68	    limit=`echo $t | cut -d: -f3`
69	fi
70	if [ "$limit" != "unlimited" ]; then
71	    tr="$tr:$limit"
72	fi
73	echo "!$name:$tr" > set_ftrace_filter
74    done
75}
76
77disable_events() {
78    echo 0 > events/enable
79}
80
81clear_synthetic_events() { # reset all current synthetic events
82    grep -v ^# synthetic_events |
83    while read line; do
84        echo "!$line" >> synthetic_events
85    done
86}
87
88initialize_ftrace() { # Reset ftrace to initial-state
89# As the initial state, ftrace will be set to nop tracer,
90# no events, no triggers, no filters, no function filters,
91# no probes, and tracing on.
92    disable_tracing
93    reset_tracer
94    reset_trigger
95    reset_events_filter
96    reset_ftrace_filter
97    disable_events
98    [ -f set_event_pid ] && echo > set_event_pid
99    [ -f set_ftrace_pid ] && echo > set_ftrace_pid
100    [ -f set_ftrace_notrace ] && echo > set_ftrace_notrace
101    [ -f set_graph_function ] && echo | tee set_graph_*
102    [ -f stack_trace_filter ] && echo > stack_trace_filter
103    [ -f kprobe_events ] && echo > kprobe_events
104    [ -f uprobe_events ] && echo > uprobe_events
105    [ -f synthetic_events ] && echo > synthetic_events
106    [ -f snapshot ] && echo 0 > snapshot
107    clear_trace
108    enable_tracing
109}
110