Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strace-log-merge 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. show_usage()
  10. {
  11. cat <<__EOF__
  12. Usage: ${0##*/} STRACE_LOG
  13. Finds all STRACE_LOG.PID files, adds PID prefix to every line,
  14. then combines and sorts them, and prints result to standard output.
  15. It is assumed that STRACE_LOGs were produced by strace with -tt[t]
  16. option which prints timestamps (otherwise sorting won't do any good).
  17. __EOF__
  18. }
  19. dd='\([0-9][0-9]\)'
  20. ds='\([0-9][0-9]*\)'
  21. if [ $# -ne 1 ]; then
  22. show_usage >&2
  23. exit 1
  24. elif [ "$1" = '--help' ]; then
  25. show_usage
  26. exit 0
  27. fi
  28. logfile=$1
  29. iterate_logfiles()
  30. {
  31. local file suffix
  32. for file in "$logfile".*; do
  33. [ -f "$file" ] || continue
  34. suffix=${file#"$logfile".}
  35. [ "$suffix" -gt 0 ] 2> /dev/null ||
  36. continue
  37. "$@" "$suffix" "$file"
  38. done
  39. }
  40. max_suffix_length=0
  41. process_suffix()
  42. {
  43. local suffix len
  44. suffix="$1"; shift
  45. len=${#suffix}
  46. if [ $len -gt $max_suffix_length ]; then
  47. max_suffix_length=$len
  48. fi
  49. }
  50. process_logfile()
  51. {
  52. local suffix file pid
  53. suffix="$1"; shift
  54. file="$1"; shift
  55. pid=$(printf "%-*s" $max_suffix_length $suffix)
  56. # Some strace logs have last line which is not '\n' terminated,
  57. # so add extra newline to every file.
  58. # Empty lines are removed later.
  59. sed -n "s/^\($dd:\)\?\($dd:\)\?\($ds\.\)\?$ds /\2\4\6\7 $pid \0/p" < "$file"
  60. echo
  61. }
  62. iterate_logfiles process_suffix
  63. [ $max_suffix_length -gt 0 ] || {
  64. echo >&2 "${0##*/}: $logfile: strace output not found"
  65. exit 1
  66. }
  67. iterate_logfiles process_logfile |
  68. sort -s -n -k1,1 |
  69. sed -n 's/^[0-9][0-9]* //p' |
  70. grep -v '^$'
  71. rc=$?
  72. [ $rc -eq 1 ] &&
  73. echo >&2 "${0##*/}: $logfile: strace output not found"
  74. exit $rc