1 #ifndef __PERF_MACHINE_H
2 #define __PERF_MACHINE_H
3
4 #include <sys/types.h>
5 #include <linux/rbtree.h>
6 #include "map.h"
7
8 struct addr_location;
9 struct branch_stack;
10 struct perf_evsel;
11 struct perf_sample;
12 struct symbol;
13 struct thread;
14 union perf_event;
15
16 /* Native host kernel uses -1 as pid index in machine */
17 #define HOST_KERNEL_ID (-1)
18 #define DEFAULT_GUEST_KERNEL_ID (0)
19
20 struct machine {
21 struct rb_node rb_node;
22 pid_t pid;
23 u16 id_hdr_size;
24 char *root_dir;
25 struct rb_root threads;
26 struct list_head dead_threads;
27 struct thread *last_match;
28 struct list_head user_dsos;
29 struct list_head kernel_dsos;
30 struct map_groups kmaps;
31 struct map *vmlinux_maps[MAP__NR_TYPES];
32 symbol_filter_t symbol_filter;
33 };
34
35 static inline
machine__kernel_map(struct machine * machine,enum map_type type)36 struct map *machine__kernel_map(struct machine *machine, enum map_type type)
37 {
38 return machine->vmlinux_maps[type];
39 }
40
41 struct thread *machine__find_thread(struct machine *machine, pid_t tid);
42
43 int machine__process_comm_event(struct machine *machine, union perf_event *event);
44 int machine__process_exit_event(struct machine *machine, union perf_event *event);
45 int machine__process_fork_event(struct machine *machine, union perf_event *event);
46 int machine__process_lost_event(struct machine *machine, union perf_event *event);
47 int machine__process_mmap_event(struct machine *machine, union perf_event *event);
48 int machine__process_mmap2_event(struct machine *machine, union perf_event *event);
49 int machine__process_event(struct machine *machine, union perf_event *event);
50
51 typedef void (*machine__process_t)(struct machine *machine, void *data);
52
53 struct machines {
54 struct machine host;
55 struct rb_root guests;
56 symbol_filter_t symbol_filter;
57 };
58
59 void machines__init(struct machines *machines);
60 void machines__exit(struct machines *machines);
61
62 void machines__process_guests(struct machines *machines,
63 machine__process_t process, void *data);
64
65 struct machine *machines__add(struct machines *machines, pid_t pid,
66 const char *root_dir);
67 struct machine *machines__find_host(struct machines *machines);
68 struct machine *machines__find(struct machines *machines, pid_t pid);
69 struct machine *machines__findnew(struct machines *machines, pid_t pid);
70
71 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size);
72 char *machine__mmap_name(struct machine *machine, char *bf, size_t size);
73
74 void machines__set_symbol_filter(struct machines *machines,
75 symbol_filter_t symbol_filter);
76
77 int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
78 void machine__exit(struct machine *machine);
79 void machine__delete_dead_threads(struct machine *machine);
80 void machine__delete_threads(struct machine *machine);
81 void machine__delete(struct machine *machine);
82
83 struct branch_info *machine__resolve_bstack(struct machine *machine,
84 struct thread *thread,
85 struct branch_stack *bs);
86 struct mem_info *machine__resolve_mem(struct machine *machine,
87 struct thread *thread,
88 struct perf_sample *sample, u8 cpumode);
89 int machine__resolve_callchain(struct machine *machine,
90 struct perf_evsel *evsel,
91 struct thread *thread,
92 struct perf_sample *sample,
93 struct symbol **parent,
94 struct addr_location *root_al);
95
96 /*
97 * Default guest kernel is defined by parameter --guestkallsyms
98 * and --guestmodules
99 */
machine__is_default_guest(struct machine * machine)100 static inline bool machine__is_default_guest(struct machine *machine)
101 {
102 return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false;
103 }
104
machine__is_host(struct machine * machine)105 static inline bool machine__is_host(struct machine *machine)
106 {
107 return machine ? machine->pid == HOST_KERNEL_ID : false;
108 }
109
110 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
111 pid_t tid);
112
113 size_t machine__fprintf(struct machine *machine, FILE *fp);
114
115 static inline
machine__find_kernel_symbol(struct machine * machine,enum map_type type,u64 addr,struct map ** mapp,symbol_filter_t filter)116 struct symbol *machine__find_kernel_symbol(struct machine *machine,
117 enum map_type type, u64 addr,
118 struct map **mapp,
119 symbol_filter_t filter)
120 {
121 return map_groups__find_symbol(&machine->kmaps, type, addr,
122 mapp, filter);
123 }
124
125 static inline
machine__find_kernel_function(struct machine * machine,u64 addr,struct map ** mapp,symbol_filter_t filter)126 struct symbol *machine__find_kernel_function(struct machine *machine, u64 addr,
127 struct map **mapp,
128 symbol_filter_t filter)
129 {
130 return machine__find_kernel_symbol(machine, MAP__FUNCTION, addr,
131 mapp, filter);
132 }
133
134 static inline
machine__find_kernel_function_by_name(struct machine * machine,const char * name,struct map ** mapp,symbol_filter_t filter)135 struct symbol *machine__find_kernel_function_by_name(struct machine *machine,
136 const char *name,
137 struct map **mapp,
138 symbol_filter_t filter)
139 {
140 return map_groups__find_function_by_name(&machine->kmaps, name, mapp,
141 filter);
142 }
143
144 struct map *machine__new_module(struct machine *machine, u64 start,
145 const char *filename);
146
147 int machine__load_kallsyms(struct machine *machine, const char *filename,
148 enum map_type type, symbol_filter_t filter);
149 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
150 symbol_filter_t filter);
151
152 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
153 bool (skip)(struct dso *dso, int parm), int parm);
154 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp);
155 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
156 bool (skip)(struct dso *dso, int parm), int parm);
157
158 void machine__destroy_kernel_maps(struct machine *machine);
159 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
160 int machine__create_kernel_maps(struct machine *machine);
161
162 int machines__create_kernel_maps(struct machines *machines, pid_t pid);
163 int machines__create_guest_kernel_maps(struct machines *machines);
164 void machines__destroy_kernel_maps(struct machines *machines);
165
166 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
167
168 #endif /* __PERF_MACHINE_H */
169