blob: b346b8eba65c0ab63c7d6acdd35428535011b90f [file] [log] [blame]
Jiri Olsabda6ee42014-04-30 15:25:10 +02001#include <asm/bug.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03002#include <linux/kernel.h>
Jiri Olsac6580452014-04-30 15:47:27 +02003#include <sys/time.h>
4#include <sys/resource.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03005#include <sys/types.h>
6#include <sys/stat.h>
7#include <unistd.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03008#include <errno.h>
Arnaldo Carvalho de Melo611f0af2017-04-19 16:29:38 -03009#include "compress.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030010#include "path.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020011#include "symbol.h"
12#include "dso.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030013#include "machine.h"
Adrian Huntercfe91742015-04-09 18:53:55 +030014#include "auxtrace.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020015#include "util.h"
16#include "debug.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030017#include "string2.h"
He Kuang6ae98ba2016-05-12 08:43:11 +000018#include "vdso.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020019
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010020static const char * const debuglink_paths[] = {
21 "%.0s%s",
22 "%s/%s",
23 "%s/.debug/%s",
24 "/usr/lib/debug%s/%s"
25};
26
Jiri Olsacdd059d2012-10-27 23:18:32 +020027char dso__symtab_origin(const struct dso *dso)
28{
29 static const char origin[] = {
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020030 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
31 [DSO_BINARY_TYPE__VMLINUX] = 'v',
32 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
33 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
34 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
35 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
36 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
37 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
38 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
39 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
40 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090041 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020042 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
43 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090044 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020045 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Jiri Olsacdd059d2012-10-27 23:18:32 +020046 };
47
48 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
49 return '!';
50 return origin[dso->symtab_type];
51}
52
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -030053int dso__read_binary_type_filename(const struct dso *dso,
54 enum dso_binary_type type,
55 char *root_dir, char *filename, size_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +020056{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +090057 char build_id_hex[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +020058 int ret = 0;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030059 size_t len;
Jiri Olsacdd059d2012-10-27 23:18:32 +020060
61 switch (type) {
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010062 case DSO_BINARY_TYPE__DEBUGLINK:
63 {
64 const char *last_slash;
65 char dso_dir[PATH_MAX];
66 char symfile[PATH_MAX];
67 unsigned int i;
Jiri Olsacdd059d2012-10-27 23:18:32 +020068
Victor Kamenskydc6254c2015-01-26 22:34:02 -080069 len = __symbol__join_symfs(filename, size, dso->long_name);
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010070 last_slash = filename + len;
71 while (last_slash != filename && *last_slash != '/')
72 last_slash--;
Jiri Olsa40356722016-01-20 12:56:32 +010073
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010074 strncpy(dso_dir, filename, last_slash - filename);
75 dso_dir[last_slash-filename] = '\0';
76
77 if (!is_regular_file(filename)) {
78 ret = -1;
79 break;
80 }
81
82 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
83 if (ret)
Jiri Olsa40356722016-01-20 12:56:32 +010084 break;
85
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010086 /* Check predefined locations where debug file might reside */
87 ret = -1;
88 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
89 snprintf(filename, size,
90 debuglink_paths[i], dso_dir, symfile);
91 if (is_regular_file(filename)) {
92 ret = 0;
93 break;
94 }
Jiri Olsacdd059d2012-10-27 23:18:32 +020095 }
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010096
Jiri Olsacdd059d2012-10-27 23:18:32 +020097 break;
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010098 }
Jiri Olsacdd059d2012-10-27 23:18:32 +020099 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
He Kuanga7066702016-05-19 11:47:37 +0000100 if (dso__build_id_filename(dso, filename, size) == NULL)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200101 ret = -1;
102 break;
103
104 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300105 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
106 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200107 break;
108
109 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300110 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
111 snprintf(filename + len, size - len, "%s", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200112 break;
113
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200114 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
115 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300116 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200117 size_t dir_size;
118
119 last_slash = dso->long_name + dso->long_name_len;
120 while (last_slash != dso->long_name && *last_slash != '/')
121 last_slash--;
122
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300123 len = __symbol__join_symfs(filename, size, "");
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200124 dir_size = last_slash - dso->long_name + 2;
125 if (dir_size > (size - len)) {
126 ret = -1;
127 break;
128 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300129 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
130 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200131 last_slash);
132 break;
133 }
134
Jiri Olsacdd059d2012-10-27 23:18:32 +0200135 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
136 if (!dso->has_build_id) {
137 ret = -1;
138 break;
139 }
140
141 build_id__sprintf(dso->build_id,
142 sizeof(dso->build_id),
143 build_id_hex);
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300144 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
145 snprintf(filename + len, size - len, "%.2s/%s.debug",
146 build_id_hex, build_id_hex + 2);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200147 break;
148
Adrian Hunter39b12f782013-08-07 14:38:47 +0300149 case DSO_BINARY_TYPE__VMLINUX:
150 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200151 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300152 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200153 break;
154
155 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900156 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300157 path__join3(filename, size, symbol_conf.symfs,
158 root_dir, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200159 break;
160
161 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900162 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300163 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200164 break;
165
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300166 case DSO_BINARY_TYPE__KCORE:
167 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300168 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300169 break;
170
Jiri Olsacdd059d2012-10-27 23:18:32 +0200171 default:
172 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200173 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200174 case DSO_BINARY_TYPE__JAVA_JIT:
175 case DSO_BINARY_TYPE__NOT_FOUND:
176 ret = -1;
177 break;
178 }
179
180 return ret;
181}
182
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900183static const struct {
184 const char *fmt;
185 int (*decompress)(const char *input, int output);
186} compressions[] = {
Namhyung Kime92ce122014-10-31 16:51:38 +0900187#ifdef HAVE_ZLIB_SUPPORT
188 { "gz", gzip_decompress_to_file },
189#endif
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100190#ifdef HAVE_LZMA_SUPPORT
191 { "xz", lzma_decompress_to_file },
192#endif
Namhyung Kime92ce122014-10-31 16:51:38 +0900193 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900194};
195
196bool is_supported_compression(const char *ext)
197{
198 unsigned i;
199
200 for (i = 0; compressions[i].fmt; i++) {
201 if (!strcmp(ext, compressions[i].fmt))
202 return true;
203 }
204 return false;
205}
206
Wang Nan1f121b02015-06-03 08:52:21 +0000207bool is_kernel_module(const char *pathname, int cpumode)
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900208{
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100209 struct kmod_path m;
Wang Nan1f121b02015-06-03 08:52:21 +0000210 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900211
Wang Nan1f121b02015-06-03 08:52:21 +0000212 WARN_ONCE(mode != cpumode,
213 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
214 cpumode);
215
216 switch (mode) {
217 case PERF_RECORD_MISC_USER:
218 case PERF_RECORD_MISC_HYPERVISOR:
219 case PERF_RECORD_MISC_GUEST_USER:
220 return false;
221 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
222 default:
223 if (kmod_path__parse(&m, pathname)) {
224 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
225 pathname);
226 return true;
227 }
228 }
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900229
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100230 return m.kmod;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900231}
232
233bool decompress_to_file(const char *ext, const char *filename, int output_fd)
234{
235 unsigned i;
236
237 for (i = 0; compressions[i].fmt; i++) {
238 if (!strcmp(ext, compressions[i].fmt))
239 return !compressions[i].decompress(filename,
240 output_fd);
241 }
242 return false;
243}
244
245bool dso__needs_decompress(struct dso *dso)
246{
247 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
248 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
249}
250
Namhyung Kim42b3fa62017-06-08 16:31:03 +0900251static int decompress_kmodule(struct dso *dso, const char *name, char *tmpbuf)
252{
253 int fd = -1;
254 struct kmod_path m;
255
256 if (!dso__needs_decompress(dso))
257 return -1;
258
259 if (kmod_path__parse_ext(&m, dso->long_name))
260 return -1;
261
262 if (!m.comp)
263 goto out;
264
265 fd = mkstemp(tmpbuf);
266 if (fd < 0) {
267 dso->load_errno = errno;
268 goto out;
269 }
270
271 if (!decompress_to_file(m.ext, name, fd)) {
272 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
273 close(fd);
274 fd = -1;
275 }
276
277out:
278 free(m.ext);
279 return fd;
280}
281
282int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
283{
284 char tmpbuf[] = KMOD_DECOMP_NAME;
285 int fd;
286
287 fd = decompress_kmodule(dso, name, tmpbuf);
288 unlink(tmpbuf);
289 return fd;
290}
291
292int dso__decompress_kmodule_path(struct dso *dso, const char *name,
293 char *pathname, size_t len)
294{
295 char tmpbuf[] = KMOD_DECOMP_NAME;
296 int fd;
297
298 fd = decompress_kmodule(dso, name, tmpbuf);
299 if (fd < 0) {
300 unlink(tmpbuf);
301 return -1;
302 }
303
304 strncpy(pathname, tmpbuf, len);
305 close(fd);
306 return 0;
307}
308
Jiri Olsaeba51022014-04-30 15:00:59 +0200309/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100310 * Parses kernel module specified in @path and updates
311 * @m argument like:
312 *
313 * @comp - true if @path contains supported compression suffix,
314 * false otherwise
315 * @kmod - true if @path contains '.ko' suffix in right position,
316 * false otherwise
317 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
318 * of the kernel module without suffixes, otherwise strudup-ed
319 * base name of @path
320 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
321 * the compression suffix
322 *
323 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
324 */
325int __kmod_path__parse(struct kmod_path *m, const char *path,
326 bool alloc_name, bool alloc_ext)
327{
328 const char *name = strrchr(path, '/');
329 const char *ext = strrchr(path, '.');
Wang Nan1f121b02015-06-03 08:52:21 +0000330 bool is_simple_name = false;
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100331
332 memset(m, 0x0, sizeof(*m));
333 name = name ? name + 1 : path;
334
Wang Nan1f121b02015-06-03 08:52:21 +0000335 /*
336 * '.' is also a valid character for module name. For example:
337 * [aaa.bbb] is a valid module name. '[' should have higher
338 * priority than '.ko' suffix.
339 *
340 * The kernel names are from machine__mmap_name. Such
341 * name should belong to kernel itself, not kernel module.
342 */
343 if (name[0] == '[') {
344 is_simple_name = true;
345 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
346 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
347 (strncmp(name, "[vdso]", 6) == 0) ||
348 (strncmp(name, "[vsyscall]", 10) == 0)) {
349 m->kmod = false;
350
351 } else
352 m->kmod = true;
353 }
354
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100355 /* No extension, just return name. */
Wang Nan1f121b02015-06-03 08:52:21 +0000356 if ((ext == NULL) || is_simple_name) {
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100357 if (alloc_name) {
358 m->name = strdup(name);
359 return m->name ? 0 : -ENOMEM;
360 }
361 return 0;
362 }
363
364 if (is_supported_compression(ext + 1)) {
365 m->comp = true;
366 ext -= 3;
367 }
368
369 /* Check .ko extension only if there's enough name left. */
370 if (ext > name)
371 m->kmod = !strncmp(ext, ".ko", 3);
372
373 if (alloc_name) {
374 if (m->kmod) {
375 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
376 return -ENOMEM;
377 } else {
378 if (asprintf(&m->name, "%s", name) == -1)
379 return -ENOMEM;
380 }
381
382 strxfrchar(m->name, '-', '_');
383 }
384
385 if (alloc_ext && m->comp) {
386 m->ext = strdup(ext + 4);
387 if (!m->ext) {
388 free((void *) m->name);
389 return -ENOMEM;
390 }
391 }
392
393 return 0;
394}
395
Namhyung Kim6b335e82017-05-31 21:01:04 +0900396void dso__set_module_info(struct dso *dso, struct kmod_path *m,
397 struct machine *machine)
398{
399 if (machine__is_host(machine))
400 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
401 else
402 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
403
404 /* _KMODULE_COMP should be next to _KMODULE */
405 if (m->kmod && m->comp)
406 dso->symtab_type++;
407
408 dso__set_short_name(dso, strdup(m->name), true);
409}
410
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100411/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200412 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200413 */
414static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200415static long dso__data_open_cnt;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900416static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
Jiri Olsaeba51022014-04-30 15:00:59 +0200417
418static void dso__list_add(struct dso *dso)
419{
420 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200421 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200422}
423
424static void dso__list_del(struct dso *dso)
425{
426 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200427 WARN_ONCE(dso__data_open_cnt <= 0,
428 "DSO data fd counter out of bounds.");
429 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200430}
431
Jiri Olsaa08cae02014-05-07 21:35:02 +0200432static void close_first_dso(void);
433
434static int do_open(char *name)
435{
436 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000437 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200438
439 do {
440 fd = open(name, O_RDONLY);
441 if (fd >= 0)
442 return fd;
443
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900444 pr_debug("dso open failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300445 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200446 if (!dso__data_open_cnt || errno != EMFILE)
447 break;
448
449 close_first_dso();
450 } while (1);
451
452 return -1;
453}
454
Jiri Olsaeba51022014-04-30 15:00:59 +0200455static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200456{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200457 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300458 char *root_dir = (char *)"";
459 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200460
Jiri Olsacdd059d2012-10-27 23:18:32 +0200461 if (!name)
462 return -ENOMEM;
463
464 if (machine)
465 root_dir = machine->root_dir;
466
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300467 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300468 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200469 free(name);
470 return -EINVAL;
471 }
472
Namhyung Kim44ad6b82017-06-08 16:31:02 +0900473 if (!is_regular_file(name)) {
474 free(name);
Jiri Olsa3c028a02016-09-20 18:12:45 +0200475 return -EINVAL;
Namhyung Kim44ad6b82017-06-08 16:31:02 +0900476 }
Jiri Olsa3c028a02016-09-20 18:12:45 +0200477
Jiri Olsaa08cae02014-05-07 21:35:02 +0200478 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200479 free(name);
480 return fd;
481}
482
Jiri Olsac6580452014-04-30 15:47:27 +0200483static void check_data_close(void);
484
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200485/**
486 * dso_close - Open DSO data file
487 * @dso: dso object
488 *
489 * Open @dso's data file descriptor and updates
490 * list/count of open DSO objects.
491 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200492static int open_dso(struct dso *dso, struct machine *machine)
493{
494 int fd = __open_dso(dso, machine);
495
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300496 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200497 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200498 /*
499 * Check if we crossed the allowed number
500 * of opened DSOs and close one if needed.
501 */
502 check_data_close();
503 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200504
505 return fd;
506}
507
508static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200509{
510 if (dso->data.fd >= 0) {
511 close(dso->data.fd);
512 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200513 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200514 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200515 }
516}
517
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200518/**
519 * dso_close - Close DSO data file
520 * @dso: dso object
521 *
522 * Close @dso's data file descriptor and updates
523 * list/count of open DSO objects.
524 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200525static void close_dso(struct dso *dso)
526{
527 close_data_fd(dso);
528}
529
Jiri Olsac6580452014-04-30 15:47:27 +0200530static void close_first_dso(void)
531{
532 struct dso *dso;
533
534 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
535 close_dso(dso);
536}
537
538static rlim_t get_fd_limit(void)
539{
540 struct rlimit l;
541 rlim_t limit = 0;
542
543 /* Allow half of the current open fd limit. */
544 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
545 if (l.rlim_cur == RLIM_INFINITY)
546 limit = l.rlim_cur;
547 else
548 limit = l.rlim_cur / 2;
549 } else {
550 pr_err("failed to get fd limit\n");
551 limit = 1;
552 }
553
554 return limit;
555}
556
Jiri Olsaf3069242016-06-28 13:29:02 +0200557static rlim_t fd_limit;
558
559/*
560 * Used only by tests/dso-data.c to reset the environment
561 * for tests. I dont expect we should change this during
562 * standard runtime.
563 */
564void reset_fd_limit(void)
565{
566 fd_limit = 0;
567}
568
Jiri Olsac6580452014-04-30 15:47:27 +0200569static bool may_cache_fd(void)
570{
Jiri Olsaf3069242016-06-28 13:29:02 +0200571 if (!fd_limit)
572 fd_limit = get_fd_limit();
Jiri Olsac6580452014-04-30 15:47:27 +0200573
Jiri Olsaf3069242016-06-28 13:29:02 +0200574 if (fd_limit == RLIM_INFINITY)
Jiri Olsac6580452014-04-30 15:47:27 +0200575 return true;
576
Jiri Olsaf3069242016-06-28 13:29:02 +0200577 return fd_limit > (rlim_t) dso__data_open_cnt;
Jiri Olsac6580452014-04-30 15:47:27 +0200578}
579
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200580/*
581 * Check and close LRU dso if we crossed allowed limit
582 * for opened dso file descriptors. The limit is half
583 * of the RLIMIT_NOFILE files opened.
584*/
Jiri Olsac6580452014-04-30 15:47:27 +0200585static void check_data_close(void)
586{
587 bool cache_fd = may_cache_fd();
588
589 if (!cache_fd)
590 close_first_dso();
591}
592
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200593/**
594 * dso__data_close - Close DSO data file
595 * @dso: dso object
596 *
597 * External interface to close @dso's data file descriptor.
598 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200599void dso__data_close(struct dso *dso)
600{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900601 pthread_mutex_lock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200602 close_dso(dso);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900603 pthread_mutex_unlock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200604}
605
Namhyung Kim71ff8242015-05-21 01:03:39 +0900606static void try_to_open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200607{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300608 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200609 DSO_BINARY_TYPE__BUILD_ID_CACHE,
610 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
611 DSO_BINARY_TYPE__NOT_FOUND,
612 };
613 int i = 0;
614
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200615 if (dso->data.fd >= 0)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900616 return;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200617
618 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
619 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300620 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200621 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200622
623 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300624 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200625
Adrian Hunterc27697d2014-07-22 16:17:18 +0300626 dso->data.fd = open_dso(dso, machine);
627 if (dso->data.fd >= 0)
628 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200629
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300630 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300631out:
632 if (dso->data.fd >= 0)
633 dso->data.status = DSO_DATA_STATUS_OK;
634 else
635 dso->data.status = DSO_DATA_STATUS_ERROR;
Namhyung Kim71ff8242015-05-21 01:03:39 +0900636}
Jiri Olsacdd059d2012-10-27 23:18:32 +0200637
Namhyung Kim71ff8242015-05-21 01:03:39 +0900638/**
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900639 * dso__data_get_fd - Get dso's data file descriptor
Namhyung Kim71ff8242015-05-21 01:03:39 +0900640 * @dso: dso object
641 * @machine: machine object
642 *
643 * External interface to find dso's file, open it and
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900644 * returns file descriptor. It should be paired with
645 * dso__data_put_fd() if it returns non-negative value.
Namhyung Kim71ff8242015-05-21 01:03:39 +0900646 */
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900647int dso__data_get_fd(struct dso *dso, struct machine *machine)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900648{
649 if (dso->data.status == DSO_DATA_STATUS_ERROR)
650 return -1;
651
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900652 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
653 return -1;
654
Namhyung Kim71ff8242015-05-21 01:03:39 +0900655 try_to_open_dso(dso, machine);
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900656
657 if (dso->data.fd < 0)
658 pthread_mutex_unlock(&dso__data_open_lock);
Namhyung Kim71ff8242015-05-21 01:03:39 +0900659
Adrian Hunterc27697d2014-07-22 16:17:18 +0300660 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200661}
662
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900663void dso__data_put_fd(struct dso *dso __maybe_unused)
664{
665 pthread_mutex_unlock(&dso__data_open_lock);
666}
667
Adrian Hunter288be942014-07-22 16:17:19 +0300668bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
669{
670 u32 flag = 1 << by;
671
672 if (dso->data.status_seen & flag)
673 return true;
674
675 dso->data.status_seen |= flag;
676
677 return false;
678}
679
Jiri Olsacdd059d2012-10-27 23:18:32 +0200680static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900681dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200682{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900683 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200684 struct rb_node *next = rb_first(root);
685
Namhyung Kim8e67b722015-05-18 09:30:41 +0900686 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200687 while (next) {
688 struct dso_cache *cache;
689
690 cache = rb_entry(next, struct dso_cache, rb_node);
691 next = rb_next(&cache->rb_node);
692 rb_erase(&cache->rb_node, root);
693 free(cache);
694 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900695 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200696}
697
Namhyung Kim8e67b722015-05-18 09:30:41 +0900698static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200699{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900700 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300701 struct rb_node * const *p = &root->rb_node;
702 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200703 struct dso_cache *cache;
704
705 while (*p != NULL) {
706 u64 end;
707
708 parent = *p;
709 cache = rb_entry(parent, struct dso_cache, rb_node);
710 end = cache->offset + DSO__DATA_CACHE_SIZE;
711
712 if (offset < cache->offset)
713 p = &(*p)->rb_left;
714 else if (offset >= end)
715 p = &(*p)->rb_right;
716 else
717 return cache;
718 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900719
Jiri Olsacdd059d2012-10-27 23:18:32 +0200720 return NULL;
721}
722
Namhyung Kim8e67b722015-05-18 09:30:41 +0900723static struct dso_cache *
724dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200725{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900726 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200727 struct rb_node **p = &root->rb_node;
728 struct rb_node *parent = NULL;
729 struct dso_cache *cache;
730 u64 offset = new->offset;
731
Namhyung Kim8e67b722015-05-18 09:30:41 +0900732 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200733 while (*p != NULL) {
734 u64 end;
735
736 parent = *p;
737 cache = rb_entry(parent, struct dso_cache, rb_node);
738 end = cache->offset + DSO__DATA_CACHE_SIZE;
739
740 if (offset < cache->offset)
741 p = &(*p)->rb_left;
742 else if (offset >= end)
743 p = &(*p)->rb_right;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900744 else
745 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200746 }
747
748 rb_link_node(&new->rb_node, parent, p);
749 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900750
751 cache = NULL;
752out:
753 pthread_mutex_unlock(&dso->lock);
754 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200755}
756
757static ssize_t
758dso_cache__memcpy(struct dso_cache *cache, u64 offset,
759 u8 *data, u64 size)
760{
761 u64 cache_offset = offset - cache->offset;
762 u64 cache_size = min(cache->size - cache_offset, size);
763
764 memcpy(data, cache->data + cache_offset, cache_size);
765 return cache_size;
766}
767
768static ssize_t
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900769dso_cache__read(struct dso *dso, struct machine *machine,
770 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200771{
772 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900773 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200774 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200775
776 do {
777 u64 cache_offset;
778
Jiri Olsacdd059d2012-10-27 23:18:32 +0200779 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
780 if (!cache)
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900781 return -ENOMEM;
782
783 pthread_mutex_lock(&dso__data_open_lock);
784
785 /*
786 * dso->data.fd might be closed if other thread opened another
787 * file (dso) due to open file limit (RLIMIT_NOFILE).
788 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900789 try_to_open_dso(dso, machine);
790
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900791 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900792 ret = -errno;
793 dso->data.status = DSO_DATA_STATUS_ERROR;
794 break;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900795 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200796
797 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200798
Namhyung Kimc52686f2015-01-29 17:02:01 -0300799 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200800 if (ret <= 0)
801 break;
802
803 cache->offset = cache_offset;
804 cache->size = ret;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900805 } while (0);
806
807 pthread_mutex_unlock(&dso__data_open_lock);
808
809 if (ret > 0) {
Namhyung Kim8e67b722015-05-18 09:30:41 +0900810 old = dso_cache__insert(dso, cache);
811 if (old) {
812 /* we lose the race */
813 free(cache);
814 cache = old;
815 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200816
817 ret = dso_cache__memcpy(cache, offset, data, size);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900818 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200819
820 if (ret <= 0)
821 free(cache);
822
Jiri Olsacdd059d2012-10-27 23:18:32 +0200823 return ret;
824}
825
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900826static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
827 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200828{
829 struct dso_cache *cache;
830
Namhyung Kim8e67b722015-05-18 09:30:41 +0900831 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200832 if (cache)
833 return dso_cache__memcpy(cache, offset, data, size);
834 else
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900835 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200836}
837
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200838/*
839 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
840 * in the rb_tree. Any read to already cached data is served
841 * by cached data.
842 */
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900843static ssize_t cached_read(struct dso *dso, struct machine *machine,
844 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200845{
846 ssize_t r = 0;
847 u8 *p = data;
848
849 do {
850 ssize_t ret;
851
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900852 ret = dso_cache_read(dso, machine, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200853 if (ret < 0)
854 return ret;
855
856 /* Reached EOF, return what we have. */
857 if (!ret)
858 break;
859
860 BUG_ON(ret > size);
861
862 r += ret;
863 p += ret;
864 offset += ret;
865 size -= ret;
866
867 } while (size);
868
869 return r;
870}
871
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900872static int data_file_size(struct dso *dso, struct machine *machine)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200873{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900874 int ret = 0;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200875 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000876 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200877
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900878 if (dso->data.file_size)
879 return 0;
880
Namhyung Kim71ff8242015-05-21 01:03:39 +0900881 if (dso->data.status == DSO_DATA_STATUS_ERROR)
882 return -1;
883
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900884 pthread_mutex_lock(&dso__data_open_lock);
885
886 /*
887 * dso->data.fd might be closed if other thread opened another
888 * file (dso) due to open file limit (RLIMIT_NOFILE).
889 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900890 try_to_open_dso(dso, machine);
891
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900892 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900893 ret = -errno;
894 dso->data.status = DSO_DATA_STATUS_ERROR;
895 goto out;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200896 }
897
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900898 if (fstat(dso->data.fd, &st) < 0) {
899 ret = -errno;
900 pr_err("dso cache fstat failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300901 str_error_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900902 dso->data.status = DSO_DATA_STATUS_ERROR;
903 goto out;
904 }
905 dso->data.file_size = st.st_size;
906
907out:
908 pthread_mutex_unlock(&dso__data_open_lock);
909 return ret;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200910}
911
Adrian Hunter6d363452014-07-22 16:17:35 +0300912/**
913 * dso__data_size - Return dso data size
914 * @dso: dso object
915 * @machine: machine object
916 *
917 * Return: dso data size
918 */
919off_t dso__data_size(struct dso *dso, struct machine *machine)
920{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900921 if (data_file_size(dso, machine))
Adrian Hunter6d363452014-07-22 16:17:35 +0300922 return -1;
923
924 /* For now just estimate dso data size is close to file size */
925 return dso->data.file_size;
926}
927
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900928static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
929 u64 offset, u8 *data, ssize_t size)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200930{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900931 if (data_file_size(dso, machine))
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200932 return -1;
933
934 /* Check the offset sanity. */
935 if (offset > dso->data.file_size)
936 return -1;
937
938 if (offset + size < offset)
939 return -1;
940
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900941 return cached_read(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200942}
943
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200944/**
945 * dso__data_read_offset - Read data from dso file offset
946 * @dso: dso object
947 * @machine: machine object
948 * @offset: file offset
949 * @data: buffer to store data
950 * @size: size of the @data buffer
951 *
952 * External interface to read data from dso file offset. Open
953 * dso data file and use cached_read to get the data.
954 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200955ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
956 u64 offset, u8 *data, ssize_t size)
957{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900958 if (dso->data.status == DSO_DATA_STATUS_ERROR)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200959 return -1;
960
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900961 return data_read_offset(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200962}
963
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200964/**
965 * dso__data_read_addr - Read data from dso address
966 * @dso: dso object
967 * @machine: machine object
968 * @add: virtual memory address
969 * @data: buffer to store data
970 * @size: size of the @data buffer
971 *
972 * External interface to read data from dso address.
973 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200974ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
975 struct machine *machine, u64 addr,
976 u8 *data, ssize_t size)
977{
978 u64 offset = map->map_ip(map, addr);
979 return dso__data_read_offset(dso, machine, offset, data, size);
980}
981
982struct map *dso__new_map(const char *name)
983{
984 struct map *map = NULL;
985 struct dso *dso = dso__new(name);
986
987 if (dso)
988 map = map__new2(0, dso, MAP__FUNCTION);
989
990 return map;
991}
992
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300993struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
994 const char *short_name, int dso_type)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200995{
996 /*
997 * The kernel dso could be created by build_id processing.
998 */
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300999 struct dso *dso = machine__findnew_dso(machine, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001000
1001 /*
1002 * We need to run this in all cases, since during the build_id
1003 * processing we had no idea this was the kernel dso.
1004 */
1005 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -03001006 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001007 dso->kernel = dso_type;
1008 }
1009
1010 return dso;
1011}
1012
Waiman Long4598a0a2014-09-30 13:36:15 -04001013/*
1014 * Find a matching entry and/or link current entry to RB tree.
1015 * Either one of the dso or name parameter must be non-NULL or the
1016 * function will not work.
1017 */
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001018static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1019 struct dso *dso, const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -04001020{
1021 struct rb_node **p = &root->rb_node;
1022 struct rb_node *parent = NULL;
1023
1024 if (!name)
1025 name = dso->long_name;
1026 /*
1027 * Find node with the matching name
1028 */
1029 while (*p) {
1030 struct dso *this = rb_entry(*p, struct dso, rb_node);
1031 int rc = strcmp(name, this->long_name);
1032
1033 parent = *p;
1034 if (rc == 0) {
1035 /*
1036 * In case the new DSO is a duplicate of an existing
Masahiro Yamada0f5e1552017-02-27 14:28:52 -08001037 * one, print a one-time warning & put the new entry
Waiman Long4598a0a2014-09-30 13:36:15 -04001038 * at the end of the list of duplicates.
1039 */
1040 if (!dso || (dso == this))
1041 return this; /* Find matching dso */
1042 /*
1043 * The core kernel DSOs may have duplicated long name.
1044 * In this case, the short name should be different.
1045 * Comparing the short names to differentiate the DSOs.
1046 */
1047 rc = strcmp(dso->short_name, this->short_name);
1048 if (rc == 0) {
1049 pr_err("Duplicated dso name: %s\n", name);
1050 return NULL;
1051 }
1052 }
1053 if (rc < 0)
1054 p = &parent->rb_left;
1055 else
1056 p = &parent->rb_right;
1057 }
1058 if (dso) {
1059 /* Add new node and rebalance tree */
1060 rb_link_node(&dso->rb_node, parent, p);
1061 rb_insert_color(&dso->rb_node, root);
Adrian Huntere266a752015-11-13 11:48:30 +02001062 dso->root = root;
Waiman Long4598a0a2014-09-30 13:36:15 -04001063 }
1064 return NULL;
1065}
1066
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001067static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1068 const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -04001069{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001070 return __dso__findlink_by_longname(root, NULL, name);
Waiman Long4598a0a2014-09-30 13:36:15 -04001071}
1072
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001073void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001074{
Adrian Huntere266a752015-11-13 11:48:30 +02001075 struct rb_root *root = dso->root;
1076
Jiri Olsacdd059d2012-10-27 23:18:32 +02001077 if (name == NULL)
1078 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001079
1080 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001081 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001082
Adrian Huntere266a752015-11-13 11:48:30 +02001083 if (root) {
1084 rb_erase(&dso->rb_node, root);
1085 /*
1086 * __dso__findlink_by_longname() isn't guaranteed to add it
1087 * back, so a clean removal is required here.
1088 */
1089 RB_CLEAR_NODE(&dso->rb_node);
1090 dso->root = NULL;
1091 }
1092
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001093 dso->long_name = name;
1094 dso->long_name_len = strlen(name);
1095 dso->long_name_allocated = name_allocated;
Adrian Huntere266a752015-11-13 11:48:30 +02001096
1097 if (root)
1098 __dso__findlink_by_longname(root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001099}
1100
Adrian Hunter58a98c92013-12-10 11:11:46 -03001101void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001102{
1103 if (name == NULL)
1104 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -03001105
1106 if (dso->short_name_allocated)
1107 free((char *)dso->short_name);
1108
1109 dso->short_name = name;
1110 dso->short_name_len = strlen(name);
1111 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001112}
1113
1114static void dso__set_basename(struct dso *dso)
1115{
Stephane Eranianac5e7f82013-12-05 19:26:42 +01001116 /*
1117 * basename() may modify path buffer, so we must pass
1118 * a copy.
1119 */
1120 char *base, *lname = strdup(dso->long_name);
1121
1122 if (!lname)
1123 return;
1124
1125 /*
1126 * basename() may return a pointer to internal
1127 * storage which is reused in subsequent calls
1128 * so copy the result.
1129 */
1130 base = strdup(basename(lname));
1131
1132 free(lname);
1133
1134 if (!base)
1135 return;
1136
1137 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001138}
1139
1140int dso__name_len(const struct dso *dso)
1141{
1142 if (!dso)
1143 return strlen("[unknown]");
Namhyung Kimbb963e12017-02-17 17:17:38 +09001144 if (verbose > 0)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001145 return dso->long_name_len;
1146
1147 return dso->short_name_len;
1148}
1149
1150bool dso__loaded(const struct dso *dso, enum map_type type)
1151{
1152 return dso->loaded & (1 << type);
1153}
1154
1155bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1156{
1157 return dso->sorted_by_name & (1 << type);
1158}
1159
1160void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1161{
1162 dso->sorted_by_name |= (1 << type);
1163}
1164
1165struct dso *dso__new(const char *name)
1166{
1167 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1168
1169 if (dso != NULL) {
1170 int i;
1171 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001172 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -03001173 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001174 for (i = 0; i < MAP__NR_TYPES; ++i)
1175 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +02001176 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001177 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +03001178 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001179 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001180 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001181 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001182 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001183 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001184 dso->sorted_by_name = 0;
1185 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +09001186 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +02001187 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001188 dso->kernel = DSO_TYPE_USER;
1189 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -04001190 RB_CLEAR_NODE(&dso->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +02001191 dso->root = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001192 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +02001193 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001194 pthread_mutex_init(&dso->lock, NULL);
Elena Reshetova71008102017-02-21 17:34:58 +02001195 refcount_set(&dso->refcnt, 1);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001196 }
1197
1198 return dso;
1199}
1200
1201void dso__delete(struct dso *dso)
1202{
1203 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -04001204
1205 if (!RB_EMPTY_NODE(&dso->rb_node))
1206 pr_err("DSO %s is still in rbtree when being deleted!\n",
1207 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001208 for (i = 0; i < MAP__NR_TYPES; ++i)
1209 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001210
1211 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001212 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001213 dso->short_name_allocated = false;
1214 }
1215
1216 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001217 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001218 dso->long_name_allocated = false;
1219 }
1220
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001221 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +03001222 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +09001223 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +02001224 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001225 zfree(&dso->symsrc_filename);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001226 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001227 free(dso);
1228}
1229
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001230struct dso *dso__get(struct dso *dso)
1231{
1232 if (dso)
Elena Reshetova71008102017-02-21 17:34:58 +02001233 refcount_inc(&dso->refcnt);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001234 return dso;
1235}
1236
1237void dso__put(struct dso *dso)
1238{
Elena Reshetova71008102017-02-21 17:34:58 +02001239 if (dso && refcount_dec_and_test(&dso->refcnt))
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001240 dso__delete(dso);
1241}
1242
Jiri Olsacdd059d2012-10-27 23:18:32 +02001243void dso__set_build_id(struct dso *dso, void *build_id)
1244{
1245 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1246 dso->has_build_id = 1;
1247}
1248
1249bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1250{
1251 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1252}
1253
1254void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1255{
1256 char path[PATH_MAX];
1257
1258 if (machine__is_default_guest(machine))
1259 return;
1260 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1261 if (sysfs__read_build_id(path, dso->build_id,
1262 sizeof(dso->build_id)) == 0)
1263 dso->has_build_id = true;
1264}
1265
1266int dso__kernel_module_get_build_id(struct dso *dso,
1267 const char *root_dir)
1268{
1269 char filename[PATH_MAX];
1270 /*
1271 * kernel module short names are of the form "[module]" and
1272 * we need just "module" here.
1273 */
1274 const char *name = dso->short_name + 1;
1275
1276 snprintf(filename, sizeof(filename),
1277 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1278 root_dir, (int)strlen(name) - 1, name);
1279
1280 if (sysfs__read_build_id(filename, dso->build_id,
1281 sizeof(dso->build_id)) == 0)
1282 dso->has_build_id = true;
1283
1284 return 0;
1285}
1286
1287bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1288{
1289 bool have_build_id = false;
1290 struct dso *pos;
1291
1292 list_for_each_entry(pos, head, node) {
He Kuang6ae98ba2016-05-12 08:43:11 +00001293 if (with_hits && !pos->hit && !dso__is_vdso(pos))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001294 continue;
1295 if (pos->has_build_id) {
1296 have_build_id = true;
1297 continue;
1298 }
1299 if (filename__read_build_id(pos->long_name, pos->build_id,
1300 sizeof(pos->build_id)) > 0) {
1301 have_build_id = true;
1302 pos->has_build_id = true;
1303 }
1304 }
1305
1306 return have_build_id;
1307}
1308
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001309void __dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001310{
Waiman Long8fa7d872014-09-29 16:07:28 -04001311 list_add_tail(&dso->node, &dsos->head);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001312 __dso__findlink_by_longname(&dsos->root, dso, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001313 /*
1314 * It is now in the linked list, grab a reference, then garbage collect
1315 * this when needing memory, by looking at LRU dso instances in the
1316 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1317 * anywhere besides the one for the list, do, under a lock for the
1318 * list: remove it from the list, then a dso__put(), that probably will
1319 * be the last and will then call dso__delete(), end of life.
1320 *
1321 * That, or at the end of the 'struct machine' lifetime, when all
1322 * 'struct dso' instances will be removed from the list, in
1323 * dsos__exit(), if they have no other reference from some other data
1324 * structure.
1325 *
1326 * E.g.: after processing a 'perf.data' file and storing references
1327 * to objects instantiated while processing events, we will have
1328 * references to the 'thread', 'map', 'dso' structs all from 'struct
1329 * hist_entry' instances, but we may not need anything not referenced,
1330 * so we might as well call machines__exit()/machines__delete() and
1331 * garbage collect it.
1332 */
1333 dso__get(dso);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001334}
1335
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001336void dsos__add(struct dsos *dsos, struct dso *dso)
1337{
1338 pthread_rwlock_wrlock(&dsos->lock);
1339 __dsos__add(dsos, dso);
1340 pthread_rwlock_unlock(&dsos->lock);
1341}
1342
1343struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001344{
1345 struct dso *pos;
1346
Waiman Longf9ceffb2013-05-09 10:42:48 -04001347 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001348 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001349 if (strcmp(pos->short_name, name) == 0)
1350 return pos;
1351 return NULL;
1352 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001353 return __dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001354}
1355
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001356struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1357{
1358 struct dso *dso;
1359 pthread_rwlock_rdlock(&dsos->lock);
1360 dso = __dsos__find(dsos, name, cmp_short);
1361 pthread_rwlock_unlock(&dsos->lock);
1362 return dso;
1363}
1364
1365struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
Jiri Olsa701d8d72015-02-12 22:06:09 +01001366{
1367 struct dso *dso = dso__new(name);
1368
1369 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001370 __dsos__add(dsos, dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001371 dso__set_basename(dso);
Masami Hiramatsu82de26a2015-11-18 15:40:31 +09001372 /* Put dso here because __dsos_add already got it */
1373 dso__put(dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001374 }
1375 return dso;
1376}
1377
Waiman Long8fa7d872014-09-29 16:07:28 -04001378struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001379{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001380 struct dso *dso = __dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001381
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001382 return dso ? dso : __dsos__addnew(dsos, name);
1383}
1384
1385struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1386{
1387 struct dso *dso;
1388 pthread_rwlock_wrlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001389 dso = dso__get(__dsos__findnew(dsos, name));
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001390 pthread_rwlock_unlock(&dsos->lock);
1391 return dso;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001392}
1393
1394size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001395 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001396{
1397 struct dso *pos;
1398 size_t ret = 0;
1399
1400 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001401 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001402 continue;
1403 ret += dso__fprintf_buildid(pos, fp);
1404 ret += fprintf(fp, " %s\n", pos->long_name);
1405 }
1406 return ret;
1407}
1408
1409size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1410{
1411 struct dso *pos;
1412 size_t ret = 0;
1413
1414 list_for_each_entry(pos, head, node) {
1415 int i;
1416 for (i = 0; i < MAP__NR_TYPES; ++i)
1417 ret += dso__fprintf(pos, i, fp);
1418 }
1419
1420 return ret;
1421}
1422
1423size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1424{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001425 char sbuild_id[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +02001426
1427 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1428 return fprintf(fp, "%s", sbuild_id);
1429}
1430
1431size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1432{
1433 struct rb_node *nd;
1434 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1435
1436 if (dso->short_name != dso->long_name)
1437 ret += fprintf(fp, "%s, ", dso->long_name);
1438 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001439 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001440 ret += dso__fprintf_buildid(dso, fp);
1441 ret += fprintf(fp, ")\n");
1442 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1443 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1444 ret += symbol__fprintf(pos, fp);
1445 }
1446
1447 return ret;
1448}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001449
1450enum dso_type dso__type(struct dso *dso, struct machine *machine)
1451{
1452 int fd;
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001453 enum dso_type type = DSO__TYPE_UNKNOWN;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001454
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001455 fd = dso__data_get_fd(dso, machine);
1456 if (fd >= 0) {
1457 type = dso__type_fd(fd);
1458 dso__data_put_fd(dso);
1459 }
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001460
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001461 return type;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001462}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001463
1464int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1465{
1466 int idx, errnum = dso->load_errno;
1467 /*
1468 * This must have a same ordering as the enum dso_load_errno.
1469 */
1470 static const char *dso_load__error_str[] = {
1471 "Internal tools/perf/ library error",
1472 "Invalid ELF file",
1473 "Can not read build id",
1474 "Mismatching build id",
1475 "Decompression failure",
1476 };
1477
1478 BUG_ON(buflen == 0);
1479
1480 if (errnum >= 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001481 const char *err = str_error_r(errnum, buf, buflen);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001482
1483 if (err != buf)
1484 scnprintf(buf, buflen, "%s", err);
1485
1486 return 0;
1487 }
1488
1489 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1490 return -1;
1491
1492 idx = errnum - __DSO_LOAD_ERRNO__START;
1493 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1494 return 0;
1495}