1999-10-07 15:25:16 +08:00
|
|
|
#! @BASH@
|
2009-01-02 16:47:18 +08:00
|
|
|
# Copyright (C) 1999, 2001-2008, 2009 Free Software Foundation, Inc.
|
1999-10-07 15:25:16 +08:00
|
|
|
# This file is part of the GNU C Library.
|
|
|
|
# Contributed by Ulrich Drepper <drepper@gnu.org>, 1999.
|
|
|
|
|
|
|
|
# The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 12:58:11 +08:00
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
1999-10-07 15:25:16 +08:00
|
|
|
|
|
|
|
# The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 12:58:11 +08:00
|
|
|
# Lesser General Public License for more details.
|
1999-10-07 15:25:16 +08:00
|
|
|
|
2001-07-06 12:58:11 +08:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with the GNU C Library; if not, write to the Free
|
|
|
|
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
# 02111-1307 USA.
|
1999-10-07 15:25:16 +08:00
|
|
|
|
2006-05-20 00:48:27 +08:00
|
|
|
pcprofileso='@SLIBDIR@/libpcprofile.so'
|
|
|
|
pcprofiledump='@BINDIR@/pcprofiledump'
|
2004-01-02 04:42:01 +08:00
|
|
|
TEXTDOMAIN=libc
|
1999-10-07 15:25:16 +08:00
|
|
|
|
|
|
|
# Print usage message.
|
|
|
|
do_usage() {
|
2002-05-15 11:36:41 +08:00
|
|
|
printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# Refer to --help option.
|
|
|
|
help_info() {
|
|
|
|
printf >&2 $"Try \`xtrace --help' for more information.\n"
|
1999-10-07 15:25:16 +08:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Message for missing argument.
|
|
|
|
do_missing_arg() {
|
2002-05-15 11:36:41 +08:00
|
|
|
printf >&2 $"xtrace: option \`$1' requires an argument.\n"
|
|
|
|
help_info
|
1999-10-07 15:25:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Print help message
|
|
|
|
do_help() {
|
2002-05-15 11:36:41 +08:00
|
|
|
printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
|
|
|
|
printf $"Trace execution of program by printing currently executed function.
|
1999-10-07 15:25:16 +08:00
|
|
|
|
|
|
|
--data=FILE Don't run the program, just print the data from FILE.
|
|
|
|
|
|
|
|
-?,--help Print this help and exit
|
|
|
|
--usage Give a short usage message
|
|
|
|
-V,--version Print version information and exit
|
|
|
|
|
|
|
|
Mandatory arguments to long options are also mandatory for any corresponding
|
|
|
|
short options.
|
|
|
|
|
2009-02-07 04:13:07 +08:00
|
|
|
"
|
|
|
|
printf $"For bug reporting instructions, please see:
|
|
|
|
<http://www.gnu.org/software/libc/bugs.html>.
|
|
|
|
"
|
1999-10-07 15:25:16 +08:00
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
do_version() {
|
|
|
|
echo 'xtrace (GNU libc) @VERSION@'
|
2004-01-02 04:42:01 +08:00
|
|
|
printf $"Copyright (C) %s Free Software Foundation, Inc.
|
1999-10-07 15:25:16 +08:00
|
|
|
This is free software; see the source for copying conditions. There is NO
|
|
|
|
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
2009-01-02 16:47:18 +08:00
|
|
|
" "2009"
|
2004-01-02 04:42:01 +08:00
|
|
|
printf $"Written by %s.
|
|
|
|
" "Ulrich Drepper"
|
1999-10-07 15:25:16 +08:00
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2002-05-15 11:36:41 +08:00
|
|
|
# Print out function name, file, and line number in a nicely formatted way.
|
1999-10-07 15:25:16 +08:00
|
|
|
format_line() {
|
|
|
|
fct=$1
|
|
|
|
file=${2%%:*}
|
|
|
|
line=${2##*:}
|
|
|
|
width=$(expr $COLUMNS - 30)
|
|
|
|
filelen=$(expr length $file)
|
|
|
|
if test "$filelen" -gt "$width"; then
|
|
|
|
rwidth=$(expr $width - 3)
|
|
|
|
file="...$(expr substr $file $(expr 1 + $filelen - $rwidth) $rwidth)"
|
|
|
|
fi
|
|
|
|
printf '%-20s %-*s %6s\n' $fct $width $file $line
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# If the variable COLUMNS is not set do this now.
|
|
|
|
COLUMNS=${COLUMNS:-80}
|
|
|
|
|
1999-10-19 11:14:36 +08:00
|
|
|
# If `TERMINAL_PROG' is not set, set it to `xterm'.
|
|
|
|
TERMINAL_PROG=${TERMINAL_PROG:-xterm}
|
1999-10-07 15:25:16 +08:00
|
|
|
|
1999-10-09 01:16:17 +08:00
|
|
|
# The data file to process, if any.
|
|
|
|
data=
|
|
|
|
|
1999-10-07 15:25:16 +08:00
|
|
|
# Process arguments. But stop as soon as the program name is found.
|
|
|
|
while test $# -gt 0; do
|
|
|
|
case "$1" in
|
|
|
|
--d | --da | --dat | --data)
|
|
|
|
if test $# -eq 1; then
|
|
|
|
do_missing_arg $1
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
data="$1"
|
|
|
|
;;
|
|
|
|
--d=* | --da=* | --dat=* | --data=*)
|
|
|
|
data=${1##*=}
|
|
|
|
;;
|
2005-09-18 01:31:56 +08:00
|
|
|
-\? | --h | --he | --hel | --help)
|
1999-10-09 01:16:17 +08:00
|
|
|
do_help
|
|
|
|
;;
|
2002-05-15 11:36:41 +08:00
|
|
|
-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
|
1999-10-09 01:16:17 +08:00
|
|
|
do_version
|
|
|
|
;;
|
2002-05-15 11:36:41 +08:00
|
|
|
--u | --us | --usa | --usag | --usage)
|
|
|
|
do_usage
|
|
|
|
;;
|
1999-10-07 15:25:16 +08:00
|
|
|
--)
|
|
|
|
# Stop processing arguments.
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
--*)
|
2002-05-15 11:36:41 +08:00
|
|
|
printf >&2 $"xtrace: unrecognized option \`$1'\n"
|
|
|
|
help_info
|
1999-10-07 15:25:16 +08:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# Unknown option. This means the rest is the program name and parameters.
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
# See whether any arguments are left.
|
|
|
|
if test $# -eq 0; then
|
2002-05-15 11:36:41 +08:00
|
|
|
printf >&2 $"No program name given\n"
|
|
|
|
help_info
|
1999-10-07 15:25:16 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Determine the program name and check whether it exists.
|
|
|
|
program=$1
|
|
|
|
shift
|
|
|
|
if test ! -f "$program"; then
|
2004-06-16 04:10:24 +08:00
|
|
|
printf >&2 $"executable \`$program' not found\n"
|
2002-05-15 11:36:41 +08:00
|
|
|
help_info
|
1999-10-07 15:25:16 +08:00
|
|
|
fi
|
|
|
|
if test ! -x "$program"; then
|
2002-05-15 11:36:41 +08:00
|
|
|
printf >&2 $"\`$program' is no executable\n"
|
|
|
|
help_info
|
1999-10-07 15:25:16 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# We have two modes. If a data file is given simply print the included data.
|
|
|
|
printf "%-20s %-*s %6s\n" Function $(expr $COLUMNS - 30) File Line
|
2002-05-15 11:36:41 +08:00
|
|
|
for i in $(seq 1 $COLUMNS); do printf -; done; printf '\n'
|
1999-10-07 15:25:16 +08:00
|
|
|
if test -n "$data"; then
|
1999-10-09 01:16:17 +08:00
|
|
|
$pcprofiledump "$data" |
|
1999-10-07 15:25:16 +08:00
|
|
|
sed 's/this = \([^,]*\).*/\1/' |
|
1999-10-09 01:16:17 +08:00
|
|
|
addr2line -fC -e "$program" |
|
1999-10-07 15:25:16 +08:00
|
|
|
while read fct; do
|
|
|
|
read file
|
|
|
|
if test "$fct" != '??' -a "$file" != '??:0'; then
|
2007-02-17 16:36:28 +08:00
|
|
|
format_line "$fct" "$file"
|
1999-10-07 15:25:16 +08:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
2007-02-17 16:36:28 +08:00
|
|
|
fifo=$(mktemp -ut xtrace.XXXXXX) || exit
|
|
|
|
trap 'rm -f "$fifo"; exit 1' HUP INT QUIT TERM PIPE
|
1999-10-07 15:25:16 +08:00
|
|
|
mkfifo -m 0600 $fifo || exit 1
|
2002-05-15 11:36:41 +08:00
|
|
|
|
1999-10-07 15:25:16 +08:00
|
|
|
# Now start the program and let it write to the FIFO.
|
2002-05-15 11:36:41 +08:00
|
|
|
$TERMINAL_PROG -T "xtrace - $program $*" -e /bin/sh -c "LD_PRELOAD=$pcprofileso PCPROFILE_OUTPUT=$fifo $program $*; read < $fifo" &
|
1999-10-07 15:25:16 +08:00
|
|
|
termpid=$!
|
2007-02-17 16:36:28 +08:00
|
|
|
$pcprofiledump -u "$fifo" |
|
2002-05-15 11:36:41 +08:00
|
|
|
while read line; do
|
2007-02-17 16:36:28 +08:00
|
|
|
echo "$line" |
|
2002-05-15 11:36:41 +08:00
|
|
|
sed 's/this = \([^,]*\).*/\1/' |
|
2007-02-17 16:36:28 +08:00
|
|
|
addr2line -fC -e "$program"
|
2002-05-15 11:36:41 +08:00
|
|
|
done |
|
1999-10-07 15:25:16 +08:00
|
|
|
while read fct; do
|
|
|
|
read file
|
|
|
|
if test "$fct" != '??' -a "$file" != '??:0'; then
|
2007-02-17 16:36:28 +08:00
|
|
|
format_line "$fct" "$file"
|
1999-10-07 15:25:16 +08:00
|
|
|
fi
|
|
|
|
done
|
2002-05-15 11:36:41 +08:00
|
|
|
read -p "Press return here to close $TERMINAL_PROG($program)."
|
2007-02-17 16:36:28 +08:00
|
|
|
echo > "$fifo"
|
|
|
|
rm "$fifo"
|
1999-10-07 15:25:16 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
# Local Variables:
|
|
|
|
# mode:ksh
|
|
|
|
# End:
|