2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
11 # This software is licensed as described in the file COPYING, which
12 # you should have received as part of this distribution. The terms
13 # are also available at http://curl.haxx.se/docs/copyright.html.
15 # You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 # copies of the Software, and permit persons to whom the Software is
17 # furnished to do so, under the terms of the COPYING file.
19 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 # KIND, either express or implied.
22 ###########################################################################
26 # MEM mprintf.c:1094 malloc(32) = e5718
27 # MEM mprintf.c:1103 realloc(e5718, 64) = e6118
28 # MEM sendf.c:232 free(f6520)
38 if($ARGV[0] eq "-v") {
42 elsif($ARGV[0] eq "-t") {
46 elsif($ARGV[0] eq "-l") {
47 # only show what alloc that caused a memlimit failure
62 if($newtot > $maxmem) {
70 print "Usage: memanalyze.pl [options] <dump file>\n",
72 " -l memlimit failure displayed\n",
82 if(/^LIMIT.*memlimit$/) {
97 if($line =~ /^LIMIT ([^ ]*):(\d*) (.*)/) {
98 # new memory limit test prefix
100 my ($source, $linenum) = ($1, $2);
101 if($trace && ($i =~ /([^ ]*) reached memlimit/)) {
102 print "LIMIT: $1 returned error at $source:$linenum\n";
105 elsif($line =~ /^MEM ([^ ]*):(\d*) (.*)/) {
106 # generic match for the filename+linenumber
111 if($function =~ /free\((\(nil\)|0x([0-9a-f]*))/) {
114 ; # do nothing when free(NULL)
116 elsif(!exists $sizeataddr{$addr}) {
117 print "FREE ERROR: No memory allocated: $line\n";
119 elsif(-1 == $sizeataddr{$addr}) {
120 print "FREE ERROR: Memory freed twice: $line\n";
121 print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
124 $totalmem -= $sizeataddr{$addr};
126 print "FREE: malloc at ".$getmem{$addr}." is freed again at $source:$linenum\n";
127 printf("FREE: %d bytes freed, left allocated: $totalmem bytes\n", $sizeataddr{$addr});
133 $sizeataddr{$addr}=-1; # set -1 to mark as freed
134 $getmem{$addr}="$source:$linenum";
138 elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
142 if($sizeataddr{$addr}>0) {
143 # this means weeeeeirdo
144 print "Mixed debug compile ($source:$linenum at line $lnum), rebuild curl now\n";
145 print "We think $sizeataddr{$addr} bytes are already allocated at that memory address: $addr!\n";
148 $sizeataddr{$addr}=$size;
152 print "MALLOC: malloc($size) at $source:$linenum",
153 " makes totally $totalmem bytes\n";
159 $getmem{$addr}="$source:$linenum";
161 elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
168 if($sizeataddr{$addr}>0) {
169 # this means weeeeeirdo
170 print "Mixed debug compile, rebuild curl now\n";
173 $sizeataddr{$addr}=$size;
177 print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
178 " makes totally $totalmem bytes\n";
184 $getmem{$addr}="$source:$linenum";
186 elsif($function =~ /realloc\((\(nil\)|0x([0-9a-f]*)), (\d*)\) = 0x([0-9a-f]*)/) {
187 my ($oldaddr, $newsize, $newaddr) = ($2, $3, $4);
189 $totalmem -= $sizeataddr{$oldaddr};
191 printf("REALLOC: %d less bytes and ", $sizeataddr{$oldaddr});
193 $sizeataddr{$oldaddr}=0;
195 $totalmem += $newsize;
196 $sizeataddr{$newaddr}=$newsize;
199 printf("%d more bytes ($source:$linenum)\n", $newsize);
205 $getmem{$oldaddr}="";
206 $getmem{$newaddr}="$source:$linenum";
208 elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
209 # strdup(a5b50) (8) = df7c0
214 $getmem{$addr}="$source:$linenum";
215 $sizeataddr{$addr}=$size;
220 printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
221 $getmem{$addr}, $totalmem);
227 elsif($function =~ /wcsdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
228 # wcsdup(a5b50) (8) = df7c0
233 $getmem{$addr}="$source:$linenum";
234 $sizeataddr{$addr}=$size;
239 printf("WCSDUP: $size bytes at %s, makes totally: %d bytes\n",
240 $getmem{$addr}, $totalmem);
247 print "Not recognized input line: $function\n";
250 # FD url.c:1282 socket() = 5
251 elsif($_ =~ /^FD ([^ ]*):(\d*) (.*)/) {
252 # generic match for the filename+linenumber
257 if($function =~ /socket\(\) = (\d*)/) {
259 $getfile{$1}="$source:$linenum";
262 elsif($function =~ /socketpair\(\) = (\d*) (\d*)/) {
264 $getfile{$1}="$source:$linenum";
267 $getfile{$2}="$source:$linenum";
270 elsif($function =~ /accept\(\) = (\d*)/) {
272 $getfile{$1}="$source:$linenum";
275 elsif($function =~ /sclose\((\d*)\)/) {
276 if($filedes{$1} != 1) {
277 print "Close without open: $line\n";
280 $filedes{$1}=0; # closed now
285 # FILE url.c:1282 fopen("blabla") = 0x5ddd
286 elsif($_ =~ /^FILE ([^ ]*):(\d*) (.*)/) {
287 # generic match for the filename+linenumber
292 if($function =~ /f[d]*open\(\"(.*)\",\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
298 $fopenfile{$4}="$source:$linenum";
303 elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
305 print "fclose() without fopen(): $line\n";
313 # GETNAME url.c:1901 getnameinfo()
314 elsif($_ =~ /^GETNAME ([^ ]*):(\d*) (.*)/) {
318 # ADDR url.c:1282 getaddrinfo() = 0x5ddd
319 elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
320 # generic match for the filename+linenumber
325 if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
327 if($add eq "(nil)") {
332 $addrinfofile{$add}="$source:$linenum";
336 printf("GETADDRINFO ($source:$linenum)\n");
340 elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
342 print "freeaddrinfo() without getaddrinfo(): $line\n";
349 printf("FREEADDRINFO ($source:$linenum)\n");
355 print "Not recognized prefix line: $line\n";
361 print "Leak detected: memory still allocated: $totalmem bytes\n";
363 for(keys %sizeataddr) {
365 $size = $sizeataddr{$addr};
367 print "At $addr, there's $size bytes.\n";
368 print " allocated by ".$getmem{$addr}."\n";
375 if($filedes{$_} == 1) {
376 print "Open file descriptor created at ".$getfile{$_}."\n";
382 print "Open FILE handles left at:\n";
384 if($fopen{$_} == 1) {
385 print "fopen() called at ".$fopenfile{$_}."\n";
391 print "IPv6-style name resolve data left at:\n";
392 for(keys %addrinfofile) {
393 if($addrinfo{$_} == 1) {
394 print "getaddrinfo() called at ".$addrinfofile{$_}."\n";
400 print "Mallocs: $mallocs\n",
401 "Reallocs: $reallocs\n",
402 "Callocs: $callocs\n",
403 "Strdups: $strdups\n",
404 "Wcsdups: $wcsdups\n",
406 "Allocations: ".($mallocs + $callocs + $reallocs + $strdups + $wcsdups)."\n";
408 print "Maximum allocated: $maxmem\n";