History

perf build-id: Fix caching files with a wrong build ID

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 $

Adrian Hunter committed 4 years ago

Commit ab66fda1 parent 4b3f7641 file · +28 −0Browse files

Unified · Split

tools/perf/util/build-id.c+28−0
@@ -872,6 +872,30 @@ out_free: · 871 unmodified lines
872 return err;872 return err;
873}873}
874 874
875static 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
889static 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
875static int dso__cache_build_id(struct dso *dso, struct machine *machine,899static int dso__cache_build_id(struct dso *dso, struct machine *machine,
876 void *priv __maybe_unused)900 void *priv __maybe_unused)
877{901{
@@ -886,6 +910,10 @@ static int dso__cache_build_id(struct dso *dso, struct machine *machine, · 8 unmodified lines
886 is_kallsyms = true;910 is_kallsyms = true;
887 name = machine->mmap_name;911 name = machine->mmap_name;
888 }912 }
913
914 if (!is_kallsyms && dso__build_id_mismatch(dso, name))
915 return 0;
916
889 return build_id_cache__add_b(&dso->bid, name, dso->nsinfo,917 return build_id_cache__add_b(&dso->bid, name, dso->nsinfo,
890 is_kallsyms, is_vdso);918 is_kallsyms, is_vdso);
891}919}