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