xref: /openbmc/linux/tools/perf/tests/shell/buildid.sh (revision fadbafc1)
1#!/bin/sh
2# build id cache operations
3# SPDX-License-Identifier: GPL-2.0
4
5# skip if there's no readelf
6if ! [ -x "$(command -v readelf)" ]; then
7	echo "failed: no readelf, install binutils"
8	exit 2
9fi
10
11# skip if there's no compiler
12if ! [ -x "$(command -v cc)" ]; then
13	echo "failed: no compiler, install gcc"
14	exit 2
15fi
16
17# check what we need to test windows binaries
18add_pe=1
19run_pe=1
20if ! perf version --build-options | grep -q 'libbfd: .* on '; then
21	echo "WARNING: perf not built with libbfd. PE binaries will not be tested."
22	add_pe=0
23	run_pe=0
24fi
25if ! which wine > /dev/null; then
26	echo "WARNING: wine not found. PE binaries will not be run."
27	run_pe=0
28fi
29
30# set up wine
31if [ ${run_pe} -eq 1 ]; then
32	wineprefix=$(mktemp -d /tmp/perf.wineprefix.XXX)
33	export WINEPREFIX=${wineprefix}
34	# clear display variables to prevent wine from popping up dialogs
35	unset DISPLAY
36	unset WAYLAND_DISPLAY
37fi
38
39ex_md5=$(mktemp /tmp/perf.ex.MD5.XXX)
40ex_sha1=$(mktemp /tmp/perf.ex.SHA1.XXX)
41ex_pe=$(dirname $0)/../pe-file.exe
42
43echo 'int main(void) { return 0; }' | cc -Wl,--build-id=sha1 -o ${ex_sha1} -x c -
44echo 'int main(void) { return 0; }' | cc -Wl,--build-id=md5 -o ${ex_md5} -x c -
45
46echo "test binaries: ${ex_sha1} ${ex_md5} ${ex_pe}"
47
48check()
49{
50	case $1 in
51	*.exe)
52		# We don't have a tool that can pull a nicely formatted build-id out of
53		# a PE file, but we can extract the whole section with objcopy and
54		# format it ourselves. The .buildid section is a Debug Directory
55		# containing a CodeView entry:
56		#     https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#debug-directory-image-only
57		#     https://github.com/dotnet/runtime/blob/da94c022576a5c3bbc0e896f006565905eb137f9/docs/design/specs/PE-COFF.md
58		# The build-id starts at byte 33 and must be rearranged into a GUID.
59		id=`objcopy -O binary --only-section=.buildid $1 /dev/stdout | \
60			cut -c 33-48 | hexdump -ve '/1 "%02x"' | \
61			sed 's@^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(.*\)0a$@\4\3\2\1\6\5\8\7\9@'`
62		;;
63	*)
64		id=`readelf -n ${1} 2>/dev/null | grep 'Build ID' | awk '{print $3}'`
65		;;
66	esac
67	echo "build id: ${id}"
68
69	link=${build_id_dir}/.build-id/${id:0:2}/${id:2}
70	echo "link: ${link}"
71
72	if [ ! -h $link ]; then
73		echo "failed: link ${link} does not exist"
74		exit 1
75	fi
76
77	file=${build_id_dir}/.build-id/${id:0:2}/`readlink ${link}`/elf
78	echo "file: ${file}"
79
80	if [ ! -x $file ]; then
81		echo "failed: file ${file} does not exist"
82		exit 1
83	fi
84
85	diff ${file} ${1}
86	if [ $? -ne 0 ]; then
87		echo "failed: ${file} do not match"
88		exit 1
89	fi
90
91	${perf} buildid-cache -l | grep ${id}
92	if [ $? -ne 0 ]; then
93		echo "failed: ${id} is not reported by \"perf buildid-cache -l\""
94		exit 1
95	fi
96
97	echo "OK for ${1}"
98}
99
100test_add()
101{
102	build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
103	perf="perf --buildid-dir ${build_id_dir}"
104
105	${perf} buildid-cache -v -a ${1}
106	if [ $? -ne 0 ]; then
107		echo "failed: add ${1} to build id cache"
108		exit 1
109	fi
110
111	check ${1}
112
113	rm -rf ${build_id_dir}
114}
115
116test_record()
117{
118	data=$(mktemp /tmp/perf.data.XXX)
119	build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
120	log=$(mktemp /tmp/perf.log.XXX)
121	perf="perf --buildid-dir ${build_id_dir}"
122
123	echo "running: perf record $@"
124	${perf} record --buildid-all -o ${data} $@ &> ${log}
125	if [ $? -ne 0 ]; then
126		echo "failed: record $@"
127		echo "see log: ${log}"
128		exit 1
129	fi
130
131	check ${@: -1}
132
133	rm -f ${log}
134	rm -rf ${build_id_dir}
135	rm -rf ${data}
136}
137
138# add binaries manual via perf buildid-cache -a
139test_add ${ex_sha1}
140test_add ${ex_md5}
141if [ ${add_pe} -eq 1 ]; then
142	test_add ${ex_pe}
143fi
144
145# add binaries via perf record post processing
146test_record ${ex_sha1}
147test_record ${ex_md5}
148if [ ${run_pe} -eq 1 ]; then
149	test_record wine ${ex_pe}
150fi
151
152# cleanup
153rm ${ex_sha1} ${ex_md5}
154if [ ${run_pe} -eq 1 ]; then
155	rm -r ${wineprefix}
156fi
157
158exit ${err}
159