blob: b4749d3eed08552ce19b304972be1140f3c5c2b5 [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"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030035
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030036#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030037#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -030038#include <linux/mman.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030039#include <linux/string.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030040#include <linux/zalloc.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030041
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030042static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
43
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030044static void dsos__init(struct dsos *dsos)
45{
46 INIT_LIST_HEAD(&dsos->head);
47 dsos->root = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030048 init_rwsem(&dsos->lock);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030049}
50
Kan Liang91e467b2017-09-10 19:23:14 -070051static void machine__threads_init(struct machine *machine)
52{
53 int i;
54
55 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
56 struct threads *threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -080057 threads->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030058 init_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -070059 threads->nr = 0;
60 INIT_LIST_HEAD(&threads->dead);
61 threads->last_match = NULL;
62 }
63}
64
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010065static int machine__set_mmap_name(struct machine *machine)
66{
Jiri Olsac1925242018-03-12 16:24:06 +010067 if (machine__is_host(machine))
68 machine->mmap_name = strdup("[kernel.kallsyms]");
69 else if (machine__is_default_guest(machine))
70 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
71 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
72 machine->pid) < 0)
73 machine->mmap_name = NULL;
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +010074
75 return machine->mmap_name ? 0 : -ENOMEM;
76}
77
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030078int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
79{
Jiri Olsa81f981d2018-02-15 13:26:29 +010080 int err = -ENOMEM;
81
Wang Nan93b0ba32015-12-08 02:25:44 +000082 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030083 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030084 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030085 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030086
Kan Liang91e467b2017-09-10 19:23:14 -070087 machine__threads_init(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030088
Adrian Hunterd027b6402014-07-23 14:23:00 +030089 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030090 machine->env = NULL;
Adrian Hunterd027b6402014-07-23 14:23:00 +030091
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030092 machine->pid = pid;
93
Jiri Olsa14bd6d22014-01-07 13:47:19 +010094 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -030095 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +030096 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030097 machine->kernel_start = 0;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -030098 machine->vmlinux_map = NULL;
Masami Hiramatsucc1121a2015-12-09 11:11:33 +090099
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300100 machine->root_dir = strdup(root_dir);
101 if (machine->root_dir == NULL)
102 return -ENOMEM;
103
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100104 if (machine__set_mmap_name(machine))
105 goto out;
106
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300107 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +0300108 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +0300109 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300110 char comm[64];
111
112 if (thread == NULL)
Jiri Olsa81f981d2018-02-15 13:26:29 +0100113 goto out;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300114
115 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200116 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300117 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300118 }
119
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300120 machine->current_tid = NULL;
Jiri Olsa81f981d2018-02-15 13:26:29 +0100121 err = 0;
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300122
Jiri Olsa81f981d2018-02-15 13:26:29 +0100123out:
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100124 if (err) {
Jiri Olsa81f981d2018-02-15 13:26:29 +0100125 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100126 zfree(&machine->mmap_name);
127 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300128 return 0;
129}
130
David Ahern8fb598e2013-09-28 13:13:00 -0600131struct machine *machine__new_host(void)
132{
133 struct machine *machine = malloc(sizeof(*machine));
134
135 if (machine != NULL) {
136 machine__init(machine, "", HOST_KERNEL_ID);
137
138 if (machine__create_kernel_maps(machine) < 0)
139 goto out_delete;
140 }
141
142 return machine;
143out_delete:
144 free(machine);
145 return NULL;
146}
147
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300148struct machine *machine__new_kallsyms(void)
149{
150 struct machine *machine = machine__new_host();
151 /*
152 * FIXME:
Ingo Molnaradba1632018-12-03 11:22:00 +0100153 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300154 * ask for not using the kcore parsing code, once this one is fixed
155 * to create a map per module.
156 */
Arnaldo Carvalho de Melo329f0ad2018-04-25 11:40:32 -0300157 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300158 machine__delete(machine);
159 machine = NULL;
160 }
161
162 return machine;
163}
164
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300165static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300166{
167 struct dso *pos, *n;
168
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300169 down_write(&dsos->lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300170
Waiman Long8fa7d872014-09-29 16:07:28 -0400171 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -0400172 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +0200173 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300174 list_del_init(&pos->node);
175 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300176 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300177
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300178 up_write(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300179}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300180
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300181static void dsos__exit(struct dsos *dsos)
182{
183 dsos__purge(dsos);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300184 exit_rwsem(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300185}
186
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300187void machine__delete_threads(struct machine *machine)
188{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300189 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700190 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300191
Kan Liang91e467b2017-09-10 19:23:14 -0700192 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
193 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300194 down_write(&threads->lock);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800195 nd = rb_first_cached(&threads->entries);
Kan Liang91e467b2017-09-10 19:23:14 -0700196 while (nd) {
197 struct thread *t = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300198
Kan Liang91e467b2017-09-10 19:23:14 -0700199 nd = rb_next(nd);
200 __machine__remove_thread(machine, t, false);
201 }
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300202 up_write(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300203 }
204}
205
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300206void machine__exit(struct machine *machine)
207{
Kan Liang91e467b2017-09-10 19:23:14 -0700208 int i;
209
Arnaldo Carvalho de Melo19993b82017-11-13 16:06:29 -0300210 if (machine == NULL)
211 return;
212
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900213 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300214 map_groups__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300215 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300216 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300217 zfree(&machine->root_dir);
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100218 zfree(&machine->mmap_name);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300219 zfree(&machine->current_tid);
Kan Liang91e467b2017-09-10 19:23:14 -0700220
221 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
222 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -0300223 struct thread *thread, *n;
224 /*
225 * Forget about the dead, at this point whatever threads were
226 * left in the dead lists better have a reference count taken
227 * by who is using them, and then, when they drop those references
228 * and it finally hits zero, thread__put() will check and see that
229 * its not in the dead threads list and will not try to remove it
230 * from there, just calling thread__delete() straight away.
231 */
232 list_for_each_entry_safe(thread, n, &threads->dead, node)
233 list_del_init(&thread->node);
234
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300235 exit_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -0700236 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300237}
238
239void machine__delete(struct machine *machine)
240{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300241 if (machine) {
242 machine__exit(machine);
243 free(machine);
244 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300245}
246
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300247void machines__init(struct machines *machines)
248{
249 machine__init(&machines->host, "", HOST_KERNEL_ID);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800250 machines->guests = RB_ROOT_CACHED;
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300251}
252
253void machines__exit(struct machines *machines)
254{
255 machine__exit(&machines->host);
256 /* XXX exit guest */
257}
258
259struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300260 const char *root_dir)
261{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800262 struct rb_node **p = &machines->guests.rb_root.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300263 struct rb_node *parent = NULL;
264 struct machine *pos, *machine = malloc(sizeof(*machine));
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800265 bool leftmost = true;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300266
267 if (machine == NULL)
268 return NULL;
269
270 if (machine__init(machine, root_dir, pid) != 0) {
271 free(machine);
272 return NULL;
273 }
274
275 while (*p != NULL) {
276 parent = *p;
277 pos = rb_entry(parent, struct machine, rb_node);
278 if (pid < pos->pid)
279 p = &(*p)->rb_left;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800280 else {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300281 p = &(*p)->rb_right;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800282 leftmost = false;
283 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300284 }
285
286 rb_link_node(&machine->rb_node, parent, p);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800287 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300288
289 return machine;
290}
291
Adrian Huntercfe1c412014-07-31 09:00:45 +0300292void machines__set_comm_exec(struct machines *machines, bool comm_exec)
293{
294 struct rb_node *nd;
295
296 machines->host.comm_exec = comm_exec;
297
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800298 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntercfe1c412014-07-31 09:00:45 +0300299 struct machine *machine = rb_entry(nd, struct machine, rb_node);
300
301 machine->comm_exec = comm_exec;
302 }
303}
304
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300305struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300306{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800307 struct rb_node **p = &machines->guests.rb_root.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300308 struct rb_node *parent = NULL;
309 struct machine *machine;
310 struct machine *default_machine = NULL;
311
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300312 if (pid == HOST_KERNEL_ID)
313 return &machines->host;
314
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300315 while (*p != NULL) {
316 parent = *p;
317 machine = rb_entry(parent, struct machine, rb_node);
318 if (pid < machine->pid)
319 p = &(*p)->rb_left;
320 else if (pid > machine->pid)
321 p = &(*p)->rb_right;
322 else
323 return machine;
324 if (!machine->pid)
325 default_machine = machine;
326 }
327
328 return default_machine;
329}
330
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300331struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300332{
333 char path[PATH_MAX];
334 const char *root_dir = "";
335 struct machine *machine = machines__find(machines, pid);
336
337 if (machine && (machine->pid == pid))
338 goto out;
339
340 if ((pid != HOST_KERNEL_ID) &&
341 (pid != DEFAULT_GUEST_KERNEL_ID) &&
342 (symbol_conf.guestmount)) {
343 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
344 if (access(path, R_OK)) {
345 static struct strlist *seen;
346
347 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300348 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300349
350 if (!strlist__has_entry(seen, path)) {
351 pr_err("Can't access file %s\n", path);
352 strlist__add(seen, path);
353 }
354 machine = NULL;
355 goto out;
356 }
357 root_dir = path;
358 }
359
360 machine = machines__add(machines, pid, root_dir);
361out:
362 return machine;
363}
364
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300365void machines__process_guests(struct machines *machines,
366 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300367{
368 struct rb_node *nd;
369
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800370 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300371 struct machine *pos = rb_entry(nd, struct machine, rb_node);
372 process(pos, data);
373 }
374}
375
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300376void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300377{
378 struct rb_node *node;
379 struct machine *machine;
380
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300381 machines->host.id_hdr_size = id_hdr_size;
382
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800383 for (node = rb_first_cached(&machines->guests); node;
384 node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300385 machine = rb_entry(node, struct machine, rb_node);
386 machine->id_hdr_size = id_hdr_size;
387 }
388
389 return;
390}
391
Adrian Hunter29ce3612014-07-16 11:07:13 +0300392static void machine__update_thread_pid(struct machine *machine,
393 struct thread *th, pid_t pid)
394{
395 struct thread *leader;
396
397 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
398 return;
399
400 th->pid_ = pid;
401
402 if (th->pid_ == th->tid)
403 return;
404
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300405 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300406 if (!leader)
407 goto out_err;
408
409 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300410 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300411
412 if (!leader->mg)
413 goto out_err;
414
415 if (th->mg == leader->mg)
416 return;
417
418 if (th->mg) {
419 /*
420 * Maps are created from MMAP events which provide the pid and
421 * tid. Consequently there never should be any maps on a thread
422 * with an unknown pid. Just print an error if there are.
423 */
424 if (!map_groups__empty(th->mg))
425 pr_err("Discarding thread maps for %d:%d\n",
426 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300427 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300428 }
429
430 th->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300431out_put:
432 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300433 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300434out_err:
435 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300436 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300437}
438
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300439/*
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200440 * Front-end cache - TID lookups come in blocks,
441 * so most of the time we dont have to look up
442 * the full rbtree:
443 */
444static struct thread*
Jiri Olsab57334b2018-07-19 16:33:44 +0200445__threads__get_last_match(struct threads *threads, struct machine *machine,
446 int pid, int tid)
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200447{
448 struct thread *th;
449
450 th = threads->last_match;
451 if (th != NULL) {
452 if (th->tid == tid) {
453 machine__update_thread_pid(machine, th, pid);
454 return thread__get(th);
455 }
456
457 threads->last_match = NULL;
458 }
459
460 return NULL;
461}
462
Jiri Olsab57334b2018-07-19 16:33:44 +0200463static struct thread*
464threads__get_last_match(struct threads *threads, struct machine *machine,
465 int pid, int tid)
466{
467 struct thread *th = NULL;
468
469 if (perf_singlethreaded)
470 th = __threads__get_last_match(threads, machine, pid, tid);
471
472 return th;
473}
474
475static void
476__threads__set_last_match(struct threads *threads, struct thread *th)
477{
478 threads->last_match = th;
479}
480
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200481static void
482threads__set_last_match(struct threads *threads, struct thread *th)
483{
Jiri Olsab57334b2018-07-19 16:33:44 +0200484 if (perf_singlethreaded)
485 __threads__set_last_match(threads, th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200486}
487
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200488/*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800489 * Caller must eventually drop thread->refcnt returned with a successful
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300490 * lookup/new thread inserted.
491 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300492static struct thread *____machine__findnew_thread(struct machine *machine,
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300493 struct threads *threads,
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300494 pid_t pid, pid_t tid,
495 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300496{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800497 struct rb_node **p = &threads->entries.rb_root.rb_node;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300498 struct rb_node *parent = NULL;
499 struct thread *th;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800500 bool leftmost = true;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300501
Jiri Olsaf8b2ebb2018-07-19 16:33:42 +0200502 th = threads__get_last_match(threads, machine, pid, tid);
503 if (th)
504 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300505
506 while (*p != NULL) {
507 parent = *p;
508 th = rb_entry(parent, struct thread, rb_node);
509
Adrian Hunter38051232013-07-04 16:20:31 +0300510 if (th->tid == tid) {
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200511 threads__set_last_match(threads, th);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300512 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300513 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300514 }
515
Adrian Hunter38051232013-07-04 16:20:31 +0300516 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300517 p = &(*p)->rb_left;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800518 else {
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300519 p = &(*p)->rb_right;
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800520 leftmost = false;
521 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300522 }
523
524 if (!create)
525 return NULL;
526
Adrian Hunter99d725f2013-08-26 16:00:19 +0300527 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300528 if (th != NULL) {
529 rb_link_node(&th->rb_node, parent, p);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800530 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
Jiri Olsacddcef62014-04-09 20:54:29 +0200531
532 /*
533 * We have to initialize map_groups separately
534 * after rb tree is updated.
535 *
536 * The reason is that we call machine__findnew_thread
537 * within thread__init_map_groups to find the thread
538 * leader and that would screwed the rb tree.
539 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300540 if (thread__init_map_groups(th, machine)) {
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800541 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300542 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300543 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200544 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300545 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300546 /*
547 * It is now in the rbtree, get a ref
548 */
549 thread__get(th);
Jiri Olsa67fda0f2018-07-19 16:33:43 +0200550 threads__set_last_match(threads, th);
Kan Liang91e467b2017-09-10 19:23:14 -0700551 ++threads->nr;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300552 }
553
554 return th;
555}
556
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300557struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
558{
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300559 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300560}
561
Adrian Hunter314add62013-08-27 11:23:03 +0300562struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
563 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300564{
Kan Liang91e467b2017-09-10 19:23:14 -0700565 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300566 struct thread *th;
567
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300568 down_write(&threads->lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300569 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300570 up_write(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300571 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300572}
573
Jiri Olsad75e6092014-03-14 15:00:03 +0100574struct thread *machine__find_thread(struct machine *machine, pid_t pid,
575 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300576{
Kan Liang91e467b2017-09-10 19:23:14 -0700577 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300578 struct thread *th;
Kan Liang91e467b2017-09-10 19:23:14 -0700579
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300580 down_read(&threads->lock);
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300581 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300582 up_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300583 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300584}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300585
Adrian Huntercfe1c412014-07-31 09:00:45 +0300586struct comm *machine__thread_exec_comm(struct machine *machine,
587 struct thread *thread)
588{
589 if (machine->comm_exec)
590 return thread__exec_comm(thread);
591 else
592 return thread__comm(thread);
593}
594
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200595int machine__process_comm_event(struct machine *machine, union perf_event *event,
596 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300597{
Adrian Hunter314add62013-08-27 11:23:03 +0300598 struct thread *thread = machine__findnew_thread(machine,
599 event->comm.pid,
600 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300601 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300602 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300603
Adrian Huntercfe1c412014-07-31 09:00:45 +0300604 if (exec)
605 machine->comm_exec = true;
606
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300607 if (dump_trace)
608 perf_event__fprintf_comm(event, stdout);
609
Adrian Hunter65de51f2014-07-31 09:00:44 +0300610 if (thread == NULL ||
611 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300612 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300613 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300614 }
615
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300616 thread__put(thread);
617
618 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300619}
620
Hari Bathinif3b36142017-03-08 02:11:43 +0530621int machine__process_namespaces_event(struct machine *machine __maybe_unused,
622 union perf_event *event,
623 struct perf_sample *sample __maybe_unused)
624{
625 struct thread *thread = machine__findnew_thread(machine,
626 event->namespaces.pid,
627 event->namespaces.tid);
628 int err = 0;
629
630 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
631 "\nWARNING: kernel seems to support more namespaces than perf"
632 " tool.\nTry updating the perf tool..\n\n");
633
634 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
635 "\nWARNING: perf tool seems to support more namespaces than"
636 " the kernel.\nTry updating the kernel..\n\n");
637
638 if (dump_trace)
639 perf_event__fprintf_namespaces(event, stdout);
640
641 if (thread == NULL ||
642 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
643 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
644 err = -1;
645 }
646
647 thread__put(thread);
648
649 return err;
650}
651
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300652int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200653 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300654{
Jiri Olsa5290ed62019-08-25 20:17:46 +0200655 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300656 event->lost.id, event->lost.lost);
657 return 0;
658}
659
Kan Liangc4937a92015-05-10 15:13:15 -0400660int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
661 union perf_event *event, struct perf_sample *sample)
662{
Jiri Olsaa2e254d2019-08-25 20:17:47 +0200663 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
Kan Liangc4937a92015-05-10 15:13:15 -0400664 sample->id, event->lost_samples.lost);
665 return 0;
666}
667
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300668static struct dso *machine__findnew_module_dso(struct machine *machine,
669 struct kmod_path *m,
670 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100671{
672 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100673
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300674 down_write(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300675
676 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100677 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300678 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100679 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300680 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100681
Namhyung Kim6b335e82017-05-31 21:01:04 +0900682 dso__set_module_info(dso, m, machine);
Jiri Olsaca333802015-02-17 17:29:57 +0100683 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100684 }
685
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300686 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300687out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300688 up_write(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100689 return dso;
690}
691
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300692int machine__process_aux_event(struct machine *machine __maybe_unused,
693 union perf_event *event)
694{
695 if (dump_trace)
696 perf_event__fprintf_aux(event, stdout);
697 return 0;
698}
699
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300700int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
701 union perf_event *event)
702{
703 if (dump_trace)
704 perf_event__fprintf_itrace_start(event, stdout);
705 return 0;
706}
707
Adrian Hunter02860392015-07-21 12:44:03 +0300708int machine__process_switch_event(struct machine *machine __maybe_unused,
709 union perf_event *event)
710{
711 if (dump_trace)
712 perf_event__fprintf_switch(event, stdout);
713 return 0;
714}
715
Song Liu9aa0bfa2019-01-17 08:15:17 -0800716static int machine__process_ksymbol_register(struct machine *machine,
717 union perf_event *event,
718 struct perf_sample *sample __maybe_unused)
719{
720 struct symbol *sym;
721 struct map *map;
722
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300723 map = map_groups__find(&machine->kmaps, event->ksymbol.addr);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800724 if (!map) {
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300725 map = dso__new_map(event->ksymbol.name);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800726 if (!map)
727 return -ENOMEM;
728
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300729 map->start = event->ksymbol.addr;
730 map->end = map->start + event->ksymbol.len;
Song Liu9aa0bfa2019-01-17 08:15:17 -0800731 map_groups__insert(&machine->kmaps, map);
732 }
733
Jiri Olsa8529f2e2019-05-08 15:20:04 +0200734 sym = symbol__new(map->map_ip(map, map->start),
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300735 event->ksymbol.len,
736 0, 0, event->ksymbol.name);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800737 if (!sym)
738 return -ENOMEM;
739 dso__insert_symbol(map->dso, sym);
740 return 0;
741}
742
743static int machine__process_ksymbol_unregister(struct machine *machine,
744 union perf_event *event,
745 struct perf_sample *sample __maybe_unused)
746{
747 struct map *map;
748
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300749 map = map_groups__find(&machine->kmaps, event->ksymbol.addr);
Song Liu9aa0bfa2019-01-17 08:15:17 -0800750 if (map)
751 map_groups__remove(&machine->kmaps, map);
752
753 return 0;
754}
755
756int machine__process_ksymbol(struct machine *machine __maybe_unused,
757 union perf_event *event,
758 struct perf_sample *sample)
759{
760 if (dump_trace)
761 perf_event__fprintf_ksymbol(event, stdout);
762
Arnaldo Carvalho de Meloebdba162019-08-26 19:15:18 -0300763 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
Song Liu9aa0bfa2019-01-17 08:15:17 -0800764 return machine__process_ksymbol_unregister(machine, event,
765 sample);
766 return machine__process_ksymbol_register(machine, event, sample);
767}
768
Wang Nanc03d5182015-11-26 03:59:57 +0000769static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
770{
771 const char *dup_filename;
772
773 if (!filename || !dso || !dso->long_name)
774 return;
775 if (dso->long_name[0] != '[')
776 return;
777 if (!strchr(filename, '/'))
778 return;
779
780 dup_filename = strdup(filename);
781 if (!dup_filename)
782 return;
783
Wang Nan5dcf16d2015-12-07 02:36:25 +0000784 dso__set_long_name(dso, dup_filename, true);
Wang Nanc03d5182015-11-26 03:59:57 +0000785}
786
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300787struct map *machine__findnew_module_map(struct machine *machine, u64 start,
788 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300789{
Jiri Olsaca333802015-02-17 17:29:57 +0100790 struct map *map = NULL;
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900791 struct dso *dso = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100792 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300793
Jiri Olsaca333802015-02-17 17:29:57 +0100794 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300795 return NULL;
796
Arnaldo Carvalho de Melo83cf7742018-04-24 12:16:09 -0300797 map = map_groups__find_by_name(&machine->kmaps, m.name);
Wang Nanc03d5182015-11-26 03:59:57 +0000798 if (map) {
799 /*
800 * If the map's dso is an offline module, give dso__load()
801 * a chance to find the file path of that module by fixing
802 * long_name.
803 */
804 dso__adjust_kmod_long_name(map->dso, filename);
Jiri Olsabc84f462015-02-17 17:31:18 +0100805 goto out;
Wang Nanc03d5182015-11-26 03:59:57 +0000806 }
Jiri Olsabc84f462015-02-17 17:31:18 +0100807
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300808 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100809 if (dso == NULL)
810 goto out;
811
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300812 map = map__new2(start, dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300813 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100814 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300815
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300816 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100817
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900818 /* Put the map here because map_groups__insert alread got it */
819 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100820out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900821 /* put the dso here, corresponding to machine__findnew_module_dso */
822 dso__put(dso);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300823 zfree(&m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300824 return map;
825}
826
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300827size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300828{
829 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300830 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300831
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800832 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300833 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300834 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300835 }
836
837 return ret;
838}
839
Waiman Long8fa7d872014-09-29 16:07:28 -0400840size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300841 bool (skip)(struct dso *dso, int parm), int parm)
842{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300843 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300844}
845
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300846size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300847 bool (skip)(struct dso *dso, int parm), int parm)
848{
849 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300850 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300851
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800852 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300853 struct machine *pos = rb_entry(nd, struct machine, rb_node);
854 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
855 }
856 return ret;
857}
858
859size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
860{
861 int i;
862 size_t printed = 0;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300863 struct dso *kdso = machine__kernel_map(machine)->dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300864
865 if (kdso->has_build_id) {
866 char filename[PATH_MAX];
Krister Johansend2396992017-07-05 18:48:13 -0700867 if (dso__build_id_filename(kdso, filename, sizeof(filename),
868 false))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300869 printed += fprintf(fp, "[0] %s\n", filename);
870 }
871
872 for (i = 0; i < vmlinux_path__nr_entries; ++i)
873 printed += fprintf(fp, "[%d] %s\n",
874 i + kdso->has_build_id, vmlinux_path[i]);
875
876 return printed;
877}
878
879size_t machine__fprintf(struct machine *machine, FILE *fp)
880{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300881 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700882 size_t ret;
883 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300884
Kan Liang91e467b2017-09-10 19:23:14 -0700885 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
886 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300887
888 down_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300889
Kan Liang91e467b2017-09-10 19:23:14 -0700890 ret = fprintf(fp, "Threads: %u\n", threads->nr);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300891
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -0800892 for (nd = rb_first_cached(&threads->entries); nd;
893 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -0700894 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300895
Kan Liang91e467b2017-09-10 19:23:14 -0700896 ret += thread__fprintf(pos, fp);
897 }
898
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300899 up_read(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300900 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300901 return ret;
902}
903
904static struct dso *machine__get_kernel(struct machine *machine)
905{
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +0100906 const char *vmlinux_name = machine->mmap_name;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300907 struct dso *kernel;
908
909 if (machine__is_host(machine)) {
Jiri Olsac1925242018-03-12 16:24:06 +0100910 if (symbol_conf.vmlinux_name)
911 vmlinux_name = symbol_conf.vmlinux_name;
912
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300913 kernel = machine__findnew_kernel(machine, vmlinux_name,
914 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300915 } else {
Jiri Olsac1925242018-03-12 16:24:06 +0100916 if (symbol_conf.default_guest_vmlinux_name)
917 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
918
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300919 kernel = machine__findnew_kernel(machine, vmlinux_name,
920 "[guest.kernel]",
921 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300922 }
923
924 if (kernel != NULL && (!kernel->has_build_id))
925 dso__read_running_kernel_build_id(kernel, machine);
926
927 return kernel;
928}
929
930struct process_args {
931 u64 start;
932};
933
Adrian Hunter1c5aae72018-05-22 13:54:36 +0300934void machine__get_kallsyms_filename(struct machine *machine, char *buf,
935 size_t bufsz)
Adrian Hunter15a0a872014-01-29 16:14:38 +0200936{
937 if (machine__is_default_guest(machine))
938 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
939 else
940 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
941}
942
Simon Quea93f0e52014-06-16 11:32:09 -0700943const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
944
945/* Figure out the start address of kernel map from /proc/kallsyms.
946 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
947 * symbol_name if it's not that important.
948 */
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300949static int machine__get_running_kernel_start(struct machine *machine,
Jiri Olsaed9adb22019-05-08 15:20:03 +0200950 const char **symbol_name,
951 u64 *start, u64 *end)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300952{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200953 char filename[PATH_MAX];
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300954 int i, err = -1;
Simon Quea93f0e52014-06-16 11:32:09 -0700955 const char *name;
956 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300957
Adrian Hunter15a0a872014-01-29 16:14:38 +0200958 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300959
960 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
961 return 0;
962
Simon Quea93f0e52014-06-16 11:32:09 -0700963 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300964 err = kallsyms__get_function_start(filename, name, &addr);
965 if (!err)
Simon Quea93f0e52014-06-16 11:32:09 -0700966 break;
967 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300968
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300969 if (err)
970 return -1;
971
Simon Quea93f0e52014-06-16 11:32:09 -0700972 if (symbol_name)
973 *symbol_name = name;
974
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300975 *start = addr;
Jiri Olsaed9adb22019-05-08 15:20:03 +0200976
977 err = kallsyms__get_function_start(filename, "_etext", &addr);
978 if (!err)
979 *end = addr;
980
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300981 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300982}
983
Adrian Hunter1c5aae72018-05-22 13:54:36 +0300984int machine__create_extra_kernel_map(struct machine *machine,
985 struct dso *kernel,
986 struct extra_kernel_map *xm)
Adrian Hunter4d99e412018-05-22 13:54:33 +0300987{
988 struct kmap *kmap;
989 struct map *map;
990
991 map = map__new2(xm->start, kernel);
992 if (!map)
993 return -1;
994
995 map->end = xm->end;
996 map->pgoff = xm->pgoff;
997
998 kmap = map__kmap(map);
999
1000 kmap->kmaps = &machine->kmaps;
Adrian Hunter5759a682018-05-22 13:54:35 +03001001 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001002
1003 map_groups__insert(&machine->kmaps, map);
1004
Adrian Hunter5759a682018-05-22 13:54:35 +03001005 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
1006 kmap->name, map->start, map->end);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001007
1008 map__put(map);
1009
1010 return 0;
1011}
1012
1013static u64 find_entry_trampoline(struct dso *dso)
1014{
1015 /* Duplicates are removed so lookup all aliases */
1016 const char *syms[] = {
1017 "_entry_trampoline",
1018 "__entry_trampoline_start",
1019 "entry_SYSCALL_64_trampoline",
1020 };
1021 struct symbol *sym = dso__first_symbol(dso);
1022 unsigned int i;
1023
1024 for (; sym; sym = dso__next_symbol(sym)) {
1025 if (sym->binding != STB_GLOBAL)
1026 continue;
1027 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1028 if (!strcmp(sym->name, syms[i]))
1029 return sym->start;
1030 }
1031 }
1032
1033 return 0;
1034}
1035
1036/*
1037 * These values can be used for kernels that do not have symbols for the entry
1038 * trampolines in kallsyms.
1039 */
1040#define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1041#define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1042#define X86_64_ENTRY_TRAMPOLINE 0x6000
1043
1044/* Map x86_64 PTI entry trampolines */
1045int machine__map_x86_64_entry_trampolines(struct machine *machine,
1046 struct dso *kernel)
1047{
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001048 struct map_groups *kmaps = &machine->kmaps;
1049 struct maps *maps = &kmaps->maps;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001050 int nr_cpus_avail, cpu;
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001051 bool found = false;
1052 struct map *map;
1053 u64 pgoff;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001054
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001055 /*
1056 * In the vmlinux case, pgoff is a virtual address which must now be
1057 * mapped to a vmlinux offset.
1058 */
1059 for (map = maps__first(maps); map; map = map__next(map)) {
1060 struct kmap *kmap = __map__kmap(map);
1061 struct map *dest_map;
1062
1063 if (!kmap || !is_entry_trampoline(kmap->name))
1064 continue;
1065
1066 dest_map = map_groups__find(kmaps, map->pgoff);
1067 if (dest_map != map)
1068 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1069 found = true;
1070 }
1071 if (found || machine->trampolines_mapped)
1072 return 0;
1073
1074 pgoff = find_entry_trampoline(kernel);
Adrian Hunter4d99e412018-05-22 13:54:33 +03001075 if (!pgoff)
1076 return 0;
1077
1078 nr_cpus_avail = machine__nr_cpus_avail(machine);
1079
1080 /* Add a 1 page map for each CPU's entry trampoline */
1081 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1082 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1083 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1084 X86_64_ENTRY_TRAMPOLINE;
1085 struct extra_kernel_map xm = {
1086 .start = va,
1087 .end = va + page_size,
1088 .pgoff = pgoff,
1089 };
1090
Adrian Hunter5759a682018-05-22 13:54:35 +03001091 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1092
Adrian Hunter4d99e412018-05-22 13:54:33 +03001093 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1094 return -1;
1095 }
1096
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001097 machine->trampolines_mapped = nr_cpus_avail;
1098
1099 return 0;
1100}
1101
1102int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1103 struct dso *kernel __maybe_unused)
1104{
Adrian Hunter4d99e412018-05-22 13:54:33 +03001105 return 0;
1106}
1107
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001108static int
1109__machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001110{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001111 struct kmap *kmap;
1112 struct map *map;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001113
Masami Hiramatsucc1121a2015-12-09 11:11:33 +09001114 /* In case of renewal the kernel map, destroy previous one */
1115 machine__destroy_kernel_maps(machine);
1116
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001117 machine->vmlinux_map = map__new2(0, kernel);
1118 if (machine->vmlinux_map == NULL)
1119 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001120
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001121 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1122 map = machine__kernel_map(machine);
1123 kmap = map__kmap(map);
1124 if (!kmap)
1125 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001126
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001127 kmap->kmaps = &machine->kmaps;
1128 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001129
1130 return 0;
1131}
1132
1133void machine__destroy_kernel_maps(struct machine *machine)
1134{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001135 struct kmap *kmap;
1136 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001137
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001138 if (map == NULL)
1139 return;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001140
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001141 kmap = map__kmap(map);
1142 map_groups__remove(&machine->kmaps, map);
1143 if (kmap && kmap->ref_reloc_sym) {
1144 zfree((char **)&kmap->ref_reloc_sym->name);
1145 zfree(&kmap->ref_reloc_sym);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001146 }
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001147
1148 map__zput(machine->vmlinux_map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001149}
1150
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001151int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001152{
1153 int ret = 0;
1154 struct dirent **namelist = NULL;
1155 int i, items = 0;
1156 char path[PATH_MAX];
1157 pid_t pid;
1158 char *endp;
1159
1160 if (symbol_conf.default_guest_vmlinux_name ||
1161 symbol_conf.default_guest_modules ||
1162 symbol_conf.default_guest_kallsyms) {
1163 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1164 }
1165
1166 if (symbol_conf.guestmount) {
1167 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1168 if (items <= 0)
1169 return -ENOENT;
1170 for (i = 0; i < items; i++) {
1171 if (!isdigit(namelist[i]->d_name[0])) {
1172 /* Filter out . and .. */
1173 continue;
1174 }
1175 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1176 if ((*endp != '\0') ||
1177 (endp == namelist[i]->d_name) ||
1178 (errno == ERANGE)) {
1179 pr_debug("invalid directory (%s). Skipping.\n",
1180 namelist[i]->d_name);
1181 continue;
1182 }
1183 sprintf(path, "%s/%s/proc/kallsyms",
1184 symbol_conf.guestmount,
1185 namelist[i]->d_name);
1186 ret = access(path, R_OK);
1187 if (ret) {
1188 pr_debug("Can't access file %s\n", path);
1189 goto failure;
1190 }
1191 machines__create_kernel_maps(machines, pid);
1192 }
1193failure:
1194 free(namelist);
1195 }
1196
1197 return ret;
1198}
1199
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001200void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001201{
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001202 struct rb_node *next = rb_first_cached(&machines->guests);
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001203
1204 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001205
1206 while (next) {
1207 struct machine *pos = rb_entry(next, struct machine, rb_node);
1208
1209 next = rb_next(&pos->rb_node);
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001210 rb_erase_cached(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001211 machine__delete(pos);
1212 }
1213}
1214
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -03001215int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001216{
1217 struct machine *machine = machines__findnew(machines, pid);
1218
1219 if (machine == NULL)
1220 return -1;
1221
1222 return machine__create_kernel_maps(machine);
1223}
1224
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001225int machine__load_kallsyms(struct machine *machine, const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001226{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001227 struct map *map = machine__kernel_map(machine);
Jiri Olsae8f38792018-02-15 13:26:33 +01001228 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001229
1230 if (ret > 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001231 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001232 /*
1233 * Since /proc/kallsyms will have multiple sessions for the
1234 * kernel, with modules between them, fixup the end of all
1235 * sections.
1236 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001237 map_groups__fixup_end(&machine->kmaps);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001238 }
1239
1240 return ret;
1241}
1242
Arnaldo Carvalho de Melo1d1a2652018-04-25 12:18:11 -03001243int machine__load_vmlinux_path(struct machine *machine)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001244{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001245 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001246 int ret = dso__load_vmlinux_path(map->dso, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001247
Adrian Hunter39b12f782013-08-07 14:38:47 +03001248 if (ret > 0)
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001249 dso__set_loaded(map->dso);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001250
1251 return ret;
1252}
1253
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001254static char *get_kernel_version(const char *root_dir)
1255{
1256 char version[PATH_MAX];
1257 FILE *file;
1258 char *name, *tmp;
1259 const char *prefix = "Linux version ";
1260
1261 sprintf(version, "%s/proc/version", root_dir);
1262 file = fopen(version, "r");
1263 if (!file)
1264 return NULL;
1265
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001266 tmp = fgets(version, sizeof(version), file);
1267 fclose(file);
Donald Yandt34b65af2019-05-28 09:41:28 -04001268 if (!tmp)
1269 return NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001270
1271 name = strstr(version, prefix);
1272 if (!name)
1273 return NULL;
1274 name += strlen(prefix);
1275 tmp = strchr(name, ' ');
1276 if (tmp)
1277 *tmp = '\0';
1278
1279 return strdup(name);
1280}
1281
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001282static bool is_kmod_dso(struct dso *dso)
1283{
1284 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1285 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1286}
1287
1288static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1289 struct kmod_path *m)
1290{
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001291 char *long_name;
Arnaldo Carvalho de Melo83cf7742018-04-24 12:16:09 -03001292 struct map *map = map_groups__find_by_name(mg, m->name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001293
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001294 if (map == NULL)
1295 return 0;
1296
1297 long_name = strdup(path);
1298 if (long_name == NULL)
1299 return -ENOMEM;
1300
1301 dso__set_long_name(map->dso, long_name, true);
1302 dso__kernel_module_get_build_id(map->dso, "");
1303
1304 /*
1305 * Full name could reveal us kmod compression, so
1306 * we need to update the symtab_type if needed.
1307 */
Jiri Olsa2af52472018-08-17 11:48:07 +02001308 if (m->comp && is_kmod_dso(map->dso)) {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001309 map->dso->symtab_type++;
Jiri Olsa2af52472018-08-17 11:48:07 +02001310 map->dso->comp = m->comp;
1311 }
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001312
1313 return 0;
1314}
1315
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001316static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -04001317 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001318{
1319 struct dirent *dent;
1320 DIR *dir = opendir(dir_name);
1321 int ret = 0;
1322
1323 if (!dir) {
1324 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1325 return -1;
1326 }
1327
1328 while ((dent = readdir(dir)) != NULL) {
1329 char path[PATH_MAX];
1330 struct stat st;
1331
1332 /*sshfs might return bad dent->d_type, so we have to stat*/
1333 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1334 if (stat(path, &st))
1335 continue;
1336
1337 if (S_ISDIR(st.st_mode)) {
1338 if (!strcmp(dent->d_name, ".") ||
1339 !strcmp(dent->d_name, ".."))
1340 continue;
1341
Richard Yao61d42902014-04-26 13:17:55 -04001342 /* Do not follow top-level source and build symlinks */
1343 if (depth == 0) {
1344 if (!strcmp(dent->d_name, "source") ||
1345 !strcmp(dent->d_name, "build"))
1346 continue;
1347 }
1348
1349 ret = map_groups__set_modules_path_dir(mg, path,
1350 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001351 if (ret < 0)
1352 goto out;
1353 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001354 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001355
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001356 ret = kmod_path__parse_name(&m, dent->d_name);
1357 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001358 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001359
1360 if (m.kmod)
1361 ret = map_groups__set_module_path(mg, path, &m);
1362
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001363 zfree(&m.name);
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001364
1365 if (ret)
1366 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001367 }
1368 }
1369
1370out:
1371 closedir(dir);
1372 return ret;
1373}
1374
1375static int machine__set_modules_path(struct machine *machine)
1376{
1377 char *version;
1378 char modules_path[PATH_MAX];
1379
1380 version = get_kernel_version(machine->root_dir);
1381 if (!version)
1382 return -1;
1383
Richard Yao61d42902014-04-26 13:17:55 -04001384 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001385 machine->root_dir, version);
1386 free(version);
1387
Richard Yao61d42902014-04-26 13:17:55 -04001388 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001389}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001390int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
Thomas Richter12a6d292019-07-24 14:27:02 +02001391 u64 *size __maybe_unused,
Song Shan Gong203d8a42016-07-21 11:10:51 +08001392 const char *name __maybe_unused)
1393{
1394 return 0;
1395}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001396
Thomas Richter9ad46522017-08-03 15:49:02 +02001397static int machine__create_module(void *arg, const char *name, u64 start,
1398 u64 size)
Adrian Hunter316d70d2013-10-08 11:45:48 +03001399{
1400 struct machine *machine = arg;
1401 struct map *map;
1402
Thomas Richter12a6d292019-07-24 14:27:02 +02001403 if (arch__fix_module_text_start(&start, &size, name) < 0)
Song Shan Gong203d8a42016-07-21 11:10:51 +08001404 return -1;
1405
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001406 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001407 if (map == NULL)
1408 return -1;
Thomas Richter9ad46522017-08-03 15:49:02 +02001409 map->end = start + size;
Adrian Hunter316d70d2013-10-08 11:45:48 +03001410
1411 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1412
1413 return 0;
1414}
1415
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001416static int machine__create_modules(struct machine *machine)
1417{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001418 const char *modules;
1419 char path[PATH_MAX];
1420
Adrian Hunterf4be9042013-09-22 13:22:09 +03001421 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001422 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001423 } else {
1424 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001425 modules = path;
1426 }
1427
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001428 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001429 return -1;
1430
Adrian Hunter316d70d2013-10-08 11:45:48 +03001431 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001432 return -1;
1433
Adrian Hunter316d70d2013-10-08 11:45:48 +03001434 if (!machine__set_modules_path(machine))
1435 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001436
Adrian Hunter316d70d2013-10-08 11:45:48 +03001437 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001438
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001439 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001440}
1441
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001442static void machine__set_kernel_mmap(struct machine *machine,
1443 u64 start, u64 end)
1444{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001445 machine->vmlinux_map->start = start;
1446 machine->vmlinux_map->end = end;
1447 /*
1448 * Be a bit paranoid here, some perf.data file came with
1449 * a zero sized synthesized MMAP event for the kernel.
1450 */
1451 if (start == 0 && end == 0)
1452 machine->vmlinux_map->end = ~0ULL;
Jiri Olsa1fb87b82018-02-15 13:26:32 +01001453}
1454
Wei Li977c7a62019-02-28 17:20:03 +08001455static void machine__update_kernel_mmap(struct machine *machine,
1456 u64 start, u64 end)
1457{
1458 struct map *map = machine__kernel_map(machine);
1459
1460 map__get(map);
1461 map_groups__remove(&machine->kmaps, map);
1462
1463 machine__set_kernel_mmap(machine, start, end);
1464
1465 map_groups__insert(&machine->kmaps, map);
1466 map__put(map);
1467}
1468
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001469int machine__create_kernel_maps(struct machine *machine)
1470{
1471 struct dso *kernel = machine__get_kernel(machine);
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001472 const char *name = NULL;
Namhyung Kimee05d21792018-02-19 19:05:45 +09001473 struct map *map;
Jiri Olsaed9adb22019-05-08 15:20:03 +02001474 u64 start = 0, end = ~0ULL;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001475 int ret;
1476
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001477 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001478 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001479
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001480 ret = __machine__create_kernel_maps(machine, kernel);
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001481 if (ret < 0)
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001482 goto out_put;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001483
1484 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1485 if (machine__is_host(machine))
1486 pr_debug("Problems creating module maps, "
1487 "continuing anyway...\n");
1488 else
1489 pr_debug("Problems creating module maps for guest %d, "
1490 "continuing anyway...\n", machine->pid);
1491 }
1492
Jiri Olsaed9adb22019-05-08 15:20:03 +02001493 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001494 if (name &&
Jiri Olsaed9adb22019-05-08 15:20:03 +02001495 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001496 machine__destroy_kernel_maps(machine);
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001497 ret = -1;
1498 goto out_put;
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001499 }
Namhyung Kimee05d21792018-02-19 19:05:45 +09001500
Wei Li977c7a62019-02-28 17:20:03 +08001501 /*
1502 * we have a real start address now, so re-order the kmaps
1503 * assume it's the last in the kmaps
1504 */
Jiri Olsaed9adb22019-05-08 15:20:03 +02001505 machine__update_kernel_mmap(machine, start, end);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001506 }
1507
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001508 if (machine__create_extra_kernel_maps(machine, kernel))
1509 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1510
Jiri Olsaed9adb22019-05-08 15:20:03 +02001511 if (end == ~0ULL) {
1512 /* update end address of the kernel map using adjacent module address */
1513 map = map__next(machine__kernel_map(machine));
1514 if (map)
1515 machine__set_kernel_mmap(machine, start, map->start);
1516 }
1517
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001518out_put:
1519 dso__put(kernel);
1520 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001521}
1522
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001523static bool machine__uses_kcore(struct machine *machine)
1524{
1525 struct dso *dso;
1526
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001527 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001528 if (dso__is_kcore(dso))
1529 return true;
1530 }
1531
1532 return false;
1533}
1534
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001535static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1536 union perf_event *event)
1537{
1538 return machine__is(machine, "x86_64") &&
1539 is_entry_trampoline(event->mmap.filename);
1540}
1541
1542static int machine__process_extra_kernel_map(struct machine *machine,
1543 union perf_event *event)
1544{
1545 struct map *kernel_map = machine__kernel_map(machine);
1546 struct dso *kernel = kernel_map ? kernel_map->dso : NULL;
1547 struct extra_kernel_map xm = {
1548 .start = event->mmap.start,
1549 .end = event->mmap.start + event->mmap.len,
1550 .pgoff = event->mmap.pgoff,
1551 };
1552
1553 if (kernel == NULL)
1554 return -1;
1555
1556 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1557
1558 return machine__create_extra_kernel_map(machine, kernel, &xm);
1559}
1560
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001561static int machine__process_kernel_mmap_event(struct machine *machine,
1562 union perf_event *event)
1563{
1564 struct map *map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001565 enum dso_kernel_type kernel_type;
1566 bool is_kernel_mmap;
1567
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001568 /* If we have maps from kcore then we do not need or want any others */
1569 if (machine__uses_kcore(machine))
1570 return 0;
1571
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001572 if (machine__is_host(machine))
1573 kernel_type = DSO_TYPE_KERNEL;
1574 else
1575 kernel_type = DSO_TYPE_GUEST_KERNEL;
1576
1577 is_kernel_mmap = memcmp(event->mmap.filename,
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001578 machine->mmap_name,
1579 strlen(machine->mmap_name) - 1) == 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001580 if (event->mmap.filename[0] == '/' ||
1581 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001582 map = machine__findnew_module_map(machine, event->mmap.start,
1583 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001584 if (map == NULL)
1585 goto out_problem;
1586
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001587 map->end = map->start + event->mmap.len;
1588 } else if (is_kernel_mmap) {
1589 const char *symbol_name = (event->mmap.filename +
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001590 strlen(machine->mmap_name));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001591 /*
1592 * Should be there already, from the build-id table in
1593 * the header.
1594 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001595 struct dso *kernel = NULL;
1596 struct dso *dso;
1597
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001598 down_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001599
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001600 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001601
1602 /*
1603 * The cpumode passed to is_kernel_module is not the
1604 * cpumode of *this* event. If we insist on passing
1605 * correct cpumode to is_kernel_module, we should
1606 * record the cpumode when we adding this dso to the
1607 * linked list.
1608 *
1609 * However we don't really need passing correct
1610 * cpumode. We know the correct cpumode must be kernel
1611 * mode (if not, we should not link it onto kernel_dsos
1612 * list).
1613 *
1614 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1615 * is_kernel_module() treats it as a kernel cpumode.
1616 */
1617
1618 if (!dso->kernel ||
1619 is_kernel_module(dso->long_name,
1620 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001621 continue;
1622
Wang Nan1f121b02015-06-03 08:52:21 +00001623
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001624 kernel = dso;
1625 break;
1626 }
1627
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001628 up_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001629
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001630 if (kernel == NULL)
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001631 kernel = machine__findnew_dso(machine, machine->mmap_name);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001632 if (kernel == NULL)
1633 goto out_problem;
1634
1635 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001636 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1637 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001638 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001639 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001640
Namhyung Kim330dfa22014-11-18 13:30:28 +09001641 if (strstr(kernel->long_name, "vmlinux"))
1642 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001643
Wei Li977c7a62019-02-28 17:20:03 +08001644 machine__update_kernel_mmap(machine, event->mmap.start,
Jiri Olsa05db6ff2018-02-15 13:26:31 +01001645 event->mmap.start + event->mmap.len);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001646
1647 /*
1648 * Avoid using a zero address (kptr_restrict) for the ref reloc
1649 * symbol. Effectively having zero here means that at record
1650 * time /proc/sys/kernel/kptr_restrict was non zero.
1651 */
1652 if (event->mmap.pgoff != 0) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001653 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1654 symbol_name,
1655 event->mmap.pgoff);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001656 }
1657
1658 if (machine__is_default_guest(machine)) {
1659 /*
1660 * preload dso of guest kernel and modules
1661 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001662 dso__load(kernel, machine__kernel_map(machine));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001663 }
Adrian Huntera8ce99b2018-05-22 13:54:37 +03001664 } else if (perf_event__is_extra_kernel_mmap(machine, event)) {
1665 return machine__process_extra_kernel_map(machine, event);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001666 }
1667 return 0;
1668out_problem:
1669 return -1;
1670}
1671
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001672int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001673 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001674 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001675{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001676 struct thread *thread;
1677 struct map *map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001678 int ret = 0;
1679
1680 if (dump_trace)
1681 perf_event__fprintf_mmap2(event, stdout);
1682
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001683 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1684 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001685 ret = machine__process_kernel_mmap_event(machine, event);
1686 if (ret < 0)
1687 goto out_problem;
1688 return 0;
1689 }
1690
1691 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001692 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001693 if (thread == NULL)
1694 goto out_problem;
1695
Adrian Hunter2a030682014-07-22 16:17:53 +03001696 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001697 event->mmap2.len, event->mmap2.pgoff,
Krister Johansenbf2e7102017-07-05 18:48:09 -07001698 event->mmap2.maj,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001699 event->mmap2.min, event->mmap2.ino,
1700 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001701 event->mmap2.prot,
1702 event->mmap2.flags,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001703 event->mmap2.filename, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001704
1705 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001706 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001707
He Kuang8132a2a2016-06-03 03:33:13 +00001708 ret = thread__insert_map(thread, map);
1709 if (ret)
1710 goto out_problem_insert;
1711
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001712 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001713 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001714 return 0;
1715
He Kuang8132a2a2016-06-03 03:33:13 +00001716out_problem_insert:
1717 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001718out_problem_map:
1719 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001720out_problem:
1721 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1722 return 0;
1723}
1724
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001725int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001726 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001727{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001728 struct thread *thread;
1729 struct map *map;
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001730 u32 prot = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001731 int ret = 0;
1732
1733 if (dump_trace)
1734 perf_event__fprintf_mmap(event, stdout);
1735
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001736 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1737 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001738 ret = machine__process_kernel_mmap_event(machine, event);
1739 if (ret < 0)
1740 goto out_problem;
1741 return 0;
1742 }
1743
Adrian Hunter314add62013-08-27 11:23:03 +03001744 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001745 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001746 if (thread == NULL)
1747 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001748
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001749 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001750 prot = PROT_EXEC;
Stephane Eranianbad40912013-01-24 16:10:40 +01001751
Adrian Hunter2a030682014-07-22 16:17:53 +03001752 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001753 event->mmap.len, event->mmap.pgoff,
Arnaldo Carvalho de Melo0f476f22018-04-26 11:30:50 -03001754 0, 0, 0, 0, prot, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001755 event->mmap.filename,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001756 thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001757
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001758 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001759 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001760
He Kuang8132a2a2016-06-03 03:33:13 +00001761 ret = thread__insert_map(thread, map);
1762 if (ret)
1763 goto out_problem_insert;
1764
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001765 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001766 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001767 return 0;
1768
He Kuang8132a2a2016-06-03 03:33:13 +00001769out_problem_insert:
1770 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001771out_problem_map:
1772 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001773out_problem:
1774 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1775 return 0;
1776}
1777
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001778static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001779{
Kan Liang91e467b2017-09-10 19:23:14 -07001780 struct threads *threads = machine__threads(machine, th->tid);
1781
1782 if (threads->last_match == th)
Jiri Olsa67fda0f2018-07-19 16:33:43 +02001783 threads__set_last_match(threads, NULL);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001784
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001785 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001786 down_write(&threads->lock);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001787
1788 BUG_ON(refcount_read(&th->refcnt) == 0);
1789
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08001790 rb_erase_cached(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001791 RB_CLEAR_NODE(&th->rb_node);
Kan Liang91e467b2017-09-10 19:23:14 -07001792 --threads->nr;
David Ahern236a3bb2013-08-14 08:49:27 -06001793 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001794 * Move it first to the dead_threads list, then drop the reference,
1795 * if this is the last reference, then the thread__delete destructor
1796 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001797 */
Kan Liang91e467b2017-09-10 19:23:14 -07001798 list_add_tail(&th->node, &threads->dead);
Arnaldo Carvalho de Melo4c00af02019-07-05 12:11:35 -03001799
1800 /*
1801 * We need to do the put here because if this is the last refcount,
1802 * then we will be touching the threads->dead head when removing the
1803 * thread.
1804 */
1805 thread__put(th);
1806
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001807 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001808 up_write(&threads->lock);
David Ahern236a3bb2013-08-14 08:49:27 -06001809}
1810
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001811void machine__remove_thread(struct machine *machine, struct thread *th)
1812{
1813 return __machine__remove_thread(machine, th, true);
1814}
1815
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001816int machine__process_fork_event(struct machine *machine, union perf_event *event,
1817 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001818{
Jiri Olsad75e6092014-03-14 15:00:03 +01001819 struct thread *thread = machine__find_thread(machine,
1820 event->fork.pid,
1821 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001822 struct thread *parent = machine__findnew_thread(machine,
1823 event->fork.ppid,
1824 event->fork.ptid);
David Miller4f8f3822018-10-30 22:24:04 -07001825 bool do_maps_clone = true;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001826 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001827
Adrian Hunter5cb73342015-08-19 17:29:20 +03001828 if (dump_trace)
1829 perf_event__fprintf_task(event, stdout);
1830
1831 /*
1832 * There may be an existing thread that is not actually the parent,
1833 * either because we are processing events out of order, or because the
1834 * (fork) event that would have removed the thread was lost. Assume the
1835 * latter case and continue on as best we can.
1836 */
1837 if (parent->pid_ != (pid_t)event->fork.ppid) {
1838 dump_printf("removing erroneous parent thread %d/%d\n",
1839 parent->pid_, parent->tid);
1840 machine__remove_thread(machine, parent);
1841 thread__put(parent);
1842 parent = machine__findnew_thread(machine, event->fork.ppid,
1843 event->fork.ptid);
1844 }
1845
David Ahern236a3bb2013-08-14 08:49:27 -06001846 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001847 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001848 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001849 thread__put(thread);
1850 }
David Ahern236a3bb2013-08-14 08:49:27 -06001851
Adrian Hunter314add62013-08-27 11:23:03 +03001852 thread = machine__findnew_thread(machine, event->fork.pid,
1853 event->fork.tid);
David Miller4f8f3822018-10-30 22:24:04 -07001854 /*
1855 * When synthesizing FORK events, we are trying to create thread
1856 * objects for the already running tasks on the machine.
1857 *
1858 * Normally, for a kernel FORK event, we want to clone the parent's
1859 * maps because that is what the kernel just did.
1860 *
1861 * But when synthesizing, this should not be done. If we do, we end up
1862 * with overlapping maps as we process the sythesized MMAP2 events that
1863 * get delivered shortly thereafter.
1864 *
1865 * Use the FORK event misc flags in an internal way to signal this
1866 * situation, so we can elide the map clone when appropriate.
1867 */
1868 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1869 do_maps_clone = false;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001870
1871 if (thread == NULL || parent == NULL ||
David Miller4f8f3822018-10-30 22:24:04 -07001872 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001873 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001874 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001875 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001876 thread__put(thread);
1877 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001878
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001879 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001880}
1881
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001882int machine__process_exit_event(struct machine *machine, union perf_event *event,
1883 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001884{
Jiri Olsad75e6092014-03-14 15:00:03 +01001885 struct thread *thread = machine__find_thread(machine,
1886 event->fork.pid,
1887 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001888
1889 if (dump_trace)
1890 perf_event__fprintf_task(event, stdout);
1891
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001892 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001893 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001894 thread__put(thread);
1895 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001896
1897 return 0;
1898}
1899
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001900int machine__process_event(struct machine *machine, union perf_event *event,
1901 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001902{
1903 int ret;
1904
1905 switch (event->header.type) {
1906 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001907 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001908 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001909 ret = machine__process_mmap_event(machine, event, sample); break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301910 case PERF_RECORD_NAMESPACES:
1911 ret = machine__process_namespaces_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001912 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001913 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001914 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001915 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001916 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001917 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001918 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001919 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001920 case PERF_RECORD_AUX:
1921 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001922 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001923 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001924 case PERF_RECORD_LOST_SAMPLES:
1925 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001926 case PERF_RECORD_SWITCH:
1927 case PERF_RECORD_SWITCH_CPU_WIDE:
1928 ret = machine__process_switch_event(machine, event); break;
Song Liu9aa0bfa2019-01-17 08:15:17 -08001929 case PERF_RECORD_KSYMBOL:
1930 ret = machine__process_ksymbol(machine, event, sample); break;
Song Liu45178a92019-01-17 08:15:18 -08001931 case PERF_RECORD_BPF_EVENT:
Arnaldo Carvalho de Melo3f604b52019-08-26 19:28:13 -03001932 ret = machine__process_bpf(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001933 default:
1934 ret = -1;
1935 break;
1936 }
1937
1938 return ret;
1939}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001940
Greg Priceb21484f2012-12-06 21:48:05 -08001941static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001942{
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001943 if (!regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001944 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001945 return 0;
1946}
1947
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001948static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001949 struct addr_map_symbol *ams,
1950 u64 ip)
1951{
1952 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001953
1954 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001955 /*
1956 * We cannot use the header.misc hint to determine whether a
1957 * branch stack address is user, kernel, guest, hypervisor.
1958 * Branches may straddle the kernel/user/hypervisor boundaries.
1959 * Thus, we have to try consecutively until we find a match
1960 * or else, the symbol is unknown
1961 */
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03001962 thread__find_cpumode_addr_location(thread, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001963
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001964 ams->addr = ip;
1965 ams->al_addr = al.addr;
1966 ams->sym = al.sym;
1967 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001968 ams->phys_addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001969}
1970
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001971static void ip__resolve_data(struct thread *thread,
Kan Liang8780fb22017-08-29 13:11:09 -04001972 u8 m, struct addr_map_symbol *ams,
1973 u64 addr, u64 phys_addr)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001974{
1975 struct addr_location al;
1976
1977 memset(&al, 0, sizeof(al));
1978
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03001979 thread__find_symbol(thread, m, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001980
Stephane Eranian98a3b322013-01-24 16:10:35 +01001981 ams->addr = addr;
1982 ams->al_addr = al.addr;
1983 ams->sym = al.sym;
1984 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001985 ams->phys_addr = phys_addr;
Stephane Eranian98a3b322013-01-24 16:10:35 +01001986}
1987
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001988struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1989 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001990{
Jiri Olsa9f874982018-03-07 16:50:06 +01001991 struct mem_info *mi = mem_info__new();
Stephane Eranian98a3b322013-01-24 16:10:35 +01001992
1993 if (!mi)
1994 return NULL;
1995
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001996 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
Kan Liang8780fb22017-08-29 13:11:09 -04001997 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1998 sample->addr, sample->phys_addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001999 mi->data_src.val = sample->data_src;
2000
2001 return mi;
2002}
2003
Milian Wolff40a342c2017-10-09 22:32:56 +02002004static char *callchain_srcline(struct map *map, struct symbol *sym, u64 ip)
2005{
Milian Wolff21ac9d52017-10-19 13:38:34 +02002006 char *srcline = NULL;
Milian Wolff40a342c2017-10-09 22:32:56 +02002007
Milian Wolff21ac9d52017-10-19 13:38:34 +02002008 if (!map || callchain_param.key == CCKEY_FUNCTION)
2009 return srcline;
2010
2011 srcline = srcline__tree_find(&map->dso->srclines, ip);
2012 if (!srcline) {
2013 bool show_sym = false;
2014 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2015
2016 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
Jin Yao935f5a92017-12-30 00:26:52 +08002017 sym, show_sym, show_addr, ip);
Milian Wolff21ac9d52017-10-19 13:38:34 +02002018 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2019 }
2020
2021 return srcline;
Milian Wolff40a342c2017-10-09 22:32:56 +02002022}
2023
Jin Yaoc4ee0622017-08-07 21:05:15 +08002024struct iterations {
2025 int nr_loop_iter;
2026 u64 cycles;
2027};
2028
Andi Kleen37592b82014-11-12 18:05:19 -08002029static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002030 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08002031 struct symbol **parent,
2032 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002033 u8 *cpumode,
Jin Yao410024d2016-10-31 09:19:49 +08002034 u64 ip,
2035 bool branch,
2036 struct branch_flags *flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002037 struct iterations *iter,
Jin Yaob851dd42017-07-18 20:13:15 +08002038 u64 branch_from)
Andi Kleen37592b82014-11-12 18:05:19 -08002039{
2040 struct addr_location al;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002041 int nr_loop_iter = 0;
2042 u64 iter_cycles = 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002043 const char *srcline = NULL;
Andi Kleen37592b82014-11-12 18:05:19 -08002044
2045 al.filtered = 0;
2046 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002047 if (!cpumode) {
Arnaldo Carvalho de Melo26bd9332018-04-25 17:58:03 -03002048 thread__find_cpumode_addr_location(thread, ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002049 } else {
Kan Liang2e777842014-12-02 10:06:53 -05002050 if (ip >= PERF_CONTEXT_MAX) {
2051 switch (ip) {
2052 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002053 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05002054 break;
2055 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002056 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05002057 break;
2058 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002059 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05002060 break;
2061 default:
2062 pr_debug("invalid callchain context: "
2063 "%"PRId64"\n", (s64) ip);
2064 /*
2065 * It seems the callchain is corrupted.
2066 * Discard all.
2067 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002068 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05002069 return 1;
2070 }
2071 return 0;
2072 }
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002073 thread__find_symbol(thread, *cpumode, ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05002074 }
2075
Andi Kleen37592b82014-11-12 18:05:19 -08002076 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02002077 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08002078 symbol__match_regex(al.sym, &parent_regex))
2079 *parent = al.sym;
2080 else if (have_ignore_callees && root_al &&
2081 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2082 /* Treat this symbol as the root,
2083 forgetting its callees. */
2084 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002085 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08002086 }
2087 }
2088
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002089 if (symbol_conf.hide_unresolved && al.sym == NULL)
2090 return 0;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002091
2092 if (iter) {
2093 nr_loop_iter = iter->nr_loop_iter;
2094 iter_cycles = iter->cycles;
2095 }
2096
Milian Wolff40a342c2017-10-09 22:32:56 +02002097 srcline = callchain_srcline(al.map, al.sym, al.addr);
Sandipan Das19610182018-05-17 12:03:25 +05302098 return callchain_cursor_append(cursor, ip, al.map, al.sym,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002099 branch, flags, nr_loop_iter,
Milian Wolff40a342c2017-10-09 22:32:56 +02002100 iter_cycles, branch_from, srcline);
Andi Kleen37592b82014-11-12 18:05:19 -08002101}
2102
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002103struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2104 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002105{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002106 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03002107 const struct branch_stack *bs = sample->branch_stack;
2108 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002109
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002110 if (!bi)
2111 return NULL;
2112
2113 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002114 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
2115 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002116 bi[i].flags = bs->entries[i].flags;
2117 }
2118 return bi;
2119}
2120
Jin Yaoc4ee0622017-08-07 21:05:15 +08002121static void save_iterations(struct iterations *iter,
2122 struct branch_entry *be, int nr)
2123{
2124 int i;
2125
Jin Yaoa3366db02019-01-04 14:10:30 +08002126 iter->nr_loop_iter++;
Jin Yaoc4ee0622017-08-07 21:05:15 +08002127 iter->cycles = 0;
2128
2129 for (i = 0; i < nr; i++)
2130 iter->cycles += be[i].flags.cycles;
2131}
2132
Andi Kleen8b7bad52014-11-12 18:05:20 -08002133#define CHASHSZ 127
2134#define CHASHBITS 7
2135#define NO_ENTRY 0xff
2136
2137#define PERF_MAX_BRANCH_DEPTH 127
2138
2139/* Remove loops. */
Jin Yaoc4ee0622017-08-07 21:05:15 +08002140static int remove_loops(struct branch_entry *l, int nr,
2141 struct iterations *iter)
Andi Kleen8b7bad52014-11-12 18:05:20 -08002142{
2143 int i, j, off;
2144 unsigned char chash[CHASHSZ];
2145
2146 memset(chash, NO_ENTRY, sizeof(chash));
2147
2148 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2149
2150 for (i = 0; i < nr; i++) {
2151 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2152
2153 /* no collision handling for now */
2154 if (chash[h] == NO_ENTRY) {
2155 chash[h] = i;
2156 } else if (l[chash[h]].from == l[i].from) {
2157 bool is_loop = true;
2158 /* check if it is a real loop */
2159 off = 0;
2160 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2161 if (l[j].from != l[i + off].from) {
2162 is_loop = false;
2163 break;
2164 }
2165 if (is_loop) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002166 j = nr - (i + off);
2167 if (j > 0) {
2168 save_iterations(iter + i + off,
2169 l + i, off);
2170
2171 memmove(iter + i, iter + i + off,
2172 j * sizeof(*iter));
2173
2174 memmove(l + i, l + i + off,
2175 j * sizeof(*l));
2176 }
2177
Andi Kleen8b7bad52014-11-12 18:05:20 -08002178 nr -= off;
2179 }
2180 }
2181 }
2182 return nr;
2183}
2184
Kan Liang384b6052015-01-05 13:23:05 -05002185/*
2186 * Recolve LBR callstack chain sample
2187 * Return:
2188 * 1 on success get LBR callchain information
2189 * 0 no available LBR callchain information, should try fp
2190 * negative error code on other errors.
2191 */
2192static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002193 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05002194 struct perf_sample *sample,
2195 struct symbol **parent,
2196 struct addr_location *root_al,
2197 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002198{
Kan Liang384b6052015-01-05 13:23:05 -05002199 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002200 int chain_nr = min(max_stack, (int)chain->nr), i;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002201 u8 cpumode = PERF_RECORD_MISC_USER;
Jin Yaob851dd42017-07-18 20:13:15 +08002202 u64 ip, branch_from = 0;
Kan Liang384b6052015-01-05 13:23:05 -05002203
2204 for (i = 0; i < chain_nr; i++) {
2205 if (chain->ips[i] == PERF_CONTEXT_USER)
2206 break;
2207 }
2208
2209 /* LBR only affects the user callchain */
2210 if (i != chain_nr) {
2211 struct branch_stack *lbr_stack = sample->branch_stack;
Jin Yao410024d2016-10-31 09:19:49 +08002212 int lbr_nr = lbr_stack->nr, j, k;
2213 bool branch;
2214 struct branch_flags *flags;
Kan Liang384b6052015-01-05 13:23:05 -05002215 /*
2216 * LBR callstack can only get user call chain.
2217 * The mix_chain_nr is kernel call chain
2218 * number plus LBR user call chain number.
2219 * i is kernel call chain number,
2220 * 1 is PERF_CONTEXT_USER,
2221 * lbr_nr + 1 is the user call chain number.
2222 * For details, please refer to the comments
2223 * in callchain__printf
2224 */
2225 int mix_chain_nr = i + 1 + lbr_nr + 1;
2226
Kan Liang384b6052015-01-05 13:23:05 -05002227 for (j = 0; j < mix_chain_nr; j++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03002228 int err;
Jin Yao410024d2016-10-31 09:19:49 +08002229 branch = false;
2230 flags = NULL;
2231
Kan Liang384b6052015-01-05 13:23:05 -05002232 if (callchain_param.order == ORDER_CALLEE) {
2233 if (j < i + 1)
2234 ip = chain->ips[j];
Jin Yao410024d2016-10-31 09:19:49 +08002235 else if (j > i + 1) {
2236 k = j - i - 2;
2237 ip = lbr_stack->entries[k].from;
2238 branch = true;
2239 flags = &lbr_stack->entries[k].flags;
2240 } else {
Kan Liang384b6052015-01-05 13:23:05 -05002241 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08002242 branch = true;
2243 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08002244 branch_from =
2245 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08002246 }
Kan Liang384b6052015-01-05 13:23:05 -05002247 } else {
Jin Yao410024d2016-10-31 09:19:49 +08002248 if (j < lbr_nr) {
2249 k = lbr_nr - j - 1;
2250 ip = lbr_stack->entries[k].from;
2251 branch = true;
2252 flags = &lbr_stack->entries[k].flags;
2253 }
Kan Liang384b6052015-01-05 13:23:05 -05002254 else if (j > lbr_nr)
2255 ip = chain->ips[i + 1 - (j - lbr_nr)];
Jin Yao410024d2016-10-31 09:19:49 +08002256 else {
Kan Liang384b6052015-01-05 13:23:05 -05002257 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08002258 branch = true;
2259 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08002260 branch_from =
2261 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08002262 }
Kan Liang384b6052015-01-05 13:23:05 -05002263 }
2264
Jin Yao410024d2016-10-31 09:19:49 +08002265 err = add_callchain_ip(thread, cursor, parent,
2266 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002267 branch, flags, NULL,
Jin Yaob851dd42017-07-18 20:13:15 +08002268 branch_from);
Kan Liang384b6052015-01-05 13:23:05 -05002269 if (err)
2270 return (err < 0) ? err : 0;
2271 }
2272 return 1;
2273 }
2274
2275 return 0;
2276}
2277
David S. Millere9024d52018-10-30 12:12:26 -03002278static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2279 struct callchain_cursor *cursor,
2280 struct symbol **parent,
2281 struct addr_location *root_al,
2282 u8 *cpumode, int ent)
2283{
2284 int err = 0;
2285
2286 while (--ent >= 0) {
2287 u64 ip = chain->ips[ent];
2288
2289 if (ip >= PERF_CONTEXT_MAX) {
2290 err = add_callchain_ip(thread, cursor, parent,
2291 root_al, cpumode, ip,
2292 false, NULL, NULL, 0);
2293 break;
2294 }
2295 }
2296 return err;
2297}
2298
Kan Liang384b6052015-01-05 13:23:05 -05002299static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002300 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002301 struct evsel *evsel,
Kan Liang384b6052015-01-05 13:23:05 -05002302 struct perf_sample *sample,
2303 struct symbol **parent,
2304 struct addr_location *root_al,
2305 int max_stack)
2306{
2307 struct branch_stack *branch = sample->branch_stack;
2308 struct ip_callchain *chain = sample->callchain;
Jin Yaob49a8212017-05-08 18:43:02 +08002309 int chain_nr = 0;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02002310 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002311 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08002312 int skip_idx = -1;
2313 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002314
Jin Yaob49a8212017-05-08 18:43:02 +08002315 if (chain)
2316 chain_nr = chain->nr;
2317
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03002318 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002319 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05002320 root_al, max_stack);
2321 if (err)
2322 return (err < 0) ? err : 0;
2323 }
2324
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002325 /*
2326 * Based on DWARF debug information, some architectures skip
2327 * a callchain entry saved by the kernel.
2328 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002329 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002330
Andi Kleen8b7bad52014-11-12 18:05:20 -08002331 /*
2332 * Add branches to call stack for easier browsing. This gives
2333 * more context for a sample than just the callers.
2334 *
2335 * This uses individual histograms of paths compared to the
2336 * aggregated histograms the normal LBR mode uses.
2337 *
2338 * Limitations for now:
2339 * - No extra filters
2340 * - No annotations (should annotate somehow)
2341 */
2342
2343 if (branch && callchain_param.branch_callstack) {
2344 int nr = min(max_stack, (int)branch->nr);
2345 struct branch_entry be[nr];
Jin Yaoc4ee0622017-08-07 21:05:15 +08002346 struct iterations iter[nr];
Andi Kleen8b7bad52014-11-12 18:05:20 -08002347
2348 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2349 pr_warning("corrupted branch chain. skipping...\n");
2350 goto check_calls;
2351 }
2352
2353 for (i = 0; i < nr; i++) {
2354 if (callchain_param.order == ORDER_CALLEE) {
2355 be[i] = branch->entries[i];
Jin Yaob49a8212017-05-08 18:43:02 +08002356
2357 if (chain == NULL)
2358 continue;
2359
Andi Kleen8b7bad52014-11-12 18:05:20 -08002360 /*
2361 * Check for overlap into the callchain.
2362 * The return address is one off compared to
2363 * the branch entry. To adjust for this
2364 * assume the calling instruction is not longer
2365 * than 8 bytes.
2366 */
2367 if (i == skip_idx ||
2368 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2369 first_call++;
2370 else if (be[i].from < chain->ips[first_call] &&
2371 be[i].from >= chain->ips[first_call] - 8)
2372 first_call++;
2373 } else
2374 be[i] = branch->entries[branch->nr - i - 1];
2375 }
2376
Jin Yaoc4ee0622017-08-07 21:05:15 +08002377 memset(iter, 0, sizeof(struct iterations) * nr);
2378 nr = remove_loops(be, nr, iter);
Jin Yao410024d2016-10-31 09:19:49 +08002379
Andi Kleen8b7bad52014-11-12 18:05:20 -08002380 for (i = 0; i < nr; i++) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002381 err = add_callchain_ip(thread, cursor, parent,
2382 root_al,
2383 NULL, be[i].to,
2384 true, &be[i].flags,
2385 NULL, be[i].from);
Jin Yao410024d2016-10-31 09:19:49 +08002386
Andi Kleen8b7bad52014-11-12 18:05:20 -08002387 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002388 err = add_callchain_ip(thread, cursor, parent, root_al,
Jin Yao410024d2016-10-31 09:19:49 +08002389 NULL, be[i].from,
2390 true, &be[i].flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002391 &iter[i], 0);
Andi Kleen8b7bad52014-11-12 18:05:20 -08002392 if (err == -EINVAL)
2393 break;
2394 if (err)
2395 return err;
2396 }
Jin Yaob49a8212017-05-08 18:43:02 +08002397
2398 if (chain_nr == 0)
2399 return 0;
2400
Andi Kleen8b7bad52014-11-12 18:05:20 -08002401 chain_nr -= nr;
2402 }
2403
2404check_calls:
David S. Millere9024d52018-10-30 12:12:26 -03002405 if (callchain_param.order != ORDER_CALLEE) {
2406 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2407 &cpumode, chain->nr - first_call);
2408 if (err)
2409 return (err < 0) ? err : 0;
2410 }
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002411 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002412 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002413 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002414
2415 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002416 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002417 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002418 j = chain->nr - i - 1;
2419
2420#ifdef HAVE_SKIP_CALLCHAIN_IDX
2421 if (j == skip_idx)
2422 continue;
2423#endif
2424 ip = chain->ips[j];
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002425 if (ip < PERF_CONTEXT_MAX)
2426 ++nr_entries;
David S. Millere9024d52018-10-30 12:12:26 -03002427 else if (callchain_param.order != ORDER_CALLEE) {
2428 err = find_prev_cpumode(chain, thread, cursor, parent,
2429 root_al, &cpumode, j);
2430 if (err)
2431 return (err < 0) ? err : 0;
2432 continue;
2433 }
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002434
Jin Yao410024d2016-10-31 09:19:49 +08002435 err = add_callchain_ip(thread, cursor, parent,
2436 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002437 false, NULL, NULL, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002438
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002439 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05002440 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002441 }
2442
2443 return 0;
2444}
2445
Milian Wolff11ea2512017-10-09 22:32:59 +02002446static int append_inlines(struct callchain_cursor *cursor,
2447 struct map *map, struct symbol *sym, u64 ip)
2448{
2449 struct inline_node *inline_node;
2450 struct inline_list *ilist;
2451 u64 addr;
Milian Wolffb38775c2017-10-19 13:38:33 +02002452 int ret = 1;
Milian Wolff11ea2512017-10-09 22:32:59 +02002453
2454 if (!symbol_conf.inline_name || !map || !sym)
Milian Wolffb38775c2017-10-19 13:38:33 +02002455 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002456
Milian Wolff7a8a8fc2018-09-26 15:52:06 +02002457 addr = map__map_ip(map, ip);
2458 addr = map__rip_2objdump(map, addr);
Milian Wolff11ea2512017-10-09 22:32:59 +02002459
2460 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2461 if (!inline_node) {
2462 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2463 if (!inline_node)
Milian Wolffb38775c2017-10-19 13:38:33 +02002464 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002465 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2466 }
2467
2468 list_for_each_entry(ilist, &inline_node->val, list) {
Milian Wolffb38775c2017-10-19 13:38:33 +02002469 ret = callchain_cursor_append(cursor, ip, map,
2470 ilist->symbol, false,
2471 NULL, 0, 0, 0, ilist->srcline);
Milian Wolff11ea2512017-10-09 22:32:59 +02002472
2473 if (ret != 0)
2474 return ret;
2475 }
2476
Milian Wolffb38775c2017-10-19 13:38:33 +02002477 return ret;
Milian Wolff11ea2512017-10-09 22:32:59 +02002478}
2479
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002480static int unwind_entry(struct unwind_entry *entry, void *arg)
2481{
2482 struct callchain_cursor *cursor = arg;
Milian Wolff40a342c2017-10-09 22:32:56 +02002483 const char *srcline = NULL;
Milian Wolffff4ce282018-09-26 15:52:05 +02002484 u64 addr = entry->ip;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002485
2486 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2487 return 0;
Milian Wolff40a342c2017-10-09 22:32:56 +02002488
Milian Wolff11ea2512017-10-09 22:32:59 +02002489 if (append_inlines(cursor, entry->map, entry->sym, entry->ip) == 0)
2490 return 0;
2491
Sandipan Das2a9d5052018-07-03 17:35:55 +05302492 /*
2493 * Convert entry->ip from a virtual address to an offset in
2494 * its corresponding binary.
2495 */
Milian Wolffff4ce282018-09-26 15:52:05 +02002496 if (entry->map)
2497 addr = map__map_ip(entry->map, entry->ip);
Sandipan Das2a9d5052018-07-03 17:35:55 +05302498
2499 srcline = callchain_srcline(entry->map, entry->sym, addr);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002500 return callchain_cursor_append(cursor, entry->ip,
Jin Yao410024d2016-10-31 09:19:49 +08002501 entry->map, entry->sym,
Milian Wolff40a342c2017-10-09 22:32:56 +02002502 false, NULL, 0, 0, 0, srcline);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002503}
2504
Chris Phlipot9919a652016-04-28 01:19:06 -07002505static int thread__resolve_callchain_unwind(struct thread *thread,
2506 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002507 struct evsel *evsel,
Chris Phlipot9919a652016-04-28 01:19:06 -07002508 struct perf_sample *sample,
2509 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002510{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002511 /* Can we do dwarf post unwind? */
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002512 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2513 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002514 return 0;
2515
2516 /* Bail out if nothing was captured. */
2517 if ((!sample->user_regs.regs) ||
2518 (!sample->user_stack.size))
2519 return 0;
2520
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002521 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01002522 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07002523}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002524
Chris Phlipot9919a652016-04-28 01:19:06 -07002525int thread__resolve_callchain(struct thread *thread,
2526 struct callchain_cursor *cursor,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002527 struct evsel *evsel,
Chris Phlipot9919a652016-04-28 01:19:06 -07002528 struct perf_sample *sample,
2529 struct symbol **parent,
2530 struct addr_location *root_al,
2531 int max_stack)
2532{
2533 int ret = 0;
2534
Jiri Olsa914eb9c2017-08-06 16:39:39 +02002535 callchain_cursor_reset(cursor);
Chris Phlipot9919a652016-04-28 01:19:06 -07002536
2537 if (callchain_param.order == ORDER_CALLEE) {
2538 ret = thread__resolve_callchain_sample(thread, cursor,
2539 evsel, sample,
2540 parent, root_al,
2541 max_stack);
2542 if (ret)
2543 return ret;
2544 ret = thread__resolve_callchain_unwind(thread, cursor,
2545 evsel, sample,
2546 max_stack);
2547 } else {
2548 ret = thread__resolve_callchain_unwind(thread, cursor,
2549 evsel, sample,
2550 max_stack);
2551 if (ret)
2552 return ret;
2553 ret = thread__resolve_callchain_sample(thread, cursor,
2554 evsel, sample,
2555 parent, root_al,
2556 max_stack);
2557 }
2558
2559 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002560}
David Ahern35feee12013-09-28 13:12:58 -06002561
2562int machine__for_each_thread(struct machine *machine,
2563 int (*fn)(struct thread *thread, void *p),
2564 void *priv)
2565{
Kan Liang91e467b2017-09-10 19:23:14 -07002566 struct threads *threads;
David Ahern35feee12013-09-28 13:12:58 -06002567 struct rb_node *nd;
2568 struct thread *thread;
2569 int rc = 0;
Kan Liang91e467b2017-09-10 19:23:14 -07002570 int i;
David Ahern35feee12013-09-28 13:12:58 -06002571
Kan Liang91e467b2017-09-10 19:23:14 -07002572 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
2573 threads = &machine->threads[i];
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08002574 for (nd = rb_first_cached(&threads->entries); nd;
2575 nd = rb_next(nd)) {
Kan Liang91e467b2017-09-10 19:23:14 -07002576 thread = rb_entry(nd, struct thread, rb_node);
2577 rc = fn(thread, priv);
2578 if (rc != 0)
2579 return rc;
2580 }
David Ahern35feee12013-09-28 13:12:58 -06002581
Kan Liang91e467b2017-09-10 19:23:14 -07002582 list_for_each_entry(thread, &threads->dead, node) {
2583 rc = fn(thread, priv);
2584 if (rc != 0)
2585 return rc;
2586 }
David Ahern35feee12013-09-28 13:12:58 -06002587 }
2588 return rc;
2589}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002590
Adrian Huntera5499b32015-05-29 16:33:30 +03002591int machines__for_each_thread(struct machines *machines,
2592 int (*fn)(struct thread *thread, void *p),
2593 void *priv)
2594{
2595 struct rb_node *nd;
2596 int rc = 0;
2597
2598 rc = machine__for_each_thread(&machines->host, fn, priv);
2599 if (rc != 0)
2600 return rc;
2601
Davidlohr Buesof3acb3a2018-12-06 11:18:14 -08002602 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
Adrian Huntera5499b32015-05-29 16:33:30 +03002603 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2604
2605 rc = machine__for_each_thread(machine, fn, priv);
2606 if (rc != 0)
2607 return rc;
2608 }
2609 return rc;
2610}
2611
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03002612int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Jiri Olsa9749b902019-07-21 13:23:50 +02002613 struct target *target, struct perf_thread_map *threads,
Kan Liang9d9cad72015-06-17 09:51:11 -04002614 perf_event__handler_t process, bool data_mmap,
Kan Liang340b47f2017-09-29 07:47:54 -07002615 unsigned int nr_threads_synthesize)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002616{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002617 if (target__has_task(target))
Mark Drayton3fcb10e2018-12-04 12:34:20 -08002618 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002619 else if (target__has_cpu(target))
Kan Liang340b47f2017-09-29 07:47:54 -07002620 return perf_event__synthesize_threads(tool, process,
2621 machine, data_mmap,
Kan Liang340b47f2017-09-29 07:47:54 -07002622 nr_threads_synthesize);
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002623 /* command specified */
2624 return 0;
2625}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002626
2627pid_t machine__get_current_tid(struct machine *machine, int cpu)
2628{
Kyle Meyer8c727462019-08-27 16:43:50 -05002629 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
2630
2631 if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002632 return -1;
2633
2634 return machine->current_tid[cpu];
2635}
2636
2637int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2638 pid_t tid)
2639{
2640 struct thread *thread;
Kyle Meyer8c727462019-08-27 16:43:50 -05002641 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002642
2643 if (cpu < 0)
2644 return -EINVAL;
2645
2646 if (!machine->current_tid) {
2647 int i;
2648
Kyle Meyer8c727462019-08-27 16:43:50 -05002649 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002650 if (!machine->current_tid)
2651 return -ENOMEM;
Kyle Meyer8c727462019-08-27 16:43:50 -05002652 for (i = 0; i < nr_cpus; i++)
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002653 machine->current_tid[i] = -1;
2654 }
2655
Kyle Meyer8c727462019-08-27 16:43:50 -05002656 if (cpu >= nr_cpus) {
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002657 pr_err("Requested CPU %d too large. ", cpu);
2658 pr_err("Consider raising MAX_NR_CPUS\n");
2659 return -EINVAL;
2660 }
2661
2662 machine->current_tid[cpu] = tid;
2663
2664 thread = machine__findnew_thread(machine, pid, tid);
2665 if (!thread)
2666 return -ENOMEM;
2667
2668 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002669 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002670
2671 return 0;
2672}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002673
Adrian Hunterdbbd34a2018-05-17 12:21:53 +03002674/*
2675 * Compares the raw arch string. N.B. see instead perf_env__arch() if a
2676 * normalized arch is needed.
2677 */
2678bool machine__is(struct machine *machine, const char *arch)
2679{
2680 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
2681}
2682
Adrian Hunter9cecca32018-05-22 13:54:32 +03002683int machine__nr_cpus_avail(struct machine *machine)
2684{
2685 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
2686}
2687
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002688int machine__get_kernel_start(struct machine *machine)
2689{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002690 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002691 int err = 0;
2692
2693 /*
2694 * The only addresses above 2^63 are kernel addresses of a 64-bit
2695 * kernel. Note that addresses are unsigned so that on a 32-bit system
2696 * all addresses including kernel addresses are less than 2^32. In
2697 * that case (32-bit system), if the kernel mapping is unknown, all
2698 * addresses will be assumed to be in user space - see
2699 * machine__kernel_ip().
2700 */
2701 machine->kernel_start = 1ULL << 63;
2702 if (map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002703 err = map__load(map);
Adrian Hunter19422a92018-05-17 12:21:54 +03002704 /*
2705 * On x86_64, PTI entry trampolines are less than the
2706 * start of kernel text, but still above 2^63. So leave
2707 * kernel_start = 1ULL << 63 for x86_64.
2708 */
2709 if (!err && !machine__is(machine, "x86_64"))
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002710 machine->kernel_start = map->start;
2711 }
2712 return err;
2713}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002714
Adrian Hunter8e80ad92018-11-06 23:07:10 +02002715u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
2716{
2717 u8 addr_cpumode = cpumode;
2718 bool kernel_ip;
2719
2720 if (!machine->single_address_space)
2721 goto out;
2722
2723 kernel_ip = machine__kernel_ip(machine, addr);
2724 switch (cpumode) {
2725 case PERF_RECORD_MISC_KERNEL:
2726 case PERF_RECORD_MISC_USER:
2727 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
2728 PERF_RECORD_MISC_USER;
2729 break;
2730 case PERF_RECORD_MISC_GUEST_KERNEL:
2731 case PERF_RECORD_MISC_GUEST_USER:
2732 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
2733 PERF_RECORD_MISC_GUEST_USER;
2734 break;
2735 default:
2736 break;
2737 }
2738out:
2739 return addr_cpumode;
2740}
2741
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002742struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2743{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03002744 return dsos__findnew(&machine->dsos, filename);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002745}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002746
2747char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2748{
2749 struct machine *machine = vmachine;
2750 struct map *map;
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03002751 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002752
2753 if (sym == NULL)
2754 return NULL;
2755
2756 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2757 *addrp = map->unmap_ip(map, sym->start);
2758 return sym->name;
2759}