| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# |
| 4 |
# Modification History |
| 5 |
# |
| 6 |
# 2004-January-17 Jason Rohrer |
| 7 |
# Fixed regexps to match both A-F and a-f for hex address strings. |
| 8 |
# |
| 9 |
|
| 10 |
|
| 11 |
# Erwin S. Andreasen <[email protected]> |
| 12 |
# Henner Zeller <[email protected]> |
| 13 |
# |
| 14 |
# Homepage: http://www.andreasen.org/LeakTracer/ |
| 15 |
# This program is Public Domain |
| 16 |
use IO::Handle; |
| 17 |
|
| 18 |
die "You must supply at least one argument.\n" unless $#ARGV >= 0; |
| 19 |
|
| 20 |
$ExeFile = shift @ARGV; |
| 21 |
$LeaksFile = $#ARGV >= 0 ? shift @ARGV : "leak.out"; |
| 22 |
open (LEAKS, $LeaksFile) or die "Could not open leaks data file $LeaksFile: $!"; |
| 23 |
|
| 24 |
if ($#ARGV >= 0) { |
| 25 |
$BreakOn = shift @ARGV; |
| 26 |
# Rest in @ARGV are program arguments |
| 27 |
} |
| 28 |
|
| 29 |
$n = $u = 0; |
| 30 |
while (<LEAKS>) { |
| 31 |
chop; |
| 32 |
next if (m/^\s*#/); |
| 33 |
# 1 2 3 4 5 6 7 |
| 34 |
#if (/^\s*L\s+(0x)?([0-9a-fA-F]+)\s+(0x)?([0-9a-fA-F]+)\s+(0x)?([0-9a-fA-F]+)\s+(\d+)/) { |
| 35 |
# Allocations, which have not been freed or deallocations which have not |
| 36 |
# been allocated. |
| 37 |
# 1 2 3 |
| 38 |
if (/^\s*L\s+(0x)?([0-9a-fA-F]+)\s+(\d+)/) { |
| 39 |
$addr="$2"; # ",$4,$6"; |
| 40 |
$u++ if not exists $Type{$addr}; |
| 41 |
$Count{$addr}++; |
| 42 |
$Size{$addr} += $3; # $7; |
| 43 |
$Type{$addr} = "Leak"; |
| 44 |
$n++; |
| 45 |
} |
| 46 |
elsif (/^\s*D\s+(0x)?([0-9a-fA-F]+)/) { |
| 47 |
$addr="$2"; # ",$4,$6"; |
| 48 |
$u++ if not exists $Type{$addr}; |
| 49 |
$Count{$addr}++; |
| 50 |
$Type{$addr} = "delete on not allocated memory"; |
| 51 |
$n++; |
| 52 |
} |
| 53 |
# allocations/deallocations with other errornous conditions |
| 54 |
# 1 2 3 4 5 |
| 55 |
elsif (/^\s*([SO])\s+(0x)?([0-9a-fA-F]+)\s+(0x)?([0-9a-fA-F]+)/) { |
| 56 |
$addrs = "$3,$5,$1"; |
| 57 |
$AllocDealloc{$addrs} = ("$1" =~ m/S/) |
| 58 |
? "Different allocation schemes" |
| 59 |
: "This Memory was overwritten"; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
print STDERR "Gathered $n ($u unique) points of data.\n"; |
| 64 |
|
| 65 |
close (LEAKS); |
| 66 |
|
| 67 |
|
| 68 |
# Instead of using -batch, we just run things as usual. with -batch, |
| 69 |
# we quit on the first error, which we don't want. |
| 70 |
open (PIPE, "|gdb -q $ExeFile") or die "Cannot start gdb"; |
| 71 |
#open (PIPE, "|cat"); |
| 72 |
|
| 73 |
# Change set listsize 2 to something else to show more lines |
| 74 |
print PIPE "set prompt\nset complaints 1000\nset height 0\n"; |
| 75 |
|
| 76 |
# Optionally, run the program |
| 77 |
if (defined($BreakOn)) { |
| 78 |
print PIPE "break $BreakOn\n"; |
| 79 |
print PIPE "run ", join(" ", @ARGV), " \n"; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
print PIPE "set listsize 2\n"; |
| 84 |
foreach (sort keys %AllocDealloc) { |
| 85 |
print PIPE "echo \\n#-- Alloc: $AllocDealloc{$_}\\nalloc here :\n"; |
| 86 |
@addrs = split(/,/,$_); |
| 87 |
print PIPE "l *0x" . (shift @addrs) . "\necho ..free here :\n"; |
| 88 |
print PIPE "set listsize 1\n"; |
| 89 |
print PIPE "l *0x" . (shift @addrs) . "\n"; |
| 90 |
} |
| 91 |
|
| 92 |
foreach (sort keys %Type) { |
| 93 |
print PIPE "echo \\n#-- $Type{$_}: counted $Count{$_}x"; |
| 94 |
if ($Size{$_} > 0) { |
| 95 |
print PIPE " / total Size: $Size{$_}"; |
| 96 |
} |
| 97 |
print PIPE "\\n\n"; |
| 98 |
@addrs = split(/,/,$_); |
| 99 |
print PIPE "set listsize 2\n"; |
| 100 |
print PIPE "l *0x" . (shift @addrs) . "\n"; |
| 101 |
#print PIPE "echo ..called from :\n"; |
| 102 |
#print PIPE "set listsize 1\n"; |
| 103 |
# gdb bails out, if it cannot find an address. |
| 104 |
#print PIPE "l *0x" . (shift @addrs) . "\necho ..called from :\n"; |
| 105 |
#print PIPE "l *0x" . (shift @addrs) . "\n"; |
| 106 |
} |
| 107 |
|
| 108 |
if (defined($BreakOn)) { |
| 109 |
print PIPE "kill\n"; |
| 110 |
} |
| 111 |
|
| 112 |
print PIPE "quit\n"; |
| 113 |
PIPE->flush(); |
| 114 |
wait(); |
| 115 |
|
| 116 |
close (PIPE); |