Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include "../../builtin.h"
0003 #include "../../perf.h"
0004 #include "../../util/util.h" // perf_exe()
0005 #include "../util.h"
0006 #include "../../util/hist.h"
0007 #include "../../util/debug.h"
0008 #include "../../util/symbol.h"
0009 #include "../browser.h"
0010 #include "../libslang.h"
0011 #include "config.h"
0012 #include <linux/string.h>
0013 #include <linux/zalloc.h>
0014 #include <stdlib.h>
0015 
0016 #define SCRIPT_NAMELEN  128
0017 #define SCRIPT_MAX_NO   64
0018 /*
0019  * Usually the full path for a script is:
0020  *  /home/username/libexec/perf-core/scripts/python/xxx.py
0021  *  /home/username/libexec/perf-core/scripts/perl/xxx.pl
0022  * So 256 should be long enough to contain the full path.
0023  */
0024 #define SCRIPT_FULLPATH_LEN 256
0025 
0026 struct script_config {
0027     const char **names;
0028     char **paths;
0029     int index;
0030     const char *perf;
0031     char extra_format[256];
0032 };
0033 
0034 void attr_to_script(char *extra_format, struct perf_event_attr *attr)
0035 {
0036     extra_format[0] = 0;
0037     if (attr->read_format & PERF_FORMAT_GROUP)
0038         strcat(extra_format, " -F +metric");
0039     if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK)
0040         strcat(extra_format, " -F +brstackinsn --xed");
0041     if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
0042         strcat(extra_format, " -F +iregs");
0043     if (attr->sample_type & PERF_SAMPLE_REGS_USER)
0044         strcat(extra_format, " -F +uregs");
0045     if (attr->sample_type & PERF_SAMPLE_PHYS_ADDR)
0046         strcat(extra_format, " -F +phys_addr");
0047 }
0048 
0049 static int add_script_option(const char *name, const char *opt,
0050                  struct script_config *c)
0051 {
0052     c->names[c->index] = name;
0053     if (asprintf(&c->paths[c->index],
0054              "%s script %s -F +metric %s %s",
0055              c->perf, opt, symbol_conf.inline_name ? " --inline" : "",
0056              c->extra_format) < 0)
0057         return -1;
0058     c->index++;
0059     return 0;
0060 }
0061 
0062 static int scripts_config(const char *var, const char *value, void *data)
0063 {
0064     struct script_config *c = data;
0065 
0066     if (!strstarts(var, "scripts."))
0067         return -1;
0068     if (c->index >= SCRIPT_MAX_NO)
0069         return -1;
0070     c->names[c->index] = strdup(var + 7);
0071     if (!c->names[c->index])
0072         return -1;
0073     if (asprintf(&c->paths[c->index], "%s %s", value,
0074              c->extra_format) < 0)
0075         return -1;
0076     c->index++;
0077     return 0;
0078 }
0079 
0080 /*
0081  * When success, will copy the full path of the selected script
0082  * into  the buffer pointed by script_name, and return 0.
0083  * Return -1 on failure.
0084  */
0085 static int list_scripts(char *script_name, bool *custom,
0086             struct evsel *evsel)
0087 {
0088     char *buf, *paths[SCRIPT_MAX_NO], *names[SCRIPT_MAX_NO];
0089     int i, num, choice;
0090     int ret = 0;
0091     int max_std, custom_perf;
0092     char pbuf[256];
0093     const char *perf = perf_exe(pbuf, sizeof pbuf);
0094     struct script_config scriptc = {
0095         .names = (const char **)names,
0096         .paths = paths,
0097         .perf = perf
0098     };
0099 
0100     script_name[0] = 0;
0101 
0102     /* Preset the script name to SCRIPT_NAMELEN */
0103     buf = malloc(SCRIPT_MAX_NO * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN));
0104     if (!buf)
0105         return -1;
0106 
0107     if (evsel)
0108         attr_to_script(scriptc.extra_format, &evsel->core.attr);
0109     add_script_option("Show individual samples", "", &scriptc);
0110     add_script_option("Show individual samples with assembler", "-F +insn --xed",
0111               &scriptc);
0112     add_script_option("Show individual samples with source", "-F +srcline,+srccode",
0113               &scriptc);
0114     perf_config(scripts_config, &scriptc);
0115     custom_perf = scriptc.index;
0116     add_script_option("Show samples with custom perf script arguments", "", &scriptc);
0117     i = scriptc.index;
0118     max_std = i;
0119 
0120     for (; i < SCRIPT_MAX_NO; i++) {
0121         names[i] = buf + (i - max_std) * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN);
0122         paths[i] = names[i] + SCRIPT_NAMELEN;
0123     }
0124 
0125     num = find_scripts(names + max_std, paths + max_std, SCRIPT_MAX_NO - max_std,
0126             SCRIPT_FULLPATH_LEN);
0127     if (num < 0)
0128         num = 0;
0129     choice = ui__popup_menu(num + max_std, (char * const *)names, NULL);
0130     if (choice < 0) {
0131         ret = -1;
0132         goto out;
0133     }
0134     if (choice == custom_perf) {
0135         char script_args[50];
0136         int key = ui_browser__input_window("perf script command",
0137                 "Enter perf script command line (without perf script prefix)",
0138                 script_args, "", 0);
0139         if (key != K_ENTER) {
0140             ret = -1;
0141             goto out;
0142         }
0143         sprintf(script_name, "%s script %s", perf, script_args);
0144     } else if (choice < num + max_std) {
0145         strcpy(script_name, paths[choice]);
0146     }
0147     *custom = choice >= max_std;
0148 
0149 out:
0150     free(buf);
0151     for (i = 0; i < max_std; i++)
0152         zfree(&paths[i]);
0153     return ret;
0154 }
0155 
0156 void run_script(char *cmd)
0157 {
0158     pr_debug("Running %s\n", cmd);
0159     SLang_reset_tty();
0160     if (system(cmd) < 0)
0161         pr_warning("Cannot run %s\n", cmd);
0162     /*
0163      * SLang doesn't seem to reset the whole terminal, so be more
0164      * forceful to get back to the original state.
0165      */
0166     printf("\033[c\033[H\033[J");
0167     fflush(stdout);
0168     SLang_init_tty(0, 0, 0);
0169     SLsmg_refresh();
0170 }
0171 
0172 int script_browse(const char *script_opt, struct evsel *evsel)
0173 {
0174     char *cmd, script_name[SCRIPT_FULLPATH_LEN];
0175     bool custom = false;
0176 
0177     memset(script_name, 0, SCRIPT_FULLPATH_LEN);
0178     if (list_scripts(script_name, &custom, evsel))
0179         return -1;
0180 
0181     if (asprintf(&cmd, "%s%s %s %s%s 2>&1 | less",
0182             custom ? "perf script -s " : "",
0183             script_name,
0184             script_opt ? script_opt : "",
0185             input_name ? "-i " : "",
0186             input_name ? input_name : "") < 0)
0187         return -1;
0188 
0189     run_script(cmd);
0190     free(cmd);
0191 
0192     return 0;
0193 }