blob: c30752c7eebbbfff684236f15d37d67aae3de99b [file] [log] [blame]
Jiri Olsabda6ee42014-04-30 15:25:10 +02001#include <asm/bug.h>
Jiri Olsac6580452014-04-30 15:47:27 +02002#include <sys/time.h>
3#include <sys/resource.h>
Jiri Olsacdd059d2012-10-27 23:18:32 +02004#include "symbol.h"
5#include "dso.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -03006#include "machine.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +02007#include "util.h"
8#include "debug.h"
9
10char dso__symtab_origin(const struct dso *dso)
11{
12 static const char origin[] = {
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020013 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
14 [DSO_BINARY_TYPE__VMLINUX] = 'v',
15 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
16 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
17 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
18 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
19 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
20 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
21 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
22 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
23 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
24 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
25 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
26 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Jiri Olsacdd059d2012-10-27 23:18:32 +020027 };
28
29 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
30 return '!';
31 return origin[dso->symtab_type];
32}
33
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -030034int dso__read_binary_type_filename(const struct dso *dso,
35 enum dso_binary_type type,
36 char *root_dir, char *filename, size_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +020037{
38 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
39 int ret = 0;
40
41 switch (type) {
42 case DSO_BINARY_TYPE__DEBUGLINK: {
43 char *debuglink;
44
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030045 strncpy(filename, dso->long_name, size);
46 debuglink = filename + dso->long_name_len;
47 while (debuglink != filename && *debuglink != '/')
Jiri Olsacdd059d2012-10-27 23:18:32 +020048 debuglink--;
49 if (*debuglink == '/')
50 debuglink++;
Stephane Eranian0d3dc5e2014-02-20 10:32:55 +090051 ret = filename__read_debuglink(dso->long_name, debuglink,
52 size - (debuglink - filename));
Jiri Olsacdd059d2012-10-27 23:18:32 +020053 }
54 break;
55 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
56 /* skip the locally configured cache if a symfs is given */
57 if (symbol_conf.symfs[0] ||
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030058 (dso__build_id_filename(dso, filename, size) == NULL))
Jiri Olsacdd059d2012-10-27 23:18:32 +020059 ret = -1;
60 break;
61
62 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030063 snprintf(filename, size, "%s/usr/lib/debug%s.debug",
Jiri Olsacdd059d2012-10-27 23:18:32 +020064 symbol_conf.symfs, dso->long_name);
65 break;
66
67 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030068 snprintf(filename, size, "%s/usr/lib/debug%s",
Jiri Olsacdd059d2012-10-27 23:18:32 +020069 symbol_conf.symfs, dso->long_name);
70 break;
71
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020072 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
73 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -030074 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020075 size_t len;
76 size_t dir_size;
77
78 last_slash = dso->long_name + dso->long_name_len;
79 while (last_slash != dso->long_name && *last_slash != '/')
80 last_slash--;
81
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030082 len = scnprintf(filename, size, "%s", symbol_conf.symfs);
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020083 dir_size = last_slash - dso->long_name + 2;
84 if (dir_size > (size - len)) {
85 ret = -1;
86 break;
87 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030088 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
89 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020090 last_slash);
91 break;
92 }
93
Jiri Olsacdd059d2012-10-27 23:18:32 +020094 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
95 if (!dso->has_build_id) {
96 ret = -1;
97 break;
98 }
99
100 build_id__sprintf(dso->build_id,
101 sizeof(dso->build_id),
102 build_id_hex);
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300103 snprintf(filename, size,
Jiri Olsacdd059d2012-10-27 23:18:32 +0200104 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
105 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
106 break;
107
Adrian Hunter39b12f782013-08-07 14:38:47 +0300108 case DSO_BINARY_TYPE__VMLINUX:
109 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200110 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300111 snprintf(filename, size, "%s%s",
Jiri Olsacdd059d2012-10-27 23:18:32 +0200112 symbol_conf.symfs, dso->long_name);
113 break;
114
115 case DSO_BINARY_TYPE__GUEST_KMODULE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300116 snprintf(filename, size, "%s%s%s", symbol_conf.symfs,
Jiri Olsacdd059d2012-10-27 23:18:32 +0200117 root_dir, dso->long_name);
118 break;
119
120 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300121 snprintf(filename, size, "%s%s", symbol_conf.symfs,
Jiri Olsacdd059d2012-10-27 23:18:32 +0200122 dso->long_name);
123 break;
124
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300125 case DSO_BINARY_TYPE__KCORE:
126 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300127 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300128 break;
129
Jiri Olsacdd059d2012-10-27 23:18:32 +0200130 default:
131 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200132 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200133 case DSO_BINARY_TYPE__JAVA_JIT:
134 case DSO_BINARY_TYPE__NOT_FOUND:
135 ret = -1;
136 break;
137 }
138
139 return ret;
140}
141
Jiri Olsaeba51022014-04-30 15:00:59 +0200142/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200143 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200144 */
145static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200146static long dso__data_open_cnt;
Jiri Olsaeba51022014-04-30 15:00:59 +0200147
148static void dso__list_add(struct dso *dso)
149{
150 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200151 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200152}
153
154static void dso__list_del(struct dso *dso)
155{
156 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200157 WARN_ONCE(dso__data_open_cnt <= 0,
158 "DSO data fd counter out of bounds.");
159 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200160}
161
Jiri Olsaa08cae02014-05-07 21:35:02 +0200162static void close_first_dso(void);
163
164static int do_open(char *name)
165{
166 int fd;
167
168 do {
169 fd = open(name, O_RDONLY);
170 if (fd >= 0)
171 return fd;
172
173 pr_debug("dso open failed, mmap: %s\n", strerror(errno));
174 if (!dso__data_open_cnt || errno != EMFILE)
175 break;
176
177 close_first_dso();
178 } while (1);
179
180 return -1;
181}
182
Jiri Olsaeba51022014-04-30 15:00:59 +0200183static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200184{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200185 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300186 char *root_dir = (char *)"";
187 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200188
Jiri Olsacdd059d2012-10-27 23:18:32 +0200189 if (!name)
190 return -ENOMEM;
191
192 if (machine)
193 root_dir = machine->root_dir;
194
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300195 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300196 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200197 free(name);
198 return -EINVAL;
199 }
200
Jiri Olsaa08cae02014-05-07 21:35:02 +0200201 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200202 free(name);
203 return fd;
204}
205
Jiri Olsac6580452014-04-30 15:47:27 +0200206static void check_data_close(void);
207
Jiri Olsaeba51022014-04-30 15:00:59 +0200208static int open_dso(struct dso *dso, struct machine *machine)
209{
210 int fd = __open_dso(dso, machine);
211
Jiri Olsac6580452014-04-30 15:47:27 +0200212 if (fd > 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200213 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200214 /*
215 * Check if we crossed the allowed number
216 * of opened DSOs and close one if needed.
217 */
218 check_data_close();
219 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200220
221 return fd;
222}
223
224static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200225{
226 if (dso->data.fd >= 0) {
227 close(dso->data.fd);
228 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200229 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200230 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200231 }
232}
233
Jiri Olsaeba51022014-04-30 15:00:59 +0200234static void close_dso(struct dso *dso)
235{
236 close_data_fd(dso);
237}
238
Jiri Olsac6580452014-04-30 15:47:27 +0200239static void close_first_dso(void)
240{
241 struct dso *dso;
242
243 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
244 close_dso(dso);
245}
246
247static rlim_t get_fd_limit(void)
248{
249 struct rlimit l;
250 rlim_t limit = 0;
251
252 /* Allow half of the current open fd limit. */
253 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
254 if (l.rlim_cur == RLIM_INFINITY)
255 limit = l.rlim_cur;
256 else
257 limit = l.rlim_cur / 2;
258 } else {
259 pr_err("failed to get fd limit\n");
260 limit = 1;
261 }
262
263 return limit;
264}
265
266static bool may_cache_fd(void)
267{
268 static rlim_t limit;
269
270 if (!limit)
271 limit = get_fd_limit();
272
273 if (limit == RLIM_INFINITY)
274 return true;
275
276 return limit > (rlim_t) dso__data_open_cnt;
277}
278
279static void check_data_close(void)
280{
281 bool cache_fd = may_cache_fd();
282
283 if (!cache_fd)
284 close_first_dso();
285}
286
Jiri Olsaeba51022014-04-30 15:00:59 +0200287void dso__data_close(struct dso *dso)
288{
289 close_dso(dso);
290}
291
Jiri Olsacdd059d2012-10-27 23:18:32 +0200292int dso__data_fd(struct dso *dso, struct machine *machine)
293{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300294 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200295 DSO_BINARY_TYPE__BUILD_ID_CACHE,
296 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
297 DSO_BINARY_TYPE__NOT_FOUND,
298 };
299 int i = 0;
300
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200301 if (dso->data.fd >= 0)
302 return dso->data.fd;
303
304 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
305 dso->data.fd = open_dso(dso, machine);
306 return dso->data.fd;
307 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200308
309 do {
310 int fd;
311
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300312 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200313
314 fd = open_dso(dso, machine);
315 if (fd >= 0)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200316 return dso->data.fd = fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200317
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300318 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200319
320 return -EINVAL;
321}
322
323static void
324dso_cache__free(struct rb_root *root)
325{
326 struct rb_node *next = rb_first(root);
327
328 while (next) {
329 struct dso_cache *cache;
330
331 cache = rb_entry(next, struct dso_cache, rb_node);
332 next = rb_next(&cache->rb_node);
333 rb_erase(&cache->rb_node, root);
334 free(cache);
335 }
336}
337
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300338static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200339{
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300340 struct rb_node * const *p = &root->rb_node;
341 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200342 struct dso_cache *cache;
343
344 while (*p != NULL) {
345 u64 end;
346
347 parent = *p;
348 cache = rb_entry(parent, struct dso_cache, rb_node);
349 end = cache->offset + DSO__DATA_CACHE_SIZE;
350
351 if (offset < cache->offset)
352 p = &(*p)->rb_left;
353 else if (offset >= end)
354 p = &(*p)->rb_right;
355 else
356 return cache;
357 }
358 return NULL;
359}
360
361static void
362dso_cache__insert(struct rb_root *root, struct dso_cache *new)
363{
364 struct rb_node **p = &root->rb_node;
365 struct rb_node *parent = NULL;
366 struct dso_cache *cache;
367 u64 offset = new->offset;
368
369 while (*p != NULL) {
370 u64 end;
371
372 parent = *p;
373 cache = rb_entry(parent, struct dso_cache, rb_node);
374 end = cache->offset + DSO__DATA_CACHE_SIZE;
375
376 if (offset < cache->offset)
377 p = &(*p)->rb_left;
378 else if (offset >= end)
379 p = &(*p)->rb_right;
380 }
381
382 rb_link_node(&new->rb_node, parent, p);
383 rb_insert_color(&new->rb_node, root);
384}
385
386static ssize_t
387dso_cache__memcpy(struct dso_cache *cache, u64 offset,
388 u8 *data, u64 size)
389{
390 u64 cache_offset = offset - cache->offset;
391 u64 cache_size = min(cache->size - cache_offset, size);
392
393 memcpy(data, cache->data + cache_offset, cache_size);
394 return cache_size;
395}
396
397static ssize_t
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200398dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200399{
400 struct dso_cache *cache;
401 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200402
403 do {
404 u64 cache_offset;
405
406 ret = -ENOMEM;
407
408 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
409 if (!cache)
410 break;
411
412 cache_offset = offset & DSO__DATA_CACHE_MASK;
413 ret = -EINVAL;
414
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200415 if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET))
Jiri Olsacdd059d2012-10-27 23:18:32 +0200416 break;
417
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200418 ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200419 if (ret <= 0)
420 break;
421
422 cache->offset = cache_offset;
423 cache->size = ret;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200424 dso_cache__insert(&dso->data.cache, cache);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200425
426 ret = dso_cache__memcpy(cache, offset, data, size);
427
428 } while (0);
429
430 if (ret <= 0)
431 free(cache);
432
Jiri Olsacdd059d2012-10-27 23:18:32 +0200433 return ret;
434}
435
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200436static ssize_t dso_cache_read(struct dso *dso, u64 offset,
437 u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200438{
439 struct dso_cache *cache;
440
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200441 cache = dso_cache__find(&dso->data.cache, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200442 if (cache)
443 return dso_cache__memcpy(cache, offset, data, size);
444 else
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200445 return dso_cache__read(dso, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200446}
447
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200448static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200449{
450 ssize_t r = 0;
451 u8 *p = data;
452
453 do {
454 ssize_t ret;
455
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200456 ret = dso_cache_read(dso, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200457 if (ret < 0)
458 return ret;
459
460 /* Reached EOF, return what we have. */
461 if (!ret)
462 break;
463
464 BUG_ON(ret > size);
465
466 r += ret;
467 p += ret;
468 offset += ret;
469 size -= ret;
470
471 } while (size);
472
473 return r;
474}
475
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200476static int data_file_size(struct dso *dso)
477{
478 struct stat st;
479
480 if (!dso->data.file_size) {
481 if (fstat(dso->data.fd, &st)) {
482 pr_err("dso mmap failed, fstat: %s\n", strerror(errno));
483 return -1;
484 }
485 dso->data.file_size = st.st_size;
486 }
487
488 return 0;
489}
490
491static ssize_t data_read_offset(struct dso *dso, u64 offset,
492 u8 *data, ssize_t size)
493{
494 if (data_file_size(dso))
495 return -1;
496
497 /* Check the offset sanity. */
498 if (offset > dso->data.file_size)
499 return -1;
500
501 if (offset + size < offset)
502 return -1;
503
504 return cached_read(dso, offset, data, size);
505}
506
507ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
508 u64 offset, u8 *data, ssize_t size)
509{
510 if (dso__data_fd(dso, machine) < 0)
511 return -1;
512
513 return data_read_offset(dso, offset, data, size);
514}
515
Jiri Olsacdd059d2012-10-27 23:18:32 +0200516ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
517 struct machine *machine, u64 addr,
518 u8 *data, ssize_t size)
519{
520 u64 offset = map->map_ip(map, addr);
521 return dso__data_read_offset(dso, machine, offset, data, size);
522}
523
524struct map *dso__new_map(const char *name)
525{
526 struct map *map = NULL;
527 struct dso *dso = dso__new(name);
528
529 if (dso)
530 map = map__new2(0, dso, MAP__FUNCTION);
531
532 return map;
533}
534
535struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
536 const char *short_name, int dso_type)
537{
538 /*
539 * The kernel dso could be created by build_id processing.
540 */
541 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
542
543 /*
544 * We need to run this in all cases, since during the build_id
545 * processing we had no idea this was the kernel dso.
546 */
547 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300548 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200549 dso->kernel = dso_type;
550 }
551
552 return dso;
553}
554
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300555void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200556{
557 if (name == NULL)
558 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300559
560 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300561 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300562
563 dso->long_name = name;
564 dso->long_name_len = strlen(name);
565 dso->long_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200566}
567
Adrian Hunter58a98c92013-12-10 11:11:46 -0300568void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200569{
570 if (name == NULL)
571 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -0300572
573 if (dso->short_name_allocated)
574 free((char *)dso->short_name);
575
576 dso->short_name = name;
577 dso->short_name_len = strlen(name);
578 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200579}
580
581static void dso__set_basename(struct dso *dso)
582{
Stephane Eranianac5e7f82013-12-05 19:26:42 +0100583 /*
584 * basename() may modify path buffer, so we must pass
585 * a copy.
586 */
587 char *base, *lname = strdup(dso->long_name);
588
589 if (!lname)
590 return;
591
592 /*
593 * basename() may return a pointer to internal
594 * storage which is reused in subsequent calls
595 * so copy the result.
596 */
597 base = strdup(basename(lname));
598
599 free(lname);
600
601 if (!base)
602 return;
603
604 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200605}
606
607int dso__name_len(const struct dso *dso)
608{
609 if (!dso)
610 return strlen("[unknown]");
611 if (verbose)
612 return dso->long_name_len;
613
614 return dso->short_name_len;
615}
616
617bool dso__loaded(const struct dso *dso, enum map_type type)
618{
619 return dso->loaded & (1 << type);
620}
621
622bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
623{
624 return dso->sorted_by_name & (1 << type);
625}
626
627void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
628{
629 dso->sorted_by_name |= (1 << type);
630}
631
632struct dso *dso__new(const char *name)
633{
634 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
635
636 if (dso != NULL) {
637 int i;
638 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300639 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -0300640 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200641 for (i = 0; i < MAP__NR_TYPES; ++i)
642 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200643 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200644 dso->data.fd = -1;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200645 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300646 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200647 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300648 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200649 dso->sorted_by_name = 0;
650 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900651 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +0200652 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200653 dso->kernel = DSO_TYPE_USER;
654 dso->needs_swap = DSO_SWAP__UNSET;
655 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +0200656 INIT_LIST_HEAD(&dso->data.open_entry);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200657 }
658
659 return dso;
660}
661
662void dso__delete(struct dso *dso)
663{
664 int i;
665 for (i = 0; i < MAP__NR_TYPES; ++i)
666 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300667
668 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300669 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300670 dso->short_name_allocated = false;
671 }
672
673 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300674 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300675 dso->long_name_allocated = false;
676 }
677
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200678 dso__data_close(dso);
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200679 dso_cache__free(&dso->data.cache);
Adrian Hunter454ff002013-12-03 09:23:07 +0200680 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300681 zfree(&dso->symsrc_filename);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200682 free(dso);
683}
684
685void dso__set_build_id(struct dso *dso, void *build_id)
686{
687 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
688 dso->has_build_id = 1;
689}
690
691bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
692{
693 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
694}
695
696void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
697{
698 char path[PATH_MAX];
699
700 if (machine__is_default_guest(machine))
701 return;
702 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
703 if (sysfs__read_build_id(path, dso->build_id,
704 sizeof(dso->build_id)) == 0)
705 dso->has_build_id = true;
706}
707
708int dso__kernel_module_get_build_id(struct dso *dso,
709 const char *root_dir)
710{
711 char filename[PATH_MAX];
712 /*
713 * kernel module short names are of the form "[module]" and
714 * we need just "module" here.
715 */
716 const char *name = dso->short_name + 1;
717
718 snprintf(filename, sizeof(filename),
719 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
720 root_dir, (int)strlen(name) - 1, name);
721
722 if (sysfs__read_build_id(filename, dso->build_id,
723 sizeof(dso->build_id)) == 0)
724 dso->has_build_id = true;
725
726 return 0;
727}
728
729bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
730{
731 bool have_build_id = false;
732 struct dso *pos;
733
734 list_for_each_entry(pos, head, node) {
735 if (with_hits && !pos->hit)
736 continue;
737 if (pos->has_build_id) {
738 have_build_id = true;
739 continue;
740 }
741 if (filename__read_build_id(pos->long_name, pos->build_id,
742 sizeof(pos->build_id)) > 0) {
743 have_build_id = true;
744 pos->has_build_id = true;
745 }
746 }
747
748 return have_build_id;
749}
750
751void dsos__add(struct list_head *head, struct dso *dso)
752{
753 list_add_tail(&dso->node, head);
754}
755
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300756struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200757{
758 struct dso *pos;
759
Waiman Longf9ceffb2013-05-09 10:42:48 -0400760 if (cmp_short) {
761 list_for_each_entry(pos, head, node)
762 if (strcmp(pos->short_name, name) == 0)
763 return pos;
764 return NULL;
765 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200766 list_for_each_entry(pos, head, node)
767 if (strcmp(pos->long_name, name) == 0)
768 return pos;
769 return NULL;
770}
771
772struct dso *__dsos__findnew(struct list_head *head, const char *name)
773{
Waiman Longf9ceffb2013-05-09 10:42:48 -0400774 struct dso *dso = dsos__find(head, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200775
776 if (!dso) {
777 dso = dso__new(name);
778 if (dso != NULL) {
779 dsos__add(head, dso);
780 dso__set_basename(dso);
781 }
782 }
783
784 return dso;
785}
786
787size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -0300788 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200789{
790 struct dso *pos;
791 size_t ret = 0;
792
793 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -0300794 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +0200795 continue;
796 ret += dso__fprintf_buildid(pos, fp);
797 ret += fprintf(fp, " %s\n", pos->long_name);
798 }
799 return ret;
800}
801
802size_t __dsos__fprintf(struct list_head *head, FILE *fp)
803{
804 struct dso *pos;
805 size_t ret = 0;
806
807 list_for_each_entry(pos, head, node) {
808 int i;
809 for (i = 0; i < MAP__NR_TYPES; ++i)
810 ret += dso__fprintf(pos, i, fp);
811 }
812
813 return ret;
814}
815
816size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
817{
818 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
819
820 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
821 return fprintf(fp, "%s", sbuild_id);
822}
823
824size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
825{
826 struct rb_node *nd;
827 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
828
829 if (dso->short_name != dso->long_name)
830 ret += fprintf(fp, "%s, ", dso->long_name);
831 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +0100832 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +0200833 ret += dso__fprintf_buildid(dso, fp);
834 ret += fprintf(fp, ")\n");
835 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
836 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
837 ret += symbol__fprintf(pos, fp);
838 }
839
840 return ret;
841}