2010-02-04 02:52:04 +08:00
|
|
|
/*
|
|
|
|
* build-id.c
|
|
|
|
*
|
|
|
|
* build-id support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009, 2010 Red Hat Inc.
|
|
|
|
* Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
|
|
|
|
*/
|
2010-05-20 23:15:33 +08:00
|
|
|
#include "util.h"
|
|
|
|
#include <stdio.h>
|
2010-02-04 02:52:04 +08:00
|
|
|
#include "build-id.h"
|
|
|
|
#include "event.h"
|
|
|
|
#include "symbol.h"
|
|
|
|
#include <linux/kernel.h>
|
2010-07-31 05:28:42 +08:00
|
|
|
#include "debug.h"
|
2011-11-25 18:19:45 +08:00
|
|
|
#include "session.h"
|
2011-11-28 18:30:20 +08:00
|
|
|
#include "tool.h"
|
2014-11-04 09:14:30 +08:00
|
|
|
#include "header.h"
|
|
|
|
#include "vdso.h"
|
2010-02-04 02:52:04 +08:00
|
|
|
|
2014-11-07 21:57:56 +08:00
|
|
|
|
|
|
|
static bool no_buildid_cache;
|
|
|
|
|
2012-08-07 20:56:05 +08:00
|
|
|
int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,
|
|
|
|
union perf_event *event,
|
2013-08-27 16:23:06 +08:00
|
|
|
struct perf_sample *sample,
|
2012-08-07 20:56:05 +08:00
|
|
|
struct perf_evsel *evsel __maybe_unused,
|
|
|
|
struct machine *machine)
|
2010-02-04 02:52:04 +08:00
|
|
|
{
|
|
|
|
struct addr_location al;
|
2013-08-27 16:23:06 +08:00
|
|
|
struct thread *thread = machine__findnew_thread(machine, sample->pid,
|
2014-05-12 08:56:42 +08:00
|
|
|
sample->tid);
|
2010-02-04 02:52:04 +08:00
|
|
|
|
|
|
|
if (thread == NULL) {
|
|
|
|
pr_err("problem processing %d event, skipping it.\n",
|
|
|
|
event->header.type);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-23 05:23:43 +08:00
|
|
|
thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, &al);
|
2010-02-04 02:52:04 +08:00
|
|
|
|
|
|
|
if (al.map != NULL)
|
|
|
|
al.map->dso->hit = 1;
|
|
|
|
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 07:43:22 +08:00
|
|
|
thread__put(thread);
|
2010-02-04 02:52:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-11 06:15:03 +08:00
|
|
|
static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
|
2011-11-25 18:19:45 +08:00
|
|
|
union perf_event *event,
|
2012-09-11 06:15:03 +08:00
|
|
|
struct perf_sample *sample
|
|
|
|
__maybe_unused,
|
2011-11-28 17:56:39 +08:00
|
|
|
struct machine *machine)
|
2010-07-31 05:28:42 +08:00
|
|
|
{
|
2013-08-27 16:23:03 +08:00
|
|
|
struct thread *thread = machine__findnew_thread(machine,
|
|
|
|
event->fork.pid,
|
|
|
|
event->fork.tid);
|
2010-07-31 05:28:42 +08:00
|
|
|
|
2011-01-30 00:01:45 +08:00
|
|
|
dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
|
|
|
|
event->fork.ppid, event->fork.ptid);
|
2010-07-31 05:28:42 +08:00
|
|
|
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 07:43:22 +08:00
|
|
|
if (thread) {
|
2015-04-10 17:35:00 +08:00
|
|
|
machine__remove_thread(machine, thread);
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 07:43:22 +08:00
|
|
|
thread__put(thread);
|
|
|
|
}
|
2010-07-31 05:28:42 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-28 18:30:20 +08:00
|
|
|
struct perf_tool build_id__mark_dso_hit_ops = {
|
2010-02-04 02:52:04 +08:00
|
|
|
.sample = build_id__mark_dso_hit,
|
2011-01-30 00:01:45 +08:00
|
|
|
.mmap = perf_event__process_mmap,
|
2013-08-21 18:10:25 +08:00
|
|
|
.mmap2 = perf_event__process_mmap2,
|
2012-10-07 02:44:59 +08:00
|
|
|
.fork = perf_event__process_fork,
|
2011-01-30 00:01:45 +08:00
|
|
|
.exit = perf_event__exit_del_thread,
|
2012-05-15 19:28:15 +08:00
|
|
|
.attr = perf_event__process_attr,
|
|
|
|
.build_id = perf_event__process_build_id,
|
2015-11-13 17:48:31 +08:00
|
|
|
.ordered_events = true,
|
2010-02-04 02:52:04 +08:00
|
|
|
};
|
2010-05-20 23:15:33 +08:00
|
|
|
|
2012-10-28 05:18:28 +08:00
|
|
|
int build_id__sprintf(const u8 *build_id, int len, char *bf)
|
|
|
|
{
|
|
|
|
char *bid = bf;
|
|
|
|
const u8 *raw = build_id;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
sprintf(bid, "%02x", *raw);
|
|
|
|
++raw;
|
|
|
|
bid += 2;
|
|
|
|
}
|
|
|
|
|
2015-11-27 21:48:09 +08:00
|
|
|
return (bid - bf) + 1;
|
2012-10-28 05:18:28 +08:00
|
|
|
}
|
|
|
|
|
2015-08-15 19:42:59 +08:00
|
|
|
int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id)
|
|
|
|
{
|
|
|
|
char notes[PATH_MAX];
|
|
|
|
u8 build_id[BUILD_ID_SIZE];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!root_dir)
|
|
|
|
root_dir = "";
|
|
|
|
|
|
|
|
scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
|
|
|
|
|
|
|
|
ret = sysfs__read_build_id(notes, build_id, sizeof(build_id));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
|
|
|
|
{
|
|
|
|
u8 build_id[BUILD_ID_SIZE];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = filename__read_build_id(pathname, build_id, sizeof(build_id));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
else if (ret != sizeof(build_id))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:18:53 +08:00
|
|
|
/* asnprintf consolidates asprintf and snprintf */
|
|
|
|
static int asnprintf(char **strp, size_t size, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!strp)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (*strp)
|
|
|
|
ret = vsnprintf(*strp, size, fmt, ap);
|
|
|
|
else
|
|
|
|
ret = vasprintf(strp, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
bool is_alloc = !!bf;
|
|
|
|
bool retry_old = true;
|
|
|
|
|
|
|
|
asnprintf(&bf, size, "%s/%s/%s/kallsyms",
|
|
|
|
buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
|
|
|
|
retry:
|
|
|
|
if (!access(bf, F_OK))
|
|
|
|
return bf;
|
|
|
|
if (is_alloc)
|
|
|
|
free(bf);
|
|
|
|
if (retry_old) {
|
|
|
|
/* Try old style kallsyms cache */
|
|
|
|
asnprintf(&bf, size, "%s/%s/%s",
|
|
|
|
buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
|
|
|
|
retry_old = false;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *build_id_cache__linkname(const char *sbuild_id, char *bf,
|
|
|
|
size_t size)
|
2015-02-10 17:18:53 +08:00
|
|
|
{
|
|
|
|
char *tmp = bf;
|
|
|
|
int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
|
|
|
|
sbuild_id, sbuild_id + 2);
|
|
|
|
if (ret < 0 || (tmp && size < (unsigned int)ret))
|
|
|
|
return NULL;
|
|
|
|
return bf;
|
|
|
|
}
|
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
static const char *build_id_cache__basename(bool is_kallsyms, bool is_vdso)
|
|
|
|
{
|
|
|
|
return is_kallsyms ? "kallsyms" : (is_vdso ? "vdso" : "elf");
|
|
|
|
}
|
|
|
|
|
2013-12-11 02:46:29 +08:00
|
|
|
char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
|
2010-05-20 23:15:33 +08:00
|
|
|
{
|
2016-05-28 23:15:37 +08:00
|
|
|
bool is_kallsyms = dso__is_kallsyms((struct dso *)dso);
|
|
|
|
bool is_vdso = dso__is_vdso((struct dso *)dso);
|
|
|
|
char sbuild_id[SBUILD_ID_SIZE];
|
|
|
|
char *linkname;
|
|
|
|
bool alloc = (bf == NULL);
|
|
|
|
int ret;
|
2010-05-20 23:15:33 +08:00
|
|
|
|
2013-10-23 06:01:31 +08:00
|
|
|
if (!dso->has_build_id)
|
2010-05-20 23:15:33 +08:00
|
|
|
return NULL;
|
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
|
|
|
|
linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
|
|
|
|
if (!linkname)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Check if old style build_id cache */
|
|
|
|
if (is_regular_file(linkname))
|
|
|
|
ret = asnprintf(&bf, size, "%s", linkname);
|
|
|
|
else
|
|
|
|
ret = asnprintf(&bf, size, "%s/%s", linkname,
|
|
|
|
build_id_cache__basename(is_kallsyms, is_vdso));
|
|
|
|
if (ret < 0 || (!alloc && size < (unsigned int)ret))
|
|
|
|
bf = NULL;
|
|
|
|
free(linkname);
|
|
|
|
|
|
|
|
return bf;
|
2010-05-20 23:15:33 +08:00
|
|
|
}
|
2014-11-04 09:14:30 +08:00
|
|
|
|
2016-02-05 22:01:27 +08:00
|
|
|
bool dso__build_id_is_kmod(const struct dso *dso, char *bf, size_t size)
|
|
|
|
{
|
2016-05-28 23:15:37 +08:00
|
|
|
char *id_name = NULL, *ch;
|
2016-02-05 22:01:27 +08:00
|
|
|
struct stat sb;
|
2016-05-28 23:15:37 +08:00
|
|
|
char sbuild_id[SBUILD_ID_SIZE];
|
|
|
|
|
|
|
|
if (!dso->has_build_id)
|
|
|
|
goto err;
|
2016-02-05 22:01:27 +08:00
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
|
|
|
|
id_name = build_id_cache__linkname(sbuild_id, NULL, 0);
|
2016-02-05 22:01:27 +08:00
|
|
|
if (!id_name)
|
|
|
|
goto err;
|
|
|
|
if (access(id_name, F_OK))
|
|
|
|
goto err;
|
|
|
|
if (lstat(id_name, &sb) == -1)
|
|
|
|
goto err;
|
|
|
|
if ((size_t)sb.st_size > size - 1)
|
|
|
|
goto err;
|
|
|
|
if (readlink(id_name, bf, size - 1) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
bf[sb.st_size] = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* link should be:
|
|
|
|
* ../../lib/modules/4.4.0-rc4/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko/a09fe3eb3147dafa4e3b31dbd6257e4d696bdc92
|
|
|
|
*/
|
|
|
|
ch = strrchr(bf, '/');
|
|
|
|
if (!ch)
|
|
|
|
goto err;
|
|
|
|
if (ch - 3 < bf)
|
|
|
|
goto err;
|
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
free(id_name);
|
2016-02-05 22:01:27 +08:00
|
|
|
return strncmp(".ko", ch - 3, 3) == 0;
|
|
|
|
err:
|
|
|
|
pr_err("Invalid build id: %s\n", id_name ? :
|
|
|
|
dso->long_name ? :
|
|
|
|
dso->short_name ? :
|
|
|
|
"[unknown]");
|
2016-05-28 23:15:37 +08:00
|
|
|
free(id_name);
|
2016-02-05 22:01:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:14:30 +08:00
|
|
|
#define dsos__for_each_with_build_id(pos, head) \
|
|
|
|
list_for_each_entry(pos, head, node) \
|
|
|
|
if (!pos->has_build_id) \
|
|
|
|
continue; \
|
|
|
|
else
|
|
|
|
|
|
|
|
static int write_buildid(const char *name, size_t name_len, u8 *build_id,
|
|
|
|
pid_t pid, u16 misc, int fd)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct build_id_event b;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = name_len + 1;
|
|
|
|
len = PERF_ALIGN(len, NAME_ALIGN);
|
|
|
|
|
|
|
|
memset(&b, 0, sizeof(b));
|
|
|
|
memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
|
|
|
|
b.pid = pid;
|
|
|
|
b.header.misc = misc;
|
|
|
|
b.header.size = sizeof(b) + len;
|
|
|
|
|
|
|
|
err = writen(fd, &b, sizeof(b));
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return write_padded(fd, name, name_len + 1, len);
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:06:42 +08:00
|
|
|
static int machine__write_buildid_table(struct machine *machine, int fd)
|
2014-11-04 09:14:30 +08:00
|
|
|
{
|
2015-05-29 00:06:42 +08:00
|
|
|
int err = 0;
|
2014-11-04 09:14:30 +08:00
|
|
|
char nm[PATH_MAX];
|
|
|
|
struct dso *pos;
|
2015-05-29 00:06:42 +08:00
|
|
|
u16 kmisc = PERF_RECORD_MISC_KERNEL,
|
|
|
|
umisc = PERF_RECORD_MISC_USER;
|
|
|
|
|
|
|
|
if (!machine__is_host(machine)) {
|
|
|
|
kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
|
|
|
|
umisc = PERF_RECORD_MISC_GUEST_USER;
|
|
|
|
}
|
2014-11-04 09:14:30 +08:00
|
|
|
|
2015-05-29 00:06:42 +08:00
|
|
|
dsos__for_each_with_build_id(pos, &machine->dsos.head) {
|
2014-11-04 09:14:30 +08:00
|
|
|
const char *name;
|
|
|
|
size_t name_len;
|
2016-01-30 01:40:51 +08:00
|
|
|
bool in_kernel = false;
|
2014-11-04 09:14:30 +08:00
|
|
|
|
2016-05-12 16:43:11 +08:00
|
|
|
if (!pos->hit && !dso__is_vdso(pos))
|
2014-11-04 09:14:30 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (dso__is_vdso(pos)) {
|
|
|
|
name = pos->short_name;
|
2016-04-19 16:17:27 +08:00
|
|
|
name_len = pos->short_name_len;
|
2014-11-04 09:14:30 +08:00
|
|
|
} else if (dso__is_kcore(pos)) {
|
|
|
|
machine__mmap_name(machine, nm, sizeof(nm));
|
|
|
|
name = nm;
|
2016-04-19 16:17:27 +08:00
|
|
|
name_len = strlen(nm);
|
2014-11-04 09:14:30 +08:00
|
|
|
} else {
|
|
|
|
name = pos->long_name;
|
2016-04-19 16:17:27 +08:00
|
|
|
name_len = pos->long_name_len;
|
2014-11-04 09:14:30 +08:00
|
|
|
}
|
|
|
|
|
2016-01-30 01:40:51 +08:00
|
|
|
in_kernel = pos->kernel ||
|
|
|
|
is_kernel_module(name,
|
|
|
|
PERF_RECORD_MISC_CPUMODE_UNKNOWN);
|
2015-05-29 00:06:42 +08:00
|
|
|
err = write_buildid(name, name_len, pos->build_id, machine->pid,
|
2016-01-30 01:40:51 +08:00
|
|
|
in_kernel ? kmisc : umisc, fd);
|
2014-11-04 09:14:30 +08:00
|
|
|
if (err)
|
2015-05-29 00:06:42 +08:00
|
|
|
break;
|
2014-11-04 09:14:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int perf_session__write_buildid_table(struct perf_session *session, int fd)
|
|
|
|
{
|
|
|
|
struct rb_node *nd;
|
|
|
|
int err = machine__write_buildid_table(&session->machines.host, fd);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
|
|
|
|
struct machine *pos = rb_entry(nd, struct machine, rb_node);
|
|
|
|
err = machine__write_buildid_table(pos, fd);
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __dsos__hit_all(struct list_head *head)
|
|
|
|
{
|
|
|
|
struct dso *pos;
|
|
|
|
|
|
|
|
list_for_each_entry(pos, head, node)
|
|
|
|
pos->hit = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int machine__hit_all_dsos(struct machine *machine)
|
|
|
|
{
|
2015-05-29 00:06:42 +08:00
|
|
|
return __dsos__hit_all(&machine->dsos.head);
|
2014-11-04 09:14:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int dsos__hit_all(struct perf_session *session)
|
|
|
|
{
|
|
|
|
struct rb_node *nd;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = machine__hit_all_dsos(&session->machines.host);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
|
|
|
|
struct machine *pos = rb_entry(nd, struct machine, rb_node);
|
|
|
|
|
|
|
|
err = machine__hit_all_dsos(pos);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-07 21:57:56 +08:00
|
|
|
void disable_buildid_cache(void)
|
|
|
|
{
|
|
|
|
no_buildid_cache = true;
|
|
|
|
}
|
|
|
|
|
2015-02-27 12:50:26 +08:00
|
|
|
static char *build_id_cache__dirname_from_path(const char *name,
|
2016-05-28 23:15:37 +08:00
|
|
|
bool is_kallsyms, bool is_vdso,
|
|
|
|
const char *sbuild_id)
|
2015-02-27 12:50:26 +08:00
|
|
|
{
|
|
|
|
char *realname = (char *)name, *filename;
|
|
|
|
bool slash = is_kallsyms || is_vdso;
|
|
|
|
|
|
|
|
if (!slash) {
|
|
|
|
realname = realpath(name, NULL);
|
|
|
|
if (!realname)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
if (asprintf(&filename, "%s%s%s%s%s", buildid_dir, slash ? "/" : "",
|
|
|
|
is_vdso ? DSO__NAME_VDSO : realname,
|
|
|
|
sbuild_id ? "/" : "", sbuild_id ?: "") < 0)
|
2015-02-27 12:50:26 +08:00
|
|
|
filename = NULL;
|
|
|
|
|
|
|
|
if (!slash)
|
|
|
|
free(realname);
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
int build_id_cache__list_build_ids(const char *pathname,
|
|
|
|
struct strlist **result)
|
|
|
|
{
|
|
|
|
char *dir_name;
|
|
|
|
int ret = 0;
|
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
dir_name = build_id_cache__dirname_from_path(pathname, false, false,
|
|
|
|
NULL);
|
2016-05-11 21:52:17 +08:00
|
|
|
if (!dir_name)
|
|
|
|
return -ENOMEM;
|
2015-02-27 12:50:26 +08:00
|
|
|
|
2016-05-11 21:52:17 +08:00
|
|
|
*result = lsdir(dir_name, lsdir_no_dot_filter);
|
|
|
|
if (!*result)
|
2015-02-27 12:50:26 +08:00
|
|
|
ret = -errno;
|
|
|
|
free(dir_name);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:18:51 +08:00
|
|
|
int build_id_cache__add_s(const char *sbuild_id, const char *name,
|
|
|
|
bool is_kallsyms, bool is_vdso)
|
2014-11-04 09:14:30 +08:00
|
|
|
{
|
|
|
|
const size_t size = PATH_MAX;
|
2015-02-27 12:50:26 +08:00
|
|
|
char *realname = NULL, *filename = NULL, *dir_name = NULL,
|
2016-05-28 23:15:37 +08:00
|
|
|
*linkname = zalloc(size), *tmp;
|
2015-02-27 12:50:26 +08:00
|
|
|
int err = -1;
|
2014-11-04 09:14:30 +08:00
|
|
|
|
2015-02-27 12:50:26 +08:00
|
|
|
if (!is_kallsyms) {
|
2014-11-04 09:14:30 +08:00
|
|
|
realname = realpath(name, NULL);
|
2015-02-27 12:50:26 +08:00
|
|
|
if (!realname)
|
|
|
|
goto out_free;
|
|
|
|
}
|
2014-11-04 09:14:30 +08:00
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
dir_name = build_id_cache__dirname_from_path(name, is_kallsyms,
|
|
|
|
is_vdso, sbuild_id);
|
2015-02-27 12:50:26 +08:00
|
|
|
if (!dir_name)
|
2014-11-04 09:14:30 +08:00
|
|
|
goto out_free;
|
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
/* Remove old style build-id cache */
|
|
|
|
if (is_regular_file(dir_name))
|
|
|
|
if (unlink(dir_name))
|
|
|
|
goto out_free;
|
|
|
|
|
2015-02-27 12:50:26 +08:00
|
|
|
if (mkdir_p(dir_name, 0755))
|
2014-11-04 09:14:30 +08:00
|
|
|
goto out_free;
|
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
/* Save the allocated buildid dirname */
|
|
|
|
if (asprintf(&filename, "%s/%s", dir_name,
|
|
|
|
build_id_cache__basename(is_kallsyms, is_vdso)) < 0) {
|
2015-02-27 12:50:26 +08:00
|
|
|
filename = NULL;
|
|
|
|
goto out_free;
|
|
|
|
}
|
2014-11-04 09:14:30 +08:00
|
|
|
|
|
|
|
if (access(filename, F_OK)) {
|
|
|
|
if (is_kallsyms) {
|
|
|
|
if (copyfile("/proc/kallsyms", filename))
|
|
|
|
goto out_free;
|
2015-03-20 18:37:25 +08:00
|
|
|
} else if (link(realname, filename) && errno != EEXIST &&
|
|
|
|
copyfile(name, filename))
|
2014-11-04 09:14:30 +08:00
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
if (!build_id_cache__linkname(sbuild_id, linkname, size))
|
2015-02-10 17:18:53 +08:00
|
|
|
goto out_free;
|
|
|
|
tmp = strrchr(linkname, '/');
|
|
|
|
*tmp = '\0';
|
2014-11-04 09:14:30 +08:00
|
|
|
|
|
|
|
if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
|
|
|
|
goto out_free;
|
|
|
|
|
2015-02-10 17:18:53 +08:00
|
|
|
*tmp = '/';
|
2016-05-28 23:15:37 +08:00
|
|
|
tmp = dir_name + strlen(buildid_dir) - 5;
|
|
|
|
memcpy(tmp, "../..", 5);
|
2014-11-04 09:14:30 +08:00
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
if (symlink(tmp, linkname) == 0)
|
2014-11-04 09:14:30 +08:00
|
|
|
err = 0;
|
|
|
|
out_free:
|
|
|
|
if (!is_kallsyms)
|
|
|
|
free(realname);
|
|
|
|
free(filename);
|
2015-02-27 12:50:26 +08:00
|
|
|
free(dir_name);
|
2014-11-04 09:14:30 +08:00
|
|
|
free(linkname);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
|
2015-02-10 17:18:51 +08:00
|
|
|
const char *name, bool is_kallsyms,
|
|
|
|
bool is_vdso)
|
2014-11-04 09:14:30 +08:00
|
|
|
{
|
2015-07-15 17:14:28 +08:00
|
|
|
char sbuild_id[SBUILD_ID_SIZE];
|
2014-11-04 09:14:30 +08:00
|
|
|
|
|
|
|
build_id__sprintf(build_id, build_id_size, sbuild_id);
|
|
|
|
|
2015-02-10 17:18:51 +08:00
|
|
|
return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso);
|
2014-11-04 09:14:30 +08:00
|
|
|
}
|
|
|
|
|
2015-02-26 14:54:40 +08:00
|
|
|
bool build_id_cache__cached(const char *sbuild_id)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2016-05-28 23:15:37 +08:00
|
|
|
char *filename = build_id_cache__linkname(sbuild_id, NULL, 0);
|
2015-02-26 14:54:40 +08:00
|
|
|
|
|
|
|
if (filename && !access(filename, F_OK))
|
|
|
|
ret = true;
|
|
|
|
free(filename);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:18:51 +08:00
|
|
|
int build_id_cache__remove_s(const char *sbuild_id)
|
2014-11-04 09:14:30 +08:00
|
|
|
{
|
|
|
|
const size_t size = PATH_MAX;
|
|
|
|
char *filename = zalloc(size),
|
2015-02-10 17:18:53 +08:00
|
|
|
*linkname = zalloc(size), *tmp;
|
2014-11-04 09:14:30 +08:00
|
|
|
int err = -1;
|
|
|
|
|
|
|
|
if (filename == NULL || linkname == NULL)
|
|
|
|
goto out_free;
|
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
if (!build_id_cache__linkname(sbuild_id, linkname, size))
|
2015-02-10 17:18:53 +08:00
|
|
|
goto out_free;
|
2014-11-04 09:14:30 +08:00
|
|
|
|
|
|
|
if (access(linkname, F_OK))
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
if (readlink(linkname, filename, size - 1) < 0)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
if (unlink(linkname))
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since the link is relative, we must make it absolute:
|
|
|
|
*/
|
2015-02-10 17:18:53 +08:00
|
|
|
tmp = strrchr(linkname, '/') + 1;
|
|
|
|
snprintf(tmp, size - (tmp - linkname), "%s", filename);
|
2014-11-04 09:14:30 +08:00
|
|
|
|
2016-05-28 23:15:37 +08:00
|
|
|
if (rm_rf(linkname))
|
2014-11-04 09:14:30 +08:00
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
out_free:
|
|
|
|
free(filename);
|
|
|
|
free(linkname);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:18:51 +08:00
|
|
|
static int dso__cache_build_id(struct dso *dso, struct machine *machine)
|
2014-11-04 09:14:30 +08:00
|
|
|
{
|
2016-05-28 23:15:37 +08:00
|
|
|
bool is_kallsyms = dso__is_kallsyms(dso);
|
2014-11-04 09:14:30 +08:00
|
|
|
bool is_vdso = dso__is_vdso(dso);
|
|
|
|
const char *name = dso->long_name;
|
|
|
|
char nm[PATH_MAX];
|
|
|
|
|
|
|
|
if (dso__is_kcore(dso)) {
|
|
|
|
is_kallsyms = true;
|
|
|
|
machine__mmap_name(machine, nm, sizeof(nm));
|
|
|
|
name = nm;
|
|
|
|
}
|
|
|
|
return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
|
2015-02-10 17:18:51 +08:00
|
|
|
is_kallsyms, is_vdso);
|
2014-11-04 09:14:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __dsos__cache_build_ids(struct list_head *head,
|
2015-02-10 17:18:51 +08:00
|
|
|
struct machine *machine)
|
2014-11-04 09:14:30 +08:00
|
|
|
{
|
|
|
|
struct dso *pos;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
dsos__for_each_with_build_id(pos, head)
|
2015-02-10 17:18:51 +08:00
|
|
|
if (dso__cache_build_id(pos, machine))
|
2014-11-04 09:14:30 +08:00
|
|
|
err = -1;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:18:51 +08:00
|
|
|
static int machine__cache_build_ids(struct machine *machine)
|
2014-11-04 09:14:30 +08:00
|
|
|
{
|
2015-05-29 00:06:42 +08:00
|
|
|
return __dsos__cache_build_ids(&machine->dsos.head, machine);
|
2014-11-04 09:14:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int perf_session__cache_build_ids(struct perf_session *session)
|
|
|
|
{
|
|
|
|
struct rb_node *nd;
|
|
|
|
int ret;
|
|
|
|
|
2014-11-07 21:57:56 +08:00
|
|
|
if (no_buildid_cache)
|
|
|
|
return 0;
|
|
|
|
|
2014-12-02 03:06:23 +08:00
|
|
|
if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
|
2014-11-04 09:14:30 +08:00
|
|
|
return -1;
|
|
|
|
|
2015-02-10 17:18:51 +08:00
|
|
|
ret = machine__cache_build_ids(&session->machines.host);
|
2014-11-04 09:14:30 +08:00
|
|
|
|
|
|
|
for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
|
|
|
|
struct machine *pos = rb_entry(nd, struct machine, rb_node);
|
2015-02-10 17:18:51 +08:00
|
|
|
ret |= machine__cache_build_ids(pos);
|
2014-11-04 09:14:30 +08:00
|
|
|
}
|
|
|
|
return ret ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool machine__read_build_ids(struct machine *machine, bool with_hits)
|
|
|
|
{
|
2015-05-29 00:06:42 +08:00
|
|
|
return __dsos__read_build_ids(&machine->dsos.head, with_hits);
|
2014-11-04 09:14:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
|
|
|
|
{
|
|
|
|
struct rb_node *nd;
|
|
|
|
bool ret = machine__read_build_ids(&session->machines.host, with_hits);
|
|
|
|
|
|
|
|
for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
|
|
|
|
struct machine *pos = rb_entry(nd, struct machine, rb_node);
|
|
|
|
ret |= machine__read_build_ids(pos, with_hits);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|