]> granicus.if.org Git - strace/commitdiff
strace_log_merge: new file. Helper to merge timestamped strace -ff logs
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 9 Mar 2012 12:43:44 +0000 (13:43 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 9 Mar 2012 12:43:44 +0000 (13:43 +0100)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
strace_log_merge [new file with mode: 0755]

diff --git a/strace_log_merge b/strace_log_merge
new file mode 100755 (executable)
index 0000000..5f96c2a
--- /dev/null
@@ -0,0 +1,48 @@
+#!/bin/sh
+#
+# TODO: install alongside strace on "make install" time?
+
+if test $# = 0; then
+       echo "Usage: ${0##*/} STRACE_LOG"
+       echo
+       echo "\
+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]
+option which prints timestamps (otherwise sorting won't do any good).\
+"
+       exit
+fi
+
+is_numeric() {
+       # Remove digits. If something remains,
+       # then $1 is not a number
+
+       u=$1
+       test "$u" || return 1 # "" is not a number
+
+       while true; do
+               v=${u#[0123456789]} # remove one digit
+               test "$v" || return 0 # we removed all chars. ok
+               test "$v" = "$u" && return 1 # we have non-digit. bad
+               u=$v
+       done
+}
+
+logfile=$1
+pfxlen=${#1}
+
+for file in "$logfile".*; do
+       suffix=${file:1+$pfxlen}
+       is_numeric "$suffix" || {
+               echo "Skipped file '$file' (bad suffix)" >&2
+               continue
+       }
+       pid=$(printf "%-5s" $suffix)
+       # Some strace logs have last line which is not '\n' terminated.
+       # 's/$/\n/' adds extra newlines to every line.
+       # grep -v '^$' removes empty lines which may result.
+       sed -e "s/^/$pid /" -e 's/$/\n/' <"$file"
+done \
+| grep -v '^$' | sort -k2