Lines Matching refs:dso
23 #include "dso.h"
39 char dso__symtab_origin(const struct dso *dso)
62 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
64 return origin[dso->symtab_type];
67 int dso__read_binary_type_filename(const struct dso *dso,
83 len = __symbol__join_symfs(filename, size, dso->long_name);
114 if (dso__build_id_filename(dso, filename, size, false) == NULL)
119 if (dso__build_id_filename(dso, filename, size, true) == NULL)
125 snprintf(filename + len, size - len, "%s.debug", dso->long_name);
130 snprintf(filename + len, size - len, "%s", dso->long_name);
139 if (strlen(dso->long_name) < 9 ||
140 strncmp(dso->long_name, "/usr/lib/", 9)) {
145 snprintf(filename + len, size - len, "%s", dso->long_name + 4);
153 last_slash = dso->long_name + dso->long_name_len;
154 while (last_slash != dso->long_name && *last_slash != '/')
158 dir_size = last_slash - dso->long_name + 2;
163 len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
170 if (!dso->has_build_id) {
175 build_id__sprintf(&dso->bid, build_id_hex);
184 __symbol__join_symfs(filename, size, dso->long_name);
190 root_dir, dso->long_name);
195 __symbol__join_symfs(filename, size, dso->long_name);
200 snprintf(filename, size, "%s", dso->long_name);
274 bool dso__needs_decompress(struct dso *dso)
276 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
277 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
280 static int decompress_kmodule(struct dso *dso, const char *name,
286 if (!dso__needs_decompress(dso))
289 if (dso->comp == COMP_ID__NONE)
305 if (!compressions[dso->comp].is_compressed(name))
310 dso->load_errno = errno;
314 if (compressions[dso->comp].decompress(name, fd)) {
315 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
329 int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
331 return decompress_kmodule(dso, name, NULL, 0);
334 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
337 int fd = decompress_kmodule(dso, name, pathname, len);
423 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
427 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
429 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
433 dso->symtab_type++;
434 dso->comp = m->comp;
437 dso__set_short_name(dso, strdup(m->name), true);
447 static void dso__list_add(struct dso *dso)
449 list_add_tail(&dso->data.open_entry, &dso__data_open);
453 static void dso__list_del(struct dso *dso)
455 list_del_init(&dso->data.open_entry);
473 pr_debug("dso open failed: %s\n",
484 static int __open_dso(struct dso *dso, struct machine *machine)
497 if (dso__read_binary_type_filename(dso, dso->binary_type,
504 if (dso__needs_decompress(dso)) {
508 if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
509 fd = -dso->load_errno;
531 * @dso: dso object
533 * Open @dso's data file descriptor and updates
536 static int open_dso(struct dso *dso, struct machine *machine)
541 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
542 nsinfo__mountns_enter(dso->nsinfo, &nsc);
543 fd = __open_dso(dso, machine);
544 if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
548 dso__list_add(dso);
559 static void close_data_fd(struct dso *dso)
561 if (dso->data.fd >= 0) {
562 close(dso->data.fd);
563 dso->data.fd = -1;
564 dso->data.file_size = 0;
565 dso__list_del(dso);
571 * @dso: dso object
573 * Close @dso's data file descriptor and updates
576 static void close_dso(struct dso *dso)
578 close_data_fd(dso);
583 struct dso *dso;
585 dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
586 close_dso(dso);
611 * Used only by tests/dso-data.c to reset the environment
632 * Check and close LRU dso if we crossed allowed limit
633 * for opened dso file descriptors. The limit is half
646 * @dso: dso object
648 * External interface to close @dso's data file descriptor.
650 void dso__data_close(struct dso *dso)
653 close_dso(dso);
657 static void try_to_open_dso(struct dso *dso, struct machine *machine)
666 if (dso->data.fd >= 0)
669 if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
670 dso->data.fd = open_dso(dso, machine);
675 dso->binary_type = binary_type_data[i++];
677 dso->data.fd = open_dso(dso, machine);
678 if (dso->data.fd >= 0)
681 } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
683 if (dso->data.fd >= 0)
684 dso->data.status = DSO_DATA_STATUS_OK;
686 dso->data.status = DSO_DATA_STATUS_ERROR;
690 * dso__data_get_fd - Get dso's data file descriptor
691 * @dso: dso object
694 * External interface to find dso's file, open it and
698 int dso__data_get_fd(struct dso *dso, struct machine *machine)
700 if (dso->data.status == DSO_DATA_STATUS_ERROR)
706 try_to_open_dso(dso, machine);
708 if (dso->data.fd < 0)
711 return dso->data.fd;
714 void dso__data_put_fd(struct dso *dso __maybe_unused)
719 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
723 if (dso->data.status_seen & flag)
726 dso->data.status_seen |= flag;
731 static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
738 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
740 dso->data.status = DSO_DATA_STATUS_ERROR;
755 static int bpf_size(struct dso *dso)
759 node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
761 dso->data.status = DSO_DATA_STATUS_ERROR;
765 dso->data.file_size = node->info_linear->info.jited_prog_len;
770 dso_cache__free(struct dso *dso)
772 struct rb_root *root = &dso->data.cache;
775 pthread_mutex_lock(&dso->lock);
784 pthread_mutex_unlock(&dso->lock);
787 static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
789 const struct rb_root *root = &dso->data.cache;
813 dso_cache__insert(struct dso *dso, struct dso_cache *new)
815 struct rb_root *root = &dso->data.cache;
821 pthread_mutex_lock(&dso->lock);
842 pthread_mutex_unlock(&dso->lock);
859 static ssize_t file_read(struct dso *dso, struct machine *machine,
867 * dso->data.fd might be closed if other thread opened another
868 * file (dso) due to open file limit (RLIMIT_NOFILE).
870 try_to_open_dso(dso, machine);
872 if (dso->data.fd < 0) {
873 dso->data.status = DSO_DATA_STATUS_ERROR;
878 ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
884 static struct dso_cache *dso_cache__populate(struct dso *dso,
898 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
899 *ret = bpf_read(dso, cache_offset, cache->data);
900 else if (dso->binary_type == DSO_BINARY_TYPE__OOL)
903 *ret = file_read(dso, machine, cache_offset, cache->data);
913 old = dso_cache__insert(dso, cache);
923 static struct dso_cache *dso_cache__find(struct dso *dso,
928 struct dso_cache *cache = __dso_cache__find(dso, offset);
930 return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
933 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
939 cache = dso_cache__find(dso, machine, offset, &ret);
947 * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
951 static ssize_t cached_io(struct dso *dso, struct machine *machine,
960 ret = dso_cache_io(dso, machine, offset, p, size, out);
980 static int file_size(struct dso *dso, struct machine *machine)
989 * dso->data.fd might be closed if other thread opened another
990 * file (dso) due to open file limit (RLIMIT_NOFILE).
992 try_to_open_dso(dso, machine);
994 if (dso->data.fd < 0) {
996 dso->data.status = DSO_DATA_STATUS_ERROR;
1000 if (fstat(dso->data.fd, &st) < 0) {
1002 pr_err("dso cache fstat failed: %s\n",
1004 dso->data.status = DSO_DATA_STATUS_ERROR;
1007 dso->data.file_size = st.st_size;
1014 int dso__data_file_size(struct dso *dso, struct machine *machine)
1016 if (dso->data.file_size)
1019 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1022 if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
1023 return bpf_size(dso);
1025 return file_size(dso, machine);
1029 * dso__data_size - Return dso data size
1030 * @dso: dso object
1033 * Return: dso data size
1035 off_t dso__data_size(struct dso *dso, struct machine *machine)
1037 if (dso__data_file_size(dso, machine))
1040 /* For now just estimate dso data size is close to file size */
1041 return dso->data.file_size;
1044 static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1048 if (dso__data_file_size(dso, machine))
1052 if (offset > dso->data.file_size)
1058 return cached_io(dso, machine, offset, data, size, out);
1062 * dso__data_read_offset - Read data from dso file offset
1063 * @dso: dso object
1069 * External interface to read data from dso file offset. Open
1070 * dso data file and use cached_read to get the data.
1072 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1075 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1078 return data_read_write_offset(dso, machine, offset, data, size, true);
1082 * dso__data_read_addr - Read data from dso address
1083 * @dso: dso object
1089 * External interface to read data from dso address.
1091 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1096 return dso__data_read_offset(dso, machine, offset, data, size);
1100 * dso__data_write_cache_offs - Write data to dso data cache at file offset
1101 * @dso: dso object
1107 * Write into the dso file data cache, but do not change the file itself.
1109 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1114 if (dso->data.status == DSO_DATA_STATUS_ERROR)
1117 return data_read_write_offset(dso, machine, offset, data, size, false);
1121 * dso__data_write_cache_addr - Write data to dso data cache at dso address
1122 * @dso: dso object
1128 * External interface to write into the dso file data cache, but do not change
1131 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1136 return dso__data_write_cache_offs(dso, machine, offset, data, size);
1142 struct dso *dso = dso__new(name);
1144 if (dso) {
1145 map = map__new2(0, dso);
1146 dso__put(dso);
1152 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
1156 * The kernel dso could be created by build_id processing.
1158 struct dso *dso = machine__findnew_dso(machine, name);
1162 * processing we had no idea this was the kernel dso.
1164 if (dso != NULL) {
1165 dso__set_short_name(dso, short_name, false);
1166 dso->kernel = dso_type;
1169 return dso;
1172 static void dso__set_long_name_id(struct dso *dso, const char *name, struct dso_id *id, bool name_allocated)
1174 struct rb_root *root = dso->root;
1179 if (dso->long_name_allocated)
1180 free((char *)dso->long_name);
1183 rb_erase(&dso->rb_node, root);
1188 RB_CLEAR_NODE(&dso->rb_node);
1189 dso->root = NULL;
1192 dso->long_name = name;
1193 dso->long_name_len = strlen(name);
1194 dso->long_name_allocated = name_allocated;
1197 __dsos__findnew_link_by_longname_id(root, dso, NULL, id);
1200 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1202 dso__set_long_name_id(dso, name, NULL, name_allocated);
1205 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1210 if (dso->short_name_allocated)
1211 free((char *)dso->short_name);
1213 dso->short_name = name;
1214 dso->short_name_len = strlen(name);
1215 dso->short_name_allocated = name_allocated;
1218 int dso__name_len(const struct dso *dso)
1220 if (!dso)
1223 return dso->long_name_len;
1225 return dso->short_name_len;
1228 bool dso__loaded(const struct dso *dso)
1230 return dso->loaded;
1233 bool dso__sorted_by_name(const struct dso *dso)
1235 return dso->sorted_by_name;
1238 void dso__set_sorted_by_name(struct dso *dso)
1240 dso->sorted_by_name = true;
1243 struct dso *dso__new_id(const char *name, struct dso_id *id)
1245 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1247 if (dso != NULL) {
1248 strcpy(dso->name, name);
1250 dso->id = *id;
1251 dso__set_long_name_id(dso, dso->name, id, false);
1252 dso__set_short_name(dso, dso->name, false);
1253 dso->symbols = dso->symbol_names = RB_ROOT_CACHED;
1254 dso->data.cache = RB_ROOT;
1255 dso->inlined_nodes = RB_ROOT_CACHED;
1256 dso->srclines = RB_ROOT_CACHED;
1257 dso->data.fd = -1;
1258 dso->data.status = DSO_DATA_STATUS_UNKNOWN;
1259 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
1260 dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
1261 dso->is_64_bit = (sizeof(void *) == 8);
1262 dso->loaded = 0;
1263 dso->rel = 0;
1264 dso->sorted_by_name = 0;
1265 dso->has_build_id = 0;
1266 dso->has_srcline = 1;
1267 dso->a2l_fails = 1;
1268 dso->kernel = DSO_SPACE__USER;
1269 dso->needs_swap = DSO_SWAP__UNSET;
1270 dso->comp = COMP_ID__NONE;
1271 RB_CLEAR_NODE(&dso->rb_node);
1272 dso->root = NULL;
1273 INIT_LIST_HEAD(&dso->node);
1274 INIT_LIST_HEAD(&dso->data.open_entry);
1275 pthread_mutex_init(&dso->lock, NULL);
1276 refcount_set(&dso->refcnt, 1);
1279 return dso;
1282 struct dso *dso__new(const char *name)
1287 void dso__delete(struct dso *dso)
1289 if (!RB_EMPTY_NODE(&dso->rb_node))
1291 dso->long_name);
1294 inlines__tree_delete(&dso->inlined_nodes);
1295 srcline__tree_delete(&dso->srclines);
1296 symbols__delete(&dso->symbols);
1298 if (dso->short_name_allocated) {
1299 zfree((char **)&dso->short_name);
1300 dso->short_name_allocated = false;
1303 if (dso->long_name_allocated) {
1304 zfree((char **)&dso->long_name);
1305 dso->long_name_allocated = false;
1308 dso__data_close(dso);
1309 auxtrace_cache__free(dso->auxtrace_cache);
1310 dso_cache__free(dso);
1311 dso__free_a2l(dso);
1312 zfree(&dso->symsrc_filename);
1313 nsinfo__zput(dso->nsinfo);
1314 pthread_mutex_destroy(&dso->lock);
1315 free(dso);
1318 struct dso *dso__get(struct dso *dso)
1320 if (dso)
1321 refcount_inc(&dso->refcnt);
1322 return dso;
1325 void dso__put(struct dso *dso)
1327 if (dso && refcount_dec_and_test(&dso->refcnt))
1328 dso__delete(dso);
1331 void dso__set_build_id(struct dso *dso, struct build_id *bid)
1333 dso->bid = *bid;
1334 dso->has_build_id = 1;
1337 bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
1339 if (dso->bid.size > bid->size && dso->bid.size == BUILD_ID_SIZE) {
1344 return !memcmp(dso->bid.data, bid->data, bid->size) &&
1345 !memchr_inv(&dso->bid.data[bid->size], 0,
1346 dso->bid.size - bid->size);
1349 return dso->bid.size == bid->size &&
1350 memcmp(dso->bid.data, bid->data, dso->bid.size) == 0;
1353 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1360 if (sysfs__read_build_id(path, &dso->bid) == 0)
1361 dso->has_build_id = true;
1364 int dso__kernel_module_get_build_id(struct dso *dso,
1372 const char *name = dso->short_name + 1;
1378 if (sysfs__read_build_id(filename, &dso->bid) == 0)
1379 dso->has_build_id = true;
1384 static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
1388 build_id__sprintf(&dso->bid, sbuild_id);
1392 size_t dso__fprintf(struct dso *dso, FILE *fp)
1395 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
1397 if (dso->short_name != dso->long_name)
1398 ret += fprintf(fp, "%s, ", dso->long_name);
1399 ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
1400 ret += dso__fprintf_buildid(dso, fp);
1402 for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
1410 enum dso_type dso__type(struct dso *dso, struct machine *machine)
1415 fd = dso__data_get_fd(dso, machine);
1418 dso__data_put_fd(dso);
1424 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1426 int idx, errnum = dso->load_errno;