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 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 static int __thread__set_namespaces(struct thread
*thread
, u64 timestamp
,
144 struct namespaces_event
*event
)
146 struct namespaces
*new, *curr
= thread__namespaces(thread
);
148 new = namespaces__new(event
);
152 list_add(&new->list
, &thread
->namespaces_list
);
154 if (timestamp
&& curr
) {
156 * setns syscall must have changed few or all the namespaces
157 * of this thread. Update end time for the namespaces
160 curr
= list_next_entry(new, list
);
161 curr
->end_time
= timestamp
;
167 int thread__set_namespaces(struct thread
*thread
, u64 timestamp
,
168 struct namespaces_event
*event
)
172 down_write(&thread
->namespaces_lock
);
173 ret
= __thread__set_namespaces(thread
, timestamp
, event
);
174 up_write(&thread
->namespaces_lock
);
178 struct comm
*thread__comm(const struct thread
*thread
)
180 if (list_empty(&thread
->comm_list
))
183 return list_first_entry(&thread
->comm_list
, struct comm
, list
);
186 struct comm
*thread__exec_comm(const struct thread
*thread
)
188 struct comm
*comm
, *last
= NULL
;
190 list_for_each_entry(comm
, &thread
->comm_list
, list
) {
199 static int ____thread__set_comm(struct thread
*thread
, const char *str
,
200 u64 timestamp
, bool exec
)
202 struct comm
*new, *curr
= thread__comm(thread
);
204 /* Override the default :tid entry */
205 if (!thread
->comm_set
) {
206 int err
= comm__override(curr
, str
, timestamp
, exec
);
210 new = comm__new(str
, timestamp
, exec
);
213 list_add(&new->list
, &thread
->comm_list
);
216 unwind__flush_access(thread
);
219 thread
->comm_set
= true;
224 int __thread__set_comm(struct thread
*thread
, const char *str
, u64 timestamp
,
229 down_write(&thread
->comm_lock
);
230 ret
= ____thread__set_comm(thread
, str
, timestamp
, exec
);
231 up_write(&thread
->comm_lock
);
235 int thread__set_comm_from_proc(struct thread
*thread
)
242 if (!(snprintf(path
, sizeof(path
), "%d/task/%d/comm",
243 thread
->pid_
, thread
->tid
) >= (int)sizeof(path
)) &&
244 procfs__read_str(path
, &comm
, &sz
) == 0) {
246 err
= thread__set_comm(thread
, comm
, 0);
252 static const char *__thread__comm_str(const struct thread
*thread
)
254 const struct comm
*comm
= thread__comm(thread
);
259 return comm__str(comm
);
262 const char *thread__comm_str(const struct thread
*thread
)
266 down_read((struct rw_semaphore
*)&thread
->comm_lock
);
267 str
= __thread__comm_str(thread
);
268 up_read((struct rw_semaphore
*)&thread
->comm_lock
);
273 /* CHECKME: it should probably better return the max comm len from its comm list */
274 int thread__comm_len(struct thread
*thread
)
276 if (!thread
->comm_len
) {
277 const char *comm
= thread__comm_str(thread
);
280 thread
->comm_len
= strlen(comm
);
283 return thread
->comm_len
;
286 size_t thread__fprintf(struct thread
*thread
, FILE *fp
)
288 return fprintf(fp
, "Thread %d %s\n", thread
->tid
, thread__comm_str(thread
)) +
289 map_groups__fprintf(thread
->mg
, fp
);
292 int thread__insert_map(struct thread
*thread
, struct map
*map
)
296 ret
= unwind__prepare_access(thread
, map
, NULL
);
300 map_groups__fixup_overlappings(thread
->mg
, map
, stderr
);
301 map_groups__insert(thread
->mg
, map
);
306 static int __thread__prepare_access(struct thread
*thread
)
308 bool initialized
= false;
310 struct maps
*maps
= &thread
->mg
->maps
;
313 down_read(&maps
->lock
);
315 for (map
= maps__first(maps
); map
; map
= map__next(map
)) {
316 err
= unwind__prepare_access(thread
, map
, &initialized
);
317 if (err
|| initialized
)
321 up_read(&maps
->lock
);
326 static int thread__prepare_access(struct thread
*thread
)
330 if (symbol_conf
.use_callchain
)
331 err
= __thread__prepare_access(thread
);
336 static int thread__clone_map_groups(struct thread
*thread
,
337 struct thread
*parent
,
340 /* This is new thread, we share map groups for process. */
341 if (thread
->pid_
== parent
->pid_
)
342 return thread__prepare_access(thread
);
344 if (thread
->mg
== parent
->mg
) {
345 pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
346 thread
->pid_
, thread
->tid
, parent
->pid_
, parent
->tid
);
349 /* But this one is new process, copy maps. */
350 return do_maps_clone
? map_groups__clone(thread
, parent
->mg
) : 0;
353 int thread__fork(struct thread
*thread
, struct thread
*parent
, u64 timestamp
, bool do_maps_clone
)
355 if (parent
->comm_set
) {
356 const char *comm
= thread__comm_str(parent
);
360 err
= thread__set_comm(thread
, comm
, timestamp
);
365 thread
->ppid
= parent
->tid
;
366 return thread__clone_map_groups(thread
, parent
, do_maps_clone
);
369 void thread__find_cpumode_addr_location(struct thread
*thread
, u64 addr
,
370 struct addr_location
*al
)
373 const u8 cpumodes
[] = {
374 PERF_RECORD_MISC_USER
,
375 PERF_RECORD_MISC_KERNEL
,
376 PERF_RECORD_MISC_GUEST_USER
,
377 PERF_RECORD_MISC_GUEST_KERNEL
380 for (i
= 0; i
< ARRAY_SIZE(cpumodes
); i
++) {
381 thread__find_symbol(thread
, cpumodes
[i
], addr
, al
);
387 struct thread
*thread__main_thread(struct machine
*machine
, struct thread
*thread
)
389 if (thread
->pid_
== thread
->tid
)
390 return thread__get(thread
);
392 if (thread
->pid_
== -1)
395 return machine__find_thread(machine
, thread
->pid_
, thread
->pid_
);
398 int thread__memcpy(struct thread
*thread
, struct machine
*machine
,
399 void *buf
, u64 ip
, int len
, bool *is64bit
)
401 u8 cpumode
= PERF_RECORD_MISC_USER
;
402 struct addr_location al
;
405 if (machine__kernel_ip(machine
, ip
))
406 cpumode
= PERF_RECORD_MISC_KERNEL
;
408 if (!thread__find_map(thread
, cpumode
, ip
, &al
) || !al
.map
->dso
||
409 al
.map
->dso
->data
.status
== DSO_DATA_STATUS_ERROR
||
410 map__load(al
.map
) < 0)
413 offset
= al
.map
->map_ip(al
.map
, ip
);
415 *is64bit
= al
.map
->dso
->is_64_bit
;
417 return dso__data_read_offset(al
.map
->dso
, machine
, offset
, buf
, len
);