]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Allow negative numbers in the cachegrind.out.* file. Also protect against
authorNicholas Nethercote <njn@valgrind.org>
Tue, 22 Jun 2010 03:40:04 +0000 (03:40 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Tue, 22 Jun 2010 03:40:04 +0000 (03:40 +0000)
division-by-zero.  Both are required for cg_diff (not yet committed) to work
properly.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@11184

cachegrind/cg_annotate.in

index 9d27fd5bea72de394bf4645125267bff3cf1bf04..e76c94a452d6862cbd8633d452187c77349900ae 100644 (file)
@@ -168,6 +168,12 @@ END
 # Used in various places of output.
 my $fancy = '-' x 80 . "\n";
 
+sub safe_div($$)
+{
+    my ($x, $y) = @_;
+    return ($y == 0 ? 0 : $x / $y);
+}
+
 #-----------------------------------------------------------------------------
 # Argument and option handling
 #-----------------------------------------------------------------------------
@@ -385,7 +391,7 @@ sub read_input_file()
     # Read body of input file.
     while (<INPUTFILE>) {
         s/#.*$//;   # remove comments
-        if (s/^(\d+)\s+//) {
+        if (s/^(-?\d+)\s+//) {
             my $lineNum = $1;
             my $CC = line_to_CC($_);
             defined($currFuncCC) || die;
@@ -494,7 +500,7 @@ sub mycmp ($$)
         $x = -1 unless defined $x;
         $y = -1 unless defined $y;
 
-        my $cmp = $y <=> $x;        # reverse sort
+        my $cmp = abs($y) <=> abs($x);      # reverse sort of absolute size
         if (0 != $cmp) {
             return $cmp;
         }
@@ -505,7 +511,7 @@ sub mycmp ($$)
 
 sub commify ($) {
     my ($val) = @_;
-    1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
+    1 while ($val =~ s/^(-?\d+)(\d{3})/$1,$2/);
     return $val;
 }
 
@@ -614,7 +620,8 @@ sub print_summary_and_fn_totals ()
         # Stop when we've reached all the thresholds
         my $reached_all_thresholds = 1;
         foreach my $i (0 .. scalar @thresholds - 1) {
-            my $prop = $curr_totals[$i] * 100 / $summary_CC->[$sort_order[$i]];
+            my $prop = safe_div(abs($curr_totals[$i] * 100),
+                                abs($summary_CC->[$sort_order[$i]]));
             $reached_all_thresholds &&= ($prop >= $thresholds[$i]);
         }
         last if $reached_all_thresholds;
@@ -874,7 +881,8 @@ sub annotate_ann_files($)
         foreach (my $i = 0; $i < @$summary_CC; $i++) {
             $percent_printed_CC->[$i] = 
                 sprintf("%.0f", 
-                        $printed_totals_CC->[$i] / $summary_CC->[$i] * 100);
+                        safe_div(abs($printed_totals_CC->[$i]),
+                                 abs($summary_CC->[$i] * 100)));
         }
         my $pp_CC_col_widths = compute_CC_col_widths($percent_printed_CC);
         print($fancy);