blob: 75b75615e2f881be286b043cdbcd256862db9d4b [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:
66 /* skip the locally configured cache if a symfs is given */
67 if (symbol_conf.symfs[0] ||
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030068 (dso__build_id_filename(dso, filename, size) == NULL))
Jiri Olsacdd059d2012-10-27 23:18:32 +020069 ret = -1;
70 break;
71
72 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030073 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
74 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +020075 break;
76
77 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030078 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
79 snprintf(filename + len, size - len, "%s", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +020080 break;
81
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020082 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
83 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -030084 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020085 size_t dir_size;
86
87 last_slash = dso->long_name + dso->long_name_len;
88 while (last_slash != dso->long_name && *last_slash != '/')
89 last_slash--;
90
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030091 len = __symbol__join_symfs(filename, size, "");
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020092 dir_size = last_slash - dso->long_name + 2;
93 if (dir_size > (size - len)) {
94 ret = -1;
95 break;
96 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030097 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
98 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020099 last_slash);
100 break;
101 }
102
Jiri Olsacdd059d2012-10-27 23:18:32 +0200103 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
104 if (!dso->has_build_id) {
105 ret = -1;
106 break;
107 }
108
109 build_id__sprintf(dso->build_id,
110 sizeof(dso->build_id),
111 build_id_hex);
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300112 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
113 snprintf(filename + len, size - len, "%.2s/%s.debug",
114 build_id_hex, build_id_hex + 2);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200115 break;
116
Adrian Hunter39b12f782013-08-07 14:38:47 +0300117 case DSO_BINARY_TYPE__VMLINUX:
118 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200119 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300120 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200121 break;
122
123 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900124 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300125 path__join3(filename, size, symbol_conf.symfs,
126 root_dir, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200127 break;
128
129 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900130 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300131 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200132 break;
133
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300134 case DSO_BINARY_TYPE__KCORE:
135 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300136 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300137 break;
138
Jiri Olsacdd059d2012-10-27 23:18:32 +0200139 default:
140 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200141 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200142 case DSO_BINARY_TYPE__JAVA_JIT:
143 case DSO_BINARY_TYPE__NOT_FOUND:
144 ret = -1;
145 break;
146 }
147
148 return ret;
149}
150
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900151static const struct {
152 const char *fmt;
153 int (*decompress)(const char *input, int output);
154} compressions[] = {
Namhyung Kime92ce122014-10-31 16:51:38 +0900155#ifdef HAVE_ZLIB_SUPPORT
156 { "gz", gzip_decompress_to_file },
157#endif
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100158#ifdef HAVE_LZMA_SUPPORT
159 { "xz", lzma_decompress_to_file },
160#endif
Namhyung Kime92ce122014-10-31 16:51:38 +0900161 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900162};
163
164bool is_supported_compression(const char *ext)
165{
166 unsigned i;
167
168 for (i = 0; compressions[i].fmt; i++) {
169 if (!strcmp(ext, compressions[i].fmt))
170 return true;
171 }
172 return false;
173}
174
Wang Nan1f121b02015-06-03 08:52:21 +0000175bool is_kernel_module(const char *pathname, int cpumode)
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900176{
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100177 struct kmod_path m;
Wang Nan1f121b02015-06-03 08:52:21 +0000178 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900179
Wang Nan1f121b02015-06-03 08:52:21 +0000180 WARN_ONCE(mode != cpumode,
181 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
182 cpumode);
183
184 switch (mode) {
185 case PERF_RECORD_MISC_USER:
186 case PERF_RECORD_MISC_HYPERVISOR:
187 case PERF_RECORD_MISC_GUEST_USER:
188 return false;
189 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
190 default:
191 if (kmod_path__parse(&m, pathname)) {
192 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
193 pathname);
194 return true;
195 }
196 }
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900197
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100198 return m.kmod;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900199}
200
201bool decompress_to_file(const char *ext, const char *filename, int output_fd)
202{
203 unsigned i;
204
205 for (i = 0; compressions[i].fmt; i++) {
206 if (!strcmp(ext, compressions[i].fmt))
207 return !compressions[i].decompress(filename,
208 output_fd);
209 }
210 return false;
211}
212
213bool dso__needs_decompress(struct dso *dso)
214{
215 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
216 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
217}
218
Jiri Olsaeba51022014-04-30 15:00:59 +0200219/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100220 * Parses kernel module specified in @path and updates
221 * @m argument like:
222 *
223 * @comp - true if @path contains supported compression suffix,
224 * false otherwise
225 * @kmod - true if @path contains '.ko' suffix in right position,
226 * false otherwise
227 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
228 * of the kernel module without suffixes, otherwise strudup-ed
229 * base name of @path
230 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
231 * the compression suffix
232 *
233 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
234 */
235int __kmod_path__parse(struct kmod_path *m, const char *path,
236 bool alloc_name, bool alloc_ext)
237{
238 const char *name = strrchr(path, '/');
239 const char *ext = strrchr(path, '.');
Wang Nan1f121b02015-06-03 08:52:21 +0000240 bool is_simple_name = false;
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100241
242 memset(m, 0x0, sizeof(*m));
243 name = name ? name + 1 : path;
244
Wang Nan1f121b02015-06-03 08:52:21 +0000245 /*
246 * '.' is also a valid character for module name. For example:
247 * [aaa.bbb] is a valid module name. '[' should have higher
248 * priority than '.ko' suffix.
249 *
250 * The kernel names are from machine__mmap_name. Such
251 * name should belong to kernel itself, not kernel module.
252 */
253 if (name[0] == '[') {
254 is_simple_name = true;
255 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
256 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
257 (strncmp(name, "[vdso]", 6) == 0) ||
258 (strncmp(name, "[vsyscall]", 10) == 0)) {
259 m->kmod = false;
260
261 } else
262 m->kmod = true;
263 }
264
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100265 /* No extension, just return name. */
Wang Nan1f121b02015-06-03 08:52:21 +0000266 if ((ext == NULL) || is_simple_name) {
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100267 if (alloc_name) {
268 m->name = strdup(name);
269 return m->name ? 0 : -ENOMEM;
270 }
271 return 0;
272 }
273
274 if (is_supported_compression(ext + 1)) {
275 m->comp = true;
276 ext -= 3;
277 }
278
279 /* Check .ko extension only if there's enough name left. */
280 if (ext > name)
281 m->kmod = !strncmp(ext, ".ko", 3);
282
283 if (alloc_name) {
284 if (m->kmod) {
285 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
286 return -ENOMEM;
287 } else {
288 if (asprintf(&m->name, "%s", name) == -1)
289 return -ENOMEM;
290 }
291
292 strxfrchar(m->name, '-', '_');
293 }
294
295 if (alloc_ext && m->comp) {
296 m->ext = strdup(ext + 4);
297 if (!m->ext) {
298 free((void *) m->name);
299 return -ENOMEM;
300 }
301 }
302
303 return 0;
304}
305
306/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200307 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200308 */
309static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200310static long dso__data_open_cnt;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900311static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
Jiri Olsaeba51022014-04-30 15:00:59 +0200312
313static void dso__list_add(struct dso *dso)
314{
315 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200316 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200317}
318
319static void dso__list_del(struct dso *dso)
320{
321 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200322 WARN_ONCE(dso__data_open_cnt <= 0,
323 "DSO data fd counter out of bounds.");
324 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200325}
326
Jiri Olsaa08cae02014-05-07 21:35:02 +0200327static void close_first_dso(void);
328
329static int do_open(char *name)
330{
331 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000332 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200333
334 do {
335 fd = open(name, O_RDONLY);
336 if (fd >= 0)
337 return fd;
338
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900339 pr_debug("dso open failed: %s\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000340 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200341 if (!dso__data_open_cnt || errno != EMFILE)
342 break;
343
344 close_first_dso();
345 } while (1);
346
347 return -1;
348}
349
Jiri Olsaeba51022014-04-30 15:00:59 +0200350static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200351{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200352 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300353 char *root_dir = (char *)"";
354 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200355
Jiri Olsacdd059d2012-10-27 23:18:32 +0200356 if (!name)
357 return -ENOMEM;
358
359 if (machine)
360 root_dir = machine->root_dir;
361
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300362 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300363 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200364 free(name);
365 return -EINVAL;
366 }
367
Jiri Olsaa08cae02014-05-07 21:35:02 +0200368 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200369 free(name);
370 return fd;
371}
372
Jiri Olsac6580452014-04-30 15:47:27 +0200373static void check_data_close(void);
374
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200375/**
376 * dso_close - Open DSO data file
377 * @dso: dso object
378 *
379 * Open @dso's data file descriptor and updates
380 * list/count of open DSO objects.
381 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200382static int open_dso(struct dso *dso, struct machine *machine)
383{
384 int fd = __open_dso(dso, machine);
385
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300386 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200387 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200388 /*
389 * Check if we crossed the allowed number
390 * of opened DSOs and close one if needed.
391 */
392 check_data_close();
393 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200394
395 return fd;
396}
397
398static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200399{
400 if (dso->data.fd >= 0) {
401 close(dso->data.fd);
402 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200403 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200404 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200405 }
406}
407
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200408/**
409 * dso_close - Close DSO data file
410 * @dso: dso object
411 *
412 * Close @dso's data file descriptor and updates
413 * list/count of open DSO objects.
414 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200415static void close_dso(struct dso *dso)
416{
417 close_data_fd(dso);
418}
419
Jiri Olsac6580452014-04-30 15:47:27 +0200420static void close_first_dso(void)
421{
422 struct dso *dso;
423
424 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
425 close_dso(dso);
426}
427
428static rlim_t get_fd_limit(void)
429{
430 struct rlimit l;
431 rlim_t limit = 0;
432
433 /* Allow half of the current open fd limit. */
434 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
435 if (l.rlim_cur == RLIM_INFINITY)
436 limit = l.rlim_cur;
437 else
438 limit = l.rlim_cur / 2;
439 } else {
440 pr_err("failed to get fd limit\n");
441 limit = 1;
442 }
443
444 return limit;
445}
446
447static bool may_cache_fd(void)
448{
449 static rlim_t limit;
450
451 if (!limit)
452 limit = get_fd_limit();
453
454 if (limit == RLIM_INFINITY)
455 return true;
456
457 return limit > (rlim_t) dso__data_open_cnt;
458}
459
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200460/*
461 * Check and close LRU dso if we crossed allowed limit
462 * for opened dso file descriptors. The limit is half
463 * of the RLIMIT_NOFILE files opened.
464*/
Jiri Olsac6580452014-04-30 15:47:27 +0200465static void check_data_close(void)
466{
467 bool cache_fd = may_cache_fd();
468
469 if (!cache_fd)
470 close_first_dso();
471}
472
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200473/**
474 * dso__data_close - Close DSO data file
475 * @dso: dso object
476 *
477 * External interface to close @dso's data file descriptor.
478 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200479void dso__data_close(struct dso *dso)
480{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900481 pthread_mutex_lock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200482 close_dso(dso);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900483 pthread_mutex_unlock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200484}
485
Namhyung Kim71ff8242015-05-21 01:03:39 +0900486static void try_to_open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200487{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300488 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200489 DSO_BINARY_TYPE__BUILD_ID_CACHE,
490 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
491 DSO_BINARY_TYPE__NOT_FOUND,
492 };
493 int i = 0;
494
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200495 if (dso->data.fd >= 0)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900496 return;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200497
498 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
499 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300500 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200501 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200502
503 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300504 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200505
Adrian Hunterc27697d2014-07-22 16:17:18 +0300506 dso->data.fd = open_dso(dso, machine);
507 if (dso->data.fd >= 0)
508 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200509
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300510 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300511out:
512 if (dso->data.fd >= 0)
513 dso->data.status = DSO_DATA_STATUS_OK;
514 else
515 dso->data.status = DSO_DATA_STATUS_ERROR;
Namhyung Kim71ff8242015-05-21 01:03:39 +0900516}
Jiri Olsacdd059d2012-10-27 23:18:32 +0200517
Namhyung Kim71ff8242015-05-21 01:03:39 +0900518/**
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900519 * dso__data_get_fd - Get dso's data file descriptor
Namhyung Kim71ff8242015-05-21 01:03:39 +0900520 * @dso: dso object
521 * @machine: machine object
522 *
523 * External interface to find dso's file, open it and
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900524 * returns file descriptor. It should be paired with
525 * dso__data_put_fd() if it returns non-negative value.
Namhyung Kim71ff8242015-05-21 01:03:39 +0900526 */
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900527int dso__data_get_fd(struct dso *dso, struct machine *machine)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900528{
529 if (dso->data.status == DSO_DATA_STATUS_ERROR)
530 return -1;
531
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900532 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
533 return -1;
534
Namhyung Kim71ff8242015-05-21 01:03:39 +0900535 try_to_open_dso(dso, machine);
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900536
537 if (dso->data.fd < 0)
538 pthread_mutex_unlock(&dso__data_open_lock);
Namhyung Kim71ff8242015-05-21 01:03:39 +0900539
Adrian Hunterc27697d2014-07-22 16:17:18 +0300540 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200541}
542
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900543void dso__data_put_fd(struct dso *dso __maybe_unused)
544{
545 pthread_mutex_unlock(&dso__data_open_lock);
546}
547
Adrian Hunter288be942014-07-22 16:17:19 +0300548bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
549{
550 u32 flag = 1 << by;
551
552 if (dso->data.status_seen & flag)
553 return true;
554
555 dso->data.status_seen |= flag;
556
557 return false;
558}
559
Jiri Olsacdd059d2012-10-27 23:18:32 +0200560static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900561dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200562{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900563 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200564 struct rb_node *next = rb_first(root);
565
Namhyung Kim8e67b722015-05-18 09:30:41 +0900566 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200567 while (next) {
568 struct dso_cache *cache;
569
570 cache = rb_entry(next, struct dso_cache, rb_node);
571 next = rb_next(&cache->rb_node);
572 rb_erase(&cache->rb_node, root);
573 free(cache);
574 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900575 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200576}
577
Namhyung Kim8e67b722015-05-18 09:30:41 +0900578static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200579{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900580 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300581 struct rb_node * const *p = &root->rb_node;
582 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200583 struct dso_cache *cache;
584
585 while (*p != NULL) {
586 u64 end;
587
588 parent = *p;
589 cache = rb_entry(parent, struct dso_cache, rb_node);
590 end = cache->offset + DSO__DATA_CACHE_SIZE;
591
592 if (offset < cache->offset)
593 p = &(*p)->rb_left;
594 else if (offset >= end)
595 p = &(*p)->rb_right;
596 else
597 return cache;
598 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900599
Jiri Olsacdd059d2012-10-27 23:18:32 +0200600 return NULL;
601}
602
Namhyung Kim8e67b722015-05-18 09:30:41 +0900603static struct dso_cache *
604dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200605{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900606 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200607 struct rb_node **p = &root->rb_node;
608 struct rb_node *parent = NULL;
609 struct dso_cache *cache;
610 u64 offset = new->offset;
611
Namhyung Kim8e67b722015-05-18 09:30:41 +0900612 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200613 while (*p != NULL) {
614 u64 end;
615
616 parent = *p;
617 cache = rb_entry(parent, struct dso_cache, rb_node);
618 end = cache->offset + DSO__DATA_CACHE_SIZE;
619
620 if (offset < cache->offset)
621 p = &(*p)->rb_left;
622 else if (offset >= end)
623 p = &(*p)->rb_right;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900624 else
625 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200626 }
627
628 rb_link_node(&new->rb_node, parent, p);
629 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900630
631 cache = NULL;
632out:
633 pthread_mutex_unlock(&dso->lock);
634 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200635}
636
637static ssize_t
638dso_cache__memcpy(struct dso_cache *cache, u64 offset,
639 u8 *data, u64 size)
640{
641 u64 cache_offset = offset - cache->offset;
642 u64 cache_size = min(cache->size - cache_offset, size);
643
644 memcpy(data, cache->data + cache_offset, cache_size);
645 return cache_size;
646}
647
648static ssize_t
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900649dso_cache__read(struct dso *dso, struct machine *machine,
650 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200651{
652 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900653 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200654 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200655
656 do {
657 u64 cache_offset;
658
Jiri Olsacdd059d2012-10-27 23:18:32 +0200659 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
660 if (!cache)
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900661 return -ENOMEM;
662
663 pthread_mutex_lock(&dso__data_open_lock);
664
665 /*
666 * dso->data.fd might be closed if other thread opened another
667 * file (dso) due to open file limit (RLIMIT_NOFILE).
668 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900669 try_to_open_dso(dso, machine);
670
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900671 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900672 ret = -errno;
673 dso->data.status = DSO_DATA_STATUS_ERROR;
674 break;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900675 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200676
677 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200678
Namhyung Kimc52686f2015-01-29 17:02:01 -0300679 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200680 if (ret <= 0)
681 break;
682
683 cache->offset = cache_offset;
684 cache->size = ret;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900685 } while (0);
686
687 pthread_mutex_unlock(&dso__data_open_lock);
688
689 if (ret > 0) {
Namhyung Kim8e67b722015-05-18 09:30:41 +0900690 old = dso_cache__insert(dso, cache);
691 if (old) {
692 /* we lose the race */
693 free(cache);
694 cache = old;
695 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200696
697 ret = dso_cache__memcpy(cache, offset, data, size);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900698 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200699
700 if (ret <= 0)
701 free(cache);
702
Jiri Olsacdd059d2012-10-27 23:18:32 +0200703 return ret;
704}
705
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900706static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
707 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200708{
709 struct dso_cache *cache;
710
Namhyung Kim8e67b722015-05-18 09:30:41 +0900711 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200712 if (cache)
713 return dso_cache__memcpy(cache, offset, data, size);
714 else
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900715 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200716}
717
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200718/*
719 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
720 * in the rb_tree. Any read to already cached data is served
721 * by cached data.
722 */
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900723static ssize_t cached_read(struct dso *dso, struct machine *machine,
724 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200725{
726 ssize_t r = 0;
727 u8 *p = data;
728
729 do {
730 ssize_t ret;
731
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900732 ret = dso_cache_read(dso, machine, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200733 if (ret < 0)
734 return ret;
735
736 /* Reached EOF, return what we have. */
737 if (!ret)
738 break;
739
740 BUG_ON(ret > size);
741
742 r += ret;
743 p += ret;
744 offset += ret;
745 size -= ret;
746
747 } while (size);
748
749 return r;
750}
751
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900752static int data_file_size(struct dso *dso, struct machine *machine)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200753{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900754 int ret = 0;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200755 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000756 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200757
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900758 if (dso->data.file_size)
759 return 0;
760
Namhyung Kim71ff8242015-05-21 01:03:39 +0900761 if (dso->data.status == DSO_DATA_STATUS_ERROR)
762 return -1;
763
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900764 pthread_mutex_lock(&dso__data_open_lock);
765
766 /*
767 * dso->data.fd might be closed if other thread opened another
768 * file (dso) due to open file limit (RLIMIT_NOFILE).
769 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900770 try_to_open_dso(dso, machine);
771
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900772 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900773 ret = -errno;
774 dso->data.status = DSO_DATA_STATUS_ERROR;
775 goto out;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200776 }
777
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900778 if (fstat(dso->data.fd, &st) < 0) {
779 ret = -errno;
780 pr_err("dso cache fstat failed: %s\n",
781 strerror_r(errno, sbuf, sizeof(sbuf)));
782 dso->data.status = DSO_DATA_STATUS_ERROR;
783 goto out;
784 }
785 dso->data.file_size = st.st_size;
786
787out:
788 pthread_mutex_unlock(&dso__data_open_lock);
789 return ret;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200790}
791
Adrian Hunter6d363452014-07-22 16:17:35 +0300792/**
793 * dso__data_size - Return dso data size
794 * @dso: dso object
795 * @machine: machine object
796 *
797 * Return: dso data size
798 */
799off_t dso__data_size(struct dso *dso, struct machine *machine)
800{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900801 if (data_file_size(dso, machine))
Adrian Hunter6d363452014-07-22 16:17:35 +0300802 return -1;
803
804 /* For now just estimate dso data size is close to file size */
805 return dso->data.file_size;
806}
807
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900808static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
809 u64 offset, u8 *data, ssize_t size)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200810{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900811 if (data_file_size(dso, machine))
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200812 return -1;
813
814 /* Check the offset sanity. */
815 if (offset > dso->data.file_size)
816 return -1;
817
818 if (offset + size < offset)
819 return -1;
820
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900821 return cached_read(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200822}
823
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200824/**
825 * dso__data_read_offset - Read data from dso file offset
826 * @dso: dso object
827 * @machine: machine object
828 * @offset: file offset
829 * @data: buffer to store data
830 * @size: size of the @data buffer
831 *
832 * External interface to read data from dso file offset. Open
833 * dso data file and use cached_read to get the data.
834 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200835ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
836 u64 offset, u8 *data, ssize_t size)
837{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900838 if (dso->data.status == DSO_DATA_STATUS_ERROR)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200839 return -1;
840
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900841 return data_read_offset(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200842}
843
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200844/**
845 * dso__data_read_addr - Read data from dso address
846 * @dso: dso object
847 * @machine: machine object
848 * @add: virtual memory address
849 * @data: buffer to store data
850 * @size: size of the @data buffer
851 *
852 * External interface to read data from dso address.
853 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200854ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
855 struct machine *machine, u64 addr,
856 u8 *data, ssize_t size)
857{
858 u64 offset = map->map_ip(map, addr);
859 return dso__data_read_offset(dso, machine, offset, data, size);
860}
861
862struct map *dso__new_map(const char *name)
863{
864 struct map *map = NULL;
865 struct dso *dso = dso__new(name);
866
867 if (dso)
868 map = map__new2(0, dso, MAP__FUNCTION);
869
870 return map;
871}
872
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300873struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
874 const char *short_name, int dso_type)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200875{
876 /*
877 * The kernel dso could be created by build_id processing.
878 */
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300879 struct dso *dso = machine__findnew_dso(machine, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200880
881 /*
882 * We need to run this in all cases, since during the build_id
883 * processing we had no idea this was the kernel dso.
884 */
885 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300886 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200887 dso->kernel = dso_type;
888 }
889
890 return dso;
891}
892
Waiman Long4598a0a2014-09-30 13:36:15 -0400893/*
894 * Find a matching entry and/or link current entry to RB tree.
895 * Either one of the dso or name parameter must be non-NULL or the
896 * function will not work.
897 */
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300898static struct dso *__dso__findlink_by_longname(struct rb_root *root,
899 struct dso *dso, const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400900{
901 struct rb_node **p = &root->rb_node;
902 struct rb_node *parent = NULL;
903
904 if (!name)
905 name = dso->long_name;
906 /*
907 * Find node with the matching name
908 */
909 while (*p) {
910 struct dso *this = rb_entry(*p, struct dso, rb_node);
911 int rc = strcmp(name, this->long_name);
912
913 parent = *p;
914 if (rc == 0) {
915 /*
916 * In case the new DSO is a duplicate of an existing
917 * one, print an one-time warning & put the new entry
918 * at the end of the list of duplicates.
919 */
920 if (!dso || (dso == this))
921 return this; /* Find matching dso */
922 /*
923 * The core kernel DSOs may have duplicated long name.
924 * In this case, the short name should be different.
925 * Comparing the short names to differentiate the DSOs.
926 */
927 rc = strcmp(dso->short_name, this->short_name);
928 if (rc == 0) {
929 pr_err("Duplicated dso name: %s\n", name);
930 return NULL;
931 }
932 }
933 if (rc < 0)
934 p = &parent->rb_left;
935 else
936 p = &parent->rb_right;
937 }
938 if (dso) {
939 /* Add new node and rebalance tree */
940 rb_link_node(&dso->rb_node, parent, p);
941 rb_insert_color(&dso->rb_node, root);
Adrian Huntere266a752015-11-13 11:48:30 +0200942 dso->root = root;
Waiman Long4598a0a2014-09-30 13:36:15 -0400943 }
944 return NULL;
945}
946
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300947static inline struct dso *__dso__find_by_longname(struct rb_root *root,
948 const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400949{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300950 return __dso__findlink_by_longname(root, NULL, name);
Waiman Long4598a0a2014-09-30 13:36:15 -0400951}
952
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300953void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200954{
Adrian Huntere266a752015-11-13 11:48:30 +0200955 struct rb_root *root = dso->root;
956
Jiri Olsacdd059d2012-10-27 23:18:32 +0200957 if (name == NULL)
958 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300959
960 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300961 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300962
Adrian Huntere266a752015-11-13 11:48:30 +0200963 if (root) {
964 rb_erase(&dso->rb_node, root);
965 /*
966 * __dso__findlink_by_longname() isn't guaranteed to add it
967 * back, so a clean removal is required here.
968 */
969 RB_CLEAR_NODE(&dso->rb_node);
970 dso->root = NULL;
971 }
972
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300973 dso->long_name = name;
974 dso->long_name_len = strlen(name);
975 dso->long_name_allocated = name_allocated;
Adrian Huntere266a752015-11-13 11:48:30 +0200976
977 if (root)
978 __dso__findlink_by_longname(root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200979}
980
Adrian Hunter58a98c92013-12-10 11:11:46 -0300981void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200982{
983 if (name == NULL)
984 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -0300985
986 if (dso->short_name_allocated)
987 free((char *)dso->short_name);
988
989 dso->short_name = name;
990 dso->short_name_len = strlen(name);
991 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200992}
993
994static void dso__set_basename(struct dso *dso)
995{
Stephane Eranianac5e7f82013-12-05 19:26:42 +0100996 /*
997 * basename() may modify path buffer, so we must pass
998 * a copy.
999 */
1000 char *base, *lname = strdup(dso->long_name);
1001
1002 if (!lname)
1003 return;
1004
1005 /*
1006 * basename() may return a pointer to internal
1007 * storage which is reused in subsequent calls
1008 * so copy the result.
1009 */
1010 base = strdup(basename(lname));
1011
1012 free(lname);
1013
1014 if (!base)
1015 return;
1016
1017 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001018}
1019
1020int dso__name_len(const struct dso *dso)
1021{
1022 if (!dso)
1023 return strlen("[unknown]");
1024 if (verbose)
1025 return dso->long_name_len;
1026
1027 return dso->short_name_len;
1028}
1029
1030bool dso__loaded(const struct dso *dso, enum map_type type)
1031{
1032 return dso->loaded & (1 << type);
1033}
1034
1035bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1036{
1037 return dso->sorted_by_name & (1 << type);
1038}
1039
1040void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1041{
1042 dso->sorted_by_name |= (1 << type);
1043}
1044
1045struct dso *dso__new(const char *name)
1046{
1047 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1048
1049 if (dso != NULL) {
1050 int i;
1051 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001052 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -03001053 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001054 for (i = 0; i < MAP__NR_TYPES; ++i)
1055 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +02001056 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001057 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +03001058 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001059 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001060 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001061 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001062 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001063 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001064 dso->sorted_by_name = 0;
1065 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +09001066 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +02001067 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001068 dso->kernel = DSO_TYPE_USER;
1069 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -04001070 RB_CLEAR_NODE(&dso->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +02001071 dso->root = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001072 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +02001073 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001074 pthread_mutex_init(&dso->lock, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001075 atomic_set(&dso->refcnt, 1);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001076 }
1077
1078 return dso;
1079}
1080
1081void dso__delete(struct dso *dso)
1082{
1083 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -04001084
1085 if (!RB_EMPTY_NODE(&dso->rb_node))
1086 pr_err("DSO %s is still in rbtree when being deleted!\n",
1087 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001088 for (i = 0; i < MAP__NR_TYPES; ++i)
1089 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001090
1091 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001092 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001093 dso->short_name_allocated = false;
1094 }
1095
1096 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001097 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001098 dso->long_name_allocated = false;
1099 }
1100
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001101 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +03001102 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +09001103 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +02001104 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001105 zfree(&dso->symsrc_filename);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001106 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001107 free(dso);
1108}
1109
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001110struct dso *dso__get(struct dso *dso)
1111{
1112 if (dso)
1113 atomic_inc(&dso->refcnt);
1114 return dso;
1115}
1116
1117void dso__put(struct dso *dso)
1118{
1119 if (dso && atomic_dec_and_test(&dso->refcnt))
1120 dso__delete(dso);
1121}
1122
Jiri Olsacdd059d2012-10-27 23:18:32 +02001123void dso__set_build_id(struct dso *dso, void *build_id)
1124{
1125 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1126 dso->has_build_id = 1;
1127}
1128
1129bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1130{
1131 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1132}
1133
1134void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1135{
1136 char path[PATH_MAX];
1137
1138 if (machine__is_default_guest(machine))
1139 return;
1140 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1141 if (sysfs__read_build_id(path, dso->build_id,
1142 sizeof(dso->build_id)) == 0)
1143 dso->has_build_id = true;
1144}
1145
1146int dso__kernel_module_get_build_id(struct dso *dso,
1147 const char *root_dir)
1148{
1149 char filename[PATH_MAX];
1150 /*
1151 * kernel module short names are of the form "[module]" and
1152 * we need just "module" here.
1153 */
1154 const char *name = dso->short_name + 1;
1155
1156 snprintf(filename, sizeof(filename),
1157 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1158 root_dir, (int)strlen(name) - 1, name);
1159
1160 if (sysfs__read_build_id(filename, dso->build_id,
1161 sizeof(dso->build_id)) == 0)
1162 dso->has_build_id = true;
1163
1164 return 0;
1165}
1166
1167bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1168{
1169 bool have_build_id = false;
1170 struct dso *pos;
1171
1172 list_for_each_entry(pos, head, node) {
He Kuang6ae98ba2016-05-12 08:43:11 +00001173 if (with_hits && !pos->hit && !dso__is_vdso(pos))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001174 continue;
1175 if (pos->has_build_id) {
1176 have_build_id = true;
1177 continue;
1178 }
1179 if (filename__read_build_id(pos->long_name, pos->build_id,
1180 sizeof(pos->build_id)) > 0) {
1181 have_build_id = true;
1182 pos->has_build_id = true;
1183 }
1184 }
1185
1186 return have_build_id;
1187}
1188
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001189void __dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001190{
Waiman Long8fa7d872014-09-29 16:07:28 -04001191 list_add_tail(&dso->node, &dsos->head);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001192 __dso__findlink_by_longname(&dsos->root, dso, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001193 /*
1194 * It is now in the linked list, grab a reference, then garbage collect
1195 * this when needing memory, by looking at LRU dso instances in the
1196 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1197 * anywhere besides the one for the list, do, under a lock for the
1198 * list: remove it from the list, then a dso__put(), that probably will
1199 * be the last and will then call dso__delete(), end of life.
1200 *
1201 * That, or at the end of the 'struct machine' lifetime, when all
1202 * 'struct dso' instances will be removed from the list, in
1203 * dsos__exit(), if they have no other reference from some other data
1204 * structure.
1205 *
1206 * E.g.: after processing a 'perf.data' file and storing references
1207 * to objects instantiated while processing events, we will have
1208 * references to the 'thread', 'map', 'dso' structs all from 'struct
1209 * hist_entry' instances, but we may not need anything not referenced,
1210 * so we might as well call machines__exit()/machines__delete() and
1211 * garbage collect it.
1212 */
1213 dso__get(dso);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001214}
1215
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001216void dsos__add(struct dsos *dsos, struct dso *dso)
1217{
1218 pthread_rwlock_wrlock(&dsos->lock);
1219 __dsos__add(dsos, dso);
1220 pthread_rwlock_unlock(&dsos->lock);
1221}
1222
1223struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001224{
1225 struct dso *pos;
1226
Waiman Longf9ceffb2013-05-09 10:42:48 -04001227 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001228 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001229 if (strcmp(pos->short_name, name) == 0)
1230 return pos;
1231 return NULL;
1232 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001233 return __dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001234}
1235
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001236struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1237{
1238 struct dso *dso;
1239 pthread_rwlock_rdlock(&dsos->lock);
1240 dso = __dsos__find(dsos, name, cmp_short);
1241 pthread_rwlock_unlock(&dsos->lock);
1242 return dso;
1243}
1244
1245struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
Jiri Olsa701d8d72015-02-12 22:06:09 +01001246{
1247 struct dso *dso = dso__new(name);
1248
1249 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001250 __dsos__add(dsos, dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001251 dso__set_basename(dso);
Masami Hiramatsu82de26a2015-11-18 15:40:31 +09001252 /* Put dso here because __dsos_add already got it */
1253 dso__put(dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001254 }
1255 return dso;
1256}
1257
Waiman Long8fa7d872014-09-29 16:07:28 -04001258struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001259{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001260 struct dso *dso = __dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001261
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001262 return dso ? dso : __dsos__addnew(dsos, name);
1263}
1264
1265struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1266{
1267 struct dso *dso;
1268 pthread_rwlock_wrlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001269 dso = dso__get(__dsos__findnew(dsos, name));
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001270 pthread_rwlock_unlock(&dsos->lock);
1271 return dso;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001272}
1273
1274size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001275 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001276{
1277 struct dso *pos;
1278 size_t ret = 0;
1279
1280 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001281 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001282 continue;
1283 ret += dso__fprintf_buildid(pos, fp);
1284 ret += fprintf(fp, " %s\n", pos->long_name);
1285 }
1286 return ret;
1287}
1288
1289size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1290{
1291 struct dso *pos;
1292 size_t ret = 0;
1293
1294 list_for_each_entry(pos, head, node) {
1295 int i;
1296 for (i = 0; i < MAP__NR_TYPES; ++i)
1297 ret += dso__fprintf(pos, i, fp);
1298 }
1299
1300 return ret;
1301}
1302
1303size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1304{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001305 char sbuild_id[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +02001306
1307 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1308 return fprintf(fp, "%s", sbuild_id);
1309}
1310
1311size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1312{
1313 struct rb_node *nd;
1314 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1315
1316 if (dso->short_name != dso->long_name)
1317 ret += fprintf(fp, "%s, ", dso->long_name);
1318 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001319 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001320 ret += dso__fprintf_buildid(dso, fp);
1321 ret += fprintf(fp, ")\n");
1322 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1323 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1324 ret += symbol__fprintf(pos, fp);
1325 }
1326
1327 return ret;
1328}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001329
1330enum dso_type dso__type(struct dso *dso, struct machine *machine)
1331{
1332 int fd;
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001333 enum dso_type type = DSO__TYPE_UNKNOWN;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001334
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001335 fd = dso__data_get_fd(dso, machine);
1336 if (fd >= 0) {
1337 type = dso__type_fd(fd);
1338 dso__data_put_fd(dso);
1339 }
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001340
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001341 return type;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001342}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001343
1344int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1345{
1346 int idx, errnum = dso->load_errno;
1347 /*
1348 * This must have a same ordering as the enum dso_load_errno.
1349 */
1350 static const char *dso_load__error_str[] = {
1351 "Internal tools/perf/ library error",
1352 "Invalid ELF file",
1353 "Can not read build id",
1354 "Mismatching build id",
1355 "Decompression failure",
1356 };
1357
1358 BUG_ON(buflen == 0);
1359
1360 if (errnum >= 0) {
1361 const char *err = strerror_r(errnum, buf, buflen);
1362
1363 if (err != buf)
1364 scnprintf(buf, buflen, "%s", err);
1365
1366 return 0;
1367 }
1368
1369 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1370 return -1;
1371
1372 idx = errnum - __DSO_LOAD_ERRNO__START;
1373 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1374 return 0;
1375}