blob: cd061dc1de7010c9385771ac19ac158cbc85ea3c [file] [log] [blame]
Jiri Olsabda6ee42014-04-30 15:25:10 +02001#include <asm/bug.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03002#include <linux/kernel.h>
Jiri Olsac6580452014-04-30 15:47:27 +02003#include <sys/time.h>
4#include <sys/resource.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03005#include <errno.h>
Jiri Olsacdd059d2012-10-27 23:18:32 +02006#include "symbol.h"
7#include "dso.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -03008#include "machine.h"
Adrian Huntercfe91742015-04-09 18:53:55 +03009#include "auxtrace.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020010#include "util.h"
11#include "debug.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030012#include "string2.h"
He Kuang6ae98ba2016-05-12 08:43:11 +000013#include "vdso.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +020014
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010015static const char * const debuglink_paths[] = {
16 "%.0s%s",
17 "%s/%s",
18 "%s/.debug/%s",
19 "/usr/lib/debug%s/%s"
20};
21
Jiri Olsacdd059d2012-10-27 23:18:32 +020022char dso__symtab_origin(const struct dso *dso)
23{
24 static const char origin[] = {
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020025 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
26 [DSO_BINARY_TYPE__VMLINUX] = 'v',
27 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
28 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
29 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
30 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
31 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
32 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
33 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
34 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
35 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090036 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020037 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
38 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090039 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020040 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Jiri Olsacdd059d2012-10-27 23:18:32 +020041 };
42
43 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
44 return '!';
45 return origin[dso->symtab_type];
46}
47
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -030048int dso__read_binary_type_filename(const struct dso *dso,
49 enum dso_binary_type type,
50 char *root_dir, char *filename, size_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +020051{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +090052 char build_id_hex[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +020053 int ret = 0;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030054 size_t len;
Jiri Olsacdd059d2012-10-27 23:18:32 +020055
56 switch (type) {
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010057 case DSO_BINARY_TYPE__DEBUGLINK:
58 {
59 const char *last_slash;
60 char dso_dir[PATH_MAX];
61 char symfile[PATH_MAX];
62 unsigned int i;
Jiri Olsacdd059d2012-10-27 23:18:32 +020063
Victor Kamenskydc6254c2015-01-26 22:34:02 -080064 len = __symbol__join_symfs(filename, size, dso->long_name);
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010065 last_slash = filename + len;
66 while (last_slash != filename && *last_slash != '/')
67 last_slash--;
Jiri Olsa40356722016-01-20 12:56:32 +010068
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010069 strncpy(dso_dir, filename, last_slash - filename);
70 dso_dir[last_slash-filename] = '\0';
71
72 if (!is_regular_file(filename)) {
73 ret = -1;
74 break;
75 }
76
77 ret = filename__read_debuglink(filename, symfile, PATH_MAX);
78 if (ret)
Jiri Olsa40356722016-01-20 12:56:32 +010079 break;
80
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010081 /* Check predefined locations where debug file might reside */
82 ret = -1;
83 for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
84 snprintf(filename, size,
85 debuglink_paths[i], dso_dir, symfile);
86 if (is_regular_file(filename)) {
87 ret = 0;
88 break;
89 }
Jiri Olsacdd059d2012-10-27 23:18:32 +020090 }
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010091
Jiri Olsacdd059d2012-10-27 23:18:32 +020092 break;
Matija Glavinic Pecotic9343e452017-01-17 15:50:35 +010093 }
Jiri Olsacdd059d2012-10-27 23:18:32 +020094 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
He Kuanga7066702016-05-19 11:47:37 +000095 if (dso__build_id_filename(dso, filename, size) == NULL)
Jiri Olsacdd059d2012-10-27 23:18:32 +020096 ret = -1;
97 break;
98
99 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300100 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
101 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200102 break;
103
104 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300105 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
106 snprintf(filename + len, size - len, "%s", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200107 break;
108
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200109 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
110 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300111 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200112 size_t dir_size;
113
114 last_slash = dso->long_name + dso->long_name_len;
115 while (last_slash != dso->long_name && *last_slash != '/')
116 last_slash--;
117
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300118 len = __symbol__join_symfs(filename, size, "");
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200119 dir_size = last_slash - dso->long_name + 2;
120 if (dir_size > (size - len)) {
121 ret = -1;
122 break;
123 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300124 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
125 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +0200126 last_slash);
127 break;
128 }
129
Jiri Olsacdd059d2012-10-27 23:18:32 +0200130 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
131 if (!dso->has_build_id) {
132 ret = -1;
133 break;
134 }
135
136 build_id__sprintf(dso->build_id,
137 sizeof(dso->build_id),
138 build_id_hex);
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300139 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
140 snprintf(filename + len, size - len, "%.2s/%s.debug",
141 build_id_hex, build_id_hex + 2);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200142 break;
143
Adrian Hunter39b12f782013-08-07 14:38:47 +0300144 case DSO_BINARY_TYPE__VMLINUX:
145 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200146 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300147 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200148 break;
149
150 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900151 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300152 path__join3(filename, size, symbol_conf.symfs,
153 root_dir, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200154 break;
155
156 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900157 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300158 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200159 break;
160
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300161 case DSO_BINARY_TYPE__KCORE:
162 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300163 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300164 break;
165
Jiri Olsacdd059d2012-10-27 23:18:32 +0200166 default:
167 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200168 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200169 case DSO_BINARY_TYPE__JAVA_JIT:
170 case DSO_BINARY_TYPE__NOT_FOUND:
171 ret = -1;
172 break;
173 }
174
175 return ret;
176}
177
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900178static const struct {
179 const char *fmt;
180 int (*decompress)(const char *input, int output);
181} compressions[] = {
Namhyung Kime92ce122014-10-31 16:51:38 +0900182#ifdef HAVE_ZLIB_SUPPORT
183 { "gz", gzip_decompress_to_file },
184#endif
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100185#ifdef HAVE_LZMA_SUPPORT
186 { "xz", lzma_decompress_to_file },
187#endif
Namhyung Kime92ce122014-10-31 16:51:38 +0900188 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900189};
190
191bool is_supported_compression(const char *ext)
192{
193 unsigned i;
194
195 for (i = 0; compressions[i].fmt; i++) {
196 if (!strcmp(ext, compressions[i].fmt))
197 return true;
198 }
199 return false;
200}
201
Wang Nan1f121b02015-06-03 08:52:21 +0000202bool is_kernel_module(const char *pathname, int cpumode)
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900203{
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100204 struct kmod_path m;
Wang Nan1f121b02015-06-03 08:52:21 +0000205 int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900206
Wang Nan1f121b02015-06-03 08:52:21 +0000207 WARN_ONCE(mode != cpumode,
208 "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
209 cpumode);
210
211 switch (mode) {
212 case PERF_RECORD_MISC_USER:
213 case PERF_RECORD_MISC_HYPERVISOR:
214 case PERF_RECORD_MISC_GUEST_USER:
215 return false;
216 /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
217 default:
218 if (kmod_path__parse(&m, pathname)) {
219 pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
220 pathname);
221 return true;
222 }
223 }
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900224
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100225 return m.kmod;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900226}
227
228bool decompress_to_file(const char *ext, const char *filename, int output_fd)
229{
230 unsigned i;
231
232 for (i = 0; compressions[i].fmt; i++) {
233 if (!strcmp(ext, compressions[i].fmt))
234 return !compressions[i].decompress(filename,
235 output_fd);
236 }
237 return false;
238}
239
240bool dso__needs_decompress(struct dso *dso)
241{
242 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
243 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
244}
245
Jiri Olsaeba51022014-04-30 15:00:59 +0200246/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100247 * Parses kernel module specified in @path and updates
248 * @m argument like:
249 *
250 * @comp - true if @path contains supported compression suffix,
251 * false otherwise
252 * @kmod - true if @path contains '.ko' suffix in right position,
253 * false otherwise
254 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
255 * of the kernel module without suffixes, otherwise strudup-ed
256 * base name of @path
257 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
258 * the compression suffix
259 *
260 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
261 */
262int __kmod_path__parse(struct kmod_path *m, const char *path,
263 bool alloc_name, bool alloc_ext)
264{
265 const char *name = strrchr(path, '/');
266 const char *ext = strrchr(path, '.');
Wang Nan1f121b02015-06-03 08:52:21 +0000267 bool is_simple_name = false;
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100268
269 memset(m, 0x0, sizeof(*m));
270 name = name ? name + 1 : path;
271
Wang Nan1f121b02015-06-03 08:52:21 +0000272 /*
273 * '.' is also a valid character for module name. For example:
274 * [aaa.bbb] is a valid module name. '[' should have higher
275 * priority than '.ko' suffix.
276 *
277 * The kernel names are from machine__mmap_name. Such
278 * name should belong to kernel itself, not kernel module.
279 */
280 if (name[0] == '[') {
281 is_simple_name = true;
282 if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
283 (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
284 (strncmp(name, "[vdso]", 6) == 0) ||
285 (strncmp(name, "[vsyscall]", 10) == 0)) {
286 m->kmod = false;
287
288 } else
289 m->kmod = true;
290 }
291
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100292 /* No extension, just return name. */
Wang Nan1f121b02015-06-03 08:52:21 +0000293 if ((ext == NULL) || is_simple_name) {
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100294 if (alloc_name) {
295 m->name = strdup(name);
296 return m->name ? 0 : -ENOMEM;
297 }
298 return 0;
299 }
300
301 if (is_supported_compression(ext + 1)) {
302 m->comp = true;
303 ext -= 3;
304 }
305
306 /* Check .ko extension only if there's enough name left. */
307 if (ext > name)
308 m->kmod = !strncmp(ext, ".ko", 3);
309
310 if (alloc_name) {
311 if (m->kmod) {
312 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
313 return -ENOMEM;
314 } else {
315 if (asprintf(&m->name, "%s", name) == -1)
316 return -ENOMEM;
317 }
318
319 strxfrchar(m->name, '-', '_');
320 }
321
322 if (alloc_ext && m->comp) {
323 m->ext = strdup(ext + 4);
324 if (!m->ext) {
325 free((void *) m->name);
326 return -ENOMEM;
327 }
328 }
329
330 return 0;
331}
332
333/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200334 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200335 */
336static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200337static long dso__data_open_cnt;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900338static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
Jiri Olsaeba51022014-04-30 15:00:59 +0200339
340static void dso__list_add(struct dso *dso)
341{
342 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200343 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200344}
345
346static void dso__list_del(struct dso *dso)
347{
348 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200349 WARN_ONCE(dso__data_open_cnt <= 0,
350 "DSO data fd counter out of bounds.");
351 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200352}
353
Jiri Olsaa08cae02014-05-07 21:35:02 +0200354static void close_first_dso(void);
355
356static int do_open(char *name)
357{
358 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000359 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200360
361 do {
362 fd = open(name, O_RDONLY);
363 if (fd >= 0)
364 return fd;
365
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900366 pr_debug("dso open failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300367 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200368 if (!dso__data_open_cnt || errno != EMFILE)
369 break;
370
371 close_first_dso();
372 } while (1);
373
374 return -1;
375}
376
Jiri Olsaeba51022014-04-30 15:00:59 +0200377static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200378{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200379 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300380 char *root_dir = (char *)"";
381 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200382
Jiri Olsacdd059d2012-10-27 23:18:32 +0200383 if (!name)
384 return -ENOMEM;
385
386 if (machine)
387 root_dir = machine->root_dir;
388
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300389 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300390 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200391 free(name);
392 return -EINVAL;
393 }
394
Jiri Olsa3c028a02016-09-20 18:12:45 +0200395 if (!is_regular_file(name))
396 return -EINVAL;
397
Jiri Olsaa08cae02014-05-07 21:35:02 +0200398 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200399 free(name);
400 return fd;
401}
402
Jiri Olsac6580452014-04-30 15:47:27 +0200403static void check_data_close(void);
404
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200405/**
406 * dso_close - Open DSO data file
407 * @dso: dso object
408 *
409 * Open @dso's data file descriptor and updates
410 * list/count of open DSO objects.
411 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200412static int open_dso(struct dso *dso, struct machine *machine)
413{
414 int fd = __open_dso(dso, machine);
415
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300416 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200417 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200418 /*
419 * Check if we crossed the allowed number
420 * of opened DSOs and close one if needed.
421 */
422 check_data_close();
423 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200424
425 return fd;
426}
427
428static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200429{
430 if (dso->data.fd >= 0) {
431 close(dso->data.fd);
432 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200433 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200434 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200435 }
436}
437
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200438/**
439 * dso_close - Close DSO data file
440 * @dso: dso object
441 *
442 * Close @dso's data file descriptor and updates
443 * list/count of open DSO objects.
444 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200445static void close_dso(struct dso *dso)
446{
447 close_data_fd(dso);
448}
449
Jiri Olsac6580452014-04-30 15:47:27 +0200450static void close_first_dso(void)
451{
452 struct dso *dso;
453
454 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
455 close_dso(dso);
456}
457
458static rlim_t get_fd_limit(void)
459{
460 struct rlimit l;
461 rlim_t limit = 0;
462
463 /* Allow half of the current open fd limit. */
464 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
465 if (l.rlim_cur == RLIM_INFINITY)
466 limit = l.rlim_cur;
467 else
468 limit = l.rlim_cur / 2;
469 } else {
470 pr_err("failed to get fd limit\n");
471 limit = 1;
472 }
473
474 return limit;
475}
476
Jiri Olsaf3069242016-06-28 13:29:02 +0200477static rlim_t fd_limit;
478
479/*
480 * Used only by tests/dso-data.c to reset the environment
481 * for tests. I dont expect we should change this during
482 * standard runtime.
483 */
484void reset_fd_limit(void)
485{
486 fd_limit = 0;
487}
488
Jiri Olsac6580452014-04-30 15:47:27 +0200489static bool may_cache_fd(void)
490{
Jiri Olsaf3069242016-06-28 13:29:02 +0200491 if (!fd_limit)
492 fd_limit = get_fd_limit();
Jiri Olsac6580452014-04-30 15:47:27 +0200493
Jiri Olsaf3069242016-06-28 13:29:02 +0200494 if (fd_limit == RLIM_INFINITY)
Jiri Olsac6580452014-04-30 15:47:27 +0200495 return true;
496
Jiri Olsaf3069242016-06-28 13:29:02 +0200497 return fd_limit > (rlim_t) dso__data_open_cnt;
Jiri Olsac6580452014-04-30 15:47:27 +0200498}
499
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200500/*
501 * Check and close LRU dso if we crossed allowed limit
502 * for opened dso file descriptors. The limit is half
503 * of the RLIMIT_NOFILE files opened.
504*/
Jiri Olsac6580452014-04-30 15:47:27 +0200505static void check_data_close(void)
506{
507 bool cache_fd = may_cache_fd();
508
509 if (!cache_fd)
510 close_first_dso();
511}
512
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200513/**
514 * dso__data_close - Close DSO data file
515 * @dso: dso object
516 *
517 * External interface to close @dso's data file descriptor.
518 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200519void dso__data_close(struct dso *dso)
520{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900521 pthread_mutex_lock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200522 close_dso(dso);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900523 pthread_mutex_unlock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200524}
525
Namhyung Kim71ff8242015-05-21 01:03:39 +0900526static void try_to_open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200527{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300528 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200529 DSO_BINARY_TYPE__BUILD_ID_CACHE,
530 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
531 DSO_BINARY_TYPE__NOT_FOUND,
532 };
533 int i = 0;
534
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200535 if (dso->data.fd >= 0)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900536 return;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200537
538 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
539 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300540 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200541 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200542
543 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300544 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200545
Adrian Hunterc27697d2014-07-22 16:17:18 +0300546 dso->data.fd = open_dso(dso, machine);
547 if (dso->data.fd >= 0)
548 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200549
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300550 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300551out:
552 if (dso->data.fd >= 0)
553 dso->data.status = DSO_DATA_STATUS_OK;
554 else
555 dso->data.status = DSO_DATA_STATUS_ERROR;
Namhyung Kim71ff8242015-05-21 01:03:39 +0900556}
Jiri Olsacdd059d2012-10-27 23:18:32 +0200557
Namhyung Kim71ff8242015-05-21 01:03:39 +0900558/**
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900559 * dso__data_get_fd - Get dso's data file descriptor
Namhyung Kim71ff8242015-05-21 01:03:39 +0900560 * @dso: dso object
561 * @machine: machine object
562 *
563 * External interface to find dso's file, open it and
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900564 * returns file descriptor. It should be paired with
565 * dso__data_put_fd() if it returns non-negative value.
Namhyung Kim71ff8242015-05-21 01:03:39 +0900566 */
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900567int dso__data_get_fd(struct dso *dso, struct machine *machine)
Namhyung Kim71ff8242015-05-21 01:03:39 +0900568{
569 if (dso->data.status == DSO_DATA_STATUS_ERROR)
570 return -1;
571
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900572 if (pthread_mutex_lock(&dso__data_open_lock) < 0)
573 return -1;
574
Namhyung Kim71ff8242015-05-21 01:03:39 +0900575 try_to_open_dso(dso, machine);
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900576
577 if (dso->data.fd < 0)
578 pthread_mutex_unlock(&dso__data_open_lock);
Namhyung Kim71ff8242015-05-21 01:03:39 +0900579
Adrian Hunterc27697d2014-07-22 16:17:18 +0300580 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200581}
582
Namhyung Kim4bb11d02015-05-21 01:03:41 +0900583void dso__data_put_fd(struct dso *dso __maybe_unused)
584{
585 pthread_mutex_unlock(&dso__data_open_lock);
586}
587
Adrian Hunter288be942014-07-22 16:17:19 +0300588bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
589{
590 u32 flag = 1 << by;
591
592 if (dso->data.status_seen & flag)
593 return true;
594
595 dso->data.status_seen |= flag;
596
597 return false;
598}
599
Jiri Olsacdd059d2012-10-27 23:18:32 +0200600static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900601dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200602{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900603 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200604 struct rb_node *next = rb_first(root);
605
Namhyung Kim8e67b722015-05-18 09:30:41 +0900606 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200607 while (next) {
608 struct dso_cache *cache;
609
610 cache = rb_entry(next, struct dso_cache, rb_node);
611 next = rb_next(&cache->rb_node);
612 rb_erase(&cache->rb_node, root);
613 free(cache);
614 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900615 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200616}
617
Namhyung Kim8e67b722015-05-18 09:30:41 +0900618static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200619{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900620 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300621 struct rb_node * const *p = &root->rb_node;
622 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200623 struct dso_cache *cache;
624
625 while (*p != NULL) {
626 u64 end;
627
628 parent = *p;
629 cache = rb_entry(parent, struct dso_cache, rb_node);
630 end = cache->offset + DSO__DATA_CACHE_SIZE;
631
632 if (offset < cache->offset)
633 p = &(*p)->rb_left;
634 else if (offset >= end)
635 p = &(*p)->rb_right;
636 else
637 return cache;
638 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900639
Jiri Olsacdd059d2012-10-27 23:18:32 +0200640 return NULL;
641}
642
Namhyung Kim8e67b722015-05-18 09:30:41 +0900643static struct dso_cache *
644dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200645{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900646 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200647 struct rb_node **p = &root->rb_node;
648 struct rb_node *parent = NULL;
649 struct dso_cache *cache;
650 u64 offset = new->offset;
651
Namhyung Kim8e67b722015-05-18 09:30:41 +0900652 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200653 while (*p != NULL) {
654 u64 end;
655
656 parent = *p;
657 cache = rb_entry(parent, struct dso_cache, rb_node);
658 end = cache->offset + DSO__DATA_CACHE_SIZE;
659
660 if (offset < cache->offset)
661 p = &(*p)->rb_left;
662 else if (offset >= end)
663 p = &(*p)->rb_right;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900664 else
665 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200666 }
667
668 rb_link_node(&new->rb_node, parent, p);
669 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900670
671 cache = NULL;
672out:
673 pthread_mutex_unlock(&dso->lock);
674 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200675}
676
677static ssize_t
678dso_cache__memcpy(struct dso_cache *cache, u64 offset,
679 u8 *data, u64 size)
680{
681 u64 cache_offset = offset - cache->offset;
682 u64 cache_size = min(cache->size - cache_offset, size);
683
684 memcpy(data, cache->data + cache_offset, cache_size);
685 return cache_size;
686}
687
688static ssize_t
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900689dso_cache__read(struct dso *dso, struct machine *machine,
690 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200691{
692 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900693 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200694 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200695
696 do {
697 u64 cache_offset;
698
Jiri Olsacdd059d2012-10-27 23:18:32 +0200699 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
700 if (!cache)
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900701 return -ENOMEM;
702
703 pthread_mutex_lock(&dso__data_open_lock);
704
705 /*
706 * dso->data.fd might be closed if other thread opened another
707 * file (dso) due to open file limit (RLIMIT_NOFILE).
708 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900709 try_to_open_dso(dso, machine);
710
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900711 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900712 ret = -errno;
713 dso->data.status = DSO_DATA_STATUS_ERROR;
714 break;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900715 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200716
717 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200718
Namhyung Kimc52686f2015-01-29 17:02:01 -0300719 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200720 if (ret <= 0)
721 break;
722
723 cache->offset = cache_offset;
724 cache->size = ret;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900725 } while (0);
726
727 pthread_mutex_unlock(&dso__data_open_lock);
728
729 if (ret > 0) {
Namhyung Kim8e67b722015-05-18 09:30:41 +0900730 old = dso_cache__insert(dso, cache);
731 if (old) {
732 /* we lose the race */
733 free(cache);
734 cache = old;
735 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200736
737 ret = dso_cache__memcpy(cache, offset, data, size);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900738 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200739
740 if (ret <= 0)
741 free(cache);
742
Jiri Olsacdd059d2012-10-27 23:18:32 +0200743 return ret;
744}
745
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900746static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
747 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200748{
749 struct dso_cache *cache;
750
Namhyung Kim8e67b722015-05-18 09:30:41 +0900751 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200752 if (cache)
753 return dso_cache__memcpy(cache, offset, data, size);
754 else
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900755 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200756}
757
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200758/*
759 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
760 * in the rb_tree. Any read to already cached data is served
761 * by cached data.
762 */
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900763static ssize_t cached_read(struct dso *dso, struct machine *machine,
764 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200765{
766 ssize_t r = 0;
767 u8 *p = data;
768
769 do {
770 ssize_t ret;
771
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900772 ret = dso_cache_read(dso, machine, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200773 if (ret < 0)
774 return ret;
775
776 /* Reached EOF, return what we have. */
777 if (!ret)
778 break;
779
780 BUG_ON(ret > size);
781
782 r += ret;
783 p += ret;
784 offset += ret;
785 size -= ret;
786
787 } while (size);
788
789 return r;
790}
791
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900792static int data_file_size(struct dso *dso, struct machine *machine)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200793{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900794 int ret = 0;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200795 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000796 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200797
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900798 if (dso->data.file_size)
799 return 0;
800
Namhyung Kim71ff8242015-05-21 01:03:39 +0900801 if (dso->data.status == DSO_DATA_STATUS_ERROR)
802 return -1;
803
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900804 pthread_mutex_lock(&dso__data_open_lock);
805
806 /*
807 * dso->data.fd might be closed if other thread opened another
808 * file (dso) due to open file limit (RLIMIT_NOFILE).
809 */
Namhyung Kim71ff8242015-05-21 01:03:39 +0900810 try_to_open_dso(dso, machine);
811
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900812 if (dso->data.fd < 0) {
Namhyung Kim71ff8242015-05-21 01:03:39 +0900813 ret = -errno;
814 dso->data.status = DSO_DATA_STATUS_ERROR;
815 goto out;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200816 }
817
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900818 if (fstat(dso->data.fd, &st) < 0) {
819 ret = -errno;
820 pr_err("dso cache fstat failed: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300821 str_error_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900822 dso->data.status = DSO_DATA_STATUS_ERROR;
823 goto out;
824 }
825 dso->data.file_size = st.st_size;
826
827out:
828 pthread_mutex_unlock(&dso__data_open_lock);
829 return ret;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200830}
831
Adrian Hunter6d363452014-07-22 16:17:35 +0300832/**
833 * dso__data_size - Return dso data size
834 * @dso: dso object
835 * @machine: machine object
836 *
837 * Return: dso data size
838 */
839off_t dso__data_size(struct dso *dso, struct machine *machine)
840{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900841 if (data_file_size(dso, machine))
Adrian Hunter6d363452014-07-22 16:17:35 +0300842 return -1;
843
844 /* For now just estimate dso data size is close to file size */
845 return dso->data.file_size;
846}
847
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900848static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
849 u64 offset, u8 *data, ssize_t size)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200850{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900851 if (data_file_size(dso, machine))
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200852 return -1;
853
854 /* Check the offset sanity. */
855 if (offset > dso->data.file_size)
856 return -1;
857
858 if (offset + size < offset)
859 return -1;
860
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900861 return cached_read(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200862}
863
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200864/**
865 * dso__data_read_offset - Read data from dso file offset
866 * @dso: dso object
867 * @machine: machine object
868 * @offset: file offset
869 * @data: buffer to store data
870 * @size: size of the @data buffer
871 *
872 * External interface to read data from dso file offset. Open
873 * dso data file and use cached_read to get the data.
874 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200875ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
876 u64 offset, u8 *data, ssize_t size)
877{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900878 if (dso->data.status == DSO_DATA_STATUS_ERROR)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200879 return -1;
880
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900881 return data_read_offset(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200882}
883
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200884/**
885 * dso__data_read_addr - Read data from dso address
886 * @dso: dso object
887 * @machine: machine object
888 * @add: virtual memory address
889 * @data: buffer to store data
890 * @size: size of the @data buffer
891 *
892 * External interface to read data from dso address.
893 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200894ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
895 struct machine *machine, u64 addr,
896 u8 *data, ssize_t size)
897{
898 u64 offset = map->map_ip(map, addr);
899 return dso__data_read_offset(dso, machine, offset, data, size);
900}
901
902struct map *dso__new_map(const char *name)
903{
904 struct map *map = NULL;
905 struct dso *dso = dso__new(name);
906
907 if (dso)
908 map = map__new2(0, dso, MAP__FUNCTION);
909
910 return map;
911}
912
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300913struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
914 const char *short_name, int dso_type)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200915{
916 /*
917 * The kernel dso could be created by build_id processing.
918 */
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300919 struct dso *dso = machine__findnew_dso(machine, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200920
921 /*
922 * We need to run this in all cases, since during the build_id
923 * processing we had no idea this was the kernel dso.
924 */
925 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300926 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200927 dso->kernel = dso_type;
928 }
929
930 return dso;
931}
932
Waiman Long4598a0a2014-09-30 13:36:15 -0400933/*
934 * Find a matching entry and/or link current entry to RB tree.
935 * Either one of the dso or name parameter must be non-NULL or the
936 * function will not work.
937 */
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300938static struct dso *__dso__findlink_by_longname(struct rb_root *root,
939 struct dso *dso, const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400940{
941 struct rb_node **p = &root->rb_node;
942 struct rb_node *parent = NULL;
943
944 if (!name)
945 name = dso->long_name;
946 /*
947 * Find node with the matching name
948 */
949 while (*p) {
950 struct dso *this = rb_entry(*p, struct dso, rb_node);
951 int rc = strcmp(name, this->long_name);
952
953 parent = *p;
954 if (rc == 0) {
955 /*
956 * In case the new DSO is a duplicate of an existing
Masahiro Yamada0f5e1552017-02-27 14:28:52 -0800957 * one, print a one-time warning & put the new entry
Waiman Long4598a0a2014-09-30 13:36:15 -0400958 * at the end of the list of duplicates.
959 */
960 if (!dso || (dso == this))
961 return this; /* Find matching dso */
962 /*
963 * The core kernel DSOs may have duplicated long name.
964 * In this case, the short name should be different.
965 * Comparing the short names to differentiate the DSOs.
966 */
967 rc = strcmp(dso->short_name, this->short_name);
968 if (rc == 0) {
969 pr_err("Duplicated dso name: %s\n", name);
970 return NULL;
971 }
972 }
973 if (rc < 0)
974 p = &parent->rb_left;
975 else
976 p = &parent->rb_right;
977 }
978 if (dso) {
979 /* Add new node and rebalance tree */
980 rb_link_node(&dso->rb_node, parent, p);
981 rb_insert_color(&dso->rb_node, root);
Adrian Huntere266a752015-11-13 11:48:30 +0200982 dso->root = root;
Waiman Long4598a0a2014-09-30 13:36:15 -0400983 }
984 return NULL;
985}
986
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300987static inline struct dso *__dso__find_by_longname(struct rb_root *root,
988 const char *name)
Waiman Long4598a0a2014-09-30 13:36:15 -0400989{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300990 return __dso__findlink_by_longname(root, NULL, name);
Waiman Long4598a0a2014-09-30 13:36:15 -0400991}
992
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300993void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200994{
Adrian Huntere266a752015-11-13 11:48:30 +0200995 struct rb_root *root = dso->root;
996
Jiri Olsacdd059d2012-10-27 23:18:32 +0200997 if (name == NULL)
998 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300999
1000 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001001 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001002
Adrian Huntere266a752015-11-13 11:48:30 +02001003 if (root) {
1004 rb_erase(&dso->rb_node, root);
1005 /*
1006 * __dso__findlink_by_longname() isn't guaranteed to add it
1007 * back, so a clean removal is required here.
1008 */
1009 RB_CLEAR_NODE(&dso->rb_node);
1010 dso->root = NULL;
1011 }
1012
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001013 dso->long_name = name;
1014 dso->long_name_len = strlen(name);
1015 dso->long_name_allocated = name_allocated;
Adrian Huntere266a752015-11-13 11:48:30 +02001016
1017 if (root)
1018 __dso__findlink_by_longname(root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001019}
1020
Adrian Hunter58a98c92013-12-10 11:11:46 -03001021void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001022{
1023 if (name == NULL)
1024 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -03001025
1026 if (dso->short_name_allocated)
1027 free((char *)dso->short_name);
1028
1029 dso->short_name = name;
1030 dso->short_name_len = strlen(name);
1031 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001032}
1033
1034static void dso__set_basename(struct dso *dso)
1035{
Stephane Eranianac5e7f82013-12-05 19:26:42 +01001036 /*
1037 * basename() may modify path buffer, so we must pass
1038 * a copy.
1039 */
1040 char *base, *lname = strdup(dso->long_name);
1041
1042 if (!lname)
1043 return;
1044
1045 /*
1046 * basename() may return a pointer to internal
1047 * storage which is reused in subsequent calls
1048 * so copy the result.
1049 */
1050 base = strdup(basename(lname));
1051
1052 free(lname);
1053
1054 if (!base)
1055 return;
1056
1057 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001058}
1059
1060int dso__name_len(const struct dso *dso)
1061{
1062 if (!dso)
1063 return strlen("[unknown]");
Namhyung Kimbb963e12017-02-17 17:17:38 +09001064 if (verbose > 0)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001065 return dso->long_name_len;
1066
1067 return dso->short_name_len;
1068}
1069
1070bool dso__loaded(const struct dso *dso, enum map_type type)
1071{
1072 return dso->loaded & (1 << type);
1073}
1074
1075bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
1076{
1077 return dso->sorted_by_name & (1 << type);
1078}
1079
1080void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
1081{
1082 dso->sorted_by_name |= (1 << type);
1083}
1084
1085struct dso *dso__new(const char *name)
1086{
1087 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1088
1089 if (dso != NULL) {
1090 int i;
1091 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001092 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -03001093 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001094 for (i = 0; i < MAP__NR_TYPES; ++i)
1095 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +02001096 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001097 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +03001098 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001099 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001100 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001101 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001102 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001103 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001104 dso->sorted_by_name = 0;
1105 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +09001106 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +02001107 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001108 dso->kernel = DSO_TYPE_USER;
1109 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -04001110 RB_CLEAR_NODE(&dso->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +02001111 dso->root = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001112 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +02001113 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001114 pthread_mutex_init(&dso->lock, NULL);
Elena Reshetova71008102017-02-21 17:34:58 +02001115 refcount_set(&dso->refcnt, 1);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001116 }
1117
1118 return dso;
1119}
1120
1121void dso__delete(struct dso *dso)
1122{
1123 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -04001124
1125 if (!RB_EMPTY_NODE(&dso->rb_node))
1126 pr_err("DSO %s is still in rbtree when being deleted!\n",
1127 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001128 for (i = 0; i < MAP__NR_TYPES; ++i)
1129 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001130
1131 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001132 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001133 dso->short_name_allocated = false;
1134 }
1135
1136 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001137 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001138 dso->long_name_allocated = false;
1139 }
1140
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001141 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +03001142 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +09001143 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +02001144 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001145 zfree(&dso->symsrc_filename);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001146 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001147 free(dso);
1148}
1149
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001150struct dso *dso__get(struct dso *dso)
1151{
1152 if (dso)
Elena Reshetova71008102017-02-21 17:34:58 +02001153 refcount_inc(&dso->refcnt);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001154 return dso;
1155}
1156
1157void dso__put(struct dso *dso)
1158{
Elena Reshetova71008102017-02-21 17:34:58 +02001159 if (dso && refcount_dec_and_test(&dso->refcnt))
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001160 dso__delete(dso);
1161}
1162
Jiri Olsacdd059d2012-10-27 23:18:32 +02001163void dso__set_build_id(struct dso *dso, void *build_id)
1164{
1165 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1166 dso->has_build_id = 1;
1167}
1168
1169bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1170{
1171 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1172}
1173
1174void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1175{
1176 char path[PATH_MAX];
1177
1178 if (machine__is_default_guest(machine))
1179 return;
1180 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1181 if (sysfs__read_build_id(path, dso->build_id,
1182 sizeof(dso->build_id)) == 0)
1183 dso->has_build_id = true;
1184}
1185
1186int dso__kernel_module_get_build_id(struct dso *dso,
1187 const char *root_dir)
1188{
1189 char filename[PATH_MAX];
1190 /*
1191 * kernel module short names are of the form "[module]" and
1192 * we need just "module" here.
1193 */
1194 const char *name = dso->short_name + 1;
1195
1196 snprintf(filename, sizeof(filename),
1197 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1198 root_dir, (int)strlen(name) - 1, name);
1199
1200 if (sysfs__read_build_id(filename, dso->build_id,
1201 sizeof(dso->build_id)) == 0)
1202 dso->has_build_id = true;
1203
1204 return 0;
1205}
1206
1207bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1208{
1209 bool have_build_id = false;
1210 struct dso *pos;
1211
1212 list_for_each_entry(pos, head, node) {
He Kuang6ae98ba2016-05-12 08:43:11 +00001213 if (with_hits && !pos->hit && !dso__is_vdso(pos))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001214 continue;
1215 if (pos->has_build_id) {
1216 have_build_id = true;
1217 continue;
1218 }
1219 if (filename__read_build_id(pos->long_name, pos->build_id,
1220 sizeof(pos->build_id)) > 0) {
1221 have_build_id = true;
1222 pos->has_build_id = true;
1223 }
1224 }
1225
1226 return have_build_id;
1227}
1228
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001229void __dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001230{
Waiman Long8fa7d872014-09-29 16:07:28 -04001231 list_add_tail(&dso->node, &dsos->head);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001232 __dso__findlink_by_longname(&dsos->root, dso, NULL);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001233 /*
1234 * It is now in the linked list, grab a reference, then garbage collect
1235 * this when needing memory, by looking at LRU dso instances in the
1236 * list with atomic_read(&dso->refcnt) == 1, i.e. no references
1237 * anywhere besides the one for the list, do, under a lock for the
1238 * list: remove it from the list, then a dso__put(), that probably will
1239 * be the last and will then call dso__delete(), end of life.
1240 *
1241 * That, or at the end of the 'struct machine' lifetime, when all
1242 * 'struct dso' instances will be removed from the list, in
1243 * dsos__exit(), if they have no other reference from some other data
1244 * structure.
1245 *
1246 * E.g.: after processing a 'perf.data' file and storing references
1247 * to objects instantiated while processing events, we will have
1248 * references to the 'thread', 'map', 'dso' structs all from 'struct
1249 * hist_entry' instances, but we may not need anything not referenced,
1250 * so we might as well call machines__exit()/machines__delete() and
1251 * garbage collect it.
1252 */
1253 dso__get(dso);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001254}
1255
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001256void dsos__add(struct dsos *dsos, struct dso *dso)
1257{
1258 pthread_rwlock_wrlock(&dsos->lock);
1259 __dsos__add(dsos, dso);
1260 pthread_rwlock_unlock(&dsos->lock);
1261}
1262
1263struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001264{
1265 struct dso *pos;
1266
Waiman Longf9ceffb2013-05-09 10:42:48 -04001267 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001268 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001269 if (strcmp(pos->short_name, name) == 0)
1270 return pos;
1271 return NULL;
1272 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001273 return __dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001274}
1275
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001276struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
1277{
1278 struct dso *dso;
1279 pthread_rwlock_rdlock(&dsos->lock);
1280 dso = __dsos__find(dsos, name, cmp_short);
1281 pthread_rwlock_unlock(&dsos->lock);
1282 return dso;
1283}
1284
1285struct dso *__dsos__addnew(struct dsos *dsos, const char *name)
Jiri Olsa701d8d72015-02-12 22:06:09 +01001286{
1287 struct dso *dso = dso__new(name);
1288
1289 if (dso != NULL) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001290 __dsos__add(dsos, dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001291 dso__set_basename(dso);
Masami Hiramatsu82de26a2015-11-18 15:40:31 +09001292 /* Put dso here because __dsos_add already got it */
1293 dso__put(dso);
Jiri Olsa701d8d72015-02-12 22:06:09 +01001294 }
1295 return dso;
1296}
1297
Waiman Long8fa7d872014-09-29 16:07:28 -04001298struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001299{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001300 struct dso *dso = __dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001301
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001302 return dso ? dso : __dsos__addnew(dsos, name);
1303}
1304
1305struct dso *dsos__findnew(struct dsos *dsos, const char *name)
1306{
1307 struct dso *dso;
1308 pthread_rwlock_wrlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001309 dso = dso__get(__dsos__findnew(dsos, name));
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001310 pthread_rwlock_unlock(&dsos->lock);
1311 return dso;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001312}
1313
1314size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001315 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001316{
1317 struct dso *pos;
1318 size_t ret = 0;
1319
1320 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001321 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001322 continue;
1323 ret += dso__fprintf_buildid(pos, fp);
1324 ret += fprintf(fp, " %s\n", pos->long_name);
1325 }
1326 return ret;
1327}
1328
1329size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1330{
1331 struct dso *pos;
1332 size_t ret = 0;
1333
1334 list_for_each_entry(pos, head, node) {
1335 int i;
1336 for (i = 0; i < MAP__NR_TYPES; ++i)
1337 ret += dso__fprintf(pos, i, fp);
1338 }
1339
1340 return ret;
1341}
1342
1343size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1344{
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001345 char sbuild_id[SBUILD_ID_SIZE];
Jiri Olsacdd059d2012-10-27 23:18:32 +02001346
1347 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1348 return fprintf(fp, "%s", sbuild_id);
1349}
1350
1351size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1352{
1353 struct rb_node *nd;
1354 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1355
1356 if (dso->short_name != dso->long_name)
1357 ret += fprintf(fp, "%s, ", dso->long_name);
1358 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001359 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001360 ret += dso__fprintf_buildid(dso, fp);
1361 ret += fprintf(fp, ")\n");
1362 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1363 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1364 ret += symbol__fprintf(pos, fp);
1365 }
1366
1367 return ret;
1368}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001369
1370enum dso_type dso__type(struct dso *dso, struct machine *machine)
1371{
1372 int fd;
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001373 enum dso_type type = DSO__TYPE_UNKNOWN;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001374
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001375 fd = dso__data_get_fd(dso, machine);
1376 if (fd >= 0) {
1377 type = dso__type_fd(fd);
1378 dso__data_put_fd(dso);
1379 }
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001380
Namhyung Kim4bb11d02015-05-21 01:03:41 +09001381 return type;
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001382}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001383
1384int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1385{
1386 int idx, errnum = dso->load_errno;
1387 /*
1388 * This must have a same ordering as the enum dso_load_errno.
1389 */
1390 static const char *dso_load__error_str[] = {
1391 "Internal tools/perf/ library error",
1392 "Invalid ELF file",
1393 "Can not read build id",
1394 "Mismatching build id",
1395 "Decompression failure",
1396 };
1397
1398 BUG_ON(buflen == 0);
1399
1400 if (errnum >= 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001401 const char *err = str_error_r(errnum, buf, buflen);
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001402
1403 if (err != buf)
1404 scnprintf(buf, buflen, "%s", err);
1405
1406 return 0;
1407 }
1408
1409 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1410 return -1;
1411
1412 idx = errnum - __DSO_LOAD_ERRNO__START;
1413 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1414 return 0;
1415}