blob: 5f96c2a4e3d6475cc3bbdee622eed0d2ac07c7e9 [file] [log] [blame]
Denys Vlasenkoda3657d2012-03-09 13:43:44 +01001#!/bin/sh
2#
3# TODO: install alongside strace on "make install" time?
4
5if test $# = 0; then
6 echo "Usage: ${0##*/} STRACE_LOG"
7 echo
8 echo "\
9Finds all STRACE_LOG.PID files, adds PID prefix to every line,
10then combines and sorts them, and prints result to standard output.
11
12It is assumed that STRACE_LOGs were produced by strace with -tt[t]
13option which prints timestamps (otherwise sorting won't do any good).\
14"
15 exit
16fi
17
18is_numeric() {
19 # Remove digits. If something remains,
20 # then $1 is not a number
21
22 u=$1
23 test "$u" || return 1 # "" is not a number
24
25 while true; do
26 v=${u#[0123456789]} # remove one digit
27 test "$v" || return 0 # we removed all chars. ok
28 test "$v" = "$u" && return 1 # we have non-digit. bad
29 u=$v
30 done
31}
32
33logfile=$1
34pfxlen=${#1}
35
36for file in "$logfile".*; do
37 suffix=${file:1+$pfxlen}
38 is_numeric "$suffix" || {
39 echo "Skipped file '$file' (bad suffix)" >&2
40 continue
41 }
42 pid=$(printf "%-5s" $suffix)
43 # Some strace logs have last line which is not '\n' terminated.
44 # 's/$/\n/' adds extra newlines to every line.
45 # grep -v '^$' removes empty lines which may result.
46 sed -e "s/^/$pid /" -e 's/$/\n/' <"$file"
47done \
48| grep -v '^$' | sort -k2