xref: /openbmc/linux/tools/perf/util/thread.h (revision cd238eff)
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 
17 struct addr_location;
18 struct map;
19 struct namespaces_event;
20 struct thread_stack;
21 struct unwind_libunwind_ops;
22 
23 struct thread {
24 	union {
25 		struct rb_node	 rb_node;
26 		struct list_head node;
27 	};
28 	struct map_groups	*mg;
29 	pid_t			pid_; /* Not all tools update this */
30 	pid_t			tid;
31 	pid_t			ppid;
32 	int			cpu;
33 	refcount_t		refcnt;
34 	bool			comm_set;
35 	int			comm_len;
36 	bool			dead; /* if set thread has exited */
37 	struct list_head	namespaces_list;
38 	struct rw_semaphore	namespaces_lock;
39 	struct list_head	comm_list;
40 	struct rw_semaphore	comm_lock;
41 	u64			db_id;
42 
43 	void			*priv;
44 	struct thread_stack	*ts;
45 	struct nsinfo		*nsinfo;
46 	struct srccode_state	srccode_state;
47 #ifdef HAVE_LIBUNWIND_SUPPORT
48 	void				*addr_space;
49 	struct unwind_libunwind_ops	*unwind_libunwind_ops;
50 #endif
51 	bool			filter;
52 	int			filter_entry_depth;
53 };
54 
55 struct machine;
56 struct namespaces;
57 struct comm;
58 
59 struct thread *thread__new(pid_t pid, pid_t tid);
60 int thread__init_map_groups(struct thread *thread, struct machine *machine);
61 void thread__delete(struct thread *thread);
62 
63 struct thread *thread__get(struct thread *thread);
64 void thread__put(struct thread *thread);
65 
66 static inline void __thread__zput(struct thread **thread)
67 {
68 	thread__put(*thread);
69 	*thread = NULL;
70 }
71 
72 #define thread__zput(thread) __thread__zput(&thread)
73 
74 static inline void thread__exited(struct thread *thread)
75 {
76 	thread->dead = true;
77 }
78 
79 struct namespaces *thread__namespaces(const struct thread *thread);
80 int thread__set_namespaces(struct thread *thread, u64 timestamp,
81 			   struct namespaces_event *event);
82 
83 int __thread__set_comm(struct thread *thread, const char *comm, u64 timestamp,
84 		       bool exec);
85 static inline int thread__set_comm(struct thread *thread, const char *comm,
86 				   u64 timestamp)
87 {
88 	return __thread__set_comm(thread, comm, timestamp, false);
89 }
90 
91 int thread__set_comm_from_proc(struct thread *thread);
92 
93 int thread__comm_len(struct thread *thread);
94 struct comm *thread__comm(const struct thread *thread);
95 struct comm *thread__exec_comm(const struct thread *thread);
96 const char *thread__comm_str(const struct thread *thread);
97 int thread__insert_map(struct thread *thread, struct map *map);
98 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone);
99 size_t thread__fprintf(struct thread *thread, FILE *fp);
100 
101 struct thread *thread__main_thread(struct machine *machine, struct thread *thread);
102 
103 struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
104 			     struct addr_location *al);
105 struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
106 				struct addr_location *al);
107 
108 struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
109 				   u64 addr, struct addr_location *al);
110 struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
111 				      u64 addr, struct addr_location *al);
112 
113 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
114 					struct addr_location *al);
115 
116 int thread__memcpy(struct thread *thread, struct machine *machine,
117 		   void *buf, u64 ip, int len, bool *is64bit);
118 
119 static inline void *thread__priv(struct thread *thread)
120 {
121 	return thread->priv;
122 }
123 
124 static inline void thread__set_priv(struct thread *thread, void *p)
125 {
126 	thread->priv = p;
127 }
128 
129 static inline bool thread__is_filtered(struct thread *thread)
130 {
131 	if (symbol_conf.comm_list &&
132 	    !strlist__has_entry(symbol_conf.comm_list, thread__comm_str(thread))) {
133 		return true;
134 	}
135 
136 	if (symbol_conf.pid_list &&
137 	    !intlist__has_entry(symbol_conf.pid_list, thread->pid_)) {
138 		return true;
139 	}
140 
141 	if (symbol_conf.tid_list &&
142 	    !intlist__has_entry(symbol_conf.tid_list, thread->tid)) {
143 		return true;
144 	}
145 
146 	return false;
147 }
148 
149 #endif	/* __PERF_THREAD_H */
150