brintos

brintos / linux-shallow public Read only

0
0
Text · 4.1 KiB · 3383ca3 Raw
176 lines · bash
1#!/bin/sh2# build id cache operations3# SPDX-License-Identifier: GPL-2.04 5# skip if there's no readelf6if ! [ -x "$(command -v readelf)" ]; then7	echo "failed: no readelf, install binutils"8	exit 29fi10 11# skip if there's no compiler12if ! [ -x "$(command -v cc)" ]; then13	echo "failed: no compiler, install gcc"14	exit 215fi16 17# check what we need to test windows binaries18add_pe=119run_pe=120if ! perf version --build-options | grep -q 'libbfd: .* on '; then21	echo "WARNING: perf not built with libbfd. PE binaries will not be tested."22	add_pe=023	run_pe=024fi25if ! which wine > /dev/null; then26	echo "WARNING: wine not found. PE binaries will not be run."27	run_pe=028fi29 30# set up wine31if [ ${run_pe} -eq 1 ]; then32	wineprefix=$(mktemp -d /tmp/perf.wineprefix.XXX)33	export WINEPREFIX=${wineprefix}34	# clear display variables to prevent wine from popping up dialogs35	unset DISPLAY36	unset WAYLAND_DISPLAY37fi38 39ex_md5=$(mktemp /tmp/perf.ex.MD5.XXX)40ex_sha1=$(mktemp /tmp/perf.ex.SHA1.XXX)41ex_pe=$(dirname $0)/../pe-file.exe42 43echo 'int main(void) { return 0; }' | cc -Wl,--build-id=sha1 -o ${ex_sha1} -x c -44echo 'int main(void) { return 0; }' | cc -Wl,--build-id=md5 -o ${ex_md5} -x c -45 46echo "test binaries: ${ex_sha1} ${ex_md5} ${ex_pe}"47 48check()49{50	case $1 in51	*.exe)52		# We don't have a tool that can pull a nicely formatted build-id out of53		# a PE file, but we can extract the whole section with objcopy and54		# format it ourselves. The .buildid section is a Debug Directory55		# containing a CodeView entry:56		#     https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#debug-directory-image-only57		#     https://github.com/dotnet/runtime/blob/da94c022576a5c3bbc0e896f006565905eb137f9/docs/design/specs/PE-COFF.md58		# 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	esac67	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_file72	echo "link: ${link}"73 74	if [ ! -h $link ]; then75		echo "failed: link ${link} does not exist"76		exit 177	fi78 79	file=${build_id_dir}/.build-id/$id_dir/`readlink ${link}`/elf80	echo "file: ${file}"81 82	# Check for file permission of original file83	# in case of pe-file.exe file84	echo $1 | grep ".exe"85	if [ $? -eq 0 ]; then86		if [ -x $1 ] && [ ! -x $file ]; then87			echo "failed: file ${file} executable does not exist"88			exit 189		fi90 91		if [ ! -x $file ] && [ ! -e $file ]; then92			echo "failed: file ${file} does not exist"93			exit 194		fi95	elif [ ! -x $file ]; then96		echo "failed: file ${file} does not exist"97		exit 198	fi99 100	diff ${file} ${1}101	if [ $? -ne 0 ]; then102		echo "failed: ${file} do not match"103		exit 1104	fi105 106	${perf} buildid-cache -l | grep ${id}107	if [ $? -ne 0 ]; then108		echo "failed: ${id} is not reported by \"perf buildid-cache -l\""109		exit 1110	fi111 112	echo "OK for ${1}"113}114 115test_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 ]; then122		echo "failed: add ${1} to build id cache"123		exit 1124	fi125 126	check ${1}127 128	rm -rf ${build_id_dir}129}130 131test_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 ]; then142		echo "failed: record $*"143		echo "see log: ${log_err}"144		exit 1145	fi146 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 -a156test_add ${ex_sha1}157test_add ${ex_md5}158if [ ${add_pe} -eq 1 ]; then159	test_add ${ex_pe}160fi161 162# add binaries via perf record post processing163test_record ${ex_sha1}164test_record ${ex_md5}165if [ ${run_pe} -eq 1 ]; then166	test_record wine ${ex_pe}167fi168 169# cleanup170rm ${ex_sha1} ${ex_md5}171if [ ${run_pe} -eq 1 ]; then172	rm -r ${wineprefix}173fi174 175exit 0176