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