blob: 205d2701736179a40868ecb9d285a4a48d007b3b [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;
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -030023 pthread_rwlock_init(&dsos->lock, NULL);
Arnaldo Carvalho de Meloe167f992014-10-14 15:07:48 -030024}
25
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030026int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
27{
Wang Nan93b0ba32015-12-08 02:25:44 +000028 memset(machine, 0, sizeof(*machine));
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030029 map_groups__init(&machine->kmaps, machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030030 RB_CLEAR_NODE(&machine->rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -030031 dsos__init(&machine->dsos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030032
33 machine->threads = RB_ROOT;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030034 pthread_rwlock_init(&machine->threads_lock, NULL);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -030035 machine->nr_threads = 0;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030036 INIT_LIST_HEAD(&machine->dead_threads);
37 machine->last_match = NULL;
38
Adrian Hunterd027b642014-07-23 14:23:00 +030039 machine->vdso_info = NULL;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -030040 machine->env = NULL;
Adrian Hunterd027b642014-07-23 14:23:00 +030041
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030042 machine->pid = pid;
43
Adrian Hunter611a5ce2013-08-08 14:32:20 +030044 machine->symbol_filter = NULL;
Jiri Olsa14bd6d22014-01-07 13:47:19 +010045 machine->id_hdr_size = 0;
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -030046 machine->kptr_restrict_warned = false;
Adrian Huntercfe1c412014-07-31 09:00:45 +030047 machine->comm_exec = false;
Adrian Hunterfbe2af42014-08-15 22:08:39 +030048 machine->kernel_start = 0;
Adrian Hunter611a5ce2013-08-08 14:32:20 +030049
Masami Hiramatsucc1121a2015-12-09 11:11:33 +090050 memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps));
51
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030052 machine->root_dir = strdup(root_dir);
53 if (machine->root_dir == NULL)
54 return -ENOMEM;
55
56 if (pid != HOST_KERNEL_ID) {
Adrian Hunter1fcb8762014-07-14 13:02:25 +030057 struct thread *thread = machine__findnew_thread(machine, -1,
Adrian Hunter314add62013-08-27 11:23:03 +030058 pid);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030059 char comm[64];
60
61 if (thread == NULL)
62 return -ENOMEM;
63
64 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020065 thread__set_comm(thread, comm, 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030066 thread__put(thread);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030067 }
68
Adrian Hunterb9d266b2014-07-22 16:17:25 +030069 machine->current_tid = NULL;
70
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030071 return 0;
72}
73
David Ahern8fb598e2013-09-28 13:13:00 -060074struct machine *machine__new_host(void)
75{
76 struct machine *machine = malloc(sizeof(*machine));
77
78 if (machine != NULL) {
79 machine__init(machine, "", HOST_KERNEL_ID);
80
81 if (machine__create_kernel_maps(machine) < 0)
82 goto out_delete;
83 }
84
85 return machine;
86out_delete:
87 free(machine);
88 return NULL;
89}
90
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -030091static void dsos__purge(struct dsos *dsos)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030092{
93 struct dso *pos, *n;
94
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -030095 pthread_rwlock_wrlock(&dsos->lock);
96
Waiman Long8fa7d872014-09-29 16:07:28 -040097 list_for_each_entry_safe(pos, n, &dsos->head, node) {
Waiman Long4598a0a2014-09-30 13:36:15 -040098 RB_CLEAR_NODE(&pos->rb_node);
Adrian Huntere266a752015-11-13 11:48:30 +020099 pos->root = NULL;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300100 list_del_init(&pos->node);
101 dso__put(pos);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300102 }
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300103
104 pthread_rwlock_unlock(&dsos->lock);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300105}
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300106
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300107static void dsos__exit(struct dsos *dsos)
108{
109 dsos__purge(dsos);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300110 pthread_rwlock_destroy(&dsos->lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300111}
112
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300113void machine__delete_threads(struct machine *machine)
114{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300115 struct rb_node *nd;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300116
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300117 pthread_rwlock_wrlock(&machine->threads_lock);
118 nd = rb_first(&machine->threads);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300119 while (nd) {
120 struct thread *t = rb_entry(nd, struct thread, rb_node);
121
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300122 nd = rb_next(nd);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300123 __machine__remove_thread(machine, t, false);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300124 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300125 pthread_rwlock_unlock(&machine->threads_lock);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300126}
127
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300128void machine__exit(struct machine *machine)
129{
Masami Hiramatsuebe97292015-11-18 15:40:24 +0900130 machine__destroy_kernel_maps(machine);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300131 map_groups__exit(&machine->kmaps);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300132 dsos__exit(&machine->dsos);
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300133 machine__exit_vdso(machine);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300134 zfree(&machine->root_dir);
Adrian Hunterb9d266b2014-07-22 16:17:25 +0300135 zfree(&machine->current_tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300136 pthread_rwlock_destroy(&machine->threads_lock);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300137}
138
139void machine__delete(struct machine *machine)
140{
141 machine__exit(machine);
142 free(machine);
143}
144
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300145void machines__init(struct machines *machines)
146{
147 machine__init(&machines->host, "", HOST_KERNEL_ID);
148 machines->guests = RB_ROOT;
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300149 machines->symbol_filter = NULL;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300150}
151
152void machines__exit(struct machines *machines)
153{
154 machine__exit(&machines->host);
155 /* XXX exit guest */
156}
157
158struct machine *machines__add(struct machines *machines, pid_t pid,
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300159 const char *root_dir)
160{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300161 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300162 struct rb_node *parent = NULL;
163 struct machine *pos, *machine = malloc(sizeof(*machine));
164
165 if (machine == NULL)
166 return NULL;
167
168 if (machine__init(machine, root_dir, pid) != 0) {
169 free(machine);
170 return NULL;
171 }
172
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300173 machine->symbol_filter = machines->symbol_filter;
174
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300175 while (*p != NULL) {
176 parent = *p;
177 pos = rb_entry(parent, struct machine, rb_node);
178 if (pid < pos->pid)
179 p = &(*p)->rb_left;
180 else
181 p = &(*p)->rb_right;
182 }
183
184 rb_link_node(&machine->rb_node, parent, p);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300185 rb_insert_color(&machine->rb_node, &machines->guests);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300186
187 return machine;
188}
189
Adrian Hunter611a5ce2013-08-08 14:32:20 +0300190void machines__set_symbol_filter(struct machines *machines,
191 symbol_filter_t symbol_filter)
192{
193 struct rb_node *nd;
194
195 machines->symbol_filter = symbol_filter;
196 machines->host.symbol_filter = symbol_filter;
197
198 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
199 struct machine *machine = rb_entry(nd, struct machine, rb_node);
200
201 machine->symbol_filter = symbol_filter;
202 }
203}
204
Adrian Huntercfe1c412014-07-31 09:00:45 +0300205void machines__set_comm_exec(struct machines *machines, bool comm_exec)
206{
207 struct rb_node *nd;
208
209 machines->host.comm_exec = comm_exec;
210
211 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
212 struct machine *machine = rb_entry(nd, struct machine, rb_node);
213
214 machine->comm_exec = comm_exec;
215 }
216}
217
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300218struct machine *machines__find(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300219{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300220 struct rb_node **p = &machines->guests.rb_node;
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300221 struct rb_node *parent = NULL;
222 struct machine *machine;
223 struct machine *default_machine = NULL;
224
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300225 if (pid == HOST_KERNEL_ID)
226 return &machines->host;
227
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300228 while (*p != NULL) {
229 parent = *p;
230 machine = rb_entry(parent, struct machine, rb_node);
231 if (pid < machine->pid)
232 p = &(*p)->rb_left;
233 else if (pid > machine->pid)
234 p = &(*p)->rb_right;
235 else
236 return machine;
237 if (!machine->pid)
238 default_machine = machine;
239 }
240
241 return default_machine;
242}
243
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300244struct machine *machines__findnew(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300245{
246 char path[PATH_MAX];
247 const char *root_dir = "";
248 struct machine *machine = machines__find(machines, pid);
249
250 if (machine && (machine->pid == pid))
251 goto out;
252
253 if ((pid != HOST_KERNEL_ID) &&
254 (pid != DEFAULT_GUEST_KERNEL_ID) &&
255 (symbol_conf.guestmount)) {
256 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
257 if (access(path, R_OK)) {
258 static struct strlist *seen;
259
260 if (!seen)
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -0300261 seen = strlist__new(NULL, NULL);
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300262
263 if (!strlist__has_entry(seen, path)) {
264 pr_err("Can't access file %s\n", path);
265 strlist__add(seen, path);
266 }
267 machine = NULL;
268 goto out;
269 }
270 root_dir = path;
271 }
272
273 machine = machines__add(machines, pid, root_dir);
274out:
275 return machine;
276}
277
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300278void machines__process_guests(struct machines *machines,
279 machine__process_t process, void *data)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300280{
281 struct rb_node *nd;
282
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300283 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300284 struct machine *pos = rb_entry(nd, struct machine, rb_node);
285 process(pos, data);
286 }
287}
288
289char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
290{
291 if (machine__is_host(machine))
292 snprintf(bf, size, "[%s]", "kernel.kallsyms");
293 else if (machine__is_default_guest(machine))
294 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
295 else {
296 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
297 machine->pid);
298 }
299
300 return bf;
301}
302
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300303void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300304{
305 struct rb_node *node;
306 struct machine *machine;
307
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300308 machines->host.id_hdr_size = id_hdr_size;
309
310 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -0300311 machine = rb_entry(node, struct machine, rb_node);
312 machine->id_hdr_size = id_hdr_size;
313 }
314
315 return;
316}
317
Adrian Hunter29ce3612014-07-16 11:07:13 +0300318static void machine__update_thread_pid(struct machine *machine,
319 struct thread *th, pid_t pid)
320{
321 struct thread *leader;
322
323 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
324 return;
325
326 th->pid_ = pid;
327
328 if (th->pid_ == th->tid)
329 return;
330
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300331 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300332 if (!leader)
333 goto out_err;
334
335 if (!leader->mg)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300336 leader->mg = map_groups__new(machine);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300337
338 if (!leader->mg)
339 goto out_err;
340
341 if (th->mg == leader->mg)
342 return;
343
344 if (th->mg) {
345 /*
346 * Maps are created from MMAP events which provide the pid and
347 * tid. Consequently there never should be any maps on a thread
348 * with an unknown pid. Just print an error if there are.
349 */
350 if (!map_groups__empty(th->mg))
351 pr_err("Discarding thread maps for %d:%d\n",
352 th->pid_, th->tid);
Arnaldo Carvalho de Melo8e160b22015-05-19 20:07:14 -0300353 map_groups__put(th->mg);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300354 }
355
356 th->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300357out_put:
358 thread__put(leader);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300359 return;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300360out_err:
361 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300362 goto out_put;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300363}
364
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300365/*
366 * Caller must eventually drop thread->refcnt returned with a successfull
367 * lookup/new thread inserted.
368 */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300369static struct thread *____machine__findnew_thread(struct machine *machine,
370 pid_t pid, pid_t tid,
371 bool create)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300372{
373 struct rb_node **p = &machine->threads.rb_node;
374 struct rb_node *parent = NULL;
375 struct thread *th;
376
377 /*
Adrian Hunter38051232013-07-04 16:20:31 +0300378 * Front-end cache - TID lookups come in blocks,
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300379 * so most of the time we dont have to look up
380 * the full rbtree:
381 */
Adrian Hunter29ce3612014-07-16 11:07:13 +0300382 th = machine->last_match;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300383 if (th != NULL) {
384 if (th->tid == tid) {
385 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300386 return thread__get(th);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300387 }
388
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300389 machine->last_match = NULL;
Adrian Hunter99d725f2013-08-26 16:00:19 +0300390 }
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300391
392 while (*p != NULL) {
393 parent = *p;
394 th = rb_entry(parent, struct thread, rb_node);
395
Adrian Hunter38051232013-07-04 16:20:31 +0300396 if (th->tid == tid) {
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300397 machine->last_match = th;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300398 machine__update_thread_pid(machine, th, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300399 return thread__get(th);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300400 }
401
Adrian Hunter38051232013-07-04 16:20:31 +0300402 if (tid < th->tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300403 p = &(*p)->rb_left;
404 else
405 p = &(*p)->rb_right;
406 }
407
408 if (!create)
409 return NULL;
410
Adrian Hunter99d725f2013-08-26 16:00:19 +0300411 th = thread__new(pid, tid);
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300412 if (th != NULL) {
413 rb_link_node(&th->rb_node, parent, p);
414 rb_insert_color(&th->rb_node, &machine->threads);
Jiri Olsacddcef62014-04-09 20:54:29 +0200415
416 /*
417 * We have to initialize map_groups separately
418 * after rb tree is updated.
419 *
420 * The reason is that we call machine__findnew_thread
421 * within thread__init_map_groups to find the thread
422 * leader and that would screwed the rb tree.
423 */
Adrian Hunter418029b2014-07-16 10:19:44 +0300424 if (thread__init_map_groups(th, machine)) {
Arnaldo Carvalho de Melo0170b142015-05-25 15:23:05 -0300425 rb_erase_init(&th->rb_node, &machine->threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300426 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300427 thread__put(th);
Jiri Olsacddcef62014-04-09 20:54:29 +0200428 return NULL;
Adrian Hunter418029b2014-07-16 10:19:44 +0300429 }
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300430 /*
431 * It is now in the rbtree, get a ref
432 */
433 thread__get(th);
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -0300434 machine->last_match = th;
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300435 ++machine->nr_threads;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300436 }
437
438 return th;
439}
440
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300441struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
442{
443 return ____machine__findnew_thread(machine, pid, tid, true);
444}
445
Adrian Hunter314add62013-08-27 11:23:03 +0300446struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
447 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300448{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300449 struct thread *th;
450
451 pthread_rwlock_wrlock(&machine->threads_lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300452 th = __machine__findnew_thread(machine, pid, tid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300453 pthread_rwlock_unlock(&machine->threads_lock);
454 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300455}
456
Jiri Olsad75e6092014-03-14 15:00:03 +0100457struct thread *machine__find_thread(struct machine *machine, pid_t pid,
458 pid_t tid)
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300459{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300460 struct thread *th;
461 pthread_rwlock_rdlock(&machine->threads_lock);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -0300462 th = ____machine__findnew_thread(machine, pid, tid, false);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300463 pthread_rwlock_unlock(&machine->threads_lock);
464 return th;
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -0300465}
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300466
Adrian Huntercfe1c412014-07-31 09:00:45 +0300467struct comm *machine__thread_exec_comm(struct machine *machine,
468 struct thread *thread)
469{
470 if (machine->comm_exec)
471 return thread__exec_comm(thread);
472 else
473 return thread__comm(thread);
474}
475
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200476int machine__process_comm_event(struct machine *machine, union perf_event *event,
477 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300478{
Adrian Hunter314add62013-08-27 11:23:03 +0300479 struct thread *thread = machine__findnew_thread(machine,
480 event->comm.pid,
481 event->comm.tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +0300482 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300483 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300484
Adrian Huntercfe1c412014-07-31 09:00:45 +0300485 if (exec)
486 machine->comm_exec = true;
487
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300488 if (dump_trace)
489 perf_event__fprintf_comm(event, stdout);
490
Adrian Hunter65de51f2014-07-31 09:00:44 +0300491 if (thread == NULL ||
492 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300493 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300494 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300495 }
496
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300497 thread__put(thread);
498
499 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300500}
501
502int machine__process_lost_event(struct machine *machine __maybe_unused,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +0200503 union perf_event *event, struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -0300504{
505 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
506 event->lost.id, event->lost.lost);
507 return 0;
508}
509
Kan Liangc4937a92015-05-10 15:13:15 -0400510int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
511 union perf_event *event, struct perf_sample *sample)
512{
513 dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n",
514 sample->id, event->lost_samples.lost);
515 return 0;
516}
517
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300518static struct dso *machine__findnew_module_dso(struct machine *machine,
519 struct kmod_path *m,
520 const char *filename)
Jiri Olsada17ea32015-02-12 22:10:52 +0100521{
522 struct dso *dso;
Jiri Olsada17ea32015-02-12 22:10:52 +0100523
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300524 pthread_rwlock_wrlock(&machine->dsos.lock);
525
526 dso = __dsos__find(&machine->dsos, m->name, true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100527 if (!dso) {
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300528 dso = __dsos__addnew(&machine->dsos, m->name);
Jiri Olsada17ea32015-02-12 22:10:52 +0100529 if (dso == NULL)
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300530 goto out_unlock;
Jiri Olsada17ea32015-02-12 22:10:52 +0100531
532 if (machine__is_host(machine))
533 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
534 else
535 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
536
537 /* _KMODULE_COMP should be next to _KMODULE */
Jiri Olsaca333802015-02-17 17:29:57 +0100538 if (m->kmod && m->comp)
Jiri Olsada17ea32015-02-12 22:10:52 +0100539 dso->symtab_type++;
Jiri Olsaca333802015-02-17 17:29:57 +0100540
541 dso__set_short_name(dso, strdup(m->name), true);
542 dso__set_long_name(dso, strdup(filename), true);
Jiri Olsada17ea32015-02-12 22:10:52 +0100543 }
544
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300545 dso__get(dso);
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -0300546out_unlock:
547 pthread_rwlock_unlock(&machine->dsos.lock);
Jiri Olsada17ea32015-02-12 22:10:52 +0100548 return dso;
549}
550
Adrian Hunter4a96f7a2015-04-30 17:37:29 +0300551int machine__process_aux_event(struct machine *machine __maybe_unused,
552 union perf_event *event)
553{
554 if (dump_trace)
555 perf_event__fprintf_aux(event, stdout);
556 return 0;
557}
558
Adrian Hunter0ad21f62015-04-30 17:37:30 +0300559int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
560 union perf_event *event)
561{
562 if (dump_trace)
563 perf_event__fprintf_itrace_start(event, stdout);
564 return 0;
565}
566
Adrian Hunter02860392015-07-21 12:44:03 +0300567int machine__process_switch_event(struct machine *machine __maybe_unused,
568 union perf_event *event)
569{
570 if (dump_trace)
571 perf_event__fprintf_switch(event, stdout);
572 return 0;
573}
574
Wang Nanc03d5182015-11-26 03:59:57 +0000575static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename)
576{
577 const char *dup_filename;
578
579 if (!filename || !dso || !dso->long_name)
580 return;
581 if (dso->long_name[0] != '[')
582 return;
583 if (!strchr(filename, '/'))
584 return;
585
586 dup_filename = strdup(filename);
587 if (!dup_filename)
588 return;
589
Wang Nan5dcf16d2015-12-07 02:36:25 +0000590 dso__set_long_name(dso, dup_filename, true);
Wang Nanc03d5182015-11-26 03:59:57 +0000591}
592
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300593struct map *machine__findnew_module_map(struct machine *machine, u64 start,
594 const char *filename)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300595{
Jiri Olsaca333802015-02-17 17:29:57 +0100596 struct map *map = NULL;
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900597 struct dso *dso = NULL;
Jiri Olsaca333802015-02-17 17:29:57 +0100598 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300599
Jiri Olsaca333802015-02-17 17:29:57 +0100600 if (kmod_path__parse_name(&m, filename))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300601 return NULL;
602
Jiri Olsabc84f462015-02-17 17:31:18 +0100603 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
604 m.name);
Wang Nanc03d5182015-11-26 03:59:57 +0000605 if (map) {
606 /*
607 * If the map's dso is an offline module, give dso__load()
608 * a chance to find the file path of that module by fixing
609 * long_name.
610 */
611 dso__adjust_kmod_long_name(map->dso, filename);
Jiri Olsabc84f462015-02-17 17:31:18 +0100612 goto out;
Wang Nanc03d5182015-11-26 03:59:57 +0000613 }
Jiri Olsabc84f462015-02-17 17:31:18 +0100614
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300615 dso = machine__findnew_module_dso(machine, &m, filename);
Jiri Olsaca333802015-02-17 17:29:57 +0100616 if (dso == NULL)
617 goto out;
618
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300619 map = map__new2(start, dso, MAP__FUNCTION);
620 if (map == NULL)
Jiri Olsaca333802015-02-17 17:29:57 +0100621 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300622
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300623 map_groups__insert(&machine->kmaps, map);
Jiri Olsaca333802015-02-17 17:29:57 +0100624
Masami Hiramatsu9afcb4202015-11-18 15:40:20 +0900625 /* Put the map here because map_groups__insert alread got it */
626 map__put(map);
Jiri Olsaca333802015-02-17 17:29:57 +0100627out:
Masami Hiramatsu566c69c2015-11-18 15:40:35 +0900628 /* put the dso here, corresponding to machine__findnew_module_dso */
629 dso__put(dso);
Jiri Olsaca333802015-02-17 17:29:57 +0100630 free(m.name);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300631 return map;
632}
633
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300634size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300635{
636 struct rb_node *nd;
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300637 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300638
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300639 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300640 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300641 ret += __dsos__fprintf(&pos->dsos.head, fp);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300642 }
643
644 return ret;
645}
646
Waiman Long8fa7d872014-09-29 16:07:28 -0400647size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300648 bool (skip)(struct dso *dso, int parm), int parm)
649{
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300650 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300651}
652
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300653size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300654 bool (skip)(struct dso *dso, int parm), int parm)
655{
656 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300657 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300658
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300659 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300660 struct machine *pos = rb_entry(nd, struct machine, rb_node);
661 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
662 }
663 return ret;
664}
665
666size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
667{
668 int i;
669 size_t printed = 0;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300670 struct dso *kdso = machine__kernel_map(machine)->dso;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300671
672 if (kdso->has_build_id) {
673 char filename[PATH_MAX];
674 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
675 printed += fprintf(fp, "[0] %s\n", filename);
676 }
677
678 for (i = 0; i < vmlinux_path__nr_entries; ++i)
679 printed += fprintf(fp, "[%d] %s\n",
680 i + kdso->has_build_id, vmlinux_path[i]);
681
682 return printed;
683}
684
685size_t machine__fprintf(struct machine *machine, FILE *fp)
686{
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300687 size_t ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300688 struct rb_node *nd;
689
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300690 pthread_rwlock_rdlock(&machine->threads_lock);
691
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -0300692 ret = fprintf(fp, "Threads: %u\n", machine->nr_threads);
693
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300694 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
695 struct thread *pos = rb_entry(nd, struct thread, rb_node);
696
697 ret += thread__fprintf(pos, fp);
698 }
699
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300700 pthread_rwlock_unlock(&machine->threads_lock);
701
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300702 return ret;
703}
704
705static struct dso *machine__get_kernel(struct machine *machine)
706{
707 const char *vmlinux_name = NULL;
708 struct dso *kernel;
709
710 if (machine__is_host(machine)) {
711 vmlinux_name = symbol_conf.vmlinux_name;
712 if (!vmlinux_name)
Masami Hiramatsu0a775822016-05-15 12:19:40 +0900713 vmlinux_name = DSO__NAME_KALLSYMS;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300714
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300715 kernel = machine__findnew_kernel(machine, vmlinux_name,
716 "[kernel]", DSO_TYPE_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300717 } else {
718 char bf[PATH_MAX];
719
720 if (machine__is_default_guest(machine))
721 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
722 if (!vmlinux_name)
723 vmlinux_name = machine__mmap_name(machine, bf,
724 sizeof(bf));
725
Arnaldo Carvalho de Melo459ce512015-05-28 12:40:55 -0300726 kernel = machine__findnew_kernel(machine, vmlinux_name,
727 "[guest.kernel]",
728 DSO_TYPE_GUEST_KERNEL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300729 }
730
731 if (kernel != NULL && (!kernel->has_build_id))
732 dso__read_running_kernel_build_id(kernel, machine);
733
734 return kernel;
735}
736
737struct process_args {
738 u64 start;
739};
740
Adrian Hunter15a0a872014-01-29 16:14:38 +0200741static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
742 size_t bufsz)
743{
744 if (machine__is_default_guest(machine))
745 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
746 else
747 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
748}
749
Simon Quea93f0e52014-06-16 11:32:09 -0700750const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
751
752/* Figure out the start address of kernel map from /proc/kallsyms.
753 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
754 * symbol_name if it's not that important.
755 */
Adrian Hunter4b993752014-08-15 22:08:38 +0300756static u64 machine__get_running_kernel_start(struct machine *machine,
757 const char **symbol_name)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300758{
Adrian Hunter15a0a872014-01-29 16:14:38 +0200759 char filename[PATH_MAX];
Simon Quea93f0e52014-06-16 11:32:09 -0700760 int i;
761 const char *name;
762 u64 addr = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300763
Adrian Hunter15a0a872014-01-29 16:14:38 +0200764 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300765
766 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
767 return 0;
768
Simon Quea93f0e52014-06-16 11:32:09 -0700769 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
770 addr = kallsyms__get_function_start(filename, name);
771 if (addr)
772 break;
773 }
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300774
Simon Quea93f0e52014-06-16 11:32:09 -0700775 if (symbol_name)
776 *symbol_name = name;
777
778 return addr;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300779}
780
781int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
782{
783 enum map_type type;
Adrian Hunter4b993752014-08-15 22:08:38 +0300784 u64 start = machine__get_running_kernel_start(machine, NULL);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300785
Masami Hiramatsucc1121a2015-12-09 11:11:33 +0900786 /* In case of renewal the kernel map, destroy previous one */
787 machine__destroy_kernel_maps(machine);
788
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300789 for (type = 0; type < MAP__NR_TYPES; ++type) {
790 struct kmap *kmap;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300791 struct map *map;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300792
793 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
794 if (machine->vmlinux_maps[type] == NULL)
795 return -1;
796
797 machine->vmlinux_maps[type]->map_ip =
798 machine->vmlinux_maps[type]->unmap_ip =
799 identity__map_ip;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300800 map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300801 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000802 if (!kmap)
803 return -1;
804
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300805 kmap->kmaps = &machine->kmaps;
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300806 map_groups__insert(&machine->kmaps, map);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300807 }
808
809 return 0;
810}
811
812void machine__destroy_kernel_maps(struct machine *machine)
813{
814 enum map_type type;
815
816 for (type = 0; type < MAP__NR_TYPES; ++type) {
817 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300818 struct map *map = __machine__kernel_map(machine, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300819
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300820 if (map == NULL)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300821 continue;
822
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300823 kmap = map__kmap(map);
824 map_groups__remove(&machine->kmaps, map);
Wang Nanba927322015-04-07 08:22:45 +0000825 if (kmap && kmap->ref_reloc_sym) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300826 /*
827 * ref_reloc_sym is shared among all maps, so free just
828 * on one of them.
829 */
830 if (type == MAP__FUNCTION) {
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300831 zfree((char **)&kmap->ref_reloc_sym->name);
832 zfree(&kmap->ref_reloc_sym);
833 } else
834 kmap->ref_reloc_sym = NULL;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300835 }
836
Masami Hiramatsue96e4072015-11-18 15:40:22 +0900837 map__put(machine->vmlinux_maps[type]);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300838 machine->vmlinux_maps[type] = NULL;
839 }
840}
841
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300842int machines__create_guest_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300843{
844 int ret = 0;
845 struct dirent **namelist = NULL;
846 int i, items = 0;
847 char path[PATH_MAX];
848 pid_t pid;
849 char *endp;
850
851 if (symbol_conf.default_guest_vmlinux_name ||
852 symbol_conf.default_guest_modules ||
853 symbol_conf.default_guest_kallsyms) {
854 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
855 }
856
857 if (symbol_conf.guestmount) {
858 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
859 if (items <= 0)
860 return -ENOENT;
861 for (i = 0; i < items; i++) {
862 if (!isdigit(namelist[i]->d_name[0])) {
863 /* Filter out . and .. */
864 continue;
865 }
866 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
867 if ((*endp != '\0') ||
868 (endp == namelist[i]->d_name) ||
869 (errno == ERANGE)) {
870 pr_debug("invalid directory (%s). Skipping.\n",
871 namelist[i]->d_name);
872 continue;
873 }
874 sprintf(path, "%s/%s/proc/kallsyms",
875 symbol_conf.guestmount,
876 namelist[i]->d_name);
877 ret = access(path, R_OK);
878 if (ret) {
879 pr_debug("Can't access file %s\n", path);
880 goto failure;
881 }
882 machines__create_kernel_maps(machines, pid);
883 }
884failure:
885 free(namelist);
886 }
887
888 return ret;
889}
890
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300891void machines__destroy_kernel_maps(struct machines *machines)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300892{
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300893 struct rb_node *next = rb_first(&machines->guests);
894
895 machine__destroy_kernel_maps(&machines->host);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300896
897 while (next) {
898 struct machine *pos = rb_entry(next, struct machine, rb_node);
899
900 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300901 rb_erase(&pos->rb_node, &machines->guests);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300902 machine__delete(pos);
903 }
904}
905
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300906int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300907{
908 struct machine *machine = machines__findnew(machines, pid);
909
910 if (machine == NULL)
911 return -1;
912
913 return machine__create_kernel_maps(machine);
914}
915
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300916int __machine__load_kallsyms(struct machine *machine, const char *filename,
917 enum map_type type, bool no_kcore, symbol_filter_t filter)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300918{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300919 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300920 int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore, filter);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300921
922 if (ret > 0) {
923 dso__set_loaded(map->dso, type);
924 /*
925 * Since /proc/kallsyms will have multiple sessions for the
926 * kernel, with modules between them, fixup the end of all
927 * sections.
928 */
929 __map_groups__fixup_end(&machine->kmaps, type);
930 }
931
932 return ret;
933}
934
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -0300935int machine__load_kallsyms(struct machine *machine, const char *filename,
936 enum map_type type, symbol_filter_t filter)
937{
938 return __machine__load_kallsyms(machine, filename, type, false, filter);
939}
940
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300941int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
942 symbol_filter_t filter)
943{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300944 struct map *map = machine__kernel_map(machine);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300945 int ret = dso__load_vmlinux_path(map->dso, map, filter);
946
Adrian Hunter39b12f782013-08-07 14:38:47 +0300947 if (ret > 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300948 dso__set_loaded(map->dso, type);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300949
950 return ret;
951}
952
953static void map_groups__fixup_end(struct map_groups *mg)
954{
955 int i;
956 for (i = 0; i < MAP__NR_TYPES; ++i)
957 __map_groups__fixup_end(mg, i);
958}
959
960static char *get_kernel_version(const char *root_dir)
961{
962 char version[PATH_MAX];
963 FILE *file;
964 char *name, *tmp;
965 const char *prefix = "Linux version ";
966
967 sprintf(version, "%s/proc/version", root_dir);
968 file = fopen(version, "r");
969 if (!file)
970 return NULL;
971
972 version[0] = '\0';
973 tmp = fgets(version, sizeof(version), file);
974 fclose(file);
975
976 name = strstr(version, prefix);
977 if (!name)
978 return NULL;
979 name += strlen(prefix);
980 tmp = strchr(name, ' ');
981 if (tmp)
982 *tmp = '\0';
983
984 return strdup(name);
985}
986
Jiri Olsabb58a8a2015-02-12 22:20:01 +0100987static bool is_kmod_dso(struct dso *dso)
988{
989 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
990 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
991}
992
993static int map_groups__set_module_path(struct map_groups *mg, const char *path,
994 struct kmod_path *m)
995{
996 struct map *map;
997 char *long_name;
998
999 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
1000 if (map == NULL)
1001 return 0;
1002
1003 long_name = strdup(path);
1004 if (long_name == NULL)
1005 return -ENOMEM;
1006
1007 dso__set_long_name(map->dso, long_name, true);
1008 dso__kernel_module_get_build_id(map->dso, "");
1009
1010 /*
1011 * Full name could reveal us kmod compression, so
1012 * we need to update the symtab_type if needed.
1013 */
1014 if (m->comp && is_kmod_dso(map->dso))
1015 map->dso->symtab_type++;
1016
1017 return 0;
1018}
1019
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001020static int map_groups__set_modules_path_dir(struct map_groups *mg,
Richard Yao61d42902014-04-26 13:17:55 -04001021 const char *dir_name, int depth)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001022{
1023 struct dirent *dent;
1024 DIR *dir = opendir(dir_name);
1025 int ret = 0;
1026
1027 if (!dir) {
1028 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1029 return -1;
1030 }
1031
1032 while ((dent = readdir(dir)) != NULL) {
1033 char path[PATH_MAX];
1034 struct stat st;
1035
1036 /*sshfs might return bad dent->d_type, so we have to stat*/
1037 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1038 if (stat(path, &st))
1039 continue;
1040
1041 if (S_ISDIR(st.st_mode)) {
1042 if (!strcmp(dent->d_name, ".") ||
1043 !strcmp(dent->d_name, ".."))
1044 continue;
1045
Richard Yao61d42902014-04-26 13:17:55 -04001046 /* Do not follow top-level source and build symlinks */
1047 if (depth == 0) {
1048 if (!strcmp(dent->d_name, "source") ||
1049 !strcmp(dent->d_name, "build"))
1050 continue;
1051 }
1052
1053 ret = map_groups__set_modules_path_dir(mg, path,
1054 depth + 1);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001055 if (ret < 0)
1056 goto out;
1057 } else {
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001058 struct kmod_path m;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001059
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001060 ret = kmod_path__parse_name(&m, dent->d_name);
1061 if (ret)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001062 goto out;
Jiri Olsabb58a8a2015-02-12 22:20:01 +01001063
1064 if (m.kmod)
1065 ret = map_groups__set_module_path(mg, path, &m);
1066
1067 free(m.name);
1068
1069 if (ret)
1070 goto out;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001071 }
1072 }
1073
1074out:
1075 closedir(dir);
1076 return ret;
1077}
1078
1079static int machine__set_modules_path(struct machine *machine)
1080{
1081 char *version;
1082 char modules_path[PATH_MAX];
1083
1084 version = get_kernel_version(machine->root_dir);
1085 if (!version)
1086 return -1;
1087
Richard Yao61d42902014-04-26 13:17:55 -04001088 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001089 machine->root_dir, version);
1090 free(version);
1091
Richard Yao61d42902014-04-26 13:17:55 -04001092 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001093}
1094
Adrian Hunter316d70d2013-10-08 11:45:48 +03001095static int machine__create_module(void *arg, const char *name, u64 start)
1096{
1097 struct machine *machine = arg;
1098 struct map *map;
1099
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001100 map = machine__findnew_module_map(machine, start, name);
Adrian Hunter316d70d2013-10-08 11:45:48 +03001101 if (map == NULL)
1102 return -1;
1103
1104 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1105
1106 return 0;
1107}
1108
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001109static int machine__create_modules(struct machine *machine)
1110{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001111 const char *modules;
1112 char path[PATH_MAX];
1113
Adrian Hunterf4be9042013-09-22 13:22:09 +03001114 if (machine__is_default_guest(machine)) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001115 modules = symbol_conf.default_guest_modules;
Adrian Hunterf4be9042013-09-22 13:22:09 +03001116 } else {
1117 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001118 modules = path;
1119 }
1120
Adrian Hunteraa7fe3b2013-09-22 13:22:09 +03001121 if (symbol__restricted_filename(modules, "/proc/modules"))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001122 return -1;
1123
Adrian Hunter316d70d2013-10-08 11:45:48 +03001124 if (modules__parse(modules, machine, machine__create_module))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001125 return -1;
1126
Adrian Hunter316d70d2013-10-08 11:45:48 +03001127 if (!machine__set_modules_path(machine))
1128 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001129
Adrian Hunter316d70d2013-10-08 11:45:48 +03001130 pr_debug("Problems setting modules path maps, continuing anyway...\n");
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001131
Jason Wessel8f76fcd2013-07-15 15:27:53 -05001132 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001133}
1134
1135int machine__create_kernel_maps(struct machine *machine)
1136{
1137 struct dso *kernel = machine__get_kernel(machine);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001138 const char *name;
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001139 u64 addr;
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001140 int ret;
1141
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001142 if (kernel == NULL)
Adrian Hunter5512cf22014-01-29 16:14:39 +02001143 return -1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001144
Masami Hiramatsu1154c952015-11-18 15:40:33 +09001145 ret = __machine__create_kernel_maps(machine, kernel);
1146 dso__put(kernel);
1147 if (ret < 0)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001148 return -1;
1149
1150 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1151 if (machine__is_host(machine))
1152 pr_debug("Problems creating module maps, "
1153 "continuing anyway...\n");
1154 else
1155 pr_debug("Problems creating module maps for guest %d, "
1156 "continuing anyway...\n", machine->pid);
1157 }
1158
1159 /*
1160 * Now that we have all the maps created, just set the ->end of them:
1161 */
1162 map_groups__fixup_end(&machine->kmaps);
Adrian Hunter5512cf22014-01-29 16:14:39 +02001163
Arnaldo Carvalho de Melo45e90052016-05-17 11:52:26 -03001164 addr = machine__get_running_kernel_start(machine, &name);
1165 if (!addr) {
1166 } else if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) {
Adrian Hunter5512cf22014-01-29 16:14:39 +02001167 machine__destroy_kernel_maps(machine);
1168 return -1;
1169 }
1170
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001171 return 0;
1172}
1173
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001174static void machine__set_kernel_mmap_len(struct machine *machine,
1175 union perf_event *event)
1176{
Namhyung Kim4552cf02012-11-07 16:27:10 +09001177 int i;
1178
1179 for (i = 0; i < MAP__NR_TYPES; i++) {
1180 machine->vmlinux_maps[i]->start = event->mmap.start;
1181 machine->vmlinux_maps[i]->end = (event->mmap.start +
1182 event->mmap.len);
1183 /*
1184 * Be a bit paranoid here, some perf.data file came with
1185 * a zero sized synthesized MMAP event for the kernel.
1186 */
1187 if (machine->vmlinux_maps[i]->end == 0)
1188 machine->vmlinux_maps[i]->end = ~0ULL;
1189 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001190}
1191
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001192static bool machine__uses_kcore(struct machine *machine)
1193{
1194 struct dso *dso;
1195
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001196 list_for_each_entry(dso, &machine->dsos.head, node) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001197 if (dso__is_kcore(dso))
1198 return true;
1199 }
1200
1201 return false;
1202}
1203
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001204static int machine__process_kernel_mmap_event(struct machine *machine,
1205 union perf_event *event)
1206{
1207 struct map *map;
1208 char kmmap_prefix[PATH_MAX];
1209 enum dso_kernel_type kernel_type;
1210 bool is_kernel_mmap;
1211
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001212 /* If we have maps from kcore then we do not need or want any others */
1213 if (machine__uses_kcore(machine))
1214 return 0;
1215
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001216 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1217 if (machine__is_host(machine))
1218 kernel_type = DSO_TYPE_KERNEL;
1219 else
1220 kernel_type = DSO_TYPE_GUEST_KERNEL;
1221
1222 is_kernel_mmap = memcmp(event->mmap.filename,
1223 kmmap_prefix,
1224 strlen(kmmap_prefix) - 1) == 0;
1225 if (event->mmap.filename[0] == '/' ||
1226 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001227 map = machine__findnew_module_map(machine, event->mmap.start,
1228 event->mmap.filename);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001229 if (map == NULL)
1230 goto out_problem;
1231
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001232 map->end = map->start + event->mmap.len;
1233 } else if (is_kernel_mmap) {
1234 const char *symbol_name = (event->mmap.filename +
1235 strlen(kmmap_prefix));
1236 /*
1237 * Should be there already, from the build-id table in
1238 * the header.
1239 */
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001240 struct dso *kernel = NULL;
1241 struct dso *dso;
1242
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001243 pthread_rwlock_rdlock(&machine->dsos.lock);
1244
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -03001245 list_for_each_entry(dso, &machine->dsos.head, node) {
Wang Nan1f121b02015-06-03 08:52:21 +00001246
1247 /*
1248 * The cpumode passed to is_kernel_module is not the
1249 * cpumode of *this* event. If we insist on passing
1250 * correct cpumode to is_kernel_module, we should
1251 * record the cpumode when we adding this dso to the
1252 * linked list.
1253 *
1254 * However we don't really need passing correct
1255 * cpumode. We know the correct cpumode must be kernel
1256 * mode (if not, we should not link it onto kernel_dsos
1257 * list).
1258 *
1259 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1260 * is_kernel_module() treats it as a kernel cpumode.
1261 */
1262
1263 if (!dso->kernel ||
1264 is_kernel_module(dso->long_name,
1265 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001266 continue;
1267
Wang Nan1f121b02015-06-03 08:52:21 +00001268
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001269 kernel = dso;
1270 break;
1271 }
1272
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03001273 pthread_rwlock_unlock(&machine->dsos.lock);
1274
Namhyung Kimb837a8b2014-11-04 10:14:33 +09001275 if (kernel == NULL)
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001276 kernel = machine__findnew_dso(machine, kmmap_prefix);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001277 if (kernel == NULL)
1278 goto out_problem;
1279
1280 kernel->kernel = kernel_type;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001281 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1282 dso__put(kernel);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001283 goto out_problem;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001284 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001285
Namhyung Kim330dfa22014-11-18 13:30:28 +09001286 if (strstr(kernel->long_name, "vmlinux"))
1287 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
Namhyung Kim96d78052014-11-04 10:14:34 +09001288
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001289 machine__set_kernel_mmap_len(machine, event);
1290
1291 /*
1292 * Avoid using a zero address (kptr_restrict) for the ref reloc
1293 * symbol. Effectively having zero here means that at record
1294 * time /proc/sys/kernel/kptr_restrict was non zero.
1295 */
1296 if (event->mmap.pgoff != 0) {
1297 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1298 symbol_name,
1299 event->mmap.pgoff);
1300 }
1301
1302 if (machine__is_default_guest(machine)) {
1303 /*
1304 * preload dso of guest kernel and modules
1305 */
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001306 dso__load(kernel, machine__kernel_map(machine), NULL);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001307 }
1308 }
1309 return 0;
1310out_problem:
1311 return -1;
1312}
1313
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001314int machine__process_mmap2_event(struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001315 union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001316 struct perf_sample *sample)
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001317{
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001318 struct thread *thread;
1319 struct map *map;
1320 enum map_type type;
1321 int ret = 0;
1322
1323 if (dump_trace)
1324 perf_event__fprintf_mmap2(event, stdout);
1325
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001326 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1327 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001328 ret = machine__process_kernel_mmap_event(machine, event);
1329 if (ret < 0)
1330 goto out_problem;
1331 return 0;
1332 }
1333
1334 thread = machine__findnew_thread(machine, event->mmap2.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001335 event->mmap2.tid);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001336 if (thread == NULL)
1337 goto out_problem;
1338
1339 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1340 type = MAP__VARIABLE;
1341 else
1342 type = MAP__FUNCTION;
1343
Adrian Hunter2a030682014-07-22 16:17:53 +03001344 map = map__new(machine, event->mmap2.start,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001345 event->mmap2.len, event->mmap2.pgoff,
1346 event->mmap2.pid, event->mmap2.maj,
1347 event->mmap2.min, event->mmap2.ino,
1348 event->mmap2.ino_generation,
Don Zickus7ef80702014-05-19 15:13:49 -04001349 event->mmap2.prot,
1350 event->mmap2.flags,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001351 event->mmap2.filename, type, thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001352
1353 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001354 goto out_problem_map;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001355
1356 thread__insert_map(thread, map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001357 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001358 map__put(map);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001359 return 0;
1360
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001361out_problem_map:
1362 thread__put(thread);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001363out_problem:
1364 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1365 return 0;
1366}
1367
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001368int machine__process_mmap_event(struct machine *machine, union perf_event *event,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001369 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001370{
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001371 struct thread *thread;
1372 struct map *map;
Stephane Eranianbad40912013-01-24 16:10:40 +01001373 enum map_type type;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001374 int ret = 0;
1375
1376 if (dump_trace)
1377 perf_event__fprintf_mmap(event, stdout);
1378
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001379 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1380 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001381 ret = machine__process_kernel_mmap_event(machine, event);
1382 if (ret < 0)
1383 goto out_problem;
1384 return 0;
1385 }
1386
Adrian Hunter314add62013-08-27 11:23:03 +03001387 thread = machine__findnew_thread(machine, event->mmap.pid,
Don Zickus11c9abf2014-02-26 10:45:27 -05001388 event->mmap.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001389 if (thread == NULL)
1390 goto out_problem;
Stephane Eranianbad40912013-01-24 16:10:40 +01001391
1392 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1393 type = MAP__VARIABLE;
1394 else
1395 type = MAP__FUNCTION;
1396
Adrian Hunter2a030682014-07-22 16:17:53 +03001397 map = map__new(machine, event->mmap.start,
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001398 event->mmap.len, event->mmap.pgoff,
Don Zickus7ef80702014-05-19 15:13:49 -04001399 event->mmap.pid, 0, 0, 0, 0, 0, 0,
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001400 event->mmap.filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +03001401 type, thread);
Stephane Eranianbad40912013-01-24 16:10:40 +01001402
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001403 if (map == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001404 goto out_problem_map;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001405
1406 thread__insert_map(thread, map);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001407 thread__put(thread);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001408 map__put(map);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001409 return 0;
1410
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001411out_problem_map:
1412 thread__put(thread);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001413out_problem:
1414 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1415 return 0;
1416}
1417
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001418static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
David Ahern236a3bb2013-08-14 08:49:27 -06001419{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001420 if (machine->last_match == th)
Arnaldo Carvalho de Melo0ceb8f62015-05-11 18:08:12 -03001421 machine->last_match = NULL;
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001422
Arnaldo Carvalho de Melo59a51c12015-05-15 15:32:55 -03001423 BUG_ON(atomic_read(&th->refcnt) == 0);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001424 if (lock)
1425 pthread_rwlock_wrlock(&machine->threads_lock);
Arnaldo Carvalho de Melo0170b142015-05-25 15:23:05 -03001426 rb_erase_init(&th->rb_node, &machine->threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001427 RB_CLEAR_NODE(&th->rb_node);
Arnaldo Carvalho de Melod2c11032016-05-04 10:09:33 -03001428 --machine->nr_threads;
David Ahern236a3bb2013-08-14 08:49:27 -06001429 /*
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001430 * Move it first to the dead_threads list, then drop the reference,
1431 * if this is the last reference, then the thread__delete destructor
1432 * will be called and we will remove it from the dead_threads list.
David Ahern236a3bb2013-08-14 08:49:27 -06001433 */
1434 list_add_tail(&th->node, &machine->dead_threads);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001435 if (lock)
1436 pthread_rwlock_unlock(&machine->threads_lock);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001437 thread__put(th);
David Ahern236a3bb2013-08-14 08:49:27 -06001438}
1439
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001440void machine__remove_thread(struct machine *machine, struct thread *th)
1441{
1442 return __machine__remove_thread(machine, th, true);
1443}
1444
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001445int machine__process_fork_event(struct machine *machine, union perf_event *event,
1446 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001447{
Jiri Olsad75e6092014-03-14 15:00:03 +01001448 struct thread *thread = machine__find_thread(machine,
1449 event->fork.pid,
1450 event->fork.tid);
Adrian Hunter314add62013-08-27 11:23:03 +03001451 struct thread *parent = machine__findnew_thread(machine,
1452 event->fork.ppid,
1453 event->fork.ptid);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001454 int err = 0;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001455
Adrian Hunter5cb73342015-08-19 17:29:20 +03001456 if (dump_trace)
1457 perf_event__fprintf_task(event, stdout);
1458
1459 /*
1460 * There may be an existing thread that is not actually the parent,
1461 * either because we are processing events out of order, or because the
1462 * (fork) event that would have removed the thread was lost. Assume the
1463 * latter case and continue on as best we can.
1464 */
1465 if (parent->pid_ != (pid_t)event->fork.ppid) {
1466 dump_printf("removing erroneous parent thread %d/%d\n",
1467 parent->pid_, parent->tid);
1468 machine__remove_thread(machine, parent);
1469 thread__put(parent);
1470 parent = machine__findnew_thread(machine, event->fork.ppid,
1471 event->fork.ptid);
1472 }
1473
David Ahern236a3bb2013-08-14 08:49:27 -06001474 /* if a thread currently exists for the thread id remove it */
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001475 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001476 machine__remove_thread(machine, thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001477 thread__put(thread);
1478 }
David Ahern236a3bb2013-08-14 08:49:27 -06001479
Adrian Hunter314add62013-08-27 11:23:03 +03001480 thread = machine__findnew_thread(machine, event->fork.pid,
1481 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001482
1483 if (thread == NULL || parent == NULL ||
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001484 thread__fork(thread, parent, sample->time) < 0) {
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001485 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001486 err = -1;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001487 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001488 thread__put(thread);
1489 thread__put(parent);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001490
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001491 return err;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001492}
1493
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001494int machine__process_exit_event(struct machine *machine, union perf_event *event,
1495 struct perf_sample *sample __maybe_unused)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001496{
Jiri Olsad75e6092014-03-14 15:00:03 +01001497 struct thread *thread = machine__find_thread(machine,
1498 event->fork.pid,
1499 event->fork.tid);
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001500
1501 if (dump_trace)
1502 perf_event__fprintf_task(event, stdout);
1503
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001504 if (thread != NULL) {
David Ahern236a3bb2013-08-14 08:49:27 -06001505 thread__exited(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001506 thread__put(thread);
1507 }
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001508
1509 return 0;
1510}
1511
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001512int machine__process_event(struct machine *machine, union perf_event *event,
1513 struct perf_sample *sample)
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001514{
1515 int ret;
1516
1517 switch (event->header.type) {
1518 case PERF_RECORD_COMM:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001519 ret = machine__process_comm_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001520 case PERF_RECORD_MMAP:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001521 ret = machine__process_mmap_event(machine, event, sample); break;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001522 case PERF_RECORD_MMAP2:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001523 ret = machine__process_mmap2_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001524 case PERF_RECORD_FORK:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001525 ret = machine__process_fork_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001526 case PERF_RECORD_EXIT:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001527 ret = machine__process_exit_event(machine, event, sample); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001528 case PERF_RECORD_LOST:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001529 ret = machine__process_lost_event(machine, event, sample); break;
Adrian Hunter4a96f7a2015-04-30 17:37:29 +03001530 case PERF_RECORD_AUX:
1531 ret = machine__process_aux_event(machine, event); break;
Adrian Hunter0ad21f62015-04-30 17:37:30 +03001532 case PERF_RECORD_ITRACE_START:
Jiri Olsaceb92912015-06-29 13:27:45 +02001533 ret = machine__process_itrace_start_event(machine, event); break;
Kan Liangc4937a92015-05-10 15:13:15 -04001534 case PERF_RECORD_LOST_SAMPLES:
1535 ret = machine__process_lost_samples_event(machine, event, sample); break;
Adrian Hunter02860392015-07-21 12:44:03 +03001536 case PERF_RECORD_SWITCH:
1537 case PERF_RECORD_SWITCH_CPU_WIDE:
1538 ret = machine__process_switch_event(machine, event); break;
Arnaldo Carvalho de Melob0a7d1a2012-10-06 16:26:02 -03001539 default:
1540 ret = -1;
1541 break;
1542 }
1543
1544 return ret;
1545}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001546
Greg Priceb21484f2012-12-06 21:48:05 -08001547static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001548{
Greg Priceb21484f2012-12-06 21:48:05 -08001549 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001550 return 1;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001551 return 0;
1552}
1553
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001554static void ip__resolve_ams(struct thread *thread,
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001555 struct addr_map_symbol *ams,
1556 u64 ip)
1557{
1558 struct addr_location al;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001559
1560 memset(&al, 0, sizeof(al));
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -03001561 /*
1562 * We cannot use the header.misc hint to determine whether a
1563 * branch stack address is user, kernel, guest, hypervisor.
1564 * Branches may straddle the kernel/user/hypervisor boundaries.
1565 * Thus, we have to try consecutively until we find a match
1566 * or else, the symbol is unknown
1567 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001568 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001569
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001570 ams->addr = ip;
1571 ams->al_addr = al.addr;
1572 ams->sym = al.sym;
1573 ams->map = al.map;
1574}
1575
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001576static void ip__resolve_data(struct thread *thread,
Stephane Eranian98a3b322013-01-24 16:10:35 +01001577 u8 m, struct addr_map_symbol *ams, u64 addr)
1578{
1579 struct addr_location al;
1580
1581 memset(&al, 0, sizeof(al));
1582
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001583 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001584 if (al.map == NULL) {
1585 /*
1586 * some shared data regions have execute bit set which puts
1587 * their mapping in the MAP__FUNCTION type array.
1588 * Check there as a fallback option before dropping the sample.
1589 */
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001590 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
Don Zickus06b2afc2014-08-20 23:25:11 -04001591 }
1592
Stephane Eranian98a3b322013-01-24 16:10:35 +01001593 ams->addr = addr;
1594 ams->al_addr = al.addr;
1595 ams->sym = al.sym;
1596 ams->map = al.map;
1597}
1598
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -03001599struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1600 struct addr_location *al)
Stephane Eranian98a3b322013-01-24 16:10:35 +01001601{
1602 struct mem_info *mi = zalloc(sizeof(*mi));
1603
1604 if (!mi)
1605 return NULL;
1606
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001607 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1608 ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001609 mi->data_src.val = sample->data_src;
1610
1611 return mi;
1612}
1613
Andi Kleen37592b82014-11-12 18:05:19 -08001614static int add_callchain_ip(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001615 struct callchain_cursor *cursor,
Andi Kleen37592b82014-11-12 18:05:19 -08001616 struct symbol **parent,
1617 struct addr_location *root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001618 u8 *cpumode,
Andi Kleen37592b82014-11-12 18:05:19 -08001619 u64 ip)
1620{
1621 struct addr_location al;
1622
1623 al.filtered = 0;
1624 al.sym = NULL;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001625 if (!cpumode) {
Andi Kleen8b7bad52014-11-12 18:05:20 -08001626 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1627 ip, &al);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001628 } else {
Kan Liang2e777842014-12-02 10:06:53 -05001629 if (ip >= PERF_CONTEXT_MAX) {
1630 switch (ip) {
1631 case PERF_CONTEXT_HV:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001632 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
Kan Liang2e777842014-12-02 10:06:53 -05001633 break;
1634 case PERF_CONTEXT_KERNEL:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001635 *cpumode = PERF_RECORD_MISC_KERNEL;
Kan Liang2e777842014-12-02 10:06:53 -05001636 break;
1637 case PERF_CONTEXT_USER:
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001638 *cpumode = PERF_RECORD_MISC_USER;
Kan Liang2e777842014-12-02 10:06:53 -05001639 break;
1640 default:
1641 pr_debug("invalid callchain context: "
1642 "%"PRId64"\n", (s64) ip);
1643 /*
1644 * It seems the callchain is corrupted.
1645 * Discard all.
1646 */
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001647 callchain_cursor_reset(cursor);
Kan Liang2e777842014-12-02 10:06:53 -05001648 return 1;
1649 }
1650 return 0;
1651 }
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001652 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1653 ip, &al);
Kan Liang2e777842014-12-02 10:06:53 -05001654 }
1655
Andi Kleen37592b82014-11-12 18:05:19 -08001656 if (al.sym != NULL) {
Jiri Olsade7e6a72016-05-03 13:54:43 +02001657 if (perf_hpp_list.parent && !*parent &&
Andi Kleen37592b82014-11-12 18:05:19 -08001658 symbol__match_regex(al.sym, &parent_regex))
1659 *parent = al.sym;
1660 else if (have_ignore_callees && root_al &&
1661 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1662 /* Treat this symbol as the root,
1663 forgetting its callees. */
1664 *root_al = al;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001665 callchain_cursor_reset(cursor);
Andi Kleen37592b82014-11-12 18:05:19 -08001666 }
1667 }
1668
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09001669 if (symbol_conf.hide_unresolved && al.sym == NULL)
1670 return 0;
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001671 return callchain_cursor_append(cursor, al.addr, al.map, al.sym);
Andi Kleen37592b82014-11-12 18:05:19 -08001672}
1673
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001674struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1675 struct addr_location *al)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001676{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001677 unsigned int i;
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -03001678 const struct branch_stack *bs = sample->branch_stack;
1679 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001680
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001681 if (!bi)
1682 return NULL;
1683
1684 for (i = 0; i < bs->nr; i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03001685 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1686 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001687 bi[i].flags = bs->entries[i].flags;
1688 }
1689 return bi;
1690}
1691
Andi Kleen8b7bad52014-11-12 18:05:20 -08001692#define CHASHSZ 127
1693#define CHASHBITS 7
1694#define NO_ENTRY 0xff
1695
1696#define PERF_MAX_BRANCH_DEPTH 127
1697
1698/* Remove loops. */
1699static int remove_loops(struct branch_entry *l, int nr)
1700{
1701 int i, j, off;
1702 unsigned char chash[CHASHSZ];
1703
1704 memset(chash, NO_ENTRY, sizeof(chash));
1705
1706 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1707
1708 for (i = 0; i < nr; i++) {
1709 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1710
1711 /* no collision handling for now */
1712 if (chash[h] == NO_ENTRY) {
1713 chash[h] = i;
1714 } else if (l[chash[h]].from == l[i].from) {
1715 bool is_loop = true;
1716 /* check if it is a real loop */
1717 off = 0;
1718 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1719 if (l[j].from != l[i + off].from) {
1720 is_loop = false;
1721 break;
1722 }
1723 if (is_loop) {
1724 memmove(l + i, l + i + off,
1725 (nr - (i + off)) * sizeof(*l));
1726 nr -= off;
1727 }
1728 }
1729 }
1730 return nr;
1731}
1732
Kan Liang384b6052015-01-05 13:23:05 -05001733/*
1734 * Recolve LBR callstack chain sample
1735 * Return:
1736 * 1 on success get LBR callchain information
1737 * 0 no available LBR callchain information, should try fp
1738 * negative error code on other errors.
1739 */
1740static int resolve_lbr_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001741 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001742 struct perf_sample *sample,
1743 struct symbol **parent,
1744 struct addr_location *root_al,
1745 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001746{
Kan Liang384b6052015-01-05 13:23:05 -05001747 struct ip_callchain *chain = sample->callchain;
1748 int chain_nr = min(max_stack, (int)chain->nr);
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001749 u8 cpumode = PERF_RECORD_MISC_USER;
Kan Liang384b6052015-01-05 13:23:05 -05001750 int i, j, err;
1751 u64 ip;
1752
1753 for (i = 0; i < chain_nr; i++) {
1754 if (chain->ips[i] == PERF_CONTEXT_USER)
1755 break;
1756 }
1757
1758 /* LBR only affects the user callchain */
1759 if (i != chain_nr) {
1760 struct branch_stack *lbr_stack = sample->branch_stack;
1761 int lbr_nr = lbr_stack->nr;
1762 /*
1763 * LBR callstack can only get user call chain.
1764 * The mix_chain_nr is kernel call chain
1765 * number plus LBR user call chain number.
1766 * i is kernel call chain number,
1767 * 1 is PERF_CONTEXT_USER,
1768 * lbr_nr + 1 is the user call chain number.
1769 * For details, please refer to the comments
1770 * in callchain__printf
1771 */
1772 int mix_chain_nr = i + 1 + lbr_nr + 1;
1773
Kan Liang384b6052015-01-05 13:23:05 -05001774 for (j = 0; j < mix_chain_nr; j++) {
1775 if (callchain_param.order == ORDER_CALLEE) {
1776 if (j < i + 1)
1777 ip = chain->ips[j];
1778 else if (j > i + 1)
1779 ip = lbr_stack->entries[j - i - 2].from;
1780 else
1781 ip = lbr_stack->entries[0].to;
1782 } else {
1783 if (j < lbr_nr)
1784 ip = lbr_stack->entries[lbr_nr - j - 1].from;
1785 else if (j > lbr_nr)
1786 ip = chain->ips[i + 1 - (j - lbr_nr)];
1787 else
1788 ip = lbr_stack->entries[0].to;
1789 }
1790
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001791 err = add_callchain_ip(thread, cursor, parent, root_al, &cpumode, ip);
Kan Liang384b6052015-01-05 13:23:05 -05001792 if (err)
1793 return (err < 0) ? err : 0;
1794 }
1795 return 1;
1796 }
1797
1798 return 0;
1799}
1800
1801static int thread__resolve_callchain_sample(struct thread *thread,
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001802 struct callchain_cursor *cursor,
Kan Liang384b6052015-01-05 13:23:05 -05001803 struct perf_evsel *evsel,
1804 struct perf_sample *sample,
1805 struct symbol **parent,
1806 struct addr_location *root_al,
1807 int max_stack)
1808{
1809 struct branch_stack *branch = sample->branch_stack;
1810 struct ip_callchain *chain = sample->callchain;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03001811 int chain_nr = chain->nr;
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001812 u8 cpumode = PERF_RECORD_MISC_USER;
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001813 int i, j, err, nr_entries;
Andi Kleen8b7bad52014-11-12 18:05:20 -08001814 int skip_idx = -1;
1815 int first_call = 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001816
Arnaldo Carvalho de Meloacf2abb2016-04-18 10:35:03 -03001817 if (perf_evsel__has_branch_callstack(evsel)) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001818 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
Kan Liang384b6052015-01-05 13:23:05 -05001819 root_al, max_stack);
1820 if (err)
1821 return (err < 0) ? err : 0;
1822 }
1823
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001824 /*
1825 * Based on DWARF debug information, some architectures skip
1826 * a callchain entry saved by the kernel.
1827 */
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001828 skip_idx = arch_skip_callchain_idx(thread, chain);
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001829
Andi Kleen8b7bad52014-11-12 18:05:20 -08001830 /*
1831 * Add branches to call stack for easier browsing. This gives
1832 * more context for a sample than just the callers.
1833 *
1834 * This uses individual histograms of paths compared to the
1835 * aggregated histograms the normal LBR mode uses.
1836 *
1837 * Limitations for now:
1838 * - No extra filters
1839 * - No annotations (should annotate somehow)
1840 */
1841
1842 if (branch && callchain_param.branch_callstack) {
1843 int nr = min(max_stack, (int)branch->nr);
1844 struct branch_entry be[nr];
1845
1846 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
1847 pr_warning("corrupted branch chain. skipping...\n");
1848 goto check_calls;
1849 }
1850
1851 for (i = 0; i < nr; i++) {
1852 if (callchain_param.order == ORDER_CALLEE) {
1853 be[i] = branch->entries[i];
1854 /*
1855 * Check for overlap into the callchain.
1856 * The return address is one off compared to
1857 * the branch entry. To adjust for this
1858 * assume the calling instruction is not longer
1859 * than 8 bytes.
1860 */
1861 if (i == skip_idx ||
1862 chain->ips[first_call] >= PERF_CONTEXT_MAX)
1863 first_call++;
1864 else if (be[i].from < chain->ips[first_call] &&
1865 be[i].from >= chain->ips[first_call] - 8)
1866 first_call++;
1867 } else
1868 be[i] = branch->entries[branch->nr - i - 1];
1869 }
1870
1871 nr = remove_loops(be, nr);
1872
1873 for (i = 0; i < nr; i++) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001874 err = add_callchain_ip(thread, cursor, parent, root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001875 NULL, be[i].to);
Andi Kleen8b7bad52014-11-12 18:05:20 -08001876 if (!err)
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001877 err = add_callchain_ip(thread, cursor, parent, root_al,
David Hildenbrand73dbcd62015-03-30 10:11:00 +02001878 NULL, be[i].from);
Andi Kleen8b7bad52014-11-12 18:05:20 -08001879 if (err == -EINVAL)
1880 break;
1881 if (err)
1882 return err;
1883 }
1884 chain_nr -= nr;
1885 }
1886
1887check_calls:
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001888 for (i = first_call, nr_entries = 0;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03001889 i < chain_nr && nr_entries < max_stack; i++) {
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001890 u64 ip;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001891
1892 if (callchain_param.order == ORDER_CALLEE)
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001893 j = i;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001894 else
Sukadev Bhattiprolua60335b2014-06-25 08:49:03 -07001895 j = chain->nr - i - 1;
1896
1897#ifdef HAVE_SKIP_CALLCHAIN_IDX
1898 if (j == skip_idx)
1899 continue;
1900#endif
1901 ip = chain->ips[j];
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001902
Arnaldo Carvalho de Melobf8bddb2016-05-19 11:14:15 -03001903 if (ip < PERF_CONTEXT_MAX)
1904 ++nr_entries;
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -03001905
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001906 err = add_callchain_ip(thread, cursor, parent, root_al, &cpumode, ip);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001907
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001908 if (err)
Kan Liang2e777842014-12-02 10:06:53 -05001909 return (err < 0) ? err : 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001910 }
1911
1912 return 0;
1913}
1914
1915static int unwind_entry(struct unwind_entry *entry, void *arg)
1916{
1917 struct callchain_cursor *cursor = arg;
Namhyung Kimb49a8fe2015-11-26 16:08:20 +09001918
1919 if (symbol_conf.hide_unresolved && entry->sym == NULL)
1920 return 0;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001921 return callchain_cursor_append(cursor, entry->ip,
1922 entry->map, entry->sym);
1923}
1924
Chris Phlipot9919a652016-04-28 01:19:06 -07001925static int thread__resolve_callchain_unwind(struct thread *thread,
1926 struct callchain_cursor *cursor,
1927 struct perf_evsel *evsel,
1928 struct perf_sample *sample,
1929 int max_stack)
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001930{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001931 /* Can we do dwarf post unwind? */
1932 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1933 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1934 return 0;
1935
1936 /* Bail out if nothing was captured. */
1937 if ((!sample->user_regs.regs) ||
1938 (!sample->user_stack.size))
1939 return 0;
1940
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001941 return unwind__get_entries(unwind_entry, cursor,
Jiri Olsa352ea452014-01-07 13:47:25 +01001942 thread, sample, max_stack);
Chris Phlipot9919a652016-04-28 01:19:06 -07001943}
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001944
Chris Phlipot9919a652016-04-28 01:19:06 -07001945int thread__resolve_callchain(struct thread *thread,
1946 struct callchain_cursor *cursor,
1947 struct perf_evsel *evsel,
1948 struct perf_sample *sample,
1949 struct symbol **parent,
1950 struct addr_location *root_al,
1951 int max_stack)
1952{
1953 int ret = 0;
1954
1955 callchain_cursor_reset(&callchain_cursor);
1956
1957 if (callchain_param.order == ORDER_CALLEE) {
1958 ret = thread__resolve_callchain_sample(thread, cursor,
1959 evsel, sample,
1960 parent, root_al,
1961 max_stack);
1962 if (ret)
1963 return ret;
1964 ret = thread__resolve_callchain_unwind(thread, cursor,
1965 evsel, sample,
1966 max_stack);
1967 } else {
1968 ret = thread__resolve_callchain_unwind(thread, cursor,
1969 evsel, sample,
1970 max_stack);
1971 if (ret)
1972 return ret;
1973 ret = thread__resolve_callchain_sample(thread, cursor,
1974 evsel, sample,
1975 parent, root_al,
1976 max_stack);
1977 }
1978
1979 return ret;
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -03001980}
David Ahern35feee12013-09-28 13:12:58 -06001981
1982int machine__for_each_thread(struct machine *machine,
1983 int (*fn)(struct thread *thread, void *p),
1984 void *priv)
1985{
1986 struct rb_node *nd;
1987 struct thread *thread;
1988 int rc = 0;
1989
1990 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1991 thread = rb_entry(nd, struct thread, rb_node);
1992 rc = fn(thread, priv);
1993 if (rc != 0)
1994 return rc;
1995 }
1996
1997 list_for_each_entry(thread, &machine->dead_threads, node) {
1998 rc = fn(thread, priv);
1999 if (rc != 0)
2000 return rc;
2001 }
2002 return rc;
2003}
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002004
Adrian Huntera5499b32015-05-29 16:33:30 +03002005int machines__for_each_thread(struct machines *machines,
2006 int (*fn)(struct thread *thread, void *p),
2007 void *priv)
2008{
2009 struct rb_node *nd;
2010 int rc = 0;
2011
2012 rc = machine__for_each_thread(&machines->host, fn, priv);
2013 if (rc != 0)
2014 return rc;
2015
2016 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
2017 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2018
2019 rc = machine__for_each_thread(machine, fn, priv);
2020 if (rc != 0)
2021 return rc;
2022 }
2023 return rc;
2024}
2025
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03002026int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002027 struct target *target, struct thread_map *threads,
Kan Liang9d9cad72015-06-17 09:51:11 -04002028 perf_event__handler_t process, bool data_mmap,
2029 unsigned int proc_map_timeout)
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002030{
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002031 if (target__has_task(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002032 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002033 else if (target__has_cpu(target))
Kan Liang9d9cad72015-06-17 09:51:11 -04002034 return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout);
Arnaldo Carvalho de Melo58d925d2013-11-11 11:28:02 -03002035 /* command specified */
2036 return 0;
2037}
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002038
2039pid_t machine__get_current_tid(struct machine *machine, int cpu)
2040{
2041 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2042 return -1;
2043
2044 return machine->current_tid[cpu];
2045}
2046
2047int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2048 pid_t tid)
2049{
2050 struct thread *thread;
2051
2052 if (cpu < 0)
2053 return -EINVAL;
2054
2055 if (!machine->current_tid) {
2056 int i;
2057
2058 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2059 if (!machine->current_tid)
2060 return -ENOMEM;
2061 for (i = 0; i < MAX_NR_CPUS; i++)
2062 machine->current_tid[i] = -1;
2063 }
2064
2065 if (cpu >= MAX_NR_CPUS) {
2066 pr_err("Requested CPU %d too large. ", cpu);
2067 pr_err("Consider raising MAX_NR_CPUS\n");
2068 return -EINVAL;
2069 }
2070
2071 machine->current_tid[cpu] = tid;
2072
2073 thread = machine__findnew_thread(machine, pid, tid);
2074 if (!thread)
2075 return -ENOMEM;
2076
2077 thread->cpu = cpu;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002078 thread__put(thread);
Adrian Hunterb9d266b2014-07-22 16:17:25 +03002079
2080 return 0;
2081}
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002082
2083int machine__get_kernel_start(struct machine *machine)
2084{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002085 struct map *map = machine__kernel_map(machine);
Adrian Hunterfbe2af42014-08-15 22:08:39 +03002086 int err = 0;
2087
2088 /*
2089 * The only addresses above 2^63 are kernel addresses of a 64-bit
2090 * kernel. Note that addresses are unsigned so that on a 32-bit system
2091 * all addresses including kernel addresses are less than 2^32. In
2092 * that case (32-bit system), if the kernel mapping is unknown, all
2093 * addresses will be assumed to be in user space - see
2094 * machine__kernel_ip().
2095 */
2096 machine->kernel_start = 1ULL << 63;
2097 if (map) {
2098 err = map__load(map, machine->symbol_filter);
2099 if (map->start)
2100 machine->kernel_start = map->start;
2101 }
2102 return err;
2103}
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002104
2105struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2106{
Arnaldo Carvalho de Meloe8807842015-06-01 15:40:01 -03002107 return dsos__findnew(&machine->dsos, filename);
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002108}
Arnaldo Carvalho de Meloc3168b02015-07-22 16:14:29 -03002109
2110char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2111{
2112 struct machine *machine = vmachine;
2113 struct map *map;
2114 struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map, NULL);
2115
2116 if (sym == NULL)
2117 return NULL;
2118
2119 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2120 *addrp = map->unmap_ip(map, sym->start);
2121 return sym->name;
2122}