2 # Copyright (C) 1999-2024 Free Software Foundation, Inc.
3 # This file is part of the GNU C Library.
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.
10 # The GNU C Library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with the GNU C Library; if not, see
17 # <https://www.gnu.org/licenses/>.
19 pcprofileso='@SLIBDIR@/libpcprofile.so'
20 pcprofiledump='@BINDIR@/pcprofiledump'
23 # Print usage message.
25 printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
29 # Refer to --help option.
31 printf >&2 $"Try \`%s --help' or \`%s --usage' for more information.\n" xtrace xtrace
35 # Message for missing argument.
37 printf >&2 $"%s: option '%s' requires an argument.\n" xtrace "$1"
43 printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
44 printf $"Trace execution of program by printing currently executed function.
46 --data=FILE Don't run the program, just print the data from FILE.
48 -?,--help Print this help and exit
49 --usage Give a short usage message
50 -V,--version Print version information and exit
52 Mandatory arguments to long options are also mandatory for any corresponding
56 printf $"For bug reporting instructions, please see:\\n%s.\\n" \
62 echo 'xtrace @PKGVERSION@@VERSION@'
63 printf $"Copyright (C) %s Free Software Foundation, Inc.
64 This is free software; see the source for copying conditions. There is NO
65 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
67 printf $"Written by %s.
72 # Print out function name, file, and line number in a nicely formatted way.
77 width=$(expr $COLUMNS - 30)
78 filelen=$(expr length $file)
79 if test "$filelen" -gt "$width"; then
80 rwidth=$(expr $width - 3)
81 file="...$(expr substr $file $(expr 1 + $filelen - $rwidth) $rwidth)"
83 printf '%-20s %-*s %6s\n' $fct $width $file $line
87 # If the variable COLUMNS is not set do this now.
88 COLUMNS=${COLUMNS:-80}
90 # If `TERMINAL_PROG' is not set, set it to `xterm'.
91 TERMINAL_PROG=${TERMINAL_PROG:-xterm}
93 # The data file to process, if any.
96 # Process arguments. But stop as soon as the program name is found.
97 while test $# -gt 0; do
99 --d | --da | --dat | --data)
100 if test $# -eq 1; then
106 --d=* | --da=* | --dat=* | --data=*)
109 -\? | --h | --he | --hel | --help)
112 -V | --v | --ve | --ver | --vers | --versi | --versio | --version)
115 --u | --us | --usa | --usag | --usage)
119 # Stop processing arguments.
124 printf >&2 $"xtrace: unrecognized option \`$1'\n"
128 # Unknown option. This means the rest is the program name and parameters.
135 # See whether any arguments are left.
136 if test $# -eq 0; then
137 printf >&2 $"No program name given\n"
141 # Determine the program name and check whether it exists.
144 if test ! -f "$program"; then
145 printf >&2 $"executable \`$program' not found\n"
148 if test ! -x "$program"; then
149 printf >&2 $"\`$program' is no executable\n"
153 # We have two modes. If a data file is given simply print the included data.
154 printf "%-20s %-*s %6s\n" Function $(expr $COLUMNS - 30) File Line
155 for i in $(seq 1 $COLUMNS); do printf -; done; printf '\n'
156 if test -n "$data"; then
157 $pcprofiledump "$data" |
158 sed 's/this = \([^,]*\).*/\1/' |
159 addr2line -fC -e "$program" |
162 if test "$fct" != '??' -a "$file" != '??:0'; then
163 format_line "$fct" "$file"
167 fifo=$(mktemp -ut xtrace.XXXXXX) || exit
168 trap 'rm -f "$fifo"; exit 1' HUP INT QUIT TERM PIPE
169 mkfifo -m 0600 $fifo || exit 1
171 # Now start the program and let it write to the FIFO.
172 $TERMINAL_PROG -T "xtrace - $program $*" -e /bin/sh -c "LD_PRELOAD=$pcprofileso PCPROFILE_OUTPUT=$fifo $program $*; read < $fifo" &
174 $pcprofiledump -u "$fifo" |
177 sed 's/this = \([^,]*\).*/\1/' |
178 addr2line -fC -e "$program"
182 if test "$fct" != '??' -a "$file" != '??:0'; then
183 format_line "$fct" "$file"
186 read -p "Press return here to close $TERMINAL_PROG($program)."