-
Notifications
You must be signed in to change notification settings - Fork 2
/
memdebug.pl
executable file
·352 lines (279 loc) · 8.29 KB
/
memdebug.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#! /usr/local/bin/perl -w
#
# memdebug.pl - parse the output of Toswin2 memory debugger.
# Copyright (C) 2001 Guido Flohr <[email protected]>,
# all rights reserved.
#
# This is a quick hack that analyzes the Toswin2 memory management.
# If the variable MEMDEBUG is set to yes in the Makefile, then all
# calls to malloc, free & friends a wrapped into functions that
# report the addresses of the allocated/freed blocks plus the
# location from where the function was called. Output is normally
# logged to "/tmp/debug.txt".
#
# In order to get good results you have to translate Toswin2
# with "-g" and without "-O".
#
# This Perl script scans the output and tries to find some
# anomalies. Call it as
#
# memdebug.pl toswin2.app /tmp/memdebug.txt
#
# The "real" bugs are usually shown in the first or in the last
# few lines. The main part will mostly be warnings about memory
# leaks (you can grep -v "never freed" memdebug.txt to avoid
# this).
#
# The script is not suitable for general usage. The format of the
# debugging output is hard-coded and it will probably fail if
# addr2line outputs any errors.
my $addr2line = "addr2line";
package Dispatcher;
require 5.004;
use strict;
use integer;
use constant HEX_ADDRESS => qr /^0x[0-9A-F]{8}$/;
use constant HEX_NULL => '0x00000000';
sub new {
bless {
_blocks => {},
_diagnostics => '',
_callers => {},
}, shift;
}
sub parse_malloc
{
my ($self, $line, $lineno) = @_;
my (undef, $caller, $bytes, $str_bytes, $str_at, $address, $garbage) =
split / /, $line;
chop $caller if defined $caller;
unless ($caller =~ HEX_ADDRESS &&
$bytes =~ HEX_ADDRESS &&
$str_bytes eq 'bytes' &&
$str_at eq 'at' &&
$address =~ HEX_ADDRESS &&
!defined $garbage) {
print STDERR <<EOF;
warning: $lineno: corrupted input ignored
EOF
# Don't return undef, we'll get a warning otherwise.
return 0;
}
$self->{_callers}->{$caller} = "address $caller";
if (exists $self->{_blocks}->{$address} && $address ne HEX_NULL) {
$self->{_diagnostics} .= <<EOF;
$caller: malloc returned address $address multiple times
EOF
} elsif ($address ne HEX_NULL) {
$self->{_blocks}->{$address}->{caller} = $caller;
$self->{_blocks}->{$address}->{bytes} = $bytes;
}
return $line;
}
sub parse_realloc
{
my ($self, $line, $lineno) = @_;
my (undef, $caller, $bytes, $str_from, $addr_old, $str_to,
$addr_new, $garbage) = split / /, $line;
chop $caller if defined $caller;
unless ($caller =~ HEX_ADDRESS &&
$bytes =~ HEX_ADDRESS &&
$str_from eq 'from' &&
$addr_old =~ HEX_ADDRESS &&
$str_to eq 'to' &&
$addr_new =~ HEX_ADDRESS &&
!defined $garbage) {
print STDERR <<EOF;
warning: $lineno: corrupted input ignored
EOF
# Don't return undef, we'll get a warning otherwise.
return 0;
}
$self->{_callers}->{$caller} = "address $caller";
if (exists $self->{_last_realloc}) {
if ($self->{_last_realloc}->{address} eq $addr_old) {
delete $self->{_last_realloc};
} # else ???
delete $self->{_last_realloc};
} # else probably reported elsewhere.
if ($addr_old ne HEX_NULL) {
if (exists ($self->{_blocks}->{$addr_old}) && $addr_new ne HEX_NULL) {
delete $self->{_blocks}->{$addr_old};
}
}
if ($addr_new ne HEX_NULL) {
$self->{_blocks}->{$addr_new}->{caller} = $caller;
$self->{_blocks}->{$addr_new}->{bytes} = $bytes;
}
return $line;
}
sub parse_pre_realloc
{
my ($self, $line, $lineno) = @_;
my (undef, $caller, $str_to, $bytes, $str_bytes, $str_at, $address,
$garbage) = split / /, $line;
chop $caller if defined $caller;
unless ($caller =~ HEX_ADDRESS &&
$str_to eq 'to' &&
$bytes =~ HEX_ADDRESS &&
$str_bytes eq 'bytes' &&
$str_at eq 'at' &&
$address =~ HEX_ADDRESS &&
!defined $garbage) {
print STDERR <<EOF;
warning: $lineno: corrupted input ignored
EOF
# Don't return undef, we'll get a warning otherwise.
return 0;
}
$self->{_callers}->{$caller} = "address $caller";
my $pre_bytes;
unless (exists $self->{_blocks}->{$address} && $address ne HEX_NULL) {
$self->{_diagnostics} .= <<EOF;
$caller: $address reallocated but never allocated
EOF
$pre_bytes = "(never allocated)";
} elsif ($address eq HEX_NULL) {
$pre_bytes = 0;
} else {
$pre_bytes = $self->{_blocks}->{$address}->{bytes};
}
$self->{_last_realloc} = {
address => $address,
bytes => $bytes,
pre_bytes => $pre_bytes,
caller => $caller,
};
return $line;
}
sub parse_free
{
my ($self, $line, $lineno) = @_;
my (undef, $caller, $str_at, $address, $garbage) =
split / /, $line;
chop $caller if defined $caller;
unless ($caller =~ HEX_ADDRESS &&
$str_at eq 'at' &&
$address =~ HEX_ADDRESS &&
!defined $garbage) {
print STDERR <<EOF;
warning: $lineno: corrupted input ignored
EOF
# Don't return undef, we'll get a warning otherwise.
return 0;
}
$self->{_callers}->{$caller} = "address $caller";
unless (exists $self->{_blocks}->{$address} && $address ne HEX_NULL) {
$self->{_diagnostics} .= <<EOF;
$caller: $address freed but never allocated
EOF
} elsif ($address eq HEX_NULL) {
$self->{_diagnostics} .= <<EOF;
$caller: NULL pointer freed
EOF
} else {
delete $self->{_blocks}->{$address};
}
return $line;
}
sub parse_calloc
{
my ($self, $line, $lineno) = @_;
my (undef, $caller, $nmemb, $str_x, $bytes, $str_bytes, $str_at, $address,
$garbage) = split / /, $line;
chop $caller if defined $caller;
unless ($caller =~ HEX_ADDRESS &&
$nmemb =~ HEX_ADDRESS &&
$str_x eq 'x' &&
$bytes =~ HEX_ADDRESS &&
$str_bytes eq 'bytes' &&
$str_at eq 'at' &&
$address =~ HEX_ADDRESS &&
!defined $garbage) {
print STDERR <<EOF;
warning: $lineno: corrupted input ignored
EOF
# Don't return undef, we'll get a warning otherwise.
return 0;
}
$self->{_callers}->{$caller} = "address $caller";
if (exists $self->{_blocks}->{$address} && $address ne HEX_NULL) {
$self->{_diagnostics} .= <<EOF;
$caller: calloc returned address $address multiple times
EOF
} elsif ($address ne HEX_NULL) {
$self->{_blocks}->{$address}->{caller} = $caller;
$self->{_blocks}->{$address}->{bytes} = "$nmemb x $bytes";
}
return $line;
}
sub diagnostics {
my $self = shift;
while (my ($addr, $block) = each %{$self->{_blocks}}) {
my ($bytes, $caller) = ($block->{bytes}, $block->{caller});
$self->{_diagnostics} .= <<EOF;
$caller: block at address $addr ($bytes bytes) never freed
EOF
}
if ($self->{_last_realloc}) {
my $last_realloc = $self->{_last_realloc};
my $pre_bytes = $self->{_blocks}->{$last_realloc->{bytes}} ||
"never allocated before";
$self->{_diagnostics} .= <<EOF;
========================================================================
$last_realloc->{caller}: probably crashed while trying to reallocate
buffer at address $last_realloc->{address} from $last_realloc->{pre_bytes}
bytes to $last_realloc->{bytes} bytes
========================================================================
EOF
}
return $self->{_diagnostics};
}
sub callers {
shift->{_callers};
}
1;
package main;
use strict;
my $re_funcs = q{(malloc|realloc|pre_realloc|free|calloc)};
my $dispatcher = Dispatcher->new;
my ($image, $debug_txt, $garbage) = ($ARGV[0], $ARGV[1], $ARGV[2]);
die "usage: $0 PROGRAM_IMAGE MEMDEBUG_TXT\n"
unless defined $debug_txt && !defined $garbage;
open IN, "<$debug_txt"
or die "$0: cannot open $debug_txt: $!\n";
while (<IN>) {
chomp;
print STDERR "warning: $.: ignoring garbage '$_'\n"
unless s/^$re_funcs: /
my $method = "parse_$1";
$dispatcher->$method ($_, $.)/xe;
}
close IN;
my $callers = $dispatcher->callers;
my @callers = keys %$callers;
my $warnings = $dispatcher->diagnostics;
my $re_hex = qr /0x[0-9A-F]{8}/;
my $caller_string = (join "\n", @callers) . "\n";
open ADDR2LINE, "echo \"$caller_string\" | $addr2line -e $image |"
or die "$0: cannot exec $addr2line -e $image: $!\n";
while (<ADDR2LINE>) {
chomp;
last unless @callers;
$callers->{shift @callers} = $_;
}
close ADDR2LINE;
$caller_string = join '|', keys %$callers;
$warnings =~ s/^($caller_string):/$callers->{$1} . ':'/gmex;
print $warnings;
__END__
Local Variables:
mode: perl
perl-indent-level: 4
perl-continued-statement-offset: 4
perl-continued-brace-offset: 0
perl-brace-offset: -4
perl-brace-imaginary-offset: 0
perl-label-offset: -4
tab-width: 4
End: