#!/bin/sh
usage () {
    echo  Usage:
    echo "    $0 PROGRAM args..."
    echo  runs PROGRAM under the kernel+itimer PCT statistical profiling system.
}
if [ $# -lt 1 ]; then usage; exit 1; fi

OUT=/tmp/pct/ct.$$
echo "PCT output sent to $OUT"

LD_PRELOAD=${LIB_SO:-/usr/local/lib}/libitimer.so
export LD_PRELOAD

: ${VMUNIX:=vmlinux}
: ${KPATH:=/usr/src/linux/${VMUNIX}}
: ${SYSTEM_MAP:=/boot/System.map-`uname -r`}

pct-kern-snap $KPATH $OUT/$VMUNIX
PCT="vo$OUT"
export PCT
"$@"
unset PCT
pct-kern-snap -d $KPATH $OUT/$VMUNIX

mkdir -p `dirname /tmp/pct/nm/$KPATH`
cp $SYSTEM_MAP /tmp/pct/nm/$KPATH
kernel=$OUT/$VMUNIX

# Here we use a simple heuristic of treating the kernel and shared objects as
# if they have only symbol data and all other objects as if they have full -g
# debugging data.

pct-pr  -t addr2nm,@ $kernel `find $OUT -type f    -name '*.so*' -print` \
	-t addr2line,-e,@    `find $OUT -type f \! -name '*.so*' -print` |
    pct-sort 1 |
    pct-fold   |
    sort -n    |
    pct-%

