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 ${perf} buildid-cache -l | grep $id 54 if [ $? -ne 0 ]; then 55 echo "failed: ${id} is not reported by \"perf buildid-cache -l\"" 56 exit 1 57 fi 58 59 echo "OK for ${1}" 60} 61 62test_add() 63{ 64 build_id_dir=$(mktemp -d /tmp/perf.debug.XXX) 65 perf="perf --buildid-dir ${build_id_dir}" 66 67 ${perf} buildid-cache -v -a ${1} 68 if [ $? -ne 0 ]; then 69 echo "failed: add ${1} to build id cache" 70 exit 1 71 fi 72 73 check ${1} 74 75 rm -rf ${build_id_dir} 76} 77 78test_record() 79{ 80 data=$(mktemp /tmp/perf.data.XXX) 81 build_id_dir=$(mktemp -d /tmp/perf.debug.XXX) 82 perf="perf --buildid-dir ${build_id_dir}" 83 84 ${perf} record --buildid-all -o ${data} ${1} 85 if [ $? -ne 0 ]; then 86 echo "failed: record ${1}" 87 exit 1 88 fi 89 90 check ${1} 91 92 rm -rf ${build_id_dir} 93 rm -rf ${data} 94} 95 96# add binaries manual via perf buildid-cache -a 97test_add ${ex_sha1} 98test_add ${ex_md5} 99 100# add binaries via perf record post processing 101test_record ${ex_sha1} 102test_record ${ex_md5} 103 104# cleanup 105rm ${ex_sha1} ${ex_md5} 106 107exit ${err} 108