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