blob: 394550003693748a7a60105b2e0d2b3d805d6c67 [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 Melof2a39fe2019-08-30 14:45:20 -03006#include <stdlib.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03007#include "callchain.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03008#include "debug.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -03009#include "dso.h"
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030010#include "env.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -030011#include "event.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030012#include "evsel.h"
13#include "hist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030014#include "machine.h"
15#include "map.h"
Arnaldo Carvalho de Melod3300a32019-08-30 15:09:54 -030016#include "map_symbol.h"
17#include "branch.h"
18#include "mem-events.h"
Arnaldo Carvalho de Melo9d5f0c32022-01-18 09:02:43 -030019#include "path.h"
Arnaldo Carvalho de Melo97b9d862019-08-22 17:10:08 -030020#include "srcline.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010021#include "symbol.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030022#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030023#include "strlist.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030024#include "target.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030025#include "thread.h"
Arnaldo Carvalho de Melo97b9d862019-08-22 17:10:08 -030026#include "util.h"
Adrian Hunterd027b6402014-07-23 14:23:00 +030027#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030028#include <stdbool.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030029#include <sys/types.h>
30#include <sys/stat.h>
31#include <unistd.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030032#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080033#include "linux/hash.h"
Hari Bathinif3b36142017-03-08 02:11:43 +053034#include "asm/bug.h"
Song Liu45178a92019-01-17 08:15:18 -080035#include "bpf-event.h"
Jiri Olsa20f2be12019-08-06 15:25:25 +020036#include <internal/lib.h> // page_size
Namhyung Kimd1277aa2020-03-25 21:45:31 +090037#include "cgroup.h"
Alexandre Truongb9f6fbb2021-12-17 15:45:20 +000038#include "arm64-frame-pointer-unwind-support.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030039
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030040#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030041#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -030042#include <linux/mman.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030043#include <linux/string.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030044#include <linux/zalloc.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030045
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030046static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
47
Arnaldo Carvalho de Melo93730f82019-10-31 15:22:24 -030048static struct dso *machine__kernel_dso(struct machine *machine)
49{
50 return machine->vmlinux_map->dso;
51}
52
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030053static void dsos__init(struct dsos *dsos)
54{
55 INIT_LIST_HEAD(&dsos->head);
56 dsos->root = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030057 init_rwsem(&dsos->lock);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030058}
59
Kan Liang91e467b2017-09-10 19:23:14 -070060static void machine__threads_init(struct machine *machine)
61{
62 int i;
63
64 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
65 struct threads *threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -080066 threads->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030067 init_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -070068 threads->nr = 0;
69 INIT_LIST_HEAD(&threads->dead);
70 threads->last_match = NULL;
71 }
72}
73
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010074static int machine__set_mmap_name(struct machine *machine)
75{
Jiri Olsac1925242018-03-12 16:24:06 +010076 if (machine__is_host(machine))
77 machine->mmap_name = strdup("[kernel.kallsyms]");
78 else if (machine__is_default_guest(machine))
79 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
80 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
81 machine->pid) < 0)
82 machine->mmap_name = NULL;
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010083
84 return machine->mmap_name ? 0 : -ENOMEM;
85}
86
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030087int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
88{
Jiri Olsa81f981d2018-02-15 13:26:29 +010089 int err = -ENOMEM;
90
Wang Nan93b0ba32015-12-08 02:25:44 +000091 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -030092 maps__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030093 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030094 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030095
Kan Liang91e467b2017-09-10 19:23:14 -070096 machine__threads_init(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030097
Adrian Hunterd027b6402014-07-23 14:23:00 +030098 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030099 machine->env = NULL;
Adrian Hunterd027b6402014-07-23 14:23:00 +0300100
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300101 machine->pid = pid;
102
Jiri Olsa14bd6d22014-01-07 13:47:19 +0100103 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -0300104 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +0300105 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +0300106 machine->kernel_start = 0;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300107 machine->vmlinux_map = NULL;
Masami Hiramatsucc1121a2015-12-09 11:11:33 +0900108
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300109 machine->root_dir = strdup(root_dir);
110 if (machine->root_dir == NULL)
111 return -ENOMEM;
112
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100113 if (machine__set_mmap_name(machine))
114 goto out;
115
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300116 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300117 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +0300118 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300119 char comm[64];
120
121 if (thread == NULL)
Jiri Olsa81f981d2018-02-15 13:26:29 +0100122 goto out;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300123
124 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200125 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300126 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300127 }
128
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300129 machine->current_tid = NULL;
Jiri Olsa81f981d2018-02-15 13:26:29 +0100130 err = 0;
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300131
Jiri Olsa81f981d2018-02-15 13:26:29 +0100132out:
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100133 if (err) {
Jiri Olsa81f981d2018-02-15 13:26:29 +0100134 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100135 zfree(&machine->mmap_name);
136 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300137 return 0;
138}
139
David Ahern8fb598e2013-09-28 13:13:00 -0600140struct machine *machine__new_host(void)
141{
142 struct machine *machine = malloc(sizeof(*machine));
143
144 if (machine != NULL) {
145 machine__init(machine, "", HOST_KERNEL_ID);
146
147 if (machine__create_kernel_maps(machine) < 0)
148 goto out_delete;
149 }
150
151 return machine;
152out_delete:
153 free(machine);
154 return NULL;
155}
156
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300157struct machine *machine__new_kallsyms(void)
158{
159 struct machine *machine = machine__new_host();
160 /*
161 * FIXME:
Ingo Molnaradba1632018-12-03 11:22:00 +0100162 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300163 * ask for not using the kcore parsing code, once this one is fixed
164 * to create a map per module.
165 */
Arnaldo Carvalho de Melo329f0ad2018-04-25 11:40:32 -0300166 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300167 machine__delete(machine);
168 machine = NULL;
169 }
170
171 return machine;
172}
173
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300174static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300175{
176 struct dso *pos, *n;
177
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300178 down_write(&dsos->lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300179
Waiman Long8fa7d872014-09-29 16:07:28 -0400180 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -0400181 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +0200182 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300183 list_del_init(&pos->node);
184 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300185 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300186
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300187 up_write(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300188}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300189
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300190static void dsos__exit(struct dsos *dsos)
191{
192 dsos__purge(dsos);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300193 exit_rwsem(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300194}
195
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300196void machine__delete_threads(struct machine *machine)
197{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300198 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700199 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300200
Kan Liang91e467b2017-09-10 19:23:14 -0700201 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
202 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300203 down_write(&threads->lock);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800204 nd = rb_first_cached(&threads->entries);
Kan Liang91e467b2017-09-10 19:23:14 -0700205 while (nd) {
206 struct thread *t = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300207
Kan Liang91e467b2017-09-10 19:23:14 -0700208 nd = rb_next(nd);
209 __machine__remove_thread(machine, t, false);
210 }
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300211 up_write(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300212 }
213}
214
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300215void machine__exit(struct machine *machine)
216{
Kan Liang91e467b2017-09-10 19:23:14 -0700217 int i;
218
Arnaldo Carvalho de Melo19993b82017-11-13 16:06:29 -0300219 if (machine == NULL)
220 return;
221
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900222 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300223 maps__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300224 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300225 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300226 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100227 zfree(&machine->mmap_name);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300228 zfree(&machine->current_tid);
Kan Liang91e467b2017-09-10 19:23:14 -0700229
230 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
231 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -0300232 struct thread *thread, *n;
233 /*
234 * Forget about the dead, at this point whatever threads were
235 * left in the dead lists better have a reference count taken
236 * by who is using them, and then, when they drop those references
237 * and it finally hits zero, thread__put() will check and see that
238 * its not in the dead threads list and will not try to remove it
239 * from there, just calling thread__delete() straight away.
240 */
241 list_for_each_entry_safe(thread, n, &threads->dead, node)
242 list_del_init(&thread->node);
243
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300244 exit_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -0700245 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300246}
247
248void machine__delete(struct machine *machine)
249{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300250 if (machine) {
251 machine__exit(machine);
252 free(machine);
253 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300254}
255
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300256void machines__init(struct machines *machines)
257{
258 machine__init(&machines->host, "", HOST_KERNEL_ID);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800259 machines->guests = RB_ROOT_CACHED;
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300260}
261
262void machines__exit(struct machines *machines)
263{
264 machine__exit(&machines->host);
265 /* XXX exit guest */
266}
267
268struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300269 const char *root_dir)
270{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800271 struct rb_node **p = &machines->guests.rb_root.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300272 struct rb_node *parent = NULL;
273 struct machine *pos, *machine = malloc(sizeof(*machine));
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800274 bool leftmost = true;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300275
276 if (machine == NULL)
277 return NULL;
278
279 if (machine__init(machine, root_dir, pid) != 0) {
280 free(machine);
281 return NULL;
282 }
283
284 while (*p != NULL) {
285 parent = *p;
286 pos = rb_entry(parent, struct machine, rb_node);
287 if (pid < pos->pid)
288 p = &(*p)->rb_left;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800289 else {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300290 p = &(*p)->rb_right;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800291 leftmost = false;
292 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300293 }
294
295 rb_link_node(&machine->rb_node, parent, p);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800296 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300297
298 return machine;
299}
300
Adrian Huntercfe1c412014-07-31 09:00:45 +0300301void machines__set_comm_exec(struct machines *machines, bool comm_exec)
302{
303 struct rb_node *nd;
304
305 machines->host.comm_exec = comm_exec;
306
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800307 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntercfe1c412014-07-31 09:00:45 +0300308 struct machine *machine = rb_entry(nd, struct machine, rb_node);
309
310 machine->comm_exec = comm_exec;
311 }
312}
313
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300314struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300315{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800316 struct rb_node **p = &machines->guests.rb_root.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300317 struct rb_node *parent = NULL;
318 struct machine *machine;
319 struct machine *default_machine = NULL;
320
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300321 if (pid == HOST_KERNEL_ID)
322 return &machines->host;
323
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300324 while (*p != NULL) {
325 parent = *p;
326 machine = rb_entry(parent, struct machine, rb_node);
327 if (pid < machine->pid)
328 p = &(*p)->rb_left;
329 else if (pid > machine->pid)
330 p = &(*p)->rb_right;
331 else
332 return machine;
333 if (!machine->pid)
334 default_machine = machine;
335 }
336
337 return default_machine;
338}
339
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300340struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300341{
342 char path[PATH_MAX];
343 const char *root_dir = "";
344 struct machine *machine = machines__find(machines, pid);
345
346 if (machine && (machine->pid == pid))
347 goto out;
348
349 if ((pid != HOST_KERNEL_ID) &&
350 (pid != DEFAULT_GUEST_KERNEL_ID) &&
351 (symbol_conf.guestmount)) {
352 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
353 if (access(path, R_OK)) {
354 static struct strlist *seen;
355
356 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300357 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300358
359 if (!strlist__has_entry(seen, path)) {
360 pr_err("Can't access file %s\n", path);
361 strlist__add(seen, path);
362 }
363 machine = NULL;
364 goto out;
365 }
366 root_dir = path;
367 }
368
369 machine = machines__add(machines, pid, root_dir);
370out:
371 return machine;
372}
373
Adrian Hunterfcda5ff2021-02-18 11:57:55 +0200374struct machine *machines__find_guest(struct machines *machines, pid_t pid)
375{
376 struct machine *machine = machines__find(machines, pid);
377
378 if (!machine)
379 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
380 return machine;
381}
382
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300383void machines__process_guests(struct machines *machines,
384 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300385{
386 struct rb_node *nd;
387
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800388 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300389 struct machine *pos = rb_entry(nd, struct machine, rb_node);
390 process(pos, data);
391 }
392}
393
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300394void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300395{
396 struct rb_node *node;
397 struct machine *machine;
398
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300399 machines->host.id_hdr_size = id_hdr_size;
400
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800401 for (node = rb_first_cached(&machines->guests); node;
402 node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300403 machine = rb_entry(node, struct machine, rb_node);
404 machine->id_hdr_size = id_hdr_size;
405 }
406
407 return;
408}
409
Adrian Hunter29ce3612014-07-16 11:07:13 +0300410static void machine__update_thread_pid(struct machine *machine,
411 struct thread *th, pid_t pid)
412{
413 struct thread *leader;
414
415 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
416 return;
417
418 th->pid_ = pid;
419
420 if (th->pid_ == th->tid)
421 return;
422
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300423 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300424 if (!leader)
425 goto out_err;
426
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300427 if (!leader->maps)
428 leader->maps = maps__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300429
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300430 if (!leader->maps)
Adrian Hunter29ce3612014-07-16 11:07:13 +0300431 goto out_err;
432
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300433 if (th->maps == leader->maps)
Adrian Hunter29ce3612014-07-16 11:07:13 +0300434 return;
435
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300436 if (th->maps) {
Adrian Hunter29ce3612014-07-16 11:07:13 +0300437 /*
438 * Maps are created from MMAP events which provide the pid and
439 * tid. Consequently there never should be any maps on a thread
440 * with an unknown pid. Just print an error if there are.
441 */
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300442 if (!maps__empty(th->maps))
Adrian Hunter29ce3612014-07-16 11:07:13 +0300443 pr_err("Discarding thread maps for %d:%d\n",
444 th->pid_, th->tid);
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300445 maps__put(th->maps);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300446 }
447
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300448 th->maps = maps__get(leader->maps);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300449out_put:
450 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300451 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300452out_err:
453 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300454 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300455}
456
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300457/*
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200458 * Front-end cache - TID lookups come in blocks,
459 * so most of the time we dont have to look up
460 * the full rbtree:
461 */
462static struct thread*
Jiri Olsab57334b2018-07-19 16:33:44 +0200463__threads__get_last_match(struct threads *threads, struct machine *machine,
464 int pid, int tid)
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200465{
466 struct thread *th;
467
468 th = threads->last_match;
469 if (th != NULL) {
470 if (th->tid == tid) {
471 machine__update_thread_pid(machine, th, pid);
472 return thread__get(th);
473 }
474
475 threads->last_match = NULL;
476 }
477
478 return NULL;
479}
480
Jiri Olsab57334b2018-07-19 16:33:44 +0200481static struct thread*
482threads__get_last_match(struct threads *threads, struct machine *machine,
483 int pid, int tid)
484{
485 struct thread *th = NULL;
486
487 if (perf_singlethreaded)
488 th = __threads__get_last_match(threads, machine, pid, tid);
489
490 return th;
491}
492
493static void
494__threads__set_last_match(struct threads *threads, struct thread *th)
495{
496 threads->last_match = th;
497}
498
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200499static void
500threads__set_last_match(struct threads *threads, struct thread *th)
501{
Jiri Olsab57334b2018-07-19 16:33:44 +0200502 if (perf_singlethreaded)
503 __threads__set_last_match(threads, th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200504}
505
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200506/*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800507 * Caller must eventually drop thread->refcnt returned with a successful
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300508 * lookup/new thread inserted.
509 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300510static struct thread *____machine__findnew_thread(struct machine *machine,
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300511 struct threads *threads,
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300512 pid_t pid, pid_t tid,
513 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300514{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800515 struct rb_node **p = &threads->entries.rb_root.rb_node;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300516 struct rb_node *parent = NULL;
517 struct thread *th;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800518 bool leftmost = true;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300519
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200520 th = threads__get_last_match(threads, machine, pid, tid);
521 if (th)
522 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300523
524 while (*p != NULL) {
525 parent = *p;
526 th = rb_entry(parent, struct thread, rb_node);
527
Adrian Hunter38051232013-07-04 16:20:31 +0300528 if (th->tid == tid) {
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200529 threads__set_last_match(threads, th);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300530 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300531 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300532 }
533
Adrian Hunter38051232013-07-04 16:20:31 +0300534 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300535 p = &(*p)->rb_left;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800536 else {
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300537 p = &(*p)->rb_right;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800538 leftmost = false;
539 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300540 }
541
542 if (!create)
543 return NULL;
544
Adrian Hunter99d725f2013-08-26 16:00:19 +0300545 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300546 if (th != NULL) {
547 rb_link_node(&th->rb_node, parent, p);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800548 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
Jiri Olsacddcef62014-04-09 20:54:29 +0200549
550 /*
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300551 * We have to initialize maps separately after rb tree is updated.
Jiri Olsacddcef62014-04-09 20:54:29 +0200552 *
553 * The reason is that we call machine__findnew_thread
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300554 * within thread__init_maps to find the thread
Jiri Olsacddcef62014-04-09 20:54:29 +0200555 * leader and that would screwed the rb tree.
556 */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300557 if (thread__init_maps(th, machine)) {
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800558 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300559 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300560 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200561 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300562 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300563 /*
564 * It is now in the rbtree, get a ref
565 */
566 thread__get(th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200567 threads__set_last_match(threads, th);
Kan Liang91e467b2017-09-10 19:23:14 -0700568 ++threads->nr;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300569 }
570
571 return th;
572}
573
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300574struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
575{
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300576 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300577}
578
Adrian Hunter314add62013-08-27 11:23:03 +0300579struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
580 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300581{
Kan Liang91e467b2017-09-10 19:23:14 -0700582 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300583 struct thread *th;
584
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300585 down_write(&threads->lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300586 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300587 up_write(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300588 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300589}
590
Jiri Olsad75e6092014-03-14 15:00:03 +0100591struct thread *machine__find_thread(struct machine *machine, pid_t pid,
592 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300593{
Kan Liang91e467b2017-09-10 19:23:14 -0700594 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300595 struct thread *th;
Kan Liang91e467b2017-09-10 19:23:14 -0700596
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300597 down_read(&threads->lock);
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300598 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300599 up_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300600 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300601}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300602
Adrian Hunter3035cb62021-02-18 11:57:56 +0200603/*
604 * Threads are identified by pid and tid, and the idle task has pid == tid == 0.
605 * So here a single thread is created for that, but actually there is a separate
606 * idle task per cpu, so there should be one 'struct thread' per cpu, but there
607 * is only 1. That causes problems for some tools, requiring workarounds. For
608 * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu().
609 */
610struct thread *machine__idle_thread(struct machine *machine)
611{
612 struct thread *thread = machine__findnew_thread(machine, 0, 0);
613
614 if (!thread || thread__set_comm(thread, "swapper", 0) ||
615 thread__set_namespaces(thread, 0, NULL))
616 pr_err("problem inserting idle task for machine pid %d\n", machine->pid);
617
618 return thread;
619}
620
Adrian Huntercfe1c412014-07-31 09:00:45 +0300621struct comm *machine__thread_exec_comm(struct machine *machine,
622 struct thread *thread)
623{
624 if (machine->comm_exec)
625 return thread__exec_comm(thread);
626 else
627 return thread__comm(thread);
628}
629
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200630int machine__process_comm_event(struct machine *machine, union perf_event *event,
631 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300632{
Adrian Hunter314add62013-08-27 11:23:03 +0300633 struct thread *thread = machine__findnew_thread(machine,
634 event->comm.pid,
635 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300636 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300637 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300638
Adrian Huntercfe1c412014-07-31 09:00:45 +0300639 if (exec)
640 machine->comm_exec = true;
641
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300642 if (dump_trace)
643 perf_event__fprintf_comm(event, stdout);
644
Adrian Hunter65de51f2014-07-31 09:00:44 +0300645 if (thread == NULL ||
646 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300647 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300648 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300649 }
650
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300651 thread__put(thread);
652
653 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300654}
655
Hari Bathinif3b36142017-03-08 02:11:43 +0530656int machine__process_namespaces_event(struct machine *machine __maybe_unused,
657 union perf_event *event,
658 struct perf_sample *sample __maybe_unused)
659{
660 struct thread *thread = machine__findnew_thread(machine,
661 event->namespaces.pid,
662 event->namespaces.tid);
663 int err = 0;
664
665 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
666 "\nWARNING: kernel seems to support more namespaces than perf"
667 " tool.\nTry updating the perf tool..\n\n");
668
669 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
670 "\nWARNING: perf tool seems to support more namespaces than"
671 " the kernel.\nTry updating the kernel..\n\n");
672
673 if (dump_trace)
674 perf_event__fprintf_namespaces(event, stdout);
675
676 if (thread == NULL ||
677 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
678 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
679 err = -1;
680 }
681
682 thread__put(thread);
683
684 return err;
685}
686
Namhyung Kimd1277aa2020-03-25 21:45:31 +0900687int machine__process_cgroup_event(struct machine *machine,
Namhyung Kimba78c1c2020-03-25 21:45:30 +0900688 union perf_event *event,
689 struct perf_sample *sample __maybe_unused)
690{
Namhyung Kimd1277aa2020-03-25 21:45:31 +0900691 struct cgroup *cgrp;
692
Namhyung Kimba78c1c2020-03-25 21:45:30 +0900693 if (dump_trace)
694 perf_event__fprintf_cgroup(event, stdout);
695
Namhyung Kimd1277aa2020-03-25 21:45:31 +0900696 cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path);
697 if (cgrp == NULL)
698 return -ENOMEM;
699
Namhyung Kimba78c1c2020-03-25 21:45:30 +0900700 return 0;
701}
702
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300703int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200704 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300705{
Jiri Olsa5290ed62019-08-25 20:17:46 +0200706 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300707 event->lost.id, event->lost.lost);
708 return 0;
709}
710
Kan Liangc4937a92015-05-10 15:13:15 -0400711int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
712 union perf_event *event, struct perf_sample *sample)
713{
Jiri Olsaa2e254d2019-08-25 20:17:47 +0200714 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
Kan Liangc4937a92015-05-10 15:13:15 -0400715 sample->id, event->lost_samples.lost);
716 return 0;
717}
718
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300719static struct dso *machine__findnew_module_dso(struct machine *machine,
720 struct kmod_path *m,
721 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100722{
723 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100724
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300725 down_write(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300726
727 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100728 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300729 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100730 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300731 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100732
Namhyung Kim6b335e82017-05-31 21:01:04 +0900733 dso__set_module_info(dso, m, machine);
Jiri Olsaca333802015-02-17 17:29:57 +0100734 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsa1c695c82020-08-08 14:21:54 +0200735 dso->kernel = DSO_SPACE__KERNEL;
Jiri Olsada17ea32015-02-12 22:10:52 +0100736 }
737
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300738 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300739out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300740 up_write(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100741 return dso;
742}
743
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300744int machine__process_aux_event(struct machine *machine __maybe_unused,
745 union perf_event *event)
746{
747 if (dump_trace)
748 perf_event__fprintf_aux(event, stdout);
749 return 0;
750}
751
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300752int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
753 union perf_event *event)
754{
755 if (dump_trace)
756 perf_event__fprintf_itrace_start(event, stdout);
757 return 0;
758}
759
Adrian Hunter61750472021-09-07 19:39:02 +0300760int machine__process_aux_output_hw_id_event(struct machine *machine __maybe_unused,
761 union perf_event *event)
762{
763 if (dump_trace)
764 perf_event__fprintf_aux_output_hw_id(event, stdout);
765 return 0;
766}
767
Adrian Hunter02860392015-07-21 12:44:03 +0300768int machine__process_switch_event(struct machine *machine __maybe_unused,
769 union perf_event *event)
770{
771 if (dump_trace)
772 perf_event__fprintf_switch(event, stdout);
773 return 0;
774}
775
Song Liu9aa0bfa2019-01-17 08:15:17 -0800776static int machine__process_ksymbol_register(struct machine *machine,
777 union perf_event *event,
778 struct perf_sample *sample __maybe_unused)
779{
780 struct symbol *sym;
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300781 struct map *map = maps__find(&machine->kmaps, event->ksymbol.addr);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800782
Song Liu9aa0bfa2019-01-17 08:15:17 -0800783 if (!map) {
Jiri Olsa4a4eb612020-02-10 21:08:47 +0100784 struct dso *dso = dso__new(event->ksymbol.name);
785
786 if (dso) {
Jiri Olsa1c695c82020-08-08 14:21:54 +0200787 dso->kernel = DSO_SPACE__KERNEL;
Jiri Olsa4a4eb612020-02-10 21:08:47 +0100788 map = map__new2(0, dso);
Riccardo Mancinic087e942021-06-12 19:37:48 +0200789 dso__put(dso);
Jiri Olsa4a4eb612020-02-10 21:08:47 +0100790 }
791
792 if (!dso || !map) {
Song Liu9aa0bfa2019-01-17 08:15:17 -0800793 return -ENOMEM;
Jiri Olsa4a4eb612020-02-10 21:08:47 +0100794 }
Song Liu9aa0bfa2019-01-17 08:15:17 -0800795
Adrian Hunter789e2412020-05-12 15:19:19 +0300796 if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) {
797 map->dso->binary_type = DSO_BINARY_TYPE__OOL;
798 map->dso->data.file_size = event->ksymbol.len;
799 dso__set_loaded(map->dso);
800 }
801
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300802 map->start = event->ksymbol.addr;
803 map->end = map->start + event->ksymbol.len;
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300804 maps__insert(&machine->kmaps, map);
Riccardo Mancinic087e942021-06-12 19:37:48 +0200805 map__put(map);
Jiri Olsa7eddf7e2020-03-12 20:56:09 +0100806 dso__set_loaded(dso);
Jiri Olsa3c29d442020-03-12 20:56:10 +0100807
808 if (is_bpf_image(event->ksymbol.name)) {
809 dso->binary_type = DSO_BINARY_TYPE__BPF_IMAGE;
810 dso__set_long_name(dso, "", false);
811 }
Song Liu9aa0bfa2019-01-17 08:15:17 -0800812 }
813
Jiri Olsa8529f2e2019-05-08 15:20:04 +0200814 sym = symbol__new(map->map_ip(map, map->start),
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300815 event->ksymbol.len,
816 0, 0, event->ksymbol.name);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800817 if (!sym)
818 return -ENOMEM;
819 dso__insert_symbol(map->dso, sym);
820 return 0;
821}
822
823static int machine__process_ksymbol_unregister(struct machine *machine,
824 union perf_event *event,
825 struct perf_sample *sample __maybe_unused)
826{
Tommi Rantalaab8bf5f2020-10-16 14:47:18 +0300827 struct symbol *sym;
Song Liu9aa0bfa2019-01-17 08:15:17 -0800828 struct map *map;
829
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300830 map = maps__find(&machine->kmaps, event->ksymbol.addr);
Tommi Rantalaab8bf5f2020-10-16 14:47:18 +0300831 if (!map)
832 return 0;
833
834 if (map != machine->vmlinux_map)
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300835 maps__remove(&machine->kmaps, map);
Tommi Rantalaab8bf5f2020-10-16 14:47:18 +0300836 else {
837 sym = dso__find_symbol(map->dso, map->map_ip(map, map->start));
838 if (sym)
839 dso__delete_symbol(map->dso, sym);
840 }
Song Liu9aa0bfa2019-01-17 08:15:17 -0800841
842 return 0;
843}
844
845int machine__process_ksymbol(struct machine *machine __maybe_unused,
846 union perf_event *event,
847 struct perf_sample *sample)
848{
849 if (dump_trace)
850 perf_event__fprintf_ksymbol(event, stdout);
851
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300852 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
Song Liu9aa0bfa2019-01-17 08:15:17 -0800853 return machine__process_ksymbol_unregister(machine, event,
854 sample);
855 return machine__process_ksymbol_register(machine, event, sample);
856}
857
Adrian Hunter246eba82020-05-12 15:19:18 +0300858int machine__process_text_poke(struct machine *machine, union perf_event *event,
859 struct perf_sample *sample __maybe_unused)
860{
861 struct map *map = maps__find(&machine->kmaps, event->text_poke.addr);
862 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
863
864 if (dump_trace)
Adrian Hunter7eeb9852020-05-12 15:19:22 +0300865 perf_event__fprintf_text_poke(event, machine, stdout);
Adrian Hunter246eba82020-05-12 15:19:18 +0300866
867 if (!event->text_poke.new_len)
868 return 0;
869
870 if (cpumode != PERF_RECORD_MISC_KERNEL) {
871 pr_debug("%s: unsupported cpumode - ignoring\n", __func__);
872 return 0;
873 }
874
875 if (map && map->dso) {
876 u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len;
877 int ret;
878
879 /*
880 * Kernel maps might be changed when loading symbols so loading
881 * must be done prior to using kernel maps.
882 */
883 map__load(map);
884 ret = dso__data_write_cache_addr(map->dso, map, machine,
885 event->text_poke.addr,
886 new_bytes,
887 event->text_poke.new_len);
888 if (ret != event->text_poke.new_len)
889 pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n",
890 event->text_poke.addr);
891 } else {
892 pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n",
893 event->text_poke.addr);
894 }
895
896 return 0;
897}
898
Arnaldo Carvalho de Meloa94ab912019-11-14 12:28:41 -0300899static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
900 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300901{
Jiri Olsaca333802015-02-17 17:29:57 +0100902 struct map *map = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100903 struct kmod_path m;
Arnaldo Carvalho de Meloa94ab912019-11-14 12:28:41 -0300904 struct dso *dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300905
Jiri Olsaca333802015-02-17 17:29:57 +0100906 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300907 return NULL;
908
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300909 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100910 if (dso == NULL)
911 goto out;
912
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300913 map = map__new2(start, dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300914 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100915 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300916
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300917 maps__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100918
Ingo Molnar4d39c892021-03-23 17:09:15 +0100919 /* Put the map here because maps__insert already got it */
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900920 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100921out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900922 /* put the dso here, corresponding to machine__findnew_module_dso */
923 dso__put(dso);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300924 zfree(&m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300925 return map;
926}
927
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300928size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300929{
930 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300931 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300932
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800933 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300934 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300935 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300936 }
937
938 return ret;
939}
940
Waiman Long8fa7d872014-09-29 16:07:28 -0400941size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300942 bool (skip)(struct dso *dso, int parm), int parm)
943{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300944 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300945}
946
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300947size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300948 bool (skip)(struct dso *dso, int parm), int parm)
949{
950 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300951 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300952
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800953 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300954 struct machine *pos = rb_entry(nd, struct machine, rb_node);
955 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
956 }
957 return ret;
958}
959
960size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
961{
962 int i;
963 size_t printed = 0;
Arnaldo Carvalho de Melo93730f82019-10-31 15:22:24 -0300964 struct dso *kdso = machine__kernel_dso(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300965
966 if (kdso->has_build_id) {
967 char filename[PATH_MAX];
Krister Johansend2396992017-07-05 18:48:13 -0700968 if (dso__build_id_filename(kdso, filename, sizeof(filename),
969 false))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300970 printed += fprintf(fp, "[0] %s\n", filename);
971 }
972
973 for (i = 0; i < vmlinux_path__nr_entries; ++i)
974 printed += fprintf(fp, "[%d] %s\n",
975 i + kdso->has_build_id, vmlinux_path[i]);
976
977 return printed;
978}
979
980size_t machine__fprintf(struct machine *machine, FILE *fp)
981{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300982 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700983 size_t ret;
984 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300985
Kan Liang91e467b2017-09-10 19:23:14 -0700986 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
987 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300988
989 down_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300990
Kan Liang91e467b2017-09-10 19:23:14 -0700991 ret = fprintf(fp, "Threads: %u\n", threads->nr);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300992
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800993 for (nd = rb_first_cached(&threads->entries); nd;
994 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -0700995 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300996
Kan Liang91e467b2017-09-10 19:23:14 -0700997 ret += thread__fprintf(pos, fp);
998 }
999
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001000 up_read(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001001 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001002 return ret;
1003}
1004
1005static struct dso *machine__get_kernel(struct machine *machine)
1006{
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001007 const char *vmlinux_name = machine->mmap_name;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001008 struct dso *kernel;
1009
1010 if (machine__is_host(machine)) {
Jiri Olsac1925242018-03-12 16:24:06 +01001011 if (symbol_conf.vmlinux_name)
1012 vmlinux_name = symbol_conf.vmlinux_name;
1013
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -03001014 kernel = machine__findnew_kernel(machine, vmlinux_name,
Jiri Olsa1c695c82020-08-08 14:21:54 +02001015 "[kernel]", DSO_SPACE__KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001016 } else {
Jiri Olsac1925242018-03-12 16:24:06 +01001017 if (symbol_conf.default_guest_vmlinux_name)
1018 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1019
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -03001020 kernel = machine__findnew_kernel(machine, vmlinux_name,
1021 "[guest.kernel]",
Jiri Olsa1c695c82020-08-08 14:21:54 +02001022 DSO_SPACE__KERNEL_GUEST);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001023 }
1024
1025 if (kernel != NULL && (!kernel->has_build_id))
1026 dso__read_running_kernel_build_id(kernel, machine);
1027
1028 return kernel;
1029}
1030
1031struct process_args {
1032 u64 start;
1033};
1034
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001035void machine__get_kallsyms_filename(struct machine *machine, char *buf,
1036 size_t bufsz)
Adrian Hunter15a0a872014-01-29 16:14:38 +02001037{
1038 if (machine__is_default_guest(machine))
1039 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
1040 else
1041 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
1042}
1043
Simon Quea93f0e52014-06-16 11:32:09 -07001044const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
1045
1046/* Figure out the start address of kernel map from /proc/kallsyms.
1047 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
1048 * symbol_name if it's not that important.
1049 */
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001050static int machine__get_running_kernel_start(struct machine *machine,
Jiri Olsaed9adb22019-05-08 15:20:03 +02001051 const char **symbol_name,
1052 u64 *start, u64 *end)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001053{
Adrian Hunter15a0a872014-01-29 16:14:38 +02001054 char filename[PATH_MAX];
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001055 int i, err = -1;
Simon Quea93f0e52014-06-16 11:32:09 -07001056 const char *name;
1057 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001058
Adrian Hunter15a0a872014-01-29 16:14:38 +02001059 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001060
1061 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1062 return 0;
1063
Simon Quea93f0e52014-06-16 11:32:09 -07001064 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001065 err = kallsyms__get_function_start(filename, name, &addr);
1066 if (!err)
Simon Quea93f0e52014-06-16 11:32:09 -07001067 break;
1068 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001069
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001070 if (err)
1071 return -1;
1072
Simon Quea93f0e52014-06-16 11:32:09 -07001073 if (symbol_name)
1074 *symbol_name = name;
1075
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001076 *start = addr;
Jiri Olsaed9adb22019-05-08 15:20:03 +02001077
1078 err = kallsyms__get_function_start(filename, "_etext", &addr);
1079 if (!err)
1080 *end = addr;
1081
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001082 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001083}
1084
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001085int machine__create_extra_kernel_map(struct machine *machine,
1086 struct dso *kernel,
1087 struct extra_kernel_map *xm)
Adrian Hunter4d99e412018-05-22 13:54:33 +03001088{
1089 struct kmap *kmap;
1090 struct map *map;
1091
1092 map = map__new2(xm->start, kernel);
1093 if (!map)
1094 return -1;
1095
1096 map->end = xm->end;
1097 map->pgoff = xm->pgoff;
1098
1099 kmap = map__kmap(map);
1100
Adrian Hunter5759a682018-05-22 13:54:35 +03001101 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001102
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001103 maps__insert(&machine->kmaps, map);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001104
Adrian Hunter5759a682018-05-22 13:54:35 +03001105 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
1106 kmap->name, map->start, map->end);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001107
1108 map__put(map);
1109
1110 return 0;
1111}
1112
1113static u64 find_entry_trampoline(struct dso *dso)
1114{
1115 /* Duplicates are removed so lookup all aliases */
1116 const char *syms[] = {
1117 "_entry_trampoline",
1118 "__entry_trampoline_start",
1119 "entry_SYSCALL_64_trampoline",
1120 };
1121 struct symbol *sym = dso__first_symbol(dso);
1122 unsigned int i;
1123
1124 for (; sym; sym = dso__next_symbol(sym)) {
1125 if (sym->binding != STB_GLOBAL)
1126 continue;
1127 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1128 if (!strcmp(sym->name, syms[i]))
1129 return sym->start;
1130 }
1131 }
1132
1133 return 0;
1134}
1135
1136/*
1137 * These values can be used for kernels that do not have symbols for the entry
1138 * trampolines in kallsyms.
1139 */
1140#define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1141#define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1142#define X86_64_ENTRY_TRAMPOLINE 0x6000
1143
1144/* Map x86_64 PTI entry trampolines */
1145int machine__map_x86_64_entry_trampolines(struct machine *machine,
1146 struct dso *kernel)
1147{
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001148 struct maps *kmaps = &machine->kmaps;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001149 int nr_cpus_avail, cpu;
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001150 bool found = false;
1151 struct map *map;
1152 u64 pgoff;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001153
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001154 /*
1155 * In the vmlinux case, pgoff is a virtual address which must now be
1156 * mapped to a vmlinux offset.
1157 */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001158 maps__for_each_entry(kmaps, map) {
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001159 struct kmap *kmap = __map__kmap(map);
1160 struct map *dest_map;
1161
1162 if (!kmap || !is_entry_trampoline(kmap->name))
1163 continue;
1164
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001165 dest_map = maps__find(kmaps, map->pgoff);
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001166 if (dest_map != map)
1167 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1168 found = true;
1169 }
1170 if (found || machine->trampolines_mapped)
1171 return 0;
1172
1173 pgoff = find_entry_trampoline(kernel);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001174 if (!pgoff)
1175 return 0;
1176
1177 nr_cpus_avail = machine__nr_cpus_avail(machine);
1178
1179 /* Add a 1 page map for each CPU's entry trampoline */
1180 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1181 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1182 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1183 X86_64_ENTRY_TRAMPOLINE;
1184 struct extra_kernel_map xm = {
1185 .start = va,
1186 .end = va + page_size,
1187 .pgoff = pgoff,
1188 };
1189
Adrian Hunter5759a682018-05-22 13:54:35 +03001190 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1191
Adrian Hunter4d99e412018-05-22 13:54:33 +03001192 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1193 return -1;
1194 }
1195
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001196 machine->trampolines_mapped = nr_cpus_avail;
1197
1198 return 0;
1199}
1200
1201int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1202 struct dso *kernel __maybe_unused)
1203{
Adrian Hunter4d99e412018-05-22 13:54:33 +03001204 return 0;
1205}
1206
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001207static int
1208__machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001209{
Masami Hiramatsucc1121a2015-12-09 11:11:33 +09001210 /* In case of renewal the kernel map, destroy previous one */
1211 machine__destroy_kernel_maps(machine);
1212
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001213 machine->vmlinux_map = map__new2(0, kernel);
1214 if (machine->vmlinux_map == NULL)
1215 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001216
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001217 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
Jiri Olsa484214f2020-02-10 15:32:18 +01001218 maps__insert(&machine->kmaps, machine->vmlinux_map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001219 return 0;
1220}
1221
1222void machine__destroy_kernel_maps(struct machine *machine)
1223{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001224 struct kmap *kmap;
1225 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001226
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001227 if (map == NULL)
1228 return;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001229
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001230 kmap = map__kmap(map);
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001231 maps__remove(&machine->kmaps, map);
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001232 if (kmap && kmap->ref_reloc_sym) {
1233 zfree((char **)&kmap->ref_reloc_sym->name);
1234 zfree(&kmap->ref_reloc_sym);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001235 }
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001236
1237 map__zput(machine->vmlinux_map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001238}
1239
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001240int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001241{
1242 int ret = 0;
1243 struct dirent **namelist = NULL;
1244 int i, items = 0;
1245 char path[PATH_MAX];
1246 pid_t pid;
1247 char *endp;
1248
1249 if (symbol_conf.default_guest_vmlinux_name ||
1250 symbol_conf.default_guest_modules ||
1251 symbol_conf.default_guest_kallsyms) {
1252 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1253 }
1254
1255 if (symbol_conf.guestmount) {
1256 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1257 if (items <= 0)
1258 return -ENOENT;
1259 for (i = 0; i < items; i++) {
1260 if (!isdigit(namelist[i]->d_name[0])) {
1261 /* Filter out . and .. */
1262 continue;
1263 }
1264 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1265 if ((*endp != '\0') ||
1266 (endp == namelist[i]->d_name) ||
1267 (errno == ERANGE)) {
1268 pr_debug("invalid directory (%s). Skipping.\n",
1269 namelist[i]->d_name);
1270 continue;
1271 }
1272 sprintf(path, "%s/%s/proc/kallsyms",
1273 symbol_conf.guestmount,
1274 namelist[i]->d_name);
1275 ret = access(path, R_OK);
1276 if (ret) {
1277 pr_debug("Can't access file %s\n", path);
1278 goto failure;
1279 }
1280 machines__create_kernel_maps(machines, pid);
1281 }
1282failure:
1283 free(namelist);
1284 }
1285
1286 return ret;
1287}
1288
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001289void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001290{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001291 struct rb_node *next = rb_first_cached(&machines->guests);
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001292
1293 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001294
1295 while (next) {
1296 struct machine *pos = rb_entry(next, struct machine, rb_node);
1297
1298 next = rb_next(&pos->rb_node);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001299 rb_erase_cached(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001300 machine__delete(pos);
1301 }
1302}
1303
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001304int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001305{
1306 struct machine *machine = machines__findnew(machines, pid);
1307
1308 if (machine == NULL)
1309 return -1;
1310
1311 return machine__create_kernel_maps(machine);
1312}
1313
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001314int machine__load_kallsyms(struct machine *machine, const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001315{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001316 struct map *map = machine__kernel_map(machine);
Jiri Olsae8f38792018-02-15 13:26:33 +01001317 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001318
1319 if (ret > 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001320 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001321 /*
1322 * Since /proc/kallsyms will have multiple sessions for the
1323 * kernel, with modules between them, fixup the end of all
1324 * sections.
1325 */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001326 maps__fixup_end(&machine->kmaps);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001327 }
1328
1329 return ret;
1330}
1331
Arnaldo Carvalho de Melo1d1a2652018-04-25 12:18:11 -03001332int machine__load_vmlinux_path(struct machine *machine)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001333{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001334 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001335 int ret = dso__load_vmlinux_path(map->dso, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001336
Adrian Hunter39b12f782013-08-07 14:38:47 +03001337 if (ret > 0)
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001338 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001339
1340 return ret;
1341}
1342
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001343static char *get_kernel_version(const char *root_dir)
1344{
1345 char version[PATH_MAX];
1346 FILE *file;
1347 char *name, *tmp;
1348 const char *prefix = "Linux version ";
1349
1350 sprintf(version, "%s/proc/version", root_dir);
1351 file = fopen(version, "r");
1352 if (!file)
1353 return NULL;
1354
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001355 tmp = fgets(version, sizeof(version), file);
1356 fclose(file);
Donald Yandt34b65af2019-05-28 09:41:28 -04001357 if (!tmp)
1358 return NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001359
1360 name = strstr(version, prefix);
1361 if (!name)
1362 return NULL;
1363 name += strlen(prefix);
1364 tmp = strchr(name, ' ');
1365 if (tmp)
1366 *tmp = '\0';
1367
1368 return strdup(name);
1369}
1370
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001371static bool is_kmod_dso(struct dso *dso)
1372{
1373 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1374 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1375}
1376
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001377static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001378{
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001379 char *long_name;
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001380 struct map *map = maps__find_by_name(maps, m->name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001381
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001382 if (map == NULL)
1383 return 0;
1384
1385 long_name = strdup(path);
1386 if (long_name == NULL)
1387 return -ENOMEM;
1388
1389 dso__set_long_name(map->dso, long_name, true);
1390 dso__kernel_module_get_build_id(map->dso, "");
1391
1392 /*
1393 * Full name could reveal us kmod compression, so
1394 * we need to update the symtab_type if needed.
1395 */
Jiri Olsa2af52472018-08-17 11:48:07 +02001396 if (m->comp && is_kmod_dso(map->dso)) {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001397 map->dso->symtab_type++;
Jiri Olsa2af52472018-08-17 11:48:07 +02001398 map->dso->comp = m->comp;
1399 }
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001400
1401 return 0;
1402}
1403
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001404static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001405{
1406 struct dirent *dent;
1407 DIR *dir = opendir(dir_name);
1408 int ret = 0;
1409
1410 if (!dir) {
1411 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1412 return -1;
1413 }
1414
1415 while ((dent = readdir(dir)) != NULL) {
1416 char path[PATH_MAX];
1417 struct stat st;
1418
1419 /*sshfs might return bad dent->d_type, so we have to stat*/
Arnaldo Carvalho de Melo9d5f0c32022-01-18 09:02:43 -03001420 path__join(path, sizeof(path), dir_name, dent->d_name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001421 if (stat(path, &st))
1422 continue;
1423
1424 if (S_ISDIR(st.st_mode)) {
1425 if (!strcmp(dent->d_name, ".") ||
1426 !strcmp(dent->d_name, ".."))
1427 continue;
1428
Richard Yao61d42902014-04-26 13:17:55 -04001429 /* Do not follow top-level source and build symlinks */
1430 if (depth == 0) {
1431 if (!strcmp(dent->d_name, "source") ||
1432 !strcmp(dent->d_name, "build"))
1433 continue;
1434 }
1435
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001436 ret = maps__set_modules_path_dir(maps, path, depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001437 if (ret < 0)
1438 goto out;
1439 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001440 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001441
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001442 ret = kmod_path__parse_name(&m, dent->d_name);
1443 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001444 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001445
1446 if (m.kmod)
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001447 ret = maps__set_module_path(maps, path, &m);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001448
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001449 zfree(&m.name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001450
1451 if (ret)
1452 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001453 }
1454 }
1455
1456out:
1457 closedir(dir);
1458 return ret;
1459}
1460
1461static int machine__set_modules_path(struct machine *machine)
1462{
1463 char *version;
1464 char modules_path[PATH_MAX];
1465
1466 version = get_kernel_version(machine->root_dir);
1467 if (!version)
1468 return -1;
1469
Richard Yao61d42902014-04-26 13:17:55 -04001470 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001471 machine->root_dir, version);
1472 free(version);
1473
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001474 return maps__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001475}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001476int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
Thomas Richter12a6d292019-07-24 14:27:02 +02001477 u64 *size __maybe_unused,
Song Shan Gong203d8a42016-07-21 11:10:51 +08001478 const char *name __maybe_unused)
1479{
1480 return 0;
1481}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001482
Thomas Richter9ad46522017-08-03 15:49:02 +02001483static int machine__create_module(void *arg, const char *name, u64 start,
1484 u64 size)
Adrian Hunter316d70d2013-10-08 11:45:48 +03001485{
1486 struct machine *machine = arg;
1487 struct map *map;
1488
Thomas Richter12a6d292019-07-24 14:27:02 +02001489 if (arch__fix_module_text_start(&start, &size, name) < 0)
Song Shan Gong203d8a42016-07-21 11:10:51 +08001490 return -1;
1491
Arnaldo Carvalho de Meloa94ab912019-11-14 12:28:41 -03001492 map = machine__addnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001493 if (map == NULL)
1494 return -1;
Thomas Richter9ad46522017-08-03 15:49:02 +02001495 map->end = start + size;
Adrian Hunter316d70d2013-10-08 11:45:48 +03001496
1497 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1498
1499 return 0;
1500}
1501
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001502static int machine__create_modules(struct machine *machine)
1503{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001504 const char *modules;
1505 char path[PATH_MAX];
1506
Adrian Hunterf4be9042013-09-22 13:22:09 +03001507 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001508 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001509 } else {
1510 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001511 modules = path;
1512 }
1513
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001514 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001515 return -1;
1516
Adrian Hunter316d70d2013-10-08 11:45:48 +03001517 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001518 return -1;
1519
Adrian Hunter316d70d2013-10-08 11:45:48 +03001520 if (!machine__set_modules_path(machine))
1521 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001522
Adrian Hunter316d70d2013-10-08 11:45:48 +03001523 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001524
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001525 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001526}
1527
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001528static void machine__set_kernel_mmap(struct machine *machine,
1529 u64 start, u64 end)
1530{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001531 machine->vmlinux_map->start = start;
1532 machine->vmlinux_map->end = end;
1533 /*
1534 * Be a bit paranoid here, some perf.data file came with
1535 * a zero sized synthesized MMAP event for the kernel.
1536 */
1537 if (start == 0 && end == 0)
1538 machine->vmlinux_map->end = ~0ULL;
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001539}
1540
Wei Li977c7a62019-02-28 17:20:03 +08001541static void machine__update_kernel_mmap(struct machine *machine,
1542 u64 start, u64 end)
1543{
1544 struct map *map = machine__kernel_map(machine);
1545
1546 map__get(map);
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001547 maps__remove(&machine->kmaps, map);
Wei Li977c7a62019-02-28 17:20:03 +08001548
1549 machine__set_kernel_mmap(machine, start, end);
1550
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001551 maps__insert(&machine->kmaps, map);
Wei Li977c7a62019-02-28 17:20:03 +08001552 map__put(map);
1553}
1554
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001555int machine__create_kernel_maps(struct machine *machine)
1556{
1557 struct dso *kernel = machine__get_kernel(machine);
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001558 const char *name = NULL;
Namhyung Kimee05d21792018-02-19 19:05:45 +09001559 struct map *map;
Jiri Olsaed9adb22019-05-08 15:20:03 +02001560 u64 start = 0, end = ~0ULL;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001561 int ret;
1562
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001563 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001564 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001565
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001566 ret = __machine__create_kernel_maps(machine, kernel);
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001567 if (ret < 0)
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001568 goto out_put;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001569
1570 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1571 if (machine__is_host(machine))
1572 pr_debug("Problems creating module maps, "
1573 "continuing anyway...\n");
1574 else
1575 pr_debug("Problems creating module maps for guest %d, "
1576 "continuing anyway...\n", machine->pid);
1577 }
1578
Jiri Olsaed9adb22019-05-08 15:20:03 +02001579 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001580 if (name &&
Jiri Olsaed9adb22019-05-08 15:20:03 +02001581 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001582 machine__destroy_kernel_maps(machine);
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001583 ret = -1;
1584 goto out_put;
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001585 }
Namhyung Kimee05d21792018-02-19 19:05:45 +09001586
Wei Li977c7a62019-02-28 17:20:03 +08001587 /*
1588 * we have a real start address now, so re-order the kmaps
1589 * assume it's the last in the kmaps
1590 */
Jiri Olsaed9adb22019-05-08 15:20:03 +02001591 machine__update_kernel_mmap(machine, start, end);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001592 }
1593
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001594 if (machine__create_extra_kernel_maps(machine, kernel))
1595 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1596
Jiri Olsaed9adb22019-05-08 15:20:03 +02001597 if (end == ~0ULL) {
1598 /* update end address of the kernel map using adjacent module address */
1599 map = map__next(machine__kernel_map(machine));
1600 if (map)
1601 machine__set_kernel_mmap(machine, start, map->start);
1602 }
1603
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001604out_put:
1605 dso__put(kernel);
1606 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001607}
1608
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001609static bool machine__uses_kcore(struct machine *machine)
1610{
1611 struct dso *dso;
1612
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001613 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001614 if (dso__is_kcore(dso))
1615 return true;
1616 }
1617
1618 return false;
1619}
1620
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001621static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
Jiri Olsa031f1122020-11-26 18:00:12 +01001622 struct extra_kernel_map *xm)
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001623{
1624 return machine__is(machine, "x86_64") &&
Jiri Olsa031f1122020-11-26 18:00:12 +01001625 is_entry_trampoline(xm->name);
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001626}
1627
1628static int machine__process_extra_kernel_map(struct machine *machine,
Jiri Olsa031f1122020-11-26 18:00:12 +01001629 struct extra_kernel_map *xm)
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001630{
Arnaldo Carvalho de Melo93730f82019-10-31 15:22:24 -03001631 struct dso *kernel = machine__kernel_dso(machine);
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001632
1633 if (kernel == NULL)
1634 return -1;
1635
Jiri Olsa031f1122020-11-26 18:00:12 +01001636 return machine__create_extra_kernel_map(machine, kernel, xm);
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001637}
1638
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001639static int machine__process_kernel_mmap_event(struct machine *machine,
Jiri Olsa1ca6e802020-12-14 11:54:49 +01001640 struct extra_kernel_map *xm,
1641 struct build_id *bid)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001642{
1643 struct map *map;
Jiri Olsa1c695c82020-08-08 14:21:54 +02001644 enum dso_space_type dso_space;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001645 bool is_kernel_mmap;
1646
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001647 /* If we have maps from kcore then we do not need or want any others */
1648 if (machine__uses_kcore(machine))
1649 return 0;
1650
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001651 if (machine__is_host(machine))
Jiri Olsa1c695c82020-08-08 14:21:54 +02001652 dso_space = DSO_SPACE__KERNEL;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001653 else
Jiri Olsa1c695c82020-08-08 14:21:54 +02001654 dso_space = DSO_SPACE__KERNEL_GUEST;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001655
Jiri Olsa031f1122020-11-26 18:00:12 +01001656 is_kernel_mmap = memcmp(xm->name, machine->mmap_name,
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001657 strlen(machine->mmap_name) - 1) == 0;
Jiri Olsa031f1122020-11-26 18:00:12 +01001658 if (xm->name[0] == '/' ||
1659 (!is_kernel_mmap && xm->name[0] == '[')) {
1660 map = machine__addnew_module_map(machine, xm->start,
1661 xm->name);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001662 if (map == NULL)
1663 goto out_problem;
1664
Jiri Olsa031f1122020-11-26 18:00:12 +01001665 map->end = map->start + xm->end - xm->start;
Jiri Olsa1ca6e802020-12-14 11:54:49 +01001666
1667 if (build_id__is_defined(bid))
1668 dso__set_build_id(map->dso, bid);
1669
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001670 } else if (is_kernel_mmap) {
Jiri Olsa031f1122020-11-26 18:00:12 +01001671 const char *symbol_name = (xm->name + strlen(machine->mmap_name));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001672 /*
1673 * Should be there already, from the build-id table in
1674 * the header.
1675 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001676 struct dso *kernel = NULL;
1677 struct dso *dso;
1678
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001679 down_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001680
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001681 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001682
1683 /*
1684 * The cpumode passed to is_kernel_module is not the
1685 * cpumode of *this* event. If we insist on passing
1686 * correct cpumode to is_kernel_module, we should
1687 * record the cpumode when we adding this dso to the
1688 * linked list.
1689 *
1690 * However we don't really need passing correct
1691 * cpumode. We know the correct cpumode must be kernel
1692 * mode (if not, we should not link it onto kernel_dsos
1693 * list).
1694 *
1695 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1696 * is_kernel_module() treats it as a kernel cpumode.
1697 */
1698
1699 if (!dso->kernel ||
1700 is_kernel_module(dso->long_name,
1701 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001702 continue;
1703
Wang Nan1f121b02015-06-03 08:52:21 +00001704
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001705 kernel = dso;
1706 break;
1707 }
1708
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001709 up_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001710
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001711 if (kernel == NULL)
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001712 kernel = machine__findnew_dso(machine, machine->mmap_name);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001713 if (kernel == NULL)
1714 goto out_problem;
1715
Jiri Olsa1c695c82020-08-08 14:21:54 +02001716 kernel->kernel = dso_space;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001717 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1718 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001719 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001720 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001721
Namhyung Kim330dfa22014-11-18 13:30:28 +09001722 if (strstr(kernel->long_name, "vmlinux"))
1723 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001724
Jiri Olsa031f1122020-11-26 18:00:12 +01001725 machine__update_kernel_mmap(machine, xm->start, xm->end);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001726
Jiri Olsa1ca6e802020-12-14 11:54:49 +01001727 if (build_id__is_defined(bid))
1728 dso__set_build_id(kernel, bid);
1729
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001730 /*
1731 * Avoid using a zero address (kptr_restrict) for the ref reloc
1732 * symbol. Effectively having zero here means that at record
1733 * time /proc/sys/kernel/kptr_restrict was non zero.
1734 */
Jiri Olsa031f1122020-11-26 18:00:12 +01001735 if (xm->pgoff != 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001736 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1737 symbol_name,
Jiri Olsa031f1122020-11-26 18:00:12 +01001738 xm->pgoff);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001739 }
1740
1741 if (machine__is_default_guest(machine)) {
1742 /*
1743 * preload dso of guest kernel and modules
1744 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001745 dso__load(kernel, machine__kernel_map(machine));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001746 }
Jiri Olsa031f1122020-11-26 18:00:12 +01001747 } else if (perf_event__is_extra_kernel_mmap(machine, xm)) {
1748 return machine__process_extra_kernel_map(machine, xm);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001749 }
1750 return 0;
1751out_problem:
1752 return -1;
1753}
1754
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001755int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001756 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001757 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001758{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001759 struct thread *thread;
1760 struct map *map;
Arnaldo Carvalho de Melo4a7380a2019-11-19 12:40:29 -03001761 struct dso_id dso_id = {
1762 .maj = event->mmap2.maj,
1763 .min = event->mmap2.min,
1764 .ino = event->mmap2.ino,
1765 .ino_generation = event->mmap2.ino_generation,
1766 };
Jiri Olsa1ca6e802020-12-14 11:54:49 +01001767 struct build_id __bid, *bid = NULL;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001768 int ret = 0;
1769
1770 if (dump_trace)
1771 perf_event__fprintf_mmap2(event, stdout);
1772
Jiri Olsa1ca6e802020-12-14 11:54:49 +01001773 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
1774 bid = &__bid;
1775 build_id__init(bid, event->mmap2.build_id, event->mmap2.build_id_size);
1776 }
1777
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001778 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1779 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Jiri Olsa031f1122020-11-26 18:00:12 +01001780 struct extra_kernel_map xm = {
1781 .start = event->mmap2.start,
1782 .end = event->mmap2.start + event->mmap2.len,
1783 .pgoff = event->mmap2.pgoff,
1784 };
1785
1786 strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN);
Jiri Olsa1ca6e802020-12-14 11:54:49 +01001787 ret = machine__process_kernel_mmap_event(machine, &xm, bid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001788 if (ret < 0)
1789 goto out_problem;
1790 return 0;
1791 }
1792
1793 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001794 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001795 if (thread == NULL)
1796 goto out_problem;
1797
Adrian Hunter2a030682014-07-22 16:17:53 +03001798 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001799 event->mmap2.len, event->mmap2.pgoff,
Arnaldo Carvalho de Melo4a7380a2019-11-19 12:40:29 -03001800 &dso_id, event->mmap2.prot,
Jiri Olsa1ca6e802020-12-14 11:54:49 +01001801 event->mmap2.flags, bid,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001802 event->mmap2.filename, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001803
1804 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001805 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001806
He Kuang8132a2a2016-06-03 03:33:13 +00001807 ret = thread__insert_map(thread, map);
1808 if (ret)
1809 goto out_problem_insert;
1810
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001811 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001812 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001813 return 0;
1814
He Kuang8132a2a2016-06-03 03:33:13 +00001815out_problem_insert:
1816 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001817out_problem_map:
1818 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001819out_problem:
1820 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1821 return 0;
1822}
1823
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001824int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001825 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001826{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001827 struct thread *thread;
1828 struct map *map;
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001829 u32 prot = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001830 int ret = 0;
1831
1832 if (dump_trace)
1833 perf_event__fprintf_mmap(event, stdout);
1834
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001835 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1836 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Jiri Olsa031f1122020-11-26 18:00:12 +01001837 struct extra_kernel_map xm = {
1838 .start = event->mmap.start,
1839 .end = event->mmap.start + event->mmap.len,
1840 .pgoff = event->mmap.pgoff,
1841 };
1842
1843 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
Jiri Olsa1ca6e802020-12-14 11:54:49 +01001844 ret = machine__process_kernel_mmap_event(machine, &xm, NULL);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001845 if (ret < 0)
1846 goto out_problem;
1847 return 0;
1848 }
1849
Adrian Hunter314add62013-08-27 11:23:03 +03001850 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001851 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001852 if (thread == NULL)
1853 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001854
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001855 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001856 prot = PROT_EXEC;
Stephane Eranianbad40912013-01-24 16:10:40 +01001857
Adrian Hunter2a030682014-07-22 16:17:53 +03001858 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001859 event->mmap.len, event->mmap.pgoff,
Jiri Olsa1ca6e802020-12-14 11:54:49 +01001860 NULL, prot, 0, NULL, event->mmap.filename, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001861
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001862 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001863 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001864
He Kuang8132a2a2016-06-03 03:33:13 +00001865 ret = thread__insert_map(thread, map);
1866 if (ret)
1867 goto out_problem_insert;
1868
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001869 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001870 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001871 return 0;
1872
He Kuang8132a2a2016-06-03 03:33:13 +00001873out_problem_insert:
1874 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001875out_problem_map:
1876 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001877out_problem:
1878 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1879 return 0;
1880}
1881
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001882static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001883{
Kan Liang91e467b2017-09-10 19:23:14 -07001884 struct threads *threads = machine__threads(machine, th->tid);
1885
1886 if (threads->last_match == th)
Jiri Olsa67fda0f2018-07-19 16:33:43 +02001887 threads__set_last_match(threads, NULL);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001888
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001889 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001890 down_write(&threads->lock);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001891
1892 BUG_ON(refcount_read(&th->refcnt) == 0);
1893
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001894 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001895 RB_CLEAR_NODE(&th->rb_node);
Kan Liang91e467b2017-09-10 19:23:14 -07001896 --threads->nr;
David Ahern236a3bb2013-08-14 08:49:27 -06001897 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001898 * Move it first to the dead_threads list, then drop the reference,
1899 * if this is the last reference, then the thread__delete destructor
1900 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001901 */
Kan Liang91e467b2017-09-10 19:23:14 -07001902 list_add_tail(&th->node, &threads->dead);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001903
1904 /*
1905 * We need to do the put here because if this is the last refcount,
1906 * then we will be touching the threads->dead head when removing the
1907 * thread.
1908 */
1909 thread__put(th);
1910
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001911 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001912 up_write(&threads->lock);
David Ahern236a3bb2013-08-14 08:49:27 -06001913}
1914
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001915void machine__remove_thread(struct machine *machine, struct thread *th)
1916{
1917 return __machine__remove_thread(machine, th, true);
1918}
1919
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001920int machine__process_fork_event(struct machine *machine, union perf_event *event,
1921 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001922{
Jiri Olsad75e6092014-03-14 15:00:03 +01001923 struct thread *thread = machine__find_thread(machine,
1924 event->fork.pid,
1925 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001926 struct thread *parent = machine__findnew_thread(machine,
1927 event->fork.ppid,
1928 event->fork.ptid);
David Miller4f8f3822018-10-30 22:24:04 -07001929 bool do_maps_clone = true;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001930 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001931
Adrian Hunter5cb73342015-08-19 17:29:20 +03001932 if (dump_trace)
1933 perf_event__fprintf_task(event, stdout);
1934
1935 /*
1936 * There may be an existing thread that is not actually the parent,
1937 * either because we are processing events out of order, or because the
1938 * (fork) event that would have removed the thread was lost. Assume the
1939 * latter case and continue on as best we can.
1940 */
1941 if (parent->pid_ != (pid_t)event->fork.ppid) {
1942 dump_printf("removing erroneous parent thread %d/%d\n",
1943 parent->pid_, parent->tid);
1944 machine__remove_thread(machine, parent);
1945 thread__put(parent);
1946 parent = machine__findnew_thread(machine, event->fork.ppid,
1947 event->fork.ptid);
1948 }
1949
David Ahern236a3bb2013-08-14 08:49:27 -06001950 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001951 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001952 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001953 thread__put(thread);
1954 }
David Ahern236a3bb2013-08-14 08:49:27 -06001955
Adrian Hunter314add62013-08-27 11:23:03 +03001956 thread = machine__findnew_thread(machine, event->fork.pid,
1957 event->fork.tid);
David Miller4f8f3822018-10-30 22:24:04 -07001958 /*
1959 * When synthesizing FORK events, we are trying to create thread
1960 * objects for the already running tasks on the machine.
1961 *
1962 * Normally, for a kernel FORK event, we want to clone the parent's
1963 * maps because that is what the kernel just did.
1964 *
1965 * But when synthesizing, this should not be done. If we do, we end up
Ingo Molnar4d39c892021-03-23 17:09:15 +01001966 * with overlapping maps as we process the synthesized MMAP2 events that
David Miller4f8f3822018-10-30 22:24:04 -07001967 * get delivered shortly thereafter.
1968 *
1969 * Use the FORK event misc flags in an internal way to signal this
1970 * situation, so we can elide the map clone when appropriate.
1971 */
1972 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1973 do_maps_clone = false;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001974
1975 if (thread == NULL || parent == NULL ||
David Miller4f8f3822018-10-30 22:24:04 -07001976 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001977 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001978 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001979 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001980 thread__put(thread);
1981 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001982
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001983 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001984}
1985
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001986int machine__process_exit_event(struct machine *machine, union perf_event *event,
1987 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001988{
Jiri Olsad75e6092014-03-14 15:00:03 +01001989 struct thread *thread = machine__find_thread(machine,
1990 event->fork.pid,
1991 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001992
1993 if (dump_trace)
1994 perf_event__fprintf_task(event, stdout);
1995
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001996 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001997 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001998 thread__put(thread);
1999 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002000
2001 return 0;
2002}
2003
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02002004int machine__process_event(struct machine *machine, union perf_event *event,
2005 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002006{
2007 int ret;
2008
2009 switch (event->header.type) {
2010 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02002011 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002012 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02002013 ret = machine__process_mmap_event(machine, event, sample); break;
Hari Bathinif3b36142017-03-08 02:11:43 +05302014 case PERF_RECORD_NAMESPACES:
2015 ret = machine__process_namespaces_event(machine, event, sample); break;
Namhyung Kimba78c1c2020-03-25 21:45:30 +09002016 case PERF_RECORD_CGROUP:
2017 ret = machine__process_cgroup_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02002018 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02002019 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002020 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02002021 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002022 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02002023 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002024 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02002025 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03002026 case PERF_RECORD_AUX:
2027 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03002028 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02002029 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04002030 case PERF_RECORD_LOST_SAMPLES:
2031 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03002032 case PERF_RECORD_SWITCH:
2033 case PERF_RECORD_SWITCH_CPU_WIDE:
2034 ret = machine__process_switch_event(machine, event); break;
Song Liu9aa0bfa2019-01-17 08:15:17 -08002035 case PERF_RECORD_KSYMBOL:
2036 ret = machine__process_ksymbol(machine, event, sample); break;
Song Liu45178a92019-01-17 08:15:18 -08002037 case PERF_RECORD_BPF_EVENT:
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03002038 ret = machine__process_bpf(machine, event, sample); break;
Adrian Hunter246eba82020-05-12 15:19:18 +03002039 case PERF_RECORD_TEXT_POKE:
2040 ret = machine__process_text_poke(machine, event, sample); break;
Adrian Hunter61750472021-09-07 19:39:02 +03002041 case PERF_RECORD_AUX_OUTPUT_HW_ID:
2042 ret = machine__process_aux_output_hw_id_event(machine, event); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002043 default:
2044 ret = -1;
2045 break;
2046 }
2047
2048 return ret;
2049}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002050
Greg Priceb21484f2012-12-06 21:48:05 -08002051static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002052{
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03002053 if (!regexec(regex, sym->name, 0, NULL, 0))
Jiapeng Chong83ff0f92021-03-09 18:11:09 +08002054 return true;
2055 return false;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002056}
2057
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002058static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002059 struct addr_map_symbol *ams,
2060 u64 ip)
2061{
2062 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002063
2064 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03002065 /*
2066 * We cannot use the header.misc hint to determine whether a
2067 * branch stack address is user, kernel, guest, hypervisor.
2068 * Branches may straddle the kernel/user/hypervisor boundaries.
2069 * Thus, we have to try consecutively until we find a match
2070 * or else, the symbol is unknown
2071 */
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03002072 thread__find_cpumode_addr_location(thread, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002073
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002074 ams->addr = ip;
2075 ams->al_addr = al.addr;
German Gomez05b5a9d2022-01-26 10:59:26 +00002076 ams->al_level = al.level;
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -03002077 ams->ms.maps = al.maps;
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03002078 ams->ms.sym = al.sym;
2079 ams->ms.map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04002080 ams->phys_addr = 0;
Kan Lianga50d03e2020-12-16 10:57:58 -08002081 ams->data_page_size = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002082}
2083
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002084static void ip__resolve_data(struct thread *thread,
Kan Liang8780fb22017-08-29 13:11:09 -04002085 u8 m, struct addr_map_symbol *ams,
Kan Lianga50d03e2020-12-16 10:57:58 -08002086 u64 addr, u64 phys_addr, u64 daddr_page_size)
Stephane Eranian98a3b322013-01-24 16:10:35 +01002087{
2088 struct addr_location al;
2089
2090 memset(&al, 0, sizeof(al));
2091
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03002092 thread__find_symbol(thread, m, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04002093
Stephane Eranian98a3b322013-01-24 16:10:35 +01002094 ams->addr = addr;
2095 ams->al_addr = al.addr;
German Gomez05b5a9d2022-01-26 10:59:26 +00002096 ams->al_level = al.level;
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -03002097 ams->ms.maps = al.maps;
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03002098 ams->ms.sym = al.sym;
2099 ams->ms.map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04002100 ams->phys_addr = phys_addr;
Kan Lianga50d03e2020-12-16 10:57:58 -08002101 ams->data_page_size = daddr_page_size;
Stephane Eranian98a3b322013-01-24 16:10:35 +01002102}
2103
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03002104struct mem_info *sample__resolve_mem(struct perf_sample *sample,
2105 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01002106{
Jiri Olsa9f874982018-03-07 16:50:06 +01002107 struct mem_info *mi = mem_info__new();
Stephane Eranian98a3b322013-01-24 16:10:35 +01002108
2109 if (!mi)
2110 return NULL;
2111
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002112 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
Kan Liang8780fb22017-08-29 13:11:09 -04002113 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
Kan Lianga50d03e2020-12-16 10:57:58 -08002114 sample->addr, sample->phys_addr,
2115 sample->data_page_size);
Stephane Eranian98a3b322013-01-24 16:10:35 +01002116 mi->data_src.val = sample->data_src;
2117
2118 return mi;
2119}
2120
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002121static char *callchain_srcline(struct map_symbol *ms, u64 ip)
Milian Wolff40a342c2017-10-09 22:32:56 +02002122{
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002123 struct map *map = ms->map;
Milian Wolff21ac9d52017-10-19 13:38:34 +02002124 char *srcline = NULL;
Milian Wolff40a342c2017-10-09 22:32:56 +02002125
Milian Wolff21ac9d52017-10-19 13:38:34 +02002126 if (!map || callchain_param.key == CCKEY_FUNCTION)
2127 return srcline;
2128
2129 srcline = srcline__tree_find(&map->dso->srclines, ip);
2130 if (!srcline) {
2131 bool show_sym = false;
2132 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2133
2134 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002135 ms->sym, show_sym, show_addr, ip);
Milian Wolff21ac9d52017-10-19 13:38:34 +02002136 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2137 }
2138
2139 return srcline;
Milian Wolff40a342c2017-10-09 22:32:56 +02002140}
2141
Jin Yaoc4ee0622017-08-07 21:05:15 +08002142struct iterations {
2143 int nr_loop_iter;
2144 u64 cycles;
2145};
2146
Andi Kleen37592b82014-11-12 18:05:19 -08002147static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002148 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08002149 struct symbol **parent,
2150 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002151 u8 *cpumode,
Jin Yao410024d2016-10-31 09:19:49 +08002152 u64 ip,
2153 bool branch,
2154 struct branch_flags *flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002155 struct iterations *iter,
Jin Yaob851dd42017-07-18 20:13:15 +08002156 u64 branch_from)
Andi Kleen37592b82014-11-12 18:05:19 -08002157{
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002158 struct map_symbol ms;
Andi Kleen37592b82014-11-12 18:05:19 -08002159 struct addr_location al;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002160 int nr_loop_iter = 0;
2161 u64 iter_cycles = 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002162 const char *srcline = NULL;
Andi Kleen37592b82014-11-12 18:05:19 -08002163
2164 al.filtered = 0;
2165 al.sym = NULL;
Michael Petlan57f0ff02021-07-19 16:53:32 +02002166 al.srcline = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002167 if (!cpumode) {
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03002168 thread__find_cpumode_addr_location(thread, ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002169 } else {
Kan Liang2e777842014-12-02 10:06:53 -05002170 if (ip >= PERF_CONTEXT_MAX) {
2171 switch (ip) {
2172 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002173 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05002174 break;
2175 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002176 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05002177 break;
2178 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002179 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05002180 break;
2181 default:
2182 pr_debug("invalid callchain context: "
2183 "%"PRId64"\n", (s64) ip);
2184 /*
2185 * It seems the callchain is corrupted.
2186 * Discard all.
2187 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002188 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05002189 return 1;
2190 }
2191 return 0;
2192 }
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002193 thread__find_symbol(thread, *cpumode, ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05002194 }
2195
Andi Kleen37592b82014-11-12 18:05:19 -08002196 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02002197 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08002198 symbol__match_regex(al.sym, &parent_regex))
2199 *parent = al.sym;
2200 else if (have_ignore_callees && root_al &&
2201 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2202 /* Treat this symbol as the root,
2203 forgetting its callees. */
2204 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002205 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08002206 }
2207 }
2208
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002209 if (symbol_conf.hide_unresolved && al.sym == NULL)
2210 return 0;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002211
2212 if (iter) {
2213 nr_loop_iter = iter->nr_loop_iter;
2214 iter_cycles = iter->cycles;
2215 }
2216
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -03002217 ms.maps = al.maps;
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002218 ms.map = al.map;
2219 ms.sym = al.sym;
2220 srcline = callchain_srcline(&ms, al.addr);
2221 return callchain_cursor_append(cursor, ip, &ms,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002222 branch, flags, nr_loop_iter,
Milian Wolff40a342c2017-10-09 22:32:56 +02002223 iter_cycles, branch_from, srcline);
Andi Kleen37592b82014-11-12 18:05:19 -08002224}
2225
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002226struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2227 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002228{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002229 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002230 const struct branch_stack *bs = sample->branch_stack;
Kan Liang42bbabe2020-02-28 08:30:00 -08002231 struct branch_entry *entries = perf_sample__branch_entries(sample);
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002232 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002233
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002234 if (!bi)
2235 return NULL;
2236
2237 for (i = 0; i < bs->nr; i++) {
Kan Liang42bbabe2020-02-28 08:30:00 -08002238 ip__resolve_ams(al->thread, &bi[i].to, entries[i].to);
2239 ip__resolve_ams(al->thread, &bi[i].from, entries[i].from);
2240 bi[i].flags = entries[i].flags;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002241 }
2242 return bi;
2243}
2244
Jin Yaoc4ee0622017-08-07 21:05:15 +08002245static void save_iterations(struct iterations *iter,
2246 struct branch_entry *be, int nr)
2247{
2248 int i;
2249
Jin Yaoa3366db02019-01-04 14:10:30 +08002250 iter->nr_loop_iter++;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002251 iter->cycles = 0;
2252
2253 for (i = 0; i < nr; i++)
2254 iter->cycles += be[i].flags.cycles;
2255}
2256
Andi Kleen8b7bad52014-11-12 18:05:20 -08002257#define CHASHSZ 127
2258#define CHASHBITS 7
2259#define NO_ENTRY 0xff
2260
2261#define PERF_MAX_BRANCH_DEPTH 127
2262
2263/* Remove loops. */
Jin Yaoc4ee0622017-08-07 21:05:15 +08002264static int remove_loops(struct branch_entry *l, int nr,
2265 struct iterations *iter)
Andi Kleen8b7bad52014-11-12 18:05:20 -08002266{
2267 int i, j, off;
2268 unsigned char chash[CHASHSZ];
2269
2270 memset(chash, NO_ENTRY, sizeof(chash));
2271
2272 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2273
2274 for (i = 0; i < nr; i++) {
2275 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2276
2277 /* no collision handling for now */
2278 if (chash[h] == NO_ENTRY) {
2279 chash[h] = i;
2280 } else if (l[chash[h]].from == l[i].from) {
2281 bool is_loop = true;
2282 /* check if it is a real loop */
2283 off = 0;
2284 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2285 if (l[j].from != l[i + off].from) {
2286 is_loop = false;
2287 break;
2288 }
2289 if (is_loop) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002290 j = nr - (i + off);
2291 if (j > 0) {
2292 save_iterations(iter + i + off,
2293 l + i, off);
2294
2295 memmove(iter + i, iter + i + off,
2296 j * sizeof(*iter));
2297
2298 memmove(l + i, l + i + off,
2299 j * sizeof(*l));
2300 }
2301
Andi Kleen8b7bad52014-11-12 18:05:20 -08002302 nr -= off;
2303 }
2304 }
2305 }
2306 return nr;
2307}
2308
Kan Liangdd3e2492020-03-19 13:25:07 -07002309static int lbr_callchain_add_kernel_ip(struct thread *thread,
2310 struct callchain_cursor *cursor,
2311 struct perf_sample *sample,
2312 struct symbol **parent,
2313 struct addr_location *root_al,
2314 u64 branch_from,
2315 bool callee, int end)
2316{
2317 struct ip_callchain *chain = sample->callchain;
2318 u8 cpumode = PERF_RECORD_MISC_USER;
2319 int err, i;
2320
2321 if (callee) {
2322 for (i = 0; i < end + 1; i++) {
2323 err = add_callchain_ip(thread, cursor, parent,
2324 root_al, &cpumode, chain->ips[i],
2325 false, NULL, NULL, branch_from);
2326 if (err)
2327 return err;
2328 }
2329 return 0;
2330 }
2331
2332 for (i = end; i >= 0; i--) {
2333 err = add_callchain_ip(thread, cursor, parent,
2334 root_al, &cpumode, chain->ips[i],
2335 false, NULL, NULL, branch_from);
2336 if (err)
2337 return err;
2338 }
2339
2340 return 0;
2341}
2342
Kan Liang7f1d3932020-03-19 13:25:11 -07002343static void save_lbr_cursor_node(struct thread *thread,
2344 struct callchain_cursor *cursor,
2345 int idx)
2346{
2347 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2348
2349 if (!lbr_stitch)
2350 return;
2351
2352 if (cursor->pos == cursor->nr) {
2353 lbr_stitch->prev_lbr_cursor[idx].valid = false;
2354 return;
2355 }
2356
2357 if (!cursor->curr)
2358 cursor->curr = cursor->first;
2359 else
2360 cursor->curr = cursor->curr->next;
2361 memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr,
2362 sizeof(struct callchain_cursor_node));
2363
2364 lbr_stitch->prev_lbr_cursor[idx].valid = true;
2365 cursor->pos++;
2366}
2367
Kan Liange2b234832020-03-19 13:25:08 -07002368static int lbr_callchain_add_lbr_ip(struct thread *thread,
2369 struct callchain_cursor *cursor,
2370 struct perf_sample *sample,
2371 struct symbol **parent,
2372 struct addr_location *root_al,
2373 u64 *branch_from,
2374 bool callee)
2375{
2376 struct branch_stack *lbr_stack = sample->branch_stack;
2377 struct branch_entry *entries = perf_sample__branch_entries(sample);
2378 u8 cpumode = PERF_RECORD_MISC_USER;
2379 int lbr_nr = lbr_stack->nr;
2380 struct branch_flags *flags;
2381 int err, i;
2382 u64 ip;
2383
Kan Liang7f1d3932020-03-19 13:25:11 -07002384 /*
2385 * The curr and pos are not used in writing session. They are cleared
2386 * in callchain_cursor_commit() when the writing session is closed.
2387 * Using curr and pos to track the current cursor node.
2388 */
2389 if (thread->lbr_stitch) {
2390 cursor->curr = NULL;
2391 cursor->pos = cursor->nr;
2392 if (cursor->nr) {
2393 cursor->curr = cursor->first;
2394 for (i = 0; i < (int)(cursor->nr - 1); i++)
2395 cursor->curr = cursor->curr->next;
2396 }
2397 }
2398
Kan Liange2b234832020-03-19 13:25:08 -07002399 if (callee) {
2400 /* Add LBR ip from first entries.to */
2401 ip = entries[0].to;
2402 flags = &entries[0].flags;
2403 *branch_from = entries[0].from;
2404 err = add_callchain_ip(thread, cursor, parent,
2405 root_al, &cpumode, ip,
2406 true, flags, NULL,
2407 *branch_from);
2408 if (err)
2409 return err;
2410
Kan Liang7f1d3932020-03-19 13:25:11 -07002411 /*
2412 * The number of cursor node increases.
2413 * Move the current cursor node.
2414 * But does not need to save current cursor node for entry 0.
2415 * It's impossible to stitch the whole LBRs of previous sample.
2416 */
2417 if (thread->lbr_stitch && (cursor->pos != cursor->nr)) {
2418 if (!cursor->curr)
2419 cursor->curr = cursor->first;
2420 else
2421 cursor->curr = cursor->curr->next;
2422 cursor->pos++;
2423 }
2424
Kan Liange2b234832020-03-19 13:25:08 -07002425 /* Add LBR ip from entries.from one by one. */
2426 for (i = 0; i < lbr_nr; i++) {
2427 ip = entries[i].from;
2428 flags = &entries[i].flags;
2429 err = add_callchain_ip(thread, cursor, parent,
2430 root_al, &cpumode, ip,
2431 true, flags, NULL,
2432 *branch_from);
2433 if (err)
2434 return err;
Kan Liang7f1d3932020-03-19 13:25:11 -07002435 save_lbr_cursor_node(thread, cursor, i);
Kan Liange2b234832020-03-19 13:25:08 -07002436 }
2437 return 0;
2438 }
2439
2440 /* Add LBR ip from entries.from one by one. */
2441 for (i = lbr_nr - 1; i >= 0; i--) {
2442 ip = entries[i].from;
2443 flags = &entries[i].flags;
2444 err = add_callchain_ip(thread, cursor, parent,
2445 root_al, &cpumode, ip,
2446 true, flags, NULL,
2447 *branch_from);
2448 if (err)
2449 return err;
Kan Liang7f1d3932020-03-19 13:25:11 -07002450 save_lbr_cursor_node(thread, cursor, i);
Kan Liange2b234832020-03-19 13:25:08 -07002451 }
2452
2453 /* Add LBR ip from first entries.to */
2454 ip = entries[0].to;
2455 flags = &entries[0].flags;
2456 *branch_from = entries[0].from;
2457 err = add_callchain_ip(thread, cursor, parent,
2458 root_al, &cpumode, ip,
2459 true, flags, NULL,
2460 *branch_from);
2461 if (err)
2462 return err;
2463
2464 return 0;
2465}
2466
Kan Liangff165622020-03-19 13:25:12 -07002467static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread,
2468 struct callchain_cursor *cursor)
2469{
2470 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2471 struct callchain_cursor_node *cnode;
2472 struct stitch_list *stitch_node;
2473 int err;
2474
2475 list_for_each_entry(stitch_node, &lbr_stitch->lists, node) {
2476 cnode = &stitch_node->cursor;
2477
2478 err = callchain_cursor_append(cursor, cnode->ip,
2479 &cnode->ms,
2480 cnode->branch,
2481 &cnode->branch_flags,
2482 cnode->nr_loop_iter,
2483 cnode->iter_cycles,
2484 cnode->branch_from,
2485 cnode->srcline);
2486 if (err)
2487 return err;
2488 }
2489 return 0;
2490}
2491
2492static struct stitch_list *get_stitch_node(struct thread *thread)
2493{
2494 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2495 struct stitch_list *stitch_node;
2496
2497 if (!list_empty(&lbr_stitch->free_lists)) {
2498 stitch_node = list_first_entry(&lbr_stitch->free_lists,
2499 struct stitch_list, node);
2500 list_del(&stitch_node->node);
2501
2502 return stitch_node;
2503 }
2504
2505 return malloc(sizeof(struct stitch_list));
2506}
2507
2508static bool has_stitched_lbr(struct thread *thread,
2509 struct perf_sample *cur,
2510 struct perf_sample *prev,
2511 unsigned int max_lbr,
2512 bool callee)
2513{
2514 struct branch_stack *cur_stack = cur->branch_stack;
2515 struct branch_entry *cur_entries = perf_sample__branch_entries(cur);
2516 struct branch_stack *prev_stack = prev->branch_stack;
2517 struct branch_entry *prev_entries = perf_sample__branch_entries(prev);
2518 struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2519 int i, j, nr_identical_branches = 0;
2520 struct stitch_list *stitch_node;
2521 u64 cur_base, distance;
2522
2523 if (!cur_stack || !prev_stack)
2524 return false;
2525
2526 /* Find the physical index of the base-of-stack for current sample. */
2527 cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1;
2528
2529 distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) :
2530 (max_lbr + prev_stack->hw_idx - cur_base);
2531 /* Previous sample has shorter stack. Nothing can be stitched. */
2532 if (distance + 1 > prev_stack->nr)
2533 return false;
2534
2535 /*
2536 * Check if there are identical LBRs between two samples.
Ingo Molnar4d39c892021-03-23 17:09:15 +01002537 * Identical LBRs must have same from, to and flags values. Also,
Kan Liangff165622020-03-19 13:25:12 -07002538 * they have to be saved in the same LBR registers (same physical
2539 * index).
2540 *
2541 * Starts from the base-of-stack of current sample.
2542 */
2543 for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) {
2544 if ((prev_entries[i].from != cur_entries[j].from) ||
2545 (prev_entries[i].to != cur_entries[j].to) ||
2546 (prev_entries[i].flags.value != cur_entries[j].flags.value))
2547 break;
2548 nr_identical_branches++;
2549 }
2550
2551 if (!nr_identical_branches)
2552 return false;
2553
2554 /*
2555 * Save the LBRs between the base-of-stack of previous sample
2556 * and the base-of-stack of current sample into lbr_stitch->lists.
2557 * These LBRs will be stitched later.
2558 */
2559 for (i = prev_stack->nr - 1; i > (int)distance; i--) {
2560
2561 if (!lbr_stitch->prev_lbr_cursor[i].valid)
2562 continue;
2563
2564 stitch_node = get_stitch_node(thread);
2565 if (!stitch_node)
2566 return false;
2567
2568 memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i],
2569 sizeof(struct callchain_cursor_node));
2570
2571 if (callee)
2572 list_add(&stitch_node->node, &lbr_stitch->lists);
2573 else
2574 list_add_tail(&stitch_node->node, &lbr_stitch->lists);
2575 }
2576
2577 return true;
2578}
2579
Kan Liang7f1d3932020-03-19 13:25:11 -07002580static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
Kan Liang9c6c3f42020-03-19 13:25:10 -07002581{
2582 if (thread->lbr_stitch)
2583 return true;
2584
2585 thread->lbr_stitch = zalloc(sizeof(*thread->lbr_stitch));
2586 if (!thread->lbr_stitch)
2587 goto err;
2588
Kan Liang7f1d3932020-03-19 13:25:11 -07002589 thread->lbr_stitch->prev_lbr_cursor = calloc(max_lbr + 1, sizeof(struct callchain_cursor_node));
2590 if (!thread->lbr_stitch->prev_lbr_cursor)
2591 goto free_lbr_stitch;
2592
Kan Liangff165622020-03-19 13:25:12 -07002593 INIT_LIST_HEAD(&thread->lbr_stitch->lists);
2594 INIT_LIST_HEAD(&thread->lbr_stitch->free_lists);
2595
Kan Liang7f1d3932020-03-19 13:25:11 -07002596 return true;
2597
2598free_lbr_stitch:
2599 zfree(&thread->lbr_stitch);
Kan Liang9c6c3f42020-03-19 13:25:10 -07002600err:
2601 pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n");
2602 thread->lbr_stitch_enable = false;
2603 return false;
2604}
2605
Kan Liang384b6052015-01-05 13:23:05 -05002606/*
Ingo Molnar4d39c892021-03-23 17:09:15 +01002607 * Resolve LBR callstack chain sample
Kan Liang384b6052015-01-05 13:23:05 -05002608 * Return:
2609 * 1 on success get LBR callchain information
2610 * 0 no available LBR callchain information, should try fp
2611 * negative error code on other errors.
2612 */
2613static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002614 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05002615 struct perf_sample *sample,
2616 struct symbol **parent,
2617 struct addr_location *root_al,
Kan Liang7f1d3932020-03-19 13:25:11 -07002618 int max_stack,
2619 unsigned int max_lbr)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002620{
Kan Liangff165622020-03-19 13:25:12 -07002621 bool callee = (callchain_param.order == ORDER_CALLEE);
Kan Liang384b6052015-01-05 13:23:05 -05002622 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002623 int chain_nr = min(max_stack, (int)chain->nr), i;
Kan Liang9c6c3f42020-03-19 13:25:10 -07002624 struct lbr_stitch *lbr_stitch;
Kan Liangff165622020-03-19 13:25:12 -07002625 bool stitched_lbr = false;
Kan Liange2b234832020-03-19 13:25:08 -07002626 u64 branch_from = 0;
Kan Liange48b8312020-03-19 13:25:06 -07002627 int err;
Kan Liang384b6052015-01-05 13:23:05 -05002628
2629 for (i = 0; i < chain_nr; i++) {
2630 if (chain->ips[i] == PERF_CONTEXT_USER)
2631 break;
2632 }
2633
2634 /* LBR only affects the user callchain */
Kan Liangf8603262020-03-19 13:25:05 -07002635 if (i == chain_nr)
2636 return 0;
Kan Liang384b6052015-01-05 13:23:05 -05002637
Kan Liang9c6c3f42020-03-19 13:25:10 -07002638 if (thread->lbr_stitch_enable && !sample->no_hw_idx &&
Kan Liang7f1d3932020-03-19 13:25:11 -07002639 (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) {
Kan Liang9c6c3f42020-03-19 13:25:10 -07002640 lbr_stitch = thread->lbr_stitch;
2641
Kan Liangff165622020-03-19 13:25:12 -07002642 stitched_lbr = has_stitched_lbr(thread, sample,
2643 &lbr_stitch->prev_sample,
2644 max_lbr, callee);
2645
2646 if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) {
2647 list_replace_init(&lbr_stitch->lists,
2648 &lbr_stitch->free_lists);
2649 }
Kan Liang9c6c3f42020-03-19 13:25:10 -07002650 memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample));
2651 }
2652
Kan Liangff165622020-03-19 13:25:12 -07002653 if (callee) {
Kan Liange48b8312020-03-19 13:25:06 -07002654 /* Add kernel ip */
Kan Liangdd3e2492020-03-19 13:25:07 -07002655 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2656 parent, root_al, branch_from,
2657 true, i);
2658 if (err)
2659 goto error;
2660
Kan Liange2b234832020-03-19 13:25:08 -07002661 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2662 root_al, &branch_from, true);
Kan Liangf8603262020-03-19 13:25:05 -07002663 if (err)
Kan Liange48b8312020-03-19 13:25:06 -07002664 goto error;
2665
Kan Liangff165622020-03-19 13:25:12 -07002666 if (stitched_lbr) {
2667 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2668 if (err)
2669 goto error;
2670 }
2671
Kan Liange48b8312020-03-19 13:25:06 -07002672 } else {
Kan Liangff165622020-03-19 13:25:12 -07002673 if (stitched_lbr) {
2674 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2675 if (err)
2676 goto error;
2677 }
Kan Liange2b234832020-03-19 13:25:08 -07002678 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2679 root_al, &branch_from, false);
Kan Liange48b8312020-03-19 13:25:06 -07002680 if (err)
2681 goto error;
2682
2683 /* Add kernel ip */
Kan Liangdd3e2492020-03-19 13:25:07 -07002684 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2685 parent, root_al, branch_from,
2686 false, i);
2687 if (err)
2688 goto error;
Kan Liangf8603262020-03-19 13:25:05 -07002689 }
2690 return 1;
Kan Liange48b8312020-03-19 13:25:06 -07002691
2692error:
2693 return (err < 0) ? err : 0;
Kan Liang384b6052015-01-05 13:23:05 -05002694}
2695
David S. Millere9024d52018-10-30 12:12:26 -03002696static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2697 struct callchain_cursor *cursor,
2698 struct symbol **parent,
2699 struct addr_location *root_al,
2700 u8 *cpumode, int ent)
2701{
2702 int err = 0;
2703
2704 while (--ent >= 0) {
2705 u64 ip = chain->ips[ent];
2706
2707 if (ip >= PERF_CONTEXT_MAX) {
2708 err = add_callchain_ip(thread, cursor, parent,
2709 root_al, cpumode, ip,
2710 false, NULL, NULL, 0);
2711 break;
2712 }
2713 }
2714 return err;
2715}
2716
Alexandre Truongb9f6fbb2021-12-17 15:45:20 +00002717static u64 get_leaf_frame_caller(struct perf_sample *sample,
2718 struct thread *thread, int usr_idx)
Alexandre Truong32bfa5b2021-12-17 15:45:16 +00002719{
Alexandre Truongb9f6fbb2021-12-17 15:45:20 +00002720 if (machine__normalized_is(thread->maps->machine, "arm64"))
2721 return get_leaf_frame_caller_aarch64(sample, thread, usr_idx);
2722 else
2723 return 0;
Alexandre Truong32bfa5b2021-12-17 15:45:16 +00002724}
2725
Kan Liang384b6052015-01-05 13:23:05 -05002726static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002727 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002728 struct evsel *evsel,
Kan Liang384b6052015-01-05 13:23:05 -05002729 struct perf_sample *sample,
2730 struct symbol **parent,
2731 struct addr_location *root_al,
2732 int max_stack)
2733{
2734 struct branch_stack *branch = sample->branch_stack;
Kan Liang42bbabe2020-02-28 08:30:00 -08002735 struct branch_entry *entries = perf_sample__branch_entries(sample);
Kan Liang384b6052015-01-05 13:23:05 -05002736 struct ip_callchain *chain = sample->callchain;
Jin Yaob49a8212017-05-08 18:43:02 +08002737 int chain_nr = 0;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002738 u8 cpumode = PERF_RECORD_MISC_USER;
Alexandre Truong32bfa5b2021-12-17 15:45:16 +00002739 int i, j, err, nr_entries, usr_idx;
Andi Kleen8b7bad52014-11-12 18:05:20 -08002740 int skip_idx = -1;
2741 int first_call = 0;
Alexandre Truong32bfa5b2021-12-17 15:45:16 +00002742 u64 leaf_frame_caller;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002743
Jin Yaob49a8212017-05-08 18:43:02 +08002744 if (chain)
2745 chain_nr = chain->nr;
2746
Arnaldo Carvalho de Melo4f138a92020-04-30 11:19:45 -03002747 if (evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo6e6d1d62020-05-04 13:44:03 -03002748 struct perf_env *env = evsel__env(evsel);
Kan Liang7f1d3932020-03-19 13:25:11 -07002749
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002750 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang7f1d3932020-03-19 13:25:11 -07002751 root_al, max_stack,
2752 !env ? 0 : env->max_branches);
Kan Liang384b6052015-01-05 13:23:05 -05002753 if (err)
2754 return (err < 0) ? err : 0;
2755 }
2756
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002757 /*
2758 * Based on DWARF debug information, some architectures skip
2759 * a callchain entry saved by the kernel.
2760 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002761 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002762
Andi Kleen8b7bad52014-11-12 18:05:20 -08002763 /*
2764 * Add branches to call stack for easier browsing. This gives
2765 * more context for a sample than just the callers.
2766 *
2767 * This uses individual histograms of paths compared to the
2768 * aggregated histograms the normal LBR mode uses.
2769 *
2770 * Limitations for now:
2771 * - No extra filters
2772 * - No annotations (should annotate somehow)
2773 */
2774
2775 if (branch && callchain_param.branch_callstack) {
2776 int nr = min(max_stack, (int)branch->nr);
2777 struct branch_entry be[nr];
Jin Yaoc4ee0622017-08-07 21:05:15 +08002778 struct iterations iter[nr];
Andi Kleen8b7bad52014-11-12 18:05:20 -08002779
2780 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2781 pr_warning("corrupted branch chain. skipping...\n");
2782 goto check_calls;
2783 }
2784
2785 for (i = 0; i < nr; i++) {
2786 if (callchain_param.order == ORDER_CALLEE) {
Kan Liang42bbabe2020-02-28 08:30:00 -08002787 be[i] = entries[i];
Jin Yaob49a8212017-05-08 18:43:02 +08002788
2789 if (chain == NULL)
2790 continue;
2791
Andi Kleen8b7bad52014-11-12 18:05:20 -08002792 /*
2793 * Check for overlap into the callchain.
2794 * The return address is one off compared to
2795 * the branch entry. To adjust for this
2796 * assume the calling instruction is not longer
2797 * than 8 bytes.
2798 */
2799 if (i == skip_idx ||
2800 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2801 first_call++;
2802 else if (be[i].from < chain->ips[first_call] &&
2803 be[i].from >= chain->ips[first_call] - 8)
2804 first_call++;
2805 } else
Kan Liang42bbabe2020-02-28 08:30:00 -08002806 be[i] = entries[branch->nr - i - 1];
Andi Kleen8b7bad52014-11-12 18:05:20 -08002807 }
2808
Jin Yaoc4ee0622017-08-07 21:05:15 +08002809 memset(iter, 0, sizeof(struct iterations) * nr);
2810 nr = remove_loops(be, nr, iter);
Jin Yao410024d2016-10-31 09:19:49 +08002811
Andi Kleen8b7bad52014-11-12 18:05:20 -08002812 for (i = 0; i < nr; i++) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002813 err = add_callchain_ip(thread, cursor, parent,
2814 root_al,
2815 NULL, be[i].to,
2816 true, &be[i].flags,
2817 NULL, be[i].from);
Jin Yao410024d2016-10-31 09:19:49 +08002818
Andi Kleen8b7bad52014-11-12 18:05:20 -08002819 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002820 err = add_callchain_ip(thread, cursor, parent, root_al,
Jin Yao410024d2016-10-31 09:19:49 +08002821 NULL, be[i].from,
2822 true, &be[i].flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002823 &iter[i], 0);
Andi Kleen8b7bad52014-11-12 18:05:20 -08002824 if (err == -EINVAL)
2825 break;
2826 if (err)
2827 return err;
2828 }
Jin Yaob49a8212017-05-08 18:43:02 +08002829
2830 if (chain_nr == 0)
2831 return 0;
2832
Andi Kleen8b7bad52014-11-12 18:05:20 -08002833 chain_nr -= nr;
2834 }
2835
2836check_calls:
Adrian Hunteraceb9822019-11-14 16:25:38 +02002837 if (chain && callchain_param.order != ORDER_CALLEE) {
David S. Millere9024d52018-10-30 12:12:26 -03002838 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2839 &cpumode, chain->nr - first_call);
2840 if (err)
2841 return (err < 0) ? err : 0;
2842 }
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002843 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002844 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002845 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002846
2847 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002848 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002849 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002850 j = chain->nr - i - 1;
2851
2852#ifdef HAVE_SKIP_CALLCHAIN_IDX
2853 if (j == skip_idx)
2854 continue;
2855#endif
2856 ip = chain->ips[j];
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002857 if (ip < PERF_CONTEXT_MAX)
2858 ++nr_entries;
David S. Millere9024d52018-10-30 12:12:26 -03002859 else if (callchain_param.order != ORDER_CALLEE) {
2860 err = find_prev_cpumode(chain, thread, cursor, parent,
2861 root_al, &cpumode, j);
2862 if (err)
2863 return (err < 0) ? err : 0;
2864 continue;
2865 }
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002866
Alexandre Truong32bfa5b2021-12-17 15:45:16 +00002867 /*
2868 * PERF_CONTEXT_USER allows us to locate where the user stack ends.
2869 * Depending on callchain_param.order and the position of PERF_CONTEXT_USER,
2870 * the index will be different in order to add the missing frame
2871 * at the right place.
2872 */
2873
2874 usr_idx = callchain_param.order == ORDER_CALLEE ? j-2 : j-1;
2875
2876 if (usr_idx >= 0 && chain->ips[usr_idx] == PERF_CONTEXT_USER) {
2877
2878 leaf_frame_caller = get_leaf_frame_caller(sample, thread, usr_idx);
2879
2880 /*
2881 * check if leaf_frame_Caller != ip to not add the same
2882 * value twice.
2883 */
2884
2885 if (leaf_frame_caller && leaf_frame_caller != ip) {
2886
2887 err = add_callchain_ip(thread, cursor, parent,
2888 root_al, &cpumode, leaf_frame_caller,
2889 false, NULL, NULL, 0);
2890 if (err)
2891 return (err < 0) ? err : 0;
2892 }
2893 }
2894
Jin Yao410024d2016-10-31 09:19:49 +08002895 err = add_callchain_ip(thread, cursor, parent,
2896 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002897 false, NULL, NULL, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002898
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002899 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05002900 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002901 }
2902
2903 return 0;
2904}
2905
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002906static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
Milian Wolff11ea2512017-10-09 22:32:59 +02002907{
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002908 struct symbol *sym = ms->sym;
2909 struct map *map = ms->map;
Milian Wolff11ea2512017-10-09 22:32:59 +02002910 struct inline_node *inline_node;
2911 struct inline_list *ilist;
2912 u64 addr;
Milian Wolffb38775c2017-10-19 13:38:33 +02002913 int ret = 1;
Milian Wolff11ea2512017-10-09 22:32:59 +02002914
2915 if (!symbol_conf.inline_name || !map || !sym)
Milian Wolffb38775c2017-10-19 13:38:33 +02002916 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002917
Milian Wolff7a8a8fc2018-09-26 15:52:06 +02002918 addr = map__map_ip(map, ip);
2919 addr = map__rip_2objdump(map, addr);
Milian Wolff11ea2512017-10-09 22:32:59 +02002920
2921 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2922 if (!inline_node) {
2923 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2924 if (!inline_node)
Milian Wolffb38775c2017-10-19 13:38:33 +02002925 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002926 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2927 }
2928
2929 list_for_each_entry(ilist, &inline_node->val, list) {
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002930 struct map_symbol ilist_ms = {
Arnaldo Carvalho de Melo77b91c12019-11-29 15:47:51 -03002931 .maps = ms->maps,
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002932 .map = map,
2933 .sym = ilist->symbol,
2934 };
2935 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
Milian Wolffb38775c2017-10-19 13:38:33 +02002936 NULL, 0, 0, 0, ilist->srcline);
Milian Wolff11ea2512017-10-09 22:32:59 +02002937
2938 if (ret != 0)
2939 return ret;
2940 }
2941
Milian Wolffb38775c2017-10-19 13:38:33 +02002942 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002943}
2944
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002945static int unwind_entry(struct unwind_entry *entry, void *arg)
2946{
2947 struct callchain_cursor *cursor = arg;
Milian Wolff40a342c2017-10-09 22:32:56 +02002948 const char *srcline = NULL;
Milian Wolffff4ce282018-09-26 15:52:05 +02002949 u64 addr = entry->ip;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002950
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002951 if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002952 return 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002953
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002954 if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
Milian Wolff11ea2512017-10-09 22:32:59 +02002955 return 0;
2956
Sandipan Das2a9d5052018-07-03 17:35:55 +05302957 /*
2958 * Convert entry->ip from a virtual address to an offset in
2959 * its corresponding binary.
2960 */
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002961 if (entry->ms.map)
2962 addr = map__map_ip(entry->ms.map, entry->ip);
Sandipan Das2a9d5052018-07-03 17:35:55 +05302963
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002964 srcline = callchain_srcline(&entry->ms, addr);
2965 return callchain_cursor_append(cursor, entry->ip, &entry->ms,
Milian Wolff40a342c2017-10-09 22:32:56 +02002966 false, NULL, 0, 0, 0, srcline);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002967}
2968
Chris Phlipot9919a652016-04-28 01:19:06 -07002969static int thread__resolve_callchain_unwind(struct thread *thread,
2970 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002971 struct evsel *evsel,
Chris Phlipot9919a652016-04-28 01:19:06 -07002972 struct perf_sample *sample,
2973 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002974{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002975 /* Can we do dwarf post unwind? */
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002976 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2977 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002978 return 0;
2979
2980 /* Bail out if nothing was captured. */
2981 if ((!sample->user_regs.regs) ||
2982 (!sample->user_stack.size))
2983 return 0;
2984
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002985 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01002986 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07002987}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002988
Chris Phlipot9919a652016-04-28 01:19:06 -07002989int thread__resolve_callchain(struct thread *thread,
2990 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002991 struct evsel *evsel,
Chris Phlipot9919a652016-04-28 01:19:06 -07002992 struct perf_sample *sample,
2993 struct symbol **parent,
2994 struct addr_location *root_al,
2995 int max_stack)
2996{
2997 int ret = 0;
2998
Jiri Olsa914eb9c2017-08-06 16:39:39 +02002999 callchain_cursor_reset(cursor);
Chris Phlipot9919a652016-04-28 01:19:06 -07003000
3001 if (callchain_param.order == ORDER_CALLEE) {
3002 ret = thread__resolve_callchain_sample(thread, cursor,
3003 evsel, sample,
3004 parent, root_al,
3005 max_stack);
3006 if (ret)
3007 return ret;
3008 ret = thread__resolve_callchain_unwind(thread, cursor,
3009 evsel, sample,
3010 max_stack);
3011 } else {
3012 ret = thread__resolve_callchain_unwind(thread, cursor,
3013 evsel, sample,
3014 max_stack);
3015 if (ret)
3016 return ret;
3017 ret = thread__resolve_callchain_sample(thread, cursor,
3018 evsel, sample,
3019 parent, root_al,
3020 max_stack);
3021 }
3022
3023 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03003024}
David Ahern35feee12013-09-28 13:12:58 -06003025
3026int machine__for_each_thread(struct machine *machine,
3027 int (*fn)(struct thread *thread, void *p),
3028 void *priv)
3029{
Kan Liang91e467b2017-09-10 19:23:14 -07003030 struct threads *threads;
David Ahern35feee12013-09-28 13:12:58 -06003031 struct rb_node *nd;
3032 struct thread *thread;
3033 int rc = 0;
Kan Liang91e467b2017-09-10 19:23:14 -07003034 int i;
David Ahern35feee12013-09-28 13:12:58 -06003035
Kan Liang91e467b2017-09-10 19:23:14 -07003036 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3037 threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08003038 for (nd = rb_first_cached(&threads->entries); nd;
3039 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -07003040 thread = rb_entry(nd, struct thread, rb_node);
3041 rc = fn(thread, priv);
3042 if (rc != 0)
3043 return rc;
3044 }
David Ahern35feee12013-09-28 13:12:58 -06003045
Kan Liang91e467b2017-09-10 19:23:14 -07003046 list_for_each_entry(thread, &threads->dead, node) {
3047 rc = fn(thread, priv);
3048 if (rc != 0)
3049 return rc;
3050 }
David Ahern35feee12013-09-28 13:12:58 -06003051 }
3052 return rc;
3053}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03003054
Adrian Huntera5499b32015-05-29 16:33:30 +03003055int machines__for_each_thread(struct machines *machines,
3056 int (*fn)(struct thread *thread, void *p),
3057 void *priv)
3058{
3059 struct rb_node *nd;
3060 int rc = 0;
3061
3062 rc = machine__for_each_thread(&machines->host, fn, priv);
3063 if (rc != 0)
3064 return rc;
3065
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08003066 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntera5499b32015-05-29 16:33:30 +03003067 struct machine *machine = rb_entry(nd, struct machine, rb_node);
3068
3069 rc = machine__for_each_thread(machine, fn, priv);
3070 if (rc != 0)
3071 return rc;
3072 }
3073 return rc;
3074}
3075
Adrian Hunterb9d266b2014-07-22 16:17:25 +03003076pid_t machine__get_current_tid(struct machine *machine, int cpu)
3077{
Adrian Hunter5501e922021-01-07 19:41:59 +02003078 int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
Kyle Meyer8c727462019-08-27 16:43:50 -05003079
3080 if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
Adrian Hunterb9d266b2014-07-22 16:17:25 +03003081 return -1;
3082
3083 return machine->current_tid[cpu];
3084}
3085
3086int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
3087 pid_t tid)
3088{
3089 struct thread *thread;
Adrian Hunter5501e922021-01-07 19:41:59 +02003090 int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03003091
3092 if (cpu < 0)
3093 return -EINVAL;
3094
3095 if (!machine->current_tid) {
3096 int i;
3097
Kyle Meyer8c727462019-08-27 16:43:50 -05003098 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
Adrian Hunterb9d266b2014-07-22 16:17:25 +03003099 if (!machine->current_tid)
3100 return -ENOMEM;
Kyle Meyer8c727462019-08-27 16:43:50 -05003101 for (i = 0; i < nr_cpus; i++)
Adrian Hunterb9d266b2014-07-22 16:17:25 +03003102 machine->current_tid[i] = -1;
3103 }
3104
Kyle Meyer8c727462019-08-27 16:43:50 -05003105 if (cpu >= nr_cpus) {
Adrian Hunterb9d266b2014-07-22 16:17:25 +03003106 pr_err("Requested CPU %d too large. ", cpu);
3107 pr_err("Consider raising MAX_NR_CPUS\n");
3108 return -EINVAL;
3109 }
3110
3111 machine->current_tid[cpu] = tid;
3112
3113 thread = machine__findnew_thread(machine, pid, tid);
3114 if (!thread)
3115 return -ENOMEM;
3116
3117 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03003118 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03003119
3120 return 0;
3121}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03003122
Adrian Hunterdbbd34a2018-05-17 12:21:53 +03003123/*
Alexandre Truongb9f6fbb2021-12-17 15:45:20 +00003124 * Compares the raw arch string. N.B. see instead perf_env__arch() or
3125 * machine__normalized_is() if a normalized arch is needed.
Adrian Hunterdbbd34a2018-05-17 12:21:53 +03003126 */
3127bool machine__is(struct machine *machine, const char *arch)
3128{
3129 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
3130}
3131
Alexandre Truongb9f6fbb2021-12-17 15:45:20 +00003132bool machine__normalized_is(struct machine *machine, const char *arch)
3133{
3134 return machine && !strcmp(perf_env__arch(machine->env), arch);
3135}
3136
Adrian Hunter9cecca32018-05-22 13:54:32 +03003137int machine__nr_cpus_avail(struct machine *machine)
3138{
3139 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
3140}
3141
Adrian Hunterfbe2af42014-08-15 22:08:39 +03003142int machine__get_kernel_start(struct machine *machine)
3143{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03003144 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03003145 int err = 0;
3146
3147 /*
3148 * The only addresses above 2^63 are kernel addresses of a 64-bit
3149 * kernel. Note that addresses are unsigned so that on a 32-bit system
3150 * all addresses including kernel addresses are less than 2^32. In
3151 * that case (32-bit system), if the kernel mapping is unknown, all
3152 * addresses will be assumed to be in user space - see
3153 * machine__kernel_ip().
3154 */
3155 machine->kernel_start = 1ULL << 63;
3156 if (map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03003157 err = map__load(map);
Adrian Hunter19422a92018-05-17 12:21:54 +03003158 /*
3159 * On x86_64, PTI entry trampolines are less than the
3160 * start of kernel text, but still above 2^63. So leave
3161 * kernel_start = 1ULL << 63 for x86_64.
3162 */
3163 if (!err && !machine__is(machine, "x86_64"))
Adrian Hunterfbe2af42014-08-15 22:08:39 +03003164 machine->kernel_start = map->start;
3165 }
3166 return err;
3167}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03003168
Adrian Hunter8e80ad92018-11-06 23:07:10 +02003169u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
3170{
3171 u8 addr_cpumode = cpumode;
3172 bool kernel_ip;
3173
3174 if (!machine->single_address_space)
3175 goto out;
3176
3177 kernel_ip = machine__kernel_ip(machine, addr);
3178 switch (cpumode) {
3179 case PERF_RECORD_MISC_KERNEL:
3180 case PERF_RECORD_MISC_USER:
3181 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
3182 PERF_RECORD_MISC_USER;
3183 break;
3184 case PERF_RECORD_MISC_GUEST_KERNEL:
3185 case PERF_RECORD_MISC_GUEST_USER:
3186 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
3187 PERF_RECORD_MISC_GUEST_USER;
3188 break;
3189 default:
3190 break;
3191 }
3192out:
3193 return addr_cpumode;
3194}
3195
Arnaldo Carvalho de Melo0e3149f2019-11-19 18:44:22 -03003196struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id)
3197{
3198 return dsos__findnew_id(&machine->dsos, filename, id);
3199}
3200
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03003201struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
3202{
Arnaldo Carvalho de Melo0e3149f2019-11-19 18:44:22 -03003203 return machine__findnew_dso_id(machine, filename, NULL);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03003204}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03003205
3206char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
3207{
3208 struct machine *machine = vmachine;
3209 struct map *map;
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03003210 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03003211
3212 if (sym == NULL)
3213 return NULL;
3214
3215 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
3216 *addrp = map->unmap_ip(map, sym->start);
3217 return sym->name;
3218}
Jiri Olsac57f5ea2020-09-13 23:03:08 +02003219
3220int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv)
3221{
3222 struct dso *pos;
3223 int err = 0;
3224
3225 list_for_each_entry(pos, &machine->dsos.head, node) {
3226 if (fn(pos, machine, priv))
3227 err = -1;
3228 }
3229 return err;
3230}