blob: cb6388dbdd9875a588207286946cb5bea7887767 [file] [log] [blame]
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001#include "callchain.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002#include "debug.h"
3#include "event.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03004#include "evsel.h"
5#include "hist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -03006#include "machine.h"
7#include "map.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03008#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -03009#include "strlist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030010#include "thread.h"
Adrian Hunterd027b642014-07-23 14:23:00 +030011#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030012#include <stdbool.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030013#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030014#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080015#include "linux/hash.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030016
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030017static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
18
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030019static void dsos__init(struct dsos *dsos)
20{
21 INIT_LIST_HEAD(&dsos->head);
22 dsos->root = RB_ROOT;
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -030023 pthread_rwlock_init(&dsos->lock, NULL);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030024}
25
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030026int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
27{
Wang Nan93b0ba32015-12-08 02:25:44 +000028 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030029 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030030 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030031 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030032
33 machine->threads = RB_ROOT;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030034 pthread_rwlock_init(&machine->threads_lock, NULL);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -030035 machine->nr_threads = 0;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030036 INIT_LIST_HEAD(&machine->dead_threads);
37 machine->last_match = NULL;
38
Adrian Hunterd027b642014-07-23 14:23:00 +030039 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030040 machine->env = NULL;
Adrian Hunterd027b642014-07-23 14:23:00 +030041
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030042 machine->pid = pid;
43
Adrian Hunter611a5ce2013-08-08 14:32:20 +030044 machine->symbol_filter = NULL;
Jiri Olsa14bd6d22014-01-07 13:47:19 +010045 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -030046 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +030047 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030048 machine->kernel_start = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030049
Masami Hiramatsucc1121a2015-12-09 11:11:33 +090050 memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps));
51
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030052 machine->root_dir = strdup(root_dir);
53 if (machine->root_dir == NULL)
54 return -ENOMEM;
55
56 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030057 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030058 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030059 char comm[64];
60
61 if (thread == NULL)
62 return -ENOMEM;
63
64 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020065 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030066 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030067 }
68
Adrian Hunterb9d266b2014-07-22 16:17:25 +030069 machine->current_tid = NULL;
70
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030071 return 0;
72}
73
David Ahern8fb598e2013-09-28 13:13:00 -060074struct machine *machine__new_host(void)
75{
76 struct machine *machine = malloc(sizeof(*machine));
77
78 if (machine != NULL) {
79 machine__init(machine, "", HOST_KERNEL_ID);
80
81 if (machine__create_kernel_maps(machine) < 0)
82 goto out_delete;
83 }
84
85 return machine;
86out_delete:
87 free(machine);
88 return NULL;
89}
90
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -030091static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030092{
93 struct dso *pos, *n;
94
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -030095 pthread_rwlock_wrlock(&dsos->lock);
96
Waiman Long8fa7d872014-09-29 16:07:28 -040097 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -040098 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +020099 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300100 list_del_init(&pos->node);
101 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300102 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300103
104 pthread_rwlock_unlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300105}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300106
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300107static void dsos__exit(struct dsos *dsos)
108{
109 dsos__purge(dsos);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300110 pthread_rwlock_destroy(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300111}
112
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300113void machine__delete_threads(struct machine *machine)
114{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300115 struct rb_node *nd;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300116
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300117 pthread_rwlock_wrlock(&machine->threads_lock);
118 nd = rb_first(&machine->threads);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300119 while (nd) {
120 struct thread *t = rb_entry(nd, struct thread, rb_node);
121
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300122 nd = rb_next(nd);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300123 __machine__remove_thread(machine, t, false);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300124 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300125 pthread_rwlock_unlock(&machine->threads_lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300126}
127
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300128void machine__exit(struct machine *machine)
129{
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900130 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300131 map_groups__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300132 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300133 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300134 zfree(&machine->root_dir);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300135 zfree(&machine->current_tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300136 pthread_rwlock_destroy(&machine->threads_lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300137}
138
139void machine__delete(struct machine *machine)
140{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300141 if (machine) {
142 machine__exit(machine);
143 free(machine);
144 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300145}
146
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300147void machines__init(struct machines *machines)
148{
149 machine__init(&machines->host, "", HOST_KERNEL_ID);
150 machines->guests = RB_ROOT;
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300151 machines->symbol_filter = NULL;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300152}
153
154void machines__exit(struct machines *machines)
155{
156 machine__exit(&machines->host);
157 /* XXX exit guest */
158}
159
160struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300161 const char *root_dir)
162{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300163 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300164 struct rb_node *parent = NULL;
165 struct machine *pos, *machine = malloc(sizeof(*machine));
166
167 if (machine == NULL)
168 return NULL;
169
170 if (machine__init(machine, root_dir, pid) != 0) {
171 free(machine);
172 return NULL;
173 }
174
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300175 machine->symbol_filter = machines->symbol_filter;
176
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300177 while (*p != NULL) {
178 parent = *p;
179 pos = rb_entry(parent, struct machine, rb_node);
180 if (pid < pos->pid)
181 p = &(*p)->rb_left;
182 else
183 p = &(*p)->rb_right;
184 }
185
186 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300187 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300188
189 return machine;
190}
191
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300192void machines__set_symbol_filter(struct machines *machines,
193 symbol_filter_t symbol_filter)
194{
195 struct rb_node *nd;
196
197 machines->symbol_filter = symbol_filter;
198 machines->host.symbol_filter = symbol_filter;
199
200 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
201 struct machine *machine = rb_entry(nd, struct machine, rb_node);
202
203 machine->symbol_filter = symbol_filter;
204 }
205}
206
Adrian Huntercfe1c412014-07-31 09:00:45 +0300207void machines__set_comm_exec(struct machines *machines, bool comm_exec)
208{
209 struct rb_node *nd;
210
211 machines->host.comm_exec = comm_exec;
212
213 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
214 struct machine *machine = rb_entry(nd, struct machine, rb_node);
215
216 machine->comm_exec = comm_exec;
217 }
218}
219
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300220struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300221{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300222 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300223 struct rb_node *parent = NULL;
224 struct machine *machine;
225 struct machine *default_machine = NULL;
226
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300227 if (pid == HOST_KERNEL_ID)
228 return &machines->host;
229
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300230 while (*p != NULL) {
231 parent = *p;
232 machine = rb_entry(parent, struct machine, rb_node);
233 if (pid < machine->pid)
234 p = &(*p)->rb_left;
235 else if (pid > machine->pid)
236 p = &(*p)->rb_right;
237 else
238 return machine;
239 if (!machine->pid)
240 default_machine = machine;
241 }
242
243 return default_machine;
244}
245
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300246struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300247{
248 char path[PATH_MAX];
249 const char *root_dir = "";
250 struct machine *machine = machines__find(machines, pid);
251
252 if (machine && (machine->pid == pid))
253 goto out;
254
255 if ((pid != HOST_KERNEL_ID) &&
256 (pid != DEFAULT_GUEST_KERNEL_ID) &&
257 (symbol_conf.guestmount)) {
258 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
259 if (access(path, R_OK)) {
260 static struct strlist *seen;
261
262 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300263 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300264
265 if (!strlist__has_entry(seen, path)) {
266 pr_err("Can't access file %s\n", path);
267 strlist__add(seen, path);
268 }
269 machine = NULL;
270 goto out;
271 }
272 root_dir = path;
273 }
274
275 machine = machines__add(machines, pid, root_dir);
276out:
277 return machine;
278}
279
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300280void machines__process_guests(struct machines *machines,
281 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300282{
283 struct rb_node *nd;
284
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300285 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300286 struct machine *pos = rb_entry(nd, struct machine, rb_node);
287 process(pos, data);
288 }
289}
290
291char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
292{
293 if (machine__is_host(machine))
294 snprintf(bf, size, "[%s]", "kernel.kallsyms");
295 else if (machine__is_default_guest(machine))
296 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
297 else {
298 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
299 machine->pid);
300 }
301
302 return bf;
303}
304
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300305void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300306{
307 struct rb_node *node;
308 struct machine *machine;
309
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300310 machines->host.id_hdr_size = id_hdr_size;
311
312 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300313 machine = rb_entry(node, struct machine, rb_node);
314 machine->id_hdr_size = id_hdr_size;
315 }
316
317 return;
318}
319
Adrian Hunter29ce3612014-07-16 11:07:13 +0300320static void machine__update_thread_pid(struct machine *machine,
321 struct thread *th, pid_t pid)
322{
323 struct thread *leader;
324
325 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
326 return;
327
328 th->pid_ = pid;
329
330 if (th->pid_ == th->tid)
331 return;
332
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300333 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300334 if (!leader)
335 goto out_err;
336
337 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300338 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300339
340 if (!leader->mg)
341 goto out_err;
342
343 if (th->mg == leader->mg)
344 return;
345
346 if (th->mg) {
347 /*
348 * Maps are created from MMAP events which provide the pid and
349 * tid. Consequently there never should be any maps on a thread
350 * with an unknown pid. Just print an error if there are.
351 */
352 if (!map_groups__empty(th->mg))
353 pr_err("Discarding thread maps for %d:%d\n",
354 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300355 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300356 }
357
358 th->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300359out_put:
360 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300361 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300362out_err:
363 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300364 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300365}
366
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300367/*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800368 * Caller must eventually drop thread->refcnt returned with a successful
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300369 * lookup/new thread inserted.
370 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300371static struct thread *____machine__findnew_thread(struct machine *machine,
372 pid_t pid, pid_t tid,
373 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300374{
375 struct rb_node **p = &machine->threads.rb_node;
376 struct rb_node *parent = NULL;
377 struct thread *th;
378
379 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300380 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300381 * so most of the time we dont have to look up
382 * the full rbtree:
383 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300384 th = machine->last_match;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300385 if (th != NULL) {
386 if (th->tid == tid) {
387 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300388 return thread__get(th);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300389 }
390
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300391 machine->last_match = NULL;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300392 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300393
394 while (*p != NULL) {
395 parent = *p;
396 th = rb_entry(parent, struct thread, rb_node);
397
Adrian Hunter38051232013-07-04 16:20:31 +0300398 if (th->tid == tid) {
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300399 machine->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300400 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300401 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300402 }
403
Adrian Hunter38051232013-07-04 16:20:31 +0300404 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300405 p = &(*p)->rb_left;
406 else
407 p = &(*p)->rb_right;
408 }
409
410 if (!create)
411 return NULL;
412
Adrian Hunter99d725f2013-08-26 16:00:19 +0300413 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300414 if (th != NULL) {
415 rb_link_node(&th->rb_node, parent, p);
416 rb_insert_color(&th->rb_node, &machine->threads);
Jiri Olsacddcef62014-04-09 20:54:29 +0200417
418 /*
419 * We have to initialize map_groups separately
420 * after rb tree is updated.
421 *
422 * The reason is that we call machine__findnew_thread
423 * within thread__init_map_groups to find the thread
424 * leader and that would screwed the rb tree.
425 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300426 if (thread__init_map_groups(th, machine)) {
Arnaldo Carvalho de Melo0170b142015-05-25 15:23:05 -0300427 rb_erase_init(&th->rb_node, &machine->threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300428 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300429 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200430 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300431 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300432 /*
433 * It is now in the rbtree, get a ref
434 */
435 thread__get(th);
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300436 machine->last_match = th;
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300437 ++machine->nr_threads;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300438 }
439
440 return th;
441}
442
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300443struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
444{
445 return ____machine__findnew_thread(machine, pid, tid, true);
446}
447
Adrian Hunter314add62013-08-27 11:23:03 +0300448struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
449 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300450{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300451 struct thread *th;
452
453 pthread_rwlock_wrlock(&machine->threads_lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300454 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300455 pthread_rwlock_unlock(&machine->threads_lock);
456 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300457}
458
Jiri Olsad75e6092014-03-14 15:00:03 +0100459struct thread *machine__find_thread(struct machine *machine, pid_t pid,
460 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300461{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300462 struct thread *th;
463 pthread_rwlock_rdlock(&machine->threads_lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300464 th = ____machine__findnew_thread(machine, pid, tid, false);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300465 pthread_rwlock_unlock(&machine->threads_lock);
466 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300467}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300468
Adrian Huntercfe1c412014-07-31 09:00:45 +0300469struct comm *machine__thread_exec_comm(struct machine *machine,
470 struct thread *thread)
471{
472 if (machine->comm_exec)
473 return thread__exec_comm(thread);
474 else
475 return thread__comm(thread);
476}
477
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200478int machine__process_comm_event(struct machine *machine, union perf_event *event,
479 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300480{
Adrian Hunter314add62013-08-27 11:23:03 +0300481 struct thread *thread = machine__findnew_thread(machine,
482 event->comm.pid,
483 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300484 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300485 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300486
Adrian Huntercfe1c412014-07-31 09:00:45 +0300487 if (exec)
488 machine->comm_exec = true;
489
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300490 if (dump_trace)
491 perf_event__fprintf_comm(event, stdout);
492
Adrian Hunter65de51f2014-07-31 09:00:44 +0300493 if (thread == NULL ||
494 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300495 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300496 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300497 }
498
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300499 thread__put(thread);
500
501 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300502}
503
504int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200505 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300506{
507 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
508 event->lost.id, event->lost.lost);
509 return 0;
510}
511
Kan Liangc4937a92015-05-10 15:13:15 -0400512int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
513 union perf_event *event, struct perf_sample *sample)
514{
515 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
516 sample->id, event->lost_samples.lost);
517 return 0;
518}
519
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300520static struct dso *machine__findnew_module_dso(struct machine *machine,
521 struct kmod_path *m,
522 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100523{
524 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100525
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300526 pthread_rwlock_wrlock(&machine->dsos.lock);
527
528 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100529 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300530 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100531 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300532 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100533
534 if (machine__is_host(machine))
535 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
536 else
537 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
538
539 /* _KMODULE_COMP should be next to _KMODULE */
Jiri Olsaca333802015-02-17 17:29:57 +0100540 if (m->kmod && m->comp)
Jiri Olsada17ea32015-02-12 22:10:52 +0100541 dso->symtab_type++;
Jiri Olsaca333802015-02-17 17:29:57 +0100542
543 dso__set_short_name(dso, strdup(m->name), true);
544 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100545 }
546
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300547 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300548out_unlock:
549 pthread_rwlock_unlock(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100550 return dso;
551}
552
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300553int machine__process_aux_event(struct machine *machine __maybe_unused,
554 union perf_event *event)
555{
556 if (dump_trace)
557 perf_event__fprintf_aux(event, stdout);
558 return 0;
559}
560
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300561int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
562 union perf_event *event)
563{
564 if (dump_trace)
565 perf_event__fprintf_itrace_start(event, stdout);
566 return 0;
567}
568
Adrian Hunter02860392015-07-21 12:44:03 +0300569int machine__process_switch_event(struct machine *machine __maybe_unused,
570 union perf_event *event)
571{
572 if (dump_trace)
573 perf_event__fprintf_switch(event, stdout);
574 return 0;
575}
576
Wang Nanc03d5182015-11-26 03:59:57 +0000577static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
578{
579 const char *dup_filename;
580
581 if (!filename || !dso || !dso->long_name)
582 return;
583 if (dso->long_name[0] != '[')
584 return;
585 if (!strchr(filename, '/'))
586 return;
587
588 dup_filename = strdup(filename);
589 if (!dup_filename)
590 return;
591
Wang Nan5dcf16d2015-12-07 02:36:25 +0000592 dso__set_long_name(dso, dup_filename, true);
Wang Nanc03d5182015-11-26 03:59:57 +0000593}
594
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300595struct map *machine__findnew_module_map(struct machine *machine, u64 start,
596 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300597{
Jiri Olsaca333802015-02-17 17:29:57 +0100598 struct map *map = NULL;
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900599 struct dso *dso = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100600 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300601
Jiri Olsaca333802015-02-17 17:29:57 +0100602 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300603 return NULL;
604
Jiri Olsabc84f462015-02-17 17:31:18 +0100605 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
606 m.name);
Wang Nanc03d5182015-11-26 03:59:57 +0000607 if (map) {
608 /*
609 * If the map's dso is an offline module, give dso__load()
610 * a chance to find the file path of that module by fixing
611 * long_name.
612 */
613 dso__adjust_kmod_long_name(map->dso, filename);
Jiri Olsabc84f462015-02-17 17:31:18 +0100614 goto out;
Wang Nanc03d5182015-11-26 03:59:57 +0000615 }
Jiri Olsabc84f462015-02-17 17:31:18 +0100616
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300617 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100618 if (dso == NULL)
619 goto out;
620
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300621 map = map__new2(start, dso, MAP__FUNCTION);
622 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100623 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300624
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300625 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100626
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900627 /* Put the map here because map_groups__insert alread got it */
628 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100629out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900630 /* put the dso here, corresponding to machine__findnew_module_dso */
631 dso__put(dso);
Jiri Olsaca333802015-02-17 17:29:57 +0100632 free(m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300633 return map;
634}
635
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300636size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300637{
638 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300639 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300640
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300641 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300642 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300643 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300644 }
645
646 return ret;
647}
648
Waiman Long8fa7d872014-09-29 16:07:28 -0400649size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300650 bool (skip)(struct dso *dso, int parm), int parm)
651{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300652 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300653}
654
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300655size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300656 bool (skip)(struct dso *dso, int parm), int parm)
657{
658 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300659 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300660
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300661 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300662 struct machine *pos = rb_entry(nd, struct machine, rb_node);
663 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
664 }
665 return ret;
666}
667
668size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
669{
670 int i;
671 size_t printed = 0;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300672 struct dso *kdso = machine__kernel_map(machine)->dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300673
674 if (kdso->has_build_id) {
675 char filename[PATH_MAX];
676 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
677 printed += fprintf(fp, "[0] %s\n", filename);
678 }
679
680 for (i = 0; i < vmlinux_path__nr_entries; ++i)
681 printed += fprintf(fp, "[%d] %s\n",
682 i + kdso->has_build_id, vmlinux_path[i]);
683
684 return printed;
685}
686
687size_t machine__fprintf(struct machine *machine, FILE *fp)
688{
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300689 size_t ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300690 struct rb_node *nd;
691
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300692 pthread_rwlock_rdlock(&machine->threads_lock);
693
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300694 ret = fprintf(fp, "Threads: %u\n", machine->nr_threads);
695
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300696 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
697 struct thread *pos = rb_entry(nd, struct thread, rb_node);
698
699 ret += thread__fprintf(pos, fp);
700 }
701
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300702 pthread_rwlock_unlock(&machine->threads_lock);
703
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300704 return ret;
705}
706
707static struct dso *machine__get_kernel(struct machine *machine)
708{
709 const char *vmlinux_name = NULL;
710 struct dso *kernel;
711
712 if (machine__is_host(machine)) {
713 vmlinux_name = symbol_conf.vmlinux_name;
714 if (!vmlinux_name)
Masami Hiramatsu0a775822016-05-15 12:19:40 +0900715 vmlinux_name = DSO__NAME_KALLSYMS;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300716
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300717 kernel = machine__findnew_kernel(machine, vmlinux_name,
718 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300719 } else {
720 char bf[PATH_MAX];
721
722 if (machine__is_default_guest(machine))
723 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
724 if (!vmlinux_name)
725 vmlinux_name = machine__mmap_name(machine, bf,
726 sizeof(bf));
727
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300728 kernel = machine__findnew_kernel(machine, vmlinux_name,
729 "[guest.kernel]",
730 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300731 }
732
733 if (kernel != NULL && (!kernel->has_build_id))
734 dso__read_running_kernel_build_id(kernel, machine);
735
736 return kernel;
737}
738
739struct process_args {
740 u64 start;
741};
742
Adrian Hunter15a0a872014-01-29 16:14:38 +0200743static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
744 size_t bufsz)
745{
746 if (machine__is_default_guest(machine))
747 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
748 else
749 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
750}
751
Simon Quea93f0e52014-06-16 11:32:09 -0700752const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
753
754/* Figure out the start address of kernel map from /proc/kallsyms.
755 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
756 * symbol_name if it's not that important.
757 */
Adrian Hunter4b993752014-08-15 22:08:38 +0300758static u64 machine__get_running_kernel_start(struct machine *machine,
759 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300760{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200761 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700762 int i;
763 const char *name;
764 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300765
Adrian Hunter15a0a872014-01-29 16:14:38 +0200766 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300767
768 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
769 return 0;
770
Simon Quea93f0e52014-06-16 11:32:09 -0700771 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
772 addr = kallsyms__get_function_start(filename, name);
773 if (addr)
774 break;
775 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300776
Simon Quea93f0e52014-06-16 11:32:09 -0700777 if (symbol_name)
778 *symbol_name = name;
779
780 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300781}
782
783int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
784{
785 enum map_type type;
Adrian Hunter4b993752014-08-15 22:08:38 +0300786 u64 start = machine__get_running_kernel_start(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300787
Masami Hiramatsucc1121a2015-12-09 11:11:33 +0900788 /* In case of renewal the kernel map, destroy previous one */
789 machine__destroy_kernel_maps(machine);
790
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300791 for (type = 0; type < MAP__NR_TYPES; ++type) {
792 struct kmap *kmap;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300793 struct map *map;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300794
795 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
796 if (machine->vmlinux_maps[type] == NULL)
797 return -1;
798
799 machine->vmlinux_maps[type]->map_ip =
800 machine->vmlinux_maps[type]->unmap_ip =
801 identity__map_ip;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300802 map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300803 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000804 if (!kmap)
805 return -1;
806
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300807 kmap->kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300808 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300809 }
810
811 return 0;
812}
813
814void machine__destroy_kernel_maps(struct machine *machine)
815{
816 enum map_type type;
817
818 for (type = 0; type < MAP__NR_TYPES; ++type) {
819 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300820 struct map *map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300821
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300822 if (map == NULL)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300823 continue;
824
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300825 kmap = map__kmap(map);
826 map_groups__remove(&machine->kmaps, map);
Wang Nanba927322015-04-07 08:22:45 +0000827 if (kmap && kmap->ref_reloc_sym) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300828 /*
829 * ref_reloc_sym is shared among all maps, so free just
830 * on one of them.
831 */
832 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300833 zfree((char **)&kmap->ref_reloc_sym->name);
834 zfree(&kmap->ref_reloc_sym);
835 } else
836 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300837 }
838
Masami Hiramatsue96e4072015-11-18 15:40:22 +0900839 map__put(machine->vmlinux_maps[type]);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300840 machine->vmlinux_maps[type] = NULL;
841 }
842}
843
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300844int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300845{
846 int ret = 0;
847 struct dirent **namelist = NULL;
848 int i, items = 0;
849 char path[PATH_MAX];
850 pid_t pid;
851 char *endp;
852
853 if (symbol_conf.default_guest_vmlinux_name ||
854 symbol_conf.default_guest_modules ||
855 symbol_conf.default_guest_kallsyms) {
856 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
857 }
858
859 if (symbol_conf.guestmount) {
860 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
861 if (items <= 0)
862 return -ENOENT;
863 for (i = 0; i < items; i++) {
864 if (!isdigit(namelist[i]->d_name[0])) {
865 /* Filter out . and .. */
866 continue;
867 }
868 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
869 if ((*endp != '\0') ||
870 (endp == namelist[i]->d_name) ||
871 (errno == ERANGE)) {
872 pr_debug("invalid directory (%s). Skipping.\n",
873 namelist[i]->d_name);
874 continue;
875 }
876 sprintf(path, "%s/%s/proc/kallsyms",
877 symbol_conf.guestmount,
878 namelist[i]->d_name);
879 ret = access(path, R_OK);
880 if (ret) {
881 pr_debug("Can't access file %s\n", path);
882 goto failure;
883 }
884 machines__create_kernel_maps(machines, pid);
885 }
886failure:
887 free(namelist);
888 }
889
890 return ret;
891}
892
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300893void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300894{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300895 struct rb_node *next = rb_first(&machines->guests);
896
897 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300898
899 while (next) {
900 struct machine *pos = rb_entry(next, struct machine, rb_node);
901
902 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300903 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300904 machine__delete(pos);
905 }
906}
907
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300908int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300909{
910 struct machine *machine = machines__findnew(machines, pid);
911
912 if (machine == NULL)
913 return -1;
914
915 return machine__create_kernel_maps(machine);
916}
917
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300918int __machine__load_kallsyms(struct machine *machine, const char *filename,
919 enum map_type type, bool no_kcore, symbol_filter_t filter)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300920{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300921 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300922 int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore, filter);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300923
924 if (ret > 0) {
925 dso__set_loaded(map->dso, type);
926 /*
927 * Since /proc/kallsyms will have multiple sessions for the
928 * kernel, with modules between them, fixup the end of all
929 * sections.
930 */
931 __map_groups__fixup_end(&machine->kmaps, type);
932 }
933
934 return ret;
935}
936
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300937int machine__load_kallsyms(struct machine *machine, const char *filename,
938 enum map_type type, symbol_filter_t filter)
939{
940 return __machine__load_kallsyms(machine, filename, type, false, filter);
941}
942
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300943int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
944 symbol_filter_t filter)
945{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300946 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300947 int ret = dso__load_vmlinux_path(map->dso, map, filter);
948
Adrian Hunter39b12f782013-08-07 14:38:47 +0300949 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300950 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300951
952 return ret;
953}
954
955static void map_groups__fixup_end(struct map_groups *mg)
956{
957 int i;
958 for (i = 0; i < MAP__NR_TYPES; ++i)
959 __map_groups__fixup_end(mg, i);
960}
961
962static char *get_kernel_version(const char *root_dir)
963{
964 char version[PATH_MAX];
965 FILE *file;
966 char *name, *tmp;
967 const char *prefix = "Linux version ";
968
969 sprintf(version, "%s/proc/version", root_dir);
970 file = fopen(version, "r");
971 if (!file)
972 return NULL;
973
974 version[0] = '\0';
975 tmp = fgets(version, sizeof(version), file);
976 fclose(file);
977
978 name = strstr(version, prefix);
979 if (!name)
980 return NULL;
981 name += strlen(prefix);
982 tmp = strchr(name, ' ');
983 if (tmp)
984 *tmp = '\0';
985
986 return strdup(name);
987}
988
Jiri Olsabb58a8a2015-02-12 22:20:01 +0100989static bool is_kmod_dso(struct dso *dso)
990{
991 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
992 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
993}
994
995static int map_groups__set_module_path(struct map_groups *mg, const char *path,
996 struct kmod_path *m)
997{
998 struct map *map;
999 char *long_name;
1000
1001 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
1002 if (map == NULL)
1003 return 0;
1004
1005 long_name = strdup(path);
1006 if (long_name == NULL)
1007 return -ENOMEM;
1008
1009 dso__set_long_name(map->dso, long_name, true);
1010 dso__kernel_module_get_build_id(map->dso, "");
1011
1012 /*
1013 * Full name could reveal us kmod compression, so
1014 * we need to update the symtab_type if needed.
1015 */
1016 if (m->comp && is_kmod_dso(map->dso))
1017 map->dso->symtab_type++;
1018
1019 return 0;
1020}
1021
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001022static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -04001023 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001024{
1025 struct dirent *dent;
1026 DIR *dir = opendir(dir_name);
1027 int ret = 0;
1028
1029 if (!dir) {
1030 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1031 return -1;
1032 }
1033
1034 while ((dent = readdir(dir)) != NULL) {
1035 char path[PATH_MAX];
1036 struct stat st;
1037
1038 /*sshfs might return bad dent->d_type, so we have to stat*/
1039 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1040 if (stat(path, &st))
1041 continue;
1042
1043 if (S_ISDIR(st.st_mode)) {
1044 if (!strcmp(dent->d_name, ".") ||
1045 !strcmp(dent->d_name, ".."))
1046 continue;
1047
Richard Yao61d42902014-04-26 13:17:55 -04001048 /* Do not follow top-level source and build symlinks */
1049 if (depth == 0) {
1050 if (!strcmp(dent->d_name, "source") ||
1051 !strcmp(dent->d_name, "build"))
1052 continue;
1053 }
1054
1055 ret = map_groups__set_modules_path_dir(mg, path,
1056 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001057 if (ret < 0)
1058 goto out;
1059 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001060 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001061
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001062 ret = kmod_path__parse_name(&m, dent->d_name);
1063 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001064 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001065
1066 if (m.kmod)
1067 ret = map_groups__set_module_path(mg, path, &m);
1068
1069 free(m.name);
1070
1071 if (ret)
1072 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001073 }
1074 }
1075
1076out:
1077 closedir(dir);
1078 return ret;
1079}
1080
1081static int machine__set_modules_path(struct machine *machine)
1082{
1083 char *version;
1084 char modules_path[PATH_MAX];
1085
1086 version = get_kernel_version(machine->root_dir);
1087 if (!version)
1088 return -1;
1089
Richard Yao61d42902014-04-26 13:17:55 -04001090 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001091 machine->root_dir, version);
1092 free(version);
1093
Richard Yao61d42902014-04-26 13:17:55 -04001094 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001095}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001096int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1097 const char *name __maybe_unused)
1098{
1099 return 0;
1100}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001101
Adrian Hunter316d70d2013-10-08 11:45:48 +03001102static int machine__create_module(void *arg, const char *name, u64 start)
1103{
1104 struct machine *machine = arg;
1105 struct map *map;
1106
Song Shan Gong203d8a42016-07-21 11:10:51 +08001107 if (arch__fix_module_text_start(&start, name) < 0)
1108 return -1;
1109
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001110 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001111 if (map == NULL)
1112 return -1;
1113
1114 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1115
1116 return 0;
1117}
1118
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001119static int machine__create_modules(struct machine *machine)
1120{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001121 const char *modules;
1122 char path[PATH_MAX];
1123
Adrian Hunterf4be9042013-09-22 13:22:09 +03001124 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001125 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001126 } else {
1127 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001128 modules = path;
1129 }
1130
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001131 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001132 return -1;
1133
Adrian Hunter316d70d2013-10-08 11:45:48 +03001134 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001135 return -1;
1136
Adrian Hunter316d70d2013-10-08 11:45:48 +03001137 if (!machine__set_modules_path(machine))
1138 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001139
Adrian Hunter316d70d2013-10-08 11:45:48 +03001140 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001141
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001142 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001143}
1144
1145int machine__create_kernel_maps(struct machine *machine)
1146{
1147 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001148 const char *name;
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001149 u64 addr;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001150 int ret;
1151
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001152 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001153 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001154
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001155 ret = __machine__create_kernel_maps(machine, kernel);
1156 dso__put(kernel);
1157 if (ret < 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001158 return -1;
1159
1160 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1161 if (machine__is_host(machine))
1162 pr_debug("Problems creating module maps, "
1163 "continuing anyway...\n");
1164 else
1165 pr_debug("Problems creating module maps for guest %d, "
1166 "continuing anyway...\n", machine->pid);
1167 }
1168
1169 /*
1170 * Now that we have all the maps created, just set the ->end of them:
1171 */
1172 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001173
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001174 addr = machine__get_running_kernel_start(machine, &name);
1175 if (!addr) {
1176 } else if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) {
Adrian Hunter5512cf22014-01-29 16:14:39 +02001177 machine__destroy_kernel_maps(machine);
1178 return -1;
1179 }
1180
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001181 return 0;
1182}
1183
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001184static void machine__set_kernel_mmap_len(struct machine *machine,
1185 union perf_event *event)
1186{
Namhyung Kim4552cf02012-11-07 16:27:10 +09001187 int i;
1188
1189 for (i = 0; i < MAP__NR_TYPES; i++) {
1190 machine->vmlinux_maps[i]->start = event->mmap.start;
1191 machine->vmlinux_maps[i]->end = (event->mmap.start +
1192 event->mmap.len);
1193 /*
1194 * Be a bit paranoid here, some perf.data file came with
1195 * a zero sized synthesized MMAP event for the kernel.
1196 */
1197 if (machine->vmlinux_maps[i]->end == 0)
1198 machine->vmlinux_maps[i]->end = ~0ULL;
1199 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001200}
1201
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001202static bool machine__uses_kcore(struct machine *machine)
1203{
1204 struct dso *dso;
1205
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001206 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001207 if (dso__is_kcore(dso))
1208 return true;
1209 }
1210
1211 return false;
1212}
1213
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001214static int machine__process_kernel_mmap_event(struct machine *machine,
1215 union perf_event *event)
1216{
1217 struct map *map;
1218 char kmmap_prefix[PATH_MAX];
1219 enum dso_kernel_type kernel_type;
1220 bool is_kernel_mmap;
1221
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001222 /* If we have maps from kcore then we do not need or want any others */
1223 if (machine__uses_kcore(machine))
1224 return 0;
1225
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001226 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1227 if (machine__is_host(machine))
1228 kernel_type = DSO_TYPE_KERNEL;
1229 else
1230 kernel_type = DSO_TYPE_GUEST_KERNEL;
1231
1232 is_kernel_mmap = memcmp(event->mmap.filename,
1233 kmmap_prefix,
1234 strlen(kmmap_prefix) - 1) == 0;
1235 if (event->mmap.filename[0] == '/' ||
1236 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001237 map = machine__findnew_module_map(machine, event->mmap.start,
1238 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001239 if (map == NULL)
1240 goto out_problem;
1241
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001242 map->end = map->start + event->mmap.len;
1243 } else if (is_kernel_mmap) {
1244 const char *symbol_name = (event->mmap.filename +
1245 strlen(kmmap_prefix));
1246 /*
1247 * Should be there already, from the build-id table in
1248 * the header.
1249 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001250 struct dso *kernel = NULL;
1251 struct dso *dso;
1252
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001253 pthread_rwlock_rdlock(&machine->dsos.lock);
1254
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001255 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001256
1257 /*
1258 * The cpumode passed to is_kernel_module is not the
1259 * cpumode of *this* event. If we insist on passing
1260 * correct cpumode to is_kernel_module, we should
1261 * record the cpumode when we adding this dso to the
1262 * linked list.
1263 *
1264 * However we don't really need passing correct
1265 * cpumode. We know the correct cpumode must be kernel
1266 * mode (if not, we should not link it onto kernel_dsos
1267 * list).
1268 *
1269 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1270 * is_kernel_module() treats it as a kernel cpumode.
1271 */
1272
1273 if (!dso->kernel ||
1274 is_kernel_module(dso->long_name,
1275 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001276 continue;
1277
Wang Nan1f121b02015-06-03 08:52:21 +00001278
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001279 kernel = dso;
1280 break;
1281 }
1282
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001283 pthread_rwlock_unlock(&machine->dsos.lock);
1284
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001285 if (kernel == NULL)
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001286 kernel = machine__findnew_dso(machine, kmmap_prefix);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001287 if (kernel == NULL)
1288 goto out_problem;
1289
1290 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001291 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1292 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001293 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001294 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001295
Namhyung Kim330dfa22014-11-18 13:30:28 +09001296 if (strstr(kernel->long_name, "vmlinux"))
1297 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001298
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001299 machine__set_kernel_mmap_len(machine, event);
1300
1301 /*
1302 * Avoid using a zero address (kptr_restrict) for the ref reloc
1303 * symbol. Effectively having zero here means that at record
1304 * time /proc/sys/kernel/kptr_restrict was non zero.
1305 */
1306 if (event->mmap.pgoff != 0) {
1307 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1308 symbol_name,
1309 event->mmap.pgoff);
1310 }
1311
1312 if (machine__is_default_guest(machine)) {
1313 /*
1314 * preload dso of guest kernel and modules
1315 */
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001316 dso__load(kernel, machine__kernel_map(machine), NULL);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001317 }
1318 }
1319 return 0;
1320out_problem:
1321 return -1;
1322}
1323
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001324int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001325 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001326 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001327{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001328 struct thread *thread;
1329 struct map *map;
1330 enum map_type type;
1331 int ret = 0;
1332
1333 if (dump_trace)
1334 perf_event__fprintf_mmap2(event, stdout);
1335
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001336 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1337 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001338 ret = machine__process_kernel_mmap_event(machine, event);
1339 if (ret < 0)
1340 goto out_problem;
1341 return 0;
1342 }
1343
1344 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001345 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001346 if (thread == NULL)
1347 goto out_problem;
1348
1349 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1350 type = MAP__VARIABLE;
1351 else
1352 type = MAP__FUNCTION;
1353
Adrian Hunter2a030682014-07-22 16:17:53 +03001354 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001355 event->mmap2.len, event->mmap2.pgoff,
1356 event->mmap2.pid, event->mmap2.maj,
1357 event->mmap2.min, event->mmap2.ino,
1358 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001359 event->mmap2.prot,
1360 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001361 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001362
1363 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001364 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001365
He Kuang8132a2a2016-06-03 03:33:13 +00001366 ret = thread__insert_map(thread, map);
1367 if (ret)
1368 goto out_problem_insert;
1369
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001370 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001371 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001372 return 0;
1373
He Kuang8132a2a2016-06-03 03:33:13 +00001374out_problem_insert:
1375 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001376out_problem_map:
1377 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001378out_problem:
1379 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1380 return 0;
1381}
1382
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001383int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001384 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001385{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001386 struct thread *thread;
1387 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001388 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001389 int ret = 0;
1390
1391 if (dump_trace)
1392 perf_event__fprintf_mmap(event, stdout);
1393
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001394 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1395 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001396 ret = machine__process_kernel_mmap_event(machine, event);
1397 if (ret < 0)
1398 goto out_problem;
1399 return 0;
1400 }
1401
Adrian Hunter314add62013-08-27 11:23:03 +03001402 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001403 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001404 if (thread == NULL)
1405 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001406
1407 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1408 type = MAP__VARIABLE;
1409 else
1410 type = MAP__FUNCTION;
1411
Adrian Hunter2a030682014-07-22 16:17:53 +03001412 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001413 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001414 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001415 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001416 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001417
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001418 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001419 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001420
He Kuang8132a2a2016-06-03 03:33:13 +00001421 ret = thread__insert_map(thread, map);
1422 if (ret)
1423 goto out_problem_insert;
1424
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001425 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001426 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001427 return 0;
1428
He Kuang8132a2a2016-06-03 03:33:13 +00001429out_problem_insert:
1430 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001431out_problem_map:
1432 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001433out_problem:
1434 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1435 return 0;
1436}
1437
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001438static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001439{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001440 if (machine->last_match == th)
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -03001441 machine->last_match = NULL;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001442
Arnaldo Carvalho de Melo59a51c12015-05-15 15:32:55 -03001443 BUG_ON(atomic_read(&th->refcnt) == 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001444 if (lock)
1445 pthread_rwlock_wrlock(&machine->threads_lock);
Arnaldo Carvalho de Melo0170b142015-05-25 15:23:05 -03001446 rb_erase_init(&th->rb_node, &machine->threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001447 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -03001448 --machine->nr_threads;
David Ahern236a3bb2013-08-14 08:49:27 -06001449 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001450 * Move it first to the dead_threads list, then drop the reference,
1451 * if this is the last reference, then the thread__delete destructor
1452 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001453 */
1454 list_add_tail(&th->node, &machine->dead_threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001455 if (lock)
1456 pthread_rwlock_unlock(&machine->threads_lock);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001457 thread__put(th);
David Ahern236a3bb2013-08-14 08:49:27 -06001458}
1459
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001460void machine__remove_thread(struct machine *machine, struct thread *th)
1461{
1462 return __machine__remove_thread(machine, th, true);
1463}
1464
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001465int machine__process_fork_event(struct machine *machine, union perf_event *event,
1466 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001467{
Jiri Olsad75e6092014-03-14 15:00:03 +01001468 struct thread *thread = machine__find_thread(machine,
1469 event->fork.pid,
1470 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001471 struct thread *parent = machine__findnew_thread(machine,
1472 event->fork.ppid,
1473 event->fork.ptid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001474 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001475
Adrian Hunter5cb73342015-08-19 17:29:20 +03001476 if (dump_trace)
1477 perf_event__fprintf_task(event, stdout);
1478
1479 /*
1480 * There may be an existing thread that is not actually the parent,
1481 * either because we are processing events out of order, or because the
1482 * (fork) event that would have removed the thread was lost. Assume the
1483 * latter case and continue on as best we can.
1484 */
1485 if (parent->pid_ != (pid_t)event->fork.ppid) {
1486 dump_printf("removing erroneous parent thread %d/%d\n",
1487 parent->pid_, parent->tid);
1488 machine__remove_thread(machine, parent);
1489 thread__put(parent);
1490 parent = machine__findnew_thread(machine, event->fork.ppid,
1491 event->fork.ptid);
1492 }
1493
David Ahern236a3bb2013-08-14 08:49:27 -06001494 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001495 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001496 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001497 thread__put(thread);
1498 }
David Ahern236a3bb2013-08-14 08:49:27 -06001499
Adrian Hunter314add62013-08-27 11:23:03 +03001500 thread = machine__findnew_thread(machine, event->fork.pid,
1501 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001502
1503 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001504 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001505 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001506 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001507 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001508 thread__put(thread);
1509 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001510
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001511 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001512}
1513
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001514int machine__process_exit_event(struct machine *machine, union perf_event *event,
1515 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001516{
Jiri Olsad75e6092014-03-14 15:00:03 +01001517 struct thread *thread = machine__find_thread(machine,
1518 event->fork.pid,
1519 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001520
1521 if (dump_trace)
1522 perf_event__fprintf_task(event, stdout);
1523
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001524 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001525 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001526 thread__put(thread);
1527 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001528
1529 return 0;
1530}
1531
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001532int machine__process_event(struct machine *machine, union perf_event *event,
1533 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001534{
1535 int ret;
1536
1537 switch (event->header.type) {
1538 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001539 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001540 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001541 ret = machine__process_mmap_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001542 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001543 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001544 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001545 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001546 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001547 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001548 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001549 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001550 case PERF_RECORD_AUX:
1551 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001552 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001553 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001554 case PERF_RECORD_LOST_SAMPLES:
1555 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001556 case PERF_RECORD_SWITCH:
1557 case PERF_RECORD_SWITCH_CPU_WIDE:
1558 ret = machine__process_switch_event(machine, event); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001559 default:
1560 ret = -1;
1561 break;
1562 }
1563
1564 return ret;
1565}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001566
Greg Priceb21484f2012-12-06 21:48:05 -08001567static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001568{
Greg Priceb21484f2012-12-06 21:48:05 -08001569 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001570 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001571 return 0;
1572}
1573
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001574static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001575 struct addr_map_symbol *ams,
1576 u64 ip)
1577{
1578 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001579
1580 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001581 /*
1582 * We cannot use the header.misc hint to determine whether a
1583 * branch stack address is user, kernel, guest, hypervisor.
1584 * Branches may straddle the kernel/user/hypervisor boundaries.
1585 * Thus, we have to try consecutively until we find a match
1586 * or else, the symbol is unknown
1587 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001588 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001589
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001590 ams->addr = ip;
1591 ams->al_addr = al.addr;
1592 ams->sym = al.sym;
1593 ams->map = al.map;
1594}
1595
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001596static void ip__resolve_data(struct thread *thread,
Stephane Eranian98a3b322013-01-24 16:10:35 +01001597 u8 m, struct addr_map_symbol *ams, u64 addr)
1598{
1599 struct addr_location al;
1600
1601 memset(&al, 0, sizeof(al));
1602
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001603 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001604 if (al.map == NULL) {
1605 /*
1606 * some shared data regions have execute bit set which puts
1607 * their mapping in the MAP__FUNCTION type array.
1608 * Check there as a fallback option before dropping the sample.
1609 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001610 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001611 }
1612
Stephane Eranian98a3b322013-01-24 16:10:35 +01001613 ams->addr = addr;
1614 ams->al_addr = al.addr;
1615 ams->sym = al.sym;
1616 ams->map = al.map;
1617}
1618
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001619struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1620 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001621{
1622 struct mem_info *mi = zalloc(sizeof(*mi));
1623
1624 if (!mi)
1625 return NULL;
1626
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001627 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1628 ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001629 mi->data_src.val = sample->data_src;
1630
1631 return mi;
1632}
1633
Andi Kleen37592b82014-11-12 18:05:19 -08001634static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001635 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08001636 struct symbol **parent,
1637 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001638 u8 *cpumode,
Andi Kleen37592b82014-11-12 18:05:19 -08001639 u64 ip)
1640{
1641 struct addr_location al;
1642
1643 al.filtered = 0;
1644 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001645 if (!cpumode) {
Andi Kleen8b7bad52014-11-12 18:05:20 -08001646 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1647 ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001648 } else {
Kan Liang2e777842014-12-02 10:06:53 -05001649 if (ip >= PERF_CONTEXT_MAX) {
1650 switch (ip) {
1651 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001652 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05001653 break;
1654 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001655 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05001656 break;
1657 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001658 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05001659 break;
1660 default:
1661 pr_debug("invalid callchain context: "
1662 "%"PRId64"\n", (s64) ip);
1663 /*
1664 * It seems the callchain is corrupted.
1665 * Discard all.
1666 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001667 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05001668 return 1;
1669 }
1670 return 0;
1671 }
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001672 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1673 ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05001674 }
1675
Andi Kleen37592b82014-11-12 18:05:19 -08001676 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02001677 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08001678 symbol__match_regex(al.sym, &parent_regex))
1679 *parent = al.sym;
1680 else if (have_ignore_callees && root_al &&
1681 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1682 /* Treat this symbol as the root,
1683 forgetting its callees. */
1684 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001685 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08001686 }
1687 }
1688
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09001689 if (symbol_conf.hide_unresolved && al.sym == NULL)
1690 return 0;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001691 return callchain_cursor_append(cursor, al.addr, al.map, al.sym);
Andi Kleen37592b82014-11-12 18:05:19 -08001692}
1693
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001694struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1695 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001696{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001697 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001698 const struct branch_stack *bs = sample->branch_stack;
1699 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001700
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001701 if (!bi)
1702 return NULL;
1703
1704 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001705 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1706 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001707 bi[i].flags = bs->entries[i].flags;
1708 }
1709 return bi;
1710}
1711
Andi Kleen8b7bad52014-11-12 18:05:20 -08001712#define CHASHSZ 127
1713#define CHASHBITS 7
1714#define NO_ENTRY 0xff
1715
1716#define PERF_MAX_BRANCH_DEPTH 127
1717
1718/* Remove loops. */
1719static int remove_loops(struct branch_entry *l, int nr)
1720{
1721 int i, j, off;
1722 unsigned char chash[CHASHSZ];
1723
1724 memset(chash, NO_ENTRY, sizeof(chash));
1725
1726 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1727
1728 for (i = 0; i < nr; i++) {
1729 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1730
1731 /* no collision handling for now */
1732 if (chash[h] == NO_ENTRY) {
1733 chash[h] = i;
1734 } else if (l[chash[h]].from == l[i].from) {
1735 bool is_loop = true;
1736 /* check if it is a real loop */
1737 off = 0;
1738 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1739 if (l[j].from != l[i + off].from) {
1740 is_loop = false;
1741 break;
1742 }
1743 if (is_loop) {
1744 memmove(l + i, l + i + off,
1745 (nr - (i + off)) * sizeof(*l));
1746 nr -= off;
1747 }
1748 }
1749 }
1750 return nr;
1751}
1752
Kan Liang384b6052015-01-05 13:23:05 -05001753/*
1754 * Recolve LBR callstack chain sample
1755 * Return:
1756 * 1 on success get LBR callchain information
1757 * 0 no available LBR callchain information, should try fp
1758 * negative error code on other errors.
1759 */
1760static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001761 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001762 struct perf_sample *sample,
1763 struct symbol **parent,
1764 struct addr_location *root_al,
1765 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001766{
Kan Liang384b6052015-01-05 13:23:05 -05001767 struct ip_callchain *chain = sample->callchain;
1768 int chain_nr = min(max_stack, (int)chain->nr);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001769 u8 cpumode = PERF_RECORD_MISC_USER;
Kan Liang384b6052015-01-05 13:23:05 -05001770 int i, j, err;
1771 u64 ip;
1772
1773 for (i = 0; i < chain_nr; i++) {
1774 if (chain->ips[i] == PERF_CONTEXT_USER)
1775 break;
1776 }
1777
1778 /* LBR only affects the user callchain */
1779 if (i != chain_nr) {
1780 struct branch_stack *lbr_stack = sample->branch_stack;
1781 int lbr_nr = lbr_stack->nr;
1782 /*
1783 * LBR callstack can only get user call chain.
1784 * The mix_chain_nr is kernel call chain
1785 * number plus LBR user call chain number.
1786 * i is kernel call chain number,
1787 * 1 is PERF_CONTEXT_USER,
1788 * lbr_nr + 1 is the user call chain number.
1789 * For details, please refer to the comments
1790 * in callchain__printf
1791 */
1792 int mix_chain_nr = i + 1 + lbr_nr + 1;
1793
Kan Liang384b6052015-01-05 13:23:05 -05001794 for (j = 0; j < mix_chain_nr; j++) {
1795 if (callchain_param.order == ORDER_CALLEE) {
1796 if (j < i + 1)
1797 ip = chain->ips[j];
1798 else if (j > i + 1)
1799 ip = lbr_stack->entries[j - i - 2].from;
1800 else
1801 ip = lbr_stack->entries[0].to;
1802 } else {
1803 if (j < lbr_nr)
1804 ip = lbr_stack->entries[lbr_nr - j - 1].from;
1805 else if (j > lbr_nr)
1806 ip = chain->ips[i + 1 - (j - lbr_nr)];
1807 else
1808 ip = lbr_stack->entries[0].to;
1809 }
1810
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001811 err = add_callchain_ip(thread, cursor, parent, root_al, &cpumode, ip);
Kan Liang384b6052015-01-05 13:23:05 -05001812 if (err)
1813 return (err < 0) ? err : 0;
1814 }
1815 return 1;
1816 }
1817
1818 return 0;
1819}
1820
1821static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001822 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001823 struct perf_evsel *evsel,
1824 struct perf_sample *sample,
1825 struct symbol **parent,
1826 struct addr_location *root_al,
1827 int max_stack)
1828{
1829 struct branch_stack *branch = sample->branch_stack;
1830 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03001831 int chain_nr = chain->nr;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001832 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001833 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08001834 int skip_idx = -1;
1835 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001836
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03001837 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001838 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05001839 root_al, max_stack);
1840 if (err)
1841 return (err < 0) ? err : 0;
1842 }
1843
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001844 /*
1845 * Based on DWARF debug information, some architectures skip
1846 * a callchain entry saved by the kernel.
1847 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001848 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001849
Andi Kleen8b7bad52014-11-12 18:05:20 -08001850 /*
1851 * Add branches to call stack for easier browsing. This gives
1852 * more context for a sample than just the callers.
1853 *
1854 * This uses individual histograms of paths compared to the
1855 * aggregated histograms the normal LBR mode uses.
1856 *
1857 * Limitations for now:
1858 * - No extra filters
1859 * - No annotations (should annotate somehow)
1860 */
1861
1862 if (branch && callchain_param.branch_callstack) {
1863 int nr = min(max_stack, (int)branch->nr);
1864 struct branch_entry be[nr];
1865
1866 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
1867 pr_warning("corrupted branch chain. skipping...\n");
1868 goto check_calls;
1869 }
1870
1871 for (i = 0; i < nr; i++) {
1872 if (callchain_param.order == ORDER_CALLEE) {
1873 be[i] = branch->entries[i];
1874 /*
1875 * Check for overlap into the callchain.
1876 * The return address is one off compared to
1877 * the branch entry. To adjust for this
1878 * assume the calling instruction is not longer
1879 * than 8 bytes.
1880 */
1881 if (i == skip_idx ||
1882 chain->ips[first_call] >= PERF_CONTEXT_MAX)
1883 first_call++;
1884 else if (be[i].from < chain->ips[first_call] &&
1885 be[i].from >= chain->ips[first_call] - 8)
1886 first_call++;
1887 } else
1888 be[i] = branch->entries[branch->nr - i - 1];
1889 }
1890
1891 nr = remove_loops(be, nr);
1892
1893 for (i = 0; i < nr; i++) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001894 err = add_callchain_ip(thread, cursor, parent, root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001895 NULL, be[i].to);
Andi Kleen8b7bad52014-11-12 18:05:20 -08001896 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001897 err = add_callchain_ip(thread, cursor, parent, root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001898 NULL, be[i].from);
Andi Kleen8b7bad52014-11-12 18:05:20 -08001899 if (err == -EINVAL)
1900 break;
1901 if (err)
1902 return err;
1903 }
1904 chain_nr -= nr;
1905 }
1906
1907check_calls:
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001908 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03001909 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001910 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001911
1912 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001913 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001914 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001915 j = chain->nr - i - 1;
1916
1917#ifdef HAVE_SKIP_CALLCHAIN_IDX
1918 if (j == skip_idx)
1919 continue;
1920#endif
1921 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001922
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001923 if (ip < PERF_CONTEXT_MAX)
1924 ++nr_entries;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03001925
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001926 err = add_callchain_ip(thread, cursor, parent, root_al, &cpumode, ip);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001927
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001928 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05001929 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001930 }
1931
1932 return 0;
1933}
1934
1935static int unwind_entry(struct unwind_entry *entry, void *arg)
1936{
1937 struct callchain_cursor *cursor = arg;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09001938
1939 if (symbol_conf.hide_unresolved && entry->sym == NULL)
1940 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001941 return callchain_cursor_append(cursor, entry->ip,
1942 entry->map, entry->sym);
1943}
1944
Chris Phlipot9919a652016-04-28 01:19:06 -07001945static int thread__resolve_callchain_unwind(struct thread *thread,
1946 struct callchain_cursor *cursor,
1947 struct perf_evsel *evsel,
1948 struct perf_sample *sample,
1949 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001950{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001951 /* Can we do dwarf post unwind? */
1952 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1953 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1954 return 0;
1955
1956 /* Bail out if nothing was captured. */
1957 if ((!sample->user_regs.regs) ||
1958 (!sample->user_stack.size))
1959 return 0;
1960
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001961 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01001962 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07001963}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001964
Chris Phlipot9919a652016-04-28 01:19:06 -07001965int thread__resolve_callchain(struct thread *thread,
1966 struct callchain_cursor *cursor,
1967 struct perf_evsel *evsel,
1968 struct perf_sample *sample,
1969 struct symbol **parent,
1970 struct addr_location *root_al,
1971 int max_stack)
1972{
1973 int ret = 0;
1974
1975 callchain_cursor_reset(&callchain_cursor);
1976
1977 if (callchain_param.order == ORDER_CALLEE) {
1978 ret = thread__resolve_callchain_sample(thread, cursor,
1979 evsel, sample,
1980 parent, root_al,
1981 max_stack);
1982 if (ret)
1983 return ret;
1984 ret = thread__resolve_callchain_unwind(thread, cursor,
1985 evsel, sample,
1986 max_stack);
1987 } else {
1988 ret = thread__resolve_callchain_unwind(thread, cursor,
1989 evsel, sample,
1990 max_stack);
1991 if (ret)
1992 return ret;
1993 ret = thread__resolve_callchain_sample(thread, cursor,
1994 evsel, sample,
1995 parent, root_al,
1996 max_stack);
1997 }
1998
1999 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002000}
David Ahern35feee12013-09-28 13:12:58 -06002001
2002int machine__for_each_thread(struct machine *machine,
2003 int (*fn)(struct thread *thread, void *p),
2004 void *priv)
2005{
2006 struct rb_node *nd;
2007 struct thread *thread;
2008 int rc = 0;
2009
2010 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
2011 thread = rb_entry(nd, struct thread, rb_node);
2012 rc = fn(thread, priv);
2013 if (rc != 0)
2014 return rc;
2015 }
2016
2017 list_for_each_entry(thread, &machine->dead_threads, node) {
2018 rc = fn(thread, priv);
2019 if (rc != 0)
2020 return rc;
2021 }
2022 return rc;
2023}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002024
Adrian Huntera5499b32015-05-29 16:33:30 +03002025int machines__for_each_thread(struct machines *machines,
2026 int (*fn)(struct thread *thread, void *p),
2027 void *priv)
2028{
2029 struct rb_node *nd;
2030 int rc = 0;
2031
2032 rc = machine__for_each_thread(&machines->host, fn, priv);
2033 if (rc != 0)
2034 return rc;
2035
2036 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
2037 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2038
2039 rc = machine__for_each_thread(machine, fn, priv);
2040 if (rc != 0)
2041 return rc;
2042 }
2043 return rc;
2044}
2045
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03002046int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002047 struct target *target, struct thread_map *threads,
Kan Liang9d9cad72015-06-17 09:51:11 -04002048 perf_event__handler_t process, bool data_mmap,
2049 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002050{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002051 if (target__has_task(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002052 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002053 else if (target__has_cpu(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002054 return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002055 /* command specified */
2056 return 0;
2057}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002058
2059pid_t machine__get_current_tid(struct machine *machine, int cpu)
2060{
2061 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2062 return -1;
2063
2064 return machine->current_tid[cpu];
2065}
2066
2067int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2068 pid_t tid)
2069{
2070 struct thread *thread;
2071
2072 if (cpu < 0)
2073 return -EINVAL;
2074
2075 if (!machine->current_tid) {
2076 int i;
2077
2078 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2079 if (!machine->current_tid)
2080 return -ENOMEM;
2081 for (i = 0; i < MAX_NR_CPUS; i++)
2082 machine->current_tid[i] = -1;
2083 }
2084
2085 if (cpu >= MAX_NR_CPUS) {
2086 pr_err("Requested CPU %d too large. ", cpu);
2087 pr_err("Consider raising MAX_NR_CPUS\n");
2088 return -EINVAL;
2089 }
2090
2091 machine->current_tid[cpu] = tid;
2092
2093 thread = machine__findnew_thread(machine, pid, tid);
2094 if (!thread)
2095 return -ENOMEM;
2096
2097 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002098 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002099
2100 return 0;
2101}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002102
2103int machine__get_kernel_start(struct machine *machine)
2104{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002105 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002106 int err = 0;
2107
2108 /*
2109 * The only addresses above 2^63 are kernel addresses of a 64-bit
2110 * kernel. Note that addresses are unsigned so that on a 32-bit system
2111 * all addresses including kernel addresses are less than 2^32. In
2112 * that case (32-bit system), if the kernel mapping is unknown, all
2113 * addresses will be assumed to be in user space - see
2114 * machine__kernel_ip().
2115 */
2116 machine->kernel_start = 1ULL << 63;
2117 if (map) {
2118 err = map__load(map, machine->symbol_filter);
2119 if (map->start)
2120 machine->kernel_start = map->start;
2121 }
2122 return err;
2123}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002124
2125struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2126{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03002127 return dsos__findnew(&machine->dsos, filename);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002128}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002129
2130char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2131{
2132 struct machine *machine = vmachine;
2133 struct map *map;
2134 struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map, NULL);
2135
2136 if (sym == NULL)
2137 return NULL;
2138
2139 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2140 *addrp = map->unmap_ip(map, sym->start);
2141 return sym->name;
2142}