blob: 378c418ca0c173ddcc83425d590c32d3d71d2a39 [file] [log] [blame]
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001#include "../perf.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02003#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03006#include <linux/kernel.h>
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02007#include "session.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02008#include "thread.h"
Adrian Hunter00447cc2014-10-30 16:09:42 +02009#include "thread-stack.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020010#include "util.h"
Frederic Weisbecker6e086432009-08-18 17:04:03 +020011#include "debug.h"
Hari Bathinif3b36142017-03-08 02:11:43 +053012#include "namespaces.h"
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020013#include "comm.h"
Namhyung Kim66f066d82014-10-06 09:46:00 +090014#include "unwind.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020015
Arnaldo Carvalho de Melo2f3027a2016-04-26 12:32:50 -030016#include <api/fs/fs.h>
17
Jiri Olsacddcef62014-04-09 20:54:29 +020018int thread__init_map_groups(struct thread *thread, struct machine *machine)
19{
Jiri Olsacddcef62014-04-09 20:54:29 +020020 pid_t pid = thread->pid_;
21
Adrian Hunter1fcb8762014-07-14 13:02:25 +030022 if (pid == thread->tid || pid == -1) {
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030023 thread->mg = map_groups__new(machine);
Jiri Olsacddcef62014-04-09 20:54:29 +020024 } else {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -030025 struct thread *leader = __machine__findnew_thread(machine, pid, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -030026 if (leader) {
Jiri Olsacddcef62014-04-09 20:54:29 +020027 thread->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -030028 thread__put(leader);
29 }
Jiri Olsacddcef62014-04-09 20:54:29 +020030 }
31
32 return thread->mg ? 0 : -1;
33}
34
Adrian Hunter99d725f2013-08-26 16:00:19 +030035struct thread *thread__new(pid_t pid, pid_t tid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020036{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020037 char *comm_str;
38 struct comm *comm;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030039 struct thread *thread = zalloc(sizeof(*thread));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020040
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030041 if (thread != NULL) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030042 thread->pid_ = pid;
43 thread->tid = tid;
44 thread->ppid = -1;
Adrian Hunterbf49c352014-07-22 16:17:24 +030045 thread->cpu = -1;
Hari Bathinif3b36142017-03-08 02:11:43 +053046 INIT_LIST_HEAD(&thread->namespaces_list);
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020047 INIT_LIST_HEAD(&thread->comm_list);
48
49 comm_str = malloc(32);
50 if (!comm_str)
51 goto err_thread;
52
53 snprintf(comm_str, 32, ":%d", tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +030054 comm = comm__new(comm_str, 0, false);
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020055 free(comm_str);
56 if (!comm)
57 goto err_thread;
58
59 list_add(&comm->list, &thread->comm_list);
Elena Reshetovae34f5b12017-02-21 17:35:02 +020060 refcount_set(&thread->refcnt, 1);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030061 RB_CLEAR_NODE(&thread->rb_node);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020062 }
63
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030064 return thread;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020065
66err_thread:
67 free(thread);
68 return NULL;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020069}
70
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030071void thread__delete(struct thread *thread)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030072{
Hari Bathinif3b36142017-03-08 02:11:43 +053073 struct namespaces *namespaces, *tmp_namespaces;
74 struct comm *comm, *tmp_comm;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020075
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030076 BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030077
Adrian Hunter00447cc2014-10-30 16:09:42 +020078 thread_stack__free(thread);
79
Adrian Hunter9608b842014-07-16 10:19:43 +030080 if (thread->mg) {
81 map_groups__put(thread->mg);
82 thread->mg = NULL;
83 }
Hari Bathinif3b36142017-03-08 02:11:43 +053084 list_for_each_entry_safe(namespaces, tmp_namespaces,
85 &thread->namespaces_list, list) {
86 list_del(&namespaces->list);
87 namespaces__free(namespaces);
88 }
89 list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020090 list_del(&comm->list);
91 comm__free(comm);
92 }
Namhyung Kim66f066d82014-10-06 09:46:00 +090093 unwind__finish_access(thread);
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020094
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030095 free(thread);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030096}
97
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -030098struct thread *thread__get(struct thread *thread)
99{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300100 if (thread)
Elena Reshetovae34f5b12017-02-21 17:35:02 +0200101 refcount_inc(&thread->refcnt);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300102 return thread;
103}
104
105void thread__put(struct thread *thread)
106{
Elena Reshetovae34f5b12017-02-21 17:35:02 +0200107 if (thread && refcount_dec_and_test(&thread->refcnt)) {
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300108 /*
109 * Remove it from the dead_threads list, as last reference
110 * is gone.
111 */
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300112 list_del_init(&thread->node);
113 thread__delete(thread);
114 }
115}
116
Hari Bathinif3b36142017-03-08 02:11:43 +0530117struct namespaces *thread__namespaces(const struct thread *thread)
118{
119 if (list_empty(&thread->namespaces_list))
120 return NULL;
121
122 return list_first_entry(&thread->namespaces_list, struct namespaces, list);
123}
124
125int thread__set_namespaces(struct thread *thread, u64 timestamp,
126 struct namespaces_event *event)
127{
128 struct namespaces *new, *curr = thread__namespaces(thread);
129
130 new = namespaces__new(event);
131 if (!new)
132 return -ENOMEM;
133
134 list_add(&new->list, &thread->namespaces_list);
135
136 if (timestamp && curr) {
137 /*
138 * setns syscall must have changed few or all the namespaces
139 * of this thread. Update end time for the namespaces
140 * previously used.
141 */
142 curr = list_next_entry(new, list);
143 curr->end_time = timestamp;
144 }
145
146 return 0;
147}
148
Namhyung Kim4dfced32013-09-13 16:28:57 +0900149struct comm *thread__comm(const struct thread *thread)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200150{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200151 if (list_empty(&thread->comm_list))
152 return NULL;
David S. Miller4385d582010-02-26 12:08:34 -0300153
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200154 return list_first_entry(&thread->comm_list, struct comm, list);
155}
156
Adrian Hunter65de51f2014-07-31 09:00:44 +0300157struct comm *thread__exec_comm(const struct thread *thread)
158{
159 struct comm *comm, *last = NULL;
160
161 list_for_each_entry(comm, &thread->comm_list, list) {
162 if (comm->exec)
163 return comm;
164 last = comm;
165 }
166
167 return last;
168}
169
Adrian Hunter65de51f2014-07-31 09:00:44 +0300170int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
171 bool exec)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200172{
173 struct comm *new, *curr = thread__comm(thread);
174
Adrian Huntera8480802014-11-11 16:16:41 +0200175 /* Override the default :tid entry */
176 if (!thread->comm_set) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -0300177 int err = comm__override(curr, str, timestamp, exec);
Frederic Weisbecker3178f582014-01-14 16:37:14 +0100178 if (err)
179 return err;
Frederic Weisbeckera5285ad2013-11-16 02:02:09 +0100180 } else {
Adrian Hunter65de51f2014-07-31 09:00:44 +0300181 new = comm__new(str, timestamp, exec);
Frederic Weisbeckera5285ad2013-11-16 02:02:09 +0100182 if (!new)
183 return -ENOMEM;
184 list_add(&new->list, &thread->comm_list);
Namhyung Kim380b5142014-10-06 09:46:01 +0900185
186 if (exec)
187 unwind__flush_access(thread);
David S. Miller4385d582010-02-26 12:08:34 -0300188 }
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200189
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200190 thread->comm_set = true;
191
192 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200193}
194
Arnaldo Carvalho de Melo2f3027a2016-04-26 12:32:50 -0300195int thread__set_comm_from_proc(struct thread *thread)
196{
197 char path[64];
198 char *comm = NULL;
199 size_t sz;
200 int err = -1;
201
202 if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
203 thread->pid_, thread->tid) >= (int)sizeof(path)) &&
204 procfs__read_str(path, &comm, &sz) == 0) {
205 comm[sz - 1] = '\0';
206 err = thread__set_comm(thread, comm, 0);
207 }
208
209 return err;
210}
211
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200212const char *thread__comm_str(const struct thread *thread)
213{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200214 const struct comm *comm = thread__comm(thread);
215
216 if (!comm)
217 return NULL;
218
219 return comm__str(comm);
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200220}
221
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200222/* CHECKME: it should probably better return the max comm len from its comm list */
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300223int thread__comm_len(struct thread *thread)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200224{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300225 if (!thread->comm_len) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200226 const char *comm = thread__comm_str(thread);
227 if (!comm)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200228 return 0;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200229 thread->comm_len = strlen(comm);
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200230 }
231
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300232 return thread->comm_len;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200233}
234
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300235size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200236{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200237 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
Jiri Olsaacebd402014-07-14 23:46:47 +0200238 map_groups__fprintf(thread->mg, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200239}
240
He Kuang8132a2a2016-06-03 03:33:13 +0000241int thread__insert_map(struct thread *thread, struct map *map)
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300242{
He Kuang8132a2a2016-06-03 03:33:13 +0000243 int ret;
244
Jiri Olsaa2873322016-07-04 14:16:22 +0200245 ret = unwind__prepare_access(thread, map, NULL);
He Kuang8132a2a2016-06-03 03:33:13 +0000246 if (ret)
247 return ret;
248
Jiri Olsaacebd402014-07-14 23:46:47 +0200249 map_groups__fixup_overlappings(thread->mg, map, stderr);
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300250 map_groups__insert(thread->mg, map);
He Kuang8132a2a2016-06-03 03:33:13 +0000251
252 return 0;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200253}
254
Jiri Olsa6c502582016-07-04 14:16:23 +0200255static int __thread__prepare_access(struct thread *thread)
256{
257 bool initialized = false;
258 int i, err = 0;
259
260 for (i = 0; i < MAP__NR_TYPES; ++i) {
261 struct maps *maps = &thread->mg->maps[i];
262 struct map *map;
263
264 pthread_rwlock_rdlock(&maps->lock);
265
266 for (map = maps__first(maps); map; map = map__next(map)) {
267 err = unwind__prepare_access(thread, map, &initialized);
268 if (err || initialized)
269 break;
270 }
271
272 pthread_rwlock_unlock(&maps->lock);
273 }
274
275 return err;
276}
277
278static int thread__prepare_access(struct thread *thread)
279{
280 int err = 0;
281
282 if (symbol_conf.use_callchain)
283 err = __thread__prepare_access(thread);
284
285 return err;
286}
287
Jiri Olsacddcef62014-04-09 20:54:29 +0200288static int thread__clone_map_groups(struct thread *thread,
289 struct thread *parent)
290{
291 int i;
292
293 /* This is new thread, we share map groups for process. */
294 if (thread->pid_ == parent->pid_)
Jiri Olsa6c502582016-07-04 14:16:23 +0200295 return thread__prepare_access(thread);
Jiri Olsacddcef62014-04-09 20:54:29 +0200296
Adrian Hunter0d7e7ac2015-08-19 17:29:19 +0300297 if (thread->mg == parent->mg) {
298 pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
299 thread->pid_, thread->tid, parent->pid_, parent->tid);
300 return 0;
301 }
302
Jiri Olsacddcef62014-04-09 20:54:29 +0200303 /* But this one is new process, copy maps. */
304 for (i = 0; i < MAP__NR_TYPES; ++i)
Jiri Olsa6c502582016-07-04 14:16:23 +0200305 if (map_groups__clone(thread, parent->mg, i) < 0)
Jiri Olsacddcef62014-04-09 20:54:29 +0200306 return -ENOMEM;
307
308 return 0;
309}
310
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200311int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200312{
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200313 if (parent->comm_set) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200314 const char *comm = thread__comm_str(parent);
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -0300315 int err;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200316 if (!comm)
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200317 return -ENOMEM;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200318 err = thread__set_comm(thread, comm, timestamp);
David Ahern8d00be82013-12-10 21:35:38 -0700319 if (err)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200320 return err;
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200321 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200322
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300323 thread->ppid = parent->tid;
Jiri Olsacddcef62014-04-09 20:54:29 +0200324 return thread__clone_map_groups(thread, parent);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200325}
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300326
327void thread__find_cpumode_addr_location(struct thread *thread,
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300328 enum map_type type, u64 addr,
329 struct addr_location *al)
330{
331 size_t i;
Eric Engestrom3b556bc2016-04-25 10:47:54 +0100332 const u8 cpumodes[] = {
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300333 PERF_RECORD_MISC_USER,
334 PERF_RECORD_MISC_KERNEL,
335 PERF_RECORD_MISC_GUEST_USER,
336 PERF_RECORD_MISC_GUEST_KERNEL
337 };
338
339 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300340 thread__find_addr_location(thread, cpumodes[i], type, addr, al);
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300341 if (al->map)
342 break;
343 }
344}
Andi Kleen480ca352016-05-23 17:52:24 -0700345
346struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
347{
348 if (thread->pid_ == thread->tid)
349 return thread__get(thread);
350
351 if (thread->pid_ == -1)
352 return NULL;
353
354 return machine__find_thread(machine, thread->pid_, thread->pid_);
355}