]> git.ipfire.org Git - thirdparty/glibc.git/blob - malloc/mtrace.pl
mtrace: Fix output with PIE and ASLR [BZ #22716]
[thirdparty/glibc.git] / malloc / mtrace.pl
1 #! @PERL@
2 eval "exec @PERL@ -S $0 $@"
3 if 0;
4 # Copyright (C) 1997-2021 Free Software Foundation, Inc.
5 # This file is part of the GNU C Library.
6 # Contributed by Ulrich Drepper <drepper@gnu.org>, 1997.
7 # Based on the mtrace.awk script.
8
9 # The GNU C Library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # The GNU C Library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with the GNU C Library; if not, see
21 # <https://www.gnu.org/licenses/>.
22
23 $VERSION = "@VERSION@";
24 $PKGVERSION = "@PKGVERSION@";
25 $REPORT_BUGS_TO = '@REPORT_BUGS_TO@';
26 $progname = $0;
27
28 sub usage {
29 print "Usage: mtrace [OPTION]... [Binary] MtraceData\n";
30 print " --help print this help, then exit\n";
31 print " --version print version number, then exit\n";
32 print "\n";
33 print "For bug reporting instructions, please see:\n";
34 print "$REPORT_BUGS_TO.\n";
35 exit 0;
36 }
37
38 # We expect two arguments:
39 # #1: the complete path to the binary
40 # #2: the mtrace data filename
41 # The usual options are also recognized.
42
43 arglist: while (@ARGV) {
44 if ($ARGV[0] eq "--v" || $ARGV[0] eq "--ve" || $ARGV[0] eq "--ver" ||
45 $ARGV[0] eq "--vers" || $ARGV[0] eq "--versi" ||
46 $ARGV[0] eq "--versio" || $ARGV[0] eq "--version") {
47 print "mtrace $PKGVERSION$VERSION\n";
48 print "Copyright (C) 2021 Free Software Foundation, Inc.\n";
49 print "This is free software; see the source for copying conditions. There is NO\n";
50 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
51 print "Written by Ulrich Drepper <drepper\@gnu.org>\n";
52
53 exit 0;
54 } elsif ($ARGV[0] eq "--h" || $ARGV[0] eq "--he" || $ARGV[0] eq "--hel" ||
55 $ARGV[0] eq "--help") {
56 &usage;
57 } elsif ($ARGV[0] =~ /^-/) {
58 print "$progname: unrecognized option `$ARGV[0]'\n";
59 print "Try `$progname --help' for more information.\n";
60 exit 1;
61 } else {
62 last arglist;
63 }
64 }
65
66 if ($#ARGV == 0) {
67 $binary="";
68 $data=$ARGV[0];
69 } elsif ($#ARGV == 1) {
70 $binary=$ARGV[0];
71 $data=$ARGV[1];
72
73 if ($binary =~ /^.*[\/].*$/) {
74 $prog = $binary;
75 } else {
76 $prog = "./$binary";
77 }
78 # Set the environment variable LD_TRACE_PRELINKING to an empty string so
79 # that we trigger tracing but do not match with the executable or any of
80 # its dependencies.
81 if (open (LOCS, "env LD_TRACE_PRELINKING= $prog |")) {
82 while (<LOCS>) {
83 chop;
84 if (/^.*=> (.*) \((0x[0123456789abcdef]*), (0x[0123456789abcdef]*).*/) {
85 $locs{$1} = $2;
86 $rel{$1} = hex($2) - hex($3);
87 }
88 }
89 close (LOCS);
90 }
91 } else {
92 die "Wrong number of arguments, run $progname --help for help.";
93 }
94
95 sub location {
96 my $str = pop(@_);
97 return $str if ($str eq "");
98 if ($str =~ /.*[[](0x[^]]*)]:(.)*/) {
99 my $addr = $1;
100 my $fct = $2;
101 return $cache{$addr} if (exists $cache{$addr});
102 if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
103 my $line = <ADDR>;
104 chomp $line;
105 close (ADDR);
106 if ($line ne '??:0') {
107 $cache{$addr} = $line;
108 return $cache{$addr};
109 }
110 }
111 $cache{$addr} = $str = "$fct @ $addr";
112 } elsif ($str =~ /^(.*):.*[[](0x[^]]*)]$/) {
113 my $prog = $1;
114 my $addr = $2;
115 my $searchaddr;
116 return $cache{$addr} if (exists $cache{$addr});
117 $searchaddr = sprintf "%#x", hex($addr) + $rel{$prog};
118 if ($binary ne "" && open (ADDR, "addr2line -e $prog $searchaddr|")) {
119 my $line = <ADDR>;
120 chomp $line;
121 close (ADDR);
122 if ($line ne '??:0') {
123 $cache{$addr} = $line;
124 return $cache{$addr};
125 }
126 }
127 $cache{$addr} = $str = $addr;
128 } elsif ($str =~ /^.*[[](0x[^]]*)]$/) {
129 my $addr = $1;
130 return $cache{$addr} if (exists $cache{$addr});
131 if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
132 my $line = <ADDR>;
133 chomp $line;
134 close (ADDR);
135 if ($line ne '??:0') {
136 $cache{$addr} = $line;
137 return $cache{$addr};
138 }
139 }
140 $cache{$addr} = $str = $addr;
141 }
142 return $str;
143 }
144
145 $nr=0;
146 open(DATA, "<$data") || die "Cannot open mtrace data file";
147 while (<DATA>) {
148 my @cols = split (' ');
149 my $n, $where;
150 if ($cols[0] eq "@") {
151 # We have address and/or function name.
152 $where=$cols[1];
153 $n=2;
154 } else {
155 $where="";
156 $n=0;
157 }
158
159 $allocaddr=$cols[$n + 1];
160 $howmuch=hex($cols[$n + 2]);
161
162 ++$nr;
163 SWITCH: {
164 if ($cols[$n] eq "+") {
165 if (defined $allocated{$allocaddr}) {
166 printf ("+ %#0@XXX@x Alloc %d duplicate: %s %s\n",
167 hex($allocaddr), $nr, &location($addrwas{$allocaddr}),
168 $where);
169 } elsif ($allocaddr =~ /^0x/) {
170 $allocated{$allocaddr}=$howmuch;
171 $addrwas{$allocaddr}=$where;
172 }
173 last SWITCH;
174 }
175 if ($cols[$n] eq "-") {
176 if (defined $allocated{$allocaddr}) {
177 undef $allocated{$allocaddr};
178 undef $addrwas{$allocaddr};
179 } else {
180 printf ("- %#0@XXX@x Free %d was never alloc'd %s\n",
181 hex($allocaddr), $nr, &location($where));
182 }
183 last SWITCH;
184 }
185 if ($cols[$n] eq "<") {
186 if (defined $allocated{$allocaddr}) {
187 undef $allocated{$allocaddr};
188 undef $addrwas{$allocaddr};
189 } else {
190 printf ("- %#0@XXX@x Realloc %d was never alloc'd %s\n",
191 hex($allocaddr), $nr, &location($where));
192 }
193 last SWITCH;
194 }
195 if ($cols[$n] eq ">") {
196 if (defined $allocated{$allocaddr}) {
197 printf ("+ %#0@XXX@x Realloc %d duplicate: %#010x %s %s\n",
198 hex($allocaddr), $nr, $allocated{$allocaddr},
199 &location($addrwas{$allocaddr}), &location($where));
200 } else {
201 $allocated{$allocaddr}=$howmuch;
202 $addrwas{$allocaddr}=$where;
203 }
204 last SWITCH;
205 }
206 if ($cols[$n] eq "=") {
207 # Ignore "= Start".
208 last SWITCH;
209 }
210 if ($cols[$n] eq "!") {
211 # Ignore failed realloc for now.
212 last SWITCH;
213 }
214 }
215 }
216 close (DATA);
217
218 # Now print all remaining entries.
219 @addrs= keys %allocated;
220 $anything=0;
221 if ($#addrs >= 0) {
222 foreach $addr (sort @addrs) {
223 if (defined $allocated{$addr}) {
224 if ($anything == 0) {
225 print "\nMemory not freed:\n-----------------\n";
226 print ' ' x (@XXX@ - 7), "Address Size Caller\n";
227 $anything=1;
228 }
229 printf ("%#0@XXX@x %#8x at %s\n", hex($addr), $allocated{$addr},
230 &location($addrwas{$addr}));
231 }
232 }
233 }
234 print "No memory leaks.\n" if ($anything == 0);
235
236 exit $anything != 0;