178b2c50cSJiri Olsa#!/bin/sh 278b2c50cSJiri Olsa# build id cache operations 378b2c50cSJiri Olsa# SPDX-License-Identifier: GPL-2.0 478b2c50cSJiri Olsa 578b2c50cSJiri Olsa# skip if there's no readelf 678b2c50cSJiri Olsaif ! [ -x "$(command -v readelf)" ]; then 778b2c50cSJiri Olsa echo "failed: no readelf, install binutils" 878b2c50cSJiri Olsa exit 2 978b2c50cSJiri Olsafi 1078b2c50cSJiri Olsa 1178b2c50cSJiri Olsa# skip if there's no compiler 1278b2c50cSJiri Olsaif ! [ -x "$(command -v cc)" ]; then 1378b2c50cSJiri Olsa echo "failed: no compiler, install gcc" 1478b2c50cSJiri Olsa exit 2 1578b2c50cSJiri Olsafi 1678b2c50cSJiri Olsa 1734968b93SNicholas Fraser# check what we need to test windows binaries 1834968b93SNicholas Fraseradd_pe=1 1934968b93SNicholas Fraserrun_pe=1 2034968b93SNicholas Fraserif ! perf version --build-options | grep -q 'libbfd: .* on '; then 2134968b93SNicholas Fraser echo "WARNING: perf not built with libbfd. PE binaries will not be tested." 2234968b93SNicholas Fraser add_pe=0 2334968b93SNicholas Fraser run_pe=0 2434968b93SNicholas Fraserfi 2534968b93SNicholas Fraserif ! which wine > /dev/null; then 2634968b93SNicholas Fraser echo "WARNING: wine not found. PE binaries will not be run." 2734968b93SNicholas Fraser run_pe=0 2834968b93SNicholas Fraserfi 2934968b93SNicholas Fraser 3034968b93SNicholas Fraser# set up wine 3134968b93SNicholas Fraserif [ ${run_pe} -eq 1 ]; then 3234968b93SNicholas Fraser wineprefix=$(mktemp -d /tmp/perf.wineprefix.XXX) 3334968b93SNicholas Fraser export WINEPREFIX=${wineprefix} 3434968b93SNicholas Fraser # clear display variables to prevent wine from popping up dialogs 3534968b93SNicholas Fraser unset DISPLAY 3634968b93SNicholas Fraser unset WAYLAND_DISPLAY 3734968b93SNicholas Fraserfi 3834968b93SNicholas Fraser 3978b2c50cSJiri Olsaex_md5=$(mktemp /tmp/perf.ex.MD5.XXX) 4078b2c50cSJiri Olsaex_sha1=$(mktemp /tmp/perf.ex.SHA1.XXX) 4134968b93SNicholas Fraserex_pe=$(dirname $0)/../pe-file.exe 4278b2c50cSJiri Olsa 4378b2c50cSJiri Olsaecho 'int main(void) { return 0; }' | cc -Wl,--build-id=sha1 -o ${ex_sha1} -x c - 4478b2c50cSJiri Olsaecho 'int main(void) { return 0; }' | cc -Wl,--build-id=md5 -o ${ex_md5} -x c - 4578b2c50cSJiri Olsa 4634968b93SNicholas Fraserecho "test binaries: ${ex_sha1} ${ex_md5} ${ex_pe}" 4778b2c50cSJiri Olsa 4878b2c50cSJiri Olsacheck() 4978b2c50cSJiri Olsa{ 5034968b93SNicholas Fraser case $1 in 5134968b93SNicholas Fraser *.exe) 5234968b93SNicholas Fraser # We don't have a tool that can pull a nicely formatted build-id out of 5334968b93SNicholas Fraser # a PE file, but we can extract the whole section with objcopy and 5434968b93SNicholas Fraser # format it ourselves. The .buildid section is a Debug Directory 5534968b93SNicholas Fraser # containing a CodeView entry: 5634968b93SNicholas Fraser # https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#debug-directory-image-only 5734968b93SNicholas Fraser # https://github.com/dotnet/runtime/blob/da94c022576a5c3bbc0e896f006565905eb137f9/docs/design/specs/PE-COFF.md 5834968b93SNicholas Fraser # The build-id starts at byte 33 and must be rearranged into a GUID. 5934968b93SNicholas Fraser id=`objcopy -O binary --only-section=.buildid $1 /dev/stdout | \ 6034968b93SNicholas Fraser cut -c 33-48 | hexdump -ve '/1 "%02x"' | \ 6134968b93SNicholas Fraser sed 's@^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(.*\)0a$@\4\3\2\1\6\5\8\7\9@'` 6234968b93SNicholas Fraser ;; 6334968b93SNicholas Fraser *) 6478b2c50cSJiri Olsa id=`readelf -n ${1} 2>/dev/null | grep 'Build ID' | awk '{print $3}'` 6534968b93SNicholas Fraser ;; 6634968b93SNicholas Fraser esac 6778b2c50cSJiri Olsa echo "build id: ${id}" 6878b2c50cSJiri Olsa 6978b2c50cSJiri Olsa link=${build_id_dir}/.build-id/${id:0:2}/${id:2} 7078b2c50cSJiri Olsa echo "link: ${link}" 7178b2c50cSJiri Olsa 7278b2c50cSJiri Olsa if [ ! -h $link ]; then 7378b2c50cSJiri Olsa echo "failed: link ${link} does not exist" 7478b2c50cSJiri Olsa exit 1 7578b2c50cSJiri Olsa fi 7678b2c50cSJiri Olsa 7778b2c50cSJiri Olsa file=${build_id_dir}/.build-id/${id:0:2}/`readlink ${link}`/elf 7878b2c50cSJiri Olsa echo "file: ${file}" 7978b2c50cSJiri Olsa 80*3d9c07c4SAthira Rajeev # Check for file permission of original file 81*3d9c07c4SAthira Rajeev # in case of pe-file.exe file 82*3d9c07c4SAthira Rajeev echo $1 | grep ".exe" 83*3d9c07c4SAthira Rajeev if [ $? -eq 0 ]; then 84*3d9c07c4SAthira Rajeev if [ -x $1 -a ! -x $file ]; then 85*3d9c07c4SAthira Rajeev echo "failed: file ${file} executable does not exist" 86*3d9c07c4SAthira Rajeev exit 1 87*3d9c07c4SAthira Rajeev fi 88*3d9c07c4SAthira Rajeev 89*3d9c07c4SAthira Rajeev if [ ! -x $file -a ! -e $file ]; then 90*3d9c07c4SAthira Rajeev echo "failed: file ${file} does not exist" 91*3d9c07c4SAthira Rajeev exit 1 92*3d9c07c4SAthira Rajeev fi 93*3d9c07c4SAthira Rajeev elif [ ! -x $file ]; then 9478b2c50cSJiri Olsa echo "failed: file ${file} does not exist" 9578b2c50cSJiri Olsa exit 1 9678b2c50cSJiri Olsa fi 9778b2c50cSJiri Olsa 9878b2c50cSJiri Olsa diff ${file} ${1} 9978b2c50cSJiri Olsa if [ $? -ne 0 ]; then 10078b2c50cSJiri Olsa echo "failed: ${file} do not match" 10178b2c50cSJiri Olsa exit 1 10278b2c50cSJiri Olsa fi 10378b2c50cSJiri Olsa 10434968b93SNicholas Fraser ${perf} buildid-cache -l | grep ${id} 105206236d3SNicholas Fraser if [ $? -ne 0 ]; then 106206236d3SNicholas Fraser echo "failed: ${id} is not reported by \"perf buildid-cache -l\"" 107206236d3SNicholas Fraser exit 1 108206236d3SNicholas Fraser fi 109206236d3SNicholas Fraser 11078b2c50cSJiri Olsa echo "OK for ${1}" 11178b2c50cSJiri Olsa} 11278b2c50cSJiri Olsa 11378b2c50cSJiri Olsatest_add() 11478b2c50cSJiri Olsa{ 11578b2c50cSJiri Olsa build_id_dir=$(mktemp -d /tmp/perf.debug.XXX) 11678b2c50cSJiri Olsa perf="perf --buildid-dir ${build_id_dir}" 11778b2c50cSJiri Olsa 11878b2c50cSJiri Olsa ${perf} buildid-cache -v -a ${1} 11978b2c50cSJiri Olsa if [ $? -ne 0 ]; then 12078b2c50cSJiri Olsa echo "failed: add ${1} to build id cache" 12178b2c50cSJiri Olsa exit 1 12278b2c50cSJiri Olsa fi 12378b2c50cSJiri Olsa 12478b2c50cSJiri Olsa check ${1} 12578b2c50cSJiri Olsa 12678b2c50cSJiri Olsa rm -rf ${build_id_dir} 12778b2c50cSJiri Olsa} 12878b2c50cSJiri Olsa 12978b2c50cSJiri Olsatest_record() 13078b2c50cSJiri Olsa{ 13178b2c50cSJiri Olsa data=$(mktemp /tmp/perf.data.XXX) 13278b2c50cSJiri Olsa build_id_dir=$(mktemp -d /tmp/perf.debug.XXX) 13334968b93SNicholas Fraser log=$(mktemp /tmp/perf.log.XXX) 13478b2c50cSJiri Olsa perf="perf --buildid-dir ${build_id_dir}" 13578b2c50cSJiri Olsa 13634968b93SNicholas Fraser echo "running: perf record $@" 13734968b93SNicholas Fraser ${perf} record --buildid-all -o ${data} $@ &> ${log} 13878b2c50cSJiri Olsa if [ $? -ne 0 ]; then 13934968b93SNicholas Fraser echo "failed: record $@" 14034968b93SNicholas Fraser echo "see log: ${log}" 14178b2c50cSJiri Olsa exit 1 14278b2c50cSJiri Olsa fi 14378b2c50cSJiri Olsa 14434968b93SNicholas Fraser check ${@: -1} 14578b2c50cSJiri Olsa 14634968b93SNicholas Fraser rm -f ${log} 14778b2c50cSJiri Olsa rm -rf ${build_id_dir} 14878b2c50cSJiri Olsa rm -rf ${data} 14978b2c50cSJiri Olsa} 15078b2c50cSJiri Olsa 15178b2c50cSJiri Olsa# add binaries manual via perf buildid-cache -a 15278b2c50cSJiri Olsatest_add ${ex_sha1} 15378b2c50cSJiri Olsatest_add ${ex_md5} 15434968b93SNicholas Fraserif [ ${add_pe} -eq 1 ]; then 15534968b93SNicholas Fraser test_add ${ex_pe} 15634968b93SNicholas Fraserfi 15778b2c50cSJiri Olsa 15878b2c50cSJiri Olsa# add binaries via perf record post processing 15978b2c50cSJiri Olsatest_record ${ex_sha1} 16078b2c50cSJiri Olsatest_record ${ex_md5} 16134968b93SNicholas Fraserif [ ${run_pe} -eq 1 ]; then 16234968b93SNicholas Fraser test_record wine ${ex_pe} 16334968b93SNicholas Fraserfi 16478b2c50cSJiri Olsa 16578b2c50cSJiri Olsa# cleanup 16678b2c50cSJiri Olsarm ${ex_sha1} ${ex_md5} 16734968b93SNicholas Fraserif [ ${run_pe} -eq 1 ]; then 16834968b93SNicholas Fraser rm -r ${wineprefix} 16934968b93SNicholas Fraserfi 17078b2c50cSJiri Olsa 17178b2c50cSJiri Olsaexit ${err} 172