dso.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 *root; /* root of rbtree that rb_node is in */
  120. struct rb_root symbols[MAP__NR_TYPES];
  121. struct rb_root symbol_names[MAP__NR_TYPES];
  122. struct {
  123. u64 addr;
  124. struct symbol *symbol;
  125. } last_find_result[MAP__NR_TYPES];
  126. void *a2l;
  127. char *symsrc_filename;
  128. unsigned int a2l_fails;
  129. enum dso_kernel_type kernel;
  130. enum dso_swap_type needs_swap;
  131. enum dso_binary_type symtab_type;
  132. enum dso_binary_type binary_type;
  133. enum dso_load_errno load_errno;
  134. u8 adjust_symbols:1;
  135. u8 has_build_id:1;
  136. u8 has_srcline:1;
  137. u8 hit:1;
  138. u8 annotate_warned:1;
  139. u8 short_name_allocated:1;
  140. u8 long_name_allocated:1;
  141. u8 is_64_bit:1;
  142. u8 sorted_by_name;
  143. u8 loaded;
  144. u8 rel;
  145. u8 build_id[BUILD_ID_SIZE];
  146. const char *short_name;
  147. const char *long_name;
  148. u16 long_name_len;
  149. u16 short_name_len;
  150. void *dwfl; /* DWARF debug info */
  151. struct auxtrace_cache *auxtrace_cache;
  152. /* dso data file */
  153. struct {
  154. struct rb_root cache;
  155. int fd;
  156. int status;
  157. u32 status_seen;
  158. size_t file_size;
  159. struct list_head open_entry;
  160. u64 debug_frame_offset;
  161. u64 eh_frame_hdr_offset;
  162. } data;
  163. union { /* Tool specific area */
  164. void *priv;
  165. u64 db_id;
  166. };
  167. atomic_t refcnt;
  168. char name[0];
  169. };
  170. /* dso__for_each_symbol - iterate over the symbols of given type
  171. *
  172. * @dso: the 'struct dso *' in which symbols itereated
  173. * @pos: the 'struct symbol *' to use as a loop cursor
  174. * @n: the 'struct rb_node *' to use as a temporary storage
  175. * @type: the 'enum map_type' type of symbols
  176. */
  177. #define dso__for_each_symbol(dso, pos, n, type) \
  178. symbols__for_each_entry(&(dso)->symbols[(type)], pos, n)
  179. static inline void dso__set_loaded(struct dso *dso, enum map_type type)
  180. {
  181. dso->loaded |= (1 << type);
  182. }
  183. struct dso *dso__new(const char *name);
  184. void dso__delete(struct dso *dso);
  185. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated);
  186. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated);
  187. int dso__name_len(const struct dso *dso);
  188. struct dso *dso__get(struct dso *dso);
  189. void dso__put(struct dso *dso);
  190. static inline void __dso__zput(struct dso **dso)
  191. {
  192. dso__put(*dso);
  193. *dso = NULL;
  194. }
  195. #define dso__zput(dso) __dso__zput(&dso)
  196. bool dso__loaded(const struct dso *dso, enum map_type type);
  197. bool dso__sorted_by_name(const struct dso *dso, enum map_type type);
  198. void dso__set_sorted_by_name(struct dso *dso, enum map_type type);
  199. void dso__sort_by_name(struct dso *dso, enum map_type type);
  200. void dso__set_build_id(struct dso *dso, void *build_id);
  201. bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
  202. void dso__read_running_kernel_build_id(struct dso *dso,
  203. struct machine *machine);
  204. int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir);
  205. char dso__symtab_origin(const struct dso *dso);
  206. int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type,
  207. char *root_dir, char *filename, size_t size);
  208. bool is_supported_compression(const char *ext);
  209. bool is_kernel_module(const char *pathname, int cpumode);
  210. bool decompress_to_file(const char *ext, const char *filename, int output_fd);
  211. bool dso__needs_decompress(struct dso *dso);
  212. struct kmod_path {
  213. char *name;
  214. char *ext;
  215. bool comp;
  216. bool kmod;
  217. };
  218. int __kmod_path__parse(struct kmod_path *m, const char *path,
  219. bool alloc_name, bool alloc_ext);
  220. #define kmod_path__parse(__m, __p) __kmod_path__parse(__m, __p, false, false)
  221. #define kmod_path__parse_name(__m, __p) __kmod_path__parse(__m, __p, true , false)
  222. #define kmod_path__parse_ext(__m, __p) __kmod_path__parse(__m, __p, false, true)
  223. /*
  224. * The dso__data_* external interface provides following functions:
  225. * dso__data_get_fd
  226. * dso__data_put_fd
  227. * dso__data_close
  228. * dso__data_size
  229. * dso__data_read_offset
  230. * dso__data_read_addr
  231. *
  232. * Please refer to the dso.c object code for each function and
  233. * arguments documentation. Following text tries to explain the
  234. * dso file descriptor caching.
  235. *
  236. * The dso__data* interface allows caching of opened file descriptors
  237. * to speed up the dso data accesses. The idea is to leave the file
  238. * descriptor opened ideally for the whole life of the dso object.
  239. *
  240. * The current usage of the dso__data_* interface is as follows:
  241. *
  242. * Get DSO's fd:
  243. * int fd = dso__data_get_fd(dso, machine);
  244. * if (fd >= 0) {
  245. * USE 'fd' SOMEHOW
  246. * dso__data_put_fd(dso);
  247. * }
  248. *
  249. * Read DSO's data:
  250. * n = dso__data_read_offset(dso_0, &machine, 0, buf, BUFSIZE);
  251. * n = dso__data_read_addr(dso_0, &machine, 0, buf, BUFSIZE);
  252. *
  253. * Eventually close DSO's fd:
  254. * dso__data_close(dso);
  255. *
  256. * It is not necessary to close the DSO object data file. Each time new
  257. * DSO data file is opened, the limit (RLIMIT_NOFILE/2) is checked. Once
  258. * it is crossed, the oldest opened DSO object is closed.
  259. *
  260. * The dso__delete function calls close_dso function to ensure the
  261. * data file descriptor gets closed/unmapped before the dso object
  262. * is freed.
  263. *
  264. * TODO
  265. */
  266. int dso__data_get_fd(struct dso *dso, struct machine *machine);
  267. void dso__data_put_fd(struct dso *dso __maybe_unused);
  268. void dso__data_close(struct dso *dso);
  269. off_t dso__data_size(struct dso *dso, struct machine *machine);
  270. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  271. u64 offset, u8 *data, ssize_t size);
  272. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  273. struct machine *machine, u64 addr,
  274. u8 *data, ssize_t size);
  275. bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by);
  276. struct map *dso__new_map(const char *name);
  277. struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
  278. const char *short_name, int dso_type);
  279. void __dsos__add(struct dsos *dsos, struct dso *dso);
  280. void dsos__add(struct dsos *dsos, struct dso *dso);
  281. struct dso *__dsos__addnew(struct dsos *dsos, const char *name);
  282. struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short);
  283. struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short);
  284. struct dso *__dsos__findnew(struct dsos *dsos, const char *name);
  285. struct dso *dsos__findnew(struct dsos *dsos, const char *name);
  286. bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
  287. void dso__reset_find_symbol_cache(struct dso *dso);
  288. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  289. bool (skip)(struct dso *dso, int parm), int parm);
  290. size_t __dsos__fprintf(struct list_head *head, FILE *fp);
  291. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp);
  292. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  293. enum map_type type, FILE *fp);
  294. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp);
  295. static inline bool dso__is_vmlinux(struct dso *dso)
  296. {
  297. return dso->binary_type == DSO_BINARY_TYPE__VMLINUX ||
  298. dso->binary_type == DSO_BINARY_TYPE__GUEST_VMLINUX;
  299. }
  300. static inline bool dso__is_kcore(struct dso *dso)
  301. {
  302. return dso->binary_type == DSO_BINARY_TYPE__KCORE ||
  303. dso->binary_type == DSO_BINARY_TYPE__GUEST_KCORE;
  304. }
  305. void dso__free_a2l(struct dso *dso);
  306. enum dso_type dso__type(struct dso *dso, struct machine *machine);
  307. int dso__strerror_load(struct dso *dso, char *buf, size_t buflen);
  308. #endif /* __PERF_DSO */