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