xtrace 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #! /bin/bash
  2. # Copyright (C) 1999-2021 Free Software Foundation, Inc.
  3. # This file is part of the GNU C Library.
  4. # Contributed by Ulrich Drepper <drepper@gnu.org>, 1999.
  5. # The GNU C Library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9. # The GNU C Library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. # You should have received a copy of the GNU Lesser General Public
  14. # License along with the GNU C Library; if not, see
  15. # <https://www.gnu.org/licenses/>.
  16. pcprofileso='/\$LIB/libpcprofile.so'
  17. pcprofiledump='/usr/bin/pcprofiledump'
  18. TEXTDOMAIN=libc
  19. # Print usage message.
  20. do_usage() {
  21. printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
  22. exit 0
  23. }
  24. # Refer to --help option.
  25. help_info() {
  26. printf >&2 $"Try \`%s --help' or \`%s --usage' for more information.\n" xtrace xtrace
  27. exit 1
  28. }
  29. # Message for missing argument.
  30. do_missing_arg() {
  31. printf >&2 $"%s: option '%s' requires an argument.\n" xtrace "$1"
  32. help_info
  33. }
  34. # Print help message
  35. do_help() {
  36. printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
  37. printf $"Trace execution of program by printing currently executed function.
  38. --data=FILE Don't run the program, just print the data from FILE.
  39. -?,--help Print this help and exit
  40. --usage Give a short usage message
  41. -V,--version Print version information and exit
  42. Mandatory arguments to long options are also mandatory for any corresponding
  43. short options.
  44. "
  45. printf $"For bug reporting instructions, please see:\\n%s.\\n" \
  46. "<https://bugs.linaro.org/>"
  47. exit 0
  48. }
  49. do_version() {
  50. echo 'xtrace (GNU) 2.33'
  51. printf $"Copyright (C) %s Free Software Foundation, Inc.
  52. This is free software; see the source for copying conditions. There is NO
  53. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  54. " "2021"
  55. printf $"Written by %s.
  56. " "Ulrich Drepper"
  57. exit 0
  58. }
  59. # Print out function name, file, and line number in a nicely formatted way.
  60. format_line() {
  61. fct=$1
  62. file=${2%%:*}
  63. line=${2##*:}
  64. width=$(expr $COLUMNS - 30)
  65. filelen=$(expr length $file)
  66. if test "$filelen" -gt "$width"; then
  67. rwidth=$(expr $width - 3)
  68. file="...$(expr substr $file $(expr 1 + $filelen - $rwidth) $rwidth)"
  69. fi
  70. printf '%-20s %-*s %6s\n' $fct $width $file $line
  71. }
  72. # If the variable COLUMNS is not set do this now.
  73. COLUMNS=${COLUMNS:-80}
  74. # If `TERMINAL_PROG' is not set, set it to `xterm'.
  75. TERMINAL_PROG=${TERMINAL_PROG:-xterm}
  76. # The data file to process, if any.
  77. data=
  78. # Process arguments. But stop as soon as the program name is found.
  79. while test $# -gt 0; do
  80. case "$1" in
  81. --d | --da | --dat | --data)
  82. if test $# -eq 1; then
  83. do_missing_arg $1
  84. fi
  85. shift
  86. data="$1"
  87. ;;
  88. --d=* | --da=* | --dat=* | --data=*)
  89. data=${1##*=}
  90. ;;
  91. -\? | --h | --he | --hel | --help)
  92. do_help
  93. ;;
  94. -V | --v | --ve | --ver | --vers | --versi | --versio | --version)
  95. do_version
  96. ;;
  97. --u | --us | --usa | --usag | --usage)
  98. do_usage
  99. ;;
  100. --)
  101. # Stop processing arguments.
  102. shift
  103. break
  104. ;;
  105. --*)
  106. printf >&2 $"xtrace: unrecognized option \`$1'\n"
  107. help_info
  108. ;;
  109. *)
  110. # Unknown option. This means the rest is the program name and parameters.
  111. break
  112. ;;
  113. esac
  114. shift
  115. done
  116. # See whether any arguments are left.
  117. if test $# -eq 0; then
  118. printf >&2 $"No program name given\n"
  119. help_info
  120. fi
  121. # Determine the program name and check whether it exists.
  122. program=$1
  123. shift
  124. if test ! -f "$program"; then
  125. printf >&2 $"executable \`$program' not found\n"
  126. help_info
  127. fi
  128. if test ! -x "$program"; then
  129. printf >&2 $"\`$program' is no executable\n"
  130. help_info
  131. fi
  132. # We have two modes. If a data file is given simply print the included data.
  133. printf "%-20s %-*s %6s\n" Function $(expr $COLUMNS - 30) File Line
  134. for i in $(seq 1 $COLUMNS); do printf -; done; printf '\n'
  135. if test -n "$data"; then
  136. $pcprofiledump "$data" |
  137. sed 's/this = \([^,]*\).*/\1/' |
  138. addr2line -fC -e "$program" |
  139. while read fct; do
  140. read file
  141. if test "$fct" != '??' -a "$file" != '??:0'; then
  142. format_line "$fct" "$file"
  143. fi
  144. done
  145. else
  146. fifo=$(mktemp -ut xtrace.XXXXXX) || exit
  147. trap 'rm -f "$fifo"; exit 1' HUP INT QUIT TERM PIPE
  148. mkfifo -m 0600 $fifo || exit 1
  149. # Now start the program and let it write to the FIFO.
  150. $TERMINAL_PROG -T "xtrace - $program $*" -e /bin/sh -c "LD_PRELOAD=$pcprofileso PCPROFILE_OUTPUT=$fifo $program $*; read < $fifo" &
  151. termpid=$!
  152. $pcprofiledump -u "$fifo" |
  153. while read line; do
  154. echo "$line" |
  155. sed 's/this = \([^,]*\).*/\1/' |
  156. addr2line -fC -e "$program"
  157. done |
  158. while read fct; do
  159. read file
  160. if test "$fct" != '??' -a "$file" != '??:0'; then
  161. format_line "$fct" "$file"
  162. fi
  163. done
  164. read -p "Press return here to close $TERMINAL_PROG($program)."
  165. echo > "$fifo"
  166. rm "$fifo"
  167. fi
  168. exit 0
  169. # Local Variables:
  170. # mode:ksh
  171. # End: