blob: 988e84ce6f8847425bc45dd13603233104d12e0e [file] [log] [blame]
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -03001#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Arnaldo Carvalho de Melo1eae20c2017-04-18 12:33:30 -03004#include <regex.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03005#include "callchain.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03006#include "debug.h"
7#include "event.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03008#include "evsel.h"
9#include "hist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030010#include "machine.h"
11#include "map.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030012#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030013#include "strlist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030014#include "thread.h"
Adrian Hunterd027b642014-07-23 14:23:00 +030015#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030016#include <stdbool.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030017#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080018#include "linux/hash.h"
Hari Bathinif3b36142017-03-08 02:11:43 +053019#include "asm/bug.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030020
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030021#include "sane_ctype.h"
22#include <symbol/kallsyms.h>
23
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030024static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
25
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030026static void dsos__init(struct dsos *dsos)
27{
28 INIT_LIST_HEAD(&dsos->head);
29 dsos->root = RB_ROOT;
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -030030 pthread_rwlock_init(&dsos->lock, NULL);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030031}
32
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030033int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
34{
Wang Nan93b0ba32015-12-08 02:25:44 +000035 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030036 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030037 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030038 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030039
40 machine->threads = RB_ROOT;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030041 pthread_rwlock_init(&machine->threads_lock, NULL);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -030042 machine->nr_threads = 0;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030043 INIT_LIST_HEAD(&machine->dead_threads);
44 machine->last_match = NULL;
45
Adrian Hunterd027b642014-07-23 14:23:00 +030046 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030047 machine->env = NULL;
Adrian Hunterd027b642014-07-23 14:23:00 +030048
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030049 machine->pid = pid;
50
Jiri Olsa14bd6d22014-01-07 13:47:19 +010051 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -030052 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +030053 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030054 machine->kernel_start = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030055
Masami Hiramatsucc1121a2015-12-09 11:11:33 +090056 memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps));
57
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030058 machine->root_dir = strdup(root_dir);
59 if (machine->root_dir == NULL)
60 return -ENOMEM;
61
62 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030063 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030064 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030065 char comm[64];
66
67 if (thread == NULL)
68 return -ENOMEM;
69
70 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020071 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030072 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030073 }
74
Adrian Hunterb9d266b2014-07-22 16:17:25 +030075 machine->current_tid = NULL;
76
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030077 return 0;
78}
79
David Ahern8fb598e2013-09-28 13:13:00 -060080struct machine *machine__new_host(void)
81{
82 struct machine *machine = malloc(sizeof(*machine));
83
84 if (machine != NULL) {
85 machine__init(machine, "", HOST_KERNEL_ID);
86
87 if (machine__create_kernel_maps(machine) < 0)
88 goto out_delete;
89 }
90
91 return machine;
92out_delete:
93 free(machine);
94 return NULL;
95}
96
Arnaldo Carvalho de Melo7d132ca2017-01-05 15:31:48 -030097struct machine *machine__new_kallsyms(void)
98{
99 struct machine *machine = machine__new_host();
100 /*
101 * FIXME:
102 * 1) MAP__FUNCTION will go away when we stop loading separate maps for
103 * functions and data objects.
104 * 2) We should switch to machine__load_kallsyms(), i.e. not explicitely
105 * ask for not using the kcore parsing code, once this one is fixed
106 * to create a map per module.
107 */
108 if (machine && __machine__load_kallsyms(machine, "/proc/kallsyms", MAP__FUNCTION, true) <= 0) {
109 machine__delete(machine);
110 machine = NULL;
111 }
112
113 return machine;
114}
115
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300116static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300117{
118 struct dso *pos, *n;
119
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300120 pthread_rwlock_wrlock(&dsos->lock);
121
Waiman Long8fa7d872014-09-29 16:07:28 -0400122 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -0400123 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +0200124 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300125 list_del_init(&pos->node);
126 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300127 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300128
129 pthread_rwlock_unlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300130}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300131
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300132static void dsos__exit(struct dsos *dsos)
133{
134 dsos__purge(dsos);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300135 pthread_rwlock_destroy(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300136}
137
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300138void machine__delete_threads(struct machine *machine)
139{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300140 struct rb_node *nd;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300141
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300142 pthread_rwlock_wrlock(&machine->threads_lock);
143 nd = rb_first(&machine->threads);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300144 while (nd) {
145 struct thread *t = rb_entry(nd, struct thread, rb_node);
146
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300147 nd = rb_next(nd);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300148 __machine__remove_thread(machine, t, false);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300149 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300150 pthread_rwlock_unlock(&machine->threads_lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300151}
152
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300153void machine__exit(struct machine *machine)
154{
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900155 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300156 map_groups__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300157 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300158 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300159 zfree(&machine->root_dir);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300160 zfree(&machine->current_tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300161 pthread_rwlock_destroy(&machine->threads_lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300162}
163
164void machine__delete(struct machine *machine)
165{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300166 if (machine) {
167 machine__exit(machine);
168 free(machine);
169 }
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300170}
171
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300172void machines__init(struct machines *machines)
173{
174 machine__init(&machines->host, "", HOST_KERNEL_ID);
175 machines->guests = RB_ROOT;
176}
177
178void machines__exit(struct machines *machines)
179{
180 machine__exit(&machines->host);
181 /* XXX exit guest */
182}
183
184struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300185 const char *root_dir)
186{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300187 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300188 struct rb_node *parent = NULL;
189 struct machine *pos, *machine = malloc(sizeof(*machine));
190
191 if (machine == NULL)
192 return NULL;
193
194 if (machine__init(machine, root_dir, pid) != 0) {
195 free(machine);
196 return NULL;
197 }
198
199 while (*p != NULL) {
200 parent = *p;
201 pos = rb_entry(parent, struct machine, rb_node);
202 if (pid < pos->pid)
203 p = &(*p)->rb_left;
204 else
205 p = &(*p)->rb_right;
206 }
207
208 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300209 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300210
211 return machine;
212}
213
Adrian Huntercfe1c412014-07-31 09:00:45 +0300214void machines__set_comm_exec(struct machines *machines, bool comm_exec)
215{
216 struct rb_node *nd;
217
218 machines->host.comm_exec = comm_exec;
219
220 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
221 struct machine *machine = rb_entry(nd, struct machine, rb_node);
222
223 machine->comm_exec = comm_exec;
224 }
225}
226
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300227struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300228{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300229 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300230 struct rb_node *parent = NULL;
231 struct machine *machine;
232 struct machine *default_machine = NULL;
233
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300234 if (pid == HOST_KERNEL_ID)
235 return &machines->host;
236
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300237 while (*p != NULL) {
238 parent = *p;
239 machine = rb_entry(parent, struct machine, rb_node);
240 if (pid < machine->pid)
241 p = &(*p)->rb_left;
242 else if (pid > machine->pid)
243 p = &(*p)->rb_right;
244 else
245 return machine;
246 if (!machine->pid)
247 default_machine = machine;
248 }
249
250 return default_machine;
251}
252
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300253struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300254{
255 char path[PATH_MAX];
256 const char *root_dir = "";
257 struct machine *machine = machines__find(machines, pid);
258
259 if (machine && (machine->pid == pid))
260 goto out;
261
262 if ((pid != HOST_KERNEL_ID) &&
263 (pid != DEFAULT_GUEST_KERNEL_ID) &&
264 (symbol_conf.guestmount)) {
265 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
266 if (access(path, R_OK)) {
267 static struct strlist *seen;
268
269 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300270 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300271
272 if (!strlist__has_entry(seen, path)) {
273 pr_err("Can't access file %s\n", path);
274 strlist__add(seen, path);
275 }
276 machine = NULL;
277 goto out;
278 }
279 root_dir = path;
280 }
281
282 machine = machines__add(machines, pid, root_dir);
283out:
284 return machine;
285}
286
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300287void machines__process_guests(struct machines *machines,
288 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300289{
290 struct rb_node *nd;
291
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300292 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300293 struct machine *pos = rb_entry(nd, struct machine, rb_node);
294 process(pos, data);
295 }
296}
297
298char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
299{
300 if (machine__is_host(machine))
301 snprintf(bf, size, "[%s]", "kernel.kallsyms");
302 else if (machine__is_default_guest(machine))
303 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
304 else {
305 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
306 machine->pid);
307 }
308
309 return bf;
310}
311
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300312void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300313{
314 struct rb_node *node;
315 struct machine *machine;
316
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300317 machines->host.id_hdr_size = id_hdr_size;
318
319 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300320 machine = rb_entry(node, struct machine, rb_node);
321 machine->id_hdr_size = id_hdr_size;
322 }
323
324 return;
325}
326
Adrian Hunter29ce3612014-07-16 11:07:13 +0300327static void machine__update_thread_pid(struct machine *machine,
328 struct thread *th, pid_t pid)
329{
330 struct thread *leader;
331
332 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
333 return;
334
335 th->pid_ = pid;
336
337 if (th->pid_ == th->tid)
338 return;
339
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300340 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300341 if (!leader)
342 goto out_err;
343
344 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300345 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300346
347 if (!leader->mg)
348 goto out_err;
349
350 if (th->mg == leader->mg)
351 return;
352
353 if (th->mg) {
354 /*
355 * Maps are created from MMAP events which provide the pid and
356 * tid. Consequently there never should be any maps on a thread
357 * with an unknown pid. Just print an error if there are.
358 */
359 if (!map_groups__empty(th->mg))
360 pr_err("Discarding thread maps for %d:%d\n",
361 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300362 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300363 }
364
365 th->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300366out_put:
367 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300368 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300369out_err:
370 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300371 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300372}
373
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300374/*
Adam Buchbinderbd1a0be2016-02-24 10:02:25 -0800375 * Caller must eventually drop thread->refcnt returned with a successful
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300376 * lookup/new thread inserted.
377 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300378static struct thread *____machine__findnew_thread(struct machine *machine,
379 pid_t pid, pid_t tid,
380 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300381{
382 struct rb_node **p = &machine->threads.rb_node;
383 struct rb_node *parent = NULL;
384 struct thread *th;
385
386 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300387 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300388 * so most of the time we dont have to look up
389 * the full rbtree:
390 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300391 th = machine->last_match;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300392 if (th != NULL) {
393 if (th->tid == tid) {
394 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300395 return thread__get(th);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300396 }
397
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300398 machine->last_match = NULL;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300399 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300400
401 while (*p != NULL) {
402 parent = *p;
403 th = rb_entry(parent, struct thread, rb_node);
404
Adrian Hunter38051232013-07-04 16:20:31 +0300405 if (th->tid == tid) {
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300406 machine->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300407 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300408 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300409 }
410
Adrian Hunter38051232013-07-04 16:20:31 +0300411 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300412 p = &(*p)->rb_left;
413 else
414 p = &(*p)->rb_right;
415 }
416
417 if (!create)
418 return NULL;
419
Adrian Hunter99d725f2013-08-26 16:00:19 +0300420 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300421 if (th != NULL) {
422 rb_link_node(&th->rb_node, parent, p);
423 rb_insert_color(&th->rb_node, &machine->threads);
Jiri Olsacddcef62014-04-09 20:54:29 +0200424
425 /*
426 * We have to initialize map_groups separately
427 * after rb tree is updated.
428 *
429 * The reason is that we call machine__findnew_thread
430 * within thread__init_map_groups to find the thread
431 * leader and that would screwed the rb tree.
432 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300433 if (thread__init_map_groups(th, machine)) {
Arnaldo Carvalho de Melo0170b142015-05-25 15:23:05 -0300434 rb_erase_init(&th->rb_node, &machine->threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300435 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300436 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200437 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300438 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300439 /*
440 * It is now in the rbtree, get a ref
441 */
442 thread__get(th);
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300443 machine->last_match = th;
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300444 ++machine->nr_threads;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300445 }
446
447 return th;
448}
449
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300450struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
451{
452 return ____machine__findnew_thread(machine, pid, tid, true);
453}
454
Adrian Hunter314add62013-08-27 11:23:03 +0300455struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
456 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300457{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300458 struct thread *th;
459
460 pthread_rwlock_wrlock(&machine->threads_lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300461 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300462 pthread_rwlock_unlock(&machine->threads_lock);
463 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300464}
465
Jiri Olsad75e6092014-03-14 15:00:03 +0100466struct thread *machine__find_thread(struct machine *machine, pid_t pid,
467 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300468{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300469 struct thread *th;
470 pthread_rwlock_rdlock(&machine->threads_lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300471 th = ____machine__findnew_thread(machine, pid, tid, false);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300472 pthread_rwlock_unlock(&machine->threads_lock);
473 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300474}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300475
Adrian Huntercfe1c412014-07-31 09:00:45 +0300476struct comm *machine__thread_exec_comm(struct machine *machine,
477 struct thread *thread)
478{
479 if (machine->comm_exec)
480 return thread__exec_comm(thread);
481 else
482 return thread__comm(thread);
483}
484
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200485int machine__process_comm_event(struct machine *machine, union perf_event *event,
486 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300487{
Adrian Hunter314add62013-08-27 11:23:03 +0300488 struct thread *thread = machine__findnew_thread(machine,
489 event->comm.pid,
490 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300491 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300492 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300493
Adrian Huntercfe1c412014-07-31 09:00:45 +0300494 if (exec)
495 machine->comm_exec = true;
496
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300497 if (dump_trace)
498 perf_event__fprintf_comm(event, stdout);
499
Adrian Hunter65de51f2014-07-31 09:00:44 +0300500 if (thread == NULL ||
501 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300502 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300503 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300504 }
505
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300506 thread__put(thread);
507
508 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300509}
510
Hari Bathinif3b36142017-03-08 02:11:43 +0530511int machine__process_namespaces_event(struct machine *machine __maybe_unused,
512 union perf_event *event,
513 struct perf_sample *sample __maybe_unused)
514{
515 struct thread *thread = machine__findnew_thread(machine,
516 event->namespaces.pid,
517 event->namespaces.tid);
518 int err = 0;
519
520 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
521 "\nWARNING: kernel seems to support more namespaces than perf"
522 " tool.\nTry updating the perf tool..\n\n");
523
524 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
525 "\nWARNING: perf tool seems to support more namespaces than"
526 " the kernel.\nTry updating the kernel..\n\n");
527
528 if (dump_trace)
529 perf_event__fprintf_namespaces(event, stdout);
530
531 if (thread == NULL ||
532 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
533 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
534 err = -1;
535 }
536
537 thread__put(thread);
538
539 return err;
540}
541
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300542int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200543 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300544{
545 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
546 event->lost.id, event->lost.lost);
547 return 0;
548}
549
Kan Liangc4937a92015-05-10 15:13:15 -0400550int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
551 union perf_event *event, struct perf_sample *sample)
552{
553 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
554 sample->id, event->lost_samples.lost);
555 return 0;
556}
557
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300558static struct dso *machine__findnew_module_dso(struct machine *machine,
559 struct kmod_path *m,
560 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100561{
562 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100563
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300564 pthread_rwlock_wrlock(&machine->dsos.lock);
565
566 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100567 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300568 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100569 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300570 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100571
572 if (machine__is_host(machine))
573 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
574 else
575 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
576
577 /* _KMODULE_COMP should be next to _KMODULE */
Jiri Olsaca333802015-02-17 17:29:57 +0100578 if (m->kmod && m->comp)
Jiri Olsada17ea32015-02-12 22:10:52 +0100579 dso->symtab_type++;
Jiri Olsaca333802015-02-17 17:29:57 +0100580
581 dso__set_short_name(dso, strdup(m->name), true);
582 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100583 }
584
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300585 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300586out_unlock:
587 pthread_rwlock_unlock(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100588 return dso;
589}
590
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300591int machine__process_aux_event(struct machine *machine __maybe_unused,
592 union perf_event *event)
593{
594 if (dump_trace)
595 perf_event__fprintf_aux(event, stdout);
596 return 0;
597}
598
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300599int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
600 union perf_event *event)
601{
602 if (dump_trace)
603 perf_event__fprintf_itrace_start(event, stdout);
604 return 0;
605}
606
Adrian Hunter02860392015-07-21 12:44:03 +0300607int machine__process_switch_event(struct machine *machine __maybe_unused,
608 union perf_event *event)
609{
610 if (dump_trace)
611 perf_event__fprintf_switch(event, stdout);
612 return 0;
613}
614
Wang Nanc03d5182015-11-26 03:59:57 +0000615static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
616{
617 const char *dup_filename;
618
619 if (!filename || !dso || !dso->long_name)
620 return;
621 if (dso->long_name[0] != '[')
622 return;
623 if (!strchr(filename, '/'))
624 return;
625
626 dup_filename = strdup(filename);
627 if (!dup_filename)
628 return;
629
Wang Nan5dcf16d2015-12-07 02:36:25 +0000630 dso__set_long_name(dso, dup_filename, true);
Wang Nanc03d5182015-11-26 03:59:57 +0000631}
632
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300633struct map *machine__findnew_module_map(struct machine *machine, u64 start,
634 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300635{
Jiri Olsaca333802015-02-17 17:29:57 +0100636 struct map *map = NULL;
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900637 struct dso *dso = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100638 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300639
Jiri Olsaca333802015-02-17 17:29:57 +0100640 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300641 return NULL;
642
Jiri Olsabc84f462015-02-17 17:31:18 +0100643 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
644 m.name);
Wang Nanc03d5182015-11-26 03:59:57 +0000645 if (map) {
646 /*
647 * If the map's dso is an offline module, give dso__load()
648 * a chance to find the file path of that module by fixing
649 * long_name.
650 */
651 dso__adjust_kmod_long_name(map->dso, filename);
Jiri Olsabc84f462015-02-17 17:31:18 +0100652 goto out;
Wang Nanc03d5182015-11-26 03:59:57 +0000653 }
Jiri Olsabc84f462015-02-17 17:31:18 +0100654
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300655 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100656 if (dso == NULL)
657 goto out;
658
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300659 map = map__new2(start, dso, MAP__FUNCTION);
660 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100661 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300662
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300663 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100664
Masami Hiramatsu9afcb422015-11-18 15:40:20 +0900665 /* Put the map here because map_groups__insert alread got it */
666 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100667out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900668 /* put the dso here, corresponding to machine__findnew_module_dso */
669 dso__put(dso);
Jiri Olsaca333802015-02-17 17:29:57 +0100670 free(m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300671 return map;
672}
673
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300674size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300675{
676 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300677 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300678
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300679 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300680 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300681 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300682 }
683
684 return ret;
685}
686
Waiman Long8fa7d872014-09-29 16:07:28 -0400687size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300688 bool (skip)(struct dso *dso, int parm), int parm)
689{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300690 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300691}
692
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300693size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300694 bool (skip)(struct dso *dso, int parm), int parm)
695{
696 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300697 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300698
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300699 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300700 struct machine *pos = rb_entry(nd, struct machine, rb_node);
701 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
702 }
703 return ret;
704}
705
706size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
707{
708 int i;
709 size_t printed = 0;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300710 struct dso *kdso = machine__kernel_map(machine)->dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300711
712 if (kdso->has_build_id) {
713 char filename[PATH_MAX];
714 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
715 printed += fprintf(fp, "[0] %s\n", filename);
716 }
717
718 for (i = 0; i < vmlinux_path__nr_entries; ++i)
719 printed += fprintf(fp, "[%d] %s\n",
720 i + kdso->has_build_id, vmlinux_path[i]);
721
722 return printed;
723}
724
725size_t machine__fprintf(struct machine *machine, FILE *fp)
726{
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300727 size_t ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300728 struct rb_node *nd;
729
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300730 pthread_rwlock_rdlock(&machine->threads_lock);
731
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300732 ret = fprintf(fp, "Threads: %u\n", machine->nr_threads);
733
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300734 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
735 struct thread *pos = rb_entry(nd, struct thread, rb_node);
736
737 ret += thread__fprintf(pos, fp);
738 }
739
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300740 pthread_rwlock_unlock(&machine->threads_lock);
741
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300742 return ret;
743}
744
745static struct dso *machine__get_kernel(struct machine *machine)
746{
747 const char *vmlinux_name = NULL;
748 struct dso *kernel;
749
750 if (machine__is_host(machine)) {
751 vmlinux_name = symbol_conf.vmlinux_name;
752 if (!vmlinux_name)
Masami Hiramatsu0a775822016-05-15 12:19:40 +0900753 vmlinux_name = DSO__NAME_KALLSYMS;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300754
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300755 kernel = machine__findnew_kernel(machine, vmlinux_name,
756 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300757 } else {
758 char bf[PATH_MAX];
759
760 if (machine__is_default_guest(machine))
761 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
762 if (!vmlinux_name)
763 vmlinux_name = machine__mmap_name(machine, bf,
764 sizeof(bf));
765
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300766 kernel = machine__findnew_kernel(machine, vmlinux_name,
767 "[guest.kernel]",
768 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300769 }
770
771 if (kernel != NULL && (!kernel->has_build_id))
772 dso__read_running_kernel_build_id(kernel, machine);
773
774 return kernel;
775}
776
777struct process_args {
778 u64 start;
779};
780
Adrian Hunter15a0a872014-01-29 16:14:38 +0200781static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
782 size_t bufsz)
783{
784 if (machine__is_default_guest(machine))
785 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
786 else
787 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
788}
789
Simon Quea93f0e52014-06-16 11:32:09 -0700790const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
791
792/* Figure out the start address of kernel map from /proc/kallsyms.
793 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
794 * symbol_name if it's not that important.
795 */
Adrian Hunter4b993752014-08-15 22:08:38 +0300796static u64 machine__get_running_kernel_start(struct machine *machine,
797 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300798{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200799 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700800 int i;
801 const char *name;
802 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300803
Adrian Hunter15a0a872014-01-29 16:14:38 +0200804 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300805
806 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
807 return 0;
808
Simon Quea93f0e52014-06-16 11:32:09 -0700809 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
810 addr = kallsyms__get_function_start(filename, name);
811 if (addr)
812 break;
813 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300814
Simon Quea93f0e52014-06-16 11:32:09 -0700815 if (symbol_name)
816 *symbol_name = name;
817
818 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300819}
820
821int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
822{
Arnaldo Carvalho de Meloa0b2f5a2017-02-14 16:19:56 -0300823 int type;
Adrian Hunter4b993752014-08-15 22:08:38 +0300824 u64 start = machine__get_running_kernel_start(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300825
Masami Hiramatsucc1121a2015-12-09 11:11:33 +0900826 /* In case of renewal the kernel map, destroy previous one */
827 machine__destroy_kernel_maps(machine);
828
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300829 for (type = 0; type < MAP__NR_TYPES; ++type) {
830 struct kmap *kmap;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300831 struct map *map;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300832
833 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
834 if (machine->vmlinux_maps[type] == NULL)
835 return -1;
836
837 machine->vmlinux_maps[type]->map_ip =
838 machine->vmlinux_maps[type]->unmap_ip =
839 identity__map_ip;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300840 map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300841 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000842 if (!kmap)
843 return -1;
844
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300845 kmap->kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300846 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300847 }
848
849 return 0;
850}
851
852void machine__destroy_kernel_maps(struct machine *machine)
853{
Arnaldo Carvalho de Meloa0b2f5a2017-02-14 16:19:56 -0300854 int type;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300855
856 for (type = 0; type < MAP__NR_TYPES; ++type) {
857 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300858 struct map *map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300859
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300860 if (map == NULL)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300861 continue;
862
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300863 kmap = map__kmap(map);
864 map_groups__remove(&machine->kmaps, map);
Wang Nanba927322015-04-07 08:22:45 +0000865 if (kmap && kmap->ref_reloc_sym) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300866 /*
867 * ref_reloc_sym is shared among all maps, so free just
868 * on one of them.
869 */
870 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300871 zfree((char **)&kmap->ref_reloc_sym->name);
872 zfree(&kmap->ref_reloc_sym);
873 } else
874 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300875 }
876
Masami Hiramatsue96e4072015-11-18 15:40:22 +0900877 map__put(machine->vmlinux_maps[type]);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300878 machine->vmlinux_maps[type] = NULL;
879 }
880}
881
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300882int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300883{
884 int ret = 0;
885 struct dirent **namelist = NULL;
886 int i, items = 0;
887 char path[PATH_MAX];
888 pid_t pid;
889 char *endp;
890
891 if (symbol_conf.default_guest_vmlinux_name ||
892 symbol_conf.default_guest_modules ||
893 symbol_conf.default_guest_kallsyms) {
894 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
895 }
896
897 if (symbol_conf.guestmount) {
898 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
899 if (items <= 0)
900 return -ENOENT;
901 for (i = 0; i < items; i++) {
902 if (!isdigit(namelist[i]->d_name[0])) {
903 /* Filter out . and .. */
904 continue;
905 }
906 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
907 if ((*endp != '\0') ||
908 (endp == namelist[i]->d_name) ||
909 (errno == ERANGE)) {
910 pr_debug("invalid directory (%s). Skipping.\n",
911 namelist[i]->d_name);
912 continue;
913 }
914 sprintf(path, "%s/%s/proc/kallsyms",
915 symbol_conf.guestmount,
916 namelist[i]->d_name);
917 ret = access(path, R_OK);
918 if (ret) {
919 pr_debug("Can't access file %s\n", path);
920 goto failure;
921 }
922 machines__create_kernel_maps(machines, pid);
923 }
924failure:
925 free(namelist);
926 }
927
928 return ret;
929}
930
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300931void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300932{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300933 struct rb_node *next = rb_first(&machines->guests);
934
935 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300936
937 while (next) {
938 struct machine *pos = rb_entry(next, struct machine, rb_node);
939
940 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300941 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300942 machine__delete(pos);
943 }
944}
945
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300946int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300947{
948 struct machine *machine = machines__findnew(machines, pid);
949
950 if (machine == NULL)
951 return -1;
952
953 return machine__create_kernel_maps(machine);
954}
955
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300956int __machine__load_kallsyms(struct machine *machine, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300957 enum map_type type, bool no_kcore)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300958{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300959 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300960 int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300961
962 if (ret > 0) {
963 dso__set_loaded(map->dso, type);
964 /*
965 * Since /proc/kallsyms will have multiple sessions for the
966 * kernel, with modules between them, fixup the end of all
967 * sections.
968 */
969 __map_groups__fixup_end(&machine->kmaps, type);
970 }
971
972 return ret;
973}
974
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300975int machine__load_kallsyms(struct machine *machine, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300976 enum map_type type)
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300977{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300978 return __machine__load_kallsyms(machine, filename, type, false);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300979}
980
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300981int machine__load_vmlinux_path(struct machine *machine, enum map_type type)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300982{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300983 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300984 int ret = dso__load_vmlinux_path(map->dso, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300985
Adrian Hunter39b12f782013-08-07 14:38:47 +0300986 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300987 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300988
989 return ret;
990}
991
992static void map_groups__fixup_end(struct map_groups *mg)
993{
994 int i;
995 for (i = 0; i < MAP__NR_TYPES; ++i)
996 __map_groups__fixup_end(mg, i);
997}
998
999static char *get_kernel_version(const char *root_dir)
1000{
1001 char version[PATH_MAX];
1002 FILE *file;
1003 char *name, *tmp;
1004 const char *prefix = "Linux version ";
1005
1006 sprintf(version, "%s/proc/version", root_dir);
1007 file = fopen(version, "r");
1008 if (!file)
1009 return NULL;
1010
1011 version[0] = '\0';
1012 tmp = fgets(version, sizeof(version), file);
1013 fclose(file);
1014
1015 name = strstr(version, prefix);
1016 if (!name)
1017 return NULL;
1018 name += strlen(prefix);
1019 tmp = strchr(name, ' ');
1020 if (tmp)
1021 *tmp = '\0';
1022
1023 return strdup(name);
1024}
1025
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001026static bool is_kmod_dso(struct dso *dso)
1027{
1028 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1029 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1030}
1031
1032static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1033 struct kmod_path *m)
1034{
1035 struct map *map;
1036 char *long_name;
1037
1038 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
1039 if (map == NULL)
1040 return 0;
1041
1042 long_name = strdup(path);
1043 if (long_name == NULL)
1044 return -ENOMEM;
1045
1046 dso__set_long_name(map->dso, long_name, true);
1047 dso__kernel_module_get_build_id(map->dso, "");
1048
1049 /*
1050 * Full name could reveal us kmod compression, so
1051 * we need to update the symtab_type if needed.
1052 */
1053 if (m->comp && is_kmod_dso(map->dso))
1054 map->dso->symtab_type++;
1055
1056 return 0;
1057}
1058
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001059static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -04001060 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001061{
1062 struct dirent *dent;
1063 DIR *dir = opendir(dir_name);
1064 int ret = 0;
1065
1066 if (!dir) {
1067 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1068 return -1;
1069 }
1070
1071 while ((dent = readdir(dir)) != NULL) {
1072 char path[PATH_MAX];
1073 struct stat st;
1074
1075 /*sshfs might return bad dent->d_type, so we have to stat*/
1076 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1077 if (stat(path, &st))
1078 continue;
1079
1080 if (S_ISDIR(st.st_mode)) {
1081 if (!strcmp(dent->d_name, ".") ||
1082 !strcmp(dent->d_name, ".."))
1083 continue;
1084
Richard Yao61d42902014-04-26 13:17:55 -04001085 /* Do not follow top-level source and build symlinks */
1086 if (depth == 0) {
1087 if (!strcmp(dent->d_name, "source") ||
1088 !strcmp(dent->d_name, "build"))
1089 continue;
1090 }
1091
1092 ret = map_groups__set_modules_path_dir(mg, path,
1093 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001094 if (ret < 0)
1095 goto out;
1096 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001097 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001098
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001099 ret = kmod_path__parse_name(&m, dent->d_name);
1100 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001101 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001102
1103 if (m.kmod)
1104 ret = map_groups__set_module_path(mg, path, &m);
1105
1106 free(m.name);
1107
1108 if (ret)
1109 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001110 }
1111 }
1112
1113out:
1114 closedir(dir);
1115 return ret;
1116}
1117
1118static int machine__set_modules_path(struct machine *machine)
1119{
1120 char *version;
1121 char modules_path[PATH_MAX];
1122
1123 version = get_kernel_version(machine->root_dir);
1124 if (!version)
1125 return -1;
1126
Richard Yao61d42902014-04-26 13:17:55 -04001127 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001128 machine->root_dir, version);
1129 free(version);
1130
Richard Yao61d42902014-04-26 13:17:55 -04001131 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001132}
Song Shan Gong203d8a42016-07-21 11:10:51 +08001133int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1134 const char *name __maybe_unused)
1135{
1136 return 0;
1137}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001138
Adrian Hunter316d70d2013-10-08 11:45:48 +03001139static int machine__create_module(void *arg, const char *name, u64 start)
1140{
1141 struct machine *machine = arg;
1142 struct map *map;
1143
Song Shan Gong203d8a42016-07-21 11:10:51 +08001144 if (arch__fix_module_text_start(&start, name) < 0)
1145 return -1;
1146
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001147 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001148 if (map == NULL)
1149 return -1;
1150
1151 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1152
1153 return 0;
1154}
1155
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001156static int machine__create_modules(struct machine *machine)
1157{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001158 const char *modules;
1159 char path[PATH_MAX];
1160
Adrian Hunterf4be9042013-09-22 13:22:09 +03001161 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001162 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001163 } else {
1164 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001165 modules = path;
1166 }
1167
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001168 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001169 return -1;
1170
Adrian Hunter316d70d2013-10-08 11:45:48 +03001171 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001172 return -1;
1173
Adrian Hunter316d70d2013-10-08 11:45:48 +03001174 if (!machine__set_modules_path(machine))
1175 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001176
Adrian Hunter316d70d2013-10-08 11:45:48 +03001177 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001178
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001179 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001180}
1181
1182int machine__create_kernel_maps(struct machine *machine)
1183{
1184 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001185 const char *name;
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001186 u64 addr;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001187 int ret;
1188
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001189 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001190 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001191
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001192 ret = __machine__create_kernel_maps(machine, kernel);
1193 dso__put(kernel);
1194 if (ret < 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001195 return -1;
1196
1197 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1198 if (machine__is_host(machine))
1199 pr_debug("Problems creating module maps, "
1200 "continuing anyway...\n");
1201 else
1202 pr_debug("Problems creating module maps for guest %d, "
1203 "continuing anyway...\n", machine->pid);
1204 }
1205
1206 /*
1207 * Now that we have all the maps created, just set the ->end of them:
1208 */
1209 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001210
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001211 addr = machine__get_running_kernel_start(machine, &name);
1212 if (!addr) {
1213 } else if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) {
Adrian Hunter5512cf22014-01-29 16:14:39 +02001214 machine__destroy_kernel_maps(machine);
1215 return -1;
1216 }
1217
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001218 return 0;
1219}
1220
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001221static void machine__set_kernel_mmap_len(struct machine *machine,
1222 union perf_event *event)
1223{
Namhyung Kim4552cf0f2012-11-07 16:27:10 +09001224 int i;
1225
1226 for (i = 0; i < MAP__NR_TYPES; i++) {
1227 machine->vmlinux_maps[i]->start = event->mmap.start;
1228 machine->vmlinux_maps[i]->end = (event->mmap.start +
1229 event->mmap.len);
1230 /*
1231 * Be a bit paranoid here, some perf.data file came with
1232 * a zero sized synthesized MMAP event for the kernel.
1233 */
1234 if (machine->vmlinux_maps[i]->end == 0)
1235 machine->vmlinux_maps[i]->end = ~0ULL;
1236 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001237}
1238
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001239static bool machine__uses_kcore(struct machine *machine)
1240{
1241 struct dso *dso;
1242
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001243 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001244 if (dso__is_kcore(dso))
1245 return true;
1246 }
1247
1248 return false;
1249}
1250
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001251static int machine__process_kernel_mmap_event(struct machine *machine,
1252 union perf_event *event)
1253{
1254 struct map *map;
1255 char kmmap_prefix[PATH_MAX];
1256 enum dso_kernel_type kernel_type;
1257 bool is_kernel_mmap;
1258
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001259 /* If we have maps from kcore then we do not need or want any others */
1260 if (machine__uses_kcore(machine))
1261 return 0;
1262
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001263 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1264 if (machine__is_host(machine))
1265 kernel_type = DSO_TYPE_KERNEL;
1266 else
1267 kernel_type = DSO_TYPE_GUEST_KERNEL;
1268
1269 is_kernel_mmap = memcmp(event->mmap.filename,
1270 kmmap_prefix,
1271 strlen(kmmap_prefix) - 1) == 0;
1272 if (event->mmap.filename[0] == '/' ||
1273 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001274 map = machine__findnew_module_map(machine, event->mmap.start,
1275 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001276 if (map == NULL)
1277 goto out_problem;
1278
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001279 map->end = map->start + event->mmap.len;
1280 } else if (is_kernel_mmap) {
1281 const char *symbol_name = (event->mmap.filename +
1282 strlen(kmmap_prefix));
1283 /*
1284 * Should be there already, from the build-id table in
1285 * the header.
1286 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001287 struct dso *kernel = NULL;
1288 struct dso *dso;
1289
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001290 pthread_rwlock_rdlock(&machine->dsos.lock);
1291
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001292 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001293
1294 /*
1295 * The cpumode passed to is_kernel_module is not the
1296 * cpumode of *this* event. If we insist on passing
1297 * correct cpumode to is_kernel_module, we should
1298 * record the cpumode when we adding this dso to the
1299 * linked list.
1300 *
1301 * However we don't really need passing correct
1302 * cpumode. We know the correct cpumode must be kernel
1303 * mode (if not, we should not link it onto kernel_dsos
1304 * list).
1305 *
1306 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1307 * is_kernel_module() treats it as a kernel cpumode.
1308 */
1309
1310 if (!dso->kernel ||
1311 is_kernel_module(dso->long_name,
1312 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001313 continue;
1314
Wang Nan1f121b02015-06-03 08:52:21 +00001315
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001316 kernel = dso;
1317 break;
1318 }
1319
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001320 pthread_rwlock_unlock(&machine->dsos.lock);
1321
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001322 if (kernel == NULL)
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001323 kernel = machine__findnew_dso(machine, kmmap_prefix);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001324 if (kernel == NULL)
1325 goto out_problem;
1326
1327 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001328 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1329 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001330 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001331 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001332
Namhyung Kim330dfa22014-11-18 13:30:28 +09001333 if (strstr(kernel->long_name, "vmlinux"))
1334 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001335
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001336 machine__set_kernel_mmap_len(machine, event);
1337
1338 /*
1339 * Avoid using a zero address (kptr_restrict) for the ref reloc
1340 * symbol. Effectively having zero here means that at record
1341 * time /proc/sys/kernel/kptr_restrict was non zero.
1342 */
1343 if (event->mmap.pgoff != 0) {
1344 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1345 symbol_name,
1346 event->mmap.pgoff);
1347 }
1348
1349 if (machine__is_default_guest(machine)) {
1350 /*
1351 * preload dso of guest kernel and modules
1352 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001353 dso__load(kernel, machine__kernel_map(machine));
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001354 }
1355 }
1356 return 0;
1357out_problem:
1358 return -1;
1359}
1360
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001361int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001362 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001363 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001364{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001365 struct thread *thread;
1366 struct map *map;
1367 enum map_type type;
1368 int ret = 0;
1369
1370 if (dump_trace)
1371 perf_event__fprintf_mmap2(event, stdout);
1372
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001373 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1374 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001375 ret = machine__process_kernel_mmap_event(machine, event);
1376 if (ret < 0)
1377 goto out_problem;
1378 return 0;
1379 }
1380
1381 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001382 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001383 if (thread == NULL)
1384 goto out_problem;
1385
1386 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1387 type = MAP__VARIABLE;
1388 else
1389 type = MAP__FUNCTION;
1390
Adrian Hunter2a030682014-07-22 16:17:53 +03001391 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001392 event->mmap2.len, event->mmap2.pgoff,
1393 event->mmap2.pid, event->mmap2.maj,
1394 event->mmap2.min, event->mmap2.ino,
1395 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001396 event->mmap2.prot,
1397 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001398 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001399
1400 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001401 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001402
He Kuang8132a2a2016-06-03 03:33:13 +00001403 ret = thread__insert_map(thread, map);
1404 if (ret)
1405 goto out_problem_insert;
1406
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001407 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001408 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001409 return 0;
1410
He Kuang8132a2a2016-06-03 03:33:13 +00001411out_problem_insert:
1412 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001413out_problem_map:
1414 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001415out_problem:
1416 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1417 return 0;
1418}
1419
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001420int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001421 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001422{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001423 struct thread *thread;
1424 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001425 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001426 int ret = 0;
1427
1428 if (dump_trace)
1429 perf_event__fprintf_mmap(event, stdout);
1430
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001431 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1432 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001433 ret = machine__process_kernel_mmap_event(machine, event);
1434 if (ret < 0)
1435 goto out_problem;
1436 return 0;
1437 }
1438
Adrian Hunter314add62013-08-27 11:23:03 +03001439 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001440 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001441 if (thread == NULL)
1442 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001443
1444 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1445 type = MAP__VARIABLE;
1446 else
1447 type = MAP__FUNCTION;
1448
Adrian Hunter2a030682014-07-22 16:17:53 +03001449 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001450 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001451 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001452 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001453 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001454
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001455 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001456 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001457
He Kuang8132a2a2016-06-03 03:33:13 +00001458 ret = thread__insert_map(thread, map);
1459 if (ret)
1460 goto out_problem_insert;
1461
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001462 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001463 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001464 return 0;
1465
He Kuang8132a2a2016-06-03 03:33:13 +00001466out_problem_insert:
1467 map__put(map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001468out_problem_map:
1469 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001470out_problem:
1471 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1472 return 0;
1473}
1474
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001475static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001476{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001477 if (machine->last_match == th)
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -03001478 machine->last_match = NULL;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001479
Elena Reshetovae34f5b12017-02-21 17:35:02 +02001480 BUG_ON(refcount_read(&th->refcnt) == 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001481 if (lock)
1482 pthread_rwlock_wrlock(&machine->threads_lock);
Arnaldo Carvalho de Melo0170b142015-05-25 15:23:05 -03001483 rb_erase_init(&th->rb_node, &machine->threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001484 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -03001485 --machine->nr_threads;
David Ahern236a3bb2013-08-14 08:49:27 -06001486 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001487 * Move it first to the dead_threads list, then drop the reference,
1488 * if this is the last reference, then the thread__delete destructor
1489 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001490 */
1491 list_add_tail(&th->node, &machine->dead_threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001492 if (lock)
1493 pthread_rwlock_unlock(&machine->threads_lock);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001494 thread__put(th);
David Ahern236a3bb2013-08-14 08:49:27 -06001495}
1496
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001497void machine__remove_thread(struct machine *machine, struct thread *th)
1498{
1499 return __machine__remove_thread(machine, th, true);
1500}
1501
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001502int machine__process_fork_event(struct machine *machine, union perf_event *event,
1503 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001504{
Jiri Olsad75e6092014-03-14 15:00:03 +01001505 struct thread *thread = machine__find_thread(machine,
1506 event->fork.pid,
1507 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001508 struct thread *parent = machine__findnew_thread(machine,
1509 event->fork.ppid,
1510 event->fork.ptid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001511 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001512
Adrian Hunter5cb73342015-08-19 17:29:20 +03001513 if (dump_trace)
1514 perf_event__fprintf_task(event, stdout);
1515
1516 /*
1517 * There may be an existing thread that is not actually the parent,
1518 * either because we are processing events out of order, or because the
1519 * (fork) event that would have removed the thread was lost. Assume the
1520 * latter case and continue on as best we can.
1521 */
1522 if (parent->pid_ != (pid_t)event->fork.ppid) {
1523 dump_printf("removing erroneous parent thread %d/%d\n",
1524 parent->pid_, parent->tid);
1525 machine__remove_thread(machine, parent);
1526 thread__put(parent);
1527 parent = machine__findnew_thread(machine, event->fork.ppid,
1528 event->fork.ptid);
1529 }
1530
David Ahern236a3bb2013-08-14 08:49:27 -06001531 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001532 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001533 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001534 thread__put(thread);
1535 }
David Ahern236a3bb2013-08-14 08:49:27 -06001536
Adrian Hunter314add62013-08-27 11:23:03 +03001537 thread = machine__findnew_thread(machine, event->fork.pid,
1538 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001539
1540 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001541 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001542 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001543 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001544 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001545 thread__put(thread);
1546 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001547
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001548 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001549}
1550
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001551int machine__process_exit_event(struct machine *machine, union perf_event *event,
1552 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001553{
Jiri Olsad75e6092014-03-14 15:00:03 +01001554 struct thread *thread = machine__find_thread(machine,
1555 event->fork.pid,
1556 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001557
1558 if (dump_trace)
1559 perf_event__fprintf_task(event, stdout);
1560
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001561 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001562 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001563 thread__put(thread);
1564 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001565
1566 return 0;
1567}
1568
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001569int machine__process_event(struct machine *machine, union perf_event *event,
1570 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001571{
1572 int ret;
1573
1574 switch (event->header.type) {
1575 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001576 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001577 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001578 ret = machine__process_mmap_event(machine, event, sample); break;
Hari Bathinif3b36142017-03-08 02:11:43 +05301579 case PERF_RECORD_NAMESPACES:
1580 ret = machine__process_namespaces_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001581 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001582 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001583 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001584 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001585 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001586 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001587 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001588 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001589 case PERF_RECORD_AUX:
1590 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001591 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001592 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001593 case PERF_RECORD_LOST_SAMPLES:
1594 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001595 case PERF_RECORD_SWITCH:
1596 case PERF_RECORD_SWITCH_CPU_WIDE:
1597 ret = machine__process_switch_event(machine, event); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001598 default:
1599 ret = -1;
1600 break;
1601 }
1602
1603 return ret;
1604}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001605
Greg Priceb21484f2012-12-06 21:48:05 -08001606static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001607{
Arnaldo Carvalho de Meloa7c38992017-02-13 16:52:15 -03001608 if (!regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001609 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001610 return 0;
1611}
1612
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001613static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001614 struct addr_map_symbol *ams,
1615 u64 ip)
1616{
1617 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001618
1619 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001620 /*
1621 * We cannot use the header.misc hint to determine whether a
1622 * branch stack address is user, kernel, guest, hypervisor.
1623 * Branches may straddle the kernel/user/hypervisor boundaries.
1624 * Thus, we have to try consecutively until we find a match
1625 * or else, the symbol is unknown
1626 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001627 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001628
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001629 ams->addr = ip;
1630 ams->al_addr = al.addr;
1631 ams->sym = al.sym;
1632 ams->map = al.map;
1633}
1634
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001635static void ip__resolve_data(struct thread *thread,
Stephane Eranian98a3b322013-01-24 16:10:35 +01001636 u8 m, struct addr_map_symbol *ams, u64 addr)
1637{
1638 struct addr_location al;
1639
1640 memset(&al, 0, sizeof(al));
1641
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001642 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001643 if (al.map == NULL) {
1644 /*
1645 * some shared data regions have execute bit set which puts
1646 * their mapping in the MAP__FUNCTION type array.
1647 * Check there as a fallback option before dropping the sample.
1648 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001649 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001650 }
1651
Stephane Eranian98a3b322013-01-24 16:10:35 +01001652 ams->addr = addr;
1653 ams->al_addr = al.addr;
1654 ams->sym = al.sym;
1655 ams->map = al.map;
1656}
1657
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001658struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1659 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001660{
1661 struct mem_info *mi = zalloc(sizeof(*mi));
1662
1663 if (!mi)
1664 return NULL;
1665
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001666 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1667 ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001668 mi->data_src.val = sample->data_src;
1669
1670 return mi;
1671}
1672
Andi Kleen37592b82014-11-12 18:05:19 -08001673static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001674 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08001675 struct symbol **parent,
1676 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001677 u8 *cpumode,
Jin Yao410024d2016-10-31 09:19:49 +08001678 u64 ip,
1679 bool branch,
1680 struct branch_flags *flags,
1681 int nr_loop_iter,
1682 int samples)
Andi Kleen37592b82014-11-12 18:05:19 -08001683{
1684 struct addr_location al;
1685
1686 al.filtered = 0;
1687 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001688 if (!cpumode) {
Andi Kleen8b7bad52014-11-12 18:05:20 -08001689 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1690 ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001691 } else {
Kan Liang2e777842014-12-02 10:06:53 -05001692 if (ip >= PERF_CONTEXT_MAX) {
1693 switch (ip) {
1694 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001695 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05001696 break;
1697 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001698 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05001699 break;
1700 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001701 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05001702 break;
1703 default:
1704 pr_debug("invalid callchain context: "
1705 "%"PRId64"\n", (s64) ip);
1706 /*
1707 * It seems the callchain is corrupted.
1708 * Discard all.
1709 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001710 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05001711 return 1;
1712 }
1713 return 0;
1714 }
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001715 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1716 ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05001717 }
1718
Andi Kleen37592b82014-11-12 18:05:19 -08001719 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02001720 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08001721 symbol__match_regex(al.sym, &parent_regex))
1722 *parent = al.sym;
1723 else if (have_ignore_callees && root_al &&
1724 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1725 /* Treat this symbol as the root,
1726 forgetting its callees. */
1727 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001728 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08001729 }
1730 }
1731
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09001732 if (symbol_conf.hide_unresolved && al.sym == NULL)
1733 return 0;
Jin Yao410024d2016-10-31 09:19:49 +08001734 return callchain_cursor_append(cursor, al.addr, al.map, al.sym,
1735 branch, flags, nr_loop_iter, samples);
Andi Kleen37592b82014-11-12 18:05:19 -08001736}
1737
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001738struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1739 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001740{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001741 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001742 const struct branch_stack *bs = sample->branch_stack;
1743 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001744
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001745 if (!bi)
1746 return NULL;
1747
1748 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001749 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1750 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001751 bi[i].flags = bs->entries[i].flags;
1752 }
1753 return bi;
1754}
1755
Andi Kleen8b7bad52014-11-12 18:05:20 -08001756#define CHASHSZ 127
1757#define CHASHBITS 7
1758#define NO_ENTRY 0xff
1759
1760#define PERF_MAX_BRANCH_DEPTH 127
1761
1762/* Remove loops. */
1763static int remove_loops(struct branch_entry *l, int nr)
1764{
1765 int i, j, off;
1766 unsigned char chash[CHASHSZ];
1767
1768 memset(chash, NO_ENTRY, sizeof(chash));
1769
1770 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1771
1772 for (i = 0; i < nr; i++) {
1773 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1774
1775 /* no collision handling for now */
1776 if (chash[h] == NO_ENTRY) {
1777 chash[h] = i;
1778 } else if (l[chash[h]].from == l[i].from) {
1779 bool is_loop = true;
1780 /* check if it is a real loop */
1781 off = 0;
1782 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1783 if (l[j].from != l[i + off].from) {
1784 is_loop = false;
1785 break;
1786 }
1787 if (is_loop) {
1788 memmove(l + i, l + i + off,
1789 (nr - (i + off)) * sizeof(*l));
1790 nr -= off;
1791 }
1792 }
1793 }
1794 return nr;
1795}
1796
Kan Liang384b6052015-01-05 13:23:05 -05001797/*
1798 * Recolve LBR callstack chain sample
1799 * Return:
1800 * 1 on success get LBR callchain information
1801 * 0 no available LBR callchain information, should try fp
1802 * negative error code on other errors.
1803 */
1804static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001805 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001806 struct perf_sample *sample,
1807 struct symbol **parent,
1808 struct addr_location *root_al,
1809 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001810{
Kan Liang384b6052015-01-05 13:23:05 -05001811 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001812 int chain_nr = min(max_stack, (int)chain->nr), i;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001813 u8 cpumode = PERF_RECORD_MISC_USER;
Kan Liang384b6052015-01-05 13:23:05 -05001814 u64 ip;
1815
1816 for (i = 0; i < chain_nr; i++) {
1817 if (chain->ips[i] == PERF_CONTEXT_USER)
1818 break;
1819 }
1820
1821 /* LBR only affects the user callchain */
1822 if (i != chain_nr) {
1823 struct branch_stack *lbr_stack = sample->branch_stack;
Jin Yao410024d2016-10-31 09:19:49 +08001824 int lbr_nr = lbr_stack->nr, j, k;
1825 bool branch;
1826 struct branch_flags *flags;
Kan Liang384b6052015-01-05 13:23:05 -05001827 /*
1828 * LBR callstack can only get user call chain.
1829 * The mix_chain_nr is kernel call chain
1830 * number plus LBR user call chain number.
1831 * i is kernel call chain number,
1832 * 1 is PERF_CONTEXT_USER,
1833 * lbr_nr + 1 is the user call chain number.
1834 * For details, please refer to the comments
1835 * in callchain__printf
1836 */
1837 int mix_chain_nr = i + 1 + lbr_nr + 1;
1838
Kan Liang384b6052015-01-05 13:23:05 -05001839 for (j = 0; j < mix_chain_nr; j++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001840 int err;
Jin Yao410024d2016-10-31 09:19:49 +08001841 branch = false;
1842 flags = NULL;
1843
Kan Liang384b6052015-01-05 13:23:05 -05001844 if (callchain_param.order == ORDER_CALLEE) {
1845 if (j < i + 1)
1846 ip = chain->ips[j];
Jin Yao410024d2016-10-31 09:19:49 +08001847 else if (j > i + 1) {
1848 k = j - i - 2;
1849 ip = lbr_stack->entries[k].from;
1850 branch = true;
1851 flags = &lbr_stack->entries[k].flags;
1852 } else {
Kan Liang384b6052015-01-05 13:23:05 -05001853 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08001854 branch = true;
1855 flags = &lbr_stack->entries[0].flags;
1856 }
Kan Liang384b6052015-01-05 13:23:05 -05001857 } else {
Jin Yao410024d2016-10-31 09:19:49 +08001858 if (j < lbr_nr) {
1859 k = lbr_nr - j - 1;
1860 ip = lbr_stack->entries[k].from;
1861 branch = true;
1862 flags = &lbr_stack->entries[k].flags;
1863 }
Kan Liang384b6052015-01-05 13:23:05 -05001864 else if (j > lbr_nr)
1865 ip = chain->ips[i + 1 - (j - lbr_nr)];
Jin Yao410024d2016-10-31 09:19:49 +08001866 else {
Kan Liang384b6052015-01-05 13:23:05 -05001867 ip = lbr_stack->entries[0].to;
Jin Yao410024d2016-10-31 09:19:49 +08001868 branch = true;
1869 flags = &lbr_stack->entries[0].flags;
1870 }
Kan Liang384b6052015-01-05 13:23:05 -05001871 }
1872
Jin Yao410024d2016-10-31 09:19:49 +08001873 err = add_callchain_ip(thread, cursor, parent,
1874 root_al, &cpumode, ip,
1875 branch, flags, 0, 0);
Kan Liang384b6052015-01-05 13:23:05 -05001876 if (err)
1877 return (err < 0) ? err : 0;
1878 }
1879 return 1;
1880 }
1881
1882 return 0;
1883}
1884
1885static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001886 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001887 struct perf_evsel *evsel,
1888 struct perf_sample *sample,
1889 struct symbol **parent,
1890 struct addr_location *root_al,
1891 int max_stack)
1892{
1893 struct branch_stack *branch = sample->branch_stack;
1894 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03001895 int chain_nr = chain->nr;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001896 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001897 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08001898 int skip_idx = -1;
1899 int first_call = 0;
Jin Yao410024d2016-10-31 09:19:49 +08001900 int nr_loop_iter;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001901
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03001902 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001903 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05001904 root_al, max_stack);
1905 if (err)
1906 return (err < 0) ? err : 0;
1907 }
1908
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001909 /*
1910 * Based on DWARF debug information, some architectures skip
1911 * a callchain entry saved by the kernel.
1912 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001913 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001914
Andi Kleen8b7bad52014-11-12 18:05:20 -08001915 /*
1916 * Add branches to call stack for easier browsing. This gives
1917 * more context for a sample than just the callers.
1918 *
1919 * This uses individual histograms of paths compared to the
1920 * aggregated histograms the normal LBR mode uses.
1921 *
1922 * Limitations for now:
1923 * - No extra filters
1924 * - No annotations (should annotate somehow)
1925 */
1926
1927 if (branch && callchain_param.branch_callstack) {
1928 int nr = min(max_stack, (int)branch->nr);
1929 struct branch_entry be[nr];
1930
1931 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
1932 pr_warning("corrupted branch chain. skipping...\n");
1933 goto check_calls;
1934 }
1935
1936 for (i = 0; i < nr; i++) {
1937 if (callchain_param.order == ORDER_CALLEE) {
1938 be[i] = branch->entries[i];
1939 /*
1940 * Check for overlap into the callchain.
1941 * The return address is one off compared to
1942 * the branch entry. To adjust for this
1943 * assume the calling instruction is not longer
1944 * than 8 bytes.
1945 */
1946 if (i == skip_idx ||
1947 chain->ips[first_call] >= PERF_CONTEXT_MAX)
1948 first_call++;
1949 else if (be[i].from < chain->ips[first_call] &&
1950 be[i].from >= chain->ips[first_call] - 8)
1951 first_call++;
1952 } else
1953 be[i] = branch->entries[branch->nr - i - 1];
1954 }
1955
Jin Yao410024d2016-10-31 09:19:49 +08001956 nr_loop_iter = nr;
Andi Kleen8b7bad52014-11-12 18:05:20 -08001957 nr = remove_loops(be, nr);
1958
Jin Yao410024d2016-10-31 09:19:49 +08001959 /*
1960 * Get the number of iterations.
1961 * It's only approximation, but good enough in practice.
1962 */
1963 if (nr_loop_iter > nr)
1964 nr_loop_iter = nr_loop_iter - nr + 1;
1965 else
1966 nr_loop_iter = 0;
1967
Andi Kleen8b7bad52014-11-12 18:05:20 -08001968 for (i = 0; i < nr; i++) {
Jin Yao410024d2016-10-31 09:19:49 +08001969 if (i == nr - 1)
1970 err = add_callchain_ip(thread, cursor, parent,
1971 root_al,
1972 NULL, be[i].to,
1973 true, &be[i].flags,
1974 nr_loop_iter, 1);
1975 else
1976 err = add_callchain_ip(thread, cursor, parent,
1977 root_al,
1978 NULL, be[i].to,
1979 true, &be[i].flags,
1980 0, 0);
1981
Andi Kleen8b7bad52014-11-12 18:05:20 -08001982 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001983 err = add_callchain_ip(thread, cursor, parent, root_al,
Jin Yao410024d2016-10-31 09:19:49 +08001984 NULL, be[i].from,
1985 true, &be[i].flags,
1986 0, 0);
Andi Kleen8b7bad52014-11-12 18:05:20 -08001987 if (err == -EINVAL)
1988 break;
1989 if (err)
1990 return err;
1991 }
1992 chain_nr -= nr;
1993 }
1994
1995check_calls:
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001996 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03001997 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001998 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001999
2000 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002001 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002002 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07002003 j = chain->nr - i - 1;
2004
2005#ifdef HAVE_SKIP_CALLCHAIN_IDX
2006 if (j == skip_idx)
2007 continue;
2008#endif
2009 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002010
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03002011 if (ip < PERF_CONTEXT_MAX)
2012 ++nr_entries;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03002013
Jin Yao410024d2016-10-31 09:19:49 +08002014 err = add_callchain_ip(thread, cursor, parent,
2015 root_al, &cpumode, ip,
2016 false, NULL, 0, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002017
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002018 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05002019 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002020 }
2021
2022 return 0;
2023}
2024
2025static int unwind_entry(struct unwind_entry *entry, void *arg)
2026{
2027 struct callchain_cursor *cursor = arg;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09002028
2029 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2030 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002031 return callchain_cursor_append(cursor, entry->ip,
Jin Yao410024d2016-10-31 09:19:49 +08002032 entry->map, entry->sym,
2033 false, NULL, 0, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002034}
2035
Chris Phlipot9919a652016-04-28 01:19:06 -07002036static int thread__resolve_callchain_unwind(struct thread *thread,
2037 struct callchain_cursor *cursor,
2038 struct perf_evsel *evsel,
2039 struct perf_sample *sample,
2040 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002041{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002042 /* Can we do dwarf post unwind? */
2043 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2044 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
2045 return 0;
2046
2047 /* Bail out if nothing was captured. */
2048 if ((!sample->user_regs.regs) ||
2049 (!sample->user_stack.size))
2050 return 0;
2051
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03002052 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01002053 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07002054}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002055
Chris Phlipot9919a652016-04-28 01:19:06 -07002056int thread__resolve_callchain(struct thread *thread,
2057 struct callchain_cursor *cursor,
2058 struct perf_evsel *evsel,
2059 struct perf_sample *sample,
2060 struct symbol **parent,
2061 struct addr_location *root_al,
2062 int max_stack)
2063{
2064 int ret = 0;
2065
2066 callchain_cursor_reset(&callchain_cursor);
2067
2068 if (callchain_param.order == ORDER_CALLEE) {
2069 ret = thread__resolve_callchain_sample(thread, cursor,
2070 evsel, sample,
2071 parent, root_al,
2072 max_stack);
2073 if (ret)
2074 return ret;
2075 ret = thread__resolve_callchain_unwind(thread, cursor,
2076 evsel, sample,
2077 max_stack);
2078 } else {
2079 ret = thread__resolve_callchain_unwind(thread, cursor,
2080 evsel, sample,
2081 max_stack);
2082 if (ret)
2083 return ret;
2084 ret = thread__resolve_callchain_sample(thread, cursor,
2085 evsel, sample,
2086 parent, root_al,
2087 max_stack);
2088 }
2089
2090 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03002091}
David Ahern35feee12013-09-28 13:12:58 -06002092
2093int machine__for_each_thread(struct machine *machine,
2094 int (*fn)(struct thread *thread, void *p),
2095 void *priv)
2096{
2097 struct rb_node *nd;
2098 struct thread *thread;
2099 int rc = 0;
2100
2101 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
2102 thread = rb_entry(nd, struct thread, rb_node);
2103 rc = fn(thread, priv);
2104 if (rc != 0)
2105 return rc;
2106 }
2107
2108 list_for_each_entry(thread, &machine->dead_threads, node) {
2109 rc = fn(thread, priv);
2110 if (rc != 0)
2111 return rc;
2112 }
2113 return rc;
2114}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002115
Adrian Huntera5499b32015-05-29 16:33:30 +03002116int machines__for_each_thread(struct machines *machines,
2117 int (*fn)(struct thread *thread, void *p),
2118 void *priv)
2119{
2120 struct rb_node *nd;
2121 int rc = 0;
2122
2123 rc = machine__for_each_thread(&machines->host, fn, priv);
2124 if (rc != 0)
2125 return rc;
2126
2127 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
2128 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2129
2130 rc = machine__for_each_thread(machine, fn, priv);
2131 if (rc != 0)
2132 return rc;
2133 }
2134 return rc;
2135}
2136
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03002137int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002138 struct target *target, struct thread_map *threads,
Kan Liang9d9cad72015-06-17 09:51:11 -04002139 perf_event__handler_t process, bool data_mmap,
2140 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002141{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002142 if (target__has_task(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002143 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002144 else if (target__has_cpu(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002145 return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002146 /* command specified */
2147 return 0;
2148}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002149
2150pid_t machine__get_current_tid(struct machine *machine, int cpu)
2151{
2152 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2153 return -1;
2154
2155 return machine->current_tid[cpu];
2156}
2157
2158int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2159 pid_t tid)
2160{
2161 struct thread *thread;
2162
2163 if (cpu < 0)
2164 return -EINVAL;
2165
2166 if (!machine->current_tid) {
2167 int i;
2168
2169 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2170 if (!machine->current_tid)
2171 return -ENOMEM;
2172 for (i = 0; i < MAX_NR_CPUS; i++)
2173 machine->current_tid[i] = -1;
2174 }
2175
2176 if (cpu >= MAX_NR_CPUS) {
2177 pr_err("Requested CPU %d too large. ", cpu);
2178 pr_err("Consider raising MAX_NR_CPUS\n");
2179 return -EINVAL;
2180 }
2181
2182 machine->current_tid[cpu] = tid;
2183
2184 thread = machine__findnew_thread(machine, pid, tid);
2185 if (!thread)
2186 return -ENOMEM;
2187
2188 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002189 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002190
2191 return 0;
2192}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002193
2194int machine__get_kernel_start(struct machine *machine)
2195{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002196 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002197 int err = 0;
2198
2199 /*
2200 * The only addresses above 2^63 are kernel addresses of a 64-bit
2201 * kernel. Note that addresses are unsigned so that on a 32-bit system
2202 * all addresses including kernel addresses are less than 2^32. In
2203 * that case (32-bit system), if the kernel mapping is unknown, all
2204 * addresses will be assumed to be in user space - see
2205 * machine__kernel_ip().
2206 */
2207 machine->kernel_start = 1ULL << 63;
2208 if (map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002209 err = map__load(map);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002210 if (map->start)
2211 machine->kernel_start = map->start;
2212 }
2213 return err;
2214}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002215
2216struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2217{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03002218 return dsos__findnew(&machine->dsos, filename);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002219}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002220
2221char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2222{
2223 struct machine *machine = vmachine;
2224 struct map *map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002225 struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map);
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002226
2227 if (sym == NULL)
2228 return NULL;
2229
2230 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2231 *addrp = map->unmap_ip(map, sym->start);
2232 return sym->name;
2233}