blob: 8d8ef1d31b1ece9aa066ca2cd1625890195318ae [file] [log] [blame]
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -02001/*
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 Melob36f19d2010-05-20 12:15:33 -03009#include "util.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030010#include <errno.h>
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030011#include <stdio.h>
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -020012#include "build-id.h"
13#include "event.h"
14#include "symbol.h"
15#include <linux/kernel.h>
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030016#include "debug.h"
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020017#include "session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020018#include "tool.h"
Namhyung Kime195fac2014-11-04 10:14:30 +090019#include "header.h"
20#include "vdso.h"
Masami Hiramatsu6430a942016-07-01 17:04:10 +090021#include "probe-file.h"
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -020022
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030023#include "sane_ctype.h"
Namhyung Kim73c5d222014-11-07 22:57:56 +090024
25static bool no_buildid_cache;
26
Andrew Vagin54a3cf52012-08-07 16:56:05 +040027int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,
28 union perf_event *event,
Adrian Hunteref893252013-08-27 11:23:06 +030029 struct perf_sample *sample,
Andrew Vagin54a3cf52012-08-07 16:56:05 +040030 struct perf_evsel *evsel __maybe_unused,
31 struct machine *machine)
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -020032{
33 struct addr_location al;
Adrian Hunteref893252013-08-27 11:23:06 +030034 struct thread *thread = machine__findnew_thread(machine, sample->pid,
Namhyung Kim13ce34d2014-05-12 09:56:42 +090035 sample->tid);
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -020036
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 Melo473398a2016-03-22 18:23:43 -030043 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, &al);
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -020044
45 if (al.map != NULL)
46 al.map->dso->hit = 1;
47
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030048 thread__put(thread);
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -020049 return 0;
50}
51
Irina Tirdea1d037ca2012-09-11 01:15:03 +030052static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020053 union perf_event *event,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030054 struct perf_sample *sample
55 __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020056 struct machine *machine)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030057{
Adrian Hunter314add62013-08-27 11:23:03 +030058 struct thread *thread = machine__findnew_thread(machine,
59 event->fork.pid,
60 event->fork.tid);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030061
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020062 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
63 event->fork.ppid, event->fork.ptid);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030064
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030065 if (thread) {
He Kuang5e78c692015-04-10 17:35:00 +080066 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030067 thread__put(thread);
68 }
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030069
70 return 0;
71}
72
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020073struct perf_tool build_id__mark_dso_hit_ops = {
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -020074 .sample = build_id__mark_dso_hit,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020075 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +020076 .mmap2 = perf_event__process_mmap2,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -030077 .fork = perf_event__process_fork,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020078 .exit = perf_event__exit_del_thread,
Stephane Eranian299c3452012-05-15 13:28:15 +020079 .attr = perf_event__process_attr,
80 .build_id = perf_event__process_build_id,
Adrian Hunter1216b652015-11-13 11:48:31 +020081 .ordered_events = true,
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -020082};
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030083
Jiri Olsaebb296c2012-10-27 23:18:28 +020084int 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 Petlan7375e152015-11-27 14:48:09 +010096 return (bid - bf) + 1;
Jiri Olsaebb296c2012-10-27 23:18:28 +020097}
98
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090099int 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
117int 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 Hiramatsu5cb113f2015-02-10 18:18:53 +0900131/* asnprintf consolidates asprintf and snprintf */
132static 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 Hiramatsu01412262016-05-29 00:15:37 +0900150char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf,
151 size_t size)
152{
Masami Hiramatsu01412262016-05-29 00:15:37 +0900153 bool retry_old = true;
154
Wang Nanc58c49a2016-06-07 03:54:38 +0000155 snprintf(bf, size, "%s/%s/%s/kallsyms",
156 buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
Masami Hiramatsu01412262016-05-29 00:15:37 +0900157retry:
158 if (!access(bf, F_OK))
159 return bf;
Masami Hiramatsu01412262016-05-29 00:15:37 +0900160 if (retry_old) {
161 /* Try old style kallsyms cache */
Wang Nanc58c49a2016-06-07 03:54:38 +0000162 snprintf(bf, size, "%s/%s/%s",
163 buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
Masami Hiramatsu01412262016-05-29 00:15:37 +0900164 retry_old = false;
165 goto retry;
166 }
167
168 return NULL;
169}
170
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900171char *build_id_cache__linkname(const char *sbuild_id, char *bf, size_t size)
Masami Hiramatsu5cb113f2015-02-10 18:18:53 +0900172{
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 Hiramatsu1f3736c2016-07-01 17:03:26 +0900181char *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 Rantala5a234212017-03-22 15:06:20 +0200187 ssize_t len;
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900188
189 linkname = build_id_cache__linkname(sbuild_id, NULL, 0);
190 if (!linkname)
191 return NULL;
192
Tommi Rantala5a234212017-03-22 15:06:20 +0200193 len = readlink(linkname, buf, sizeof(buf) - 1);
194 if (len <= 0)
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900195 goto out;
Tommi Rantala5a234212017-03-22 15:06:20 +0200196 buf[len] = '\0';
197
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900198 /* 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 }
210out:
211 free(linkname);
212 return ret;
213}
214
Masami Hiramatsuc3492a32016-07-12 19:04:54 +0900215/* Check if the given build_id cache is valid on current running system */
216static 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 Hiramatsu01412262016-05-29 00:15:37 +0900240static 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 Melo33449962013-12-10 15:46:29 -0300245char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -0300246{
Masami Hiramatsu01412262016-05-29 00:15:37 +0900247 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 Melob36f19d2010-05-20 12:15:33 -0300253
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300254 if (!dso->has_build_id)
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -0300255 return NULL;
256
Masami Hiramatsu01412262016-05-29 00:15:37 +0900257 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 Melob36f19d2010-05-20 12:15:33 -0300273}
Namhyung Kime195fac2014-11-04 10:14:30 +0900274
Wang Nane7ee4042016-02-05 14:01:27 +0000275bool dso__build_id_is_kmod(const struct dso *dso, char *bf, size_t size)
276{
Masami Hiramatsu01412262016-05-29 00:15:37 +0900277 char *id_name = NULL, *ch;
Wang Nane7ee4042016-02-05 14:01:27 +0000278 struct stat sb;
Masami Hiramatsu01412262016-05-29 00:15:37 +0900279 char sbuild_id[SBUILD_ID_SIZE];
Wang Nane7ee4042016-02-05 14:01:27 +0000280
Masami Hiramatsu01412262016-05-29 00:15:37 +0900281 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 Nane7ee4042016-02-05 14:01:27 +0000286 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 Hiramatsu01412262016-05-29 00:15:37 +0900309 free(id_name);
Wang Nane7ee4042016-02-05 14:01:27 +0000310 return strncmp(".ko", ch - 3, 3) == 0;
311err:
Wang Nane7ee4042016-02-05 14:01:27 +0000312 pr_err("Invalid build id: %s\n", id_name ? :
313 dso->long_name ? :
314 dso->short_name ? :
315 "[unknown]");
Masami Hiramatsu01412262016-05-29 00:15:37 +0900316 free(id_name);
Wang Nane7ee4042016-02-05 14:01:27 +0000317 return false;
318}
319
Namhyung Kime195fac2014-11-04 10:14:30 +0900320#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
326static 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 Melo3d39ac52015-05-28 13:06:42 -0300349static int machine__write_buildid_table(struct machine *machine, int fd)
Namhyung Kime195fac2014-11-04 10:14:30 +0900350{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300351 int err = 0;
Namhyung Kime195fac2014-11-04 10:14:30 +0900352 char nm[PATH_MAX];
353 struct dso *pos;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300354 u16 kmisc = PERF_RECORD_MISC_KERNEL,
355 umisc = PERF_RECORD_MISC_USER;
Namhyung Kime195fac2014-11-04 10:14:30 +0900356
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300357 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 Kime195fac2014-11-04 10:14:30 +0900363 const char *name;
364 size_t name_len;
Wang Nanfd786fa2016-01-29 17:40:51 +0000365 bool in_kernel = false;
Namhyung Kime195fac2014-11-04 10:14:30 +0900366
He Kuang6ae98ba2016-05-12 08:43:11 +0000367 if (!pos->hit && !dso__is_vdso(pos))
Namhyung Kime195fac2014-11-04 10:14:30 +0900368 continue;
369
370 if (dso__is_vdso(pos)) {
371 name = pos->short_name;
Andrey Ryabinin70a2cba2016-04-19 11:17:27 +0300372 name_len = pos->short_name_len;
Namhyung Kime195fac2014-11-04 10:14:30 +0900373 } else if (dso__is_kcore(pos)) {
374 machine__mmap_name(machine, nm, sizeof(nm));
375 name = nm;
Andrey Ryabinin70a2cba2016-04-19 11:17:27 +0300376 name_len = strlen(nm);
Namhyung Kime195fac2014-11-04 10:14:30 +0900377 } else {
378 name = pos->long_name;
Andrey Ryabinin70a2cba2016-04-19 11:17:27 +0300379 name_len = pos->long_name_len;
Namhyung Kime195fac2014-11-04 10:14:30 +0900380 }
381
Wang Nanfd786fa2016-01-29 17:40:51 +0000382 in_kernel = pos->kernel ||
383 is_kernel_module(name,
384 PERF_RECORD_MISC_CPUMODE_UNKNOWN);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300385 err = write_buildid(name, name_len, pos->build_id, machine->pid,
Wang Nanfd786fa2016-01-29 17:40:51 +0000386 in_kernel ? kmisc : umisc, fd);
Namhyung Kime195fac2014-11-04 10:14:30 +0900387 if (err)
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300388 break;
Namhyung Kime195fac2014-11-04 10:14:30 +0900389 }
390
Namhyung Kime195fac2014-11-04 10:14:30 +0900391 return err;
392}
393
394int 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
411static 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
421static int machine__hit_all_dsos(struct machine *machine)
422{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300423 return __dsos__hit_all(&machine->dsos.head);
Namhyung Kime195fac2014-11-04 10:14:30 +0900424}
425
426int 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 Kim73c5d222014-11-07 22:57:56 +0900446void disable_buildid_cache(void)
447{
448 no_buildid_cache = true;
449}
450
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900451static 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
458static 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 Hiramatsuc3492a32016-07-12 19:04:54 +0900467struct strlist *build_id_cache__list_all(bool validonly)
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900468{
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 Hiramatsuc3492a32016-07-12 19:04:54 +0900474 /* for filename__ functions */
475 if (validonly)
476 symbol__init(NULL);
477
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900478 /* 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 Hiramatsuc3492a32016-07-12 19:04:54 +0900508 if (validonly && !build_id_cache__valid_id(sbuild_id))
509 continue;
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +0900510 if (strlist__add(bidlist, sbuild_id) < 0)
511 goto err_out;
512 }
513 strlist__delete(linklist);
514 zfree(&linkdir);
515 }
516
517out_free:
518 strlist__delete(toplist);
519out:
520 free(topdir);
521
522 return bidlist;
523
524err_out:
525 strlist__delete(linklist);
526 zfree(&linkdir);
527 strlist__delete(bidlist);
528 bidlist = NULL;
529 goto out_free;
530}
531
Masami Hiramatsua5981802016-07-12 19:05:37 +0900532static 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 */
544char *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 Hiramatsu4698b8b2016-06-08 18:29:30 +0900575char *build_id_cache__cachedir(const char *sbuild_id, const char *name,
576 bool is_kallsyms, bool is_vdso)
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900577{
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 Hiramatsu01412262016-05-29 00:15:37 +0900587 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 Hiramatsu8d8c8e42015-02-27 13:50:26 +0900590 filename = NULL;
591
592 if (!slash)
593 free(realname);
594
595 return filename;
596}
597
598int build_id_cache__list_build_ids(const char *pathname,
599 struct strlist **result)
600{
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900601 char *dir_name;
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900602 int ret = 0;
603
Masami Hiramatsu4698b8b2016-06-08 18:29:30 +0900604 dir_name = build_id_cache__cachedir(NULL, pathname, false, false);
Masami Hiramatsud65444d2016-05-11 22:52:17 +0900605 if (!dir_name)
606 return -ENOMEM;
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900607
Masami Hiramatsud65444d2016-05-11 22:52:17 +0900608 *result = lsdir(dir_name, lsdir_no_dot_filter);
609 if (!*result)
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900610 ret = -errno;
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900611 free(dir_name);
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900612
613 return ret;
614}
615
Arnaldo Carvalho de Melo1c1a3a42016-07-12 12:19:09 -0300616#if defined(HAVE_LIBELF_SUPPORT) && defined(HAVE_GELF_GETNOTE_SUPPORT)
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900617static 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 Hunterf9655202016-09-23 17:38:40 +0300629 pr_debug4("Found %d SDTs in %s\n", ret, realname);
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900630 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 Hiramatsue35f7362015-02-10 18:18:51 +0900640int build_id_cache__add_s(const char *sbuild_id, const char *name,
641 bool is_kallsyms, bool is_vdso)
Namhyung Kime195fac2014-11-04 10:14:30 +0900642{
643 const size_t size = PATH_MAX;
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900644 char *realname = NULL, *filename = NULL, *dir_name = NULL,
Masami Hiramatsu01412262016-05-29 00:15:37 +0900645 *linkname = zalloc(size), *tmp;
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900646 int err = -1;
Namhyung Kime195fac2014-11-04 10:14:30 +0900647
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900648 if (!is_kallsyms) {
Namhyung Kime195fac2014-11-04 10:14:30 +0900649 realname = realpath(name, NULL);
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900650 if (!realname)
651 goto out_free;
652 }
Namhyung Kime195fac2014-11-04 10:14:30 +0900653
Masami Hiramatsu4698b8b2016-06-08 18:29:30 +0900654 dir_name = build_id_cache__cachedir(sbuild_id, name,
655 is_kallsyms, is_vdso);
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900656 if (!dir_name)
Namhyung Kime195fac2014-11-04 10:14:30 +0900657 goto out_free;
658
Masami Hiramatsu01412262016-05-29 00:15:37 +0900659 /* Remove old style build-id cache */
660 if (is_regular_file(dir_name))
661 if (unlink(dir_name))
662 goto out_free;
663
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900664 if (mkdir_p(dir_name, 0755))
Namhyung Kime195fac2014-11-04 10:14:30 +0900665 goto out_free;
666
Masami Hiramatsu01412262016-05-29 00:15:37 +0900667 /* Save the allocated buildid dirname */
668 if (asprintf(&filename, "%s/%s", dir_name,
669 build_id_cache__basename(is_kallsyms, is_vdso)) < 0) {
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900670 filename = NULL;
671 goto out_free;
672 }
Namhyung Kime195fac2014-11-04 10:14:30 +0900673
674 if (access(filename, F_OK)) {
675 if (is_kallsyms) {
676 if (copyfile("/proc/kallsyms", filename))
677 goto out_free;
Milos Vyletel0635b0f2015-03-20 11:37:25 +0100678 } else if (link(realname, filename) && errno != EEXIST &&
679 copyfile(name, filename))
Namhyung Kime195fac2014-11-04 10:14:30 +0900680 goto out_free;
681 }
682
Masami Hiramatsu01412262016-05-29 00:15:37 +0900683 if (!build_id_cache__linkname(sbuild_id, linkname, size))
Masami Hiramatsu5cb113f2015-02-10 18:18:53 +0900684 goto out_free;
685 tmp = strrchr(linkname, '/');
686 *tmp = '\0';
Namhyung Kime195fac2014-11-04 10:14:30 +0900687
688 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
689 goto out_free;
690
Masami Hiramatsu5cb113f2015-02-10 18:18:53 +0900691 *tmp = '/';
Masami Hiramatsu01412262016-05-29 00:15:37 +0900692 tmp = dir_name + strlen(buildid_dir) - 5;
693 memcpy(tmp, "../..", 5);
Namhyung Kime195fac2014-11-04 10:14:30 +0900694
Masami Hiramatsu01412262016-05-29 00:15:37 +0900695 if (symlink(tmp, linkname) == 0)
Namhyung Kime195fac2014-11-04 10:14:30 +0900696 err = 0;
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900697
698 /* Update SDT cache : error is just warned */
Tommi Rantala2ccc2202017-03-22 15:06:19 +0200699 if (realname && build_id_cache__add_sdt_cache(sbuild_id, realname) < 0)
Adrian Hunterf9655202016-09-23 17:38:40 +0300700 pr_debug4("Failed to update/scan SDT cache for %s\n", realname);
Masami Hiramatsu6430a942016-07-01 17:04:10 +0900701
Namhyung Kime195fac2014-11-04 10:14:30 +0900702out_free:
703 if (!is_kallsyms)
704 free(realname);
705 free(filename);
Masami Hiramatsu8d8c8e42015-02-27 13:50:26 +0900706 free(dir_name);
Namhyung Kime195fac2014-11-04 10:14:30 +0900707 free(linkname);
708 return err;
709}
710
711static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
Masami Hiramatsue35f7362015-02-10 18:18:51 +0900712 const char *name, bool is_kallsyms,
713 bool is_vdso)
Namhyung Kime195fac2014-11-04 10:14:30 +0900714{
Masami Hiramatsud77fac72015-07-15 18:14:28 +0900715 char sbuild_id[SBUILD_ID_SIZE];
Namhyung Kime195fac2014-11-04 10:14:30 +0900716
717 build_id__sprintf(build_id, build_id_size, sbuild_id);
718
Masami Hiramatsue35f7362015-02-10 18:18:51 +0900719 return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso);
Namhyung Kime195fac2014-11-04 10:14:30 +0900720}
721
Masami Hiramatsua50d11a2015-02-26 15:54:40 +0900722bool build_id_cache__cached(const char *sbuild_id)
723{
724 bool ret = false;
Masami Hiramatsu01412262016-05-29 00:15:37 +0900725 char *filename = build_id_cache__linkname(sbuild_id, NULL, 0);
Masami Hiramatsua50d11a2015-02-26 15:54:40 +0900726
727 if (filename && !access(filename, F_OK))
728 ret = true;
729 free(filename);
730
731 return ret;
732}
733
Masami Hiramatsue35f7362015-02-10 18:18:51 +0900734int build_id_cache__remove_s(const char *sbuild_id)
Namhyung Kime195fac2014-11-04 10:14:30 +0900735{
736 const size_t size = PATH_MAX;
737 char *filename = zalloc(size),
Masami Hiramatsu5cb113f2015-02-10 18:18:53 +0900738 *linkname = zalloc(size), *tmp;
Namhyung Kime195fac2014-11-04 10:14:30 +0900739 int err = -1;
740
741 if (filename == NULL || linkname == NULL)
742 goto out_free;
743
Masami Hiramatsu01412262016-05-29 00:15:37 +0900744 if (!build_id_cache__linkname(sbuild_id, linkname, size))
Masami Hiramatsu5cb113f2015-02-10 18:18:53 +0900745 goto out_free;
Namhyung Kime195fac2014-11-04 10:14:30 +0900746
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 Hiramatsu5cb113f2015-02-10 18:18:53 +0900759 tmp = strrchr(linkname, '/') + 1;
760 snprintf(tmp, size - (tmp - linkname), "%s", filename);
Namhyung Kime195fac2014-11-04 10:14:30 +0900761
Masami Hiramatsu01412262016-05-29 00:15:37 +0900762 if (rm_rf(linkname))
Namhyung Kime195fac2014-11-04 10:14:30 +0900763 goto out_free;
764
765 err = 0;
766out_free:
767 free(filename);
768 free(linkname);
769 return err;
770}
771
Masami Hiramatsue35f7362015-02-10 18:18:51 +0900772static int dso__cache_build_id(struct dso *dso, struct machine *machine)
Namhyung Kime195fac2014-11-04 10:14:30 +0900773{
Masami Hiramatsu01412262016-05-29 00:15:37 +0900774 bool is_kallsyms = dso__is_kallsyms(dso);
Namhyung Kime195fac2014-11-04 10:14:30 +0900775 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 Hiramatsue35f7362015-02-10 18:18:51 +0900785 is_kallsyms, is_vdso);
Namhyung Kime195fac2014-11-04 10:14:30 +0900786}
787
788static int __dsos__cache_build_ids(struct list_head *head,
Masami Hiramatsue35f7362015-02-10 18:18:51 +0900789 struct machine *machine)
Namhyung Kime195fac2014-11-04 10:14:30 +0900790{
791 struct dso *pos;
792 int err = 0;
793
794 dsos__for_each_with_build_id(pos, head)
Masami Hiramatsue35f7362015-02-10 18:18:51 +0900795 if (dso__cache_build_id(pos, machine))
Namhyung Kime195fac2014-11-04 10:14:30 +0900796 err = -1;
797
798 return err;
799}
800
Masami Hiramatsue35f7362015-02-10 18:18:51 +0900801static int machine__cache_build_ids(struct machine *machine)
Namhyung Kime195fac2014-11-04 10:14:30 +0900802{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300803 return __dsos__cache_build_ids(&machine->dsos.head, machine);
Namhyung Kime195fac2014-11-04 10:14:30 +0900804}
805
806int perf_session__cache_build_ids(struct perf_session *session)
807{
808 struct rb_node *nd;
809 int ret;
Namhyung Kime195fac2014-11-04 10:14:30 +0900810
Namhyung Kim73c5d222014-11-07 22:57:56 +0900811 if (no_buildid_cache)
812 return 0;
813
Jiri Olsa498922a2014-12-01 20:06:23 +0100814 if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
Namhyung Kime195fac2014-11-04 10:14:30 +0900815 return -1;
816
Masami Hiramatsue35f7362015-02-10 18:18:51 +0900817 ret = machine__cache_build_ids(&session->machines.host);
Namhyung Kime195fac2014-11-04 10:14:30 +0900818
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 Hiramatsue35f7362015-02-10 18:18:51 +0900821 ret |= machine__cache_build_ids(pos);
Namhyung Kime195fac2014-11-04 10:14:30 +0900822 }
823 return ret ? -1 : 0;
824}
825
826static bool machine__read_build_ids(struct machine *machine, bool with_hits)
827{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300828 return __dsos__read_build_ids(&machine->dsos.head, with_hits);
Namhyung Kime195fac2014-11-04 10:14:30 +0900829}
830
831bool 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}