Remove unused localtime makefile variables.
[platform/upstream/glibc.git] / debug / xtrace.sh
1 #! @BASH@
2 # Copyright (C) 1999, 2001-2011, 2012 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, see
18 # <http://www.gnu.org/licenses/>.
19
20 pcprofileso='@SLIBDIR@/libpcprofile.so'
21 pcprofiledump='@BINDIR@/pcprofiledump'
22 TEXTDOMAIN=libc
23
24 # Print usage message.
25 do_usage() {
26   printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
27   exit 0
28 }
29
30 # Refer to --help option.
31 help_info() {
32   printf >&2 $"Try \`%s --help' or \`%s --usage' for more information.\n" xtrace xtrace
33   exit 1
34 }
35
36 # Message for missing argument.
37 do_missing_arg() {
38   printf >&2 $"%s: option '%s' requires an argument.\n" xtrace "$1"
39   help_info
40 }
41
42 # Print help message
43 do_help() {
44   printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
45   printf $"Trace execution of program by printing currently executed function.
46
47      --data=FILE          Don't run the program, just print the data from FILE.
48
49    -?,--help              Print this help and exit
50       --usage             Give a short usage message
51    -V,--version           Print version information and exit
52
53 Mandatory arguments to long options are also mandatory for any corresponding
54 short options.
55
56 "
57   echo $"For bug reporting instructions, please see:
58 <http://www.gnu.org/software/libc/bugs.html>.
59 "
60   exit 0
61 }
62
63 do_version() {
64   echo 'xtrace (GNU libc) @VERSION@'
65   printf $"Copyright (C) %s Free Software Foundation, Inc.
66 This is free software; see the source for copying conditions.  There is NO
67 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
68 " "2012"
69   printf $"Written by %s.
70 " "Ulrich Drepper"
71   exit 0
72 }
73
74 # Print out function name, file, and line number in a nicely formatted way.
75 format_line() {
76   fct=$1
77   file=${2%%:*}
78   line=${2##*:}
79   width=$(expr $COLUMNS - 30)
80   filelen=$(expr length $file)
81   if test "$filelen" -gt "$width"; then
82     rwidth=$(expr $width - 3)
83     file="...$(expr substr $file $(expr 1 + $filelen - $rwidth) $rwidth)"
84   fi
85   printf '%-20s  %-*s  %6s\n' $fct $width $file $line
86 }
87
88
89 # If the variable COLUMNS is not set do this now.
90 COLUMNS=${COLUMNS:-80}
91
92 # If `TERMINAL_PROG' is not set, set it to `xterm'.
93 TERMINAL_PROG=${TERMINAL_PROG:-xterm}
94
95 # The data file to process, if any.
96 data=
97
98 # Process arguments.  But stop as soon as the program name is found.
99 while test $# -gt 0; do
100   case "$1" in
101   --d | --da | --dat | --data)
102     if test $# -eq 1; then
103       do_missing_arg $1
104     fi
105     shift
106     data="$1"
107     ;;
108   --d=* | --da=* | --dat=* | --data=*)
109     data=${1##*=}
110     ;;
111   -\? | --h | --he | --hel | --help)
112     do_help
113     ;;
114   -V | --v | --ve | --ver | --vers | --versi | --versio | --version)
115     do_version
116     ;;
117   --u | --us | --usa | --usag | --usage)
118     do_usage
119     ;;
120   --)
121     # Stop processing arguments.
122     shift
123     break
124     ;;
125   --*)
126     printf >&2 $"xtrace: unrecognized option \`$1'\n"
127     help_info
128     ;;
129   *)
130     # Unknown option.  This means the rest is the program name and parameters.
131     break
132     ;;
133   esac
134   shift
135 done
136
137 # See whether any arguments are left.
138 if test $# -eq 0; then
139   printf >&2 $"No program name given\n"
140   help_info
141 fi
142
143 # Determine the program name and check whether it exists.
144 program=$1
145 shift
146 if test ! -f "$program"; then
147   printf >&2 $"executable \`$program' not found\n"
148   help_info
149 fi
150 if test ! -x "$program"; then
151   printf >&2 $"\`$program' is no executable\n"
152   help_info
153 fi
154
155 # We have two modes.  If a data file is given simply print the included data.
156 printf "%-20s  %-*s  %6s\n" Function $(expr $COLUMNS - 30) File Line
157 for i in $(seq 1 $COLUMNS); do printf -; done; printf '\n'
158 if test -n "$data"; then
159   $pcprofiledump "$data" |
160   sed 's/this = \([^,]*\).*/\1/' |
161   addr2line -fC -e "$program" |
162   while read fct; do
163     read file
164     if test "$fct" != '??' -a "$file" != '??:0'; then
165       format_line "$fct" "$file"
166     fi
167   done
168 else
169   fifo=$(mktemp -ut xtrace.XXXXXX) || exit
170   trap 'rm -f "$fifo"; exit 1' HUP INT QUIT TERM PIPE
171   mkfifo -m 0600 $fifo || exit 1
172
173   # Now start the program and let it write to the FIFO.
174   $TERMINAL_PROG -T "xtrace - $program $*" -e /bin/sh -c "LD_PRELOAD=$pcprofileso PCPROFILE_OUTPUT=$fifo $program $*; read < $fifo" &
175   termpid=$!
176   $pcprofiledump -u "$fifo" |
177   while read line; do
178      echo "$line" |
179      sed 's/this = \([^,]*\).*/\1/' |
180      addr2line -fC -e "$program"
181   done |
182   while read fct; do
183     read file
184     if test "$fct" != '??' -a "$file" != '??:0'; then
185       format_line "$fct" "$file"
186     fi
187   done
188   read -p "Press return here to close $TERMINAL_PROG($program)."
189   echo > "$fifo"
190   rm "$fifo"
191 fi
192
193 exit 0
194 # Local Variables:
195 #  mode:ksh
196 # End: