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-2017 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, see <http://www.gnu.org/licenses/>.
30 # vgdb_exe will be set to a vgdb found 'near' the callgrind_control file
32 my $vgdbPrefixOption = "";
39 sub getCallgrindPids
{
42 open LIST
, $vgdb_exe . " $vgdbPrefixOption -l|";
44 if (/^use --pid=(\d+) for \S*?valgrind\s+(.*?)\s*$/) {
47 if (!($cmd =~ /--tool=callgrind/)) { next; }
48 while($cmd =~ s/^-+\S+\s+//) {}
49 $cmdline{$pid} = $cmd;
50 $cmd =~ s/^(\S*).*/$1/;
52 #print "Found PID $pid, cmd '$cmd{$pid}', cmdline '$cmdline{$pid}'.\n";
59 my $headerPrinted = 0;
62 if ($headerPrinted) { return; }
65 print "Observe the status and control currently active callgrind runs.\n";
66 print "(C) 2003-2011, Josef Weidendorfer (Josef.Weidendorfer\@gmx.de)\n\n";
70 print "callgrind_control-@VERSION@\n";
75 print "See '$0 -h' for help.\n";
82 print "Usage: callgrind_control [options] [pid|program-name...]\n\n";
83 print "If no pids/names are given, an action is applied to all currently\n";
84 print "active Callgrind runs. Default action is printing short information.\n\n";
86 print " -h --help Show this help text\n";
87 print " --version Show version\n";
88 print " -s --stat Show statistics\n";
89 print " -b --back Show stack/back trace\n";
90 print " -e [<A>,...] Show event counters for <A>,... (default: all)\n";
91 print " --dump[=<s>] Request a dump optionally using <s> as description\n";
92 print " -z --zero Zero all event counters\n";
93 print " -k --kill Kill\n";
94 print " -i --instr=on|off Switch instrumentation state on/off\n";
95 print "Uncommon options:\n";
96 print " --vgdb-prefix=<prefix> Only provide this if the same was given to Valgrind\n";
103 # Parts more or less copied from cg_annotate (author: Nicholas Nethercote)
110 my @show_events = ();
115 @events = split(/\s+/, $events);
117 foreach $event (@events) {
118 $events{$event} = $n;
122 foreach my $show_event (@show_events) {
123 (defined $events{$show_event}) or
124 print "Warning: Event `$show_event' is not being collected\n";
127 @show_events = @events;
130 foreach my $show_event (@show_events) {
131 push(@show_order, $events{$show_event});
138 return ($x > $y ?
$x : $y);
143 my @CC = (split /\s+/, $_[0]);
144 (@CC <= @events) or die("Line $.: too many event counts\n");
150 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
154 sub compute_CC_col_widths
(@
)
157 my $CC_col_widths = [];
159 # Initialise with minimum widths (from event names)
160 foreach my $event (@events) {
161 push(@
$CC_col_widths, length($event));
164 # Find maximum width count for each column. @CC_col_width positions
165 # correspond to @CC positions.
166 foreach my $CC (@CCs) {
167 foreach my $i (0 .. scalar(@
$CC)-1) {
168 if (defined $CC->[$i]) {
169 # Find length, accounting for commas that will be added
170 my $length = length $CC->[$i];
171 my $clength = $length + int(($length - 1) / 3);
172 $CC_col_widths->[$i] = max
($CC_col_widths->[$i], $clength);
176 return $CC_col_widths;
179 # Print the CC with each column's size dictated by $CC_col_widths.
182 my ($CC, $CC_col_widths) = @_;
184 foreach my $i (@show_order) {
185 my $count = (defined $CC->[$i] ? commify
($CC->[$i]) : ".");
186 my $space = ' ' x
($CC_col_widths->[$i] - length($count));
187 print("$space$count ");
193 my ($CC_col_widths) = @_;
195 foreach my $i (@show_order) {
196 my $event = $events[$i];
197 my $event_width = length($event);
198 my $col_width = $CC_col_widths->[$i];
199 my $space = ' ' x
($col_width - $event_width);
200 print("$space$event ");
210 # Search the appropriate vgdb executable
211 my $controldir = dirname
(__FILE__
);
212 if (-x
$controldir . "/vgdb") {
213 # classical case: callgrind_control and vgdb from the install bin dir
214 $vgdb_exe = $controldir . "/vgdb";
215 } elsif (-x
$controldir . "/../coregrind/vgdb") {
216 # callgrind_control called from the callgrind tool source/build dir
217 $vgdb_exe = $controldir . "/../coregrind/vgdb";
219 # no idea. Use whatever vgdb found in PATH
222 # print "will use vgdb at [" . $vgdb_exe . "]\n";
224 # To find the list of active pids, we need to have
225 # the --vgdb-prefix option if given.
227 foreach $arg (@ARGV) {
228 if ($arg =~ /^--vgdb-prefix=.*$/) {
229 $vgdbPrefixOption=$arg;
236 my $requestEvents = 0;
240 my $printBacktrace = 0;
242 my $switchInstrMode = "";
243 my $requestKill = "";
244 my $requestZero = "";
249 foreach $arg (@ARGV) {
251 if ($requestDump == 1) { $requestDump = 2; }
252 if ($requestEvents == 1) { $requestEvents = 2; }
254 if ($arg =~ /^(-h|--help)$/) {
257 elsif ($arg =~ /^--version$/) {
260 elsif ($arg =~ /^--vgdb-prefix=.*$/) {
261 # handled during the initial parsing.
264 elsif ($arg =~ /^-v$/) {
268 elsif ($arg =~ /^(-s|--stat)$/) {
272 elsif ($arg =~ /^(-b|--back)$/) {
276 elsif ($arg =~ /^-e$/) {
280 elsif ($arg =~ /^(-d|--dump)(|=.*)$/) {
283 $dumpHint = substr($2,1);
286 # take next argument as dump hint
291 elsif ($arg =~ /^(-z|--zero)$/) {
295 elsif ($arg =~ /^(-k|--kill)$/) {
299 elsif ($arg =~ /^(-i|--instr)(|=on|=off)$/) {
302 $switchInstrMode = "on";
304 elsif ($2 eq "=off") {
305 $switchInstrMode = "off";
308 # check next argument for "on" or "off"
314 print "Error: unknown command line option '$arg'.\n";
319 if ($arg =~ /^[A-Za-z_]/) {
320 # arguments of -d/-e/-i are non-numeric
321 if ($requestDump == 1) {
327 if ($requestEvents == 1) {
329 @show_events = split(/,/, $arg);
333 if ($switchInstr == 1) {
336 $switchInstrMode = "on";
338 elsif ($arg eq "off") {
339 $switchInstrMode = "off";
342 print "Error: need to specify 'on' or 'off' after '-i'.\n";
349 if (defined $cmd{$arg}) { $spids{$arg} = 1; next; }
351 foreach my $p (@pids) {
352 if ($cmd{$p} =~ /$arg$/) {
357 if ($nameFound) { next; }
359 print "Error: Callgrind task with PID/name '$arg' not detected.\n";
364 if ($switchInstr == 1) {
365 print "Error: need to specify 'on' or 'off' after '-i'.\n";
369 if (scalar @pids == 0) {
370 print "No active callgrind runs detected.\n";
374 my @spids = keys %spids;
375 if (scalar @spids >0) { @pids = @spids; }
377 my $vgdbCommand = "";
378 my $waitForAnswer = 0;
380 $vgdbCommand = "dump";
381 if ($dumpHint ne "") { $vgdbCommand .= " ".$dumpHint; }
383 if ($requestZero) { $vgdbCommand = "zero"; }
384 if ($requestKill) { $vgdbCommand = "v.kill"; }
385 if ($switchInstr) { $vgdbCommand = "instrumentation ".$switchInstrMode; }
386 if ($printStatus || $printBacktrace || $requestEvents) {
387 $vgdbCommand = "status internal";
391 foreach $pid (@pids) {
392 my $pidstr = "PID $pid: ";
393 if ($pid >0) { print $pidstr.$cmdline{$pid}; }
395 if ($vgdbCommand eq "") {
400 print " [requesting '$vgdbCommand']\n";
404 open RESULT
, $vgdb_exe . " $vgdbPrefixOption --pid=$pid $vgdbCommand|";
414 my $totals_width = [];
423 my $instrumentation = "";
426 if (/function-(\d+)-(\d+): (.+)$/) {
433 $func{$ctid,$fcount{$ctid}} = $3;
435 elsif (/calls-(\d+)-(\d+): (.+)$/) {
436 if ($ctid != $1) { next; }
437 $calls{$ctid,$fcount{$ctid}} = $3;
439 elsif (/events-(\d+)-(\d+): (.+)$/) {
440 if ($ctid != $1) { next; }
441 $events{$ctid,$fcount{$ctid}} = line_to_CC
($3);
443 elsif (/events-(\d+): (.+)$/) {
444 if (scalar @events == 0) { next; }
445 $totals{$1} = line_to_CC
($2);
447 elsif (/executed-bbs: (\d+)/) { $exec_bbs = $1; }
448 elsif (/distinct-bbs: (\d+)/) { $dist_bbs = $1; }
449 elsif (/executed-calls: (\d+)/) { $exec_calls = $1; }
450 elsif (/distinct-calls: (\d+)/) { $dist_calls = $1; }
451 elsif (/distinct-functions: (\d+)/) { $dist_funcs = $1; }
452 elsif (/distinct-contexts: (\d+)/) { $dist_ctxs = $1; }
453 elsif (/events: (.+)$/) { $events = $1; prepareEvents
; }
454 elsif (/threads: (.+)$/) { $threads = $1; @threads = split " ", $threads; }
455 elsif (/instrumentation: (\w+)$/) { $instrumentation = $1; }
458 #if ($? ne "0") { print " Got Error $?\n"; }
459 if (!$waitForAnswer) { print " OK.\n"; next; }
461 if ($instrumentation eq "off") {
462 print " No information available as instrumentation is switched off.\n\n";
467 if ($requestEvents <1) {
468 print " Number of running threads: " .($#threads+1). ", thread IDs: $threads\n";
469 print " Events collected: $events\n";
472 print " Functions: ".commify
($dist_funcs);
473 print " (executed ".commify
($exec_calls);
474 print ", contexts ".commify
($dist_ctxs).")\n";
476 print " Basic blocks: ".commify
($dist_bbs);
477 print " (executed ".commify
($exec_bbs);
478 print ", call sites ".commify
($dist_calls).")\n";
481 if ($requestEvents >0) {
482 $totals_width = compute_CC_col_widths
(values %totals);
484 print_events
($totals_width);
486 foreach $tid (@tids) {
487 print " Th".substr(" ".$tid,-2)." ";
488 print_CC
($totals{$tid}, $totals_width);
493 if ($printBacktrace) {
495 if ($requestEvents >0) {
496 $totals_width = compute_CC_col_widths
(values %events);
499 foreach $tid (@tids) {
501 if ($requestEvents >0) {
502 print_events
($totals_width);
504 print "Backtrace for Thread $tid\n";
506 my $i = $fcount{$tid};
508 while($i>0 && $c<100) {
509 my $fc = substr(" $c",-2);
511 if ($requestEvents >0) {
512 print_CC
($events{$tid,$i-1}, $totals_width);
514 print $func{$tid,$i};
516 print " (".$calls{$tid,$i-1}." x)";