summaryrefslogtreecommitdiffstats
path: root/bcheck.pl
blob: cca973efd54faa483397adf0fb08b95010dcb15d (plain)
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
#!/usr/bin/perl -w

use DB_File;
use Fcntl ':flock';

if (!defined($ARGV[0])) {
    print "usage: requires .class dump as parameter!\n";
    exit;
}

sub bailout
{
    untie %bcheckdb if(defined(%bcheckdb));

    if(defined(MYLOCK)) {
        flock MYLOCK, LOCK_UN;
        close(MYLOCK);
    }

    print @_;
    exit 5;
}

sub ask_user
{
    my ($dbkey, $dbchunk) = @_;

    if (defined($ENV{"BCHECK_UPDATE"})) {
        $bcheckdb{$dbkey} = $dbchunk;
        return;
    }

    &bailout("BC problem detected") if (! -t STDIN);

    print "(I)gnore / (Q)uit / (U)pdate: ";

    my $key;
    while(defined(read STDIN, $key, 1)) {
        $key = lc($key);

        print "got: >$key<\n";

        return if ($key eq 'i');

        &bailout("BC problem. aborted") if ($key eq 'q');

        if ($key eq 'u') {
            $bcheckdb{$dbkey} = $dbchunk;
            return;
        }
        print "\n(I)gnore / (Q)uit / (U)pdate: ";
    }
}

sub diff_chunk($$)
{
    my ($oldl, $newl) = @_;
    my @old = split /^/m, $oldl;
    my @new = split /^/m, $newl;
    my $haschanges = 0;
    my $max = $#old > $#new ? $#old : $#new;

    die "whoops. key different" if ($old[0] ne $new[0]);

    if ($#old != $#new) {
        warn ("Structural difference.\n");
        print @old;
        print "-----------------------------------------------\n";
        print @new;
        $haschanges = 1;
        return $haschanges;
    }

    print $old[0];

    my ($class) = ($old[0] =~ /^(?:Class |Vtable for )(\S+)/);

    my $c = 1;
    while ($c < $max) {
        my ($o, $n) = ($old[$c], $new[$c]);
        chomp $o;
        chomp $n;
        $c++;
        next if ($o eq $n);

        if(defined($class) and $n =~ /^(\d+\s+)\w+(::\S+\s*.*)$/) {
            next if ($n eq "$1$class$2");
        }

        $haschanges = 1;

        print "-$o\n+$n\n\n";
    }

    return $haschanges;
}

local $dblock = $ENV{"HOME"} . "/bcheck.lock";
my $dbfile = $ENV{"HOME"} . "/bcheck.db";
my $cdump  = $ARGV[0];

die "file $cdump is not readable: $!" if (! -f $cdump);

# make sure the advisory lock exists
open(MYLOCK, ">$dblock");
print MYLOCK "";

flock MYLOCK, LOCK_EX;

tie %bcheckdb, 'DB_File', $dbfile;

my $chunk = "";

open (IN, "<$cdump") or die "cannot open $cdump: $!";
while (<IN>) {

    chop;

    s/0x[0-9a-fA-F]+/0x......../g;
    s/base size=/size=/g;
    s/\(\)\s*$//g;
    s/base align=/align=/g;

    $chunk .= $_ . "\n";

    if(/^\s*$/) {
        my @lines = split /^/m, $chunk;
        my $key = $lines[0];
        chomp $key;

        if($key !~ /<anonymous struct>/ &&
           $key !~ /<anonymous union>/) {
            if(defined($bcheckdb{$key})) {
                my $dbversion = $bcheckdb{$key};

                if($dbversion ne $chunk) {
                     &ask_user($key, $chunk) if(&diff_chunk($dbversion, $chunk));
                }
            }
            else {
                $bcheckdb{$key} = $chunk;
                print "NEW: $key\n";
            }
        }

        $chunk = "";
        next;
    }

}
close(IN);

untie %bcheckdb;
flock MYLOCK, LOCK_UN;
close(MYLOCK);

exit 0;