blob: 1b96c8d18435dc404d77f7fdf87556f55902bc93 [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;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900268static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
Jiri Olsaeba51022014-04-30 15:00:59 +0200269
270static void dso__list_add(struct dso *dso)
271{
272 list_add_tail(&dso->data.open_entry, &dso__data_open);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200273 dso__data_open_cnt++;
Jiri Olsaeba51022014-04-30 15:00:59 +0200274}
275
276static void dso__list_del(struct dso *dso)
277{
278 list_del(&dso->data.open_entry);
Jiri Olsabda6ee42014-04-30 15:25:10 +0200279 WARN_ONCE(dso__data_open_cnt <= 0,
280 "DSO data fd counter out of bounds.");
281 dso__data_open_cnt--;
Jiri Olsaeba51022014-04-30 15:00:59 +0200282}
283
Jiri Olsaa08cae02014-05-07 21:35:02 +0200284static void close_first_dso(void);
285
286static int do_open(char *name)
287{
288 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000289 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa08cae02014-05-07 21:35:02 +0200290
291 do {
292 fd = open(name, O_RDONLY);
293 if (fd >= 0)
294 return fd;
295
Namhyung Kima3c0cc22015-01-30 11:33:29 +0900296 pr_debug("dso open failed: %s\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000297 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa08cae02014-05-07 21:35:02 +0200298 if (!dso__data_open_cnt || errno != EMFILE)
299 break;
300
301 close_first_dso();
302 } while (1);
303
304 return -1;
305}
306
Jiri Olsaeba51022014-04-30 15:00:59 +0200307static int __open_dso(struct dso *dso, struct machine *machine)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200308{
Jiri Olsacdd059d2012-10-27 23:18:32 +0200309 int fd;
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300310 char *root_dir = (char *)"";
311 char *name = malloc(PATH_MAX);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200312
Jiri Olsacdd059d2012-10-27 23:18:32 +0200313 if (!name)
314 return -ENOMEM;
315
316 if (machine)
317 root_dir = machine->root_dir;
318
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300319 if (dso__read_binary_type_filename(dso, dso->binary_type,
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -0300320 root_dir, name, PATH_MAX)) {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200321 free(name);
322 return -EINVAL;
323 }
324
Jiri Olsaa08cae02014-05-07 21:35:02 +0200325 fd = do_open(name);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200326 free(name);
327 return fd;
328}
329
Jiri Olsac6580452014-04-30 15:47:27 +0200330static void check_data_close(void);
331
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200332/**
333 * dso_close - Open DSO data file
334 * @dso: dso object
335 *
336 * Open @dso's data file descriptor and updates
337 * list/count of open DSO objects.
338 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200339static int open_dso(struct dso *dso, struct machine *machine)
340{
341 int fd = __open_dso(dso, machine);
342
Adrian Huntera6f6ae92014-07-17 11:43:09 +0300343 if (fd >= 0) {
Jiri Olsaeba51022014-04-30 15:00:59 +0200344 dso__list_add(dso);
Jiri Olsac6580452014-04-30 15:47:27 +0200345 /*
346 * Check if we crossed the allowed number
347 * of opened DSOs and close one if needed.
348 */
349 check_data_close();
350 }
Jiri Olsaeba51022014-04-30 15:00:59 +0200351
352 return fd;
353}
354
355static void close_data_fd(struct dso *dso)
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200356{
357 if (dso->data.fd >= 0) {
358 close(dso->data.fd);
359 dso->data.fd = -1;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200360 dso->data.file_size = 0;
Jiri Olsaeba51022014-04-30 15:00:59 +0200361 dso__list_del(dso);
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200362 }
363}
364
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200365/**
366 * dso_close - Close DSO data file
367 * @dso: dso object
368 *
369 * Close @dso's data file descriptor and updates
370 * list/count of open DSO objects.
371 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200372static void close_dso(struct dso *dso)
373{
374 close_data_fd(dso);
375}
376
Jiri Olsac6580452014-04-30 15:47:27 +0200377static void close_first_dso(void)
378{
379 struct dso *dso;
380
381 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
382 close_dso(dso);
383}
384
385static rlim_t get_fd_limit(void)
386{
387 struct rlimit l;
388 rlim_t limit = 0;
389
390 /* Allow half of the current open fd limit. */
391 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
392 if (l.rlim_cur == RLIM_INFINITY)
393 limit = l.rlim_cur;
394 else
395 limit = l.rlim_cur / 2;
396 } else {
397 pr_err("failed to get fd limit\n");
398 limit = 1;
399 }
400
401 return limit;
402}
403
404static bool may_cache_fd(void)
405{
406 static rlim_t limit;
407
408 if (!limit)
409 limit = get_fd_limit();
410
411 if (limit == RLIM_INFINITY)
412 return true;
413
414 return limit > (rlim_t) dso__data_open_cnt;
415}
416
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200417/*
418 * Check and close LRU dso if we crossed allowed limit
419 * for opened dso file descriptors. The limit is half
420 * of the RLIMIT_NOFILE files opened.
421*/
Jiri Olsac6580452014-04-30 15:47:27 +0200422static void check_data_close(void)
423{
424 bool cache_fd = may_cache_fd();
425
426 if (!cache_fd)
427 close_first_dso();
428}
429
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200430/**
431 * dso__data_close - Close DSO data file
432 * @dso: dso object
433 *
434 * External interface to close @dso's data file descriptor.
435 */
Jiri Olsaeba51022014-04-30 15:00:59 +0200436void dso__data_close(struct dso *dso)
437{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900438 pthread_mutex_lock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200439 close_dso(dso);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900440 pthread_mutex_unlock(&dso__data_open_lock);
Jiri Olsaeba51022014-04-30 15:00:59 +0200441}
442
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200443/**
444 * dso__data_fd - Get dso's data file descriptor
445 * @dso: dso object
446 * @machine: machine object
447 *
448 * External interface to find dso's file, open it and
449 * returns file descriptor.
450 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200451int dso__data_fd(struct dso *dso, struct machine *machine)
452{
Arnaldo Carvalho de Melo631d34b2013-12-16 16:57:43 -0300453 enum dso_binary_type binary_type_data[] = {
Jiri Olsacdd059d2012-10-27 23:18:32 +0200454 DSO_BINARY_TYPE__BUILD_ID_CACHE,
455 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
456 DSO_BINARY_TYPE__NOT_FOUND,
457 };
458 int i = 0;
459
Adrian Hunterc27697d2014-07-22 16:17:18 +0300460 if (dso->data.status == DSO_DATA_STATUS_ERROR)
461 return -1;
462
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900463 pthread_mutex_lock(&dso__data_open_lock);
464
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200465 if (dso->data.fd >= 0)
Adrian Hunterc27697d2014-07-22 16:17:18 +0300466 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200467
468 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
469 dso->data.fd = open_dso(dso, machine);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300470 goto out;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200471 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200472
473 do {
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300474 dso->binary_type = binary_type_data[i++];
Jiri Olsacdd059d2012-10-27 23:18:32 +0200475
Adrian Hunterc27697d2014-07-22 16:17:18 +0300476 dso->data.fd = open_dso(dso, machine);
477 if (dso->data.fd >= 0)
478 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200479
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300480 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300481out:
482 if (dso->data.fd >= 0)
483 dso->data.status = DSO_DATA_STATUS_OK;
484 else
485 dso->data.status = DSO_DATA_STATUS_ERROR;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200486
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900487 pthread_mutex_unlock(&dso__data_open_lock);
Adrian Hunterc27697d2014-07-22 16:17:18 +0300488 return dso->data.fd;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200489}
490
Adrian Hunter288be942014-07-22 16:17:19 +0300491bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
492{
493 u32 flag = 1 << by;
494
495 if (dso->data.status_seen & flag)
496 return true;
497
498 dso->data.status_seen |= flag;
499
500 return false;
501}
502
Jiri Olsacdd059d2012-10-27 23:18:32 +0200503static void
Namhyung Kim8e67b722015-05-18 09:30:41 +0900504dso_cache__free(struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200505{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900506 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200507 struct rb_node *next = rb_first(root);
508
Namhyung Kim8e67b722015-05-18 09:30:41 +0900509 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200510 while (next) {
511 struct dso_cache *cache;
512
513 cache = rb_entry(next, struct dso_cache, rb_node);
514 next = rb_next(&cache->rb_node);
515 rb_erase(&cache->rb_node, root);
516 free(cache);
517 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900518 pthread_mutex_unlock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200519}
520
Namhyung Kim8e67b722015-05-18 09:30:41 +0900521static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200522{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900523 const struct rb_root *root = &dso->data.cache;
Arnaldo Carvalho de Melo33449962013-12-10 15:46:29 -0300524 struct rb_node * const *p = &root->rb_node;
525 const struct rb_node *parent = NULL;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200526 struct dso_cache *cache;
527
528 while (*p != NULL) {
529 u64 end;
530
531 parent = *p;
532 cache = rb_entry(parent, struct dso_cache, rb_node);
533 end = cache->offset + DSO__DATA_CACHE_SIZE;
534
535 if (offset < cache->offset)
536 p = &(*p)->rb_left;
537 else if (offset >= end)
538 p = &(*p)->rb_right;
539 else
540 return cache;
541 }
Namhyung Kim8e67b722015-05-18 09:30:41 +0900542
Jiri Olsacdd059d2012-10-27 23:18:32 +0200543 return NULL;
544}
545
Namhyung Kim8e67b722015-05-18 09:30:41 +0900546static struct dso_cache *
547dso_cache__insert(struct dso *dso, struct dso_cache *new)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200548{
Namhyung Kim8e67b722015-05-18 09:30:41 +0900549 struct rb_root *root = &dso->data.cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200550 struct rb_node **p = &root->rb_node;
551 struct rb_node *parent = NULL;
552 struct dso_cache *cache;
553 u64 offset = new->offset;
554
Namhyung Kim8e67b722015-05-18 09:30:41 +0900555 pthread_mutex_lock(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200556 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;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900567 else
568 goto out;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200569 }
570
571 rb_link_node(&new->rb_node, parent, p);
572 rb_insert_color(&new->rb_node, root);
Namhyung Kim8e67b722015-05-18 09:30:41 +0900573
574 cache = NULL;
575out:
576 pthread_mutex_unlock(&dso->lock);
577 return cache;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200578}
579
580static ssize_t
581dso_cache__memcpy(struct dso_cache *cache, u64 offset,
582 u8 *data, u64 size)
583{
584 u64 cache_offset = offset - cache->offset;
585 u64 cache_size = min(cache->size - cache_offset, size);
586
587 memcpy(data, cache->data + cache_offset, cache_size);
588 return cache_size;
589}
590
591static ssize_t
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900592dso_cache__read(struct dso *dso, struct machine *machine,
593 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200594{
595 struct dso_cache *cache;
Namhyung Kim8e67b722015-05-18 09:30:41 +0900596 struct dso_cache *old;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200597 ssize_t ret;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200598
599 do {
600 u64 cache_offset;
601
Jiri Olsacdd059d2012-10-27 23:18:32 +0200602 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
603 if (!cache)
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900604 return -ENOMEM;
605
606 pthread_mutex_lock(&dso__data_open_lock);
607
608 /*
609 * dso->data.fd might be closed if other thread opened another
610 * file (dso) due to open file limit (RLIMIT_NOFILE).
611 */
612 if (dso->data.fd < 0) {
613 dso->data.fd = open_dso(dso, machine);
614 if (dso->data.fd < 0) {
615 ret = -errno;
616 dso->data.status = DSO_DATA_STATUS_ERROR;
617 break;
618 }
619 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200620
621 cache_offset = offset & DSO__DATA_CACHE_MASK;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200622
Namhyung Kimc52686f2015-01-29 17:02:01 -0300623 ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200624 if (ret <= 0)
625 break;
626
627 cache->offset = cache_offset;
628 cache->size = ret;
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900629 } while (0);
630
631 pthread_mutex_unlock(&dso__data_open_lock);
632
633 if (ret > 0) {
Namhyung Kim8e67b722015-05-18 09:30:41 +0900634 old = dso_cache__insert(dso, cache);
635 if (old) {
636 /* we lose the race */
637 free(cache);
638 cache = old;
639 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200640
641 ret = dso_cache__memcpy(cache, offset, data, size);
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900642 }
Jiri Olsacdd059d2012-10-27 23:18:32 +0200643
644 if (ret <= 0)
645 free(cache);
646
Jiri Olsacdd059d2012-10-27 23:18:32 +0200647 return ret;
648}
649
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900650static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
651 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200652{
653 struct dso_cache *cache;
654
Namhyung Kim8e67b722015-05-18 09:30:41 +0900655 cache = dso_cache__find(dso, offset);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200656 if (cache)
657 return dso_cache__memcpy(cache, offset, data, size);
658 else
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900659 return dso_cache__read(dso, machine, offset, data, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200660}
661
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200662/*
663 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
664 * in the rb_tree. Any read to already cached data is served
665 * by cached data.
666 */
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900667static ssize_t cached_read(struct dso *dso, struct machine *machine,
668 u64 offset, u8 *data, ssize_t size)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200669{
670 ssize_t r = 0;
671 u8 *p = data;
672
673 do {
674 ssize_t ret;
675
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900676 ret = dso_cache_read(dso, machine, offset, p, size);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200677 if (ret < 0)
678 return ret;
679
680 /* Reached EOF, return what we have. */
681 if (!ret)
682 break;
683
684 BUG_ON(ret > size);
685
686 r += ret;
687 p += ret;
688 offset += ret;
689 size -= ret;
690
691 } while (size);
692
693 return r;
694}
695
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900696static int data_file_size(struct dso *dso, struct machine *machine)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200697{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900698 int ret = 0;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200699 struct stat st;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000700 char sbuf[STRERR_BUFSIZE];
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200701
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900702 if (dso->data.file_size)
703 return 0;
704
705 pthread_mutex_lock(&dso__data_open_lock);
706
707 /*
708 * dso->data.fd might be closed if other thread opened another
709 * file (dso) due to open file limit (RLIMIT_NOFILE).
710 */
711 if (dso->data.fd < 0) {
712 dso->data.fd = open_dso(dso, machine);
713 if (dso->data.fd < 0) {
714 ret = -errno;
715 dso->data.status = DSO_DATA_STATUS_ERROR;
716 goto out;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200717 }
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200718 }
719
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900720 if (fstat(dso->data.fd, &st) < 0) {
721 ret = -errno;
722 pr_err("dso cache fstat failed: %s\n",
723 strerror_r(errno, sbuf, sizeof(sbuf)));
724 dso->data.status = DSO_DATA_STATUS_ERROR;
725 goto out;
726 }
727 dso->data.file_size = st.st_size;
728
729out:
730 pthread_mutex_unlock(&dso__data_open_lock);
731 return ret;
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200732}
733
Adrian Hunter6d363452014-07-22 16:17:35 +0300734/**
735 * dso__data_size - Return dso data size
736 * @dso: dso object
737 * @machine: machine object
738 *
739 * Return: dso data size
740 */
741off_t dso__data_size(struct dso *dso, struct machine *machine)
742{
743 int fd;
744
745 fd = dso__data_fd(dso, machine);
746 if (fd < 0)
747 return fd;
748
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900749 if (data_file_size(dso, machine))
Adrian Hunter6d363452014-07-22 16:17:35 +0300750 return -1;
751
752 /* For now just estimate dso data size is close to file size */
753 return dso->data.file_size;
754}
755
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900756static ssize_t data_read_offset(struct dso *dso, struct machine *machine,
757 u64 offset, u8 *data, ssize_t size)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200758{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900759 if (data_file_size(dso, machine))
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200760 return -1;
761
762 /* Check the offset sanity. */
763 if (offset > dso->data.file_size)
764 return -1;
765
766 if (offset + size < offset)
767 return -1;
768
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900769 return cached_read(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200770}
771
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200772/**
773 * dso__data_read_offset - Read data from dso file offset
774 * @dso: dso object
775 * @machine: machine object
776 * @offset: file offset
777 * @data: buffer to store data
778 * @size: size of the @data buffer
779 *
780 * External interface to read data from dso file offset. Open
781 * dso data file and use cached_read to get the data.
782 */
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200783ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
784 u64 offset, u8 *data, ssize_t size)
785{
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900786 if (dso->data.status == DSO_DATA_STATUS_ERROR)
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200787 return -1;
788
Namhyung Kim33bdedc2015-05-18 09:30:42 +0900789 return data_read_offset(dso, machine, offset, data, size);
Jiri Olsac3fbd2a2014-05-07 18:51:41 +0200790}
791
Jiri Olsac1f9aa02014-05-07 21:09:59 +0200792/**
793 * dso__data_read_addr - Read data from dso address
794 * @dso: dso object
795 * @machine: machine object
796 * @add: virtual memory address
797 * @data: buffer to store data
798 * @size: size of the @data buffer
799 *
800 * External interface to read data from dso address.
801 */
Jiri Olsacdd059d2012-10-27 23:18:32 +0200802ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
803 struct machine *machine, u64 addr,
804 u8 *data, ssize_t size)
805{
806 u64 offset = map->map_ip(map, addr);
807 return dso__data_read_offset(dso, machine, offset, data, size);
808}
809
810struct map *dso__new_map(const char *name)
811{
812 struct map *map = NULL;
813 struct dso *dso = dso__new(name);
814
815 if (dso)
816 map = map__new2(0, dso, MAP__FUNCTION);
817
818 return map;
819}
820
821struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
822 const char *short_name, int dso_type)
823{
824 /*
825 * The kernel dso could be created by build_id processing.
826 */
827 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
828
829 /*
830 * We need to run this in all cases, since during the build_id
831 * processing we had no idea this was the kernel dso.
832 */
833 if (dso != NULL) {
Adrian Hunter58a98c92013-12-10 11:11:46 -0300834 dso__set_short_name(dso, short_name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200835 dso->kernel = dso_type;
836 }
837
838 return dso;
839}
840
Waiman Long4598a0a2014-09-30 13:36:15 -0400841/*
842 * Find a matching entry and/or link current entry to RB tree.
843 * Either one of the dso or name parameter must be non-NULL or the
844 * function will not work.
845 */
846static struct dso *dso__findlink_by_longname(struct rb_root *root,
847 struct dso *dso, const char *name)
848{
849 struct rb_node **p = &root->rb_node;
850 struct rb_node *parent = NULL;
851
852 if (!name)
853 name = dso->long_name;
854 /*
855 * Find node with the matching name
856 */
857 while (*p) {
858 struct dso *this = rb_entry(*p, struct dso, rb_node);
859 int rc = strcmp(name, this->long_name);
860
861 parent = *p;
862 if (rc == 0) {
863 /*
864 * In case the new DSO is a duplicate of an existing
865 * one, print an one-time warning & put the new entry
866 * at the end of the list of duplicates.
867 */
868 if (!dso || (dso == this))
869 return this; /* Find matching dso */
870 /*
871 * The core kernel DSOs may have duplicated long name.
872 * In this case, the short name should be different.
873 * Comparing the short names to differentiate the DSOs.
874 */
875 rc = strcmp(dso->short_name, this->short_name);
876 if (rc == 0) {
877 pr_err("Duplicated dso name: %s\n", name);
878 return NULL;
879 }
880 }
881 if (rc < 0)
882 p = &parent->rb_left;
883 else
884 p = &parent->rb_right;
885 }
886 if (dso) {
887 /* Add new node and rebalance tree */
888 rb_link_node(&dso->rb_node, parent, p);
889 rb_insert_color(&dso->rb_node, root);
890 }
891 return NULL;
892}
893
894static inline struct dso *
895dso__find_by_longname(const struct rb_root *root, const char *name)
896{
897 return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
898}
899
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300900void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200901{
902 if (name == NULL)
903 return;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300904
905 if (dso->long_name_allocated)
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300906 free((char *)dso->long_name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300907
908 dso->long_name = name;
909 dso->long_name_len = strlen(name);
910 dso->long_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200911}
912
Adrian Hunter58a98c92013-12-10 11:11:46 -0300913void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
Jiri Olsacdd059d2012-10-27 23:18:32 +0200914{
915 if (name == NULL)
916 return;
Adrian Hunter58a98c92013-12-10 11:11:46 -0300917
918 if (dso->short_name_allocated)
919 free((char *)dso->short_name);
920
921 dso->short_name = name;
922 dso->short_name_len = strlen(name);
923 dso->short_name_allocated = name_allocated;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200924}
925
926static void dso__set_basename(struct dso *dso)
927{
Stephane Eranianac5e7f82013-12-05 19:26:42 +0100928 /*
929 * basename() may modify path buffer, so we must pass
930 * a copy.
931 */
932 char *base, *lname = strdup(dso->long_name);
933
934 if (!lname)
935 return;
936
937 /*
938 * basename() may return a pointer to internal
939 * storage which is reused in subsequent calls
940 * so copy the result.
941 */
942 base = strdup(basename(lname));
943
944 free(lname);
945
946 if (!base)
947 return;
948
949 dso__set_short_name(dso, base, true);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200950}
951
952int dso__name_len(const struct dso *dso)
953{
954 if (!dso)
955 return strlen("[unknown]");
956 if (verbose)
957 return dso->long_name_len;
958
959 return dso->short_name_len;
960}
961
962bool dso__loaded(const struct dso *dso, enum map_type type)
963{
964 return dso->loaded & (1 << type);
965}
966
967bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
968{
969 return dso->sorted_by_name & (1 << type);
970}
971
972void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
973{
974 dso->sorted_by_name |= (1 << type);
975}
976
977struct dso *dso__new(const char *name)
978{
979 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
980
981 if (dso != NULL) {
982 int i;
983 strcpy(dso->name, name);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300984 dso__set_long_name(dso, dso->name, false);
Adrian Hunter58a98c92013-12-10 11:11:46 -0300985 dso__set_short_name(dso, dso->name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200986 for (i = 0; i < MAP__NR_TYPES; ++i)
987 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
Jiri Olsaca40e2a2014-05-07 18:30:45 +0200988 dso->data.cache = RB_ROOT;
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +0200989 dso->data.fd = -1;
Adrian Hunterc27697d2014-07-22 16:17:18 +0300990 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200991 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -0300992 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +0300993 dso->is_64_bit = (sizeof(void *) == 8);
Jiri Olsacdd059d2012-10-27 23:18:32 +0200994 dso->loaded = 0;
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300995 dso->rel = 0;
Jiri Olsacdd059d2012-10-27 23:18:32 +0200996 dso->sorted_by_name = 0;
997 dso->has_build_id = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900998 dso->has_srcline = 1;
Adrian Hunter906049c82013-12-03 09:23:10 +0200999 dso->a2l_fails = 1;
Jiri Olsacdd059d2012-10-27 23:18:32 +02001000 dso->kernel = DSO_TYPE_USER;
1001 dso->needs_swap = DSO_SWAP__UNSET;
Waiman Long4598a0a2014-09-30 13:36:15 -04001002 RB_CLEAR_NODE(&dso->rb_node);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001003 INIT_LIST_HEAD(&dso->node);
Jiri Olsaeba51022014-04-30 15:00:59 +02001004 INIT_LIST_HEAD(&dso->data.open_entry);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001005 pthread_mutex_init(&dso->lock, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001006 }
1007
1008 return dso;
1009}
1010
1011void dso__delete(struct dso *dso)
1012{
1013 int i;
Waiman Long4598a0a2014-09-30 13:36:15 -04001014
1015 if (!RB_EMPTY_NODE(&dso->rb_node))
1016 pr_err("DSO %s is still in rbtree when being deleted!\n",
1017 dso->long_name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001018 for (i = 0; i < MAP__NR_TYPES; ++i)
1019 symbols__delete(&dso->symbols[i]);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001020
1021 if (dso->short_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001022 zfree((char **)&dso->short_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001023 dso->short_name_allocated = false;
1024 }
1025
1026 if (dso->long_name_allocated) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001027 zfree((char **)&dso->long_name);
Arnaldo Carvalho de Meloee021d42013-12-10 15:26:55 -03001028 dso->long_name_allocated = false;
1029 }
1030
Jiri Olsa53fa8eaa2014-04-28 16:43:43 +02001031 dso__data_close(dso);
Adrian Huntercfe91742015-04-09 18:53:55 +03001032 auxtrace_cache__free(dso->auxtrace_cache);
Namhyung Kim8e67b722015-05-18 09:30:41 +09001033 dso_cache__free(dso);
Adrian Hunter454ff002013-12-03 09:23:07 +02001034 dso__free_a2l(dso);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001035 zfree(&dso->symsrc_filename);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001036 pthread_mutex_destroy(&dso->lock);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001037 free(dso);
1038}
1039
1040void dso__set_build_id(struct dso *dso, void *build_id)
1041{
1042 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
1043 dso->has_build_id = 1;
1044}
1045
1046bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1047{
1048 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1049}
1050
1051void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1052{
1053 char path[PATH_MAX];
1054
1055 if (machine__is_default_guest(machine))
1056 return;
1057 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1058 if (sysfs__read_build_id(path, dso->build_id,
1059 sizeof(dso->build_id)) == 0)
1060 dso->has_build_id = true;
1061}
1062
1063int dso__kernel_module_get_build_id(struct dso *dso,
1064 const char *root_dir)
1065{
1066 char filename[PATH_MAX];
1067 /*
1068 * kernel module short names are of the form "[module]" and
1069 * we need just "module" here.
1070 */
1071 const char *name = dso->short_name + 1;
1072
1073 snprintf(filename, sizeof(filename),
1074 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1075 root_dir, (int)strlen(name) - 1, name);
1076
1077 if (sysfs__read_build_id(filename, dso->build_id,
1078 sizeof(dso->build_id)) == 0)
1079 dso->has_build_id = true;
1080
1081 return 0;
1082}
1083
1084bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1085{
1086 bool have_build_id = false;
1087 struct dso *pos;
1088
1089 list_for_each_entry(pos, head, node) {
1090 if (with_hits && !pos->hit)
1091 continue;
1092 if (pos->has_build_id) {
1093 have_build_id = true;
1094 continue;
1095 }
1096 if (filename__read_build_id(pos->long_name, pos->build_id,
1097 sizeof(pos->build_id)) > 0) {
1098 have_build_id = true;
1099 pos->has_build_id = true;
1100 }
1101 }
1102
1103 return have_build_id;
1104}
1105
Waiman Long8fa7d872014-09-29 16:07:28 -04001106void dsos__add(struct dsos *dsos, struct dso *dso)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001107{
Waiman Long8fa7d872014-09-29 16:07:28 -04001108 list_add_tail(&dso->node, &dsos->head);
Waiman Long4598a0a2014-09-30 13:36:15 -04001109 dso__findlink_by_longname(&dsos->root, dso, NULL);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001110}
1111
Waiman Long8fa7d872014-09-29 16:07:28 -04001112struct dso *dsos__find(const struct dsos *dsos, const char *name,
1113 bool cmp_short)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001114{
1115 struct dso *pos;
1116
Waiman Longf9ceffb2013-05-09 10:42:48 -04001117 if (cmp_short) {
Waiman Long8fa7d872014-09-29 16:07:28 -04001118 list_for_each_entry(pos, &dsos->head, node)
Waiman Longf9ceffb2013-05-09 10:42:48 -04001119 if (strcmp(pos->short_name, name) == 0)
1120 return pos;
1121 return NULL;
1122 }
Waiman Long4598a0a2014-09-30 13:36:15 -04001123 return dso__find_by_longname(&dsos->root, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001124}
1125
Jiri Olsa701d8d72015-02-12 22:06:09 +01001126struct dso *dsos__addnew(struct dsos *dsos, const char *name)
1127{
1128 struct dso *dso = dso__new(name);
1129
1130 if (dso != NULL) {
1131 dsos__add(dsos, dso);
1132 dso__set_basename(dso);
1133 }
1134 return dso;
1135}
1136
Waiman Long8fa7d872014-09-29 16:07:28 -04001137struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001138{
Waiman Long8fa7d872014-09-29 16:07:28 -04001139 struct dso *dso = dsos__find(dsos, name, false);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001140
Jiri Olsa701d8d72015-02-12 22:06:09 +01001141 return dso ? dso : dsos__addnew(dsos, name);
Jiri Olsacdd059d2012-10-27 23:18:32 +02001142}
1143
1144size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001145 bool (skip)(struct dso *dso, int parm), int parm)
Jiri Olsacdd059d2012-10-27 23:18:32 +02001146{
1147 struct dso *pos;
1148 size_t ret = 0;
1149
1150 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -03001151 if (skip && skip(pos, parm))
Jiri Olsacdd059d2012-10-27 23:18:32 +02001152 continue;
1153 ret += dso__fprintf_buildid(pos, fp);
1154 ret += fprintf(fp, " %s\n", pos->long_name);
1155 }
1156 return ret;
1157}
1158
1159size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1160{
1161 struct dso *pos;
1162 size_t ret = 0;
1163
1164 list_for_each_entry(pos, head, node) {
1165 int i;
1166 for (i = 0; i < MAP__NR_TYPES; ++i)
1167 ret += dso__fprintf(pos, i, fp);
1168 }
1169
1170 return ret;
1171}
1172
1173size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1174{
1175 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1176
1177 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1178 return fprintf(fp, "%s", sbuild_id);
1179}
1180
1181size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
1182{
1183 struct rb_node *nd;
1184 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1185
1186 if (dso->short_name != dso->long_name)
1187 ret += fprintf(fp, "%s, ", dso->long_name);
1188 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
Stephane Eranian919d5902012-11-20 10:51:02 +01001189 dso__loaded(dso, type) ? "" : "NOT ");
Jiri Olsacdd059d2012-10-27 23:18:32 +02001190 ret += dso__fprintf_buildid(dso, fp);
1191 ret += fprintf(fp, ")\n");
1192 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
1193 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
1194 ret += symbol__fprintf(pos, fp);
1195 }
1196
1197 return ret;
1198}
Adrian Hunter2b5b8bb2014-07-22 16:17:59 +03001199
1200enum dso_type dso__type(struct dso *dso, struct machine *machine)
1201{
1202 int fd;
1203
1204 fd = dso__data_fd(dso, machine);
1205 if (fd < 0)
1206 return DSO__TYPE_UNKNOWN;
1207
1208 return dso__type_fd(fd);
1209}
Arnaldo Carvalho de Melo18425f12015-03-24 11:49:02 -03001210
1211int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1212{
1213 int idx, errnum = dso->load_errno;
1214 /*
1215 * This must have a same ordering as the enum dso_load_errno.
1216 */
1217 static const char *dso_load__error_str[] = {
1218 "Internal tools/perf/ library error",
1219 "Invalid ELF file",
1220 "Can not read build id",
1221 "Mismatching build id",
1222 "Decompression failure",
1223 };
1224
1225 BUG_ON(buflen == 0);
1226
1227 if (errnum >= 0) {
1228 const char *err = strerror_r(errnum, buf, buflen);
1229
1230 if (err != buf)
1231 scnprintf(buf, buflen, "%s", err);
1232
1233 return 0;
1234 }
1235
1236 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1237 return -1;
1238
1239 idx = errnum - __DSO_LOAD_ERRNO__START;
1240 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1241 return 0;
1242}