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