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