perf tools: Store build id when available in PERF_RECORD_MMAP2 metadata events
authorJiri Olsa <jolsa@kernel.org>
Mon, 14 Dec 2020 10:54:49 +0000 (11:54 +0100)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 28 Dec 2020 13:01:55 +0000 (10:01 -0300)
When processing a PERF_RECORD_MMAP2 metadata event, check on the build
id misc bit: PERF_RECORD_MISC_MMAP_BUILD_ID and if it is set, store the
build id in mmap's dso object.

Also adding the build id data to struct perf_record_mmap2 event
definition.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexei Budankov <abudankov@huawei.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <songliubraving@fb.com>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/20201214105457.543111-8-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/perf/include/perf/event.h
tools/perf/util/machine.c
tools/perf/util/map.c
tools/perf/util/map.h

index 988c539bedb6e4b607d4e95ae2adf6f71bb17b00..d82054225fcc0ee16e08329e14bbdf089e961802 100644 (file)
@@ -23,10 +23,20 @@ struct perf_record_mmap2 {
        __u64                    start;
        __u64                    len;
        __u64                    pgoff;
-       __u32                    maj;
-       __u32                    min;
-       __u64                    ino;
-       __u64                    ino_generation;
+       union {
+               struct {
+                       __u32    maj;
+                       __u32    min;
+                       __u64    ino;
+                       __u64    ino_generation;
+               };
+               struct {
+                       __u8     build_id_size;
+                       __u8     __reserved_1;
+                       __u16    __reserved_2;
+                       __u8     build_id[20];
+               };
+       };
        __u32                    prot;
        __u32                    flags;
        char                     filename[PATH_MAX];
index f841f3503cae61de55ce1b8dbe912b7b0f8e2f53..522ea3236bcc57dcd8f2d443b410f792d8b779ef 100644 (file)
@@ -1599,7 +1599,8 @@ static int machine__process_extra_kernel_map(struct machine *machine,
 }
 
 static int machine__process_kernel_mmap_event(struct machine *machine,
-                                             struct extra_kernel_map *xm)
+                                             struct extra_kernel_map *xm,
+                                             struct build_id *bid)
 {
        struct map *map;
        enum dso_space_type dso_space;
@@ -1624,6 +1625,10 @@ static int machine__process_kernel_mmap_event(struct machine *machine,
                        goto out_problem;
 
                map->end = map->start + xm->end - xm->start;
+
+               if (build_id__is_defined(bid))
+                       dso__set_build_id(map->dso, bid);
+
        } else if (is_kernel_mmap) {
                const char *symbol_name = (xm->name + strlen(machine->mmap_name));
                /*
@@ -1681,6 +1686,9 @@ static int machine__process_kernel_mmap_event(struct machine *machine,
 
                machine__update_kernel_mmap(machine, xm->start, xm->end);
 
+               if (build_id__is_defined(bid))
+                       dso__set_build_id(kernel, bid);
+
                /*
                 * Avoid using a zero address (kptr_restrict) for the ref reloc
                 * symbol. Effectively having zero here means that at record
@@ -1718,11 +1726,17 @@ int machine__process_mmap2_event(struct machine *machine,
                .ino = event->mmap2.ino,
                .ino_generation = event->mmap2.ino_generation,
        };
+       struct build_id __bid, *bid = NULL;
        int ret = 0;
 
        if (dump_trace)
                perf_event__fprintf_mmap2(event, stdout);
 
+       if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
+               bid = &__bid;
+               build_id__init(bid, event->mmap2.build_id, event->mmap2.build_id_size);
+       }
+
        if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
            sample->cpumode == PERF_RECORD_MISC_KERNEL) {
                struct extra_kernel_map xm = {
@@ -1732,7 +1746,7 @@ int machine__process_mmap2_event(struct machine *machine,
                };
 
                strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN);
-               ret = machine__process_kernel_mmap_event(machine, &xm);
+               ret = machine__process_kernel_mmap_event(machine, &xm, bid);
                if (ret < 0)
                        goto out_problem;
                return 0;
@@ -1746,7 +1760,7 @@ int machine__process_mmap2_event(struct machine *machine,
        map = map__new(machine, event->mmap2.start,
                        event->mmap2.len, event->mmap2.pgoff,
                        &dso_id, event->mmap2.prot,
-                       event->mmap2.flags,
+                       event->mmap2.flags, bid,
                        event->mmap2.filename, thread);
 
        if (map == NULL)
@@ -1789,7 +1803,7 @@ int machine__process_mmap_event(struct machine *machine, union perf_event *event
                };
 
                strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
-               ret = machine__process_kernel_mmap_event(machine, &xm);
+               ret = machine__process_kernel_mmap_event(machine, &xm, NULL);
                if (ret < 0)
                        goto out_problem;
                return 0;
@@ -1805,7 +1819,7 @@ int machine__process_mmap_event(struct machine *machine, union perf_event *event
 
        map = map__new(machine, event->mmap.start,
                        event->mmap.len, event->mmap.pgoff,
-                       NULL, prot, 0, event->mmap.filename, thread);
+                       NULL, prot, 0, NULL, event->mmap.filename, thread);
 
        if (map == NULL)
                goto out_problem_map;
index f44ede437dc7f5a2bfe28893e2b09f456091533b..692e56dc832e78b0079b18e6bd5c1fd7b1753246 100644 (file)
@@ -130,8 +130,8 @@ void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
 
 struct map *map__new(struct machine *machine, u64 start, u64 len,
                     u64 pgoff, struct dso_id *id,
-                    u32 prot, u32 flags, char *filename,
-                    struct thread *thread)
+                    u32 prot, u32 flags, struct build_id *bid,
+                    char *filename, struct thread *thread)
 {
        struct map *map = malloc(sizeof(*map));
        struct nsinfo *nsi = NULL;
@@ -194,6 +194,10 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
                                dso__set_loaded(dso);
                }
                dso->nsinfo = nsi;
+
+               if (build_id__is_defined(bid))
+                       dso__set_build_id(dso, bid);
+
                dso__put(dso);
        }
        return map;
index b1c0686db1b7fc9b95d5b259eeadca2ccc7a86a4..9f32825c98d8471268c6cdba279552143905b9fb 100644 (file)
@@ -104,10 +104,11 @@ void map__init(struct map *map,
               u64 start, u64 end, u64 pgoff, struct dso *dso);
 
 struct dso_id;
+struct build_id;
 
 struct map *map__new(struct machine *machine, u64 start, u64 len,
                     u64 pgoff, struct dso_id *id, u32 prot, u32 flags,
-                    char *filename, struct thread *thread);
+                    struct build_id *bid, char *filename, struct thread *thread);
 struct map *map__new2(u64 start, struct dso *dso);
 void map__delete(struct map *map);
 struct map *map__clone(struct map *map);