1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/kernel.h>
10 #include "thread-stack.h"
13 #include "namespaces.h"
17 #include <api/fs/fs.h>
19 int thread__init_map_groups(struct thread *thread, struct machine *machine)
21 pid_t pid = thread->pid_;
23 if (pid == thread->tid || pid == -1) {
24 thread->mg = map_groups__new(machine);
26 struct thread *leader = __machine__findnew_thread(machine, pid, pid);
28 thread->mg = map_groups__get(leader->mg);
33 return thread->mg ? 0 : -1;
36 struct thread *thread__new(pid_t pid, pid_t tid)
40 struct thread *thread = zalloc(sizeof(*thread));
47 INIT_LIST_HEAD(&thread->namespaces_list);
48 INIT_LIST_HEAD(&thread->comm_list);
50 comm_str = malloc(32);
54 snprintf(comm_str, 32, ":%d", tid);
55 comm = comm__new(comm_str, 0, false);
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);
74 void thread__delete(struct thread *thread)
76 struct namespaces *namespaces, *tmp_namespaces;
77 struct comm *comm, *tmp_comm;
79 BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
81 thread_stack__free(thread);
84 map_groups__put(thread->mg);
87 list_for_each_entry_safe(namespaces, tmp_namespaces,
88 &thread->namespaces_list, list) {
89 list_del(&namespaces->list);
90 namespaces__free(namespaces);
92 list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
93 list_del(&comm->list);
96 unwind__finish_access(thread);
97 nsinfo__zput(thread->nsinfo);
102 struct thread *thread__get(struct thread *thread)
105 refcount_inc(&thread->refcnt);
109 void thread__put(struct thread *thread)
111 if (thread && refcount_dec_and_test(&thread->refcnt)) {
113 * Remove it from the dead_threads list, as last reference
116 list_del_init(&thread->node);
117 thread__delete(thread);
121 struct namespaces *thread__namespaces(const struct thread *thread)
123 if (list_empty(&thread->namespaces_list))
126 return list_first_entry(&thread->namespaces_list, struct namespaces, list);
129 int thread__set_namespaces(struct thread *thread, u64 timestamp,
130 struct namespaces_event *event)
132 struct namespaces *new, *curr = thread__namespaces(thread);
134 new = namespaces__new(event);
138 list_add(&new->list, &thread->namespaces_list);
140 if (timestamp && curr) {
142 * setns syscall must have changed few or all the namespaces
143 * of this thread. Update end time for the namespaces
146 curr = list_next_entry(new, list);
147 curr->end_time = timestamp;
153 struct comm *thread__comm(const struct thread *thread)
155 if (list_empty(&thread->comm_list))
158 return list_first_entry(&thread->comm_list, struct comm, list);
161 struct comm *thread__exec_comm(const struct thread *thread)
163 struct comm *comm, *last = NULL, *second_last = NULL;
165 list_for_each_entry(comm, &thread->comm_list, list) {
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
178 if (second_last && !last->start && thread->pid_ == thread->tid)
184 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
187 struct comm *new, *curr = thread__comm(thread);
189 /* Override the default :tid entry */
190 if (!thread->comm_set) {
191 int err = comm__override(curr, str, timestamp, exec);
195 new = comm__new(str, timestamp, exec);
198 list_add(&new->list, &thread->comm_list);
201 unwind__flush_access(thread);
204 thread->comm_set = true;
209 int thread__set_comm_from_proc(struct thread *thread)
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) {
220 err = thread__set_comm(thread, comm, 0);
226 const char *thread__comm_str(const struct thread *thread)
228 const struct comm *comm = thread__comm(thread);
233 return comm__str(comm);
236 /* CHECKME: it should probably better return the max comm len from its comm list */
237 int thread__comm_len(struct thread *thread)
239 if (!thread->comm_len) {
240 const char *comm = thread__comm_str(thread);
243 thread->comm_len = strlen(comm);
246 return thread->comm_len;
249 size_t thread__fprintf(struct thread *thread, FILE *fp)
251 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
252 map_groups__fprintf(thread->mg, fp);
255 int thread__insert_map(struct thread *thread, struct map *map)
259 ret = unwind__prepare_access(thread, map, NULL);
263 map_groups__fixup_overlappings(thread->mg, map, stderr);
264 map_groups__insert(thread->mg, map);
269 static int __thread__prepare_access(struct thread *thread)
271 bool initialized = false;
274 for (i = 0; i < MAP__NR_TYPES; ++i) {
275 struct maps *maps = &thread->mg->maps[i];
278 pthread_rwlock_rdlock(&maps->lock);
280 for (map = maps__first(maps); map; map = map__next(map)) {
281 err = unwind__prepare_access(thread, map, &initialized);
282 if (err || initialized)
286 pthread_rwlock_unlock(&maps->lock);
292 static int thread__prepare_access(struct thread *thread)
296 if (symbol_conf.use_callchain)
297 err = __thread__prepare_access(thread);
302 static int thread__clone_map_groups(struct thread *thread,
303 struct thread *parent)
307 /* This is new thread, we share map groups for process. */
308 if (thread->pid_ == parent->pid_)
309 return thread__prepare_access(thread);
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);
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)
325 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
327 if (parent->comm_set) {
328 const char *comm = thread__comm_str(parent);
332 err = thread__set_comm(thread, comm, timestamp);
337 thread->ppid = parent->tid;
338 return thread__clone_map_groups(thread, parent);
341 void thread__find_cpumode_addr_location(struct thread *thread,
342 enum map_type type, u64 addr,
343 struct addr_location *al)
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
353 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
354 thread__find_addr_location(thread, cpumodes[i], type, addr, al);
360 struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
362 if (thread->pid_ == thread->tid)
363 return thread__get(thread);
365 if (thread->pid_ == -1)
368 return machine__find_thread(machine, thread->pid_, thread->pid_);