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