5 # MEM mprintf.c:1094 malloc(32) = e5718
6 # MEM mprintf.c:1103 realloc(e5718, 64) = e6118
7 # MEM sendf.c:232 free(f6520)
16 if($ARGV[0] eq "-v") {
20 elsif($ARGV[0] eq "-t") {
24 elsif($ARGV[0] eq "-l") {
25 # only show what alloc that caused a memlimit failure
40 if($newtot > $maxmem) {
48 print "Usage: memanalyze.pl [options] <dump file>\n",
50 " -l memlimit failure displayed\n",
60 if(/^LIMIT.*memlimit$/) {
75 if($line =~ /^LIMIT ([^ ]*):(\d*) (.*)/) {
76 # new memory limit test prefix
78 my ($source, $linenum) = ($1, $2);
79 if($trace && ($i =~ /([^ ]*) reached memlimit/)) {
80 print "LIMIT: $1 returned error at $source:$linenum\n";
83 elsif($line =~ /^MEM ([^ ]*):(\d*) (.*)/) {
84 # generic match for the filename+linenumber
89 if($function =~ /free\(0x([0-9a-f]*)/) {
91 if(!exists $sizeataddr{$addr}) {
92 print "FREE ERROR: No memory allocated: $line\n";
94 elsif(-1 == $sizeataddr{$addr}) {
95 print "FREE ERROR: Memory freed twice: $line\n";
96 print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
99 $totalmem -= $sizeataddr{$addr};
101 print "FREE: malloc at ".$getmem{$addr}." is freed again at $source:$linenum\n";
102 printf("FREE: %d bytes freed, left allocated: $totalmem bytes\n", $sizeataddr{$addr});
108 $sizeataddr{$addr}=-1; # set -1 to mark as freed
109 $getmem{$addr}="$source:$linenum";
113 elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
117 if($sizeataddr{$addr}>0) {
118 # this means weeeeeirdo
119 print "Mixed debug compile ($source:$linenum at line $lnum), rebuild curl now\n";
120 print "We think $sizeataddr{$addr} bytes are already allocated at that memory address: $addr!\n";
123 $sizeataddr{$addr}=$size;
127 print "MALLOC: malloc($size) at $source:$linenum",
128 " makes totally $totalmem bytes\n";
134 $getmem{$addr}="$source:$linenum";
136 elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
143 if($sizeataddr{$addr}>0) {
144 # this means weeeeeirdo
145 print "Mixed debug compile, rebuild curl now\n";
148 $sizeataddr{$addr}=$size;
152 print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
153 " makes totally $totalmem bytes\n";
159 $getmem{$addr}="$source:$linenum";
161 elsif($function =~ /realloc\((\(nil\)|0x([0-9a-f]*)), (\d*)\) = 0x([0-9a-f]*)/) {
162 my ($oldaddr, $newsize, $newaddr) = ($2, $3, $4);
164 $totalmem -= $sizeataddr{$oldaddr};
166 printf("REALLOC: %d less bytes and ", $sizeataddr{$oldaddr});
168 $sizeataddr{$oldaddr}=0;
170 $totalmem += $newsize;
171 $sizeataddr{$newaddr}=$newsize;
174 printf("%d more bytes ($source:$linenum)\n", $newsize);
180 $getmem{$oldaddr}="";
181 $getmem{$newaddr}="$source:$linenum";
183 elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
184 # strdup(a5b50) (8) = df7c0
189 $getmem{$addr}="$source:$linenum";
190 $sizeataddr{$addr}=$size;
195 printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
196 $getmem{$addr}, $totalmem);
203 print "Not recognized input line: $function\n";
206 # FD url.c:1282 socket() = 5
207 elsif($_ =~ /^FD ([^ ]*):(\d*) (.*)/) {
208 # generic match for the filename+linenumber
213 if($function =~ /socket\(\) = (\d*)/) {
215 $getfile{$1}="$source:$linenum";
218 elsif($function =~ /accept\(\) = (\d*)/) {
220 $getfile{$1}="$source:$linenum";
223 elsif($function =~ /sclose\((\d*)\)/) {
224 if($filedes{$1} != 1) {
225 print "Close without open: $line\n";
228 $filedes{$1}=0; # closed now
233 # FILE url.c:1282 fopen("blabla") = 0x5ddd
234 elsif($_ =~ /^FILE ([^ ]*):(\d*) (.*)/) {
235 # generic match for the filename+linenumber
240 if($function =~ /f[d]*open\(\"([^\"]*)\",\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
246 $fopenfile{$4}="$source:$linenum";
251 elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
253 print "fclose() without fopen(): $line\n";
261 # GETNAME url.c:1901 getnameinfo()
262 elsif($_ =~ /^GETNAME ([^ ]*):(\d*) (.*)/) {
266 # ADDR url.c:1282 getaddrinfo() = 0x5ddd
267 elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
268 # generic match for the filename+linenumber
273 if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
275 if($add eq "(nil)") {
280 $addrinfofile{$add}="$source:$linenum";
284 printf("GETADDRINFO ($source:$linenum)\n");
288 elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
290 print "freeaddrinfo() without getaddrinfo(): $line\n";
297 printf("FREEADDRINFO ($source:$linenum)\n");
303 print "Not recognized prefix line: $line\n";
309 print "Leak detected: memory still allocated: $totalmem bytes\n";
311 for(keys %sizeataddr) {
313 $size = $sizeataddr{$addr};
315 print "At $addr, there's $size bytes.\n";
316 print " allocated by ".$getmem{$addr}."\n";
323 if($filedes{$_} == 1) {
324 print "Open file descriptor created at ".$getfile{$_}."\n";
330 print "Open FILE handles left at:\n";
332 if($fopen{$_} == 1) {
333 print "fopen() called at ".$fopenfile{$_}."\n";
339 print "IPv6-style name resolve data left at:\n";
340 for(keys %addrinfofile) {
341 if($addrinfo{$_} == 1) {
342 print "getaddrinfo() called at ".$addrinfofile{$_}."\n";
348 print "Mallocs: $mallocs\n",
349 "Reallocs: $reallocs\n",
350 "Callocs: $callocs\n",
351 "Strdups: $strdups\n",
353 "Allocations: ".($mallocs + $callocs + $reallocs + $strdups)."\n";
355 print "Maximum allocated: $maxmem\n";