3 # Show current allocations from a trace file. If run over a trace file
4 # from a program which has exited, this will show memory leaks. If run
5 # over a trace file for a program which is currently running, this will
6 # show currently allocated space.
8 # By Richard W.M. Jones <rich@annexia.org>
10 # $Id: curallocs.pl,v 1.1 2001/02/08 12:51:35 rich Exp $
24 if (/^([a-z_]+)\s+caller:\s+([0-9a-fx]+)\s+ptr1:\s+([0-9a-fx]+|\(nil\))\s+ptr2:\s+([0-9a-fx]+|\(nil\))\s+ptr3:\s+([0-9a-fx]+|\(nil\))\s+i1:\s+([0-9]+)\s*$/)
28 my $ptr1 = $3 ne '(nil)' ? $3 : 0;
29 my $ptr2 = $4 ne '(nil)' ? $4 : 0;
30 my $ptr3 = $5 ne '(nil)' ? $4 : 0;
33 if ($fn eq "new_pool")
35 die "new_pool: pool exists, line $lineno"
36 if exists $pools{$ptr1};
45 elsif ($fn eq "new_subpool")
47 die "new_subpool: pool exists, line $lineno"
48 if exists $pools{$ptr1};
57 $pools{$ptr2}{children}{$ptr1} = 1;
59 elsif ($fn eq "delete_pool")
61 if ($pools{$ptr1}{parent})
63 delete $pools{$pools{$ptr1}{parent}}{children}{$ptr1};
66 remove_pool_recursively ($ptr1);
68 elsif ($fn eq "pmalloc")
70 die "pmalloc: no pool $ptr1, line $lineno"
71 unless exists $pools{$ptr1};
73 $pools{$ptr1}{allocations}{$ptr2} = {
80 elsif ($fn eq "prealloc")
82 die "prealloc: no pool $ptr1, line $lineno"
83 unless exists $pools{$ptr1};
84 die "prealloc: allocation already exists, line $lineno"
85 unless exists $pools{$ptr1}{allocations}{$ptr2};
87 # Delete the old allocation.
88 delete $pools{$ptr1}{allocations}{$ptr2};
90 $pools{$ptr1}{allocations}{$ptr3} = {
99 die "unknown pool function traced: $fn, line $lineno";
104 print "$lineno: $_\n";
105 die "cannot parse line";
109 if (keys %pools > 0) {
112 print "No pools are currently allocated.\n";
117 sub remove_pool_recursively
122 die unless exists $pools{$pool};
124 # Remove children first.
125 foreach (keys %{$pools{$pool}{children}})
127 remove_pool_recursively ($_);
130 delete $pools{$pool};
137 foreach (keys %pools)
146 my $indent = shift || 0;
149 my $sp = " " x $indent;
151 print $sp, "pool $pool created by $pools{$pool}{creator}:\n";
152 print $sp, " number of direct allocations: ", 0 + keys %{$pools{$pool}{allocations}}, "\n";
153 print $sp, " number of children: ", 0 + keys %{$pools{$pool}{children}}, "\n";
154 print $sp, " allocations:\n";
156 show_allocations ($pools{$pool}{allocations}, $indent);
158 print $sp, " children:\n";
160 foreach (keys %{$pools{$pool}{children}})
162 show_pool ($_, $indent + 2);
168 my $allocations = shift;
169 my $indent = shift || 0;
172 foreach (keys %$allocations)
174 show_allocation ($allocations->{$_}, $indent);
180 my $allocation = shift;
181 my $indent = shift || 0;
184 my $sp = " " x $indent;
186 print $sp, " allocation $allocation->{address} created by $allocation->{creator} size $allocation->{size}\n";