Update.
[glibc.git] / malloc / mtrace.pl
blob5db3290dd5b2755f0fa0f9b3b82c372dd9d4b467
1 #! @PERL@
2 eval "exec @PERL@ -S $0 $*"
3 if 0;
4 # Copyright (C) 1997,1998,1999,2000,2001,2002 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.
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.
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.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with the GNU C Library; if not, write to the Free
21 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # 02111-1307 USA.
24 $VERSION = "@VERSION@";
25 $PACKAGE = "libc";
26 $progname = $0;
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 "Report bugs using the `glibcbug' script to <bugs\@gnu.org>.\n";
34 exit 0;
37 # We expect two arguments:
38 # #1: the complete path to the binary
39 # #2: the mtrace data filename
40 # The usual options are also recognized.
42 arglist: while (@ARGV) {
43 if ($ARGV[0] eq "--v" || $ARGV[0] eq "--ve" || $ARGV[0] eq "--ver" ||
44 $ARGV[0] eq "--vers" || $ARGV[0] eq "--versi" ||
45 $ARGV[0] eq "--versio" || $ARGV[0] eq "--version") {
46 print "mtrace (GNU $PACKAGE) $VERSION\n";
47 print "Copyright (C) 2002 Free Software Foundation, Inc.\n";
48 print "This is free software; see the source for copying conditions. There is NO\n";
49 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
50 print "Written by Ulrich Drepper <drepper\@gnu.org>\n";
52 exit 0;
53 } elsif ($ARGV[0] eq "--h" || $ARGV[0] eq "--he" || $ARGV[0] eq "--hel" ||
54 $ARGV[0] eq "--help") {
55 &usage;
56 } elsif ($ARGV[0] =~ /^-/) {
57 print "$progname: unrecognized option `$ARGV[0]'\n";
58 print "Try `$progname --help' for more information.\n";
59 exit 1;
60 } else {
61 last arglist;
65 if ($#ARGV == 0) {
66 $binary="";
67 $data=$ARGV[0];
68 } elsif ($#ARGV == 1) {
69 $binary=$ARGV[0];
70 $data=$ARGV[1];
72 if ($binary =~ /^.*[\/].*$/) {
73 $prog = $binary;
74 } else {
75 $prog = "./$binary";
77 if (open (LOCS, "env LD_TRACE_LOADED_OBJECTS=1 $prog |")) {
78 while (<LOCS>) {
79 chop;
80 if (/^.*=> (.*) .(0x[0123456789abcdef]*).$/) {
81 $locs{$1} = $2;
84 close (LOCS);
86 } else {
87 die "Wrong number of arguments, run $progname --help for help.";
90 sub location {
91 my $str = pop(@_);
92 return $str if ($str eq "");
93 if ($str =~ /.*[[](0x[^]]*)]:(.)*/) {
94 my $addr = $1;
95 my $fct = $2;
96 return $cache{$addr} if (exists $cache{$addr});
97 if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
98 my $line = <ADDR>;
99 chomp $line;
100 close (ADDR);
101 if ($line ne '??:0') {
102 $cache{$addr} = $line;
103 return $cache{$addr};
106 $cache{$addr} = $str = "$fct @ $addr";
107 } elsif ($str =~ /^(.*):.*[[](0x[^]]*)]$/) {
108 my $prog = $1;
109 my $addr = $2;
110 my $searchaddr;
111 return $cache{$addr} if (exists $cache{$addr});
112 if ($locs{$prog} ne "") {
113 $searchaddr = sprintf "%#x", $addr - $locs{$prog};
114 } else {
115 $searchaddr = $addr;
116 $prog = $binary;
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};
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};
140 $cache{$addr} = $str = $addr;
142 return $str;
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;
159 $allocaddr=$cols[$n + 1];
160 $howmuch=hex($cols[$n + 2]);
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, $wherewas{$allocaddr}, $where);
168 } else {
169 $allocated{$allocaddr}=$howmuch;
170 $wherewas{$allocaddr}=&location($where);
172 last SWITCH;
174 if ($cols[$n] eq "-") {
175 if (defined $allocated{$allocaddr}) {
176 undef $allocated{$allocaddr};
177 undef $wherewas{$allocaddr};
178 } else {
179 printf ("- %#0@XXX@x Free %d was never alloc'd %s\n",
180 hex($allocaddr), $nr, &location($where));
182 last SWITCH;
184 if ($cols[$n] eq "<") {
185 if (defined $allocated{$allocaddr}) {
186 undef $allocated{$allocaddr};
187 undef $wherewas{$allocaddr};
188 } else {
189 printf ("- %#0@XXX@x Realloc %d was never alloc'd %s\n",
190 hex($allocaddr), $nr, &location($where));
192 last SWITCH;
194 if ($cols[$n] eq ">") {
195 if (defined $allocated{$allocaddr}) {
196 printf ("+ %#0@XXX@x Realloc %d duplicate: %#010x %s %s\n",
197 hex($allocaddr), $nr, $allocated{$allocaddr},
198 $wherewas{$allocaddr}, &location($where));
199 } else {
200 $allocated{$allocaddr}=$howmuch;
201 $wherewas{$allocaddr}=&location($where);
203 last SWITCH;
205 if ($cols[$n] eq "=") {
206 # Ignore "= Start".
207 last SWITCH;
209 if ($cols[$n] eq "!") {
210 # Ignore failed realloc for now.
211 last SWITCH;
215 close (DATA);
217 # Now print all remaining entries.
218 @addrs= keys %allocated;
219 $anything=0;
220 if ($#addrs >= 0) {
221 foreach $addr (sort @addrs) {
222 if (defined $allocated{$addr}) {
223 if ($anything == 0) {
224 print "\nMemory not freed:\n-----------------\n";
225 print ' ' x (@XXX@ - 7), "Address Size Caller\n";
226 $anything=1;
228 printf ("%#0@XXX@x %#8x at %s\n", hex($addr), $allocated{$addr},
229 $wherewas{$addr});
233 print "No memory leaks.\n" if ($anything == 0);
235 exit $anything != 0;