2002-05-21 18:20:52 -04:00
|
|
|
#!/usr/bin/env perl
|
2011-03-22 17:48:11 -04:00
|
|
|
#***************************************************************************
|
|
|
|
# _ _ ____ _
|
|
|
|
# Project ___| | | | _ \| |
|
|
|
|
# / __| | | | |_) | |
|
|
|
|
# | (__| |_| | _ <| |___
|
|
|
|
# \___|\___/|_| \_\_____|
|
|
|
|
#
|
2013-01-22 08:39:50 -05:00
|
|
|
# Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-22 17:48:11 -04:00
|
|
|
#
|
|
|
|
# This software is licensed as described in the file COPYING, which
|
|
|
|
# you should have received as part of this distribution. The terms
|
|
|
|
# are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
#
|
|
|
|
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
# copies of the Software, and permit persons to whom the Software is
|
|
|
|
# furnished to do so, under the terms of the COPYING file.
|
|
|
|
#
|
|
|
|
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
# KIND, either express or implied.
|
|
|
|
#
|
|
|
|
###########################################################################
|
2000-10-09 07:31:55 -04:00
|
|
|
#
|
|
|
|
# Example input:
|
|
|
|
#
|
2013-01-03 20:50:28 -05:00
|
|
|
# MEM mprintf.c:1094 malloc(32) = e5718
|
|
|
|
# MEM mprintf.c:1103 realloc(e5718, 64) = e6118
|
|
|
|
# MEM sendf.c:232 free(f6520)
|
2000-10-09 07:31:55 -04:00
|
|
|
|
2003-10-24 04:09:33 -04:00
|
|
|
my $mallocs=0;
|
2004-04-29 10:33:19 -04:00
|
|
|
my $callocs=0;
|
2003-10-24 04:09:33 -04:00
|
|
|
my $reallocs=0;
|
|
|
|
my $strdups=0;
|
2013-07-18 14:04:02 -04:00
|
|
|
my $wcsdups=0;
|
2003-10-24 08:59:58 -04:00
|
|
|
my $showlimit;
|
2003-10-24 04:09:33 -04:00
|
|
|
|
2003-01-09 06:26:57 -05:00
|
|
|
while(1) {
|
2000-10-09 07:31:55 -04:00
|
|
|
if($ARGV[0] eq "-v") {
|
|
|
|
$verbose=1;
|
2003-01-09 06:26:57 -05:00
|
|
|
shift @ARGV;
|
2000-10-09 07:31:55 -04:00
|
|
|
}
|
2002-02-28 07:35:54 -05:00
|
|
|
elsif($ARGV[0] eq "-t") {
|
|
|
|
$trace=1;
|
2003-01-09 06:26:57 -05:00
|
|
|
shift @ARGV;
|
2002-02-28 07:35:54 -05:00
|
|
|
}
|
2003-10-24 08:59:58 -04:00
|
|
|
elsif($ARGV[0] eq "-l") {
|
|
|
|
# only show what alloc that caused a memlimit failure
|
|
|
|
$showlimit=1;
|
|
|
|
shift @ARGV;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
last;
|
|
|
|
}
|
2003-01-09 06:26:57 -05:00
|
|
|
}
|
2000-10-09 07:31:55 -04:00
|
|
|
|
2001-04-24 17:47:01 -04:00
|
|
|
my $maxmem;
|
|
|
|
|
|
|
|
sub newtotal {
|
|
|
|
my ($newtot)=@_;
|
|
|
|
# count a max here
|
|
|
|
|
|
|
|
if($newtot > $maxmem) {
|
|
|
|
$maxmem= $newtot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-09 06:26:57 -05:00
|
|
|
my $file = $ARGV[0];
|
|
|
|
|
|
|
|
if(! -f $file) {
|
|
|
|
print "Usage: memanalyze.pl [options] <dump file>\n",
|
|
|
|
"Options:\n",
|
2003-10-24 08:59:58 -04:00
|
|
|
" -l memlimit failure displayed\n",
|
2003-01-09 06:26:57 -05:00
|
|
|
" -v Verbose\n",
|
|
|
|
" -t Trace\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
open(FILE, "<$file");
|
|
|
|
|
2003-10-24 08:59:58 -04:00
|
|
|
if($showlimit) {
|
|
|
|
while(<FILE>) {
|
|
|
|
if(/^LIMIT.*memlimit$/) {
|
|
|
|
print $_;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(FILE);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-30 10:07:52 -04:00
|
|
|
my $lnum=0;
|
2003-01-09 06:26:57 -05:00
|
|
|
while(<FILE>) {
|
2000-10-09 07:31:55 -04:00
|
|
|
chomp $_;
|
|
|
|
$line = $_;
|
2005-06-30 10:07:52 -04:00
|
|
|
$lnum++;
|
2003-08-14 18:38:03 -04:00
|
|
|
if($line =~ /^LIMIT ([^ ]*):(\d*) (.*)/) {
|
|
|
|
# new memory limit test prefix
|
|
|
|
my $i = $3;
|
|
|
|
my ($source, $linenum) = ($1, $2);
|
|
|
|
if($trace && ($i =~ /([^ ]*) reached memlimit/)) {
|
|
|
|
print "LIMIT: $1 returned error at $source:$linenum\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif($line =~ /^MEM ([^ ]*):(\d*) (.*)/) {
|
2000-10-09 07:31:55 -04:00
|
|
|
# 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;
|
2004-05-07 14:54:09 -04:00
|
|
|
if(!exists $sizeataddr{$addr}) {
|
2000-10-09 07:31:55 -04:00
|
|
|
print "FREE ERROR: No memory allocated: $line\n";
|
|
|
|
}
|
2001-01-19 07:20:30 -05: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 07:31:55 -04:00
|
|
|
else {
|
|
|
|
$totalmem -= $sizeataddr{$addr};
|
2002-02-28 07:35:54 -05: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 17:47:01 -04:00
|
|
|
|
|
|
|
newtotal($totalmem);
|
|
|
|
$frees++;
|
|
|
|
|
2001-01-19 07:20:30 -05:00
|
|
|
$sizeataddr{$addr}=-1; # set -1 to mark as freed
|
|
|
|
$getmem{$addr}="$source:$linenum";
|
2001-04-24 17:47:01 -04:00
|
|
|
|
2000-10-09 07:31:55 -04:00
|
|
|
}
|
|
|
|
}
|
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;
|
2001-04-24 17:47:01 -04:00
|
|
|
|
|
|
|
if($sizeataddr{$addr}>0) {
|
|
|
|
# this means weeeeeirdo
|
2005-06-30 10:07:52 -04:00
|
|
|
print "Mixed debug compile ($source:$linenum at line $lnum), rebuild curl now\n";
|
2010-02-16 08:32:45 -05:00
|
|
|
print "We think $sizeataddr{$addr} bytes are already allocated at that memory address: $addr!\n";
|
2001-04-24 17:47:01 -04:00
|
|
|
}
|
|
|
|
|
2000-10-09 07:31:55 -04:00
|
|
|
$sizeataddr{$addr}=$size;
|
|
|
|
$totalmem += $size;
|
|
|
|
|
2002-02-28 07:35:54 -05:00
|
|
|
if($trace) {
|
|
|
|
print "MALLOC: malloc($size) at $source:$linenum",
|
|
|
|
" makes totally $totalmem bytes\n";
|
2001-04-24 17:47:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
newtotal($totalmem);
|
|
|
|
$mallocs++;
|
|
|
|
|
2000-10-09 07:31:55 -04:00
|
|
|
$getmem{$addr}="$source:$linenum";
|
|
|
|
}
|
2004-02-26 09:53:17 -05:00
|
|
|
elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
|
|
|
|
$size = $1*$2;
|
|
|
|
$addr = $3;
|
|
|
|
|
|
|
|
$arg1 = $1;
|
|
|
|
$arg2 = $2;
|
|
|
|
|
|
|
|
if($sizeataddr{$addr}>0) {
|
|
|
|
# this means weeeeeirdo
|
|
|
|
print "Mixed debug compile, rebuild curl now\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$sizeataddr{$addr}=$size;
|
|
|
|
$totalmem += $size;
|
|
|
|
|
|
|
|
if($trace) {
|
|
|
|
print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
|
|
|
|
" makes totally $totalmem bytes\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
newtotal($totalmem);
|
|
|
|
$callocs++;
|
|
|
|
|
|
|
|
$getmem{$addr}="$source:$linenum";
|
|
|
|
}
|
2005-08-04 19:05:36 -04:00
|
|
|
elsif($function =~ /realloc\((\(nil\)|0x([0-9a-f]*)), (\d*)\) = 0x([0-9a-f]*)/) {
|
|
|
|
my ($oldaddr, $newsize, $newaddr) = ($2, $3, $4);
|
2000-10-09 07:31:55 -04:00
|
|
|
|
|
|
|
$totalmem -= $sizeataddr{$oldaddr};
|
2002-02-28 07:35:54 -05:00
|
|
|
if($trace) {
|
|
|
|
printf("REALLOC: %d less bytes and ", $sizeataddr{$oldaddr});
|
|
|
|
}
|
2000-10-09 07:31:55 -04:00
|
|
|
$sizeataddr{$oldaddr}=0;
|
|
|
|
|
|
|
|
$totalmem += $newsize;
|
|
|
|
$sizeataddr{$newaddr}=$newsize;
|
|
|
|
|
2002-02-28 07:35:54 -05:00
|
|
|
if($trace) {
|
|
|
|
printf("%d more bytes ($source:$linenum)\n", $newsize);
|
|
|
|
}
|
|
|
|
|
2001-04-24 17:47:01 -04:00
|
|
|
newtotal($totalmem);
|
|
|
|
$reallocs++;
|
2010-02-14 14:40:18 -05:00
|
|
|
|
2000-10-09 07:31:55 -04:00
|
|
|
$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;
|
2001-04-24 17:47:01 -04:00
|
|
|
|
2002-02-28 07:35:54 -05:00
|
|
|
if($trace) {
|
2010-02-14 14:40:18 -05:00
|
|
|
printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
|
2002-02-28 07:35:54 -05:00
|
|
|
$getmem{$addr}, $totalmem);
|
|
|
|
}
|
|
|
|
|
2001-04-24 17:47:01 -04:00
|
|
|
newtotal($totalmem);
|
|
|
|
$strdups++;
|
2000-10-09 07:31:55 -04:00
|
|
|
}
|
2013-07-18 14:04:02 -04:00
|
|
|
elsif($function =~ /wcsdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
|
|
|
|
# wcsdup(a5b50) (8) = df7c0
|
|
|
|
|
|
|
|
$dup = $1;
|
|
|
|
$size = $2;
|
|
|
|
$addr = $3;
|
|
|
|
$getmem{$addr}="$source:$linenum";
|
|
|
|
$sizeataddr{$addr}=$size;
|
|
|
|
|
|
|
|
$totalmem += $size;
|
|
|
|
|
|
|
|
if($trace) {
|
|
|
|
printf("WCSDUP: $size bytes at %s, makes totally: %d bytes\n",
|
|
|
|
$getmem{$addr}, $totalmem);
|
|
|
|
}
|
|
|
|
|
|
|
|
newtotal($totalmem);
|
|
|
|
$wcsdups++;
|
|
|
|
}
|
2000-10-09 07:31:55 -04:00
|
|
|
else {
|
|
|
|
print "Not recognized input line: $function\n";
|
2010-02-14 14:40:18 -05:00
|
|
|
}
|
2000-10-09 07:31:55 -04:00
|
|
|
}
|
2013-01-03 20:50:28 -05:00
|
|
|
# FD url.c:1282 socket() = 5
|
2003-01-09 06:03:02 -05:00
|
|
|
elsif($_ =~ /^FD ([^ ]*):(\d*) (.*)/) {
|
2000-12-14 10:56:59 -05:00
|
|
|
# generic match for the filename+linenumber
|
|
|
|
$source = $1;
|
|
|
|
$linenum = $2;
|
|
|
|
$function = $3;
|
|
|
|
|
|
|
|
if($function =~ /socket\(\) = (\d*)/) {
|
|
|
|
$filedes{$1}=1;
|
|
|
|
$getfile{$1}="$source:$linenum";
|
|
|
|
$openfile++;
|
|
|
|
}
|
2011-07-29 07:25:52 -04:00
|
|
|
elsif($function =~ /socketpair\(\) = (\d*) (\d*)/) {
|
|
|
|
$filedes{$1}=1;
|
|
|
|
$getfile{$1}="$source:$linenum";
|
|
|
|
$openfile++;
|
|
|
|
$filedes{$2}=1;
|
|
|
|
$getfile{$2}="$source:$linenum";
|
|
|
|
$openfile++;
|
|
|
|
}
|
2000-12-18 11:13:37 -05:00
|
|
|
elsif($function =~ /accept\(\) = (\d*)/) {
|
|
|
|
$filedes{$1}=1;
|
|
|
|
$getfile{$1}="$source:$linenum";
|
|
|
|
$openfile++;
|
|
|
|
}
|
2000-12-14 10:56:59 -05:00
|
|
|
elsif($function =~ /sclose\((\d*)\)/) {
|
|
|
|
if($filedes{$1} != 1) {
|
|
|
|
print "Close without open: $line\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$filedes{$1}=0; # closed now
|
|
|
|
$openfile--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-03 20:50:28 -05:00
|
|
|
# FILE url.c:1282 fopen("blabla") = 0x5ddd
|
2003-01-09 06:03:02 -05:00
|
|
|
elsif($_ =~ /^FILE ([^ ]*):(\d*) (.*)/) {
|
2000-12-19 08:32:30 -05:00
|
|
|
# generic match for the filename+linenumber
|
|
|
|
$source = $1;
|
|
|
|
$linenum = $2;
|
|
|
|
$function = $3;
|
|
|
|
|
2013-01-22 08:39:50 -05:00
|
|
|
if($function =~ /f[d]*open\(\"(.*)\",\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
|
2004-02-26 09:53:17 -05:00
|
|
|
if($3 eq "(nil)") {
|
2000-12-19 08:32:30 -05:00
|
|
|
;
|
|
|
|
}
|
|
|
|
else {
|
2004-02-26 09:53:17 -05:00
|
|
|
$fopen{$4}=1;
|
|
|
|
$fopenfile{$4}="$source:$linenum";
|
2000-12-19 08:32:30 -05:00
|
|
|
$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 09:25:40 -04:00
|
|
|
}
|
2013-01-03 20:50:28 -05:00
|
|
|
# GETNAME url.c:1901 getnameinfo()
|
2004-04-22 09:09:00 -04:00
|
|
|
elsif($_ =~ /^GETNAME ([^ ]*):(\d*) (.*)/) {
|
|
|
|
# not much to do
|
|
|
|
}
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
# ADDR url.c:1282 getaddrinfo() = 0x5ddd
|
2003-01-09 06:03:02 -05:00
|
|
|
elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
|
2001-10-04 09:25:40 -04:00
|
|
|
# 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++;
|
|
|
|
}
|
2005-06-30 10:07:52 -04:00
|
|
|
if($trace) {
|
|
|
|
printf("GETADDRINFO ($source:$linenum)\n");
|
|
|
|
}
|
2001-10-04 09:25:40 -04:00
|
|
|
}
|
|
|
|
# fclose(0x1026c8)
|
|
|
|
elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
|
|
|
|
if(!$addrinfo{$1}) {
|
|
|
|
print "freeaddrinfo() without getaddrinfo(): $line\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$addrinfo{$1}=0;
|
|
|
|
$addrinfos--;
|
|
|
|
}
|
2005-06-30 10:07:52 -04:00
|
|
|
if($trace) {
|
|
|
|
printf("FREEADDRINFO ($source:$linenum)\n");
|
|
|
|
}
|
2001-10-04 09:25:40 -04:00
|
|
|
}
|
2010-02-14 14:40:18 -05:00
|
|
|
|
2000-12-19 08:32:30 -05:00
|
|
|
}
|
2000-10-09 07:31:55 -04:00
|
|
|
else {
|
|
|
|
print "Not recognized prefix line: $line\n";
|
|
|
|
}
|
|
|
|
}
|
2003-01-09 06:26:57 -05:00
|
|
|
close(FILE);
|
2000-10-09 07:31:55 -04:00
|
|
|
|
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};
|
2001-02-20 08:58:56 -05:00
|
|
|
if($size > 0) {
|
2000-11-21 10:50:17 -05:00
|
|
|
print "At $addr, there's $size bytes.\n";
|
|
|
|
print " allocated by ".$getmem{$addr}."\n";
|
|
|
|
}
|
2000-10-09 07:31:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-14 10:56:59 -05:00
|
|
|
if($openfile) {
|
|
|
|
for(keys %filedes) {
|
|
|
|
if($filedes{$_} == 1) {
|
|
|
|
print "Open file descriptor created at ".$getfile{$_}."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-12-19 08:32:30 -05: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 17:47:01 -04:00
|
|
|
|
2001-10-04 09:25:40 -04: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 17:47:01 -04:00
|
|
|
if($verbose) {
|
|
|
|
print "Mallocs: $mallocs\n",
|
|
|
|
"Reallocs: $reallocs\n",
|
2004-04-29 10:33:19 -04:00
|
|
|
"Callocs: $callocs\n",
|
2001-04-24 17:47:01 -04:00
|
|
|
"Strdups: $strdups\n",
|
2013-07-18 14:04:02 -04:00
|
|
|
"Wcsdups: $wcsdups\n",
|
2003-10-24 04:09:33 -04:00
|
|
|
"Frees: $frees\n",
|
2013-07-18 14:04:02 -04:00
|
|
|
"Allocations: ".($mallocs + $callocs + $reallocs + $strdups + $wcsdups)."\n";
|
2001-04-24 17:47:01 -04:00
|
|
|
|
|
|
|
print "Maximum allocated: $maxmem\n";
|
|
|
|
}
|