12 int thread__init_map_groups(struct thread *thread, struct machine *machine)
14 struct thread *leader;
15 pid_t pid = thread->pid_;
17 if (pid == thread->tid || pid == -1) {
18 thread->mg = map_groups__new();
20 leader = machine__findnew_thread(machine, pid, pid);
22 thread->mg = map_groups__get(leader->mg);
25 return thread->mg ? 0 : -1;
28 struct thread *thread__new(pid_t pid, pid_t tid)
32 struct thread *thread = zalloc(sizeof(*thread));
39 INIT_LIST_HEAD(&thread->comm_list);
41 if (unwind__prepare_access(thread) < 0)
44 comm_str = malloc(32);
48 snprintf(comm_str, 32, ":%d", tid);
49 comm = comm__new(comm_str, 0, false);
54 list_add(&comm->list, &thread->comm_list);
65 void thread__delete(struct thread *thread)
67 struct comm *comm, *tmp;
70 map_groups__put(thread->mg);
73 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
74 list_del(&comm->list);
77 unwind__finish_access(thread);
82 struct comm *thread__comm(const struct thread *thread)
84 if (list_empty(&thread->comm_list))
87 return list_first_entry(&thread->comm_list, struct comm, list);
90 struct comm *thread__exec_comm(const struct thread *thread)
92 struct comm *comm, *last = NULL;
94 list_for_each_entry(comm, &thread->comm_list, list) {
103 /* CHECKME: time should always be 0 if event aren't ordered */
104 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
107 struct comm *new, *curr = thread__comm(thread);
110 /* Override latest entry if it had no specific time coverage */
111 if (!curr->start && !curr->exec) {
112 err = comm__override(curr, str, timestamp, exec);
116 new = comm__new(str, timestamp, exec);
119 list_add(&new->list, &thread->comm_list);
122 unwind__flush_access(thread);
125 thread->comm_set = true;
130 const char *thread__comm_str(const struct thread *thread)
132 const struct comm *comm = thread__comm(thread);
137 return comm__str(comm);
140 /* CHECKME: it should probably better return the max comm len from its comm list */
141 int thread__comm_len(struct thread *thread)
143 if (!thread->comm_len) {
144 const char *comm = thread__comm_str(thread);
147 thread->comm_len = strlen(comm);
150 return thread->comm_len;
153 size_t thread__fprintf(struct thread *thread, FILE *fp)
155 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
156 map_groups__fprintf(thread->mg, fp);
159 void thread__insert_map(struct thread *thread, struct map *map)
161 map_groups__fixup_overlappings(thread->mg, map, stderr);
162 map_groups__insert(thread->mg, map);
165 static int thread__clone_map_groups(struct thread *thread,
166 struct thread *parent)
170 /* This is new thread, we share map groups for process. */
171 if (thread->pid_ == parent->pid_)
174 /* But this one is new process, copy maps. */
175 for (i = 0; i < MAP__NR_TYPES; ++i)
176 if (map_groups__clone(thread->mg, parent->mg, i) < 0)
182 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
186 if (parent->comm_set) {
187 const char *comm = thread__comm_str(parent);
190 err = thread__set_comm(thread, comm, timestamp);
193 thread->comm_set = true;
196 thread->ppid = parent->tid;
197 return thread__clone_map_groups(thread, parent);
200 void thread__find_cpumode_addr_location(struct thread *thread,
201 struct machine *machine,
202 enum map_type type, u64 addr,
203 struct addr_location *al)
206 const u8 const cpumodes[] = {
207 PERF_RECORD_MISC_USER,
208 PERF_RECORD_MISC_KERNEL,
209 PERF_RECORD_MISC_GUEST_USER,
210 PERF_RECORD_MISC_GUEST_KERNEL
213 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
214 thread__find_addr_location(thread, machine, cpumodes[i], type,