1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_THREAD_H
3 #define __PERF_THREAD_H
4
5 #include <linux/refcount.h>
6 #include <linux/list.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <sys/types.h>
10 #include "srccode.h"
11 #include "symbol_conf.h"
12 #include <strlist.h>
13 #include <intlist.h>
14 #include "rwsem.h"
15 #include "callchain.h"
16 #include <internal/rc_check.h>
17
18 struct addr_location;
19 struct map;
20 struct perf_record_namespaces;
21 struct thread_stack;
22 struct unwind_libunwind_ops;
23
24 struct lbr_stitch {
25 struct list_head lists;
26 struct list_head free_lists;
27 struct perf_sample prev_sample;
28 struct callchain_cursor_node *prev_lbr_cursor;
29 unsigned int prev_lbr_cursor_size;
30 };
31
DECLARE_RC_STRUCT(thread)32 DECLARE_RC_STRUCT(thread) {
33 /** @maps: mmaps associated with this thread. */
34 struct maps *maps;
35 pid_t pid_; /* Not all tools update this */
36 /** @tid: thread ID number unique to a machine. */
37 pid_t tid;
38 /** @ppid: parent process of the process this thread belongs to. */
39 pid_t ppid;
40 int cpu;
41 int guest_cpu; /* For QEMU thread */
42 refcount_t refcnt;
43 /**
44 * @exited: Has the thread had an exit event. Such threads are usually
45 * removed from the machine's threads but some events/tools require
46 * access to dead threads.
47 */
48 bool exited;
49 bool comm_set;
50 int comm_len;
51 struct list_head namespaces_list;
52 struct rw_semaphore namespaces_lock;
53 struct list_head comm_list;
54 struct rw_semaphore comm_lock;
55 u64 db_id;
56
57 void *priv;
58 struct thread_stack *ts;
59 struct nsinfo *nsinfo;
60 struct srccode_state srccode_state;
61 bool filter;
62 int filter_entry_depth;
63 /**
64 * @e_machine: The ELF EM_* associated with the thread. EM_NONE if not
65 * computed.
66 */
67 uint16_t e_machine;
68 /* LBR call stack stitch */
69 bool lbr_stitch_enable;
70 struct lbr_stitch *lbr_stitch;
71 };
72
73 struct machine;
74 struct namespaces;
75 struct comm;
76
77 struct thread *thread__new(pid_t pid, pid_t tid);
78 int thread__init_maps(struct thread *thread, struct machine *machine);
79 void thread__delete(struct thread *thread);
80
81 void thread__set_priv_destructor(void (*destructor)(void *priv));
82
83 struct thread *thread__get(struct thread *thread);
84 void thread__put(struct thread *thread);
85
__thread__zput(struct thread ** thread)86 static inline void __thread__zput(struct thread **thread)
87 {
88 thread__put(*thread);
89 *thread = NULL;
90 }
91
92 #define thread__zput(thread) __thread__zput(&thread)
93
94 struct namespaces *thread__namespaces(struct thread *thread);
95 int thread__set_namespaces(struct thread *thread, u64 timestamp,
96 struct perf_record_namespaces *event);
97
98 int __thread__set_comm(struct thread *thread, const char *comm, u64 timestamp,
99 bool exec);
thread__set_comm(struct thread * thread,const char * comm,u64 timestamp)100 static inline int thread__set_comm(struct thread *thread, const char *comm,
101 u64 timestamp)
102 {
103 return __thread__set_comm(thread, comm, timestamp, false);
104 }
105
106 int thread__set_comm_from_proc(struct thread *thread);
107
108 int thread__comm_len(struct thread *thread);
109 struct comm *thread__comm(struct thread *thread);
110 struct comm *thread__exec_comm(struct thread *thread);
111 const char *thread__comm_str(struct thread *thread);
112 int thread__insert_map(struct thread *thread, struct map *map);
113 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone);
114 size_t thread__fprintf(struct thread *thread, FILE *fp);
115
116 struct thread *thread__main_thread(struct machine *machine, struct thread *thread);
117
118 struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
119 struct addr_location *al);
120 struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
121 struct addr_location *al);
122
123 struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
124 u64 addr, struct addr_location *al);
125 struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
126 u64 addr, struct addr_location *al);
127
128 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
129 bool symbols, struct addr_location *al);
130
131 int thread__memcpy(struct thread *thread, struct machine *machine,
132 void *buf, u64 ip, int len, bool *is64bit);
133
thread__maps(struct thread * thread)134 static inline struct maps *thread__maps(struct thread *thread)
135 {
136 return RC_CHK_ACCESS(thread)->maps;
137 }
138
thread__set_maps(struct thread * thread,struct maps * maps)139 static inline void thread__set_maps(struct thread *thread, struct maps *maps)
140 {
141 RC_CHK_ACCESS(thread)->maps = maps;
142 }
143
thread__pid(const struct thread * thread)144 static inline pid_t thread__pid(const struct thread *thread)
145 {
146 return RC_CHK_ACCESS(thread)->pid_;
147 }
148
thread__set_pid(struct thread * thread,pid_t pid_)149 static inline void thread__set_pid(struct thread *thread, pid_t pid_)
150 {
151 RC_CHK_ACCESS(thread)->pid_ = pid_;
152 }
153
thread__tid(const struct thread * thread)154 static inline pid_t thread__tid(const struct thread *thread)
155 {
156 return RC_CHK_ACCESS(thread)->tid;
157 }
158
thread__set_tid(struct thread * thread,pid_t tid)159 static inline void thread__set_tid(struct thread *thread, pid_t tid)
160 {
161 RC_CHK_ACCESS(thread)->tid = tid;
162 }
163
thread__ppid(const struct thread * thread)164 static inline pid_t thread__ppid(const struct thread *thread)
165 {
166 return RC_CHK_ACCESS(thread)->ppid;
167 }
168
thread__set_ppid(struct thread * thread,pid_t ppid)169 static inline void thread__set_ppid(struct thread *thread, pid_t ppid)
170 {
171 RC_CHK_ACCESS(thread)->ppid = ppid;
172 }
173
thread__cpu(const struct thread * thread)174 static inline int thread__cpu(const struct thread *thread)
175 {
176 return RC_CHK_ACCESS(thread)->cpu;
177 }
178
thread__set_cpu(struct thread * thread,int cpu)179 static inline void thread__set_cpu(struct thread *thread, int cpu)
180 {
181 RC_CHK_ACCESS(thread)->cpu = cpu;
182 }
183
thread__guest_cpu(const struct thread * thread)184 static inline int thread__guest_cpu(const struct thread *thread)
185 {
186 return RC_CHK_ACCESS(thread)->guest_cpu;
187 }
188
thread__set_guest_cpu(struct thread * thread,int guest_cpu)189 static inline void thread__set_guest_cpu(struct thread *thread, int guest_cpu)
190 {
191 RC_CHK_ACCESS(thread)->guest_cpu = guest_cpu;
192 }
193
thread__refcnt(struct thread * thread)194 static inline refcount_t *thread__refcnt(struct thread *thread)
195 {
196 return &RC_CHK_ACCESS(thread)->refcnt;
197 }
198
thread__set_exited(struct thread * thread,bool exited)199 static inline void thread__set_exited(struct thread *thread, bool exited)
200 {
201 RC_CHK_ACCESS(thread)->exited = exited;
202 }
203
thread__comm_set(const struct thread * thread)204 static inline bool thread__comm_set(const struct thread *thread)
205 {
206 return RC_CHK_ACCESS(thread)->comm_set;
207 }
208
thread__set_comm_set(struct thread * thread,bool set)209 static inline void thread__set_comm_set(struct thread *thread, bool set)
210 {
211 RC_CHK_ACCESS(thread)->comm_set = set;
212 }
213
thread__var_comm_len(const struct thread * thread)214 static inline int thread__var_comm_len(const struct thread *thread)
215 {
216 return RC_CHK_ACCESS(thread)->comm_len;
217 }
218
thread__set_comm_len(struct thread * thread,int len)219 static inline void thread__set_comm_len(struct thread *thread, int len)
220 {
221 RC_CHK_ACCESS(thread)->comm_len = len;
222 }
223
thread__namespaces_list(struct thread * thread)224 static inline struct list_head *thread__namespaces_list(struct thread *thread)
225 {
226 return &RC_CHK_ACCESS(thread)->namespaces_list;
227 }
228
thread__namespaces_list_empty(const struct thread * thread)229 static inline int thread__namespaces_list_empty(const struct thread *thread)
230 {
231 return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list);
232 }
233
thread__namespaces_lock(struct thread * thread)234 static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread)
235 {
236 return &RC_CHK_ACCESS(thread)->namespaces_lock;
237 }
238
thread__comm_lock(struct thread * thread)239 static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
240 {
241 return &RC_CHK_ACCESS(thread)->comm_lock;
242 }
243
thread__comm_list(struct thread * thread)244 static inline struct list_head *thread__comm_list(struct thread *thread)
245 SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
246 {
247 return &RC_CHK_ACCESS(thread)->comm_list;
248 }
249
thread__db_id(const struct thread * thread)250 static inline u64 thread__db_id(const struct thread *thread)
251 {
252 return RC_CHK_ACCESS(thread)->db_id;
253 }
254
thread__set_db_id(struct thread * thread,u64 db_id)255 static inline void thread__set_db_id(struct thread *thread, u64 db_id)
256 {
257 RC_CHK_ACCESS(thread)->db_id = db_id;
258 }
259
thread__priv(struct thread * thread)260 static inline void *thread__priv(struct thread *thread)
261 {
262 return RC_CHK_ACCESS(thread)->priv;
263 }
264
thread__set_priv(struct thread * thread,void * p)265 static inline void thread__set_priv(struct thread *thread, void *p)
266 {
267 RC_CHK_ACCESS(thread)->priv = p;
268 }
269
thread__ts(struct thread * thread)270 static inline struct thread_stack *thread__ts(struct thread *thread)
271 {
272 return RC_CHK_ACCESS(thread)->ts;
273 }
274
thread__set_ts(struct thread * thread,struct thread_stack * ts)275 static inline void thread__set_ts(struct thread *thread, struct thread_stack *ts)
276 {
277 RC_CHK_ACCESS(thread)->ts = ts;
278 }
279
thread__nsinfo(struct thread * thread)280 static inline struct nsinfo *thread__nsinfo(struct thread *thread)
281 {
282 return RC_CHK_ACCESS(thread)->nsinfo;
283 }
284
thread__srccode_state(struct thread * thread)285 static inline struct srccode_state *thread__srccode_state(struct thread *thread)
286 {
287 return &RC_CHK_ACCESS(thread)->srccode_state;
288 }
289
thread__filter(const struct thread * thread)290 static inline bool thread__filter(const struct thread *thread)
291 {
292 return RC_CHK_ACCESS(thread)->filter;
293 }
294
thread__set_filter(struct thread * thread,bool filter)295 static inline void thread__set_filter(struct thread *thread, bool filter)
296 {
297 RC_CHK_ACCESS(thread)->filter = filter;
298 }
299
thread__filter_entry_depth(const struct thread * thread)300 static inline int thread__filter_entry_depth(const struct thread *thread)
301 {
302 return RC_CHK_ACCESS(thread)->filter_entry_depth;
303 }
304
thread__set_filter_entry_depth(struct thread * thread,int depth)305 static inline void thread__set_filter_entry_depth(struct thread *thread, int depth)
306 {
307 RC_CHK_ACCESS(thread)->filter_entry_depth = depth;
308 }
309
310 uint16_t thread__e_machine(struct thread *thread, struct machine *machine);
311
thread__set_e_machine(struct thread * thread,uint16_t e_machine)312 static inline void thread__set_e_machine(struct thread *thread, uint16_t e_machine)
313 {
314 RC_CHK_ACCESS(thread)->e_machine = e_machine;
315 }
316
317
thread__lbr_stitch_enable(const struct thread * thread)318 static inline bool thread__lbr_stitch_enable(const struct thread *thread)
319 {
320 return RC_CHK_ACCESS(thread)->lbr_stitch_enable;
321 }
322
thread__set_lbr_stitch_enable(struct thread * thread,bool en)323 static inline void thread__set_lbr_stitch_enable(struct thread *thread, bool en)
324 {
325 RC_CHK_ACCESS(thread)->lbr_stitch_enable = en;
326 }
327
thread__lbr_stitch(struct thread * thread)328 static inline struct lbr_stitch *thread__lbr_stitch(struct thread *thread)
329 {
330 return RC_CHK_ACCESS(thread)->lbr_stitch;
331 }
332
thread__set_lbr_stitch(struct thread * thread,struct lbr_stitch * lbrs)333 static inline void thread__set_lbr_stitch(struct thread *thread, struct lbr_stitch *lbrs)
334 {
335 RC_CHK_ACCESS(thread)->lbr_stitch = lbrs;
336 }
337
thread__is_filtered(struct thread * thread)338 static inline bool thread__is_filtered(struct thread *thread)
339 {
340 if (symbol_conf.comm_list &&
341 !strlist__has_entry(symbol_conf.comm_list, thread__comm_str(thread))) {
342 return true;
343 }
344
345 if (symbol_conf.pid_list &&
346 !intlist__has_entry(symbol_conf.pid_list, thread__pid(thread))) {
347 return true;
348 }
349
350 if (symbol_conf.tid_list &&
351 !intlist__has_entry(symbol_conf.tid_list, thread__tid(thread))) {
352 return true;
353 }
354
355 return false;
356 }
357
358 void thread__free_stitch_list(struct thread *thread);
359
360 void thread__resolve(struct thread *thread, struct addr_location *al,
361 struct perf_sample *sample);
362
363 #endif /* __PERF_THREAD_H */
364