Skip to content

Commit f766819

Browse files
olsajiriacmel
authored andcommitted
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>
1 parent 0aba7f0 commit f766819

11 files changed

Lines changed: 60 additions & 57 deletions

File tree

‎tools/perf/bench/inject-buildid.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static int add_dso(const char *fpath, const struct stat *sb __maybe_unused,
8484
if (typeflag == FTW_D || typeflag == FTW_SL)
8585
return 0;
8686

87-
if (filename__read_build_id(fpath, bid.data, sizeof(bid.data)) < 0)
87+
if (filename__read_build_id(fpath, &bid) < 0)
8888
return 0;
8989

9090
dso->name = realpath(fpath, NULL);

‎tools/perf/builtin-buildid-cache.c‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,19 @@ static int build_id_cache__add_kcore(const char *filename, bool force)
174174
static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
175175
{
176176
char sbuild_id[SBUILD_ID_SIZE];
177-
u8 build_id[BUILD_ID_SIZE];
177+
struct build_id bid;
178178
int err;
179179
struct nscookie nsc;
180180

181181
nsinfo__mountns_enter(nsi, &nsc);
182-
err = filename__read_build_id(filename, &build_id, sizeof(build_id));
182+
err = filename__read_build_id(filename, &bid);
183183
nsinfo__mountns_exit(&nsc);
184184
if (err < 0) {
185185
pr_debug("Couldn't read a build-id in %s\n", filename);
186186
return -1;
187187
}
188188

189-
build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
189+
build_id__sprintf(bid.data, sizeof(bid.data), sbuild_id);
190190
err = build_id_cache__add_s(sbuild_id, filename, nsi,
191191
false, false);
192192
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)
196196

197197
static int build_id_cache__remove_file(const char *filename, struct nsinfo *nsi)
198198
{
199-
u8 build_id[BUILD_ID_SIZE];
200199
char sbuild_id[SBUILD_ID_SIZE];
200+
struct build_id bid;
201201
struct nscookie nsc;
202202

203203
int err;
204204

205205
nsinfo__mountns_enter(nsi, &nsc);
206-
err = filename__read_build_id(filename, &build_id, sizeof(build_id));
206+
err = filename__read_build_id(filename, &bid);
207207
nsinfo__mountns_exit(&nsc);
208208
if (err < 0) {
209209
pr_debug("Couldn't read a build-id in %s\n", filename);
210210
return -1;
211211
}
212212

213-
build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
213+
build_id__sprintf(bid.data, sizeof(bid.data), sbuild_id);
214214
err = build_id_cache__remove_s(sbuild_id);
215215
pr_debug("Removing %s %s: %s\n", sbuild_id, filename,
216216
err ? "FAIL" : "Ok");
@@ -274,17 +274,16 @@ static int build_id_cache__purge_all(void)
274274
static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
275275
{
276276
char filename[PATH_MAX];
277-
u8 build_id[BUILD_ID_SIZE];
277+
struct build_id bid;
278278

279279
if (dso__build_id_filename(dso, filename, sizeof(filename), false) &&
280-
filename__read_build_id(filename, build_id,
281-
sizeof(build_id)) != sizeof(build_id)) {
280+
filename__read_build_id(filename, &bid) == -1) {
282281
if (errno == ENOENT)
283282
return false;
284283

285284
pr_warning("Problems with %s file, consider removing it from the cache\n",
286285
filename);
287-
} else if (memcmp(dso->bid.data, build_id, sizeof(dso->bid.data))) {
286+
} else if (memcmp(dso->bid.data, bid.data, bid.size)) {
288287
pr_warning("Problems with %s file, consider removing it from the cache\n",
289288
filename);
290289
}
@@ -300,22 +299,22 @@ static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *f
300299

301300
static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
302301
{
303-
u8 build_id[BUILD_ID_SIZE];
304302
char sbuild_id[SBUILD_ID_SIZE];
303+
struct build_id bid;
305304
struct nscookie nsc;
306305

307306
int err;
308307

309308
nsinfo__mountns_enter(nsi, &nsc);
310-
err = filename__read_build_id(filename, &build_id, sizeof(build_id));
309+
err = filename__read_build_id(filename, &bid);
311310
nsinfo__mountns_exit(&nsc);
312311
if (err < 0) {
313312
pr_debug("Couldn't read a build-id in %s\n", filename);
314313
return -1;
315314
}
316315
err = 0;
317316

318-
build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
317+
build_id__sprintf(bid.data, sizeof(bid.data), sbuild_id);
319318
if (build_id_cache__cached(sbuild_id))
320319
err = build_id_cache__remove_s(sbuild_id);
321320

‎tools/perf/builtin-inject.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,8 @@ static int dso__read_build_id(struct dso *dso)
522522
return 0;
523523

524524
nsinfo__mountns_enter(dso->nsinfo, &nsc);
525-
if (filename__read_build_id(dso->long_name, dso->bid.data,
526-
sizeof(dso->bid.data)) > 0) {
525+
if (filename__read_build_id(dso->long_name, &dso->bid) > 0)
527526
dso->has_build_id = true;
528-
}
529527
nsinfo__mountns_exit(&nsc);
530528

531529
return dso->has_build_id ? 0 : -1;

‎tools/perf/tests/pe-file-parsing.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static int run_dir(const char *d)
2424
{
2525
char filename[PATH_MAX];
2626
char debugfile[PATH_MAX];
27-
char build_id[BUILD_ID_SIZE];
27+
struct build_id bid;
2828
char debuglink[PATH_MAX];
2929
char expect_build_id[] = {
3030
0x5a, 0x0f, 0xd8, 0x82, 0xb5, 0x30, 0x84, 0x22,
@@ -36,10 +36,10 @@ static int run_dir(const char *d)
3636
int ret;
3737

3838
scnprintf(filename, PATH_MAX, "%s/pe-file.exe", d);
39-
ret = filename__read_build_id(filename, build_id, BUILD_ID_SIZE);
39+
ret = filename__read_build_id(filename, &bid);
4040
TEST_ASSERT_VAL("Failed to read build_id",
4141
ret == sizeof(expect_build_id));
42-
TEST_ASSERT_VAL("Wrong build_id", !memcmp(build_id, expect_build_id,
42+
TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
4343
sizeof(expect_build_id)));
4444

4545
ret = filename__read_debuglink(filename, debuglink, PATH_MAX);
@@ -48,10 +48,10 @@ static int run_dir(const char *d)
4848
!strcmp(debuglink, expect_debuglink));
4949

5050
scnprintf(debugfile, PATH_MAX, "%s/%s", d, debuglink);
51-
ret = filename__read_build_id(debugfile, build_id, BUILD_ID_SIZE);
51+
ret = filename__read_build_id(debugfile, &bid);
5252
TEST_ASSERT_VAL("Failed to read debug file build_id",
5353
ret == sizeof(expect_build_id));
54-
TEST_ASSERT_VAL("Wrong build_id", !memcmp(build_id, expect_build_id,
54+
TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
5555
sizeof(expect_build_id)));
5656

5757
dso = dso__new(filename);

‎tools/perf/tests/sdt.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ static int target_function(void)
2828
static int build_id_cache__add_file(const char *filename)
2929
{
3030
char sbuild_id[SBUILD_ID_SIZE];
31-
u8 build_id[BUILD_ID_SIZE];
31+
struct build_id bid;
3232
int err;
3333

34-
err = filename__read_build_id(filename, &build_id, sizeof(build_id));
34+
err = filename__read_build_id(filename, &bid);
3535
if (err < 0) {
3636
pr_debug("Failed to read build id of %s\n", filename);
3737
return err;
3838
}
3939

40-
build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
40+
build_id__sprintf(bid.data, sizeof(bid.data), sbuild_id);
4141
err = build_id_cache__add_s(sbuild_id, filename, NULL, false, false);
4242
if (err < 0)
4343
pr_debug("Failed to add build id cache of %s\n", filename);

‎tools/perf/util/build-id.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,14 @@ int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
130130

131131
int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
132132
{
133-
u8 build_id[BUILD_ID_SIZE];
133+
struct build_id bid;
134134
int ret;
135135

136-
ret = filename__read_build_id(pathname, build_id, sizeof(build_id));
136+
ret = filename__read_build_id(pathname, &bid);
137137
if (ret < 0)
138138
return ret;
139-
else if (ret != sizeof(build_id))
140-
return -EINVAL;
141139

142-
return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
140+
return build_id__sprintf(bid.data, sizeof(bid.data), sbuild_id);
143141
}
144142

145143
/* asnprintf consolidates asprintf and snprintf */

‎tools/perf/util/dsos.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
7373
continue;
7474
}
7575
nsinfo__mountns_enter(pos->nsinfo, &nsc);
76-
if (filename__read_build_id(pos->long_name, pos->bid.data,
77-
sizeof(pos->bid.data)) > 0) {
76+
if (filename__read_build_id(pos->long_name, &pos->bid) > 0) {
7877
have_build_id = true;
7978
pos->has_build_id = true;
8079
}

‎tools/perf/util/symbol-elf.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,9 @@ static int elf_read_build_id(Elf *elf, void *bf, size_t size)
534534

535535
#ifdef HAVE_LIBBFD_BUILDID_SUPPORT
536536

537-
int filename__read_build_id(const char *filename, void *bf, size_t size)
537+
int filename__read_build_id(const char *filename, struct build_id *bid)
538538
{
539+
size_t size = sizeof(bid->data);
539540
int err = -1;
540541
bfd *abfd;
541542

@@ -551,9 +552,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
551552
if (!abfd->build_id || abfd->build_id->size > size)
552553
goto out_close;
553554

554-
memcpy(bf, abfd->build_id->data, abfd->build_id->size);
555-
memset(bf + abfd->build_id->size, 0, size - abfd->build_id->size);
556-
err = abfd->build_id->size;
555+
memcpy(bid->data, abfd->build_id->data, abfd->build_id->size);
556+
memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size);
557+
err = bid->size = abfd->build_id->size;
557558

558559
out_close:
559560
bfd_close(abfd);
@@ -562,8 +563,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
562563

563564
#else // HAVE_LIBBFD_BUILDID_SUPPORT
564565

565-
int filename__read_build_id(const char *filename, void *bf, size_t size)
566+
int filename__read_build_id(const char *filename, struct build_id *bid)
566567
{
568+
size_t size = sizeof(bid->data);
567569
int fd, err = -1;
568570
Elf *elf;
569571

@@ -580,7 +582,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
580582
goto out_close;
581583
}
582584

583-
err = elf_read_build_id(elf, bf, size);
585+
err = elf_read_build_id(elf, bid->data, size);
586+
if (err > 0)
587+
bid->size = err;
584588

585589
elf_end(elf);
586590
out_close:

‎tools/perf/util/symbol-minimal.c‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ static bool check_need_swap(int file_endian)
3131

3232
#define NT_GNU_BUILD_ID 3
3333

34-
static int read_build_id(void *note_data, size_t note_len, void *bf,
35-
size_t size, bool need_swap)
34+
static int read_build_id(void *note_data, size_t note_len, struct build_id *bid,
35+
bool need_swap)
3636
{
37+
size_t size = sizeof(bid->data);
3738
struct {
3839
u32 n_namesz;
3940
u32 n_descsz;
@@ -63,8 +64,9 @@ static int read_build_id(void *note_data, size_t note_len, void *bf,
6364
nhdr->n_namesz == sizeof("GNU")) {
6465
if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
6566
size_t sz = min(size, descsz);
66-
memcpy(bf, ptr, sz);
67-
memset(bf + sz, 0, size - sz);
67+
memcpy(bid->data, ptr, sz);
68+
memset(bid->data + sz, 0, size - sz);
69+
bid->size = sz;
6870
return 0;
6971
}
7072
}
@@ -84,7 +86,7 @@ int filename__read_debuglink(const char *filename __maybe_unused,
8486
/*
8587
* Just try PT_NOTE header otherwise fails
8688
*/
87-
int filename__read_build_id(const char *filename, void *bf, size_t size)
89+
int filename__read_build_id(const char *filename, struct build_id *bid)
8890
{
8991
FILE *fp;
9092
int ret = -1;
@@ -156,9 +158,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
156158
if (fread(buf, buf_size, 1, fp) != 1)
157159
goto out_free;
158160

159-
ret = read_build_id(buf, buf_size, bf, size, need_swap);
161+
ret = read_build_id(buf, buf_size, bid, need_swap);
160162
if (ret == 0)
161-
ret = size;
163+
ret = bid->size;
162164
break;
163165
}
164166
} else {
@@ -207,9 +209,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
207209
if (fread(buf, buf_size, 1, fp) != 1)
208210
goto out_free;
209211

210-
ret = read_build_id(buf, buf_size, bf, size, need_swap);
212+
ret = read_build_id(buf, buf_size, bid, need_swap);
211213
if (ret == 0)
212-
ret = size;
214+
ret = bid->size;
213215
break;
214216
}
215217
}
@@ -220,8 +222,9 @@ int filename__read_build_id(const char *filename, void *bf, size_t size)
220222
return ret;
221223
}
222224

223-
int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
225+
int sysfs__read_build_id(const char *filename, void *build_id, size_t size __maybe_unused)
224226
{
227+
struct build_id bid;
225228
int fd;
226229
int ret = -1;
227230
struct stat stbuf;
@@ -243,7 +246,9 @@ int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
243246
if (read(fd, buf, buf_size) != (ssize_t) buf_size)
244247
goto out_free;
245248

246-
ret = read_build_id(buf, buf_size, build_id, size, false);
249+
ret = read_build_id(buf, buf_size, &bid, false);
250+
if (ret > 0)
251+
memcpy(build_id, bid.data, bid.size);
247252
out_free:
248253
free(buf);
249254
out:
@@ -339,16 +344,15 @@ int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
339344
struct symsrc *runtime_ss __maybe_unused,
340345
int kmodule __maybe_unused)
341346
{
342-
unsigned char build_id[BUILD_ID_SIZE];
347+
struct build_id bid;
343348
int ret;
344349

345350
ret = fd__is_64_bit(ss->fd);
346351
if (ret >= 0)
347352
dso->is_64_bit = ret;
348353

349-
if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
350-
dso__set_build_id(dso, build_id);
351-
}
354+
if (filename__read_build_id(ss->name, &bid) > 0)
355+
dso__set_build_id(dso, bid.data);
352356
return 0;
353357
}
354358

‎tools/perf/util/symbol.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ int dso__load(struct dso *dso, struct map *map)
17551755
struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
17561756
bool kmod;
17571757
bool perfmap;
1758-
unsigned char build_id[BUILD_ID_SIZE];
1758+
struct build_id bid;
17591759
struct nscookie nsc;
17601760
char newmapname[PATH_MAX];
17611761
const char *map_path = dso->long_name;
@@ -1817,8 +1817,8 @@ int dso__load(struct dso *dso, struct map *map)
18171817
if (!dso->has_build_id &&
18181818
is_regular_file(dso->long_name)) {
18191819
__symbol__join_symfs(name, PATH_MAX, dso->long_name);
1820-
if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
1821-
dso__set_build_id(dso, build_id);
1820+
if (filename__read_build_id(name, &bid) > 0)
1821+
dso__set_build_id(dso, bid.data);
18221822
}
18231823

18241824
/*

0 commit comments

Comments
 (0)