blob: dfd419797e6ea42e5d609e876d46f667abddcb6d [file] [log] [blame]
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001#include "callchain.h"
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03002#include "debug.h"
3#include "event.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03004#include "evsel.h"
5#include "hist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -03006#include "machine.h"
7#include "map.h"
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03008#include "sort.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -03009#include "strlist.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030010#include "thread.h"
Adrian Hunterd027b642014-07-23 14:23:00 +030011#include "vdso.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030012#include <stdbool.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030013#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030014#include "unwind.h"
Andi Kleen8b7bad52014-11-12 18:05:20 -080015#include "linux/hash.h"
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030016
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030017static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
18
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030019static void dsos__init(struct dsos *dsos)
20{
21 INIT_LIST_HEAD(&dsos->head);
22 dsos->root = RB_ROOT;
23}
24
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030025int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
26{
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030027 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030028 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030029 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030030
31 machine->threads = RB_ROOT;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030032 pthread_rwlock_init(&machine->threads_lock, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030033 INIT_LIST_HEAD(&machine->dead_threads);
34 machine->last_match = NULL;
35
Adrian Hunterd027b642014-07-23 14:23:00 +030036 machine->vdso_info = NULL;
37
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030038 machine->pid = pid;
39
Adrian Hunter611a5ce2013-08-08 14:32:20 +030040 machine->symbol_filter = NULL;
Jiri Olsa14bd6d22014-01-07 13:47:19 +010041 machine->id_hdr_size = 0;
Adrian Huntercfe1c412014-07-31 09:00:45 +030042 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030043 machine->kernel_start = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030044
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030045 machine->root_dir = strdup(root_dir);
46 if (machine->root_dir == NULL)
47 return -ENOMEM;
48
49 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030050 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030051 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030052 char comm[64];
53
54 if (thread == NULL)
55 return -ENOMEM;
56
57 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020058 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030059 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030060 }
61
Adrian Hunterb9d266b2014-07-22 16:17:25 +030062 machine->current_tid = NULL;
63
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030064 return 0;
65}
66
David Ahern8fb598e2013-09-28 13:13:00 -060067struct machine *machine__new_host(void)
68{
69 struct machine *machine = malloc(sizeof(*machine));
70
71 if (machine != NULL) {
72 machine__init(machine, "", HOST_KERNEL_ID);
73
74 if (machine__create_kernel_maps(machine) < 0)
75 goto out_delete;
76 }
77
78 return machine;
79out_delete:
80 free(machine);
81 return NULL;
82}
83
Waiman Long8fa7d872014-09-29 16:07:28 -040084static void dsos__delete(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030085{
86 struct dso *pos, *n;
87
Waiman Long8fa7d872014-09-29 16:07:28 -040088 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -040089 RB_CLEAR_NODE(&pos->rb_node);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030090 list_del(&pos->node);
91 dso__delete(pos);
92 }
93}
94
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030095void machine__delete_threads(struct machine *machine)
96{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030097 struct rb_node *nd;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030098
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030099 pthread_rwlock_wrlock(&machine->threads_lock);
100 nd = rb_first(&machine->threads);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300101 while (nd) {
102 struct thread *t = rb_entry(nd, struct thread, rb_node);
103
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300104 nd = rb_next(nd);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300105 __machine__remove_thread(machine, t, false);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300106 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300107 pthread_rwlock_unlock(&machine->threads_lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300108}
109
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300110void machine__exit(struct machine *machine)
111{
112 map_groups__exit(&machine->kmaps);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300113 dsos__delete(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300114 machine__exit_vdso(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 Melob91fc392015-04-06 20:43:22 -0300117 pthread_rwlock_destroy(&machine->threads_lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300118}
119
120void machine__delete(struct machine *machine)
121{
122 machine__exit(machine);
123 free(machine);
124}
125
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300126void machines__init(struct machines *machines)
127{
128 machine__init(&machines->host, "", HOST_KERNEL_ID);
129 machines->guests = RB_ROOT;
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300130 machines->symbol_filter = NULL;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300131}
132
133void machines__exit(struct machines *machines)
134{
135 machine__exit(&machines->host);
136 /* XXX exit guest */
137}
138
139struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300140 const char *root_dir)
141{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300142 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300143 struct rb_node *parent = NULL;
144 struct machine *pos, *machine = malloc(sizeof(*machine));
145
146 if (machine == NULL)
147 return NULL;
148
149 if (machine__init(machine, root_dir, pid) != 0) {
150 free(machine);
151 return NULL;
152 }
153
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300154 machine->symbol_filter = machines->symbol_filter;
155
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300156 while (*p != NULL) {
157 parent = *p;
158 pos = rb_entry(parent, struct machine, rb_node);
159 if (pid < pos->pid)
160 p = &(*p)->rb_left;
161 else
162 p = &(*p)->rb_right;
163 }
164
165 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300166 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300167
168 return machine;
169}
170
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300171void machines__set_symbol_filter(struct machines *machines,
172 symbol_filter_t symbol_filter)
173{
174 struct rb_node *nd;
175
176 machines->symbol_filter = symbol_filter;
177 machines->host.symbol_filter = symbol_filter;
178
179 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
180 struct machine *machine = rb_entry(nd, struct machine, rb_node);
181
182 machine->symbol_filter = symbol_filter;
183 }
184}
185
Adrian Huntercfe1c412014-07-31 09:00:45 +0300186void machines__set_comm_exec(struct machines *machines, bool comm_exec)
187{
188 struct rb_node *nd;
189
190 machines->host.comm_exec = comm_exec;
191
192 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
193 struct machine *machine = rb_entry(nd, struct machine, rb_node);
194
195 machine->comm_exec = comm_exec;
196 }
197}
198
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300199struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300200{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300201 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300202 struct rb_node *parent = NULL;
203 struct machine *machine;
204 struct machine *default_machine = NULL;
205
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300206 if (pid == HOST_KERNEL_ID)
207 return &machines->host;
208
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300209 while (*p != NULL) {
210 parent = *p;
211 machine = rb_entry(parent, struct machine, rb_node);
212 if (pid < machine->pid)
213 p = &(*p)->rb_left;
214 else if (pid > machine->pid)
215 p = &(*p)->rb_right;
216 else
217 return machine;
218 if (!machine->pid)
219 default_machine = machine;
220 }
221
222 return default_machine;
223}
224
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300225struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300226{
227 char path[PATH_MAX];
228 const char *root_dir = "";
229 struct machine *machine = machines__find(machines, pid);
230
231 if (machine && (machine->pid == pid))
232 goto out;
233
234 if ((pid != HOST_KERNEL_ID) &&
235 (pid != DEFAULT_GUEST_KERNEL_ID) &&
236 (symbol_conf.guestmount)) {
237 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
238 if (access(path, R_OK)) {
239 static struct strlist *seen;
240
241 if (!seen)
242 seen = strlist__new(true, NULL);
243
244 if (!strlist__has_entry(seen, path)) {
245 pr_err("Can't access file %s\n", path);
246 strlist__add(seen, path);
247 }
248 machine = NULL;
249 goto out;
250 }
251 root_dir = path;
252 }
253
254 machine = machines__add(machines, pid, root_dir);
255out:
256 return machine;
257}
258
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300259void machines__process_guests(struct machines *machines,
260 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300261{
262 struct rb_node *nd;
263
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300264 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300265 struct machine *pos = rb_entry(nd, struct machine, rb_node);
266 process(pos, data);
267 }
268}
269
270char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
271{
272 if (machine__is_host(machine))
273 snprintf(bf, size, "[%s]", "kernel.kallsyms");
274 else if (machine__is_default_guest(machine))
275 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
276 else {
277 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
278 machine->pid);
279 }
280
281 return bf;
282}
283
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300284void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300285{
286 struct rb_node *node;
287 struct machine *machine;
288
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300289 machines->host.id_hdr_size = id_hdr_size;
290
291 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300292 machine = rb_entry(node, struct machine, rb_node);
293 machine->id_hdr_size = id_hdr_size;
294 }
295
296 return;
297}
298
Adrian Hunter29ce3612014-07-16 11:07:13 +0300299static void machine__update_thread_pid(struct machine *machine,
300 struct thread *th, pid_t pid)
301{
302 struct thread *leader;
303
304 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
305 return;
306
307 th->pid_ = pid;
308
309 if (th->pid_ == th->tid)
310 return;
311
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300312 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300313 if (!leader)
314 goto out_err;
315
316 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300317 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300318
319 if (!leader->mg)
320 goto out_err;
321
322 if (th->mg == leader->mg)
323 return;
324
325 if (th->mg) {
326 /*
327 * Maps are created from MMAP events which provide the pid and
328 * tid. Consequently there never should be any maps on a thread
329 * with an unknown pid. Just print an error if there are.
330 */
331 if (!map_groups__empty(th->mg))
332 pr_err("Discarding thread maps for %d:%d\n",
333 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300334 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300335 }
336
337 th->mg = map_groups__get(leader->mg);
338
339 return;
340
341out_err:
342 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
343}
344
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300345static struct thread *____machine__findnew_thread(struct machine *machine,
346 pid_t pid, pid_t tid,
347 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300348{
349 struct rb_node **p = &machine->threads.rb_node;
350 struct rb_node *parent = NULL;
351 struct thread *th;
352
353 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300354 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300355 * so most of the time we dont have to look up
356 * the full rbtree:
357 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300358 th = machine->last_match;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300359 if (th != NULL) {
360 if (th->tid == tid) {
361 machine__update_thread_pid(machine, th, pid);
362 return th;
363 }
364
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300365 machine->last_match = NULL;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300366 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300367
368 while (*p != NULL) {
369 parent = *p;
370 th = rb_entry(parent, struct thread, rb_node);
371
Adrian Hunter38051232013-07-04 16:20:31 +0300372 if (th->tid == tid) {
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300373 machine->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300374 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300375 return th;
376 }
377
Adrian Hunter38051232013-07-04 16:20:31 +0300378 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300379 p = &(*p)->rb_left;
380 else
381 p = &(*p)->rb_right;
382 }
383
384 if (!create)
385 return NULL;
386
Adrian Hunter99d725f2013-08-26 16:00:19 +0300387 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300388 if (th != NULL) {
389 rb_link_node(&th->rb_node, parent, p);
390 rb_insert_color(&th->rb_node, &machine->threads);
Jiri Olsacddcef62014-04-09 20:54:29 +0200391
392 /*
393 * We have to initialize map_groups separately
394 * after rb tree is updated.
395 *
396 * The reason is that we call machine__findnew_thread
397 * within thread__init_map_groups to find the thread
398 * leader and that would screwed the rb tree.
399 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300400 if (thread__init_map_groups(th, machine)) {
Arnaldo Carvalho de Melo0170b142015-05-25 15:23:05 -0300401 rb_erase_init(&th->rb_node, &machine->threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300402 RB_CLEAR_NODE(&th->rb_node);
Adrian Hunter418029b2014-07-16 10:19:44 +0300403 thread__delete(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200404 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300405 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300406 /*
407 * It is now in the rbtree, get a ref
408 */
409 thread__get(th);
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300410 machine->last_match = th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300411 }
412
413 return th;
414}
415
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300416struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
417{
418 return ____machine__findnew_thread(machine, pid, tid, true);
419}
420
Adrian Hunter314add62013-08-27 11:23:03 +0300421struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
422 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300423{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300424 struct thread *th;
425
426 pthread_rwlock_wrlock(&machine->threads_lock);
427 th = thread__get(__machine__findnew_thread(machine, pid, tid));
428 pthread_rwlock_unlock(&machine->threads_lock);
429 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300430}
431
Jiri Olsad75e6092014-03-14 15:00:03 +0100432struct thread *machine__find_thread(struct machine *machine, pid_t pid,
433 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300434{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300435 struct thread *th;
436 pthread_rwlock_rdlock(&machine->threads_lock);
437 th = thread__get(____machine__findnew_thread(machine, pid, tid, false));
438 pthread_rwlock_unlock(&machine->threads_lock);
439 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300440}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300441
Adrian Huntercfe1c412014-07-31 09:00:45 +0300442struct comm *machine__thread_exec_comm(struct machine *machine,
443 struct thread *thread)
444{
445 if (machine->comm_exec)
446 return thread__exec_comm(thread);
447 else
448 return thread__comm(thread);
449}
450
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200451int machine__process_comm_event(struct machine *machine, union perf_event *event,
452 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300453{
Adrian Hunter314add62013-08-27 11:23:03 +0300454 struct thread *thread = machine__findnew_thread(machine,
455 event->comm.pid,
456 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300457 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300458 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300459
Adrian Huntercfe1c412014-07-31 09:00:45 +0300460 if (exec)
461 machine->comm_exec = true;
462
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300463 if (dump_trace)
464 perf_event__fprintf_comm(event, stdout);
465
Adrian Hunter65de51f2014-07-31 09:00:44 +0300466 if (thread == NULL ||
467 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300468 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300469 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300470 }
471
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300472 thread__put(thread);
473
474 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300475}
476
477int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200478 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300479{
480 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
481 event->lost.id, event->lost.lost);
482 return 0;
483}
484
Kan Liangc4937a92015-05-10 15:13:15 -0400485int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
486 union perf_event *event, struct perf_sample *sample)
487{
488 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
489 sample->id, event->lost_samples.lost);
490 return 0;
491}
492
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300493static struct dso *machine__findnew_module_dso(struct machine *machine,
494 struct kmod_path *m,
495 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100496{
497 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100498
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300499 dso = dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100500 if (!dso) {
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300501 dso = dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100502 if (dso == NULL)
503 return NULL;
504
505 if (machine__is_host(machine))
506 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
507 else
508 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
509
510 /* _KMODULE_COMP should be next to _KMODULE */
Jiri Olsaca333802015-02-17 17:29:57 +0100511 if (m->kmod && m->comp)
Jiri Olsada17ea32015-02-12 22:10:52 +0100512 dso->symtab_type++;
Jiri Olsaca333802015-02-17 17:29:57 +0100513
514 dso__set_short_name(dso, strdup(m->name), true);
515 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100516 }
517
518 return dso;
519}
520
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300521int machine__process_aux_event(struct machine *machine __maybe_unused,
522 union perf_event *event)
523{
524 if (dump_trace)
525 perf_event__fprintf_aux(event, stdout);
526 return 0;
527}
528
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300529int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
530 union perf_event *event)
531{
532 if (dump_trace)
533 perf_event__fprintf_itrace_start(event, stdout);
534 return 0;
535}
536
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300537struct map *machine__findnew_module_map(struct machine *machine, u64 start,
538 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300539{
Jiri Olsaca333802015-02-17 17:29:57 +0100540 struct map *map = NULL;
541 struct dso *dso;
542 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300543
Jiri Olsaca333802015-02-17 17:29:57 +0100544 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300545 return NULL;
546
Jiri Olsabc84f462015-02-17 17:31:18 +0100547 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
548 m.name);
549 if (map)
550 goto out;
551
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300552 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100553 if (dso == NULL)
554 goto out;
555
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300556 map = map__new2(start, dso, MAP__FUNCTION);
557 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100558 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300559
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300560 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100561
562out:
563 free(m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300564 return map;
565}
566
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300567size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300568{
569 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300570 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300571
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300572 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300573 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300574 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300575 }
576
577 return ret;
578}
579
Waiman Long8fa7d872014-09-29 16:07:28 -0400580size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300581 bool (skip)(struct dso *dso, int parm), int parm)
582{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300583 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300584}
585
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300586size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300587 bool (skip)(struct dso *dso, int parm), int parm)
588{
589 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300590 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300591
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300592 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300593 struct machine *pos = rb_entry(nd, struct machine, rb_node);
594 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
595 }
596 return ret;
597}
598
599size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
600{
601 int i;
602 size_t printed = 0;
603 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
604
605 if (kdso->has_build_id) {
606 char filename[PATH_MAX];
607 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
608 printed += fprintf(fp, "[0] %s\n", filename);
609 }
610
611 for (i = 0; i < vmlinux_path__nr_entries; ++i)
612 printed += fprintf(fp, "[%d] %s\n",
613 i + kdso->has_build_id, vmlinux_path[i]);
614
615 return printed;
616}
617
618size_t machine__fprintf(struct machine *machine, FILE *fp)
619{
620 size_t ret = 0;
621 struct rb_node *nd;
622
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300623 pthread_rwlock_rdlock(&machine->threads_lock);
624
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300625 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
626 struct thread *pos = rb_entry(nd, struct thread, rb_node);
627
628 ret += thread__fprintf(pos, fp);
629 }
630
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300631 pthread_rwlock_unlock(&machine->threads_lock);
632
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300633 return ret;
634}
635
636static struct dso *machine__get_kernel(struct machine *machine)
637{
638 const char *vmlinux_name = NULL;
639 struct dso *kernel;
640
641 if (machine__is_host(machine)) {
642 vmlinux_name = symbol_conf.vmlinux_name;
643 if (!vmlinux_name)
644 vmlinux_name = "[kernel.kallsyms]";
645
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300646 kernel = machine__findnew_kernel(machine, vmlinux_name,
647 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300648 } else {
649 char bf[PATH_MAX];
650
651 if (machine__is_default_guest(machine))
652 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
653 if (!vmlinux_name)
654 vmlinux_name = machine__mmap_name(machine, bf,
655 sizeof(bf));
656
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300657 kernel = machine__findnew_kernel(machine, vmlinux_name,
658 "[guest.kernel]",
659 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300660 }
661
662 if (kernel != NULL && (!kernel->has_build_id))
663 dso__read_running_kernel_build_id(kernel, machine);
664
665 return kernel;
666}
667
668struct process_args {
669 u64 start;
670};
671
Adrian Hunter15a0a872014-01-29 16:14:38 +0200672static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
673 size_t bufsz)
674{
675 if (machine__is_default_guest(machine))
676 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
677 else
678 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
679}
680
Simon Quea93f0e52014-06-16 11:32:09 -0700681const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
682
683/* Figure out the start address of kernel map from /proc/kallsyms.
684 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
685 * symbol_name if it's not that important.
686 */
Adrian Hunter4b993752014-08-15 22:08:38 +0300687static u64 machine__get_running_kernel_start(struct machine *machine,
688 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300689{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200690 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700691 int i;
692 const char *name;
693 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300694
Adrian Hunter15a0a872014-01-29 16:14:38 +0200695 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300696
697 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
698 return 0;
699
Simon Quea93f0e52014-06-16 11:32:09 -0700700 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
701 addr = kallsyms__get_function_start(filename, name);
702 if (addr)
703 break;
704 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300705
Simon Quea93f0e52014-06-16 11:32:09 -0700706 if (symbol_name)
707 *symbol_name = name;
708
709 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300710}
711
712int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
713{
714 enum map_type type;
Adrian Hunter4b993752014-08-15 22:08:38 +0300715 u64 start = machine__get_running_kernel_start(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300716
717 for (type = 0; type < MAP__NR_TYPES; ++type) {
718 struct kmap *kmap;
719
720 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
721 if (machine->vmlinux_maps[type] == NULL)
722 return -1;
723
724 machine->vmlinux_maps[type]->map_ip =
725 machine->vmlinux_maps[type]->unmap_ip =
726 identity__map_ip;
727 kmap = map__kmap(machine->vmlinux_maps[type]);
Wang Nanba927322015-04-07 08:22:45 +0000728 if (!kmap)
729 return -1;
730
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300731 kmap->kmaps = &machine->kmaps;
732 map_groups__insert(&machine->kmaps,
733 machine->vmlinux_maps[type]);
734 }
735
736 return 0;
737}
738
739void machine__destroy_kernel_maps(struct machine *machine)
740{
741 enum map_type type;
742
743 for (type = 0; type < MAP__NR_TYPES; ++type) {
744 struct kmap *kmap;
745
746 if (machine->vmlinux_maps[type] == NULL)
747 continue;
748
749 kmap = map__kmap(machine->vmlinux_maps[type]);
750 map_groups__remove(&machine->kmaps,
751 machine->vmlinux_maps[type]);
Wang Nanba927322015-04-07 08:22:45 +0000752 if (kmap && kmap->ref_reloc_sym) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300753 /*
754 * ref_reloc_sym is shared among all maps, so free just
755 * on one of them.
756 */
757 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300758 zfree((char **)&kmap->ref_reloc_sym->name);
759 zfree(&kmap->ref_reloc_sym);
760 } else
761 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300762 }
763
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300764 machine->vmlinux_maps[type] = NULL;
765 }
766}
767
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300768int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300769{
770 int ret = 0;
771 struct dirent **namelist = NULL;
772 int i, items = 0;
773 char path[PATH_MAX];
774 pid_t pid;
775 char *endp;
776
777 if (symbol_conf.default_guest_vmlinux_name ||
778 symbol_conf.default_guest_modules ||
779 symbol_conf.default_guest_kallsyms) {
780 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
781 }
782
783 if (symbol_conf.guestmount) {
784 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
785 if (items <= 0)
786 return -ENOENT;
787 for (i = 0; i < items; i++) {
788 if (!isdigit(namelist[i]->d_name[0])) {
789 /* Filter out . and .. */
790 continue;
791 }
792 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
793 if ((*endp != '\0') ||
794 (endp == namelist[i]->d_name) ||
795 (errno == ERANGE)) {
796 pr_debug("invalid directory (%s). Skipping.\n",
797 namelist[i]->d_name);
798 continue;
799 }
800 sprintf(path, "%s/%s/proc/kallsyms",
801 symbol_conf.guestmount,
802 namelist[i]->d_name);
803 ret = access(path, R_OK);
804 if (ret) {
805 pr_debug("Can't access file %s\n", path);
806 goto failure;
807 }
808 machines__create_kernel_maps(machines, pid);
809 }
810failure:
811 free(namelist);
812 }
813
814 return ret;
815}
816
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300817void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300818{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300819 struct rb_node *next = rb_first(&machines->guests);
820
821 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300822
823 while (next) {
824 struct machine *pos = rb_entry(next, struct machine, rb_node);
825
826 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300827 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300828 machine__delete(pos);
829 }
830}
831
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300832int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300833{
834 struct machine *machine = machines__findnew(machines, pid);
835
836 if (machine == NULL)
837 return -1;
838
839 return machine__create_kernel_maps(machine);
840}
841
842int machine__load_kallsyms(struct machine *machine, const char *filename,
843 enum map_type type, symbol_filter_t filter)
844{
845 struct map *map = machine->vmlinux_maps[type];
846 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
847
848 if (ret > 0) {
849 dso__set_loaded(map->dso, type);
850 /*
851 * Since /proc/kallsyms will have multiple sessions for the
852 * kernel, with modules between them, fixup the end of all
853 * sections.
854 */
855 __map_groups__fixup_end(&machine->kmaps, type);
856 }
857
858 return ret;
859}
860
861int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
862 symbol_filter_t filter)
863{
864 struct map *map = machine->vmlinux_maps[type];
865 int ret = dso__load_vmlinux_path(map->dso, map, filter);
866
Adrian Hunter39b12f782013-08-07 14:38:47 +0300867 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300868 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300869
870 return ret;
871}
872
873static void map_groups__fixup_end(struct map_groups *mg)
874{
875 int i;
876 for (i = 0; i < MAP__NR_TYPES; ++i)
877 __map_groups__fixup_end(mg, i);
878}
879
880static char *get_kernel_version(const char *root_dir)
881{
882 char version[PATH_MAX];
883 FILE *file;
884 char *name, *tmp;
885 const char *prefix = "Linux version ";
886
887 sprintf(version, "%s/proc/version", root_dir);
888 file = fopen(version, "r");
889 if (!file)
890 return NULL;
891
892 version[0] = '\0';
893 tmp = fgets(version, sizeof(version), file);
894 fclose(file);
895
896 name = strstr(version, prefix);
897 if (!name)
898 return NULL;
899 name += strlen(prefix);
900 tmp = strchr(name, ' ');
901 if (tmp)
902 *tmp = '\0';
903
904 return strdup(name);
905}
906
Jiri Olsabb58a8a2015-02-12 22:20:01 +0100907static bool is_kmod_dso(struct dso *dso)
908{
909 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
910 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
911}
912
913static int map_groups__set_module_path(struct map_groups *mg, const char *path,
914 struct kmod_path *m)
915{
916 struct map *map;
917 char *long_name;
918
919 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
920 if (map == NULL)
921 return 0;
922
923 long_name = strdup(path);
924 if (long_name == NULL)
925 return -ENOMEM;
926
927 dso__set_long_name(map->dso, long_name, true);
928 dso__kernel_module_get_build_id(map->dso, "");
929
930 /*
931 * Full name could reveal us kmod compression, so
932 * we need to update the symtab_type if needed.
933 */
934 if (m->comp && is_kmod_dso(map->dso))
935 map->dso->symtab_type++;
936
937 return 0;
938}
939
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300940static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -0400941 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300942{
943 struct dirent *dent;
944 DIR *dir = opendir(dir_name);
945 int ret = 0;
946
947 if (!dir) {
948 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
949 return -1;
950 }
951
952 while ((dent = readdir(dir)) != NULL) {
953 char path[PATH_MAX];
954 struct stat st;
955
956 /*sshfs might return bad dent->d_type, so we have to stat*/
957 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
958 if (stat(path, &st))
959 continue;
960
961 if (S_ISDIR(st.st_mode)) {
962 if (!strcmp(dent->d_name, ".") ||
963 !strcmp(dent->d_name, ".."))
964 continue;
965
Richard Yao61d42902014-04-26 13:17:55 -0400966 /* Do not follow top-level source and build symlinks */
967 if (depth == 0) {
968 if (!strcmp(dent->d_name, "source") ||
969 !strcmp(dent->d_name, "build"))
970 continue;
971 }
972
973 ret = map_groups__set_modules_path_dir(mg, path,
974 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300975 if (ret < 0)
976 goto out;
977 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +0100978 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300979
Jiri Olsabb58a8a2015-02-12 22:20:01 +0100980 ret = kmod_path__parse_name(&m, dent->d_name);
981 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300982 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +0100983
984 if (m.kmod)
985 ret = map_groups__set_module_path(mg, path, &m);
986
987 free(m.name);
988
989 if (ret)
990 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300991 }
992 }
993
994out:
995 closedir(dir);
996 return ret;
997}
998
999static int machine__set_modules_path(struct machine *machine)
1000{
1001 char *version;
1002 char modules_path[PATH_MAX];
1003
1004 version = get_kernel_version(machine->root_dir);
1005 if (!version)
1006 return -1;
1007
Richard Yao61d42902014-04-26 13:17:55 -04001008 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001009 machine->root_dir, version);
1010 free(version);
1011
Richard Yao61d42902014-04-26 13:17:55 -04001012 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001013}
1014
Adrian Hunter316d70d2013-10-08 11:45:48 +03001015static int machine__create_module(void *arg, const char *name, u64 start)
1016{
1017 struct machine *machine = arg;
1018 struct map *map;
1019
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001020 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001021 if (map == NULL)
1022 return -1;
1023
1024 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1025
1026 return 0;
1027}
1028
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001029static int machine__create_modules(struct machine *machine)
1030{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001031 const char *modules;
1032 char path[PATH_MAX];
1033
Adrian Hunterf4be9042013-09-22 13:22:09 +03001034 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001035 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001036 } else {
1037 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001038 modules = path;
1039 }
1040
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001041 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001042 return -1;
1043
Adrian Hunter316d70d2013-10-08 11:45:48 +03001044 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001045 return -1;
1046
Adrian Hunter316d70d2013-10-08 11:45:48 +03001047 if (!machine__set_modules_path(machine))
1048 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001049
Adrian Hunter316d70d2013-10-08 11:45:48 +03001050 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001051
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001052 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001053}
1054
1055int machine__create_kernel_maps(struct machine *machine)
1056{
1057 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001058 const char *name;
Adrian Hunter4b993752014-08-15 22:08:38 +03001059 u64 addr = machine__get_running_kernel_start(machine, &name);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001060 if (!addr)
1061 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001062
1063 if (kernel == NULL ||
1064 __machine__create_kernel_maps(machine, kernel) < 0)
1065 return -1;
1066
1067 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1068 if (machine__is_host(machine))
1069 pr_debug("Problems creating module maps, "
1070 "continuing anyway...\n");
1071 else
1072 pr_debug("Problems creating module maps for guest %d, "
1073 "continuing anyway...\n", machine->pid);
1074 }
1075
1076 /*
1077 * Now that we have all the maps created, just set the ->end of them:
1078 */
1079 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001080
1081 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
1082 addr)) {
1083 machine__destroy_kernel_maps(machine);
1084 return -1;
1085 }
1086
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001087 return 0;
1088}
1089
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001090static void machine__set_kernel_mmap_len(struct machine *machine,
1091 union perf_event *event)
1092{
Namhyung Kim4552cf0f2012-11-07 16:27:10 +09001093 int i;
1094
1095 for (i = 0; i < MAP__NR_TYPES; i++) {
1096 machine->vmlinux_maps[i]->start = event->mmap.start;
1097 machine->vmlinux_maps[i]->end = (event->mmap.start +
1098 event->mmap.len);
1099 /*
1100 * Be a bit paranoid here, some perf.data file came with
1101 * a zero sized synthesized MMAP event for the kernel.
1102 */
1103 if (machine->vmlinux_maps[i]->end == 0)
1104 machine->vmlinux_maps[i]->end = ~0ULL;
1105 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001106}
1107
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001108static bool machine__uses_kcore(struct machine *machine)
1109{
1110 struct dso *dso;
1111
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001112 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001113 if (dso__is_kcore(dso))
1114 return true;
1115 }
1116
1117 return false;
1118}
1119
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001120static int machine__process_kernel_mmap_event(struct machine *machine,
1121 union perf_event *event)
1122{
1123 struct map *map;
1124 char kmmap_prefix[PATH_MAX];
1125 enum dso_kernel_type kernel_type;
1126 bool is_kernel_mmap;
1127
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001128 /* If we have maps from kcore then we do not need or want any others */
1129 if (machine__uses_kcore(machine))
1130 return 0;
1131
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001132 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1133 if (machine__is_host(machine))
1134 kernel_type = DSO_TYPE_KERNEL;
1135 else
1136 kernel_type = DSO_TYPE_GUEST_KERNEL;
1137
1138 is_kernel_mmap = memcmp(event->mmap.filename,
1139 kmmap_prefix,
1140 strlen(kmmap_prefix) - 1) == 0;
1141 if (event->mmap.filename[0] == '/' ||
1142 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001143 map = machine__findnew_module_map(machine, event->mmap.start,
1144 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001145 if (map == NULL)
1146 goto out_problem;
1147
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001148 map->end = map->start + event->mmap.len;
1149 } else if (is_kernel_mmap) {
1150 const char *symbol_name = (event->mmap.filename +
1151 strlen(kmmap_prefix));
1152 /*
1153 * Should be there already, from the build-id table in
1154 * the header.
1155 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001156 struct dso *kernel = NULL;
1157 struct dso *dso;
1158
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001159 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001160
1161 /*
1162 * The cpumode passed to is_kernel_module is not the
1163 * cpumode of *this* event. If we insist on passing
1164 * correct cpumode to is_kernel_module, we should
1165 * record the cpumode when we adding this dso to the
1166 * linked list.
1167 *
1168 * However we don't really need passing correct
1169 * cpumode. We know the correct cpumode must be kernel
1170 * mode (if not, we should not link it onto kernel_dsos
1171 * list).
1172 *
1173 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1174 * is_kernel_module() treats it as a kernel cpumode.
1175 */
1176
1177 if (!dso->kernel ||
1178 is_kernel_module(dso->long_name,
1179 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001180 continue;
1181
Wang Nan1f121b02015-06-03 08:52:21 +00001182
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001183 kernel = dso;
1184 break;
1185 }
1186
1187 if (kernel == NULL)
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001188 kernel = machine__findnew_dso(machine, kmmap_prefix);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001189 if (kernel == NULL)
1190 goto out_problem;
1191
1192 kernel->kernel = kernel_type;
1193 if (__machine__create_kernel_maps(machine, kernel) < 0)
1194 goto out_problem;
1195
Namhyung Kim330dfa22014-11-18 13:30:28 +09001196 if (strstr(kernel->long_name, "vmlinux"))
1197 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001198
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001199 machine__set_kernel_mmap_len(machine, event);
1200
1201 /*
1202 * Avoid using a zero address (kptr_restrict) for the ref reloc
1203 * symbol. Effectively having zero here means that at record
1204 * time /proc/sys/kernel/kptr_restrict was non zero.
1205 */
1206 if (event->mmap.pgoff != 0) {
1207 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1208 symbol_name,
1209 event->mmap.pgoff);
1210 }
1211
1212 if (machine__is_default_guest(machine)) {
1213 /*
1214 * preload dso of guest kernel and modules
1215 */
1216 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
1217 NULL);
1218 }
1219 }
1220 return 0;
1221out_problem:
1222 return -1;
1223}
1224
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001225int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001226 union perf_event *event,
1227 struct perf_sample *sample __maybe_unused)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001228{
1229 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1230 struct thread *thread;
1231 struct map *map;
1232 enum map_type type;
1233 int ret = 0;
1234
1235 if (dump_trace)
1236 perf_event__fprintf_mmap2(event, stdout);
1237
1238 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1239 cpumode == PERF_RECORD_MISC_KERNEL) {
1240 ret = machine__process_kernel_mmap_event(machine, event);
1241 if (ret < 0)
1242 goto out_problem;
1243 return 0;
1244 }
1245
1246 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001247 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001248 if (thread == NULL)
1249 goto out_problem;
1250
1251 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1252 type = MAP__VARIABLE;
1253 else
1254 type = MAP__FUNCTION;
1255
Adrian Hunter2a030682014-07-22 16:17:53 +03001256 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001257 event->mmap2.len, event->mmap2.pgoff,
1258 event->mmap2.pid, event->mmap2.maj,
1259 event->mmap2.min, event->mmap2.ino,
1260 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001261 event->mmap2.prot,
1262 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001263 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001264
1265 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001266 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001267
1268 thread__insert_map(thread, map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001269 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001270 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001271 return 0;
1272
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001273out_problem_map:
1274 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001275out_problem:
1276 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1277 return 0;
1278}
1279
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001280int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1281 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001282{
1283 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1284 struct thread *thread;
1285 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001286 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001287 int ret = 0;
1288
1289 if (dump_trace)
1290 perf_event__fprintf_mmap(event, stdout);
1291
1292 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1293 cpumode == PERF_RECORD_MISC_KERNEL) {
1294 ret = machine__process_kernel_mmap_event(machine, event);
1295 if (ret < 0)
1296 goto out_problem;
1297 return 0;
1298 }
1299
Adrian Hunter314add62013-08-27 11:23:03 +03001300 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001301 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001302 if (thread == NULL)
1303 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001304
1305 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1306 type = MAP__VARIABLE;
1307 else
1308 type = MAP__FUNCTION;
1309
Adrian Hunter2a030682014-07-22 16:17:53 +03001310 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001311 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001312 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001313 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001314 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001315
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001316 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001317 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001318
1319 thread__insert_map(thread, map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001320 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001321 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001322 return 0;
1323
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001324out_problem_map:
1325 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001326out_problem:
1327 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1328 return 0;
1329}
1330
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001331static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001332{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001333 if (machine->last_match == th)
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -03001334 machine->last_match = NULL;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001335
Arnaldo Carvalho de Melo59a51c12015-05-15 15:32:55 -03001336 BUG_ON(atomic_read(&th->refcnt) == 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001337 if (lock)
1338 pthread_rwlock_wrlock(&machine->threads_lock);
Arnaldo Carvalho de Melo0170b142015-05-25 15:23:05 -03001339 rb_erase_init(&th->rb_node, &machine->threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001340 RB_CLEAR_NODE(&th->rb_node);
David Ahern236a3bb2013-08-14 08:49:27 -06001341 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001342 * Move it first to the dead_threads list, then drop the reference,
1343 * if this is the last reference, then the thread__delete destructor
1344 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001345 */
1346 list_add_tail(&th->node, &machine->dead_threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001347 if (lock)
1348 pthread_rwlock_unlock(&machine->threads_lock);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001349 thread__put(th);
David Ahern236a3bb2013-08-14 08:49:27 -06001350}
1351
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001352void machine__remove_thread(struct machine *machine, struct thread *th)
1353{
1354 return __machine__remove_thread(machine, th, true);
1355}
1356
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001357int machine__process_fork_event(struct machine *machine, union perf_event *event,
1358 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001359{
Jiri Olsad75e6092014-03-14 15:00:03 +01001360 struct thread *thread = machine__find_thread(machine,
1361 event->fork.pid,
1362 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001363 struct thread *parent = machine__findnew_thread(machine,
1364 event->fork.ppid,
1365 event->fork.ptid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001366 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001367
David Ahern236a3bb2013-08-14 08:49:27 -06001368 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001369 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001370 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001371 thread__put(thread);
1372 }
David Ahern236a3bb2013-08-14 08:49:27 -06001373
Adrian Hunter314add62013-08-27 11:23:03 +03001374 thread = machine__findnew_thread(machine, event->fork.pid,
1375 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001376 if (dump_trace)
1377 perf_event__fprintf_task(event, stdout);
1378
1379 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001380 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001381 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001382 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001383 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001384 thread__put(thread);
1385 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001386
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001387 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001388}
1389
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001390int machine__process_exit_event(struct machine *machine, union perf_event *event,
1391 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001392{
Jiri Olsad75e6092014-03-14 15:00:03 +01001393 struct thread *thread = machine__find_thread(machine,
1394 event->fork.pid,
1395 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001396
1397 if (dump_trace)
1398 perf_event__fprintf_task(event, stdout);
1399
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001400 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001401 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001402 thread__put(thread);
1403 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001404
1405 return 0;
1406}
1407
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001408int machine__process_event(struct machine *machine, union perf_event *event,
1409 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001410{
1411 int ret;
1412
1413 switch (event->header.type) {
1414 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001415 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001416 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001417 ret = machine__process_mmap_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001418 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001419 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001420 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001421 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001422 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001423 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001424 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001425 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001426 case PERF_RECORD_AUX:
1427 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001428 case PERF_RECORD_ITRACE_START:
1429 ret = machine__process_itrace_start_event(machine, event);
Kan Liangc4937a92015-05-10 15:13:15 -04001430 case PERF_RECORD_LOST_SAMPLES:
1431 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001432 break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001433 default:
1434 ret = -1;
1435 break;
1436 }
1437
1438 return ret;
1439}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001440
Greg Priceb21484f2012-12-06 21:48:05 -08001441static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001442{
Greg Priceb21484f2012-12-06 21:48:05 -08001443 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001444 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001445 return 0;
1446}
1447
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001448static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001449 struct addr_map_symbol *ams,
1450 u64 ip)
1451{
1452 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001453
1454 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001455 /*
1456 * We cannot use the header.misc hint to determine whether a
1457 * branch stack address is user, kernel, guest, hypervisor.
1458 * Branches may straddle the kernel/user/hypervisor boundaries.
1459 * Thus, we have to try consecutively until we find a match
1460 * or else, the symbol is unknown
1461 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001462 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001463
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001464 ams->addr = ip;
1465 ams->al_addr = al.addr;
1466 ams->sym = al.sym;
1467 ams->map = al.map;
1468}
1469
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001470static void ip__resolve_data(struct thread *thread,
Stephane Eranian98a3b322013-01-24 16:10:35 +01001471 u8 m, struct addr_map_symbol *ams, u64 addr)
1472{
1473 struct addr_location al;
1474
1475 memset(&al, 0, sizeof(al));
1476
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001477 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001478 if (al.map == NULL) {
1479 /*
1480 * some shared data regions have execute bit set which puts
1481 * their mapping in the MAP__FUNCTION type array.
1482 * Check there as a fallback option before dropping the sample.
1483 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001484 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001485 }
1486
Stephane Eranian98a3b322013-01-24 16:10:35 +01001487 ams->addr = addr;
1488 ams->al_addr = al.addr;
1489 ams->sym = al.sym;
1490 ams->map = al.map;
1491}
1492
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001493struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1494 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001495{
1496 struct mem_info *mi = zalloc(sizeof(*mi));
1497
1498 if (!mi)
1499 return NULL;
1500
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001501 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1502 ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001503 mi->data_src.val = sample->data_src;
1504
1505 return mi;
1506}
1507
Andi Kleen37592b82014-11-12 18:05:19 -08001508static int add_callchain_ip(struct thread *thread,
1509 struct symbol **parent,
1510 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001511 u8 *cpumode,
Andi Kleen37592b82014-11-12 18:05:19 -08001512 u64 ip)
1513{
1514 struct addr_location al;
1515
1516 al.filtered = 0;
1517 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001518 if (!cpumode) {
Andi Kleen8b7bad52014-11-12 18:05:20 -08001519 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1520 ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001521 } else {
Kan Liang2e777842014-12-02 10:06:53 -05001522 if (ip >= PERF_CONTEXT_MAX) {
1523 switch (ip) {
1524 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001525 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05001526 break;
1527 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001528 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05001529 break;
1530 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001531 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05001532 break;
1533 default:
1534 pr_debug("invalid callchain context: "
1535 "%"PRId64"\n", (s64) ip);
1536 /*
1537 * It seems the callchain is corrupted.
1538 * Discard all.
1539 */
1540 callchain_cursor_reset(&callchain_cursor);
1541 return 1;
1542 }
1543 return 0;
1544 }
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001545 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1546 ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05001547 }
1548
Andi Kleen37592b82014-11-12 18:05:19 -08001549 if (al.sym != NULL) {
1550 if (sort__has_parent && !*parent &&
1551 symbol__match_regex(al.sym, &parent_regex))
1552 *parent = al.sym;
1553 else if (have_ignore_callees && root_al &&
1554 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1555 /* Treat this symbol as the root,
1556 forgetting its callees. */
1557 *root_al = al;
1558 callchain_cursor_reset(&callchain_cursor);
1559 }
1560 }
1561
Andi Kleen55501712014-11-12 18:05:21 -08001562 return callchain_cursor_append(&callchain_cursor, al.addr, al.map, al.sym);
Andi Kleen37592b82014-11-12 18:05:19 -08001563}
1564
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001565struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1566 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001567{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001568 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001569 const struct branch_stack *bs = sample->branch_stack;
1570 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001571
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001572 if (!bi)
1573 return NULL;
1574
1575 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001576 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1577 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001578 bi[i].flags = bs->entries[i].flags;
1579 }
1580 return bi;
1581}
1582
Andi Kleen8b7bad52014-11-12 18:05:20 -08001583#define CHASHSZ 127
1584#define CHASHBITS 7
1585#define NO_ENTRY 0xff
1586
1587#define PERF_MAX_BRANCH_DEPTH 127
1588
1589/* Remove loops. */
1590static int remove_loops(struct branch_entry *l, int nr)
1591{
1592 int i, j, off;
1593 unsigned char chash[CHASHSZ];
1594
1595 memset(chash, NO_ENTRY, sizeof(chash));
1596
1597 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1598
1599 for (i = 0; i < nr; i++) {
1600 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1601
1602 /* no collision handling for now */
1603 if (chash[h] == NO_ENTRY) {
1604 chash[h] = i;
1605 } else if (l[chash[h]].from == l[i].from) {
1606 bool is_loop = true;
1607 /* check if it is a real loop */
1608 off = 0;
1609 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1610 if (l[j].from != l[i + off].from) {
1611 is_loop = false;
1612 break;
1613 }
1614 if (is_loop) {
1615 memmove(l + i, l + i + off,
1616 (nr - (i + off)) * sizeof(*l));
1617 nr -= off;
1618 }
1619 }
1620 }
1621 return nr;
1622}
1623
Kan Liang384b6052015-01-05 13:23:05 -05001624/*
1625 * Recolve LBR callstack chain sample
1626 * Return:
1627 * 1 on success get LBR callchain information
1628 * 0 no available LBR callchain information, should try fp
1629 * negative error code on other errors.
1630 */
1631static int resolve_lbr_callchain_sample(struct thread *thread,
1632 struct perf_sample *sample,
1633 struct symbol **parent,
1634 struct addr_location *root_al,
1635 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001636{
Kan Liang384b6052015-01-05 13:23:05 -05001637 struct ip_callchain *chain = sample->callchain;
1638 int chain_nr = min(max_stack, (int)chain->nr);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001639 u8 cpumode = PERF_RECORD_MISC_USER;
Kan Liang384b6052015-01-05 13:23:05 -05001640 int i, j, err;
1641 u64 ip;
1642
1643 for (i = 0; i < chain_nr; i++) {
1644 if (chain->ips[i] == PERF_CONTEXT_USER)
1645 break;
1646 }
1647
1648 /* LBR only affects the user callchain */
1649 if (i != chain_nr) {
1650 struct branch_stack *lbr_stack = sample->branch_stack;
1651 int lbr_nr = lbr_stack->nr;
1652 /*
1653 * LBR callstack can only get user call chain.
1654 * The mix_chain_nr is kernel call chain
1655 * number plus LBR user call chain number.
1656 * i is kernel call chain number,
1657 * 1 is PERF_CONTEXT_USER,
1658 * lbr_nr + 1 is the user call chain number.
1659 * For details, please refer to the comments
1660 * in callchain__printf
1661 */
1662 int mix_chain_nr = i + 1 + lbr_nr + 1;
1663
1664 if (mix_chain_nr > PERF_MAX_STACK_DEPTH + PERF_MAX_BRANCH_DEPTH) {
1665 pr_warning("corrupted callchain. skipping...\n");
1666 return 0;
1667 }
1668
1669 for (j = 0; j < mix_chain_nr; j++) {
1670 if (callchain_param.order == ORDER_CALLEE) {
1671 if (j < i + 1)
1672 ip = chain->ips[j];
1673 else if (j > i + 1)
1674 ip = lbr_stack->entries[j - i - 2].from;
1675 else
1676 ip = lbr_stack->entries[0].to;
1677 } else {
1678 if (j < lbr_nr)
1679 ip = lbr_stack->entries[lbr_nr - j - 1].from;
1680 else if (j > lbr_nr)
1681 ip = chain->ips[i + 1 - (j - lbr_nr)];
1682 else
1683 ip = lbr_stack->entries[0].to;
1684 }
1685
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001686 err = add_callchain_ip(thread, parent, root_al, &cpumode, ip);
Kan Liang384b6052015-01-05 13:23:05 -05001687 if (err)
1688 return (err < 0) ? err : 0;
1689 }
1690 return 1;
1691 }
1692
1693 return 0;
1694}
1695
1696static int thread__resolve_callchain_sample(struct thread *thread,
1697 struct perf_evsel *evsel,
1698 struct perf_sample *sample,
1699 struct symbol **parent,
1700 struct addr_location *root_al,
1701 int max_stack)
1702{
1703 struct branch_stack *branch = sample->branch_stack;
1704 struct ip_callchain *chain = sample->callchain;
Waiman Long91e95612013-10-18 10:38:48 -04001705 int chain_nr = min(max_stack, (int)chain->nr);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001706 u8 cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05001707 int i, j, err;
Andi Kleen8b7bad52014-11-12 18:05:20 -08001708 int skip_idx = -1;
1709 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001710
Kan Liang384b6052015-01-05 13:23:05 -05001711 callchain_cursor_reset(&callchain_cursor);
1712
1713 if (has_branch_callstack(evsel)) {
1714 err = resolve_lbr_callchain_sample(thread, sample, parent,
1715 root_al, max_stack);
1716 if (err)
1717 return (err < 0) ? err : 0;
1718 }
1719
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001720 /*
1721 * Based on DWARF debug information, some architectures skip
1722 * a callchain entry saved by the kernel.
1723 */
Andi Kleen8b7bad52014-11-12 18:05:20 -08001724 if (chain->nr < PERF_MAX_STACK_DEPTH)
1725 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001726
Andi Kleen8b7bad52014-11-12 18:05:20 -08001727 /*
1728 * Add branches to call stack for easier browsing. This gives
1729 * more context for a sample than just the callers.
1730 *
1731 * This uses individual histograms of paths compared to the
1732 * aggregated histograms the normal LBR mode uses.
1733 *
1734 * Limitations for now:
1735 * - No extra filters
1736 * - No annotations (should annotate somehow)
1737 */
1738
1739 if (branch && callchain_param.branch_callstack) {
1740 int nr = min(max_stack, (int)branch->nr);
1741 struct branch_entry be[nr];
1742
1743 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
1744 pr_warning("corrupted branch chain. skipping...\n");
1745 goto check_calls;
1746 }
1747
1748 for (i = 0; i < nr; i++) {
1749 if (callchain_param.order == ORDER_CALLEE) {
1750 be[i] = branch->entries[i];
1751 /*
1752 * Check for overlap into the callchain.
1753 * The return address is one off compared to
1754 * the branch entry. To adjust for this
1755 * assume the calling instruction is not longer
1756 * than 8 bytes.
1757 */
1758 if (i == skip_idx ||
1759 chain->ips[first_call] >= PERF_CONTEXT_MAX)
1760 first_call++;
1761 else if (be[i].from < chain->ips[first_call] &&
1762 be[i].from >= chain->ips[first_call] - 8)
1763 first_call++;
1764 } else
1765 be[i] = branch->entries[branch->nr - i - 1];
1766 }
1767
1768 nr = remove_loops(be, nr);
1769
1770 for (i = 0; i < nr; i++) {
1771 err = add_callchain_ip(thread, parent, root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001772 NULL, be[i].to);
Andi Kleen8b7bad52014-11-12 18:05:20 -08001773 if (!err)
1774 err = add_callchain_ip(thread, parent, root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001775 NULL, be[i].from);
Andi Kleen8b7bad52014-11-12 18:05:20 -08001776 if (err == -EINVAL)
1777 break;
1778 if (err)
1779 return err;
1780 }
1781 chain_nr -= nr;
1782 }
1783
1784check_calls:
1785 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1786 pr_warning("corrupted callchain. skipping...\n");
1787 return 0;
1788 }
1789
1790 for (i = first_call; i < chain_nr; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001791 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001792
1793 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001794 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001795 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001796 j = chain->nr - i - 1;
1797
1798#ifdef HAVE_SKIP_CALLCHAIN_IDX
1799 if (j == skip_idx)
1800 continue;
1801#endif
1802 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001803
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001804 err = add_callchain_ip(thread, parent, root_al, &cpumode, ip);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001805
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001806 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05001807 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001808 }
1809
1810 return 0;
1811}
1812
1813static int unwind_entry(struct unwind_entry *entry, void *arg)
1814{
1815 struct callchain_cursor *cursor = arg;
1816 return callchain_cursor_append(cursor, entry->ip,
1817 entry->map, entry->sym);
1818}
1819
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -03001820int thread__resolve_callchain(struct thread *thread,
1821 struct perf_evsel *evsel,
1822 struct perf_sample *sample,
1823 struct symbol **parent,
1824 struct addr_location *root_al,
1825 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001826{
Kan Liang384b6052015-01-05 13:23:05 -05001827 int ret = thread__resolve_callchain_sample(thread, evsel,
1828 sample, parent,
1829 root_al, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001830 if (ret)
1831 return ret;
1832
1833 /* Can we do dwarf post unwind? */
1834 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1835 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1836 return 0;
1837
1838 /* Bail out if nothing was captured. */
1839 if ((!sample->user_regs.regs) ||
1840 (!sample->user_stack.size))
1841 return 0;
1842
Arnaldo Carvalho de Melodd8c17a2014-10-23 16:42:19 -03001843 return unwind__get_entries(unwind_entry, &callchain_cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01001844 thread, sample, max_stack);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001845
1846}
David Ahern35feee12013-09-28 13:12:58 -06001847
1848int machine__for_each_thread(struct machine *machine,
1849 int (*fn)(struct thread *thread, void *p),
1850 void *priv)
1851{
1852 struct rb_node *nd;
1853 struct thread *thread;
1854 int rc = 0;
1855
1856 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1857 thread = rb_entry(nd, struct thread, rb_node);
1858 rc = fn(thread, priv);
1859 if (rc != 0)
1860 return rc;
1861 }
1862
1863 list_for_each_entry(thread, &machine->dead_threads, node) {
1864 rc = fn(thread, priv);
1865 if (rc != 0)
1866 return rc;
1867 }
1868 return rc;
1869}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001870
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001871int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001872 struct target *target, struct thread_map *threads,
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001873 perf_event__handler_t process, bool data_mmap)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001874{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001875 if (target__has_task(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001876 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001877 else if (target__has_cpu(target))
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03001878 return perf_event__synthesize_threads(tool, process, machine, data_mmap);
1879 /* command specified */
1880 return 0;
1881}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03001882
1883pid_t machine__get_current_tid(struct machine *machine, int cpu)
1884{
1885 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
1886 return -1;
1887
1888 return machine->current_tid[cpu];
1889}
1890
1891int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
1892 pid_t tid)
1893{
1894 struct thread *thread;
1895
1896 if (cpu < 0)
1897 return -EINVAL;
1898
1899 if (!machine->current_tid) {
1900 int i;
1901
1902 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
1903 if (!machine->current_tid)
1904 return -ENOMEM;
1905 for (i = 0; i < MAX_NR_CPUS; i++)
1906 machine->current_tid[i] = -1;
1907 }
1908
1909 if (cpu >= MAX_NR_CPUS) {
1910 pr_err("Requested CPU %d too large. ", cpu);
1911 pr_err("Consider raising MAX_NR_CPUS\n");
1912 return -EINVAL;
1913 }
1914
1915 machine->current_tid[cpu] = tid;
1916
1917 thread = machine__findnew_thread(machine, pid, tid);
1918 if (!thread)
1919 return -ENOMEM;
1920
1921 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001922 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03001923
1924 return 0;
1925}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03001926
1927int machine__get_kernel_start(struct machine *machine)
1928{
1929 struct map *map = machine__kernel_map(machine, MAP__FUNCTION);
1930 int err = 0;
1931
1932 /*
1933 * The only addresses above 2^63 are kernel addresses of a 64-bit
1934 * kernel. Note that addresses are unsigned so that on a 32-bit system
1935 * all addresses including kernel addresses are less than 2^32. In
1936 * that case (32-bit system), if the kernel mapping is unknown, all
1937 * addresses will be assumed to be in user space - see
1938 * machine__kernel_ip().
1939 */
1940 machine->kernel_start = 1ULL << 63;
1941 if (map) {
1942 err = map__load(map, machine->symbol_filter);
1943 if (map->start)
1944 machine->kernel_start = map->start;
1945 }
1946 return err;
1947}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001948
1949struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
1950{
1951 return __dsos__findnew(&machine->dsos, filename);
1952}