xref: /linux-6.15/tools/perf/util/thread.c (revision 70351029)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2*70351029SIan Rogers #include <elf.h>
3a43783aeSArnaldo Carvalho de Melo #include <errno.h>
4*70351029SIan Rogers #include <fcntl.h>
56baa0a5aSFrederic Weisbecker #include <stdlib.h>
66baa0a5aSFrederic Weisbecker #include <stdio.h>
76baa0a5aSFrederic Weisbecker #include <string.h>
8877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
97f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
104a3cec84SArnaldo Carvalho de Melo #include "dso.h"
11b3165f41SArnaldo Carvalho de Melo #include "session.h"
126baa0a5aSFrederic Weisbecker #include "thread.h"
1300447ccdSAdrian Hunter #include "thread-stack.h"
146e086437SFrederic Weisbecker #include "debug.h"
15f3b3614aSHari Bathini #include "namespaces.h"
161902efe7SFrederic Weisbecker #include "comm.h"
1715325938SAndi Kleen #include "map.h"
18daecf9e0SArnaldo Carvalho de Melo #include "symbol.h"
1966f066d8SNamhyung Kim #include "unwind.h"
20382619c0SJiri Olsa #include "callchain.h"
21*70351029SIan Rogers #include "dwarf-regs.h"
226baa0a5aSFrederic Weisbecker 
232f3027acSArnaldo Carvalho de Melo #include <api/fs/fs.h>
242f3027acSArnaldo Carvalho de Melo 
thread__init_maps(struct thread * thread,struct machine * machine)2579b6bb73SArnaldo Carvalho de Melo int thread__init_maps(struct thread *thread, struct machine *machine)
26cddcef60SJiri Olsa {
27ee84a303SIan Rogers 	pid_t pid = thread__pid(thread);
28cddcef60SJiri Olsa 
29ee84a303SIan Rogers 	if (pid == thread__tid(thread) || pid == -1) {
30ee84a303SIan Rogers 		thread__set_maps(thread, maps__new(machine));
31cddcef60SJiri Olsa 	} else {
32d436f90aSIan Rogers 		struct thread *leader = machine__findnew_thread(machine, pid, pid);
33ee84a303SIan Rogers 
34abd82868SArnaldo Carvalho de Melo 		if (leader) {
35ee84a303SIan Rogers 			thread__set_maps(thread, maps__get(thread__maps(leader)));
36abd82868SArnaldo Carvalho de Melo 			thread__put(leader);
37abd82868SArnaldo Carvalho de Melo 		}
38cddcef60SJiri Olsa 	}
39cddcef60SJiri Olsa 
40ee84a303SIan Rogers 	return thread__maps(thread) ? 0 : -1;
41cddcef60SJiri Olsa }
42cddcef60SJiri Olsa 
thread__new(pid_t pid,pid_t tid)4399d725fcSAdrian Hunter struct thread *thread__new(pid_t pid, pid_t tid)
446baa0a5aSFrederic Weisbecker {
45f6005cafSIan Rogers 	RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread));
46f6005cafSIan Rogers 	struct thread *thread;
476baa0a5aSFrederic Weisbecker 
48f6005cafSIan Rogers 	if (ADD_RC_CHK(thread, _thread) != NULL) {
493536c257SIan Rogers 		struct comm *comm;
503536c257SIan Rogers 		char comm_str[32];
513536c257SIan Rogers 
52ee84a303SIan Rogers 		thread__set_pid(thread, pid);
53ee84a303SIan Rogers 		thread__set_tid(thread, tid);
54ee84a303SIan Rogers 		thread__set_ppid(thread, -1);
55ee84a303SIan Rogers 		thread__set_cpu(thread, -1);
56ee84a303SIan Rogers 		thread__set_guest_cpu(thread, -1);
57*70351029SIan Rogers 		thread__set_e_machine(thread, EM_NONE);
58ee84a303SIan Rogers 		thread__set_lbr_stitch_enable(thread, false);
59ee84a303SIan Rogers 		INIT_LIST_HEAD(thread__namespaces_list(thread));
60ee84a303SIan Rogers 		INIT_LIST_HEAD(thread__comm_list(thread));
61ee84a303SIan Rogers 		init_rwsem(thread__namespaces_lock(thread));
62ee84a303SIan Rogers 		init_rwsem(thread__comm_lock(thread));
631902efe7SFrederic Weisbecker 
643536c257SIan Rogers 		snprintf(comm_str, sizeof(comm_str), ":%d", tid);
6565de51f9SAdrian Hunter 		comm = comm__new(comm_str, 0, false);
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:
793536c257SIan Rogers 	thread__delete(thread);
801902efe7SFrederic Weisbecker 	return NULL;
816baa0a5aSFrederic Weisbecker }
826baa0a5aSFrederic Weisbecker 
8304cb4fc4SArnaldo Carvalho de Melo static void (*thread__priv_destructor)(void *priv);
8404cb4fc4SArnaldo Carvalho de Melo 
thread__set_priv_destructor(void (* destructor)(void * priv))8504cb4fc4SArnaldo Carvalho de Melo void thread__set_priv_destructor(void (*destructor)(void *priv))
8604cb4fc4SArnaldo Carvalho de Melo {
8704cb4fc4SArnaldo Carvalho de Melo 	assert(thread__priv_destructor == NULL);
8804cb4fc4SArnaldo Carvalho de Melo 
8904cb4fc4SArnaldo Carvalho de Melo 	thread__priv_destructor = destructor;
9004cb4fc4SArnaldo Carvalho de Melo }
9104cb4fc4SArnaldo Carvalho de Melo 
thread__delete(struct thread * thread)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);
12404cb4fc4SArnaldo Carvalho de Melo 
12504cb4fc4SArnaldo Carvalho de Melo 	if (thread__priv_destructor)
12604cb4fc4SArnaldo Carvalho de Melo 		thread__priv_destructor(thread__priv(thread));
12704cb4fc4SArnaldo Carvalho de Melo 
128f6005cafSIan Rogers 	RC_CHK_FREE(thread);
129591765fdSArnaldo Carvalho de Melo }
130591765fdSArnaldo Carvalho de Melo 
thread__get(struct thread * thread)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 
thread__put(struct thread * thread)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 
__thread__namespaces(struct thread * thread)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 
thread__namespaces(struct thread * thread)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 
__thread__set_namespaces(struct thread * thread,u64 timestamp,struct perf_record_namespaces * event)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 
thread__set_namespaces(struct thread * thread,u64 timestamp,struct perf_record_namespaces * event)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 
thread__comm(struct thread * thread)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 
thread__exec_comm(struct thread * thread)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 
____thread__set_comm(struct thread * thread,const char * str,u64 timestamp,bool exec)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 
__thread__set_comm(struct thread * thread,const char * str,u64 timestamp,bool exec)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 
thread__set_comm_from_proc(struct thread * thread)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 
__thread__comm_str(struct thread * thread)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 
thread__comm_str(struct thread * thread)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 
__thread__comm_len(struct thread * thread,const char * comm)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 */
thread__comm_len(struct thread * thread)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 
thread__fprintf(struct thread * thread,FILE * fp)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 
thread__insert_map(struct thread * thread,struct map * map)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 
34807ef14d5SIan Rogers 	return maps__fixup_overlap_and_insert(thread__maps(thread), map);
34995011c60SArnaldo Carvalho de Melo }
35095011c60SArnaldo Carvalho de Melo 
35171225af1SIan Rogers struct thread__prepare_access_maps_cb_args {
35271225af1SIan Rogers 	int err;
35371225af1SIan Rogers 	struct maps *maps;
35471225af1SIan Rogers };
35571225af1SIan Rogers 
thread__prepare_access_maps_cb(struct map * map,void * data)35671225af1SIan Rogers static int thread__prepare_access_maps_cb(struct map *map, void *data)
3576c502584SJiri Olsa {
3586c502584SJiri Olsa 	bool initialized = false;
35971225af1SIan Rogers 	struct thread__prepare_access_maps_cb_args *args = data;
3606c502584SJiri Olsa 
36171225af1SIan Rogers 	args->err = unwind__prepare_access(args->maps, map, &initialized);
3626c502584SJiri Olsa 
36371225af1SIan Rogers 	return (args->err || initialized) ? 1 : 0;
3646c502584SJiri Olsa }
3656c502584SJiri Olsa 
thread__prepare_access(struct thread * thread)3666c502584SJiri Olsa static int thread__prepare_access(struct thread *thread)
3676c502584SJiri Olsa {
36871225af1SIan Rogers 	struct thread__prepare_access_maps_cb_args args = {
36971225af1SIan Rogers 		.err = 0,
37071225af1SIan Rogers 	};
3716c502584SJiri Olsa 
37271225af1SIan Rogers 	if (dwarf_callchain_users) {
37371225af1SIan Rogers 		args.maps = thread__maps(thread);
37471225af1SIan Rogers 		maps__for_each_map(thread__maps(thread), thread__prepare_access_maps_cb, &args);
37571225af1SIan Rogers 	}
3766c502584SJiri Olsa 
37771225af1SIan Rogers 	return args.err;
3786c502584SJiri Olsa }
3796c502584SJiri Olsa 
thread__clone_maps(struct thread * thread,struct thread * parent,bool do_maps_clone)38079b6bb73SArnaldo Carvalho de Melo static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone)
381cddcef60SJiri Olsa {
382cddcef60SJiri Olsa 	/* This is new thread, we share map groups for process. */
383ee84a303SIan Rogers 	if (thread__pid(thread) == thread__pid(parent))
3846c502584SJiri Olsa 		return thread__prepare_access(thread);
385cddcef60SJiri Olsa 
386ff0bd799SIan Rogers 	if (maps__equal(thread__maps(thread), thread__maps(parent))) {
3870d7e7accSAdrian Hunter 		pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
388ee84a303SIan Rogers 			 thread__pid(thread), thread__tid(thread),
389ee84a303SIan Rogers 			 thread__pid(parent), thread__tid(parent));
3900d7e7accSAdrian Hunter 		return 0;
3910d7e7accSAdrian Hunter 	}
392cddcef60SJiri Olsa 	/* But this one is new process, copy maps. */
39390849527SIan Rogers 	return do_maps_clone ? maps__copy_from(thread__maps(thread), thread__maps(parent)) : 0;
394cddcef60SJiri Olsa }
395cddcef60SJiri Olsa 
thread__fork(struct thread * thread,struct thread * parent,u64 timestamp,bool do_maps_clone)3964f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
3976baa0a5aSFrederic Weisbecker {
398ee84a303SIan Rogers 	if (thread__comm_set(parent)) {
3991902efe7SFrederic Weisbecker 		const char *comm = thread__comm_str(parent);
40018ef15c6SArnaldo Carvalho de Melo 		int err;
4011902efe7SFrederic Weisbecker 		if (!comm)
4026baa0a5aSFrederic Weisbecker 			return -ENOMEM;
4031902efe7SFrederic Weisbecker 		err = thread__set_comm(thread, comm, timestamp);
4048d00be81SDavid Ahern 		if (err)
4051902efe7SFrederic Weisbecker 			return err;
406faa5c5c3SArnaldo Carvalho de Melo 	}
4076baa0a5aSFrederic Weisbecker 
408ee84a303SIan Rogers 	thread__set_ppid(thread, thread__tid(parent));
40979b6bb73SArnaldo Carvalho de Melo 	return thread__clone_maps(thread, parent, do_maps_clone);
4106baa0a5aSFrederic Weisbecker }
41152a3cb8cSArnaldo Carvalho de Melo 
thread__find_cpumode_addr_location(struct thread * thread,u64 addr,struct addr_location * al)41226bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
41352a3cb8cSArnaldo Carvalho de Melo 					struct addr_location *al)
41452a3cb8cSArnaldo Carvalho de Melo {
41552a3cb8cSArnaldo Carvalho de Melo 	size_t i;
4163b556bceSEric Engestrom 	const u8 cpumodes[] = {
41752a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_USER,
41852a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_KERNEL,
41952a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_USER,
42052a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_KERNEL
42152a3cb8cSArnaldo Carvalho de Melo 	};
42252a3cb8cSArnaldo Carvalho de Melo 
42352a3cb8cSArnaldo Carvalho de Melo 	for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
42426bd9331SArnaldo Carvalho de Melo 		thread__find_symbol(thread, cpumodes[i], addr, al);
42552a3cb8cSArnaldo Carvalho de Melo 		if (al->map)
42652a3cb8cSArnaldo Carvalho de Melo 			break;
42752a3cb8cSArnaldo Carvalho de Melo 	}
42852a3cb8cSArnaldo Carvalho de Melo }
429480ca357SAndi Kleen 
read_proc_e_machine_for_pid(pid_t pid)430*70351029SIan Rogers static uint16_t read_proc_e_machine_for_pid(pid_t pid)
431*70351029SIan Rogers {
432*70351029SIan Rogers 	char path[6 /* "/proc/" */ + 11 /* max length of pid */ + 5 /* "/exe\0" */];
433*70351029SIan Rogers 	int fd;
434*70351029SIan Rogers 	uint16_t e_machine = EM_NONE;
435*70351029SIan Rogers 
436*70351029SIan Rogers 	snprintf(path, sizeof(path), "/proc/%d/exe", pid);
437*70351029SIan Rogers 	fd = open(path, O_RDONLY);
438*70351029SIan Rogers 	if (fd >= 0) {
439*70351029SIan Rogers 		_Static_assert(offsetof(Elf32_Ehdr, e_machine) == 18, "Unexpected offset");
440*70351029SIan Rogers 		_Static_assert(offsetof(Elf64_Ehdr, e_machine) == 18, "Unexpected offset");
441*70351029SIan Rogers 		if (pread(fd, &e_machine, sizeof(e_machine), 18) != sizeof(e_machine))
442*70351029SIan Rogers 			e_machine = EM_NONE;
443*70351029SIan Rogers 		close(fd);
444*70351029SIan Rogers 	}
445*70351029SIan Rogers 	return e_machine;
446*70351029SIan Rogers }
447*70351029SIan Rogers 
thread__e_machine_callback(struct map * map,void * machine)448*70351029SIan Rogers static int thread__e_machine_callback(struct map *map, void *machine)
449*70351029SIan Rogers {
450*70351029SIan Rogers 	struct dso *dso = map__dso(map);
451*70351029SIan Rogers 
452*70351029SIan Rogers 	_Static_assert(0 == EM_NONE, "Unexpected EM_NONE");
453*70351029SIan Rogers 	if (!dso)
454*70351029SIan Rogers 		return EM_NONE;
455*70351029SIan Rogers 
456*70351029SIan Rogers 	return dso__e_machine(dso, machine);
457*70351029SIan Rogers }
458*70351029SIan Rogers 
thread__e_machine(struct thread * thread,struct machine * machine)459*70351029SIan Rogers uint16_t thread__e_machine(struct thread *thread, struct machine *machine)
460*70351029SIan Rogers {
461*70351029SIan Rogers 	pid_t tid, pid;
462*70351029SIan Rogers 	uint16_t e_machine = RC_CHK_ACCESS(thread)->e_machine;
463*70351029SIan Rogers 
464*70351029SIan Rogers 	if (e_machine != EM_NONE)
465*70351029SIan Rogers 		return e_machine;
466*70351029SIan Rogers 
467*70351029SIan Rogers 	tid = thread__tid(thread);
468*70351029SIan Rogers 	pid = thread__pid(thread);
469*70351029SIan Rogers 	if (pid != tid) {
470*70351029SIan Rogers 		struct thread *parent = machine__findnew_thread(machine, pid, pid);
471*70351029SIan Rogers 
472*70351029SIan Rogers 		if (parent) {
473*70351029SIan Rogers 			e_machine = thread__e_machine(parent, machine);
474*70351029SIan Rogers 			thread__set_e_machine(thread, e_machine);
475*70351029SIan Rogers 			return e_machine;
476*70351029SIan Rogers 		}
477*70351029SIan Rogers 		/* Something went wrong, fallback. */
478*70351029SIan Rogers 	}
479*70351029SIan Rogers 	/* Reading on the PID thread. First try to find from the maps. */
480*70351029SIan Rogers 	e_machine = maps__for_each_map(thread__maps(thread),
481*70351029SIan Rogers 				       thread__e_machine_callback,
482*70351029SIan Rogers 				       machine);
483*70351029SIan Rogers 	if (e_machine == EM_NONE) {
484*70351029SIan Rogers 		/* Maps failed, perhaps we're live with map events disabled. */
485*70351029SIan Rogers 		bool is_live = machine->machines == NULL;
486*70351029SIan Rogers 
487*70351029SIan Rogers 		if (!is_live) {
488*70351029SIan Rogers 			/* Check if the session has a data file. */
489*70351029SIan Rogers 			struct perf_session *session = container_of(machine->machines,
490*70351029SIan Rogers 								    struct perf_session,
491*70351029SIan Rogers 								    machines);
492*70351029SIan Rogers 
493*70351029SIan Rogers 			is_live = !!session->data;
494*70351029SIan Rogers 		}
495*70351029SIan Rogers 		/* Read from /proc/pid/exe if live. */
496*70351029SIan Rogers 		if (is_live)
497*70351029SIan Rogers 			e_machine = read_proc_e_machine_for_pid(pid);
498*70351029SIan Rogers 	}
499*70351029SIan Rogers 	if (e_machine != EM_NONE)
500*70351029SIan Rogers 		thread__set_e_machine(thread, e_machine);
501*70351029SIan Rogers 	else
502*70351029SIan Rogers 		e_machine = EM_HOST;
503*70351029SIan Rogers 	return e_machine;
504*70351029SIan Rogers }
505*70351029SIan Rogers 
thread__main_thread(struct machine * machine,struct thread * thread)506480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
507480ca357SAndi Kleen {
508ee84a303SIan Rogers 	if (thread__pid(thread) == thread__tid(thread))
509480ca357SAndi Kleen 		return thread__get(thread);
510480ca357SAndi Kleen 
511ee84a303SIan Rogers 	if (thread__pid(thread) == -1)
512480ca357SAndi Kleen 		return NULL;
513480ca357SAndi Kleen 
514ee84a303SIan Rogers 	return machine__find_thread(machine, thread__pid(thread), thread__pid(thread));
515480ca357SAndi Kleen }
51615325938SAndi Kleen 
thread__memcpy(struct thread * thread,struct machine * machine,void * buf,u64 ip,int len,bool * is64bit)51715325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine,
51815325938SAndi Kleen 		   void *buf, u64 ip, int len, bool *is64bit)
51915325938SAndi Kleen {
52015325938SAndi Kleen 	u8 cpumode = PERF_RECORD_MISC_USER;
52115325938SAndi Kleen 	struct addr_location al;
52263df0e4bSIan Rogers 	struct dso *dso;
52315325938SAndi Kleen 	long offset;
52415325938SAndi Kleen 
52515325938SAndi Kleen 	if (machine__kernel_ip(machine, ip))
52615325938SAndi Kleen 		cpumode = PERF_RECORD_MISC_KERNEL;
52715325938SAndi Kleen 
5280dd5041cSIan Rogers 	addr_location__init(&al);
5290dd5041cSIan Rogers 	if (!thread__find_map(thread, cpumode, ip, &al)) {
5300dd5041cSIan Rogers 		addr_location__exit(&al);
53163df0e4bSIan Rogers 		return -1;
5320dd5041cSIan Rogers 	}
53363df0e4bSIan Rogers 
53463df0e4bSIan Rogers 	dso = map__dso(al.map);
53563df0e4bSIan Rogers 
536ee756ef7SIan Rogers 	if (!dso || dso__data(dso)->status == DSO_DATA_STATUS_ERROR || map__load(al.map) < 0) {
5370dd5041cSIan Rogers 		addr_location__exit(&al);
53815325938SAndi Kleen 		return -1;
5390dd5041cSIan Rogers 	}
54015325938SAndi Kleen 
54178a1f7cdSIan Rogers 	offset = map__map_ip(al.map, ip);
54215325938SAndi Kleen 	if (is64bit)
543ee756ef7SIan Rogers 		*is64bit = dso__is_64_bit(dso);
54415325938SAndi Kleen 
5450dd5041cSIan Rogers 	addr_location__exit(&al);
5460dd5041cSIan Rogers 
54763df0e4bSIan Rogers 	return dso__data_read_offset(dso, machine, offset, buf, len);
54815325938SAndi Kleen }
549ff165628SKan Liang 
thread__free_stitch_list(struct thread * thread)550ff165628SKan Liang void thread__free_stitch_list(struct thread *thread)
551ff165628SKan Liang {
552ee84a303SIan Rogers 	struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread);
553ff165628SKan Liang 	struct stitch_list *pos, *tmp;
554ff165628SKan Liang 
555ff165628SKan Liang 	if (!lbr_stitch)
556ff165628SKan Liang 		return;
557ff165628SKan Liang 
558ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) {
559599c1939SIan Rogers 		map_symbol__exit(&pos->cursor.ms);
560ff165628SKan Liang 		list_del_init(&pos->node);
561ff165628SKan Liang 		free(pos);
562ff165628SKan Liang 	}
563ff165628SKan Liang 
564ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) {
565ff165628SKan Liang 		list_del_init(&pos->node);
566ff165628SKan Liang 		free(pos);
567ff165628SKan Liang 	}
568ff165628SKan Liang 
569599c1939SIan Rogers 	for (unsigned int i = 0 ; i < lbr_stitch->prev_lbr_cursor_size; i++)
570599c1939SIan Rogers 		map_symbol__exit(&lbr_stitch->prev_lbr_cursor[i].ms);
571599c1939SIan Rogers 
572ff165628SKan Liang 	zfree(&lbr_stitch->prev_lbr_cursor);
573ee84a303SIan Rogers 	free(thread__lbr_stitch(thread));
574ee84a303SIan Rogers 	thread__set_lbr_stitch(thread, NULL);
575ff165628SKan Liang }
576