blob: 4c1c2da704b227e19db64a7eea7730af20a0ea50 [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);
136char *machine__mmap_name(struct machine *self, char *buff);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800137
138/*
139 * Default guest kernel is defined by parameter --guestkallsyms
140 * and --guestmodules
141 */
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300142static inline bool machine__is_default_guest(struct machine *self)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800143{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300144 return self ? self->pid == DEFAULT_GUEST_KERNEL_ID : false;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800145}
146
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300147static inline bool machine__is_host(struct machine *self)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800148{
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300149 return self ? self->pid == HOST_KERNEL_ID : false;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800150}
151
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300152static inline void map_groups__insert(struct map_groups *self, struct map *map)
153{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800154 maps__insert(&self->maps[map->type], map);
155 map->groups = self;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300156}
157
158static inline struct map *map_groups__find(struct map_groups *self,
159 enum map_type type, u64 addr)
160{
161 return maps__find(&self->maps[type], addr);
162}
163
164struct symbol *map_groups__find_symbol(struct map_groups *self,
165 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300166 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300167 symbol_filter_t filter);
168
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300169struct symbol *map_groups__find_symbol_by_name(struct map_groups *self,
170 enum map_type type,
171 const char *name,
172 struct map **mapp,
173 symbol_filter_t filter);
174
175static inline
176struct symbol *map_groups__find_function(struct map_groups *self, u64 addr,
177 struct map **mapp, symbol_filter_t filter)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300178{
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300179 return map_groups__find_symbol(self, MAP__FUNCTION, addr, mapp, filter);
180}
181
182static inline
183struct symbol *map_groups__find_function_by_name(struct map_groups *self,
184 const char *name, struct map **mapp,
185 symbol_filter_t filter)
186{
187 return map_groups__find_symbol_by_name(self, MAP__FUNCTION, name, mapp, filter);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300188}
189
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300190int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
191 int verbose, FILE *fp);
192
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300193struct map *map_groups__find_by_name(struct map_groups *self,
194 enum map_type type, const char *name);
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300195struct map *map_groups__new_module(struct map_groups *self, u64 start,
196 const char *filename, struct machine *machine);
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 */