linux/tools/perf/builtin-trace.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * builtin-trace.c
   4 *
   5 * Builtin 'trace' command:
   6 *
   7 * Display a continuously updated trace of any workload, CPU, specific PID,
   8 * system wide, etc.  Default format is loosely strace like, but any other
   9 * event may be specified using --event.
  10 *
  11 * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  12 *
  13 * Initially based on the 'trace' prototype by Thomas Gleixner:
  14 *
  15 * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
  16 */
  17
  18#include <traceevent/event-parse.h>
  19#include <api/fs/tracing_path.h>
  20#include <bpf/bpf.h>
  21#include "util/bpf_map.h"
  22#include "builtin.h"
  23#include "util/cgroup.h"
  24#include "util/color.h"
  25#include "util/config.h"
  26#include "util/debug.h"
  27#include "util/env.h"
  28#include "util/event.h"
  29#include "util/evlist.h"
  30#include <subcmd/exec-cmd.h>
  31#include "util/machine.h"
  32#include "util/map.h"
  33#include "util/symbol.h"
  34#include "util/path.h"
  35#include "util/session.h"
  36#include "util/thread.h"
  37#include <subcmd/parse-options.h>
  38#include "util/strlist.h"
  39#include "util/intlist.h"
  40#include "util/thread_map.h"
  41#include "util/stat.h"
  42#include "trace/beauty/beauty.h"
  43#include "trace-event.h"
  44#include "util/parse-events.h"
  45#include "util/bpf-loader.h"
  46#include "callchain.h"
  47#include "print_binary.h"
  48#include "string2.h"
  49#include "syscalltbl.h"
  50#include "rb_resort.h"
  51
  52#include <errno.h>
  53#include <inttypes.h>
  54#include <poll.h>
  55#include <signal.h>
  56#include <stdlib.h>
  57#include <string.h>
  58#include <linux/err.h>
  59#include <linux/filter.h>
  60#include <linux/kernel.h>
  61#include <linux/random.h>
  62#include <linux/stringify.h>
  63#include <linux/time64.h>
  64#include <fcntl.h>
  65#include <sys/sysmacros.h>
  66
  67#include "sane_ctype.h"
  68
  69#ifndef O_CLOEXEC
  70# define O_CLOEXEC              02000000
  71#endif
  72
  73#ifndef F_LINUX_SPECIFIC_BASE
  74# define F_LINUX_SPECIFIC_BASE  1024
  75#endif
  76
  77struct trace {
  78        struct perf_tool        tool;
  79        struct syscalltbl       *sctbl;
  80        struct {
  81                int             max;
  82                struct syscall  *table;
  83                struct bpf_map  *map;
  84                struct {
  85                        struct perf_evsel *sys_enter,
  86                                          *sys_exit,
  87                                          *augmented;
  88                }               events;
  89        } syscalls;
  90        struct {
  91                struct bpf_map *map;
  92        } dump;
  93        struct record_opts      opts;
  94        struct perf_evlist      *evlist;
  95        struct machine          *host;
  96        struct thread           *current;
  97        struct cgroup           *cgroup;
  98        u64                     base_time;
  99        FILE                    *output;
 100        unsigned long           nr_events;
 101        unsigned long           nr_events_printed;
 102        unsigned long           max_events;
 103        struct strlist          *ev_qualifier;
 104        struct {
 105                size_t          nr;
 106                int             *entries;
 107        }                       ev_qualifier_ids;
 108        struct {
 109                size_t          nr;
 110                pid_t           *entries;
 111                struct bpf_map  *map;
 112        }                       filter_pids;
 113        double                  duration_filter;
 114        double                  runtime_ms;
 115        struct {
 116                u64             vfs_getname,
 117                                proc_getname;
 118        } stats;
 119        unsigned int            max_stack;
 120        unsigned int            min_stack;
 121        int                     raw_augmented_syscalls_args_size;
 122        bool                    raw_augmented_syscalls;
 123        bool                    sort_events;
 124        bool                    not_ev_qualifier;
 125        bool                    live;
 126        bool                    full_time;
 127        bool                    sched;
 128        bool                    multiple_threads;
 129        bool                    summary;
 130        bool                    summary_only;
 131        bool                    failure_only;
 132        bool                    show_comm;
 133        bool                    print_sample;
 134        bool                    show_tool_stats;
 135        bool                    trace_syscalls;
 136        bool                    kernel_syscallchains;
 137        s16                     args_alignment;
 138        bool                    show_tstamp;
 139        bool                    show_duration;
 140        bool                    show_zeros;
 141        bool                    show_arg_names;
 142        bool                    show_string_prefix;
 143        bool                    force;
 144        bool                    vfs_getname;
 145        int                     trace_pgfaults;
 146        struct {
 147                struct ordered_events   data;
 148                u64                     last;
 149        } oe;
 150};
 151
 152struct tp_field {
 153        int offset;
 154        union {
 155                u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
 156                void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
 157        };
 158};
 159
 160#define TP_UINT_FIELD(bits) \
 161static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
 162{ \
 163        u##bits value; \
 164        memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
 165        return value;  \
 166}
 167
 168TP_UINT_FIELD(8);
 169TP_UINT_FIELD(16);
 170TP_UINT_FIELD(32);
 171TP_UINT_FIELD(64);
 172
 173#define TP_UINT_FIELD__SWAPPED(bits) \
 174static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
 175{ \
 176        u##bits value; \
 177        memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
 178        return bswap_##bits(value);\
 179}
 180
 181TP_UINT_FIELD__SWAPPED(16);
 182TP_UINT_FIELD__SWAPPED(32);
 183TP_UINT_FIELD__SWAPPED(64);
 184
 185static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap)
 186{
 187        field->offset = offset;
 188
 189        switch (size) {
 190        case 1:
 191                field->integer = tp_field__u8;
 192                break;
 193        case 2:
 194                field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
 195                break;
 196        case 4:
 197                field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
 198                break;
 199        case 8:
 200                field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
 201                break;
 202        default:
 203                return -1;
 204        }
 205
 206        return 0;
 207}
 208
 209static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap)
 210{
 211        return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
 212}
 213
 214static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
 215{
 216        return sample->raw_data + field->offset;
 217}
 218
 219static int __tp_field__init_ptr(struct tp_field *field, int offset)
 220{
 221        field->offset = offset;
 222        field->pointer = tp_field__ptr;
 223        return 0;
 224}
 225
 226static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
 227{
 228        return __tp_field__init_ptr(field, format_field->offset);
 229}
 230
 231struct syscall_tp {
 232        struct tp_field id;
 233        union {
 234                struct tp_field args, ret;
 235        };
 236};
 237
 238static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
 239                                          struct tp_field *field,
 240                                          const char *name)
 241{
 242        struct tep_format_field *format_field = perf_evsel__field(evsel, name);
 243
 244        if (format_field == NULL)
 245                return -1;
 246
 247        return tp_field__init_uint(field, format_field, evsel->needs_swap);
 248}
 249
 250#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
 251        ({ struct syscall_tp *sc = evsel->priv;\
 252           perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
 253
 254static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
 255                                         struct tp_field *field,
 256                                         const char *name)
 257{
 258        struct tep_format_field *format_field = perf_evsel__field(evsel, name);
 259
 260        if (format_field == NULL)
 261                return -1;
 262
 263        return tp_field__init_ptr(field, format_field);
 264}
 265
 266#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
 267        ({ struct syscall_tp *sc = evsel->priv;\
 268           perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
 269
 270static void perf_evsel__delete_priv(struct perf_evsel *evsel)
 271{
 272        zfree(&evsel->priv);
 273        perf_evsel__delete(evsel);
 274}
 275
 276static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel)
 277{
 278        struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
 279
 280        if (evsel->priv != NULL) {
 281                if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
 282                    perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
 283                        goto out_delete;
 284                return 0;
 285        }
 286
 287        return -ENOMEM;
 288out_delete:
 289        zfree(&evsel->priv);
 290        return -ENOENT;
 291}
 292
 293static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel, struct perf_evsel *tp)
 294{
 295        struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
 296
 297        if (evsel->priv != NULL) {
 298                struct tep_format_field *syscall_id = perf_evsel__field(tp, "id");
 299                if (syscall_id == NULL)
 300                        syscall_id = perf_evsel__field(tp, "__syscall_nr");
 301                if (syscall_id == NULL)
 302                        goto out_delete;
 303                if (__tp_field__init_uint(&sc->id, syscall_id->size, syscall_id->offset, evsel->needs_swap))
 304                        goto out_delete;
 305
 306                return 0;
 307        }
 308
 309        return -ENOMEM;
 310out_delete:
 311        zfree(&evsel->priv);
 312        return -EINVAL;
 313}
 314
 315static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel)
 316{
 317        struct syscall_tp *sc = evsel->priv;
 318
 319        return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
 320}
 321
 322static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel)
 323{
 324        struct syscall_tp *sc = evsel->priv;
 325
 326        return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
 327}
 328
 329static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
 330{
 331        evsel->priv = malloc(sizeof(struct syscall_tp));
 332        if (evsel->priv != NULL) {
 333                if (perf_evsel__init_sc_tp_uint_field(evsel, id))
 334                        goto out_delete;
 335
 336                evsel->handler = handler;
 337                return 0;
 338        }
 339
 340        return -ENOMEM;
 341
 342out_delete:
 343        zfree(&evsel->priv);
 344        return -ENOENT;
 345}
 346
 347static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
 348{
 349        struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
 350
 351        /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
 352        if (IS_ERR(evsel))
 353                evsel = perf_evsel__newtp("syscalls", direction);
 354
 355        if (IS_ERR(evsel))
 356                return NULL;
 357
 358        if (perf_evsel__init_raw_syscall_tp(evsel, handler))
 359                goto out_delete;
 360
 361        return evsel;
 362
 363out_delete:
 364        perf_evsel__delete_priv(evsel);
 365        return NULL;
 366}
 367
 368#define perf_evsel__sc_tp_uint(evsel, name, sample) \
 369        ({ struct syscall_tp *fields = evsel->priv; \
 370           fields->name.integer(&fields->name, sample); })
 371
 372#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
 373        ({ struct syscall_tp *fields = evsel->priv; \
 374           fields->name.pointer(&fields->name, sample); })
 375
 376size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
 377{
 378        int idx = val - sa->offset;
 379
 380        if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
 381                size_t printed = scnprintf(bf, size, intfmt, val);
 382                if (show_prefix)
 383                        printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
 384                return printed;
 385        }
 386
 387        return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
 388}
 389
 390static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
 391                                                const char *intfmt,
 392                                                struct syscall_arg *arg)
 393{
 394        return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val);
 395}
 396
 397static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
 398                                              struct syscall_arg *arg)
 399{
 400        return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
 401}
 402
 403#define SCA_STRARRAY syscall_arg__scnprintf_strarray
 404
 405size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
 406{
 407        size_t printed;
 408        int i;
 409
 410        for (i = 0; i < sas->nr_entries; ++i) {
 411                struct strarray *sa = sas->entries[i];
 412                int idx = val - sa->offset;
 413
 414                if (idx >= 0 && idx < sa->nr_entries) {
 415                        if (sa->entries[idx] == NULL)
 416                                break;
 417                        return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
 418                }
 419        }
 420
 421        printed = scnprintf(bf, size, intfmt, val);
 422        if (show_prefix)
 423                printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix);
 424        return printed;
 425}
 426
 427size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
 428                                        struct syscall_arg *arg)
 429{
 430        return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val);
 431}
 432
 433#ifndef AT_FDCWD
 434#define AT_FDCWD        -100
 435#endif
 436
 437static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
 438                                           struct syscall_arg *arg)
 439{
 440        int fd = arg->val;
 441        const char *prefix = "AT_FD";
 442
 443        if (fd == AT_FDCWD)
 444                return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD");
 445
 446        return syscall_arg__scnprintf_fd(bf, size, arg);
 447}
 448
 449#define SCA_FDAT syscall_arg__scnprintf_fd_at
 450
 451static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
 452                                              struct syscall_arg *arg);
 453
 454#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
 455
 456size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
 457{
 458        return scnprintf(bf, size, "%#lx", arg->val);
 459}
 460
 461size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg)
 462{
 463        if (arg->val == 0)
 464                return scnprintf(bf, size, "NULL");
 465        return syscall_arg__scnprintf_hex(bf, size, arg);
 466}
 467
 468size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
 469{
 470        return scnprintf(bf, size, "%d", arg->val);
 471}
 472
 473size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
 474{
 475        return scnprintf(bf, size, "%ld", arg->val);
 476}
 477
 478static const char *bpf_cmd[] = {
 479        "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
 480        "MAP_GET_NEXT_KEY", "PROG_LOAD",
 481};
 482static DEFINE_STRARRAY(bpf_cmd, "BPF_");
 483
 484static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
 485static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1);
 486
 487static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
 488static DEFINE_STRARRAY(itimers, "ITIMER_");
 489
 490static const char *keyctl_options[] = {
 491        "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
 492        "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
 493        "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
 494        "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
 495        "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
 496};
 497static DEFINE_STRARRAY(keyctl_options, "KEYCTL_");
 498
 499static const char *whences[] = { "SET", "CUR", "END",
 500#ifdef SEEK_DATA
 501"DATA",
 502#endif
 503#ifdef SEEK_HOLE
 504"HOLE",
 505#endif
 506};
 507static DEFINE_STRARRAY(whences, "SEEK_");
 508
 509static const char *fcntl_cmds[] = {
 510        "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
 511        "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
 512        "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
 513        "GETOWNER_UIDS",
 514};
 515static DEFINE_STRARRAY(fcntl_cmds, "F_");
 516
 517static const char *fcntl_linux_specific_cmds[] = {
 518        "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
 519        "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
 520        "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
 521};
 522
 523static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE);
 524
 525static struct strarray *fcntl_cmds_arrays[] = {
 526        &strarray__fcntl_cmds,
 527        &strarray__fcntl_linux_specific_cmds,
 528};
 529
 530static DEFINE_STRARRAYS(fcntl_cmds_arrays);
 531
 532static const char *rlimit_resources[] = {
 533        "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
 534        "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
 535        "RTTIME",
 536};
 537static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_");
 538
 539static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
 540static DEFINE_STRARRAY(sighow, "SIG_");
 541
 542static const char *clockid[] = {
 543        "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
 544        "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
 545        "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
 546};
 547static DEFINE_STRARRAY(clockid, "CLOCK_");
 548
 549static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
 550                                                 struct syscall_arg *arg)
 551{
 552        bool show_prefix = arg->show_string_prefix;
 553        const char *suffix = "_OK";
 554        size_t printed = 0;
 555        int mode = arg->val;
 556
 557        if (mode == F_OK) /* 0 */
 558                return scnprintf(bf, size, "F%s", show_prefix ? suffix : "");
 559#define P_MODE(n) \
 560        if (mode & n##_OK) { \
 561                printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
 562                mode &= ~n##_OK; \
 563        }
 564
 565        P_MODE(R);
 566        P_MODE(W);
 567        P_MODE(X);
 568#undef P_MODE
 569
 570        if (mode)
 571                printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
 572
 573        return printed;
 574}
 575
 576#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
 577
 578static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
 579                                              struct syscall_arg *arg);
 580
 581#define SCA_FILENAME syscall_arg__scnprintf_filename
 582
 583static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
 584                                                struct syscall_arg *arg)
 585{
 586        bool show_prefix = arg->show_string_prefix;
 587        const char *prefix = "O_";
 588        int printed = 0, flags = arg->val;
 589
 590#define P_FLAG(n) \
 591        if (flags & O_##n) { \
 592                printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
 593                flags &= ~O_##n; \
 594        }
 595
 596        P_FLAG(CLOEXEC);
 597        P_FLAG(NONBLOCK);
 598#undef P_FLAG
 599
 600        if (flags)
 601                printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
 602
 603        return printed;
 604}
 605
 606#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
 607
 608#ifndef GRND_NONBLOCK
 609#define GRND_NONBLOCK   0x0001
 610#endif
 611#ifndef GRND_RANDOM
 612#define GRND_RANDOM     0x0002
 613#endif
 614
 615static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
 616                                                   struct syscall_arg *arg)
 617{
 618        bool show_prefix = arg->show_string_prefix;
 619        const char *prefix = "GRND_";
 620        int printed = 0, flags = arg->val;
 621
 622#define P_FLAG(n) \
 623        if (flags & GRND_##n) { \
 624                printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
 625                flags &= ~GRND_##n; \
 626        }
 627
 628        P_FLAG(RANDOM);
 629        P_FLAG(NONBLOCK);
 630#undef P_FLAG
 631
 632        if (flags)
 633                printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
 634
 635        return printed;
 636}
 637
 638#define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
 639
 640#define STRARRAY(name, array) \
 641          { .scnprintf  = SCA_STRARRAY, \
 642            .parm       = &strarray__##array, }
 643
 644#include "trace/beauty/arch_errno_names.c"
 645#include "trace/beauty/eventfd.c"
 646#include "trace/beauty/futex_op.c"
 647#include "trace/beauty/futex_val3.c"
 648#include "trace/beauty/mmap.c"
 649#include "trace/beauty/mode_t.c"
 650#include "trace/beauty/msg_flags.c"
 651#include "trace/beauty/open_flags.c"
 652#include "trace/beauty/perf_event_open.c"
 653#include "trace/beauty/pid.c"
 654#include "trace/beauty/sched_policy.c"
 655#include "trace/beauty/seccomp.c"
 656#include "trace/beauty/signum.c"
 657#include "trace/beauty/socket_type.c"
 658#include "trace/beauty/waitid_options.c"
 659
 660struct syscall_arg_fmt {
 661        size_t     (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
 662        unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
 663        void       *parm;
 664        const char *name;
 665        bool       show_zero;
 666};
 667
 668static struct syscall_fmt {
 669        const char *name;
 670        const char *alias;
 671        struct syscall_arg_fmt arg[6];
 672        u8         nr_args;
 673        bool       errpid;
 674        bool       timeout;
 675        bool       hexret;
 676} syscall_fmts[] = {
 677        { .name     = "access",
 678          .arg = { [1] = { .scnprintf = SCA_ACCMODE,  /* mode */ }, }, },
 679        { .name     = "arch_prctl",
 680          .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ },
 681                   [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, },
 682        { .name     = "bind",
 683          .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, },
 684        { .name     = "bpf",
 685          .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
 686        { .name     = "brk",        .hexret = true,
 687          .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, },
 688        { .name     = "clock_gettime",
 689          .arg = { [0] = STRARRAY(clk_id, clockid), }, },
 690        { .name     = "clone",      .errpid = true, .nr_args = 5,
 691          .arg = { [0] = { .name = "flags",         .scnprintf = SCA_CLONE_FLAGS, },
 692                   [1] = { .name = "child_stack",   .scnprintf = SCA_HEX, },
 693                   [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
 694                   [3] = { .name = "child_tidptr",  .scnprintf = SCA_HEX, },
 695                   [4] = { .name = "tls",           .scnprintf = SCA_HEX, }, }, },
 696        { .name     = "close",
 697          .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
 698        { .name     = "connect",
 699          .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, },
 700        { .name     = "epoll_ctl",
 701          .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
 702        { .name     = "eventfd2",
 703          .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
 704        { .name     = "fchmodat",
 705          .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
 706        { .name     = "fchownat",
 707          .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
 708        { .name     = "fcntl",
 709          .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
 710                           .parm      = &strarrays__fcntl_cmds_arrays,
 711                           .show_zero = true, },
 712                   [2] = { .scnprintf =  SCA_FCNTL_ARG, /* arg */ }, }, },
 713        { .name     = "flock",
 714          .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
 715        { .name     = "fstat", .alias = "newfstat", },
 716        { .name     = "fstatat", .alias = "newfstatat", },
 717        { .name     = "futex",
 718          .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
 719                   [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
 720        { .name     = "futimesat",
 721          .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
 722        { .name     = "getitimer",
 723          .arg = { [0] = STRARRAY(which, itimers), }, },
 724        { .name     = "getpid",     .errpid = true, },
 725        { .name     = "getpgid",    .errpid = true, },
 726        { .name     = "getppid",    .errpid = true, },
 727        { .name     = "getrandom",
 728          .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
 729        { .name     = "getrlimit",
 730          .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
 731        { .name     = "gettid",     .errpid = true, },
 732        { .name     = "ioctl",
 733          .arg = {
 734#if defined(__i386__) || defined(__x86_64__)
 735/*
 736 * FIXME: Make this available to all arches.
 737 */
 738                   [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
 739                   [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
 740#else
 741                   [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
 742#endif
 743        { .name     = "kcmp",       .nr_args = 5,
 744          .arg = { [0] = { .name = "pid1",      .scnprintf = SCA_PID, },
 745                   [1] = { .name = "pid2",      .scnprintf = SCA_PID, },
 746                   [2] = { .name = "type",      .scnprintf = SCA_KCMP_TYPE, },
 747                   [3] = { .name = "idx1",      .scnprintf = SCA_KCMP_IDX, },
 748                   [4] = { .name = "idx2",      .scnprintf = SCA_KCMP_IDX, }, }, },
 749        { .name     = "keyctl",
 750          .arg = { [0] = STRARRAY(option, keyctl_options), }, },
 751        { .name     = "kill",
 752          .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
 753        { .name     = "linkat",
 754          .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
 755        { .name     = "lseek",
 756          .arg = { [2] = STRARRAY(whence, whences), }, },
 757        { .name     = "lstat", .alias = "newlstat", },
 758        { .name     = "madvise",
 759          .arg = { [0] = { .scnprintf = SCA_HEX,      /* start */ },
 760                   [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
 761        { .name     = "mkdirat",
 762          .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
 763        { .name     = "mknodat",
 764          .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
 765        { .name     = "mmap",       .hexret = true,
 766/* The standard mmap maps to old_mmap on s390x */
 767#if defined(__s390x__)
 768        .alias = "old_mmap",
 769#endif
 770          .arg = { [2] = { .scnprintf = SCA_MMAP_PROT,  /* prot */ },
 771                   [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ },
 772                   [5] = { .scnprintf = SCA_HEX,        /* offset */ }, }, },
 773        { .name     = "mount",
 774          .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
 775                   [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
 776                           .mask_val  = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
 777        { .name     = "mprotect",
 778          .arg = { [0] = { .scnprintf = SCA_HEX,        /* start */ },
 779                   [2] = { .scnprintf = SCA_MMAP_PROT,  /* prot */ }, }, },
 780        { .name     = "mq_unlink",
 781          .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
 782        { .name     = "mremap",     .hexret = true,
 783          .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, },
 784        { .name     = "name_to_handle_at",
 785          .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
 786        { .name     = "newfstatat",
 787          .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
 788        { .name     = "open",
 789          .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
 790        { .name     = "open_by_handle_at",
 791          .arg = { [0] = { .scnprintf = SCA_FDAT,       /* dfd */ },
 792                   [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
 793        { .name     = "openat",
 794          .arg = { [0] = { .scnprintf = SCA_FDAT,       /* dfd */ },
 795                   [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
 796        { .name     = "perf_event_open",
 797          .arg = { [2] = { .scnprintf = SCA_INT,        /* cpu */ },
 798                   [3] = { .scnprintf = SCA_FD,         /* group_fd */ },
 799                   [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
 800        { .name     = "pipe2",
 801          .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
 802        { .name     = "pkey_alloc",
 803          .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS,   /* access_rights */ }, }, },
 804        { .name     = "pkey_free",
 805          .arg = { [0] = { .scnprintf = SCA_INT,        /* key */ }, }, },
 806        { .name     = "pkey_mprotect",
 807          .arg = { [0] = { .scnprintf = SCA_HEX,        /* start */ },
 808                   [2] = { .scnprintf = SCA_MMAP_PROT,  /* prot */ },
 809                   [3] = { .scnprintf = SCA_INT,        /* pkey */ }, }, },
 810        { .name     = "poll", .timeout = true, },
 811        { .name     = "ppoll", .timeout = true, },
 812        { .name     = "prctl",
 813          .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
 814                   [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
 815                   [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
 816        { .name     = "pread", .alias = "pread64", },
 817        { .name     = "preadv", .alias = "pread", },
 818        { .name     = "prlimit64",
 819          .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
 820        { .name     = "pwrite", .alias = "pwrite64", },
 821        { .name     = "readlinkat",
 822          .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
 823        { .name     = "recvfrom",
 824          .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
 825        { .name     = "recvmmsg",
 826          .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
 827        { .name     = "recvmsg",
 828          .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
 829        { .name     = "renameat",
 830          .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
 831                   [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
 832        { .name     = "renameat2",
 833          .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
 834                   [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
 835                   [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
 836        { .name     = "rt_sigaction",
 837          .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
 838        { .name     = "rt_sigprocmask",
 839          .arg = { [0] = STRARRAY(how, sighow), }, },
 840        { .name     = "rt_sigqueueinfo",
 841          .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
 842        { .name     = "rt_tgsigqueueinfo",
 843          .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
 844        { .name     = "sched_setscheduler",
 845          .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
 846        { .name     = "seccomp",
 847          .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP,    /* op */ },
 848                   [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
 849        { .name     = "select", .timeout = true, },
 850        { .name     = "sendmmsg",
 851          .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
 852        { .name     = "sendmsg",
 853          .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
 854        { .name     = "sendto",
 855          .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
 856                   [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
 857        { .name     = "set_tid_address", .errpid = true, },
 858        { .name     = "setitimer",
 859          .arg = { [0] = STRARRAY(which, itimers), }, },
 860        { .name     = "setrlimit",
 861          .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
 862        { .name     = "socket",
 863          .arg = { [0] = STRARRAY(family, socket_families),
 864                   [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
 865                   [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
 866        { .name     = "socketpair",
 867          .arg = { [0] = STRARRAY(family, socket_families),
 868                   [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
 869                   [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
 870        { .name     = "stat", .alias = "newstat", },
 871        { .name     = "statx",
 872          .arg = { [0] = { .scnprintf = SCA_FDAT,        /* fdat */ },
 873                   [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
 874                   [3] = { .scnprintf = SCA_STATX_MASK,  /* mask */ }, }, },
 875        { .name     = "swapoff",
 876          .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
 877        { .name     = "swapon",
 878          .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
 879        { .name     = "symlinkat",
 880          .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
 881        { .name     = "tgkill",
 882          .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
 883        { .name     = "tkill",
 884          .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
 885        { .name     = "umount2", .alias = "umount",
 886          .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
 887        { .name     = "uname", .alias = "newuname", },
 888        { .name     = "unlinkat",
 889          .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
 890        { .name     = "utimensat",
 891          .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
 892        { .name     = "wait4",      .errpid = true,
 893          .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
 894        { .name     = "waitid",     .errpid = true,
 895          .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
 896};
 897
 898static int syscall_fmt__cmp(const void *name, const void *fmtp)
 899{
 900        const struct syscall_fmt *fmt = fmtp;
 901        return strcmp(name, fmt->name);
 902}
 903
 904static struct syscall_fmt *syscall_fmt__find(const char *name)
 905{
 906        const int nmemb = ARRAY_SIZE(syscall_fmts);
 907        return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
 908}
 909
 910static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
 911{
 912        int i, nmemb = ARRAY_SIZE(syscall_fmts);
 913
 914        for (i = 0; i < nmemb; ++i) {
 915                if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
 916                        return &syscall_fmts[i];
 917        }
 918
 919        return NULL;
 920}
 921
 922/*
 923 * is_exit: is this "exit" or "exit_group"?
 924 * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter.
 925 * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc.
 926 */
 927struct syscall {
 928        struct tep_event    *tp_format;
 929        int                 nr_args;
 930        int                 args_size;
 931        bool                is_exit;
 932        bool                is_open;
 933        struct tep_format_field *args;
 934        const char          *name;
 935        struct syscall_fmt  *fmt;
 936        struct syscall_arg_fmt *arg_fmt;
 937};
 938
 939struct bpf_map_syscall_entry {
 940        bool    enabled;
 941};
 942
 943/*
 944 * We need to have this 'calculated' boolean because in some cases we really
 945 * don't know what is the duration of a syscall, for instance, when we start
 946 * a session and some threads are waiting for a syscall to finish, say 'poll',
 947 * in which case all we can do is to print "( ? ) for duration and for the
 948 * start timestamp.
 949 */
 950static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
 951{
 952        double duration = (double)t / NSEC_PER_MSEC;
 953        size_t printed = fprintf(fp, "(");
 954
 955        if (!calculated)
 956                printed += fprintf(fp, "         ");
 957        else if (duration >= 1.0)
 958                printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
 959        else if (duration >= 0.01)
 960                printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
 961        else
 962                printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
 963        return printed + fprintf(fp, "): ");
 964}
 965
 966/**
 967 * filename.ptr: The filename char pointer that will be vfs_getname'd
 968 * filename.entry_str_pos: Where to insert the string translated from
 969 *                         filename.ptr by the vfs_getname tracepoint/kprobe.
 970 * ret_scnprintf: syscall args may set this to a different syscall return
 971 *                formatter, for instance, fcntl may return fds, file flags, etc.
 972 */
 973struct thread_trace {
 974        u64               entry_time;
 975        bool              entry_pending;
 976        unsigned long     nr_events;
 977        unsigned long     pfmaj, pfmin;
 978        char              *entry_str;
 979        double            runtime_ms;
 980        size_t            (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
 981        struct {
 982                unsigned long ptr;
 983                short int     entry_str_pos;
 984                bool          pending_open;
 985                unsigned int  namelen;
 986                char          *name;
 987        } filename;
 988        struct {
 989                int           max;
 990                struct file   *table;
 991        } files;
 992
 993        struct intlist *syscall_stats;
 994};
 995
 996static struct thread_trace *thread_trace__new(void)
 997{
 998        struct thread_trace *ttrace =  zalloc(sizeof(struct thread_trace));
 999
1000        if (ttrace)
1001                ttrace->files.max = -1;
1002
1003        ttrace->syscall_stats = intlist__new(NULL);
1004
1005        return ttrace;
1006}
1007
1008static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
1009{
1010        struct thread_trace *ttrace;
1011
1012        if (thread == NULL)
1013                goto fail;
1014
1015        if (thread__priv(thread) == NULL)
1016                thread__set_priv(thread, thread_trace__new());
1017
1018        if (thread__priv(thread) == NULL)
1019                goto fail;
1020
1021        ttrace = thread__priv(thread);
1022        ++ttrace->nr_events;
1023
1024        return ttrace;
1025fail:
1026        color_fprintf(fp, PERF_COLOR_RED,
1027                      "WARNING: not enough memory, dropping samples!\n");
1028        return NULL;
1029}
1030
1031
1032void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
1033                                    size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
1034{
1035        struct thread_trace *ttrace = thread__priv(arg->thread);
1036
1037        ttrace->ret_scnprintf = ret_scnprintf;
1038}
1039
1040#define TRACE_PFMAJ             (1 << 0)
1041#define TRACE_PFMIN             (1 << 1)
1042
1043static const size_t trace__entry_str_size = 2048;
1044
1045static struct file *thread_trace__files_entry(struct thread_trace *ttrace, int fd)
1046{
1047        if (fd < 0)
1048                return NULL;
1049
1050        if (fd > ttrace->files.max) {
1051                struct file *nfiles = realloc(ttrace->files.table, (fd + 1) * sizeof(struct file));
1052
1053                if (nfiles == NULL)
1054                        return NULL;
1055
1056                if (ttrace->files.max != -1) {
1057                        memset(nfiles + ttrace->files.max + 1, 0,
1058                               (fd - ttrace->files.max) * sizeof(struct file));
1059                } else {
1060                        memset(nfiles, 0, (fd + 1) * sizeof(struct file));
1061                }
1062
1063                ttrace->files.table = nfiles;
1064                ttrace->files.max   = fd;
1065        }
1066
1067        return ttrace->files.table + fd;
1068}
1069
1070struct file *thread__files_entry(struct thread *thread, int fd)
1071{
1072        return thread_trace__files_entry(thread__priv(thread), fd);
1073}
1074
1075static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
1076{
1077        struct thread_trace *ttrace = thread__priv(thread);
1078        struct file *file = thread_trace__files_entry(ttrace, fd);
1079
1080        if (file != NULL) {
1081                struct stat st;
1082                if (stat(pathname, &st) == 0)
1083                        file->dev_maj = major(st.st_rdev);
1084                file->pathname = strdup(pathname);
1085                if (file->pathname)
1086                        return 0;
1087        }
1088
1089        return -1;
1090}
1091
1092static int thread__read_fd_path(struct thread *thread, int fd)
1093{
1094        char linkname[PATH_MAX], pathname[PATH_MAX];
1095        struct stat st;
1096        int ret;
1097
1098        if (thread->pid_ == thread->tid) {
1099                scnprintf(linkname, sizeof(linkname),
1100                          "/proc/%d/fd/%d", thread->pid_, fd);
1101        } else {
1102                scnprintf(linkname, sizeof(linkname),
1103                          "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1104        }
1105
1106        if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1107                return -1;
1108
1109        ret = readlink(linkname, pathname, sizeof(pathname));
1110
1111        if (ret < 0 || ret > st.st_size)
1112                return -1;
1113
1114        pathname[ret] = '\0';
1115        return trace__set_fd_pathname(thread, fd, pathname);
1116}
1117
1118static const char *thread__fd_path(struct thread *thread, int fd,
1119                                   struct trace *trace)
1120{
1121        struct thread_trace *ttrace = thread__priv(thread);
1122
1123        if (ttrace == NULL)
1124                return NULL;
1125
1126        if (fd < 0)
1127                return NULL;
1128
1129        if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) {
1130                if (!trace->live)
1131                        return NULL;
1132                ++trace->stats.proc_getname;
1133                if (thread__read_fd_path(thread, fd))
1134                        return NULL;
1135        }
1136
1137        return ttrace->files.table[fd].pathname;
1138}
1139
1140size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
1141{
1142        int fd = arg->val;
1143        size_t printed = scnprintf(bf, size, "%d", fd);
1144        const char *path = thread__fd_path(arg->thread, fd, arg->trace);
1145
1146        if (path)
1147                printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1148
1149        return printed;
1150}
1151
1152size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1153{
1154        size_t printed = scnprintf(bf, size, "%d", fd);
1155        struct thread *thread = machine__find_thread(trace->host, pid, pid);
1156
1157        if (thread) {
1158                const char *path = thread__fd_path(thread, fd, trace);
1159
1160                if (path)
1161                        printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1162
1163                thread__put(thread);
1164        }
1165
1166        return printed;
1167}
1168
1169static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1170                                              struct syscall_arg *arg)
1171{
1172        int fd = arg->val;
1173        size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
1174        struct thread_trace *ttrace = thread__priv(arg->thread);
1175
1176        if (ttrace && fd >= 0 && fd <= ttrace->files.max)
1177                zfree(&ttrace->files.table[fd].pathname);
1178
1179        return printed;
1180}
1181
1182static void thread__set_filename_pos(struct thread *thread, const char *bf,
1183                                     unsigned long ptr)
1184{
1185        struct thread_trace *ttrace = thread__priv(thread);
1186
1187        ttrace->filename.ptr = ptr;
1188        ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1189}
1190
1191static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1192{
1193        struct augmented_arg *augmented_arg = arg->augmented.args;
1194
1195        return scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value);
1196}
1197
1198static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1199                                              struct syscall_arg *arg)
1200{
1201        unsigned long ptr = arg->val;
1202
1203        if (arg->augmented.args)
1204                return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1205
1206        if (!arg->trace->vfs_getname)
1207                return scnprintf(bf, size, "%#x", ptr);
1208
1209        thread__set_filename_pos(arg->thread, bf, ptr);
1210        return 0;
1211}
1212
1213static bool trace__filter_duration(struct trace *trace, double t)
1214{
1215        return t < (trace->duration_filter * NSEC_PER_MSEC);
1216}
1217
1218static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1219{
1220        double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1221
1222        return fprintf(fp, "%10.3f ", ts);
1223}
1224
1225/*
1226 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1227 * using ttrace->entry_time for a thread that receives a sys_exit without
1228 * first having received a sys_enter ("poll" issued before tracing session
1229 * starts, lost sys_enter exit due to ring buffer overflow).
1230 */
1231static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1232{
1233        if (tstamp > 0)
1234                return __trace__fprintf_tstamp(trace, tstamp, fp);
1235
1236        return fprintf(fp, "         ? ");
1237}
1238
1239static bool done = false;
1240static bool interrupted = false;
1241
1242static void sig_handler(int sig)
1243{
1244        done = true;
1245        interrupted = sig == SIGINT;
1246}
1247
1248static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
1249{
1250        size_t printed = 0;
1251
1252        if (trace->multiple_threads) {
1253                if (trace->show_comm)
1254                        printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
1255                printed += fprintf(fp, "%d ", thread->tid);
1256        }
1257
1258        return printed;
1259}
1260
1261static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1262                                        u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1263{
1264        size_t printed = 0;
1265
1266        if (trace->show_tstamp)
1267                printed = trace__fprintf_tstamp(trace, tstamp, fp);
1268        if (trace->show_duration)
1269                printed += fprintf_duration(duration, duration_calculated, fp);
1270        return printed + trace__fprintf_comm_tid(trace, thread, fp);
1271}
1272
1273static int trace__process_event(struct trace *trace, struct machine *machine,
1274                                union perf_event *event, struct perf_sample *sample)
1275{
1276        int ret = 0;
1277
1278        switch (event->header.type) {
1279        case PERF_RECORD_LOST:
1280                color_fprintf(trace->output, PERF_COLOR_RED,
1281                              "LOST %" PRIu64 " events!\n", event->lost.lost);
1282                ret = machine__process_lost_event(machine, event, sample);
1283                break;
1284        default:
1285                ret = machine__process_event(machine, event, sample);
1286                break;
1287        }
1288
1289        return ret;
1290}
1291
1292static int trace__tool_process(struct perf_tool *tool,
1293                               union perf_event *event,
1294                               struct perf_sample *sample,
1295                               struct machine *machine)
1296{
1297        struct trace *trace = container_of(tool, struct trace, tool);
1298        return trace__process_event(trace, machine, event, sample);
1299}
1300
1301static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1302{
1303        struct machine *machine = vmachine;
1304
1305        if (machine->kptr_restrict_warned)
1306                return NULL;
1307
1308        if (symbol_conf.kptr_restrict) {
1309                pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1310                           "Check /proc/sys/kernel/kptr_restrict.\n\n"
1311                           "Kernel samples will not be resolved.\n");
1312                machine->kptr_restrict_warned = true;
1313                return NULL;
1314        }
1315
1316        return machine__resolve_kernel_addr(vmachine, addrp, modp);
1317}
1318
1319static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1320{
1321        int err = symbol__init(NULL);
1322
1323        if (err)
1324                return err;
1325
1326        trace->host = machine__new_host();
1327        if (trace->host == NULL)
1328                return -ENOMEM;
1329
1330        err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1331        if (err < 0)
1332                goto out;
1333
1334        err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
1335                                            evlist->threads, trace__tool_process, false,
1336                                            1);
1337out:
1338        if (err)
1339                symbol__exit();
1340
1341        return err;
1342}
1343
1344static void trace__symbols__exit(struct trace *trace)
1345{
1346        machine__exit(trace->host);
1347        trace->host = NULL;
1348
1349        symbol__exit();
1350}
1351
1352static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1353{
1354        int idx;
1355
1356        if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1357                nr_args = sc->fmt->nr_args;
1358
1359        sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1360        if (sc->arg_fmt == NULL)
1361                return -1;
1362
1363        for (idx = 0; idx < nr_args; ++idx) {
1364                if (sc->fmt)
1365                        sc->arg_fmt[idx] = sc->fmt->arg[idx];
1366        }
1367
1368        sc->nr_args = nr_args;
1369        return 0;
1370}
1371
1372static int syscall__set_arg_fmts(struct syscall *sc)
1373{
1374        struct tep_format_field *field, *last_field = NULL;
1375        int idx = 0, len;
1376
1377        for (field = sc->args; field; field = field->next, ++idx) {
1378                last_field = field;
1379
1380                if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1381                        continue;
1382
1383                if (strcmp(field->type, "const char *") == 0 &&
1384                         (strcmp(field->name, "filename") == 0 ||
1385                          strcmp(field->name, "path") == 0 ||
1386                          strcmp(field->name, "pathname") == 0))
1387                        sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
1388                else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
1389                        sc->arg_fmt[idx].scnprintf = SCA_PTR;
1390                else if (strcmp(field->type, "pid_t") == 0)
1391                        sc->arg_fmt[idx].scnprintf = SCA_PID;
1392                else if (strcmp(field->type, "umode_t") == 0)
1393                        sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
1394                else if ((strcmp(field->type, "int") == 0 ||
1395                          strcmp(field->type, "unsigned int") == 0 ||
1396                          strcmp(field->type, "long") == 0) &&
1397                         (len = strlen(field->name)) >= 2 &&
1398                         strcmp(field->name + len - 2, "fd") == 0) {
1399                        /*
1400                         * /sys/kernel/tracing/events/syscalls/sys_enter*
1401                         * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1402                         * 65 int
1403                         * 23 unsigned int
1404                         * 7 unsigned long
1405                         */
1406                        sc->arg_fmt[idx].scnprintf = SCA_FD;
1407                }
1408        }
1409
1410        if (last_field)
1411                sc->args_size = last_field->offset + last_field->size;
1412
1413        return 0;
1414}
1415
1416static int trace__read_syscall_info(struct trace *trace, int id)
1417{
1418        char tp_name[128];
1419        struct syscall *sc;
1420        const char *name = syscalltbl__name(trace->sctbl, id);
1421
1422        if (name == NULL)
1423                return -1;
1424
1425        if (id > trace->syscalls.max) {
1426                struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1427
1428                if (nsyscalls == NULL)
1429                        return -1;
1430
1431                if (trace->syscalls.max != -1) {
1432                        memset(nsyscalls + trace->syscalls.max + 1, 0,
1433                               (id - trace->syscalls.max) * sizeof(*sc));
1434                } else {
1435                        memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1436                }
1437
1438                trace->syscalls.table = nsyscalls;
1439                trace->syscalls.max   = id;
1440        }
1441
1442        sc = trace->syscalls.table + id;
1443        sc->name = name;
1444
1445        sc->fmt  = syscall_fmt__find(sc->name);
1446
1447        snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
1448        sc->tp_format = trace_event__tp_format("syscalls", tp_name);
1449
1450        if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
1451                snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
1452                sc->tp_format = trace_event__tp_format("syscalls", tp_name);
1453        }
1454
1455        if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1456                return -1;
1457
1458        if (IS_ERR(sc->tp_format))
1459                return -1;
1460
1461        sc->args = sc->tp_format->format.fields;
1462        /*
1463         * We need to check and discard the first variable '__syscall_nr'
1464         * or 'nr' that mean the syscall number. It is needless here.
1465         * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1466         */
1467        if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
1468                sc->args = sc->args->next;
1469                --sc->nr_args;
1470        }
1471
1472        sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
1473        sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
1474
1475        return syscall__set_arg_fmts(sc);
1476}
1477
1478static int trace__validate_ev_qualifier(struct trace *trace)
1479{
1480        int err = 0, i;
1481        size_t nr_allocated;
1482        struct str_node *pos;
1483
1484        trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1485        trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1486                                                 sizeof(trace->ev_qualifier_ids.entries[0]));
1487
1488        if (trace->ev_qualifier_ids.entries == NULL) {
1489                fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1490                       trace->output);
1491                err = -EINVAL;
1492                goto out;
1493        }
1494
1495        nr_allocated = trace->ev_qualifier_ids.nr;
1496        i = 0;
1497
1498        strlist__for_each_entry(pos, trace->ev_qualifier) {
1499                const char *sc = pos->s;
1500                int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
1501
1502                if (id < 0) {
1503                        id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1504                        if (id >= 0)
1505                                goto matches;
1506
1507                        if (err == 0) {
1508                                fputs("Error:\tInvalid syscall ", trace->output);
1509                                err = -EINVAL;
1510                        } else {
1511                                fputs(", ", trace->output);
1512                        }
1513
1514                        fputs(sc, trace->output);
1515                }
1516matches:
1517                trace->ev_qualifier_ids.entries[i++] = id;
1518                if (match_next == -1)
1519                        continue;
1520
1521                while (1) {
1522                        id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1523                        if (id < 0)
1524                                break;
1525                        if (nr_allocated == trace->ev_qualifier_ids.nr) {
1526                                void *entries;
1527
1528                                nr_allocated += 8;
1529                                entries = realloc(trace->ev_qualifier_ids.entries,
1530                                                  nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1531                                if (entries == NULL) {
1532                                        err = -ENOMEM;
1533                                        fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1534                                        goto out_free;
1535                                }
1536                                trace->ev_qualifier_ids.entries = entries;
1537                        }
1538                        trace->ev_qualifier_ids.nr++;
1539                        trace->ev_qualifier_ids.entries[i++] = id;
1540                }
1541        }
1542
1543        if (err < 0) {
1544                fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1545                      "\nHint:\tand: 'man syscalls'\n", trace->output);
1546out_free:
1547                zfree(&trace->ev_qualifier_ids.entries);
1548                trace->ev_qualifier_ids.nr = 0;
1549        }
1550out:
1551        return err;
1552}
1553
1554/*
1555 * args is to be interpreted as a series of longs but we need to handle
1556 * 8-byte unaligned accesses. args points to raw_data within the event
1557 * and raw_data is guaranteed to be 8-byte unaligned because it is
1558 * preceded by raw_size which is a u32. So we need to copy args to a temp
1559 * variable to read it. Most notably this avoids extended load instructions
1560 * on unaligned addresses
1561 */
1562unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
1563{
1564        unsigned long val;
1565        unsigned char *p = arg->args + sizeof(unsigned long) * idx;
1566
1567        memcpy(&val, p, sizeof(val));
1568        return val;
1569}
1570
1571static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1572                                      struct syscall_arg *arg)
1573{
1574        if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1575                return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1576
1577        return scnprintf(bf, size, "arg%d: ", arg->idx);
1578}
1579
1580/*
1581 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1582 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1583 * in tools/perf/trace/beauty/mount_flags.c
1584 */
1585static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1586{
1587        if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1588                return sc->arg_fmt[arg->idx].mask_val(arg, val);
1589
1590        return val;
1591}
1592
1593static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1594                                     struct syscall_arg *arg, unsigned long val)
1595{
1596        if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1597                arg->val = val;
1598                if (sc->arg_fmt[arg->idx].parm)
1599                        arg->parm = sc->arg_fmt[arg->idx].parm;
1600                return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1601        }
1602        return scnprintf(bf, size, "%ld", val);
1603}
1604
1605static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
1606                                      unsigned char *args, void *augmented_args, int augmented_args_size,
1607                                      struct trace *trace, struct thread *thread)
1608{
1609        size_t printed = 0;
1610        unsigned long val;
1611        u8 bit = 1;
1612        struct syscall_arg arg = {
1613                .args   = args,
1614                .augmented = {
1615                        .size = augmented_args_size,
1616                        .args = augmented_args,
1617                },
1618                .idx    = 0,
1619                .mask   = 0,
1620                .trace  = trace,
1621                .thread = thread,
1622                .show_string_prefix = trace->show_string_prefix,
1623        };
1624        struct thread_trace *ttrace = thread__priv(thread);
1625
1626        /*
1627         * Things like fcntl will set this in its 'cmd' formatter to pick the
1628         * right formatter for the return value (an fd? file flags?), which is
1629         * not needed for syscalls that always return a given type, say an fd.
1630         */
1631        ttrace->ret_scnprintf = NULL;
1632
1633        if (sc->args != NULL) {
1634                struct tep_format_field *field;
1635
1636                for (field = sc->args; field;
1637                     field = field->next, ++arg.idx, bit <<= 1) {
1638                        if (arg.mask & bit)
1639                                continue;
1640
1641                        val = syscall_arg__val(&arg, arg.idx);
1642                        /*
1643                         * Some syscall args need some mask, most don't and
1644                         * return val untouched.
1645                         */
1646                        val = syscall__mask_val(sc, &arg, val);
1647
1648                        /*
1649                         * Suppress this argument if its value is zero and
1650                         * and we don't have a string associated in an
1651                         * strarray for it.
1652                         */
1653                        if (val == 0 &&
1654                            !trace->show_zeros &&
1655                            !(sc->arg_fmt &&
1656                              (sc->arg_fmt[arg.idx].show_zero ||
1657                               sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
1658                               sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1659                              sc->arg_fmt[arg.idx].parm))
1660                                continue;
1661
1662                        printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
1663
1664                        if (trace->show_arg_names)
1665                                printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
1666
1667                        printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1668                }
1669        } else if (IS_ERR(sc->tp_format)) {
1670                /*
1671                 * If we managed to read the tracepoint /format file, then we
1672                 * may end up not having any args, like with gettid(), so only
1673                 * print the raw args when we didn't manage to read it.
1674                 */
1675                while (arg.idx < sc->nr_args) {
1676                        if (arg.mask & bit)
1677                                goto next_arg;
1678                        val = syscall_arg__val(&arg, arg.idx);
1679                        if (printed)
1680                                printed += scnprintf(bf + printed, size - printed, ", ");
1681                        printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
1682                        printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1683next_arg:
1684                        ++arg.idx;
1685                        bit <<= 1;
1686                }
1687        }
1688
1689        return printed;
1690}
1691
1692typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
1693                                  union perf_event *event,
1694                                  struct perf_sample *sample);
1695
1696static struct syscall *trace__syscall_info(struct trace *trace,
1697                                           struct perf_evsel *evsel, int id)
1698{
1699
1700        if (id < 0) {
1701
1702                /*
1703                 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1704                 * before that, leaving at a higher verbosity level till that is
1705                 * explained. Reproduced with plain ftrace with:
1706                 *
1707                 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1708                 * grep "NR -1 " /t/trace_pipe
1709                 *
1710                 * After generating some load on the machine.
1711                 */
1712                if (verbose > 1) {
1713                        static u64 n;
1714                        fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1715                                id, perf_evsel__name(evsel), ++n);
1716                }
1717                return NULL;
1718        }
1719
1720        if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1721            trace__read_syscall_info(trace, id))
1722                goto out_cant_read;
1723
1724        if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1725                goto out_cant_read;
1726
1727        return &trace->syscalls.table[id];
1728
1729out_cant_read:
1730        if (verbose > 0) {
1731                fprintf(trace->output, "Problems reading syscall %d", id);
1732                if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1733                        fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1734                fputs(" information\n", trace->output);
1735        }
1736        return NULL;
1737}
1738
1739static void thread__update_stats(struct thread_trace *ttrace,
1740                                 int id, struct perf_sample *sample)
1741{
1742        struct int_node *inode;
1743        struct stats *stats;
1744        u64 duration = 0;
1745
1746        inode = intlist__findnew(ttrace->syscall_stats, id);
1747        if (inode == NULL)
1748                return;
1749
1750        stats = inode->priv;
1751        if (stats == NULL) {
1752                stats = malloc(sizeof(struct stats));
1753                if (stats == NULL)
1754                        return;
1755                init_stats(stats);
1756                inode->priv = stats;
1757        }
1758
1759        if (ttrace->entry_time && sample->time > ttrace->entry_time)
1760                duration = sample->time - ttrace->entry_time;
1761
1762        update_stats(stats, duration);
1763}
1764
1765static int trace__printf_interrupted_entry(struct trace *trace)
1766{
1767        struct thread_trace *ttrace;
1768        size_t printed;
1769        int len;
1770
1771        if (trace->failure_only || trace->current == NULL)
1772                return 0;
1773
1774        ttrace = thread__priv(trace->current);
1775
1776        if (!ttrace->entry_pending)
1777                return 0;
1778
1779        printed  = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
1780        printed += len = fprintf(trace->output, "%s)", ttrace->entry_str);
1781
1782        if (len < trace->args_alignment - 4)
1783                printed += fprintf(trace->output, "%-*s", trace->args_alignment - 4 - len, " ");
1784
1785        printed += fprintf(trace->output, " ...\n");
1786
1787        ttrace->entry_pending = false;
1788        ++trace->nr_events_printed;
1789
1790        return printed;
1791}
1792
1793static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
1794                                 struct perf_sample *sample, struct thread *thread)
1795{
1796        int printed = 0;
1797
1798        if (trace->print_sample) {
1799                double ts = (double)sample->time / NSEC_PER_MSEC;
1800
1801                printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1802                                   perf_evsel__name(evsel), ts,
1803                                   thread__comm_str(thread),
1804                                   sample->pid, sample->tid, sample->cpu);
1805        }
1806
1807        return printed;
1808}
1809
1810static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, int raw_augmented_args_size)
1811{
1812        void *augmented_args = NULL;
1813        /*
1814         * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
1815         * and there we get all 6 syscall args plus the tracepoint common fields
1816         * that gets calculated at the start and the syscall_nr (another long).
1817         * So we check if that is the case and if so don't look after the
1818         * sc->args_size but always after the full raw_syscalls:sys_enter payload,
1819         * which is fixed.
1820         *
1821         * We'll revisit this later to pass s->args_size to the BPF augmenter
1822         * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1823         * copies only what we need for each syscall, like what happens when we
1824         * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1825         * traffic to just what is needed for each syscall.
1826         */
1827        int args_size = raw_augmented_args_size ?: sc->args_size;
1828
1829        *augmented_args_size = sample->raw_size - args_size;
1830        if (*augmented_args_size > 0)
1831                augmented_args = sample->raw_data + args_size;
1832
1833        return augmented_args;
1834}
1835
1836static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
1837                            union perf_event *event __maybe_unused,
1838                            struct perf_sample *sample)
1839{
1840        char *msg;
1841        void *args;
1842        int printed = 0;
1843        struct thread *thread;
1844        int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1845        int augmented_args_size = 0;
1846        void *augmented_args = NULL;
1847        struct syscall *sc = trace__syscall_info(trace, evsel, id);
1848        struct thread_trace *ttrace;
1849
1850        if (sc == NULL)
1851                return -1;
1852
1853        thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1854        ttrace = thread__trace(thread, trace->output);
1855        if (ttrace == NULL)
1856                goto out_put;
1857
1858        trace__fprintf_sample(trace, evsel, sample, thread);
1859
1860        args = perf_evsel__sc_tp_ptr(evsel, args, sample);
1861
1862        if (ttrace->entry_str == NULL) {
1863                ttrace->entry_str = malloc(trace__entry_str_size);
1864                if (!ttrace->entry_str)
1865                        goto out_put;
1866        }
1867
1868        if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
1869                trace__printf_interrupted_entry(trace);
1870        /*
1871         * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1872         * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1873         * this breaks syscall__augmented_args() check for augmented args, as we calculate
1874         * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1875         * so when handling, say the openat syscall, we end up getting 6 args for the
1876         * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1877         * thinking that the extra 2 u64 args are the augmented filename, so just check
1878         * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1879         */
1880        if (evsel != trace->syscalls.events.sys_enter)
1881                augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
1882        ttrace->entry_time = sample->time;
1883        msg = ttrace->entry_str;
1884        printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
1885
1886        printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
1887                                           args, augmented_args, augmented_args_size, trace, thread);
1888
1889        if (sc->is_exit) {
1890                if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
1891                        int alignment = 0;
1892
1893                        trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
1894                        printed = fprintf(trace->output, "%s)", ttrace->entry_str);
1895                        if (trace->args_alignment > printed)
1896                                alignment = trace->args_alignment - printed;
1897                        fprintf(trace->output, "%*s= ?\n", alignment, " ");
1898                }
1899        } else {
1900                ttrace->entry_pending = true;
1901                /* See trace__vfs_getname & trace__sys_exit */
1902                ttrace->filename.pending_open = false;
1903        }
1904
1905        if (trace->current != thread) {
1906                thread__put(trace->current);
1907                trace->current = thread__get(thread);
1908        }
1909        err = 0;
1910out_put:
1911        thread__put(thread);
1912        return err;
1913}
1914
1915static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
1916                                    struct perf_sample *sample)
1917{
1918        struct thread_trace *ttrace;
1919        struct thread *thread;
1920        int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1921        struct syscall *sc = trace__syscall_info(trace, evsel, id);
1922        char msg[1024];
1923        void *args, *augmented_args = NULL;
1924        int augmented_args_size;
1925
1926        if (sc == NULL)
1927                return -1;
1928
1929        thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1930        ttrace = thread__trace(thread, trace->output);
1931        /*
1932         * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
1933         * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
1934         */
1935        if (ttrace == NULL)
1936                goto out_put;
1937
1938        args = perf_evsel__sc_tp_ptr(evsel, args, sample);
1939        augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
1940        syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
1941        fprintf(trace->output, "%s", msg);
1942        err = 0;
1943out_put:
1944        thread__put(thread);
1945        return err;
1946}
1947
1948static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
1949                                    struct perf_sample *sample,
1950                                    struct callchain_cursor *cursor)
1951{
1952        struct addr_location al;
1953        int max_stack = evsel->attr.sample_max_stack ?
1954                        evsel->attr.sample_max_stack :
1955                        trace->max_stack;
1956        int err;
1957
1958        if (machine__resolve(trace->host, &al, sample) < 0)
1959                return -1;
1960
1961        err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
1962        addr_location__put(&al);
1963        return err;
1964}
1965
1966static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
1967{
1968        /* TODO: user-configurable print_opts */
1969        const unsigned int print_opts = EVSEL__PRINT_SYM |
1970                                        EVSEL__PRINT_DSO |
1971                                        EVSEL__PRINT_UNKNOWN_AS_ADDR;
1972
1973        return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
1974}
1975
1976static const char *errno_to_name(struct perf_evsel *evsel, int err)
1977{
1978        struct perf_env *env = perf_evsel__env(evsel);
1979        const char *arch_name = perf_env__arch(env);
1980
1981        return arch_syscalls__strerrno(arch_name, err);
1982}
1983
1984static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
1985                           union perf_event *event __maybe_unused,
1986                           struct perf_sample *sample)
1987{
1988        long ret;
1989        u64 duration = 0;
1990        bool duration_calculated = false;
1991        struct thread *thread;
1992        int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
1993        int alignment = trace->args_alignment;
1994        struct syscall *sc = trace__syscall_info(trace, evsel, id);
1995        struct thread_trace *ttrace;
1996
1997        if (sc == NULL)
1998                return -1;
1999
2000        thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2001        ttrace = thread__trace(thread, trace->output);
2002        if (ttrace == NULL)
2003                goto out_put;
2004
2005        trace__fprintf_sample(trace, evsel, sample, thread);
2006
2007        if (trace->summary)
2008                thread__update_stats(ttrace, id, sample);
2009
2010        ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
2011
2012        if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
2013                trace__set_fd_pathname(thread, ret, ttrace->filename.name);
2014                ttrace->filename.pending_open = false;
2015                ++trace->stats.vfs_getname;
2016        }
2017
2018        if (ttrace->entry_time) {
2019                duration = sample->time - ttrace->entry_time;
2020                if (trace__filter_duration(trace, duration))
2021                        goto out;
2022                duration_calculated = true;
2023        } else if (trace->duration_filter)
2024                goto out;
2025
2026        if (sample->callchain) {
2027                callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2028                if (callchain_ret == 0) {
2029                        if (callchain_cursor.nr < trace->min_stack)
2030                                goto out;
2031                        callchain_ret = 1;
2032                }
2033        }
2034
2035        if (trace->summary_only || (ret >= 0 && trace->failure_only))
2036                goto out;
2037
2038        trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
2039
2040        if (ttrace->entry_pending) {
2041                printed = fprintf(trace->output, "%s", ttrace->entry_str);
2042        } else {
2043                printed += fprintf(trace->output, " ... [");
2044                color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
2045                printed += 9;
2046                printed += fprintf(trace->output, "]: %s()", sc->name);
2047        }
2048
2049        printed++; /* the closing ')' */
2050
2051        if (alignment > printed)
2052                alignment -= printed;
2053        else
2054                alignment = 0;
2055
2056        fprintf(trace->output, ")%*s= ", alignment, " ");
2057
2058        if (sc->fmt == NULL) {
2059                if (ret < 0)
2060                        goto errno_print;
2061signed_print:
2062                fprintf(trace->output, "%ld", ret);
2063        } else if (ret < 0) {
2064errno_print: {
2065                char bf[STRERR_BUFSIZE];
2066                const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
2067                           *e = errno_to_name(evsel, -ret);
2068
2069                fprintf(trace->output, "-1 %s (%s)", e, emsg);
2070        }
2071        } else if (ret == 0 && sc->fmt->timeout)
2072                fprintf(trace->output, "0 (Timeout)");
2073        else if (ttrace->ret_scnprintf) {
2074                char bf[1024];
2075                struct syscall_arg arg = {
2076                        .val    = ret,
2077                        .thread = thread,
2078                        .trace  = trace,
2079                };
2080                ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
2081                ttrace->ret_scnprintf = NULL;
2082                fprintf(trace->output, "%s", bf);
2083        } else if (sc->fmt->hexret)
2084                fprintf(trace->output, "%#lx", ret);
2085        else if (sc->fmt->errpid) {
2086                struct thread *child = machine__find_thread(trace->host, ret, ret);
2087
2088                if (child != NULL) {
2089                        fprintf(trace->output, "%ld", ret);
2090                        if (child->comm_set)
2091                                fprintf(trace->output, " (%s)", thread__comm_str(child));
2092                        thread__put(child);
2093                }
2094        } else
2095                goto signed_print;
2096
2097        fputc('\n', trace->output);
2098
2099        /*
2100         * We only consider an 'event' for the sake of --max-events a non-filtered
2101         * sys_enter + sys_exit and other tracepoint events.
2102         */
2103        if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2104                interrupted = true;
2105
2106        if (callchain_ret > 0)
2107                trace__fprintf_callchain(trace, sample);
2108        else if (callchain_ret < 0)
2109                pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2110out:
2111        ttrace->entry_pending = false;
2112        err = 0;
2113out_put:
2114        thread__put(thread);
2115        return err;
2116}
2117
2118static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
2119                              union perf_event *event __maybe_unused,
2120                              struct perf_sample *sample)
2121{
2122        struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2123        struct thread_trace *ttrace;
2124        size_t filename_len, entry_str_len, to_move;
2125        ssize_t remaining_space;
2126        char *pos;
2127        const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
2128
2129        if (!thread)
2130                goto out;
2131
2132        ttrace = thread__priv(thread);
2133        if (!ttrace)
2134                goto out_put;
2135
2136        filename_len = strlen(filename);
2137        if (filename_len == 0)
2138                goto out_put;
2139
2140        if (ttrace->filename.namelen < filename_len) {
2141                char *f = realloc(ttrace->filename.name, filename_len + 1);
2142
2143                if (f == NULL)
2144                        goto out_put;
2145
2146                ttrace->filename.namelen = filename_len;
2147                ttrace->filename.name = f;
2148        }
2149
2150        strcpy(ttrace->filename.name, filename);
2151        ttrace->filename.pending_open = true;
2152
2153        if (!ttrace->filename.ptr)
2154                goto out_put;
2155
2156        entry_str_len = strlen(ttrace->entry_str);
2157        remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2158        if (remaining_space <= 0)
2159                goto out_put;
2160
2161        if (filename_len > (size_t)remaining_space) {
2162                filename += filename_len - remaining_space;
2163                filename_len = remaining_space;
2164        }
2165
2166        to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2167        pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2168        memmove(pos + filename_len, pos, to_move);
2169        memcpy(pos, filename, filename_len);
2170
2171        ttrace->filename.ptr = 0;
2172        ttrace->filename.entry_str_pos = 0;
2173out_put:
2174        thread__put(thread);
2175out:
2176        return 0;
2177}
2178
2179static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
2180                                     union perf_event *event __maybe_unused,
2181                                     struct perf_sample *sample)
2182{
2183        u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2184        double runtime_ms = (double)runtime / NSEC_PER_MSEC;
2185        struct thread *thread = machine__findnew_thread(trace->host,
2186                                                        sample->pid,
2187                                                        sample->tid);
2188        struct thread_trace *ttrace = thread__trace(thread, trace->output);
2189
2190        if (ttrace == NULL)
2191                goto out_dump;
2192
2193        ttrace->runtime_ms += runtime_ms;
2194        trace->runtime_ms += runtime_ms;
2195out_put:
2196        thread__put(thread);
2197        return 0;
2198
2199out_dump:
2200        fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
2201               evsel->name,
2202               perf_evsel__strval(evsel, sample, "comm"),
2203               (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2204               runtime,
2205               perf_evsel__intval(evsel, sample, "vruntime"));
2206        goto out_put;
2207}
2208
2209static int bpf_output__printer(enum binary_printer_ops op,
2210                               unsigned int val, void *extra __maybe_unused, FILE *fp)
2211{
2212        unsigned char ch = (unsigned char)val;
2213
2214        switch (op) {
2215        case BINARY_PRINT_CHAR_DATA:
2216                return fprintf(fp, "%c", isprint(ch) ? ch : '.');
2217        case BINARY_PRINT_DATA_BEGIN:
2218        case BINARY_PRINT_LINE_BEGIN:
2219        case BINARY_PRINT_ADDR:
2220        case BINARY_PRINT_NUM_DATA:
2221        case BINARY_PRINT_NUM_PAD:
2222        case BINARY_PRINT_SEP:
2223        case BINARY_PRINT_CHAR_PAD:
2224        case BINARY_PRINT_LINE_END:
2225        case BINARY_PRINT_DATA_END:
2226        default:
2227                break;
2228        }
2229
2230        return 0;
2231}
2232
2233static void bpf_output__fprintf(struct trace *trace,
2234                                struct perf_sample *sample)
2235{
2236        binary__fprintf(sample->raw_data, sample->raw_size, 8,
2237                        bpf_output__printer, NULL, trace->output);
2238        ++trace->nr_events_printed;
2239}
2240
2241static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2242                                union perf_event *event __maybe_unused,
2243                                struct perf_sample *sample)
2244{
2245        struct thread *thread;
2246        int callchain_ret = 0;
2247        /*
2248         * Check if we called perf_evsel__disable(evsel) due to, for instance,
2249         * this event's max_events having been hit and this is an entry coming
2250         * from the ring buffer that we should discard, since the max events
2251         * have already been considered/printed.
2252         */
2253        if (evsel->disabled)
2254                return 0;
2255
2256        thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2257
2258        if (sample->callchain) {
2259                callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2260                if (callchain_ret == 0) {
2261                        if (callchain_cursor.nr < trace->min_stack)
2262                                goto out;
2263                        callchain_ret = 1;
2264                }
2265        }
2266
2267        trace__printf_interrupted_entry(trace);
2268        trace__fprintf_tstamp(trace, sample->time, trace->output);
2269
2270        if (trace->trace_syscalls && trace->show_duration)
2271                fprintf(trace->output, "(         ): ");
2272
2273        if (thread)
2274                trace__fprintf_comm_tid(trace, thread, trace->output);
2275
2276        if (evsel == trace->syscalls.events.augmented) {
2277                int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2278                struct syscall *sc = trace__syscall_info(trace, evsel, id);
2279
2280                if (sc) {
2281                        fprintf(trace->output, "%s(", sc->name);
2282                        trace__fprintf_sys_enter(trace, evsel, sample);
2283                        fputc(')', trace->output);
2284                        goto newline;
2285                }
2286
2287                /*
2288                 * XXX: Not having the associated syscall info or not finding/adding
2289                 *      the thread should never happen, but if it does...
2290                 *      fall thru and print it as a bpf_output event.
2291                 */
2292        }
2293
2294        fprintf(trace->output, "%s:", evsel->name);
2295
2296        if (perf_evsel__is_bpf_output(evsel)) {
2297                bpf_output__fprintf(trace, sample);
2298        } else if (evsel->tp_format) {
2299                if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2300                    trace__fprintf_sys_enter(trace, evsel, sample)) {
2301                        event_format__fprintf(evsel->tp_format, sample->cpu,
2302                                              sample->raw_data, sample->raw_size,
2303                                              trace->output);
2304                        ++trace->nr_events_printed;
2305
2306                        if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
2307                                perf_evsel__disable(evsel);
2308                                perf_evsel__close(evsel);
2309                        }
2310                }
2311        }
2312
2313newline:
2314        fprintf(trace->output, "\n");
2315
2316        if (callchain_ret > 0)
2317                trace__fprintf_callchain(trace, sample);
2318        else if (callchain_ret < 0)
2319                pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2320out:
2321        thread__put(thread);
2322        return 0;
2323}
2324
2325static void print_location(FILE *f, struct perf_sample *sample,
2326                           struct addr_location *al,
2327                           bool print_dso, bool print_sym)
2328{
2329
2330        if ((verbose > 0 || print_dso) && al->map)
2331                fprintf(f, "%s@", al->map->dso->long_name);
2332
2333        if ((verbose > 0 || print_sym) && al->sym)
2334                fprintf(f, "%s+0x%" PRIx64, al->sym->name,
2335                        al->addr - al->sym->start);
2336        else if (al->map)
2337                fprintf(f, "0x%" PRIx64, al->addr);
2338        else
2339                fprintf(f, "0x%" PRIx64, sample->addr);
2340}
2341
2342static int trace__pgfault(struct trace *trace,
2343                          struct perf_evsel *evsel,
2344                          union perf_event *event __maybe_unused,
2345                          struct perf_sample *sample)
2346{
2347        struct thread *thread;
2348        struct addr_location al;
2349        char map_type = 'd';
2350        struct thread_trace *ttrace;
2351        int err = -1;
2352        int callchain_ret = 0;
2353
2354        thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2355
2356        if (sample->callchain) {
2357                callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2358                if (callchain_ret == 0) {
2359                        if (callchain_cursor.nr < trace->min_stack)
2360                                goto out_put;
2361                        callchain_ret = 1;
2362                }
2363        }
2364
2365        ttrace = thread__trace(thread, trace->output);
2366        if (ttrace == NULL)
2367                goto out_put;
2368
2369        if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2370                ttrace->pfmaj++;
2371        else
2372                ttrace->pfmin++;
2373
2374        if (trace->summary_only)
2375                goto out;
2376
2377        thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
2378
2379        trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
2380
2381        fprintf(trace->output, "%sfault [",
2382                evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2383                "maj" : "min");
2384
2385        print_location(trace->output, sample, &al, false, true);
2386
2387        fprintf(trace->output, "] => ");
2388
2389        thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
2390
2391        if (!al.map) {
2392                thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
2393
2394                if (al.map)
2395                        map_type = 'x';
2396                else
2397                        map_type = '?';
2398        }
2399
2400        print_location(trace->output, sample, &al, true, false);
2401
2402        fprintf(trace->output, " (%c%c)\n", map_type, al.level);
2403
2404        if (callchain_ret > 0)
2405                trace__fprintf_callchain(trace, sample);
2406        else if (callchain_ret < 0)
2407                pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2408
2409        ++trace->nr_events_printed;
2410out:
2411        err = 0;
2412out_put:
2413        thread__put(thread);
2414        return err;
2415}
2416
2417static void trace__set_base_time(struct trace *trace,
2418                                 struct perf_evsel *evsel,
2419                                 struct perf_sample *sample)
2420{
2421        /*
2422         * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2423         * and don't use sample->time unconditionally, we may end up having
2424         * some other event in the future without PERF_SAMPLE_TIME for good
2425         * reason, i.e. we may not be interested in its timestamps, just in
2426         * it taking place, picking some piece of information when it
2427         * appears in our event stream (vfs_getname comes to mind).
2428         */
2429        if (trace->base_time == 0 && !trace->full_time &&
2430            (evsel->attr.sample_type & PERF_SAMPLE_TIME))
2431                trace->base_time = sample->time;
2432}
2433
2434static int trace__process_sample(struct perf_tool *tool,
2435                                 union perf_event *event,
2436                                 struct perf_sample *sample,
2437                                 struct perf_evsel *evsel,
2438                                 struct machine *machine __maybe_unused)
2439{
2440        struct trace *trace = container_of(tool, struct trace, tool);
2441        struct thread *thread;
2442        int err = 0;
2443
2444        tracepoint_handler handler = evsel->handler;
2445
2446        thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2447        if (thread && thread__is_filtered(thread))
2448                goto out;
2449
2450        trace__set_base_time(trace, evsel, sample);
2451
2452        if (handler) {
2453                ++trace->nr_events;
2454                handler(trace, evsel, event, sample);
2455        }
2456out:
2457        thread__put(thread);
2458        return err;
2459}
2460
2461static int trace__record(struct trace *trace, int argc, const char **argv)
2462{
2463        unsigned int rec_argc, i, j;
2464        const char **rec_argv;
2465        const char * const record_args[] = {
2466                "record",
2467                "-R",
2468                "-m", "1024",
2469                "-c", "1",
2470        };
2471
2472        const char * const sc_args[] = { "-e", };
2473        unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2474        const char * const majpf_args[] = { "-e", "major-faults" };
2475        unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2476        const char * const minpf_args[] = { "-e", "minor-faults" };
2477        unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2478
2479        /* +1 is for the event string below */
2480        rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2481                majpf_args_nr + minpf_args_nr + argc;
2482        rec_argv = calloc(rec_argc + 1, sizeof(char *));
2483
2484        if (rec_argv == NULL)
2485                return -ENOMEM;
2486
2487        j = 0;
2488        for (i = 0; i < ARRAY_SIZE(record_args); i++)
2489                rec_argv[j++] = record_args[i];
2490
2491        if (trace->trace_syscalls) {
2492                for (i = 0; i < sc_args_nr; i++)
2493                        rec_argv[j++] = sc_args[i];
2494
2495                /* event string may be different for older kernels - e.g., RHEL6 */
2496                if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2497                        rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2498                else if (is_valid_tracepoint("syscalls:sys_enter"))
2499                        rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2500                else {
2501                        pr_err("Neither raw_syscalls nor syscalls events exist.\n");
2502                        free(rec_argv);
2503                        return -1;
2504                }
2505        }
2506
2507        if (trace->trace_pgfaults & TRACE_PFMAJ)
2508                for (i = 0; i < majpf_args_nr; i++)
2509                        rec_argv[j++] = majpf_args[i];
2510
2511        if (trace->trace_pgfaults & TRACE_PFMIN)
2512                for (i = 0; i < minpf_args_nr; i++)
2513                        rec_argv[j++] = minpf_args[i];
2514
2515        for (i = 0; i < (unsigned int)argc; i++)
2516                rec_argv[j++] = argv[i];
2517
2518        return cmd_record(j, rec_argv);
2519}
2520
2521static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2522
2523static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
2524{
2525        bool found = false;
2526        struct perf_evsel *evsel, *tmp;
2527        struct parse_events_error err = { .idx = 0, };
2528        int ret = parse_events(evlist, "probe:vfs_getname*", &err);
2529
2530        if (ret)
2531                return false;
2532
2533        evlist__for_each_entry_safe(evlist, evsel, tmp) {
2534                if (!strstarts(perf_evsel__name(evsel), "probe:vfs_getname"))
2535                        continue;
2536
2537                if (perf_evsel__field(evsel, "pathname")) {
2538                        evsel->handler = trace__vfs_getname;
2539                        found = true;
2540                        continue;
2541                }
2542
2543                list_del_init(&evsel->node);
2544                evsel->evlist = NULL;
2545                perf_evsel__delete(evsel);
2546        }
2547
2548        return found;
2549}
2550
2551static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
2552{
2553        struct perf_evsel *evsel;
2554        struct perf_event_attr attr = {
2555                .type = PERF_TYPE_SOFTWARE,
2556                .mmap_data = 1,
2557        };
2558
2559        attr.config = config;
2560        attr.sample_period = 1;
2561
2562        event_attr_init(&attr);
2563
2564        evsel = perf_evsel__new(&attr);
2565        if (evsel)
2566                evsel->handler = trace__pgfault;
2567
2568        return evsel;
2569}
2570
2571static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2572{
2573        const u32 type = event->header.type;
2574        struct perf_evsel *evsel;
2575
2576        if (type != PERF_RECORD_SAMPLE) {
2577                trace__process_event(trace, trace->host, event, sample);
2578                return;
2579        }
2580
2581        evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2582        if (evsel == NULL) {
2583                fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2584                return;
2585        }
2586
2587        trace__set_base_time(trace, evsel, sample);
2588
2589        if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2590            sample->raw_data == NULL) {
2591                fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2592                       perf_evsel__name(evsel), sample->tid,
2593                       sample->cpu, sample->raw_size);
2594        } else {
2595                tracepoint_handler handler = evsel->handler;
2596                handler(trace, evsel, event, sample);
2597        }
2598
2599        if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2600                interrupted = true;
2601}
2602
2603static int trace__add_syscall_newtp(struct trace *trace)
2604{
2605        int ret = -1;
2606        struct perf_evlist *evlist = trace->evlist;
2607        struct perf_evsel *sys_enter, *sys_exit;
2608
2609        sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
2610        if (sys_enter == NULL)
2611                goto out;
2612
2613        if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2614                goto out_delete_sys_enter;
2615
2616        sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
2617        if (sys_exit == NULL)
2618                goto out_delete_sys_enter;
2619
2620        if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2621                goto out_delete_sys_exit;
2622
2623        perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2624        perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2625
2626        perf_evlist__add(evlist, sys_enter);
2627        perf_evlist__add(evlist, sys_exit);
2628
2629        if (callchain_param.enabled && !trace->kernel_syscallchains) {
2630                /*
2631                 * We're interested only in the user space callchain
2632                 * leading to the syscall, allow overriding that for
2633                 * debugging reasons using --kernel_syscall_callchains
2634                 */
2635                sys_exit->attr.exclude_callchain_kernel = 1;
2636        }
2637
2638        trace->syscalls.events.sys_enter = sys_enter;
2639        trace->syscalls.events.sys_exit  = sys_exit;
2640
2641        ret = 0;
2642out:
2643        return ret;
2644
2645out_delete_sys_exit:
2646        perf_evsel__delete_priv(sys_exit);
2647out_delete_sys_enter:
2648        perf_evsel__delete_priv(sys_enter);
2649        goto out;
2650}
2651
2652static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
2653{
2654        int err = -1;
2655        struct perf_evsel *sys_exit;
2656        char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2657                                                trace->ev_qualifier_ids.nr,
2658                                                trace->ev_qualifier_ids.entries);
2659
2660        if (filter == NULL)
2661                goto out_enomem;
2662
2663        if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2664                                          filter)) {
2665                sys_exit = trace->syscalls.events.sys_exit;
2666                err = perf_evsel__append_tp_filter(sys_exit, filter);
2667        }
2668
2669        free(filter);
2670out:
2671        return err;
2672out_enomem:
2673        errno = ENOMEM;
2674        goto out;
2675}
2676
2677#ifdef HAVE_LIBBPF_SUPPORT
2678static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
2679{
2680        int fd = bpf_map__fd(trace->syscalls.map);
2681        struct bpf_map_syscall_entry value = {
2682                .enabled = !trace->not_ev_qualifier,
2683        };
2684        int err = 0;
2685        size_t i;
2686
2687        for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
2688                int key = trace->ev_qualifier_ids.entries[i];
2689
2690                err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
2691                if (err)
2692                        break;
2693        }
2694
2695        return err;
2696}
2697
2698static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
2699{
2700        int fd = bpf_map__fd(trace->syscalls.map);
2701        struct bpf_map_syscall_entry value = {
2702                .enabled = enabled,
2703        };
2704        int err = 0, key;
2705
2706        for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
2707                err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
2708                if (err)
2709                        break;
2710        }
2711
2712        return err;
2713}
2714
2715static int trace__init_syscalls_bpf_map(struct trace *trace)
2716{
2717        bool enabled = true;
2718
2719        if (trace->ev_qualifier_ids.nr)
2720                enabled = trace->not_ev_qualifier;
2721
2722        return __trace__init_syscalls_bpf_map(trace, enabled);
2723}
2724#else
2725static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
2726{
2727        return 0;
2728}
2729
2730static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
2731{
2732        return 0;
2733}
2734#endif // HAVE_LIBBPF_SUPPORT
2735
2736static int trace__set_ev_qualifier_filter(struct trace *trace)
2737{
2738        if (trace->syscalls.map)
2739                return trace__set_ev_qualifier_bpf_filter(trace);
2740        if (trace->syscalls.events.sys_enter)
2741                return trace__set_ev_qualifier_tp_filter(trace);
2742        return 0;
2743}
2744
2745static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
2746                                    size_t npids __maybe_unused, pid_t *pids __maybe_unused)
2747{
2748        int err = 0;
2749#ifdef HAVE_LIBBPF_SUPPORT
2750        bool value = true;
2751        int map_fd = bpf_map__fd(map);
2752        size_t i;
2753
2754        for (i = 0; i < npids; ++i) {
2755                err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
2756                if (err)
2757                        break;
2758        }
2759#endif
2760        return err;
2761}
2762
2763static int trace__set_filter_loop_pids(struct trace *trace)
2764{
2765        unsigned int nr = 1, err;
2766        pid_t pids[32] = {
2767                getpid(),
2768        };
2769        struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
2770
2771        while (thread && nr < ARRAY_SIZE(pids)) {
2772                struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
2773
2774                if (parent == NULL)
2775                        break;
2776
2777                if (!strcmp(thread__comm_str(parent), "sshd") ||
2778                    strstarts(thread__comm_str(parent), "gnome-terminal")) {
2779                        pids[nr++] = parent->tid;
2780                        break;
2781                }
2782                thread = parent;
2783        }
2784
2785        err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
2786        if (!err && trace->filter_pids.map)
2787                err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
2788
2789        return err;
2790}
2791
2792static int trace__set_filter_pids(struct trace *trace)
2793{
2794        int err = 0;
2795        /*
2796         * Better not use !target__has_task() here because we need to cover the
2797         * case where no threads were specified in the command line, but a
2798         * workload was, and in that case we will fill in the thread_map when
2799         * we fork the workload in perf_evlist__prepare_workload.
2800         */
2801        if (trace->filter_pids.nr > 0) {
2802                err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
2803                                                      trace->filter_pids.entries);
2804                if (!err && trace->filter_pids.map) {
2805                        err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
2806                                                       trace->filter_pids.entries);
2807                }
2808        } else if (thread_map__pid(trace->evlist->threads, 0) == -1) {
2809                err = trace__set_filter_loop_pids(trace);
2810        }
2811
2812        return err;
2813}
2814
2815static int __trace__deliver_event(struct trace *trace, union perf_event *event)
2816{
2817        struct perf_evlist *evlist = trace->evlist;
2818        struct perf_sample sample;
2819        int err;
2820
2821        err = perf_evlist__parse_sample(evlist, event, &sample);
2822        if (err)
2823                fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
2824        else
2825                trace__handle_event(trace, event, &sample);
2826
2827        return 0;
2828}
2829
2830static int __trace__flush_events(struct trace *trace)
2831{
2832        u64 first = ordered_events__first_time(&trace->oe.data);
2833        u64 flush = trace->oe.last - NSEC_PER_SEC;
2834
2835        /* Is there some thing to flush.. */
2836        if (first && first < flush)
2837                return ordered_events__flush_time(&trace->oe.data, flush);
2838
2839        return 0;
2840}
2841
2842static int trace__flush_events(struct trace *trace)
2843{
2844        return !trace->sort_events ? 0 : __trace__flush_events(trace);
2845}
2846
2847static int trace__deliver_event(struct trace *trace, union perf_event *event)
2848{
2849        int err;
2850
2851        if (!trace->sort_events)
2852                return __trace__deliver_event(trace, event);
2853
2854        err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
2855        if (err && err != -1)
2856                return err;
2857
2858        err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
2859        if (err)
2860                return err;
2861
2862        return trace__flush_events(trace);
2863}
2864
2865static int ordered_events__deliver_event(struct ordered_events *oe,
2866                                         struct ordered_event *event)
2867{
2868        struct trace *trace = container_of(oe, struct trace, oe.data);
2869
2870        return __trace__deliver_event(trace, event->event);
2871}
2872
2873static int trace__run(struct trace *trace, int argc, const char **argv)
2874{
2875        struct perf_evlist *evlist = trace->evlist;
2876        struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
2877        int err = -1, i;
2878        unsigned long before;
2879        const bool forks = argc > 0;
2880        bool draining = false;
2881
2882        trace->live = true;
2883
2884        if (!trace->raw_augmented_syscalls) {
2885                if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
2886                        goto out_error_raw_syscalls;
2887
2888                if (trace->trace_syscalls)
2889                        trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
2890        }
2891
2892        if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
2893                pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
2894                if (pgfault_maj == NULL)
2895                        goto out_error_mem;
2896                perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
2897                perf_evlist__add(evlist, pgfault_maj);
2898        }
2899
2900        if ((trace->trace_pgfaults & TRACE_PFMIN)) {
2901                pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
2902                if (pgfault_min == NULL)
2903                        goto out_error_mem;
2904                perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
2905                perf_evlist__add(evlist, pgfault_min);
2906        }
2907
2908        if (trace->sched &&
2909            perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2910                                   trace__sched_stat_runtime))
2911                goto out_error_sched_stat_runtime;
2912
2913        /*
2914         * If a global cgroup was set, apply it to all the events without an
2915         * explicit cgroup. I.e.:
2916         *
2917         *      trace -G A -e sched:*switch
2918         *
2919         * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
2920         * _and_ sched:sched_switch to the 'A' cgroup, while:
2921         *
2922         * trace -e sched:*switch -G A
2923         *
2924         * will only set the sched:sched_switch event to the 'A' cgroup, all the
2925         * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
2926         * a cgroup (on the root cgroup, sys wide, etc).
2927         *
2928         * Multiple cgroups:
2929         *
2930         * trace -G A -e sched:*switch -G B
2931         *
2932         * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
2933         * to the 'B' cgroup.
2934         *
2935         * evlist__set_default_cgroup() grabs a reference of the passed cgroup
2936         * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
2937         */
2938        if (trace->cgroup)
2939                evlist__set_default_cgroup(trace->evlist, trace->cgroup);
2940
2941        err = perf_evlist__create_maps(evlist, &trace->opts.target);
2942        if (err < 0) {
2943                fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
2944                goto out_delete_evlist;
2945        }
2946
2947        err = trace__symbols_init(trace, evlist);
2948        if (err < 0) {
2949                fprintf(trace->output, "Problems initializing symbol libraries!\n");
2950                goto out_delete_evlist;
2951        }
2952
2953        perf_evlist__config(evlist, &trace->opts, &callchain_param);
2954
2955        signal(SIGCHLD, sig_handler);
2956        signal(SIGINT, sig_handler);
2957
2958        if (forks) {
2959                err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
2960                                                    argv, false, NULL);
2961                if (err < 0) {
2962                        fprintf(trace->output, "Couldn't run the workload!\n");
2963                        goto out_delete_evlist;
2964                }
2965        }
2966
2967        err = perf_evlist__open(evlist);
2968        if (err < 0)
2969                goto out_error_open;
2970
2971        err = bpf__apply_obj_config();
2972        if (err) {
2973                char errbuf[BUFSIZ];
2974
2975                bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
2976                pr_err("ERROR: Apply config to BPF failed: %s\n",
2977                         errbuf);
2978                goto out_error_open;
2979        }
2980
2981        err = trace__set_filter_pids(trace);
2982        if (err < 0)
2983                goto out_error_mem;
2984
2985        if (trace->syscalls.map)
2986                trace__init_syscalls_bpf_map(trace);
2987
2988        if (trace->ev_qualifier_ids.nr > 0) {
2989                err = trace__set_ev_qualifier_filter(trace);
2990                if (err < 0)
2991                        goto out_errno;
2992
2993                if (trace->syscalls.events.sys_exit) {
2994                        pr_debug("event qualifier tracepoint filter: %s\n",
2995                                 trace->syscalls.events.sys_exit->filter);
2996                }
2997        }
2998
2999        err = perf_evlist__apply_filters(evlist, &evsel);
3000        if (err < 0)
3001                goto out_error_apply_filters;
3002
3003        if (trace->dump.map)
3004                bpf_map__fprintf(trace->dump.map, trace->output);
3005
3006        err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
3007        if (err < 0)
3008                goto out_error_mmap;
3009
3010        if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
3011                perf_evlist__enable(evlist);
3012
3013        if (forks)
3014                perf_evlist__start_workload(evlist);
3015
3016        if (trace->opts.initial_delay) {
3017                usleep(trace->opts.initial_delay * 1000);
3018                perf_evlist__enable(evlist);
3019        }
3020
3021        trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
3022                                  evlist->threads->nr > 1 ||
3023                                  perf_evlist__first(evlist)->attr.inherit;
3024
3025        /*
3026         * Now that we already used evsel->attr to ask the kernel to setup the
3027         * events, lets reuse evsel->attr.sample_max_stack as the limit in
3028         * trace__resolve_callchain(), allowing per-event max-stack settings
3029         * to override an explicitly set --max-stack global setting.
3030         */
3031        evlist__for_each_entry(evlist, evsel) {
3032                if (evsel__has_callchain(evsel) &&
3033                    evsel->attr.sample_max_stack == 0)
3034                        evsel->attr.sample_max_stack = trace->max_stack;
3035        }
3036again:
3037        before = trace->nr_events;
3038
3039        for (i = 0; i < evlist->nr_mmaps; i++) {
3040                union perf_event *event;
3041                struct perf_mmap *md;
3042
3043                md = &evlist->mmap[i];
3044                if (perf_mmap__read_init(md) < 0)
3045                        continue;
3046
3047                while ((event = perf_mmap__read_event(md)) != NULL) {
3048                        ++trace->nr_events;
3049
3050                        err = trace__deliver_event(trace, event);
3051                        if (err)
3052                                goto out_disable;
3053
3054                        perf_mmap__consume(md);
3055
3056                        if (interrupted)
3057                                goto out_disable;
3058
3059                        if (done && !draining) {
3060                                perf_evlist__disable(evlist);
3061                                draining = true;
3062                        }
3063                }
3064                perf_mmap__read_done(md);
3065        }
3066
3067        if (trace->nr_events == before) {
3068                int timeout = done ? 100 : -1;
3069
3070                if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
3071                        if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
3072                                draining = true;
3073
3074                        goto again;
3075                } else {
3076                        if (trace__flush_events(trace))
3077                                goto out_disable;
3078                }
3079        } else {
3080                goto again;
3081        }
3082
3083out_disable:
3084        thread__zput(trace->current);
3085
3086        perf_evlist__disable(evlist);
3087
3088        if (trace->sort_events)
3089                ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
3090
3091        if (!err) {
3092                if (trace->summary)
3093                        trace__fprintf_thread_summary(trace, trace->output);
3094
3095                if (trace->show_tool_stats) {
3096                        fprintf(trace->output, "Stats:\n "
3097                                               " vfs_getname : %" PRIu64 "\n"
3098                                               " proc_getname: %" PRIu64 "\n",
3099                                trace->stats.vfs_getname,
3100                                trace->stats.proc_getname);
3101                }
3102        }
3103
3104out_delete_evlist:
3105        trace__symbols__exit(trace);
3106
3107        perf_evlist__delete(evlist);
3108        cgroup__put(trace->cgroup);
3109        trace->evlist = NULL;
3110        trace->live = false;
3111        return err;
3112{
3113        char errbuf[BUFSIZ];
3114
3115out_error_sched_stat_runtime:
3116        tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
3117        goto out_error;
3118
3119out_error_raw_syscalls:
3120        tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
3121        goto out_error;
3122
3123out_error_mmap:
3124        perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
3125        goto out_error;
3126
3127out_error_open:
3128        perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
3129
3130out_error:
3131        fprintf(trace->output, "%s\n", errbuf);
3132        goto out_delete_evlist;
3133
3134out_error_apply_filters:
3135        fprintf(trace->output,
3136                "Failed to set filter \"%s\" on event %s with %d (%s)\n",
3137                evsel->filter, perf_evsel__name(evsel), errno,
3138                str_error_r(errno, errbuf, sizeof(errbuf)));
3139        goto out_delete_evlist;
3140}
3141out_error_mem:
3142        fprintf(trace->output, "Not enough memory to run!\n");
3143        goto out_delete_evlist;
3144
3145out_errno:
3146        fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
3147        goto out_delete_evlist;
3148}
3149
3150static int trace__replay(struct trace *trace)
3151{
3152        const struct perf_evsel_str_handler handlers[] = {
3153                { "probe:vfs_getname",       trace__vfs_getname, },
3154        };
3155        struct perf_data data = {
3156                .path  = input_name,
3157                .mode  = PERF_DATA_MODE_READ,
3158                .force = trace->force,
3159        };
3160        struct perf_session *session;
3161        struct perf_evsel *evsel;
3162        int err = -1;
3163
3164        trace->tool.sample        = trace__process_sample;
3165        trace->tool.mmap          = perf_event__process_mmap;
3166        trace->tool.mmap2         = perf_event__process_mmap2;
3167        trace->tool.comm          = perf_event__process_comm;
3168        trace->tool.exit          = perf_event__process_exit;
3169        trace->tool.fork          = perf_event__process_fork;
3170        trace->tool.attr          = perf_event__process_attr;
3171        trace->tool.tracing_data  = perf_event__process_tracing_data;
3172        trace->tool.build_id      = perf_event__process_build_id;
3173        trace->tool.namespaces    = perf_event__process_namespaces;
3174
3175        trace->tool.ordered_events = true;
3176        trace->tool.ordering_requires_timestamps = true;
3177
3178        /* add tid to output */
3179        trace->multiple_threads = true;
3180
3181        session = perf_session__new(&data, false, &trace->tool);
3182        if (session == NULL)
3183                return -1;
3184
3185        if (trace->opts.target.pid)
3186                symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
3187
3188        if (trace->opts.target.tid)
3189                symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
3190
3191        if (symbol__init(&session->header.env) < 0)
3192                goto out;
3193
3194        trace->host = &session->machines.host;
3195
3196        err = perf_session__set_tracepoints_handlers(session, handlers);
3197        if (err)
3198                goto out;
3199
3200        evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3201                                                     "raw_syscalls:sys_enter");
3202        /* older kernels have syscalls tp versus raw_syscalls */
3203        if (evsel == NULL)
3204                evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3205                                                             "syscalls:sys_enter");
3206
3207        if (evsel &&
3208            (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
3209            perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
3210                pr_err("Error during initialize raw_syscalls:sys_enter event\n");
3211                goto out;
3212        }
3213
3214        evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3215                                                     "raw_syscalls:sys_exit");
3216        if (evsel == NULL)
3217                evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3218                                                             "syscalls:sys_exit");
3219        if (evsel &&
3220            (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
3221            perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
3222                pr_err("Error during initialize raw_syscalls:sys_exit event\n");
3223                goto out;
3224        }
3225
3226        evlist__for_each_entry(session->evlist, evsel) {
3227                if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
3228                    (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
3229                     evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
3230                     evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
3231                        evsel->handler = trace__pgfault;
3232        }
3233
3234        setup_pager();
3235
3236        err = perf_session__process_events(session);
3237        if (err)
3238                pr_err("Failed to process events, error %d", err);
3239
3240        else if (trace->summary)
3241                trace__fprintf_thread_summary(trace, trace->output);
3242
3243out:
3244        perf_session__delete(session);
3245
3246        return err;
3247}
3248
3249static size_t trace__fprintf_threads_header(FILE *fp)
3250{
3251        size_t printed;
3252
3253        printed  = fprintf(fp, "\n Summary of events:\n\n");
3254
3255        return printed;
3256}
3257
3258DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3259        struct stats    *stats;
3260        double          msecs;
3261        int             syscall;
3262)
3263{
3264        struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3265        struct stats *stats = source->priv;
3266
3267        entry->syscall = source->i;
3268        entry->stats   = stats;
3269        entry->msecs   = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3270}
3271
3272static size_t thread__dump_stats(struct thread_trace *ttrace,
3273                                 struct trace *trace, FILE *fp)
3274{
3275        size_t printed = 0;
3276        struct syscall *sc;
3277        struct rb_node *nd;
3278        DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
3279
3280        if (syscall_stats == NULL)
3281                return 0;
3282
3283        printed += fprintf(fp, "\n");
3284
3285        printed += fprintf(fp, "   syscall            calls    total       min       avg       max      stddev\n");
3286        printed += fprintf(fp, "                               (msec)    (msec)    (msec)    (msec)        (%%)\n");
3287        printed += fprintf(fp, "   --------------- -------- --------- --------- --------- ---------     ------\n");
3288
3289        resort_rb__for_each_entry(nd, syscall_stats) {
3290                struct stats *stats = syscall_stats_entry->stats;
3291                if (stats) {
3292                        double min = (double)(stats->min) / NSEC_PER_MSEC;
3293                        double max = (double)(stats->max) / NSEC_PER_MSEC;
3294                        double avg = avg_stats(stats);
3295                        double pct;
3296                        u64 n = (u64) stats->n;
3297
3298                        pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3299                        avg /= NSEC_PER_MSEC;
3300
3301                        sc = &trace->syscalls.table[syscall_stats_entry->syscall];
3302                        printed += fprintf(fp, "   %-15s", sc->name);
3303                        printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
3304                                           n, syscall_stats_entry->msecs, min, avg);
3305                        printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
3306                }
3307        }
3308
3309        resort_rb__delete(syscall_stats);
3310        printed += fprintf(fp, "\n\n");
3311
3312        return printed;
3313}
3314
3315static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
3316{
3317        size_t printed = 0;
3318        struct thread_trace *ttrace = thread__priv(thread);
3319        double ratio;
3320
3321        if (ttrace == NULL)
3322                return 0;
3323
3324        ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3325
3326        printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
3327        printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
3328        printed += fprintf(fp, "%.1f%%", ratio);
3329        if (ttrace->pfmaj)
3330                printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3331        if (ttrace->pfmin)
3332                printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
3333        if (trace->sched)
3334                printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3335        else if (fputc('\n', fp) != EOF)
3336                ++printed;
3337
3338        printed += thread__dump_stats(ttrace, trace, fp);
3339
3340        return printed;
3341}
3342
3343static unsigned long thread__nr_events(struct thread_trace *ttrace)
3344{
3345        return ttrace ? ttrace->nr_events : 0;
3346}
3347
3348DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3349        struct thread *thread;
3350)
3351{
3352        entry->thread = rb_entry(nd, struct thread, rb_node);
3353}
3354
3355static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3356{
3357        size_t printed = trace__fprintf_threads_header(fp);
3358        struct rb_node *nd;
3359        int i;
3360
3361        for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3362                DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3363
3364                if (threads == NULL) {
3365                        fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3366                        return 0;
3367                }
3368
3369                resort_rb__for_each_entry(nd, threads)
3370                        printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3371
3372                resort_rb__delete(threads);
3373        }
3374        return printed;
3375}
3376
3377static int trace__set_duration(const struct option *opt, const char *str,
3378                               int unset __maybe_unused)
3379{
3380        struct trace *trace = opt->value;
3381
3382        trace->duration_filter = atof(str);
3383        return 0;
3384}
3385
3386static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3387                                              int unset __maybe_unused)
3388{
3389        int ret = -1;
3390        size_t i;
3391        struct trace *trace = opt->value;
3392        /*
3393         * FIXME: introduce a intarray class, plain parse csv and create a
3394         * { int nr, int entries[] } struct...
3395         */
3396        struct intlist *list = intlist__new(str);
3397
3398        if (list == NULL)
3399                return -1;
3400
3401        i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3402        trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3403
3404        if (trace->filter_pids.entries == NULL)
3405                goto out;
3406
3407        trace->filter_pids.entries[0] = getpid();
3408
3409        for (i = 1; i < trace->filter_pids.nr; ++i)
3410                trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3411
3412        intlist__delete(list);
3413        ret = 0;
3414out:
3415        return ret;
3416}
3417
3418static int trace__open_output(struct trace *trace, const char *filename)
3419{
3420        struct stat st;
3421
3422        if (!stat(filename, &st) && st.st_size) {
3423                char oldname[PATH_MAX];
3424
3425                scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3426                unlink(oldname);
3427                rename(filename, oldname);
3428        }
3429
3430        trace->output = fopen(filename, "w");
3431
3432        return trace->output == NULL ? -errno : 0;
3433}
3434
3435static int parse_pagefaults(const struct option *opt, const char *str,
3436                            int unset __maybe_unused)
3437{
3438        int *trace_pgfaults = opt->value;
3439
3440        if (strcmp(str, "all") == 0)
3441                *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3442        else if (strcmp(str, "maj") == 0)
3443                *trace_pgfaults |= TRACE_PFMAJ;
3444        else if (strcmp(str, "min") == 0)
3445                *trace_pgfaults |= TRACE_PFMIN;
3446        else
3447                return -1;
3448
3449        return 0;
3450}
3451
3452static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3453{
3454        struct perf_evsel *evsel;
3455
3456        evlist__for_each_entry(evlist, evsel)
3457                evsel->handler = handler;
3458}
3459
3460static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist)
3461{
3462        struct perf_evsel *evsel;
3463
3464        evlist__for_each_entry(evlist, evsel) {
3465                if (evsel->priv || !evsel->tp_format)
3466                        continue;
3467
3468                if (strcmp(evsel->tp_format->system, "syscalls"))
3469                        continue;
3470
3471                if (perf_evsel__init_syscall_tp(evsel))
3472                        return -1;
3473
3474                if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3475                        struct syscall_tp *sc = evsel->priv;
3476
3477                        if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3478                                return -1;
3479                } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3480                        struct syscall_tp *sc = evsel->priv;
3481
3482                        if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3483                                return -1;
3484                }
3485        }
3486
3487        return 0;
3488}
3489
3490/*
3491 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3492 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3493 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3494 *
3495 * It'd be better to introduce a parse_options() variant that would return a
3496 * list with the terms it didn't match to an event...
3497 */
3498static int trace__parse_events_option(const struct option *opt, const char *str,
3499                                      int unset __maybe_unused)
3500{
3501        struct trace *trace = (struct trace *)opt->value;
3502        const char *s = str;
3503        char *sep = NULL, *lists[2] = { NULL, NULL, };
3504        int len = strlen(str) + 1, err = -1, list, idx;
3505        char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3506        char group_name[PATH_MAX];
3507        struct syscall_fmt *fmt;
3508
3509        if (strace_groups_dir == NULL)
3510                return -1;
3511
3512        if (*s == '!') {
3513                ++s;
3514                trace->not_ev_qualifier = true;
3515        }
3516
3517        while (1) {
3518                if ((sep = strchr(s, ',')) != NULL)
3519                        *sep = '\0';
3520
3521                list = 0;
3522                if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3523                    syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
3524                        list = 1;
3525                        goto do_concat;
3526                }
3527
3528                fmt = syscall_fmt__find_by_alias(s);
3529                if (fmt != NULL) {
3530                        list = 1;
3531                        s = fmt->name;
3532                } else {
3533                        path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3534                        if (access(group_name, R_OK) == 0)
3535                                list = 1;
3536                }
3537do_concat:
3538                if (lists[list]) {
3539                        sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3540                } else {
3541                        lists[list] = malloc(len);
3542                        if (lists[list] == NULL)
3543                                goto out;
3544                        strcpy(lists[list], s);
3545                }
3546
3547                if (!sep)
3548                        break;
3549
3550                *sep = ',';
3551                s = sep + 1;
3552        }
3553
3554        if (lists[1] != NULL) {
3555                struct strlist_config slist_config = {
3556                        .dirname = strace_groups_dir,
3557                };
3558
3559                trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3560                if (trace->ev_qualifier == NULL) {
3561                        fputs("Not enough memory to parse event qualifier", trace->output);
3562                        goto out;
3563                }
3564
3565                if (trace__validate_ev_qualifier(trace))
3566                        goto out;
3567                trace->trace_syscalls = true;
3568        }
3569
3570        err = 0;
3571
3572        if (lists[0]) {
3573                struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3574                                               "event selector. use 'perf list' to list available events",
3575                                               parse_events_option);
3576                err = parse_events_option(&o, lists[0], 0);
3577        }
3578out:
3579        if (sep)
3580                *sep = ',';
3581
3582        return err;
3583}
3584
3585static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3586{
3587        struct trace *trace = opt->value;
3588
3589        if (!list_empty(&trace->evlist->entries))
3590                return parse_cgroups(opt, str, unset);
3591
3592        trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3593
3594        return 0;
3595}
3596
3597static struct bpf_map *bpf__find_map_by_name(const char *name)
3598{
3599        struct bpf_object *obj, *tmp;
3600
3601        bpf_object__for_each_safe(obj, tmp) {
3602                struct bpf_map *map = bpf_object__find_map_by_name(obj, name);
3603                if (map)
3604                        return map;
3605
3606        }
3607
3608        return NULL;
3609}
3610
3611static void trace__set_bpf_map_filtered_pids(struct trace *trace)
3612{
3613        trace->filter_pids.map = bpf__find_map_by_name("pids_filtered");
3614}
3615
3616static void trace__set_bpf_map_syscalls(struct trace *trace)
3617{
3618        trace->syscalls.map = bpf__find_map_by_name("syscalls");
3619}
3620
3621static int trace__config(const char *var, const char *value, void *arg)
3622{
3623        struct trace *trace = arg;
3624        int err = 0;
3625
3626        if (!strcmp(var, "trace.add_events")) {
3627                struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3628                                               "event selector. use 'perf list' to list available events",
3629                                               parse_events_option);
3630                err = parse_events_option(&o, value, 0);
3631        } else if (!strcmp(var, "trace.show_timestamp")) {
3632                trace->show_tstamp = perf_config_bool(var, value);
3633        } else if (!strcmp(var, "trace.show_duration")) {
3634                trace->show_duration = perf_config_bool(var, value);
3635        } else if (!strcmp(var, "trace.show_arg_names")) {
3636                trace->show_arg_names = perf_config_bool(var, value);
3637                if (!trace->show_arg_names)
3638                        trace->show_zeros = true;
3639        } else if (!strcmp(var, "trace.show_zeros")) {
3640                bool new_show_zeros = perf_config_bool(var, value);
3641                if (!trace->show_arg_names && !new_show_zeros) {
3642                        pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n");
3643                        goto out;
3644                }
3645                trace->show_zeros = new_show_zeros;
3646        } else if (!strcmp(var, "trace.show_prefix")) {
3647                trace->show_string_prefix = perf_config_bool(var, value);
3648        } else if (!strcmp(var, "trace.no_inherit")) {
3649                trace->opts.no_inherit = perf_config_bool(var, value);
3650        } else if (!strcmp(var, "trace.args_alignment")) {
3651                int args_alignment = 0;
3652                if (perf_config_int(&args_alignment, var, value) == 0)
3653                        trace->args_alignment = args_alignment;
3654        }
3655out:
3656        return err;
3657}
3658
3659int cmd_trace(int argc, const char **argv)
3660{
3661        const char *trace_usage[] = {
3662                "perf trace [<options>] [<command>]",
3663                "perf trace [<options>] -- <command> [<options>]",
3664                "perf trace record [<options>] [<command>]",
3665                "perf trace record [<options>] -- <command> [<options>]",
3666                NULL
3667        };
3668        struct trace trace = {
3669                .syscalls = {
3670                        . max = -1,
3671                },
3672                .opts = {
3673                        .target = {
3674                                .uid       = UINT_MAX,
3675                                .uses_mmap = true,
3676                        },
3677                        .user_freq     = UINT_MAX,
3678                        .user_interval = ULLONG_MAX,
3679                        .no_buffering  = true,
3680                        .mmap_pages    = UINT_MAX,
3681                },
3682                .output = stderr,
3683                .show_comm = true,
3684                .show_tstamp = true,
3685                .show_duration = true,
3686                .show_arg_names = true,
3687                .args_alignment = 70,
3688                .trace_syscalls = false,
3689                .kernel_syscallchains = false,
3690                .max_stack = UINT_MAX,
3691                .max_events = ULONG_MAX,
3692        };
3693        const char *map_dump_str = NULL;
3694        const char *output_name = NULL;
3695        const struct option trace_options[] = {
3696        OPT_CALLBACK('e', "event", &trace, "event",
3697                     "event/syscall selector. use 'perf list' to list available events",
3698                     trace__parse_events_option),
3699        OPT_BOOLEAN(0, "comm", &trace.show_comm,
3700                    "show the thread COMM next to its id"),
3701        OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
3702        OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
3703                     trace__parse_events_option),
3704        OPT_STRING('o', "output", &output_name, "file", "output file name"),
3705        OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
3706        OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3707                    "trace events on existing process id"),
3708        OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
3709                    "trace events on existing thread id"),
3710        OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
3711                     "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
3712        OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
3713                    "system-wide collection from all CPUs"),
3714        OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
3715                    "list of cpus to monitor"),
3716        OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
3717                    "child tasks do not inherit counters"),
3718        OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3719                     "number of mmap data pages",
3720                     perf_evlist__parse_mmap_pages),
3721        OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
3722                   "user to profile"),
3723        OPT_CALLBACK(0, "duration", &trace, "float",
3724                     "show only events with duration > N.M ms",
3725                     trace__set_duration),
3726#ifdef HAVE_LIBBPF_SUPPORT
3727        OPT_STRING(0, "map-dump", &map_dump_str, "BPF map", "BPF map to periodically dump"),
3728#endif
3729        OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
3730        OPT_INCR('v', "verbose", &verbose, "be more verbose"),
3731        OPT_BOOLEAN('T', "time", &trace.full_time,
3732                    "Show full timestamp, not time relative to first start"),
3733        OPT_BOOLEAN(0, "failure", &trace.failure_only,
3734                    "Show only syscalls that failed"),
3735        OPT_BOOLEAN('s', "summary", &trace.summary_only,
3736                    "Show only syscall summary with statistics"),
3737        OPT_BOOLEAN('S', "with-summary", &trace.summary,
3738                    "Show all syscalls and summary with statistics"),
3739        OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3740                     "Trace pagefaults", parse_pagefaults, "maj"),
3741        OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
3742        OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
3743        OPT_CALLBACK(0, "call-graph", &trace.opts,
3744                     "record_mode[,record_size]", record_callchain_help,
3745                     &record_parse_callchain_opt),
3746        OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
3747                    "Show the kernel callchains on the syscall exit path"),
3748        OPT_ULONG(0, "max-events", &trace.max_events,
3749                "Set the maximum number of events to print, exit after that is reached. "),
3750        OPT_UINTEGER(0, "min-stack", &trace.min_stack,
3751                     "Set the minimum stack depth when parsing the callchain, "
3752                     "anything below the specified depth will be ignored."),
3753        OPT_UINTEGER(0, "max-stack", &trace.max_stack,
3754                     "Set the maximum stack depth when parsing the callchain, "
3755                     "anything beyond the specified depth will be ignored. "
3756                     "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
3757        OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
3758                        "Sort batch of events before processing, use if getting out of order events"),
3759        OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
3760                        "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
3761        OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
3762                        "per thread proc mmap processing timeout in ms"),
3763        OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
3764                     trace__parse_cgroups),
3765        OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
3766                     "ms to wait before starting measurement after program "
3767                     "start"),
3768        OPT_END()
3769        };
3770        bool __maybe_unused max_stack_user_set = true;
3771        bool mmap_pages_user_set = true;
3772        struct perf_evsel *evsel;
3773        const char * const trace_subcommands[] = { "record", NULL };
3774        int err = -1;
3775        char bf[BUFSIZ];
3776
3777        signal(SIGSEGV, sighandler_dump_stack);
3778        signal(SIGFPE, sighandler_dump_stack);
3779
3780        trace.evlist = perf_evlist__new();
3781        trace.sctbl = syscalltbl__new();
3782
3783        if (trace.evlist == NULL || trace.sctbl == NULL) {
3784                pr_err("Not enough memory to run!\n");
3785                err = -ENOMEM;
3786                goto out;
3787        }
3788
3789        err = perf_config(trace__config, &trace);
3790        if (err)
3791                goto out;
3792
3793        argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3794                                 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
3795
3796        if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
3797                usage_with_options_msg(trace_usage, trace_options,
3798                                       "cgroup monitoring only available in system-wide mode");
3799        }
3800
3801        evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
3802        if (IS_ERR(evsel)) {
3803                bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
3804                pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
3805                goto out;
3806        }
3807
3808        if (evsel) {
3809                trace.syscalls.events.augmented = evsel;
3810                trace__set_bpf_map_filtered_pids(&trace);
3811                trace__set_bpf_map_syscalls(&trace);
3812        }
3813
3814        err = bpf__setup_stdout(trace.evlist);
3815        if (err) {
3816                bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
3817                pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
3818                goto out;
3819        }
3820
3821        err = -1;
3822
3823        if (map_dump_str) {
3824                trace.dump.map = bpf__find_map_by_name(map_dump_str);
3825                if (trace.dump.map == NULL) {
3826                        pr_err("ERROR: BPF map \"%s\" not found\n", map_dump_str);
3827                        goto out;
3828                }
3829        }
3830
3831        if (trace.trace_pgfaults) {
3832                trace.opts.sample_address = true;
3833                trace.opts.sample_time = true;
3834        }
3835
3836        if (trace.opts.mmap_pages == UINT_MAX)
3837                mmap_pages_user_set = false;
3838
3839        if (trace.max_stack == UINT_MAX) {
3840                trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
3841                max_stack_user_set = false;
3842        }
3843
3844#ifdef HAVE_DWARF_UNWIND_SUPPORT
3845        if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
3846                record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
3847        }
3848#endif
3849
3850        if (callchain_param.enabled) {
3851                if (!mmap_pages_user_set && geteuid() == 0)
3852                        trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
3853
3854                symbol_conf.use_callchain = true;
3855        }
3856
3857        if (trace.evlist->nr_entries > 0) {
3858                evlist__set_evsel_handler(trace.evlist, trace__event_handler);
3859                if (evlist__set_syscall_tp_fields(trace.evlist)) {
3860                        perror("failed to set syscalls:* tracepoint fields");
3861                        goto out;
3862                }
3863        }
3864
3865        if (trace.sort_events) {
3866                ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
3867                ordered_events__set_copy_on_queue(&trace.oe.data, true);
3868        }
3869
3870        /*
3871         * If we are augmenting syscalls, then combine what we put in the
3872         * __augmented_syscalls__ BPF map with what is in the
3873         * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
3874         * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
3875         *
3876         * We'll switch to look at two BPF maps, one for sys_enter and the
3877         * other for sys_exit when we start augmenting the sys_exit paths with
3878         * buffers that are being copied from kernel to userspace, think 'read'
3879         * syscall.
3880         */
3881        if (trace.syscalls.events.augmented) {
3882                evlist__for_each_entry(trace.evlist, evsel) {
3883                        bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
3884
3885                        if (raw_syscalls_sys_exit) {
3886                                trace.raw_augmented_syscalls = true;
3887                                goto init_augmented_syscall_tp;
3888                        }
3889
3890                        if (trace.syscalls.events.augmented->priv == NULL &&
3891                            strstr(perf_evsel__name(evsel), "syscalls:sys_enter")) {
3892                                struct perf_evsel *augmented = trace.syscalls.events.augmented;
3893                                if (perf_evsel__init_augmented_syscall_tp(augmented, evsel) ||
3894                                    perf_evsel__init_augmented_syscall_tp_args(augmented))
3895                                        goto out;
3896                                augmented->handler = trace__sys_enter;
3897                        }
3898
3899                        if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
3900                                struct syscall_tp *sc;
3901init_augmented_syscall_tp:
3902                                if (perf_evsel__init_augmented_syscall_tp(evsel, evsel))
3903                                        goto out;
3904                                sc = evsel->priv;
3905                                /*
3906                                 * For now with BPF raw_augmented we hook into
3907                                 * raw_syscalls:sys_enter and there we get all
3908                                 * 6 syscall args plus the tracepoint common
3909                                 * fields and the syscall_nr (another long).
3910                                 * So we check if that is the case and if so
3911                                 * don't look after the sc->args_size but
3912                                 * always after the full raw_syscalls:sys_enter
3913                                 * payload, which is fixed.
3914                                 *
3915                                 * We'll revisit this later to pass
3916                                 * s->args_size to the BPF augmenter (now
3917                                 * tools/perf/examples/bpf/augmented_raw_syscalls.c,
3918                                 * so that it copies only what we need for each
3919                                 * syscall, like what happens when we use
3920                                 * syscalls:sys_enter_NAME, so that we reduce
3921                                 * the kernel/userspace traffic to just what is
3922                                 * needed for each syscall.
3923                                 */
3924                                if (trace.raw_augmented_syscalls)
3925                                        trace.raw_augmented_syscalls_args_size = (6 + 1) * sizeof(long) + sc->id.offset;
3926                                perf_evsel__init_augmented_syscall_tp_ret(evsel);
3927                                evsel->handler = trace__sys_exit;
3928                        }
3929                }
3930        }
3931
3932        if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
3933                return trace__record(&trace, argc-1, &argv[1]);
3934
3935        /* summary_only implies summary option, but don't overwrite summary if set */
3936        if (trace.summary_only)
3937                trace.summary = trace.summary_only;
3938
3939        if (!trace.trace_syscalls && !trace.trace_pgfaults &&
3940            trace.evlist->nr_entries == 0 /* Was --events used? */) {
3941                trace.trace_syscalls = true;
3942        }
3943
3944        if (output_name != NULL) {
3945                err = trace__open_output(&trace, output_name);
3946                if (err < 0) {
3947                        perror("failed to create output file");
3948                        goto out;
3949                }
3950        }
3951
3952        err = target__validate(&trace.opts.target);
3953        if (err) {
3954                target__strerror(&trace.opts.target, err, bf, sizeof(bf));
3955                fprintf(trace.output, "%s", bf);
3956                goto out_close;
3957        }
3958
3959        err = target__parse_uid(&trace.opts.target);
3960        if (err) {
3961                target__strerror(&trace.opts.target, err, bf, sizeof(bf));
3962                fprintf(trace.output, "%s", bf);
3963                goto out_close;
3964        }
3965
3966        if (!argc && target__none(&trace.opts.target))
3967                trace.opts.target.system_wide = true;
3968
3969        if (input_name)
3970                err = trace__replay(&trace);
3971        else
3972                err = trace__run(&trace, argc, argv);
3973
3974out_close:
3975        if (output_name != NULL)
3976                fclose(trace.output);
3977out:
3978        return err;
3979}
3980