1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
14 # The Original Code is TraceMalloc.pm, released
17 # The Initial Developer of the Original Code is
18 # Netscape Communications Corporation.
19 # Portions created by the Initial Developer are Copyright (C) 2000
20 # the Initial Developer. All Rights Reserved.
23 # Chris Waterson <waterson@netscape.com>
25 # Alternatively, the contents of this file may be used under the terms of
26 # either the GNU General Public License Version 2 or later (the "GPL"), or
27 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 # in which case the provisions of the GPL or the LGPL are applicable instead
29 # of those above. If you wish to allow use of your version of this file only
30 # under the terms of either the GPL or the LGPL, and not to allow others to
31 # use your version of this file under the terms of the MPL, indicate your
32 # decision by deleting the provisions above and replace them with the notice
33 # and other provisions required by the GPL or the LGPL. If you do not delete
34 # the provisions above, a recipient may use your version of this file under
35 # the terms of any one of the MPL, the GPL or the LGPL.
37 # ***** END LICENSE BLOCK *****
42 # Read in the type inference file and construct a network that we can
43 # use to match stack prefixes to types.
44 sub init_type_inference
($) {
47 $::Fingerprints
= { };
49 open(TYPES
, "<$file") || die "unable to open $::opt_types, $!";
51 TYPE
: while (<TYPES
>) {
52 next TYPE
unless /<(.*)>/;
55 my $link = \
%::Fingerprints
;
57 FRAME
: while (<TYPES
>) {
61 my $next = $link->{$_};
63 $next = $link->{$_} = {};
68 $link->{'#type#'} = $type;
74 # Infer the type, trying to find the most specific type possible.
78 my $link = \
%::Fingerprints
;
81 FRAME
: foreach my $frame (@
$stack) {
82 last FRAME
unless $link;
84 $frame =~ s/\[.*\]$//; # ignore exact addresses, as they'll drift
89 # Remember this type, but keep going. We use the longest match
90 # we find, but substacks of longer matches will also match.
92 if ($last->{'#type#'}) {
93 $type = $last->{'#type#'};
96 $link = $link->{$frame};
99 CHILD
: foreach my $child (keys %$last) {
100 next CHILD
unless $child =~ /^~/;
104 if ($frame =~ $child) {
105 $link = $last->{'~' . $child};
116 #----------------------------------------------------------------------
118 # Read in the output a trace malloc's dump.
121 my ($callback, $noslop) = @_;
124 # e.g., 0x0832FBD0 <void*> (80)
125 next OBJECT
unless /^0x(\S+) <(.*)> \((\d+)\)/;
126 my ($addr, $type, $size) = (hex $1, $2, $3);
128 my $object = { 'type' => $type, 'size' => $size };
130 # Record the object's slots
135 last SLOT
unless /^\t0x(\S+)/;
138 # Ignore low bits, unless they've specified --noslop
139 $value &= ~0x7 unless $noslop;
141 $slots[$#slots + 1] = $value;
144 $object->{'slots'} = \
@slots;
146 # Record the stack by which the object was allocated
149 while (/^(.*)\[(.*) \+0x(\S+)\]$/) {
150 # e.g., _dl_debug_message[/lib/ld-linux.so.2 +0x0000B858]
151 my ($func, $lib, $off) = ($1, $2, hex $3);
154 $stack[$#stack + 1] = $_;
159 $object->{'stack'} = \
@stack;
161 $object->{'type'} = infer_type
(\
@stack)
162 if $object->{'type'} eq 'void*';
164 &$callback($object) if $callback;
166 # Gotta check EOF explicitly...
176 TraceMalloc - Perl routines to deal with output from ``trace malloc''
183 TraceMalloc::init_type_inference("types.dat");
184 TraceMalloc::read(0);