blob: e354d94a68e85ee33d31644f84ecc755de89de8c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -03002#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03003#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03004#include <inttypes.h>
Arnaldo Carvalho de Melo1eae20c2017-04-18 12:33:30 -03005#include <regex.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03006#include "callchain.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03007#include "debug.h"
8#include "event.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03009#include "evsel.h"
10#include "hist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030011#include "machine.h"
12#include "map.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030013#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030014#include "strlist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030015#include "thread.h"
Adrian Hunterd027b642014-07-23 14:23:00 +030016#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030017#include <stdbool.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030018#include <sys/types.h>
19#include <sys/stat.h>
20#include <unistd.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030021#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080022#include "linux/hash.h"
Hari Bathinif3b36142017-03-08 02:11:43 +053023#include "asm/bug.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030024
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030025#include "sane_ctype.h"
26#include <symbol/kallsyms.h>
27
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030028static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
29
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030030static void dsos__init(struct dsos *dsos)
31{
32 INIT_LIST_HEAD(&dsos->head);
33 dsos->root = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030034 init_rwsem(&dsos->lock);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030035}
36
Kan Liang91e467b2017-09-10 19:23:14 -070037static void machine__threads_init(struct machine *machine)
38{
39 int i;
40
41 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
42 struct threads *threads = &machine->threads[i];
43 threads->entries = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030044 init_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -070045 threads->nr = 0;
46 INIT_LIST_HEAD(&threads->dead);
47 threads->last_match = NULL;
48 }
49}
50
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010051static int machine__set_mmap_name(struct machine *machine)
52{
Jiri Olsac1925242018-03-12 16:24:06 +010053 if (machine__is_host(machine))
54 machine->mmap_name = strdup("[kernel.kallsyms]");
55 else if (machine__is_default_guest(machine))
56 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
57 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
58 machine->pid) < 0)
59 machine->mmap_name = NULL;
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010060
61 return machine->mmap_name ? 0 : -ENOMEM;
62}
63
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030064int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
65{
Jiri Olsa81f981d2018-02-15 13:26:29 +010066 int err = -ENOMEM;
67
Wang Nan93b0ba32015-12-08 02:25:44 +000068 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030069 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030070 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030071 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030072
Kan Liang91e467b2017-09-10 19:23:14 -070073 machine__threads_init(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030074
Adrian Hunterd027b642014-07-23 14:23:00 +030075 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030076 machine->env = NULL;
Adrian Hunterd027b642014-07-23 14:23:00 +030077
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030078 machine->pid = pid;
79
Jiri Olsa14bd6d22014-01-07 13:47:19 +010080 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -030081 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +030082 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030083 machine->kernel_start = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030084
Masami Hiramatsucc1121a2015-12-09 11:11:33 +090085 memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps));
86
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030087 machine->root_dir = strdup(root_dir);
88 if (machine->root_dir == NULL)
89 return -ENOMEM;
90
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010091 if (machine__set_mmap_name(machine))
92 goto out;
93
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030094 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030095 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030096 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030097 char comm[64];
98
99 if (thread == NULL)
Jiri Olsa81f981d2018-02-15 13:26:29 +0100100 goto out;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300101
102 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200103 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300104 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300105 }
106
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300107 machine->current_tid = NULL;
Jiri Olsa81f981d2018-02-15 13:26:29 +0100108 err = 0;
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300109
Jiri Olsa81f981d2018-02-15 13:26:29 +0100110out:
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100111 if (err) {
Jiri Olsa81f981d2018-02-15 13:26:29 +0100112 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100113 zfree(&machine->mmap_name);
114 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300115 return 0;
116}
117
David Ahern8fb598e2013-09-28 13:13:00 -0600118struct machine *machine__new_host(void)
119{
120 struct machine *machine = malloc(sizeof(*machine));
121
122 if (machine != NULL) {
123 machine__init(machine, "", HOST_KERNEL_ID);
124
125 if (machine__create_kernel_maps(machine) < 0)
126 goto out_delete;
127 }
128
129 return machine;
130out_delete:
131 free(machine);
132 return NULL;
133}
134
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300135struct machine *machine__new_kallsyms(void)
136{
137 struct machine *machine = machine__new_host();
138 /*
139 * FIXME:
140 * 1) MAP__FUNCTION will go away when we stop loading separate maps for
141 * functions and data objects.
142 * 2) We should switch to machine__load_kallsyms(), i.e. not explicitely
143 * ask for not using the kcore parsing code, once this one is fixed
144 * to create a map per module.
145 */
Jiri Olsae8f38792018-02-15 13:26:33 +0100146 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms", MAP__FUNCTION) <= 0) {
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300147 machine__delete(machine);
148 machine = NULL;
149 }
150
151 return machine;
152}
153
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300154static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300155{
156 struct dso *pos, *n;
157
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300158 down_write(&dsos->lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300159
Waiman Long8fa7d872014-09-29 16:07:28 -0400160 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -0400161 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +0200162 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300163 list_del_init(&pos->node);
164 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300165 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300166
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300167 up_write(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300168}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300169
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300170static void dsos__exit(struct dsos *dsos)
171{
172 dsos__purge(dsos);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300173 exit_rwsem(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300174}
175
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300176void machine__delete_threads(struct machine *machine)
177{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300178 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700179 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300180
Kan Liang91e467b2017-09-10 19:23:14 -0700181 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
182 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300183 down_write(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -0700184 nd = rb_first(&threads->entries);
185 while (nd) {
186 struct thread *t = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300187
Kan Liang91e467b2017-09-10 19:23:14 -0700188 nd = rb_next(nd);
189 __machine__remove_thread(machine, t, false);
190 }
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300191 up_write(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300192 }
193}
194
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300195void machine__exit(struct machine *machine)
196{
Kan Liang91e467b2017-09-10 19:23:14 -0700197 int i;
198
Arnaldo Carvalho de Melo19993b82017-11-13 16:06:29 -0300199 if (machine == NULL)
200 return;
201
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900202 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300203 map_groups__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300204 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300205 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300206 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100207 zfree(&machine->mmap_name);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300208 zfree(&machine->current_tid);
Kan Liang91e467b2017-09-10 19:23:14 -0700209
210 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
211 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300212 exit_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -0700213 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300214}
215
216void machine__delete(struct machine *machine)
217{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300218 if (machine) {
219 machine__exit(machine);
220 free(machine);
221 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300222}
223
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300224void machines__init(struct machines *machines)
225{
226 machine__init(&machines->host, "", HOST_KERNEL_ID);
227 machines->guests = RB_ROOT;
228}
229
230void machines__exit(struct machines *machines)
231{
232 machine__exit(&machines->host);
233 /* XXX exit guest */
234}
235
236struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300237 const char *root_dir)
238{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300239 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300240 struct rb_node *parent = NULL;
241 struct machine *pos, *machine = malloc(sizeof(*machine));
242
243 if (machine == NULL)
244 return NULL;
245
246 if (machine__init(machine, root_dir, pid) != 0) {
247 free(machine);
248 return NULL;
249 }
250
251 while (*p != NULL) {
252 parent = *p;
253 pos = rb_entry(parent, struct machine, rb_node);
254 if (pid < pos->pid)
255 p = &(*p)->rb_left;
256 else
257 p = &(*p)->rb_right;
258 }
259
260 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300261 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300262
263 return machine;
264}
265
Adrian Huntercfe1c412014-07-31 09:00:45 +0300266void machines__set_comm_exec(struct machines *machines, bool comm_exec)
267{
268 struct rb_node *nd;
269
270 machines->host.comm_exec = comm_exec;
271
272 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
273 struct machine *machine = rb_entry(nd, struct machine, rb_node);
274
275 machine->comm_exec = comm_exec;
276 }
277}
278
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300279struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300280{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300281 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300282 struct rb_node *parent = NULL;
283 struct machine *machine;
284 struct machine *default_machine = NULL;
285
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300286 if (pid == HOST_KERNEL_ID)
287 return &machines->host;
288
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300289 while (*p != NULL) {
290 parent = *p;
291 machine = rb_entry(parent, struct machine, rb_node);
292 if (pid < machine->pid)
293 p = &(*p)->rb_left;
294 else if (pid > machine->pid)
295 p = &(*p)->rb_right;
296 else
297 return machine;
298 if (!machine->pid)
299 default_machine = machine;
300 }
301
302 return default_machine;
303}
304
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300305struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300306{
307 char path[PATH_MAX];
308 const char *root_dir = "";
309 struct machine *machine = machines__find(machines, pid);
310
311 if (machine && (machine->pid == pid))
312 goto out;
313
314 if ((pid != HOST_KERNEL_ID) &&
315 (pid != DEFAULT_GUEST_KERNEL_ID) &&
316 (symbol_conf.guestmount)) {
317 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
318 if (access(path, R_OK)) {
319 static struct strlist *seen;
320
321 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300322 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300323
324 if (!strlist__has_entry(seen, path)) {
325 pr_err("Can't access file %s\n", path);
326 strlist__add(seen, path);
327 }
328 machine = NULL;
329 goto out;
330 }
331 root_dir = path;
332 }
333
334 machine = machines__add(machines, pid, root_dir);
335out:
336 return machine;
337}
338
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300339void machines__process_guests(struct machines *machines,
340 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300341{
342 struct rb_node *nd;
343
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300344 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300345 struct machine *pos = rb_entry(nd, struct machine, rb_node);
346 process(pos, data);
347 }
348}
349
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300350void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300351{
352 struct rb_node *node;
353 struct machine *machine;
354
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300355 machines->host.id_hdr_size = id_hdr_size;
356
357 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300358 machine = rb_entry(node, struct machine, rb_node);
359 machine->id_hdr_size = id_hdr_size;
360 }
361
362 return;
363}
364
Adrian Hunter29ce3612014-07-16 11:07:13 +0300365static void machine__update_thread_pid(struct machine *machine,
366 struct thread *th, pid_t pid)
367{
368 struct thread *leader;
369
370 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
371 return;
372
373 th->pid_ = pid;
374
375 if (th->pid_ == th->tid)
376 return;
377
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300378 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300379 if (!leader)
380 goto out_err;
381
382 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300383 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300384
385 if (!leader->mg)
386 goto out_err;
387
388 if (th->mg == leader->mg)
389 return;
390
391 if (th->mg) {
392 /*
393 * Maps are created from MMAP events which provide the pid and
394 * tid. Consequently there never should be any maps on a thread
395 * with an unknown pid. Just print an error if there are.
396 */
397 if (!map_groups__empty(th->mg))
398 pr_err("Discarding thread maps for %d:%d\n",
399 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300400 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300401 }
402
403 th->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300404out_put:
405 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300406 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300407out_err:
408 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300409 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300410}
411
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300412/*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800413 * Caller must eventually drop thread->refcnt returned with a successful
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300414 * lookup/new thread inserted.
415 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300416static struct thread *____machine__findnew_thread(struct machine *machine,
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300417 struct threads *threads,
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300418 pid_t pid, pid_t tid,
419 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300420{
Kan Liang91e467b2017-09-10 19:23:14 -0700421 struct rb_node **p = &threads->entries.rb_node;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300422 struct rb_node *parent = NULL;
423 struct thread *th;
424
425 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300426 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300427 * so most of the time we dont have to look up
428 * the full rbtree:
429 */
Kan Liang91e467b2017-09-10 19:23:14 -0700430 th = threads->last_match;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300431 if (th != NULL) {
432 if (th->tid == tid) {
433 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300434 return thread__get(th);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300435 }
436
Kan Liang91e467b2017-09-10 19:23:14 -0700437 threads->last_match = NULL;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300438 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300439
440 while (*p != NULL) {
441 parent = *p;
442 th = rb_entry(parent, struct thread, rb_node);
443
Adrian Hunter38051232013-07-04 16:20:31 +0300444 if (th->tid == tid) {
Kan Liang91e467b2017-09-10 19:23:14 -0700445 threads->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300446 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300447 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300448 }
449
Adrian Hunter38051232013-07-04 16:20:31 +0300450 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300451 p = &(*p)->rb_left;
452 else
453 p = &(*p)->rb_right;
454 }
455
456 if (!create)
457 return NULL;
458
Adrian Hunter99d725f2013-08-26 16:00:19 +0300459 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300460 if (th != NULL) {
461 rb_link_node(&th->rb_node, parent, p);
Kan Liang91e467b2017-09-10 19:23:14 -0700462 rb_insert_color(&th->rb_node, &threads->entries);
Jiri Olsacddcef62014-04-09 20:54:29 +0200463
464 /*
465 * We have to initialize map_groups separately
466 * after rb tree is updated.
467 *
468 * The reason is that we call machine__findnew_thread
469 * within thread__init_map_groups to find the thread
470 * leader and that would screwed the rb tree.
471 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300472 if (thread__init_map_groups(th, machine)) {
Kan Liang91e467b2017-09-10 19:23:14 -0700473 rb_erase_init(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300474 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300475 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200476 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300477 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300478 /*
479 * It is now in the rbtree, get a ref
480 */
481 thread__get(th);
Kan Liang91e467b2017-09-10 19:23:14 -0700482 threads->last_match = th;
483 ++threads->nr;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300484 }
485
486 return th;
487}
488
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300489struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
490{
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300491 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300492}
493
Adrian Hunter314add62013-08-27 11:23:03 +0300494struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
495 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300496{
Kan Liang91e467b2017-09-10 19:23:14 -0700497 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300498 struct thread *th;
499
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300500 down_write(&threads->lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300501 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300502 up_write(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300503 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300504}
505
Jiri Olsad75e6092014-03-14 15:00:03 +0100506struct thread *machine__find_thread(struct machine *machine, pid_t pid,
507 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300508{
Kan Liang91e467b2017-09-10 19:23:14 -0700509 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300510 struct thread *th;
Kan Liang91e467b2017-09-10 19:23:14 -0700511
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300512 down_read(&threads->lock);
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300513 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300514 up_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300515 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300516}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300517
Adrian Huntercfe1c412014-07-31 09:00:45 +0300518struct comm *machine__thread_exec_comm(struct machine *machine,
519 struct thread *thread)
520{
521 if (machine->comm_exec)
522 return thread__exec_comm(thread);
523 else
524 return thread__comm(thread);
525}
526
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200527int machine__process_comm_event(struct machine *machine, union perf_event *event,
528 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300529{
Adrian Hunter314add62013-08-27 11:23:03 +0300530 struct thread *thread = machine__findnew_thread(machine,
531 event->comm.pid,
532 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300533 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300534 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300535
Adrian Huntercfe1c412014-07-31 09:00:45 +0300536 if (exec)
537 machine->comm_exec = true;
538
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300539 if (dump_trace)
540 perf_event__fprintf_comm(event, stdout);
541
Adrian Hunter65de51f2014-07-31 09:00:44 +0300542 if (thread == NULL ||
543 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300544 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300545 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300546 }
547
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300548 thread__put(thread);
549
550 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300551}
552
Hari Bathinif3b36142017-03-08 02:11:43 +0530553int machine__process_namespaces_event(struct machine *machine __maybe_unused,
554 union perf_event *event,
555 struct perf_sample *sample __maybe_unused)
556{
557 struct thread *thread = machine__findnew_thread(machine,
558 event->namespaces.pid,
559 event->namespaces.tid);
560 int err = 0;
561
562 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
563 "\nWARNING: kernel seems to support more namespaces than perf"
564 " tool.\nTry updating the perf tool..\n\n");
565
566 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
567 "\nWARNING: perf tool seems to support more namespaces than"
568 " the kernel.\nTry updating the kernel..\n\n");
569
570 if (dump_trace)
571 perf_event__fprintf_namespaces(event, stdout);
572
573 if (thread == NULL ||
574 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
575 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
576 err = -1;
577 }
578
579 thread__put(thread);
580
581 return err;
582}
583
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300584int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200585 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300586{
587 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
588 event->lost.id, event->lost.lost);
589 return 0;
590}
591
Kan Liangc4937a92015-05-10 15:13:15 -0400592int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
593 union perf_event *event, struct perf_sample *sample)
594{
595 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
596 sample->id, event->lost_samples.lost);
597 return 0;
598}
599
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300600static struct dso *machine__findnew_module_dso(struct machine *machine,
601 struct kmod_path *m,
602 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100603{
604 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100605
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300606 down_write(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300607
608 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100609 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300610 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100611 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300612 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100613
Namhyung Kim6b335e82017-05-31 21:01:04 +0900614 dso__set_module_info(dso, m, machine);
Jiri Olsaca333802015-02-17 17:29:57 +0100615 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100616 }
617
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300618 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300619out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300620 up_write(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100621 return dso;
622}
623
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300624int machine__process_aux_event(struct machine *machine __maybe_unused,
625 union perf_event *event)
626{
627 if (dump_trace)
628 perf_event__fprintf_aux(event, stdout);
629 return 0;
630}
631
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300632int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
633 union perf_event *event)
634{
635 if (dump_trace)
636 perf_event__fprintf_itrace_start(event, stdout);
637 return 0;
638}
639
Adrian Hunter02860392015-07-21 12:44:03 +0300640int machine__process_switch_event(struct machine *machine __maybe_unused,
641 union perf_event *event)
642{
643 if (dump_trace)
644 perf_event__fprintf_switch(event, stdout);
645 return 0;
646}
647
Wang Nanc03d5182015-11-26 03:59:57 +0000648static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
649{
650 const char *dup_filename;
651
652 if (!filename || !dso || !dso->long_name)
653 return;
654 if (dso->long_name[0] != '[')
655 return;
656 if (!strchr(filename, '/'))
657 return;
658
659 dup_filename = strdup(filename);
660 if (!dup_filename)
661 return;
662
Wang Nan5dcf16d2015-12-07 02:36:25 +0000663 dso__set_long_name(dso, dup_filename, true);
Wang Nanc03d5182015-11-26 03:59:57 +0000664}
665
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300666struct map *machine__findnew_module_map(struct machine *machine, u64 start,
667 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300668{
Jiri Olsaca333802015-02-17 17:29:57 +0100669 struct map *map = NULL;
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900670 struct dso *dso = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100671 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300672
Jiri Olsaca333802015-02-17 17:29:57 +0100673 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300674 return NULL;
675
Jiri Olsabc84f462015-02-17 17:31:18 +0100676 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
677 m.name);
Wang Nanc03d5182015-11-26 03:59:57 +0000678 if (map) {
679 /*
680 * If the map's dso is an offline module, give dso__load()
681 * a chance to find the file path of that module by fixing
682 * long_name.
683 */
684 dso__adjust_kmod_long_name(map->dso, filename);
Jiri Olsabc84f462015-02-17 17:31:18 +0100685 goto out;
Wang Nanc03d5182015-11-26 03:59:57 +0000686 }
Jiri Olsabc84f462015-02-17 17:31:18 +0100687
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300688 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100689 if (dso == NULL)
690 goto out;
691
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300692 map = map__new2(start, dso, MAP__FUNCTION);
693 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100694 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300695
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300696 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100697
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900698 /* Put the map here because map_groups__insert alread got it */
699 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100700out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900701 /* put the dso here, corresponding to machine__findnew_module_dso */
702 dso__put(dso);
Jiri Olsaca333802015-02-17 17:29:57 +0100703 free(m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300704 return map;
705}
706
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300707size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300708{
709 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300710 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300711
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300712 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300713 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300714 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300715 }
716
717 return ret;
718}
719
Waiman Long8fa7d872014-09-29 16:07:28 -0400720size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300721 bool (skip)(struct dso *dso, int parm), int parm)
722{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300723 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300724}
725
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300726size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300727 bool (skip)(struct dso *dso, int parm), int parm)
728{
729 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300730 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300731
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300732 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300733 struct machine *pos = rb_entry(nd, struct machine, rb_node);
734 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
735 }
736 return ret;
737}
738
739size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
740{
741 int i;
742 size_t printed = 0;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300743 struct dso *kdso = machine__kernel_map(machine)->dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300744
745 if (kdso->has_build_id) {
746 char filename[PATH_MAX];
Krister Johansend2396992017-07-05 18:48:13 -0700747 if (dso__build_id_filename(kdso, filename, sizeof(filename),
748 false))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300749 printed += fprintf(fp, "[0] %s\n", filename);
750 }
751
752 for (i = 0; i < vmlinux_path__nr_entries; ++i)
753 printed += fprintf(fp, "[%d] %s\n",
754 i + kdso->has_build_id, vmlinux_path[i]);
755
756 return printed;
757}
758
759size_t machine__fprintf(struct machine *machine, FILE *fp)
760{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300761 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700762 size_t ret;
763 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300764
Kan Liang91e467b2017-09-10 19:23:14 -0700765 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
766 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300767
768 down_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300769
Kan Liang91e467b2017-09-10 19:23:14 -0700770 ret = fprintf(fp, "Threads: %u\n", threads->nr);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300771
Kan Liang91e467b2017-09-10 19:23:14 -0700772 for (nd = rb_first(&threads->entries); nd; nd = rb_next(nd)) {
773 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300774
Kan Liang91e467b2017-09-10 19:23:14 -0700775 ret += thread__fprintf(pos, fp);
776 }
777
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300778 up_read(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300779 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300780 return ret;
781}
782
783static struct dso *machine__get_kernel(struct machine *machine)
784{
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100785 const char *vmlinux_name = machine->mmap_name;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300786 struct dso *kernel;
787
788 if (machine__is_host(machine)) {
Jiri Olsac1925242018-03-12 16:24:06 +0100789 if (symbol_conf.vmlinux_name)
790 vmlinux_name = symbol_conf.vmlinux_name;
791
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300792 kernel = machine__findnew_kernel(machine, vmlinux_name,
793 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300794 } else {
Jiri Olsac1925242018-03-12 16:24:06 +0100795 if (symbol_conf.default_guest_vmlinux_name)
796 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
797
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300798 kernel = machine__findnew_kernel(machine, vmlinux_name,
799 "[guest.kernel]",
800 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300801 }
802
803 if (kernel != NULL && (!kernel->has_build_id))
804 dso__read_running_kernel_build_id(kernel, machine);
805
806 return kernel;
807}
808
809struct process_args {
810 u64 start;
811};
812
Adrian Hunter15a0a872014-01-29 16:14:38 +0200813static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
814 size_t bufsz)
815{
816 if (machine__is_default_guest(machine))
817 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
818 else
819 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
820}
821
Simon Quea93f0e52014-06-16 11:32:09 -0700822const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
823
824/* Figure out the start address of kernel map from /proc/kallsyms.
825 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
826 * symbol_name if it's not that important.
827 */
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300828static int machine__get_running_kernel_start(struct machine *machine,
829 const char **symbol_name, u64 *start)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300830{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200831 char filename[PATH_MAX];
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300832 int i, err = -1;
Simon Quea93f0e52014-06-16 11:32:09 -0700833 const char *name;
834 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300835
Adrian Hunter15a0a872014-01-29 16:14:38 +0200836 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300837
838 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
839 return 0;
840
Simon Quea93f0e52014-06-16 11:32:09 -0700841 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300842 err = kallsyms__get_function_start(filename, name, &addr);
843 if (!err)
Simon Quea93f0e52014-06-16 11:32:09 -0700844 break;
845 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300846
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300847 if (err)
848 return -1;
849
Simon Quea93f0e52014-06-16 11:32:09 -0700850 if (symbol_name)
851 *symbol_name = name;
852
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300853 *start = addr;
854 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300855}
856
Jiri Olsa1fb87b82018-02-15 13:26:32 +0100857static int
858__machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300859{
Arnaldo Carvalho de Meloa0b2f5a2017-02-14 16:19:56 -0300860 int type;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300861
Masami Hiramatsucc1121a2015-12-09 11:11:33 +0900862 /* In case of renewal the kernel map, destroy previous one */
863 machine__destroy_kernel_maps(machine);
864
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300865 for (type = 0; type < MAP__NR_TYPES; ++type) {
866 struct kmap *kmap;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300867 struct map *map;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300868
Jiri Olsa1fb87b82018-02-15 13:26:32 +0100869 machine->vmlinux_maps[type] = map__new2(0, kernel, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300870 if (machine->vmlinux_maps[type] == NULL)
871 return -1;
872
873 machine->vmlinux_maps[type]->map_ip =
874 machine->vmlinux_maps[type]->unmap_ip =
875 identity__map_ip;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300876 map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300877 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000878 if (!kmap)
879 return -1;
880
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300881 kmap->kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300882 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300883 }
884
885 return 0;
886}
887
888void machine__destroy_kernel_maps(struct machine *machine)
889{
Arnaldo Carvalho de Meloa0b2f5a2017-02-14 16:19:56 -0300890 int type;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300891
892 for (type = 0; type < MAP__NR_TYPES; ++type) {
893 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300894 struct map *map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300895
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300896 if (map == NULL)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300897 continue;
898
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300899 kmap = map__kmap(map);
900 map_groups__remove(&machine->kmaps, map);
Wang Nanba927322015-04-07 08:22:45 +0000901 if (kmap && kmap->ref_reloc_sym) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300902 /*
903 * ref_reloc_sym is shared among all maps, so free just
904 * on one of them.
905 */
906 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300907 zfree((char **)&kmap->ref_reloc_sym->name);
908 zfree(&kmap->ref_reloc_sym);
909 } else
910 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300911 }
912
Masami Hiramatsue96e4072015-11-18 15:40:22 +0900913 map__put(machine->vmlinux_maps[type]);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300914 machine->vmlinux_maps[type] = NULL;
915 }
916}
917
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300918int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300919{
920 int ret = 0;
921 struct dirent **namelist = NULL;
922 int i, items = 0;
923 char path[PATH_MAX];
924 pid_t pid;
925 char *endp;
926
927 if (symbol_conf.default_guest_vmlinux_name ||
928 symbol_conf.default_guest_modules ||
929 symbol_conf.default_guest_kallsyms) {
930 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
931 }
932
933 if (symbol_conf.guestmount) {
934 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
935 if (items <= 0)
936 return -ENOENT;
937 for (i = 0; i < items; i++) {
938 if (!isdigit(namelist[i]->d_name[0])) {
939 /* Filter out . and .. */
940 continue;
941 }
942 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
943 if ((*endp != '\0') ||
944 (endp == namelist[i]->d_name) ||
945 (errno == ERANGE)) {
946 pr_debug("invalid directory (%s). Skipping.\n",
947 namelist[i]->d_name);
948 continue;
949 }
950 sprintf(path, "%s/%s/proc/kallsyms",
951 symbol_conf.guestmount,
952 namelist[i]->d_name);
953 ret = access(path, R_OK);
954 if (ret) {
955 pr_debug("Can't access file %s\n", path);
956 goto failure;
957 }
958 machines__create_kernel_maps(machines, pid);
959 }
960failure:
961 free(namelist);
962 }
963
964 return ret;
965}
966
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300967void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300968{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300969 struct rb_node *next = rb_first(&machines->guests);
970
971 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300972
973 while (next) {
974 struct machine *pos = rb_entry(next, struct machine, rb_node);
975
976 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300977 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300978 machine__delete(pos);
979 }
980}
981
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300982int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300983{
984 struct machine *machine = machines__findnew(machines, pid);
985
986 if (machine == NULL)
987 return -1;
988
989 return machine__create_kernel_maps(machine);
990}
991
Jiri Olsae8f38792018-02-15 13:26:33 +0100992int machine__load_kallsyms(struct machine *machine, const char *filename,
993 enum map_type type)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300994{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300995 struct map *map = machine__kernel_map(machine);
Jiri Olsae8f38792018-02-15 13:26:33 +0100996 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300997
998 if (ret > 0) {
999 dso__set_loaded(map->dso, type);
1000 /*
1001 * Since /proc/kallsyms will have multiple sessions for the
1002 * kernel, with modules between them, fixup the end of all
1003 * sections.
1004 */
1005 __map_groups__fixup_end(&machine->kmaps, type);
1006 }
1007
1008 return ret;
1009}
1010
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001011int machine__load_vmlinux_path(struct machine *machine, enum map_type type)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001012{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001013 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001014 int ret = dso__load_vmlinux_path(map->dso, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001015
Adrian Hunter39b12f782013-08-07 14:38:47 +03001016 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001017 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001018
1019 return ret;
1020}
1021
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001022static char *get_kernel_version(const char *root_dir)
1023{
1024 char version[PATH_MAX];
1025 FILE *file;
1026 char *name, *tmp;
1027 const char *prefix = "Linux version ";
1028
1029 sprintf(version, "%s/proc/version", root_dir);
1030 file = fopen(version, "r");
1031 if (!file)
1032 return NULL;
1033
1034 version[0] = '\0';
1035 tmp = fgets(version, sizeof(version), file);
1036 fclose(file);
1037
1038 name = strstr(version, prefix);
1039 if (!name)
1040 return NULL;
1041 name += strlen(prefix);
1042 tmp = strchr(name, ' ');
1043 if (tmp)
1044 *tmp = '\0';
1045
1046 return strdup(name);
1047}
1048
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001049static bool is_kmod_dso(struct dso *dso)
1050{
1051 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1052 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1053}
1054
1055static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1056 struct kmod_path *m)
1057{
1058 struct map *map;
1059 char *long_name;
1060
1061 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
1062 if (map == NULL)
1063 return 0;
1064
1065 long_name = strdup(path);
1066 if (long_name == NULL)
1067 return -ENOMEM;
1068
1069 dso__set_long_name(map->dso, long_name, true);
1070 dso__kernel_module_get_build_id(map->dso, "");
1071
1072 /*
1073 * Full name could reveal us kmod compression, so
1074 * we need to update the symtab_type if needed.
1075 */
1076 if (m->comp && is_kmod_dso(map->dso))
1077 map->dso->symtab_type++;
1078
1079 return 0;
1080}
1081
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001082static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -04001083 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001084{
1085 struct dirent *dent;
1086 DIR *dir = opendir(dir_name);
1087 int ret = 0;
1088
1089 if (!dir) {
1090 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1091 return -1;
1092 }
1093
1094 while ((dent = readdir(dir)) != NULL) {
1095 char path[PATH_MAX];
1096 struct stat st;
1097
1098 /*sshfs might return bad dent->d_type, so we have to stat*/
1099 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1100 if (stat(path, &st))
1101 continue;
1102
1103 if (S_ISDIR(st.st_mode)) {
1104 if (!strcmp(dent->d_name, ".") ||
1105 !strcmp(dent->d_name, ".."))
1106 continue;
1107
Richard Yao61d42902014-04-26 13:17:55 -04001108 /* Do not follow top-level source and build symlinks */
1109 if (depth == 0) {
1110 if (!strcmp(dent->d_name, "source") ||
1111 !strcmp(dent->d_name, "build"))
1112 continue;
1113 }
1114
1115 ret = map_groups__set_modules_path_dir(mg, path,
1116 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001117 if (ret < 0)
1118 goto out;
1119 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001120 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001121
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001122 ret = kmod_path__parse_name(&m, dent->d_name);
1123 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001124 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001125
1126 if (m.kmod)
1127 ret = map_groups__set_module_path(mg, path, &m);
1128
1129 free(m.name);
1130
1131 if (ret)
1132 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001133 }
1134 }
1135
1136out:
1137 closedir(dir);
1138 return ret;
1139}
1140
1141static int machine__set_modules_path(struct machine *machine)
1142{
1143 char *version;
1144 char modules_path[PATH_MAX];
1145
1146 version = get_kernel_version(machine->root_dir);
1147 if (!version)
1148 return -1;
1149
Richard Yao61d42902014-04-26 13:17:55 -04001150 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001151 machine->root_dir, version);
1152 free(version);
1153
Richard Yao61d42902014-04-26 13:17:55 -04001154 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001155}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001156int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1157 const char *name __maybe_unused)
1158{
1159 return 0;
1160}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001161
Thomas Richter9ad46522017-08-03 15:49:02 +02001162static int machine__create_module(void *arg, const char *name, u64 start,
1163 u64 size)
Adrian Hunter316d70d2013-10-08 11:45:48 +03001164{
1165 struct machine *machine = arg;
1166 struct map *map;
1167
Song Shan Gong203d8a42016-07-21 11:10:51 +08001168 if (arch__fix_module_text_start(&start, name) < 0)
1169 return -1;
1170
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001171 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001172 if (map == NULL)
1173 return -1;
Thomas Richter9ad46522017-08-03 15:49:02 +02001174 map->end = start + size;
Adrian Hunter316d70d2013-10-08 11:45:48 +03001175
1176 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1177
1178 return 0;
1179}
1180
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001181static int machine__create_modules(struct machine *machine)
1182{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001183 const char *modules;
1184 char path[PATH_MAX];
1185
Adrian Hunterf4be9042013-09-22 13:22:09 +03001186 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001187 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001188 } else {
1189 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001190 modules = path;
1191 }
1192
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001193 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001194 return -1;
1195
Adrian Hunter316d70d2013-10-08 11:45:48 +03001196 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001197 return -1;
1198
Adrian Hunter316d70d2013-10-08 11:45:48 +03001199 if (!machine__set_modules_path(machine))
1200 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001201
Adrian Hunter316d70d2013-10-08 11:45:48 +03001202 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001203
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001204 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001205}
1206
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001207static void machine__set_kernel_mmap(struct machine *machine,
1208 u64 start, u64 end)
1209{
1210 int i;
1211
1212 for (i = 0; i < MAP__NR_TYPES; i++) {
1213 machine->vmlinux_maps[i]->start = start;
1214 machine->vmlinux_maps[i]->end = end;
1215
1216 /*
1217 * Be a bit paranoid here, some perf.data file came with
1218 * a zero sized synthesized MMAP event for the kernel.
1219 */
Namhyung Kim1d12cec2018-02-19 19:00:46 +09001220 if (start == 0 && end == 0)
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001221 machine->vmlinux_maps[i]->end = ~0ULL;
1222 }
1223}
1224
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001225int machine__create_kernel_maps(struct machine *machine)
1226{
1227 struct dso *kernel = machine__get_kernel(machine);
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001228 const char *name = NULL;
Namhyung Kimee05d21792018-02-19 19:05:45 +09001229 struct map *map;
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001230 u64 addr = 0;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001231 int ret;
1232
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001233 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001234 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001235
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001236 ret = __machine__create_kernel_maps(machine, kernel);
1237 dso__put(kernel);
1238 if (ret < 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001239 return -1;
1240
1241 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1242 if (machine__is_host(machine))
1243 pr_debug("Problems creating module maps, "
1244 "continuing anyway...\n");
1245 else
1246 pr_debug("Problems creating module maps for guest %d, "
1247 "continuing anyway...\n", machine->pid);
1248 }
1249
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001250 if (!machine__get_running_kernel_start(machine, &name, &addr)) {
1251 if (name &&
1252 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) {
1253 machine__destroy_kernel_maps(machine);
1254 return -1;
1255 }
Namhyung Kimee05d21792018-02-19 19:05:45 +09001256
1257 /* we have a real start address now, so re-order the kmaps */
1258 map = machine__kernel_map(machine);
1259
1260 map__get(map);
1261 map_groups__remove(&machine->kmaps, map);
1262
1263 /* assume it's the last in the kmaps */
1264 machine__set_kernel_mmap(machine, addr, ~0ULL);
1265
1266 map_groups__insert(&machine->kmaps, map);
1267 map__put(map);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001268 }
1269
Namhyung Kimee05d21792018-02-19 19:05:45 +09001270 /* update end address of the kernel map using adjacent module address */
1271 map = map__next(machine__kernel_map(machine));
1272 if (map)
1273 machine__set_kernel_mmap(machine, addr, map->start);
1274
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001275 return 0;
1276}
1277
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001278static bool machine__uses_kcore(struct machine *machine)
1279{
1280 struct dso *dso;
1281
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001282 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001283 if (dso__is_kcore(dso))
1284 return true;
1285 }
1286
1287 return false;
1288}
1289
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001290static int machine__process_kernel_mmap_event(struct machine *machine,
1291 union perf_event *event)
1292{
1293 struct map *map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001294 enum dso_kernel_type kernel_type;
1295 bool is_kernel_mmap;
1296
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001297 /* If we have maps from kcore then we do not need or want any others */
1298 if (machine__uses_kcore(machine))
1299 return 0;
1300
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001301 if (machine__is_host(machine))
1302 kernel_type = DSO_TYPE_KERNEL;
1303 else
1304 kernel_type = DSO_TYPE_GUEST_KERNEL;
1305
1306 is_kernel_mmap = memcmp(event->mmap.filename,
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001307 machine->mmap_name,
1308 strlen(machine->mmap_name) - 1) == 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001309 if (event->mmap.filename[0] == '/' ||
1310 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001311 map = machine__findnew_module_map(machine, event->mmap.start,
1312 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001313 if (map == NULL)
1314 goto out_problem;
1315
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001316 map->end = map->start + event->mmap.len;
1317 } else if (is_kernel_mmap) {
1318 const char *symbol_name = (event->mmap.filename +
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001319 strlen(machine->mmap_name));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001320 /*
1321 * Should be there already, from the build-id table in
1322 * the header.
1323 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001324 struct dso *kernel = NULL;
1325 struct dso *dso;
1326
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001327 down_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001328
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001329 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001330
1331 /*
1332 * The cpumode passed to is_kernel_module is not the
1333 * cpumode of *this* event. If we insist on passing
1334 * correct cpumode to is_kernel_module, we should
1335 * record the cpumode when we adding this dso to the
1336 * linked list.
1337 *
1338 * However we don't really need passing correct
1339 * cpumode. We know the correct cpumode must be kernel
1340 * mode (if not, we should not link it onto kernel_dsos
1341 * list).
1342 *
1343 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1344 * is_kernel_module() treats it as a kernel cpumode.
1345 */
1346
1347 if (!dso->kernel ||
1348 is_kernel_module(dso->long_name,
1349 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001350 continue;
1351
Wang Nan1f121b02015-06-03 08:52:21 +00001352
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001353 kernel = dso;
1354 break;
1355 }
1356
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001357 up_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001358
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001359 if (kernel == NULL)
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001360 kernel = machine__findnew_dso(machine, machine->mmap_name);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001361 if (kernel == NULL)
1362 goto out_problem;
1363
1364 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001365 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1366 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001367 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001368 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001369
Namhyung Kim330dfa22014-11-18 13:30:28 +09001370 if (strstr(kernel->long_name, "vmlinux"))
1371 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001372
Jiri Olsa05db6ff2018-02-15 13:26:31 +01001373 machine__set_kernel_mmap(machine, event->mmap.start,
1374 event->mmap.start + event->mmap.len);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001375
1376 /*
1377 * Avoid using a zero address (kptr_restrict) for the ref reloc
1378 * symbol. Effectively having zero here means that at record
1379 * time /proc/sys/kernel/kptr_restrict was non zero.
1380 */
1381 if (event->mmap.pgoff != 0) {
1382 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1383 symbol_name,
1384 event->mmap.pgoff);
1385 }
1386
1387 if (machine__is_default_guest(machine)) {
1388 /*
1389 * preload dso of guest kernel and modules
1390 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001391 dso__load(kernel, machine__kernel_map(machine));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001392 }
1393 }
1394 return 0;
1395out_problem:
1396 return -1;
1397}
1398
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001399int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001400 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001401 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001402{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001403 struct thread *thread;
1404 struct map *map;
1405 enum map_type type;
1406 int ret = 0;
1407
1408 if (dump_trace)
1409 perf_event__fprintf_mmap2(event, stdout);
1410
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001411 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1412 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001413 ret = machine__process_kernel_mmap_event(machine, event);
1414 if (ret < 0)
1415 goto out_problem;
1416 return 0;
1417 }
1418
1419 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001420 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001421 if (thread == NULL)
1422 goto out_problem;
1423
1424 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1425 type = MAP__VARIABLE;
1426 else
1427 type = MAP__FUNCTION;
1428
Adrian Hunter2a030682014-07-22 16:17:53 +03001429 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001430 event->mmap2.len, event->mmap2.pgoff,
Krister Johansenbf2e7102017-07-05 18:48:09 -07001431 event->mmap2.maj,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001432 event->mmap2.min, event->mmap2.ino,
1433 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001434 event->mmap2.prot,
1435 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001436 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001437
1438 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001439 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001440
He Kuang8132a2a2016-06-03 03:33:13 +00001441 ret = thread__insert_map(thread, map);
1442 if (ret)
1443 goto out_problem_insert;
1444
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001445 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001446 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001447 return 0;
1448
He Kuang8132a2a2016-06-03 03:33:13 +00001449out_problem_insert:
1450 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001451out_problem_map:
1452 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001453out_problem:
1454 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1455 return 0;
1456}
1457
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001458int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001459 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001460{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001461 struct thread *thread;
1462 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001463 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001464 int ret = 0;
1465
1466 if (dump_trace)
1467 perf_event__fprintf_mmap(event, stdout);
1468
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001469 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1470 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001471 ret = machine__process_kernel_mmap_event(machine, event);
1472 if (ret < 0)
1473 goto out_problem;
1474 return 0;
1475 }
1476
Adrian Hunter314add62013-08-27 11:23:03 +03001477 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001478 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001479 if (thread == NULL)
1480 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001481
1482 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1483 type = MAP__VARIABLE;
1484 else
1485 type = MAP__FUNCTION;
1486
Adrian Hunter2a030682014-07-22 16:17:53 +03001487 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001488 event->mmap.len, event->mmap.pgoff,
Krister Johansenbf2e7102017-07-05 18:48:09 -07001489 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001490 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001491 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001492
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001493 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001494 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001495
He Kuang8132a2a2016-06-03 03:33:13 +00001496 ret = thread__insert_map(thread, map);
1497 if (ret)
1498 goto out_problem_insert;
1499
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001500 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001501 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001502 return 0;
1503
He Kuang8132a2a2016-06-03 03:33:13 +00001504out_problem_insert:
1505 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001506out_problem_map:
1507 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001508out_problem:
1509 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1510 return 0;
1511}
1512
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001513static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001514{
Kan Liang91e467b2017-09-10 19:23:14 -07001515 struct threads *threads = machine__threads(machine, th->tid);
1516
1517 if (threads->last_match == th)
1518 threads->last_match = NULL;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001519
Elena Reshetovae34f5b12017-02-21 17:35:02 +02001520 BUG_ON(refcount_read(&th->refcnt) == 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001521 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001522 down_write(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -07001523 rb_erase_init(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001524 RB_CLEAR_NODE(&th->rb_node);
Kan Liang91e467b2017-09-10 19:23:14 -07001525 --threads->nr;
David Ahern236a3bb2013-08-14 08:49:27 -06001526 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001527 * Move it first to the dead_threads list, then drop the reference,
1528 * if this is the last reference, then the thread__delete destructor
1529 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001530 */
Kan Liang91e467b2017-09-10 19:23:14 -07001531 list_add_tail(&th->node, &threads->dead);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001532 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001533 up_write(&threads->lock);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001534 thread__put(th);
David Ahern236a3bb2013-08-14 08:49:27 -06001535}
1536
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001537void machine__remove_thread(struct machine *machine, struct thread *th)
1538{
1539 return __machine__remove_thread(machine, th, true);
1540}
1541
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001542int machine__process_fork_event(struct machine *machine, union perf_event *event,
1543 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001544{
Jiri Olsad75e6092014-03-14 15:00:03 +01001545 struct thread *thread = machine__find_thread(machine,
1546 event->fork.pid,
1547 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001548 struct thread *parent = machine__findnew_thread(machine,
1549 event->fork.ppid,
1550 event->fork.ptid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001551 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001552
Adrian Hunter5cb73342015-08-19 17:29:20 +03001553 if (dump_trace)
1554 perf_event__fprintf_task(event, stdout);
1555
1556 /*
1557 * There may be an existing thread that is not actually the parent,
1558 * either because we are processing events out of order, or because the
1559 * (fork) event that would have removed the thread was lost. Assume the
1560 * latter case and continue on as best we can.
1561 */
1562 if (parent->pid_ != (pid_t)event->fork.ppid) {
1563 dump_printf("removing erroneous parent thread %d/%d\n",
1564 parent->pid_, parent->tid);
1565 machine__remove_thread(machine, parent);
1566 thread__put(parent);
1567 parent = machine__findnew_thread(machine, event->fork.ppid,
1568 event->fork.ptid);
1569 }
1570
David Ahern236a3bb2013-08-14 08:49:27 -06001571 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001572 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001573 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001574 thread__put(thread);
1575 }
David Ahern236a3bb2013-08-14 08:49:27 -06001576
Adrian Hunter314add62013-08-27 11:23:03 +03001577 thread = machine__findnew_thread(machine, event->fork.pid,
1578 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001579
1580 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001581 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001582 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001583 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001584 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001585 thread__put(thread);
1586 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001587
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001588 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001589}
1590
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001591int machine__process_exit_event(struct machine *machine, union perf_event *event,
1592 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001593{
Jiri Olsad75e6092014-03-14 15:00:03 +01001594 struct thread *thread = machine__find_thread(machine,
1595 event->fork.pid,
1596 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001597
1598 if (dump_trace)
1599 perf_event__fprintf_task(event, stdout);
1600
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001601 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001602 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001603 thread__put(thread);
1604 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001605
1606 return 0;
1607}
1608
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001609int machine__process_event(struct machine *machine, union perf_event *event,
1610 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001611{
1612 int ret;
1613
1614 switch (event->header.type) {
1615 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001616 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001617 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001618 ret = machine__process_mmap_event(machine, event, sample); break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301619 case PERF_RECORD_NAMESPACES:
1620 ret = machine__process_namespaces_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001621 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001622 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001623 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001624 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001625 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001626 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001627 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001628 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001629 case PERF_RECORD_AUX:
1630 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001631 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001632 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001633 case PERF_RECORD_LOST_SAMPLES:
1634 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001635 case PERF_RECORD_SWITCH:
1636 case PERF_RECORD_SWITCH_CPU_WIDE:
1637 ret = machine__process_switch_event(machine, event); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001638 default:
1639 ret = -1;
1640 break;
1641 }
1642
1643 return ret;
1644}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001645
Greg Priceb21484f2012-12-06 21:48:05 -08001646static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001647{
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001648 if (!regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001649 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001650 return 0;
1651}
1652
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001653static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001654 struct addr_map_symbol *ams,
1655 u64 ip)
1656{
1657 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001658
1659 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001660 /*
1661 * We cannot use the header.misc hint to determine whether a
1662 * branch stack address is user, kernel, guest, hypervisor.
1663 * Branches may straddle the kernel/user/hypervisor boundaries.
1664 * Thus, we have to try consecutively until we find a match
1665 * or else, the symbol is unknown
1666 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001667 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001668
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001669 ams->addr = ip;
1670 ams->al_addr = al.addr;
1671 ams->sym = al.sym;
1672 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001673 ams->phys_addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001674}
1675
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001676static void ip__resolve_data(struct thread *thread,
Kan Liang8780fb22017-08-29 13:11:09 -04001677 u8 m, struct addr_map_symbol *ams,
1678 u64 addr, u64 phys_addr)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001679{
1680 struct addr_location al;
1681
1682 memset(&al, 0, sizeof(al));
1683
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03001684 __thread__find_symbol(thread, m, MAP__VARIABLE, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001685 if (al.map == NULL) {
1686 /*
1687 * some shared data regions have execute bit set which puts
1688 * their mapping in the MAP__FUNCTION type array.
1689 * Check there as a fallback option before dropping the sample.
1690 */
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03001691 thread__find_symbol(thread, m, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001692 }
1693
Stephane Eranian98a3b322013-01-24 16:10:35 +01001694 ams->addr = addr;
1695 ams->al_addr = al.addr;
1696 ams->sym = al.sym;
1697 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001698 ams->phys_addr = phys_addr;
Stephane Eranian98a3b322013-01-24 16:10:35 +01001699}
1700
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001701struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1702 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001703{
Jiri Olsa9f874982018-03-07 16:50:06 +01001704 struct mem_info *mi = mem_info__new();
Stephane Eranian98a3b322013-01-24 16:10:35 +01001705
1706 if (!mi)
1707 return NULL;
1708
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001709 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
Kan Liang8780fb22017-08-29 13:11:09 -04001710 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1711 sample->addr, sample->phys_addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001712 mi->data_src.val = sample->data_src;
1713
1714 return mi;
1715}
1716
Milian Wolff40a342c2017-10-09 22:32:56 +02001717static char *callchain_srcline(struct map *map, struct symbol *sym, u64 ip)
1718{
Milian Wolff21ac9d52017-10-19 13:38:34 +02001719 char *srcline = NULL;
Milian Wolff40a342c2017-10-09 22:32:56 +02001720
Milian Wolff21ac9d52017-10-19 13:38:34 +02001721 if (!map || callchain_param.key == CCKEY_FUNCTION)
1722 return srcline;
1723
1724 srcline = srcline__tree_find(&map->dso->srclines, ip);
1725 if (!srcline) {
1726 bool show_sym = false;
1727 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
1728
1729 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
Jin Yao935f5a92017-12-30 00:26:52 +08001730 sym, show_sym, show_addr, ip);
Milian Wolff21ac9d52017-10-19 13:38:34 +02001731 srcline__tree_insert(&map->dso->srclines, ip, srcline);
1732 }
1733
1734 return srcline;
Milian Wolff40a342c2017-10-09 22:32:56 +02001735}
1736
Jin Yaoc4ee0622017-08-07 21:05:15 +08001737struct iterations {
1738 int nr_loop_iter;
1739 u64 cycles;
1740};
1741
Andi Kleen37592b82014-11-12 18:05:19 -08001742static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001743 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08001744 struct symbol **parent,
1745 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001746 u8 *cpumode,
Jin Yao410024d2016-10-31 09:19:49 +08001747 u64 ip,
1748 bool branch,
1749 struct branch_flags *flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08001750 struct iterations *iter,
Jin Yaob851dd42017-07-18 20:13:15 +08001751 u64 branch_from)
Andi Kleen37592b82014-11-12 18:05:19 -08001752{
1753 struct addr_location al;
Jin Yaoc4ee0622017-08-07 21:05:15 +08001754 int nr_loop_iter = 0;
1755 u64 iter_cycles = 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02001756 const char *srcline = NULL;
Andi Kleen37592b82014-11-12 18:05:19 -08001757
1758 al.filtered = 0;
1759 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001760 if (!cpumode) {
Andi Kleen8b7bad52014-11-12 18:05:20 -08001761 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1762 ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001763 } else {
Kan Liang2e777842014-12-02 10:06:53 -05001764 if (ip >= PERF_CONTEXT_MAX) {
1765 switch (ip) {
1766 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001767 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05001768 break;
1769 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001770 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05001771 break;
1772 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001773 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05001774 break;
1775 default:
1776 pr_debug("invalid callchain context: "
1777 "%"PRId64"\n", (s64) ip);
1778 /*
1779 * It seems the callchain is corrupted.
1780 * Discard all.
1781 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001782 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05001783 return 1;
1784 }
1785 return 0;
1786 }
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03001787 thread__find_symbol(thread, *cpumode, ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05001788 }
1789
Andi Kleen37592b82014-11-12 18:05:19 -08001790 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02001791 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08001792 symbol__match_regex(al.sym, &parent_regex))
1793 *parent = al.sym;
1794 else if (have_ignore_callees && root_al &&
1795 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1796 /* Treat this symbol as the root,
1797 forgetting its callees. */
1798 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001799 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08001800 }
1801 }
1802
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09001803 if (symbol_conf.hide_unresolved && al.sym == NULL)
1804 return 0;
Jin Yaoc4ee0622017-08-07 21:05:15 +08001805
1806 if (iter) {
1807 nr_loop_iter = iter->nr_loop_iter;
1808 iter_cycles = iter->cycles;
1809 }
1810
Milian Wolff40a342c2017-10-09 22:32:56 +02001811 srcline = callchain_srcline(al.map, al.sym, al.addr);
Jin Yao410024d2016-10-31 09:19:49 +08001812 return callchain_cursor_append(cursor, al.addr, al.map, al.sym,
Jin Yaoc4ee0622017-08-07 21:05:15 +08001813 branch, flags, nr_loop_iter,
Milian Wolff40a342c2017-10-09 22:32:56 +02001814 iter_cycles, branch_from, srcline);
Andi Kleen37592b82014-11-12 18:05:19 -08001815}
1816
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001817struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1818 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001819{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001820 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001821 const struct branch_stack *bs = sample->branch_stack;
1822 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001823
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001824 if (!bi)
1825 return NULL;
1826
1827 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001828 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1829 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001830 bi[i].flags = bs->entries[i].flags;
1831 }
1832 return bi;
1833}
1834
Jin Yaoc4ee0622017-08-07 21:05:15 +08001835static void save_iterations(struct iterations *iter,
1836 struct branch_entry *be, int nr)
1837{
1838 int i;
1839
1840 iter->nr_loop_iter = nr;
1841 iter->cycles = 0;
1842
1843 for (i = 0; i < nr; i++)
1844 iter->cycles += be[i].flags.cycles;
1845}
1846
Andi Kleen8b7bad52014-11-12 18:05:20 -08001847#define CHASHSZ 127
1848#define CHASHBITS 7
1849#define NO_ENTRY 0xff
1850
1851#define PERF_MAX_BRANCH_DEPTH 127
1852
1853/* Remove loops. */
Jin Yaoc4ee0622017-08-07 21:05:15 +08001854static int remove_loops(struct branch_entry *l, int nr,
1855 struct iterations *iter)
Andi Kleen8b7bad52014-11-12 18:05:20 -08001856{
1857 int i, j, off;
1858 unsigned char chash[CHASHSZ];
1859
1860 memset(chash, NO_ENTRY, sizeof(chash));
1861
1862 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1863
1864 for (i = 0; i < nr; i++) {
1865 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1866
1867 /* no collision handling for now */
1868 if (chash[h] == NO_ENTRY) {
1869 chash[h] = i;
1870 } else if (l[chash[h]].from == l[i].from) {
1871 bool is_loop = true;
1872 /* check if it is a real loop */
1873 off = 0;
1874 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1875 if (l[j].from != l[i + off].from) {
1876 is_loop = false;
1877 break;
1878 }
1879 if (is_loop) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08001880 j = nr - (i + off);
1881 if (j > 0) {
1882 save_iterations(iter + i + off,
1883 l + i, off);
1884
1885 memmove(iter + i, iter + i + off,
1886 j * sizeof(*iter));
1887
1888 memmove(l + i, l + i + off,
1889 j * sizeof(*l));
1890 }
1891
Andi Kleen8b7bad52014-11-12 18:05:20 -08001892 nr -= off;
1893 }
1894 }
1895 }
1896 return nr;
1897}
1898
Kan Liang384b6052015-01-05 13:23:05 -05001899/*
1900 * Recolve LBR callstack chain sample
1901 * Return:
1902 * 1 on success get LBR callchain information
1903 * 0 no available LBR callchain information, should try fp
1904 * negative error code on other errors.
1905 */
1906static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001907 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001908 struct perf_sample *sample,
1909 struct symbol **parent,
1910 struct addr_location *root_al,
1911 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001912{
Kan Liang384b6052015-01-05 13:23:05 -05001913 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001914 int chain_nr = min(max_stack, (int)chain->nr), i;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001915 u8 cpumode = PERF_RECORD_MISC_USER;
Jin Yaob851dd42017-07-18 20:13:15 +08001916 u64 ip, branch_from = 0;
Kan Liang384b6052015-01-05 13:23:05 -05001917
1918 for (i = 0; i < chain_nr; i++) {
1919 if (chain->ips[i] == PERF_CONTEXT_USER)
1920 break;
1921 }
1922
1923 /* LBR only affects the user callchain */
1924 if (i != chain_nr) {
1925 struct branch_stack *lbr_stack = sample->branch_stack;
Jin Yao410024d2016-10-31 09:19:49 +08001926 int lbr_nr = lbr_stack->nr, j, k;
1927 bool branch;
1928 struct branch_flags *flags;
Kan Liang384b6052015-01-05 13:23:05 -05001929 /*
1930 * LBR callstack can only get user call chain.
1931 * The mix_chain_nr is kernel call chain
1932 * number plus LBR user call chain number.
1933 * i is kernel call chain number,
1934 * 1 is PERF_CONTEXT_USER,
1935 * lbr_nr + 1 is the user call chain number.
1936 * For details, please refer to the comments
1937 * in callchain__printf
1938 */
1939 int mix_chain_nr = i + 1 + lbr_nr + 1;
1940
Kan Liang384b6052015-01-05 13:23:05 -05001941 for (j = 0; j < mix_chain_nr; j++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001942 int err;
Jin Yao410024d2016-10-31 09:19:49 +08001943 branch = false;
1944 flags = NULL;
1945
Kan Liang384b6052015-01-05 13:23:05 -05001946 if (callchain_param.order == ORDER_CALLEE) {
1947 if (j < i + 1)
1948 ip = chain->ips[j];
Jin Yao410024d2016-10-31 09:19:49 +08001949 else if (j > i + 1) {
1950 k = j - i - 2;
1951 ip = lbr_stack->entries[k].from;
1952 branch = true;
1953 flags = &lbr_stack->entries[k].flags;
1954 } else {
Kan Liang384b6052015-01-05 13:23:05 -05001955 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08001956 branch = true;
1957 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08001958 branch_from =
1959 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08001960 }
Kan Liang384b6052015-01-05 13:23:05 -05001961 } else {
Jin Yao410024d2016-10-31 09:19:49 +08001962 if (j < lbr_nr) {
1963 k = lbr_nr - j - 1;
1964 ip = lbr_stack->entries[k].from;
1965 branch = true;
1966 flags = &lbr_stack->entries[k].flags;
1967 }
Kan Liang384b6052015-01-05 13:23:05 -05001968 else if (j > lbr_nr)
1969 ip = chain->ips[i + 1 - (j - lbr_nr)];
Jin Yao410024d2016-10-31 09:19:49 +08001970 else {
Kan Liang384b6052015-01-05 13:23:05 -05001971 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08001972 branch = true;
1973 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08001974 branch_from =
1975 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08001976 }
Kan Liang384b6052015-01-05 13:23:05 -05001977 }
1978
Jin Yao410024d2016-10-31 09:19:49 +08001979 err = add_callchain_ip(thread, cursor, parent,
1980 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08001981 branch, flags, NULL,
Jin Yaob851dd42017-07-18 20:13:15 +08001982 branch_from);
Kan Liang384b6052015-01-05 13:23:05 -05001983 if (err)
1984 return (err < 0) ? err : 0;
1985 }
1986 return 1;
1987 }
1988
1989 return 0;
1990}
1991
1992static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001993 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001994 struct perf_evsel *evsel,
1995 struct perf_sample *sample,
1996 struct symbol **parent,
1997 struct addr_location *root_al,
1998 int max_stack)
1999{
2000 struct branch_stack *branch = sample->branch_stack;
2001 struct ip_callchain *chain = sample->callchain;
Jin Yaob49a8212017-05-08 18:43:02 +08002002 int chain_nr = 0;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002003 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002004 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08002005 int skip_idx = -1;
2006 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002007
Jin Yaob49a8212017-05-08 18:43:02 +08002008 if (chain)
2009 chain_nr = chain->nr;
2010
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03002011 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002012 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05002013 root_al, max_stack);
2014 if (err)
2015 return (err < 0) ? err : 0;
2016 }
2017
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002018 /*
2019 * Based on DWARF debug information, some architectures skip
2020 * a callchain entry saved by the kernel.
2021 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002022 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002023
Andi Kleen8b7bad52014-11-12 18:05:20 -08002024 /*
2025 * Add branches to call stack for easier browsing. This gives
2026 * more context for a sample than just the callers.
2027 *
2028 * This uses individual histograms of paths compared to the
2029 * aggregated histograms the normal LBR mode uses.
2030 *
2031 * Limitations for now:
2032 * - No extra filters
2033 * - No annotations (should annotate somehow)
2034 */
2035
2036 if (branch && callchain_param.branch_callstack) {
2037 int nr = min(max_stack, (int)branch->nr);
2038 struct branch_entry be[nr];
Jin Yaoc4ee0622017-08-07 21:05:15 +08002039 struct iterations iter[nr];
Andi Kleen8b7bad52014-11-12 18:05:20 -08002040
2041 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2042 pr_warning("corrupted branch chain. skipping...\n");
2043 goto check_calls;
2044 }
2045
2046 for (i = 0; i < nr; i++) {
2047 if (callchain_param.order == ORDER_CALLEE) {
2048 be[i] = branch->entries[i];
Jin Yaob49a8212017-05-08 18:43:02 +08002049
2050 if (chain == NULL)
2051 continue;
2052
Andi Kleen8b7bad52014-11-12 18:05:20 -08002053 /*
2054 * Check for overlap into the callchain.
2055 * The return address is one off compared to
2056 * the branch entry. To adjust for this
2057 * assume the calling instruction is not longer
2058 * than 8 bytes.
2059 */
2060 if (i == skip_idx ||
2061 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2062 first_call++;
2063 else if (be[i].from < chain->ips[first_call] &&
2064 be[i].from >= chain->ips[first_call] - 8)
2065 first_call++;
2066 } else
2067 be[i] = branch->entries[branch->nr - i - 1];
2068 }
2069
Jin Yaoc4ee0622017-08-07 21:05:15 +08002070 memset(iter, 0, sizeof(struct iterations) * nr);
2071 nr = remove_loops(be, nr, iter);
Jin Yao410024d2016-10-31 09:19:49 +08002072
Andi Kleen8b7bad52014-11-12 18:05:20 -08002073 for (i = 0; i < nr; i++) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002074 err = add_callchain_ip(thread, cursor, parent,
2075 root_al,
2076 NULL, be[i].to,
2077 true, &be[i].flags,
2078 NULL, be[i].from);
Jin Yao410024d2016-10-31 09:19:49 +08002079
Andi Kleen8b7bad52014-11-12 18:05:20 -08002080 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002081 err = add_callchain_ip(thread, cursor, parent, root_al,
Jin Yao410024d2016-10-31 09:19:49 +08002082 NULL, be[i].from,
2083 true, &be[i].flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002084 &iter[i], 0);
Andi Kleen8b7bad52014-11-12 18:05:20 -08002085 if (err == -EINVAL)
2086 break;
2087 if (err)
2088 return err;
2089 }
Jin Yaob49a8212017-05-08 18:43:02 +08002090
2091 if (chain_nr == 0)
2092 return 0;
2093
Andi Kleen8b7bad52014-11-12 18:05:20 -08002094 chain_nr -= nr;
2095 }
2096
2097check_calls:
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002098 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002099 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002100 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002101
2102 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002103 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002104 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002105 j = chain->nr - i - 1;
2106
2107#ifdef HAVE_SKIP_CALLCHAIN_IDX
2108 if (j == skip_idx)
2109 continue;
2110#endif
2111 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002112
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002113 if (ip < PERF_CONTEXT_MAX)
2114 ++nr_entries;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002115
Jin Yao410024d2016-10-31 09:19:49 +08002116 err = add_callchain_ip(thread, cursor, parent,
2117 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002118 false, NULL, NULL, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002119
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002120 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05002121 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002122 }
2123
2124 return 0;
2125}
2126
Milian Wolff11ea2512017-10-09 22:32:59 +02002127static int append_inlines(struct callchain_cursor *cursor,
2128 struct map *map, struct symbol *sym, u64 ip)
2129{
2130 struct inline_node *inline_node;
2131 struct inline_list *ilist;
2132 u64 addr;
Milian Wolffb38775c2017-10-19 13:38:33 +02002133 int ret = 1;
Milian Wolff11ea2512017-10-09 22:32:59 +02002134
2135 if (!symbol_conf.inline_name || !map || !sym)
Milian Wolffb38775c2017-10-19 13:38:33 +02002136 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002137
2138 addr = map__rip_2objdump(map, ip);
2139
2140 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2141 if (!inline_node) {
2142 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2143 if (!inline_node)
Milian Wolffb38775c2017-10-19 13:38:33 +02002144 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002145 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2146 }
2147
2148 list_for_each_entry(ilist, &inline_node->val, list) {
Milian Wolffb38775c2017-10-19 13:38:33 +02002149 ret = callchain_cursor_append(cursor, ip, map,
2150 ilist->symbol, false,
2151 NULL, 0, 0, 0, ilist->srcline);
Milian Wolff11ea2512017-10-09 22:32:59 +02002152
2153 if (ret != 0)
2154 return ret;
2155 }
2156
Milian Wolffb38775c2017-10-19 13:38:33 +02002157 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002158}
2159
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002160static int unwind_entry(struct unwind_entry *entry, void *arg)
2161{
2162 struct callchain_cursor *cursor = arg;
Milian Wolff40a342c2017-10-09 22:32:56 +02002163 const char *srcline = NULL;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002164
2165 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2166 return 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002167
Milian Wolff11ea2512017-10-09 22:32:59 +02002168 if (append_inlines(cursor, entry->map, entry->sym, entry->ip) == 0)
2169 return 0;
2170
Milian Wolff40a342c2017-10-09 22:32:56 +02002171 srcline = callchain_srcline(entry->map, entry->sym, entry->ip);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002172 return callchain_cursor_append(cursor, entry->ip,
Jin Yao410024d2016-10-31 09:19:49 +08002173 entry->map, entry->sym,
Milian Wolff40a342c2017-10-09 22:32:56 +02002174 false, NULL, 0, 0, 0, srcline);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002175}
2176
Chris Phlipot9919a652016-04-28 01:19:06 -07002177static int thread__resolve_callchain_unwind(struct thread *thread,
2178 struct callchain_cursor *cursor,
2179 struct perf_evsel *evsel,
2180 struct perf_sample *sample,
2181 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002182{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002183 /* Can we do dwarf post unwind? */
2184 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2185 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
2186 return 0;
2187
2188 /* Bail out if nothing was captured. */
2189 if ((!sample->user_regs.regs) ||
2190 (!sample->user_stack.size))
2191 return 0;
2192
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002193 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01002194 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07002195}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002196
Chris Phlipot9919a652016-04-28 01:19:06 -07002197int thread__resolve_callchain(struct thread *thread,
2198 struct callchain_cursor *cursor,
2199 struct perf_evsel *evsel,
2200 struct perf_sample *sample,
2201 struct symbol **parent,
2202 struct addr_location *root_al,
2203 int max_stack)
2204{
2205 int ret = 0;
2206
Jiri Olsa914eb9c2017-08-06 16:39:39 +02002207 callchain_cursor_reset(cursor);
Chris Phlipot9919a652016-04-28 01:19:06 -07002208
2209 if (callchain_param.order == ORDER_CALLEE) {
2210 ret = thread__resolve_callchain_sample(thread, cursor,
2211 evsel, sample,
2212 parent, root_al,
2213 max_stack);
2214 if (ret)
2215 return ret;
2216 ret = thread__resolve_callchain_unwind(thread, cursor,
2217 evsel, sample,
2218 max_stack);
2219 } else {
2220 ret = thread__resolve_callchain_unwind(thread, cursor,
2221 evsel, sample,
2222 max_stack);
2223 if (ret)
2224 return ret;
2225 ret = thread__resolve_callchain_sample(thread, cursor,
2226 evsel, sample,
2227 parent, root_al,
2228 max_stack);
2229 }
2230
2231 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002232}
David Ahern35feee12013-09-28 13:12:58 -06002233
2234int machine__for_each_thread(struct machine *machine,
2235 int (*fn)(struct thread *thread, void *p),
2236 void *priv)
2237{
Kan Liang91e467b2017-09-10 19:23:14 -07002238 struct threads *threads;
David Ahern35feee12013-09-28 13:12:58 -06002239 struct rb_node *nd;
2240 struct thread *thread;
2241 int rc = 0;
Kan Liang91e467b2017-09-10 19:23:14 -07002242 int i;
David Ahern35feee12013-09-28 13:12:58 -06002243
Kan Liang91e467b2017-09-10 19:23:14 -07002244 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
2245 threads = &machine->threads[i];
2246 for (nd = rb_first(&threads->entries); nd; nd = rb_next(nd)) {
2247 thread = rb_entry(nd, struct thread, rb_node);
2248 rc = fn(thread, priv);
2249 if (rc != 0)
2250 return rc;
2251 }
David Ahern35feee12013-09-28 13:12:58 -06002252
Kan Liang91e467b2017-09-10 19:23:14 -07002253 list_for_each_entry(thread, &threads->dead, node) {
2254 rc = fn(thread, priv);
2255 if (rc != 0)
2256 return rc;
2257 }
David Ahern35feee12013-09-28 13:12:58 -06002258 }
2259 return rc;
2260}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002261
Adrian Huntera5499b32015-05-29 16:33:30 +03002262int machines__for_each_thread(struct machines *machines,
2263 int (*fn)(struct thread *thread, void *p),
2264 void *priv)
2265{
2266 struct rb_node *nd;
2267 int rc = 0;
2268
2269 rc = machine__for_each_thread(&machines->host, fn, priv);
2270 if (rc != 0)
2271 return rc;
2272
2273 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
2274 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2275
2276 rc = machine__for_each_thread(machine, fn, priv);
2277 if (rc != 0)
2278 return rc;
2279 }
2280 return rc;
2281}
2282
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03002283int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002284 struct target *target, struct thread_map *threads,
Kan Liang9d9cad72015-06-17 09:51:11 -04002285 perf_event__handler_t process, bool data_mmap,
Kan Liang340b47f2017-09-29 07:47:54 -07002286 unsigned int proc_map_timeout,
2287 unsigned int nr_threads_synthesize)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002288{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002289 if (target__has_task(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002290 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002291 else if (target__has_cpu(target))
Kan Liang340b47f2017-09-29 07:47:54 -07002292 return perf_event__synthesize_threads(tool, process,
2293 machine, data_mmap,
2294 proc_map_timeout,
2295 nr_threads_synthesize);
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002296 /* command specified */
2297 return 0;
2298}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002299
2300pid_t machine__get_current_tid(struct machine *machine, int cpu)
2301{
2302 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2303 return -1;
2304
2305 return machine->current_tid[cpu];
2306}
2307
2308int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2309 pid_t tid)
2310{
2311 struct thread *thread;
2312
2313 if (cpu < 0)
2314 return -EINVAL;
2315
2316 if (!machine->current_tid) {
2317 int i;
2318
2319 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2320 if (!machine->current_tid)
2321 return -ENOMEM;
2322 for (i = 0; i < MAX_NR_CPUS; i++)
2323 machine->current_tid[i] = -1;
2324 }
2325
2326 if (cpu >= MAX_NR_CPUS) {
2327 pr_err("Requested CPU %d too large. ", cpu);
2328 pr_err("Consider raising MAX_NR_CPUS\n");
2329 return -EINVAL;
2330 }
2331
2332 machine->current_tid[cpu] = tid;
2333
2334 thread = machine__findnew_thread(machine, pid, tid);
2335 if (!thread)
2336 return -ENOMEM;
2337
2338 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002339 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002340
2341 return 0;
2342}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002343
2344int machine__get_kernel_start(struct machine *machine)
2345{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002346 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002347 int err = 0;
2348
2349 /*
2350 * The only addresses above 2^63 are kernel addresses of a 64-bit
2351 * kernel. Note that addresses are unsigned so that on a 32-bit system
2352 * all addresses including kernel addresses are less than 2^32. In
2353 * that case (32-bit system), if the kernel mapping is unknown, all
2354 * addresses will be assumed to be in user space - see
2355 * machine__kernel_ip().
2356 */
2357 machine->kernel_start = 1ULL << 63;
2358 if (map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002359 err = map__load(map);
Arnaldo Carvalho de Melo4b1303d2017-07-11 16:21:40 -03002360 if (!err)
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002361 machine->kernel_start = map->start;
2362 }
2363 return err;
2364}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002365
2366struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2367{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03002368 return dsos__findnew(&machine->dsos, filename);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002369}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002370
2371char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2372{
2373 struct machine *machine = vmachine;
2374 struct map *map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002375 struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002376
2377 if (sym == NULL)
2378 return NULL;
2379
2380 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2381 *addrp = map->unmap_ip(map, sym->start);
2382 return sym->name;
2383}