map.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_MAP_H
  3. #define __PERF_MAP_H
  4. #include <linux/refcount.h>
  5. #include <linux/compiler.h>
  6. #include <linux/list.h>
  7. #include <linux/rbtree.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdbool.h>
  11. #include <linux/types.h>
  12. struct dso;
  13. struct maps;
  14. struct machine;
  15. struct map {
  16. union {
  17. struct rb_node rb_node;
  18. struct list_head node;
  19. };
  20. u64 start;
  21. u64 end;
  22. bool erange_warned:1;
  23. bool priv:1;
  24. u32 prot;
  25. u64 pgoff;
  26. u64 reloc;
  27. /* ip -> dso rip */
  28. u64 (*map_ip)(struct map *, u64);
  29. /* dso rip -> ip */
  30. u64 (*unmap_ip)(struct map *, u64);
  31. struct dso *dso;
  32. refcount_t refcnt;
  33. u32 flags;
  34. };
  35. struct kmap;
  36. struct kmap *__map__kmap(struct map *map);
  37. struct kmap *map__kmap(struct map *map);
  38. struct maps *map__kmaps(struct map *map);
  39. static inline u64 map__map_ip(struct map *map, u64 ip)
  40. {
  41. return ip - map->start + map->pgoff;
  42. }
  43. static inline u64 map__unmap_ip(struct map *map, u64 ip)
  44. {
  45. return ip + map->start - map->pgoff;
  46. }
  47. static inline u64 identity__map_ip(struct map *map __maybe_unused, u64 ip)
  48. {
  49. return ip;
  50. }
  51. static inline size_t map__size(const struct map *map)
  52. {
  53. return map->end - map->start;
  54. }
  55. /* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
  56. u64 map__rip_2objdump(struct map *map, u64 rip);
  57. /* objdump address -> memory address */
  58. u64 map__objdump_2mem(struct map *map, u64 ip);
  59. struct symbol;
  60. struct thread;
  61. /* map__for_each_symbol - iterate over the symbols in the given map
  62. *
  63. * @map: the 'struct map *' in which symbols itereated
  64. * @pos: the 'struct symbol *' to use as a loop cursor
  65. * @n: the 'struct rb_node *' to use as a temporary storage
  66. * Note: caller must ensure map->dso is not NULL (map is loaded).
  67. */
  68. #define map__for_each_symbol(map, pos, n) \
  69. dso__for_each_symbol(map->dso, pos, n)
  70. /* map__for_each_symbol_with_name - iterate over the symbols in the given map
  71. * that have the given name
  72. *
  73. * @map: the 'struct map *' in which symbols itereated
  74. * @sym_name: the symbol name
  75. * @pos: the 'struct symbol *' to use as a loop cursor
  76. */
  77. #define __map__for_each_symbol_by_name(map, sym_name, pos) \
  78. for (pos = map__find_symbol_by_name(map, sym_name); \
  79. pos && \
  80. !symbol__match_symbol_name(pos->name, sym_name, \
  81. SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); \
  82. pos = symbol__next_by_name(pos))
  83. #define map__for_each_symbol_by_name(map, sym_name, pos) \
  84. __map__for_each_symbol_by_name(map, sym_name, (pos))
  85. void map__init(struct map *map,
  86. u64 start, u64 end, u64 pgoff, struct dso *dso);
  87. struct dso_id;
  88. struct map *map__new(struct machine *machine, u64 start, u64 len,
  89. u64 pgoff, struct dso_id *id, u32 prot, u32 flags,
  90. char *filename, struct thread *thread);
  91. struct map *map__new2(u64 start, struct dso *dso);
  92. void map__delete(struct map *map);
  93. struct map *map__clone(struct map *map);
  94. static inline struct map *map__get(struct map *map)
  95. {
  96. if (map)
  97. refcount_inc(&map->refcnt);
  98. return map;
  99. }
  100. void map__put(struct map *map);
  101. static inline void __map__zput(struct map **map)
  102. {
  103. map__put(*map);
  104. *map = NULL;
  105. }
  106. #define map__zput(map) __map__zput(&map)
  107. size_t map__fprintf(struct map *map, FILE *fp);
  108. size_t map__fprintf_dsoname(struct map *map, FILE *fp);
  109. char *map__srcline(struct map *map, u64 addr, struct symbol *sym);
  110. int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
  111. FILE *fp);
  112. int map__load(struct map *map);
  113. struct symbol *map__find_symbol(struct map *map, u64 addr);
  114. struct symbol *map__find_symbol_by_name(struct map *map, const char *name);
  115. void map__fixup_start(struct map *map);
  116. void map__fixup_end(struct map *map);
  117. int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name,
  118. u64 addr);
  119. bool __map__is_kernel(const struct map *map);
  120. bool __map__is_extra_kernel_map(const struct map *map);
  121. bool __map__is_bpf_prog(const struct map *map);
  122. bool __map__is_bpf_image(const struct map *map);
  123. bool __map__is_ool(const struct map *map);
  124. static inline bool __map__is_kmodule(const struct map *map)
  125. {
  126. return !__map__is_kernel(map) && !__map__is_extra_kernel_map(map) &&
  127. !__map__is_bpf_prog(map) && !__map__is_ool(map) &&
  128. !__map__is_bpf_image(map);
  129. }
  130. bool map__has_symbols(const struct map *map);
  131. #define ENTRY_TRAMPOLINE_NAME "__entry_SYSCALL_64_trampoline"
  132. static inline bool is_entry_trampoline(const char *name)
  133. {
  134. return !strcmp(name, ENTRY_TRAMPOLINE_NAME);
  135. }
  136. static inline bool is_bpf_image(const char *name)
  137. {
  138. return strncmp(name, "bpf_trampoline_", sizeof("bpf_trampoline_") - 1) == 0 ||
  139. strncmp(name, "bpf_dispatcher_", sizeof("bpf_dispatcher_") - 1) == 0;
  140. }
  141. static inline int is_anon_memory(const char *filename)
  142. {
  143. return !strcmp(filename, "//anon") ||
  144. !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
  145. !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
  146. }
  147. static inline int is_no_dso_memory(const char *filename)
  148. {
  149. return !strncmp(filename, "[stack", 6) ||
  150. !strncmp(filename, "/SYSV", 5) ||
  151. !strcmp(filename, "[heap]");
  152. }
  153. #endif /* __PERF_MAP_H */