blob: 45be944d450adfcfaf9c0dff3a0c68296769027a [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',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090024 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020025 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
26 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090027 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020028 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Jiri Olsacdd059d2012-10-27 23:18:32 +020029 };
30
31 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
32 return '!';
33 return origin[dso->symtab_type];
34}
35
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -030036int dso__read_binary_type_filename(const struct dso *dso,
37 enum dso_binary_type type,
38 char *root_dir, char *filename, size_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +020039{
40 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
41 int ret = 0;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030042 size_t len;
Jiri Olsacdd059d2012-10-27 23:18:32 +020043
44 switch (type) {
45 case DSO_BINARY_TYPE__DEBUGLINK: {
46 char *debuglink;
47
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030048 strncpy(filename, dso->long_name, size);
49 debuglink = filename + dso->long_name_len;
50 while (debuglink != filename && *debuglink != '/')
Jiri Olsacdd059d2012-10-27 23:18:32 +020051 debuglink--;
52 if (*debuglink == '/')
53 debuglink++;
Stephane Eranian0d3dc5e2014-02-20 10:32:55 +090054 ret = filename__read_debuglink(dso->long_name, debuglink,
55 size - (debuglink - filename));
Jiri Olsacdd059d2012-10-27 23:18:32 +020056 }
57 break;
58 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
59 /* skip the locally configured cache if a symfs is given */
60 if (symbol_conf.symfs[0] ||
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030061 (dso__build_id_filename(dso, filename, size) == NULL))
Jiri Olsacdd059d2012-10-27 23:18:32 +020062 ret = -1;
63 break;
64
65 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030066 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
67 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +020068 break;
69
70 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030071 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
72 snprintf(filename + len, size - len, "%s", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +020073 break;
74
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020075 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
76 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -030077 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020078 size_t dir_size;
79
80 last_slash = dso->long_name + dso->long_name_len;
81 while (last_slash != dso->long_name && *last_slash != '/')
82 last_slash--;
83
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030084 len = __symbol__join_symfs(filename, size, "");
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020085 dir_size = last_slash - dso->long_name + 2;
86 if (dir_size > (size - len)) {
87 ret = -1;
88 break;
89 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030090 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
91 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020092 last_slash);
93 break;
94 }
95
Jiri Olsacdd059d2012-10-27 23:18:32 +020096 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
97 if (!dso->has_build_id) {
98 ret = -1;
99 break;
100 }
101
102 build_id__sprintf(dso->build_id,
103 sizeof(dso->build_id),
104 build_id_hex);
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300105 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
106 snprintf(filename + len, size - len, "%.2s/%s.debug",
107 build_id_hex, build_id_hex + 2);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200108 break;
109
Adrian Hunter39b12f782013-08-07 14:38:47 +0300110 case DSO_BINARY_TYPE__VMLINUX:
111 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200112 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300113 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200114 break;
115
116 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900117 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300118 path__join3(filename, size, symbol_conf.symfs,
119 root_dir, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200120 break;
121
122 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900123 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300124 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200125 break;
126
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300127 case DSO_BINARY_TYPE__KCORE:
128 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300129 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300130 break;
131
Jiri Olsacdd059d2012-10-27 23:18:32 +0200132 default:
133 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200134 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200135 case DSO_BINARY_TYPE__JAVA_JIT:
136 case DSO_BINARY_TYPE__NOT_FOUND:
137 ret = -1;
138 break;
139 }
140
141 return ret;
142}
143
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900144static const struct {
145 const char *fmt;
146 int (*decompress)(const char *input, int output);
147} compressions[] = {
Namhyung Kime92ce122014-10-31 16:51:38 +0900148#ifdef HAVE_ZLIB_SUPPORT
149 { "gz", gzip_decompress_to_file },
150#endif
151 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900152};
153
154bool is_supported_compression(const char *ext)
155{
156 unsigned i;
157
158 for (i = 0; compressions[i].fmt; i++) {
159 if (!strcmp(ext, compressions[i].fmt))
160 return true;
161 }
162 return false;
163}
164
165bool is_kmodule_extension(const char *ext)
166{
167 if (strncmp(ext, "ko", 2))
168 return false;
169
170 if (ext[2] == '\0' || (ext[2] == '.' && is_supported_compression(ext+3)))
171 return true;
172
173 return false;
174}
175
176bool is_kernel_module(const char *pathname, bool *compressed)
177{
178 const char *ext = strrchr(pathname, '.');
179
180 if (ext == NULL)
181 return false;
182
183 if (is_supported_compression(ext + 1)) {
184 if (compressed)
185 *compressed = true;
186 ext -= 3;
187 } else if (compressed)
188 *compressed = false;
189
190 return is_kmodule_extension(ext + 1);
191}
192
193bool decompress_to_file(const char *ext, const char *filename, int output_fd)
194{
195 unsigned i;
196
197 for (i = 0; compressions[i].fmt; i++) {
198 if (!strcmp(ext, compressions[i].fmt))
199 return !compressions[i].decompress(filename,
200 output_fd);
201 }
202 return false;
203}
204
205bool dso__needs_decompress(struct dso *dso)
206{
207 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
208 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
209}
210
Jiri Olsaeba51022014-04-30 15:00:59 +0200211/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200212 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200213 */
214static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200215static long dso__data_open_cnt;
Jiri Olsaeba51022014-04-30 15:00:59 +0200216
217static void dso__list_add(struct dso *dso)
218{
219 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200220 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200221}
222
223static void dso__list_del(struct dso *dso)
224{
225 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200226 WARN_ONCE(dso__data_open_cnt <= 0,
227 "DSO data fd counter out of bounds.");
228 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200229}
230
Jiri Olsaa08cae02014-05-07 21:35:02 +0200231static void close_first_dso(void);
232
233static int do_open(char *name)
234{
235 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000236 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200237
238 do {
239 fd = open(name, O_RDONLY);
240 if (fd >= 0)
241 return fd;
242
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000243 pr_debug("dso open failed, mmap: %s\n",
244 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200245 if (!dso__data_open_cnt || errno != EMFILE)
246 break;
247
248 close_first_dso();
249 } while (1);
250
251 return -1;
252}
253
Jiri Olsaeba51022014-04-30 15:00:59 +0200254static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200255{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200256 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300257 char *root_dir = (char *)"";
258 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200259
Jiri Olsacdd059d2012-10-27 23:18:32 +0200260 if (!name)
261 return -ENOMEM;
262
263 if (machine)
264 root_dir = machine->root_dir;
265
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300266 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300267 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200268 free(name);
269 return -EINVAL;
270 }
271
Jiri Olsaa08cae02014-05-07 21:35:02 +0200272 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200273 free(name);
274 return fd;
275}
276
Jiri Olsac6580452014-04-30 15:47:27 +0200277static void check_data_close(void);
278
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200279/**
280 * dso_close - Open DSO data file
281 * @dso: dso object
282 *
283 * Open @dso's data file descriptor and updates
284 * list/count of open DSO objects.
285 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200286static int open_dso(struct dso *dso, struct machine *machine)
287{
288 int fd = __open_dso(dso, machine);
289
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300290 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200291 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200292 /*
293 * Check if we crossed the allowed number
294 * of opened DSOs and close one if needed.
295 */
296 check_data_close();
297 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200298
299 return fd;
300}
301
302static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200303{
304 if (dso->data.fd >= 0) {
305 close(dso->data.fd);
306 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200307 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200308 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200309 }
310}
311
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200312/**
313 * dso_close - Close DSO data file
314 * @dso: dso object
315 *
316 * Close @dso's data file descriptor and updates
317 * list/count of open DSO objects.
318 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200319static void close_dso(struct dso *dso)
320{
321 close_data_fd(dso);
322}
323
Jiri Olsac6580452014-04-30 15:47:27 +0200324static void close_first_dso(void)
325{
326 struct dso *dso;
327
328 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
329 close_dso(dso);
330}
331
332static rlim_t get_fd_limit(void)
333{
334 struct rlimit l;
335 rlim_t limit = 0;
336
337 /* Allow half of the current open fd limit. */
338 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
339 if (l.rlim_cur == RLIM_INFINITY)
340 limit = l.rlim_cur;
341 else
342 limit = l.rlim_cur / 2;
343 } else {
344 pr_err("failed to get fd limit\n");
345 limit = 1;
346 }
347
348 return limit;
349}
350
351static bool may_cache_fd(void)
352{
353 static rlim_t limit;
354
355 if (!limit)
356 limit = get_fd_limit();
357
358 if (limit == RLIM_INFINITY)
359 return true;
360
361 return limit > (rlim_t) dso__data_open_cnt;
362}
363
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200364/*
365 * Check and close LRU dso if we crossed allowed limit
366 * for opened dso file descriptors. The limit is half
367 * of the RLIMIT_NOFILE files opened.
368*/
Jiri Olsac6580452014-04-30 15:47:27 +0200369static void check_data_close(void)
370{
371 bool cache_fd = may_cache_fd();
372
373 if (!cache_fd)
374 close_first_dso();
375}
376
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200377/**
378 * dso__data_close - Close DSO data file
379 * @dso: dso object
380 *
381 * External interface to close @dso's data file descriptor.
382 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200383void dso__data_close(struct dso *dso)
384{
385 close_dso(dso);
386}
387
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200388/**
389 * dso__data_fd - Get dso's data file descriptor
390 * @dso: dso object
391 * @machine: machine object
392 *
393 * External interface to find dso's file, open it and
394 * returns file descriptor.
395 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200396int dso__data_fd(struct dso *dso, struct machine *machine)
397{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300398 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200399 DSO_BINARY_TYPE__BUILD_ID_CACHE,
400 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
401 DSO_BINARY_TYPE__NOT_FOUND,
402 };
403 int i = 0;
404
Adrian Hunterc27697d2014-07-22 16:17:18 +0300405 if (dso->data.status == DSO_DATA_STATUS_ERROR)
406 return -1;
407
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200408 if (dso->data.fd >= 0)
Adrian Hunterc27697d2014-07-22 16:17:18 +0300409 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200410
411 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
412 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300413 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200414 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200415
416 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300417 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200418
Adrian Hunterc27697d2014-07-22 16:17:18 +0300419 dso->data.fd = open_dso(dso, machine);
420 if (dso->data.fd >= 0)
421 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200422
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300423 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300424out:
425 if (dso->data.fd >= 0)
426 dso->data.status = DSO_DATA_STATUS_OK;
427 else
428 dso->data.status = DSO_DATA_STATUS_ERROR;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200429
Adrian Hunterc27697d2014-07-22 16:17:18 +0300430 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200431}
432
Adrian Hunter288be942014-07-22 16:17:19 +0300433bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
434{
435 u32 flag = 1 << by;
436
437 if (dso->data.status_seen & flag)
438 return true;
439
440 dso->data.status_seen |= flag;
441
442 return false;
443}
444
Jiri Olsacdd059d2012-10-27 23:18:32 +0200445static void
446dso_cache__free(struct rb_root *root)
447{
448 struct rb_node *next = rb_first(root);
449
450 while (next) {
451 struct dso_cache *cache;
452
453 cache = rb_entry(next, struct dso_cache, rb_node);
454 next = rb_next(&cache->rb_node);
455 rb_erase(&cache->rb_node, root);
456 free(cache);
457 }
458}
459
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300460static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200461{
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300462 struct rb_node * const *p = &root->rb_node;
463 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200464 struct dso_cache *cache;
465
466 while (*p != NULL) {
467 u64 end;
468
469 parent = *p;
470 cache = rb_entry(parent, struct dso_cache, rb_node);
471 end = cache->offset + DSO__DATA_CACHE_SIZE;
472
473 if (offset < cache->offset)
474 p = &(*p)->rb_left;
475 else if (offset >= end)
476 p = &(*p)->rb_right;
477 else
478 return cache;
479 }
480 return NULL;
481}
482
483static void
484dso_cache__insert(struct rb_root *root, struct dso_cache *new)
485{
486 struct rb_node **p = &root->rb_node;
487 struct rb_node *parent = NULL;
488 struct dso_cache *cache;
489 u64 offset = new->offset;
490
491 while (*p != NULL) {
492 u64 end;
493
494 parent = *p;
495 cache = rb_entry(parent, struct dso_cache, rb_node);
496 end = cache->offset + DSO__DATA_CACHE_SIZE;
497
498 if (offset < cache->offset)
499 p = &(*p)->rb_left;
500 else if (offset >= end)
501 p = &(*p)->rb_right;
502 }
503
504 rb_link_node(&new->rb_node, parent, p);
505 rb_insert_color(&new->rb_node, root);
506}
507
508static ssize_t
509dso_cache__memcpy(struct dso_cache *cache, u64 offset,
510 u8 *data, u64 size)
511{
512 u64 cache_offset = offset - cache->offset;
513 u64 cache_size = min(cache->size - cache_offset, size);
514
515 memcpy(data, cache->data + cache_offset, cache_size);
516 return cache_size;
517}
518
519static ssize_t
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200520dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200521{
522 struct dso_cache *cache;
523 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200524
525 do {
526 u64 cache_offset;
527
528 ret = -ENOMEM;
529
530 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
531 if (!cache)
532 break;
533
534 cache_offset = offset & DSO__DATA_CACHE_MASK;
535 ret = -EINVAL;
536
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200537 if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET))
Jiri Olsacdd059d2012-10-27 23:18:32 +0200538 break;
539
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200540 ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200541 if (ret <= 0)
542 break;
543
544 cache->offset = cache_offset;
545 cache->size = ret;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200546 dso_cache__insert(&dso->data.cache, cache);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200547
548 ret = dso_cache__memcpy(cache, offset, data, size);
549
550 } while (0);
551
552 if (ret <= 0)
553 free(cache);
554
Jiri Olsacdd059d2012-10-27 23:18:32 +0200555 return ret;
556}
557
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200558static ssize_t dso_cache_read(struct dso *dso, u64 offset,
559 u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200560{
561 struct dso_cache *cache;
562
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200563 cache = dso_cache__find(&dso->data.cache, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200564 if (cache)
565 return dso_cache__memcpy(cache, offset, data, size);
566 else
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200567 return dso_cache__read(dso, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200568}
569
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200570/*
571 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
572 * in the rb_tree. Any read to already cached data is served
573 * by cached data.
574 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200575static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200576{
577 ssize_t r = 0;
578 u8 *p = data;
579
580 do {
581 ssize_t ret;
582
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200583 ret = dso_cache_read(dso, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200584 if (ret < 0)
585 return ret;
586
587 /* Reached EOF, return what we have. */
588 if (!ret)
589 break;
590
591 BUG_ON(ret > size);
592
593 r += ret;
594 p += ret;
595 offset += ret;
596 size -= ret;
597
598 } while (size);
599
600 return r;
601}
602
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200603static int data_file_size(struct dso *dso)
604{
605 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000606 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200607
608 if (!dso->data.file_size) {
609 if (fstat(dso->data.fd, &st)) {
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000610 pr_err("dso mmap failed, fstat: %s\n",
611 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200612 return -1;
613 }
614 dso->data.file_size = st.st_size;
615 }
616
617 return 0;
618}
619
Adrian Hunter6d363452014-07-22 16:17:35 +0300620/**
621 * dso__data_size - Return dso data size
622 * @dso: dso object
623 * @machine: machine object
624 *
625 * Return: dso data size
626 */
627off_t dso__data_size(struct dso *dso, struct machine *machine)
628{
629 int fd;
630
631 fd = dso__data_fd(dso, machine);
632 if (fd < 0)
633 return fd;
634
635 if (data_file_size(dso))
636 return -1;
637
638 /* For now just estimate dso data size is close to file size */
639 return dso->data.file_size;
640}
641
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200642static ssize_t data_read_offset(struct dso *dso, u64 offset,
643 u8 *data, ssize_t size)
644{
645 if (data_file_size(dso))
646 return -1;
647
648 /* Check the offset sanity. */
649 if (offset > dso->data.file_size)
650 return -1;
651
652 if (offset + size < offset)
653 return -1;
654
655 return cached_read(dso, offset, data, size);
656}
657
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200658/**
659 * dso__data_read_offset - Read data from dso file offset
660 * @dso: dso object
661 * @machine: machine object
662 * @offset: file offset
663 * @data: buffer to store data
664 * @size: size of the @data buffer
665 *
666 * External interface to read data from dso file offset. Open
667 * dso data file and use cached_read to get the data.
668 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200669ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
670 u64 offset, u8 *data, ssize_t size)
671{
672 if (dso__data_fd(dso, machine) < 0)
673 return -1;
674
675 return data_read_offset(dso, offset, data, size);
676}
677
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200678/**
679 * dso__data_read_addr - Read data from dso address
680 * @dso: dso object
681 * @machine: machine object
682 * @add: virtual memory address
683 * @data: buffer to store data
684 * @size: size of the @data buffer
685 *
686 * External interface to read data from dso address.
687 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200688ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
689 struct machine *machine, u64 addr,
690 u8 *data, ssize_t size)
691{
692 u64 offset = map->map_ip(map, addr);
693 return dso__data_read_offset(dso, machine, offset, data, size);
694}
695
696struct map *dso__new_map(const char *name)
697{
698 struct map *map = NULL;
699 struct dso *dso = dso__new(name);
700
701 if (dso)
702 map = map__new2(0, dso, MAP__FUNCTION);
703
704 return map;
705}
706
707struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
708 const char *short_name, int dso_type)
709{
710 /*
711 * The kernel dso could be created by build_id processing.
712 */
713 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
714
715 /*
716 * We need to run this in all cases, since during the build_id
717 * processing we had no idea this was the kernel dso.
718 */
719 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300720 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200721 dso->kernel = dso_type;
722 }
723
724 return dso;
725}
726
Waiman Long4598a0a2014-09-30 13:36:15 -0400727/*
728 * Find a matching entry and/or link current entry to RB tree.
729 * Either one of the dso or name parameter must be non-NULL or the
730 * function will not work.
731 */
732static struct dso *dso__findlink_by_longname(struct rb_root *root,
733 struct dso *dso, const char *name)
734{
735 struct rb_node **p = &root->rb_node;
736 struct rb_node *parent = NULL;
737
738 if (!name)
739 name = dso->long_name;
740 /*
741 * Find node with the matching name
742 */
743 while (*p) {
744 struct dso *this = rb_entry(*p, struct dso, rb_node);
745 int rc = strcmp(name, this->long_name);
746
747 parent = *p;
748 if (rc == 0) {
749 /*
750 * In case the new DSO is a duplicate of an existing
751 * one, print an one-time warning & put the new entry
752 * at the end of the list of duplicates.
753 */
754 if (!dso || (dso == this))
755 return this; /* Find matching dso */
756 /*
757 * The core kernel DSOs may have duplicated long name.
758 * In this case, the short name should be different.
759 * Comparing the short names to differentiate the DSOs.
760 */
761 rc = strcmp(dso->short_name, this->short_name);
762 if (rc == 0) {
763 pr_err("Duplicated dso name: %s\n", name);
764 return NULL;
765 }
766 }
767 if (rc < 0)
768 p = &parent->rb_left;
769 else
770 p = &parent->rb_right;
771 }
772 if (dso) {
773 /* Add new node and rebalance tree */
774 rb_link_node(&dso->rb_node, parent, p);
775 rb_insert_color(&dso->rb_node, root);
776 }
777 return NULL;
778}
779
780static inline struct dso *
781dso__find_by_longname(const struct rb_root *root, const char *name)
782{
783 return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
784}
785
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300786void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200787{
788 if (name == NULL)
789 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300790
791 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300792 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300793
794 dso->long_name = name;
795 dso->long_name_len = strlen(name);
796 dso->long_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200797}
798
Adrian Hunter58a98c92013-12-10 11:11:46 -0300799void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200800{
801 if (name == NULL)
802 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -0300803
804 if (dso->short_name_allocated)
805 free((char *)dso->short_name);
806
807 dso->short_name = name;
808 dso->short_name_len = strlen(name);
809 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200810}
811
812static void dso__set_basename(struct dso *dso)
813{
Stephane Eranianac5e7f82013-12-05 19:26:42 +0100814 /*
815 * basename() may modify path buffer, so we must pass
816 * a copy.
817 */
818 char *base, *lname = strdup(dso->long_name);
819
820 if (!lname)
821 return;
822
823 /*
824 * basename() may return a pointer to internal
825 * storage which is reused in subsequent calls
826 * so copy the result.
827 */
828 base = strdup(basename(lname));
829
830 free(lname);
831
832 if (!base)
833 return;
834
835 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200836}
837
838int dso__name_len(const struct dso *dso)
839{
840 if (!dso)
841 return strlen("[unknown]");
842 if (verbose)
843 return dso->long_name_len;
844
845 return dso->short_name_len;
846}
847
848bool dso__loaded(const struct dso *dso, enum map_type type)
849{
850 return dso->loaded & (1 << type);
851}
852
853bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
854{
855 return dso->sorted_by_name & (1 << type);
856}
857
858void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
859{
860 dso->sorted_by_name |= (1 << type);
861}
862
863struct dso *dso__new(const char *name)
864{
865 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
866
867 if (dso != NULL) {
868 int i;
869 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300870 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -0300871 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200872 for (i = 0; i < MAP__NR_TYPES; ++i)
873 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200874 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200875 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +0300876 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200877 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300878 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300879 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200880 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300881 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200882 dso->sorted_by_name = 0;
883 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900884 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +0200885 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200886 dso->kernel = DSO_TYPE_USER;
887 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -0400888 RB_CLEAR_NODE(&dso->rb_node);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200889 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +0200890 INIT_LIST_HEAD(&dso->data.open_entry);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200891 }
892
893 return dso;
894}
895
896void dso__delete(struct dso *dso)
897{
898 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -0400899
900 if (!RB_EMPTY_NODE(&dso->rb_node))
901 pr_err("DSO %s is still in rbtree when being deleted!\n",
902 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200903 for (i = 0; i < MAP__NR_TYPES; ++i)
904 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300905
906 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300907 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300908 dso->short_name_allocated = false;
909 }
910
911 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300912 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300913 dso->long_name_allocated = false;
914 }
915
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200916 dso__data_close(dso);
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200917 dso_cache__free(&dso->data.cache);
Adrian Hunter454ff002013-12-03 09:23:07 +0200918 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300919 zfree(&dso->symsrc_filename);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200920 free(dso);
921}
922
923void dso__set_build_id(struct dso *dso, void *build_id)
924{
925 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
926 dso->has_build_id = 1;
927}
928
929bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
930{
931 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
932}
933
934void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
935{
936 char path[PATH_MAX];
937
938 if (machine__is_default_guest(machine))
939 return;
940 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
941 if (sysfs__read_build_id(path, dso->build_id,
942 sizeof(dso->build_id)) == 0)
943 dso->has_build_id = true;
944}
945
946int dso__kernel_module_get_build_id(struct dso *dso,
947 const char *root_dir)
948{
949 char filename[PATH_MAX];
950 /*
951 * kernel module short names are of the form "[module]" and
952 * we need just "module" here.
953 */
954 const char *name = dso->short_name + 1;
955
956 snprintf(filename, sizeof(filename),
957 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
958 root_dir, (int)strlen(name) - 1, name);
959
960 if (sysfs__read_build_id(filename, dso->build_id,
961 sizeof(dso->build_id)) == 0)
962 dso->has_build_id = true;
963
964 return 0;
965}
966
967bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
968{
969 bool have_build_id = false;
970 struct dso *pos;
971
972 list_for_each_entry(pos, head, node) {
973 if (with_hits && !pos->hit)
974 continue;
975 if (pos->has_build_id) {
976 have_build_id = true;
977 continue;
978 }
979 if (filename__read_build_id(pos->long_name, pos->build_id,
980 sizeof(pos->build_id)) > 0) {
981 have_build_id = true;
982 pos->has_build_id = true;
983 }
984 }
985
986 return have_build_id;
987}
988
Waiman Long8fa7d872014-09-29 16:07:28 -0400989void dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200990{
Waiman Long8fa7d872014-09-29 16:07:28 -0400991 list_add_tail(&dso->node, &dsos->head);
Waiman Long4598a0a2014-09-30 13:36:15 -0400992 dso__findlink_by_longname(&dsos->root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200993}
994
Waiman Long8fa7d872014-09-29 16:07:28 -0400995struct dso *dsos__find(const struct dsos *dsos, const char *name,
996 bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200997{
998 struct dso *pos;
999
Waiman Longf9ceffb2013-05-09 10:42:48 -04001000 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001001 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001002 if (strcmp(pos->short_name, name) == 0)
1003 return pos;
1004 return NULL;
1005 }
Waiman Long4598a0a2014-09-30 13:36:15 -04001006 return dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001007}
1008
Waiman Long8fa7d872014-09-29 16:07:28 -04001009struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001010{
Waiman Long8fa7d872014-09-29 16:07:28 -04001011 struct dso *dso = dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001012
1013 if (!dso) {
1014 dso = dso__new(name);
1015 if (dso != NULL) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001016 dsos__add(dsos, dso);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001017 dso__set_basename(dso);
1018 }
1019 }
1020
1021 return dso;
1022}
1023
1024size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001025 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001026{
1027 struct dso *pos;
1028 size_t ret = 0;
1029
1030 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001031 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001032 continue;
1033 ret += dso__fprintf_buildid(pos, fp);
1034 ret += fprintf(fp, " %s\n", pos->long_name);
1035 }
1036 return ret;
1037}
1038
1039size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1040{
1041 struct dso *pos;
1042 size_t ret = 0;
1043
1044 list_for_each_entry(pos, head, node) {
1045 int i;
1046 for (i = 0; i < MAP__NR_TYPES; ++i)
1047 ret += dso__fprintf(pos, i, fp);
1048 }
1049
1050 return ret;
1051}
1052
1053size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1054{
1055 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1056
1057 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1058 return fprintf(fp, "%s", sbuild_id);
1059}
1060
1061size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1062{
1063 struct rb_node *nd;
1064 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1065
1066 if (dso->short_name != dso->long_name)
1067 ret += fprintf(fp, "%s, ", dso->long_name);
1068 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001069 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001070 ret += dso__fprintf_buildid(dso, fp);
1071 ret += fprintf(fp, ")\n");
1072 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1073 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1074 ret += symbol__fprintf(pos, fp);
1075 }
1076
1077 return ret;
1078}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001079
1080enum dso_type dso__type(struct dso *dso, struct machine *machine)
1081{
1082 int fd;
1083
1084 fd = dso__data_fd(dso, machine);
1085 if (fd < 0)
1086 return DSO__TYPE_UNKNOWN;
1087
1088 return dso__type_fd(fd);
1089}