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