dso.h 10 KB

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