6 * Copyright (C) 2009, 2010 Red Hat Inc.
7 * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
14 #include <linux/kernel.h>
20 #include "probe-file.h"
23 static bool no_buildid_cache;
25 int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,
26 union perf_event *event,
27 struct perf_sample *sample,
28 struct perf_evsel *evsel __maybe_unused,
29 struct machine *machine)
31 struct addr_location al;
32 struct thread *thread = machine__findnew_thread(machine, sample->pid,
36 pr_err("problem processing %d event, skipping it.\n",
41 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, &al);
50 static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
51 union perf_event *event,
52 struct perf_sample *sample
54 struct machine *machine)
56 struct thread *thread = machine__findnew_thread(machine,
60 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
61 event->fork.ppid, event->fork.ptid);
64 machine__remove_thread(machine, thread);
71 struct perf_tool build_id__mark_dso_hit_ops = {
72 .sample = build_id__mark_dso_hit,
73 .mmap = perf_event__process_mmap,
74 .mmap2 = perf_event__process_mmap2,
75 .fork = perf_event__process_fork,
76 .exit = perf_event__exit_del_thread,
77 .attr = perf_event__process_attr,
78 .build_id = perf_event__process_build_id,
79 .ordered_events = true,
82 int build_id__sprintf(const u8 *build_id, int len, char *bf)
85 const u8 *raw = build_id;
88 for (i = 0; i < len; ++i) {
89 sprintf(bid, "%02x", *raw);
94 return (bid - bf) + 1;
97 int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
100 u8 build_id[BUILD_ID_SIZE];
106 scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
108 ret = sysfs__read_build_id(notes, build_id, sizeof(build_id));
112 return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
115 int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
117 u8 build_id[BUILD_ID_SIZE];
120 ret = filename__read_build_id(pathname, build_id, sizeof(build_id));
123 else if (ret != sizeof(build_id))
126 return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
129 /* asnprintf consolidates asprintf and snprintf */
130 static int asnprintf(char **strp, size_t size, const char *fmt, ...)
140 ret = vsnprintf(*strp, size, fmt, ap);
142 ret = vasprintf(strp, fmt, ap);
148 char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf,
151 bool retry_old = true;
153 snprintf(bf, size, "%s/%s/%s/kallsyms",
154 buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
156 if (!access(bf, F_OK))
159 /* Try old style kallsyms cache */
160 snprintf(bf, size, "%s/%s/%s",
161 buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
169 char *build_id_cache__linkname(const char *sbuild_id, char *bf, size_t size)
172 int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
173 sbuild_id, sbuild_id + 2);
174 if (ret < 0 || (tmp && size < (unsigned int)ret))
179 /* The caller is responsible to free the returned buffer. */
180 char *build_id_cache__origname(const char *sbuild_id)
184 char *ret = NULL, *p;
185 size_t offs = 5; /* == strlen("../..") */
188 linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
192 len = readlink(linkname, buf, sizeof(buf) - 1);
197 /* The link should be "../..<origpath>/<sbuild_id>" */
198 p = strrchr(buf, '/'); /* Cut off the "/<sbuild_id>" */
199 if (p && (p > buf + offs)) {
201 if (buf[offs + 1] == '[')
203 * This is a DSO name, like [kernel.kallsyms].
204 * Skip the first '/', since this is not the
205 * cache of a regular file.
207 ret = strdup(buf + offs); /* Skip "../..[/]" */
214 /* Check if the given build_id cache is valid on current running system */
215 static bool build_id_cache__valid_id(char *sbuild_id)
217 char real_sbuild_id[SBUILD_ID_SIZE] = "";
222 pathname = build_id_cache__origname(sbuild_id);
226 if (!strcmp(pathname, DSO__NAME_KALLSYMS))
227 ret = sysfs__sprintf_build_id("/", real_sbuild_id);
228 else if (pathname[0] == '/')
229 ret = filename__sprintf_build_id(pathname, real_sbuild_id);
231 ret = -EINVAL; /* Should we support other special DSO cache? */
233 result = (strcmp(sbuild_id, real_sbuild_id) == 0);
239 static const char *build_id_cache__basename(bool is_kallsyms, bool is_vdso)
241 return is_kallsyms ? "kallsyms" : (is_vdso ? "vdso" : "elf");
244 char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
246 bool is_kallsyms = dso__is_kallsyms((struct dso *)dso);
247 bool is_vdso = dso__is_vdso((struct dso *)dso);
248 char sbuild_id[SBUILD_ID_SIZE];
250 bool alloc = (bf == NULL);
253 if (!dso->has_build_id)
256 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
257 linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
261 /* Check if old style build_id cache */
262 if (is_regular_file(linkname))
263 ret = asnprintf(&bf, size, "%s", linkname);
265 ret = asnprintf(&bf, size, "%s/%s", linkname,
266 build_id_cache__basename(is_kallsyms, is_vdso));
267 if (ret < 0 || (!alloc && size < (unsigned int)ret))
274 bool dso__build_id_is_kmod(const struct dso *dso, char *bf, size_t size)
276 char *id_name = NULL, *ch;
278 char sbuild_id[SBUILD_ID_SIZE];
280 if (!dso->has_build_id)
283 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
284 id_name = build_id_cache__linkname(sbuild_id, NULL, 0);
287 if (access(id_name, F_OK))
289 if (lstat(id_name, &sb) == -1)
291 if ((size_t)sb.st_size > size - 1)
293 if (readlink(id_name, bf, size - 1) < 0)
296 bf[sb.st_size] = '\0';
300 * ../../lib/modules/4.4.0-rc4/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko/a09fe3eb3147dafa4e3b31dbd6257e4d696bdc92
302 ch = strrchr(bf, '/');
309 return strncmp(".ko", ch - 3, 3) == 0;
311 pr_err("Invalid build id: %s\n", id_name ? :
319 #define dsos__for_each_with_build_id(pos, head) \
320 list_for_each_entry(pos, head, node) \
321 if (!pos->has_build_id) \
325 static int write_buildid(const char *name, size_t name_len, u8 *build_id,
326 pid_t pid, u16 misc, int fd)
329 struct build_id_event b;
333 len = PERF_ALIGN(len, NAME_ALIGN);
335 memset(&b, 0, sizeof(b));
336 memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
338 b.header.misc = misc;
339 b.header.size = sizeof(b) + len;
341 err = writen(fd, &b, sizeof(b));
345 return write_padded(fd, name, name_len + 1, len);
348 static int machine__write_buildid_table(struct machine *machine, int fd)
353 u16 kmisc = PERF_RECORD_MISC_KERNEL,
354 umisc = PERF_RECORD_MISC_USER;
356 if (!machine__is_host(machine)) {
357 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
358 umisc = PERF_RECORD_MISC_GUEST_USER;
361 dsos__for_each_with_build_id(pos, &machine->dsos.head) {
364 bool in_kernel = false;
366 if (!pos->hit && !dso__is_vdso(pos))
369 if (dso__is_vdso(pos)) {
370 name = pos->short_name;
371 name_len = pos->short_name_len;
372 } else if (dso__is_kcore(pos)) {
373 machine__mmap_name(machine, nm, sizeof(nm));
375 name_len = strlen(nm);
377 name = pos->long_name;
378 name_len = pos->long_name_len;
381 in_kernel = pos->kernel ||
382 is_kernel_module(name,
383 PERF_RECORD_MISC_CPUMODE_UNKNOWN);
384 err = write_buildid(name, name_len, pos->build_id, machine->pid,
385 in_kernel ? kmisc : umisc, fd);
393 int perf_session__write_buildid_table(struct perf_session *session, int fd)
396 int err = machine__write_buildid_table(&session->machines.host, fd);
401 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
402 struct machine *pos = rb_entry(nd, struct machine, rb_node);
403 err = machine__write_buildid_table(pos, fd);
410 static int __dsos__hit_all(struct list_head *head)
414 list_for_each_entry(pos, head, node)
420 static int machine__hit_all_dsos(struct machine *machine)
422 return __dsos__hit_all(&machine->dsos.head);
425 int dsos__hit_all(struct perf_session *session)
430 err = machine__hit_all_dsos(&session->machines.host);
434 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
435 struct machine *pos = rb_entry(nd, struct machine, rb_node);
437 err = machine__hit_all_dsos(pos);
445 void disable_buildid_cache(void)
447 no_buildid_cache = true;
450 static bool lsdir_bid_head_filter(const char *name __maybe_unused,
451 struct dirent *d __maybe_unused)
453 return (strlen(d->d_name) == 2) &&
454 isxdigit(d->d_name[0]) && isxdigit(d->d_name[1]);
457 static bool lsdir_bid_tail_filter(const char *name __maybe_unused,
458 struct dirent *d __maybe_unused)
461 while (isxdigit(d->d_name[i]) && i < SBUILD_ID_SIZE - 3)
463 return (i == SBUILD_ID_SIZE - 3) && (d->d_name[i] == '\0');
466 struct strlist *build_id_cache__list_all(bool validonly)
468 struct strlist *toplist, *linklist = NULL, *bidlist;
469 struct str_node *nd, *nd2;
470 char *topdir, *linkdir = NULL;
471 char sbuild_id[SBUILD_ID_SIZE];
473 /* for filename__ functions */
477 /* Open the top-level directory */
478 if (asprintf(&topdir, "%s/.build-id/", buildid_dir) < 0)
481 bidlist = strlist__new(NULL, NULL);
485 toplist = lsdir(topdir, lsdir_bid_head_filter);
487 pr_debug("Error in lsdir(%s): %d\n", topdir, errno);
488 /* If there is no buildid cache, return an empty list */
494 strlist__for_each_entry(nd, toplist) {
495 if (asprintf(&linkdir, "%s/%s", topdir, nd->s) < 0)
497 /* Open the lower-level directory */
498 linklist = lsdir(linkdir, lsdir_bid_tail_filter);
500 pr_debug("Error in lsdir(%s): %d\n", linkdir, errno);
503 strlist__for_each_entry(nd2, linklist) {
504 if (snprintf(sbuild_id, SBUILD_ID_SIZE, "%s%s",
505 nd->s, nd2->s) != SBUILD_ID_SIZE - 1)
507 if (validonly && !build_id_cache__valid_id(sbuild_id))
509 if (strlist__add(bidlist, sbuild_id) < 0)
512 strlist__delete(linklist);
517 strlist__delete(toplist);
524 strlist__delete(linklist);
526 strlist__delete(bidlist);
531 static bool str_is_build_id(const char *maybe_sbuild_id, size_t len)
535 for (i = 0; i < len; i++) {
536 if (!isxdigit(maybe_sbuild_id[i]))
542 /* Return the valid complete build-id */
543 char *build_id_cache__complement(const char *incomplete_sbuild_id)
545 struct strlist *bidlist;
546 struct str_node *nd, *cand = NULL;
547 char *sbuild_id = NULL;
548 size_t len = strlen(incomplete_sbuild_id);
550 if (len >= SBUILD_ID_SIZE ||
551 !str_is_build_id(incomplete_sbuild_id, len))
554 bidlist = build_id_cache__list_all(true);
558 strlist__for_each_entry(nd, bidlist) {
559 if (strncmp(nd->s, incomplete_sbuild_id, len) != 0)
561 if (cand) { /* Error: There are more than 2 candidates. */
568 sbuild_id = strdup(cand->s);
569 strlist__delete(bidlist);
574 char *build_id_cache__cachedir(const char *sbuild_id, const char *name,
575 bool is_kallsyms, bool is_vdso)
577 char *realname = (char *)name, *filename;
578 bool slash = is_kallsyms || is_vdso;
581 realname = realpath(name, NULL);
586 if (asprintf(&filename, "%s%s%s%s%s", buildid_dir, slash ? "/" : "",
587 is_vdso ? DSO__NAME_VDSO : realname,
588 sbuild_id ? "/" : "", sbuild_id ?: "") < 0)
597 int build_id_cache__list_build_ids(const char *pathname,
598 struct strlist **result)
603 dir_name = build_id_cache__cachedir(NULL, pathname, false, false);
607 *result = lsdir(dir_name, lsdir_no_dot_filter);
615 #if defined(HAVE_LIBELF_SUPPORT) && defined(HAVE_GELF_GETNOTE_SUPPORT)
616 static int build_id_cache__add_sdt_cache(const char *sbuild_id,
617 const char *realname)
619 struct probe_cache *cache;
622 cache = probe_cache__new(sbuild_id);
626 ret = probe_cache__scan_sdt(cache, realname);
628 pr_debug4("Found %d SDTs in %s\n", ret, realname);
629 if (probe_cache__commit(cache) < 0)
632 probe_cache__delete(cache);
636 #define build_id_cache__add_sdt_cache(sbuild_id, realname) (0)
639 int build_id_cache__add_s(const char *sbuild_id, const char *name,
640 bool is_kallsyms, bool is_vdso)
642 const size_t size = PATH_MAX;
643 char *realname = NULL, *filename = NULL, *dir_name = NULL,
644 *linkname = zalloc(size), *tmp;
648 realname = realpath(name, NULL);
653 dir_name = build_id_cache__cachedir(sbuild_id, name,
654 is_kallsyms, is_vdso);
658 /* Remove old style build-id cache */
659 if (is_regular_file(dir_name))
660 if (unlink(dir_name))
663 if (mkdir_p(dir_name, 0755))
666 /* Save the allocated buildid dirname */
667 if (asprintf(&filename, "%s/%s", dir_name,
668 build_id_cache__basename(is_kallsyms, is_vdso)) < 0) {
673 if (access(filename, F_OK)) {
675 if (copyfile("/proc/kallsyms", filename))
677 } else if (link(realname, filename) && errno != EEXIST &&
678 copyfile(name, filename))
682 if (!build_id_cache__linkname(sbuild_id, linkname, size))
684 tmp = strrchr(linkname, '/');
687 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
691 tmp = dir_name + strlen(buildid_dir) - 5;
692 memcpy(tmp, "../..", 5);
694 if (symlink(tmp, linkname) == 0)
697 /* Update SDT cache : error is just warned */
698 if (build_id_cache__add_sdt_cache(sbuild_id, realname) < 0)
699 pr_debug4("Failed to update/scan SDT cache for %s\n", realname);
710 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
711 const char *name, bool is_kallsyms,
714 char sbuild_id[SBUILD_ID_SIZE];
716 build_id__sprintf(build_id, build_id_size, sbuild_id);
718 return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso);
721 bool build_id_cache__cached(const char *sbuild_id)
724 char *filename = build_id_cache__linkname(sbuild_id, NULL, 0);
726 if (filename && !access(filename, F_OK))
733 int build_id_cache__remove_s(const char *sbuild_id)
735 const size_t size = PATH_MAX;
736 char *filename = zalloc(size),
737 *linkname = zalloc(size), *tmp;
740 if (filename == NULL || linkname == NULL)
743 if (!build_id_cache__linkname(sbuild_id, linkname, size))
746 if (access(linkname, F_OK))
749 if (readlink(linkname, filename, size - 1) < 0)
752 if (unlink(linkname))
756 * Since the link is relative, we must make it absolute:
758 tmp = strrchr(linkname, '/') + 1;
759 snprintf(tmp, size - (tmp - linkname), "%s", filename);
771 static int dso__cache_build_id(struct dso *dso, struct machine *machine)
773 bool is_kallsyms = dso__is_kallsyms(dso);
774 bool is_vdso = dso__is_vdso(dso);
775 const char *name = dso->long_name;
778 if (dso__is_kcore(dso)) {
780 machine__mmap_name(machine, nm, sizeof(nm));
783 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
784 is_kallsyms, is_vdso);
787 static int __dsos__cache_build_ids(struct list_head *head,
788 struct machine *machine)
793 dsos__for_each_with_build_id(pos, head)
794 if (dso__cache_build_id(pos, machine))
800 static int machine__cache_build_ids(struct machine *machine)
802 return __dsos__cache_build_ids(&machine->dsos.head, machine);
805 int perf_session__cache_build_ids(struct perf_session *session)
810 if (no_buildid_cache)
813 if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
816 ret = machine__cache_build_ids(&session->machines.host);
818 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
819 struct machine *pos = rb_entry(nd, struct machine, rb_node);
820 ret |= machine__cache_build_ids(pos);
825 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
827 return __dsos__read_build_ids(&machine->dsos.head, with_hits);
830 bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
833 bool ret = machine__read_build_ids(&session->machines.host, with_hits);
835 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
836 struct machine *pos = rb_entry(nd, struct machine, rb_node);
837 ret |= machine__read_build_ids(pos, with_hits);