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