Denys Vlasenko | da3657d | 2012-03-09 13:43:44 +0100 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # TODO: install alongside strace on "make install" time? |
| 4 | |
| 5 | if test $# = 0; then |
| 6 | echo "Usage: ${0##*/} STRACE_LOG" |
| 7 | echo |
| 8 | echo "\ |
| 9 | Finds all STRACE_LOG.PID files, adds PID prefix to every line, |
| 10 | then combines and sorts them, and prints result to standard output. |
| 11 | |
| 12 | It is assumed that STRACE_LOGs were produced by strace with -tt[t] |
| 13 | option which prints timestamps (otherwise sorting won't do any good).\ |
| 14 | " |
| 15 | exit |
| 16 | fi |
| 17 | |
| 18 | is_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 | |
| 33 | logfile=$1 |
| 34 | pfxlen=${#1} |
| 35 | |
| 36 | for 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" |
| 47 | done \ |
| 48 | | grep -v '^$' | sort -k2 |