blob: a96a99d2369f800634025bcdfa9838d1d6bc9d97 [file] [log] [blame]
Jiri Olsabda6ee42014-04-30 15:25:10 +02001#include <asm/bug.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03002#include <linux/kernel.h>
Jiri Olsac6580452014-04-30 15:47:27 +02003#include <sys/time.h>
4#include <sys/resource.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03005#include <sys/types.h>
6#include <sys/stat.h>
7#include <unistd.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03008#include <errno.h>
Arnaldo Carvalho de Melo611f0af2017-04-19 16:29:38 -03009#include "compress.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030010#include "path.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020011#include "symbol.h"
12#include "dso.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030013#include "machine.h"
Adrian Huntercfe91742015-04-09 18:53:55 +030014#include "auxtrace.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020015#include "util.h"
16#include "debug.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030017#include "string2.h"
He Kuang6ae98ba2016-05-12 08:43:11 +000018#include "vdso.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020019
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010020static const char * const debuglink_paths[] = {
21 "%.0s%s",
22 "%s/%s",
23 "%s/.debug/%s",
24 "/usr/lib/debug%s/%s"
25};
26
Jiri Olsacdd059d2012-10-27 23:18:32 +020027char dso__symtab_origin(const struct dso *dso)
28{
29 static const char origin[] = {
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020030 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
31 [DSO_BINARY_TYPE__VMLINUX] = 'v',
32 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
33 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
34 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
35 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
36 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
37 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
38 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
39 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
40 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090041 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020042 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
43 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090044 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020045 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Jiri Olsacdd059d2012-10-27 23:18:32 +020046 };
47
48 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
49 return '!';
50 return origin[dso->symtab_type];
51}
52
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -030053int dso__read_binary_type_filename(const struct dso *dso,
54 enum dso_binary_type type,
55 char *root_dir, char *filename, size_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +020056{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +090057 char build_id_hex[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +020058 int ret = 0;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030059 size_t len;
Jiri Olsacdd059d2012-10-27 23:18:32 +020060
61 switch (type) {
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010062 case DSO_BINARY_TYPE__DEBUGLINK:
63 {
64 const char *last_slash;
65 char dso_dir[PATH_MAX];
66 char symfile[PATH_MAX];
67 unsigned int i;
Jiri Olsacdd059d2012-10-27 23:18:32 +020068
Victor Kamenskydc6254c2015-01-26 22:34:02 -080069 len = __symbol__join_symfs(filename, size, dso->long_name);
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010070 last_slash = filename + len;
71 while (last_slash != filename && *last_slash != '/')
72 last_slash--;
Jiri Olsa40356722016-01-20 12:56:32 +010073
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010074 strncpy(dso_dir, filename, last_slash - filename);
75 dso_dir[last_slash-filename] = '\0';
76
77 if (!is_regular_file(filename)) {
78 ret = -1;
79 break;
80 }
81
82 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
83 if (ret)
Jiri Olsa40356722016-01-20 12:56:32 +010084 break;
85
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010086 /* Check predefined locations where debug file might reside */
87 ret = -1;
88 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
89 snprintf(filename, size,
90 debuglink_paths[i], dso_dir, symfile);
91 if (is_regular_file(filename)) {
92 ret = 0;
93 break;
94 }
Jiri Olsacdd059d2012-10-27 23:18:32 +020095 }
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010096
Jiri Olsacdd059d2012-10-27 23:18:32 +020097 break;
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010098 }
Jiri Olsacdd059d2012-10-27 23:18:32 +020099 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
He Kuanga7066702016-05-19 11:47:37 +0000100 if (dso__build_id_filename(dso, filename, size) == NULL)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200101 ret = -1;
102 break;
103
104 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300105 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
106 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200107 break;
108
109 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300110 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
111 snprintf(filename + len, size - len, "%s", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200112 break;
113
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200114 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
115 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300116 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200117 size_t dir_size;
118
119 last_slash = dso->long_name + dso->long_name_len;
120 while (last_slash != dso->long_name && *last_slash != '/')
121 last_slash--;
122
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300123 len = __symbol__join_symfs(filename, size, "");
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200124 dir_size = last_slash - dso->long_name + 2;
125 if (dir_size > (size - len)) {
126 ret = -1;
127 break;
128 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300129 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
130 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200131 last_slash);
132 break;
133 }
134
Jiri Olsacdd059d2012-10-27 23:18:32 +0200135 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
136 if (!dso->has_build_id) {
137 ret = -1;
138 break;
139 }
140
141 build_id__sprintf(dso->build_id,
142 sizeof(dso->build_id),
143 build_id_hex);
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300144 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
145 snprintf(filename + len, size - len, "%.2s/%s.debug",
146 build_id_hex, build_id_hex + 2);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200147 break;
148
Adrian Hunter39b12f782013-08-07 14:38:47 +0300149 case DSO_BINARY_TYPE__VMLINUX:
150 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200151 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300152 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200153 break;
154
155 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900156 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300157 path__join3(filename, size, symbol_conf.symfs,
158 root_dir, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200159 break;
160
161 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900162 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300163 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200164 break;
165
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300166 case DSO_BINARY_TYPE__KCORE:
167 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300168 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300169 break;
170
Jiri Olsacdd059d2012-10-27 23:18:32 +0200171 default:
172 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200173 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200174 case DSO_BINARY_TYPE__JAVA_JIT:
175 case DSO_BINARY_TYPE__NOT_FOUND:
176 ret = -1;
177 break;
178 }
179
180 return ret;
181}
182
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900183static const struct {
184 const char *fmt;
185 int (*decompress)(const char *input, int output);
186} compressions[] = {
Namhyung Kime92ce122014-10-31 16:51:38 +0900187#ifdef HAVE_ZLIB_SUPPORT
188 { "gz", gzip_decompress_to_file },
189#endif
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100190#ifdef HAVE_LZMA_SUPPORT
191 { "xz", lzma_decompress_to_file },
192#endif
Namhyung Kime92ce122014-10-31 16:51:38 +0900193 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900194};
195
196bool is_supported_compression(const char *ext)
197{
198 unsigned i;
199
200 for (i = 0; compressions[i].fmt; i++) {
201 if (!strcmp(ext, compressions[i].fmt))
202 return true;
203 }
204 return false;
205}
206
Wang Nan1f121b02015-06-03 08:52:21 +0000207bool is_kernel_module(const char *pathname, int cpumode)
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900208{
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100209 struct kmod_path m;
Wang Nan1f121b02015-06-03 08:52:21 +0000210 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900211
Wang Nan1f121b02015-06-03 08:52:21 +0000212 WARN_ONCE(mode != cpumode,
213 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
214 cpumode);
215
216 switch (mode) {
217 case PERF_RECORD_MISC_USER:
218 case PERF_RECORD_MISC_HYPERVISOR:
219 case PERF_RECORD_MISC_GUEST_USER:
220 return false;
221 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
222 default:
223 if (kmod_path__parse(&m, pathname)) {
224 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
225 pathname);
226 return true;
227 }
228 }
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900229
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100230 return m.kmod;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900231}
232
233bool decompress_to_file(const char *ext, const char *filename, int output_fd)
234{
235 unsigned i;
236
237 for (i = 0; compressions[i].fmt; i++) {
238 if (!strcmp(ext, compressions[i].fmt))
239 return !compressions[i].decompress(filename,
240 output_fd);
241 }
242 return false;
243}
244
245bool dso__needs_decompress(struct dso *dso)
246{
247 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
248 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
249}
250
Jiri Olsaeba51022014-04-30 15:00:59 +0200251/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100252 * Parses kernel module specified in @path and updates
253 * @m argument like:
254 *
255 * @comp - true if @path contains supported compression suffix,
256 * false otherwise
257 * @kmod - true if @path contains '.ko' suffix in right position,
258 * false otherwise
259 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
260 * of the kernel module without suffixes, otherwise strudup-ed
261 * base name of @path
262 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
263 * the compression suffix
264 *
265 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
266 */
267int __kmod_path__parse(struct kmod_path *m, const char *path,
268 bool alloc_name, bool alloc_ext)
269{
270 const char *name = strrchr(path, '/');
271 const char *ext = strrchr(path, '.');
Wang Nan1f121b02015-06-03 08:52:21 +0000272 bool is_simple_name = false;
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100273
274 memset(m, 0x0, sizeof(*m));
275 name = name ? name + 1 : path;
276
Wang Nan1f121b02015-06-03 08:52:21 +0000277 /*
278 * '.' is also a valid character for module name. For example:
279 * [aaa.bbb] is a valid module name. '[' should have higher
280 * priority than '.ko' suffix.
281 *
282 * The kernel names are from machine__mmap_name. Such
283 * name should belong to kernel itself, not kernel module.
284 */
285 if (name[0] == '[') {
286 is_simple_name = true;
287 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
288 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
289 (strncmp(name, "[vdso]", 6) == 0) ||
290 (strncmp(name, "[vsyscall]", 10) == 0)) {
291 m->kmod = false;
292
293 } else
294 m->kmod = true;
295 }
296
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100297 /* No extension, just return name. */
Wang Nan1f121b02015-06-03 08:52:21 +0000298 if ((ext == NULL) || is_simple_name) {
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100299 if (alloc_name) {
300 m->name = strdup(name);
301 return m->name ? 0 : -ENOMEM;
302 }
303 return 0;
304 }
305
306 if (is_supported_compression(ext + 1)) {
307 m->comp = true;
308 ext -= 3;
309 }
310
311 /* Check .ko extension only if there's enough name left. */
312 if (ext > name)
313 m->kmod = !strncmp(ext, ".ko", 3);
314
315 if (alloc_name) {
316 if (m->kmod) {
317 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
318 return -ENOMEM;
319 } else {
320 if (asprintf(&m->name, "%s", name) == -1)
321 return -ENOMEM;
322 }
323
324 strxfrchar(m->name, '-', '_');
325 }
326
327 if (alloc_ext && m->comp) {
328 m->ext = strdup(ext + 4);
329 if (!m->ext) {
330 free((void *) m->name);
331 return -ENOMEM;
332 }
333 }
334
335 return 0;
336}
337
338/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200339 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200340 */
341static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200342static long dso__data_open_cnt;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900343static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
Jiri Olsaeba51022014-04-30 15:00:59 +0200344
345static void dso__list_add(struct dso *dso)
346{
347 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200348 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200349}
350
351static void dso__list_del(struct dso *dso)
352{
353 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200354 WARN_ONCE(dso__data_open_cnt <= 0,
355 "DSO data fd counter out of bounds.");
356 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200357}
358
Jiri Olsaa08cae02014-05-07 21:35:02 +0200359static void close_first_dso(void);
360
361static int do_open(char *name)
362{
363 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000364 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200365
366 do {
367 fd = open(name, O_RDONLY);
368 if (fd >= 0)
369 return fd;
370
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900371 pr_debug("dso open failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300372 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200373 if (!dso__data_open_cnt || errno != EMFILE)
374 break;
375
376 close_first_dso();
377 } while (1);
378
379 return -1;
380}
381
Jiri Olsaeba51022014-04-30 15:00:59 +0200382static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200383{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200384 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300385 char *root_dir = (char *)"";
386 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200387
Jiri Olsacdd059d2012-10-27 23:18:32 +0200388 if (!name)
389 return -ENOMEM;
390
391 if (machine)
392 root_dir = machine->root_dir;
393
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300394 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300395 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200396 free(name);
397 return -EINVAL;
398 }
399
Jiri Olsa3c028a02016-09-20 18:12:45 +0200400 if (!is_regular_file(name))
401 return -EINVAL;
402
Jiri Olsaa08cae02014-05-07 21:35:02 +0200403 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200404 free(name);
405 return fd;
406}
407
Jiri Olsac6580452014-04-30 15:47:27 +0200408static void check_data_close(void);
409
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200410/**
411 * dso_close - Open DSO data file
412 * @dso: dso object
413 *
414 * Open @dso's data file descriptor and updates
415 * list/count of open DSO objects.
416 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200417static int open_dso(struct dso *dso, struct machine *machine)
418{
419 int fd = __open_dso(dso, machine);
420
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300421 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200422 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200423 /*
424 * Check if we crossed the allowed number
425 * of opened DSOs and close one if needed.
426 */
427 check_data_close();
428 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200429
430 return fd;
431}
432
433static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200434{
435 if (dso->data.fd >= 0) {
436 close(dso->data.fd);
437 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200438 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200439 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200440 }
441}
442
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200443/**
444 * dso_close - Close DSO data file
445 * @dso: dso object
446 *
447 * Close @dso's data file descriptor and updates
448 * list/count of open DSO objects.
449 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200450static void close_dso(struct dso *dso)
451{
452 close_data_fd(dso);
453}
454
Jiri Olsac6580452014-04-30 15:47:27 +0200455static void close_first_dso(void)
456{
457 struct dso *dso;
458
459 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
460 close_dso(dso);
461}
462
463static rlim_t get_fd_limit(void)
464{
465 struct rlimit l;
466 rlim_t limit = 0;
467
468 /* Allow half of the current open fd limit. */
469 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
470 if (l.rlim_cur == RLIM_INFINITY)
471 limit = l.rlim_cur;
472 else
473 limit = l.rlim_cur / 2;
474 } else {
475 pr_err("failed to get fd limit\n");
476 limit = 1;
477 }
478
479 return limit;
480}
481
Jiri Olsaf3069242016-06-28 13:29:02 +0200482static rlim_t fd_limit;
483
484/*
485 * Used only by tests/dso-data.c to reset the environment
486 * for tests. I dont expect we should change this during
487 * standard runtime.
488 */
489void reset_fd_limit(void)
490{
491 fd_limit = 0;
492}
493
Jiri Olsac6580452014-04-30 15:47:27 +0200494static bool may_cache_fd(void)
495{
Jiri Olsaf3069242016-06-28 13:29:02 +0200496 if (!fd_limit)
497 fd_limit = get_fd_limit();
Jiri Olsac6580452014-04-30 15:47:27 +0200498
Jiri Olsaf3069242016-06-28 13:29:02 +0200499 if (fd_limit == RLIM_INFINITY)
Jiri Olsac6580452014-04-30 15:47:27 +0200500 return true;
501
Jiri Olsaf3069242016-06-28 13:29:02 +0200502 return fd_limit > (rlim_t) dso__data_open_cnt;
Jiri Olsac6580452014-04-30 15:47:27 +0200503}
504
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200505/*
506 * Check and close LRU dso if we crossed allowed limit
507 * for opened dso file descriptors. The limit is half
508 * of the RLIMIT_NOFILE files opened.
509*/
Jiri Olsac6580452014-04-30 15:47:27 +0200510static void check_data_close(void)
511{
512 bool cache_fd = may_cache_fd();
513
514 if (!cache_fd)
515 close_first_dso();
516}
517
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200518/**
519 * dso__data_close - Close DSO data file
520 * @dso: dso object
521 *
522 * External interface to close @dso's data file descriptor.
523 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200524void dso__data_close(struct dso *dso)
525{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900526 pthread_mutex_lock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200527 close_dso(dso);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900528 pthread_mutex_unlock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200529}
530
Namhyung Kim71ff8242015-05-21 01:03:39 +0900531static void try_to_open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200532{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300533 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200534 DSO_BINARY_TYPE__BUILD_ID_CACHE,
535 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
536 DSO_BINARY_TYPE__NOT_FOUND,
537 };
538 int i = 0;
539
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200540 if (dso->data.fd >= 0)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900541 return;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200542
543 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
544 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300545 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200546 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200547
548 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300549 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200550
Adrian Hunterc27697d2014-07-22 16:17:18 +0300551 dso->data.fd = open_dso(dso, machine);
552 if (dso->data.fd >= 0)
553 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200554
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300555 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300556out:
557 if (dso->data.fd >= 0)
558 dso->data.status = DSO_DATA_STATUS_OK;
559 else
560 dso->data.status = DSO_DATA_STATUS_ERROR;
Namhyung Kim71ff8242015-05-21 01:03:39 +0900561}
Jiri Olsacdd059d2012-10-27 23:18:32 +0200562
Namhyung Kim71ff8242015-05-21 01:03:39 +0900563/**
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900564 * dso__data_get_fd - Get dso's data file descriptor
Namhyung Kim71ff8242015-05-21 01:03:39 +0900565 * @dso: dso object
566 * @machine: machine object
567 *
568 * External interface to find dso's file, open it and
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900569 * returns file descriptor. It should be paired with
570 * dso__data_put_fd() if it returns non-negative value.
Namhyung Kim71ff8242015-05-21 01:03:39 +0900571 */
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900572int dso__data_get_fd(struct dso *dso, struct machine *machine)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900573{
574 if (dso->data.status == DSO_DATA_STATUS_ERROR)
575 return -1;
576
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900577 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
578 return -1;
579
Namhyung Kim71ff8242015-05-21 01:03:39 +0900580 try_to_open_dso(dso, machine);
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900581
582 if (dso->data.fd < 0)
583 pthread_mutex_unlock(&dso__data_open_lock);
Namhyung Kim71ff8242015-05-21 01:03:39 +0900584
Adrian Hunterc27697d2014-07-22 16:17:18 +0300585 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200586}
587
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900588void dso__data_put_fd(struct dso *dso __maybe_unused)
589{
590 pthread_mutex_unlock(&dso__data_open_lock);
591}
592
Adrian Hunter288be942014-07-22 16:17:19 +0300593bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
594{
595 u32 flag = 1 << by;
596
597 if (dso->data.status_seen & flag)
598 return true;
599
600 dso->data.status_seen |= flag;
601
602 return false;
603}
604
Jiri Olsacdd059d2012-10-27 23:18:32 +0200605static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900606dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200607{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900608 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200609 struct rb_node *next = rb_first(root);
610
Namhyung Kim8e67b722015-05-18 09:30:41 +0900611 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200612 while (next) {
613 struct dso_cache *cache;
614
615 cache = rb_entry(next, struct dso_cache, rb_node);
616 next = rb_next(&cache->rb_node);
617 rb_erase(&cache->rb_node, root);
618 free(cache);
619 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900620 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200621}
622
Namhyung Kim8e67b722015-05-18 09:30:41 +0900623static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200624{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900625 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300626 struct rb_node * const *p = &root->rb_node;
627 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200628 struct dso_cache *cache;
629
630 while (*p != NULL) {
631 u64 end;
632
633 parent = *p;
634 cache = rb_entry(parent, struct dso_cache, rb_node);
635 end = cache->offset + DSO__DATA_CACHE_SIZE;
636
637 if (offset < cache->offset)
638 p = &(*p)->rb_left;
639 else if (offset >= end)
640 p = &(*p)->rb_right;
641 else
642 return cache;
643 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900644
Jiri Olsacdd059d2012-10-27 23:18:32 +0200645 return NULL;
646}
647
Namhyung Kim8e67b722015-05-18 09:30:41 +0900648static struct dso_cache *
649dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200650{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900651 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200652 struct rb_node **p = &root->rb_node;
653 struct rb_node *parent = NULL;
654 struct dso_cache *cache;
655 u64 offset = new->offset;
656
Namhyung Kim8e67b722015-05-18 09:30:41 +0900657 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200658 while (*p != NULL) {
659 u64 end;
660
661 parent = *p;
662 cache = rb_entry(parent, struct dso_cache, rb_node);
663 end = cache->offset + DSO__DATA_CACHE_SIZE;
664
665 if (offset < cache->offset)
666 p = &(*p)->rb_left;
667 else if (offset >= end)
668 p = &(*p)->rb_right;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900669 else
670 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200671 }
672
673 rb_link_node(&new->rb_node, parent, p);
674 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900675
676 cache = NULL;
677out:
678 pthread_mutex_unlock(&dso->lock);
679 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200680}
681
682static ssize_t
683dso_cache__memcpy(struct dso_cache *cache, u64 offset,
684 u8 *data, u64 size)
685{
686 u64 cache_offset = offset - cache->offset;
687 u64 cache_size = min(cache->size - cache_offset, size);
688
689 memcpy(data, cache->data + cache_offset, cache_size);
690 return cache_size;
691}
692
693static ssize_t
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900694dso_cache__read(struct dso *dso, struct machine *machine,
695 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200696{
697 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900698 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200699 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200700
701 do {
702 u64 cache_offset;
703
Jiri Olsacdd059d2012-10-27 23:18:32 +0200704 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
705 if (!cache)
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900706 return -ENOMEM;
707
708 pthread_mutex_lock(&dso__data_open_lock);
709
710 /*
711 * dso->data.fd might be closed if other thread opened another
712 * file (dso) due to open file limit (RLIMIT_NOFILE).
713 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900714 try_to_open_dso(dso, machine);
715
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900716 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900717 ret = -errno;
718 dso->data.status = DSO_DATA_STATUS_ERROR;
719 break;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900720 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200721
722 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200723
Namhyung Kimc52686f2015-01-29 17:02:01 -0300724 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200725 if (ret <= 0)
726 break;
727
728 cache->offset = cache_offset;
729 cache->size = ret;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900730 } while (0);
731
732 pthread_mutex_unlock(&dso__data_open_lock);
733
734 if (ret > 0) {
Namhyung Kim8e67b722015-05-18 09:30:41 +0900735 old = dso_cache__insert(dso, cache);
736 if (old) {
737 /* we lose the race */
738 free(cache);
739 cache = old;
740 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200741
742 ret = dso_cache__memcpy(cache, offset, data, size);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900743 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200744
745 if (ret <= 0)
746 free(cache);
747
Jiri Olsacdd059d2012-10-27 23:18:32 +0200748 return ret;
749}
750
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900751static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
752 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200753{
754 struct dso_cache *cache;
755
Namhyung Kim8e67b722015-05-18 09:30:41 +0900756 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200757 if (cache)
758 return dso_cache__memcpy(cache, offset, data, size);
759 else
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900760 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200761}
762
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200763/*
764 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
765 * in the rb_tree. Any read to already cached data is served
766 * by cached data.
767 */
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900768static ssize_t cached_read(struct dso *dso, struct machine *machine,
769 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200770{
771 ssize_t r = 0;
772 u8 *p = data;
773
774 do {
775 ssize_t ret;
776
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900777 ret = dso_cache_read(dso, machine, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200778 if (ret < 0)
779 return ret;
780
781 /* Reached EOF, return what we have. */
782 if (!ret)
783 break;
784
785 BUG_ON(ret > size);
786
787 r += ret;
788 p += ret;
789 offset += ret;
790 size -= ret;
791
792 } while (size);
793
794 return r;
795}
796
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900797static int data_file_size(struct dso *dso, struct machine *machine)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200798{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900799 int ret = 0;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200800 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000801 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200802
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900803 if (dso->data.file_size)
804 return 0;
805
Namhyung Kim71ff8242015-05-21 01:03:39 +0900806 if (dso->data.status == DSO_DATA_STATUS_ERROR)
807 return -1;
808
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900809 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 goto out;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200821 }
822
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900823 if (fstat(dso->data.fd, &st) < 0) {
824 ret = -errno;
825 pr_err("dso cache fstat failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300826 str_error_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900827 dso->data.status = DSO_DATA_STATUS_ERROR;
828 goto out;
829 }
830 dso->data.file_size = st.st_size;
831
832out:
833 pthread_mutex_unlock(&dso__data_open_lock);
834 return ret;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200835}
836
Adrian Hunter6d363452014-07-22 16:17:35 +0300837/**
838 * dso__data_size - Return dso data size
839 * @dso: dso object
840 * @machine: machine object
841 *
842 * Return: dso data size
843 */
844off_t dso__data_size(struct dso *dso, struct machine *machine)
845{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900846 if (data_file_size(dso, machine))
Adrian Hunter6d363452014-07-22 16:17:35 +0300847 return -1;
848
849 /* For now just estimate dso data size is close to file size */
850 return dso->data.file_size;
851}
852
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900853static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
854 u64 offset, u8 *data, ssize_t size)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200855{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900856 if (data_file_size(dso, machine))
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200857 return -1;
858
859 /* Check the offset sanity. */
860 if (offset > dso->data.file_size)
861 return -1;
862
863 if (offset + size < offset)
864 return -1;
865
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900866 return cached_read(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200867}
868
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200869/**
870 * dso__data_read_offset - Read data from dso file offset
871 * @dso: dso object
872 * @machine: machine object
873 * @offset: file offset
874 * @data: buffer to store data
875 * @size: size of the @data buffer
876 *
877 * External interface to read data from dso file offset. Open
878 * dso data file and use cached_read to get the data.
879 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200880ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
881 u64 offset, u8 *data, ssize_t size)
882{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900883 if (dso->data.status == DSO_DATA_STATUS_ERROR)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200884 return -1;
885
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900886 return data_read_offset(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200887}
888
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200889/**
890 * dso__data_read_addr - Read data from dso address
891 * @dso: dso object
892 * @machine: machine object
893 * @add: virtual memory address
894 * @data: buffer to store data
895 * @size: size of the @data buffer
896 *
897 * External interface to read data from dso address.
898 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200899ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
900 struct machine *machine, u64 addr,
901 u8 *data, ssize_t size)
902{
903 u64 offset = map->map_ip(map, addr);
904 return dso__data_read_offset(dso, machine, offset, data, size);
905}
906
907struct map *dso__new_map(const char *name)
908{
909 struct map *map = NULL;
910 struct dso *dso = dso__new(name);
911
912 if (dso)
913 map = map__new2(0, dso, MAP__FUNCTION);
914
915 return map;
916}
917
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300918struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
919 const char *short_name, int dso_type)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200920{
921 /*
922 * The kernel dso could be created by build_id processing.
923 */
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300924 struct dso *dso = machine__findnew_dso(machine, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200925
926 /*
927 * We need to run this in all cases, since during the build_id
928 * processing we had no idea this was the kernel dso.
929 */
930 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300931 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200932 dso->kernel = dso_type;
933 }
934
935 return dso;
936}
937
Waiman Long4598a0a2014-09-30 13:36:15 -0400938/*
939 * Find a matching entry and/or link current entry to RB tree.
940 * Either one of the dso or name parameter must be non-NULL or the
941 * function will not work.
942 */
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300943static struct dso *__dso__findlink_by_longname(struct rb_root *root,
944 struct dso *dso, const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400945{
946 struct rb_node **p = &root->rb_node;
947 struct rb_node *parent = NULL;
948
949 if (!name)
950 name = dso->long_name;
951 /*
952 * Find node with the matching name
953 */
954 while (*p) {
955 struct dso *this = rb_entry(*p, struct dso, rb_node);
956 int rc = strcmp(name, this->long_name);
957
958 parent = *p;
959 if (rc == 0) {
960 /*
961 * In case the new DSO is a duplicate of an existing
Masahiro Yamada0f5e1552017-02-27 14:28:52 -0800962 * one, print a one-time warning & put the new entry
Waiman Long4598a0a2014-09-30 13:36:15 -0400963 * at the end of the list of duplicates.
964 */
965 if (!dso || (dso == this))
966 return this; /* Find matching dso */
967 /*
968 * The core kernel DSOs may have duplicated long name.
969 * In this case, the short name should be different.
970 * Comparing the short names to differentiate the DSOs.
971 */
972 rc = strcmp(dso->short_name, this->short_name);
973 if (rc == 0) {
974 pr_err("Duplicated dso name: %s\n", name);
975 return NULL;
976 }
977 }
978 if (rc < 0)
979 p = &parent->rb_left;
980 else
981 p = &parent->rb_right;
982 }
983 if (dso) {
984 /* Add new node and rebalance tree */
985 rb_link_node(&dso->rb_node, parent, p);
986 rb_insert_color(&dso->rb_node, root);
Adrian Huntere266a752015-11-13 11:48:30 +0200987 dso->root = root;
Waiman Long4598a0a2014-09-30 13:36:15 -0400988 }
989 return NULL;
990}
991
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300992static inline struct dso *__dso__find_by_longname(struct rb_root *root,
993 const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400994{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300995 return __dso__findlink_by_longname(root, NULL, name);
Waiman Long4598a0a2014-09-30 13:36:15 -0400996}
997
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300998void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200999{
Adrian Huntere266a752015-11-13 11:48:30 +02001000 struct rb_root *root = dso->root;
1001
Jiri Olsacdd059d2012-10-27 23:18:32 +02001002 if (name == NULL)
1003 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001004
1005 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001006 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001007
Adrian Huntere266a752015-11-13 11:48:30 +02001008 if (root) {
1009 rb_erase(&dso->rb_node, root);
1010 /*
1011 * __dso__findlink_by_longname() isn't guaranteed to add it
1012 * back, so a clean removal is required here.
1013 */
1014 RB_CLEAR_NODE(&dso->rb_node);
1015 dso->root = NULL;
1016 }
1017
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001018 dso->long_name = name;
1019 dso->long_name_len = strlen(name);
1020 dso->long_name_allocated = name_allocated;
Adrian Huntere266a752015-11-13 11:48:30 +02001021
1022 if (root)
1023 __dso__findlink_by_longname(root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001024}
1025
Adrian Hunter58a98c92013-12-10 11:11:46 -03001026void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001027{
1028 if (name == NULL)
1029 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -03001030
1031 if (dso->short_name_allocated)
1032 free((char *)dso->short_name);
1033
1034 dso->short_name = name;
1035 dso->short_name_len = strlen(name);
1036 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001037}
1038
1039static void dso__set_basename(struct dso *dso)
1040{
Stephane Eranianac5e7f82013-12-05 19:26:42 +01001041 /*
1042 * basename() may modify path buffer, so we must pass
1043 * a copy.
1044 */
1045 char *base, *lname = strdup(dso->long_name);
1046
1047 if (!lname)
1048 return;
1049
1050 /*
1051 * basename() may return a pointer to internal
1052 * storage which is reused in subsequent calls
1053 * so copy the result.
1054 */
1055 base = strdup(basename(lname));
1056
1057 free(lname);
1058
1059 if (!base)
1060 return;
1061
1062 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001063}
1064
1065int dso__name_len(const struct dso *dso)
1066{
1067 if (!dso)
1068 return strlen("[unknown]");
Namhyung Kimbb963e12017-02-17 17:17:38 +09001069 if (verbose > 0)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001070 return dso->long_name_len;
1071
1072 return dso->short_name_len;
1073}
1074
1075bool dso__loaded(const struct dso *dso, enum map_type type)
1076{
1077 return dso->loaded & (1 << type);
1078}
1079
1080bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1081{
1082 return dso->sorted_by_name & (1 << type);
1083}
1084
1085void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1086{
1087 dso->sorted_by_name |= (1 << type);
1088}
1089
1090struct dso *dso__new(const char *name)
1091{
1092 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1093
1094 if (dso != NULL) {
1095 int i;
1096 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001097 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -03001098 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001099 for (i = 0; i < MAP__NR_TYPES; ++i)
1100 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +02001101 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001102 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +03001103 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001104 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001105 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001106 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001107 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001108 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001109 dso->sorted_by_name = 0;
1110 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +09001111 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +02001112 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001113 dso->kernel = DSO_TYPE_USER;
1114 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -04001115 RB_CLEAR_NODE(&dso->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +02001116 dso->root = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001117 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +02001118 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001119 pthread_mutex_init(&dso->lock, NULL);
Elena Reshetova71008102017-02-21 17:34:58 +02001120 refcount_set(&dso->refcnt, 1);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001121 }
1122
1123 return dso;
1124}
1125
1126void dso__delete(struct dso *dso)
1127{
1128 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -04001129
1130 if (!RB_EMPTY_NODE(&dso->rb_node))
1131 pr_err("DSO %s is still in rbtree when being deleted!\n",
1132 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001133 for (i = 0; i < MAP__NR_TYPES; ++i)
1134 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001135
1136 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001137 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001138 dso->short_name_allocated = false;
1139 }
1140
1141 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001142 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001143 dso->long_name_allocated = false;
1144 }
1145
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001146 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +03001147 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +09001148 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +02001149 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001150 zfree(&dso->symsrc_filename);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001151 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001152 free(dso);
1153}
1154
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001155struct dso *dso__get(struct dso *dso)
1156{
1157 if (dso)
Elena Reshetova71008102017-02-21 17:34:58 +02001158 refcount_inc(&dso->refcnt);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001159 return dso;
1160}
1161
1162void dso__put(struct dso *dso)
1163{
Elena Reshetova71008102017-02-21 17:34:58 +02001164 if (dso && refcount_dec_and_test(&dso->refcnt))
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001165 dso__delete(dso);
1166}
1167
Jiri Olsacdd059d2012-10-27 23:18:32 +02001168void dso__set_build_id(struct dso *dso, void *build_id)
1169{
1170 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1171 dso->has_build_id = 1;
1172}
1173
1174bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1175{
1176 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1177}
1178
1179void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1180{
1181 char path[PATH_MAX];
1182
1183 if (machine__is_default_guest(machine))
1184 return;
1185 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1186 if (sysfs__read_build_id(path, dso->build_id,
1187 sizeof(dso->build_id)) == 0)
1188 dso->has_build_id = true;
1189}
1190
1191int dso__kernel_module_get_build_id(struct dso *dso,
1192 const char *root_dir)
1193{
1194 char filename[PATH_MAX];
1195 /*
1196 * kernel module short names are of the form "[module]" and
1197 * we need just "module" here.
1198 */
1199 const char *name = dso->short_name + 1;
1200
1201 snprintf(filename, sizeof(filename),
1202 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1203 root_dir, (int)strlen(name) - 1, name);
1204
1205 if (sysfs__read_build_id(filename, dso->build_id,
1206 sizeof(dso->build_id)) == 0)
1207 dso->has_build_id = true;
1208
1209 return 0;
1210}
1211
1212bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1213{
1214 bool have_build_id = false;
1215 struct dso *pos;
1216
1217 list_for_each_entry(pos, head, node) {
He Kuang6ae98ba2016-05-12 08:43:11 +00001218 if (with_hits && !pos->hit && !dso__is_vdso(pos))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001219 continue;
1220 if (pos->has_build_id) {
1221 have_build_id = true;
1222 continue;
1223 }
1224 if (filename__read_build_id(pos->long_name, pos->build_id,
1225 sizeof(pos->build_id)) > 0) {
1226 have_build_id = true;
1227 pos->has_build_id = true;
1228 }
1229 }
1230
1231 return have_build_id;
1232}
1233
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001234void __dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001235{
Waiman Long8fa7d872014-09-29 16:07:28 -04001236 list_add_tail(&dso->node, &dsos->head);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001237 __dso__findlink_by_longname(&dsos->root, dso, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001238 /*
1239 * It is now in the linked list, grab a reference, then garbage collect
1240 * this when needing memory, by looking at LRU dso instances in the
1241 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1242 * anywhere besides the one for the list, do, under a lock for the
1243 * list: remove it from the list, then a dso__put(), that probably will
1244 * be the last and will then call dso__delete(), end of life.
1245 *
1246 * That, or at the end of the 'struct machine' lifetime, when all
1247 * 'struct dso' instances will be removed from the list, in
1248 * dsos__exit(), if they have no other reference from some other data
1249 * structure.
1250 *
1251 * E.g.: after processing a 'perf.data' file and storing references
1252 * to objects instantiated while processing events, we will have
1253 * references to the 'thread', 'map', 'dso' structs all from 'struct
1254 * hist_entry' instances, but we may not need anything not referenced,
1255 * so we might as well call machines__exit()/machines__delete() and
1256 * garbage collect it.
1257 */
1258 dso__get(dso);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001259}
1260
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001261void dsos__add(struct dsos *dsos, struct dso *dso)
1262{
1263 pthread_rwlock_wrlock(&dsos->lock);
1264 __dsos__add(dsos, dso);
1265 pthread_rwlock_unlock(&dsos->lock);
1266}
1267
1268struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001269{
1270 struct dso *pos;
1271
Waiman Longf9ceffb2013-05-09 10:42:48 -04001272 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001273 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001274 if (strcmp(pos->short_name, name) == 0)
1275 return pos;
1276 return NULL;
1277 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001278 return __dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001279}
1280
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001281struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1282{
1283 struct dso *dso;
1284 pthread_rwlock_rdlock(&dsos->lock);
1285 dso = __dsos__find(dsos, name, cmp_short);
1286 pthread_rwlock_unlock(&dsos->lock);
1287 return dso;
1288}
1289
1290struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
Jiri Olsa701d8d72015-02-12 22:06:09 +01001291{
1292 struct dso *dso = dso__new(name);
1293
1294 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001295 __dsos__add(dsos, dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001296 dso__set_basename(dso);
Masami Hiramatsu82de26a2015-11-18 15:40:31 +09001297 /* Put dso here because __dsos_add already got it */
1298 dso__put(dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001299 }
1300 return dso;
1301}
1302
Waiman Long8fa7d872014-09-29 16:07:28 -04001303struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001304{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001305 struct dso *dso = __dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001306
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001307 return dso ? dso : __dsos__addnew(dsos, name);
1308}
1309
1310struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1311{
1312 struct dso *dso;
1313 pthread_rwlock_wrlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001314 dso = dso__get(__dsos__findnew(dsos, name));
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001315 pthread_rwlock_unlock(&dsos->lock);
1316 return dso;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001317}
1318
1319size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001320 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001321{
1322 struct dso *pos;
1323 size_t ret = 0;
1324
1325 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001326 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001327 continue;
1328 ret += dso__fprintf_buildid(pos, fp);
1329 ret += fprintf(fp, " %s\n", pos->long_name);
1330 }
1331 return ret;
1332}
1333
1334size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1335{
1336 struct dso *pos;
1337 size_t ret = 0;
1338
1339 list_for_each_entry(pos, head, node) {
1340 int i;
1341 for (i = 0; i < MAP__NR_TYPES; ++i)
1342 ret += dso__fprintf(pos, i, fp);
1343 }
1344
1345 return ret;
1346}
1347
1348size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1349{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001350 char sbuild_id[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +02001351
1352 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1353 return fprintf(fp, "%s", sbuild_id);
1354}
1355
1356size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1357{
1358 struct rb_node *nd;
1359 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1360
1361 if (dso->short_name != dso->long_name)
1362 ret += fprintf(fp, "%s, ", dso->long_name);
1363 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001364 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001365 ret += dso__fprintf_buildid(dso, fp);
1366 ret += fprintf(fp, ")\n");
1367 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1368 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1369 ret += symbol__fprintf(pos, fp);
1370 }
1371
1372 return ret;
1373}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001374
1375enum dso_type dso__type(struct dso *dso, struct machine *machine)
1376{
1377 int fd;
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001378 enum dso_type type = DSO__TYPE_UNKNOWN;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001379
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001380 fd = dso__data_get_fd(dso, machine);
1381 if (fd >= 0) {
1382 type = dso__type_fd(fd);
1383 dso__data_put_fd(dso);
1384 }
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001385
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001386 return type;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001387}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001388
1389int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1390{
1391 int idx, errnum = dso->load_errno;
1392 /*
1393 * This must have a same ordering as the enum dso_load_errno.
1394 */
1395 static const char *dso_load__error_str[] = {
1396 "Internal tools/perf/ library error",
1397 "Invalid ELF file",
1398 "Can not read build id",
1399 "Mismatching build id",
1400 "Decompression failure",
1401 };
1402
1403 BUG_ON(buflen == 0);
1404
1405 if (errnum >= 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001406 const char *err = str_error_r(errnum, buf, buflen);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001407
1408 if (err != buf)
1409 scnprintf(buf, buflen, "%s", err);
1410
1411 return 0;
1412 }
1413
1414 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1415 return -1;
1416
1417 idx = errnum - __DSO_LOAD_ERRNO__START;
1418 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1419 return 0;
1420}