1 #!/bin/sh 2 # build id cache operations 3 # SPDX-License-Identifier: GPL-2.0 4 5 # skip if there's no readelf 6 if ! [ -x "$(command -v readelf)" ]; then 7 echo "failed: no readelf, install binutils" 8 exit 2 9 fi 10 11 # skip if there's no compiler 12 if ! [ -x "$(command -v cc)" ]; then 13 echo "failed: no compiler, install gcc" 14 exit 2 15 fi 16 17 # check what we need to test windows binaries 18 add_pe=1 19 run_pe=1 20 if ! 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 24 fi 25 if ! which wine > /dev/null; then 26 echo "WARNING: wine not found. PE binaries will not be run." 27 run_pe=0 28 fi 29 30 # set up wine 31 if [ ${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 37 fi 38 39 ex_md5=$(mktemp /tmp/perf.ex.MD5.XXX) 40 ex_sha1=$(mktemp /tmp/perf.ex.SHA1.XXX) 41 ex_pe=$(dirname $0)/../pe-file.exe 42 43 echo 'int main(void) { return 0; }' | cc -Wl,--build-id=sha1 -o ${ex_sha1} -x c - 44 echo 'int main(void) { return 0; }' | cc -Wl,--build-id=md5 -o ${ex_md5} -x c - 45 46 echo "test binaries: ${ex_sha1} ${ex_md5} ${ex_pe}" 47 48 check() 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 id_file=${id#??} 70 id_dir=${id%$id_file} 71 link=$build_id_dir/.build-id/$id_dir/$id_file 72 echo "link: ${link}" 73 74 if [ ! -h $link ]; then 75 echo "failed: link ${link} does not exist" 76 exit 1 77 fi 78 79 file=${build_id_dir}/.build-id/$id_dir/`readlink ${link}`/elf 80 echo "file: ${file}" 81 82 # Check for file permission of original file 83 # in case of pe-file.exe file 84 echo $1 | grep ".exe" 85 if [ $? -eq 0 ]; then 86 if [ -x $1 ] && [ ! -x $file ]; then 87 echo "failed: file ${file} executable does not exist" 88 exit 1 89 fi 90 91 if [ ! -x $file ] && [ ! -e $file ]; then 92 echo "failed: file ${file} does not exist" 93 exit 1 94 fi 95 elif [ ! -x $file ]; then 96 echo "failed: file ${file} does not exist" 97 exit 1 98 fi 99 100 diff ${file} ${1} 101 if [ $? -ne 0 ]; then 102 echo "failed: ${file} do not match" 103 exit 1 104 fi 105 106 ${perf} buildid-cache -l | grep ${id} 107 if [ $? -ne 0 ]; then 108 echo "failed: ${id} is not reported by \"perf buildid-cache -l\"" 109 exit 1 110 fi 111 112 echo "OK for ${1}" 113 } 114 115 test_add() 116 { 117 build_id_dir=$(mktemp -d /tmp/perf.debug.XXX) 118 perf="perf --buildid-dir ${build_id_dir}" 119 120 ${perf} buildid-cache -v -a ${1} 121 if [ $? -ne 0 ]; then 122 echo "failed: add ${1} to build id cache" 123 exit 1 124 fi 125 126 check ${1} 127 128 rm -rf ${build_id_dir} 129 } 130 131 test_record() 132 { 133 data=$(mktemp /tmp/perf.data.XXX) 134 build_id_dir=$(mktemp -d /tmp/perf.debug.XXX) 135 log_out=$(mktemp /tmp/perf.log.out.XXX) 136 log_err=$(mktemp /tmp/perf.log.err.XXX) 137 perf="perf --buildid-dir ${build_id_dir}" 138 139 echo "running: perf record $*" 140 ${perf} record --buildid-all -o ${data} "$@" 1>${log_out} 2>${log_err} 141 if [ $? -ne 0 ]; then 142 echo "failed: record $*" 143 echo "see log: ${log_err}" 144 exit 1 145 fi 146 147 args="$*" 148 check ${args##* } 149 150 rm -f ${log_out} ${log_err} 151 rm -rf ${build_id_dir} 152 rm -rf ${data} 153 } 154 155 # add binaries manual via perf buildid-cache -a 156 test_add ${ex_sha1} 157 test_add ${ex_md5} 158 if [ ${add_pe} -eq 1 ]; then 159 test_add ${ex_pe} 160 fi 161 162 # add binaries via perf record post processing 163 test_record ${ex_sha1} 164 test_record ${ex_md5} 165 if [ ${run_pe} -eq 1 ]; then 166 test_record wine ${ex_pe} 167 fi 168 169 # cleanup 170 rm ${ex_sha1} ${ex_md5} 171 if [ ${run_pe} -eq 1 ]; then 172 rm -r ${wineprefix} 173 fi 174 175 exit 0
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.