2002-05-22 00:20:52 +02:00
|
|
|
#!/usr/bin/env perl
|
2000-10-09 13:31:55 +02:00
|
|
|
#
|
|
|
|
# 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;
|
|
|
|
}
|
2002-02-28 13:35:54 +01:00
|
|
|
elsif($ARGV[0] eq "-t") {
|
|
|
|
$trace=1;
|
|
|
|
}
|
2000-10-09 13:31:55 +02:00
|
|
|
} while (shift @ARGV);
|
|
|
|
|
2001-04-24 23:47:01 +02:00
|
|
|
my $maxmem;
|
|
|
|
|
|
|
|
sub newtotal {
|
|
|
|
my ($newtot)=@_;
|
|
|
|
# count a max here
|
|
|
|
|
|
|
|
if($newtot > $maxmem) {
|
|
|
|
$maxmem= $newtot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-09 13:31:55 +02:00
|
|
|
while(<STDIN>) {
|
|
|
|
chomp $_;
|
|
|
|
$line = $_;
|
2001-04-24 23:47:01 +02:00
|
|
|
|
2000-10-09 13:31:55 +02:00
|
|
|
if($line =~ /^MEM ([^:]*):(\d*) (.*)/) {
|
|
|
|
# generic match for the filename+linenumber
|
|
|
|
$source = $1;
|
|
|
|
$linenum = $2;
|
|
|
|
$function = $3;
|
|
|
|
|
2000-11-21 20:37:15 +01:00
|
|
|
if($function =~ /free\(0x([0-9a-f]*)/) {
|
2000-10-09 13:31:55 +02:00
|
|
|
$addr = $1;
|
2001-01-19 13:20:30 +01:00
|
|
|
if($sizeataddr{$addr} == 0) {
|
2000-10-09 13:31:55 +02:00
|
|
|
print "FREE ERROR: No memory allocated: $line\n";
|
|
|
|
}
|
2001-01-19 13:20:30 +01:00
|
|
|
elsif(-1 == $sizeataddr{$addr}) {
|
|
|
|
print "FREE ERROR: Memory freed twice: $line\n";
|
|
|
|
print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
|
|
|
|
}
|
2000-10-09 13:31:55 +02:00
|
|
|
else {
|
|
|
|
$totalmem -= $sizeataddr{$addr};
|
2002-02-28 13:35:54 +01:00
|
|
|
if($trace) {
|
|
|
|
print "FREE: malloc at ".$getmem{$addr}." is freed again at $source:$linenum\n";
|
|
|
|
printf("FREE: %d bytes freed, left allocated: $totalmem bytes\n", $sizeataddr{$addr});
|
|
|
|
}
|
2001-04-24 23:47:01 +02:00
|
|
|
|
|
|
|
newtotal($totalmem);
|
|
|
|
$frees++;
|
|
|
|
|
2001-01-19 13:20:30 +01:00
|
|
|
$sizeataddr{$addr}=-1; # set -1 to mark as freed
|
|
|
|
$getmem{$addr}="$source:$linenum";
|
2001-04-24 23:47:01 +02:00
|
|
|
|
2000-10-09 13:31:55 +02:00
|
|
|
}
|
|
|
|
}
|
2000-11-21 20:37:15 +01:00
|
|
|
elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
|
2000-10-09 13:31:55 +02:00
|
|
|
$size = $1;
|
|
|
|
$addr = $2;
|
2001-04-24 23:47:01 +02:00
|
|
|
|
|
|
|
if($sizeataddr{$addr}>0) {
|
|
|
|
# this means weeeeeirdo
|
|
|
|
print "Fucked up debug compile, rebuild curl now\n";
|
|
|
|
}
|
|
|
|
|
2000-10-09 13:31:55 +02:00
|
|
|
$sizeataddr{$addr}=$size;
|
|
|
|
$totalmem += $size;
|
|
|
|
|
2002-02-28 13:35:54 +01:00
|
|
|
if($trace) {
|
|
|
|
print "MALLOC: malloc($size) at $source:$linenum",
|
|
|
|
" makes totally $totalmem bytes\n";
|
2001-04-24 23:47:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
newtotal($totalmem);
|
|
|
|
$mallocs++;
|
|
|
|
|
2000-10-09 13:31:55 +02:00
|
|
|
$getmem{$addr}="$source:$linenum";
|
|
|
|
}
|
2000-11-21 20:37:15 +01:00
|
|
|
elsif($function =~ /realloc\(0x([0-9a-f]*), (\d*)\) = 0x([0-9a-f]*)/) {
|
2000-10-09 13:31:55 +02:00
|
|
|
$oldaddr = $1;
|
|
|
|
$newsize = $2;
|
|
|
|
$newaddr = $3;
|
|
|
|
|
|
|
|
$totalmem -= $sizeataddr{$oldaddr};
|
2002-02-28 13:35:54 +01:00
|
|
|
if($trace) {
|
|
|
|
printf("REALLOC: %d less bytes and ", $sizeataddr{$oldaddr});
|
|
|
|
}
|
2000-10-09 13:31:55 +02:00
|
|
|
$sizeataddr{$oldaddr}=0;
|
|
|
|
|
|
|
|
$totalmem += $newsize;
|
|
|
|
$sizeataddr{$newaddr}=$newsize;
|
|
|
|
|
2002-02-28 13:35:54 +01:00
|
|
|
if($trace) {
|
|
|
|
printf("%d more bytes ($source:$linenum)\n", $newsize);
|
|
|
|
}
|
|
|
|
|
2001-04-24 23:47:01 +02:00
|
|
|
newtotal($totalmem);
|
|
|
|
$reallocs++;
|
|
|
|
|
2000-10-09 13:31:55 +02:00
|
|
|
$getmem{$oldaddr}="";
|
|
|
|
$getmem{$newaddr}="$source:$linenum";
|
|
|
|
}
|
2000-11-21 20:37:15 +01:00
|
|
|
elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
|
2000-10-09 13:31:55 +02:00
|
|
|
# strdup(a5b50) (8) = df7c0
|
|
|
|
|
|
|
|
$dup = $1;
|
|
|
|
$size = $2;
|
|
|
|
$addr = $3;
|
|
|
|
$getmem{$addr}="$source:$linenum";
|
|
|
|
$sizeataddr{$addr}=$size;
|
|
|
|
|
|
|
|
$totalmem += $size;
|
2001-04-24 23:47:01 +02:00
|
|
|
|
2002-02-28 13:35:54 +01:00
|
|
|
if($trace) {
|
|
|
|
printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
|
|
|
|
$getmem{$addr}, $totalmem);
|
|
|
|
}
|
|
|
|
|
2001-04-24 23:47:01 +02:00
|
|
|
newtotal($totalmem);
|
|
|
|
$strdups++;
|
2000-10-09 13:31:55 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "Not recognized input line: $function\n";
|
|
|
|
}
|
|
|
|
}
|
2000-12-14 16:56:59 +01:00
|
|
|
# FD url.c:1282 socket() = 5
|
|
|
|
elsif($_ =~ /^FD ([^:]*):(\d*) (.*)/) {
|
|
|
|
# generic match for the filename+linenumber
|
|
|
|
$source = $1;
|
|
|
|
$linenum = $2;
|
|
|
|
$function = $3;
|
|
|
|
|
|
|
|
if($function =~ /socket\(\) = (\d*)/) {
|
|
|
|
$filedes{$1}=1;
|
|
|
|
$getfile{$1}="$source:$linenum";
|
|
|
|
$openfile++;
|
|
|
|
}
|
2000-12-18 17:13:37 +01:00
|
|
|
elsif($function =~ /accept\(\) = (\d*)/) {
|
|
|
|
$filedes{$1}=1;
|
|
|
|
$getfile{$1}="$source:$linenum";
|
|
|
|
$openfile++;
|
|
|
|
}
|
2000-12-14 16:56:59 +01:00
|
|
|
elsif($function =~ /sclose\((\d*)\)/) {
|
|
|
|
if($filedes{$1} != 1) {
|
|
|
|
print "Close without open: $line\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$filedes{$1}=0; # closed now
|
|
|
|
$openfile--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-12-19 14:32:30 +01:00
|
|
|
# FILE url.c:1282 fopen("blabla") = 0x5ddd
|
|
|
|
elsif($_ =~ /^FILE ([^:]*):(\d*) (.*)/) {
|
|
|
|
# generic match for the filename+linenumber
|
|
|
|
$source = $1;
|
|
|
|
$linenum = $2;
|
|
|
|
$function = $3;
|
|
|
|
|
|
|
|
if($function =~ /fopen\(\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
|
|
|
|
if($2 eq "(nil)") {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$fopen{$3}=1;
|
|
|
|
$fopenfile{$3}="$source:$linenum";
|
|
|
|
$fopens++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# fclose(0x1026c8)
|
|
|
|
elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
|
|
|
|
if(!$fopen{$1}) {
|
|
|
|
print "fclose() without fopen(): $line\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$fopen{$1}=0;
|
|
|
|
$fopens--;
|
|
|
|
}
|
|
|
|
}
|
2001-10-04 15:25:40 +02:00
|
|
|
}
|
|
|
|
# ADDR url.c:1282 getaddrinfo() = 0x5ddd
|
|
|
|
elsif($_ =~ /^ADDR ([^:]*):(\d*) (.*)/) {
|
|
|
|
# generic match for the filename+linenumber
|
|
|
|
$source = $1;
|
|
|
|
$linenum = $2;
|
|
|
|
$function = $3;
|
|
|
|
|
|
|
|
if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
|
|
|
|
my $add = $2;
|
|
|
|
if($add eq "(nil)") {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$addrinfo{$add}=1;
|
|
|
|
$addrinfofile{$add}="$source:$linenum";
|
|
|
|
$addrinfos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# fclose(0x1026c8)
|
|
|
|
elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
|
|
|
|
if(!$addrinfo{$1}) {
|
|
|
|
print "freeaddrinfo() without getaddrinfo(): $line\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$addrinfo{$1}=0;
|
|
|
|
$addrinfos--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-19 14:32:30 +01:00
|
|
|
}
|
2000-10-09 13:31:55 +02:00
|
|
|
else {
|
|
|
|
print "Not recognized prefix line: $line\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-11-21 16:50:17 +01:00
|
|
|
if($totalmem) {
|
|
|
|
print "Leak detected: memory still allocated: $totalmem bytes\n";
|
2000-10-09 13:31:55 +02:00
|
|
|
|
2000-11-21 16:50:17 +01:00
|
|
|
for(keys %sizeataddr) {
|
|
|
|
$addr = $_;
|
|
|
|
$size = $sizeataddr{$addr};
|
2001-02-20 14:58:56 +01:00
|
|
|
if($size > 0) {
|
2000-11-21 16:50:17 +01:00
|
|
|
print "At $addr, there's $size bytes.\n";
|
|
|
|
print " allocated by ".$getmem{$addr}."\n";
|
|
|
|
}
|
2000-10-09 13:31:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-14 16:56:59 +01:00
|
|
|
if($openfile) {
|
|
|
|
for(keys %filedes) {
|
|
|
|
if($filedes{$_} == 1) {
|
|
|
|
print "Open file descriptor created at ".$getfile{$_}."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-12-19 14:32:30 +01:00
|
|
|
|
|
|
|
if($fopens) {
|
|
|
|
print "Open FILE handles left at:\n";
|
|
|
|
for(keys %fopen) {
|
|
|
|
if($fopen{$_} == 1) {
|
|
|
|
print "fopen() called at ".$fopenfile{$_}."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-04-24 23:47:01 +02:00
|
|
|
|
2001-10-04 15:25:40 +02:00
|
|
|
if($addrinfos) {
|
|
|
|
print "IPv6-style name resolve data left at:\n";
|
|
|
|
for(keys %addrinfofile) {
|
|
|
|
if($addrinfo{$_} == 1) {
|
|
|
|
print "getaddrinfo() called at ".$addrinfofile{$_}."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-24 23:47:01 +02:00
|
|
|
if($verbose) {
|
|
|
|
print "Mallocs: $mallocs\n",
|
|
|
|
"Reallocs: $reallocs\n",
|
|
|
|
"Strdups: $strdups\n",
|
|
|
|
"Frees: $frees\n";
|
|
|
|
|
|
|
|
print "Maximum allocated: $maxmem\n";
|
|
|
|
}
|