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