This file is indexed.

/usr/bin/callgrind_control 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#! /usr/bin/perl -w
##--------------------------------------------------------------------##
##--- Control supervision of applications run with callgrind       ---##
##---                                            callgrind_control ---##
##--------------------------------------------------------------------##

#  This file is part of Callgrind, a cache-simulator and call graph
#  tracer built on Valgrind.
#
#  Copyright (C) 2003-2011 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
#
#  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.

sub getCallgrindPids {

  @pids = ();
  open LIST, "vgdb -l|";
  while(<LIST>) {
      if (/^use --pid=(\d+) for \S*?valgrind\s+(.*?)\s*$/) {
	  $pid = $1;
	  $cmd = $2;
	  if (!($cmd =~ /--tool=callgrind/)) { next; }
	  while($cmd =~ s/^-+\S+\s+//) {}
	  $cmdline{$pid} = $cmd;
	  $cmd =~ s/^(\S*).*/$1/;
	  $cmd{$pid} = $cmd;
	  #print "Found PID $pid, cmd '$cmd{$pid}', cmdline '$cmdline{$pid}'.\n";
	  push(@pids, $pid);
      }
  }
  close LIST;
}

sub printHeader {
  if ($headerPrinted) { return; }
  $headerPrinted = 1;

  print "Observe the status and control currently active callgrind runs.\n";
  print "(C) 2003-2011, Josef Weidendorfer (Josef.Weidendorfer\@gmx.de)\n\n";
}

sub printVersion {
  print "callgrind_control-3.7.0\n";
  exit;
}

sub shortHelp {
  print "See '$0 -h' for help.\n";
  exit;
}

sub printHelp {
  printHeader;

  print "Usage: callgrind_control [options] [pid|program-name...]\n\n";
  print "If no pids/names are given, an action is applied to all currently\n";
  print "active Callgrind runs. Default action is printing short information.\n\n";
  print "Options:\n";
  print "  -h --help         Show this help text\n";
  print "  --version         Show version\n";
  print "  -s --stat         Show statistics\n";
  print "  -b --back         Show stack/back trace\n";
  print "  -e [<A>,...]      Show event counters for <A>,... (default: all)\n";
  print "  --dump[=<s>]      Request a dump optionally using <s> as description\n";
  print "  -z --zero         Zero all event counters\n";
  print "  -k --kill         Kill\n";
  print "  -i --instr=on|off Switch instrumentation state on/off\n";
  print "\n";
  exit;
}


#
# Parts more or less copied from cg_annotate (author: Nicholas Nethercote)
#

sub prepareEvents {

  @events = split(/\s+/, $events);
  %events = ();
  $n = 0;
  foreach $event (@events) {
    $events{$event} = $n;
    $n++;
  }
  if (@show_events) {
    foreach my $show_event (@show_events) {
      (defined $events{$show_event}) or
	print "Warning: Event `$show_event' is not being collected\n";
    }
  } else {
    @show_events = @events;
  }
  @show_order = ();
  foreach my $show_event (@show_events) {
    push(@show_order, $events{$show_event});
  }
}

sub max ($$) 
{
    my ($x, $y) = @_;
    return ($x > $y ? $x : $y);
}

sub line_to_CC ($)
{
    my @CC = (split /\s+/, $_[0]);
    (@CC <= @events) or die("Line $.: too many event counts\n");
    return \@CC;
}

sub commify ($) {
    my ($val) = @_;
    1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
    return $val;
}

sub compute_CC_col_widths (@) 
{
    my @CCs = @_;
    my $CC_col_widths = [];

    # Initialise with minimum widths (from event names)
    foreach my $event (@events) {
        push(@$CC_col_widths, length($event));
    }
    
    # Find maximum width count for each column.  @CC_col_width positions
    # correspond to @CC positions.
    foreach my $CC (@CCs) {
        foreach my $i (0 .. scalar(@$CC)-1) {
            if (defined $CC->[$i]) {
                # Find length, accounting for commas that will be added
                my $length = length $CC->[$i];
                my $clength = $length + int(($length - 1) / 3);
                $CC_col_widths->[$i] = max($CC_col_widths->[$i], $clength); 
            }
        }
    }
    return $CC_col_widths;
}

# Print the CC with each column's size dictated by $CC_col_widths.
sub print_CC ($$) 
{
    my ($CC, $CC_col_widths) = @_;

    foreach my $i (@show_order) {
        my $count = (defined $CC->[$i] ? commify($CC->[$i]) : ".");
        my $space = ' ' x ($CC_col_widths->[$i] - length($count));
        print("$space$count ");
    }
}

sub print_events ($)
{
    my ($CC_col_widths) = @_;

    foreach my $i (@show_order) { 
        my $event       = $events[$i];
        my $event_width = length($event);
        my $col_width   = $CC_col_widths->[$i];
        my $space       = ' ' x ($col_width - $event_width);
        print("$space$event ");
    }
}



#
# Main
#

getCallgrindPids;

$requestEvents = 0;
$requestDump = 0;
$switchInstr = 0;
$headerPrinted = 0;
$dumpHint = "";
$verbose = 0;

%spids = ();
foreach $arg (@ARGV) {
  if ($arg =~ /^-/) {
    if ($requestDump == 1) { $requestDump = 2; }
    if ($requestEvents == 1) { $requestEvents = 2; }

    if ($arg =~ /^(-h|--help)$/) {
	printHelp;
    }
    elsif ($arg =~ /^--version$/) {
	printVersion;
    }
    elsif ($arg =~ /^-v$/) {
	$verbose++;
	next;
    }
    elsif ($arg =~ /^(-s|--stat)$/) {
	$printStatus = 1;
	next;
    }
    elsif ($arg =~ /^(-b|--back)$/) {
	$printBacktrace = 1;
	next;
    }
    elsif ($arg =~ /^-e$/) {
	$requestEvents = 1;
	next;
    }
    elsif ($arg =~ /^(-d|--dump)(|=.*)$/) {
	if ($2 ne "") {
	    $requestDump = 2;
	    $dumpHint = substr($2,1);
	}
	else {
	    # take next argument as dump hint
	    $requestDump = 1;
	}
	next;
    }
    elsif ($arg =~ /^(-z|--zero)$/) {
	$requestZero = 1;
	next;
    }
    elsif ($arg =~ /^(-k|--kill)$/) {
	$requestKill = 1;
	next;
    }
    elsif ($arg =~ /^(-i|--instr)(|=on|=off)$/) {
	$switchInstr = 2;
	if ($2 eq "=on") {
	    $switchInstrMode = "on";
	}
	elsif ($2 eq "=off") {
	    $switchInstrMode = "off";
	}
	else {
	    # check next argument for "on" or "off"
	    $switchInstr = 1;
	}
	next;
    }
    else {
	print "Error: unknown command line option '$arg'.\n";
	shortHelp;
    }
  }

  if ($arg =~ /^[A-Za-z_]/) {
    # arguments of -d/-e/-i are non-numeric
    if ($requestDump == 1) {
      $requestDump = 2;
      $dumpHint = $arg;
      next;
    }

    if ($requestEvents == 1) {
      $requestEvents = 2;
      @show_events = split(/,/, $arg);
      next;
    }

    if ($switchInstr == 1) {
      $switchInstr = 2;
      if ($arg eq "on") {
	  $switchInstrMode = "on";
      }
      elsif ($arg eq "off") {
	  $switchInstrMode = "off";
      }
      else {
	  print "Error: need to specify 'on' or 'off' after '-i'.\n";
	  shortHelp;
      }
      next;
    }
  }

  if (defined $cmd{$arg}) { $spids{$arg} = 1; next; }
  $nameFound = 0;
  foreach $p (@pids) {
    if ($cmd{$p} =~ /$arg$/) {
      $nameFound = 1;
      $spids{$p} = 1;
    }
  }
  if ($nameFound) { next; }

  print "Error: Callgrind task with PID/name '$arg' not detected.\n";
  shortHelp;
}


if ($switchInstr == 1) {
  print "Error: need to specify 'on' or 'off' after '-i'.\n";
  shortHelp;
}

if (scalar @pids == 0) {
  print "No active callgrind runs detected.\n";
  exit;
}

@spids = keys %spids;
if (scalar @spids >0) { @pids = @spids; }

$vgdbCommand = "";
$waitForAnswer = 0;
if ($requestDump) {
  $vgdbCommand = "dump";
  if ($dumpHint ne "") { $vgdbCommand .= " ".$dumpHint; }
}
if ($requestZero) { $vgdbCommand = "zero"; }
if ($requestKill) { $vgdbCommand = "v.kill"; }
if ($switchInstr) { $vgdbCommand = "instrumentation ".$switchInstrMode; }
if ($printStatus || $printBacktrace || $requestEvents) {
  $vgdbCommand = "status internal";
  $waitForAnswer = 1;
}

foreach $pid (@pids) {
  $pidstr = "PID $pid: ";
  if ($pid >0) { print $pidstr.$cmdline{$pid}; }

  if ($vgdbCommand eq "") {
      print "\n";
      next;
  }
  if ($verbose>0) {
      print " [requesting '$vgdbCommand']\n";
  } else {
      print "\n";
  }
  open RESULT, "vgdb --pid=$pid $vgdbCommand|";

  @tids = ();
  $ctid = 0;
  %fcount = ();
  %func = ();
  %calls = ();
  %events = ();
  @events = ();
  @threads = ();
  %totals = ();

  $exec_bbs = 0;
  $dist_bbs = 0;
  $exec_calls = 0;
  $dist_calls = 0;
  $dist_ctxs = 0;
  $dist_funcs = 0;
  $threads = "";
  $events = "";

  while(<RESULT>) {
    if (/function-(\d+)-(\d+): (.+)$/) {
      if ($ctid != $1) {
	$ctid = $1;
	push(@tids, $ctid);
	$fcount{$ctid} = 0;
      }
      $fcount{$ctid}++;
      $func{$ctid,$fcount{$ctid}} = $3;
    }
    elsif (/calls-(\d+)-(\d+): (.+)$/) {
      if ($ctid != $1) { next; }
      $calls{$ctid,$fcount{$ctid}} = $3;
    }
    elsif (/events-(\d+)-(\d+): (.+)$/) {
      if ($ctid != $1) { next; }
      $events{$ctid,$fcount{$ctid}} = line_to_CC($3);
    }
    elsif (/events-(\d+): (.+)$/) {
      if (scalar @events == 0) { next; }
      $totals{$1} = line_to_CC($2);
    }
    elsif (/executed-bbs: (\d+)/) { $exec_bbs = $1; }
    elsif (/distinct-bbs: (\d+)/) { $dist_bbs = $1; }
    elsif (/executed-calls: (\d+)/) { $exec_calls = $1; }
    elsif (/distinct-calls: (\d+)/) { $dist_calls = $1; }
    elsif (/distinct-functions: (\d+)/) { $dist_funcs = $1; }
    elsif (/distinct-contexts: (\d+)/) { $dist_ctxs = $1; }
    elsif (/events: (.+)$/) { $events = $1; prepareEvents; }
    elsif (/threads: (.+)$/) { $threads = $1; @threads = split " ", $threads; }
    elsif (/instrumentation: (\w+)$/) { $instrumentation = $1; }
  }

  #if ($? ne "0") { print " Got Error $?\n"; }
  if (!$waitForAnswer) { print "  OK.\n"; next; }

  if ($instrumentation eq "off") {
    print "  No information available as instrumentation is switched off.\n\n";
    exit;
  }

  if ($printStatus) {
    if ($requestEvents <1) {
      print "  Number of running threads: " .($#threads+1). ", thread IDs: $threads\n";
      print "  Events collected: $events\n";
    }

    print "  Functions: ".commify($dist_funcs);
    print " (executed ".commify($exec_calls);
    print ", contexts ".commify($dist_ctxs).")\n";

    print "  Basic blocks: ".commify($dist_bbs);
    print " (executed ".commify($exec_bbs);
    print ", call sites ".commify($dist_calls).")\n";
  }

  if ($requestEvents >0) {
    $totals_width = compute_CC_col_widths(values %totals);
    print "\n  Totals:";
    print_events($totals_width);
    print("\n");
    foreach $tid (@tids) {
      print "   Th".substr("  ".$tid,-2)."  ";
      print_CC($totals{$tid}, $totals_width);
      print("\n");
    }
  }

  if ($printBacktrace) {

    if ($requestEvents >0) {
      $totals_width = compute_CC_col_widths(values %events);
    }

    foreach $tid (@tids) {
      print "\n  Frame: ";
      if ($requestEvents >0) {
	print_events($totals_width);
      }
      print "Backtrace for Thread $tid\n";

      $i = $fcount{$tid};
      $c = 0;
      while($i>0 && $c<100) {
	$fc = substr(" $c",-2);
	print "   [$fc]  ";
	if ($requestEvents >0) {
	  print_CC($events{$tid,$i-1}, $totals_width);
	}
	print $func{$tid,$i};
	if ($i > 1) {
	  print " (".$calls{$tid,$i-1}." x)";
	}
	print "\n";
	$i--;
	$c++;
      }
      print "\n";
    }
  }
  print "\n";
}