5 # MEM mprintf.c:1094 malloc(32) = e5718
6 # MEM mprintf.c:1103 realloc(e5718, 64) = e6118
7 # MEM sendf.c:232 free(f6520)
15 if($ARGV[0] eq "-v") {
19 elsif($ARGV[0] eq "-t") {
23 elsif($ARGV[0] eq "-l") {
24 # only show what alloc that caused a memlimit failure
39 if($newtot > $maxmem) {
47 print "Usage: memanalyze.pl [options] <dump file>\n",
49 " -l memlimit failure displayed\n",
59 if(/^LIMIT.*memlimit$/) {
74 if($line =~ /^LIMIT ([^ ]*):(\d*) (.*)/) {
75 # new memory limit test prefix
77 my ($source, $linenum) = ($1, $2);
78 if($trace && ($i =~ /([^ ]*) reached memlimit/)) {
79 print "LIMIT: $1 returned error at $source:$linenum\n";
82 elsif($line =~ /^MEM ([^ ]*):(\d*) (.*)/) {
83 # generic match for the filename+linenumber
88 if($function =~ /free\(0x([0-9a-f]*)/) {
90 if($sizeataddr{$addr} == 0) {
91 print "FREE ERROR: No memory allocated: $line\n";
93 elsif(-1 == $sizeataddr{$addr}) {
94 print "FREE ERROR: Memory freed twice: $line\n";
95 print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
98 $totalmem -= $sizeataddr{$addr};
100 print "FREE: malloc at ".$getmem{$addr}." is freed again at $source:$linenum\n";
101 printf("FREE: %d bytes freed, left allocated: $totalmem bytes\n", $sizeataddr{$addr});
107 $sizeataddr{$addr}=-1; # set -1 to mark as freed
108 $getmem{$addr}="$source:$linenum";
112 elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
116 if($sizeataddr{$addr}>0) {
117 # this means weeeeeirdo
118 print "Mixed debug compile, rebuild curl now\n";
121 $sizeataddr{$addr}=$size;
125 print "MALLOC: malloc($size) at $source:$linenum",
126 " makes totally $totalmem bytes\n";
132 $getmem{$addr}="$source:$linenum";
134 elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
141 if($sizeataddr{$addr}>0) {
142 # this means weeeeeirdo
143 print "Mixed debug compile, rebuild curl now\n";
146 $sizeataddr{$addr}=$size;
150 print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
151 " makes totally $totalmem bytes\n";
157 $getmem{$addr}="$source:$linenum";
159 elsif($function =~ /realloc\(0x([0-9a-f]*), (\d*)\) = 0x([0-9a-f]*)/) {
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 =~ /fopen\(\"([^\"]*)\",\"([^\"]*)\"\) = (\(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 # ADDR url.c:1282 getaddrinfo() = 0x5ddd
262 elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
263 # generic match for the filename+linenumber
268 if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
270 if($add eq "(nil)") {
275 $addrinfofile{$add}="$source:$linenum";
280 elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
282 print "freeaddrinfo() without getaddrinfo(): $line\n";
293 print "Not recognized prefix line: $line\n";
299 print "Leak detected: memory still allocated: $totalmem bytes\n";
301 for(keys %sizeataddr) {
303 $size = $sizeataddr{$addr};
305 print "At $addr, there's $size bytes.\n";
306 print " allocated by ".$getmem{$addr}."\n";
313 if($filedes{$_} == 1) {
314 print "Open file descriptor created at ".$getfile{$_}."\n";
320 print "Open FILE handles left at:\n";
322 if($fopen{$_} == 1) {
323 print "fopen() called at ".$fopenfile{$_}."\n";
329 print "IPv6-style name resolve data left at:\n";
330 for(keys %addrinfofile) {
331 if($addrinfo{$_} == 1) {
332 print "getaddrinfo() called at ".$addrinfofile{$_}."\n";
338 print "Mallocs: $mallocs\n",
339 "Reallocs: $reallocs\n",
340 "Callocs: $callcs\n",
341 "Strdups: $strdups\n",
343 "Allocations: ".($mallocs + $reallocs + $strdups)."\n";
345 print "Maximum allocated: $maxmem\n";