162306a36Sopenharmony_ciclear_trace() { # reset trace output
262306a36Sopenharmony_ci    echo > trace
362306a36Sopenharmony_ci}
462306a36Sopenharmony_ci
562306a36Sopenharmony_cidisable_tracing() { # stop trace recording
662306a36Sopenharmony_ci    echo 0 > tracing_on
762306a36Sopenharmony_ci}
862306a36Sopenharmony_ci
962306a36Sopenharmony_cienable_tracing() { # start trace recording
1062306a36Sopenharmony_ci    echo 1 > tracing_on
1162306a36Sopenharmony_ci}
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cireset_tracer() { # reset the current tracer
1462306a36Sopenharmony_ci    echo nop > current_tracer
1562306a36Sopenharmony_ci}
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cireset_trigger_file() {
1862306a36Sopenharmony_ci    # remove action triggers first
1962306a36Sopenharmony_ci    grep -H ':on[^:]*(' $@ |
2062306a36Sopenharmony_ci    while read line; do
2162306a36Sopenharmony_ci        cmd=`echo $line | cut -f2- -d: | cut -f1 -d"["`
2262306a36Sopenharmony_ci	file=`echo $line | cut -f1 -d:`
2362306a36Sopenharmony_ci	echo "!$cmd" >> $file
2462306a36Sopenharmony_ci    done
2562306a36Sopenharmony_ci    grep -Hv ^# $@ |
2662306a36Sopenharmony_ci    while read line; do
2762306a36Sopenharmony_ci        cmd=`echo $line | cut -f2- -d: | cut -f1 -d"["`
2862306a36Sopenharmony_ci	file=`echo $line | cut -f1 -d:`
2962306a36Sopenharmony_ci	echo "!$cmd" > $file
3062306a36Sopenharmony_ci    done
3162306a36Sopenharmony_ci}
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cireset_trigger() { # reset all current setting triggers
3462306a36Sopenharmony_ci    if [ -d events/synthetic ]; then
3562306a36Sopenharmony_ci        reset_trigger_file events/synthetic/*/trigger
3662306a36Sopenharmony_ci    fi
3762306a36Sopenharmony_ci    reset_trigger_file events/*/*/trigger
3862306a36Sopenharmony_ci}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cireset_events_filter() { # reset all current setting filters
4162306a36Sopenharmony_ci    grep -v ^none events/*/*/filter |
4262306a36Sopenharmony_ci    while read line; do
4362306a36Sopenharmony_ci	echo 0 > `echo $line | cut -f1 -d:`
4462306a36Sopenharmony_ci    done
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cireset_ftrace_filter() { # reset all triggers in set_ftrace_filter
4862306a36Sopenharmony_ci    if [ ! -f set_ftrace_filter ]; then
4962306a36Sopenharmony_ci      return 0
5062306a36Sopenharmony_ci    fi
5162306a36Sopenharmony_ci    echo > set_ftrace_filter
5262306a36Sopenharmony_ci    grep -v '^#' set_ftrace_filter | while read t; do
5362306a36Sopenharmony_ci	tr=`echo $t | cut -d: -f2`
5462306a36Sopenharmony_ci	if [ "$tr" = "" ]; then
5562306a36Sopenharmony_ci	    continue
5662306a36Sopenharmony_ci	fi
5762306a36Sopenharmony_ci	if ! grep -q "$t" set_ftrace_filter; then
5862306a36Sopenharmony_ci		continue;
5962306a36Sopenharmony_ci	fi
6062306a36Sopenharmony_ci	name=`echo $t | cut -d: -f1 | cut -d' ' -f1`
6162306a36Sopenharmony_ci	if [ $tr = "enable_event" -o $tr = "disable_event" ]; then
6262306a36Sopenharmony_ci	    tr=`echo $t | cut -d: -f2-4`
6362306a36Sopenharmony_ci	    limit=`echo $t | cut -d: -f5`
6462306a36Sopenharmony_ci	else
6562306a36Sopenharmony_ci	    tr=`echo $t | cut -d: -f2`
6662306a36Sopenharmony_ci	    limit=`echo $t | cut -d: -f3`
6762306a36Sopenharmony_ci	fi
6862306a36Sopenharmony_ci	if [ "$limit" != "unlimited" ]; then
6962306a36Sopenharmony_ci	    tr="$tr:$limit"
7062306a36Sopenharmony_ci	fi
7162306a36Sopenharmony_ci	echo "!$name:$tr" > set_ftrace_filter
7262306a36Sopenharmony_ci    done
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cidisable_events() {
7662306a36Sopenharmony_ci    echo 0 > events/enable
7762306a36Sopenharmony_ci}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciclear_synthetic_events() { # reset all current synthetic events
8062306a36Sopenharmony_ci    grep -v ^# synthetic_events |
8162306a36Sopenharmony_ci    while read line; do
8262306a36Sopenharmony_ci        echo "!$line" >> synthetic_events
8362306a36Sopenharmony_ci    done
8462306a36Sopenharmony_ci}
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ciclear_dynamic_events() { # reset all current dynamic events
8762306a36Sopenharmony_ci    again=1
8862306a36Sopenharmony_ci    stop=1
8962306a36Sopenharmony_ci    # loop mulitple times as some events require other to be removed first
9062306a36Sopenharmony_ci    while [ $again -eq 1 ]; do
9162306a36Sopenharmony_ci	stop=$((stop+1))
9262306a36Sopenharmony_ci	# Prevent infinite loops
9362306a36Sopenharmony_ci	if [ $stop -gt 10 ]; then
9462306a36Sopenharmony_ci	    break;
9562306a36Sopenharmony_ci	fi
9662306a36Sopenharmony_ci	again=2
9762306a36Sopenharmony_ci	grep -v '^#' dynamic_events|
9862306a36Sopenharmony_ci	while read line; do
9962306a36Sopenharmony_ci	    del=`echo $line | sed -e 's/^.\([^ ]*\).*/-\1/'`
10062306a36Sopenharmony_ci	    if ! echo "$del" >> dynamic_events; then
10162306a36Sopenharmony_ci		again=1
10262306a36Sopenharmony_ci	    fi
10362306a36Sopenharmony_ci	done
10462306a36Sopenharmony_ci    done
10562306a36Sopenharmony_ci}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciinitialize_ftrace() { # Reset ftrace to initial-state
10862306a36Sopenharmony_ci# As the initial state, ftrace will be set to nop tracer,
10962306a36Sopenharmony_ci# no events, no triggers, no filters, no function filters,
11062306a36Sopenharmony_ci# no probes, and tracing on.
11162306a36Sopenharmony_ci    disable_tracing
11262306a36Sopenharmony_ci    reset_tracer
11362306a36Sopenharmony_ci    reset_trigger
11462306a36Sopenharmony_ci    reset_events_filter
11562306a36Sopenharmony_ci    reset_ftrace_filter
11662306a36Sopenharmony_ci    disable_events
11762306a36Sopenharmony_ci    clear_dynamic_events
11862306a36Sopenharmony_ci    [ -f set_event_pid ] && echo > set_event_pid
11962306a36Sopenharmony_ci    [ -f set_ftrace_pid ] && echo > set_ftrace_pid
12062306a36Sopenharmony_ci    [ -f set_ftrace_notrace ] && echo > set_ftrace_notrace
12162306a36Sopenharmony_ci    [ -f set_graph_function ] && echo | tee set_graph_*
12262306a36Sopenharmony_ci    [ -f stack_trace_filter ] && echo > stack_trace_filter
12362306a36Sopenharmony_ci    [ -f kprobe_events ] && echo > kprobe_events
12462306a36Sopenharmony_ci    [ -f uprobe_events ] && echo > uprobe_events
12562306a36Sopenharmony_ci    [ -f synthetic_events ] && echo > synthetic_events
12662306a36Sopenharmony_ci    [ -f snapshot ] && echo 0 > snapshot
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci# Stop tracing while reading the trace file by default, to prevent
12962306a36Sopenharmony_ci# the test results while checking it and to avoid taking a long time
13062306a36Sopenharmony_ci# to check the result.
13162306a36Sopenharmony_ci    [ -f options/pause-on-trace ] && echo 1 > options/pause-on-trace
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci    clear_trace
13462306a36Sopenharmony_ci    enable_tracing
13562306a36Sopenharmony_ci}
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_cifinish_ftrace() {
13862306a36Sopenharmony_ci    initialize_ftrace
13962306a36Sopenharmony_ci# And recover it to default.
14062306a36Sopenharmony_ci    [ -f options/pause-on-trace ] && echo 0 > options/pause-on-trace
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_cicheck_requires() { # Check required files and tracers
14462306a36Sopenharmony_ci    for i in "$@" ; do
14562306a36Sopenharmony_ci	p=${i%:program}
14662306a36Sopenharmony_ci        r=${i%:README}
14762306a36Sopenharmony_ci        t=${i%:tracer}
14862306a36Sopenharmony_ci	if [ $p != $i ]; then
14962306a36Sopenharmony_ci	    if ! which $p ; then
15062306a36Sopenharmony_ci                echo "Required program $p is not found."
15162306a36Sopenharmony_ci                exit_unresolved
15262306a36Sopenharmony_ci	    fi
15362306a36Sopenharmony_ci        elif [ $t != $i ]; then
15462306a36Sopenharmony_ci            if ! grep -wq $t available_tracers ; then
15562306a36Sopenharmony_ci                echo "Required tracer $t is not configured."
15662306a36Sopenharmony_ci                exit_unsupported
15762306a36Sopenharmony_ci            fi
15862306a36Sopenharmony_ci        elif [ "$r" != "$i" ]; then
15962306a36Sopenharmony_ci            if ! grep -Fq "$r" README ; then
16062306a36Sopenharmony_ci                echo "Required feature pattern \"$r\" is not in README."
16162306a36Sopenharmony_ci                exit_unsupported
16262306a36Sopenharmony_ci            fi
16362306a36Sopenharmony_ci        elif [ ! -e $i ]; then
16462306a36Sopenharmony_ci            echo "Required feature interface $i doesn't exist."
16562306a36Sopenharmony_ci            exit_unsupported
16662306a36Sopenharmony_ci        fi
16762306a36Sopenharmony_ci    done
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ciLOCALHOST=127.0.0.1
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciyield() {
17362306a36Sopenharmony_ci    ping $LOCALHOST -c 1 || sleep .001 || usleep 1 || sleep 1
17462306a36Sopenharmony_ci}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci# The fork function in the kernel was renamed from "_do_fork" to
17762306a36Sopenharmony_ci# "kernel_fork". As older tests should still work with older kernels
17862306a36Sopenharmony_ci# as well as newer kernels, check which version of fork is used on this
17962306a36Sopenharmony_ci# kernel so that the tests can use the fork function for the running kernel.
18062306a36Sopenharmony_ciFUNCTION_FORK=`(if grep '\bkernel_clone\b' /proc/kallsyms > /dev/null; then
18162306a36Sopenharmony_ci                echo kernel_clone; else echo '_do_fork'; fi)`
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci# Since probe event command may include backslash, explicitly use printf "%s"
18462306a36Sopenharmony_ci# to NOT interpret it.
18562306a36Sopenharmony_ciftrace_errlog_check() { # err-prefix command-with-error-pos-by-^ command-file
18662306a36Sopenharmony_ci    pos=$(printf "%s" "${2%^*}" | wc -c) # error position
18762306a36Sopenharmony_ci    command=$(printf "%s" "$2" | tr -d ^)
18862306a36Sopenharmony_ci    echo "Test command: $command"
18962306a36Sopenharmony_ci    echo > error_log
19062306a36Sopenharmony_ci    (! printf "%s" "$command" >> "$3" ) 2> /dev/null
19162306a36Sopenharmony_ci    grep "$1: error:" -A 3 error_log
19262306a36Sopenharmony_ci    N=$(tail -n 1 error_log | wc -c)
19362306a36Sopenharmony_ci    # "  Command: " and "^\n" => 13
19462306a36Sopenharmony_ci    test $(expr 13 + $pos) -eq $N
19562306a36Sopenharmony_ci}
196