11 int thread__init_map_groups(struct thread *thread, struct machine *machine)
13 struct thread *leader;
14 pid_t pid = thread->pid_;
16 if (pid == thread->tid || pid == -1) {
17 thread->mg = map_groups__new();
19 leader = machine__findnew_thread(machine, pid, pid);
21 thread->mg = map_groups__get(leader->mg);
24 return thread->mg ? 0 : -1;
27 struct thread *thread__new(pid_t pid, pid_t tid)
31 struct thread *thread = zalloc(sizeof(*thread));
38 INIT_LIST_HEAD(&thread->comm_list);
40 comm_str = malloc(32);
44 snprintf(comm_str, 32, ":%d", tid);
45 comm = comm__new(comm_str, 0, false);
50 list_add(&comm->list, &thread->comm_list);
60 void thread__delete(struct thread *thread)
62 struct comm *comm, *tmp;
65 map_groups__put(thread->mg);
68 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
69 list_del(&comm->list);
76 struct comm *thread__comm(const struct thread *thread)
78 if (list_empty(&thread->comm_list))
81 return list_first_entry(&thread->comm_list, struct comm, list);
84 struct comm *thread__exec_comm(const struct thread *thread)
86 struct comm *comm, *last = NULL;
88 list_for_each_entry(comm, &thread->comm_list, list) {
97 /* CHECKME: time should always be 0 if event aren't ordered */
98 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
101 struct comm *new, *curr = thread__comm(thread);
104 /* Override latest entry if it had no specific time coverage */
105 if (!curr->start && !curr->exec) {
106 err = comm__override(curr, str, timestamp, exec);
110 new = comm__new(str, timestamp, exec);
113 list_add(&new->list, &thread->comm_list);
116 thread->comm_set = true;
121 const char *thread__comm_str(const struct thread *thread)
123 const struct comm *comm = thread__comm(thread);
128 return comm__str(comm);
131 /* CHECKME: it should probably better return the max comm len from its comm list */
132 int thread__comm_len(struct thread *thread)
134 if (!thread->comm_len) {
135 const char *comm = thread__comm_str(thread);
138 thread->comm_len = strlen(comm);
141 return thread->comm_len;
144 size_t thread__fprintf(struct thread *thread, FILE *fp)
146 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
147 map_groups__fprintf(thread->mg, fp);
150 void thread__insert_map(struct thread *thread, struct map *map)
152 map_groups__fixup_overlappings(thread->mg, map, stderr);
153 map_groups__insert(thread->mg, map);
156 static int thread__clone_map_groups(struct thread *thread,
157 struct thread *parent)
161 /* This is new thread, we share map groups for process. */
162 if (thread->pid_ == parent->pid_)
165 /* But this one is new process, copy maps. */
166 for (i = 0; i < MAP__NR_TYPES; ++i)
167 if (map_groups__clone(thread->mg, parent->mg, i) < 0)
173 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
177 if (parent->comm_set) {
178 const char *comm = thread__comm_str(parent);
181 err = thread__set_comm(thread, comm, timestamp);
184 thread->comm_set = true;
187 thread->ppid = parent->tid;
188 return thread__clone_map_groups(thread, parent);
191 void thread__find_cpumode_addr_location(struct thread *thread,
192 struct machine *machine,
193 enum map_type type, u64 addr,
194 struct addr_location *al)
197 const u8 const cpumodes[] = {
198 PERF_RECORD_MISC_USER,
199 PERF_RECORD_MISC_KERNEL,
200 PERF_RECORD_MISC_GUEST_USER,
201 PERF_RECORD_MISC_GUEST_KERNEL
204 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
205 thread__find_addr_location(thread, machine, cpumodes[i], type,