xref: /openbmc/linux/tools/perf/util/thread.c (revision ff583dc43dc8b62c9ec639604adae641ba38a91d)
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 {
24cddcef60SJiri Olsa 	pid_t pid = thread->pid_;
25cddcef60SJiri Olsa 
261fcb8768SAdrian Hunter 	if (pid == thread->tid || pid == -1) {
27fe87797dSArnaldo Carvalho de Melo 		thread->maps = maps__new(machine);
28cddcef60SJiri Olsa 	} else {
2918ef15c6SArnaldo Carvalho de Melo 		struct thread *leader = __machine__findnew_thread(machine, pid, pid);
30abd82868SArnaldo Carvalho de Melo 		if (leader) {
31fe87797dSArnaldo Carvalho de Melo 			thread->maps = maps__get(leader->maps);
32abd82868SArnaldo Carvalho de Melo 			thread__put(leader);
33abd82868SArnaldo Carvalho de Melo 		}
34cddcef60SJiri Olsa 	}
35cddcef60SJiri Olsa 
36fe87797dSArnaldo Carvalho de Melo 	return thread->maps ? 0 : -1;
37cddcef60SJiri Olsa }
38cddcef60SJiri Olsa 
3999d725fcSAdrian Hunter struct thread *thread__new(pid_t pid, pid_t tid)
406baa0a5aSFrederic Weisbecker {
411902efe7SFrederic Weisbecker 	char *comm_str;
421902efe7SFrederic Weisbecker 	struct comm *comm;
43c824c433SArnaldo Carvalho de Melo 	struct thread *thread = zalloc(sizeof(*thread));
446baa0a5aSFrederic Weisbecker 
45c824c433SArnaldo Carvalho de Melo 	if (thread != NULL) {
46c824c433SArnaldo Carvalho de Melo 		thread->pid_ = pid;
47c824c433SArnaldo Carvalho de Melo 		thread->tid = tid;
48c824c433SArnaldo Carvalho de Melo 		thread->ppid = -1;
49bf49c35fSAdrian Hunter 		thread->cpu = -1;
50797efbc5SAdrian Hunter 		thread->guest_cpu = -1;
51771fd155SKan Liang 		thread->lbr_stitch_enable = false;
52f3b3614aSHari Bathini 		INIT_LIST_HEAD(&thread->namespaces_list);
531902efe7SFrederic Weisbecker 		INIT_LIST_HEAD(&thread->comm_list);
54b32ee9e5SKan Liang 		init_rwsem(&thread->namespaces_lock);
55b32ee9e5SKan Liang 		init_rwsem(&thread->comm_lock);
561902efe7SFrederic Weisbecker 
571902efe7SFrederic Weisbecker 		comm_str = malloc(32);
581902efe7SFrederic Weisbecker 		if (!comm_str)
591902efe7SFrederic Weisbecker 			goto err_thread;
601902efe7SFrederic Weisbecker 
611902efe7SFrederic Weisbecker 		snprintf(comm_str, 32, ":%d", tid);
6265de51f9SAdrian Hunter 		comm = comm__new(comm_str, 0, false);
631902efe7SFrederic Weisbecker 		free(comm_str);
641902efe7SFrederic Weisbecker 		if (!comm)
651902efe7SFrederic Weisbecker 			goto err_thread;
661902efe7SFrederic Weisbecker 
671902efe7SFrederic Weisbecker 		list_add(&comm->list, &thread->comm_list);
68e34f5b11SElena Reshetova 		refcount_set(&thread->refcnt, 1);
69b91fc39fSArnaldo Carvalho de Melo 		RB_CLEAR_NODE(&thread->rb_node);
70843ff37bSKrister Johansen 		/* Thread holds first ref to nsdata. */
71843ff37bSKrister Johansen 		thread->nsinfo = nsinfo__new(pid);
72dd2e18e9SAndi Kleen 		srccode_state_init(&thread->srccode_state);
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 
87b91fc39fSArnaldo Carvalho de Melo 	BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
88b91fc39fSArnaldo Carvalho de Melo 
8900447ccdSAdrian Hunter 	thread_stack__free(thread);
9000447ccdSAdrian Hunter 
91fe87797dSArnaldo Carvalho de Melo 	if (thread->maps) {
92fe87797dSArnaldo Carvalho de Melo 		maps__put(thread->maps);
93fe87797dSArnaldo Carvalho de Melo 		thread->maps = NULL;
949608b84eSAdrian Hunter 	}
95b32ee9e5SKan Liang 	down_write(&thread->namespaces_lock);
96f3b3614aSHari Bathini 	list_for_each_entry_safe(namespaces, tmp_namespaces,
97f3b3614aSHari Bathini 				 &thread->namespaces_list, list) {
98e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&namespaces->list);
99f3b3614aSHari Bathini 		namespaces__free(namespaces);
100f3b3614aSHari Bathini 	}
101b32ee9e5SKan Liang 	up_write(&thread->namespaces_lock);
102b32ee9e5SKan Liang 
103b32ee9e5SKan Liang 	down_write(&thread->comm_lock);
104f3b3614aSHari Bathini 	list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
105e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&comm->list);
1061902efe7SFrederic Weisbecker 		comm__free(comm);
1071902efe7SFrederic Weisbecker 	}
108b32ee9e5SKan Liang 	up_write(&thread->comm_lock);
109b32ee9e5SKan Liang 
110843ff37bSKrister Johansen 	nsinfo__zput(thread->nsinfo);
111dd2e18e9SAndi Kleen 	srccode_state_free(&thread->srccode_state);
1121902efe7SFrederic Weisbecker 
113b32ee9e5SKan Liang 	exit_rwsem(&thread->namespaces_lock);
114b32ee9e5SKan Liang 	exit_rwsem(&thread->comm_lock);
1159c6c3f47SKan Liang 	thread__free_stitch_list(thread);
116c824c433SArnaldo Carvalho de Melo 	free(thread);
117591765fdSArnaldo Carvalho de Melo }
118591765fdSArnaldo Carvalho de Melo 
119f3b623b8SArnaldo Carvalho de Melo struct thread *thread__get(struct thread *thread)
120f3b623b8SArnaldo Carvalho de Melo {
121b91fc39fSArnaldo Carvalho de Melo 	if (thread)
122e34f5b11SElena Reshetova 		refcount_inc(&thread->refcnt);
123f3b623b8SArnaldo Carvalho de Melo 	return thread;
124f3b623b8SArnaldo Carvalho de Melo }
125f3b623b8SArnaldo Carvalho de Melo 
126f3b623b8SArnaldo Carvalho de Melo void thread__put(struct thread *thread)
127f3b623b8SArnaldo Carvalho de Melo {
128e34f5b11SElena Reshetova 	if (thread && refcount_dec_and_test(&thread->refcnt)) {
129abd82868SArnaldo Carvalho de Melo 		/*
1304c00af0eSArnaldo Carvalho de Melo 		 * Remove it from the dead threads list, as last reference is
1314c00af0eSArnaldo Carvalho de Melo 		 * gone, if it is in a dead threads list.
1324c00af0eSArnaldo Carvalho de Melo 		 *
1334c00af0eSArnaldo Carvalho de Melo 		 * We may not be there anymore if say, the machine where it was
1344c00af0eSArnaldo Carvalho de Melo 		 * stored was already deleted, so we already removed it from
1354c00af0eSArnaldo Carvalho de Melo 		 * the dead threads and some other piece of code still keeps a
1364c00af0eSArnaldo Carvalho de Melo 		 * reference.
1374c00af0eSArnaldo Carvalho de Melo 		 *
1384c00af0eSArnaldo Carvalho de Melo 		 * This is what 'perf sched' does and finally drops it in
1394c00af0eSArnaldo Carvalho de Melo 		 * perf_sched__lat(), where it calls perf_sched__read_events(),
1404c00af0eSArnaldo Carvalho de Melo 		 * that processes the events by creating a session and deleting
1414c00af0eSArnaldo Carvalho de Melo 		 * it, which ends up destroying the list heads for the dead
1424c00af0eSArnaldo Carvalho de Melo 		 * threads, but before it does that it removes all threads from
1434c00af0eSArnaldo Carvalho de Melo 		 * it using list_del_init().
1444c00af0eSArnaldo Carvalho de Melo 		 *
1454c00af0eSArnaldo Carvalho de Melo 		 * So we need to check here if it is in a dead threads list and
1464c00af0eSArnaldo Carvalho de Melo 		 * if so, remove it before finally deleting the thread, to avoid
1474c00af0eSArnaldo Carvalho de Melo 		 * an use after free situation.
148abd82868SArnaldo Carvalho de Melo 		 */
1494c00af0eSArnaldo Carvalho de Melo 		if (!list_empty(&thread->node))
150f3b623b8SArnaldo Carvalho de Melo 			list_del_init(&thread->node);
151f3b623b8SArnaldo Carvalho de Melo 		thread__delete(thread);
152f3b623b8SArnaldo Carvalho de Melo 	}
153f3b623b8SArnaldo Carvalho de Melo }
154f3b623b8SArnaldo Carvalho de Melo 
1556584140bSNamhyung Kim static struct namespaces *__thread__namespaces(const struct thread *thread)
156f3b3614aSHari Bathini {
157f3b3614aSHari Bathini 	if (list_empty(&thread->namespaces_list))
158f3b3614aSHari Bathini 		return NULL;
159f3b3614aSHari Bathini 
160f3b3614aSHari Bathini 	return list_first_entry(&thread->namespaces_list, struct namespaces, list);
161f3b3614aSHari Bathini }
162f3b3614aSHari Bathini 
1637cb10a08SNamhyung Kim struct namespaces *thread__namespaces(struct thread *thread)
1646584140bSNamhyung Kim {
1656584140bSNamhyung Kim 	struct namespaces *ns;
1666584140bSNamhyung Kim 
1677cb10a08SNamhyung Kim 	down_read(&thread->namespaces_lock);
1686584140bSNamhyung Kim 	ns = __thread__namespaces(thread);
1697cb10a08SNamhyung Kim 	up_read(&thread->namespaces_lock);
1706584140bSNamhyung Kim 
1716584140bSNamhyung Kim 	return ns;
1726584140bSNamhyung Kim }
1736584140bSNamhyung Kim 
174b32ee9e5SKan Liang static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
17569d81f09SArnaldo Carvalho de Melo 				    struct perf_record_namespaces *event)
176f3b3614aSHari Bathini {
1776584140bSNamhyung Kim 	struct namespaces *new, *curr = __thread__namespaces(thread);
178f3b3614aSHari Bathini 
179f3b3614aSHari Bathini 	new = namespaces__new(event);
180f3b3614aSHari Bathini 	if (!new)
181f3b3614aSHari Bathini 		return -ENOMEM;
182f3b3614aSHari Bathini 
183f3b3614aSHari Bathini 	list_add(&new->list, &thread->namespaces_list);
184f3b3614aSHari Bathini 
185f3b3614aSHari Bathini 	if (timestamp && curr) {
186f3b3614aSHari Bathini 		/*
187f3b3614aSHari Bathini 		 * setns syscall must have changed few or all the namespaces
188f3b3614aSHari Bathini 		 * of this thread. Update end time for the namespaces
189f3b3614aSHari Bathini 		 * previously used.
190f3b3614aSHari Bathini 		 */
191f3b3614aSHari Bathini 		curr = list_next_entry(new, list);
192f3b3614aSHari Bathini 		curr->end_time = timestamp;
193f3b3614aSHari Bathini 	}
194f3b3614aSHari Bathini 
195f3b3614aSHari Bathini 	return 0;
196f3b3614aSHari Bathini }
197f3b3614aSHari Bathini 
198b32ee9e5SKan Liang int thread__set_namespaces(struct thread *thread, u64 timestamp,
19969d81f09SArnaldo Carvalho de Melo 			   struct perf_record_namespaces *event)
200b32ee9e5SKan Liang {
201b32ee9e5SKan Liang 	int ret;
202b32ee9e5SKan Liang 
203b32ee9e5SKan Liang 	down_write(&thread->namespaces_lock);
204b32ee9e5SKan Liang 	ret = __thread__set_namespaces(thread, timestamp, event);
205b32ee9e5SKan Liang 	up_write(&thread->namespaces_lock);
206b32ee9e5SKan Liang 	return ret;
207b32ee9e5SKan Liang }
208b32ee9e5SKan Liang 
2094dfced35SNamhyung Kim struct comm *thread__comm(const struct thread *thread)
2106baa0a5aSFrederic Weisbecker {
2111902efe7SFrederic Weisbecker 	if (list_empty(&thread->comm_list))
2121902efe7SFrederic Weisbecker 		return NULL;
2134385d580SDavid S. Miller 
2141902efe7SFrederic Weisbecker 	return list_first_entry(&thread->comm_list, struct comm, list);
2154385d580SDavid S. Miller }
2161902efe7SFrederic Weisbecker 
21765de51f9SAdrian Hunter struct comm *thread__exec_comm(const struct thread *thread)
21865de51f9SAdrian Hunter {
2193de7ae0bSAdrian Hunter 	struct comm *comm, *last = NULL, *second_last = NULL;
22065de51f9SAdrian Hunter 
22165de51f9SAdrian Hunter 	list_for_each_entry(comm, &thread->comm_list, list) {
22265de51f9SAdrian Hunter 		if (comm->exec)
22365de51f9SAdrian Hunter 			return comm;
2243de7ae0bSAdrian Hunter 		second_last = last;
22565de51f9SAdrian Hunter 		last = comm;
22665de51f9SAdrian Hunter 	}
22765de51f9SAdrian Hunter 
2283de7ae0bSAdrian Hunter 	/*
2293de7ae0bSAdrian Hunter 	 * 'last' with no start time might be the parent's comm of a synthesized
2303de7ae0bSAdrian Hunter 	 * thread (created by processing a synthesized fork event). For a main
2313de7ae0bSAdrian Hunter 	 * thread, that is very probably wrong. Prefer a later comm to avoid
2323de7ae0bSAdrian Hunter 	 * that case.
2333de7ae0bSAdrian Hunter 	 */
2343de7ae0bSAdrian Hunter 	if (second_last && !last->start && thread->pid_ == thread->tid)
2353de7ae0bSAdrian Hunter 		return second_last;
2363de7ae0bSAdrian Hunter 
23765de51f9SAdrian Hunter 	return last;
23865de51f9SAdrian Hunter }
23965de51f9SAdrian Hunter 
240b32ee9e5SKan Liang static int ____thread__set_comm(struct thread *thread, const char *str,
241b32ee9e5SKan Liang 				u64 timestamp, bool exec)
2421902efe7SFrederic Weisbecker {
2431902efe7SFrederic Weisbecker 	struct comm *new, *curr = thread__comm(thread);
2441902efe7SFrederic Weisbecker 
245a8480808SAdrian Hunter 	/* Override the default :tid entry */
246a8480808SAdrian Hunter 	if (!thread->comm_set) {
24718ef15c6SArnaldo Carvalho de Melo 		int err = comm__override(curr, str, timestamp, exec);
2483178f58bSFrederic Weisbecker 		if (err)
2493178f58bSFrederic Weisbecker 			return err;
250a5285ad9SFrederic Weisbecker 	} else {
25165de51f9SAdrian Hunter 		new = comm__new(str, timestamp, exec);
2521902efe7SFrederic Weisbecker 		if (!new)
2531902efe7SFrederic Weisbecker 			return -ENOMEM;
2541902efe7SFrederic Weisbecker 		list_add(&new->list, &thread->comm_list);
255380b5143SNamhyung Kim 
256380b5143SNamhyung Kim 		if (exec)
257fe87797dSArnaldo Carvalho de Melo 			unwind__flush_access(thread->maps);
258a5285ad9SFrederic Weisbecker 	}
259a5285ad9SFrederic Weisbecker 
2601902efe7SFrederic Weisbecker 	thread->comm_set = true;
2611902efe7SFrederic Weisbecker 
2621902efe7SFrederic Weisbecker 	return 0;
2636baa0a5aSFrederic Weisbecker }
2646baa0a5aSFrederic Weisbecker 
265b32ee9e5SKan Liang int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
266b32ee9e5SKan Liang 		       bool exec)
267b32ee9e5SKan Liang {
268b32ee9e5SKan Liang 	int ret;
269b32ee9e5SKan Liang 
270b32ee9e5SKan Liang 	down_write(&thread->comm_lock);
271b32ee9e5SKan Liang 	ret = ____thread__set_comm(thread, str, timestamp, exec);
272b32ee9e5SKan Liang 	up_write(&thread->comm_lock);
273b32ee9e5SKan Liang 	return ret;
274b32ee9e5SKan Liang }
275b32ee9e5SKan Liang 
2762f3027acSArnaldo Carvalho de Melo int thread__set_comm_from_proc(struct thread *thread)
2772f3027acSArnaldo Carvalho de Melo {
2782f3027acSArnaldo Carvalho de Melo 	char path[64];
2792f3027acSArnaldo Carvalho de Melo 	char *comm = NULL;
2802f3027acSArnaldo Carvalho de Melo 	size_t sz;
2812f3027acSArnaldo Carvalho de Melo 	int err = -1;
2822f3027acSArnaldo Carvalho de Melo 
2832f3027acSArnaldo Carvalho de Melo 	if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
2842f3027acSArnaldo Carvalho de Melo 		       thread->pid_, thread->tid) >= (int)sizeof(path)) &&
2852f3027acSArnaldo Carvalho de Melo 	    procfs__read_str(path, &comm, &sz) == 0) {
2862f3027acSArnaldo Carvalho de Melo 		comm[sz - 1] = '\0';
2872f3027acSArnaldo Carvalho de Melo 		err = thread__set_comm(thread, comm, 0);
2882f3027acSArnaldo Carvalho de Melo 	}
2892f3027acSArnaldo Carvalho de Melo 
2902f3027acSArnaldo Carvalho de Melo 	return err;
2912f3027acSArnaldo Carvalho de Melo }
2922f3027acSArnaldo Carvalho de Melo 
293b32ee9e5SKan Liang static const char *__thread__comm_str(const struct thread *thread)
294b9c5143aSFrederic Weisbecker {
2951902efe7SFrederic Weisbecker 	const struct comm *comm = thread__comm(thread);
2961902efe7SFrederic Weisbecker 
2971902efe7SFrederic Weisbecker 	if (!comm)
2981902efe7SFrederic Weisbecker 		return NULL;
2991902efe7SFrederic Weisbecker 
3001902efe7SFrederic Weisbecker 	return comm__str(comm);
301b9c5143aSFrederic Weisbecker }
302b9c5143aSFrederic Weisbecker 
3037cb10a08SNamhyung Kim const char *thread__comm_str(struct thread *thread)
304b32ee9e5SKan Liang {
305b32ee9e5SKan Liang 	const char *str;
306b32ee9e5SKan Liang 
3077cb10a08SNamhyung Kim 	down_read(&thread->comm_lock);
308b32ee9e5SKan Liang 	str = __thread__comm_str(thread);
3097cb10a08SNamhyung Kim 	up_read(&thread->comm_lock);
310b32ee9e5SKan Liang 
311b32ee9e5SKan Liang 	return str;
312b32ee9e5SKan Liang }
313b32ee9e5SKan Liang 
3146e57f69fSliuwenyu static int __thread__comm_len(struct thread *thread, const char *comm)
315a4fb581bSFrederic Weisbecker {
3161902efe7SFrederic Weisbecker 	if (!comm)
317a4fb581bSFrederic Weisbecker 		return 0;
3181902efe7SFrederic Weisbecker 	thread->comm_len = strlen(comm);
319a4fb581bSFrederic Weisbecker 
320c824c433SArnaldo Carvalho de Melo 	return thread->comm_len;
321a4fb581bSFrederic Weisbecker }
322a4fb581bSFrederic Weisbecker 
3236e57f69fSliuwenyu /* CHECKME: it should probably better return the max comm len from its comm list */
3246e57f69fSliuwenyu int thread__comm_len(struct thread *thread)
3256e57f69fSliuwenyu {
3266e57f69fSliuwenyu 	int comm_len = thread->comm_len;
3276e57f69fSliuwenyu 
3286e57f69fSliuwenyu 	if (!comm_len) {
3296e57f69fSliuwenyu 		const char *comm;
3306e57f69fSliuwenyu 
3316e57f69fSliuwenyu 		down_read(&thread->comm_lock);
3326e57f69fSliuwenyu 		comm = __thread__comm_str(thread);
3336e57f69fSliuwenyu 		comm_len = __thread__comm_len(thread, comm);
3346e57f69fSliuwenyu 		up_read(&thread->comm_lock);
3356e57f69fSliuwenyu 	}
3366e57f69fSliuwenyu 
3376e57f69fSliuwenyu 	return comm_len;
3386e57f69fSliuwenyu }
3396e57f69fSliuwenyu 
3403f067dcaSArnaldo Carvalho de Melo size_t thread__fprintf(struct thread *thread, FILE *fp)
34195011c60SArnaldo Carvalho de Melo {
342b9c5143aSFrederic Weisbecker 	return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
343fe87797dSArnaldo Carvalho de Melo 	       maps__fprintf(thread->maps, fp);
3446baa0a5aSFrederic Weisbecker }
3456baa0a5aSFrederic Weisbecker 
3468132a2a8SHe Kuang int thread__insert_map(struct thread *thread, struct map *map)
3471b46cddfSArnaldo Carvalho de Melo {
3488132a2a8SHe Kuang 	int ret;
3498132a2a8SHe Kuang 
350fe87797dSArnaldo Carvalho de Melo 	ret = unwind__prepare_access(thread->maps, map, NULL);
3518132a2a8SHe Kuang 	if (ret)
3528132a2a8SHe Kuang 		return ret;
3538132a2a8SHe Kuang 
354fe87797dSArnaldo Carvalho de Melo 	maps__fixup_overlappings(thread->maps, map, stderr);
355*ff583dc4SIan Rogers 	return maps__insert(thread->maps, map);
35695011c60SArnaldo Carvalho de Melo }
35795011c60SArnaldo Carvalho de Melo 
3586c502584SJiri Olsa static int __thread__prepare_access(struct thread *thread)
3596c502584SJiri Olsa {
3606c502584SJiri Olsa 	bool initialized = false;
3613183f8caSArnaldo Carvalho de Melo 	int err = 0;
362fe87797dSArnaldo Carvalho de Melo 	struct maps *maps = thread->maps;
363*ff583dc4SIan Rogers 	struct map_rb_node *rb_node;
3646c502584SJiri Olsa 
3650a7c74eaSArnaldo Carvalho de Melo 	down_read(&maps->lock);
3666c502584SJiri Olsa 
367*ff583dc4SIan Rogers 	maps__for_each_entry(maps, rb_node) {
368*ff583dc4SIan Rogers 		err = unwind__prepare_access(thread->maps, rb_node->map, &initialized);
3696c502584SJiri Olsa 		if (err || initialized)
3706c502584SJiri Olsa 			break;
3716c502584SJiri Olsa 	}
3726c502584SJiri Olsa 
3730a7c74eaSArnaldo Carvalho de Melo 	up_read(&maps->lock);
3746c502584SJiri Olsa 
3756c502584SJiri Olsa 	return err;
3766c502584SJiri Olsa }
3776c502584SJiri Olsa 
3786c502584SJiri Olsa static int thread__prepare_access(struct thread *thread)
3796c502584SJiri Olsa {
3806c502584SJiri Olsa 	int err = 0;
3816c502584SJiri Olsa 
382382619c0SJiri Olsa 	if (dwarf_callchain_users)
3836c502584SJiri Olsa 		err = __thread__prepare_access(thread);
3846c502584SJiri Olsa 
3856c502584SJiri Olsa 	return err;
3866c502584SJiri Olsa }
3876c502584SJiri Olsa 
38879b6bb73SArnaldo Carvalho de Melo static int thread__clone_maps(struct thread *thread, struct thread *parent, bool do_maps_clone)
389cddcef60SJiri Olsa {
390cddcef60SJiri Olsa 	/* This is new thread, we share map groups for process. */
391cddcef60SJiri Olsa 	if (thread->pid_ == parent->pid_)
3926c502584SJiri Olsa 		return thread__prepare_access(thread);
393cddcef60SJiri Olsa 
394fe87797dSArnaldo Carvalho de Melo 	if (thread->maps == parent->maps) {
3950d7e7accSAdrian Hunter 		pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
3960d7e7accSAdrian Hunter 			 thread->pid_, thread->tid, parent->pid_, parent->tid);
3970d7e7accSAdrian Hunter 		return 0;
3980d7e7accSAdrian Hunter 	}
399cddcef60SJiri Olsa 	/* But this one is new process, copy maps. */
400fe87797dSArnaldo Carvalho de Melo 	return do_maps_clone ? maps__clone(thread, parent->maps) : 0;
401cddcef60SJiri Olsa }
402cddcef60SJiri Olsa 
4034f8f382eSDavid Miller int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
4046baa0a5aSFrederic Weisbecker {
405faa5c5c3SArnaldo Carvalho de Melo 	if (parent->comm_set) {
4061902efe7SFrederic Weisbecker 		const char *comm = thread__comm_str(parent);
40718ef15c6SArnaldo Carvalho de Melo 		int err;
4081902efe7SFrederic Weisbecker 		if (!comm)
4096baa0a5aSFrederic Weisbecker 			return -ENOMEM;
4101902efe7SFrederic Weisbecker 		err = thread__set_comm(thread, comm, timestamp);
4118d00be81SDavid Ahern 		if (err)
4121902efe7SFrederic Weisbecker 			return err;
413faa5c5c3SArnaldo Carvalho de Melo 	}
4146baa0a5aSFrederic Weisbecker 
415c824c433SArnaldo Carvalho de Melo 	thread->ppid = parent->tid;
41679b6bb73SArnaldo Carvalho de Melo 	return thread__clone_maps(thread, parent, do_maps_clone);
4176baa0a5aSFrederic Weisbecker }
41852a3cb8cSArnaldo Carvalho de Melo 
41926bd9331SArnaldo Carvalho de Melo void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
42052a3cb8cSArnaldo Carvalho de Melo 					struct addr_location *al)
42152a3cb8cSArnaldo Carvalho de Melo {
42252a3cb8cSArnaldo Carvalho de Melo 	size_t i;
4233b556bceSEric Engestrom 	const u8 cpumodes[] = {
42452a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_USER,
42552a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_KERNEL,
42652a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_USER,
42752a3cb8cSArnaldo Carvalho de Melo 		PERF_RECORD_MISC_GUEST_KERNEL
42852a3cb8cSArnaldo Carvalho de Melo 	};
42952a3cb8cSArnaldo Carvalho de Melo 
43052a3cb8cSArnaldo Carvalho de Melo 	for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
43126bd9331SArnaldo Carvalho de Melo 		thread__find_symbol(thread, cpumodes[i], addr, al);
43252a3cb8cSArnaldo Carvalho de Melo 		if (al->map)
43352a3cb8cSArnaldo Carvalho de Melo 			break;
43452a3cb8cSArnaldo Carvalho de Melo 	}
43552a3cb8cSArnaldo Carvalho de Melo }
436480ca357SAndi Kleen 
437480ca357SAndi Kleen struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
438480ca357SAndi Kleen {
439480ca357SAndi Kleen 	if (thread->pid_ == thread->tid)
440480ca357SAndi Kleen 		return thread__get(thread);
441480ca357SAndi Kleen 
442480ca357SAndi Kleen 	if (thread->pid_ == -1)
443480ca357SAndi Kleen 		return NULL;
444480ca357SAndi Kleen 
445480ca357SAndi Kleen 	return machine__find_thread(machine, thread->pid_, thread->pid_);
446480ca357SAndi Kleen }
44715325938SAndi Kleen 
44815325938SAndi Kleen int thread__memcpy(struct thread *thread, struct machine *machine,
44915325938SAndi Kleen 		   void *buf, u64 ip, int len, bool *is64bit)
45015325938SAndi Kleen {
45115325938SAndi Kleen        u8 cpumode = PERF_RECORD_MISC_USER;
45215325938SAndi Kleen        struct addr_location al;
45315325938SAndi Kleen        long offset;
45415325938SAndi Kleen 
45515325938SAndi Kleen        if (machine__kernel_ip(machine, ip))
45615325938SAndi Kleen                cpumode = PERF_RECORD_MISC_KERNEL;
45715325938SAndi Kleen 
45815325938SAndi Kleen        if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso ||
45915325938SAndi Kleen 	   al.map->dso->data.status == DSO_DATA_STATUS_ERROR ||
46015325938SAndi Kleen 	   map__load(al.map) < 0)
46115325938SAndi Kleen                return -1;
46215325938SAndi Kleen 
46315325938SAndi Kleen        offset = al.map->map_ip(al.map, ip);
46415325938SAndi Kleen        if (is64bit)
46515325938SAndi Kleen                *is64bit = al.map->dso->is_64_bit;
46615325938SAndi Kleen 
46715325938SAndi Kleen        return dso__data_read_offset(al.map->dso, machine, offset, buf, len);
46815325938SAndi Kleen }
469ff165628SKan Liang 
470ff165628SKan Liang void thread__free_stitch_list(struct thread *thread)
471ff165628SKan Liang {
472ff165628SKan Liang 	struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
473ff165628SKan Liang 	struct stitch_list *pos, *tmp;
474ff165628SKan Liang 
475ff165628SKan Liang 	if (!lbr_stitch)
476ff165628SKan Liang 		return;
477ff165628SKan Liang 
478ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->lists, node) {
479ff165628SKan Liang 		list_del_init(&pos->node);
480ff165628SKan Liang 		free(pos);
481ff165628SKan Liang 	}
482ff165628SKan Liang 
483ff165628SKan Liang 	list_for_each_entry_safe(pos, tmp, &lbr_stitch->free_lists, node) {
484ff165628SKan Liang 		list_del_init(&pos->node);
485ff165628SKan Liang 		free(pos);
486ff165628SKan Liang 	}
487ff165628SKan Liang 
488ff165628SKan Liang 	zfree(&lbr_stitch->prev_lbr_cursor);
489ff165628SKan Liang 	zfree(&thread->lbr_stitch);
490ff165628SKan Liang }
491