1#!/bin/sh
2
3show_usage()
4{
5	cat <<__EOF__
6Usage: ${0##*/} STRACE_LOG
7
8Finds all STRACE_LOG.PID files, adds PID prefix to every line,
9then combines and sorts them, and prints result to standard output.
10
11It is assumed that STRACE_LOGs were produced by strace with -tt[t]
12option which prints timestamps (otherwise sorting won't do any good).
13__EOF__
14}
15
16if [ $# -ne 1 ]; then
17	show_usage >&2
18	exit 1
19elif [ "$1" = '--help' ]; then
20	show_usage
21	exit 0
22fi
23
24logfile=$1
25
26for file in "$logfile".*; do
27	[ -f "$file" ] || continue
28	suffix=${file#"$logfile".}
29	[ "$suffix" -gt 0 ] 2> /dev/null ||
30		continue
31	pid=$(printf "%-5s" $suffix)
32	# Some strace logs have last line which is not '\n' terminated,
33	# so add extra newline to every file.
34	# grep -v '^$' removes empty lines which may result.
35	sed "s/^/$pid /" < "$file"
36	echo
37done \
38| sort -s -k2,2 | grep -v '^$'
39
40rc=$?
41[ $rc -eq 1 ] &&
42	echo >&2 "${0##*/}: $logfile: strace output not found"
43exit $rc
44