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