dso.h 7.4 KB

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