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 69f1942108SAthira Rajeev id_file=${id#??} 70f1942108SAthira Rajeev id_dir=${id%$id_file} 71f1942108SAthira Rajeev link=$build_id_dir/.build-id/$id_dir/$id_file 7278b2c50cSJiri Olsa echo "link: ${link}" 7378b2c50cSJiri Olsa 7478b2c50cSJiri Olsa if [ ! -h $link ]; then 7578b2c50cSJiri Olsa echo "failed: link ${link} does not exist" 7678b2c50cSJiri Olsa exit 1 7778b2c50cSJiri Olsa fi 7878b2c50cSJiri Olsa 79f1942108SAthira Rajeev file=${build_id_dir}/.build-id/$id_dir/`readlink ${link}`/elf 8078b2c50cSJiri Olsa echo "file: ${file}" 8178b2c50cSJiri Olsa 823d9c07c4SAthira Rajeev # Check for file permission of original file 833d9c07c4SAthira Rajeev # in case of pe-file.exe file 843d9c07c4SAthira Rajeev echo $1 | grep ".exe" 853d9c07c4SAthira Rajeev if [ $? -eq 0 ]; then 86*ed46a999SSamir Mulani if [ -x $1 ] && [ ! -x $file ]; then 873d9c07c4SAthira Rajeev echo "failed: file ${file} executable does not exist" 883d9c07c4SAthira Rajeev exit 1 893d9c07c4SAthira Rajeev fi 903d9c07c4SAthira Rajeev 91*ed46a999SSamir Mulani if [ ! -x $file ] && [ ! -e $file ]; then 923d9c07c4SAthira Rajeev echo "failed: file ${file} does not exist" 933d9c07c4SAthira Rajeev exit 1 943d9c07c4SAthira Rajeev fi 953d9c07c4SAthira Rajeev elif [ ! -x $file ]; then 9678b2c50cSJiri Olsa echo "failed: file ${file} does not exist" 9778b2c50cSJiri Olsa exit 1 9878b2c50cSJiri Olsa fi 9978b2c50cSJiri Olsa 10078b2c50cSJiri Olsa diff ${file} ${1} 10178b2c50cSJiri Olsa if [ $? -ne 0 ]; then 10278b2c50cSJiri Olsa echo "failed: ${file} do not match" 10378b2c50cSJiri Olsa exit 1 10478b2c50cSJiri Olsa fi 10578b2c50cSJiri Olsa 10634968b93SNicholas Fraser ${perf} buildid-cache -l | grep ${id} 107206236d3SNicholas Fraser if [ $? -ne 0 ]; then 108206236d3SNicholas Fraser echo "failed: ${id} is not reported by \"perf buildid-cache -l\"" 109206236d3SNicholas Fraser exit 1 110206236d3SNicholas Fraser fi 111206236d3SNicholas Fraser 11278b2c50cSJiri Olsa echo "OK for ${1}" 11378b2c50cSJiri Olsa} 11478b2c50cSJiri Olsa 11578b2c50cSJiri Olsatest_add() 11678b2c50cSJiri Olsa{ 11778b2c50cSJiri Olsa build_id_dir=$(mktemp -d /tmp/perf.debug.XXX) 11878b2c50cSJiri Olsa perf="perf --buildid-dir ${build_id_dir}" 11978b2c50cSJiri Olsa 12078b2c50cSJiri Olsa ${perf} buildid-cache -v -a ${1} 12178b2c50cSJiri Olsa if [ $? -ne 0 ]; then 12278b2c50cSJiri Olsa echo "failed: add ${1} to build id cache" 12378b2c50cSJiri Olsa exit 1 12478b2c50cSJiri Olsa fi 12578b2c50cSJiri Olsa 12678b2c50cSJiri Olsa check ${1} 12778b2c50cSJiri Olsa 12878b2c50cSJiri Olsa rm -rf ${build_id_dir} 12978b2c50cSJiri Olsa} 13078b2c50cSJiri Olsa 13178b2c50cSJiri Olsatest_record() 13278b2c50cSJiri Olsa{ 13378b2c50cSJiri Olsa data=$(mktemp /tmp/perf.data.XXX) 13478b2c50cSJiri Olsa build_id_dir=$(mktemp -d /tmp/perf.debug.XXX) 135f1942108SAthira Rajeev log_out=$(mktemp /tmp/perf.log.out.XXX) 136f1942108SAthira Rajeev log_err=$(mktemp /tmp/perf.log.err.XXX) 13778b2c50cSJiri Olsa perf="perf --buildid-dir ${build_id_dir}" 13878b2c50cSJiri Olsa 139*ed46a999SSamir Mulani echo "running: perf record $*" 140*ed46a999SSamir Mulani ${perf} record --buildid-all -o ${data} "$@" 1>${log_out} 2>${log_err} 14178b2c50cSJiri Olsa if [ $? -ne 0 ]; then 142*ed46a999SSamir Mulani echo "failed: record $*" 143f1942108SAthira Rajeev echo "see log: ${log_err}" 14478b2c50cSJiri Olsa exit 1 14578b2c50cSJiri Olsa fi 14678b2c50cSJiri Olsa 147f1942108SAthira Rajeev args="$*" 148f1942108SAthira Rajeev check ${args##* } 14978b2c50cSJiri Olsa 150f1942108SAthira Rajeev rm -f ${log_out} ${log_err} 15178b2c50cSJiri Olsa rm -rf ${build_id_dir} 15278b2c50cSJiri Olsa rm -rf ${data} 15378b2c50cSJiri Olsa} 15478b2c50cSJiri Olsa 15578b2c50cSJiri Olsa# add binaries manual via perf buildid-cache -a 15678b2c50cSJiri Olsatest_add ${ex_sha1} 15778b2c50cSJiri Olsatest_add ${ex_md5} 15834968b93SNicholas Fraserif [ ${add_pe} -eq 1 ]; then 15934968b93SNicholas Fraser test_add ${ex_pe} 16034968b93SNicholas Fraserfi 16178b2c50cSJiri Olsa 16278b2c50cSJiri Olsa# add binaries via perf record post processing 16378b2c50cSJiri Olsatest_record ${ex_sha1} 16478b2c50cSJiri Olsatest_record ${ex_md5} 16534968b93SNicholas Fraserif [ ${run_pe} -eq 1 ]; then 16634968b93SNicholas Fraser test_record wine ${ex_pe} 16734968b93SNicholas Fraserfi 16878b2c50cSJiri Olsa 16978b2c50cSJiri Olsa# cleanup 17078b2c50cSJiri Olsarm ${ex_sha1} ${ex_md5} 17134968b93SNicholas Fraserif [ ${run_pe} -eq 1 ]; then 17234968b93SNicholas Fraser rm -r ${wineprefix} 17334968b93SNicholas Fraserfi 17478b2c50cSJiri Olsa 175*ed46a999SSamir Mulaniexit 0 176