]>
Commit | Line | Data |
---|---|---|
bda6ee4a | 1 | #include <asm/bug.h> |
877a7a11 | 2 | #include <linux/kernel.h> |
c6580451 JO |
3 | #include <sys/time.h> |
4 | #include <sys/resource.h> | |
7a8ef4c4 ACM |
5 | #include <sys/types.h> |
6 | #include <sys/stat.h> | |
7 | #include <unistd.h> | |
a43783ae | 8 | #include <errno.h> |
611f0afe | 9 | #include "compress.h" |
9a3993d4 | 10 | #include "path.h" |
cdd059d7 JO |
11 | #include "symbol.h" |
12 | #include "dso.h" | |
69d2591a | 13 | #include "machine.h" |
cfe9174f | 14 | #include "auxtrace.h" |
cdd059d7 JO |
15 | #include "util.h" |
16 | #include "debug.h" | |
a067558e | 17 | #include "string2.h" |
6ae98ba6 | 18 | #include "vdso.h" |
cdd059d7 | 19 | |
9343e45b MGP |
20 | static const char * const debuglink_paths[] = { |
21 | "%.0s%s", | |
22 | "%s/%s", | |
23 | "%s/.debug/%s", | |
24 | "/usr/lib/debug%s/%s" | |
25 | }; | |
26 | ||
cdd059d7 JO |
27 | char dso__symtab_origin(const struct dso *dso) |
28 | { | |
29 | static const char origin[] = { | |
9cd00941 RRD |
30 | [DSO_BINARY_TYPE__KALLSYMS] = 'k', |
31 | [DSO_BINARY_TYPE__VMLINUX] = 'v', | |
32 | [DSO_BINARY_TYPE__JAVA_JIT] = 'j', | |
33 | [DSO_BINARY_TYPE__DEBUGLINK] = 'l', | |
34 | [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B', | |
35 | [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f', | |
36 | [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u', | |
37 | [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o', | |
38 | [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b', | |
39 | [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd', | |
40 | [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K', | |
c00c48fc | 41 | [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm', |
9cd00941 RRD |
42 | [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g', |
43 | [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G', | |
c00c48fc | 44 | [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M', |
9cd00941 | 45 | [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V', |
cdd059d7 JO |
46 | }; |
47 | ||
48 | if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND) | |
49 | return '!'; | |
50 | return origin[dso->symtab_type]; | |
51 | } | |
52 | ||
ee4e9625 ACM |
53 | int dso__read_binary_type_filename(const struct dso *dso, |
54 | enum dso_binary_type type, | |
55 | char *root_dir, char *filename, size_t size) | |
cdd059d7 | 56 | { |
b5d8bbe8 | 57 | char build_id_hex[SBUILD_ID_SIZE]; |
cdd059d7 | 58 | int ret = 0; |
972f393b | 59 | size_t len; |
cdd059d7 JO |
60 | |
61 | switch (type) { | |
9343e45b MGP |
62 | case DSO_BINARY_TYPE__DEBUGLINK: |
63 | { | |
64 | const char *last_slash; | |
65 | char dso_dir[PATH_MAX]; | |
66 | char symfile[PATH_MAX]; | |
67 | unsigned int i; | |
cdd059d7 | 68 | |
dc6254cf | 69 | len = __symbol__join_symfs(filename, size, dso->long_name); |
9343e45b MGP |
70 | last_slash = filename + len; |
71 | while (last_slash != filename && *last_slash != '/') | |
72 | last_slash--; | |
40356721 | 73 | |
9343e45b MGP |
74 | strncpy(dso_dir, filename, last_slash - filename); |
75 | dso_dir[last_slash-filename] = '\0'; | |
76 | ||
77 | if (!is_regular_file(filename)) { | |
78 | ret = -1; | |
40356721 | 79 | break; |
9343e45b | 80 | } |
40356721 | 81 | |
9343e45b MGP |
82 | ret = filename__read_debuglink(filename, symfile, PATH_MAX); |
83 | if (ret) | |
84 | break; | |
85 | ||
86 | /* Check predefined locations where debug file might reside */ | |
87 | ret = -1; | |
88 | for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) { | |
89 | snprintf(filename, size, | |
90 | debuglink_paths[i], dso_dir, symfile); | |
91 | if (is_regular_file(filename)) { | |
92 | ret = 0; | |
93 | break; | |
94 | } | |
cdd059d7 | 95 | } |
9343e45b | 96 | |
cdd059d7 | 97 | break; |
9343e45b | 98 | } |
cdd059d7 | 99 | case DSO_BINARY_TYPE__BUILD_ID_CACHE: |
a7066709 | 100 | if (dso__build_id_filename(dso, filename, size) == NULL) |
cdd059d7 JO |
101 | ret = -1; |
102 | break; | |
103 | ||
104 | case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: | |
972f393b ACM |
105 | len = __symbol__join_symfs(filename, size, "/usr/lib/debug"); |
106 | snprintf(filename + len, size - len, "%s.debug", dso->long_name); | |
cdd059d7 JO |
107 | break; |
108 | ||
109 | case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: | |
972f393b ACM |
110 | len = __symbol__join_symfs(filename, size, "/usr/lib/debug"); |
111 | snprintf(filename + len, size - len, "%s", dso->long_name); | |
cdd059d7 JO |
112 | break; |
113 | ||
9cd00941 RRD |
114 | case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: |
115 | { | |
bf4414ae | 116 | const char *last_slash; |
9cd00941 RRD |
117 | size_t dir_size; |
118 | ||
119 | last_slash = dso->long_name + dso->long_name_len; | |
120 | while (last_slash != dso->long_name && *last_slash != '/') | |
121 | last_slash--; | |
122 | ||
972f393b | 123 | len = __symbol__join_symfs(filename, size, ""); |
9cd00941 RRD |
124 | dir_size = last_slash - dso->long_name + 2; |
125 | if (dir_size > (size - len)) { | |
126 | ret = -1; | |
127 | break; | |
128 | } | |
7d2a5122 ACM |
129 | len += scnprintf(filename + len, dir_size, "%s", dso->long_name); |
130 | len += scnprintf(filename + len , size - len, ".debug%s", | |
9cd00941 RRD |
131 | last_slash); |
132 | break; | |
133 | } | |
134 | ||
cdd059d7 JO |
135 | case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: |
136 | if (!dso->has_build_id) { | |
137 | ret = -1; | |
138 | break; | |
139 | } | |
140 | ||
141 | build_id__sprintf(dso->build_id, | |
142 | sizeof(dso->build_id), | |
143 | build_id_hex); | |
972f393b ACM |
144 | len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/"); |
145 | snprintf(filename + len, size - len, "%.2s/%s.debug", | |
146 | build_id_hex, build_id_hex + 2); | |
cdd059d7 JO |
147 | break; |
148 | ||
39b12f78 AH |
149 | case DSO_BINARY_TYPE__VMLINUX: |
150 | case DSO_BINARY_TYPE__GUEST_VMLINUX: | |
cdd059d7 | 151 | case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: |
972f393b | 152 | __symbol__join_symfs(filename, size, dso->long_name); |
cdd059d7 JO |
153 | break; |
154 | ||
155 | case DSO_BINARY_TYPE__GUEST_KMODULE: | |
c00c48fc | 156 | case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: |
972f393b ACM |
157 | path__join3(filename, size, symbol_conf.symfs, |
158 | root_dir, dso->long_name); | |
cdd059d7 JO |
159 | break; |
160 | ||
161 | case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: | |
c00c48fc | 162 | case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: |
972f393b | 163 | __symbol__join_symfs(filename, size, dso->long_name); |
cdd059d7 JO |
164 | break; |
165 | ||
8e0cf965 AH |
166 | case DSO_BINARY_TYPE__KCORE: |
167 | case DSO_BINARY_TYPE__GUEST_KCORE: | |
7d2a5122 | 168 | snprintf(filename, size, "%s", dso->long_name); |
8e0cf965 AH |
169 | break; |
170 | ||
cdd059d7 JO |
171 | default: |
172 | case DSO_BINARY_TYPE__KALLSYMS: | |
cdd059d7 | 173 | case DSO_BINARY_TYPE__GUEST_KALLSYMS: |
cdd059d7 JO |
174 | case DSO_BINARY_TYPE__JAVA_JIT: |
175 | case DSO_BINARY_TYPE__NOT_FOUND: | |
176 | ret = -1; | |
177 | break; | |
178 | } | |
179 | ||
180 | return ret; | |
181 | } | |
182 | ||
c00c48fc NK |
183 | static const struct { |
184 | const char *fmt; | |
185 | int (*decompress)(const char *input, int output); | |
186 | } compressions[] = { | |
e92ce12e NK |
187 | #ifdef HAVE_ZLIB_SUPPORT |
188 | { "gz", gzip_decompress_to_file }, | |
80a32e5b JO |
189 | #endif |
190 | #ifdef HAVE_LZMA_SUPPORT | |
191 | { "xz", lzma_decompress_to_file }, | |
e92ce12e NK |
192 | #endif |
193 | { NULL, NULL }, | |
c00c48fc NK |
194 | }; |
195 | ||
196 | bool is_supported_compression(const char *ext) | |
197 | { | |
198 | unsigned i; | |
199 | ||
200 | for (i = 0; compressions[i].fmt; i++) { | |
201 | if (!strcmp(ext, compressions[i].fmt)) | |
202 | return true; | |
203 | } | |
204 | return false; | |
205 | } | |
206 | ||
1f121b03 | 207 | bool is_kernel_module(const char *pathname, int cpumode) |
c00c48fc | 208 | { |
8dee9ff1 | 209 | struct kmod_path m; |
1f121b03 WN |
210 | int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK; |
211 | ||
212 | WARN_ONCE(mode != cpumode, | |
213 | "Internal error: passing unmasked cpumode (%x) to is_kernel_module", | |
214 | cpumode); | |
215 | ||
216 | switch (mode) { | |
217 | case PERF_RECORD_MISC_USER: | |
218 | case PERF_RECORD_MISC_HYPERVISOR: | |
219 | case PERF_RECORD_MISC_GUEST_USER: | |
220 | return false; | |
221 | /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */ | |
222 | default: | |
223 | if (kmod_path__parse(&m, pathname)) { | |
224 | pr_err("Failed to check whether %s is a kernel module or not. Assume it is.", | |
225 | pathname); | |
226 | return true; | |
227 | } | |
228 | } | |
c00c48fc | 229 | |
8dee9ff1 | 230 | return m.kmod; |
c00c48fc NK |
231 | } |
232 | ||
233 | bool decompress_to_file(const char *ext, const char *filename, int output_fd) | |
234 | { | |
235 | unsigned i; | |
236 | ||
237 | for (i = 0; compressions[i].fmt; i++) { | |
238 | if (!strcmp(ext, compressions[i].fmt)) | |
239 | return !compressions[i].decompress(filename, | |
240 | output_fd); | |
241 | } | |
242 | return false; | |
243 | } | |
244 | ||
245 | bool dso__needs_decompress(struct dso *dso) | |
246 | { | |
247 | return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP || | |
248 | dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP; | |
249 | } | |
250 | ||
3c8a67f5 JO |
251 | /* |
252 | * Parses kernel module specified in @path and updates | |
253 | * @m argument like: | |
254 | * | |
255 | * @comp - true if @path contains supported compression suffix, | |
256 | * false otherwise | |
257 | * @kmod - true if @path contains '.ko' suffix in right position, | |
258 | * false otherwise | |
259 | * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name | |
260 | * of the kernel module without suffixes, otherwise strudup-ed | |
261 | * base name of @path | |
262 | * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string | |
263 | * the compression suffix | |
264 | * | |
265 | * Returns 0 if there's no strdup error, -ENOMEM otherwise. | |
266 | */ | |
267 | int __kmod_path__parse(struct kmod_path *m, const char *path, | |
268 | bool alloc_name, bool alloc_ext) | |
269 | { | |
270 | const char *name = strrchr(path, '/'); | |
271 | const char *ext = strrchr(path, '.'); | |
1f121b03 | 272 | bool is_simple_name = false; |
3c8a67f5 JO |
273 | |
274 | memset(m, 0x0, sizeof(*m)); | |
275 | name = name ? name + 1 : path; | |
276 | ||
1f121b03 WN |
277 | /* |
278 | * '.' is also a valid character for module name. For example: | |
279 | * [aaa.bbb] is a valid module name. '[' should have higher | |
280 | * priority than '.ko' suffix. | |
281 | * | |
282 | * The kernel names are from machine__mmap_name. Such | |
283 | * name should belong to kernel itself, not kernel module. | |
284 | */ | |
285 | if (name[0] == '[') { | |
286 | is_simple_name = true; | |
287 | if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) || | |
288 | (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) || | |
289 | (strncmp(name, "[vdso]", 6) == 0) || | |
290 | (strncmp(name, "[vsyscall]", 10) == 0)) { | |
291 | m->kmod = false; | |
292 | ||
293 | } else | |
294 | m->kmod = true; | |
295 | } | |
296 | ||
3c8a67f5 | 297 | /* No extension, just return name. */ |
1f121b03 | 298 | if ((ext == NULL) || is_simple_name) { |
3c8a67f5 JO |
299 | if (alloc_name) { |
300 | m->name = strdup(name); | |
301 | return m->name ? 0 : -ENOMEM; | |
302 | } | |
303 | return 0; | |
304 | } | |
305 | ||
306 | if (is_supported_compression(ext + 1)) { | |
307 | m->comp = true; | |
308 | ext -= 3; | |
309 | } | |
310 | ||
311 | /* Check .ko extension only if there's enough name left. */ | |
312 | if (ext > name) | |
313 | m->kmod = !strncmp(ext, ".ko", 3); | |
314 | ||
315 | if (alloc_name) { | |
316 | if (m->kmod) { | |
317 | if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1) | |
318 | return -ENOMEM; | |
319 | } else { | |
320 | if (asprintf(&m->name, "%s", name) == -1) | |
321 | return -ENOMEM; | |
322 | } | |
323 | ||
324 | strxfrchar(m->name, '-', '_'); | |
325 | } | |
326 | ||
327 | if (alloc_ext && m->comp) { | |
328 | m->ext = strdup(ext + 4); | |
329 | if (!m->ext) { | |
330 | free((void *) m->name); | |
331 | return -ENOMEM; | |
332 | } | |
333 | } | |
334 | ||
335 | return 0; | |
336 | } | |
337 | ||
eba5102d | 338 | /* |
bda6ee4a | 339 | * Global list of open DSOs and the counter. |
eba5102d JO |
340 | */ |
341 | static LIST_HEAD(dso__data_open); | |
bda6ee4a | 342 | static long dso__data_open_cnt; |
33bdedce | 343 | static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER; |
eba5102d JO |
344 | |
345 | static void dso__list_add(struct dso *dso) | |
346 | { | |
347 | list_add_tail(&dso->data.open_entry, &dso__data_open); | |
bda6ee4a | 348 | dso__data_open_cnt++; |
eba5102d JO |
349 | } |
350 | ||
351 | static void dso__list_del(struct dso *dso) | |
352 | { | |
353 | list_del(&dso->data.open_entry); | |
bda6ee4a JO |
354 | WARN_ONCE(dso__data_open_cnt <= 0, |
355 | "DSO data fd counter out of bounds."); | |
356 | dso__data_open_cnt--; | |
eba5102d JO |
357 | } |
358 | ||
a08cae03 JO |
359 | static void close_first_dso(void); |
360 | ||
361 | static int do_open(char *name) | |
362 | { | |
363 | int fd; | |
6e81c74c | 364 | char sbuf[STRERR_BUFSIZE]; |
a08cae03 JO |
365 | |
366 | do { | |
367 | fd = open(name, O_RDONLY); | |
368 | if (fd >= 0) | |
369 | return fd; | |
370 | ||
a3c0cc2a | 371 | pr_debug("dso open failed: %s\n", |
c8b5f2c9 | 372 | str_error_r(errno, sbuf, sizeof(sbuf))); |
a08cae03 JO |
373 | if (!dso__data_open_cnt || errno != EMFILE) |
374 | break; | |
375 | ||
376 | close_first_dso(); | |
377 | } while (1); | |
378 | ||
379 | return -1; | |
380 | } | |
381 | ||
eba5102d | 382 | static int __open_dso(struct dso *dso, struct machine *machine) |
cdd059d7 | 383 | { |
cdd059d7 | 384 | int fd; |
ee4e9625 ACM |
385 | char *root_dir = (char *)""; |
386 | char *name = malloc(PATH_MAX); | |
cdd059d7 | 387 | |
cdd059d7 JO |
388 | if (!name) |
389 | return -ENOMEM; | |
390 | ||
391 | if (machine) | |
392 | root_dir = machine->root_dir; | |
393 | ||
5f70619d | 394 | if (dso__read_binary_type_filename(dso, dso->binary_type, |
ee4e9625 | 395 | root_dir, name, PATH_MAX)) { |
cdd059d7 JO |
396 | free(name); |
397 | return -EINVAL; | |
398 | } | |
399 | ||
3c028a0c JO |
400 | if (!is_regular_file(name)) |
401 | return -EINVAL; | |
402 | ||
a08cae03 | 403 | fd = do_open(name); |
cdd059d7 JO |
404 | free(name); |
405 | return fd; | |
406 | } | |
407 | ||
c6580451 JO |
408 | static void check_data_close(void); |
409 | ||
c1f9aa0a JO |
410 | /** |
411 | * dso_close - Open DSO data file | |
412 | * @dso: dso object | |
413 | * | |
414 | * Open @dso's data file descriptor and updates | |
415 | * list/count of open DSO objects. | |
416 | */ | |
eba5102d JO |
417 | static int open_dso(struct dso *dso, struct machine *machine) |
418 | { | |
419 | int fd = __open_dso(dso, machine); | |
420 | ||
a6f6ae99 | 421 | if (fd >= 0) { |
eba5102d | 422 | dso__list_add(dso); |
c6580451 JO |
423 | /* |
424 | * Check if we crossed the allowed number | |
425 | * of opened DSOs and close one if needed. | |
426 | */ | |
427 | check_data_close(); | |
428 | } | |
eba5102d JO |
429 | |
430 | return fd; | |
431 | } | |
432 | ||
433 | static void close_data_fd(struct dso *dso) | |
53fa8eaa JO |
434 | { |
435 | if (dso->data.fd >= 0) { | |
436 | close(dso->data.fd); | |
437 | dso->data.fd = -1; | |
c3fbd2a6 | 438 | dso->data.file_size = 0; |
eba5102d | 439 | dso__list_del(dso); |
53fa8eaa JO |
440 | } |
441 | } | |
442 | ||
c1f9aa0a JO |
443 | /** |
444 | * dso_close - Close DSO data file | |
445 | * @dso: dso object | |
446 | * | |
447 | * Close @dso's data file descriptor and updates | |
448 | * list/count of open DSO objects. | |
449 | */ | |
eba5102d JO |
450 | static void close_dso(struct dso *dso) |
451 | { | |
452 | close_data_fd(dso); | |
453 | } | |
454 | ||
c6580451 JO |
455 | static void close_first_dso(void) |
456 | { | |
457 | struct dso *dso; | |
458 | ||
459 | dso = list_first_entry(&dso__data_open, struct dso, data.open_entry); | |
460 | close_dso(dso); | |
461 | } | |
462 | ||
463 | static rlim_t get_fd_limit(void) | |
464 | { | |
465 | struct rlimit l; | |
466 | rlim_t limit = 0; | |
467 | ||
468 | /* Allow half of the current open fd limit. */ | |
469 | if (getrlimit(RLIMIT_NOFILE, &l) == 0) { | |
470 | if (l.rlim_cur == RLIM_INFINITY) | |
471 | limit = l.rlim_cur; | |
472 | else | |
473 | limit = l.rlim_cur / 2; | |
474 | } else { | |
475 | pr_err("failed to get fd limit\n"); | |
476 | limit = 1; | |
477 | } | |
478 | ||
479 | return limit; | |
480 | } | |
481 | ||
f3069249 JO |
482 | static rlim_t fd_limit; |
483 | ||
484 | /* | |
485 | * Used only by tests/dso-data.c to reset the environment | |
486 | * for tests. I dont expect we should change this during | |
487 | * standard runtime. | |
488 | */ | |
489 | void reset_fd_limit(void) | |
c6580451 | 490 | { |
f3069249 JO |
491 | fd_limit = 0; |
492 | } | |
c6580451 | 493 | |
f3069249 JO |
494 | static bool may_cache_fd(void) |
495 | { | |
496 | if (!fd_limit) | |
497 | fd_limit = get_fd_limit(); | |
c6580451 | 498 | |
f3069249 | 499 | if (fd_limit == RLIM_INFINITY) |
c6580451 JO |
500 | return true; |
501 | ||
f3069249 | 502 | return fd_limit > (rlim_t) dso__data_open_cnt; |
c6580451 JO |
503 | } |
504 | ||
c1f9aa0a JO |
505 | /* |
506 | * Check and close LRU dso if we crossed allowed limit | |
507 | * for opened dso file descriptors. The limit is half | |
508 | * of the RLIMIT_NOFILE files opened. | |
509 | */ | |
c6580451 JO |
510 | static void check_data_close(void) |
511 | { | |
512 | bool cache_fd = may_cache_fd(); | |
513 | ||
514 | if (!cache_fd) | |
515 | close_first_dso(); | |
516 | } | |
517 | ||
c1f9aa0a JO |
518 | /** |
519 | * dso__data_close - Close DSO data file | |
520 | * @dso: dso object | |
521 | * | |
522 | * External interface to close @dso's data file descriptor. | |
523 | */ | |
eba5102d JO |
524 | void dso__data_close(struct dso *dso) |
525 | { | |
33bdedce | 526 | pthread_mutex_lock(&dso__data_open_lock); |
eba5102d | 527 | close_dso(dso); |
33bdedce | 528 | pthread_mutex_unlock(&dso__data_open_lock); |
eba5102d JO |
529 | } |
530 | ||
71ff824a | 531 | static void try_to_open_dso(struct dso *dso, struct machine *machine) |
cdd059d7 | 532 | { |
631d34b5 | 533 | enum dso_binary_type binary_type_data[] = { |
cdd059d7 JO |
534 | DSO_BINARY_TYPE__BUILD_ID_CACHE, |
535 | DSO_BINARY_TYPE__SYSTEM_PATH_DSO, | |
536 | DSO_BINARY_TYPE__NOT_FOUND, | |
537 | }; | |
538 | int i = 0; | |
539 | ||
53fa8eaa | 540 | if (dso->data.fd >= 0) |
71ff824a | 541 | return; |
53fa8eaa JO |
542 | |
543 | if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) { | |
544 | dso->data.fd = open_dso(dso, machine); | |
c27697d6 | 545 | goto out; |
53fa8eaa | 546 | } |
cdd059d7 JO |
547 | |
548 | do { | |
5f70619d | 549 | dso->binary_type = binary_type_data[i++]; |
cdd059d7 | 550 | |
c27697d6 AH |
551 | dso->data.fd = open_dso(dso, machine); |
552 | if (dso->data.fd >= 0) | |
553 | goto out; | |
cdd059d7 | 554 | |
5f70619d | 555 | } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND); |
c27697d6 AH |
556 | out: |
557 | if (dso->data.fd >= 0) | |
558 | dso->data.status = DSO_DATA_STATUS_OK; | |
559 | else | |
560 | dso->data.status = DSO_DATA_STATUS_ERROR; | |
71ff824a NK |
561 | } |
562 | ||
563 | /** | |
4bb11d01 | 564 | * dso__data_get_fd - Get dso's data file descriptor |
71ff824a NK |
565 | * @dso: dso object |
566 | * @machine: machine object | |
567 | * | |
568 | * External interface to find dso's file, open it and | |
4bb11d01 NK |
569 | * returns file descriptor. It should be paired with |
570 | * dso__data_put_fd() if it returns non-negative value. | |
71ff824a | 571 | */ |
4bb11d01 | 572 | int dso__data_get_fd(struct dso *dso, struct machine *machine) |
71ff824a NK |
573 | { |
574 | if (dso->data.status == DSO_DATA_STATUS_ERROR) | |
575 | return -1; | |
cdd059d7 | 576 | |
4bb11d01 NK |
577 | if (pthread_mutex_lock(&dso__data_open_lock) < 0) |
578 | return -1; | |
579 | ||
71ff824a | 580 | try_to_open_dso(dso, machine); |
4bb11d01 NK |
581 | |
582 | if (dso->data.fd < 0) | |
583 | pthread_mutex_unlock(&dso__data_open_lock); | |
71ff824a | 584 | |
c27697d6 | 585 | return dso->data.fd; |
cdd059d7 JO |
586 | } |
587 | ||
4bb11d01 NK |
588 | void dso__data_put_fd(struct dso *dso __maybe_unused) |
589 | { | |
590 | pthread_mutex_unlock(&dso__data_open_lock); | |
591 | } | |
592 | ||
288be943 AH |
593 | bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by) |
594 | { | |
595 | u32 flag = 1 << by; | |
596 | ||
597 | if (dso->data.status_seen & flag) | |
598 | return true; | |
599 | ||
600 | dso->data.status_seen |= flag; | |
601 | ||
602 | return false; | |
603 | } | |
604 | ||
cdd059d7 | 605 | static void |
8e67b725 | 606 | dso_cache__free(struct dso *dso) |
cdd059d7 | 607 | { |
8e67b725 | 608 | struct rb_root *root = &dso->data.cache; |
cdd059d7 JO |
609 | struct rb_node *next = rb_first(root); |
610 | ||
8e67b725 | 611 | pthread_mutex_lock(&dso->lock); |
cdd059d7 JO |
612 | while (next) { |
613 | struct dso_cache *cache; | |
614 | ||
615 | cache = rb_entry(next, struct dso_cache, rb_node); | |
616 | next = rb_next(&cache->rb_node); | |
617 | rb_erase(&cache->rb_node, root); | |
618 | free(cache); | |
619 | } | |
8e67b725 | 620 | pthread_mutex_unlock(&dso->lock); |
cdd059d7 JO |
621 | } |
622 | ||
8e67b725 | 623 | static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset) |
cdd059d7 | 624 | { |
8e67b725 | 625 | const struct rb_root *root = &dso->data.cache; |
3344996e ACM |
626 | struct rb_node * const *p = &root->rb_node; |
627 | const struct rb_node *parent = NULL; | |
cdd059d7 JO |
628 | struct dso_cache *cache; |
629 | ||
630 | while (*p != NULL) { | |
631 | u64 end; | |
632 | ||
633 | parent = *p; | |
634 | cache = rb_entry(parent, struct dso_cache, rb_node); | |
635 | end = cache->offset + DSO__DATA_CACHE_SIZE; | |
636 | ||
637 | if (offset < cache->offset) | |
638 | p = &(*p)->rb_left; | |
639 | else if (offset >= end) | |
640 | p = &(*p)->rb_right; | |
641 | else | |
642 | return cache; | |
643 | } | |
8e67b725 | 644 | |
cdd059d7 JO |
645 | return NULL; |
646 | } | |
647 | ||
8e67b725 NK |
648 | static struct dso_cache * |
649 | dso_cache__insert(struct dso *dso, struct dso_cache *new) | |
cdd059d7 | 650 | { |
8e67b725 | 651 | struct rb_root *root = &dso->data.cache; |
cdd059d7 JO |
652 | struct rb_node **p = &root->rb_node; |
653 | struct rb_node *parent = NULL; | |
654 | struct dso_cache *cache; | |
655 | u64 offset = new->offset; | |
656 | ||
8e67b725 | 657 | pthread_mutex_lock(&dso->lock); |
cdd059d7 JO |
658 | while (*p != NULL) { |
659 | u64 end; | |
660 | ||
661 | parent = *p; | |
662 | cache = rb_entry(parent, struct dso_cache, rb_node); | |
663 | end = cache->offset + DSO__DATA_CACHE_SIZE; | |
664 | ||
665 | if (offset < cache->offset) | |
666 | p = &(*p)->rb_left; | |
667 | else if (offset >= end) | |
668 | p = &(*p)->rb_right; | |
8e67b725 NK |
669 | else |
670 | goto out; | |
cdd059d7 JO |
671 | } |
672 | ||
673 | rb_link_node(&new->rb_node, parent, p); | |
674 | rb_insert_color(&new->rb_node, root); | |
8e67b725 NK |
675 | |
676 | cache = NULL; | |
677 | out: | |
678 | pthread_mutex_unlock(&dso->lock); | |
679 | return cache; | |
cdd059d7 JO |
680 | } |
681 | ||
682 | static ssize_t | |
683 | dso_cache__memcpy(struct dso_cache *cache, u64 offset, | |
684 | u8 *data, u64 size) | |
685 | { | |
686 | u64 cache_offset = offset - cache->offset; | |
687 | u64 cache_size = min(cache->size - cache_offset, size); | |
688 | ||
689 | memcpy(data, cache->data + cache_offset, cache_size); | |
690 | return cache_size; | |
691 | } | |
692 | ||
693 | static ssize_t | |
33bdedce NK |
694 | dso_cache__read(struct dso *dso, struct machine *machine, |
695 | u64 offset, u8 *data, ssize_t size) | |
cdd059d7 JO |
696 | { |
697 | struct dso_cache *cache; | |
8e67b725 | 698 | struct dso_cache *old; |
cdd059d7 | 699 | ssize_t ret; |
cdd059d7 JO |
700 | |
701 | do { | |
702 | u64 cache_offset; | |
703 | ||
cdd059d7 JO |
704 | cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE); |
705 | if (!cache) | |
33bdedce NK |
706 | return -ENOMEM; |
707 | ||
708 | pthread_mutex_lock(&dso__data_open_lock); | |
709 | ||
710 | /* | |
711 | * dso->data.fd might be closed if other thread opened another | |
712 | * file (dso) due to open file limit (RLIMIT_NOFILE). | |
713 | */ | |
71ff824a NK |
714 | try_to_open_dso(dso, machine); |
715 | ||
33bdedce | 716 | if (dso->data.fd < 0) { |
71ff824a NK |
717 | ret = -errno; |
718 | dso->data.status = DSO_DATA_STATUS_ERROR; | |
719 | break; | |
33bdedce | 720 | } |
cdd059d7 JO |
721 | |
722 | cache_offset = offset & DSO__DATA_CACHE_MASK; | |
cdd059d7 | 723 | |
c52686f9 | 724 | ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset); |
cdd059d7 JO |
725 | if (ret <= 0) |
726 | break; | |
727 | ||
728 | cache->offset = cache_offset; | |
729 | cache->size = ret; | |
33bdedce NK |
730 | } while (0); |
731 | ||
732 | pthread_mutex_unlock(&dso__data_open_lock); | |
733 | ||
734 | if (ret > 0) { | |
8e67b725 NK |
735 | old = dso_cache__insert(dso, cache); |
736 | if (old) { | |
737 | /* we lose the race */ | |
738 | free(cache); | |
739 | cache = old; | |
740 | } | |
cdd059d7 JO |
741 | |
742 | ret = dso_cache__memcpy(cache, offset, data, size); | |
33bdedce | 743 | } |
cdd059d7 JO |
744 | |
745 | if (ret <= 0) | |
746 | free(cache); | |
747 | ||
cdd059d7 JO |
748 | return ret; |
749 | } | |
750 | ||
33bdedce NK |
751 | static ssize_t dso_cache_read(struct dso *dso, struct machine *machine, |
752 | u64 offset, u8 *data, ssize_t size) | |
cdd059d7 JO |
753 | { |
754 | struct dso_cache *cache; | |
755 | ||
8e67b725 | 756 | cache = dso_cache__find(dso, offset); |
cdd059d7 JO |
757 | if (cache) |
758 | return dso_cache__memcpy(cache, offset, data, size); | |
759 | else | |
33bdedce | 760 | return dso_cache__read(dso, machine, offset, data, size); |
cdd059d7 JO |
761 | } |
762 | ||
c1f9aa0a JO |
763 | /* |
764 | * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks | |
765 | * in the rb_tree. Any read to already cached data is served | |
766 | * by cached data. | |
767 | */ | |
33bdedce NK |
768 | static ssize_t cached_read(struct dso *dso, struct machine *machine, |
769 | u64 offset, u8 *data, ssize_t size) | |
cdd059d7 JO |
770 | { |
771 | ssize_t r = 0; | |
772 | u8 *p = data; | |
773 | ||
774 | do { | |
775 | ssize_t ret; | |
776 | ||
33bdedce | 777 | ret = dso_cache_read(dso, machine, offset, p, size); |
cdd059d7 JO |
778 | if (ret < 0) |
779 | return ret; | |
780 | ||
781 | /* Reached EOF, return what we have. */ | |
782 | if (!ret) | |
783 | break; | |
784 | ||
785 | BUG_ON(ret > size); | |
786 | ||
787 | r += ret; | |
788 | p += ret; | |
789 | offset += ret; | |
790 | size -= ret; | |
791 | ||
792 | } while (size); | |
793 | ||
794 | return r; | |
795 | } | |
796 | ||
33bdedce | 797 | static int data_file_size(struct dso *dso, struct machine *machine) |
c3fbd2a6 | 798 | { |
33bdedce | 799 | int ret = 0; |
c3fbd2a6 | 800 | struct stat st; |
6e81c74c | 801 | char sbuf[STRERR_BUFSIZE]; |
c3fbd2a6 | 802 | |
33bdedce NK |
803 | if (dso->data.file_size) |
804 | return 0; | |
805 | ||
71ff824a NK |
806 | if (dso->data.status == DSO_DATA_STATUS_ERROR) |
807 | return -1; | |
808 | ||
33bdedce NK |
809 | pthread_mutex_lock(&dso__data_open_lock); |
810 | ||
811 | /* | |
812 | * dso->data.fd might be closed if other thread opened another | |
813 | * file (dso) due to open file limit (RLIMIT_NOFILE). | |
814 | */ | |
71ff824a NK |
815 | try_to_open_dso(dso, machine); |
816 | ||
33bdedce | 817 | if (dso->data.fd < 0) { |
71ff824a NK |
818 | ret = -errno; |
819 | dso->data.status = DSO_DATA_STATUS_ERROR; | |
820 | goto out; | |
c3fbd2a6 JO |
821 | } |
822 | ||
33bdedce NK |
823 | if (fstat(dso->data.fd, &st) < 0) { |
824 | ret = -errno; | |
825 | pr_err("dso cache fstat failed: %s\n", | |
c8b5f2c9 | 826 | str_error_r(errno, sbuf, sizeof(sbuf))); |
33bdedce NK |
827 | dso->data.status = DSO_DATA_STATUS_ERROR; |
828 | goto out; | |
829 | } | |
830 | dso->data.file_size = st.st_size; | |
831 | ||
832 | out: | |
833 | pthread_mutex_unlock(&dso__data_open_lock); | |
834 | return ret; | |
c3fbd2a6 JO |
835 | } |
836 | ||
6d363459 AH |
837 | /** |
838 | * dso__data_size - Return dso data size | |
839 | * @dso: dso object | |
840 | * @machine: machine object | |
841 | * | |
842 | * Return: dso data size | |
843 | */ | |
844 | off_t dso__data_size(struct dso *dso, struct machine *machine) | |
845 | { | |
33bdedce | 846 | if (data_file_size(dso, machine)) |
6d363459 AH |
847 | return -1; |
848 | ||
849 | /* For now just estimate dso data size is close to file size */ | |
850 | return dso->data.file_size; | |
851 | } | |
852 | ||
33bdedce NK |
853 | static ssize_t data_read_offset(struct dso *dso, struct machine *machine, |
854 | u64 offset, u8 *data, ssize_t size) | |
c3fbd2a6 | 855 | { |
33bdedce | 856 | if (data_file_size(dso, machine)) |
c3fbd2a6 JO |
857 | return -1; |
858 | ||
859 | /* Check the offset sanity. */ | |
860 | if (offset > dso->data.file_size) | |
861 | return -1; | |
862 | ||
863 | if (offset + size < offset) | |
864 | return -1; | |
865 | ||
33bdedce | 866 | return cached_read(dso, machine, offset, data, size); |
c3fbd2a6 JO |
867 | } |
868 | ||
c1f9aa0a JO |
869 | /** |
870 | * dso__data_read_offset - Read data from dso file offset | |
871 | * @dso: dso object | |
872 | * @machine: machine object | |
873 | * @offset: file offset | |
874 | * @data: buffer to store data | |
875 | * @size: size of the @data buffer | |
876 | * | |
877 | * External interface to read data from dso file offset. Open | |
878 | * dso data file and use cached_read to get the data. | |
879 | */ | |
c3fbd2a6 JO |
880 | ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine, |
881 | u64 offset, u8 *data, ssize_t size) | |
882 | { | |
33bdedce | 883 | if (dso->data.status == DSO_DATA_STATUS_ERROR) |
c3fbd2a6 JO |
884 | return -1; |
885 | ||
33bdedce | 886 | return data_read_offset(dso, machine, offset, data, size); |
c3fbd2a6 JO |
887 | } |
888 | ||
c1f9aa0a JO |
889 | /** |
890 | * dso__data_read_addr - Read data from dso address | |
891 | * @dso: dso object | |
892 | * @machine: machine object | |
893 | * @add: virtual memory address | |
894 | * @data: buffer to store data | |
895 | * @size: size of the @data buffer | |
896 | * | |
897 | * External interface to read data from dso address. | |
898 | */ | |
cdd059d7 JO |
899 | ssize_t dso__data_read_addr(struct dso *dso, struct map *map, |
900 | struct machine *machine, u64 addr, | |
901 | u8 *data, ssize_t size) | |
902 | { | |
903 | u64 offset = map->map_ip(map, addr); | |
904 | return dso__data_read_offset(dso, machine, offset, data, size); | |
905 | } | |
906 | ||
907 | struct map *dso__new_map(const char *name) | |
908 | { | |
909 | struct map *map = NULL; | |
910 | struct dso *dso = dso__new(name); | |
911 | ||
912 | if (dso) | |
913 | map = map__new2(0, dso, MAP__FUNCTION); | |
914 | ||
915 | return map; | |
916 | } | |
917 | ||
459ce518 ACM |
918 | struct dso *machine__findnew_kernel(struct machine *machine, const char *name, |
919 | const char *short_name, int dso_type) | |
cdd059d7 JO |
920 | { |
921 | /* | |
922 | * The kernel dso could be created by build_id processing. | |
923 | */ | |
aa7cc2ae | 924 | struct dso *dso = machine__findnew_dso(machine, name); |
cdd059d7 JO |
925 | |
926 | /* | |
927 | * We need to run this in all cases, since during the build_id | |
928 | * processing we had no idea this was the kernel dso. | |
929 | */ | |
930 | if (dso != NULL) { | |
58a98c9c | 931 | dso__set_short_name(dso, short_name, false); |
cdd059d7 JO |
932 | dso->kernel = dso_type; |
933 | } | |
934 | ||
935 | return dso; | |
936 | } | |
937 | ||
4598a0a6 WL |
938 | /* |
939 | * Find a matching entry and/or link current entry to RB tree. | |
940 | * Either one of the dso or name parameter must be non-NULL or the | |
941 | * function will not work. | |
942 | */ | |
e8807844 ACM |
943 | static struct dso *__dso__findlink_by_longname(struct rb_root *root, |
944 | struct dso *dso, const char *name) | |
4598a0a6 WL |
945 | { |
946 | struct rb_node **p = &root->rb_node; | |
947 | struct rb_node *parent = NULL; | |
948 | ||
949 | if (!name) | |
950 | name = dso->long_name; | |
951 | /* | |
952 | * Find node with the matching name | |
953 | */ | |
954 | while (*p) { | |
955 | struct dso *this = rb_entry(*p, struct dso, rb_node); | |
956 | int rc = strcmp(name, this->long_name); | |
957 | ||
958 | parent = *p; | |
959 | if (rc == 0) { | |
960 | /* | |
961 | * In case the new DSO is a duplicate of an existing | |
0f5e1558 | 962 | * one, print a one-time warning & put the new entry |
4598a0a6 WL |
963 | * at the end of the list of duplicates. |
964 | */ | |
965 | if (!dso || (dso == this)) | |
966 | return this; /* Find matching dso */ | |
967 | /* | |
968 | * The core kernel DSOs may have duplicated long name. | |
969 | * In this case, the short name should be different. | |
970 | * Comparing the short names to differentiate the DSOs. | |
971 | */ | |
972 | rc = strcmp(dso->short_name, this->short_name); | |
973 | if (rc == 0) { | |
974 | pr_err("Duplicated dso name: %s\n", name); | |
975 | return NULL; | |
976 | } | |
977 | } | |
978 | if (rc < 0) | |
979 | p = &parent->rb_left; | |
980 | else | |
981 | p = &parent->rb_right; | |
982 | } | |
983 | if (dso) { | |
984 | /* Add new node and rebalance tree */ | |
985 | rb_link_node(&dso->rb_node, parent, p); | |
986 | rb_insert_color(&dso->rb_node, root); | |
e266a753 | 987 | dso->root = root; |
4598a0a6 WL |
988 | } |
989 | return NULL; | |
990 | } | |
991 | ||
e8807844 ACM |
992 | static inline struct dso *__dso__find_by_longname(struct rb_root *root, |
993 | const char *name) | |
4598a0a6 | 994 | { |
e8807844 | 995 | return __dso__findlink_by_longname(root, NULL, name); |
4598a0a6 WL |
996 | } |
997 | ||
bf4414ae | 998 | void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated) |
cdd059d7 | 999 | { |
e266a753 AH |
1000 | struct rb_root *root = dso->root; |
1001 | ||
cdd059d7 JO |
1002 | if (name == NULL) |
1003 | return; | |
7e155d4d ACM |
1004 | |
1005 | if (dso->long_name_allocated) | |
bf4414ae | 1006 | free((char *)dso->long_name); |
7e155d4d | 1007 | |
e266a753 AH |
1008 | if (root) { |
1009 | rb_erase(&dso->rb_node, root); | |
1010 | /* | |
1011 | * __dso__findlink_by_longname() isn't guaranteed to add it | |
1012 | * back, so a clean removal is required here. | |
1013 | */ | |
1014 | RB_CLEAR_NODE(&dso->rb_node); | |
1015 | dso->root = NULL; | |
1016 | } | |
1017 | ||
7e155d4d ACM |
1018 | dso->long_name = name; |
1019 | dso->long_name_len = strlen(name); | |
1020 | dso->long_name_allocated = name_allocated; | |
e266a753 AH |
1021 | |
1022 | if (root) | |
1023 | __dso__findlink_by_longname(root, dso, NULL); | |
cdd059d7 JO |
1024 | } |
1025 | ||
58a98c9c | 1026 | void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated) |
cdd059d7 JO |
1027 | { |
1028 | if (name == NULL) | |
1029 | return; | |
58a98c9c AH |
1030 | |
1031 | if (dso->short_name_allocated) | |
1032 | free((char *)dso->short_name); | |
1033 | ||
1034 | dso->short_name = name; | |
1035 | dso->short_name_len = strlen(name); | |
1036 | dso->short_name_allocated = name_allocated; | |
cdd059d7 JO |
1037 | } |
1038 | ||
1039 | static void dso__set_basename(struct dso *dso) | |
1040 | { | |
ac5e7f84 SE |
1041 | /* |
1042 | * basename() may modify path buffer, so we must pass | |
1043 | * a copy. | |
1044 | */ | |
1045 | char *base, *lname = strdup(dso->long_name); | |
1046 | ||
1047 | if (!lname) | |
1048 | return; | |
1049 | ||
1050 | /* | |
1051 | * basename() may return a pointer to internal | |
1052 | * storage which is reused in subsequent calls | |
1053 | * so copy the result. | |
1054 | */ | |
1055 | base = strdup(basename(lname)); | |
1056 | ||
1057 | free(lname); | |
1058 | ||
1059 | if (!base) | |
1060 | return; | |
1061 | ||
1062 | dso__set_short_name(dso, base, true); | |
cdd059d7 JO |
1063 | } |
1064 | ||
1065 | int dso__name_len(const struct dso *dso) | |
1066 | { | |
1067 | if (!dso) | |
1068 | return strlen("[unknown]"); | |
bb963e16 | 1069 | if (verbose > 0) |
cdd059d7 JO |
1070 | return dso->long_name_len; |
1071 | ||
1072 | return dso->short_name_len; | |
1073 | } | |
1074 | ||
1075 | bool dso__loaded(const struct dso *dso, enum map_type type) | |
1076 | { | |
1077 | return dso->loaded & (1 << type); | |
1078 | } | |
1079 | ||
1080 | bool dso__sorted_by_name(const struct dso *dso, enum map_type type) | |
1081 | { | |
1082 | return dso->sorted_by_name & (1 << type); | |
1083 | } | |
1084 | ||
1085 | void dso__set_sorted_by_name(struct dso *dso, enum map_type type) | |
1086 | { | |
1087 | dso->sorted_by_name |= (1 << type); | |
1088 | } | |
1089 | ||
1090 | struct dso *dso__new(const char *name) | |
1091 | { | |
1092 | struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1); | |
1093 | ||
1094 | if (dso != NULL) { | |
1095 | int i; | |
1096 | strcpy(dso->name, name); | |
7e155d4d | 1097 | dso__set_long_name(dso, dso->name, false); |
58a98c9c | 1098 | dso__set_short_name(dso, dso->name, false); |
cdd059d7 JO |
1099 | for (i = 0; i < MAP__NR_TYPES; ++i) |
1100 | dso->symbols[i] = dso->symbol_names[i] = RB_ROOT; | |
ca40e2af | 1101 | dso->data.cache = RB_ROOT; |
53fa8eaa | 1102 | dso->data.fd = -1; |
c27697d6 | 1103 | dso->data.status = DSO_DATA_STATUS_UNKNOWN; |
cdd059d7 | 1104 | dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND; |
5f70619d | 1105 | dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND; |
c6d8f2a4 | 1106 | dso->is_64_bit = (sizeof(void *) == 8); |
cdd059d7 | 1107 | dso->loaded = 0; |
0131c4ec | 1108 | dso->rel = 0; |
cdd059d7 JO |
1109 | dso->sorted_by_name = 0; |
1110 | dso->has_build_id = 0; | |
2cc9d0ef | 1111 | dso->has_srcline = 1; |
906049c8 | 1112 | dso->a2l_fails = 1; |
cdd059d7 JO |
1113 | dso->kernel = DSO_TYPE_USER; |
1114 | dso->needs_swap = DSO_SWAP__UNSET; | |
4598a0a6 | 1115 | RB_CLEAR_NODE(&dso->rb_node); |
e266a753 | 1116 | dso->root = NULL; |
cdd059d7 | 1117 | INIT_LIST_HEAD(&dso->node); |
eba5102d | 1118 | INIT_LIST_HEAD(&dso->data.open_entry); |
4a936edc | 1119 | pthread_mutex_init(&dso->lock, NULL); |
7100810a | 1120 | refcount_set(&dso->refcnt, 1); |
cdd059d7 JO |
1121 | } |
1122 | ||
1123 | return dso; | |
1124 | } | |
1125 | ||
1126 | void dso__delete(struct dso *dso) | |
1127 | { | |
1128 | int i; | |
4598a0a6 WL |
1129 | |
1130 | if (!RB_EMPTY_NODE(&dso->rb_node)) | |
1131 | pr_err("DSO %s is still in rbtree when being deleted!\n", | |
1132 | dso->long_name); | |
cdd059d7 JO |
1133 | for (i = 0; i < MAP__NR_TYPES; ++i) |
1134 | symbols__delete(&dso->symbols[i]); | |
ee021d42 ACM |
1135 | |
1136 | if (dso->short_name_allocated) { | |
04662523 | 1137 | zfree((char **)&dso->short_name); |
ee021d42 ACM |
1138 | dso->short_name_allocated = false; |
1139 | } | |
1140 | ||
1141 | if (dso->long_name_allocated) { | |
04662523 | 1142 | zfree((char **)&dso->long_name); |
ee021d42 ACM |
1143 | dso->long_name_allocated = false; |
1144 | } | |
1145 | ||
53fa8eaa | 1146 | dso__data_close(dso); |
cfe9174f | 1147 | auxtrace_cache__free(dso->auxtrace_cache); |
8e67b725 | 1148 | dso_cache__free(dso); |
454ff00f | 1149 | dso__free_a2l(dso); |
04662523 | 1150 | zfree(&dso->symsrc_filename); |
4a936edc | 1151 | pthread_mutex_destroy(&dso->lock); |
cdd059d7 JO |
1152 | free(dso); |
1153 | } | |
1154 | ||
d3a7c489 ACM |
1155 | struct dso *dso__get(struct dso *dso) |
1156 | { | |
1157 | if (dso) | |
7100810a | 1158 | refcount_inc(&dso->refcnt); |
d3a7c489 ACM |
1159 | return dso; |
1160 | } | |
1161 | ||
1162 | void dso__put(struct dso *dso) | |
1163 | { | |
7100810a | 1164 | if (dso && refcount_dec_and_test(&dso->refcnt)) |
d3a7c489 ACM |
1165 | dso__delete(dso); |
1166 | } | |
1167 | ||
cdd059d7 JO |
1168 | void dso__set_build_id(struct dso *dso, void *build_id) |
1169 | { | |
1170 | memcpy(dso->build_id, build_id, sizeof(dso->build_id)); | |
1171 | dso->has_build_id = 1; | |
1172 | } | |
1173 | ||
1174 | bool dso__build_id_equal(const struct dso *dso, u8 *build_id) | |
1175 | { | |
1176 | return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0; | |
1177 | } | |
1178 | ||
1179 | void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine) | |
1180 | { | |
1181 | char path[PATH_MAX]; | |
1182 | ||
1183 | if (machine__is_default_guest(machine)) | |
1184 | return; | |
1185 | sprintf(path, "%s/sys/kernel/notes", machine->root_dir); | |
1186 | if (sysfs__read_build_id(path, dso->build_id, | |
1187 | sizeof(dso->build_id)) == 0) | |
1188 | dso->has_build_id = true; | |
1189 | } | |
1190 | ||
1191 | int dso__kernel_module_get_build_id(struct dso *dso, | |
1192 | const char *root_dir) | |
1193 | { | |
1194 | char filename[PATH_MAX]; | |
1195 | /* | |
1196 | * kernel module short names are of the form "[module]" and | |
1197 | * we need just "module" here. | |
1198 | */ | |
1199 | const char *name = dso->short_name + 1; | |
1200 | ||
1201 | snprintf(filename, sizeof(filename), | |
1202 | "%s/sys/module/%.*s/notes/.note.gnu.build-id", | |
1203 | root_dir, (int)strlen(name) - 1, name); | |
1204 | ||
1205 | if (sysfs__read_build_id(filename, dso->build_id, | |
1206 | sizeof(dso->build_id)) == 0) | |
1207 | dso->has_build_id = true; | |
1208 | ||
1209 | return 0; | |
1210 | } | |
1211 | ||
1212 | bool __dsos__read_build_ids(struct list_head *head, bool with_hits) | |
1213 | { | |
1214 | bool have_build_id = false; | |
1215 | struct dso *pos; | |
1216 | ||
1217 | list_for_each_entry(pos, head, node) { | |
6ae98ba6 | 1218 | if (with_hits && !pos->hit && !dso__is_vdso(pos)) |
cdd059d7 JO |
1219 | continue; |
1220 | if (pos->has_build_id) { | |
1221 | have_build_id = true; | |
1222 | continue; | |
1223 | } | |
1224 | if (filename__read_build_id(pos->long_name, pos->build_id, | |
1225 | sizeof(pos->build_id)) > 0) { | |
1226 | have_build_id = true; | |
1227 | pos->has_build_id = true; | |
1228 | } | |
1229 | } | |
1230 | ||
1231 | return have_build_id; | |
1232 | } | |
1233 | ||
e8807844 | 1234 | void __dsos__add(struct dsos *dsos, struct dso *dso) |
cdd059d7 | 1235 | { |
8fa7d87f | 1236 | list_add_tail(&dso->node, &dsos->head); |
e8807844 | 1237 | __dso__findlink_by_longname(&dsos->root, dso, NULL); |
d3a7c489 ACM |
1238 | /* |
1239 | * It is now in the linked list, grab a reference, then garbage collect | |
1240 | * this when needing memory, by looking at LRU dso instances in the | |
1241 | * list with atomic_read(&dso->refcnt) == 1, i.e. no references | |
1242 | * anywhere besides the one for the list, do, under a lock for the | |
1243 | * list: remove it from the list, then a dso__put(), that probably will | |
1244 | * be the last and will then call dso__delete(), end of life. | |
1245 | * | |
1246 | * That, or at the end of the 'struct machine' lifetime, when all | |
1247 | * 'struct dso' instances will be removed from the list, in | |
1248 | * dsos__exit(), if they have no other reference from some other data | |
1249 | * structure. | |
1250 | * | |
1251 | * E.g.: after processing a 'perf.data' file and storing references | |
1252 | * to objects instantiated while processing events, we will have | |
1253 | * references to the 'thread', 'map', 'dso' structs all from 'struct | |
1254 | * hist_entry' instances, but we may not need anything not referenced, | |
1255 | * so we might as well call machines__exit()/machines__delete() and | |
1256 | * garbage collect it. | |
1257 | */ | |
1258 | dso__get(dso); | |
e8807844 ACM |
1259 | } |
1260 | ||
1261 | void dsos__add(struct dsos *dsos, struct dso *dso) | |
1262 | { | |
1263 | pthread_rwlock_wrlock(&dsos->lock); | |
1264 | __dsos__add(dsos, dso); | |
1265 | pthread_rwlock_unlock(&dsos->lock); | |
cdd059d7 JO |
1266 | } |
1267 | ||
e8807844 | 1268 | struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short) |
cdd059d7 JO |
1269 | { |
1270 | struct dso *pos; | |
1271 | ||
f9ceffb6 | 1272 | if (cmp_short) { |
8fa7d87f | 1273 | list_for_each_entry(pos, &dsos->head, node) |
f9ceffb6 WL |
1274 | if (strcmp(pos->short_name, name) == 0) |
1275 | return pos; | |
1276 | return NULL; | |
1277 | } | |
e8807844 | 1278 | return __dso__find_by_longname(&dsos->root, name); |
cdd059d7 JO |
1279 | } |
1280 | ||
e8807844 ACM |
1281 | struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short) |
1282 | { | |
1283 | struct dso *dso; | |
1284 | pthread_rwlock_rdlock(&dsos->lock); | |
1285 | dso = __dsos__find(dsos, name, cmp_short); | |
1286 | pthread_rwlock_unlock(&dsos->lock); | |
1287 | return dso; | |
1288 | } | |
1289 | ||
1290 | struct dso *__dsos__addnew(struct dsos *dsos, const char *name) | |
cdd059d7 | 1291 | { |
701d8d7f | 1292 | struct dso *dso = dso__new(name); |
cdd059d7 | 1293 | |
701d8d7f | 1294 | if (dso != NULL) { |
e8807844 | 1295 | __dsos__add(dsos, dso); |
701d8d7f | 1296 | dso__set_basename(dso); |
82de26ab MH |
1297 | /* Put dso here because __dsos_add already got it */ |
1298 | dso__put(dso); | |
cdd059d7 | 1299 | } |
cdd059d7 JO |
1300 | return dso; |
1301 | } | |
1302 | ||
701d8d7f JO |
1303 | struct dso *__dsos__findnew(struct dsos *dsos, const char *name) |
1304 | { | |
e8807844 ACM |
1305 | struct dso *dso = __dsos__find(dsos, name, false); |
1306 | ||
1307 | return dso ? dso : __dsos__addnew(dsos, name); | |
1308 | } | |
701d8d7f | 1309 | |
e8807844 ACM |
1310 | struct dso *dsos__findnew(struct dsos *dsos, const char *name) |
1311 | { | |
1312 | struct dso *dso; | |
1313 | pthread_rwlock_wrlock(&dsos->lock); | |
d3a7c489 | 1314 | dso = dso__get(__dsos__findnew(dsos, name)); |
e8807844 ACM |
1315 | pthread_rwlock_unlock(&dsos->lock); |
1316 | return dso; | |
701d8d7f JO |
1317 | } |
1318 | ||
cdd059d7 | 1319 | size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp, |
417c2ff6 | 1320 | bool (skip)(struct dso *dso, int parm), int parm) |
cdd059d7 JO |
1321 | { |
1322 | struct dso *pos; | |
1323 | size_t ret = 0; | |
1324 | ||
1325 | list_for_each_entry(pos, head, node) { | |
417c2ff6 | 1326 | if (skip && skip(pos, parm)) |
cdd059d7 JO |
1327 | continue; |
1328 | ret += dso__fprintf_buildid(pos, fp); | |
1329 | ret += fprintf(fp, " %s\n", pos->long_name); | |
1330 | } | |
1331 | return ret; | |
1332 | } | |
1333 | ||
1334 | size_t __dsos__fprintf(struct list_head *head, FILE *fp) | |
1335 | { | |
1336 | struct dso *pos; | |
1337 | size_t ret = 0; | |
1338 | ||
1339 | list_for_each_entry(pos, head, node) { | |
1340 | int i; | |
1341 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
1342 | ret += dso__fprintf(pos, i, fp); | |
1343 | } | |
1344 | ||
1345 | return ret; | |
1346 | } | |
1347 | ||
1348 | size_t dso__fprintf_buildid(struct dso *dso, FILE *fp) | |
1349 | { | |
b5d8bbe8 | 1350 | char sbuild_id[SBUILD_ID_SIZE]; |
cdd059d7 JO |
1351 | |
1352 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); | |
1353 | return fprintf(fp, "%s", sbuild_id); | |
1354 | } | |
1355 | ||
1356 | size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp) | |
1357 | { | |
1358 | struct rb_node *nd; | |
1359 | size_t ret = fprintf(fp, "dso: %s (", dso->short_name); | |
1360 | ||
1361 | if (dso->short_name != dso->long_name) | |
1362 | ret += fprintf(fp, "%s, ", dso->long_name); | |
1363 | ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type], | |
919d590f | 1364 | dso__loaded(dso, type) ? "" : "NOT "); |
cdd059d7 JO |
1365 | ret += dso__fprintf_buildid(dso, fp); |
1366 | ret += fprintf(fp, ")\n"); | |
1367 | for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) { | |
1368 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); | |
1369 | ret += symbol__fprintf(pos, fp); | |
1370 | } | |
1371 | ||
1372 | return ret; | |
1373 | } | |
2b5b8bb2 AH |
1374 | |
1375 | enum dso_type dso__type(struct dso *dso, struct machine *machine) | |
1376 | { | |
1377 | int fd; | |
4bb11d01 | 1378 | enum dso_type type = DSO__TYPE_UNKNOWN; |
2b5b8bb2 | 1379 | |
4bb11d01 NK |
1380 | fd = dso__data_get_fd(dso, machine); |
1381 | if (fd >= 0) { | |
1382 | type = dso__type_fd(fd); | |
1383 | dso__data_put_fd(dso); | |
1384 | } | |
2b5b8bb2 | 1385 | |
4bb11d01 | 1386 | return type; |
2b5b8bb2 | 1387 | } |
18425f13 ACM |
1388 | |
1389 | int dso__strerror_load(struct dso *dso, char *buf, size_t buflen) | |
1390 | { | |
1391 | int idx, errnum = dso->load_errno; | |
1392 | /* | |
1393 | * This must have a same ordering as the enum dso_load_errno. | |
1394 | */ | |
1395 | static const char *dso_load__error_str[] = { | |
1396 | "Internal tools/perf/ library error", | |
1397 | "Invalid ELF file", | |
1398 | "Can not read build id", | |
1399 | "Mismatching build id", | |
1400 | "Decompression failure", | |
1401 | }; | |
1402 | ||
1403 | BUG_ON(buflen == 0); | |
1404 | ||
1405 | if (errnum >= 0) { | |
c8b5f2c9 | 1406 | const char *err = str_error_r(errnum, buf, buflen); |
18425f13 ACM |
1407 | |
1408 | if (err != buf) | |
1409 | scnprintf(buf, buflen, "%s", err); | |
1410 | ||
1411 | return 0; | |
1412 | } | |
1413 | ||
1414 | if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END) | |
1415 | return -1; | |
1416 | ||
1417 | idx = errnum - __DSO_LOAD_ERRNO__START; | |
1418 | scnprintf(buf, buflen, "%s", dso_load__error_str[idx]); | |
1419 | return 0; | |
1420 | } |