1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# perf archive
4# Arnaldo Carvalho de Melo <acme@redhat.com>
5
6PERF_DATA=perf.data
7PERF_SYMBOLS=perf.symbols
8PERF_ALL=perf.all
9ALL=0
10UNPACK=0
11
12while [ $# -gt 0 ] ; do
13	if [ $1 == "--all" ]; then
14		ALL=1
15		shift
16	elif [ $1 == "--unpack" ]; then
17		UNPACK=1
18		shift
19	elif [ $1 == "--exclude-buildids" ]; then
20		EXCLUDE_BUILDIDS="$2"
21		if [ ! -e "$EXCLUDE_BUILDIDS" ]; then
22			echo "Provided exclude-buildids file $EXCLUDE_BUILDIDS does not exist"
23			exit 1
24		fi
25		shift 2
26	else
27		PERF_DATA=$1
28		UNPACK_TAR=$1
29		shift
30	fi
31done
32
33if [ $UNPACK -eq 1 ]; then
34	if [ ! -z "$UNPACK_TAR" ]; then					# tar given as an argument
35		if [ ! -e "$UNPACK_TAR" ]; then
36			echo "Provided file $UNPACK_TAR does not exist"
37			exit 1
38		fi
39		TARGET="$UNPACK_TAR"
40	else																# search for perf tar in the current directory
41		TARGET=`find . -regex "\./perf.*\.tar\.bz2"`
42		TARGET_NUM=`echo -n "$TARGET" | grep -c '^'`
43
44		if [ -z "$TARGET" ] || [ $TARGET_NUM -gt 1 ]; then
45			echo -e "Error: $TARGET_NUM files found for unpacking:\n$TARGET"
46			echo "Provide the requested file as an argument"
47			exit 1
48		else
49			echo "Found target file for unpacking: $TARGET"
50		fi
51	fi
52
53	if [[ "$TARGET" =~ (\./)?$PERF_ALL.*.tar.bz2 ]]; then				# perf tar generated by --all option
54		TAR_CONTENTS=`tar tvf "$TARGET" | tr -s " " | cut -d " " -f 6`
55		VALID_TAR=`echo "$TAR_CONTENTS" | grep "$PERF_SYMBOLS.tar.bz2" | wc -l`		# check if it contains a sub-tar perf.symbols
56		if [ $VALID_TAR -ne 1 ]; then
57			echo "Error: $TARGET file is not valid (contains zero or multiple sub-tar files with debug symbols)"
58			exit 1
59		fi
60
61		INTERSECT=`comm -12 <(ls) <(echo "$TAR_CONTENTS") | tr "\n" " "`	# check for overwriting
62		if [ ! -z "$INTERSECT" ]; then										# prompt if file(s) already exist in the current directory
63			echo "File(s) ${INTERSECT::-1} already exist in the current directory."
64			while true; do
65				read -p 'Do you wish to overwrite them? ' yn
66				case $yn in
67					[Yy]* ) break;;
68					[Nn]* ) exit 1;;
69					* ) echo "Please answer yes or no.";;
70				esac
71			done
72		fi
73
74		# unzip the perf.data file in the current working directory	and debug symbols in ~/.debug directory
75		tar xvf $TARGET && tar xvf $PERF_SYMBOLS.tar.bz2 -C ~/.debug
76
77	else																# perf tar generated by perf archive (contains only debug symbols)
78		tar xvf $TARGET -C ~/.debug
79	fi
80	exit 0
81fi
82
83#
84# PERF_BUILDID_DIR environment variable set by perf
85# path to buildid directory, default to $HOME/.debug
86#
87if [ -z $PERF_BUILDID_DIR ]; then
88	PERF_BUILDID_DIR=~/.debug/
89else
90        # append / to make substitutions work
91        PERF_BUILDID_DIR=$PERF_BUILDID_DIR/
92fi
93
94BUILDIDS=$(mktemp /tmp/perf-archive-buildids.XXXXXX)
95
96#
97# EXCLUDE_BUILDIDS is an optional file that contains build-ids to be excluded from the
98# archive. It is a list of build-ids, one per line, without any leading or trailing spaces.
99# If the file is empty, all build-ids will be included in the archive. To create a exclude-
100# buildids file, you can use the following command:
101# 	perf buildid-list -i perf.data --with-hits | grep -v "^ " > exclude_buildids.txt
102# You can edit the file to remove the lines that you want to keep in the archive, then:
103# 	perf archive --exclude-buildids exclude_buildids.txt
104#
105if [ -s "$EXCLUDE_BUILDIDS" ]; then
106	perf buildid-list -i $PERF_DATA --with-hits | grep -v "^ " | grep -Fv -f $EXCLUDE_BUILDIDS > $BUILDIDS
107	if [ ! -s "$BUILDIDS" ] ; then
108		echo "perf archive: no build-ids found after applying exclude-buildids file"
109		rm $BUILDIDS || true
110		exit 1
111	fi
112else
113	perf buildid-list -i $PERF_DATA --with-hits | grep -v "^ " > $BUILDIDS
114	if [ ! -s "$BUILDIDS" ] ; then
115		echo "perf archive: no build-ids found"
116		rm $BUILDIDS || true
117		exit 1
118	fi
119fi
120
121MANIFEST=$(mktemp /tmp/perf-archive-manifest.XXXXXX)
122PERF_BUILDID_LINKDIR=$(readlink -f $PERF_BUILDID_DIR)/
123
124cut -d ' ' -f 1 $BUILDIDS | \
125while read build_id ; do
126	linkname=$PERF_BUILDID_DIR.build-id/${build_id:0:2}/${build_id:2}
127	filename=$(readlink -f $linkname)
128	echo ${linkname#$PERF_BUILDID_DIR} >> $MANIFEST
129	echo ${filename#$PERF_BUILDID_LINKDIR} >> $MANIFEST
130done
131
132if [ $ALL -eq 1 ]; then						# pack perf.data file together with tar containing debug symbols
133	HOSTNAME=$(hostname)
134	DATE=$(date '+%Y%m%d-%H%M%S')
135	tar cjf $PERF_SYMBOLS.tar.bz2 -C $PERF_BUILDID_DIR -T $MANIFEST
136	tar cjf	$PERF_ALL-$HOSTNAME-$DATE.tar.bz2 $PERF_DATA $PERF_SYMBOLS.tar.bz2
137	rm $PERF_SYMBOLS.tar.bz2 $MANIFEST $BUILDIDS || true
138else										# pack only the debug symbols
139	tar cjf $PERF_DATA.tar.bz2 -C $PERF_BUILDID_DIR -T $MANIFEST
140	rm $MANIFEST $BUILDIDS || true
141fi
142
143echo -e "Now please run:\n"
144echo -e "$ perf archive --unpack\n"
145echo "or unpack the tar manually wherever you need to run 'perf report' on."
146exit 0
147