linux/tools/perf/builtin-script.c
<<
>>
Prefs
   1#include "builtin.h"
   2
   3#include "perf.h"
   4#include "util/cache.h"
   5#include "util/debug.h"
   6#include <subcmd/exec-cmd.h>
   7#include "util/header.h"
   8#include <subcmd/parse-options.h>
   9#include "util/perf_regs.h"
  10#include "util/session.h"
  11#include "util/tool.h"
  12#include "util/symbol.h"
  13#include "util/thread.h"
  14#include "util/trace-event.h"
  15#include "util/util.h"
  16#include "util/evlist.h"
  17#include "util/evsel.h"
  18#include "util/sort.h"
  19#include "util/data.h"
  20#include "util/auxtrace.h"
  21#include "util/cpumap.h"
  22#include "util/thread_map.h"
  23#include "util/stat.h"
  24#include "util/thread-stack.h"
  25#include <linux/bitmap.h>
  26#include <linux/stringify.h>
  27#include <linux/time64.h>
  28#include "asm/bug.h"
  29#include "util/mem-events.h"
  30
  31static char const               *script_name;
  32static char const               *generate_script_lang;
  33static bool                     debug_mode;
  34static u64                      last_timestamp;
  35static u64                      nr_unordered;
  36static bool                     no_callchain;
  37static bool                     latency_format;
  38static bool                     system_wide;
  39static bool                     print_flags;
  40static bool                     nanosecs;
  41static const char               *cpu_list;
  42static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  43static struct perf_stat_config  stat_config;
  44
  45unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
  46
  47enum perf_output_field {
  48        PERF_OUTPUT_COMM            = 1U << 0,
  49        PERF_OUTPUT_TID             = 1U << 1,
  50        PERF_OUTPUT_PID             = 1U << 2,
  51        PERF_OUTPUT_TIME            = 1U << 3,
  52        PERF_OUTPUT_CPU             = 1U << 4,
  53        PERF_OUTPUT_EVNAME          = 1U << 5,
  54        PERF_OUTPUT_TRACE           = 1U << 6,
  55        PERF_OUTPUT_IP              = 1U << 7,
  56        PERF_OUTPUT_SYM             = 1U << 8,
  57        PERF_OUTPUT_DSO             = 1U << 9,
  58        PERF_OUTPUT_ADDR            = 1U << 10,
  59        PERF_OUTPUT_SYMOFFSET       = 1U << 11,
  60        PERF_OUTPUT_SRCLINE         = 1U << 12,
  61        PERF_OUTPUT_PERIOD          = 1U << 13,
  62        PERF_OUTPUT_IREGS           = 1U << 14,
  63        PERF_OUTPUT_BRSTACK         = 1U << 15,
  64        PERF_OUTPUT_BRSTACKSYM      = 1U << 16,
  65        PERF_OUTPUT_DATA_SRC        = 1U << 17,
  66        PERF_OUTPUT_WEIGHT          = 1U << 18,
  67        PERF_OUTPUT_BPF_OUTPUT      = 1U << 19,
  68        PERF_OUTPUT_CALLINDENT      = 1U << 20,
  69};
  70
  71struct output_option {
  72        const char *str;
  73        enum perf_output_field field;
  74} all_output_options[] = {
  75        {.str = "comm",  .field = PERF_OUTPUT_COMM},
  76        {.str = "tid",   .field = PERF_OUTPUT_TID},
  77        {.str = "pid",   .field = PERF_OUTPUT_PID},
  78        {.str = "time",  .field = PERF_OUTPUT_TIME},
  79        {.str = "cpu",   .field = PERF_OUTPUT_CPU},
  80        {.str = "event", .field = PERF_OUTPUT_EVNAME},
  81        {.str = "trace", .field = PERF_OUTPUT_TRACE},
  82        {.str = "ip",    .field = PERF_OUTPUT_IP},
  83        {.str = "sym",   .field = PERF_OUTPUT_SYM},
  84        {.str = "dso",   .field = PERF_OUTPUT_DSO},
  85        {.str = "addr",  .field = PERF_OUTPUT_ADDR},
  86        {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
  87        {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
  88        {.str = "period", .field = PERF_OUTPUT_PERIOD},
  89        {.str = "iregs", .field = PERF_OUTPUT_IREGS},
  90        {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
  91        {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
  92        {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
  93        {.str = "weight",   .field = PERF_OUTPUT_WEIGHT},
  94        {.str = "bpf-output",   .field = PERF_OUTPUT_BPF_OUTPUT},
  95        {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
  96};
  97
  98/* default set to maintain compatibility with current format */
  99static struct {
 100        bool user_set;
 101        bool wildcard_set;
 102        unsigned int print_ip_opts;
 103        u64 fields;
 104        u64 invalid_fields;
 105} output[PERF_TYPE_MAX] = {
 106
 107        [PERF_TYPE_HARDWARE] = {
 108                .user_set = false,
 109
 110                .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
 111                              PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
 112                              PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
 113                              PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
 114                              PERF_OUTPUT_PERIOD,
 115
 116                .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
 117        },
 118
 119        [PERF_TYPE_SOFTWARE] = {
 120                .user_set = false,
 121
 122                .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
 123                              PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
 124                              PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
 125                              PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
 126                              PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
 127
 128                .invalid_fields = PERF_OUTPUT_TRACE,
 129        },
 130
 131        [PERF_TYPE_TRACEPOINT] = {
 132                .user_set = false,
 133
 134                .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
 135                                  PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
 136                                  PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
 137        },
 138
 139        [PERF_TYPE_RAW] = {
 140                .user_set = false,
 141
 142                .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
 143                              PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
 144                              PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
 145                              PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
 146                              PERF_OUTPUT_PERIOD |  PERF_OUTPUT_ADDR |
 147                              PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
 148
 149                .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
 150        },
 151
 152        [PERF_TYPE_BREAKPOINT] = {
 153                .user_set = false,
 154
 155                .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
 156                              PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
 157                              PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
 158                              PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
 159                              PERF_OUTPUT_PERIOD,
 160
 161                .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
 162        },
 163};
 164
 165static bool output_set_by_user(void)
 166{
 167        int j;
 168        for (j = 0; j < PERF_TYPE_MAX; ++j) {
 169                if (output[j].user_set)
 170                        return true;
 171        }
 172        return false;
 173}
 174
 175static const char *output_field2str(enum perf_output_field field)
 176{
 177        int i, imax = ARRAY_SIZE(all_output_options);
 178        const char *str = "";
 179
 180        for (i = 0; i < imax; ++i) {
 181                if (all_output_options[i].field == field) {
 182                        str = all_output_options[i].str;
 183                        break;
 184                }
 185        }
 186        return str;
 187}
 188
 189#define PRINT_FIELD(x)  (output[attr->type].fields & PERF_OUTPUT_##x)
 190
 191static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
 192                                      u64 sample_type, const char *sample_msg,
 193                                      enum perf_output_field field,
 194                                      bool allow_user_set)
 195{
 196        struct perf_event_attr *attr = &evsel->attr;
 197        int type = attr->type;
 198        const char *evname;
 199
 200        if (attr->sample_type & sample_type)
 201                return 0;
 202
 203        if (output[type].user_set) {
 204                if (allow_user_set)
 205                        return 0;
 206                evname = perf_evsel__name(evsel);
 207                pr_err("Samples for '%s' event do not have %s attribute set. "
 208                       "Cannot print '%s' field.\n",
 209                       evname, sample_msg, output_field2str(field));
 210                return -1;
 211        }
 212
 213        /* user did not ask for it explicitly so remove from the default list */
 214        output[type].fields &= ~field;
 215        evname = perf_evsel__name(evsel);
 216        pr_debug("Samples for '%s' event do not have %s attribute set. "
 217                 "Skipping '%s' field.\n",
 218                 evname, sample_msg, output_field2str(field));
 219
 220        return 0;
 221}
 222
 223static int perf_evsel__check_stype(struct perf_evsel *evsel,
 224                                   u64 sample_type, const char *sample_msg,
 225                                   enum perf_output_field field)
 226{
 227        return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
 228                                          false);
 229}
 230
 231static int perf_evsel__check_attr(struct perf_evsel *evsel,
 232                                  struct perf_session *session)
 233{
 234        struct perf_event_attr *attr = &evsel->attr;
 235        bool allow_user_set;
 236
 237        if (perf_header__has_feat(&session->header, HEADER_STAT))
 238                return 0;
 239
 240        allow_user_set = perf_header__has_feat(&session->header,
 241                                               HEADER_AUXTRACE);
 242
 243        if (PRINT_FIELD(TRACE) &&
 244                !perf_session__has_traces(session, "record -R"))
 245                return -EINVAL;
 246
 247        if (PRINT_FIELD(IP)) {
 248                if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
 249                                            PERF_OUTPUT_IP))
 250                        return -EINVAL;
 251        }
 252
 253        if (PRINT_FIELD(ADDR) &&
 254                perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
 255                                           PERF_OUTPUT_ADDR, allow_user_set))
 256                return -EINVAL;
 257
 258        if (PRINT_FIELD(DATA_SRC) &&
 259                perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
 260                                        PERF_OUTPUT_DATA_SRC))
 261                return -EINVAL;
 262
 263        if (PRINT_FIELD(WEIGHT) &&
 264                perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
 265                                        PERF_OUTPUT_WEIGHT))
 266                return -EINVAL;
 267
 268        if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
 269                pr_err("Display of symbols requested but neither sample IP nor "
 270                           "sample address\nis selected. Hence, no addresses to convert "
 271                       "to symbols.\n");
 272                return -EINVAL;
 273        }
 274        if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
 275                pr_err("Display of offsets requested but symbol is not"
 276                       "selected.\n");
 277                return -EINVAL;
 278        }
 279        if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
 280                pr_err("Display of DSO requested but neither sample IP nor "
 281                           "sample address\nis selected. Hence, no addresses to convert "
 282                       "to DSO.\n");
 283                return -EINVAL;
 284        }
 285        if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
 286                pr_err("Display of source line number requested but sample IP is not\n"
 287                       "selected. Hence, no address to lookup the source line number.\n");
 288                return -EINVAL;
 289        }
 290
 291        if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
 292                perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
 293                                        PERF_OUTPUT_TID|PERF_OUTPUT_PID))
 294                return -EINVAL;
 295
 296        if (PRINT_FIELD(TIME) &&
 297                perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
 298                                        PERF_OUTPUT_TIME))
 299                return -EINVAL;
 300
 301        if (PRINT_FIELD(CPU) &&
 302                perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
 303                                           PERF_OUTPUT_CPU, allow_user_set))
 304                return -EINVAL;
 305
 306        if (PRINT_FIELD(PERIOD) &&
 307                perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
 308                                        PERF_OUTPUT_PERIOD))
 309                return -EINVAL;
 310
 311        if (PRINT_FIELD(IREGS) &&
 312                perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
 313                                        PERF_OUTPUT_IREGS))
 314                return -EINVAL;
 315
 316        return 0;
 317}
 318
 319static void set_print_ip_opts(struct perf_event_attr *attr)
 320{
 321        unsigned int type = attr->type;
 322
 323        output[type].print_ip_opts = 0;
 324        if (PRINT_FIELD(IP))
 325                output[type].print_ip_opts |= EVSEL__PRINT_IP;
 326
 327        if (PRINT_FIELD(SYM))
 328                output[type].print_ip_opts |= EVSEL__PRINT_SYM;
 329
 330        if (PRINT_FIELD(DSO))
 331                output[type].print_ip_opts |= EVSEL__PRINT_DSO;
 332
 333        if (PRINT_FIELD(SYMOFFSET))
 334                output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
 335
 336        if (PRINT_FIELD(SRCLINE))
 337                output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
 338}
 339
 340/*
 341 * verify all user requested events exist and the samples
 342 * have the expected data
 343 */
 344static int perf_session__check_output_opt(struct perf_session *session)
 345{
 346        unsigned int j;
 347        struct perf_evsel *evsel;
 348
 349        for (j = 0; j < PERF_TYPE_MAX; ++j) {
 350                evsel = perf_session__find_first_evtype(session, j);
 351
 352                /*
 353                 * even if fields is set to 0 (ie., show nothing) event must
 354                 * exist if user explicitly includes it on the command line
 355                 */
 356                if (!evsel && output[j].user_set && !output[j].wildcard_set) {
 357                        pr_err("%s events do not exist. "
 358                               "Remove corresponding -f option to proceed.\n",
 359                               event_type(j));
 360                        return -1;
 361                }
 362
 363                if (evsel && output[j].fields &&
 364                        perf_evsel__check_attr(evsel, session))
 365                        return -1;
 366
 367                if (evsel == NULL)
 368                        continue;
 369
 370                set_print_ip_opts(&evsel->attr);
 371        }
 372
 373        if (!no_callchain) {
 374                bool use_callchain = false;
 375                bool not_pipe = false;
 376
 377                evlist__for_each_entry(session->evlist, evsel) {
 378                        not_pipe = true;
 379                        if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
 380                                use_callchain = true;
 381                                break;
 382                        }
 383                }
 384                if (not_pipe && !use_callchain)
 385                        symbol_conf.use_callchain = false;
 386        }
 387
 388        /*
 389         * set default for tracepoints to print symbols only
 390         * if callchains are present
 391         */
 392        if (symbol_conf.use_callchain &&
 393            !output[PERF_TYPE_TRACEPOINT].user_set) {
 394                struct perf_event_attr *attr;
 395
 396                j = PERF_TYPE_TRACEPOINT;
 397
 398                evlist__for_each_entry(session->evlist, evsel) {
 399                        if (evsel->attr.type != j)
 400                                continue;
 401
 402                        attr = &evsel->attr;
 403
 404                        if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
 405                                output[j].fields |= PERF_OUTPUT_IP;
 406                                output[j].fields |= PERF_OUTPUT_SYM;
 407                                output[j].fields |= PERF_OUTPUT_DSO;
 408                                set_print_ip_opts(attr);
 409                                goto out;
 410                        }
 411                }
 412        }
 413
 414out:
 415        return 0;
 416}
 417
 418static void print_sample_iregs(struct perf_sample *sample,
 419                          struct perf_event_attr *attr)
 420{
 421        struct regs_dump *regs = &sample->intr_regs;
 422        uint64_t mask = attr->sample_regs_intr;
 423        unsigned i = 0, r;
 424
 425        if (!regs)
 426                return;
 427
 428        for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
 429                u64 val = regs->regs[i++];
 430                printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
 431        }
 432}
 433
 434static void print_sample_start(struct perf_sample *sample,
 435                               struct thread *thread,
 436                               struct perf_evsel *evsel)
 437{
 438        struct perf_event_attr *attr = &evsel->attr;
 439        unsigned long secs;
 440        unsigned long usecs;
 441        unsigned long long nsecs;
 442
 443        if (PRINT_FIELD(COMM)) {
 444                if (latency_format)
 445                        printf("%8.8s ", thread__comm_str(thread));
 446                else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
 447                        printf("%s ", thread__comm_str(thread));
 448                else
 449                        printf("%16s ", thread__comm_str(thread));
 450        }
 451
 452        if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
 453                printf("%5d/%-5d ", sample->pid, sample->tid);
 454        else if (PRINT_FIELD(PID))
 455                printf("%5d ", sample->pid);
 456        else if (PRINT_FIELD(TID))
 457                printf("%5d ", sample->tid);
 458
 459        if (PRINT_FIELD(CPU)) {
 460                if (latency_format)
 461                        printf("%3d ", sample->cpu);
 462                else
 463                        printf("[%03d] ", sample->cpu);
 464        }
 465
 466        if (PRINT_FIELD(TIME)) {
 467                nsecs = sample->time;
 468                secs = nsecs / NSEC_PER_SEC;
 469                nsecs -= secs * NSEC_PER_SEC;
 470                usecs = nsecs / NSEC_PER_USEC;
 471                if (nanosecs)
 472                        printf("%5lu.%09llu: ", secs, nsecs);
 473                else
 474                        printf("%5lu.%06lu: ", secs, usecs);
 475        }
 476}
 477
 478static inline char
 479mispred_str(struct branch_entry *br)
 480{
 481        if (!(br->flags.mispred  || br->flags.predicted))
 482                return '-';
 483
 484        return br->flags.predicted ? 'P' : 'M';
 485}
 486
 487static void print_sample_brstack(struct perf_sample *sample)
 488{
 489        struct branch_stack *br = sample->branch_stack;
 490        u64 i;
 491
 492        if (!(br && br->nr))
 493                return;
 494
 495        for (i = 0; i < br->nr; i++) {
 496                printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
 497                        br->entries[i].from,
 498                        br->entries[i].to,
 499                        mispred_str( br->entries + i),
 500                        br->entries[i].flags.in_tx? 'X' : '-',
 501                        br->entries[i].flags.abort? 'A' : '-',
 502                        br->entries[i].flags.cycles);
 503        }
 504}
 505
 506static void print_sample_brstacksym(struct perf_sample *sample,
 507                                    struct thread *thread)
 508{
 509        struct branch_stack *br = sample->branch_stack;
 510        struct addr_location alf, alt;
 511        u64 i, from, to;
 512
 513        if (!(br && br->nr))
 514                return;
 515
 516        for (i = 0; i < br->nr; i++) {
 517
 518                memset(&alf, 0, sizeof(alf));
 519                memset(&alt, 0, sizeof(alt));
 520                from = br->entries[i].from;
 521                to   = br->entries[i].to;
 522
 523                thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
 524                if (alf.map)
 525                        alf.sym = map__find_symbol(alf.map, alf.addr);
 526
 527                thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
 528                if (alt.map)
 529                        alt.sym = map__find_symbol(alt.map, alt.addr);
 530
 531                symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
 532                putchar('/');
 533                symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
 534                printf("/%c/%c/%c/%d ",
 535                        mispred_str( br->entries + i),
 536                        br->entries[i].flags.in_tx? 'X' : '-',
 537                        br->entries[i].flags.abort? 'A' : '-',
 538                        br->entries[i].flags.cycles);
 539        }
 540}
 541
 542
 543static void print_sample_addr(struct perf_sample *sample,
 544                          struct thread *thread,
 545                          struct perf_event_attr *attr)
 546{
 547        struct addr_location al;
 548
 549        printf("%16" PRIx64, sample->addr);
 550
 551        if (!sample_addr_correlates_sym(attr))
 552                return;
 553
 554        thread__resolve(thread, &al, sample);
 555
 556        if (PRINT_FIELD(SYM)) {
 557                printf(" ");
 558                if (PRINT_FIELD(SYMOFFSET))
 559                        symbol__fprintf_symname_offs(al.sym, &al, stdout);
 560                else
 561                        symbol__fprintf_symname(al.sym, stdout);
 562        }
 563
 564        if (PRINT_FIELD(DSO)) {
 565                printf(" (");
 566                map__fprintf_dsoname(al.map, stdout);
 567                printf(")");
 568        }
 569}
 570
 571static void print_sample_callindent(struct perf_sample *sample,
 572                                    struct perf_evsel *evsel,
 573                                    struct thread *thread,
 574                                    struct addr_location *al)
 575{
 576        struct perf_event_attr *attr = &evsel->attr;
 577        size_t depth = thread_stack__depth(thread);
 578        struct addr_location addr_al;
 579        const char *name = NULL;
 580        static int spacing;
 581        int len = 0;
 582        u64 ip = 0;
 583
 584        /*
 585         * The 'return' has already been popped off the stack so the depth has
 586         * to be adjusted to match the 'call'.
 587         */
 588        if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
 589                depth += 1;
 590
 591        if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
 592                if (sample_addr_correlates_sym(attr)) {
 593                        thread__resolve(thread, &addr_al, sample);
 594                        if (addr_al.sym)
 595                                name = addr_al.sym->name;
 596                        else
 597                                ip = sample->addr;
 598                } else {
 599                        ip = sample->addr;
 600                }
 601        } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
 602                if (al->sym)
 603                        name = al->sym->name;
 604                else
 605                        ip = sample->ip;
 606        }
 607
 608        if (name)
 609                len = printf("%*s%s", (int)depth * 4, "", name);
 610        else if (ip)
 611                len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
 612
 613        if (len < 0)
 614                return;
 615
 616        /*
 617         * Try to keep the output length from changing frequently so that the
 618         * output lines up more nicely.
 619         */
 620        if (len > spacing || (len && len < spacing - 52))
 621                spacing = round_up(len + 4, 32);
 622
 623        if (len < spacing)
 624                printf("%*s", spacing - len, "");
 625}
 626
 627static void print_sample_bts(struct perf_sample *sample,
 628                             struct perf_evsel *evsel,
 629                             struct thread *thread,
 630                             struct addr_location *al)
 631{
 632        struct perf_event_attr *attr = &evsel->attr;
 633        bool print_srcline_last = false;
 634
 635        if (PRINT_FIELD(CALLINDENT))
 636                print_sample_callindent(sample, evsel, thread, al);
 637
 638        /* print branch_from information */
 639        if (PRINT_FIELD(IP)) {
 640                unsigned int print_opts = output[attr->type].print_ip_opts;
 641                struct callchain_cursor *cursor = NULL;
 642
 643                if (symbol_conf.use_callchain && sample->callchain &&
 644                    thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
 645                                              sample, NULL, NULL, scripting_max_stack) == 0)
 646                        cursor = &callchain_cursor;
 647
 648                if (cursor == NULL) {
 649                        putchar(' ');
 650                        if (print_opts & EVSEL__PRINT_SRCLINE) {
 651                                print_srcline_last = true;
 652                                print_opts &= ~EVSEL__PRINT_SRCLINE;
 653                        }
 654                } else
 655                        putchar('\n');
 656
 657                sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
 658        }
 659
 660        /* print branch_to information */
 661        if (PRINT_FIELD(ADDR) ||
 662            ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
 663             !output[attr->type].user_set)) {
 664                printf(" => ");
 665                print_sample_addr(sample, thread, attr);
 666        }
 667
 668        if (print_srcline_last)
 669                map__fprintf_srcline(al->map, al->addr, "\n  ", stdout);
 670
 671        printf("\n");
 672}
 673
 674static struct {
 675        u32 flags;
 676        const char *name;
 677} sample_flags[] = {
 678        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
 679        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
 680        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
 681        {PERF_IP_FLAG_BRANCH, "jmp"},
 682        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
 683        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
 684        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
 685        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
 686        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
 687        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
 688        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
 689        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
 690        {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
 691        {0, NULL}
 692};
 693
 694static void print_sample_flags(u32 flags)
 695{
 696        const char *chars = PERF_IP_FLAG_CHARS;
 697        const int n = strlen(PERF_IP_FLAG_CHARS);
 698        bool in_tx = flags & PERF_IP_FLAG_IN_TX;
 699        const char *name = NULL;
 700        char str[33];
 701        int i, pos = 0;
 702
 703        for (i = 0; sample_flags[i].name ; i++) {
 704                if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
 705                        name = sample_flags[i].name;
 706                        break;
 707                }
 708        }
 709
 710        for (i = 0; i < n; i++, flags >>= 1) {
 711                if (flags & 1)
 712                        str[pos++] = chars[i];
 713        }
 714        for (; i < 32; i++, flags >>= 1) {
 715                if (flags & 1)
 716                        str[pos++] = '?';
 717        }
 718        str[pos] = 0;
 719
 720        if (name)
 721                printf("  %-7s%4s ", name, in_tx ? "(x)" : "");
 722        else
 723                printf("  %-11s ", str);
 724}
 725
 726struct printer_data {
 727        int line_no;
 728        bool hit_nul;
 729        bool is_printable;
 730};
 731
 732static void
 733print_sample_bpf_output_printer(enum binary_printer_ops op,
 734                                unsigned int val,
 735                                void *extra)
 736{
 737        unsigned char ch = (unsigned char)val;
 738        struct printer_data *printer_data = extra;
 739
 740        switch (op) {
 741        case BINARY_PRINT_DATA_BEGIN:
 742                printf("\n");
 743                break;
 744        case BINARY_PRINT_LINE_BEGIN:
 745                printf("%17s", !printer_data->line_no ? "BPF output:" :
 746                                                        "           ");
 747                break;
 748        case BINARY_PRINT_ADDR:
 749                printf(" %04x:", val);
 750                break;
 751        case BINARY_PRINT_NUM_DATA:
 752                printf(" %02x", val);
 753                break;
 754        case BINARY_PRINT_NUM_PAD:
 755                printf("   ");
 756                break;
 757        case BINARY_PRINT_SEP:
 758                printf("  ");
 759                break;
 760        case BINARY_PRINT_CHAR_DATA:
 761                if (printer_data->hit_nul && ch)
 762                        printer_data->is_printable = false;
 763
 764                if (!isprint(ch)) {
 765                        printf("%c", '.');
 766
 767                        if (!printer_data->is_printable)
 768                                break;
 769
 770                        if (ch == '\0')
 771                                printer_data->hit_nul = true;
 772                        else
 773                                printer_data->is_printable = false;
 774                } else {
 775                        printf("%c", ch);
 776                }
 777                break;
 778        case BINARY_PRINT_CHAR_PAD:
 779                printf(" ");
 780                break;
 781        case BINARY_PRINT_LINE_END:
 782                printf("\n");
 783                printer_data->line_no++;
 784                break;
 785        case BINARY_PRINT_DATA_END:
 786        default:
 787                break;
 788        }
 789}
 790
 791static void print_sample_bpf_output(struct perf_sample *sample)
 792{
 793        unsigned int nr_bytes = sample->raw_size;
 794        struct printer_data printer_data = {0, false, true};
 795
 796        print_binary(sample->raw_data, nr_bytes, 8,
 797                     print_sample_bpf_output_printer, &printer_data);
 798
 799        if (printer_data.is_printable && printer_data.hit_nul)
 800                printf("%17s \"%s\"\n", "BPF string:",
 801                       (char *)(sample->raw_data));
 802}
 803
 804struct perf_script {
 805        struct perf_tool        tool;
 806        struct perf_session     *session;
 807        bool                    show_task_events;
 808        bool                    show_mmap_events;
 809        bool                    show_switch_events;
 810        bool                    allocated;
 811        struct cpu_map          *cpus;
 812        struct thread_map       *threads;
 813        int                     name_width;
 814};
 815
 816static int perf_evlist__max_name_len(struct perf_evlist *evlist)
 817{
 818        struct perf_evsel *evsel;
 819        int max = 0;
 820
 821        evlist__for_each_entry(evlist, evsel) {
 822                int len = strlen(perf_evsel__name(evsel));
 823
 824                max = MAX(len, max);
 825        }
 826
 827        return max;
 828}
 829
 830static size_t data_src__printf(u64 data_src)
 831{
 832        struct mem_info mi = { .data_src.val = data_src };
 833        char decode[100];
 834        char out[100];
 835        static int maxlen;
 836        int len;
 837
 838        perf_script__meminfo_scnprintf(decode, 100, &mi);
 839
 840        len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
 841        if (maxlen < len)
 842                maxlen = len;
 843
 844        return printf("%-*s", maxlen, out);
 845}
 846
 847static void process_event(struct perf_script *script,
 848                          struct perf_sample *sample, struct perf_evsel *evsel,
 849                          struct addr_location *al)
 850{
 851        struct thread *thread = al->thread;
 852        struct perf_event_attr *attr = &evsel->attr;
 853
 854        if (output[attr->type].fields == 0)
 855                return;
 856
 857        print_sample_start(sample, thread, evsel);
 858
 859        if (PRINT_FIELD(PERIOD))
 860                printf("%10" PRIu64 " ", sample->period);
 861
 862        if (PRINT_FIELD(EVNAME)) {
 863                const char *evname = perf_evsel__name(evsel);
 864
 865                if (!script->name_width)
 866                        script->name_width = perf_evlist__max_name_len(script->session->evlist);
 867
 868                printf("%*s: ", script->name_width,
 869                       evname ? evname : "[unknown]");
 870        }
 871
 872        if (print_flags)
 873                print_sample_flags(sample->flags);
 874
 875        if (is_bts_event(attr)) {
 876                print_sample_bts(sample, evsel, thread, al);
 877                return;
 878        }
 879
 880        if (PRINT_FIELD(TRACE))
 881                event_format__print(evsel->tp_format, sample->cpu,
 882                                    sample->raw_data, sample->raw_size);
 883        if (PRINT_FIELD(ADDR))
 884                print_sample_addr(sample, thread, attr);
 885
 886        if (PRINT_FIELD(DATA_SRC))
 887                data_src__printf(sample->data_src);
 888
 889        if (PRINT_FIELD(WEIGHT))
 890                printf("%16" PRIu64, sample->weight);
 891
 892        if (PRINT_FIELD(IP)) {
 893                struct callchain_cursor *cursor = NULL;
 894
 895                if (symbol_conf.use_callchain && sample->callchain &&
 896                    thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
 897                                              sample, NULL, NULL, scripting_max_stack) == 0)
 898                        cursor = &callchain_cursor;
 899
 900                putchar(cursor ? '\n' : ' ');
 901                sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
 902        }
 903
 904        if (PRINT_FIELD(IREGS))
 905                print_sample_iregs(sample, attr);
 906
 907        if (PRINT_FIELD(BRSTACK))
 908                print_sample_brstack(sample);
 909        else if (PRINT_FIELD(BRSTACKSYM))
 910                print_sample_brstacksym(sample, thread);
 911
 912        if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
 913                print_sample_bpf_output(sample);
 914
 915        printf("\n");
 916}
 917
 918static struct scripting_ops     *scripting_ops;
 919
 920static void __process_stat(struct perf_evsel *counter, u64 tstamp)
 921{
 922        int nthreads = thread_map__nr(counter->threads);
 923        int ncpus = perf_evsel__nr_cpus(counter);
 924        int cpu, thread;
 925        static int header_printed;
 926
 927        if (counter->system_wide)
 928                nthreads = 1;
 929
 930        if (!header_printed) {
 931                printf("%3s %8s %15s %15s %15s %15s %s\n",
 932                       "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
 933                header_printed = 1;
 934        }
 935
 936        for (thread = 0; thread < nthreads; thread++) {
 937                for (cpu = 0; cpu < ncpus; cpu++) {
 938                        struct perf_counts_values *counts;
 939
 940                        counts = perf_counts(counter->counts, cpu, thread);
 941
 942                        printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
 943                                counter->cpus->map[cpu],
 944                                thread_map__pid(counter->threads, thread),
 945                                counts->val,
 946                                counts->ena,
 947                                counts->run,
 948                                tstamp,
 949                                perf_evsel__name(counter));
 950                }
 951        }
 952}
 953
 954static void process_stat(struct perf_evsel *counter, u64 tstamp)
 955{
 956        if (scripting_ops && scripting_ops->process_stat)
 957                scripting_ops->process_stat(&stat_config, counter, tstamp);
 958        else
 959                __process_stat(counter, tstamp);
 960}
 961
 962static void process_stat_interval(u64 tstamp)
 963{
 964        if (scripting_ops && scripting_ops->process_stat_interval)
 965                scripting_ops->process_stat_interval(tstamp);
 966}
 967
 968static void setup_scripting(void)
 969{
 970        setup_perl_scripting();
 971        setup_python_scripting();
 972}
 973
 974static int flush_scripting(void)
 975{
 976        return scripting_ops ? scripting_ops->flush_script() : 0;
 977}
 978
 979static int cleanup_scripting(void)
 980{
 981        pr_debug("\nperf script stopped\n");
 982
 983        return scripting_ops ? scripting_ops->stop_script() : 0;
 984}
 985
 986static int process_sample_event(struct perf_tool *tool,
 987                                union perf_event *event,
 988                                struct perf_sample *sample,
 989                                struct perf_evsel *evsel,
 990                                struct machine *machine)
 991{
 992        struct perf_script *scr = container_of(tool, struct perf_script, tool);
 993        struct addr_location al;
 994
 995        if (debug_mode) {
 996                if (sample->time < last_timestamp) {
 997                        pr_err("Samples misordered, previous: %" PRIu64
 998                                " this: %" PRIu64 "\n", last_timestamp,
 999                                sample->time);
1000                        nr_unordered++;
1001                }
1002                last_timestamp = sample->time;
1003                return 0;
1004        }
1005
1006        if (machine__resolve(machine, &al, sample) < 0) {
1007                pr_err("problem processing %d event, skipping it.\n",
1008                       event->header.type);
1009                return -1;
1010        }
1011
1012        if (al.filtered)
1013                goto out_put;
1014
1015        if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
1016                goto out_put;
1017
1018        if (scripting_ops)
1019                scripting_ops->process_event(event, sample, evsel, &al);
1020        else
1021                process_event(scr, sample, evsel, &al);
1022
1023out_put:
1024        addr_location__put(&al);
1025        return 0;
1026}
1027
1028static int process_attr(struct perf_tool *tool, union perf_event *event,
1029                        struct perf_evlist **pevlist)
1030{
1031        struct perf_script *scr = container_of(tool, struct perf_script, tool);
1032        struct perf_evlist *evlist;
1033        struct perf_evsel *evsel, *pos;
1034        int err;
1035
1036        err = perf_event__process_attr(tool, event, pevlist);
1037        if (err)
1038                return err;
1039
1040        evlist = *pevlist;
1041        evsel = perf_evlist__last(*pevlist);
1042
1043        if (evsel->attr.type >= PERF_TYPE_MAX)
1044                return 0;
1045
1046        evlist__for_each_entry(evlist, pos) {
1047                if (pos->attr.type == evsel->attr.type && pos != evsel)
1048                        return 0;
1049        }
1050
1051        set_print_ip_opts(&evsel->attr);
1052
1053        if (evsel->attr.sample_type)
1054                err = perf_evsel__check_attr(evsel, scr->session);
1055
1056        return err;
1057}
1058
1059static int process_comm_event(struct perf_tool *tool,
1060                              union perf_event *event,
1061                              struct perf_sample *sample,
1062                              struct machine *machine)
1063{
1064        struct thread *thread;
1065        struct perf_script *script = container_of(tool, struct perf_script, tool);
1066        struct perf_session *session = script->session;
1067        struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1068        int ret = -1;
1069
1070        thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1071        if (thread == NULL) {
1072                pr_debug("problem processing COMM event, skipping it.\n");
1073                return -1;
1074        }
1075
1076        if (perf_event__process_comm(tool, event, sample, machine) < 0)
1077                goto out;
1078
1079        if (!evsel->attr.sample_id_all) {
1080                sample->cpu = 0;
1081                sample->time = 0;
1082                sample->tid = event->comm.tid;
1083                sample->pid = event->comm.pid;
1084        }
1085        print_sample_start(sample, thread, evsel);
1086        perf_event__fprintf(event, stdout);
1087        ret = 0;
1088out:
1089        thread__put(thread);
1090        return ret;
1091}
1092
1093static int process_fork_event(struct perf_tool *tool,
1094                              union perf_event *event,
1095                              struct perf_sample *sample,
1096                              struct machine *machine)
1097{
1098        struct thread *thread;
1099        struct perf_script *script = container_of(tool, struct perf_script, tool);
1100        struct perf_session *session = script->session;
1101        struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1102
1103        if (perf_event__process_fork(tool, event, sample, machine) < 0)
1104                return -1;
1105
1106        thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1107        if (thread == NULL) {
1108                pr_debug("problem processing FORK event, skipping it.\n");
1109                return -1;
1110        }
1111
1112        if (!evsel->attr.sample_id_all) {
1113                sample->cpu = 0;
1114                sample->time = event->fork.time;
1115                sample->tid = event->fork.tid;
1116                sample->pid = event->fork.pid;
1117        }
1118        print_sample_start(sample, thread, evsel);
1119        perf_event__fprintf(event, stdout);
1120        thread__put(thread);
1121
1122        return 0;
1123}
1124static int process_exit_event(struct perf_tool *tool,
1125                              union perf_event *event,
1126                              struct perf_sample *sample,
1127                              struct machine *machine)
1128{
1129        int err = 0;
1130        struct thread *thread;
1131        struct perf_script *script = container_of(tool, struct perf_script, tool);
1132        struct perf_session *session = script->session;
1133        struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1134
1135        thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1136        if (thread == NULL) {
1137                pr_debug("problem processing EXIT event, skipping it.\n");
1138                return -1;
1139        }
1140
1141        if (!evsel->attr.sample_id_all) {
1142                sample->cpu = 0;
1143                sample->time = 0;
1144                sample->tid = event->fork.tid;
1145                sample->pid = event->fork.pid;
1146        }
1147        print_sample_start(sample, thread, evsel);
1148        perf_event__fprintf(event, stdout);
1149
1150        if (perf_event__process_exit(tool, event, sample, machine) < 0)
1151                err = -1;
1152
1153        thread__put(thread);
1154        return err;
1155}
1156
1157static int process_mmap_event(struct perf_tool *tool,
1158                              union perf_event *event,
1159                              struct perf_sample *sample,
1160                              struct machine *machine)
1161{
1162        struct thread *thread;
1163        struct perf_script *script = container_of(tool, struct perf_script, tool);
1164        struct perf_session *session = script->session;
1165        struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1166
1167        if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1168                return -1;
1169
1170        thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1171        if (thread == NULL) {
1172                pr_debug("problem processing MMAP event, skipping it.\n");
1173                return -1;
1174        }
1175
1176        if (!evsel->attr.sample_id_all) {
1177                sample->cpu = 0;
1178                sample->time = 0;
1179                sample->tid = event->mmap.tid;
1180                sample->pid = event->mmap.pid;
1181        }
1182        print_sample_start(sample, thread, evsel);
1183        perf_event__fprintf(event, stdout);
1184        thread__put(thread);
1185        return 0;
1186}
1187
1188static int process_mmap2_event(struct perf_tool *tool,
1189                              union perf_event *event,
1190                              struct perf_sample *sample,
1191                              struct machine *machine)
1192{
1193        struct thread *thread;
1194        struct perf_script *script = container_of(tool, struct perf_script, tool);
1195        struct perf_session *session = script->session;
1196        struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1197
1198        if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1199                return -1;
1200
1201        thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1202        if (thread == NULL) {
1203                pr_debug("problem processing MMAP2 event, skipping it.\n");
1204                return -1;
1205        }
1206
1207        if (!evsel->attr.sample_id_all) {
1208                sample->cpu = 0;
1209                sample->time = 0;
1210                sample->tid = event->mmap2.tid;
1211                sample->pid = event->mmap2.pid;
1212        }
1213        print_sample_start(sample, thread, evsel);
1214        perf_event__fprintf(event, stdout);
1215        thread__put(thread);
1216        return 0;
1217}
1218
1219static int process_switch_event(struct perf_tool *tool,
1220                                union perf_event *event,
1221                                struct perf_sample *sample,
1222                                struct machine *machine)
1223{
1224        struct thread *thread;
1225        struct perf_script *script = container_of(tool, struct perf_script, tool);
1226        struct perf_session *session = script->session;
1227        struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1228
1229        if (perf_event__process_switch(tool, event, sample, machine) < 0)
1230                return -1;
1231
1232        thread = machine__findnew_thread(machine, sample->pid,
1233                                         sample->tid);
1234        if (thread == NULL) {
1235                pr_debug("problem processing SWITCH event, skipping it.\n");
1236                return -1;
1237        }
1238
1239        print_sample_start(sample, thread, evsel);
1240        perf_event__fprintf(event, stdout);
1241        thread__put(thread);
1242        return 0;
1243}
1244
1245static void sig_handler(int sig __maybe_unused)
1246{
1247        session_done = 1;
1248}
1249
1250static int __cmd_script(struct perf_script *script)
1251{
1252        int ret;
1253
1254        signal(SIGINT, sig_handler);
1255
1256        /* override event processing functions */
1257        if (script->show_task_events) {
1258                script->tool.comm = process_comm_event;
1259                script->tool.fork = process_fork_event;
1260                script->tool.exit = process_exit_event;
1261        }
1262        if (script->show_mmap_events) {
1263                script->tool.mmap = process_mmap_event;
1264                script->tool.mmap2 = process_mmap2_event;
1265        }
1266        if (script->show_switch_events)
1267                script->tool.context_switch = process_switch_event;
1268
1269        ret = perf_session__process_events(script->session);
1270
1271        if (debug_mode)
1272                pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
1273
1274        return ret;
1275}
1276
1277struct script_spec {
1278        struct list_head        node;
1279        struct scripting_ops    *ops;
1280        char                    spec[0];
1281};
1282
1283static LIST_HEAD(script_specs);
1284
1285static struct script_spec *script_spec__new(const char *spec,
1286                                            struct scripting_ops *ops)
1287{
1288        struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1289
1290        if (s != NULL) {
1291                strcpy(s->spec, spec);
1292                s->ops = ops;
1293        }
1294
1295        return s;
1296}
1297
1298static void script_spec__add(struct script_spec *s)
1299{
1300        list_add_tail(&s->node, &script_specs);
1301}
1302
1303static struct script_spec *script_spec__find(const char *spec)
1304{
1305        struct script_spec *s;
1306
1307        list_for_each_entry(s, &script_specs, node)
1308                if (strcasecmp(s->spec, spec) == 0)
1309                        return s;
1310        return NULL;
1311}
1312
1313int script_spec_register(const char *spec, struct scripting_ops *ops)
1314{
1315        struct script_spec *s;
1316
1317        s = script_spec__find(spec);
1318        if (s)
1319                return -1;
1320
1321        s = script_spec__new(spec, ops);
1322        if (!s)
1323                return -1;
1324        else
1325                script_spec__add(s);
1326
1327        return 0;
1328}
1329
1330static struct scripting_ops *script_spec__lookup(const char *spec)
1331{
1332        struct script_spec *s = script_spec__find(spec);
1333        if (!s)
1334                return NULL;
1335
1336        return s->ops;
1337}
1338
1339static void list_available_languages(void)
1340{
1341        struct script_spec *s;
1342
1343        fprintf(stderr, "\n");
1344        fprintf(stderr, "Scripting language extensions (used in "
1345                "perf script -s [spec:]script.[spec]):\n\n");
1346
1347        list_for_each_entry(s, &script_specs, node)
1348                fprintf(stderr, "  %-42s [%s]\n", s->spec, s->ops->name);
1349
1350        fprintf(stderr, "\n");
1351}
1352
1353static int parse_scriptname(const struct option *opt __maybe_unused,
1354                            const char *str, int unset __maybe_unused)
1355{
1356        char spec[PATH_MAX];
1357        const char *script, *ext;
1358        int len;
1359
1360        if (strcmp(str, "lang") == 0) {
1361                list_available_languages();
1362                exit(0);
1363        }
1364
1365        script = strchr(str, ':');
1366        if (script) {
1367                len = script - str;
1368                if (len >= PATH_MAX) {
1369                        fprintf(stderr, "invalid language specifier");
1370                        return -1;
1371                }
1372                strncpy(spec, str, len);
1373                spec[len] = '\0';
1374                scripting_ops = script_spec__lookup(spec);
1375                if (!scripting_ops) {
1376                        fprintf(stderr, "invalid language specifier");
1377                        return -1;
1378                }
1379                script++;
1380        } else {
1381                script = str;
1382                ext = strrchr(script, '.');
1383                if (!ext) {
1384                        fprintf(stderr, "invalid script extension");
1385                        return -1;
1386                }
1387                scripting_ops = script_spec__lookup(++ext);
1388                if (!scripting_ops) {
1389                        fprintf(stderr, "invalid script extension");
1390                        return -1;
1391                }
1392        }
1393
1394        script_name = strdup(script);
1395
1396        return 0;
1397}
1398
1399static int parse_output_fields(const struct option *opt __maybe_unused,
1400                            const char *arg, int unset __maybe_unused)
1401{
1402        char *tok;
1403        int i, imax = ARRAY_SIZE(all_output_options);
1404        int j;
1405        int rc = 0;
1406        char *str = strdup(arg);
1407        int type = -1;
1408
1409        if (!str)
1410                return -ENOMEM;
1411
1412        /* first word can state for which event type the user is specifying
1413         * the fields. If no type exists, the specified fields apply to all
1414         * event types found in the file minus the invalid fields for a type.
1415         */
1416        tok = strchr(str, ':');
1417        if (tok) {
1418                *tok = '\0';
1419                tok++;
1420                if (!strcmp(str, "hw"))
1421                        type = PERF_TYPE_HARDWARE;
1422                else if (!strcmp(str, "sw"))
1423                        type = PERF_TYPE_SOFTWARE;
1424                else if (!strcmp(str, "trace"))
1425                        type = PERF_TYPE_TRACEPOINT;
1426                else if (!strcmp(str, "raw"))
1427                        type = PERF_TYPE_RAW;
1428                else if (!strcmp(str, "break"))
1429                        type = PERF_TYPE_BREAKPOINT;
1430                else {
1431                        fprintf(stderr, "Invalid event type in field string.\n");
1432                        rc = -EINVAL;
1433                        goto out;
1434                }
1435
1436                if (output[type].user_set)
1437                        pr_warning("Overriding previous field request for %s events.\n",
1438                                   event_type(type));
1439
1440                output[type].fields = 0;
1441                output[type].user_set = true;
1442                output[type].wildcard_set = false;
1443
1444        } else {
1445                tok = str;
1446                if (strlen(str) == 0) {
1447                        fprintf(stderr,
1448                                "Cannot set fields to 'none' for all event types.\n");
1449                        rc = -EINVAL;
1450                        goto out;
1451                }
1452
1453                if (output_set_by_user())
1454                        pr_warning("Overriding previous field request for all events.\n");
1455
1456                for (j = 0; j < PERF_TYPE_MAX; ++j) {
1457                        output[j].fields = 0;
1458                        output[j].user_set = true;
1459                        output[j].wildcard_set = true;
1460                }
1461        }
1462
1463        for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
1464                for (i = 0; i < imax; ++i) {
1465                        if (strcmp(tok, all_output_options[i].str) == 0)
1466                                break;
1467                }
1468                if (i == imax && strcmp(tok, "flags") == 0) {
1469                        print_flags = true;
1470                        continue;
1471                }
1472                if (i == imax) {
1473                        fprintf(stderr, "Invalid field requested.\n");
1474                        rc = -EINVAL;
1475                        goto out;
1476                }
1477
1478                if (type == -1) {
1479                        /* add user option to all events types for
1480                         * which it is valid
1481                         */
1482                        for (j = 0; j < PERF_TYPE_MAX; ++j) {
1483                                if (output[j].invalid_fields & all_output_options[i].field) {
1484                                        pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1485                                                   all_output_options[i].str, event_type(j));
1486                                } else
1487                                        output[j].fields |= all_output_options[i].field;
1488                        }
1489                } else {
1490                        if (output[type].invalid_fields & all_output_options[i].field) {
1491                                fprintf(stderr, "\'%s\' not valid for %s events.\n",
1492                                         all_output_options[i].str, event_type(type));
1493
1494                                rc = -EINVAL;
1495                                goto out;
1496                        }
1497                        output[type].fields |= all_output_options[i].field;
1498                }
1499        }
1500
1501        if (type >= 0) {
1502                if (output[type].fields == 0) {
1503                        pr_debug("No fields requested for %s type. "
1504                                 "Events will not be displayed.\n", event_type(type));
1505                }
1506        }
1507
1508out:
1509        free(str);
1510        return rc;
1511}
1512
1513/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1514static int is_directory(const char *base_path, const struct dirent *dent)
1515{
1516        char path[PATH_MAX];
1517        struct stat st;
1518
1519        sprintf(path, "%s/%s", base_path, dent->d_name);
1520        if (stat(path, &st))
1521                return 0;
1522
1523        return S_ISDIR(st.st_mode);
1524}
1525
1526#define for_each_lang(scripts_path, scripts_dir, lang_dirent)           \
1527        while ((lang_dirent = readdir(scripts_dir)) != NULL)            \
1528                if ((lang_dirent->d_type == DT_DIR ||                   \
1529                     (lang_dirent->d_type == DT_UNKNOWN &&              \
1530                      is_directory(scripts_path, lang_dirent))) &&      \
1531                    (strcmp(lang_dirent->d_name, ".")) &&               \
1532                    (strcmp(lang_dirent->d_name, "..")))
1533
1534#define for_each_script(lang_path, lang_dir, script_dirent)             \
1535        while ((script_dirent = readdir(lang_dir)) != NULL)             \
1536                if (script_dirent->d_type != DT_DIR &&                  \
1537                    (script_dirent->d_type != DT_UNKNOWN ||             \
1538                     !is_directory(lang_path, script_dirent)))
1539
1540
1541#define RECORD_SUFFIX                   "-record"
1542#define REPORT_SUFFIX                   "-report"
1543
1544struct script_desc {
1545        struct list_head        node;
1546        char                    *name;
1547        char                    *half_liner;
1548        char                    *args;
1549};
1550
1551static LIST_HEAD(script_descs);
1552
1553static struct script_desc *script_desc__new(const char *name)
1554{
1555        struct script_desc *s = zalloc(sizeof(*s));
1556
1557        if (s != NULL && name)
1558                s->name = strdup(name);
1559
1560        return s;
1561}
1562
1563static void script_desc__delete(struct script_desc *s)
1564{
1565        zfree(&s->name);
1566        zfree(&s->half_liner);
1567        zfree(&s->args);
1568        free(s);
1569}
1570
1571static void script_desc__add(struct script_desc *s)
1572{
1573        list_add_tail(&s->node, &script_descs);
1574}
1575
1576static struct script_desc *script_desc__find(const char *name)
1577{
1578        struct script_desc *s;
1579
1580        list_for_each_entry(s, &script_descs, node)
1581                if (strcasecmp(s->name, name) == 0)
1582                        return s;
1583        return NULL;
1584}
1585
1586static struct script_desc *script_desc__findnew(const char *name)
1587{
1588        struct script_desc *s = script_desc__find(name);
1589
1590        if (s)
1591                return s;
1592
1593        s = script_desc__new(name);
1594        if (!s)
1595                goto out_delete_desc;
1596
1597        script_desc__add(s);
1598
1599        return s;
1600
1601out_delete_desc:
1602        script_desc__delete(s);
1603
1604        return NULL;
1605}
1606
1607static const char *ends_with(const char *str, const char *suffix)
1608{
1609        size_t suffix_len = strlen(suffix);
1610        const char *p = str;
1611
1612        if (strlen(str) > suffix_len) {
1613                p = str + strlen(str) - suffix_len;
1614                if (!strncmp(p, suffix, suffix_len))
1615                        return p;
1616        }
1617
1618        return NULL;
1619}
1620
1621static int read_script_info(struct script_desc *desc, const char *filename)
1622{
1623        char line[BUFSIZ], *p;
1624        FILE *fp;
1625
1626        fp = fopen(filename, "r");
1627        if (!fp)
1628                return -1;
1629
1630        while (fgets(line, sizeof(line), fp)) {
1631                p = ltrim(line);
1632                if (strlen(p) == 0)
1633                        continue;
1634                if (*p != '#')
1635                        continue;
1636                p++;
1637                if (strlen(p) && *p == '!')
1638                        continue;
1639
1640                p = ltrim(p);
1641                if (strlen(p) && p[strlen(p) - 1] == '\n')
1642                        p[strlen(p) - 1] = '\0';
1643
1644                if (!strncmp(p, "description:", strlen("description:"))) {
1645                        p += strlen("description:");
1646                        desc->half_liner = strdup(ltrim(p));
1647                        continue;
1648                }
1649
1650                if (!strncmp(p, "args:", strlen("args:"))) {
1651                        p += strlen("args:");
1652                        desc->args = strdup(ltrim(p));
1653                        continue;
1654                }
1655        }
1656
1657        fclose(fp);
1658
1659        return 0;
1660}
1661
1662static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1663{
1664        char *script_root, *str;
1665
1666        script_root = strdup(script_dirent->d_name);
1667        if (!script_root)
1668                return NULL;
1669
1670        str = (char *)ends_with(script_root, suffix);
1671        if (!str) {
1672                free(script_root);
1673                return NULL;
1674        }
1675
1676        *str = '\0';
1677        return script_root;
1678}
1679
1680static int list_available_scripts(const struct option *opt __maybe_unused,
1681                                  const char *s __maybe_unused,
1682                                  int unset __maybe_unused)
1683{
1684        struct dirent *script_dirent, *lang_dirent;
1685        char scripts_path[MAXPATHLEN];
1686        DIR *scripts_dir, *lang_dir;
1687        char script_path[MAXPATHLEN];
1688        char lang_path[MAXPATHLEN];
1689        struct script_desc *desc;
1690        char first_half[BUFSIZ];
1691        char *script_root;
1692
1693        snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1694
1695        scripts_dir = opendir(scripts_path);
1696        if (!scripts_dir) {
1697                fprintf(stdout,
1698                        "open(%s) failed.\n"
1699                        "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
1700                        scripts_path);
1701                exit(-1);
1702        }
1703
1704        for_each_lang(scripts_path, scripts_dir, lang_dirent) {
1705                snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1706                         lang_dirent->d_name);
1707                lang_dir = opendir(lang_path);
1708                if (!lang_dir)
1709                        continue;
1710
1711                for_each_script(lang_path, lang_dir, script_dirent) {
1712                        script_root = get_script_root(script_dirent, REPORT_SUFFIX);
1713                        if (script_root) {
1714                                desc = script_desc__findnew(script_root);
1715                                snprintf(script_path, MAXPATHLEN, "%s/%s",
1716                                         lang_path, script_dirent->d_name);
1717                                read_script_info(desc, script_path);
1718                                free(script_root);
1719                        }
1720                }
1721        }
1722
1723        fprintf(stdout, "List of available trace scripts:\n");
1724        list_for_each_entry(desc, &script_descs, node) {
1725                sprintf(first_half, "%s %s", desc->name,
1726                        desc->args ? desc->args : "");
1727                fprintf(stdout, "  %-36s %s\n", first_half,
1728                        desc->half_liner ? desc->half_liner : "");
1729        }
1730
1731        exit(0);
1732}
1733
1734/*
1735 * Some scripts specify the required events in their "xxx-record" file,
1736 * this function will check if the events in perf.data match those
1737 * mentioned in the "xxx-record".
1738 *
1739 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1740 * which is covered well now. And new parsing code should be added to
1741 * cover the future complexing formats like event groups etc.
1742 */
1743static int check_ev_match(char *dir_name, char *scriptname,
1744                        struct perf_session *session)
1745{
1746        char filename[MAXPATHLEN], evname[128];
1747        char line[BUFSIZ], *p;
1748        struct perf_evsel *pos;
1749        int match, len;
1750        FILE *fp;
1751
1752        sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1753
1754        fp = fopen(filename, "r");
1755        if (!fp)
1756                return -1;
1757
1758        while (fgets(line, sizeof(line), fp)) {
1759                p = ltrim(line);
1760                if (*p == '#')
1761                        continue;
1762
1763                while (strlen(p)) {
1764                        p = strstr(p, "-e");
1765                        if (!p)
1766                                break;
1767
1768                        p += 2;
1769                        p = ltrim(p);
1770                        len = strcspn(p, " \t");
1771                        if (!len)
1772                                break;
1773
1774                        snprintf(evname, len + 1, "%s", p);
1775
1776                        match = 0;
1777                        evlist__for_each_entry(session->evlist, pos) {
1778                                if (!strcmp(perf_evsel__name(pos), evname)) {
1779                                        match = 1;
1780                                        break;
1781                                }
1782                        }
1783
1784                        if (!match) {
1785                                fclose(fp);
1786                                return -1;
1787                        }
1788                }
1789        }
1790
1791        fclose(fp);
1792        return 0;
1793}
1794
1795/*
1796 * Return -1 if none is found, otherwise the actual scripts number.
1797 *
1798 * Currently the only user of this function is the script browser, which
1799 * will list all statically runnable scripts, select one, execute it and
1800 * show the output in a perf browser.
1801 */
1802int find_scripts(char **scripts_array, char **scripts_path_array)
1803{
1804        struct dirent *script_dirent, *lang_dirent;
1805        char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
1806        DIR *scripts_dir, *lang_dir;
1807        struct perf_session *session;
1808        struct perf_data_file file = {
1809                .path = input_name,
1810                .mode = PERF_DATA_MODE_READ,
1811        };
1812        char *temp;
1813        int i = 0;
1814
1815        session = perf_session__new(&file, false, NULL);
1816        if (!session)
1817                return -1;
1818
1819        snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1820
1821        scripts_dir = opendir(scripts_path);
1822        if (!scripts_dir) {
1823                perf_session__delete(session);
1824                return -1;
1825        }
1826
1827        for_each_lang(scripts_path, scripts_dir, lang_dirent) {
1828                snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1829                         lang_dirent->d_name);
1830#ifdef NO_LIBPERL
1831                if (strstr(lang_path, "perl"))
1832                        continue;
1833#endif
1834#ifdef NO_LIBPYTHON
1835                if (strstr(lang_path, "python"))
1836                        continue;
1837#endif
1838
1839                lang_dir = opendir(lang_path);
1840                if (!lang_dir)
1841                        continue;
1842
1843                for_each_script(lang_path, lang_dir, script_dirent) {
1844                        /* Skip those real time scripts: xxxtop.p[yl] */
1845                        if (strstr(script_dirent->d_name, "top."))
1846                                continue;
1847                        sprintf(scripts_path_array[i], "%s/%s", lang_path,
1848                                script_dirent->d_name);
1849                        temp = strchr(script_dirent->d_name, '.');
1850                        snprintf(scripts_array[i],
1851                                (temp - script_dirent->d_name) + 1,
1852                                "%s", script_dirent->d_name);
1853
1854                        if (check_ev_match(lang_path,
1855                                        scripts_array[i], session))
1856                                continue;
1857
1858                        i++;
1859                }
1860                closedir(lang_dir);
1861        }
1862
1863        closedir(scripts_dir);
1864        perf_session__delete(session);
1865        return i;
1866}
1867
1868static char *get_script_path(const char *script_root, const char *suffix)
1869{
1870        struct dirent *script_dirent, *lang_dirent;
1871        char scripts_path[MAXPATHLEN];
1872        char script_path[MAXPATHLEN];
1873        DIR *scripts_dir, *lang_dir;
1874        char lang_path[MAXPATHLEN];
1875        char *__script_root;
1876
1877        snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1878
1879        scripts_dir = opendir(scripts_path);
1880        if (!scripts_dir)
1881                return NULL;
1882
1883        for_each_lang(scripts_path, scripts_dir, lang_dirent) {
1884                snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1885                         lang_dirent->d_name);
1886                lang_dir = opendir(lang_path);
1887                if (!lang_dir)
1888                        continue;
1889
1890                for_each_script(lang_path, lang_dir, script_dirent) {
1891                        __script_root = get_script_root(script_dirent, suffix);
1892                        if (__script_root && !strcmp(script_root, __script_root)) {
1893                                free(__script_root);
1894                                closedir(lang_dir);
1895                                closedir(scripts_dir);
1896                                snprintf(script_path, MAXPATHLEN, "%s/%s",
1897                                         lang_path, script_dirent->d_name);
1898                                return strdup(script_path);
1899                        }
1900                        free(__script_root);
1901                }
1902                closedir(lang_dir);
1903        }
1904        closedir(scripts_dir);
1905
1906        return NULL;
1907}
1908
1909static bool is_top_script(const char *script_path)
1910{
1911        return ends_with(script_path, "top") == NULL ? false : true;
1912}
1913
1914static int has_required_arg(char *script_path)
1915{
1916        struct script_desc *desc;
1917        int n_args = 0;
1918        char *p;
1919
1920        desc = script_desc__new(NULL);
1921
1922        if (read_script_info(desc, script_path))
1923                goto out;
1924
1925        if (!desc->args)
1926                goto out;
1927
1928        for (p = desc->args; *p; p++)
1929                if (*p == '<')
1930                        n_args++;
1931out:
1932        script_desc__delete(desc);
1933
1934        return n_args;
1935}
1936
1937static int have_cmd(int argc, const char **argv)
1938{
1939        char **__argv = malloc(sizeof(const char *) * argc);
1940
1941        if (!__argv) {
1942                pr_err("malloc failed\n");
1943                return -1;
1944        }
1945
1946        memcpy(__argv, argv, sizeof(const char *) * argc);
1947        argc = parse_options(argc, (const char **)__argv, record_options,
1948                             NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1949        free(__argv);
1950
1951        system_wide = (argc == 0);
1952
1953        return 0;
1954}
1955
1956static void script__setup_sample_type(struct perf_script *script)
1957{
1958        struct perf_session *session = script->session;
1959        u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1960
1961        if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1962                if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1963                    (sample_type & PERF_SAMPLE_STACK_USER))
1964                        callchain_param.record_mode = CALLCHAIN_DWARF;
1965                else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1966                        callchain_param.record_mode = CALLCHAIN_LBR;
1967                else
1968                        callchain_param.record_mode = CALLCHAIN_FP;
1969        }
1970}
1971
1972static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1973                                    union perf_event *event,
1974                                    struct perf_session *session)
1975{
1976        struct stat_round_event *round = &event->stat_round;
1977        struct perf_evsel *counter;
1978
1979        evlist__for_each_entry(session->evlist, counter) {
1980                perf_stat_process_counter(&stat_config, counter);
1981                process_stat(counter, round->time);
1982        }
1983
1984        process_stat_interval(round->time);
1985        return 0;
1986}
1987
1988static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1989                                     union perf_event *event,
1990                                     struct perf_session *session __maybe_unused)
1991{
1992        perf_event__read_stat_config(&stat_config, &event->stat_config);
1993        return 0;
1994}
1995
1996static int set_maps(struct perf_script *script)
1997{
1998        struct perf_evlist *evlist = script->session->evlist;
1999
2000        if (!script->cpus || !script->threads)
2001                return 0;
2002
2003        if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2004                return -EINVAL;
2005
2006        perf_evlist__set_maps(evlist, script->cpus, script->threads);
2007
2008        if (perf_evlist__alloc_stats(evlist, true))
2009                return -ENOMEM;
2010
2011        script->allocated = true;
2012        return 0;
2013}
2014
2015static
2016int process_thread_map_event(struct perf_tool *tool,
2017                             union perf_event *event,
2018                             struct perf_session *session __maybe_unused)
2019{
2020        struct perf_script *script = container_of(tool, struct perf_script, tool);
2021
2022        if (script->threads) {
2023                pr_warning("Extra thread map event, ignoring.\n");
2024                return 0;
2025        }
2026
2027        script->threads = thread_map__new_event(&event->thread_map);
2028        if (!script->threads)
2029                return -ENOMEM;
2030
2031        return set_maps(script);
2032}
2033
2034static
2035int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2036                          union perf_event *event,
2037                          struct perf_session *session __maybe_unused)
2038{
2039        struct perf_script *script = container_of(tool, struct perf_script, tool);
2040
2041        if (script->cpus) {
2042                pr_warning("Extra cpu map event, ignoring.\n");
2043                return 0;
2044        }
2045
2046        script->cpus = cpu_map__new_data(&event->cpu_map.data);
2047        if (!script->cpus)
2048                return -ENOMEM;
2049
2050        return set_maps(script);
2051}
2052
2053int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
2054{
2055        bool show_full_info = false;
2056        bool header = false;
2057        bool header_only = false;
2058        bool script_started = false;
2059        char *rec_script_path = NULL;
2060        char *rep_script_path = NULL;
2061        struct perf_session *session;
2062        struct itrace_synth_opts itrace_synth_opts = { .set = false, };
2063        char *script_path = NULL;
2064        const char **__argv;
2065        int i, j, err = 0;
2066        struct perf_script script = {
2067                .tool = {
2068                        .sample          = process_sample_event,
2069                        .mmap            = perf_event__process_mmap,
2070                        .mmap2           = perf_event__process_mmap2,
2071                        .comm            = perf_event__process_comm,
2072                        .exit            = perf_event__process_exit,
2073                        .fork            = perf_event__process_fork,
2074                        .attr            = process_attr,
2075                        .event_update   = perf_event__process_event_update,
2076                        .tracing_data    = perf_event__process_tracing_data,
2077                        .build_id        = perf_event__process_build_id,
2078                        .id_index        = perf_event__process_id_index,
2079                        .auxtrace_info   = perf_event__process_auxtrace_info,
2080                        .auxtrace        = perf_event__process_auxtrace,
2081                        .auxtrace_error  = perf_event__process_auxtrace_error,
2082                        .stat            = perf_event__process_stat_event,
2083                        .stat_round      = process_stat_round_event,
2084                        .stat_config     = process_stat_config_event,
2085                        .thread_map      = process_thread_map_event,
2086                        .cpu_map         = process_cpu_map_event,
2087                        .ordered_events  = true,
2088                        .ordering_requires_timestamps = true,
2089                },
2090        };
2091        struct perf_data_file file = {
2092                .mode = PERF_DATA_MODE_READ,
2093        };
2094        const struct option options[] = {
2095        OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2096                    "dump raw trace in ASCII"),
2097        OPT_INCR('v', "verbose", &verbose,
2098                 "be more verbose (show symbol address, etc)"),
2099        OPT_BOOLEAN('L', "Latency", &latency_format,
2100                    "show latency attributes (irqs/preemption disabled, etc)"),
2101        OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2102                           list_available_scripts),
2103        OPT_CALLBACK('s', "script", NULL, "name",
2104                     "script file name (lang:script name, script name, or *)",
2105                     parse_scriptname),
2106        OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2107                   "generate perf-script.xx script in specified language"),
2108        OPT_STRING('i', "input", &input_name, "file", "input file name"),
2109        OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2110                   "do various checks like samples ordering and lost events"),
2111        OPT_BOOLEAN(0, "header", &header, "Show data header."),
2112        OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
2113        OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2114                   "file", "vmlinux pathname"),
2115        OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2116                   "file", "kallsyms pathname"),
2117        OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2118                    "When printing symbols do not display call chain"),
2119        OPT_CALLBACK(0, "symfs", NULL, "directory",
2120                     "Look for files with symbols relative to this directory",
2121                     symbol__config_symfs),
2122        OPT_CALLBACK('F', "fields", NULL, "str",
2123                     "comma separated output fields prepend with 'type:'. "
2124                     "Valid types: hw,sw,trace,raw. "
2125                     "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
2126                     "addr,symoff,period,iregs,brstack,brstacksym,flags,"
2127                     "bpf-output,callindent", parse_output_fields),
2128        OPT_BOOLEAN('a', "all-cpus", &system_wide,
2129                    "system-wide collection from all CPUs"),
2130        OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2131                   "only consider these symbols"),
2132        OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2133        OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2134                   "only display events for these comms"),
2135        OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2136                   "only consider symbols in these pids"),
2137        OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2138                   "only consider symbols in these tids"),
2139        OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2140                     "Set the maximum stack depth when parsing the callchain, "
2141                     "anything beyond the specified depth will be ignored. "
2142                     "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
2143        OPT_BOOLEAN('I', "show-info", &show_full_info,
2144                    "display extended information from perf.data file"),
2145        OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2146                    "Show the path of [kernel.kallsyms]"),
2147        OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2148                    "Show the fork/comm/exit events"),
2149        OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2150                    "Show the mmap events"),
2151        OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2152                    "Show context switch events (if recorded)"),
2153        OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
2154        OPT_BOOLEAN(0, "ns", &nanosecs,
2155                    "Use 9 decimal places when displaying time"),
2156        OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2157                            "Instruction Tracing options",
2158                            itrace_parse_synth_opts),
2159        OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2160                        "Show full source file name path for source lines"),
2161        OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2162                        "Enable symbol demangling"),
2163        OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2164                        "Enable kernel symbol demangling"),
2165
2166        OPT_END()
2167        };
2168        const char * const script_subcommands[] = { "record", "report", NULL };
2169        const char *script_usage[] = {
2170                "perf script [<options>]",
2171                "perf script [<options>] record <script> [<record-options>] <command>",
2172                "perf script [<options>] report <script> [script-args]",
2173                "perf script [<options>] <script> [<record-options>] <command>",
2174                "perf script [<options>] <top-script> [script-args]",
2175                NULL
2176        };
2177
2178        setup_scripting();
2179
2180        argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
2181                             PARSE_OPT_STOP_AT_NON_OPTION);
2182
2183        file.path = input_name;
2184
2185        if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2186                rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2187                if (!rec_script_path)
2188                        return cmd_record(argc, argv, NULL);
2189        }
2190
2191        if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2192                rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2193                if (!rep_script_path) {
2194                        fprintf(stderr,
2195                                "Please specify a valid report script"
2196                                "(see 'perf script -l' for listing)\n");
2197                        return -1;
2198                }
2199        }
2200
2201        if (itrace_synth_opts.callchain &&
2202            itrace_synth_opts.callchain_sz > scripting_max_stack)
2203                scripting_max_stack = itrace_synth_opts.callchain_sz;
2204
2205        /* make sure PERF_EXEC_PATH is set for scripts */
2206        set_argv_exec_path(get_argv_exec_path());
2207
2208        if (argc && !script_name && !rec_script_path && !rep_script_path) {
2209                int live_pipe[2];
2210                int rep_args;
2211                pid_t pid;
2212
2213                rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2214                rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2215
2216                if (!rec_script_path && !rep_script_path) {
2217                        usage_with_options_msg(script_usage, options,
2218                                "Couldn't find script `%s'\n\n See perf"
2219                                " script -l for available scripts.\n", argv[0]);
2220                }
2221
2222                if (is_top_script(argv[0])) {
2223                        rep_args = argc - 1;
2224                } else {
2225                        int rec_args;
2226
2227                        rep_args = has_required_arg(rep_script_path);
2228                        rec_args = (argc - 1) - rep_args;
2229                        if (rec_args < 0) {
2230                                usage_with_options_msg(script_usage, options,
2231                                        "`%s' script requires options."
2232                                        "\n\n See perf script -l for available "
2233                                        "scripts and options.\n", argv[0]);
2234                        }
2235                }
2236
2237                if (pipe(live_pipe) < 0) {
2238                        perror("failed to create pipe");
2239                        return -1;
2240                }
2241
2242                pid = fork();
2243                if (pid < 0) {
2244                        perror("failed to fork");
2245                        return -1;
2246                }
2247
2248                if (!pid) {
2249                        j = 0;
2250
2251                        dup2(live_pipe[1], 1);
2252                        close(live_pipe[0]);
2253
2254                        if (is_top_script(argv[0])) {
2255                                system_wide = true;
2256                        } else if (!system_wide) {
2257                                if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2258                                        err = -1;
2259                                        goto out;
2260                                }
2261                        }
2262
2263                        __argv = malloc((argc + 6) * sizeof(const char *));
2264                        if (!__argv) {
2265                                pr_err("malloc failed\n");
2266                                err = -ENOMEM;
2267                                goto out;
2268                        }
2269
2270                        __argv[j++] = "/bin/sh";
2271                        __argv[j++] = rec_script_path;
2272                        if (system_wide)
2273                                __argv[j++] = "-a";
2274                        __argv[j++] = "-q";
2275                        __argv[j++] = "-o";
2276                        __argv[j++] = "-";
2277                        for (i = rep_args + 1; i < argc; i++)
2278                                __argv[j++] = argv[i];
2279                        __argv[j++] = NULL;
2280
2281                        execvp("/bin/sh", (char **)__argv);
2282                        free(__argv);
2283                        exit(-1);
2284                }
2285
2286                dup2(live_pipe[0], 0);
2287                close(live_pipe[1]);
2288
2289                __argv = malloc((argc + 4) * sizeof(const char *));
2290                if (!__argv) {
2291                        pr_err("malloc failed\n");
2292                        err = -ENOMEM;
2293                        goto out;
2294                }
2295
2296                j = 0;
2297                __argv[j++] = "/bin/sh";
2298                __argv[j++] = rep_script_path;
2299                for (i = 1; i < rep_args + 1; i++)
2300                        __argv[j++] = argv[i];
2301                __argv[j++] = "-i";
2302                __argv[j++] = "-";
2303                __argv[j++] = NULL;
2304
2305                execvp("/bin/sh", (char **)__argv);
2306                free(__argv);
2307                exit(-1);
2308        }
2309
2310        if (rec_script_path)
2311                script_path = rec_script_path;
2312        if (rep_script_path)
2313                script_path = rep_script_path;
2314
2315        if (script_path) {
2316                j = 0;
2317
2318                if (!rec_script_path)
2319                        system_wide = false;
2320                else if (!system_wide) {
2321                        if (have_cmd(argc - 1, &argv[1]) != 0) {
2322                                err = -1;
2323                                goto out;
2324                        }
2325                }
2326
2327                __argv = malloc((argc + 2) * sizeof(const char *));
2328                if (!__argv) {
2329                        pr_err("malloc failed\n");
2330                        err = -ENOMEM;
2331                        goto out;
2332                }
2333
2334                __argv[j++] = "/bin/sh";
2335                __argv[j++] = script_path;
2336                if (system_wide)
2337                        __argv[j++] = "-a";
2338                for (i = 2; i < argc; i++)
2339                        __argv[j++] = argv[i];
2340                __argv[j++] = NULL;
2341
2342                execvp("/bin/sh", (char **)__argv);
2343                free(__argv);
2344                exit(-1);
2345        }
2346
2347        if (!script_name)
2348                setup_pager();
2349
2350        session = perf_session__new(&file, false, &script.tool);
2351        if (session == NULL)
2352                return -1;
2353
2354        if (header || header_only) {
2355                perf_session__fprintf_info(session, stdout, show_full_info);
2356                if (header_only)
2357                        goto out_delete;
2358        }
2359
2360        if (symbol__init(&session->header.env) < 0)
2361                goto out_delete;
2362
2363        script.session = session;
2364        script__setup_sample_type(&script);
2365
2366        if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2367                itrace_synth_opts.thread_stack = true;
2368
2369        session->itrace_synth_opts = &itrace_synth_opts;
2370
2371        if (cpu_list) {
2372                err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2373                if (err < 0)
2374                        goto out_delete;
2375        }
2376
2377        if (!no_callchain)
2378                symbol_conf.use_callchain = true;
2379        else
2380                symbol_conf.use_callchain = false;
2381
2382        if (session->tevent.pevent &&
2383            pevent_set_function_resolver(session->tevent.pevent,
2384                                         machine__resolve_kernel_addr,
2385                                         &session->machines.host) < 0) {
2386                pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2387                return -1;
2388        }
2389
2390        if (generate_script_lang) {
2391                struct stat perf_stat;
2392                int input;
2393
2394                if (output_set_by_user()) {
2395                        fprintf(stderr,
2396                                "custom fields not supported for generated scripts");
2397                        err = -EINVAL;
2398                        goto out_delete;
2399                }
2400
2401                input = open(file.path, O_RDONLY);      /* input_name */
2402                if (input < 0) {
2403                        err = -errno;
2404                        perror("failed to open file");
2405                        goto out_delete;
2406                }
2407
2408                err = fstat(input, &perf_stat);
2409                if (err < 0) {
2410                        perror("failed to stat file");
2411                        goto out_delete;
2412                }
2413
2414                if (!perf_stat.st_size) {
2415                        fprintf(stderr, "zero-sized file, nothing to do!\n");
2416                        goto out_delete;
2417                }
2418
2419                scripting_ops = script_spec__lookup(generate_script_lang);
2420                if (!scripting_ops) {
2421                        fprintf(stderr, "invalid language specifier");
2422                        err = -ENOENT;
2423                        goto out_delete;
2424                }
2425
2426                err = scripting_ops->generate_script(session->tevent.pevent,
2427                                                     "perf-script");
2428                goto out_delete;
2429        }
2430
2431        if (script_name) {
2432                err = scripting_ops->start_script(script_name, argc, argv);
2433                if (err)
2434                        goto out_delete;
2435                pr_debug("perf script started with script %s\n\n", script_name);
2436                script_started = true;
2437        }
2438
2439
2440        err = perf_session__check_output_opt(session);
2441        if (err < 0)
2442                goto out_delete;
2443
2444        err = __cmd_script(&script);
2445
2446        flush_scripting();
2447
2448out_delete:
2449        perf_evlist__free_stats(session->evlist);
2450        perf_session__delete(session);
2451
2452        if (script_started)
2453                cleanup_scripting();
2454out:
2455        return err;
2456}
2457