blob: d08c7285c708f5874234fdbeff248cdf281cba0f [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 Melo11246c72014-10-21 17:29:02 -030089 map_groups__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 Melo69d25912012-11-09 11:32:52 -0300220 map_groups__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
415 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300416 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300417
418 if (!leader->mg)
419 goto out_err;
420
421 if (th->mg == leader->mg)
422 return;
423
424 if (th->mg) {
425 /*
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 */
430 if (!map_groups__empty(th->mg))
431 pr_err("Discarding thread maps for %d:%d\n",
432 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300433 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300434 }
435
436 th->mg = map_groups__get(leader->mg);
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 /*
539 * We have to initialize map_groups separately
540 * after rb tree is updated.
541 *
542 * The reason is that we call machine__findnew_thread
543 * within thread__init_map_groups to find the thread
544 * leader and that would screwed the rb tree.
545 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300546 if (thread__init_map_groups(th, machine)) {
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800547 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300548 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300549 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200550 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300551 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300552 /*
553 * It is now in the rbtree, get a ref
554 */
555 thread__get(th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200556 threads__set_last_match(threads, th);
Kan Liang91e467b2017-09-10 19:23:14 -0700557 ++threads->nr;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300558 }
559
560 return th;
561}
562
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300563struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
564{
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300565 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300566}
567
Adrian Hunter314add62013-08-27 11:23:03 +0300568struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
569 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300570{
Kan Liang91e467b2017-09-10 19:23:14 -0700571 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300572 struct thread *th;
573
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300574 down_write(&threads->lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300575 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300576 up_write(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300577 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300578}
579
Jiri Olsad75e6092014-03-14 15:00:03 +0100580struct thread *machine__find_thread(struct machine *machine, pid_t pid,
581 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300582{
Kan Liang91e467b2017-09-10 19:23:14 -0700583 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300584 struct thread *th;
Kan Liang91e467b2017-09-10 19:23:14 -0700585
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300586 down_read(&threads->lock);
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300587 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300588 up_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300589 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300590}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300591
Adrian Huntercfe1c412014-07-31 09:00:45 +0300592struct comm *machine__thread_exec_comm(struct machine *machine,
593 struct thread *thread)
594{
595 if (machine->comm_exec)
596 return thread__exec_comm(thread);
597 else
598 return thread__comm(thread);
599}
600
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200601int machine__process_comm_event(struct machine *machine, union perf_event *event,
602 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300603{
Adrian Hunter314add62013-08-27 11:23:03 +0300604 struct thread *thread = machine__findnew_thread(machine,
605 event->comm.pid,
606 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300607 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300608 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300609
Adrian Huntercfe1c412014-07-31 09:00:45 +0300610 if (exec)
611 machine->comm_exec = true;
612
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300613 if (dump_trace)
614 perf_event__fprintf_comm(event, stdout);
615
Adrian Hunter65de51f2014-07-31 09:00:44 +0300616 if (thread == NULL ||
617 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300618 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300619 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300620 }
621
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300622 thread__put(thread);
623
624 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300625}
626
Hari Bathinif3b36142017-03-08 02:11:43 +0530627int machine__process_namespaces_event(struct machine *machine __maybe_unused,
628 union perf_event *event,
629 struct perf_sample *sample __maybe_unused)
630{
631 struct thread *thread = machine__findnew_thread(machine,
632 event->namespaces.pid,
633 event->namespaces.tid);
634 int err = 0;
635
636 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
637 "\nWARNING: kernel seems to support more namespaces than perf"
638 " tool.\nTry updating the perf tool..\n\n");
639
640 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
641 "\nWARNING: perf tool seems to support more namespaces than"
642 " the kernel.\nTry updating the kernel..\n\n");
643
644 if (dump_trace)
645 perf_event__fprintf_namespaces(event, stdout);
646
647 if (thread == NULL ||
648 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
649 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
650 err = -1;
651 }
652
653 thread__put(thread);
654
655 return err;
656}
657
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300658int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200659 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300660{
Jiri Olsa5290ed62019-08-25 20:17:46 +0200661 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300662 event->lost.id, event->lost.lost);
663 return 0;
664}
665
Kan Liangc4937a92015-05-10 15:13:15 -0400666int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
667 union perf_event *event, struct perf_sample *sample)
668{
Jiri Olsaa2e254d2019-08-25 20:17:47 +0200669 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
Kan Liangc4937a92015-05-10 15:13:15 -0400670 sample->id, event->lost_samples.lost);
671 return 0;
672}
673
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300674static struct dso *machine__findnew_module_dso(struct machine *machine,
675 struct kmod_path *m,
676 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100677{
678 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100679
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300680 down_write(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300681
682 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100683 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300684 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100685 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300686 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100687
Namhyung Kim6b335e82017-05-31 21:01:04 +0900688 dso__set_module_info(dso, m, machine);
Jiri Olsaca333802015-02-17 17:29:57 +0100689 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100690 }
691
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300692 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300693out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300694 up_write(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100695 return dso;
696}
697
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300698int machine__process_aux_event(struct machine *machine __maybe_unused,
699 union perf_event *event)
700{
701 if (dump_trace)
702 perf_event__fprintf_aux(event, stdout);
703 return 0;
704}
705
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300706int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
707 union perf_event *event)
708{
709 if (dump_trace)
710 perf_event__fprintf_itrace_start(event, stdout);
711 return 0;
712}
713
Adrian Hunter02860392015-07-21 12:44:03 +0300714int machine__process_switch_event(struct machine *machine __maybe_unused,
715 union perf_event *event)
716{
717 if (dump_trace)
718 perf_event__fprintf_switch(event, stdout);
719 return 0;
720}
721
Song Liu9aa0bfa2019-01-17 08:15:17 -0800722static int machine__process_ksymbol_register(struct machine *machine,
723 union perf_event *event,
724 struct perf_sample *sample __maybe_unused)
725{
726 struct symbol *sym;
727 struct map *map;
728
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300729 map = map_groups__find(&machine->kmaps, event->ksymbol.addr);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800730 if (!map) {
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300731 map = dso__new_map(event->ksymbol.name);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800732 if (!map)
733 return -ENOMEM;
734
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300735 map->start = event->ksymbol.addr;
736 map->end = map->start + event->ksymbol.len;
Song Liu9aa0bfa2019-01-17 08:15:17 -0800737 map_groups__insert(&machine->kmaps, map);
738 }
739
Jiri Olsa8529f2e2019-05-08 15:20:04 +0200740 sym = symbol__new(map->map_ip(map, map->start),
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300741 event->ksymbol.len,
742 0, 0, event->ksymbol.name);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800743 if (!sym)
744 return -ENOMEM;
745 dso__insert_symbol(map->dso, sym);
746 return 0;
747}
748
749static int machine__process_ksymbol_unregister(struct machine *machine,
750 union perf_event *event,
751 struct perf_sample *sample __maybe_unused)
752{
753 struct map *map;
754
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300755 map = map_groups__find(&machine->kmaps, event->ksymbol.addr);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800756 if (map)
757 map_groups__remove(&machine->kmaps, map);
758
759 return 0;
760}
761
762int machine__process_ksymbol(struct machine *machine __maybe_unused,
763 union perf_event *event,
764 struct perf_sample *sample)
765{
766 if (dump_trace)
767 perf_event__fprintf_ksymbol(event, stdout);
768
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300769 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
Song Liu9aa0bfa2019-01-17 08:15:17 -0800770 return machine__process_ksymbol_unregister(machine, event,
771 sample);
772 return machine__process_ksymbol_register(machine, event, sample);
773}
774
Wang Nanc03d5182015-11-26 03:59:57 +0000775static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
776{
777 const char *dup_filename;
778
779 if (!filename || !dso || !dso->long_name)
780 return;
781 if (dso->long_name[0] != '[')
782 return;
783 if (!strchr(filename, '/'))
784 return;
785
786 dup_filename = strdup(filename);
787 if (!dup_filename)
788 return;
789
Wang Nan5dcf16d2015-12-07 02:36:25 +0000790 dso__set_long_name(dso, dup_filename, true);
Wang Nanc03d5182015-11-26 03:59:57 +0000791}
792
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300793struct map *machine__findnew_module_map(struct machine *machine, u64 start,
794 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300795{
Jiri Olsaca333802015-02-17 17:29:57 +0100796 struct map *map = NULL;
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900797 struct dso *dso = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100798 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300799
Jiri Olsaca333802015-02-17 17:29:57 +0100800 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300801 return NULL;
802
Arnaldo Carvalho de Melo83cf7742018-04-24 12:16:09 -0300803 map = map_groups__find_by_name(&machine->kmaps, m.name);
Wang Nanc03d5182015-11-26 03:59:57 +0000804 if (map) {
805 /*
806 * If the map's dso is an offline module, give dso__load()
807 * a chance to find the file path of that module by fixing
808 * long_name.
809 */
810 dso__adjust_kmod_long_name(map->dso, filename);
Jiri Olsabc84f462015-02-17 17:31:18 +0100811 goto out;
Wang Nanc03d5182015-11-26 03:59:57 +0000812 }
Jiri Olsabc84f462015-02-17 17:31:18 +0100813
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300814 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100815 if (dso == NULL)
816 goto out;
817
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300818 map = map__new2(start, dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300819 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100820 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300821
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300822 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100823
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900824 /* Put the map here because map_groups__insert alread got it */
825 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100826out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900827 /* put the dso here, corresponding to machine__findnew_module_dso */
828 dso__put(dso);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300829 zfree(&m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300830 return map;
831}
832
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300833size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300834{
835 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300836 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300837
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800838 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300839 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300840 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300841 }
842
843 return ret;
844}
845
Waiman Long8fa7d872014-09-29 16:07:28 -0400846size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300847 bool (skip)(struct dso *dso, int parm), int parm)
848{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300849 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300850}
851
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300852size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300853 bool (skip)(struct dso *dso, int parm), int parm)
854{
855 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300856 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300857
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800858 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300859 struct machine *pos = rb_entry(nd, struct machine, rb_node);
860 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
861 }
862 return ret;
863}
864
865size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
866{
867 int i;
868 size_t printed = 0;
Arnaldo Carvalho de Melo93730f82019-10-31 15:22:24 -0300869 struct dso *kdso = machine__kernel_dso(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300870
871 if (kdso->has_build_id) {
872 char filename[PATH_MAX];
Krister Johansend2396992017-07-05 18:48:13 -0700873 if (dso__build_id_filename(kdso, filename, sizeof(filename),
874 false))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300875 printed += fprintf(fp, "[0] %s\n", filename);
876 }
877
878 for (i = 0; i < vmlinux_path__nr_entries; ++i)
879 printed += fprintf(fp, "[%d] %s\n",
880 i + kdso->has_build_id, vmlinux_path[i]);
881
882 return printed;
883}
884
885size_t machine__fprintf(struct machine *machine, FILE *fp)
886{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300887 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700888 size_t ret;
889 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300890
Kan Liang91e467b2017-09-10 19:23:14 -0700891 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
892 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300893
894 down_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300895
Kan Liang91e467b2017-09-10 19:23:14 -0700896 ret = fprintf(fp, "Threads: %u\n", threads->nr);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300897
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800898 for (nd = rb_first_cached(&threads->entries); nd;
899 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -0700900 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300901
Kan Liang91e467b2017-09-10 19:23:14 -0700902 ret += thread__fprintf(pos, fp);
903 }
904
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300905 up_read(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300906 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300907 return ret;
908}
909
910static struct dso *machine__get_kernel(struct machine *machine)
911{
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100912 const char *vmlinux_name = machine->mmap_name;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300913 struct dso *kernel;
914
915 if (machine__is_host(machine)) {
Jiri Olsac1925242018-03-12 16:24:06 +0100916 if (symbol_conf.vmlinux_name)
917 vmlinux_name = symbol_conf.vmlinux_name;
918
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300919 kernel = machine__findnew_kernel(machine, vmlinux_name,
920 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300921 } else {
Jiri Olsac1925242018-03-12 16:24:06 +0100922 if (symbol_conf.default_guest_vmlinux_name)
923 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
924
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300925 kernel = machine__findnew_kernel(machine, vmlinux_name,
926 "[guest.kernel]",
927 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300928 }
929
930 if (kernel != NULL && (!kernel->has_build_id))
931 dso__read_running_kernel_build_id(kernel, machine);
932
933 return kernel;
934}
935
936struct process_args {
937 u64 start;
938};
939
Adrian Hunter1c5aae72018-05-22 13:54:36 +0300940void machine__get_kallsyms_filename(struct machine *machine, char *buf,
941 size_t bufsz)
Adrian Hunter15a0a872014-01-29 16:14:38 +0200942{
943 if (machine__is_default_guest(machine))
944 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
945 else
946 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
947}
948
Simon Quea93f0e52014-06-16 11:32:09 -0700949const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
950
951/* Figure out the start address of kernel map from /proc/kallsyms.
952 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
953 * symbol_name if it's not that important.
954 */
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300955static int machine__get_running_kernel_start(struct machine *machine,
Jiri Olsaed9adb22019-05-08 15:20:03 +0200956 const char **symbol_name,
957 u64 *start, u64 *end)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300958{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200959 char filename[PATH_MAX];
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300960 int i, err = -1;
Simon Quea93f0e52014-06-16 11:32:09 -0700961 const char *name;
962 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300963
Adrian Hunter15a0a872014-01-29 16:14:38 +0200964 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300965
966 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
967 return 0;
968
Simon Quea93f0e52014-06-16 11:32:09 -0700969 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300970 err = kallsyms__get_function_start(filename, name, &addr);
971 if (!err)
Simon Quea93f0e52014-06-16 11:32:09 -0700972 break;
973 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300974
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300975 if (err)
976 return -1;
977
Simon Quea93f0e52014-06-16 11:32:09 -0700978 if (symbol_name)
979 *symbol_name = name;
980
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300981 *start = addr;
Jiri Olsaed9adb22019-05-08 15:20:03 +0200982
983 err = kallsyms__get_function_start(filename, "_etext", &addr);
984 if (!err)
985 *end = addr;
986
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300987 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300988}
989
Adrian Hunter1c5aae72018-05-22 13:54:36 +0300990int machine__create_extra_kernel_map(struct machine *machine,
991 struct dso *kernel,
992 struct extra_kernel_map *xm)
Adrian Hunter4d99e412018-05-22 13:54:33 +0300993{
994 struct kmap *kmap;
995 struct map *map;
996
997 map = map__new2(xm->start, kernel);
998 if (!map)
999 return -1;
1000
1001 map->end = xm->end;
1002 map->pgoff = xm->pgoff;
1003
1004 kmap = map__kmap(map);
1005
1006 kmap->kmaps = &machine->kmaps;
Adrian Hunter5759a682018-05-22 13:54:35 +03001007 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001008
1009 map_groups__insert(&machine->kmaps, map);
1010
Adrian Hunter5759a682018-05-22 13:54:35 +03001011 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
1012 kmap->name, map->start, map->end);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001013
1014 map__put(map);
1015
1016 return 0;
1017}
1018
1019static u64 find_entry_trampoline(struct dso *dso)
1020{
1021 /* Duplicates are removed so lookup all aliases */
1022 const char *syms[] = {
1023 "_entry_trampoline",
1024 "__entry_trampoline_start",
1025 "entry_SYSCALL_64_trampoline",
1026 };
1027 struct symbol *sym = dso__first_symbol(dso);
1028 unsigned int i;
1029
1030 for (; sym; sym = dso__next_symbol(sym)) {
1031 if (sym->binding != STB_GLOBAL)
1032 continue;
1033 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1034 if (!strcmp(sym->name, syms[i]))
1035 return sym->start;
1036 }
1037 }
1038
1039 return 0;
1040}
1041
1042/*
1043 * These values can be used for kernels that do not have symbols for the entry
1044 * trampolines in kallsyms.
1045 */
1046#define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1047#define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1048#define X86_64_ENTRY_TRAMPOLINE 0x6000
1049
1050/* Map x86_64 PTI entry trampolines */
1051int machine__map_x86_64_entry_trampolines(struct machine *machine,
1052 struct dso *kernel)
1053{
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001054 struct map_groups *kmaps = &machine->kmaps;
1055 struct maps *maps = &kmaps->maps;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001056 int nr_cpus_avail, cpu;
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001057 bool found = false;
1058 struct map *map;
1059 u64 pgoff;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001060
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001061 /*
1062 * In the vmlinux case, pgoff is a virtual address which must now be
1063 * mapped to a vmlinux offset.
1064 */
Arnaldo Carvalho de Melo8efc4f02019-10-28 11:31:38 -03001065 maps__for_each_entry(maps, map) {
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001066 struct kmap *kmap = __map__kmap(map);
1067 struct map *dest_map;
1068
1069 if (!kmap || !is_entry_trampoline(kmap->name))
1070 continue;
1071
1072 dest_map = map_groups__find(kmaps, map->pgoff);
1073 if (dest_map != map)
1074 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1075 found = true;
1076 }
1077 if (found || machine->trampolines_mapped)
1078 return 0;
1079
1080 pgoff = find_entry_trampoline(kernel);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001081 if (!pgoff)
1082 return 0;
1083
1084 nr_cpus_avail = machine__nr_cpus_avail(machine);
1085
1086 /* Add a 1 page map for each CPU's entry trampoline */
1087 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1088 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1089 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1090 X86_64_ENTRY_TRAMPOLINE;
1091 struct extra_kernel_map xm = {
1092 .start = va,
1093 .end = va + page_size,
1094 .pgoff = pgoff,
1095 };
1096
Adrian Hunter5759a682018-05-22 13:54:35 +03001097 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1098
Adrian Hunter4d99e412018-05-22 13:54:33 +03001099 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1100 return -1;
1101 }
1102
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001103 machine->trampolines_mapped = nr_cpus_avail;
1104
1105 return 0;
1106}
1107
1108int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1109 struct dso *kernel __maybe_unused)
1110{
Adrian Hunter4d99e412018-05-22 13:54:33 +03001111 return 0;
1112}
1113
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001114static int
1115__machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001116{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001117 struct kmap *kmap;
1118 struct map *map;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001119
Masami Hiramatsucc1121a2015-12-09 11:11:33 +09001120 /* In case of renewal the kernel map, destroy previous one */
1121 machine__destroy_kernel_maps(machine);
1122
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001123 machine->vmlinux_map = map__new2(0, kernel);
1124 if (machine->vmlinux_map == NULL)
1125 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001126
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001127 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1128 map = machine__kernel_map(machine);
1129 kmap = map__kmap(map);
1130 if (!kmap)
1131 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001132
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001133 kmap->kmaps = &machine->kmaps;
1134 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001135
1136 return 0;
1137}
1138
1139void machine__destroy_kernel_maps(struct machine *machine)
1140{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001141 struct kmap *kmap;
1142 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001143
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001144 if (map == NULL)
1145 return;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001146
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001147 kmap = map__kmap(map);
1148 map_groups__remove(&machine->kmaps, map);
1149 if (kmap && kmap->ref_reloc_sym) {
1150 zfree((char **)&kmap->ref_reloc_sym->name);
1151 zfree(&kmap->ref_reloc_sym);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001152 }
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001153
1154 map__zput(machine->vmlinux_map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001155}
1156
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001157int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001158{
1159 int ret = 0;
1160 struct dirent **namelist = NULL;
1161 int i, items = 0;
1162 char path[PATH_MAX];
1163 pid_t pid;
1164 char *endp;
1165
1166 if (symbol_conf.default_guest_vmlinux_name ||
1167 symbol_conf.default_guest_modules ||
1168 symbol_conf.default_guest_kallsyms) {
1169 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1170 }
1171
1172 if (symbol_conf.guestmount) {
1173 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1174 if (items <= 0)
1175 return -ENOENT;
1176 for (i = 0; i < items; i++) {
1177 if (!isdigit(namelist[i]->d_name[0])) {
1178 /* Filter out . and .. */
1179 continue;
1180 }
1181 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1182 if ((*endp != '\0') ||
1183 (endp == namelist[i]->d_name) ||
1184 (errno == ERANGE)) {
1185 pr_debug("invalid directory (%s). Skipping.\n",
1186 namelist[i]->d_name);
1187 continue;
1188 }
1189 sprintf(path, "%s/%s/proc/kallsyms",
1190 symbol_conf.guestmount,
1191 namelist[i]->d_name);
1192 ret = access(path, R_OK);
1193 if (ret) {
1194 pr_debug("Can't access file %s\n", path);
1195 goto failure;
1196 }
1197 machines__create_kernel_maps(machines, pid);
1198 }
1199failure:
1200 free(namelist);
1201 }
1202
1203 return ret;
1204}
1205
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001206void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001207{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001208 struct rb_node *next = rb_first_cached(&machines->guests);
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001209
1210 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001211
1212 while (next) {
1213 struct machine *pos = rb_entry(next, struct machine, rb_node);
1214
1215 next = rb_next(&pos->rb_node);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001216 rb_erase_cached(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001217 machine__delete(pos);
1218 }
1219}
1220
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001221int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001222{
1223 struct machine *machine = machines__findnew(machines, pid);
1224
1225 if (machine == NULL)
1226 return -1;
1227
1228 return machine__create_kernel_maps(machine);
1229}
1230
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001231int machine__load_kallsyms(struct machine *machine, const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001232{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001233 struct map *map = machine__kernel_map(machine);
Jiri Olsae8f38792018-02-15 13:26:33 +01001234 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001235
1236 if (ret > 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001237 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001238 /*
1239 * Since /proc/kallsyms will have multiple sessions for the
1240 * kernel, with modules between them, fixup the end of all
1241 * sections.
1242 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001243 map_groups__fixup_end(&machine->kmaps);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001244 }
1245
1246 return ret;
1247}
1248
Arnaldo Carvalho de Melo1d1a2652018-04-25 12:18:11 -03001249int machine__load_vmlinux_path(struct machine *machine)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001250{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001251 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001252 int ret = dso__load_vmlinux_path(map->dso, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001253
Adrian Hunter39b12f782013-08-07 14:38:47 +03001254 if (ret > 0)
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001255 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001256
1257 return ret;
1258}
1259
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001260static char *get_kernel_version(const char *root_dir)
1261{
1262 char version[PATH_MAX];
1263 FILE *file;
1264 char *name, *tmp;
1265 const char *prefix = "Linux version ";
1266
1267 sprintf(version, "%s/proc/version", root_dir);
1268 file = fopen(version, "r");
1269 if (!file)
1270 return NULL;
1271
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001272 tmp = fgets(version, sizeof(version), file);
1273 fclose(file);
Donald Yandt34b65af2019-05-28 09:41:28 -04001274 if (!tmp)
1275 return NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001276
1277 name = strstr(version, prefix);
1278 if (!name)
1279 return NULL;
1280 name += strlen(prefix);
1281 tmp = strchr(name, ' ');
1282 if (tmp)
1283 *tmp = '\0';
1284
1285 return strdup(name);
1286}
1287
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001288static bool is_kmod_dso(struct dso *dso)
1289{
1290 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1291 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1292}
1293
1294static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1295 struct kmod_path *m)
1296{
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001297 char *long_name;
Arnaldo Carvalho de Melo83cf7742018-04-24 12:16:09 -03001298 struct map *map = map_groups__find_by_name(mg, m->name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001299
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001300 if (map == NULL)
1301 return 0;
1302
1303 long_name = strdup(path);
1304 if (long_name == NULL)
1305 return -ENOMEM;
1306
1307 dso__set_long_name(map->dso, long_name, true);
1308 dso__kernel_module_get_build_id(map->dso, "");
1309
1310 /*
1311 * Full name could reveal us kmod compression, so
1312 * we need to update the symtab_type if needed.
1313 */
Jiri Olsa2af52472018-08-17 11:48:07 +02001314 if (m->comp && is_kmod_dso(map->dso)) {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001315 map->dso->symtab_type++;
Jiri Olsa2af52472018-08-17 11:48:07 +02001316 map->dso->comp = m->comp;
1317 }
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001318
1319 return 0;
1320}
1321
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001322static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -04001323 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001324{
1325 struct dirent *dent;
1326 DIR *dir = opendir(dir_name);
1327 int ret = 0;
1328
1329 if (!dir) {
1330 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1331 return -1;
1332 }
1333
1334 while ((dent = readdir(dir)) != NULL) {
1335 char path[PATH_MAX];
1336 struct stat st;
1337
1338 /*sshfs might return bad dent->d_type, so we have to stat*/
1339 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1340 if (stat(path, &st))
1341 continue;
1342
1343 if (S_ISDIR(st.st_mode)) {
1344 if (!strcmp(dent->d_name, ".") ||
1345 !strcmp(dent->d_name, ".."))
1346 continue;
1347
Richard Yao61d42902014-04-26 13:17:55 -04001348 /* Do not follow top-level source and build symlinks */
1349 if (depth == 0) {
1350 if (!strcmp(dent->d_name, "source") ||
1351 !strcmp(dent->d_name, "build"))
1352 continue;
1353 }
1354
1355 ret = map_groups__set_modules_path_dir(mg, path,
1356 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001357 if (ret < 0)
1358 goto out;
1359 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001360 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001361
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001362 ret = kmod_path__parse_name(&m, dent->d_name);
1363 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001364 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001365
1366 if (m.kmod)
1367 ret = map_groups__set_module_path(mg, path, &m);
1368
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001369 zfree(&m.name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001370
1371 if (ret)
1372 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001373 }
1374 }
1375
1376out:
1377 closedir(dir);
1378 return ret;
1379}
1380
1381static int machine__set_modules_path(struct machine *machine)
1382{
1383 char *version;
1384 char modules_path[PATH_MAX];
1385
1386 version = get_kernel_version(machine->root_dir);
1387 if (!version)
1388 return -1;
1389
Richard Yao61d42902014-04-26 13:17:55 -04001390 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001391 machine->root_dir, version);
1392 free(version);
1393
Richard Yao61d42902014-04-26 13:17:55 -04001394 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001395}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001396int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
Thomas Richter12a6d292019-07-24 14:27:02 +02001397 u64 *size __maybe_unused,
Song Shan Gong203d8a42016-07-21 11:10:51 +08001398 const char *name __maybe_unused)
1399{
1400 return 0;
1401}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001402
Thomas Richter9ad46522017-08-03 15:49:02 +02001403static int machine__create_module(void *arg, const char *name, u64 start,
1404 u64 size)
Adrian Hunter316d70d2013-10-08 11:45:48 +03001405{
1406 struct machine *machine = arg;
1407 struct map *map;
1408
Thomas Richter12a6d292019-07-24 14:27:02 +02001409 if (arch__fix_module_text_start(&start, &size, name) < 0)
Song Shan Gong203d8a42016-07-21 11:10:51 +08001410 return -1;
1411
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001412 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001413 if (map == NULL)
1414 return -1;
Thomas Richter9ad46522017-08-03 15:49:02 +02001415 map->end = start + size;
Adrian Hunter316d70d2013-10-08 11:45:48 +03001416
1417 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1418
1419 return 0;
1420}
1421
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001422static int machine__create_modules(struct machine *machine)
1423{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001424 const char *modules;
1425 char path[PATH_MAX];
1426
Adrian Hunterf4be9042013-09-22 13:22:09 +03001427 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001428 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001429 } else {
1430 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001431 modules = path;
1432 }
1433
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001434 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001435 return -1;
1436
Adrian Hunter316d70d2013-10-08 11:45:48 +03001437 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001438 return -1;
1439
Adrian Hunter316d70d2013-10-08 11:45:48 +03001440 if (!machine__set_modules_path(machine))
1441 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001442
Adrian Hunter316d70d2013-10-08 11:45:48 +03001443 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001444
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001445 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001446}
1447
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001448static void machine__set_kernel_mmap(struct machine *machine,
1449 u64 start, u64 end)
1450{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001451 machine->vmlinux_map->start = start;
1452 machine->vmlinux_map->end = end;
1453 /*
1454 * Be a bit paranoid here, some perf.data file came with
1455 * a zero sized synthesized MMAP event for the kernel.
1456 */
1457 if (start == 0 && end == 0)
1458 machine->vmlinux_map->end = ~0ULL;
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001459}
1460
Wei Li977c7a62019-02-28 17:20:03 +08001461static void machine__update_kernel_mmap(struct machine *machine,
1462 u64 start, u64 end)
1463{
1464 struct map *map = machine__kernel_map(machine);
1465
1466 map__get(map);
1467 map_groups__remove(&machine->kmaps, map);
1468
1469 machine__set_kernel_mmap(machine, start, end);
1470
1471 map_groups__insert(&machine->kmaps, map);
1472 map__put(map);
1473}
1474
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001475int machine__create_kernel_maps(struct machine *machine)
1476{
1477 struct dso *kernel = machine__get_kernel(machine);
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001478 const char *name = NULL;
Namhyung Kimee05d21792018-02-19 19:05:45 +09001479 struct map *map;
Jiri Olsaed9adb22019-05-08 15:20:03 +02001480 u64 start = 0, end = ~0ULL;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001481 int ret;
1482
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001483 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001484 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001485
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001486 ret = __machine__create_kernel_maps(machine, kernel);
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001487 if (ret < 0)
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001488 goto out_put;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001489
1490 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1491 if (machine__is_host(machine))
1492 pr_debug("Problems creating module maps, "
1493 "continuing anyway...\n");
1494 else
1495 pr_debug("Problems creating module maps for guest %d, "
1496 "continuing anyway...\n", machine->pid);
1497 }
1498
Jiri Olsaed9adb22019-05-08 15:20:03 +02001499 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001500 if (name &&
Jiri Olsaed9adb22019-05-08 15:20:03 +02001501 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001502 machine__destroy_kernel_maps(machine);
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001503 ret = -1;
1504 goto out_put;
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001505 }
Namhyung Kimee05d21792018-02-19 19:05:45 +09001506
Wei Li977c7a62019-02-28 17:20:03 +08001507 /*
1508 * we have a real start address now, so re-order the kmaps
1509 * assume it's the last in the kmaps
1510 */
Jiri Olsaed9adb22019-05-08 15:20:03 +02001511 machine__update_kernel_mmap(machine, start, end);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001512 }
1513
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001514 if (machine__create_extra_kernel_maps(machine, kernel))
1515 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1516
Jiri Olsaed9adb22019-05-08 15:20:03 +02001517 if (end == ~0ULL) {
1518 /* update end address of the kernel map using adjacent module address */
1519 map = map__next(machine__kernel_map(machine));
1520 if (map)
1521 machine__set_kernel_mmap(machine, start, map->start);
1522 }
1523
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001524out_put:
1525 dso__put(kernel);
1526 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001527}
1528
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001529static bool machine__uses_kcore(struct machine *machine)
1530{
1531 struct dso *dso;
1532
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001533 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001534 if (dso__is_kcore(dso))
1535 return true;
1536 }
1537
1538 return false;
1539}
1540
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001541static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1542 union perf_event *event)
1543{
1544 return machine__is(machine, "x86_64") &&
1545 is_entry_trampoline(event->mmap.filename);
1546}
1547
1548static int machine__process_extra_kernel_map(struct machine *machine,
1549 union perf_event *event)
1550{
Arnaldo Carvalho de Melo93730f82019-10-31 15:22:24 -03001551 struct dso *kernel = machine__kernel_dso(machine);
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001552 struct extra_kernel_map xm = {
1553 .start = event->mmap.start,
1554 .end = event->mmap.start + event->mmap.len,
1555 .pgoff = event->mmap.pgoff,
1556 };
1557
1558 if (kernel == NULL)
1559 return -1;
1560
1561 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1562
1563 return machine__create_extra_kernel_map(machine, kernel, &xm);
1564}
1565
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001566static int machine__process_kernel_mmap_event(struct machine *machine,
1567 union perf_event *event)
1568{
1569 struct map *map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001570 enum dso_kernel_type kernel_type;
1571 bool is_kernel_mmap;
1572
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001573 /* If we have maps from kcore then we do not need or want any others */
1574 if (machine__uses_kcore(machine))
1575 return 0;
1576
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001577 if (machine__is_host(machine))
1578 kernel_type = DSO_TYPE_KERNEL;
1579 else
1580 kernel_type = DSO_TYPE_GUEST_KERNEL;
1581
1582 is_kernel_mmap = memcmp(event->mmap.filename,
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001583 machine->mmap_name,
1584 strlen(machine->mmap_name) - 1) == 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001585 if (event->mmap.filename[0] == '/' ||
1586 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001587 map = machine__findnew_module_map(machine, event->mmap.start,
1588 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001589 if (map == NULL)
1590 goto out_problem;
1591
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001592 map->end = map->start + event->mmap.len;
1593 } else if (is_kernel_mmap) {
1594 const char *symbol_name = (event->mmap.filename +
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001595 strlen(machine->mmap_name));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001596 /*
1597 * Should be there already, from the build-id table in
1598 * the header.
1599 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001600 struct dso *kernel = NULL;
1601 struct dso *dso;
1602
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001603 down_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001604
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001605 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001606
1607 /*
1608 * The cpumode passed to is_kernel_module is not the
1609 * cpumode of *this* event. If we insist on passing
1610 * correct cpumode to is_kernel_module, we should
1611 * record the cpumode when we adding this dso to the
1612 * linked list.
1613 *
1614 * However we don't really need passing correct
1615 * cpumode. We know the correct cpumode must be kernel
1616 * mode (if not, we should not link it onto kernel_dsos
1617 * list).
1618 *
1619 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1620 * is_kernel_module() treats it as a kernel cpumode.
1621 */
1622
1623 if (!dso->kernel ||
1624 is_kernel_module(dso->long_name,
1625 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001626 continue;
1627
Wang Nan1f121b02015-06-03 08:52:21 +00001628
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001629 kernel = dso;
1630 break;
1631 }
1632
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001633 up_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001634
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001635 if (kernel == NULL)
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001636 kernel = machine__findnew_dso(machine, machine->mmap_name);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001637 if (kernel == NULL)
1638 goto out_problem;
1639
1640 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001641 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1642 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001643 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001644 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001645
Namhyung Kim330dfa22014-11-18 13:30:28 +09001646 if (strstr(kernel->long_name, "vmlinux"))
1647 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001648
Wei Li977c7a62019-02-28 17:20:03 +08001649 machine__update_kernel_mmap(machine, event->mmap.start,
Jiri Olsa05db6ff2018-02-15 13:26:31 +01001650 event->mmap.start + event->mmap.len);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001651
1652 /*
1653 * Avoid using a zero address (kptr_restrict) for the ref reloc
1654 * symbol. Effectively having zero here means that at record
1655 * time /proc/sys/kernel/kptr_restrict was non zero.
1656 */
1657 if (event->mmap.pgoff != 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001658 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1659 symbol_name,
1660 event->mmap.pgoff);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001661 }
1662
1663 if (machine__is_default_guest(machine)) {
1664 /*
1665 * preload dso of guest kernel and modules
1666 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001667 dso__load(kernel, machine__kernel_map(machine));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001668 }
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001669 } else if (perf_event__is_extra_kernel_mmap(machine, event)) {
1670 return machine__process_extra_kernel_map(machine, event);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001671 }
1672 return 0;
1673out_problem:
1674 return -1;
1675}
1676
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001677int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001678 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001679 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001680{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001681 struct thread *thread;
1682 struct map *map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001683 int ret = 0;
1684
1685 if (dump_trace)
1686 perf_event__fprintf_mmap2(event, stdout);
1687
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001688 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1689 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001690 ret = machine__process_kernel_mmap_event(machine, event);
1691 if (ret < 0)
1692 goto out_problem;
1693 return 0;
1694 }
1695
1696 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001697 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001698 if (thread == NULL)
1699 goto out_problem;
1700
Adrian Hunter2a030682014-07-22 16:17:53 +03001701 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001702 event->mmap2.len, event->mmap2.pgoff,
Krister Johansenbf2e7102017-07-05 18:48:09 -07001703 event->mmap2.maj,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001704 event->mmap2.min, event->mmap2.ino,
1705 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001706 event->mmap2.prot,
1707 event->mmap2.flags,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001708 event->mmap2.filename, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001709
1710 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001711 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001712
He Kuang8132a2a2016-06-03 03:33:13 +00001713 ret = thread__insert_map(thread, map);
1714 if (ret)
1715 goto out_problem_insert;
1716
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001717 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001718 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001719 return 0;
1720
He Kuang8132a2a2016-06-03 03:33:13 +00001721out_problem_insert:
1722 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001723out_problem_map:
1724 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001725out_problem:
1726 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1727 return 0;
1728}
1729
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001730int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001731 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001732{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001733 struct thread *thread;
1734 struct map *map;
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001735 u32 prot = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001736 int ret = 0;
1737
1738 if (dump_trace)
1739 perf_event__fprintf_mmap(event, stdout);
1740
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001741 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1742 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001743 ret = machine__process_kernel_mmap_event(machine, event);
1744 if (ret < 0)
1745 goto out_problem;
1746 return 0;
1747 }
1748
Adrian Hunter314add62013-08-27 11:23:03 +03001749 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001750 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001751 if (thread == NULL)
1752 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001753
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001754 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001755 prot = PROT_EXEC;
Stephane Eranianbad40912013-01-24 16:10:40 +01001756
Adrian Hunter2a030682014-07-22 16:17:53 +03001757 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001758 event->mmap.len, event->mmap.pgoff,
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001759 0, 0, 0, 0, prot, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001760 event->mmap.filename,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001761 thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001762
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001763 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001764 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001765
He Kuang8132a2a2016-06-03 03:33:13 +00001766 ret = thread__insert_map(thread, map);
1767 if (ret)
1768 goto out_problem_insert;
1769
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001770 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001771 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001772 return 0;
1773
He Kuang8132a2a2016-06-03 03:33:13 +00001774out_problem_insert:
1775 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001776out_problem_map:
1777 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001778out_problem:
1779 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1780 return 0;
1781}
1782
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001783static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001784{
Kan Liang91e467b2017-09-10 19:23:14 -07001785 struct threads *threads = machine__threads(machine, th->tid);
1786
1787 if (threads->last_match == th)
Jiri Olsa67fda0f2018-07-19 16:33:43 +02001788 threads__set_last_match(threads, NULL);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001789
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001790 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001791 down_write(&threads->lock);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001792
1793 BUG_ON(refcount_read(&th->refcnt) == 0);
1794
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001795 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001796 RB_CLEAR_NODE(&th->rb_node);
Kan Liang91e467b2017-09-10 19:23:14 -07001797 --threads->nr;
David Ahern236a3bb2013-08-14 08:49:27 -06001798 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001799 * Move it first to the dead_threads list, then drop the reference,
1800 * if this is the last reference, then the thread__delete destructor
1801 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001802 */
Kan Liang91e467b2017-09-10 19:23:14 -07001803 list_add_tail(&th->node, &threads->dead);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001804
1805 /*
1806 * We need to do the put here because if this is the last refcount,
1807 * then we will be touching the threads->dead head when removing the
1808 * thread.
1809 */
1810 thread__put(th);
1811
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001812 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001813 up_write(&threads->lock);
David Ahern236a3bb2013-08-14 08:49:27 -06001814}
1815
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001816void machine__remove_thread(struct machine *machine, struct thread *th)
1817{
1818 return __machine__remove_thread(machine, th, true);
1819}
1820
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001821int machine__process_fork_event(struct machine *machine, union perf_event *event,
1822 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001823{
Jiri Olsad75e6092014-03-14 15:00:03 +01001824 struct thread *thread = machine__find_thread(machine,
1825 event->fork.pid,
1826 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001827 struct thread *parent = machine__findnew_thread(machine,
1828 event->fork.ppid,
1829 event->fork.ptid);
David Miller4f8f3822018-10-30 22:24:04 -07001830 bool do_maps_clone = true;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001831 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001832
Adrian Hunter5cb73342015-08-19 17:29:20 +03001833 if (dump_trace)
1834 perf_event__fprintf_task(event, stdout);
1835
1836 /*
1837 * There may be an existing thread that is not actually the parent,
1838 * either because we are processing events out of order, or because the
1839 * (fork) event that would have removed the thread was lost. Assume the
1840 * latter case and continue on as best we can.
1841 */
1842 if (parent->pid_ != (pid_t)event->fork.ppid) {
1843 dump_printf("removing erroneous parent thread %d/%d\n",
1844 parent->pid_, parent->tid);
1845 machine__remove_thread(machine, parent);
1846 thread__put(parent);
1847 parent = machine__findnew_thread(machine, event->fork.ppid,
1848 event->fork.ptid);
1849 }
1850
David Ahern236a3bb2013-08-14 08:49:27 -06001851 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001852 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001853 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001854 thread__put(thread);
1855 }
David Ahern236a3bb2013-08-14 08:49:27 -06001856
Adrian Hunter314add62013-08-27 11:23:03 +03001857 thread = machine__findnew_thread(machine, event->fork.pid,
1858 event->fork.tid);
David Miller4f8f3822018-10-30 22:24:04 -07001859 /*
1860 * When synthesizing FORK events, we are trying to create thread
1861 * objects for the already running tasks on the machine.
1862 *
1863 * Normally, for a kernel FORK event, we want to clone the parent's
1864 * maps because that is what the kernel just did.
1865 *
1866 * But when synthesizing, this should not be done. If we do, we end up
1867 * with overlapping maps as we process the sythesized MMAP2 events that
1868 * get delivered shortly thereafter.
1869 *
1870 * Use the FORK event misc flags in an internal way to signal this
1871 * situation, so we can elide the map clone when appropriate.
1872 */
1873 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1874 do_maps_clone = false;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001875
1876 if (thread == NULL || parent == NULL ||
David Miller4f8f3822018-10-30 22:24:04 -07001877 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001878 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001879 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001880 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001881 thread__put(thread);
1882 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001883
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001884 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001885}
1886
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001887int machine__process_exit_event(struct machine *machine, union perf_event *event,
1888 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001889{
Jiri Olsad75e6092014-03-14 15:00:03 +01001890 struct thread *thread = machine__find_thread(machine,
1891 event->fork.pid,
1892 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001893
1894 if (dump_trace)
1895 perf_event__fprintf_task(event, stdout);
1896
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001897 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001898 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001899 thread__put(thread);
1900 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001901
1902 return 0;
1903}
1904
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001905int machine__process_event(struct machine *machine, union perf_event *event,
1906 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001907{
1908 int ret;
1909
1910 switch (event->header.type) {
1911 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001912 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001913 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001914 ret = machine__process_mmap_event(machine, event, sample); break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301915 case PERF_RECORD_NAMESPACES:
1916 ret = machine__process_namespaces_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001917 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001918 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001919 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001920 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001921 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001922 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001923 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001924 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001925 case PERF_RECORD_AUX:
1926 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001927 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001928 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001929 case PERF_RECORD_LOST_SAMPLES:
1930 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001931 case PERF_RECORD_SWITCH:
1932 case PERF_RECORD_SWITCH_CPU_WIDE:
1933 ret = machine__process_switch_event(machine, event); break;
Song Liu9aa0bfa2019-01-17 08:15:17 -08001934 case PERF_RECORD_KSYMBOL:
1935 ret = machine__process_ksymbol(machine, event, sample); break;
Song Liu45178a92019-01-17 08:15:18 -08001936 case PERF_RECORD_BPF_EVENT:
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03001937 ret = machine__process_bpf(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001938 default:
1939 ret = -1;
1940 break;
1941 }
1942
1943 return ret;
1944}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001945
Greg Priceb21484f2012-12-06 21:48:05 -08001946static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001947{
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001948 if (!regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001949 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001950 return 0;
1951}
1952
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001953static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001954 struct addr_map_symbol *ams,
1955 u64 ip)
1956{
1957 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001958
1959 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001960 /*
1961 * We cannot use the header.misc hint to determine whether a
1962 * branch stack address is user, kernel, guest, hypervisor.
1963 * Branches may straddle the kernel/user/hypervisor boundaries.
1964 * Thus, we have to try consecutively until we find a match
1965 * or else, the symbol is unknown
1966 */
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03001967 thread__find_cpumode_addr_location(thread, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001968
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001969 ams->addr = ip;
1970 ams->al_addr = al.addr;
1971 ams->sym = al.sym;
1972 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001973 ams->phys_addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001974}
1975
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001976static void ip__resolve_data(struct thread *thread,
Kan Liang8780fb22017-08-29 13:11:09 -04001977 u8 m, struct addr_map_symbol *ams,
1978 u64 addr, u64 phys_addr)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001979{
1980 struct addr_location al;
1981
1982 memset(&al, 0, sizeof(al));
1983
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03001984 thread__find_symbol(thread, m, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001985
Stephane Eranian98a3b322013-01-24 16:10:35 +01001986 ams->addr = addr;
1987 ams->al_addr = al.addr;
1988 ams->sym = al.sym;
1989 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001990 ams->phys_addr = phys_addr;
Stephane Eranian98a3b322013-01-24 16:10:35 +01001991}
1992
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001993struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1994 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001995{
Jiri Olsa9f874982018-03-07 16:50:06 +01001996 struct mem_info *mi = mem_info__new();
Stephane Eranian98a3b322013-01-24 16:10:35 +01001997
1998 if (!mi)
1999 return NULL;
2000
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002001 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
Kan Liang8780fb22017-08-29 13:11:09 -04002002 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
2003 sample->addr, sample->phys_addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01002004 mi->data_src.val = sample->data_src;
2005
2006 return mi;
2007}
2008
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002009static char *callchain_srcline(struct map_symbol *ms, u64 ip)
Milian Wolff40a342c2017-10-09 22:32:56 +02002010{
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002011 struct map *map = ms->map;
Milian Wolff21ac9d52017-10-19 13:38:34 +02002012 char *srcline = NULL;
Milian Wolff40a342c2017-10-09 22:32:56 +02002013
Milian Wolff21ac9d52017-10-19 13:38:34 +02002014 if (!map || callchain_param.key == CCKEY_FUNCTION)
2015 return srcline;
2016
2017 srcline = srcline__tree_find(&map->dso->srclines, ip);
2018 if (!srcline) {
2019 bool show_sym = false;
2020 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2021
2022 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002023 ms->sym, show_sym, show_addr, ip);
Milian Wolff21ac9d52017-10-19 13:38:34 +02002024 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2025 }
2026
2027 return srcline;
Milian Wolff40a342c2017-10-09 22:32:56 +02002028}
2029
Jin Yaoc4ee0622017-08-07 21:05:15 +08002030struct iterations {
2031 int nr_loop_iter;
2032 u64 cycles;
2033};
2034
Andi Kleen37592b82014-11-12 18:05:19 -08002035static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002036 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08002037 struct symbol **parent,
2038 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002039 u8 *cpumode,
Jin Yao410024d2016-10-31 09:19:49 +08002040 u64 ip,
2041 bool branch,
2042 struct branch_flags *flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002043 struct iterations *iter,
Jin Yaob851dd42017-07-18 20:13:15 +08002044 u64 branch_from)
Andi Kleen37592b82014-11-12 18:05:19 -08002045{
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002046 struct map_symbol ms;
Andi Kleen37592b82014-11-12 18:05:19 -08002047 struct addr_location al;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002048 int nr_loop_iter = 0;
2049 u64 iter_cycles = 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002050 const char *srcline = NULL;
Andi Kleen37592b82014-11-12 18:05:19 -08002051
2052 al.filtered = 0;
2053 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002054 if (!cpumode) {
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03002055 thread__find_cpumode_addr_location(thread, ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002056 } else {
Kan Liang2e777842014-12-02 10:06:53 -05002057 if (ip >= PERF_CONTEXT_MAX) {
2058 switch (ip) {
2059 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002060 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05002061 break;
2062 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002063 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05002064 break;
2065 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002066 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05002067 break;
2068 default:
2069 pr_debug("invalid callchain context: "
2070 "%"PRId64"\n", (s64) ip);
2071 /*
2072 * It seems the callchain is corrupted.
2073 * Discard all.
2074 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002075 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05002076 return 1;
2077 }
2078 return 0;
2079 }
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002080 thread__find_symbol(thread, *cpumode, ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05002081 }
2082
Andi Kleen37592b82014-11-12 18:05:19 -08002083 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02002084 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08002085 symbol__match_regex(al.sym, &parent_regex))
2086 *parent = al.sym;
2087 else if (have_ignore_callees && root_al &&
2088 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2089 /* Treat this symbol as the root,
2090 forgetting its callees. */
2091 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002092 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08002093 }
2094 }
2095
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002096 if (symbol_conf.hide_unresolved && al.sym == NULL)
2097 return 0;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002098
2099 if (iter) {
2100 nr_loop_iter = iter->nr_loop_iter;
2101 iter_cycles = iter->cycles;
2102 }
2103
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002104 ms.map = al.map;
2105 ms.sym = al.sym;
2106 srcline = callchain_srcline(&ms, al.addr);
2107 return callchain_cursor_append(cursor, ip, &ms,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002108 branch, flags, nr_loop_iter,
Milian Wolff40a342c2017-10-09 22:32:56 +02002109 iter_cycles, branch_from, srcline);
Andi Kleen37592b82014-11-12 18:05:19 -08002110}
2111
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002112struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2113 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002114{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002115 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002116 const struct branch_stack *bs = sample->branch_stack;
2117 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002118
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002119 if (!bi)
2120 return NULL;
2121
2122 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002123 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
2124 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002125 bi[i].flags = bs->entries[i].flags;
2126 }
2127 return bi;
2128}
2129
Jin Yaoc4ee0622017-08-07 21:05:15 +08002130static void save_iterations(struct iterations *iter,
2131 struct branch_entry *be, int nr)
2132{
2133 int i;
2134
Jin Yaoa3366db02019-01-04 14:10:30 +08002135 iter->nr_loop_iter++;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002136 iter->cycles = 0;
2137
2138 for (i = 0; i < nr; i++)
2139 iter->cycles += be[i].flags.cycles;
2140}
2141
Andi Kleen8b7bad52014-11-12 18:05:20 -08002142#define CHASHSZ 127
2143#define CHASHBITS 7
2144#define NO_ENTRY 0xff
2145
2146#define PERF_MAX_BRANCH_DEPTH 127
2147
2148/* Remove loops. */
Jin Yaoc4ee0622017-08-07 21:05:15 +08002149static int remove_loops(struct branch_entry *l, int nr,
2150 struct iterations *iter)
Andi Kleen8b7bad52014-11-12 18:05:20 -08002151{
2152 int i, j, off;
2153 unsigned char chash[CHASHSZ];
2154
2155 memset(chash, NO_ENTRY, sizeof(chash));
2156
2157 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2158
2159 for (i = 0; i < nr; i++) {
2160 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2161
2162 /* no collision handling for now */
2163 if (chash[h] == NO_ENTRY) {
2164 chash[h] = i;
2165 } else if (l[chash[h]].from == l[i].from) {
2166 bool is_loop = true;
2167 /* check if it is a real loop */
2168 off = 0;
2169 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2170 if (l[j].from != l[i + off].from) {
2171 is_loop = false;
2172 break;
2173 }
2174 if (is_loop) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002175 j = nr - (i + off);
2176 if (j > 0) {
2177 save_iterations(iter + i + off,
2178 l + i, off);
2179
2180 memmove(iter + i, iter + i + off,
2181 j * sizeof(*iter));
2182
2183 memmove(l + i, l + i + off,
2184 j * sizeof(*l));
2185 }
2186
Andi Kleen8b7bad52014-11-12 18:05:20 -08002187 nr -= off;
2188 }
2189 }
2190 }
2191 return nr;
2192}
2193
Kan Liang384b6052015-01-05 13:23:05 -05002194/*
2195 * Recolve LBR callstack chain sample
2196 * Return:
2197 * 1 on success get LBR callchain information
2198 * 0 no available LBR callchain information, should try fp
2199 * negative error code on other errors.
2200 */
2201static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002202 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05002203 struct perf_sample *sample,
2204 struct symbol **parent,
2205 struct addr_location *root_al,
2206 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002207{
Kan Liang384b6052015-01-05 13:23:05 -05002208 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002209 int chain_nr = min(max_stack, (int)chain->nr), i;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002210 u8 cpumode = PERF_RECORD_MISC_USER;
Jin Yaob851dd42017-07-18 20:13:15 +08002211 u64 ip, branch_from = 0;
Kan Liang384b6052015-01-05 13:23:05 -05002212
2213 for (i = 0; i < chain_nr; i++) {
2214 if (chain->ips[i] == PERF_CONTEXT_USER)
2215 break;
2216 }
2217
2218 /* LBR only affects the user callchain */
2219 if (i != chain_nr) {
2220 struct branch_stack *lbr_stack = sample->branch_stack;
Jin Yao410024d2016-10-31 09:19:49 +08002221 int lbr_nr = lbr_stack->nr, j, k;
2222 bool branch;
2223 struct branch_flags *flags;
Kan Liang384b6052015-01-05 13:23:05 -05002224 /*
2225 * LBR callstack can only get user call chain.
2226 * The mix_chain_nr is kernel call chain
2227 * number plus LBR user call chain number.
2228 * i is kernel call chain number,
2229 * 1 is PERF_CONTEXT_USER,
2230 * lbr_nr + 1 is the user call chain number.
2231 * For details, please refer to the comments
2232 * in callchain__printf
2233 */
2234 int mix_chain_nr = i + 1 + lbr_nr + 1;
2235
Kan Liang384b6052015-01-05 13:23:05 -05002236 for (j = 0; j < mix_chain_nr; j++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002237 int err;
Jin Yao410024d2016-10-31 09:19:49 +08002238 branch = false;
2239 flags = NULL;
2240
Kan Liang384b6052015-01-05 13:23:05 -05002241 if (callchain_param.order == ORDER_CALLEE) {
2242 if (j < i + 1)
2243 ip = chain->ips[j];
Jin Yao410024d2016-10-31 09:19:49 +08002244 else if (j > i + 1) {
2245 k = j - i - 2;
2246 ip = lbr_stack->entries[k].from;
2247 branch = true;
2248 flags = &lbr_stack->entries[k].flags;
2249 } else {
Kan Liang384b6052015-01-05 13:23:05 -05002250 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08002251 branch = true;
2252 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08002253 branch_from =
2254 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08002255 }
Kan Liang384b6052015-01-05 13:23:05 -05002256 } else {
Jin Yao410024d2016-10-31 09:19:49 +08002257 if (j < lbr_nr) {
2258 k = lbr_nr - j - 1;
2259 ip = lbr_stack->entries[k].from;
2260 branch = true;
2261 flags = &lbr_stack->entries[k].flags;
2262 }
Kan Liang384b6052015-01-05 13:23:05 -05002263 else if (j > lbr_nr)
2264 ip = chain->ips[i + 1 - (j - lbr_nr)];
Jin Yao410024d2016-10-31 09:19:49 +08002265 else {
Kan Liang384b6052015-01-05 13:23:05 -05002266 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08002267 branch = true;
2268 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08002269 branch_from =
2270 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08002271 }
Kan Liang384b6052015-01-05 13:23:05 -05002272 }
2273
Jin Yao410024d2016-10-31 09:19:49 +08002274 err = add_callchain_ip(thread, cursor, parent,
2275 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002276 branch, flags, NULL,
Jin Yaob851dd42017-07-18 20:13:15 +08002277 branch_from);
Kan Liang384b6052015-01-05 13:23:05 -05002278 if (err)
2279 return (err < 0) ? err : 0;
2280 }
2281 return 1;
2282 }
2283
2284 return 0;
2285}
2286
David S. Millere9024d52018-10-30 12:12:26 -03002287static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2288 struct callchain_cursor *cursor,
2289 struct symbol **parent,
2290 struct addr_location *root_al,
2291 u8 *cpumode, int ent)
2292{
2293 int err = 0;
2294
2295 while (--ent >= 0) {
2296 u64 ip = chain->ips[ent];
2297
2298 if (ip >= PERF_CONTEXT_MAX) {
2299 err = add_callchain_ip(thread, cursor, parent,
2300 root_al, cpumode, ip,
2301 false, NULL, NULL, 0);
2302 break;
2303 }
2304 }
2305 return err;
2306}
2307
Kan Liang384b6052015-01-05 13:23:05 -05002308static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002309 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002310 struct evsel *evsel,
Kan Liang384b6052015-01-05 13:23:05 -05002311 struct perf_sample *sample,
2312 struct symbol **parent,
2313 struct addr_location *root_al,
2314 int max_stack)
2315{
2316 struct branch_stack *branch = sample->branch_stack;
2317 struct ip_callchain *chain = sample->callchain;
Jin Yaob49a8212017-05-08 18:43:02 +08002318 int chain_nr = 0;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002319 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002320 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08002321 int skip_idx = -1;
2322 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002323
Jin Yaob49a8212017-05-08 18:43:02 +08002324 if (chain)
2325 chain_nr = chain->nr;
2326
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03002327 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002328 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05002329 root_al, max_stack);
2330 if (err)
2331 return (err < 0) ? err : 0;
2332 }
2333
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002334 /*
2335 * Based on DWARF debug information, some architectures skip
2336 * a callchain entry saved by the kernel.
2337 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002338 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002339
Andi Kleen8b7bad52014-11-12 18:05:20 -08002340 /*
2341 * Add branches to call stack for easier browsing. This gives
2342 * more context for a sample than just the callers.
2343 *
2344 * This uses individual histograms of paths compared to the
2345 * aggregated histograms the normal LBR mode uses.
2346 *
2347 * Limitations for now:
2348 * - No extra filters
2349 * - No annotations (should annotate somehow)
2350 */
2351
2352 if (branch && callchain_param.branch_callstack) {
2353 int nr = min(max_stack, (int)branch->nr);
2354 struct branch_entry be[nr];
Jin Yaoc4ee0622017-08-07 21:05:15 +08002355 struct iterations iter[nr];
Andi Kleen8b7bad52014-11-12 18:05:20 -08002356
2357 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2358 pr_warning("corrupted branch chain. skipping...\n");
2359 goto check_calls;
2360 }
2361
2362 for (i = 0; i < nr; i++) {
2363 if (callchain_param.order == ORDER_CALLEE) {
2364 be[i] = branch->entries[i];
Jin Yaob49a8212017-05-08 18:43:02 +08002365
2366 if (chain == NULL)
2367 continue;
2368
Andi Kleen8b7bad52014-11-12 18:05:20 -08002369 /*
2370 * Check for overlap into the callchain.
2371 * The return address is one off compared to
2372 * the branch entry. To adjust for this
2373 * assume the calling instruction is not longer
2374 * than 8 bytes.
2375 */
2376 if (i == skip_idx ||
2377 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2378 first_call++;
2379 else if (be[i].from < chain->ips[first_call] &&
2380 be[i].from >= chain->ips[first_call] - 8)
2381 first_call++;
2382 } else
2383 be[i] = branch->entries[branch->nr - i - 1];
2384 }
2385
Jin Yaoc4ee0622017-08-07 21:05:15 +08002386 memset(iter, 0, sizeof(struct iterations) * nr);
2387 nr = remove_loops(be, nr, iter);
Jin Yao410024d2016-10-31 09:19:49 +08002388
Andi Kleen8b7bad52014-11-12 18:05:20 -08002389 for (i = 0; i < nr; i++) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002390 err = add_callchain_ip(thread, cursor, parent,
2391 root_al,
2392 NULL, be[i].to,
2393 true, &be[i].flags,
2394 NULL, be[i].from);
Jin Yao410024d2016-10-31 09:19:49 +08002395
Andi Kleen8b7bad52014-11-12 18:05:20 -08002396 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002397 err = add_callchain_ip(thread, cursor, parent, root_al,
Jin Yao410024d2016-10-31 09:19:49 +08002398 NULL, be[i].from,
2399 true, &be[i].flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002400 &iter[i], 0);
Andi Kleen8b7bad52014-11-12 18:05:20 -08002401 if (err == -EINVAL)
2402 break;
2403 if (err)
2404 return err;
2405 }
Jin Yaob49a8212017-05-08 18:43:02 +08002406
2407 if (chain_nr == 0)
2408 return 0;
2409
Andi Kleen8b7bad52014-11-12 18:05:20 -08002410 chain_nr -= nr;
2411 }
2412
2413check_calls:
David S. Millere9024d52018-10-30 12:12:26 -03002414 if (callchain_param.order != ORDER_CALLEE) {
2415 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2416 &cpumode, chain->nr - first_call);
2417 if (err)
2418 return (err < 0) ? err : 0;
2419 }
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002420 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002421 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002422 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002423
2424 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002425 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002426 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002427 j = chain->nr - i - 1;
2428
2429#ifdef HAVE_SKIP_CALLCHAIN_IDX
2430 if (j == skip_idx)
2431 continue;
2432#endif
2433 ip = chain->ips[j];
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002434 if (ip < PERF_CONTEXT_MAX)
2435 ++nr_entries;
David S. Millere9024d52018-10-30 12:12:26 -03002436 else if (callchain_param.order != ORDER_CALLEE) {
2437 err = find_prev_cpumode(chain, thread, cursor, parent,
2438 root_al, &cpumode, j);
2439 if (err)
2440 return (err < 0) ? err : 0;
2441 continue;
2442 }
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002443
Jin Yao410024d2016-10-31 09:19:49 +08002444 err = add_callchain_ip(thread, cursor, parent,
2445 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002446 false, NULL, NULL, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002447
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002448 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05002449 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002450 }
2451
2452 return 0;
2453}
2454
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002455static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
Milian Wolff11ea2512017-10-09 22:32:59 +02002456{
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002457 struct symbol *sym = ms->sym;
2458 struct map *map = ms->map;
Milian Wolff11ea2512017-10-09 22:32:59 +02002459 struct inline_node *inline_node;
2460 struct inline_list *ilist;
2461 u64 addr;
Milian Wolffb38775c2017-10-19 13:38:33 +02002462 int ret = 1;
Milian Wolff11ea2512017-10-09 22:32:59 +02002463
2464 if (!symbol_conf.inline_name || !map || !sym)
Milian Wolffb38775c2017-10-19 13:38:33 +02002465 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002466
Milian Wolff7a8a8fc2018-09-26 15:52:06 +02002467 addr = map__map_ip(map, ip);
2468 addr = map__rip_2objdump(map, addr);
Milian Wolff11ea2512017-10-09 22:32:59 +02002469
2470 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2471 if (!inline_node) {
2472 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2473 if (!inline_node)
Milian Wolffb38775c2017-10-19 13:38:33 +02002474 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002475 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2476 }
2477
2478 list_for_each_entry(ilist, &inline_node->val, list) {
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002479 struct map_symbol ilist_ms = {
2480 .map = map,
2481 .sym = ilist->symbol,
2482 };
2483 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
Milian Wolffb38775c2017-10-19 13:38:33 +02002484 NULL, 0, 0, 0, ilist->srcline);
Milian Wolff11ea2512017-10-09 22:32:59 +02002485
2486 if (ret != 0)
2487 return ret;
2488 }
2489
Milian Wolffb38775c2017-10-19 13:38:33 +02002490 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002491}
2492
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002493static int unwind_entry(struct unwind_entry *entry, void *arg)
2494{
2495 struct callchain_cursor *cursor = arg;
Milian Wolff40a342c2017-10-09 22:32:56 +02002496 const char *srcline = NULL;
Milian Wolffff4ce282018-09-26 15:52:05 +02002497 u64 addr = entry->ip;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002498
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002499 if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002500 return 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002501
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002502 if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
Milian Wolff11ea2512017-10-09 22:32:59 +02002503 return 0;
2504
Sandipan Das2a9d5052018-07-03 17:35:55 +05302505 /*
2506 * Convert entry->ip from a virtual address to an offset in
2507 * its corresponding binary.
2508 */
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -03002509 if (entry->ms.map)
2510 addr = map__map_ip(entry->ms.map, entry->ip);
Sandipan Das2a9d5052018-07-03 17:35:55 +05302511
Arnaldo Carvalho de Melo5f0fef82019-11-04 12:14:32 -03002512 srcline = callchain_srcline(&entry->ms, addr);
2513 return callchain_cursor_append(cursor, entry->ip, &entry->ms,
Milian Wolff40a342c2017-10-09 22:32:56 +02002514 false, NULL, 0, 0, 0, srcline);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002515}
2516
Chris Phlipot9919a652016-04-28 01:19:06 -07002517static int thread__resolve_callchain_unwind(struct thread *thread,
2518 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002519 struct evsel *evsel,
Chris Phlipot9919a652016-04-28 01:19:06 -07002520 struct perf_sample *sample,
2521 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002522{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002523 /* Can we do dwarf post unwind? */
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002524 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2525 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002526 return 0;
2527
2528 /* Bail out if nothing was captured. */
2529 if ((!sample->user_regs.regs) ||
2530 (!sample->user_stack.size))
2531 return 0;
2532
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002533 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01002534 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07002535}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002536
Chris Phlipot9919a652016-04-28 01:19:06 -07002537int thread__resolve_callchain(struct thread *thread,
2538 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002539 struct evsel *evsel,
Chris Phlipot9919a652016-04-28 01:19:06 -07002540 struct perf_sample *sample,
2541 struct symbol **parent,
2542 struct addr_location *root_al,
2543 int max_stack)
2544{
2545 int ret = 0;
2546
Jiri Olsa914eb9c2017-08-06 16:39:39 +02002547 callchain_cursor_reset(cursor);
Chris Phlipot9919a652016-04-28 01:19:06 -07002548
2549 if (callchain_param.order == ORDER_CALLEE) {
2550 ret = thread__resolve_callchain_sample(thread, cursor,
2551 evsel, sample,
2552 parent, root_al,
2553 max_stack);
2554 if (ret)
2555 return ret;
2556 ret = thread__resolve_callchain_unwind(thread, cursor,
2557 evsel, sample,
2558 max_stack);
2559 } else {
2560 ret = thread__resolve_callchain_unwind(thread, cursor,
2561 evsel, sample,
2562 max_stack);
2563 if (ret)
2564 return ret;
2565 ret = thread__resolve_callchain_sample(thread, cursor,
2566 evsel, sample,
2567 parent, root_al,
2568 max_stack);
2569 }
2570
2571 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002572}
David Ahern35feee12013-09-28 13:12:58 -06002573
2574int machine__for_each_thread(struct machine *machine,
2575 int (*fn)(struct thread *thread, void *p),
2576 void *priv)
2577{
Kan Liang91e467b2017-09-10 19:23:14 -07002578 struct threads *threads;
David Ahern35feee12013-09-28 13:12:58 -06002579 struct rb_node *nd;
2580 struct thread *thread;
2581 int rc = 0;
Kan Liang91e467b2017-09-10 19:23:14 -07002582 int i;
David Ahern35feee12013-09-28 13:12:58 -06002583
Kan Liang91e467b2017-09-10 19:23:14 -07002584 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
2585 threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08002586 for (nd = rb_first_cached(&threads->entries); nd;
2587 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -07002588 thread = rb_entry(nd, struct thread, rb_node);
2589 rc = fn(thread, priv);
2590 if (rc != 0)
2591 return rc;
2592 }
David Ahern35feee12013-09-28 13:12:58 -06002593
Kan Liang91e467b2017-09-10 19:23:14 -07002594 list_for_each_entry(thread, &threads->dead, node) {
2595 rc = fn(thread, priv);
2596 if (rc != 0)
2597 return rc;
2598 }
David Ahern35feee12013-09-28 13:12:58 -06002599 }
2600 return rc;
2601}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002602
Adrian Huntera5499b32015-05-29 16:33:30 +03002603int machines__for_each_thread(struct machines *machines,
2604 int (*fn)(struct thread *thread, void *p),
2605 void *priv)
2606{
2607 struct rb_node *nd;
2608 int rc = 0;
2609
2610 rc = machine__for_each_thread(&machines->host, fn, priv);
2611 if (rc != 0)
2612 return rc;
2613
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08002614 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntera5499b32015-05-29 16:33:30 +03002615 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2616
2617 rc = machine__for_each_thread(machine, fn, priv);
2618 if (rc != 0)
2619 return rc;
2620 }
2621 return rc;
2622}
2623
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002624pid_t machine__get_current_tid(struct machine *machine, int cpu)
2625{
Kyle Meyer8c727462019-08-27 16:43:50 -05002626 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
2627
2628 if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002629 return -1;
2630
2631 return machine->current_tid[cpu];
2632}
2633
2634int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2635 pid_t tid)
2636{
2637 struct thread *thread;
Kyle Meyer8c727462019-08-27 16:43:50 -05002638 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002639
2640 if (cpu < 0)
2641 return -EINVAL;
2642
2643 if (!machine->current_tid) {
2644 int i;
2645
Kyle Meyer8c727462019-08-27 16:43:50 -05002646 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002647 if (!machine->current_tid)
2648 return -ENOMEM;
Kyle Meyer8c727462019-08-27 16:43:50 -05002649 for (i = 0; i < nr_cpus; i++)
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002650 machine->current_tid[i] = -1;
2651 }
2652
Kyle Meyer8c727462019-08-27 16:43:50 -05002653 if (cpu >= nr_cpus) {
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002654 pr_err("Requested CPU %d too large. ", cpu);
2655 pr_err("Consider raising MAX_NR_CPUS\n");
2656 return -EINVAL;
2657 }
2658
2659 machine->current_tid[cpu] = tid;
2660
2661 thread = machine__findnew_thread(machine, pid, tid);
2662 if (!thread)
2663 return -ENOMEM;
2664
2665 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002666 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002667
2668 return 0;
2669}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002670
Adrian Hunterdbbd34a2018-05-17 12:21:53 +03002671/*
2672 * Compares the raw arch string. N.B. see instead perf_env__arch() if a
2673 * normalized arch is needed.
2674 */
2675bool machine__is(struct machine *machine, const char *arch)
2676{
2677 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
2678}
2679
Adrian Hunter9cecca32018-05-22 13:54:32 +03002680int machine__nr_cpus_avail(struct machine *machine)
2681{
2682 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
2683}
2684
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002685int machine__get_kernel_start(struct machine *machine)
2686{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002687 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002688 int err = 0;
2689
2690 /*
2691 * The only addresses above 2^63 are kernel addresses of a 64-bit
2692 * kernel. Note that addresses are unsigned so that on a 32-bit system
2693 * all addresses including kernel addresses are less than 2^32. In
2694 * that case (32-bit system), if the kernel mapping is unknown, all
2695 * addresses will be assumed to be in user space - see
2696 * machine__kernel_ip().
2697 */
2698 machine->kernel_start = 1ULL << 63;
2699 if (map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002700 err = map__load(map);
Adrian Hunter19422a92018-05-17 12:21:54 +03002701 /*
2702 * On x86_64, PTI entry trampolines are less than the
2703 * start of kernel text, but still above 2^63. So leave
2704 * kernel_start = 1ULL << 63 for x86_64.
2705 */
2706 if (!err && !machine__is(machine, "x86_64"))
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002707 machine->kernel_start = map->start;
2708 }
2709 return err;
2710}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002711
Adrian Hunter8e80ad92018-11-06 23:07:10 +02002712u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
2713{
2714 u8 addr_cpumode = cpumode;
2715 bool kernel_ip;
2716
2717 if (!machine->single_address_space)
2718 goto out;
2719
2720 kernel_ip = machine__kernel_ip(machine, addr);
2721 switch (cpumode) {
2722 case PERF_RECORD_MISC_KERNEL:
2723 case PERF_RECORD_MISC_USER:
2724 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
2725 PERF_RECORD_MISC_USER;
2726 break;
2727 case PERF_RECORD_MISC_GUEST_KERNEL:
2728 case PERF_RECORD_MISC_GUEST_USER:
2729 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
2730 PERF_RECORD_MISC_GUEST_USER;
2731 break;
2732 default:
2733 break;
2734 }
2735out:
2736 return addr_cpumode;
2737}
2738
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002739struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2740{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03002741 return dsos__findnew(&machine->dsos, filename);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002742}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002743
2744char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2745{
2746 struct machine *machine = vmachine;
2747 struct map *map;
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03002748 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002749
2750 if (sym == NULL)
2751 return NULL;
2752
2753 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2754 *addrp = map->unmap_ip(map, sym->start);
2755 return sym->name;
2756}