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
8struct addr_location;
9struct branch_stack;
10struct perf_evsel;
11struct perf_sample;
12struct symbol;
13struct thread;
14union 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
20struct 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
35static inline
36struct map *machine__kernel_map(struct machine *machine, enum map_type type)
37{
38	return machine->vmlinux_maps[type];
39}
40
41struct thread *machine__find_thread(struct machine *machine, pid_t tid);
42
43int machine__process_comm_event(struct machine *machine, union perf_event *event);
44int machine__process_exit_event(struct machine *machine, union perf_event *event);
45int machine__process_fork_event(struct machine *machine, union perf_event *event);
46int machine__process_lost_event(struct machine *machine, union perf_event *event);
47int machine__process_mmap_event(struct machine *machine, union perf_event *event);
48int machine__process_mmap2_event(struct machine *machine, union perf_event *event);
49int machine__process_event(struct machine *machine, union perf_event *event);
50
51typedef void (*machine__process_t)(struct machine *machine, void *data);
52
53struct machines {
54	struct machine host;
55	struct rb_root guests;
56	symbol_filter_t symbol_filter;
57};
58
59void machines__init(struct machines *machines);
60void machines__exit(struct machines *machines);
61
62void machines__process_guests(struct machines *machines,
63			      machine__process_t process, void *data);
64
65struct machine *machines__add(struct machines *machines, pid_t pid,
66			      const char *root_dir);
67struct machine *machines__find_host(struct machines *machines);
68struct machine *machines__find(struct machines *machines, pid_t pid);
69struct machine *machines__findnew(struct machines *machines, pid_t pid);
70
71void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size);
72char *machine__mmap_name(struct machine *machine, char *bf, size_t size);
73
74void machines__set_symbol_filter(struct machines *machines,
75				 symbol_filter_t symbol_filter);
76
77int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
78void machine__exit(struct machine *machine);
79void machine__delete_dead_threads(struct machine *machine);
80void machine__delete_threads(struct machine *machine);
81void machine__delete(struct machine *machine);
82
83struct branch_info *machine__resolve_bstack(struct machine *machine,
84					    struct thread *thread,
85					    struct branch_stack *bs);
86struct mem_info *machine__resolve_mem(struct machine *machine,
87				      struct thread *thread,
88				      struct perf_sample *sample, u8 cpumode);
89int 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 */
100static inline bool machine__is_default_guest(struct machine *machine)
101{
102	return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false;
103}
104
105static inline bool machine__is_host(struct machine *machine)
106{
107	return machine ? machine->pid == HOST_KERNEL_ID : false;
108}
109
110struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
111				       pid_t tid);
112
113size_t machine__fprintf(struct machine *machine, FILE *fp);
114
115static inline
116struct 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
125static inline
126struct 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
134static inline
135struct 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
144struct map *machine__new_module(struct machine *machine, u64 start,
145				const char *filename);
146
147int machine__load_kallsyms(struct machine *machine, const char *filename,
148			   enum map_type type, symbol_filter_t filter);
149int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
150			       symbol_filter_t filter);
151
152size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
153				     bool (skip)(struct dso *dso, int parm), int parm);
154size_t machines__fprintf_dsos(struct machines *machines, FILE *fp);
155size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
156				     bool (skip)(struct dso *dso, int parm), int parm);
157
158void machine__destroy_kernel_maps(struct machine *machine);
159int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
160int machine__create_kernel_maps(struct machine *machine);
161
162int machines__create_kernel_maps(struct machines *machines, pid_t pid);
163int machines__create_guest_kernel_maps(struct machines *machines);
164void machines__destroy_kernel_maps(struct machines *machines);
165
166size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
167
168#endif /* __PERF_MACHINE_H */
169