blob: 585b4a3d64a446421a6e3900cc715d8c08560368 [file] [log] [blame]
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -03001#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Arnaldo Carvalho de Melo1eae20c2017-04-18 12:33:30 -03004#include <regex.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03005#include "callchain.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03006#include "debug.h"
7#include "event.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03008#include "evsel.h"
9#include "hist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030010#include "machine.h"
11#include "map.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030012#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030013#include "strlist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030014#include "thread.h"
Adrian Hunterd027b6402014-07-23 14:23:00 +030015#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030016#include <stdbool.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030017#include <sys/types.h>
18#include <sys/stat.h>
19#include <unistd.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030020#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080021#include "linux/hash.h"
Hari Bathinif3b36142017-03-08 02:11:43 +053022#include "asm/bug.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030023
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030024#include "sane_ctype.h"
25#include <symbol/kallsyms.h>
26
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030027static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
28
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030029static void dsos__init(struct dsos *dsos)
30{
31 INIT_LIST_HEAD(&dsos->head);
32 dsos->root = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030033 init_rwsem(&dsos->lock);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030034}
35
Kan Liang91e467b2017-09-10 19:23:14 -070036static void machine__threads_init(struct machine *machine)
37{
38 int i;
39
40 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
41 struct threads *threads = &machine->threads[i];
42 threads->entries = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030043 init_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -070044 threads->nr = 0;
45 INIT_LIST_HEAD(&threads->dead);
46 threads->last_match = NULL;
47 }
48}
49
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030050int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
51{
Wang Nan93b0ba32015-12-08 02:25:44 +000052 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030053 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030054 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030055 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030056
Kan Liang91e467b2017-09-10 19:23:14 -070057 machine__threads_init(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030058
Adrian Hunterd027b6402014-07-23 14:23:00 +030059 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030060 machine->env = NULL;
Adrian Hunterd027b6402014-07-23 14:23:00 +030061
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030062 machine->pid = pid;
63
Jiri Olsa14bd6d22014-01-07 13:47:19 +010064 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -030065 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +030066 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030067 machine->kernel_start = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030068
Masami Hiramatsucc1121a2015-12-09 11:11:33 +090069 memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps));
70
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030071 machine->root_dir = strdup(root_dir);
72 if (machine->root_dir == NULL)
73 return -ENOMEM;
74
75 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030076 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030077 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030078 char comm[64];
79
80 if (thread == NULL)
81 return -ENOMEM;
82
83 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020084 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030085 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030086 }
87
Adrian Hunterb9d266b2014-07-22 16:17:25 +030088 machine->current_tid = NULL;
89
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030090 return 0;
91}
92
David Ahern8fb598e2013-09-28 13:13:00 -060093struct machine *machine__new_host(void)
94{
95 struct machine *machine = malloc(sizeof(*machine));
96
97 if (machine != NULL) {
98 machine__init(machine, "", HOST_KERNEL_ID);
99
100 if (machine__create_kernel_maps(machine) < 0)
101 goto out_delete;
102 }
103
104 return machine;
105out_delete:
106 free(machine);
107 return NULL;
108}
109
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -0300110struct machine *machine__new_kallsyms(void)
111{
112 struct machine *machine = machine__new_host();
113 /*
114 * FIXME:
115 * 1) MAP__FUNCTION will go away when we stop loading separate maps for
116 * functions and data objects.
117 * 2) We should switch to machine__load_kallsyms(), i.e. not explicitely
118 * ask for not using the kcore parsing code, once this one is fixed
119 * to create a map per module.
120 */
121 if (machine && __machine__load_kallsyms(machine, "/proc/kallsyms", MAP__FUNCTION, true) <= 0) {
122 machine__delete(machine);
123 machine = NULL;
124 }
125
126 return machine;
127}
128
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300129static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300130{
131 struct dso *pos, *n;
132
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300133 down_write(&dsos->lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300134
Waiman Long8fa7d872014-09-29 16:07:28 -0400135 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -0400136 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +0200137 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300138 list_del_init(&pos->node);
139 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300140 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300141
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300142 up_write(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300143}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300144
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300145static void dsos__exit(struct dsos *dsos)
146{
147 dsos__purge(dsos);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300148 exit_rwsem(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300149}
150
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300151void machine__delete_threads(struct machine *machine)
152{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300153 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700154 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300155
Kan Liang91e467b2017-09-10 19:23:14 -0700156 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
157 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300158 down_write(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -0700159 nd = rb_first(&threads->entries);
160 while (nd) {
161 struct thread *t = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300162
Kan Liang91e467b2017-09-10 19:23:14 -0700163 nd = rb_next(nd);
164 __machine__remove_thread(machine, t, false);
165 }
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300166 up_write(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300167 }
168}
169
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300170void machine__exit(struct machine *machine)
171{
Kan Liang91e467b2017-09-10 19:23:14 -0700172 int i;
173
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900174 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300175 map_groups__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300176 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300177 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300178 zfree(&machine->root_dir);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300179 zfree(&machine->current_tid);
Kan Liang91e467b2017-09-10 19:23:14 -0700180
181 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
182 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300183 exit_rwsem(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -0700184 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300185}
186
187void machine__delete(struct machine *machine)
188{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300189 if (machine) {
190 machine__exit(machine);
191 free(machine);
192 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300193}
194
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300195void machines__init(struct machines *machines)
196{
197 machine__init(&machines->host, "", HOST_KERNEL_ID);
198 machines->guests = RB_ROOT;
199}
200
201void machines__exit(struct machines *machines)
202{
203 machine__exit(&machines->host);
204 /* XXX exit guest */
205}
206
207struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300208 const char *root_dir)
209{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300210 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300211 struct rb_node *parent = NULL;
212 struct machine *pos, *machine = malloc(sizeof(*machine));
213
214 if (machine == NULL)
215 return NULL;
216
217 if (machine__init(machine, root_dir, pid) != 0) {
218 free(machine);
219 return NULL;
220 }
221
222 while (*p != NULL) {
223 parent = *p;
224 pos = rb_entry(parent, struct machine, rb_node);
225 if (pid < pos->pid)
226 p = &(*p)->rb_left;
227 else
228 p = &(*p)->rb_right;
229 }
230
231 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300232 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300233
234 return machine;
235}
236
Adrian Huntercfe1c412014-07-31 09:00:45 +0300237void machines__set_comm_exec(struct machines *machines, bool comm_exec)
238{
239 struct rb_node *nd;
240
241 machines->host.comm_exec = comm_exec;
242
243 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
244 struct machine *machine = rb_entry(nd, struct machine, rb_node);
245
246 machine->comm_exec = comm_exec;
247 }
248}
249
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300250struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300251{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300252 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300253 struct rb_node *parent = NULL;
254 struct machine *machine;
255 struct machine *default_machine = NULL;
256
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300257 if (pid == HOST_KERNEL_ID)
258 return &machines->host;
259
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300260 while (*p != NULL) {
261 parent = *p;
262 machine = rb_entry(parent, struct machine, rb_node);
263 if (pid < machine->pid)
264 p = &(*p)->rb_left;
265 else if (pid > machine->pid)
266 p = &(*p)->rb_right;
267 else
268 return machine;
269 if (!machine->pid)
270 default_machine = machine;
271 }
272
273 return default_machine;
274}
275
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300276struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300277{
278 char path[PATH_MAX];
279 const char *root_dir = "";
280 struct machine *machine = machines__find(machines, pid);
281
282 if (machine && (machine->pid == pid))
283 goto out;
284
285 if ((pid != HOST_KERNEL_ID) &&
286 (pid != DEFAULT_GUEST_KERNEL_ID) &&
287 (symbol_conf.guestmount)) {
288 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
289 if (access(path, R_OK)) {
290 static struct strlist *seen;
291
292 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300293 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300294
295 if (!strlist__has_entry(seen, path)) {
296 pr_err("Can't access file %s\n", path);
297 strlist__add(seen, path);
298 }
299 machine = NULL;
300 goto out;
301 }
302 root_dir = path;
303 }
304
305 machine = machines__add(machines, pid, root_dir);
306out:
307 return machine;
308}
309
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300310void machines__process_guests(struct machines *machines,
311 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300312{
313 struct rb_node *nd;
314
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300315 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300316 struct machine *pos = rb_entry(nd, struct machine, rb_node);
317 process(pos, data);
318 }
319}
320
321char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
322{
323 if (machine__is_host(machine))
324 snprintf(bf, size, "[%s]", "kernel.kallsyms");
325 else if (machine__is_default_guest(machine))
326 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
327 else {
328 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
329 machine->pid);
330 }
331
332 return bf;
333}
334
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300335void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300336{
337 struct rb_node *node;
338 struct machine *machine;
339
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300340 machines->host.id_hdr_size = id_hdr_size;
341
342 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300343 machine = rb_entry(node, struct machine, rb_node);
344 machine->id_hdr_size = id_hdr_size;
345 }
346
347 return;
348}
349
Adrian Hunter29ce3612014-07-16 11:07:13 +0300350static void machine__update_thread_pid(struct machine *machine,
351 struct thread *th, pid_t pid)
352{
353 struct thread *leader;
354
355 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
356 return;
357
358 th->pid_ = pid;
359
360 if (th->pid_ == th->tid)
361 return;
362
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300363 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300364 if (!leader)
365 goto out_err;
366
367 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300368 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300369
370 if (!leader->mg)
371 goto out_err;
372
373 if (th->mg == leader->mg)
374 return;
375
376 if (th->mg) {
377 /*
378 * Maps are created from MMAP events which provide the pid and
379 * tid. Consequently there never should be any maps on a thread
380 * with an unknown pid. Just print an error if there are.
381 */
382 if (!map_groups__empty(th->mg))
383 pr_err("Discarding thread maps for %d:%d\n",
384 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300385 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300386 }
387
388 th->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300389out_put:
390 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300391 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300392out_err:
393 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300394 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300395}
396
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300397/*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800398 * Caller must eventually drop thread->refcnt returned with a successful
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300399 * lookup/new thread inserted.
400 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300401static struct thread *____machine__findnew_thread(struct machine *machine,
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300402 struct threads *threads,
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300403 pid_t pid, pid_t tid,
404 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300405{
Kan Liang91e467b2017-09-10 19:23:14 -0700406 struct rb_node **p = &threads->entries.rb_node;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300407 struct rb_node *parent = NULL;
408 struct thread *th;
409
410 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300411 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300412 * so most of the time we dont have to look up
413 * the full rbtree:
414 */
Kan Liang91e467b2017-09-10 19:23:14 -0700415 th = threads->last_match;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300416 if (th != NULL) {
417 if (th->tid == tid) {
418 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300419 return thread__get(th);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300420 }
421
Kan Liang91e467b2017-09-10 19:23:14 -0700422 threads->last_match = NULL;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300423 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300424
425 while (*p != NULL) {
426 parent = *p;
427 th = rb_entry(parent, struct thread, rb_node);
428
Adrian Hunter38051232013-07-04 16:20:31 +0300429 if (th->tid == tid) {
Kan Liang91e467b2017-09-10 19:23:14 -0700430 threads->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300431 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300432 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300433 }
434
Adrian Hunter38051232013-07-04 16:20:31 +0300435 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300436 p = &(*p)->rb_left;
437 else
438 p = &(*p)->rb_right;
439 }
440
441 if (!create)
442 return NULL;
443
Adrian Hunter99d725f2013-08-26 16:00:19 +0300444 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300445 if (th != NULL) {
446 rb_link_node(&th->rb_node, parent, p);
Kan Liang91e467b2017-09-10 19:23:14 -0700447 rb_insert_color(&th->rb_node, &threads->entries);
Jiri Olsacddcef62014-04-09 20:54:29 +0200448
449 /*
450 * We have to initialize map_groups separately
451 * after rb tree is updated.
452 *
453 * The reason is that we call machine__findnew_thread
454 * within thread__init_map_groups to find the thread
455 * leader and that would screwed the rb tree.
456 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300457 if (thread__init_map_groups(th, machine)) {
Kan Liang91e467b2017-09-10 19:23:14 -0700458 rb_erase_init(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300459 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300460 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200461 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300462 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300463 /*
464 * It is now in the rbtree, get a ref
465 */
466 thread__get(th);
Kan Liang91e467b2017-09-10 19:23:14 -0700467 threads->last_match = th;
468 ++threads->nr;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300469 }
470
471 return th;
472}
473
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300474struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
475{
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300476 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300477}
478
Adrian Hunter314add62013-08-27 11:23:03 +0300479struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
480 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300481{
Kan Liang91e467b2017-09-10 19:23:14 -0700482 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300483 struct thread *th;
484
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300485 down_write(&threads->lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300486 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300487 up_write(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300488 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300489}
490
Jiri Olsad75e6092014-03-14 15:00:03 +0100491struct thread *machine__find_thread(struct machine *machine, pid_t pid,
492 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300493{
Kan Liang91e467b2017-09-10 19:23:14 -0700494 struct threads *threads = machine__threads(machine, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300495 struct thread *th;
Kan Liang91e467b2017-09-10 19:23:14 -0700496
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300497 down_read(&threads->lock);
Arnaldo Carvalho de Melo75e45e42017-09-14 16:16:34 -0300498 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300499 up_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300500 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300501}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300502
Adrian Huntercfe1c412014-07-31 09:00:45 +0300503struct comm *machine__thread_exec_comm(struct machine *machine,
504 struct thread *thread)
505{
506 if (machine->comm_exec)
507 return thread__exec_comm(thread);
508 else
509 return thread__comm(thread);
510}
511
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200512int machine__process_comm_event(struct machine *machine, union perf_event *event,
513 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300514{
Adrian Hunter314add62013-08-27 11:23:03 +0300515 struct thread *thread = machine__findnew_thread(machine,
516 event->comm.pid,
517 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300518 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300519 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300520
Adrian Huntercfe1c412014-07-31 09:00:45 +0300521 if (exec)
522 machine->comm_exec = true;
523
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300524 if (dump_trace)
525 perf_event__fprintf_comm(event, stdout);
526
Adrian Hunter65de51f2014-07-31 09:00:44 +0300527 if (thread == NULL ||
528 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300529 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300530 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300531 }
532
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300533 thread__put(thread);
534
535 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300536}
537
Hari Bathinif3b36142017-03-08 02:11:43 +0530538int machine__process_namespaces_event(struct machine *machine __maybe_unused,
539 union perf_event *event,
540 struct perf_sample *sample __maybe_unused)
541{
542 struct thread *thread = machine__findnew_thread(machine,
543 event->namespaces.pid,
544 event->namespaces.tid);
545 int err = 0;
546
547 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
548 "\nWARNING: kernel seems to support more namespaces than perf"
549 " tool.\nTry updating the perf tool..\n\n");
550
551 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
552 "\nWARNING: perf tool seems to support more namespaces than"
553 " the kernel.\nTry updating the kernel..\n\n");
554
555 if (dump_trace)
556 perf_event__fprintf_namespaces(event, stdout);
557
558 if (thread == NULL ||
559 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
560 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
561 err = -1;
562 }
563
564 thread__put(thread);
565
566 return err;
567}
568
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300569int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200570 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300571{
572 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
573 event->lost.id, event->lost.lost);
574 return 0;
575}
576
Kan Liangc4937a92015-05-10 15:13:15 -0400577int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
578 union perf_event *event, struct perf_sample *sample)
579{
580 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
581 sample->id, event->lost_samples.lost);
582 return 0;
583}
584
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300585static struct dso *machine__findnew_module_dso(struct machine *machine,
586 struct kmod_path *m,
587 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100588{
589 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100590
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300591 down_write(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300592
593 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100594 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300595 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100596 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300597 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100598
Namhyung Kim6b335e82017-05-31 21:01:04 +0900599 dso__set_module_info(dso, m, machine);
Jiri Olsaca333802015-02-17 17:29:57 +0100600 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100601 }
602
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300603 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300604out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300605 up_write(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100606 return dso;
607}
608
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300609int machine__process_aux_event(struct machine *machine __maybe_unused,
610 union perf_event *event)
611{
612 if (dump_trace)
613 perf_event__fprintf_aux(event, stdout);
614 return 0;
615}
616
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300617int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
618 union perf_event *event)
619{
620 if (dump_trace)
621 perf_event__fprintf_itrace_start(event, stdout);
622 return 0;
623}
624
Adrian Hunter02860392015-07-21 12:44:03 +0300625int machine__process_switch_event(struct machine *machine __maybe_unused,
626 union perf_event *event)
627{
628 if (dump_trace)
629 perf_event__fprintf_switch(event, stdout);
630 return 0;
631}
632
Wang Nanc03d5182015-11-26 03:59:57 +0000633static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
634{
635 const char *dup_filename;
636
637 if (!filename || !dso || !dso->long_name)
638 return;
639 if (dso->long_name[0] != '[')
640 return;
641 if (!strchr(filename, '/'))
642 return;
643
644 dup_filename = strdup(filename);
645 if (!dup_filename)
646 return;
647
Wang Nan5dcf16d2015-12-07 02:36:25 +0000648 dso__set_long_name(dso, dup_filename, true);
Wang Nanc03d5182015-11-26 03:59:57 +0000649}
650
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300651struct map *machine__findnew_module_map(struct machine *machine, u64 start,
652 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300653{
Jiri Olsaca333802015-02-17 17:29:57 +0100654 struct map *map = NULL;
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900655 struct dso *dso = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100656 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300657
Jiri Olsaca333802015-02-17 17:29:57 +0100658 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300659 return NULL;
660
Jiri Olsabc84f462015-02-17 17:31:18 +0100661 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
662 m.name);
Wang Nanc03d5182015-11-26 03:59:57 +0000663 if (map) {
664 /*
665 * If the map's dso is an offline module, give dso__load()
666 * a chance to find the file path of that module by fixing
667 * long_name.
668 */
669 dso__adjust_kmod_long_name(map->dso, filename);
Jiri Olsabc84f462015-02-17 17:31:18 +0100670 goto out;
Wang Nanc03d5182015-11-26 03:59:57 +0000671 }
Jiri Olsabc84f462015-02-17 17:31:18 +0100672
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300673 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100674 if (dso == NULL)
675 goto out;
676
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300677 map = map__new2(start, dso, MAP__FUNCTION);
678 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100679 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300680
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300681 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100682
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900683 /* Put the map here because map_groups__insert alread got it */
684 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100685out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900686 /* put the dso here, corresponding to machine__findnew_module_dso */
687 dso__put(dso);
Jiri Olsaca333802015-02-17 17:29:57 +0100688 free(m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300689 return map;
690}
691
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300692size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300693{
694 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300695 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300696
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300697 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300698 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300699 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300700 }
701
702 return ret;
703}
704
Waiman Long8fa7d872014-09-29 16:07:28 -0400705size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300706 bool (skip)(struct dso *dso, int parm), int parm)
707{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300708 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300709}
710
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300711size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300712 bool (skip)(struct dso *dso, int parm), int parm)
713{
714 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300715 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300716
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300717 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300718 struct machine *pos = rb_entry(nd, struct machine, rb_node);
719 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
720 }
721 return ret;
722}
723
724size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
725{
726 int i;
727 size_t printed = 0;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300728 struct dso *kdso = machine__kernel_map(machine)->dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300729
730 if (kdso->has_build_id) {
731 char filename[PATH_MAX];
Krister Johansend2396992017-07-05 18:48:13 -0700732 if (dso__build_id_filename(kdso, filename, sizeof(filename),
733 false))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300734 printed += fprintf(fp, "[0] %s\n", filename);
735 }
736
737 for (i = 0; i < vmlinux_path__nr_entries; ++i)
738 printed += fprintf(fp, "[%d] %s\n",
739 i + kdso->has_build_id, vmlinux_path[i]);
740
741 return printed;
742}
743
744size_t machine__fprintf(struct machine *machine, FILE *fp)
745{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300746 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -0700747 size_t ret;
748 int i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300749
Kan Liang91e467b2017-09-10 19:23:14 -0700750 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
751 struct threads *threads = &machine->threads[i];
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300752
753 down_read(&threads->lock);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300754
Kan Liang91e467b2017-09-10 19:23:14 -0700755 ret = fprintf(fp, "Threads: %u\n", threads->nr);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300756
Kan Liang91e467b2017-09-10 19:23:14 -0700757 for (nd = rb_first(&threads->entries); nd; nd = rb_next(nd)) {
758 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300759
Kan Liang91e467b2017-09-10 19:23:14 -0700760 ret += thread__fprintf(pos, fp);
761 }
762
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300763 up_read(&threads->lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300764 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300765 return ret;
766}
767
768static struct dso *machine__get_kernel(struct machine *machine)
769{
770 const char *vmlinux_name = NULL;
771 struct dso *kernel;
772
773 if (machine__is_host(machine)) {
774 vmlinux_name = symbol_conf.vmlinux_name;
775 if (!vmlinux_name)
Masami Hiramatsu0a775822016-05-15 12:19:40 +0900776 vmlinux_name = DSO__NAME_KALLSYMS;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300777
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300778 kernel = machine__findnew_kernel(machine, vmlinux_name,
779 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300780 } else {
781 char bf[PATH_MAX];
782
783 if (machine__is_default_guest(machine))
784 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
785 if (!vmlinux_name)
786 vmlinux_name = machine__mmap_name(machine, bf,
787 sizeof(bf));
788
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300789 kernel = machine__findnew_kernel(machine, vmlinux_name,
790 "[guest.kernel]",
791 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300792 }
793
794 if (kernel != NULL && (!kernel->has_build_id))
795 dso__read_running_kernel_build_id(kernel, machine);
796
797 return kernel;
798}
799
800struct process_args {
801 u64 start;
802};
803
Adrian Hunter15a0a872014-01-29 16:14:38 +0200804static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
805 size_t bufsz)
806{
807 if (machine__is_default_guest(machine))
808 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
809 else
810 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
811}
812
Simon Quea93f0e52014-06-16 11:32:09 -0700813const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
814
815/* Figure out the start address of kernel map from /proc/kallsyms.
816 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
817 * symbol_name if it's not that important.
818 */
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300819static int machine__get_running_kernel_start(struct machine *machine,
820 const char **symbol_name, u64 *start)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300821{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200822 char filename[PATH_MAX];
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300823 int i, err = -1;
Simon Quea93f0e52014-06-16 11:32:09 -0700824 const char *name;
825 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300826
Adrian Hunter15a0a872014-01-29 16:14:38 +0200827 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300828
829 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
830 return 0;
831
Simon Quea93f0e52014-06-16 11:32:09 -0700832 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300833 err = kallsyms__get_function_start(filename, name, &addr);
834 if (!err)
Simon Quea93f0e52014-06-16 11:32:09 -0700835 break;
836 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300837
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300838 if (err)
839 return -1;
840
Simon Quea93f0e52014-06-16 11:32:09 -0700841 if (symbol_name)
842 *symbol_name = name;
843
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300844 *start = addr;
845 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300846}
847
848int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
849{
Arnaldo Carvalho de Meloa0b2f5a2017-02-14 16:19:56 -0300850 int type;
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300851 u64 start = 0;
852
853 if (machine__get_running_kernel_start(machine, NULL, &start))
854 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300855
Masami Hiramatsucc1121a2015-12-09 11:11:33 +0900856 /* In case of renewal the kernel map, destroy previous one */
857 machine__destroy_kernel_maps(machine);
858
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300859 for (type = 0; type < MAP__NR_TYPES; ++type) {
860 struct kmap *kmap;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300861 struct map *map;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300862
863 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
864 if (machine->vmlinux_maps[type] == NULL)
865 return -1;
866
867 machine->vmlinux_maps[type]->map_ip =
868 machine->vmlinux_maps[type]->unmap_ip =
869 identity__map_ip;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300870 map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300871 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000872 if (!kmap)
873 return -1;
874
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300875 kmap->kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300876 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300877 }
878
879 return 0;
880}
881
882void machine__destroy_kernel_maps(struct machine *machine)
883{
Arnaldo Carvalho de Meloa0b2f5a2017-02-14 16:19:56 -0300884 int type;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300885
886 for (type = 0; type < MAP__NR_TYPES; ++type) {
887 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300888 struct map *map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300889
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300890 if (map == NULL)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300891 continue;
892
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300893 kmap = map__kmap(map);
894 map_groups__remove(&machine->kmaps, map);
Wang Nanba927322015-04-07 08:22:45 +0000895 if (kmap && kmap->ref_reloc_sym) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300896 /*
897 * ref_reloc_sym is shared among all maps, so free just
898 * on one of them.
899 */
900 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300901 zfree((char **)&kmap->ref_reloc_sym->name);
902 zfree(&kmap->ref_reloc_sym);
903 } else
904 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300905 }
906
Masami Hiramatsue96e4072015-11-18 15:40:22 +0900907 map__put(machine->vmlinux_maps[type]);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300908 machine->vmlinux_maps[type] = NULL;
909 }
910}
911
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300912int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300913{
914 int ret = 0;
915 struct dirent **namelist = NULL;
916 int i, items = 0;
917 char path[PATH_MAX];
918 pid_t pid;
919 char *endp;
920
921 if (symbol_conf.default_guest_vmlinux_name ||
922 symbol_conf.default_guest_modules ||
923 symbol_conf.default_guest_kallsyms) {
924 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
925 }
926
927 if (symbol_conf.guestmount) {
928 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
929 if (items <= 0)
930 return -ENOENT;
931 for (i = 0; i < items; i++) {
932 if (!isdigit(namelist[i]->d_name[0])) {
933 /* Filter out . and .. */
934 continue;
935 }
936 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
937 if ((*endp != '\0') ||
938 (endp == namelist[i]->d_name) ||
939 (errno == ERANGE)) {
940 pr_debug("invalid directory (%s). Skipping.\n",
941 namelist[i]->d_name);
942 continue;
943 }
944 sprintf(path, "%s/%s/proc/kallsyms",
945 symbol_conf.guestmount,
946 namelist[i]->d_name);
947 ret = access(path, R_OK);
948 if (ret) {
949 pr_debug("Can't access file %s\n", path);
950 goto failure;
951 }
952 machines__create_kernel_maps(machines, pid);
953 }
954failure:
955 free(namelist);
956 }
957
958 return ret;
959}
960
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300961void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300962{
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300963 struct rb_node *next = rb_first(&machines->guests);
964
965 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300966
967 while (next) {
968 struct machine *pos = rb_entry(next, struct machine, rb_node);
969
970 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300971 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300972 machine__delete(pos);
973 }
974}
975
Arnaldo Carvalho de Melo876650e62012-12-18 19:15:48 -0300976int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300977{
978 struct machine *machine = machines__findnew(machines, pid);
979
980 if (machine == NULL)
981 return -1;
982
983 return machine__create_kernel_maps(machine);
984}
985
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300986int __machine__load_kallsyms(struct machine *machine, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300987 enum map_type type, bool no_kcore)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300988{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300989 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300990 int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300991
992 if (ret > 0) {
993 dso__set_loaded(map->dso, type);
994 /*
995 * Since /proc/kallsyms will have multiple sessions for the
996 * kernel, with modules between them, fixup the end of all
997 * sections.
998 */
999 __map_groups__fixup_end(&machine->kmaps, type);
1000 }
1001
1002 return ret;
1003}
1004
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001005int machine__load_kallsyms(struct machine *machine, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001006 enum map_type type)
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001007{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001008 return __machine__load_kallsyms(machine, filename, type, false);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001009}
1010
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001011int machine__load_vmlinux_path(struct machine *machine, enum map_type type)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001012{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001013 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001014 int ret = dso__load_vmlinux_path(map->dso, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001015
Adrian Hunter39b12f782013-08-07 14:38:47 +03001016 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001017 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001018
1019 return ret;
1020}
1021
1022static void map_groups__fixup_end(struct map_groups *mg)
1023{
1024 int i;
1025 for (i = 0; i < MAP__NR_TYPES; ++i)
1026 __map_groups__fixup_end(mg, i);
1027}
1028
1029static char *get_kernel_version(const char *root_dir)
1030{
1031 char version[PATH_MAX];
1032 FILE *file;
1033 char *name, *tmp;
1034 const char *prefix = "Linux version ";
1035
1036 sprintf(version, "%s/proc/version", root_dir);
1037 file = fopen(version, "r");
1038 if (!file)
1039 return NULL;
1040
1041 version[0] = '\0';
1042 tmp = fgets(version, sizeof(version), file);
1043 fclose(file);
1044
1045 name = strstr(version, prefix);
1046 if (!name)
1047 return NULL;
1048 name += strlen(prefix);
1049 tmp = strchr(name, ' ');
1050 if (tmp)
1051 *tmp = '\0';
1052
1053 return strdup(name);
1054}
1055
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001056static bool is_kmod_dso(struct dso *dso)
1057{
1058 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1059 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1060}
1061
1062static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1063 struct kmod_path *m)
1064{
1065 struct map *map;
1066 char *long_name;
1067
1068 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
1069 if (map == NULL)
1070 return 0;
1071
1072 long_name = strdup(path);
1073 if (long_name == NULL)
1074 return -ENOMEM;
1075
1076 dso__set_long_name(map->dso, long_name, true);
1077 dso__kernel_module_get_build_id(map->dso, "");
1078
1079 /*
1080 * Full name could reveal us kmod compression, so
1081 * we need to update the symtab_type if needed.
1082 */
1083 if (m->comp && is_kmod_dso(map->dso))
1084 map->dso->symtab_type++;
1085
1086 return 0;
1087}
1088
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001089static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -04001090 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001091{
1092 struct dirent *dent;
1093 DIR *dir = opendir(dir_name);
1094 int ret = 0;
1095
1096 if (!dir) {
1097 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1098 return -1;
1099 }
1100
1101 while ((dent = readdir(dir)) != NULL) {
1102 char path[PATH_MAX];
1103 struct stat st;
1104
1105 /*sshfs might return bad dent->d_type, so we have to stat*/
1106 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1107 if (stat(path, &st))
1108 continue;
1109
1110 if (S_ISDIR(st.st_mode)) {
1111 if (!strcmp(dent->d_name, ".") ||
1112 !strcmp(dent->d_name, ".."))
1113 continue;
1114
Richard Yao61d42902014-04-26 13:17:55 -04001115 /* Do not follow top-level source and build symlinks */
1116 if (depth == 0) {
1117 if (!strcmp(dent->d_name, "source") ||
1118 !strcmp(dent->d_name, "build"))
1119 continue;
1120 }
1121
1122 ret = map_groups__set_modules_path_dir(mg, path,
1123 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001124 if (ret < 0)
1125 goto out;
1126 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001127 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001128
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001129 ret = kmod_path__parse_name(&m, dent->d_name);
1130 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001131 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001132
1133 if (m.kmod)
1134 ret = map_groups__set_module_path(mg, path, &m);
1135
1136 free(m.name);
1137
1138 if (ret)
1139 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001140 }
1141 }
1142
1143out:
1144 closedir(dir);
1145 return ret;
1146}
1147
1148static int machine__set_modules_path(struct machine *machine)
1149{
1150 char *version;
1151 char modules_path[PATH_MAX];
1152
1153 version = get_kernel_version(machine->root_dir);
1154 if (!version)
1155 return -1;
1156
Richard Yao61d42902014-04-26 13:17:55 -04001157 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001158 machine->root_dir, version);
1159 free(version);
1160
Richard Yao61d42902014-04-26 13:17:55 -04001161 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001162}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001163int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1164 const char *name __maybe_unused)
1165{
1166 return 0;
1167}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001168
Thomas Richter9ad46522017-08-03 15:49:02 +02001169static int machine__create_module(void *arg, const char *name, u64 start,
1170 u64 size)
Adrian Hunter316d70d2013-10-08 11:45:48 +03001171{
1172 struct machine *machine = arg;
1173 struct map *map;
1174
Song Shan Gong203d8a42016-07-21 11:10:51 +08001175 if (arch__fix_module_text_start(&start, name) < 0)
1176 return -1;
1177
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001178 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001179 if (map == NULL)
1180 return -1;
Thomas Richter9ad46522017-08-03 15:49:02 +02001181 map->end = start + size;
Adrian Hunter316d70d2013-10-08 11:45:48 +03001182
1183 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1184
1185 return 0;
1186}
1187
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001188static int machine__create_modules(struct machine *machine)
1189{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001190 const char *modules;
1191 char path[PATH_MAX];
1192
Adrian Hunterf4be9042013-09-22 13:22:09 +03001193 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001194 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001195 } else {
1196 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001197 modules = path;
1198 }
1199
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001200 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001201 return -1;
1202
Adrian Hunter316d70d2013-10-08 11:45:48 +03001203 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001204 return -1;
1205
Adrian Hunter316d70d2013-10-08 11:45:48 +03001206 if (!machine__set_modules_path(machine))
1207 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001208
Adrian Hunter316d70d2013-10-08 11:45:48 +03001209 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001210
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001211 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001212}
1213
1214int machine__create_kernel_maps(struct machine *machine)
1215{
1216 struct dso *kernel = machine__get_kernel(machine);
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001217 const char *name = NULL;
1218 u64 addr = 0;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001219 int ret;
1220
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001221 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001222 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001223
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001224 ret = __machine__create_kernel_maps(machine, kernel);
1225 dso__put(kernel);
1226 if (ret < 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001227 return -1;
1228
1229 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1230 if (machine__is_host(machine))
1231 pr_debug("Problems creating module maps, "
1232 "continuing anyway...\n");
1233 else
1234 pr_debug("Problems creating module maps for guest %d, "
1235 "continuing anyway...\n", machine->pid);
1236 }
1237
1238 /*
1239 * Now that we have all the maps created, just set the ->end of them:
1240 */
1241 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001242
Jiri Olsa3f938ee2017-06-26 11:51:53 +02001243 if (!machine__get_running_kernel_start(machine, &name, &addr)) {
1244 if (name &&
1245 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) {
1246 machine__destroy_kernel_maps(machine);
1247 return -1;
1248 }
Adrian Hunter5512cf22014-01-29 16:14:39 +02001249 }
1250
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001251 return 0;
1252}
1253
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001254static void machine__set_kernel_mmap_len(struct machine *machine,
1255 union perf_event *event)
1256{
Namhyung Kim4552cf02012-11-07 16:27:10 +09001257 int i;
1258
1259 for (i = 0; i < MAP__NR_TYPES; i++) {
1260 machine->vmlinux_maps[i]->start = event->mmap.start;
1261 machine->vmlinux_maps[i]->end = (event->mmap.start +
1262 event->mmap.len);
1263 /*
1264 * Be a bit paranoid here, some perf.data file came with
1265 * a zero sized synthesized MMAP event for the kernel.
1266 */
1267 if (machine->vmlinux_maps[i]->end == 0)
1268 machine->vmlinux_maps[i]->end = ~0ULL;
1269 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001270}
1271
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001272static bool machine__uses_kcore(struct machine *machine)
1273{
1274 struct dso *dso;
1275
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001276 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001277 if (dso__is_kcore(dso))
1278 return true;
1279 }
1280
1281 return false;
1282}
1283
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001284static int machine__process_kernel_mmap_event(struct machine *machine,
1285 union perf_event *event)
1286{
1287 struct map *map;
1288 char kmmap_prefix[PATH_MAX];
1289 enum dso_kernel_type kernel_type;
1290 bool is_kernel_mmap;
1291
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001292 /* If we have maps from kcore then we do not need or want any others */
1293 if (machine__uses_kcore(machine))
1294 return 0;
1295
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001296 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1297 if (machine__is_host(machine))
1298 kernel_type = DSO_TYPE_KERNEL;
1299 else
1300 kernel_type = DSO_TYPE_GUEST_KERNEL;
1301
1302 is_kernel_mmap = memcmp(event->mmap.filename,
1303 kmmap_prefix,
1304 strlen(kmmap_prefix) - 1) == 0;
1305 if (event->mmap.filename[0] == '/' ||
1306 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001307 map = machine__findnew_module_map(machine, event->mmap.start,
1308 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001309 if (map == NULL)
1310 goto out_problem;
1311
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001312 map->end = map->start + event->mmap.len;
1313 } else if (is_kernel_mmap) {
1314 const char *symbol_name = (event->mmap.filename +
1315 strlen(kmmap_prefix));
1316 /*
1317 * Should be there already, from the build-id table in
1318 * the header.
1319 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001320 struct dso *kernel = NULL;
1321 struct dso *dso;
1322
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001323 down_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001324
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001325 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001326
1327 /*
1328 * The cpumode passed to is_kernel_module is not the
1329 * cpumode of *this* event. If we insist on passing
1330 * correct cpumode to is_kernel_module, we should
1331 * record the cpumode when we adding this dso to the
1332 * linked list.
1333 *
1334 * However we don't really need passing correct
1335 * cpumode. We know the correct cpumode must be kernel
1336 * mode (if not, we should not link it onto kernel_dsos
1337 * list).
1338 *
1339 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1340 * is_kernel_module() treats it as a kernel cpumode.
1341 */
1342
1343 if (!dso->kernel ||
1344 is_kernel_module(dso->long_name,
1345 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001346 continue;
1347
Wang Nan1f121b02015-06-03 08:52:21 +00001348
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001349 kernel = dso;
1350 break;
1351 }
1352
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001353 up_read(&machine->dsos.lock);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001354
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001355 if (kernel == NULL)
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001356 kernel = machine__findnew_dso(machine, kmmap_prefix);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001357 if (kernel == NULL)
1358 goto out_problem;
1359
1360 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001361 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1362 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001363 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001364 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001365
Namhyung Kim330dfa22014-11-18 13:30:28 +09001366 if (strstr(kernel->long_name, "vmlinux"))
1367 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001368
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001369 machine__set_kernel_mmap_len(machine, event);
1370
1371 /*
1372 * Avoid using a zero address (kptr_restrict) for the ref reloc
1373 * symbol. Effectively having zero here means that at record
1374 * time /proc/sys/kernel/kptr_restrict was non zero.
1375 */
1376 if (event->mmap.pgoff != 0) {
1377 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1378 symbol_name,
1379 event->mmap.pgoff);
1380 }
1381
1382 if (machine__is_default_guest(machine)) {
1383 /*
1384 * preload dso of guest kernel and modules
1385 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001386 dso__load(kernel, machine__kernel_map(machine));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001387 }
1388 }
1389 return 0;
1390out_problem:
1391 return -1;
1392}
1393
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001394int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001395 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001396 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001397{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001398 struct thread *thread;
1399 struct map *map;
1400 enum map_type type;
1401 int ret = 0;
1402
1403 if (dump_trace)
1404 perf_event__fprintf_mmap2(event, stdout);
1405
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001406 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1407 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001408 ret = machine__process_kernel_mmap_event(machine, event);
1409 if (ret < 0)
1410 goto out_problem;
1411 return 0;
1412 }
1413
1414 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001415 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001416 if (thread == NULL)
1417 goto out_problem;
1418
1419 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1420 type = MAP__VARIABLE;
1421 else
1422 type = MAP__FUNCTION;
1423
Adrian Hunter2a030682014-07-22 16:17:53 +03001424 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001425 event->mmap2.len, event->mmap2.pgoff,
Krister Johansenbf2e7102017-07-05 18:48:09 -07001426 event->mmap2.maj,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001427 event->mmap2.min, event->mmap2.ino,
1428 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001429 event->mmap2.prot,
1430 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001431 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001432
1433 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001434 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001435
He Kuang8132a2a2016-06-03 03:33:13 +00001436 ret = thread__insert_map(thread, map);
1437 if (ret)
1438 goto out_problem_insert;
1439
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001440 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001441 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001442 return 0;
1443
He Kuang8132a2a2016-06-03 03:33:13 +00001444out_problem_insert:
1445 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001446out_problem_map:
1447 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001448out_problem:
1449 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1450 return 0;
1451}
1452
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001453int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001454 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001455{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001456 struct thread *thread;
1457 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001458 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001459 int ret = 0;
1460
1461 if (dump_trace)
1462 perf_event__fprintf_mmap(event, stdout);
1463
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001464 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1465 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001466 ret = machine__process_kernel_mmap_event(machine, event);
1467 if (ret < 0)
1468 goto out_problem;
1469 return 0;
1470 }
1471
Adrian Hunter314add62013-08-27 11:23:03 +03001472 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001473 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001474 if (thread == NULL)
1475 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001476
1477 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1478 type = MAP__VARIABLE;
1479 else
1480 type = MAP__FUNCTION;
1481
Adrian Hunter2a030682014-07-22 16:17:53 +03001482 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001483 event->mmap.len, event->mmap.pgoff,
Krister Johansenbf2e7102017-07-05 18:48:09 -07001484 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001485 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001486 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001487
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001488 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001489 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001490
He Kuang8132a2a2016-06-03 03:33:13 +00001491 ret = thread__insert_map(thread, map);
1492 if (ret)
1493 goto out_problem_insert;
1494
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001495 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001496 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001497 return 0;
1498
He Kuang8132a2a2016-06-03 03:33:13 +00001499out_problem_insert:
1500 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001501out_problem_map:
1502 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001503out_problem:
1504 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1505 return 0;
1506}
1507
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001508static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001509{
Kan Liang91e467b2017-09-10 19:23:14 -07001510 struct threads *threads = machine__threads(machine, th->tid);
1511
1512 if (threads->last_match == th)
1513 threads->last_match = NULL;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001514
Elena Reshetovae34f5b12017-02-21 17:35:02 +02001515 BUG_ON(refcount_read(&th->refcnt) == 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001516 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001517 down_write(&threads->lock);
Kan Liang91e467b2017-09-10 19:23:14 -07001518 rb_erase_init(&th->rb_node, &threads->entries);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001519 RB_CLEAR_NODE(&th->rb_node);
Kan Liang91e467b2017-09-10 19:23:14 -07001520 --threads->nr;
David Ahern236a3bb2013-08-14 08:49:27 -06001521 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001522 * Move it first to the dead_threads list, then drop the reference,
1523 * if this is the last reference, then the thread__delete destructor
1524 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001525 */
Kan Liang91e467b2017-09-10 19:23:14 -07001526 list_add_tail(&th->node, &threads->dead);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001527 if (lock)
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001528 up_write(&threads->lock);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001529 thread__put(th);
David Ahern236a3bb2013-08-14 08:49:27 -06001530}
1531
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001532void machine__remove_thread(struct machine *machine, struct thread *th)
1533{
1534 return __machine__remove_thread(machine, th, true);
1535}
1536
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001537int machine__process_fork_event(struct machine *machine, union perf_event *event,
1538 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001539{
Jiri Olsad75e6092014-03-14 15:00:03 +01001540 struct thread *thread = machine__find_thread(machine,
1541 event->fork.pid,
1542 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001543 struct thread *parent = machine__findnew_thread(machine,
1544 event->fork.ppid,
1545 event->fork.ptid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001546 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001547
Adrian Hunter5cb73342015-08-19 17:29:20 +03001548 if (dump_trace)
1549 perf_event__fprintf_task(event, stdout);
1550
1551 /*
1552 * There may be an existing thread that is not actually the parent,
1553 * either because we are processing events out of order, or because the
1554 * (fork) event that would have removed the thread was lost. Assume the
1555 * latter case and continue on as best we can.
1556 */
1557 if (parent->pid_ != (pid_t)event->fork.ppid) {
1558 dump_printf("removing erroneous parent thread %d/%d\n",
1559 parent->pid_, parent->tid);
1560 machine__remove_thread(machine, parent);
1561 thread__put(parent);
1562 parent = machine__findnew_thread(machine, event->fork.ppid,
1563 event->fork.ptid);
1564 }
1565
David Ahern236a3bb2013-08-14 08:49:27 -06001566 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001567 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001568 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001569 thread__put(thread);
1570 }
David Ahern236a3bb2013-08-14 08:49:27 -06001571
Adrian Hunter314add62013-08-27 11:23:03 +03001572 thread = machine__findnew_thread(machine, event->fork.pid,
1573 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001574
1575 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001576 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001577 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001578 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001579 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001580 thread__put(thread);
1581 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001582
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001583 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001584}
1585
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001586int machine__process_exit_event(struct machine *machine, union perf_event *event,
1587 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001588{
Jiri Olsad75e6092014-03-14 15:00:03 +01001589 struct thread *thread = machine__find_thread(machine,
1590 event->fork.pid,
1591 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001592
1593 if (dump_trace)
1594 perf_event__fprintf_task(event, stdout);
1595
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001596 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001597 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001598 thread__put(thread);
1599 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001600
1601 return 0;
1602}
1603
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001604int machine__process_event(struct machine *machine, union perf_event *event,
1605 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001606{
1607 int ret;
1608
1609 switch (event->header.type) {
1610 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001611 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001612 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001613 ret = machine__process_mmap_event(machine, event, sample); break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301614 case PERF_RECORD_NAMESPACES:
1615 ret = machine__process_namespaces_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001616 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001617 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001618 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001619 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001620 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001621 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001622 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001623 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001624 case PERF_RECORD_AUX:
1625 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001626 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001627 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001628 case PERF_RECORD_LOST_SAMPLES:
1629 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001630 case PERF_RECORD_SWITCH:
1631 case PERF_RECORD_SWITCH_CPU_WIDE:
1632 ret = machine__process_switch_event(machine, event); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001633 default:
1634 ret = -1;
1635 break;
1636 }
1637
1638 return ret;
1639}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001640
Greg Priceb21484f2012-12-06 21:48:05 -08001641static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001642{
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001643 if (!regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001644 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001645 return 0;
1646}
1647
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001648static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001649 struct addr_map_symbol *ams,
1650 u64 ip)
1651{
1652 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001653
1654 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001655 /*
1656 * We cannot use the header.misc hint to determine whether a
1657 * branch stack address is user, kernel, guest, hypervisor.
1658 * Branches may straddle the kernel/user/hypervisor boundaries.
1659 * Thus, we have to try consecutively until we find a match
1660 * or else, the symbol is unknown
1661 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001662 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001663
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001664 ams->addr = ip;
1665 ams->al_addr = al.addr;
1666 ams->sym = al.sym;
1667 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001668 ams->phys_addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001669}
1670
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001671static void ip__resolve_data(struct thread *thread,
Kan Liang8780fb22017-08-29 13:11:09 -04001672 u8 m, struct addr_map_symbol *ams,
1673 u64 addr, u64 phys_addr)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001674{
1675 struct addr_location al;
1676
1677 memset(&al, 0, sizeof(al));
1678
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001679 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001680 if (al.map == NULL) {
1681 /*
1682 * some shared data regions have execute bit set which puts
1683 * their mapping in the MAP__FUNCTION type array.
1684 * Check there as a fallback option before dropping the sample.
1685 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001686 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001687 }
1688
Stephane Eranian98a3b322013-01-24 16:10:35 +01001689 ams->addr = addr;
1690 ams->al_addr = al.addr;
1691 ams->sym = al.sym;
1692 ams->map = al.map;
Kan Liang8780fb22017-08-29 13:11:09 -04001693 ams->phys_addr = phys_addr;
Stephane Eranian98a3b322013-01-24 16:10:35 +01001694}
1695
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001696struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1697 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001698{
1699 struct mem_info *mi = zalloc(sizeof(*mi));
1700
1701 if (!mi)
1702 return NULL;
1703
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001704 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
Kan Liang8780fb22017-08-29 13:11:09 -04001705 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1706 sample->addr, sample->phys_addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001707 mi->data_src.val = sample->data_src;
1708
1709 return mi;
1710}
1711
Jin Yaoc4ee0622017-08-07 21:05:15 +08001712struct iterations {
1713 int nr_loop_iter;
1714 u64 cycles;
1715};
1716
Andi Kleen37592b82014-11-12 18:05:19 -08001717static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001718 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08001719 struct symbol **parent,
1720 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001721 u8 *cpumode,
Jin Yao410024d2016-10-31 09:19:49 +08001722 u64 ip,
1723 bool branch,
1724 struct branch_flags *flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08001725 struct iterations *iter,
Jin Yaob851dd42017-07-18 20:13:15 +08001726 u64 branch_from)
Andi Kleen37592b82014-11-12 18:05:19 -08001727{
1728 struct addr_location al;
Jin Yaoc4ee0622017-08-07 21:05:15 +08001729 int nr_loop_iter = 0;
1730 u64 iter_cycles = 0;
Andi Kleen37592b82014-11-12 18:05:19 -08001731
1732 al.filtered = 0;
1733 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001734 if (!cpumode) {
Andi Kleen8b7bad52014-11-12 18:05:20 -08001735 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1736 ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001737 } else {
Kan Liang2e777842014-12-02 10:06:53 -05001738 if (ip >= PERF_CONTEXT_MAX) {
1739 switch (ip) {
1740 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001741 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05001742 break;
1743 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001744 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05001745 break;
1746 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001747 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05001748 break;
1749 default:
1750 pr_debug("invalid callchain context: "
1751 "%"PRId64"\n", (s64) ip);
1752 /*
1753 * It seems the callchain is corrupted.
1754 * Discard all.
1755 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001756 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05001757 return 1;
1758 }
1759 return 0;
1760 }
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001761 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1762 ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05001763 }
1764
Andi Kleen37592b82014-11-12 18:05:19 -08001765 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02001766 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08001767 symbol__match_regex(al.sym, &parent_regex))
1768 *parent = al.sym;
1769 else if (have_ignore_callees && root_al &&
1770 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1771 /* Treat this symbol as the root,
1772 forgetting its callees. */
1773 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001774 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08001775 }
1776 }
1777
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09001778 if (symbol_conf.hide_unresolved && al.sym == NULL)
1779 return 0;
Jin Yaoc4ee0622017-08-07 21:05:15 +08001780
1781 if (iter) {
1782 nr_loop_iter = iter->nr_loop_iter;
1783 iter_cycles = iter->cycles;
1784 }
1785
Jin Yao410024d2016-10-31 09:19:49 +08001786 return callchain_cursor_append(cursor, al.addr, al.map, al.sym,
Jin Yaoc4ee0622017-08-07 21:05:15 +08001787 branch, flags, nr_loop_iter,
1788 iter_cycles, branch_from);
Andi Kleen37592b82014-11-12 18:05:19 -08001789}
1790
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001791struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1792 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001793{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001794 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001795 const struct branch_stack *bs = sample->branch_stack;
1796 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001797
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001798 if (!bi)
1799 return NULL;
1800
1801 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001802 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1803 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001804 bi[i].flags = bs->entries[i].flags;
1805 }
1806 return bi;
1807}
1808
Jin Yaoc4ee0622017-08-07 21:05:15 +08001809static void save_iterations(struct iterations *iter,
1810 struct branch_entry *be, int nr)
1811{
1812 int i;
1813
1814 iter->nr_loop_iter = nr;
1815 iter->cycles = 0;
1816
1817 for (i = 0; i < nr; i++)
1818 iter->cycles += be[i].flags.cycles;
1819}
1820
Andi Kleen8b7bad52014-11-12 18:05:20 -08001821#define CHASHSZ 127
1822#define CHASHBITS 7
1823#define NO_ENTRY 0xff
1824
1825#define PERF_MAX_BRANCH_DEPTH 127
1826
1827/* Remove loops. */
Jin Yaoc4ee0622017-08-07 21:05:15 +08001828static int remove_loops(struct branch_entry *l, int nr,
1829 struct iterations *iter)
Andi Kleen8b7bad52014-11-12 18:05:20 -08001830{
1831 int i, j, off;
1832 unsigned char chash[CHASHSZ];
1833
1834 memset(chash, NO_ENTRY, sizeof(chash));
1835
1836 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1837
1838 for (i = 0; i < nr; i++) {
1839 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1840
1841 /* no collision handling for now */
1842 if (chash[h] == NO_ENTRY) {
1843 chash[h] = i;
1844 } else if (l[chash[h]].from == l[i].from) {
1845 bool is_loop = true;
1846 /* check if it is a real loop */
1847 off = 0;
1848 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1849 if (l[j].from != l[i + off].from) {
1850 is_loop = false;
1851 break;
1852 }
1853 if (is_loop) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08001854 j = nr - (i + off);
1855 if (j > 0) {
1856 save_iterations(iter + i + off,
1857 l + i, off);
1858
1859 memmove(iter + i, iter + i + off,
1860 j * sizeof(*iter));
1861
1862 memmove(l + i, l + i + off,
1863 j * sizeof(*l));
1864 }
1865
Andi Kleen8b7bad52014-11-12 18:05:20 -08001866 nr -= off;
1867 }
1868 }
1869 }
1870 return nr;
1871}
1872
Kan Liang384b6052015-01-05 13:23:05 -05001873/*
1874 * Recolve LBR callstack chain sample
1875 * Return:
1876 * 1 on success get LBR callchain information
1877 * 0 no available LBR callchain information, should try fp
1878 * negative error code on other errors.
1879 */
1880static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001881 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001882 struct perf_sample *sample,
1883 struct symbol **parent,
1884 struct addr_location *root_al,
1885 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001886{
Kan Liang384b6052015-01-05 13:23:05 -05001887 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001888 int chain_nr = min(max_stack, (int)chain->nr), i;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001889 u8 cpumode = PERF_RECORD_MISC_USER;
Jin Yaob851dd42017-07-18 20:13:15 +08001890 u64 ip, branch_from = 0;
Kan Liang384b6052015-01-05 13:23:05 -05001891
1892 for (i = 0; i < chain_nr; i++) {
1893 if (chain->ips[i] == PERF_CONTEXT_USER)
1894 break;
1895 }
1896
1897 /* LBR only affects the user callchain */
1898 if (i != chain_nr) {
1899 struct branch_stack *lbr_stack = sample->branch_stack;
Jin Yao410024d2016-10-31 09:19:49 +08001900 int lbr_nr = lbr_stack->nr, j, k;
1901 bool branch;
1902 struct branch_flags *flags;
Kan Liang384b6052015-01-05 13:23:05 -05001903 /*
1904 * LBR callstack can only get user call chain.
1905 * The mix_chain_nr is kernel call chain
1906 * number plus LBR user call chain number.
1907 * i is kernel call chain number,
1908 * 1 is PERF_CONTEXT_USER,
1909 * lbr_nr + 1 is the user call chain number.
1910 * For details, please refer to the comments
1911 * in callchain__printf
1912 */
1913 int mix_chain_nr = i + 1 + lbr_nr + 1;
1914
Kan Liang384b6052015-01-05 13:23:05 -05001915 for (j = 0; j < mix_chain_nr; j++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001916 int err;
Jin Yao410024d2016-10-31 09:19:49 +08001917 branch = false;
1918 flags = NULL;
1919
Kan Liang384b6052015-01-05 13:23:05 -05001920 if (callchain_param.order == ORDER_CALLEE) {
1921 if (j < i + 1)
1922 ip = chain->ips[j];
Jin Yao410024d2016-10-31 09:19:49 +08001923 else if (j > i + 1) {
1924 k = j - i - 2;
1925 ip = lbr_stack->entries[k].from;
1926 branch = true;
1927 flags = &lbr_stack->entries[k].flags;
1928 } else {
Kan Liang384b6052015-01-05 13:23:05 -05001929 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08001930 branch = true;
1931 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08001932 branch_from =
1933 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08001934 }
Kan Liang384b6052015-01-05 13:23:05 -05001935 } else {
Jin Yao410024d2016-10-31 09:19:49 +08001936 if (j < lbr_nr) {
1937 k = lbr_nr - j - 1;
1938 ip = lbr_stack->entries[k].from;
1939 branch = true;
1940 flags = &lbr_stack->entries[k].flags;
1941 }
Kan Liang384b6052015-01-05 13:23:05 -05001942 else if (j > lbr_nr)
1943 ip = chain->ips[i + 1 - (j - lbr_nr)];
Jin Yao410024d2016-10-31 09:19:49 +08001944 else {
Kan Liang384b6052015-01-05 13:23:05 -05001945 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08001946 branch = true;
1947 flags = &lbr_stack->entries[0].flags;
Jin Yaob851dd42017-07-18 20:13:15 +08001948 branch_from =
1949 lbr_stack->entries[0].from;
Jin Yao410024d2016-10-31 09:19:49 +08001950 }
Kan Liang384b6052015-01-05 13:23:05 -05001951 }
1952
Jin Yao410024d2016-10-31 09:19:49 +08001953 err = add_callchain_ip(thread, cursor, parent,
1954 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08001955 branch, flags, NULL,
Jin Yaob851dd42017-07-18 20:13:15 +08001956 branch_from);
Kan Liang384b6052015-01-05 13:23:05 -05001957 if (err)
1958 return (err < 0) ? err : 0;
1959 }
1960 return 1;
1961 }
1962
1963 return 0;
1964}
1965
1966static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001967 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001968 struct perf_evsel *evsel,
1969 struct perf_sample *sample,
1970 struct symbol **parent,
1971 struct addr_location *root_al,
1972 int max_stack)
1973{
1974 struct branch_stack *branch = sample->branch_stack;
1975 struct ip_callchain *chain = sample->callchain;
Jin Yaob49a8212017-05-08 18:43:02 +08001976 int chain_nr = 0;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001977 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001978 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08001979 int skip_idx = -1;
1980 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001981
Jin Yaob49a8212017-05-08 18:43:02 +08001982 if (chain)
1983 chain_nr = chain->nr;
1984
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03001985 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001986 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05001987 root_al, max_stack);
1988 if (err)
1989 return (err < 0) ? err : 0;
1990 }
1991
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001992 /*
1993 * Based on DWARF debug information, some architectures skip
1994 * a callchain entry saved by the kernel.
1995 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001996 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001997
Andi Kleen8b7bad52014-11-12 18:05:20 -08001998 /*
1999 * Add branches to call stack for easier browsing. This gives
2000 * more context for a sample than just the callers.
2001 *
2002 * This uses individual histograms of paths compared to the
2003 * aggregated histograms the normal LBR mode uses.
2004 *
2005 * Limitations for now:
2006 * - No extra filters
2007 * - No annotations (should annotate somehow)
2008 */
2009
2010 if (branch && callchain_param.branch_callstack) {
2011 int nr = min(max_stack, (int)branch->nr);
2012 struct branch_entry be[nr];
Jin Yaoc4ee0622017-08-07 21:05:15 +08002013 struct iterations iter[nr];
Andi Kleen8b7bad52014-11-12 18:05:20 -08002014
2015 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2016 pr_warning("corrupted branch chain. skipping...\n");
2017 goto check_calls;
2018 }
2019
2020 for (i = 0; i < nr; i++) {
2021 if (callchain_param.order == ORDER_CALLEE) {
2022 be[i] = branch->entries[i];
Jin Yaob49a8212017-05-08 18:43:02 +08002023
2024 if (chain == NULL)
2025 continue;
2026
Andi Kleen8b7bad52014-11-12 18:05:20 -08002027 /*
2028 * Check for overlap into the callchain.
2029 * The return address is one off compared to
2030 * the branch entry. To adjust for this
2031 * assume the calling instruction is not longer
2032 * than 8 bytes.
2033 */
2034 if (i == skip_idx ||
2035 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2036 first_call++;
2037 else if (be[i].from < chain->ips[first_call] &&
2038 be[i].from >= chain->ips[first_call] - 8)
2039 first_call++;
2040 } else
2041 be[i] = branch->entries[branch->nr - i - 1];
2042 }
2043
Jin Yaoc4ee0622017-08-07 21:05:15 +08002044 memset(iter, 0, sizeof(struct iterations) * nr);
2045 nr = remove_loops(be, nr, iter);
Jin Yao410024d2016-10-31 09:19:49 +08002046
Andi Kleen8b7bad52014-11-12 18:05:20 -08002047 for (i = 0; i < nr; i++) {
Jin Yaoc4ee0622017-08-07 21:05:15 +08002048 err = add_callchain_ip(thread, cursor, parent,
2049 root_al,
2050 NULL, be[i].to,
2051 true, &be[i].flags,
2052 NULL, be[i].from);
Jin Yao410024d2016-10-31 09:19:49 +08002053
Andi Kleen8b7bad52014-11-12 18:05:20 -08002054 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002055 err = add_callchain_ip(thread, cursor, parent, root_al,
Jin Yao410024d2016-10-31 09:19:49 +08002056 NULL, be[i].from,
2057 true, &be[i].flags,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002058 &iter[i], 0);
Andi Kleen8b7bad52014-11-12 18:05:20 -08002059 if (err == -EINVAL)
2060 break;
2061 if (err)
2062 return err;
2063 }
Jin Yaob49a8212017-05-08 18:43:02 +08002064
2065 if (chain_nr == 0)
2066 return 0;
2067
Andi Kleen8b7bad52014-11-12 18:05:20 -08002068 chain_nr -= nr;
2069 }
2070
2071check_calls:
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002072 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002073 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002074 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002075
2076 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002077 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002078 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002079 j = chain->nr - i - 1;
2080
2081#ifdef HAVE_SKIP_CALLCHAIN_IDX
2082 if (j == skip_idx)
2083 continue;
2084#endif
2085 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002086
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002087 if (ip < PERF_CONTEXT_MAX)
2088 ++nr_entries;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002089
Jin Yao410024d2016-10-31 09:19:49 +08002090 err = add_callchain_ip(thread, cursor, parent,
2091 root_al, &cpumode, ip,
Jin Yaoc4ee0622017-08-07 21:05:15 +08002092 false, NULL, NULL, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002093
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002094 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05002095 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002096 }
2097
2098 return 0;
2099}
2100
2101static int unwind_entry(struct unwind_entry *entry, void *arg)
2102{
2103 struct callchain_cursor *cursor = arg;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002104
2105 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2106 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002107 return callchain_cursor_append(cursor, entry->ip,
Jin Yao410024d2016-10-31 09:19:49 +08002108 entry->map, entry->sym,
Jin Yaob851dd42017-07-18 20:13:15 +08002109 false, NULL, 0, 0, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002110}
2111
Chris Phlipot9919a652016-04-28 01:19:06 -07002112static int thread__resolve_callchain_unwind(struct thread *thread,
2113 struct callchain_cursor *cursor,
2114 struct perf_evsel *evsel,
2115 struct perf_sample *sample,
2116 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002117{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002118 /* Can we do dwarf post unwind? */
2119 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2120 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
2121 return 0;
2122
2123 /* Bail out if nothing was captured. */
2124 if ((!sample->user_regs.regs) ||
2125 (!sample->user_stack.size))
2126 return 0;
2127
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002128 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01002129 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07002130}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002131
Chris Phlipot9919a652016-04-28 01:19:06 -07002132int thread__resolve_callchain(struct thread *thread,
2133 struct callchain_cursor *cursor,
2134 struct perf_evsel *evsel,
2135 struct perf_sample *sample,
2136 struct symbol **parent,
2137 struct addr_location *root_al,
2138 int max_stack)
2139{
2140 int ret = 0;
2141
2142 callchain_cursor_reset(&callchain_cursor);
2143
2144 if (callchain_param.order == ORDER_CALLEE) {
2145 ret = thread__resolve_callchain_sample(thread, cursor,
2146 evsel, sample,
2147 parent, root_al,
2148 max_stack);
2149 if (ret)
2150 return ret;
2151 ret = thread__resolve_callchain_unwind(thread, cursor,
2152 evsel, sample,
2153 max_stack);
2154 } else {
2155 ret = thread__resolve_callchain_unwind(thread, cursor,
2156 evsel, sample,
2157 max_stack);
2158 if (ret)
2159 return ret;
2160 ret = thread__resolve_callchain_sample(thread, cursor,
2161 evsel, sample,
2162 parent, root_al,
2163 max_stack);
2164 }
2165
2166 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002167}
David Ahern35feee12013-09-28 13:12:58 -06002168
2169int machine__for_each_thread(struct machine *machine,
2170 int (*fn)(struct thread *thread, void *p),
2171 void *priv)
2172{
Kan Liang91e467b2017-09-10 19:23:14 -07002173 struct threads *threads;
David Ahern35feee12013-09-28 13:12:58 -06002174 struct rb_node *nd;
2175 struct thread *thread;
2176 int rc = 0;
Kan Liang91e467b2017-09-10 19:23:14 -07002177 int i;
David Ahern35feee12013-09-28 13:12:58 -06002178
Kan Liang91e467b2017-09-10 19:23:14 -07002179 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
2180 threads = &machine->threads[i];
2181 for (nd = rb_first(&threads->entries); nd; nd = rb_next(nd)) {
2182 thread = rb_entry(nd, struct thread, rb_node);
2183 rc = fn(thread, priv);
2184 if (rc != 0)
2185 return rc;
2186 }
David Ahern35feee12013-09-28 13:12:58 -06002187
Kan Liang91e467b2017-09-10 19:23:14 -07002188 list_for_each_entry(thread, &threads->dead, node) {
2189 rc = fn(thread, priv);
2190 if (rc != 0)
2191 return rc;
2192 }
David Ahern35feee12013-09-28 13:12:58 -06002193 }
2194 return rc;
2195}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002196
Adrian Huntera5499b32015-05-29 16:33:30 +03002197int machines__for_each_thread(struct machines *machines,
2198 int (*fn)(struct thread *thread, void *p),
2199 void *priv)
2200{
2201 struct rb_node *nd;
2202 int rc = 0;
2203
2204 rc = machine__for_each_thread(&machines->host, fn, priv);
2205 if (rc != 0)
2206 return rc;
2207
2208 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
2209 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2210
2211 rc = machine__for_each_thread(machine, fn, priv);
2212 if (rc != 0)
2213 return rc;
2214 }
2215 return rc;
2216}
2217
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03002218int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002219 struct target *target, struct thread_map *threads,
Kan Liang9d9cad72015-06-17 09:51:11 -04002220 perf_event__handler_t process, bool data_mmap,
2221 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002222{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002223 if (target__has_task(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002224 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002225 else if (target__has_cpu(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002226 return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002227 /* command specified */
2228 return 0;
2229}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002230
2231pid_t machine__get_current_tid(struct machine *machine, int cpu)
2232{
2233 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2234 return -1;
2235
2236 return machine->current_tid[cpu];
2237}
2238
2239int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2240 pid_t tid)
2241{
2242 struct thread *thread;
2243
2244 if (cpu < 0)
2245 return -EINVAL;
2246
2247 if (!machine->current_tid) {
2248 int i;
2249
2250 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2251 if (!machine->current_tid)
2252 return -ENOMEM;
2253 for (i = 0; i < MAX_NR_CPUS; i++)
2254 machine->current_tid[i] = -1;
2255 }
2256
2257 if (cpu >= MAX_NR_CPUS) {
2258 pr_err("Requested CPU %d too large. ", cpu);
2259 pr_err("Consider raising MAX_NR_CPUS\n");
2260 return -EINVAL;
2261 }
2262
2263 machine->current_tid[cpu] = tid;
2264
2265 thread = machine__findnew_thread(machine, pid, tid);
2266 if (!thread)
2267 return -ENOMEM;
2268
2269 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002270 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002271
2272 return 0;
2273}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002274
2275int machine__get_kernel_start(struct machine *machine)
2276{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002277 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002278 int err = 0;
2279
2280 /*
2281 * The only addresses above 2^63 are kernel addresses of a 64-bit
2282 * kernel. Note that addresses are unsigned so that on a 32-bit system
2283 * all addresses including kernel addresses are less than 2^32. In
2284 * that case (32-bit system), if the kernel mapping is unknown, all
2285 * addresses will be assumed to be in user space - see
2286 * machine__kernel_ip().
2287 */
2288 machine->kernel_start = 1ULL << 63;
2289 if (map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002290 err = map__load(map);
Arnaldo Carvalho de Melo4b1303d2017-07-11 16:21:40 -03002291 if (!err)
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002292 machine->kernel_start = map->start;
2293 }
2294 return err;
2295}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002296
2297struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2298{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03002299 return dsos__findnew(&machine->dsos, filename);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002300}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002301
2302char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2303{
2304 struct machine *machine = vmachine;
2305 struct map *map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002306 struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002307
2308 if (sym == NULL)
2309 return NULL;
2310
2311 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2312 *addrp = map->unmap_ip(map, sym->start);
2313 return sym->name;
2314}