at master 9.5 kB view raw
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 unsigned int prev_lbr_cursor_size; 30}; 31 32DECLARE_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_machine: The ELF EM_* associated with the thread. EM_NONE if not 65 * computed. 66 */ 67 uint16_t e_machine; 68 /* LBR call stack stitch */ 69 bool lbr_stitch_enable; 70 struct lbr_stitch *lbr_stitch; 71}; 72 73struct machine; 74struct namespaces; 75struct comm; 76 77struct thread *thread__new(pid_t pid, pid_t tid); 78int thread__init_maps(struct thread *thread, struct machine *machine); 79void thread__delete(struct thread *thread); 80 81void thread__set_priv_destructor(void (*destructor)(void *priv)); 82 83struct thread *thread__get(struct thread *thread); 84void thread__put(struct thread *thread); 85 86static inline void __thread__zput(struct thread **thread) 87{ 88 thread__put(*thread); 89 *thread = NULL; 90} 91 92#define thread__zput(thread) __thread__zput(&thread) 93 94struct namespaces *thread__namespaces(struct thread *thread); 95int thread__set_namespaces(struct thread *thread, u64 timestamp, 96 struct perf_record_namespaces *event); 97 98int __thread__set_comm(struct thread *thread, const char *comm, u64 timestamp, 99 bool exec); 100static inline int thread__set_comm(struct thread *thread, const char *comm, 101 u64 timestamp) 102{ 103 return __thread__set_comm(thread, comm, timestamp, false); 104} 105 106int thread__set_comm_from_proc(struct thread *thread); 107 108int thread__comm_len(struct thread *thread); 109struct comm *thread__comm(struct thread *thread); 110struct comm *thread__exec_comm(struct thread *thread); 111const char *thread__comm_str(struct thread *thread); 112int thread__insert_map(struct thread *thread, struct map *map); 113int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone); 114size_t thread__fprintf(struct thread *thread, FILE *fp); 115 116struct thread *thread__main_thread(struct machine *machine, struct thread *thread); 117 118struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr, 119 struct addr_location *al); 120struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr, 121 struct addr_location *al); 122 123struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode, 124 u64 addr, struct addr_location *al); 125struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode, 126 u64 addr, struct addr_location *al); 127 128void thread__find_cpumode_addr_location(struct thread *thread, u64 addr, 129 bool symbols, struct addr_location *al); 130 131int thread__memcpy(struct thread *thread, struct machine *machine, 132 void *buf, u64 ip, int len, bool *is64bit); 133 134static inline struct maps *thread__maps(struct thread *thread) 135{ 136 return RC_CHK_ACCESS(thread)->maps; 137} 138 139static inline void thread__set_maps(struct thread *thread, struct maps *maps) 140{ 141 RC_CHK_ACCESS(thread)->maps = maps; 142} 143 144static inline pid_t thread__pid(const struct thread *thread) 145{ 146 return RC_CHK_ACCESS(thread)->pid_; 147} 148 149static inline void thread__set_pid(struct thread *thread, pid_t pid_) 150{ 151 RC_CHK_ACCESS(thread)->pid_ = pid_; 152} 153 154static inline pid_t thread__tid(const struct thread *thread) 155{ 156 return RC_CHK_ACCESS(thread)->tid; 157} 158 159static inline void thread__set_tid(struct thread *thread, pid_t tid) 160{ 161 RC_CHK_ACCESS(thread)->tid = tid; 162} 163 164static inline pid_t thread__ppid(const struct thread *thread) 165{ 166 return RC_CHK_ACCESS(thread)->ppid; 167} 168 169static inline void thread__set_ppid(struct thread *thread, pid_t ppid) 170{ 171 RC_CHK_ACCESS(thread)->ppid = ppid; 172} 173 174static inline int thread__cpu(const struct thread *thread) 175{ 176 return RC_CHK_ACCESS(thread)->cpu; 177} 178 179static inline void thread__set_cpu(struct thread *thread, int cpu) 180{ 181 RC_CHK_ACCESS(thread)->cpu = cpu; 182} 183 184static inline int thread__guest_cpu(const struct thread *thread) 185{ 186 return RC_CHK_ACCESS(thread)->guest_cpu; 187} 188 189static inline void thread__set_guest_cpu(struct thread *thread, int guest_cpu) 190{ 191 RC_CHK_ACCESS(thread)->guest_cpu = guest_cpu; 192} 193 194static inline refcount_t *thread__refcnt(struct thread *thread) 195{ 196 return &RC_CHK_ACCESS(thread)->refcnt; 197} 198 199static inline void thread__set_exited(struct thread *thread, bool exited) 200{ 201 RC_CHK_ACCESS(thread)->exited = exited; 202} 203 204static inline bool thread__comm_set(const struct thread *thread) 205{ 206 return RC_CHK_ACCESS(thread)->comm_set; 207} 208 209static inline void thread__set_comm_set(struct thread *thread, bool set) 210{ 211 RC_CHK_ACCESS(thread)->comm_set = set; 212} 213 214static inline int thread__var_comm_len(const struct thread *thread) 215{ 216 return RC_CHK_ACCESS(thread)->comm_len; 217} 218 219static inline void thread__set_comm_len(struct thread *thread, int len) 220{ 221 RC_CHK_ACCESS(thread)->comm_len = len; 222} 223 224static inline struct list_head *thread__namespaces_list(struct thread *thread) 225{ 226 return &RC_CHK_ACCESS(thread)->namespaces_list; 227} 228 229static inline int thread__namespaces_list_empty(const struct thread *thread) 230{ 231 return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list); 232} 233 234static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread) 235{ 236 return &RC_CHK_ACCESS(thread)->namespaces_lock; 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 struct list_head *thread__comm_list(struct thread *thread) 245 SHARED_LOCKS_REQUIRED(thread__comm_lock(thread)) 246{ 247 return &RC_CHK_ACCESS(thread)->comm_list; 248} 249 250static inline u64 thread__db_id(const struct thread *thread) 251{ 252 return RC_CHK_ACCESS(thread)->db_id; 253} 254 255static inline void thread__set_db_id(struct thread *thread, u64 db_id) 256{ 257 RC_CHK_ACCESS(thread)->db_id = db_id; 258} 259 260static inline void *thread__priv(struct thread *thread) 261{ 262 return RC_CHK_ACCESS(thread)->priv; 263} 264 265static inline void thread__set_priv(struct thread *thread, void *p) 266{ 267 RC_CHK_ACCESS(thread)->priv = p; 268} 269 270static inline struct thread_stack *thread__ts(struct thread *thread) 271{ 272 return RC_CHK_ACCESS(thread)->ts; 273} 274 275static inline void thread__set_ts(struct thread *thread, struct thread_stack *ts) 276{ 277 RC_CHK_ACCESS(thread)->ts = ts; 278} 279 280static inline struct nsinfo *thread__nsinfo(struct thread *thread) 281{ 282 return RC_CHK_ACCESS(thread)->nsinfo; 283} 284 285static inline struct srccode_state *thread__srccode_state(struct thread *thread) 286{ 287 return &RC_CHK_ACCESS(thread)->srccode_state; 288} 289 290static inline bool thread__filter(const struct thread *thread) 291{ 292 return RC_CHK_ACCESS(thread)->filter; 293} 294 295static inline void thread__set_filter(struct thread *thread, bool filter) 296{ 297 RC_CHK_ACCESS(thread)->filter = filter; 298} 299 300static inline int thread__filter_entry_depth(const struct thread *thread) 301{ 302 return RC_CHK_ACCESS(thread)->filter_entry_depth; 303} 304 305static inline void thread__set_filter_entry_depth(struct thread *thread, int depth) 306{ 307 RC_CHK_ACCESS(thread)->filter_entry_depth = depth; 308} 309 310uint16_t thread__e_machine(struct thread *thread, struct machine *machine); 311 312static inline void thread__set_e_machine(struct thread *thread, uint16_t e_machine) 313{ 314 RC_CHK_ACCESS(thread)->e_machine = e_machine; 315} 316 317 318static inline bool thread__lbr_stitch_enable(const struct thread *thread) 319{ 320 return RC_CHK_ACCESS(thread)->lbr_stitch_enable; 321} 322 323static inline void thread__set_lbr_stitch_enable(struct thread *thread, bool en) 324{ 325 RC_CHK_ACCESS(thread)->lbr_stitch_enable = en; 326} 327 328static inline struct lbr_stitch *thread__lbr_stitch(struct thread *thread) 329{ 330 return RC_CHK_ACCESS(thread)->lbr_stitch; 331} 332 333static inline void thread__set_lbr_stitch(struct thread *thread, struct lbr_stitch *lbrs) 334{ 335 RC_CHK_ACCESS(thread)->lbr_stitch = lbrs; 336} 337 338static inline bool thread__is_filtered(struct thread *thread) 339{ 340 if (symbol_conf.comm_list && 341 !strlist__has_entry(symbol_conf.comm_list, thread__comm_str(thread))) { 342 return true; 343 } 344 345 if (symbol_conf.pid_list && 346 !intlist__has_entry(symbol_conf.pid_list, thread__pid(thread))) { 347 return true; 348 } 349 350 if (symbol_conf.tid_list && 351 !intlist__has_entry(symbol_conf.tid_list, thread__tid(thread))) { 352 return true; 353 } 354 355 return false; 356} 357 358void thread__free_stitch_list(struct thread *thread); 359 360void thread__resolve(struct thread *thread, struct addr_location *al, 361 struct perf_sample *sample); 362 363#endif /* __PERF_THREAD_H */