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