2 # Copyright (C) 1999, 2001-2008, 2009, 2010, 2011 Free Software Foundation, Inc.
3 # This file is part of the GNU C Library.
4 # Contributed by Ulrich Drepper <drepper@gnu.org>, 1999.
6 # The GNU C Library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # The GNU C Library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with the GNU C Library; if not, write to the Free
18 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 pcprofileso
='@SLIBDIR@/libpcprofile.so'
22 pcprofiledump
='@BINDIR@/pcprofiledump'
25 # Print usage message.
27 printf $
"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
31 # Refer to --help option.
33 printf >&2 $
"Try \`%s --help' or \`%s --usage' for more information.\n" xtrace xtrace
37 # Message for missing argument.
39 printf >&2 $
"%s: option '%s' requires an argument.\n" xtrace
"$1"
45 printf $
"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
46 printf $
"Trace execution of program by printing currently executed function.
48 --data=FILE Don't run the program, just print the data from FILE.
50 -?,--help Print this help and exit
51 --usage Give a short usage message
52 -V,--version Print version information and exit
54 Mandatory arguments to long options are also mandatory for any corresponding
58 echo $
"For bug reporting instructions, please see:
59 <http://www.gnu.org/software/libc/bugs.html>.
65 echo 'xtrace (GNU libc) @VERSION@'
66 printf $
"Copyright (C) %s Free Software Foundation, Inc.
67 This is free software; see the source for copying conditions. There is NO
68 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
70 printf $
"Written by %s.
75 # Print out function name, file, and line number in a nicely formatted way.
80 width
=$
(expr $COLUMNS - 30)
81 filelen
=$
(expr length
$file)
82 if test "$filelen" -gt "$width"; then
83 rwidth
=$
(expr $width - 3)
84 file="...$(expr substr $file $(expr 1 + $filelen - $rwidth) $rwidth)"
86 printf '%-20s %-*s %6s\n' $fct $width $file $line
90 # If the variable COLUMNS is not set do this now.
91 COLUMNS
=${COLUMNS:-80}
93 # If `TERMINAL_PROG' is not set, set it to `xterm'.
94 TERMINAL_PROG
=${TERMINAL_PROG:-xterm}
96 # The data file to process, if any.
99 # Process arguments. But stop as soon as the program name is found.
100 while test $# -gt 0; do
102 --d |
--da |
--dat |
--data)
103 if test $# -eq 1; then
109 --d=* |
--da=* |
--dat=* |
--data=*)
112 -\? |
--h |
--he |
--hel |
--help)
115 -V |
--v |
--ve |
--ver |
--vers |
--versi |
--versio |
--version)
118 --u |
--us |
--usa |
--usag |
--usage)
122 # Stop processing arguments.
127 printf >&2 $
"xtrace: unrecognized option \`$1'\n"
131 # Unknown option. This means the rest is the program name and parameters.
138 # See whether any arguments are left.
139 if test $# -eq 0; then
140 printf >&2 $
"No program name given\n"
144 # Determine the program name and check whether it exists.
147 if test ! -f "$program"; then
148 printf >&2 $
"executable \`$program' not found\n"
151 if test ! -x "$program"; then
152 printf >&2 $
"\`$program' is no executable\n"
156 # We have two modes. If a data file is given simply print the included data.
157 printf "%-20s %-*s %6s\n" Function $
(expr $COLUMNS - 30) File Line
158 for i
in $
(seq 1 $COLUMNS); do printf -; done; printf '\n'
159 if test -n "$data"; then
160 $pcprofiledump "$data" |
161 sed 's/this = \([^,]*\).*/\1/' |
162 addr2line
-fC -e "$program" |
165 if test "$fct" != '??' -a "$file" != '??:0'; then
166 format_line
"$fct" "$file"
170 fifo
=$
(mktemp
-ut xtrace.XXXXXX
) ||
exit
171 trap 'rm -f "$fifo"; exit 1' HUP INT QUIT TERM PIPE
172 mkfifo -m 0600 $fifo ||
exit 1
174 # Now start the program and let it write to the FIFO.
175 $TERMINAL_PROG -T "xtrace - $program $*" -e /bin
/sh
-c "LD_PRELOAD=$pcprofileso PCPROFILE_OUTPUT=$fifo $program $*; read < $fifo" &
177 $pcprofiledump -u "$fifo" |
180 sed 's/this = \([^,]*\).*/\1/' |
181 addr2line
-fC -e "$program"
185 if test "$fct" != '??' -a "$file" != '??:0'; then
186 format_line
"$fct" "$file"
189 read -p "Press return here to close $TERMINAL_PROG($program)."