blob: abdacf800c98a78148565f23b7d9b4c916cbacaf [file] [log] [blame]
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -02001#ifndef __PERF_MAP_H
2#define __PERF_MAP_H
3
Arnaldo Carvalho de Melo848cbd22015-05-15 15:20:44 -03004#include <linux/atomic.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;
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -030054 atomic_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;
Arnaldo Carvalho de Melo848cbd22015-05-15 15:20:44 -030070 atomic_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)
80 atomic_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); \
Naveen N. Rao031b84c2015-04-28 17:35:37 +0530133 pos && arch__compare_symbol_names(pos->name, sym_name) == 0; \
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -0300134 pos = symbol__next_by_name(pos))
135
136#define map__for_each_symbol_by_name(map, sym_name, pos) \
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300137 __map__for_each_symbol_by_name(map, sym_name, (pos))
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200138
Naveen N. Rao031b84c2015-04-28 17:35:37 +0530139int arch__compare_symbol_names(const char *namea, const char *nameb);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300140void map__init(struct map *map, enum map_type type,
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200141 u64 start, u64 end, u64 pgoff, struct dso *dso);
Adrian Hunter2a030682014-07-22 16:17:53 +0300142struct map *map__new(struct machine *machine, u64 start, u64 len,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200143 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
Don Zickus7ef80702014-05-19 15:13:49 -0400144 u64 ino_gen, u32 prot, u32 flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +0300145 char *filename, enum map_type type, struct thread *thread);
Namhyung Kime5a18452012-08-06 13:41:20 +0900146struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300147void map__delete(struct map *map);
148struct map *map__clone(struct map *map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300149
150static inline struct map *map__get(struct map *map)
151{
152 if (map)
153 atomic_inc(&map->refcnt);
154 return map;
155}
156
157void map__put(struct map *map);
158
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300159static inline void __map__zput(struct map **map)
160{
161 map__put(*map);
162 *map = NULL;
163}
164
165#define map__zput(map) __map__zput(&map)
166
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200167int map__overlap(struct map *l, struct map *r);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300168size_t map__fprintf(struct map *map, FILE *fp);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900169size_t map__fprintf_dsoname(struct map *map, FILE *fp);
Adrian Huntercc8fae12013-12-06 09:42:57 +0200170int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
171 FILE *fp);
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200172
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300173int map__load(struct map *map);
174struct symbol *map__find_symbol(struct map *map, u64 addr);
175struct symbol *map__find_symbol_by_name(struct map *map, const char *name);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300176void map__fixup_start(struct map *map);
177void map__fixup_end(struct map *map);
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200178
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300179void map__reloc_vmlinux(struct map *map);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200180
Jiri Olsaacebd402014-07-14 23:46:47 +0200181size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
182 FILE *fp);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300183void maps__insert(struct maps *maps, struct map *map);
184void maps__remove(struct maps *maps, struct map *map);
185struct map *maps__find(struct maps *maps, u64 addr);
186struct map *maps__first(struct maps *maps);
Arnaldo Carvalho de Melo4d4dee92015-05-21 17:48:33 -0300187struct map *map__next(struct map *map);
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300188struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300189 struct map **mapp);
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300190void map_groups__init(struct map_groups *mg, struct machine *machine);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300191void map_groups__exit(struct map_groups *mg);
Jiri Olsa6c502582016-07-04 14:16:23 +0200192int map_groups__clone(struct thread *thread,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300193 struct map_groups *parent, enum map_type type);
Jiri Olsaacebd402014-07-14 23:46:47 +0200194size_t map_groups__fprintf(struct map_groups *mg, FILE *fp);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300195
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200196int maps__set_kallsyms_ref_reloc_sym(struct map **maps, const char *symbol_name,
197 u64 addr);
198
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300199static inline void map_groups__insert(struct map_groups *mg, struct map *map)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300200{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300201 maps__insert(&mg->maps[map->type], map);
202 map->groups = mg;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300203}
204
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300205static inline void map_groups__remove(struct map_groups *mg, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300206{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300207 maps__remove(&mg->maps[map->type], map);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300208}
209
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300210static inline struct map *map_groups__find(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300211 enum map_type type, u64 addr)
212{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300213 return maps__find(&mg->maps[type], addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300214}
215
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300216static inline struct map *map_groups__first(struct map_groups *mg,
217 enum map_type type)
218{
219 return maps__first(&mg->maps[type]);
220}
221
222static inline struct map *map_groups__next(struct map *map)
223{
Arnaldo Carvalho de Melo4d4dee92015-05-21 17:48:33 -0300224 return map__next(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300225}
226
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300227struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300228 enum map_type type, u64 addr,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300229 struct map **mapp);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300230
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300231struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300232 enum map_type type,
233 const char *name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300234 struct map **mapp);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300235
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300236struct addr_map_symbol;
237
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300238int map_groups__find_ams(struct addr_map_symbol *ams);
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300239
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300240static inline
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300241struct symbol *map_groups__find_function_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300242 const char *name, struct map **mapp)
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300243{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300244 return map_groups__find_symbol_by_name(mg, MAP__FUNCTION, name, mapp);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300245}
246
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300247int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Jiri Olsaacebd402014-07-14 23:46:47 +0200248 FILE *fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300249
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300250struct map *map_groups__find_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300251 enum map_type type, const char *name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800252
Arnaldo Carvalho de Meloe6ce71262015-07-23 11:06:16 -0300253bool __map__is_kernel(const struct map *map);
254
255static inline bool __map__is_kmodule(const struct map *map)
256{
257 return !__map__is_kernel(map);
258}
259
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200260#endif /* __PERF_MAP_H */