blob: a7ad51100b557cab2bf4310e40782c1eb67044ba [file] [log] [blame]
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001#include "callchain.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002#include "debug.h"
3#include "event.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03004#include "evsel.h"
5#include "hist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -03006#include "machine.h"
7#include "map.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03008#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -03009#include "strlist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030010#include "thread.h"
Adrian Hunterd027b642014-07-23 14:23:00 +030011#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030012#include <stdbool.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030013#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030014#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080015#include "linux/hash.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030016
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030017static void dsos__init(struct dsos *dsos)
18{
19 INIT_LIST_HEAD(&dsos->head);
20 dsos->root = RB_ROOT;
21}
22
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030023int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
24{
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030025 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030026 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030027 dsos__init(&machine->user_dsos);
28 dsos__init(&machine->kernel_dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030029
30 machine->threads = RB_ROOT;
31 INIT_LIST_HEAD(&machine->dead_threads);
32 machine->last_match = NULL;
33
Adrian Hunterd027b642014-07-23 14:23:00 +030034 machine->vdso_info = NULL;
35
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030036 machine->pid = pid;
37
Adrian Hunter611a5ce2013-08-08 14:32:20 +030038 machine->symbol_filter = NULL;
Jiri Olsa14bd6d22014-01-07 13:47:19 +010039 machine->id_hdr_size = 0;
Adrian Huntercfe1c412014-07-31 09:00:45 +030040 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030041 machine->kernel_start = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030042
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030043 machine->root_dir = strdup(root_dir);
44 if (machine->root_dir == NULL)
45 return -ENOMEM;
46
47 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030048 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030049 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030050 char comm[64];
51
52 if (thread == NULL)
53 return -ENOMEM;
54
55 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020056 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030057 }
58
Adrian Hunterb9d266b2014-07-22 16:17:25 +030059 machine->current_tid = NULL;
60
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030061 return 0;
62}
63
David Ahern8fb598e2013-09-28 13:13:00 -060064struct machine *machine__new_host(void)
65{
66 struct machine *machine = malloc(sizeof(*machine));
67
68 if (machine != NULL) {
69 machine__init(machine, "", HOST_KERNEL_ID);
70
71 if (machine__create_kernel_maps(machine) < 0)
72 goto out_delete;
73 }
74
75 return machine;
76out_delete:
77 free(machine);
78 return NULL;
79}
80
Waiman Long8fa7d872014-09-29 16:07:28 -040081static void dsos__delete(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030082{
83 struct dso *pos, *n;
84
Waiman Long8fa7d872014-09-29 16:07:28 -040085 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -040086 RB_CLEAR_NODE(&pos->rb_node);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030087 list_del(&pos->node);
88 dso__delete(pos);
89 }
90}
91
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030092void machine__delete_threads(struct machine *machine)
93{
94 struct rb_node *nd = rb_first(&machine->threads);
95
96 while (nd) {
97 struct thread *t = rb_entry(nd, struct thread, rb_node);
98
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030099 nd = rb_next(nd);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300100 machine__remove_thread(machine, t);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300101 }
102}
103
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300104void machine__exit(struct machine *machine)
105{
106 map_groups__exit(&machine->kmaps);
107 dsos__delete(&machine->user_dsos);
108 dsos__delete(&machine->kernel_dsos);
Adrian Hunterd027b642014-07-23 14:23:00 +0300109 vdso__exit(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300110 zfree(&machine->root_dir);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300111 zfree(&machine->current_tid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300112}
113
114void machine__delete(struct machine *machine)
115{
116 machine__exit(machine);
117 free(machine);
118}
119
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300120void machines__init(struct machines *machines)
121{
122 machine__init(&machines->host, "", HOST_KERNEL_ID);
123 machines->guests = RB_ROOT;
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300124 machines->symbol_filter = NULL;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300125}
126
127void machines__exit(struct machines *machines)
128{
129 machine__exit(&machines->host);
130 /* XXX exit guest */
131}
132
133struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300134 const char *root_dir)
135{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300136 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300137 struct rb_node *parent = NULL;
138 struct machine *pos, *machine = malloc(sizeof(*machine));
139
140 if (machine == NULL)
141 return NULL;
142
143 if (machine__init(machine, root_dir, pid) != 0) {
144 free(machine);
145 return NULL;
146 }
147
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300148 machine->symbol_filter = machines->symbol_filter;
149
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300150 while (*p != NULL) {
151 parent = *p;
152 pos = rb_entry(parent, struct machine, rb_node);
153 if (pid < pos->pid)
154 p = &(*p)->rb_left;
155 else
156 p = &(*p)->rb_right;
157 }
158
159 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300160 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300161
162 return machine;
163}
164
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300165void machines__set_symbol_filter(struct machines *machines,
166 symbol_filter_t symbol_filter)
167{
168 struct rb_node *nd;
169
170 machines->symbol_filter = symbol_filter;
171 machines->host.symbol_filter = symbol_filter;
172
173 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
174 struct machine *machine = rb_entry(nd, struct machine, rb_node);
175
176 machine->symbol_filter = symbol_filter;
177 }
178}
179
Adrian Huntercfe1c412014-07-31 09:00:45 +0300180void machines__set_comm_exec(struct machines *machines, bool comm_exec)
181{
182 struct rb_node *nd;
183
184 machines->host.comm_exec = comm_exec;
185
186 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
187 struct machine *machine = rb_entry(nd, struct machine, rb_node);
188
189 machine->comm_exec = comm_exec;
190 }
191}
192
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300193struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300194{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300195 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300196 struct rb_node *parent = NULL;
197 struct machine *machine;
198 struct machine *default_machine = NULL;
199
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300200 if (pid == HOST_KERNEL_ID)
201 return &machines->host;
202
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300203 while (*p != NULL) {
204 parent = *p;
205 machine = rb_entry(parent, struct machine, rb_node);
206 if (pid < machine->pid)
207 p = &(*p)->rb_left;
208 else if (pid > machine->pid)
209 p = &(*p)->rb_right;
210 else
211 return machine;
212 if (!machine->pid)
213 default_machine = machine;
214 }
215
216 return default_machine;
217}
218
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300219struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300220{
221 char path[PATH_MAX];
222 const char *root_dir = "";
223 struct machine *machine = machines__find(machines, pid);
224
225 if (machine && (machine->pid == pid))
226 goto out;
227
228 if ((pid != HOST_KERNEL_ID) &&
229 (pid != DEFAULT_GUEST_KERNEL_ID) &&
230 (symbol_conf.guestmount)) {
231 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
232 if (access(path, R_OK)) {
233 static struct strlist *seen;
234
235 if (!seen)
236 seen = strlist__new(true, NULL);
237
238 if (!strlist__has_entry(seen, path)) {
239 pr_err("Can't access file %s\n", path);
240 strlist__add(seen, path);
241 }
242 machine = NULL;
243 goto out;
244 }
245 root_dir = path;
246 }
247
248 machine = machines__add(machines, pid, root_dir);
249out:
250 return machine;
251}
252
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300253void machines__process_guests(struct machines *machines,
254 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300255{
256 struct rb_node *nd;
257
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300258 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300259 struct machine *pos = rb_entry(nd, struct machine, rb_node);
260 process(pos, data);
261 }
262}
263
264char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
265{
266 if (machine__is_host(machine))
267 snprintf(bf, size, "[%s]", "kernel.kallsyms");
268 else if (machine__is_default_guest(machine))
269 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
270 else {
271 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
272 machine->pid);
273 }
274
275 return bf;
276}
277
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300278void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300279{
280 struct rb_node *node;
281 struct machine *machine;
282
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300283 machines->host.id_hdr_size = id_hdr_size;
284
285 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300286 machine = rb_entry(node, struct machine, rb_node);
287 machine->id_hdr_size = id_hdr_size;
288 }
289
290 return;
291}
292
Adrian Hunter29ce3612014-07-16 11:07:13 +0300293static void machine__update_thread_pid(struct machine *machine,
294 struct thread *th, pid_t pid)
295{
296 struct thread *leader;
297
298 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
299 return;
300
301 th->pid_ = pid;
302
303 if (th->pid_ == th->tid)
304 return;
305
306 leader = machine__findnew_thread(machine, th->pid_, th->pid_);
307 if (!leader)
308 goto out_err;
309
310 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300311 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300312
313 if (!leader->mg)
314 goto out_err;
315
316 if (th->mg == leader->mg)
317 return;
318
319 if (th->mg) {
320 /*
321 * Maps are created from MMAP events which provide the pid and
322 * tid. Consequently there never should be any maps on a thread
323 * with an unknown pid. Just print an error if there are.
324 */
325 if (!map_groups__empty(th->mg))
326 pr_err("Discarding thread maps for %d:%d\n",
327 th->pid_, th->tid);
328 map_groups__delete(th->mg);
329 }
330
331 th->mg = map_groups__get(leader->mg);
332
333 return;
334
335out_err:
336 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
337}
338
Adrian Hunter99d725f2013-08-26 16:00:19 +0300339static struct thread *__machine__findnew_thread(struct machine *machine,
340 pid_t pid, pid_t tid,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300341 bool create)
342{
343 struct rb_node **p = &machine->threads.rb_node;
344 struct rb_node *parent = NULL;
345 struct thread *th;
346
347 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300348 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300349 * so most of the time we dont have to look up
350 * the full rbtree:
351 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300352 th = machine->last_match;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300353 if (th != NULL) {
354 if (th->tid == tid) {
355 machine__update_thread_pid(machine, th, pid);
356 return th;
357 }
358
359 thread__zput(machine->last_match);
Adrian Hunter99d725f2013-08-26 16:00:19 +0300360 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300361
362 while (*p != NULL) {
363 parent = *p;
364 th = rb_entry(parent, struct thread, rb_node);
365
Adrian Hunter38051232013-07-04 16:20:31 +0300366 if (th->tid == tid) {
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300367 machine->last_match = thread__get(th);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300368 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300369 return th;
370 }
371
Adrian Hunter38051232013-07-04 16:20:31 +0300372 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300373 p = &(*p)->rb_left;
374 else
375 p = &(*p)->rb_right;
376 }
377
378 if (!create)
379 return NULL;
380
Adrian Hunter99d725f2013-08-26 16:00:19 +0300381 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300382 if (th != NULL) {
383 rb_link_node(&th->rb_node, parent, p);
384 rb_insert_color(&th->rb_node, &machine->threads);
Jiri Olsacddcef62014-04-09 20:54:29 +0200385
386 /*
387 * We have to initialize map_groups separately
388 * after rb tree is updated.
389 *
390 * The reason is that we call machine__findnew_thread
391 * within thread__init_map_groups to find the thread
392 * leader and that would screwed the rb tree.
393 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300394 if (thread__init_map_groups(th, machine)) {
Namhyung Kim260d8192015-01-09 09:38:12 +0900395 rb_erase(&th->rb_node, &machine->threads);
Adrian Hunter418029b2014-07-16 10:19:44 +0300396 thread__delete(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200397 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300398 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300399 /*
400 * It is now in the rbtree, get a ref
401 */
402 thread__get(th);
403 machine->last_match = thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300404 }
405
406 return th;
407}
408
Adrian Hunter314add62013-08-27 11:23:03 +0300409struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
410 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300411{
Adrian Hunter314add62013-08-27 11:23:03 +0300412 return __machine__findnew_thread(machine, pid, tid, true);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300413}
414
Jiri Olsad75e6092014-03-14 15:00:03 +0100415struct thread *machine__find_thread(struct machine *machine, pid_t pid,
416 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300417{
Jiri Olsad75e6092014-03-14 15:00:03 +0100418 return __machine__findnew_thread(machine, pid, tid, false);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300419}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300420
Adrian Huntercfe1c412014-07-31 09:00:45 +0300421struct comm *machine__thread_exec_comm(struct machine *machine,
422 struct thread *thread)
423{
424 if (machine->comm_exec)
425 return thread__exec_comm(thread);
426 else
427 return thread__comm(thread);
428}
429
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200430int machine__process_comm_event(struct machine *machine, union perf_event *event,
431 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300432{
Adrian Hunter314add62013-08-27 11:23:03 +0300433 struct thread *thread = machine__findnew_thread(machine,
434 event->comm.pid,
435 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300436 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300437
Adrian Huntercfe1c412014-07-31 09:00:45 +0300438 if (exec)
439 machine->comm_exec = true;
440
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300441 if (dump_trace)
442 perf_event__fprintf_comm(event, stdout);
443
Adrian Hunter65de51f2014-07-31 09:00:44 +0300444 if (thread == NULL ||
445 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300446 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
447 return -1;
448 }
449
450 return 0;
451}
452
453int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200454 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300455{
456 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
457 event->lost.id, event->lost.lost);
458 return 0;
459}
460
Jiri Olsaca333802015-02-17 17:29:57 +0100461static struct dso*
462machine__module_dso(struct machine *machine, struct kmod_path *m,
463 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100464{
465 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100466
Jiri Olsaca333802015-02-17 17:29:57 +0100467 dso = dsos__find(&machine->kernel_dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100468 if (!dso) {
Jiri Olsaca333802015-02-17 17:29:57 +0100469 dso = dsos__addnew(&machine->kernel_dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100470 if (dso == NULL)
471 return NULL;
472
473 if (machine__is_host(machine))
474 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
475 else
476 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
477
478 /* _KMODULE_COMP should be next to _KMODULE */
Jiri Olsaca333802015-02-17 17:29:57 +0100479 if (m->kmod && m->comp)
Jiri Olsada17ea32015-02-12 22:10:52 +0100480 dso->symtab_type++;
Jiri Olsaca333802015-02-17 17:29:57 +0100481
482 dso__set_short_name(dso, strdup(m->name), true);
483 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100484 }
485
486 return dso;
487}
488
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300489int machine__process_aux_event(struct machine *machine __maybe_unused,
490 union perf_event *event)
491{
492 if (dump_trace)
493 perf_event__fprintf_aux(event, stdout);
494 return 0;
495}
496
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300497struct map *machine__new_module(struct machine *machine, u64 start,
498 const char *filename)
499{
Jiri Olsaca333802015-02-17 17:29:57 +0100500 struct map *map = NULL;
501 struct dso *dso;
502 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300503
Jiri Olsaca333802015-02-17 17:29:57 +0100504 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300505 return NULL;
506
Jiri Olsabc84f462015-02-17 17:31:18 +0100507 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
508 m.name);
509 if (map)
510 goto out;
511
Jiri Olsaca333802015-02-17 17:29:57 +0100512 dso = machine__module_dso(machine, &m, filename);
513 if (dso == NULL)
514 goto out;
515
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300516 map = map__new2(start, dso, MAP__FUNCTION);
517 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100518 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300519
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300520 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100521
522out:
523 free(m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300524 return map;
525}
526
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300527size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300528{
529 struct rb_node *nd;
Waiman Long8fa7d872014-09-29 16:07:28 -0400530 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos.head, fp) +
531 __dsos__fprintf(&machines->host.user_dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300532
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300533 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300534 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Waiman Long8fa7d872014-09-29 16:07:28 -0400535 ret += __dsos__fprintf(&pos->kernel_dsos.head, fp);
536 ret += __dsos__fprintf(&pos->user_dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300537 }
538
539 return ret;
540}
541
Waiman Long8fa7d872014-09-29 16:07:28 -0400542size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300543 bool (skip)(struct dso *dso, int parm), int parm)
544{
Waiman Long8fa7d872014-09-29 16:07:28 -0400545 return __dsos__fprintf_buildid(&m->kernel_dsos.head, fp, skip, parm) +
546 __dsos__fprintf_buildid(&m->user_dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300547}
548
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300549size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300550 bool (skip)(struct dso *dso, int parm), int parm)
551{
552 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300553 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300554
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300555 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300556 struct machine *pos = rb_entry(nd, struct machine, rb_node);
557 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
558 }
559 return ret;
560}
561
562size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
563{
564 int i;
565 size_t printed = 0;
566 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
567
568 if (kdso->has_build_id) {
569 char filename[PATH_MAX];
570 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
571 printed += fprintf(fp, "[0] %s\n", filename);
572 }
573
574 for (i = 0; i < vmlinux_path__nr_entries; ++i)
575 printed += fprintf(fp, "[%d] %s\n",
576 i + kdso->has_build_id, vmlinux_path[i]);
577
578 return printed;
579}
580
581size_t machine__fprintf(struct machine *machine, FILE *fp)
582{
583 size_t ret = 0;
584 struct rb_node *nd;
585
586 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
587 struct thread *pos = rb_entry(nd, struct thread, rb_node);
588
589 ret += thread__fprintf(pos, fp);
590 }
591
592 return ret;
593}
594
595static struct dso *machine__get_kernel(struct machine *machine)
596{
597 const char *vmlinux_name = NULL;
598 struct dso *kernel;
599
600 if (machine__is_host(machine)) {
601 vmlinux_name = symbol_conf.vmlinux_name;
602 if (!vmlinux_name)
603 vmlinux_name = "[kernel.kallsyms]";
604
605 kernel = dso__kernel_findnew(machine, vmlinux_name,
606 "[kernel]",
607 DSO_TYPE_KERNEL);
608 } else {
609 char bf[PATH_MAX];
610
611 if (machine__is_default_guest(machine))
612 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
613 if (!vmlinux_name)
614 vmlinux_name = machine__mmap_name(machine, bf,
615 sizeof(bf));
616
617 kernel = dso__kernel_findnew(machine, vmlinux_name,
618 "[guest.kernel]",
619 DSO_TYPE_GUEST_KERNEL);
620 }
621
622 if (kernel != NULL && (!kernel->has_build_id))
623 dso__read_running_kernel_build_id(kernel, machine);
624
625 return kernel;
626}
627
628struct process_args {
629 u64 start;
630};
631
Adrian Hunter15a0a872014-01-29 16:14:38 +0200632static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
633 size_t bufsz)
634{
635 if (machine__is_default_guest(machine))
636 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
637 else
638 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
639}
640
Simon Quea93f0e52014-06-16 11:32:09 -0700641const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
642
643/* Figure out the start address of kernel map from /proc/kallsyms.
644 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
645 * symbol_name if it's not that important.
646 */
Adrian Hunter4b993752014-08-15 22:08:38 +0300647static u64 machine__get_running_kernel_start(struct machine *machine,
648 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300649{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200650 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700651 int i;
652 const char *name;
653 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300654
Adrian Hunter15a0a872014-01-29 16:14:38 +0200655 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300656
657 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
658 return 0;
659
Simon Quea93f0e52014-06-16 11:32:09 -0700660 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
661 addr = kallsyms__get_function_start(filename, name);
662 if (addr)
663 break;
664 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300665
Simon Quea93f0e52014-06-16 11:32:09 -0700666 if (symbol_name)
667 *symbol_name = name;
668
669 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300670}
671
672int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
673{
674 enum map_type type;
Adrian Hunter4b993752014-08-15 22:08:38 +0300675 u64 start = machine__get_running_kernel_start(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300676
677 for (type = 0; type < MAP__NR_TYPES; ++type) {
678 struct kmap *kmap;
679
680 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
681 if (machine->vmlinux_maps[type] == NULL)
682 return -1;
683
684 machine->vmlinux_maps[type]->map_ip =
685 machine->vmlinux_maps[type]->unmap_ip =
686 identity__map_ip;
687 kmap = map__kmap(machine->vmlinux_maps[type]);
Wang Nanba927322015-04-07 08:22:45 +0000688 if (!kmap)
689 return -1;
690
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300691 kmap->kmaps = &machine->kmaps;
692 map_groups__insert(&machine->kmaps,
693 machine->vmlinux_maps[type]);
694 }
695
696 return 0;
697}
698
699void machine__destroy_kernel_maps(struct machine *machine)
700{
701 enum map_type type;
702
703 for (type = 0; type < MAP__NR_TYPES; ++type) {
704 struct kmap *kmap;
705
706 if (machine->vmlinux_maps[type] == NULL)
707 continue;
708
709 kmap = map__kmap(machine->vmlinux_maps[type]);
710 map_groups__remove(&machine->kmaps,
711 machine->vmlinux_maps[type]);
Wang Nanba927322015-04-07 08:22:45 +0000712 if (kmap && kmap->ref_reloc_sym) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300713 /*
714 * ref_reloc_sym is shared among all maps, so free just
715 * on one of them.
716 */
717 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300718 zfree((char **)&kmap->ref_reloc_sym->name);
719 zfree(&kmap->ref_reloc_sym);
720 } else
721 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300722 }
723
724 map__delete(machine->vmlinux_maps[type]);
725 machine->vmlinux_maps[type] = NULL;
726 }
727}
728
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300729int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300730{
731 int ret = 0;
732 struct dirent **namelist = NULL;
733 int i, items = 0;
734 char path[PATH_MAX];
735 pid_t pid;
736 char *endp;
737
738 if (symbol_conf.default_guest_vmlinux_name ||
739 symbol_conf.default_guest_modules ||
740 symbol_conf.default_guest_kallsyms) {
741 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
742 }
743
744 if (symbol_conf.guestmount) {
745 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
746 if (items <= 0)
747 return -ENOENT;
748 for (i = 0; i < items; i++) {
749 if (!isdigit(namelist[i]->d_name[0])) {
750 /* Filter out . and .. */
751 continue;
752 }
753 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
754 if ((*endp != '\0') ||
755 (endp == namelist[i]->d_name) ||
756 (errno == ERANGE)) {
757 pr_debug("invalid directory (%s). Skipping.\n",
758 namelist[i]->d_name);
759 continue;
760 }
761 sprintf(path, "%s/%s/proc/kallsyms",
762 symbol_conf.guestmount,
763 namelist[i]->d_name);
764 ret = access(path, R_OK);
765 if (ret) {
766 pr_debug("Can't access file %s\n", path);
767 goto failure;
768 }
769 machines__create_kernel_maps(machines, pid);
770 }
771failure:
772 free(namelist);
773 }
774
775 return ret;
776}
777
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300778void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300779{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300780 struct rb_node *next = rb_first(&machines->guests);
781
782 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300783
784 while (next) {
785 struct machine *pos = rb_entry(next, struct machine, rb_node);
786
787 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300788 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300789 machine__delete(pos);
790 }
791}
792
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300793int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300794{
795 struct machine *machine = machines__findnew(machines, pid);
796
797 if (machine == NULL)
798 return -1;
799
800 return machine__create_kernel_maps(machine);
801}
802
803int machine__load_kallsyms(struct machine *machine, const char *filename,
804 enum map_type type, symbol_filter_t filter)
805{
806 struct map *map = machine->vmlinux_maps[type];
807 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
808
809 if (ret > 0) {
810 dso__set_loaded(map->dso, type);
811 /*
812 * Since /proc/kallsyms will have multiple sessions for the
813 * kernel, with modules between them, fixup the end of all
814 * sections.
815 */
816 __map_groups__fixup_end(&machine->kmaps, type);
817 }
818
819 return ret;
820}
821
822int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
823 symbol_filter_t filter)
824{
825 struct map *map = machine->vmlinux_maps[type];
826 int ret = dso__load_vmlinux_path(map->dso, map, filter);
827
Adrian Hunter39b12f782013-08-07 14:38:47 +0300828 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300829 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300830
831 return ret;
832}
833
834static void map_groups__fixup_end(struct map_groups *mg)
835{
836 int i;
837 for (i = 0; i < MAP__NR_TYPES; ++i)
838 __map_groups__fixup_end(mg, i);
839}
840
841static char *get_kernel_version(const char *root_dir)
842{
843 char version[PATH_MAX];
844 FILE *file;
845 char *name, *tmp;
846 const char *prefix = "Linux version ";
847
848 sprintf(version, "%s/proc/version", root_dir);
849 file = fopen(version, "r");
850 if (!file)
851 return NULL;
852
853 version[0] = '\0';
854 tmp = fgets(version, sizeof(version), file);
855 fclose(file);
856
857 name = strstr(version, prefix);
858 if (!name)
859 return NULL;
860 name += strlen(prefix);
861 tmp = strchr(name, ' ');
862 if (tmp)
863 *tmp = '\0';
864
865 return strdup(name);
866}
867
Jiri Olsabb58a8a2015-02-12 22:20:01 +0100868static bool is_kmod_dso(struct dso *dso)
869{
870 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
871 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
872}
873
874static int map_groups__set_module_path(struct map_groups *mg, const char *path,
875 struct kmod_path *m)
876{
877 struct map *map;
878 char *long_name;
879
880 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
881 if (map == NULL)
882 return 0;
883
884 long_name = strdup(path);
885 if (long_name == NULL)
886 return -ENOMEM;
887
888 dso__set_long_name(map->dso, long_name, true);
889 dso__kernel_module_get_build_id(map->dso, "");
890
891 /*
892 * Full name could reveal us kmod compression, so
893 * we need to update the symtab_type if needed.
894 */
895 if (m->comp && is_kmod_dso(map->dso))
896 map->dso->symtab_type++;
897
898 return 0;
899}
900
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300901static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -0400902 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300903{
904 struct dirent *dent;
905 DIR *dir = opendir(dir_name);
906 int ret = 0;
907
908 if (!dir) {
909 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
910 return -1;
911 }
912
913 while ((dent = readdir(dir)) != NULL) {
914 char path[PATH_MAX];
915 struct stat st;
916
917 /*sshfs might return bad dent->d_type, so we have to stat*/
918 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
919 if (stat(path, &st))
920 continue;
921
922 if (S_ISDIR(st.st_mode)) {
923 if (!strcmp(dent->d_name, ".") ||
924 !strcmp(dent->d_name, ".."))
925 continue;
926
Richard Yao61d42902014-04-26 13:17:55 -0400927 /* Do not follow top-level source and build symlinks */
928 if (depth == 0) {
929 if (!strcmp(dent->d_name, "source") ||
930 !strcmp(dent->d_name, "build"))
931 continue;
932 }
933
934 ret = map_groups__set_modules_path_dir(mg, path,
935 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300936 if (ret < 0)
937 goto out;
938 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +0100939 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300940
Jiri Olsabb58a8a2015-02-12 22:20:01 +0100941 ret = kmod_path__parse_name(&m, dent->d_name);
942 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300943 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +0100944
945 if (m.kmod)
946 ret = map_groups__set_module_path(mg, path, &m);
947
948 free(m.name);
949
950 if (ret)
951 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300952 }
953 }
954
955out:
956 closedir(dir);
957 return ret;
958}
959
960static int machine__set_modules_path(struct machine *machine)
961{
962 char *version;
963 char modules_path[PATH_MAX];
964
965 version = get_kernel_version(machine->root_dir);
966 if (!version)
967 return -1;
968
Richard Yao61d42902014-04-26 13:17:55 -0400969 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300970 machine->root_dir, version);
971 free(version);
972
Richard Yao61d42902014-04-26 13:17:55 -0400973 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300974}
975
Adrian Hunter316d70d2013-10-08 11:45:48 +0300976static int machine__create_module(void *arg, const char *name, u64 start)
977{
978 struct machine *machine = arg;
979 struct map *map;
980
981 map = machine__new_module(machine, start, name);
982 if (map == NULL)
983 return -1;
984
985 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
986
987 return 0;
988}
989
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300990static int machine__create_modules(struct machine *machine)
991{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300992 const char *modules;
993 char path[PATH_MAX];
994
Adrian Hunterf4be9042013-09-22 13:22:09 +0300995 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300996 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +0300997 } else {
998 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300999 modules = path;
1000 }
1001
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001002 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001003 return -1;
1004
Adrian Hunter316d70d2013-10-08 11:45:48 +03001005 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001006 return -1;
1007
Adrian Hunter316d70d2013-10-08 11:45:48 +03001008 if (!machine__set_modules_path(machine))
1009 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001010
Adrian Hunter316d70d2013-10-08 11:45:48 +03001011 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001012
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001013 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001014}
1015
1016int machine__create_kernel_maps(struct machine *machine)
1017{
1018 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001019 const char *name;
Adrian Hunter4b993752014-08-15 22:08:38 +03001020 u64 addr = machine__get_running_kernel_start(machine, &name);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001021 if (!addr)
1022 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001023
1024 if (kernel == NULL ||
1025 __machine__create_kernel_maps(machine, kernel) < 0)
1026 return -1;
1027
1028 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1029 if (machine__is_host(machine))
1030 pr_debug("Problems creating module maps, "
1031 "continuing anyway...\n");
1032 else
1033 pr_debug("Problems creating module maps for guest %d, "
1034 "continuing anyway...\n", machine->pid);
1035 }
1036
1037 /*
1038 * Now that we have all the maps created, just set the ->end of them:
1039 */
1040 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001041
1042 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
1043 addr)) {
1044 machine__destroy_kernel_maps(machine);
1045 return -1;
1046 }
1047
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001048 return 0;
1049}
1050
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001051static void machine__set_kernel_mmap_len(struct machine *machine,
1052 union perf_event *event)
1053{
Namhyung Kim4552cf0f2012-11-07 16:27:10 +09001054 int i;
1055
1056 for (i = 0; i < MAP__NR_TYPES; i++) {
1057 machine->vmlinux_maps[i]->start = event->mmap.start;
1058 machine->vmlinux_maps[i]->end = (event->mmap.start +
1059 event->mmap.len);
1060 /*
1061 * Be a bit paranoid here, some perf.data file came with
1062 * a zero sized synthesized MMAP event for the kernel.
1063 */
1064 if (machine->vmlinux_maps[i]->end == 0)
1065 machine->vmlinux_maps[i]->end = ~0ULL;
1066 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001067}
1068
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001069static bool machine__uses_kcore(struct machine *machine)
1070{
1071 struct dso *dso;
1072
Waiman Long8fa7d872014-09-29 16:07:28 -04001073 list_for_each_entry(dso, &machine->kernel_dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001074 if (dso__is_kcore(dso))
1075 return true;
1076 }
1077
1078 return false;
1079}
1080
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001081static int machine__process_kernel_mmap_event(struct machine *machine,
1082 union perf_event *event)
1083{
1084 struct map *map;
1085 char kmmap_prefix[PATH_MAX];
1086 enum dso_kernel_type kernel_type;
1087 bool is_kernel_mmap;
1088
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001089 /* If we have maps from kcore then we do not need or want any others */
1090 if (machine__uses_kcore(machine))
1091 return 0;
1092
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001093 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1094 if (machine__is_host(machine))
1095 kernel_type = DSO_TYPE_KERNEL;
1096 else
1097 kernel_type = DSO_TYPE_GUEST_KERNEL;
1098
1099 is_kernel_mmap = memcmp(event->mmap.filename,
1100 kmmap_prefix,
1101 strlen(kmmap_prefix) - 1) == 0;
1102 if (event->mmap.filename[0] == '/' ||
1103 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001104 map = machine__new_module(machine, event->mmap.start,
1105 event->mmap.filename);
1106 if (map == NULL)
1107 goto out_problem;
1108
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001109 map->end = map->start + event->mmap.len;
1110 } else if (is_kernel_mmap) {
1111 const char *symbol_name = (event->mmap.filename +
1112 strlen(kmmap_prefix));
1113 /*
1114 * Should be there already, from the build-id table in
1115 * the header.
1116 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001117 struct dso *kernel = NULL;
1118 struct dso *dso;
1119
1120 list_for_each_entry(dso, &machine->kernel_dsos.head, node) {
Jiri Olsae746b3e2015-02-12 22:16:34 +01001121 if (is_kernel_module(dso->long_name))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001122 continue;
1123
1124 kernel = dso;
1125 break;
1126 }
1127
1128 if (kernel == NULL)
1129 kernel = __dsos__findnew(&machine->kernel_dsos,
1130 kmmap_prefix);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001131 if (kernel == NULL)
1132 goto out_problem;
1133
1134 kernel->kernel = kernel_type;
1135 if (__machine__create_kernel_maps(machine, kernel) < 0)
1136 goto out_problem;
1137
Namhyung Kim330dfa22014-11-18 13:30:28 +09001138 if (strstr(kernel->long_name, "vmlinux"))
1139 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001140
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001141 machine__set_kernel_mmap_len(machine, event);
1142
1143 /*
1144 * Avoid using a zero address (kptr_restrict) for the ref reloc
1145 * symbol. Effectively having zero here means that at record
1146 * time /proc/sys/kernel/kptr_restrict was non zero.
1147 */
1148 if (event->mmap.pgoff != 0) {
1149 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1150 symbol_name,
1151 event->mmap.pgoff);
1152 }
1153
1154 if (machine__is_default_guest(machine)) {
1155 /*
1156 * preload dso of guest kernel and modules
1157 */
1158 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
1159 NULL);
1160 }
1161 }
1162 return 0;
1163out_problem:
1164 return -1;
1165}
1166
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001167int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001168 union perf_event *event,
1169 struct perf_sample *sample __maybe_unused)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001170{
1171 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1172 struct thread *thread;
1173 struct map *map;
1174 enum map_type type;
1175 int ret = 0;
1176
1177 if (dump_trace)
1178 perf_event__fprintf_mmap2(event, stdout);
1179
1180 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1181 cpumode == PERF_RECORD_MISC_KERNEL) {
1182 ret = machine__process_kernel_mmap_event(machine, event);
1183 if (ret < 0)
1184 goto out_problem;
1185 return 0;
1186 }
1187
1188 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001189 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001190 if (thread == NULL)
1191 goto out_problem;
1192
1193 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1194 type = MAP__VARIABLE;
1195 else
1196 type = MAP__FUNCTION;
1197
Adrian Hunter2a030682014-07-22 16:17:53 +03001198 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001199 event->mmap2.len, event->mmap2.pgoff,
1200 event->mmap2.pid, event->mmap2.maj,
1201 event->mmap2.min, event->mmap2.ino,
1202 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001203 event->mmap2.prot,
1204 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001205 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001206
1207 if (map == NULL)
1208 goto out_problem;
1209
1210 thread__insert_map(thread, map);
1211 return 0;
1212
1213out_problem:
1214 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1215 return 0;
1216}
1217
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001218int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1219 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001220{
1221 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1222 struct thread *thread;
1223 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001224 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001225 int ret = 0;
1226
1227 if (dump_trace)
1228 perf_event__fprintf_mmap(event, stdout);
1229
1230 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1231 cpumode == PERF_RECORD_MISC_KERNEL) {
1232 ret = machine__process_kernel_mmap_event(machine, event);
1233 if (ret < 0)
1234 goto out_problem;
1235 return 0;
1236 }
1237
Adrian Hunter314add62013-08-27 11:23:03 +03001238 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001239 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001240 if (thread == NULL)
1241 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001242
1243 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1244 type = MAP__VARIABLE;
1245 else
1246 type = MAP__FUNCTION;
1247
Adrian Hunter2a030682014-07-22 16:17:53 +03001248 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001249 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001250 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001251 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001252 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001253
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001254 if (map == NULL)
1255 goto out_problem;
1256
1257 thread__insert_map(thread, map);
1258 return 0;
1259
1260out_problem:
1261 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1262 return 0;
1263}
1264
He Kuang5e78c692015-04-10 17:35:00 +08001265void machine__remove_thread(struct machine *machine, struct thread *th)
David Ahern236a3bb2013-08-14 08:49:27 -06001266{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001267 if (machine->last_match == th)
1268 thread__zput(machine->last_match);
1269
David Ahern236a3bb2013-08-14 08:49:27 -06001270 rb_erase(&th->rb_node, &machine->threads);
1271 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001272 * Move it first to the dead_threads list, then drop the reference,
1273 * if this is the last reference, then the thread__delete destructor
1274 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001275 */
1276 list_add_tail(&th->node, &machine->dead_threads);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001277 thread__put(th);
David Ahern236a3bb2013-08-14 08:49:27 -06001278}
1279
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001280int machine__process_fork_event(struct machine *machine, union perf_event *event,
1281 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001282{
Jiri Olsad75e6092014-03-14 15:00:03 +01001283 struct thread *thread = machine__find_thread(machine,
1284 event->fork.pid,
1285 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001286 struct thread *parent = machine__findnew_thread(machine,
1287 event->fork.ppid,
1288 event->fork.ptid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001289
David Ahern236a3bb2013-08-14 08:49:27 -06001290 /* if a thread currently exists for the thread id remove it */
1291 if (thread != NULL)
1292 machine__remove_thread(machine, thread);
1293
Adrian Hunter314add62013-08-27 11:23:03 +03001294 thread = machine__findnew_thread(machine, event->fork.pid,
1295 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001296 if (dump_trace)
1297 perf_event__fprintf_task(event, stdout);
1298
1299 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001300 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001301 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1302 return -1;
1303 }
1304
1305 return 0;
1306}
1307
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001308int machine__process_exit_event(struct machine *machine, union perf_event *event,
1309 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001310{
Jiri Olsad75e6092014-03-14 15:00:03 +01001311 struct thread *thread = machine__find_thread(machine,
1312 event->fork.pid,
1313 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001314
1315 if (dump_trace)
1316 perf_event__fprintf_task(event, stdout);
1317
1318 if (thread != NULL)
David Ahern236a3bb2013-08-14 08:49:27 -06001319 thread__exited(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001320
1321 return 0;
1322}
1323
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001324int machine__process_event(struct machine *machine, union perf_event *event,
1325 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001326{
1327 int ret;
1328
1329 switch (event->header.type) {
1330 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001331 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001332 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001333 ret = machine__process_mmap_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001334 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001335 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001336 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001337 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001338 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001339 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001340 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001341 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001342 case PERF_RECORD_AUX:
1343 ret = machine__process_aux_event(machine, event); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001344 default:
1345 ret = -1;
1346 break;
1347 }
1348
1349 return ret;
1350}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001351
Greg Priceb21484f2012-12-06 21:48:05 -08001352static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001353{
Greg Priceb21484f2012-12-06 21:48:05 -08001354 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001355 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001356 return 0;
1357}
1358
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001359static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001360 struct addr_map_symbol *ams,
1361 u64 ip)
1362{
1363 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001364
1365 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001366 /*
1367 * We cannot use the header.misc hint to determine whether a
1368 * branch stack address is user, kernel, guest, hypervisor.
1369 * Branches may straddle the kernel/user/hypervisor boundaries.
1370 * Thus, we have to try consecutively until we find a match
1371 * or else, the symbol is unknown
1372 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001373 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001374
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001375 ams->addr = ip;
1376 ams->al_addr = al.addr;
1377 ams->sym = al.sym;
1378 ams->map = al.map;
1379}
1380
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001381static void ip__resolve_data(struct thread *thread,
Stephane Eranian98a3b322013-01-24 16:10:35 +01001382 u8 m, struct addr_map_symbol *ams, u64 addr)
1383{
1384 struct addr_location al;
1385
1386 memset(&al, 0, sizeof(al));
1387
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001388 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001389 if (al.map == NULL) {
1390 /*
1391 * some shared data regions have execute bit set which puts
1392 * their mapping in the MAP__FUNCTION type array.
1393 * Check there as a fallback option before dropping the sample.
1394 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001395 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001396 }
1397
Stephane Eranian98a3b322013-01-24 16:10:35 +01001398 ams->addr = addr;
1399 ams->al_addr = al.addr;
1400 ams->sym = al.sym;
1401 ams->map = al.map;
1402}
1403
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001404struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1405 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001406{
1407 struct mem_info *mi = zalloc(sizeof(*mi));
1408
1409 if (!mi)
1410 return NULL;
1411
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001412 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1413 ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001414 mi->data_src.val = sample->data_src;
1415
1416 return mi;
1417}
1418
Andi Kleen37592b82014-11-12 18:05:19 -08001419static int add_callchain_ip(struct thread *thread,
1420 struct symbol **parent,
1421 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001422 u8 *cpumode,
Andi Kleen37592b82014-11-12 18:05:19 -08001423 u64 ip)
1424{
1425 struct addr_location al;
1426
1427 al.filtered = 0;
1428 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001429 if (!cpumode) {
Andi Kleen8b7bad52014-11-12 18:05:20 -08001430 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1431 ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001432 } else {
Kan Liang2e777842014-12-02 10:06:53 -05001433 if (ip >= PERF_CONTEXT_MAX) {
1434 switch (ip) {
1435 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001436 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05001437 break;
1438 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001439 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05001440 break;
1441 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001442 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05001443 break;
1444 default:
1445 pr_debug("invalid callchain context: "
1446 "%"PRId64"\n", (s64) ip);
1447 /*
1448 * It seems the callchain is corrupted.
1449 * Discard all.
1450 */
1451 callchain_cursor_reset(&callchain_cursor);
1452 return 1;
1453 }
1454 return 0;
1455 }
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001456 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1457 ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05001458 }
1459
Andi Kleen37592b82014-11-12 18:05:19 -08001460 if (al.sym != NULL) {
1461 if (sort__has_parent && !*parent &&
1462 symbol__match_regex(al.sym, &parent_regex))
1463 *parent = al.sym;
1464 else if (have_ignore_callees && root_al &&
1465 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1466 /* Treat this symbol as the root,
1467 forgetting its callees. */
1468 *root_al = al;
1469 callchain_cursor_reset(&callchain_cursor);
1470 }
1471 }
1472
Andi Kleen55501712014-11-12 18:05:21 -08001473 return callchain_cursor_append(&callchain_cursor, al.addr, al.map, al.sym);
Andi Kleen37592b82014-11-12 18:05:19 -08001474}
1475
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001476struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1477 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001478{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001479 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001480 const struct branch_stack *bs = sample->branch_stack;
1481 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001482
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001483 if (!bi)
1484 return NULL;
1485
1486 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001487 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1488 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001489 bi[i].flags = bs->entries[i].flags;
1490 }
1491 return bi;
1492}
1493
Andi Kleen8b7bad52014-11-12 18:05:20 -08001494#define CHASHSZ 127
1495#define CHASHBITS 7
1496#define NO_ENTRY 0xff
1497
1498#define PERF_MAX_BRANCH_DEPTH 127
1499
1500/* Remove loops. */
1501static int remove_loops(struct branch_entry *l, int nr)
1502{
1503 int i, j, off;
1504 unsigned char chash[CHASHSZ];
1505
1506 memset(chash, NO_ENTRY, sizeof(chash));
1507
1508 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1509
1510 for (i = 0; i < nr; i++) {
1511 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1512
1513 /* no collision handling for now */
1514 if (chash[h] == NO_ENTRY) {
1515 chash[h] = i;
1516 } else if (l[chash[h]].from == l[i].from) {
1517 bool is_loop = true;
1518 /* check if it is a real loop */
1519 off = 0;
1520 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1521 if (l[j].from != l[i + off].from) {
1522 is_loop = false;
1523 break;
1524 }
1525 if (is_loop) {
1526 memmove(l + i, l + i + off,
1527 (nr - (i + off)) * sizeof(*l));
1528 nr -= off;
1529 }
1530 }
1531 }
1532 return nr;
1533}
1534
Kan Liang384b6052015-01-05 13:23:05 -05001535/*
1536 * Recolve LBR callstack chain sample
1537 * Return:
1538 * 1 on success get LBR callchain information
1539 * 0 no available LBR callchain information, should try fp
1540 * negative error code on other errors.
1541 */
1542static int resolve_lbr_callchain_sample(struct thread *thread,
1543 struct perf_sample *sample,
1544 struct symbol **parent,
1545 struct addr_location *root_al,
1546 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001547{
Kan Liang384b6052015-01-05 13:23:05 -05001548 struct ip_callchain *chain = sample->callchain;
1549 int chain_nr = min(max_stack, (int)chain->nr);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001550 u8 cpumode = PERF_RECORD_MISC_USER;
Kan Liang384b6052015-01-05 13:23:05 -05001551 int i, j, err;
1552 u64 ip;
1553
1554 for (i = 0; i < chain_nr; i++) {
1555 if (chain->ips[i] == PERF_CONTEXT_USER)
1556 break;
1557 }
1558
1559 /* LBR only affects the user callchain */
1560 if (i != chain_nr) {
1561 struct branch_stack *lbr_stack = sample->branch_stack;
1562 int lbr_nr = lbr_stack->nr;
1563 /*
1564 * LBR callstack can only get user call chain.
1565 * The mix_chain_nr is kernel call chain
1566 * number plus LBR user call chain number.
1567 * i is kernel call chain number,
1568 * 1 is PERF_CONTEXT_USER,
1569 * lbr_nr + 1 is the user call chain number.
1570 * For details, please refer to the comments
1571 * in callchain__printf
1572 */
1573 int mix_chain_nr = i + 1 + lbr_nr + 1;
1574
1575 if (mix_chain_nr > PERF_MAX_STACK_DEPTH + PERF_MAX_BRANCH_DEPTH) {
1576 pr_warning("corrupted callchain. skipping...\n");
1577 return 0;
1578 }
1579
1580 for (j = 0; j < mix_chain_nr; j++) {
1581 if (callchain_param.order == ORDER_CALLEE) {
1582 if (j < i + 1)
1583 ip = chain->ips[j];
1584 else if (j > i + 1)
1585 ip = lbr_stack->entries[j - i - 2].from;
1586 else
1587 ip = lbr_stack->entries[0].to;
1588 } else {
1589 if (j < lbr_nr)
1590 ip = lbr_stack->entries[lbr_nr - j - 1].from;
1591 else if (j > lbr_nr)
1592 ip = chain->ips[i + 1 - (j - lbr_nr)];
1593 else
1594 ip = lbr_stack->entries[0].to;
1595 }
1596
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001597 err = add_callchain_ip(thread, parent, root_al, &cpumode, ip);
Kan Liang384b6052015-01-05 13:23:05 -05001598 if (err)
1599 return (err < 0) ? err : 0;
1600 }
1601 return 1;
1602 }
1603
1604 return 0;
1605}
1606
1607static int thread__resolve_callchain_sample(struct thread *thread,
1608 struct perf_evsel *evsel,
1609 struct perf_sample *sample,
1610 struct symbol **parent,
1611 struct addr_location *root_al,
1612 int max_stack)
1613{
1614 struct branch_stack *branch = sample->branch_stack;
1615 struct ip_callchain *chain = sample->callchain;
Waiman Long91e95612013-10-18 10:38:48 -04001616 int chain_nr = min(max_stack, (int)chain->nr);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001617 u8 cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05001618 int i, j, err;
Andi Kleen8b7bad52014-11-12 18:05:20 -08001619 int skip_idx = -1;
1620 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001621
Kan Liang384b6052015-01-05 13:23:05 -05001622 callchain_cursor_reset(&callchain_cursor);
1623
1624 if (has_branch_callstack(evsel)) {
1625 err = resolve_lbr_callchain_sample(thread, sample, parent,
1626 root_al, max_stack);
1627 if (err)
1628 return (err < 0) ? err : 0;
1629 }
1630
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001631 /*
1632 * Based on DWARF debug information, some architectures skip
1633 * a callchain entry saved by the kernel.
1634 */
Andi Kleen8b7bad52014-11-12 18:05:20 -08001635 if (chain->nr < PERF_MAX_STACK_DEPTH)
1636 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001637
Andi Kleen8b7bad52014-11-12 18:05:20 -08001638 /*
1639 * Add branches to call stack for easier browsing. This gives
1640 * more context for a sample than just the callers.
1641 *
1642 * This uses individual histograms of paths compared to the
1643 * aggregated histograms the normal LBR mode uses.
1644 *
1645 * Limitations for now:
1646 * - No extra filters
1647 * - No annotations (should annotate somehow)
1648 */
1649
1650 if (branch && callchain_param.branch_callstack) {
1651 int nr = min(max_stack, (int)branch->nr);
1652 struct branch_entry be[nr];
1653
1654 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
1655 pr_warning("corrupted branch chain. skipping...\n");
1656 goto check_calls;
1657 }
1658
1659 for (i = 0; i < nr; i++) {
1660 if (callchain_param.order == ORDER_CALLEE) {
1661 be[i] = branch->entries[i];
1662 /*
1663 * Check for overlap into the callchain.
1664 * The return address is one off compared to
1665 * the branch entry. To adjust for this
1666 * assume the calling instruction is not longer
1667 * than 8 bytes.
1668 */
1669 if (i == skip_idx ||
1670 chain->ips[first_call] >= PERF_CONTEXT_MAX)
1671 first_call++;
1672 else if (be[i].from < chain->ips[first_call] &&
1673 be[i].from >= chain->ips[first_call] - 8)
1674 first_call++;
1675 } else
1676 be[i] = branch->entries[branch->nr - i - 1];
1677 }
1678
1679 nr = remove_loops(be, nr);
1680
1681 for (i = 0; i < nr; i++) {
1682 err = add_callchain_ip(thread, parent, root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001683 NULL, be[i].to);
Andi Kleen8b7bad52014-11-12 18:05:20 -08001684 if (!err)
1685 err = add_callchain_ip(thread, parent, root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001686 NULL, be[i].from);
Andi Kleen8b7bad52014-11-12 18:05:20 -08001687 if (err == -EINVAL)
1688 break;
1689 if (err)
1690 return err;
1691 }
1692 chain_nr -= nr;
1693 }
1694
1695check_calls:
1696 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1697 pr_warning("corrupted callchain. skipping...\n");
1698 return 0;
1699 }
1700
1701 for (i = first_call; i < chain_nr; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001702 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001703
1704 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001705 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001706 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001707 j = chain->nr - i - 1;
1708
1709#ifdef HAVE_SKIP_CALLCHAIN_IDX
1710 if (j == skip_idx)
1711 continue;
1712#endif
1713 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001714
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001715 err = add_callchain_ip(thread, parent, root_al, &cpumode, ip);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001716
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001717 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05001718 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001719 }
1720
1721 return 0;
1722}
1723
1724static int unwind_entry(struct unwind_entry *entry, void *arg)
1725{
1726 struct callchain_cursor *cursor = arg;
1727 return callchain_cursor_append(cursor, entry->ip,
1728 entry->map, entry->sym);
1729}
1730
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -03001731int thread__resolve_callchain(struct thread *thread,
1732 struct perf_evsel *evsel,
1733 struct perf_sample *sample,
1734 struct symbol **parent,
1735 struct addr_location *root_al,
1736 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001737{
Kan Liang384b6052015-01-05 13:23:05 -05001738 int ret = thread__resolve_callchain_sample(thread, evsel,
1739 sample, parent,
1740 root_al, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001741 if (ret)
1742 return ret;
1743
1744 /* Can we do dwarf post unwind? */
1745 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1746 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1747 return 0;
1748
1749 /* Bail out if nothing was captured. */
1750 if ((!sample->user_regs.regs) ||
1751 (!sample->user_stack.size))
1752 return 0;
1753
Arnaldo Carvalho de Melodd8c17a2014-10-23 16:42:19 -03001754 return unwind__get_entries(unwind_entry, &callchain_cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01001755 thread, sample, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001756
1757}
David Ahern35feee12013-09-28 13:12:58 -06001758
1759int machine__for_each_thread(struct machine *machine,
1760 int (*fn)(struct thread *thread, void *p),
1761 void *priv)
1762{
1763 struct rb_node *nd;
1764 struct thread *thread;
1765 int rc = 0;
1766
1767 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1768 thread = rb_entry(nd, struct thread, rb_node);
1769 rc = fn(thread, priv);
1770 if (rc != 0)
1771 return rc;
1772 }
1773
1774 list_for_each_entry(thread, &machine->dead_threads, node) {
1775 rc = fn(thread, priv);
1776 if (rc != 0)
1777 return rc;
1778 }
1779 return rc;
1780}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001781
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001782int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001783 struct target *target, struct thread_map *threads,
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001784 perf_event__handler_t process, bool data_mmap)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001785{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001786 if (target__has_task(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001787 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001788 else if (target__has_cpu(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001789 return perf_event__synthesize_threads(tool, process, machine, data_mmap);
1790 /* command specified */
1791 return 0;
1792}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03001793
1794pid_t machine__get_current_tid(struct machine *machine, int cpu)
1795{
1796 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
1797 return -1;
1798
1799 return machine->current_tid[cpu];
1800}
1801
1802int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
1803 pid_t tid)
1804{
1805 struct thread *thread;
1806
1807 if (cpu < 0)
1808 return -EINVAL;
1809
1810 if (!machine->current_tid) {
1811 int i;
1812
1813 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
1814 if (!machine->current_tid)
1815 return -ENOMEM;
1816 for (i = 0; i < MAX_NR_CPUS; i++)
1817 machine->current_tid[i] = -1;
1818 }
1819
1820 if (cpu >= MAX_NR_CPUS) {
1821 pr_err("Requested CPU %d too large. ", cpu);
1822 pr_err("Consider raising MAX_NR_CPUS\n");
1823 return -EINVAL;
1824 }
1825
1826 machine->current_tid[cpu] = tid;
1827
1828 thread = machine__findnew_thread(machine, pid, tid);
1829 if (!thread)
1830 return -ENOMEM;
1831
1832 thread->cpu = cpu;
1833
1834 return 0;
1835}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03001836
1837int machine__get_kernel_start(struct machine *machine)
1838{
1839 struct map *map = machine__kernel_map(machine, MAP__FUNCTION);
1840 int err = 0;
1841
1842 /*
1843 * The only addresses above 2^63 are kernel addresses of a 64-bit
1844 * kernel. Note that addresses are unsigned so that on a 32-bit system
1845 * all addresses including kernel addresses are less than 2^32. In
1846 * that case (32-bit system), if the kernel mapping is unknown, all
1847 * addresses will be assumed to be in user space - see
1848 * machine__kernel_ip().
1849 */
1850 machine->kernel_start = 1ULL << 63;
1851 if (map) {
1852 err = map__load(map, machine->symbol_filter);
1853 if (map->start)
1854 machine->kernel_start = map->start;
1855 }
1856 return err;
1857}