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
|
#!/usr/bin/perl -w
# a perl script to help debug memory leaks.
# Erez Zadok <ezk@cs.sunysb.edu>, September 2003
#
# How to use:
# 0. change all yuor code so it uses the KMALLOC() and KFREE() macros!
#
# 1. compile your f/s with FIST_MALLOC_DEBUG.
#
# 2. run it without debugging, so you only see the memdebug printk's
#
# 3. when you're done with your test of the file system, unmount it and
# unload the module: this will flush out a few more things which will
# result in more kfree's to be called
#
# 4. Collect your log info from where the kernel puts in, usually /var/log/all
#
# 5. run this perl script on the log file: ./match-malloc.pl foo.log
# 6. Investigate each line of output from the script to see if it's really a
# memory leak, then fix it.
#
# 7. Repeat this procedure until there are no memory leaks.
#
my $ret = 0;
$debug = 0;
$counter = 0;
@bufs = ();
while (($line = <>)) {
chop $line;
printf(STDERR "LINE %s\n", $line) if $debug;
if ($line =~ /KM:(\d+):\-?\d+:([^:]+):/) {
if ($counter + 1 != $1) {
printf(STDERR "COUNTER ORDER: counter is %d: line is %s\n", $counter, $line);
$ret++;
}
$counter = $1;
$addr = $2;
printf(STDERR "KM ADDR %s\n", $addr) if $debug;
if (defined($bufs{$addr})) {
printf(STDOUT "double alloc: %sr\n", $line);
$ret++;
} else {
$bufs{$addr} = $line;
}
next;
}
if ($line =~ /KF:(\d+):\-?\d+:([^:]+):/) {
if ($counter + 1 != $1) {
printf(STDERR "COUNTER ORDER:%d:%s\n", $counter, $line);
$ret++;
}
$counter = $1;
$addr = $2;
printf(STDERR "KF ADDR %s\n", $addr) if $debug;
if ($addr eq "00000000") {
# We don't care about NULLs.
print "Freed NULL" if $debug;
} elsif (defined($bufs{$addr})) {
$bufs{$addr} = undef;
} else {
printf(STDOUT "unallocated free: %s\n", $line);
$ret++;
}
next;
}
printf(STDERR "SKIP %s\n", $line) if $debug > 1;
}
foreach $buf (keys %bufs) {
next unless defined($bufs{$buf});
printf(STDOUT "leaked: %s\n", $bufs{$buf});
$ret++;
}
exit($ret > 126 ? 126 : $ret);
|