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