| 1 | #!/bin/bash |
| 2 | |
| 3 | # |
| 4 | # Runtime syscall inspector |
| 5 | # |
| 6 | # Copyright (c) 2012 Red Hat <[email protected]> |
| 7 | # Author: Paul Moore <[email protected]> |
| 8 | # |
| 9 | |
| 10 | # |
| 11 | # This library is free software; you can redistribute it and/or modify it |
| 12 | # under the terms of version 2.1 of the GNU Lesser General Public License as |
| 13 | # published by the Free Software Foundation. |
| 14 | # |
| 15 | # This library is distributed in the hope that it will be useful, but WITHOUT |
| 16 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 17 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 18 | # for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU Lesser General Public License |
| 21 | # along with this library; if not, see <http://www.gnu.org/licenses>. |
| 22 | # |
| 23 | |
| 24 | #### |
| 25 | # functions |
| 26 | |
| 27 | function verify_deps() { |
| 28 | [[ -z "$1" ]] && return |
| 29 | if ! which "$1" >& /dev/null; then |
| 30 | echo "error: install \"$1\" and include it in your \$PATH" |
| 31 | exit 1 |
| 32 | fi |
| 33 | } |
| 34 | |
| 35 | #### |
| 36 | # main |
| 37 | |
| 38 | # verify script dependencies |
| 39 | verify_deps strace |
| 40 | verify_deps sed |
| 41 | verify_deps sort |
| 42 | verify_deps uniq |
| 43 | |
| 44 | # get the command line arguments |
| 45 | opt_freq=0 |
| 46 | opt_args=0 |
| 47 | opt_out="/proc/self/fd/1" |
| 48 | while getopts "afo:h" opt; do |
| 49 | case $opt in |
| 50 | a) |
| 51 | opt_args=1 |
| 52 | ;; |
| 53 | f) |
| 54 | opt_freq=1 |
| 55 | ;; |
| 56 | o) |
| 57 | opt_out="$OPTARG" |
| 58 | ;; |
| 59 | h|*) |
| 60 | echo "usage $0 [-f] [-a] [-o <file>] <command> [<args>]" |
| 61 | exit 1 |
| 62 | esac |
| 63 | done |
| 64 | shift $(expr $OPTIND - 1) |
| 65 | |
| 66 | # generate a temporary output file |
| 67 | raw=$(mktemp -t strace-raw_XXXXXX) |
| 68 | out="$raw-out" |
| 69 | |
| 70 | # capture the strace output |
| 71 | strace -o $raw -- $* |
| 72 | |
| 73 | # filter the raw strace |
| 74 | if [[ $opt_args -eq 0 ]]; then |
| 75 | if [[ $opt_freq -eq 0 ]]; then |
| 76 | cat $raw | sed -e 's/(.*//' | sort -u > $out |
| 77 | else |
| 78 | cat $raw | sed -e 's/(.*//' | sort | uniq -c | sort -nr > $out |
| 79 | fi |
| 80 | else |
| 81 | if [[ $opt_freq -eq 0 ]]; then |
| 82 | cat $raw | sed -e 's/)[ \t]*=.*$/)/' \ |
| 83 | | sed -e 's/".*,/"...",/g;s/\/\*.*\*\//.../g' \ |
| 84 | | sed -e 's/0x[a-f0-9]\+/.../g' \ |
| 85 | | sort -u > $out |
| 86 | else |
| 87 | cat $raw | sed -e 's/)[ \t]*=.*$/)/' \ |
| 88 | | sed -e 's/".*,/"...",/g;s/\/\*.*\*\//.../g' \ |
| 89 | | sed -e 's/0x[a-f0-9]\+/.../g' \ |
| 90 | | sort | uniq -c | sort -nr > $out |
| 91 | fi |
| 92 | fi |
| 93 | |
| 94 | # display the output |
| 95 | echo "============================================================" > $opt_out |
| 96 | echo "Syscall Report (\"$*\")" >> $opt_out |
| 97 | [[ $opt_freq -eq 1 ]] && echo " freq syscall" >> $opt_out |
| 98 | echo "============================================================" >> $opt_out |
| 99 | cat $out >> $opt_out |
| 100 | |
| 101 | # cleanup and exit |
| 102 | rm -f $raw $out |
| 103 | exit 0 |