xref: /openbmc/linux/tools/perf/util/thread.c (revision 8efc4f05685dae2da1d21973eba5e59e7863c77f)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a43783aeSArnaldo Carvalho de Melo #include <errno.h>
36baa0a5aSFrederic Weisbecker #include <stdlib.h>
46baa0a5aSFrederic Weisbecker #include <stdio.h>
56baa0a5aSFrederic Weisbecker #include <string.h>
6877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
77f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
84a3cec84SArnaldo Carvalho de Melo #include "dso.h"
9b3165f41SArnaldo Carvalho de Melo #include "session.h"
106baa0a5aSFrederic Weisbecker #include "thread.h"
1100447ccdSAdrian Hunter #include "thread-stack.h"
126e086437SFrederic Weisbecker #include "debug.h"
13f3b3614aSHari Bathini #include "namespaces.h"
141902efe7SFrederic Weisbecker #include "comm.h"
1515325938SAndi Kleen #include "map.h"
16daecf9e0SArnaldo Carvalho de Melo #include "symbol.h"
1766f066d8SNamhyung Kim #include "unwind.h"
18382619c0SJiri Olsa #include "callchain.h"
196baa0a5aSFrederic Weisbecker 
202f3027acSArnaldo Carvalho de Melo #include <api/fs/fs.h>
212f3027acSArnaldo Carvalho de Melo 
22cddcef60SJiri Olsa int thread__init_map_groups(struct thread *thread, struct machine *machine)
23cddcef60SJiri Olsa {
24cddcef60SJiri Olsa 	pid_t pid = thread->pid_;
25cddcef60SJiri Olsa 
261fcb8768SAdrian Hunter 	if (pid == thread->tid || pid == -1) {
2711246c70SArnaldo Carvalho de Melo 		thread->mg = map_groups__new(machine);
28cddcef60SJiri Olsa 	} else {
2918ef15c6SArnaldo Carvalho de Melo 		struct thread *leader = __machine__findnew_thread(machine, pid, pid);
30abd82868SArnaldo Carvalho de Melo 		if (leader) {
31cddcef60SJiri Olsa 			thread->mg = map_groups__get(leader->mg);
32abd82868SArnaldo Carvalho de Melo 			thread__put(leader);
33abd82868SArnaldo Carvalho de Melo 		}
34cddcef60SJiri Olsa 	}
35cddcef60SJiri Olsa 
36cddcef60SJiri Olsa 	return thread->mg ? 0 : -1;
37cddcef60SJiri Olsa }
38cddcef60SJiri Olsa 
3999d725fcSAdrian Hunter struct thread *thread__new(pid_t pid, pid_t tid)
406baa0a5aSFrederic Weisbecker {
411902efe7SFrederic Weisbecker 	char *comm_str;
421902efe7SFrederic Weisbecker 	struct comm *comm;
43c824c433SArnaldo Carvalho de Melo 	struct thread *thread = zalloc(sizeof(*thread));
446baa0a5aSFrederic Weisbecker 
45c824c433SArnaldo Carvalho de Melo 	if (thread != NULL) {
46c824c433SArnaldo Carvalho de Melo 		thread->pid_ = pid;
47c824c433SArnaldo Carvalho de Melo 		thread->tid = tid;
48c824c433SArnaldo Carvalho de Melo 		thread->ppid = -1;
49bf49c35fSAdrian Hunter 		thread->cpu = -1;
50f3b3614aSHari Bathini 		INIT_LIST_HEAD(&thread->namespaces_list);
511902efe7SFrederic Weisbecker 		INIT_LIST_HEAD(&thread->comm_list);
52b32ee9e5SKan Liang 		init_rwsem(&thread->namespaces_lock);
53b32ee9e5SKan Liang 		init_rwsem(&thread->comm_lock);
541902efe7SFrederic Weisbecker 
551902efe7SFrederic Weisbecker 		comm_str = malloc(32);
561902efe7SFrederic Weisbecker 		if (!comm_str)
571902efe7SFrederic Weisbecker 			goto err_thread;
581902efe7SFrederic Weisbecker 
591902efe7SFrederic Weisbecker 		snprintf(comm_str, 32, ":%d", tid);
6065de51f9SAdrian Hunter 		comm = comm__new(comm_str, 0, false);
611902efe7SFrederic Weisbecker 		free(comm_str);
621902efe7SFrederic Weisbecker 		if (!comm)
631902efe7SFrederic Weisbecker 			goto err_thread;
641902efe7SFrederic Weisbecker 
651902efe7SFrederic Weisbecker 		list_add(&comm->list, &thread->comm_list);
66e34f5b11SElena Reshetova 		refcount_set(&thread->refcnt, 1);
67b91fc39fSArnaldo Carvalho de Melo 		RB_CLEAR_NODE(&thread->rb_node);
68843ff37bSKrister Johansen 		/* Thread holds first ref to nsdata. */
69843ff37bSKrister Johansen 		thread->nsinfo = nsinfo__new(pid);
70dd2e18e9SAndi Kleen 		srccode_state_init(&thread->srccode_state);
716baa0a5aSFrederic Weisbecker 	}
726baa0a5aSFrederic Weisbecker 
73c824c433SArnaldo Carvalho de Melo 	return thread;
741902efe7SFrederic Weisbecker 
751902efe7SFrederic Weisbecker err_thread:
761902efe7SFrederic Weisbecker 	free(thread);
771902efe7SFrederic Weisbecker 	return NULL;
786baa0a5aSFrederic Weisbecker }
796baa0a5aSFrederic Weisbecker 
80c824c433SArnaldo Carvalho de Melo void thread__delete(struct thread *thread)
81591765fdSArnaldo Carvalho de Melo {
82f3b3614aSHari Bathini 	struct namespaces *namespaces, *tmp_namespaces;
83f3b3614aSHari Bathini 	struct comm *comm, *tmp_comm;
841902efe7SFrederic Weisbecker 
85b91fc39fSArnaldo Carvalho de Melo 	BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
86b91fc39fSArnaldo Carvalho de Melo 
8700447ccdSAdrian Hunter 	thread_stack__free(thread);
8800447ccdSAdrian Hunter 
899608b84eSAdrian Hunter 	if (thread->mg) {
90a26ca671SArnaldo Carvalho de Melo 		map_groups__put(thread->mg);
9193d5731dSArnaldo Carvalho de Melo 		thread->mg = NULL;
929608b84eSAdrian Hunter 	}
93b32ee9e5SKan Liang 	down_write(&thread->namespaces_lock);
94f3b3614aSHari Bathini 	list_for_each_entry_safe(namespaces, tmp_namespaces,
95f3b3614aSHari Bathini 				 &thread->namespaces_list, list) {
96e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&namespaces->list);
97f3b3614aSHari Bathini 		namespaces__free(namespaces);
98f3b3614aSHari Bathini 	}
99b32ee9e5SKan Liang 	up_write(&thread->namespaces_lock);
100b32ee9e5SKan Liang 
101b32ee9e5SKan Liang 	down_write(&thread->comm_lock);
102f3b3614aSHari Bathini 	list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
103e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&comm->list);
1041902efe7SFrederic Weisbecker 		comm__free(comm);
1051902efe7SFrederic Weisbecker 	}
106b32ee9e5SKan Liang 	up_write(&thread->comm_lock);
107b32ee9e5SKan Liang 
108843ff37bSKrister Johansen 	nsinfo__zput(thread->nsinfo);
109dd2e18e9SAndi Kleen 	srccode_state_free(&thread->srccode_state);
1101902efe7SFrederic Weisbecker 
111b32ee9e5SKan Liang 	exit_rwsem(&thread->namespaces_lock);
112b32ee9e5SKan Liang 	exit_rwsem(&thread->comm_lock);
113c824c433SArnaldo Carvalho de Melo 	free(thread);
114591765fdSArnaldo Carvalho de Melo }
115591765fdSArnaldo Carvalho de Melo 
116f3b623b8SArnaldo Carvalho de Melo struct thread *thread__get(struct thread *thread)
117f3b623b8SArnaldo Carvalho de Melo {
118b91fc39fSArnaldo Carvalho de Melo 	if (thread)
119e34f5b11SElena Reshetova 		refcount_inc(&thread->refcnt);
120f3b623b8SArnaldo Carvalho de Melo 	return thread;
121f3b623b8SArnaldo Carvalho de Melo }
122f3b623b8SArnaldo Carvalho de Melo 
123f3b623b8SArnaldo Carvalho de Melo void thread__put(struct thread *thread)
124f3b623b8SArnaldo Carvalho de Melo {
125e34f5b11SElena Reshetova 	if (thread && refcount_dec_and_test(&thread->refcnt)) {
126abd82868SArnaldo Carvalho de Melo 		/*
1274c00af0eSArnaldo Carvalho de Melo 		 * Remove it from the dead threads list, as last reference is
1284c00af0eSArnaldo Carvalho de Melo 		 * gone, if it is in a dead threads list.
1294c00af0eSArnaldo Carvalho de Melo 		 *
1304c00af0eSArnaldo Carvalho de Melo 		 * We may not be there anymore if say, the machine where it was
1314c00af0eSArnaldo Carvalho de Melo 		 * stored was already deleted, so we already removed it from
1324c00af0eSArnaldo Carvalho de Melo 		 * the dead threads and some other piece of code still keeps a
1334c00af0eSArnaldo Carvalho de Melo 		 * reference.
1344c00af0eSArnaldo Carvalho de Melo 		 *
1354c00af0eSArnaldo Carvalho de Melo 		 * This is what 'perf sched' does and finally drops it in
1364c00af0eSArnaldo Carvalho de Melo 		 * perf_sched__lat(), where it calls perf_sched__read_events(),
1374c00af0eSArnaldo Carvalho de Melo 		 * that processes the events by creating a session and deleting
1384c00af0eSArnaldo Carvalho de Melo 		 * it, which ends up destroying the list heads for the dead
1394c00af0eSArnaldo Carvalho de Melo 		 * threads, but before it does that it removes all threads from
1404c00af0eSArnaldo Carvalho de Melo 		 * it using list_del_init().
1414c00af0eSArnaldo Carvalho de Melo 		 *
1424c00af0eSArnaldo Carvalho de Melo 		 * So we need to check here if it is in a dead threads list and
1434c00af0eSArnaldo Carvalho de Melo 		 * if so, remove it before finally deleting the thread, to avoid
1444c00af0eSArnaldo Carvalho de Melo 		 * an use after free situation.
145abd82868SArnaldo Carvalho de Melo 		 */
1464c00af0eSArnaldo Carvalho de Melo 		if (!list_empty(&thread->node))
147f3b623b8SArnaldo Carvalho de Melo 			list_del_init(&thread->node);
148f3b623b8SArnaldo Carvalho de Melo 		thread__delete(thread);
149f3b623b8SArnaldo Carvalho de Melo 	}
150f3b623b8SArnaldo Carvalho de Melo }
151f3b623b8SArnaldo Carvalho de Melo 
1526584140bSNamhyung Kim static struct namespaces *__thread__namespaces(const struct thread *thread)
153f3b3614aSHari Bathini {
154f3b3614aSHari Bathini 	if (list_empty(&thread->namespaces_list))
155f3b3614aSHari Bathini 		return NULL;
156f3b3614aSHari Bathini 
157f3b3614aSHari Bathini 	return list_first_entry(&thread->namespaces_list, struct namespaces, list);
158f3b3614aSHari Bathini }
159f3b3614aSHari Bathini 
1607cb10a08SNamhyung Kim struct namespaces *thread__namespaces(struct thread *thread)
1616584140bSNamhyung Kim {
1626584140bSNamhyung Kim 	struct namespaces *ns;
1636584140bSNamhyung Kim 
1647cb10a08SNamhyung Kim 	down_read(&thread->namespaces_lock);
1656584140bSNamhyung Kim 	ns = __thread__namespaces(thread);
1667cb10a08SNamhyung Kim 	up_read(&thread->namespaces_lock);
1676584140bSNamhyung Kim 
1686584140bSNamhyung Kim 	return ns;
1696584140bSNamhyung Kim }
1706584140bSNamhyung Kim 
171b32ee9e5SKan Liang static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
17269d81f09SArnaldo Carvalho de Melo 				    struct perf_record_namespaces *event)
173f3b3614aSHari Bathini {
1746584140bSNamhyung Kim 	struct namespaces *new, *curr = __thread__namespaces(thread);
175f3b3614aSHari Bathini 
176f3b3614aSHari Bathini 	new = namespaces__new(event);
177f3b3614aSHari Bathini 	if (!new)
178f3b3614aSHari Bathini 		return -ENOMEM;
179f3b3614aSHari Bathini 
180f3b3614aSHari Bathini 	list_add(&new->list, &thread->namespaces_list);
181f3b3614aSHari Bathini 
182f3b3614aSHari Bathini 	if (timestamp && curr) {
183f3b3614aSHari Bathini 		/*
184f3b3614aSHari Bathini 		 * setns syscall must have changed few or all the namespaces
185f3b3614aSHari Bathini 		 * of this thread. Update end time for the namespaces
186f3b3614aSHari Bathini 		 * previously used.
187f3b3614aSHari Bathini 		 */
188f3b3614aSHari Bathini 		curr = list_next_entry(new, list);
189f3b3614aSHari Bathini 		curr->end_time = timestamp;
190f3b3614aSHari Bathini 	}
191f3b3614aSHari Bathini 
192f3b3614aSHari Bathini 	return 0;
193f3b3614aSHari Bathini }
194f3b3614aSHari Bathini 
195b32ee9e5SKan Liang int thread__set_namespaces(struct thread *thread, u64 timestamp,
19669d81f09SArnaldo Carvalho de Melo 			   struct perf_record_namespaces *event)
197b32ee9e5SKan Liang {
198b32ee9e5SKan Liang 	int ret;
199b32ee9e5SKan Liang 
200b32ee9e5SKan Liang 	down_write(&thread->namespaces_lock);
201b32ee9e5SKan Liang 	ret = __thread__set_namespaces(thread, timestamp, event);
202b32ee9e5SKan Liang 	up_write(&thread->namespaces_lock);
203b32ee9e5SKan Liang 	return ret;
204b32ee9e5SKan Liang }
205b32ee9e5SKan Liang 
2064dfced35SNamhyung Kim struct comm *thread__comm(const struct thread *thread)
2076baa0a5aSFrederic Weisbecker {
2081902efe7SFrederic Weisbecker 	if (list_empty(&thread->comm_list))
2091902efe7SFrederic Weisbecker 		return NULL;
2104385d580SDavid S. Miller 
2111902efe7SFrederic Weisbecker 	return list_first_entry(&thread->comm_list, struct comm, list);
2124385d580SDavid S. Miller }
2131902efe7SFrederic Weisbecker 
21465de51f9SAdrian Hunter struct comm *thread__exec_comm(const struct thread *thread)
21565de51f9SAdrian Hunter {
2163de7ae0bSAdrian Hunter 	struct comm *comm, *last = NULL, *second_last = NULL;
21765de51f9SAdrian Hunter 
21865de51f9SAdrian Hunter 	list_for_each_entry(comm, &thread->comm_list, list) {
21965de51f9SAdrian Hunter 		if (comm->exec)
22065de51f9SAdrian Hunter 			return comm;
2213de7ae0bSAdrian Hunter 		second_last = last;
22265de51f9SAdrian Hunter 		last = comm;
22365de51f9SAdrian Hunter 	}
22465de51f9SAdrian Hunter 
2253de7ae0bSAdrian Hunter 	/*
2263de7ae0bSAdrian Hunter 	 * 'last' with no start time might be the parent's comm of a synthesized
2273de7ae0bSAdrian Hunter 	 * thread (created by processing a synthesized fork event). For a main
2283de7ae0bSAdrian Hunter 	 * thread, that is very probably wrong. Prefer a later comm to avoid
2293de7ae0bSAdrian Hunter 	 * that case.
2303de7ae0bSAdrian Hunter 	 */
2313de7ae0bSAdrian Hunter 	if (second_last && !last->start && thread->pid_ == thread->tid)
2323de7ae0bSAdrian Hunter 		return second_last;
2333de7ae0bSAdrian Hunter 
23465de51f9SAdrian Hunter 	return last;
23565de51f9SAdrian Hunter }
23665de51f9SAdrian Hunter 
237b32ee9e5SKan Liang static int ____thread__set_comm(struct thread *thread, const char *str,
238b32ee9e5SKan Liang 				u64 timestamp, bool exec)
2391902efe7SFrederic Weisbecker {
2401902efe7SFrederic Weisbecker 	struct comm *new, *curr = thread__comm(thread);
2411902efe7SFrederic Weisbecker 
242a8480808SAdrian Hunter 	/* Override the default :tid entry */
243a8480808SAdrian Hunter 	if (!thread->comm_set) {
24418ef15c6SArnaldo Carvalho de Melo 		int err = comm__override(curr, str, timestamp, exec);
2453178f58bSFrederic Weisbecker 		if (err)
2463178f58bSFrederic Weisbecker 			return err;
247a5285ad9SFrederic Weisbecker 	} else {
24865de51f9SAdrian Hunter 		new = comm__new(str, timestamp, exec);
2491902efe7SFrederic Weisbecker 		if (!new)
2501902efe7SFrederic Weisbecker 			return -ENOMEM;
2511902efe7SFrederic Weisbecker 		list_add(&new->list, &thread->comm_list);
252380b5143SNamhyung Kim 
253380b5143SNamhyung Kim 		if (exec)
254e8ba2906SJohn Keeping 			unwind__flush_access(thread->mg);
255a5285ad9SFrederic Weisbecker 	}
256a5285ad9SFrederic Weisbecker 
2571902efe7SFrederic Weisbecker 	thread->comm_set = true;
2581902efe7SFrederic Weisbecker 
2591902efe7SFrederic Weisbecker 	return 0;
2606baa0a5aSFrederic Weisbecker }
2616baa0a5aSFrederic Weisbecker 
262b32ee9e5SKan Liang int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
263b32ee9e5SKan Liang 		       bool exec)
264b32ee9e5SKan Liang {
265b32ee9e5SKan Liang 	int ret;
266b32ee9e5SKan Liang 
267b32ee9e5SKan Liang 	down_write(&thread->comm_lock);
268b32ee9e5SKan Liang 	ret = ____thread__set_comm(thread, str, timestamp, exec);
269b32ee9e5SKan Liang 	up_write(&thread->comm_lock);
270b32ee9e5SKan Liang 	return ret;
271b32ee9e5SKan Liang }
272b32ee9e5SKan Liang 
2732f3027acSArnaldo Carvalho de Melo int thread__set_comm_from_proc(struct thread *thread)
2742f3027acSArnaldo Carvalho de Melo {
2752f3027acSArnaldo Carvalho de Melo 	char path[64];
2762f3027acSArnaldo Carvalho de Melo 	char *comm = NULL;
2772f3027acSArnaldo Carvalho de Melo 	size_t sz;
2782f3027acSArnaldo Carvalho de Melo 	int err = -1;
2792f3027acSArnaldo Carvalho de Melo 
2802f3027acSArnaldo Carvalho de Melo 	if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
2812f3027acSArnaldo Carvalho de Melo 		       thread->pid_, thread->tid) >= (int)sizeof(path)) &&
2822f3027acSArnaldo Carvalho de Melo 	    procfs__read_str(path, &comm, &sz) == 0) {
2832f3027acSArnaldo Carvalho de Melo 		comm[sz - 1] = '\0';
2842f3027acSArnaldo Carvalho de Melo 		err = thread__set_comm(thread, comm, 0);
2852f3027acSArnaldo Carvalho de Melo 	}
2862f3027acSArnaldo Carvalho de Melo 
2872f3027acSArnaldo Carvalho de Melo 	return err;
2882f3027acSArnaldo Carvalho de Melo }
2892f3027acSArnaldo Carvalho de Melo 
290b32ee9e5SKan Liang static const char *__thread__comm_str(const struct thread *thread)
291b9c5143aSFrederic Weisbecker {
2921902efe7SFrederic Weisbecker 	const struct comm *comm = thread__comm(thread);
2931902efe7SFrederic Weisbecker 
2941902efe7SFrederic Weisbecker 	if (!comm)
2951902efe7SFrederic Weisbecker 		return NULL;
2961902efe7SFrederic Weisbecker 
2971902efe7SFrederic Weisbecker 	return comm__str(comm);
298b9c5143aSFrederic Weisbecker }
299b9c5143aSFrederic Weisbecker 
3007cb10a08SNamhyung Kim const char *thread__comm_str(struct thread *thread)
301b32ee9e5SKan Liang {
302b32ee9e5SKan Liang 	const char *str;
303b32ee9e5SKan Liang 
3047cb10a08SNamhyung Kim 	down_read(&thread->comm_lock);
305b32ee9e5SKan Liang 	str = __thread__comm_str(thread);
3067cb10a08SNamhyung Kim 	up_read(&thread->comm_lock);
307b32ee9e5SKan Liang 
308b32ee9e5SKan Liang 	return str;
309b32ee9e5SKan Liang }
310b32ee9e5SKan Liang 
3111902efe7SFrederic Weisbecker /* CHECKME: it should probably better return the max comm len from its comm list */
312c824c433SArnaldo Carvalho de Melo int thread__comm_len(struct thread *thread)
313a4fb581bSFrederic Weisbecker {
314c824c433SArnaldo Carvalho de Melo 	if (!thread->comm_len) {
3151902efe7SFrederic Weisbecker 		const char *comm = thread__comm_str(thread);
3161902efe7SFrederic Weisbecker 		if (!comm)
317a4fb581bSFrederic Weisbecker 			return 0;
3181902efe7SFrederic Weisbecker 		thread->comm_len = strlen(comm);
319a4fb581bSFrederic Weisbecker 	}
320a4fb581bSFrederic Weisbecker 
321c824c433SArnaldo Carvalho de Melo 	return thread->comm_len;
322a4fb581bSFrederic Weisbecker }
323a4fb581bSFrederic Weisbecker 
3243f067dcaSArnaldo Carvalho de Melo size_t thread__fprintf(struct thread *thread, FILE *fp)
32595011c60SArnaldo Carvalho de Melo {
326b9c5143aSFrederic Weisbecker 	return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
327acebd408SJiri Olsa 	       map_groups__fprintf(thread->mg, fp);
3286baa0a5aSFrederic Weisbecker }
3296baa0a5aSFrederic Weisbecker 
3308132a2a8SHe Kuang int thread__insert_map(struct thread *thread, struct map *map)
3311b46cddfSArnaldo Carvalho de Melo {
3328132a2a8SHe Kuang 	int ret;
3338132a2a8SHe Kuang 
334e8ba2906SJohn Keeping 	ret = unwind__prepare_access(thread->mg, map, NULL);
3358132a2a8SHe Kuang 	if (ret)
3368132a2a8SHe Kuang 		return ret;
3378132a2a8SHe Kuang 
338acebd408SJiri Olsa 	map_groups__fixup_overlappings(thread->mg, map, stderr);
33993d5731dSArnaldo Carvalho de Melo 	map_groups__insert(thread->mg, map);
3408132a2a8SHe Kuang 
3418132a2a8SHe Kuang 	return 0;
34295011c60SArnaldo Carvalho de Melo }
34395011c60SArnaldo Carvalho de Melo 
3446c502584SJiri Olsa static int __thread__prepare_access(struct thread *thread)
3456c502584SJiri Olsa {
3466c502584SJiri Olsa 	bool initialized = false;
3473183f8caSArnaldo Carvalho de Melo 	int err = 0;
3483183f8caSArnaldo Carvalho de Melo 	struct maps *maps = &thread->mg->maps;
3496c502584SJiri Olsa 	struct map *map;
3506c502584SJiri Olsa 
3510a7c74eaSArnaldo Carvalho de Melo 	down_read(&maps->lock);
3526c502584SJiri Olsa 
353*8efc4f05SArnaldo Carvalho de Melo 	maps__for_each_entry(maps, map) {
354e8ba2906SJohn Keeping 		err = unwind__prepare_access(thread->mg, map, &initialized);
3556c502584SJiri Olsa 		if (err || initialized)
3566c502584SJiri Olsa 			break;
3576c502584SJiri Olsa 	}
3586c502584SJiri Olsa 
3590a7c74eaSArnaldo Carvalho de Melo 	up_read(&maps->lock);
3606c502584SJiri Olsa 
3616c502584SJiri Olsa 	return err;
3626c502584SJiri Olsa }
3636c502584SJiri Olsa 
3646c502584SJiri Olsa static int thread__prepare_access(struct thread *thread)
3656c502584SJiri Olsa {
3666c502584SJiri Olsa 	int err = 0;
3676c502584SJiri Olsa 
368382619c0SJiri Olsa 	if (dwarf_callchain_users)
3696c502584SJiri Olsa 		err = __thread__prepare_access(thread);
3706c502584SJiri Olsa 
3716c502584SJiri Olsa 	return err;
3726c502584SJiri Olsa }
3736c502584SJiri Olsa 
374cddcef60SJiri Olsa static int thread__clone_map_groups(struct thread *thread,
3754f8f382eSDavid Miller 				    struct thread *parent,
3764f8f382eSDavid Miller 				    bool do_maps_clone)
377cddcef60SJiri Olsa {
378cddcef60SJiri Olsa 	/* This is new thread, we share map groups for process. */
379cddcef60SJiri Olsa 	if (thread->pid_ == parent->pid_)
3806c502584SJiri Olsa 		return thread__prepare_access(thread);
381cddcef60SJiri Olsa 
3820d7e7accSAdrian Hunter 	if (thread->mg == parent->mg) {
3830d7e7accSAdrian Hunter 		pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
3840d7e7accSAdrian Hunter 			 thread->pid_, thread->tid, parent->pid_, parent->tid);
3850d7e7accSAdrian Hunter 		return 0;
3860d7e7accSAdrian Hunter 	}
387cddcef60SJiri Olsa 	/* But this one is new process, copy maps. */
3884f8f382eSDavid Miller 	return do_maps_clone ? map_groups__clone(thread, parent->mg) : 0;
389cddcef60SJiri Olsa }
390cddcef60SJiri Olsa 
3914f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
3926baa0a5aSFrederic Weisbecker {
393faa5c5c3SArnaldo Carvalho de Melo 	if (parent->comm_set) {
3941902efe7SFrederic Weisbecker 		const char *comm = thread__comm_str(parent);
39518ef15c6SArnaldo Carvalho de Melo 		int err;
3961902efe7SFrederic Weisbecker 		if (!comm)
3976baa0a5aSFrederic Weisbecker 			return -ENOMEM;
3981902efe7SFrederic Weisbecker 		err = thread__set_comm(thread, comm, timestamp);
3998d00be81SDavid Ahern 		if (err)
4001902efe7SFrederic Weisbecker 			return err;
401faa5c5c3SArnaldo Carvalho de Melo 	}
4026baa0a5aSFrederic Weisbecker 
403c824c433SArnaldo Carvalho de Melo 	thread->ppid = parent->tid;
4044f8f382eSDavid Miller 	return thread__clone_map_groups(thread, parent, do_maps_clone);
4056baa0a5aSFrederic Weisbecker }
40652a3cb8cSArnaldo Carvalho de Melo 
40726bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
40852a3cb8cSArnaldo Carvalho de Melo 					struct addr_location *al)
40952a3cb8cSArnaldo Carvalho de Melo {
41052a3cb8cSArnaldo Carvalho de Melo 	size_t i;
4113b556bceSEric Engestrom 	const u8 cpumodes[] = {
41252a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_USER,
41352a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_KERNEL,
41452a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_USER,
41552a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_KERNEL
41652a3cb8cSArnaldo Carvalho de Melo 	};
41752a3cb8cSArnaldo Carvalho de Melo 
41852a3cb8cSArnaldo Carvalho de Melo 	for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
41926bd9331SArnaldo Carvalho de Melo 		thread__find_symbol(thread, cpumodes[i], addr, al);
42052a3cb8cSArnaldo Carvalho de Melo 		if (al->map)
42152a3cb8cSArnaldo Carvalho de Melo 			break;
42252a3cb8cSArnaldo Carvalho de Melo 	}
42352a3cb8cSArnaldo Carvalho de Melo }
424480ca357SAndi Kleen 
425480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
426480ca357SAndi Kleen {
427480ca357SAndi Kleen 	if (thread->pid_ == thread->tid)
428480ca357SAndi Kleen 		return thread__get(thread);
429480ca357SAndi Kleen 
430480ca357SAndi Kleen 	if (thread->pid_ == -1)
431480ca357SAndi Kleen 		return NULL;
432480ca357SAndi Kleen 
433480ca357SAndi Kleen 	return machine__find_thread(machine, thread->pid_, thread->pid_);
434480ca357SAndi Kleen }
43515325938SAndi Kleen 
43615325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine,
43715325938SAndi Kleen 		   void *buf, u64 ip, int len, bool *is64bit)
43815325938SAndi Kleen {
43915325938SAndi Kleen        u8 cpumode = PERF_RECORD_MISC_USER;
44015325938SAndi Kleen        struct addr_location al;
44115325938SAndi Kleen        long offset;
44215325938SAndi Kleen 
44315325938SAndi Kleen        if (machine__kernel_ip(machine, ip))
44415325938SAndi Kleen                cpumode = PERF_RECORD_MISC_KERNEL;
44515325938SAndi Kleen 
44615325938SAndi Kleen        if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso ||
44715325938SAndi Kleen 	   al.map->dso->data.status == DSO_DATA_STATUS_ERROR ||
44815325938SAndi Kleen 	   map__load(al.map) < 0)
44915325938SAndi Kleen                return -1;
45015325938SAndi Kleen 
45115325938SAndi Kleen        offset = al.map->map_ip(al.map, ip);
45215325938SAndi Kleen        if (is64bit)
45315325938SAndi Kleen                *is64bit = al.map->dso->is_64_bit;
45415325938SAndi Kleen 
45515325938SAndi Kleen        return dso__data_read_offset(al.map->dso, machine, offset, buf, len);
45615325938SAndi Kleen }
457