|
Packit |
56e23f |
#!/bin/bash
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
#
|
|
Packit |
56e23f |
# Runtime syscall inspector
|
|
Packit |
56e23f |
#
|
|
Packit |
56e23f |
# Copyright (c) 2012 Red Hat <pmoore@redhat.com>
|
|
Packit |
56e23f |
# Author: Paul Moore <paul@paul-moore.com>
|
|
Packit |
56e23f |
#
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
#
|
|
Packit |
56e23f |
# This library is free software; you can redistribute it and/or modify it
|
|
Packit |
56e23f |
# under the terms of version 2.1 of the GNU Lesser General Public License as
|
|
Packit |
56e23f |
# published by the Free Software Foundation.
|
|
Packit |
56e23f |
#
|
|
Packit |
56e23f |
# This library is distributed in the hope that it will be useful, but WITHOUT
|
|
Packit |
56e23f |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
Packit |
56e23f |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
Packit |
56e23f |
# for more details.
|
|
Packit |
56e23f |
#
|
|
Packit |
56e23f |
# You should have received a copy of the GNU Lesser General Public License
|
|
Packit |
56e23f |
# along with this library; if not, see <http://www.gnu.org/licenses>.
|
|
Packit |
56e23f |
#
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
####
|
|
Packit |
56e23f |
# functions
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
function verify_deps() {
|
|
Packit |
56e23f |
[[ -z "$1" ]] && return
|
|
Packit |
56e23f |
if ! which "$1" >& /dev/null; then
|
|
Packit |
56e23f |
echo "error: install \"$1\" and include it in your \$PATH"
|
|
Packit |
56e23f |
exit 1
|
|
Packit |
56e23f |
fi
|
|
Packit |
56e23f |
}
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
####
|
|
Packit |
56e23f |
# main
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
# verify script dependencies
|
|
Packit |
56e23f |
verify_deps strace
|
|
Packit |
56e23f |
verify_deps sed
|
|
Packit |
56e23f |
verify_deps sort
|
|
Packit |
56e23f |
verify_deps uniq
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
# get the command line arguments
|
|
Packit |
56e23f |
opt_freq=0
|
|
Packit |
56e23f |
opt_args=0
|
|
Packit |
56e23f |
opt_out="/proc/self/fd/1"
|
|
Packit |
56e23f |
while getopts "afo:h" opt; do
|
|
Packit |
56e23f |
case $opt in
|
|
Packit |
56e23f |
a)
|
|
Packit |
56e23f |
opt_args=1
|
|
Packit |
56e23f |
;;
|
|
Packit |
56e23f |
f)
|
|
Packit |
56e23f |
opt_freq=1
|
|
Packit |
56e23f |
;;
|
|
Packit |
56e23f |
o)
|
|
Packit |
56e23f |
opt_out="$OPTARG"
|
|
Packit |
56e23f |
;;
|
|
Packit |
56e23f |
h|*)
|
|
Packit |
56e23f |
echo "usage $0 [-f] [-a] [-o <file>] <command> [<args>]"
|
|
Packit |
56e23f |
exit 1
|
|
Packit |
56e23f |
esac
|
|
Packit |
56e23f |
done
|
|
Packit |
56e23f |
shift $(expr $OPTIND - 1)
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
# generate a temporary output file
|
|
Packit |
56e23f |
raw=$(mktemp -t strace-raw_XXXXXX)
|
|
Packit |
56e23f |
out="$raw-out"
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
# capture the strace output
|
|
Packit |
56e23f |
strace -o $raw -- $*
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
# filter the raw strace
|
|
Packit |
56e23f |
if [[ $opt_args -eq 0 ]]; then
|
|
Packit |
56e23f |
if [[ $opt_freq -eq 0 ]]; then
|
|
Packit |
56e23f |
cat $raw | sed -e 's/(.*//' | sort -u > $out
|
|
Packit |
56e23f |
else
|
|
Packit |
56e23f |
cat $raw | sed -e 's/(.*//' | sort | uniq -c | sort -nr > $out
|
|
Packit |
56e23f |
fi
|
|
Packit |
56e23f |
else
|
|
Packit |
56e23f |
if [[ $opt_freq -eq 0 ]]; then
|
|
Packit |
56e23f |
cat $raw | sed -e 's/)[ \t]*=.*$/)/' \
|
|
Packit |
56e23f |
| sed -e 's/".*,/"...",/g;s/\/\*.*\*\//.../g' \
|
|
Packit |
56e23f |
| sed -e 's/0x[a-f0-9]\+/.../g' \
|
|
Packit |
56e23f |
| sort -u > $out
|
|
Packit |
56e23f |
else
|
|
Packit |
56e23f |
cat $raw | sed -e 's/)[ \t]*=.*$/)/' \
|
|
Packit |
56e23f |
| sed -e 's/".*,/"...",/g;s/\/\*.*\*\//.../g' \
|
|
Packit |
56e23f |
| sed -e 's/0x[a-f0-9]\+/.../g' \
|
|
Packit |
56e23f |
| sort | uniq -c | sort -nr > $out
|
|
Packit |
56e23f |
fi
|
|
Packit |
56e23f |
fi
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
# display the output
|
|
Packit |
56e23f |
echo "============================================================" > $opt_out
|
|
Packit |
56e23f |
echo "Syscall Report (\"$*\")" >> $opt_out
|
|
Packit |
56e23f |
[[ $opt_freq -eq 1 ]] && echo " freq syscall" >> $opt_out
|
|
Packit |
56e23f |
echo "============================================================" >> $opt_out
|
|
Packit |
56e23f |
cat $out >> $opt_out
|
|
Packit |
56e23f |
|
|
Packit |
56e23f |
# cleanup and exit
|
|
Packit |
56e23f |
rm -f $raw $out
|
|
Packit |
56e23f |
exit 0
|