perf tools: Fix report -F mispredict for data without branch info
authorJiri Olsa <jolsa@kernel.org>
Thu, 16 Oct 2014 14:07:03 +0000 (16:07 +0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 29 Oct 2014 12:28:20 +0000 (10:28 -0200)
The branch field sorting code assumes hist_entry::branch_info is
allocated, which is wrong and following perf session ends up with report
segfault.

  $ perf record ls
  $ perf report -F mispredict
  perf: Segmentation fault

Checking that hist_entry::branch_info is valid and display "N/A" string
in snprint callback if it's not.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1413468427-31049-4-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/sort.c

index 9bcdb57..0c68af8 100644 (file)
@@ -471,11 +471,13 @@ struct sort_entry sort_sym_to = {
 static int64_t
 sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
 {
-       const unsigned char mp = left->branch_info->flags.mispred !=
-                                       right->branch_info->flags.mispred;
-       const unsigned char p = left->branch_info->flags.predicted !=
-                                       right->branch_info->flags.predicted;
+       unsigned char mp, p;
 
+       if (!left->branch_info || !right->branch_info)
+               return cmp_null(left->branch_info, right->branch_info);
+
+       mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
+       p  = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
        return mp || p;
 }
 
@@ -483,10 +485,12 @@ static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
                                    size_t size, unsigned int width){
        static const char *out = "N/A";
 
-       if (he->branch_info->flags.predicted)
-               out = "N";
-       else if (he->branch_info->flags.mispred)
-               out = "Y";
+       if (he->branch_info) {
+               if (he->branch_info->flags.predicted)
+                       out = "N";
+               else if (he->branch_info->flags.mispred)
+                       out = "Y";
+       }
 
        return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
 }