2
##--------------------------------------------------------------------##
3
##--- Control supervision of applications run with callgrind ---##
4
##--- callgrind_control ---##
5
##--------------------------------------------------------------------##
7
# This file is part of Callgrind, a cache-simulator and call graph
8
# tracer built on Valgrind.
10
# Copyright (C) 2003-2011 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
12
# This program is free software; you can redistribute it and/or
13
# modify it under the terms of the GNU General Public License as
14
# published by the Free Software Foundation; either version 2 of the
15
# License, or (at your option) any later version.
17
# This program is distributed in the hope that it will be useful, but
18
# WITHOUT ANY WARRANTY; without even the implied warranty of
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
# General Public License for more details.
22
# You should have received a copy of the GNU General Public License
23
# along with this program; if not, write to the Free Software
24
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27
sub getCallgrindPids {
30
open LIST, "vgdb -l|";
32
if (/^use --pid=(\d+) for \S*?valgrind\s+(.*?)\s*$/) {
35
if (!($cmd =~ /--tool=callgrind/)) { next; }
36
while($cmd =~ s/^-+\S+\s+//) {}
37
$cmdline{$pid} = $cmd;
38
$cmd =~ s/^(\S*).*/$1/;
40
#print "Found PID $pid, cmd '$cmd{$pid}', cmdline '$cmdline{$pid}'.\n";
48
if ($headerPrinted) { return; }
51
print "Observe the status and control currently active callgrind runs.\n";
52
print "(C) 2003-2011, Josef Weidendorfer (Josef.Weidendorfer\@gmx.de)\n\n";
56
print "callgrind_control-@VERSION@\n";
61
print "See '$0 -h' for help.\n";
68
print "Usage: callgrind_control [options] [pid|program-name...]\n\n";
69
print "If no pids/names are given, an action is applied to all currently\n";
70
print "active Callgrind runs. Default action is printing short information.\n\n";
72
print " -h --help Show this help text\n";
73
print " --version Show version\n";
74
print " -s --stat Show statistics\n";
75
print " -b --back Show stack/back trace\n";
76
print " -e [<A>,...] Show event counters for <A>,... (default: all)\n";
77
print " --dump[=<s>] Request a dump optionally using <s> as description\n";
78
print " -z --zero Zero all event counters\n";
79
print " -k --kill Kill\n";
80
print " -i --instr=on|off Switch instrumentation state on/off\n";
87
# Parts more or less copied from cg_annotate (author: Nicholas Nethercote)
92
@events = split(/\s+/, $events);
95
foreach $event (@events) {
100
foreach my $show_event (@show_events) {
101
(defined $events{$show_event}) or
102
print "Warning: Event `$show_event' is not being collected\n";
105
@show_events = @events;
108
foreach my $show_event (@show_events) {
109
push(@show_order, $events{$show_event});
116
return ($x > $y ? $x : $y);
121
my @CC = (split /\s+/, $_[0]);
122
(@CC <= @events) or die("Line $.: too many event counts\n");
128
1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
132
sub compute_CC_col_widths (@)
135
my $CC_col_widths = [];
137
# Initialise with minimum widths (from event names)
138
foreach my $event (@events) {
139
push(@$CC_col_widths, length($event));
142
# Find maximum width count for each column. @CC_col_width positions
143
# correspond to @CC positions.
144
foreach my $CC (@CCs) {
145
foreach my $i (0 .. scalar(@$CC)-1) {
146
if (defined $CC->[$i]) {
147
# Find length, accounting for commas that will be added
148
my $length = length $CC->[$i];
149
my $clength = $length + int(($length - 1) / 3);
150
$CC_col_widths->[$i] = max($CC_col_widths->[$i], $clength);
154
return $CC_col_widths;
157
# Print the CC with each column's size dictated by $CC_col_widths.
160
my ($CC, $CC_col_widths) = @_;
162
foreach my $i (@show_order) {
163
my $count = (defined $CC->[$i] ? commify($CC->[$i]) : ".");
164
my $space = ' ' x ($CC_col_widths->[$i] - length($count));
165
print("$space$count ");
171
my ($CC_col_widths) = @_;
173
foreach my $i (@show_order) {
174
my $event = $events[$i];
175
my $event_width = length($event);
176
my $col_width = $CC_col_widths->[$i];
177
my $space = ' ' x ($col_width - $event_width);
178
print("$space$event ");
198
foreach $arg (@ARGV) {
200
if ($requestDump == 1) { $requestDump = 2; }
201
if ($requestEvents == 1) { $requestEvents = 2; }
203
if ($arg =~ /^(-h|--help)$/) {
206
elsif ($arg =~ /^--version$/) {
209
elsif ($arg =~ /^-v$/) {
213
elsif ($arg =~ /^(-s|--stat)$/) {
217
elsif ($arg =~ /^(-b|--back)$/) {
221
elsif ($arg =~ /^-e$/) {
225
elsif ($arg =~ /^(-d|--dump)(|=.*)$/) {
228
$dumpHint = substr($2,1);
231
# take next argument as dump hint
236
elsif ($arg =~ /^(-z|--zero)$/) {
240
elsif ($arg =~ /^(-k|--kill)$/) {
244
elsif ($arg =~ /^(-i|--instr)(|=on|=off)$/) {
247
$switchInstrMode = "on";
249
elsif ($2 eq "=off") {
250
$switchInstrMode = "off";
253
# check next argument for "on" or "off"
259
print "Error: unknown command line option '$arg'.\n";
264
if ($arg =~ /^[A-Za-z_]/) {
265
# arguments of -d/-e/-i are non-numeric
266
if ($requestDump == 1) {
272
if ($requestEvents == 1) {
274
@show_events = split(/,/, $arg);
278
if ($switchInstr == 1) {
281
$switchInstrMode = "on";
283
elsif ($arg eq "off") {
284
$switchInstrMode = "off";
287
print "Error: need to specify 'on' or 'off' after '-i'.\n";
294
if (defined $cmd{$arg}) { $spids{$arg} = 1; next; }
297
if ($cmd{$p} =~ /$arg$/) {
302
if ($nameFound) { next; }
304
print "Error: Callgrind task with PID/name '$arg' not detected.\n";
309
if ($switchInstr == 1) {
310
print "Error: need to specify 'on' or 'off' after '-i'.\n";
314
if (scalar @pids == 0) {
315
print "No active callgrind runs detected.\n";
319
@spids = keys %spids;
320
if (scalar @spids >0) { @pids = @spids; }
325
$vgdbCommand = "dump";
326
if ($dumpHint ne "") { $vgdbCommand .= " ".$dumpHint; }
328
if ($requestZero) { $vgdbCommand = "zero"; }
329
if ($requestKill) { $vgdbCommand = "v.kill"; }
330
if ($switchInstr) { $vgdbCommand = "instrumentation ".$switchInstrMode; }
331
if ($printStatus || $printBacktrace || $requestEvents) {
332
$vgdbCommand = "status internal";
336
foreach $pid (@pids) {
337
$pidstr = "PID $pid: ";
338
if ($pid >0) { print $pidstr.$cmdline{$pid}; }
340
if ($vgdbCommand eq "") {
345
print " [requesting '$vgdbCommand']\n";
349
open RESULT, "vgdb --pid=$pid $vgdbCommand|";
371
if (/function-(\d+)-(\d+): (.+)$/) {
378
$func{$ctid,$fcount{$ctid}} = $3;
380
elsif (/calls-(\d+)-(\d+): (.+)$/) {
381
if ($ctid != $1) { next; }
382
$calls{$ctid,$fcount{$ctid}} = $3;
384
elsif (/events-(\d+)-(\d+): (.+)$/) {
385
if ($ctid != $1) { next; }
386
$events{$ctid,$fcount{$ctid}} = line_to_CC($3);
388
elsif (/events-(\d+): (.+)$/) {
389
if (scalar @events == 0) { next; }
390
$totals{$1} = line_to_CC($2);
392
elsif (/executed-bbs: (\d+)/) { $exec_bbs = $1; }
393
elsif (/distinct-bbs: (\d+)/) { $dist_bbs = $1; }
394
elsif (/executed-calls: (\d+)/) { $exec_calls = $1; }
395
elsif (/distinct-calls: (\d+)/) { $dist_calls = $1; }
396
elsif (/distinct-functions: (\d+)/) { $dist_funcs = $1; }
397
elsif (/distinct-contexts: (\d+)/) { $dist_ctxs = $1; }
398
elsif (/events: (.+)$/) { $events = $1; prepareEvents; }
399
elsif (/threads: (.+)$/) { $threads = $1; @threads = split " ", $threads; }
400
elsif (/instrumentation: (\w+)$/) { $instrumentation = $1; }
403
#if ($? ne "0") { print " Got Error $?\n"; }
404
if (!$waitForAnswer) { print " OK.\n"; next; }
406
if ($instrumentation eq "off") {
407
print " No information available as instrumentation is switched off.\n\n";
412
if ($requestEvents <1) {
413
print " Number of running threads: " .($#threads+1). ", thread IDs: $threads\n";
414
print " Events collected: $events\n";
417
print " Functions: ".commify($dist_funcs);
418
print " (executed ".commify($exec_calls);
419
print ", contexts ".commify($dist_ctxs).")\n";
421
print " Basic blocks: ".commify($dist_bbs);
422
print " (executed ".commify($exec_bbs);
423
print ", call sites ".commify($dist_calls).")\n";
426
if ($requestEvents >0) {
427
$totals_width = compute_CC_col_widths(values %totals);
429
print_events($totals_width);
431
foreach $tid (@tids) {
432
print " Th".substr(" ".$tid,-2)." ";
433
print_CC($totals{$tid}, $totals_width);
438
if ($printBacktrace) {
440
if ($requestEvents >0) {
441
$totals_width = compute_CC_col_widths(values %events);
444
foreach $tid (@tids) {
446
if ($requestEvents >0) {
447
print_events($totals_width);
449
print "Backtrace for Thread $tid\n";
453
while($i>0 && $c<100) {
454
$fc = substr(" $c",-2);
456
if ($requestEvents >0) {
457
print_CC($events{$tid,$i-1}, $totals_width);
459
print $func{$tid,$i};
461
print " (".$calls{$tid,$i-1}." x)";