xref: /linux/tools/perf/util/thread.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 #include "../perf.h"
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <linux/kernel.h>
7 #include "session.h"
8 #include "thread.h"
9 #include "thread-stack.h"
10 #include "util.h"
11 #include "debug.h"
12 #include "namespaces.h"
13 #include "comm.h"
14 #include "unwind.h"
15 
16 #include <api/fs/fs.h>
17 
18 int thread__init_map_groups(struct thread *thread, struct machine *machine)
19 {
20 	pid_t pid = thread->pid_;
21 
22 	if (pid == thread->tid || pid == -1) {
23 		thread->mg = map_groups__new(machine);
24 	} else {
25 		struct thread *leader = __machine__findnew_thread(machine, pid, pid);
26 		if (leader) {
27 			thread->mg = map_groups__get(leader->mg);
28 			thread__put(leader);
29 		}
30 	}
31 
32 	return thread->mg ? 0 : -1;
33 }
34 
35 struct thread *thread__new(pid_t pid, pid_t tid)
36 {
37 	char *comm_str;
38 	struct comm *comm;
39 	struct thread *thread = zalloc(sizeof(*thread));
40 
41 	if (thread != NULL) {
42 		thread->pid_ = pid;
43 		thread->tid = tid;
44 		thread->ppid = -1;
45 		thread->cpu = -1;
46 		INIT_LIST_HEAD(&thread->namespaces_list);
47 		INIT_LIST_HEAD(&thread->comm_list);
48 
49 		comm_str = malloc(32);
50 		if (!comm_str)
51 			goto err_thread;
52 
53 		snprintf(comm_str, 32, ":%d", tid);
54 		comm = comm__new(comm_str, 0, false);
55 		free(comm_str);
56 		if (!comm)
57 			goto err_thread;
58 
59 		list_add(&comm->list, &thread->comm_list);
60 		refcount_set(&thread->refcnt, 1);
61 		RB_CLEAR_NODE(&thread->rb_node);
62 	}
63 
64 	return thread;
65 
66 err_thread:
67 	free(thread);
68 	return NULL;
69 }
70 
71 void thread__delete(struct thread *thread)
72 {
73 	struct namespaces *namespaces, *tmp_namespaces;
74 	struct comm *comm, *tmp_comm;
75 
76 	BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
77 
78 	thread_stack__free(thread);
79 
80 	if (thread->mg) {
81 		map_groups__put(thread->mg);
82 		thread->mg = NULL;
83 	}
84 	list_for_each_entry_safe(namespaces, tmp_namespaces,
85 				 &thread->namespaces_list, list) {
86 		list_del(&namespaces->list);
87 		namespaces__free(namespaces);
88 	}
89 	list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
90 		list_del(&comm->list);
91 		comm__free(comm);
92 	}
93 	unwind__finish_access(thread);
94 
95 	free(thread);
96 }
97 
98 struct thread *thread__get(struct thread *thread)
99 {
100 	if (thread)
101 		refcount_inc(&thread->refcnt);
102 	return thread;
103 }
104 
105 void thread__put(struct thread *thread)
106 {
107 	if (thread && refcount_dec_and_test(&thread->refcnt)) {
108 		/*
109 		 * Remove it from the dead_threads list, as last reference
110 		 * is gone.
111 		 */
112 		list_del_init(&thread->node);
113 		thread__delete(thread);
114 	}
115 }
116 
117 struct namespaces *thread__namespaces(const struct thread *thread)
118 {
119 	if (list_empty(&thread->namespaces_list))
120 		return NULL;
121 
122 	return list_first_entry(&thread->namespaces_list, struct namespaces, list);
123 }
124 
125 int thread__set_namespaces(struct thread *thread, u64 timestamp,
126 			   struct namespaces_event *event)
127 {
128 	struct namespaces *new, *curr = thread__namespaces(thread);
129 
130 	new = namespaces__new(event);
131 	if (!new)
132 		return -ENOMEM;
133 
134 	list_add(&new->list, &thread->namespaces_list);
135 
136 	if (timestamp && curr) {
137 		/*
138 		 * setns syscall must have changed few or all the namespaces
139 		 * of this thread. Update end time for the namespaces
140 		 * previously used.
141 		 */
142 		curr = list_next_entry(new, list);
143 		curr->end_time = timestamp;
144 	}
145 
146 	return 0;
147 }
148 
149 struct comm *thread__comm(const struct thread *thread)
150 {
151 	if (list_empty(&thread->comm_list))
152 		return NULL;
153 
154 	return list_first_entry(&thread->comm_list, struct comm, list);
155 }
156 
157 struct comm *thread__exec_comm(const struct thread *thread)
158 {
159 	struct comm *comm, *last = NULL;
160 
161 	list_for_each_entry(comm, &thread->comm_list, list) {
162 		if (comm->exec)
163 			return comm;
164 		last = comm;
165 	}
166 
167 	return last;
168 }
169 
170 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
171 		       bool exec)
172 {
173 	struct comm *new, *curr = thread__comm(thread);
174 
175 	/* Override the default :tid entry */
176 	if (!thread->comm_set) {
177 		int err = comm__override(curr, str, timestamp, exec);
178 		if (err)
179 			return err;
180 	} else {
181 		new = comm__new(str, timestamp, exec);
182 		if (!new)
183 			return -ENOMEM;
184 		list_add(&new->list, &thread->comm_list);
185 
186 		if (exec)
187 			unwind__flush_access(thread);
188 	}
189 
190 	thread->comm_set = true;
191 
192 	return 0;
193 }
194 
195 int thread__set_comm_from_proc(struct thread *thread)
196 {
197 	char path[64];
198 	char *comm = NULL;
199 	size_t sz;
200 	int err = -1;
201 
202 	if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
203 		       thread->pid_, thread->tid) >= (int)sizeof(path)) &&
204 	    procfs__read_str(path, &comm, &sz) == 0) {
205 		comm[sz - 1] = '\0';
206 		err = thread__set_comm(thread, comm, 0);
207 	}
208 
209 	return err;
210 }
211 
212 const char *thread__comm_str(const struct thread *thread)
213 {
214 	const struct comm *comm = thread__comm(thread);
215 
216 	if (!comm)
217 		return NULL;
218 
219 	return comm__str(comm);
220 }
221 
222 /* CHECKME: it should probably better return the max comm len from its comm list */
223 int thread__comm_len(struct thread *thread)
224 {
225 	if (!thread->comm_len) {
226 		const char *comm = thread__comm_str(thread);
227 		if (!comm)
228 			return 0;
229 		thread->comm_len = strlen(comm);
230 	}
231 
232 	return thread->comm_len;
233 }
234 
235 size_t thread__fprintf(struct thread *thread, FILE *fp)
236 {
237 	return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
238 	       map_groups__fprintf(thread->mg, fp);
239 }
240 
241 int thread__insert_map(struct thread *thread, struct map *map)
242 {
243 	int ret;
244 
245 	ret = unwind__prepare_access(thread, map, NULL);
246 	if (ret)
247 		return ret;
248 
249 	map_groups__fixup_overlappings(thread->mg, map, stderr);
250 	map_groups__insert(thread->mg, map);
251 
252 	return 0;
253 }
254 
255 static int __thread__prepare_access(struct thread *thread)
256 {
257 	bool initialized = false;
258 	int i, err = 0;
259 
260 	for (i = 0; i < MAP__NR_TYPES; ++i) {
261 		struct maps *maps = &thread->mg->maps[i];
262 		struct map *map;
263 
264 		pthread_rwlock_rdlock(&maps->lock);
265 
266 		for (map = maps__first(maps); map; map = map__next(map)) {
267 			err = unwind__prepare_access(thread, map, &initialized);
268 			if (err || initialized)
269 				break;
270 		}
271 
272 		pthread_rwlock_unlock(&maps->lock);
273 	}
274 
275 	return err;
276 }
277 
278 static int thread__prepare_access(struct thread *thread)
279 {
280 	int err = 0;
281 
282 	if (symbol_conf.use_callchain)
283 		err = __thread__prepare_access(thread);
284 
285 	return err;
286 }
287 
288 static int thread__clone_map_groups(struct thread *thread,
289 				    struct thread *parent)
290 {
291 	int i;
292 
293 	/* This is new thread, we share map groups for process. */
294 	if (thread->pid_ == parent->pid_)
295 		return thread__prepare_access(thread);
296 
297 	if (thread->mg == parent->mg) {
298 		pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
299 			 thread->pid_, thread->tid, parent->pid_, parent->tid);
300 		return 0;
301 	}
302 
303 	/* But this one is new process, copy maps. */
304 	for (i = 0; i < MAP__NR_TYPES; ++i)
305 		if (map_groups__clone(thread, parent->mg, i) < 0)
306 			return -ENOMEM;
307 
308 	return 0;
309 }
310 
311 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
312 {
313 	if (parent->comm_set) {
314 		const char *comm = thread__comm_str(parent);
315 		int err;
316 		if (!comm)
317 			return -ENOMEM;
318 		err = thread__set_comm(thread, comm, timestamp);
319 		if (err)
320 			return err;
321 	}
322 
323 	thread->ppid = parent->tid;
324 	return thread__clone_map_groups(thread, parent);
325 }
326 
327 void thread__find_cpumode_addr_location(struct thread *thread,
328 					enum map_type type, u64 addr,
329 					struct addr_location *al)
330 {
331 	size_t i;
332 	const u8 cpumodes[] = {
333 		PERF_RECORD_MISC_USER,
334 		PERF_RECORD_MISC_KERNEL,
335 		PERF_RECORD_MISC_GUEST_USER,
336 		PERF_RECORD_MISC_GUEST_KERNEL
337 	};
338 
339 	for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
340 		thread__find_addr_location(thread, cpumodes[i], type, addr, al);
341 		if (al->map)
342 			break;
343 	}
344 }
345 
346 struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
347 {
348 	if (thread->pid_ == thread->tid)
349 		return thread__get(thread);
350 
351 	if (thread->pid_ == -1)
352 		return NULL;
353 
354 	return machine__find_thread(machine, thread->pid_, thread->pid_);
355 }
356