• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dso.h"
8 #include "event.h"
9 
10 struct addr_location;
11 struct branch_stack;
12 struct perf_evsel;
13 struct perf_sample;
14 struct symbol;
15 struct thread;
16 union perf_event;
17 
18 /* Native host kernel uses -1 as pid index in machine */
19 #define	HOST_KERNEL_ID			(-1)
20 #define	DEFAULT_GUEST_KERNEL_ID		(0)
21 
22 extern const char *ref_reloc_sym_names[];
23 
24 struct vdso_info;
25 
26 struct machine {
27 	struct rb_node	  rb_node;
28 	pid_t		  pid;
29 	u16		  id_hdr_size;
30 	bool		  comm_exec;
31 	char		  *root_dir;
32 	struct rb_root	  threads;
33 	struct list_head  dead_threads;
34 	struct thread	  *last_match;
35 	struct vdso_info  *vdso_info;
36 	struct dsos	  user_dsos;
37 	struct dsos	  kernel_dsos;
38 	struct map_groups kmaps;
39 	struct map	  *vmlinux_maps[MAP__NR_TYPES];
40 	u64		  kernel_start;
41 	symbol_filter_t	  symbol_filter;
42 	pid_t		  *current_tid;
43 };
44 
45 static inline
machine__kernel_map(struct machine * machine,enum map_type type)46 struct map *machine__kernel_map(struct machine *machine, enum map_type type)
47 {
48 	return machine->vmlinux_maps[type];
49 }
50 
51 int machine__get_kernel_start(struct machine *machine);
52 
machine__kernel_start(struct machine * machine)53 static inline u64 machine__kernel_start(struct machine *machine)
54 {
55 	if (!machine->kernel_start)
56 		machine__get_kernel_start(machine);
57 	return machine->kernel_start;
58 }
59 
machine__kernel_ip(struct machine * machine,u64 ip)60 static inline bool machine__kernel_ip(struct machine *machine, u64 ip)
61 {
62 	u64 kernel_start = machine__kernel_start(machine);
63 
64 	return ip >= kernel_start;
65 }
66 
67 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
68 				    pid_t tid);
69 struct comm *machine__thread_exec_comm(struct machine *machine,
70 				       struct thread *thread);
71 
72 int machine__process_comm_event(struct machine *machine, union perf_event *event,
73 				struct perf_sample *sample);
74 int machine__process_exit_event(struct machine *machine, union perf_event *event,
75 				struct perf_sample *sample);
76 int machine__process_fork_event(struct machine *machine, union perf_event *event,
77 				struct perf_sample *sample);
78 int machine__process_lost_event(struct machine *machine, union perf_event *event,
79 				struct perf_sample *sample);
80 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
81 				struct perf_sample *sample);
82 int machine__process_mmap2_event(struct machine *machine, union perf_event *event,
83 				 struct perf_sample *sample);
84 int machine__process_event(struct machine *machine, union perf_event *event,
85 				struct perf_sample *sample);
86 
87 typedef void (*machine__process_t)(struct machine *machine, void *data);
88 
89 struct machines {
90 	struct machine host;
91 	struct rb_root guests;
92 	symbol_filter_t symbol_filter;
93 };
94 
95 void machines__init(struct machines *machines);
96 void machines__exit(struct machines *machines);
97 
98 void machines__process_guests(struct machines *machines,
99 			      machine__process_t process, void *data);
100 
101 struct machine *machines__add(struct machines *machines, pid_t pid,
102 			      const char *root_dir);
103 struct machine *machines__find_host(struct machines *machines);
104 struct machine *machines__find(struct machines *machines, pid_t pid);
105 struct machine *machines__findnew(struct machines *machines, pid_t pid);
106 
107 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size);
108 char *machine__mmap_name(struct machine *machine, char *bf, size_t size);
109 
110 void machines__set_symbol_filter(struct machines *machines,
111 				 symbol_filter_t symbol_filter);
112 void machines__set_comm_exec(struct machines *machines, bool comm_exec);
113 
114 struct machine *machine__new_host(void);
115 int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
116 void machine__exit(struct machine *machine);
117 void machine__delete_dead_threads(struct machine *machine);
118 void machine__delete_threads(struct machine *machine);
119 void machine__delete(struct machine *machine);
120 
121 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
122 					   struct addr_location *al);
123 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
124 				     struct addr_location *al);
125 int machine__resolve_callchain(struct machine *machine,
126 			       struct perf_evsel *evsel,
127 			       struct thread *thread,
128 			       struct perf_sample *sample,
129 			       struct symbol **parent,
130 			       struct addr_location *root_al,
131 			       int max_stack);
132 
133 /*
134  * Default guest kernel is defined by parameter --guestkallsyms
135  * and --guestmodules
136  */
machine__is_default_guest(struct machine * machine)137 static inline bool machine__is_default_guest(struct machine *machine)
138 {
139 	return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false;
140 }
141 
machine__is_host(struct machine * machine)142 static inline bool machine__is_host(struct machine *machine)
143 {
144 	return machine ? machine->pid == HOST_KERNEL_ID : false;
145 }
146 
147 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
148 				       pid_t tid);
149 
150 size_t machine__fprintf(struct machine *machine, FILE *fp);
151 
152 static inline
machine__find_kernel_symbol(struct machine * machine,enum map_type type,u64 addr,struct map ** mapp,symbol_filter_t filter)153 struct symbol *machine__find_kernel_symbol(struct machine *machine,
154 					   enum map_type type, u64 addr,
155 					   struct map **mapp,
156 					   symbol_filter_t filter)
157 {
158 	return map_groups__find_symbol(&machine->kmaps, type, addr,
159 				       mapp, filter);
160 }
161 
162 static inline
machine__find_kernel_function(struct machine * machine,u64 addr,struct map ** mapp,symbol_filter_t filter)163 struct symbol *machine__find_kernel_function(struct machine *machine, u64 addr,
164 					     struct map **mapp,
165 					     symbol_filter_t filter)
166 {
167 	return machine__find_kernel_symbol(machine, MAP__FUNCTION, addr,
168 					   mapp, filter);
169 }
170 
171 static inline
machine__find_kernel_function_by_name(struct machine * machine,const char * name,struct map ** mapp,symbol_filter_t filter)172 struct symbol *machine__find_kernel_function_by_name(struct machine *machine,
173 						     const char *name,
174 						     struct map **mapp,
175 						     symbol_filter_t filter)
176 {
177 	return map_groups__find_function_by_name(&machine->kmaps, name, mapp,
178 						 filter);
179 }
180 
181 struct map *machine__new_module(struct machine *machine, u64 start,
182 				const char *filename);
183 
184 int machine__load_kallsyms(struct machine *machine, const char *filename,
185 			   enum map_type type, symbol_filter_t filter);
186 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
187 			       symbol_filter_t filter);
188 
189 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
190 				     bool (skip)(struct dso *dso, int parm), int parm);
191 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp);
192 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
193 				     bool (skip)(struct dso *dso, int parm), int parm);
194 
195 void machine__destroy_kernel_maps(struct machine *machine);
196 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
197 int machine__create_kernel_maps(struct machine *machine);
198 
199 int machines__create_kernel_maps(struct machines *machines, pid_t pid);
200 int machines__create_guest_kernel_maps(struct machines *machines);
201 void machines__destroy_kernel_maps(struct machines *machines);
202 
203 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
204 
205 int machine__for_each_thread(struct machine *machine,
206 			     int (*fn)(struct thread *thread, void *p),
207 			     void *priv);
208 
209 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
210 				  struct target *target, struct thread_map *threads,
211 				  perf_event__handler_t process, bool data_mmap);
212 static inline
machine__synthesize_threads(struct machine * machine,struct target * target,struct thread_map * threads,bool data_mmap)213 int machine__synthesize_threads(struct machine *machine, struct target *target,
214 				struct thread_map *threads, bool data_mmap)
215 {
216 	return __machine__synthesize_threads(machine, NULL, target, threads,
217 					     perf_event__process, data_mmap);
218 }
219 
220 pid_t machine__get_current_tid(struct machine *machine, int cpu);
221 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
222 			     pid_t tid);
223 
224 #endif /* __PERF_MACHINE_H */
225