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