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