#! @BASH@ # Copyright (C) 1999-2016 Free Software Foundation, Inc. # This file is part of the GNU C Library. # Contributed by Ulrich Drepper , 1999. # 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 # . setglobal pcprofileso = ''@SLIBDIR@/libpcprofile.so'' setglobal pcprofiledump = ''@BINDIR@/pcprofiledump'' setglobal TEXTDOMAIN = 'libc' # Print usage message. proc do_usage { printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n" exit 0 } # Refer to --help option. proc help_info { printf >&2 $"Try \`%s --help' or \`%s --usage' for more information.\n" xtrace xtrace> !2 $"Try \`%s --help' or \`%s --usage' for more information.\n" xtrace xtrace exit 1 } # Message for missing argument. proc do_missing_arg { printf >&2 $"%s: option '%s' requires an argument.\n" xtrace $1> !2 $"%s: option '%s' requires an argument.\n" xtrace "$1" help_info } # Print help message proc 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 } proc do_version { echo 'xtrace @PKGVERSION@@VERSION@' 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. " "2016" printf $"Written by %s. " "Ulrich Drepper" exit 0 } # Print out function name, file, and line number in a nicely formatted way. proc format_line { setglobal fct = $1 setglobal file = $(2%%:*) setglobal line = $(2##*:) setglobal width = $[expr $COLUMNS - 30] setglobal filelen = $[expr length $file] if test $filelen -gt $width { setglobal rwidth = $[expr $width - 3] setglobal file = ""...$[expr substr $file $[expr 1 + $filelen - $rwidth] $rwidth]"" } printf '%-20s %-*s %6s\n' $fct $width $file $line } # If the variable COLUMNS is not set do this now. setglobal COLUMNS = $(COLUMNS:-80) # If `TERMINAL_PROG' is not set, set it to `xterm'. setglobal TERMINAL_PROG = $(TERMINAL_PROG:-xterm) # The data file to process, if any. setglobal data = '' # Process arguments. But stop as soon as the program name is found. while test $Argc -gt 0 { match $1 { with --d | --da | --dat | --data if test $Argc -eq 1 { do_missing_arg $1 } shift setglobal data = $1 with --d=* | --da=* | --dat=* | --data=* setglobal data = $(1##*=) with -\? | --h | --he | --hel | --help do_help with -V | --v | --ve | --ver | --vers | --versi | --versio | --version do_version with --u | --us | --usa | --usag | --usage do_usage with -- # Stop processing arguments. shift break with --* printf >&2 $"xtrace: unrecognized option \`$1'\n> !2 $"xtrace: unrecognized option \`$1'\n" help_info with * # Unknown option. This means the rest is the program name and parameters. break } shift } # See whether any arguments are left. if test $Argc -eq 0 { printf >&2 $"No program name given\n> !2 $"No program name given\n" help_info } # Determine the program name and check whether it exists. setglobal program = $1 shift if test ! -f $program { printf >&2 $"executable \`$program' not found\n> !2 $"executable \`$program' not found\n" help_info } if test ! -x $program { printf >&2 $"\`$program' is no executable\n> !2 $"\`$program' is no executable\n" help_info } # 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]] { printf -; }; printf '\n' if test -n $data { $pcprofiledump $data | sed 's/this = \([^,]*\).*/\1/' | addr2line -fC -e $program | while read fct { read file if test $fct != '??' -a $file != '??:0' { format_line $fct $file } } } else { setglobal 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 $ifsjoin(Argv)" -e /bin/sh -c "LD_PRELOAD=$pcprofileso PCPROFILE_OUTPUT=$fifo $program $ifsjoin(Argv); read < $fifo" & setglobal termpid = $BgPid $pcprofiledump -u $fifo | while read line { echo $line | sed 's/this = \([^,]*\).*/\1/' | addr2line -fC -e $program } | while read fct { read file if test $fct != '??' -a $file != '??:0' { format_line $fct $file } } read -p "Press return here to close $TERMINAL_PROG($program)." echo > $fifo rm $fifo } exit 0 # Local Variables: # mode:ksh # End: