122 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03# perf archive4# Arnaldo Carvalho de Melo <acme@redhat.com>5 6PERF_DATA=perf.data7PERF_SYMBOLS=perf.symbols8PERF_ALL=perf.all9ALL=010UNPACK=011 12while [ $# -gt 0 ] ; do13 if [ $1 == "--all" ]; then14 ALL=115 shift16 elif [ $1 == "--unpack" ]; then17 UNPACK=118 shift19 else20 PERF_DATA=$121 UNPACK_TAR=$122 shift23 fi24done25 26if [ $UNPACK -eq 1 ]; then27 if [ ! -z "$UNPACK_TAR" ]; then # tar given as an argument28 if [ ! -e "$UNPACK_TAR" ]; then29 echo "Provided file $UNPACK_TAR does not exist"30 exit 131 fi32 TARGET="$UNPACK_TAR"33 else # search for perf tar in the current directory34 TARGET=`find . -regex "\./perf.*\.tar\.bz2"`35 TARGET_NUM=`echo -n "$TARGET" | grep -c '^'`36 37 if [ -z "$TARGET" ] || [ $TARGET_NUM -gt 1 ]; then38 echo -e "Error: $TARGET_NUM files found for unpacking:\n$TARGET"39 echo "Provide the requested file as an argument"40 exit 141 else42 echo "Found target file for unpacking: $TARGET"43 fi44 fi45 46 if [[ "$TARGET" =~ (\./)?$PERF_ALL.*.tar.bz2 ]]; then # perf tar generated by --all option47 TAR_CONTENTS=`tar tvf "$TARGET" | tr -s " " | cut -d " " -f 6`48 VALID_TAR=`echo "$TAR_CONTENTS" | grep "$PERF_SYMBOLS.tar.bz2" | wc -l` # check if it contains a sub-tar perf.symbols49 if [ $VALID_TAR -ne 1 ]; then50 echo "Error: $TARGET file is not valid (contains zero or multiple sub-tar files with debug symbols)"51 exit 152 fi53 54 INTERSECT=`comm -12 <(ls) <(echo "$TAR_CONTENTS") | tr "\n" " "` # check for overwriting55 if [ ! -z "$INTERSECT" ]; then # prompt if file(s) already exist in the current directory56 echo "File(s) ${INTERSECT::-1} already exist in the current directory."57 while true; do58 read -p 'Do you wish to overwrite them? ' yn59 case $yn in60 [Yy]* ) break;;61 [Nn]* ) exit 1;;62 * ) echo "Please answer yes or no.";;63 esac64 done65 fi66 67 # unzip the perf.data file in the current working directory and debug symbols in ~/.debug directory68 tar xvf $TARGET && tar xvf $PERF_SYMBOLS.tar.bz2 -C ~/.debug69 70 else # perf tar generated by perf archive (contains only debug symbols)71 tar xvf $TARGET -C ~/.debug72 fi73 exit 074fi75 76#77# PERF_BUILDID_DIR environment variable set by perf78# path to buildid directory, default to $HOME/.debug79#80if [ -z $PERF_BUILDID_DIR ]; then81 PERF_BUILDID_DIR=~/.debug/82else83 # append / to make substitutions work84 PERF_BUILDID_DIR=$PERF_BUILDID_DIR/85fi86 87BUILDIDS=$(mktemp /tmp/perf-archive-buildids.XXXXXX)88 89perf buildid-list -i $PERF_DATA --with-hits | grep -v "^ " > $BUILDIDS90if [ ! -s $BUILDIDS ] ; then91 echo "perf archive: no build-ids found"92 rm $BUILDIDS || true93 exit 194fi95 96MANIFEST=$(mktemp /tmp/perf-archive-manifest.XXXXXX)97PERF_BUILDID_LINKDIR=$(readlink -f $PERF_BUILDID_DIR)/98 99cut -d ' ' -f 1 $BUILDIDS | \100while read build_id ; do101 linkname=$PERF_BUILDID_DIR.build-id/${build_id:0:2}/${build_id:2}102 filename=$(readlink -f $linkname)103 echo ${linkname#$PERF_BUILDID_DIR} >> $MANIFEST104 echo ${filename#$PERF_BUILDID_LINKDIR} >> $MANIFEST105done106 107if [ $ALL -eq 1 ]; then # pack perf.data file together with tar containing debug symbols108 HOSTNAME=$(hostname)109 DATE=$(date '+%Y%m%d-%H%M%S')110 tar cjf $PERF_SYMBOLS.tar.bz2 -C $PERF_BUILDID_DIR -T $MANIFEST111 tar cjf $PERF_ALL-$HOSTNAME-$DATE.tar.bz2 $PERF_DATA $PERF_SYMBOLS.tar.bz2112 rm $PERF_SYMBOLS.tar.bz2 $MANIFEST $BUILDIDS || true113else # pack only the debug symbols114 tar cjf $PERF_DATA.tar.bz2 -C $PERF_BUILDID_DIR -T $MANIFEST115 rm $MANIFEST $BUILDIDS || true116fi117 118echo -e "Now please run:\n"119echo -e "$ perf archive --unpack\n"120echo "or unpack the tar manually wherever you need to run 'perf report' on."121exit 0122