dso.h 9.9 KB

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