When re-using a connection, the path pointers were not setup properly so
[platform/upstream/curl.git] / memanalyze.pl
1 #!/usr/bin/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 do {
10     if($ARGV[0] eq "-v") {
11         $verbose=1;
12     }
13 } while (shift @ARGV);
14
15 my $maxmem;
16
17 sub newtotal {
18     my ($newtot)=@_;
19     # count a max here
20
21     if($newtot > $maxmem) {
22         $maxmem= $newtot;
23     }
24 }
25
26 while(<STDIN>) {
27     chomp $_;
28     $line = $_;
29
30     if($line =~ /^MEM ([^:]*):(\d*) (.*)/) {
31         # generic match for the filename+linenumber
32         $source = $1;
33         $linenum = $2;
34         $function = $3;
35
36         if($function =~ /free\(0x([0-9a-f]*)/) {
37             $addr = $1;
38             if($sizeataddr{$addr} == 0) {
39                 print "FREE ERROR: No memory allocated: $line\n";
40             }
41             elsif(-1 == $sizeataddr{$addr}) {
42                 print "FREE ERROR: Memory freed twice: $line\n";
43                 print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
44             }
45             else {
46                 if(0 && $verbose) {
47                     print "malloc at ".$getmem{$addr}." is freed again at $source:$linenum\n";
48                 }
49
50                 $totalmem -= $sizeataddr{$addr};
51
52                 newtotal($totalmem);
53                 $frees++;
54
55                 $sizeataddr{$addr}=-1; # set -1 to mark as freed
56                 $getmem{$addr}="$source:$linenum";
57
58             }
59         }
60         elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
61             $size = $1;
62             $addr = $2;
63
64             if($sizeataddr{$addr}>0) {
65                 # this means weeeeeirdo
66                 print "Fucked up debug compile, rebuild curl now\n";
67             }
68
69             $sizeataddr{$addr}=$size;
70             $totalmem += $size;
71
72             if(0 && $verbose) {
73                 print "malloc($size) at $source:$linenum\n";
74             }
75
76             newtotal($totalmem);
77             $mallocs++;
78
79             $getmem{$addr}="$source:$linenum";
80         }
81         elsif($function =~ /realloc\(0x([0-9a-f]*), (\d*)\) = 0x([0-9a-f]*)/) {
82             $oldaddr = $1;
83             $newsize = $2;
84             $newaddr = $3;
85
86             $totalmem -= $sizeataddr{$oldaddr};
87             $sizeataddr{$oldaddr}=0;
88
89             $totalmem += $newsize;
90             $sizeataddr{$newaddr}=$newsize;
91
92             newtotal($totalmem);
93             $reallocs++;
94             
95             $getmem{$oldaddr}="";
96             $getmem{$newaddr}="$source:$linenum";
97         }
98         elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
99             # strdup(a5b50) (8) = df7c0
100
101             $dup = $1;
102             $size = $2;
103             $addr = $3;
104             $getmem{$addr}="$source:$linenum";
105             $sizeataddr{$addr}=$size;
106
107             $totalmem += $size;
108
109             newtotal($totalmem);
110             $strdups++;
111         }
112         else {
113             print "Not recognized input line: $function\n";
114         }        
115     }
116     # FD url.c:1282 socket() = 5
117     elsif($_ =~ /^FD ([^:]*):(\d*) (.*)/) {
118         # generic match for the filename+linenumber
119         $source = $1;
120         $linenum = $2;
121         $function = $3;
122
123         if($function =~ /socket\(\) = (\d*)/) {
124             $filedes{$1}=1;
125             $getfile{$1}="$source:$linenum";
126             $openfile++;
127         }
128         elsif($function =~ /accept\(\) = (\d*)/) {
129             $filedes{$1}=1;
130             $getfile{$1}="$source:$linenum";
131             $openfile++;
132         }
133         elsif($function =~ /sclose\((\d*)\)/) {
134             if($filedes{$1} != 1) {
135                 print "Close without open: $line\n";
136             }
137             else {
138                 $filedes{$1}=0; # closed now
139                 $openfile--;
140             }
141         }
142     }
143     # FILE url.c:1282 fopen("blabla") = 0x5ddd
144     elsif($_ =~ /^FILE ([^:]*):(\d*) (.*)/) {
145         # generic match for the filename+linenumber
146         $source = $1;
147         $linenum = $2;
148         $function = $3;
149
150         if($function =~ /fopen\(\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
151             if($2 eq "(nil)") {
152                 ;
153             }
154             else {
155                 $fopen{$3}=1;
156                 $fopenfile{$3}="$source:$linenum";
157                 $fopens++;
158             }
159         }
160         # fclose(0x1026c8)
161         elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
162             if(!$fopen{$1}) {
163                 print "fclose() without fopen(): $line\n";
164             }
165             else {
166                 $fopen{$1}=0;
167                 $fopens--;
168             }
169         }
170     }
171     else {
172         print "Not recognized prefix line: $line\n";
173     }
174 }
175
176 if($totalmem) {
177     print "Leak detected: memory still allocated: $totalmem bytes\n";
178
179     for(keys %sizeataddr) {
180         $addr = $_;
181         $size = $sizeataddr{$addr};
182         if($size > 0) {
183             print "At $addr, there's $size bytes.\n";
184             print " allocated by ".$getmem{$addr}."\n";
185         }
186     }
187 }
188
189 if($openfile) {
190     for(keys %filedes) {
191         if($filedes{$_} == 1) {
192             print "Open file descriptor created at ".$getfile{$_}."\n";
193         }
194     }
195 }
196
197 if($fopens) {
198     print "Open FILE handles left at:\n";
199     for(keys %fopen) {
200         if($fopen{$_} == 1) {
201             print "fopen() called at ".$fopenfile{$_}."\n";
202         }
203     }
204 }
205
206 if($verbose) {
207     print "Mallocs: $mallocs\n",
208     "Reallocs: $reallocs\n",
209     "Strdups:  $strdups\n",
210     "Frees: $frees\n";
211
212     print "Maximum allocated: $maxmem\n";
213 }