Skip to content

Commit 7bdcfaf

Browse files
ahunter6gregkh
authored andcommitted
perf build-id: Fix caching files with a wrong build ID
commit ab66fda upstream. Build ID events associate a file name with a build ID. However, when using perf inject, there is no guarantee that the file on the current machine at the current time has that build ID. Fix by comparing the build IDs and skip adding to the cache if they are different. Example: $ echo "int main() {return 0;}" > prog.c $ gcc -o prog prog.c $ perf record --buildid-all ./prog [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.019 MB perf.data ] $ file-buildid() { file $1 | awk -F= '{print $2}' | awk -F, '{print $1}' ; } $ file-buildid prog 444ad9be165d8058a48ce2ffb4e9f55854a3293e $ file-buildid ~/.debug/$(pwd)/prog/444ad9be165d8058a48ce2ffb4e9f55854a3293e/elf 444ad9be165d8058a48ce2ffb4e9f55854a3293e $ echo "int main() {return 1;}" > prog.c $ gcc -o prog prog.c $ file-buildid prog 885524d5aaa24008a3e2b06caa3ea95d013c0fc5 Before: $ perf buildid-cache --purge $(pwd)/prog $ perf inject -i perf.data -o junk $ file-buildid ~/.debug/$(pwd)/prog/444ad9be165d8058a48ce2ffb4e9f55854a3293e/elf 885524d5aaa24008a3e2b06caa3ea95d013c0fc5 $ After: $ perf buildid-cache --purge $(pwd)/prog $ perf inject -i perf.data -o junk $ file-buildid ~/.debug/$(pwd)/prog/444ad9be165d8058a48ce2ffb4e9f55854a3293e/elf $ Fixes: 454c407 ("perf: add perf-inject builtin") Signed-off-by: Adrian Hunter <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Tom Zanussi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 5641285 commit 7bdcfaf

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tools/perf/util/build-id.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,30 @@ int build_id_cache__remove_s(const char *sbuild_id)
872872
return err;
873873
}
874874

875+
static int filename__read_build_id_ns(const char *filename,
876+
struct build_id *bid,
877+
struct nsinfo *nsi)
878+
{
879+
struct nscookie nsc;
880+
int ret;
881+
882+
nsinfo__mountns_enter(nsi, &nsc);
883+
ret = filename__read_build_id(filename, bid);
884+
nsinfo__mountns_exit(&nsc);
885+
886+
return ret;
887+
}
888+
889+
static bool dso__build_id_mismatch(struct dso *dso, const char *name)
890+
{
891+
struct build_id bid;
892+
893+
if (filename__read_build_id_ns(name, &bid, dso->nsinfo) < 0)
894+
return false;
895+
896+
return !dso__build_id_equal(dso, &bid);
897+
}
898+
875899
static int dso__cache_build_id(struct dso *dso, struct machine *machine,
876900
void *priv __maybe_unused)
877901
{
@@ -886,6 +910,10 @@ static int dso__cache_build_id(struct dso *dso, struct machine *machine,
886910
is_kallsyms = true;
887911
name = machine->mmap_name;
888912
}
913+
914+
if (!is_kallsyms && dso__build_id_mismatch(dso, name))
915+
return 0;
916+
889917
return build_id_cache__add_b(&dso->bid, name, dso->nsinfo,
890918
is_kallsyms, is_vdso);
891919
}

0 commit comments

Comments
 (0)