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