1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __PERF_THREAD_H
3*4882a593Smuzhiyun #define __PERF_THREAD_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/refcount.h>
6*4882a593Smuzhiyun #include <linux/rbtree.h>
7*4882a593Smuzhiyun #include <linux/list.h>
8*4882a593Smuzhiyun #include <stdio.h>
9*4882a593Smuzhiyun #include <unistd.h>
10*4882a593Smuzhiyun #include <sys/types.h>
11*4882a593Smuzhiyun #include "srccode.h"
12*4882a593Smuzhiyun #include "symbol_conf.h"
13*4882a593Smuzhiyun #include <strlist.h>
14*4882a593Smuzhiyun #include <intlist.h>
15*4882a593Smuzhiyun #include "rwsem.h"
16*4882a593Smuzhiyun #include "event.h"
17*4882a593Smuzhiyun #include "callchain.h"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun struct addr_location;
20*4882a593Smuzhiyun struct map;
21*4882a593Smuzhiyun struct perf_record_namespaces;
22*4882a593Smuzhiyun struct thread_stack;
23*4882a593Smuzhiyun struct unwind_libunwind_ops;
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun struct lbr_stitch {
26*4882a593Smuzhiyun struct list_head lists;
27*4882a593Smuzhiyun struct list_head free_lists;
28*4882a593Smuzhiyun struct perf_sample prev_sample;
29*4882a593Smuzhiyun struct callchain_cursor_node *prev_lbr_cursor;
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun struct thread {
33*4882a593Smuzhiyun union {
34*4882a593Smuzhiyun struct rb_node rb_node;
35*4882a593Smuzhiyun struct list_head node;
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun struct maps *maps;
38*4882a593Smuzhiyun pid_t pid_; /* Not all tools update this */
39*4882a593Smuzhiyun pid_t tid;
40*4882a593Smuzhiyun pid_t ppid;
41*4882a593Smuzhiyun int cpu;
42*4882a593Smuzhiyun refcount_t refcnt;
43*4882a593Smuzhiyun bool comm_set;
44*4882a593Smuzhiyun int comm_len;
45*4882a593Smuzhiyun bool dead; /* if set thread has exited */
46*4882a593Smuzhiyun struct list_head namespaces_list;
47*4882a593Smuzhiyun struct rw_semaphore namespaces_lock;
48*4882a593Smuzhiyun struct list_head comm_list;
49*4882a593Smuzhiyun struct rw_semaphore comm_lock;
50*4882a593Smuzhiyun u64 db_id;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun void *priv;
53*4882a593Smuzhiyun struct thread_stack *ts;
54*4882a593Smuzhiyun struct nsinfo *nsinfo;
55*4882a593Smuzhiyun struct srccode_state srccode_state;
56*4882a593Smuzhiyun bool filter;
57*4882a593Smuzhiyun int filter_entry_depth;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* LBR call stack stitch */
60*4882a593Smuzhiyun bool lbr_stitch_enable;
61*4882a593Smuzhiyun struct lbr_stitch *lbr_stitch;
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun struct machine;
65*4882a593Smuzhiyun struct namespaces;
66*4882a593Smuzhiyun struct comm;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun struct thread *thread__new(pid_t pid, pid_t tid);
69*4882a593Smuzhiyun int thread__init_maps(struct thread *thread, struct machine *machine);
70*4882a593Smuzhiyun void thread__delete(struct thread *thread);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun struct thread *thread__get(struct thread *thread);
73*4882a593Smuzhiyun void thread__put(struct thread *thread);
74*4882a593Smuzhiyun
__thread__zput(struct thread ** thread)75*4882a593Smuzhiyun static inline void __thread__zput(struct thread **thread)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun thread__put(*thread);
78*4882a593Smuzhiyun *thread = NULL;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun #define thread__zput(thread) __thread__zput(&thread)
82*4882a593Smuzhiyun
thread__exited(struct thread * thread)83*4882a593Smuzhiyun static inline void thread__exited(struct thread *thread)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun thread->dead = true;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun struct namespaces *thread__namespaces(struct thread *thread);
89*4882a593Smuzhiyun int thread__set_namespaces(struct thread *thread, u64 timestamp,
90*4882a593Smuzhiyun struct perf_record_namespaces *event);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun int __thread__set_comm(struct thread *thread, const char *comm, u64 timestamp,
93*4882a593Smuzhiyun bool exec);
thread__set_comm(struct thread * thread,const char * comm,u64 timestamp)94*4882a593Smuzhiyun static inline int thread__set_comm(struct thread *thread, const char *comm,
95*4882a593Smuzhiyun u64 timestamp)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun return __thread__set_comm(thread, comm, timestamp, false);
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun int thread__set_comm_from_proc(struct thread *thread);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun int thread__comm_len(struct thread *thread);
103*4882a593Smuzhiyun struct comm *thread__comm(const struct thread *thread);
104*4882a593Smuzhiyun struct comm *thread__exec_comm(const struct thread *thread);
105*4882a593Smuzhiyun const char *thread__comm_str(struct thread *thread);
106*4882a593Smuzhiyun int thread__insert_map(struct thread *thread, struct map *map);
107*4882a593Smuzhiyun int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone);
108*4882a593Smuzhiyun size_t thread__fprintf(struct thread *thread, FILE *fp);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun struct thread *thread__main_thread(struct machine *machine, struct thread *thread);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
113*4882a593Smuzhiyun struct addr_location *al);
114*4882a593Smuzhiyun struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
115*4882a593Smuzhiyun struct addr_location *al);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
118*4882a593Smuzhiyun u64 addr, struct addr_location *al);
119*4882a593Smuzhiyun struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
120*4882a593Smuzhiyun u64 addr, struct addr_location *al);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
123*4882a593Smuzhiyun struct addr_location *al);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun int thread__memcpy(struct thread *thread, struct machine *machine,
126*4882a593Smuzhiyun void *buf, u64 ip, int len, bool *is64bit);
127*4882a593Smuzhiyun
thread__priv(struct thread * thread)128*4882a593Smuzhiyun static inline void *thread__priv(struct thread *thread)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun return thread->priv;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
thread__set_priv(struct thread * thread,void * p)133*4882a593Smuzhiyun static inline void thread__set_priv(struct thread *thread, void *p)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun thread->priv = p;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
thread__is_filtered(struct thread * thread)138*4882a593Smuzhiyun static inline bool thread__is_filtered(struct thread *thread)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun if (symbol_conf.comm_list &&
141*4882a593Smuzhiyun !strlist__has_entry(symbol_conf.comm_list, thread__comm_str(thread))) {
142*4882a593Smuzhiyun return true;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun if (symbol_conf.pid_list &&
146*4882a593Smuzhiyun !intlist__has_entry(symbol_conf.pid_list, thread->pid_)) {
147*4882a593Smuzhiyun return true;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun if (symbol_conf.tid_list &&
151*4882a593Smuzhiyun !intlist__has_entry(symbol_conf.tid_list, thread->tid)) {
152*4882a593Smuzhiyun return true;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun return false;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun void thread__free_stitch_list(struct thread *thread);
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun #endif /* __PERF_THREAD_H */
161