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