blob: f7c1a7e6c4bac933a640a2a657d08e07cd4790ba [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 Melodaecf9e2019-01-28 00:03:34 +010013#include "symbol.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030014#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030015#include "strlist.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030016#include "target.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030017#include "thread.h"
Adrian Hunterd027b642014-07-23 14:23:00 +030018#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030019#include <stdbool.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030020#include <sys/types.h>
21#include <sys/stat.h>
22#include <unistd.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030023#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080024#include "linux/hash.h"
Hari Bathinif3b36142017-03-08 02:11:43 +053025#include "asm/bug.h"
Song Liu45178a92019-01-17 08:15:18 -080026#include "bpf-event.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030027
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030028#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030029#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -030030#include <linux/mman.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030031#include <linux/zalloc.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030032
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030033static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
34
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030035static void dsos__init(struct dsos *dsos)
36{
37 INIT_LIST_HEAD(&dsos->head);
38 dsos->root = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030039 init_rwsem(&dsos->lock);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030040}
41
Kan Liang91e467b2017-09-10 19:23:14 -070042static void machine__threads_init(struct machine *machine)
43{
44 int i;
45
46 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
47 struct threads *threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -080048 threads->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030049 init_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -070050 threads->nr = 0;
51 INIT_LIST_HEAD(&threads->dead);
52 threads->last_match = NULL;
53 }
54}
55
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010056static int machine__set_mmap_name(struct machine *machine)
57{
Jiri Olsac1925242018-03-12 16:24:06 +010058 if (machine__is_host(machine))
59 machine->mmap_name = strdup("[kernel.kallsyms]");
60 else if (machine__is_default_guest(machine))
61 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
62 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
63 machine->pid) < 0)
64 machine->mmap_name = NULL;
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010065
66 return machine->mmap_name ? 0 : -ENOMEM;
67}
68
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030069int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
70{
Jiri Olsa81f981d2018-02-15 13:26:29 +010071 int err = -ENOMEM;
72
Wang Nan93b0ba32015-12-08 02:25:44 +000073 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030074 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030075 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030076 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030077
Kan Liang91e467b2017-09-10 19:23:14 -070078 machine__threads_init(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030079
Adrian Hunterd027b642014-07-23 14:23:00 +030080 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030081 machine->env = NULL;
Adrian Hunterd027b642014-07-23 14:23:00 +030082
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030083 machine->pid = pid;
84
Jiri Olsa14bd6d22014-01-07 13:47:19 +010085 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -030086 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +030087 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030088 machine->kernel_start = 0;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -030089 machine->vmlinux_map = NULL;
Masami Hiramatsucc1121a2015-12-09 11:11:33 +090090
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030091 machine->root_dir = strdup(root_dir);
92 if (machine->root_dir == NULL)
93 return -ENOMEM;
94
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010095 if (machine__set_mmap_name(machine))
96 goto out;
97
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030098 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030099 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +0300100 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300101 char comm[64];
102
103 if (thread == NULL)
Jiri Olsa81f981d2018-02-15 13:26:29 +0100104 goto out;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300105
106 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200107 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300108 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300109 }
110
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300111 machine->current_tid = NULL;
Jiri Olsa81f981d2018-02-15 13:26:29 +0100112 err = 0;
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300113
Jiri Olsa81f981d2018-02-15 13:26:29 +0100114out:
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100115 if (err) {
Jiri Olsa81f981d2018-02-15 13:26:29 +0100116 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100117 zfree(&machine->mmap_name);
118 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300119 return 0;
120}
121
David Ahern8fb598e2013-09-28 13:13:00 -0600122struct machine *machine__new_host(void)
123{
124 struct machine *machine = malloc(sizeof(*machine));
125
126 if (machine != NULL) {
127 machine__init(machine, "", HOST_KERNEL_ID);
128
129 if (machine__create_kernel_maps(machine) < 0)
130 goto out_delete;
131 }
132
133 return machine;
134out_delete:
135 free(machine);
136 return NULL;
137}
138
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300139struct machine *machine__new_kallsyms(void)
140{
141 struct machine *machine = machine__new_host();
142 /*
143 * FIXME:
Ingo Molnaradba1632018-12-03 11:22:00 +0100144 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300145 * ask for not using the kcore parsing code, once this one is fixed
146 * to create a map per module.
147 */
Arnaldo Carvalho de Melo329f0ad2018-04-25 11:40:32 -0300148 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300149 machine__delete(machine);
150 machine = NULL;
151 }
152
153 return machine;
154}
155
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300156static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300157{
158 struct dso *pos, *n;
159
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300160 down_write(&dsos->lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300161
Waiman Long8fa7d872014-09-29 16:07:28 -0400162 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -0400163 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +0200164 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300165 list_del_init(&pos->node);
166 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300167 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300168
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300169 up_write(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300170}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300171
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300172static void dsos__exit(struct dsos *dsos)
173{
174 dsos__purge(dsos);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300175 exit_rwsem(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300176}
177
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300178void machine__delete_threads(struct machine *machine)
179{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300180 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700181 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300182
Kan Liang91e467b2017-09-10 19:23:14 -0700183 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
184 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300185 down_write(&threads->lock);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800186 nd = rb_first_cached(&threads->entries);
Kan Liang91e467b2017-09-10 19:23:14 -0700187 while (nd) {
188 struct thread *t = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300189
Kan Liang91e467b2017-09-10 19:23:14 -0700190 nd = rb_next(nd);
191 __machine__remove_thread(machine, t, false);
192 }
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300193 up_write(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300194 }
195}
196
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300197void machine__exit(struct machine *machine)
198{
Kan Liang91e467b2017-09-10 19:23:14 -0700199 int i;
200
Arnaldo Carvalho de Melo19993b82017-11-13 16:06:29 -0300201 if (machine == NULL)
202 return;
203
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900204 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300205 map_groups__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300206 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300207 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300208 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100209 zfree(&machine->mmap_name);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300210 zfree(&machine->current_tid);
Kan Liang91e467b2017-09-10 19:23:14 -0700211
212 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
213 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -0300214 struct thread *thread, *n;
215 /*
216 * Forget about the dead, at this point whatever threads were
217 * left in the dead lists better have a reference count taken
218 * by who is using them, and then, when they drop those references
219 * and it finally hits zero, thread__put() will check and see that
220 * its not in the dead threads list and will not try to remove it
221 * from there, just calling thread__delete() straight away.
222 */
223 list_for_each_entry_safe(thread, n, &threads->dead, node)
224 list_del_init(&thread->node);
225
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300226 exit_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -0700227 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300228}
229
230void machine__delete(struct machine *machine)
231{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300232 if (machine) {
233 machine__exit(machine);
234 free(machine);
235 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300236}
237
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300238void machines__init(struct machines *machines)
239{
240 machine__init(&machines->host, "", HOST_KERNEL_ID);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800241 machines->guests = RB_ROOT_CACHED;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300242}
243
244void machines__exit(struct machines *machines)
245{
246 machine__exit(&machines->host);
247 /* XXX exit guest */
248}
249
250struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300251 const char *root_dir)
252{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800253 struct rb_node **p = &machines->guests.rb_root.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300254 struct rb_node *parent = NULL;
255 struct machine *pos, *machine = malloc(sizeof(*machine));
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800256 bool leftmost = true;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300257
258 if (machine == NULL)
259 return NULL;
260
261 if (machine__init(machine, root_dir, pid) != 0) {
262 free(machine);
263 return NULL;
264 }
265
266 while (*p != NULL) {
267 parent = *p;
268 pos = rb_entry(parent, struct machine, rb_node);
269 if (pid < pos->pid)
270 p = &(*p)->rb_left;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800271 else {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300272 p = &(*p)->rb_right;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800273 leftmost = false;
274 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300275 }
276
277 rb_link_node(&machine->rb_node, parent, p);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800278 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300279
280 return machine;
281}
282
Adrian Huntercfe1c412014-07-31 09:00:45 +0300283void machines__set_comm_exec(struct machines *machines, bool comm_exec)
284{
285 struct rb_node *nd;
286
287 machines->host.comm_exec = comm_exec;
288
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800289 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntercfe1c412014-07-31 09:00:45 +0300290 struct machine *machine = rb_entry(nd, struct machine, rb_node);
291
292 machine->comm_exec = comm_exec;
293 }
294}
295
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300296struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300297{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800298 struct rb_node **p = &machines->guests.rb_root.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300299 struct rb_node *parent = NULL;
300 struct machine *machine;
301 struct machine *default_machine = NULL;
302
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300303 if (pid == HOST_KERNEL_ID)
304 return &machines->host;
305
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300306 while (*p != NULL) {
307 parent = *p;
308 machine = rb_entry(parent, struct machine, rb_node);
309 if (pid < machine->pid)
310 p = &(*p)->rb_left;
311 else if (pid > machine->pid)
312 p = &(*p)->rb_right;
313 else
314 return machine;
315 if (!machine->pid)
316 default_machine = machine;
317 }
318
319 return default_machine;
320}
321
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300322struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300323{
324 char path[PATH_MAX];
325 const char *root_dir = "";
326 struct machine *machine = machines__find(machines, pid);
327
328 if (machine && (machine->pid == pid))
329 goto out;
330
331 if ((pid != HOST_KERNEL_ID) &&
332 (pid != DEFAULT_GUEST_KERNEL_ID) &&
333 (symbol_conf.guestmount)) {
334 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
335 if (access(path, R_OK)) {
336 static struct strlist *seen;
337
338 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300339 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300340
341 if (!strlist__has_entry(seen, path)) {
342 pr_err("Can't access file %s\n", path);
343 strlist__add(seen, path);
344 }
345 machine = NULL;
346 goto out;
347 }
348 root_dir = path;
349 }
350
351 machine = machines__add(machines, pid, root_dir);
352out:
353 return machine;
354}
355
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300356void machines__process_guests(struct machines *machines,
357 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300358{
359 struct rb_node *nd;
360
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800361 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300362 struct machine *pos = rb_entry(nd, struct machine, rb_node);
363 process(pos, data);
364 }
365}
366
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300367void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300368{
369 struct rb_node *node;
370 struct machine *machine;
371
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300372 machines->host.id_hdr_size = id_hdr_size;
373
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800374 for (node = rb_first_cached(&machines->guests); node;
375 node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300376 machine = rb_entry(node, struct machine, rb_node);
377 machine->id_hdr_size = id_hdr_size;
378 }
379
380 return;
381}
382
Adrian Hunter29ce3612014-07-16 11:07:13 +0300383static void machine__update_thread_pid(struct machine *machine,
384 struct thread *th, pid_t pid)
385{
386 struct thread *leader;
387
388 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
389 return;
390
391 th->pid_ = pid;
392
393 if (th->pid_ == th->tid)
394 return;
395
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300396 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300397 if (!leader)
398 goto out_err;
399
400 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300401 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300402
403 if (!leader->mg)
404 goto out_err;
405
406 if (th->mg == leader->mg)
407 return;
408
409 if (th->mg) {
410 /*
411 * Maps are created from MMAP events which provide the pid and
412 * tid. Consequently there never should be any maps on a thread
413 * with an unknown pid. Just print an error if there are.
414 */
415 if (!map_groups__empty(th->mg))
416 pr_err("Discarding thread maps for %d:%d\n",
417 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300418 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300419 }
420
421 th->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300422out_put:
423 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300424 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300425out_err:
426 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300427 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300428}
429
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300430/*
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200431 * Front-end cache - TID lookups come in blocks,
432 * so most of the time we dont have to look up
433 * the full rbtree:
434 */
435static struct thread*
Jiri Olsab57334b2018-07-19 16:33:44 +0200436__threads__get_last_match(struct threads *threads, struct machine *machine,
437 int pid, int tid)
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200438{
439 struct thread *th;
440
441 th = threads->last_match;
442 if (th != NULL) {
443 if (th->tid == tid) {
444 machine__update_thread_pid(machine, th, pid);
445 return thread__get(th);
446 }
447
448 threads->last_match = NULL;
449 }
450
451 return NULL;
452}
453
Jiri Olsab57334b2018-07-19 16:33:44 +0200454static struct thread*
455threads__get_last_match(struct threads *threads, struct machine *machine,
456 int pid, int tid)
457{
458 struct thread *th = NULL;
459
460 if (perf_singlethreaded)
461 th = __threads__get_last_match(threads, machine, pid, tid);
462
463 return th;
464}
465
466static void
467__threads__set_last_match(struct threads *threads, struct thread *th)
468{
469 threads->last_match = th;
470}
471
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200472static void
473threads__set_last_match(struct threads *threads, struct thread *th)
474{
Jiri Olsab57334b2018-07-19 16:33:44 +0200475 if (perf_singlethreaded)
476 __threads__set_last_match(threads, th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200477}
478
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200479/*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800480 * Caller must eventually drop thread->refcnt returned with a successful
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300481 * lookup/new thread inserted.
482 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300483static struct thread *____machine__findnew_thread(struct machine *machine,
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300484 struct threads *threads,
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300485 pid_t pid, pid_t tid,
486 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300487{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800488 struct rb_node **p = &threads->entries.rb_root.rb_node;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300489 struct rb_node *parent = NULL;
490 struct thread *th;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800491 bool leftmost = true;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300492
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200493 th = threads__get_last_match(threads, machine, pid, tid);
494 if (th)
495 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300496
497 while (*p != NULL) {
498 parent = *p;
499 th = rb_entry(parent, struct thread, rb_node);
500
Adrian Hunter38051232013-07-04 16:20:31 +0300501 if (th->tid == tid) {
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200502 threads__set_last_match(threads, th);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300503 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300504 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300505 }
506
Adrian Hunter38051232013-07-04 16:20:31 +0300507 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300508 p = &(*p)->rb_left;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800509 else {
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300510 p = &(*p)->rb_right;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800511 leftmost = false;
512 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300513 }
514
515 if (!create)
516 return NULL;
517
Adrian Hunter99d725f2013-08-26 16:00:19 +0300518 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300519 if (th != NULL) {
520 rb_link_node(&th->rb_node, parent, p);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800521 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
Jiri Olsacddcef62014-04-09 20:54:29 +0200522
523 /*
524 * We have to initialize map_groups separately
525 * after rb tree is updated.
526 *
527 * The reason is that we call machine__findnew_thread
528 * within thread__init_map_groups to find the thread
529 * leader and that would screwed the rb tree.
530 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300531 if (thread__init_map_groups(th, machine)) {
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800532 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300533 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300534 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200535 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300536 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300537 /*
538 * It is now in the rbtree, get a ref
539 */
540 thread__get(th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200541 threads__set_last_match(threads, th);
Kan Liang91e467b2017-09-10 19:23:14 -0700542 ++threads->nr;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300543 }
544
545 return th;
546}
547
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300548struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
549{
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300550 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300551}
552
Adrian Hunter314add62013-08-27 11:23:03 +0300553struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
554 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300555{
Kan Liang91e467b2017-09-10 19:23:14 -0700556 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300557 struct thread *th;
558
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300559 down_write(&threads->lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300560 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300561 up_write(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300562 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300563}
564
Jiri Olsad75e6092014-03-14 15:00:03 +0100565struct thread *machine__find_thread(struct machine *machine, pid_t pid,
566 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300567{
Kan Liang91e467b2017-09-10 19:23:14 -0700568 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300569 struct thread *th;
Kan Liang91e467b2017-09-10 19:23:14 -0700570
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300571 down_read(&threads->lock);
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300572 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300573 up_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300574 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300575}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300576
Adrian Huntercfe1c412014-07-31 09:00:45 +0300577struct comm *machine__thread_exec_comm(struct machine *machine,
578 struct thread *thread)
579{
580 if (machine->comm_exec)
581 return thread__exec_comm(thread);
582 else
583 return thread__comm(thread);
584}
585
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200586int machine__process_comm_event(struct machine *machine, union perf_event *event,
587 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300588{
Adrian Hunter314add62013-08-27 11:23:03 +0300589 struct thread *thread = machine__findnew_thread(machine,
590 event->comm.pid,
591 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300592 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300593 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300594
Adrian Huntercfe1c412014-07-31 09:00:45 +0300595 if (exec)
596 machine->comm_exec = true;
597
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300598 if (dump_trace)
599 perf_event__fprintf_comm(event, stdout);
600
Adrian Hunter65de51f2014-07-31 09:00:44 +0300601 if (thread == NULL ||
602 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300603 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300604 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300605 }
606
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300607 thread__put(thread);
608
609 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300610}
611
Hari Bathinif3b36142017-03-08 02:11:43 +0530612int machine__process_namespaces_event(struct machine *machine __maybe_unused,
613 union perf_event *event,
614 struct perf_sample *sample __maybe_unused)
615{
616 struct thread *thread = machine__findnew_thread(machine,
617 event->namespaces.pid,
618 event->namespaces.tid);
619 int err = 0;
620
621 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
622 "\nWARNING: kernel seems to support more namespaces than perf"
623 " tool.\nTry updating the perf tool..\n\n");
624
625 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
626 "\nWARNING: perf tool seems to support more namespaces than"
627 " the kernel.\nTry updating the kernel..\n\n");
628
629 if (dump_trace)
630 perf_event__fprintf_namespaces(event, stdout);
631
632 if (thread == NULL ||
633 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
634 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
635 err = -1;
636 }
637
638 thread__put(thread);
639
640 return err;
641}
642
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300643int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200644 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300645{
646 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
647 event->lost.id, event->lost.lost);
648 return 0;
649}
650
Kan Liangc4937a92015-05-10 15:13:15 -0400651int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
652 union perf_event *event, struct perf_sample *sample)
653{
654 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
655 sample->id, event->lost_samples.lost);
656 return 0;
657}
658
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300659static struct dso *machine__findnew_module_dso(struct machine *machine,
660 struct kmod_path *m,
661 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100662{
663 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100664
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300665 down_write(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300666
667 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100668 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300669 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100670 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300671 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100672
Namhyung Kim6b335e82017-05-31 21:01:04 +0900673 dso__set_module_info(dso, m, machine);
Jiri Olsaca333802015-02-17 17:29:57 +0100674 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100675 }
676
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300677 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300678out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300679 up_write(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100680 return dso;
681}
682
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300683int machine__process_aux_event(struct machine *machine __maybe_unused,
684 union perf_event *event)
685{
686 if (dump_trace)
687 perf_event__fprintf_aux(event, stdout);
688 return 0;
689}
690
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300691int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
692 union perf_event *event)
693{
694 if (dump_trace)
695 perf_event__fprintf_itrace_start(event, stdout);
696 return 0;
697}
698
Adrian Hunter02860392015-07-21 12:44:03 +0300699int machine__process_switch_event(struct machine *machine __maybe_unused,
700 union perf_event *event)
701{
702 if (dump_trace)
703 perf_event__fprintf_switch(event, stdout);
704 return 0;
705}
706
Song Liu9aa0bfa2019-01-17 08:15:17 -0800707static int machine__process_ksymbol_register(struct machine *machine,
708 union perf_event *event,
709 struct perf_sample *sample __maybe_unused)
710{
711 struct symbol *sym;
712 struct map *map;
713
714 map = map_groups__find(&machine->kmaps, event->ksymbol_event.addr);
715 if (!map) {
716 map = dso__new_map(event->ksymbol_event.name);
717 if (!map)
718 return -ENOMEM;
719
720 map->start = event->ksymbol_event.addr;
Song Liu9aa0bfa2019-01-17 08:15:17 -0800721 map->end = map->start + event->ksymbol_event.len;
722 map_groups__insert(&machine->kmaps, map);
723 }
724
Jiri Olsa8529f2e2019-05-08 15:20:04 +0200725 sym = symbol__new(map->map_ip(map, map->start),
726 event->ksymbol_event.len,
Song Liu9aa0bfa2019-01-17 08:15:17 -0800727 0, 0, event->ksymbol_event.name);
728 if (!sym)
729 return -ENOMEM;
730 dso__insert_symbol(map->dso, sym);
731 return 0;
732}
733
734static int machine__process_ksymbol_unregister(struct machine *machine,
735 union perf_event *event,
736 struct perf_sample *sample __maybe_unused)
737{
738 struct map *map;
739
740 map = map_groups__find(&machine->kmaps, event->ksymbol_event.addr);
741 if (map)
742 map_groups__remove(&machine->kmaps, map);
743
744 return 0;
745}
746
747int machine__process_ksymbol(struct machine *machine __maybe_unused,
748 union perf_event *event,
749 struct perf_sample *sample)
750{
751 if (dump_trace)
752 perf_event__fprintf_ksymbol(event, stdout);
753
754 if (event->ksymbol_event.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
755 return machine__process_ksymbol_unregister(machine, event,
756 sample);
757 return machine__process_ksymbol_register(machine, event, sample);
758}
759
Wang Nanc03d5182015-11-26 03:59:57 +0000760static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
761{
762 const char *dup_filename;
763
764 if (!filename || !dso || !dso->long_name)
765 return;
766 if (dso->long_name[0] != '[')
767 return;
768 if (!strchr(filename, '/'))
769 return;
770
771 dup_filename = strdup(filename);
772 if (!dup_filename)
773 return;
774
Wang Nan5dcf16d2015-12-07 02:36:25 +0000775 dso__set_long_name(dso, dup_filename, true);
Wang Nanc03d5182015-11-26 03:59:57 +0000776}
777
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300778struct map *machine__findnew_module_map(struct machine *machine, u64 start,
779 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300780{
Jiri Olsaca333802015-02-17 17:29:57 +0100781 struct map *map = NULL;
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900782 struct dso *dso = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100783 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300784
Jiri Olsaca333802015-02-17 17:29:57 +0100785 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300786 return NULL;
787
Arnaldo Carvalho de Melo83cf7742018-04-24 12:16:09 -0300788 map = map_groups__find_by_name(&machine->kmaps, m.name);
Wang Nanc03d5182015-11-26 03:59:57 +0000789 if (map) {
790 /*
791 * If the map's dso is an offline module, give dso__load()
792 * a chance to find the file path of that module by fixing
793 * long_name.
794 */
795 dso__adjust_kmod_long_name(map->dso, filename);
Jiri Olsabc84f462015-02-17 17:31:18 +0100796 goto out;
Wang Nanc03d5182015-11-26 03:59:57 +0000797 }
Jiri Olsabc84f462015-02-17 17:31:18 +0100798
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300799 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100800 if (dso == NULL)
801 goto out;
802
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300803 map = map__new2(start, dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300804 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100805 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300806
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300807 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100808
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900809 /* Put the map here because map_groups__insert alread got it */
810 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100811out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900812 /* put the dso here, corresponding to machine__findnew_module_dso */
813 dso__put(dso);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300814 zfree(&m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300815 return map;
816}
817
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300818size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300819{
820 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300821 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300822
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800823 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300824 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300825 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300826 }
827
828 return ret;
829}
830
Waiman Long8fa7d872014-09-29 16:07:28 -0400831size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300832 bool (skip)(struct dso *dso, int parm), int parm)
833{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300834 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300835}
836
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300837size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300838 bool (skip)(struct dso *dso, int parm), int parm)
839{
840 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300841 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300842
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800843 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300844 struct machine *pos = rb_entry(nd, struct machine, rb_node);
845 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
846 }
847 return ret;
848}
849
850size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
851{
852 int i;
853 size_t printed = 0;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300854 struct dso *kdso = machine__kernel_map(machine)->dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300855
856 if (kdso->has_build_id) {
857 char filename[PATH_MAX];
Krister Johansend2396992017-07-05 18:48:13 -0700858 if (dso__build_id_filename(kdso, filename, sizeof(filename),
859 false))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300860 printed += fprintf(fp, "[0] %s\n", filename);
861 }
862
863 for (i = 0; i < vmlinux_path__nr_entries; ++i)
864 printed += fprintf(fp, "[%d] %s\n",
865 i + kdso->has_build_id, vmlinux_path[i]);
866
867 return printed;
868}
869
870size_t machine__fprintf(struct machine *machine, FILE *fp)
871{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300872 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700873 size_t ret;
874 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300875
Kan Liang91e467b2017-09-10 19:23:14 -0700876 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
877 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300878
879 down_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300880
Kan Liang91e467b2017-09-10 19:23:14 -0700881 ret = fprintf(fp, "Threads: %u\n", threads->nr);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300882
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800883 for (nd = rb_first_cached(&threads->entries); nd;
884 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -0700885 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300886
Kan Liang91e467b2017-09-10 19:23:14 -0700887 ret += thread__fprintf(pos, fp);
888 }
889
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300890 up_read(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300891 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300892 return ret;
893}
894
895static struct dso *machine__get_kernel(struct machine *machine)
896{
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100897 const char *vmlinux_name = machine->mmap_name;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300898 struct dso *kernel;
899
900 if (machine__is_host(machine)) {
Jiri Olsac1925242018-03-12 16:24:06 +0100901 if (symbol_conf.vmlinux_name)
902 vmlinux_name = symbol_conf.vmlinux_name;
903
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300904 kernel = machine__findnew_kernel(machine, vmlinux_name,
905 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300906 } else {
Jiri Olsac1925242018-03-12 16:24:06 +0100907 if (symbol_conf.default_guest_vmlinux_name)
908 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
909
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300910 kernel = machine__findnew_kernel(machine, vmlinux_name,
911 "[guest.kernel]",
912 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300913 }
914
915 if (kernel != NULL && (!kernel->has_build_id))
916 dso__read_running_kernel_build_id(kernel, machine);
917
918 return kernel;
919}
920
921struct process_args {
922 u64 start;
923};
924
Adrian Hunter1c5aae72018-05-22 13:54:36 +0300925void machine__get_kallsyms_filename(struct machine *machine, char *buf,
926 size_t bufsz)
Adrian Hunter15a0a872014-01-29 16:14:38 +0200927{
928 if (machine__is_default_guest(machine))
929 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
930 else
931 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
932}
933
Simon Quea93f0e52014-06-16 11:32:09 -0700934const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
935
936/* Figure out the start address of kernel map from /proc/kallsyms.
937 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
938 * symbol_name if it's not that important.
939 */
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300940static int machine__get_running_kernel_start(struct machine *machine,
Jiri Olsaed9adb22019-05-08 15:20:03 +0200941 const char **symbol_name,
942 u64 *start, u64 *end)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300943{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200944 char filename[PATH_MAX];
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300945 int i, err = -1;
Simon Quea93f0e52014-06-16 11:32:09 -0700946 const char *name;
947 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300948
Adrian Hunter15a0a872014-01-29 16:14:38 +0200949 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300950
951 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
952 return 0;
953
Simon Quea93f0e52014-06-16 11:32:09 -0700954 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300955 err = kallsyms__get_function_start(filename, name, &addr);
956 if (!err)
Simon Quea93f0e52014-06-16 11:32:09 -0700957 break;
958 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300959
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300960 if (err)
961 return -1;
962
Simon Quea93f0e52014-06-16 11:32:09 -0700963 if (symbol_name)
964 *symbol_name = name;
965
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300966 *start = addr;
Jiri Olsaed9adb22019-05-08 15:20:03 +0200967
968 err = kallsyms__get_function_start(filename, "_etext", &addr);
969 if (!err)
970 *end = addr;
971
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300972 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300973}
974
Adrian Hunter1c5aae72018-05-22 13:54:36 +0300975int machine__create_extra_kernel_map(struct machine *machine,
976 struct dso *kernel,
977 struct extra_kernel_map *xm)
Adrian Hunter4d99e412018-05-22 13:54:33 +0300978{
979 struct kmap *kmap;
980 struct map *map;
981
982 map = map__new2(xm->start, kernel);
983 if (!map)
984 return -1;
985
986 map->end = xm->end;
987 map->pgoff = xm->pgoff;
988
989 kmap = map__kmap(map);
990
991 kmap->kmaps = &machine->kmaps;
Adrian Hunter5759a682018-05-22 13:54:35 +0300992 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
Adrian Hunter4d99e412018-05-22 13:54:33 +0300993
994 map_groups__insert(&machine->kmaps, map);
995
Adrian Hunter5759a682018-05-22 13:54:35 +0300996 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
997 kmap->name, map->start, map->end);
Adrian Hunter4d99e412018-05-22 13:54:33 +0300998
999 map__put(map);
1000
1001 return 0;
1002}
1003
1004static u64 find_entry_trampoline(struct dso *dso)
1005{
1006 /* Duplicates are removed so lookup all aliases */
1007 const char *syms[] = {
1008 "_entry_trampoline",
1009 "__entry_trampoline_start",
1010 "entry_SYSCALL_64_trampoline",
1011 };
1012 struct symbol *sym = dso__first_symbol(dso);
1013 unsigned int i;
1014
1015 for (; sym; sym = dso__next_symbol(sym)) {
1016 if (sym->binding != STB_GLOBAL)
1017 continue;
1018 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1019 if (!strcmp(sym->name, syms[i]))
1020 return sym->start;
1021 }
1022 }
1023
1024 return 0;
1025}
1026
1027/*
1028 * These values can be used for kernels that do not have symbols for the entry
1029 * trampolines in kallsyms.
1030 */
1031#define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1032#define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1033#define X86_64_ENTRY_TRAMPOLINE 0x6000
1034
1035/* Map x86_64 PTI entry trampolines */
1036int machine__map_x86_64_entry_trampolines(struct machine *machine,
1037 struct dso *kernel)
1038{
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001039 struct map_groups *kmaps = &machine->kmaps;
1040 struct maps *maps = &kmaps->maps;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001041 int nr_cpus_avail, cpu;
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001042 bool found = false;
1043 struct map *map;
1044 u64 pgoff;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001045
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001046 /*
1047 * In the vmlinux case, pgoff is a virtual address which must now be
1048 * mapped to a vmlinux offset.
1049 */
1050 for (map = maps__first(maps); map; map = map__next(map)) {
1051 struct kmap *kmap = __map__kmap(map);
1052 struct map *dest_map;
1053
1054 if (!kmap || !is_entry_trampoline(kmap->name))
1055 continue;
1056
1057 dest_map = map_groups__find(kmaps, map->pgoff);
1058 if (dest_map != map)
1059 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1060 found = true;
1061 }
1062 if (found || machine->trampolines_mapped)
1063 return 0;
1064
1065 pgoff = find_entry_trampoline(kernel);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001066 if (!pgoff)
1067 return 0;
1068
1069 nr_cpus_avail = machine__nr_cpus_avail(machine);
1070
1071 /* Add a 1 page map for each CPU's entry trampoline */
1072 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1073 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1074 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1075 X86_64_ENTRY_TRAMPOLINE;
1076 struct extra_kernel_map xm = {
1077 .start = va,
1078 .end = va + page_size,
1079 .pgoff = pgoff,
1080 };
1081
Adrian Hunter5759a682018-05-22 13:54:35 +03001082 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1083
Adrian Hunter4d99e412018-05-22 13:54:33 +03001084 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1085 return -1;
1086 }
1087
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001088 machine->trampolines_mapped = nr_cpus_avail;
1089
1090 return 0;
1091}
1092
1093int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1094 struct dso *kernel __maybe_unused)
1095{
Adrian Hunter4d99e412018-05-22 13:54:33 +03001096 return 0;
1097}
1098
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001099static int
1100__machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001101{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001102 struct kmap *kmap;
1103 struct map *map;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001104
Masami Hiramatsucc1121a2015-12-09 11:11:33 +09001105 /* In case of renewal the kernel map, destroy previous one */
1106 machine__destroy_kernel_maps(machine);
1107
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001108 machine->vmlinux_map = map__new2(0, kernel);
1109 if (machine->vmlinux_map == NULL)
1110 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001111
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001112 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1113 map = machine__kernel_map(machine);
1114 kmap = map__kmap(map);
1115 if (!kmap)
1116 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001117
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001118 kmap->kmaps = &machine->kmaps;
1119 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001120
1121 return 0;
1122}
1123
1124void machine__destroy_kernel_maps(struct machine *machine)
1125{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001126 struct kmap *kmap;
1127 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001128
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001129 if (map == NULL)
1130 return;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001131
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001132 kmap = map__kmap(map);
1133 map_groups__remove(&machine->kmaps, map);
1134 if (kmap && kmap->ref_reloc_sym) {
1135 zfree((char **)&kmap->ref_reloc_sym->name);
1136 zfree(&kmap->ref_reloc_sym);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001137 }
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001138
1139 map__zput(machine->vmlinux_map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001140}
1141
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001142int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001143{
1144 int ret = 0;
1145 struct dirent **namelist = NULL;
1146 int i, items = 0;
1147 char path[PATH_MAX];
1148 pid_t pid;
1149 char *endp;
1150
1151 if (symbol_conf.default_guest_vmlinux_name ||
1152 symbol_conf.default_guest_modules ||
1153 symbol_conf.default_guest_kallsyms) {
1154 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1155 }
1156
1157 if (symbol_conf.guestmount) {
1158 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1159 if (items <= 0)
1160 return -ENOENT;
1161 for (i = 0; i < items; i++) {
1162 if (!isdigit(namelist[i]->d_name[0])) {
1163 /* Filter out . and .. */
1164 continue;
1165 }
1166 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1167 if ((*endp != '\0') ||
1168 (endp == namelist[i]->d_name) ||
1169 (errno == ERANGE)) {
1170 pr_debug("invalid directory (%s). Skipping.\n",
1171 namelist[i]->d_name);
1172 continue;
1173 }
1174 sprintf(path, "%s/%s/proc/kallsyms",
1175 symbol_conf.guestmount,
1176 namelist[i]->d_name);
1177 ret = access(path, R_OK);
1178 if (ret) {
1179 pr_debug("Can't access file %s\n", path);
1180 goto failure;
1181 }
1182 machines__create_kernel_maps(machines, pid);
1183 }
1184failure:
1185 free(namelist);
1186 }
1187
1188 return ret;
1189}
1190
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001191void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001192{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001193 struct rb_node *next = rb_first_cached(&machines->guests);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001194
1195 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001196
1197 while (next) {
1198 struct machine *pos = rb_entry(next, struct machine, rb_node);
1199
1200 next = rb_next(&pos->rb_node);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001201 rb_erase_cached(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001202 machine__delete(pos);
1203 }
1204}
1205
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -03001206int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001207{
1208 struct machine *machine = machines__findnew(machines, pid);
1209
1210 if (machine == NULL)
1211 return -1;
1212
1213 return machine__create_kernel_maps(machine);
1214}
1215
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001216int machine__load_kallsyms(struct machine *machine, const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001217{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001218 struct map *map = machine__kernel_map(machine);
Jiri Olsae8f38792018-02-15 13:26:33 +01001219 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001220
1221 if (ret > 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001222 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001223 /*
1224 * Since /proc/kallsyms will have multiple sessions for the
1225 * kernel, with modules between them, fixup the end of all
1226 * sections.
1227 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001228 map_groups__fixup_end(&machine->kmaps);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001229 }
1230
1231 return ret;
1232}
1233
Arnaldo Carvalho de Melo1d1a2652018-04-25 12:18:11 -03001234int machine__load_vmlinux_path(struct machine *machine)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001235{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001236 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001237 int ret = dso__load_vmlinux_path(map->dso, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001238
Adrian Hunter39b12f782013-08-07 14:38:47 +03001239 if (ret > 0)
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001240 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001241
1242 return ret;
1243}
1244
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001245static char *get_kernel_version(const char *root_dir)
1246{
1247 char version[PATH_MAX];
1248 FILE *file;
1249 char *name, *tmp;
1250 const char *prefix = "Linux version ";
1251
1252 sprintf(version, "%s/proc/version", root_dir);
1253 file = fopen(version, "r");
1254 if (!file)
1255 return NULL;
1256
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001257 tmp = fgets(version, sizeof(version), file);
1258 fclose(file);
Donald Yandt34b65af2019-05-28 09:41:28 -04001259 if (!tmp)
1260 return NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001261
1262 name = strstr(version, prefix);
1263 if (!name)
1264 return NULL;
1265 name += strlen(prefix);
1266 tmp = strchr(name, ' ');
1267 if (tmp)
1268 *tmp = '\0';
1269
1270 return strdup(name);
1271}
1272
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001273static bool is_kmod_dso(struct dso *dso)
1274{
1275 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1276 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1277}
1278
1279static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1280 struct kmod_path *m)
1281{
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001282 char *long_name;
Arnaldo Carvalho de Melo83cf7742018-04-24 12:16:09 -03001283 struct map *map = map_groups__find_by_name(mg, m->name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001284
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001285 if (map == NULL)
1286 return 0;
1287
1288 long_name = strdup(path);
1289 if (long_name == NULL)
1290 return -ENOMEM;
1291
1292 dso__set_long_name(map->dso, long_name, true);
1293 dso__kernel_module_get_build_id(map->dso, "");
1294
1295 /*
1296 * Full name could reveal us kmod compression, so
1297 * we need to update the symtab_type if needed.
1298 */
Jiri Olsa2af52472018-08-17 11:48:07 +02001299 if (m->comp && is_kmod_dso(map->dso)) {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001300 map->dso->symtab_type++;
Jiri Olsa2af52472018-08-17 11:48:07 +02001301 map->dso->comp = m->comp;
1302 }
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001303
1304 return 0;
1305}
1306
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001307static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -04001308 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001309{
1310 struct dirent *dent;
1311 DIR *dir = opendir(dir_name);
1312 int ret = 0;
1313
1314 if (!dir) {
1315 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1316 return -1;
1317 }
1318
1319 while ((dent = readdir(dir)) != NULL) {
1320 char path[PATH_MAX];
1321 struct stat st;
1322
1323 /*sshfs might return bad dent->d_type, so we have to stat*/
1324 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1325 if (stat(path, &st))
1326 continue;
1327
1328 if (S_ISDIR(st.st_mode)) {
1329 if (!strcmp(dent->d_name, ".") ||
1330 !strcmp(dent->d_name, ".."))
1331 continue;
1332
Richard Yao61d42902014-04-26 13:17:55 -04001333 /* Do not follow top-level source and build symlinks */
1334 if (depth == 0) {
1335 if (!strcmp(dent->d_name, "source") ||
1336 !strcmp(dent->d_name, "build"))
1337 continue;
1338 }
1339
1340 ret = map_groups__set_modules_path_dir(mg, path,
1341 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001342 if (ret < 0)
1343 goto out;
1344 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001345 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001346
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001347 ret = kmod_path__parse_name(&m, dent->d_name);
1348 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001349 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001350
1351 if (m.kmod)
1352 ret = map_groups__set_module_path(mg, path, &m);
1353
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001354 zfree(&m.name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001355
1356 if (ret)
1357 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001358 }
1359 }
1360
1361out:
1362 closedir(dir);
1363 return ret;
1364}
1365
1366static int machine__set_modules_path(struct machine *machine)
1367{
1368 char *version;
1369 char modules_path[PATH_MAX];
1370
1371 version = get_kernel_version(machine->root_dir);
1372 if (!version)
1373 return -1;
1374
Richard Yao61d42902014-04-26 13:17:55 -04001375 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001376 machine->root_dir, version);
1377 free(version);
1378
Richard Yao61d42902014-04-26 13:17:55 -04001379 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001380}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001381int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
Thomas Richter12a6d292019-07-24 14:27:02 +02001382 u64 *size __maybe_unused,
Song Shan Gong203d8a42016-07-21 11:10:51 +08001383 const char *name __maybe_unused)
1384{
1385 return 0;
1386}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001387
Thomas Richter9ad46522017-08-03 15:49:02 +02001388static int machine__create_module(void *arg, const char *name, u64 start,
1389 u64 size)
Adrian Hunter316d70d2013-10-08 11:45:48 +03001390{
1391 struct machine *machine = arg;
1392 struct map *map;
1393
Thomas Richter12a6d292019-07-24 14:27:02 +02001394 if (arch__fix_module_text_start(&start, &size, name) < 0)
Song Shan Gong203d8a42016-07-21 11:10:51 +08001395 return -1;
1396
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001397 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001398 if (map == NULL)
1399 return -1;
Thomas Richter9ad46522017-08-03 15:49:02 +02001400 map->end = start + size;
Adrian Hunter316d70d2013-10-08 11:45:48 +03001401
1402 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1403
1404 return 0;
1405}
1406
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001407static int machine__create_modules(struct machine *machine)
1408{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001409 const char *modules;
1410 char path[PATH_MAX];
1411
Adrian Hunterf4be9042013-09-22 13:22:09 +03001412 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001413 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001414 } else {
1415 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001416 modules = path;
1417 }
1418
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001419 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001420 return -1;
1421
Adrian Hunter316d70d2013-10-08 11:45:48 +03001422 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001423 return -1;
1424
Adrian Hunter316d70d2013-10-08 11:45:48 +03001425 if (!machine__set_modules_path(machine))
1426 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001427
Adrian Hunter316d70d2013-10-08 11:45:48 +03001428 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001429
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001430 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001431}
1432
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001433static void machine__set_kernel_mmap(struct machine *machine,
1434 u64 start, u64 end)
1435{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001436 machine->vmlinux_map->start = start;
1437 machine->vmlinux_map->end = end;
1438 /*
1439 * Be a bit paranoid here, some perf.data file came with
1440 * a zero sized synthesized MMAP event for the kernel.
1441 */
1442 if (start == 0 && end == 0)
1443 machine->vmlinux_map->end = ~0ULL;
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001444}
1445
Wei Li977c7a62019-02-28 17:20:03 +08001446static void machine__update_kernel_mmap(struct machine *machine,
1447 u64 start, u64 end)
1448{
1449 struct map *map = machine__kernel_map(machine);
1450
1451 map__get(map);
1452 map_groups__remove(&machine->kmaps, map);
1453
1454 machine__set_kernel_mmap(machine, start, end);
1455
1456 map_groups__insert(&machine->kmaps, map);
1457 map__put(map);
1458}
1459
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001460int machine__create_kernel_maps(struct machine *machine)
1461{
1462 struct dso *kernel = machine__get_kernel(machine);
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001463 const char *name = NULL;
Namhyung Kimee05d21792018-02-19 19:05:45 +09001464 struct map *map;
Jiri Olsaed9adb22019-05-08 15:20:03 +02001465 u64 start = 0, end = ~0ULL;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001466 int ret;
1467
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001468 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001469 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001470
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001471 ret = __machine__create_kernel_maps(machine, kernel);
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001472 if (ret < 0)
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001473 goto out_put;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001474
1475 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1476 if (machine__is_host(machine))
1477 pr_debug("Problems creating module maps, "
1478 "continuing anyway...\n");
1479 else
1480 pr_debug("Problems creating module maps for guest %d, "
1481 "continuing anyway...\n", machine->pid);
1482 }
1483
Jiri Olsaed9adb22019-05-08 15:20:03 +02001484 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001485 if (name &&
Jiri Olsaed9adb22019-05-08 15:20:03 +02001486 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001487 machine__destroy_kernel_maps(machine);
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001488 ret = -1;
1489 goto out_put;
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001490 }
Namhyung Kimee05d21792018-02-19 19:05:45 +09001491
Wei Li977c7a62019-02-28 17:20:03 +08001492 /*
1493 * we have a real start address now, so re-order the kmaps
1494 * assume it's the last in the kmaps
1495 */
Jiri Olsaed9adb22019-05-08 15:20:03 +02001496 machine__update_kernel_mmap(machine, start, end);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001497 }
1498
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001499 if (machine__create_extra_kernel_maps(machine, kernel))
1500 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1501
Jiri Olsaed9adb22019-05-08 15:20:03 +02001502 if (end == ~0ULL) {
1503 /* update end address of the kernel map using adjacent module address */
1504 map = map__next(machine__kernel_map(machine));
1505 if (map)
1506 machine__set_kernel_mmap(machine, start, map->start);
1507 }
1508
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001509out_put:
1510 dso__put(kernel);
1511 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001512}
1513
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001514static bool machine__uses_kcore(struct machine *machine)
1515{
1516 struct dso *dso;
1517
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001518 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001519 if (dso__is_kcore(dso))
1520 return true;
1521 }
1522
1523 return false;
1524}
1525
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001526static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1527 union perf_event *event)
1528{
1529 return machine__is(machine, "x86_64") &&
1530 is_entry_trampoline(event->mmap.filename);
1531}
1532
1533static int machine__process_extra_kernel_map(struct machine *machine,
1534 union perf_event *event)
1535{
1536 struct map *kernel_map = machine__kernel_map(machine);
1537 struct dso *kernel = kernel_map ? kernel_map->dso : NULL;
1538 struct extra_kernel_map xm = {
1539 .start = event->mmap.start,
1540 .end = event->mmap.start + event->mmap.len,
1541 .pgoff = event->mmap.pgoff,
1542 };
1543
1544 if (kernel == NULL)
1545 return -1;
1546
1547 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1548
1549 return machine__create_extra_kernel_map(machine, kernel, &xm);
1550}
1551
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001552static int machine__process_kernel_mmap_event(struct machine *machine,
1553 union perf_event *event)
1554{
1555 struct map *map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001556 enum dso_kernel_type kernel_type;
1557 bool is_kernel_mmap;
1558
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001559 /* If we have maps from kcore then we do not need or want any others */
1560 if (machine__uses_kcore(machine))
1561 return 0;
1562
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001563 if (machine__is_host(machine))
1564 kernel_type = DSO_TYPE_KERNEL;
1565 else
1566 kernel_type = DSO_TYPE_GUEST_KERNEL;
1567
1568 is_kernel_mmap = memcmp(event->mmap.filename,
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001569 machine->mmap_name,
1570 strlen(machine->mmap_name) - 1) == 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001571 if (event->mmap.filename[0] == '/' ||
1572 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001573 map = machine__findnew_module_map(machine, event->mmap.start,
1574 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001575 if (map == NULL)
1576 goto out_problem;
1577
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001578 map->end = map->start + event->mmap.len;
1579 } else if (is_kernel_mmap) {
1580 const char *symbol_name = (event->mmap.filename +
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001581 strlen(machine->mmap_name));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001582 /*
1583 * Should be there already, from the build-id table in
1584 * the header.
1585 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001586 struct dso *kernel = NULL;
1587 struct dso *dso;
1588
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001589 down_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001590
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001591 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001592
1593 /*
1594 * The cpumode passed to is_kernel_module is not the
1595 * cpumode of *this* event. If we insist on passing
1596 * correct cpumode to is_kernel_module, we should
1597 * record the cpumode when we adding this dso to the
1598 * linked list.
1599 *
1600 * However we don't really need passing correct
1601 * cpumode. We know the correct cpumode must be kernel
1602 * mode (if not, we should not link it onto kernel_dsos
1603 * list).
1604 *
1605 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1606 * is_kernel_module() treats it as a kernel cpumode.
1607 */
1608
1609 if (!dso->kernel ||
1610 is_kernel_module(dso->long_name,
1611 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001612 continue;
1613
Wang Nan1f121b02015-06-03 08:52:21 +00001614
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001615 kernel = dso;
1616 break;
1617 }
1618
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001619 up_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001620
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001621 if (kernel == NULL)
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001622 kernel = machine__findnew_dso(machine, machine->mmap_name);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001623 if (kernel == NULL)
1624 goto out_problem;
1625
1626 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001627 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1628 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001629 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001630 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001631
Namhyung Kim330dfa22014-11-18 13:30:28 +09001632 if (strstr(kernel->long_name, "vmlinux"))
1633 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001634
Wei Li977c7a62019-02-28 17:20:03 +08001635 machine__update_kernel_mmap(machine, event->mmap.start,
Jiri Olsa05db6ff2018-02-15 13:26:31 +01001636 event->mmap.start + event->mmap.len);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001637
1638 /*
1639 * Avoid using a zero address (kptr_restrict) for the ref reloc
1640 * symbol. Effectively having zero here means that at record
1641 * time /proc/sys/kernel/kptr_restrict was non zero.
1642 */
1643 if (event->mmap.pgoff != 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001644 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1645 symbol_name,
1646 event->mmap.pgoff);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001647 }
1648
1649 if (machine__is_default_guest(machine)) {
1650 /*
1651 * preload dso of guest kernel and modules
1652 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001653 dso__load(kernel, machine__kernel_map(machine));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001654 }
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001655 } else if (perf_event__is_extra_kernel_mmap(machine, event)) {
1656 return machine__process_extra_kernel_map(machine, event);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001657 }
1658 return 0;
1659out_problem:
1660 return -1;
1661}
1662
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001663int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001664 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001665 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001666{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001667 struct thread *thread;
1668 struct map *map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001669 int ret = 0;
1670
1671 if (dump_trace)
1672 perf_event__fprintf_mmap2(event, stdout);
1673
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001674 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1675 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001676 ret = machine__process_kernel_mmap_event(machine, event);
1677 if (ret < 0)
1678 goto out_problem;
1679 return 0;
1680 }
1681
1682 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001683 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001684 if (thread == NULL)
1685 goto out_problem;
1686
Adrian Hunter2a030682014-07-22 16:17:53 +03001687 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001688 event->mmap2.len, event->mmap2.pgoff,
Krister Johansenbf2e7102017-07-05 18:48:09 -07001689 event->mmap2.maj,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001690 event->mmap2.min, event->mmap2.ino,
1691 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001692 event->mmap2.prot,
1693 event->mmap2.flags,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001694 event->mmap2.filename, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001695
1696 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001697 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001698
He Kuang8132a2a2016-06-03 03:33:13 +00001699 ret = thread__insert_map(thread, map);
1700 if (ret)
1701 goto out_problem_insert;
1702
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001703 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001704 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001705 return 0;
1706
He Kuang8132a2a2016-06-03 03:33:13 +00001707out_problem_insert:
1708 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001709out_problem_map:
1710 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001711out_problem:
1712 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1713 return 0;
1714}
1715
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001716int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001717 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001718{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001719 struct thread *thread;
1720 struct map *map;
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001721 u32 prot = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001722 int ret = 0;
1723
1724 if (dump_trace)
1725 perf_event__fprintf_mmap(event, stdout);
1726
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001727 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1728 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001729 ret = machine__process_kernel_mmap_event(machine, event);
1730 if (ret < 0)
1731 goto out_problem;
1732 return 0;
1733 }
1734
Adrian Hunter314add62013-08-27 11:23:03 +03001735 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001736 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001737 if (thread == NULL)
1738 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001739
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001740 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001741 prot = PROT_EXEC;
Stephane Eranianbad40912013-01-24 16:10:40 +01001742
Adrian Hunter2a030682014-07-22 16:17:53 +03001743 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001744 event->mmap.len, event->mmap.pgoff,
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001745 0, 0, 0, 0, prot, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001746 event->mmap.filename,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001747 thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001748
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001749 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001750 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001751
He Kuang8132a2a2016-06-03 03:33:13 +00001752 ret = thread__insert_map(thread, map);
1753 if (ret)
1754 goto out_problem_insert;
1755
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001756 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001757 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001758 return 0;
1759
He Kuang8132a2a2016-06-03 03:33:13 +00001760out_problem_insert:
1761 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001762out_problem_map:
1763 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001764out_problem:
1765 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1766 return 0;
1767}
1768
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001769static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001770{
Kan Liang91e467b2017-09-10 19:23:14 -07001771 struct threads *threads = machine__threads(machine, th->tid);
1772
1773 if (threads->last_match == th)
Jiri Olsa67fda0f2018-07-19 16:33:43 +02001774 threads__set_last_match(threads, NULL);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001775
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001776 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001777 down_write(&threads->lock);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001778
1779 BUG_ON(refcount_read(&th->refcnt) == 0);
1780
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001781 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001782 RB_CLEAR_NODE(&th->rb_node);
Kan Liang91e467b2017-09-10 19:23:14 -07001783 --threads->nr;
David Ahern236a3bb2013-08-14 08:49:27 -06001784 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001785 * Move it first to the dead_threads list, then drop the reference,
1786 * if this is the last reference, then the thread__delete destructor
1787 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001788 */
Kan Liang91e467b2017-09-10 19:23:14 -07001789 list_add_tail(&th->node, &threads->dead);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001790
1791 /*
1792 * We need to do the put here because if this is the last refcount,
1793 * then we will be touching the threads->dead head when removing the
1794 * thread.
1795 */
1796 thread__put(th);
1797
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001798 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001799 up_write(&threads->lock);
David Ahern236a3bb2013-08-14 08:49:27 -06001800}
1801
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001802void machine__remove_thread(struct machine *machine, struct thread *th)
1803{
1804 return __machine__remove_thread(machine, th, true);
1805}
1806
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001807int machine__process_fork_event(struct machine *machine, union perf_event *event,
1808 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001809{
Jiri Olsad75e6092014-03-14 15:00:03 +01001810 struct thread *thread = machine__find_thread(machine,
1811 event->fork.pid,
1812 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001813 struct thread *parent = machine__findnew_thread(machine,
1814 event->fork.ppid,
1815 event->fork.ptid);
David Miller4f8f3822018-10-30 22:24:04 -07001816 bool do_maps_clone = true;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001817 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001818
Adrian Hunter5cb73342015-08-19 17:29:20 +03001819 if (dump_trace)
1820 perf_event__fprintf_task(event, stdout);
1821
1822 /*
1823 * There may be an existing thread that is not actually the parent,
1824 * either because we are processing events out of order, or because the
1825 * (fork) event that would have removed the thread was lost. Assume the
1826 * latter case and continue on as best we can.
1827 */
1828 if (parent->pid_ != (pid_t)event->fork.ppid) {
1829 dump_printf("removing erroneous parent thread %d/%d\n",
1830 parent->pid_, parent->tid);
1831 machine__remove_thread(machine, parent);
1832 thread__put(parent);
1833 parent = machine__findnew_thread(machine, event->fork.ppid,
1834 event->fork.ptid);
1835 }
1836
David Ahern236a3bb2013-08-14 08:49:27 -06001837 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001838 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001839 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001840 thread__put(thread);
1841 }
David Ahern236a3bb2013-08-14 08:49:27 -06001842
Adrian Hunter314add62013-08-27 11:23:03 +03001843 thread = machine__findnew_thread(machine, event->fork.pid,
1844 event->fork.tid);
David Miller4f8f3822018-10-30 22:24:04 -07001845 /*
1846 * When synthesizing FORK events, we are trying to create thread
1847 * objects for the already running tasks on the machine.
1848 *
1849 * Normally, for a kernel FORK event, we want to clone the parent's
1850 * maps because that is what the kernel just did.
1851 *
1852 * But when synthesizing, this should not be done. If we do, we end up
1853 * with overlapping maps as we process the sythesized MMAP2 events that
1854 * get delivered shortly thereafter.
1855 *
1856 * Use the FORK event misc flags in an internal way to signal this
1857 * situation, so we can elide the map clone when appropriate.
1858 */
1859 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1860 do_maps_clone = false;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001861
1862 if (thread == NULL || parent == NULL ||
David Miller4f8f3822018-10-30 22:24:04 -07001863 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001864 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001865 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001866 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001867 thread__put(thread);
1868 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001869
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001870 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001871}
1872
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001873int machine__process_exit_event(struct machine *machine, union perf_event *event,
1874 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001875{
Jiri Olsad75e6092014-03-14 15:00:03 +01001876 struct thread *thread = machine__find_thread(machine,
1877 event->fork.pid,
1878 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001879
1880 if (dump_trace)
1881 perf_event__fprintf_task(event, stdout);
1882
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001883 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001884 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001885 thread__put(thread);
1886 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001887
1888 return 0;
1889}
1890
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001891int machine__process_event(struct machine *machine, union perf_event *event,
1892 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001893{
1894 int ret;
1895
1896 switch (event->header.type) {
1897 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001898 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001899 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001900 ret = machine__process_mmap_event(machine, event, sample); break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301901 case PERF_RECORD_NAMESPACES:
1902 ret = machine__process_namespaces_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001903 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001904 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001905 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001906 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001907 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001908 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001909 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001910 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001911 case PERF_RECORD_AUX:
1912 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001913 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001914 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001915 case PERF_RECORD_LOST_SAMPLES:
1916 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001917 case PERF_RECORD_SWITCH:
1918 case PERF_RECORD_SWITCH_CPU_WIDE:
1919 ret = machine__process_switch_event(machine, event); break;
Song Liu9aa0bfa2019-01-17 08:15:17 -08001920 case PERF_RECORD_KSYMBOL:
1921 ret = machine__process_ksymbol(machine, event, sample); break;
Song Liu45178a92019-01-17 08:15:18 -08001922 case PERF_RECORD_BPF_EVENT:
1923 ret = machine__process_bpf_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001924 default:
1925 ret = -1;
1926 break;
1927 }
1928
1929 return ret;
1930}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001931
Greg Priceb21484f2012-12-06 21:48:05 -08001932static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001933{
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001934 if (!regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001935 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001936 return 0;
1937}
1938
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001939static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001940 struct addr_map_symbol *ams,
1941 u64 ip)
1942{
1943 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001944
1945 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001946 /*
1947 * We cannot use the header.misc hint to determine whether a
1948 * branch stack address is user, kernel, guest, hypervisor.
1949 * Branches may straddle the kernel/user/hypervisor boundaries.
1950 * Thus, we have to try consecutively until we find a match
1951 * or else, the symbol is unknown
1952 */
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03001953 thread__find_cpumode_addr_location(thread, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001954
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001955 ams->addr = ip;
1956 ams->al_addr = al.addr;
1957 ams->sym = al.sym;
1958 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001959 ams->phys_addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001960}
1961
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001962static void ip__resolve_data(struct thread *thread,
Kan Liang8780fb22017-08-29 13:11:09 -04001963 u8 m, struct addr_map_symbol *ams,
1964 u64 addr, u64 phys_addr)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001965{
1966 struct addr_location al;
1967
1968 memset(&al, 0, sizeof(al));
1969
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03001970 thread__find_symbol(thread, m, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001971
Stephane Eranian98a3b322013-01-24 16:10:35 +01001972 ams->addr = addr;
1973 ams->al_addr = al.addr;
1974 ams->sym = al.sym;
1975 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001976 ams->phys_addr = phys_addr;
Stephane Eranian98a3b322013-01-24 16:10:35 +01001977}
1978
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001979struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1980 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001981{
Jiri Olsa9f874982018-03-07 16:50:06 +01001982 struct mem_info *mi = mem_info__new();
Stephane Eranian98a3b322013-01-24 16:10:35 +01001983
1984 if (!mi)
1985 return NULL;
1986
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001987 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
Kan Liang8780fb22017-08-29 13:11:09 -04001988 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1989 sample->addr, sample->phys_addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001990 mi->data_src.val = sample->data_src;
1991
1992 return mi;
1993}
1994
Milian Wolff40a342c2017-10-09 22:32:56 +02001995static char *callchain_srcline(struct map *map, struct symbol *sym, u64 ip)
1996{
Milian Wolff21ac9d52017-10-19 13:38:34 +02001997 char *srcline = NULL;
Milian Wolff40a342c2017-10-09 22:32:56 +02001998
Milian Wolff21ac9d52017-10-19 13:38:34 +02001999 if (!map || callchain_param.key == CCKEY_FUNCTION)
2000 return srcline;
2001
2002 srcline = srcline__tree_find(&map->dso->srclines, ip);
2003 if (!srcline) {
2004 bool show_sym = false;
2005 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2006
2007 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
Jin Yao935f5a92017-12-30 00:26:52 +08002008 sym, show_sym, show_addr, ip);
Milian Wolff21ac9d52017-10-19 13:38:34 +02002009 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2010 }
2011
2012 return srcline;
Milian Wolff40a342c2017-10-09 22:32:56 +02002013}
2014
Jin Yaoc4ee0622017-08-07 21:05:15 +08002015struct iterations {
2016 int nr_loop_iter;
2017 u64 cycles;
2018};
2019
Andi Kleen37592b82014-11-12 18:05:19 -08002020static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002021 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08002022 struct symbol **parent,
2023 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002024 u8 *cpumode,
Jin Yao410024d2016-10-31 09:19:49 +08002025 u64 ip,
2026 bool branch,
2027 struct branch_flags *flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002028 struct iterations *iter,
Jin Yaob851dd42017-07-18 20:13:15 +08002029 u64 branch_from)
Andi Kleen37592b82014-11-12 18:05:19 -08002030{
2031 struct addr_location al;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002032 int nr_loop_iter = 0;
2033 u64 iter_cycles = 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002034 const char *srcline = NULL;
Andi Kleen37592b82014-11-12 18:05:19 -08002035
2036 al.filtered = 0;
2037 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002038 if (!cpumode) {
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03002039 thread__find_cpumode_addr_location(thread, ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002040 } else {
Kan Liang2e777842014-12-02 10:06:53 -05002041 if (ip >= PERF_CONTEXT_MAX) {
2042 switch (ip) {
2043 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002044 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05002045 break;
2046 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002047 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05002048 break;
2049 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002050 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05002051 break;
2052 default:
2053 pr_debug("invalid callchain context: "
2054 "%"PRId64"\n", (s64) ip);
2055 /*
2056 * It seems the callchain is corrupted.
2057 * Discard all.
2058 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002059 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05002060 return 1;
2061 }
2062 return 0;
2063 }
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002064 thread__find_symbol(thread, *cpumode, ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05002065 }
2066
Andi Kleen37592b82014-11-12 18:05:19 -08002067 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02002068 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08002069 symbol__match_regex(al.sym, &parent_regex))
2070 *parent = al.sym;
2071 else if (have_ignore_callees && root_al &&
2072 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2073 /* Treat this symbol as the root,
2074 forgetting its callees. */
2075 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002076 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08002077 }
2078 }
2079
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002080 if (symbol_conf.hide_unresolved && al.sym == NULL)
2081 return 0;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002082
2083 if (iter) {
2084 nr_loop_iter = iter->nr_loop_iter;
2085 iter_cycles = iter->cycles;
2086 }
2087
Milian Wolff40a342c2017-10-09 22:32:56 +02002088 srcline = callchain_srcline(al.map, al.sym, al.addr);
Sandipan Das19610182018-05-17 12:03:25 +05302089 return callchain_cursor_append(cursor, ip, al.map, al.sym,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002090 branch, flags, nr_loop_iter,
Milian Wolff40a342c2017-10-09 22:32:56 +02002091 iter_cycles, branch_from, srcline);
Andi Kleen37592b82014-11-12 18:05:19 -08002092}
2093
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002094struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2095 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002096{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002097 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002098 const struct branch_stack *bs = sample->branch_stack;
2099 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002100
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002101 if (!bi)
2102 return NULL;
2103
2104 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002105 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
2106 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002107 bi[i].flags = bs->entries[i].flags;
2108 }
2109 return bi;
2110}
2111
Jin Yaoc4ee0622017-08-07 21:05:15 +08002112static void save_iterations(struct iterations *iter,
2113 struct branch_entry *be, int nr)
2114{
2115 int i;
2116
Jin Yaoa3366db02019-01-04 14:10:30 +08002117 iter->nr_loop_iter++;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002118 iter->cycles = 0;
2119
2120 for (i = 0; i < nr; i++)
2121 iter->cycles += be[i].flags.cycles;
2122}
2123
Andi Kleen8b7bad52014-11-12 18:05:20 -08002124#define CHASHSZ 127
2125#define CHASHBITS 7
2126#define NO_ENTRY 0xff
2127
2128#define PERF_MAX_BRANCH_DEPTH 127
2129
2130/* Remove loops. */
Jin Yaoc4ee0622017-08-07 21:05:15 +08002131static int remove_loops(struct branch_entry *l, int nr,
2132 struct iterations *iter)
Andi Kleen8b7bad52014-11-12 18:05:20 -08002133{
2134 int i, j, off;
2135 unsigned char chash[CHASHSZ];
2136
2137 memset(chash, NO_ENTRY, sizeof(chash));
2138
2139 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2140
2141 for (i = 0; i < nr; i++) {
2142 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2143
2144 /* no collision handling for now */
2145 if (chash[h] == NO_ENTRY) {
2146 chash[h] = i;
2147 } else if (l[chash[h]].from == l[i].from) {
2148 bool is_loop = true;
2149 /* check if it is a real loop */
2150 off = 0;
2151 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2152 if (l[j].from != l[i + off].from) {
2153 is_loop = false;
2154 break;
2155 }
2156 if (is_loop) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002157 j = nr - (i + off);
2158 if (j > 0) {
2159 save_iterations(iter + i + off,
2160 l + i, off);
2161
2162 memmove(iter + i, iter + i + off,
2163 j * sizeof(*iter));
2164
2165 memmove(l + i, l + i + off,
2166 j * sizeof(*l));
2167 }
2168
Andi Kleen8b7bad52014-11-12 18:05:20 -08002169 nr -= off;
2170 }
2171 }
2172 }
2173 return nr;
2174}
2175
Kan Liang384b6052015-01-05 13:23:05 -05002176/*
2177 * Recolve LBR callstack chain sample
2178 * Return:
2179 * 1 on success get LBR callchain information
2180 * 0 no available LBR callchain information, should try fp
2181 * negative error code on other errors.
2182 */
2183static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002184 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05002185 struct perf_sample *sample,
2186 struct symbol **parent,
2187 struct addr_location *root_al,
2188 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002189{
Kan Liang384b6052015-01-05 13:23:05 -05002190 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002191 int chain_nr = min(max_stack, (int)chain->nr), i;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002192 u8 cpumode = PERF_RECORD_MISC_USER;
Jin Yaob851dd42017-07-18 20:13:15 +08002193 u64 ip, branch_from = 0;
Kan Liang384b6052015-01-05 13:23:05 -05002194
2195 for (i = 0; i < chain_nr; i++) {
2196 if (chain->ips[i] == PERF_CONTEXT_USER)
2197 break;
2198 }
2199
2200 /* LBR only affects the user callchain */
2201 if (i != chain_nr) {
2202 struct branch_stack *lbr_stack = sample->branch_stack;
Jin Yao410024d2016-10-31 09:19:49 +08002203 int lbr_nr = lbr_stack->nr, j, k;
2204 bool branch;
2205 struct branch_flags *flags;
Kan Liang384b6052015-01-05 13:23:05 -05002206 /*
2207 * LBR callstack can only get user call chain.
2208 * The mix_chain_nr is kernel call chain
2209 * number plus LBR user call chain number.
2210 * i is kernel call chain number,
2211 * 1 is PERF_CONTEXT_USER,
2212 * lbr_nr + 1 is the user call chain number.
2213 * For details, please refer to the comments
2214 * in callchain__printf
2215 */
2216 int mix_chain_nr = i + 1 + lbr_nr + 1;
2217
Kan Liang384b6052015-01-05 13:23:05 -05002218 for (j = 0; j < mix_chain_nr; j++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002219 int err;
Jin Yao410024d2016-10-31 09:19:49 +08002220 branch = false;
2221 flags = NULL;
2222
Kan Liang384b6052015-01-05 13:23:05 -05002223 if (callchain_param.order == ORDER_CALLEE) {
2224 if (j < i + 1)
2225 ip = chain->ips[j];
Jin Yao410024d2016-10-31 09:19:49 +08002226 else if (j > i + 1) {
2227 k = j - i - 2;
2228 ip = lbr_stack->entries[k].from;
2229 branch = true;
2230 flags = &lbr_stack->entries[k].flags;
2231 } else {
Kan Liang384b6052015-01-05 13:23:05 -05002232 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08002233 branch = true;
2234 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08002235 branch_from =
2236 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08002237 }
Kan Liang384b6052015-01-05 13:23:05 -05002238 } else {
Jin Yao410024d2016-10-31 09:19:49 +08002239 if (j < lbr_nr) {
2240 k = lbr_nr - j - 1;
2241 ip = lbr_stack->entries[k].from;
2242 branch = true;
2243 flags = &lbr_stack->entries[k].flags;
2244 }
Kan Liang384b6052015-01-05 13:23:05 -05002245 else if (j > lbr_nr)
2246 ip = chain->ips[i + 1 - (j - lbr_nr)];
Jin Yao410024d2016-10-31 09:19:49 +08002247 else {
Kan Liang384b6052015-01-05 13:23:05 -05002248 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08002249 branch = true;
2250 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08002251 branch_from =
2252 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08002253 }
Kan Liang384b6052015-01-05 13:23:05 -05002254 }
2255
Jin Yao410024d2016-10-31 09:19:49 +08002256 err = add_callchain_ip(thread, cursor, parent,
2257 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002258 branch, flags, NULL,
Jin Yaob851dd42017-07-18 20:13:15 +08002259 branch_from);
Kan Liang384b6052015-01-05 13:23:05 -05002260 if (err)
2261 return (err < 0) ? err : 0;
2262 }
2263 return 1;
2264 }
2265
2266 return 0;
2267}
2268
David S. Millere9024d52018-10-30 12:12:26 -03002269static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2270 struct callchain_cursor *cursor,
2271 struct symbol **parent,
2272 struct addr_location *root_al,
2273 u8 *cpumode, int ent)
2274{
2275 int err = 0;
2276
2277 while (--ent >= 0) {
2278 u64 ip = chain->ips[ent];
2279
2280 if (ip >= PERF_CONTEXT_MAX) {
2281 err = add_callchain_ip(thread, cursor, parent,
2282 root_al, cpumode, ip,
2283 false, NULL, NULL, 0);
2284 break;
2285 }
2286 }
2287 return err;
2288}
2289
Kan Liang384b6052015-01-05 13:23:05 -05002290static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002291 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002292 struct evsel *evsel,
Kan Liang384b6052015-01-05 13:23:05 -05002293 struct perf_sample *sample,
2294 struct symbol **parent,
2295 struct addr_location *root_al,
2296 int max_stack)
2297{
2298 struct branch_stack *branch = sample->branch_stack;
2299 struct ip_callchain *chain = sample->callchain;
Jin Yaob49a8212017-05-08 18:43:02 +08002300 int chain_nr = 0;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002301 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002302 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08002303 int skip_idx = -1;
2304 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002305
Jin Yaob49a8212017-05-08 18:43:02 +08002306 if (chain)
2307 chain_nr = chain->nr;
2308
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03002309 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002310 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05002311 root_al, max_stack);
2312 if (err)
2313 return (err < 0) ? err : 0;
2314 }
2315
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002316 /*
2317 * Based on DWARF debug information, some architectures skip
2318 * a callchain entry saved by the kernel.
2319 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002320 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002321
Andi Kleen8b7bad52014-11-12 18:05:20 -08002322 /*
2323 * Add branches to call stack for easier browsing. This gives
2324 * more context for a sample than just the callers.
2325 *
2326 * This uses individual histograms of paths compared to the
2327 * aggregated histograms the normal LBR mode uses.
2328 *
2329 * Limitations for now:
2330 * - No extra filters
2331 * - No annotations (should annotate somehow)
2332 */
2333
2334 if (branch && callchain_param.branch_callstack) {
2335 int nr = min(max_stack, (int)branch->nr);
2336 struct branch_entry be[nr];
Jin Yaoc4ee0622017-08-07 21:05:15 +08002337 struct iterations iter[nr];
Andi Kleen8b7bad52014-11-12 18:05:20 -08002338
2339 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2340 pr_warning("corrupted branch chain. skipping...\n");
2341 goto check_calls;
2342 }
2343
2344 for (i = 0; i < nr; i++) {
2345 if (callchain_param.order == ORDER_CALLEE) {
2346 be[i] = branch->entries[i];
Jin Yaob49a8212017-05-08 18:43:02 +08002347
2348 if (chain == NULL)
2349 continue;
2350
Andi Kleen8b7bad52014-11-12 18:05:20 -08002351 /*
2352 * Check for overlap into the callchain.
2353 * The return address is one off compared to
2354 * the branch entry. To adjust for this
2355 * assume the calling instruction is not longer
2356 * than 8 bytes.
2357 */
2358 if (i == skip_idx ||
2359 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2360 first_call++;
2361 else if (be[i].from < chain->ips[first_call] &&
2362 be[i].from >= chain->ips[first_call] - 8)
2363 first_call++;
2364 } else
2365 be[i] = branch->entries[branch->nr - i - 1];
2366 }
2367
Jin Yaoc4ee0622017-08-07 21:05:15 +08002368 memset(iter, 0, sizeof(struct iterations) * nr);
2369 nr = remove_loops(be, nr, iter);
Jin Yao410024d2016-10-31 09:19:49 +08002370
Andi Kleen8b7bad52014-11-12 18:05:20 -08002371 for (i = 0; i < nr; i++) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002372 err = add_callchain_ip(thread, cursor, parent,
2373 root_al,
2374 NULL, be[i].to,
2375 true, &be[i].flags,
2376 NULL, be[i].from);
Jin Yao410024d2016-10-31 09:19:49 +08002377
Andi Kleen8b7bad52014-11-12 18:05:20 -08002378 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002379 err = add_callchain_ip(thread, cursor, parent, root_al,
Jin Yao410024d2016-10-31 09:19:49 +08002380 NULL, be[i].from,
2381 true, &be[i].flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002382 &iter[i], 0);
Andi Kleen8b7bad52014-11-12 18:05:20 -08002383 if (err == -EINVAL)
2384 break;
2385 if (err)
2386 return err;
2387 }
Jin Yaob49a8212017-05-08 18:43:02 +08002388
2389 if (chain_nr == 0)
2390 return 0;
2391
Andi Kleen8b7bad52014-11-12 18:05:20 -08002392 chain_nr -= nr;
2393 }
2394
2395check_calls:
David S. Millere9024d52018-10-30 12:12:26 -03002396 if (callchain_param.order != ORDER_CALLEE) {
2397 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2398 &cpumode, chain->nr - first_call);
2399 if (err)
2400 return (err < 0) ? err : 0;
2401 }
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002402 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002403 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002404 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002405
2406 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002407 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002408 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002409 j = chain->nr - i - 1;
2410
2411#ifdef HAVE_SKIP_CALLCHAIN_IDX
2412 if (j == skip_idx)
2413 continue;
2414#endif
2415 ip = chain->ips[j];
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002416 if (ip < PERF_CONTEXT_MAX)
2417 ++nr_entries;
David S. Millere9024d52018-10-30 12:12:26 -03002418 else if (callchain_param.order != ORDER_CALLEE) {
2419 err = find_prev_cpumode(chain, thread, cursor, parent,
2420 root_al, &cpumode, j);
2421 if (err)
2422 return (err < 0) ? err : 0;
2423 continue;
2424 }
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002425
Jin Yao410024d2016-10-31 09:19:49 +08002426 err = add_callchain_ip(thread, cursor, parent,
2427 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002428 false, NULL, NULL, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002429
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002430 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05002431 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002432 }
2433
2434 return 0;
2435}
2436
Milian Wolff11ea2512017-10-09 22:32:59 +02002437static int append_inlines(struct callchain_cursor *cursor,
2438 struct map *map, struct symbol *sym, u64 ip)
2439{
2440 struct inline_node *inline_node;
2441 struct inline_list *ilist;
2442 u64 addr;
Milian Wolffb38775c2017-10-19 13:38:33 +02002443 int ret = 1;
Milian Wolff11ea2512017-10-09 22:32:59 +02002444
2445 if (!symbol_conf.inline_name || !map || !sym)
Milian Wolffb38775c2017-10-19 13:38:33 +02002446 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002447
Milian Wolff7a8a8fc2018-09-26 15:52:06 +02002448 addr = map__map_ip(map, ip);
2449 addr = map__rip_2objdump(map, addr);
Milian Wolff11ea2512017-10-09 22:32:59 +02002450
2451 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2452 if (!inline_node) {
2453 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2454 if (!inline_node)
Milian Wolffb38775c2017-10-19 13:38:33 +02002455 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002456 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2457 }
2458
2459 list_for_each_entry(ilist, &inline_node->val, list) {
Milian Wolffb38775c2017-10-19 13:38:33 +02002460 ret = callchain_cursor_append(cursor, ip, map,
2461 ilist->symbol, false,
2462 NULL, 0, 0, 0, ilist->srcline);
Milian Wolff11ea2512017-10-09 22:32:59 +02002463
2464 if (ret != 0)
2465 return ret;
2466 }
2467
Milian Wolffb38775c2017-10-19 13:38:33 +02002468 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002469}
2470
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002471static int unwind_entry(struct unwind_entry *entry, void *arg)
2472{
2473 struct callchain_cursor *cursor = arg;
Milian Wolff40a342c2017-10-09 22:32:56 +02002474 const char *srcline = NULL;
Milian Wolffff4ce282018-09-26 15:52:05 +02002475 u64 addr = entry->ip;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002476
2477 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2478 return 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002479
Milian Wolff11ea2512017-10-09 22:32:59 +02002480 if (append_inlines(cursor, entry->map, entry->sym, entry->ip) == 0)
2481 return 0;
2482
Sandipan Das2a9d5052018-07-03 17:35:55 +05302483 /*
2484 * Convert entry->ip from a virtual address to an offset in
2485 * its corresponding binary.
2486 */
Milian Wolffff4ce282018-09-26 15:52:05 +02002487 if (entry->map)
2488 addr = map__map_ip(entry->map, entry->ip);
Sandipan Das2a9d5052018-07-03 17:35:55 +05302489
2490 srcline = callchain_srcline(entry->map, entry->sym, addr);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002491 return callchain_cursor_append(cursor, entry->ip,
Jin Yao410024d2016-10-31 09:19:49 +08002492 entry->map, entry->sym,
Milian Wolff40a342c2017-10-09 22:32:56 +02002493 false, NULL, 0, 0, 0, srcline);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002494}
2495
Chris Phlipot9919a652016-04-28 01:19:06 -07002496static int thread__resolve_callchain_unwind(struct thread *thread,
2497 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002498 struct evsel *evsel,
Chris Phlipot9919a652016-04-28 01:19:06 -07002499 struct perf_sample *sample,
2500 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002501{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002502 /* Can we do dwarf post unwind? */
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002503 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2504 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002505 return 0;
2506
2507 /* Bail out if nothing was captured. */
2508 if ((!sample->user_regs.regs) ||
2509 (!sample->user_stack.size))
2510 return 0;
2511
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002512 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01002513 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07002514}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002515
Chris Phlipot9919a652016-04-28 01:19:06 -07002516int thread__resolve_callchain(struct thread *thread,
2517 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002518 struct evsel *evsel,
Chris Phlipot9919a652016-04-28 01:19:06 -07002519 struct perf_sample *sample,
2520 struct symbol **parent,
2521 struct addr_location *root_al,
2522 int max_stack)
2523{
2524 int ret = 0;
2525
Jiri Olsa914eb9c2017-08-06 16:39:39 +02002526 callchain_cursor_reset(cursor);
Chris Phlipot9919a652016-04-28 01:19:06 -07002527
2528 if (callchain_param.order == ORDER_CALLEE) {
2529 ret = thread__resolve_callchain_sample(thread, cursor,
2530 evsel, sample,
2531 parent, root_al,
2532 max_stack);
2533 if (ret)
2534 return ret;
2535 ret = thread__resolve_callchain_unwind(thread, cursor,
2536 evsel, sample,
2537 max_stack);
2538 } else {
2539 ret = thread__resolve_callchain_unwind(thread, cursor,
2540 evsel, sample,
2541 max_stack);
2542 if (ret)
2543 return ret;
2544 ret = thread__resolve_callchain_sample(thread, cursor,
2545 evsel, sample,
2546 parent, root_al,
2547 max_stack);
2548 }
2549
2550 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002551}
David Ahern35feee12013-09-28 13:12:58 -06002552
2553int machine__for_each_thread(struct machine *machine,
2554 int (*fn)(struct thread *thread, void *p),
2555 void *priv)
2556{
Kan Liang91e467b2017-09-10 19:23:14 -07002557 struct threads *threads;
David Ahern35feee12013-09-28 13:12:58 -06002558 struct rb_node *nd;
2559 struct thread *thread;
2560 int rc = 0;
Kan Liang91e467b2017-09-10 19:23:14 -07002561 int i;
David Ahern35feee12013-09-28 13:12:58 -06002562
Kan Liang91e467b2017-09-10 19:23:14 -07002563 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
2564 threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08002565 for (nd = rb_first_cached(&threads->entries); nd;
2566 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -07002567 thread = rb_entry(nd, struct thread, rb_node);
2568 rc = fn(thread, priv);
2569 if (rc != 0)
2570 return rc;
2571 }
David Ahern35feee12013-09-28 13:12:58 -06002572
Kan Liang91e467b2017-09-10 19:23:14 -07002573 list_for_each_entry(thread, &threads->dead, node) {
2574 rc = fn(thread, priv);
2575 if (rc != 0)
2576 return rc;
2577 }
David Ahern35feee12013-09-28 13:12:58 -06002578 }
2579 return rc;
2580}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002581
Adrian Huntera5499b32015-05-29 16:33:30 +03002582int machines__for_each_thread(struct machines *machines,
2583 int (*fn)(struct thread *thread, void *p),
2584 void *priv)
2585{
2586 struct rb_node *nd;
2587 int rc = 0;
2588
2589 rc = machine__for_each_thread(&machines->host, fn, priv);
2590 if (rc != 0)
2591 return rc;
2592
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08002593 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntera5499b32015-05-29 16:33:30 +03002594 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2595
2596 rc = machine__for_each_thread(machine, fn, priv);
2597 if (rc != 0)
2598 return rc;
2599 }
2600 return rc;
2601}
2602
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03002603int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Jiri Olsa9749b902019-07-21 13:23:50 +02002604 struct target *target, struct perf_thread_map *threads,
Kan Liang9d9cad72015-06-17 09:51:11 -04002605 perf_event__handler_t process, bool data_mmap,
Kan Liang340b47f2017-09-29 07:47:54 -07002606 unsigned int nr_threads_synthesize)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002607{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002608 if (target__has_task(target))
Mark Drayton3fcb10e2018-12-04 12:34:20 -08002609 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002610 else if (target__has_cpu(target))
Kan Liang340b47f2017-09-29 07:47:54 -07002611 return perf_event__synthesize_threads(tool, process,
2612 machine, data_mmap,
Kan Liang340b47f2017-09-29 07:47:54 -07002613 nr_threads_synthesize);
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002614 /* command specified */
2615 return 0;
2616}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002617
2618pid_t machine__get_current_tid(struct machine *machine, int cpu)
2619{
2620 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2621 return -1;
2622
2623 return machine->current_tid[cpu];
2624}
2625
2626int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2627 pid_t tid)
2628{
2629 struct thread *thread;
2630
2631 if (cpu < 0)
2632 return -EINVAL;
2633
2634 if (!machine->current_tid) {
2635 int i;
2636
2637 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2638 if (!machine->current_tid)
2639 return -ENOMEM;
2640 for (i = 0; i < MAX_NR_CPUS; i++)
2641 machine->current_tid[i] = -1;
2642 }
2643
2644 if (cpu >= MAX_NR_CPUS) {
2645 pr_err("Requested CPU %d too large. ", cpu);
2646 pr_err("Consider raising MAX_NR_CPUS\n");
2647 return -EINVAL;
2648 }
2649
2650 machine->current_tid[cpu] = tid;
2651
2652 thread = machine__findnew_thread(machine, pid, tid);
2653 if (!thread)
2654 return -ENOMEM;
2655
2656 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002657 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002658
2659 return 0;
2660}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002661
Adrian Hunterdbbd34a2018-05-17 12:21:53 +03002662/*
2663 * Compares the raw arch string. N.B. see instead perf_env__arch() if a
2664 * normalized arch is needed.
2665 */
2666bool machine__is(struct machine *machine, const char *arch)
2667{
2668 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
2669}
2670
Adrian Hunter9cecca32018-05-22 13:54:32 +03002671int machine__nr_cpus_avail(struct machine *machine)
2672{
2673 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
2674}
2675
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002676int machine__get_kernel_start(struct machine *machine)
2677{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002678 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002679 int err = 0;
2680
2681 /*
2682 * The only addresses above 2^63 are kernel addresses of a 64-bit
2683 * kernel. Note that addresses are unsigned so that on a 32-bit system
2684 * all addresses including kernel addresses are less than 2^32. In
2685 * that case (32-bit system), if the kernel mapping is unknown, all
2686 * addresses will be assumed to be in user space - see
2687 * machine__kernel_ip().
2688 */
2689 machine->kernel_start = 1ULL << 63;
2690 if (map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002691 err = map__load(map);
Adrian Hunter19422a92018-05-17 12:21:54 +03002692 /*
2693 * On x86_64, PTI entry trampolines are less than the
2694 * start of kernel text, but still above 2^63. So leave
2695 * kernel_start = 1ULL << 63 for x86_64.
2696 */
2697 if (!err && !machine__is(machine, "x86_64"))
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002698 machine->kernel_start = map->start;
2699 }
2700 return err;
2701}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002702
Adrian Hunter8e80ad92018-11-06 23:07:10 +02002703u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
2704{
2705 u8 addr_cpumode = cpumode;
2706 bool kernel_ip;
2707
2708 if (!machine->single_address_space)
2709 goto out;
2710
2711 kernel_ip = machine__kernel_ip(machine, addr);
2712 switch (cpumode) {
2713 case PERF_RECORD_MISC_KERNEL:
2714 case PERF_RECORD_MISC_USER:
2715 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
2716 PERF_RECORD_MISC_USER;
2717 break;
2718 case PERF_RECORD_MISC_GUEST_KERNEL:
2719 case PERF_RECORD_MISC_GUEST_USER:
2720 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
2721 PERF_RECORD_MISC_GUEST_USER;
2722 break;
2723 default:
2724 break;
2725 }
2726out:
2727 return addr_cpumode;
2728}
2729
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002730struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2731{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03002732 return dsos__findnew(&machine->dsos, filename);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002733}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002734
2735char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2736{
2737 struct machine *machine = vmachine;
2738 struct map *map;
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03002739 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002740
2741 if (sym == NULL)
2742 return NULL;
2743
2744 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2745 *addrp = map->unmap_ip(map, sym->start);
2746 return sym->name;
2747}