]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/util/event.c
perf thread_map: Use readdir() instead of deprecated readdir_r()
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / event.c
CommitLineData
234fbbf5 1#include <linux/types.h>
7ef80703 2#include <sys/mman.h>
234fbbf5
ACM
3#include "event.h"
4#include "debug.h"
b3cef7f6 5#include "hist.h"
f62d3f0f 6#include "machine.h"
c410a338 7#include "sort.h"
234fbbf5 8#include "string.h"
c410a338 9#include "strlist.h"
62daacb5 10#include "thread.h"
7c940c18 11#include "thread_map.h"
c506c96b 12#include "symbol/kallsyms.h"
67424342
JO
13#include "asm/bug.h"
14#include "stat.h"
234fbbf5 15
8115d60c 16static const char *perf_event__names[] = {
3cb6d154
FW
17 [0] = "TOTAL",
18 [PERF_RECORD_MMAP] = "MMAP",
5c5e854b 19 [PERF_RECORD_MMAP2] = "MMAP2",
3cb6d154
FW
20 [PERF_RECORD_LOST] = "LOST",
21 [PERF_RECORD_COMM] = "COMM",
22 [PERF_RECORD_EXIT] = "EXIT",
23 [PERF_RECORD_THROTTLE] = "THROTTLE",
24 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
25 [PERF_RECORD_FORK] = "FORK",
26 [PERF_RECORD_READ] = "READ",
27 [PERF_RECORD_SAMPLE] = "SAMPLE",
4a96f7a0 28 [PERF_RECORD_AUX] = "AUX",
0ad21f68 29 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
c4937a91 30 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
0286039f
AH
31 [PERF_RECORD_SWITCH] = "SWITCH",
32 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
3cb6d154
FW
33 [PERF_RECORD_HEADER_ATTR] = "ATTR",
34 [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
35 [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
36 [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
37 [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
3c659eed 38 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
a16ac023
AH
39 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
40 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
e9bf54d2 41 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
5f3339d2 42 [PERF_RECORD_THREAD_MAP] = "THREAD_MAP",
6640b6c2 43 [PERF_RECORD_CPU_MAP] = "CPU_MAP",
374fb9e3 44 [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG",
d80518c9 45 [PERF_RECORD_STAT] = "STAT",
2d8f0f18 46 [PERF_RECORD_STAT_ROUND] = "STAT_ROUND",
ffe77725 47 [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE",
46bc29b9 48 [PERF_RECORD_TIME_CONV] = "TIME_CONV",
c8446b9b
ACM
49};
50
8115d60c 51const char *perf_event__name(unsigned int id)
3835bc00 52{
8115d60c 53 if (id >= ARRAY_SIZE(perf_event__names))
3835bc00 54 return "INVALID";
8115d60c 55 if (!perf_event__names[id])
3835bc00 56 return "UNKNOWN";
8115d60c 57 return perf_event__names[id];
3835bc00
TG
58}
59
3ea223ad
ACM
60static int perf_tool__process_synth_event(struct perf_tool *tool,
61 union perf_event *event,
62 struct machine *machine,
63 perf_event__handler_t process)
64{
65 struct perf_sample synth_sample = {
640c03ce
ACM
66 .pid = -1,
67 .tid = -1,
68 .time = -1,
69 .stream_id = -1,
70 .cpu = -1,
71 .period = 1,
3ea223ad
ACM
72 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
73 };
74
75 return process(tool, event, &synth_sample, machine);
640c03ce
ACM
76};
77
5aa0b030
DA
78/*
79 * Assumes that the first 4095 bytes of /proc/pid/stat contains
ca6c41c5 80 * the comm, tgid and ppid.
5aa0b030 81 */
ca6c41c5
DA
82static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
83 pid_t *tgid, pid_t *ppid)
234fbbf5 84{
234fbbf5 85 char filename[PATH_MAX];
5aa0b030
DA
86 char bf[4096];
87 int fd;
38349665
AH
88 size_t size = 0;
89 ssize_t n;
ca6c41c5
DA
90 char *nl, *name, *tgids, *ppids;
91
92 *tgid = -1;
93 *ppid = -1;
234fbbf5
ACM
94
95 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
96
5aa0b030
DA
97 fd = open(filename, O_RDONLY);
98 if (fd < 0) {
234fbbf5 99 pr_debug("couldn't open %s\n", filename);
ca6c41c5 100 return -1;
234fbbf5
ACM
101 }
102
5aa0b030
DA
103 n = read(fd, bf, sizeof(bf) - 1);
104 close(fd);
105 if (n <= 0) {
ca6c41c5 106 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
5aa0b030
DA
107 pid);
108 return -1;
234fbbf5 109 }
5aa0b030 110 bf[n] = '\0';
234fbbf5 111
5aa0b030
DA
112 name = strstr(bf, "Name:");
113 tgids = strstr(bf, "Tgid:");
ca6c41c5 114 ppids = strstr(bf, "PPid:");
5aa0b030
DA
115
116 if (name) {
117 name += 5; /* strlen("Name:") */
118
119 while (*name && isspace(*name))
120 ++name;
121
122 nl = strchr(name, '\n');
123 if (nl)
124 *nl = '\0';
125
126 size = strlen(name);
127 if (size >= len)
128 size = len - 1;
129 memcpy(comm, name, size);
130 comm[size] = '\0';
131 } else {
132 pr_debug("Name: string not found for pid %d\n", pid);
133 }
134
135 if (tgids) {
136 tgids += 5; /* strlen("Tgid:") */
ca6c41c5 137 *tgid = atoi(tgids);
5aa0b030
DA
138 } else {
139 pr_debug("Tgid: string not found for pid %d\n", pid);
140 }
f5faf726 141
ca6c41c5
DA
142 if (ppids) {
143 ppids += 5; /* strlen("PPid:") */
144 *ppid = atoi(ppids);
145 } else {
146 pr_debug("PPid: string not found for pid %d\n", pid);
147 }
148
149 return 0;
f5faf726
DA
150}
151
ca6c41c5
DA
152static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
153 struct machine *machine,
154 pid_t *tgid, pid_t *ppid)
f5faf726 155{
f5faf726 156 size_t size;
ca6c41c5
DA
157
158 *ppid = -1;
f5faf726
DA
159
160 memset(&event->comm, 0, sizeof(event->comm));
161
ca6c41c5
DA
162 if (machine__is_host(machine)) {
163 if (perf_event__get_comm_ids(pid, event->comm.comm,
164 sizeof(event->comm.comm),
165 tgid, ppid) != 0) {
166 return -1;
167 }
168 } else {
169 *tgid = machine->pid;
170 }
f5db57c4 171
ca6c41c5
DA
172 if (*tgid < 0)
173 return -1;
f5faf726 174
ca6c41c5 175 event->comm.pid = *tgid;
9c90a61c 176 event->comm.header.type = PERF_RECORD_COMM;
f5faf726
DA
177
178 size = strlen(event->comm.comm) + 1;
9ac3e487 179 size = PERF_ALIGN(size, sizeof(u64));
743eb868 180 memset(event->comm.comm + size, 0, machine->id_hdr_size);
9c90a61c
ACM
181 event->comm.header.size = (sizeof(event->comm) -
182 (sizeof(event->comm.comm) - size) +
743eb868 183 machine->id_hdr_size);
bfd66cc7 184 event->comm.tid = pid;
ca6c41c5
DA
185
186 return 0;
4aa5f4f7
ACM
187}
188
e803cf97 189pid_t perf_event__synthesize_comm(struct perf_tool *tool,
4aa5f4f7
ACM
190 union perf_event *event, pid_t pid,
191 perf_event__handler_t process,
192 struct machine *machine)
193{
ca6c41c5 194 pid_t tgid, ppid;
4aa5f4f7 195
ca6c41c5
DA
196 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
197 return -1;
f5faf726 198
3ea223ad 199 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
bfd66cc7 200 return -1;
234fbbf5 201
9c90a61c 202 return tgid;
234fbbf5
ACM
203}
204
363b785f 205static int perf_event__synthesize_fork(struct perf_tool *tool,
ca6c41c5
DA
206 union perf_event *event,
207 pid_t pid, pid_t tgid, pid_t ppid,
208 perf_event__handler_t process,
363b785f
DZ
209 struct machine *machine)
210{
211 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
212
7764a385
DA
213 /*
214 * for main thread set parent to ppid from status file. For other
215 * threads set parent pid to main thread. ie., assume main thread
216 * spawns all threads in a process
217 */
218 if (tgid == pid) {
219 event->fork.ppid = ppid;
220 event->fork.ptid = ppid;
221 } else {
222 event->fork.ppid = tgid;
223 event->fork.ptid = tgid;
224 }
363b785f
DZ
225 event->fork.pid = tgid;
226 event->fork.tid = pid;
227 event->fork.header.type = PERF_RECORD_FORK;
228
229 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
230
3ea223ad 231 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
363b785f
DZ
232 return -1;
233
234 return 0;
235}
236
a18382b6
JO
237int perf_event__synthesize_mmap_events(struct perf_tool *tool,
238 union perf_event *event,
239 pid_t pid, pid_t tgid,
240 perf_event__handler_t process,
241 struct machine *machine,
9d9cad76
KL
242 bool mmap_data,
243 unsigned int proc_map_timeout)
234fbbf5
ACM
244{
245 char filename[PATH_MAX];
246 FILE *fp;
930e6fcd
KL
247 unsigned long long t;
248 bool truncation = false;
9d9cad76 249 unsigned long long timeout = proc_map_timeout * 1000000ULL;
1e6d5322 250 int rc = 0;
234fbbf5 251
c239c25a
DY
252 if (machine__is_default_guest(machine))
253 return 0;
254
99563465
DY
255 snprintf(filename, sizeof(filename), "%s/proc/%d/maps",
256 machine->root_dir, pid);
234fbbf5
ACM
257
258 fp = fopen(filename, "r");
259 if (fp == NULL) {
260 /*
261 * We raced with a task exiting - just return:
262 */
263 pr_debug("couldn't open %s\n", filename);
264 return -1;
265 }
266
a5a5ba72 267 event->header.type = PERF_RECORD_MMAP2;
930e6fcd 268 t = rdclock();
9c90a61c 269
234fbbf5 270 while (1) {
60648033
NK
271 char bf[BUFSIZ];
272 char prot[5];
273 char execname[PATH_MAX];
274 char anonstr[] = "//anon";
a5a5ba72 275 unsigned int ino;
234fbbf5 276 size_t size;
5c5e854b 277 ssize_t n;
60648033 278
234fbbf5
ACM
279 if (fgets(bf, sizeof(bf), fp) == NULL)
280 break;
281
9d9cad76
KL
282 if ((rdclock() - t) > timeout) {
283 pr_warning("Reading %s time out. "
284 "You may want to increase "
285 "the time limit by --proc-map-timeout\n",
286 filename);
930e6fcd
KL
287 truncation = true;
288 goto out;
289 }
290
60648033
NK
291 /* ensure null termination since stack will be reused. */
292 strcpy(execname, "");
293
234fbbf5 294 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
89fee59b 295 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
a5a5ba72
DZ
296 &event->mmap2.start, &event->mmap2.len, prot,
297 &event->mmap2.pgoff, &event->mmap2.maj,
298 &event->mmap2.min,
299 &ino, execname);
300
9d4ecc88
DZ
301 /*
302 * Anon maps don't have the execname.
303 */
a5a5ba72 304 if (n < 7)
5c5e854b 305 continue;
a5a5ba72
DZ
306
307 event->mmap2.ino = (u64)ino;
308
62605dc5
ACM
309 /*
310 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
311 */
73547aac
DY
312 if (machine__is_host(machine))
313 event->header.misc = PERF_RECORD_MISC_USER;
314 else
315 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
60648033 316
7ef80703
DZ
317 /* map protection and flags bits */
318 event->mmap2.prot = 0;
319 event->mmap2.flags = 0;
320 if (prot[0] == 'r')
321 event->mmap2.prot |= PROT_READ;
322 if (prot[1] == 'w')
323 event->mmap2.prot |= PROT_WRITE;
324 if (prot[2] == 'x')
325 event->mmap2.prot |= PROT_EXEC;
326
327 if (prot[3] == 's')
328 event->mmap2.flags |= MAP_SHARED;
329 else
330 event->mmap2.flags |= MAP_PRIVATE;
331
62605dc5
ACM
332 if (prot[2] != 'x') {
333 if (!mmap_data || prot[0] != 'r')
334 continue;
335
336 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
337 }
60648033 338
930e6fcd
KL
339out:
340 if (truncation)
341 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
342
60648033
NK
343 if (!strcmp(execname, ""))
344 strcpy(execname, anonstr);
345
346 size = strlen(execname) + 1;
a5a5ba72 347 memcpy(event->mmap2.filename, execname, size);
60648033 348 size = PERF_ALIGN(size, sizeof(u64));
a5a5ba72
DZ
349 event->mmap2.len -= event->mmap.start;
350 event->mmap2.header.size = (sizeof(event->mmap2) -
351 (sizeof(event->mmap2.filename) - size));
352 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
353 event->mmap2.header.size += machine->id_hdr_size;
354 event->mmap2.pid = tgid;
355 event->mmap2.tid = pid;
60648033 356
3ea223ad 357 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
60648033
NK
358 rc = -1;
359 break;
234fbbf5 360 }
930e6fcd
KL
361
362 if (truncation)
363 break;
234fbbf5
ACM
364 }
365
366 fclose(fp);
1e6d5322 367 return rc;
234fbbf5
ACM
368}
369
45694aa7 370int perf_event__synthesize_modules(struct perf_tool *tool,
d20deb64 371 perf_event__handler_t process,
8115d60c 372 struct machine *machine)
b7cece76 373{
1e6d5322 374 int rc = 0;
4bb7123d 375 struct map *pos;
23346f21 376 struct map_groups *kmaps = &machine->kmaps;
1eee78ae 377 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
8115d60c 378 union perf_event *event = zalloc((sizeof(event->mmap) +
743eb868 379 machine->id_hdr_size));
9c90a61c
ACM
380 if (event == NULL) {
381 pr_debug("Not enough memory synthesizing mmap event "
382 "for kernel modules\n");
383 return -1;
384 }
385
386 event->header.type = PERF_RECORD_MMAP;
b7cece76 387
a1645ce1
ZY
388 /*
389 * kernel uses 0 for user space maps, see kernel/perf_event.c
390 * __perf_event_mmap
391 */
23346f21 392 if (machine__is_host(machine))
9c90a61c 393 event->header.misc = PERF_RECORD_MISC_KERNEL;
a1645ce1 394 else
9c90a61c 395 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
a1645ce1 396
4bb7123d 397 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
b7cece76 398 size_t size;
b7cece76 399
ab9c2bdc 400 if (__map__is_kernel(pos))
b7cece76
ACM
401 continue;
402
9ac3e487 403 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
9c90a61c
ACM
404 event->mmap.header.type = PERF_RECORD_MMAP;
405 event->mmap.header.size = (sizeof(event->mmap) -
406 (sizeof(event->mmap.filename) - size));
743eb868
ACM
407 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
408 event->mmap.header.size += machine->id_hdr_size;
9c90a61c
ACM
409 event->mmap.start = pos->start;
410 event->mmap.len = pos->end - pos->start;
411 event->mmap.pid = machine->pid;
412
413 memcpy(event->mmap.filename, pos->dso->long_name,
b7cece76 414 pos->dso->long_name_len + 1);
3ea223ad 415 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
1e6d5322
DA
416 rc = -1;
417 break;
418 }
b7cece76
ACM
419 }
420
9c90a61c 421 free(event);
1e6d5322 422 return rc;
b7cece76
ACM
423}
424
8115d60c
ACM
425static int __event__synthesize_thread(union perf_event *comm_event,
426 union perf_event *mmap_event,
363b785f 427 union perf_event *fork_event,
defd8d38
DA
428 pid_t pid, int full,
429 perf_event__handler_t process,
45694aa7 430 struct perf_tool *tool,
9d9cad76
KL
431 struct machine *machine,
432 bool mmap_data,
433 unsigned int proc_map_timeout)
234fbbf5 434{
bfd66cc7
DZ
435 char filename[PATH_MAX];
436 DIR *tasks;
437 struct dirent dirent, *next;
ca6c41c5 438 pid_t tgid, ppid;
d998b732 439 int rc = 0;
bfd66cc7
DZ
440
441 /* special case: only send one comm event using passed in pid */
442 if (!full) {
443 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
444 process, machine);
445
446 if (tgid == -1)
447 return -1;
448
449 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
9d9cad76
KL
450 process, machine, mmap_data,
451 proc_map_timeout);
bfd66cc7
DZ
452 }
453
454 if (machine__is_default_guest(machine))
455 return 0;
456
457 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
458 machine->root_dir, pid);
459
460 tasks = opendir(filename);
461 if (tasks == NULL) {
462 pr_debug("couldn't open %s\n", filename);
463 return 0;
464 }
465
466 while (!readdir_r(tasks, &dirent, &next) && next) {
467 char *end;
bfd66cc7
DZ
468 pid_t _pid;
469
470 _pid = strtol(dirent.d_name, &end, 10);
471 if (*end)
472 continue;
473
d998b732 474 rc = -1;
ca6c41c5
DA
475 if (perf_event__prepare_comm(comm_event, _pid, machine,
476 &tgid, &ppid) != 0)
d998b732 477 break;
bfd66cc7 478
4aa5f4f7 479 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
ca6c41c5 480 ppid, process, machine) < 0)
d998b732 481 break;
4aa5f4f7
ACM
482 /*
483 * Send the prepared comm event
484 */
3ea223ad 485 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
d998b732 486 break;
4aa5f4f7 487
d998b732 488 rc = 0;
363b785f
DZ
489 if (_pid == pid) {
490 /* process the parent's maps too */
491 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
9d9cad76 492 process, machine, mmap_data, proc_map_timeout);
d998b732
ACM
493 if (rc)
494 break;
363b785f 495 }
bfd66cc7
DZ
496 }
497
498 closedir(tasks);
d998b732 499 return rc;
234fbbf5
ACM
500}
501
45694aa7 502int perf_event__synthesize_thread_map(struct perf_tool *tool,
d20deb64 503 struct thread_map *threads,
7c940c18 504 perf_event__handler_t process,
62605dc5 505 struct machine *machine,
9d9cad76
KL
506 bool mmap_data,
507 unsigned int proc_map_timeout)
9c90a61c 508{
363b785f 509 union perf_event *comm_event, *mmap_event, *fork_event;
defd8d38 510 int err = -1, thread, j;
9c90a61c 511
743eb868 512 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
9c90a61c
ACM
513 if (comm_event == NULL)
514 goto out;
515
b0fb978e 516 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
9c90a61c
ACM
517 if (mmap_event == NULL)
518 goto out_free_comm;
519
363b785f
DZ
520 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
521 if (fork_event == NULL)
522 goto out_free_mmap;
523
401b8e13
ACM
524 err = 0;
525 for (thread = 0; thread < threads->nr; ++thread) {
526 if (__event__synthesize_thread(comm_event, mmap_event,
363b785f 527 fork_event,
e13798c7 528 thread_map__pid(threads, thread), 0,
62605dc5 529 process, tool, machine,
9d9cad76 530 mmap_data, proc_map_timeout)) {
401b8e13
ACM
531 err = -1;
532 break;
533 }
defd8d38
DA
534
535 /*
536 * comm.pid is set to thread group id by
537 * perf_event__synthesize_comm
538 */
e13798c7 539 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
defd8d38
DA
540 bool need_leader = true;
541
542 /* is thread group leader in thread_map? */
543 for (j = 0; j < threads->nr; ++j) {
e13798c7 544 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
defd8d38
DA
545 need_leader = false;
546 break;
547 }
548 }
549
550 /* if not, generate events for it */
551 if (need_leader &&
62605dc5 552 __event__synthesize_thread(comm_event, mmap_event,
363b785f 553 fork_event,
62605dc5
ACM
554 comm_event->comm.pid, 0,
555 process, tool, machine,
9d9cad76 556 mmap_data, proc_map_timeout)) {
defd8d38
DA
557 err = -1;
558 break;
559 }
560 }
401b8e13 561 }
363b785f
DZ
562 free(fork_event);
563out_free_mmap:
9c90a61c
ACM
564 free(mmap_event);
565out_free_comm:
566 free(comm_event);
567out:
568 return err;
569}
570
45694aa7 571int perf_event__synthesize_threads(struct perf_tool *tool,
d20deb64 572 perf_event__handler_t process,
9d9cad76
KL
573 struct machine *machine,
574 bool mmap_data,
575 unsigned int proc_map_timeout)
234fbbf5
ACM
576{
577 DIR *proc;
99563465 578 char proc_path[PATH_MAX];
234fbbf5 579 struct dirent dirent, *next;
363b785f 580 union perf_event *comm_event, *mmap_event, *fork_event;
9c90a61c
ACM
581 int err = -1;
582
574799bf
NK
583 if (machine__is_default_guest(machine))
584 return 0;
585
743eb868 586 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
9c90a61c
ACM
587 if (comm_event == NULL)
588 goto out;
589
b0fb978e 590 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
9c90a61c
ACM
591 if (mmap_event == NULL)
592 goto out_free_comm;
234fbbf5 593
363b785f
DZ
594 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
595 if (fork_event == NULL)
596 goto out_free_mmap;
597
99563465
DY
598 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
599 proc = opendir(proc_path);
600
9c90a61c 601 if (proc == NULL)
363b785f 602 goto out_free_fork;
234fbbf5
ACM
603
604 while (!readdir_r(proc, &dirent, &next) && next) {
605 char *end;
606 pid_t pid = strtol(dirent.d_name, &end, 10);
607
608 if (*end) /* only interested in proper numerical dirents */
609 continue;
ba361c92
ACM
610 /*
611 * We may race with exiting thread, so don't stop just because
612 * one thread couldn't be synthesized.
613 */
363b785f 614 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
9d9cad76
KL
615 1, process, tool, machine, mmap_data,
616 proc_map_timeout);
234fbbf5
ACM
617 }
618
9c90a61c 619 err = 0;
1e6d5322 620 closedir(proc);
363b785f
DZ
621out_free_fork:
622 free(fork_event);
9c90a61c
ACM
623out_free_mmap:
624 free(mmap_event);
625out_free_comm:
626 free(comm_event);
627out:
628 return err;
234fbbf5 629}
62daacb5 630
56b03f3c
ACM
631struct process_symbol_args {
632 const char *name;
633 u64 start;
634};
635
3b01a413 636static int find_symbol_cb(void *arg, const char *name, char type,
82151520 637 u64 start)
56b03f3c
ACM
638{
639 struct process_symbol_args *args = arg;
640
881516eb
ACM
641 /*
642 * Must be a function or at least an alias, as in PARISC64, where "_text" is
643 * an 'A' to the same address as "_stext".
644 */
645 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
646 type == 'A') || strcmp(name, args->name))
56b03f3c
ACM
647 return 0;
648
649 args->start = start;
650 return 1;
651}
652
29b596b5
AH
653u64 kallsyms__get_function_start(const char *kallsyms_filename,
654 const char *symbol_name)
655{
656 struct process_symbol_args args = { .name = symbol_name, };
657
658 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
659 return 0;
660
661 return args.start;
662}
663
45694aa7 664int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
d20deb64 665 perf_event__handler_t process,
0ae617be 666 struct machine *machine)
56b03f3c
ACM
667{
668 size_t size;
0ae617be 669 const char *mmap_name;
a1645ce1 670 char name_buff[PATH_MAX];
a5e813c6 671 struct map *map = machine__kernel_map(machine);
0ae617be 672 struct kmap *kmap;
9c90a61c 673 int err;
a5c2a4c9
AK
674 union perf_event *event;
675
77e65977 676 if (map == NULL)
a5c2a4c9
AK
677 return -1;
678
56b03f3c
ACM
679 /*
680 * We should get this from /sys/kernel/sections/.text, but till that is
681 * available use this, and after it is use this as a fallback for older
682 * kernels.
683 */
a5c2a4c9 684 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
9c90a61c
ACM
685 if (event == NULL) {
686 pr_debug("Not enough memory synthesizing mmap event "
687 "for kernel modules\n");
688 return -1;
689 }
56b03f3c 690
48ea8f54 691 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
23346f21 692 if (machine__is_host(machine)) {
a1645ce1
ZY
693 /*
694 * kernel uses PERF_RECORD_MISC_USER for user space maps,
695 * see kernel/perf_event.c __perf_event_mmap
696 */
9c90a61c 697 event->header.misc = PERF_RECORD_MISC_KERNEL;
a1645ce1 698 } else {
9c90a61c 699 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
0b9e01a4 700 }
56b03f3c 701
0ae617be 702 kmap = map__kmap(map);
9c90a61c 703 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
0ae617be 704 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
9ac3e487 705 size = PERF_ALIGN(size, sizeof(u64));
9c90a61c
ACM
706 event->mmap.header.type = PERF_RECORD_MMAP;
707 event->mmap.header.size = (sizeof(event->mmap) -
743eb868 708 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
0ae617be 709 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
9c90a61c
ACM
710 event->mmap.start = map->start;
711 event->mmap.len = map->end - event->mmap.start;
712 event->mmap.pid = machine->pid;
713
3ea223ad 714 err = perf_tool__process_synth_event(tool, event, machine, process);
9c90a61c
ACM
715 free(event);
716
717 return err;
56b03f3c
ACM
718}
719
99471c96
JO
720int perf_event__synthesize_thread_map2(struct perf_tool *tool,
721 struct thread_map *threads,
722 perf_event__handler_t process,
723 struct machine *machine)
724{
725 union perf_event *event;
726 int i, err, size;
727
728 size = sizeof(event->thread_map);
729 size += threads->nr * sizeof(event->thread_map.entries[0]);
730
731 event = zalloc(size);
732 if (!event)
733 return -ENOMEM;
734
735 event->header.type = PERF_RECORD_THREAD_MAP;
736 event->header.size = size;
737 event->thread_map.nr = threads->nr;
738
739 for (i = 0; i < threads->nr; i++) {
740 struct thread_map_event_entry *entry = &event->thread_map.entries[i];
741 char *comm = thread_map__comm(threads, i);
742
743 if (!comm)
744 comm = (char *) "";
745
746 entry->pid = thread_map__pid(threads, i);
747 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
748 }
749
750 err = process(tool, event, NULL, machine);
751
752 free(event);
753 return err;
754}
755
6c872901
JO
756static void synthesize_cpus(struct cpu_map_entries *cpus,
757 struct cpu_map *map)
758{
759 int i;
760
761 cpus->nr = map->nr;
762
763 for (i = 0; i < map->nr; i++)
764 cpus->cpu[i] = map->map[i];
765}
766
767static void synthesize_mask(struct cpu_map_mask *mask,
768 struct cpu_map *map, int max)
769{
770 int i;
771
772 mask->nr = BITS_TO_LONGS(max);
773 mask->long_size = sizeof(long);
774
775 for (i = 0; i < map->nr; i++)
776 set_bit(map->map[i], mask->mask);
777}
778
779static size_t cpus_size(struct cpu_map *map)
780{
781 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
782}
783
784static size_t mask_size(struct cpu_map *map, int *max)
785{
786 int i;
787
788 *max = 0;
789
790 for (i = 0; i < map->nr; i++) {
791 /* bit possition of the cpu is + 1 */
792 int bit = map->map[i] + 1;
793
794 if (bit > *max)
795 *max = bit;
796 }
797
798 return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
799}
800
801void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
802{
803 size_t size_cpus, size_mask;
804 bool is_dummy = cpu_map__empty(map);
805
806 /*
807 * Both array and mask data have variable size based
808 * on the number of cpus and their actual values.
809 * The size of the 'struct cpu_map_data' is:
810 *
811 * array = size of 'struct cpu_map_entries' +
812 * number of cpus * sizeof(u64)
813 *
814 * mask = size of 'struct cpu_map_mask' +
815 * maximum cpu bit converted to size of longs
816 *
817 * and finaly + the size of 'struct cpu_map_data'.
818 */
819 size_cpus = cpus_size(map);
820 size_mask = mask_size(map, max);
821
822 if (is_dummy || (size_cpus < size_mask)) {
823 *size += size_cpus;
824 *type = PERF_CPU_MAP__CPUS;
825 } else {
826 *size += size_mask;
827 *type = PERF_CPU_MAP__MASK;
828 }
829
830 *size += sizeof(struct cpu_map_data);
831 return zalloc(*size);
832}
833
834void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
835 u16 type, int max)
836{
837 data->type = type;
838
839 switch (type) {
840 case PERF_CPU_MAP__CPUS:
841 synthesize_cpus((struct cpu_map_entries *) data->data, map);
842 break;
843 case PERF_CPU_MAP__MASK:
844 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
845 default:
846 break;
847 };
848}
849
850static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
851{
852 size_t size = sizeof(struct cpu_map_event);
853 struct cpu_map_event *event;
854 int max;
855 u16 type;
856
857 event = cpu_map_data__alloc(map, &size, &type, &max);
858 if (!event)
859 return NULL;
860
861 event->header.type = PERF_RECORD_CPU_MAP;
862 event->header.size = size;
863 event->data.type = type;
864
865 cpu_map_data__synthesize(&event->data, map, type, max);
866 return event;
867}
868
869int perf_event__synthesize_cpu_map(struct perf_tool *tool,
870 struct cpu_map *map,
871 perf_event__handler_t process,
872 struct machine *machine)
873{
874 struct cpu_map_event *event;
875 int err;
876
877 event = cpu_map_event__new(map);
878 if (!event)
879 return -ENOMEM;
880
881 err = process(tool, (union perf_event *) event, NULL, machine);
882
883 free(event);
884 return err;
885}
886
67424342
JO
887int perf_event__synthesize_stat_config(struct perf_tool *tool,
888 struct perf_stat_config *config,
889 perf_event__handler_t process,
890 struct machine *machine)
891{
892 struct stat_config_event *event;
893 int size, i = 0, err;
894
895 size = sizeof(*event);
896 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
897
898 event = zalloc(size);
899 if (!event)
900 return -ENOMEM;
901
902 event->header.type = PERF_RECORD_STAT_CONFIG;
903 event->header.size = size;
904 event->nr = PERF_STAT_CONFIG_TERM__MAX;
905
906#define ADD(__term, __val) \
907 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
908 event->data[i].val = __val; \
909 i++;
910
911 ADD(AGGR_MODE, config->aggr_mode)
912 ADD(INTERVAL, config->interval)
913 ADD(SCALE, config->scale)
914
915 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
916 "stat config terms unbalanced\n");
917#undef ADD
918
919 err = process(tool, (union perf_event *) event, NULL, machine);
920
921 free(event);
922 return err;
923}
924
5796f8f0
JO
925int perf_event__synthesize_stat(struct perf_tool *tool,
926 u32 cpu, u32 thread, u64 id,
927 struct perf_counts_values *count,
928 perf_event__handler_t process,
929 struct machine *machine)
930{
931 struct stat_event event;
932
933 event.header.type = PERF_RECORD_STAT;
934 event.header.size = sizeof(event);
935 event.header.misc = 0;
936
937 event.id = id;
938 event.cpu = cpu;
939 event.thread = thread;
940 event.val = count->val;
941 event.ena = count->ena;
942 event.run = count->run;
943
944 return process(tool, (union perf_event *) &event, NULL, machine);
945}
946
d4c22591
JO
947int perf_event__synthesize_stat_round(struct perf_tool *tool,
948 u64 evtime, u64 type,
949 perf_event__handler_t process,
950 struct machine *machine)
951{
952 struct stat_round_event event;
953
954 event.header.type = PERF_RECORD_STAT_ROUND;
955 event.header.size = sizeof(event);
956 event.header.misc = 0;
957
958 event.time = evtime;
959 event.type = type;
960
961 return process(tool, (union perf_event *) &event, NULL, machine);
962}
963
8e381596
JO
964void perf_event__read_stat_config(struct perf_stat_config *config,
965 struct stat_config_event *event)
966{
967 unsigned i;
968
969 for (i = 0; i < event->nr; i++) {
970
971 switch (event->data[i].tag) {
972#define CASE(__term, __val) \
973 case PERF_STAT_CONFIG_TERM__##__term: \
974 config->__val = event->data[i].val; \
975 break;
976
977 CASE(AGGR_MODE, aggr_mode)
978 CASE(SCALE, scale)
979 CASE(INTERVAL, interval)
980#undef CASE
981 default:
982 pr_warning("unknown stat config term %" PRIu64 "\n",
983 event->data[i].tag);
984 }
985 }
986}
987
482ad897
ACM
988size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
989{
022c50d0
AH
990 const char *s;
991
992 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
993 s = " exec";
994 else
995 s = "";
996
50674065 997 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
482ad897
ACM
998}
999
1d037ca1 1000int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
d20deb64 1001 union perf_event *event,
162f0bef 1002 struct perf_sample *sample,
743eb868 1003 struct machine *machine)
62daacb5 1004{
162f0bef 1005 return machine__process_comm_event(machine, event, sample);
62daacb5
ACM
1006}
1007
1d037ca1 1008int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
d20deb64 1009 union perf_event *event,
162f0bef 1010 struct perf_sample *sample,
b0a7d1a0 1011 struct machine *machine)
62daacb5 1012{
162f0bef 1013 return machine__process_lost_event(machine, event, sample);
a1645ce1 1014}
b7cece76 1015
4a96f7a0
AH
1016int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1017 union perf_event *event,
1018 struct perf_sample *sample __maybe_unused,
1019 struct machine *machine)
1020{
1021 return machine__process_aux_event(machine, event);
1022}
1023
0ad21f68
AH
1024int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1025 union perf_event *event,
1026 struct perf_sample *sample __maybe_unused,
1027 struct machine *machine)
1028{
1029 return machine__process_itrace_start_event(machine, event);
1030}
1031
c4937a91
KL
1032int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1033 union perf_event *event,
1034 struct perf_sample *sample,
1035 struct machine *machine)
1036{
1037 return machine__process_lost_samples_event(machine, event, sample);
1038}
1039
0286039f
AH
1040int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1041 union perf_event *event,
1042 struct perf_sample *sample __maybe_unused,
1043 struct machine *machine)
1044{
1045 return machine__process_switch_event(machine, event);
1046}
1047
482ad897
ACM
1048size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1049{
62605dc5 1050 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
482ad897 1051 event->mmap.pid, event->mmap.tid, event->mmap.start,
62605dc5
ACM
1052 event->mmap.len, event->mmap.pgoff,
1053 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1054 event->mmap.filename);
482ad897
ACM
1055}
1056
5c5e854b
SE
1057size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1058{
1059 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
7ef80703 1060 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
5c5e854b
SE
1061 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1062 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1063 event->mmap2.min, event->mmap2.ino,
1064 event->mmap2.ino_generation,
7ef80703
DZ
1065 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1066 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1067 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1068 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
5c5e854b
SE
1069 event->mmap2.filename);
1070}
1071
ec7fa596
JO
1072size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1073{
1074 struct thread_map *threads = thread_map__new_event(&event->thread_map);
1075 size_t ret;
1076
1077 ret = fprintf(fp, " nr: ");
1078
1079 if (threads)
1080 ret += thread_map__fprintf(threads, fp);
1081 else
1082 ret += fprintf(fp, "failed to get threads from event\n");
1083
1084 thread_map__put(threads);
1085 return ret;
1086}
1087
eb12a1af
JO
1088size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1089{
1090 struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1091 size_t ret;
1092
1093 ret = fprintf(fp, " nr: ");
1094
1095 if (cpus)
1096 ret += cpu_map__fprintf(cpus, fp);
1097 else
1098 ret += fprintf(fp, "failed to get cpumap from event\n");
1099
1100 cpu_map__put(cpus);
1101 return ret;
1102}
1103
b0a7d1a0 1104int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
d20deb64 1105 union perf_event *event,
162f0bef 1106 struct perf_sample *sample,
743eb868 1107 struct machine *machine)
a1645ce1 1108{
162f0bef 1109 return machine__process_mmap_event(machine, event, sample);
62daacb5
ACM
1110}
1111
5c5e854b
SE
1112int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1113 union perf_event *event,
162f0bef 1114 struct perf_sample *sample,
5c5e854b
SE
1115 struct machine *machine)
1116{
162f0bef 1117 return machine__process_mmap2_event(machine, event, sample);
5c5e854b
SE
1118}
1119
482ad897
ACM
1120size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1121{
1122 return fprintf(fp, "(%d:%d):(%d:%d)\n",
1123 event->fork.pid, event->fork.tid,
1124 event->fork.ppid, event->fork.ptid);
1125}
1126
f62d3f0f 1127int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
d20deb64 1128 union perf_event *event,
162f0bef 1129 struct perf_sample *sample,
f62d3f0f 1130 struct machine *machine)
62daacb5 1131{
162f0bef 1132 return machine__process_fork_event(machine, event, sample);
62daacb5 1133}
1ed091c4 1134
f62d3f0f
ACM
1135int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1136 union perf_event *event,
162f0bef 1137 struct perf_sample *sample,
f62d3f0f
ACM
1138 struct machine *machine)
1139{
162f0bef 1140 return machine__process_exit_event(machine, event, sample);
f62d3f0f
ACM
1141}
1142
4a96f7a0
AH
1143size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1144{
1145 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
1146 event->aux.aux_offset, event->aux.aux_size,
1147 event->aux.flags,
1148 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
1149 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
1150}
1151
0ad21f68
AH
1152size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1153{
1154 return fprintf(fp, " pid: %u tid: %u\n",
1155 event->itrace_start.pid, event->itrace_start.tid);
1156}
1157
0286039f
AH
1158size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1159{
1160 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1161 const char *in_out = out ? "OUT" : "IN ";
1162
1163 if (event->header.type == PERF_RECORD_SWITCH)
1164 return fprintf(fp, " %s\n", in_out);
1165
1166 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
1167 in_out, out ? "next" : "prev",
1168 event->context_switch.next_prev_pid,
1169 event->context_switch.next_prev_tid);
1170}
1171
482ad897
ACM
1172size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1173{
1174 size_t ret = fprintf(fp, "PERF_RECORD_%s",
1175 perf_event__name(event->header.type));
1176
1177 switch (event->header.type) {
1178 case PERF_RECORD_COMM:
1179 ret += perf_event__fprintf_comm(event, fp);
1180 break;
1181 case PERF_RECORD_FORK:
1182 case PERF_RECORD_EXIT:
1183 ret += perf_event__fprintf_task(event, fp);
1184 break;
1185 case PERF_RECORD_MMAP:
1186 ret += perf_event__fprintf_mmap(event, fp);
1187 break;
5c5e854b
SE
1188 case PERF_RECORD_MMAP2:
1189 ret += perf_event__fprintf_mmap2(event, fp);
1190 break;
4a96f7a0
AH
1191 case PERF_RECORD_AUX:
1192 ret += perf_event__fprintf_aux(event, fp);
1193 break;
0ad21f68
AH
1194 case PERF_RECORD_ITRACE_START:
1195 ret += perf_event__fprintf_itrace_start(event, fp);
1196 break;
0286039f
AH
1197 case PERF_RECORD_SWITCH:
1198 case PERF_RECORD_SWITCH_CPU_WIDE:
1199 ret += perf_event__fprintf_switch(event, fp);
1200 break;
482ad897
ACM
1201 default:
1202 ret += fprintf(fp, "\n");
1203 }
1204
1205 return ret;
1206}
1207
b0a7d1a0
ACM
1208int perf_event__process(struct perf_tool *tool __maybe_unused,
1209 union perf_event *event,
162f0bef 1210 struct perf_sample *sample,
b0a7d1a0 1211 struct machine *machine)
b83f920e 1212{
162f0bef 1213 return machine__process_event(machine, event, sample);
b83f920e
SD
1214}
1215
bb871a9c 1216void thread__find_addr_map(struct thread *thread, u8 cpumode,
743eb868 1217 enum map_type type, u64 addr,
326f59bf 1218 struct addr_location *al)
1ed091c4 1219{
93d5731d 1220 struct map_groups *mg = thread->mg;
bb871a9c 1221 struct machine *machine = mg->machine;
5b7ba82a 1222 bool load_map = false;
1ed091c4 1223
cc22e575 1224 al->machine = machine;
316c7136 1225 al->thread = thread;
1ed091c4 1226 al->addr = addr;
a1645ce1 1227 al->cpumode = cpumode;
b3cef7f6 1228 al->filtered = 0;
1ed091c4 1229
743eb868
ACM
1230 if (machine == NULL) {
1231 al->map = NULL;
1232 return;
1233 }
1234
a1645ce1 1235 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
1ed091c4 1236 al->level = 'k';
23346f21 1237 mg = &machine->kmaps;
5b7ba82a 1238 load_map = true;
a1645ce1 1239 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
1ed091c4 1240 al->level = '.';
a1645ce1
ZY
1241 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1242 al->level = 'g';
23346f21 1243 mg = &machine->kmaps;
5b7ba82a 1244 load_map = true;
fb50bb43
DY
1245 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1246 al->level = 'u';
a1645ce1 1247 } else {
fb50bb43 1248 al->level = 'H';
1ed091c4 1249 al->map = NULL;
a1645ce1
ZY
1250
1251 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1252 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1253 !perf_guest)
b3cef7f6 1254 al->filtered |= (1 << HIST_FILTER__GUEST);
a1645ce1
ZY
1255 if ((cpumode == PERF_RECORD_MISC_USER ||
1256 cpumode == PERF_RECORD_MISC_KERNEL) &&
1257 !perf_host)
b3cef7f6 1258 al->filtered |= (1 << HIST_FILTER__HOST);
a1645ce1 1259
1ed091c4
ACM
1260 return;
1261 }
1262try_again:
9958e1f0 1263 al->map = map_groups__find(mg, type, al->addr);
1ed091c4
ACM
1264 if (al->map == NULL) {
1265 /*
1266 * If this is outside of all known maps, and is a negative
1267 * address, try to look it up in the kernel dso, as it might be
1268 * a vsyscall or vdso (which executes in user-mode).
1269 *
1270 * XXX This is nasty, we should have a symbol list in the
1271 * "[vdso]" dso, but for now lets use the old trick of looking
1272 * in the whole kernel symbol list.
1273 */
fbe2af45
AH
1274 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1275 mg != &machine->kmaps &&
1276 machine__kernel_ip(machine, al->addr)) {
23346f21 1277 mg = &machine->kmaps;
1f2a7069 1278 load_map = true;
1ed091c4
ACM
1279 goto try_again;
1280 }
5b7ba82a
AH
1281 } else {
1282 /*
1283 * Kernel maps might be changed when loading symbols so loading
1284 * must be done prior to using kernel maps.
1285 */
1286 if (load_map)
326f59bf 1287 map__load(al->map, machine->symbol_filter);
1ed091c4 1288 al->addr = al->map->map_ip(al->map, al->addr);
5b7ba82a 1289 }
59ee68ec
ACM
1290}
1291
bb871a9c 1292void thread__find_addr_location(struct thread *thread,
743eb868 1293 u8 cpumode, enum map_type type, u64 addr,
61710bde 1294 struct addr_location *al)
59ee68ec 1295{
bb871a9c 1296 thread__find_addr_map(thread, cpumode, type, addr, al);
59ee68ec 1297 if (al->map != NULL)
61710bde 1298 al->sym = map__find_symbol(al->map, al->addr,
bb871a9c 1299 thread->mg->machine->symbol_filter);
59ee68ec
ACM
1300 else
1301 al->sym = NULL;
1ed091c4
ACM
1302}
1303
b91fc39f
ACM
1304/*
1305 * Callers need to drop the reference to al->thread, obtained in
1306 * machine__findnew_thread()
1307 */
bb3eb566
ACM
1308int machine__resolve(struct machine *machine, struct addr_location *al,
1309 struct perf_sample *sample)
1ed091c4 1310{
ef89325f 1311 struct thread *thread = machine__findnew_thread(machine, sample->pid,
13ce34df 1312 sample->tid);
41a37e20 1313
1ed091c4
ACM
1314 if (thread == NULL)
1315 return -1;
1316
b9c5143a 1317 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1f626bc3 1318 /*
743eb868 1319 * Have we already created the kernel maps for this machine?
1f626bc3
ACM
1320 *
1321 * This should have happened earlier, when we processed the kernel MMAP
1322 * events, but for older perf.data files there was no such thing, so do
1323 * it now.
1324 */
473398a2 1325 if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
a5e813c6 1326 machine__kernel_map(machine) == NULL)
743eb868 1327 machine__create_kernel_maps(machine);
1ed091c4 1328
473398a2 1329 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
1ed091c4
ACM
1330 dump_printf(" ...... dso: %s\n",
1331 al->map ? al->map->dso->long_name :
1332 al->level == 'H' ? "[hypervisor]" : "<not found>");
466fa764
NK
1333
1334 if (thread__is_filtered(thread))
1335 al->filtered |= (1 << HIST_FILTER__THREAD);
1336
96415e4d 1337 al->sym = NULL;
8d50e5b4 1338 al->cpu = sample->cpu;
0c4c4deb
KL
1339 al->socket = -1;
1340
1341 if (al->cpu >= 0) {
1342 struct perf_env *env = machine->env;
1343
1344 if (env && env->cpu)
1345 al->socket = env->cpu[al->cpu].socket_id;
1346 }
96415e4d
ACM
1347
1348 if (al->map) {
cb8f4e9a
NK
1349 struct dso *dso = al->map->dso;
1350
96415e4d 1351 if (symbol_conf.dso_list &&
cb8f4e9a
NK
1352 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1353 dso->short_name) ||
1354 (dso->short_name != dso->long_name &&
1355 strlist__has_entry(symbol_conf.dso_list,
b3cef7f6
NK
1356 dso->long_name))))) {
1357 al->filtered |= (1 << HIST_FILTER__DSO);
b3cef7f6 1358 }
96415e4d 1359
e44baa3e
AH
1360 al->sym = map__find_symbol(al->map, al->addr,
1361 machine->symbol_filter);
96415e4d 1362 }
c410a338 1363
1500b93b
FT
1364 if (symbol_conf.sym_list &&
1365 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
b3cef7f6
NK
1366 al->sym->name))) {
1367 al->filtered |= (1 << HIST_FILTER__SYMBOL);
b3cef7f6 1368 }
c410a338 1369
c410a338 1370 return 0;
1ed091c4 1371}
9b0d2d87 1372
b91fc39f
ACM
1373/*
1374 * The preprocess_sample method will return with reference counts for the
1375 * in it, when done using (and perhaps getting ref counts if needing to
1376 * keep a pointer to one of those entries) it must be paired with
1377 * addr_location__put(), so that the refcounts can be decremented.
1378 */
1379void addr_location__put(struct addr_location *al)
1380{
1381 thread__zput(al->thread);
1382}
1383
9b0d2d87
AH
1384bool is_bts_event(struct perf_event_attr *attr)
1385{
1386 return attr->type == PERF_TYPE_HARDWARE &&
1387 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1388 attr->sample_period == 1;
1389}
1390
1391bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1392{
1393 if (attr->type == PERF_TYPE_SOFTWARE &&
1394 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1395 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1396 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1397 return true;
1398
1399 if (is_bts_event(attr))
1400 return true;
1401
1402 return false;
1403}
1404
c2740a87
ACM
1405void thread__resolve(struct thread *thread, struct addr_location *al,
1406 struct perf_sample *sample)
9b0d2d87 1407{
473398a2 1408 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
9b0d2d87 1409 if (!al->map)
473398a2 1410 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
9b0d2d87
AH
1411 sample->addr, al);
1412
1413 al->cpu = sample->cpu;
1414 al->sym = NULL;
1415
1416 if (al->map)
1417 al->sym = map__find_symbol(al->map, al->addr, NULL);
1418}