dso.h 9.1 KB

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