123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107 |
- #include <linux/types.h>
- #include <sys/mman.h>
- #include "event.h"
- #include "debug.h"
- #include "hist.h"
- #include "machine.h"
- #include "sort.h"
- #include "string.h"
- #include "strlist.h"
- #include "thread.h"
- #include "thread_map.h"
- #include "symbol/kallsyms.h"
- static const char *perf_event__names[] = {
- [0] = "TOTAL",
- [PERF_RECORD_MMAP] = "MMAP",
- [PERF_RECORD_MMAP2] = "MMAP2",
- [PERF_RECORD_LOST] = "LOST",
- [PERF_RECORD_COMM] = "COMM",
- [PERF_RECORD_EXIT] = "EXIT",
- [PERF_RECORD_THROTTLE] = "THROTTLE",
- [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
- [PERF_RECORD_FORK] = "FORK",
- [PERF_RECORD_READ] = "READ",
- [PERF_RECORD_SAMPLE] = "SAMPLE",
- [PERF_RECORD_AUX] = "AUX",
- [PERF_RECORD_ITRACE_START] = "ITRACE_START",
- [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES",
- [PERF_RECORD_SWITCH] = "SWITCH",
- [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE",
- [PERF_RECORD_HEADER_ATTR] = "ATTR",
- [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE",
- [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA",
- [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID",
- [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND",
- [PERF_RECORD_ID_INDEX] = "ID_INDEX",
- [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO",
- [PERF_RECORD_AUXTRACE] = "AUXTRACE",
- [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR",
- };
- const char *perf_event__name(unsigned int id)
- {
- if (id >= ARRAY_SIZE(perf_event__names))
- return "INVALID";
- if (!perf_event__names[id])
- return "UNKNOWN";
- return perf_event__names[id];
- }
- static struct perf_sample synth_sample = {
- .pid = -1,
- .tid = -1,
- .time = -1,
- .stream_id = -1,
- .cpu = -1,
- .period = 1,
- };
- /*
- * Assumes that the first 4095 bytes of /proc/pid/stat contains
- * the comm, tgid and ppid.
- */
- static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
- pid_t *tgid, pid_t *ppid)
- {
- char filename[PATH_MAX];
- char bf[4096];
- int fd;
- size_t size = 0;
- ssize_t n;
- char *nl, *name, *tgids, *ppids;
- *tgid = -1;
- *ppid = -1;
- snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
- fd = open(filename, O_RDONLY);
- if (fd < 0) {
- pr_debug("couldn't open %s\n", filename);
- return -1;
- }
- n = read(fd, bf, sizeof(bf) - 1);
- close(fd);
- if (n <= 0) {
- pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
- pid);
- return -1;
- }
- bf[n] = '\0';
- name = strstr(bf, "Name:");
- tgids = strstr(bf, "Tgid:");
- ppids = strstr(bf, "PPid:");
- if (name) {
- name += 5; /* strlen("Name:") */
- while (*name && isspace(*name))
- ++name;
- nl = strchr(name, '\n');
- if (nl)
- *nl = '\0';
- size = strlen(name);
- if (size >= len)
- size = len - 1;
- memcpy(comm, name, size);
- comm[size] = '\0';
- } else {
- pr_debug("Name: string not found for pid %d\n", pid);
- }
- if (tgids) {
- tgids += 5; /* strlen("Tgid:") */
- *tgid = atoi(tgids);
- } else {
- pr_debug("Tgid: string not found for pid %d\n", pid);
- }
- if (ppids) {
- ppids += 5; /* strlen("PPid:") */
- *ppid = atoi(ppids);
- } else {
- pr_debug("PPid: string not found for pid %d\n", pid);
- }
- return 0;
- }
- static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
- struct machine *machine,
- pid_t *tgid, pid_t *ppid)
- {
- size_t size;
- *ppid = -1;
- memset(&event->comm, 0, sizeof(event->comm));
- if (machine__is_host(machine)) {
- if (perf_event__get_comm_ids(pid, event->comm.comm,
- sizeof(event->comm.comm),
- tgid, ppid) != 0) {
- return -1;
- }
- } else {
- *tgid = machine->pid;
- }
- if (*tgid < 0)
- return -1;
- event->comm.pid = *tgid;
- event->comm.header.type = PERF_RECORD_COMM;
- size = strlen(event->comm.comm) + 1;
- size = PERF_ALIGN(size, sizeof(u64));
- memset(event->comm.comm + size, 0, machine->id_hdr_size);
- event->comm.header.size = (sizeof(event->comm) -
- (sizeof(event->comm.comm) - size) +
- machine->id_hdr_size);
- event->comm.tid = pid;
- return 0;
- }
- pid_t perf_event__synthesize_comm(struct perf_tool *tool,
- union perf_event *event, pid_t pid,
- perf_event__handler_t process,
- struct machine *machine)
- {
- pid_t tgid, ppid;
- if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
- return -1;
- if (process(tool, event, &synth_sample, machine) != 0)
- return -1;
- return tgid;
- }
- static int perf_event__synthesize_fork(struct perf_tool *tool,
- union perf_event *event,
- pid_t pid, pid_t tgid, pid_t ppid,
- perf_event__handler_t process,
- struct machine *machine)
- {
- memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
- /*
- * for main thread set parent to ppid from status file. For other
- * threads set parent pid to main thread. ie., assume main thread
- * spawns all threads in a process
- */
- if (tgid == pid) {
- event->fork.ppid = ppid;
- event->fork.ptid = ppid;
- } else {
- event->fork.ppid = tgid;
- event->fork.ptid = tgid;
- }
- event->fork.pid = tgid;
- event->fork.tid = pid;
- event->fork.header.type = PERF_RECORD_FORK;
- event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
- if (process(tool, event, &synth_sample, machine) != 0)
- return -1;
- return 0;
- }
- int perf_event__synthesize_mmap_events(struct perf_tool *tool,
- union perf_event *event,
- pid_t pid, pid_t tgid,
- perf_event__handler_t process,
- struct machine *machine,
- bool mmap_data,
- unsigned int proc_map_timeout)
- {
- char filename[PATH_MAX];
- FILE *fp;
- unsigned long long t;
- bool truncation = false;
- unsigned long long timeout = proc_map_timeout * 1000000ULL;
- int rc = 0;
- if (machine__is_default_guest(machine))
- return 0;
- snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
- machine->root_dir, pid, pid);
- fp = fopen(filename, "r");
- if (fp == NULL) {
- /*
- * We raced with a task exiting - just return:
- */
- pr_debug("couldn't open %s\n", filename);
- return -1;
- }
- event->header.type = PERF_RECORD_MMAP2;
- t = rdclock();
- while (1) {
- char bf[BUFSIZ];
- char prot[5];
- char execname[PATH_MAX];
- char anonstr[] = "//anon";
- unsigned int ino;
- size_t size;
- ssize_t n;
- if (fgets(bf, sizeof(bf), fp) == NULL)
- break;
- if ((rdclock() - t) > timeout) {
- pr_warning("Reading %s time out. "
- "You may want to increase "
- "the time limit by --proc-map-timeout\n",
- filename);
- truncation = true;
- goto out;
- }
- /* ensure null termination since stack will be reused. */
- strcpy(execname, "");
- /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
- n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
- &event->mmap2.start, &event->mmap2.len, prot,
- &event->mmap2.pgoff, &event->mmap2.maj,
- &event->mmap2.min,
- &ino, execname);
- /*
- * Anon maps don't have the execname.
- */
- if (n < 7)
- continue;
- event->mmap2.ino = (u64)ino;
- /*
- * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
- */
- if (machine__is_host(machine))
- event->header.misc = PERF_RECORD_MISC_USER;
- else
- event->header.misc = PERF_RECORD_MISC_GUEST_USER;
- /* map protection and flags bits */
- event->mmap2.prot = 0;
- event->mmap2.flags = 0;
- if (prot[0] == 'r')
- event->mmap2.prot |= PROT_READ;
- if (prot[1] == 'w')
- event->mmap2.prot |= PROT_WRITE;
- if (prot[2] == 'x')
- event->mmap2.prot |= PROT_EXEC;
- if (prot[3] == 's')
- event->mmap2.flags |= MAP_SHARED;
- else
- event->mmap2.flags |= MAP_PRIVATE;
- if (prot[2] != 'x') {
- if (!mmap_data || prot[0] != 'r')
- continue;
- event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
- }
- out:
- if (truncation)
- event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
- if (!strcmp(execname, ""))
- strcpy(execname, anonstr);
- size = strlen(execname) + 1;
- memcpy(event->mmap2.filename, execname, size);
- size = PERF_ALIGN(size, sizeof(u64));
- event->mmap2.len -= event->mmap.start;
- event->mmap2.header.size = (sizeof(event->mmap2) -
- (sizeof(event->mmap2.filename) - size));
- memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
- event->mmap2.header.size += machine->id_hdr_size;
- event->mmap2.pid = tgid;
- event->mmap2.tid = pid;
- if (process(tool, event, &synth_sample, machine) != 0) {
- rc = -1;
- break;
- }
- if (truncation)
- break;
- }
- fclose(fp);
- return rc;
- }
- int perf_event__synthesize_modules(struct perf_tool *tool,
- perf_event__handler_t process,
- struct machine *machine)
- {
- int rc = 0;
- struct map *pos;
- struct map_groups *kmaps = &machine->kmaps;
- struct maps *maps = &kmaps->maps[MAP__FUNCTION];
- union perf_event *event = zalloc((sizeof(event->mmap) +
- machine->id_hdr_size));
- if (event == NULL) {
- pr_debug("Not enough memory synthesizing mmap event "
- "for kernel modules\n");
- return -1;
- }
- event->header.type = PERF_RECORD_MMAP;
- /*
- * kernel uses 0 for user space maps, see kernel/perf_event.c
- * __perf_event_mmap
- */
- if (machine__is_host(machine))
- event->header.misc = PERF_RECORD_MISC_KERNEL;
- else
- event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
- for (pos = maps__first(maps); pos; pos = map__next(pos)) {
- size_t size;
- if (__map__is_kernel(pos))
- continue;
- size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
- event->mmap.header.type = PERF_RECORD_MMAP;
- event->mmap.header.size = (sizeof(event->mmap) -
- (sizeof(event->mmap.filename) - size));
- memset(event->mmap.filename + size, 0, machine->id_hdr_size);
- event->mmap.header.size += machine->id_hdr_size;
- event->mmap.start = pos->start;
- event->mmap.len = pos->end - pos->start;
- event->mmap.pid = machine->pid;
- memcpy(event->mmap.filename, pos->dso->long_name,
- pos->dso->long_name_len + 1);
- if (process(tool, event, &synth_sample, machine) != 0) {
- rc = -1;
- break;
- }
- }
- free(event);
- return rc;
- }
- static int __event__synthesize_thread(union perf_event *comm_event,
- union perf_event *mmap_event,
- union perf_event *fork_event,
- pid_t pid, int full,
- perf_event__handler_t process,
- struct perf_tool *tool,
- struct machine *machine,
- bool mmap_data,
- unsigned int proc_map_timeout)
- {
- char filename[PATH_MAX];
- DIR *tasks;
- struct dirent *dirent;
- pid_t tgid, ppid;
- int rc = 0;
- /* special case: only send one comm event using passed in pid */
- if (!full) {
- tgid = perf_event__synthesize_comm(tool, comm_event, pid,
- process, machine);
- if (tgid == -1)
- return -1;
- return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
- process, machine, mmap_data,
- proc_map_timeout);
- }
- if (machine__is_default_guest(machine))
- return 0;
- snprintf(filename, sizeof(filename), "%s/proc/%d/task",
- machine->root_dir, pid);
- tasks = opendir(filename);
- if (tasks == NULL) {
- pr_debug("couldn't open %s\n", filename);
- return 0;
- }
- while ((dirent = readdir(tasks)) != NULL) {
- char *end;
- pid_t _pid;
- _pid = strtol(dirent->d_name, &end, 10);
- if (*end)
- continue;
- rc = -1;
- if (perf_event__prepare_comm(comm_event, _pid, machine,
- &tgid, &ppid) != 0)
- break;
- if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
- ppid, process, machine) < 0)
- break;
- /*
- * Send the prepared comm event
- */
- if (process(tool, comm_event, &synth_sample, machine) != 0)
- break;
- rc = 0;
- if (_pid == pid) {
- /* process the parent's maps too */
- rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
- process, machine, mmap_data, proc_map_timeout);
- if (rc)
- break;
- }
- }
- closedir(tasks);
- return rc;
- }
- int perf_event__synthesize_thread_map(struct perf_tool *tool,
- struct thread_map *threads,
- perf_event__handler_t process,
- struct machine *machine,
- bool mmap_data,
- unsigned int proc_map_timeout)
- {
- union perf_event *comm_event, *mmap_event, *fork_event;
- int err = -1, thread, j;
- comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
- if (comm_event == NULL)
- goto out;
- mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
- if (mmap_event == NULL)
- goto out_free_comm;
- fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
- if (fork_event == NULL)
- goto out_free_mmap;
- err = 0;
- for (thread = 0; thread < threads->nr; ++thread) {
- if (__event__synthesize_thread(comm_event, mmap_event,
- fork_event,
- thread_map__pid(threads, thread), 0,
- process, tool, machine,
- mmap_data, proc_map_timeout)) {
- err = -1;
- break;
- }
- /*
- * comm.pid is set to thread group id by
- * perf_event__synthesize_comm
- */
- if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
- bool need_leader = true;
- /* is thread group leader in thread_map? */
- for (j = 0; j < threads->nr; ++j) {
- if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
- need_leader = false;
- break;
- }
- }
- /* if not, generate events for it */
- if (need_leader &&
- __event__synthesize_thread(comm_event, mmap_event,
- fork_event,
- comm_event->comm.pid, 0,
- process, tool, machine,
- mmap_data, proc_map_timeout)) {
- err = -1;
- break;
- }
- }
- }
- free(fork_event);
- out_free_mmap:
- free(mmap_event);
- out_free_comm:
- free(comm_event);
- out:
- return err;
- }
- int perf_event__synthesize_threads(struct perf_tool *tool,
- perf_event__handler_t process,
- struct machine *machine,
- bool mmap_data,
- unsigned int proc_map_timeout)
- {
- DIR *proc;
- char proc_path[PATH_MAX];
- struct dirent *dirent;
- union perf_event *comm_event, *mmap_event, *fork_event;
- int err = -1;
- if (machine__is_default_guest(machine))
- return 0;
- comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
- if (comm_event == NULL)
- goto out;
- mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size);
- if (mmap_event == NULL)
- goto out_free_comm;
- fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
- if (fork_event == NULL)
- goto out_free_mmap;
- snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
- proc = opendir(proc_path);
- if (proc == NULL)
- goto out_free_fork;
- while ((dirent = readdir(proc)) != NULL) {
- char *end;
- pid_t pid = strtol(dirent->d_name, &end, 10);
- if (*end) /* only interested in proper numerical dirents */
- continue;
- /*
- * We may race with exiting thread, so don't stop just because
- * one thread couldn't be synthesized.
- */
- __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
- 1, process, tool, machine, mmap_data,
- proc_map_timeout);
- }
- err = 0;
- closedir(proc);
- out_free_fork:
- free(fork_event);
- out_free_mmap:
- free(mmap_event);
- out_free_comm:
- free(comm_event);
- out:
- return err;
- }
- struct process_symbol_args {
- const char *name;
- u64 start;
- };
- static int find_symbol_cb(void *arg, const char *name, char type,
- u64 start)
- {
- struct process_symbol_args *args = arg;
- /*
- * Must be a function or at least an alias, as in PARISC64, where "_text" is
- * an 'A' to the same address as "_stext".
- */
- if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
- type == 'A') || strcmp(name, args->name))
- return 0;
- args->start = start;
- return 1;
- }
- u64 kallsyms__get_function_start(const char *kallsyms_filename,
- const char *symbol_name)
- {
- struct process_symbol_args args = { .name = symbol_name, };
- if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
- return 0;
- return args.start;
- }
- int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
- perf_event__handler_t process,
- struct machine *machine)
- {
- size_t size;
- const char *mmap_name;
- char name_buff[PATH_MAX];
- struct map *map = machine__kernel_map(machine);
- struct kmap *kmap;
- int err;
- union perf_event *event;
- if (map == NULL)
- return -1;
- /*
- * We should get this from /sys/kernel/sections/.text, but till that is
- * available use this, and after it is use this as a fallback for older
- * kernels.
- */
- event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
- if (event == NULL) {
- pr_debug("Not enough memory synthesizing mmap event "
- "for kernel modules\n");
- return -1;
- }
- mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
- if (machine__is_host(machine)) {
- /*
- * kernel uses PERF_RECORD_MISC_USER for user space maps,
- * see kernel/perf_event.c __perf_event_mmap
- */
- event->header.misc = PERF_RECORD_MISC_KERNEL;
- } else {
- event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
- }
- kmap = map__kmap(map);
- size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
- "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
- size = PERF_ALIGN(size, sizeof(u64));
- event->mmap.header.type = PERF_RECORD_MMAP;
- event->mmap.header.size = (sizeof(event->mmap) -
- (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
- event->mmap.pgoff = kmap->ref_reloc_sym->addr;
- event->mmap.start = map->start;
- event->mmap.len = map->end - event->mmap.start;
- event->mmap.pid = machine->pid;
- err = process(tool, event, &synth_sample, machine);
- free(event);
- return err;
- }
- size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
- {
- const char *s;
- if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
- s = " exec";
- else
- s = "";
- return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
- }
- int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample,
- struct machine *machine)
- {
- return machine__process_comm_event(machine, event, sample);
- }
- int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample,
- struct machine *machine)
- {
- return machine__process_lost_event(machine, event, sample);
- }
- int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample __maybe_unused,
- struct machine *machine)
- {
- return machine__process_aux_event(machine, event);
- }
- int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample __maybe_unused,
- struct machine *machine)
- {
- return machine__process_itrace_start_event(machine, event);
- }
- int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample,
- struct machine *machine)
- {
- return machine__process_lost_samples_event(machine, event, sample);
- }
- int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample __maybe_unused,
- struct machine *machine)
- {
- return machine__process_switch_event(machine, event);
- }
- size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
- {
- return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
- event->mmap.pid, event->mmap.tid, event->mmap.start,
- event->mmap.len, event->mmap.pgoff,
- (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
- event->mmap.filename);
- }
- size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
- {
- return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
- " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
- event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
- event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
- event->mmap2.min, event->mmap2.ino,
- event->mmap2.ino_generation,
- (event->mmap2.prot & PROT_READ) ? 'r' : '-',
- (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
- (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
- (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
- event->mmap2.filename);
- }
- int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample,
- struct machine *machine)
- {
- return machine__process_mmap_event(machine, event, sample);
- }
- int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample,
- struct machine *machine)
- {
- return machine__process_mmap2_event(machine, event, sample);
- }
- size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
- {
- return fprintf(fp, "(%d:%d):(%d:%d)\n",
- event->fork.pid, event->fork.tid,
- event->fork.ppid, event->fork.ptid);
- }
- int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample,
- struct machine *machine)
- {
- return machine__process_fork_event(machine, event, sample);
- }
- int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample,
- struct machine *machine)
- {
- return machine__process_exit_event(machine, event, sample);
- }
- size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
- {
- return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n",
- event->aux.aux_offset, event->aux.aux_size,
- event->aux.flags,
- event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
- event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "");
- }
- size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
- {
- return fprintf(fp, " pid: %u tid: %u\n",
- event->itrace_start.pid, event->itrace_start.tid);
- }
- size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
- {
- bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
- const char *in_out = out ? "OUT" : "IN ";
- if (event->header.type == PERF_RECORD_SWITCH)
- return fprintf(fp, " %s\n", in_out);
- return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n",
- in_out, out ? "next" : "prev",
- event->context_switch.next_prev_pid,
- event->context_switch.next_prev_tid);
- }
- size_t perf_event__fprintf(union perf_event *event, FILE *fp)
- {
- size_t ret = fprintf(fp, "PERF_RECORD_%s",
- perf_event__name(event->header.type));
- switch (event->header.type) {
- case PERF_RECORD_COMM:
- ret += perf_event__fprintf_comm(event, fp);
- break;
- case PERF_RECORD_FORK:
- case PERF_RECORD_EXIT:
- ret += perf_event__fprintf_task(event, fp);
- break;
- case PERF_RECORD_MMAP:
- ret += perf_event__fprintf_mmap(event, fp);
- break;
- case PERF_RECORD_MMAP2:
- ret += perf_event__fprintf_mmap2(event, fp);
- break;
- case PERF_RECORD_AUX:
- ret += perf_event__fprintf_aux(event, fp);
- break;
- case PERF_RECORD_ITRACE_START:
- ret += perf_event__fprintf_itrace_start(event, fp);
- break;
- case PERF_RECORD_SWITCH:
- case PERF_RECORD_SWITCH_CPU_WIDE:
- ret += perf_event__fprintf_switch(event, fp);
- break;
- default:
- ret += fprintf(fp, "\n");
- }
- return ret;
- }
- int perf_event__process(struct perf_tool *tool __maybe_unused,
- union perf_event *event,
- struct perf_sample *sample,
- struct machine *machine)
- {
- return machine__process_event(machine, event, sample);
- }
- void thread__find_addr_map(struct thread *thread, u8 cpumode,
- enum map_type type, u64 addr,
- struct addr_location *al)
- {
- struct map_groups *mg = thread->mg;
- struct machine *machine = mg->machine;
- bool load_map = false;
- al->machine = machine;
- al->thread = thread;
- al->addr = addr;
- al->cpumode = cpumode;
- al->filtered = 0;
- if (machine == NULL) {
- al->map = NULL;
- return;
- }
- if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
- al->level = 'k';
- mg = &machine->kmaps;
- load_map = true;
- } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
- al->level = '.';
- } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
- al->level = 'g';
- mg = &machine->kmaps;
- load_map = true;
- } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
- al->level = 'u';
- } else {
- al->level = 'H';
- al->map = NULL;
- if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
- cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
- !perf_guest)
- al->filtered |= (1 << HIST_FILTER__GUEST);
- if ((cpumode == PERF_RECORD_MISC_USER ||
- cpumode == PERF_RECORD_MISC_KERNEL) &&
- !perf_host)
- al->filtered |= (1 << HIST_FILTER__HOST);
- return;
- }
- try_again:
- al->map = map_groups__find(mg, type, al->addr);
- if (al->map == NULL) {
- /*
- * If this is outside of all known maps, and is a negative
- * address, try to look it up in the kernel dso, as it might be
- * a vsyscall or vdso (which executes in user-mode).
- *
- * XXX This is nasty, we should have a symbol list in the
- * "[vdso]" dso, but for now lets use the old trick of looking
- * in the whole kernel symbol list.
- */
- if (cpumode == PERF_RECORD_MISC_USER && machine &&
- mg != &machine->kmaps &&
- machine__kernel_ip(machine, al->addr)) {
- mg = &machine->kmaps;
- load_map = true;
- goto try_again;
- }
- } else {
- /*
- * Kernel maps might be changed when loading symbols so loading
- * must be done prior to using kernel maps.
- */
- if (load_map)
- map__load(al->map, machine->symbol_filter);
- al->addr = al->map->map_ip(al->map, al->addr);
- }
- }
- void thread__find_addr_location(struct thread *thread,
- u8 cpumode, enum map_type type, u64 addr,
- struct addr_location *al)
- {
- thread__find_addr_map(thread, cpumode, type, addr, al);
- if (al->map != NULL)
- al->sym = map__find_symbol(al->map, al->addr,
- thread->mg->machine->symbol_filter);
- else
- al->sym = NULL;
- }
- /*
- * Callers need to drop the reference to al->thread, obtained in
- * machine__findnew_thread()
- */
- int perf_event__preprocess_sample(const union perf_event *event,
- struct machine *machine,
- struct addr_location *al,
- struct perf_sample *sample)
- {
- u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
- struct thread *thread = machine__findnew_thread(machine, sample->pid,
- sample->tid);
- if (thread == NULL)
- return -1;
- dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
- /*
- * Have we already created the kernel maps for this machine?
- *
- * This should have happened earlier, when we processed the kernel MMAP
- * events, but for older perf.data files there was no such thing, so do
- * it now.
- */
- if (cpumode == PERF_RECORD_MISC_KERNEL &&
- machine__kernel_map(machine) == NULL)
- machine__create_kernel_maps(machine);
- thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al);
- dump_printf(" ...... dso: %s\n",
- al->map ? al->map->dso->long_name :
- al->level == 'H' ? "[hypervisor]" : "<not found>");
- if (thread__is_filtered(thread))
- al->filtered |= (1 << HIST_FILTER__THREAD);
- al->sym = NULL;
- al->cpu = sample->cpu;
- al->socket = -1;
- if (al->cpu >= 0) {
- struct perf_env *env = machine->env;
- if (env && env->cpu)
- al->socket = env->cpu[al->cpu].socket_id;
- }
- if (al->map) {
- struct dso *dso = al->map->dso;
- if (symbol_conf.dso_list &&
- (!dso || !(strlist__has_entry(symbol_conf.dso_list,
- dso->short_name) ||
- (dso->short_name != dso->long_name &&
- strlist__has_entry(symbol_conf.dso_list,
- dso->long_name))))) {
- al->filtered |= (1 << HIST_FILTER__DSO);
- }
- al->sym = map__find_symbol(al->map, al->addr,
- machine->symbol_filter);
- }
- if (symbol_conf.sym_list &&
- (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
- al->sym->name))) {
- al->filtered |= (1 << HIST_FILTER__SYMBOL);
- }
- return 0;
- }
- /*
- * The preprocess_sample method will return with reference counts for the
- * in it, when done using (and perhaps getting ref counts if needing to
- * keep a pointer to one of those entries) it must be paired with
- * addr_location__put(), so that the refcounts can be decremented.
- */
- void addr_location__put(struct addr_location *al)
- {
- thread__zput(al->thread);
- }
- bool is_bts_event(struct perf_event_attr *attr)
- {
- return attr->type == PERF_TYPE_HARDWARE &&
- (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
- attr->sample_period == 1;
- }
- bool sample_addr_correlates_sym(struct perf_event_attr *attr)
- {
- if (attr->type == PERF_TYPE_SOFTWARE &&
- (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
- attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
- attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
- return true;
- if (is_bts_event(attr))
- return true;
- return false;
- }
- void perf_event__preprocess_sample_addr(union perf_event *event,
- struct perf_sample *sample,
- struct thread *thread,
- struct addr_location *al)
- {
- u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
- thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al);
- if (!al->map)
- thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
- sample->addr, al);
- al->cpu = sample->cpu;
- al->sym = NULL;
- if (al->map)
- al->sym = map__find_symbol(al->map, al->addr, NULL);
- }
|