adjusted to work with the modified fopen() line and the new calloc line
[platform/upstream/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 $reallocs=0;
11 my $strdups=0;
12 my $showlimit;
13
14 while(1) {
15     if($ARGV[0] eq "-v") {
16         $verbose=1;
17         shift @ARGV;
18     }
19     elsif($ARGV[0] eq "-t") {
20         $trace=1;
21         shift @ARGV;
22     }
23     elsif($ARGV[0] eq "-l") {
24         # only show what alloc that caused a memlimit failure
25         $showlimit=1;
26         shift @ARGV;
27     }
28     else {
29         last;
30     }
31 }
32
33 my $maxmem;
34
35 sub newtotal {
36     my ($newtot)=@_;
37     # count a max here
38
39     if($newtot > $maxmem) {
40         $maxmem= $newtot;
41     }
42 }
43
44 my $file = $ARGV[0];
45
46 if(! -f $file) {
47     print "Usage: memanalyze.pl [options] <dump file>\n",
48     "Options:\n",
49     " -l  memlimit failure displayed\n",
50     " -v  Verbose\n",
51     " -t  Trace\n";
52     exit;
53 }
54
55 open(FILE, "<$file");
56
57 if($showlimit) {
58     while(<FILE>) {
59         if(/^LIMIT.*memlimit$/) {
60             print $_;
61             last;
62         }
63     }
64     close(FILE);
65     exit;
66 }
67
68
69
70 while(<FILE>) {
71     chomp $_;
72     $line = $_;
73
74     if($line =~ /^LIMIT ([^ ]*):(\d*) (.*)/) {
75         # new memory limit test prefix
76         my $i = $3;
77         my ($source, $linenum) = ($1, $2);
78         if($trace && ($i =~ /([^ ]*) reached memlimit/)) {
79             print "LIMIT: $1 returned error at $source:$linenum\n";
80         }
81     }
82     elsif($line =~ /^MEM ([^ ]*):(\d*) (.*)/) {
83         # generic match for the filename+linenumber
84         $source = $1;
85         $linenum = $2;
86         $function = $3;
87
88         if($function =~ /free\(0x([0-9a-f]*)/) {
89             $addr = $1;
90             if($sizeataddr{$addr} == 0) {
91                 print "FREE ERROR: No memory allocated: $line\n";
92             }
93             elsif(-1 == $sizeataddr{$addr}) {
94                 print "FREE ERROR: Memory freed twice: $line\n";
95                 print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
96             }
97             else {
98                 $totalmem -= $sizeataddr{$addr};
99                 if($trace) {
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});
102                 }
103
104                 newtotal($totalmem);
105                 $frees++;
106
107                 $sizeataddr{$addr}=-1; # set -1 to mark as freed
108                 $getmem{$addr}="$source:$linenum";
109
110             }
111         }
112         elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
113             $size = $1;
114             $addr = $2;
115
116             if($sizeataddr{$addr}>0) {
117                 # this means weeeeeirdo
118                 print "Mixed debug compile, rebuild curl now\n";
119             }
120
121             $sizeataddr{$addr}=$size;
122             $totalmem += $size;
123
124             if($trace) {
125                 print "MALLOC: malloc($size) at $source:$linenum",
126                 " makes totally $totalmem bytes\n";
127             }
128
129             newtotal($totalmem);
130             $mallocs++;
131
132             $getmem{$addr}="$source:$linenum";
133         }
134         elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
135             $size = $1*$2;
136             $addr = $3;
137
138             $arg1 = $1;
139             $arg2 = $2;
140
141             if($sizeataddr{$addr}>0) {
142                 # this means weeeeeirdo
143                 print "Mixed debug compile, rebuild curl now\n";
144             }
145
146             $sizeataddr{$addr}=$size;
147             $totalmem += $size;
148
149             if($trace) {
150                 print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
151                 " makes totally $totalmem bytes\n";
152             }
153
154             newtotal($totalmem);
155             $callocs++;
156
157             $getmem{$addr}="$source:$linenum";
158         }
159         elsif($function =~ /realloc\(0x([0-9a-f]*), (\d*)\) = 0x([0-9a-f]*)/) {
160             $oldaddr = $1;
161             $newsize = $2;
162             $newaddr = $3;
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 =~ /fopen\(\"([^\"]*)\",\"([^\"]*)\"\) = (\(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     # ADDR url.c:1282 getaddrinfo() = 0x5ddd
262     elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
263         # generic match for the filename+linenumber
264         $source = $1;
265         $linenum = $2;
266         $function = $3;
267
268         if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
269             my $add = $2;
270             if($add eq "(nil)") {
271                 ;
272             }
273             else {
274                 $addrinfo{$add}=1;
275                 $addrinfofile{$add}="$source:$linenum";
276                 $addrinfos++;
277             }
278         }
279         # fclose(0x1026c8)
280         elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
281             if(!$addrinfo{$1}) {
282                 print "freeaddrinfo() without getaddrinfo(): $line\n";
283             }
284             else {
285                 $addrinfo{$1}=0;
286                 $addrinfos--;
287             }
288         }
289
290         
291     }
292     else {
293         print "Not recognized prefix line: $line\n";
294     }
295 }
296 close(FILE);
297
298 if($totalmem) {
299     print "Leak detected: memory still allocated: $totalmem bytes\n";
300
301     for(keys %sizeataddr) {
302         $addr = $_;
303         $size = $sizeataddr{$addr};
304         if($size > 0) {
305             print "At $addr, there's $size bytes.\n";
306             print " allocated by ".$getmem{$addr}."\n";
307         }
308     }
309 }
310
311 if($openfile) {
312     for(keys %filedes) {
313         if($filedes{$_} == 1) {
314             print "Open file descriptor created at ".$getfile{$_}."\n";
315         }
316     }
317 }
318
319 if($fopens) {
320     print "Open FILE handles left at:\n";
321     for(keys %fopen) {
322         if($fopen{$_} == 1) {
323             print "fopen() called at ".$fopenfile{$_}."\n";
324         }
325     }
326 }
327
328 if($addrinfos) {
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";
333         }
334     }
335 }
336
337 if($verbose) {
338     print "Mallocs: $mallocs\n",
339     "Reallocs: $reallocs\n",
340     "Callocs: $callcs\n",
341     "Strdups:  $strdups\n",
342     "Frees: $frees\n",
343     "Allocations: ".($mallocs + $reallocs + $strdups)."\n";
344
345     print "Maximum allocated: $maxmem\n";
346 }