Home | History | Annotate | Download | only in utils
      1 #!/usr/bin/perl

      2 
      3 use Getopt::Std;
      4 $DEBUG = 0;
      5 
      6 sub parse_objdump_file {
      7   my ($filename) = @_;
      8   my @result;
      9   open (INPUT, $filename) or die "$filename: $!\n";
     10   print "opened objdump output file $filename\n" if $DEBUG;
     11   while (<INPUT>) {
     12     if (/\s*([0-9a-f]*):\t(([0-9a-f]{2} )+) *\t(.*)$/) {
     13       my ($addr, $bytes, $instr) = ($1, $2, $4);
     14       $addr = "0x" . $addr;
     15       $bytes =~ s/\s*(.*\S)\s*/$1/; # trim any remaining whitespace

     16       $instr =~ s/\s*(.*\S)\s*/$1/;
     17       push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr});
     18       print "addr=$addr bytes='$bytes' instr='$instr'\n" if $DEBUG;
     19     }
     20   }
     21   close INPUT;
     22   return @result;
     23 }
     24 
     25 sub parse_gdb_file {
     26   my ($filename) = @_;
     27   my @result;
     28   my $got_addr;
     29   open (INPUT, $filename) or die "$filename: $!\n";
     30   print "opened gdb output file $filename\n" if $DEBUG;
     31   while (<INPUT>) {
     32     if (/^(0x[0-9a-f]*):\t([^\t]*)\t[^:]*:\t((0x[0-9a-f]{2}\s*)+)\s*$/) {
     33       my ($addr, $bytes, $instr) = ($1, $3, $2);
     34       $bytes =~ s/0x//g;
     35       $bytes =~ s/\s+/ /g;           # regularize whitespace

     36       $bytes =~ s/\s*(.*\S)\s*/$1/;  # trim any remaining whitespace

     37       $instr =~ s/\s*(.*\S)\s*/$1/;
     38       push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr});
     39       print "addr=$addr bytes='$bytes' instr='$instr'\n" if $DEBUG;
     40     } elsif (/^(0x[0-9a-f]*):\t$/) { # deal with gdb's line breaker

     41       $got_addr = $1;
     42     } elsif ($got_addr && /^    ([^\t]*)\t[^:]*:\t((0x[0-9a-f]{2}\s*)+)\s*$/) {
     43       my ($addr, $bytes, $instr) = ($got_addr, $2, $1);
     44       $bytes =~ s/0x//g;
     45       $bytes =~ s/\s+/ /g;           # regularize whitespace

     46       $bytes =~ s/\s*(.*\S)\s*/$1/;  # trim any remaining whitespace

     47       $instr =~ s/\s*(.*\S)\s*/$1/;
     48       push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr});
     49       print "addr=$addr bytes='$bytes' instr='$instr'\n" if $DEBUG;
     50       undef $got_addr;
     51     }
     52   }
     53   close INPUT;
     54   return @result;
     55 }
     56 
     57 sub binary_diffs {
     58   my ($objdump_file, $gdb_file) = @_;
     59   my @file1 = parse_objdump_file ($objdump_file);
     60   my @file2 = parse_gdb_file ($gdb_file);
     61   my $lastrecord = ($#file1 >= $#file2) ? ($#file1) : ($#file2);

     62   for (my $i = 0; $i <= $lastrecord; ++$i) {
     63     my $d1 = $file1[$i];
     64     my $d2 = $file2[$i];
     65     if ($d1->{'bytes'} ne $d2->{'bytes'}) {
     66       next if (($d1->{'instr'} eq $d2->{'instr'}) && $opt_d);
     67       printf "0x%08x:\t%30s \t%s\n", 0+$d1->{'addr'}, $d1->{'bytes'}, $d1->{'instr'};
     68       printf "0x%08x:\t%30s \t%s\n\n", 0+$d2->{'addr'}, $d2->{'bytes'}, $d2->{'instr'};
     69     }
     70   }
     71 }
     72 
     73 &getopts('d');
     74 $objdump_file = $ARGV[0];
     75 $gdb_file = $ARGV[1];
     76 binary_diffs ($objdump_file, $gdb_file);
     77 exit (0);
     78 __END__
     79 =pod
     80 
     81 =head1 NAME
     82 
     83 codegen-diff
     84 
     85 =head1 SYNOPSIS
     86 
     87 codegen-diff [-d] I<OBJDUMP-OUTPUT-FILE> I<GDB-DISASSEMBLY-FILE>
     88 
     89 =head1 DESCRIPTION
     90 
     91 B<codegen-diff> is a program that tries to show you the differences
     92 between the code that B<llc> generated and the code that B<lli> generated.
     93 
     94 The way you use it is as follows: first, you create I<OBJDUMP-OUTPUT-FILE>
     95 by running B<objdump> on the B<llc> compiled and linked binary. You need to
     96 trim down the result so it contains only the function of interest.
     97 
     98 Second, you create I<GDB-DISASSEMBLY-FILE> by running B<gdb>, with my patch
     99 to print out hex bytes in the B<disassemble> command output, on
    100 B<lli>.  Set a breakpoint in C<Emitter::finishFunction()> and wait until
    101 the function you want is compiled.  Then use the B<disassemble> command
    102 to print out the assembly dump of the function B<lli> just compiled.
    103 (Use C<lli -debug> to find out where the function starts and ends in memory.)
    104 It's easiest to save this output by using B<script>.
    105 
    106 Finally, you run B<codegen-diff>, as indicated in the Synopsis section of
    107 this manpage. It will print out a two-line stanza for each mismatched
    108 instruction, with the  B<llc> version first, and the  B<lli> version second.
    109 
    110 =head1 OPTIONS
    111 
    112 =over 4
    113 
    114 =item -d
    115 
    116 Don't show instructions where the bytes are different but they
    117 disassemble to the same thing. This puts a lot of trust in the
    118 disassembler, but it might help you highlight the more egregious cases
    119 of misassembly.
    120 
    121 =back
    122 
    123 =head1 AUTHOR
    124 
    125 B<codegen-diff> was written by Brian Gaeke.
    126 
    127 =head1 SEE ALSO
    128 
    129 L<gdb(1)>, L<objdump(1)>, L<script(1)>.
    130 
    131 You will need my B<gdb> patch:
    132 
    133   http://llvm.cs.uiuc.edu/~gaeke/gdb-disassembly-print-bytes.patch
    134 
    135 =cut
    136