6 #include <linux/kernel.h>
9 #include "thread-stack.h"
12 #include "namespaces.h"
16 #include <api/fs/fs.h>
18 int thread__init_map_groups(struct thread *thread, struct machine *machine)
20 pid_t pid = thread->pid_;
22 if (pid == thread->tid || pid == -1) {
23 thread->mg = map_groups__new(machine);
25 struct thread *leader = __machine__findnew_thread(machine, pid, pid);
27 thread->mg = map_groups__get(leader->mg);
32 return thread->mg ? 0 : -1;
35 struct thread *thread__new(pid_t pid, pid_t tid)
39 struct thread *thread = zalloc(sizeof(*thread));
46 INIT_LIST_HEAD(&thread->namespaces_list);
47 INIT_LIST_HEAD(&thread->comm_list);
48 init_rwsem(&thread->namespaces_lock);
49 init_rwsem(&thread->comm_lock);
51 comm_str = malloc(32);
55 snprintf(comm_str, 32, ":%d", tid);
56 comm = comm__new(comm_str, 0, false);
61 list_add(&comm->list, &thread->comm_list);
62 refcount_set(&thread->refcnt, 1);
63 RB_CLEAR_NODE(&thread->rb_node);
64 /* Thread holds first ref to nsdata. */
65 thread->nsinfo = nsinfo__new(pid);
75 void thread__delete(struct thread *thread)
77 struct namespaces *namespaces, *tmp_namespaces;
78 struct comm *comm, *tmp_comm;
80 BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
82 thread_stack__free(thread);
85 map_groups__put(thread->mg);
88 down_write(&thread->namespaces_lock);
89 list_for_each_entry_safe(namespaces, tmp_namespaces,
90 &thread->namespaces_list, list) {
91 list_del(&namespaces->list);
92 namespaces__free(namespaces);
94 up_write(&thread->namespaces_lock);
96 down_write(&thread->comm_lock);
97 list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
98 list_del(&comm->list);
101 up_write(&thread->comm_lock);
103 unwind__finish_access(thread);
104 nsinfo__zput(thread->nsinfo);
106 exit_rwsem(&thread->namespaces_lock);
107 exit_rwsem(&thread->comm_lock);
111 struct thread *thread__get(struct thread *thread)
114 refcount_inc(&thread->refcnt);
118 void thread__put(struct thread *thread)
120 if (thread && refcount_dec_and_test(&thread->refcnt)) {
122 * Remove it from the dead_threads list, as last reference
125 list_del_init(&thread->node);
126 thread__delete(thread);
130 struct namespaces *thread__namespaces(const struct thread *thread)
132 if (list_empty(&thread->namespaces_list))
135 return list_first_entry(&thread->namespaces_list, struct namespaces, list);
138 static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
139 struct namespaces_event *event)
141 struct namespaces *new, *curr = thread__namespaces(thread);
143 new = namespaces__new(event);
147 list_add(&new->list, &thread->namespaces_list);
149 if (timestamp && curr) {
151 * setns syscall must have changed few or all the namespaces
152 * of this thread. Update end time for the namespaces
155 curr = list_next_entry(new, list);
156 curr->end_time = timestamp;
162 int thread__set_namespaces(struct thread *thread, u64 timestamp,
163 struct namespaces_event *event)
167 down_write(&thread->namespaces_lock);
168 ret = __thread__set_namespaces(thread, timestamp, event);
169 up_write(&thread->namespaces_lock);
173 struct comm *thread__comm(const struct thread *thread)
175 if (list_empty(&thread->comm_list))
178 return list_first_entry(&thread->comm_list, struct comm, list);
181 struct comm *thread__exec_comm(const struct thread *thread)
183 struct comm *comm, *last = NULL;
185 list_for_each_entry(comm, &thread->comm_list, list) {
194 static int ____thread__set_comm(struct thread *thread, const char *str,
195 u64 timestamp, bool exec)
197 struct comm *new, *curr = thread__comm(thread);
199 /* Override the default :tid entry */
200 if (!thread->comm_set) {
201 int err = comm__override(curr, str, timestamp, exec);
205 new = comm__new(str, timestamp, exec);
208 list_add(&new->list, &thread->comm_list);
211 unwind__flush_access(thread);
214 thread->comm_set = true;
219 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
224 down_write(&thread->comm_lock);
225 ret = ____thread__set_comm(thread, str, timestamp, exec);
226 up_write(&thread->comm_lock);
230 int thread__set_comm_from_proc(struct thread *thread)
237 if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
238 thread->pid_, thread->tid) >= (int)sizeof(path)) &&
239 procfs__read_str(path, &comm, &sz) == 0) {
241 err = thread__set_comm(thread, comm, 0);
247 static const char *__thread__comm_str(const struct thread *thread)
249 const struct comm *comm = thread__comm(thread);
254 return comm__str(comm);
257 const char *thread__comm_str(const struct thread *thread)
261 down_read((struct rw_semaphore *)&thread->comm_lock);
262 str = __thread__comm_str(thread);
263 up_read((struct rw_semaphore *)&thread->comm_lock);
268 /* CHECKME: it should probably better return the max comm len from its comm list */
269 int thread__comm_len(struct thread *thread)
271 if (!thread->comm_len) {
272 const char *comm = thread__comm_str(thread);
275 thread->comm_len = strlen(comm);
278 return thread->comm_len;
281 size_t thread__fprintf(struct thread *thread, FILE *fp)
283 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
284 map_groups__fprintf(thread->mg, fp);
287 int thread__insert_map(struct thread *thread, struct map *map)
291 ret = unwind__prepare_access(thread, map, NULL);
295 map_groups__fixup_overlappings(thread->mg, map, stderr);
296 map_groups__insert(thread->mg, map);
301 static int __thread__prepare_access(struct thread *thread)
303 bool initialized = false;
306 for (i = 0; i < MAP__NR_TYPES; ++i) {
307 struct maps *maps = &thread->mg->maps[i];
310 down_read(&maps->lock);
312 for (map = maps__first(maps); map; map = map__next(map)) {
313 err = unwind__prepare_access(thread, map, &initialized);
314 if (err || initialized)
318 up_read(&maps->lock);
324 static int thread__prepare_access(struct thread *thread)
328 if (symbol_conf.use_callchain)
329 err = __thread__prepare_access(thread);
334 static int thread__clone_map_groups(struct thread *thread,
335 struct thread *parent)
339 /* This is new thread, we share map groups for process. */
340 if (thread->pid_ == parent->pid_)
341 return thread__prepare_access(thread);
343 if (thread->mg == parent->mg) {
344 pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
345 thread->pid_, thread->tid, parent->pid_, parent->tid);
349 /* But this one is new process, copy maps. */
350 for (i = 0; i < MAP__NR_TYPES; ++i)
351 if (map_groups__clone(thread, parent->mg, i) < 0)
357 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
359 if (parent->comm_set) {
360 const char *comm = thread__comm_str(parent);
364 err = thread__set_comm(thread, comm, timestamp);
369 thread->ppid = parent->tid;
370 return thread__clone_map_groups(thread, parent);
373 void thread__find_cpumode_addr_location(struct thread *thread,
374 enum map_type type, u64 addr,
375 struct addr_location *al)
378 const u8 cpumodes[] = {
379 PERF_RECORD_MISC_USER,
380 PERF_RECORD_MISC_KERNEL,
381 PERF_RECORD_MISC_GUEST_USER,
382 PERF_RECORD_MISC_GUEST_KERNEL
385 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
386 thread__find_addr_location(thread, cpumodes[i], type, addr, al);
392 struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
394 if (thread->pid_ == thread->tid)
395 return thread__get(thread);
397 if (thread->pid_ == -1)
400 return machine__find_thread(machine, thread->pid_, thread->pid_);