Skip to content

Commit f1617b4

Browse files
acmelIngo Molnar
authored andcommitted
perf symbols: Record the build_ids of kernel modules too
[root@doppio linux-2.6-tip]# perf record -a sleep 2s;perf buildid-list|tail [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.162 MB perf.data (~7078 samples) ] 881588fa57b3c1696bc91e5e804a11304f093535 [cfg80211] 4d47ce1da9d16bad00c962c072451b7c681e82df [snd_page_alloc] 5146377e89a7caac617f9782f1a02e46263d3a31 [rfkill] 2153b937bff0d345fea83b63a2e1d3138569f83d [i915] 4e6fb1bb97362e3ee4d306988b9ad6912d5fb9ae [drm_kms_helper] f56ef2bf853e3a798f0d8d51f797622e5dc4420e [drm] b0d157a3b5c4e017329ffc07c64623cd6ad65e95 [i2c_algo_bit] 8125374b905ef9fa8b65d98e166b008ad952f198 [i2c_core] fc875c6e5a90e7b915e9d445d0efc859e1b2678c [video] 4b43c5006589f977e9762fdfc7ac1a92b72fca52 [output] [root@doppio linux-2.6-tip]# elfutils libdwfl/linux-kernel-modules.c was used as reference, as suggested by Roland McGrath. Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Roland McGrath <roland@redhat.com> Cc: Frédéric Weisbecker <fweisbec@gmail.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Paul Mackerras <paulus@samba.org> LKML-Reference: <1258582853-8579-3-git-send-email-acme@infradead.org> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent e30a3d1 commit f1617b4

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎tools/perf/util/header.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ perf_header__adds_write(struct perf_header *self, int fd)
241241

242242
buildid_sec = &feat_sec[idx++];
243243

244+
/*
245+
* Read the list of loaded modules with its build_ids
246+
*/
247+
dsos__load_modules();
248+
244249
/* Write build-ids */
245250
buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
246251
if (dsos__write_buildid_table(fd) < 0)

‎tools/perf/util/symbol.c‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <libelf.h>
1010
#include <gelf.h>
1111
#include <elf.h>
12+
#include <limits.h>
1213
#include <sys/utsname.h>
1314

1415
enum dso_origin {
@@ -943,6 +944,50 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
943944
return err;
944945
}
945946

947+
int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
948+
{
949+
int fd, err = -1;
950+
951+
if (size < BUILD_ID_SIZE)
952+
goto out;
953+
954+
fd = open(filename, O_RDONLY);
955+
if (fd < 0)
956+
goto out;
957+
958+
while (1) {
959+
char bf[BUFSIZ];
960+
GElf_Nhdr nhdr;
961+
int namesz, descsz;
962+
963+
if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
964+
break;
965+
966+
namesz = (nhdr.n_namesz + 3) & -4U;
967+
descsz = (nhdr.n_descsz + 3) & -4U;
968+
if (nhdr.n_type == NT_GNU_BUILD_ID &&
969+
nhdr.n_namesz == sizeof("GNU")) {
970+
if (read(fd, bf, namesz) != namesz)
971+
break;
972+
if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
973+
if (read(fd, build_id,
974+
BUILD_ID_SIZE) == BUILD_ID_SIZE) {
975+
err = 0;
976+
break;
977+
}
978+
} else if (read(fd, bf, descsz) != descsz)
979+
break;
980+
} else {
981+
int n = namesz + descsz;
982+
if (read(fd, bf, n) != n)
983+
break;
984+
}
985+
}
986+
close(fd);
987+
out:
988+
return err;
989+
}
990+
946991
char dso__symtab_origin(const struct dso *self)
947992
{
948993
static const char origin[] = {
@@ -1218,7 +1263,7 @@ static struct map *map__new2(u64 start, struct dso *dso)
12181263
return self;
12191264
}
12201265

1221-
static int dsos__load_modules(void)
1266+
int dsos__load_modules(void)
12221267
{
12231268
char *line = NULL;
12241269
size_t n;
@@ -1268,6 +1313,12 @@ static int dsos__load_modules(void)
12681313
goto out_delete_line;
12691314
}
12701315

1316+
snprintf(name, sizeof(name),
1317+
"/sys/module/%s/notes/.note.gnu.build-id", line);
1318+
if (sysfs__read_build_id(name, dso->build_id,
1319+
sizeof(dso->build_id)) == 0)
1320+
dso->has_build_id = true;
1321+
12711322
dso->origin = DSO__ORIG_KMODULE;
12721323
kernel_maps__insert(map);
12731324
dsos__add(dso);

‎tools/perf/util/symbol.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ void dso__delete(struct dso *self);
7878
struct symbol *dso__find_symbol(struct dso *self, u64 ip);
7979

8080
int dsos__load_kernel(const char *vmlinux, symbol_filter_t filter, int modules);
81+
int dsos__load_modules(void);
8182
struct dso *dsos__findnew(const char *name);
8283
int dso__load(struct dso *self, struct map *map, symbol_filter_t filter);
8384
void dsos__fprintf(FILE *fp);
@@ -89,6 +90,7 @@ char dso__symtab_origin(const struct dso *self);
8990
void dso__set_build_id(struct dso *self, void *build_id);
9091

9192
int filename__read_build_id(const char *filename, void *bf, size_t size);
93+
int sysfs__read_build_id(const char *filename, void *bf, size_t size);
9294
bool dsos__read_build_ids(void);
9395
int build_id__sprintf(u8 *self, int len, char *bf);
9496

0 commit comments

Comments
 (0)