1 #include <linux/types.h>
11 #include "thread_map.h"
12 #include "symbol/kallsyms.h"
14 static const char *perf_event__names[] = {
16 [PERF_RECORD_MMAP] = "MMAP",
17 [PERF_RECORD_MMAP2] = "MMAP2",
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",
26 [PERF_RECORD_AUX] = "AUX",
27 [PERF_RECORD_ITRACE_START] = "ITRACE_START",
28 [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
29 [PERF_RECORD_SWITCH] = "SWITCH",
30 [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
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",
36 [PERF_RECORD_ID_INDEX] = "ID_INDEX",
37 [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
38 [PERF_RECORD_AUXTRACE] = "AUXTRACE",
39 [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
42 const char *perf_event__name(unsigned int id)
44 if (id >= ARRAY_SIZE(perf_event__names))
46 if (!perf_event__names[id])
48 return perf_event__names[id];
51 static struct perf_sample synth_sample = {
61 * Assumes that the first 4095 bytes of /proc/pid/stat contains
62 * the comm, tgid and ppid.
64 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
65 pid_t *tgid, pid_t *ppid)
67 char filename[PATH_MAX];
72 char *nl, *name, *tgids, *ppids;
77 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
79 fd = open(filename, O_RDONLY);
81 pr_debug("couldn't open %s\n", filename);
85 n = read(fd, bf, sizeof(bf) - 1);
88 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
94 name = strstr(bf, "Name:");
95 tgids = strstr(bf, "Tgid:");
96 ppids = strstr(bf, "PPid:");
99 name += 5; /* strlen("Name:") */
101 while (*name && isspace(*name))
104 nl = strchr(name, '\n');
111 memcpy(comm, name, size);
114 pr_debug("Name: string not found for pid %d\n", pid);
118 tgids += 5; /* strlen("Tgid:") */
121 pr_debug("Tgid: string not found for pid %d\n", pid);
125 ppids += 5; /* strlen("PPid:") */
128 pr_debug("PPid: string not found for pid %d\n", pid);
134 static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
135 struct machine *machine,
136 pid_t *tgid, pid_t *ppid)
142 memset(&event->comm, 0, sizeof(event->comm));
144 if (machine__is_host(machine)) {
145 if (perf_event__get_comm_ids(pid, event->comm.comm,
146 sizeof(event->comm.comm),
151 *tgid = machine->pid;
157 event->comm.pid = *tgid;
158 event->comm.header.type = PERF_RECORD_COMM;
160 size = strlen(event->comm.comm) + 1;
161 size = PERF_ALIGN(size, sizeof(u64));
162 memset(event->comm.comm + size, 0, machine->id_hdr_size);
163 event->comm.header.size = (sizeof(event->comm) -
164 (sizeof(event->comm.comm) - size) +
165 machine->id_hdr_size);
166 event->comm.tid = pid;
171 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
172 union perf_event *event, pid_t pid,
173 perf_event__handler_t process,
174 struct machine *machine)
178 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
181 if (process(tool, event, &synth_sample, machine) != 0)
187 static int perf_event__synthesize_fork(struct perf_tool *tool,
188 union perf_event *event,
189 pid_t pid, pid_t tgid, pid_t ppid,
190 perf_event__handler_t process,
191 struct machine *machine)
193 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
196 * for main thread set parent to ppid from status file. For other
197 * threads set parent pid to main thread. ie., assume main thread
198 * spawns all threads in a process
201 event->fork.ppid = ppid;
202 event->fork.ptid = ppid;
204 event->fork.ppid = tgid;
205 event->fork.ptid = tgid;
207 event->fork.pid = tgid;
208 event->fork.tid = pid;
209 event->fork.header.type = PERF_RECORD_FORK;
211 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
213 if (process(tool, event, &synth_sample, machine) != 0)
219 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
220 union perf_event *event,
221 pid_t pid, pid_t tgid,
222 perf_event__handler_t process,
223 struct machine *machine,
225 unsigned int proc_map_timeout)
227 char filename[PATH_MAX];
229 unsigned long long t;
230 bool truncation = false;
231 unsigned long long timeout = proc_map_timeout * 1000000ULL;
234 if (machine__is_default_guest(machine))
237 snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
238 machine->root_dir, pid, pid);
240 fp = fopen(filename, "r");
243 * We raced with a task exiting - just return:
245 pr_debug("couldn't open %s\n", filename);
249 event->header.type = PERF_RECORD_MMAP2;
255 char execname[PATH_MAX];
256 char anonstr[] = "//anon";
261 if (fgets(bf, sizeof(bf), fp) == NULL)
264 if ((rdclock() - t) > timeout) {
265 pr_warning("Reading %s time out. "
266 "You may want to increase "
267 "the time limit by --proc-map-timeout\n",
273 /* ensure null termination since stack will be reused. */
274 strcpy(execname, "");
276 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
277 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
278 &event->mmap2.start, &event->mmap2.len, prot,
279 &event->mmap2.pgoff, &event->mmap2.maj,
284 * Anon maps don't have the execname.
289 event->mmap2.ino = (u64)ino;
292 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
294 if (machine__is_host(machine))
295 event->header.misc = PERF_RECORD_MISC_USER;
297 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
299 /* map protection and flags bits */
300 event->mmap2.prot = 0;
301 event->mmap2.flags = 0;
303 event->mmap2.prot |= PROT_READ;
305 event->mmap2.prot |= PROT_WRITE;
307 event->mmap2.prot |= PROT_EXEC;
310 event->mmap2.flags |= MAP_SHARED;
312 event->mmap2.flags |= MAP_PRIVATE;
314 if (prot[2] != 'x') {
315 if (!mmap_data || prot[0] != 'r')
318 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
323 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
325 if (!strcmp(execname, ""))
326 strcpy(execname, anonstr);
328 size = strlen(execname) + 1;
329 memcpy(event->mmap2.filename, execname, size);
330 size = PERF_ALIGN(size, sizeof(u64));
331 event->mmap2.len -= event->mmap.start;
332 event->mmap2.header.size = (sizeof(event->mmap2) -
333 (sizeof(event->mmap2.filename) - size));
334 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
335 event->mmap2.header.size += machine->id_hdr_size;
336 event->mmap2.pid = tgid;
337 event->mmap2.tid = pid;
339 if (process(tool, event, &synth_sample, machine) != 0) {
352 int perf_event__synthesize_modules(struct perf_tool *tool,
353 perf_event__handler_t process,
354 struct machine *machine)
358 struct map_groups *kmaps = &machine->kmaps;
359 struct maps *maps = &kmaps->maps[MAP__FUNCTION];
360 union perf_event *event = zalloc((sizeof(event->mmap) +
361 machine->id_hdr_size));
363 pr_debug("Not enough memory synthesizing mmap event "
364 "for kernel modules\n");
368 event->header.type = PERF_RECORD_MMAP;
371 * kernel uses 0 for user space maps, see kernel/perf_event.c
374 if (machine__is_host(machine))
375 event->header.misc = PERF_RECORD_MISC_KERNEL;
377 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
379 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
382 if (__map__is_kernel(pos))
385 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
386 event->mmap.header.type = PERF_RECORD_MMAP;
387 event->mmap.header.size = (sizeof(event->mmap) -
388 (sizeof(event->mmap.filename) - size));
389 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
390 event->mmap.header.size += machine->id_hdr_size;
391 event->mmap.start = pos->start;
392 event->mmap.len = pos->end - pos->start;
393 event->mmap.pid = machine->pid;
395 memcpy(event->mmap.filename, pos->dso->long_name,
396 pos->dso->long_name_len + 1);
397 if (process(tool, event, &synth_sample, machine) != 0) {
407 static int __event__synthesize_thread(union perf_event *comm_event,
408 union perf_event *mmap_event,
409 union perf_event *fork_event,
411 perf_event__handler_t process,
412 struct perf_tool *tool,
413 struct machine *machine,
415 unsigned int proc_map_timeout)
417 char filename[PATH_MAX];
419 struct dirent *dirent;
423 /* special case: only send one comm event using passed in pid */
425 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
431 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
432 process, machine, mmap_data,
436 if (machine__is_default_guest(machine))
439 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
440 machine->root_dir, pid);
442 tasks = opendir(filename);
444 pr_debug("couldn't open %s\n", filename);
448 while ((dirent = readdir(tasks)) != NULL) {
452 _pid = strtol(dirent->d_name, &end, 10);
457 if (perf_event__prepare_comm(comm_event, _pid, machine,
461 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
462 ppid, process, machine) < 0)
465 * Send the prepared comm event
467 if (process(tool, comm_event, &synth_sample, machine) != 0)
472 /* process the parent's maps too */
473 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
474 process, machine, mmap_data, proc_map_timeout);
484 int perf_event__synthesize_thread_map(struct perf_tool *tool,
485 struct thread_map *threads,
486 perf_event__handler_t process,
487 struct machine *machine,
489 unsigned int proc_map_timeout)
491 union perf_event *comm_event, *mmap_event, *fork_event;
492 int err = -1, thread, j;
494 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
495 if (comm_event == NULL)
498 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
499 if (mmap_event == NULL)
502 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
503 if (fork_event == NULL)
507 for (thread = 0; thread < threads->nr; ++thread) {
508 if (__event__synthesize_thread(comm_event, mmap_event,
510 thread_map__pid(threads, thread), 0,
511 process, tool, machine,
512 mmap_data, proc_map_timeout)) {
518 * comm.pid is set to thread group id by
519 * perf_event__synthesize_comm
521 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
522 bool need_leader = true;
524 /* is thread group leader in thread_map? */
525 for (j = 0; j < threads->nr; ++j) {
526 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
532 /* if not, generate events for it */
534 __event__synthesize_thread(comm_event, mmap_event,
536 comm_event->comm.pid, 0,
537 process, tool, machine,
538 mmap_data, proc_map_timeout)) {
553 int perf_event__synthesize_threads(struct perf_tool *tool,
554 perf_event__handler_t process,
555 struct machine *machine,
557 unsigned int proc_map_timeout)
560 char proc_path[PATH_MAX];
561 struct dirent *dirent;
562 union perf_event *comm_event, *mmap_event, *fork_event;
565 if (machine__is_default_guest(machine))
568 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
569 if (comm_event == NULL)
572 mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
573 if (mmap_event == NULL)
576 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
577 if (fork_event == NULL)
580 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
581 proc = opendir(proc_path);
586 while ((dirent = readdir(proc)) != NULL) {
588 pid_t pid = strtol(dirent->d_name, &end, 10);
590 if (*end) /* only interested in proper numerical dirents */
593 * We may race with exiting thread, so don't stop just because
594 * one thread couldn't be synthesized.
596 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
597 1, process, tool, machine, mmap_data,
613 struct process_symbol_args {
618 static int find_symbol_cb(void *arg, const char *name, char type,
621 struct process_symbol_args *args = arg;
624 * Must be a function or at least an alias, as in PARISC64, where "_text" is
625 * an 'A' to the same address as "_stext".
627 if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
628 type == 'A') || strcmp(name, args->name))
635 u64 kallsyms__get_function_start(const char *kallsyms_filename,
636 const char *symbol_name)
638 struct process_symbol_args args = { .name = symbol_name, };
640 if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
646 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
647 perf_event__handler_t process,
648 struct machine *machine)
651 const char *mmap_name;
652 char name_buff[PATH_MAX];
653 struct map *map = machine__kernel_map(machine);
656 union perf_event *event;
662 * We should get this from /sys/kernel/sections/.text, but till that is
663 * available use this, and after it is use this as a fallback for older
666 event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
668 pr_debug("Not enough memory synthesizing mmap event "
669 "for kernel modules\n");
673 mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
674 if (machine__is_host(machine)) {
676 * kernel uses PERF_RECORD_MISC_USER for user space maps,
677 * see kernel/perf_event.c __perf_event_mmap
679 event->header.misc = PERF_RECORD_MISC_KERNEL;
681 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
684 kmap = map__kmap(map);
685 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
686 "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
687 size = PERF_ALIGN(size, sizeof(u64));
688 event->mmap.header.type = PERF_RECORD_MMAP;
689 event->mmap.header.size = (sizeof(event->mmap) -
690 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
691 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
692 event->mmap.start = map->start;
693 event->mmap.len = map->end - event->mmap.start;
694 event->mmap.pid = machine->pid;
696 err = process(tool, event, &synth_sample, machine);
702 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
706 if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
711 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
714 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
715 union perf_event *event,
716 struct perf_sample *sample,
717 struct machine *machine)
719 return machine__process_comm_event(machine, event, sample);
722 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
723 union perf_event *event,
724 struct perf_sample *sample,
725 struct machine *machine)
727 return machine__process_lost_event(machine, event, sample);
730 int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
731 union perf_event *event,
732 struct perf_sample *sample __maybe_unused,
733 struct machine *machine)
735 return machine__process_aux_event(machine, event);
738 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
739 union perf_event *event,
740 struct perf_sample *sample __maybe_unused,
741 struct machine *machine)
743 return machine__process_itrace_start_event(machine, event);
746 int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
747 union perf_event *event,
748 struct perf_sample *sample,
749 struct machine *machine)
751 return machine__process_lost_samples_event(machine, event, sample);
754 int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
755 union perf_event *event,
756 struct perf_sample *sample __maybe_unused,
757 struct machine *machine)
759 return machine__process_switch_event(machine, event);
762 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
764 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
765 event->mmap.pid, event->mmap.tid, event->mmap.start,
766 event->mmap.len, event->mmap.pgoff,
767 (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
768 event->mmap.filename);
771 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
773 return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
774 " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
775 event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
776 event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
777 event->mmap2.min, event->mmap2.ino,
778 event->mmap2.ino_generation,
779 (event->mmap2.prot & PROT_READ) ? 'r' : '-',
780 (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
781 (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
782 (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
783 event->mmap2.filename);
786 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
787 union perf_event *event,
788 struct perf_sample *sample,
789 struct machine *machine)
791 return machine__process_mmap_event(machine, event, sample);
794 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
795 union perf_event *event,
796 struct perf_sample *sample,
797 struct machine *machine)
799 return machine__process_mmap2_event(machine, event, sample);
802 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
804 return fprintf(fp, "(%d:%d):(%d:%d)\n",
805 event->fork.pid, event->fork.tid,
806 event->fork.ppid, event->fork.ptid);
809 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
810 union perf_event *event,
811 struct perf_sample *sample,
812 struct machine *machine)
814 return machine__process_fork_event(machine, event, sample);
817 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
818 union perf_event *event,
819 struct perf_sample *sample,
820 struct machine *machine)
822 return machine__process_exit_event(machine, event, sample);
825 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
827 return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
828 event->aux.aux_offset, event->aux.aux_size,
830 event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
831 event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
834 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
836 return fprintf(fp, " pid: %u tid: %u\n",
837 event->itrace_start.pid, event->itrace_start.tid);
840 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
842 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
843 const char *in_out = out ? "OUT" : "IN ";
845 if (event->header.type == PERF_RECORD_SWITCH)
846 return fprintf(fp, " %s\n", in_out);
848 return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
849 in_out, out ? "next" : "prev",
850 event->context_switch.next_prev_pid,
851 event->context_switch.next_prev_tid);
854 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
856 size_t ret = fprintf(fp, "PERF_RECORD_%s",
857 perf_event__name(event->header.type));
859 switch (event->header.type) {
860 case PERF_RECORD_COMM:
861 ret += perf_event__fprintf_comm(event, fp);
863 case PERF_RECORD_FORK:
864 case PERF_RECORD_EXIT:
865 ret += perf_event__fprintf_task(event, fp);
867 case PERF_RECORD_MMAP:
868 ret += perf_event__fprintf_mmap(event, fp);
870 case PERF_RECORD_MMAP2:
871 ret += perf_event__fprintf_mmap2(event, fp);
873 case PERF_RECORD_AUX:
874 ret += perf_event__fprintf_aux(event, fp);
876 case PERF_RECORD_ITRACE_START:
877 ret += perf_event__fprintf_itrace_start(event, fp);
879 case PERF_RECORD_SWITCH:
880 case PERF_RECORD_SWITCH_CPU_WIDE:
881 ret += perf_event__fprintf_switch(event, fp);
884 ret += fprintf(fp, "\n");
890 int perf_event__process(struct perf_tool *tool __maybe_unused,
891 union perf_event *event,
892 struct perf_sample *sample,
893 struct machine *machine)
895 return machine__process_event(machine, event, sample);
898 void thread__find_addr_map(struct thread *thread, u8 cpumode,
899 enum map_type type, u64 addr,
900 struct addr_location *al)
902 struct map_groups *mg = thread->mg;
903 struct machine *machine = mg->machine;
904 bool load_map = false;
906 al->machine = machine;
909 al->cpumode = cpumode;
912 if (machine == NULL) {
917 if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
919 mg = &machine->kmaps;
921 } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
923 } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
925 mg = &machine->kmaps;
927 } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
933 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
934 cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
936 al->filtered |= (1 << HIST_FILTER__GUEST);
937 if ((cpumode == PERF_RECORD_MISC_USER ||
938 cpumode == PERF_RECORD_MISC_KERNEL) &&
940 al->filtered |= (1 << HIST_FILTER__HOST);
945 al->map = map_groups__find(mg, type, al->addr);
946 if (al->map == NULL) {
948 * If this is outside of all known maps, and is a negative
949 * address, try to look it up in the kernel dso, as it might be
950 * a vsyscall or vdso (which executes in user-mode).
952 * XXX This is nasty, we should have a symbol list in the
953 * "[vdso]" dso, but for now lets use the old trick of looking
954 * in the whole kernel symbol list.
956 if (cpumode == PERF_RECORD_MISC_USER && machine &&
957 mg != &machine->kmaps &&
958 machine__kernel_ip(machine, al->addr)) {
959 mg = &machine->kmaps;
965 * Kernel maps might be changed when loading symbols so loading
966 * must be done prior to using kernel maps.
969 map__load(al->map, machine->symbol_filter);
970 al->addr = al->map->map_ip(al->map, al->addr);
974 void thread__find_addr_location(struct thread *thread,
975 u8 cpumode, enum map_type type, u64 addr,
976 struct addr_location *al)
978 thread__find_addr_map(thread, cpumode, type, addr, al);
980 al->sym = map__find_symbol(al->map, al->addr,
981 thread->mg->machine->symbol_filter);
987 * Callers need to drop the reference to al->thread, obtained in
988 * machine__findnew_thread()
990 int perf_event__preprocess_sample(const union perf_event *event,
991 struct machine *machine,
992 struct addr_location *al,
993 struct perf_sample *sample)
995 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
996 struct thread *thread = machine__findnew_thread(machine, sample->pid,
1002 dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1004 * Have we already created the kernel maps for this machine?
1006 * This should have happened earlier, when we processed the kernel MMAP
1007 * events, but for older perf.data files there was no such thing, so do
1010 if (cpumode == PERF_RECORD_MISC_KERNEL &&
1011 machine__kernel_map(machine) == NULL)
1012 machine__create_kernel_maps(machine);
1014 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
1015 dump_printf(" ...... dso: %s\n",
1016 al->map ? al->map->dso->long_name :
1017 al->level == 'H' ? "[hypervisor]" : "<not found>");
1019 if (thread__is_filtered(thread))
1020 al->filtered |= (1 << HIST_FILTER__THREAD);
1023 al->cpu = sample->cpu;
1027 struct perf_env *env = machine->env;
1029 if (env && env->cpu)
1030 al->socket = env->cpu[al->cpu].socket_id;
1034 struct dso *dso = al->map->dso;
1036 if (symbol_conf.dso_list &&
1037 (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1039 (dso->short_name != dso->long_name &&
1040 strlist__has_entry(symbol_conf.dso_list,
1041 dso->long_name))))) {
1042 al->filtered |= (1 << HIST_FILTER__DSO);
1045 al->sym = map__find_symbol(al->map, al->addr,
1046 machine->symbol_filter);
1049 if (symbol_conf.sym_list &&
1050 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
1052 al->filtered |= (1 << HIST_FILTER__SYMBOL);
1059 * The preprocess_sample method will return with reference counts for the
1060 * in it, when done using (and perhaps getting ref counts if needing to
1061 * keep a pointer to one of those entries) it must be paired with
1062 * addr_location__put(), so that the refcounts can be decremented.
1064 void addr_location__put(struct addr_location *al)
1066 thread__zput(al->thread);
1069 bool is_bts_event(struct perf_event_attr *attr)
1071 return attr->type == PERF_TYPE_HARDWARE &&
1072 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1073 attr->sample_period == 1;
1076 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1078 if (attr->type == PERF_TYPE_SOFTWARE &&
1079 (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1080 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1081 attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1084 if (is_bts_event(attr))
1090 void perf_event__preprocess_sample_addr(union perf_event *event,
1091 struct perf_sample *sample,
1092 struct thread *thread,
1093 struct addr_location *al)
1095 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1097 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
1099 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
1102 al->cpu = sample->cpu;
1106 al->sym = map__find_symbol(al->map, al->addr, NULL);