dso.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #define DSO__SWAP(dso, type, val) \
  56. ({ \
  57. type ____r = val; \
  58. BUG_ON(dso->needs_swap == DSO_SWAP__UNSET); \
  59. if (dso->needs_swap == DSO_SWAP__YES) { \
  60. switch (sizeof(____r)) { \
  61. case 2: \
  62. ____r = bswap_16(val); \
  63. break; \
  64. case 4: \
  65. ____r = bswap_32(val); \
  66. break; \
  67. case 8: \
  68. ____r = bswap_64(val); \
  69. break; \
  70. default: \
  71. BUG_ON(1); \
  72. } \
  73. } \
  74. ____r; \
  75. })
  76. #define DSO__DATA_CACHE_SIZE 4096
  77. #define DSO__DATA_CACHE_MASK ~(DSO__DATA_CACHE_SIZE - 1)
  78. struct dso_cache {
  79. struct rb_node rb_node;
  80. u64 offset;
  81. u64 size;
  82. char data[0];
  83. };
  84. /*
  85. * DSOs are put into both a list for fast iteration and rbtree for fast
  86. * long name lookup.
  87. */
  88. struct dsos {
  89. struct list_head head;
  90. struct rb_root root; /* rbtree root sorted by long name */
  91. };
  92. struct dso {
  93. struct list_head node;
  94. struct rb_node rb_node; /* rbtree node sorted by long name */
  95. struct rb_root symbols[MAP__NR_TYPES];
  96. struct rb_root symbol_names[MAP__NR_TYPES];
  97. void *a2l;
  98. char *symsrc_filename;
  99. unsigned int a2l_fails;
  100. enum dso_kernel_type kernel;
  101. enum dso_swap_type needs_swap;
  102. enum dso_binary_type symtab_type;
  103. enum dso_binary_type binary_type;
  104. u8 adjust_symbols:1;
  105. u8 has_build_id:1;
  106. u8 has_srcline:1;
  107. u8 hit:1;
  108. u8 annotate_warned:1;
  109. u8 short_name_allocated:1;
  110. u8 long_name_allocated:1;
  111. u8 is_64_bit:1;
  112. u8 sorted_by_name;
  113. u8 loaded;
  114. u8 rel;
  115. u8 build_id[BUILD_ID_SIZE];
  116. const char *short_name;
  117. const char *long_name;
  118. u16 long_name_len;
  119. u16 short_name_len;
  120. void *dwfl; /* DWARF debug info */
  121. /* dso data file */
  122. struct {
  123. struct rb_root cache;
  124. int fd;
  125. int status;
  126. u32 status_seen;
  127. size_t file_size;
  128. struct list_head open_entry;
  129. u64 frame_offset;
  130. } data;
  131. union { /* Tool specific area */
  132. void *priv;
  133. u64 db_id;
  134. };
  135. char name[0];
  136. };
  137. /* dso__for_each_symbol - iterate over the symbols of given type
  138. *
  139. * @dso: the 'struct dso *' in which symbols itereated
  140. * @pos: the 'struct symbol *' to use as a loop cursor
  141. * @n: the 'struct rb_node *' to use as a temporary storage
  142. * @type: the 'enum map_type' type of symbols
  143. */
  144. #define dso__for_each_symbol(dso, pos, n, type) \
  145. symbols__for_each_entry(&(dso)->symbols[(type)], pos, n)
  146. static inline void dso__set_loaded(struct dso *dso, enum map_type type)
  147. {
  148. dso->loaded |= (1 << type);
  149. }
  150. struct dso *dso__new(const char *name);
  151. void dso__delete(struct dso *dso);
  152. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated);
  153. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated);
  154. int dso__name_len(const struct dso *dso);
  155. bool dso__loaded(const struct dso *dso, enum map_type type);
  156. bool dso__sorted_by_name(const struct dso *dso, enum map_type type);
  157. void dso__set_sorted_by_name(struct dso *dso, enum map_type type);
  158. void dso__sort_by_name(struct dso *dso, enum map_type type);
  159. void dso__set_build_id(struct dso *dso, void *build_id);
  160. bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
  161. void dso__read_running_kernel_build_id(struct dso *dso,
  162. struct machine *machine);
  163. int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir);
  164. char dso__symtab_origin(const struct dso *dso);
  165. int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type,
  166. char *root_dir, char *filename, size_t size);
  167. bool is_supported_compression(const char *ext);
  168. bool is_kmodule_extension(const char *ext);
  169. bool is_kernel_module(const char *pathname, bool *compressed);
  170. bool decompress_to_file(const char *ext, const char *filename, int output_fd);
  171. bool dso__needs_decompress(struct dso *dso);
  172. /*
  173. * The dso__data_* external interface provides following functions:
  174. * dso__data_fd
  175. * dso__data_close
  176. * dso__data_size
  177. * dso__data_read_offset
  178. * dso__data_read_addr
  179. *
  180. * Please refer to the dso.c object code for each function and
  181. * arguments documentation. Following text tries to explain the
  182. * dso file descriptor caching.
  183. *
  184. * The dso__data* interface allows caching of opened file descriptors
  185. * to speed up the dso data accesses. The idea is to leave the file
  186. * descriptor opened ideally for the whole life of the dso object.
  187. *
  188. * The current usage of the dso__data_* interface is as follows:
  189. *
  190. * Get DSO's fd:
  191. * int fd = dso__data_fd(dso, machine);
  192. * USE 'fd' SOMEHOW
  193. *
  194. * Read DSO's data:
  195. * n = dso__data_read_offset(dso_0, &machine, 0, buf, BUFSIZE);
  196. * n = dso__data_read_addr(dso_0, &machine, 0, buf, BUFSIZE);
  197. *
  198. * Eventually close DSO's fd:
  199. * dso__data_close(dso);
  200. *
  201. * It is not necessary to close the DSO object data file. Each time new
  202. * DSO data file is opened, the limit (RLIMIT_NOFILE/2) is checked. Once
  203. * it is crossed, the oldest opened DSO object is closed.
  204. *
  205. * The dso__delete function calls close_dso function to ensure the
  206. * data file descriptor gets closed/unmapped before the dso object
  207. * is freed.
  208. *
  209. * TODO
  210. */
  211. int dso__data_fd(struct dso *dso, struct machine *machine);
  212. void dso__data_close(struct dso *dso);
  213. off_t dso__data_size(struct dso *dso, struct machine *machine);
  214. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  215. u64 offset, u8 *data, ssize_t size);
  216. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  217. struct machine *machine, u64 addr,
  218. u8 *data, ssize_t size);
  219. bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by);
  220. struct map *dso__new_map(const char *name);
  221. struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
  222. const char *short_name, int dso_type);
  223. void dsos__add(struct dsos *dsos, struct dso *dso);
  224. struct dso *dsos__find(const struct dsos *dsos, const char *name,
  225. bool cmp_short);
  226. struct dso *__dsos__findnew(struct dsos *dsos, const char *name);
  227. bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
  228. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  229. bool (skip)(struct dso *dso, int parm), int parm);
  230. size_t __dsos__fprintf(struct list_head *head, FILE *fp);
  231. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp);
  232. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  233. enum map_type type, FILE *fp);
  234. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp);
  235. static inline bool dso__is_vmlinux(struct dso *dso)
  236. {
  237. return dso->binary_type == DSO_BINARY_TYPE__VMLINUX ||
  238. dso->binary_type == DSO_BINARY_TYPE__GUEST_VMLINUX;
  239. }
  240. static inline bool dso__is_kcore(struct dso *dso)
  241. {
  242. return dso->binary_type == DSO_BINARY_TYPE__KCORE ||
  243. dso->binary_type == DSO_BINARY_TYPE__GUEST_KCORE;
  244. }
  245. void dso__free_a2l(struct dso *dso);
  246. enum dso_type dso__type(struct dso *dso, struct machine *machine);
  247. #endif /* __PERF_DSO */