blob: f13f46a99b362204209541462154f7df27e854e4 [file] [log] [blame]
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03001#include <inttypes.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002#include "callchain.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03003#include "debug.h"
4#include "event.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03005#include "evsel.h"
6#include "hist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -03007#include "machine.h"
8#include "map.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03009#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030010#include "strlist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030011#include "thread.h"
Adrian Hunterd027b642014-07-23 14:23:00 +030012#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030013#include <stdbool.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030014#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080015#include "linux/hash.h"
Hari Bathinif3b36142017-03-08 02:11:43 +053016#include "asm/bug.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030017
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030018#include "sane_ctype.h"
19#include <symbol/kallsyms.h>
20
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030021static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
22
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030023static void dsos__init(struct dsos *dsos)
24{
25 INIT_LIST_HEAD(&dsos->head);
26 dsos->root = RB_ROOT;
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -030027 pthread_rwlock_init(&dsos->lock, NULL);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030028}
29
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030030int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
31{
Wang Nan93b0ba32015-12-08 02:25:44 +000032 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030033 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030034 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030035 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030036
37 machine->threads = RB_ROOT;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030038 pthread_rwlock_init(&machine->threads_lock, NULL);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -030039 machine->nr_threads = 0;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030040 INIT_LIST_HEAD(&machine->dead_threads);
41 machine->last_match = NULL;
42
Adrian Hunterd027b642014-07-23 14:23:00 +030043 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030044 machine->env = NULL;
Adrian Hunterd027b642014-07-23 14:23:00 +030045
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030046 machine->pid = pid;
47
Jiri Olsa14bd6d22014-01-07 13:47:19 +010048 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -030049 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +030050 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030051 machine->kernel_start = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030052
Masami Hiramatsucc1121a2015-12-09 11:11:33 +090053 memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps));
54
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030055 machine->root_dir = strdup(root_dir);
56 if (machine->root_dir == NULL)
57 return -ENOMEM;
58
59 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030060 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030061 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030062 char comm[64];
63
64 if (thread == NULL)
65 return -ENOMEM;
66
67 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020068 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030069 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030070 }
71
Adrian Hunterb9d266b2014-07-22 16:17:25 +030072 machine->current_tid = NULL;
73
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030074 return 0;
75}
76
David Ahern8fb598e2013-09-28 13:13:00 -060077struct machine *machine__new_host(void)
78{
79 struct machine *machine = malloc(sizeof(*machine));
80
81 if (machine != NULL) {
82 machine__init(machine, "", HOST_KERNEL_ID);
83
84 if (machine__create_kernel_maps(machine) < 0)
85 goto out_delete;
86 }
87
88 return machine;
89out_delete:
90 free(machine);
91 return NULL;
92}
93
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -030094struct machine *machine__new_kallsyms(void)
95{
96 struct machine *machine = machine__new_host();
97 /*
98 * FIXME:
99 * 1) MAP__FUNCTION will go away when we stop loading separate maps for
100 * functions and data objects.
101 * 2) We should switch to machine__load_kallsyms(), i.e. not explicitely
102 * ask for not using the kcore parsing code, once this one is fixed
103 * to create a map per module.
104 */
105 if (machine && __machine__load_kallsyms(machine, "/proc/kallsyms", MAP__FUNCTION, true) <= 0) {
106 machine__delete(machine);
107 machine = NULL;
108 }
109
110 return machine;
111}
112
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300113static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300114{
115 struct dso *pos, *n;
116
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300117 pthread_rwlock_wrlock(&dsos->lock);
118
Waiman Long8fa7d872014-09-29 16:07:28 -0400119 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -0400120 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +0200121 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300122 list_del_init(&pos->node);
123 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300124 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300125
126 pthread_rwlock_unlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300127}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300128
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300129static void dsos__exit(struct dsos *dsos)
130{
131 dsos__purge(dsos);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300132 pthread_rwlock_destroy(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300133}
134
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300135void machine__delete_threads(struct machine *machine)
136{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300137 struct rb_node *nd;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300138
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300139 pthread_rwlock_wrlock(&machine->threads_lock);
140 nd = rb_first(&machine->threads);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300141 while (nd) {
142 struct thread *t = rb_entry(nd, struct thread, rb_node);
143
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300144 nd = rb_next(nd);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300145 __machine__remove_thread(machine, t, false);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300146 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300147 pthread_rwlock_unlock(&machine->threads_lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300148}
149
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300150void machine__exit(struct machine *machine)
151{
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900152 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300153 map_groups__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300154 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300155 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300156 zfree(&machine->root_dir);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300157 zfree(&machine->current_tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300158 pthread_rwlock_destroy(&machine->threads_lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300159}
160
161void machine__delete(struct machine *machine)
162{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300163 if (machine) {
164 machine__exit(machine);
165 free(machine);
166 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300167}
168
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300169void machines__init(struct machines *machines)
170{
171 machine__init(&machines->host, "", HOST_KERNEL_ID);
172 machines->guests = RB_ROOT;
173}
174
175void machines__exit(struct machines *machines)
176{
177 machine__exit(&machines->host);
178 /* XXX exit guest */
179}
180
181struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300182 const char *root_dir)
183{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300184 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300185 struct rb_node *parent = NULL;
186 struct machine *pos, *machine = malloc(sizeof(*machine));
187
188 if (machine == NULL)
189 return NULL;
190
191 if (machine__init(machine, root_dir, pid) != 0) {
192 free(machine);
193 return NULL;
194 }
195
196 while (*p != NULL) {
197 parent = *p;
198 pos = rb_entry(parent, struct machine, rb_node);
199 if (pid < pos->pid)
200 p = &(*p)->rb_left;
201 else
202 p = &(*p)->rb_right;
203 }
204
205 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300206 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300207
208 return machine;
209}
210
Adrian Huntercfe1c412014-07-31 09:00:45 +0300211void machines__set_comm_exec(struct machines *machines, bool comm_exec)
212{
213 struct rb_node *nd;
214
215 machines->host.comm_exec = comm_exec;
216
217 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
218 struct machine *machine = rb_entry(nd, struct machine, rb_node);
219
220 machine->comm_exec = comm_exec;
221 }
222}
223
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300224struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300225{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300226 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300227 struct rb_node *parent = NULL;
228 struct machine *machine;
229 struct machine *default_machine = NULL;
230
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300231 if (pid == HOST_KERNEL_ID)
232 return &machines->host;
233
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300234 while (*p != NULL) {
235 parent = *p;
236 machine = rb_entry(parent, struct machine, rb_node);
237 if (pid < machine->pid)
238 p = &(*p)->rb_left;
239 else if (pid > machine->pid)
240 p = &(*p)->rb_right;
241 else
242 return machine;
243 if (!machine->pid)
244 default_machine = machine;
245 }
246
247 return default_machine;
248}
249
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300250struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300251{
252 char path[PATH_MAX];
253 const char *root_dir = "";
254 struct machine *machine = machines__find(machines, pid);
255
256 if (machine && (machine->pid == pid))
257 goto out;
258
259 if ((pid != HOST_KERNEL_ID) &&
260 (pid != DEFAULT_GUEST_KERNEL_ID) &&
261 (symbol_conf.guestmount)) {
262 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
263 if (access(path, R_OK)) {
264 static struct strlist *seen;
265
266 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300267 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300268
269 if (!strlist__has_entry(seen, path)) {
270 pr_err("Can't access file %s\n", path);
271 strlist__add(seen, path);
272 }
273 machine = NULL;
274 goto out;
275 }
276 root_dir = path;
277 }
278
279 machine = machines__add(machines, pid, root_dir);
280out:
281 return machine;
282}
283
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300284void machines__process_guests(struct machines *machines,
285 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300286{
287 struct rb_node *nd;
288
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300289 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300290 struct machine *pos = rb_entry(nd, struct machine, rb_node);
291 process(pos, data);
292 }
293}
294
295char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
296{
297 if (machine__is_host(machine))
298 snprintf(bf, size, "[%s]", "kernel.kallsyms");
299 else if (machine__is_default_guest(machine))
300 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
301 else {
302 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
303 machine->pid);
304 }
305
306 return bf;
307}
308
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300309void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300310{
311 struct rb_node *node;
312 struct machine *machine;
313
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300314 machines->host.id_hdr_size = id_hdr_size;
315
316 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300317 machine = rb_entry(node, struct machine, rb_node);
318 machine->id_hdr_size = id_hdr_size;
319 }
320
321 return;
322}
323
Adrian Hunter29ce3612014-07-16 11:07:13 +0300324static void machine__update_thread_pid(struct machine *machine,
325 struct thread *th, pid_t pid)
326{
327 struct thread *leader;
328
329 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
330 return;
331
332 th->pid_ = pid;
333
334 if (th->pid_ == th->tid)
335 return;
336
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300337 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300338 if (!leader)
339 goto out_err;
340
341 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300342 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300343
344 if (!leader->mg)
345 goto out_err;
346
347 if (th->mg == leader->mg)
348 return;
349
350 if (th->mg) {
351 /*
352 * Maps are created from MMAP events which provide the pid and
353 * tid. Consequently there never should be any maps on a thread
354 * with an unknown pid. Just print an error if there are.
355 */
356 if (!map_groups__empty(th->mg))
357 pr_err("Discarding thread maps for %d:%d\n",
358 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300359 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300360 }
361
362 th->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300363out_put:
364 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300365 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300366out_err:
367 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300368 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300369}
370
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300371/*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800372 * Caller must eventually drop thread->refcnt returned with a successful
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300373 * lookup/new thread inserted.
374 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300375static struct thread *____machine__findnew_thread(struct machine *machine,
376 pid_t pid, pid_t tid,
377 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300378{
379 struct rb_node **p = &machine->threads.rb_node;
380 struct rb_node *parent = NULL;
381 struct thread *th;
382
383 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300384 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300385 * so most of the time we dont have to look up
386 * the full rbtree:
387 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300388 th = machine->last_match;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300389 if (th != NULL) {
390 if (th->tid == tid) {
391 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300392 return thread__get(th);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300393 }
394
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300395 machine->last_match = NULL;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300396 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300397
398 while (*p != NULL) {
399 parent = *p;
400 th = rb_entry(parent, struct thread, rb_node);
401
Adrian Hunter38051232013-07-04 16:20:31 +0300402 if (th->tid == tid) {
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300403 machine->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300404 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300405 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300406 }
407
Adrian Hunter38051232013-07-04 16:20:31 +0300408 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300409 p = &(*p)->rb_left;
410 else
411 p = &(*p)->rb_right;
412 }
413
414 if (!create)
415 return NULL;
416
Adrian Hunter99d725f2013-08-26 16:00:19 +0300417 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300418 if (th != NULL) {
419 rb_link_node(&th->rb_node, parent, p);
420 rb_insert_color(&th->rb_node, &machine->threads);
Jiri Olsacddcef62014-04-09 20:54:29 +0200421
422 /*
423 * We have to initialize map_groups separately
424 * after rb tree is updated.
425 *
426 * The reason is that we call machine__findnew_thread
427 * within thread__init_map_groups to find the thread
428 * leader and that would screwed the rb tree.
429 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300430 if (thread__init_map_groups(th, machine)) {
Arnaldo Carvalho de Melo0170b142015-05-25 15:23:05 -0300431 rb_erase_init(&th->rb_node, &machine->threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300432 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300433 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200434 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300435 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300436 /*
437 * It is now in the rbtree, get a ref
438 */
439 thread__get(th);
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300440 machine->last_match = th;
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300441 ++machine->nr_threads;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300442 }
443
444 return th;
445}
446
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300447struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
448{
449 return ____machine__findnew_thread(machine, pid, tid, true);
450}
451
Adrian Hunter314add62013-08-27 11:23:03 +0300452struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
453 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300454{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300455 struct thread *th;
456
457 pthread_rwlock_wrlock(&machine->threads_lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300458 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300459 pthread_rwlock_unlock(&machine->threads_lock);
460 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300461}
462
Jiri Olsad75e6092014-03-14 15:00:03 +0100463struct thread *machine__find_thread(struct machine *machine, pid_t pid,
464 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300465{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300466 struct thread *th;
467 pthread_rwlock_rdlock(&machine->threads_lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300468 th = ____machine__findnew_thread(machine, pid, tid, false);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300469 pthread_rwlock_unlock(&machine->threads_lock);
470 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300471}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300472
Adrian Huntercfe1c412014-07-31 09:00:45 +0300473struct comm *machine__thread_exec_comm(struct machine *machine,
474 struct thread *thread)
475{
476 if (machine->comm_exec)
477 return thread__exec_comm(thread);
478 else
479 return thread__comm(thread);
480}
481
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200482int machine__process_comm_event(struct machine *machine, union perf_event *event,
483 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300484{
Adrian Hunter314add62013-08-27 11:23:03 +0300485 struct thread *thread = machine__findnew_thread(machine,
486 event->comm.pid,
487 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300488 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300489 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300490
Adrian Huntercfe1c412014-07-31 09:00:45 +0300491 if (exec)
492 machine->comm_exec = true;
493
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300494 if (dump_trace)
495 perf_event__fprintf_comm(event, stdout);
496
Adrian Hunter65de51f2014-07-31 09:00:44 +0300497 if (thread == NULL ||
498 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300499 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300500 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300501 }
502
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300503 thread__put(thread);
504
505 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300506}
507
Hari Bathinif3b36142017-03-08 02:11:43 +0530508int machine__process_namespaces_event(struct machine *machine __maybe_unused,
509 union perf_event *event,
510 struct perf_sample *sample __maybe_unused)
511{
512 struct thread *thread = machine__findnew_thread(machine,
513 event->namespaces.pid,
514 event->namespaces.tid);
515 int err = 0;
516
517 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
518 "\nWARNING: kernel seems to support more namespaces than perf"
519 " tool.\nTry updating the perf tool..\n\n");
520
521 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
522 "\nWARNING: perf tool seems to support more namespaces than"
523 " the kernel.\nTry updating the kernel..\n\n");
524
525 if (dump_trace)
526 perf_event__fprintf_namespaces(event, stdout);
527
528 if (thread == NULL ||
529 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
530 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
531 err = -1;
532 }
533
534 thread__put(thread);
535
536 return err;
537}
538
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300539int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200540 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300541{
542 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
543 event->lost.id, event->lost.lost);
544 return 0;
545}
546
Kan Liangc4937a92015-05-10 15:13:15 -0400547int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
548 union perf_event *event, struct perf_sample *sample)
549{
550 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
551 sample->id, event->lost_samples.lost);
552 return 0;
553}
554
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300555static struct dso *machine__findnew_module_dso(struct machine *machine,
556 struct kmod_path *m,
557 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100558{
559 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100560
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300561 pthread_rwlock_wrlock(&machine->dsos.lock);
562
563 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100564 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300565 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100566 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300567 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100568
569 if (machine__is_host(machine))
570 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
571 else
572 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
573
574 /* _KMODULE_COMP should be next to _KMODULE */
Jiri Olsaca333802015-02-17 17:29:57 +0100575 if (m->kmod && m->comp)
Jiri Olsada17ea32015-02-12 22:10:52 +0100576 dso->symtab_type++;
Jiri Olsaca333802015-02-17 17:29:57 +0100577
578 dso__set_short_name(dso, strdup(m->name), true);
579 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100580 }
581
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300582 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300583out_unlock:
584 pthread_rwlock_unlock(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100585 return dso;
586}
587
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300588int machine__process_aux_event(struct machine *machine __maybe_unused,
589 union perf_event *event)
590{
591 if (dump_trace)
592 perf_event__fprintf_aux(event, stdout);
593 return 0;
594}
595
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300596int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
597 union perf_event *event)
598{
599 if (dump_trace)
600 perf_event__fprintf_itrace_start(event, stdout);
601 return 0;
602}
603
Adrian Hunter02860392015-07-21 12:44:03 +0300604int machine__process_switch_event(struct machine *machine __maybe_unused,
605 union perf_event *event)
606{
607 if (dump_trace)
608 perf_event__fprintf_switch(event, stdout);
609 return 0;
610}
611
Wang Nanc03d5182015-11-26 03:59:57 +0000612static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
613{
614 const char *dup_filename;
615
616 if (!filename || !dso || !dso->long_name)
617 return;
618 if (dso->long_name[0] != '[')
619 return;
620 if (!strchr(filename, '/'))
621 return;
622
623 dup_filename = strdup(filename);
624 if (!dup_filename)
625 return;
626
Wang Nan5dcf16d2015-12-07 02:36:25 +0000627 dso__set_long_name(dso, dup_filename, true);
Wang Nanc03d5182015-11-26 03:59:57 +0000628}
629
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300630struct map *machine__findnew_module_map(struct machine *machine, u64 start,
631 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300632{
Jiri Olsaca333802015-02-17 17:29:57 +0100633 struct map *map = NULL;
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900634 struct dso *dso = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100635 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300636
Jiri Olsaca333802015-02-17 17:29:57 +0100637 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300638 return NULL;
639
Jiri Olsabc84f462015-02-17 17:31:18 +0100640 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
641 m.name);
Wang Nanc03d5182015-11-26 03:59:57 +0000642 if (map) {
643 /*
644 * If the map's dso is an offline module, give dso__load()
645 * a chance to find the file path of that module by fixing
646 * long_name.
647 */
648 dso__adjust_kmod_long_name(map->dso, filename);
Jiri Olsabc84f462015-02-17 17:31:18 +0100649 goto out;
Wang Nanc03d5182015-11-26 03:59:57 +0000650 }
Jiri Olsabc84f462015-02-17 17:31:18 +0100651
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300652 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100653 if (dso == NULL)
654 goto out;
655
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300656 map = map__new2(start, dso, MAP__FUNCTION);
657 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100658 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300659
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300660 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100661
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900662 /* Put the map here because map_groups__insert alread got it */
663 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100664out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900665 /* put the dso here, corresponding to machine__findnew_module_dso */
666 dso__put(dso);
Jiri Olsaca333802015-02-17 17:29:57 +0100667 free(m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300668 return map;
669}
670
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300671size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300672{
673 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300674 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300675
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300676 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300677 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300678 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300679 }
680
681 return ret;
682}
683
Waiman Long8fa7d872014-09-29 16:07:28 -0400684size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300685 bool (skip)(struct dso *dso, int parm), int parm)
686{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300687 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300688}
689
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300690size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300691 bool (skip)(struct dso *dso, int parm), int parm)
692{
693 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300694 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300695
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300696 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300697 struct machine *pos = rb_entry(nd, struct machine, rb_node);
698 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
699 }
700 return ret;
701}
702
703size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
704{
705 int i;
706 size_t printed = 0;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300707 struct dso *kdso = machine__kernel_map(machine)->dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300708
709 if (kdso->has_build_id) {
710 char filename[PATH_MAX];
711 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
712 printed += fprintf(fp, "[0] %s\n", filename);
713 }
714
715 for (i = 0; i < vmlinux_path__nr_entries; ++i)
716 printed += fprintf(fp, "[%d] %s\n",
717 i + kdso->has_build_id, vmlinux_path[i]);
718
719 return printed;
720}
721
722size_t machine__fprintf(struct machine *machine, FILE *fp)
723{
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300724 size_t ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300725 struct rb_node *nd;
726
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300727 pthread_rwlock_rdlock(&machine->threads_lock);
728
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300729 ret = fprintf(fp, "Threads: %u\n", machine->nr_threads);
730
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300731 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
732 struct thread *pos = rb_entry(nd, struct thread, rb_node);
733
734 ret += thread__fprintf(pos, fp);
735 }
736
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300737 pthread_rwlock_unlock(&machine->threads_lock);
738
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300739 return ret;
740}
741
742static struct dso *machine__get_kernel(struct machine *machine)
743{
744 const char *vmlinux_name = NULL;
745 struct dso *kernel;
746
747 if (machine__is_host(machine)) {
748 vmlinux_name = symbol_conf.vmlinux_name;
749 if (!vmlinux_name)
Masami Hiramatsu0a775822016-05-15 12:19:40 +0900750 vmlinux_name = DSO__NAME_KALLSYMS;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300751
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300752 kernel = machine__findnew_kernel(machine, vmlinux_name,
753 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300754 } else {
755 char bf[PATH_MAX];
756
757 if (machine__is_default_guest(machine))
758 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
759 if (!vmlinux_name)
760 vmlinux_name = machine__mmap_name(machine, bf,
761 sizeof(bf));
762
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300763 kernel = machine__findnew_kernel(machine, vmlinux_name,
764 "[guest.kernel]",
765 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300766 }
767
768 if (kernel != NULL && (!kernel->has_build_id))
769 dso__read_running_kernel_build_id(kernel, machine);
770
771 return kernel;
772}
773
774struct process_args {
775 u64 start;
776};
777
Adrian Hunter15a0a872014-01-29 16:14:38 +0200778static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
779 size_t bufsz)
780{
781 if (machine__is_default_guest(machine))
782 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
783 else
784 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
785}
786
Simon Quea93f0e52014-06-16 11:32:09 -0700787const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
788
789/* Figure out the start address of kernel map from /proc/kallsyms.
790 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
791 * symbol_name if it's not that important.
792 */
Adrian Hunter4b993752014-08-15 22:08:38 +0300793static u64 machine__get_running_kernel_start(struct machine *machine,
794 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300795{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200796 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700797 int i;
798 const char *name;
799 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300800
Adrian Hunter15a0a872014-01-29 16:14:38 +0200801 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300802
803 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
804 return 0;
805
Simon Quea93f0e52014-06-16 11:32:09 -0700806 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
807 addr = kallsyms__get_function_start(filename, name);
808 if (addr)
809 break;
810 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300811
Simon Quea93f0e52014-06-16 11:32:09 -0700812 if (symbol_name)
813 *symbol_name = name;
814
815 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300816}
817
818int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
819{
Arnaldo Carvalho de Meloa0b2f5a2017-02-14 16:19:56 -0300820 int type;
Adrian Hunter4b993752014-08-15 22:08:38 +0300821 u64 start = machine__get_running_kernel_start(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300822
Masami Hiramatsucc1121a2015-12-09 11:11:33 +0900823 /* In case of renewal the kernel map, destroy previous one */
824 machine__destroy_kernel_maps(machine);
825
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300826 for (type = 0; type < MAP__NR_TYPES; ++type) {
827 struct kmap *kmap;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300828 struct map *map;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300829
830 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
831 if (machine->vmlinux_maps[type] == NULL)
832 return -1;
833
834 machine->vmlinux_maps[type]->map_ip =
835 machine->vmlinux_maps[type]->unmap_ip =
836 identity__map_ip;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300837 map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300838 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000839 if (!kmap)
840 return -1;
841
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300842 kmap->kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300843 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300844 }
845
846 return 0;
847}
848
849void machine__destroy_kernel_maps(struct machine *machine)
850{
Arnaldo Carvalho de Meloa0b2f5a2017-02-14 16:19:56 -0300851 int type;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300852
853 for (type = 0; type < MAP__NR_TYPES; ++type) {
854 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300855 struct map *map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300856
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300857 if (map == NULL)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300858 continue;
859
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300860 kmap = map__kmap(map);
861 map_groups__remove(&machine->kmaps, map);
Wang Nanba927322015-04-07 08:22:45 +0000862 if (kmap && kmap->ref_reloc_sym) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300863 /*
864 * ref_reloc_sym is shared among all maps, so free just
865 * on one of them.
866 */
867 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300868 zfree((char **)&kmap->ref_reloc_sym->name);
869 zfree(&kmap->ref_reloc_sym);
870 } else
871 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300872 }
873
Masami Hiramatsue96e4072015-11-18 15:40:22 +0900874 map__put(machine->vmlinux_maps[type]);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300875 machine->vmlinux_maps[type] = NULL;
876 }
877}
878
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300879int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300880{
881 int ret = 0;
882 struct dirent **namelist = NULL;
883 int i, items = 0;
884 char path[PATH_MAX];
885 pid_t pid;
886 char *endp;
887
888 if (symbol_conf.default_guest_vmlinux_name ||
889 symbol_conf.default_guest_modules ||
890 symbol_conf.default_guest_kallsyms) {
891 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
892 }
893
894 if (symbol_conf.guestmount) {
895 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
896 if (items <= 0)
897 return -ENOENT;
898 for (i = 0; i < items; i++) {
899 if (!isdigit(namelist[i]->d_name[0])) {
900 /* Filter out . and .. */
901 continue;
902 }
903 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
904 if ((*endp != '\0') ||
905 (endp == namelist[i]->d_name) ||
906 (errno == ERANGE)) {
907 pr_debug("invalid directory (%s). Skipping.\n",
908 namelist[i]->d_name);
909 continue;
910 }
911 sprintf(path, "%s/%s/proc/kallsyms",
912 symbol_conf.guestmount,
913 namelist[i]->d_name);
914 ret = access(path, R_OK);
915 if (ret) {
916 pr_debug("Can't access file %s\n", path);
917 goto failure;
918 }
919 machines__create_kernel_maps(machines, pid);
920 }
921failure:
922 free(namelist);
923 }
924
925 return ret;
926}
927
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300928void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300929{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300930 struct rb_node *next = rb_first(&machines->guests);
931
932 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300933
934 while (next) {
935 struct machine *pos = rb_entry(next, struct machine, rb_node);
936
937 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300938 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300939 machine__delete(pos);
940 }
941}
942
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300943int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300944{
945 struct machine *machine = machines__findnew(machines, pid);
946
947 if (machine == NULL)
948 return -1;
949
950 return machine__create_kernel_maps(machine);
951}
952
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300953int __machine__load_kallsyms(struct machine *machine, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300954 enum map_type type, bool no_kcore)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300955{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300956 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300957 int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300958
959 if (ret > 0) {
960 dso__set_loaded(map->dso, type);
961 /*
962 * Since /proc/kallsyms will have multiple sessions for the
963 * kernel, with modules between them, fixup the end of all
964 * sections.
965 */
966 __map_groups__fixup_end(&machine->kmaps, type);
967 }
968
969 return ret;
970}
971
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300972int machine__load_kallsyms(struct machine *machine, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300973 enum map_type type)
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300974{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300975 return __machine__load_kallsyms(machine, filename, type, false);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300976}
977
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300978int machine__load_vmlinux_path(struct machine *machine, enum map_type type)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300979{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300980 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300981 int ret = dso__load_vmlinux_path(map->dso, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300982
Adrian Hunter39b12f782013-08-07 14:38:47 +0300983 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300984 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300985
986 return ret;
987}
988
989static void map_groups__fixup_end(struct map_groups *mg)
990{
991 int i;
992 for (i = 0; i < MAP__NR_TYPES; ++i)
993 __map_groups__fixup_end(mg, i);
994}
995
996static char *get_kernel_version(const char *root_dir)
997{
998 char version[PATH_MAX];
999 FILE *file;
1000 char *name, *tmp;
1001 const char *prefix = "Linux version ";
1002
1003 sprintf(version, "%s/proc/version", root_dir);
1004 file = fopen(version, "r");
1005 if (!file)
1006 return NULL;
1007
1008 version[0] = '\0';
1009 tmp = fgets(version, sizeof(version), file);
1010 fclose(file);
1011
1012 name = strstr(version, prefix);
1013 if (!name)
1014 return NULL;
1015 name += strlen(prefix);
1016 tmp = strchr(name, ' ');
1017 if (tmp)
1018 *tmp = '\0';
1019
1020 return strdup(name);
1021}
1022
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001023static bool is_kmod_dso(struct dso *dso)
1024{
1025 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1026 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1027}
1028
1029static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1030 struct kmod_path *m)
1031{
1032 struct map *map;
1033 char *long_name;
1034
1035 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
1036 if (map == NULL)
1037 return 0;
1038
1039 long_name = strdup(path);
1040 if (long_name == NULL)
1041 return -ENOMEM;
1042
1043 dso__set_long_name(map->dso, long_name, true);
1044 dso__kernel_module_get_build_id(map->dso, "");
1045
1046 /*
1047 * Full name could reveal us kmod compression, so
1048 * we need to update the symtab_type if needed.
1049 */
1050 if (m->comp && is_kmod_dso(map->dso))
1051 map->dso->symtab_type++;
1052
1053 return 0;
1054}
1055
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001056static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -04001057 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001058{
1059 struct dirent *dent;
1060 DIR *dir = opendir(dir_name);
1061 int ret = 0;
1062
1063 if (!dir) {
1064 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1065 return -1;
1066 }
1067
1068 while ((dent = readdir(dir)) != NULL) {
1069 char path[PATH_MAX];
1070 struct stat st;
1071
1072 /*sshfs might return bad dent->d_type, so we have to stat*/
1073 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1074 if (stat(path, &st))
1075 continue;
1076
1077 if (S_ISDIR(st.st_mode)) {
1078 if (!strcmp(dent->d_name, ".") ||
1079 !strcmp(dent->d_name, ".."))
1080 continue;
1081
Richard Yao61d42902014-04-26 13:17:55 -04001082 /* Do not follow top-level source and build symlinks */
1083 if (depth == 0) {
1084 if (!strcmp(dent->d_name, "source") ||
1085 !strcmp(dent->d_name, "build"))
1086 continue;
1087 }
1088
1089 ret = map_groups__set_modules_path_dir(mg, path,
1090 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001091 if (ret < 0)
1092 goto out;
1093 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001094 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001095
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001096 ret = kmod_path__parse_name(&m, dent->d_name);
1097 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001098 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001099
1100 if (m.kmod)
1101 ret = map_groups__set_module_path(mg, path, &m);
1102
1103 free(m.name);
1104
1105 if (ret)
1106 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001107 }
1108 }
1109
1110out:
1111 closedir(dir);
1112 return ret;
1113}
1114
1115static int machine__set_modules_path(struct machine *machine)
1116{
1117 char *version;
1118 char modules_path[PATH_MAX];
1119
1120 version = get_kernel_version(machine->root_dir);
1121 if (!version)
1122 return -1;
1123
Richard Yao61d42902014-04-26 13:17:55 -04001124 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001125 machine->root_dir, version);
1126 free(version);
1127
Richard Yao61d42902014-04-26 13:17:55 -04001128 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001129}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001130int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1131 const char *name __maybe_unused)
1132{
1133 return 0;
1134}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001135
Adrian Hunter316d70d2013-10-08 11:45:48 +03001136static int machine__create_module(void *arg, const char *name, u64 start)
1137{
1138 struct machine *machine = arg;
1139 struct map *map;
1140
Song Shan Gong203d8a42016-07-21 11:10:51 +08001141 if (arch__fix_module_text_start(&start, name) < 0)
1142 return -1;
1143
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001144 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001145 if (map == NULL)
1146 return -1;
1147
1148 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1149
1150 return 0;
1151}
1152
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001153static int machine__create_modules(struct machine *machine)
1154{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001155 const char *modules;
1156 char path[PATH_MAX];
1157
Adrian Hunterf4be9042013-09-22 13:22:09 +03001158 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001159 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001160 } else {
1161 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001162 modules = path;
1163 }
1164
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001165 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001166 return -1;
1167
Adrian Hunter316d70d2013-10-08 11:45:48 +03001168 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001169 return -1;
1170
Adrian Hunter316d70d2013-10-08 11:45:48 +03001171 if (!machine__set_modules_path(machine))
1172 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001173
Adrian Hunter316d70d2013-10-08 11:45:48 +03001174 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001175
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001176 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001177}
1178
1179int machine__create_kernel_maps(struct machine *machine)
1180{
1181 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001182 const char *name;
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001183 u64 addr;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001184 int ret;
1185
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001186 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001187 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001188
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001189 ret = __machine__create_kernel_maps(machine, kernel);
1190 dso__put(kernel);
1191 if (ret < 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001192 return -1;
1193
1194 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1195 if (machine__is_host(machine))
1196 pr_debug("Problems creating module maps, "
1197 "continuing anyway...\n");
1198 else
1199 pr_debug("Problems creating module maps for guest %d, "
1200 "continuing anyway...\n", machine->pid);
1201 }
1202
1203 /*
1204 * Now that we have all the maps created, just set the ->end of them:
1205 */
1206 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001207
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001208 addr = machine__get_running_kernel_start(machine, &name);
1209 if (!addr) {
1210 } else if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) {
Adrian Hunter5512cf22014-01-29 16:14:39 +02001211 machine__destroy_kernel_maps(machine);
1212 return -1;
1213 }
1214
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001215 return 0;
1216}
1217
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001218static void machine__set_kernel_mmap_len(struct machine *machine,
1219 union perf_event *event)
1220{
Namhyung Kim4552cf02012-11-07 16:27:10 +09001221 int i;
1222
1223 for (i = 0; i < MAP__NR_TYPES; i++) {
1224 machine->vmlinux_maps[i]->start = event->mmap.start;
1225 machine->vmlinux_maps[i]->end = (event->mmap.start +
1226 event->mmap.len);
1227 /*
1228 * Be a bit paranoid here, some perf.data file came with
1229 * a zero sized synthesized MMAP event for the kernel.
1230 */
1231 if (machine->vmlinux_maps[i]->end == 0)
1232 machine->vmlinux_maps[i]->end = ~0ULL;
1233 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001234}
1235
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001236static bool machine__uses_kcore(struct machine *machine)
1237{
1238 struct dso *dso;
1239
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001240 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001241 if (dso__is_kcore(dso))
1242 return true;
1243 }
1244
1245 return false;
1246}
1247
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001248static int machine__process_kernel_mmap_event(struct machine *machine,
1249 union perf_event *event)
1250{
1251 struct map *map;
1252 char kmmap_prefix[PATH_MAX];
1253 enum dso_kernel_type kernel_type;
1254 bool is_kernel_mmap;
1255
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001256 /* If we have maps from kcore then we do not need or want any others */
1257 if (machine__uses_kcore(machine))
1258 return 0;
1259
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001260 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1261 if (machine__is_host(machine))
1262 kernel_type = DSO_TYPE_KERNEL;
1263 else
1264 kernel_type = DSO_TYPE_GUEST_KERNEL;
1265
1266 is_kernel_mmap = memcmp(event->mmap.filename,
1267 kmmap_prefix,
1268 strlen(kmmap_prefix) - 1) == 0;
1269 if (event->mmap.filename[0] == '/' ||
1270 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001271 map = machine__findnew_module_map(machine, event->mmap.start,
1272 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001273 if (map == NULL)
1274 goto out_problem;
1275
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001276 map->end = map->start + event->mmap.len;
1277 } else if (is_kernel_mmap) {
1278 const char *symbol_name = (event->mmap.filename +
1279 strlen(kmmap_prefix));
1280 /*
1281 * Should be there already, from the build-id table in
1282 * the header.
1283 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001284 struct dso *kernel = NULL;
1285 struct dso *dso;
1286
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001287 pthread_rwlock_rdlock(&machine->dsos.lock);
1288
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001289 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001290
1291 /*
1292 * The cpumode passed to is_kernel_module is not the
1293 * cpumode of *this* event. If we insist on passing
1294 * correct cpumode to is_kernel_module, we should
1295 * record the cpumode when we adding this dso to the
1296 * linked list.
1297 *
1298 * However we don't really need passing correct
1299 * cpumode. We know the correct cpumode must be kernel
1300 * mode (if not, we should not link it onto kernel_dsos
1301 * list).
1302 *
1303 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1304 * is_kernel_module() treats it as a kernel cpumode.
1305 */
1306
1307 if (!dso->kernel ||
1308 is_kernel_module(dso->long_name,
1309 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001310 continue;
1311
Wang Nan1f121b02015-06-03 08:52:21 +00001312
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001313 kernel = dso;
1314 break;
1315 }
1316
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001317 pthread_rwlock_unlock(&machine->dsos.lock);
1318
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001319 if (kernel == NULL)
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001320 kernel = machine__findnew_dso(machine, kmmap_prefix);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001321 if (kernel == NULL)
1322 goto out_problem;
1323
1324 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001325 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1326 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001327 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001328 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001329
Namhyung Kim330dfa22014-11-18 13:30:28 +09001330 if (strstr(kernel->long_name, "vmlinux"))
1331 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001332
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001333 machine__set_kernel_mmap_len(machine, event);
1334
1335 /*
1336 * Avoid using a zero address (kptr_restrict) for the ref reloc
1337 * symbol. Effectively having zero here means that at record
1338 * time /proc/sys/kernel/kptr_restrict was non zero.
1339 */
1340 if (event->mmap.pgoff != 0) {
1341 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1342 symbol_name,
1343 event->mmap.pgoff);
1344 }
1345
1346 if (machine__is_default_guest(machine)) {
1347 /*
1348 * preload dso of guest kernel and modules
1349 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001350 dso__load(kernel, machine__kernel_map(machine));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001351 }
1352 }
1353 return 0;
1354out_problem:
1355 return -1;
1356}
1357
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001358int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001359 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001360 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001361{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001362 struct thread *thread;
1363 struct map *map;
1364 enum map_type type;
1365 int ret = 0;
1366
1367 if (dump_trace)
1368 perf_event__fprintf_mmap2(event, stdout);
1369
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001370 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1371 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001372 ret = machine__process_kernel_mmap_event(machine, event);
1373 if (ret < 0)
1374 goto out_problem;
1375 return 0;
1376 }
1377
1378 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001379 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001380 if (thread == NULL)
1381 goto out_problem;
1382
1383 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1384 type = MAP__VARIABLE;
1385 else
1386 type = MAP__FUNCTION;
1387
Adrian Hunter2a030682014-07-22 16:17:53 +03001388 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001389 event->mmap2.len, event->mmap2.pgoff,
1390 event->mmap2.pid, event->mmap2.maj,
1391 event->mmap2.min, event->mmap2.ino,
1392 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001393 event->mmap2.prot,
1394 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001395 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001396
1397 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001398 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001399
He Kuang8132a2a2016-06-03 03:33:13 +00001400 ret = thread__insert_map(thread, map);
1401 if (ret)
1402 goto out_problem_insert;
1403
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001404 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001405 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001406 return 0;
1407
He Kuang8132a2a2016-06-03 03:33:13 +00001408out_problem_insert:
1409 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001410out_problem_map:
1411 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001412out_problem:
1413 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1414 return 0;
1415}
1416
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001417int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001418 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001419{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001420 struct thread *thread;
1421 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001422 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001423 int ret = 0;
1424
1425 if (dump_trace)
1426 perf_event__fprintf_mmap(event, stdout);
1427
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001428 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1429 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001430 ret = machine__process_kernel_mmap_event(machine, event);
1431 if (ret < 0)
1432 goto out_problem;
1433 return 0;
1434 }
1435
Adrian Hunter314add62013-08-27 11:23:03 +03001436 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001437 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001438 if (thread == NULL)
1439 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001440
1441 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1442 type = MAP__VARIABLE;
1443 else
1444 type = MAP__FUNCTION;
1445
Adrian Hunter2a030682014-07-22 16:17:53 +03001446 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001447 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001448 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001449 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001450 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001451
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001452 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001453 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001454
He Kuang8132a2a2016-06-03 03:33:13 +00001455 ret = thread__insert_map(thread, map);
1456 if (ret)
1457 goto out_problem_insert;
1458
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001459 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001460 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001461 return 0;
1462
He Kuang8132a2a2016-06-03 03:33:13 +00001463out_problem_insert:
1464 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001465out_problem_map:
1466 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001467out_problem:
1468 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1469 return 0;
1470}
1471
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001472static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001473{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001474 if (machine->last_match == th)
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -03001475 machine->last_match = NULL;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001476
Elena Reshetovae34f5b12017-02-21 17:35:02 +02001477 BUG_ON(refcount_read(&th->refcnt) == 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001478 if (lock)
1479 pthread_rwlock_wrlock(&machine->threads_lock);
Arnaldo Carvalho de Melo0170b142015-05-25 15:23:05 -03001480 rb_erase_init(&th->rb_node, &machine->threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001481 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -03001482 --machine->nr_threads;
David Ahern236a3bb2013-08-14 08:49:27 -06001483 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001484 * Move it first to the dead_threads list, then drop the reference,
1485 * if this is the last reference, then the thread__delete destructor
1486 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001487 */
1488 list_add_tail(&th->node, &machine->dead_threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001489 if (lock)
1490 pthread_rwlock_unlock(&machine->threads_lock);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001491 thread__put(th);
David Ahern236a3bb2013-08-14 08:49:27 -06001492}
1493
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001494void machine__remove_thread(struct machine *machine, struct thread *th)
1495{
1496 return __machine__remove_thread(machine, th, true);
1497}
1498
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001499int machine__process_fork_event(struct machine *machine, union perf_event *event,
1500 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001501{
Jiri Olsad75e6092014-03-14 15:00:03 +01001502 struct thread *thread = machine__find_thread(machine,
1503 event->fork.pid,
1504 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001505 struct thread *parent = machine__findnew_thread(machine,
1506 event->fork.ppid,
1507 event->fork.ptid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001508 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001509
Adrian Hunter5cb73342015-08-19 17:29:20 +03001510 if (dump_trace)
1511 perf_event__fprintf_task(event, stdout);
1512
1513 /*
1514 * There may be an existing thread that is not actually the parent,
1515 * either because we are processing events out of order, or because the
1516 * (fork) event that would have removed the thread was lost. Assume the
1517 * latter case and continue on as best we can.
1518 */
1519 if (parent->pid_ != (pid_t)event->fork.ppid) {
1520 dump_printf("removing erroneous parent thread %d/%d\n",
1521 parent->pid_, parent->tid);
1522 machine__remove_thread(machine, parent);
1523 thread__put(parent);
1524 parent = machine__findnew_thread(machine, event->fork.ppid,
1525 event->fork.ptid);
1526 }
1527
David Ahern236a3bb2013-08-14 08:49:27 -06001528 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001529 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001530 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001531 thread__put(thread);
1532 }
David Ahern236a3bb2013-08-14 08:49:27 -06001533
Adrian Hunter314add62013-08-27 11:23:03 +03001534 thread = machine__findnew_thread(machine, event->fork.pid,
1535 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001536
1537 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001538 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001539 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001540 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001541 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001542 thread__put(thread);
1543 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001544
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001545 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001546}
1547
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001548int machine__process_exit_event(struct machine *machine, union perf_event *event,
1549 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001550{
Jiri Olsad75e6092014-03-14 15:00:03 +01001551 struct thread *thread = machine__find_thread(machine,
1552 event->fork.pid,
1553 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001554
1555 if (dump_trace)
1556 perf_event__fprintf_task(event, stdout);
1557
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001558 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001559 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001560 thread__put(thread);
1561 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001562
1563 return 0;
1564}
1565
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001566int machine__process_event(struct machine *machine, union perf_event *event,
1567 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001568{
1569 int ret;
1570
1571 switch (event->header.type) {
1572 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001573 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001574 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001575 ret = machine__process_mmap_event(machine, event, sample); break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301576 case PERF_RECORD_NAMESPACES:
1577 ret = machine__process_namespaces_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001578 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001579 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001580 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001581 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001582 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001583 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001584 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001585 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001586 case PERF_RECORD_AUX:
1587 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001588 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001589 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001590 case PERF_RECORD_LOST_SAMPLES:
1591 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001592 case PERF_RECORD_SWITCH:
1593 case PERF_RECORD_SWITCH_CPU_WIDE:
1594 ret = machine__process_switch_event(machine, event); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001595 default:
1596 ret = -1;
1597 break;
1598 }
1599
1600 return ret;
1601}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001602
Greg Priceb21484f2012-12-06 21:48:05 -08001603static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001604{
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001605 if (!regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001606 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001607 return 0;
1608}
1609
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001610static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001611 struct addr_map_symbol *ams,
1612 u64 ip)
1613{
1614 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001615
1616 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001617 /*
1618 * We cannot use the header.misc hint to determine whether a
1619 * branch stack address is user, kernel, guest, hypervisor.
1620 * Branches may straddle the kernel/user/hypervisor boundaries.
1621 * Thus, we have to try consecutively until we find a match
1622 * or else, the symbol is unknown
1623 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001624 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001625
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001626 ams->addr = ip;
1627 ams->al_addr = al.addr;
1628 ams->sym = al.sym;
1629 ams->map = al.map;
1630}
1631
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001632static void ip__resolve_data(struct thread *thread,
Stephane Eranian98a3b322013-01-24 16:10:35 +01001633 u8 m, struct addr_map_symbol *ams, u64 addr)
1634{
1635 struct addr_location al;
1636
1637 memset(&al, 0, sizeof(al));
1638
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001639 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001640 if (al.map == NULL) {
1641 /*
1642 * some shared data regions have execute bit set which puts
1643 * their mapping in the MAP__FUNCTION type array.
1644 * Check there as a fallback option before dropping the sample.
1645 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001646 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001647 }
1648
Stephane Eranian98a3b322013-01-24 16:10:35 +01001649 ams->addr = addr;
1650 ams->al_addr = al.addr;
1651 ams->sym = al.sym;
1652 ams->map = al.map;
1653}
1654
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001655struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1656 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001657{
1658 struct mem_info *mi = zalloc(sizeof(*mi));
1659
1660 if (!mi)
1661 return NULL;
1662
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001663 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1664 ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001665 mi->data_src.val = sample->data_src;
1666
1667 return mi;
1668}
1669
Andi Kleen37592b82014-11-12 18:05:19 -08001670static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001671 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08001672 struct symbol **parent,
1673 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001674 u8 *cpumode,
Jin Yao410024d2016-10-31 09:19:49 +08001675 u64 ip,
1676 bool branch,
1677 struct branch_flags *flags,
1678 int nr_loop_iter,
1679 int samples)
Andi Kleen37592b82014-11-12 18:05:19 -08001680{
1681 struct addr_location al;
1682
1683 al.filtered = 0;
1684 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001685 if (!cpumode) {
Andi Kleen8b7bad52014-11-12 18:05:20 -08001686 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1687 ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001688 } else {
Kan Liang2e777842014-12-02 10:06:53 -05001689 if (ip >= PERF_CONTEXT_MAX) {
1690 switch (ip) {
1691 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001692 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05001693 break;
1694 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001695 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05001696 break;
1697 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001698 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05001699 break;
1700 default:
1701 pr_debug("invalid callchain context: "
1702 "%"PRId64"\n", (s64) ip);
1703 /*
1704 * It seems the callchain is corrupted.
1705 * Discard all.
1706 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001707 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05001708 return 1;
1709 }
1710 return 0;
1711 }
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001712 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1713 ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05001714 }
1715
Andi Kleen37592b82014-11-12 18:05:19 -08001716 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02001717 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08001718 symbol__match_regex(al.sym, &parent_regex))
1719 *parent = al.sym;
1720 else if (have_ignore_callees && root_al &&
1721 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1722 /* Treat this symbol as the root,
1723 forgetting its callees. */
1724 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001725 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08001726 }
1727 }
1728
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09001729 if (symbol_conf.hide_unresolved && al.sym == NULL)
1730 return 0;
Jin Yao410024d2016-10-31 09:19:49 +08001731 return callchain_cursor_append(cursor, al.addr, al.map, al.sym,
1732 branch, flags, nr_loop_iter, samples);
Andi Kleen37592b82014-11-12 18:05:19 -08001733}
1734
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001735struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1736 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001737{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001738 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001739 const struct branch_stack *bs = sample->branch_stack;
1740 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001741
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001742 if (!bi)
1743 return NULL;
1744
1745 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001746 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1747 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001748 bi[i].flags = bs->entries[i].flags;
1749 }
1750 return bi;
1751}
1752
Andi Kleen8b7bad52014-11-12 18:05:20 -08001753#define CHASHSZ 127
1754#define CHASHBITS 7
1755#define NO_ENTRY 0xff
1756
1757#define PERF_MAX_BRANCH_DEPTH 127
1758
1759/* Remove loops. */
1760static int remove_loops(struct branch_entry *l, int nr)
1761{
1762 int i, j, off;
1763 unsigned char chash[CHASHSZ];
1764
1765 memset(chash, NO_ENTRY, sizeof(chash));
1766
1767 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1768
1769 for (i = 0; i < nr; i++) {
1770 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1771
1772 /* no collision handling for now */
1773 if (chash[h] == NO_ENTRY) {
1774 chash[h] = i;
1775 } else if (l[chash[h]].from == l[i].from) {
1776 bool is_loop = true;
1777 /* check if it is a real loop */
1778 off = 0;
1779 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1780 if (l[j].from != l[i + off].from) {
1781 is_loop = false;
1782 break;
1783 }
1784 if (is_loop) {
1785 memmove(l + i, l + i + off,
1786 (nr - (i + off)) * sizeof(*l));
1787 nr -= off;
1788 }
1789 }
1790 }
1791 return nr;
1792}
1793
Kan Liang384b6052015-01-05 13:23:05 -05001794/*
1795 * Recolve LBR callstack chain sample
1796 * Return:
1797 * 1 on success get LBR callchain information
1798 * 0 no available LBR callchain information, should try fp
1799 * negative error code on other errors.
1800 */
1801static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001802 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001803 struct perf_sample *sample,
1804 struct symbol **parent,
1805 struct addr_location *root_al,
1806 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001807{
Kan Liang384b6052015-01-05 13:23:05 -05001808 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001809 int chain_nr = min(max_stack, (int)chain->nr), i;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001810 u8 cpumode = PERF_RECORD_MISC_USER;
Kan Liang384b6052015-01-05 13:23:05 -05001811 u64 ip;
1812
1813 for (i = 0; i < chain_nr; i++) {
1814 if (chain->ips[i] == PERF_CONTEXT_USER)
1815 break;
1816 }
1817
1818 /* LBR only affects the user callchain */
1819 if (i != chain_nr) {
1820 struct branch_stack *lbr_stack = sample->branch_stack;
Jin Yao410024d2016-10-31 09:19:49 +08001821 int lbr_nr = lbr_stack->nr, j, k;
1822 bool branch;
1823 struct branch_flags *flags;
Kan Liang384b6052015-01-05 13:23:05 -05001824 /*
1825 * LBR callstack can only get user call chain.
1826 * The mix_chain_nr is kernel call chain
1827 * number plus LBR user call chain number.
1828 * i is kernel call chain number,
1829 * 1 is PERF_CONTEXT_USER,
1830 * lbr_nr + 1 is the user call chain number.
1831 * For details, please refer to the comments
1832 * in callchain__printf
1833 */
1834 int mix_chain_nr = i + 1 + lbr_nr + 1;
1835
Kan Liang384b6052015-01-05 13:23:05 -05001836 for (j = 0; j < mix_chain_nr; j++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001837 int err;
Jin Yao410024d2016-10-31 09:19:49 +08001838 branch = false;
1839 flags = NULL;
1840
Kan Liang384b6052015-01-05 13:23:05 -05001841 if (callchain_param.order == ORDER_CALLEE) {
1842 if (j < i + 1)
1843 ip = chain->ips[j];
Jin Yao410024d2016-10-31 09:19:49 +08001844 else if (j > i + 1) {
1845 k = j - i - 2;
1846 ip = lbr_stack->entries[k].from;
1847 branch = true;
1848 flags = &lbr_stack->entries[k].flags;
1849 } else {
Kan Liang384b6052015-01-05 13:23:05 -05001850 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08001851 branch = true;
1852 flags = &lbr_stack->entries[0].flags;
1853 }
Kan Liang384b6052015-01-05 13:23:05 -05001854 } else {
Jin Yao410024d2016-10-31 09:19:49 +08001855 if (j < lbr_nr) {
1856 k = lbr_nr - j - 1;
1857 ip = lbr_stack->entries[k].from;
1858 branch = true;
1859 flags = &lbr_stack->entries[k].flags;
1860 }
Kan Liang384b6052015-01-05 13:23:05 -05001861 else if (j > lbr_nr)
1862 ip = chain->ips[i + 1 - (j - lbr_nr)];
Jin Yao410024d2016-10-31 09:19:49 +08001863 else {
Kan Liang384b6052015-01-05 13:23:05 -05001864 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08001865 branch = true;
1866 flags = &lbr_stack->entries[0].flags;
1867 }
Kan Liang384b6052015-01-05 13:23:05 -05001868 }
1869
Jin Yao410024d2016-10-31 09:19:49 +08001870 err = add_callchain_ip(thread, cursor, parent,
1871 root_al, &cpumode, ip,
1872 branch, flags, 0, 0);
Kan Liang384b6052015-01-05 13:23:05 -05001873 if (err)
1874 return (err < 0) ? err : 0;
1875 }
1876 return 1;
1877 }
1878
1879 return 0;
1880}
1881
1882static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001883 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001884 struct perf_evsel *evsel,
1885 struct perf_sample *sample,
1886 struct symbol **parent,
1887 struct addr_location *root_al,
1888 int max_stack)
1889{
1890 struct branch_stack *branch = sample->branch_stack;
1891 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03001892 int chain_nr = chain->nr;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001893 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001894 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08001895 int skip_idx = -1;
1896 int first_call = 0;
Jin Yao410024d2016-10-31 09:19:49 +08001897 int nr_loop_iter;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001898
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03001899 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001900 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05001901 root_al, max_stack);
1902 if (err)
1903 return (err < 0) ? err : 0;
1904 }
1905
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001906 /*
1907 * Based on DWARF debug information, some architectures skip
1908 * a callchain entry saved by the kernel.
1909 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001910 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001911
Andi Kleen8b7bad52014-11-12 18:05:20 -08001912 /*
1913 * Add branches to call stack for easier browsing. This gives
1914 * more context for a sample than just the callers.
1915 *
1916 * This uses individual histograms of paths compared to the
1917 * aggregated histograms the normal LBR mode uses.
1918 *
1919 * Limitations for now:
1920 * - No extra filters
1921 * - No annotations (should annotate somehow)
1922 */
1923
1924 if (branch && callchain_param.branch_callstack) {
1925 int nr = min(max_stack, (int)branch->nr);
1926 struct branch_entry be[nr];
1927
1928 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
1929 pr_warning("corrupted branch chain. skipping...\n");
1930 goto check_calls;
1931 }
1932
1933 for (i = 0; i < nr; i++) {
1934 if (callchain_param.order == ORDER_CALLEE) {
1935 be[i] = branch->entries[i];
1936 /*
1937 * Check for overlap into the callchain.
1938 * The return address is one off compared to
1939 * the branch entry. To adjust for this
1940 * assume the calling instruction is not longer
1941 * than 8 bytes.
1942 */
1943 if (i == skip_idx ||
1944 chain->ips[first_call] >= PERF_CONTEXT_MAX)
1945 first_call++;
1946 else if (be[i].from < chain->ips[first_call] &&
1947 be[i].from >= chain->ips[first_call] - 8)
1948 first_call++;
1949 } else
1950 be[i] = branch->entries[branch->nr - i - 1];
1951 }
1952
Jin Yao410024d2016-10-31 09:19:49 +08001953 nr_loop_iter = nr;
Andi Kleen8b7bad52014-11-12 18:05:20 -08001954 nr = remove_loops(be, nr);
1955
Jin Yao410024d2016-10-31 09:19:49 +08001956 /*
1957 * Get the number of iterations.
1958 * It's only approximation, but good enough in practice.
1959 */
1960 if (nr_loop_iter > nr)
1961 nr_loop_iter = nr_loop_iter - nr + 1;
1962 else
1963 nr_loop_iter = 0;
1964
Andi Kleen8b7bad52014-11-12 18:05:20 -08001965 for (i = 0; i < nr; i++) {
Jin Yao410024d2016-10-31 09:19:49 +08001966 if (i == nr - 1)
1967 err = add_callchain_ip(thread, cursor, parent,
1968 root_al,
1969 NULL, be[i].to,
1970 true, &be[i].flags,
1971 nr_loop_iter, 1);
1972 else
1973 err = add_callchain_ip(thread, cursor, parent,
1974 root_al,
1975 NULL, be[i].to,
1976 true, &be[i].flags,
1977 0, 0);
1978
Andi Kleen8b7bad52014-11-12 18:05:20 -08001979 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001980 err = add_callchain_ip(thread, cursor, parent, root_al,
Jin Yao410024d2016-10-31 09:19:49 +08001981 NULL, be[i].from,
1982 true, &be[i].flags,
1983 0, 0);
Andi Kleen8b7bad52014-11-12 18:05:20 -08001984 if (err == -EINVAL)
1985 break;
1986 if (err)
1987 return err;
1988 }
1989 chain_nr -= nr;
1990 }
1991
1992check_calls:
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001993 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03001994 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001995 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001996
1997 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001998 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001999 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002000 j = chain->nr - i - 1;
2001
2002#ifdef HAVE_SKIP_CALLCHAIN_IDX
2003 if (j == skip_idx)
2004 continue;
2005#endif
2006 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002007
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002008 if (ip < PERF_CONTEXT_MAX)
2009 ++nr_entries;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002010
Jin Yao410024d2016-10-31 09:19:49 +08002011 err = add_callchain_ip(thread, cursor, parent,
2012 root_al, &cpumode, ip,
2013 false, NULL, 0, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002014
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002015 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05002016 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002017 }
2018
2019 return 0;
2020}
2021
2022static int unwind_entry(struct unwind_entry *entry, void *arg)
2023{
2024 struct callchain_cursor *cursor = arg;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002025
2026 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2027 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002028 return callchain_cursor_append(cursor, entry->ip,
Jin Yao410024d2016-10-31 09:19:49 +08002029 entry->map, entry->sym,
2030 false, NULL, 0, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002031}
2032
Chris Phlipot9919a652016-04-28 01:19:06 -07002033static int thread__resolve_callchain_unwind(struct thread *thread,
2034 struct callchain_cursor *cursor,
2035 struct perf_evsel *evsel,
2036 struct perf_sample *sample,
2037 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002038{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002039 /* Can we do dwarf post unwind? */
2040 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2041 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
2042 return 0;
2043
2044 /* Bail out if nothing was captured. */
2045 if ((!sample->user_regs.regs) ||
2046 (!sample->user_stack.size))
2047 return 0;
2048
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002049 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01002050 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07002051}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002052
Chris Phlipot9919a652016-04-28 01:19:06 -07002053int thread__resolve_callchain(struct thread *thread,
2054 struct callchain_cursor *cursor,
2055 struct perf_evsel *evsel,
2056 struct perf_sample *sample,
2057 struct symbol **parent,
2058 struct addr_location *root_al,
2059 int max_stack)
2060{
2061 int ret = 0;
2062
2063 callchain_cursor_reset(&callchain_cursor);
2064
2065 if (callchain_param.order == ORDER_CALLEE) {
2066 ret = thread__resolve_callchain_sample(thread, cursor,
2067 evsel, sample,
2068 parent, root_al,
2069 max_stack);
2070 if (ret)
2071 return ret;
2072 ret = thread__resolve_callchain_unwind(thread, cursor,
2073 evsel, sample,
2074 max_stack);
2075 } else {
2076 ret = thread__resolve_callchain_unwind(thread, cursor,
2077 evsel, sample,
2078 max_stack);
2079 if (ret)
2080 return ret;
2081 ret = thread__resolve_callchain_sample(thread, cursor,
2082 evsel, sample,
2083 parent, root_al,
2084 max_stack);
2085 }
2086
2087 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002088}
David Ahern35feee12013-09-28 13:12:58 -06002089
2090int machine__for_each_thread(struct machine *machine,
2091 int (*fn)(struct thread *thread, void *p),
2092 void *priv)
2093{
2094 struct rb_node *nd;
2095 struct thread *thread;
2096 int rc = 0;
2097
2098 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
2099 thread = rb_entry(nd, struct thread, rb_node);
2100 rc = fn(thread, priv);
2101 if (rc != 0)
2102 return rc;
2103 }
2104
2105 list_for_each_entry(thread, &machine->dead_threads, node) {
2106 rc = fn(thread, priv);
2107 if (rc != 0)
2108 return rc;
2109 }
2110 return rc;
2111}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002112
Adrian Huntera5499b32015-05-29 16:33:30 +03002113int machines__for_each_thread(struct machines *machines,
2114 int (*fn)(struct thread *thread, void *p),
2115 void *priv)
2116{
2117 struct rb_node *nd;
2118 int rc = 0;
2119
2120 rc = machine__for_each_thread(&machines->host, fn, priv);
2121 if (rc != 0)
2122 return rc;
2123
2124 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
2125 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2126
2127 rc = machine__for_each_thread(machine, fn, priv);
2128 if (rc != 0)
2129 return rc;
2130 }
2131 return rc;
2132}
2133
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03002134int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002135 struct target *target, struct thread_map *threads,
Kan Liang9d9cad72015-06-17 09:51:11 -04002136 perf_event__handler_t process, bool data_mmap,
2137 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002138{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002139 if (target__has_task(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002140 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002141 else if (target__has_cpu(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002142 return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002143 /* command specified */
2144 return 0;
2145}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002146
2147pid_t machine__get_current_tid(struct machine *machine, int cpu)
2148{
2149 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2150 return -1;
2151
2152 return machine->current_tid[cpu];
2153}
2154
2155int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2156 pid_t tid)
2157{
2158 struct thread *thread;
2159
2160 if (cpu < 0)
2161 return -EINVAL;
2162
2163 if (!machine->current_tid) {
2164 int i;
2165
2166 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2167 if (!machine->current_tid)
2168 return -ENOMEM;
2169 for (i = 0; i < MAX_NR_CPUS; i++)
2170 machine->current_tid[i] = -1;
2171 }
2172
2173 if (cpu >= MAX_NR_CPUS) {
2174 pr_err("Requested CPU %d too large. ", cpu);
2175 pr_err("Consider raising MAX_NR_CPUS\n");
2176 return -EINVAL;
2177 }
2178
2179 machine->current_tid[cpu] = tid;
2180
2181 thread = machine__findnew_thread(machine, pid, tid);
2182 if (!thread)
2183 return -ENOMEM;
2184
2185 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002186 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002187
2188 return 0;
2189}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002190
2191int machine__get_kernel_start(struct machine *machine)
2192{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002193 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002194 int err = 0;
2195
2196 /*
2197 * The only addresses above 2^63 are kernel addresses of a 64-bit
2198 * kernel. Note that addresses are unsigned so that on a 32-bit system
2199 * all addresses including kernel addresses are less than 2^32. In
2200 * that case (32-bit system), if the kernel mapping is unknown, all
2201 * addresses will be assumed to be in user space - see
2202 * machine__kernel_ip().
2203 */
2204 machine->kernel_start = 1ULL << 63;
2205 if (map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002206 err = map__load(map);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002207 if (map->start)
2208 machine->kernel_start = map->start;
2209 }
2210 return err;
2211}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002212
2213struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2214{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03002215 return dsos__findnew(&machine->dsos, filename);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002216}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002217
2218char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2219{
2220 struct machine *machine = vmachine;
2221 struct map *map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002222 struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002223
2224 if (sym == NULL)
2225 return NULL;
2226
2227 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2228 *addrp = map->unmap_ip(map, sym->start);
2229 return sym->name;
2230}