Linux 5.1.15
[linux/fpc-iii.git] / tools / perf / util / thread.c
blobb800752745af2ff20b4ed42df2f1e831516ee174
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 "map.h"
16 #include "symbol.h"
17 #include "unwind.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);
27 } else {
28 struct thread *leader = __machine__findnew_thread(machine, pid, pid);
29 if (leader) {
30 thread->mg = map_groups__get(leader->mg);
31 thread__put(leader);
35 return thread->mg ? 0 : -1;
38 struct thread *thread__new(pid_t pid, pid_t tid)
40 char *comm_str;
41 struct comm *comm;
42 struct thread *thread = zalloc(sizeof(*thread));
44 if (thread != NULL) {
45 thread->pid_ = pid;
46 thread->tid = tid;
47 thread->ppid = -1;
48 thread->cpu = -1;
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);
55 if (!comm_str)
56 goto err_thread;
58 snprintf(comm_str, 32, ":%d", tid);
59 comm = comm__new(comm_str, 0, false);
60 free(comm_str);
61 if (!comm)
62 goto err_thread;
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);
72 return thread;
74 err_thread:
75 free(thread);
76 return NULL;
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);
88 if (thread->mg) {
89 map_groups__put(thread->mg);
90 thread->mg = NULL;
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);
103 comm__free(comm);
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);
113 free(thread);
116 struct thread *thread__get(struct thread *thread)
118 if (thread)
119 refcount_inc(&thread->refcnt);
120 return thread;
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
128 * is gone.
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))
138 return NULL;
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);
151 return ns;
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);
160 if (!new)
161 return -ENOMEM;
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
169 * previously used.
171 curr = list_next_entry(new, list);
172 curr->end_time = timestamp;
175 return 0;
178 int thread__set_namespaces(struct thread *thread, u64 timestamp,
179 struct namespaces_event *event)
181 int ret;
183 down_write(&thread->namespaces_lock);
184 ret = __thread__set_namespaces(thread, timestamp, event);
185 up_write(&thread->namespaces_lock);
186 return ret;
189 struct comm *thread__comm(const struct thread *thread)
191 if (list_empty(&thread->comm_list))
192 return NULL;
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) {
202 if (comm->exec)
203 return comm;
204 last = comm;
207 return last;
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);
218 if (err)
219 return err;
220 } else {
221 new = comm__new(str, timestamp, exec);
222 if (!new)
223 return -ENOMEM;
224 list_add(&new->list, &thread->comm_list);
226 if (exec)
227 unwind__flush_access(thread);
230 thread->comm_set = true;
232 return 0;
235 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
236 bool exec)
238 int ret;
240 down_write(&thread->comm_lock);
241 ret = ____thread__set_comm(thread, str, timestamp, exec);
242 up_write(&thread->comm_lock);
243 return ret;
246 int thread__set_comm_from_proc(struct thread *thread)
248 char path[64];
249 char *comm = NULL;
250 size_t sz;
251 int err = -1;
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) {
256 comm[sz - 1] = '\0';
257 err = thread__set_comm(thread, comm, 0);
260 return err;
263 static const char *__thread__comm_str(const struct thread *thread)
265 const struct comm *comm = thread__comm(thread);
267 if (!comm)
268 return NULL;
270 return comm__str(comm);
273 const char *thread__comm_str(const struct thread *thread)
275 const char *str;
277 down_read((struct rw_semaphore *)&thread->comm_lock);
278 str = __thread__comm_str(thread);
279 up_read((struct rw_semaphore *)&thread->comm_lock);
281 return str;
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);
289 if (!comm)
290 return 0;
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)
305 int ret;
307 ret = unwind__prepare_access(thread, map, NULL);
308 if (ret)
309 return ret;
311 map_groups__fixup_overlappings(thread->mg, map, stderr);
312 map_groups__insert(thread->mg, map);
314 return 0;
317 static int __thread__prepare_access(struct thread *thread)
319 bool initialized = false;
320 int err = 0;
321 struct maps *maps = &thread->mg->maps;
322 struct map *map;
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)
329 break;
332 up_read(&maps->lock);
334 return err;
337 static int thread__prepare_access(struct thread *thread)
339 int err = 0;
341 if (symbol_conf.use_callchain)
342 err = __thread__prepare_access(thread);
344 return err;
347 static int thread__clone_map_groups(struct thread *thread,
348 struct thread *parent,
349 bool do_maps_clone)
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);
358 return 0;
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);
368 int err;
369 if (!comm)
370 return -ENOMEM;
371 err = thread__set_comm(thread, comm, timestamp);
372 if (err)
373 return err;
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)
383 size_t i;
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);
393 if (al->map)
394 break;
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)
404 return NULL;
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;
414 long offset;
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)
422 return -1;
424 offset = al.map->map_ip(al.map, ip);
425 if (is64bit)
426 *is64bit = al.map->dso->is_64_bit;
428 return dso__data_read_offset(al.map->dso, machine, offset, buf, len);