]> git.ipfire.org Git - thirdparty/glibc.git/blame - debug/xtrace.sh
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / debug / xtrace.sh
CommitLineData
5188a9d0 1#!/bin/bash
6d7e8eda 2# Copyright (C) 1999-2023 Free Software Foundation, Inc.
cab30d75 3# This file is part of the GNU C Library.
cab30d75
UD
4
5# The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
cab30d75
UD
9
10# The GNU C Library is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13# Lesser General Public License for more details.
cab30d75 14
41bdb6e2 15# You should have received a copy of the GNU Lesser General Public
59ba27a6 16# License along with the GNU C Library; if not, see
5a82c748 17# <https://www.gnu.org/licenses/>.
cab30d75 18
f50fa10c
UD
19pcprofileso='@SLIBDIR@/libpcprofile.so'
20pcprofiledump='@BINDIR@/pcprofiledump'
8e597a18 21TEXTDOMAIN=libc
cab30d75
UD
22
23# Print usage message.
24do_usage() {
30224e2b
UD
25 printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
26 exit 0
27}
28
29# Refer to --help option.
30help_info() {
5c0b8d90 31 printf >&2 $"Try \`%s --help' or \`%s --usage' for more information.\n" xtrace xtrace
cab30d75
UD
32 exit 1
33}
34
35# Message for missing argument.
36do_missing_arg() {
de81b246 37 printf >&2 $"%s: option '%s' requires an argument.\n" xtrace "$1"
30224e2b 38 help_info
cab30d75
UD
39}
40
41# Print help message
42do_help() {
30224e2b
UD
43 printf $"Usage: xtrace [OPTION]... PROGRAM [PROGRAMOPTION]...\n"
44 printf $"Trace execution of program by printing currently executed function.
cab30d75
UD
45
46 --data=FILE Don't run the program, just print the data from FILE.
47
48 -?,--help Print this help and exit
49 --usage Give a short usage message
50 -V,--version Print version information and exit
51
52Mandatory arguments to long options are also mandatory for any corresponding
53short options.
54
cbbcaf23 55"
8b748aed
JM
56 printf $"For bug reporting instructions, please see:\\n%s.\\n" \
57 "@REPORT_BUGS_TO@"
cab30d75
UD
58 exit 0
59}
60
61do_version() {
8b748aed 62 echo 'xtrace @PKGVERSION@@VERSION@'
8e597a18 63 printf $"Copyright (C) %s Free Software Foundation, Inc.
cab30d75
UD
64This is free software; see the source for copying conditions. There is NO
65warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
634b5eba 66" "2022"
8e597a18
UD
67 printf $"Written by %s.
68" "Ulrich Drepper"
cab30d75
UD
69 exit 0
70}
71
30224e2b 72# Print out function name, file, and line number in a nicely formatted way.
cab30d75
UD
73format_line() {
74 fct=$1
75 file=${2%%:*}
76 line=${2##*:}
77 width=$(expr $COLUMNS - 30)
78 filelen=$(expr length $file)
79 if test "$filelen" -gt "$width"; then
80 rwidth=$(expr $width - 3)
81 file="...$(expr substr $file $(expr 1 + $filelen - $rwidth) $rwidth)"
82 fi
83 printf '%-20s %-*s %6s\n' $fct $width $file $line
84}
85
86
87# If the variable COLUMNS is not set do this now.
88COLUMNS=${COLUMNS:-80}
89
4d3a563f
UD
90# If `TERMINAL_PROG' is not set, set it to `xterm'.
91TERMINAL_PROG=${TERMINAL_PROG:-xterm}
cab30d75 92
b5c6276a
UD
93# The data file to process, if any.
94data=
95
cab30d75
UD
96# Process arguments. But stop as soon as the program name is found.
97while test $# -gt 0; do
98 case "$1" in
99 --d | --da | --dat | --data)
100 if test $# -eq 1; then
101 do_missing_arg $1
102 fi
103 shift
104 data="$1"
105 ;;
106 --d=* | --da=* | --dat=* | --data=*)
107 data=${1##*=}
108 ;;
2009127c 109 -\? | --h | --he | --hel | --help)
b5c6276a
UD
110 do_help
111 ;;
30224e2b 112 -V | --v | --ve | --ver | --vers | --versi | --versio | --version)
b5c6276a
UD
113 do_version
114 ;;
30224e2b
UD
115 --u | --us | --usa | --usag | --usage)
116 do_usage
117 ;;
cab30d75
UD
118 --)
119 # Stop processing arguments.
120 shift
121 break
122 ;;
123 --*)
30224e2b
UD
124 printf >&2 $"xtrace: unrecognized option \`$1'\n"
125 help_info
cab30d75
UD
126 ;;
127 *)
128 # Unknown option. This means the rest is the program name and parameters.
129 break
130 ;;
131 esac
132 shift
133done
134
135# See whether any arguments are left.
136if test $# -eq 0; then
30224e2b
UD
137 printf >&2 $"No program name given\n"
138 help_info
cab30d75
UD
139fi
140
141# Determine the program name and check whether it exists.
142program=$1
143shift
144if test ! -f "$program"; then
0ab7f77e 145 printf >&2 $"executable \`$program' not found\n"
30224e2b 146 help_info
cab30d75
UD
147fi
148if test ! -x "$program"; then
30224e2b
UD
149 printf >&2 $"\`$program' is no executable\n"
150 help_info
cab30d75
UD
151fi
152
153# We have two modes. If a data file is given simply print the included data.
154printf "%-20s %-*s %6s\n" Function $(expr $COLUMNS - 30) File Line
30224e2b 155for i in $(seq 1 $COLUMNS); do printf -; done; printf '\n'
cab30d75 156if test -n "$data"; then
b5c6276a 157 $pcprofiledump "$data" |
cab30d75 158 sed 's/this = \([^,]*\).*/\1/' |
b5c6276a 159 addr2line -fC -e "$program" |
cab30d75
UD
160 while read fct; do
161 read file
162 if test "$fct" != '??' -a "$file" != '??:0'; then
07fb5185 163 format_line "$fct" "$file"
cab30d75
UD
164 fi
165 done
166else
07fb5185
UD
167 fifo=$(mktemp -ut xtrace.XXXXXX) || exit
168 trap 'rm -f "$fifo"; exit 1' HUP INT QUIT TERM PIPE
cab30d75 169 mkfifo -m 0600 $fifo || exit 1
30224e2b 170
cab30d75 171 # Now start the program and let it write to the FIFO.
30224e2b 172 $TERMINAL_PROG -T "xtrace - $program $*" -e /bin/sh -c "LD_PRELOAD=$pcprofileso PCPROFILE_OUTPUT=$fifo $program $*; read < $fifo" &
cab30d75 173 termpid=$!
07fb5185 174 $pcprofiledump -u "$fifo" |
30224e2b 175 while read line; do
07fb5185 176 echo "$line" |
30224e2b 177 sed 's/this = \([^,]*\).*/\1/' |
07fb5185 178 addr2line -fC -e "$program"
30224e2b 179 done |
cab30d75
UD
180 while read fct; do
181 read file
182 if test "$fct" != '??' -a "$file" != '??:0'; then
07fb5185 183 format_line "$fct" "$file"
cab30d75
UD
184 fi
185 done
30224e2b 186 read -p "Press return here to close $TERMINAL_PROG($program)."
07fb5185
UD
187 echo > "$fifo"
188 rm "$fifo"
cab30d75
UD
189fi
190
191exit 0
192# Local Variables:
193# mode:ksh
194# End: