blob: f9e8ac8a52cde5fefb74cb87464c1346e3d08431 [file] [log] [blame]
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -02001#ifndef __PERF_MAP_H
2#define __PERF_MAP_H
3
Elena Reshetovae3a42cd2017-02-21 17:34:59 +02004#include <linux/refcount.h>
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -02005#include <linux/compiler.h>
6#include <linux/list.h>
7#include <linux/rbtree.h>
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03008#include <pthread.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03009#include <stdio.h>
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -030010#include <stdbool.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +020011#include <linux/types.h>
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020012
13enum map_type {
14 MAP__FUNCTION = 0,
15 MAP__VARIABLE,
16};
17
18#define MAP__NR_TYPES (MAP__VARIABLE + 1)
19
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030020extern const char *map_type__name[MAP__NR_TYPES];
21
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020022struct dso;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020023struct ip_callchain;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020024struct ref_reloc_sym;
25struct map_groups;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -030026struct machine;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020027struct perf_evsel;
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020028
29struct map {
30 union {
31 struct rb_node rb_node;
32 struct list_head node;
33 };
34 u64 start;
35 u64 end;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -030036 u8 /* enum map_type */ type;
Arnaldo Carvalho de Melo31d68e72012-03-27 12:55:57 -030037 bool erange_warned;
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -030038 u32 priv;
Don Zickus7ef80702014-05-19 15:13:49 -040039 u32 prot;
40 u32 flags;
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020041 u64 pgoff;
Adrian Hunter91767532014-01-29 16:14:36 +020042 u64 reloc;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020043 u32 maj, min; /* only valid for MMAP2 record */
44 u64 ino; /* only valid for MMAP2 record */
45 u64 ino_generation;/* only valid for MMAP2 record */
Kirill Smelkov7a2b6202010-02-03 16:52:07 -020046
47 /* ip -> dso rip */
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020048 u64 (*map_ip)(struct map *, u64);
Kirill Smelkov7a2b6202010-02-03 16:52:07 -020049 /* dso rip -> ip */
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020050 u64 (*unmap_ip)(struct map *, u64);
Kirill Smelkov7a2b6202010-02-03 16:52:07 -020051
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020052 struct dso *dso;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080053 struct map_groups *groups;
Elena Reshetovae3a42cd2017-02-21 17:34:59 +020054 refcount_t refcnt;
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020055};
56
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020057struct kmap {
58 struct ref_reloc_sym *ref_reloc_sym;
59 struct map_groups *kmaps;
60};
61
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -030062struct maps {
63 struct rb_root entries;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -030064 pthread_rwlock_t lock;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -030065};
66
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080067struct map_groups {
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -030068 struct maps maps[MAP__NR_TYPES];
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -030069 struct machine *machine;
Elena Reshetovaead05e82017-02-21 17:35:00 +020070 refcount_t refcnt;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080071};
72
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030073struct map_groups *map_groups__new(struct machine *machine);
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -030074void map_groups__delete(struct map_groups *mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +030075bool map_groups__empty(struct map_groups *mg);
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -030076
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -030077static inline struct map_groups *map_groups__get(struct map_groups *mg)
78{
Arnaldo Carvalho de Melo848cbd22015-05-15 15:20:44 -030079 if (mg)
Elena Reshetovaead05e82017-02-21 17:35:00 +020080 refcount_inc(&mg->refcnt);
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -030081 return mg;
82}
83
84void map_groups__put(struct map_groups *mg);
85
Wang Nanba927322015-04-07 08:22:45 +000086struct kmap *map__kmap(struct map *map);
87struct map_groups *map__kmaps(struct map *map);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020088
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020089static inline u64 map__map_ip(struct map *map, u64 ip)
90{
91 return ip - map->start + map->pgoff;
92}
93
94static inline u64 map__unmap_ip(struct map *map, u64 ip)
95{
96 return ip + map->start - map->pgoff;
97}
98
Irina Tirdea1d037ca2012-09-11 01:15:03 +030099static inline u64 identity__map_ip(struct map *map __maybe_unused, u64 ip)
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200100{
101 return ip;
102}
103
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200104
Kirill Smelkovee11b902010-02-07 11:46:15 -0200105/* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200106u64 map__rip_2objdump(struct map *map, u64 rip);
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200107
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300108/* objdump address -> memory address */
109u64 map__objdump_2mem(struct map *map, u64 ip);
110
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200111struct symbol;
Adrian Hunter5835edd2014-07-22 16:18:00 +0300112struct thread;
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200113
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000114/* map__for_each_symbol - iterate over the symbols in the given map
115 *
116 * @map: the 'struct map *' in which symbols itereated
117 * @pos: the 'struct symbol *' to use as a loop cursor
118 * @n: the 'struct rb_node *' to use as a temporary storage
119 * Note: caller must ensure map->dso is not NULL (map is loaded).
120 */
121#define map__for_each_symbol(map, pos, n) \
122 dso__for_each_symbol(map->dso, pos, n, map->type)
123
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -0300124/* map__for_each_symbol_with_name - iterate over the symbols in the given map
125 * that have the given name
126 *
127 * @map: the 'struct map *' in which symbols itereated
128 * @sym_name: the symbol name
129 * @pos: the 'struct symbol *' to use as a loop cursor
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -0300130 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300131#define __map__for_each_symbol_by_name(map, sym_name, pos) \
132 for (pos = map__find_symbol_by_name(map, sym_name); \
Paul Clarked80406452017-04-25 13:15:49 -0500133 pos && \
134 !symbol__match_symbol_name(pos->name, sym_name, \
135 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); \
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -0300136 pos = symbol__next_by_name(pos))
137
138#define map__for_each_symbol_by_name(map, sym_name, pos) \
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300139 __map__for_each_symbol_by_name(map, sym_name, (pos))
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200140
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300141void map__init(struct map *map, enum map_type type,
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200142 u64 start, u64 end, u64 pgoff, struct dso *dso);
Adrian Hunter2a030682014-07-22 16:17:53 +0300143struct map *map__new(struct machine *machine, u64 start, u64 len,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200144 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
Don Zickus7ef80702014-05-19 15:13:49 -0400145 u64 ino_gen, u32 prot, u32 flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +0300146 char *filename, enum map_type type, struct thread *thread);
Namhyung Kime5a18452012-08-06 13:41:20 +0900147struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300148void map__delete(struct map *map);
149struct map *map__clone(struct map *map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300150
151static inline struct map *map__get(struct map *map)
152{
153 if (map)
Elena Reshetovae3a42cd2017-02-21 17:34:59 +0200154 refcount_inc(&map->refcnt);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300155 return map;
156}
157
158void map__put(struct map *map);
159
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300160static inline void __map__zput(struct map **map)
161{
162 map__put(*map);
163 *map = NULL;
164}
165
166#define map__zput(map) __map__zput(&map)
167
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200168int map__overlap(struct map *l, struct map *r);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300169size_t map__fprintf(struct map *map, FILE *fp);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900170size_t map__fprintf_dsoname(struct map *map, FILE *fp);
Adrian Huntercc8fae12013-12-06 09:42:57 +0200171int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
172 FILE *fp);
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200173
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300174int map__load(struct map *map);
175struct symbol *map__find_symbol(struct map *map, u64 addr);
176struct symbol *map__find_symbol_by_name(struct map *map, const char *name);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300177void map__fixup_start(struct map *map);
178void map__fixup_end(struct map *map);
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200179
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300180void map__reloc_vmlinux(struct map *map);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200181
Jiri Olsaacebd402014-07-14 23:46:47 +0200182size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
183 FILE *fp);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300184void maps__insert(struct maps *maps, struct map *map);
185void maps__remove(struct maps *maps, struct map *map);
186struct map *maps__find(struct maps *maps, u64 addr);
187struct map *maps__first(struct maps *maps);
Arnaldo Carvalho de Melo4d4dee92015-05-21 17:48:33 -0300188struct map *map__next(struct map *map);
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300189struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300190 struct map **mapp);
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300191void map_groups__init(struct map_groups *mg, struct machine *machine);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300192void map_groups__exit(struct map_groups *mg);
Jiri Olsa6c502582016-07-04 14:16:23 +0200193int map_groups__clone(struct thread *thread,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300194 struct map_groups *parent, enum map_type type);
Jiri Olsaacebd402014-07-14 23:46:47 +0200195size_t map_groups__fprintf(struct map_groups *mg, FILE *fp);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300196
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200197int maps__set_kallsyms_ref_reloc_sym(struct map **maps, const char *symbol_name,
198 u64 addr);
199
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300200static inline void map_groups__insert(struct map_groups *mg, struct map *map)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300201{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300202 maps__insert(&mg->maps[map->type], map);
203 map->groups = mg;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300204}
205
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300206static inline void map_groups__remove(struct map_groups *mg, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300207{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300208 maps__remove(&mg->maps[map->type], map);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300209}
210
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300211static inline struct map *map_groups__find(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300212 enum map_type type, u64 addr)
213{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300214 return maps__find(&mg->maps[type], addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300215}
216
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300217static inline struct map *map_groups__first(struct map_groups *mg,
218 enum map_type type)
219{
220 return maps__first(&mg->maps[type]);
221}
222
223static inline struct map *map_groups__next(struct map *map)
224{
Arnaldo Carvalho de Melo4d4dee92015-05-21 17:48:33 -0300225 return map__next(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300226}
227
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300228struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300229 enum map_type type, u64 addr,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300230 struct map **mapp);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300231
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300232struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300233 enum map_type type,
234 const char *name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300235 struct map **mapp);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300236
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300237struct addr_map_symbol;
238
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300239int map_groups__find_ams(struct addr_map_symbol *ams);
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300240
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300241static inline
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300242struct symbol *map_groups__find_function_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300243 const char *name, struct map **mapp)
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300244{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300245 return map_groups__find_symbol_by_name(mg, MAP__FUNCTION, name, mapp);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300246}
247
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300248int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Jiri Olsaacebd402014-07-14 23:46:47 +0200249 FILE *fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300250
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300251struct map *map_groups__find_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300252 enum map_type type, const char *name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800253
Arnaldo Carvalho de Meloe6ce71262015-07-23 11:06:16 -0300254bool __map__is_kernel(const struct map *map);
255
256static inline bool __map__is_kmodule(const struct map *map)
257{
258 return !__map__is_kernel(map);
259}
260
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200261#endif /* __PERF_MAP_H */