Commit | Line | Data |
---|---|---|
6baa0a5a FW |
1 | #include "../perf.h" |
2 | #include <stdlib.h> | |
3 | #include <stdio.h> | |
4 | #include <string.h> | |
b3165f41 | 5 | #include "session.h" |
6baa0a5a | 6 | #include "thread.h" |
00447ccd | 7 | #include "thread-stack.h" |
6baa0a5a | 8 | #include "util.h" |
6e086437 | 9 | #include "debug.h" |
1902efe7 | 10 | #include "comm.h" |
66f066d8 | 11 | #include "unwind.h" |
6baa0a5a | 12 | |
cddcef60 JO |
13 | int thread__init_map_groups(struct thread *thread, struct machine *machine) |
14 | { | |
15 | struct thread *leader; | |
16 | pid_t pid = thread->pid_; | |
17 | ||
1fcb8768 | 18 | if (pid == thread->tid || pid == -1) { |
11246c70 | 19 | thread->mg = map_groups__new(machine); |
cddcef60 JO |
20 | } else { |
21 | leader = machine__findnew_thread(machine, pid, pid); | |
22 | if (leader) | |
23 | thread->mg = map_groups__get(leader->mg); | |
24 | } | |
25 | ||
26 | return thread->mg ? 0 : -1; | |
27 | } | |
28 | ||
99d725fc | 29 | struct thread *thread__new(pid_t pid, pid_t tid) |
6baa0a5a | 30 | { |
1902efe7 FW |
31 | char *comm_str; |
32 | struct comm *comm; | |
c824c433 | 33 | struct thread *thread = zalloc(sizeof(*thread)); |
6baa0a5a | 34 | |
c824c433 | 35 | if (thread != NULL) { |
c824c433 ACM |
36 | thread->pid_ = pid; |
37 | thread->tid = tid; | |
38 | thread->ppid = -1; | |
bf49c35f | 39 | thread->cpu = -1; |
1902efe7 FW |
40 | INIT_LIST_HEAD(&thread->comm_list); |
41 | ||
66f066d8 NK |
42 | if (unwind__prepare_access(thread) < 0) |
43 | goto err_thread; | |
44 | ||
1902efe7 FW |
45 | comm_str = malloc(32); |
46 | if (!comm_str) | |
47 | goto err_thread; | |
48 | ||
49 | snprintf(comm_str, 32, ":%d", tid); | |
65de51f9 | 50 | comm = comm__new(comm_str, 0, false); |
1902efe7 FW |
51 | free(comm_str); |
52 | if (!comm) | |
53 | goto err_thread; | |
54 | ||
55 | list_add(&comm->list, &thread->comm_list); | |
66f066d8 | 56 | |
6baa0a5a FW |
57 | } |
58 | ||
c824c433 | 59 | return thread; |
1902efe7 FW |
60 | |
61 | err_thread: | |
62 | free(thread); | |
63 | return NULL; | |
6baa0a5a FW |
64 | } |
65 | ||
c824c433 | 66 | void thread__delete(struct thread *thread) |
591765fd | 67 | { |
1902efe7 FW |
68 | struct comm *comm, *tmp; |
69 | ||
00447ccd AH |
70 | thread_stack__free(thread); |
71 | ||
9608b84e AH |
72 | if (thread->mg) { |
73 | map_groups__put(thread->mg); | |
74 | thread->mg = NULL; | |
75 | } | |
1902efe7 FW |
76 | list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) { |
77 | list_del(&comm->list); | |
78 | comm__free(comm); | |
79 | } | |
66f066d8 | 80 | unwind__finish_access(thread); |
1902efe7 | 81 | |
c824c433 | 82 | free(thread); |
591765fd ACM |
83 | } |
84 | ||
4dfced35 | 85 | struct comm *thread__comm(const struct thread *thread) |
6baa0a5a | 86 | { |
1902efe7 FW |
87 | if (list_empty(&thread->comm_list)) |
88 | return NULL; | |
4385d580 | 89 | |
1902efe7 FW |
90 | return list_first_entry(&thread->comm_list, struct comm, list); |
91 | } | |
92 | ||
65de51f9 AH |
93 | struct comm *thread__exec_comm(const struct thread *thread) |
94 | { | |
95 | struct comm *comm, *last = NULL; | |
96 | ||
97 | list_for_each_entry(comm, &thread->comm_list, list) { | |
98 | if (comm->exec) | |
99 | return comm; | |
100 | last = comm; | |
101 | } | |
102 | ||
103 | return last; | |
104 | } | |
105 | ||
1902efe7 | 106 | /* CHECKME: time should always be 0 if event aren't ordered */ |
65de51f9 AH |
107 | int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp, |
108 | bool exec) | |
1902efe7 FW |
109 | { |
110 | struct comm *new, *curr = thread__comm(thread); | |
3178f58b | 111 | int err; |
1902efe7 FW |
112 | |
113 | /* Override latest entry if it had no specific time coverage */ | |
65de51f9 AH |
114 | if (!curr->start && !curr->exec) { |
115 | err = comm__override(curr, str, timestamp, exec); | |
3178f58b FW |
116 | if (err) |
117 | return err; | |
a5285ad9 | 118 | } else { |
65de51f9 | 119 | new = comm__new(str, timestamp, exec); |
a5285ad9 FW |
120 | if (!new) |
121 | return -ENOMEM; | |
122 | list_add(&new->list, &thread->comm_list); | |
380b5143 NK |
123 | |
124 | if (exec) | |
125 | unwind__flush_access(thread); | |
4385d580 | 126 | } |
1902efe7 | 127 | |
1902efe7 FW |
128 | thread->comm_set = true; |
129 | ||
130 | return 0; | |
6baa0a5a FW |
131 | } |
132 | ||
b9c5143a FW |
133 | const char *thread__comm_str(const struct thread *thread) |
134 | { | |
1902efe7 FW |
135 | const struct comm *comm = thread__comm(thread); |
136 | ||
137 | if (!comm) | |
138 | return NULL; | |
139 | ||
140 | return comm__str(comm); | |
b9c5143a FW |
141 | } |
142 | ||
1902efe7 | 143 | /* CHECKME: it should probably better return the max comm len from its comm list */ |
c824c433 | 144 | int thread__comm_len(struct thread *thread) |
a4fb581b | 145 | { |
c824c433 | 146 | if (!thread->comm_len) { |
1902efe7 FW |
147 | const char *comm = thread__comm_str(thread); |
148 | if (!comm) | |
a4fb581b | 149 | return 0; |
1902efe7 | 150 | thread->comm_len = strlen(comm); |
a4fb581b FW |
151 | } |
152 | ||
c824c433 | 153 | return thread->comm_len; |
a4fb581b FW |
154 | } |
155 | ||
3f067dca | 156 | size_t thread__fprintf(struct thread *thread, FILE *fp) |
9958e1f0 | 157 | { |
b9c5143a | 158 | return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) + |
acebd408 | 159 | map_groups__fprintf(thread->mg, fp); |
6baa0a5a FW |
160 | } |
161 | ||
c824c433 | 162 | void thread__insert_map(struct thread *thread, struct map *map) |
1b46cddf | 163 | { |
acebd408 | 164 | map_groups__fixup_overlappings(thread->mg, map, stderr); |
93d5731d | 165 | map_groups__insert(thread->mg, map); |
6baa0a5a FW |
166 | } |
167 | ||
cddcef60 JO |
168 | static int thread__clone_map_groups(struct thread *thread, |
169 | struct thread *parent) | |
170 | { | |
171 | int i; | |
172 | ||
173 | /* This is new thread, we share map groups for process. */ | |
174 | if (thread->pid_ == parent->pid_) | |
175 | return 0; | |
176 | ||
177 | /* But this one is new process, copy maps. */ | |
178 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
179 | if (map_groups__clone(thread->mg, parent->mg, i) < 0) | |
180 | return -ENOMEM; | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
1902efe7 | 185 | int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp) |
95011c60 | 186 | { |
cddcef60 | 187 | int err; |
6baa0a5a | 188 | |
faa5c5c3 | 189 | if (parent->comm_set) { |
1902efe7 FW |
190 | const char *comm = thread__comm_str(parent); |
191 | if (!comm) | |
faa5c5c3 | 192 | return -ENOMEM; |
1902efe7 | 193 | err = thread__set_comm(thread, comm, timestamp); |
8d00be81 | 194 | if (err) |
1902efe7 | 195 | return err; |
c824c433 | 196 | thread->comm_set = true; |
faa5c5c3 | 197 | } |
6baa0a5a | 198 | |
c824c433 | 199 | thread->ppid = parent->tid; |
cddcef60 | 200 | return thread__clone_map_groups(thread, parent); |
6baa0a5a | 201 | } |
52a3cb8c ACM |
202 | |
203 | void thread__find_cpumode_addr_location(struct thread *thread, | |
52a3cb8c ACM |
204 | enum map_type type, u64 addr, |
205 | struct addr_location *al) | |
206 | { | |
207 | size_t i; | |
208 | const u8 const cpumodes[] = { | |
209 | PERF_RECORD_MISC_USER, | |
210 | PERF_RECORD_MISC_KERNEL, | |
211 | PERF_RECORD_MISC_GUEST_USER, | |
212 | PERF_RECORD_MISC_GUEST_KERNEL | |
213 | }; | |
214 | ||
215 | for (i = 0; i < ARRAY_SIZE(cpumodes); i++) { | |
bb871a9c | 216 | thread__find_addr_location(thread, cpumodes[i], type, addr, al); |
52a3cb8c ACM |
217 | if (al->map) |
218 | break; | |
219 | } | |
220 | } |