Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/sh
0002 # build id cache operations
0003 # SPDX-License-Identifier: GPL-2.0
0004 
0005 # skip if there's no readelf
0006 if ! [ -x "$(command -v readelf)" ]; then
0007         echo "failed: no readelf, install binutils"
0008         exit 2
0009 fi
0010 
0011 # skip if there's no compiler
0012 if ! [ -x "$(command -v cc)" ]; then
0013         echo "failed: no compiler, install gcc"
0014         exit 2
0015 fi
0016 
0017 # check what we need to test windows binaries
0018 add_pe=1
0019 run_pe=1
0020 if ! perf version --build-options | grep -q 'libbfd: .* on '; then
0021         echo "WARNING: perf not built with libbfd. PE binaries will not be tested."
0022         add_pe=0
0023         run_pe=0
0024 fi
0025 if ! which wine > /dev/null; then
0026         echo "WARNING: wine not found. PE binaries will not be run."
0027         run_pe=0
0028 fi
0029 
0030 # set up wine
0031 if [ ${run_pe} -eq 1 ]; then
0032         wineprefix=$(mktemp -d /tmp/perf.wineprefix.XXX)
0033         export WINEPREFIX=${wineprefix}
0034         # clear display variables to prevent wine from popping up dialogs
0035         unset DISPLAY
0036         unset WAYLAND_DISPLAY
0037 fi
0038 
0039 ex_md5=$(mktemp /tmp/perf.ex.MD5.XXX)
0040 ex_sha1=$(mktemp /tmp/perf.ex.SHA1.XXX)
0041 ex_pe=$(dirname $0)/../pe-file.exe
0042 
0043 echo 'int main(void) { return 0; }' | cc -Wl,--build-id=sha1 -o ${ex_sha1} -x c -
0044 echo 'int main(void) { return 0; }' | cc -Wl,--build-id=md5 -o ${ex_md5} -x c -
0045 
0046 echo "test binaries: ${ex_sha1} ${ex_md5} ${ex_pe}"
0047 
0048 check()
0049 {
0050         case $1 in
0051         *.exe)
0052                 # We don't have a tool that can pull a nicely formatted build-id out of
0053                 # a PE file, but we can extract the whole section with objcopy and
0054                 # format it ourselves. The .buildid section is a Debug Directory
0055                 # containing a CodeView entry:
0056                 #     https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#debug-directory-image-only
0057                 #     https://github.com/dotnet/runtime/blob/da94c022576a5c3bbc0e896f006565905eb137f9/docs/design/specs/PE-COFF.md
0058                 # The build-id starts at byte 33 and must be rearranged into a GUID.
0059                 id=`objcopy -O binary --only-section=.buildid $1 /dev/stdout | \
0060                         cut -c 33-48 | hexdump -ve '/1 "%02x"' | \
0061                         sed 's@^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(.*\)0a$@\4\3\2\1\6\5\8\7\9@'`
0062                 ;;
0063         *)
0064                 id=`readelf -n ${1} 2>/dev/null | grep 'Build ID' | awk '{print $3}'`
0065                 ;;
0066         esac
0067         echo "build id: ${id}"
0068 
0069         link=${build_id_dir}/.build-id/${id:0:2}/${id:2}
0070         echo "link: ${link}"
0071 
0072         if [ ! -h $link ]; then
0073                 echo "failed: link ${link} does not exist"
0074                 exit 1
0075         fi
0076 
0077         file=${build_id_dir}/.build-id/${id:0:2}/`readlink ${link}`/elf
0078         echo "file: ${file}"
0079 
0080         if [ ! -x $file ]; then
0081                 echo "failed: file ${file} does not exist"
0082                 exit 1
0083         fi
0084 
0085         diff ${file} ${1}
0086         if [ $? -ne 0 ]; then
0087                 echo "failed: ${file} do not match"
0088                 exit 1
0089         fi
0090 
0091         ${perf} buildid-cache -l | grep ${id}
0092         if [ $? -ne 0 ]; then
0093                 echo "failed: ${id} is not reported by \"perf buildid-cache -l\""
0094                 exit 1
0095         fi
0096 
0097         echo "OK for ${1}"
0098 }
0099 
0100 test_add()
0101 {
0102         build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
0103         perf="perf --buildid-dir ${build_id_dir}"
0104 
0105         ${perf} buildid-cache -v -a ${1}
0106         if [ $? -ne 0 ]; then
0107                 echo "failed: add ${1} to build id cache"
0108                 exit 1
0109         fi
0110 
0111         check ${1}
0112 
0113         rm -rf ${build_id_dir}
0114 }
0115 
0116 test_record()
0117 {
0118         data=$(mktemp /tmp/perf.data.XXX)
0119         build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
0120         log=$(mktemp /tmp/perf.log.XXX)
0121         perf="perf --buildid-dir ${build_id_dir}"
0122 
0123         echo "running: perf record $@"
0124         ${perf} record --buildid-all -o ${data} $@ &> ${log}
0125         if [ $? -ne 0 ]; then
0126                 echo "failed: record $@"
0127                 echo "see log: ${log}"
0128                 exit 1
0129         fi
0130 
0131         check ${@: -1}
0132 
0133         rm -f ${log}
0134         rm -rf ${build_id_dir}
0135         rm -rf ${data}
0136 }
0137 
0138 # add binaries manual via perf buildid-cache -a
0139 test_add ${ex_sha1}
0140 test_add ${ex_md5}
0141 if [ ${add_pe} -eq 1 ]; then
0142         test_add ${ex_pe}
0143 fi
0144 
0145 # add binaries via perf record post processing
0146 test_record ${ex_sha1}
0147 test_record ${ex_md5}
0148 if [ ${run_pe} -eq 1 ]; then
0149         test_record wine ${ex_pe}
0150 fi
0151 
0152 # cleanup
0153 rm ${ex_sha1} ${ex_md5}
0154 if [ ${run_pe} -eq 1 ]; then
0155         rm -r ${wineprefix}
0156 fi
0157 
0158 exit ${err}