dso.h 10 KB

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