blob: f37548322c9debe2218bb538f1758585f440a797 [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 Hunter39b12f72013-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{
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100181 struct kmod_path m;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900182
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100183 if (kmod_path__parse(&m, pathname))
184 return NULL;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900185
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100186 if (compressed)
187 *compressed = m.comp;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900188
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100189 return m.kmod;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900190}
191
192bool decompress_to_file(const char *ext, const char *filename, int output_fd)
193{
194 unsigned i;
195
196 for (i = 0; compressions[i].fmt; i++) {
197 if (!strcmp(ext, compressions[i].fmt))
198 return !compressions[i].decompress(filename,
199 output_fd);
200 }
201 return false;
202}
203
204bool dso__needs_decompress(struct dso *dso)
205{
206 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
207 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
208}
209
Jiri Olsaeba51022014-04-30 15:00:59 +0200210/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100211 * Parses kernel module specified in @path and updates
212 * @m argument like:
213 *
214 * @comp - true if @path contains supported compression suffix,
215 * false otherwise
216 * @kmod - true if @path contains '.ko' suffix in right position,
217 * false otherwise
218 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
219 * of the kernel module without suffixes, otherwise strudup-ed
220 * base name of @path
221 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
222 * the compression suffix
223 *
224 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
225 */
226int __kmod_path__parse(struct kmod_path *m, const char *path,
227 bool alloc_name, bool alloc_ext)
228{
229 const char *name = strrchr(path, '/');
230 const char *ext = strrchr(path, '.');
231
232 memset(m, 0x0, sizeof(*m));
233 name = name ? name + 1 : path;
234
235 /* No extension, just return name. */
236 if (ext == NULL) {
237 if (alloc_name) {
238 m->name = strdup(name);
239 return m->name ? 0 : -ENOMEM;
240 }
241 return 0;
242 }
243
244 if (is_supported_compression(ext + 1)) {
245 m->comp = true;
246 ext -= 3;
247 }
248
249 /* Check .ko extension only if there's enough name left. */
250 if (ext > name)
251 m->kmod = !strncmp(ext, ".ko", 3);
252
253 if (alloc_name) {
254 if (m->kmod) {
255 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
256 return -ENOMEM;
257 } else {
258 if (asprintf(&m->name, "%s", name) == -1)
259 return -ENOMEM;
260 }
261
262 strxfrchar(m->name, '-', '_');
263 }
264
265 if (alloc_ext && m->comp) {
266 m->ext = strdup(ext + 4);
267 if (!m->ext) {
268 free((void *) m->name);
269 return -ENOMEM;
270 }
271 }
272
273 return 0;
274}
275
276/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200277 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200278 */
279static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200280static long dso__data_open_cnt;
Jiri Olsaeba51022014-04-30 15:00:59 +0200281
282static void dso__list_add(struct dso *dso)
283{
284 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200285 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200286}
287
288static void dso__list_del(struct dso *dso)
289{
290 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200291 WARN_ONCE(dso__data_open_cnt <= 0,
292 "DSO data fd counter out of bounds.");
293 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200294}
295
Jiri Olsaa08cae02014-05-07 21:35:02 +0200296static void close_first_dso(void);
297
298static int do_open(char *name)
299{
300 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000301 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200302
303 do {
304 fd = open(name, O_RDONLY);
305 if (fd >= 0)
306 return fd;
307
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900308 pr_debug("dso open failed: %s\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000309 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200310 if (!dso__data_open_cnt || errno != EMFILE)
311 break;
312
313 close_first_dso();
314 } while (1);
315
316 return -1;
317}
318
Jiri Olsaeba51022014-04-30 15:00:59 +0200319static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200320{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200321 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300322 char *root_dir = (char *)"";
323 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200324
Jiri Olsacdd059d2012-10-27 23:18:32 +0200325 if (!name)
326 return -ENOMEM;
327
328 if (machine)
329 root_dir = machine->root_dir;
330
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300331 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300332 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200333 free(name);
334 return -EINVAL;
335 }
336
Jiri Olsaa08cae02014-05-07 21:35:02 +0200337 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200338 free(name);
339 return fd;
340}
341
Jiri Olsac6580452014-04-30 15:47:27 +0200342static void check_data_close(void);
343
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200344/**
345 * dso_close - Open DSO data file
346 * @dso: dso object
347 *
348 * Open @dso's data file descriptor and updates
349 * list/count of open DSO objects.
350 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200351static int open_dso(struct dso *dso, struct machine *machine)
352{
353 int fd = __open_dso(dso, machine);
354
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300355 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200356 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200357 /*
358 * Check if we crossed the allowed number
359 * of opened DSOs and close one if needed.
360 */
361 check_data_close();
362 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200363
364 return fd;
365}
366
367static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200368{
369 if (dso->data.fd >= 0) {
370 close(dso->data.fd);
371 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200372 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200373 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200374 }
375}
376
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200377/**
378 * dso_close - Close DSO data file
379 * @dso: dso object
380 *
381 * Close @dso's data file descriptor and updates
382 * list/count of open DSO objects.
383 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200384static void close_dso(struct dso *dso)
385{
386 close_data_fd(dso);
387}
388
Jiri Olsac6580452014-04-30 15:47:27 +0200389static void close_first_dso(void)
390{
391 struct dso *dso;
392
393 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
394 close_dso(dso);
395}
396
397static rlim_t get_fd_limit(void)
398{
399 struct rlimit l;
400 rlim_t limit = 0;
401
402 /* Allow half of the current open fd limit. */
403 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
404 if (l.rlim_cur == RLIM_INFINITY)
405 limit = l.rlim_cur;
406 else
407 limit = l.rlim_cur / 2;
408 } else {
409 pr_err("failed to get fd limit\n");
410 limit = 1;
411 }
412
413 return limit;
414}
415
416static bool may_cache_fd(void)
417{
418 static rlim_t limit;
419
420 if (!limit)
421 limit = get_fd_limit();
422
423 if (limit == RLIM_INFINITY)
424 return true;
425
426 return limit > (rlim_t) dso__data_open_cnt;
427}
428
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200429/*
430 * Check and close LRU dso if we crossed allowed limit
431 * for opened dso file descriptors. The limit is half
432 * of the RLIMIT_NOFILE files opened.
433*/
Jiri Olsac6580452014-04-30 15:47:27 +0200434static void check_data_close(void)
435{
436 bool cache_fd = may_cache_fd();
437
438 if (!cache_fd)
439 close_first_dso();
440}
441
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200442/**
443 * dso__data_close - Close DSO data file
444 * @dso: dso object
445 *
446 * External interface to close @dso's data file descriptor.
447 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200448void dso__data_close(struct dso *dso)
449{
450 close_dso(dso);
451}
452
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200453/**
454 * dso__data_fd - Get dso's data file descriptor
455 * @dso: dso object
456 * @machine: machine object
457 *
458 * External interface to find dso's file, open it and
459 * returns file descriptor.
460 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200461int dso__data_fd(struct dso *dso, struct machine *machine)
462{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300463 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200464 DSO_BINARY_TYPE__BUILD_ID_CACHE,
465 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
466 DSO_BINARY_TYPE__NOT_FOUND,
467 };
468 int i = 0;
469
Adrian Hunterc27697d2014-07-22 16:17:18 +0300470 if (dso->data.status == DSO_DATA_STATUS_ERROR)
471 return -1;
472
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200473 if (dso->data.fd >= 0)
Adrian Hunterc27697d2014-07-22 16:17:18 +0300474 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200475
476 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
477 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300478 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200479 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200480
481 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300482 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200483
Adrian Hunterc27697d2014-07-22 16:17:18 +0300484 dso->data.fd = open_dso(dso, machine);
485 if (dso->data.fd >= 0)
486 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200487
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300488 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300489out:
490 if (dso->data.fd >= 0)
491 dso->data.status = DSO_DATA_STATUS_OK;
492 else
493 dso->data.status = DSO_DATA_STATUS_ERROR;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200494
Adrian Hunterc27697d2014-07-22 16:17:18 +0300495 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200496}
497
Adrian Hunter288be942014-07-22 16:17:19 +0300498bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
499{
500 u32 flag = 1 << by;
501
502 if (dso->data.status_seen & flag)
503 return true;
504
505 dso->data.status_seen |= flag;
506
507 return false;
508}
509
Jiri Olsacdd059d2012-10-27 23:18:32 +0200510static void
511dso_cache__free(struct rb_root *root)
512{
513 struct rb_node *next = rb_first(root);
514
515 while (next) {
516 struct dso_cache *cache;
517
518 cache = rb_entry(next, struct dso_cache, rb_node);
519 next = rb_next(&cache->rb_node);
520 rb_erase(&cache->rb_node, root);
521 free(cache);
522 }
523}
524
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300525static struct dso_cache *dso_cache__find(const struct rb_root *root, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200526{
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300527 struct rb_node * const *p = &root->rb_node;
528 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200529 struct dso_cache *cache;
530
531 while (*p != NULL) {
532 u64 end;
533
534 parent = *p;
535 cache = rb_entry(parent, struct dso_cache, rb_node);
536 end = cache->offset + DSO__DATA_CACHE_SIZE;
537
538 if (offset < cache->offset)
539 p = &(*p)->rb_left;
540 else if (offset >= end)
541 p = &(*p)->rb_right;
542 else
543 return cache;
544 }
545 return NULL;
546}
547
548static void
549dso_cache__insert(struct rb_root *root, struct dso_cache *new)
550{
551 struct rb_node **p = &root->rb_node;
552 struct rb_node *parent = NULL;
553 struct dso_cache *cache;
554 u64 offset = new->offset;
555
556 while (*p != NULL) {
557 u64 end;
558
559 parent = *p;
560 cache = rb_entry(parent, struct dso_cache, rb_node);
561 end = cache->offset + DSO__DATA_CACHE_SIZE;
562
563 if (offset < cache->offset)
564 p = &(*p)->rb_left;
565 else if (offset >= end)
566 p = &(*p)->rb_right;
567 }
568
569 rb_link_node(&new->rb_node, parent, p);
570 rb_insert_color(&new->rb_node, root);
571}
572
573static ssize_t
574dso_cache__memcpy(struct dso_cache *cache, u64 offset,
575 u8 *data, u64 size)
576{
577 u64 cache_offset = offset - cache->offset;
578 u64 cache_size = min(cache->size - cache_offset, size);
579
580 memcpy(data, cache->data + cache_offset, cache_size);
581 return cache_size;
582}
583
584static ssize_t
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200585dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200586{
587 struct dso_cache *cache;
588 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200589
590 do {
591 u64 cache_offset;
592
593 ret = -ENOMEM;
594
595 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
596 if (!cache)
597 break;
598
599 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200600
Namhyung Kimc52686f2015-01-29 17:02:01 -0300601 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200602 if (ret <= 0)
603 break;
604
605 cache->offset = cache_offset;
606 cache->size = ret;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200607 dso_cache__insert(&dso->data.cache, cache);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200608
609 ret = dso_cache__memcpy(cache, offset, data, size);
610
611 } while (0);
612
613 if (ret <= 0)
614 free(cache);
615
Jiri Olsacdd059d2012-10-27 23:18:32 +0200616 return ret;
617}
618
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200619static ssize_t dso_cache_read(struct dso *dso, u64 offset,
620 u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200621{
622 struct dso_cache *cache;
623
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200624 cache = dso_cache__find(&dso->data.cache, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200625 if (cache)
626 return dso_cache__memcpy(cache, offset, data, size);
627 else
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200628 return dso_cache__read(dso, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200629}
630
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200631/*
632 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
633 * in the rb_tree. Any read to already cached data is served
634 * by cached data.
635 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200636static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200637{
638 ssize_t r = 0;
639 u8 *p = data;
640
641 do {
642 ssize_t ret;
643
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200644 ret = dso_cache_read(dso, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200645 if (ret < 0)
646 return ret;
647
648 /* Reached EOF, return what we have. */
649 if (!ret)
650 break;
651
652 BUG_ON(ret > size);
653
654 r += ret;
655 p += ret;
656 offset += ret;
657 size -= ret;
658
659 } while (size);
660
661 return r;
662}
663
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200664static int data_file_size(struct dso *dso)
665{
666 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000667 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200668
669 if (!dso->data.file_size) {
670 if (fstat(dso->data.fd, &st)) {
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000671 pr_err("dso mmap failed, fstat: %s\n",
672 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200673 return -1;
674 }
675 dso->data.file_size = st.st_size;
676 }
677
678 return 0;
679}
680
Adrian Hunter6d363452014-07-22 16:17:35 +0300681/**
682 * dso__data_size - Return dso data size
683 * @dso: dso object
684 * @machine: machine object
685 *
686 * Return: dso data size
687 */
688off_t dso__data_size(struct dso *dso, struct machine *machine)
689{
690 int fd;
691
692 fd = dso__data_fd(dso, machine);
693 if (fd < 0)
694 return fd;
695
696 if (data_file_size(dso))
697 return -1;
698
699 /* For now just estimate dso data size is close to file size */
700 return dso->data.file_size;
701}
702
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200703static ssize_t data_read_offset(struct dso *dso, u64 offset,
704 u8 *data, ssize_t size)
705{
706 if (data_file_size(dso))
707 return -1;
708
709 /* Check the offset sanity. */
710 if (offset > dso->data.file_size)
711 return -1;
712
713 if (offset + size < offset)
714 return -1;
715
716 return cached_read(dso, offset, data, size);
717}
718
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200719/**
720 * dso__data_read_offset - Read data from dso file offset
721 * @dso: dso object
722 * @machine: machine object
723 * @offset: file offset
724 * @data: buffer to store data
725 * @size: size of the @data buffer
726 *
727 * External interface to read data from dso file offset. Open
728 * dso data file and use cached_read to get the data.
729 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200730ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
731 u64 offset, u8 *data, ssize_t size)
732{
733 if (dso__data_fd(dso, machine) < 0)
734 return -1;
735
736 return data_read_offset(dso, offset, data, size);
737}
738
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200739/**
740 * dso__data_read_addr - Read data from dso address
741 * @dso: dso object
742 * @machine: machine object
743 * @add: virtual memory address
744 * @data: buffer to store data
745 * @size: size of the @data buffer
746 *
747 * External interface to read data from dso address.
748 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200749ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
750 struct machine *machine, u64 addr,
751 u8 *data, ssize_t size)
752{
753 u64 offset = map->map_ip(map, addr);
754 return dso__data_read_offset(dso, machine, offset, data, size);
755}
756
757struct map *dso__new_map(const char *name)
758{
759 struct map *map = NULL;
760 struct dso *dso = dso__new(name);
761
762 if (dso)
763 map = map__new2(0, dso, MAP__FUNCTION);
764
765 return map;
766}
767
768struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
769 const char *short_name, int dso_type)
770{
771 /*
772 * The kernel dso could be created by build_id processing.
773 */
774 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
775
776 /*
777 * We need to run this in all cases, since during the build_id
778 * processing we had no idea this was the kernel dso.
779 */
780 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300781 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200782 dso->kernel = dso_type;
783 }
784
785 return dso;
786}
787
Waiman Long4598a0a2014-09-30 13:36:15 -0400788/*
789 * Find a matching entry and/or link current entry to RB tree.
790 * Either one of the dso or name parameter must be non-NULL or the
791 * function will not work.
792 */
793static struct dso *dso__findlink_by_longname(struct rb_root *root,
794 struct dso *dso, const char *name)
795{
796 struct rb_node **p = &root->rb_node;
797 struct rb_node *parent = NULL;
798
799 if (!name)
800 name = dso->long_name;
801 /*
802 * Find node with the matching name
803 */
804 while (*p) {
805 struct dso *this = rb_entry(*p, struct dso, rb_node);
806 int rc = strcmp(name, this->long_name);
807
808 parent = *p;
809 if (rc == 0) {
810 /*
811 * In case the new DSO is a duplicate of an existing
812 * one, print an one-time warning & put the new entry
813 * at the end of the list of duplicates.
814 */
815 if (!dso || (dso == this))
816 return this; /* Find matching dso */
817 /*
818 * The core kernel DSOs may have duplicated long name.
819 * In this case, the short name should be different.
820 * Comparing the short names to differentiate the DSOs.
821 */
822 rc = strcmp(dso->short_name, this->short_name);
823 if (rc == 0) {
824 pr_err("Duplicated dso name: %s\n", name);
825 return NULL;
826 }
827 }
828 if (rc < 0)
829 p = &parent->rb_left;
830 else
831 p = &parent->rb_right;
832 }
833 if (dso) {
834 /* Add new node and rebalance tree */
835 rb_link_node(&dso->rb_node, parent, p);
836 rb_insert_color(&dso->rb_node, root);
837 }
838 return NULL;
839}
840
841static inline struct dso *
842dso__find_by_longname(const struct rb_root *root, const char *name)
843{
844 return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
845}
846
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300847void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200848{
849 if (name == NULL)
850 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300851
852 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300853 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300854
855 dso->long_name = name;
856 dso->long_name_len = strlen(name);
857 dso->long_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200858}
859
Adrian Hunter58a98c92013-12-10 11:11:46 -0300860void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200861{
862 if (name == NULL)
863 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -0300864
865 if (dso->short_name_allocated)
866 free((char *)dso->short_name);
867
868 dso->short_name = name;
869 dso->short_name_len = strlen(name);
870 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200871}
872
873static void dso__set_basename(struct dso *dso)
874{
Stephane Eranianac5e7f82013-12-05 19:26:42 +0100875 /*
876 * basename() may modify path buffer, so we must pass
877 * a copy.
878 */
879 char *base, *lname = strdup(dso->long_name);
880
881 if (!lname)
882 return;
883
884 /*
885 * basename() may return a pointer to internal
886 * storage which is reused in subsequent calls
887 * so copy the result.
888 */
889 base = strdup(basename(lname));
890
891 free(lname);
892
893 if (!base)
894 return;
895
896 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200897}
898
899int dso__name_len(const struct dso *dso)
900{
901 if (!dso)
902 return strlen("[unknown]");
903 if (verbose)
904 return dso->long_name_len;
905
906 return dso->short_name_len;
907}
908
909bool dso__loaded(const struct dso *dso, enum map_type type)
910{
911 return dso->loaded & (1 << type);
912}
913
914bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
915{
916 return dso->sorted_by_name & (1 << type);
917}
918
919void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
920{
921 dso->sorted_by_name |= (1 << type);
922}
923
924struct dso *dso__new(const char *name)
925{
926 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
927
928 if (dso != NULL) {
929 int i;
930 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300931 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -0300932 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200933 for (i = 0; i < MAP__NR_TYPES; ++i)
934 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200935 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200936 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +0300937 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200938 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300939 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300940 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200941 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300942 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200943 dso->sorted_by_name = 0;
944 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900945 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +0200946 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200947 dso->kernel = DSO_TYPE_USER;
948 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -0400949 RB_CLEAR_NODE(&dso->rb_node);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200950 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +0200951 INIT_LIST_HEAD(&dso->data.open_entry);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200952 }
953
954 return dso;
955}
956
957void dso__delete(struct dso *dso)
958{
959 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -0400960
961 if (!RB_EMPTY_NODE(&dso->rb_node))
962 pr_err("DSO %s is still in rbtree when being deleted!\n",
963 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200964 for (i = 0; i < MAP__NR_TYPES; ++i)
965 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300966
967 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300968 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300969 dso->short_name_allocated = false;
970 }
971
972 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300973 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300974 dso->long_name_allocated = false;
975 }
976
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200977 dso__data_close(dso);
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200978 dso_cache__free(&dso->data.cache);
Adrian Hunter454ff002013-12-03 09:23:07 +0200979 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300980 zfree(&dso->symsrc_filename);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200981 free(dso);
982}
983
984void dso__set_build_id(struct dso *dso, void *build_id)
985{
986 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
987 dso->has_build_id = 1;
988}
989
990bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
991{
992 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
993}
994
995void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
996{
997 char path[PATH_MAX];
998
999 if (machine__is_default_guest(machine))
1000 return;
1001 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1002 if (sysfs__read_build_id(path, dso->build_id,
1003 sizeof(dso->build_id)) == 0)
1004 dso->has_build_id = true;
1005}
1006
1007int dso__kernel_module_get_build_id(struct dso *dso,
1008 const char *root_dir)
1009{
1010 char filename[PATH_MAX];
1011 /*
1012 * kernel module short names are of the form "[module]" and
1013 * we need just "module" here.
1014 */
1015 const char *name = dso->short_name + 1;
1016
1017 snprintf(filename, sizeof(filename),
1018 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1019 root_dir, (int)strlen(name) - 1, name);
1020
1021 if (sysfs__read_build_id(filename, dso->build_id,
1022 sizeof(dso->build_id)) == 0)
1023 dso->has_build_id = true;
1024
1025 return 0;
1026}
1027
1028bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1029{
1030 bool have_build_id = false;
1031 struct dso *pos;
1032
1033 list_for_each_entry(pos, head, node) {
1034 if (with_hits && !pos->hit)
1035 continue;
1036 if (pos->has_build_id) {
1037 have_build_id = true;
1038 continue;
1039 }
1040 if (filename__read_build_id(pos->long_name, pos->build_id,
1041 sizeof(pos->build_id)) > 0) {
1042 have_build_id = true;
1043 pos->has_build_id = true;
1044 }
1045 }
1046
1047 return have_build_id;
1048}
1049
Waiman Long8fa7d872014-09-29 16:07:28 -04001050void dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001051{
Waiman Long8fa7d872014-09-29 16:07:28 -04001052 list_add_tail(&dso->node, &dsos->head);
Waiman Long4598a0a2014-09-30 13:36:15 -04001053 dso__findlink_by_longname(&dsos->root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001054}
1055
Waiman Long8fa7d872014-09-29 16:07:28 -04001056struct dso *dsos__find(const struct dsos *dsos, const char *name,
1057 bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001058{
1059 struct dso *pos;
1060
Waiman Longf9ceffb2013-05-09 10:42:48 -04001061 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001062 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001063 if (strcmp(pos->short_name, name) == 0)
1064 return pos;
1065 return NULL;
1066 }
Waiman Long4598a0a2014-09-30 13:36:15 -04001067 return dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001068}
1069
Jiri Olsa701d8d72015-02-12 22:06:09 +01001070struct dso *dsos__addnew(struct dsos *dsos, const char *name)
1071{
1072 struct dso *dso = dso__new(name);
1073
1074 if (dso != NULL) {
1075 dsos__add(dsos, dso);
1076 dso__set_basename(dso);
1077 }
1078 return dso;
1079}
1080
Waiman Long8fa7d872014-09-29 16:07:28 -04001081struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001082{
Waiman Long8fa7d872014-09-29 16:07:28 -04001083 struct dso *dso = dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001084
Jiri Olsa701d8d72015-02-12 22:06:09 +01001085 return dso ? dso : dsos__addnew(dsos, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001086}
1087
1088size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001089 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001090{
1091 struct dso *pos;
1092 size_t ret = 0;
1093
1094 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001095 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001096 continue;
1097 ret += dso__fprintf_buildid(pos, fp);
1098 ret += fprintf(fp, " %s\n", pos->long_name);
1099 }
1100 return ret;
1101}
1102
1103size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1104{
1105 struct dso *pos;
1106 size_t ret = 0;
1107
1108 list_for_each_entry(pos, head, node) {
1109 int i;
1110 for (i = 0; i < MAP__NR_TYPES; ++i)
1111 ret += dso__fprintf(pos, i, fp);
1112 }
1113
1114 return ret;
1115}
1116
1117size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1118{
1119 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1120
1121 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1122 return fprintf(fp, "%s", sbuild_id);
1123}
1124
1125size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1126{
1127 struct rb_node *nd;
1128 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1129
1130 if (dso->short_name != dso->long_name)
1131 ret += fprintf(fp, "%s, ", dso->long_name);
1132 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001133 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001134 ret += dso__fprintf_buildid(dso, fp);
1135 ret += fprintf(fp, ")\n");
1136 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1137 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1138 ret += symbol__fprintf(pos, fp);
1139 }
1140
1141 return ret;
1142}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001143
1144enum dso_type dso__type(struct dso *dso, struct machine *machine)
1145{
1146 int fd;
1147
1148 fd = dso__data_fd(dso, machine);
1149 if (fd < 0)
1150 return DSO__TYPE_UNKNOWN;
1151
1152 return dso__type_fd(fd);
1153}