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