2012-03-09 16:43:44 +04:00
#!/bin/sh
2017-11-29 23:38:27 +03:00
#
# This script processes strace -ff -tt output. It merges the contents of all
# STRACE_LOG.PID files and sorts them, printing result on the standard output.
#
2018-02-14 01:00:00 +03:00
# Copyright (c) 2012-2017 The strace developers.
2017-11-29 23:38:27 +03:00
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2012-03-09 16:43:44 +04:00
2012-03-26 01:04:57 +04:00
show_usage()
{
cat <<__EOF__
2012-03-10 20:10:06 +04:00
Usage: ${0##*/} STRACE_LOG
2012-03-09 16:43:44 +04:00
Finds all STRACE_LOG.PID files, adds PID prefix to every line,
then combines and sorts them, and prints result to standard output.
It is assumed that STRACE_LOGs were produced by strace with -tt[t]
2012-03-10 20:10:06 +04:00
option which prints timestamps (otherwise sorting won't do any good).
__EOF__
2012-03-26 01:04:57 +04:00
}
2017-11-30 04:14:14 +03:00
dd='\([0-9][0-9]\)'
ds='\([0-9][0-9]*\)'
2012-03-26 01:04:57 +04:00
if [ $# -ne 1 ]; then
show_usage >&2
2012-03-10 20:10:06 +04:00
exit 1
2012-03-26 01:04:57 +04:00
elif [ "$1" = '--help' ]; then
show_usage
exit 0
2012-03-09 16:43:44 +04:00
fi
logfile=$1
for file in "$logfile".*; do
2012-03-10 20:10:06 +04:00
[ -f "$file" ] || continue
2012-03-26 16:10:01 +04:00
suffix=${file#"$logfile".}
2012-03-26 01:04:57 +04:00
[ "$suffix" -gt 0 ] 2> /dev/null ||
2012-03-09 16:43:44 +04:00
continue
pid=$(printf "%-5s" $suffix)
2012-03-10 20:10:06 +04:00
# Some strace logs have last line which is not '\n' terminated,
# so add extra newline to every file.
2012-03-09 16:43:44 +04:00
# grep -v '^$' removes empty lines which may result.
2017-11-30 04:14:14 +03:00
sed -n "s/^\($dd:\)\?\($dd:\)\?\($ds\.\)\?$ds /\2\4\6\7 $pid \0/p" < "$file"
2012-03-10 20:10:06 +04:00
echo
2012-03-09 16:43:44 +04:00
done \
2017-11-30 04:14:14 +03:00
| sort -s -n -k1,1 | sed -n 's/^[0-9][0-9]* //p'
2012-03-26 01:04:57 +04:00
rc=$?
[ $rc -eq 1 ] &&
echo >&2 "${0##*/}: $logfile: strace output not found"
exit $rc