#!/usr/bin/perl
use Getopt::Std;
sub parse_objdump_file {
my ($filename) = @_;
my @result;
open (INPUT, $filename) or die "$filename: $!\n";
while () {
if (/\s*([0-9a-f]*):\t(([0-9a-f]{2} )+) *\t(.*)$/) {
my ($addr, $bytes, $instr) = ($1, $2, $4);
$addr = "0x" . $addr;
$bytes =~ s/\s*(.*\S)\s*/$1/; # trim any remaining whitespace
$instr =~ s/\s*(.*\S)\s*/$1/;
push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr});
}
}
close INPUT;
return @result;
}
sub parse_gdb_file {
my ($filename) = @_;
my @result;
my $got_addr;
open (INPUT, $filename) or die "$filename: $!\n";
while () {
if (/^(0x[0-9a-f]*):\t([^\t]*)\t[^:]*:\t((0x[0-9a-f]{2}\s*)+)\s*$/) {
my ($addr, $bytes, $instr) = ($1, $3, $2);
$bytes =~ s/0x//g;
$bytes =~ s/\s+/ /g; # regularize whitespace
$bytes =~ s/\s*(.*\S)\s*/$1/; # trim any remaining whitespace
$instr =~ s/\s*(.*\S)\s*/$1/;
push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr});
} elsif (/^(0x[0-9a-f]*):\t$/) { # deal with gdb's line breaker
$got_addr = $1;
} elsif ($got_addr && /^ ([^\t]*)\t[^:]*:\t((0x[0-9a-f]{2}\s*)+)\s*$/) {
my ($addr, $bytes, $instr) = ($got_addr, $2, $1);
$bytes =~ s/0x//g;
$bytes =~ s/\s+/ /g; # regularize whitespace
$bytes =~ s/\s*(.*\S)\s*/$1/; # trim any remaining whitespace
$instr =~ s/\s*(.*\S)\s*/$1/;
push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr});
undef $got_addr;
}
}
close INPUT;
return @result;
}
sub binary_diffs {
my ($objdump_file, $gdb_file) = @_;
my @file1 = parse_objdump_file ($objdump_file);
my @file2 = parse_gdb_file ($gdb_file);
my $lastrecord = ($#file1 >= $#file2) ? ($#file1) : ($#file2);
for (my $i = 0; $i <= $lastrecord; ++$i) {
my $d1 = $file1[$i];
my $d2 = $file2[$i];
if ($d1->{'bytes'} ne $d2->{'bytes'}) {
next if (($d1->{'instr'} eq $d2->{'instr'}) && $opt_d);
printf "0x%08x:\t%30s \t%s\n", 0+$d1->{'addr'}, $d1->{'bytes'}, $d1->{'instr'};
printf "0x%08x:\t%30s \t%s\n\n", 0+$d2->{'addr'}, $d2->{'bytes'}, $d2->{'instr'};
}
}
}
&getopts('d');
$objdump_file = $ARGV[0];
$gdb_file = $ARGV[1];
binary_diffs ($objdump_file, $gdb_file);
exit (0);
__END__
=pod
=head1 NAME
codegen-diff
=head1 SYNOPSIS
codegen-diff [-d] I I
=head1 DESCRIPTION
B is a program that tries to show you the differences
between the code that B generated and the code that B generated.
The way you use it is as follows: first, you create I
by running B on the B compiled and linked binary. You need to
trim down the result so it contains only the function of interest.
Second, you create I by running B, with my patch
to print out hex bytes in the B command output, on
B. Set a breakpoint in C and wait until
the function you want is compiled. Then use the B command
to print out the assembly dump of the function B just compiled.
(Use C to find out where the function starts and ends in memory.)
It's easiest to save this output by using B