buildid.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #!/bin/bash
  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. build_id_dir=
  35. ex_source=$(mktemp /tmp/perf_buildid_test.ex.XXX.c)
  36. ex_md5=$(mktemp /tmp/perf_buildid_test.ex.MD5.XXX)
  37. ex_sha1=$(mktemp /tmp/perf_buildid_test.ex.SHA1.XXX)
  38. ex_pe=$(dirname $0)/../pe-file.exe
  39. data=$(mktemp /tmp/perf_buildid_test.data.XXX)
  40. log_out=$(mktemp /tmp/perf_buildid_test.log.out.XXX)
  41. log_err=$(mktemp /tmp/perf_buildid_test.log.err.XXX)
  42. cleanup() {
  43. rm -f ${ex_source} ${ex_md5} ${ex_sha1} ${data} ${log_out} ${log_err}
  44. if [ ${run_pe} -eq 1 ]; then
  45. rm -r ${wineprefix}
  46. fi
  47. if [ -d ${build_id_dir} ]; then
  48. rm -rf ${build_id_dir}
  49. fi
  50. trap - EXIT TERM INT
  51. }
  52. trap_cleanup() {
  53. echo "Unexpected signal in ${FUNCNAME[1]}"
  54. cleanup
  55. exit 1
  56. }
  57. trap trap_cleanup EXIT TERM INT
  58. # Test program based on the noploop workload.
  59. cat <<EOF > ${ex_source}
  60. #include <stdlib.h>
  61. #include <signal.h>
  62. #include <unistd.h>
  63. static volatile sig_atomic_t done;
  64. static void sighandler(int sig)
  65. {
  66. (void)sig;
  67. done = 1;
  68. }
  69. int main(int argc, const char **argv)
  70. {
  71. int sec = 1;
  72. if (argc > 1)
  73. sec = atoi(argv[1]);
  74. signal(SIGINT, sighandler);
  75. signal(SIGALRM, sighandler);
  76. alarm(sec);
  77. while (!done)
  78. continue;
  79. return 0;
  80. }
  81. EOF
  82. cc -Wl,--build-id=sha1 ${ex_source} -o ${ex_sha1} -x c -
  83. cc -Wl,--build-id=md5 ${ex_source} -o ${ex_md5} -x c -
  84. echo "test binaries: ${ex_sha1} ${ex_md5} ${ex_pe}"
  85. get_build_id()
  86. {
  87. case $1 in
  88. *.exe)
  89. # We don't have a tool that can pull a nicely formatted build-id out of
  90. # a PE file, but we can extract the whole section with objcopy and
  91. # format it ourselves. The .buildid section is a Debug Directory
  92. # containing a CodeView entry:
  93. # https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#debug-directory-image-only
  94. # https://github.com/dotnet/runtime/blob/da94c022576a5c3bbc0e896f006565905eb137f9/docs/design/specs/PE-COFF.md
  95. # The build-id starts at byte 33 and must be rearranged into a GUID.
  96. id=`objcopy -O binary --only-section=.buildid $1 /dev/stdout | \
  97. cut -c 33-48 | hexdump -ve '/1 "%02x"' | \
  98. sed 's@^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(.*\)0a$@\4\3\2\1\6\5\8\7\9@'`
  99. ;;
  100. *)
  101. id=`readelf -n ${1} 2>/dev/null | grep 'Build ID' | awk '{print $3}'`
  102. ;;
  103. esac
  104. echo ${id}
  105. }
  106. check()
  107. {
  108. file=$1
  109. perf_data=$2
  110. id=$(get_build_id $file)
  111. echo "build id: ${id}"
  112. id_file=${id#??}
  113. id_dir=${id%$id_file}
  114. link=$build_id_dir/.build-id/$id_dir/$id_file
  115. echo "link: ${link}"
  116. if [ ! -h $link ]; then
  117. echo "failed: link ${link} does not exist"
  118. exit 1
  119. fi
  120. cached_file=${build_id_dir}/.build-id/$id_dir/`readlink ${link}`/elf
  121. echo "file: ${cached_file}"
  122. # Check for file permission of original file
  123. # in case of pe-file.exe file
  124. echo $1 | grep ".exe"
  125. if [ $? -eq 0 ]; then
  126. if [ -x $1 ] && [ ! -x $cached_file ]; then
  127. echo "failed: file ${cached_file} executable does not exist"
  128. exit 1
  129. fi
  130. if [ ! -x $cached_file ] && [ ! -e $cached_file ]; then
  131. echo "failed: file ${cached_file} does not exist"
  132. exit 1
  133. fi
  134. elif [ ! -x $cached_file ]; then
  135. echo "failed: file ${cached_file} does not exist"
  136. exit 1
  137. fi
  138. diff ${cached_file} ${1}
  139. if [ $? -ne 0 ]; then
  140. echo "failed: ${cached_file} do not match"
  141. exit 1
  142. fi
  143. ${perf} buildid-cache -l | grep -q ${id}
  144. if [ $? -ne 0 ]; then
  145. echo "failed: ${id} is not reported by \"perf buildid-cache -l\""
  146. exit 1
  147. fi
  148. if [ -n "${perf_data}" ]; then
  149. ${perf} buildid-list -i ${perf_data} | grep -q ${id}
  150. if [ $? -ne 0 ]; then
  151. echo "failed: ${id} is not reported by \"perf buildid-list -i ${perf_data}\""
  152. exit 1
  153. fi
  154. fi
  155. echo "OK for ${1}"
  156. }
  157. test_add()
  158. {
  159. build_id_dir=$(mktemp -d /tmp/perf_buildid_test.debug.XXX)
  160. perf="perf --buildid-dir ${build_id_dir}"
  161. ${perf} buildid-cache -v -a ${1}
  162. if [ $? -ne 0 ]; then
  163. echo "failed: add ${1} to build id cache"
  164. exit 1
  165. fi
  166. check ${1}
  167. rm -rf ${build_id_dir}
  168. }
  169. test_remove()
  170. {
  171. build_id_dir=$(mktemp -d /tmp/perf_buildid_test.debug.XXX)
  172. perf="perf --buildid-dir ${build_id_dir}"
  173. ${perf} buildid-cache -v -a ${1}
  174. if [ $? -ne 0 ]; then
  175. echo "failed: add ${1} to build id cache"
  176. exit 1
  177. fi
  178. id=$(get_build_id ${1})
  179. if ! ${perf} buildid-cache -l | grep -q ${id}; then
  180. echo "failed: ${id} not in cache"
  181. exit 1
  182. fi
  183. ${perf} buildid-cache -v -r ${1}
  184. if [ $? -ne 0 ]; then
  185. echo "failed: remove ${id} from build id cache"
  186. exit 1
  187. fi
  188. if ${perf} buildid-cache -l | grep -q ${id}; then
  189. echo "failed: ${id} still in cache after remove"
  190. exit 1
  191. fi
  192. echo "remove: OK"
  193. rm -rf ${build_id_dir}
  194. }
  195. test_purge()
  196. {
  197. build_id_dir=$(mktemp -d /tmp/perf_buildid_test.debug.XXX)
  198. perf="perf --buildid-dir ${build_id_dir}"
  199. id1=$(get_build_id ${ex_sha1})
  200. ${perf} buildid-cache -v -a ${ex_sha1}
  201. if ! ${perf} buildid-cache -l | grep -q ${id1}; then
  202. echo "failed: ${id1} not in cache"
  203. exit 1
  204. fi
  205. id2=$(get_build_id ${ex_md5})
  206. ${perf} buildid-cache -v -a ${ex_md5}
  207. if ! ${perf} buildid-cache -l | grep -q ${id2}; then
  208. echo "failed: ${id2} not in cache"
  209. exit 1
  210. fi
  211. # Purge by path
  212. ${perf} buildid-cache -v -p ${ex_sha1}
  213. if [ $? -ne 0 ]; then
  214. echo "failed: purge build id cache of ${ex_sha1}"
  215. exit 1
  216. fi
  217. ${perf} buildid-cache -v -p ${ex_md5}
  218. if [ $? -ne 0 ]; then
  219. echo "failed: purge build id cache of ${ex_md5}"
  220. exit 1
  221. fi
  222. # Verify both are gone
  223. if ${perf} buildid-cache -l | grep -q ${id1}; then
  224. echo "failed: ${id1} still in cache after purge"
  225. exit 1
  226. fi
  227. if ${perf} buildid-cache -l | grep -q ${id2}; then
  228. echo "failed: ${id2} still in cache after purge"
  229. exit 1
  230. fi
  231. echo "purge: OK"
  232. rm -rf ${build_id_dir}
  233. }
  234. test_record()
  235. {
  236. build_id_dir=$(mktemp -d /tmp/perf_buildid_test.debug.XXX)
  237. perf="perf --buildid-dir ${build_id_dir}"
  238. echo "running: perf record $*"
  239. ${perf} record --buildid-all -o ${data} "$@" 1>${log_out} 2>${log_err}
  240. if [ $? -ne 0 ]; then
  241. echo "failed: record $*"
  242. echo "see log: ${log_err}"
  243. exit 1
  244. fi
  245. args="$*"
  246. check ${args##* } ${data}
  247. rm -f ${log_out} ${log_err}
  248. rm -rf ${build_id_dir}
  249. rm -rf ${data}
  250. }
  251. # add binaries manual via perf buildid-cache -a
  252. test_add ${ex_sha1}
  253. test_add ${ex_md5}
  254. if [ ${add_pe} -eq 1 ]; then
  255. test_add ${ex_pe}
  256. fi
  257. # add binaries via perf record post processing
  258. test_record ${ex_sha1}
  259. test_record ${ex_md5}
  260. if [ ${run_pe} -eq 1 ]; then
  261. test_record wine ${ex_pe}
  262. fi
  263. # remove binaries manually via perf buildid-cache -r
  264. test_remove ${ex_sha1}
  265. test_remove ${ex_md5}
  266. if [ ${add_pe} -eq 1 ]; then
  267. test_remove ${ex_pe}
  268. fi
  269. # purge binaries manually via perf buildid-cache -p
  270. test_purge
  271. cleanup
  272. exit 0