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_flags: The ELF EF_* associated with the thread. Valid if e_machine != EM_NONE.
65 */
66 uint16_t e_flags;
67 /**
68 * @e_machine: The ELF EM_* associated with the thread. EM_NONE if not
69 * computed.
70 */
71 uint16_t e_machine;
72 /* LBR call stack stitch */
73 bool lbr_stitch_enable;
74 struct lbr_stitch *lbr_stitch;
75 };
76
77 struct machine;
78 struct namespaces;
79 struct comm;
80
81 struct thread *thread__new(pid_t pid, pid_t tid);
82 int thread__init_maps(struct thread *thread, struct machine *machine);
83 void thread__delete(struct thread *thread);
84
85 void thread__set_priv_destructor(void (*destructor)(void *priv));
86
87 struct thread *thread__get(struct thread *thread);
88 void thread__put(struct thread *thread);
89
__thread__zput(struct thread ** thread)90 static inline void __thread__zput(struct thread **thread)
91 {
92 thread__put(*thread);
93 *thread = NULL;
94 }
95
96 #define thread__zput(thread) __thread__zput(&thread)
97
98 struct namespaces *thread__namespaces(struct thread *thread);
99 int thread__set_namespaces(struct thread *thread, u64 timestamp,
100 struct perf_record_namespaces *event);
101
102 int __thread__set_comm(struct thread *thread, const char *comm, u64 timestamp,
103 bool exec);
thread__set_comm(struct thread * thread,const char * comm,u64 timestamp)104 static inline int thread__set_comm(struct thread *thread, const char *comm,
105 u64 timestamp)
106 {
107 return __thread__set_comm(thread, comm, timestamp, false);
108 }
109
110 int thread__set_comm_from_proc(struct thread *thread);
111
112 int thread__comm_len(struct thread *thread);
113 struct comm *thread__comm(struct thread *thread);
114 struct comm *thread__exec_comm(struct thread *thread);
115 const char *thread__comm_str(struct thread *thread);
116 int thread__insert_map(struct thread *thread, struct map *map);
117 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone);
118 size_t thread__fprintf(struct thread *thread, FILE *fp);
119
120 struct thread *thread__main_thread(struct machine *machine, struct thread *thread);
121
122 struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
123 struct addr_location *al);
124 struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
125 struct addr_location *al);
126
127 struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
128 u64 addr, struct addr_location *al);
129 struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
130 u64 addr, struct addr_location *al);
131
132 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
133 bool symbols, struct addr_location *al);
134
135 int thread__memcpy(struct thread *thread, struct machine *machine,
136 void *buf, u64 ip, int len, bool *is64bit);
137
thread__maps(struct thread * thread)138 static inline struct maps *thread__maps(struct thread *thread)
139 {
140 return RC_CHK_ACCESS(thread)->maps;
141 }
142
thread__set_maps(struct thread * thread,struct maps * maps)143 static inline void thread__set_maps(struct thread *thread, struct maps *maps)
144 {
145 RC_CHK_ACCESS(thread)->maps = maps;
146 }
147
thread__pid(const struct thread * thread)148 static inline pid_t thread__pid(const struct thread *thread)
149 {
150 return RC_CHK_ACCESS(thread)->pid_;
151 }
152
thread__set_pid(struct thread * thread,pid_t pid_)153 static inline void thread__set_pid(struct thread *thread, pid_t pid_)
154 {
155 RC_CHK_ACCESS(thread)->pid_ = pid_;
156 }
157
thread__tid(const struct thread * thread)158 static inline pid_t thread__tid(const struct thread *thread)
159 {
160 return RC_CHK_ACCESS(thread)->tid;
161 }
162
thread__set_tid(struct thread * thread,pid_t tid)163 static inline void thread__set_tid(struct thread *thread, pid_t tid)
164 {
165 RC_CHK_ACCESS(thread)->tid = tid;
166 }
167
thread__ppid(const struct thread * thread)168 static inline pid_t thread__ppid(const struct thread *thread)
169 {
170 return RC_CHK_ACCESS(thread)->ppid;
171 }
172
thread__set_ppid(struct thread * thread,pid_t ppid)173 static inline void thread__set_ppid(struct thread *thread, pid_t ppid)
174 {
175 RC_CHK_ACCESS(thread)->ppid = ppid;
176 }
177
thread__cpu(const struct thread * thread)178 static inline int thread__cpu(const struct thread *thread)
179 {
180 return RC_CHK_ACCESS(thread)->cpu;
181 }
182
thread__set_cpu(struct thread * thread,int cpu)183 static inline void thread__set_cpu(struct thread *thread, int cpu)
184 {
185 RC_CHK_ACCESS(thread)->cpu = cpu;
186 }
187
thread__guest_cpu(const struct thread * thread)188 static inline int thread__guest_cpu(const struct thread *thread)
189 {
190 return RC_CHK_ACCESS(thread)->guest_cpu;
191 }
192
thread__set_guest_cpu(struct thread * thread,int guest_cpu)193 static inline void thread__set_guest_cpu(struct thread *thread, int guest_cpu)
194 {
195 RC_CHK_ACCESS(thread)->guest_cpu = guest_cpu;
196 }
197
thread__refcnt(struct thread * thread)198 static inline refcount_t *thread__refcnt(struct thread *thread)
199 {
200 return &RC_CHK_ACCESS(thread)->refcnt;
201 }
202
thread__set_exited(struct thread * thread,bool exited)203 static inline void thread__set_exited(struct thread *thread, bool exited)
204 {
205 RC_CHK_ACCESS(thread)->exited = exited;
206 }
207
thread__comm_set(const struct thread * thread)208 static inline bool thread__comm_set(const struct thread *thread)
209 {
210 return RC_CHK_ACCESS(thread)->comm_set;
211 }
212
thread__set_comm_set(struct thread * thread,bool set)213 static inline void thread__set_comm_set(struct thread *thread, bool set)
214 {
215 RC_CHK_ACCESS(thread)->comm_set = set;
216 }
217
thread__var_comm_len(const struct thread * thread)218 static inline int thread__var_comm_len(const struct thread *thread)
219 {
220 return RC_CHK_ACCESS(thread)->comm_len;
221 }
222
thread__set_comm_len(struct thread * thread,int len)223 static inline void thread__set_comm_len(struct thread *thread, int len)
224 {
225 RC_CHK_ACCESS(thread)->comm_len = len;
226 }
227
thread__namespaces_list(struct thread * thread)228 static inline struct list_head *thread__namespaces_list(struct thread *thread)
229 {
230 return &RC_CHK_ACCESS(thread)->namespaces_list;
231 }
232
thread__namespaces_list_empty(const struct thread * thread)233 static inline int thread__namespaces_list_empty(const struct thread *thread)
234 {
235 return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list);
236 }
237
thread__namespaces_lock(struct thread * thread)238 static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread)
239 {
240 return &RC_CHK_ACCESS(thread)->namespaces_lock;
241 }
242
thread__comm_lock(struct thread * thread)243 static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
244 {
245 return &RC_CHK_ACCESS(thread)->comm_lock;
246 }
247
thread__comm_list(struct thread * thread)248 static inline struct list_head *thread__comm_list(struct thread *thread)
249 SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
250 {
251 return &RC_CHK_ACCESS(thread)->comm_list;
252 }
253
thread__db_id(const struct thread * thread)254 static inline u64 thread__db_id(const struct thread *thread)
255 {
256 return RC_CHK_ACCESS(thread)->db_id;
257 }
258
thread__set_db_id(struct thread * thread,u64 db_id)259 static inline void thread__set_db_id(struct thread *thread, u64 db_id)
260 {
261 RC_CHK_ACCESS(thread)->db_id = db_id;
262 }
263
thread__priv(struct thread * thread)264 static inline void *thread__priv(struct thread *thread)
265 {
266 return RC_CHK_ACCESS(thread)->priv;
267 }
268
thread__set_priv(struct thread * thread,void * p)269 static inline void thread__set_priv(struct thread *thread, void *p)
270 {
271 RC_CHK_ACCESS(thread)->priv = p;
272 }
273
thread__ts(struct thread * thread)274 static inline struct thread_stack *thread__ts(struct thread *thread)
275 {
276 return RC_CHK_ACCESS(thread)->ts;
277 }
278
thread__set_ts(struct thread * thread,struct thread_stack * ts)279 static inline void thread__set_ts(struct thread *thread, struct thread_stack *ts)
280 {
281 RC_CHK_ACCESS(thread)->ts = ts;
282 }
283
thread__nsinfo(struct thread * thread)284 static inline struct nsinfo *thread__nsinfo(struct thread *thread)
285 {
286 return RC_CHK_ACCESS(thread)->nsinfo;
287 }
288
thread__srccode_state(struct thread * thread)289 static inline struct srccode_state *thread__srccode_state(struct thread *thread)
290 {
291 return &RC_CHK_ACCESS(thread)->srccode_state;
292 }
293
thread__filter(const struct thread * thread)294 static inline bool thread__filter(const struct thread *thread)
295 {
296 return RC_CHK_ACCESS(thread)->filter;
297 }
298
thread__set_filter(struct thread * thread,bool filter)299 static inline void thread__set_filter(struct thread *thread, bool filter)
300 {
301 RC_CHK_ACCESS(thread)->filter = filter;
302 }
303
thread__filter_entry_depth(const struct thread * thread)304 static inline int thread__filter_entry_depth(const struct thread *thread)
305 {
306 return RC_CHK_ACCESS(thread)->filter_entry_depth;
307 }
308
thread__set_filter_entry_depth(struct thread * thread,int depth)309 static inline void thread__set_filter_entry_depth(struct thread *thread, int depth)
310 {
311 RC_CHK_ACCESS(thread)->filter_entry_depth = depth;
312 }
313
314 uint16_t thread__e_machine(struct thread *thread, struct machine *machine, uint32_t *e_flags);
315
thread__set_e_machine(struct thread * thread,uint16_t e_machine)316 static inline void thread__set_e_machine(struct thread *thread, uint16_t e_machine)
317 {
318 RC_CHK_ACCESS(thread)->e_machine = e_machine;
319 }
320
thread__e_flags(const struct thread * thread)321 static inline uint32_t thread__e_flags(const struct thread *thread)
322 {
323 return RC_CHK_ACCESS(thread)->e_flags;
324 }
325
thread__set_e_flags(struct thread * thread,uint32_t e_flags)326 static inline void thread__set_e_flags(struct thread *thread, uint32_t e_flags)
327 {
328 RC_CHK_ACCESS(thread)->e_flags = e_flags;
329 }
330
331
thread__lbr_stitch_enable(const struct thread * thread)332 static inline bool thread__lbr_stitch_enable(const struct thread *thread)
333 {
334 return RC_CHK_ACCESS(thread)->lbr_stitch_enable;
335 }
336
thread__set_lbr_stitch_enable(struct thread * thread,bool en)337 static inline void thread__set_lbr_stitch_enable(struct thread *thread, bool en)
338 {
339 RC_CHK_ACCESS(thread)->lbr_stitch_enable = en;
340 }
341
thread__lbr_stitch(struct thread * thread)342 static inline struct lbr_stitch *thread__lbr_stitch(struct thread *thread)
343 {
344 return RC_CHK_ACCESS(thread)->lbr_stitch;
345 }
346
thread__set_lbr_stitch(struct thread * thread,struct lbr_stitch * lbrs)347 static inline void thread__set_lbr_stitch(struct thread *thread, struct lbr_stitch *lbrs)
348 {
349 RC_CHK_ACCESS(thread)->lbr_stitch = lbrs;
350 }
351
thread__is_filtered(struct thread * thread)352 static inline bool thread__is_filtered(struct thread *thread)
353 {
354 if (symbol_conf.comm_list &&
355 !strlist__has_entry(symbol_conf.comm_list, thread__comm_str(thread))) {
356 return true;
357 }
358
359 if (symbol_conf.pid_list &&
360 !intlist__has_entry(symbol_conf.pid_list, thread__pid(thread))) {
361 return true;
362 }
363
364 if (symbol_conf.tid_list &&
365 !intlist__has_entry(symbol_conf.tid_list, thread__tid(thread))) {
366 return true;
367 }
368
369 return false;
370 }
371
372 void thread__free_stitch_list(struct thread *thread);
373
374 void thread__resolve(struct thread *thread, struct addr_location *al,
375 struct perf_sample *sample);
376
377 #endif /* __PERF_THREAD_H */
378