blob: d2c6cdd9d42b72a194d913bdc1840d5ff72bb281 [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
Jiri Olsaa08cae02014-05-07 21:35:02 +0200369 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200370 free(name);
371 return fd;
372}
373
Jiri Olsac6580452014-04-30 15:47:27 +0200374static void check_data_close(void);
375
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200376/**
377 * dso_close - Open DSO data file
378 * @dso: dso object
379 *
380 * Open @dso's data file descriptor and updates
381 * list/count of open DSO objects.
382 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200383static int open_dso(struct dso *dso, struct machine *machine)
384{
385 int fd = __open_dso(dso, machine);
386
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300387 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200388 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200389 /*
390 * Check if we crossed the allowed number
391 * of opened DSOs and close one if needed.
392 */
393 check_data_close();
394 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200395
396 return fd;
397}
398
399static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200400{
401 if (dso->data.fd >= 0) {
402 close(dso->data.fd);
403 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200404 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200405 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200406 }
407}
408
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200409/**
410 * dso_close - Close DSO data file
411 * @dso: dso object
412 *
413 * Close @dso's data file descriptor and updates
414 * list/count of open DSO objects.
415 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200416static void close_dso(struct dso *dso)
417{
418 close_data_fd(dso);
419}
420
Jiri Olsac6580452014-04-30 15:47:27 +0200421static void close_first_dso(void)
422{
423 struct dso *dso;
424
425 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
426 close_dso(dso);
427}
428
429static rlim_t get_fd_limit(void)
430{
431 struct rlimit l;
432 rlim_t limit = 0;
433
434 /* Allow half of the current open fd limit. */
435 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
436 if (l.rlim_cur == RLIM_INFINITY)
437 limit = l.rlim_cur;
438 else
439 limit = l.rlim_cur / 2;
440 } else {
441 pr_err("failed to get fd limit\n");
442 limit = 1;
443 }
444
445 return limit;
446}
447
Jiri Olsaf3069242016-06-28 13:29:02 +0200448static rlim_t fd_limit;
449
450/*
451 * Used only by tests/dso-data.c to reset the environment
452 * for tests. I dont expect we should change this during
453 * standard runtime.
454 */
455void reset_fd_limit(void)
456{
457 fd_limit = 0;
458}
459
Jiri Olsac6580452014-04-30 15:47:27 +0200460static bool may_cache_fd(void)
461{
Jiri Olsaf3069242016-06-28 13:29:02 +0200462 if (!fd_limit)
463 fd_limit = get_fd_limit();
Jiri Olsac6580452014-04-30 15:47:27 +0200464
Jiri Olsaf3069242016-06-28 13:29:02 +0200465 if (fd_limit == RLIM_INFINITY)
Jiri Olsac6580452014-04-30 15:47:27 +0200466 return true;
467
Jiri Olsaf3069242016-06-28 13:29:02 +0200468 return fd_limit > (rlim_t) dso__data_open_cnt;
Jiri Olsac6580452014-04-30 15:47:27 +0200469}
470
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200471/*
472 * Check and close LRU dso if we crossed allowed limit
473 * for opened dso file descriptors. The limit is half
474 * of the RLIMIT_NOFILE files opened.
475*/
Jiri Olsac6580452014-04-30 15:47:27 +0200476static void check_data_close(void)
477{
478 bool cache_fd = may_cache_fd();
479
480 if (!cache_fd)
481 close_first_dso();
482}
483
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200484/**
485 * dso__data_close - Close DSO data file
486 * @dso: dso object
487 *
488 * External interface to close @dso's data file descriptor.
489 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200490void dso__data_close(struct dso *dso)
491{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900492 pthread_mutex_lock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200493 close_dso(dso);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900494 pthread_mutex_unlock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200495}
496
Namhyung Kim71ff8242015-05-21 01:03:39 +0900497static void try_to_open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200498{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300499 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200500 DSO_BINARY_TYPE__BUILD_ID_CACHE,
501 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
502 DSO_BINARY_TYPE__NOT_FOUND,
503 };
504 int i = 0;
505
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200506 if (dso->data.fd >= 0)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900507 return;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200508
509 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
510 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300511 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200512 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200513
514 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300515 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200516
Adrian Hunterc27697d2014-07-22 16:17:18 +0300517 dso->data.fd = open_dso(dso, machine);
518 if (dso->data.fd >= 0)
519 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200520
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300521 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300522out:
523 if (dso->data.fd >= 0)
524 dso->data.status = DSO_DATA_STATUS_OK;
525 else
526 dso->data.status = DSO_DATA_STATUS_ERROR;
Namhyung Kim71ff8242015-05-21 01:03:39 +0900527}
Jiri Olsacdd059d2012-10-27 23:18:32 +0200528
Namhyung Kim71ff8242015-05-21 01:03:39 +0900529/**
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900530 * dso__data_get_fd - Get dso's data file descriptor
Namhyung Kim71ff8242015-05-21 01:03:39 +0900531 * @dso: dso object
532 * @machine: machine object
533 *
534 * External interface to find dso's file, open it and
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900535 * returns file descriptor. It should be paired with
536 * dso__data_put_fd() if it returns non-negative value.
Namhyung Kim71ff8242015-05-21 01:03:39 +0900537 */
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900538int dso__data_get_fd(struct dso *dso, struct machine *machine)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900539{
540 if (dso->data.status == DSO_DATA_STATUS_ERROR)
541 return -1;
542
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900543 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
544 return -1;
545
Namhyung Kim71ff8242015-05-21 01:03:39 +0900546 try_to_open_dso(dso, machine);
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900547
548 if (dso->data.fd < 0)
549 pthread_mutex_unlock(&dso__data_open_lock);
Namhyung Kim71ff8242015-05-21 01:03:39 +0900550
Adrian Hunterc27697d2014-07-22 16:17:18 +0300551 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200552}
553
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900554void dso__data_put_fd(struct dso *dso __maybe_unused)
555{
556 pthread_mutex_unlock(&dso__data_open_lock);
557}
558
Adrian Hunter288be942014-07-22 16:17:19 +0300559bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
560{
561 u32 flag = 1 << by;
562
563 if (dso->data.status_seen & flag)
564 return true;
565
566 dso->data.status_seen |= flag;
567
568 return false;
569}
570
Jiri Olsacdd059d2012-10-27 23:18:32 +0200571static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900572dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200573{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900574 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200575 struct rb_node *next = rb_first(root);
576
Namhyung Kim8e67b722015-05-18 09:30:41 +0900577 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200578 while (next) {
579 struct dso_cache *cache;
580
581 cache = rb_entry(next, struct dso_cache, rb_node);
582 next = rb_next(&cache->rb_node);
583 rb_erase(&cache->rb_node, root);
584 free(cache);
585 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900586 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200587}
588
Namhyung Kim8e67b722015-05-18 09:30:41 +0900589static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200590{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900591 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300592 struct rb_node * const *p = &root->rb_node;
593 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200594 struct dso_cache *cache;
595
596 while (*p != NULL) {
597 u64 end;
598
599 parent = *p;
600 cache = rb_entry(parent, struct dso_cache, rb_node);
601 end = cache->offset + DSO__DATA_CACHE_SIZE;
602
603 if (offset < cache->offset)
604 p = &(*p)->rb_left;
605 else if (offset >= end)
606 p = &(*p)->rb_right;
607 else
608 return cache;
609 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900610
Jiri Olsacdd059d2012-10-27 23:18:32 +0200611 return NULL;
612}
613
Namhyung Kim8e67b722015-05-18 09:30:41 +0900614static struct dso_cache *
615dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200616{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900617 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200618 struct rb_node **p = &root->rb_node;
619 struct rb_node *parent = NULL;
620 struct dso_cache *cache;
621 u64 offset = new->offset;
622
Namhyung Kim8e67b722015-05-18 09:30:41 +0900623 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200624 while (*p != NULL) {
625 u64 end;
626
627 parent = *p;
628 cache = rb_entry(parent, struct dso_cache, rb_node);
629 end = cache->offset + DSO__DATA_CACHE_SIZE;
630
631 if (offset < cache->offset)
632 p = &(*p)->rb_left;
633 else if (offset >= end)
634 p = &(*p)->rb_right;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900635 else
636 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200637 }
638
639 rb_link_node(&new->rb_node, parent, p);
640 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900641
642 cache = NULL;
643out:
644 pthread_mutex_unlock(&dso->lock);
645 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200646}
647
648static ssize_t
649dso_cache__memcpy(struct dso_cache *cache, u64 offset,
650 u8 *data, u64 size)
651{
652 u64 cache_offset = offset - cache->offset;
653 u64 cache_size = min(cache->size - cache_offset, size);
654
655 memcpy(data, cache->data + cache_offset, cache_size);
656 return cache_size;
657}
658
659static ssize_t
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900660dso_cache__read(struct dso *dso, struct machine *machine,
661 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200662{
663 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900664 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200665 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200666
667 do {
668 u64 cache_offset;
669
Jiri Olsacdd059d2012-10-27 23:18:32 +0200670 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
671 if (!cache)
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900672 return -ENOMEM;
673
674 pthread_mutex_lock(&dso__data_open_lock);
675
676 /*
677 * dso->data.fd might be closed if other thread opened another
678 * file (dso) due to open file limit (RLIMIT_NOFILE).
679 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900680 try_to_open_dso(dso, machine);
681
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900682 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900683 ret = -errno;
684 dso->data.status = DSO_DATA_STATUS_ERROR;
685 break;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900686 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200687
688 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200689
Namhyung Kimc52686f2015-01-29 17:02:01 -0300690 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200691 if (ret <= 0)
692 break;
693
694 cache->offset = cache_offset;
695 cache->size = ret;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900696 } while (0);
697
698 pthread_mutex_unlock(&dso__data_open_lock);
699
700 if (ret > 0) {
Namhyung Kim8e67b722015-05-18 09:30:41 +0900701 old = dso_cache__insert(dso, cache);
702 if (old) {
703 /* we lose the race */
704 free(cache);
705 cache = old;
706 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200707
708 ret = dso_cache__memcpy(cache, offset, data, size);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900709 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200710
711 if (ret <= 0)
712 free(cache);
713
Jiri Olsacdd059d2012-10-27 23:18:32 +0200714 return ret;
715}
716
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900717static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
718 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200719{
720 struct dso_cache *cache;
721
Namhyung Kim8e67b722015-05-18 09:30:41 +0900722 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200723 if (cache)
724 return dso_cache__memcpy(cache, offset, data, size);
725 else
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900726 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200727}
728
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200729/*
730 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
731 * in the rb_tree. Any read to already cached data is served
732 * by cached data.
733 */
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900734static ssize_t cached_read(struct dso *dso, struct machine *machine,
735 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200736{
737 ssize_t r = 0;
738 u8 *p = data;
739
740 do {
741 ssize_t ret;
742
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900743 ret = dso_cache_read(dso, machine, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200744 if (ret < 0)
745 return ret;
746
747 /* Reached EOF, return what we have. */
748 if (!ret)
749 break;
750
751 BUG_ON(ret > size);
752
753 r += ret;
754 p += ret;
755 offset += ret;
756 size -= ret;
757
758 } while (size);
759
760 return r;
761}
762
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900763static int data_file_size(struct dso *dso, struct machine *machine)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200764{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900765 int ret = 0;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200766 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000767 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200768
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900769 if (dso->data.file_size)
770 return 0;
771
Namhyung Kim71ff8242015-05-21 01:03:39 +0900772 if (dso->data.status == DSO_DATA_STATUS_ERROR)
773 return -1;
774
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900775 pthread_mutex_lock(&dso__data_open_lock);
776
777 /*
778 * dso->data.fd might be closed if other thread opened another
779 * file (dso) due to open file limit (RLIMIT_NOFILE).
780 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900781 try_to_open_dso(dso, machine);
782
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900783 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900784 ret = -errno;
785 dso->data.status = DSO_DATA_STATUS_ERROR;
786 goto out;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200787 }
788
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900789 if (fstat(dso->data.fd, &st) < 0) {
790 ret = -errno;
791 pr_err("dso cache fstat failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300792 str_error_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900793 dso->data.status = DSO_DATA_STATUS_ERROR;
794 goto out;
795 }
796 dso->data.file_size = st.st_size;
797
798out:
799 pthread_mutex_unlock(&dso__data_open_lock);
800 return ret;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200801}
802
Adrian Hunter6d363452014-07-22 16:17:35 +0300803/**
804 * dso__data_size - Return dso data size
805 * @dso: dso object
806 * @machine: machine object
807 *
808 * Return: dso data size
809 */
810off_t dso__data_size(struct dso *dso, struct machine *machine)
811{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900812 if (data_file_size(dso, machine))
Adrian Hunter6d363452014-07-22 16:17:35 +0300813 return -1;
814
815 /* For now just estimate dso data size is close to file size */
816 return dso->data.file_size;
817}
818
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900819static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
820 u64 offset, u8 *data, ssize_t size)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200821{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900822 if (data_file_size(dso, machine))
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200823 return -1;
824
825 /* Check the offset sanity. */
826 if (offset > dso->data.file_size)
827 return -1;
828
829 if (offset + size < offset)
830 return -1;
831
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900832 return cached_read(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200833}
834
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200835/**
836 * dso__data_read_offset - Read data from dso file offset
837 * @dso: dso object
838 * @machine: machine object
839 * @offset: file offset
840 * @data: buffer to store data
841 * @size: size of the @data buffer
842 *
843 * External interface to read data from dso file offset. Open
844 * dso data file and use cached_read to get the data.
845 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200846ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
847 u64 offset, u8 *data, ssize_t size)
848{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900849 if (dso->data.status == DSO_DATA_STATUS_ERROR)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200850 return -1;
851
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900852 return data_read_offset(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200853}
854
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200855/**
856 * dso__data_read_addr - Read data from dso address
857 * @dso: dso object
858 * @machine: machine object
859 * @add: virtual memory address
860 * @data: buffer to store data
861 * @size: size of the @data buffer
862 *
863 * External interface to read data from dso address.
864 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200865ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
866 struct machine *machine, u64 addr,
867 u8 *data, ssize_t size)
868{
869 u64 offset = map->map_ip(map, addr);
870 return dso__data_read_offset(dso, machine, offset, data, size);
871}
872
873struct map *dso__new_map(const char *name)
874{
875 struct map *map = NULL;
876 struct dso *dso = dso__new(name);
877
878 if (dso)
879 map = map__new2(0, dso, MAP__FUNCTION);
880
881 return map;
882}
883
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300884struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
885 const char *short_name, int dso_type)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200886{
887 /*
888 * The kernel dso could be created by build_id processing.
889 */
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300890 struct dso *dso = machine__findnew_dso(machine, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200891
892 /*
893 * We need to run this in all cases, since during the build_id
894 * processing we had no idea this was the kernel dso.
895 */
896 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300897 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200898 dso->kernel = dso_type;
899 }
900
901 return dso;
902}
903
Waiman Long4598a0a2014-09-30 13:36:15 -0400904/*
905 * Find a matching entry and/or link current entry to RB tree.
906 * Either one of the dso or name parameter must be non-NULL or the
907 * function will not work.
908 */
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300909static struct dso *__dso__findlink_by_longname(struct rb_root *root,
910 struct dso *dso, const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400911{
912 struct rb_node **p = &root->rb_node;
913 struct rb_node *parent = NULL;
914
915 if (!name)
916 name = dso->long_name;
917 /*
918 * Find node with the matching name
919 */
920 while (*p) {
921 struct dso *this = rb_entry(*p, struct dso, rb_node);
922 int rc = strcmp(name, this->long_name);
923
924 parent = *p;
925 if (rc == 0) {
926 /*
927 * In case the new DSO is a duplicate of an existing
928 * one, print an one-time warning & put the new entry
929 * at the end of the list of duplicates.
930 */
931 if (!dso || (dso == this))
932 return this; /* Find matching dso */
933 /*
934 * The core kernel DSOs may have duplicated long name.
935 * In this case, the short name should be different.
936 * Comparing the short names to differentiate the DSOs.
937 */
938 rc = strcmp(dso->short_name, this->short_name);
939 if (rc == 0) {
940 pr_err("Duplicated dso name: %s\n", name);
941 return NULL;
942 }
943 }
944 if (rc < 0)
945 p = &parent->rb_left;
946 else
947 p = &parent->rb_right;
948 }
949 if (dso) {
950 /* Add new node and rebalance tree */
951 rb_link_node(&dso->rb_node, parent, p);
952 rb_insert_color(&dso->rb_node, root);
Adrian Huntere266a752015-11-13 11:48:30 +0200953 dso->root = root;
Waiman Long4598a0a2014-09-30 13:36:15 -0400954 }
955 return NULL;
956}
957
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300958static inline struct dso *__dso__find_by_longname(struct rb_root *root,
959 const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400960{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300961 return __dso__findlink_by_longname(root, NULL, name);
Waiman Long4598a0a2014-09-30 13:36:15 -0400962}
963
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300964void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200965{
Adrian Huntere266a752015-11-13 11:48:30 +0200966 struct rb_root *root = dso->root;
967
Jiri Olsacdd059d2012-10-27 23:18:32 +0200968 if (name == NULL)
969 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300970
971 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300972 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300973
Adrian Huntere266a752015-11-13 11:48:30 +0200974 if (root) {
975 rb_erase(&dso->rb_node, root);
976 /*
977 * __dso__findlink_by_longname() isn't guaranteed to add it
978 * back, so a clean removal is required here.
979 */
980 RB_CLEAR_NODE(&dso->rb_node);
981 dso->root = NULL;
982 }
983
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300984 dso->long_name = name;
985 dso->long_name_len = strlen(name);
986 dso->long_name_allocated = name_allocated;
Adrian Huntere266a752015-11-13 11:48:30 +0200987
988 if (root)
989 __dso__findlink_by_longname(root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200990}
991
Adrian Hunter58a98c92013-12-10 11:11:46 -0300992void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200993{
994 if (name == NULL)
995 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -0300996
997 if (dso->short_name_allocated)
998 free((char *)dso->short_name);
999
1000 dso->short_name = name;
1001 dso->short_name_len = strlen(name);
1002 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001003}
1004
1005static void dso__set_basename(struct dso *dso)
1006{
Stephane Eranianac5e7f82013-12-05 19:26:42 +01001007 /*
1008 * basename() may modify path buffer, so we must pass
1009 * a copy.
1010 */
1011 char *base, *lname = strdup(dso->long_name);
1012
1013 if (!lname)
1014 return;
1015
1016 /*
1017 * basename() may return a pointer to internal
1018 * storage which is reused in subsequent calls
1019 * so copy the result.
1020 */
1021 base = strdup(basename(lname));
1022
1023 free(lname);
1024
1025 if (!base)
1026 return;
1027
1028 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001029}
1030
1031int dso__name_len(const struct dso *dso)
1032{
1033 if (!dso)
1034 return strlen("[unknown]");
1035 if (verbose)
1036 return dso->long_name_len;
1037
1038 return dso->short_name_len;
1039}
1040
1041bool dso__loaded(const struct dso *dso, enum map_type type)
1042{
1043 return dso->loaded & (1 << type);
1044}
1045
1046bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1047{
1048 return dso->sorted_by_name & (1 << type);
1049}
1050
1051void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1052{
1053 dso->sorted_by_name |= (1 << type);
1054}
1055
1056struct dso *dso__new(const char *name)
1057{
1058 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1059
1060 if (dso != NULL) {
1061 int i;
1062 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001063 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -03001064 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001065 for (i = 0; i < MAP__NR_TYPES; ++i)
1066 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +02001067 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001068 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +03001069 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001070 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001071 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001072 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001073 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001074 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001075 dso->sorted_by_name = 0;
1076 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +09001077 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +02001078 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001079 dso->kernel = DSO_TYPE_USER;
1080 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -04001081 RB_CLEAR_NODE(&dso->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +02001082 dso->root = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001083 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +02001084 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001085 pthread_mutex_init(&dso->lock, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001086 atomic_set(&dso->refcnt, 1);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001087 }
1088
1089 return dso;
1090}
1091
1092void dso__delete(struct dso *dso)
1093{
1094 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -04001095
1096 if (!RB_EMPTY_NODE(&dso->rb_node))
1097 pr_err("DSO %s is still in rbtree when being deleted!\n",
1098 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001099 for (i = 0; i < MAP__NR_TYPES; ++i)
1100 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001101
1102 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001103 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001104 dso->short_name_allocated = false;
1105 }
1106
1107 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001108 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001109 dso->long_name_allocated = false;
1110 }
1111
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001112 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +03001113 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +09001114 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +02001115 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001116 zfree(&dso->symsrc_filename);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001117 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001118 free(dso);
1119}
1120
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001121struct dso *dso__get(struct dso *dso)
1122{
1123 if (dso)
1124 atomic_inc(&dso->refcnt);
1125 return dso;
1126}
1127
1128void dso__put(struct dso *dso)
1129{
1130 if (dso && atomic_dec_and_test(&dso->refcnt))
1131 dso__delete(dso);
1132}
1133
Jiri Olsacdd059d2012-10-27 23:18:32 +02001134void dso__set_build_id(struct dso *dso, void *build_id)
1135{
1136 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1137 dso->has_build_id = 1;
1138}
1139
1140bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1141{
1142 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1143}
1144
1145void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1146{
1147 char path[PATH_MAX];
1148
1149 if (machine__is_default_guest(machine))
1150 return;
1151 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1152 if (sysfs__read_build_id(path, dso->build_id,
1153 sizeof(dso->build_id)) == 0)
1154 dso->has_build_id = true;
1155}
1156
1157int dso__kernel_module_get_build_id(struct dso *dso,
1158 const char *root_dir)
1159{
1160 char filename[PATH_MAX];
1161 /*
1162 * kernel module short names are of the form "[module]" and
1163 * we need just "module" here.
1164 */
1165 const char *name = dso->short_name + 1;
1166
1167 snprintf(filename, sizeof(filename),
1168 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1169 root_dir, (int)strlen(name) - 1, name);
1170
1171 if (sysfs__read_build_id(filename, dso->build_id,
1172 sizeof(dso->build_id)) == 0)
1173 dso->has_build_id = true;
1174
1175 return 0;
1176}
1177
1178bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1179{
1180 bool have_build_id = false;
1181 struct dso *pos;
1182
1183 list_for_each_entry(pos, head, node) {
He Kuang6ae98ba2016-05-12 08:43:11 +00001184 if (with_hits && !pos->hit && !dso__is_vdso(pos))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001185 continue;
1186 if (pos->has_build_id) {
1187 have_build_id = true;
1188 continue;
1189 }
1190 if (filename__read_build_id(pos->long_name, pos->build_id,
1191 sizeof(pos->build_id)) > 0) {
1192 have_build_id = true;
1193 pos->has_build_id = true;
1194 }
1195 }
1196
1197 return have_build_id;
1198}
1199
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001200void __dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001201{
Waiman Long8fa7d872014-09-29 16:07:28 -04001202 list_add_tail(&dso->node, &dsos->head);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001203 __dso__findlink_by_longname(&dsos->root, dso, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001204 /*
1205 * It is now in the linked list, grab a reference, then garbage collect
1206 * this when needing memory, by looking at LRU dso instances in the
1207 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1208 * anywhere besides the one for the list, do, under a lock for the
1209 * list: remove it from the list, then a dso__put(), that probably will
1210 * be the last and will then call dso__delete(), end of life.
1211 *
1212 * That, or at the end of the 'struct machine' lifetime, when all
1213 * 'struct dso' instances will be removed from the list, in
1214 * dsos__exit(), if they have no other reference from some other data
1215 * structure.
1216 *
1217 * E.g.: after processing a 'perf.data' file and storing references
1218 * to objects instantiated while processing events, we will have
1219 * references to the 'thread', 'map', 'dso' structs all from 'struct
1220 * hist_entry' instances, but we may not need anything not referenced,
1221 * so we might as well call machines__exit()/machines__delete() and
1222 * garbage collect it.
1223 */
1224 dso__get(dso);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001225}
1226
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001227void dsos__add(struct dsos *dsos, struct dso *dso)
1228{
1229 pthread_rwlock_wrlock(&dsos->lock);
1230 __dsos__add(dsos, dso);
1231 pthread_rwlock_unlock(&dsos->lock);
1232}
1233
1234struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001235{
1236 struct dso *pos;
1237
Waiman Longf9ceffb2013-05-09 10:42:48 -04001238 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001239 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001240 if (strcmp(pos->short_name, name) == 0)
1241 return pos;
1242 return NULL;
1243 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001244 return __dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001245}
1246
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001247struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1248{
1249 struct dso *dso;
1250 pthread_rwlock_rdlock(&dsos->lock);
1251 dso = __dsos__find(dsos, name, cmp_short);
1252 pthread_rwlock_unlock(&dsos->lock);
1253 return dso;
1254}
1255
1256struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
Jiri Olsa701d8d72015-02-12 22:06:09 +01001257{
1258 struct dso *dso = dso__new(name);
1259
1260 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001261 __dsos__add(dsos, dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001262 dso__set_basename(dso);
Masami Hiramatsu82de26a2015-11-18 15:40:31 +09001263 /* Put dso here because __dsos_add already got it */
1264 dso__put(dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001265 }
1266 return dso;
1267}
1268
Waiman Long8fa7d872014-09-29 16:07:28 -04001269struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001270{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001271 struct dso *dso = __dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001272
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001273 return dso ? dso : __dsos__addnew(dsos, name);
1274}
1275
1276struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1277{
1278 struct dso *dso;
1279 pthread_rwlock_wrlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001280 dso = dso__get(__dsos__findnew(dsos, name));
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001281 pthread_rwlock_unlock(&dsos->lock);
1282 return dso;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001283}
1284
1285size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001286 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001287{
1288 struct dso *pos;
1289 size_t ret = 0;
1290
1291 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001292 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001293 continue;
1294 ret += dso__fprintf_buildid(pos, fp);
1295 ret += fprintf(fp, " %s\n", pos->long_name);
1296 }
1297 return ret;
1298}
1299
1300size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1301{
1302 struct dso *pos;
1303 size_t ret = 0;
1304
1305 list_for_each_entry(pos, head, node) {
1306 int i;
1307 for (i = 0; i < MAP__NR_TYPES; ++i)
1308 ret += dso__fprintf(pos, i, fp);
1309 }
1310
1311 return ret;
1312}
1313
1314size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1315{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001316 char sbuild_id[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +02001317
1318 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1319 return fprintf(fp, "%s", sbuild_id);
1320}
1321
1322size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1323{
1324 struct rb_node *nd;
1325 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1326
1327 if (dso->short_name != dso->long_name)
1328 ret += fprintf(fp, "%s, ", dso->long_name);
1329 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001330 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001331 ret += dso__fprintf_buildid(dso, fp);
1332 ret += fprintf(fp, ")\n");
1333 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1334 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1335 ret += symbol__fprintf(pos, fp);
1336 }
1337
1338 return ret;
1339}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001340
1341enum dso_type dso__type(struct dso *dso, struct machine *machine)
1342{
1343 int fd;
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001344 enum dso_type type = DSO__TYPE_UNKNOWN;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001345
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001346 fd = dso__data_get_fd(dso, machine);
1347 if (fd >= 0) {
1348 type = dso__type_fd(fd);
1349 dso__data_put_fd(dso);
1350 }
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001351
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001352 return type;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001353}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001354
1355int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1356{
1357 int idx, errnum = dso->load_errno;
1358 /*
1359 * This must have a same ordering as the enum dso_load_errno.
1360 */
1361 static const char *dso_load__error_str[] = {
1362 "Internal tools/perf/ library error",
1363 "Invalid ELF file",
1364 "Can not read build id",
1365 "Mismatching build id",
1366 "Decompression failure",
1367 };
1368
1369 BUG_ON(buflen == 0);
1370
1371 if (errnum >= 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001372 const char *err = str_error_r(errnum, buf, buflen);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001373
1374 if (err != buf)
1375 scnprintf(buf, buflen, "%s", err);
1376
1377 return 0;
1378 }
1379
1380 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1381 return -1;
1382
1383 idx = errnum - __DSO_LOAD_ERRNO__START;
1384 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1385 return 0;
1386}