blob: 4fd28eba303caf577ac473d01c0f3f411625d3b0 [file] [log] [blame]
njn4f9c9342002-04-29 16:03:24 +00001#! /usr/bin/perl -w
2##--------------------------------------------------------------------##
3##--- The cache simulation framework: instrumentation, recording ---##
4##--- and results printing. ---##
5##--- vg_annotate ---##
6##--------------------------------------------------------------------##
7
8# This file is part of Valgrind, an x86 protected-mode emulator
9# designed for debugging and profiling binaries on x86-Unixes.
10#
sewardj3c23d432002-06-01 23:43:49 +000011# Copyright (C) 2002 Nicholas Nethercote
12# njn25@cam.ac.uk
njn4f9c9342002-04-29 16:03:24 +000013#
14# This program is free software; you can redistribute it and/or
15# modify it under the terms of the GNU General Public License as
16# published by the Free Software Foundation; either version 2 of the
17# License, or (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful, but
20# WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22# General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27# 02111-1307, USA.
28#
njn25e49d8e72002-09-23 09:36:25 +000029# The GNU General Public License is contained in the file COPYING.
njn4f9c9342002-04-29 16:03:24 +000030
31#----------------------------------------------------------------------------
njn75b30a92002-05-03 17:54:51 +000032# Annotator for cachegrind.
njn4f9c9342002-04-29 16:03:24 +000033#
njn75b30a92002-05-03 17:54:51 +000034# File format is described in /docs/techdocs.html.
njn4f9c9342002-04-29 16:03:24 +000035#
njn4f9c9342002-04-29 16:03:24 +000036# Performance improvements record, using cachegrind.out for cacheprof, doing no
37# source annotation (irrelevant ones removed):
38# user time
39# 1. turned off warnings in add_hash_a_to_b() 3.81 --> 3.48s
40# [now add_array_a_to_b()]
41# 6. make line_to_CC() return a ref instead of a hash 3.01 --> 2.77s
42#
43#10. changed file format to avoid file/fn name repetition 2.40s
44# (not sure why higher; maybe due to new '.' entries?)
45#11. changed file format to drop unnecessary end-line "."s 2.36s
46# (shrunk file by about 37%)
47#12. switched from hash CCs to array CCs 1.61s
48#13. only adding b[i] to a[i] if b[i] defined (was doing it if
49# either a[i] or b[i] was defined, but if b[i] was undefined
50# it just added 0) 1.48s
51#14. Stopped converting "." entries to undef and then back 1.16s
52#15. Using foreach $i (x..y) instead of for ($i = 0...) in
53# add_array_a_to_b() 1.11s
54#
55# Auto-annotating primes:
56#16. Finding count lengths by int((length-1)/3), not by
57# commifying (halves the number of commify calls) 1.68s --> 1.47s
58
59use strict;
60
61#----------------------------------------------------------------------------
62# Overview: the running example in the comments is for:
63# - events = A,B,C,D
64# - --show=C,A,D
65# - --sort=D,C
66#----------------------------------------------------------------------------
67
68#----------------------------------------------------------------------------
69# Global variables, main data structures
70#----------------------------------------------------------------------------
71# CCs are arrays, the counts corresponding to @events, with 'undef'
72# representing '.'. This makes things fast (faster than using hashes for CCs)
73# but we have to use @sort_order and @show_order below to handle the --sort and
74# --show options, which is a bit tricky.
75#----------------------------------------------------------------------------
76
77# Total counts for summary (an array reference).
78my $summary_CC;
79
80# Totals for each function, for overall summary.
81# hash(filename:fn_name => CC array)
82my %fn_totals;
83
84# Individual CCs, organised by filename and line_num for easy annotation.
85# hash(filename => hash(line_num => CC array))
86my %all_ind_CCs;
87
88# Files chosen for annotation on the command line.
89# key = basename (trimmed of any directory), value = full filename
90my %user_ann_files;
91
92# Generic description string.
93my $desc = "";
94
95# Command line of profiled program.
96my $cmd;
97
98# Events in input file, eg. (A,B,C,D)
99my @events;
100
101# Events to show, from command line, eg. (C,A,D)
102my @show_events;
103
104# Map from @show_events indices to @events indices, eg. (2,0,3). Gives the
105# order in which we must traverse @events in order to show the @show_events,
106# eg. (@events[$show_order[1]], @events[$show_order[2]]...) = @show_events.
107# (Might help to think of it like a hash (0 => 2, 1 => 0, 2 => 3).)
108my @show_order;
109
110# Print out the function totals sorted by these events, eg. (D,C).
111my @sort_events;
112
113# Map from @sort_events indices to @events indices, eg. (3,2). Same idea as
njnbff88762002-05-13 20:27:54 +0000114# for @show_order.
njn4f9c9342002-04-29 16:03:24 +0000115my @sort_order;
116
njnbff88762002-05-13 20:27:54 +0000117# Thresholds, one for each sort event (or default to 1 if no sort events
118# specified). We print out functions and do auto-annotations until we've
119# handled this proportion of all the events thresholded.
120my @thresholds;
121
122my $default_threshold = 99;
njn4f9c9342002-04-29 16:03:24 +0000123
njn9b3366a2002-06-10 15:31:16 +0000124my $single_threshold = $default_threshold;
125
njn4f9c9342002-04-29 16:03:24 +0000126# If on, automatically annotates all files that are involved in getting over
njnbff88762002-05-13 20:27:54 +0000127# all the threshold counts.
njn4f9c9342002-04-29 16:03:24 +0000128my $auto_annotate = 0;
129
130# Number of lines to show around each annotated line.
131my $context = 8;
132
133# Directories in which to look for annotation files.
134my @include_dirs = ("");
135
136# Input file name
njn25e49d8e72002-09-23 09:36:25 +0000137my $input_file = undef;
njn4f9c9342002-04-29 16:03:24 +0000138
139# Version number
140my $version = "@VERSION@";
141
142# Usage message.
143my $usage = <<END
njn25e49d8e72002-09-23 09:36:25 +0000144usage: vg_annotate [options] --<pid> [source-files]
njn4f9c9342002-04-29 16:03:24 +0000145
146 options for the user, with defaults in [ ], are:
147 -h --help show this message
148 -v --version show version
149 --show=A,B,C only show figures for events A,B,C [all]
150 --sort=A,B,C sort columns by events A,B,C [event column order]
151 --threshold=<0--100> percentage of counts (of primary sort event) we
njnbff88762002-05-13 20:27:54 +0000152 are interested in [$default_threshold%]
njn4f9c9342002-04-29 16:03:24 +0000153 --auto=yes|no annotate all source files containing functions
154 that helped reach the event count threshold [no]
155 --context=N print N lines of context before and after
156 annotated lines [8]
157 -I --include=<dir> add <dir> to list of directories to search for
158 source files
159
160 Valgrind is Copyright (C) 2000-2002 Julian Seward
161 and licensed under the GNU General Public License, version 2.
162 Bug reports, feedback, admiration, abuse, etc, to: jseward\@acm.org.
163
164END
165;
166
167# Used in various places of output.
168my $fancy = '-' x 80 . "\n";
169
170#-----------------------------------------------------------------------------
171# Argument and option handling
172#-----------------------------------------------------------------------------
173sub process_cmd_line()
174{
175 for my $arg (@ARGV) {
176
177 # Option handling
178 if ($arg =~ /^-/) {
179
180 # --version
181 if ($arg =~ /^-v$|^--version$/) {
sewardjaa6fecc2002-04-29 17:27:07 +0000182 die("vg_annotate-$version\n");
njn4f9c9342002-04-29 16:03:24 +0000183
184 # --show=A,B,C
185 } elsif ($arg =~ /^--show=(.*)$/) {
186 @show_events = split(/,/, $1);
187
188 # --sort=A,B,C
189 } elsif ($arg =~ /^--sort=(.*)$/) {
190 @sort_events = split(/,/, $1);
njnbff88762002-05-13 20:27:54 +0000191 foreach my $i (0 .. scalar @sort_events - 1) {
192 if ($sort_events[$i] =~#/.*:(\d+)$/) {
193 /.*:([\d\.]+)%?$/) {
194 my $th = $1;
195 ($th >= 0 && $th <= 100) or die($usage);
196 $sort_events[$i] =~ s/:.*//;
197 $thresholds[$i] = $th;
198 } else {
199 $thresholds[$i] = 0;
200 }
201 }
njn4f9c9342002-04-29 16:03:24 +0000202
203 # --threshold=X (tolerates a trailing '%')
204 } elsif ($arg =~ /^--threshold=([\d\.]+)%?$/) {
njn9b3366a2002-06-10 15:31:16 +0000205 $single_threshold = $1;
njnbff88762002-05-13 20:27:54 +0000206 ($1 >= 0 && $1 <= 100) or die($usage);
njn4f9c9342002-04-29 16:03:24 +0000207
208 # --auto=yes|no
209 } elsif ($arg =~ /^--auto=(yes|no)$/) {
210 $auto_annotate = 1 if ($1 eq "yes");
211 $auto_annotate = 0 if ($1 eq "no");
212
213 # --context=N
214 } elsif ($arg =~ /^--context=([\d\.]+)$/) {
215 $context = $1;
216 if ($context < 0) {
217 die($usage);
218 }
219
220 # --include=A,B,C
221 } elsif ($arg =~ /^(-I|--include)=(.*)$/) {
222 my $inc = $2;
223 $inc =~ s|/$||; # trim trailing '/'
224 push(@include_dirs, "$inc/");
225
njn25e49d8e72002-09-23 09:36:25 +0000226 } elsif ($arg =~ /^--(\d+)$/) {
227 my $pid = $1;
228 if (not defined $input_file) {
229 $input_file = "cachegrind.out.$pid";
230 } else {
231 die("One cachegrind.out.<pid> file at a time, please\n");
232 }
233
njn4f9c9342002-04-29 16:03:24 +0000234 } else { # -h and --help fall under this case
235 die($usage);
236 }
237
238 # Argument handling -- annotation file checking and selection.
njn25e49d8e72002-09-23 09:36:25 +0000239 # Stick filenames into a hash for quick 'n easy lookup throughout.
njn4f9c9342002-04-29 16:03:24 +0000240 } else {
241 my $readable = 0;
242 foreach my $include_dir (@include_dirs) {
243 if (-r $include_dir . $arg) {
244 $readable = 1;
245 }
246 }
247 $readable or die("File $arg not found in any of: @include_dirs\n");
248 $user_ann_files{$arg} = 1;
njn25e49d8e72002-09-23 09:36:25 +0000249 }
250 }
251
252 # Must have chosen an input file
253 if (not defined $input_file) {
254 die($usage);
njn4f9c9342002-04-29 16:03:24 +0000255 }
256}
257
258#-----------------------------------------------------------------------------
259# Reading of input file
260#-----------------------------------------------------------------------------
261sub max ($$)
262{
263 my ($x, $y) = @_;
264 return ($x > $y ? $x : $y);
265}
266
267# Add the two arrays; any '.' entries are ignored. Two tricky things:
268# 1. If $a2->[$i] is undefined, it defaults to 0 which is what we want; we turn
269# off warnings to allow this. This makes things about 10% faster than
270# checking for definedness ourselves.
njnbff88762002-05-13 20:27:54 +0000271# 2. We don't add an undefined count or a ".", even though it's value is 0,
272# because we don't want to make an $a2->[$i] that is undef become 0
273# unnecessarily.
njn4f9c9342002-04-29 16:03:24 +0000274sub add_array_a_to_b ($$)
275{
276 my ($a1, $a2) = @_;
277
278 my $n = max(scalar @$a1, scalar @$a2);
279 $^W = 0;
280 foreach my $i (0 .. $n-1) {
njnbff88762002-05-13 20:27:54 +0000281 $a2->[$i] += $a1->[$i] if (defined $a1->[$i] && "." ne $a1->[$i]);
njn4f9c9342002-04-29 16:03:24 +0000282 }
283 $^W = 1;
284}
285
286# Add each event count to the CC array. '.' counts become undef, as do
287# missing entries (implicitly).
288sub line_to_CC ($)
289{
290 my @CC = (split /\s+/, $_[0]);
291 (@CC <= @events) or die("Line $.: too many event counts\n");
292 return \@CC;
293}
294
295sub read_input_file()
296{
297 open(INPUTFILE, "< $input_file") || die "File $input_file not opened\n";
298
299 # Read "desc:" lines.
300 my $line;
301 # This gives a "uninitialized value in substitution (s///)" warning; hmm...
302 #while ($line = <INPUTFILE> && $line =~ s/desc:\s+//) {
303 # $desc .= "$line\n";
304 #}
305 while (1) {
306 $line = <INPUTFILE>;
307 if ($line =~ s/desc:\s+//) {
308 $desc .= $line;
309 } else {
310 last;
311 }
312 }
313
314 # Read "cmd:" line (Nb: will already be in $line from "desc:" loop above).
315 ($line =~ s/cmd:\s+//) or die("Line $.: missing command line\n");
316 $cmd = $line;
317 chomp($cmd); # Remove newline
318
319 # Read "events:" line. We make a temporary hash in which the Nth event's
320 # value is N, which is useful for handling --show/--sort options below.
321 $line = <INPUTFILE>;
322 ($line =~ s/events:\s+//) or die("Line $.: missing events line\n");
323 @events = split(/\s+/, $line);
324 my %events;
325 my $n = 0;
326 foreach my $event (@events) {
327 $events{$event} = $n;
328 $n++
329 }
330
331 # If no --show arg give, default to showing all events in the file.
332 # If --show option is used, check all specified events appeared in the
333 # "events:" line. Then initialise @show_order.
334 if (@show_events) {
335 foreach my $show_event (@show_events) {
336 (defined $events{$show_event}) or
337 die("--show event `$show_event' did not appear in input\n");
338 }
339 } else {
340 @show_events = @events;
341 }
342 foreach my $show_event (@show_events) {
343 push(@show_order, $events{$show_event});
344 }
345
346 # Do as for --show, but if no --sort arg given, default to sorting by
347 # column order (ie. first column event is primary sort key, 2nd column is
348 # 2ndary key, etc).
349 if (@sort_events) {
350 foreach my $sort_event (@sort_events) {
351 (defined $events{$sort_event}) or
352 die("--sort event `$sort_event' did not appear in input\n");
353 }
354 } else {
355 @sort_events = @events;
356 }
357 foreach my $sort_event (@sort_events) {
358 push(@sort_order, $events{$sort_event});
359 }
360
njn9b3366a2002-06-10 15:31:16 +0000361 # If multiple threshold args weren't given via --sort, stick in the single
362 # threshold (either from --threshold if used, or the default otherwise) for
363 # the primary sort event, and 0% for the rest.
njnbff88762002-05-13 20:27:54 +0000364 if (not @thresholds) {
365 foreach my $e (@sort_order) {
366 push(@thresholds, 0);
367 }
njn9b3366a2002-06-10 15:31:16 +0000368 $thresholds[0] = $single_threshold;
njnbff88762002-05-13 20:27:54 +0000369 }
370
njn4f9c9342002-04-29 16:03:24 +0000371 my $curr_file;
372 my $curr_fn;
373 my $curr_name;
374
375 my $curr_fn_CC = [];
376 my $curr_file_ind_CCs = {}; # hash(line_num => CC)
377
378 # Read body of input file.
379 while (<INPUTFILE>) {
380 s/#.*$//; # remove comments
381 if (s/^(\d+)\s+//) {
382 my $line_num = $1;
383 my $CC = line_to_CC($_);
384 add_array_a_to_b($CC, $curr_fn_CC);
385
386 # If curr_file is selected, add CC to curr_file list. We look for
387 # full filename matches; or, if auto-annotating, we have to
388 # remember everything -- we won't know until the end what's needed.
389 if ($auto_annotate || defined $user_ann_files{$curr_file}) {
390 my $tmp = $curr_file_ind_CCs->{$line_num};
391 $tmp = [] unless defined $tmp;
392 add_array_a_to_b($CC, $tmp);
393 $curr_file_ind_CCs->{$line_num} = $tmp;
394 }
395
396 } elsif (s/^fn=(.*)$//) {
397 # Commit result from previous function
398 $fn_totals{$curr_name} = $curr_fn_CC if (defined $curr_name);
399
400 # Setup new one
401 $curr_fn = $1;
402 $curr_name = "$curr_file:$curr_fn";
403 $curr_fn_CC = $fn_totals{$curr_name};
404 $curr_fn_CC = [] unless (defined $curr_fn_CC);
405
406 } elsif (s/^fl=(.*)$//) {
407 $all_ind_CCs{$curr_file} = $curr_file_ind_CCs
408 if (defined $curr_file);
409
410 $curr_file = $1;
411 $curr_file_ind_CCs = $all_ind_CCs{$curr_file};
412 $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs);
413
414 } elsif (s/^(fi|fe)=(.*)$//) {
415 (defined $curr_name) or die("Line $.: Unexpected fi/fe line\n");
416 $fn_totals{$curr_name} = $curr_fn_CC;
417 $all_ind_CCs{$curr_file} = $curr_file_ind_CCs;
418
419 $curr_file = $2;
420 $curr_name = "$curr_file:$curr_fn";
421 $curr_file_ind_CCs = $all_ind_CCs{$curr_file};
422 $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs);
423 $curr_fn_CC = $fn_totals{$curr_name};
424 $curr_fn_CC = [] unless (defined $curr_fn_CC);
425
426 } elsif (s/^\s*$//) {
427 # blank, do nothing
428
429 } elsif (s/^summary:\s+//) {
430 # Finish up handling final filename/fn_name counts
431 $fn_totals{"$curr_file:$curr_fn"} = $curr_fn_CC
432 if (defined $curr_file && defined $curr_fn);
433 $all_ind_CCs{$curr_file} =
434 $curr_file_ind_CCs if (defined $curr_file);
435
436 $summary_CC = line_to_CC($_);
437 (scalar(@$summary_CC) == @events)
438 or die("Line $.: summary event and total event mismatch\n");
439
440 } else {
441 warn("WARNING: line $. malformed, ignoring\n");
442 }
443 }
444
445 # Check if summary line was present
446 if (not defined $summary_CC) {
447 warn("WARNING: missing final summary line, no summary will be printed\n");
448 }
449
450 close(INPUTFILE);
451}
452
453#-----------------------------------------------------------------------------
454# Print options used
455#-----------------------------------------------------------------------------
456sub print_options ()
457{
458 print($fancy);
459 print($desc);
460 print("Command: $cmd\n");
461 print("Events recorded: @events\n");
462 print("Events shown: @show_events\n");
463 print("Event sort order: @sort_events\n");
njnbff88762002-05-13 20:27:54 +0000464 print("Thresholds: @thresholds\n");
njn4f9c9342002-04-29 16:03:24 +0000465
466 my @include_dirs2 = @include_dirs; # copy @include_dirs
467 shift(@include_dirs2); # remove "" entry, which is always the first
468 unshift(@include_dirs2, "") if (0 == @include_dirs2);
469 my $include_dir = shift(@include_dirs2);
470 print("Include dirs: $include_dir\n");
471 foreach my $include_dir (@include_dirs2) {
472 print(" $include_dir\n");
473 }
474
475 my @user_ann_files = keys %user_ann_files;
476 unshift(@user_ann_files, "") if (0 == @user_ann_files);
477 my $user_ann_file = shift(@user_ann_files);
478 print("User annotated: $user_ann_file\n");
479 foreach $user_ann_file (@user_ann_files) {
480 print(" $user_ann_file\n");
481 }
482
483 my $is_on = ($auto_annotate ? "on" : "off");
484 print("Auto-annotation: $is_on\n");
485 print("\n");
486}
487
488#-----------------------------------------------------------------------------
489# Print summary and sorted function totals
490#-----------------------------------------------------------------------------
491sub mycmp ($$)
492{
493 my ($c, $d) = @_;
494
495 # Iterate through sort events (eg. 3,2); return result if two are different
496 foreach my $i (@sort_order) {
497 my ($x, $y);
498 $x = $c->[$i];
499 $y = $d->[$i];
500 $x = -1 unless defined $x;
501 $y = -1 unless defined $y;
502
503 my $cmp = $y <=> $x; # reverse sort
504 if (0 != $cmp) {
505 return $cmp;
506 }
507 }
508 # Exhausted events, equal
509 return 0;
510}
511
512sub commify ($) {
513 my ($val) = @_;
514 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
515 return $val;
516}
517
518# Because the counts can get very big, and we don't want to waste screen space
519# and make lines too long, we compute exactly how wide each column needs to be
520# by finding the widest entry for each one.
521sub compute_CC_col_widths (@)
522{
523 my @CCs = @_;
524 my $CC_col_widths = [];
525
526 # Initialise with minimum widths (from event names)
527 foreach my $event (@events) {
528 push(@$CC_col_widths, length($event));
529 }
530
531 # Find maximum width count for each column. @CC_col_width positions
532 # correspond to @CC positions.
533 foreach my $CC (@CCs) {
534 foreach my $i (0 .. scalar(@$CC)-1) {
535 if (defined $CC->[$i]) {
536 # Find length, accounting for commas that will be added
537 my $length = length $CC->[$i];
538 my $clength = $length + int(($length - 1) / 3);
539 $CC_col_widths->[$i] = max($CC_col_widths->[$i], $clength);
540 }
541 }
542 }
543 return $CC_col_widths;
544}
545
546# Print the CC with each column's size dictated by $CC_col_widths.
547sub print_CC ($$)
548{
549 my ($CC, $CC_col_widths) = @_;
550
551 foreach my $i (@show_order) {
552 my $count = (defined $CC->[$i] ? commify($CC->[$i]) : ".");
553 my $space = ' ' x ($CC_col_widths->[$i] - length($count));
554 print("$space$count ");
555 }
556}
557
558sub print_events ($)
559{
560 my ($CC_col_widths) = @_;
561
562 foreach my $i (@show_order) {
563 my $event = $events[$i];
564 my $event_width = length($event);
565 my $col_width = $CC_col_widths->[$i];
566 my $space = ' ' x ($col_width - $event_width);
njn602392b2002-04-30 11:34:54 +0000567 print("$space$event ");
njn4f9c9342002-04-29 16:03:24 +0000568 }
569}
570
571# Prints summary and function totals (with separate column widths, so that
572# function names aren't pushed over unnecessarily by huge summary figures).
573# Also returns a hash containing all the files that are involved in getting the
njnbff88762002-05-13 20:27:54 +0000574# events count above the thresholds (ie. all the interesting ones).
njn4f9c9342002-04-29 16:03:24 +0000575sub print_summary_and_fn_totals ()
576{
577 my @fn_fullnames = keys %fn_totals;
578
579 # Work out the size of each column for printing (summary and functions
580 # separately).
581 my $summary_CC_col_widths = compute_CC_col_widths($summary_CC);
582 my $fn_CC_col_widths = compute_CC_col_widths(values %fn_totals);
583
584 # Header and counts for summary
585 print($fancy);
586 print_events($summary_CC_col_widths);
587 print("\n");
588 print($fancy);
589 print_CC($summary_CC, $summary_CC_col_widths);
590 print(" PROGRAM TOTALS\n");
591 print("\n");
592
593 # Header for functions
594 print($fancy);
595 print_events($fn_CC_col_widths);
596 print(" file:function\n");
597 print($fancy);
598
599 # Sort function names into order dictated by --sort option.
600 @fn_fullnames = sort {
601 mycmp($fn_totals{$a}, $fn_totals{$b})
602 } @fn_fullnames;
603
njnbff88762002-05-13 20:27:54 +0000604
605 # Assertion
606 (scalar @sort_order == scalar @thresholds) or
607 die("sort_order length != thresholds length:\n",
608 " @sort_order\n @thresholds\n");
609
njn4f9c9342002-04-29 16:03:24 +0000610 my $threshold_files = {};
njnbff88762002-05-13 20:27:54 +0000611 # @curr_totals has the same shape as @sort_order and @thresholds
612 my @curr_totals = ();
613 foreach my $e (@thresholds) {
614 push(@curr_totals, 0);
615 }
njn4f9c9342002-04-29 16:03:24 +0000616
617 # Print functions, stopping when the threshold has been reached.
618 foreach my $fn_name (@fn_fullnames) {
619
njnbff88762002-05-13 20:27:54 +0000620 # Stop when we've reached all the thresholds
621 my $reached_all_thresholds = 1;
njnb94e77a2002-05-15 14:30:55 +0000622 foreach my $i (0 .. scalar @thresholds - 1) {
njnbff88762002-05-13 20:27:54 +0000623 my $prop = $curr_totals[$i] * 100 / $summary_CC->[$sort_order[$i]];
624 $reached_all_thresholds &= ($prop >= $thresholds[$i]);
625 }
626 last if $reached_all_thresholds;
njn4f9c9342002-04-29 16:03:24 +0000627
628 # Print function results
629 my $fn_CC = $fn_totals{$fn_name};
630 print_CC($fn_CC, $fn_CC_col_widths);
631 print(" $fn_name\n");
632
njnbff88762002-05-13 20:27:54 +0000633 # Update the threshold counts
njn4f9c9342002-04-29 16:03:24 +0000634 my $filename = $fn_name;
daywalkerd722c202002-05-01 21:52:05 +0000635 $filename =~ s/:.+$//; # remove function name
njn4f9c9342002-04-29 16:03:24 +0000636 $threshold_files->{$filename} = 1;
njnbff88762002-05-13 20:27:54 +0000637 foreach my $i (0 .. scalar @sort_order - 1) {
638 $curr_totals[$i] += $fn_CC->[$sort_order[$i]]
639 if (defined $fn_CC->[$sort_order[$i]]);
640 }
njn4f9c9342002-04-29 16:03:24 +0000641 }
642 print("\n");
643
644 return $threshold_files;
645}
646
647#-----------------------------------------------------------------------------
648# Annotate selected files
649#-----------------------------------------------------------------------------
650
651# Issue a warning that the source file is more recent than the input file.
652sub warning_on_src_more_recent_than_inputfile ($)
653{
654 my $src_file = $_[0];
655
656 my $warning = <<END
657@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
658@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
659@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
660@ Source file '$src_file' is more recent than input file '$input_file'.
661@ Annotations may not be correct.
662@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
663
664END
665;
666 print($warning);
667}
668
669# If there is information about lines not in the file, issue a warning
670# explaining possible causes.
671sub warning_on_nonexistent_lines ($$$)
672{
673 my ($src_more_recent_than_inputfile, $src_file, $excess_line_nums) = @_;
674 my $cause_and_solution;
675
676 if ($src_more_recent_than_inputfile) {
677 $cause_and_solution = <<END
678@@ cause: '$src_file' has changed since information was gathered.
679@@ If so, a warning will have already been issued about this.
680@@ solution: Recompile program and rerun under "valgrind --cachesim=yes" to
681@@ gather new information.
682END
683 # We suppress warnings about .h files
684 } elsif ($src_file =~ /\.h$/) {
685 $cause_and_solution = <<END
686@@ cause: bug in the Valgrind's debug info reader that screws up with .h
687@@ files sometimes
688@@ solution: none, sorry
689END
690 } else {
691 $cause_and_solution = <<END
692@@ cause: not sure, sorry
693END
694 }
695
696 my $warning = <<END
697@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
698@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
699@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
700@@
701@@ Information recorded about lines past the end of '$src_file'.
702@@
703@@ Probable cause and solution:
704$cause_and_solution@@
705@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
706END
707;
708 print($warning);
709}
710
711sub annotate_ann_files($)
712{
713 my ($threshold_files) = @_;
714
715 my %all_ann_files;
716 my @unfound_auto_annotate_files;
717 my $printed_totals_CC = [];
718
719 # If auto-annotating, add interesting files (but not "???")
720 if ($auto_annotate) {
721 delete $threshold_files->{"???"};
722 %all_ann_files = (%user_ann_files, %$threshold_files)
723 } else {
724 %all_ann_files = %user_ann_files;
725 }
726
727 # Track if we did any annotations.
728 my $did_annotations = 0;
729
730 LOOP:
731 foreach my $src_file (keys %all_ann_files) {
732
733 my $opened_file = "";
734 my $full_file_name = "";
735 foreach my $include_dir (@include_dirs) {
736 my $try_name = $include_dir . $src_file;
737 if (open(INPUTFILE, "< $try_name")) {
738 $opened_file = $try_name;
739 $full_file_name = ($include_dir eq ""
740 ? $src_file
741 : "$include_dir + $src_file");
742 last;
743 }
744 }
745
746 if (not $opened_file) {
747 # Failed to open the file. If chosen on the command line, die.
748 # If arose from auto-annotation, print a little message.
749 if (defined $user_ann_files{$src_file}) {
750 die("File $src_file not opened in any of: @include_dirs\n");
751
752 } else {
753 push(@unfound_auto_annotate_files, $src_file);
754 }
755
756 } else {
757 # File header (distinguish between user- and auto-selected files).
758 print("$fancy");
759 my $ann_type =
760 (defined $user_ann_files{$src_file} ? "User" : "Auto");
761 print("-- $ann_type-annotated source: $full_file_name\n");
762 print("$fancy");
763
764 # Get file's CCs
765 my $src_file_CCs = $all_ind_CCs{$src_file};
766 if (!defined $src_file_CCs) {
767 print(" No information has been collected for $src_file\n\n");
768 next LOOP;
769 }
770
771 $did_annotations = 1;
772
773 # Numeric, not lexicographic sort!
774 my @line_nums = sort {$a <=> $b} keys %$src_file_CCs;
775
776 # If $src_file more recent than cachegrind.out, issue warning
777 my $src_more_recent_than_inputfile = 0;
778 if ((stat $opened_file)[9] > (stat $input_file)[9]) {
779 $src_more_recent_than_inputfile = 1;
780 warning_on_src_more_recent_than_inputfile($src_file);
781 }
782
783 # Work out the size of each column for printing
784 my $CC_col_widths = compute_CC_col_widths(values %$src_file_CCs);
785
786 # Events header
787 print_events($CC_col_widths);
788 print("\n\n");
789
790 # Shift out 0 if it's in the line numbers (from unknown entries,
791 # likely due to bugs in Valgrind's stabs debug info reader)
792 shift(@line_nums) if (0 == $line_nums[0]);
793
794 # Finds interesting line ranges -- all lines with a CC, and all
795 # lines within $context lines of a line with a CC.
796 my $n = @line_nums;
797 my @pairs;
798 for (my $i = 0; $i < $n; $i++) {
799 push(@pairs, $line_nums[$i] - $context); # lower marker
800 while ($i < $n-1 &&
801 $line_nums[$i] + 2*$context >= $line_nums[$i+1]) {
802 $i++;
803 }
804 push(@pairs, $line_nums[$i] + $context); # upper marker
805 }
806
807 # Annotate chosen lines, tracking total counts of lines printed
808 $pairs[0] = 1 if ($pairs[0] < 1);
809 while (@pairs) {
810 my $low = shift @pairs;
811 my $high = shift @pairs;
812 while ($. < $low-1) {
813 my $tmp = <INPUTFILE>;
814 last unless (defined $tmp); # hack to detect EOF
815 }
816 my $src_line;
817 # Print line number, unless start of file
818 print("-- line $low " . '-' x 40 . "\n") if ($low != 1);
819 while (($. < $high) && ($src_line = <INPUTFILE>)) {
820 if (defined $line_nums[0] && $. == $line_nums[0]) {
821 print_CC($src_file_CCs->{$.}, $CC_col_widths);
822 add_array_a_to_b($src_file_CCs->{$.},
823 $printed_totals_CC);
824 shift(@line_nums);
825
826 } else {
827 print_CC( [], $CC_col_widths);
828 }
829
830 print(" $src_line");
831 }
832 # Print line number, unless EOF
833 if ($src_line) {
834 print("-- line $high " . '-' x 40 . "\n");
835 } else {
836 last;
837 }
838 }
839
840 # If there was info on lines past the end of the file...
841 if (@line_nums) {
842 foreach my $line_num (@line_nums) {
843 print_CC($src_file_CCs->{$line_num}, $CC_col_widths);
844 print(" <bogus line $line_num>\n");
845 }
846 print("\n");
847 warning_on_nonexistent_lines($src_more_recent_than_inputfile,
848 $src_file, \@line_nums);
849 }
850 print("\n");
851
852 # Print summary of counts attributed to file but not to any
853 # particular line (due to incomplete debug info).
854 if ($src_file_CCs->{0}) {
855 print_CC($src_file_CCs->{0}, $CC_col_widths);
856 print(" <counts for unidentified lines in $src_file>\n\n");
857 }
858
859 close(INPUTFILE);
860 }
861 }
862
863 # Print list of unfound auto-annotate selected files.
864 if (@unfound_auto_annotate_files) {
865 print("$fancy");
866 print("The following files chosen for auto-annotation could not be found:\n");
867 print($fancy);
868 foreach my $f (@unfound_auto_annotate_files) {
869 print(" $f\n");
870 }
871 print("\n");
872 }
873
874 # If we did any annotating, print what proportion of events were covered by
875 # annotated lines above.
876 if ($did_annotations) {
877 my $percent_printed_CC;
878 foreach (my $i = 0; $i < @$summary_CC; $i++) {
879 $percent_printed_CC->[$i] =
880 sprintf("%.0f",
881 $printed_totals_CC->[$i] / $summary_CC->[$i] * 100);
882 }
883 my $pp_CC_col_widths = compute_CC_col_widths($percent_printed_CC);
884 print($fancy);
885 print_events($pp_CC_col_widths);
886 print("\n");
887 print($fancy);
888 print_CC($percent_printed_CC, $pp_CC_col_widths);
889 print(" percentage of events annotated\n\n");
890 }
891}
892
893#----------------------------------------------------------------------------
894# "main()"
895#----------------------------------------------------------------------------
896process_cmd_line();
897read_input_file();
898print_options();
899my $threshold_files = print_summary_and_fn_totals();
900annotate_ann_files($threshold_files);
901
njn7cf0bd32002-06-08 13:36:03 +0000902##--------------------------------------------------------------------##
903##--- end vg_annotate.in ---##
904##--------------------------------------------------------------------##
905
906