blob: 7a7c54b42b41bc7bae4a766a4e6992c4045aeaf3 [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
Victor Kamenskydc6254c2015-01-26 22:34:02 -080048 len = __symbol__join_symfs(filename, size, dso->long_name);
49 debuglink = filename + len;
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030050 while (debuglink != filename && *debuglink != '/')
Jiri Olsacdd059d2012-10-27 23:18:32 +020051 debuglink--;
52 if (*debuglink == '/')
53 debuglink++;
Victor Kamenskydc6254c2015-01-26 22:34:02 -080054 ret = filename__read_debuglink(filename, debuglink,
Stephane Eranian0d3dc5e2014-02-20 10:32:55 +090055 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
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100151#ifdef HAVE_LZMA_SUPPORT
152 { "xz", lzma_decompress_to_file },
153#endif
Namhyung Kime92ce122014-10-31 16:51:38 +0900154 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900155};
156
157bool is_supported_compression(const char *ext)
158{
159 unsigned i;
160
161 for (i = 0; compressions[i].fmt; i++) {
162 if (!strcmp(ext, compressions[i].fmt))
163 return true;
164 }
165 return false;
166}
167
168bool is_kmodule_extension(const char *ext)
169{
170 if (strncmp(ext, "ko", 2))
171 return false;
172
173 if (ext[2] == '\0' || (ext[2] == '.' && is_supported_compression(ext+3)))
174 return true;
175
176 return false;
177}
178
179bool is_kernel_module(const char *pathname, bool *compressed)
180{
181 const char *ext = strrchr(pathname, '.');
182
183 if (ext == NULL)
184 return false;
185
186 if (is_supported_compression(ext + 1)) {
187 if (compressed)
188 *compressed = true;
189 ext -= 3;
190 } else if (compressed)
191 *compressed = false;
192
193 return is_kmodule_extension(ext + 1);
194}
195
196bool decompress_to_file(const char *ext, const char *filename, int output_fd)
197{
198 unsigned i;
199
200 for (i = 0; compressions[i].fmt; i++) {
201 if (!strcmp(ext, compressions[i].fmt))
202 return !compressions[i].decompress(filename,
203 output_fd);
204 }
205 return false;
206}
207
208bool dso__needs_decompress(struct dso *dso)
209{
210 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
211 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
212}
213
Jiri Olsaeba51022014-04-30 15:00:59 +0200214/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100215 * Parses kernel module specified in @path and updates
216 * @m argument like:
217 *
218 * @comp - true if @path contains supported compression suffix,
219 * false otherwise
220 * @kmod - true if @path contains '.ko' suffix in right position,
221 * false otherwise
222 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
223 * of the kernel module without suffixes, otherwise strudup-ed
224 * base name of @path
225 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
226 * the compression suffix
227 *
228 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
229 */
230int __kmod_path__parse(struct kmod_path *m, const char *path,
231 bool alloc_name, bool alloc_ext)
232{
233 const char *name = strrchr(path, '/');
234 const char *ext = strrchr(path, '.');
235
236 memset(m, 0x0, sizeof(*m));
237 name = name ? name + 1 : path;
238
239 /* No extension, just return name. */
240 if (ext == NULL) {
241 if (alloc_name) {
242 m->name = strdup(name);
243 return m->name ? 0 : -ENOMEM;
244 }
245 return 0;
246 }
247
248 if (is_supported_compression(ext + 1)) {
249 m->comp = true;
250 ext -= 3;
251 }
252
253 /* Check .ko extension only if there's enough name left. */
254 if (ext > name)
255 m->kmod = !strncmp(ext, ".ko", 3);
256
257 if (alloc_name) {
258 if (m->kmod) {
259 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
260 return -ENOMEM;
261 } else {
262 if (asprintf(&m->name, "%s", name) == -1)
263 return -ENOMEM;
264 }
265
266 strxfrchar(m->name, '-', '_');
267 }
268
269 if (alloc_ext && m->comp) {
270 m->ext = strdup(ext + 4);
271 if (!m->ext) {
272 free((void *) m->name);
273 return -ENOMEM;
274 }
275 }
276
277 return 0;
278}
279
280/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200281 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200282 */
283static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200284static long dso__data_open_cnt;
Jiri Olsaeba51022014-04-30 15:00:59 +0200285
286static void dso__list_add(struct dso *dso)
287{
288 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200289 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200290}
291
292static void dso__list_del(struct dso *dso)
293{
294 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200295 WARN_ONCE(dso__data_open_cnt <= 0,
296 "DSO data fd counter out of bounds.");
297 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200298}
299
Jiri Olsaa08cae02014-05-07 21:35:02 +0200300static void close_first_dso(void);
301
302static int do_open(char *name)
303{
304 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000305 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200306
307 do {
308 fd = open(name, O_RDONLY);
309 if (fd >= 0)
310 return fd;
311
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900312 pr_debug("dso open failed: %s\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000313 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200314 if (!dso__data_open_cnt || errno != EMFILE)
315 break;
316
317 close_first_dso();
318 } while (1);
319
320 return -1;
321}
322
Jiri Olsaeba51022014-04-30 15:00:59 +0200323static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200324{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200325 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300326 char *root_dir = (char *)"";
327 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200328
Jiri Olsacdd059d2012-10-27 23:18:32 +0200329 if (!name)
330 return -ENOMEM;
331
332 if (machine)
333 root_dir = machine->root_dir;
334
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300335 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300336 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200337 free(name);
338 return -EINVAL;
339 }
340
Jiri Olsaa08cae02014-05-07 21:35:02 +0200341 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200342 free(name);
343 return fd;
344}
345
Jiri Olsac6580452014-04-30 15:47:27 +0200346static void check_data_close(void);
347
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200348/**
349 * dso_close - Open DSO data file
350 * @dso: dso object
351 *
352 * Open @dso's data file descriptor and updates
353 * list/count of open DSO objects.
354 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200355static int open_dso(struct dso *dso, struct machine *machine)
356{
357 int fd = __open_dso(dso, machine);
358
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300359 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200360 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200361 /*
362 * Check if we crossed the allowed number
363 * of opened DSOs and close one if needed.
364 */
365 check_data_close();
366 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200367
368 return fd;
369}
370
371static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200372{
373 if (dso->data.fd >= 0) {
374 close(dso->data.fd);
375 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200376 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200377 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200378 }
379}
380
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200381/**
382 * dso_close - Close DSO data file
383 * @dso: dso object
384 *
385 * Close @dso's data file descriptor and updates
386 * list/count of open DSO objects.
387 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200388static void close_dso(struct dso *dso)
389{
390 close_data_fd(dso);
391}
392
Jiri Olsac6580452014-04-30 15:47:27 +0200393static void close_first_dso(void)
394{
395 struct dso *dso;
396
397 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
398 close_dso(dso);
399}
400
401static rlim_t get_fd_limit(void)
402{
403 struct rlimit l;
404 rlim_t limit = 0;
405
406 /* Allow half of the current open fd limit. */
407 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
408 if (l.rlim_cur == RLIM_INFINITY)
409 limit = l.rlim_cur;
410 else
411 limit = l.rlim_cur / 2;
412 } else {
413 pr_err("failed to get fd limit\n");
414 limit = 1;
415 }
416
417 return limit;
418}
419
420static bool may_cache_fd(void)
421{
422 static rlim_t limit;
423
424 if (!limit)
425 limit = get_fd_limit();
426
427 if (limit == RLIM_INFINITY)
428 return true;
429
430 return limit > (rlim_t) dso__data_open_cnt;
431}
432
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200433/*
434 * Check and close LRU dso if we crossed allowed limit
435 * for opened dso file descriptors. The limit is half
436 * of the RLIMIT_NOFILE files opened.
437*/
Jiri Olsac6580452014-04-30 15:47:27 +0200438static void check_data_close(void)
439{
440 bool cache_fd = may_cache_fd();
441
442 if (!cache_fd)
443 close_first_dso();
444}
445
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200446/**
447 * dso__data_close - Close DSO data file
448 * @dso: dso object
449 *
450 * External interface to close @dso's data file descriptor.
451 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200452void dso__data_close(struct dso *dso)
453{
454 close_dso(dso);
455}
456
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200457/**
458 * dso__data_fd - Get dso's data file descriptor
459 * @dso: dso object
460 * @machine: machine object
461 *
462 * External interface to find dso's file, open it and
463 * returns file descriptor.
464 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200465int dso__data_fd(struct dso *dso, struct machine *machine)
466{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300467 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200468 DSO_BINARY_TYPE__BUILD_ID_CACHE,
469 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
470 DSO_BINARY_TYPE__NOT_FOUND,
471 };
472 int i = 0;
473
Adrian Hunterc27697d2014-07-22 16:17:18 +0300474 if (dso->data.status == DSO_DATA_STATUS_ERROR)
475 return -1;
476
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200477 if (dso->data.fd >= 0)
Adrian Hunterc27697d2014-07-22 16:17:18 +0300478 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200479
480 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
481 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300482 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200483 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200484
485 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300486 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200487
Adrian Hunterc27697d2014-07-22 16:17:18 +0300488 dso->data.fd = open_dso(dso, machine);
489 if (dso->data.fd >= 0)
490 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200491
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300492 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300493out:
494 if (dso->data.fd >= 0)
495 dso->data.status = DSO_DATA_STATUS_OK;
496 else
497 dso->data.status = DSO_DATA_STATUS_ERROR;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200498
Adrian Hunterc27697d2014-07-22 16:17:18 +0300499 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200500}
501
Adrian Hunter288be942014-07-22 16:17:19 +0300502bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
503{
504 u32 flag = 1 << by;
505
506 if (dso->data.status_seen & flag)
507 return true;
508
509 dso->data.status_seen |= flag;
510
511 return false;
512}
513
Jiri Olsacdd059d2012-10-27 23:18:32 +0200514static void
515dso_cache__free(struct rb_root *root)
516{
517 struct rb_node *next = rb_first(root);
518
519 while (next) {
520 struct dso_cache *cache;
521
522 cache = rb_entry(next, struct dso_cache, rb_node);
523 next = rb_next(&cache->rb_node);
524 rb_erase(&cache->rb_node, root);
525 free(cache);
526 }
527}
528
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300529static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200530{
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300531 struct rb_node * const *p = &root->rb_node;
532 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200533 struct dso_cache *cache;
534
535 while (*p != NULL) {
536 u64 end;
537
538 parent = *p;
539 cache = rb_entry(parent, struct dso_cache, rb_node);
540 end = cache->offset + DSO__DATA_CACHE_SIZE;
541
542 if (offset < cache->offset)
543 p = &(*p)->rb_left;
544 else if (offset >= end)
545 p = &(*p)->rb_right;
546 else
547 return cache;
548 }
549 return NULL;
550}
551
552static void
553dso_cache__insert(struct rb_root *root, struct dso_cache *new)
554{
555 struct rb_node **p = &root->rb_node;
556 struct rb_node *parent = NULL;
557 struct dso_cache *cache;
558 u64 offset = new->offset;
559
560 while (*p != NULL) {
561 u64 end;
562
563 parent = *p;
564 cache = rb_entry(parent, struct dso_cache, rb_node);
565 end = cache->offset + DSO__DATA_CACHE_SIZE;
566
567 if (offset < cache->offset)
568 p = &(*p)->rb_left;
569 else if (offset >= end)
570 p = &(*p)->rb_right;
571 }
572
573 rb_link_node(&new->rb_node, parent, p);
574 rb_insert_color(&new->rb_node, root);
575}
576
577static ssize_t
578dso_cache__memcpy(struct dso_cache *cache, u64 offset,
579 u8 *data, u64 size)
580{
581 u64 cache_offset = offset - cache->offset;
582 u64 cache_size = min(cache->size - cache_offset, size);
583
584 memcpy(data, cache->data + cache_offset, cache_size);
585 return cache_size;
586}
587
588static ssize_t
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200589dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200590{
591 struct dso_cache *cache;
592 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200593
594 do {
595 u64 cache_offset;
596
597 ret = -ENOMEM;
598
599 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
600 if (!cache)
601 break;
602
603 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200604
Namhyung Kimc52686f2015-01-29 17:02:01 -0300605 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200606 if (ret <= 0)
607 break;
608
609 cache->offset = cache_offset;
610 cache->size = ret;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200611 dso_cache__insert(&dso->data.cache, cache);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200612
613 ret = dso_cache__memcpy(cache, offset, data, size);
614
615 } while (0);
616
617 if (ret <= 0)
618 free(cache);
619
Jiri Olsacdd059d2012-10-27 23:18:32 +0200620 return ret;
621}
622
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200623static ssize_t dso_cache_read(struct dso *dso, u64 offset,
624 u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200625{
626 struct dso_cache *cache;
627
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200628 cache = dso_cache__find(&dso->data.cache, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200629 if (cache)
630 return dso_cache__memcpy(cache, offset, data, size);
631 else
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200632 return dso_cache__read(dso, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200633}
634
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200635/*
636 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
637 * in the rb_tree. Any read to already cached data is served
638 * by cached data.
639 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200640static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200641{
642 ssize_t r = 0;
643 u8 *p = data;
644
645 do {
646 ssize_t ret;
647
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200648 ret = dso_cache_read(dso, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200649 if (ret < 0)
650 return ret;
651
652 /* Reached EOF, return what we have. */
653 if (!ret)
654 break;
655
656 BUG_ON(ret > size);
657
658 r += ret;
659 p += ret;
660 offset += ret;
661 size -= ret;
662
663 } while (size);
664
665 return r;
666}
667
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200668static int data_file_size(struct dso *dso)
669{
670 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000671 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200672
673 if (!dso->data.file_size) {
674 if (fstat(dso->data.fd, &st)) {
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000675 pr_err("dso mmap failed, fstat: %s\n",
676 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200677 return -1;
678 }
679 dso->data.file_size = st.st_size;
680 }
681
682 return 0;
683}
684
Adrian Hunter6d363452014-07-22 16:17:35 +0300685/**
686 * dso__data_size - Return dso data size
687 * @dso: dso object
688 * @machine: machine object
689 *
690 * Return: dso data size
691 */
692off_t dso__data_size(struct dso *dso, struct machine *machine)
693{
694 int fd;
695
696 fd = dso__data_fd(dso, machine);
697 if (fd < 0)
698 return fd;
699
700 if (data_file_size(dso))
701 return -1;
702
703 /* For now just estimate dso data size is close to file size */
704 return dso->data.file_size;
705}
706
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200707static ssize_t data_read_offset(struct dso *dso, u64 offset,
708 u8 *data, ssize_t size)
709{
710 if (data_file_size(dso))
711 return -1;
712
713 /* Check the offset sanity. */
714 if (offset > dso->data.file_size)
715 return -1;
716
717 if (offset + size < offset)
718 return -1;
719
720 return cached_read(dso, offset, data, size);
721}
722
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200723/**
724 * dso__data_read_offset - Read data from dso file offset
725 * @dso: dso object
726 * @machine: machine object
727 * @offset: file offset
728 * @data: buffer to store data
729 * @size: size of the @data buffer
730 *
731 * External interface to read data from dso file offset. Open
732 * dso data file and use cached_read to get the data.
733 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200734ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
735 u64 offset, u8 *data, ssize_t size)
736{
737 if (dso__data_fd(dso, machine) < 0)
738 return -1;
739
740 return data_read_offset(dso, offset, data, size);
741}
742
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200743/**
744 * dso__data_read_addr - Read data from dso address
745 * @dso: dso object
746 * @machine: machine object
747 * @add: virtual memory address
748 * @data: buffer to store data
749 * @size: size of the @data buffer
750 *
751 * External interface to read data from dso address.
752 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200753ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
754 struct machine *machine, u64 addr,
755 u8 *data, ssize_t size)
756{
757 u64 offset = map->map_ip(map, addr);
758 return dso__data_read_offset(dso, machine, offset, data, size);
759}
760
761struct map *dso__new_map(const char *name)
762{
763 struct map *map = NULL;
764 struct dso *dso = dso__new(name);
765
766 if (dso)
767 map = map__new2(0, dso, MAP__FUNCTION);
768
769 return map;
770}
771
772struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
773 const char *short_name, int dso_type)
774{
775 /*
776 * The kernel dso could be created by build_id processing.
777 */
778 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
779
780 /*
781 * We need to run this in all cases, since during the build_id
782 * processing we had no idea this was the kernel dso.
783 */
784 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300785 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200786 dso->kernel = dso_type;
787 }
788
789 return dso;
790}
791
Waiman Long4598a0a2014-09-30 13:36:15 -0400792/*
793 * Find a matching entry and/or link current entry to RB tree.
794 * Either one of the dso or name parameter must be non-NULL or the
795 * function will not work.
796 */
797static struct dso *dso__findlink_by_longname(struct rb_root *root,
798 struct dso *dso, const char *name)
799{
800 struct rb_node **p = &root->rb_node;
801 struct rb_node *parent = NULL;
802
803 if (!name)
804 name = dso->long_name;
805 /*
806 * Find node with the matching name
807 */
808 while (*p) {
809 struct dso *this = rb_entry(*p, struct dso, rb_node);
810 int rc = strcmp(name, this->long_name);
811
812 parent = *p;
813 if (rc == 0) {
814 /*
815 * In case the new DSO is a duplicate of an existing
816 * one, print an one-time warning & put the new entry
817 * at the end of the list of duplicates.
818 */
819 if (!dso || (dso == this))
820 return this; /* Find matching dso */
821 /*
822 * The core kernel DSOs may have duplicated long name.
823 * In this case, the short name should be different.
824 * Comparing the short names to differentiate the DSOs.
825 */
826 rc = strcmp(dso->short_name, this->short_name);
827 if (rc == 0) {
828 pr_err("Duplicated dso name: %s\n", name);
829 return NULL;
830 }
831 }
832 if (rc < 0)
833 p = &parent->rb_left;
834 else
835 p = &parent->rb_right;
836 }
837 if (dso) {
838 /* Add new node and rebalance tree */
839 rb_link_node(&dso->rb_node, parent, p);
840 rb_insert_color(&dso->rb_node, root);
841 }
842 return NULL;
843}
844
845static inline struct dso *
846dso__find_by_longname(const struct rb_root *root, const char *name)
847{
848 return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
849}
850
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300851void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200852{
853 if (name == NULL)
854 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300855
856 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300857 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300858
859 dso->long_name = name;
860 dso->long_name_len = strlen(name);
861 dso->long_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200862}
863
Adrian Hunter58a98c92013-12-10 11:11:46 -0300864void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200865{
866 if (name == NULL)
867 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -0300868
869 if (dso->short_name_allocated)
870 free((char *)dso->short_name);
871
872 dso->short_name = name;
873 dso->short_name_len = strlen(name);
874 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200875}
876
877static void dso__set_basename(struct dso *dso)
878{
Stephane Eranianac5e7f82013-12-05 19:26:42 +0100879 /*
880 * basename() may modify path buffer, so we must pass
881 * a copy.
882 */
883 char *base, *lname = strdup(dso->long_name);
884
885 if (!lname)
886 return;
887
888 /*
889 * basename() may return a pointer to internal
890 * storage which is reused in subsequent calls
891 * so copy the result.
892 */
893 base = strdup(basename(lname));
894
895 free(lname);
896
897 if (!base)
898 return;
899
900 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200901}
902
903int dso__name_len(const struct dso *dso)
904{
905 if (!dso)
906 return strlen("[unknown]");
907 if (verbose)
908 return dso->long_name_len;
909
910 return dso->short_name_len;
911}
912
913bool dso__loaded(const struct dso *dso, enum map_type type)
914{
915 return dso->loaded & (1 << type);
916}
917
918bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
919{
920 return dso->sorted_by_name & (1 << type);
921}
922
923void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
924{
925 dso->sorted_by_name |= (1 << type);
926}
927
928struct dso *dso__new(const char *name)
929{
930 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
931
932 if (dso != NULL) {
933 int i;
934 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300935 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -0300936 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200937 for (i = 0; i < MAP__NR_TYPES; ++i)
938 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200939 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200940 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +0300941 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200942 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300943 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300944 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200945 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300946 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200947 dso->sorted_by_name = 0;
948 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900949 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +0200950 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200951 dso->kernel = DSO_TYPE_USER;
952 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -0400953 RB_CLEAR_NODE(&dso->rb_node);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200954 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +0200955 INIT_LIST_HEAD(&dso->data.open_entry);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200956 }
957
958 return dso;
959}
960
961void dso__delete(struct dso *dso)
962{
963 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -0400964
965 if (!RB_EMPTY_NODE(&dso->rb_node))
966 pr_err("DSO %s is still in rbtree when being deleted!\n",
967 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200968 for (i = 0; i < MAP__NR_TYPES; ++i)
969 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300970
971 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300972 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300973 dso->short_name_allocated = false;
974 }
975
976 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300977 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300978 dso->long_name_allocated = false;
979 }
980
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200981 dso__data_close(dso);
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200982 dso_cache__free(&dso->data.cache);
Adrian Hunter454ff002013-12-03 09:23:07 +0200983 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300984 zfree(&dso->symsrc_filename);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200985 free(dso);
986}
987
988void dso__set_build_id(struct dso *dso, void *build_id)
989{
990 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
991 dso->has_build_id = 1;
992}
993
994bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
995{
996 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
997}
998
999void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1000{
1001 char path[PATH_MAX];
1002
1003 if (machine__is_default_guest(machine))
1004 return;
1005 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1006 if (sysfs__read_build_id(path, dso->build_id,
1007 sizeof(dso->build_id)) == 0)
1008 dso->has_build_id = true;
1009}
1010
1011int dso__kernel_module_get_build_id(struct dso *dso,
1012 const char *root_dir)
1013{
1014 char filename[PATH_MAX];
1015 /*
1016 * kernel module short names are of the form "[module]" and
1017 * we need just "module" here.
1018 */
1019 const char *name = dso->short_name + 1;
1020
1021 snprintf(filename, sizeof(filename),
1022 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1023 root_dir, (int)strlen(name) - 1, name);
1024
1025 if (sysfs__read_build_id(filename, dso->build_id,
1026 sizeof(dso->build_id)) == 0)
1027 dso->has_build_id = true;
1028
1029 return 0;
1030}
1031
1032bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1033{
1034 bool have_build_id = false;
1035 struct dso *pos;
1036
1037 list_for_each_entry(pos, head, node) {
1038 if (with_hits && !pos->hit)
1039 continue;
1040 if (pos->has_build_id) {
1041 have_build_id = true;
1042 continue;
1043 }
1044 if (filename__read_build_id(pos->long_name, pos->build_id,
1045 sizeof(pos->build_id)) > 0) {
1046 have_build_id = true;
1047 pos->has_build_id = true;
1048 }
1049 }
1050
1051 return have_build_id;
1052}
1053
Waiman Long8fa7d872014-09-29 16:07:28 -04001054void dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001055{
Waiman Long8fa7d872014-09-29 16:07:28 -04001056 list_add_tail(&dso->node, &dsos->head);
Waiman Long4598a0a2014-09-30 13:36:15 -04001057 dso__findlink_by_longname(&dsos->root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001058}
1059
Waiman Long8fa7d872014-09-29 16:07:28 -04001060struct dso *dsos__find(const struct dsos *dsos, const char *name,
1061 bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001062{
1063 struct dso *pos;
1064
Waiman Longf9ceffb2013-05-09 10:42:48 -04001065 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001066 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001067 if (strcmp(pos->short_name, name) == 0)
1068 return pos;
1069 return NULL;
1070 }
Waiman Long4598a0a2014-09-30 13:36:15 -04001071 return dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001072}
1073
Jiri Olsa701d8d72015-02-12 22:06:09 +01001074struct dso *dsos__addnew(struct dsos *dsos, const char *name)
1075{
1076 struct dso *dso = dso__new(name);
1077
1078 if (dso != NULL) {
1079 dsos__add(dsos, dso);
1080 dso__set_basename(dso);
1081 }
1082 return dso;
1083}
1084
Waiman Long8fa7d872014-09-29 16:07:28 -04001085struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001086{
Waiman Long8fa7d872014-09-29 16:07:28 -04001087 struct dso *dso = dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001088
Jiri Olsa701d8d72015-02-12 22:06:09 +01001089 return dso ? dso : dsos__addnew(dsos, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001090}
1091
1092size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001093 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001094{
1095 struct dso *pos;
1096 size_t ret = 0;
1097
1098 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001099 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001100 continue;
1101 ret += dso__fprintf_buildid(pos, fp);
1102 ret += fprintf(fp, " %s\n", pos->long_name);
1103 }
1104 return ret;
1105}
1106
1107size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1108{
1109 struct dso *pos;
1110 size_t ret = 0;
1111
1112 list_for_each_entry(pos, head, node) {
1113 int i;
1114 for (i = 0; i < MAP__NR_TYPES; ++i)
1115 ret += dso__fprintf(pos, i, fp);
1116 }
1117
1118 return ret;
1119}
1120
1121size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1122{
1123 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1124
1125 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1126 return fprintf(fp, "%s", sbuild_id);
1127}
1128
1129size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1130{
1131 struct rb_node *nd;
1132 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1133
1134 if (dso->short_name != dso->long_name)
1135 ret += fprintf(fp, "%s, ", dso->long_name);
1136 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001137 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001138 ret += dso__fprintf_buildid(dso, fp);
1139 ret += fprintf(fp, ")\n");
1140 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1141 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1142 ret += symbol__fprintf(pos, fp);
1143 }
1144
1145 return ret;
1146}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001147
1148enum dso_type dso__type(struct dso *dso, struct machine *machine)
1149{
1150 int fd;
1151
1152 fd = dso__data_fd(dso, machine);
1153 if (fd < 0)
1154 return DSO__TYPE_UNKNOWN;
1155
1156 return dso__type_fd(fd);
1157}