xref: /openbmc/linux/tools/perf/util/thread.c (revision 04cb4fc4d40a5bf1f9d116bc77e69791451f9fcc)
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 
2279b6bb73SArnaldo Carvalho de Melo int thread__init_maps(struct thread *thread, struct machine *machine)
23cddcef60SJiri Olsa {
24ee84a303SIan Rogers 	pid_t pid = thread__pid(thread);
25cddcef60SJiri Olsa 
26ee84a303SIan Rogers 	if (pid == thread__tid(thread) || pid == -1) {
27ee84a303SIan Rogers 		thread__set_maps(thread, maps__new(machine));
28cddcef60SJiri Olsa 	} else {
2918ef15c6SArnaldo Carvalho de Melo 		struct thread *leader = __machine__findnew_thread(machine, pid, pid);
30ee84a303SIan Rogers 
31abd82868SArnaldo Carvalho de Melo 		if (leader) {
32ee84a303SIan Rogers 			thread__set_maps(thread, maps__get(thread__maps(leader)));
33abd82868SArnaldo Carvalho de Melo 			thread__put(leader);
34abd82868SArnaldo Carvalho de Melo 		}
35cddcef60SJiri Olsa 	}
36cddcef60SJiri Olsa 
37ee84a303SIan Rogers 	return thread__maps(thread) ? 0 : -1;
38cddcef60SJiri Olsa }
39cddcef60SJiri Olsa 
4099d725fcSAdrian Hunter struct thread *thread__new(pid_t pid, pid_t tid)
416baa0a5aSFrederic Weisbecker {
421902efe7SFrederic Weisbecker 	char *comm_str;
431902efe7SFrederic Weisbecker 	struct comm *comm;
44f6005cafSIan Rogers 	RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread));
45f6005cafSIan Rogers 	struct thread *thread;
466baa0a5aSFrederic Weisbecker 
47f6005cafSIan Rogers 	if (ADD_RC_CHK(thread, _thread) != NULL) {
48ee84a303SIan Rogers 		thread__set_pid(thread, pid);
49ee84a303SIan Rogers 		thread__set_tid(thread, tid);
50ee84a303SIan Rogers 		thread__set_ppid(thread, -1);
51ee84a303SIan Rogers 		thread__set_cpu(thread, -1);
52ee84a303SIan Rogers 		thread__set_guest_cpu(thread, -1);
53ee84a303SIan Rogers 		thread__set_lbr_stitch_enable(thread, false);
54ee84a303SIan Rogers 		INIT_LIST_HEAD(thread__namespaces_list(thread));
55ee84a303SIan Rogers 		INIT_LIST_HEAD(thread__comm_list(thread));
56ee84a303SIan Rogers 		init_rwsem(thread__namespaces_lock(thread));
57ee84a303SIan Rogers 		init_rwsem(thread__comm_lock(thread));
581902efe7SFrederic Weisbecker 
591902efe7SFrederic Weisbecker 		comm_str = malloc(32);
601902efe7SFrederic Weisbecker 		if (!comm_str)
611902efe7SFrederic Weisbecker 			goto err_thread;
621902efe7SFrederic Weisbecker 
631902efe7SFrederic Weisbecker 		snprintf(comm_str, 32, ":%d", tid);
6465de51f9SAdrian Hunter 		comm = comm__new(comm_str, 0, false);
651902efe7SFrederic Weisbecker 		free(comm_str);
661902efe7SFrederic Weisbecker 		if (!comm)
671902efe7SFrederic Weisbecker 			goto err_thread;
681902efe7SFrederic Weisbecker 
69ee84a303SIan Rogers 		list_add(&comm->list, thread__comm_list(thread));
70ee84a303SIan Rogers 		refcount_set(thread__refcnt(thread), 1);
71843ff37bSKrister Johansen 		/* Thread holds first ref to nsdata. */
72f6005cafSIan Rogers 		RC_CHK_ACCESS(thread)->nsinfo = nsinfo__new(pid);
73ee84a303SIan Rogers 		srccode_state_init(thread__srccode_state(thread));
746baa0a5aSFrederic Weisbecker 	}
756baa0a5aSFrederic Weisbecker 
76c824c433SArnaldo Carvalho de Melo 	return thread;
771902efe7SFrederic Weisbecker 
781902efe7SFrederic Weisbecker err_thread:
791902efe7SFrederic Weisbecker 	free(thread);
801902efe7SFrederic Weisbecker 	return NULL;
816baa0a5aSFrederic Weisbecker }
826baa0a5aSFrederic Weisbecker 
83*04cb4fc4SArnaldo Carvalho de Melo static void (*thread__priv_destructor)(void *priv);
84*04cb4fc4SArnaldo Carvalho de Melo 
85*04cb4fc4SArnaldo Carvalho de Melo void thread__set_priv_destructor(void (*destructor)(void *priv))
86*04cb4fc4SArnaldo Carvalho de Melo {
87*04cb4fc4SArnaldo Carvalho de Melo 	assert(thread__priv_destructor == NULL);
88*04cb4fc4SArnaldo Carvalho de Melo 
89*04cb4fc4SArnaldo Carvalho de Melo 	thread__priv_destructor = destructor;
90*04cb4fc4SArnaldo Carvalho de Melo }
91*04cb4fc4SArnaldo Carvalho de Melo 
92c824c433SArnaldo Carvalho de Melo void thread__delete(struct thread *thread)
93591765fdSArnaldo Carvalho de Melo {
94f3b3614aSHari Bathini 	struct namespaces *namespaces, *tmp_namespaces;
95f3b3614aSHari Bathini 	struct comm *comm, *tmp_comm;
961902efe7SFrederic Weisbecker 
9700447ccdSAdrian Hunter 	thread_stack__free(thread);
9800447ccdSAdrian Hunter 
99ee84a303SIan Rogers 	if (thread__maps(thread)) {
100ee84a303SIan Rogers 		maps__put(thread__maps(thread));
101ee84a303SIan Rogers 		thread__set_maps(thread, NULL);
1029608b84eSAdrian Hunter 	}
103ee84a303SIan Rogers 	down_write(thread__namespaces_lock(thread));
104f3b3614aSHari Bathini 	list_for_each_entry_safe(namespaces, tmp_namespaces,
105ee84a303SIan Rogers 				 thread__namespaces_list(thread), list) {
106e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&namespaces->list);
107f3b3614aSHari Bathini 		namespaces__free(namespaces);
108f3b3614aSHari Bathini 	}
109ee84a303SIan Rogers 	up_write(thread__namespaces_lock(thread));
110b32ee9e5SKan Liang 
111ee84a303SIan Rogers 	down_write(thread__comm_lock(thread));
112ee84a303SIan Rogers 	list_for_each_entry_safe(comm, tmp_comm, thread__comm_list(thread), list) {
113e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&comm->list);
1141902efe7SFrederic Weisbecker 		comm__free(comm);
1151902efe7SFrederic Weisbecker 	}
116ee84a303SIan Rogers 	up_write(thread__comm_lock(thread));
117b32ee9e5SKan Liang 
118f6005cafSIan Rogers 	nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo);
119ee84a303SIan Rogers 	srccode_state_free(thread__srccode_state(thread));
1201902efe7SFrederic Weisbecker 
121ee84a303SIan Rogers 	exit_rwsem(thread__namespaces_lock(thread));
122ee84a303SIan Rogers 	exit_rwsem(thread__comm_lock(thread));
1239c6c3f47SKan Liang 	thread__free_stitch_list(thread);
124*04cb4fc4SArnaldo Carvalho de Melo 
125*04cb4fc4SArnaldo Carvalho de Melo 	if (thread__priv_destructor)
126*04cb4fc4SArnaldo Carvalho de Melo 		thread__priv_destructor(thread__priv(thread));
127*04cb4fc4SArnaldo Carvalho de Melo 
128f6005cafSIan Rogers 	RC_CHK_FREE(thread);
129591765fdSArnaldo Carvalho de Melo }
130591765fdSArnaldo Carvalho de Melo 
131f3b623b8SArnaldo Carvalho de Melo struct thread *thread__get(struct thread *thread)
132f3b623b8SArnaldo Carvalho de Melo {
133f6005cafSIan Rogers 	struct thread *result;
134f6005cafSIan Rogers 
135f6005cafSIan Rogers 	if (RC_CHK_GET(result, thread))
136ee84a303SIan Rogers 		refcount_inc(thread__refcnt(thread));
137f6005cafSIan Rogers 
138f6005cafSIan Rogers 	return result;
139f3b623b8SArnaldo Carvalho de Melo }
140f3b623b8SArnaldo Carvalho de Melo 
141f3b623b8SArnaldo Carvalho de Melo void thread__put(struct thread *thread)
142f3b623b8SArnaldo Carvalho de Melo {
143ee84a303SIan Rogers 	if (thread && refcount_dec_and_test(thread__refcnt(thread)))
144f3b623b8SArnaldo Carvalho de Melo 		thread__delete(thread);
145f6005cafSIan Rogers 	else
146f6005cafSIan Rogers 		RC_CHK_PUT(thread);
147f3b623b8SArnaldo Carvalho de Melo }
148f3b623b8SArnaldo Carvalho de Melo 
149ee84a303SIan Rogers static struct namespaces *__thread__namespaces(struct thread *thread)
150f3b3614aSHari Bathini {
151ee84a303SIan Rogers 	if (list_empty(thread__namespaces_list(thread)))
152f3b3614aSHari Bathini 		return NULL;
153f3b3614aSHari Bathini 
154ee84a303SIan Rogers 	return list_first_entry(thread__namespaces_list(thread), struct namespaces, list);
155f3b3614aSHari Bathini }
156f3b3614aSHari Bathini 
1577cb10a08SNamhyung Kim struct namespaces *thread__namespaces(struct thread *thread)
1586584140bSNamhyung Kim {
1596584140bSNamhyung Kim 	struct namespaces *ns;
1606584140bSNamhyung Kim 
161ee84a303SIan Rogers 	down_read(thread__namespaces_lock(thread));
1626584140bSNamhyung Kim 	ns = __thread__namespaces(thread);
163ee84a303SIan Rogers 	up_read(thread__namespaces_lock(thread));
1646584140bSNamhyung Kim 
1656584140bSNamhyung Kim 	return ns;
1666584140bSNamhyung Kim }
1676584140bSNamhyung Kim 
168b32ee9e5SKan Liang static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
16969d81f09SArnaldo Carvalho de Melo 				    struct perf_record_namespaces *event)
170f3b3614aSHari Bathini {
1716584140bSNamhyung Kim 	struct namespaces *new, *curr = __thread__namespaces(thread);
172f3b3614aSHari Bathini 
173f3b3614aSHari Bathini 	new = namespaces__new(event);
174f3b3614aSHari Bathini 	if (!new)
175f3b3614aSHari Bathini 		return -ENOMEM;
176f3b3614aSHari Bathini 
177ee84a303SIan Rogers 	list_add(&new->list, thread__namespaces_list(thread));
178f3b3614aSHari Bathini 
179f3b3614aSHari Bathini 	if (timestamp && curr) {
180f3b3614aSHari Bathini 		/*
181f3b3614aSHari Bathini 		 * setns syscall must have changed few or all the namespaces
182f3b3614aSHari Bathini 		 * of this thread. Update end time for the namespaces
183f3b3614aSHari Bathini 		 * previously used.
184f3b3614aSHari Bathini 		 */
185f3b3614aSHari Bathini 		curr = list_next_entry(new, list);
186f3b3614aSHari Bathini 		curr->end_time = timestamp;
187f3b3614aSHari Bathini 	}
188f3b3614aSHari Bathini 
189f3b3614aSHari Bathini 	return 0;
190f3b3614aSHari Bathini }
191f3b3614aSHari Bathini 
192b32ee9e5SKan Liang int thread__set_namespaces(struct thread *thread, u64 timestamp,
19369d81f09SArnaldo Carvalho de Melo 			   struct perf_record_namespaces *event)
194b32ee9e5SKan Liang {
195b32ee9e5SKan Liang 	int ret;
196b32ee9e5SKan Liang 
197ee84a303SIan Rogers 	down_write(thread__namespaces_lock(thread));
198b32ee9e5SKan Liang 	ret = __thread__set_namespaces(thread, timestamp, event);
199ee84a303SIan Rogers 	up_write(thread__namespaces_lock(thread));
200b32ee9e5SKan Liang 	return ret;
201b32ee9e5SKan Liang }
202b32ee9e5SKan Liang 
203ee84a303SIan Rogers struct comm *thread__comm(struct thread *thread)
2046baa0a5aSFrederic Weisbecker {
205ee84a303SIan Rogers 	if (list_empty(thread__comm_list(thread)))
2061902efe7SFrederic Weisbecker 		return NULL;
2074385d580SDavid S. Miller 
208ee84a303SIan Rogers 	return list_first_entry(thread__comm_list(thread), struct comm, list);
2094385d580SDavid S. Miller }
2101902efe7SFrederic Weisbecker 
211ee84a303SIan Rogers struct comm *thread__exec_comm(struct thread *thread)
21265de51f9SAdrian Hunter {
2133de7ae0bSAdrian Hunter 	struct comm *comm, *last = NULL, *second_last = NULL;
21465de51f9SAdrian Hunter 
215ee84a303SIan Rogers 	list_for_each_entry(comm, thread__comm_list(thread), list) {
21665de51f9SAdrian Hunter 		if (comm->exec)
21765de51f9SAdrian Hunter 			return comm;
2183de7ae0bSAdrian Hunter 		second_last = last;
21965de51f9SAdrian Hunter 		last = comm;
22065de51f9SAdrian Hunter 	}
22165de51f9SAdrian Hunter 
2223de7ae0bSAdrian Hunter 	/*
2233de7ae0bSAdrian Hunter 	 * 'last' with no start time might be the parent's comm of a synthesized
2243de7ae0bSAdrian Hunter 	 * thread (created by processing a synthesized fork event). For a main
2253de7ae0bSAdrian Hunter 	 * thread, that is very probably wrong. Prefer a later comm to avoid
2263de7ae0bSAdrian Hunter 	 * that case.
2273de7ae0bSAdrian Hunter 	 */
228ee84a303SIan Rogers 	if (second_last && !last->start && thread__pid(thread) == thread__tid(thread))
2293de7ae0bSAdrian Hunter 		return second_last;
2303de7ae0bSAdrian Hunter 
23165de51f9SAdrian Hunter 	return last;
23265de51f9SAdrian Hunter }
23365de51f9SAdrian Hunter 
234b32ee9e5SKan Liang static int ____thread__set_comm(struct thread *thread, const char *str,
235b32ee9e5SKan Liang 				u64 timestamp, bool exec)
2361902efe7SFrederic Weisbecker {
2371902efe7SFrederic Weisbecker 	struct comm *new, *curr = thread__comm(thread);
2381902efe7SFrederic Weisbecker 
239a8480808SAdrian Hunter 	/* Override the default :tid entry */
240ee84a303SIan Rogers 	if (!thread__comm_set(thread)) {
24118ef15c6SArnaldo Carvalho de Melo 		int err = comm__override(curr, str, timestamp, exec);
2423178f58bSFrederic Weisbecker 		if (err)
2433178f58bSFrederic Weisbecker 			return err;
244a5285ad9SFrederic Weisbecker 	} else {
24565de51f9SAdrian Hunter 		new = comm__new(str, timestamp, exec);
2461902efe7SFrederic Weisbecker 		if (!new)
2471902efe7SFrederic Weisbecker 			return -ENOMEM;
248ee84a303SIan Rogers 		list_add(&new->list, thread__comm_list(thread));
249380b5143SNamhyung Kim 
250380b5143SNamhyung Kim 		if (exec)
251ee84a303SIan Rogers 			unwind__flush_access(thread__maps(thread));
252a5285ad9SFrederic Weisbecker 	}
253a5285ad9SFrederic Weisbecker 
254ee84a303SIan Rogers 	thread__set_comm_set(thread, true);
2551902efe7SFrederic Weisbecker 
2561902efe7SFrederic Weisbecker 	return 0;
2576baa0a5aSFrederic Weisbecker }
2586baa0a5aSFrederic Weisbecker 
259b32ee9e5SKan Liang int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
260b32ee9e5SKan Liang 		       bool exec)
261b32ee9e5SKan Liang {
262b32ee9e5SKan Liang 	int ret;
263b32ee9e5SKan Liang 
264ee84a303SIan Rogers 	down_write(thread__comm_lock(thread));
265b32ee9e5SKan Liang 	ret = ____thread__set_comm(thread, str, timestamp, exec);
266ee84a303SIan Rogers 	up_write(thread__comm_lock(thread));
267b32ee9e5SKan Liang 	return ret;
268b32ee9e5SKan Liang }
269b32ee9e5SKan Liang 
2702f3027acSArnaldo Carvalho de Melo int thread__set_comm_from_proc(struct thread *thread)
2712f3027acSArnaldo Carvalho de Melo {
2722f3027acSArnaldo Carvalho de Melo 	char path[64];
2732f3027acSArnaldo Carvalho de Melo 	char *comm = NULL;
2742f3027acSArnaldo Carvalho de Melo 	size_t sz;
2752f3027acSArnaldo Carvalho de Melo 	int err = -1;
2762f3027acSArnaldo Carvalho de Melo 
2772f3027acSArnaldo Carvalho de Melo 	if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
278ee84a303SIan Rogers 		       thread__pid(thread), thread__tid(thread)) >= (int)sizeof(path)) &&
2792f3027acSArnaldo Carvalho de Melo 	    procfs__read_str(path, &comm, &sz) == 0) {
2802f3027acSArnaldo Carvalho de Melo 		comm[sz - 1] = '\0';
2812f3027acSArnaldo Carvalho de Melo 		err = thread__set_comm(thread, comm, 0);
2822f3027acSArnaldo Carvalho de Melo 	}
2832f3027acSArnaldo Carvalho de Melo 
2842f3027acSArnaldo Carvalho de Melo 	return err;
2852f3027acSArnaldo Carvalho de Melo }
2862f3027acSArnaldo Carvalho de Melo 
287ee84a303SIan Rogers static const char *__thread__comm_str(struct thread *thread)
288b9c5143aSFrederic Weisbecker {
2891902efe7SFrederic Weisbecker 	const struct comm *comm = thread__comm(thread);
2901902efe7SFrederic Weisbecker 
2911902efe7SFrederic Weisbecker 	if (!comm)
2921902efe7SFrederic Weisbecker 		return NULL;
2931902efe7SFrederic Weisbecker 
2941902efe7SFrederic Weisbecker 	return comm__str(comm);
295b9c5143aSFrederic Weisbecker }
296b9c5143aSFrederic Weisbecker 
2977cb10a08SNamhyung Kim const char *thread__comm_str(struct thread *thread)
298b32ee9e5SKan Liang {
299b32ee9e5SKan Liang 	const char *str;
300b32ee9e5SKan Liang 
301ee84a303SIan Rogers 	down_read(thread__comm_lock(thread));
302b32ee9e5SKan Liang 	str = __thread__comm_str(thread);
303ee84a303SIan Rogers 	up_read(thread__comm_lock(thread));
304b32ee9e5SKan Liang 
305b32ee9e5SKan Liang 	return str;
306b32ee9e5SKan Liang }
307b32ee9e5SKan Liang 
3086e57f69fSliuwenyu static int __thread__comm_len(struct thread *thread, const char *comm)
309a4fb581bSFrederic Weisbecker {
3101902efe7SFrederic Weisbecker 	if (!comm)
311a4fb581bSFrederic Weisbecker 		return 0;
312ee84a303SIan Rogers 	thread__set_comm_len(thread, strlen(comm));
313a4fb581bSFrederic Weisbecker 
314ee84a303SIan Rogers 	return thread__var_comm_len(thread);
315a4fb581bSFrederic Weisbecker }
316a4fb581bSFrederic Weisbecker 
3176e57f69fSliuwenyu /* CHECKME: it should probably better return the max comm len from its comm list */
3186e57f69fSliuwenyu int thread__comm_len(struct thread *thread)
3196e57f69fSliuwenyu {
320ee84a303SIan Rogers 	int comm_len = thread__var_comm_len(thread);
3216e57f69fSliuwenyu 
3226e57f69fSliuwenyu 	if (!comm_len) {
3236e57f69fSliuwenyu 		const char *comm;
3246e57f69fSliuwenyu 
325ee84a303SIan Rogers 		down_read(thread__comm_lock(thread));
3266e57f69fSliuwenyu 		comm = __thread__comm_str(thread);
3276e57f69fSliuwenyu 		comm_len = __thread__comm_len(thread, comm);
328ee84a303SIan Rogers 		up_read(thread__comm_lock(thread));
3296e57f69fSliuwenyu 	}
3306e57f69fSliuwenyu 
3316e57f69fSliuwenyu 	return comm_len;
3326e57f69fSliuwenyu }
3336e57f69fSliuwenyu 
3343f067dcaSArnaldo Carvalho de Melo size_t thread__fprintf(struct thread *thread, FILE *fp)
33595011c60SArnaldo Carvalho de Melo {
336ee84a303SIan Rogers 	return fprintf(fp, "Thread %d %s\n", thread__tid(thread), thread__comm_str(thread)) +
337ee84a303SIan Rogers 	       maps__fprintf(thread__maps(thread), fp);
3386baa0a5aSFrederic Weisbecker }
3396baa0a5aSFrederic Weisbecker 
3408132a2a8SHe Kuang int thread__insert_map(struct thread *thread, struct map *map)
3411b46cddfSArnaldo Carvalho de Melo {
3428132a2a8SHe Kuang 	int ret;
3438132a2a8SHe Kuang 
344ee84a303SIan Rogers 	ret = unwind__prepare_access(thread__maps(thread), map, NULL);
3458132a2a8SHe Kuang 	if (ret)
3468132a2a8SHe Kuang 		return ret;
3478132a2a8SHe Kuang 
348ee84a303SIan Rogers 	maps__fixup_overlappings(thread__maps(thread), map, stderr);
349ee84a303SIan Rogers 	return maps__insert(thread__maps(thread), map);
35095011c60SArnaldo Carvalho de Melo }
35195011c60SArnaldo Carvalho de Melo 
3526c502584SJiri Olsa static int __thread__prepare_access(struct thread *thread)
3536c502584SJiri Olsa {
3546c502584SJiri Olsa 	bool initialized = false;
3553183f8caSArnaldo Carvalho de Melo 	int err = 0;
356ee84a303SIan Rogers 	struct maps *maps = thread__maps(thread);
357ff583dc4SIan Rogers 	struct map_rb_node *rb_node;
3586c502584SJiri Olsa 
3595ab6d715SIan Rogers 	down_read(maps__lock(maps));
3606c502584SJiri Olsa 
361ff583dc4SIan Rogers 	maps__for_each_entry(maps, rb_node) {
362ee84a303SIan Rogers 		err = unwind__prepare_access(thread__maps(thread), rb_node->map, &initialized);
3636c502584SJiri Olsa 		if (err || initialized)
3646c502584SJiri Olsa 			break;
3656c502584SJiri Olsa 	}
3666c502584SJiri Olsa 
3675ab6d715SIan Rogers 	up_read(maps__lock(maps));
3686c502584SJiri Olsa 
3696c502584SJiri Olsa 	return err;
3706c502584SJiri Olsa }
3716c502584SJiri Olsa 
3726c502584SJiri Olsa static int thread__prepare_access(struct thread *thread)
3736c502584SJiri Olsa {
3746c502584SJiri Olsa 	int err = 0;
3756c502584SJiri Olsa 
376382619c0SJiri Olsa 	if (dwarf_callchain_users)
3776c502584SJiri Olsa 		err = __thread__prepare_access(thread);
3786c502584SJiri Olsa 
3796c502584SJiri Olsa 	return err;
3806c502584SJiri Olsa }
3816c502584SJiri Olsa 
38279b6bb73SArnaldo Carvalho de Melo static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone)
383cddcef60SJiri Olsa {
384cddcef60SJiri Olsa 	/* This is new thread, we share map groups for process. */
385ee84a303SIan Rogers 	if (thread__pid(thread) == thread__pid(parent))
3866c502584SJiri Olsa 		return thread__prepare_access(thread);
387cddcef60SJiri Olsa 
388ee84a303SIan Rogers 	if (thread__maps(thread) == thread__maps(parent)) {
3890d7e7accSAdrian Hunter 		pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
390ee84a303SIan Rogers 			 thread__pid(thread), thread__tid(thread),
391ee84a303SIan Rogers 			 thread__pid(parent), thread__tid(parent));
3920d7e7accSAdrian Hunter 		return 0;
3930d7e7accSAdrian Hunter 	}
394cddcef60SJiri Olsa 	/* But this one is new process, copy maps. */
395ee84a303SIan Rogers 	return do_maps_clone ? maps__clone(thread, thread__maps(parent)) : 0;
396cddcef60SJiri Olsa }
397cddcef60SJiri Olsa 
3984f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
3996baa0a5aSFrederic Weisbecker {
400ee84a303SIan Rogers 	if (thread__comm_set(parent)) {
4011902efe7SFrederic Weisbecker 		const char *comm = thread__comm_str(parent);
40218ef15c6SArnaldo Carvalho de Melo 		int err;
4031902efe7SFrederic Weisbecker 		if (!comm)
4046baa0a5aSFrederic Weisbecker 			return -ENOMEM;
4051902efe7SFrederic Weisbecker 		err = thread__set_comm(thread, comm, timestamp);
4068d00be81SDavid Ahern 		if (err)
4071902efe7SFrederic Weisbecker 			return err;
408faa5c5c3SArnaldo Carvalho de Melo 	}
4096baa0a5aSFrederic Weisbecker 
410ee84a303SIan Rogers 	thread__set_ppid(thread, thread__tid(parent));
41179b6bb73SArnaldo Carvalho de Melo 	return thread__clone_maps(thread, parent, do_maps_clone);
4126baa0a5aSFrederic Weisbecker }
41352a3cb8cSArnaldo Carvalho de Melo 
41426bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
41552a3cb8cSArnaldo Carvalho de Melo 					struct addr_location *al)
41652a3cb8cSArnaldo Carvalho de Melo {
41752a3cb8cSArnaldo Carvalho de Melo 	size_t i;
4183b556bceSEric Engestrom 	const u8 cpumodes[] = {
41952a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_USER,
42052a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_KERNEL,
42152a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_USER,
42252a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_KERNEL
42352a3cb8cSArnaldo Carvalho de Melo 	};
42452a3cb8cSArnaldo Carvalho de Melo 
42552a3cb8cSArnaldo Carvalho de Melo 	for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
42626bd9331SArnaldo Carvalho de Melo 		thread__find_symbol(thread, cpumodes[i], addr, al);
42752a3cb8cSArnaldo Carvalho de Melo 		if (al->map)
42852a3cb8cSArnaldo Carvalho de Melo 			break;
42952a3cb8cSArnaldo Carvalho de Melo 	}
43052a3cb8cSArnaldo Carvalho de Melo }
431480ca357SAndi Kleen 
432480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
433480ca357SAndi Kleen {
434ee84a303SIan Rogers 	if (thread__pid(thread) == thread__tid(thread))
435480ca357SAndi Kleen 		return thread__get(thread);
436480ca357SAndi Kleen 
437ee84a303SIan Rogers 	if (thread__pid(thread) == -1)
438480ca357SAndi Kleen 		return NULL;
439480ca357SAndi Kleen 
440ee84a303SIan Rogers 	return machine__find_thread(machine, thread__pid(thread), thread__pid(thread));
441480ca357SAndi Kleen }
44215325938SAndi Kleen 
44315325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine,
44415325938SAndi Kleen 		   void *buf, u64 ip, int len, bool *is64bit)
44515325938SAndi Kleen {
44615325938SAndi Kleen 	u8 cpumode = PERF_RECORD_MISC_USER;
44715325938SAndi Kleen 	struct addr_location al;
44863df0e4bSIan Rogers 	struct dso *dso;
44915325938SAndi Kleen 	long offset;
45015325938SAndi Kleen 
45115325938SAndi Kleen 	if (machine__kernel_ip(machine, ip))
45215325938SAndi Kleen 		cpumode = PERF_RECORD_MISC_KERNEL;
45315325938SAndi Kleen 
4540dd5041cSIan Rogers 	addr_location__init(&al);
4550dd5041cSIan Rogers 	if (!thread__find_map(thread, cpumode, ip, &al)) {
4560dd5041cSIan Rogers 		addr_location__exit(&al);
45763df0e4bSIan Rogers 		return -1;
4580dd5041cSIan Rogers 	}
45963df0e4bSIan Rogers 
46063df0e4bSIan Rogers 	dso = map__dso(al.map);
46163df0e4bSIan Rogers 
4620dd5041cSIan Rogers 	if (!dso || dso->data.status == DSO_DATA_STATUS_ERROR || map__load(al.map) < 0) {
4630dd5041cSIan Rogers 		addr_location__exit(&al);
46415325938SAndi Kleen 		return -1;
4650dd5041cSIan Rogers 	}
46615325938SAndi Kleen 
46778a1f7cdSIan Rogers 	offset = map__map_ip(al.map, ip);
46815325938SAndi Kleen 	if (is64bit)
46963df0e4bSIan Rogers 		*is64bit = dso->is_64_bit;
47015325938SAndi Kleen 
4710dd5041cSIan Rogers 	addr_location__exit(&al);
4720dd5041cSIan Rogers 
47363df0e4bSIan Rogers 	return dso__data_read_offset(dso, machine, offset, buf, len);
47415325938SAndi Kleen }
475ff165628SKan Liang 
476ff165628SKan Liang void thread__free_stitch_list(struct thread *thread)
477ff165628SKan Liang {
478ee84a303SIan Rogers 	struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
479ff165628SKan Liang 	struct stitch_list *pos, *tmp;
480ff165628SKan Liang 
481ff165628SKan Liang 	if (!lbr_stitch)
482ff165628SKan Liang 		return;
483ff165628SKan Liang 
484ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) {
485ff165628SKan Liang 		list_del_init(&pos->node);
486ff165628SKan Liang 		free(pos);
487ff165628SKan Liang 	}
488ff165628SKan Liang 
489ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) {
490ff165628SKan Liang 		list_del_init(&pos->node);
491ff165628SKan Liang 		free(pos);
492ff165628SKan Liang 	}
493ff165628SKan Liang 
494ff165628SKan Liang 	zfree(&lbr_stitch->prev_lbr_cursor);
495ee84a303SIan Rogers 	free(thread__lbr_stitch(thread));
496ee84a303SIan Rogers 	thread__set_lbr_stitch(thread, NULL);
497ff165628SKan Liang }
498