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\(0x([0-9a-f]*)/) {
113 if(!exists $sizeataddr{$addr}) {
114 print "FREE ERROR: No memory allocated: $line\n";
116 elsif(-1 == $sizeataddr{$addr}) {
117 print "FREE ERROR: Memory freed twice: $line\n";
118 print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
121 $totalmem -= $sizeataddr{$addr};
123 print "FREE: malloc at ".$getmem{$addr}." is freed again at $source:$linenum\n";
124 printf("FREE: %d bytes freed, left allocated: $totalmem bytes\n", $sizeataddr{$addr});
130 $sizeataddr{$addr}=-1; # set -1 to mark as freed
131 $getmem{$addr}="$source:$linenum";
135 elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
139 if($sizeataddr{$addr}>0) {
140 # this means weeeeeirdo
141 print "Mixed debug compile ($source:$linenum at line $lnum), rebuild curl now\n";
142 print "We think $sizeataddr{$addr} bytes are already allocated at that memory address: $addr!\n";
145 $sizeataddr{$addr}=$size;
149 print "MALLOC: malloc($size) at $source:$linenum",
150 " makes totally $totalmem bytes\n";
156 $getmem{$addr}="$source:$linenum";
158 elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
165 if($sizeataddr{$addr}>0) {
166 # this means weeeeeirdo
167 print "Mixed debug compile, rebuild curl now\n";
170 $sizeataddr{$addr}=$size;
174 print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
175 " makes totally $totalmem bytes\n";
181 $getmem{$addr}="$source:$linenum";
183 elsif($function =~ /realloc\((\(nil\)|0x([0-9a-f]*)), (\d*)\) = 0x([0-9a-f]*)/) {
184 my ($oldaddr, $newsize, $newaddr) = ($2, $3, $4);
186 $totalmem -= $sizeataddr{$oldaddr};
188 printf("REALLOC: %d less bytes and ", $sizeataddr{$oldaddr});
190 $sizeataddr{$oldaddr}=0;
192 $totalmem += $newsize;
193 $sizeataddr{$newaddr}=$newsize;
196 printf("%d more bytes ($source:$linenum)\n", $newsize);
202 $getmem{$oldaddr}="";
203 $getmem{$newaddr}="$source:$linenum";
205 elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
206 # strdup(a5b50) (8) = df7c0
211 $getmem{$addr}="$source:$linenum";
212 $sizeataddr{$addr}=$size;
217 printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
218 $getmem{$addr}, $totalmem);
224 elsif($function =~ /wcsdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
225 # wcsdup(a5b50) (8) = df7c0
230 $getmem{$addr}="$source:$linenum";
231 $sizeataddr{$addr}=$size;
236 printf("WCSDUP: $size bytes at %s, makes totally: %d bytes\n",
237 $getmem{$addr}, $totalmem);
244 print "Not recognized input line: $function\n";
247 # FD url.c:1282 socket() = 5
248 elsif($_ =~ /^FD ([^ ]*):(\d*) (.*)/) {
249 # generic match for the filename+linenumber
254 if($function =~ /socket\(\) = (\d*)/) {
256 $getfile{$1}="$source:$linenum";
259 elsif($function =~ /socketpair\(\) = (\d*) (\d*)/) {
261 $getfile{$1}="$source:$linenum";
264 $getfile{$2}="$source:$linenum";
267 elsif($function =~ /accept\(\) = (\d*)/) {
269 $getfile{$1}="$source:$linenum";
272 elsif($function =~ /sclose\((\d*)\)/) {
273 if($filedes{$1} != 1) {
274 print "Close without open: $line\n";
277 $filedes{$1}=0; # closed now
282 # FILE url.c:1282 fopen("blabla") = 0x5ddd
283 elsif($_ =~ /^FILE ([^ ]*):(\d*) (.*)/) {
284 # generic match for the filename+linenumber
289 if($function =~ /f[d]*open\(\"(.*)\",\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
295 $fopenfile{$4}="$source:$linenum";
300 elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
302 print "fclose() without fopen(): $line\n";
310 # GETNAME url.c:1901 getnameinfo()
311 elsif($_ =~ /^GETNAME ([^ ]*):(\d*) (.*)/) {
315 # ADDR url.c:1282 getaddrinfo() = 0x5ddd
316 elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
317 # generic match for the filename+linenumber
322 if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
324 if($add eq "(nil)") {
329 $addrinfofile{$add}="$source:$linenum";
333 printf("GETADDRINFO ($source:$linenum)\n");
337 elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
339 print "freeaddrinfo() without getaddrinfo(): $line\n";
346 printf("FREEADDRINFO ($source:$linenum)\n");
352 print "Not recognized prefix line: $line\n";
358 print "Leak detected: memory still allocated: $totalmem bytes\n";
360 for(keys %sizeataddr) {
362 $size = $sizeataddr{$addr};
364 print "At $addr, there's $size bytes.\n";
365 print " allocated by ".$getmem{$addr}."\n";
372 if($filedes{$_} == 1) {
373 print "Open file descriptor created at ".$getfile{$_}."\n";
379 print "Open FILE handles left at:\n";
381 if($fopen{$_} == 1) {
382 print "fopen() called at ".$fopenfile{$_}."\n";
388 print "IPv6-style name resolve data left at:\n";
389 for(keys %addrinfofile) {
390 if($addrinfo{$_} == 1) {
391 print "getaddrinfo() called at ".$addrinfofile{$_}."\n";
397 print "Mallocs: $mallocs\n",
398 "Reallocs: $reallocs\n",
399 "Callocs: $callocs\n",
400 "Strdups: $strdups\n",
401 "Wcsdups: $wcsdups\n",
403 "Allocations: ".($mallocs + $callocs + $reallocs + $strdups + $wcsdups)."\n";
405 print "Maximum allocated: $maxmem\n";