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