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