1 // SPDX-License-Identifier: GPL-2.0 2 #include <dirent.h> 3 #include <fcntl.h> 4 #include <limits.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <unistd.h> 9 #include "../../util/util.h" // perf_exe() 10 #include "../util.h" 11 #include "../../util/evlist.h" 12 #include "../../util/hist.h" 13 #include "../../util/debug.h" 14 #include "../../util/session.h" 15 #include "../../util/symbol.h" 16 #include "../browser.h" 17 #include "../keysyms.h" 18 #include "../libslang.h" 19 #include "config.h" 20 #include <linux/err.h> 21 #include <linux/string.h> 22 #include <linux/zalloc.h> 23 #include <subcmd/exec-cmd.h> 24 25 #define SCRIPT_NAMELEN 128 26 #define SCRIPT_MAX_NO 64 27 /* 28 * Usually the full path for a script is: 29 * /home/username/libexec/perf-core/scripts/python/xxx.py 30 * /home/username/libexec/perf-core/scripts/perl/xxx.pl 31 * So 256 should be long enough to contain the full path. 32 */ 33 #define SCRIPT_FULLPATH_LEN 256 34 35 struct script_config { 36 const char **names; 37 char **paths; 38 int index; 39 const char *perf; 40 char extra_format[256]; 41 }; 42 43 void attr_to_script(char *extra_format, struct perf_event_attr *attr) 44 { 45 extra_format[0] = 0; 46 if (attr->read_format & PERF_FORMAT_GROUP) 47 strcat(extra_format, " -F +metric"); 48 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) 49 strcat(extra_format, " -F +brstackinsn --xed"); 50 if (attr->sample_type & PERF_SAMPLE_REGS_INTR) 51 strcat(extra_format, " -F +iregs"); 52 if (attr->sample_type & PERF_SAMPLE_REGS_USER) 53 strcat(extra_format, " -F +uregs"); 54 if (attr->sample_type & PERF_SAMPLE_PHYS_ADDR) 55 strcat(extra_format, " -F +phys_addr"); 56 } 57 58 static int add_script_option(const char *name, const char *opt, 59 struct script_config *c) 60 { 61 c->names[c->index] = name; 62 if (asprintf(&c->paths[c->index], 63 "%s script %s -F +metric %s %s", 64 c->perf, opt, symbol_conf.inline_name ? " --inline" : "", 65 c->extra_format) < 0) 66 return -1; 67 c->index++; 68 return 0; 69 } 70 71 static int scripts_config(const char *var, const char *value, void *data) 72 { 73 struct script_config *c = data; 74 75 if (!strstarts(var, "scripts.")) 76 return -1; 77 if (c->index >= SCRIPT_MAX_NO) 78 return -1; 79 c->names[c->index] = strdup(var + 7); 80 if (!c->names[c->index]) 81 return -1; 82 if (asprintf(&c->paths[c->index], "%s %s", value, 83 c->extra_format) < 0) 84 return -1; 85 c->index++; 86 return 0; 87 } 88 89 /* 90 * Return -1 if none is found, otherwise the actual scripts number. 91 * 92 * Currently the only user of this function is the script browser, which 93 * will list all statically runnable scripts, select one, execute it and 94 * show the output in a perf browser. 95 */ 96 static int find_scripts(char **scripts_array __maybe_unused, 97 char **scripts_path_array __maybe_unused, int num __maybe_unused, 98 int pathlen __maybe_unused) 99 { 100 int i = 0; 101 102 #ifdef HAVE_PYTHON_MODULE_SUPPORT 103 { 104 int namelen; 105 struct dirent *script_dirent; 106 char *temp; 107 const char *exec_path = get_argv_exec_path(); 108 char py_scripts_path[PATH_MAX]; 109 int py_scripts_dir_fd; 110 DIR *py_scripts_dir; 111 int len; 112 113 snprintf(py_scripts_path, sizeof(py_scripts_path), "%s/python", exec_path); 114 py_scripts_dir_fd = open(py_scripts_path, O_DIRECTORY); 115 if (py_scripts_dir_fd != -1) { 116 py_scripts_dir = fdopendir(py_scripts_dir_fd); 117 if (py_scripts_dir) { 118 while ((script_dirent = readdir(py_scripts_dir)) != NULL) { 119 if (script_dirent->d_type == DT_DIR) 120 continue; 121 if (script_dirent->d_type == DT_UNKNOWN && 122 is_directory_at(py_scripts_dir_fd, 123 script_dirent->d_name)) 124 continue; 125 /* Skip those real time scripts: xxxtop.p[yl] */ 126 if (strstr(script_dirent->d_name, "top.")) 127 continue; 128 if (i >= num) 129 break; 130 len = strlen(script_dirent->d_name); 131 if (len <= 3 || 132 strcmp(script_dirent->d_name + len - 3, ".py")) 133 continue; 134 135 scnprintf(scripts_path_array[i], pathlen, "%s/python/%s", 136 exec_path, 137 script_dirent->d_name); 138 temp = strrchr(script_dirent->d_name, '.'); 139 namelen = temp ? (int)(temp - script_dirent->d_name) 140 : (int)strlen(script_dirent->d_name); 141 142 if (namelen >= SCRIPT_NAMELEN) 143 namelen = SCRIPT_NAMELEN - 1; 144 snprintf(scripts_array[i], namelen + 1, "%s", 145 script_dirent->d_name); 146 147 i++; 148 } 149 closedir(py_scripts_dir); 150 } else { 151 close(py_scripts_dir_fd); 152 } 153 } 154 } 155 #endif 156 return i; 157 } 158 159 /* 160 * When success, will copy the full path of the selected script 161 * into the buffer pointed by script_name, and return 0. 162 * Return -1 on failure. 163 */ 164 static int list_scripts(char *script_name, bool *custom, 165 struct evsel *evsel) 166 { 167 char *buf, *paths[SCRIPT_MAX_NO], *names[SCRIPT_MAX_NO]; 168 int i, num, choice; 169 int ret = 0; 170 int max_std, custom_perf; 171 char pbuf[256]; 172 const char *perf = perf_exe(pbuf, sizeof(pbuf)); 173 struct script_config scriptc = { 174 .names = (const char **)names, 175 .paths = paths, 176 .perf = perf 177 }; 178 179 script_name[0] = 0; 180 181 /* Preset the script name to SCRIPT_NAMELEN */ 182 buf = malloc(SCRIPT_MAX_NO * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN)); 183 if (!buf) 184 return -1; 185 186 if (evsel) 187 attr_to_script(scriptc.extra_format, &evsel->core.attr); 188 add_script_option("Show individual samples", "", &scriptc); 189 add_script_option("Show individual samples with assembler", "-F +disasm", 190 &scriptc); 191 add_script_option("Show individual samples with source", "-F +srcline,+srccode", 192 &scriptc); 193 perf_config(scripts_config, &scriptc); 194 custom_perf = scriptc.index; 195 add_script_option("Show samples with custom perf script arguments", "", &scriptc); 196 i = scriptc.index; 197 max_std = i; 198 199 for (; i < SCRIPT_MAX_NO; i++) { 200 names[i] = buf + (i - max_std) * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN); 201 paths[i] = names[i] + SCRIPT_NAMELEN; 202 } 203 204 num = find_scripts(names + max_std, paths + max_std, SCRIPT_MAX_NO - max_std, 205 SCRIPT_FULLPATH_LEN); 206 if (num < 0) 207 num = 0; 208 choice = ui__popup_menu(num + max_std, (char * const *)names, NULL); 209 if (choice < 0) { 210 ret = -1; 211 goto out; 212 } 213 if (choice == custom_perf) { 214 char script_args[50]; 215 int key = ui_browser__input_window("perf script command", 216 "Enter perf script command line (without perf script prefix)", 217 script_args, "", 0); 218 if (key != K_ENTER) { 219 ret = -1; 220 goto out; 221 } 222 sprintf(script_name, "%s script %s", perf, script_args); 223 } else if (choice < num + max_std) { 224 strcpy(script_name, paths[choice]); 225 } 226 *custom = choice >= max_std; 227 228 out: 229 free(buf); 230 for (i = 0; i < max_std; i++) 231 zfree(&paths[i]); 232 return ret; 233 } 234 235 void run_script(char *cmd) 236 { 237 pr_debug("Running %s\n", cmd); 238 SLang_reset_tty(); 239 if (system(cmd) < 0) 240 pr_warning("Cannot run %s\n", cmd); 241 /* 242 * SLang doesn't seem to reset the whole terminal, so be more 243 * forceful to get back to the original state. 244 */ 245 printf("\033[c\033[H\033[J"); 246 fflush(stdout); 247 SLang_init_tty(0, 0, 0); 248 SLtty_set_suspend_state(true); 249 SLsmg_refresh(); 250 } 251 252 int script_browse(const char *script_opt, struct evsel *evsel) 253 { 254 char *cmd, script_name[SCRIPT_FULLPATH_LEN]; 255 bool custom = false; 256 257 memset(script_name, 0, SCRIPT_FULLPATH_LEN); 258 if (list_scripts(script_name, &custom, evsel)) 259 return -1; 260 261 if (asprintf(&cmd, "%s%s %s %s%s 2>&1 | less", 262 custom ? "perf script " : "", 263 script_name, 264 script_opt ? script_opt : "", 265 input_name ? "-i " : "", 266 input_name ? input_name : "") < 0) 267 return -1; 268 269 run_script(cmd); 270 free(cmd); 271 272 return 0; 273 } 274