blob: 1a03e9e310a457e140fc3332504cd60d948e7942 [file] [log] [blame]
Jiri Olsabda6ee42014-04-30 15:25:10 +02001#include <asm/bug.h>
Jiri Olsac6580452014-04-30 15:47:27 +02002#include <sys/time.h>
3#include <sys/resource.h>
Jiri Olsacdd059d2012-10-27 23:18:32 +02004#include "symbol.h"
5#include "dso.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -03006#include "machine.h"
Adrian Huntercfe91742015-04-09 18:53:55 +03007#include "auxtrace.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +02008#include "util.h"
9#include "debug.h"
He Kuang6ae98ba2016-05-12 08:43:11 +000010#include "vdso.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020011
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010012static const char * const debuglink_paths[] = {
13 "%.0s%s",
14 "%s/%s",
15 "%s/.debug/%s",
16 "/usr/lib/debug%s/%s"
17};
18
Jiri Olsacdd059d2012-10-27 23:18:32 +020019char dso__symtab_origin(const struct dso *dso)
20{
21 static const char origin[] = {
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020022 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
23 [DSO_BINARY_TYPE__VMLINUX] = 'v',
24 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
25 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
26 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
27 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
28 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
29 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
30 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
31 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
32 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090033 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020034 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
35 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090036 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020037 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Jiri Olsacdd059d2012-10-27 23:18:32 +020038 };
39
40 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
41 return '!';
42 return origin[dso->symtab_type];
43}
44
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -030045int dso__read_binary_type_filename(const struct dso *dso,
46 enum dso_binary_type type,
47 char *root_dir, char *filename, size_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +020048{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +090049 char build_id_hex[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +020050 int ret = 0;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030051 size_t len;
Jiri Olsacdd059d2012-10-27 23:18:32 +020052
53 switch (type) {
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010054 case DSO_BINARY_TYPE__DEBUGLINK:
55 {
56 const char *last_slash;
57 char dso_dir[PATH_MAX];
58 char symfile[PATH_MAX];
59 unsigned int i;
Jiri Olsacdd059d2012-10-27 23:18:32 +020060
Victor Kamenskydc6254c2015-01-26 22:34:02 -080061 len = __symbol__join_symfs(filename, size, dso->long_name);
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010062 last_slash = filename + len;
63 while (last_slash != filename && *last_slash != '/')
64 last_slash--;
Jiri Olsa40356722016-01-20 12:56:32 +010065
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010066 strncpy(dso_dir, filename, last_slash - filename);
67 dso_dir[last_slash-filename] = '\0';
68
69 if (!is_regular_file(filename)) {
70 ret = -1;
71 break;
72 }
73
74 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
75 if (ret)
Jiri Olsa40356722016-01-20 12:56:32 +010076 break;
77
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010078 /* Check predefined locations where debug file might reside */
79 ret = -1;
80 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
81 snprintf(filename, size,
82 debuglink_paths[i], dso_dir, symfile);
83 if (is_regular_file(filename)) {
84 ret = 0;
85 break;
86 }
Jiri Olsacdd059d2012-10-27 23:18:32 +020087 }
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010088
Jiri Olsacdd059d2012-10-27 23:18:32 +020089 break;
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010090 }
Jiri Olsacdd059d2012-10-27 23:18:32 +020091 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
He Kuanga7066702016-05-19 11:47:37 +000092 if (dso__build_id_filename(dso, filename, size) == NULL)
Jiri Olsacdd059d2012-10-27 23:18:32 +020093 ret = -1;
94 break;
95
96 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030097 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
98 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +020099 break;
100
101 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300102 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
103 snprintf(filename + len, size - len, "%s", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200104 break;
105
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200106 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
107 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300108 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200109 size_t dir_size;
110
111 last_slash = dso->long_name + dso->long_name_len;
112 while (last_slash != dso->long_name && *last_slash != '/')
113 last_slash--;
114
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300115 len = __symbol__join_symfs(filename, size, "");
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200116 dir_size = last_slash - dso->long_name + 2;
117 if (dir_size > (size - len)) {
118 ret = -1;
119 break;
120 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300121 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
122 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200123 last_slash);
124 break;
125 }
126
Jiri Olsacdd059d2012-10-27 23:18:32 +0200127 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
128 if (!dso->has_build_id) {
129 ret = -1;
130 break;
131 }
132
133 build_id__sprintf(dso->build_id,
134 sizeof(dso->build_id),
135 build_id_hex);
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300136 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
137 snprintf(filename + len, size - len, "%.2s/%s.debug",
138 build_id_hex, build_id_hex + 2);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200139 break;
140
Adrian Hunter39b12f782013-08-07 14:38:47 +0300141 case DSO_BINARY_TYPE__VMLINUX:
142 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200143 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300144 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200145 break;
146
147 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900148 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300149 path__join3(filename, size, symbol_conf.symfs,
150 root_dir, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200151 break;
152
153 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900154 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300155 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200156 break;
157
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300158 case DSO_BINARY_TYPE__KCORE:
159 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300160 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300161 break;
162
Jiri Olsacdd059d2012-10-27 23:18:32 +0200163 default:
164 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200165 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200166 case DSO_BINARY_TYPE__JAVA_JIT:
167 case DSO_BINARY_TYPE__NOT_FOUND:
168 ret = -1;
169 break;
170 }
171
172 return ret;
173}
174
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900175static const struct {
176 const char *fmt;
177 int (*decompress)(const char *input, int output);
178} compressions[] = {
Namhyung Kime92ce122014-10-31 16:51:38 +0900179#ifdef HAVE_ZLIB_SUPPORT
180 { "gz", gzip_decompress_to_file },
181#endif
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100182#ifdef HAVE_LZMA_SUPPORT
183 { "xz", lzma_decompress_to_file },
184#endif
Namhyung Kime92ce122014-10-31 16:51:38 +0900185 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900186};
187
188bool is_supported_compression(const char *ext)
189{
190 unsigned i;
191
192 for (i = 0; compressions[i].fmt; i++) {
193 if (!strcmp(ext, compressions[i].fmt))
194 return true;
195 }
196 return false;
197}
198
Wang Nan1f121b02015-06-03 08:52:21 +0000199bool is_kernel_module(const char *pathname, int cpumode)
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900200{
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100201 struct kmod_path m;
Wang Nan1f121b02015-06-03 08:52:21 +0000202 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900203
Wang Nan1f121b02015-06-03 08:52:21 +0000204 WARN_ONCE(mode != cpumode,
205 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
206 cpumode);
207
208 switch (mode) {
209 case PERF_RECORD_MISC_USER:
210 case PERF_RECORD_MISC_HYPERVISOR:
211 case PERF_RECORD_MISC_GUEST_USER:
212 return false;
213 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
214 default:
215 if (kmod_path__parse(&m, pathname)) {
216 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
217 pathname);
218 return true;
219 }
220 }
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900221
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100222 return m.kmod;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900223}
224
225bool decompress_to_file(const char *ext, const char *filename, int output_fd)
226{
227 unsigned i;
228
229 for (i = 0; compressions[i].fmt; i++) {
230 if (!strcmp(ext, compressions[i].fmt))
231 return !compressions[i].decompress(filename,
232 output_fd);
233 }
234 return false;
235}
236
237bool dso__needs_decompress(struct dso *dso)
238{
239 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
240 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
241}
242
Jiri Olsaeba51022014-04-30 15:00:59 +0200243/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100244 * Parses kernel module specified in @path and updates
245 * @m argument like:
246 *
247 * @comp - true if @path contains supported compression suffix,
248 * false otherwise
249 * @kmod - true if @path contains '.ko' suffix in right position,
250 * false otherwise
251 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
252 * of the kernel module without suffixes, otherwise strudup-ed
253 * base name of @path
254 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
255 * the compression suffix
256 *
257 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
258 */
259int __kmod_path__parse(struct kmod_path *m, const char *path,
260 bool alloc_name, bool alloc_ext)
261{
262 const char *name = strrchr(path, '/');
263 const char *ext = strrchr(path, '.');
Wang Nan1f121b02015-06-03 08:52:21 +0000264 bool is_simple_name = false;
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100265
266 memset(m, 0x0, sizeof(*m));
267 name = name ? name + 1 : path;
268
Wang Nan1f121b02015-06-03 08:52:21 +0000269 /*
270 * '.' is also a valid character for module name. For example:
271 * [aaa.bbb] is a valid module name. '[' should have higher
272 * priority than '.ko' suffix.
273 *
274 * The kernel names are from machine__mmap_name. Such
275 * name should belong to kernel itself, not kernel module.
276 */
277 if (name[0] == '[') {
278 is_simple_name = true;
279 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
280 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
281 (strncmp(name, "[vdso]", 6) == 0) ||
282 (strncmp(name, "[vsyscall]", 10) == 0)) {
283 m->kmod = false;
284
285 } else
286 m->kmod = true;
287 }
288
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100289 /* No extension, just return name. */
Wang Nan1f121b02015-06-03 08:52:21 +0000290 if ((ext == NULL) || is_simple_name) {
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100291 if (alloc_name) {
292 m->name = strdup(name);
293 return m->name ? 0 : -ENOMEM;
294 }
295 return 0;
296 }
297
298 if (is_supported_compression(ext + 1)) {
299 m->comp = true;
300 ext -= 3;
301 }
302
303 /* Check .ko extension only if there's enough name left. */
304 if (ext > name)
305 m->kmod = !strncmp(ext, ".ko", 3);
306
307 if (alloc_name) {
308 if (m->kmod) {
309 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
310 return -ENOMEM;
311 } else {
312 if (asprintf(&m->name, "%s", name) == -1)
313 return -ENOMEM;
314 }
315
316 strxfrchar(m->name, '-', '_');
317 }
318
319 if (alloc_ext && m->comp) {
320 m->ext = strdup(ext + 4);
321 if (!m->ext) {
322 free((void *) m->name);
323 return -ENOMEM;
324 }
325 }
326
327 return 0;
328}
329
330/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200331 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200332 */
333static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200334static long dso__data_open_cnt;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900335static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
Jiri Olsaeba51022014-04-30 15:00:59 +0200336
337static void dso__list_add(struct dso *dso)
338{
339 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200340 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200341}
342
343static void dso__list_del(struct dso *dso)
344{
345 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200346 WARN_ONCE(dso__data_open_cnt <= 0,
347 "DSO data fd counter out of bounds.");
348 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200349}
350
Jiri Olsaa08cae02014-05-07 21:35:02 +0200351static void close_first_dso(void);
352
353static int do_open(char *name)
354{
355 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000356 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200357
358 do {
359 fd = open(name, O_RDONLY);
360 if (fd >= 0)
361 return fd;
362
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900363 pr_debug("dso open failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300364 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200365 if (!dso__data_open_cnt || errno != EMFILE)
366 break;
367
368 close_first_dso();
369 } while (1);
370
371 return -1;
372}
373
Jiri Olsaeba51022014-04-30 15:00:59 +0200374static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200375{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200376 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300377 char *root_dir = (char *)"";
378 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200379
Jiri Olsacdd059d2012-10-27 23:18:32 +0200380 if (!name)
381 return -ENOMEM;
382
383 if (machine)
384 root_dir = machine->root_dir;
385
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300386 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300387 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200388 free(name);
389 return -EINVAL;
390 }
391
Jiri Olsa3c028a02016-09-20 18:12:45 +0200392 if (!is_regular_file(name))
393 return -EINVAL;
394
Jiri Olsaa08cae02014-05-07 21:35:02 +0200395 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200396 free(name);
397 return fd;
398}
399
Jiri Olsac6580452014-04-30 15:47:27 +0200400static void check_data_close(void);
401
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200402/**
403 * dso_close - Open DSO data file
404 * @dso: dso object
405 *
406 * Open @dso's data file descriptor and updates
407 * list/count of open DSO objects.
408 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200409static int open_dso(struct dso *dso, struct machine *machine)
410{
411 int fd = __open_dso(dso, machine);
412
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300413 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200414 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200415 /*
416 * Check if we crossed the allowed number
417 * of opened DSOs and close one if needed.
418 */
419 check_data_close();
420 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200421
422 return fd;
423}
424
425static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200426{
427 if (dso->data.fd >= 0) {
428 close(dso->data.fd);
429 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200430 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200431 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200432 }
433}
434
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200435/**
436 * dso_close - Close DSO data file
437 * @dso: dso object
438 *
439 * Close @dso's data file descriptor and updates
440 * list/count of open DSO objects.
441 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200442static void close_dso(struct dso *dso)
443{
444 close_data_fd(dso);
445}
446
Jiri Olsac6580452014-04-30 15:47:27 +0200447static void close_first_dso(void)
448{
449 struct dso *dso;
450
451 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
452 close_dso(dso);
453}
454
455static rlim_t get_fd_limit(void)
456{
457 struct rlimit l;
458 rlim_t limit = 0;
459
460 /* Allow half of the current open fd limit. */
461 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
462 if (l.rlim_cur == RLIM_INFINITY)
463 limit = l.rlim_cur;
464 else
465 limit = l.rlim_cur / 2;
466 } else {
467 pr_err("failed to get fd limit\n");
468 limit = 1;
469 }
470
471 return limit;
472}
473
Jiri Olsaf3069242016-06-28 13:29:02 +0200474static rlim_t fd_limit;
475
476/*
477 * Used only by tests/dso-data.c to reset the environment
478 * for tests. I dont expect we should change this during
479 * standard runtime.
480 */
481void reset_fd_limit(void)
482{
483 fd_limit = 0;
484}
485
Jiri Olsac6580452014-04-30 15:47:27 +0200486static bool may_cache_fd(void)
487{
Jiri Olsaf3069242016-06-28 13:29:02 +0200488 if (!fd_limit)
489 fd_limit = get_fd_limit();
Jiri Olsac6580452014-04-30 15:47:27 +0200490
Jiri Olsaf3069242016-06-28 13:29:02 +0200491 if (fd_limit == RLIM_INFINITY)
Jiri Olsac6580452014-04-30 15:47:27 +0200492 return true;
493
Jiri Olsaf3069242016-06-28 13:29:02 +0200494 return fd_limit > (rlim_t) dso__data_open_cnt;
Jiri Olsac6580452014-04-30 15:47:27 +0200495}
496
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200497/*
498 * Check and close LRU dso if we crossed allowed limit
499 * for opened dso file descriptors. The limit is half
500 * of the RLIMIT_NOFILE files opened.
501*/
Jiri Olsac6580452014-04-30 15:47:27 +0200502static void check_data_close(void)
503{
504 bool cache_fd = may_cache_fd();
505
506 if (!cache_fd)
507 close_first_dso();
508}
509
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200510/**
511 * dso__data_close - Close DSO data file
512 * @dso: dso object
513 *
514 * External interface to close @dso's data file descriptor.
515 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200516void dso__data_close(struct dso *dso)
517{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900518 pthread_mutex_lock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200519 close_dso(dso);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900520 pthread_mutex_unlock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200521}
522
Namhyung Kim71ff8242015-05-21 01:03:39 +0900523static void try_to_open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200524{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300525 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200526 DSO_BINARY_TYPE__BUILD_ID_CACHE,
527 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
528 DSO_BINARY_TYPE__NOT_FOUND,
529 };
530 int i = 0;
531
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200532 if (dso->data.fd >= 0)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900533 return;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200534
535 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
536 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300537 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200538 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200539
540 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300541 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200542
Adrian Hunterc27697d2014-07-22 16:17:18 +0300543 dso->data.fd = open_dso(dso, machine);
544 if (dso->data.fd >= 0)
545 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200546
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300547 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300548out:
549 if (dso->data.fd >= 0)
550 dso->data.status = DSO_DATA_STATUS_OK;
551 else
552 dso->data.status = DSO_DATA_STATUS_ERROR;
Namhyung Kim71ff8242015-05-21 01:03:39 +0900553}
Jiri Olsacdd059d2012-10-27 23:18:32 +0200554
Namhyung Kim71ff8242015-05-21 01:03:39 +0900555/**
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900556 * dso__data_get_fd - Get dso's data file descriptor
Namhyung Kim71ff8242015-05-21 01:03:39 +0900557 * @dso: dso object
558 * @machine: machine object
559 *
560 * External interface to find dso's file, open it and
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900561 * returns file descriptor. It should be paired with
562 * dso__data_put_fd() if it returns non-negative value.
Namhyung Kim71ff8242015-05-21 01:03:39 +0900563 */
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900564int dso__data_get_fd(struct dso *dso, struct machine *machine)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900565{
566 if (dso->data.status == DSO_DATA_STATUS_ERROR)
567 return -1;
568
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900569 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
570 return -1;
571
Namhyung Kim71ff8242015-05-21 01:03:39 +0900572 try_to_open_dso(dso, machine);
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900573
574 if (dso->data.fd < 0)
575 pthread_mutex_unlock(&dso__data_open_lock);
Namhyung Kim71ff8242015-05-21 01:03:39 +0900576
Adrian Hunterc27697d2014-07-22 16:17:18 +0300577 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200578}
579
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900580void dso__data_put_fd(struct dso *dso __maybe_unused)
581{
582 pthread_mutex_unlock(&dso__data_open_lock);
583}
584
Adrian Hunter288be942014-07-22 16:17:19 +0300585bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
586{
587 u32 flag = 1 << by;
588
589 if (dso->data.status_seen & flag)
590 return true;
591
592 dso->data.status_seen |= flag;
593
594 return false;
595}
596
Jiri Olsacdd059d2012-10-27 23:18:32 +0200597static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900598dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200599{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900600 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200601 struct rb_node *next = rb_first(root);
602
Namhyung Kim8e67b722015-05-18 09:30:41 +0900603 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200604 while (next) {
605 struct dso_cache *cache;
606
607 cache = rb_entry(next, struct dso_cache, rb_node);
608 next = rb_next(&cache->rb_node);
609 rb_erase(&cache->rb_node, root);
610 free(cache);
611 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900612 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200613}
614
Namhyung Kim8e67b722015-05-18 09:30:41 +0900615static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200616{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900617 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300618 struct rb_node * const *p = &root->rb_node;
619 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200620 struct dso_cache *cache;
621
622 while (*p != NULL) {
623 u64 end;
624
625 parent = *p;
626 cache = rb_entry(parent, struct dso_cache, rb_node);
627 end = cache->offset + DSO__DATA_CACHE_SIZE;
628
629 if (offset < cache->offset)
630 p = &(*p)->rb_left;
631 else if (offset >= end)
632 p = &(*p)->rb_right;
633 else
634 return cache;
635 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900636
Jiri Olsacdd059d2012-10-27 23:18:32 +0200637 return NULL;
638}
639
Namhyung Kim8e67b722015-05-18 09:30:41 +0900640static struct dso_cache *
641dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200642{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900643 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200644 struct rb_node **p = &root->rb_node;
645 struct rb_node *parent = NULL;
646 struct dso_cache *cache;
647 u64 offset = new->offset;
648
Namhyung Kim8e67b722015-05-18 09:30:41 +0900649 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200650 while (*p != NULL) {
651 u64 end;
652
653 parent = *p;
654 cache = rb_entry(parent, struct dso_cache, rb_node);
655 end = cache->offset + DSO__DATA_CACHE_SIZE;
656
657 if (offset < cache->offset)
658 p = &(*p)->rb_left;
659 else if (offset >= end)
660 p = &(*p)->rb_right;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900661 else
662 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200663 }
664
665 rb_link_node(&new->rb_node, parent, p);
666 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900667
668 cache = NULL;
669out:
670 pthread_mutex_unlock(&dso->lock);
671 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200672}
673
674static ssize_t
675dso_cache__memcpy(struct dso_cache *cache, u64 offset,
676 u8 *data, u64 size)
677{
678 u64 cache_offset = offset - cache->offset;
679 u64 cache_size = min(cache->size - cache_offset, size);
680
681 memcpy(data, cache->data + cache_offset, cache_size);
682 return cache_size;
683}
684
685static ssize_t
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900686dso_cache__read(struct dso *dso, struct machine *machine,
687 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200688{
689 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900690 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200691 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200692
693 do {
694 u64 cache_offset;
695
Jiri Olsacdd059d2012-10-27 23:18:32 +0200696 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
697 if (!cache)
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900698 return -ENOMEM;
699
700 pthread_mutex_lock(&dso__data_open_lock);
701
702 /*
703 * dso->data.fd might be closed if other thread opened another
704 * file (dso) due to open file limit (RLIMIT_NOFILE).
705 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900706 try_to_open_dso(dso, machine);
707
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900708 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900709 ret = -errno;
710 dso->data.status = DSO_DATA_STATUS_ERROR;
711 break;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900712 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200713
714 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200715
Namhyung Kimc52686f2015-01-29 17:02:01 -0300716 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200717 if (ret <= 0)
718 break;
719
720 cache->offset = cache_offset;
721 cache->size = ret;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900722 } while (0);
723
724 pthread_mutex_unlock(&dso__data_open_lock);
725
726 if (ret > 0) {
Namhyung Kim8e67b722015-05-18 09:30:41 +0900727 old = dso_cache__insert(dso, cache);
728 if (old) {
729 /* we lose the race */
730 free(cache);
731 cache = old;
732 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200733
734 ret = dso_cache__memcpy(cache, offset, data, size);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900735 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200736
737 if (ret <= 0)
738 free(cache);
739
Jiri Olsacdd059d2012-10-27 23:18:32 +0200740 return ret;
741}
742
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900743static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
744 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200745{
746 struct dso_cache *cache;
747
Namhyung Kim8e67b722015-05-18 09:30:41 +0900748 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200749 if (cache)
750 return dso_cache__memcpy(cache, offset, data, size);
751 else
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900752 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200753}
754
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200755/*
756 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
757 * in the rb_tree. Any read to already cached data is served
758 * by cached data.
759 */
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900760static ssize_t cached_read(struct dso *dso, struct machine *machine,
761 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200762{
763 ssize_t r = 0;
764 u8 *p = data;
765
766 do {
767 ssize_t ret;
768
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900769 ret = dso_cache_read(dso, machine, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200770 if (ret < 0)
771 return ret;
772
773 /* Reached EOF, return what we have. */
774 if (!ret)
775 break;
776
777 BUG_ON(ret > size);
778
779 r += ret;
780 p += ret;
781 offset += ret;
782 size -= ret;
783
784 } while (size);
785
786 return r;
787}
788
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900789static int data_file_size(struct dso *dso, struct machine *machine)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200790{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900791 int ret = 0;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200792 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000793 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200794
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900795 if (dso->data.file_size)
796 return 0;
797
Namhyung Kim71ff8242015-05-21 01:03:39 +0900798 if (dso->data.status == DSO_DATA_STATUS_ERROR)
799 return -1;
800
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900801 pthread_mutex_lock(&dso__data_open_lock);
802
803 /*
804 * dso->data.fd might be closed if other thread opened another
805 * file (dso) due to open file limit (RLIMIT_NOFILE).
806 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900807 try_to_open_dso(dso, machine);
808
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900809 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900810 ret = -errno;
811 dso->data.status = DSO_DATA_STATUS_ERROR;
812 goto out;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200813 }
814
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900815 if (fstat(dso->data.fd, &st) < 0) {
816 ret = -errno;
817 pr_err("dso cache fstat failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300818 str_error_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900819 dso->data.status = DSO_DATA_STATUS_ERROR;
820 goto out;
821 }
822 dso->data.file_size = st.st_size;
823
824out:
825 pthread_mutex_unlock(&dso__data_open_lock);
826 return ret;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200827}
828
Adrian Hunter6d363452014-07-22 16:17:35 +0300829/**
830 * dso__data_size - Return dso data size
831 * @dso: dso object
832 * @machine: machine object
833 *
834 * Return: dso data size
835 */
836off_t dso__data_size(struct dso *dso, struct machine *machine)
837{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900838 if (data_file_size(dso, machine))
Adrian Hunter6d363452014-07-22 16:17:35 +0300839 return -1;
840
841 /* For now just estimate dso data size is close to file size */
842 return dso->data.file_size;
843}
844
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900845static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
846 u64 offset, u8 *data, ssize_t size)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200847{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900848 if (data_file_size(dso, machine))
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200849 return -1;
850
851 /* Check the offset sanity. */
852 if (offset > dso->data.file_size)
853 return -1;
854
855 if (offset + size < offset)
856 return -1;
857
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900858 return cached_read(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200859}
860
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200861/**
862 * dso__data_read_offset - Read data from dso file offset
863 * @dso: dso object
864 * @machine: machine object
865 * @offset: file offset
866 * @data: buffer to store data
867 * @size: size of the @data buffer
868 *
869 * External interface to read data from dso file offset. Open
870 * dso data file and use cached_read to get the data.
871 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200872ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
873 u64 offset, u8 *data, ssize_t size)
874{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900875 if (dso->data.status == DSO_DATA_STATUS_ERROR)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200876 return -1;
877
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900878 return data_read_offset(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200879}
880
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200881/**
882 * dso__data_read_addr - Read data from dso address
883 * @dso: dso object
884 * @machine: machine object
885 * @add: virtual memory address
886 * @data: buffer to store data
887 * @size: size of the @data buffer
888 *
889 * External interface to read data from dso address.
890 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200891ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
892 struct machine *machine, u64 addr,
893 u8 *data, ssize_t size)
894{
895 u64 offset = map->map_ip(map, addr);
896 return dso__data_read_offset(dso, machine, offset, data, size);
897}
898
899struct map *dso__new_map(const char *name)
900{
901 struct map *map = NULL;
902 struct dso *dso = dso__new(name);
903
904 if (dso)
905 map = map__new2(0, dso, MAP__FUNCTION);
906
907 return map;
908}
909
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300910struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
911 const char *short_name, int dso_type)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200912{
913 /*
914 * The kernel dso could be created by build_id processing.
915 */
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300916 struct dso *dso = machine__findnew_dso(machine, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200917
918 /*
919 * We need to run this in all cases, since during the build_id
920 * processing we had no idea this was the kernel dso.
921 */
922 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300923 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200924 dso->kernel = dso_type;
925 }
926
927 return dso;
928}
929
Waiman Long4598a0a2014-09-30 13:36:15 -0400930/*
931 * Find a matching entry and/or link current entry to RB tree.
932 * Either one of the dso or name parameter must be non-NULL or the
933 * function will not work.
934 */
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300935static struct dso *__dso__findlink_by_longname(struct rb_root *root,
936 struct dso *dso, const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400937{
938 struct rb_node **p = &root->rb_node;
939 struct rb_node *parent = NULL;
940
941 if (!name)
942 name = dso->long_name;
943 /*
944 * Find node with the matching name
945 */
946 while (*p) {
947 struct dso *this = rb_entry(*p, struct dso, rb_node);
948 int rc = strcmp(name, this->long_name);
949
950 parent = *p;
951 if (rc == 0) {
952 /*
953 * In case the new DSO is a duplicate of an existing
954 * one, print an one-time warning & put the new entry
955 * at the end of the list of duplicates.
956 */
957 if (!dso || (dso == this))
958 return this; /* Find matching dso */
959 /*
960 * The core kernel DSOs may have duplicated long name.
961 * In this case, the short name should be different.
962 * Comparing the short names to differentiate the DSOs.
963 */
964 rc = strcmp(dso->short_name, this->short_name);
965 if (rc == 0) {
966 pr_err("Duplicated dso name: %s\n", name);
967 return NULL;
968 }
969 }
970 if (rc < 0)
971 p = &parent->rb_left;
972 else
973 p = &parent->rb_right;
974 }
975 if (dso) {
976 /* Add new node and rebalance tree */
977 rb_link_node(&dso->rb_node, parent, p);
978 rb_insert_color(&dso->rb_node, root);
Adrian Huntere266a752015-11-13 11:48:30 +0200979 dso->root = root;
Waiman Long4598a0a2014-09-30 13:36:15 -0400980 }
981 return NULL;
982}
983
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300984static inline struct dso *__dso__find_by_longname(struct rb_root *root,
985 const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400986{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300987 return __dso__findlink_by_longname(root, NULL, name);
Waiman Long4598a0a2014-09-30 13:36:15 -0400988}
989
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300990void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200991{
Adrian Huntere266a752015-11-13 11:48:30 +0200992 struct rb_root *root = dso->root;
993
Jiri Olsacdd059d2012-10-27 23:18:32 +0200994 if (name == NULL)
995 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300996
997 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300998 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300999
Adrian Huntere266a752015-11-13 11:48:30 +02001000 if (root) {
1001 rb_erase(&dso->rb_node, root);
1002 /*
1003 * __dso__findlink_by_longname() isn't guaranteed to add it
1004 * back, so a clean removal is required here.
1005 */
1006 RB_CLEAR_NODE(&dso->rb_node);
1007 dso->root = NULL;
1008 }
1009
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001010 dso->long_name = name;
1011 dso->long_name_len = strlen(name);
1012 dso->long_name_allocated = name_allocated;
Adrian Huntere266a752015-11-13 11:48:30 +02001013
1014 if (root)
1015 __dso__findlink_by_longname(root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001016}
1017
Adrian Hunter58a98c92013-12-10 11:11:46 -03001018void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001019{
1020 if (name == NULL)
1021 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -03001022
1023 if (dso->short_name_allocated)
1024 free((char *)dso->short_name);
1025
1026 dso->short_name = name;
1027 dso->short_name_len = strlen(name);
1028 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001029}
1030
1031static void dso__set_basename(struct dso *dso)
1032{
Stephane Eranianac5e7f82013-12-05 19:26:42 +01001033 /*
1034 * basename() may modify path buffer, so we must pass
1035 * a copy.
1036 */
1037 char *base, *lname = strdup(dso->long_name);
1038
1039 if (!lname)
1040 return;
1041
1042 /*
1043 * basename() may return a pointer to internal
1044 * storage which is reused in subsequent calls
1045 * so copy the result.
1046 */
1047 base = strdup(basename(lname));
1048
1049 free(lname);
1050
1051 if (!base)
1052 return;
1053
1054 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001055}
1056
1057int dso__name_len(const struct dso *dso)
1058{
1059 if (!dso)
1060 return strlen("[unknown]");
Namhyung Kimbb963e12017-02-17 17:17:38 +09001061 if (verbose > 0)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001062 return dso->long_name_len;
1063
1064 return dso->short_name_len;
1065}
1066
1067bool dso__loaded(const struct dso *dso, enum map_type type)
1068{
1069 return dso->loaded & (1 << type);
1070}
1071
1072bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1073{
1074 return dso->sorted_by_name & (1 << type);
1075}
1076
1077void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1078{
1079 dso->sorted_by_name |= (1 << type);
1080}
1081
1082struct dso *dso__new(const char *name)
1083{
1084 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1085
1086 if (dso != NULL) {
1087 int i;
1088 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001089 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -03001090 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001091 for (i = 0; i < MAP__NR_TYPES; ++i)
1092 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +02001093 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001094 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +03001095 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001096 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001097 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001098 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001099 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001100 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001101 dso->sorted_by_name = 0;
1102 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +09001103 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +02001104 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001105 dso->kernel = DSO_TYPE_USER;
1106 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -04001107 RB_CLEAR_NODE(&dso->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +02001108 dso->root = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001109 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +02001110 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001111 pthread_mutex_init(&dso->lock, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001112 atomic_set(&dso->refcnt, 1);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001113 }
1114
1115 return dso;
1116}
1117
1118void dso__delete(struct dso *dso)
1119{
1120 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -04001121
1122 if (!RB_EMPTY_NODE(&dso->rb_node))
1123 pr_err("DSO %s is still in rbtree when being deleted!\n",
1124 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001125 for (i = 0; i < MAP__NR_TYPES; ++i)
1126 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001127
1128 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001129 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001130 dso->short_name_allocated = false;
1131 }
1132
1133 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001134 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001135 dso->long_name_allocated = false;
1136 }
1137
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001138 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +03001139 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +09001140 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +02001141 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001142 zfree(&dso->symsrc_filename);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001143 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001144 free(dso);
1145}
1146
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001147struct dso *dso__get(struct dso *dso)
1148{
1149 if (dso)
1150 atomic_inc(&dso->refcnt);
1151 return dso;
1152}
1153
1154void dso__put(struct dso *dso)
1155{
1156 if (dso && atomic_dec_and_test(&dso->refcnt))
1157 dso__delete(dso);
1158}
1159
Jiri Olsacdd059d2012-10-27 23:18:32 +02001160void dso__set_build_id(struct dso *dso, void *build_id)
1161{
1162 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1163 dso->has_build_id = 1;
1164}
1165
1166bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1167{
1168 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1169}
1170
1171void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1172{
1173 char path[PATH_MAX];
1174
1175 if (machine__is_default_guest(machine))
1176 return;
1177 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1178 if (sysfs__read_build_id(path, dso->build_id,
1179 sizeof(dso->build_id)) == 0)
1180 dso->has_build_id = true;
1181}
1182
1183int dso__kernel_module_get_build_id(struct dso *dso,
1184 const char *root_dir)
1185{
1186 char filename[PATH_MAX];
1187 /*
1188 * kernel module short names are of the form "[module]" and
1189 * we need just "module" here.
1190 */
1191 const char *name = dso->short_name + 1;
1192
1193 snprintf(filename, sizeof(filename),
1194 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1195 root_dir, (int)strlen(name) - 1, name);
1196
1197 if (sysfs__read_build_id(filename, dso->build_id,
1198 sizeof(dso->build_id)) == 0)
1199 dso->has_build_id = true;
1200
1201 return 0;
1202}
1203
1204bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1205{
1206 bool have_build_id = false;
1207 struct dso *pos;
1208
1209 list_for_each_entry(pos, head, node) {
He Kuang6ae98ba2016-05-12 08:43:11 +00001210 if (with_hits && !pos->hit && !dso__is_vdso(pos))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001211 continue;
1212 if (pos->has_build_id) {
1213 have_build_id = true;
1214 continue;
1215 }
1216 if (filename__read_build_id(pos->long_name, pos->build_id,
1217 sizeof(pos->build_id)) > 0) {
1218 have_build_id = true;
1219 pos->has_build_id = true;
1220 }
1221 }
1222
1223 return have_build_id;
1224}
1225
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001226void __dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001227{
Waiman Long8fa7d872014-09-29 16:07:28 -04001228 list_add_tail(&dso->node, &dsos->head);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001229 __dso__findlink_by_longname(&dsos->root, dso, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001230 /*
1231 * It is now in the linked list, grab a reference, then garbage collect
1232 * this when needing memory, by looking at LRU dso instances in the
1233 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1234 * anywhere besides the one for the list, do, under a lock for the
1235 * list: remove it from the list, then a dso__put(), that probably will
1236 * be the last and will then call dso__delete(), end of life.
1237 *
1238 * That, or at the end of the 'struct machine' lifetime, when all
1239 * 'struct dso' instances will be removed from the list, in
1240 * dsos__exit(), if they have no other reference from some other data
1241 * structure.
1242 *
1243 * E.g.: after processing a 'perf.data' file and storing references
1244 * to objects instantiated while processing events, we will have
1245 * references to the 'thread', 'map', 'dso' structs all from 'struct
1246 * hist_entry' instances, but we may not need anything not referenced,
1247 * so we might as well call machines__exit()/machines__delete() and
1248 * garbage collect it.
1249 */
1250 dso__get(dso);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001251}
1252
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001253void dsos__add(struct dsos *dsos, struct dso *dso)
1254{
1255 pthread_rwlock_wrlock(&dsos->lock);
1256 __dsos__add(dsos, dso);
1257 pthread_rwlock_unlock(&dsos->lock);
1258}
1259
1260struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001261{
1262 struct dso *pos;
1263
Waiman Longf9ceffb2013-05-09 10:42:48 -04001264 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001265 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001266 if (strcmp(pos->short_name, name) == 0)
1267 return pos;
1268 return NULL;
1269 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001270 return __dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001271}
1272
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001273struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1274{
1275 struct dso *dso;
1276 pthread_rwlock_rdlock(&dsos->lock);
1277 dso = __dsos__find(dsos, name, cmp_short);
1278 pthread_rwlock_unlock(&dsos->lock);
1279 return dso;
1280}
1281
1282struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
Jiri Olsa701d8d72015-02-12 22:06:09 +01001283{
1284 struct dso *dso = dso__new(name);
1285
1286 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001287 __dsos__add(dsos, dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001288 dso__set_basename(dso);
Masami Hiramatsu82de26a2015-11-18 15:40:31 +09001289 /* Put dso here because __dsos_add already got it */
1290 dso__put(dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001291 }
1292 return dso;
1293}
1294
Waiman Long8fa7d872014-09-29 16:07:28 -04001295struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001296{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001297 struct dso *dso = __dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001298
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001299 return dso ? dso : __dsos__addnew(dsos, name);
1300}
1301
1302struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1303{
1304 struct dso *dso;
1305 pthread_rwlock_wrlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001306 dso = dso__get(__dsos__findnew(dsos, name));
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001307 pthread_rwlock_unlock(&dsos->lock);
1308 return dso;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001309}
1310
1311size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001312 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001313{
1314 struct dso *pos;
1315 size_t ret = 0;
1316
1317 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001318 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001319 continue;
1320 ret += dso__fprintf_buildid(pos, fp);
1321 ret += fprintf(fp, " %s\n", pos->long_name);
1322 }
1323 return ret;
1324}
1325
1326size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1327{
1328 struct dso *pos;
1329 size_t ret = 0;
1330
1331 list_for_each_entry(pos, head, node) {
1332 int i;
1333 for (i = 0; i < MAP__NR_TYPES; ++i)
1334 ret += dso__fprintf(pos, i, fp);
1335 }
1336
1337 return ret;
1338}
1339
1340size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1341{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001342 char sbuild_id[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +02001343
1344 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1345 return fprintf(fp, "%s", sbuild_id);
1346}
1347
1348size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1349{
1350 struct rb_node *nd;
1351 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1352
1353 if (dso->short_name != dso->long_name)
1354 ret += fprintf(fp, "%s, ", dso->long_name);
1355 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001356 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001357 ret += dso__fprintf_buildid(dso, fp);
1358 ret += fprintf(fp, ")\n");
1359 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1360 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1361 ret += symbol__fprintf(pos, fp);
1362 }
1363
1364 return ret;
1365}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001366
1367enum dso_type dso__type(struct dso *dso, struct machine *machine)
1368{
1369 int fd;
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001370 enum dso_type type = DSO__TYPE_UNKNOWN;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001371
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001372 fd = dso__data_get_fd(dso, machine);
1373 if (fd >= 0) {
1374 type = dso__type_fd(fd);
1375 dso__data_put_fd(dso);
1376 }
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001377
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001378 return type;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001379}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001380
1381int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1382{
1383 int idx, errnum = dso->load_errno;
1384 /*
1385 * This must have a same ordering as the enum dso_load_errno.
1386 */
1387 static const char *dso_load__error_str[] = {
1388 "Internal tools/perf/ library error",
1389 "Invalid ELF file",
1390 "Can not read build id",
1391 "Mismatching build id",
1392 "Decompression failure",
1393 };
1394
1395 BUG_ON(buflen == 0);
1396
1397 if (errnum >= 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001398 const char *err = str_error_r(errnum, buf, buflen);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001399
1400 if (err != buf)
1401 scnprintf(buf, buflen, "%s", err);
1402
1403 return 0;
1404 }
1405
1406 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1407 return -1;
1408
1409 idx = errnum - __DSO_LOAD_ERRNO__START;
1410 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1411 return 0;
1412}