]>
Commit | Line | Data |
---|---|---|
a2b08ee5 | 1 | #! @PERL@ |
ae39e102 | 2 | eval "exec @PERL@ -S $0 $@" |
a2b08ee5 | 3 | if 0; |
81fb02b0 | 4 | # Copyright (C) 1997-2011, 2012 Free Software Foundation, Inc. |
a2b08ee5 | 5 | # This file is part of the GNU C Library. |
3c5edd4d | 6 | # Contributed by Ulrich Drepper <drepper@gnu.org>, 1997. |
a2b08ee5 UD |
7 | # Based on the mtrace.awk script. |
8 | ||
9 | # The GNU C Library is free software; you can redistribute it and/or | |
e2cb5c1d AJ |
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. | |
a2b08ee5 UD |
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 | |
e2cb5c1d | 17 | # Lesser General Public License for more details. |
a2b08ee5 | 18 | |
e2cb5c1d | 19 | # You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
20 | # License along with the GNU C Library; if not, see |
21 | # <http://www.gnu.org/licenses/>. | |
a2b08ee5 UD |
22 | |
23 | $VERSION = "@VERSION@"; | |
24 | $PACKAGE = "libc"; | |
25 | $progname = $0; | |
26 | ||
27 | sub usage { | |
28 | print "Usage: mtrace [OPTION]... [Binary] MtraceData\n"; | |
29 | print " --help print this help, then exit\n"; | |
30 | print " --version print version number, then exit\n"; | |
a8a1269d | 31 | print "\n"; |
893a3511 | 32 | print "For bug reporting instructions, please see:\n"; |
d40eb37a | 33 | print "<http://www.gnu.org/software/libc/bugs.html>.\n"; |
a2b08ee5 UD |
34 | exit 0; |
35 | } | |
36 | ||
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. | |
41 | ||
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"; | |
81fb02b0 | 47 | print "Copyright (C) 2012 Free Software Foundation, Inc.\n"; |
a2b08ee5 UD |
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"; | |
a8a1269d | 50 | print "Written by Ulrich Drepper <drepper\@gnu.org>\n"; |
a2b08ee5 UD |
51 | |
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; | |
62 | } | |
63 | } | |
64 | ||
65 | if ($#ARGV == 0) { | |
66 | $binary=""; | |
67 | $data=$ARGV[0]; | |
68 | } elsif ($#ARGV == 1) { | |
69 | $binary=$ARGV[0]; | |
70 | $data=$ARGV[1]; | |
6a904bbf UD |
71 | |
72 | if ($binary =~ /^.*[\/].*$/) { | |
73 | $prog = $binary; | |
74 | } else { | |
75 | $prog = "./$binary"; | |
76 | } | |
77 | if (open (LOCS, "env LD_TRACE_LOADED_OBJECTS=1 $prog |")) { | |
78 | while (<LOCS>) { | |
79 | chop; | |
80 | if (/^.*=> (.*) .(0x[0123456789abcdef]*).$/) { | |
81 | $locs{$1} = $2; | |
82 | } | |
83 | } | |
84 | close (LOCS); | |
85 | } | |
a2b08ee5 | 86 | } else { |
60f0e64b | 87 | die "Wrong number of arguments, run $progname --help for help."; |
a2b08ee5 UD |
88 | } |
89 | ||
90 | sub location { | |
91 | my $str = pop(@_); | |
92 | return $str if ($str eq ""); | |
e1fa1730 | 93 | if ($str =~ /.*[[](0x[^]]*)]:(.)*/) { |
a2b08ee5 UD |
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}; | |
104 | } | |
105 | } | |
106 | $cache{$addr} = $str = "$fct @ $addr"; | |
6a904bbf UD |
107 | } elsif ($str =~ /^(.*):.*[[](0x[^]]*)]$/) { |
108 | my $prog = $1; | |
109 | my $addr = $2; | |
110 | my $searchaddr; | |
a2b08ee5 | 111 | return $cache{$addr} if (exists $cache{$addr}); |
6a904bbf UD |
112 | if ($locs{$prog} ne "") { |
113 | $searchaddr = sprintf "%#x", $addr - $locs{$prog}; | |
114 | } else { | |
115 | $searchaddr = $addr; | |
116 | $prog = $binary; | |
117 | } | |
118 | if ($binary ne "" && open (ADDR, "addr2line -e $prog $searchaddr|")) { | |
a2b08ee5 UD |
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; | |
129d706d UD |
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; | |
a2b08ee5 UD |
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}) { | |
a8a1269d | 166 | printf ("+ %#0@XXX@x Alloc %d duplicate: %s %s\n", |
e582c5ed UD |
167 | hex($allocaddr), $nr, &location($addrwas{$allocaddr}), |
168 | $where); | |
a2b08ee5 UD |
169 | } else { |
170 | $allocated{$allocaddr}=$howmuch; | |
e582c5ed | 171 | $addrwas{$allocaddr}=$where; |
a2b08ee5 UD |
172 | } |
173 | last SWITCH; | |
174 | } | |
175 | if ($cols[$n] eq "-") { | |
176 | if (defined $allocated{$allocaddr}) { | |
177 | undef $allocated{$allocaddr}; | |
e582c5ed | 178 | undef $addrwas{$allocaddr}; |
a2b08ee5 | 179 | } else { |
a8a1269d | 180 | printf ("- %#0@XXX@x Free %d was never alloc'd %s\n", |
a2b08ee5 UD |
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}; | |
e582c5ed | 188 | undef $addrwas{$allocaddr}; |
a2b08ee5 | 189 | } else { |
a8a1269d | 190 | printf ("- %#0@XXX@x Realloc %d was never alloc'd %s\n", |
a2b08ee5 UD |
191 | hex($allocaddr), $nr, &location($where)); |
192 | } | |
193 | last SWITCH; | |
194 | } | |
195 | if ($cols[$n] eq ">") { | |
196 | if (defined $allocated{$allocaddr}) { | |
a8a1269d | 197 | printf ("+ %#0@XXX@x Realloc %d duplicate: %#010x %s %s\n", |
a2b08ee5 | 198 | hex($allocaddr), $nr, $allocated{$allocaddr}, |
e582c5ed | 199 | &location($addrwas{$allocaddr}), &location($where)); |
a2b08ee5 UD |
200 | } else { |
201 | $allocated{$allocaddr}=$howmuch; | |
e582c5ed | 202 | $addrwas{$allocaddr}=$where; |
a2b08ee5 UD |
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; | |
bd355af0 | 220 | $anything=0; |
a2b08ee5 | 221 | if ($#addrs >= 0) { |
a2b08ee5 UD |
222 | foreach $addr (sort @addrs) { |
223 | if (defined $allocated{$addr}) { | |
bd355af0 UD |
224 | if ($anything == 0) { |
225 | print "\nMemory not freed:\n-----------------\n"; | |
226 | print ' ' x (@XXX@ - 7), "Address Size Caller\n"; | |
227 | $anything=1; | |
228 | } | |
a2b08ee5 | 229 | printf ("%#0@XXX@x %#8x at %s\n", hex($addr), $allocated{$addr}, |
e582c5ed | 230 | &location($addrwas{$addr})); |
a2b08ee5 UD |
231 | } |
232 | } | |
233 | } | |
bd355af0 | 234 | print "No memory leaks.\n" if ($anything == 0); |
a2b08ee5 | 235 | |
b97e3f1f | 236 | exit $anything != 0; |