2 # build id cache operations
3 # SPDX-License-Identifier: GPL-2.0
5 # skip if there's no readelf
6 if ! [ -x "$(command -v readelf)" ]; then
7 echo "failed: no readelf, install binutils"
11 # skip if there's no compiler
12 if ! [ -x "$(command -v cc)" ]; then
13 echo "failed: no compiler, install gcc"
17 # check what we need to test windows binaries
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."
25 if ! which wine
> /dev
/null
; then
26 echo "WARNING: wine not found. PE binaries will not be run."
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
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
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
-
46 echo "test binaries: ${ex_sha1} ${ex_md5} ${ex_pe}"
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@'`
64 id
=`readelf -n ${1} 2>/dev/null | grep 'Build ID' | awk '{print $3}'`
67 echo "build id: ${id}"
71 link
=$build_id_dir/.build-id
/$id_dir/$id_file
74 if [ ! -h $link ]; then
75 echo "failed: link ${link} does not exist"
79 file=${build_id_dir}/.build-id
/$id_dir/`readlink ${link}`/elf
82 # Check for file permission of original file
83 # in case of pe-file.exe file
86 if [ -x $1 ] && [ ! -x $file ]; then
87 echo "failed: file ${file} executable does not exist"
91 if [ ! -x $file ] && [ ! -e $file ]; then
92 echo "failed: file ${file} does not exist"
95 elif [ ! -x $file ]; then
96 echo "failed: file ${file} does not exist"
101 if [ $?
-ne 0 ]; then
102 echo "failed: ${file} do not match"
106 ${perf} buildid-cache
-l |
grep ${id}
107 if [ $?
-ne 0 ]; then
108 echo "failed: ${id} is not reported by \"perf buildid-cache -l\""
117 build_id_dir
=$
(mktemp
-d /tmp
/perf.debug.XXX
)
118 perf
="perf --buildid-dir ${build_id_dir}"
120 ${perf} buildid-cache
-v -a ${1}
121 if [ $?
-ne 0 ]; then
122 echo "failed: add ${1} to build id cache"
128 rm -rf ${build_id_dir}
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}"
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}"
150 rm -f ${log_out} ${log_err}
151 rm -rf ${build_id_dir}
155 # add binaries manual via perf buildid-cache -a
158 if [ ${add_pe} -eq 1 ]; then
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}
170 rm ${ex_sha1} ${ex_md5}
171 if [ ${run_pe} -eq 1 ]; then