^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include "../../builtin.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include "../../perf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include "../../util/util.h" // perf_exe()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "../util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "../../util/hist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "../../util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "../../util/symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "../browser.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "../libslang.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "config.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define SCRIPT_NAMELEN 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define SCRIPT_MAX_NO 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Usually the full path for a script is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * /home/username/libexec/perf-core/scripts/python/xxx.py
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * /home/username/libexec/perf-core/scripts/perl/xxx.pl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * So 256 should be long enough to contain the full path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define SCRIPT_FULLPATH_LEN 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct script_config {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) const char **names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) char **paths;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const char *perf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) char extra_format[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) void attr_to_script(char *extra_format, struct perf_event_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) extra_format[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (attr->read_format & PERF_FORMAT_GROUP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) strcat(extra_format, " -F +metric");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) strcat(extra_format, " -F +brstackinsn --xed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) strcat(extra_format, " -F +iregs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (attr->sample_type & PERF_SAMPLE_REGS_USER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) strcat(extra_format, " -F +uregs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (attr->sample_type & PERF_SAMPLE_PHYS_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) strcat(extra_format, " -F +phys_addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int add_script_option(const char *name, const char *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct script_config *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) c->names[c->index] = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (asprintf(&c->paths[c->index],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) "%s script %s -F +metric %s %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) c->perf, opt, symbol_conf.inline_name ? " --inline" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) c->extra_format) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) c->index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static int scripts_config(const char *var, const char *value, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct script_config *c = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!strstarts(var, "scripts."))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (c->index >= SCRIPT_MAX_NO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) c->names[c->index] = strdup(var + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!c->names[c->index])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (asprintf(&c->paths[c->index], "%s %s", value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) c->extra_format) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) c->index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * When success, will copy the full path of the selected script
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * into the buffer pointed by script_name, and return 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Return -1 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int list_scripts(char *script_name, bool *custom,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) char *buf, *paths[SCRIPT_MAX_NO], *names[SCRIPT_MAX_NO];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int i, num, choice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int max_std, custom_perf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) char pbuf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) const char *perf = perf_exe(pbuf, sizeof pbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct script_config scriptc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .names = (const char **)names,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .paths = paths,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .perf = perf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) script_name[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* Preset the script name to SCRIPT_NAMELEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) buf = malloc(SCRIPT_MAX_NO * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) attr_to_script(scriptc.extra_format, &evsel->core.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) add_script_option("Show individual samples", "", &scriptc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) add_script_option("Show individual samples with assembler", "-F +insn --xed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) &scriptc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) add_script_option("Show individual samples with source", "-F +srcline,+srccode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) &scriptc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) perf_config(scripts_config, &scriptc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) custom_perf = scriptc.index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) add_script_option("Show samples with custom perf script arguments", "", &scriptc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) i = scriptc.index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) max_std = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) for (; i < SCRIPT_MAX_NO; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) names[i] = buf + (i - max_std) * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) paths[i] = names[i] + SCRIPT_NAMELEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) num = find_scripts(names + max_std, paths + max_std, SCRIPT_MAX_NO - max_std,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) SCRIPT_FULLPATH_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (num < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) choice = ui__popup_menu(num + max_std, (char * const *)names, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (choice < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (choice == custom_perf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) char script_args[50];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int key = ui_browser__input_window("perf script command",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) "Enter perf script command line (without perf script prefix)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) script_args, "", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (key != K_ENTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) sprintf(script_name, "%s script %s", perf, script_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) } else if (choice < num + max_std) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) strcpy(script_name, paths[choice]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) *custom = choice >= max_std;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) for (i = 0; i < max_std; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) zfree(&paths[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) void run_script(char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) pr_debug("Running %s\n", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) SLang_reset_tty();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (system(cmd) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) pr_warning("Cannot run %s\n", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * SLang doesn't seem to reset the whole terminal, so be more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * forceful to get back to the original state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) printf("\033[c\033[H\033[J");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) SLang_init_tty(0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) SLsmg_refresh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int script_browse(const char *script_opt, struct evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) char *cmd, script_name[SCRIPT_FULLPATH_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) bool custom = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) memset(script_name, 0, SCRIPT_FULLPATH_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (list_scripts(script_name, &custom, evsel))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (asprintf(&cmd, "%s%s %s %s%s 2>&1 | less",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) custom ? "perf script -s " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) script_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) script_opt ? script_opt : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) input_name ? "-i " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) input_name ? input_name : "") < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) run_script(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) free(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }