perf tools: Pass build_id object to filename__read_build_id()

Pass a build_id object to filename__read_build_id function, so it can
populate the size of the build_id object.

Changing filename__read_build_id() code for both ELF/non-ELF code.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20201013192441.1299447-3-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Jiri Olsa 2020-10-13 21:24:34 +02:00 committed by Arnaldo Carvalho de Melo
parent 0aba7f036a
commit f766819cd5
11 changed files with 60 additions and 57 deletions

View File

@ -84,7 +84,7 @@ static int add_dso(const char *fpath, const struct stat *sb __maybe_unused,
if (typeflag == FTW_D || typeflag == FTW_SL) if (typeflag == FTW_D || typeflag == FTW_SL)
return 0; return 0;
if (filename__read_build_id(fpath, bid.data, sizeof(bid.data)) < 0) if (filename__read_build_id(fpath, &bid) < 0)
return 0; return 0;
dso->name = realpath(fpath, NULL); dso->name = realpath(fpath, NULL);

View File

@ -174,19 +174,19 @@ static int build_id_cache__add_kcore(const char *filename, bool force)
static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi) static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
{ {
char sbuild_id[SBUILD_ID_SIZE]; char sbuild_id[SBUILD_ID_SIZE];
u8 build_id[BUILD_ID_SIZE]; struct build_id bid;
int err; int err;
struct nscookie nsc; struct nscookie nsc;
nsinfo__mountns_enter(nsi, &nsc); nsinfo__mountns_enter(nsi, &nsc);
err = filename__read_build_id(filename, &build_id, sizeof(build_id)); err = filename__read_build_id(filename, &bid);
nsinfo__mountns_exit(&nsc); nsinfo__mountns_exit(&nsc);
if (err < 0) { if (err < 0) {
pr_debug("Couldn't read a build-id in %s\n", filename); pr_debug("Couldn't read a build-id in %s\n", filename);
return -1; return -1;
} }
build_id__sprintf(build_id, sizeof(build_id), sbuild_id); build_id__sprintf(bid.data, sizeof(bid.data), sbuild_id);
err = build_id_cache__add_s(sbuild_id, filename, nsi, err = build_id_cache__add_s(sbuild_id, filename, nsi,
false, false); false, false);
pr_debug("Adding %s %s: %s\n", sbuild_id, filename, pr_debug("Adding %s %s: %s\n", sbuild_id, filename,
@ -196,21 +196,21 @@ static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
static int build_id_cache__remove_file(const char *filename, struct nsinfo *nsi) static int build_id_cache__remove_file(const char *filename, struct nsinfo *nsi)
{ {
u8 build_id[BUILD_ID_SIZE];
char sbuild_id[SBUILD_ID_SIZE]; char sbuild_id[SBUILD_ID_SIZE];
struct build_id bid;
struct nscookie nsc; struct nscookie nsc;
int err; int err;
nsinfo__mountns_enter(nsi, &nsc); nsinfo__mountns_enter(nsi, &nsc);
err = filename__read_build_id(filename, &build_id, sizeof(build_id)); err = filename__read_build_id(filename, &bid);
nsinfo__mountns_exit(&nsc); nsinfo__mountns_exit(&nsc);
if (err < 0) { if (err < 0) {
pr_debug("Couldn't read a build-id in %s\n", filename); pr_debug("Couldn't read a build-id in %s\n", filename);
return -1; return -1;
} }
build_id__sprintf(build_id, sizeof(build_id), sbuild_id); build_id__sprintf(bid.data, sizeof(bid.data), sbuild_id);
err = build_id_cache__remove_s(sbuild_id); err = build_id_cache__remove_s(sbuild_id);
pr_debug("Removing %s %s: %s\n", sbuild_id, filename, pr_debug("Removing %s %s: %s\n", sbuild_id, filename,
err ? "FAIL" : "Ok"); err ? "FAIL" : "Ok");
@ -274,17 +274,16 @@ static int build_id_cache__purge_all(void)
static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused) static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
{ {
char filename[PATH_MAX]; char filename[PATH_MAX];
u8 build_id[BUILD_ID_SIZE]; struct build_id bid;
if (dso__build_id_filename(dso, filename, sizeof(filename), false) && if (dso__build_id_filename(dso, filename, sizeof(filename), false) &&
filename__read_build_id(filename, build_id, filename__read_build_id(filename, &bid) == -1) {
sizeof(build_id)) != sizeof(build_id)) {
if (errno == ENOENT) if (errno == ENOENT)
return false; return false;
pr_warning("Problems with %s file, consider removing it from the cache\n", pr_warning("Problems with %s file, consider removing it from the cache\n",
filename); filename);
} else if (memcmp(dso->bid.data, build_id, sizeof(dso->bid.data))) { } else if (memcmp(dso->bid.data, bid.data, bid.size)) {
pr_warning("Problems with %s file, consider removing it from the cache\n", pr_warning("Problems with %s file, consider removing it from the cache\n",
filename); filename);
} }
@ -300,14 +299,14 @@ static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *f
static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi) static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
{ {
u8 build_id[BUILD_ID_SIZE];
char sbuild_id[SBUILD_ID_SIZE]; char sbuild_id[SBUILD_ID_SIZE];
struct build_id bid;
struct nscookie nsc; struct nscookie nsc;
int err; int err;
nsinfo__mountns_enter(nsi, &nsc); nsinfo__mountns_enter(nsi, &nsc);
err = filename__read_build_id(filename, &build_id, sizeof(build_id)); err = filename__read_build_id(filename, &bid);
nsinfo__mountns_exit(&nsc); nsinfo__mountns_exit(&nsc);
if (err < 0) { if (err < 0) {
pr_debug("Couldn't read a build-id in %s\n", filename); pr_debug("Couldn't read a build-id in %s\n", filename);
@ -315,7 +314,7 @@ static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
} }
err = 0; err = 0;
build_id__sprintf(build_id, sizeof(build_id), sbuild_id); build_id__sprintf(bid.data, sizeof(bid.data), sbuild_id);
if (build_id_cache__cached(sbuild_id)) if (build_id_cache__cached(sbuild_id))
err = build_id_cache__remove_s(sbuild_id); err = build_id_cache__remove_s(sbuild_id);

View File

@ -522,10 +522,8 @@ static int dso__read_build_id(struct dso *dso)
return 0; return 0;
nsinfo__mountns_enter(dso->nsinfo, &nsc); nsinfo__mountns_enter(dso->nsinfo, &nsc);
if (filename__read_build_id(dso->long_name, dso->bid.data, if (filename__read_build_id(dso->long_name, &dso->bid) > 0)
sizeof(dso->bid.data)) > 0) {
dso->has_build_id = true; dso->has_build_id = true;
}
nsinfo__mountns_exit(&nsc); nsinfo__mountns_exit(&nsc);
return dso->has_build_id ? 0 : -1; return dso->has_build_id ? 0 : -1;

View File

@ -24,7 +24,7 @@ static int run_dir(const char *d)
{ {
char filename[PATH_MAX]; char filename[PATH_MAX];
char debugfile[PATH_MAX]; char debugfile[PATH_MAX];
char build_id[BUILD_ID_SIZE]; struct build_id bid;
char debuglink[PATH_MAX]; char debuglink[PATH_MAX];
char expect_build_id[] = { char expect_build_id[] = {
0x5a, 0x0f, 0xd8, 0x82, 0xb5, 0x30, 0x84, 0x22, 0x5a, 0x0f, 0xd8, 0x82, 0xb5, 0x30, 0x84, 0x22,
@ -36,10 +36,10 @@ static int run_dir(const char *d)
int ret; int ret;
scnprintf(filename, PATH_MAX, "%s/pe-file.exe", d); scnprintf(filename, PATH_MAX, "%s/pe-file.exe", d);
ret = filename__read_build_id(filename, build_id, BUILD_ID_SIZE); ret = filename__read_build_id(filename, &bid);
TEST_ASSERT_VAL("Failed to read build_id", TEST_ASSERT_VAL("Failed to read build_id",
ret == sizeof(expect_build_id)); ret == sizeof(expect_build_id));
TEST_ASSERT_VAL("Wrong build_id", !memcmp(build_id, expect_build_id, TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
sizeof(expect_build_id))); sizeof(expect_build_id)));
ret = filename__read_debuglink(filename, debuglink, PATH_MAX); ret = filename__read_debuglink(filename, debuglink, PATH_MAX);
@ -48,10 +48,10 @@ static int run_dir(const char *d)
!strcmp(debuglink, expect_debuglink)); !strcmp(debuglink, expect_debuglink));
scnprintf(debugfile, PATH_MAX, "%s/%s", d, debuglink); scnprintf(debugfile, PATH_MAX, "%s/%s", d, debuglink);
ret = filename__read_build_id(debugfile, build_id, BUILD_ID_SIZE); ret = filename__read_build_id(debugfile, &bid);
TEST_ASSERT_VAL("Failed to read debug file build_id", TEST_ASSERT_VAL("Failed to read debug file build_id",
ret == sizeof(expect_build_id)); ret == sizeof(expect_build_id));
TEST_ASSERT_VAL("Wrong build_id", !memcmp(build_id, expect_build_id, TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
sizeof(expect_build_id))); sizeof(expect_build_id)));
dso = dso__new(filename); dso = dso__new(filename);

View File

@ -28,16 +28,16 @@ static int target_function(void)
static int build_id_cache__add_file(const char *filename) static int build_id_cache__add_file(const char *filename)
{ {
char sbuild_id[SBUILD_ID_SIZE]; char sbuild_id[SBUILD_ID_SIZE];
u8 build_id[BUILD_ID_SIZE]; struct build_id bid;
int err; int err;
err = filename__read_build_id(filename, &build_id, sizeof(build_id)); err = filename__read_build_id(filename, &bid);
if (err < 0) { if (err < 0) {
pr_debug("Failed to read build id of %s\n", filename); pr_debug("Failed to read build id of %s\n", filename);
return err; return err;
} }
build_id__sprintf(build_id, sizeof(build_id), sbuild_id); build_id__sprintf(bid.data, sizeof(bid.data), sbuild_id);
err = build_id_cache__add_s(sbuild_id, filename, NULL, false, false); err = build_id_cache__add_s(sbuild_id, filename, NULL, false, false);
if (err < 0) if (err < 0)
pr_debug("Failed to add build id cache of %s\n", filename); pr_debug("Failed to add build id cache of %s\n", filename);

View File

@ -130,16 +130,14 @@ int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
int filename__sprintf_build_id(const char *pathname, char *sbuild_id) int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
{ {
u8 build_id[BUILD_ID_SIZE]; struct build_id bid;
int ret; int ret;
ret = filename__read_build_id(pathname, build_id, sizeof(build_id)); ret = filename__read_build_id(pathname, &bid);
if (ret < 0) if (ret < 0)
return ret; return ret;
else if (ret != sizeof(build_id))
return -EINVAL;
return build_id__sprintf(build_id, sizeof(build_id), sbuild_id); return build_id__sprintf(bid.data, sizeof(bid.data), sbuild_id);
} }
/* asnprintf consolidates asprintf and snprintf */ /* asnprintf consolidates asprintf and snprintf */

View File

@ -73,8 +73,7 @@ bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
continue; continue;
} }
nsinfo__mountns_enter(pos->nsinfo, &nsc); nsinfo__mountns_enter(pos->nsinfo, &nsc);
if (filename__read_build_id(pos->long_name, pos->bid.data, if (filename__read_build_id(pos->long_name, &pos->bid) > 0) {
sizeof(pos->bid.data)) > 0) {
have_build_id = true; have_build_id = true;
pos->has_build_id = true; pos->has_build_id = true;
} }

View File

@ -534,8 +534,9 @@ static int elf_read_build_id(Elf *elf, void *bf, size_t size)
#ifdef HAVE_LIBBFD_BUILDID_SUPPORT #ifdef HAVE_LIBBFD_BUILDID_SUPPORT
int filename__read_build_id(const char *filename, void *bf, size_t size) int filename__read_build_id(const char *filename, struct build_id *bid)
{ {
size_t size = sizeof(bid->data);
int err = -1; int err = -1;
bfd *abfd; bfd *abfd;
@ -551,9 +552,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
if (!abfd->build_id || abfd->build_id->size > size) if (!abfd->build_id || abfd->build_id->size > size)
goto out_close; goto out_close;
memcpy(bf, abfd->build_id->data, abfd->build_id->size); memcpy(bid->data, abfd->build_id->data, abfd->build_id->size);
memset(bf + abfd->build_id->size, 0, size - abfd->build_id->size); memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size);
err = abfd->build_id->size; err = bid->size = abfd->build_id->size;
out_close: out_close:
bfd_close(abfd); bfd_close(abfd);
@ -562,8 +563,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
#else // HAVE_LIBBFD_BUILDID_SUPPORT #else // HAVE_LIBBFD_BUILDID_SUPPORT
int filename__read_build_id(const char *filename, void *bf, size_t size) int filename__read_build_id(const char *filename, struct build_id *bid)
{ {
size_t size = sizeof(bid->data);
int fd, err = -1; int fd, err = -1;
Elf *elf; Elf *elf;
@ -580,7 +582,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
goto out_close; goto out_close;
} }
err = elf_read_build_id(elf, bf, size); err = elf_read_build_id(elf, bid->data, size);
if (err > 0)
bid->size = err;
elf_end(elf); elf_end(elf);
out_close: out_close:

View File

@ -31,9 +31,10 @@ static bool check_need_swap(int file_endian)
#define NT_GNU_BUILD_ID 3 #define NT_GNU_BUILD_ID 3
static int read_build_id(void *note_data, size_t note_len, void *bf, static int read_build_id(void *note_data, size_t note_len, struct build_id *bid,
size_t size, bool need_swap) bool need_swap)
{ {
size_t size = sizeof(bid->data);
struct { struct {
u32 n_namesz; u32 n_namesz;
u32 n_descsz; u32 n_descsz;
@ -63,8 +64,9 @@ static int read_build_id(void *note_data, size_t note_len, void *bf,
nhdr->n_namesz == sizeof("GNU")) { nhdr->n_namesz == sizeof("GNU")) {
if (memcmp(name, "GNU", sizeof("GNU")) == 0) { if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
size_t sz = min(size, descsz); size_t sz = min(size, descsz);
memcpy(bf, ptr, sz); memcpy(bid->data, ptr, sz);
memset(bf + sz, 0, size - sz); memset(bid->data + sz, 0, size - sz);
bid->size = sz;
return 0; return 0;
} }
} }
@ -84,7 +86,7 @@ int filename__read_debuglink(const char *filename __maybe_unused,
/* /*
* Just try PT_NOTE header otherwise fails * Just try PT_NOTE header otherwise fails
*/ */
int filename__read_build_id(const char *filename, void *bf, size_t size) int filename__read_build_id(const char *filename, struct build_id *bid)
{ {
FILE *fp; FILE *fp;
int ret = -1; int ret = -1;
@ -156,9 +158,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
if (fread(buf, buf_size, 1, fp) != 1) if (fread(buf, buf_size, 1, fp) != 1)
goto out_free; goto out_free;
ret = read_build_id(buf, buf_size, bf, size, need_swap); ret = read_build_id(buf, buf_size, bid, need_swap);
if (ret == 0) if (ret == 0)
ret = size; ret = bid->size;
break; break;
} }
} else { } else {
@ -207,9 +209,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
if (fread(buf, buf_size, 1, fp) != 1) if (fread(buf, buf_size, 1, fp) != 1)
goto out_free; goto out_free;
ret = read_build_id(buf, buf_size, bf, size, need_swap); ret = read_build_id(buf, buf_size, bid, need_swap);
if (ret == 0) if (ret == 0)
ret = size; ret = bid->size;
break; break;
} }
} }
@ -220,8 +222,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
return ret; return ret;
} }
int sysfs__read_build_id(const char *filename, void *build_id, size_t size) int sysfs__read_build_id(const char *filename, void *build_id, size_t size __maybe_unused)
{ {
struct build_id bid;
int fd; int fd;
int ret = -1; int ret = -1;
struct stat stbuf; struct stat stbuf;
@ -243,7 +246,9 @@ int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
if (read(fd, buf, buf_size) != (ssize_t) buf_size) if (read(fd, buf, buf_size) != (ssize_t) buf_size)
goto out_free; goto out_free;
ret = read_build_id(buf, buf_size, build_id, size, false); ret = read_build_id(buf, buf_size, &bid, false);
if (ret > 0)
memcpy(build_id, bid.data, bid.size);
out_free: out_free:
free(buf); free(buf);
out: out:
@ -339,16 +344,15 @@ int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
struct symsrc *runtime_ss __maybe_unused, struct symsrc *runtime_ss __maybe_unused,
int kmodule __maybe_unused) int kmodule __maybe_unused)
{ {
unsigned char build_id[BUILD_ID_SIZE]; struct build_id bid;
int ret; int ret;
ret = fd__is_64_bit(ss->fd); ret = fd__is_64_bit(ss->fd);
if (ret >= 0) if (ret >= 0)
dso->is_64_bit = ret; dso->is_64_bit = ret;
if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) { if (filename__read_build_id(ss->name, &bid) > 0)
dso__set_build_id(dso, build_id); dso__set_build_id(dso, bid.data);
}
return 0; return 0;
} }

View File

@ -1755,7 +1755,7 @@ int dso__load(struct dso *dso, struct map *map)
struct symsrc *syms_ss = NULL, *runtime_ss = NULL; struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
bool kmod; bool kmod;
bool perfmap; bool perfmap;
unsigned char build_id[BUILD_ID_SIZE]; struct build_id bid;
struct nscookie nsc; struct nscookie nsc;
char newmapname[PATH_MAX]; char newmapname[PATH_MAX];
const char *map_path = dso->long_name; const char *map_path = dso->long_name;
@ -1817,8 +1817,8 @@ int dso__load(struct dso *dso, struct map *map)
if (!dso->has_build_id && if (!dso->has_build_id &&
is_regular_file(dso->long_name)) { is_regular_file(dso->long_name)) {
__symbol__join_symfs(name, PATH_MAX, dso->long_name); __symbol__join_symfs(name, PATH_MAX, dso->long_name);
if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0) if (filename__read_build_id(name, &bid) > 0)
dso__set_build_id(dso, build_id); dso__set_build_id(dso, bid.data);
} }
/* /*

View File

@ -23,6 +23,7 @@ struct dso;
struct map; struct map;
struct maps; struct maps;
struct option; struct option;
struct build_id;
/* /*
* libelf 0.8.x and earlier do not support ELF_C_READ_MMAP; * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP;
@ -142,7 +143,7 @@ struct symbol *dso__next_symbol(struct symbol *sym);
enum dso_type dso__type_fd(int fd); enum dso_type dso__type_fd(int fd);
int filename__read_build_id(const char *filename, void *bf, size_t size); int filename__read_build_id(const char *filename, struct build_id *id);
int sysfs__read_build_id(const char *filename, void *bf, size_t size); int sysfs__read_build_id(const char *filename, void *bf, size_t size);
int modules__parse(const char *filename, void *arg, int modules__parse(const char *filename, void *arg,
int (*process_module)(void *arg, const char *name, int (*process_module)(void *arg, const char *name,