From b734e8c1e8b8e76703b5614b393c89b59080e836 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Mon, 18 Mar 2019 19:06:47 +0000 Subject: [PATCH] strace-log-merge: enhance pid formatting * 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 | 1 + strace-log-merge | 51 +++++++++++++++++++++++++----- tests/Makefile.am | 1 + tests/strace-log-merge-suffix.test | 29 +++++++++++++++++ 4 files changed, 74 insertions(+), 8 deletions(-) create mode 100755 tests/strace-log-merge-suffix.test diff --git a/NEWS b/NEWS index 3bdfd0e0..96daad3e 100644 --- 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_*, diff --git a/strace-log-merge b/strace-log-merge index 9ef5705b..93d87ba2 100755 --- a/strace-log-merge +++ b/strace-log-merge @@ -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 '^$' diff --git a/tests/Makefile.am b/tests/Makefile.am index 553ff5e1..9e99aff2 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 00000000..51a2e5fb --- /dev/null +++ b/tests/strace-log-merge-suffix.test @@ -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]* -- 2.40.0