Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/memanalyze.pl
2654 views
1
#!/usr/bin/env perl
2
#***************************************************************************
3
# _ _ ____ _
4
# Project ___| | | | _ \| |
5
# / __| | | | |_) | |
6
# | (__| |_| | _ <| |___
7
# \___|\___/|_| \_\_____|
8
#
9
# Copyright (C) Daniel Stenberg, <[email protected]>, et al.
10
#
11
# This software is licensed as described in the file COPYING, which
12
# you should have received as part of this distribution. The terms
13
# are also available at https://curl.se/docs/copyright.html.
14
#
15
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
16
# copies of the Software, and permit persons to whom the Software is
17
# furnished to do so, under the terms of the COPYING file.
18
#
19
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20
# KIND, either express or implied.
21
#
22
# SPDX-License-Identifier: curl
23
#
24
###########################################################################
25
#
26
# Example input:
27
#
28
# MEM mprintf.c:1094 malloc(32) = e5718
29
# MEM mprintf.c:1103 realloc(e5718, 64) = e6118
30
# MEM sendf.c:232 free(f6520)
31
32
use strict;
33
use warnings;
34
35
my $mallocs=0;
36
my $callocs=0;
37
my $reallocs=0;
38
my $strdups=0;
39
my $wcsdups=0;
40
my $showlimit=0;
41
my $sends=0;
42
my $recvs=0;
43
my $sockets=0;
44
my $verbose=0;
45
my $trace=0;
46
47
while(@ARGV) {
48
if($ARGV[0] eq "-v") {
49
$verbose=1;
50
shift @ARGV;
51
}
52
elsif($ARGV[0] eq "-t") {
53
$trace=1;
54
shift @ARGV;
55
}
56
elsif($ARGV[0] eq "-l") {
57
# only show what alloc that caused a memlimit failure
58
$showlimit=1;
59
shift @ARGV;
60
}
61
else {
62
last;
63
}
64
}
65
66
my $memsum = 0; # the total number of memory allocated over the lifetime
67
my $maxmem = 0; # the high water mark
68
69
sub newtotal {
70
my ($newtot)=@_;
71
# count a max here
72
73
if($newtot > $maxmem) {
74
$maxmem = $newtot;
75
}
76
}
77
78
my $file = $ARGV[0] || '';
79
80
if(! -f $file) {
81
print "Usage: memanalyze.pl [options] <dump file>\n",
82
"Options:\n",
83
" -l memlimit failure displayed\n",
84
" -v Verbose\n",
85
" -t Trace\n";
86
exit;
87
}
88
89
open(my $fileh, "<", "$file");
90
91
if($showlimit) {
92
while(<$fileh>) {
93
if(/^LIMIT.*memlimit$/) {
94
print $_;
95
last;
96
}
97
}
98
close($fileh);
99
exit;
100
}
101
102
my %sizeataddr;
103
my %getmem;
104
105
my $totalmem = 0;
106
my $frees = 0;
107
108
my $dup;
109
my $size;
110
my $addr;
111
112
my %filedes;
113
my %getfile;
114
115
my %fopen;
116
my %fopenfile;
117
my $openfile = 0;
118
my $fopens = 0;
119
120
my %addrinfo;
121
my %addrinfofile;
122
my $addrinfos = 0;
123
124
my $source;
125
my $linenum;
126
my $function;
127
128
my $lnum = 0;
129
while(<$fileh>) {
130
chomp $_;
131
my $line = $_;
132
$lnum++;
133
if($line =~ /^LIMIT ([^ ]*):(\d*) (.*)/) {
134
# new memory limit test prefix
135
my $i = $3;
136
my ($source, $linenum) = ($1, $2);
137
if($trace && ($i =~ /([^ ]*) reached memlimit/)) {
138
print "LIMIT: $1 returned error at $source:$linenum\n";
139
}
140
}
141
elsif($line =~ /^MEM ([^ ]*):(\d*) (.*)/) {
142
# generic match for the filename+linenumber
143
$source = $1;
144
$linenum = $2;
145
$function = $3;
146
147
if($function =~ /free\((\(nil\)|0x([0-9a-f]*))/) {
148
$addr = $2;
149
if($1 eq "(nil)") {
150
; # do nothing when free(NULL)
151
}
152
elsif(!exists $sizeataddr{$addr}) {
153
print "FREE ERROR: No memory allocated: $line\n";
154
}
155
elsif(-1 == $sizeataddr{$addr}) {
156
print "FREE ERROR: Memory freed twice: $line\n";
157
print "FREE ERROR: Previously freed at: ".$getmem{$addr}."\n";
158
}
159
else {
160
$totalmem -= $sizeataddr{$addr};
161
if($trace) {
162
print "FREE: malloc at ".$getmem{$addr}." is freed again at $source:$linenum\n";
163
printf("FREE: %d bytes freed, left allocated: $totalmem bytes\n", $sizeataddr{$addr});
164
}
165
166
newtotal($totalmem);
167
$frees++;
168
169
$sizeataddr{$addr}=-1; # set -1 to mark as freed
170
$getmem{$addr}="$source:$linenum";
171
172
}
173
}
174
elsif($function =~ /malloc\((\d*)\) = 0x([0-9a-f]*)/) {
175
$size = $1;
176
$addr = $2;
177
178
if($sizeataddr{$addr} && $sizeataddr{$addr}>0) {
179
# this means weeeeeirdo
180
print "Mixed debug compile ($source:$linenum at line $lnum), rebuild curl now\n";
181
print "We think $sizeataddr{$addr} bytes are already allocated at that memory address: $addr!\n";
182
}
183
184
$sizeataddr{$addr} = $size;
185
$totalmem += $size;
186
$memsum += $size;
187
188
if($trace) {
189
print "MALLOC: malloc($size) at $source:$linenum",
190
" makes totally $totalmem bytes\n";
191
}
192
193
newtotal($totalmem);
194
$mallocs++;
195
196
$getmem{$addr}="$source:$linenum";
197
}
198
elsif($function =~ /calloc\((\d*),(\d*)\) = 0x([0-9a-f]*)/) {
199
$size = $1*$2;
200
$addr = $3;
201
202
my $arg1 = $1;
203
my $arg2 = $2;
204
205
if($sizeataddr{$addr} && $sizeataddr{$addr}>0) {
206
# this means weeeeeirdo
207
print "Mixed debug compile, rebuild curl now\n";
208
}
209
210
$sizeataddr{$addr}=$size;
211
$totalmem += $size;
212
$memsum += $size;
213
214
if($trace) {
215
print "CALLOC: calloc($arg1,$arg2) at $source:$linenum",
216
" makes totally $totalmem bytes\n";
217
}
218
219
newtotal($totalmem);
220
$callocs++;
221
222
$getmem{$addr}="$source:$linenum";
223
}
224
elsif($function =~ /realloc\((\(nil\)|0x([0-9a-f]*)), (\d*)\) = 0x([0-9a-f]*)/) {
225
my ($oldaddr, $newsize, $newaddr) = ($2, $3, $4);
226
227
if($oldaddr) {
228
my $oldsize = $sizeataddr{$oldaddr} ? $sizeataddr{$oldaddr} : 0;
229
230
$totalmem -= $oldsize;
231
if($trace) {
232
printf("REALLOC: %d less bytes and ", $oldsize);
233
}
234
$sizeataddr{$oldaddr}=0;
235
236
$getmem{$oldaddr}="";
237
}
238
239
$totalmem += $newsize;
240
$memsum += $newsize;
241
$sizeataddr{$newaddr}=$newsize;
242
243
if($trace) {
244
printf("%d more bytes ($source:$linenum)\n", $newsize);
245
}
246
247
newtotal($totalmem);
248
$reallocs++;
249
250
$getmem{$newaddr}="$source:$linenum";
251
}
252
elsif($function =~ /strdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
253
# strdup(a5b50) (8) = df7c0
254
255
$dup = $1;
256
$size = $2;
257
$addr = $3;
258
$getmem{$addr}="$source:$linenum";
259
$sizeataddr{$addr}=$size;
260
261
$totalmem += $size;
262
$memsum += $size;
263
264
if($trace) {
265
printf("STRDUP: $size bytes at %s, makes totally: %d bytes\n",
266
$getmem{$addr}, $totalmem);
267
}
268
269
newtotal($totalmem);
270
$strdups++;
271
}
272
elsif($function =~ /wcsdup\(0x([0-9a-f]*)\) \((\d*)\) = 0x([0-9a-f]*)/) {
273
# wcsdup(a5b50) (8) = df7c0
274
275
$dup = $1;
276
$size = $2;
277
$addr = $3;
278
$getmem{$addr}="$source:$linenum";
279
$sizeataddr{$addr}=$size;
280
281
$totalmem += $size;
282
$memsum += $size;
283
284
if($trace) {
285
printf("WCSDUP: $size bytes at %s, makes totally: %d bytes\n",
286
$getmem{$addr}, $totalmem);
287
}
288
289
newtotal($totalmem);
290
$wcsdups++;
291
}
292
else {
293
print "Not recognized input line: $function\n";
294
}
295
}
296
# FD url.c:1282 socket() = 5
297
elsif($_ =~ /^FD ([^ ]*):(\d*) (.*)/) {
298
# generic match for the filename+linenumber
299
$source = $1;
300
$linenum = $2;
301
$function = $3;
302
303
if($function =~ /socket\(\) = (\d*)/) {
304
$filedes{$1}=1;
305
$getfile{$1}="$source:$linenum";
306
$openfile++;
307
$sockets++; # number of socket() calls
308
}
309
elsif($function =~ /socketpair\(\) = (\d*) (\d*)/) {
310
$filedes{$1}=1;
311
$getfile{$1}="$source:$linenum";
312
$openfile++;
313
$filedes{$2}=1;
314
$getfile{$2}="$source:$linenum";
315
$openfile++;
316
}
317
elsif($function =~ /accept\(\) = (\d*)/) {
318
$filedes{$1}=1;
319
$getfile{$1}="$source:$linenum";
320
$openfile++;
321
}
322
elsif($function =~ /sclose\((\d*)\)/) {
323
if($filedes{$1} != 1) {
324
print "Close without open: $line\n";
325
}
326
else {
327
$filedes{$1}=0; # closed now
328
$openfile--;
329
}
330
}
331
}
332
# FILE url.c:1282 fopen("blabla") = 0x5ddd
333
elsif($_ =~ /^FILE ([^ ]*):(\d*) (.*)/) {
334
# generic match for the filename+linenumber
335
$source = $1;
336
$linenum = $2;
337
$function = $3;
338
339
if($function =~ /f[d]*open\(\"(.*)\",\"([^\"]*)\"\) = (\(nil\)|0x([0-9a-f]*))/) {
340
if($3 eq "(nil)") {
341
;
342
}
343
else {
344
$fopen{$4}=1;
345
$fopenfile{$4}="$source:$linenum";
346
$fopens++;
347
}
348
}
349
# fclose(0x1026c8)
350
elsif($function =~ /fclose\(0x([0-9a-f]*)\)/) {
351
if(!$fopen{$1}) {
352
print "fclose() without fopen(): $line\n";
353
}
354
else {
355
$fopen{$1}=0;
356
$fopens--;
357
}
358
}
359
}
360
# GETNAME url.c:1901 getnameinfo()
361
elsif($_ =~ /^GETNAME ([^ ]*):(\d*) (.*)/) {
362
# not much to do
363
}
364
# SEND url.c:1901 send(83) = 83
365
elsif($_ =~ /^SEND ([^ ]*):(\d*) (.*)/) {
366
$sends++;
367
}
368
# RECV url.c:1901 recv(102400) = 256
369
elsif($_ =~ /^RECV ([^ ]*):(\d*) (.*)/) {
370
$recvs++;
371
}
372
373
# ADDR url.c:1282 getaddrinfo() = 0x5ddd
374
elsif($_ =~ /^ADDR ([^ ]*):(\d*) (.*)/) {
375
# generic match for the filename+linenumber
376
$source = $1;
377
$linenum = $2;
378
$function = $3;
379
380
if($function =~ /getaddrinfo\(\) = (\(nil\)|0x([0-9a-f]*))/) {
381
my $add = $1;
382
if($add eq "(nil)") {
383
;
384
}
385
else {
386
$addrinfo{$add}=1;
387
$addrinfofile{$add}="$source:$linenum";
388
$addrinfos++;
389
}
390
if($trace) {
391
printf("GETADDRINFO ($source:$linenum)\n");
392
}
393
}
394
# fclose(0x1026c8)
395
elsif($function =~ /freeaddrinfo\((0x[0-9a-f]*)\)/) {
396
my $addr = $1;
397
if(!$addrinfo{$addr}) {
398
print "freeaddrinfo() without getaddrinfo(): $line\n";
399
}
400
else {
401
$addrinfo{$addr}=0;
402
$addrinfos--;
403
}
404
if($trace) {
405
printf("FREEADDRINFO ($source:$linenum)\n");
406
}
407
}
408
409
}
410
else {
411
print "Not recognized prefix line: $line\n";
412
}
413
}
414
close($fileh);
415
416
if($totalmem) {
417
print "Leak detected: memory still allocated: $totalmem bytes\n";
418
419
for(keys %sizeataddr) {
420
$addr = $_;
421
$size = $sizeataddr{$addr};
422
if($size > 0) {
423
print "At $addr, there's $size bytes.\n";
424
print " allocated by ".$getmem{$addr}."\n";
425
}
426
}
427
}
428
429
if($openfile) {
430
for(keys %filedes) {
431
if($filedes{$_} == 1) {
432
print "Open file descriptor created at ".$getfile{$_}."\n";
433
}
434
}
435
}
436
437
if($fopens) {
438
print "Open FILE handles left at:\n";
439
for(keys %fopen) {
440
if($fopen{$_} == 1) {
441
print "fopen() called at ".$fopenfile{$_}."\n";
442
}
443
}
444
}
445
446
if($addrinfos) {
447
print "IPv6-style name resolve data left at:\n";
448
for(keys %addrinfofile) {
449
if($addrinfo{$_} == 1) {
450
print "getaddrinfo() called at ".$addrinfofile{$_}."\n";
451
}
452
}
453
}
454
455
if($verbose) {
456
print "Mallocs: $mallocs\n",
457
"Reallocs: $reallocs\n",
458
"Callocs: $callocs\n",
459
"Strdups: $strdups\n",
460
"Wcsdups: $wcsdups\n",
461
"Frees: $frees\n",
462
"Sends: $sends\n",
463
"Recvs: $recvs\n",
464
"Sockets: $sockets\n",
465
"Allocations: ".($mallocs + $callocs + $reallocs + $strdups + $wcsdups)."\n",
466
"Operations: ".($mallocs + $callocs + $reallocs + $strdups + $wcsdups + $sends + $recvs + $sockets)."\n";
467
468
print "Maximum allocated: $maxmem\n";
469
print "Total allocated: $memsum\n";
470
}
471
472