]> granicus.if.org Git - strace/commitdiff
strace-log-merge: enhance pid formatting
authorDmitry V. Levin <ldv@altlinux.org>
Mon, 18 Mar 2019 19:06:47 +0000 (19:06 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Mon, 18 Mar 2019 19:06:47 +0000 (19:06 +0000)
* strace-log-merge (max_suffix_length): New variable.
(iterate_logfiles, process_suffix, process_logfile): New functions.
Use them to choose the optimum width for pid column.
* NEWS: Mention this enhancement.
* tests/strace-log-merge-suffix.test: New test.
* tests/Makefile.am (MISC_TESTS): Add it.

NEWS
strace-log-merge
tests/Makefile.am
tests/strace-log-merge-suffix.test [new file with mode: 0755]

diff --git a/NEWS b/NEWS
index 3bdfd0e0151ceb4a2b382789f29e45845e78a74a..96daad3e9a8f3197eb56421476f8d8b619f4f816 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -20,6 +20,7 @@ Noteworthy changes in release ?.?? (????-??-??)
   * Enhanced xlat styles support configured by -X option.
   * Enhanced decoding of bpf syscall.
   * Enhanced decoding of PTRACE_PEEKUSER and PTRACE_POKEUSER on hppa.
+  * Enhanced pid formatting in strace-log-merge output.
   * Wired up kexec_file_load and rseq syscalls on aarch64, arc, metag, nios2,
     or1k, riscv, and tile architectures.
   * Updated lists of BPF_*, BTRFS_*, FAN_*, IFLA_*, KERN_*, KVM_CAP_*, NDA_*,
index 9ef5705be25e0423dbce805334c6e6b856b151d8..93d87ba28be41ea7952992478b7ccd6a79ca0890 100755 (executable)
@@ -33,18 +33,53 @@ fi
 
 logfile=$1
 
-for file in "$logfile".*; do
-       [ -f "$file" ] || continue
-       suffix=${file#"$logfile".}
-       [ "$suffix" -gt 0 ] 2> /dev/null ||
-               continue
-       pid=$(printf "%-5s" $suffix)
+iterate_logfiles()
+{
+       local file suffix
+
+       for file in "$logfile".*; do
+               [ -f "$file" ] || continue
+               suffix=${file#"$logfile".}
+               [ "$suffix" -gt 0 ] 2> /dev/null ||
+                       continue
+               "$@" "$suffix" "$file"
+       done
+}
+
+max_suffix_length=0
+process_suffix()
+{
+       local suffix len
+       suffix="$1"; shift
+
+       len=${#suffix}
+       if [ $len -gt $max_suffix_length ]; then
+               max_suffix_length=$len
+       fi
+}
+
+process_logfile()
+{
+       local suffix file pid
+       suffix="$1"; shift
+       file="$1"; shift
+
+       pid=$(printf "%-*s" $max_suffix_length $suffix)
        # Some strace logs have last line which is not '\n' terminated,
        # so add extra newline to every file.
-       # grep -v '^$' removes empty lines which may result.
+       # Empty lines are removed later.
        sed -n "s/^\($dd:\)\?\($dd:\)\?\($ds\.\)\?$ds /\2\4\6\7 $pid \0/p" < "$file"
        echo
-done |
+}
+
+iterate_logfiles process_suffix
+
+[ $max_suffix_length -gt 0 ] || {
+       echo >&2 "${0##*/}: $logfile: strace output not found"
+       exit 1
+}
+
+iterate_logfiles process_logfile |
        sort -s -n -k1,1 |
        sed -n 's/^[0-9][0-9]* //p' |
        grep -v '^$'
index 553ff5e19c1dd777792e0a1b6312929bf505603b..9e99aff2f677da07c2fb8d2309d25fa0a3bb0927 100644 (file)
@@ -342,6 +342,7 @@ MISC_TESTS = \
        strace-V.test \
        strace-ff.test \
        strace-log-merge-error.test \
+       strace-log-merge-suffix.test \
        strace-r.test \
        strace-t.test \
        strace-tt.test \
diff --git a/tests/strace-log-merge-suffix.test b/tests/strace-log-merge-suffix.test
new file mode 100755 (executable)
index 0000000..51a2e5f
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/sh
+#
+# Check strace-log-merge pid formatting.
+#
+# Copyright (c) 2019 The strace developers.
+# All rights reserved.
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+. "${srcdir=.}/init.sh"
+
+rm -f -- "$LOG".[0-9]*
+
+echo '3456789012.345678 +++ exited with 3 +++' > "$LOG".4294967295
+echo '1234567890.123456 +++ exited with 2 +++' > "$LOG".65535
+echo '2345678901.234567 +++ exited with 1 +++' > "$LOG".1
+
+cat > "$EXP" <<'EOF'
+65535      1234567890.123456 +++ exited with 2 +++
+1          2345678901.234567 +++ exited with 1 +++
+4294967295 3456789012.345678 +++ exited with 3 +++
+EOF
+
+"$srcdir"/../strace-log-merge "$LOG" > "$OUT" 2> "$LOG" ||
+       dump_log_and_fail_with 'strace-log-merge failed'
+
+match_diff "$OUT" "$EXP" 'strace-log-merge output mismatch'
+
+rm -f -- "$LOG".[0-9]*