1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_THREAD_H
3 #define __PERF_THREAD_H
5 #include <linux/refcount.h>
6 #include <linux/list.h>
11 #include "symbol_conf.h"
15 #include "callchain.h"
16 #include <internal/rc_check.h>
20 struct perf_record_namespaces;
22 struct unwind_libunwind_ops;
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;
31 DECLARE_RC_STRUCT(thread) {
32 /** @maps: mmaps associated with this thread. */
34 pid_t pid_; /* Not all tools update this */
35 /** @tid: thread ID number unique to a machine. */
37 /** @ppid: parent process of the process this thread belongs to. */
40 int guest_cpu; /* For QEMU thread */
43 * @exited: Has the thread had an exit event. Such threads are usually
44 * removed from the machine's threads but some events/tools require
45 * access to dead threads.
50 struct list_head namespaces_list;
51 struct rw_semaphore namespaces_lock;
52 struct list_head comm_list;
53 struct rw_semaphore comm_lock;
57 struct thread_stack *ts;
58 struct nsinfo *nsinfo;
59 struct srccode_state srccode_state;
61 int filter_entry_depth;
63 /* LBR call stack stitch */
64 bool lbr_stitch_enable;
65 struct lbr_stitch *lbr_stitch;
72 struct thread *thread__new(pid_t pid, pid_t tid);
73 int thread__init_maps(struct thread *thread, struct machine *machine);
74 void thread__delete(struct thread *thread);
76 void thread__set_priv_destructor(void (*destructor)(void *priv));
78 struct thread *thread__get(struct thread *thread);
79 void thread__put(struct thread *thread);
81 static inline void __thread__zput(struct thread **thread)
87 #define thread__zput(thread) __thread__zput(&thread)
89 struct namespaces *thread__namespaces(struct thread *thread);
90 int thread__set_namespaces(struct thread *thread, u64 timestamp,
91 struct perf_record_namespaces *event);
93 int __thread__set_comm(struct thread *thread, const char *comm, u64 timestamp,
95 static inline int thread__set_comm(struct thread *thread, const char *comm,
98 return __thread__set_comm(thread, comm, timestamp, false);
101 int thread__set_comm_from_proc(struct thread *thread);
103 int thread__comm_len(struct thread *thread);
104 struct comm *thread__comm(struct thread *thread);
105 struct comm *thread__exec_comm(struct thread *thread);
106 const char *thread__comm_str(struct thread *thread);
107 int thread__insert_map(struct thread *thread, struct map *map);
108 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone);
109 size_t thread__fprintf(struct thread *thread, FILE *fp);
111 struct thread *thread__main_thread(struct machine *machine, struct thread *thread);
113 struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
114 struct addr_location *al);
115 struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
116 struct addr_location *al);
118 struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
119 u64 addr, struct addr_location *al);
120 struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
121 u64 addr, struct addr_location *al);
123 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
124 struct addr_location *al);
126 int thread__memcpy(struct thread *thread, struct machine *machine,
127 void *buf, u64 ip, int len, bool *is64bit);
129 static inline struct maps *thread__maps(struct thread *thread)
131 return RC_CHK_ACCESS(thread)->maps;
134 static inline void thread__set_maps(struct thread *thread, struct maps *maps)
136 RC_CHK_ACCESS(thread)->maps = maps;
139 static inline pid_t thread__pid(const struct thread *thread)
141 return RC_CHK_ACCESS(thread)->pid_;
144 static inline void thread__set_pid(struct thread *thread, pid_t pid_)
146 RC_CHK_ACCESS(thread)->pid_ = pid_;
149 static inline pid_t thread__tid(const struct thread *thread)
151 return RC_CHK_ACCESS(thread)->tid;
154 static inline void thread__set_tid(struct thread *thread, pid_t tid)
156 RC_CHK_ACCESS(thread)->tid = tid;
159 static inline pid_t thread__ppid(const struct thread *thread)
161 return RC_CHK_ACCESS(thread)->ppid;
164 static inline void thread__set_ppid(struct thread *thread, pid_t ppid)
166 RC_CHK_ACCESS(thread)->ppid = ppid;
169 static inline int thread__cpu(const struct thread *thread)
171 return RC_CHK_ACCESS(thread)->cpu;
174 static inline void thread__set_cpu(struct thread *thread, int cpu)
176 RC_CHK_ACCESS(thread)->cpu = cpu;
179 static inline int thread__guest_cpu(const struct thread *thread)
181 return RC_CHK_ACCESS(thread)->guest_cpu;
184 static inline void thread__set_guest_cpu(struct thread *thread, int guest_cpu)
186 RC_CHK_ACCESS(thread)->guest_cpu = guest_cpu;
189 static inline refcount_t *thread__refcnt(struct thread *thread)
191 return &RC_CHK_ACCESS(thread)->refcnt;
194 static inline void thread__set_exited(struct thread *thread, bool exited)
196 RC_CHK_ACCESS(thread)->exited = exited;
199 static inline bool thread__comm_set(const struct thread *thread)
201 return RC_CHK_ACCESS(thread)->comm_set;
204 static inline void thread__set_comm_set(struct thread *thread, bool set)
206 RC_CHK_ACCESS(thread)->comm_set = set;
209 static inline int thread__var_comm_len(const struct thread *thread)
211 return RC_CHK_ACCESS(thread)->comm_len;
214 static inline void thread__set_comm_len(struct thread *thread, int len)
216 RC_CHK_ACCESS(thread)->comm_len = len;
219 static inline struct list_head *thread__namespaces_list(struct thread *thread)
221 return &RC_CHK_ACCESS(thread)->namespaces_list;
224 static inline int thread__namespaces_list_empty(const struct thread *thread)
226 return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list);
229 static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread)
231 return &RC_CHK_ACCESS(thread)->namespaces_lock;
234 static inline struct list_head *thread__comm_list(struct thread *thread)
236 return &RC_CHK_ACCESS(thread)->comm_list;
239 static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
241 return &RC_CHK_ACCESS(thread)->comm_lock;
244 static inline u64 thread__db_id(const struct thread *thread)
246 return RC_CHK_ACCESS(thread)->db_id;
249 static inline void thread__set_db_id(struct thread *thread, u64 db_id)
251 RC_CHK_ACCESS(thread)->db_id = db_id;
254 static inline void *thread__priv(struct thread *thread)
256 return RC_CHK_ACCESS(thread)->priv;
259 static inline void thread__set_priv(struct thread *thread, void *p)
261 RC_CHK_ACCESS(thread)->priv = p;
264 static inline struct thread_stack *thread__ts(struct thread *thread)
266 return RC_CHK_ACCESS(thread)->ts;
269 static inline void thread__set_ts(struct thread *thread, struct thread_stack *ts)
271 RC_CHK_ACCESS(thread)->ts = ts;
274 static inline struct nsinfo *thread__nsinfo(struct thread *thread)
276 return RC_CHK_ACCESS(thread)->nsinfo;
279 static inline struct srccode_state *thread__srccode_state(struct thread *thread)
281 return &RC_CHK_ACCESS(thread)->srccode_state;
284 static inline bool thread__filter(const struct thread *thread)
286 return RC_CHK_ACCESS(thread)->filter;
289 static inline void thread__set_filter(struct thread *thread, bool filter)
291 RC_CHK_ACCESS(thread)->filter = filter;
294 static inline int thread__filter_entry_depth(const struct thread *thread)
296 return RC_CHK_ACCESS(thread)->filter_entry_depth;
299 static inline void thread__set_filter_entry_depth(struct thread *thread, int depth)
301 RC_CHK_ACCESS(thread)->filter_entry_depth = depth;
304 static inline bool thread__lbr_stitch_enable(const struct thread *thread)
306 return RC_CHK_ACCESS(thread)->lbr_stitch_enable;
309 static inline void thread__set_lbr_stitch_enable(struct thread *thread, bool en)
311 RC_CHK_ACCESS(thread)->lbr_stitch_enable = en;
314 static inline struct lbr_stitch *thread__lbr_stitch(struct thread *thread)
316 return RC_CHK_ACCESS(thread)->lbr_stitch;
319 static inline void thread__set_lbr_stitch(struct thread *thread, struct lbr_stitch *lbrs)
321 RC_CHK_ACCESS(thread)->lbr_stitch = lbrs;
324 static inline bool thread__is_filtered(struct thread *thread)
326 if (symbol_conf.comm_list &&
327 !strlist__has_entry(symbol_conf.comm_list, thread__comm_str(thread))) {
331 if (symbol_conf.pid_list &&
332 !intlist__has_entry(symbol_conf.pid_list, thread__pid(thread))) {
336 if (symbol_conf.tid_list &&
337 !intlist__has_entry(symbol_conf.tid_list, thread__tid(thread))) {
344 void thread__free_stitch_list(struct thread *thread);
346 void thread__resolve(struct thread *thread, struct addr_location *al,
347 struct perf_sample *sample);
349 #endif /* __PERF_THREAD_H */