Arnaldo Carvalho de Melo | 7b2567c | 2010-02-03 16:52:04 -0200 | [diff] [blame] | 1 | /* |
| 2 | * build-id.c |
| 3 | * |
| 4 | * build-id support |
| 5 | * |
| 6 | * Copyright (C) 2009, 2010 Red Hat Inc. |
| 7 | * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com> |
| 8 | */ |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 9 | #include "util.h" |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame^] | 10 | #include <errno.h> |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 11 | #include <stdio.h> |
Arnaldo Carvalho de Melo | 7b2567c | 2010-02-03 16:52:04 -0200 | [diff] [blame] | 12 | #include "build-id.h" |
| 13 | #include "event.h" |
| 14 | #include "symbol.h" |
| 15 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 16 | #include "debug.h" |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 17 | #include "session.h" |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 18 | #include "tool.h" |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 19 | #include "header.h" |
| 20 | #include "vdso.h" |
Masami Hiramatsu | 6430a94 | 2016-07-01 17:04:10 +0900 | [diff] [blame] | 21 | #include "probe-file.h" |
Arnaldo Carvalho de Melo | 7b2567c | 2010-02-03 16:52:04 -0200 | [diff] [blame] | 22 | |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 23 | #include "sane_ctype.h" |
Namhyung Kim | 73c5d22 | 2014-11-07 22:57:56 +0900 | [diff] [blame] | 24 | |
| 25 | static bool no_buildid_cache; |
| 26 | |
Andrew Vagin | 54a3cf5 | 2012-08-07 16:56:05 +0400 | [diff] [blame] | 27 | int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused, |
| 28 | union perf_event *event, |
Adrian Hunter | ef89325 | 2013-08-27 11:23:06 +0300 | [diff] [blame] | 29 | struct perf_sample *sample, |
Andrew Vagin | 54a3cf5 | 2012-08-07 16:56:05 +0400 | [diff] [blame] | 30 | struct perf_evsel *evsel __maybe_unused, |
| 31 | struct machine *machine) |
Arnaldo Carvalho de Melo | 7b2567c | 2010-02-03 16:52:04 -0200 | [diff] [blame] | 32 | { |
| 33 | struct addr_location al; |
Adrian Hunter | ef89325 | 2013-08-27 11:23:06 +0300 | [diff] [blame] | 34 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
Namhyung Kim | 13ce34d | 2014-05-12 09:56:42 +0900 | [diff] [blame] | 35 | sample->tid); |
Arnaldo Carvalho de Melo | 7b2567c | 2010-02-03 16:52:04 -0200 | [diff] [blame] | 36 | |
| 37 | if (thread == NULL) { |
| 38 | pr_err("problem processing %d event, skipping it.\n", |
| 39 | event->header.type); |
| 40 | return -1; |
| 41 | } |
| 42 | |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 43 | thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, &al); |
Arnaldo Carvalho de Melo | 7b2567c | 2010-02-03 16:52:04 -0200 | [diff] [blame] | 44 | |
| 45 | if (al.map != NULL) |
| 46 | al.map->dso->hit = 1; |
| 47 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 48 | thread__put(thread); |
Arnaldo Carvalho de Melo | 7b2567c | 2010-02-03 16:52:04 -0200 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 52 | static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 53 | union perf_event *event, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 54 | struct perf_sample *sample |
| 55 | __maybe_unused, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 56 | struct machine *machine) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 57 | { |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 58 | struct thread *thread = machine__findnew_thread(machine, |
| 59 | event->fork.pid, |
| 60 | event->fork.tid); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 61 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 62 | dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid, |
| 63 | event->fork.ppid, event->fork.ptid); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 64 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 65 | if (thread) { |
He Kuang | 5e78c69 | 2015-04-10 17:35:00 +0800 | [diff] [blame] | 66 | machine__remove_thread(machine, thread); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 67 | thread__put(thread); |
| 68 | } |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 73 | struct perf_tool build_id__mark_dso_hit_ops = { |
Arnaldo Carvalho de Melo | 7b2567c | 2010-02-03 16:52:04 -0200 | [diff] [blame] | 74 | .sample = build_id__mark_dso_hit, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 75 | .mmap = perf_event__process_mmap, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 76 | .mmap2 = perf_event__process_mmap2, |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 77 | .fork = perf_event__process_fork, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 78 | .exit = perf_event__exit_del_thread, |
Stephane Eranian | 299c345 | 2012-05-15 13:28:15 +0200 | [diff] [blame] | 79 | .attr = perf_event__process_attr, |
| 80 | .build_id = perf_event__process_build_id, |
Adrian Hunter | 1216b65 | 2015-11-13 11:48:31 +0200 | [diff] [blame] | 81 | .ordered_events = true, |
Arnaldo Carvalho de Melo | 7b2567c | 2010-02-03 16:52:04 -0200 | [diff] [blame] | 82 | }; |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 83 | |
Jiri Olsa | ebb296c | 2012-10-27 23:18:28 +0200 | [diff] [blame] | 84 | int build_id__sprintf(const u8 *build_id, int len, char *bf) |
| 85 | { |
| 86 | char *bid = bf; |
| 87 | const u8 *raw = build_id; |
| 88 | int i; |
| 89 | |
| 90 | for (i = 0; i < len; ++i) { |
| 91 | sprintf(bid, "%02x", *raw); |
| 92 | ++raw; |
| 93 | bid += 2; |
| 94 | } |
| 95 | |
Michael Petlan | 7375e15 | 2015-11-27 14:48:09 +0100 | [diff] [blame] | 96 | return (bid - bf) + 1; |
Jiri Olsa | ebb296c | 2012-10-27 23:18:28 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Masami Hiramatsu | 0b5a793 | 2015-08-15 20:42:59 +0900 | [diff] [blame] | 99 | int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id) |
| 100 | { |
| 101 | char notes[PATH_MAX]; |
| 102 | u8 build_id[BUILD_ID_SIZE]; |
| 103 | int ret; |
| 104 | |
| 105 | if (!root_dir) |
| 106 | root_dir = ""; |
| 107 | |
| 108 | scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir); |
| 109 | |
| 110 | ret = sysfs__read_build_id(notes, build_id, sizeof(build_id)); |
| 111 | if (ret < 0) |
| 112 | return ret; |
| 113 | |
| 114 | return build_id__sprintf(build_id, sizeof(build_id), sbuild_id); |
| 115 | } |
| 116 | |
| 117 | int filename__sprintf_build_id(const char *pathname, char *sbuild_id) |
| 118 | { |
| 119 | u8 build_id[BUILD_ID_SIZE]; |
| 120 | int ret; |
| 121 | |
| 122 | ret = filename__read_build_id(pathname, build_id, sizeof(build_id)); |
| 123 | if (ret < 0) |
| 124 | return ret; |
| 125 | else if (ret != sizeof(build_id)) |
| 126 | return -EINVAL; |
| 127 | |
| 128 | return build_id__sprintf(build_id, sizeof(build_id), sbuild_id); |
| 129 | } |
| 130 | |
Masami Hiramatsu | 5cb113f | 2015-02-10 18:18:53 +0900 | [diff] [blame] | 131 | /* asnprintf consolidates asprintf and snprintf */ |
| 132 | static int asnprintf(char **strp, size_t size, const char *fmt, ...) |
| 133 | { |
| 134 | va_list ap; |
| 135 | int ret; |
| 136 | |
| 137 | if (!strp) |
| 138 | return -EINVAL; |
| 139 | |
| 140 | va_start(ap, fmt); |
| 141 | if (*strp) |
| 142 | ret = vsnprintf(*strp, size, fmt, ap); |
| 143 | else |
| 144 | ret = vasprintf(strp, fmt, ap); |
| 145 | va_end(ap); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 150 | char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf, |
| 151 | size_t size) |
| 152 | { |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 153 | bool retry_old = true; |
| 154 | |
Wang Nan | c58c49a | 2016-06-07 03:54:38 +0000 | [diff] [blame] | 155 | snprintf(bf, size, "%s/%s/%s/kallsyms", |
| 156 | buildid_dir, DSO__NAME_KALLSYMS, sbuild_id); |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 157 | retry: |
| 158 | if (!access(bf, F_OK)) |
| 159 | return bf; |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 160 | if (retry_old) { |
| 161 | /* Try old style kallsyms cache */ |
Wang Nan | c58c49a | 2016-06-07 03:54:38 +0000 | [diff] [blame] | 162 | snprintf(bf, size, "%s/%s/%s", |
| 163 | buildid_dir, DSO__NAME_KALLSYMS, sbuild_id); |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 164 | retry_old = false; |
| 165 | goto retry; |
| 166 | } |
| 167 | |
| 168 | return NULL; |
| 169 | } |
| 170 | |
Masami Hiramatsu | 1f3736c | 2016-07-01 17:03:26 +0900 | [diff] [blame] | 171 | char *build_id_cache__linkname(const char *sbuild_id, char *bf, size_t size) |
Masami Hiramatsu | 5cb113f | 2015-02-10 18:18:53 +0900 | [diff] [blame] | 172 | { |
| 173 | char *tmp = bf; |
| 174 | int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir, |
| 175 | sbuild_id, sbuild_id + 2); |
| 176 | if (ret < 0 || (tmp && size < (unsigned int)ret)) |
| 177 | return NULL; |
| 178 | return bf; |
| 179 | } |
| 180 | |
Masami Hiramatsu | 1f3736c | 2016-07-01 17:03:26 +0900 | [diff] [blame] | 181 | char *build_id_cache__origname(const char *sbuild_id) |
| 182 | { |
| 183 | char *linkname; |
| 184 | char buf[PATH_MAX]; |
| 185 | char *ret = NULL, *p; |
| 186 | size_t offs = 5; /* == strlen("../..") */ |
Tommi Rantala | 5a23421 | 2017-03-22 15:06:20 +0200 | [diff] [blame] | 187 | ssize_t len; |
Masami Hiramatsu | 1f3736c | 2016-07-01 17:03:26 +0900 | [diff] [blame] | 188 | |
| 189 | linkname = build_id_cache__linkname(sbuild_id, NULL, 0); |
| 190 | if (!linkname) |
| 191 | return NULL; |
| 192 | |
Tommi Rantala | 5a23421 | 2017-03-22 15:06:20 +0200 | [diff] [blame] | 193 | len = readlink(linkname, buf, sizeof(buf) - 1); |
| 194 | if (len <= 0) |
Masami Hiramatsu | 1f3736c | 2016-07-01 17:03:26 +0900 | [diff] [blame] | 195 | goto out; |
Tommi Rantala | 5a23421 | 2017-03-22 15:06:20 +0200 | [diff] [blame] | 196 | buf[len] = '\0'; |
| 197 | |
Masami Hiramatsu | 1f3736c | 2016-07-01 17:03:26 +0900 | [diff] [blame] | 198 | /* The link should be "../..<origpath>/<sbuild_id>" */ |
| 199 | p = strrchr(buf, '/'); /* Cut off the "/<sbuild_id>" */ |
| 200 | if (p && (p > buf + offs)) { |
| 201 | *p = '\0'; |
| 202 | if (buf[offs + 1] == '[') |
| 203 | offs++; /* |
| 204 | * This is a DSO name, like [kernel.kallsyms]. |
| 205 | * Skip the first '/', since this is not the |
| 206 | * cache of a regular file. |
| 207 | */ |
| 208 | ret = strdup(buf + offs); /* Skip "../..[/]" */ |
| 209 | } |
| 210 | out: |
| 211 | free(linkname); |
| 212 | return ret; |
| 213 | } |
| 214 | |
Masami Hiramatsu | c3492a3 | 2016-07-12 19:04:54 +0900 | [diff] [blame] | 215 | /* Check if the given build_id cache is valid on current running system */ |
| 216 | static bool build_id_cache__valid_id(char *sbuild_id) |
| 217 | { |
| 218 | char real_sbuild_id[SBUILD_ID_SIZE] = ""; |
| 219 | char *pathname; |
| 220 | int ret = 0; |
| 221 | bool result = false; |
| 222 | |
| 223 | pathname = build_id_cache__origname(sbuild_id); |
| 224 | if (!pathname) |
| 225 | return false; |
| 226 | |
| 227 | if (!strcmp(pathname, DSO__NAME_KALLSYMS)) |
| 228 | ret = sysfs__sprintf_build_id("/", real_sbuild_id); |
| 229 | else if (pathname[0] == '/') |
| 230 | ret = filename__sprintf_build_id(pathname, real_sbuild_id); |
| 231 | else |
| 232 | ret = -EINVAL; /* Should we support other special DSO cache? */ |
| 233 | if (ret >= 0) |
| 234 | result = (strcmp(sbuild_id, real_sbuild_id) == 0); |
| 235 | free(pathname); |
| 236 | |
| 237 | return result; |
| 238 | } |
| 239 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 240 | static const char *build_id_cache__basename(bool is_kallsyms, bool is_vdso) |
| 241 | { |
| 242 | return is_kallsyms ? "kallsyms" : (is_vdso ? "vdso" : "elf"); |
| 243 | } |
| 244 | |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 245 | char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size) |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 246 | { |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 247 | bool is_kallsyms = dso__is_kallsyms((struct dso *)dso); |
| 248 | bool is_vdso = dso__is_vdso((struct dso *)dso); |
| 249 | char sbuild_id[SBUILD_ID_SIZE]; |
| 250 | char *linkname; |
| 251 | bool alloc = (bf == NULL); |
| 252 | int ret; |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 253 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 254 | if (!dso->has_build_id) |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 255 | return NULL; |
| 256 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 257 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 258 | linkname = build_id_cache__linkname(sbuild_id, NULL, 0); |
| 259 | if (!linkname) |
| 260 | return NULL; |
| 261 | |
| 262 | /* Check if old style build_id cache */ |
| 263 | if (is_regular_file(linkname)) |
| 264 | ret = asnprintf(&bf, size, "%s", linkname); |
| 265 | else |
| 266 | ret = asnprintf(&bf, size, "%s/%s", linkname, |
| 267 | build_id_cache__basename(is_kallsyms, is_vdso)); |
| 268 | if (ret < 0 || (!alloc && size < (unsigned int)ret)) |
| 269 | bf = NULL; |
| 270 | free(linkname); |
| 271 | |
| 272 | return bf; |
Arnaldo Carvalho de Melo | b36f19d | 2010-05-20 12:15:33 -0300 | [diff] [blame] | 273 | } |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 274 | |
Wang Nan | e7ee404 | 2016-02-05 14:01:27 +0000 | [diff] [blame] | 275 | bool dso__build_id_is_kmod(const struct dso *dso, char *bf, size_t size) |
| 276 | { |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 277 | char *id_name = NULL, *ch; |
Wang Nan | e7ee404 | 2016-02-05 14:01:27 +0000 | [diff] [blame] | 278 | struct stat sb; |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 279 | char sbuild_id[SBUILD_ID_SIZE]; |
Wang Nan | e7ee404 | 2016-02-05 14:01:27 +0000 | [diff] [blame] | 280 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 281 | if (!dso->has_build_id) |
| 282 | goto err; |
| 283 | |
| 284 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 285 | id_name = build_id_cache__linkname(sbuild_id, NULL, 0); |
Wang Nan | e7ee404 | 2016-02-05 14:01:27 +0000 | [diff] [blame] | 286 | if (!id_name) |
| 287 | goto err; |
| 288 | if (access(id_name, F_OK)) |
| 289 | goto err; |
| 290 | if (lstat(id_name, &sb) == -1) |
| 291 | goto err; |
| 292 | if ((size_t)sb.st_size > size - 1) |
| 293 | goto err; |
| 294 | if (readlink(id_name, bf, size - 1) < 0) |
| 295 | goto err; |
| 296 | |
| 297 | bf[sb.st_size] = '\0'; |
| 298 | |
| 299 | /* |
| 300 | * link should be: |
| 301 | * ../../lib/modules/4.4.0-rc4/kernel/net/ipv4/netfilter/nf_nat_ipv4.ko/a09fe3eb3147dafa4e3b31dbd6257e4d696bdc92 |
| 302 | */ |
| 303 | ch = strrchr(bf, '/'); |
| 304 | if (!ch) |
| 305 | goto err; |
| 306 | if (ch - 3 < bf) |
| 307 | goto err; |
| 308 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 309 | free(id_name); |
Wang Nan | e7ee404 | 2016-02-05 14:01:27 +0000 | [diff] [blame] | 310 | return strncmp(".ko", ch - 3, 3) == 0; |
| 311 | err: |
Wang Nan | e7ee404 | 2016-02-05 14:01:27 +0000 | [diff] [blame] | 312 | pr_err("Invalid build id: %s\n", id_name ? : |
| 313 | dso->long_name ? : |
| 314 | dso->short_name ? : |
| 315 | "[unknown]"); |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 316 | free(id_name); |
Wang Nan | e7ee404 | 2016-02-05 14:01:27 +0000 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 320 | #define dsos__for_each_with_build_id(pos, head) \ |
| 321 | list_for_each_entry(pos, head, node) \ |
| 322 | if (!pos->has_build_id) \ |
| 323 | continue; \ |
| 324 | else |
| 325 | |
| 326 | static int write_buildid(const char *name, size_t name_len, u8 *build_id, |
| 327 | pid_t pid, u16 misc, int fd) |
| 328 | { |
| 329 | int err; |
| 330 | struct build_id_event b; |
| 331 | size_t len; |
| 332 | |
| 333 | len = name_len + 1; |
| 334 | len = PERF_ALIGN(len, NAME_ALIGN); |
| 335 | |
| 336 | memset(&b, 0, sizeof(b)); |
| 337 | memcpy(&b.build_id, build_id, BUILD_ID_SIZE); |
| 338 | b.pid = pid; |
| 339 | b.header.misc = misc; |
| 340 | b.header.size = sizeof(b) + len; |
| 341 | |
| 342 | err = writen(fd, &b, sizeof(b)); |
| 343 | if (err < 0) |
| 344 | return err; |
| 345 | |
| 346 | return write_padded(fd, name, name_len + 1, len); |
| 347 | } |
| 348 | |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 349 | static int machine__write_buildid_table(struct machine *machine, int fd) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 350 | { |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 351 | int err = 0; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 352 | char nm[PATH_MAX]; |
| 353 | struct dso *pos; |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 354 | u16 kmisc = PERF_RECORD_MISC_KERNEL, |
| 355 | umisc = PERF_RECORD_MISC_USER; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 356 | |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 357 | if (!machine__is_host(machine)) { |
| 358 | kmisc = PERF_RECORD_MISC_GUEST_KERNEL; |
| 359 | umisc = PERF_RECORD_MISC_GUEST_USER; |
| 360 | } |
| 361 | |
| 362 | dsos__for_each_with_build_id(pos, &machine->dsos.head) { |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 363 | const char *name; |
| 364 | size_t name_len; |
Wang Nan | fd786fa | 2016-01-29 17:40:51 +0000 | [diff] [blame] | 365 | bool in_kernel = false; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 366 | |
He Kuang | 6ae98ba | 2016-05-12 08:43:11 +0000 | [diff] [blame] | 367 | if (!pos->hit && !dso__is_vdso(pos)) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 368 | continue; |
| 369 | |
| 370 | if (dso__is_vdso(pos)) { |
| 371 | name = pos->short_name; |
Andrey Ryabinin | 70a2cba | 2016-04-19 11:17:27 +0300 | [diff] [blame] | 372 | name_len = pos->short_name_len; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 373 | } else if (dso__is_kcore(pos)) { |
| 374 | machine__mmap_name(machine, nm, sizeof(nm)); |
| 375 | name = nm; |
Andrey Ryabinin | 70a2cba | 2016-04-19 11:17:27 +0300 | [diff] [blame] | 376 | name_len = strlen(nm); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 377 | } else { |
| 378 | name = pos->long_name; |
Andrey Ryabinin | 70a2cba | 2016-04-19 11:17:27 +0300 | [diff] [blame] | 379 | name_len = pos->long_name_len; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 380 | } |
| 381 | |
Wang Nan | fd786fa | 2016-01-29 17:40:51 +0000 | [diff] [blame] | 382 | in_kernel = pos->kernel || |
| 383 | is_kernel_module(name, |
| 384 | PERF_RECORD_MISC_CPUMODE_UNKNOWN); |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 385 | err = write_buildid(name, name_len, pos->build_id, machine->pid, |
Wang Nan | fd786fa | 2016-01-29 17:40:51 +0000 | [diff] [blame] | 386 | in_kernel ? kmisc : umisc, fd); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 387 | if (err) |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 388 | break; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 389 | } |
| 390 | |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 391 | return err; |
| 392 | } |
| 393 | |
| 394 | int perf_session__write_buildid_table(struct perf_session *session, int fd) |
| 395 | { |
| 396 | struct rb_node *nd; |
| 397 | int err = machine__write_buildid_table(&session->machines.host, fd); |
| 398 | |
| 399 | if (err) |
| 400 | return err; |
| 401 | |
| 402 | for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { |
| 403 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 404 | err = machine__write_buildid_table(pos, fd); |
| 405 | if (err) |
| 406 | break; |
| 407 | } |
| 408 | return err; |
| 409 | } |
| 410 | |
| 411 | static int __dsos__hit_all(struct list_head *head) |
| 412 | { |
| 413 | struct dso *pos; |
| 414 | |
| 415 | list_for_each_entry(pos, head, node) |
| 416 | pos->hit = true; |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static int machine__hit_all_dsos(struct machine *machine) |
| 422 | { |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 423 | return __dsos__hit_all(&machine->dsos.head); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | int dsos__hit_all(struct perf_session *session) |
| 427 | { |
| 428 | struct rb_node *nd; |
| 429 | int err; |
| 430 | |
| 431 | err = machine__hit_all_dsos(&session->machines.host); |
| 432 | if (err) |
| 433 | return err; |
| 434 | |
| 435 | for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { |
| 436 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 437 | |
| 438 | err = machine__hit_all_dsos(pos); |
| 439 | if (err) |
| 440 | return err; |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Namhyung Kim | 73c5d22 | 2014-11-07 22:57:56 +0900 | [diff] [blame] | 446 | void disable_buildid_cache(void) |
| 447 | { |
| 448 | no_buildid_cache = true; |
| 449 | } |
| 450 | |
Masami Hiramatsu | 1f3736c | 2016-07-01 17:03:26 +0900 | [diff] [blame] | 451 | static bool lsdir_bid_head_filter(const char *name __maybe_unused, |
| 452 | struct dirent *d __maybe_unused) |
| 453 | { |
| 454 | return (strlen(d->d_name) == 2) && |
| 455 | isxdigit(d->d_name[0]) && isxdigit(d->d_name[1]); |
| 456 | } |
| 457 | |
| 458 | static bool lsdir_bid_tail_filter(const char *name __maybe_unused, |
| 459 | struct dirent *d __maybe_unused) |
| 460 | { |
| 461 | int i = 0; |
| 462 | while (isxdigit(d->d_name[i]) && i < SBUILD_ID_SIZE - 3) |
| 463 | i++; |
| 464 | return (i == SBUILD_ID_SIZE - 3) && (d->d_name[i] == '\0'); |
| 465 | } |
| 466 | |
Masami Hiramatsu | c3492a3 | 2016-07-12 19:04:54 +0900 | [diff] [blame] | 467 | struct strlist *build_id_cache__list_all(bool validonly) |
Masami Hiramatsu | 1f3736c | 2016-07-01 17:03:26 +0900 | [diff] [blame] | 468 | { |
| 469 | struct strlist *toplist, *linklist = NULL, *bidlist; |
| 470 | struct str_node *nd, *nd2; |
| 471 | char *topdir, *linkdir = NULL; |
| 472 | char sbuild_id[SBUILD_ID_SIZE]; |
| 473 | |
Masami Hiramatsu | c3492a3 | 2016-07-12 19:04:54 +0900 | [diff] [blame] | 474 | /* for filename__ functions */ |
| 475 | if (validonly) |
| 476 | symbol__init(NULL); |
| 477 | |
Masami Hiramatsu | 1f3736c | 2016-07-01 17:03:26 +0900 | [diff] [blame] | 478 | /* Open the top-level directory */ |
| 479 | if (asprintf(&topdir, "%s/.build-id/", buildid_dir) < 0) |
| 480 | return NULL; |
| 481 | |
| 482 | bidlist = strlist__new(NULL, NULL); |
| 483 | if (!bidlist) |
| 484 | goto out; |
| 485 | |
| 486 | toplist = lsdir(topdir, lsdir_bid_head_filter); |
| 487 | if (!toplist) { |
| 488 | pr_debug("Error in lsdir(%s): %d\n", topdir, errno); |
| 489 | /* If there is no buildid cache, return an empty list */ |
| 490 | if (errno == ENOENT) |
| 491 | goto out; |
| 492 | goto err_out; |
| 493 | } |
| 494 | |
| 495 | strlist__for_each_entry(nd, toplist) { |
| 496 | if (asprintf(&linkdir, "%s/%s", topdir, nd->s) < 0) |
| 497 | goto err_out; |
| 498 | /* Open the lower-level directory */ |
| 499 | linklist = lsdir(linkdir, lsdir_bid_tail_filter); |
| 500 | if (!linklist) { |
| 501 | pr_debug("Error in lsdir(%s): %d\n", linkdir, errno); |
| 502 | goto err_out; |
| 503 | } |
| 504 | strlist__for_each_entry(nd2, linklist) { |
| 505 | if (snprintf(sbuild_id, SBUILD_ID_SIZE, "%s%s", |
| 506 | nd->s, nd2->s) != SBUILD_ID_SIZE - 1) |
| 507 | goto err_out; |
Masami Hiramatsu | c3492a3 | 2016-07-12 19:04:54 +0900 | [diff] [blame] | 508 | if (validonly && !build_id_cache__valid_id(sbuild_id)) |
| 509 | continue; |
Masami Hiramatsu | 1f3736c | 2016-07-01 17:03:26 +0900 | [diff] [blame] | 510 | if (strlist__add(bidlist, sbuild_id) < 0) |
| 511 | goto err_out; |
| 512 | } |
| 513 | strlist__delete(linklist); |
| 514 | zfree(&linkdir); |
| 515 | } |
| 516 | |
| 517 | out_free: |
| 518 | strlist__delete(toplist); |
| 519 | out: |
| 520 | free(topdir); |
| 521 | |
| 522 | return bidlist; |
| 523 | |
| 524 | err_out: |
| 525 | strlist__delete(linklist); |
| 526 | zfree(&linkdir); |
| 527 | strlist__delete(bidlist); |
| 528 | bidlist = NULL; |
| 529 | goto out_free; |
| 530 | } |
| 531 | |
Masami Hiramatsu | a598180 | 2016-07-12 19:05:37 +0900 | [diff] [blame] | 532 | static bool str_is_build_id(const char *maybe_sbuild_id, size_t len) |
| 533 | { |
| 534 | size_t i; |
| 535 | |
| 536 | for (i = 0; i < len; i++) { |
| 537 | if (!isxdigit(maybe_sbuild_id[i])) |
| 538 | return false; |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 | |
| 543 | /* Return the valid complete build-id */ |
| 544 | char *build_id_cache__complement(const char *incomplete_sbuild_id) |
| 545 | { |
| 546 | struct strlist *bidlist; |
| 547 | struct str_node *nd, *cand = NULL; |
| 548 | char *sbuild_id = NULL; |
| 549 | size_t len = strlen(incomplete_sbuild_id); |
| 550 | |
| 551 | if (len >= SBUILD_ID_SIZE || |
| 552 | !str_is_build_id(incomplete_sbuild_id, len)) |
| 553 | return NULL; |
| 554 | |
| 555 | bidlist = build_id_cache__list_all(true); |
| 556 | if (!bidlist) |
| 557 | return NULL; |
| 558 | |
| 559 | strlist__for_each_entry(nd, bidlist) { |
| 560 | if (strncmp(nd->s, incomplete_sbuild_id, len) != 0) |
| 561 | continue; |
| 562 | if (cand) { /* Error: There are more than 2 candidates. */ |
| 563 | cand = NULL; |
| 564 | break; |
| 565 | } |
| 566 | cand = nd; |
| 567 | } |
| 568 | if (cand) |
| 569 | sbuild_id = strdup(cand->s); |
| 570 | strlist__delete(bidlist); |
| 571 | |
| 572 | return sbuild_id; |
| 573 | } |
| 574 | |
Masami Hiramatsu | 4698b8b | 2016-06-08 18:29:30 +0900 | [diff] [blame] | 575 | char *build_id_cache__cachedir(const char *sbuild_id, const char *name, |
| 576 | bool is_kallsyms, bool is_vdso) |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 577 | { |
| 578 | char *realname = (char *)name, *filename; |
| 579 | bool slash = is_kallsyms || is_vdso; |
| 580 | |
| 581 | if (!slash) { |
| 582 | realname = realpath(name, NULL); |
| 583 | if (!realname) |
| 584 | return NULL; |
| 585 | } |
| 586 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 587 | if (asprintf(&filename, "%s%s%s%s%s", buildid_dir, slash ? "/" : "", |
| 588 | is_vdso ? DSO__NAME_VDSO : realname, |
| 589 | sbuild_id ? "/" : "", sbuild_id ?: "") < 0) |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 590 | filename = NULL; |
| 591 | |
| 592 | if (!slash) |
| 593 | free(realname); |
| 594 | |
| 595 | return filename; |
| 596 | } |
| 597 | |
| 598 | int build_id_cache__list_build_ids(const char *pathname, |
| 599 | struct strlist **result) |
| 600 | { |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 601 | char *dir_name; |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 602 | int ret = 0; |
| 603 | |
Masami Hiramatsu | 4698b8b | 2016-06-08 18:29:30 +0900 | [diff] [blame] | 604 | dir_name = build_id_cache__cachedir(NULL, pathname, false, false); |
Masami Hiramatsu | d65444d | 2016-05-11 22:52:17 +0900 | [diff] [blame] | 605 | if (!dir_name) |
| 606 | return -ENOMEM; |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 607 | |
Masami Hiramatsu | d65444d | 2016-05-11 22:52:17 +0900 | [diff] [blame] | 608 | *result = lsdir(dir_name, lsdir_no_dot_filter); |
| 609 | if (!*result) |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 610 | ret = -errno; |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 611 | free(dir_name); |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 612 | |
| 613 | return ret; |
| 614 | } |
| 615 | |
Arnaldo Carvalho de Melo | 1c1a3a4 | 2016-07-12 12:19:09 -0300 | [diff] [blame] | 616 | #if defined(HAVE_LIBELF_SUPPORT) && defined(HAVE_GELF_GETNOTE_SUPPORT) |
Masami Hiramatsu | 6430a94 | 2016-07-01 17:04:10 +0900 | [diff] [blame] | 617 | static int build_id_cache__add_sdt_cache(const char *sbuild_id, |
| 618 | const char *realname) |
| 619 | { |
| 620 | struct probe_cache *cache; |
| 621 | int ret; |
| 622 | |
| 623 | cache = probe_cache__new(sbuild_id); |
| 624 | if (!cache) |
| 625 | return -1; |
| 626 | |
| 627 | ret = probe_cache__scan_sdt(cache, realname); |
| 628 | if (ret >= 0) { |
Adrian Hunter | f965520 | 2016-09-23 17:38:40 +0300 | [diff] [blame] | 629 | pr_debug4("Found %d SDTs in %s\n", ret, realname); |
Masami Hiramatsu | 6430a94 | 2016-07-01 17:04:10 +0900 | [diff] [blame] | 630 | if (probe_cache__commit(cache) < 0) |
| 631 | ret = -1; |
| 632 | } |
| 633 | probe_cache__delete(cache); |
| 634 | return ret; |
| 635 | } |
| 636 | #else |
| 637 | #define build_id_cache__add_sdt_cache(sbuild_id, realname) (0) |
| 638 | #endif |
| 639 | |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 640 | int build_id_cache__add_s(const char *sbuild_id, const char *name, |
| 641 | bool is_kallsyms, bool is_vdso) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 642 | { |
| 643 | const size_t size = PATH_MAX; |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 644 | char *realname = NULL, *filename = NULL, *dir_name = NULL, |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 645 | *linkname = zalloc(size), *tmp; |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 646 | int err = -1; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 647 | |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 648 | if (!is_kallsyms) { |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 649 | realname = realpath(name, NULL); |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 650 | if (!realname) |
| 651 | goto out_free; |
| 652 | } |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 653 | |
Masami Hiramatsu | 4698b8b | 2016-06-08 18:29:30 +0900 | [diff] [blame] | 654 | dir_name = build_id_cache__cachedir(sbuild_id, name, |
| 655 | is_kallsyms, is_vdso); |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 656 | if (!dir_name) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 657 | goto out_free; |
| 658 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 659 | /* Remove old style build-id cache */ |
| 660 | if (is_regular_file(dir_name)) |
| 661 | if (unlink(dir_name)) |
| 662 | goto out_free; |
| 663 | |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 664 | if (mkdir_p(dir_name, 0755)) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 665 | goto out_free; |
| 666 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 667 | /* Save the allocated buildid dirname */ |
| 668 | if (asprintf(&filename, "%s/%s", dir_name, |
| 669 | build_id_cache__basename(is_kallsyms, is_vdso)) < 0) { |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 670 | filename = NULL; |
| 671 | goto out_free; |
| 672 | } |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 673 | |
| 674 | if (access(filename, F_OK)) { |
| 675 | if (is_kallsyms) { |
| 676 | if (copyfile("/proc/kallsyms", filename)) |
| 677 | goto out_free; |
Milos Vyletel | 0635b0f | 2015-03-20 11:37:25 +0100 | [diff] [blame] | 678 | } else if (link(realname, filename) && errno != EEXIST && |
| 679 | copyfile(name, filename)) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 680 | goto out_free; |
| 681 | } |
| 682 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 683 | if (!build_id_cache__linkname(sbuild_id, linkname, size)) |
Masami Hiramatsu | 5cb113f | 2015-02-10 18:18:53 +0900 | [diff] [blame] | 684 | goto out_free; |
| 685 | tmp = strrchr(linkname, '/'); |
| 686 | *tmp = '\0'; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 687 | |
| 688 | if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) |
| 689 | goto out_free; |
| 690 | |
Masami Hiramatsu | 5cb113f | 2015-02-10 18:18:53 +0900 | [diff] [blame] | 691 | *tmp = '/'; |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 692 | tmp = dir_name + strlen(buildid_dir) - 5; |
| 693 | memcpy(tmp, "../..", 5); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 694 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 695 | if (symlink(tmp, linkname) == 0) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 696 | err = 0; |
Masami Hiramatsu | 6430a94 | 2016-07-01 17:04:10 +0900 | [diff] [blame] | 697 | |
| 698 | /* Update SDT cache : error is just warned */ |
Tommi Rantala | 2ccc220 | 2017-03-22 15:06:19 +0200 | [diff] [blame] | 699 | if (realname && build_id_cache__add_sdt_cache(sbuild_id, realname) < 0) |
Adrian Hunter | f965520 | 2016-09-23 17:38:40 +0300 | [diff] [blame] | 700 | pr_debug4("Failed to update/scan SDT cache for %s\n", realname); |
Masami Hiramatsu | 6430a94 | 2016-07-01 17:04:10 +0900 | [diff] [blame] | 701 | |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 702 | out_free: |
| 703 | if (!is_kallsyms) |
| 704 | free(realname); |
| 705 | free(filename); |
Masami Hiramatsu | 8d8c8e4 | 2015-02-27 13:50:26 +0900 | [diff] [blame] | 706 | free(dir_name); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 707 | free(linkname); |
| 708 | return err; |
| 709 | } |
| 710 | |
| 711 | static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 712 | const char *name, bool is_kallsyms, |
| 713 | bool is_vdso) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 714 | { |
Masami Hiramatsu | d77fac7 | 2015-07-15 18:14:28 +0900 | [diff] [blame] | 715 | char sbuild_id[SBUILD_ID_SIZE]; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 716 | |
| 717 | build_id__sprintf(build_id, build_id_size, sbuild_id); |
| 718 | |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 719 | return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 720 | } |
| 721 | |
Masami Hiramatsu | a50d11a | 2015-02-26 15:54:40 +0900 | [diff] [blame] | 722 | bool build_id_cache__cached(const char *sbuild_id) |
| 723 | { |
| 724 | bool ret = false; |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 725 | char *filename = build_id_cache__linkname(sbuild_id, NULL, 0); |
Masami Hiramatsu | a50d11a | 2015-02-26 15:54:40 +0900 | [diff] [blame] | 726 | |
| 727 | if (filename && !access(filename, F_OK)) |
| 728 | ret = true; |
| 729 | free(filename); |
| 730 | |
| 731 | return ret; |
| 732 | } |
| 733 | |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 734 | int build_id_cache__remove_s(const char *sbuild_id) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 735 | { |
| 736 | const size_t size = PATH_MAX; |
| 737 | char *filename = zalloc(size), |
Masami Hiramatsu | 5cb113f | 2015-02-10 18:18:53 +0900 | [diff] [blame] | 738 | *linkname = zalloc(size), *tmp; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 739 | int err = -1; |
| 740 | |
| 741 | if (filename == NULL || linkname == NULL) |
| 742 | goto out_free; |
| 743 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 744 | if (!build_id_cache__linkname(sbuild_id, linkname, size)) |
Masami Hiramatsu | 5cb113f | 2015-02-10 18:18:53 +0900 | [diff] [blame] | 745 | goto out_free; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 746 | |
| 747 | if (access(linkname, F_OK)) |
| 748 | goto out_free; |
| 749 | |
| 750 | if (readlink(linkname, filename, size - 1) < 0) |
| 751 | goto out_free; |
| 752 | |
| 753 | if (unlink(linkname)) |
| 754 | goto out_free; |
| 755 | |
| 756 | /* |
| 757 | * Since the link is relative, we must make it absolute: |
| 758 | */ |
Masami Hiramatsu | 5cb113f | 2015-02-10 18:18:53 +0900 | [diff] [blame] | 759 | tmp = strrchr(linkname, '/') + 1; |
| 760 | snprintf(tmp, size - (tmp - linkname), "%s", filename); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 761 | |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 762 | if (rm_rf(linkname)) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 763 | goto out_free; |
| 764 | |
| 765 | err = 0; |
| 766 | out_free: |
| 767 | free(filename); |
| 768 | free(linkname); |
| 769 | return err; |
| 770 | } |
| 771 | |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 772 | static int dso__cache_build_id(struct dso *dso, struct machine *machine) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 773 | { |
Masami Hiramatsu | 0141226 | 2016-05-29 00:15:37 +0900 | [diff] [blame] | 774 | bool is_kallsyms = dso__is_kallsyms(dso); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 775 | bool is_vdso = dso__is_vdso(dso); |
| 776 | const char *name = dso->long_name; |
| 777 | char nm[PATH_MAX]; |
| 778 | |
| 779 | if (dso__is_kcore(dso)) { |
| 780 | is_kallsyms = true; |
| 781 | machine__mmap_name(machine, nm, sizeof(nm)); |
| 782 | name = nm; |
| 783 | } |
| 784 | return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name, |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 785 | is_kallsyms, is_vdso); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | static int __dsos__cache_build_ids(struct list_head *head, |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 789 | struct machine *machine) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 790 | { |
| 791 | struct dso *pos; |
| 792 | int err = 0; |
| 793 | |
| 794 | dsos__for_each_with_build_id(pos, head) |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 795 | if (dso__cache_build_id(pos, machine)) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 796 | err = -1; |
| 797 | |
| 798 | return err; |
| 799 | } |
| 800 | |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 801 | static int machine__cache_build_ids(struct machine *machine) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 802 | { |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 803 | return __dsos__cache_build_ids(&machine->dsos.head, machine); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | int perf_session__cache_build_ids(struct perf_session *session) |
| 807 | { |
| 808 | struct rb_node *nd; |
| 809 | int ret; |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 810 | |
Namhyung Kim | 73c5d22 | 2014-11-07 22:57:56 +0900 | [diff] [blame] | 811 | if (no_buildid_cache) |
| 812 | return 0; |
| 813 | |
Jiri Olsa | 498922a | 2014-12-01 20:06:23 +0100 | [diff] [blame] | 814 | if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST) |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 815 | return -1; |
| 816 | |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 817 | ret = machine__cache_build_ids(&session->machines.host); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 818 | |
| 819 | for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { |
| 820 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Masami Hiramatsu | e35f736 | 2015-02-10 18:18:51 +0900 | [diff] [blame] | 821 | ret |= machine__cache_build_ids(pos); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 822 | } |
| 823 | return ret ? -1 : 0; |
| 824 | } |
| 825 | |
| 826 | static bool machine__read_build_ids(struct machine *machine, bool with_hits) |
| 827 | { |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 828 | return __dsos__read_build_ids(&machine->dsos.head, with_hits); |
Namhyung Kim | e195fac | 2014-11-04 10:14:30 +0900 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) |
| 832 | { |
| 833 | struct rb_node *nd; |
| 834 | bool ret = machine__read_build_ids(&session->machines.host, with_hits); |
| 835 | |
| 836 | for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { |
| 837 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 838 | ret |= machine__read_build_ids(pos, with_hits); |
| 839 | } |
| 840 | |
| 841 | return ret; |
| 842 | } |