xref: /openbmc/linux/tools/perf/util/thread.c (revision 1e0f696469d6892959e621cebb3d2b722b816951)
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 
thread__init_maps(struct thread * thread,struct machine * machine)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 
thread__new(pid_t pid,pid_t tid)4099d725fcSAdrian Hunter struct thread *thread__new(pid_t pid, pid_t tid)
416baa0a5aSFrederic Weisbecker {
42f6005cafSIan Rogers 	RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread));
43f6005cafSIan Rogers 	struct thread *thread;
446baa0a5aSFrederic Weisbecker 
45f6005cafSIan Rogers 	if (ADD_RC_CHK(thread, _thread) != NULL) {
46*c5314cfaSIan Rogers 		struct comm *comm;
47*c5314cfaSIan Rogers 		char comm_str[32];
48*c5314cfaSIan Rogers 
49ee84a303SIan Rogers 		thread__set_pid(thread, pid);
50ee84a303SIan Rogers 		thread__set_tid(thread, tid);
51ee84a303SIan Rogers 		thread__set_ppid(thread, -1);
52ee84a303SIan Rogers 		thread__set_cpu(thread, -1);
53ee84a303SIan Rogers 		thread__set_guest_cpu(thread, -1);
54ee84a303SIan Rogers 		thread__set_lbr_stitch_enable(thread, false);
55ee84a303SIan Rogers 		INIT_LIST_HEAD(thread__namespaces_list(thread));
56ee84a303SIan Rogers 		INIT_LIST_HEAD(thread__comm_list(thread));
57ee84a303SIan Rogers 		init_rwsem(thread__namespaces_lock(thread));
58ee84a303SIan Rogers 		init_rwsem(thread__comm_lock(thread));
591902efe7SFrederic Weisbecker 
60*c5314cfaSIan Rogers 		snprintf(comm_str, sizeof(comm_str), ":%d", tid);
6165de51f9SAdrian Hunter 		comm = comm__new(comm_str, 0, false);
621902efe7SFrederic Weisbecker 		if (!comm)
631902efe7SFrederic Weisbecker 			goto err_thread;
641902efe7SFrederic Weisbecker 
65ee84a303SIan Rogers 		list_add(&comm->list, thread__comm_list(thread));
66ee84a303SIan Rogers 		refcount_set(thread__refcnt(thread), 1);
67843ff37bSKrister Johansen 		/* Thread holds first ref to nsdata. */
68f6005cafSIan Rogers 		RC_CHK_ACCESS(thread)->nsinfo = nsinfo__new(pid);
69ee84a303SIan Rogers 		srccode_state_init(thread__srccode_state(thread));
706baa0a5aSFrederic Weisbecker 	}
716baa0a5aSFrederic Weisbecker 
72c824c433SArnaldo Carvalho de Melo 	return thread;
731902efe7SFrederic Weisbecker 
741902efe7SFrederic Weisbecker err_thread:
75*c5314cfaSIan Rogers 	thread__delete(thread);
761902efe7SFrederic Weisbecker 	return NULL;
776baa0a5aSFrederic Weisbecker }
786baa0a5aSFrederic Weisbecker 
7904cb4fc4SArnaldo Carvalho de Melo static void (*thread__priv_destructor)(void *priv);
8004cb4fc4SArnaldo Carvalho de Melo 
thread__set_priv_destructor(void (* destructor)(void * priv))8104cb4fc4SArnaldo Carvalho de Melo void thread__set_priv_destructor(void (*destructor)(void *priv))
8204cb4fc4SArnaldo Carvalho de Melo {
8304cb4fc4SArnaldo Carvalho de Melo 	assert(thread__priv_destructor == NULL);
8404cb4fc4SArnaldo Carvalho de Melo 
8504cb4fc4SArnaldo Carvalho de Melo 	thread__priv_destructor = destructor;
8604cb4fc4SArnaldo Carvalho de Melo }
8704cb4fc4SArnaldo Carvalho de Melo 
thread__delete(struct thread * thread)88c824c433SArnaldo Carvalho de Melo void thread__delete(struct thread *thread)
89591765fdSArnaldo Carvalho de Melo {
90f3b3614aSHari Bathini 	struct namespaces *namespaces, *tmp_namespaces;
91f3b3614aSHari Bathini 	struct comm *comm, *tmp_comm;
921902efe7SFrederic Weisbecker 
9300447ccdSAdrian Hunter 	thread_stack__free(thread);
9400447ccdSAdrian Hunter 
95ee84a303SIan Rogers 	if (thread__maps(thread)) {
96ee84a303SIan Rogers 		maps__put(thread__maps(thread));
97ee84a303SIan Rogers 		thread__set_maps(thread, NULL);
989608b84eSAdrian Hunter 	}
99ee84a303SIan Rogers 	down_write(thread__namespaces_lock(thread));
100f3b3614aSHari Bathini 	list_for_each_entry_safe(namespaces, tmp_namespaces,
101ee84a303SIan Rogers 				 thread__namespaces_list(thread), list) {
102e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&namespaces->list);
103f3b3614aSHari Bathini 		namespaces__free(namespaces);
104f3b3614aSHari Bathini 	}
105ee84a303SIan Rogers 	up_write(thread__namespaces_lock(thread));
106b32ee9e5SKan Liang 
107ee84a303SIan Rogers 	down_write(thread__comm_lock(thread));
108ee84a303SIan Rogers 	list_for_each_entry_safe(comm, tmp_comm, thread__comm_list(thread), list) {
109e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&comm->list);
1101902efe7SFrederic Weisbecker 		comm__free(comm);
1111902efe7SFrederic Weisbecker 	}
112ee84a303SIan Rogers 	up_write(thread__comm_lock(thread));
113b32ee9e5SKan Liang 
114f6005cafSIan Rogers 	nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo);
115ee84a303SIan Rogers 	srccode_state_free(thread__srccode_state(thread));
1161902efe7SFrederic Weisbecker 
117ee84a303SIan Rogers 	exit_rwsem(thread__namespaces_lock(thread));
118ee84a303SIan Rogers 	exit_rwsem(thread__comm_lock(thread));
1199c6c3f47SKan Liang 	thread__free_stitch_list(thread);
12004cb4fc4SArnaldo Carvalho de Melo 
12104cb4fc4SArnaldo Carvalho de Melo 	if (thread__priv_destructor)
12204cb4fc4SArnaldo Carvalho de Melo 		thread__priv_destructor(thread__priv(thread));
12304cb4fc4SArnaldo Carvalho de Melo 
124f6005cafSIan Rogers 	RC_CHK_FREE(thread);
125591765fdSArnaldo Carvalho de Melo }
126591765fdSArnaldo Carvalho de Melo 
thread__get(struct thread * thread)127f3b623b8SArnaldo Carvalho de Melo struct thread *thread__get(struct thread *thread)
128f3b623b8SArnaldo Carvalho de Melo {
129f6005cafSIan Rogers 	struct thread *result;
130f6005cafSIan Rogers 
131f6005cafSIan Rogers 	if (RC_CHK_GET(result, thread))
132ee84a303SIan Rogers 		refcount_inc(thread__refcnt(thread));
133f6005cafSIan Rogers 
134f6005cafSIan Rogers 	return result;
135f3b623b8SArnaldo Carvalho de Melo }
136f3b623b8SArnaldo Carvalho de Melo 
thread__put(struct thread * thread)137f3b623b8SArnaldo Carvalho de Melo void thread__put(struct thread *thread)
138f3b623b8SArnaldo Carvalho de Melo {
139ee84a303SIan Rogers 	if (thread && refcount_dec_and_test(thread__refcnt(thread)))
140f3b623b8SArnaldo Carvalho de Melo 		thread__delete(thread);
141f6005cafSIan Rogers 	else
142f6005cafSIan Rogers 		RC_CHK_PUT(thread);
143f3b623b8SArnaldo Carvalho de Melo }
144f3b623b8SArnaldo Carvalho de Melo 
__thread__namespaces(struct thread * thread)145ee84a303SIan Rogers static struct namespaces *__thread__namespaces(struct thread *thread)
146f3b3614aSHari Bathini {
147ee84a303SIan Rogers 	if (list_empty(thread__namespaces_list(thread)))
148f3b3614aSHari Bathini 		return NULL;
149f3b3614aSHari Bathini 
150ee84a303SIan Rogers 	return list_first_entry(thread__namespaces_list(thread), struct namespaces, list);
151f3b3614aSHari Bathini }
152f3b3614aSHari Bathini 
thread__namespaces(struct thread * thread)1537cb10a08SNamhyung Kim struct namespaces *thread__namespaces(struct thread *thread)
1546584140bSNamhyung Kim {
1556584140bSNamhyung Kim 	struct namespaces *ns;
1566584140bSNamhyung Kim 
157ee84a303SIan Rogers 	down_read(thread__namespaces_lock(thread));
1586584140bSNamhyung Kim 	ns = __thread__namespaces(thread);
159ee84a303SIan Rogers 	up_read(thread__namespaces_lock(thread));
1606584140bSNamhyung Kim 
1616584140bSNamhyung Kim 	return ns;
1626584140bSNamhyung Kim }
1636584140bSNamhyung Kim 
__thread__set_namespaces(struct thread * thread,u64 timestamp,struct perf_record_namespaces * event)164b32ee9e5SKan Liang static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
16569d81f09SArnaldo Carvalho de Melo 				    struct perf_record_namespaces *event)
166f3b3614aSHari Bathini {
1676584140bSNamhyung Kim 	struct namespaces *new, *curr = __thread__namespaces(thread);
168f3b3614aSHari Bathini 
169f3b3614aSHari Bathini 	new = namespaces__new(event);
170f3b3614aSHari Bathini 	if (!new)
171f3b3614aSHari Bathini 		return -ENOMEM;
172f3b3614aSHari Bathini 
173ee84a303SIan Rogers 	list_add(&new->list, thread__namespaces_list(thread));
174f3b3614aSHari Bathini 
175f3b3614aSHari Bathini 	if (timestamp && curr) {
176f3b3614aSHari Bathini 		/*
177f3b3614aSHari Bathini 		 * setns syscall must have changed few or all the namespaces
178f3b3614aSHari Bathini 		 * of this thread. Update end time for the namespaces
179f3b3614aSHari Bathini 		 * previously used.
180f3b3614aSHari Bathini 		 */
181f3b3614aSHari Bathini 		curr = list_next_entry(new, list);
182f3b3614aSHari Bathini 		curr->end_time = timestamp;
183f3b3614aSHari Bathini 	}
184f3b3614aSHari Bathini 
185f3b3614aSHari Bathini 	return 0;
186f3b3614aSHari Bathini }
187f3b3614aSHari Bathini 
thread__set_namespaces(struct thread * thread,u64 timestamp,struct perf_record_namespaces * event)188b32ee9e5SKan Liang int thread__set_namespaces(struct thread *thread, u64 timestamp,
18969d81f09SArnaldo Carvalho de Melo 			   struct perf_record_namespaces *event)
190b32ee9e5SKan Liang {
191b32ee9e5SKan Liang 	int ret;
192b32ee9e5SKan Liang 
193ee84a303SIan Rogers 	down_write(thread__namespaces_lock(thread));
194b32ee9e5SKan Liang 	ret = __thread__set_namespaces(thread, timestamp, event);
195ee84a303SIan Rogers 	up_write(thread__namespaces_lock(thread));
196b32ee9e5SKan Liang 	return ret;
197b32ee9e5SKan Liang }
198b32ee9e5SKan Liang 
thread__comm(struct thread * thread)199ee84a303SIan Rogers struct comm *thread__comm(struct thread *thread)
2006baa0a5aSFrederic Weisbecker {
201ee84a303SIan Rogers 	if (list_empty(thread__comm_list(thread)))
2021902efe7SFrederic Weisbecker 		return NULL;
2034385d580SDavid S. Miller 
204ee84a303SIan Rogers 	return list_first_entry(thread__comm_list(thread), struct comm, list);
2054385d580SDavid S. Miller }
2061902efe7SFrederic Weisbecker 
thread__exec_comm(struct thread * thread)207ee84a303SIan Rogers struct comm *thread__exec_comm(struct thread *thread)
20865de51f9SAdrian Hunter {
2093de7ae0bSAdrian Hunter 	struct comm *comm, *last = NULL, *second_last = NULL;
21065de51f9SAdrian Hunter 
211ee84a303SIan Rogers 	list_for_each_entry(comm, thread__comm_list(thread), list) {
21265de51f9SAdrian Hunter 		if (comm->exec)
21365de51f9SAdrian Hunter 			return comm;
2143de7ae0bSAdrian Hunter 		second_last = last;
21565de51f9SAdrian Hunter 		last = comm;
21665de51f9SAdrian Hunter 	}
21765de51f9SAdrian Hunter 
2183de7ae0bSAdrian Hunter 	/*
2193de7ae0bSAdrian Hunter 	 * 'last' with no start time might be the parent's comm of a synthesized
2203de7ae0bSAdrian Hunter 	 * thread (created by processing a synthesized fork event). For a main
2213de7ae0bSAdrian Hunter 	 * thread, that is very probably wrong. Prefer a later comm to avoid
2223de7ae0bSAdrian Hunter 	 * that case.
2233de7ae0bSAdrian Hunter 	 */
224ee84a303SIan Rogers 	if (second_last && !last->start && thread__pid(thread) == thread__tid(thread))
2253de7ae0bSAdrian Hunter 		return second_last;
2263de7ae0bSAdrian Hunter 
22765de51f9SAdrian Hunter 	return last;
22865de51f9SAdrian Hunter }
22965de51f9SAdrian Hunter 
____thread__set_comm(struct thread * thread,const char * str,u64 timestamp,bool exec)230b32ee9e5SKan Liang static int ____thread__set_comm(struct thread *thread, const char *str,
231b32ee9e5SKan Liang 				u64 timestamp, bool exec)
2321902efe7SFrederic Weisbecker {
2331902efe7SFrederic Weisbecker 	struct comm *new, *curr = thread__comm(thread);
2341902efe7SFrederic Weisbecker 
235a8480808SAdrian Hunter 	/* Override the default :tid entry */
236ee84a303SIan Rogers 	if (!thread__comm_set(thread)) {
23718ef15c6SArnaldo Carvalho de Melo 		int err = comm__override(curr, str, timestamp, exec);
2383178f58bSFrederic Weisbecker 		if (err)
2393178f58bSFrederic Weisbecker 			return err;
240a5285ad9SFrederic Weisbecker 	} else {
24165de51f9SAdrian Hunter 		new = comm__new(str, timestamp, exec);
2421902efe7SFrederic Weisbecker 		if (!new)
2431902efe7SFrederic Weisbecker 			return -ENOMEM;
244ee84a303SIan Rogers 		list_add(&new->list, thread__comm_list(thread));
245380b5143SNamhyung Kim 
246380b5143SNamhyung Kim 		if (exec)
247ee84a303SIan Rogers 			unwind__flush_access(thread__maps(thread));
248a5285ad9SFrederic Weisbecker 	}
249a5285ad9SFrederic Weisbecker 
250ee84a303SIan Rogers 	thread__set_comm_set(thread, true);
2511902efe7SFrederic Weisbecker 
2521902efe7SFrederic Weisbecker 	return 0;
2536baa0a5aSFrederic Weisbecker }
2546baa0a5aSFrederic Weisbecker 
__thread__set_comm(struct thread * thread,const char * str,u64 timestamp,bool exec)255b32ee9e5SKan Liang int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
256b32ee9e5SKan Liang 		       bool exec)
257b32ee9e5SKan Liang {
258b32ee9e5SKan Liang 	int ret;
259b32ee9e5SKan Liang 
260ee84a303SIan Rogers 	down_write(thread__comm_lock(thread));
261b32ee9e5SKan Liang 	ret = ____thread__set_comm(thread, str, timestamp, exec);
262ee84a303SIan Rogers 	up_write(thread__comm_lock(thread));
263b32ee9e5SKan Liang 	return ret;
264b32ee9e5SKan Liang }
265b32ee9e5SKan Liang 
thread__set_comm_from_proc(struct thread * thread)2662f3027acSArnaldo Carvalho de Melo int thread__set_comm_from_proc(struct thread *thread)
2672f3027acSArnaldo Carvalho de Melo {
2682f3027acSArnaldo Carvalho de Melo 	char path[64];
2692f3027acSArnaldo Carvalho de Melo 	char *comm = NULL;
2702f3027acSArnaldo Carvalho de Melo 	size_t sz;
2712f3027acSArnaldo Carvalho de Melo 	int err = -1;
2722f3027acSArnaldo Carvalho de Melo 
2732f3027acSArnaldo Carvalho de Melo 	if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
274ee84a303SIan Rogers 		       thread__pid(thread), thread__tid(thread)) >= (int)sizeof(path)) &&
2752f3027acSArnaldo Carvalho de Melo 	    procfs__read_str(path, &comm, &sz) == 0) {
2762f3027acSArnaldo Carvalho de Melo 		comm[sz - 1] = '\0';
2772f3027acSArnaldo Carvalho de Melo 		err = thread__set_comm(thread, comm, 0);
2782f3027acSArnaldo Carvalho de Melo 	}
2792f3027acSArnaldo Carvalho de Melo 
2802f3027acSArnaldo Carvalho de Melo 	return err;
2812f3027acSArnaldo Carvalho de Melo }
2822f3027acSArnaldo Carvalho de Melo 
__thread__comm_str(struct thread * thread)283ee84a303SIan Rogers static const char *__thread__comm_str(struct thread *thread)
284b9c5143aSFrederic Weisbecker {
2851902efe7SFrederic Weisbecker 	const struct comm *comm = thread__comm(thread);
2861902efe7SFrederic Weisbecker 
2871902efe7SFrederic Weisbecker 	if (!comm)
2881902efe7SFrederic Weisbecker 		return NULL;
2891902efe7SFrederic Weisbecker 
2901902efe7SFrederic Weisbecker 	return comm__str(comm);
291b9c5143aSFrederic Weisbecker }
292b9c5143aSFrederic Weisbecker 
thread__comm_str(struct thread * thread)2937cb10a08SNamhyung Kim const char *thread__comm_str(struct thread *thread)
294b32ee9e5SKan Liang {
295b32ee9e5SKan Liang 	const char *str;
296b32ee9e5SKan Liang 
297ee84a303SIan Rogers 	down_read(thread__comm_lock(thread));
298b32ee9e5SKan Liang 	str = __thread__comm_str(thread);
299ee84a303SIan Rogers 	up_read(thread__comm_lock(thread));
300b32ee9e5SKan Liang 
301b32ee9e5SKan Liang 	return str;
302b32ee9e5SKan Liang }
303b32ee9e5SKan Liang 
__thread__comm_len(struct thread * thread,const char * comm)3046e57f69fSliuwenyu static int __thread__comm_len(struct thread *thread, const char *comm)
305a4fb581bSFrederic Weisbecker {
3061902efe7SFrederic Weisbecker 	if (!comm)
307a4fb581bSFrederic Weisbecker 		return 0;
308ee84a303SIan Rogers 	thread__set_comm_len(thread, strlen(comm));
309a4fb581bSFrederic Weisbecker 
310ee84a303SIan Rogers 	return thread__var_comm_len(thread);
311a4fb581bSFrederic Weisbecker }
312a4fb581bSFrederic Weisbecker 
3136e57f69fSliuwenyu /* CHECKME: it should probably better return the max comm len from its comm list */
thread__comm_len(struct thread * thread)3146e57f69fSliuwenyu int thread__comm_len(struct thread *thread)
3156e57f69fSliuwenyu {
316ee84a303SIan Rogers 	int comm_len = thread__var_comm_len(thread);
3176e57f69fSliuwenyu 
3186e57f69fSliuwenyu 	if (!comm_len) {
3196e57f69fSliuwenyu 		const char *comm;
3206e57f69fSliuwenyu 
321ee84a303SIan Rogers 		down_read(thread__comm_lock(thread));
3226e57f69fSliuwenyu 		comm = __thread__comm_str(thread);
3236e57f69fSliuwenyu 		comm_len = __thread__comm_len(thread, comm);
324ee84a303SIan Rogers 		up_read(thread__comm_lock(thread));
3256e57f69fSliuwenyu 	}
3266e57f69fSliuwenyu 
3276e57f69fSliuwenyu 	return comm_len;
3286e57f69fSliuwenyu }
3296e57f69fSliuwenyu 
thread__fprintf(struct thread * thread,FILE * fp)3303f067dcaSArnaldo Carvalho de Melo size_t thread__fprintf(struct thread *thread, FILE *fp)
33195011c60SArnaldo Carvalho de Melo {
332ee84a303SIan Rogers 	return fprintf(fp, "Thread %d %s\n", thread__tid(thread), thread__comm_str(thread)) +
333ee84a303SIan Rogers 	       maps__fprintf(thread__maps(thread), fp);
3346baa0a5aSFrederic Weisbecker }
3356baa0a5aSFrederic Weisbecker 
thread__insert_map(struct thread * thread,struct map * map)3368132a2a8SHe Kuang int thread__insert_map(struct thread *thread, struct map *map)
3371b46cddfSArnaldo Carvalho de Melo {
3388132a2a8SHe Kuang 	int ret;
3398132a2a8SHe Kuang 
340ee84a303SIan Rogers 	ret = unwind__prepare_access(thread__maps(thread), map, NULL);
3418132a2a8SHe Kuang 	if (ret)
3428132a2a8SHe Kuang 		return ret;
3438132a2a8SHe Kuang 
344ee84a303SIan Rogers 	maps__fixup_overlappings(thread__maps(thread), map, stderr);
345ee84a303SIan Rogers 	return maps__insert(thread__maps(thread), map);
34695011c60SArnaldo Carvalho de Melo }
34795011c60SArnaldo Carvalho de Melo 
__thread__prepare_access(struct thread * thread)3486c502584SJiri Olsa static int __thread__prepare_access(struct thread *thread)
3496c502584SJiri Olsa {
3506c502584SJiri Olsa 	bool initialized = false;
3513183f8caSArnaldo Carvalho de Melo 	int err = 0;
352ee84a303SIan Rogers 	struct maps *maps = thread__maps(thread);
353ff583dc4SIan Rogers 	struct map_rb_node *rb_node;
3546c502584SJiri Olsa 
3555ab6d715SIan Rogers 	down_read(maps__lock(maps));
3566c502584SJiri Olsa 
357ff583dc4SIan Rogers 	maps__for_each_entry(maps, rb_node) {
358ee84a303SIan Rogers 		err = unwind__prepare_access(thread__maps(thread), rb_node->map, &initialized);
3596c502584SJiri Olsa 		if (err || initialized)
3606c502584SJiri Olsa 			break;
3616c502584SJiri Olsa 	}
3626c502584SJiri Olsa 
3635ab6d715SIan Rogers 	up_read(maps__lock(maps));
3646c502584SJiri Olsa 
3656c502584SJiri Olsa 	return err;
3666c502584SJiri Olsa }
3676c502584SJiri Olsa 
thread__prepare_access(struct thread * thread)3686c502584SJiri Olsa static int thread__prepare_access(struct thread *thread)
3696c502584SJiri Olsa {
3706c502584SJiri Olsa 	int err = 0;
3716c502584SJiri Olsa 
372382619c0SJiri Olsa 	if (dwarf_callchain_users)
3736c502584SJiri Olsa 		err = __thread__prepare_access(thread);
3746c502584SJiri Olsa 
3756c502584SJiri Olsa 	return err;
3766c502584SJiri Olsa }
3776c502584SJiri Olsa 
thread__clone_maps(struct thread * thread,struct thread * parent,bool do_maps_clone)37879b6bb73SArnaldo Carvalho de Melo static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone)
379cddcef60SJiri Olsa {
380cddcef60SJiri Olsa 	/* This is new thread, we share map groups for process. */
381ee84a303SIan Rogers 	if (thread__pid(thread) == thread__pid(parent))
3826c502584SJiri Olsa 		return thread__prepare_access(thread);
383cddcef60SJiri Olsa 
384ee84a303SIan Rogers 	if (thread__maps(thread) == thread__maps(parent)) {
3850d7e7accSAdrian Hunter 		pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
386ee84a303SIan Rogers 			 thread__pid(thread), thread__tid(thread),
387ee84a303SIan Rogers 			 thread__pid(parent), thread__tid(parent));
3880d7e7accSAdrian Hunter 		return 0;
3890d7e7accSAdrian Hunter 	}
390cddcef60SJiri Olsa 	/* But this one is new process, copy maps. */
391ee84a303SIan Rogers 	return do_maps_clone ? maps__clone(thread, thread__maps(parent)) : 0;
392cddcef60SJiri Olsa }
393cddcef60SJiri Olsa 
thread__fork(struct thread * thread,struct thread * parent,u64 timestamp,bool do_maps_clone)3944f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
3956baa0a5aSFrederic Weisbecker {
396ee84a303SIan Rogers 	if (thread__comm_set(parent)) {
3971902efe7SFrederic Weisbecker 		const char *comm = thread__comm_str(parent);
39818ef15c6SArnaldo Carvalho de Melo 		int err;
3991902efe7SFrederic Weisbecker 		if (!comm)
4006baa0a5aSFrederic Weisbecker 			return -ENOMEM;
4011902efe7SFrederic Weisbecker 		err = thread__set_comm(thread, comm, timestamp);
4028d00be81SDavid Ahern 		if (err)
4031902efe7SFrederic Weisbecker 			return err;
404faa5c5c3SArnaldo Carvalho de Melo 	}
4056baa0a5aSFrederic Weisbecker 
406ee84a303SIan Rogers 	thread__set_ppid(thread, thread__tid(parent));
40779b6bb73SArnaldo Carvalho de Melo 	return thread__clone_maps(thread, parent, do_maps_clone);
4086baa0a5aSFrederic Weisbecker }
40952a3cb8cSArnaldo Carvalho de Melo 
thread__find_cpumode_addr_location(struct thread * thread,u64 addr,struct addr_location * al)41026bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
41152a3cb8cSArnaldo Carvalho de Melo 					struct addr_location *al)
41252a3cb8cSArnaldo Carvalho de Melo {
41352a3cb8cSArnaldo Carvalho de Melo 	size_t i;
4143b556bceSEric Engestrom 	const u8 cpumodes[] = {
41552a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_USER,
41652a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_KERNEL,
41752a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_USER,
41852a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_KERNEL
41952a3cb8cSArnaldo Carvalho de Melo 	};
42052a3cb8cSArnaldo Carvalho de Melo 
42152a3cb8cSArnaldo Carvalho de Melo 	for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
42226bd9331SArnaldo Carvalho de Melo 		thread__find_symbol(thread, cpumodes[i], addr, al);
42352a3cb8cSArnaldo Carvalho de Melo 		if (al->map)
42452a3cb8cSArnaldo Carvalho de Melo 			break;
42552a3cb8cSArnaldo Carvalho de Melo 	}
42652a3cb8cSArnaldo Carvalho de Melo }
427480ca357SAndi Kleen 
thread__main_thread(struct machine * machine,struct thread * thread)428480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
429480ca357SAndi Kleen {
430ee84a303SIan Rogers 	if (thread__pid(thread) == thread__tid(thread))
431480ca357SAndi Kleen 		return thread__get(thread);
432480ca357SAndi Kleen 
433ee84a303SIan Rogers 	if (thread__pid(thread) == -1)
434480ca357SAndi Kleen 		return NULL;
435480ca357SAndi Kleen 
436ee84a303SIan Rogers 	return machine__find_thread(machine, thread__pid(thread), thread__pid(thread));
437480ca357SAndi Kleen }
43815325938SAndi Kleen 
thread__memcpy(struct thread * thread,struct machine * machine,void * buf,u64 ip,int len,bool * is64bit)43915325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine,
44015325938SAndi Kleen 		   void *buf, u64 ip, int len, bool *is64bit)
44115325938SAndi Kleen {
44215325938SAndi Kleen 	u8 cpumode = PERF_RECORD_MISC_USER;
44315325938SAndi Kleen 	struct addr_location al;
44463df0e4bSIan Rogers 	struct dso *dso;
44515325938SAndi Kleen 	long offset;
44615325938SAndi Kleen 
44715325938SAndi Kleen 	if (machine__kernel_ip(machine, ip))
44815325938SAndi Kleen 		cpumode = PERF_RECORD_MISC_KERNEL;
44915325938SAndi Kleen 
4500dd5041cSIan Rogers 	addr_location__init(&al);
4510dd5041cSIan Rogers 	if (!thread__find_map(thread, cpumode, ip, &al)) {
4520dd5041cSIan Rogers 		addr_location__exit(&al);
45363df0e4bSIan Rogers 		return -1;
4540dd5041cSIan Rogers 	}
45563df0e4bSIan Rogers 
45663df0e4bSIan Rogers 	dso = map__dso(al.map);
45763df0e4bSIan Rogers 
4580dd5041cSIan Rogers 	if (!dso || dso->data.status == DSO_DATA_STATUS_ERROR || map__load(al.map) < 0) {
4590dd5041cSIan Rogers 		addr_location__exit(&al);
46015325938SAndi Kleen 		return -1;
4610dd5041cSIan Rogers 	}
46215325938SAndi Kleen 
46378a1f7cdSIan Rogers 	offset = map__map_ip(al.map, ip);
46415325938SAndi Kleen 	if (is64bit)
46563df0e4bSIan Rogers 		*is64bit = dso->is_64_bit;
46615325938SAndi Kleen 
4670dd5041cSIan Rogers 	addr_location__exit(&al);
4680dd5041cSIan Rogers 
46963df0e4bSIan Rogers 	return dso__data_read_offset(dso, machine, offset, buf, len);
47015325938SAndi Kleen }
471ff165628SKan Liang 
thread__free_stitch_list(struct thread * thread)472ff165628SKan Liang void thread__free_stitch_list(struct thread *thread)
473ff165628SKan Liang {
474ee84a303SIan Rogers 	struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
475ff165628SKan Liang 	struct stitch_list *pos, *tmp;
476ff165628SKan Liang 
477ff165628SKan Liang 	if (!lbr_stitch)
478ff165628SKan Liang 		return;
479ff165628SKan Liang 
480ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) {
481ff165628SKan Liang 		list_del_init(&pos->node);
482ff165628SKan Liang 		free(pos);
483ff165628SKan Liang 	}
484ff165628SKan Liang 
485ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) {
486ff165628SKan Liang 		list_del_init(&pos->node);
487ff165628SKan Liang 		free(pos);
488ff165628SKan Liang 	}
489ff165628SKan Liang 
490ff165628SKan Liang 	zfree(&lbr_stitch->prev_lbr_cursor);
491ee84a303SIan Rogers 	free(thread__lbr_stitch(thread));
492ee84a303SIan Rogers 	thread__set_lbr_stitch(thread, NULL);
493ff165628SKan Liang }
494