blob: b9e087fb8247983f0f3aed7e984dc5eb2615de7c [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',
Krister Johansend2396992017-07-05 18:48:13 -070035 [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020036 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
37 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
38 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
39 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
40 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
41 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090042 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020043 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
44 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090045 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020046 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Jiri Olsacdd059d2012-10-27 23:18:32 +020047 };
48
49 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
50 return '!';
51 return origin[dso->symtab_type];
52}
53
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -030054int dso__read_binary_type_filename(const struct dso *dso,
55 enum dso_binary_type type,
56 char *root_dir, char *filename, size_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +020057{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +090058 char build_id_hex[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +020059 int ret = 0;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030060 size_t len;
Jiri Olsacdd059d2012-10-27 23:18:32 +020061
62 switch (type) {
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010063 case DSO_BINARY_TYPE__DEBUGLINK:
64 {
65 const char *last_slash;
66 char dso_dir[PATH_MAX];
67 char symfile[PATH_MAX];
68 unsigned int i;
Jiri Olsacdd059d2012-10-27 23:18:32 +020069
Victor Kamenskydc6254c2015-01-26 22:34:02 -080070 len = __symbol__join_symfs(filename, size, dso->long_name);
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010071 last_slash = filename + len;
72 while (last_slash != filename && *last_slash != '/')
73 last_slash--;
Jiri Olsa40356722016-01-20 12:56:32 +010074
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010075 strncpy(dso_dir, filename, last_slash - filename);
76 dso_dir[last_slash-filename] = '\0';
77
78 if (!is_regular_file(filename)) {
79 ret = -1;
80 break;
81 }
82
83 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
84 if (ret)
Jiri Olsa40356722016-01-20 12:56:32 +010085 break;
86
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010087 /* Check predefined locations where debug file might reside */
88 ret = -1;
89 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
90 snprintf(filename, size,
91 debuglink_paths[i], dso_dir, symfile);
92 if (is_regular_file(filename)) {
93 ret = 0;
94 break;
95 }
Jiri Olsacdd059d2012-10-27 23:18:32 +020096 }
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010097
Jiri Olsacdd059d2012-10-27 23:18:32 +020098 break;
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010099 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200100 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
Krister Johansend2396992017-07-05 18:48:13 -0700101 if (dso__build_id_filename(dso, filename, size, false) == NULL)
102 ret = -1;
103 break;
104
105 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
106 if (dso__build_id_filename(dso, filename, size, true) == NULL)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200107 ret = -1;
108 break;
109
110 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300111 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
112 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200113 break;
114
115 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300116 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
117 snprintf(filename + len, size - len, "%s", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200118 break;
119
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200120 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
121 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300122 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200123 size_t dir_size;
124
125 last_slash = dso->long_name + dso->long_name_len;
126 while (last_slash != dso->long_name && *last_slash != '/')
127 last_slash--;
128
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300129 len = __symbol__join_symfs(filename, size, "");
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200130 dir_size = last_slash - dso->long_name + 2;
131 if (dir_size > (size - len)) {
132 ret = -1;
133 break;
134 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300135 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
136 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200137 last_slash);
138 break;
139 }
140
Jiri Olsacdd059d2012-10-27 23:18:32 +0200141 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
142 if (!dso->has_build_id) {
143 ret = -1;
144 break;
145 }
146
147 build_id__sprintf(dso->build_id,
148 sizeof(dso->build_id),
149 build_id_hex);
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300150 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
151 snprintf(filename + len, size - len, "%.2s/%s.debug",
152 build_id_hex, build_id_hex + 2);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200153 break;
154
Adrian Hunter39b12f782013-08-07 14:38:47 +0300155 case DSO_BINARY_TYPE__VMLINUX:
156 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200157 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300158 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200159 break;
160
161 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900162 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300163 path__join3(filename, size, symbol_conf.symfs,
164 root_dir, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200165 break;
166
167 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900168 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300169 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200170 break;
171
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300172 case DSO_BINARY_TYPE__KCORE:
173 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300174 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300175 break;
176
Jiri Olsacdd059d2012-10-27 23:18:32 +0200177 default:
178 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200179 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200180 case DSO_BINARY_TYPE__JAVA_JIT:
181 case DSO_BINARY_TYPE__NOT_FOUND:
182 ret = -1;
183 break;
184 }
185
186 return ret;
187}
188
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900189static const struct {
190 const char *fmt;
191 int (*decompress)(const char *input, int output);
192} compressions[] = {
Namhyung Kime92ce122014-10-31 16:51:38 +0900193#ifdef HAVE_ZLIB_SUPPORT
194 { "gz", gzip_decompress_to_file },
195#endif
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100196#ifdef HAVE_LZMA_SUPPORT
197 { "xz", lzma_decompress_to_file },
198#endif
Namhyung Kime92ce122014-10-31 16:51:38 +0900199 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900200};
201
202bool is_supported_compression(const char *ext)
203{
204 unsigned i;
205
206 for (i = 0; compressions[i].fmt; i++) {
207 if (!strcmp(ext, compressions[i].fmt))
208 return true;
209 }
210 return false;
211}
212
Wang Nan1f121b02015-06-03 08:52:21 +0000213bool is_kernel_module(const char *pathname, int cpumode)
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900214{
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100215 struct kmod_path m;
Wang Nan1f121b02015-06-03 08:52:21 +0000216 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900217
Wang Nan1f121b02015-06-03 08:52:21 +0000218 WARN_ONCE(mode != cpumode,
219 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
220 cpumode);
221
222 switch (mode) {
223 case PERF_RECORD_MISC_USER:
224 case PERF_RECORD_MISC_HYPERVISOR:
225 case PERF_RECORD_MISC_GUEST_USER:
226 return false;
227 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
228 default:
229 if (kmod_path__parse(&m, pathname)) {
230 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
231 pathname);
232 return true;
233 }
234 }
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900235
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100236 return m.kmod;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900237}
238
239bool decompress_to_file(const char *ext, const char *filename, int output_fd)
240{
241 unsigned i;
242
243 for (i = 0; compressions[i].fmt; i++) {
244 if (!strcmp(ext, compressions[i].fmt))
245 return !compressions[i].decompress(filename,
246 output_fd);
247 }
248 return false;
249}
250
251bool dso__needs_decompress(struct dso *dso)
252{
253 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
254 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
255}
256
Namhyung Kim42b3fa62017-06-08 16:31:03 +0900257static int decompress_kmodule(struct dso *dso, const char *name, char *tmpbuf)
258{
259 int fd = -1;
260 struct kmod_path m;
261
262 if (!dso__needs_decompress(dso))
263 return -1;
264
265 if (kmod_path__parse_ext(&m, dso->long_name))
266 return -1;
267
268 if (!m.comp)
269 goto out;
270
271 fd = mkstemp(tmpbuf);
272 if (fd < 0) {
273 dso->load_errno = errno;
274 goto out;
275 }
276
277 if (!decompress_to_file(m.ext, name, fd)) {
278 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
279 close(fd);
280 fd = -1;
281 }
282
283out:
284 free(m.ext);
285 return fd;
286}
287
288int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
289{
290 char tmpbuf[] = KMOD_DECOMP_NAME;
291 int fd;
292
293 fd = decompress_kmodule(dso, name, tmpbuf);
294 unlink(tmpbuf);
295 return fd;
296}
297
298int dso__decompress_kmodule_path(struct dso *dso, const char *name,
299 char *pathname, size_t len)
300{
301 char tmpbuf[] = KMOD_DECOMP_NAME;
302 int fd;
303
304 fd = decompress_kmodule(dso, name, tmpbuf);
305 if (fd < 0) {
306 unlink(tmpbuf);
307 return -1;
308 }
309
310 strncpy(pathname, tmpbuf, len);
311 close(fd);
312 return 0;
313}
314
Jiri Olsaeba51022014-04-30 15:00:59 +0200315/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100316 * Parses kernel module specified in @path and updates
317 * @m argument like:
318 *
319 * @comp - true if @path contains supported compression suffix,
320 * false otherwise
321 * @kmod - true if @path contains '.ko' suffix in right position,
322 * false otherwise
323 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
324 * of the kernel module without suffixes, otherwise strudup-ed
325 * base name of @path
326 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
327 * the compression suffix
328 *
329 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
330 */
331int __kmod_path__parse(struct kmod_path *m, const char *path,
332 bool alloc_name, bool alloc_ext)
333{
334 const char *name = strrchr(path, '/');
335 const char *ext = strrchr(path, '.');
Wang Nan1f121b02015-06-03 08:52:21 +0000336 bool is_simple_name = false;
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100337
338 memset(m, 0x0, sizeof(*m));
339 name = name ? name + 1 : path;
340
Wang Nan1f121b02015-06-03 08:52:21 +0000341 /*
342 * '.' is also a valid character for module name. For example:
343 * [aaa.bbb] is a valid module name. '[' should have higher
344 * priority than '.ko' suffix.
345 *
346 * The kernel names are from machine__mmap_name. Such
347 * name should belong to kernel itself, not kernel module.
348 */
349 if (name[0] == '[') {
350 is_simple_name = true;
351 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
352 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
353 (strncmp(name, "[vdso]", 6) == 0) ||
354 (strncmp(name, "[vsyscall]", 10) == 0)) {
355 m->kmod = false;
356
357 } else
358 m->kmod = true;
359 }
360
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100361 /* No extension, just return name. */
Wang Nan1f121b02015-06-03 08:52:21 +0000362 if ((ext == NULL) || is_simple_name) {
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100363 if (alloc_name) {
364 m->name = strdup(name);
365 return m->name ? 0 : -ENOMEM;
366 }
367 return 0;
368 }
369
370 if (is_supported_compression(ext + 1)) {
371 m->comp = true;
372 ext -= 3;
373 }
374
375 /* Check .ko extension only if there's enough name left. */
376 if (ext > name)
377 m->kmod = !strncmp(ext, ".ko", 3);
378
379 if (alloc_name) {
380 if (m->kmod) {
381 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
382 return -ENOMEM;
383 } else {
384 if (asprintf(&m->name, "%s", name) == -1)
385 return -ENOMEM;
386 }
387
388 strxfrchar(m->name, '-', '_');
389 }
390
391 if (alloc_ext && m->comp) {
392 m->ext = strdup(ext + 4);
393 if (!m->ext) {
394 free((void *) m->name);
395 return -ENOMEM;
396 }
397 }
398
399 return 0;
400}
401
Namhyung Kim6b335e82017-05-31 21:01:04 +0900402void dso__set_module_info(struct dso *dso, struct kmod_path *m,
403 struct machine *machine)
404{
405 if (machine__is_host(machine))
406 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
407 else
408 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
409
410 /* _KMODULE_COMP should be next to _KMODULE */
411 if (m->kmod && m->comp)
412 dso->symtab_type++;
413
414 dso__set_short_name(dso, strdup(m->name), true);
415}
416
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100417/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200418 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200419 */
420static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200421static long dso__data_open_cnt;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900422static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
Jiri Olsaeba51022014-04-30 15:00:59 +0200423
424static void dso__list_add(struct dso *dso)
425{
426 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200427 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200428}
429
430static void dso__list_del(struct dso *dso)
431{
432 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200433 WARN_ONCE(dso__data_open_cnt <= 0,
434 "DSO data fd counter out of bounds.");
435 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200436}
437
Jiri Olsaa08cae02014-05-07 21:35:02 +0200438static void close_first_dso(void);
439
440static int do_open(char *name)
441{
442 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000443 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200444
445 do {
446 fd = open(name, O_RDONLY);
447 if (fd >= 0)
448 return fd;
449
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900450 pr_debug("dso open failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300451 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200452 if (!dso__data_open_cnt || errno != EMFILE)
453 break;
454
455 close_first_dso();
456 } while (1);
457
458 return -1;
459}
460
Jiri Olsaeba51022014-04-30 15:00:59 +0200461static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200462{
Namhyung Kim8ba29ad2017-06-08 16:31:06 +0900463 int fd = -EINVAL;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300464 char *root_dir = (char *)"";
465 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200466
Jiri Olsacdd059d2012-10-27 23:18:32 +0200467 if (!name)
468 return -ENOMEM;
469
470 if (machine)
471 root_dir = machine->root_dir;
472
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300473 if (dso__read_binary_type_filename(dso, dso->binary_type,
Namhyung Kim8ba29ad2017-06-08 16:31:06 +0900474 root_dir, name, PATH_MAX))
475 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200476
Namhyung Kim8ba29ad2017-06-08 16:31:06 +0900477 if (!is_regular_file(name))
478 goto out;
Jiri Olsa3c028a02016-09-20 18:12:45 +0200479
Namhyung Kim1d6b3c92017-06-08 16:31:05 +0900480 if (dso__needs_decompress(dso)) {
481 char newpath[KMOD_DECOMP_LEN];
482 size_t len = sizeof(newpath);
483
484 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
Namhyung Kim8ba29ad2017-06-08 16:31:06 +0900485 fd = -dso->load_errno;
486 goto out;
Namhyung Kim1d6b3c92017-06-08 16:31:05 +0900487 }
488
489 strcpy(name, newpath);
490 }
491
Jiri Olsaa08cae02014-05-07 21:35:02 +0200492 fd = do_open(name);
Namhyung Kim1d6b3c92017-06-08 16:31:05 +0900493
494 if (dso__needs_decompress(dso))
495 unlink(name);
496
Namhyung Kim8ba29ad2017-06-08 16:31:06 +0900497out:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200498 free(name);
499 return fd;
500}
501
Jiri Olsac6580452014-04-30 15:47:27 +0200502static void check_data_close(void);
503
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200504/**
505 * dso_close - Open DSO data file
506 * @dso: dso object
507 *
508 * Open @dso's data file descriptor and updates
509 * list/count of open DSO objects.
510 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200511static int open_dso(struct dso *dso, struct machine *machine)
512{
Krister Johansenf045b8c2017-07-05 18:48:11 -0700513 int fd;
514 struct nscookie nsc;
515
516 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
517 nsinfo__mountns_enter(dso->nsinfo, &nsc);
518 fd = __open_dso(dso, machine);
519 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
520 nsinfo__mountns_exit(&nsc);
Jiri Olsaeba51022014-04-30 15:00:59 +0200521
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300522 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200523 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200524 /*
525 * Check if we crossed the allowed number
526 * of opened DSOs and close one if needed.
527 */
528 check_data_close();
529 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200530
531 return fd;
532}
533
534static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200535{
536 if (dso->data.fd >= 0) {
537 close(dso->data.fd);
538 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200539 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200540 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200541 }
542}
543
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200544/**
545 * dso_close - Close DSO data file
546 * @dso: dso object
547 *
548 * Close @dso's data file descriptor and updates
549 * list/count of open DSO objects.
550 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200551static void close_dso(struct dso *dso)
552{
553 close_data_fd(dso);
554}
555
Jiri Olsac6580452014-04-30 15:47:27 +0200556static void close_first_dso(void)
557{
558 struct dso *dso;
559
560 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
561 close_dso(dso);
562}
563
564static rlim_t get_fd_limit(void)
565{
566 struct rlimit l;
567 rlim_t limit = 0;
568
569 /* Allow half of the current open fd limit. */
570 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
571 if (l.rlim_cur == RLIM_INFINITY)
572 limit = l.rlim_cur;
573 else
574 limit = l.rlim_cur / 2;
575 } else {
576 pr_err("failed to get fd limit\n");
577 limit = 1;
578 }
579
580 return limit;
581}
582
Jiri Olsaf3069242016-06-28 13:29:02 +0200583static rlim_t fd_limit;
584
585/*
586 * Used only by tests/dso-data.c to reset the environment
587 * for tests. I dont expect we should change this during
588 * standard runtime.
589 */
590void reset_fd_limit(void)
591{
592 fd_limit = 0;
593}
594
Jiri Olsac6580452014-04-30 15:47:27 +0200595static bool may_cache_fd(void)
596{
Jiri Olsaf3069242016-06-28 13:29:02 +0200597 if (!fd_limit)
598 fd_limit = get_fd_limit();
Jiri Olsac6580452014-04-30 15:47:27 +0200599
Jiri Olsaf3069242016-06-28 13:29:02 +0200600 if (fd_limit == RLIM_INFINITY)
Jiri Olsac6580452014-04-30 15:47:27 +0200601 return true;
602
Jiri Olsaf3069242016-06-28 13:29:02 +0200603 return fd_limit > (rlim_t) dso__data_open_cnt;
Jiri Olsac6580452014-04-30 15:47:27 +0200604}
605
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200606/*
607 * Check and close LRU dso if we crossed allowed limit
608 * for opened dso file descriptors. The limit is half
609 * of the RLIMIT_NOFILE files opened.
610*/
Jiri Olsac6580452014-04-30 15:47:27 +0200611static void check_data_close(void)
612{
613 bool cache_fd = may_cache_fd();
614
615 if (!cache_fd)
616 close_first_dso();
617}
618
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200619/**
620 * dso__data_close - Close DSO data file
621 * @dso: dso object
622 *
623 * External interface to close @dso's data file descriptor.
624 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200625void dso__data_close(struct dso *dso)
626{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900627 pthread_mutex_lock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200628 close_dso(dso);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900629 pthread_mutex_unlock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200630}
631
Namhyung Kim71ff8242015-05-21 01:03:39 +0900632static void try_to_open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200633{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300634 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200635 DSO_BINARY_TYPE__BUILD_ID_CACHE,
636 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
637 DSO_BINARY_TYPE__NOT_FOUND,
638 };
639 int i = 0;
640
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200641 if (dso->data.fd >= 0)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900642 return;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200643
644 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
645 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300646 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200647 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200648
649 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300650 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200651
Adrian Hunterc27697d2014-07-22 16:17:18 +0300652 dso->data.fd = open_dso(dso, machine);
653 if (dso->data.fd >= 0)
654 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200655
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300656 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300657out:
658 if (dso->data.fd >= 0)
659 dso->data.status = DSO_DATA_STATUS_OK;
660 else
661 dso->data.status = DSO_DATA_STATUS_ERROR;
Namhyung Kim71ff8242015-05-21 01:03:39 +0900662}
Jiri Olsacdd059d2012-10-27 23:18:32 +0200663
Namhyung Kim71ff8242015-05-21 01:03:39 +0900664/**
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900665 * dso__data_get_fd - Get dso's data file descriptor
Namhyung Kim71ff8242015-05-21 01:03:39 +0900666 * @dso: dso object
667 * @machine: machine object
668 *
669 * External interface to find dso's file, open it and
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900670 * returns file descriptor. It should be paired with
671 * dso__data_put_fd() if it returns non-negative value.
Namhyung Kim71ff8242015-05-21 01:03:39 +0900672 */
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900673int dso__data_get_fd(struct dso *dso, struct machine *machine)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900674{
675 if (dso->data.status == DSO_DATA_STATUS_ERROR)
676 return -1;
677
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900678 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
679 return -1;
680
Namhyung Kim71ff8242015-05-21 01:03:39 +0900681 try_to_open_dso(dso, machine);
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900682
683 if (dso->data.fd < 0)
684 pthread_mutex_unlock(&dso__data_open_lock);
Namhyung Kim71ff8242015-05-21 01:03:39 +0900685
Adrian Hunterc27697d2014-07-22 16:17:18 +0300686 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200687}
688
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900689void dso__data_put_fd(struct dso *dso __maybe_unused)
690{
691 pthread_mutex_unlock(&dso__data_open_lock);
692}
693
Adrian Hunter288be942014-07-22 16:17:19 +0300694bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
695{
696 u32 flag = 1 << by;
697
698 if (dso->data.status_seen & flag)
699 return true;
700
701 dso->data.status_seen |= flag;
702
703 return false;
704}
705
Jiri Olsacdd059d2012-10-27 23:18:32 +0200706static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900707dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200708{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900709 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200710 struct rb_node *next = rb_first(root);
711
Namhyung Kim8e67b722015-05-18 09:30:41 +0900712 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200713 while (next) {
714 struct dso_cache *cache;
715
716 cache = rb_entry(next, struct dso_cache, rb_node);
717 next = rb_next(&cache->rb_node);
718 rb_erase(&cache->rb_node, root);
719 free(cache);
720 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900721 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200722}
723
Namhyung Kim8e67b722015-05-18 09:30:41 +0900724static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200725{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900726 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300727 struct rb_node * const *p = &root->rb_node;
728 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200729 struct dso_cache *cache;
730
731 while (*p != NULL) {
732 u64 end;
733
734 parent = *p;
735 cache = rb_entry(parent, struct dso_cache, rb_node);
736 end = cache->offset + DSO__DATA_CACHE_SIZE;
737
738 if (offset < cache->offset)
739 p = &(*p)->rb_left;
740 else if (offset >= end)
741 p = &(*p)->rb_right;
742 else
743 return cache;
744 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900745
Jiri Olsacdd059d2012-10-27 23:18:32 +0200746 return NULL;
747}
748
Namhyung Kim8e67b722015-05-18 09:30:41 +0900749static struct dso_cache *
750dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200751{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900752 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200753 struct rb_node **p = &root->rb_node;
754 struct rb_node *parent = NULL;
755 struct dso_cache *cache;
756 u64 offset = new->offset;
757
Namhyung Kim8e67b722015-05-18 09:30:41 +0900758 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200759 while (*p != NULL) {
760 u64 end;
761
762 parent = *p;
763 cache = rb_entry(parent, struct dso_cache, rb_node);
764 end = cache->offset + DSO__DATA_CACHE_SIZE;
765
766 if (offset < cache->offset)
767 p = &(*p)->rb_left;
768 else if (offset >= end)
769 p = &(*p)->rb_right;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900770 else
771 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200772 }
773
774 rb_link_node(&new->rb_node, parent, p);
775 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900776
777 cache = NULL;
778out:
779 pthread_mutex_unlock(&dso->lock);
780 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200781}
782
783static ssize_t
784dso_cache__memcpy(struct dso_cache *cache, u64 offset,
785 u8 *data, u64 size)
786{
787 u64 cache_offset = offset - cache->offset;
788 u64 cache_size = min(cache->size - cache_offset, size);
789
790 memcpy(data, cache->data + cache_offset, cache_size);
791 return cache_size;
792}
793
794static ssize_t
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900795dso_cache__read(struct dso *dso, struct machine *machine,
796 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200797{
798 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900799 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200800 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200801
802 do {
803 u64 cache_offset;
804
Jiri Olsacdd059d2012-10-27 23:18:32 +0200805 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
806 if (!cache)
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900807 return -ENOMEM;
808
809 pthread_mutex_lock(&dso__data_open_lock);
810
811 /*
812 * dso->data.fd might be closed if other thread opened another
813 * file (dso) due to open file limit (RLIMIT_NOFILE).
814 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900815 try_to_open_dso(dso, machine);
816
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900817 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900818 ret = -errno;
819 dso->data.status = DSO_DATA_STATUS_ERROR;
820 break;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900821 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200822
823 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200824
Namhyung Kimc52686f2015-01-29 17:02:01 -0300825 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200826 if (ret <= 0)
827 break;
828
829 cache->offset = cache_offset;
830 cache->size = ret;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900831 } while (0);
832
833 pthread_mutex_unlock(&dso__data_open_lock);
834
835 if (ret > 0) {
Namhyung Kim8e67b722015-05-18 09:30:41 +0900836 old = dso_cache__insert(dso, cache);
837 if (old) {
838 /* we lose the race */
839 free(cache);
840 cache = old;
841 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200842
843 ret = dso_cache__memcpy(cache, offset, data, size);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900844 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200845
846 if (ret <= 0)
847 free(cache);
848
Jiri Olsacdd059d2012-10-27 23:18:32 +0200849 return ret;
850}
851
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900852static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
853 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200854{
855 struct dso_cache *cache;
856
Namhyung Kim8e67b722015-05-18 09:30:41 +0900857 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200858 if (cache)
859 return dso_cache__memcpy(cache, offset, data, size);
860 else
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900861 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200862}
863
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200864/*
865 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
866 * in the rb_tree. Any read to already cached data is served
867 * by cached data.
868 */
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900869static ssize_t cached_read(struct dso *dso, struct machine *machine,
870 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200871{
872 ssize_t r = 0;
873 u8 *p = data;
874
875 do {
876 ssize_t ret;
877
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900878 ret = dso_cache_read(dso, machine, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200879 if (ret < 0)
880 return ret;
881
882 /* Reached EOF, return what we have. */
883 if (!ret)
884 break;
885
886 BUG_ON(ret > size);
887
888 r += ret;
889 p += ret;
890 offset += ret;
891 size -= ret;
892
893 } while (size);
894
895 return r;
896}
897
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900898static int data_file_size(struct dso *dso, struct machine *machine)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200899{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900900 int ret = 0;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200901 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000902 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200903
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900904 if (dso->data.file_size)
905 return 0;
906
Namhyung Kim71ff8242015-05-21 01:03:39 +0900907 if (dso->data.status == DSO_DATA_STATUS_ERROR)
908 return -1;
909
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900910 pthread_mutex_lock(&dso__data_open_lock);
911
912 /*
913 * dso->data.fd might be closed if other thread opened another
914 * file (dso) due to open file limit (RLIMIT_NOFILE).
915 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900916 try_to_open_dso(dso, machine);
917
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900918 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900919 ret = -errno;
920 dso->data.status = DSO_DATA_STATUS_ERROR;
921 goto out;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200922 }
923
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900924 if (fstat(dso->data.fd, &st) < 0) {
925 ret = -errno;
926 pr_err("dso cache fstat failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300927 str_error_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900928 dso->data.status = DSO_DATA_STATUS_ERROR;
929 goto out;
930 }
931 dso->data.file_size = st.st_size;
932
933out:
934 pthread_mutex_unlock(&dso__data_open_lock);
935 return ret;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200936}
937
Adrian Hunter6d363452014-07-22 16:17:35 +0300938/**
939 * dso__data_size - Return dso data size
940 * @dso: dso object
941 * @machine: machine object
942 *
943 * Return: dso data size
944 */
945off_t dso__data_size(struct dso *dso, struct machine *machine)
946{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900947 if (data_file_size(dso, machine))
Adrian Hunter6d363452014-07-22 16:17:35 +0300948 return -1;
949
950 /* For now just estimate dso data size is close to file size */
951 return dso->data.file_size;
952}
953
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900954static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
955 u64 offset, u8 *data, ssize_t size)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200956{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900957 if (data_file_size(dso, machine))
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200958 return -1;
959
960 /* Check the offset sanity. */
961 if (offset > dso->data.file_size)
962 return -1;
963
964 if (offset + size < offset)
965 return -1;
966
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900967 return cached_read(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200968}
969
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200970/**
971 * dso__data_read_offset - Read data from dso file offset
972 * @dso: dso object
973 * @machine: machine object
974 * @offset: file offset
975 * @data: buffer to store data
976 * @size: size of the @data buffer
977 *
978 * External interface to read data from dso file offset. Open
979 * dso data file and use cached_read to get the data.
980 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200981ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
982 u64 offset, u8 *data, ssize_t size)
983{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900984 if (dso->data.status == DSO_DATA_STATUS_ERROR)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200985 return -1;
986
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900987 return data_read_offset(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200988}
989
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200990/**
991 * dso__data_read_addr - Read data from dso address
992 * @dso: dso object
993 * @machine: machine object
994 * @add: virtual memory address
995 * @data: buffer to store data
996 * @size: size of the @data buffer
997 *
998 * External interface to read data from dso address.
999 */
Jiri Olsacdd059d2012-10-27 23:18:32 +02001000ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1001 struct machine *machine, u64 addr,
1002 u8 *data, ssize_t size)
1003{
1004 u64 offset = map->map_ip(map, addr);
1005 return dso__data_read_offset(dso, machine, offset, data, size);
1006}
1007
1008struct map *dso__new_map(const char *name)
1009{
1010 struct map *map = NULL;
1011 struct dso *dso = dso__new(name);
1012
1013 if (dso)
1014 map = map__new2(0, dso, MAP__FUNCTION);
1015
1016 return map;
1017}
1018
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -03001019struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1020 const char *short_name, int dso_type)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001021{
1022 /*
1023 * The kernel dso could be created by build_id processing.
1024 */
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001025 struct dso *dso = machine__findnew_dso(machine, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001026
1027 /*
1028 * We need to run this in all cases, since during the build_id
1029 * processing we had no idea this was the kernel dso.
1030 */
1031 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -03001032 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001033 dso->kernel = dso_type;
1034 }
1035
1036 return dso;
1037}
1038
Waiman Long4598a0a2014-09-30 13:36:15 -04001039/*
1040 * Find a matching entry and/or link current entry to RB tree.
1041 * Either one of the dso or name parameter must be non-NULL or the
1042 * function will not work.
1043 */
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001044static struct dso *__dso__findlink_by_longname(struct rb_root *root,
1045 struct dso *dso, const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -04001046{
1047 struct rb_node **p = &root->rb_node;
1048 struct rb_node *parent = NULL;
1049
1050 if (!name)
1051 name = dso->long_name;
1052 /*
1053 * Find node with the matching name
1054 */
1055 while (*p) {
1056 struct dso *this = rb_entry(*p, struct dso, rb_node);
1057 int rc = strcmp(name, this->long_name);
1058
1059 parent = *p;
1060 if (rc == 0) {
1061 /*
1062 * In case the new DSO is a duplicate of an existing
Masahiro Yamada0f5e1552017-02-27 14:28:52 -08001063 * one, print a one-time warning & put the new entry
Waiman Long4598a0a2014-09-30 13:36:15 -04001064 * at the end of the list of duplicates.
1065 */
1066 if (!dso || (dso == this))
1067 return this; /* Find matching dso */
1068 /*
1069 * The core kernel DSOs may have duplicated long name.
1070 * In this case, the short name should be different.
1071 * Comparing the short names to differentiate the DSOs.
1072 */
1073 rc = strcmp(dso->short_name, this->short_name);
1074 if (rc == 0) {
1075 pr_err("Duplicated dso name: %s\n", name);
1076 return NULL;
1077 }
1078 }
1079 if (rc < 0)
1080 p = &parent->rb_left;
1081 else
1082 p = &parent->rb_right;
1083 }
1084 if (dso) {
1085 /* Add new node and rebalance tree */
1086 rb_link_node(&dso->rb_node, parent, p);
1087 rb_insert_color(&dso->rb_node, root);
Adrian Huntere266a752015-11-13 11:48:30 +02001088 dso->root = root;
Waiman Long4598a0a2014-09-30 13:36:15 -04001089 }
1090 return NULL;
1091}
1092
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001093static inline struct dso *__dso__find_by_longname(struct rb_root *root,
1094 const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -04001095{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001096 return __dso__findlink_by_longname(root, NULL, name);
Waiman Long4598a0a2014-09-30 13:36:15 -04001097}
1098
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001099void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001100{
Adrian Huntere266a752015-11-13 11:48:30 +02001101 struct rb_root *root = dso->root;
1102
Jiri Olsacdd059d2012-10-27 23:18:32 +02001103 if (name == NULL)
1104 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001105
1106 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001107 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001108
Adrian Huntere266a752015-11-13 11:48:30 +02001109 if (root) {
1110 rb_erase(&dso->rb_node, root);
1111 /*
1112 * __dso__findlink_by_longname() isn't guaranteed to add it
1113 * back, so a clean removal is required here.
1114 */
1115 RB_CLEAR_NODE(&dso->rb_node);
1116 dso->root = NULL;
1117 }
1118
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001119 dso->long_name = name;
1120 dso->long_name_len = strlen(name);
1121 dso->long_name_allocated = name_allocated;
Adrian Huntere266a752015-11-13 11:48:30 +02001122
1123 if (root)
1124 __dso__findlink_by_longname(root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001125}
1126
Adrian Hunter58a98c92013-12-10 11:11:46 -03001127void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001128{
1129 if (name == NULL)
1130 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -03001131
1132 if (dso->short_name_allocated)
1133 free((char *)dso->short_name);
1134
1135 dso->short_name = name;
1136 dso->short_name_len = strlen(name);
1137 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001138}
1139
1140static void dso__set_basename(struct dso *dso)
1141{
Stephane Eranianac5e7f82013-12-05 19:26:42 +01001142 /*
1143 * basename() may modify path buffer, so we must pass
1144 * a copy.
1145 */
1146 char *base, *lname = strdup(dso->long_name);
1147
1148 if (!lname)
1149 return;
1150
1151 /*
1152 * basename() may return a pointer to internal
1153 * storage which is reused in subsequent calls
1154 * so copy the result.
1155 */
1156 base = strdup(basename(lname));
1157
1158 free(lname);
1159
1160 if (!base)
1161 return;
1162
1163 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001164}
1165
1166int dso__name_len(const struct dso *dso)
1167{
1168 if (!dso)
1169 return strlen("[unknown]");
Namhyung Kimbb963e12017-02-17 17:17:38 +09001170 if (verbose > 0)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001171 return dso->long_name_len;
1172
1173 return dso->short_name_len;
1174}
1175
1176bool dso__loaded(const struct dso *dso, enum map_type type)
1177{
1178 return dso->loaded & (1 << type);
1179}
1180
1181bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1182{
1183 return dso->sorted_by_name & (1 << type);
1184}
1185
1186void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1187{
1188 dso->sorted_by_name |= (1 << type);
1189}
1190
1191struct dso *dso__new(const char *name)
1192{
1193 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1194
1195 if (dso != NULL) {
1196 int i;
1197 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001198 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -03001199 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001200 for (i = 0; i < MAP__NR_TYPES; ++i)
1201 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +02001202 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001203 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +03001204 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001205 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001206 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001207 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001208 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001209 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001210 dso->sorted_by_name = 0;
1211 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +09001212 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +02001213 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001214 dso->kernel = DSO_TYPE_USER;
1215 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -04001216 RB_CLEAR_NODE(&dso->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +02001217 dso->root = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001218 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +02001219 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001220 pthread_mutex_init(&dso->lock, NULL);
Elena Reshetova71008102017-02-21 17:34:58 +02001221 refcount_set(&dso->refcnt, 1);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001222 }
1223
1224 return dso;
1225}
1226
1227void dso__delete(struct dso *dso)
1228{
1229 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -04001230
1231 if (!RB_EMPTY_NODE(&dso->rb_node))
1232 pr_err("DSO %s is still in rbtree when being deleted!\n",
1233 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001234 for (i = 0; i < MAP__NR_TYPES; ++i)
1235 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001236
1237 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001238 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001239 dso->short_name_allocated = false;
1240 }
1241
1242 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001243 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001244 dso->long_name_allocated = false;
1245 }
1246
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001247 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +03001248 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +09001249 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +02001250 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001251 zfree(&dso->symsrc_filename);
Krister Johansen843ff372017-07-05 18:48:08 -07001252 nsinfo__zput(dso->nsinfo);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001253 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001254 free(dso);
1255}
1256
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001257struct dso *dso__get(struct dso *dso)
1258{
1259 if (dso)
Elena Reshetova71008102017-02-21 17:34:58 +02001260 refcount_inc(&dso->refcnt);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001261 return dso;
1262}
1263
1264void dso__put(struct dso *dso)
1265{
Elena Reshetova71008102017-02-21 17:34:58 +02001266 if (dso && refcount_dec_and_test(&dso->refcnt))
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001267 dso__delete(dso);
1268}
1269
Jiri Olsacdd059d2012-10-27 23:18:32 +02001270void dso__set_build_id(struct dso *dso, void *build_id)
1271{
1272 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1273 dso->has_build_id = 1;
1274}
1275
1276bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1277{
1278 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1279}
1280
1281void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1282{
1283 char path[PATH_MAX];
1284
1285 if (machine__is_default_guest(machine))
1286 return;
1287 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1288 if (sysfs__read_build_id(path, dso->build_id,
1289 sizeof(dso->build_id)) == 0)
1290 dso->has_build_id = true;
1291}
1292
1293int dso__kernel_module_get_build_id(struct dso *dso,
1294 const char *root_dir)
1295{
1296 char filename[PATH_MAX];
1297 /*
1298 * kernel module short names are of the form "[module]" and
1299 * we need just "module" here.
1300 */
1301 const char *name = dso->short_name + 1;
1302
1303 snprintf(filename, sizeof(filename),
1304 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1305 root_dir, (int)strlen(name) - 1, name);
1306
1307 if (sysfs__read_build_id(filename, dso->build_id,
1308 sizeof(dso->build_id)) == 0)
1309 dso->has_build_id = true;
1310
1311 return 0;
1312}
1313
1314bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1315{
1316 bool have_build_id = false;
1317 struct dso *pos;
Krister Johansenf045b8c2017-07-05 18:48:11 -07001318 struct nscookie nsc;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001319
1320 list_for_each_entry(pos, head, node) {
He Kuang6ae98ba2016-05-12 08:43:11 +00001321 if (with_hits && !pos->hit && !dso__is_vdso(pos))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001322 continue;
1323 if (pos->has_build_id) {
1324 have_build_id = true;
1325 continue;
1326 }
Krister Johansenf045b8c2017-07-05 18:48:11 -07001327 nsinfo__mountns_enter(pos->nsinfo, &nsc);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001328 if (filename__read_build_id(pos->long_name, pos->build_id,
1329 sizeof(pos->build_id)) > 0) {
1330 have_build_id = true;
1331 pos->has_build_id = true;
1332 }
Krister Johansenf045b8c2017-07-05 18:48:11 -07001333 nsinfo__mountns_exit(&nsc);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001334 }
1335
1336 return have_build_id;
1337}
1338
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001339void __dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001340{
Waiman Long8fa7d872014-09-29 16:07:28 -04001341 list_add_tail(&dso->node, &dsos->head);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001342 __dso__findlink_by_longname(&dsos->root, dso, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001343 /*
1344 * It is now in the linked list, grab a reference, then garbage collect
1345 * this when needing memory, by looking at LRU dso instances in the
1346 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1347 * anywhere besides the one for the list, do, under a lock for the
1348 * list: remove it from the list, then a dso__put(), that probably will
1349 * be the last and will then call dso__delete(), end of life.
1350 *
1351 * That, or at the end of the 'struct machine' lifetime, when all
1352 * 'struct dso' instances will be removed from the list, in
1353 * dsos__exit(), if they have no other reference from some other data
1354 * structure.
1355 *
1356 * E.g.: after processing a 'perf.data' file and storing references
1357 * to objects instantiated while processing events, we will have
1358 * references to the 'thread', 'map', 'dso' structs all from 'struct
1359 * hist_entry' instances, but we may not need anything not referenced,
1360 * so we might as well call machines__exit()/machines__delete() and
1361 * garbage collect it.
1362 */
1363 dso__get(dso);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001364}
1365
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001366void dsos__add(struct dsos *dsos, struct dso *dso)
1367{
1368 pthread_rwlock_wrlock(&dsos->lock);
1369 __dsos__add(dsos, dso);
1370 pthread_rwlock_unlock(&dsos->lock);
1371}
1372
1373struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001374{
1375 struct dso *pos;
1376
Waiman Longf9ceffb2013-05-09 10:42:48 -04001377 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001378 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001379 if (strcmp(pos->short_name, name) == 0)
1380 return pos;
1381 return NULL;
1382 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001383 return __dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001384}
1385
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001386struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1387{
1388 struct dso *dso;
1389 pthread_rwlock_rdlock(&dsos->lock);
1390 dso = __dsos__find(dsos, name, cmp_short);
1391 pthread_rwlock_unlock(&dsos->lock);
1392 return dso;
1393}
1394
1395struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
Jiri Olsa701d8d72015-02-12 22:06:09 +01001396{
1397 struct dso *dso = dso__new(name);
1398
1399 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001400 __dsos__add(dsos, dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001401 dso__set_basename(dso);
Masami Hiramatsu82de26a2015-11-18 15:40:31 +09001402 /* Put dso here because __dsos_add already got it */
1403 dso__put(dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001404 }
1405 return dso;
1406}
1407
Waiman Long8fa7d872014-09-29 16:07:28 -04001408struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001409{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001410 struct dso *dso = __dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001411
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001412 return dso ? dso : __dsos__addnew(dsos, name);
1413}
1414
1415struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1416{
1417 struct dso *dso;
1418 pthread_rwlock_wrlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001419 dso = dso__get(__dsos__findnew(dsos, name));
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001420 pthread_rwlock_unlock(&dsos->lock);
1421 return dso;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001422}
1423
1424size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001425 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001426{
1427 struct dso *pos;
1428 size_t ret = 0;
1429
1430 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001431 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001432 continue;
1433 ret += dso__fprintf_buildid(pos, fp);
1434 ret += fprintf(fp, " %s\n", pos->long_name);
1435 }
1436 return ret;
1437}
1438
1439size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1440{
1441 struct dso *pos;
1442 size_t ret = 0;
1443
1444 list_for_each_entry(pos, head, node) {
1445 int i;
1446 for (i = 0; i < MAP__NR_TYPES; ++i)
1447 ret += dso__fprintf(pos, i, fp);
1448 }
1449
1450 return ret;
1451}
1452
1453size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1454{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001455 char sbuild_id[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +02001456
1457 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1458 return fprintf(fp, "%s", sbuild_id);
1459}
1460
1461size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1462{
1463 struct rb_node *nd;
1464 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1465
1466 if (dso->short_name != dso->long_name)
1467 ret += fprintf(fp, "%s, ", dso->long_name);
1468 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001469 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001470 ret += dso__fprintf_buildid(dso, fp);
1471 ret += fprintf(fp, ")\n");
1472 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1473 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1474 ret += symbol__fprintf(pos, fp);
1475 }
1476
1477 return ret;
1478}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001479
1480enum dso_type dso__type(struct dso *dso, struct machine *machine)
1481{
1482 int fd;
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001483 enum dso_type type = DSO__TYPE_UNKNOWN;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001484
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001485 fd = dso__data_get_fd(dso, machine);
1486 if (fd >= 0) {
1487 type = dso__type_fd(fd);
1488 dso__data_put_fd(dso);
1489 }
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001490
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001491 return type;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001492}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001493
1494int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1495{
1496 int idx, errnum = dso->load_errno;
1497 /*
1498 * This must have a same ordering as the enum dso_load_errno.
1499 */
1500 static const char *dso_load__error_str[] = {
1501 "Internal tools/perf/ library error",
1502 "Invalid ELF file",
1503 "Can not read build id",
1504 "Mismatching build id",
1505 "Decompression failure",
1506 };
1507
1508 BUG_ON(buflen == 0);
1509
1510 if (errnum >= 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001511 const char *err = str_error_r(errnum, buf, buflen);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001512
1513 if (err != buf)
1514 scnprintf(buf, buflen, "%s", err);
1515
1516 return 0;
1517 }
1518
1519 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1520 return -1;
1521
1522 idx = errnum - __DSO_LOAD_ERRNO__START;
1523 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1524 return 0;
1525}