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, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 sub getCallgrindPids
{
30 open LIST
, "vgdb $vgdbPrefixOption -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";
81 print "Uncommon options:\n";
82 print " --vgdb-prefix=<prefix> Only provide this if the same was given to Valgrind\n";
89 # Parts more or less copied from cg_annotate (author: Nicholas Nethercote)
94 @events = split(/\s+/, $events);
97 foreach $event (@events) {
102 foreach my $show_event (@show_events) {
103 (defined $events{$show_event}) or
104 print "Warning: Event `$show_event' is not being collected\n";
107 @show_events = @events;
110 foreach my $show_event (@show_events) {
111 push(@show_order, $events{$show_event});
118 return ($x > $y ?
$x : $y);
123 my @CC = (split /\s+/, $_[0]);
124 (@CC <= @events) or die("Line $.: too many event counts\n");
130 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
134 sub compute_CC_col_widths
(@
)
137 my $CC_col_widths = [];
139 # Initialise with minimum widths (from event names)
140 foreach my $event (@events) {
141 push(@
$CC_col_widths, length($event));
144 # Find maximum width count for each column. @CC_col_width positions
145 # correspond to @CC positions.
146 foreach my $CC (@CCs) {
147 foreach my $i (0 .. scalar(@
$CC)-1) {
148 if (defined $CC->[$i]) {
149 # Find length, accounting for commas that will be added
150 my $length = length $CC->[$i];
151 my $clength = $length + int(($length - 1) / 3);
152 $CC_col_widths->[$i] = max
($CC_col_widths->[$i], $clength);
156 return $CC_col_widths;
159 # Print the CC with each column's size dictated by $CC_col_widths.
162 my ($CC, $CC_col_widths) = @_;
164 foreach my $i (@show_order) {
165 my $count = (defined $CC->[$i] ? commify
($CC->[$i]) : ".");
166 my $space = ' ' x
($CC_col_widths->[$i] - length($count));
167 print("$space$count ");
173 my ($CC_col_widths) = @_;
175 foreach my $i (@show_order) {
176 my $event = $events[$i];
177 my $event_width = length($event);
178 my $col_width = $CC_col_widths->[$i];
179 my $space = ' ' x
($col_width - $event_width);
180 print("$space$event ");
190 # To find the list of active pids, we need to have
191 # the --vgdb-prefix option if given.
192 $vgdbPrefixOption = "";
193 foreach $arg (@ARGV) {
194 if ($arg =~ /^--vgdb-prefix=.*$/) {
195 $vgdbPrefixOption=$arg;
211 foreach $arg (@ARGV) {
213 if ($requestDump == 1) { $requestDump = 2; }
214 if ($requestEvents == 1) { $requestEvents = 2; }
216 if ($arg =~ /^(-h|--help)$/) {
219 elsif ($arg =~ /^--version$/) {
222 elsif ($arg =~ /^--vgdb-prefix=.*$/) {
223 # handled during the initial parsing.
226 elsif ($arg =~ /^-v$/) {
230 elsif ($arg =~ /^(-s|--stat)$/) {
234 elsif ($arg =~ /^(-b|--back)$/) {
238 elsif ($arg =~ /^-e$/) {
242 elsif ($arg =~ /^(-d|--dump)(|=.*)$/) {
245 $dumpHint = substr($2,1);
248 # take next argument as dump hint
253 elsif ($arg =~ /^(-z|--zero)$/) {
257 elsif ($arg =~ /^(-k|--kill)$/) {
261 elsif ($arg =~ /^(-i|--instr)(|=on|=off)$/) {
264 $switchInstrMode = "on";
266 elsif ($2 eq "=off") {
267 $switchInstrMode = "off";
270 # check next argument for "on" or "off"
276 print "Error: unknown command line option '$arg'.\n";
281 if ($arg =~ /^[A-Za-z_]/) {
282 # arguments of -d/-e/-i are non-numeric
283 if ($requestDump == 1) {
289 if ($requestEvents == 1) {
291 @show_events = split(/,/, $arg);
295 if ($switchInstr == 1) {
298 $switchInstrMode = "on";
300 elsif ($arg eq "off") {
301 $switchInstrMode = "off";
304 print "Error: need to specify 'on' or 'off' after '-i'.\n";
311 if (defined $cmd{$arg}) { $spids{$arg} = 1; next; }
314 if ($cmd{$p} =~ /$arg$/) {
319 if ($nameFound) { next; }
321 print "Error: Callgrind task with PID/name '$arg' not detected.\n";
326 if ($switchInstr == 1) {
327 print "Error: need to specify 'on' or 'off' after '-i'.\n";
331 if (scalar @pids == 0) {
332 print "No active callgrind runs detected.\n";
336 @spids = keys %spids;
337 if (scalar @spids >0) { @pids = @spids; }
342 $vgdbCommand = "dump";
343 if ($dumpHint ne "") { $vgdbCommand .= " ".$dumpHint; }
345 if ($requestZero) { $vgdbCommand = "zero"; }
346 if ($requestKill) { $vgdbCommand = "v.kill"; }
347 if ($switchInstr) { $vgdbCommand = "instrumentation ".$switchInstrMode; }
348 if ($printStatus || $printBacktrace || $requestEvents) {
349 $vgdbCommand = "status internal";
353 foreach $pid (@pids) {
354 $pidstr = "PID $pid: ";
355 if ($pid >0) { print $pidstr.$cmdline{$pid}; }
357 if ($vgdbCommand eq "") {
362 print " [requesting '$vgdbCommand']\n";
366 open RESULT
, "vgdb $vgdbPrefixOption --pid=$pid $vgdbCommand|";
388 if (/function-(\d+)-(\d+): (.+)$/) {
395 $func{$ctid,$fcount{$ctid}} = $3;
397 elsif (/calls-(\d+)-(\d+): (.+)$/) {
398 if ($ctid != $1) { next; }
399 $calls{$ctid,$fcount{$ctid}} = $3;
401 elsif (/events-(\d+)-(\d+): (.+)$/) {
402 if ($ctid != $1) { next; }
403 $events{$ctid,$fcount{$ctid}} = line_to_CC
($3);
405 elsif (/events-(\d+): (.+)$/) {
406 if (scalar @events == 0) { next; }
407 $totals{$1} = line_to_CC
($2);
409 elsif (/executed-bbs: (\d+)/) { $exec_bbs = $1; }
410 elsif (/distinct-bbs: (\d+)/) { $dist_bbs = $1; }
411 elsif (/executed-calls: (\d+)/) { $exec_calls = $1; }
412 elsif (/distinct-calls: (\d+)/) { $dist_calls = $1; }
413 elsif (/distinct-functions: (\d+)/) { $dist_funcs = $1; }
414 elsif (/distinct-contexts: (\d+)/) { $dist_ctxs = $1; }
415 elsif (/events: (.+)$/) { $events = $1; prepareEvents
; }
416 elsif (/threads: (.+)$/) { $threads = $1; @threads = split " ", $threads; }
417 elsif (/instrumentation: (\w+)$/) { $instrumentation = $1; }
420 #if ($? ne "0") { print " Got Error $?\n"; }
421 if (!$waitForAnswer) { print " OK.\n"; next; }
423 if ($instrumentation eq "off") {
424 print " No information available as instrumentation is switched off.\n\n";
429 if ($requestEvents <1) {
430 print " Number of running threads: " .($#threads+1). ", thread IDs: $threads\n";
431 print " Events collected: $events\n";
434 print " Functions: ".commify
($dist_funcs);
435 print " (executed ".commify
($exec_calls);
436 print ", contexts ".commify
($dist_ctxs).")\n";
438 print " Basic blocks: ".commify
($dist_bbs);
439 print " (executed ".commify
($exec_bbs);
440 print ", call sites ".commify
($dist_calls).")\n";
443 if ($requestEvents >0) {
444 $totals_width = compute_CC_col_widths
(values %totals);
446 print_events
($totals_width);
448 foreach $tid (@tids) {
449 print " Th".substr(" ".$tid,-2)." ";
450 print_CC
($totals{$tid}, $totals_width);
455 if ($printBacktrace) {
457 if ($requestEvents >0) {
458 $totals_width = compute_CC_col_widths
(values %events);
461 foreach $tid (@tids) {
463 if ($requestEvents >0) {
464 print_events
($totals_width);
466 print "Backtrace for Thread $tid\n";
470 while($i>0 && $c<100) {
471 $fc = substr(" $c",-2);
473 if ($requestEvents >0) {
474 print_CC
($events{$tid,$i-1}, $totals_width);
476 print $func{$tid,$i};
478 print " (".$calls{$tid,$i-1}." x)";