Update.
[platform/upstream/glibc.git] / debug / xtrace.sh
1 #! @BASH@
2 # Copyright (C) 1999 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=@LIBDIR@/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) 1999 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 `TERM' is not set, set it to `xterm'.
82 TERM=${TERM:-xterm}
83
84 # Process arguments.  But stop as soon as the program name is found.
85 while test $# -gt 0; do
86   case "$1" in
87   --d | --da | --dat | --data)
88     if test $# -eq 1; then
89       do_missing_arg $1
90     fi
91     shift
92     data="$1"
93     ;;
94   --d=* | --da=* | --dat=* | --data=*)
95     data=${1##*=}
96     ;;
97   --)
98     # Stop processing arguments.
99     shift
100     break
101     ;;
102   --*)
103     echo >&2 $"memprof: unrecognized option \`$1'"
104     do_usage
105     ;;
106   *)
107     # Unknown option.  This means the rest is the program name and parameters.
108     break
109     ;;
110   esac
111   shift
112 done
113
114 # See whether any arguments are left.
115 if test $# -eq 0; then
116   echo >&2 $"No program name given"
117   do_usage
118 fi
119
120 # Determine the program name and check whether it exists.
121 program=$1
122 shift
123 if test ! -f "$program"; then
124   echo >2& $"\executable \`$program' not found"
125   do_usage
126 fi
127 if test ! -x "$program"; then
128   echo >&2 $"\`$program' is no executable"
129   do_usage
130 fi
131
132 # We have two modes.  If a data file is given simply print the included data.
133 printf "%-20s  %-*s  %6s\n" Function $(expr $COLUMNS - 30) File Line
134 for i in $(seq 1 $COLUMNS); do echo -n -; done; echo
135 if test -n "$data"; then
136   eval $pcprofiledump $data |
137   sed 's/this = \([^,]*\).*/\1/' |
138   addr2line -fC -e $program |
139   while read fct; do
140     read file
141     if test "$fct" != '??' -a "$file" != '??:0'; then
142       format_line $fct $file
143     fi
144   done
145 else
146   fifo=$(mktemp -u ${TMPDIR:-/tmp}/xprof.XXXXXX)
147   mkfifo -m 0600 $fifo || exit 1
148   # Now start the program and let it write to the FIFO.
149   eval $TERM -T "'xtrace - $program $*'" -e /bin/sh -c "'LD_PRELOAD=$pcprofileso PCPROFILE_OUTPUT=$fifo $program $*; read $fifo'" &
150   termpid=$!
151   eval $pcprofiledump $fifo |
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   read -p "Press return to end the program."
161   echo > $fifo
162   rm $fifo
163 fi
164
165 exit 0
166 # Local Variables:
167 #  mode:ksh
168 # End: