1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/kernel.h>
10 #include "thread-stack.h"
13 #include "namespaces.h"
19 #include <api/fs/fs.h>
21 int thread__init_map_groups(struct thread
*thread
, struct machine
*machine
)
23 pid_t pid
= thread
->pid_
;
25 if (pid
== thread
->tid
|| pid
== -1) {
26 thread
->mg
= map_groups__new(machine
);
28 struct thread
*leader
= __machine__findnew_thread(machine
, pid
, pid
);
30 thread
->mg
= map_groups__get(leader
->mg
);
35 return thread
->mg
? 0 : -1;
38 struct thread
*thread__new(pid_t pid
, pid_t tid
)
42 struct thread
*thread
= zalloc(sizeof(*thread
));
49 INIT_LIST_HEAD(&thread
->namespaces_list
);
50 INIT_LIST_HEAD(&thread
->comm_list
);
51 init_rwsem(&thread
->namespaces_lock
);
52 init_rwsem(&thread
->comm_lock
);
54 comm_str
= malloc(32);
58 snprintf(comm_str
, 32, ":%d", tid
);
59 comm
= comm__new(comm_str
, 0, false);
64 list_add(&comm
->list
, &thread
->comm_list
);
65 refcount_set(&thread
->refcnt
, 1);
66 RB_CLEAR_NODE(&thread
->rb_node
);
67 /* Thread holds first ref to nsdata. */
68 thread
->nsinfo
= nsinfo__new(pid
);
69 srccode_state_init(&thread
->srccode_state
);
79 void thread__delete(struct thread
*thread
)
81 struct namespaces
*namespaces
, *tmp_namespaces
;
82 struct comm
*comm
, *tmp_comm
;
84 BUG_ON(!RB_EMPTY_NODE(&thread
->rb_node
));
86 thread_stack__free(thread
);
89 map_groups__put(thread
->mg
);
92 down_write(&thread
->namespaces_lock
);
93 list_for_each_entry_safe(namespaces
, tmp_namespaces
,
94 &thread
->namespaces_list
, list
) {
95 list_del(&namespaces
->list
);
96 namespaces__free(namespaces
);
98 up_write(&thread
->namespaces_lock
);
100 down_write(&thread
->comm_lock
);
101 list_for_each_entry_safe(comm
, tmp_comm
, &thread
->comm_list
, list
) {
102 list_del(&comm
->list
);
105 up_write(&thread
->comm_lock
);
107 unwind__finish_access(thread
);
108 nsinfo__zput(thread
->nsinfo
);
109 srccode_state_free(&thread
->srccode_state
);
111 exit_rwsem(&thread
->namespaces_lock
);
112 exit_rwsem(&thread
->comm_lock
);
116 struct thread
*thread__get(struct thread
*thread
)
119 refcount_inc(&thread
->refcnt
);
123 void thread__put(struct thread
*thread
)
125 if (thread
&& refcount_dec_and_test(&thread
->refcnt
)) {
127 * Remove it from the dead_threads list, as last reference
130 list_del_init(&thread
->node
);
131 thread__delete(thread
);
135 static struct namespaces
*__thread__namespaces(const struct thread
*thread
)
137 if (list_empty(&thread
->namespaces_list
))
140 return list_first_entry(&thread
->namespaces_list
, struct namespaces
, list
);
143 struct namespaces
*thread__namespaces(const struct thread
*thread
)
145 struct namespaces
*ns
;
147 down_read((struct rw_semaphore
*)&thread
->namespaces_lock
);
148 ns
= __thread__namespaces(thread
);
149 up_read((struct rw_semaphore
*)&thread
->namespaces_lock
);
154 static int __thread__set_namespaces(struct thread
*thread
, u64 timestamp
,
155 struct namespaces_event
*event
)
157 struct namespaces
*new, *curr
= __thread__namespaces(thread
);
159 new = namespaces__new(event
);
163 list_add(&new->list
, &thread
->namespaces_list
);
165 if (timestamp
&& curr
) {
167 * setns syscall must have changed few or all the namespaces
168 * of this thread. Update end time for the namespaces
171 curr
= list_next_entry(new, list
);
172 curr
->end_time
= timestamp
;
178 int thread__set_namespaces(struct thread
*thread
, u64 timestamp
,
179 struct namespaces_event
*event
)
183 down_write(&thread
->namespaces_lock
);
184 ret
= __thread__set_namespaces(thread
, timestamp
, event
);
185 up_write(&thread
->namespaces_lock
);
189 struct comm
*thread__comm(const struct thread
*thread
)
191 if (list_empty(&thread
->comm_list
))
194 return list_first_entry(&thread
->comm_list
, struct comm
, list
);
197 struct comm
*thread__exec_comm(const struct thread
*thread
)
199 struct comm
*comm
, *last
= NULL
;
201 list_for_each_entry(comm
, &thread
->comm_list
, list
) {
210 static int ____thread__set_comm(struct thread
*thread
, const char *str
,
211 u64 timestamp
, bool exec
)
213 struct comm
*new, *curr
= thread__comm(thread
);
215 /* Override the default :tid entry */
216 if (!thread
->comm_set
) {
217 int err
= comm__override(curr
, str
, timestamp
, exec
);
221 new = comm__new(str
, timestamp
, exec
);
224 list_add(&new->list
, &thread
->comm_list
);
227 unwind__flush_access(thread
);
230 thread
->comm_set
= true;
235 int __thread__set_comm(struct thread
*thread
, const char *str
, u64 timestamp
,
240 down_write(&thread
->comm_lock
);
241 ret
= ____thread__set_comm(thread
, str
, timestamp
, exec
);
242 up_write(&thread
->comm_lock
);
246 int thread__set_comm_from_proc(struct thread
*thread
)
253 if (!(snprintf(path
, sizeof(path
), "%d/task/%d/comm",
254 thread
->pid_
, thread
->tid
) >= (int)sizeof(path
)) &&
255 procfs__read_str(path
, &comm
, &sz
) == 0) {
257 err
= thread__set_comm(thread
, comm
, 0);
263 static const char *__thread__comm_str(const struct thread
*thread
)
265 const struct comm
*comm
= thread__comm(thread
);
270 return comm__str(comm
);
273 const char *thread__comm_str(const struct thread
*thread
)
277 down_read((struct rw_semaphore
*)&thread
->comm_lock
);
278 str
= __thread__comm_str(thread
);
279 up_read((struct rw_semaphore
*)&thread
->comm_lock
);
284 /* CHECKME: it should probably better return the max comm len from its comm list */
285 int thread__comm_len(struct thread
*thread
)
287 if (!thread
->comm_len
) {
288 const char *comm
= thread__comm_str(thread
);
291 thread
->comm_len
= strlen(comm
);
294 return thread
->comm_len
;
297 size_t thread__fprintf(struct thread
*thread
, FILE *fp
)
299 return fprintf(fp
, "Thread %d %s\n", thread
->tid
, thread__comm_str(thread
)) +
300 map_groups__fprintf(thread
->mg
, fp
);
303 int thread__insert_map(struct thread
*thread
, struct map
*map
)
307 ret
= unwind__prepare_access(thread
, map
, NULL
);
311 map_groups__fixup_overlappings(thread
->mg
, map
, stderr
);
312 map_groups__insert(thread
->mg
, map
);
317 static int __thread__prepare_access(struct thread
*thread
)
319 bool initialized
= false;
321 struct maps
*maps
= &thread
->mg
->maps
;
324 down_read(&maps
->lock
);
326 for (map
= maps__first(maps
); map
; map
= map__next(map
)) {
327 err
= unwind__prepare_access(thread
, map
, &initialized
);
328 if (err
|| initialized
)
332 up_read(&maps
->lock
);
337 static int thread__prepare_access(struct thread
*thread
)
341 if (symbol_conf
.use_callchain
)
342 err
= __thread__prepare_access(thread
);
347 static int thread__clone_map_groups(struct thread
*thread
,
348 struct thread
*parent
,
351 /* This is new thread, we share map groups for process. */
352 if (thread
->pid_
== parent
->pid_
)
353 return thread__prepare_access(thread
);
355 if (thread
->mg
== parent
->mg
) {
356 pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
357 thread
->pid_
, thread
->tid
, parent
->pid_
, parent
->tid
);
360 /* But this one is new process, copy maps. */
361 return do_maps_clone
? map_groups__clone(thread
, parent
->mg
) : 0;
364 int thread__fork(struct thread
*thread
, struct thread
*parent
, u64 timestamp
, bool do_maps_clone
)
366 if (parent
->comm_set
) {
367 const char *comm
= thread__comm_str(parent
);
371 err
= thread__set_comm(thread
, comm
, timestamp
);
376 thread
->ppid
= parent
->tid
;
377 return thread__clone_map_groups(thread
, parent
, do_maps_clone
);
380 void thread__find_cpumode_addr_location(struct thread
*thread
, u64 addr
,
381 struct addr_location
*al
)
384 const u8 cpumodes
[] = {
385 PERF_RECORD_MISC_USER
,
386 PERF_RECORD_MISC_KERNEL
,
387 PERF_RECORD_MISC_GUEST_USER
,
388 PERF_RECORD_MISC_GUEST_KERNEL
391 for (i
= 0; i
< ARRAY_SIZE(cpumodes
); i
++) {
392 thread__find_symbol(thread
, cpumodes
[i
], addr
, al
);
398 struct thread
*thread__main_thread(struct machine
*machine
, struct thread
*thread
)
400 if (thread
->pid_
== thread
->tid
)
401 return thread__get(thread
);
403 if (thread
->pid_
== -1)
406 return machine__find_thread(machine
, thread
->pid_
, thread
->pid_
);
409 int thread__memcpy(struct thread
*thread
, struct machine
*machine
,
410 void *buf
, u64 ip
, int len
, bool *is64bit
)
412 u8 cpumode
= PERF_RECORD_MISC_USER
;
413 struct addr_location al
;
416 if (machine__kernel_ip(machine
, ip
))
417 cpumode
= PERF_RECORD_MISC_KERNEL
;
419 if (!thread__find_map(thread
, cpumode
, ip
, &al
) || !al
.map
->dso
||
420 al
.map
->dso
->data
.status
== DSO_DATA_STATUS_ERROR
||
421 map__load(al
.map
) < 0)
424 offset
= al
.map
->map_ip(al
.map
, ip
);
426 *is64bit
= al
.map
->dso
->is_64_bit
;
428 return dso__data_read_offset(al
.map
->dso
, machine
, offset
, buf
, len
);