Lines Matching refs:dso

26 #include "dso.h"
43 char dso__symtab_origin(const struct dso *dso)
66 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
68 return origin[dso->symtab_type];
71 bool dso__is_object_file(const struct dso *dso)
73 switch (dso->binary_type) {
104 int dso__read_binary_type_filename(const struct dso *dso,
120 len = __symbol__join_symfs(filename, size, dso->long_name);
151 if (dso__build_id_filename(dso, filename, size, false) == NULL)
156 if (dso__build_id_filename(dso, filename, size, true) == NULL)
162 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
167 snprintf(filename + len, size - len, "%s", dso->long_name);
176 if (strlen(dso->long_name) < 9 ||
177 strncmp(dso->long_name, "/usr/lib/", 9)) {
182 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
190 last_slash = dso->long_name + dso->long_name_len;
191 while (last_slash != dso->long_name && *last_slash != '/')
195 dir_size = last_slash - dso->long_name + 2;
200 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
207 if (!dso->has_build_id) {
212 build_id__sprintf(&dso->bid, build_id_hex);
221 __symbol__join_symfs(filename, size, dso->long_name);
227 root_dir, dso->long_name);
232 __symbol__join_symfs(filename, size, dso->long_name);
237 snprintf(filename, size, "%s", dso->long_name);
311 bool dso__needs_decompress(struct dso *dso)
313 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
314 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
360 static int decompress_kmodule(struct dso *dso, const char *name,
363 if (!dso__needs_decompress(dso))
366 if (dso->comp == COMP_ID__NONE)
369 return filename__decompress(name, pathname, len, dso->comp,
370 &dso->load_errno);
373 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
375 return decompress_kmodule(dso, name, NULL, 0);
378 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
381 int fd = decompress_kmodule(dso, name, pathname, len);
467 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
471 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
473 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
477 dso->symtab_type++;
478 dso->comp = m->comp;
481 dso->is_kmod = 1;
482 dso__set_short_name(dso, strdup(m->name), true);
492 static void dso__list_add(struct dso *dso)
494 list_add_tail(&dso->data.open_entry, &dso__data_open);
498 static void dso__list_del(struct dso *dso)
500 list_del_init(&dso->data.open_entry);
518 pr_debug("dso open failed: %s\n",
529 char *dso__filename_with_chroot(const struct dso *dso, const char *filename)
531 return filename_with_chroot(nsinfo__pid(dso->nsinfo), filename);
534 static int __open_dso(struct dso *dso, struct machine *machine)
544 mutex_lock(&dso->lock);
548 if (dso__read_binary_type_filename(dso, dso->binary_type,
555 if (errno != ENOENT || dso->nsinfo == NULL)
558 new_name = dso__filename_with_chroot(dso, name);
566 if (dso__needs_decompress(dso)) {
570 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
571 fd = -dso->load_errno;
585 mutex_unlock(&dso->lock);
594 * @dso: dso object
596 * Open @dso's data file descriptor and updates
599 static int open_dso(struct dso *dso, struct machine *machine)
604 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE) {
605 mutex_lock(&dso->lock);
606 nsinfo__mountns_enter(dso->nsinfo, &nsc);
607 mutex_unlock(&dso->lock);
609 fd = __open_dso(dso, machine);
610 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
614 dso__list_add(dso);
625 static void close_data_fd(struct dso *dso)
627 if (dso->data.fd >= 0) {
628 close(dso->data.fd);
629 dso->data.fd = -1;
630 dso->data.file_size = 0;
631 dso__list_del(dso);
637 * @dso: dso object
639 * Close @dso's data file descriptor and updates
642 static void close_dso(struct dso *dso)
644 close_data_fd(dso);
649 struct dso *dso;
651 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
652 close_dso(dso);
677 * Used only by tests/dso-data.c to reset the environment
698 * Check and close LRU dso if we crossed allowed limit
699 * for opened dso file descriptors. The limit is half
712 * @dso: dso object
714 * External interface to close @dso's data file descriptor.
716 void dso__data_close(struct dso *dso)
719 close_dso(dso);
723 static void try_to_open_dso(struct dso *dso, struct machine *machine)
732 if (dso->data.fd >= 0)
735 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
736 dso->data.fd = open_dso(dso, machine);
741 dso->binary_type = binary_type_data[i++];
743 dso->data.fd = open_dso(dso, machine);
744 if (dso->data.fd >= 0)
747 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
749 if (dso->data.fd >= 0)
750 dso->data.status = DSO_DATA_STATUS_OK;
752 dso->data.status = DSO_DATA_STATUS_ERROR;
756 * dso__data_get_fd - Get dso's data file descriptor
757 * @dso: dso object
760 * External interface to find dso's file, open it and
764 int dso__data_get_fd(struct dso *dso, struct machine *machine)
766 if (dso->data.status == DSO_DATA_STATUS_ERROR)
772 try_to_open_dso(dso, machine);
774 if (dso->data.fd < 0)
777 return dso->data.fd;
780 void dso__data_put_fd(struct dso *dso __maybe_unused)
785 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
789 if (dso->data.status_seen & flag)
792 dso->data.status_seen |= flag;
798 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
805 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
807 dso->data.status = DSO_DATA_STATUS_ERROR;
822 static int bpf_size(struct dso *dso)
826 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
828 dso->data.status = DSO_DATA_STATUS_ERROR;
832 dso->data.file_size = node->info_linear->info.jited_prog_len;
838 dso_cache__free(struct dso *dso)
840 struct rb_root *root = &dso->data.cache;
843 mutex_lock(&dso->lock);
852 mutex_unlock(&dso->lock);
855 static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
857 const struct rb_root *root = &dso->data.cache;
881 dso_cache__insert(struct dso *dso, struct dso_cache *new)
883 struct rb_root *root = &dso->data.cache;
889 mutex_lock(&dso->lock);
910 mutex_unlock(&dso->lock);
927 static ssize_t file_read(struct dso *dso, struct machine *machine,
935 * dso->data.fd might be closed if other thread opened another
936 * file (dso) due to open file limit (RLIMIT_NOFILE).
938 try_to_open_dso(dso, machine);
940 if (dso->data.fd < 0) {
941 dso->data.status = DSO_DATA_STATUS_ERROR;
946 ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
952 static struct dso_cache *dso_cache__populate(struct dso *dso,
966 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
967 *ret = bpf_read(dso, cache_offset, cache->data);
970 if (dso->binary_type == DSO_BINARY_TYPE__OOL)
973 *ret = file_read(dso, machine, cache_offset, cache->data);
983 old = dso_cache__insert(dso, cache);
993 static struct dso_cache *dso_cache__find(struct dso *dso,
998 struct dso_cache *cache = __dso_cache__find(dso, offset);
1000 return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
1003 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
1009 cache = dso_cache__find(dso, machine, offset, &ret);
1017 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
1021 static ssize_t cached_io(struct dso *dso, struct machine *machine,
1030 ret = dso_cache_io(dso, machine, offset, p, size, out);
1050 static int file_size(struct dso *dso, struct machine *machine)
1059 * dso->data.fd might be closed if other thread opened another
1060 * file (dso) due to open file limit (RLIMIT_NOFILE).
1062 try_to_open_dso(dso, machine);
1064 if (dso->data.fd < 0) {
1066 dso->data.status = DSO_DATA_STATUS_ERROR;
1070 if (fstat(dso->data.fd, &st) < 0) {
1072 pr_err("dso cache fstat failed: %s\n",
1074 dso->data.status = DSO_DATA_STATUS_ERROR;
1077 dso->data.file_size = st.st_size;
1084 int dso__data_file_size(struct dso *dso, struct machine *machine)
1086 if (dso->data.file_size)
1089 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1092 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
1093 return bpf_size(dso);
1095 return file_size(dso, machine);
1099 * dso__data_size - Return dso data size
1100 * @dso: dso object
1103 * Return: dso data size
1105 off_t dso__data_size(struct dso *dso, struct machine *machine)
1107 if (dso__data_file_size(dso, machine))
1110 /* For now just estimate dso data size is close to file size */
1111 return dso->data.file_size;
1114 static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1118 if (dso__data_file_size(dso, machine))
1122 if (offset > dso->data.file_size)
1128 return cached_io(dso, machine, offset, data, size, out);
1132 * dso__data_read_offset - Read data from dso file offset
1133 * @dso: dso object
1139 * External interface to read data from dso file offset. Open
1140 * dso data file and use cached_read to get the data.
1142 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1145 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1148 return data_read_write_offset(dso, machine, offset, data, size, true);
1152 * dso__data_read_addr - Read data from dso address
1153 * @dso: dso object
1159 * External interface to read data from dso address.
1161 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1167 return dso__data_read_offset(dso, machine, offset, data, size);
1171 * dso__data_write_cache_offs - Write data to dso data cache at file offset
1172 * @dso: dso object
1178 * Write into the dso file data cache, but do not change the file itself.
1180 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1185 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1188 return data_read_write_offset(dso, machine, offset, data, size, false);
1192 * dso__data_write_cache_addr - Write data to dso data cache at dso address
1193 * @dso: dso object
1199 * External interface to write into the dso file data cache, but do not change
1202 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1208 return dso__data_write_cache_offs(dso, machine, offset, data, size);
1214 struct dso *dso = dso__new(name);
1216 if (dso) {
1217 map = map__new2(0, dso);
1218 dso__put(dso);
1224 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1228 * The kernel dso could be created by build_id processing.
1230 struct dso *dso = machine__findnew_dso(machine, name);
1234 * processing we had no idea this was the kernel dso.
1236 if (dso != NULL) {
1237 dso__set_short_name(dso, short_name, false);
1238 dso->kernel = dso_type;
1241 return dso;
1244 static void dso__set_long_name_id(struct dso *dso, const char *name, struct dso_id *id, bool name_allocated)
1246 struct rb_root *root = dso->root;
1251 if (dso->long_name_allocated)
1252 free((char *)dso->long_name);
1255 rb_erase(&dso->rb_node, root);
1260 RB_CLEAR_NODE(&dso->rb_node);
1261 dso->root = NULL;
1264 dso->long_name = name;
1265 dso->long_name_len = strlen(name);
1266 dso->long_name_allocated = name_allocated;
1269 __dsos__findnew_link_by_longname_id(root, dso, NULL, id);
1272 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1274 dso__set_long_name_id(dso, name, NULL, name_allocated);
1277 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1282 if (dso->short_name_allocated)
1283 free((char *)dso->short_name);
1285 dso->short_name = name;
1286 dso->short_name_len = strlen(name);
1287 dso->short_name_allocated = name_allocated;
1290 int dso__name_len(const struct dso *dso)
1292 if (!dso)
1295 return dso->long_name_len;
1297 return dso->short_name_len;
1300 bool dso__loaded(const struct dso *dso)
1302 return dso->loaded;
1305 bool dso__sorted_by_name(const struct dso *dso)
1307 return dso->sorted_by_name;
1310 void dso__set_sorted_by_name(struct dso *dso)
1312 dso->sorted_by_name = true;
1315 struct dso *dso__new_id(const char *name, struct dso_id *id)
1317 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1319 if (dso != NULL) {
1320 strcpy(dso->name, name);
1322 dso->id = *id;
1323 dso__set_long_name_id(dso, dso->name, id, false);
1324 dso__set_short_name(dso, dso->name, false);
1325 dso->symbols = RB_ROOT_CACHED;
1326 dso->symbol_names = NULL;
1327 dso->symbol_names_len = 0;
1328 dso->data.cache = RB_ROOT;
1329 dso->inlined_nodes = RB_ROOT_CACHED;
1330 dso->srclines = RB_ROOT_CACHED;
1331 dso->data_types = RB_ROOT;
1332 dso->data.fd = -1;
1333 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1334 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1335 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1336 dso->is_64_bit = (sizeof(void *) == 8);
1337 dso->loaded = 0;
1338 dso->rel = 0;
1339 dso->sorted_by_name = 0;
1340 dso->has_build_id = 0;
1341 dso->has_srcline = 1;
1342 dso->a2l_fails = 1;
1343 dso->kernel = DSO_SPACE__USER;
1344 dso->is_kmod = 0;
1345 dso->needs_swap = DSO_SWAP__UNSET;
1346 dso->comp = COMP_ID__NONE;
1347 RB_CLEAR_NODE(&dso->rb_node);
1348 dso->root = NULL;
1349 INIT_LIST_HEAD(&dso->node);
1350 INIT_LIST_HEAD(&dso->data.open_entry);
1351 mutex_init(&dso->lock);
1352 refcount_set(&dso->refcnt, 1);
1355 return dso;
1358 struct dso *dso__new(const char *name)
1363 void dso__delete(struct dso *dso)
1365 if (!RB_EMPTY_NODE(&dso->rb_node))
1367 dso->long_name);
1370 inlines__tree_delete(&dso->inlined_nodes);
1371 srcline__tree_delete(&dso->srclines);
1372 symbols__delete(&dso->symbols);
1373 dso->symbol_names_len = 0;
1374 zfree(&dso->symbol_names);
1375 annotated_data_type__tree_delete(&dso->data_types);
1377 if (dso->short_name_allocated) {
1378 zfree((char **)&dso->short_name);
1379 dso->short_name_allocated = false;
1382 if (dso->long_name_allocated) {
1383 zfree((char **)&dso->long_name);
1384 dso->long_name_allocated = false;
1387 dso__data_close(dso);
1388 auxtrace_cache__free(dso->auxtrace_cache);
1389 dso_cache__free(dso);
1390 dso__free_a2l(dso);
1391 zfree(&dso->symsrc_filename);
1392 nsinfo__zput(dso->nsinfo);
1393 mutex_destroy(&dso->lock);
1394 free(dso);
1397 struct dso *dso__get(struct dso *dso)
1399 if (dso)
1400 refcount_inc(&dso->refcnt);
1401 return dso;
1404 void dso__put(struct dso *dso)
1406 if (dso && refcount_dec_and_test(&dso->refcnt))
1407 dso__delete(dso);
1410 void dso__set_build_id(struct dso *dso, struct build_id *bid)
1412 dso->bid = *bid;
1413 dso->has_build_id = 1;
1416 bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
1418 if (dso->bid.size > bid->size && dso->bid.size == BUILD_ID_SIZE) {
1423 return !memcmp(dso->bid.data, bid->data, bid->size) &&
1424 !memchr_inv(&dso->bid.data[bid->size], 0,
1425 dso->bid.size - bid->size);
1428 return dso->bid.size == bid->size &&
1429 memcmp(dso->bid.data, bid->data, dso->bid.size) == 0;
1432 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1439 if (sysfs__read_build_id(path, &dso->bid) == 0)
1440 dso->has_build_id = true;
1443 int dso__kernel_module_get_build_id(struct dso *dso,
1451 const char *name = dso->short_name + 1;
1457 if (sysfs__read_build_id(filename, &dso->bid) == 0)
1458 dso->has_build_id = true;
1463 static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1467 build_id__sprintf(&dso->bid, sbuild_id);
1471 size_t dso__fprintf(struct dso *dso, FILE *fp)
1474 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1476 if (dso->short_name != dso->long_name)
1477 ret += fprintf(fp, "%s, ", dso->long_name);
1478 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1479 ret += dso__fprintf_buildid(dso, fp);
1481 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1489 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1494 fd = dso__data_get_fd(dso, machine);
1497 dso__data_put_fd(dso);
1503 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1505 int idx, errnum = dso->load_errno;