-
Notifications
You must be signed in to change notification settings - Fork 34
/
check_malloc.pl
executable file
·116 lines (110 loc) · 2.64 KB
/
check_malloc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#! /usr/local/bin/perl
# Scans output generated by the check_malloc.h macros, tracks
# memory usage and checks for memory leaks
$tot = 0;
$n = 0;
while(<>){
$line++;
if(/malloc/){
# Line format is
# 95768 = malloc(1024) make_gather:594
@a = split(" ",$_);
# Double check format
if($a[1] eq "="){
$size = $a[2];
$loc = $a[3];
$size =~ s/malloc\((\w+)\)/$1/;
$tot += $size;
$totMB = $tot/1e6;
print "$n $totMB $loc\n";
$n++;
$addr = $a[0];
if ( defined($table{$addr}) && $table{$addr} != 0){
print "WARNING: expected 0 above but got $table{$addr}\n";
}
$table{$addr} = $size;
$location{$addr} = $loc;
}
}
elsif(/calloc/){
# Line format is
# 95768 = calloc(256,8) routine:594
print " $_";
@a = split(" ",$_);
# Double check format
if($a[1] eq "="){
$nelem = $a[2];
$loc = $a[3];
$nelem =~ s/calloc\((\w+),\w+\)/$1/;
$elsize = $a[2];
$elsize =~ s/calloc\(\w+,(\w+)\)/$1/;
$size = $nelem*$elsize;
$tot += $size;
$totMB = $tot/1e6;
print "$n $totMB $loc\n";
$n++;
$addr = $a[0];
if ( defined($table{$addr}) && $table{$addr} != 0){
print "WARNING line $line: expected 0 but got $table{$addr}\n";
}
$table{$addr} = $size;
$location{$addr} = $loc;
}
}
elsif(/realloc/){
# Line format is
# 95e28 = realloc(92fe0,40) make_gather:590
print "% $_";
@a = split(" ",$_);
# Double check format
if($a[1] eq "="){
$addr = $a[2];
$loc = $a[3];
$addr =~ s/realloc\((\w+),\w+\)/$1/;
$size = $a[2];
$size =~ s/realloc\(\w+,(\w+)\)/$1/;
print "% addr = $addr size = $size\n";
defined($table{$addr}) || die "Can't find $addr in table\n";
$oldsize = $table{$addr};
$table{$addr} = 0;
$tot += ($size - $oldsize);
$totMB = $tot/1e6;
print "$n $totMB $loc\n";
$n++;
$addr = $a[0];
$table{$addr} = $size;
$location{$addr} = $loc;
}
}
elsif(/free\(/){
# Line format is
# free(94348) make_gather:711
@a = split(" ",$_);
$addr = $a[0];
$addr =~ s/free\((\w+)\)/$1/;
if($addr ne "0"){
defined($table{$addr}) || die "Can't find $addr in table\n";
$size = $table{$addr};
if($size == 0){
printf "WARNING: line $line: Double free?\n";
}
$tot -= $size;
$totMB = $tot/1e6;
print "$n $totMB $a[1]\n";
$n++;
$table{$addr} = 0;
}
}
else {
print "% $_";
}
}
# Check for hanging mallocs
foreach $key (sort keys %table)
{
if($table{$key} > 0){
$tot -= $table{$key};
print "% alloc $key leaves $table{$key} bytes unfreed from $location{$key}.\n";
}
}
print "% $tot bytes remain unaccounted\n";