blob: 399b4731b24608c8e8e8130d22e93c58356f511d [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 Melo97b9d862019-08-22 17:10:08 -030019#include "srcline.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010020#include "symbol.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030021#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030022#include "strlist.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030023#include "target.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030024#include "thread.h"
Arnaldo Carvalho de Melo97b9d862019-08-22 17:10:08 -030025#include "util.h"
Adrian Hunterd027b6402014-07-23 14:23:00 +030026#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030027#include <stdbool.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030028#include <sys/types.h>
29#include <sys/stat.h>
30#include <unistd.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030031#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080032#include "linux/hash.h"
Hari Bathinif3b36142017-03-08 02:11:43 +053033#include "asm/bug.h"
Song Liu45178a92019-01-17 08:15:18 -080034#include "bpf-event.h"
Jiri Olsa20f2be12019-08-06 15:25:25 +020035#include <internal/lib.h> // page_size
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030036
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030037#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030038#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -030039#include <linux/mman.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030040#include <linux/string.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030041#include <linux/zalloc.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030042
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030043static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
44
Arnaldo Carvalho de Melo93730f82019-10-31 15:22:24 -030045static struct dso *machine__kernel_dso(struct machine *machine)
46{
47 return machine->vmlinux_map->dso;
48}
49
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030050static void dsos__init(struct dsos *dsos)
51{
52 INIT_LIST_HEAD(&dsos->head);
53 dsos->root = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030054 init_rwsem(&dsos->lock);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030055}
56
Kan Liang91e467b2017-09-10 19:23:14 -070057static void machine__threads_init(struct machine *machine)
58{
59 int i;
60
61 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
62 struct threads *threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -080063 threads->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030064 init_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -070065 threads->nr = 0;
66 INIT_LIST_HEAD(&threads->dead);
67 threads->last_match = NULL;
68 }
69}
70
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010071static int machine__set_mmap_name(struct machine *machine)
72{
Jiri Olsac1925242018-03-12 16:24:06 +010073 if (machine__is_host(machine))
74 machine->mmap_name = strdup("[kernel.kallsyms]");
75 else if (machine__is_default_guest(machine))
76 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
77 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
78 machine->pid) < 0)
79 machine->mmap_name = NULL;
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010080
81 return machine->mmap_name ? 0 : -ENOMEM;
82}
83
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030084int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
85{
Jiri Olsa81f981d2018-02-15 13:26:29 +010086 int err = -ENOMEM;
87
Wang Nan93b0ba32015-12-08 02:25:44 +000088 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -030089 maps__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030090 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030091 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030092
Kan Liang91e467b2017-09-10 19:23:14 -070093 machine__threads_init(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030094
Adrian Hunterd027b6402014-07-23 14:23:00 +030095 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030096 machine->env = NULL;
Adrian Hunterd027b6402014-07-23 14:23:00 +030097
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030098 machine->pid = pid;
99
Jiri Olsa14bd6d22014-01-07 13:47:19 +0100100 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -0300101 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +0300102 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +0300103 machine->kernel_start = 0;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300104 machine->vmlinux_map = NULL;
Masami Hiramatsucc1121a2015-12-09 11:11:33 +0900105
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300106 machine->root_dir = strdup(root_dir);
107 if (machine->root_dir == NULL)
108 return -ENOMEM;
109
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100110 if (machine__set_mmap_name(machine))
111 goto out;
112
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300113 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300114 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +0300115 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300116 char comm[64];
117
118 if (thread == NULL)
Jiri Olsa81f981d2018-02-15 13:26:29 +0100119 goto out;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300120
121 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200122 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300123 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300124 }
125
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300126 machine->current_tid = NULL;
Jiri Olsa81f981d2018-02-15 13:26:29 +0100127 err = 0;
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300128
Jiri Olsa81f981d2018-02-15 13:26:29 +0100129out:
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100130 if (err) {
Jiri Olsa81f981d2018-02-15 13:26:29 +0100131 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100132 zfree(&machine->mmap_name);
133 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300134 return 0;
135}
136
David Ahern8fb598e2013-09-28 13:13:00 -0600137struct machine *machine__new_host(void)
138{
139 struct machine *machine = malloc(sizeof(*machine));
140
141 if (machine != NULL) {
142 machine__init(machine, "", HOST_KERNEL_ID);
143
144 if (machine__create_kernel_maps(machine) < 0)
145 goto out_delete;
146 }
147
148 return machine;
149out_delete:
150 free(machine);
151 return NULL;
152}
153
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300154struct machine *machine__new_kallsyms(void)
155{
156 struct machine *machine = machine__new_host();
157 /*
158 * FIXME:
Ingo Molnaradba1632018-12-03 11:22:00 +0100159 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300160 * ask for not using the kcore parsing code, once this one is fixed
161 * to create a map per module.
162 */
Arnaldo Carvalho de Melo329f0ad2018-04-25 11:40:32 -0300163 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300164 machine__delete(machine);
165 machine = NULL;
166 }
167
168 return machine;
169}
170
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300171static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300172{
173 struct dso *pos, *n;
174
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300175 down_write(&dsos->lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300176
Waiman Long8fa7d872014-09-29 16:07:28 -0400177 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -0400178 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +0200179 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300180 list_del_init(&pos->node);
181 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300182 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300183
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300184 up_write(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300185}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300186
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300187static void dsos__exit(struct dsos *dsos)
188{
189 dsos__purge(dsos);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300190 exit_rwsem(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300191}
192
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300193void machine__delete_threads(struct machine *machine)
194{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300195 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700196 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300197
Kan Liang91e467b2017-09-10 19:23:14 -0700198 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
199 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300200 down_write(&threads->lock);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800201 nd = rb_first_cached(&threads->entries);
Kan Liang91e467b2017-09-10 19:23:14 -0700202 while (nd) {
203 struct thread *t = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300204
Kan Liang91e467b2017-09-10 19:23:14 -0700205 nd = rb_next(nd);
206 __machine__remove_thread(machine, t, false);
207 }
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300208 up_write(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300209 }
210}
211
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300212void machine__exit(struct machine *machine)
213{
Kan Liang91e467b2017-09-10 19:23:14 -0700214 int i;
215
Arnaldo Carvalho de Melo19993b82017-11-13 16:06:29 -0300216 if (machine == NULL)
217 return;
218
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900219 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300220 maps__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300221 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300222 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300223 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100224 zfree(&machine->mmap_name);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300225 zfree(&machine->current_tid);
Kan Liang91e467b2017-09-10 19:23:14 -0700226
227 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
228 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -0300229 struct thread *thread, *n;
230 /*
231 * Forget about the dead, at this point whatever threads were
232 * left in the dead lists better have a reference count taken
233 * by who is using them, and then, when they drop those references
234 * and it finally hits zero, thread__put() will check and see that
235 * its not in the dead threads list and will not try to remove it
236 * from there, just calling thread__delete() straight away.
237 */
238 list_for_each_entry_safe(thread, n, &threads->dead, node)
239 list_del_init(&thread->node);
240
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300241 exit_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -0700242 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300243}
244
245void machine__delete(struct machine *machine)
246{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300247 if (machine) {
248 machine__exit(machine);
249 free(machine);
250 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300251}
252
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300253void machines__init(struct machines *machines)
254{
255 machine__init(&machines->host, "", HOST_KERNEL_ID);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800256 machines->guests = RB_ROOT_CACHED;
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300257}
258
259void machines__exit(struct machines *machines)
260{
261 machine__exit(&machines->host);
262 /* XXX exit guest */
263}
264
265struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300266 const char *root_dir)
267{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800268 struct rb_node **p = &machines->guests.rb_root.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300269 struct rb_node *parent = NULL;
270 struct machine *pos, *machine = malloc(sizeof(*machine));
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800271 bool leftmost = true;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300272
273 if (machine == NULL)
274 return NULL;
275
276 if (machine__init(machine, root_dir, pid) != 0) {
277 free(machine);
278 return NULL;
279 }
280
281 while (*p != NULL) {
282 parent = *p;
283 pos = rb_entry(parent, struct machine, rb_node);
284 if (pid < pos->pid)
285 p = &(*p)->rb_left;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800286 else {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300287 p = &(*p)->rb_right;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800288 leftmost = false;
289 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300290 }
291
292 rb_link_node(&machine->rb_node, parent, p);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800293 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300294
295 return machine;
296}
297
Adrian Huntercfe1c412014-07-31 09:00:45 +0300298void machines__set_comm_exec(struct machines *machines, bool comm_exec)
299{
300 struct rb_node *nd;
301
302 machines->host.comm_exec = comm_exec;
303
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800304 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntercfe1c412014-07-31 09:00:45 +0300305 struct machine *machine = rb_entry(nd, struct machine, rb_node);
306
307 machine->comm_exec = comm_exec;
308 }
309}
310
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300311struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300312{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800313 struct rb_node **p = &machines->guests.rb_root.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300314 struct rb_node *parent = NULL;
315 struct machine *machine;
316 struct machine *default_machine = NULL;
317
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300318 if (pid == HOST_KERNEL_ID)
319 return &machines->host;
320
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300321 while (*p != NULL) {
322 parent = *p;
323 machine = rb_entry(parent, struct machine, rb_node);
324 if (pid < machine->pid)
325 p = &(*p)->rb_left;
326 else if (pid > machine->pid)
327 p = &(*p)->rb_right;
328 else
329 return machine;
330 if (!machine->pid)
331 default_machine = machine;
332 }
333
334 return default_machine;
335}
336
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300337struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300338{
339 char path[PATH_MAX];
340 const char *root_dir = "";
341 struct machine *machine = machines__find(machines, pid);
342
343 if (machine && (machine->pid == pid))
344 goto out;
345
346 if ((pid != HOST_KERNEL_ID) &&
347 (pid != DEFAULT_GUEST_KERNEL_ID) &&
348 (symbol_conf.guestmount)) {
349 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
350 if (access(path, R_OK)) {
351 static struct strlist *seen;
352
353 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300354 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300355
356 if (!strlist__has_entry(seen, path)) {
357 pr_err("Can't access file %s\n", path);
358 strlist__add(seen, path);
359 }
360 machine = NULL;
361 goto out;
362 }
363 root_dir = path;
364 }
365
366 machine = machines__add(machines, pid, root_dir);
367out:
368 return machine;
369}
370
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300371void machines__process_guests(struct machines *machines,
372 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300373{
374 struct rb_node *nd;
375
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800376 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300377 struct machine *pos = rb_entry(nd, struct machine, rb_node);
378 process(pos, data);
379 }
380}
381
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300382void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300383{
384 struct rb_node *node;
385 struct machine *machine;
386
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300387 machines->host.id_hdr_size = id_hdr_size;
388
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800389 for (node = rb_first_cached(&machines->guests); node;
390 node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300391 machine = rb_entry(node, struct machine, rb_node);
392 machine->id_hdr_size = id_hdr_size;
393 }
394
395 return;
396}
397
Adrian Hunter29ce3612014-07-16 11:07:13 +0300398static void machine__update_thread_pid(struct machine *machine,
399 struct thread *th, pid_t pid)
400{
401 struct thread *leader;
402
403 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
404 return;
405
406 th->pid_ = pid;
407
408 if (th->pid_ == th->tid)
409 return;
410
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300411 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300412 if (!leader)
413 goto out_err;
414
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300415 if (!leader->maps)
416 leader->maps = maps__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300417
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300418 if (!leader->maps)
Adrian Hunter29ce3612014-07-16 11:07:13 +0300419 goto out_err;
420
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300421 if (th->maps == leader->maps)
Adrian Hunter29ce3612014-07-16 11:07:13 +0300422 return;
423
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300424 if (th->maps) {
Adrian Hunter29ce3612014-07-16 11:07:13 +0300425 /*
426 * Maps are created from MMAP events which provide the pid and
427 * tid. Consequently there never should be any maps on a thread
428 * with an unknown pid. Just print an error if there are.
429 */
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300430 if (!maps__empty(th->maps))
Adrian Hunter29ce3612014-07-16 11:07:13 +0300431 pr_err("Discarding thread maps for %d:%d\n",
432 th->pid_, th->tid);
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300433 maps__put(th->maps);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300434 }
435
Arnaldo Carvalho de Melofe87797d2019-11-25 22:07:43 -0300436 th->maps = maps__get(leader->maps);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300437out_put:
438 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300439 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300440out_err:
441 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300442 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300443}
444
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300445/*
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200446 * Front-end cache - TID lookups come in blocks,
447 * so most of the time we dont have to look up
448 * the full rbtree:
449 */
450static struct thread*
Jiri Olsab57334b2018-07-19 16:33:44 +0200451__threads__get_last_match(struct threads *threads, struct machine *machine,
452 int pid, int tid)
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200453{
454 struct thread *th;
455
456 th = threads->last_match;
457 if (th != NULL) {
458 if (th->tid == tid) {
459 machine__update_thread_pid(machine, th, pid);
460 return thread__get(th);
461 }
462
463 threads->last_match = NULL;
464 }
465
466 return NULL;
467}
468
Jiri Olsab57334b2018-07-19 16:33:44 +0200469static struct thread*
470threads__get_last_match(struct threads *threads, struct machine *machine,
471 int pid, int tid)
472{
473 struct thread *th = NULL;
474
475 if (perf_singlethreaded)
476 th = __threads__get_last_match(threads, machine, pid, tid);
477
478 return th;
479}
480
481static void
482__threads__set_last_match(struct threads *threads, struct thread *th)
483{
484 threads->last_match = th;
485}
486
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200487static void
488threads__set_last_match(struct threads *threads, struct thread *th)
489{
Jiri Olsab57334b2018-07-19 16:33:44 +0200490 if (perf_singlethreaded)
491 __threads__set_last_match(threads, th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200492}
493
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200494/*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800495 * Caller must eventually drop thread->refcnt returned with a successful
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300496 * lookup/new thread inserted.
497 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300498static struct thread *____machine__findnew_thread(struct machine *machine,
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300499 struct threads *threads,
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300500 pid_t pid, pid_t tid,
501 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300502{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800503 struct rb_node **p = &threads->entries.rb_root.rb_node;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300504 struct rb_node *parent = NULL;
505 struct thread *th;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800506 bool leftmost = true;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300507
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200508 th = threads__get_last_match(threads, machine, pid, tid);
509 if (th)
510 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300511
512 while (*p != NULL) {
513 parent = *p;
514 th = rb_entry(parent, struct thread, rb_node);
515
Adrian Hunter38051232013-07-04 16:20:31 +0300516 if (th->tid == tid) {
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200517 threads__set_last_match(threads, th);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300518 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300519 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300520 }
521
Adrian Hunter38051232013-07-04 16:20:31 +0300522 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300523 p = &(*p)->rb_left;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800524 else {
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300525 p = &(*p)->rb_right;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800526 leftmost = false;
527 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300528 }
529
530 if (!create)
531 return NULL;
532
Adrian Hunter99d725f2013-08-26 16:00:19 +0300533 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300534 if (th != NULL) {
535 rb_link_node(&th->rb_node, parent, p);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800536 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
Jiri Olsacddcef62014-04-09 20:54:29 +0200537
538 /*
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300539 * We have to initialize maps separately after rb tree is updated.
Jiri Olsacddcef62014-04-09 20:54:29 +0200540 *
541 * The reason is that we call machine__findnew_thread
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300542 * within thread__init_maps to find the thread
Jiri Olsacddcef62014-04-09 20:54:29 +0200543 * leader and that would screwed the rb tree.
544 */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300545 if (thread__init_maps(th, machine)) {
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800546 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300547 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300548 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200549 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300550 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300551 /*
552 * It is now in the rbtree, get a ref
553 */
554 thread__get(th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200555 threads__set_last_match(threads, th);
Kan Liang91e467b2017-09-10 19:23:14 -0700556 ++threads->nr;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300557 }
558
559 return th;
560}
561
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300562struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
563{
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300564 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300565}
566
Adrian Hunter314add62013-08-27 11:23:03 +0300567struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
568 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300569{
Kan Liang91e467b2017-09-10 19:23:14 -0700570 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300571 struct thread *th;
572
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300573 down_write(&threads->lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300574 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300575 up_write(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300576 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300577}
578
Jiri Olsad75e6092014-03-14 15:00:03 +0100579struct thread *machine__find_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;
Kan Liang91e467b2017-09-10 19:23:14 -0700584
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300585 down_read(&threads->lock);
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300586 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300587 up_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300588 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300589}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300590
Adrian Huntercfe1c412014-07-31 09:00:45 +0300591struct comm *machine__thread_exec_comm(struct machine *machine,
592 struct thread *thread)
593{
594 if (machine->comm_exec)
595 return thread__exec_comm(thread);
596 else
597 return thread__comm(thread);
598}
599
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200600int machine__process_comm_event(struct machine *machine, union perf_event *event,
601 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300602{
Adrian Hunter314add62013-08-27 11:23:03 +0300603 struct thread *thread = machine__findnew_thread(machine,
604 event->comm.pid,
605 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300606 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300607 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300608
Adrian Huntercfe1c412014-07-31 09:00:45 +0300609 if (exec)
610 machine->comm_exec = true;
611
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300612 if (dump_trace)
613 perf_event__fprintf_comm(event, stdout);
614
Adrian Hunter65de51f2014-07-31 09:00:44 +0300615 if (thread == NULL ||
616 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300617 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300618 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300619 }
620
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300621 thread__put(thread);
622
623 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300624}
625
Hari Bathinif3b36142017-03-08 02:11:43 +0530626int machine__process_namespaces_event(struct machine *machine __maybe_unused,
627 union perf_event *event,
628 struct perf_sample *sample __maybe_unused)
629{
630 struct thread *thread = machine__findnew_thread(machine,
631 event->namespaces.pid,
632 event->namespaces.tid);
633 int err = 0;
634
635 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
636 "\nWARNING: kernel seems to support more namespaces than perf"
637 " tool.\nTry updating the perf tool..\n\n");
638
639 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
640 "\nWARNING: perf tool seems to support more namespaces than"
641 " the kernel.\nTry updating the kernel..\n\n");
642
643 if (dump_trace)
644 perf_event__fprintf_namespaces(event, stdout);
645
646 if (thread == NULL ||
647 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
648 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
649 err = -1;
650 }
651
652 thread__put(thread);
653
654 return err;
655}
656
Namhyung Kimba78c1c2020-03-25 21:45:30 +0900657int machine__process_cgroup_event(struct machine *machine __maybe_unused,
658 union perf_event *event,
659 struct perf_sample *sample __maybe_unused)
660{
661 if (dump_trace)
662 perf_event__fprintf_cgroup(event, stdout);
663
664 return 0;
665}
666
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300667int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200668 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300669{
Jiri Olsa5290ed62019-08-25 20:17:46 +0200670 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300671 event->lost.id, event->lost.lost);
672 return 0;
673}
674
Kan Liangc4937a92015-05-10 15:13:15 -0400675int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
676 union perf_event *event, struct perf_sample *sample)
677{
Jiri Olsaa2e254d2019-08-25 20:17:47 +0200678 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
Kan Liangc4937a92015-05-10 15:13:15 -0400679 sample->id, event->lost_samples.lost);
680 return 0;
681}
682
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300683static struct dso *machine__findnew_module_dso(struct machine *machine,
684 struct kmod_path *m,
685 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100686{
687 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100688
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300689 down_write(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300690
691 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100692 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300693 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100694 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300695 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100696
Namhyung Kim6b335e82017-05-31 21:01:04 +0900697 dso__set_module_info(dso, m, machine);
Jiri Olsaca333802015-02-17 17:29:57 +0100698 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsa02213ce2020-02-10 15:32:15 +0100699 dso->kernel = DSO_TYPE_KERNEL;
Jiri Olsada17ea32015-02-12 22:10:52 +0100700 }
701
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300702 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300703out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300704 up_write(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100705 return dso;
706}
707
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300708int machine__process_aux_event(struct machine *machine __maybe_unused,
709 union perf_event *event)
710{
711 if (dump_trace)
712 perf_event__fprintf_aux(event, stdout);
713 return 0;
714}
715
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300716int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
717 union perf_event *event)
718{
719 if (dump_trace)
720 perf_event__fprintf_itrace_start(event, stdout);
721 return 0;
722}
723
Adrian Hunter02860392015-07-21 12:44:03 +0300724int machine__process_switch_event(struct machine *machine __maybe_unused,
725 union perf_event *event)
726{
727 if (dump_trace)
728 perf_event__fprintf_switch(event, stdout);
729 return 0;
730}
731
Song Liu9aa0bfa2019-01-17 08:15:17 -0800732static int machine__process_ksymbol_register(struct machine *machine,
733 union perf_event *event,
734 struct perf_sample *sample __maybe_unused)
735{
736 struct symbol *sym;
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300737 struct map *map = maps__find(&machine->kmaps, event->ksymbol.addr);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800738
Song Liu9aa0bfa2019-01-17 08:15:17 -0800739 if (!map) {
Jiri Olsa4a4eb612020-02-10 21:08:47 +0100740 struct dso *dso = dso__new(event->ksymbol.name);
741
742 if (dso) {
743 dso->kernel = DSO_TYPE_KERNEL;
744 map = map__new2(0, dso);
745 }
746
747 if (!dso || !map) {
748 dso__put(dso);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800749 return -ENOMEM;
Jiri Olsa4a4eb612020-02-10 21:08:47 +0100750 }
Song Liu9aa0bfa2019-01-17 08:15:17 -0800751
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300752 map->start = event->ksymbol.addr;
753 map->end = map->start + event->ksymbol.len;
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300754 maps__insert(&machine->kmaps, map);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800755 }
756
Jiri Olsa8529f2e2019-05-08 15:20:04 +0200757 sym = symbol__new(map->map_ip(map, map->start),
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300758 event->ksymbol.len,
759 0, 0, event->ksymbol.name);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800760 if (!sym)
761 return -ENOMEM;
762 dso__insert_symbol(map->dso, sym);
763 return 0;
764}
765
766static int machine__process_ksymbol_unregister(struct machine *machine,
767 union perf_event *event,
768 struct perf_sample *sample __maybe_unused)
769{
770 struct map *map;
771
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300772 map = maps__find(&machine->kmaps, event->ksymbol.addr);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800773 if (map)
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300774 maps__remove(&machine->kmaps, map);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800775
776 return 0;
777}
778
779int machine__process_ksymbol(struct machine *machine __maybe_unused,
780 union perf_event *event,
781 struct perf_sample *sample)
782{
783 if (dump_trace)
784 perf_event__fprintf_ksymbol(event, stdout);
785
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300786 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
Song Liu9aa0bfa2019-01-17 08:15:17 -0800787 return machine__process_ksymbol_unregister(machine, event,
788 sample);
789 return machine__process_ksymbol_register(machine, event, sample);
790}
791
Arnaldo Carvalho de Meloa94ab912019-11-14 12:28:41 -0300792static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
793 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300794{
Jiri Olsaca333802015-02-17 17:29:57 +0100795 struct map *map = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100796 struct kmod_path m;
Arnaldo Carvalho de Meloa94ab912019-11-14 12:28:41 -0300797 struct dso *dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300798
Jiri Olsaca333802015-02-17 17:29:57 +0100799 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300800 return NULL;
801
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300802 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100803 if (dso == NULL)
804 goto out;
805
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300806 map = map__new2(start, dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300807 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100808 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300809
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300810 maps__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100811
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300812 /* Put the map here because maps__insert alread got it */
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900813 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100814out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900815 /* put the dso here, corresponding to machine__findnew_module_dso */
816 dso__put(dso);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300817 zfree(&m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300818 return map;
819}
820
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300821size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300822{
823 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300824 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300825
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800826 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300827 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300828 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300829 }
830
831 return ret;
832}
833
Waiman Long8fa7d872014-09-29 16:07:28 -0400834size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300835 bool (skip)(struct dso *dso, int parm), int parm)
836{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300837 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300838}
839
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300840size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300841 bool (skip)(struct dso *dso, int parm), int parm)
842{
843 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300844 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300845
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800846 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300847 struct machine *pos = rb_entry(nd, struct machine, rb_node);
848 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
849 }
850 return ret;
851}
852
853size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
854{
855 int i;
856 size_t printed = 0;
Arnaldo Carvalho de Melo93730f82019-10-31 15:22:24 -0300857 struct dso *kdso = machine__kernel_dso(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300858
859 if (kdso->has_build_id) {
860 char filename[PATH_MAX];
Krister Johansend2396992017-07-05 18:48:13 -0700861 if (dso__build_id_filename(kdso, filename, sizeof(filename),
862 false))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300863 printed += fprintf(fp, "[0] %s\n", filename);
864 }
865
866 for (i = 0; i < vmlinux_path__nr_entries; ++i)
867 printed += fprintf(fp, "[%d] %s\n",
868 i + kdso->has_build_id, vmlinux_path[i]);
869
870 return printed;
871}
872
873size_t machine__fprintf(struct machine *machine, FILE *fp)
874{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300875 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700876 size_t ret;
877 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300878
Kan Liang91e467b2017-09-10 19:23:14 -0700879 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
880 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300881
882 down_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300883
Kan Liang91e467b2017-09-10 19:23:14 -0700884 ret = fprintf(fp, "Threads: %u\n", threads->nr);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300885
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800886 for (nd = rb_first_cached(&threads->entries); nd;
887 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -0700888 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300889
Kan Liang91e467b2017-09-10 19:23:14 -0700890 ret += thread__fprintf(pos, fp);
891 }
892
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300893 up_read(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300894 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300895 return ret;
896}
897
898static struct dso *machine__get_kernel(struct machine *machine)
899{
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100900 const char *vmlinux_name = machine->mmap_name;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300901 struct dso *kernel;
902
903 if (machine__is_host(machine)) {
Jiri Olsac1925242018-03-12 16:24:06 +0100904 if (symbol_conf.vmlinux_name)
905 vmlinux_name = symbol_conf.vmlinux_name;
906
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300907 kernel = machine__findnew_kernel(machine, vmlinux_name,
908 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300909 } else {
Jiri Olsac1925242018-03-12 16:24:06 +0100910 if (symbol_conf.default_guest_vmlinux_name)
911 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
912
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300913 kernel = machine__findnew_kernel(machine, vmlinux_name,
914 "[guest.kernel]",
915 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300916 }
917
918 if (kernel != NULL && (!kernel->has_build_id))
919 dso__read_running_kernel_build_id(kernel, machine);
920
921 return kernel;
922}
923
924struct process_args {
925 u64 start;
926};
927
Adrian Hunter1c5aae72018-05-22 13:54:36 +0300928void machine__get_kallsyms_filename(struct machine *machine, char *buf,
929 size_t bufsz)
Adrian Hunter15a0a872014-01-29 16:14:38 +0200930{
931 if (machine__is_default_guest(machine))
932 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
933 else
934 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
935}
936
Simon Quea93f0e52014-06-16 11:32:09 -0700937const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
938
939/* Figure out the start address of kernel map from /proc/kallsyms.
940 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
941 * symbol_name if it's not that important.
942 */
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300943static int machine__get_running_kernel_start(struct machine *machine,
Jiri Olsaed9adb22019-05-08 15:20:03 +0200944 const char **symbol_name,
945 u64 *start, u64 *end)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300946{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200947 char filename[PATH_MAX];
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300948 int i, err = -1;
Simon Quea93f0e52014-06-16 11:32:09 -0700949 const char *name;
950 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300951
Adrian Hunter15a0a872014-01-29 16:14:38 +0200952 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300953
954 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
955 return 0;
956
Simon Quea93f0e52014-06-16 11:32:09 -0700957 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300958 err = kallsyms__get_function_start(filename, name, &addr);
959 if (!err)
Simon Quea93f0e52014-06-16 11:32:09 -0700960 break;
961 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300962
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300963 if (err)
964 return -1;
965
Simon Quea93f0e52014-06-16 11:32:09 -0700966 if (symbol_name)
967 *symbol_name = name;
968
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300969 *start = addr;
Jiri Olsaed9adb22019-05-08 15:20:03 +0200970
971 err = kallsyms__get_function_start(filename, "_etext", &addr);
972 if (!err)
973 *end = addr;
974
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300975 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300976}
977
Adrian Hunter1c5aae72018-05-22 13:54:36 +0300978int machine__create_extra_kernel_map(struct machine *machine,
979 struct dso *kernel,
980 struct extra_kernel_map *xm)
Adrian Hunter4d99e412018-05-22 13:54:33 +0300981{
982 struct kmap *kmap;
983 struct map *map;
984
985 map = map__new2(xm->start, kernel);
986 if (!map)
987 return -1;
988
989 map->end = xm->end;
990 map->pgoff = xm->pgoff;
991
992 kmap = map__kmap(map);
993
Adrian Hunter5759a682018-05-22 13:54:35 +0300994 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
Adrian Hunter4d99e412018-05-22 13:54:33 +0300995
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300996 maps__insert(&machine->kmaps, map);
Adrian Hunter4d99e412018-05-22 13:54:33 +0300997
Adrian Hunter5759a682018-05-22 13:54:35 +0300998 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
999 kmap->name, map->start, map->end);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001000
1001 map__put(map);
1002
1003 return 0;
1004}
1005
1006static u64 find_entry_trampoline(struct dso *dso)
1007{
1008 /* Duplicates are removed so lookup all aliases */
1009 const char *syms[] = {
1010 "_entry_trampoline",
1011 "__entry_trampoline_start",
1012 "entry_SYSCALL_64_trampoline",
1013 };
1014 struct symbol *sym = dso__first_symbol(dso);
1015 unsigned int i;
1016
1017 for (; sym; sym = dso__next_symbol(sym)) {
1018 if (sym->binding != STB_GLOBAL)
1019 continue;
1020 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1021 if (!strcmp(sym->name, syms[i]))
1022 return sym->start;
1023 }
1024 }
1025
1026 return 0;
1027}
1028
1029/*
1030 * These values can be used for kernels that do not have symbols for the entry
1031 * trampolines in kallsyms.
1032 */
1033#define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1034#define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1035#define X86_64_ENTRY_TRAMPOLINE 0x6000
1036
1037/* Map x86_64 PTI entry trampolines */
1038int machine__map_x86_64_entry_trampolines(struct machine *machine,
1039 struct dso *kernel)
1040{
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001041 struct maps *kmaps = &machine->kmaps;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001042 int nr_cpus_avail, cpu;
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001043 bool found = false;
1044 struct map *map;
1045 u64 pgoff;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001046
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001047 /*
1048 * In the vmlinux case, pgoff is a virtual address which must now be
1049 * mapped to a vmlinux offset.
1050 */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001051 maps__for_each_entry(kmaps, map) {
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001052 struct kmap *kmap = __map__kmap(map);
1053 struct map *dest_map;
1054
1055 if (!kmap || !is_entry_trampoline(kmap->name))
1056 continue;
1057
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001058 dest_map = maps__find(kmaps, map->pgoff);
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001059 if (dest_map != map)
1060 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1061 found = true;
1062 }
1063 if (found || machine->trampolines_mapped)
1064 return 0;
1065
1066 pgoff = find_entry_trampoline(kernel);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001067 if (!pgoff)
1068 return 0;
1069
1070 nr_cpus_avail = machine__nr_cpus_avail(machine);
1071
1072 /* Add a 1 page map for each CPU's entry trampoline */
1073 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1074 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1075 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1076 X86_64_ENTRY_TRAMPOLINE;
1077 struct extra_kernel_map xm = {
1078 .start = va,
1079 .end = va + page_size,
1080 .pgoff = pgoff,
1081 };
1082
Adrian Hunter5759a682018-05-22 13:54:35 +03001083 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1084
Adrian Hunter4d99e412018-05-22 13:54:33 +03001085 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1086 return -1;
1087 }
1088
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001089 machine->trampolines_mapped = nr_cpus_avail;
1090
1091 return 0;
1092}
1093
1094int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1095 struct dso *kernel __maybe_unused)
1096{
Adrian Hunter4d99e412018-05-22 13:54:33 +03001097 return 0;
1098}
1099
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001100static int
1101__machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001102{
Masami Hiramatsucc1121a2015-12-09 11:11:33 +09001103 /* In case of renewal the kernel map, destroy previous one */
1104 machine__destroy_kernel_maps(machine);
1105
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001106 machine->vmlinux_map = map__new2(0, kernel);
1107 if (machine->vmlinux_map == NULL)
1108 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001109
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001110 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
Jiri Olsa484214f2020-02-10 15:32:18 +01001111 maps__insert(&machine->kmaps, machine->vmlinux_map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001112 return 0;
1113}
1114
1115void machine__destroy_kernel_maps(struct machine *machine)
1116{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001117 struct kmap *kmap;
1118 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001119
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001120 if (map == NULL)
1121 return;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001122
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001123 kmap = map__kmap(map);
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001124 maps__remove(&machine->kmaps, map);
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001125 if (kmap && kmap->ref_reloc_sym) {
1126 zfree((char **)&kmap->ref_reloc_sym->name);
1127 zfree(&kmap->ref_reloc_sym);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001128 }
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001129
1130 map__zput(machine->vmlinux_map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001131}
1132
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001133int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001134{
1135 int ret = 0;
1136 struct dirent **namelist = NULL;
1137 int i, items = 0;
1138 char path[PATH_MAX];
1139 pid_t pid;
1140 char *endp;
1141
1142 if (symbol_conf.default_guest_vmlinux_name ||
1143 symbol_conf.default_guest_modules ||
1144 symbol_conf.default_guest_kallsyms) {
1145 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1146 }
1147
1148 if (symbol_conf.guestmount) {
1149 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1150 if (items <= 0)
1151 return -ENOENT;
1152 for (i = 0; i < items; i++) {
1153 if (!isdigit(namelist[i]->d_name[0])) {
1154 /* Filter out . and .. */
1155 continue;
1156 }
1157 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1158 if ((*endp != '\0') ||
1159 (endp == namelist[i]->d_name) ||
1160 (errno == ERANGE)) {
1161 pr_debug("invalid directory (%s). Skipping.\n",
1162 namelist[i]->d_name);
1163 continue;
1164 }
1165 sprintf(path, "%s/%s/proc/kallsyms",
1166 symbol_conf.guestmount,
1167 namelist[i]->d_name);
1168 ret = access(path, R_OK);
1169 if (ret) {
1170 pr_debug("Can't access file %s\n", path);
1171 goto failure;
1172 }
1173 machines__create_kernel_maps(machines, pid);
1174 }
1175failure:
1176 free(namelist);
1177 }
1178
1179 return ret;
1180}
1181
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001182void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001183{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001184 struct rb_node *next = rb_first_cached(&machines->guests);
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001185
1186 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001187
1188 while (next) {
1189 struct machine *pos = rb_entry(next, struct machine, rb_node);
1190
1191 next = rb_next(&pos->rb_node);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001192 rb_erase_cached(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001193 machine__delete(pos);
1194 }
1195}
1196
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001197int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001198{
1199 struct machine *machine = machines__findnew(machines, pid);
1200
1201 if (machine == NULL)
1202 return -1;
1203
1204 return machine__create_kernel_maps(machine);
1205}
1206
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001207int machine__load_kallsyms(struct machine *machine, const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001208{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001209 struct map *map = machine__kernel_map(machine);
Jiri Olsae8f38792018-02-15 13:26:33 +01001210 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001211
1212 if (ret > 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001213 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001214 /*
1215 * Since /proc/kallsyms will have multiple sessions for the
1216 * kernel, with modules between them, fixup the end of all
1217 * sections.
1218 */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001219 maps__fixup_end(&machine->kmaps);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001220 }
1221
1222 return ret;
1223}
1224
Arnaldo Carvalho de Melo1d1a2652018-04-25 12:18:11 -03001225int machine__load_vmlinux_path(struct machine *machine)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001226{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001227 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001228 int ret = dso__load_vmlinux_path(map->dso, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001229
Adrian Hunter39b12f782013-08-07 14:38:47 +03001230 if (ret > 0)
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001231 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001232
1233 return ret;
1234}
1235
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001236static char *get_kernel_version(const char *root_dir)
1237{
1238 char version[PATH_MAX];
1239 FILE *file;
1240 char *name, *tmp;
1241 const char *prefix = "Linux version ";
1242
1243 sprintf(version, "%s/proc/version", root_dir);
1244 file = fopen(version, "r");
1245 if (!file)
1246 return NULL;
1247
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001248 tmp = fgets(version, sizeof(version), file);
1249 fclose(file);
Donald Yandt34b65af2019-05-28 09:41:28 -04001250 if (!tmp)
1251 return NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001252
1253 name = strstr(version, prefix);
1254 if (!name)
1255 return NULL;
1256 name += strlen(prefix);
1257 tmp = strchr(name, ' ');
1258 if (tmp)
1259 *tmp = '\0';
1260
1261 return strdup(name);
1262}
1263
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001264static bool is_kmod_dso(struct dso *dso)
1265{
1266 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1267 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1268}
1269
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001270static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001271{
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001272 char *long_name;
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001273 struct map *map = maps__find_by_name(maps, m->name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001274
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001275 if (map == NULL)
1276 return 0;
1277
1278 long_name = strdup(path);
1279 if (long_name == NULL)
1280 return -ENOMEM;
1281
1282 dso__set_long_name(map->dso, long_name, true);
1283 dso__kernel_module_get_build_id(map->dso, "");
1284
1285 /*
1286 * Full name could reveal us kmod compression, so
1287 * we need to update the symtab_type if needed.
1288 */
Jiri Olsa2af52472018-08-17 11:48:07 +02001289 if (m->comp && is_kmod_dso(map->dso)) {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001290 map->dso->symtab_type++;
Jiri Olsa2af52472018-08-17 11:48:07 +02001291 map->dso->comp = m->comp;
1292 }
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001293
1294 return 0;
1295}
1296
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001297static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001298{
1299 struct dirent *dent;
1300 DIR *dir = opendir(dir_name);
1301 int ret = 0;
1302
1303 if (!dir) {
1304 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1305 return -1;
1306 }
1307
1308 while ((dent = readdir(dir)) != NULL) {
1309 char path[PATH_MAX];
1310 struct stat st;
1311
1312 /*sshfs might return bad dent->d_type, so we have to stat*/
1313 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1314 if (stat(path, &st))
1315 continue;
1316
1317 if (S_ISDIR(st.st_mode)) {
1318 if (!strcmp(dent->d_name, ".") ||
1319 !strcmp(dent->d_name, ".."))
1320 continue;
1321
Richard Yao61d42902014-04-26 13:17:55 -04001322 /* Do not follow top-level source and build symlinks */
1323 if (depth == 0) {
1324 if (!strcmp(dent->d_name, "source") ||
1325 !strcmp(dent->d_name, "build"))
1326 continue;
1327 }
1328
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001329 ret = maps__set_modules_path_dir(maps, path, depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001330 if (ret < 0)
1331 goto out;
1332 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001333 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001334
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001335 ret = kmod_path__parse_name(&m, dent->d_name);
1336 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001337 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001338
1339 if (m.kmod)
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001340 ret = maps__set_module_path(maps, path, &m);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001341
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001342 zfree(&m.name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001343
1344 if (ret)
1345 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001346 }
1347 }
1348
1349out:
1350 closedir(dir);
1351 return ret;
1352}
1353
1354static int machine__set_modules_path(struct machine *machine)
1355{
1356 char *version;
1357 char modules_path[PATH_MAX];
1358
1359 version = get_kernel_version(machine->root_dir);
1360 if (!version)
1361 return -1;
1362
Richard Yao61d42902014-04-26 13:17:55 -04001363 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001364 machine->root_dir, version);
1365 free(version);
1366
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001367 return maps__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001368}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001369int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
Thomas Richter12a6d292019-07-24 14:27:02 +02001370 u64 *size __maybe_unused,
Song Shan Gong203d8a42016-07-21 11:10:51 +08001371 const char *name __maybe_unused)
1372{
1373 return 0;
1374}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001375
Thomas Richter9ad46522017-08-03 15:49:02 +02001376static int machine__create_module(void *arg, const char *name, u64 start,
1377 u64 size)
Adrian Hunter316d70d2013-10-08 11:45:48 +03001378{
1379 struct machine *machine = arg;
1380 struct map *map;
1381
Thomas Richter12a6d292019-07-24 14:27:02 +02001382 if (arch__fix_module_text_start(&start, &size, name) < 0)
Song Shan Gong203d8a42016-07-21 11:10:51 +08001383 return -1;
1384
Arnaldo Carvalho de Meloa94ab912019-11-14 12:28:41 -03001385 map = machine__addnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001386 if (map == NULL)
1387 return -1;
Thomas Richter9ad46522017-08-03 15:49:02 +02001388 map->end = start + size;
Adrian Hunter316d70d2013-10-08 11:45:48 +03001389
1390 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1391
1392 return 0;
1393}
1394
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001395static int machine__create_modules(struct machine *machine)
1396{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001397 const char *modules;
1398 char path[PATH_MAX];
1399
Adrian Hunterf4be9042013-09-22 13:22:09 +03001400 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001401 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001402 } else {
1403 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001404 modules = path;
1405 }
1406
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001407 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001408 return -1;
1409
Adrian Hunter316d70d2013-10-08 11:45:48 +03001410 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001411 return -1;
1412
Adrian Hunter316d70d2013-10-08 11:45:48 +03001413 if (!machine__set_modules_path(machine))
1414 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001415
Adrian Hunter316d70d2013-10-08 11:45:48 +03001416 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001417
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001418 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001419}
1420
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001421static void machine__set_kernel_mmap(struct machine *machine,
1422 u64 start, u64 end)
1423{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001424 machine->vmlinux_map->start = start;
1425 machine->vmlinux_map->end = end;
1426 /*
1427 * Be a bit paranoid here, some perf.data file came with
1428 * a zero sized synthesized MMAP event for the kernel.
1429 */
1430 if (start == 0 && end == 0)
1431 machine->vmlinux_map->end = ~0ULL;
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001432}
1433
Wei Li977c7a62019-02-28 17:20:03 +08001434static void machine__update_kernel_mmap(struct machine *machine,
1435 u64 start, u64 end)
1436{
1437 struct map *map = machine__kernel_map(machine);
1438
1439 map__get(map);
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001440 maps__remove(&machine->kmaps, map);
Wei Li977c7a62019-02-28 17:20:03 +08001441
1442 machine__set_kernel_mmap(machine, start, end);
1443
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001444 maps__insert(&machine->kmaps, map);
Wei Li977c7a62019-02-28 17:20:03 +08001445 map__put(map);
1446}
1447
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001448int machine__create_kernel_maps(struct machine *machine)
1449{
1450 struct dso *kernel = machine__get_kernel(machine);
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001451 const char *name = NULL;
Namhyung Kimee05d21792018-02-19 19:05:45 +09001452 struct map *map;
Jiri Olsaed9adb22019-05-08 15:20:03 +02001453 u64 start = 0, end = ~0ULL;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001454 int ret;
1455
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001456 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001457 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001458
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001459 ret = __machine__create_kernel_maps(machine, kernel);
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001460 if (ret < 0)
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001461 goto out_put;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001462
1463 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1464 if (machine__is_host(machine))
1465 pr_debug("Problems creating module maps, "
1466 "continuing anyway...\n");
1467 else
1468 pr_debug("Problems creating module maps for guest %d, "
1469 "continuing anyway...\n", machine->pid);
1470 }
1471
Jiri Olsaed9adb22019-05-08 15:20:03 +02001472 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001473 if (name &&
Jiri Olsaed9adb22019-05-08 15:20:03 +02001474 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001475 machine__destroy_kernel_maps(machine);
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001476 ret = -1;
1477 goto out_put;
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001478 }
Namhyung Kimee05d21792018-02-19 19:05:45 +09001479
Wei Li977c7a62019-02-28 17:20:03 +08001480 /*
1481 * we have a real start address now, so re-order the kmaps
1482 * assume it's the last in the kmaps
1483 */
Jiri Olsaed9adb22019-05-08 15:20:03 +02001484 machine__update_kernel_mmap(machine, start, end);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001485 }
1486
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001487 if (machine__create_extra_kernel_maps(machine, kernel))
1488 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1489
Jiri Olsaed9adb22019-05-08 15:20:03 +02001490 if (end == ~0ULL) {
1491 /* update end address of the kernel map using adjacent module address */
1492 map = map__next(machine__kernel_map(machine));
1493 if (map)
1494 machine__set_kernel_mmap(machine, start, map->start);
1495 }
1496
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001497out_put:
1498 dso__put(kernel);
1499 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001500}
1501
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001502static bool machine__uses_kcore(struct machine *machine)
1503{
1504 struct dso *dso;
1505
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001506 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001507 if (dso__is_kcore(dso))
1508 return true;
1509 }
1510
1511 return false;
1512}
1513
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001514static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1515 union perf_event *event)
1516{
1517 return machine__is(machine, "x86_64") &&
1518 is_entry_trampoline(event->mmap.filename);
1519}
1520
1521static int machine__process_extra_kernel_map(struct machine *machine,
1522 union perf_event *event)
1523{
Arnaldo Carvalho de Melo93730f82019-10-31 15:22:24 -03001524 struct dso *kernel = machine__kernel_dso(machine);
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001525 struct extra_kernel_map xm = {
1526 .start = event->mmap.start,
1527 .end = event->mmap.start + event->mmap.len,
1528 .pgoff = event->mmap.pgoff,
1529 };
1530
1531 if (kernel == NULL)
1532 return -1;
1533
1534 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1535
1536 return machine__create_extra_kernel_map(machine, kernel, &xm);
1537}
1538
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001539static int machine__process_kernel_mmap_event(struct machine *machine,
1540 union perf_event *event)
1541{
1542 struct map *map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001543 enum dso_kernel_type kernel_type;
1544 bool is_kernel_mmap;
1545
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001546 /* If we have maps from kcore then we do not need or want any others */
1547 if (machine__uses_kcore(machine))
1548 return 0;
1549
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001550 if (machine__is_host(machine))
1551 kernel_type = DSO_TYPE_KERNEL;
1552 else
1553 kernel_type = DSO_TYPE_GUEST_KERNEL;
1554
1555 is_kernel_mmap = memcmp(event->mmap.filename,
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001556 machine->mmap_name,
1557 strlen(machine->mmap_name) - 1) == 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001558 if (event->mmap.filename[0] == '/' ||
1559 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Meloa94ab912019-11-14 12:28:41 -03001560 map = machine__addnew_module_map(machine, event->mmap.start,
1561 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001562 if (map == NULL)
1563 goto out_problem;
1564
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001565 map->end = map->start + event->mmap.len;
1566 } else if (is_kernel_mmap) {
1567 const char *symbol_name = (event->mmap.filename +
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001568 strlen(machine->mmap_name));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001569 /*
1570 * Should be there already, from the build-id table in
1571 * the header.
1572 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001573 struct dso *kernel = NULL;
1574 struct dso *dso;
1575
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001576 down_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001577
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001578 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001579
1580 /*
1581 * The cpumode passed to is_kernel_module is not the
1582 * cpumode of *this* event. If we insist on passing
1583 * correct cpumode to is_kernel_module, we should
1584 * record the cpumode when we adding this dso to the
1585 * linked list.
1586 *
1587 * However we don't really need passing correct
1588 * cpumode. We know the correct cpumode must be kernel
1589 * mode (if not, we should not link it onto kernel_dsos
1590 * list).
1591 *
1592 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1593 * is_kernel_module() treats it as a kernel cpumode.
1594 */
1595
1596 if (!dso->kernel ||
1597 is_kernel_module(dso->long_name,
1598 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001599 continue;
1600
Wang Nan1f121b02015-06-03 08:52:21 +00001601
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001602 kernel = dso;
1603 break;
1604 }
1605
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001606 up_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001607
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001608 if (kernel == NULL)
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001609 kernel = machine__findnew_dso(machine, machine->mmap_name);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001610 if (kernel == NULL)
1611 goto out_problem;
1612
1613 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001614 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1615 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001616 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001617 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001618
Namhyung Kim330dfa22014-11-18 13:30:28 +09001619 if (strstr(kernel->long_name, "vmlinux"))
1620 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001621
Wei Li977c7a62019-02-28 17:20:03 +08001622 machine__update_kernel_mmap(machine, event->mmap.start,
Jiri Olsa05db6ff2018-02-15 13:26:31 +01001623 event->mmap.start + event->mmap.len);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001624
1625 /*
1626 * Avoid using a zero address (kptr_restrict) for the ref reloc
1627 * symbol. Effectively having zero here means that at record
1628 * time /proc/sys/kernel/kptr_restrict was non zero.
1629 */
1630 if (event->mmap.pgoff != 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001631 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1632 symbol_name,
1633 event->mmap.pgoff);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001634 }
1635
1636 if (machine__is_default_guest(machine)) {
1637 /*
1638 * preload dso of guest kernel and modules
1639 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001640 dso__load(kernel, machine__kernel_map(machine));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001641 }
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001642 } else if (perf_event__is_extra_kernel_mmap(machine, event)) {
1643 return machine__process_extra_kernel_map(machine, event);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001644 }
1645 return 0;
1646out_problem:
1647 return -1;
1648}
1649
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001650int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001651 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001652 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001653{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001654 struct thread *thread;
1655 struct map *map;
Arnaldo Carvalho de Melo4a7380a2019-11-19 12:40:29 -03001656 struct dso_id dso_id = {
1657 .maj = event->mmap2.maj,
1658 .min = event->mmap2.min,
1659 .ino = event->mmap2.ino,
1660 .ino_generation = event->mmap2.ino_generation,
1661 };
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001662 int ret = 0;
1663
1664 if (dump_trace)
1665 perf_event__fprintf_mmap2(event, stdout);
1666
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001667 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1668 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001669 ret = machine__process_kernel_mmap_event(machine, event);
1670 if (ret < 0)
1671 goto out_problem;
1672 return 0;
1673 }
1674
1675 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001676 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001677 if (thread == NULL)
1678 goto out_problem;
1679
Adrian Hunter2a030682014-07-22 16:17:53 +03001680 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001681 event->mmap2.len, event->mmap2.pgoff,
Arnaldo Carvalho de Melo4a7380a2019-11-19 12:40:29 -03001682 &dso_id, event->mmap2.prot,
Don Zickus7ef80702014-05-19 15:13:49 -04001683 event->mmap2.flags,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001684 event->mmap2.filename, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001685
1686 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001687 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001688
He Kuang8132a2a2016-06-03 03:33:13 +00001689 ret = thread__insert_map(thread, map);
1690 if (ret)
1691 goto out_problem_insert;
1692
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001693 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001694 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001695 return 0;
1696
He Kuang8132a2a2016-06-03 03:33:13 +00001697out_problem_insert:
1698 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001699out_problem_map:
1700 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001701out_problem:
1702 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1703 return 0;
1704}
1705
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001706int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001707 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001708{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001709 struct thread *thread;
1710 struct map *map;
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001711 u32 prot = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001712 int ret = 0;
1713
1714 if (dump_trace)
1715 perf_event__fprintf_mmap(event, stdout);
1716
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001717 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1718 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001719 ret = machine__process_kernel_mmap_event(machine, event);
1720 if (ret < 0)
1721 goto out_problem;
1722 return 0;
1723 }
1724
Adrian Hunter314add62013-08-27 11:23:03 +03001725 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001726 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001727 if (thread == NULL)
1728 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001729
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001730 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001731 prot = PROT_EXEC;
Stephane Eranianbad40912013-01-24 16:10:40 +01001732
Adrian Hunter2a030682014-07-22 16:17:53 +03001733 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001734 event->mmap.len, event->mmap.pgoff,
Arnaldo Carvalho de Melo4a7380a2019-11-19 12:40:29 -03001735 NULL, prot, 0, event->mmap.filename, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001736
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001737 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001738 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001739
He Kuang8132a2a2016-06-03 03:33:13 +00001740 ret = thread__insert_map(thread, map);
1741 if (ret)
1742 goto out_problem_insert;
1743
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001744 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001745 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001746 return 0;
1747
He Kuang8132a2a2016-06-03 03:33:13 +00001748out_problem_insert:
1749 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001750out_problem_map:
1751 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001752out_problem:
1753 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1754 return 0;
1755}
1756
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001757static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001758{
Kan Liang91e467b2017-09-10 19:23:14 -07001759 struct threads *threads = machine__threads(machine, th->tid);
1760
1761 if (threads->last_match == th)
Jiri Olsa67fda0f2018-07-19 16:33:43 +02001762 threads__set_last_match(threads, NULL);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001763
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001764 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001765 down_write(&threads->lock);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001766
1767 BUG_ON(refcount_read(&th->refcnt) == 0);
1768
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001769 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001770 RB_CLEAR_NODE(&th->rb_node);
Kan Liang91e467b2017-09-10 19:23:14 -07001771 --threads->nr;
David Ahern236a3bb2013-08-14 08:49:27 -06001772 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001773 * Move it first to the dead_threads list, then drop the reference,
1774 * if this is the last reference, then the thread__delete destructor
1775 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001776 */
Kan Liang91e467b2017-09-10 19:23:14 -07001777 list_add_tail(&th->node, &threads->dead);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001778
1779 /*
1780 * We need to do the put here because if this is the last refcount,
1781 * then we will be touching the threads->dead head when removing the
1782 * thread.
1783 */
1784 thread__put(th);
1785
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001786 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001787 up_write(&threads->lock);
David Ahern236a3bb2013-08-14 08:49:27 -06001788}
1789
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001790void machine__remove_thread(struct machine *machine, struct thread *th)
1791{
1792 return __machine__remove_thread(machine, th, true);
1793}
1794
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001795int machine__process_fork_event(struct machine *machine, union perf_event *event,
1796 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001797{
Jiri Olsad75e6092014-03-14 15:00:03 +01001798 struct thread *thread = machine__find_thread(machine,
1799 event->fork.pid,
1800 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001801 struct thread *parent = machine__findnew_thread(machine,
1802 event->fork.ppid,
1803 event->fork.ptid);
David Miller4f8f3822018-10-30 22:24:04 -07001804 bool do_maps_clone = true;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001805 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001806
Adrian Hunter5cb73342015-08-19 17:29:20 +03001807 if (dump_trace)
1808 perf_event__fprintf_task(event, stdout);
1809
1810 /*
1811 * There may be an existing thread that is not actually the parent,
1812 * either because we are processing events out of order, or because the
1813 * (fork) event that would have removed the thread was lost. Assume the
1814 * latter case and continue on as best we can.
1815 */
1816 if (parent->pid_ != (pid_t)event->fork.ppid) {
1817 dump_printf("removing erroneous parent thread %d/%d\n",
1818 parent->pid_, parent->tid);
1819 machine__remove_thread(machine, parent);
1820 thread__put(parent);
1821 parent = machine__findnew_thread(machine, event->fork.ppid,
1822 event->fork.ptid);
1823 }
1824
David Ahern236a3bb2013-08-14 08:49:27 -06001825 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001826 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001827 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001828 thread__put(thread);
1829 }
David Ahern236a3bb2013-08-14 08:49:27 -06001830
Adrian Hunter314add62013-08-27 11:23:03 +03001831 thread = machine__findnew_thread(machine, event->fork.pid,
1832 event->fork.tid);
David Miller4f8f3822018-10-30 22:24:04 -07001833 /*
1834 * When synthesizing FORK events, we are trying to create thread
1835 * objects for the already running tasks on the machine.
1836 *
1837 * Normally, for a kernel FORK event, we want to clone the parent's
1838 * maps because that is what the kernel just did.
1839 *
1840 * But when synthesizing, this should not be done. If we do, we end up
1841 * with overlapping maps as we process the sythesized MMAP2 events that
1842 * get delivered shortly thereafter.
1843 *
1844 * Use the FORK event misc flags in an internal way to signal this
1845 * situation, so we can elide the map clone when appropriate.
1846 */
1847 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1848 do_maps_clone = false;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001849
1850 if (thread == NULL || parent == NULL ||
David Miller4f8f3822018-10-30 22:24:04 -07001851 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001852 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001853 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001854 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001855 thread__put(thread);
1856 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001857
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001858 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001859}
1860
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001861int machine__process_exit_event(struct machine *machine, union perf_event *event,
1862 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001863{
Jiri Olsad75e6092014-03-14 15:00:03 +01001864 struct thread *thread = machine__find_thread(machine,
1865 event->fork.pid,
1866 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001867
1868 if (dump_trace)
1869 perf_event__fprintf_task(event, stdout);
1870
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001871 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001872 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001873 thread__put(thread);
1874 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001875
1876 return 0;
1877}
1878
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001879int machine__process_event(struct machine *machine, union perf_event *event,
1880 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001881{
1882 int ret;
1883
1884 switch (event->header.type) {
1885 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001886 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001887 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001888 ret = machine__process_mmap_event(machine, event, sample); break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301889 case PERF_RECORD_NAMESPACES:
1890 ret = machine__process_namespaces_event(machine, event, sample); break;
Namhyung Kimba78c1c2020-03-25 21:45:30 +09001891 case PERF_RECORD_CGROUP:
1892 ret = machine__process_cgroup_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001893 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001894 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001895 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001896 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001897 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001898 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001899 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001900 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001901 case PERF_RECORD_AUX:
1902 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001903 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001904 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001905 case PERF_RECORD_LOST_SAMPLES:
1906 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001907 case PERF_RECORD_SWITCH:
1908 case PERF_RECORD_SWITCH_CPU_WIDE:
1909 ret = machine__process_switch_event(machine, event); break;
Song Liu9aa0bfa2019-01-17 08:15:17 -08001910 case PERF_RECORD_KSYMBOL:
1911 ret = machine__process_ksymbol(machine, event, sample); break;
Song Liu45178a92019-01-17 08:15:18 -08001912 case PERF_RECORD_BPF_EVENT:
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03001913 ret = machine__process_bpf(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001914 default:
1915 ret = -1;
1916 break;
1917 }
1918
1919 return ret;
1920}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001921
Greg Priceb21484f2012-12-06 21:48:05 -08001922static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001923{
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001924 if (!regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001925 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001926 return 0;
1927}
1928
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001929static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001930 struct addr_map_symbol *ams,
1931 u64 ip)
1932{
1933 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001934
1935 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001936 /*
1937 * We cannot use the header.misc hint to determine whether a
1938 * branch stack address is user, kernel, guest, hypervisor.
1939 * Branches may straddle the kernel/user/hypervisor boundaries.
1940 * Thus, we have to try consecutively until we find a match
1941 * or else, the symbol is unknown
1942 */
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03001943 thread__find_cpumode_addr_location(thread, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001944
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001945 ams->addr = ip;
1946 ams->al_addr = al.addr;
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -03001947 ams->ms.maps = al.maps;
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001948 ams->ms.sym = al.sym;
1949 ams->ms.map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001950 ams->phys_addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001951}
1952
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001953static void ip__resolve_data(struct thread *thread,
Kan Liang8780fb22017-08-29 13:11:09 -04001954 u8 m, struct addr_map_symbol *ams,
1955 u64 addr, u64 phys_addr)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001956{
1957 struct addr_location al;
1958
1959 memset(&al, 0, sizeof(al));
1960
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03001961 thread__find_symbol(thread, m, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001962
Stephane Eranian98a3b322013-01-24 16:10:35 +01001963 ams->addr = addr;
1964 ams->al_addr = al.addr;
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -03001965 ams->ms.maps = al.maps;
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001966 ams->ms.sym = al.sym;
1967 ams->ms.map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001968 ams->phys_addr = phys_addr;
Stephane Eranian98a3b322013-01-24 16:10:35 +01001969}
1970
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001971struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1972 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001973{
Jiri Olsa9f874982018-03-07 16:50:06 +01001974 struct mem_info *mi = mem_info__new();
Stephane Eranian98a3b322013-01-24 16:10:35 +01001975
1976 if (!mi)
1977 return NULL;
1978
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001979 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
Kan Liang8780fb22017-08-29 13:11:09 -04001980 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1981 sample->addr, sample->phys_addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001982 mi->data_src.val = sample->data_src;
1983
1984 return mi;
1985}
1986
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03001987static char *callchain_srcline(struct map_symbol *ms, u64 ip)
Milian Wolff40a342c2017-10-09 22:32:56 +02001988{
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03001989 struct map *map = ms->map;
Milian Wolff21ac9d52017-10-19 13:38:34 +02001990 char *srcline = NULL;
Milian Wolff40a342c2017-10-09 22:32:56 +02001991
Milian Wolff21ac9d52017-10-19 13:38:34 +02001992 if (!map || callchain_param.key == CCKEY_FUNCTION)
1993 return srcline;
1994
1995 srcline = srcline__tree_find(&map->dso->srclines, ip);
1996 if (!srcline) {
1997 bool show_sym = false;
1998 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
1999
2000 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002001 ms->sym, show_sym, show_addr, ip);
Milian Wolff21ac9d52017-10-19 13:38:34 +02002002 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2003 }
2004
2005 return srcline;
Milian Wolff40a342c2017-10-09 22:32:56 +02002006}
2007
Jin Yaoc4ee0622017-08-07 21:05:15 +08002008struct iterations {
2009 int nr_loop_iter;
2010 u64 cycles;
2011};
2012
Andi Kleen37592b82014-11-12 18:05:19 -08002013static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002014 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08002015 struct symbol **parent,
2016 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002017 u8 *cpumode,
Jin Yao410024d2016-10-31 09:19:49 +08002018 u64 ip,
2019 bool branch,
2020 struct branch_flags *flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002021 struct iterations *iter,
Jin Yaob851dd42017-07-18 20:13:15 +08002022 u64 branch_from)
Andi Kleen37592b82014-11-12 18:05:19 -08002023{
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002024 struct map_symbol ms;
Andi Kleen37592b82014-11-12 18:05:19 -08002025 struct addr_location al;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002026 int nr_loop_iter = 0;
2027 u64 iter_cycles = 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002028 const char *srcline = NULL;
Andi Kleen37592b82014-11-12 18:05:19 -08002029
2030 al.filtered = 0;
2031 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002032 if (!cpumode) {
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03002033 thread__find_cpumode_addr_location(thread, ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002034 } else {
Kan Liang2e777842014-12-02 10:06:53 -05002035 if (ip >= PERF_CONTEXT_MAX) {
2036 switch (ip) {
2037 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002038 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05002039 break;
2040 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002041 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05002042 break;
2043 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002044 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05002045 break;
2046 default:
2047 pr_debug("invalid callchain context: "
2048 "%"PRId64"\n", (s64) ip);
2049 /*
2050 * It seems the callchain is corrupted.
2051 * Discard all.
2052 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002053 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05002054 return 1;
2055 }
2056 return 0;
2057 }
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002058 thread__find_symbol(thread, *cpumode, ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05002059 }
2060
Andi Kleen37592b82014-11-12 18:05:19 -08002061 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02002062 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08002063 symbol__match_regex(al.sym, &parent_regex))
2064 *parent = al.sym;
2065 else if (have_ignore_callees && root_al &&
2066 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2067 /* Treat this symbol as the root,
2068 forgetting its callees. */
2069 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002070 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08002071 }
2072 }
2073
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002074 if (symbol_conf.hide_unresolved && al.sym == NULL)
2075 return 0;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002076
2077 if (iter) {
2078 nr_loop_iter = iter->nr_loop_iter;
2079 iter_cycles = iter->cycles;
2080 }
2081
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -03002082 ms.maps = al.maps;
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002083 ms.map = al.map;
2084 ms.sym = al.sym;
2085 srcline = callchain_srcline(&ms, al.addr);
2086 return callchain_cursor_append(cursor, ip, &ms,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002087 branch, flags, nr_loop_iter,
Milian Wolff40a342c2017-10-09 22:32:56 +02002088 iter_cycles, branch_from, srcline);
Andi Kleen37592b82014-11-12 18:05:19 -08002089}
2090
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002091struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2092 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002093{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002094 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002095 const struct branch_stack *bs = sample->branch_stack;
Kan Liang42bbabe2020-02-28 08:30:00 -08002096 struct branch_entry *entries = perf_sample__branch_entries(sample);
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002097 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002098
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002099 if (!bi)
2100 return NULL;
2101
2102 for (i = 0; i < bs->nr; i++) {
Kan Liang42bbabe2020-02-28 08:30:00 -08002103 ip__resolve_ams(al->thread, &bi[i].to, entries[i].to);
2104 ip__resolve_ams(al->thread, &bi[i].from, entries[i].from);
2105 bi[i].flags = entries[i].flags;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002106 }
2107 return bi;
2108}
2109
Jin Yaoc4ee0622017-08-07 21:05:15 +08002110static void save_iterations(struct iterations *iter,
2111 struct branch_entry *be, int nr)
2112{
2113 int i;
2114
Jin Yaoa3366db02019-01-04 14:10:30 +08002115 iter->nr_loop_iter++;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002116 iter->cycles = 0;
2117
2118 for (i = 0; i < nr; i++)
2119 iter->cycles += be[i].flags.cycles;
2120}
2121
Andi Kleen8b7bad52014-11-12 18:05:20 -08002122#define CHASHSZ 127
2123#define CHASHBITS 7
2124#define NO_ENTRY 0xff
2125
2126#define PERF_MAX_BRANCH_DEPTH 127
2127
2128/* Remove loops. */
Jin Yaoc4ee0622017-08-07 21:05:15 +08002129static int remove_loops(struct branch_entry *l, int nr,
2130 struct iterations *iter)
Andi Kleen8b7bad52014-11-12 18:05:20 -08002131{
2132 int i, j, off;
2133 unsigned char chash[CHASHSZ];
2134
2135 memset(chash, NO_ENTRY, sizeof(chash));
2136
2137 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2138
2139 for (i = 0; i < nr; i++) {
2140 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2141
2142 /* no collision handling for now */
2143 if (chash[h] == NO_ENTRY) {
2144 chash[h] = i;
2145 } else if (l[chash[h]].from == l[i].from) {
2146 bool is_loop = true;
2147 /* check if it is a real loop */
2148 off = 0;
2149 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2150 if (l[j].from != l[i + off].from) {
2151 is_loop = false;
2152 break;
2153 }
2154 if (is_loop) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002155 j = nr - (i + off);
2156 if (j > 0) {
2157 save_iterations(iter + i + off,
2158 l + i, off);
2159
2160 memmove(iter + i, iter + i + off,
2161 j * sizeof(*iter));
2162
2163 memmove(l + i, l + i + off,
2164 j * sizeof(*l));
2165 }
2166
Andi Kleen8b7bad52014-11-12 18:05:20 -08002167 nr -= off;
2168 }
2169 }
2170 }
2171 return nr;
2172}
2173
Kan Liang384b6052015-01-05 13:23:05 -05002174/*
2175 * Recolve LBR callstack chain sample
2176 * Return:
2177 * 1 on success get LBR callchain information
2178 * 0 no available LBR callchain information, should try fp
2179 * negative error code on other errors.
2180 */
2181static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002182 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05002183 struct perf_sample *sample,
2184 struct symbol **parent,
2185 struct addr_location *root_al,
2186 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002187{
Kan Liang384b6052015-01-05 13:23:05 -05002188 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002189 int chain_nr = min(max_stack, (int)chain->nr), i;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002190 u8 cpumode = PERF_RECORD_MISC_USER;
Jin Yaob851dd42017-07-18 20:13:15 +08002191 u64 ip, branch_from = 0;
Kan Liang384b6052015-01-05 13:23:05 -05002192
2193 for (i = 0; i < chain_nr; i++) {
2194 if (chain->ips[i] == PERF_CONTEXT_USER)
2195 break;
2196 }
2197
2198 /* LBR only affects the user callchain */
2199 if (i != chain_nr) {
2200 struct branch_stack *lbr_stack = sample->branch_stack;
Kan Liang42bbabe2020-02-28 08:30:00 -08002201 struct branch_entry *entries = perf_sample__branch_entries(sample);
Jin Yao410024d2016-10-31 09:19:49 +08002202 int lbr_nr = lbr_stack->nr, j, k;
2203 bool branch;
2204 struct branch_flags *flags;
Kan Liang384b6052015-01-05 13:23:05 -05002205 /*
2206 * LBR callstack can only get user call chain.
2207 * The mix_chain_nr is kernel call chain
2208 * number plus LBR user call chain number.
2209 * i is kernel call chain number,
2210 * 1 is PERF_CONTEXT_USER,
2211 * lbr_nr + 1 is the user call chain number.
2212 * For details, please refer to the comments
2213 * in callchain__printf
2214 */
2215 int mix_chain_nr = i + 1 + lbr_nr + 1;
2216
Kan Liang384b6052015-01-05 13:23:05 -05002217 for (j = 0; j < mix_chain_nr; j++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002218 int err;
Jin Yao410024d2016-10-31 09:19:49 +08002219 branch = false;
2220 flags = NULL;
2221
Kan Liang384b6052015-01-05 13:23:05 -05002222 if (callchain_param.order == ORDER_CALLEE) {
2223 if (j < i + 1)
2224 ip = chain->ips[j];
Jin Yao410024d2016-10-31 09:19:49 +08002225 else if (j > i + 1) {
2226 k = j - i - 2;
Kan Liang42bbabe2020-02-28 08:30:00 -08002227 ip = entries[k].from;
Jin Yao410024d2016-10-31 09:19:49 +08002228 branch = true;
Kan Liang42bbabe2020-02-28 08:30:00 -08002229 flags = &entries[k].flags;
Jin Yao410024d2016-10-31 09:19:49 +08002230 } else {
Kan Liang42bbabe2020-02-28 08:30:00 -08002231 ip = entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08002232 branch = true;
Kan Liang42bbabe2020-02-28 08:30:00 -08002233 flags = &entries[0].flags;
2234 branch_from = entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08002235 }
Kan Liang384b6052015-01-05 13:23:05 -05002236 } else {
Jin Yao410024d2016-10-31 09:19:49 +08002237 if (j < lbr_nr) {
2238 k = lbr_nr - j - 1;
Kan Liang42bbabe2020-02-28 08:30:00 -08002239 ip = entries[k].from;
Jin Yao410024d2016-10-31 09:19:49 +08002240 branch = true;
Kan Liang42bbabe2020-02-28 08:30:00 -08002241 flags = &entries[k].flags;
Jin Yao410024d2016-10-31 09:19:49 +08002242 }
Kan Liang384b6052015-01-05 13:23:05 -05002243 else if (j > lbr_nr)
2244 ip = chain->ips[i + 1 - (j - lbr_nr)];
Jin Yao410024d2016-10-31 09:19:49 +08002245 else {
Kan Liang42bbabe2020-02-28 08:30:00 -08002246 ip = entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08002247 branch = true;
Kan Liang42bbabe2020-02-28 08:30:00 -08002248 flags = &entries[0].flags;
2249 branch_from = entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08002250 }
Kan Liang384b6052015-01-05 13:23:05 -05002251 }
2252
Jin Yao410024d2016-10-31 09:19:49 +08002253 err = add_callchain_ip(thread, cursor, parent,
2254 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002255 branch, flags, NULL,
Jin Yaob851dd42017-07-18 20:13:15 +08002256 branch_from);
Kan Liang384b6052015-01-05 13:23:05 -05002257 if (err)
2258 return (err < 0) ? err : 0;
2259 }
2260 return 1;
2261 }
2262
2263 return 0;
2264}
2265
David S. Millere9024d52018-10-30 12:12:26 -03002266static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2267 struct callchain_cursor *cursor,
2268 struct symbol **parent,
2269 struct addr_location *root_al,
2270 u8 *cpumode, int ent)
2271{
2272 int err = 0;
2273
2274 while (--ent >= 0) {
2275 u64 ip = chain->ips[ent];
2276
2277 if (ip >= PERF_CONTEXT_MAX) {
2278 err = add_callchain_ip(thread, cursor, parent,
2279 root_al, cpumode, ip,
2280 false, NULL, NULL, 0);
2281 break;
2282 }
2283 }
2284 return err;
2285}
2286
Kan Liang384b6052015-01-05 13:23:05 -05002287static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002288 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002289 struct evsel *evsel,
Kan Liang384b6052015-01-05 13:23:05 -05002290 struct perf_sample *sample,
2291 struct symbol **parent,
2292 struct addr_location *root_al,
2293 int max_stack)
2294{
2295 struct branch_stack *branch = sample->branch_stack;
Kan Liang42bbabe2020-02-28 08:30:00 -08002296 struct branch_entry *entries = perf_sample__branch_entries(sample);
Kan Liang384b6052015-01-05 13:23:05 -05002297 struct ip_callchain *chain = sample->callchain;
Jin Yaob49a8212017-05-08 18:43:02 +08002298 int chain_nr = 0;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002299 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002300 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08002301 int skip_idx = -1;
2302 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002303
Jin Yaob49a8212017-05-08 18:43:02 +08002304 if (chain)
2305 chain_nr = chain->nr;
2306
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03002307 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002308 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05002309 root_al, max_stack);
2310 if (err)
2311 return (err < 0) ? err : 0;
2312 }
2313
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002314 /*
2315 * Based on DWARF debug information, some architectures skip
2316 * a callchain entry saved by the kernel.
2317 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002318 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002319
Andi Kleen8b7bad52014-11-12 18:05:20 -08002320 /*
2321 * Add branches to call stack for easier browsing. This gives
2322 * more context for a sample than just the callers.
2323 *
2324 * This uses individual histograms of paths compared to the
2325 * aggregated histograms the normal LBR mode uses.
2326 *
2327 * Limitations for now:
2328 * - No extra filters
2329 * - No annotations (should annotate somehow)
2330 */
2331
2332 if (branch && callchain_param.branch_callstack) {
2333 int nr = min(max_stack, (int)branch->nr);
2334 struct branch_entry be[nr];
Jin Yaoc4ee0622017-08-07 21:05:15 +08002335 struct iterations iter[nr];
Andi Kleen8b7bad52014-11-12 18:05:20 -08002336
2337 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2338 pr_warning("corrupted branch chain. skipping...\n");
2339 goto check_calls;
2340 }
2341
2342 for (i = 0; i < nr; i++) {
2343 if (callchain_param.order == ORDER_CALLEE) {
Kan Liang42bbabe2020-02-28 08:30:00 -08002344 be[i] = entries[i];
Jin Yaob49a8212017-05-08 18:43:02 +08002345
2346 if (chain == NULL)
2347 continue;
2348
Andi Kleen8b7bad52014-11-12 18:05:20 -08002349 /*
2350 * Check for overlap into the callchain.
2351 * The return address is one off compared to
2352 * the branch entry. To adjust for this
2353 * assume the calling instruction is not longer
2354 * than 8 bytes.
2355 */
2356 if (i == skip_idx ||
2357 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2358 first_call++;
2359 else if (be[i].from < chain->ips[first_call] &&
2360 be[i].from >= chain->ips[first_call] - 8)
2361 first_call++;
2362 } else
Kan Liang42bbabe2020-02-28 08:30:00 -08002363 be[i] = entries[branch->nr - i - 1];
Andi Kleen8b7bad52014-11-12 18:05:20 -08002364 }
2365
Jin Yaoc4ee0622017-08-07 21:05:15 +08002366 memset(iter, 0, sizeof(struct iterations) * nr);
2367 nr = remove_loops(be, nr, iter);
Jin Yao410024d2016-10-31 09:19:49 +08002368
Andi Kleen8b7bad52014-11-12 18:05:20 -08002369 for (i = 0; i < nr; i++) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002370 err = add_callchain_ip(thread, cursor, parent,
2371 root_al,
2372 NULL, be[i].to,
2373 true, &be[i].flags,
2374 NULL, be[i].from);
Jin Yao410024d2016-10-31 09:19:49 +08002375
Andi Kleen8b7bad52014-11-12 18:05:20 -08002376 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002377 err = add_callchain_ip(thread, cursor, parent, root_al,
Jin Yao410024d2016-10-31 09:19:49 +08002378 NULL, be[i].from,
2379 true, &be[i].flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002380 &iter[i], 0);
Andi Kleen8b7bad52014-11-12 18:05:20 -08002381 if (err == -EINVAL)
2382 break;
2383 if (err)
2384 return err;
2385 }
Jin Yaob49a8212017-05-08 18:43:02 +08002386
2387 if (chain_nr == 0)
2388 return 0;
2389
Andi Kleen8b7bad52014-11-12 18:05:20 -08002390 chain_nr -= nr;
2391 }
2392
2393check_calls:
Adrian Hunteraceb9822019-11-14 16:25:38 +02002394 if (chain && callchain_param.order != ORDER_CALLEE) {
David S. Millere9024d52018-10-30 12:12:26 -03002395 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2396 &cpumode, chain->nr - first_call);
2397 if (err)
2398 return (err < 0) ? err : 0;
2399 }
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002400 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002401 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002402 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002403
2404 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002405 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002406 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002407 j = chain->nr - i - 1;
2408
2409#ifdef HAVE_SKIP_CALLCHAIN_IDX
2410 if (j == skip_idx)
2411 continue;
2412#endif
2413 ip = chain->ips[j];
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002414 if (ip < PERF_CONTEXT_MAX)
2415 ++nr_entries;
David S. Millere9024d52018-10-30 12:12:26 -03002416 else if (callchain_param.order != ORDER_CALLEE) {
2417 err = find_prev_cpumode(chain, thread, cursor, parent,
2418 root_al, &cpumode, j);
2419 if (err)
2420 return (err < 0) ? err : 0;
2421 continue;
2422 }
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002423
Jin Yao410024d2016-10-31 09:19:49 +08002424 err = add_callchain_ip(thread, cursor, parent,
2425 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002426 false, NULL, NULL, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002427
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002428 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05002429 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002430 }
2431
2432 return 0;
2433}
2434
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002435static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
Milian Wolff11ea2512017-10-09 22:32:59 +02002436{
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002437 struct symbol *sym = ms->sym;
2438 struct map *map = ms->map;
Milian Wolff11ea2512017-10-09 22:32:59 +02002439 struct inline_node *inline_node;
2440 struct inline_list *ilist;
2441 u64 addr;
Milian Wolffb38775c2017-10-19 13:38:33 +02002442 int ret = 1;
Milian Wolff11ea2512017-10-09 22:32:59 +02002443
2444 if (!symbol_conf.inline_name || !map || !sym)
Milian Wolffb38775c2017-10-19 13:38:33 +02002445 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002446
Milian Wolff7a8a8fc2018-09-26 15:52:06 +02002447 addr = map__map_ip(map, ip);
2448 addr = map__rip_2objdump(map, addr);
Milian Wolff11ea2512017-10-09 22:32:59 +02002449
2450 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2451 if (!inline_node) {
2452 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2453 if (!inline_node)
Milian Wolffb38775c2017-10-19 13:38:33 +02002454 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002455 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2456 }
2457
2458 list_for_each_entry(ilist, &inline_node->val, list) {
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002459 struct map_symbol ilist_ms = {
Arnaldo Carvalho de Melo77b91c12019-11-29 15:47:51 -03002460 .maps = ms->maps,
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002461 .map = map,
2462 .sym = ilist->symbol,
2463 };
2464 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
Milian Wolffb38775c2017-10-19 13:38:33 +02002465 NULL, 0, 0, 0, ilist->srcline);
Milian Wolff11ea2512017-10-09 22:32:59 +02002466
2467 if (ret != 0)
2468 return ret;
2469 }
2470
Milian Wolffb38775c2017-10-19 13:38:33 +02002471 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002472}
2473
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002474static int unwind_entry(struct unwind_entry *entry, void *arg)
2475{
2476 struct callchain_cursor *cursor = arg;
Milian Wolff40a342c2017-10-09 22:32:56 +02002477 const char *srcline = NULL;
Milian Wolffff4ce282018-09-26 15:52:05 +02002478 u64 addr = entry->ip;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002479
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002480 if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002481 return 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002482
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002483 if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
Milian Wolff11ea2512017-10-09 22:32:59 +02002484 return 0;
2485
Sandipan Das2a9d5052018-07-03 17:35:55 +05302486 /*
2487 * Convert entry->ip from a virtual address to an offset in
2488 * its corresponding binary.
2489 */
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002490 if (entry->ms.map)
2491 addr = map__map_ip(entry->ms.map, entry->ip);
Sandipan Das2a9d5052018-07-03 17:35:55 +05302492
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002493 srcline = callchain_srcline(&entry->ms, addr);
2494 return callchain_cursor_append(cursor, entry->ip, &entry->ms,
Milian Wolff40a342c2017-10-09 22:32:56 +02002495 false, NULL, 0, 0, 0, srcline);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002496}
2497
Chris Phlipot9919a652016-04-28 01:19:06 -07002498static int thread__resolve_callchain_unwind(struct thread *thread,
2499 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002500 struct evsel *evsel,
Chris Phlipot9919a652016-04-28 01:19:06 -07002501 struct perf_sample *sample,
2502 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002503{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002504 /* Can we do dwarf post unwind? */
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002505 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2506 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002507 return 0;
2508
2509 /* Bail out if nothing was captured. */
2510 if ((!sample->user_regs.regs) ||
2511 (!sample->user_stack.size))
2512 return 0;
2513
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002514 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01002515 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07002516}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002517
Chris Phlipot9919a652016-04-28 01:19:06 -07002518int thread__resolve_callchain(struct thread *thread,
2519 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002520 struct evsel *evsel,
Chris Phlipot9919a652016-04-28 01:19:06 -07002521 struct perf_sample *sample,
2522 struct symbol **parent,
2523 struct addr_location *root_al,
2524 int max_stack)
2525{
2526 int ret = 0;
2527
Jiri Olsa914eb9c2017-08-06 16:39:39 +02002528 callchain_cursor_reset(cursor);
Chris Phlipot9919a652016-04-28 01:19:06 -07002529
2530 if (callchain_param.order == ORDER_CALLEE) {
2531 ret = thread__resolve_callchain_sample(thread, cursor,
2532 evsel, sample,
2533 parent, root_al,
2534 max_stack);
2535 if (ret)
2536 return ret;
2537 ret = thread__resolve_callchain_unwind(thread, cursor,
2538 evsel, sample,
2539 max_stack);
2540 } else {
2541 ret = thread__resolve_callchain_unwind(thread, cursor,
2542 evsel, sample,
2543 max_stack);
2544 if (ret)
2545 return ret;
2546 ret = thread__resolve_callchain_sample(thread, cursor,
2547 evsel, sample,
2548 parent, root_al,
2549 max_stack);
2550 }
2551
2552 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002553}
David Ahern35feee12013-09-28 13:12:58 -06002554
2555int machine__for_each_thread(struct machine *machine,
2556 int (*fn)(struct thread *thread, void *p),
2557 void *priv)
2558{
Kan Liang91e467b2017-09-10 19:23:14 -07002559 struct threads *threads;
David Ahern35feee12013-09-28 13:12:58 -06002560 struct rb_node *nd;
2561 struct thread *thread;
2562 int rc = 0;
Kan Liang91e467b2017-09-10 19:23:14 -07002563 int i;
David Ahern35feee12013-09-28 13:12:58 -06002564
Kan Liang91e467b2017-09-10 19:23:14 -07002565 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
2566 threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08002567 for (nd = rb_first_cached(&threads->entries); nd;
2568 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -07002569 thread = rb_entry(nd, struct thread, rb_node);
2570 rc = fn(thread, priv);
2571 if (rc != 0)
2572 return rc;
2573 }
David Ahern35feee12013-09-28 13:12:58 -06002574
Kan Liang91e467b2017-09-10 19:23:14 -07002575 list_for_each_entry(thread, &threads->dead, node) {
2576 rc = fn(thread, priv);
2577 if (rc != 0)
2578 return rc;
2579 }
David Ahern35feee12013-09-28 13:12:58 -06002580 }
2581 return rc;
2582}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002583
Adrian Huntera5499b32015-05-29 16:33:30 +03002584int machines__for_each_thread(struct machines *machines,
2585 int (*fn)(struct thread *thread, void *p),
2586 void *priv)
2587{
2588 struct rb_node *nd;
2589 int rc = 0;
2590
2591 rc = machine__for_each_thread(&machines->host, fn, priv);
2592 if (rc != 0)
2593 return rc;
2594
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08002595 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntera5499b32015-05-29 16:33:30 +03002596 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2597
2598 rc = machine__for_each_thread(machine, fn, priv);
2599 if (rc != 0)
2600 return rc;
2601 }
2602 return rc;
2603}
2604
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002605pid_t machine__get_current_tid(struct machine *machine, int cpu)
2606{
Kyle Meyer8c727462019-08-27 16:43:50 -05002607 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
2608
2609 if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002610 return -1;
2611
2612 return machine->current_tid[cpu];
2613}
2614
2615int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2616 pid_t tid)
2617{
2618 struct thread *thread;
Kyle Meyer8c727462019-08-27 16:43:50 -05002619 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002620
2621 if (cpu < 0)
2622 return -EINVAL;
2623
2624 if (!machine->current_tid) {
2625 int i;
2626
Kyle Meyer8c727462019-08-27 16:43:50 -05002627 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002628 if (!machine->current_tid)
2629 return -ENOMEM;
Kyle Meyer8c727462019-08-27 16:43:50 -05002630 for (i = 0; i < nr_cpus; i++)
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002631 machine->current_tid[i] = -1;
2632 }
2633
Kyle Meyer8c727462019-08-27 16:43:50 -05002634 if (cpu >= nr_cpus) {
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002635 pr_err("Requested CPU %d too large. ", cpu);
2636 pr_err("Consider raising MAX_NR_CPUS\n");
2637 return -EINVAL;
2638 }
2639
2640 machine->current_tid[cpu] = tid;
2641
2642 thread = machine__findnew_thread(machine, pid, tid);
2643 if (!thread)
2644 return -ENOMEM;
2645
2646 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002647 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002648
2649 return 0;
2650}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002651
Adrian Hunterdbbd34a2018-05-17 12:21:53 +03002652/*
2653 * Compares the raw arch string. N.B. see instead perf_env__arch() if a
2654 * normalized arch is needed.
2655 */
2656bool machine__is(struct machine *machine, const char *arch)
2657{
2658 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
2659}
2660
Adrian Hunter9cecca32018-05-22 13:54:32 +03002661int machine__nr_cpus_avail(struct machine *machine)
2662{
2663 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
2664}
2665
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002666int machine__get_kernel_start(struct machine *machine)
2667{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002668 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002669 int err = 0;
2670
2671 /*
2672 * The only addresses above 2^63 are kernel addresses of a 64-bit
2673 * kernel. Note that addresses are unsigned so that on a 32-bit system
2674 * all addresses including kernel addresses are less than 2^32. In
2675 * that case (32-bit system), if the kernel mapping is unknown, all
2676 * addresses will be assumed to be in user space - see
2677 * machine__kernel_ip().
2678 */
2679 machine->kernel_start = 1ULL << 63;
2680 if (map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002681 err = map__load(map);
Adrian Hunter19422a92018-05-17 12:21:54 +03002682 /*
2683 * On x86_64, PTI entry trampolines are less than the
2684 * start of kernel text, but still above 2^63. So leave
2685 * kernel_start = 1ULL << 63 for x86_64.
2686 */
2687 if (!err && !machine__is(machine, "x86_64"))
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002688 machine->kernel_start = map->start;
2689 }
2690 return err;
2691}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002692
Adrian Hunter8e80ad92018-11-06 23:07:10 +02002693u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
2694{
2695 u8 addr_cpumode = cpumode;
2696 bool kernel_ip;
2697
2698 if (!machine->single_address_space)
2699 goto out;
2700
2701 kernel_ip = machine__kernel_ip(machine, addr);
2702 switch (cpumode) {
2703 case PERF_RECORD_MISC_KERNEL:
2704 case PERF_RECORD_MISC_USER:
2705 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
2706 PERF_RECORD_MISC_USER;
2707 break;
2708 case PERF_RECORD_MISC_GUEST_KERNEL:
2709 case PERF_RECORD_MISC_GUEST_USER:
2710 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
2711 PERF_RECORD_MISC_GUEST_USER;
2712 break;
2713 default:
2714 break;
2715 }
2716out:
2717 return addr_cpumode;
2718}
2719
Arnaldo Carvalho de Melo0e3149f2019-11-19 18:44:22 -03002720struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id)
2721{
2722 return dsos__findnew_id(&machine->dsos, filename, id);
2723}
2724
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002725struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2726{
Arnaldo Carvalho de Melo0e3149f2019-11-19 18:44:22 -03002727 return machine__findnew_dso_id(machine, filename, NULL);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002728}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002729
2730char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2731{
2732 struct machine *machine = vmachine;
2733 struct map *map;
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03002734 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002735
2736 if (sym == NULL)
2737 return NULL;
2738
2739 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2740 *addrp = map->unmap_ip(map, sym->start);
2741 return sym->name;
2742}