blob: e657d86e82b9078fb051a5c26d6a4a3697438cd3 [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 Olsac1f9aa02014-05-07 21:09:59 +0200208/**
209 * dso_close - Open DSO data file
210 * @dso: dso object
211 *
212 * Open @dso's data file descriptor and updates
213 * list/count of open DSO objects.
214 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200215static int open_dso(struct dso *dso, struct machine *machine)
216{
217 int fd = __open_dso(dso, machine);
218
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300219 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200220 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200221 /*
222 * Check if we crossed the allowed number
223 * of opened DSOs and close one if needed.
224 */
225 check_data_close();
226 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200227
228 return fd;
229}
230
231static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200232{
233 if (dso->data.fd >= 0) {
234 close(dso->data.fd);
235 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200236 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200237 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200238 }
239}
240
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200241/**
242 * dso_close - Close DSO data file
243 * @dso: dso object
244 *
245 * Close @dso's data file descriptor and updates
246 * list/count of open DSO objects.
247 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200248static void close_dso(struct dso *dso)
249{
250 close_data_fd(dso);
251}
252
Jiri Olsac6580452014-04-30 15:47:27 +0200253static void close_first_dso(void)
254{
255 struct dso *dso;
256
257 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
258 close_dso(dso);
259}
260
261static rlim_t get_fd_limit(void)
262{
263 struct rlimit l;
264 rlim_t limit = 0;
265
266 /* Allow half of the current open fd limit. */
267 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
268 if (l.rlim_cur == RLIM_INFINITY)
269 limit = l.rlim_cur;
270 else
271 limit = l.rlim_cur / 2;
272 } else {
273 pr_err("failed to get fd limit\n");
274 limit = 1;
275 }
276
277 return limit;
278}
279
280static bool may_cache_fd(void)
281{
282 static rlim_t limit;
283
284 if (!limit)
285 limit = get_fd_limit();
286
287 if (limit == RLIM_INFINITY)
288 return true;
289
290 return limit > (rlim_t) dso__data_open_cnt;
291}
292
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200293/*
294 * Check and close LRU dso if we crossed allowed limit
295 * for opened dso file descriptors. The limit is half
296 * of the RLIMIT_NOFILE files opened.
297*/
Jiri Olsac6580452014-04-30 15:47:27 +0200298static void check_data_close(void)
299{
300 bool cache_fd = may_cache_fd();
301
302 if (!cache_fd)
303 close_first_dso();
304}
305
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200306/**
307 * dso__data_close - Close DSO data file
308 * @dso: dso object
309 *
310 * External interface to close @dso's data file descriptor.
311 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200312void dso__data_close(struct dso *dso)
313{
314 close_dso(dso);
315}
316
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200317/**
318 * dso__data_fd - Get dso's data file descriptor
319 * @dso: dso object
320 * @machine: machine object
321 *
322 * External interface to find dso's file, open it and
323 * returns file descriptor.
324 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200325int dso__data_fd(struct dso *dso, struct machine *machine)
326{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300327 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200328 DSO_BINARY_TYPE__BUILD_ID_CACHE,
329 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
330 DSO_BINARY_TYPE__NOT_FOUND,
331 };
332 int i = 0;
333
Adrian Hunterc27697d2014-07-22 16:17:18 +0300334 if (dso->data.status == DSO_DATA_STATUS_ERROR)
335 return -1;
336
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200337 if (dso->data.fd >= 0)
Adrian Hunterc27697d2014-07-22 16:17:18 +0300338 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200339
340 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
341 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300342 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200343 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200344
345 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300346 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200347
Adrian Hunterc27697d2014-07-22 16:17:18 +0300348 dso->data.fd = open_dso(dso, machine);
349 if (dso->data.fd >= 0)
350 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200351
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300352 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300353out:
354 if (dso->data.fd >= 0)
355 dso->data.status = DSO_DATA_STATUS_OK;
356 else
357 dso->data.status = DSO_DATA_STATUS_ERROR;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200358
Adrian Hunterc27697d2014-07-22 16:17:18 +0300359 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200360}
361
Adrian Hunter288be942014-07-22 16:17:19 +0300362bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
363{
364 u32 flag = 1 << by;
365
366 if (dso->data.status_seen & flag)
367 return true;
368
369 dso->data.status_seen |= flag;
370
371 return false;
372}
373
Jiri Olsacdd059d2012-10-27 23:18:32 +0200374static void
375dso_cache__free(struct rb_root *root)
376{
377 struct rb_node *next = rb_first(root);
378
379 while (next) {
380 struct dso_cache *cache;
381
382 cache = rb_entry(next, struct dso_cache, rb_node);
383 next = rb_next(&cache->rb_node);
384 rb_erase(&cache->rb_node, root);
385 free(cache);
386 }
387}
388
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300389static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200390{
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300391 struct rb_node * const *p = &root->rb_node;
392 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200393 struct dso_cache *cache;
394
395 while (*p != NULL) {
396 u64 end;
397
398 parent = *p;
399 cache = rb_entry(parent, struct dso_cache, rb_node);
400 end = cache->offset + DSO__DATA_CACHE_SIZE;
401
402 if (offset < cache->offset)
403 p = &(*p)->rb_left;
404 else if (offset >= end)
405 p = &(*p)->rb_right;
406 else
407 return cache;
408 }
409 return NULL;
410}
411
412static void
413dso_cache__insert(struct rb_root *root, struct dso_cache *new)
414{
415 struct rb_node **p = &root->rb_node;
416 struct rb_node *parent = NULL;
417 struct dso_cache *cache;
418 u64 offset = new->offset;
419
420 while (*p != NULL) {
421 u64 end;
422
423 parent = *p;
424 cache = rb_entry(parent, struct dso_cache, rb_node);
425 end = cache->offset + DSO__DATA_CACHE_SIZE;
426
427 if (offset < cache->offset)
428 p = &(*p)->rb_left;
429 else if (offset >= end)
430 p = &(*p)->rb_right;
431 }
432
433 rb_link_node(&new->rb_node, parent, p);
434 rb_insert_color(&new->rb_node, root);
435}
436
437static ssize_t
438dso_cache__memcpy(struct dso_cache *cache, u64 offset,
439 u8 *data, u64 size)
440{
441 u64 cache_offset = offset - cache->offset;
442 u64 cache_size = min(cache->size - cache_offset, size);
443
444 memcpy(data, cache->data + cache_offset, cache_size);
445 return cache_size;
446}
447
448static ssize_t
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200449dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200450{
451 struct dso_cache *cache;
452 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200453
454 do {
455 u64 cache_offset;
456
457 ret = -ENOMEM;
458
459 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
460 if (!cache)
461 break;
462
463 cache_offset = offset & DSO__DATA_CACHE_MASK;
464 ret = -EINVAL;
465
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200466 if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET))
Jiri Olsacdd059d2012-10-27 23:18:32 +0200467 break;
468
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200469 ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200470 if (ret <= 0)
471 break;
472
473 cache->offset = cache_offset;
474 cache->size = ret;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200475 dso_cache__insert(&dso->data.cache, cache);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200476
477 ret = dso_cache__memcpy(cache, offset, data, size);
478
479 } while (0);
480
481 if (ret <= 0)
482 free(cache);
483
Jiri Olsacdd059d2012-10-27 23:18:32 +0200484 return ret;
485}
486
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200487static ssize_t dso_cache_read(struct dso *dso, u64 offset,
488 u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200489{
490 struct dso_cache *cache;
491
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200492 cache = dso_cache__find(&dso->data.cache, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200493 if (cache)
494 return dso_cache__memcpy(cache, offset, data, size);
495 else
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200496 return dso_cache__read(dso, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200497}
498
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200499/*
500 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
501 * in the rb_tree. Any read to already cached data is served
502 * by cached data.
503 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200504static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200505{
506 ssize_t r = 0;
507 u8 *p = data;
508
509 do {
510 ssize_t ret;
511
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200512 ret = dso_cache_read(dso, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200513 if (ret < 0)
514 return ret;
515
516 /* Reached EOF, return what we have. */
517 if (!ret)
518 break;
519
520 BUG_ON(ret > size);
521
522 r += ret;
523 p += ret;
524 offset += ret;
525 size -= ret;
526
527 } while (size);
528
529 return r;
530}
531
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200532static int data_file_size(struct dso *dso)
533{
534 struct stat st;
535
536 if (!dso->data.file_size) {
537 if (fstat(dso->data.fd, &st)) {
538 pr_err("dso mmap failed, fstat: %s\n", strerror(errno));
539 return -1;
540 }
541 dso->data.file_size = st.st_size;
542 }
543
544 return 0;
545}
546
Adrian Hunter6d363452014-07-22 16:17:35 +0300547/**
548 * dso__data_size - Return dso data size
549 * @dso: dso object
550 * @machine: machine object
551 *
552 * Return: dso data size
553 */
554off_t dso__data_size(struct dso *dso, struct machine *machine)
555{
556 int fd;
557
558 fd = dso__data_fd(dso, machine);
559 if (fd < 0)
560 return fd;
561
562 if (data_file_size(dso))
563 return -1;
564
565 /* For now just estimate dso data size is close to file size */
566 return dso->data.file_size;
567}
568
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200569static ssize_t data_read_offset(struct dso *dso, u64 offset,
570 u8 *data, ssize_t size)
571{
572 if (data_file_size(dso))
573 return -1;
574
575 /* Check the offset sanity. */
576 if (offset > dso->data.file_size)
577 return -1;
578
579 if (offset + size < offset)
580 return -1;
581
582 return cached_read(dso, offset, data, size);
583}
584
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200585/**
586 * dso__data_read_offset - Read data from dso file offset
587 * @dso: dso object
588 * @machine: machine object
589 * @offset: file offset
590 * @data: buffer to store data
591 * @size: size of the @data buffer
592 *
593 * External interface to read data from dso file offset. Open
594 * dso data file and use cached_read to get the data.
595 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200596ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
597 u64 offset, u8 *data, ssize_t size)
598{
599 if (dso__data_fd(dso, machine) < 0)
600 return -1;
601
602 return data_read_offset(dso, offset, data, size);
603}
604
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200605/**
606 * dso__data_read_addr - Read data from dso address
607 * @dso: dso object
608 * @machine: machine object
609 * @add: virtual memory address
610 * @data: buffer to store data
611 * @size: size of the @data buffer
612 *
613 * External interface to read data from dso address.
614 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200615ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
616 struct machine *machine, u64 addr,
617 u8 *data, ssize_t size)
618{
619 u64 offset = map->map_ip(map, addr);
620 return dso__data_read_offset(dso, machine, offset, data, size);
621}
622
623struct map *dso__new_map(const char *name)
624{
625 struct map *map = NULL;
626 struct dso *dso = dso__new(name);
627
628 if (dso)
629 map = map__new2(0, dso, MAP__FUNCTION);
630
631 return map;
632}
633
634struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
635 const char *short_name, int dso_type)
636{
637 /*
638 * The kernel dso could be created by build_id processing.
639 */
640 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
641
642 /*
643 * We need to run this in all cases, since during the build_id
644 * processing we had no idea this was the kernel dso.
645 */
646 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300647 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200648 dso->kernel = dso_type;
649 }
650
651 return dso;
652}
653
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300654void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200655{
656 if (name == NULL)
657 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300658
659 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300660 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300661
662 dso->long_name = name;
663 dso->long_name_len = strlen(name);
664 dso->long_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200665}
666
Adrian Hunter58a98c92013-12-10 11:11:46 -0300667void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200668{
669 if (name == NULL)
670 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -0300671
672 if (dso->short_name_allocated)
673 free((char *)dso->short_name);
674
675 dso->short_name = name;
676 dso->short_name_len = strlen(name);
677 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200678}
679
680static void dso__set_basename(struct dso *dso)
681{
Stephane Eranianac5e7f82013-12-05 19:26:42 +0100682 /*
683 * basename() may modify path buffer, so we must pass
684 * a copy.
685 */
686 char *base, *lname = strdup(dso->long_name);
687
688 if (!lname)
689 return;
690
691 /*
692 * basename() may return a pointer to internal
693 * storage which is reused in subsequent calls
694 * so copy the result.
695 */
696 base = strdup(basename(lname));
697
698 free(lname);
699
700 if (!base)
701 return;
702
703 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200704}
705
706int dso__name_len(const struct dso *dso)
707{
708 if (!dso)
709 return strlen("[unknown]");
710 if (verbose)
711 return dso->long_name_len;
712
713 return dso->short_name_len;
714}
715
716bool dso__loaded(const struct dso *dso, enum map_type type)
717{
718 return dso->loaded & (1 << type);
719}
720
721bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
722{
723 return dso->sorted_by_name & (1 << type);
724}
725
726void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
727{
728 dso->sorted_by_name |= (1 << type);
729}
730
731struct dso *dso__new(const char *name)
732{
733 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
734
735 if (dso != NULL) {
736 int i;
737 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300738 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -0300739 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200740 for (i = 0; i < MAP__NR_TYPES; ++i)
741 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200742 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200743 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +0300744 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200745 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300746 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300747 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200748 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300749 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200750 dso->sorted_by_name = 0;
751 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900752 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +0200753 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200754 dso->kernel = DSO_TYPE_USER;
755 dso->needs_swap = DSO_SWAP__UNSET;
756 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +0200757 INIT_LIST_HEAD(&dso->data.open_entry);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200758 }
759
760 return dso;
761}
762
763void dso__delete(struct dso *dso)
764{
765 int i;
766 for (i = 0; i < MAP__NR_TYPES; ++i)
767 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300768
769 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300770 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300771 dso->short_name_allocated = false;
772 }
773
774 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300775 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300776 dso->long_name_allocated = false;
777 }
778
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200779 dso__data_close(dso);
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200780 dso_cache__free(&dso->data.cache);
Adrian Hunter454ff002013-12-03 09:23:07 +0200781 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300782 zfree(&dso->symsrc_filename);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200783 free(dso);
784}
785
786void dso__set_build_id(struct dso *dso, void *build_id)
787{
788 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
789 dso->has_build_id = 1;
790}
791
792bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
793{
794 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
795}
796
797void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
798{
799 char path[PATH_MAX];
800
801 if (machine__is_default_guest(machine))
802 return;
803 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
804 if (sysfs__read_build_id(path, dso->build_id,
805 sizeof(dso->build_id)) == 0)
806 dso->has_build_id = true;
807}
808
809int dso__kernel_module_get_build_id(struct dso *dso,
810 const char *root_dir)
811{
812 char filename[PATH_MAX];
813 /*
814 * kernel module short names are of the form "[module]" and
815 * we need just "module" here.
816 */
817 const char *name = dso->short_name + 1;
818
819 snprintf(filename, sizeof(filename),
820 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
821 root_dir, (int)strlen(name) - 1, name);
822
823 if (sysfs__read_build_id(filename, dso->build_id,
824 sizeof(dso->build_id)) == 0)
825 dso->has_build_id = true;
826
827 return 0;
828}
829
830bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
831{
832 bool have_build_id = false;
833 struct dso *pos;
834
835 list_for_each_entry(pos, head, node) {
836 if (with_hits && !pos->hit)
837 continue;
838 if (pos->has_build_id) {
839 have_build_id = true;
840 continue;
841 }
842 if (filename__read_build_id(pos->long_name, pos->build_id,
843 sizeof(pos->build_id)) > 0) {
844 have_build_id = true;
845 pos->has_build_id = true;
846 }
847 }
848
849 return have_build_id;
850}
851
852void dsos__add(struct list_head *head, struct dso *dso)
853{
854 list_add_tail(&dso->node, head);
855}
856
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300857struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200858{
859 struct dso *pos;
860
Waiman Longf9ceffb2013-05-09 10:42:48 -0400861 if (cmp_short) {
862 list_for_each_entry(pos, head, node)
863 if (strcmp(pos->short_name, name) == 0)
864 return pos;
865 return NULL;
866 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200867 list_for_each_entry(pos, head, node)
868 if (strcmp(pos->long_name, name) == 0)
869 return pos;
870 return NULL;
871}
872
873struct dso *__dsos__findnew(struct list_head *head, const char *name)
874{
Waiman Longf9ceffb2013-05-09 10:42:48 -0400875 struct dso *dso = dsos__find(head, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200876
877 if (!dso) {
878 dso = dso__new(name);
879 if (dso != NULL) {
880 dsos__add(head, dso);
881 dso__set_basename(dso);
882 }
883 }
884
885 return dso;
886}
887
888size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -0300889 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200890{
891 struct dso *pos;
892 size_t ret = 0;
893
894 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -0300895 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +0200896 continue;
897 ret += dso__fprintf_buildid(pos, fp);
898 ret += fprintf(fp, " %s\n", pos->long_name);
899 }
900 return ret;
901}
902
903size_t __dsos__fprintf(struct list_head *head, FILE *fp)
904{
905 struct dso *pos;
906 size_t ret = 0;
907
908 list_for_each_entry(pos, head, node) {
909 int i;
910 for (i = 0; i < MAP__NR_TYPES; ++i)
911 ret += dso__fprintf(pos, i, fp);
912 }
913
914 return ret;
915}
916
917size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
918{
919 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
920
921 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
922 return fprintf(fp, "%s", sbuild_id);
923}
924
925size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
926{
927 struct rb_node *nd;
928 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
929
930 if (dso->short_name != dso->long_name)
931 ret += fprintf(fp, "%s, ", dso->long_name);
932 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +0100933 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +0200934 ret += dso__fprintf_buildid(dso, fp);
935 ret += fprintf(fp, ")\n");
936 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
937 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
938 ret += symbol__fprintf(pos, fp);
939 }
940
941 return ret;
942}