dso.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. int comp;
  160. /* dso data file */
  161. struct {
  162. struct rb_root cache;
  163. int fd;
  164. int status;
  165. u32 status_seen;
  166. size_t file_size;
  167. struct list_head open_entry;
  168. u64 debug_frame_offset;
  169. u64 eh_frame_hdr_offset;
  170. } data;
  171. union { /* Tool specific area */
  172. void *priv;
  173. u64 db_id;
  174. };
  175. struct nsinfo *nsinfo;
  176. refcount_t refcnt;
  177. char name[0];
  178. };
  179. /* dso__for_each_symbol - iterate over the symbols of given type
  180. *
  181. * @dso: the 'struct dso *' in which symbols itereated
  182. * @pos: the 'struct symbol *' to use as a loop cursor
  183. * @n: the 'struct rb_node *' to use as a temporary storage
  184. */
  185. #define dso__for_each_symbol(dso, pos, n) \
  186. symbols__for_each_entry(&(dso)->symbols, pos, n)
  187. static inline void dso__set_loaded(struct dso *dso)
  188. {
  189. dso->loaded = true;
  190. }
  191. struct dso *dso__new(const char *name);
  192. void dso__delete(struct dso *dso);
  193. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated);
  194. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated);
  195. int dso__name_len(const struct dso *dso);
  196. struct dso *dso__get(struct dso *dso);
  197. void dso__put(struct dso *dso);
  198. static inline void __dso__zput(struct dso **dso)
  199. {
  200. dso__put(*dso);
  201. *dso = NULL;
  202. }
  203. #define dso__zput(dso) __dso__zput(&dso)
  204. bool dso__loaded(const struct dso *dso);
  205. static inline bool dso__has_symbols(const struct dso *dso)
  206. {
  207. return !RB_EMPTY_ROOT(&dso->symbols);
  208. }
  209. bool dso__sorted_by_name(const struct dso *dso);
  210. void dso__set_sorted_by_name(struct dso *dso);
  211. void dso__sort_by_name(struct dso *dso);
  212. void dso__set_build_id(struct dso *dso, void *build_id);
  213. bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
  214. void dso__read_running_kernel_build_id(struct dso *dso,
  215. struct machine *machine);
  216. int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir);
  217. char dso__symtab_origin(const struct dso *dso);
  218. int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type,
  219. char *root_dir, char *filename, size_t size);
  220. bool is_kernel_module(const char *pathname, int cpumode);
  221. bool dso__needs_decompress(struct dso *dso);
  222. int dso__decompress_kmodule_fd(struct dso *dso, const char *name);
  223. int dso__decompress_kmodule_path(struct dso *dso, const char *name,
  224. char *pathname, size_t len);
  225. #define KMOD_DECOMP_NAME "/tmp/perf-kmod-XXXXXX"
  226. #define KMOD_DECOMP_LEN sizeof(KMOD_DECOMP_NAME)
  227. struct kmod_path {
  228. char *name;
  229. int comp;
  230. bool kmod;
  231. };
  232. int __kmod_path__parse(struct kmod_path *m, const char *path,
  233. bool alloc_name);
  234. #define kmod_path__parse(__m, __p) __kmod_path__parse(__m, __p, false)
  235. #define kmod_path__parse_name(__m, __p) __kmod_path__parse(__m, __p, true)
  236. void dso__set_module_info(struct dso *dso, struct kmod_path *m,
  237. struct machine *machine);
  238. /*
  239. * The dso__data_* external interface provides following functions:
  240. * dso__data_get_fd
  241. * dso__data_put_fd
  242. * dso__data_close
  243. * dso__data_size
  244. * dso__data_read_offset
  245. * dso__data_read_addr
  246. *
  247. * Please refer to the dso.c object code for each function and
  248. * arguments documentation. Following text tries to explain the
  249. * dso file descriptor caching.
  250. *
  251. * The dso__data* interface allows caching of opened file descriptors
  252. * to speed up the dso data accesses. The idea is to leave the file
  253. * descriptor opened ideally for the whole life of the dso object.
  254. *
  255. * The current usage of the dso__data_* interface is as follows:
  256. *
  257. * Get DSO's fd:
  258. * int fd = dso__data_get_fd(dso, machine);
  259. * if (fd >= 0) {
  260. * USE 'fd' SOMEHOW
  261. * dso__data_put_fd(dso);
  262. * }
  263. *
  264. * Read DSO's data:
  265. * n = dso__data_read_offset(dso_0, &machine, 0, buf, BUFSIZE);
  266. * n = dso__data_read_addr(dso_0, &machine, 0, buf, BUFSIZE);
  267. *
  268. * Eventually close DSO's fd:
  269. * dso__data_close(dso);
  270. *
  271. * It is not necessary to close the DSO object data file. Each time new
  272. * DSO data file is opened, the limit (RLIMIT_NOFILE/2) is checked. Once
  273. * it is crossed, the oldest opened DSO object is closed.
  274. *
  275. * The dso__delete function calls close_dso function to ensure the
  276. * data file descriptor gets closed/unmapped before the dso object
  277. * is freed.
  278. *
  279. * TODO
  280. */
  281. int dso__data_get_fd(struct dso *dso, struct machine *machine);
  282. void dso__data_put_fd(struct dso *dso);
  283. void dso__data_close(struct dso *dso);
  284. off_t dso__data_size(struct dso *dso, struct machine *machine);
  285. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  286. u64 offset, u8 *data, ssize_t size);
  287. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  288. struct machine *machine, u64 addr,
  289. u8 *data, ssize_t size);
  290. bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by);
  291. struct map *dso__new_map(const char *name);
  292. struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
  293. const char *short_name, int dso_type);
  294. void __dsos__add(struct dsos *dsos, struct dso *dso);
  295. void dsos__add(struct dsos *dsos, struct dso *dso);
  296. struct dso *__dsos__addnew(struct dsos *dsos, const char *name);
  297. struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short);
  298. struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short);
  299. struct dso *__dsos__findnew(struct dsos *dsos, const char *name);
  300. struct dso *dsos__findnew(struct dsos *dsos, const char *name);
  301. bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
  302. void dso__reset_find_symbol_cache(struct dso *dso);
  303. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  304. bool (skip)(struct dso *dso, int parm), int parm);
  305. size_t __dsos__fprintf(struct list_head *head, FILE *fp);
  306. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp);
  307. size_t dso__fprintf_symbols_by_name(struct dso *dso, FILE *fp);
  308. size_t dso__fprintf(struct dso *dso, FILE *fp);
  309. static inline bool dso__is_vmlinux(struct dso *dso)
  310. {
  311. return dso->binary_type == DSO_BINARY_TYPE__VMLINUX ||
  312. dso->binary_type == DSO_BINARY_TYPE__GUEST_VMLINUX;
  313. }
  314. static inline bool dso__is_kcore(struct dso *dso)
  315. {
  316. return dso->binary_type == DSO_BINARY_TYPE__KCORE ||
  317. dso->binary_type == DSO_BINARY_TYPE__GUEST_KCORE;
  318. }
  319. static inline bool dso__is_kallsyms(struct dso *dso)
  320. {
  321. return dso->kernel && dso->long_name[0] != '/';
  322. }
  323. void dso__free_a2l(struct dso *dso);
  324. enum dso_type dso__type(struct dso *dso, struct machine *machine);
  325. int dso__strerror_load(struct dso *dso, char *buf, size_t buflen);
  326. void reset_fd_limit(void);
  327. #endif /* __PERF_DSO */