perf event: Prevent unbound event__name array access
authorThomas Gleixner <tglx@linutronix.de>
Tue, 7 Dec 2010 12:48:42 +0000 (12:48 +0000)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 9 Dec 2010 13:15:07 +0000 (11:15 -0200)
event__name[] is missing an entry for PERF_RECORD_FINISHED_ROUND, but we
happily access the array from the dump code.

Make event__name[] static and provide an accessor function, fix up all
callers and add the missing string.

Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ian Munsie <imunsie@au1.ibm.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Peter Zijlstra <peterz@infradead.org>
LKML-Reference: <20101207124550.432593943@linutronix.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/event.c
tools/perf/util/event.h
tools/perf/util/hist.c
tools/perf/util/session.c

index e4cdc1ebe0fbbbb120fd74195c93f55863b99645..183aedd4db8383041a84b21ad981ec39cdfe5c4d 100644 (file)
@@ -7,7 +7,7 @@
 #include "strlist.h"
 #include "thread.h"
 
-const char *event__name[] = {
+static const char *event__name[] = {
        [0]                      = "TOTAL",
        [PERF_RECORD_MMAP]       = "MMAP",
        [PERF_RECORD_LOST]       = "LOST",
@@ -22,8 +22,18 @@ const char *event__name[] = {
        [PERF_RECORD_HEADER_EVENT_TYPE]  = "EVENT_TYPE",
        [PERF_RECORD_HEADER_TRACING_DATA]        = "TRACING_DATA",
        [PERF_RECORD_HEADER_BUILD_ID]    = "BUILD_ID",
+       [PERF_RECORD_FINISHED_ROUND]     = "FINISHED_ROUND",
 };
 
+const char *event__get_event_name(unsigned int id)
+{
+       if (id >= ARRAY_SIZE(event__name))
+               return "INVALID";
+       if (!event__name[id])
+               return "UNKNOWN";
+       return event__name[id];
+}
+
 static struct sample_data synth_sample = {
        .pid       = -1,
        .tid       = -1,
index a95ab18575cea0eda51bba5ba7e4a56b06d374e4..4716a8f6d3eb8f2ca35f8ee4463c9f6f36f1ce4b 100644 (file)
@@ -171,6 +171,6 @@ int event__preprocess_sample(const event_t *self, struct perf_session *session,
 int event__parse_sample(const event_t *event, struct perf_session *session,
                        struct sample_data *sample);
 
-extern const char *event__name[];
+const char *event__get_event_name(unsigned int id);
 
 #endif /* __PERF_RECORD_H */
index 2022e87409942ca4b0d133c3f889e41178a663d1..a3b84160c42eb0ff56fa258ce29f0b26d9290c5d 100644 (file)
@@ -1168,10 +1168,13 @@ size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
        size_t ret = 0;
 
        for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
-               if (!event__name[i])
+               const char *name = event__get_event_name(i);
+
+               if (!strcmp(name, "UNKNOWN"))
                        continue;
-               ret += fprintf(fp, "%10s events: %10d\n",
-                              event__name[i], self->stats.nr_events[i]);
+
+               ret += fprintf(fp, "%16s events: %10d\n", name,
+                              self->stats.nr_events[i]);
        }
 
        return ret;
index 3074d38897e6a4ad197e948fc6d55c5acaa017e6..b3b145a8ff7c4de8687ea260519b82e780cd0583 100644 (file)
@@ -718,7 +718,7 @@ static int perf_session__process_event(struct perf_session *session,
        if (event->header.type < PERF_RECORD_HEADER_MAX) {
                dump_printf("%#Lx [%#x]: PERF_RECORD_%s",
                            file_offset, event->header.size,
-                           event__name[event->header.type]);
+                           event__get_event_name(event->header.type));
                hists__inc_nr_events(&session->hists, event->header.type);
        }