1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_MAPS_H
3 #define __PERF_MAPS_H
4
5 #include <linux/refcount.h>
6 #include <linux/rbtree.h>
7 #include <stdio.h>
8 #include <stdbool.h>
9 #include <linux/types.h>
10 #include "rwsem.h"
11 #include <internal/rc_check.h>
12
13 struct ref_reloc_sym;
14 struct machine;
15 struct map;
16 struct maps;
17 struct thread;
18
19 struct map_rb_node {
20 struct rb_node rb_node;
21 struct map *map;
22 };
23
24 struct map_list_node {
25 struct list_head node;
26 struct map *map;
27 };
28
map_list_node__new(void)29 static inline struct map_list_node *map_list_node__new(void)
30 {
31 return malloc(sizeof(struct map_list_node));
32 }
33
34 struct map_rb_node *maps__first(struct maps *maps);
35 struct map_rb_node *map_rb_node__next(struct map_rb_node *node);
36 struct map_rb_node *maps__find_node(struct maps *maps, struct map *map);
37 struct map *maps__find(struct maps *maps, u64 addr);
38
39 #define maps__for_each_entry(maps, map) \
40 for (map = maps__first(maps); map; map = map_rb_node__next(map))
41
42 #define maps__for_each_entry_safe(maps, map, next) \
43 for (map = maps__first(maps), next = map_rb_node__next(map); map; \
44 map = next, next = map_rb_node__next(map))
45
DECLARE_RC_STRUCT(maps)46 DECLARE_RC_STRUCT(maps) {
47 struct rb_root entries;
48 struct rw_semaphore lock;
49 struct machine *machine;
50 struct map *last_search_by_name;
51 struct map **maps_by_name;
52 refcount_t refcnt;
53 unsigned int nr_maps;
54 unsigned int nr_maps_allocated;
55 #ifdef HAVE_LIBUNWIND_SUPPORT
56 void *addr_space;
57 const struct unwind_libunwind_ops *unwind_libunwind_ops;
58 #endif
59 };
60
61 #define KMAP_NAME_LEN 256
62
63 struct kmap {
64 struct ref_reloc_sym *ref_reloc_sym;
65 struct maps *kmaps;
66 char name[KMAP_NAME_LEN];
67 };
68
69 struct maps *maps__new(struct machine *machine);
70 bool maps__empty(struct maps *maps);
71 int maps__clone(struct thread *thread, struct maps *parent);
72
73 struct maps *maps__get(struct maps *maps);
74 void maps__put(struct maps *maps);
75
__maps__zput(struct maps ** map)76 static inline void __maps__zput(struct maps **map)
77 {
78 maps__put(*map);
79 *map = NULL;
80 }
81
82 #define maps__zput(map) __maps__zput(&map)
83
maps__entries(struct maps * maps)84 static inline struct rb_root *maps__entries(struct maps *maps)
85 {
86 return &RC_CHK_ACCESS(maps)->entries;
87 }
88
maps__machine(struct maps * maps)89 static inline struct machine *maps__machine(struct maps *maps)
90 {
91 return RC_CHK_ACCESS(maps)->machine;
92 }
93
maps__lock(struct maps * maps)94 static inline struct rw_semaphore *maps__lock(struct maps *maps)
95 {
96 return &RC_CHK_ACCESS(maps)->lock;
97 }
98
maps__maps_by_name(struct maps * maps)99 static inline struct map **maps__maps_by_name(struct maps *maps)
100 {
101 return RC_CHK_ACCESS(maps)->maps_by_name;
102 }
103
maps__nr_maps(const struct maps * maps)104 static inline unsigned int maps__nr_maps(const struct maps *maps)
105 {
106 return RC_CHK_ACCESS(maps)->nr_maps;
107 }
108
maps__refcnt(struct maps * maps)109 static inline refcount_t *maps__refcnt(struct maps *maps)
110 {
111 return &RC_CHK_ACCESS(maps)->refcnt;
112 }
113
114 #ifdef HAVE_LIBUNWIND_SUPPORT
maps__addr_space(struct maps * maps)115 static inline void *maps__addr_space(struct maps *maps)
116 {
117 return RC_CHK_ACCESS(maps)->addr_space;
118 }
119
maps__unwind_libunwind_ops(const struct maps * maps)120 static inline const struct unwind_libunwind_ops *maps__unwind_libunwind_ops(const struct maps *maps)
121 {
122 return RC_CHK_ACCESS(maps)->unwind_libunwind_ops;
123 }
124 #endif
125
126 size_t maps__fprintf(struct maps *maps, FILE *fp);
127
128 int maps__insert(struct maps *maps, struct map *map);
129 void maps__remove(struct maps *maps, struct map *map);
130
131 struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp);
132 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp);
133
134 struct addr_map_symbol;
135
136 int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams);
137
138 int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp);
139
140 struct map *maps__find_by_name(struct maps *maps, const char *name);
141
142 int maps__merge_in(struct maps *kmaps, struct map *new_map);
143
144 void __maps__sort_by_name(struct maps *maps);
145
146 void maps__fixup_end(struct maps *maps);
147
148 #endif // __PERF_MAPS_H
149