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