2000-10-09 07:31:55 -04:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# Example input:
|
|
|
|
#
|
|
|
|
# MEM mprintf.c:1094 malloc(32) = e5718
|
|
|
|
# MEM mprintf.c:1103 realloc(e5718, 64) = e6118
|
|
|
|
# MEM sendf.c:232 free(f6520)
|
|
|
|
|
|
|
|
do {
|
|
|
|
if($ARGV[0] eq "-v") {
|
|
|
|
$verbose=1;
|
|
|
|
}
|
|
|
|
} while (shift @ARGV);
|
|
|
|
|
|
|
|
while(<STDIN>) {
|
|
|
|
chomp $_;
|
|
|
|
$line = $_;
|
|
|
|
if($verbose) {
|
|
|
|
print "IN: $line\n";
|
|
|
|
}
|
|
|
|
if($line =~ /^MEM ([^:]*):(\d*) (.*)/) {
|
|
|
|
# generic match for the filename+linenumber
|
|
|
|
$source = $1;
|
|
|
|
$linenum = $2;
|
|
|
|
$function = $3;
|
|
|
|
|
2000-11-21 14:37:15 -05:00
|
|
|
if($function =~ /free\(0x([0-9a-f]*)/) {
|
2000-10-09 07:31:55 -04:00
|
|
|
$addr = $1;
|
|
|
|
if($sizeataddr{$addr} <= 0) {
|
|
|
|
print "FREE ERROR: No memory allocated: $line\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$totalmem -= $sizeataddr{$addr};
|
|
|
|
$sizeataddr{$addr}=0;
|
|
|
|
$getmem{$addr}=""; # forget after a good free()
|
|
|
|
}
|
|
|
|
}
|
2000-11-21 14:37:15 -05:00
|
|
|
elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
|
2000-10-09 07:31:55 -04:00
|
|
|
$size = $1;
|
|
|
|
$addr = $2;
|
|
|
|
$sizeataddr{$addr}=$size;
|
|
|
|
$totalmem += $size;
|
|
|
|
|
|
|
|
$getmem{$addr}="$source:$linenum";
|
|
|
|
}
|
2000-11-21 14:37:15 -05:00
|
|
|
elsif($function =~ /realloc\(0x([0-9a-f]*), (\d*)\) = 0x([0-9a-f]*)/) {
|
2000-10-09 07:31:55 -04:00
|
|
|
$oldaddr = $1;
|
|
|
|
$newsize = $2;
|
|
|
|
$newaddr = $3;
|
|
|
|
|
|
|
|
$totalmem -= $sizeataddr{$oldaddr};
|
|
|
|
$sizeataddr{$oldaddr}=0;
|
|
|
|
|
|
|
|
$totalmem += $newsize;
|
|
|
|
$sizeataddr{$newaddr}=$newsize;
|
|
|
|
|
|
|
|
$getmem{$oldaddr}="";
|
|
|
|
$getmem{$newaddr}="$source:$linenum";
|
|
|
|
}
|
2000-11-21 14:37:15 -05:00
|
|
|
elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
|
2000-10-09 07:31:55 -04:00
|
|
|
# strdup(a5b50) (8) = df7c0
|
|
|
|
|
|
|
|
$dup = $1;
|
|
|
|
$size = $2;
|
|
|
|
$addr = $3;
|
|
|
|
$getmem{$addr}="$source:$linenum";
|
|
|
|
$sizeataddr{$addr}=$size;
|
|
|
|
|
|
|
|
$totalmem += $size;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "Not recognized input line: $function\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "Not recognized prefix line: $line\n";
|
|
|
|
}
|
|
|
|
if($verbose) {
|
|
|
|
print "TOTAL: $totalmem\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-11-21 10:50:17 -05:00
|
|
|
if($totalmem) {
|
|
|
|
print "Leak detected: memory still allocated: $totalmem bytes\n";
|
2000-10-09 07:31:55 -04:00
|
|
|
|
2000-11-21 10:50:17 -05:00
|
|
|
for(keys %sizeataddr) {
|
|
|
|
$addr = $_;
|
|
|
|
$size = $sizeataddr{$addr};
|
|
|
|
if($size) {
|
|
|
|
print "At $addr, there's $size bytes.\n";
|
|
|
|
print " allocated by ".$getmem{$addr}."\n";
|
|
|
|
}
|
2000-10-09 07:31:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|