Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / memanalyze.pl
blob763f9a3b93db408c3bc48eb87f9ded633efa5d75
1 #!/usr/bin/env perl
3 # Example input:
5 # MEM mprintf.c:1094 malloc(32) = e5718
6 # MEM mprintf.c:1103 realloc(e5718, 64) = e6118
7 # MEM sendf.c:232 free(f6520)
9 my $mallocs=0;
10 my $callocs=0;
11 my $reallocs=0;
12 my $strdups=0;
13 my $showlimit;
15 while(1) {
16 if($ARGV[0] eq "-v") {
17 $verbose=1;
18 shift @ARGV;
20 elsif($ARGV[0] eq "-t") {
21 $trace=1;
22 shift @ARGV;
24 elsif($ARGV[0] eq "-l") {
25 # only show what alloc that caused a memlimit failure
26 $showlimit=1;
27 shift @ARGV;
29 else {
30 last;
34 my $maxmem;
36 sub newtotal {
37 my ($newtot)=@_;
38 # count a max here
40 if($newtot > $maxmem) {
41 $maxmem= $newtot;
45 my $file = $ARGV[0];
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;
56 open(FILE, "<$file");
58 if($showlimit) {
59 while(<FILE>) {
60 if(/^LIMIT.*memlimit$/) {
61 print $_;
62 last;
65 close(FILE);
66 exit;
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";
83 elsif($line =~ /^MEM ([^ ]*):(\d*) (.*)/) {
84 # generic match for the filename+linenumber
85 $source = $1;
86 $linenum = $2;
87 $function = $3;
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";
94 elsif(-1 == $sizeataddr{$addr}) {
95 print "FREE ERROR: Memory freed twice: $line\n";
96 print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
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});
105 newtotal($totalmem);
106 $frees++;
108 $sizeataddr{$addr}=-1; # set -1 to mark as freed
109 $getmem{$addr}="$source:$linenum";
113 elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
114 $size = $1;
115 $addr = $2;
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;
124 $totalmem += $size;
126 if($trace) {
127 print "MALLOC: malloc($size) at $source:$linenum",
128 " makes totally $totalmem bytes\n";
131 newtotal($totalmem);
132 $mallocs++;
134 $getmem{$addr}="$source:$linenum";
136 elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
137 $size = $1*$2;
138 $addr = $3;
140 $arg1 = $1;
141 $arg2 = $2;
143 if($sizeataddr{$addr}>0) {
144 # this means weeeeeirdo
145 print "Mixed debug compile, rebuild curl now\n";
148 $sizeataddr{$addr}=$size;
149 $totalmem += $size;
151 if($trace) {
152 print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
153 " makes totally $totalmem bytes\n";
156 newtotal($totalmem);
157 $callocs++;
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};
165 if($trace) {
166 printf("REALLOC: %d less bytes and ", $sizeataddr{$oldaddr});
168 $sizeataddr{$oldaddr}=0;
170 $totalmem += $newsize;
171 $sizeataddr{$newaddr}=$newsize;
173 if($trace) {
174 printf("%d more bytes ($source:$linenum)\n", $newsize);
177 newtotal($totalmem);
178 $reallocs++;
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
186 $dup = $1;
187 $size = $2;
188 $addr = $3;
189 $getmem{$addr}="$source:$linenum";
190 $sizeataddr{$addr}=$size;
192 $totalmem += $size;
194 if($trace) {
195 printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
196 $getmem{$addr}, $totalmem);
199 newtotal($totalmem);
200 $strdups++;
202 else {
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
209 $source = $1;
210 $linenum = $2;
211 $function = $3;
213 if($function =~ /socket\(\) = (\d*)/) {
214 $filedes{$1}=1;
215 $getfile{$1}="$source:$linenum";
216 $openfile++;
218 elsif($function =~ /accept\(\) = (\d*)/) {
219 $filedes{$1}=1;
220 $getfile{$1}="$source:$linenum";
221 $openfile++;
223 elsif($function =~ /sclose\((\d*)\)/) {
224 if($filedes{$1} != 1) {
225 print "Close without open: $line\n";
227 else {
228 $filedes{$1}=0; # closed now
229 $openfile--;
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;
240 if($function =~ /f[d]*open\(\"([^\"]*)\",\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
241 if($3 eq "(nil)") {
244 else {
245 $fopen{$4}=1;
246 $fopenfile{$4}="$source:$linenum";
247 $fopens++;
250 # fclose(0x1026c8)
251 elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
252 if(!$fopen{$1}) {
253 print "fclose() without fopen(): $line\n";
255 else {
256 $fopen{$1}=0;
257 $fopens--;
261 # GETNAME url.c:1901 getnameinfo()
262 elsif($_ =~ /^GETNAME ([^ ]*):(\d*) (.*)/) {
263 # not much to do
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;
273 if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
274 my $add = $2;
275 if($add eq "(nil)") {
278 else {
279 $addrinfo{$add}=1;
280 $addrinfofile{$add}="$source:$linenum";
281 $addrinfos++;
283 if($trace) {
284 printf("GETADDRINFO ($source:$linenum)\n");
287 # fclose(0x1026c8)
288 elsif($function =~ /freeaddrinfo\(0x([0-9a-f]*)\)/) {
289 if(!$addrinfo{$1}) {
290 print "freeaddrinfo() without getaddrinfo(): $line\n";
292 else {
293 $addrinfo{$1}=0;
294 $addrinfos--;
296 if($trace) {
297 printf("FREEADDRINFO ($source:$linenum)\n");
302 else {
303 print "Not recognized prefix line: $line\n";
306 close(FILE);
308 if($totalmem) {
309 print "Leak detected: memory still allocated: $totalmem bytes\n";
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";
321 if($openfile) {
322 for(keys %filedes) {
323 if($filedes{$_} == 1) {
324 print "Open file descriptor created at ".$getfile{$_}."\n";
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";
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";
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";
355 print "Maximum allocated: $maxmem\n";