xref: /openbmc/linux/tools/perf/util/thread.c (revision f6005cafebab72f8c02100dc896d6cfd5b8918cb)
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;
44*f6005cafSIan Rogers 	RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread));
45*f6005cafSIan Rogers 	struct thread *thread;
466baa0a5aSFrederic Weisbecker 
47*f6005cafSIan 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. */
72*f6005cafSIan 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 
83c824c433SArnaldo Carvalho de Melo void thread__delete(struct thread *thread)
84591765fdSArnaldo Carvalho de Melo {
85f3b3614aSHari Bathini 	struct namespaces *namespaces, *tmp_namespaces;
86f3b3614aSHari Bathini 	struct comm *comm, *tmp_comm;
871902efe7SFrederic Weisbecker 
8800447ccdSAdrian Hunter 	thread_stack__free(thread);
8900447ccdSAdrian Hunter 
90ee84a303SIan Rogers 	if (thread__maps(thread)) {
91ee84a303SIan Rogers 		maps__put(thread__maps(thread));
92ee84a303SIan Rogers 		thread__set_maps(thread, NULL);
939608b84eSAdrian Hunter 	}
94ee84a303SIan Rogers 	down_write(thread__namespaces_lock(thread));
95f3b3614aSHari Bathini 	list_for_each_entry_safe(namespaces, tmp_namespaces,
96ee84a303SIan Rogers 				 thread__namespaces_list(thread), list) {
97e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&namespaces->list);
98f3b3614aSHari Bathini 		namespaces__free(namespaces);
99f3b3614aSHari Bathini 	}
100ee84a303SIan Rogers 	up_write(thread__namespaces_lock(thread));
101b32ee9e5SKan Liang 
102ee84a303SIan Rogers 	down_write(thread__comm_lock(thread));
103ee84a303SIan Rogers 	list_for_each_entry_safe(comm, tmp_comm, thread__comm_list(thread), list) {
104e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&comm->list);
1051902efe7SFrederic Weisbecker 		comm__free(comm);
1061902efe7SFrederic Weisbecker 	}
107ee84a303SIan Rogers 	up_write(thread__comm_lock(thread));
108b32ee9e5SKan Liang 
109*f6005cafSIan Rogers 	nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo);
110ee84a303SIan Rogers 	srccode_state_free(thread__srccode_state(thread));
1111902efe7SFrederic Weisbecker 
112ee84a303SIan Rogers 	exit_rwsem(thread__namespaces_lock(thread));
113ee84a303SIan Rogers 	exit_rwsem(thread__comm_lock(thread));
1149c6c3f47SKan Liang 	thread__free_stitch_list(thread);
115*f6005cafSIan Rogers 	RC_CHK_FREE(thread);
116591765fdSArnaldo Carvalho de Melo }
117591765fdSArnaldo Carvalho de Melo 
118f3b623b8SArnaldo Carvalho de Melo struct thread *thread__get(struct thread *thread)
119f3b623b8SArnaldo Carvalho de Melo {
120*f6005cafSIan Rogers 	struct thread *result;
121*f6005cafSIan Rogers 
122*f6005cafSIan Rogers 	if (RC_CHK_GET(result, thread))
123ee84a303SIan Rogers 		refcount_inc(thread__refcnt(thread));
124*f6005cafSIan Rogers 
125*f6005cafSIan Rogers 	return result;
126f3b623b8SArnaldo Carvalho de Melo }
127f3b623b8SArnaldo Carvalho de Melo 
128f3b623b8SArnaldo Carvalho de Melo void thread__put(struct thread *thread)
129f3b623b8SArnaldo Carvalho de Melo {
130ee84a303SIan Rogers 	if (thread && refcount_dec_and_test(thread__refcnt(thread)))
131f3b623b8SArnaldo Carvalho de Melo 		thread__delete(thread);
132*f6005cafSIan Rogers 	else
133*f6005cafSIan Rogers 		RC_CHK_PUT(thread);
134f3b623b8SArnaldo Carvalho de Melo }
135f3b623b8SArnaldo Carvalho de Melo 
136ee84a303SIan Rogers static struct namespaces *__thread__namespaces(struct thread *thread)
137f3b3614aSHari Bathini {
138ee84a303SIan Rogers 	if (list_empty(thread__namespaces_list(thread)))
139f3b3614aSHari Bathini 		return NULL;
140f3b3614aSHari Bathini 
141ee84a303SIan Rogers 	return list_first_entry(thread__namespaces_list(thread), struct namespaces, list);
142f3b3614aSHari Bathini }
143f3b3614aSHari Bathini 
1447cb10a08SNamhyung Kim struct namespaces *thread__namespaces(struct thread *thread)
1456584140bSNamhyung Kim {
1466584140bSNamhyung Kim 	struct namespaces *ns;
1476584140bSNamhyung Kim 
148ee84a303SIan Rogers 	down_read(thread__namespaces_lock(thread));
1496584140bSNamhyung Kim 	ns = __thread__namespaces(thread);
150ee84a303SIan Rogers 	up_read(thread__namespaces_lock(thread));
1516584140bSNamhyung Kim 
1526584140bSNamhyung Kim 	return ns;
1536584140bSNamhyung Kim }
1546584140bSNamhyung Kim 
155b32ee9e5SKan Liang static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
15669d81f09SArnaldo Carvalho de Melo 				    struct perf_record_namespaces *event)
157f3b3614aSHari Bathini {
1586584140bSNamhyung Kim 	struct namespaces *new, *curr = __thread__namespaces(thread);
159f3b3614aSHari Bathini 
160f3b3614aSHari Bathini 	new = namespaces__new(event);
161f3b3614aSHari Bathini 	if (!new)
162f3b3614aSHari Bathini 		return -ENOMEM;
163f3b3614aSHari Bathini 
164ee84a303SIan Rogers 	list_add(&new->list, thread__namespaces_list(thread));
165f3b3614aSHari Bathini 
166f3b3614aSHari Bathini 	if (timestamp && curr) {
167f3b3614aSHari Bathini 		/*
168f3b3614aSHari Bathini 		 * setns syscall must have changed few or all the namespaces
169f3b3614aSHari Bathini 		 * of this thread. Update end time for the namespaces
170f3b3614aSHari Bathini 		 * previously used.
171f3b3614aSHari Bathini 		 */
172f3b3614aSHari Bathini 		curr = list_next_entry(new, list);
173f3b3614aSHari Bathini 		curr->end_time = timestamp;
174f3b3614aSHari Bathini 	}
175f3b3614aSHari Bathini 
176f3b3614aSHari Bathini 	return 0;
177f3b3614aSHari Bathini }
178f3b3614aSHari Bathini 
179b32ee9e5SKan Liang int thread__set_namespaces(struct thread *thread, u64 timestamp,
18069d81f09SArnaldo Carvalho de Melo 			   struct perf_record_namespaces *event)
181b32ee9e5SKan Liang {
182b32ee9e5SKan Liang 	int ret;
183b32ee9e5SKan Liang 
184ee84a303SIan Rogers 	down_write(thread__namespaces_lock(thread));
185b32ee9e5SKan Liang 	ret = __thread__set_namespaces(thread, timestamp, event);
186ee84a303SIan Rogers 	up_write(thread__namespaces_lock(thread));
187b32ee9e5SKan Liang 	return ret;
188b32ee9e5SKan Liang }
189b32ee9e5SKan Liang 
190ee84a303SIan Rogers struct comm *thread__comm(struct thread *thread)
1916baa0a5aSFrederic Weisbecker {
192ee84a303SIan Rogers 	if (list_empty(thread__comm_list(thread)))
1931902efe7SFrederic Weisbecker 		return NULL;
1944385d580SDavid S. Miller 
195ee84a303SIan Rogers 	return list_first_entry(thread__comm_list(thread), struct comm, list);
1964385d580SDavid S. Miller }
1971902efe7SFrederic Weisbecker 
198ee84a303SIan Rogers struct comm *thread__exec_comm(struct thread *thread)
19965de51f9SAdrian Hunter {
2003de7ae0bSAdrian Hunter 	struct comm *comm, *last = NULL, *second_last = NULL;
20165de51f9SAdrian Hunter 
202ee84a303SIan Rogers 	list_for_each_entry(comm, thread__comm_list(thread), list) {
20365de51f9SAdrian Hunter 		if (comm->exec)
20465de51f9SAdrian Hunter 			return comm;
2053de7ae0bSAdrian Hunter 		second_last = last;
20665de51f9SAdrian Hunter 		last = comm;
20765de51f9SAdrian Hunter 	}
20865de51f9SAdrian Hunter 
2093de7ae0bSAdrian Hunter 	/*
2103de7ae0bSAdrian Hunter 	 * 'last' with no start time might be the parent's comm of a synthesized
2113de7ae0bSAdrian Hunter 	 * thread (created by processing a synthesized fork event). For a main
2123de7ae0bSAdrian Hunter 	 * thread, that is very probably wrong. Prefer a later comm to avoid
2133de7ae0bSAdrian Hunter 	 * that case.
2143de7ae0bSAdrian Hunter 	 */
215ee84a303SIan Rogers 	if (second_last && !last->start && thread__pid(thread) == thread__tid(thread))
2163de7ae0bSAdrian Hunter 		return second_last;
2173de7ae0bSAdrian Hunter 
21865de51f9SAdrian Hunter 	return last;
21965de51f9SAdrian Hunter }
22065de51f9SAdrian Hunter 
221b32ee9e5SKan Liang static int ____thread__set_comm(struct thread *thread, const char *str,
222b32ee9e5SKan Liang 				u64 timestamp, bool exec)
2231902efe7SFrederic Weisbecker {
2241902efe7SFrederic Weisbecker 	struct comm *new, *curr = thread__comm(thread);
2251902efe7SFrederic Weisbecker 
226a8480808SAdrian Hunter 	/* Override the default :tid entry */
227ee84a303SIan Rogers 	if (!thread__comm_set(thread)) {
22818ef15c6SArnaldo Carvalho de Melo 		int err = comm__override(curr, str, timestamp, exec);
2293178f58bSFrederic Weisbecker 		if (err)
2303178f58bSFrederic Weisbecker 			return err;
231a5285ad9SFrederic Weisbecker 	} else {
23265de51f9SAdrian Hunter 		new = comm__new(str, timestamp, exec);
2331902efe7SFrederic Weisbecker 		if (!new)
2341902efe7SFrederic Weisbecker 			return -ENOMEM;
235ee84a303SIan Rogers 		list_add(&new->list, thread__comm_list(thread));
236380b5143SNamhyung Kim 
237380b5143SNamhyung Kim 		if (exec)
238ee84a303SIan Rogers 			unwind__flush_access(thread__maps(thread));
239a5285ad9SFrederic Weisbecker 	}
240a5285ad9SFrederic Weisbecker 
241ee84a303SIan Rogers 	thread__set_comm_set(thread, true);
2421902efe7SFrederic Weisbecker 
2431902efe7SFrederic Weisbecker 	return 0;
2446baa0a5aSFrederic Weisbecker }
2456baa0a5aSFrederic Weisbecker 
246b32ee9e5SKan Liang int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
247b32ee9e5SKan Liang 		       bool exec)
248b32ee9e5SKan Liang {
249b32ee9e5SKan Liang 	int ret;
250b32ee9e5SKan Liang 
251ee84a303SIan Rogers 	down_write(thread__comm_lock(thread));
252b32ee9e5SKan Liang 	ret = ____thread__set_comm(thread, str, timestamp, exec);
253ee84a303SIan Rogers 	up_write(thread__comm_lock(thread));
254b32ee9e5SKan Liang 	return ret;
255b32ee9e5SKan Liang }
256b32ee9e5SKan Liang 
2572f3027acSArnaldo Carvalho de Melo int thread__set_comm_from_proc(struct thread *thread)
2582f3027acSArnaldo Carvalho de Melo {
2592f3027acSArnaldo Carvalho de Melo 	char path[64];
2602f3027acSArnaldo Carvalho de Melo 	char *comm = NULL;
2612f3027acSArnaldo Carvalho de Melo 	size_t sz;
2622f3027acSArnaldo Carvalho de Melo 	int err = -1;
2632f3027acSArnaldo Carvalho de Melo 
2642f3027acSArnaldo Carvalho de Melo 	if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
265ee84a303SIan Rogers 		       thread__pid(thread), thread__tid(thread)) >= (int)sizeof(path)) &&
2662f3027acSArnaldo Carvalho de Melo 	    procfs__read_str(path, &comm, &sz) == 0) {
2672f3027acSArnaldo Carvalho de Melo 		comm[sz - 1] = '\0';
2682f3027acSArnaldo Carvalho de Melo 		err = thread__set_comm(thread, comm, 0);
2692f3027acSArnaldo Carvalho de Melo 	}
2702f3027acSArnaldo Carvalho de Melo 
2712f3027acSArnaldo Carvalho de Melo 	return err;
2722f3027acSArnaldo Carvalho de Melo }
2732f3027acSArnaldo Carvalho de Melo 
274ee84a303SIan Rogers static const char *__thread__comm_str(struct thread *thread)
275b9c5143aSFrederic Weisbecker {
2761902efe7SFrederic Weisbecker 	const struct comm *comm = thread__comm(thread);
2771902efe7SFrederic Weisbecker 
2781902efe7SFrederic Weisbecker 	if (!comm)
2791902efe7SFrederic Weisbecker 		return NULL;
2801902efe7SFrederic Weisbecker 
2811902efe7SFrederic Weisbecker 	return comm__str(comm);
282b9c5143aSFrederic Weisbecker }
283b9c5143aSFrederic Weisbecker 
2847cb10a08SNamhyung Kim const char *thread__comm_str(struct thread *thread)
285b32ee9e5SKan Liang {
286b32ee9e5SKan Liang 	const char *str;
287b32ee9e5SKan Liang 
288ee84a303SIan Rogers 	down_read(thread__comm_lock(thread));
289b32ee9e5SKan Liang 	str = __thread__comm_str(thread);
290ee84a303SIan Rogers 	up_read(thread__comm_lock(thread));
291b32ee9e5SKan Liang 
292b32ee9e5SKan Liang 	return str;
293b32ee9e5SKan Liang }
294b32ee9e5SKan Liang 
2956e57f69fSliuwenyu static int __thread__comm_len(struct thread *thread, const char *comm)
296a4fb581bSFrederic Weisbecker {
2971902efe7SFrederic Weisbecker 	if (!comm)
298a4fb581bSFrederic Weisbecker 		return 0;
299ee84a303SIan Rogers 	thread__set_comm_len(thread, strlen(comm));
300a4fb581bSFrederic Weisbecker 
301ee84a303SIan Rogers 	return thread__var_comm_len(thread);
302a4fb581bSFrederic Weisbecker }
303a4fb581bSFrederic Weisbecker 
3046e57f69fSliuwenyu /* CHECKME: it should probably better return the max comm len from its comm list */
3056e57f69fSliuwenyu int thread__comm_len(struct thread *thread)
3066e57f69fSliuwenyu {
307ee84a303SIan Rogers 	int comm_len = thread__var_comm_len(thread);
3086e57f69fSliuwenyu 
3096e57f69fSliuwenyu 	if (!comm_len) {
3106e57f69fSliuwenyu 		const char *comm;
3116e57f69fSliuwenyu 
312ee84a303SIan Rogers 		down_read(thread__comm_lock(thread));
3136e57f69fSliuwenyu 		comm = __thread__comm_str(thread);
3146e57f69fSliuwenyu 		comm_len = __thread__comm_len(thread, comm);
315ee84a303SIan Rogers 		up_read(thread__comm_lock(thread));
3166e57f69fSliuwenyu 	}
3176e57f69fSliuwenyu 
3186e57f69fSliuwenyu 	return comm_len;
3196e57f69fSliuwenyu }
3206e57f69fSliuwenyu 
3213f067dcaSArnaldo Carvalho de Melo size_t thread__fprintf(struct thread *thread, FILE *fp)
32295011c60SArnaldo Carvalho de Melo {
323ee84a303SIan Rogers 	return fprintf(fp, "Thread %d %s\n", thread__tid(thread), thread__comm_str(thread)) +
324ee84a303SIan Rogers 	       maps__fprintf(thread__maps(thread), fp);
3256baa0a5aSFrederic Weisbecker }
3266baa0a5aSFrederic Weisbecker 
3278132a2a8SHe Kuang int thread__insert_map(struct thread *thread, struct map *map)
3281b46cddfSArnaldo Carvalho de Melo {
3298132a2a8SHe Kuang 	int ret;
3308132a2a8SHe Kuang 
331ee84a303SIan Rogers 	ret = unwind__prepare_access(thread__maps(thread), map, NULL);
3328132a2a8SHe Kuang 	if (ret)
3338132a2a8SHe Kuang 		return ret;
3348132a2a8SHe Kuang 
335ee84a303SIan Rogers 	maps__fixup_overlappings(thread__maps(thread), map, stderr);
336ee84a303SIan Rogers 	return maps__insert(thread__maps(thread), map);
33795011c60SArnaldo Carvalho de Melo }
33895011c60SArnaldo Carvalho de Melo 
3396c502584SJiri Olsa static int __thread__prepare_access(struct thread *thread)
3406c502584SJiri Olsa {
3416c502584SJiri Olsa 	bool initialized = false;
3423183f8caSArnaldo Carvalho de Melo 	int err = 0;
343ee84a303SIan Rogers 	struct maps *maps = thread__maps(thread);
344ff583dc4SIan Rogers 	struct map_rb_node *rb_node;
3456c502584SJiri Olsa 
3465ab6d715SIan Rogers 	down_read(maps__lock(maps));
3476c502584SJiri Olsa 
348ff583dc4SIan Rogers 	maps__for_each_entry(maps, rb_node) {
349ee84a303SIan Rogers 		err = unwind__prepare_access(thread__maps(thread), rb_node->map, &initialized);
3506c502584SJiri Olsa 		if (err || initialized)
3516c502584SJiri Olsa 			break;
3526c502584SJiri Olsa 	}
3536c502584SJiri Olsa 
3545ab6d715SIan Rogers 	up_read(maps__lock(maps));
3556c502584SJiri Olsa 
3566c502584SJiri Olsa 	return err;
3576c502584SJiri Olsa }
3586c502584SJiri Olsa 
3596c502584SJiri Olsa static int thread__prepare_access(struct thread *thread)
3606c502584SJiri Olsa {
3616c502584SJiri Olsa 	int err = 0;
3626c502584SJiri Olsa 
363382619c0SJiri Olsa 	if (dwarf_callchain_users)
3646c502584SJiri Olsa 		err = __thread__prepare_access(thread);
3656c502584SJiri Olsa 
3666c502584SJiri Olsa 	return err;
3676c502584SJiri Olsa }
3686c502584SJiri Olsa 
36979b6bb73SArnaldo Carvalho de Melo static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone)
370cddcef60SJiri Olsa {
371cddcef60SJiri Olsa 	/* This is new thread, we share map groups for process. */
372ee84a303SIan Rogers 	if (thread__pid(thread) == thread__pid(parent))
3736c502584SJiri Olsa 		return thread__prepare_access(thread);
374cddcef60SJiri Olsa 
375ee84a303SIan Rogers 	if (thread__maps(thread) == thread__maps(parent)) {
3760d7e7accSAdrian Hunter 		pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
377ee84a303SIan Rogers 			 thread__pid(thread), thread__tid(thread),
378ee84a303SIan Rogers 			 thread__pid(parent), thread__tid(parent));
3790d7e7accSAdrian Hunter 		return 0;
3800d7e7accSAdrian Hunter 	}
381cddcef60SJiri Olsa 	/* But this one is new process, copy maps. */
382ee84a303SIan Rogers 	return do_maps_clone ? maps__clone(thread, thread__maps(parent)) : 0;
383cddcef60SJiri Olsa }
384cddcef60SJiri Olsa 
3854f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
3866baa0a5aSFrederic Weisbecker {
387ee84a303SIan Rogers 	if (thread__comm_set(parent)) {
3881902efe7SFrederic Weisbecker 		const char *comm = thread__comm_str(parent);
38918ef15c6SArnaldo Carvalho de Melo 		int err;
3901902efe7SFrederic Weisbecker 		if (!comm)
3916baa0a5aSFrederic Weisbecker 			return -ENOMEM;
3921902efe7SFrederic Weisbecker 		err = thread__set_comm(thread, comm, timestamp);
3938d00be81SDavid Ahern 		if (err)
3941902efe7SFrederic Weisbecker 			return err;
395faa5c5c3SArnaldo Carvalho de Melo 	}
3966baa0a5aSFrederic Weisbecker 
397ee84a303SIan Rogers 	thread__set_ppid(thread, thread__tid(parent));
39879b6bb73SArnaldo Carvalho de Melo 	return thread__clone_maps(thread, parent, do_maps_clone);
3996baa0a5aSFrederic Weisbecker }
40052a3cb8cSArnaldo Carvalho de Melo 
40126bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
40252a3cb8cSArnaldo Carvalho de Melo 					struct addr_location *al)
40352a3cb8cSArnaldo Carvalho de Melo {
40452a3cb8cSArnaldo Carvalho de Melo 	size_t i;
4053b556bceSEric Engestrom 	const u8 cpumodes[] = {
40652a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_USER,
40752a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_KERNEL,
40852a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_USER,
40952a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_KERNEL
41052a3cb8cSArnaldo Carvalho de Melo 	};
41152a3cb8cSArnaldo Carvalho de Melo 
41252a3cb8cSArnaldo Carvalho de Melo 	for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
41326bd9331SArnaldo Carvalho de Melo 		thread__find_symbol(thread, cpumodes[i], addr, al);
41452a3cb8cSArnaldo Carvalho de Melo 		if (al->map)
41552a3cb8cSArnaldo Carvalho de Melo 			break;
41652a3cb8cSArnaldo Carvalho de Melo 	}
41752a3cb8cSArnaldo Carvalho de Melo }
418480ca357SAndi Kleen 
419480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
420480ca357SAndi Kleen {
421ee84a303SIan Rogers 	if (thread__pid(thread) == thread__tid(thread))
422480ca357SAndi Kleen 		return thread__get(thread);
423480ca357SAndi Kleen 
424ee84a303SIan Rogers 	if (thread__pid(thread) == -1)
425480ca357SAndi Kleen 		return NULL;
426480ca357SAndi Kleen 
427ee84a303SIan Rogers 	return machine__find_thread(machine, thread__pid(thread), thread__pid(thread));
428480ca357SAndi Kleen }
42915325938SAndi Kleen 
43015325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine,
43115325938SAndi Kleen 		   void *buf, u64 ip, int len, bool *is64bit)
43215325938SAndi Kleen {
43315325938SAndi Kleen 	u8 cpumode = PERF_RECORD_MISC_USER;
43415325938SAndi Kleen 	struct addr_location al;
43563df0e4bSIan Rogers 	struct dso *dso;
43615325938SAndi Kleen 	long offset;
43715325938SAndi Kleen 
43815325938SAndi Kleen 	if (machine__kernel_ip(machine, ip))
43915325938SAndi Kleen 		cpumode = PERF_RECORD_MISC_KERNEL;
44015325938SAndi Kleen 
4410dd5041cSIan Rogers 	addr_location__init(&al);
4420dd5041cSIan Rogers 	if (!thread__find_map(thread, cpumode, ip, &al)) {
4430dd5041cSIan Rogers 		addr_location__exit(&al);
44463df0e4bSIan Rogers 		return -1;
4450dd5041cSIan Rogers 	}
44663df0e4bSIan Rogers 
44763df0e4bSIan Rogers 	dso = map__dso(al.map);
44863df0e4bSIan Rogers 
4490dd5041cSIan Rogers 	if (!dso || dso->data.status == DSO_DATA_STATUS_ERROR || map__load(al.map) < 0) {
4500dd5041cSIan Rogers 		addr_location__exit(&al);
45115325938SAndi Kleen 		return -1;
4520dd5041cSIan Rogers 	}
45315325938SAndi Kleen 
45478a1f7cdSIan Rogers 	offset = map__map_ip(al.map, ip);
45515325938SAndi Kleen 	if (is64bit)
45663df0e4bSIan Rogers 		*is64bit = dso->is_64_bit;
45715325938SAndi Kleen 
4580dd5041cSIan Rogers 	addr_location__exit(&al);
4590dd5041cSIan Rogers 
46063df0e4bSIan Rogers 	return dso__data_read_offset(dso, machine, offset, buf, len);
46115325938SAndi Kleen }
462ff165628SKan Liang 
463ff165628SKan Liang void thread__free_stitch_list(struct thread *thread)
464ff165628SKan Liang {
465ee84a303SIan Rogers 	struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
466ff165628SKan Liang 	struct stitch_list *pos, *tmp;
467ff165628SKan Liang 
468ff165628SKan Liang 	if (!lbr_stitch)
469ff165628SKan Liang 		return;
470ff165628SKan Liang 
471ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) {
472ff165628SKan Liang 		list_del_init(&pos->node);
473ff165628SKan Liang 		free(pos);
474ff165628SKan Liang 	}
475ff165628SKan Liang 
476ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) {
477ff165628SKan Liang 		list_del_init(&pos->node);
478ff165628SKan Liang 		free(pos);
479ff165628SKan Liang 	}
480ff165628SKan Liang 
481ff165628SKan Liang 	zfree(&lbr_stitch->prev_lbr_cursor);
482ee84a303SIan Rogers 	free(thread__lbr_stitch(thread));
483ee84a303SIan Rogers 	thread__set_lbr_stitch(thread, NULL);
484ff165628SKan Liang }
485