Update.
[platform/upstream/glibc.git] / debug / xtrace.sh
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.
5
6 # The GNU C Library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Library General Public License as
8 # published by the Free Software Foundation; either version 2 of the
9 # License, or (at your option) any later version.
10
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 # Library General Public License for more details.
15
16 # You should have received a copy of the GNU Library General Public
17 # License along with the GNU C Library; see the file COPYING.LIB.  If not,
18 # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA.
20
21 pcprofileso=@SLIBDIR@/libpcprofile.so
22 pcprofiledump=@BINDIR@/pcprofiledump
23
24 # Print usage message.
25 do_usage() {
26   echo >&2 $"Try \`xtrace --help' for more information."
27   exit 1
28 }
29
30 # Message for missing argument.
31 do_missing_arg() {
32   echo >&2 $"xtrace: option \`$1' requires an argument"
33   do_usage
34 }
35
36 # Print help message
37 do_help() {
38   echo $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...
39 Trace execution of program by printing currently executed function.
40
41      --data=FILE          Don't run the program, just print the data from FILE.
42
43    -?,--help              Print this help and exit
44       --usage             Give a short usage message
45    -V,--version           Print version information and exit
46
47 Mandatory arguments to long options are also mandatory for any corresponding
48 short options.
49
50 Report bugs using the \`glibcbug' script to <bugs@gnu.org>."
51   exit 0
52 }
53
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
61 }
62
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)"
73   fi
74   printf '%-20s  %-*s  %6s\n' $fct $width $file $line
75 }
76
77
78 # If the variable COLUMNS is not set do this now.
79 COLUMNS=${COLUMNS:-80}
80
81 # If `TERMINAL_PROG' is not set, set it to `xterm'.
82 TERMINAL_PROG=${TERMINAL_PROG:-xterm}
83
84 # The data file to process, if any.
85 data=
86
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
93     fi
94     shift
95     data="$1"
96     ;;
97   --d=* | --da=* | --dat=* | --data=*)
98     data=${1##*=}
99     ;;
100   -? | --h | --he | --hel | --help)
101     do_help
102     ;;
103   --v | --ve | --ver | --vers | --versi | --versio | --version)
104     do_version
105     ;;
106   --)
107     # Stop processing arguments.
108     shift
109     break
110     ;;
111   --help)
112     do_help
113     ;;
114   --version)
115     do_version
116     ;;
117   --*)
118     echo >&2 $"memprof: unrecognized option \`$1'"
119     do_usage
120     ;;
121   *)
122     # Unknown option.  This means the rest is the program name and parameters.
123     break
124     ;;
125   esac
126   shift
127 done
128
129 # See whether any arguments are left.
130 if test $# -eq 0; then
131   echo >&2 $"No program name given"
132   do_usage
133 fi
134
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
141 fi
142 if test ! -x "$program"; then
143   echo >&2 $"\`$program' is no executable"
144   do_usage
145 fi
146
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
158     fi
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
173     fi
174   done
175   read -p "Press return to end the program."
176   echo > $fifo
177   rm $fifo
178 fi
179
180 exit 0
181 # Local Variables:
182 #  mode:ksh
183 # End: