buildid.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/sh
  2. # build id cache operations
  3. # SPDX-License-Identifier: GPL-2.0
  4. # skip if there's no readelf
  5. if ! [ -x "$(command -v readelf)" ]; then
  6. echo "failed: no readelf, install binutils"
  7. exit 2
  8. fi
  9. # skip if there's no compiler
  10. if ! [ -x "$(command -v cc)" ]; then
  11. echo "failed: no compiler, install gcc"
  12. exit 2
  13. fi
  14. # check what we need to test windows binaries
  15. add_pe=1
  16. run_pe=1
  17. if ! perf version --build-options | grep -q 'libbfd: .* on '; then
  18. echo "WARNING: perf not built with libbfd. PE binaries will not be tested."
  19. add_pe=0
  20. run_pe=0
  21. fi
  22. if ! which wine > /dev/null; then
  23. echo "WARNING: wine not found. PE binaries will not be run."
  24. run_pe=0
  25. fi
  26. # set up wine
  27. if [ ${run_pe} -eq 1 ]; then
  28. wineprefix=$(mktemp -d /tmp/perf.wineprefix.XXX)
  29. export WINEPREFIX=${wineprefix}
  30. # clear display variables to prevent wine from popping up dialogs
  31. unset DISPLAY
  32. unset WAYLAND_DISPLAY
  33. fi
  34. ex_md5=$(mktemp /tmp/perf.ex.MD5.XXX)
  35. ex_sha1=$(mktemp /tmp/perf.ex.SHA1.XXX)
  36. ex_pe=$(dirname $0)/../pe-file.exe
  37. echo 'int main(void) { return 0; }' | cc -Wl,--build-id=sha1 -o ${ex_sha1} -x c -
  38. echo 'int main(void) { return 0; }' | cc -Wl,--build-id=md5 -o ${ex_md5} -x c -
  39. echo "test binaries: ${ex_sha1} ${ex_md5} ${ex_pe}"
  40. check()
  41. {
  42. case $1 in
  43. *.exe)
  44. # We don't have a tool that can pull a nicely formatted build-id out of
  45. # a PE file, but we can extract the whole section with objcopy and
  46. # format it ourselves. The .buildid section is a Debug Directory
  47. # containing a CodeView entry:
  48. # https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#debug-directory-image-only
  49. # https://github.com/dotnet/runtime/blob/da94c022576a5c3bbc0e896f006565905eb137f9/docs/design/specs/PE-COFF.md
  50. # The build-id starts at byte 33 and must be rearranged into a GUID.
  51. id=`objcopy -O binary --only-section=.buildid $1 /dev/stdout | \
  52. cut -c 33-48 | hexdump -ve '/1 "%02x"' | \
  53. sed 's@^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(.*\)0a$@\4\3\2\1\6\5\8\7\9@'`
  54. ;;
  55. *)
  56. id=`readelf -n ${1} 2>/dev/null | grep 'Build ID' | awk '{print $3}'`
  57. ;;
  58. esac
  59. echo "build id: ${id}"
  60. id_file=${id#??}
  61. id_dir=${id%$id_file}
  62. link=$build_id_dir/.build-id/$id_dir/$id_file
  63. echo "link: ${link}"
  64. if [ ! -h $link ]; then
  65. echo "failed: link ${link} does not exist"
  66. exit 1
  67. fi
  68. file=${build_id_dir}/.build-id/$id_dir/`readlink ${link}`/elf
  69. echo "file: ${file}"
  70. # Check for file permission of original file
  71. # in case of pe-file.exe file
  72. echo $1 | grep ".exe"
  73. if [ $? -eq 0 ]; then
  74. if [ -x $1 ] && [ ! -x $file ]; then
  75. echo "failed: file ${file} executable does not exist"
  76. exit 1
  77. fi
  78. if [ ! -x $file ] && [ ! -e $file ]; then
  79. echo "failed: file ${file} does not exist"
  80. exit 1
  81. fi
  82. elif [ ! -x $file ]; then
  83. echo "failed: file ${file} does not exist"
  84. exit 1
  85. fi
  86. diff ${file} ${1}
  87. if [ $? -ne 0 ]; then
  88. echo "failed: ${file} do not match"
  89. exit 1
  90. fi
  91. ${perf} buildid-cache -l | grep ${id}
  92. if [ $? -ne 0 ]; then
  93. echo "failed: ${id} is not reported by \"perf buildid-cache -l\""
  94. exit 1
  95. fi
  96. echo "OK for ${1}"
  97. }
  98. test_add()
  99. {
  100. build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
  101. perf="perf --buildid-dir ${build_id_dir}"
  102. ${perf} buildid-cache -v -a ${1}
  103. if [ $? -ne 0 ]; then
  104. echo "failed: add ${1} to build id cache"
  105. exit 1
  106. fi
  107. check ${1}
  108. rm -rf ${build_id_dir}
  109. }
  110. test_record()
  111. {
  112. data=$(mktemp /tmp/perf.data.XXX)
  113. build_id_dir=$(mktemp -d /tmp/perf.debug.XXX)
  114. log_out=$(mktemp /tmp/perf.log.out.XXX)
  115. log_err=$(mktemp /tmp/perf.log.err.XXX)
  116. perf="perf --buildid-dir ${build_id_dir}"
  117. echo "running: perf record $*"
  118. ${perf} record --buildid-all -o ${data} "$@" 1>${log_out} 2>${log_err}
  119. if [ $? -ne 0 ]; then
  120. echo "failed: record $*"
  121. echo "see log: ${log_err}"
  122. exit 1
  123. fi
  124. args="$*"
  125. check ${args##* }
  126. rm -f ${log_out} ${log_err}
  127. rm -rf ${build_id_dir}
  128. rm -rf ${data}
  129. }
  130. # add binaries manual via perf buildid-cache -a
  131. test_add ${ex_sha1}
  132. test_add ${ex_md5}
  133. if [ ${add_pe} -eq 1 ]; then
  134. test_add ${ex_pe}
  135. fi
  136. # add binaries via perf record post processing
  137. test_record ${ex_sha1}
  138. test_record ${ex_md5}
  139. if [ ${run_pe} -eq 1 ]; then
  140. test_record wine ${ex_pe}
  141. fi
  142. # cleanup
  143. rm ${ex_sha1} ${ex_md5}
  144. if [ ${run_pe} -eq 1 ]; then
  145. rm -r ${wineprefix}
  146. fi
  147. exit 0