2 eval "exec @PERL@ -S $0 $*"
4 # Copyright (C) 1997 Free Software Foundation, Inc.
5 # This file is part of the GNU C Library.
6 # Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 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 Library General Public License as
11 # published by the Free Software Foundation; either version 2 of the
12 # 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 # Library General Public License for more details.
19 # You should have received a copy of the GNU Library General Public
20 # License along with the GNU C Library; see the file COPYING.LIB. If not,
21 # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 # Boston, MA 02111-1307, USA.
24 $VERSION = "@VERSION@";
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";
33 print "Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n";
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) 1997, 1998 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";
53 } elsif ($ARGV[0] eq "--h" || $ARGV[0] eq "--he" || $ARGV[0] eq "--hel" ||
54 $ARGV[0] eq "--help") {
56 } elsif ($ARGV[0] =~ /^-/) {
57 print "$progname: unrecognized option `$ARGV[0]'\n";
58 print "Try `$progname --help' for more information.\n";
68 } elsif ($#ARGV == 1) {
72 die "Wrong number of arguments.";
77 return $str if ($str eq "");
78 if ($str =~ /[[](0x[^]]*)]:(.)*/) {
81 return $cache{$addr} if (exists $cache{$addr});
82 if ($binary ne "" && open (ADDR
, "addr2line -e $binary $addr|")) {
86 if ($line ne '??:0') {
87 $cache{$addr} = $line;
91 $cache{$addr} = $str = "$fct @ $addr";
92 } elsif ($str =~ /^[[](0x[^]]*)]$/) {
94 return $cache{$addr} if (exists $cache{$addr});
95 if ($binary ne "" && open (ADDR
, "addr2line -e $binary $addr|")) {
99 if ($line ne '??:0') {
100 $cache{$addr} = $line;
101 return $cache{$addr};
104 $cache{$addr} = $str = $addr;
110 open(DATA
, "<$data") || die "Cannot open mtrace data file";
112 my @cols = split (' ');
114 if ($cols[0] eq "@") {
115 # We have address and/or function name.
123 $allocaddr=$cols[$n + 1];
124 $howmuch=hex($cols[$n + 2]);
128 if ($cols[$n] eq "+") {
129 if (defined $allocated{$allocaddr}) {
130 printf ("+ %#0@XXX@x Alloc %d duplicate: %s %s\n",
131 hex($allocaddr), $nr, $wherewas{$allocaddr}, $where);
133 $allocated{$allocaddr}=$howmuch;
134 $wherewas{$allocaddr}=&location
($where);
138 if ($cols[$n] eq "-") {
139 if (defined $allocated{$allocaddr}) {
140 undef $allocated{$allocaddr};
141 undef $wherewas{$allocaddr};
143 printf ("- %#0@XXX@x Free %d was never alloc'd %s\n",
144 hex($allocaddr), $nr, &location
($where));
148 if ($cols[$n] eq "<") {
149 if (defined $allocated{$allocaddr}) {
150 undef $allocated{$allocaddr};
151 undef $wherewas{$allocaddr};
153 printf ("- %#0@XXX@x Realloc %d was never alloc'd %s\n",
154 hex($allocaddr), $nr, &location
($where));
158 if ($cols[$n] eq ">") {
159 if (defined $allocated{$allocaddr}) {
160 printf ("+ %#0@XXX@x Realloc %d duplicate: %#010x %s %s\n",
161 hex($allocaddr), $nr, $allocated{$allocaddr},
162 $wherewas{$allocaddr}, &location
($where));
164 $allocated{$allocaddr}=$howmuch;
165 $wherewas{$allocaddr}=&location
($where);
169 if ($cols[$n] eq "=") {
173 if ($cols[$n] eq "!") {
174 # Ignore failed realloc for now.
181 # Now print all remaining entries.
182 @addrs= keys %allocated;
185 foreach $addr (sort @addrs) {
186 if (defined $allocated{$addr}) {
187 if ($anything == 0) {
188 print "\nMemory not freed:\n-----------------\n";
189 print ' ' x
(@XXX@
- 7), "Address Size Caller\n";
192 printf ("%#0@XXX@x %#8x at %s\n", hex($addr), $allocated{$addr},
197 print "No memory leaks.\n" if ($anything == 0);