blob: 666e1db44541855555c296b0867a9d3530d9a32e [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"
Adrian Huntercfe91742015-04-09 18:53:55 +03007#include "auxtrace.h"
Jiri Olsacdd059d2012-10-27 23:18:32 +02008#include "util.h"
9#include "debug.h"
10
11char dso__symtab_origin(const struct dso *dso)
12{
13 static const char origin[] = {
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020014 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
15 [DSO_BINARY_TYPE__VMLINUX] = 'v',
16 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
17 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
18 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
19 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
20 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
21 [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
22 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
23 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
24 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090025 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020026 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
27 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
Namhyung Kimc00c48f2014-11-04 10:14:27 +090028 [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020029 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
Jiri Olsacdd059d2012-10-27 23:18:32 +020030 };
31
32 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
33 return '!';
34 return origin[dso->symtab_type];
35}
36
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -030037int dso__read_binary_type_filename(const struct dso *dso,
38 enum dso_binary_type type,
39 char *root_dir, char *filename, size_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +020040{
41 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
42 int ret = 0;
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030043 size_t len;
Jiri Olsacdd059d2012-10-27 23:18:32 +020044
45 switch (type) {
46 case DSO_BINARY_TYPE__DEBUGLINK: {
47 char *debuglink;
48
Victor Kamenskydc6254c2015-01-26 22:34:02 -080049 len = __symbol__join_symfs(filename, size, dso->long_name);
50 debuglink = filename + len;
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030051 while (debuglink != filename && *debuglink != '/')
Jiri Olsacdd059d2012-10-27 23:18:32 +020052 debuglink--;
53 if (*debuglink == '/')
54 debuglink++;
Victor Kamenskydc6254c2015-01-26 22:34:02 -080055 ret = filename__read_debuglink(filename, debuglink,
Stephane Eranian0d3dc5e2014-02-20 10:32:55 +090056 size - (debuglink - filename));
Jiri Olsacdd059d2012-10-27 23:18:32 +020057 }
58 break;
59 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
60 /* skip the locally configured cache if a symfs is given */
61 if (symbol_conf.symfs[0] ||
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030062 (dso__build_id_filename(dso, filename, size) == NULL))
Jiri Olsacdd059d2012-10-27 23:18:32 +020063 ret = -1;
64 break;
65
66 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030067 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
68 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +020069 break;
70
71 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030072 len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
73 snprintf(filename + len, size - len, "%s", dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +020074 break;
75
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020076 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
77 {
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -030078 const char *last_slash;
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020079 size_t dir_size;
80
81 last_slash = dso->long_name + dso->long_name_len;
82 while (last_slash != dso->long_name && *last_slash != '/')
83 last_slash--;
84
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -030085 len = __symbol__join_symfs(filename, size, "");
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020086 dir_size = last_slash - dso->long_name + 2;
87 if (dir_size > (size - len)) {
88 ret = -1;
89 break;
90 }
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -030091 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
92 len += scnprintf(filename + len , size - len, ".debug%s",
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020093 last_slash);
94 break;
95 }
96
Jiri Olsacdd059d2012-10-27 23:18:32 +020097 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
98 if (!dso->has_build_id) {
99 ret = -1;
100 break;
101 }
102
103 build_id__sprintf(dso->build_id,
104 sizeof(dso->build_id),
105 build_id_hex);
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300106 len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
107 snprintf(filename + len, size - len, "%.2s/%s.debug",
108 build_id_hex, build_id_hex + 2);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200109 break;
110
Adrian Hunter39b12f782013-08-07 14:38:47 +0300111 case DSO_BINARY_TYPE__VMLINUX:
112 case DSO_BINARY_TYPE__GUEST_VMLINUX:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200113 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300114 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200115 break;
116
117 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900118 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300119 path__join3(filename, size, symbol_conf.symfs,
120 root_dir, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200121 break;
122
123 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900124 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -0300125 __symbol__join_symfs(filename, size, dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200126 break;
127
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300128 case DSO_BINARY_TYPE__KCORE:
129 case DSO_BINARY_TYPE__GUEST_KCORE:
Arnaldo Carvalho de Melo7d2a5122013-12-10 16:02:50 -0300130 snprintf(filename, size, "%s", dso->long_name);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300131 break;
132
Jiri Olsacdd059d2012-10-27 23:18:32 +0200133 default:
134 case DSO_BINARY_TYPE__KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200135 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
Jiri Olsacdd059d2012-10-27 23:18:32 +0200136 case DSO_BINARY_TYPE__JAVA_JIT:
137 case DSO_BINARY_TYPE__NOT_FOUND:
138 ret = -1;
139 break;
140 }
141
142 return ret;
143}
144
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900145static const struct {
146 const char *fmt;
147 int (*decompress)(const char *input, int output);
148} compressions[] = {
Namhyung Kime92ce122014-10-31 16:51:38 +0900149#ifdef HAVE_ZLIB_SUPPORT
150 { "gz", gzip_decompress_to_file },
151#endif
Jiri Olsa80a32e5b2015-01-29 13:29:39 +0100152#ifdef HAVE_LZMA_SUPPORT
153 { "xz", lzma_decompress_to_file },
154#endif
Namhyung Kime92ce122014-10-31 16:51:38 +0900155 { NULL, NULL },
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900156};
157
158bool is_supported_compression(const char *ext)
159{
160 unsigned i;
161
162 for (i = 0; compressions[i].fmt; i++) {
163 if (!strcmp(ext, compressions[i].fmt))
164 return true;
165 }
166 return false;
167}
168
Jiri Olsae746b3e2015-02-12 22:16:34 +0100169bool is_kernel_module(const char *pathname)
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900170{
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100171 struct kmod_path m;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900172
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100173 if (kmod_path__parse(&m, pathname))
174 return NULL;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900175
Jiri Olsa8dee9ff112015-02-12 15:56:21 +0100176 return m.kmod;
Namhyung Kimc00c48f2014-11-04 10:14:27 +0900177}
178
179bool decompress_to_file(const char *ext, const char *filename, int output_fd)
180{
181 unsigned i;
182
183 for (i = 0; compressions[i].fmt; i++) {
184 if (!strcmp(ext, compressions[i].fmt))
185 return !compressions[i].decompress(filename,
186 output_fd);
187 }
188 return false;
189}
190
191bool dso__needs_decompress(struct dso *dso)
192{
193 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
194 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
195}
196
Jiri Olsaeba51022014-04-30 15:00:59 +0200197/*
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100198 * Parses kernel module specified in @path and updates
199 * @m argument like:
200 *
201 * @comp - true if @path contains supported compression suffix,
202 * false otherwise
203 * @kmod - true if @path contains '.ko' suffix in right position,
204 * false otherwise
205 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
206 * of the kernel module without suffixes, otherwise strudup-ed
207 * base name of @path
208 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
209 * the compression suffix
210 *
211 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
212 */
213int __kmod_path__parse(struct kmod_path *m, const char *path,
214 bool alloc_name, bool alloc_ext)
215{
216 const char *name = strrchr(path, '/');
217 const char *ext = strrchr(path, '.');
218
219 memset(m, 0x0, sizeof(*m));
220 name = name ? name + 1 : path;
221
222 /* No extension, just return name. */
223 if (ext == NULL) {
224 if (alloc_name) {
225 m->name = strdup(name);
226 return m->name ? 0 : -ENOMEM;
227 }
228 return 0;
229 }
230
231 if (is_supported_compression(ext + 1)) {
232 m->comp = true;
233 ext -= 3;
234 }
235
236 /* Check .ko extension only if there's enough name left. */
237 if (ext > name)
238 m->kmod = !strncmp(ext, ".ko", 3);
239
240 if (alloc_name) {
241 if (m->kmod) {
242 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
243 return -ENOMEM;
244 } else {
245 if (asprintf(&m->name, "%s", name) == -1)
246 return -ENOMEM;
247 }
248
249 strxfrchar(m->name, '-', '_');
250 }
251
252 if (alloc_ext && m->comp) {
253 m->ext = strdup(ext + 4);
254 if (!m->ext) {
255 free((void *) m->name);
256 return -ENOMEM;
257 }
258 }
259
260 return 0;
261}
262
263/*
Jiri Olsabda6ee42014-04-30 15:25:10 +0200264 * Global list of open DSOs and the counter.
Jiri Olsaeba51022014-04-30 15:00:59 +0200265 */
266static LIST_HEAD(dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200267static long dso__data_open_cnt;
Jiri Olsaeba51022014-04-30 15:00:59 +0200268
269static void dso__list_add(struct dso *dso)
270{
271 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200272 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200273}
274
275static void dso__list_del(struct dso *dso)
276{
277 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200278 WARN_ONCE(dso__data_open_cnt <= 0,
279 "DSO data fd counter out of bounds.");
280 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200281}
282
Jiri Olsaa08cae02014-05-07 21:35:02 +0200283static void close_first_dso(void);
284
285static int do_open(char *name)
286{
287 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000288 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200289
290 do {
291 fd = open(name, O_RDONLY);
292 if (fd >= 0)
293 return fd;
294
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900295 pr_debug("dso open failed: %s\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000296 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200297 if (!dso__data_open_cnt || errno != EMFILE)
298 break;
299
300 close_first_dso();
301 } while (1);
302
303 return -1;
304}
305
Jiri Olsaeba51022014-04-30 15:00:59 +0200306static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200307{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200308 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300309 char *root_dir = (char *)"";
310 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200311
Jiri Olsacdd059d2012-10-27 23:18:32 +0200312 if (!name)
313 return -ENOMEM;
314
315 if (machine)
316 root_dir = machine->root_dir;
317
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300318 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300319 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200320 free(name);
321 return -EINVAL;
322 }
323
Jiri Olsaa08cae02014-05-07 21:35:02 +0200324 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200325 free(name);
326 return fd;
327}
328
Jiri Olsac6580452014-04-30 15:47:27 +0200329static void check_data_close(void);
330
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200331/**
332 * dso_close - Open DSO data file
333 * @dso: dso object
334 *
335 * Open @dso's data file descriptor and updates
336 * list/count of open DSO objects.
337 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200338static int open_dso(struct dso *dso, struct machine *machine)
339{
340 int fd = __open_dso(dso, machine);
341
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300342 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200343 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200344 /*
345 * Check if we crossed the allowed number
346 * of opened DSOs and close one if needed.
347 */
348 check_data_close();
349 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200350
351 return fd;
352}
353
354static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200355{
356 if (dso->data.fd >= 0) {
357 close(dso->data.fd);
358 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200359 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200360 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200361 }
362}
363
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200364/**
365 * dso_close - Close DSO data file
366 * @dso: dso object
367 *
368 * Close @dso's data file descriptor and updates
369 * list/count of open DSO objects.
370 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200371static void close_dso(struct dso *dso)
372{
373 close_data_fd(dso);
374}
375
Jiri Olsac6580452014-04-30 15:47:27 +0200376static void close_first_dso(void)
377{
378 struct dso *dso;
379
380 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
381 close_dso(dso);
382}
383
384static rlim_t get_fd_limit(void)
385{
386 struct rlimit l;
387 rlim_t limit = 0;
388
389 /* Allow half of the current open fd limit. */
390 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
391 if (l.rlim_cur == RLIM_INFINITY)
392 limit = l.rlim_cur;
393 else
394 limit = l.rlim_cur / 2;
395 } else {
396 pr_err("failed to get fd limit\n");
397 limit = 1;
398 }
399
400 return limit;
401}
402
403static bool may_cache_fd(void)
404{
405 static rlim_t limit;
406
407 if (!limit)
408 limit = get_fd_limit();
409
410 if (limit == RLIM_INFINITY)
411 return true;
412
413 return limit > (rlim_t) dso__data_open_cnt;
414}
415
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200416/*
417 * Check and close LRU dso if we crossed allowed limit
418 * for opened dso file descriptors. The limit is half
419 * of the RLIMIT_NOFILE files opened.
420*/
Jiri Olsac6580452014-04-30 15:47:27 +0200421static void check_data_close(void)
422{
423 bool cache_fd = may_cache_fd();
424
425 if (!cache_fd)
426 close_first_dso();
427}
428
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200429/**
430 * dso__data_close - Close DSO data file
431 * @dso: dso object
432 *
433 * External interface to close @dso's data file descriptor.
434 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200435void dso__data_close(struct dso *dso)
436{
437 close_dso(dso);
438}
439
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200440/**
441 * dso__data_fd - Get dso's data file descriptor
442 * @dso: dso object
443 * @machine: machine object
444 *
445 * External interface to find dso's file, open it and
446 * returns file descriptor.
447 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200448int dso__data_fd(struct dso *dso, struct machine *machine)
449{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300450 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200451 DSO_BINARY_TYPE__BUILD_ID_CACHE,
452 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
453 DSO_BINARY_TYPE__NOT_FOUND,
454 };
455 int i = 0;
456
Adrian Hunterc27697d2014-07-22 16:17:18 +0300457 if (dso->data.status == DSO_DATA_STATUS_ERROR)
458 return -1;
459
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200460 if (dso->data.fd >= 0)
Adrian Hunterc27697d2014-07-22 16:17:18 +0300461 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200462
463 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
464 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300465 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200466 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200467
468 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300469 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200470
Adrian Hunterc27697d2014-07-22 16:17:18 +0300471 dso->data.fd = open_dso(dso, machine);
472 if (dso->data.fd >= 0)
473 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200474
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300475 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300476out:
477 if (dso->data.fd >= 0)
478 dso->data.status = DSO_DATA_STATUS_OK;
479 else
480 dso->data.status = DSO_DATA_STATUS_ERROR;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200481
Adrian Hunterc27697d2014-07-22 16:17:18 +0300482 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200483}
484
Adrian Hunter288be942014-07-22 16:17:19 +0300485bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
486{
487 u32 flag = 1 << by;
488
489 if (dso->data.status_seen & flag)
490 return true;
491
492 dso->data.status_seen |= flag;
493
494 return false;
495}
496
Jiri Olsacdd059d2012-10-27 23:18:32 +0200497static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900498dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200499{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900500 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200501 struct rb_node *next = rb_first(root);
502
Namhyung Kim8e67b722015-05-18 09:30:41 +0900503 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200504 while (next) {
505 struct dso_cache *cache;
506
507 cache = rb_entry(next, struct dso_cache, rb_node);
508 next = rb_next(&cache->rb_node);
509 rb_erase(&cache->rb_node, root);
510 free(cache);
511 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900512 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200513}
514
Namhyung Kim8e67b722015-05-18 09:30:41 +0900515static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200516{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900517 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300518 struct rb_node * const *p = &root->rb_node;
519 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200520 struct dso_cache *cache;
521
522 while (*p != NULL) {
523 u64 end;
524
525 parent = *p;
526 cache = rb_entry(parent, struct dso_cache, rb_node);
527 end = cache->offset + DSO__DATA_CACHE_SIZE;
528
529 if (offset < cache->offset)
530 p = &(*p)->rb_left;
531 else if (offset >= end)
532 p = &(*p)->rb_right;
533 else
534 return cache;
535 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900536
Jiri Olsacdd059d2012-10-27 23:18:32 +0200537 return NULL;
538}
539
Namhyung Kim8e67b722015-05-18 09:30:41 +0900540static struct dso_cache *
541dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200542{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900543 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200544 struct rb_node **p = &root->rb_node;
545 struct rb_node *parent = NULL;
546 struct dso_cache *cache;
547 u64 offset = new->offset;
548
Namhyung Kim8e67b722015-05-18 09:30:41 +0900549 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200550 while (*p != NULL) {
551 u64 end;
552
553 parent = *p;
554 cache = rb_entry(parent, struct dso_cache, rb_node);
555 end = cache->offset + DSO__DATA_CACHE_SIZE;
556
557 if (offset < cache->offset)
558 p = &(*p)->rb_left;
559 else if (offset >= end)
560 p = &(*p)->rb_right;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900561 else
562 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200563 }
564
565 rb_link_node(&new->rb_node, parent, p);
566 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900567
568 cache = NULL;
569out:
570 pthread_mutex_unlock(&dso->lock);
571 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200572}
573
574static ssize_t
575dso_cache__memcpy(struct dso_cache *cache, u64 offset,
576 u8 *data, u64 size)
577{
578 u64 cache_offset = offset - cache->offset;
579 u64 cache_size = min(cache->size - cache_offset, size);
580
581 memcpy(data, cache->data + cache_offset, cache_size);
582 return cache_size;
583}
584
585static ssize_t
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200586dso_cache__read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200587{
588 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900589 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200590 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200591
592 do {
593 u64 cache_offset;
594
595 ret = -ENOMEM;
596
597 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
598 if (!cache)
599 break;
600
601 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200602
Namhyung Kimc52686f2015-01-29 17:02:01 -0300603 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200604 if (ret <= 0)
605 break;
606
607 cache->offset = cache_offset;
608 cache->size = ret;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900609 old = dso_cache__insert(dso, cache);
610 if (old) {
611 /* we lose the race */
612 free(cache);
613 cache = old;
614 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200615
616 ret = dso_cache__memcpy(cache, offset, data, size);
617
618 } while (0);
619
620 if (ret <= 0)
621 free(cache);
622
Jiri Olsacdd059d2012-10-27 23:18:32 +0200623 return ret;
624}
625
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200626static ssize_t dso_cache_read(struct dso *dso, u64 offset,
627 u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200628{
629 struct dso_cache *cache;
630
Namhyung Kim8e67b722015-05-18 09:30:41 +0900631 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200632 if (cache)
633 return dso_cache__memcpy(cache, offset, data, size);
634 else
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200635 return dso_cache__read(dso, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200636}
637
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200638/*
639 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
640 * in the rb_tree. Any read to already cached data is served
641 * by cached data.
642 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200643static ssize_t cached_read(struct dso *dso, u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200644{
645 ssize_t r = 0;
646 u8 *p = data;
647
648 do {
649 ssize_t ret;
650
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200651 ret = dso_cache_read(dso, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200652 if (ret < 0)
653 return ret;
654
655 /* Reached EOF, return what we have. */
656 if (!ret)
657 break;
658
659 BUG_ON(ret > size);
660
661 r += ret;
662 p += ret;
663 offset += ret;
664 size -= ret;
665
666 } while (size);
667
668 return r;
669}
670
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200671static int data_file_size(struct dso *dso)
672{
673 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000674 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200675
676 if (!dso->data.file_size) {
677 if (fstat(dso->data.fd, &st)) {
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000678 pr_err("dso mmap failed, fstat: %s\n",
679 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200680 return -1;
681 }
682 dso->data.file_size = st.st_size;
683 }
684
685 return 0;
686}
687
Adrian Hunter6d363452014-07-22 16:17:35 +0300688/**
689 * dso__data_size - Return dso data size
690 * @dso: dso object
691 * @machine: machine object
692 *
693 * Return: dso data size
694 */
695off_t dso__data_size(struct dso *dso, struct machine *machine)
696{
697 int fd;
698
699 fd = dso__data_fd(dso, machine);
700 if (fd < 0)
701 return fd;
702
703 if (data_file_size(dso))
704 return -1;
705
706 /* For now just estimate dso data size is close to file size */
707 return dso->data.file_size;
708}
709
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200710static ssize_t data_read_offset(struct dso *dso, u64 offset,
711 u8 *data, ssize_t size)
712{
713 if (data_file_size(dso))
714 return -1;
715
716 /* Check the offset sanity. */
717 if (offset > dso->data.file_size)
718 return -1;
719
720 if (offset + size < offset)
721 return -1;
722
723 return cached_read(dso, offset, data, size);
724}
725
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200726/**
727 * dso__data_read_offset - Read data from dso file offset
728 * @dso: dso object
729 * @machine: machine object
730 * @offset: file offset
731 * @data: buffer to store data
732 * @size: size of the @data buffer
733 *
734 * External interface to read data from dso file offset. Open
735 * dso data file and use cached_read to get the data.
736 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200737ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
738 u64 offset, u8 *data, ssize_t size)
739{
740 if (dso__data_fd(dso, machine) < 0)
741 return -1;
742
743 return data_read_offset(dso, offset, data, size);
744}
745
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200746/**
747 * dso__data_read_addr - Read data from dso address
748 * @dso: dso object
749 * @machine: machine object
750 * @add: virtual memory address
751 * @data: buffer to store data
752 * @size: size of the @data buffer
753 *
754 * External interface to read data from dso address.
755 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200756ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
757 struct machine *machine, u64 addr,
758 u8 *data, ssize_t size)
759{
760 u64 offset = map->map_ip(map, addr);
761 return dso__data_read_offset(dso, machine, offset, data, size);
762}
763
764struct map *dso__new_map(const char *name)
765{
766 struct map *map = NULL;
767 struct dso *dso = dso__new(name);
768
769 if (dso)
770 map = map__new2(0, dso, MAP__FUNCTION);
771
772 return map;
773}
774
775struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
776 const char *short_name, int dso_type)
777{
778 /*
779 * The kernel dso could be created by build_id processing.
780 */
781 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
782
783 /*
784 * We need to run this in all cases, since during the build_id
785 * processing we had no idea this was the kernel dso.
786 */
787 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300788 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200789 dso->kernel = dso_type;
790 }
791
792 return dso;
793}
794
Waiman Long4598a0a2014-09-30 13:36:15 -0400795/*
796 * Find a matching entry and/or link current entry to RB tree.
797 * Either one of the dso or name parameter must be non-NULL or the
798 * function will not work.
799 */
800static struct dso *dso__findlink_by_longname(struct rb_root *root,
801 struct dso *dso, const char *name)
802{
803 struct rb_node **p = &root->rb_node;
804 struct rb_node *parent = NULL;
805
806 if (!name)
807 name = dso->long_name;
808 /*
809 * Find node with the matching name
810 */
811 while (*p) {
812 struct dso *this = rb_entry(*p, struct dso, rb_node);
813 int rc = strcmp(name, this->long_name);
814
815 parent = *p;
816 if (rc == 0) {
817 /*
818 * In case the new DSO is a duplicate of an existing
819 * one, print an one-time warning & put the new entry
820 * at the end of the list of duplicates.
821 */
822 if (!dso || (dso == this))
823 return this; /* Find matching dso */
824 /*
825 * The core kernel DSOs may have duplicated long name.
826 * In this case, the short name should be different.
827 * Comparing the short names to differentiate the DSOs.
828 */
829 rc = strcmp(dso->short_name, this->short_name);
830 if (rc == 0) {
831 pr_err("Duplicated dso name: %s\n", name);
832 return NULL;
833 }
834 }
835 if (rc < 0)
836 p = &parent->rb_left;
837 else
838 p = &parent->rb_right;
839 }
840 if (dso) {
841 /* Add new node and rebalance tree */
842 rb_link_node(&dso->rb_node, parent, p);
843 rb_insert_color(&dso->rb_node, root);
844 }
845 return NULL;
846}
847
848static inline struct dso *
849dso__find_by_longname(const struct rb_root *root, const char *name)
850{
851 return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
852}
853
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300854void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200855{
856 if (name == NULL)
857 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300858
859 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300860 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300861
862 dso->long_name = name;
863 dso->long_name_len = strlen(name);
864 dso->long_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200865}
866
Adrian Hunter58a98c92013-12-10 11:11:46 -0300867void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200868{
869 if (name == NULL)
870 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -0300871
872 if (dso->short_name_allocated)
873 free((char *)dso->short_name);
874
875 dso->short_name = name;
876 dso->short_name_len = strlen(name);
877 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200878}
879
880static void dso__set_basename(struct dso *dso)
881{
Stephane Eranianac5e7f82013-12-05 19:26:42 +0100882 /*
883 * basename() may modify path buffer, so we must pass
884 * a copy.
885 */
886 char *base, *lname = strdup(dso->long_name);
887
888 if (!lname)
889 return;
890
891 /*
892 * basename() may return a pointer to internal
893 * storage which is reused in subsequent calls
894 * so copy the result.
895 */
896 base = strdup(basename(lname));
897
898 free(lname);
899
900 if (!base)
901 return;
902
903 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200904}
905
906int dso__name_len(const struct dso *dso)
907{
908 if (!dso)
909 return strlen("[unknown]");
910 if (verbose)
911 return dso->long_name_len;
912
913 return dso->short_name_len;
914}
915
916bool dso__loaded(const struct dso *dso, enum map_type type)
917{
918 return dso->loaded & (1 << type);
919}
920
921bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
922{
923 return dso->sorted_by_name & (1 << type);
924}
925
926void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
927{
928 dso->sorted_by_name |= (1 << type);
929}
930
931struct dso *dso__new(const char *name)
932{
933 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
934
935 if (dso != NULL) {
936 int i;
937 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300938 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -0300939 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200940 for (i = 0; i < MAP__NR_TYPES; ++i)
941 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200942 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200943 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +0300944 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200945 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300946 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300947 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200948 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300949 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200950 dso->sorted_by_name = 0;
951 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900952 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +0200953 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200954 dso->kernel = DSO_TYPE_USER;
955 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -0400956 RB_CLEAR_NODE(&dso->rb_node);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200957 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +0200958 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +0900959 pthread_mutex_init(&dso->lock, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200960 }
961
962 return dso;
963}
964
965void dso__delete(struct dso *dso)
966{
967 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -0400968
969 if (!RB_EMPTY_NODE(&dso->rb_node))
970 pr_err("DSO %s is still in rbtree when being deleted!\n",
971 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200972 for (i = 0; i < MAP__NR_TYPES; ++i)
973 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300974
975 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300976 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300977 dso->short_name_allocated = false;
978 }
979
980 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300981 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -0300982 dso->long_name_allocated = false;
983 }
984
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200985 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +0300986 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900987 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +0200988 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300989 zfree(&dso->symsrc_filename);
Namhyung Kim4a936ed2015-05-18 09:30:40 +0900990 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200991 free(dso);
992}
993
994void dso__set_build_id(struct dso *dso, void *build_id)
995{
996 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
997 dso->has_build_id = 1;
998}
999
1000bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1001{
1002 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1003}
1004
1005void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1006{
1007 char path[PATH_MAX];
1008
1009 if (machine__is_default_guest(machine))
1010 return;
1011 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1012 if (sysfs__read_build_id(path, dso->build_id,
1013 sizeof(dso->build_id)) == 0)
1014 dso->has_build_id = true;
1015}
1016
1017int dso__kernel_module_get_build_id(struct dso *dso,
1018 const char *root_dir)
1019{
1020 char filename[PATH_MAX];
1021 /*
1022 * kernel module short names are of the form "[module]" and
1023 * we need just "module" here.
1024 */
1025 const char *name = dso->short_name + 1;
1026
1027 snprintf(filename, sizeof(filename),
1028 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1029 root_dir, (int)strlen(name) - 1, name);
1030
1031 if (sysfs__read_build_id(filename, dso->build_id,
1032 sizeof(dso->build_id)) == 0)
1033 dso->has_build_id = true;
1034
1035 return 0;
1036}
1037
1038bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1039{
1040 bool have_build_id = false;
1041 struct dso *pos;
1042
1043 list_for_each_entry(pos, head, node) {
1044 if (with_hits && !pos->hit)
1045 continue;
1046 if (pos->has_build_id) {
1047 have_build_id = true;
1048 continue;
1049 }
1050 if (filename__read_build_id(pos->long_name, pos->build_id,
1051 sizeof(pos->build_id)) > 0) {
1052 have_build_id = true;
1053 pos->has_build_id = true;
1054 }
1055 }
1056
1057 return have_build_id;
1058}
1059
Waiman Long8fa7d872014-09-29 16:07:28 -04001060void dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001061{
Waiman Long8fa7d872014-09-29 16:07:28 -04001062 list_add_tail(&dso->node, &dsos->head);
Waiman Long4598a0a2014-09-30 13:36:15 -04001063 dso__findlink_by_longname(&dsos->root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001064}
1065
Waiman Long8fa7d872014-09-29 16:07:28 -04001066struct dso *dsos__find(const struct dsos *dsos, const char *name,
1067 bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001068{
1069 struct dso *pos;
1070
Waiman Longf9ceffb2013-05-09 10:42:48 -04001071 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001072 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001073 if (strcmp(pos->short_name, name) == 0)
1074 return pos;
1075 return NULL;
1076 }
Waiman Long4598a0a2014-09-30 13:36:15 -04001077 return dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001078}
1079
Jiri Olsa701d8d72015-02-12 22:06:09 +01001080struct dso *dsos__addnew(struct dsos *dsos, const char *name)
1081{
1082 struct dso *dso = dso__new(name);
1083
1084 if (dso != NULL) {
1085 dsos__add(dsos, dso);
1086 dso__set_basename(dso);
1087 }
1088 return dso;
1089}
1090
Waiman Long8fa7d872014-09-29 16:07:28 -04001091struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001092{
Waiman Long8fa7d872014-09-29 16:07:28 -04001093 struct dso *dso = dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001094
Jiri Olsa701d8d72015-02-12 22:06:09 +01001095 return dso ? dso : dsos__addnew(dsos, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001096}
1097
1098size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001099 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001100{
1101 struct dso *pos;
1102 size_t ret = 0;
1103
1104 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001105 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001106 continue;
1107 ret += dso__fprintf_buildid(pos, fp);
1108 ret += fprintf(fp, " %s\n", pos->long_name);
1109 }
1110 return ret;
1111}
1112
1113size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1114{
1115 struct dso *pos;
1116 size_t ret = 0;
1117
1118 list_for_each_entry(pos, head, node) {
1119 int i;
1120 for (i = 0; i < MAP__NR_TYPES; ++i)
1121 ret += dso__fprintf(pos, i, fp);
1122 }
1123
1124 return ret;
1125}
1126
1127size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1128{
1129 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1130
1131 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1132 return fprintf(fp, "%s", sbuild_id);
1133}
1134
1135size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1136{
1137 struct rb_node *nd;
1138 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1139
1140 if (dso->short_name != dso->long_name)
1141 ret += fprintf(fp, "%s, ", dso->long_name);
1142 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001143 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001144 ret += dso__fprintf_buildid(dso, fp);
1145 ret += fprintf(fp, ")\n");
1146 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1147 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1148 ret += symbol__fprintf(pos, fp);
1149 }
1150
1151 return ret;
1152}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001153
1154enum dso_type dso__type(struct dso *dso, struct machine *machine)
1155{
1156 int fd;
1157
1158 fd = dso__data_fd(dso, machine);
1159 if (fd < 0)
1160 return DSO__TYPE_UNKNOWN;
1161
1162 return dso__type_fd(fd);
1163}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001164
1165int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1166{
1167 int idx, errnum = dso->load_errno;
1168 /*
1169 * This must have a same ordering as the enum dso_load_errno.
1170 */
1171 static const char *dso_load__error_str[] = {
1172 "Internal tools/perf/ library error",
1173 "Invalid ELF file",
1174 "Can not read build id",
1175 "Mismatching build id",
1176 "Decompression failure",
1177 };
1178
1179 BUG_ON(buflen == 0);
1180
1181 if (errnum >= 0) {
1182 const char *err = strerror_r(errnum, buf, buflen);
1183
1184 if (err != buf)
1185 scnprintf(buf, buflen, "%s", err);
1186
1187 return 0;
1188 }
1189
1190 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1191 return -1;
1192
1193 idx = errnum - __DSO_LOAD_ERRNO__START;
1194 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1195 return 0;
1196}