dso.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. struct dso {
  83. struct list_head node;
  84. struct rb_root symbols[MAP__NR_TYPES];
  85. struct rb_root symbol_names[MAP__NR_TYPES];
  86. void *a2l;
  87. char *symsrc_filename;
  88. unsigned int a2l_fails;
  89. enum dso_kernel_type kernel;
  90. enum dso_swap_type needs_swap;
  91. enum dso_binary_type symtab_type;
  92. enum dso_binary_type binary_type;
  93. u8 adjust_symbols:1;
  94. u8 has_build_id:1;
  95. u8 has_srcline:1;
  96. u8 hit:1;
  97. u8 annotate_warned:1;
  98. u8 short_name_allocated:1;
  99. u8 long_name_allocated:1;
  100. u8 is_64_bit:1;
  101. u8 sorted_by_name;
  102. u8 loaded;
  103. u8 rel;
  104. u8 build_id[BUILD_ID_SIZE];
  105. const char *short_name;
  106. const char *long_name;
  107. u16 long_name_len;
  108. u16 short_name_len;
  109. /* dso data file */
  110. struct {
  111. struct rb_root cache;
  112. int fd;
  113. int status;
  114. u32 status_seen;
  115. size_t file_size;
  116. struct list_head open_entry;
  117. } data;
  118. char name[0];
  119. };
  120. /* dso__for_each_symbol - iterate over the symbols of given type
  121. *
  122. * @dso: the 'struct dso *' in which symbols itereated
  123. * @pos: the 'struct symbol *' to use as a loop cursor
  124. * @n: the 'struct rb_node *' to use as a temporary storage
  125. * @type: the 'enum map_type' type of symbols
  126. */
  127. #define dso__for_each_symbol(dso, pos, n, type) \
  128. symbols__for_each_entry(&(dso)->symbols[(type)], pos, n)
  129. static inline void dso__set_loaded(struct dso *dso, enum map_type type)
  130. {
  131. dso->loaded |= (1 << type);
  132. }
  133. struct dso *dso__new(const char *name);
  134. void dso__delete(struct dso *dso);
  135. void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated);
  136. void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated);
  137. int dso__name_len(const struct dso *dso);
  138. bool dso__loaded(const struct dso *dso, enum map_type type);
  139. bool dso__sorted_by_name(const struct dso *dso, enum map_type type);
  140. void dso__set_sorted_by_name(struct dso *dso, enum map_type type);
  141. void dso__sort_by_name(struct dso *dso, enum map_type type);
  142. void dso__set_build_id(struct dso *dso, void *build_id);
  143. bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
  144. void dso__read_running_kernel_build_id(struct dso *dso,
  145. struct machine *machine);
  146. int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir);
  147. char dso__symtab_origin(const struct dso *dso);
  148. int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type,
  149. char *root_dir, char *filename, size_t size);
  150. /*
  151. * The dso__data_* external interface provides following functions:
  152. * dso__data_fd
  153. * dso__data_close
  154. * dso__data_size
  155. * dso__data_read_offset
  156. * dso__data_read_addr
  157. *
  158. * Please refer to the dso.c object code for each function and
  159. * arguments documentation. Following text tries to explain the
  160. * dso file descriptor caching.
  161. *
  162. * The dso__data* interface allows caching of opened file descriptors
  163. * to speed up the dso data accesses. The idea is to leave the file
  164. * descriptor opened ideally for the whole life of the dso object.
  165. *
  166. * The current usage of the dso__data_* interface is as follows:
  167. *
  168. * Get DSO's fd:
  169. * int fd = dso__data_fd(dso, machine);
  170. * USE 'fd' SOMEHOW
  171. *
  172. * Read DSO's data:
  173. * n = dso__data_read_offset(dso_0, &machine, 0, buf, BUFSIZE);
  174. * n = dso__data_read_addr(dso_0, &machine, 0, buf, BUFSIZE);
  175. *
  176. * Eventually close DSO's fd:
  177. * dso__data_close(dso);
  178. *
  179. * It is not necessary to close the DSO object data file. Each time new
  180. * DSO data file is opened, the limit (RLIMIT_NOFILE/2) is checked. Once
  181. * it is crossed, the oldest opened DSO object is closed.
  182. *
  183. * The dso__delete function calls close_dso function to ensure the
  184. * data file descriptor gets closed/unmapped before the dso object
  185. * is freed.
  186. *
  187. * TODO
  188. */
  189. int dso__data_fd(struct dso *dso, struct machine *machine);
  190. void dso__data_close(struct dso *dso);
  191. off_t dso__data_size(struct dso *dso, struct machine *machine);
  192. ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
  193. u64 offset, u8 *data, ssize_t size);
  194. ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
  195. struct machine *machine, u64 addr,
  196. u8 *data, ssize_t size);
  197. bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by);
  198. struct map *dso__new_map(const char *name);
  199. struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
  200. const char *short_name, int dso_type);
  201. void dsos__add(struct list_head *head, struct dso *dso);
  202. struct dso *dsos__find(const struct list_head *head, const char *name,
  203. bool cmp_short);
  204. struct dso *__dsos__findnew(struct list_head *head, const char *name);
  205. bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
  206. size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
  207. bool (skip)(struct dso *dso, int parm), int parm);
  208. size_t __dsos__fprintf(struct list_head *head, FILE *fp);
  209. size_t dso__fprintf_buildid(struct dso *dso, FILE *fp);
  210. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  211. enum map_type type, FILE *fp);
  212. size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp);
  213. static inline bool dso__is_vmlinux(struct dso *dso)
  214. {
  215. return dso->binary_type == DSO_BINARY_TYPE__VMLINUX ||
  216. dso->binary_type == DSO_BINARY_TYPE__GUEST_VMLINUX;
  217. }
  218. static inline bool dso__is_kcore(struct dso *dso)
  219. {
  220. return dso->binary_type == DSO_BINARY_TYPE__KCORE ||
  221. dso->binary_type == DSO_BINARY_TYPE__GUEST_KCORE;
  222. }
  223. void dso__free_a2l(struct dso *dso);
  224. enum dso_type dso__type(struct dso *dso, struct machine *machine);
  225. #endif /* __PERF_DSO */