dso.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_DSO
  3. #define __PERF_DSO
  4. #include <linux/refcount.h>
  5. #include <linux/types.h>
  6. #include <linux/rbtree.h>
  7. #include <sys/types.h>
  8. #include <stdbool.h>
  9. #include "rwsem.h"
  10. #include <linux/types.h>
  11. #include <linux/bitops.h>
  12. #include "map.h"
  13. #include "namespaces.h"
  14. #include "build-id.h"
  15. enum dso_binary_type {
  16. DSO_BINARY_TYPE__KALLSYMS = 0,
  17. DSO_BINARY_TYPE__GUEST_KALLSYMS,
  18. DSO_BINARY_TYPE__VMLINUX,
  19. DSO_BINARY_TYPE__GUEST_VMLINUX,
  20. DSO_BINARY_TYPE__JAVA_JIT,
  21. DSO_BINARY_TYPE__DEBUGLINK,
  22. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  23. DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
  24. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  25. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  26. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  27. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  28. DSO_BINARY_TYPE__GUEST_KMODULE,
  29. DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
  30. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  31. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
  32. DSO_BINARY_TYPE__KCORE,
  33. DSO_BINARY_TYPE__GUEST_KCORE,
  34. DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
  35. DSO_BINARY_TYPE__NOT_FOUND,
  36. };
  37. enum dso_kernel_type {
  38. DSO_TYPE_USER = 0,
  39. DSO_TYPE_KERNEL,
  40. DSO_TYPE_GUEST_KERNEL
  41. };
  42. enum dso_swap_type {
  43. DSO_SWAP__UNSET,
  44. DSO_SWAP__NO,
  45. DSO_SWAP__YES,
  46. };
  47. enum dso_data_status {
  48. DSO_DATA_STATUS_ERROR = -1,
  49. DSO_DATA_STATUS_UNKNOWN = 0,
  50. DSO_DATA_STATUS_OK = 1,
  51. };
  52. enum dso_data_status_seen {
  53. DSO_DATA_STATUS_SEEN_ITRACE,
  54. };
  55. enum dso_type {
  56. DSO__TYPE_UNKNOWN,
  57. DSO__TYPE_64BIT,
  58. DSO__TYPE_32BIT,
  59. DSO__TYPE_X32BIT,
  60. };
  61. enum dso_load_errno {
  62. DSO_LOAD_ERRNO__SUCCESS = 0,
  63. /*
  64. * Choose an arbitrary negative big number not to clash with standard
  65. * errno since SUS requires the errno has distinct positive values.
  66. * See 'Issue 6' in the link below.
  67. *
  68. * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
  69. */
  70. __DSO_LOAD_ERRNO__START = -10000,
  71. DSO_LOAD_ERRNO__INTERNAL_ERROR = __DSO_LOAD_ERRNO__START,
  72. /* for symsrc__init() */
  73. DSO_LOAD_ERRNO__INVALID_ELF,
  74. DSO_LOAD_ERRNO__CANNOT_READ_BUILDID,
  75. DSO_LOAD_ERRNO__MISMATCHING_BUILDID,
  76. /* for decompress_kmodule */
  77. DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE,
  78. __DSO_LOAD_ERRNO__END,
  79. };
  80. #define DSO__SWAP(dso, type, val) \
  81. ({ \
  82. type ____r = val; \
  83. BUG_ON(dso->needs_swap == DSO_SWAP__UNSET); \
  84. if (dso->needs_swap == DSO_SWAP__YES) { \
  85. switch (sizeof(____r)) { \
  86. case 2: \
  87. ____r = bswap_16(val); \
  88. break; \
  89. case 4: \
  90. ____r = bswap_32(val); \
  91. break; \
  92. case 8: \
  93. ____r = bswap_64(val); \
  94. break; \
  95. default: \
  96. BUG_ON(1); \
  97. } \
  98. } \
  99. ____r; \
  100. })
  101. #define DSO__DATA_CACHE_SIZE 4096
  102. #define DSO__DATA_CACHE_MASK ~(DSO__DATA_CACHE_SIZE - 1)
  103. struct dso_cache {
  104. struct rb_node rb_node;
  105. u64 offset;
  106. u64 size;
  107. char data[0];
  108. };
  109. /*
  110. * DSOs are put into both a list for fast iteration and rbtree for fast
  111. * long name lookup.
  112. */
  113. struct dsos {
  114. struct list_head head;
  115. struct rb_root root; /* rbtree root sorted by long name */
  116. struct rw_semaphore lock;
  117. };
  118. struct auxtrace_cache;
  119. struct dso {
  120. pthread_mutex_t lock;
  121. struct list_head node;
  122. struct rb_node rb_node; /* rbtree node sorted by long name */
  123. struct rb_root *root; /* root of rbtree that rb_node is in */
  124. struct rb_root symbols;
  125. struct rb_root symbol_names;
  126. struct rb_root inlined_nodes;
  127. struct rb_root srclines;
  128. struct {
  129. u64 addr;
  130. struct symbol *symbol;
  131. } last_find_result;
  132. void *a2l;
  133. char *symsrc_filename;
  134. unsigned int a2l_fails;
  135. enum dso_kernel_type kernel;
  136. enum dso_swap_type needs_swap;
  137. enum dso_binary_type symtab_type;
  138. enum dso_binary_type binary_type;
  139. enum dso_load_errno load_errno;
  140. u8 adjust_symbols:1;
  141. u8 has_build_id:1;
  142. u8 has_srcline:1;
  143. u8 hit:1;
  144. u8 annotate_warned:1;
  145. u8 short_name_allocated:1;
  146. u8 long_name_allocated:1;
  147. u8 is_64_bit:1;
  148. bool sorted_by_name;
  149. bool loaded;
  150. u8 rel;
  151. u8 build_id[BUILD_ID_SIZE];
  152. u64 text_offset;
  153. const char *short_name;
  154. const char *long_name;
  155. u16 long_name_len;
  156. u16 short_name_len;
  157. void *dwfl; /* DWARF debug info */
  158. struct auxtrace_cache *auxtrace_cache;
  159. /* dso data file */
  160. struct {
  161. struct rb_root cache;
  162. int fd;
  163. int status;
  164. u32 status_seen;
  165. size_t file_size;
  166. struct list_head open_entry;
  167. u64 debug_frame_offset;
  168. u64 eh_frame_hdr_offset;
  169. } data;
  170. union { /* Tool specific area */
  171. void *priv;
  172. u64 db_id;
  173. };
  174. struct nsinfo *nsinfo;
  175. refcount_t refcnt;
  176. char name[0];
  177. };
  178. /* dso__for_each_symbol - iterate over the symbols of given type
  179. *
  180. * @dso: the 'struct dso *' in which symbols itereated
  181. * @pos: the 'struct symbol *' to use as a loop cursor
  182. * @n: the 'struct rb_node *' to use as a temporary storage
  183. */
  184. #define dso__for_each_symbol(dso, pos, n) \
  185. symbols__for_each_entry(&(dso)->symbols, pos, n)
  186. static inline void dso__set_loaded(struct dso *dso)
  187. {
  188. dso->loaded = true;
  189. }
  190. struct dso *dso__new(const char *name);
  191. void dso__delete(struct dso *dso);
  192. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated);
  193. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated);
  194. int dso__name_len(const struct dso *dso);
  195. struct dso *dso__get(struct dso *dso);
  196. void dso__put(struct dso *dso);
  197. static inline void __dso__zput(struct dso **dso)
  198. {
  199. dso__put(*dso);
  200. *dso = NULL;
  201. }
  202. #define dso__zput(dso) __dso__zput(&dso)
  203. bool dso__loaded(const struct dso *dso);
  204. static inline bool dso__has_symbols(const struct dso *dso)
  205. {
  206. return !RB_EMPTY_ROOT(&dso->symbols);
  207. }
  208. bool dso__sorted_by_name(const struct dso *dso);
  209. void dso__set_sorted_by_name(struct dso *dso);
  210. void dso__sort_by_name(struct dso *dso);
  211. void dso__set_build_id(struct dso *dso, void *build_id);
  212. bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
  213. void dso__read_running_kernel_build_id(struct dso *dso,
  214. struct machine *machine);
  215. int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir);
  216. char dso__symtab_origin(const struct dso *dso);
  217. int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type,
  218. char *root_dir, char *filename, size_t size);
  219. bool is_supported_compression(const char *ext);
  220. bool is_kernel_module(const char *pathname, int cpumode);
  221. bool decompress_to_file(const char *ext, const char *filename, int output_fd);
  222. bool dso__needs_decompress(struct dso *dso);
  223. int dso__decompress_kmodule_fd(struct dso *dso, const char *name);
  224. int dso__decompress_kmodule_path(struct dso *dso, const char *name,
  225. char *pathname, size_t len);
  226. #define KMOD_DECOMP_NAME "/tmp/perf-kmod-XXXXXX"
  227. #define KMOD_DECOMP_LEN sizeof(KMOD_DECOMP_NAME)
  228. struct kmod_path {
  229. char *name;
  230. char *ext;
  231. bool comp;
  232. bool kmod;
  233. };
  234. int __kmod_path__parse(struct kmod_path *m, const char *path,
  235. bool alloc_name, bool alloc_ext);
  236. #define kmod_path__parse(__m, __p) __kmod_path__parse(__m, __p, false, false)
  237. #define kmod_path__parse_name(__m, __p) __kmod_path__parse(__m, __p, true , false)
  238. #define kmod_path__parse_ext(__m, __p) __kmod_path__parse(__m, __p, false, true)
  239. void dso__set_module_info(struct dso *dso, struct kmod_path *m,
  240. struct machine *machine);
  241. /*
  242. * The dso__data_* external interface provides following functions:
  243. * dso__data_get_fd
  244. * dso__data_put_fd
  245. * dso__data_close
  246. * dso__data_size
  247. * dso__data_read_offset
  248. * dso__data_read_addr
  249. *
  250. * Please refer to the dso.c object code for each function and
  251. * arguments documentation. Following text tries to explain the
  252. * dso file descriptor caching.
  253. *
  254. * The dso__data* interface allows caching of opened file descriptors
  255. * to speed up the dso data accesses. The idea is to leave the file
  256. * descriptor opened ideally for the whole life of the dso object.
  257. *
  258. * The current usage of the dso__data_* interface is as follows:
  259. *
  260. * Get DSO's fd:
  261. * int fd = dso__data_get_fd(dso, machine);
  262. * if (fd >= 0) {
  263. * USE 'fd' SOMEHOW
  264. * dso__data_put_fd(dso);
  265. * }
  266. *
  267. * Read DSO's data:
  268. * n = dso__data_read_offset(dso_0, &machine, 0, buf, BUFSIZE);
  269. * n = dso__data_read_addr(dso_0, &machine, 0, buf, BUFSIZE);
  270. *
  271. * Eventually close DSO's fd:
  272. * dso__data_close(dso);
  273. *
  274. * It is not necessary to close the DSO object data file. Each time new
  275. * DSO data file is opened, the limit (RLIMIT_NOFILE/2) is checked. Once
  276. * it is crossed, the oldest opened DSO object is closed.
  277. *
  278. * The dso__delete function calls close_dso function to ensure the
  279. * data file descriptor gets closed/unmapped before the dso object
  280. * is freed.
  281. *
  282. * TODO
  283. */
  284. int dso__data_get_fd(struct dso *dso, struct machine *machine);
  285. void dso__data_put_fd(struct dso *dso);
  286. void dso__data_close(struct dso *dso);
  287. off_t dso__data_size(struct dso *dso, struct machine *machine);
  288. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  289. u64 offset, u8 *data, ssize_t size);
  290. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  291. struct machine *machine, u64 addr,
  292. u8 *data, ssize_t size);
  293. bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by);
  294. struct map *dso__new_map(const char *name);
  295. struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
  296. const char *short_name, int dso_type);
  297. void __dsos__add(struct dsos *dsos, struct dso *dso);
  298. void dsos__add(struct dsos *dsos, struct dso *dso);
  299. struct dso *__dsos__addnew(struct dsos *dsos, const char *name);
  300. struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short);
  301. struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short);
  302. struct dso *__dsos__findnew(struct dsos *dsos, const char *name);
  303. struct dso *dsos__findnew(struct dsos *dsos, const char *name);
  304. bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
  305. void dso__reset_find_symbol_cache(struct dso *dso);
  306. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  307. bool (skip)(struct dso *dso, int parm), int parm);
  308. size_t __dsos__fprintf(struct list_head *head, FILE *fp);
  309. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp);
  310. size_t dso__fprintf_symbols_by_name(struct dso *dso, FILE *fp);
  311. size_t dso__fprintf(struct dso *dso, FILE *fp);
  312. static inline bool dso__is_vmlinux(struct dso *dso)
  313. {
  314. return dso->binary_type == DSO_BINARY_TYPE__VMLINUX ||
  315. dso->binary_type == DSO_BINARY_TYPE__GUEST_VMLINUX;
  316. }
  317. static inline bool dso__is_kcore(struct dso *dso)
  318. {
  319. return dso->binary_type == DSO_BINARY_TYPE__KCORE ||
  320. dso->binary_type == DSO_BINARY_TYPE__GUEST_KCORE;
  321. }
  322. static inline bool dso__is_kallsyms(struct dso *dso)
  323. {
  324. return dso->kernel && dso->long_name[0] != '/';
  325. }
  326. void dso__free_a2l(struct dso *dso);
  327. enum dso_type dso__type(struct dso *dso, struct machine *machine);
  328. int dso__strerror_load(struct dso *dso, char *buf, size_t buflen);
  329. void reset_fd_limit(void);
  330. #endif /* __PERF_DSO */