blob: 7309d64ce39e17d74416a1af04d57249c0cf5295 [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
130 * @filter: to use when loading the DSO
131 */
132#define __map__for_each_symbol_by_name(map, sym_name, pos, filter) \
133 for (pos = map__find_symbol_by_name(map, sym_name, filter); \
Naveen N. Rao031b84c2015-04-28 17:35:37 +0530134 pos && arch__compare_symbol_names(pos->name, sym_name) == 0; \
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -0300135 pos = symbol__next_by_name(pos))
136
137#define map__for_each_symbol_by_name(map, sym_name, pos) \
138 __map__for_each_symbol_by_name(map, sym_name, (pos), NULL)
139
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200140typedef int (*symbol_filter_t)(struct map *map, struct symbol *sym);
141
Naveen N. Rao031b84c2015-04-28 17:35:37 +0530142int arch__compare_symbol_names(const char *namea, const char *nameb);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300143void map__init(struct map *map, enum map_type type,
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200144 u64 start, u64 end, u64 pgoff, struct dso *dso);
Adrian Hunter2a030682014-07-22 16:17:53 +0300145struct map *map__new(struct machine *machine, u64 start, u64 len,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200146 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
Don Zickus7ef80702014-05-19 15:13:49 -0400147 u64 ino_gen, u32 prot, u32 flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +0300148 char *filename, enum map_type type, struct thread *thread);
Namhyung Kime5a18452012-08-06 13:41:20 +0900149struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300150void map__delete(struct map *map);
151struct map *map__clone(struct map *map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300152
153static inline struct map *map__get(struct map *map)
154{
155 if (map)
156 atomic_inc(&map->refcnt);
157 return map;
158}
159
160void map__put(struct map *map);
161
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300162static inline void __map__zput(struct map **map)
163{
164 map__put(*map);
165 *map = NULL;
166}
167
168#define map__zput(map) __map__zput(&map)
169
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200170int map__overlap(struct map *l, struct map *r);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300171size_t map__fprintf(struct map *map, FILE *fp);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900172size_t map__fprintf_dsoname(struct map *map, FILE *fp);
Adrian Huntercc8fae12013-12-06 09:42:57 +0200173int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
174 FILE *fp);
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200175
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300176int map__load(struct map *map, symbol_filter_t filter);
177struct symbol *map__find_symbol(struct map *map,
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200178 u64 addr, symbol_filter_t filter);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300179struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200180 symbol_filter_t filter);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300181void map__fixup_start(struct map *map);
182void map__fixup_end(struct map *map);
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200183
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300184void map__reloc_vmlinux(struct map *map);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200185
Jiri Olsaacebd402014-07-14 23:46:47 +0200186size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
187 FILE *fp);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300188void maps__insert(struct maps *maps, struct map *map);
189void maps__remove(struct maps *maps, struct map *map);
190struct map *maps__find(struct maps *maps, u64 addr);
191struct map *maps__first(struct maps *maps);
Arnaldo Carvalho de Melo4d4dee92015-05-21 17:48:33 -0300192struct map *map__next(struct map *map);
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300193struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
194 struct map **mapp, symbol_filter_t filter);
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300195void map_groups__init(struct map_groups *mg, struct machine *machine);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300196void map_groups__exit(struct map_groups *mg);
197int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300198 struct map_groups *parent, enum map_type type);
Jiri Olsaacebd402014-07-14 23:46:47 +0200199size_t map_groups__fprintf(struct map_groups *mg, FILE *fp);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300200
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200201int maps__set_kallsyms_ref_reloc_sym(struct map **maps, const char *symbol_name,
202 u64 addr);
203
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300204static inline void map_groups__insert(struct map_groups *mg, struct map *map)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300205{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300206 maps__insert(&mg->maps[map->type], map);
207 map->groups = mg;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300208}
209
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300210static inline void map_groups__remove(struct map_groups *mg, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300211{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300212 maps__remove(&mg->maps[map->type], map);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300213}
214
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300215static inline struct map *map_groups__find(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300216 enum map_type type, u64 addr)
217{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300218 return maps__find(&mg->maps[type], addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300219}
220
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300221static inline struct map *map_groups__first(struct map_groups *mg,
222 enum map_type type)
223{
224 return maps__first(&mg->maps[type]);
225}
226
227static inline struct map *map_groups__next(struct map *map)
228{
Arnaldo Carvalho de Melo4d4dee92015-05-21 17:48:33 -0300229 return map__next(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300230}
231
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300232struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300233 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300234 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300235 symbol_filter_t filter);
236
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300237struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300238 enum map_type type,
239 const char *name,
240 struct map **mapp,
241 symbol_filter_t filter);
242
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300243struct addr_map_symbol;
244
245int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter);
246
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300247static inline
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300248struct symbol *map_groups__find_function_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300249 const char *name, struct map **mapp,
250 symbol_filter_t filter)
251{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300252 return map_groups__find_symbol_by_name(mg, MAP__FUNCTION, name, mapp, filter);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300253}
254
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300255int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Jiri Olsaacebd402014-07-14 23:46:47 +0200256 FILE *fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300257
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300258struct map *map_groups__find_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300259 enum map_type type, const char *name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800260
Arnaldo Carvalho de Meloe6ce71262015-07-23 11:06:16 -0300261bool __map__is_kernel(const struct map *map);
262
263static inline bool __map__is_kmodule(const struct map *map)
264{
265 return !__map__is_kernel(map);
266}
267
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200268#endif /* __PERF_MAP_H */