blob: 40b5f656ebc39cdbee35118d4e61cf5fd41ae55e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsabda6ee42014-04-30 15:25:10 +02002#include <asm/bug.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03003#include <linux/kernel.h>
Jiri Olsac6580452014-04-30 15:47:27 +02004#include <sys/time.h>
5#include <sys/resource.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03006#include <sys/types.h>
7#include <sys/stat.h>
8#include <unistd.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03009#include <errno.h>
Arnaldo Carvalho de Melo611f0af2017-04-19 16:29:38 -030010#include "compress.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030011#include "path.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020012#include "symbol.h"
13#include "dso.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030014#include "machine.h"
Adrian Huntercfe91742015-04-09 18:53:55 +030015#include "auxtrace.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020016#include "util.h"
17#include "debug.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030018#include "string2.h"
He Kuang6ae98ba2016-05-12 08:43:11 +000019#include "vdso.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020020
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010021static const char * const debuglink_paths[] = {
22 "%.0s%s",
23 "%s/%s",
24 "%s/.debug/%s",
25 "/usr/lib/debug%s/%s"
26};
27
Jiri Olsacdd059d2012-10-27 23:18:32 +020028char dso__symtab_origin(const struct dso *dso)
29{
30 static const char origin[] = {
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020031 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
32 [DSO_BINARY_TYPE__VMLINUX] = 'v',
33 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
34 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
35 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
Krister Johansend2396992017-07-05 18:48:13 -070036 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020037 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
38 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
Adrian Hunterdf5bd932020-05-26 18:52:07 +030039 [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020040 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
41 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
42 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
43 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090044 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020045 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
46 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090047 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020048 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Jiri Olsacdd059d2012-10-27 23:18:32 +020049 };
50
51 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
52 return '!';
53 return origin[dso->symtab_type];
54}
55
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -030056int dso__read_binary_type_filename(const struct dso *dso,
57 enum dso_binary_type type,
58 char *root_dir, char *filename, size_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +020059{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +090060 char build_id_hex[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +020061 int ret = 0;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030062 size_t len;
Jiri Olsacdd059d2012-10-27 23:18:32 +020063
64 switch (type) {
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010065 case DSO_BINARY_TYPE__DEBUGLINK:
66 {
67 const char *last_slash;
68 char dso_dir[PATH_MAX];
69 char symfile[PATH_MAX];
70 unsigned int i;
Jiri Olsacdd059d2012-10-27 23:18:32 +020071
Victor Kamenskydc6254c2015-01-26 22:34:02 -080072 len = __symbol__join_symfs(filename, size, dso->long_name);
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010073 last_slash = filename + len;
74 while (last_slash != filename && *last_slash != '/')
75 last_slash--;
Jiri Olsa40356722016-01-20 12:56:32 +010076
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010077 strncpy(dso_dir, filename, last_slash - filename);
78 dso_dir[last_slash-filename] = '\0';
79
80 if (!is_regular_file(filename)) {
81 ret = -1;
82 break;
83 }
84
85 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
86 if (ret)
Jiri Olsa40356722016-01-20 12:56:32 +010087 break;
88
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010089 /* Check predefined locations where debug file might reside */
90 ret = -1;
91 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
92 snprintf(filename, size,
93 debuglink_paths[i], dso_dir, symfile);
94 if (is_regular_file(filename)) {
95 ret = 0;
96 break;
97 }
Jiri Olsacdd059d2012-10-27 23:18:32 +020098 }
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010099
Jiri Olsacdd059d2012-10-27 23:18:32 +0200100 break;
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +0100101 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200102 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
Krister Johansend2396992017-07-05 18:48:13 -0700103 if (dso__build_id_filename(dso, filename, size, false) == NULL)
104 ret = -1;
105 break;
106
107 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
108 if (dso__build_id_filename(dso, filename, size, true) == NULL)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200109 ret = -1;
110 break;
111
112 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300113 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
114 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200115 break;
116
117 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300118 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
119 snprintf(filename + len, size - len, "%s", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200120 break;
121
Adrian Hunterdf5bd932020-05-26 18:52:07 +0300122 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
123 /*
124 * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
125 * /usr/lib/debug/lib when it is expected to be in
126 * /usr/lib/debug/usr/lib
127 */
128 if (strlen(dso->long_name) < 9 ||
129 strncmp(dso->long_name, "/usr/lib/", 9)) {
130 ret = -1;
131 break;
132 }
133 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
134 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
135 break;
136
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200137 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
138 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300139 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200140 size_t dir_size;
141
142 last_slash = dso->long_name + dso->long_name_len;
143 while (last_slash != dso->long_name && *last_slash != '/')
144 last_slash--;
145
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300146 len = __symbol__join_symfs(filename, size, "");
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200147 dir_size = last_slash - dso->long_name + 2;
148 if (dir_size > (size - len)) {
149 ret = -1;
150 break;
151 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300152 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
153 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200154 last_slash);
155 break;
156 }
157
Jiri Olsacdd059d2012-10-27 23:18:32 +0200158 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
159 if (!dso->has_build_id) {
160 ret = -1;
161 break;
162 }
163
164 build_id__sprintf(dso->build_id,
165 sizeof(dso->build_id),
166 build_id_hex);
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300167 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
168 snprintf(filename + len, size - len, "%.2s/%s.debug",
169 build_id_hex, build_id_hex + 2);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200170 break;
171
Adrian Hunter39b12f782013-08-07 14:38:47 +0300172 case DSO_BINARY_TYPE__VMLINUX:
173 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200174 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300175 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200176 break;
177
178 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900179 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300180 path__join3(filename, size, symbol_conf.symfs,
181 root_dir, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200182 break;
183
184 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900185 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300186 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200187 break;
188
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300189 case DSO_BINARY_TYPE__KCORE:
190 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300191 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300192 break;
193
Jiri Olsacdd059d2012-10-27 23:18:32 +0200194 default:
195 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200196 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200197 case DSO_BINARY_TYPE__JAVA_JIT:
198 case DSO_BINARY_TYPE__NOT_FOUND:
199 ret = -1;
200 break;
201 }
202
203 return ret;
204}
205
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900206static const struct {
207 const char *fmt;
208 int (*decompress)(const char *input, int output);
209} compressions[] = {
Namhyung Kime92ce122014-10-31 16:51:38 +0900210#ifdef HAVE_ZLIB_SUPPORT
211 { "gz", gzip_decompress_to_file },
212#endif
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100213#ifdef HAVE_LZMA_SUPPORT
214 { "xz", lzma_decompress_to_file },
215#endif
Namhyung Kime92ce122014-10-31 16:51:38 +0900216 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900217};
218
219bool is_supported_compression(const char *ext)
220{
221 unsigned i;
222
223 for (i = 0; compressions[i].fmt; i++) {
224 if (!strcmp(ext, compressions[i].fmt))
225 return true;
226 }
227 return false;
228}
229
Wang Nan1f121b02015-06-03 08:52:21 +0000230bool is_kernel_module(const char *pathname, int cpumode)
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900231{
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100232 struct kmod_path m;
Wang Nan1f121b02015-06-03 08:52:21 +0000233 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900234
Wang Nan1f121b02015-06-03 08:52:21 +0000235 WARN_ONCE(mode != cpumode,
236 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
237 cpumode);
238
239 switch (mode) {
240 case PERF_RECORD_MISC_USER:
241 case PERF_RECORD_MISC_HYPERVISOR:
242 case PERF_RECORD_MISC_GUEST_USER:
243 return false;
244 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
245 default:
246 if (kmod_path__parse(&m, pathname)) {
247 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
248 pathname);
249 return true;
250 }
251 }
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900252
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100253 return m.kmod;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900254}
255
256bool decompress_to_file(const char *ext, const char *filename, int output_fd)
257{
258 unsigned i;
259
260 for (i = 0; compressions[i].fmt; i++) {
261 if (!strcmp(ext, compressions[i].fmt))
262 return !compressions[i].decompress(filename,
263 output_fd);
264 }
265 return false;
266}
267
268bool dso__needs_decompress(struct dso *dso)
269{
270 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
271 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
272}
273
Namhyung Kim42b3fa62017-06-08 16:31:03 +0900274static int decompress_kmodule(struct dso *dso, const char *name, char *tmpbuf)
275{
276 int fd = -1;
277 struct kmod_path m;
278
279 if (!dso__needs_decompress(dso))
280 return -1;
281
282 if (kmod_path__parse_ext(&m, dso->long_name))
283 return -1;
284
285 if (!m.comp)
286 goto out;
287
288 fd = mkstemp(tmpbuf);
289 if (fd < 0) {
290 dso->load_errno = errno;
291 goto out;
292 }
293
294 if (!decompress_to_file(m.ext, name, fd)) {
295 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
296 close(fd);
297 fd = -1;
298 }
299
300out:
301 free(m.ext);
302 return fd;
303}
304
305int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
306{
307 char tmpbuf[] = KMOD_DECOMP_NAME;
308 int fd;
309
310 fd = decompress_kmodule(dso, name, tmpbuf);
311 unlink(tmpbuf);
312 return fd;
313}
314
315int dso__decompress_kmodule_path(struct dso *dso, const char *name,
316 char *pathname, size_t len)
317{
318 char tmpbuf[] = KMOD_DECOMP_NAME;
319 int fd;
320
321 fd = decompress_kmodule(dso, name, tmpbuf);
322 if (fd < 0) {
323 unlink(tmpbuf);
324 return -1;
325 }
326
327 strncpy(pathname, tmpbuf, len);
328 close(fd);
329 return 0;
330}
331
Jiri Olsaeba51022014-04-30 15:00:59 +0200332/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100333 * Parses kernel module specified in @path and updates
334 * @m argument like:
335 *
336 * @comp - true if @path contains supported compression suffix,
337 * false otherwise
338 * @kmod - true if @path contains '.ko' suffix in right position,
339 * false otherwise
340 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
341 * of the kernel module without suffixes, otherwise strudup-ed
342 * base name of @path
343 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
344 * the compression suffix
345 *
346 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
347 */
348int __kmod_path__parse(struct kmod_path *m, const char *path,
349 bool alloc_name, bool alloc_ext)
350{
351 const char *name = strrchr(path, '/');
352 const char *ext = strrchr(path, '.');
Wang Nan1f121b02015-06-03 08:52:21 +0000353 bool is_simple_name = false;
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100354
355 memset(m, 0x0, sizeof(*m));
356 name = name ? name + 1 : path;
357
Wang Nan1f121b02015-06-03 08:52:21 +0000358 /*
359 * '.' is also a valid character for module name. For example:
360 * [aaa.bbb] is a valid module name. '[' should have higher
361 * priority than '.ko' suffix.
362 *
363 * The kernel names are from machine__mmap_name. Such
364 * name should belong to kernel itself, not kernel module.
365 */
366 if (name[0] == '[') {
367 is_simple_name = true;
368 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
369 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
370 (strncmp(name, "[vdso]", 6) == 0) ||
Adrian Hunter5009bd52018-06-04 15:56:54 +0300371 (strncmp(name, "[vdso32]", 8) == 0) ||
372 (strncmp(name, "[vdsox32]", 9) == 0) ||
Wang Nan1f121b02015-06-03 08:52:21 +0000373 (strncmp(name, "[vsyscall]", 10) == 0)) {
374 m->kmod = false;
375
376 } else
377 m->kmod = true;
378 }
379
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100380 /* No extension, just return name. */
Wang Nan1f121b02015-06-03 08:52:21 +0000381 if ((ext == NULL) || is_simple_name) {
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100382 if (alloc_name) {
383 m->name = strdup(name);
384 return m->name ? 0 : -ENOMEM;
385 }
386 return 0;
387 }
388
389 if (is_supported_compression(ext + 1)) {
390 m->comp = true;
391 ext -= 3;
392 }
393
394 /* Check .ko extension only if there's enough name left. */
395 if (ext > name)
396 m->kmod = !strncmp(ext, ".ko", 3);
397
398 if (alloc_name) {
399 if (m->kmod) {
400 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
401 return -ENOMEM;
402 } else {
403 if (asprintf(&m->name, "%s", name) == -1)
404 return -ENOMEM;
405 }
406
407 strxfrchar(m->name, '-', '_');
408 }
409
410 if (alloc_ext && m->comp) {
411 m->ext = strdup(ext + 4);
412 if (!m->ext) {
413 free((void *) m->name);
414 return -ENOMEM;
415 }
416 }
417
418 return 0;
419}
420
Namhyung Kim6b335e82017-05-31 21:01:04 +0900421void dso__set_module_info(struct dso *dso, struct kmod_path *m,
422 struct machine *machine)
423{
424 if (machine__is_host(machine))
425 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
426 else
427 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
428
429 /* _KMODULE_COMP should be next to _KMODULE */
430 if (m->kmod && m->comp)
431 dso->symtab_type++;
432
433 dso__set_short_name(dso, strdup(m->name), true);
434}
435
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100436/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200437 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200438 */
439static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200440static long dso__data_open_cnt;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900441static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
Jiri Olsaeba51022014-04-30 15:00:59 +0200442
443static void dso__list_add(struct dso *dso)
444{
445 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200446 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200447}
448
449static void dso__list_del(struct dso *dso)
450{
451 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200452 WARN_ONCE(dso__data_open_cnt <= 0,
453 "DSO data fd counter out of bounds.");
454 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200455}
456
Jiri Olsaa08cae02014-05-07 21:35:02 +0200457static void close_first_dso(void);
458
459static int do_open(char *name)
460{
461 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000462 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200463
464 do {
465 fd = open(name, O_RDONLY);
466 if (fd >= 0)
467 return fd;
468
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900469 pr_debug("dso open failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300470 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200471 if (!dso__data_open_cnt || errno != EMFILE)
472 break;
473
474 close_first_dso();
475 } while (1);
476
477 return -1;
478}
479
Jiri Olsaeba51022014-04-30 15:00:59 +0200480static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200481{
Namhyung Kim8ba29ad2017-06-08 16:31:06 +0900482 int fd = -EINVAL;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300483 char *root_dir = (char *)"";
484 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200485
Jiri Olsacdd059d2012-10-27 23:18:32 +0200486 if (!name)
487 return -ENOMEM;
488
489 if (machine)
490 root_dir = machine->root_dir;
491
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300492 if (dso__read_binary_type_filename(dso, dso->binary_type,
Namhyung Kim8ba29ad2017-06-08 16:31:06 +0900493 root_dir, name, PATH_MAX))
494 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200495
Namhyung Kim8ba29ad2017-06-08 16:31:06 +0900496 if (!is_regular_file(name))
497 goto out;
Jiri Olsa3c028a02016-09-20 18:12:45 +0200498
Namhyung Kim1d6b3c92017-06-08 16:31:05 +0900499 if (dso__needs_decompress(dso)) {
500 char newpath[KMOD_DECOMP_LEN];
501 size_t len = sizeof(newpath);
502
503 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
Namhyung Kim8ba29ad2017-06-08 16:31:06 +0900504 fd = -dso->load_errno;
505 goto out;
Namhyung Kim1d6b3c92017-06-08 16:31:05 +0900506 }
507
508 strcpy(name, newpath);
509 }
510
Jiri Olsaa08cae02014-05-07 21:35:02 +0200511 fd = do_open(name);
Namhyung Kim1d6b3c92017-06-08 16:31:05 +0900512
513 if (dso__needs_decompress(dso))
514 unlink(name);
515
Namhyung Kim8ba29ad2017-06-08 16:31:06 +0900516out:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200517 free(name);
518 return fd;
519}
520
Jiri Olsac6580452014-04-30 15:47:27 +0200521static void check_data_close(void);
522
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200523/**
524 * dso_close - Open DSO data file
525 * @dso: dso object
526 *
527 * Open @dso's data file descriptor and updates
528 * list/count of open DSO objects.
529 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200530static int open_dso(struct dso *dso, struct machine *machine)
531{
Krister Johansenf045b8c2017-07-05 18:48:11 -0700532 int fd;
533 struct nscookie nsc;
534
535 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
536 nsinfo__mountns_enter(dso->nsinfo, &nsc);
537 fd = __open_dso(dso, machine);
538 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
539 nsinfo__mountns_exit(&nsc);
Jiri Olsaeba51022014-04-30 15:00:59 +0200540
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300541 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200542 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200543 /*
544 * Check if we crossed the allowed number
545 * of opened DSOs and close one if needed.
546 */
547 check_data_close();
548 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200549
550 return fd;
551}
552
553static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200554{
555 if (dso->data.fd >= 0) {
556 close(dso->data.fd);
557 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200558 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200559 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200560 }
561}
562
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200563/**
564 * dso_close - Close DSO data file
565 * @dso: dso object
566 *
567 * Close @dso's data file descriptor and updates
568 * list/count of open DSO objects.
569 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200570static void close_dso(struct dso *dso)
571{
572 close_data_fd(dso);
573}
574
Jiri Olsac6580452014-04-30 15:47:27 +0200575static void close_first_dso(void)
576{
577 struct dso *dso;
578
579 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
580 close_dso(dso);
581}
582
583static rlim_t get_fd_limit(void)
584{
585 struct rlimit l;
586 rlim_t limit = 0;
587
588 /* Allow half of the current open fd limit. */
589 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
590 if (l.rlim_cur == RLIM_INFINITY)
591 limit = l.rlim_cur;
592 else
593 limit = l.rlim_cur / 2;
594 } else {
595 pr_err("failed to get fd limit\n");
596 limit = 1;
597 }
598
599 return limit;
600}
601
Jiri Olsaf3069242016-06-28 13:29:02 +0200602static rlim_t fd_limit;
603
604/*
605 * Used only by tests/dso-data.c to reset the environment
606 * for tests. I dont expect we should change this during
607 * standard runtime.
608 */
609void reset_fd_limit(void)
610{
611 fd_limit = 0;
612}
613
Jiri Olsac6580452014-04-30 15:47:27 +0200614static bool may_cache_fd(void)
615{
Jiri Olsaf3069242016-06-28 13:29:02 +0200616 if (!fd_limit)
617 fd_limit = get_fd_limit();
Jiri Olsac6580452014-04-30 15:47:27 +0200618
Jiri Olsaf3069242016-06-28 13:29:02 +0200619 if (fd_limit == RLIM_INFINITY)
Jiri Olsac6580452014-04-30 15:47:27 +0200620 return true;
621
Jiri Olsaf3069242016-06-28 13:29:02 +0200622 return fd_limit > (rlim_t) dso__data_open_cnt;
Jiri Olsac6580452014-04-30 15:47:27 +0200623}
624
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200625/*
626 * Check and close LRU dso if we crossed allowed limit
627 * for opened dso file descriptors. The limit is half
628 * of the RLIMIT_NOFILE files opened.
629*/
Jiri Olsac6580452014-04-30 15:47:27 +0200630static void check_data_close(void)
631{
632 bool cache_fd = may_cache_fd();
633
634 if (!cache_fd)
635 close_first_dso();
636}
637
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200638/**
639 * dso__data_close - Close DSO data file
640 * @dso: dso object
641 *
642 * External interface to close @dso's data file descriptor.
643 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200644void dso__data_close(struct dso *dso)
645{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900646 pthread_mutex_lock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200647 close_dso(dso);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900648 pthread_mutex_unlock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200649}
650
Namhyung Kim71ff8242015-05-21 01:03:39 +0900651static void try_to_open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200652{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300653 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200654 DSO_BINARY_TYPE__BUILD_ID_CACHE,
655 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
656 DSO_BINARY_TYPE__NOT_FOUND,
657 };
658 int i = 0;
659
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200660 if (dso->data.fd >= 0)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900661 return;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200662
663 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
664 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300665 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200666 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200667
668 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300669 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200670
Adrian Hunterc27697d2014-07-22 16:17:18 +0300671 dso->data.fd = open_dso(dso, machine);
672 if (dso->data.fd >= 0)
673 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200674
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300675 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300676out:
677 if (dso->data.fd >= 0)
678 dso->data.status = DSO_DATA_STATUS_OK;
679 else
680 dso->data.status = DSO_DATA_STATUS_ERROR;
Namhyung Kim71ff8242015-05-21 01:03:39 +0900681}
Jiri Olsacdd059d2012-10-27 23:18:32 +0200682
Namhyung Kim71ff8242015-05-21 01:03:39 +0900683/**
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900684 * dso__data_get_fd - Get dso's data file descriptor
Namhyung Kim71ff8242015-05-21 01:03:39 +0900685 * @dso: dso object
686 * @machine: machine object
687 *
688 * External interface to find dso's file, open it and
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900689 * returns file descriptor. It should be paired with
690 * dso__data_put_fd() if it returns non-negative value.
Namhyung Kim71ff8242015-05-21 01:03:39 +0900691 */
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900692int dso__data_get_fd(struct dso *dso, struct machine *machine)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900693{
694 if (dso->data.status == DSO_DATA_STATUS_ERROR)
695 return -1;
696
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900697 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
698 return -1;
699
Namhyung Kim71ff8242015-05-21 01:03:39 +0900700 try_to_open_dso(dso, machine);
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900701
702 if (dso->data.fd < 0)
703 pthread_mutex_unlock(&dso__data_open_lock);
Namhyung Kim71ff8242015-05-21 01:03:39 +0900704
Adrian Hunterc27697d2014-07-22 16:17:18 +0300705 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200706}
707
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900708void dso__data_put_fd(struct dso *dso __maybe_unused)
709{
710 pthread_mutex_unlock(&dso__data_open_lock);
711}
712
Adrian Hunter288be942014-07-22 16:17:19 +0300713bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
714{
715 u32 flag = 1 << by;
716
717 if (dso->data.status_seen & flag)
718 return true;
719
720 dso->data.status_seen |= flag;
721
722 return false;
723}
724
Jiri Olsacdd059d2012-10-27 23:18:32 +0200725static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900726dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200727{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900728 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200729 struct rb_node *next = rb_first(root);
730
Namhyung Kim8e67b722015-05-18 09:30:41 +0900731 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200732 while (next) {
733 struct dso_cache *cache;
734
735 cache = rb_entry(next, struct dso_cache, rb_node);
736 next = rb_next(&cache->rb_node);
737 rb_erase(&cache->rb_node, root);
738 free(cache);
739 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900740 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200741}
742
Namhyung Kim8e67b722015-05-18 09:30:41 +0900743static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200744{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900745 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300746 struct rb_node * const *p = &root->rb_node;
747 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200748 struct dso_cache *cache;
749
750 while (*p != NULL) {
751 u64 end;
752
753 parent = *p;
754 cache = rb_entry(parent, struct dso_cache, rb_node);
755 end = cache->offset + DSO__DATA_CACHE_SIZE;
756
757 if (offset < cache->offset)
758 p = &(*p)->rb_left;
759 else if (offset >= end)
760 p = &(*p)->rb_right;
761 else
762 return cache;
763 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900764
Jiri Olsacdd059d2012-10-27 23:18:32 +0200765 return NULL;
766}
767
Namhyung Kim8e67b722015-05-18 09:30:41 +0900768static struct dso_cache *
769dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200770{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900771 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200772 struct rb_node **p = &root->rb_node;
773 struct rb_node *parent = NULL;
774 struct dso_cache *cache;
775 u64 offset = new->offset;
776
Namhyung Kim8e67b722015-05-18 09:30:41 +0900777 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200778 while (*p != NULL) {
779 u64 end;
780
781 parent = *p;
782 cache = rb_entry(parent, struct dso_cache, rb_node);
783 end = cache->offset + DSO__DATA_CACHE_SIZE;
784
785 if (offset < cache->offset)
786 p = &(*p)->rb_left;
787 else if (offset >= end)
788 p = &(*p)->rb_right;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900789 else
790 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200791 }
792
793 rb_link_node(&new->rb_node, parent, p);
794 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900795
796 cache = NULL;
797out:
798 pthread_mutex_unlock(&dso->lock);
799 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200800}
801
802static ssize_t
803dso_cache__memcpy(struct dso_cache *cache, u64 offset,
804 u8 *data, u64 size)
805{
806 u64 cache_offset = offset - cache->offset;
807 u64 cache_size = min(cache->size - cache_offset, size);
808
809 memcpy(data, cache->data + cache_offset, cache_size);
810 return cache_size;
811}
812
813static ssize_t
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900814dso_cache__read(struct dso *dso, struct machine *machine,
815 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200816{
817 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900818 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200819 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200820
821 do {
822 u64 cache_offset;
823
Jiri Olsacdd059d2012-10-27 23:18:32 +0200824 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
825 if (!cache)
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900826 return -ENOMEM;
827
828 pthread_mutex_lock(&dso__data_open_lock);
829
830 /*
831 * dso->data.fd might be closed if other thread opened another
832 * file (dso) due to open file limit (RLIMIT_NOFILE).
833 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900834 try_to_open_dso(dso, machine);
835
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900836 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900837 ret = -errno;
838 dso->data.status = DSO_DATA_STATUS_ERROR;
839 break;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900840 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200841
842 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200843
Namhyung Kimc52686f2015-01-29 17:02:01 -0300844 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200845 if (ret <= 0)
846 break;
847
848 cache->offset = cache_offset;
849 cache->size = ret;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900850 } while (0);
851
852 pthread_mutex_unlock(&dso__data_open_lock);
853
854 if (ret > 0) {
Namhyung Kim8e67b722015-05-18 09:30:41 +0900855 old = dso_cache__insert(dso, cache);
856 if (old) {
857 /* we lose the race */
858 free(cache);
859 cache = old;
860 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200861
862 ret = dso_cache__memcpy(cache, offset, data, size);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900863 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200864
865 if (ret <= 0)
866 free(cache);
867
Jiri Olsacdd059d2012-10-27 23:18:32 +0200868 return ret;
869}
870
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900871static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
872 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200873{
874 struct dso_cache *cache;
875
Namhyung Kim8e67b722015-05-18 09:30:41 +0900876 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200877 if (cache)
878 return dso_cache__memcpy(cache, offset, data, size);
879 else
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900880 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200881}
882
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200883/*
884 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
885 * in the rb_tree. Any read to already cached data is served
886 * by cached data.
887 */
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900888static ssize_t cached_read(struct dso *dso, struct machine *machine,
889 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200890{
891 ssize_t r = 0;
892 u8 *p = data;
893
894 do {
895 ssize_t ret;
896
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900897 ret = dso_cache_read(dso, machine, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200898 if (ret < 0)
899 return ret;
900
901 /* Reached EOF, return what we have. */
902 if (!ret)
903 break;
904
905 BUG_ON(ret > size);
906
907 r += ret;
908 p += ret;
909 offset += ret;
910 size -= ret;
911
912 } while (size);
913
914 return r;
915}
916
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900917static int data_file_size(struct dso *dso, struct machine *machine)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200918{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900919 int ret = 0;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200920 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000921 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200922
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900923 if (dso->data.file_size)
924 return 0;
925
Namhyung Kim71ff8242015-05-21 01:03:39 +0900926 if (dso->data.status == DSO_DATA_STATUS_ERROR)
927 return -1;
928
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900929 pthread_mutex_lock(&dso__data_open_lock);
930
931 /*
932 * dso->data.fd might be closed if other thread opened another
933 * file (dso) due to open file limit (RLIMIT_NOFILE).
934 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900935 try_to_open_dso(dso, machine);
936
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900937 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900938 ret = -errno;
939 dso->data.status = DSO_DATA_STATUS_ERROR;
940 goto out;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200941 }
942
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900943 if (fstat(dso->data.fd, &st) < 0) {
944 ret = -errno;
945 pr_err("dso cache fstat failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300946 str_error_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900947 dso->data.status = DSO_DATA_STATUS_ERROR;
948 goto out;
949 }
950 dso->data.file_size = st.st_size;
951
952out:
953 pthread_mutex_unlock(&dso__data_open_lock);
954 return ret;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200955}
956
Adrian Hunter6d363452014-07-22 16:17:35 +0300957/**
958 * dso__data_size - Return dso data size
959 * @dso: dso object
960 * @machine: machine object
961 *
962 * Return: dso data size
963 */
964off_t dso__data_size(struct dso *dso, struct machine *machine)
965{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900966 if (data_file_size(dso, machine))
Adrian Hunter6d363452014-07-22 16:17:35 +0300967 return -1;
968
969 /* For now just estimate dso data size is close to file size */
970 return dso->data.file_size;
971}
972
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900973static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
974 u64 offset, u8 *data, ssize_t size)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200975{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900976 if (data_file_size(dso, machine))
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200977 return -1;
978
979 /* Check the offset sanity. */
980 if (offset > dso->data.file_size)
981 return -1;
982
983 if (offset + size < offset)
984 return -1;
985
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900986 return cached_read(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200987}
988
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200989/**
990 * dso__data_read_offset - Read data from dso file offset
991 * @dso: dso object
992 * @machine: machine object
993 * @offset: file offset
994 * @data: buffer to store data
995 * @size: size of the @data buffer
996 *
997 * External interface to read data from dso file offset. Open
998 * dso data file and use cached_read to get the data.
999 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +02001000ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1001 u64 offset, u8 *data, ssize_t size)
1002{
Namhyung Kim33bdedc2015-05-18 09:30:42 +09001003 if (dso->data.status == DSO_DATA_STATUS_ERROR)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +02001004 return -1;
1005
Namhyung Kim33bdedc2015-05-18 09:30:42 +09001006 return data_read_offset(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +02001007}
1008
Jiri Olsac1f9aa02014-05-07 21:09:59 +02001009/**
1010 * dso__data_read_addr - Read data from dso address
1011 * @dso: dso object
1012 * @machine: machine object
1013 * @add: virtual memory address
1014 * @data: buffer to store data
1015 * @size: size of the @data buffer
1016 *
1017 * External interface to read data from dso address.
1018 */
Jiri Olsacdd059d2012-10-27 23:18:32 +02001019ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1020 struct machine *machine, u64 addr,
1021 u8 *data, ssize_t size)
1022{
1023 u64 offset = map->map_ip(map, addr);
1024 return dso__data_read_offset(dso, machine, offset, data, size);
1025}
1026
1027struct map *dso__new_map(const char *name)
1028{
1029 struct map *map = NULL;
1030 struct dso *dso = dso__new(name);
1031
1032 if (dso)
1033 map = map__new2(0, dso, MAP__FUNCTION);
1034
1035 return map;
1036}
1037
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -03001038struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1039 const char *short_name, int dso_type)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001040{
1041 /*
1042 * The kernel dso could be created by build_id processing.
1043 */
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001044 struct dso *dso = machine__findnew_dso(machine, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001045
1046 /*
1047 * We need to run this in all cases, since during the build_id
1048 * processing we had no idea this was the kernel dso.
1049 */
1050 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -03001051 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001052 dso->kernel = dso_type;
1053 }
1054
1055 return dso;
1056}
1057
Waiman Long4598a0a2014-09-30 13:36:15 -04001058/*
1059 * Find a matching entry and/or link current entry to RB tree.
1060 * Either one of the dso or name parameter must be non-NULL or the
1061 * function will not work.
1062 */
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001063static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1064 struct dso *dso, const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -04001065{
1066 struct rb_node **p = &root->rb_node;
1067 struct rb_node *parent = NULL;
1068
1069 if (!name)
1070 name = dso->long_name;
1071 /*
1072 * Find node with the matching name
1073 */
1074 while (*p) {
1075 struct dso *this = rb_entry(*p, struct dso, rb_node);
1076 int rc = strcmp(name, this->long_name);
1077
1078 parent = *p;
1079 if (rc == 0) {
1080 /*
1081 * In case the new DSO is a duplicate of an existing
Masahiro Yamada0f5e1552017-02-27 14:28:52 -08001082 * one, print a one-time warning & put the new entry
Waiman Long4598a0a2014-09-30 13:36:15 -04001083 * at the end of the list of duplicates.
1084 */
1085 if (!dso || (dso == this))
1086 return this; /* Find matching dso */
1087 /*
1088 * The core kernel DSOs may have duplicated long name.
1089 * In this case, the short name should be different.
1090 * Comparing the short names to differentiate the DSOs.
1091 */
1092 rc = strcmp(dso->short_name, this->short_name);
1093 if (rc == 0) {
1094 pr_err("Duplicated dso name: %s\n", name);
1095 return NULL;
1096 }
1097 }
1098 if (rc < 0)
1099 p = &parent->rb_left;
1100 else
1101 p = &parent->rb_right;
1102 }
1103 if (dso) {
1104 /* Add new node and rebalance tree */
1105 rb_link_node(&dso->rb_node, parent, p);
1106 rb_insert_color(&dso->rb_node, root);
Adrian Huntere266a752015-11-13 11:48:30 +02001107 dso->root = root;
Waiman Long4598a0a2014-09-30 13:36:15 -04001108 }
1109 return NULL;
1110}
1111
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001112static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1113 const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -04001114{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001115 return __dso__findlink_by_longname(root, NULL, name);
Waiman Long4598a0a2014-09-30 13:36:15 -04001116}
1117
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001118void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001119{
Adrian Huntere266a752015-11-13 11:48:30 +02001120 struct rb_root *root = dso->root;
1121
Jiri Olsacdd059d2012-10-27 23:18:32 +02001122 if (name == NULL)
1123 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001124
1125 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001126 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001127
Adrian Huntere266a752015-11-13 11:48:30 +02001128 if (root) {
1129 rb_erase(&dso->rb_node, root);
1130 /*
1131 * __dso__findlink_by_longname() isn't guaranteed to add it
1132 * back, so a clean removal is required here.
1133 */
1134 RB_CLEAR_NODE(&dso->rb_node);
1135 dso->root = NULL;
1136 }
1137
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001138 dso->long_name = name;
1139 dso->long_name_len = strlen(name);
1140 dso->long_name_allocated = name_allocated;
Adrian Huntere266a752015-11-13 11:48:30 +02001141
1142 if (root)
1143 __dso__findlink_by_longname(root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001144}
1145
Adrian Hunter58a98c92013-12-10 11:11:46 -03001146void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001147{
1148 if (name == NULL)
1149 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -03001150
1151 if (dso->short_name_allocated)
1152 free((char *)dso->short_name);
1153
1154 dso->short_name = name;
1155 dso->short_name_len = strlen(name);
1156 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001157}
1158
1159static void dso__set_basename(struct dso *dso)
1160{
Stephane Eranianac5e7f82013-12-05 19:26:42 +01001161 /*
1162 * basename() may modify path buffer, so we must pass
1163 * a copy.
1164 */
1165 char *base, *lname = strdup(dso->long_name);
1166
1167 if (!lname)
1168 return;
1169
1170 /*
1171 * basename() may return a pointer to internal
1172 * storage which is reused in subsequent calls
1173 * so copy the result.
1174 */
1175 base = strdup(basename(lname));
1176
1177 free(lname);
1178
1179 if (!base)
1180 return;
1181
1182 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001183}
1184
1185int dso__name_len(const struct dso *dso)
1186{
1187 if (!dso)
1188 return strlen("[unknown]");
Namhyung Kimbb963e12017-02-17 17:17:38 +09001189 if (verbose > 0)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001190 return dso->long_name_len;
1191
1192 return dso->short_name_len;
1193}
1194
1195bool dso__loaded(const struct dso *dso, enum map_type type)
1196{
1197 return dso->loaded & (1 << type);
1198}
1199
1200bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1201{
1202 return dso->sorted_by_name & (1 << type);
1203}
1204
1205void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1206{
1207 dso->sorted_by_name |= (1 << type);
1208}
1209
1210struct dso *dso__new(const char *name)
1211{
1212 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1213
1214 if (dso != NULL) {
1215 int i;
1216 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001217 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -03001218 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001219 for (i = 0; i < MAP__NR_TYPES; ++i)
1220 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +02001221 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001222 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +03001223 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001224 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001225 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001226 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001227 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001228 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001229 dso->sorted_by_name = 0;
1230 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +09001231 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +02001232 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001233 dso->kernel = DSO_TYPE_USER;
1234 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -04001235 RB_CLEAR_NODE(&dso->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +02001236 dso->root = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001237 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +02001238 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001239 pthread_mutex_init(&dso->lock, NULL);
Elena Reshetova71008102017-02-21 17:34:58 +02001240 refcount_set(&dso->refcnt, 1);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001241 }
1242
1243 return dso;
1244}
1245
1246void dso__delete(struct dso *dso)
1247{
1248 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -04001249
1250 if (!RB_EMPTY_NODE(&dso->rb_node))
1251 pr_err("DSO %s is still in rbtree when being deleted!\n",
1252 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001253 for (i = 0; i < MAP__NR_TYPES; ++i)
1254 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001255
1256 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001257 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001258 dso->short_name_allocated = false;
1259 }
1260
1261 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001262 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001263 dso->long_name_allocated = false;
1264 }
1265
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001266 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +03001267 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +09001268 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +02001269 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001270 zfree(&dso->symsrc_filename);
Krister Johansen843ff372017-07-05 18:48:08 -07001271 nsinfo__zput(dso->nsinfo);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001272 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001273 free(dso);
1274}
1275
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001276struct dso *dso__get(struct dso *dso)
1277{
1278 if (dso)
Elena Reshetova71008102017-02-21 17:34:58 +02001279 refcount_inc(&dso->refcnt);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001280 return dso;
1281}
1282
1283void dso__put(struct dso *dso)
1284{
Elena Reshetova71008102017-02-21 17:34:58 +02001285 if (dso && refcount_dec_and_test(&dso->refcnt))
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001286 dso__delete(dso);
1287}
1288
Jiri Olsacdd059d2012-10-27 23:18:32 +02001289void dso__set_build_id(struct dso *dso, void *build_id)
1290{
1291 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1292 dso->has_build_id = 1;
1293}
1294
1295bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1296{
1297 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1298}
1299
1300void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1301{
1302 char path[PATH_MAX];
1303
1304 if (machine__is_default_guest(machine))
1305 return;
1306 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1307 if (sysfs__read_build_id(path, dso->build_id,
1308 sizeof(dso->build_id)) == 0)
1309 dso->has_build_id = true;
1310}
1311
1312int dso__kernel_module_get_build_id(struct dso *dso,
1313 const char *root_dir)
1314{
1315 char filename[PATH_MAX];
1316 /*
1317 * kernel module short names are of the form "[module]" and
1318 * we need just "module" here.
1319 */
1320 const char *name = dso->short_name + 1;
1321
1322 snprintf(filename, sizeof(filename),
1323 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1324 root_dir, (int)strlen(name) - 1, name);
1325
1326 if (sysfs__read_build_id(filename, dso->build_id,
1327 sizeof(dso->build_id)) == 0)
1328 dso->has_build_id = true;
1329
1330 return 0;
1331}
1332
1333bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1334{
1335 bool have_build_id = false;
1336 struct dso *pos;
Krister Johansenf045b8c2017-07-05 18:48:11 -07001337 struct nscookie nsc;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001338
1339 list_for_each_entry(pos, head, node) {
He Kuang6ae98ba2016-05-12 08:43:11 +00001340 if (with_hits && !pos->hit && !dso__is_vdso(pos))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001341 continue;
1342 if (pos->has_build_id) {
1343 have_build_id = true;
1344 continue;
1345 }
Krister Johansenf045b8c2017-07-05 18:48:11 -07001346 nsinfo__mountns_enter(pos->nsinfo, &nsc);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001347 if (filename__read_build_id(pos->long_name, pos->build_id,
1348 sizeof(pos->build_id)) > 0) {
1349 have_build_id = true;
1350 pos->has_build_id = true;
1351 }
Krister Johansenf045b8c2017-07-05 18:48:11 -07001352 nsinfo__mountns_exit(&nsc);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001353 }
1354
1355 return have_build_id;
1356}
1357
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001358void __dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001359{
Waiman Long8fa7d872014-09-29 16:07:28 -04001360 list_add_tail(&dso->node, &dsos->head);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001361 __dso__findlink_by_longname(&dsos->root, dso, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001362 /*
1363 * It is now in the linked list, grab a reference, then garbage collect
1364 * this when needing memory, by looking at LRU dso instances in the
1365 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1366 * anywhere besides the one for the list, do, under a lock for the
1367 * list: remove it from the list, then a dso__put(), that probably will
1368 * be the last and will then call dso__delete(), end of life.
1369 *
1370 * That, or at the end of the 'struct machine' lifetime, when all
1371 * 'struct dso' instances will be removed from the list, in
1372 * dsos__exit(), if they have no other reference from some other data
1373 * structure.
1374 *
1375 * E.g.: after processing a 'perf.data' file and storing references
1376 * to objects instantiated while processing events, we will have
1377 * references to the 'thread', 'map', 'dso' structs all from 'struct
1378 * hist_entry' instances, but we may not need anything not referenced,
1379 * so we might as well call machines__exit()/machines__delete() and
1380 * garbage collect it.
1381 */
1382 dso__get(dso);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001383}
1384
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001385void dsos__add(struct dsos *dsos, struct dso *dso)
1386{
1387 pthread_rwlock_wrlock(&dsos->lock);
1388 __dsos__add(dsos, dso);
1389 pthread_rwlock_unlock(&dsos->lock);
1390}
1391
1392struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001393{
1394 struct dso *pos;
1395
Waiman Longf9ceffb2013-05-09 10:42:48 -04001396 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001397 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001398 if (strcmp(pos->short_name, name) == 0)
1399 return pos;
1400 return NULL;
1401 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001402 return __dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001403}
1404
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001405struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1406{
1407 struct dso *dso;
1408 pthread_rwlock_rdlock(&dsos->lock);
1409 dso = __dsos__find(dsos, name, cmp_short);
1410 pthread_rwlock_unlock(&dsos->lock);
1411 return dso;
1412}
1413
1414struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
Jiri Olsa701d8d72015-02-12 22:06:09 +01001415{
1416 struct dso *dso = dso__new(name);
1417
1418 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001419 __dsos__add(dsos, dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001420 dso__set_basename(dso);
Masami Hiramatsu82de26a2015-11-18 15:40:31 +09001421 /* Put dso here because __dsos_add already got it */
1422 dso__put(dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001423 }
1424 return dso;
1425}
1426
Waiman Long8fa7d872014-09-29 16:07:28 -04001427struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001428{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001429 struct dso *dso = __dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001430
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001431 return dso ? dso : __dsos__addnew(dsos, name);
1432}
1433
1434struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1435{
1436 struct dso *dso;
1437 pthread_rwlock_wrlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001438 dso = dso__get(__dsos__findnew(dsos, name));
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001439 pthread_rwlock_unlock(&dsos->lock);
1440 return dso;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001441}
1442
1443size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001444 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001445{
1446 struct dso *pos;
1447 size_t ret = 0;
1448
1449 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001450 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001451 continue;
1452 ret += dso__fprintf_buildid(pos, fp);
1453 ret += fprintf(fp, " %s\n", pos->long_name);
1454 }
1455 return ret;
1456}
1457
1458size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1459{
1460 struct dso *pos;
1461 size_t ret = 0;
1462
1463 list_for_each_entry(pos, head, node) {
1464 int i;
1465 for (i = 0; i < MAP__NR_TYPES; ++i)
1466 ret += dso__fprintf(pos, i, fp);
1467 }
1468
1469 return ret;
1470}
1471
1472size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1473{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001474 char sbuild_id[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +02001475
1476 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1477 return fprintf(fp, "%s", sbuild_id);
1478}
1479
1480size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1481{
1482 struct rb_node *nd;
1483 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1484
1485 if (dso->short_name != dso->long_name)
1486 ret += fprintf(fp, "%s, ", dso->long_name);
1487 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001488 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001489 ret += dso__fprintf_buildid(dso, fp);
1490 ret += fprintf(fp, ")\n");
1491 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1492 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1493 ret += symbol__fprintf(pos, fp);
1494 }
1495
1496 return ret;
1497}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001498
1499enum dso_type dso__type(struct dso *dso, struct machine *machine)
1500{
1501 int fd;
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001502 enum dso_type type = DSO__TYPE_UNKNOWN;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001503
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001504 fd = dso__data_get_fd(dso, machine);
1505 if (fd >= 0) {
1506 type = dso__type_fd(fd);
1507 dso__data_put_fd(dso);
1508 }
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001509
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001510 return type;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001511}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001512
1513int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1514{
1515 int idx, errnum = dso->load_errno;
1516 /*
1517 * This must have a same ordering as the enum dso_load_errno.
1518 */
1519 static const char *dso_load__error_str[] = {
1520 "Internal tools/perf/ library error",
1521 "Invalid ELF file",
1522 "Can not read build id",
1523 "Mismatching build id",
1524 "Decompression failure",
1525 };
1526
1527 BUG_ON(buflen == 0);
1528
1529 if (errnum >= 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001530 const char *err = str_error_r(errnum, buf, buflen);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001531
1532 if (err != buf)
1533 scnprintf(buf, buflen, "%s", err);
1534
1535 return 0;
1536 }
1537
1538 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1539 return -1;
1540
1541 idx = errnum - __DSO_LOAD_ERRNO__START;
1542 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1543 return 0;
1544}