This file is indexed.

/usr/bin/cg_diff is in valgrind 1:3.7.0-0ubuntu3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#! /usr/bin/perl

##--------------------------------------------------------------------##
##--- Cachegrind's differencer.                         cg_diff.in ---##
##--------------------------------------------------------------------##

#  This file is part of Cachegrind, a Valgrind tool for cache
#  profiling programs.
#
#  Copyright (C) 2002-2010 Nicholas Nethercote
#     njn@valgrind.org
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#  02111-1307, USA.
#
#  The GNU General Public License is contained in the file COPYING.

#----------------------------------------------------------------------------
# This is a very cut-down and modified version of cg_annotate.
#----------------------------------------------------------------------------

use warnings;
use strict;

#----------------------------------------------------------------------------
# Global variables
#----------------------------------------------------------------------------

# Version number
my $version = "3.7.0";

# Usage message.
my $usage = <<END
usage: cg_diff [options] <cachegrind-out-file1> <cachegrind-out-file2>

  options for the user, with defaults in [ ], are:
    -h --help             show this message
    -v --version          show version
    --mod-filename=<expr> a Perl search-and-replace expression that is applied
                          to filenames, eg. --mod-filename='s/prog[0-9]/projN/'
    --mod-funcname=<expr> like --mod-filename, but applied to function names

  cg_diff is Copyright (C) 2010-2010 Nicholas Nethercote.
  and licensed under the GNU General Public License, version 2.
  Bug reports, feedback, admiration, abuse, etc, to: njn\@valgrind.org.
                                                
END
;

# --mod-filename expression
my $mod_filename = undef;

# --mod-funcname expression
my $mod_funcname = undef;

#-----------------------------------------------------------------------------
# Argument and option handling
#-----------------------------------------------------------------------------
sub process_cmd_line() 
{
    my ($file1, $file2) = (undef, undef);

    for my $arg (@ARGV) { 

        if ($arg =~ /^-/) {
            # --version
            if ($arg =~ /^-v$|^--version$/) {
                die("cg_diff-$version\n");

            } elsif ($arg =~ /^--mod-filename=(.*)/) {
                $mod_filename = $1;

            } elsif ($arg =~ /^--mod-funcname=(.*)/) {
                $mod_funcname = $1;

            } else {            # -h and --help fall under this case
                die($usage);
            }

        } elsif (not defined($file1)) {
            $file1 = $arg;

        } elsif (not defined($file2)) {
            $file2 = $arg;

        } else {
            die($usage);
        }
    }

    # Must have specified two input files.
    if (not defined $file1 or not defined $file2) {
        die($usage);
    }

    return ($file1, $file2);
}

#-----------------------------------------------------------------------------
# Reading of input file
#-----------------------------------------------------------------------------
sub max ($$) 
{
    my ($x, $y) = @_;
    return ($x > $y ? $x : $y);
}

# Add the two arrays;  any '.' entries are ignored.  Two tricky things:
# 1. If $a2->[$i] is undefined, it defaults to 0 which is what we want; we turn
#    off warnings to allow this.  This makes things about 10% faster than
#    checking for definedness ourselves.
# 2. We don't add an undefined count or a ".", even though it's value is 0,
#    because we don't want to make an $a2->[$i] that is undef become 0
#    unnecessarily.
sub add_array_a_to_b ($$) 
{
    my ($a, $b) = @_;

    my $n = max(scalar @$a, scalar @$b);
    $^W = 0;
    foreach my $i (0 .. $n-1) {
        $b->[$i] += $a->[$i] if (defined $a->[$i] && "." ne $a->[$i]);
    }
    $^W = 1;
}

sub sub_array_b_from_a ($$) 
{
    my ($a, $b) = @_;

    my $n = max(scalar @$a, scalar @$b);
    $^W = 0;
    foreach my $i (0 .. $n-1) {
        $a->[$i] -= $b->[$i];       # XXX: doesn't handle '.' entries
    }
    $^W = 1;
}

# Add each event count to the CC array.  '.' counts become undef, as do
# missing entries (implicitly).
sub line_to_CC ($$)
{
    my ($line, $numEvents) = @_;

    my @CC = (split /\s+/, $line);
    (@CC <= $numEvents) or die("Line $.: too many event counts\n");
    return \@CC;
}

sub read_input_file($) 
{
    my ($input_file) = @_;

    open(INPUTFILE, "< $input_file") 
         || die "Cannot open $input_file for reading\n";

    # Read "desc:" lines.
    my $desc;
    my $line;
    while ($line = <INPUTFILE>) {
        if ($line =~ s/desc:\s+//) {
            $desc .= $line;
        } else {
            last;
        }
    }

    # Read "cmd:" line (Nb: will already be in $line from "desc:" loop above).
    ($line =~ s/^cmd:\s+//) or die("Line $.: missing command line\n");
    my $cmd = $line;
    chomp($cmd);    # Remove newline

    # Read "events:" line.  We make a temporary hash in which the Nth event's
    # value is N, which is useful for handling --show/--sort options below.
    $line = <INPUTFILE>;
    (defined $line && $line =~ s/^events:\s+//) 
        or die("Line $.: missing events line\n");
    my @events = split(/\s+/, $line);
    my $numEvents = scalar @events;

    my $currFileName;
    my $currFileFuncName;

    my %CCs;                    # hash("$filename#$funcname" => CC array)
    my $currCC = undef;         # CC array

    my $summaryCC;

    # Read body of input file.
    while (<INPUTFILE>) {
        s/#.*$//;   # remove comments
        if (s/^(\d+)\s+//) {
            my $CC = line_to_CC($_, $numEvents);
            defined($currCC) || die;
            add_array_a_to_b($CC, $currCC);

        } elsif (s/^fn=(.*)$//) {
            defined($currFileName) || die;
            my $tmpFuncName = $1;
            if (defined $mod_funcname) {
                eval "\$tmpFuncName =~ $mod_funcname";
            }
            $currFileFuncName = "$currFileName#$tmpFuncName";
            $currCC = $CCs{$currFileFuncName};
            if (not defined $currCC) {
                $currCC = [];
                $CCs{$currFileFuncName} = $currCC;
            }

        } elsif (s/^fl=(.*)$//) {
            $currFileName = $1;
            if (defined $mod_filename) {
                eval "\$currFileName =~ $mod_filename";
            }
            # Assume that a "fn=" line is followed by a "fl=" line.
            $currFileFuncName = undef;  

        } elsif (s/^\s*$//) {
            # blank, do nothing
        
        } elsif (s/^summary:\s+//) {
            $summaryCC = line_to_CC($_, $numEvents);
            (scalar(@$summaryCC) == @events) 
                or die("Line $.: summary event and total event mismatch\n");

        } else {
            warn("WARNING: line $. malformed, ignoring\n");
        }
    }

    # Check if summary line was present
    if (not defined $summaryCC) {
        die("missing final summary line, aborting\n");
    }

    close(INPUTFILE);

    return ($cmd, \@events, \%CCs, $summaryCC);
}

#----------------------------------------------------------------------------
# "main()"
#----------------------------------------------------------------------------
# Commands seen in the files.  Need not match.
my $cmd1;
my $cmd2;

# Events seen in the files.  They must match.
my $events1;
my $events2;

# Individual CCs, organised by filename/funcname/line_num.
# hashref("$filename#$funcname", CC array)
my $CCs1;
my $CCs2;

# Total counts for summary (an arrayref).
my $summaryCC1;
my $summaryCC2;

#----------------------------------------------------------------------------
# Read the input files
#----------------------------------------------------------------------------
my ($file1, $file2) = process_cmd_line();
($cmd1, $events1, $CCs1, $summaryCC1) = read_input_file($file1);
($cmd2, $events2, $CCs2, $summaryCC2) = read_input_file($file2);

#----------------------------------------------------------------------------
# Check the events match
#----------------------------------------------------------------------------
my $n = max(scalar @$events1, scalar @$events2);
$^W = 0;    # turn off warnings, because we might hit undefs
foreach my $i (0 .. $n-1) {
    ($events1->[$i] eq $events2->[$i]) || die "events don't match, aborting\n";
}
$^W = 1;

#----------------------------------------------------------------------------
# Do the subtraction: CCs2 -= CCs1
#----------------------------------------------------------------------------
while (my ($filefuncname, $CC1) = each(%$CCs1)) {
    my $CC2 = $CCs2->{$filefuncname};
    if (not defined $CC2) {
        $CC2 = [];
        sub_array_b_from_a($CC2, $CC1);     # CC2 -= CC1
        $CCs2->{$filefuncname} = $CC2;
    } else {
        sub_array_b_from_a($CC2, $CC1);     # CC2 -= CC1
    }
}
sub_array_b_from_a($summaryCC2, $summaryCC1);

#----------------------------------------------------------------------------
# Print the result, in CCs2
#----------------------------------------------------------------------------
print("desc: Files compared:   $file1; $file2\n");
print("cmd:  $cmd1; $cmd2\n");
print("events: ");
for my $e (@$events1) {
    print(" $e");
}
print("\n");

while (my ($filefuncname, $CC) = each(%$CCs2)) {

    my @x = split(/#/, $filefuncname);
    (scalar @x == 2) || die;

    print("fl=$x[0]\n");
    print("fn=$x[1]\n");

    print("0");
    foreach my $n (@$CC) {
        print(" $n");
    }
    print("\n");
}

print("summary:");
foreach my $n (@$summaryCC2) {
    print(" $n");
}
print("\n");

##--------------------------------------------------------------------##
##--- end                                                          ---##
##--------------------------------------------------------------------##