xref: /openbmc/linux/tools/perf/tests/shell/buildid.sh (revision a90fa0ad)
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	# Check for file permission of original file
81	# in case of pe-file.exe file
82	echo $1 | grep ".exe"
83	if [ $? -eq 0 ]; then
84		if [ -x $1  -a ! -x $file ]; then
85			echo "failed: file ${file} executable does not exist"
86			exit 1
87		fi
88
89		if [ ! -x $file -a ! -e $file ]; then
90			echo "failed: file ${file} does not exist"
91			exit 1
92		fi
93	elif [ ! -x $file ]; then
94		echo "failed: file ${file} does not exist"
95		exit 1
96	fi
97
98	diff ${file} ${1}
99	if [ $? -ne 0 ]; then
100		echo "failed: ${file} do not match"
101		exit 1
102	fi
103
104	${perf} buildid-cache -l | grep ${id}
105	if [ $? -ne 0 ]; then
106		echo "failed: ${id} is not reported by \"perf buildid-cache -l\""
107		exit 1
108	fi
109
110	echo "OK for ${1}"
111}
112
113test_add()
114{
115	build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
116	perf="perf --buildid-dir ${build_id_dir}"
117
118	${perf} buildid-cache -v -a ${1}
119	if [ $? -ne 0 ]; then
120		echo "failed: add ${1} to build id cache"
121		exit 1
122	fi
123
124	check ${1}
125
126	rm -rf ${build_id_dir}
127}
128
129test_record()
130{
131	data=$(mktemp /tmp/perf.data.XXX)
132	build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
133	log=$(mktemp /tmp/perf.log.XXX)
134	perf="perf --buildid-dir ${build_id_dir}"
135
136	echo "running: perf record $@"
137	${perf} record --buildid-all -o ${data} $@ &> ${log}
138	if [ $? -ne 0 ]; then
139		echo "failed: record $@"
140		echo "see log: ${log}"
141		exit 1
142	fi
143
144	check ${@: -1}
145
146	rm -f ${log}
147	rm -rf ${build_id_dir}
148	rm -rf ${data}
149}
150
151# add binaries manual via perf buildid-cache -a
152test_add ${ex_sha1}
153test_add ${ex_md5}
154if [ ${add_pe} -eq 1 ]; then
155	test_add ${ex_pe}
156fi
157
158# add binaries via perf record post processing
159test_record ${ex_sha1}
160test_record ${ex_md5}
161if [ ${run_pe} -eq 1 ]; then
162	test_record wine ${ex_pe}
163fi
164
165# cleanup
166rm ${ex_sha1} ${ex_md5}
167if [ ${run_pe} -eq 1 ]; then
168	rm -r ${wineprefix}
169fi
170
171exit ${err}
172