dso.h 9.8 KB

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