Git init
[external/curl.git] / tests / memanalyze.pl
1 #!/usr/bin/env perl
2 #
3 # Example input:
4 #
5 # MEM mprintf.c:1094 malloc(32) = e5718
6 # MEM mprintf.c:1103 realloc(e5718, 64) = e6118
7 # MEM sendf.c:232 free(f6520)
8
9 my $mallocs=0;
10 my $callocs=0;
11 my $reallocs=0;
12 my $strdups=0;
13 my $showlimit;
14
15 while(1) {
16     if($ARGV[0] eq "-v") {
17         $verbose=1;
18         shift @ARGV;
19     }
20     elsif($ARGV[0] eq "-t") {
21         $trace=1;
22         shift @ARGV;
23     }
24     elsif($ARGV[0] eq "-l") {
25         # only show what alloc that caused a memlimit failure
26         $showlimit=1;
27         shift @ARGV;
28     }
29     else {
30         last;
31     }
32 }
33
34 my $maxmem;
35
36 sub newtotal {
37     my ($newtot)=@_;
38     # count a max here
39
40     if($newtot > $maxmem) {
41         $maxmem= $newtot;
42     }
43 }
44
45 my $file = $ARGV[0];
46
47 if(! -f $file) {
48     print "Usage: memanalyze.pl [options] <dump file>\n",
49     "Options:\n",
50     " -l  memlimit failure displayed\n",
51     " -v  Verbose\n",
52     " -t  Trace\n";
53     exit;
54 }
55
56 open(FILE, "<$file");
57
58 if($showlimit) {
59     while(<FILE>) {
60         if(/^LIMIT.*memlimit$/) {
61             print $_;
62             last;
63         }
64     }
65     close(FILE);
66     exit;
67 }
68
69
70 my $lnum=0;
71 while(<FILE>) {
72     chomp $_;
73     $line = $_;
74     $lnum++;
75     if($line =~ /^LIMIT ([^ ]*):(\d*) (.*)/) {
76         # new memory limit test prefix
77         my $i = $3;
78         my ($source, $linenum) = ($1, $2);
79         if($trace && ($i =~ /([^ ]*) reached memlimit/)) {
80             print "LIMIT: $1 returned error at $source:$linenum\n";
81         }
82     }
83     elsif($line =~ /^MEM ([^ ]*):(\d*) (.*)/) {
84         # generic match for the filename+linenumber
85         $source = $1;
86         $linenum = $2;
87         $function = $3;
88
89         if($function =~ /free\(0x([0-9a-f]*)/) {
90             $addr = $1;
91             if(!exists $sizeataddr{$addr}) {
92                 print "FREE ERROR: No memory allocated: $line\n";
93             }
94             elsif(-1 == $sizeataddr{$addr}) {
95                 print "FREE ERROR: Memory freed twice: $line\n";
96                 print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
97             }
98             else {
99                 $totalmem -= $sizeataddr{$addr};
100                 if($trace) {
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});
103                 }
104
105                 newtotal($totalmem);
106                 $frees++;
107
108                 $sizeataddr{$addr}=-1; # set -1 to mark as freed
109                 $getmem{$addr}="$source:$linenum";
110
111             }
112         }
113         elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
114             $size = $1;
115             $addr = $2;
116
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";
121             }
122
123             $sizeataddr{$addr}=$size;
124             $totalmem += $size;
125
126             if($trace) {
127                 print "MALLOC: malloc($size) at $source:$linenum",
128                 " makes totally $totalmem bytes\n";
129             }
130
131             newtotal($totalmem);
132             $mallocs++;
133
134             $getmem{$addr}="$source:$linenum";
135         }
136         elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
137             $size = $1*$2;
138             $addr = $3;
139
140             $arg1 = $1;
141             $arg2 = $2;
142
143             if($sizeataddr{$addr}>0) {
144                 # this means weeeeeirdo
145                 print "Mixed debug compile, rebuild curl now\n";
146             }
147
148             $sizeataddr{$addr}=$size;
149             $totalmem += $size;
150
151             if($trace) {
152                 print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
153                 " makes totally $totalmem bytes\n";
154             }
155
156             newtotal($totalmem);
157             $callocs++;
158
159             $getmem{$addr}="$source:$linenum";
160         }
161         elsif($function =~ /realloc\((\(nil\)|0x([0-9a-f]*)), (\d*)\) = 0x([0-9a-f]*)/) {
162             my ($oldaddr, $newsize, $newaddr) = ($2, $3, $4);
163
164             $totalmem -= $sizeataddr{$oldaddr};
165             if($trace) {
166                 printf("REALLOC: %d less bytes and ", $sizeataddr{$oldaddr});
167             }
168             $sizeataddr{$oldaddr}=0;
169
170             $totalmem += $newsize;
171             $sizeataddr{$newaddr}=$newsize;
172
173             if($trace) {
174                 printf("%d more bytes ($source:$linenum)\n", $newsize);
175             }
176
177             newtotal($totalmem);
178             $reallocs++;
179
180             $getmem{$oldaddr}="";
181             $getmem{$newaddr}="$source:$linenum";
182         }
183         elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
184             # strdup(a5b50) (8) = df7c0
185
186             $dup = $1;
187             $size = $2;
188             $addr = $3;
189             $getmem{$addr}="$source:$linenum";
190             $sizeataddr{$addr}=$size;
191
192             $totalmem += $size;
193
194             if($trace) {
195                 printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
196                        $getmem{$addr}, $totalmem);
197             }
198
199             newtotal($totalmem);
200             $strdups++;
201         }
202         else {
203             print "Not recognized input line: $function\n";
204         }
205     }
206     # FD url.c:1282 socket() = 5
207     elsif($_ =~ /^FD ([^ ]*):(\d*) (.*)/) {
208         # generic match for the filename+linenumber
209         $source = $1;
210         $linenum = $2;
211         $function = $3;
212
213         if($function =~ /socket\(\) = (\d*)/) {
214             $filedes{$1}=1;
215             $getfile{$1}="$source:$linenum";
216             $openfile++;
217         }
218         elsif($function =~ /accept\(\) = (\d*)/) {
219             $filedes{$1}=1;
220             $getfile{$1}="$source:$linenum";
221             $openfile++;
222         }
223         elsif($function =~ /sclose\((\d*)\)/) {
224             if($filedes{$1} != 1) {
225                 print "Close without open: $line\n";
226             }
227             else {
228                 $filedes{$1}=0; # closed now
229                 $openfile--;
230             }
231         }
232     }
233     # FILE url.c:1282 fopen("blabla") = 0x5ddd
234     elsif($_ =~ /^FILE ([^ ]*):(\d*) (.*)/) {
235         # generic match for the filename+linenumber
236         $source = $1;
237         $linenum = $2;
238         $function = $3;
239
240         if($function =~ /f[d]*open\(\"([^\"]*)\",\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
241             if($3 eq "(nil)") {
242                 ;
243             }
244             else {
245                 $fopen{$4}=1;
246                 $fopenfile{$4}="$source:$linenum";
247                 $fopens++;
248             }
249         }
250         # fclose(0x1026c8)
251         elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
252             if(!$fopen{$1}) {
253                 print "fclose() without fopen(): $line\n";
254             }
255             else {
256                 $fopen{$1}=0;
257                 $fopens--;
258             }
259         }
260     }
261     # GETNAME url.c:1901 getnameinfo()
262     elsif($_ =~ /^GETNAME ([^ ]*):(\d*) (.*)/) {
263         # not much to do
264     }
265
266     # ADDR url.c:1282 getaddrinfo() = 0x5ddd
267     elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
268         # generic match for the filename+linenumber
269         $source = $1;
270         $linenum = $2;
271         $function = $3;
272
273         if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
274             my $add = $2;
275             if($add eq "(nil)") {
276                 ;
277             }
278             else {
279                 $addrinfo{$add}=1;
280                 $addrinfofile{$add}="$source:$linenum";
281                 $addrinfos++;
282             }
283             if($trace) {
284                 printf("GETADDRINFO ($source:$linenum)\n");
285             }
286         }
287         # fclose(0x1026c8)
288         elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
289             if(!$addrinfo{$1}) {
290                 print "freeaddrinfo() without getaddrinfo(): $line\n";
291             }
292             else {
293                 $addrinfo{$1}=0;
294                 $addrinfos--;
295             }
296             if($trace) {
297                 printf("FREEADDRINFO ($source:$linenum)\n");
298             }
299         }
300
301     }
302     else {
303         print "Not recognized prefix line: $line\n";
304     }
305 }
306 close(FILE);
307
308 if($totalmem) {
309     print "Leak detected: memory still allocated: $totalmem bytes\n";
310
311     for(keys %sizeataddr) {
312         $addr = $_;
313         $size = $sizeataddr{$addr};
314         if($size > 0) {
315             print "At $addr, there's $size bytes.\n";
316             print " allocated by ".$getmem{$addr}."\n";
317         }
318     }
319 }
320
321 if($openfile) {
322     for(keys %filedes) {
323         if($filedes{$_} == 1) {
324             print "Open file descriptor created at ".$getfile{$_}."\n";
325         }
326     }
327 }
328
329 if($fopens) {
330     print "Open FILE handles left at:\n";
331     for(keys %fopen) {
332         if($fopen{$_} == 1) {
333             print "fopen() called at ".$fopenfile{$_}."\n";
334         }
335     }
336 }
337
338 if($addrinfos) {
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";
343         }
344     }
345 }
346
347 if($verbose) {
348     print "Mallocs: $mallocs\n",
349     "Reallocs: $reallocs\n",
350     "Callocs: $callocs\n",
351     "Strdups:  $strdups\n",
352     "Frees: $frees\n",
353     "Allocations: ".($mallocs + $callocs + $reallocs + $strdups)."\n";
354
355     print "Maximum allocated: $maxmem\n";
356 }