]> granicus.if.org Git - strace/blob - strace-log-merge
xlat: add BPF_F_TEST_STATE_FREQ to bpf_prog_flags
[strace] / strace-log-merge
1 #!/bin/sh
2 #
3 # This script processes strace -ff -tt output.  It merges the contents of all
4 # STRACE_LOG.PID files and sorts them, printing result on the standard output.
5 #
6 # Copyright (c) 2012-2019 The strace developers.
7 #
8 # SPDX-License-Identifier: LGPL-2.1-or-later
9
10 show_usage()
11 {
12         cat <<__EOF__
13 Usage: ${0##*/} STRACE_LOG
14
15 Finds all STRACE_LOG.PID files, adds PID prefix to every line,
16 then combines and sorts them, and prints result to standard output.
17
18 It is assumed that STRACE_LOGs were produced by strace with -tt[t]
19 option which prints timestamps (otherwise sorting won't do any good).
20 __EOF__
21 }
22
23 dd='\([0-9][0-9]\)'
24 ds='\([0-9][0-9]*\)'
25
26 if [ $# -ne 1 ]; then
27         show_usage >&2
28         exit 1
29 elif [ "$1" = '--help' ]; then
30         show_usage
31         exit 0
32 fi
33
34 logfile=$1
35
36 iterate_logfiles()
37 {
38         local file suffix
39
40         for file in "$logfile".*; do
41                 [ -f "$file" ] || continue
42                 suffix=${file#"$logfile".}
43                 [ "$suffix" -gt 0 ] 2> /dev/null ||
44                         continue
45                 "$@" "$suffix" "$file"
46         done
47 }
48
49 max_suffix_length=0
50 process_suffix()
51 {
52         local suffix len
53         suffix="$1"; shift
54
55         len=${#suffix}
56         if [ $len -gt $max_suffix_length ]; then
57                 max_suffix_length=$len
58         fi
59 }
60
61 process_logfile()
62 {
63         local suffix file pid
64         suffix="$1"; shift
65         file="$1"; shift
66
67         pid=$(printf "%-*s" $max_suffix_length $suffix)
68         # Some strace logs have last line which is not '\n' terminated,
69         # so add extra newline to every file.
70         # Empty lines are removed later.
71         sed -n "s/^\($dd:\)\?\($dd:\)\?\($ds\.\)\?$ds /\2\4\6\7 $pid \0/p" < "$file"
72         echo
73 }
74
75 iterate_logfiles process_suffix
76
77 [ $max_suffix_length -gt 0 ] || {
78         echo >&2 "${0##*/}: $logfile: strace output not found"
79         exit 1
80 }
81
82 iterate_logfiles process_logfile |
83         sort -s -n -k1,1 |
84         sed -n 's/^[0-9][0-9]* //p' |
85         grep -v '^$'
86
87 rc=$?
88 [ $rc -eq 1 ] &&
89         echo >&2 "${0##*/}: $logfile: strace output not found"
90 exit $rc