1#!/bin/bash
2# perf annotate basic tests
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7shelldir=$(dirname "$0")
8
9# shellcheck source=lib/perf_has_symbol.sh
10. "${shelldir}"/lib/perf_has_symbol.sh
11
12testsym="noploop"
13
14skip_test_missing_symbol ${testsym}
15
16err=0
17perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
18perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
19testprog="perf test -w noploop"
20# disassembly format: "percent : offset: instruction (operands ...)"
21disasm_regex="[0-9]*\.[0-9]* *: *\w*: *\w*"
22
23cleanup() {
24  rm -rf "${perfdata}" "${perfout}"
25  rm -rf "${perfdata}".old
26
27  trap - EXIT TERM INT
28}
29
30trap_cleanup() {
31  echo "Unexpected signal in ${FUNCNAME[1]}"
32  cleanup
33  exit 1
34}
35trap trap_cleanup EXIT TERM INT
36
37test_basic() {
38  mode=$1
39  echo "${mode} perf annotate test"
40  if [ "x${mode}" == "xBasic" ]
41  then
42    perf record -o "${perfdata}" ${testprog} 2> /dev/null
43  else
44    perf record -o - ${testprog} 2> /dev/null > "${perfdata}"
45  fi
46  if [ "x$?" != "x0" ]
47  then
48    echo "${mode} annotate [Failed: perf record]"
49    err=1
50    return
51  fi
52
53  # Generate the annotated output file
54  if [ "x${mode}" == "xBasic" ]
55  then
56    perf annotate --no-demangle -i "${perfdata}" --stdio 2> /dev/null > "${perfout}"
57  else
58    perf annotate --no-demangle -i - --stdio 2> /dev/null < "${perfdata}" > "${perfout}"
59  fi
60
61  # check if it has the target symbol
62  if ! head -250 "${perfout}" | grep -q "${testsym}"
63  then
64    echo "${mode} annotate [Failed: missing target symbol]"
65    err=1
66    return
67  fi
68
69  # check if it has the disassembly lines
70  if ! head -250 "${perfout}" | grep -q "${disasm_regex}"
71  then
72    echo "${mode} annotate [Failed: missing disasm output from default disassembler]"
73    err=1
74    return
75  fi
76
77  # check again with a target symbol name
78  if [ "x${mode}" == "xBasic" ]
79  then
80    perf annotate --no-demangle -i "${perfdata}" "${testsym}" 2> /dev/null > "${perfout}"
81  else
82    perf annotate --no-demangle -i - "${testsym}" 2> /dev/null < "${perfdata}" > "${perfout}"
83  fi
84
85  if ! head -250 "${perfout}"| grep -q -m 3 "${disasm_regex}"
86  then
87    echo "${mode} annotate [Failed: missing disasm output when specifying the target symbol]"
88    err=1
89    return
90  fi
91
92  # check one more with external objdump tool (forced by --objdump option)
93  if [ "x${mode}" == "xBasic" ]
94  then
95    perf annotate --no-demangle -i "${perfdata}" --objdump=objdump 2> /dev/null > "${perfout}"
96  else
97    perf annotate --no-demangle -i - "${testsym}" 2> /dev/null < "${perfdata}" > "${perfout}"
98  fi
99  if ! head -250 "${perfout}" | grep -q -m 3 "${disasm_regex}"
100  then
101    echo "${mode} annotate [Failed: missing disasm output from non default disassembler (using --objdump)]"
102    err=1
103    return
104  fi
105  echo "${mode} annotate test [Success]"
106}
107
108test_basic Basic
109test_basic Pipe
110
111cleanup
112exit $err
113