#!/bin/bash -e

# This script is copied from https://www.kernel.org/doc/Documentation/gcov.txt
# Appendix B: gather_on_test.sh

DEST=$1
GCDA=/sys/kernel/debug/gcov

if [ -z "$DEST" ] ; then
	echo "Usage: $0 <output.tar.gz>" >&2
	exit 1
fi

TEMPDIR=$(mktemp -d /tmp/gather-gcov-test-XXXXXX)
echo Collecting data..
find $GCDA -type d -exec mkdir -p $TEMPDIR/\{\} \;
find $GCDA -name '*.gcda' -exec sh -c 'cat < $0 > '$TEMPDIR'/$0' {} \;
find $GCDA -name '*.gcno' -exec sh -c 'cp -d $0 '$TEMPDIR'/$0' {} \;
tar czf $DEST -C $TEMPDIR sys
rm -rf $TEMPDIR

echo "$DEST successfully created, copy to build system and unpack with:"
echo "  tar xfz $DEST"
