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
;
165 list_for_each_entry(comm
, &thread
->comm_list
, list
) {
174 int __thread__set_comm(struct thread
*thread
, const char *str
, u64 timestamp
,
177 struct comm
*new, *curr
= thread__comm(thread
);
179 /* Override the default :tid entry */
180 if (!thread
->comm_set
) {
181 int err
= comm__override(curr
, str
, timestamp
, exec
);
185 new = comm__new(str
, timestamp
, exec
);
188 list_add(&new->list
, &thread
->comm_list
);
191 unwind__flush_access(thread
);
194 thread
->comm_set
= true;
199 int thread__set_comm_from_proc(struct thread
*thread
)
206 if (!(snprintf(path
, sizeof(path
), "%d/task/%d/comm",
207 thread
->pid_
, thread
->tid
) >= (int)sizeof(path
)) &&
208 procfs__read_str(path
, &comm
, &sz
) == 0) {
210 err
= thread__set_comm(thread
, comm
, 0);
216 const char *thread__comm_str(const struct thread
*thread
)
218 const struct comm
*comm
= thread__comm(thread
);
223 return comm__str(comm
);
226 /* CHECKME: it should probably better return the max comm len from its comm list */
227 int thread__comm_len(struct thread
*thread
)
229 if (!thread
->comm_len
) {
230 const char *comm
= thread__comm_str(thread
);
233 thread
->comm_len
= strlen(comm
);
236 return thread
->comm_len
;
239 size_t thread__fprintf(struct thread
*thread
, FILE *fp
)
241 return fprintf(fp
, "Thread %d %s\n", thread
->tid
, thread__comm_str(thread
)) +
242 map_groups__fprintf(thread
->mg
, fp
);
245 int thread__insert_map(struct thread
*thread
, struct map
*map
)
249 ret
= unwind__prepare_access(thread
, map
, NULL
);
253 map_groups__fixup_overlappings(thread
->mg
, map
, stderr
);
254 map_groups__insert(thread
->mg
, map
);
259 static int __thread__prepare_access(struct thread
*thread
)
261 bool initialized
= false;
264 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
265 struct maps
*maps
= &thread
->mg
->maps
[i
];
268 pthread_rwlock_rdlock(&maps
->lock
);
270 for (map
= maps__first(maps
); map
; map
= map__next(map
)) {
271 err
= unwind__prepare_access(thread
, map
, &initialized
);
272 if (err
|| initialized
)
276 pthread_rwlock_unlock(&maps
->lock
);
282 static int thread__prepare_access(struct thread
*thread
)
286 if (symbol_conf
.use_callchain
)
287 err
= __thread__prepare_access(thread
);
292 static int thread__clone_map_groups(struct thread
*thread
,
293 struct thread
*parent
)
297 /* This is new thread, we share map groups for process. */
298 if (thread
->pid_
== parent
->pid_
)
299 return thread__prepare_access(thread
);
301 if (thread
->mg
== parent
->mg
) {
302 pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
303 thread
->pid_
, thread
->tid
, parent
->pid_
, parent
->tid
);
307 /* But this one is new process, copy maps. */
308 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
309 if (map_groups__clone(thread
, parent
->mg
, i
) < 0)
315 int thread__fork(struct thread
*thread
, struct thread
*parent
, u64 timestamp
)
317 if (parent
->comm_set
) {
318 const char *comm
= thread__comm_str(parent
);
322 err
= thread__set_comm(thread
, comm
, timestamp
);
327 thread
->ppid
= parent
->tid
;
328 return thread__clone_map_groups(thread
, parent
);
331 void thread__find_cpumode_addr_location(struct thread
*thread
,
332 enum map_type type
, u64 addr
,
333 struct addr_location
*al
)
336 const u8 cpumodes
[] = {
337 PERF_RECORD_MISC_USER
,
338 PERF_RECORD_MISC_KERNEL
,
339 PERF_RECORD_MISC_GUEST_USER
,
340 PERF_RECORD_MISC_GUEST_KERNEL
343 for (i
= 0; i
< ARRAY_SIZE(cpumodes
); i
++) {
344 thread__find_addr_location(thread
, cpumodes
[i
], type
, addr
, al
);
350 struct thread
*thread__main_thread(struct machine
*machine
, struct thread
*thread
)
352 if (thread
->pid_
== thread
->tid
)
353 return thread__get(thread
);
355 if (thread
->pid_
== -1)
358 return machine__find_thread(machine
, thread
->pid_
, thread
->pid_
);