blob: 4bc58822416cf9c204f435bab52a0df6889a76b1 [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
12char dso__symtab_origin(const struct dso *dso)
13{
14 static const char origin[] = {
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020015 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
16 [DSO_BINARY_TYPE__VMLINUX] = 'v',
17 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
18 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
19 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
20 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
21 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
22 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
23 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
24 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
25 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090026 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020027 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
28 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090029 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020030 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Jiri Olsacdd059d2012-10-27 23:18:32 +020031 };
32
33 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
34 return '!';
35 return origin[dso->symtab_type];
36}
37
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -030038int dso__read_binary_type_filename(const struct dso *dso,
39 enum dso_binary_type type,
40 char *root_dir, char *filename, size_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +020041{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +090042 char build_id_hex[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +020043 int ret = 0;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030044 size_t len;
Jiri Olsacdd059d2012-10-27 23:18:32 +020045
46 switch (type) {
47 case DSO_BINARY_TYPE__DEBUGLINK: {
48 char *debuglink;
49
Victor Kamenskydc6254c2015-01-26 22:34:02 -080050 len = __symbol__join_symfs(filename, size, dso->long_name);
51 debuglink = filename + len;
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030052 while (debuglink != filename && *debuglink != '/')
Jiri Olsacdd059d2012-10-27 23:18:32 +020053 debuglink--;
54 if (*debuglink == '/')
55 debuglink++;
Jiri Olsa40356722016-01-20 12:56:32 +010056
57 ret = -1;
58 if (!is_regular_file(filename))
59 break;
60
Victor Kamenskydc6254c2015-01-26 22:34:02 -080061 ret = filename__read_debuglink(filename, debuglink,
Stephane Eranian0d3dc5e2014-02-20 10:32:55 +090062 size - (debuglink - filename));
Jiri Olsacdd059d2012-10-27 23:18:32 +020063 }
64 break;
65 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
He Kuanga7066702016-05-19 11:47:37 +000066 if (dso__build_id_filename(dso, filename, size) == NULL)
Jiri Olsacdd059d2012-10-27 23:18:32 +020067 ret = -1;
68 break;
69
70 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030071 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
72 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +020073 break;
74
75 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030076 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
77 snprintf(filename + len, size - len, "%s", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +020078 break;
79
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020080 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
81 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -030082 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020083 size_t dir_size;
84
85 last_slash = dso->long_name + dso->long_name_len;
86 while (last_slash != dso->long_name && *last_slash != '/')
87 last_slash--;
88
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030089 len = __symbol__join_symfs(filename, size, "");
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020090 dir_size = last_slash - dso->long_name + 2;
91 if (dir_size > (size - len)) {
92 ret = -1;
93 break;
94 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030095 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
96 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020097 last_slash);
98 break;
99 }
100
Jiri Olsacdd059d2012-10-27 23:18:32 +0200101 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
102 if (!dso->has_build_id) {
103 ret = -1;
104 break;
105 }
106
107 build_id__sprintf(dso->build_id,
108 sizeof(dso->build_id),
109 build_id_hex);
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300110 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
111 snprintf(filename + len, size - len, "%.2s/%s.debug",
112 build_id_hex, build_id_hex + 2);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200113 break;
114
Adrian Hunter39b12f72013-08-07 14:38:47 +0300115 case DSO_BINARY_TYPE__VMLINUX:
116 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200117 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300118 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200119 break;
120
121 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900122 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300123 path__join3(filename, size, symbol_conf.symfs,
124 root_dir, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200125 break;
126
127 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900128 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300129 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200130 break;
131
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300132 case DSO_BINARY_TYPE__KCORE:
133 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300134 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300135 break;
136
Jiri Olsacdd059d2012-10-27 23:18:32 +0200137 default:
138 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200139 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200140 case DSO_BINARY_TYPE__JAVA_JIT:
141 case DSO_BINARY_TYPE__NOT_FOUND:
142 ret = -1;
143 break;
144 }
145
146 return ret;
147}
148
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900149static const struct {
150 const char *fmt;
151 int (*decompress)(const char *input, int output);
152} compressions[] = {
Namhyung Kime92ce122014-10-31 16:51:38 +0900153#ifdef HAVE_ZLIB_SUPPORT
154 { "gz", gzip_decompress_to_file },
155#endif
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100156#ifdef HAVE_LZMA_SUPPORT
157 { "xz", lzma_decompress_to_file },
158#endif
Namhyung Kime92ce122014-10-31 16:51:38 +0900159 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900160};
161
162bool is_supported_compression(const char *ext)
163{
164 unsigned i;
165
166 for (i = 0; compressions[i].fmt; i++) {
167 if (!strcmp(ext, compressions[i].fmt))
168 return true;
169 }
170 return false;
171}
172
Wang Nan1f121b02015-06-03 08:52:21 +0000173bool is_kernel_module(const char *pathname, int cpumode)
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900174{
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100175 struct kmod_path m;
Wang Nan1f121b02015-06-03 08:52:21 +0000176 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900177
Wang Nan1f121b02015-06-03 08:52:21 +0000178 WARN_ONCE(mode != cpumode,
179 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
180 cpumode);
181
182 switch (mode) {
183 case PERF_RECORD_MISC_USER:
184 case PERF_RECORD_MISC_HYPERVISOR:
185 case PERF_RECORD_MISC_GUEST_USER:
186 return false;
187 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
188 default:
189 if (kmod_path__parse(&m, pathname)) {
190 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
191 pathname);
192 return true;
193 }
194 }
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900195
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100196 return m.kmod;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900197}
198
199bool decompress_to_file(const char *ext, const char *filename, int output_fd)
200{
201 unsigned i;
202
203 for (i = 0; compressions[i].fmt; i++) {
204 if (!strcmp(ext, compressions[i].fmt))
205 return !compressions[i].decompress(filename,
206 output_fd);
207 }
208 return false;
209}
210
211bool dso__needs_decompress(struct dso *dso)
212{
213 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
214 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
215}
216
Jiri Olsaeba51022014-04-30 15:00:59 +0200217/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100218 * Parses kernel module specified in @path and updates
219 * @m argument like:
220 *
221 * @comp - true if @path contains supported compression suffix,
222 * false otherwise
223 * @kmod - true if @path contains '.ko' suffix in right position,
224 * false otherwise
225 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
226 * of the kernel module without suffixes, otherwise strudup-ed
227 * base name of @path
228 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
229 * the compression suffix
230 *
231 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
232 */
233int __kmod_path__parse(struct kmod_path *m, const char *path,
234 bool alloc_name, bool alloc_ext)
235{
236 const char *name = strrchr(path, '/');
237 const char *ext = strrchr(path, '.');
Wang Nan1f121b02015-06-03 08:52:21 +0000238 bool is_simple_name = false;
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100239
240 memset(m, 0x0, sizeof(*m));
241 name = name ? name + 1 : path;
242
Wang Nan1f121b02015-06-03 08:52:21 +0000243 /*
244 * '.' is also a valid character for module name. For example:
245 * [aaa.bbb] is a valid module name. '[' should have higher
246 * priority than '.ko' suffix.
247 *
248 * The kernel names are from machine__mmap_name. Such
249 * name should belong to kernel itself, not kernel module.
250 */
251 if (name[0] == '[') {
252 is_simple_name = true;
253 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
254 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
255 (strncmp(name, "[vdso]", 6) == 0) ||
256 (strncmp(name, "[vsyscall]", 10) == 0)) {
257 m->kmod = false;
258
259 } else
260 m->kmod = true;
261 }
262
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100263 /* No extension, just return name. */
Wang Nan1f121b02015-06-03 08:52:21 +0000264 if ((ext == NULL) || is_simple_name) {
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100265 if (alloc_name) {
266 m->name = strdup(name);
267 return m->name ? 0 : -ENOMEM;
268 }
269 return 0;
270 }
271
272 if (is_supported_compression(ext + 1)) {
273 m->comp = true;
274 ext -= 3;
275 }
276
277 /* Check .ko extension only if there's enough name left. */
278 if (ext > name)
279 m->kmod = !strncmp(ext, ".ko", 3);
280
281 if (alloc_name) {
282 if (m->kmod) {
283 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
284 return -ENOMEM;
285 } else {
286 if (asprintf(&m->name, "%s", name) == -1)
287 return -ENOMEM;
288 }
289
290 strxfrchar(m->name, '-', '_');
291 }
292
293 if (alloc_ext && m->comp) {
294 m->ext = strdup(ext + 4);
295 if (!m->ext) {
296 free((void *) m->name);
297 return -ENOMEM;
298 }
299 }
300
301 return 0;
302}
303
304/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200305 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200306 */
307static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200308static long dso__data_open_cnt;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900309static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
Jiri Olsaeba51022014-04-30 15:00:59 +0200310
311static void dso__list_add(struct dso *dso)
312{
313 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200314 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200315}
316
317static void dso__list_del(struct dso *dso)
318{
319 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200320 WARN_ONCE(dso__data_open_cnt <= 0,
321 "DSO data fd counter out of bounds.");
322 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200323}
324
Jiri Olsaa08cae02014-05-07 21:35:02 +0200325static void close_first_dso(void);
326
327static int do_open(char *name)
328{
329 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000330 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200331
332 do {
333 fd = open(name, O_RDONLY);
334 if (fd >= 0)
335 return fd;
336
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900337 pr_debug("dso open failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300338 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200339 if (!dso__data_open_cnt || errno != EMFILE)
340 break;
341
342 close_first_dso();
343 } while (1);
344
345 return -1;
346}
347
Jiri Olsaeba51022014-04-30 15:00:59 +0200348static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200349{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200350 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300351 char *root_dir = (char *)"";
352 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200353
Jiri Olsacdd059d2012-10-27 23:18:32 +0200354 if (!name)
355 return -ENOMEM;
356
357 if (machine)
358 root_dir = machine->root_dir;
359
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300360 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300361 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200362 free(name);
363 return -EINVAL;
364 }
365
Jiri Olsa3c028a02016-09-20 18:12:45 +0200366 if (!is_regular_file(name))
367 return -EINVAL;
368
Namhyung Kime2d05492017-06-08 16:31:05 +0900369 if (dso__needs_decompress(dso)) {
370 char newpath[KMOD_DECOMP_LEN];
371 size_t len = sizeof(newpath);
372
373 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
374 free(name);
375 return -dso->load_errno;
376 }
377
378 strcpy(name, newpath);
379 }
380
Jiri Olsaa08cae02014-05-07 21:35:02 +0200381 fd = do_open(name);
Namhyung Kime2d05492017-06-08 16:31:05 +0900382
383 if (dso__needs_decompress(dso))
384 unlink(name);
385
Jiri Olsacdd059d2012-10-27 23:18:32 +0200386 free(name);
387 return fd;
388}
389
Jiri Olsac6580452014-04-30 15:47:27 +0200390static void check_data_close(void);
391
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200392/**
393 * dso_close - Open DSO data file
394 * @dso: dso object
395 *
396 * Open @dso's data file descriptor and updates
397 * list/count of open DSO objects.
398 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200399static int open_dso(struct dso *dso, struct machine *machine)
400{
401 int fd = __open_dso(dso, machine);
402
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300403 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200404 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200405 /*
406 * Check if we crossed the allowed number
407 * of opened DSOs and close one if needed.
408 */
409 check_data_close();
410 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200411
412 return fd;
413}
414
415static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200416{
417 if (dso->data.fd >= 0) {
418 close(dso->data.fd);
419 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200420 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200421 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200422 }
423}
424
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200425/**
426 * dso_close - Close DSO data file
427 * @dso: dso object
428 *
429 * Close @dso's data file descriptor and updates
430 * list/count of open DSO objects.
431 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200432static void close_dso(struct dso *dso)
433{
434 close_data_fd(dso);
435}
436
Jiri Olsac6580452014-04-30 15:47:27 +0200437static void close_first_dso(void)
438{
439 struct dso *dso;
440
441 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
442 close_dso(dso);
443}
444
445static rlim_t get_fd_limit(void)
446{
447 struct rlimit l;
448 rlim_t limit = 0;
449
450 /* Allow half of the current open fd limit. */
451 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
452 if (l.rlim_cur == RLIM_INFINITY)
453 limit = l.rlim_cur;
454 else
455 limit = l.rlim_cur / 2;
456 } else {
457 pr_err("failed to get fd limit\n");
458 limit = 1;
459 }
460
461 return limit;
462}
463
Jiri Olsaf3069242016-06-28 13:29:02 +0200464static rlim_t fd_limit;
465
466/*
467 * Used only by tests/dso-data.c to reset the environment
468 * for tests. I dont expect we should change this during
469 * standard runtime.
470 */
471void reset_fd_limit(void)
472{
473 fd_limit = 0;
474}
475
Jiri Olsac6580452014-04-30 15:47:27 +0200476static bool may_cache_fd(void)
477{
Jiri Olsaf3069242016-06-28 13:29:02 +0200478 if (!fd_limit)
479 fd_limit = get_fd_limit();
Jiri Olsac6580452014-04-30 15:47:27 +0200480
Jiri Olsaf3069242016-06-28 13:29:02 +0200481 if (fd_limit == RLIM_INFINITY)
Jiri Olsac6580452014-04-30 15:47:27 +0200482 return true;
483
Jiri Olsaf3069242016-06-28 13:29:02 +0200484 return fd_limit > (rlim_t) dso__data_open_cnt;
Jiri Olsac6580452014-04-30 15:47:27 +0200485}
486
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200487/*
488 * Check and close LRU dso if we crossed allowed limit
489 * for opened dso file descriptors. The limit is half
490 * of the RLIMIT_NOFILE files opened.
491*/
Jiri Olsac6580452014-04-30 15:47:27 +0200492static void check_data_close(void)
493{
494 bool cache_fd = may_cache_fd();
495
496 if (!cache_fd)
497 close_first_dso();
498}
499
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200500/**
501 * dso__data_close - Close DSO data file
502 * @dso: dso object
503 *
504 * External interface to close @dso's data file descriptor.
505 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200506void dso__data_close(struct dso *dso)
507{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900508 pthread_mutex_lock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200509 close_dso(dso);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900510 pthread_mutex_unlock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200511}
512
Namhyung Kim71ff8242015-05-21 01:03:39 +0900513static void try_to_open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200514{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300515 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200516 DSO_BINARY_TYPE__BUILD_ID_CACHE,
517 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
518 DSO_BINARY_TYPE__NOT_FOUND,
519 };
520 int i = 0;
521
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200522 if (dso->data.fd >= 0)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900523 return;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200524
525 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
526 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300527 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200528 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200529
530 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300531 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200532
Adrian Hunterc27697d2014-07-22 16:17:18 +0300533 dso->data.fd = open_dso(dso, machine);
534 if (dso->data.fd >= 0)
535 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200536
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300537 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300538out:
539 if (dso->data.fd >= 0)
540 dso->data.status = DSO_DATA_STATUS_OK;
541 else
542 dso->data.status = DSO_DATA_STATUS_ERROR;
Namhyung Kim71ff8242015-05-21 01:03:39 +0900543}
Jiri Olsacdd059d2012-10-27 23:18:32 +0200544
Namhyung Kim71ff8242015-05-21 01:03:39 +0900545/**
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900546 * dso__data_get_fd - Get dso's data file descriptor
Namhyung Kim71ff8242015-05-21 01:03:39 +0900547 * @dso: dso object
548 * @machine: machine object
549 *
550 * External interface to find dso's file, open it and
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900551 * returns file descriptor. It should be paired with
552 * dso__data_put_fd() if it returns non-negative value.
Namhyung Kim71ff8242015-05-21 01:03:39 +0900553 */
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900554int dso__data_get_fd(struct dso *dso, struct machine *machine)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900555{
556 if (dso->data.status == DSO_DATA_STATUS_ERROR)
557 return -1;
558
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900559 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
560 return -1;
561
Namhyung Kim71ff8242015-05-21 01:03:39 +0900562 try_to_open_dso(dso, machine);
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900563
564 if (dso->data.fd < 0)
565 pthread_mutex_unlock(&dso__data_open_lock);
Namhyung Kim71ff8242015-05-21 01:03:39 +0900566
Adrian Hunterc27697d2014-07-22 16:17:18 +0300567 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200568}
569
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900570void dso__data_put_fd(struct dso *dso __maybe_unused)
571{
572 pthread_mutex_unlock(&dso__data_open_lock);
573}
574
Adrian Hunter288be942014-07-22 16:17:19 +0300575bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
576{
577 u32 flag = 1 << by;
578
579 if (dso->data.status_seen & flag)
580 return true;
581
582 dso->data.status_seen |= flag;
583
584 return false;
585}
586
Jiri Olsacdd059d2012-10-27 23:18:32 +0200587static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900588dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200589{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900590 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200591 struct rb_node *next = rb_first(root);
592
Namhyung Kim8e67b722015-05-18 09:30:41 +0900593 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200594 while (next) {
595 struct dso_cache *cache;
596
597 cache = rb_entry(next, struct dso_cache, rb_node);
598 next = rb_next(&cache->rb_node);
599 rb_erase(&cache->rb_node, root);
600 free(cache);
601 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900602 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200603}
604
Namhyung Kim8e67b722015-05-18 09:30:41 +0900605static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200606{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900607 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300608 struct rb_node * const *p = &root->rb_node;
609 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200610 struct dso_cache *cache;
611
612 while (*p != NULL) {
613 u64 end;
614
615 parent = *p;
616 cache = rb_entry(parent, struct dso_cache, rb_node);
617 end = cache->offset + DSO__DATA_CACHE_SIZE;
618
619 if (offset < cache->offset)
620 p = &(*p)->rb_left;
621 else if (offset >= end)
622 p = &(*p)->rb_right;
623 else
624 return cache;
625 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900626
Jiri Olsacdd059d2012-10-27 23:18:32 +0200627 return NULL;
628}
629
Namhyung Kim8e67b722015-05-18 09:30:41 +0900630static struct dso_cache *
631dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200632{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900633 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200634 struct rb_node **p = &root->rb_node;
635 struct rb_node *parent = NULL;
636 struct dso_cache *cache;
637 u64 offset = new->offset;
638
Namhyung Kim8e67b722015-05-18 09:30:41 +0900639 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200640 while (*p != NULL) {
641 u64 end;
642
643 parent = *p;
644 cache = rb_entry(parent, struct dso_cache, rb_node);
645 end = cache->offset + DSO__DATA_CACHE_SIZE;
646
647 if (offset < cache->offset)
648 p = &(*p)->rb_left;
649 else if (offset >= end)
650 p = &(*p)->rb_right;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900651 else
652 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200653 }
654
655 rb_link_node(&new->rb_node, parent, p);
656 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900657
658 cache = NULL;
659out:
660 pthread_mutex_unlock(&dso->lock);
661 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200662}
663
664static ssize_t
665dso_cache__memcpy(struct dso_cache *cache, u64 offset,
666 u8 *data, u64 size)
667{
668 u64 cache_offset = offset - cache->offset;
669 u64 cache_size = min(cache->size - cache_offset, size);
670
671 memcpy(data, cache->data + cache_offset, cache_size);
672 return cache_size;
673}
674
675static ssize_t
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900676dso_cache__read(struct dso *dso, struct machine *machine,
677 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200678{
679 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900680 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200681 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200682
683 do {
684 u64 cache_offset;
685
Jiri Olsacdd059d2012-10-27 23:18:32 +0200686 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
687 if (!cache)
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900688 return -ENOMEM;
689
690 pthread_mutex_lock(&dso__data_open_lock);
691
692 /*
693 * dso->data.fd might be closed if other thread opened another
694 * file (dso) due to open file limit (RLIMIT_NOFILE).
695 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900696 try_to_open_dso(dso, machine);
697
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900698 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900699 ret = -errno;
700 dso->data.status = DSO_DATA_STATUS_ERROR;
701 break;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900702 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200703
704 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200705
Namhyung Kimc52686f2015-01-29 17:02:01 -0300706 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200707 if (ret <= 0)
708 break;
709
710 cache->offset = cache_offset;
711 cache->size = ret;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900712 } while (0);
713
714 pthread_mutex_unlock(&dso__data_open_lock);
715
716 if (ret > 0) {
Namhyung Kim8e67b722015-05-18 09:30:41 +0900717 old = dso_cache__insert(dso, cache);
718 if (old) {
719 /* we lose the race */
720 free(cache);
721 cache = old;
722 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200723
724 ret = dso_cache__memcpy(cache, offset, data, size);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900725 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200726
727 if (ret <= 0)
728 free(cache);
729
Jiri Olsacdd059d2012-10-27 23:18:32 +0200730 return ret;
731}
732
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900733static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
734 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200735{
736 struct dso_cache *cache;
737
Namhyung Kim8e67b722015-05-18 09:30:41 +0900738 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200739 if (cache)
740 return dso_cache__memcpy(cache, offset, data, size);
741 else
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900742 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200743}
744
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200745/*
746 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
747 * in the rb_tree. Any read to already cached data is served
748 * by cached data.
749 */
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900750static ssize_t cached_read(struct dso *dso, struct machine *machine,
751 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200752{
753 ssize_t r = 0;
754 u8 *p = data;
755
756 do {
757 ssize_t ret;
758
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900759 ret = dso_cache_read(dso, machine, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200760 if (ret < 0)
761 return ret;
762
763 /* Reached EOF, return what we have. */
764 if (!ret)
765 break;
766
767 BUG_ON(ret > size);
768
769 r += ret;
770 p += ret;
771 offset += ret;
772 size -= ret;
773
774 } while (size);
775
776 return r;
777}
778
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900779static int data_file_size(struct dso *dso, struct machine *machine)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200780{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900781 int ret = 0;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200782 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000783 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200784
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900785 if (dso->data.file_size)
786 return 0;
787
Namhyung Kim71ff8242015-05-21 01:03:39 +0900788 if (dso->data.status == DSO_DATA_STATUS_ERROR)
789 return -1;
790
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900791 pthread_mutex_lock(&dso__data_open_lock);
792
793 /*
794 * dso->data.fd might be closed if other thread opened another
795 * file (dso) due to open file limit (RLIMIT_NOFILE).
796 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900797 try_to_open_dso(dso, machine);
798
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900799 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900800 ret = -errno;
801 dso->data.status = DSO_DATA_STATUS_ERROR;
802 goto out;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200803 }
804
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900805 if (fstat(dso->data.fd, &st) < 0) {
806 ret = -errno;
807 pr_err("dso cache fstat failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300808 str_error_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900809 dso->data.status = DSO_DATA_STATUS_ERROR;
810 goto out;
811 }
812 dso->data.file_size = st.st_size;
813
814out:
815 pthread_mutex_unlock(&dso__data_open_lock);
816 return ret;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200817}
818
Adrian Hunter6d363452014-07-22 16:17:35 +0300819/**
820 * dso__data_size - Return dso data size
821 * @dso: dso object
822 * @machine: machine object
823 *
824 * Return: dso data size
825 */
826off_t dso__data_size(struct dso *dso, struct machine *machine)
827{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900828 if (data_file_size(dso, machine))
Adrian Hunter6d363452014-07-22 16:17:35 +0300829 return -1;
830
831 /* For now just estimate dso data size is close to file size */
832 return dso->data.file_size;
833}
834
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900835static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
836 u64 offset, u8 *data, ssize_t size)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200837{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900838 if (data_file_size(dso, machine))
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200839 return -1;
840
841 /* Check the offset sanity. */
842 if (offset > dso->data.file_size)
843 return -1;
844
845 if (offset + size < offset)
846 return -1;
847
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900848 return cached_read(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200849}
850
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200851/**
852 * dso__data_read_offset - Read data from dso file offset
853 * @dso: dso object
854 * @machine: machine object
855 * @offset: file offset
856 * @data: buffer to store data
857 * @size: size of the @data buffer
858 *
859 * External interface to read data from dso file offset. Open
860 * dso data file and use cached_read to get the data.
861 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200862ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
863 u64 offset, u8 *data, ssize_t size)
864{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900865 if (dso->data.status == DSO_DATA_STATUS_ERROR)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200866 return -1;
867
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900868 return data_read_offset(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200869}
870
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200871/**
872 * dso__data_read_addr - Read data from dso address
873 * @dso: dso object
874 * @machine: machine object
875 * @add: virtual memory address
876 * @data: buffer to store data
877 * @size: size of the @data buffer
878 *
879 * External interface to read data from dso address.
880 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200881ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
882 struct machine *machine, u64 addr,
883 u8 *data, ssize_t size)
884{
885 u64 offset = map->map_ip(map, addr);
886 return dso__data_read_offset(dso, machine, offset, data, size);
887}
888
889struct map *dso__new_map(const char *name)
890{
891 struct map *map = NULL;
892 struct dso *dso = dso__new(name);
893
894 if (dso)
895 map = map__new2(0, dso, MAP__FUNCTION);
896
897 return map;
898}
899
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300900struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
901 const char *short_name, int dso_type)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200902{
903 /*
904 * The kernel dso could be created by build_id processing.
905 */
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300906 struct dso *dso = machine__findnew_dso(machine, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200907
908 /*
909 * We need to run this in all cases, since during the build_id
910 * processing we had no idea this was the kernel dso.
911 */
912 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300913 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200914 dso->kernel = dso_type;
915 }
916
917 return dso;
918}
919
Waiman Long4598a0a2014-09-30 13:36:15 -0400920/*
921 * Find a matching entry and/or link current entry to RB tree.
922 * Either one of the dso or name parameter must be non-NULL or the
923 * function will not work.
924 */
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300925static struct dso *__dso__findlink_by_longname(struct rb_root *root,
926 struct dso *dso, const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400927{
928 struct rb_node **p = &root->rb_node;
929 struct rb_node *parent = NULL;
930
931 if (!name)
932 name = dso->long_name;
933 /*
934 * Find node with the matching name
935 */
936 while (*p) {
937 struct dso *this = rb_entry(*p, struct dso, rb_node);
938 int rc = strcmp(name, this->long_name);
939
940 parent = *p;
941 if (rc == 0) {
942 /*
943 * In case the new DSO is a duplicate of an existing
944 * one, print an one-time warning & put the new entry
945 * at the end of the list of duplicates.
946 */
947 if (!dso || (dso == this))
948 return this; /* Find matching dso */
949 /*
950 * The core kernel DSOs may have duplicated long name.
951 * In this case, the short name should be different.
952 * Comparing the short names to differentiate the DSOs.
953 */
954 rc = strcmp(dso->short_name, this->short_name);
955 if (rc == 0) {
956 pr_err("Duplicated dso name: %s\n", name);
957 return NULL;
958 }
959 }
960 if (rc < 0)
961 p = &parent->rb_left;
962 else
963 p = &parent->rb_right;
964 }
965 if (dso) {
966 /* Add new node and rebalance tree */
967 rb_link_node(&dso->rb_node, parent, p);
968 rb_insert_color(&dso->rb_node, root);
Adrian Huntere266a752015-11-13 11:48:30 +0200969 dso->root = root;
Waiman Long4598a0a2014-09-30 13:36:15 -0400970 }
971 return NULL;
972}
973
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300974static inline struct dso *__dso__find_by_longname(struct rb_root *root,
975 const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400976{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300977 return __dso__findlink_by_longname(root, NULL, name);
Waiman Long4598a0a2014-09-30 13:36:15 -0400978}
979
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300980void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200981{
Adrian Huntere266a752015-11-13 11:48:30 +0200982 struct rb_root *root = dso->root;
983
Jiri Olsacdd059d2012-10-27 23:18:32 +0200984 if (name == NULL)
985 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300986
987 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300988 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300989
Adrian Huntere266a752015-11-13 11:48:30 +0200990 if (root) {
991 rb_erase(&dso->rb_node, root);
992 /*
993 * __dso__findlink_by_longname() isn't guaranteed to add it
994 * back, so a clean removal is required here.
995 */
996 RB_CLEAR_NODE(&dso->rb_node);
997 dso->root = NULL;
998 }
999
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001000 dso->long_name = name;
1001 dso->long_name_len = strlen(name);
1002 dso->long_name_allocated = name_allocated;
Adrian Huntere266a752015-11-13 11:48:30 +02001003
1004 if (root)
1005 __dso__findlink_by_longname(root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001006}
1007
Adrian Hunter58a98c92013-12-10 11:11:46 -03001008void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001009{
1010 if (name == NULL)
1011 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -03001012
1013 if (dso->short_name_allocated)
1014 free((char *)dso->short_name);
1015
1016 dso->short_name = name;
1017 dso->short_name_len = strlen(name);
1018 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001019}
1020
1021static void dso__set_basename(struct dso *dso)
1022{
Stephane Eranianac5e7f82013-12-05 19:26:42 +01001023 /*
1024 * basename() may modify path buffer, so we must pass
1025 * a copy.
1026 */
1027 char *base, *lname = strdup(dso->long_name);
1028
1029 if (!lname)
1030 return;
1031
1032 /*
1033 * basename() may return a pointer to internal
1034 * storage which is reused in subsequent calls
1035 * so copy the result.
1036 */
1037 base = strdup(basename(lname));
1038
1039 free(lname);
1040
1041 if (!base)
1042 return;
1043
1044 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001045}
1046
1047int dso__name_len(const struct dso *dso)
1048{
1049 if (!dso)
1050 return strlen("[unknown]");
1051 if (verbose)
1052 return dso->long_name_len;
1053
1054 return dso->short_name_len;
1055}
1056
1057bool dso__loaded(const struct dso *dso, enum map_type type)
1058{
1059 return dso->loaded & (1 << type);
1060}
1061
1062bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1063{
1064 return dso->sorted_by_name & (1 << type);
1065}
1066
1067void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1068{
1069 dso->sorted_by_name |= (1 << type);
1070}
1071
1072struct dso *dso__new(const char *name)
1073{
1074 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1075
1076 if (dso != NULL) {
1077 int i;
1078 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001079 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -03001080 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001081 for (i = 0; i < MAP__NR_TYPES; ++i)
1082 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +02001083 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001084 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +03001085 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001086 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001087 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001088 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001089 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001090 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001091 dso->sorted_by_name = 0;
1092 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +09001093 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +02001094 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001095 dso->kernel = DSO_TYPE_USER;
1096 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -04001097 RB_CLEAR_NODE(&dso->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +02001098 dso->root = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001099 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +02001100 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001101 pthread_mutex_init(&dso->lock, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001102 atomic_set(&dso->refcnt, 1);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001103 }
1104
1105 return dso;
1106}
1107
1108void dso__delete(struct dso *dso)
1109{
1110 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -04001111
1112 if (!RB_EMPTY_NODE(&dso->rb_node))
1113 pr_err("DSO %s is still in rbtree when being deleted!\n",
1114 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001115 for (i = 0; i < MAP__NR_TYPES; ++i)
1116 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001117
1118 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001119 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001120 dso->short_name_allocated = false;
1121 }
1122
1123 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001124 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001125 dso->long_name_allocated = false;
1126 }
1127
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001128 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +03001129 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +09001130 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +02001131 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001132 zfree(&dso->symsrc_filename);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001133 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001134 free(dso);
1135}
1136
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001137struct dso *dso__get(struct dso *dso)
1138{
1139 if (dso)
1140 atomic_inc(&dso->refcnt);
1141 return dso;
1142}
1143
1144void dso__put(struct dso *dso)
1145{
1146 if (dso && atomic_dec_and_test(&dso->refcnt))
1147 dso__delete(dso);
1148}
1149
Jiri Olsacdd059d2012-10-27 23:18:32 +02001150void dso__set_build_id(struct dso *dso, void *build_id)
1151{
1152 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1153 dso->has_build_id = 1;
1154}
1155
1156bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1157{
1158 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1159}
1160
1161void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1162{
1163 char path[PATH_MAX];
1164
1165 if (machine__is_default_guest(machine))
1166 return;
1167 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1168 if (sysfs__read_build_id(path, dso->build_id,
1169 sizeof(dso->build_id)) == 0)
1170 dso->has_build_id = true;
1171}
1172
1173int dso__kernel_module_get_build_id(struct dso *dso,
1174 const char *root_dir)
1175{
1176 char filename[PATH_MAX];
1177 /*
1178 * kernel module short names are of the form "[module]" and
1179 * we need just "module" here.
1180 */
1181 const char *name = dso->short_name + 1;
1182
1183 snprintf(filename, sizeof(filename),
1184 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1185 root_dir, (int)strlen(name) - 1, name);
1186
1187 if (sysfs__read_build_id(filename, dso->build_id,
1188 sizeof(dso->build_id)) == 0)
1189 dso->has_build_id = true;
1190
1191 return 0;
1192}
1193
1194bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1195{
1196 bool have_build_id = false;
1197 struct dso *pos;
1198
1199 list_for_each_entry(pos, head, node) {
He Kuang6ae98ba2016-05-12 08:43:11 +00001200 if (with_hits && !pos->hit && !dso__is_vdso(pos))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001201 continue;
1202 if (pos->has_build_id) {
1203 have_build_id = true;
1204 continue;
1205 }
1206 if (filename__read_build_id(pos->long_name, pos->build_id,
1207 sizeof(pos->build_id)) > 0) {
1208 have_build_id = true;
1209 pos->has_build_id = true;
1210 }
1211 }
1212
1213 return have_build_id;
1214}
1215
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001216void __dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001217{
Waiman Long8fa7d872014-09-29 16:07:28 -04001218 list_add_tail(&dso->node, &dsos->head);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001219 __dso__findlink_by_longname(&dsos->root, dso, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001220 /*
1221 * It is now in the linked list, grab a reference, then garbage collect
1222 * this when needing memory, by looking at LRU dso instances in the
1223 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1224 * anywhere besides the one for the list, do, under a lock for the
1225 * list: remove it from the list, then a dso__put(), that probably will
1226 * be the last and will then call dso__delete(), end of life.
1227 *
1228 * That, or at the end of the 'struct machine' lifetime, when all
1229 * 'struct dso' instances will be removed from the list, in
1230 * dsos__exit(), if they have no other reference from some other data
1231 * structure.
1232 *
1233 * E.g.: after processing a 'perf.data' file and storing references
1234 * to objects instantiated while processing events, we will have
1235 * references to the 'thread', 'map', 'dso' structs all from 'struct
1236 * hist_entry' instances, but we may not need anything not referenced,
1237 * so we might as well call machines__exit()/machines__delete() and
1238 * garbage collect it.
1239 */
1240 dso__get(dso);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001241}
1242
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001243void dsos__add(struct dsos *dsos, struct dso *dso)
1244{
1245 pthread_rwlock_wrlock(&dsos->lock);
1246 __dsos__add(dsos, dso);
1247 pthread_rwlock_unlock(&dsos->lock);
1248}
1249
1250struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001251{
1252 struct dso *pos;
1253
Waiman Longf9ceffb2013-05-09 10:42:48 -04001254 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001255 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001256 if (strcmp(pos->short_name, name) == 0)
1257 return pos;
1258 return NULL;
1259 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001260 return __dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001261}
1262
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001263struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1264{
1265 struct dso *dso;
1266 pthread_rwlock_rdlock(&dsos->lock);
1267 dso = __dsos__find(dsos, name, cmp_short);
1268 pthread_rwlock_unlock(&dsos->lock);
1269 return dso;
1270}
1271
1272struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
Jiri Olsa701d8d72015-02-12 22:06:09 +01001273{
1274 struct dso *dso = dso__new(name);
1275
1276 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001277 __dsos__add(dsos, dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001278 dso__set_basename(dso);
Masami Hiramatsu82de26a2015-11-18 15:40:31 +09001279 /* Put dso here because __dsos_add already got it */
1280 dso__put(dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001281 }
1282 return dso;
1283}
1284
Waiman Long8fa7d872014-09-29 16:07:28 -04001285struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001286{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001287 struct dso *dso = __dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001288
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001289 return dso ? dso : __dsos__addnew(dsos, name);
1290}
1291
1292struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1293{
1294 struct dso *dso;
1295 pthread_rwlock_wrlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001296 dso = dso__get(__dsos__findnew(dsos, name));
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001297 pthread_rwlock_unlock(&dsos->lock);
1298 return dso;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001299}
1300
1301size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001302 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001303{
1304 struct dso *pos;
1305 size_t ret = 0;
1306
1307 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001308 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001309 continue;
1310 ret += dso__fprintf_buildid(pos, fp);
1311 ret += fprintf(fp, " %s\n", pos->long_name);
1312 }
1313 return ret;
1314}
1315
1316size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1317{
1318 struct dso *pos;
1319 size_t ret = 0;
1320
1321 list_for_each_entry(pos, head, node) {
1322 int i;
1323 for (i = 0; i < MAP__NR_TYPES; ++i)
1324 ret += dso__fprintf(pos, i, fp);
1325 }
1326
1327 return ret;
1328}
1329
1330size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1331{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001332 char sbuild_id[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +02001333
1334 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1335 return fprintf(fp, "%s", sbuild_id);
1336}
1337
1338size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1339{
1340 struct rb_node *nd;
1341 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1342
1343 if (dso->short_name != dso->long_name)
1344 ret += fprintf(fp, "%s, ", dso->long_name);
1345 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001346 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001347 ret += dso__fprintf_buildid(dso, fp);
1348 ret += fprintf(fp, ")\n");
1349 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1350 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1351 ret += symbol__fprintf(pos, fp);
1352 }
1353
1354 return ret;
1355}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001356
1357enum dso_type dso__type(struct dso *dso, struct machine *machine)
1358{
1359 int fd;
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001360 enum dso_type type = DSO__TYPE_UNKNOWN;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001361
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001362 fd = dso__data_get_fd(dso, machine);
1363 if (fd >= 0) {
1364 type = dso__type_fd(fd);
1365 dso__data_put_fd(dso);
1366 }
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001367
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001368 return type;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001369}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001370
1371int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1372{
1373 int idx, errnum = dso->load_errno;
1374 /*
1375 * This must have a same ordering as the enum dso_load_errno.
1376 */
1377 static const char *dso_load__error_str[] = {
1378 "Internal tools/perf/ library error",
1379 "Invalid ELF file",
1380 "Can not read build id",
1381 "Mismatching build id",
1382 "Decompression failure",
1383 };
1384
1385 BUG_ON(buflen == 0);
1386
1387 if (errnum >= 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001388 const char *err = str_error_r(errnum, buf, buflen);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001389
1390 if (err != buf)
1391 scnprintf(buf, buflen, "%s", err);
1392
1393 return 0;
1394 }
1395
1396 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1397 return -1;
1398
1399 idx = errnum - __DSO_LOAD_ERRNO__START;
1400 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1401 return 0;
1402}