2 #***************************************************************************
4 # Project ___| | | | _ \| |
6 # | (__| |_| | _ <| |___
7 # \___|\___/|_| \_\_____|
9 # Copyright (C) 1998 - 2017, 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 https://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)
41 if($ARGV[0] eq "-v") {
45 elsif($ARGV[0] eq "-t") {
49 elsif($ARGV[0] eq "-l") {
50 # only show what alloc that caused a memlimit failure
65 if($newtot > $maxmem) {
73 print "Usage: memanalyze.pl [options] <dump file>\n",
75 " -l memlimit failure displayed\n",
85 if(/^LIMIT.*memlimit$/) {
100 if($line =~ /^LIMIT ([^ ]*):(\d*) (.*)/) {
101 # new memory limit test prefix
103 my ($source, $linenum) = ($1, $2);
104 if($trace && ($i =~ /([^ ]*) reached memlimit/)) {
105 print "LIMIT: $1 returned error at $source:$linenum\n";
108 elsif($line =~ /^MEM ([^ ]*):(\d*) (.*)/) {
109 # generic match for the filename+linenumber
114 if($function =~ /free\((\(nil\)|0x([0-9a-f]*))/) {
117 ; # do nothing when free(NULL)
119 elsif(!exists $sizeataddr{$addr}) {
120 print "FREE ERROR: No memory allocated: $line\n";
122 elsif(-1 == $sizeataddr{$addr}) {
123 print "FREE ERROR: Memory freed twice: $line\n";
124 print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
127 $totalmem -= $sizeataddr{$addr};
129 print "FREE: malloc at ".$getmem{$addr}." is freed again at $source:$linenum\n";
130 printf("FREE: %d bytes freed, left allocated: $totalmem bytes\n", $sizeataddr{$addr});
136 $sizeataddr{$addr}=-1; # set -1 to mark as freed
137 $getmem{$addr}="$source:$linenum";
141 elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
145 if($sizeataddr{$addr}>0) {
146 # this means weeeeeirdo
147 print "Mixed debug compile ($source:$linenum at line $lnum), rebuild curl now\n";
148 print "We think $sizeataddr{$addr} bytes are already allocated at that memory address: $addr!\n";
151 $sizeataddr{$addr}=$size;
155 print "MALLOC: malloc($size) at $source:$linenum",
156 " makes totally $totalmem bytes\n";
162 $getmem{$addr}="$source:$linenum";
164 elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
171 if($sizeataddr{$addr}>0) {
172 # this means weeeeeirdo
173 print "Mixed debug compile, rebuild curl now\n";
176 $sizeataddr{$addr}=$size;
180 print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
181 " makes totally $totalmem bytes\n";
187 $getmem{$addr}="$source:$linenum";
189 elsif($function =~ /realloc\((\(nil\)|0x([0-9a-f]*)), (\d*)\) = 0x([0-9a-f]*)/) {
190 my ($oldaddr, $newsize, $newaddr) = ($2, $3, $4);
192 $totalmem -= $sizeataddr{$oldaddr};
194 printf("REALLOC: %d less bytes and ", $sizeataddr{$oldaddr});
196 $sizeataddr{$oldaddr}=0;
198 $totalmem += $newsize;
199 $sizeataddr{$newaddr}=$newsize;
202 printf("%d more bytes ($source:$linenum)\n", $newsize);
208 $getmem{$oldaddr}="";
209 $getmem{$newaddr}="$source:$linenum";
211 elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
212 # strdup(a5b50) (8) = df7c0
217 $getmem{$addr}="$source:$linenum";
218 $sizeataddr{$addr}=$size;
223 printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
224 $getmem{$addr}, $totalmem);
230 elsif($function =~ /wcsdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
231 # wcsdup(a5b50) (8) = df7c0
236 $getmem{$addr}="$source:$linenum";
237 $sizeataddr{$addr}=$size;
242 printf("WCSDUP: $size bytes at %s, makes totally: %d bytes\n",
243 $getmem{$addr}, $totalmem);
250 print "Not recognized input line: $function\n";
253 # FD url.c:1282 socket() = 5
254 elsif($_ =~ /^FD ([^ ]*):(\d*) (.*)/) {
255 # generic match for the filename+linenumber
260 if($function =~ /socket\(\) = (\d*)/) {
262 $getfile{$1}="$source:$linenum";
264 $sockets++; # number of socket() calls
266 elsif($function =~ /socketpair\(\) = (\d*) (\d*)/) {
268 $getfile{$1}="$source:$linenum";
271 $getfile{$2}="$source:$linenum";
274 elsif($function =~ /accept\(\) = (\d*)/) {
276 $getfile{$1}="$source:$linenum";
279 elsif($function =~ /sclose\((\d*)\)/) {
280 if($filedes{$1} != 1) {
281 print "Close without open: $line\n";
284 $filedes{$1}=0; # closed now
289 # FILE url.c:1282 fopen("blabla") = 0x5ddd
290 elsif($_ =~ /^FILE ([^ ]*):(\d*) (.*)/) {
291 # generic match for the filename+linenumber
296 if($function =~ /f[d]*open\(\"(.*)\",\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
302 $fopenfile{$4}="$source:$linenum";
307 elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
309 print "fclose() without fopen(): $line\n";
317 # GETNAME url.c:1901 getnameinfo()
318 elsif($_ =~ /^GETNAME ([^ ]*):(\d*) (.*)/) {
321 # SEND url.c:1901 send(83) = 83
322 elsif($_ =~ /^SEND ([^ ]*):(\d*) (.*)/) {
325 # RECV url.c:1901 recv(102400) = 256
326 elsif($_ =~ /^RECV ([^ ]*):(\d*) (.*)/) {
330 # ADDR url.c:1282 getaddrinfo() = 0x5ddd
331 elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
332 # generic match for the filename+linenumber
337 if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
339 if($add eq "(nil)") {
344 $addrinfofile{$add}="$source:$linenum";
348 printf("GETADDRINFO ($source:$linenum)\n");
352 elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
354 print "freeaddrinfo() without getaddrinfo(): $line\n";
361 printf("FREEADDRINFO ($source:$linenum)\n");
367 print "Not recognized prefix line: $line\n";
373 print "Leak detected: memory still allocated: $totalmem bytes\n";
375 for(keys %sizeataddr) {
377 $size = $sizeataddr{$addr};
379 print "At $addr, there's $size bytes.\n";
380 print " allocated by ".$getmem{$addr}."\n";
387 if($filedes{$_} == 1) {
388 print "Open file descriptor created at ".$getfile{$_}."\n";
394 print "Open FILE handles left at:\n";
396 if($fopen{$_} == 1) {
397 print "fopen() called at ".$fopenfile{$_}."\n";
403 print "IPv6-style name resolve data left at:\n";
404 for(keys %addrinfofile) {
405 if($addrinfo{$_} == 1) {
406 print "getaddrinfo() called at ".$addrinfofile{$_}."\n";
412 print "Mallocs: $mallocs\n",
413 "Reallocs: $reallocs\n",
414 "Callocs: $callocs\n",
415 "Strdups: $strdups\n",
416 "Wcsdups: $wcsdups\n",
420 "Sockets: $sockets\n",
421 "Allocations: ".($mallocs + $callocs + $reallocs + $strdups + $wcsdups)."\n",
422 "Operations: ".($mallocs + $callocs + $reallocs + $strdups + $wcsdups + $sends + $recvs + $sockets)."\n";
424 print "Maximum allocated: $maxmem\n";