blob: 49a75ec4c47bd582a96290092d77ee1d62bb0924 [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"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030015
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030016int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
17{
18 map_groups__init(&machine->kmaps);
19 RB_CLEAR_NODE(&machine->rb_node);
Waiman Long8fa7d872014-09-29 16:07:28 -040020 INIT_LIST_HEAD(&machine->user_dsos.head);
21 INIT_LIST_HEAD(&machine->kernel_dsos.head);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030022
23 machine->threads = RB_ROOT;
24 INIT_LIST_HEAD(&machine->dead_threads);
25 machine->last_match = NULL;
26
Adrian Hunterd027b642014-07-23 14:23:00 +030027 machine->vdso_info = NULL;
28
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030029 machine->kmaps.machine = machine;
30 machine->pid = pid;
31
Adrian Hunter611a5ce2013-08-08 14:32:20 +030032 machine->symbol_filter = NULL;
Jiri Olsa14bd6d22014-01-07 13:47:19 +010033 machine->id_hdr_size = 0;
Adrian Huntercfe1c412014-07-31 09:00:45 +030034 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030035 machine->kernel_start = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030036
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030037 machine->root_dir = strdup(root_dir);
38 if (machine->root_dir == NULL)
39 return -ENOMEM;
40
41 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030042 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030043 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030044 char comm[64];
45
46 if (thread == NULL)
47 return -ENOMEM;
48
49 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020050 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030051 }
52
Adrian Hunterb9d266b2014-07-22 16:17:25 +030053 machine->current_tid = NULL;
54
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030055 return 0;
56}
57
David Ahern8fb598e2013-09-28 13:13:00 -060058struct machine *machine__new_host(void)
59{
60 struct machine *machine = malloc(sizeof(*machine));
61
62 if (machine != NULL) {
63 machine__init(machine, "", HOST_KERNEL_ID);
64
65 if (machine__create_kernel_maps(machine) < 0)
66 goto out_delete;
67 }
68
69 return machine;
70out_delete:
71 free(machine);
72 return NULL;
73}
74
Waiman Long8fa7d872014-09-29 16:07:28 -040075static void dsos__delete(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030076{
77 struct dso *pos, *n;
78
Waiman Long8fa7d872014-09-29 16:07:28 -040079 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030080 list_del(&pos->node);
81 dso__delete(pos);
82 }
83}
84
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030085void machine__delete_dead_threads(struct machine *machine)
86{
87 struct thread *n, *t;
88
89 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
90 list_del(&t->node);
91 thread__delete(t);
92 }
93}
94
95void machine__delete_threads(struct machine *machine)
96{
97 struct rb_node *nd = rb_first(&machine->threads);
98
99 while (nd) {
100 struct thread *t = rb_entry(nd, struct thread, rb_node);
101
102 rb_erase(&t->rb_node, &machine->threads);
103 nd = rb_next(nd);
104 thread__delete(t);
105 }
106}
107
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300108void machine__exit(struct machine *machine)
109{
110 map_groups__exit(&machine->kmaps);
111 dsos__delete(&machine->user_dsos);
112 dsos__delete(&machine->kernel_dsos);
Adrian Hunterd027b642014-07-23 14:23:00 +0300113 vdso__exit(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300114 zfree(&machine->root_dir);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300115 zfree(&machine->current_tid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300116}
117
118void machine__delete(struct machine *machine)
119{
120 machine__exit(machine);
121 free(machine);
122}
123
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300124void machines__init(struct machines *machines)
125{
126 machine__init(&machines->host, "", HOST_KERNEL_ID);
127 machines->guests = RB_ROOT;
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300128 machines->symbol_filter = NULL;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300129}
130
131void machines__exit(struct machines *machines)
132{
133 machine__exit(&machines->host);
134 /* XXX exit guest */
135}
136
137struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300138 const char *root_dir)
139{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300140 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300141 struct rb_node *parent = NULL;
142 struct machine *pos, *machine = malloc(sizeof(*machine));
143
144 if (machine == NULL)
145 return NULL;
146
147 if (machine__init(machine, root_dir, pid) != 0) {
148 free(machine);
149 return NULL;
150 }
151
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300152 machine->symbol_filter = machines->symbol_filter;
153
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300154 while (*p != NULL) {
155 parent = *p;
156 pos = rb_entry(parent, struct machine, rb_node);
157 if (pid < pos->pid)
158 p = &(*p)->rb_left;
159 else
160 p = &(*p)->rb_right;
161 }
162
163 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300164 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300165
166 return machine;
167}
168
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300169void machines__set_symbol_filter(struct machines *machines,
170 symbol_filter_t symbol_filter)
171{
172 struct rb_node *nd;
173
174 machines->symbol_filter = symbol_filter;
175 machines->host.symbol_filter = symbol_filter;
176
177 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
178 struct machine *machine = rb_entry(nd, struct machine, rb_node);
179
180 machine->symbol_filter = symbol_filter;
181 }
182}
183
Adrian Huntercfe1c412014-07-31 09:00:45 +0300184void machines__set_comm_exec(struct machines *machines, bool comm_exec)
185{
186 struct rb_node *nd;
187
188 machines->host.comm_exec = comm_exec;
189
190 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
191 struct machine *machine = rb_entry(nd, struct machine, rb_node);
192
193 machine->comm_exec = comm_exec;
194 }
195}
196
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300197struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300198{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300199 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300200 struct rb_node *parent = NULL;
201 struct machine *machine;
202 struct machine *default_machine = NULL;
203
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300204 if (pid == HOST_KERNEL_ID)
205 return &machines->host;
206
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300207 while (*p != NULL) {
208 parent = *p;
209 machine = rb_entry(parent, struct machine, rb_node);
210 if (pid < machine->pid)
211 p = &(*p)->rb_left;
212 else if (pid > machine->pid)
213 p = &(*p)->rb_right;
214 else
215 return machine;
216 if (!machine->pid)
217 default_machine = machine;
218 }
219
220 return default_machine;
221}
222
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300223struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300224{
225 char path[PATH_MAX];
226 const char *root_dir = "";
227 struct machine *machine = machines__find(machines, pid);
228
229 if (machine && (machine->pid == pid))
230 goto out;
231
232 if ((pid != HOST_KERNEL_ID) &&
233 (pid != DEFAULT_GUEST_KERNEL_ID) &&
234 (symbol_conf.guestmount)) {
235 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
236 if (access(path, R_OK)) {
237 static struct strlist *seen;
238
239 if (!seen)
240 seen = strlist__new(true, NULL);
241
242 if (!strlist__has_entry(seen, path)) {
243 pr_err("Can't access file %s\n", path);
244 strlist__add(seen, path);
245 }
246 machine = NULL;
247 goto out;
248 }
249 root_dir = path;
250 }
251
252 machine = machines__add(machines, pid, root_dir);
253out:
254 return machine;
255}
256
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300257void machines__process_guests(struct machines *machines,
258 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300259{
260 struct rb_node *nd;
261
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300262 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300263 struct machine *pos = rb_entry(nd, struct machine, rb_node);
264 process(pos, data);
265 }
266}
267
268char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
269{
270 if (machine__is_host(machine))
271 snprintf(bf, size, "[%s]", "kernel.kallsyms");
272 else if (machine__is_default_guest(machine))
273 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
274 else {
275 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
276 machine->pid);
277 }
278
279 return bf;
280}
281
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300282void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300283{
284 struct rb_node *node;
285 struct machine *machine;
286
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300287 machines->host.id_hdr_size = id_hdr_size;
288
289 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300290 machine = rb_entry(node, struct machine, rb_node);
291 machine->id_hdr_size = id_hdr_size;
292 }
293
294 return;
295}
296
Adrian Hunter29ce3612014-07-16 11:07:13 +0300297static void machine__update_thread_pid(struct machine *machine,
298 struct thread *th, pid_t pid)
299{
300 struct thread *leader;
301
302 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
303 return;
304
305 th->pid_ = pid;
306
307 if (th->pid_ == th->tid)
308 return;
309
310 leader = machine__findnew_thread(machine, th->pid_, th->pid_);
311 if (!leader)
312 goto out_err;
313
314 if (!leader->mg)
315 leader->mg = map_groups__new();
316
317 if (!leader->mg)
318 goto out_err;
319
320 if (th->mg == leader->mg)
321 return;
322
323 if (th->mg) {
324 /*
325 * Maps are created from MMAP events which provide the pid and
326 * tid. Consequently there never should be any maps on a thread
327 * with an unknown pid. Just print an error if there are.
328 */
329 if (!map_groups__empty(th->mg))
330 pr_err("Discarding thread maps for %d:%d\n",
331 th->pid_, th->tid);
332 map_groups__delete(th->mg);
333 }
334
335 th->mg = map_groups__get(leader->mg);
336
337 return;
338
339out_err:
340 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
341}
342
Adrian Hunter99d725f2013-08-26 16:00:19 +0300343static struct thread *__machine__findnew_thread(struct machine *machine,
344 pid_t pid, pid_t tid,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300345 bool create)
346{
347 struct rb_node **p = &machine->threads.rb_node;
348 struct rb_node *parent = NULL;
349 struct thread *th;
350
351 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300352 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300353 * so most of the time we dont have to look up
354 * the full rbtree:
355 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300356 th = machine->last_match;
357 if (th && th->tid == tid) {
358 machine__update_thread_pid(machine, th, pid);
359 return th;
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 Melo9d2f8e22012-10-06 15:43:20 -0300367 machine->last_match = 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);
385 machine->last_match = th;
Jiri Olsacddcef62014-04-09 20:54:29 +0200386
387 /*
388 * We have to initialize map_groups separately
389 * after rb tree is updated.
390 *
391 * The reason is that we call machine__findnew_thread
392 * within thread__init_map_groups to find the thread
393 * leader and that would screwed the rb tree.
394 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300395 if (thread__init_map_groups(th, machine)) {
396 thread__delete(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200397 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300398 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300399 }
400
401 return th;
402}
403
Adrian Hunter314add62013-08-27 11:23:03 +0300404struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
405 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300406{
Adrian Hunter314add62013-08-27 11:23:03 +0300407 return __machine__findnew_thread(machine, pid, tid, true);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300408}
409
Jiri Olsad75e6092014-03-14 15:00:03 +0100410struct thread *machine__find_thread(struct machine *machine, pid_t pid,
411 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300412{
Jiri Olsad75e6092014-03-14 15:00:03 +0100413 return __machine__findnew_thread(machine, pid, tid, false);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300414}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300415
Adrian Huntercfe1c412014-07-31 09:00:45 +0300416struct comm *machine__thread_exec_comm(struct machine *machine,
417 struct thread *thread)
418{
419 if (machine->comm_exec)
420 return thread__exec_comm(thread);
421 else
422 return thread__comm(thread);
423}
424
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200425int machine__process_comm_event(struct machine *machine, union perf_event *event,
426 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300427{
Adrian Hunter314add62013-08-27 11:23:03 +0300428 struct thread *thread = machine__findnew_thread(machine,
429 event->comm.pid,
430 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300431 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300432
Adrian Huntercfe1c412014-07-31 09:00:45 +0300433 if (exec)
434 machine->comm_exec = true;
435
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300436 if (dump_trace)
437 perf_event__fprintf_comm(event, stdout);
438
Adrian Hunter65de51f2014-07-31 09:00:44 +0300439 if (thread == NULL ||
440 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300441 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
442 return -1;
443 }
444
445 return 0;
446}
447
448int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200449 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300450{
451 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
452 event->lost.id, event->lost.lost);
453 return 0;
454}
455
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300456struct map *machine__new_module(struct machine *machine, u64 start,
457 const char *filename)
458{
459 struct map *map;
460 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
461
462 if (dso == NULL)
463 return NULL;
464
465 map = map__new2(start, dso, MAP__FUNCTION);
466 if (map == NULL)
467 return NULL;
468
469 if (machine__is_host(machine))
470 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
471 else
472 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
473 map_groups__insert(&machine->kmaps, map);
474 return map;
475}
476
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300477size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300478{
479 struct rb_node *nd;
Waiman Long8fa7d872014-09-29 16:07:28 -0400480 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos.head, fp) +
481 __dsos__fprintf(&machines->host.user_dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300482
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300483 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300484 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Waiman Long8fa7d872014-09-29 16:07:28 -0400485 ret += __dsos__fprintf(&pos->kernel_dsos.head, fp);
486 ret += __dsos__fprintf(&pos->user_dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300487 }
488
489 return ret;
490}
491
Waiman Long8fa7d872014-09-29 16:07:28 -0400492size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300493 bool (skip)(struct dso *dso, int parm), int parm)
494{
Waiman Long8fa7d872014-09-29 16:07:28 -0400495 return __dsos__fprintf_buildid(&m->kernel_dsos.head, fp, skip, parm) +
496 __dsos__fprintf_buildid(&m->user_dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300497}
498
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300499size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300500 bool (skip)(struct dso *dso, int parm), int parm)
501{
502 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300503 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300504
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300505 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300506 struct machine *pos = rb_entry(nd, struct machine, rb_node);
507 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
508 }
509 return ret;
510}
511
512size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
513{
514 int i;
515 size_t printed = 0;
516 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
517
518 if (kdso->has_build_id) {
519 char filename[PATH_MAX];
520 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
521 printed += fprintf(fp, "[0] %s\n", filename);
522 }
523
524 for (i = 0; i < vmlinux_path__nr_entries; ++i)
525 printed += fprintf(fp, "[%d] %s\n",
526 i + kdso->has_build_id, vmlinux_path[i]);
527
528 return printed;
529}
530
531size_t machine__fprintf(struct machine *machine, FILE *fp)
532{
533 size_t ret = 0;
534 struct rb_node *nd;
535
536 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
537 struct thread *pos = rb_entry(nd, struct thread, rb_node);
538
539 ret += thread__fprintf(pos, fp);
540 }
541
542 return ret;
543}
544
545static struct dso *machine__get_kernel(struct machine *machine)
546{
547 const char *vmlinux_name = NULL;
548 struct dso *kernel;
549
550 if (machine__is_host(machine)) {
551 vmlinux_name = symbol_conf.vmlinux_name;
552 if (!vmlinux_name)
553 vmlinux_name = "[kernel.kallsyms]";
554
555 kernel = dso__kernel_findnew(machine, vmlinux_name,
556 "[kernel]",
557 DSO_TYPE_KERNEL);
558 } else {
559 char bf[PATH_MAX];
560
561 if (machine__is_default_guest(machine))
562 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
563 if (!vmlinux_name)
564 vmlinux_name = machine__mmap_name(machine, bf,
565 sizeof(bf));
566
567 kernel = dso__kernel_findnew(machine, vmlinux_name,
568 "[guest.kernel]",
569 DSO_TYPE_GUEST_KERNEL);
570 }
571
572 if (kernel != NULL && (!kernel->has_build_id))
573 dso__read_running_kernel_build_id(kernel, machine);
574
575 return kernel;
576}
577
578struct process_args {
579 u64 start;
580};
581
Adrian Hunter15a0a872014-01-29 16:14:38 +0200582static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
583 size_t bufsz)
584{
585 if (machine__is_default_guest(machine))
586 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
587 else
588 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
589}
590
Simon Quea93f0e52014-06-16 11:32:09 -0700591const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
592
593/* Figure out the start address of kernel map from /proc/kallsyms.
594 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
595 * symbol_name if it's not that important.
596 */
Adrian Hunter4b993752014-08-15 22:08:38 +0300597static u64 machine__get_running_kernel_start(struct machine *machine,
598 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300599{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200600 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700601 int i;
602 const char *name;
603 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300604
Adrian Hunter15a0a872014-01-29 16:14:38 +0200605 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300606
607 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
608 return 0;
609
Simon Quea93f0e52014-06-16 11:32:09 -0700610 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
611 addr = kallsyms__get_function_start(filename, name);
612 if (addr)
613 break;
614 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300615
Simon Quea93f0e52014-06-16 11:32:09 -0700616 if (symbol_name)
617 *symbol_name = name;
618
619 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300620}
621
622int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
623{
624 enum map_type type;
Adrian Hunter4b993752014-08-15 22:08:38 +0300625 u64 start = machine__get_running_kernel_start(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300626
627 for (type = 0; type < MAP__NR_TYPES; ++type) {
628 struct kmap *kmap;
629
630 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
631 if (machine->vmlinux_maps[type] == NULL)
632 return -1;
633
634 machine->vmlinux_maps[type]->map_ip =
635 machine->vmlinux_maps[type]->unmap_ip =
636 identity__map_ip;
637 kmap = map__kmap(machine->vmlinux_maps[type]);
638 kmap->kmaps = &machine->kmaps;
639 map_groups__insert(&machine->kmaps,
640 machine->vmlinux_maps[type]);
641 }
642
643 return 0;
644}
645
646void machine__destroy_kernel_maps(struct machine *machine)
647{
648 enum map_type type;
649
650 for (type = 0; type < MAP__NR_TYPES; ++type) {
651 struct kmap *kmap;
652
653 if (machine->vmlinux_maps[type] == NULL)
654 continue;
655
656 kmap = map__kmap(machine->vmlinux_maps[type]);
657 map_groups__remove(&machine->kmaps,
658 machine->vmlinux_maps[type]);
659 if (kmap->ref_reloc_sym) {
660 /*
661 * ref_reloc_sym is shared among all maps, so free just
662 * on one of them.
663 */
664 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300665 zfree((char **)&kmap->ref_reloc_sym->name);
666 zfree(&kmap->ref_reloc_sym);
667 } else
668 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300669 }
670
671 map__delete(machine->vmlinux_maps[type]);
672 machine->vmlinux_maps[type] = NULL;
673 }
674}
675
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300676int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300677{
678 int ret = 0;
679 struct dirent **namelist = NULL;
680 int i, items = 0;
681 char path[PATH_MAX];
682 pid_t pid;
683 char *endp;
684
685 if (symbol_conf.default_guest_vmlinux_name ||
686 symbol_conf.default_guest_modules ||
687 symbol_conf.default_guest_kallsyms) {
688 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
689 }
690
691 if (symbol_conf.guestmount) {
692 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
693 if (items <= 0)
694 return -ENOENT;
695 for (i = 0; i < items; i++) {
696 if (!isdigit(namelist[i]->d_name[0])) {
697 /* Filter out . and .. */
698 continue;
699 }
700 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
701 if ((*endp != '\0') ||
702 (endp == namelist[i]->d_name) ||
703 (errno == ERANGE)) {
704 pr_debug("invalid directory (%s). Skipping.\n",
705 namelist[i]->d_name);
706 continue;
707 }
708 sprintf(path, "%s/%s/proc/kallsyms",
709 symbol_conf.guestmount,
710 namelist[i]->d_name);
711 ret = access(path, R_OK);
712 if (ret) {
713 pr_debug("Can't access file %s\n", path);
714 goto failure;
715 }
716 machines__create_kernel_maps(machines, pid);
717 }
718failure:
719 free(namelist);
720 }
721
722 return ret;
723}
724
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300725void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300726{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300727 struct rb_node *next = rb_first(&machines->guests);
728
729 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300730
731 while (next) {
732 struct machine *pos = rb_entry(next, struct machine, rb_node);
733
734 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300735 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300736 machine__delete(pos);
737 }
738}
739
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300740int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300741{
742 struct machine *machine = machines__findnew(machines, pid);
743
744 if (machine == NULL)
745 return -1;
746
747 return machine__create_kernel_maps(machine);
748}
749
750int machine__load_kallsyms(struct machine *machine, const char *filename,
751 enum map_type type, symbol_filter_t filter)
752{
753 struct map *map = machine->vmlinux_maps[type];
754 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
755
756 if (ret > 0) {
757 dso__set_loaded(map->dso, type);
758 /*
759 * Since /proc/kallsyms will have multiple sessions for the
760 * kernel, with modules between them, fixup the end of all
761 * sections.
762 */
763 __map_groups__fixup_end(&machine->kmaps, type);
764 }
765
766 return ret;
767}
768
769int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
770 symbol_filter_t filter)
771{
772 struct map *map = machine->vmlinux_maps[type];
773 int ret = dso__load_vmlinux_path(map->dso, map, filter);
774
Adrian Hunter39b12f782013-08-07 14:38:47 +0300775 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300776 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300777
778 return ret;
779}
780
781static void map_groups__fixup_end(struct map_groups *mg)
782{
783 int i;
784 for (i = 0; i < MAP__NR_TYPES; ++i)
785 __map_groups__fixup_end(mg, i);
786}
787
788static char *get_kernel_version(const char *root_dir)
789{
790 char version[PATH_MAX];
791 FILE *file;
792 char *name, *tmp;
793 const char *prefix = "Linux version ";
794
795 sprintf(version, "%s/proc/version", root_dir);
796 file = fopen(version, "r");
797 if (!file)
798 return NULL;
799
800 version[0] = '\0';
801 tmp = fgets(version, sizeof(version), file);
802 fclose(file);
803
804 name = strstr(version, prefix);
805 if (!name)
806 return NULL;
807 name += strlen(prefix);
808 tmp = strchr(name, ' ');
809 if (tmp)
810 *tmp = '\0';
811
812 return strdup(name);
813}
814
815static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -0400816 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300817{
818 struct dirent *dent;
819 DIR *dir = opendir(dir_name);
820 int ret = 0;
821
822 if (!dir) {
823 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
824 return -1;
825 }
826
827 while ((dent = readdir(dir)) != NULL) {
828 char path[PATH_MAX];
829 struct stat st;
830
831 /*sshfs might return bad dent->d_type, so we have to stat*/
832 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
833 if (stat(path, &st))
834 continue;
835
836 if (S_ISDIR(st.st_mode)) {
837 if (!strcmp(dent->d_name, ".") ||
838 !strcmp(dent->d_name, ".."))
839 continue;
840
Richard Yao61d42902014-04-26 13:17:55 -0400841 /* Do not follow top-level source and build symlinks */
842 if (depth == 0) {
843 if (!strcmp(dent->d_name, "source") ||
844 !strcmp(dent->d_name, "build"))
845 continue;
846 }
847
848 ret = map_groups__set_modules_path_dir(mg, path,
849 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300850 if (ret < 0)
851 goto out;
852 } else {
853 char *dot = strrchr(dent->d_name, '.'),
854 dso_name[PATH_MAX];
855 struct map *map;
856 char *long_name;
857
858 if (dot == NULL || strcmp(dot, ".ko"))
859 continue;
860 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
861 (int)(dot - dent->d_name), dent->d_name);
862
863 strxfrchar(dso_name, '-', '_');
864 map = map_groups__find_by_name(mg, MAP__FUNCTION,
865 dso_name);
866 if (map == NULL)
867 continue;
868
869 long_name = strdup(path);
870 if (long_name == NULL) {
871 ret = -1;
872 goto out;
873 }
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300874 dso__set_long_name(map->dso, long_name, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300875 dso__kernel_module_get_build_id(map->dso, "");
876 }
877 }
878
879out:
880 closedir(dir);
881 return ret;
882}
883
884static int machine__set_modules_path(struct machine *machine)
885{
886 char *version;
887 char modules_path[PATH_MAX];
888
889 version = get_kernel_version(machine->root_dir);
890 if (!version)
891 return -1;
892
Richard Yao61d42902014-04-26 13:17:55 -0400893 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300894 machine->root_dir, version);
895 free(version);
896
Richard Yao61d42902014-04-26 13:17:55 -0400897 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300898}
899
Adrian Hunter316d70d2013-10-08 11:45:48 +0300900static int machine__create_module(void *arg, const char *name, u64 start)
901{
902 struct machine *machine = arg;
903 struct map *map;
904
905 map = machine__new_module(machine, start, name);
906 if (map == NULL)
907 return -1;
908
909 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
910
911 return 0;
912}
913
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300914static int machine__create_modules(struct machine *machine)
915{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300916 const char *modules;
917 char path[PATH_MAX];
918
Adrian Hunterf4be9042013-09-22 13:22:09 +0300919 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300920 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +0300921 } else {
922 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300923 modules = path;
924 }
925
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +0300926 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300927 return -1;
928
Adrian Hunter316d70d2013-10-08 11:45:48 +0300929 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300930 return -1;
931
Adrian Hunter316d70d2013-10-08 11:45:48 +0300932 if (!machine__set_modules_path(machine))
933 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300934
Adrian Hunter316d70d2013-10-08 11:45:48 +0300935 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300936
Jason Wessel8f76fcd2013-07-15 15:27:53 -0500937 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300938}
939
940int machine__create_kernel_maps(struct machine *machine)
941{
942 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200943 const char *name;
Adrian Hunter4b993752014-08-15 22:08:38 +0300944 u64 addr = machine__get_running_kernel_start(machine, &name);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200945 if (!addr)
946 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300947
948 if (kernel == NULL ||
949 __machine__create_kernel_maps(machine, kernel) < 0)
950 return -1;
951
952 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
953 if (machine__is_host(machine))
954 pr_debug("Problems creating module maps, "
955 "continuing anyway...\n");
956 else
957 pr_debug("Problems creating module maps for guest %d, "
958 "continuing anyway...\n", machine->pid);
959 }
960
961 /*
962 * Now that we have all the maps created, just set the ->end of them:
963 */
964 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200965
966 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
967 addr)) {
968 machine__destroy_kernel_maps(machine);
969 return -1;
970 }
971
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300972 return 0;
973}
974
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300975static void machine__set_kernel_mmap_len(struct machine *machine,
976 union perf_event *event)
977{
Namhyung Kim4552cf0f2012-11-07 16:27:10 +0900978 int i;
979
980 for (i = 0; i < MAP__NR_TYPES; i++) {
981 machine->vmlinux_maps[i]->start = event->mmap.start;
982 machine->vmlinux_maps[i]->end = (event->mmap.start +
983 event->mmap.len);
984 /*
985 * Be a bit paranoid here, some perf.data file came with
986 * a zero sized synthesized MMAP event for the kernel.
987 */
988 if (machine->vmlinux_maps[i]->end == 0)
989 machine->vmlinux_maps[i]->end = ~0ULL;
990 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300991}
992
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300993static bool machine__uses_kcore(struct machine *machine)
994{
995 struct dso *dso;
996
Waiman Long8fa7d872014-09-29 16:07:28 -0400997 list_for_each_entry(dso, &machine->kernel_dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300998 if (dso__is_kcore(dso))
999 return true;
1000 }
1001
1002 return false;
1003}
1004
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001005static int machine__process_kernel_mmap_event(struct machine *machine,
1006 union perf_event *event)
1007{
1008 struct map *map;
1009 char kmmap_prefix[PATH_MAX];
1010 enum dso_kernel_type kernel_type;
1011 bool is_kernel_mmap;
1012
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001013 /* If we have maps from kcore then we do not need or want any others */
1014 if (machine__uses_kcore(machine))
1015 return 0;
1016
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001017 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1018 if (machine__is_host(machine))
1019 kernel_type = DSO_TYPE_KERNEL;
1020 else
1021 kernel_type = DSO_TYPE_GUEST_KERNEL;
1022
1023 is_kernel_mmap = memcmp(event->mmap.filename,
1024 kmmap_prefix,
1025 strlen(kmmap_prefix) - 1) == 0;
1026 if (event->mmap.filename[0] == '/' ||
1027 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1028
1029 char short_module_name[1024];
1030 char *name, *dot;
1031
1032 if (event->mmap.filename[0] == '/') {
1033 name = strrchr(event->mmap.filename, '/');
1034 if (name == NULL)
1035 goto out_problem;
1036
1037 ++name; /* skip / */
1038 dot = strrchr(name, '.');
1039 if (dot == NULL)
1040 goto out_problem;
1041 snprintf(short_module_name, sizeof(short_module_name),
1042 "[%.*s]", (int)(dot - name), name);
1043 strxfrchar(short_module_name, '-', '_');
1044 } else
1045 strcpy(short_module_name, event->mmap.filename);
1046
1047 map = machine__new_module(machine, event->mmap.start,
1048 event->mmap.filename);
1049 if (map == NULL)
1050 goto out_problem;
1051
1052 name = strdup(short_module_name);
1053 if (name == NULL)
1054 goto out_problem;
1055
Adrian Hunter58a98c92013-12-10 11:11:46 -03001056 dso__set_short_name(map->dso, name, true);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001057 map->end = map->start + event->mmap.len;
1058 } else if (is_kernel_mmap) {
1059 const char *symbol_name = (event->mmap.filename +
1060 strlen(kmmap_prefix));
1061 /*
1062 * Should be there already, from the build-id table in
1063 * the header.
1064 */
1065 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
1066 kmmap_prefix);
1067 if (kernel == NULL)
1068 goto out_problem;
1069
1070 kernel->kernel = kernel_type;
1071 if (__machine__create_kernel_maps(machine, kernel) < 0)
1072 goto out_problem;
1073
1074 machine__set_kernel_mmap_len(machine, event);
1075
1076 /*
1077 * Avoid using a zero address (kptr_restrict) for the ref reloc
1078 * symbol. Effectively having zero here means that at record
1079 * time /proc/sys/kernel/kptr_restrict was non zero.
1080 */
1081 if (event->mmap.pgoff != 0) {
1082 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1083 symbol_name,
1084 event->mmap.pgoff);
1085 }
1086
1087 if (machine__is_default_guest(machine)) {
1088 /*
1089 * preload dso of guest kernel and modules
1090 */
1091 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
1092 NULL);
1093 }
1094 }
1095 return 0;
1096out_problem:
1097 return -1;
1098}
1099
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001100int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001101 union perf_event *event,
1102 struct perf_sample *sample __maybe_unused)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001103{
1104 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1105 struct thread *thread;
1106 struct map *map;
1107 enum map_type type;
1108 int ret = 0;
1109
1110 if (dump_trace)
1111 perf_event__fprintf_mmap2(event, stdout);
1112
1113 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1114 cpumode == PERF_RECORD_MISC_KERNEL) {
1115 ret = machine__process_kernel_mmap_event(machine, event);
1116 if (ret < 0)
1117 goto out_problem;
1118 return 0;
1119 }
1120
1121 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001122 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001123 if (thread == NULL)
1124 goto out_problem;
1125
1126 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1127 type = MAP__VARIABLE;
1128 else
1129 type = MAP__FUNCTION;
1130
Adrian Hunter2a030682014-07-22 16:17:53 +03001131 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001132 event->mmap2.len, event->mmap2.pgoff,
1133 event->mmap2.pid, event->mmap2.maj,
1134 event->mmap2.min, event->mmap2.ino,
1135 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001136 event->mmap2.prot,
1137 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001138 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001139
1140 if (map == NULL)
1141 goto out_problem;
1142
1143 thread__insert_map(thread, map);
1144 return 0;
1145
1146out_problem:
1147 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1148 return 0;
1149}
1150
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001151int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1152 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001153{
1154 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1155 struct thread *thread;
1156 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001157 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001158 int ret = 0;
1159
1160 if (dump_trace)
1161 perf_event__fprintf_mmap(event, stdout);
1162
1163 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1164 cpumode == PERF_RECORD_MISC_KERNEL) {
1165 ret = machine__process_kernel_mmap_event(machine, event);
1166 if (ret < 0)
1167 goto out_problem;
1168 return 0;
1169 }
1170
Adrian Hunter314add62013-08-27 11:23:03 +03001171 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001172 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001173 if (thread == NULL)
1174 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001175
1176 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1177 type = MAP__VARIABLE;
1178 else
1179 type = MAP__FUNCTION;
1180
Adrian Hunter2a030682014-07-22 16:17:53 +03001181 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001182 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001183 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001184 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001185 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001186
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001187 if (map == NULL)
1188 goto out_problem;
1189
1190 thread__insert_map(thread, map);
1191 return 0;
1192
1193out_problem:
1194 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1195 return 0;
1196}
1197
David Ahern236a3bb2013-08-14 08:49:27 -06001198static void machine__remove_thread(struct machine *machine, struct thread *th)
1199{
1200 machine->last_match = NULL;
1201 rb_erase(&th->rb_node, &machine->threads);
1202 /*
1203 * We may have references to this thread, for instance in some hist_entry
1204 * instances, so just move them to a separate list.
1205 */
1206 list_add_tail(&th->node, &machine->dead_threads);
1207}
1208
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001209int machine__process_fork_event(struct machine *machine, union perf_event *event,
1210 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001211{
Jiri Olsad75e6092014-03-14 15:00:03 +01001212 struct thread *thread = machine__find_thread(machine,
1213 event->fork.pid,
1214 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001215 struct thread *parent = machine__findnew_thread(machine,
1216 event->fork.ppid,
1217 event->fork.ptid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001218
David Ahern236a3bb2013-08-14 08:49:27 -06001219 /* if a thread currently exists for the thread id remove it */
1220 if (thread != NULL)
1221 machine__remove_thread(machine, thread);
1222
Adrian Hunter314add62013-08-27 11:23:03 +03001223 thread = machine__findnew_thread(machine, event->fork.pid,
1224 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001225 if (dump_trace)
1226 perf_event__fprintf_task(event, stdout);
1227
1228 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001229 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001230 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1231 return -1;
1232 }
1233
1234 return 0;
1235}
1236
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001237int machine__process_exit_event(struct machine *machine, union perf_event *event,
1238 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001239{
Jiri Olsad75e6092014-03-14 15:00:03 +01001240 struct thread *thread = machine__find_thread(machine,
1241 event->fork.pid,
1242 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001243
1244 if (dump_trace)
1245 perf_event__fprintf_task(event, stdout);
1246
1247 if (thread != NULL)
David Ahern236a3bb2013-08-14 08:49:27 -06001248 thread__exited(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001249
1250 return 0;
1251}
1252
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001253int machine__process_event(struct machine *machine, union perf_event *event,
1254 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001255{
1256 int ret;
1257
1258 switch (event->header.type) {
1259 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001260 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001261 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001262 ret = machine__process_mmap_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001263 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001264 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001265 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001266 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001267 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001268 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001269 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001270 ret = machine__process_lost_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001271 default:
1272 ret = -1;
1273 break;
1274 }
1275
1276 return ret;
1277}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001278
Greg Priceb21484f2012-12-06 21:48:05 -08001279static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001280{
Greg Priceb21484f2012-12-06 21:48:05 -08001281 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001282 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001283 return 0;
1284}
1285
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001286static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1287 struct addr_map_symbol *ams,
1288 u64 ip)
1289{
1290 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001291
1292 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001293 /*
1294 * We cannot use the header.misc hint to determine whether a
1295 * branch stack address is user, kernel, guest, hypervisor.
1296 * Branches may straddle the kernel/user/hypervisor boundaries.
1297 * Thus, we have to try consecutively until we find a match
1298 * or else, the symbol is unknown
1299 */
1300 thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001301
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001302 ams->addr = ip;
1303 ams->al_addr = al.addr;
1304 ams->sym = al.sym;
1305 ams->map = al.map;
1306}
1307
Stephane Eranian98a3b322013-01-24 16:10:35 +01001308static void ip__resolve_data(struct machine *machine, struct thread *thread,
1309 u8 m, struct addr_map_symbol *ams, u64 addr)
1310{
1311 struct addr_location al;
1312
1313 memset(&al, 0, sizeof(al));
1314
Adrian Hunter61710bd2013-08-08 14:32:26 +03001315 thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
1316 &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001317 if (al.map == NULL) {
1318 /*
1319 * some shared data regions have execute bit set which puts
1320 * their mapping in the MAP__FUNCTION type array.
1321 * Check there as a fallback option before dropping the sample.
1322 */
1323 thread__find_addr_location(thread, machine, m, MAP__FUNCTION, addr,
1324 &al);
1325 }
1326
Stephane Eranian98a3b322013-01-24 16:10:35 +01001327 ams->addr = addr;
1328 ams->al_addr = al.addr;
1329 ams->sym = al.sym;
1330 ams->map = al.map;
1331}
1332
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001333struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1334 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001335{
1336 struct mem_info *mi = zalloc(sizeof(*mi));
1337
1338 if (!mi)
1339 return NULL;
1340
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001341 ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip);
1342 ip__resolve_data(al->machine, al->thread, al->cpumode,
1343 &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001344 mi->data_src.val = sample->data_src;
1345
1346 return mi;
1347}
1348
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001349struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1350 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001351{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001352 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001353 const struct branch_stack *bs = sample->branch_stack;
1354 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001355
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001356 if (!bi)
1357 return NULL;
1358
1359 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001360 ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to);
1361 ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001362 bi[i].flags = bs->entries[i].flags;
1363 }
1364 return bi;
1365}
1366
1367static int machine__resolve_callchain_sample(struct machine *machine,
1368 struct thread *thread,
1369 struct ip_callchain *chain,
Greg Priceb21484f2012-12-06 21:48:05 -08001370 struct symbol **parent,
Waiman Long91e95612013-10-18 10:38:48 -04001371 struct addr_location *root_al,
1372 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001373{
1374 u8 cpumode = PERF_RECORD_MISC_USER;
Waiman Long91e95612013-10-18 10:38:48 -04001375 int chain_nr = min(max_stack, (int)chain->nr);
1376 int i;
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001377 int j;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001378 int err;
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001379 int skip_idx __maybe_unused;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001380
1381 callchain_cursor_reset(&callchain_cursor);
1382
1383 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1384 pr_warning("corrupted callchain. skipping...\n");
1385 return 0;
1386 }
1387
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001388 /*
1389 * Based on DWARF debug information, some architectures skip
1390 * a callchain entry saved by the kernel.
1391 */
1392 skip_idx = arch_skip_callchain_idx(machine, thread, chain);
1393
Waiman Long91e95612013-10-18 10:38:48 -04001394 for (i = 0; i < chain_nr; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001395 u64 ip;
1396 struct addr_location al;
1397
1398 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001399 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001400 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001401 j = chain->nr - i - 1;
1402
1403#ifdef HAVE_SKIP_CALLCHAIN_IDX
1404 if (j == skip_idx)
1405 continue;
1406#endif
1407 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001408
1409 if (ip >= PERF_CONTEXT_MAX) {
1410 switch (ip) {
1411 case PERF_CONTEXT_HV:
1412 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1413 break;
1414 case PERF_CONTEXT_KERNEL:
1415 cpumode = PERF_RECORD_MISC_KERNEL;
1416 break;
1417 case PERF_CONTEXT_USER:
1418 cpumode = PERF_RECORD_MISC_USER;
1419 break;
1420 default:
1421 pr_debug("invalid callchain context: "
1422 "%"PRId64"\n", (s64) ip);
1423 /*
1424 * It seems the callchain is corrupted.
1425 * Discard all.
1426 */
1427 callchain_cursor_reset(&callchain_cursor);
1428 return 0;
1429 }
1430 continue;
1431 }
1432
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001433 al.filtered = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001434 thread__find_addr_location(thread, machine, cpumode,
Adrian Hunter61710bd2013-08-08 14:32:26 +03001435 MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001436 if (al.sym != NULL) {
1437 if (sort__has_parent && !*parent &&
Greg Priceb21484f2012-12-06 21:48:05 -08001438 symbol__match_regex(al.sym, &parent_regex))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001439 *parent = al.sym;
Greg Priceb21484f2012-12-06 21:48:05 -08001440 else if (have_ignore_callees && root_al &&
1441 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1442 /* Treat this symbol as the root,
1443 forgetting its callees. */
1444 *root_al = al;
1445 callchain_cursor_reset(&callchain_cursor);
1446 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001447 }
1448
1449 err = callchain_cursor_append(&callchain_cursor,
1450 ip, al.map, al.sym);
1451 if (err)
1452 return err;
1453 }
1454
1455 return 0;
1456}
1457
1458static int unwind_entry(struct unwind_entry *entry, void *arg)
1459{
1460 struct callchain_cursor *cursor = arg;
1461 return callchain_cursor_append(cursor, entry->ip,
1462 entry->map, entry->sym);
1463}
1464
1465int machine__resolve_callchain(struct machine *machine,
1466 struct perf_evsel *evsel,
1467 struct thread *thread,
1468 struct perf_sample *sample,
Greg Priceb21484f2012-12-06 21:48:05 -08001469 struct symbol **parent,
Waiman Long91e95612013-10-18 10:38:48 -04001470 struct addr_location *root_al,
1471 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001472{
1473 int ret;
1474
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001475 ret = machine__resolve_callchain_sample(machine, thread,
Waiman Long91e95612013-10-18 10:38:48 -04001476 sample->callchain, parent,
1477 root_al, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001478 if (ret)
1479 return ret;
1480
1481 /* Can we do dwarf post unwind? */
1482 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1483 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1484 return 0;
1485
1486 /* Bail out if nothing was captured. */
1487 if ((!sample->user_regs.regs) ||
1488 (!sample->user_stack.size))
1489 return 0;
1490
1491 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
Jiri Olsa352ea452014-01-07 13:47:25 +01001492 thread, sample, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001493
1494}
David Ahern35feee12013-09-28 13:12:58 -06001495
1496int machine__for_each_thread(struct machine *machine,
1497 int (*fn)(struct thread *thread, void *p),
1498 void *priv)
1499{
1500 struct rb_node *nd;
1501 struct thread *thread;
1502 int rc = 0;
1503
1504 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1505 thread = rb_entry(nd, struct thread, rb_node);
1506 rc = fn(thread, priv);
1507 if (rc != 0)
1508 return rc;
1509 }
1510
1511 list_for_each_entry(thread, &machine->dead_threads, node) {
1512 rc = fn(thread, priv);
1513 if (rc != 0)
1514 return rc;
1515 }
1516 return rc;
1517}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001518
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001519int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001520 struct target *target, struct thread_map *threads,
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001521 perf_event__handler_t process, bool data_mmap)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001522{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001523 if (target__has_task(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001524 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001525 else if (target__has_cpu(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001526 return perf_event__synthesize_threads(tool, process, machine, data_mmap);
1527 /* command specified */
1528 return 0;
1529}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03001530
1531pid_t machine__get_current_tid(struct machine *machine, int cpu)
1532{
1533 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
1534 return -1;
1535
1536 return machine->current_tid[cpu];
1537}
1538
1539int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
1540 pid_t tid)
1541{
1542 struct thread *thread;
1543
1544 if (cpu < 0)
1545 return -EINVAL;
1546
1547 if (!machine->current_tid) {
1548 int i;
1549
1550 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
1551 if (!machine->current_tid)
1552 return -ENOMEM;
1553 for (i = 0; i < MAX_NR_CPUS; i++)
1554 machine->current_tid[i] = -1;
1555 }
1556
1557 if (cpu >= MAX_NR_CPUS) {
1558 pr_err("Requested CPU %d too large. ", cpu);
1559 pr_err("Consider raising MAX_NR_CPUS\n");
1560 return -EINVAL;
1561 }
1562
1563 machine->current_tid[cpu] = tid;
1564
1565 thread = machine__findnew_thread(machine, pid, tid);
1566 if (!thread)
1567 return -ENOMEM;
1568
1569 thread->cpu = cpu;
1570
1571 return 0;
1572}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03001573
1574int machine__get_kernel_start(struct machine *machine)
1575{
1576 struct map *map = machine__kernel_map(machine, MAP__FUNCTION);
1577 int err = 0;
1578
1579 /*
1580 * The only addresses above 2^63 are kernel addresses of a 64-bit
1581 * kernel. Note that addresses are unsigned so that on a 32-bit system
1582 * all addresses including kernel addresses are less than 2^32. In
1583 * that case (32-bit system), if the kernel mapping is unknown, all
1584 * addresses will be assumed to be in user space - see
1585 * machine__kernel_ip().
1586 */
1587 machine->kernel_start = 1ULL << 63;
1588 if (map) {
1589 err = map__load(map, machine->symbol_filter);
1590 if (map->start)
1591 machine->kernel_start = map->start;
1592 }
1593 return err;
1594}