xref: /linux/tools/perf/tests/shell/trace_btf_general.sh (revision f4f346c3465949ebba80c6cc52cd8d2eeaa545fd)
1#!/bin/bash
2# perf trace BTF general tests
3# SPDX-License-Identifier: GPL-2.0
4
5err=0
6
7# shellcheck source=lib/probe.sh
8. "$(dirname $0)"/lib/probe.sh
9
10file1=$(mktemp /tmp/file1_XXXX)
11file2=$(echo $file1 | sed 's/file1/file2/g')
12
13buffer="buffer content"
14perf_config_tmp=$(mktemp /tmp/.perfconfig_XXXXX)
15
16trap cleanup EXIT TERM INT HUP
17
18check_vmlinux() {
19  echo "Checking if vmlinux BTF exists"
20  if [ ! -f /sys/kernel/btf/vmlinux ]
21  then
22    echo "Skipped due to missing vmlinux BTF"
23    return 2
24  fi
25  return 0
26}
27
28trace_test_string() {
29  echo "Testing perf trace's string augmentation"
30  output="$(perf trace --sort-events -e renameat* --max-events=1 -- mv ${file1} ${file2} 2>&1)"
31  if ! echo "$output" | grep -q -E "^mv/[0-9]+ renameat(2)?\(.*, \"${file1}\", .*, \"${file2}\", .*\) += +[0-9]+$"
32  then
33    printf "String augmentation test failed, output:\n$output\n"
34    err=1
35  fi
36}
37
38trace_test_buffer() {
39  echo "Testing perf trace's buffer augmentation"
40  # echo will insert a newline (\10) at the end of the buffer
41  output="$(perf trace --sort-events -e write --max-events=1 -- echo "${buffer}" 2>&1)"
42  if ! echo "$output" | grep -q -E "^echo/[0-9]+ write\([0-9]+, ${buffer}.*, [0-9]+\) += +[0-9]+$"
43  then
44    printf "Buffer augmentation test failed, output:\n$output\n"
45    err=1
46  fi
47}
48
49trace_test_struct_btf() {
50  echo "Testing perf trace's struct augmentation"
51  output="$(perf trace --sort-events -e clock_nanosleep --force-btf --max-events=1 -- sleep 1 2>&1)"
52  if ! echo "$output" | grep -q -E "^sleep/[0-9]+ clock_nanosleep\(0, 0, \{1,.*\}, 0x[0-9a-f]+\) += +[0-9]+$"
53  then
54	printf "BTF struct augmentation test failed, output:\n$output\n"
55    err=1
56  fi
57}
58
59cleanup() {
60  rm -rf ${file1} ${file2} ${perf_config_tmp}
61}
62
63trap_cleanup() {
64  echo "Unexpected signal in ${FUNCNAME[1]}"
65  cleanup
66  exit 1
67}
68
69# don't overwrite user's perf config
70trace_config() {
71  export PERF_CONFIG=${perf_config_tmp}
72  perf config trace.show_arg_names=false trace.show_duration=false \
73    trace.show_timestamp=false trace.args_alignment=0
74}
75
76skip_if_no_perf_trace || exit 2
77check_vmlinux || exit 2
78[ "$(id -u)" = 0 ] || exit 2
79
80trace_config
81
82trace_test_string
83
84if [ $err = 0 ]; then
85  trace_test_buffer
86fi
87
88if [ $err = 0 ]; then
89  trace_test_struct_btf
90fi
91
92cleanup
93
94exit $err
95