Update copyright year.
[platform/upstream/glibc.git] / debug / xtrace.sh
1 #! @BASH@
2 # Copyright (C) 1999, 2001, 2002, 2003, 2004 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 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.
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 # Lesser General Public License for more details.
15
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.
20
21 pcprofileso=@SLIBDIR@/libpcprofile.so
22 pcprofiledump=@BINDIR@/pcprofiledump
23 TEXTDOMAIN=libc
24
25 # Print usage message.
26 do_usage() {
27   printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
28   exit 0
29 }
30
31 # Refer to --help option.
32 help_info() {
33   printf >&2 $"Try \`xtrace --help' for more information.\n"
34   exit 1
35 }
36
37 # Message for missing argument.
38 do_missing_arg() {
39   printf >&2 $"xtrace: option \`$1' requires an argument.\n"
40   help_info
41 }
42
43 # Print help message
44 do_help() {
45   printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
46   printf $"Trace execution of program by printing currently executed function.
47
48      --data=FILE          Don't run the program, just print the data from FILE.
49
50    -?,--help              Print this help and exit
51       --usage             Give a short usage message
52    -V,--version           Print version information and exit
53
54 Mandatory arguments to long options are also mandatory for any corresponding
55 short options.
56
57 Report bugs using the \`glibcbug' script to <bugs@gnu.org>.\n"
58   exit 0
59 }
60
61 do_version() {
62   echo 'xtrace (GNU libc) @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.
66 " "2004"
67   printf $"Written by %s.
68 " "Ulrich Drepper"
69   exit 0
70 }
71
72 # Print out function name, file, and line number in a nicely formatted way.
73 format_line() {
74   fct=$1
75   file=${2%%:*}
76   line=${2##*:}
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)"
82   fi
83   printf '%-20s  %-*s  %6s\n' $fct $width $file $line
84 }
85
86
87 # If the variable COLUMNS is not set do this now.
88 COLUMNS=${COLUMNS:-80}
89
90 # If `TERMINAL_PROG' is not set, set it to `xterm'.
91 TERMINAL_PROG=${TERMINAL_PROG:-xterm}
92
93 # The data file to process, if any.
94 data=
95
96 # Process arguments.  But stop as soon as the program name is found.
97 while test $# -gt 0; do
98   case "$1" in
99   --d | --da | --dat | --data)
100     if test $# -eq 1; then
101       do_missing_arg $1
102     fi
103     shift
104     data="$1"
105     ;;
106   --d=* | --da=* | --dat=* | --data=*)
107     data=${1##*=}
108     ;;
109   -? | --h | --he | --hel | --help)
110     do_help
111     ;;
112   -V | --v | --ve | --ver | --vers | --versi | --versio | --version)
113     do_version
114     ;;
115   --u | --us | --usa | --usag | --usage)
116     do_usage
117     ;;
118   --)
119     # Stop processing arguments.
120     shift
121     break
122     ;;
123   --*)
124     printf >&2 $"xtrace: unrecognized option \`$1'\n"
125     help_info
126     ;;
127   *)
128     # Unknown option.  This means the rest is the program name and parameters.
129     break
130     ;;
131   esac
132   shift
133 done
134
135 # See whether any arguments are left.
136 if test $# -eq 0; then
137   printf >&2 $"No program name given\n"
138   help_info
139 fi
140
141 # Determine the program name and check whether it exists.
142 program=$1
143 shift
144 if test ! -f "$program"; then
145   printf >2& $"executable \`$program' not found\n"
146   help_info
147 fi
148 if test ! -x "$program"; then
149   printf >&2 $"\`$program' is no executable\n"
150   help_info
151 fi
152
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" |
160   while read fct; do
161     read file
162     if test "$fct" != '??' -a "$file" != '??:0'; then
163       format_line $fct $file
164     fi
165   done
166 else
167   fifo=$(mktemp -u ${TMPDIR:-/tmp}/xtrace.XXXXXX)
168   mkfifo -m 0600 $fifo || exit 1
169   trap 'rm $fifo; exit 1' SIGINT SIGTERM SIGPIPE
170
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" &
173   termpid=$!
174   $pcprofiledump -u $fifo |
175   while read line; do
176      echo $line |
177      sed 's/this = \([^,]*\).*/\1/' |
178      addr2line -fC -e $program
179   done |
180   while read fct; do
181     read file
182     if test "$fct" != '??' -a "$file" != '??:0'; then
183       format_line $fct $file
184     fi
185   done
186   read -p "Press return here to close $TERMINAL_PROG($program)."
187   echo > $fifo
188   rm $fifo
189 fi
190
191 exit 0
192 # Local Variables:
193 #  mode:ksh
194 # End: