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) {
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));
37 INIT_LIST_HEAD(&thread->comm_list);
39 comm_str = malloc(32);
43 snprintf(comm_str, 32, ":%d", tid);
44 comm = comm__new(comm_str, 0);
49 list_add(&comm->list, &thread->comm_list);
59 void thread__delete(struct thread *thread)
61 struct comm *comm, *tmp;
63 map_groups__put(thread->mg);
65 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
66 list_del(&comm->list);
73 struct comm *thread__comm(const struct thread *thread)
75 if (list_empty(&thread->comm_list))
78 return list_first_entry(&thread->comm_list, struct comm, list);
81 /* CHECKME: time should always be 0 if event aren't ordered */
82 int thread__set_comm(struct thread *thread, const char *str, u64 timestamp)
84 struct comm *new, *curr = thread__comm(thread);
87 /* Override latest entry if it had no specific time coverage */
89 err = comm__override(curr, str, timestamp);
93 new = comm__new(str, timestamp);
96 list_add(&new->list, &thread->comm_list);
99 thread->comm_set = true;
104 const char *thread__comm_str(const struct thread *thread)
106 const struct comm *comm = thread__comm(thread);
111 return comm__str(comm);
114 /* CHECKME: it should probably better return the max comm len from its comm list */
115 int thread__comm_len(struct thread *thread)
117 if (!thread->comm_len) {
118 const char *comm = thread__comm_str(thread);
121 thread->comm_len = strlen(comm);
124 return thread->comm_len;
127 size_t thread__fprintf(struct thread *thread, FILE *fp)
129 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
130 map_groups__fprintf(thread->mg, verbose, fp);
133 void thread__insert_map(struct thread *thread, struct map *map)
135 map_groups__fixup_overlappings(thread->mg, map, verbose, stderr);
136 map_groups__insert(thread->mg, map);
139 static int thread__clone_map_groups(struct thread *thread,
140 struct thread *parent)
144 /* This is new thread, we share map groups for process. */
145 if (thread->pid_ == parent->pid_)
148 /* But this one is new process, copy maps. */
149 for (i = 0; i < MAP__NR_TYPES; ++i)
150 if (map_groups__clone(thread->mg, parent->mg, i) < 0)
156 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
160 if (parent->comm_set) {
161 const char *comm = thread__comm_str(parent);
164 err = thread__set_comm(thread, comm, timestamp);
167 thread->comm_set = true;
170 thread->ppid = parent->tid;
171 return thread__clone_map_groups(thread, parent);
174 void thread__find_cpumode_addr_location(struct thread *thread,
175 struct machine *machine,
176 enum map_type type, u64 addr,
177 struct addr_location *al)
180 const u8 const cpumodes[] = {
181 PERF_RECORD_MISC_USER,
182 PERF_RECORD_MISC_KERNEL,
183 PERF_RECORD_MISC_GUEST_USER,
184 PERF_RECORD_MISC_GUEST_KERNEL
187 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
188 thread__find_addr_location(thread, machine, cpumodes[i], type,