Update.
[glibc.git] / malloc / mtrace.pl
blob46d8425bee62bdd6a661e904f7b3c8a641433066
1 #! @PERL@
2 eval "exec @PERL@ -S $0 $*"
3 if 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@";
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 exit 0;
35 # We expect two arguments:
36 # #1: the complete path to the binary
37 # #2: the mtrace data filename
38 # The usual options are also recognized.
40 arglist: while (@ARGV) {
41 if ($ARGV[0] eq "--v" || $ARGV[0] eq "--ve" || $ARGV[0] eq "--ver" ||
42 $ARGV[0] eq "--vers" || $ARGV[0] eq "--versi" ||
43 $ARGV[0] eq "--versio" || $ARGV[0] eq "--version") {
44 print "mtrace (GNU $PACKAGE) $VERSION\n";
45 print "Copyright (C) 1997 Free Software Foundation, Inc.\n";
46 print "This is free software; see the source for copying conditions. There is NO\n";
47 print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
48 print "Written by Ulrich Drepper <drepper\@gnu.ai.mit.edu>\n";
50 exit 0;
51 } elsif ($ARGV[0] eq "--h" || $ARGV[0] eq "--he" || $ARGV[0] eq "--hel" ||
52 $ARGV[0] eq "--help") {
53 &usage;
54 } elsif ($ARGV[0] =~ /^-/) {
55 print "$progname: unrecognized option `$ARGV[0]'\n";
56 print "Try `$progname --help' for more information.\n";
57 exit 1;
58 } else {
59 last arglist;
63 if ($#ARGV == 0) {
64 $binary="";
65 $data=$ARGV[0];
66 } elsif ($#ARGV == 1) {
67 $binary=$ARGV[0];
68 $data=$ARGV[1];
69 } else {
70 die "Wrong number of arguments.";
73 sub location {
74 my $str = pop(@_);
75 return $str if ($str eq "");
76 if ($str =~ /[[](0x[^]]*)]:(.)*/) {
77 my $addr = $1;
78 my $fct = $2;
79 return $cache{$addr} if (exists $cache{$addr});
80 if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
81 my $line = <ADDR>;
82 chomp $line;
83 close (ADDR);
84 if ($line ne '??:0') {
85 $cache{$addr} = $line;
86 return $cache{$addr};
89 $cache{$addr} = $str = "$fct @ $addr";
90 } elsif ($str =~ /^[[](0x[^]]*)]$/) {
91 my $addr = $1;
92 return $cache{$addr} if (exists $cache{$addr});
93 if ($binary ne "" && open (ADDR, "addr2line -e $binary $addr|")) {
94 my $line = <ADDR>;
95 chomp $line;
96 close (ADDR);
97 if ($line ne '??:0') {
98 $cache{$addr} = $line;
99 return $cache{$addr};
102 $cache{$addr} = $str = $addr;
104 return $str;
107 $nr=0;
108 open(DATA, "<$data") || die "Cannot open mtrace data file";
109 while (<DATA>) {
110 my @cols = split (' ');
111 my $n, $where;
112 if ($cols[0] eq "@") {
113 # We have address and/or function name.
114 $where=$cols[1];
115 $n=2;
116 } else {
117 $where="";
118 $n=0;
121 $allocaddr=$cols[$n + 1];
122 $howmuch=hex($cols[$n + 2]);
124 ++$nr;
125 SWITCH: {
126 if ($cols[$n] eq "+") {
127 if (defined $allocated{$allocaddr}) {
128 printf ("+ %#010x Alloc %d duplicate: %s %s\n",
129 hex($allocaddr), $nr, $wherewas{$allocaddr}, $where);
130 } else {
131 $allocated{$allocaddr}=$howmuch;
132 $wherewas{$allocaddr}=&location($where);
134 last SWITCH;
136 if ($cols[$n] eq "-") {
137 if (defined $allocated{$allocaddr}) {
138 undef $allocated{$allocaddr};
139 undef $wherewas{$allocaddr};
140 } else {
141 printf ("- %#010x Free %d was never alloc'd %s\n",
142 hex($allocaddr), $nr, &location($where));
144 last SWITCH;
146 if ($cols[$n] eq "<") {
147 if (defined $allocated{$allocaddr}) {
148 undef $allocated{$allocaddr};
149 undef $wherewas{$allocaddr};
150 } else {
151 printf ("- %#010x Realloc %d was never alloc'd %s\n",
152 hex($allocaddr), $nr, &location($where));
154 last SWITCH;
156 if ($cols[$n] eq ">") {
157 if (defined $allocated{$allocaddr}) {
158 printf ("+ %#010x Realloc %d duplicate: %#010x %s %s\n",
159 hex($allocaddr), $nr, $allocated{$allocaddr},
160 $wherewas{$allocaddr}, &location($where));
161 } else {
162 $allocated{$allocaddr}=$howmuch;
163 $wherewas{$allocaddr}=&location($where);
165 last SWITCH;
167 if ($cols[$n] eq "=") {
168 # Ignore "= Start".
169 last SWITCH;
171 if ($cols[$n] eq "!") {
172 # Ignore failed realloc for now.
173 last SWITCH;
177 close (DATA);
179 # Now print all remaining entries.
180 @addrs= keys %allocated;
181 if ($#addrs >= 0) {
182 print "\nNot freed memory:\n-----------------\n";
183 print ' ' x (@XXX@ - 7), "Address Size Caller\n";
184 foreach $addr (sort @addrs) {
185 if (defined $allocated{$addr}) {
186 printf ("%#0@XXX@x %#8x at %s\n", hex($addr), $allocated{$addr},
187 $wherewas{$addr});
192 exit 0;