xref: /openbmc/linux/tools/perf/tests/shell/buildid.sh (revision abe9af53)
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
17ex_md5=$(mktemp /tmp/perf.ex.MD5.XXX)
18ex_sha1=$(mktemp /tmp/perf.ex.SHA1.XXX)
19
20echo 'int main(void) { return 0; }' | cc -Wl,--build-id=sha1 -o ${ex_sha1} -x c -
21echo 'int main(void) { return 0; }' | cc -Wl,--build-id=md5 -o ${ex_md5} -x c -
22
23echo "test binaries: ${ex_sha1} ${ex_md5}"
24
25check()
26{
27	id=`readelf -n ${1} 2>/dev/null | grep 'Build ID' | awk '{print $3}'`
28
29	echo "build id: ${id}"
30
31	link=${build_id_dir}/.build-id/${id:0:2}/${id:2}
32	echo "link: ${link}"
33
34	if [ ! -h $link ]; then
35		echo "failed: link ${link} does not exist"
36		exit 1
37	fi
38
39	file=${build_id_dir}/.build-id/${id:0:2}/`readlink ${link}`/elf
40	echo "file: ${file}"
41
42	if [ ! -x $file ]; then
43		echo "failed: file ${file} does not exist"
44		exit 1
45	fi
46
47	diff ${file} ${1}
48	if [ $? -ne 0 ]; then
49		echo "failed: ${file} do not match"
50		exit 1
51	fi
52
53	echo "OK for ${1}"
54}
55
56test_add()
57{
58	build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
59	perf="perf --buildid-dir ${build_id_dir}"
60
61	${perf} buildid-cache -v -a ${1}
62	if [ $? -ne 0 ]; then
63		echo "failed: add ${1} to build id cache"
64		exit 1
65	fi
66
67	check ${1}
68
69	rm -rf ${build_id_dir}
70}
71
72test_record()
73{
74	data=$(mktemp /tmp/perf.data.XXX)
75	build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
76	perf="perf --buildid-dir ${build_id_dir}"
77
78	${perf} record --buildid-all -o ${data} ${1}
79	if [ $? -ne 0 ]; then
80		echo "failed: record ${1}"
81		exit 1
82	fi
83
84	check ${1}
85
86	rm -rf ${build_id_dir}
87	rm -rf ${data}
88}
89
90# add binaries manual via perf buildid-cache -a
91test_add ${ex_sha1}
92test_add ${ex_md5}
93
94# add binaries via perf record post processing
95test_record ${ex_sha1}
96test_record ${ex_md5}
97
98# cleanup
99rm ${ex_sha1} ${ex_md5}
100
101exit ${err}
102