glibc/debug/xtrace.sh

195 lines
5.1 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# Copyright (C) 1999-2024 Free Software Foundation, Inc.
# This file is part of the GNU C Library.
# The GNU C Library is free software; you can redistribute it and/or
# 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.
# 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
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
# <https://www.gnu.org/licenses/>.
pcprofileso='@SLIBDIR@/libpcprofile.so'
pcprofiledump='@BINDIR@/pcprofiledump'
2004-01-02 04:42:01 +08:00
TEXTDOMAIN=libc
# Print usage message.
do_usage() {
printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
exit 0
}
# Refer to --help option.
help_info() {
printf >&2 $"Try \`%s --help' or \`%s --usage' for more information.\n" xtrace xtrace
exit 1
}
# Message for missing argument.
do_missing_arg() {
printf >&2 $"%s: option '%s' requires an argument.\n" xtrace "$1"
help_info
}
# Print help message
do_help() {
printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
printf $"Trace execution of program by printing currently executed function.
--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.
"
printf $"For bug reporting instructions, please see:\\n%s.\\n" \
"@REPORT_BUGS_TO@"
exit 0
}
do_version() {
echo 'xtrace @PKGVERSION@@VERSION@'
2004-01-02 04:42:01 +08:00
printf $"Copyright (C) %s Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
" "2024"
2004-01-02 04:42:01 +08:00
printf $"Written by %s.
" "Ulrich Drepper"
exit 0
}
# Print out function name, file, and line number in a nicely formatted way.
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}
Update. 1999-10-18 Ulrich Drepper <drepper@cygnus.com> * scripts/config.sub: Update from latest autoconf version. * scripts/config.guess: Likewise. 1999-10-18 Andreas Jaeger <aj@suse.de> * inet/arpa/inet.h: Remove K&R compatibility. * math/fenv.h: Likewise. 1999-10-18 Andreas Jaeger <aj@suse.de> * sysdeps/unix/sysv/linux/kernel_sigaction.h (struct kernel_sigaction): Remove K&R compatibility. * sysdeps/unix/sysv/linux/net/if.h: Likewise. * sysdeps/unix/sysv/linux/sys/acct.h: Likewise. * sysdeps/unix/sysv/linux/sys/fsuid.h: Likewise. * sysdeps/unix/sysv/linux/sys/io.h: Likewise. * sysdeps/unix/sysv/linux/sys/kdaemon.h: Likewise. * sysdeps/unix/sysv/linux/sys/klog.h: Likewise. * sysdeps/unix/sysv/linux/sys/mount.h: Likewise. * sysdeps/unix/sysv/linux/sys/prctl.h: Likewise. * sysdeps/unix/sysv/linux/sys/ptrace.h: Likewise. * sysdeps/unix/sysv/linux/sys/quota.h: Likewise. * sysdeps/unix/sysv/linux/sys/sendfile.h: Likewise. * sysdeps/unix/sysv/linux/sys/swap.h: Likewise. * sysdeps/unix/sysv/linux/sys/sysctl.h: Likewise. * sysdeps/unix/sysv/linux/sys/sysinfo.h: Likewise. * sysdeps/unix/sysv/linux/sys/timex.h: Likewise. * sysdeps/unix/sysv/linux/i386/sys/vm86.h: Likewise. * sysdeps/unix/sysv/linux/i386/sys/perm.h: Likewise. 1999-10-18 Andreas Jaeger <aj@suse.de> * iconv/iconv_prog.c (process_block): Save errno value from iconv call. Patch by Bruno Haible <haible@ilog.fr> [libc/1402]. 1999-10-18 Ulrich Drepper <drepper@cygnus.com> * po/gl.po: New file. 1999-10-18 Andreas Schwab <schwab@suse.de> * sysdeps/unix/sysv/linux/alpha/bits/sigaction.h: Correct namespace selection. 1999-10-18 Andreas Schwab <schwab@suse.de> * debug/xtrace.sh: Use TERMINAL_PROG instead of TERM to avoid clash with the TERM environment variable. 1999-10-18 Andreas Jaeger <aj@suse.de> * sysdeps/generic/s_cproj.c (__cproj): Fix bug: NaN + i NaN returns now NaN + i NaN. * sysdeps/generic/s_cprojf.c (__cprojf): Likewise * sysdeps/generic/s_cprojl.c (__cprojl): Likewise. 1999-10-18 Andreas Jaeger <aj@suse.de> * include/aio.h: Remove K&R compatiblity. * include/alloca.h: Likewise. * include/db.h: Likewise. * include/dirent.h: Likewise. * include/fcntl.h: Likewise. * include/fpu_control.h: Likewise. * include/glob.h: Likewise. * include/grp.h: Likewise. * sysdeps/unix/sysv/linux/bits/errno.h: Likewise. * sysdeps/unix/sysv/linux/bits/sched.h: Likewise. * sysdeps/unix/sysv/linux/bits/siginfo.h: Likewise. * sysdeps/unix/sysv/linux/bits/socket.h: Likewise. * include/execinfo.h: Add __THROW. 1999-10-18 Ulrich Drepper <drepper@cygnus.com> * sysdeps/mips/Implies: Remove wordsize-32 reference. * sysdeps/mips/mips3/Implies: Add it here.
1999-10-19 11:14:36 +08:00
# If `TERMINAL_PROG' is not set, set it to `xterm'.
TERMINAL_PROG=${TERMINAL_PROG:-xterm}
# The data file to process, if any.
data=
# 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##*=}
;;
-\? | --h | --he | --hel | --help)
do_help
;;
-V | --v | --ve | --ver | --vers | --versi | --versio | --version)
do_version
;;
--u | --us | --usa | --usag | --usage)
do_usage
;;
--)
# Stop processing arguments.
shift
break
;;
--*)
printf >&2 $"xtrace: unrecognized option \`$1'\n"
help_info
;;
*)
# 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
printf >&2 $"No program name given\n"
help_info
fi
# Determine the program name and check whether it exists.
program=$1
shift
if test ! -f "$program"; then
printf >&2 $"executable \`$program' not found\n"
help_info
fi
if test ! -x "$program"; then
printf >&2 $"\`$program' is no executable\n"
help_info
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
for i in $(seq 1 $COLUMNS); do printf -; done; printf '\n'
if test -n "$data"; then
$pcprofiledump "$data" |
sed 's/this = \([^,]*\).*/\1/' |
addr2line -fC -e "$program" |
while read fct; do
read file
if test "$fct" != '??' -a "$file" != '??:0'; then
format_line "$fct" "$file"
fi
done
else
fifo=$(mktemp -ut xtrace.XXXXXX) || exit
trap 'rm -f "$fifo"; exit 1' HUP INT QUIT TERM PIPE
mkfifo -m 0600 $fifo || exit 1
# Now start the program and let it write to the FIFO.
$TERMINAL_PROG -T "xtrace - $program $*" -e /bin/sh -c "LD_PRELOAD=$pcprofileso PCPROFILE_OUTPUT=$fifo $program $*; read < $fifo" &
termpid=$!
$pcprofiledump -u "$fifo" |
while read line; do
echo "$line" |
sed 's/this = \([^,]*\).*/\1/' |
addr2line -fC -e "$program"
done |
while read fct; do
read file
if test "$fct" != '??' -a "$file" != '??:0'; then
format_line "$fct" "$file"
fi
done
read -p "Press return here to close $TERMINAL_PROG($program)."
echo > "$fifo"
rm "$fifo"
fi
exit 0
# Local Variables:
# mode:ksh
# End: