blob: 881dba4f82030e9cf731637b4c013d8d70d3d77c [file] [log] [blame]
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -02001#ifndef __PERF_MAP_H
2#define __PERF_MAP_H
3
4#include <linux/compiler.h>
5#include <linux/list.h>
6#include <linux/rbtree.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03007#include <stdio.h>
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03008#include <stdbool.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03009#include "types.h"
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020010
11enum map_type {
12 MAP__FUNCTION = 0,
13 MAP__VARIABLE,
14};
15
16#define MAP__NR_TYPES (MAP__VARIABLE + 1)
17
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030018extern const char *map_type__name[MAP__NR_TYPES];
19
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020020struct dso;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020021struct ref_reloc_sym;
22struct map_groups;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -030023struct machine;
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020024
25struct map {
26 union {
27 struct rb_node rb_node;
28 struct list_head node;
29 };
30 u64 start;
31 u64 end;
32 enum map_type type;
33 u64 pgoff;
Kirill Smelkov7a2b6202010-02-03 16:52:07 -020034
35 /* ip -> dso rip */
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020036 u64 (*map_ip)(struct map *, u64);
Kirill Smelkov7a2b6202010-02-03 16:52:07 -020037 /* dso rip -> ip */
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020038 u64 (*unmap_ip)(struct map *, u64);
Kirill Smelkov7a2b6202010-02-03 16:52:07 -020039
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020040 struct dso *dso;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080041 struct map_groups *groups;
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020042};
43
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020044struct kmap {
45 struct ref_reloc_sym *ref_reloc_sym;
46 struct map_groups *kmaps;
47};
48
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080049struct map_groups {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -030050 struct rb_root maps[MAP__NR_TYPES];
51 struct list_head removed_maps[MAP__NR_TYPES];
52 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080053};
54
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -030055/* Native host kernel uses -1 as pid index in machine */
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080056#define HOST_KERNEL_ID (-1)
57#define DEFAULT_GUEST_KERNEL_ID (0)
58
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -030059struct machine {
60 struct rb_node rb_node;
61 pid_t pid;
62 char *root_dir;
63 struct list_head user_dsos;
64 struct list_head kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080065 struct map_groups kmaps;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -030066 struct map *vmlinux_maps[MAP__NR_TYPES];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080067};
68
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020069static inline struct kmap *map__kmap(struct map *self)
70{
71 return (struct kmap *)(self + 1);
72}
73
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020074static inline u64 map__map_ip(struct map *map, u64 ip)
75{
76 return ip - map->start + map->pgoff;
77}
78
79static inline u64 map__unmap_ip(struct map *map, u64 ip)
80{
81 return ip + map->start - map->pgoff;
82}
83
84static inline u64 identity__map_ip(struct map *map __used, u64 ip)
85{
86 return ip;
87}
88
Kirill Smelkov7a2b6202010-02-03 16:52:07 -020089
Kirill Smelkovee11b902010-02-07 11:46:15 -020090/* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
Kirill Smelkov7a2b6202010-02-03 16:52:07 -020091u64 map__rip_2objdump(struct map *map, u64 rip);
Kirill Smelkovee11b902010-02-07 11:46:15 -020092u64 map__objdump_2ip(struct map *map, u64 addr);
Kirill Smelkov7a2b6202010-02-03 16:52:07 -020093
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020094struct symbol;
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -020095
96typedef int (*symbol_filter_t)(struct map *map, struct symbol *sym);
97
98void map__init(struct map *self, enum map_type type,
99 u64 start, u64 end, u64 pgoff, struct dso *dso);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800100struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
101 u64 pgoff, u32 pid, char *filename,
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -0300102 enum map_type type, char *cwd, int cwdlen);
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200103void map__delete(struct map *self);
104struct map *map__clone(struct map *self);
105int map__overlap(struct map *l, struct map *r);
106size_t map__fprintf(struct map *self, FILE *fp);
107
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200108int map__load(struct map *self, symbol_filter_t filter);
109struct symbol *map__find_symbol(struct map *self,
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200110 u64 addr, symbol_filter_t filter);
111struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200112 symbol_filter_t filter);
113void map__fixup_start(struct map *self);
114void map__fixup_end(struct map *self);
115
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200116void map__reloc_vmlinux(struct map *self);
117
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300118size_t __map_groups__fprintf_maps(struct map_groups *self,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300119 enum map_type type, int verbose, FILE *fp);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300120void maps__insert(struct rb_root *maps, struct map *map);
121struct map *maps__find(struct rb_root *maps, u64 addr);
122void map_groups__init(struct map_groups *self);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300123int map_groups__clone(struct map_groups *self,
124 struct map_groups *parent, enum map_type type);
125size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp);
126size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300127
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300128typedef void (*machine__process_t)(struct machine *self, void *data);
129
130void machines__process(struct rb_root *self, machine__process_t process, void *data);
131struct machine *machines__add(struct rb_root *self, pid_t pid,
132 const char *root_dir);
133struct machine *machines__find_host(struct rb_root *self);
134struct machine *machines__find(struct rb_root *self, pid_t pid);
135struct machine *machines__findnew(struct rb_root *self, pid_t pid);
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -0300136char *machine__mmap_name(struct machine *self, char *bf, size_t size);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300137int machine__init(struct machine *self, const char *root_dir, pid_t pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800138
139/*
140 * Default guest kernel is defined by parameter --guestkallsyms
141 * and --guestmodules
142 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300143static inline bool machine__is_default_guest(struct machine *self)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800144{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300145 return self ? self->pid == DEFAULT_GUEST_KERNEL_ID : false;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800146}
147
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300148static inline bool machine__is_host(struct machine *self)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800149{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300150 return self ? self->pid == HOST_KERNEL_ID : false;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800151}
152
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300153static inline void map_groups__insert(struct map_groups *self, struct map *map)
154{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800155 maps__insert(&self->maps[map->type], map);
156 map->groups = self;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300157}
158
159static inline struct map *map_groups__find(struct map_groups *self,
160 enum map_type type, u64 addr)
161{
162 return maps__find(&self->maps[type], addr);
163}
164
165struct symbol *map_groups__find_symbol(struct map_groups *self,
166 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300167 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300168 symbol_filter_t filter);
169
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300170struct symbol *map_groups__find_symbol_by_name(struct map_groups *self,
171 enum map_type type,
172 const char *name,
173 struct map **mapp,
174 symbol_filter_t filter);
175
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300176static inline struct symbol *machine__find_function(struct machine *self,
177 u64 addr, struct map **mapp,
178 symbol_filter_t filter)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300179{
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300180 return map_groups__find_symbol(&self->kmaps, MAP__FUNCTION, addr, mapp, filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300181}
182
183static inline
184struct symbol *map_groups__find_function_by_name(struct map_groups *self,
185 const char *name, struct map **mapp,
186 symbol_filter_t filter)
187{
188 return map_groups__find_symbol_by_name(self, MAP__FUNCTION, name, mapp, filter);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300189}
190
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300191int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
192 int verbose, FILE *fp);
193
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300194struct map *map_groups__find_by_name(struct map_groups *self,
195 enum map_type type, const char *name);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -0300196struct map *machine__new_module(struct machine *self, u64 start, const char *filename);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800197
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300198void map_groups__flush(struct map_groups *self);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300199
Arnaldo Carvalho de Melo4a58e612009-12-27 21:37:00 -0200200#endif /* __PERF_MAP_H */