dso.h 10 KB

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