xref: /linux-6.15/tools/perf/tests/shell/annotate.sh (revision 658b34cc)
1653ac51fSIan Rogers#!/bin/bash
2281bf8f6SNamhyung Kim# perf annotate basic tests
3281bf8f6SNamhyung Kim# SPDX-License-Identifier: GPL-2.0
4281bf8f6SNamhyung Kim
5281bf8f6SNamhyung Kimset -e
6281bf8f6SNamhyung Kim
7281bf8f6SNamhyung Kimshelldir=$(dirname "$0")
8281bf8f6SNamhyung Kim
9281bf8f6SNamhyung Kim# shellcheck source=lib/perf_has_symbol.sh
10281bf8f6SNamhyung Kim. "${shelldir}"/lib/perf_has_symbol.sh
11281bf8f6SNamhyung Kim
12281bf8f6SNamhyung Kimtestsym="noploop"
13281bf8f6SNamhyung Kim
14281bf8f6SNamhyung Kimskip_test_missing_symbol ${testsym}
15281bf8f6SNamhyung Kim
16281bf8f6SNamhyung Kimerr=0
17281bf8f6SNamhyung Kimperfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
18658a8805SThomas Richterperfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
19281bf8f6SNamhyung Kimtestprog="perf test -w noploop"
20281bf8f6SNamhyung Kim# disassembly format: "percent : offset: instruction (operands ...)"
21281bf8f6SNamhyung Kimdisasm_regex="[0-9]*\.[0-9]* *: *\w*: *\w*"
22281bf8f6SNamhyung Kim
23281bf8f6SNamhyung Kimcleanup() {
24658a8805SThomas Richter  rm -rf "${perfdata}" "${perfout}"
25281bf8f6SNamhyung Kim  rm -rf "${perfdata}".old
26281bf8f6SNamhyung Kim
27281bf8f6SNamhyung Kim  trap - EXIT TERM INT
28281bf8f6SNamhyung Kim}
29281bf8f6SNamhyung Kim
30281bf8f6SNamhyung Kimtrap_cleanup() {
31653ac51fSIan Rogers  echo "Unexpected signal in ${FUNCNAME[1]}"
32281bf8f6SNamhyung Kim  cleanup
33281bf8f6SNamhyung Kim  exit 1
34281bf8f6SNamhyung Kim}
35281bf8f6SNamhyung Kimtrap trap_cleanup EXIT TERM INT
36281bf8f6SNamhyung Kim
37281bf8f6SNamhyung Kimtest_basic() {
38*658b34ccSIan Rogers  mode=$1
39*658b34ccSIan Rogers  echo "${mode} perf annotate test"
40*658b34ccSIan Rogers  if [ "x${mode}" == "xBasic" ]
41281bf8f6SNamhyung Kim  then
42*658b34ccSIan Rogers    perf record -o "${perfdata}" ${testprog} 2> /dev/null
43*658b34ccSIan Rogers  else
44*658b34ccSIan Rogers    perf record -o - ${testprog} 2> /dev/null > "${perfdata}"
45*658b34ccSIan Rogers  fi
46*658b34ccSIan Rogers  if [ "x$?" != "x0" ]
47*658b34ccSIan Rogers  then
48*658b34ccSIan Rogers    echo "${mode} annotate [Failed: perf record]"
49281bf8f6SNamhyung Kim    err=1
50281bf8f6SNamhyung Kim    return
51281bf8f6SNamhyung Kim  fi
52281bf8f6SNamhyung Kim
53658a8805SThomas Richter  # Generate the annotated output file
54*658b34ccSIan Rogers  if [ "x${mode}" == "xBasic" ]
55*658b34ccSIan Rogers  then
56*658b34ccSIan Rogers    perf annotate --no-demangle -i "${perfdata}" --stdio 2> /dev/null > "${perfout}"
57*658b34ccSIan Rogers  else
58*658b34ccSIan Rogers    perf annotate --no-demangle -i - --stdio 2> /dev/null < "${perfdata}" > "${perfout}"
59*658b34ccSIan Rogers  fi
60658a8805SThomas Richter
61281bf8f6SNamhyung Kim  # check if it has the target symbol
62*658b34ccSIan Rogers  if ! head -250 "${perfout}" | grep -q "${testsym}"
63281bf8f6SNamhyung Kim  then
64*658b34ccSIan Rogers    echo "${mode} annotate [Failed: missing target symbol]"
65281bf8f6SNamhyung Kim    err=1
66281bf8f6SNamhyung Kim    return
67281bf8f6SNamhyung Kim  fi
68281bf8f6SNamhyung Kim
69281bf8f6SNamhyung Kim  # check if it has the disassembly lines
70*658b34ccSIan Rogers  if ! head -250 "${perfout}" | grep -q "${disasm_regex}"
71281bf8f6SNamhyung Kim  then
72*658b34ccSIan Rogers    echo "${mode} annotate [Failed: missing disasm output from default disassembler]"
73281bf8f6SNamhyung Kim    err=1
74281bf8f6SNamhyung Kim    return
75281bf8f6SNamhyung Kim  fi
76281bf8f6SNamhyung Kim
77281bf8f6SNamhyung Kim  # check again with a target symbol name
78*658b34ccSIan Rogers  if [ "x${mode}" == "xBasic" ]
79281bf8f6SNamhyung Kim  then
80*658b34ccSIan Rogers    perf annotate --no-demangle -i "${perfdata}" "${testsym}" 2> /dev/null > "${perfout}"
81*658b34ccSIan Rogers  else
82*658b34ccSIan Rogers    perf annotate --no-demangle -i - "${testsym}" 2> /dev/null < "${perfdata}" > "${perfout}"
83*658b34ccSIan Rogers  fi
84*658b34ccSIan Rogers
85*658b34ccSIan Rogers  if ! head -250 "${perfout}"| grep -q -m 3 "${disasm_regex}"
86*658b34ccSIan Rogers  then
87*658b34ccSIan Rogers    echo "${mode} annotate [Failed: missing disasm output when specifying the target symbol]"
88281bf8f6SNamhyung Kim    err=1
89281bf8f6SNamhyung Kim    return
90281bf8f6SNamhyung Kim  fi
91281bf8f6SNamhyung Kim
92281bf8f6SNamhyung Kim  # check one more with external objdump tool (forced by --objdump option)
93*658b34ccSIan Rogers  if [ "x${mode}" == "xBasic" ]
94281bf8f6SNamhyung Kim  then
95*658b34ccSIan Rogers    perf annotate --no-demangle -i "${perfdata}" --objdump=objdump 2> /dev/null > "${perfout}"
96*658b34ccSIan Rogers  else
97*658b34ccSIan Rogers    perf annotate --no-demangle -i - "${testsym}" 2> /dev/null < "${perfdata}" > "${perfout}"
98*658b34ccSIan Rogers  fi
99*658b34ccSIan Rogers  if ! head -250 "${perfout}" | grep -q -m 3 "${disasm_regex}"
100*658b34ccSIan Rogers  then
101*658b34ccSIan Rogers    echo "${mode} annotate [Failed: missing disasm output from non default disassembler (using --objdump)]"
102281bf8f6SNamhyung Kim    err=1
103281bf8f6SNamhyung Kim    return
104281bf8f6SNamhyung Kim  fi
105*658b34ccSIan Rogers  echo "${mode} annotate test [Success]"
106281bf8f6SNamhyung Kim}
107281bf8f6SNamhyung Kim
108*658b34ccSIan Rogerstest_basic Basic
109*658b34ccSIan Rogerstest_basic Pipe
110281bf8f6SNamhyung Kim
111281bf8f6SNamhyung Kimcleanup
112281bf8f6SNamhyung Kimexit $err
113