1 // SPDX-License-Identifier: GPL-2.0
2 #include "../../util/sort.h"
3 #include "../../util/hist.h"
4 #include "../../util/debug.h"
5 #include "../../util/symbol.h"
6 #include "../browser.h"
7 #include "../libslang.h"
9 #include <linux/zalloc.h>
11 #define SCRIPT_NAMELEN 128
12 #define SCRIPT_MAX_NO 64
14 * Usually the full path for a script is:
15 * /home/username/libexec/perf-core/scripts/python/xxx.py
16 * /home/username/libexec/perf-core/scripts/perl/xxx.pl
17 * So 256 should be long enough to contain the full path.
19 #define SCRIPT_FULLPATH_LEN 256
21 struct script_config {
26 char extra_format[256];
29 void attr_to_script(char *extra_format, struct perf_event_attr *attr)
32 if (attr->read_format & PERF_FORMAT_GROUP)
33 strcat(extra_format, " -F +metric");
34 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK)
35 strcat(extra_format, " -F +brstackinsn --xed");
36 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
37 strcat(extra_format, " -F +iregs");
38 if (attr->sample_type & PERF_SAMPLE_REGS_USER)
39 strcat(extra_format, " -F +uregs");
40 if (attr->sample_type & PERF_SAMPLE_PHYS_ADDR)
41 strcat(extra_format, " -F +phys_addr");
44 static int add_script_option(const char *name, const char *opt,
45 struct script_config *c)
47 c->names[c->index] = name;
48 if (asprintf(&c->paths[c->index],
49 "%s script %s -F +metric %s %s",
50 c->perf, opt, symbol_conf.inline_name ? " --inline" : "",
57 static int scripts_config(const char *var, const char *value, void *data)
59 struct script_config *c = data;
61 if (!strstarts(var, "scripts."))
63 if (c->index >= SCRIPT_MAX_NO)
65 c->names[c->index] = strdup(var + 7);
66 if (!c->names[c->index])
68 if (asprintf(&c->paths[c->index], "%s %s", value,
76 * When success, will copy the full path of the selected script
77 * into the buffer pointed by script_name, and return 0.
78 * Return -1 on failure.
80 static int list_scripts(char *script_name, bool *custom,
81 struct perf_evsel *evsel)
83 char *buf, *paths[SCRIPT_MAX_NO], *names[SCRIPT_MAX_NO];
86 int max_std, custom_perf;
88 const char *perf = perf_exe(pbuf, sizeof pbuf);
89 struct script_config scriptc = {
90 .names = (const char **)names,
97 /* Preset the script name to SCRIPT_NAMELEN */
98 buf = malloc(SCRIPT_MAX_NO * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN));
103 attr_to_script(scriptc.extra_format, &evsel->attr);
104 add_script_option("Show individual samples", "", &scriptc);
105 add_script_option("Show individual samples with assembler", "-F +insn --xed",
107 add_script_option("Show individual samples with source", "-F +srcline,+srccode",
109 perf_config(scripts_config, &scriptc);
110 custom_perf = scriptc.index;
111 add_script_option("Show samples with custom perf script arguments", "", &scriptc);
115 for (; i < SCRIPT_MAX_NO; i++) {
116 names[i] = buf + (i - max_std) * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN);
117 paths[i] = names[i] + SCRIPT_NAMELEN;
120 num = find_scripts(names + max_std, paths + max_std, SCRIPT_MAX_NO - max_std,
121 SCRIPT_FULLPATH_LEN);
124 choice = ui__popup_menu(num + max_std, (char * const *)names);
129 if (choice == custom_perf) {
130 char script_args[50];
131 int key = ui_browser__input_window("perf script command",
132 "Enter perf script command line (without perf script prefix)",
136 sprintf(script_name, "%s script %s", perf, script_args);
137 } else if (choice < num + max_std) {
138 strcpy(script_name, paths[choice]);
140 *custom = choice >= max_std;
144 for (i = 0; i < max_std; i++)
149 void run_script(char *cmd)
151 pr_debug("Running %s\n", cmd);
154 pr_warning("Cannot run %s\n", cmd);
156 * SLang doesn't seem to reset the whole terminal, so be more
157 * forceful to get back to the original state.
159 printf("\033[c\033[H\033[J");
161 SLang_init_tty(0, 0, 0);
165 int script_browse(const char *script_opt, struct perf_evsel *evsel)
167 char *cmd, script_name[SCRIPT_FULLPATH_LEN];
170 memset(script_name, 0, SCRIPT_FULLPATH_LEN);
171 if (list_scripts(script_name, &custom, evsel))
174 if (asprintf(&cmd, "%s%s %s %s%s 2>&1 | less",
175 custom ? "perf script -s " : "",
177 script_opt ? script_opt : "",
178 input_name ? "-i " : "",
179 input_name ? input_name : "") < 0)