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);
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 /* CHECKME: time should always be 0 if event aren't ordered */
85 int thread__set_comm(struct thread *thread, const char *str, u64 timestamp)
87 struct comm *new, *curr = thread__comm(thread);
90 /* Override latest entry if it had no specific time coverage */
92 err = comm__override(curr, str, timestamp);
96 new = comm__new(str, timestamp);
99 list_add(&new->list, &thread->comm_list);
102 thread->comm_set = true;
107 const char *thread__comm_str(const struct thread *thread)
109 const struct comm *comm = thread__comm(thread);
114 return comm__str(comm);
117 /* CHECKME: it should probably better return the max comm len from its comm list */
118 int thread__comm_len(struct thread *thread)
120 if (!thread->comm_len) {
121 const char *comm = thread__comm_str(thread);
124 thread->comm_len = strlen(comm);
127 return thread->comm_len;
130 size_t thread__fprintf(struct thread *thread, FILE *fp)
132 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
133 map_groups__fprintf(thread->mg, fp);
136 void thread__insert_map(struct thread *thread, struct map *map)
138 map_groups__fixup_overlappings(thread->mg, map, stderr);
139 map_groups__insert(thread->mg, map);
142 static int thread__clone_map_groups(struct thread *thread,
143 struct thread *parent)
147 /* This is new thread, we share map groups for process. */
148 if (thread->pid_ == parent->pid_)
151 /* But this one is new process, copy maps. */
152 for (i = 0; i < MAP__NR_TYPES; ++i)
153 if (map_groups__clone(thread->mg, parent->mg, i) < 0)
159 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
163 if (parent->comm_set) {
164 const char *comm = thread__comm_str(parent);
167 err = thread__set_comm(thread, comm, timestamp);
170 thread->comm_set = true;
173 thread->ppid = parent->tid;
174 return thread__clone_map_groups(thread, parent);
177 void thread__find_cpumode_addr_location(struct thread *thread,
178 struct machine *machine,
179 enum map_type type, u64 addr,
180 struct addr_location *al)
183 const u8 const cpumodes[] = {
184 PERF_RECORD_MISC_USER,
185 PERF_RECORD_MISC_KERNEL,
186 PERF_RECORD_MISC_GUEST_USER,
187 PERF_RECORD_MISC_GUEST_KERNEL
190 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
191 thread__find_addr_location(thread, machine, cpumodes[i], type,