blob: b7d477fbda0208bb7ef6b4923a0769c7273049e2 [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) {
Waiman Long4598a0a2014-09-30 13:36:15 -040080 RB_CLEAR_NODE(&pos->rb_node);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030081 list_del(&pos->node);
82 dso__delete(pos);
83 }
84}
85
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030086void machine__delete_dead_threads(struct machine *machine)
87{
88 struct thread *n, *t;
89
90 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
91 list_del(&t->node);
92 thread__delete(t);
93 }
94}
95
96void machine__delete_threads(struct machine *machine)
97{
98 struct rb_node *nd = rb_first(&machine->threads);
99
100 while (nd) {
101 struct thread *t = rb_entry(nd, struct thread, rb_node);
102
103 rb_erase(&t->rb_node, &machine->threads);
104 nd = rb_next(nd);
105 thread__delete(t);
106 }
107}
108
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300109void machine__exit(struct machine *machine)
110{
111 map_groups__exit(&machine->kmaps);
112 dsos__delete(&machine->user_dsos);
113 dsos__delete(&machine->kernel_dsos);
Adrian Hunterd027b642014-07-23 14:23:00 +0300114 vdso__exit(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300115 zfree(&machine->root_dir);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300116 zfree(&machine->current_tid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300117}
118
119void machine__delete(struct machine *machine)
120{
121 machine__exit(machine);
122 free(machine);
123}
124
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300125void machines__init(struct machines *machines)
126{
127 machine__init(&machines->host, "", HOST_KERNEL_ID);
128 machines->guests = RB_ROOT;
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300129 machines->symbol_filter = NULL;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300130}
131
132void machines__exit(struct machines *machines)
133{
134 machine__exit(&machines->host);
135 /* XXX exit guest */
136}
137
138struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300139 const char *root_dir)
140{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300141 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300142 struct rb_node *parent = NULL;
143 struct machine *pos, *machine = malloc(sizeof(*machine));
144
145 if (machine == NULL)
146 return NULL;
147
148 if (machine__init(machine, root_dir, pid) != 0) {
149 free(machine);
150 return NULL;
151 }
152
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300153 machine->symbol_filter = machines->symbol_filter;
154
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300155 while (*p != NULL) {
156 parent = *p;
157 pos = rb_entry(parent, struct machine, rb_node);
158 if (pid < pos->pid)
159 p = &(*p)->rb_left;
160 else
161 p = &(*p)->rb_right;
162 }
163
164 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300165 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300166
167 return machine;
168}
169
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300170void machines__set_symbol_filter(struct machines *machines,
171 symbol_filter_t symbol_filter)
172{
173 struct rb_node *nd;
174
175 machines->symbol_filter = symbol_filter;
176 machines->host.symbol_filter = symbol_filter;
177
178 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
179 struct machine *machine = rb_entry(nd, struct machine, rb_node);
180
181 machine->symbol_filter = symbol_filter;
182 }
183}
184
Adrian Huntercfe1c412014-07-31 09:00:45 +0300185void machines__set_comm_exec(struct machines *machines, bool comm_exec)
186{
187 struct rb_node *nd;
188
189 machines->host.comm_exec = comm_exec;
190
191 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
192 struct machine *machine = rb_entry(nd, struct machine, rb_node);
193
194 machine->comm_exec = comm_exec;
195 }
196}
197
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300198struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300199{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300200 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300201 struct rb_node *parent = NULL;
202 struct machine *machine;
203 struct machine *default_machine = NULL;
204
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300205 if (pid == HOST_KERNEL_ID)
206 return &machines->host;
207
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300208 while (*p != NULL) {
209 parent = *p;
210 machine = rb_entry(parent, struct machine, rb_node);
211 if (pid < machine->pid)
212 p = &(*p)->rb_left;
213 else if (pid > machine->pid)
214 p = &(*p)->rb_right;
215 else
216 return machine;
217 if (!machine->pid)
218 default_machine = machine;
219 }
220
221 return default_machine;
222}
223
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300224struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300225{
226 char path[PATH_MAX];
227 const char *root_dir = "";
228 struct machine *machine = machines__find(machines, pid);
229
230 if (machine && (machine->pid == pid))
231 goto out;
232
233 if ((pid != HOST_KERNEL_ID) &&
234 (pid != DEFAULT_GUEST_KERNEL_ID) &&
235 (symbol_conf.guestmount)) {
236 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
237 if (access(path, R_OK)) {
238 static struct strlist *seen;
239
240 if (!seen)
241 seen = strlist__new(true, NULL);
242
243 if (!strlist__has_entry(seen, path)) {
244 pr_err("Can't access file %s\n", path);
245 strlist__add(seen, path);
246 }
247 machine = NULL;
248 goto out;
249 }
250 root_dir = path;
251 }
252
253 machine = machines__add(machines, pid, root_dir);
254out:
255 return machine;
256}
257
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300258void machines__process_guests(struct machines *machines,
259 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300260{
261 struct rb_node *nd;
262
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300263 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300264 struct machine *pos = rb_entry(nd, struct machine, rb_node);
265 process(pos, data);
266 }
267}
268
269char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
270{
271 if (machine__is_host(machine))
272 snprintf(bf, size, "[%s]", "kernel.kallsyms");
273 else if (machine__is_default_guest(machine))
274 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
275 else {
276 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
277 machine->pid);
278 }
279
280 return bf;
281}
282
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300283void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300284{
285 struct rb_node *node;
286 struct machine *machine;
287
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300288 machines->host.id_hdr_size = id_hdr_size;
289
290 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300291 machine = rb_entry(node, struct machine, rb_node);
292 machine->id_hdr_size = id_hdr_size;
293 }
294
295 return;
296}
297
Adrian Hunter29ce3612014-07-16 11:07:13 +0300298static void machine__update_thread_pid(struct machine *machine,
299 struct thread *th, pid_t pid)
300{
301 struct thread *leader;
302
303 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
304 return;
305
306 th->pid_ = pid;
307
308 if (th->pid_ == th->tid)
309 return;
310
311 leader = machine__findnew_thread(machine, th->pid_, th->pid_);
312 if (!leader)
313 goto out_err;
314
315 if (!leader->mg)
316 leader->mg = map_groups__new();
317
318 if (!leader->mg)
319 goto out_err;
320
321 if (th->mg == leader->mg)
322 return;
323
324 if (th->mg) {
325 /*
326 * Maps are created from MMAP events which provide the pid and
327 * tid. Consequently there never should be any maps on a thread
328 * with an unknown pid. Just print an error if there are.
329 */
330 if (!map_groups__empty(th->mg))
331 pr_err("Discarding thread maps for %d:%d\n",
332 th->pid_, th->tid);
333 map_groups__delete(th->mg);
334 }
335
336 th->mg = map_groups__get(leader->mg);
337
338 return;
339
340out_err:
341 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
342}
343
Adrian Hunter99d725f2013-08-26 16:00:19 +0300344static struct thread *__machine__findnew_thread(struct machine *machine,
345 pid_t pid, pid_t tid,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300346 bool create)
347{
348 struct rb_node **p = &machine->threads.rb_node;
349 struct rb_node *parent = NULL;
350 struct thread *th;
351
352 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300353 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300354 * so most of the time we dont have to look up
355 * the full rbtree:
356 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300357 th = machine->last_match;
358 if (th && th->tid == tid) {
359 machine__update_thread_pid(machine, th, pid);
360 return th;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300361 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300362
363 while (*p != NULL) {
364 parent = *p;
365 th = rb_entry(parent, struct thread, rb_node);
366
Adrian Hunter38051232013-07-04 16:20:31 +0300367 if (th->tid == tid) {
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300368 machine->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300369 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300370 return th;
371 }
372
Adrian Hunter38051232013-07-04 16:20:31 +0300373 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300374 p = &(*p)->rb_left;
375 else
376 p = &(*p)->rb_right;
377 }
378
379 if (!create)
380 return NULL;
381
Adrian Hunter99d725f2013-08-26 16:00:19 +0300382 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300383 if (th != NULL) {
384 rb_link_node(&th->rb_node, parent, p);
385 rb_insert_color(&th->rb_node, &machine->threads);
386 machine->last_match = th;
Jiri Olsacddcef62014-04-09 20:54:29 +0200387
388 /*
389 * We have to initialize map_groups separately
390 * after rb tree is updated.
391 *
392 * The reason is that we call machine__findnew_thread
393 * within thread__init_map_groups to find the thread
394 * leader and that would screwed the rb tree.
395 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300396 if (thread__init_map_groups(th, machine)) {
397 thread__delete(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200398 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300399 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300400 }
401
402 return th;
403}
404
Adrian Hunter314add62013-08-27 11:23:03 +0300405struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
406 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300407{
Adrian Hunter314add62013-08-27 11:23:03 +0300408 return __machine__findnew_thread(machine, pid, tid, true);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300409}
410
Jiri Olsad75e6092014-03-14 15:00:03 +0100411struct thread *machine__find_thread(struct machine *machine, pid_t pid,
412 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300413{
Jiri Olsad75e6092014-03-14 15:00:03 +0100414 return __machine__findnew_thread(machine, pid, tid, false);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300415}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300416
Adrian Huntercfe1c412014-07-31 09:00:45 +0300417struct comm *machine__thread_exec_comm(struct machine *machine,
418 struct thread *thread)
419{
420 if (machine->comm_exec)
421 return thread__exec_comm(thread);
422 else
423 return thread__comm(thread);
424}
425
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200426int machine__process_comm_event(struct machine *machine, union perf_event *event,
427 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300428{
Adrian Hunter314add62013-08-27 11:23:03 +0300429 struct thread *thread = machine__findnew_thread(machine,
430 event->comm.pid,
431 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300432 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300433
Adrian Huntercfe1c412014-07-31 09:00:45 +0300434 if (exec)
435 machine->comm_exec = true;
436
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300437 if (dump_trace)
438 perf_event__fprintf_comm(event, stdout);
439
Adrian Hunter65de51f2014-07-31 09:00:44 +0300440 if (thread == NULL ||
441 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300442 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
443 return -1;
444 }
445
446 return 0;
447}
448
449int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200450 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300451{
452 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
453 event->lost.id, event->lost.lost);
454 return 0;
455}
456
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300457struct map *machine__new_module(struct machine *machine, u64 start,
458 const char *filename)
459{
460 struct map *map;
461 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
462
463 if (dso == NULL)
464 return NULL;
465
466 map = map__new2(start, dso, MAP__FUNCTION);
467 if (map == NULL)
468 return NULL;
469
470 if (machine__is_host(machine))
471 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
472 else
473 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
474 map_groups__insert(&machine->kmaps, map);
475 return map;
476}
477
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300478size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300479{
480 struct rb_node *nd;
Waiman Long8fa7d872014-09-29 16:07:28 -0400481 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos.head, fp) +
482 __dsos__fprintf(&machines->host.user_dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300483
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300484 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300485 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Waiman Long8fa7d872014-09-29 16:07:28 -0400486 ret += __dsos__fprintf(&pos->kernel_dsos.head, fp);
487 ret += __dsos__fprintf(&pos->user_dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300488 }
489
490 return ret;
491}
492
Waiman Long8fa7d872014-09-29 16:07:28 -0400493size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300494 bool (skip)(struct dso *dso, int parm), int parm)
495{
Waiman Long8fa7d872014-09-29 16:07:28 -0400496 return __dsos__fprintf_buildid(&m->kernel_dsos.head, fp, skip, parm) +
497 __dsos__fprintf_buildid(&m->user_dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300498}
499
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300500size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300501 bool (skip)(struct dso *dso, int parm), int parm)
502{
503 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300504 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300505
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300506 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300507 struct machine *pos = rb_entry(nd, struct machine, rb_node);
508 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
509 }
510 return ret;
511}
512
513size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
514{
515 int i;
516 size_t printed = 0;
517 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
518
519 if (kdso->has_build_id) {
520 char filename[PATH_MAX];
521 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
522 printed += fprintf(fp, "[0] %s\n", filename);
523 }
524
525 for (i = 0; i < vmlinux_path__nr_entries; ++i)
526 printed += fprintf(fp, "[%d] %s\n",
527 i + kdso->has_build_id, vmlinux_path[i]);
528
529 return printed;
530}
531
532size_t machine__fprintf(struct machine *machine, FILE *fp)
533{
534 size_t ret = 0;
535 struct rb_node *nd;
536
537 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
538 struct thread *pos = rb_entry(nd, struct thread, rb_node);
539
540 ret += thread__fprintf(pos, fp);
541 }
542
543 return ret;
544}
545
546static struct dso *machine__get_kernel(struct machine *machine)
547{
548 const char *vmlinux_name = NULL;
549 struct dso *kernel;
550
551 if (machine__is_host(machine)) {
552 vmlinux_name = symbol_conf.vmlinux_name;
553 if (!vmlinux_name)
554 vmlinux_name = "[kernel.kallsyms]";
555
556 kernel = dso__kernel_findnew(machine, vmlinux_name,
557 "[kernel]",
558 DSO_TYPE_KERNEL);
559 } else {
560 char bf[PATH_MAX];
561
562 if (machine__is_default_guest(machine))
563 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
564 if (!vmlinux_name)
565 vmlinux_name = machine__mmap_name(machine, bf,
566 sizeof(bf));
567
568 kernel = dso__kernel_findnew(machine, vmlinux_name,
569 "[guest.kernel]",
570 DSO_TYPE_GUEST_KERNEL);
571 }
572
573 if (kernel != NULL && (!kernel->has_build_id))
574 dso__read_running_kernel_build_id(kernel, machine);
575
576 return kernel;
577}
578
579struct process_args {
580 u64 start;
581};
582
Adrian Hunter15a0a872014-01-29 16:14:38 +0200583static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
584 size_t bufsz)
585{
586 if (machine__is_default_guest(machine))
587 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
588 else
589 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
590}
591
Simon Quea93f0e52014-06-16 11:32:09 -0700592const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
593
594/* Figure out the start address of kernel map from /proc/kallsyms.
595 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
596 * symbol_name if it's not that important.
597 */
Adrian Hunter4b993752014-08-15 22:08:38 +0300598static u64 machine__get_running_kernel_start(struct machine *machine,
599 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300600{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200601 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700602 int i;
603 const char *name;
604 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300605
Adrian Hunter15a0a872014-01-29 16:14:38 +0200606 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300607
608 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
609 return 0;
610
Simon Quea93f0e52014-06-16 11:32:09 -0700611 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
612 addr = kallsyms__get_function_start(filename, name);
613 if (addr)
614 break;
615 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300616
Simon Quea93f0e52014-06-16 11:32:09 -0700617 if (symbol_name)
618 *symbol_name = name;
619
620 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300621}
622
623int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
624{
625 enum map_type type;
Adrian Hunter4b993752014-08-15 22:08:38 +0300626 u64 start = machine__get_running_kernel_start(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300627
628 for (type = 0; type < MAP__NR_TYPES; ++type) {
629 struct kmap *kmap;
630
631 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
632 if (machine->vmlinux_maps[type] == NULL)
633 return -1;
634
635 machine->vmlinux_maps[type]->map_ip =
636 machine->vmlinux_maps[type]->unmap_ip =
637 identity__map_ip;
638 kmap = map__kmap(machine->vmlinux_maps[type]);
639 kmap->kmaps = &machine->kmaps;
640 map_groups__insert(&machine->kmaps,
641 machine->vmlinux_maps[type]);
642 }
643
644 return 0;
645}
646
647void machine__destroy_kernel_maps(struct machine *machine)
648{
649 enum map_type type;
650
651 for (type = 0; type < MAP__NR_TYPES; ++type) {
652 struct kmap *kmap;
653
654 if (machine->vmlinux_maps[type] == NULL)
655 continue;
656
657 kmap = map__kmap(machine->vmlinux_maps[type]);
658 map_groups__remove(&machine->kmaps,
659 machine->vmlinux_maps[type]);
660 if (kmap->ref_reloc_sym) {
661 /*
662 * ref_reloc_sym is shared among all maps, so free just
663 * on one of them.
664 */
665 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300666 zfree((char **)&kmap->ref_reloc_sym->name);
667 zfree(&kmap->ref_reloc_sym);
668 } else
669 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300670 }
671
672 map__delete(machine->vmlinux_maps[type]);
673 machine->vmlinux_maps[type] = NULL;
674 }
675}
676
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300677int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300678{
679 int ret = 0;
680 struct dirent **namelist = NULL;
681 int i, items = 0;
682 char path[PATH_MAX];
683 pid_t pid;
684 char *endp;
685
686 if (symbol_conf.default_guest_vmlinux_name ||
687 symbol_conf.default_guest_modules ||
688 symbol_conf.default_guest_kallsyms) {
689 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
690 }
691
692 if (symbol_conf.guestmount) {
693 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
694 if (items <= 0)
695 return -ENOENT;
696 for (i = 0; i < items; i++) {
697 if (!isdigit(namelist[i]->d_name[0])) {
698 /* Filter out . and .. */
699 continue;
700 }
701 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
702 if ((*endp != '\0') ||
703 (endp == namelist[i]->d_name) ||
704 (errno == ERANGE)) {
705 pr_debug("invalid directory (%s). Skipping.\n",
706 namelist[i]->d_name);
707 continue;
708 }
709 sprintf(path, "%s/%s/proc/kallsyms",
710 symbol_conf.guestmount,
711 namelist[i]->d_name);
712 ret = access(path, R_OK);
713 if (ret) {
714 pr_debug("Can't access file %s\n", path);
715 goto failure;
716 }
717 machines__create_kernel_maps(machines, pid);
718 }
719failure:
720 free(namelist);
721 }
722
723 return ret;
724}
725
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300726void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300727{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300728 struct rb_node *next = rb_first(&machines->guests);
729
730 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300731
732 while (next) {
733 struct machine *pos = rb_entry(next, struct machine, rb_node);
734
735 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300736 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300737 machine__delete(pos);
738 }
739}
740
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300741int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300742{
743 struct machine *machine = machines__findnew(machines, pid);
744
745 if (machine == NULL)
746 return -1;
747
748 return machine__create_kernel_maps(machine);
749}
750
751int machine__load_kallsyms(struct machine *machine, const char *filename,
752 enum map_type type, symbol_filter_t filter)
753{
754 struct map *map = machine->vmlinux_maps[type];
755 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
756
757 if (ret > 0) {
758 dso__set_loaded(map->dso, type);
759 /*
760 * Since /proc/kallsyms will have multiple sessions for the
761 * kernel, with modules between them, fixup the end of all
762 * sections.
763 */
764 __map_groups__fixup_end(&machine->kmaps, type);
765 }
766
767 return ret;
768}
769
770int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
771 symbol_filter_t filter)
772{
773 struct map *map = machine->vmlinux_maps[type];
774 int ret = dso__load_vmlinux_path(map->dso, map, filter);
775
Adrian Hunter39b12f782013-08-07 14:38:47 +0300776 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300777 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300778
779 return ret;
780}
781
782static void map_groups__fixup_end(struct map_groups *mg)
783{
784 int i;
785 for (i = 0; i < MAP__NR_TYPES; ++i)
786 __map_groups__fixup_end(mg, i);
787}
788
789static char *get_kernel_version(const char *root_dir)
790{
791 char version[PATH_MAX];
792 FILE *file;
793 char *name, *tmp;
794 const char *prefix = "Linux version ";
795
796 sprintf(version, "%s/proc/version", root_dir);
797 file = fopen(version, "r");
798 if (!file)
799 return NULL;
800
801 version[0] = '\0';
802 tmp = fgets(version, sizeof(version), file);
803 fclose(file);
804
805 name = strstr(version, prefix);
806 if (!name)
807 return NULL;
808 name += strlen(prefix);
809 tmp = strchr(name, ' ');
810 if (tmp)
811 *tmp = '\0';
812
813 return strdup(name);
814}
815
816static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -0400817 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300818{
819 struct dirent *dent;
820 DIR *dir = opendir(dir_name);
821 int ret = 0;
822
823 if (!dir) {
824 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
825 return -1;
826 }
827
828 while ((dent = readdir(dir)) != NULL) {
829 char path[PATH_MAX];
830 struct stat st;
831
832 /*sshfs might return bad dent->d_type, so we have to stat*/
833 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
834 if (stat(path, &st))
835 continue;
836
837 if (S_ISDIR(st.st_mode)) {
838 if (!strcmp(dent->d_name, ".") ||
839 !strcmp(dent->d_name, ".."))
840 continue;
841
Richard Yao61d42902014-04-26 13:17:55 -0400842 /* Do not follow top-level source and build symlinks */
843 if (depth == 0) {
844 if (!strcmp(dent->d_name, "source") ||
845 !strcmp(dent->d_name, "build"))
846 continue;
847 }
848
849 ret = map_groups__set_modules_path_dir(mg, path,
850 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300851 if (ret < 0)
852 goto out;
853 } else {
854 char *dot = strrchr(dent->d_name, '.'),
855 dso_name[PATH_MAX];
856 struct map *map;
857 char *long_name;
858
859 if (dot == NULL || strcmp(dot, ".ko"))
860 continue;
861 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
862 (int)(dot - dent->d_name), dent->d_name);
863
864 strxfrchar(dso_name, '-', '_');
865 map = map_groups__find_by_name(mg, MAP__FUNCTION,
866 dso_name);
867 if (map == NULL)
868 continue;
869
870 long_name = strdup(path);
871 if (long_name == NULL) {
872 ret = -1;
873 goto out;
874 }
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300875 dso__set_long_name(map->dso, long_name, true);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300876 dso__kernel_module_get_build_id(map->dso, "");
877 }
878 }
879
880out:
881 closedir(dir);
882 return ret;
883}
884
885static int machine__set_modules_path(struct machine *machine)
886{
887 char *version;
888 char modules_path[PATH_MAX];
889
890 version = get_kernel_version(machine->root_dir);
891 if (!version)
892 return -1;
893
Richard Yao61d42902014-04-26 13:17:55 -0400894 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300895 machine->root_dir, version);
896 free(version);
897
Richard Yao61d42902014-04-26 13:17:55 -0400898 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300899}
900
Adrian Hunter316d70d2013-10-08 11:45:48 +0300901static int machine__create_module(void *arg, const char *name, u64 start)
902{
903 struct machine *machine = arg;
904 struct map *map;
905
906 map = machine__new_module(machine, start, name);
907 if (map == NULL)
908 return -1;
909
910 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
911
912 return 0;
913}
914
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300915static int machine__create_modules(struct machine *machine)
916{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300917 const char *modules;
918 char path[PATH_MAX];
919
Adrian Hunterf4be9042013-09-22 13:22:09 +0300920 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300921 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +0300922 } else {
923 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300924 modules = path;
925 }
926
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +0300927 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300928 return -1;
929
Adrian Hunter316d70d2013-10-08 11:45:48 +0300930 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300931 return -1;
932
Adrian Hunter316d70d2013-10-08 11:45:48 +0300933 if (!machine__set_modules_path(machine))
934 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300935
Adrian Hunter316d70d2013-10-08 11:45:48 +0300936 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300937
Jason Wessel8f76fcd2013-07-15 15:27:53 -0500938 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300939}
940
941int machine__create_kernel_maps(struct machine *machine)
942{
943 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200944 const char *name;
Adrian Hunter4b993752014-08-15 22:08:38 +0300945 u64 addr = machine__get_running_kernel_start(machine, &name);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200946 if (!addr)
947 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300948
949 if (kernel == NULL ||
950 __machine__create_kernel_maps(machine, kernel) < 0)
951 return -1;
952
953 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
954 if (machine__is_host(machine))
955 pr_debug("Problems creating module maps, "
956 "continuing anyway...\n");
957 else
958 pr_debug("Problems creating module maps for guest %d, "
959 "continuing anyway...\n", machine->pid);
960 }
961
962 /*
963 * Now that we have all the maps created, just set the ->end of them:
964 */
965 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +0200966
967 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
968 addr)) {
969 machine__destroy_kernel_maps(machine);
970 return -1;
971 }
972
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300973 return 0;
974}
975
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300976static void machine__set_kernel_mmap_len(struct machine *machine,
977 union perf_event *event)
978{
Namhyung Kim4552cf0f2012-11-07 16:27:10 +0900979 int i;
980
981 for (i = 0; i < MAP__NR_TYPES; i++) {
982 machine->vmlinux_maps[i]->start = event->mmap.start;
983 machine->vmlinux_maps[i]->end = (event->mmap.start +
984 event->mmap.len);
985 /*
986 * Be a bit paranoid here, some perf.data file came with
987 * a zero sized synthesized MMAP event for the kernel.
988 */
989 if (machine->vmlinux_maps[i]->end == 0)
990 machine->vmlinux_maps[i]->end = ~0ULL;
991 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300992}
993
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300994static bool machine__uses_kcore(struct machine *machine)
995{
996 struct dso *dso;
997
Waiman Long8fa7d872014-09-29 16:07:28 -0400998 list_for_each_entry(dso, &machine->kernel_dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300999 if (dso__is_kcore(dso))
1000 return true;
1001 }
1002
1003 return false;
1004}
1005
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001006static int machine__process_kernel_mmap_event(struct machine *machine,
1007 union perf_event *event)
1008{
1009 struct map *map;
1010 char kmmap_prefix[PATH_MAX];
1011 enum dso_kernel_type kernel_type;
1012 bool is_kernel_mmap;
1013
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001014 /* If we have maps from kcore then we do not need or want any others */
1015 if (machine__uses_kcore(machine))
1016 return 0;
1017
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001018 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1019 if (machine__is_host(machine))
1020 kernel_type = DSO_TYPE_KERNEL;
1021 else
1022 kernel_type = DSO_TYPE_GUEST_KERNEL;
1023
1024 is_kernel_mmap = memcmp(event->mmap.filename,
1025 kmmap_prefix,
1026 strlen(kmmap_prefix) - 1) == 0;
1027 if (event->mmap.filename[0] == '/' ||
1028 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1029
1030 char short_module_name[1024];
1031 char *name, *dot;
1032
1033 if (event->mmap.filename[0] == '/') {
1034 name = strrchr(event->mmap.filename, '/');
1035 if (name == NULL)
1036 goto out_problem;
1037
1038 ++name; /* skip / */
1039 dot = strrchr(name, '.');
1040 if (dot == NULL)
1041 goto out_problem;
1042 snprintf(short_module_name, sizeof(short_module_name),
1043 "[%.*s]", (int)(dot - name), name);
1044 strxfrchar(short_module_name, '-', '_');
1045 } else
1046 strcpy(short_module_name, event->mmap.filename);
1047
1048 map = machine__new_module(machine, event->mmap.start,
1049 event->mmap.filename);
1050 if (map == NULL)
1051 goto out_problem;
1052
1053 name = strdup(short_module_name);
1054 if (name == NULL)
1055 goto out_problem;
1056
Adrian Hunter58a98c92013-12-10 11:11:46 -03001057 dso__set_short_name(map->dso, name, true);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001058 map->end = map->start + event->mmap.len;
1059 } else if (is_kernel_mmap) {
1060 const char *symbol_name = (event->mmap.filename +
1061 strlen(kmmap_prefix));
1062 /*
1063 * Should be there already, from the build-id table in
1064 * the header.
1065 */
1066 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
1067 kmmap_prefix);
1068 if (kernel == NULL)
1069 goto out_problem;
1070
1071 kernel->kernel = kernel_type;
1072 if (__machine__create_kernel_maps(machine, kernel) < 0)
1073 goto out_problem;
1074
1075 machine__set_kernel_mmap_len(machine, event);
1076
1077 /*
1078 * Avoid using a zero address (kptr_restrict) for the ref reloc
1079 * symbol. Effectively having zero here means that at record
1080 * time /proc/sys/kernel/kptr_restrict was non zero.
1081 */
1082 if (event->mmap.pgoff != 0) {
1083 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1084 symbol_name,
1085 event->mmap.pgoff);
1086 }
1087
1088 if (machine__is_default_guest(machine)) {
1089 /*
1090 * preload dso of guest kernel and modules
1091 */
1092 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
1093 NULL);
1094 }
1095 }
1096 return 0;
1097out_problem:
1098 return -1;
1099}
1100
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001101int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001102 union perf_event *event,
1103 struct perf_sample *sample __maybe_unused)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001104{
1105 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1106 struct thread *thread;
1107 struct map *map;
1108 enum map_type type;
1109 int ret = 0;
1110
1111 if (dump_trace)
1112 perf_event__fprintf_mmap2(event, stdout);
1113
1114 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1115 cpumode == PERF_RECORD_MISC_KERNEL) {
1116 ret = machine__process_kernel_mmap_event(machine, event);
1117 if (ret < 0)
1118 goto out_problem;
1119 return 0;
1120 }
1121
1122 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001123 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001124 if (thread == NULL)
1125 goto out_problem;
1126
1127 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1128 type = MAP__VARIABLE;
1129 else
1130 type = MAP__FUNCTION;
1131
Adrian Hunter2a030682014-07-22 16:17:53 +03001132 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001133 event->mmap2.len, event->mmap2.pgoff,
1134 event->mmap2.pid, event->mmap2.maj,
1135 event->mmap2.min, event->mmap2.ino,
1136 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001137 event->mmap2.prot,
1138 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001139 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001140
1141 if (map == NULL)
1142 goto out_problem;
1143
1144 thread__insert_map(thread, map);
1145 return 0;
1146
1147out_problem:
1148 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1149 return 0;
1150}
1151
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001152int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1153 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001154{
1155 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1156 struct thread *thread;
1157 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001158 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001159 int ret = 0;
1160
1161 if (dump_trace)
1162 perf_event__fprintf_mmap(event, stdout);
1163
1164 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1165 cpumode == PERF_RECORD_MISC_KERNEL) {
1166 ret = machine__process_kernel_mmap_event(machine, event);
1167 if (ret < 0)
1168 goto out_problem;
1169 return 0;
1170 }
1171
Adrian Hunter314add62013-08-27 11:23:03 +03001172 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001173 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001174 if (thread == NULL)
1175 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001176
1177 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1178 type = MAP__VARIABLE;
1179 else
1180 type = MAP__FUNCTION;
1181
Adrian Hunter2a030682014-07-22 16:17:53 +03001182 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001183 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001184 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001185 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001186 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001187
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001188 if (map == NULL)
1189 goto out_problem;
1190
1191 thread__insert_map(thread, map);
1192 return 0;
1193
1194out_problem:
1195 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1196 return 0;
1197}
1198
David Ahern236a3bb2013-08-14 08:49:27 -06001199static void machine__remove_thread(struct machine *machine, struct thread *th)
1200{
1201 machine->last_match = NULL;
1202 rb_erase(&th->rb_node, &machine->threads);
1203 /*
1204 * We may have references to this thread, for instance in some hist_entry
1205 * instances, so just move them to a separate list.
1206 */
1207 list_add_tail(&th->node, &machine->dead_threads);
1208}
1209
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001210int machine__process_fork_event(struct machine *machine, union perf_event *event,
1211 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001212{
Jiri Olsad75e6092014-03-14 15:00:03 +01001213 struct thread *thread = machine__find_thread(machine,
1214 event->fork.pid,
1215 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001216 struct thread *parent = machine__findnew_thread(machine,
1217 event->fork.ppid,
1218 event->fork.ptid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001219
David Ahern236a3bb2013-08-14 08:49:27 -06001220 /* if a thread currently exists for the thread id remove it */
1221 if (thread != NULL)
1222 machine__remove_thread(machine, thread);
1223
Adrian Hunter314add62013-08-27 11:23:03 +03001224 thread = machine__findnew_thread(machine, event->fork.pid,
1225 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001226 if (dump_trace)
1227 perf_event__fprintf_task(event, stdout);
1228
1229 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001230 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001231 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1232 return -1;
1233 }
1234
1235 return 0;
1236}
1237
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001238int machine__process_exit_event(struct machine *machine, union perf_event *event,
1239 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001240{
Jiri Olsad75e6092014-03-14 15:00:03 +01001241 struct thread *thread = machine__find_thread(machine,
1242 event->fork.pid,
1243 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001244
1245 if (dump_trace)
1246 perf_event__fprintf_task(event, stdout);
1247
1248 if (thread != NULL)
David Ahern236a3bb2013-08-14 08:49:27 -06001249 thread__exited(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001250
1251 return 0;
1252}
1253
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001254int machine__process_event(struct machine *machine, union perf_event *event,
1255 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001256{
1257 int ret;
1258
1259 switch (event->header.type) {
1260 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001261 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001262 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001263 ret = machine__process_mmap_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001264 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001265 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001266 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001267 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001268 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001269 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001270 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001271 ret = machine__process_lost_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001272 default:
1273 ret = -1;
1274 break;
1275 }
1276
1277 return ret;
1278}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001279
Greg Priceb21484f2012-12-06 21:48:05 -08001280static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001281{
Greg Priceb21484f2012-12-06 21:48:05 -08001282 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001283 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001284 return 0;
1285}
1286
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001287static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1288 struct addr_map_symbol *ams,
1289 u64 ip)
1290{
1291 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001292
1293 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001294 /*
1295 * We cannot use the header.misc hint to determine whether a
1296 * branch stack address is user, kernel, guest, hypervisor.
1297 * Branches may straddle the kernel/user/hypervisor boundaries.
1298 * Thus, we have to try consecutively until we find a match
1299 * or else, the symbol is unknown
1300 */
1301 thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001302
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001303 ams->addr = ip;
1304 ams->al_addr = al.addr;
1305 ams->sym = al.sym;
1306 ams->map = al.map;
1307}
1308
Stephane Eranian98a3b322013-01-24 16:10:35 +01001309static void ip__resolve_data(struct machine *machine, struct thread *thread,
1310 u8 m, struct addr_map_symbol *ams, u64 addr)
1311{
1312 struct addr_location al;
1313
1314 memset(&al, 0, sizeof(al));
1315
Adrian Hunter61710bd2013-08-08 14:32:26 +03001316 thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
1317 &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001318 if (al.map == NULL) {
1319 /*
1320 * some shared data regions have execute bit set which puts
1321 * their mapping in the MAP__FUNCTION type array.
1322 * Check there as a fallback option before dropping the sample.
1323 */
1324 thread__find_addr_location(thread, machine, m, MAP__FUNCTION, addr,
1325 &al);
1326 }
1327
Stephane Eranian98a3b322013-01-24 16:10:35 +01001328 ams->addr = addr;
1329 ams->al_addr = al.addr;
1330 ams->sym = al.sym;
1331 ams->map = al.map;
1332}
1333
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001334struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1335 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001336{
1337 struct mem_info *mi = zalloc(sizeof(*mi));
1338
1339 if (!mi)
1340 return NULL;
1341
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001342 ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip);
1343 ip__resolve_data(al->machine, al->thread, al->cpumode,
1344 &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001345 mi->data_src.val = sample->data_src;
1346
1347 return mi;
1348}
1349
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001350struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1351 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001352{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001353 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001354 const struct branch_stack *bs = sample->branch_stack;
1355 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001356
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001357 if (!bi)
1358 return NULL;
1359
1360 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001361 ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to);
1362 ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001363 bi[i].flags = bs->entries[i].flags;
1364 }
1365 return bi;
1366}
1367
1368static int machine__resolve_callchain_sample(struct machine *machine,
1369 struct thread *thread,
1370 struct ip_callchain *chain,
Greg Priceb21484f2012-12-06 21:48:05 -08001371 struct symbol **parent,
Waiman Long91e95612013-10-18 10:38:48 -04001372 struct addr_location *root_al,
1373 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001374{
1375 u8 cpumode = PERF_RECORD_MISC_USER;
Waiman Long91e95612013-10-18 10:38:48 -04001376 int chain_nr = min(max_stack, (int)chain->nr);
1377 int i;
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001378 int j;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001379 int err;
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001380 int skip_idx __maybe_unused;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001381
1382 callchain_cursor_reset(&callchain_cursor);
1383
1384 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1385 pr_warning("corrupted callchain. skipping...\n");
1386 return 0;
1387 }
1388
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001389 /*
1390 * Based on DWARF debug information, some architectures skip
1391 * a callchain entry saved by the kernel.
1392 */
1393 skip_idx = arch_skip_callchain_idx(machine, thread, chain);
1394
Waiman Long91e95612013-10-18 10:38:48 -04001395 for (i = 0; i < chain_nr; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001396 u64 ip;
1397 struct addr_location al;
1398
1399 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001400 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001401 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001402 j = chain->nr - i - 1;
1403
1404#ifdef HAVE_SKIP_CALLCHAIN_IDX
1405 if (j == skip_idx)
1406 continue;
1407#endif
1408 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001409
1410 if (ip >= PERF_CONTEXT_MAX) {
1411 switch (ip) {
1412 case PERF_CONTEXT_HV:
1413 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1414 break;
1415 case PERF_CONTEXT_KERNEL:
1416 cpumode = PERF_RECORD_MISC_KERNEL;
1417 break;
1418 case PERF_CONTEXT_USER:
1419 cpumode = PERF_RECORD_MISC_USER;
1420 break;
1421 default:
1422 pr_debug("invalid callchain context: "
1423 "%"PRId64"\n", (s64) ip);
1424 /*
1425 * It seems the callchain is corrupted.
1426 * Discard all.
1427 */
1428 callchain_cursor_reset(&callchain_cursor);
1429 return 0;
1430 }
1431 continue;
1432 }
1433
Namhyung Kimb3cef7f2014-03-17 16:59:21 -03001434 al.filtered = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001435 thread__find_addr_location(thread, machine, cpumode,
Adrian Hunter61710bd2013-08-08 14:32:26 +03001436 MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001437 if (al.sym != NULL) {
1438 if (sort__has_parent && !*parent &&
Greg Priceb21484f2012-12-06 21:48:05 -08001439 symbol__match_regex(al.sym, &parent_regex))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001440 *parent = al.sym;
Greg Priceb21484f2012-12-06 21:48:05 -08001441 else if (have_ignore_callees && root_al &&
1442 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1443 /* Treat this symbol as the root,
1444 forgetting its callees. */
1445 *root_al = al;
1446 callchain_cursor_reset(&callchain_cursor);
1447 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001448 }
1449
1450 err = callchain_cursor_append(&callchain_cursor,
1451 ip, al.map, al.sym);
1452 if (err)
1453 return err;
1454 }
1455
1456 return 0;
1457}
1458
1459static int unwind_entry(struct unwind_entry *entry, void *arg)
1460{
1461 struct callchain_cursor *cursor = arg;
1462 return callchain_cursor_append(cursor, entry->ip,
1463 entry->map, entry->sym);
1464}
1465
1466int machine__resolve_callchain(struct machine *machine,
1467 struct perf_evsel *evsel,
1468 struct thread *thread,
1469 struct perf_sample *sample,
Greg Priceb21484f2012-12-06 21:48:05 -08001470 struct symbol **parent,
Waiman Long91e95612013-10-18 10:38:48 -04001471 struct addr_location *root_al,
1472 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001473{
1474 int ret;
1475
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001476 ret = machine__resolve_callchain_sample(machine, thread,
Waiman Long91e95612013-10-18 10:38:48 -04001477 sample->callchain, parent,
1478 root_al, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001479 if (ret)
1480 return ret;
1481
1482 /* Can we do dwarf post unwind? */
1483 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1484 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1485 return 0;
1486
1487 /* Bail out if nothing was captured. */
1488 if ((!sample->user_regs.regs) ||
1489 (!sample->user_stack.size))
1490 return 0;
1491
1492 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
Jiri Olsa352ea452014-01-07 13:47:25 +01001493 thread, sample, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001494
1495}
David Ahern35feee12013-09-28 13:12:58 -06001496
1497int machine__for_each_thread(struct machine *machine,
1498 int (*fn)(struct thread *thread, void *p),
1499 void *priv)
1500{
1501 struct rb_node *nd;
1502 struct thread *thread;
1503 int rc = 0;
1504
1505 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1506 thread = rb_entry(nd, struct thread, rb_node);
1507 rc = fn(thread, priv);
1508 if (rc != 0)
1509 return rc;
1510 }
1511
1512 list_for_each_entry(thread, &machine->dead_threads, node) {
1513 rc = fn(thread, priv);
1514 if (rc != 0)
1515 return rc;
1516 }
1517 return rc;
1518}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001519
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001520int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001521 struct target *target, struct thread_map *threads,
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001522 perf_event__handler_t process, bool data_mmap)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001523{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001524 if (target__has_task(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001525 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001526 else if (target__has_cpu(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001527 return perf_event__synthesize_threads(tool, process, machine, data_mmap);
1528 /* command specified */
1529 return 0;
1530}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03001531
1532pid_t machine__get_current_tid(struct machine *machine, int cpu)
1533{
1534 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
1535 return -1;
1536
1537 return machine->current_tid[cpu];
1538}
1539
1540int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
1541 pid_t tid)
1542{
1543 struct thread *thread;
1544
1545 if (cpu < 0)
1546 return -EINVAL;
1547
1548 if (!machine->current_tid) {
1549 int i;
1550
1551 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
1552 if (!machine->current_tid)
1553 return -ENOMEM;
1554 for (i = 0; i < MAX_NR_CPUS; i++)
1555 machine->current_tid[i] = -1;
1556 }
1557
1558 if (cpu >= MAX_NR_CPUS) {
1559 pr_err("Requested CPU %d too large. ", cpu);
1560 pr_err("Consider raising MAX_NR_CPUS\n");
1561 return -EINVAL;
1562 }
1563
1564 machine->current_tid[cpu] = tid;
1565
1566 thread = machine__findnew_thread(machine, pid, tid);
1567 if (!thread)
1568 return -ENOMEM;
1569
1570 thread->cpu = cpu;
1571
1572 return 0;
1573}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03001574
1575int machine__get_kernel_start(struct machine *machine)
1576{
1577 struct map *map = machine__kernel_map(machine, MAP__FUNCTION);
1578 int err = 0;
1579
1580 /*
1581 * The only addresses above 2^63 are kernel addresses of a 64-bit
1582 * kernel. Note that addresses are unsigned so that on a 32-bit system
1583 * all addresses including kernel addresses are less than 2^32. In
1584 * that case (32-bit system), if the kernel mapping is unknown, all
1585 * addresses will be assumed to be in user space - see
1586 * machine__kernel_ip().
1587 */
1588 machine->kernel_start = 1ULL << 63;
1589 if (map) {
1590 err = map__load(map, machine->symbol_filter);
1591 if (map->start)
1592 machine->kernel_start = map->start;
1593 }
1594 return err;
1595}