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