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