blob: 11821901c22bdbd457f959161d33d9c19194ded2 [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#
29# The GNU General Public License is contained in the file LICENSE.
30
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
137my $input_file = "cachegrind.out";
138
139# Version number
140my $version = "@VERSION@";
141
142# Usage message.
143my $usage = <<END
144usage: vg_annotate [options] [source-files]
145
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
226 } else { # -h and --help fall under this case
227 die($usage);
228 }
229
230 # Argument handling -- annotation file checking and selection.
231 # Stick filenames into a hash for quick 'n easy lookup throughout
232 } else {
233 my $readable = 0;
234 foreach my $include_dir (@include_dirs) {
235 if (-r $include_dir . $arg) {
236 $readable = 1;
237 }
238 }
239 $readable or die("File $arg not found in any of: @include_dirs\n");
240 $user_ann_files{$arg} = 1;
241 }
242 }
243}
244
245#-----------------------------------------------------------------------------
246# Reading of input file
247#-----------------------------------------------------------------------------
248sub max ($$)
249{
250 my ($x, $y) = @_;
251 return ($x > $y ? $x : $y);
252}
253
254# Add the two arrays; any '.' entries are ignored. Two tricky things:
255# 1. If $a2->[$i] is undefined, it defaults to 0 which is what we want; we turn
256# off warnings to allow this. This makes things about 10% faster than
257# checking for definedness ourselves.
njnbff88762002-05-13 20:27:54 +0000258# 2. We don't add an undefined count or a ".", even though it's value is 0,
259# because we don't want to make an $a2->[$i] that is undef become 0
260# unnecessarily.
njn4f9c9342002-04-29 16:03:24 +0000261sub add_array_a_to_b ($$)
262{
263 my ($a1, $a2) = @_;
264
265 my $n = max(scalar @$a1, scalar @$a2);
266 $^W = 0;
267 foreach my $i (0 .. $n-1) {
njnbff88762002-05-13 20:27:54 +0000268 $a2->[$i] += $a1->[$i] if (defined $a1->[$i] && "." ne $a1->[$i]);
njn4f9c9342002-04-29 16:03:24 +0000269 }
270 $^W = 1;
271}
272
273# Add each event count to the CC array. '.' counts become undef, as do
274# missing entries (implicitly).
275sub line_to_CC ($)
276{
277 my @CC = (split /\s+/, $_[0]);
278 (@CC <= @events) or die("Line $.: too many event counts\n");
279 return \@CC;
280}
281
282sub read_input_file()
283{
284 open(INPUTFILE, "< $input_file") || die "File $input_file not opened\n";
285
286 # Read "desc:" lines.
287 my $line;
288 # This gives a "uninitialized value in substitution (s///)" warning; hmm...
289 #while ($line = <INPUTFILE> && $line =~ s/desc:\s+//) {
290 # $desc .= "$line\n";
291 #}
292 while (1) {
293 $line = <INPUTFILE>;
294 if ($line =~ s/desc:\s+//) {
295 $desc .= $line;
296 } else {
297 last;
298 }
299 }
300
301 # Read "cmd:" line (Nb: will already be in $line from "desc:" loop above).
302 ($line =~ s/cmd:\s+//) or die("Line $.: missing command line\n");
303 $cmd = $line;
304 chomp($cmd); # Remove newline
305
306 # Read "events:" line. We make a temporary hash in which the Nth event's
307 # value is N, which is useful for handling --show/--sort options below.
308 $line = <INPUTFILE>;
309 ($line =~ s/events:\s+//) or die("Line $.: missing events line\n");
310 @events = split(/\s+/, $line);
311 my %events;
312 my $n = 0;
313 foreach my $event (@events) {
314 $events{$event} = $n;
315 $n++
316 }
317
318 # If no --show arg give, default to showing all events in the file.
319 # If --show option is used, check all specified events appeared in the
320 # "events:" line. Then initialise @show_order.
321 if (@show_events) {
322 foreach my $show_event (@show_events) {
323 (defined $events{$show_event}) or
324 die("--show event `$show_event' did not appear in input\n");
325 }
326 } else {
327 @show_events = @events;
328 }
329 foreach my $show_event (@show_events) {
330 push(@show_order, $events{$show_event});
331 }
332
333 # Do as for --show, but if no --sort arg given, default to sorting by
334 # column order (ie. first column event is primary sort key, 2nd column is
335 # 2ndary key, etc).
336 if (@sort_events) {
337 foreach my $sort_event (@sort_events) {
338 (defined $events{$sort_event}) or
339 die("--sort event `$sort_event' did not appear in input\n");
340 }
341 } else {
342 @sort_events = @events;
343 }
344 foreach my $sort_event (@sort_events) {
345 push(@sort_order, $events{$sort_event});
346 }
347
njn9b3366a2002-06-10 15:31:16 +0000348 # If multiple threshold args weren't given via --sort, stick in the single
349 # threshold (either from --threshold if used, or the default otherwise) for
350 # the primary sort event, and 0% for the rest.
njnbff88762002-05-13 20:27:54 +0000351 if (not @thresholds) {
352 foreach my $e (@sort_order) {
353 push(@thresholds, 0);
354 }
njn9b3366a2002-06-10 15:31:16 +0000355 $thresholds[0] = $single_threshold;
njnbff88762002-05-13 20:27:54 +0000356 }
357
njn4f9c9342002-04-29 16:03:24 +0000358 my $curr_file;
359 my $curr_fn;
360 my $curr_name;
361
362 my $curr_fn_CC = [];
363 my $curr_file_ind_CCs = {}; # hash(line_num => CC)
364
365 # Read body of input file.
366 while (<INPUTFILE>) {
367 s/#.*$//; # remove comments
368 if (s/^(\d+)\s+//) {
369 my $line_num = $1;
370 my $CC = line_to_CC($_);
371 add_array_a_to_b($CC, $curr_fn_CC);
372
373 # If curr_file is selected, add CC to curr_file list. We look for
374 # full filename matches; or, if auto-annotating, we have to
375 # remember everything -- we won't know until the end what's needed.
376 if ($auto_annotate || defined $user_ann_files{$curr_file}) {
377 my $tmp = $curr_file_ind_CCs->{$line_num};
378 $tmp = [] unless defined $tmp;
379 add_array_a_to_b($CC, $tmp);
380 $curr_file_ind_CCs->{$line_num} = $tmp;
381 }
382
383 } elsif (s/^fn=(.*)$//) {
384 # Commit result from previous function
385 $fn_totals{$curr_name} = $curr_fn_CC if (defined $curr_name);
386
387 # Setup new one
388 $curr_fn = $1;
389 $curr_name = "$curr_file:$curr_fn";
390 $curr_fn_CC = $fn_totals{$curr_name};
391 $curr_fn_CC = [] unless (defined $curr_fn_CC);
392
393 } elsif (s/^fl=(.*)$//) {
394 $all_ind_CCs{$curr_file} = $curr_file_ind_CCs
395 if (defined $curr_file);
396
397 $curr_file = $1;
398 $curr_file_ind_CCs = $all_ind_CCs{$curr_file};
399 $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs);
400
401 } elsif (s/^(fi|fe)=(.*)$//) {
402 (defined $curr_name) or die("Line $.: Unexpected fi/fe line\n");
403 $fn_totals{$curr_name} = $curr_fn_CC;
404 $all_ind_CCs{$curr_file} = $curr_file_ind_CCs;
405
406 $curr_file = $2;
407 $curr_name = "$curr_file:$curr_fn";
408 $curr_file_ind_CCs = $all_ind_CCs{$curr_file};
409 $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs);
410 $curr_fn_CC = $fn_totals{$curr_name};
411 $curr_fn_CC = [] unless (defined $curr_fn_CC);
412
413 } elsif (s/^\s*$//) {
414 # blank, do nothing
415
416 } elsif (s/^summary:\s+//) {
417 # Finish up handling final filename/fn_name counts
418 $fn_totals{"$curr_file:$curr_fn"} = $curr_fn_CC
419 if (defined $curr_file && defined $curr_fn);
420 $all_ind_CCs{$curr_file} =
421 $curr_file_ind_CCs if (defined $curr_file);
422
423 $summary_CC = line_to_CC($_);
424 (scalar(@$summary_CC) == @events)
425 or die("Line $.: summary event and total event mismatch\n");
426
427 } else {
428 warn("WARNING: line $. malformed, ignoring\n");
429 }
430 }
431
432 # Check if summary line was present
433 if (not defined $summary_CC) {
434 warn("WARNING: missing final summary line, no summary will be printed\n");
435 }
436
437 close(INPUTFILE);
438}
439
440#-----------------------------------------------------------------------------
441# Print options used
442#-----------------------------------------------------------------------------
443sub print_options ()
444{
445 print($fancy);
446 print($desc);
447 print("Command: $cmd\n");
448 print("Events recorded: @events\n");
449 print("Events shown: @show_events\n");
450 print("Event sort order: @sort_events\n");
njnbff88762002-05-13 20:27:54 +0000451 print("Thresholds: @thresholds\n");
njn4f9c9342002-04-29 16:03:24 +0000452
453 my @include_dirs2 = @include_dirs; # copy @include_dirs
454 shift(@include_dirs2); # remove "" entry, which is always the first
455 unshift(@include_dirs2, "") if (0 == @include_dirs2);
456 my $include_dir = shift(@include_dirs2);
457 print("Include dirs: $include_dir\n");
458 foreach my $include_dir (@include_dirs2) {
459 print(" $include_dir\n");
460 }
461
462 my @user_ann_files = keys %user_ann_files;
463 unshift(@user_ann_files, "") if (0 == @user_ann_files);
464 my $user_ann_file = shift(@user_ann_files);
465 print("User annotated: $user_ann_file\n");
466 foreach $user_ann_file (@user_ann_files) {
467 print(" $user_ann_file\n");
468 }
469
470 my $is_on = ($auto_annotate ? "on" : "off");
471 print("Auto-annotation: $is_on\n");
472 print("\n");
473}
474
475#-----------------------------------------------------------------------------
476# Print summary and sorted function totals
477#-----------------------------------------------------------------------------
478sub mycmp ($$)
479{
480 my ($c, $d) = @_;
481
482 # Iterate through sort events (eg. 3,2); return result if two are different
483 foreach my $i (@sort_order) {
484 my ($x, $y);
485 $x = $c->[$i];
486 $y = $d->[$i];
487 $x = -1 unless defined $x;
488 $y = -1 unless defined $y;
489
490 my $cmp = $y <=> $x; # reverse sort
491 if (0 != $cmp) {
492 return $cmp;
493 }
494 }
495 # Exhausted events, equal
496 return 0;
497}
498
499sub commify ($) {
500 my ($val) = @_;
501 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
502 return $val;
503}
504
505# Because the counts can get very big, and we don't want to waste screen space
506# and make lines too long, we compute exactly how wide each column needs to be
507# by finding the widest entry for each one.
508sub compute_CC_col_widths (@)
509{
510 my @CCs = @_;
511 my $CC_col_widths = [];
512
513 # Initialise with minimum widths (from event names)
514 foreach my $event (@events) {
515 push(@$CC_col_widths, length($event));
516 }
517
518 # Find maximum width count for each column. @CC_col_width positions
519 # correspond to @CC positions.
520 foreach my $CC (@CCs) {
521 foreach my $i (0 .. scalar(@$CC)-1) {
522 if (defined $CC->[$i]) {
523 # Find length, accounting for commas that will be added
524 my $length = length $CC->[$i];
525 my $clength = $length + int(($length - 1) / 3);
526 $CC_col_widths->[$i] = max($CC_col_widths->[$i], $clength);
527 }
528 }
529 }
530 return $CC_col_widths;
531}
532
533# Print the CC with each column's size dictated by $CC_col_widths.
534sub print_CC ($$)
535{
536 my ($CC, $CC_col_widths) = @_;
537
538 foreach my $i (@show_order) {
539 my $count = (defined $CC->[$i] ? commify($CC->[$i]) : ".");
540 my $space = ' ' x ($CC_col_widths->[$i] - length($count));
541 print("$space$count ");
542 }
543}
544
545sub print_events ($)
546{
547 my ($CC_col_widths) = @_;
548
549 foreach my $i (@show_order) {
550 my $event = $events[$i];
551 my $event_width = length($event);
552 my $col_width = $CC_col_widths->[$i];
553 my $space = ' ' x ($col_width - $event_width);
njn602392b2002-04-30 11:34:54 +0000554 print("$space$event ");
njn4f9c9342002-04-29 16:03:24 +0000555 }
556}
557
558# Prints summary and function totals (with separate column widths, so that
559# function names aren't pushed over unnecessarily by huge summary figures).
560# Also returns a hash containing all the files that are involved in getting the
njnbff88762002-05-13 20:27:54 +0000561# events count above the thresholds (ie. all the interesting ones).
njn4f9c9342002-04-29 16:03:24 +0000562sub print_summary_and_fn_totals ()
563{
564 my @fn_fullnames = keys %fn_totals;
565
566 # Work out the size of each column for printing (summary and functions
567 # separately).
568 my $summary_CC_col_widths = compute_CC_col_widths($summary_CC);
569 my $fn_CC_col_widths = compute_CC_col_widths(values %fn_totals);
570
571 # Header and counts for summary
572 print($fancy);
573 print_events($summary_CC_col_widths);
574 print("\n");
575 print($fancy);
576 print_CC($summary_CC, $summary_CC_col_widths);
577 print(" PROGRAM TOTALS\n");
578 print("\n");
579
580 # Header for functions
581 print($fancy);
582 print_events($fn_CC_col_widths);
583 print(" file:function\n");
584 print($fancy);
585
586 # Sort function names into order dictated by --sort option.
587 @fn_fullnames = sort {
588 mycmp($fn_totals{$a}, $fn_totals{$b})
589 } @fn_fullnames;
590
njnbff88762002-05-13 20:27:54 +0000591
592 # Assertion
593 (scalar @sort_order == scalar @thresholds) or
594 die("sort_order length != thresholds length:\n",
595 " @sort_order\n @thresholds\n");
596
njn4f9c9342002-04-29 16:03:24 +0000597 my $threshold_files = {};
njnbff88762002-05-13 20:27:54 +0000598 # @curr_totals has the same shape as @sort_order and @thresholds
599 my @curr_totals = ();
600 foreach my $e (@thresholds) {
601 push(@curr_totals, 0);
602 }
njn4f9c9342002-04-29 16:03:24 +0000603
604 # Print functions, stopping when the threshold has been reached.
605 foreach my $fn_name (@fn_fullnames) {
606
njnbff88762002-05-13 20:27:54 +0000607 # Stop when we've reached all the thresholds
608 my $reached_all_thresholds = 1;
njnb94e77a2002-05-15 14:30:55 +0000609 foreach my $i (0 .. scalar @thresholds - 1) {
njnbff88762002-05-13 20:27:54 +0000610 my $prop = $curr_totals[$i] * 100 / $summary_CC->[$sort_order[$i]];
611 $reached_all_thresholds &= ($prop >= $thresholds[$i]);
612 }
613 last if $reached_all_thresholds;
njn4f9c9342002-04-29 16:03:24 +0000614
615 # Print function results
616 my $fn_CC = $fn_totals{$fn_name};
617 print_CC($fn_CC, $fn_CC_col_widths);
618 print(" $fn_name\n");
619
njnbff88762002-05-13 20:27:54 +0000620 # Update the threshold counts
njn4f9c9342002-04-29 16:03:24 +0000621 my $filename = $fn_name;
daywalkerd722c202002-05-01 21:52:05 +0000622 $filename =~ s/:.+$//; # remove function name
njn4f9c9342002-04-29 16:03:24 +0000623 $threshold_files->{$filename} = 1;
njnbff88762002-05-13 20:27:54 +0000624 foreach my $i (0 .. scalar @sort_order - 1) {
625 $curr_totals[$i] += $fn_CC->[$sort_order[$i]]
626 if (defined $fn_CC->[$sort_order[$i]]);
627 }
njn4f9c9342002-04-29 16:03:24 +0000628 }
629 print("\n");
630
631 return $threshold_files;
632}
633
634#-----------------------------------------------------------------------------
635# Annotate selected files
636#-----------------------------------------------------------------------------
637
638# Issue a warning that the source file is more recent than the input file.
639sub warning_on_src_more_recent_than_inputfile ($)
640{
641 my $src_file = $_[0];
642
643 my $warning = <<END
644@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
645@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
646@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
647@ Source file '$src_file' is more recent than input file '$input_file'.
648@ Annotations may not be correct.
649@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
650
651END
652;
653 print($warning);
654}
655
656# If there is information about lines not in the file, issue a warning
657# explaining possible causes.
658sub warning_on_nonexistent_lines ($$$)
659{
660 my ($src_more_recent_than_inputfile, $src_file, $excess_line_nums) = @_;
661 my $cause_and_solution;
662
663 if ($src_more_recent_than_inputfile) {
664 $cause_and_solution = <<END
665@@ cause: '$src_file' has changed since information was gathered.
666@@ If so, a warning will have already been issued about this.
667@@ solution: Recompile program and rerun under "valgrind --cachesim=yes" to
668@@ gather new information.
669END
670 # We suppress warnings about .h files
671 } elsif ($src_file =~ /\.h$/) {
672 $cause_and_solution = <<END
673@@ cause: bug in the Valgrind's debug info reader that screws up with .h
674@@ files sometimes
675@@ solution: none, sorry
676END
677 } else {
678 $cause_and_solution = <<END
679@@ cause: not sure, sorry
680END
681 }
682
683 my $warning = <<END
684@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
685@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
686@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
687@@
688@@ Information recorded about lines past the end of '$src_file'.
689@@
690@@ Probable cause and solution:
691$cause_and_solution@@
692@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
693END
694;
695 print($warning);
696}
697
698sub annotate_ann_files($)
699{
700 my ($threshold_files) = @_;
701
702 my %all_ann_files;
703 my @unfound_auto_annotate_files;
704 my $printed_totals_CC = [];
705
706 # If auto-annotating, add interesting files (but not "???")
707 if ($auto_annotate) {
708 delete $threshold_files->{"???"};
709 %all_ann_files = (%user_ann_files, %$threshold_files)
710 } else {
711 %all_ann_files = %user_ann_files;
712 }
713
714 # Track if we did any annotations.
715 my $did_annotations = 0;
716
717 LOOP:
718 foreach my $src_file (keys %all_ann_files) {
719
720 my $opened_file = "";
721 my $full_file_name = "";
722 foreach my $include_dir (@include_dirs) {
723 my $try_name = $include_dir . $src_file;
724 if (open(INPUTFILE, "< $try_name")) {
725 $opened_file = $try_name;
726 $full_file_name = ($include_dir eq ""
727 ? $src_file
728 : "$include_dir + $src_file");
729 last;
730 }
731 }
732
733 if (not $opened_file) {
734 # Failed to open the file. If chosen on the command line, die.
735 # If arose from auto-annotation, print a little message.
736 if (defined $user_ann_files{$src_file}) {
737 die("File $src_file not opened in any of: @include_dirs\n");
738
739 } else {
740 push(@unfound_auto_annotate_files, $src_file);
741 }
742
743 } else {
744 # File header (distinguish between user- and auto-selected files).
745 print("$fancy");
746 my $ann_type =
747 (defined $user_ann_files{$src_file} ? "User" : "Auto");
748 print("-- $ann_type-annotated source: $full_file_name\n");
749 print("$fancy");
750
751 # Get file's CCs
752 my $src_file_CCs = $all_ind_CCs{$src_file};
753 if (!defined $src_file_CCs) {
754 print(" No information has been collected for $src_file\n\n");
755 next LOOP;
756 }
757
758 $did_annotations = 1;
759
760 # Numeric, not lexicographic sort!
761 my @line_nums = sort {$a <=> $b} keys %$src_file_CCs;
762
763 # If $src_file more recent than cachegrind.out, issue warning
764 my $src_more_recent_than_inputfile = 0;
765 if ((stat $opened_file)[9] > (stat $input_file)[9]) {
766 $src_more_recent_than_inputfile = 1;
767 warning_on_src_more_recent_than_inputfile($src_file);
768 }
769
770 # Work out the size of each column for printing
771 my $CC_col_widths = compute_CC_col_widths(values %$src_file_CCs);
772
773 # Events header
774 print_events($CC_col_widths);
775 print("\n\n");
776
777 # Shift out 0 if it's in the line numbers (from unknown entries,
778 # likely due to bugs in Valgrind's stabs debug info reader)
779 shift(@line_nums) if (0 == $line_nums[0]);
780
781 # Finds interesting line ranges -- all lines with a CC, and all
782 # lines within $context lines of a line with a CC.
783 my $n = @line_nums;
784 my @pairs;
785 for (my $i = 0; $i < $n; $i++) {
786 push(@pairs, $line_nums[$i] - $context); # lower marker
787 while ($i < $n-1 &&
788 $line_nums[$i] + 2*$context >= $line_nums[$i+1]) {
789 $i++;
790 }
791 push(@pairs, $line_nums[$i] + $context); # upper marker
792 }
793
794 # Annotate chosen lines, tracking total counts of lines printed
795 $pairs[0] = 1 if ($pairs[0] < 1);
796 while (@pairs) {
797 my $low = shift @pairs;
798 my $high = shift @pairs;
799 while ($. < $low-1) {
800 my $tmp = <INPUTFILE>;
801 last unless (defined $tmp); # hack to detect EOF
802 }
803 my $src_line;
804 # Print line number, unless start of file
805 print("-- line $low " . '-' x 40 . "\n") if ($low != 1);
806 while (($. < $high) && ($src_line = <INPUTFILE>)) {
807 if (defined $line_nums[0] && $. == $line_nums[0]) {
808 print_CC($src_file_CCs->{$.}, $CC_col_widths);
809 add_array_a_to_b($src_file_CCs->{$.},
810 $printed_totals_CC);
811 shift(@line_nums);
812
813 } else {
814 print_CC( [], $CC_col_widths);
815 }
816
817 print(" $src_line");
818 }
819 # Print line number, unless EOF
820 if ($src_line) {
821 print("-- line $high " . '-' x 40 . "\n");
822 } else {
823 last;
824 }
825 }
826
827 # If there was info on lines past the end of the file...
828 if (@line_nums) {
829 foreach my $line_num (@line_nums) {
830 print_CC($src_file_CCs->{$line_num}, $CC_col_widths);
831 print(" <bogus line $line_num>\n");
832 }
833 print("\n");
834 warning_on_nonexistent_lines($src_more_recent_than_inputfile,
835 $src_file, \@line_nums);
836 }
837 print("\n");
838
839 # Print summary of counts attributed to file but not to any
840 # particular line (due to incomplete debug info).
841 if ($src_file_CCs->{0}) {
842 print_CC($src_file_CCs->{0}, $CC_col_widths);
843 print(" <counts for unidentified lines in $src_file>\n\n");
844 }
845
846 close(INPUTFILE);
847 }
848 }
849
850 # Print list of unfound auto-annotate selected files.
851 if (@unfound_auto_annotate_files) {
852 print("$fancy");
853 print("The following files chosen for auto-annotation could not be found:\n");
854 print($fancy);
855 foreach my $f (@unfound_auto_annotate_files) {
856 print(" $f\n");
857 }
858 print("\n");
859 }
860
861 # If we did any annotating, print what proportion of events were covered by
862 # annotated lines above.
863 if ($did_annotations) {
864 my $percent_printed_CC;
865 foreach (my $i = 0; $i < @$summary_CC; $i++) {
866 $percent_printed_CC->[$i] =
867 sprintf("%.0f",
868 $printed_totals_CC->[$i] / $summary_CC->[$i] * 100);
869 }
870 my $pp_CC_col_widths = compute_CC_col_widths($percent_printed_CC);
871 print($fancy);
872 print_events($pp_CC_col_widths);
873 print("\n");
874 print($fancy);
875 print_CC($percent_printed_CC, $pp_CC_col_widths);
876 print(" percentage of events annotated\n\n");
877 }
878}
879
880#----------------------------------------------------------------------------
881# "main()"
882#----------------------------------------------------------------------------
883process_cmd_line();
884read_input_file();
885print_options();
886my $threshold_files = print_summary_and_fn_totals();
887annotate_ann_files($threshold_files);
888
njn7cf0bd32002-06-08 13:36:03 +0000889##--------------------------------------------------------------------##
890##--- end vg_annotate.in ---##
891##--------------------------------------------------------------------##
892
893