xref: /openbmc/linux/tools/perf/util/thread.c (revision ee84a3032b74055feed192a727e872b0a18d1140)
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 {
24*ee84a303SIan Rogers 	pid_t pid = thread__pid(thread);
25cddcef60SJiri Olsa 
26*ee84a303SIan Rogers 	if (pid == thread__tid(thread) || pid == -1) {
27*ee84a303SIan Rogers 		thread__set_maps(thread, maps__new(machine));
28cddcef60SJiri Olsa 	} else {
2918ef15c6SArnaldo Carvalho de Melo 		struct thread *leader = __machine__findnew_thread(machine, pid, pid);
30*ee84a303SIan Rogers 
31abd82868SArnaldo Carvalho de Melo 		if (leader) {
32*ee84a303SIan 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 
37*ee84a303SIan 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;
44c824c433SArnaldo Carvalho de Melo 	struct thread *thread = zalloc(sizeof(*thread));
456baa0a5aSFrederic Weisbecker 
46c824c433SArnaldo Carvalho de Melo 	if (thread != NULL) {
47*ee84a303SIan Rogers 		thread__set_pid(thread, pid);
48*ee84a303SIan Rogers 		thread__set_tid(thread, tid);
49*ee84a303SIan Rogers 		thread__set_ppid(thread, -1);
50*ee84a303SIan Rogers 		thread__set_cpu(thread, -1);
51*ee84a303SIan Rogers 		thread__set_guest_cpu(thread, -1);
52*ee84a303SIan Rogers 		thread__set_lbr_stitch_enable(thread, false);
53*ee84a303SIan Rogers 		INIT_LIST_HEAD(thread__namespaces_list(thread));
54*ee84a303SIan Rogers 		INIT_LIST_HEAD(thread__comm_list(thread));
55*ee84a303SIan Rogers 		init_rwsem(thread__namespaces_lock(thread));
56*ee84a303SIan Rogers 		init_rwsem(thread__comm_lock(thread));
571902efe7SFrederic Weisbecker 
581902efe7SFrederic Weisbecker 		comm_str = malloc(32);
591902efe7SFrederic Weisbecker 		if (!comm_str)
601902efe7SFrederic Weisbecker 			goto err_thread;
611902efe7SFrederic Weisbecker 
621902efe7SFrederic Weisbecker 		snprintf(comm_str, 32, ":%d", tid);
6365de51f9SAdrian Hunter 		comm = comm__new(comm_str, 0, false);
641902efe7SFrederic Weisbecker 		free(comm_str);
651902efe7SFrederic Weisbecker 		if (!comm)
661902efe7SFrederic Weisbecker 			goto err_thread;
671902efe7SFrederic Weisbecker 
68*ee84a303SIan Rogers 		list_add(&comm->list, thread__comm_list(thread));
69*ee84a303SIan Rogers 		refcount_set(thread__refcnt(thread), 1);
70843ff37bSKrister Johansen 		/* Thread holds first ref to nsdata. */
71843ff37bSKrister Johansen 		thread->nsinfo = nsinfo__new(pid);
72*ee84a303SIan Rogers 		srccode_state_init(thread__srccode_state(thread));
736baa0a5aSFrederic Weisbecker 	}
746baa0a5aSFrederic Weisbecker 
75c824c433SArnaldo Carvalho de Melo 	return thread;
761902efe7SFrederic Weisbecker 
771902efe7SFrederic Weisbecker err_thread:
781902efe7SFrederic Weisbecker 	free(thread);
791902efe7SFrederic Weisbecker 	return NULL;
806baa0a5aSFrederic Weisbecker }
816baa0a5aSFrederic Weisbecker 
82c824c433SArnaldo Carvalho de Melo void thread__delete(struct thread *thread)
83591765fdSArnaldo Carvalho de Melo {
84f3b3614aSHari Bathini 	struct namespaces *namespaces, *tmp_namespaces;
85f3b3614aSHari Bathini 	struct comm *comm, *tmp_comm;
861902efe7SFrederic Weisbecker 
8700447ccdSAdrian Hunter 	thread_stack__free(thread);
8800447ccdSAdrian Hunter 
89*ee84a303SIan Rogers 	if (thread__maps(thread)) {
90*ee84a303SIan Rogers 		maps__put(thread__maps(thread));
91*ee84a303SIan Rogers 		thread__set_maps(thread, NULL);
929608b84eSAdrian Hunter 	}
93*ee84a303SIan Rogers 	down_write(thread__namespaces_lock(thread));
94f3b3614aSHari Bathini 	list_for_each_entry_safe(namespaces, tmp_namespaces,
95*ee84a303SIan Rogers 				 thread__namespaces_list(thread), list) {
96e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&namespaces->list);
97f3b3614aSHari Bathini 		namespaces__free(namespaces);
98f3b3614aSHari Bathini 	}
99*ee84a303SIan Rogers 	up_write(thread__namespaces_lock(thread));
100b32ee9e5SKan Liang 
101*ee84a303SIan Rogers 	down_write(thread__comm_lock(thread));
102*ee84a303SIan Rogers 	list_for_each_entry_safe(comm, tmp_comm, thread__comm_list(thread), list) {
103e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&comm->list);
1041902efe7SFrederic Weisbecker 		comm__free(comm);
1051902efe7SFrederic Weisbecker 	}
106*ee84a303SIan Rogers 	up_write(thread__comm_lock(thread));
107b32ee9e5SKan Liang 
108843ff37bSKrister Johansen 	nsinfo__zput(thread->nsinfo);
109*ee84a303SIan Rogers 	srccode_state_free(thread__srccode_state(thread));
1101902efe7SFrederic Weisbecker 
111*ee84a303SIan Rogers 	exit_rwsem(thread__namespaces_lock(thread));
112*ee84a303SIan Rogers 	exit_rwsem(thread__comm_lock(thread));
1139c6c3f47SKan Liang 	thread__free_stitch_list(thread);
114c824c433SArnaldo Carvalho de Melo 	free(thread);
115591765fdSArnaldo Carvalho de Melo }
116591765fdSArnaldo Carvalho de Melo 
117f3b623b8SArnaldo Carvalho de Melo struct thread *thread__get(struct thread *thread)
118f3b623b8SArnaldo Carvalho de Melo {
119b91fc39fSArnaldo Carvalho de Melo 	if (thread)
120*ee84a303SIan Rogers 		refcount_inc(thread__refcnt(thread));
121f3b623b8SArnaldo Carvalho de Melo 	return thread;
122f3b623b8SArnaldo Carvalho de Melo }
123f3b623b8SArnaldo Carvalho de Melo 
124f3b623b8SArnaldo Carvalho de Melo void thread__put(struct thread *thread)
125f3b623b8SArnaldo Carvalho de Melo {
126*ee84a303SIan Rogers 	if (thread && refcount_dec_and_test(thread__refcnt(thread)))
127f3b623b8SArnaldo Carvalho de Melo 		thread__delete(thread);
128f3b623b8SArnaldo Carvalho de Melo }
129f3b623b8SArnaldo Carvalho de Melo 
130*ee84a303SIan Rogers static struct namespaces *__thread__namespaces(struct thread *thread)
131f3b3614aSHari Bathini {
132*ee84a303SIan Rogers 	if (list_empty(thread__namespaces_list(thread)))
133f3b3614aSHari Bathini 		return NULL;
134f3b3614aSHari Bathini 
135*ee84a303SIan Rogers 	return list_first_entry(thread__namespaces_list(thread), struct namespaces, list);
136f3b3614aSHari Bathini }
137f3b3614aSHari Bathini 
1387cb10a08SNamhyung Kim struct namespaces *thread__namespaces(struct thread *thread)
1396584140bSNamhyung Kim {
1406584140bSNamhyung Kim 	struct namespaces *ns;
1416584140bSNamhyung Kim 
142*ee84a303SIan Rogers 	down_read(thread__namespaces_lock(thread));
1436584140bSNamhyung Kim 	ns = __thread__namespaces(thread);
144*ee84a303SIan Rogers 	up_read(thread__namespaces_lock(thread));
1456584140bSNamhyung Kim 
1466584140bSNamhyung Kim 	return ns;
1476584140bSNamhyung Kim }
1486584140bSNamhyung Kim 
149b32ee9e5SKan Liang static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
15069d81f09SArnaldo Carvalho de Melo 				    struct perf_record_namespaces *event)
151f3b3614aSHari Bathini {
1526584140bSNamhyung Kim 	struct namespaces *new, *curr = __thread__namespaces(thread);
153f3b3614aSHari Bathini 
154f3b3614aSHari Bathini 	new = namespaces__new(event);
155f3b3614aSHari Bathini 	if (!new)
156f3b3614aSHari Bathini 		return -ENOMEM;
157f3b3614aSHari Bathini 
158*ee84a303SIan Rogers 	list_add(&new->list, thread__namespaces_list(thread));
159f3b3614aSHari Bathini 
160f3b3614aSHari Bathini 	if (timestamp && curr) {
161f3b3614aSHari Bathini 		/*
162f3b3614aSHari Bathini 		 * setns syscall must have changed few or all the namespaces
163f3b3614aSHari Bathini 		 * of this thread. Update end time for the namespaces
164f3b3614aSHari Bathini 		 * previously used.
165f3b3614aSHari Bathini 		 */
166f3b3614aSHari Bathini 		curr = list_next_entry(new, list);
167f3b3614aSHari Bathini 		curr->end_time = timestamp;
168f3b3614aSHari Bathini 	}
169f3b3614aSHari Bathini 
170f3b3614aSHari Bathini 	return 0;
171f3b3614aSHari Bathini }
172f3b3614aSHari Bathini 
173b32ee9e5SKan Liang int thread__set_namespaces(struct thread *thread, u64 timestamp,
17469d81f09SArnaldo Carvalho de Melo 			   struct perf_record_namespaces *event)
175b32ee9e5SKan Liang {
176b32ee9e5SKan Liang 	int ret;
177b32ee9e5SKan Liang 
178*ee84a303SIan Rogers 	down_write(thread__namespaces_lock(thread));
179b32ee9e5SKan Liang 	ret = __thread__set_namespaces(thread, timestamp, event);
180*ee84a303SIan Rogers 	up_write(thread__namespaces_lock(thread));
181b32ee9e5SKan Liang 	return ret;
182b32ee9e5SKan Liang }
183b32ee9e5SKan Liang 
184*ee84a303SIan Rogers struct comm *thread__comm(struct thread *thread)
1856baa0a5aSFrederic Weisbecker {
186*ee84a303SIan Rogers 	if (list_empty(thread__comm_list(thread)))
1871902efe7SFrederic Weisbecker 		return NULL;
1884385d580SDavid S. Miller 
189*ee84a303SIan Rogers 	return list_first_entry(thread__comm_list(thread), struct comm, list);
1904385d580SDavid S. Miller }
1911902efe7SFrederic Weisbecker 
192*ee84a303SIan Rogers struct comm *thread__exec_comm(struct thread *thread)
19365de51f9SAdrian Hunter {
1943de7ae0bSAdrian Hunter 	struct comm *comm, *last = NULL, *second_last = NULL;
19565de51f9SAdrian Hunter 
196*ee84a303SIan Rogers 	list_for_each_entry(comm, thread__comm_list(thread), list) {
19765de51f9SAdrian Hunter 		if (comm->exec)
19865de51f9SAdrian Hunter 			return comm;
1993de7ae0bSAdrian Hunter 		second_last = last;
20065de51f9SAdrian Hunter 		last = comm;
20165de51f9SAdrian Hunter 	}
20265de51f9SAdrian Hunter 
2033de7ae0bSAdrian Hunter 	/*
2043de7ae0bSAdrian Hunter 	 * 'last' with no start time might be the parent's comm of a synthesized
2053de7ae0bSAdrian Hunter 	 * thread (created by processing a synthesized fork event). For a main
2063de7ae0bSAdrian Hunter 	 * thread, that is very probably wrong. Prefer a later comm to avoid
2073de7ae0bSAdrian Hunter 	 * that case.
2083de7ae0bSAdrian Hunter 	 */
209*ee84a303SIan Rogers 	if (second_last && !last->start && thread__pid(thread) == thread__tid(thread))
2103de7ae0bSAdrian Hunter 		return second_last;
2113de7ae0bSAdrian Hunter 
21265de51f9SAdrian Hunter 	return last;
21365de51f9SAdrian Hunter }
21465de51f9SAdrian Hunter 
215b32ee9e5SKan Liang static int ____thread__set_comm(struct thread *thread, const char *str,
216b32ee9e5SKan Liang 				u64 timestamp, bool exec)
2171902efe7SFrederic Weisbecker {
2181902efe7SFrederic Weisbecker 	struct comm *new, *curr = thread__comm(thread);
2191902efe7SFrederic Weisbecker 
220a8480808SAdrian Hunter 	/* Override the default :tid entry */
221*ee84a303SIan Rogers 	if (!thread__comm_set(thread)) {
22218ef15c6SArnaldo Carvalho de Melo 		int err = comm__override(curr, str, timestamp, exec);
2233178f58bSFrederic Weisbecker 		if (err)
2243178f58bSFrederic Weisbecker 			return err;
225a5285ad9SFrederic Weisbecker 	} else {
22665de51f9SAdrian Hunter 		new = comm__new(str, timestamp, exec);
2271902efe7SFrederic Weisbecker 		if (!new)
2281902efe7SFrederic Weisbecker 			return -ENOMEM;
229*ee84a303SIan Rogers 		list_add(&new->list, thread__comm_list(thread));
230380b5143SNamhyung Kim 
231380b5143SNamhyung Kim 		if (exec)
232*ee84a303SIan Rogers 			unwind__flush_access(thread__maps(thread));
233a5285ad9SFrederic Weisbecker 	}
234a5285ad9SFrederic Weisbecker 
235*ee84a303SIan Rogers 	thread__set_comm_set(thread, true);
2361902efe7SFrederic Weisbecker 
2371902efe7SFrederic Weisbecker 	return 0;
2386baa0a5aSFrederic Weisbecker }
2396baa0a5aSFrederic Weisbecker 
240b32ee9e5SKan Liang int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
241b32ee9e5SKan Liang 		       bool exec)
242b32ee9e5SKan Liang {
243b32ee9e5SKan Liang 	int ret;
244b32ee9e5SKan Liang 
245*ee84a303SIan Rogers 	down_write(thread__comm_lock(thread));
246b32ee9e5SKan Liang 	ret = ____thread__set_comm(thread, str, timestamp, exec);
247*ee84a303SIan Rogers 	up_write(thread__comm_lock(thread));
248b32ee9e5SKan Liang 	return ret;
249b32ee9e5SKan Liang }
250b32ee9e5SKan Liang 
2512f3027acSArnaldo Carvalho de Melo int thread__set_comm_from_proc(struct thread *thread)
2522f3027acSArnaldo Carvalho de Melo {
2532f3027acSArnaldo Carvalho de Melo 	char path[64];
2542f3027acSArnaldo Carvalho de Melo 	char *comm = NULL;
2552f3027acSArnaldo Carvalho de Melo 	size_t sz;
2562f3027acSArnaldo Carvalho de Melo 	int err = -1;
2572f3027acSArnaldo Carvalho de Melo 
2582f3027acSArnaldo Carvalho de Melo 	if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
259*ee84a303SIan Rogers 		       thread__pid(thread), thread__tid(thread)) >= (int)sizeof(path)) &&
2602f3027acSArnaldo Carvalho de Melo 	    procfs__read_str(path, &comm, &sz) == 0) {
2612f3027acSArnaldo Carvalho de Melo 		comm[sz - 1] = '\0';
2622f3027acSArnaldo Carvalho de Melo 		err = thread__set_comm(thread, comm, 0);
2632f3027acSArnaldo Carvalho de Melo 	}
2642f3027acSArnaldo Carvalho de Melo 
2652f3027acSArnaldo Carvalho de Melo 	return err;
2662f3027acSArnaldo Carvalho de Melo }
2672f3027acSArnaldo Carvalho de Melo 
268*ee84a303SIan Rogers static const char *__thread__comm_str(struct thread *thread)
269b9c5143aSFrederic Weisbecker {
2701902efe7SFrederic Weisbecker 	const struct comm *comm = thread__comm(thread);
2711902efe7SFrederic Weisbecker 
2721902efe7SFrederic Weisbecker 	if (!comm)
2731902efe7SFrederic Weisbecker 		return NULL;
2741902efe7SFrederic Weisbecker 
2751902efe7SFrederic Weisbecker 	return comm__str(comm);
276b9c5143aSFrederic Weisbecker }
277b9c5143aSFrederic Weisbecker 
2787cb10a08SNamhyung Kim const char *thread__comm_str(struct thread *thread)
279b32ee9e5SKan Liang {
280b32ee9e5SKan Liang 	const char *str;
281b32ee9e5SKan Liang 
282*ee84a303SIan Rogers 	down_read(thread__comm_lock(thread));
283b32ee9e5SKan Liang 	str = __thread__comm_str(thread);
284*ee84a303SIan Rogers 	up_read(thread__comm_lock(thread));
285b32ee9e5SKan Liang 
286b32ee9e5SKan Liang 	return str;
287b32ee9e5SKan Liang }
288b32ee9e5SKan Liang 
2896e57f69fSliuwenyu static int __thread__comm_len(struct thread *thread, const char *comm)
290a4fb581bSFrederic Weisbecker {
2911902efe7SFrederic Weisbecker 	if (!comm)
292a4fb581bSFrederic Weisbecker 		return 0;
293*ee84a303SIan Rogers 	thread__set_comm_len(thread, strlen(comm));
294a4fb581bSFrederic Weisbecker 
295*ee84a303SIan Rogers 	return thread__var_comm_len(thread);
296a4fb581bSFrederic Weisbecker }
297a4fb581bSFrederic Weisbecker 
2986e57f69fSliuwenyu /* CHECKME: it should probably better return the max comm len from its comm list */
2996e57f69fSliuwenyu int thread__comm_len(struct thread *thread)
3006e57f69fSliuwenyu {
301*ee84a303SIan Rogers 	int comm_len = thread__var_comm_len(thread);
3026e57f69fSliuwenyu 
3036e57f69fSliuwenyu 	if (!comm_len) {
3046e57f69fSliuwenyu 		const char *comm;
3056e57f69fSliuwenyu 
306*ee84a303SIan Rogers 		down_read(thread__comm_lock(thread));
3076e57f69fSliuwenyu 		comm = __thread__comm_str(thread);
3086e57f69fSliuwenyu 		comm_len = __thread__comm_len(thread, comm);
309*ee84a303SIan Rogers 		up_read(thread__comm_lock(thread));
3106e57f69fSliuwenyu 	}
3116e57f69fSliuwenyu 
3126e57f69fSliuwenyu 	return comm_len;
3136e57f69fSliuwenyu }
3146e57f69fSliuwenyu 
3153f067dcaSArnaldo Carvalho de Melo size_t thread__fprintf(struct thread *thread, FILE *fp)
31695011c60SArnaldo Carvalho de Melo {
317*ee84a303SIan Rogers 	return fprintf(fp, "Thread %d %s\n", thread__tid(thread), thread__comm_str(thread)) +
318*ee84a303SIan Rogers 	       maps__fprintf(thread__maps(thread), fp);
3196baa0a5aSFrederic Weisbecker }
3206baa0a5aSFrederic Weisbecker 
3218132a2a8SHe Kuang int thread__insert_map(struct thread *thread, struct map *map)
3221b46cddfSArnaldo Carvalho de Melo {
3238132a2a8SHe Kuang 	int ret;
3248132a2a8SHe Kuang 
325*ee84a303SIan Rogers 	ret = unwind__prepare_access(thread__maps(thread), map, NULL);
3268132a2a8SHe Kuang 	if (ret)
3278132a2a8SHe Kuang 		return ret;
3288132a2a8SHe Kuang 
329*ee84a303SIan Rogers 	maps__fixup_overlappings(thread__maps(thread), map, stderr);
330*ee84a303SIan Rogers 	return maps__insert(thread__maps(thread), map);
33195011c60SArnaldo Carvalho de Melo }
33295011c60SArnaldo Carvalho de Melo 
3336c502584SJiri Olsa static int __thread__prepare_access(struct thread *thread)
3346c502584SJiri Olsa {
3356c502584SJiri Olsa 	bool initialized = false;
3363183f8caSArnaldo Carvalho de Melo 	int err = 0;
337*ee84a303SIan Rogers 	struct maps *maps = thread__maps(thread);
338ff583dc4SIan Rogers 	struct map_rb_node *rb_node;
3396c502584SJiri Olsa 
3405ab6d715SIan Rogers 	down_read(maps__lock(maps));
3416c502584SJiri Olsa 
342ff583dc4SIan Rogers 	maps__for_each_entry(maps, rb_node) {
343*ee84a303SIan Rogers 		err = unwind__prepare_access(thread__maps(thread), rb_node->map, &initialized);
3446c502584SJiri Olsa 		if (err || initialized)
3456c502584SJiri Olsa 			break;
3466c502584SJiri Olsa 	}
3476c502584SJiri Olsa 
3485ab6d715SIan Rogers 	up_read(maps__lock(maps));
3496c502584SJiri Olsa 
3506c502584SJiri Olsa 	return err;
3516c502584SJiri Olsa }
3526c502584SJiri Olsa 
3536c502584SJiri Olsa static int thread__prepare_access(struct thread *thread)
3546c502584SJiri Olsa {
3556c502584SJiri Olsa 	int err = 0;
3566c502584SJiri Olsa 
357382619c0SJiri Olsa 	if (dwarf_callchain_users)
3586c502584SJiri Olsa 		err = __thread__prepare_access(thread);
3596c502584SJiri Olsa 
3606c502584SJiri Olsa 	return err;
3616c502584SJiri Olsa }
3626c502584SJiri Olsa 
36379b6bb73SArnaldo Carvalho de Melo static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone)
364cddcef60SJiri Olsa {
365cddcef60SJiri Olsa 	/* This is new thread, we share map groups for process. */
366*ee84a303SIan Rogers 	if (thread__pid(thread) == thread__pid(parent))
3676c502584SJiri Olsa 		return thread__prepare_access(thread);
368cddcef60SJiri Olsa 
369*ee84a303SIan Rogers 	if (thread__maps(thread) == thread__maps(parent)) {
3700d7e7accSAdrian Hunter 		pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
371*ee84a303SIan Rogers 			 thread__pid(thread), thread__tid(thread),
372*ee84a303SIan Rogers 			 thread__pid(parent), thread__tid(parent));
3730d7e7accSAdrian Hunter 		return 0;
3740d7e7accSAdrian Hunter 	}
375cddcef60SJiri Olsa 	/* But this one is new process, copy maps. */
376*ee84a303SIan Rogers 	return do_maps_clone ? maps__clone(thread, thread__maps(parent)) : 0;
377cddcef60SJiri Olsa }
378cddcef60SJiri Olsa 
3794f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
3806baa0a5aSFrederic Weisbecker {
381*ee84a303SIan Rogers 	if (thread__comm_set(parent)) {
3821902efe7SFrederic Weisbecker 		const char *comm = thread__comm_str(parent);
38318ef15c6SArnaldo Carvalho de Melo 		int err;
3841902efe7SFrederic Weisbecker 		if (!comm)
3856baa0a5aSFrederic Weisbecker 			return -ENOMEM;
3861902efe7SFrederic Weisbecker 		err = thread__set_comm(thread, comm, timestamp);
3878d00be81SDavid Ahern 		if (err)
3881902efe7SFrederic Weisbecker 			return err;
389faa5c5c3SArnaldo Carvalho de Melo 	}
3906baa0a5aSFrederic Weisbecker 
391*ee84a303SIan Rogers 	thread__set_ppid(thread, thread__tid(parent));
39279b6bb73SArnaldo Carvalho de Melo 	return thread__clone_maps(thread, parent, do_maps_clone);
3936baa0a5aSFrederic Weisbecker }
39452a3cb8cSArnaldo Carvalho de Melo 
39526bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
39652a3cb8cSArnaldo Carvalho de Melo 					struct addr_location *al)
39752a3cb8cSArnaldo Carvalho de Melo {
39852a3cb8cSArnaldo Carvalho de Melo 	size_t i;
3993b556bceSEric Engestrom 	const u8 cpumodes[] = {
40052a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_USER,
40152a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_KERNEL,
40252a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_USER,
40352a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_KERNEL
40452a3cb8cSArnaldo Carvalho de Melo 	};
40552a3cb8cSArnaldo Carvalho de Melo 
40652a3cb8cSArnaldo Carvalho de Melo 	for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
40726bd9331SArnaldo Carvalho de Melo 		thread__find_symbol(thread, cpumodes[i], addr, al);
40852a3cb8cSArnaldo Carvalho de Melo 		if (al->map)
40952a3cb8cSArnaldo Carvalho de Melo 			break;
41052a3cb8cSArnaldo Carvalho de Melo 	}
41152a3cb8cSArnaldo Carvalho de Melo }
412480ca357SAndi Kleen 
413480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
414480ca357SAndi Kleen {
415*ee84a303SIan Rogers 	if (thread__pid(thread) == thread__tid(thread))
416480ca357SAndi Kleen 		return thread__get(thread);
417480ca357SAndi Kleen 
418*ee84a303SIan Rogers 	if (thread__pid(thread) == -1)
419480ca357SAndi Kleen 		return NULL;
420480ca357SAndi Kleen 
421*ee84a303SIan Rogers 	return machine__find_thread(machine, thread__pid(thread), thread__pid(thread));
422480ca357SAndi Kleen }
42315325938SAndi Kleen 
42415325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine,
42515325938SAndi Kleen 		   void *buf, u64 ip, int len, bool *is64bit)
42615325938SAndi Kleen {
42715325938SAndi Kleen 	u8 cpumode = PERF_RECORD_MISC_USER;
42815325938SAndi Kleen 	struct addr_location al;
42963df0e4bSIan Rogers 	struct dso *dso;
43015325938SAndi Kleen 	long offset;
43115325938SAndi Kleen 
43215325938SAndi Kleen 	if (machine__kernel_ip(machine, ip))
43315325938SAndi Kleen 		cpumode = PERF_RECORD_MISC_KERNEL;
43415325938SAndi Kleen 
43563df0e4bSIan Rogers 	if (!thread__find_map(thread, cpumode, ip, &al))
43663df0e4bSIan Rogers 	       return -1;
43763df0e4bSIan Rogers 
43863df0e4bSIan Rogers 	dso = map__dso(al.map);
43963df0e4bSIan Rogers 
44063df0e4bSIan Rogers 	if( !dso || dso->data.status == DSO_DATA_STATUS_ERROR || map__load(al.map) < 0)
44115325938SAndi Kleen 		return -1;
44215325938SAndi Kleen 
44378a1f7cdSIan Rogers 	offset = map__map_ip(al.map, ip);
44415325938SAndi Kleen 	if (is64bit)
44563df0e4bSIan Rogers 		*is64bit = dso->is_64_bit;
44615325938SAndi Kleen 
44763df0e4bSIan Rogers 	return dso__data_read_offset(dso, machine, offset, buf, len);
44815325938SAndi Kleen }
449ff165628SKan Liang 
450ff165628SKan Liang void thread__free_stitch_list(struct thread *thread)
451ff165628SKan Liang {
452*ee84a303SIan Rogers 	struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
453ff165628SKan Liang 	struct stitch_list *pos, *tmp;
454ff165628SKan Liang 
455ff165628SKan Liang 	if (!lbr_stitch)
456ff165628SKan Liang 		return;
457ff165628SKan Liang 
458ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) {
459ff165628SKan Liang 		list_del_init(&pos->node);
460ff165628SKan Liang 		free(pos);
461ff165628SKan Liang 	}
462ff165628SKan Liang 
463ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) {
464ff165628SKan Liang 		list_del_init(&pos->node);
465ff165628SKan Liang 		free(pos);
466ff165628SKan Liang 	}
467ff165628SKan Liang 
468ff165628SKan Liang 	zfree(&lbr_stitch->prev_lbr_cursor);
469*ee84a303SIan Rogers 	free(thread__lbr_stitch(thread));
470*ee84a303SIan Rogers 	thread__set_lbr_stitch(thread, NULL);
471ff165628SKan Liang }
472