Update.
[glibc.git] / debug / xtrace.sh
blob1afca1a84ee863f51522ee2e2e6788e69aca8c66
1 #! @BASH@
2 # Copyright (C) 1999, 2001 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
19 # 02111-1307 USA.
21 pcprofileso=@SLIBDIR@/libpcprofile.so
22 pcprofiledump=@BINDIR@/pcprofiledump
24 # Print usage message.
25 do_usage() {
26 echo >&2 $"Try \`xtrace --help' for more information."
27 exit 1
30 # Message for missing argument.
31 do_missing_arg() {
32 echo >&2 $"xtrace: option \`$1' requires an argument"
33 do_usage
36 # Print help message
37 do_help() {
38 echo $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...
39 Trace execution of program by printing currently executed function.
41 --data=FILE Don't run the program, just print the data from FILE.
43 -?,--help Print this help and exit
44 --usage Give a short usage message
45 -V,--version Print version information and exit
47 Mandatory arguments to long options are also mandatory for any corresponding
48 short options.
50 Report bugs using the \`glibcbug' script to <bugs@gnu.org>."
51 exit 0
54 do_version() {
55 echo 'xtrace (GNU libc) @VERSION@'
56 echo $"Copyright (C) 2001 Free Software Foundation, Inc.
57 This is free software; see the source for copying conditions. There is NO
58 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
59 Written by Ulrich Drepper."
60 exit 0
63 # Print out function name, file, and line number is a nice formatted way.
64 format_line() {
65 fct=$1
66 file=${2%%:*}
67 line=${2##*:}
68 width=$(expr $COLUMNS - 30)
69 filelen=$(expr length $file)
70 if test "$filelen" -gt "$width"; then
71 rwidth=$(expr $width - 3)
72 file="...$(expr substr $file $(expr 1 + $filelen - $rwidth) $rwidth)"
74 printf '%-20s %-*s %6s\n' $fct $width $file $line
78 # If the variable COLUMNS is not set do this now.
79 COLUMNS=${COLUMNS:-80}
81 # If `TERMINAL_PROG' is not set, set it to `xterm'.
82 TERMINAL_PROG=${TERMINAL_PROG:-xterm}
84 # The data file to process, if any.
85 data=
87 # Process arguments. But stop as soon as the program name is found.
88 while test $# -gt 0; do
89 case "$1" in
90 --d | --da | --dat | --data)
91 if test $# -eq 1; then
92 do_missing_arg $1
94 shift
95 data="$1"
97 --d=* | --da=* | --dat=* | --data=*)
98 data=${1##*=}
100 -? | --h | --he | --hel | --help)
101 do_help
103 --v | --ve | --ver | --vers | --versi | --versio | --version)
104 do_version
107 # Stop processing arguments.
108 shift
109 break
111 --help)
112 do_help
114 --version)
115 do_version
117 --*)
118 echo >&2 $"memprof: unrecognized option \`$1'"
119 do_usage
122 # Unknown option. This means the rest is the program name and parameters.
123 break
125 esac
126 shift
127 done
129 # See whether any arguments are left.
130 if test $# -eq 0; then
131 echo >&2 $"No program name given"
132 do_usage
135 # Determine the program name and check whether it exists.
136 program=$1
137 shift
138 if test ! -f "$program"; then
139 echo >2& $"executable \`$program' not found"
140 do_usage
142 if test ! -x "$program"; then
143 echo >&2 $"\`$program' is no executable"
144 do_usage
147 # We have two modes. If a data file is given simply print the included data.
148 printf "%-20s %-*s %6s\n" Function $(expr $COLUMNS - 30) File Line
149 for i in $(seq 1 $COLUMNS); do echo -n -; done; echo
150 if test -n "$data"; then
151 $pcprofiledump "$data" |
152 sed 's/this = \([^,]*\).*/\1/' |
153 addr2line -fC -e "$program" |
154 while read fct; do
155 read file
156 if test "$fct" != '??' -a "$file" != '??:0'; then
157 format_line $fct $file
159 done
160 else
161 fifo=$(mktemp -u ${TMPDIR:-/tmp}/xprof.XXXXXX)
162 mkfifo -m 0600 $fifo || exit 1
163 # Now start the program and let it write to the FIFO.
164 $TERMINAL_PROG -T "xtrace - $program $*" -e /bin/sh -c "LD_PRELOAD=$pcprofileso PCPROFILE_OUTPUT=$fifo $program $*; read $fifo" &
165 termpid=$!
166 $pcprofiledump $fifo |
167 sed 's/this = \([^,]*\).*/\1/' |
168 addr2line -fC -e $program |
169 while read fct; do
170 read file
171 if test "$fct" != '??' -a "$file" != '??:0'; then
172 format_line $fct $file
174 done
175 read -p "Press return to end the program."
176 echo > $fifo
177 rm $fifo
180 exit 0
181 # Local Variables:
182 # mode:ksh
183 # End: