Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * perf.c |
| 3 | * |
| 4 | * Performance analysis utility. |
| 5 | * |
| 6 | * This is the main hub from which the sub-commands (perf stat, |
| 7 | * perf top, perf record, perf report, etc.) are started. |
| 8 | */ |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 9 | #include "builtin.h" |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 10 | |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 11 | #include "util/env.h" |
Ingo Molnar | 148be2c | 2009-04-27 08:02:14 +0200 | [diff] [blame] | 12 | #include "util/exec_cmd.h" |
| 13 | #include "util/cache.h" |
| 14 | #include "util/quote.h" |
| 15 | #include "util/run-command.h" |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 16 | #include "util/parse-events.h" |
Yunlong Song | 7335399 | 2015-02-27 18:21:31 +0800 | [diff] [blame] | 17 | #include "util/parse-options.h" |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 18 | #include "util/bpf-loader.h" |
Jiri Olsa | bbb2cea | 2014-07-17 12:55:00 +0200 | [diff] [blame] | 19 | #include "util/debug.h" |
Jiri Olsa | 592d5a6 | 2015-09-02 09:56:34 +0200 | [diff] [blame] | 20 | #include <api/fs/tracing_path.h> |
Irina Tirdea | 27683dc | 2012-09-08 03:43:19 +0300 | [diff] [blame] | 21 | #include <pthread.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 22 | |
| 23 | const char perf_usage_string[] = |
Jiri Olsa | 78a1b50 | 2014-07-18 09:11:30 +0200 | [diff] [blame] | 24 | "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]"; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 25 | |
| 26 | const char perf_more_info_string[] = |
| 27 | "See 'perf help COMMAND' for more information on a specific command."; |
| 28 | |
Arnaldo Carvalho de Melo | 5d06e69 | 2010-05-20 22:01:10 -0300 | [diff] [blame] | 29 | int use_browser = -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 30 | static int use_pager = -1; |
Feng Tang | 70cb4e9 | 2012-10-30 11:56:02 +0800 | [diff] [blame] | 31 | const char *input_name; |
Arnaldo Carvalho de Melo | 5d06e69 | 2010-05-20 22:01:10 -0300 | [diff] [blame] | 32 | |
Frederic Weisbecker | 98a4179 | 2012-08-09 16:31:51 +0200 | [diff] [blame] | 33 | struct cmd_struct { |
| 34 | const char *cmd; |
| 35 | int (*fn)(int, const char **, const char *); |
| 36 | int option; |
| 37 | }; |
| 38 | |
| 39 | static struct cmd_struct commands[] = { |
| 40 | { "buildid-cache", cmd_buildid_cache, 0 }, |
| 41 | { "buildid-list", cmd_buildid_list, 0 }, |
| 42 | { "diff", cmd_diff, 0 }, |
| 43 | { "evlist", cmd_evlist, 0 }, |
| 44 | { "help", cmd_help, 0 }, |
| 45 | { "list", cmd_list, 0 }, |
| 46 | { "record", cmd_record, 0 }, |
| 47 | { "report", cmd_report, 0 }, |
| 48 | { "bench", cmd_bench, 0 }, |
| 49 | { "stat", cmd_stat, 0 }, |
| 50 | { "timechart", cmd_timechart, 0 }, |
| 51 | { "top", cmd_top, 0 }, |
| 52 | { "annotate", cmd_annotate, 0 }, |
| 53 | { "version", cmd_version, 0 }, |
| 54 | { "script", cmd_script, 0 }, |
| 55 | { "sched", cmd_sched, 0 }, |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 56 | #ifdef HAVE_LIBELF_SUPPORT |
Frederic Weisbecker | 98a4179 | 2012-08-09 16:31:51 +0200 | [diff] [blame] | 57 | { "probe", cmd_probe, 0 }, |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 58 | #endif |
Frederic Weisbecker | 98a4179 | 2012-08-09 16:31:51 +0200 | [diff] [blame] | 59 | { "kmem", cmd_kmem, 0 }, |
| 60 | { "lock", cmd_lock, 0 }, |
| 61 | { "kvm", cmd_kvm, 0 }, |
| 62 | { "test", cmd_test, 0 }, |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 63 | #ifdef HAVE_LIBAUDIT_SUPPORT |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 64 | { "trace", cmd_trace, 0 }, |
Namhyung Kim | 4d29089 | 2012-09-27 20:23:38 +0900 | [diff] [blame] | 65 | #endif |
Frederic Weisbecker | 98a4179 | 2012-08-09 16:31:51 +0200 | [diff] [blame] | 66 | { "inject", cmd_inject, 0 }, |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 67 | { "mem", cmd_mem, 0 }, |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 68 | { "data", cmd_data, 0 }, |
Frederic Weisbecker | 98a4179 | 2012-08-09 16:31:51 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 71 | struct pager_config { |
| 72 | const char *cmd; |
| 73 | int val; |
| 74 | }; |
| 75 | |
| 76 | static int pager_command_config(const char *var, const char *value, void *data) |
| 77 | { |
| 78 | struct pager_config *c = data; |
| 79 | if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd)) |
| 80 | c->val = perf_config_bool(var, value); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */ |
| 85 | int check_pager_config(const char *cmd) |
| 86 | { |
| 87 | struct pager_config c; |
| 88 | c.cmd = cmd; |
| 89 | c.val = -1; |
| 90 | perf_config(pager_command_config, &c); |
| 91 | return c.val; |
| 92 | } |
| 93 | |
Namhyung Kim | 0020ce2 | 2012-11-12 11:50:17 +0900 | [diff] [blame] | 94 | static int browser_command_config(const char *var, const char *value, void *data) |
Arnaldo Carvalho de Melo | 5d06e69 | 2010-05-20 22:01:10 -0300 | [diff] [blame] | 95 | { |
| 96 | struct pager_config *c = data; |
| 97 | if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd)) |
| 98 | c->val = perf_config_bool(var, value); |
Namhyung Kim | 0020ce2 | 2012-11-12 11:50:17 +0900 | [diff] [blame] | 99 | if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd)) |
| 100 | c->val = perf_config_bool(var, value) ? 2 : 0; |
Arnaldo Carvalho de Melo | 5d06e69 | 2010-05-20 22:01:10 -0300 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Namhyung Kim | 0020ce2 | 2012-11-12 11:50:17 +0900 | [diff] [blame] | 104 | /* |
| 105 | * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk", |
| 106 | * and -1 for "not specified" |
| 107 | */ |
| 108 | static int check_browser_config(const char *cmd) |
Arnaldo Carvalho de Melo | 5d06e69 | 2010-05-20 22:01:10 -0300 | [diff] [blame] | 109 | { |
| 110 | struct pager_config c; |
| 111 | c.cmd = cmd; |
| 112 | c.val = -1; |
Namhyung Kim | 0020ce2 | 2012-11-12 11:50:17 +0900 | [diff] [blame] | 113 | perf_config(browser_command_config, &c); |
Arnaldo Carvalho de Melo | 5d06e69 | 2010-05-20 22:01:10 -0300 | [diff] [blame] | 114 | return c.val; |
| 115 | } |
| 116 | |
Thiago Farina | 4c57415 | 2010-01-27 21:05:55 -0200 | [diff] [blame] | 117 | static void commit_pager_choice(void) |
| 118 | { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 119 | switch (use_pager) { |
| 120 | case 0: |
| 121 | setenv("PERF_PAGER", "cat", 1); |
| 122 | break; |
| 123 | case 1: |
| 124 | /* setup_pager(); */ |
| 125 | break; |
| 126 | default: |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
Yunlong Song | 7335399 | 2015-02-27 18:21:31 +0800 | [diff] [blame] | 131 | struct option options[] = { |
| 132 | OPT_ARGUMENT("help", "help"), |
| 133 | OPT_ARGUMENT("version", "version"), |
| 134 | OPT_ARGUMENT("exec-path", "exec-path"), |
| 135 | OPT_ARGUMENT("html-path", "html-path"), |
| 136 | OPT_ARGUMENT("paginate", "paginate"), |
| 137 | OPT_ARGUMENT("no-pager", "no-pager"), |
| 138 | OPT_ARGUMENT("perf-dir", "perf-dir"), |
| 139 | OPT_ARGUMENT("work-tree", "work-tree"), |
| 140 | OPT_ARGUMENT("debugfs-dir", "debugfs-dir"), |
| 141 | OPT_ARGUMENT("buildid-dir", "buildid-dir"), |
| 142 | OPT_ARGUMENT("list-cmds", "list-cmds"), |
| 143 | OPT_ARGUMENT("list-opts", "list-opts"), |
| 144 | OPT_ARGUMENT("debug", "debug"), |
| 145 | OPT_END() |
| 146 | }; |
| 147 | |
Thiago Farina | 4c57415 | 2010-01-27 21:05:55 -0200 | [diff] [blame] | 148 | static int handle_options(const char ***argv, int *argc, int *envchanged) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 149 | { |
| 150 | int handled = 0; |
| 151 | |
| 152 | while (*argc > 0) { |
| 153 | const char *cmd = (*argv)[0]; |
| 154 | if (cmd[0] != '-') |
| 155 | break; |
| 156 | |
| 157 | /* |
| 158 | * For legacy reasons, the "version" and "help" |
| 159 | * commands can be written with "--" prepended |
| 160 | * to make them look like flags. |
| 161 | */ |
| 162 | if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) |
| 163 | break; |
| 164 | |
| 165 | /* |
Jiri Olsa | a1853e2 | 2015-10-05 20:06:09 +0200 | [diff] [blame] | 166 | * Shortcut for '-h' and '-v' options to invoke help |
| 167 | * and version command. |
| 168 | */ |
| 169 | if (!strcmp(cmd, "-h")) { |
| 170 | (*argv)[0] = "--help"; |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | if (!strcmp(cmd, "-v")) { |
| 175 | (*argv)[0] = "--version"; |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | /* |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 180 | * Check remaining flags. |
| 181 | */ |
Vincent Legoll | cfed95a | 2009-10-13 10:18:16 +0200 | [diff] [blame] | 182 | if (!prefixcmp(cmd, CMD_EXEC_PATH)) { |
| 183 | cmd += strlen(CMD_EXEC_PATH); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 184 | if (*cmd == '=') |
| 185 | perf_set_argv_exec_path(cmd + 1); |
| 186 | else { |
| 187 | puts(perf_exec_path()); |
| 188 | exit(0); |
| 189 | } |
| 190 | } else if (!strcmp(cmd, "--html-path")) { |
| 191 | puts(system_path(PERF_HTML_PATH)); |
| 192 | exit(0); |
| 193 | } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { |
| 194 | use_pager = 1; |
| 195 | } else if (!strcmp(cmd, "--no-pager")) { |
| 196 | use_pager = 0; |
| 197 | if (envchanged) |
| 198 | *envchanged = 1; |
| 199 | } else if (!strcmp(cmd, "--perf-dir")) { |
| 200 | if (*argc < 2) { |
Thiago Farina | 4c57415 | 2010-01-27 21:05:55 -0200 | [diff] [blame] | 201 | fprintf(stderr, "No directory given for --perf-dir.\n"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 202 | usage(perf_usage_string); |
| 203 | } |
| 204 | setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1); |
| 205 | if (envchanged) |
| 206 | *envchanged = 1; |
| 207 | (*argv)++; |
| 208 | (*argc)--; |
| 209 | handled++; |
Vincent Legoll | cfed95a | 2009-10-13 10:18:16 +0200 | [diff] [blame] | 210 | } else if (!prefixcmp(cmd, CMD_PERF_DIR)) { |
| 211 | setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 212 | if (envchanged) |
| 213 | *envchanged = 1; |
| 214 | } else if (!strcmp(cmd, "--work-tree")) { |
| 215 | if (*argc < 2) { |
Thiago Farina | 4c57415 | 2010-01-27 21:05:55 -0200 | [diff] [blame] | 216 | fprintf(stderr, "No directory given for --work-tree.\n"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 217 | usage(perf_usage_string); |
| 218 | } |
| 219 | setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1); |
| 220 | if (envchanged) |
| 221 | *envchanged = 1; |
| 222 | (*argv)++; |
| 223 | (*argc)--; |
Vincent Legoll | cfed95a | 2009-10-13 10:18:16 +0200 | [diff] [blame] | 224 | } else if (!prefixcmp(cmd, CMD_WORK_TREE)) { |
| 225 | setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 226 | if (envchanged) |
| 227 | *envchanged = 1; |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 228 | } else if (!strcmp(cmd, "--debugfs-dir")) { |
| 229 | if (*argc < 2) { |
| 230 | fprintf(stderr, "No directory given for --debugfs-dir.\n"); |
| 231 | usage(perf_usage_string); |
| 232 | } |
Jiri Olsa | 65d4b26 | 2015-09-02 09:56:33 +0200 | [diff] [blame] | 233 | tracing_path_set((*argv)[1]); |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 234 | if (envchanged) |
| 235 | *envchanged = 1; |
| 236 | (*argv)++; |
| 237 | (*argc)--; |
Jiri Olsa | 99ce8e9 | 2014-12-01 20:06:24 +0100 | [diff] [blame] | 238 | } else if (!strcmp(cmd, "--buildid-dir")) { |
| 239 | if (*argc < 2) { |
| 240 | fprintf(stderr, "No directory given for --buildid-dir.\n"); |
| 241 | usage(perf_usage_string); |
| 242 | } |
| 243 | set_buildid_dir((*argv)[1]); |
| 244 | if (envchanged) |
| 245 | *envchanged = 1; |
| 246 | (*argv)++; |
| 247 | (*argc)--; |
Vincent Legoll | cfed95a | 2009-10-13 10:18:16 +0200 | [diff] [blame] | 248 | } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) { |
Jiri Olsa | 65d4b26 | 2015-09-02 09:56:33 +0200 | [diff] [blame] | 249 | tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR)); |
Jiri Olsa | 9f30fff | 2015-08-26 15:46:45 +0200 | [diff] [blame] | 250 | fprintf(stderr, "dir: %s\n", tracing_path); |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 251 | if (envchanged) |
| 252 | *envchanged = 1; |
Frederic Weisbecker | 98a4179 | 2012-08-09 16:31:51 +0200 | [diff] [blame] | 253 | } else if (!strcmp(cmd, "--list-cmds")) { |
| 254 | unsigned int i; |
| 255 | |
| 256 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
| 257 | struct cmd_struct *p = commands+i; |
| 258 | printf("%s ", p->cmd); |
| 259 | } |
Yunlong Song | ed45752 | 2015-02-27 18:21:29 +0800 | [diff] [blame] | 260 | putchar('\n'); |
Frederic Weisbecker | 98a4179 | 2012-08-09 16:31:51 +0200 | [diff] [blame] | 261 | exit(0); |
Yunlong Song | 7335399 | 2015-02-27 18:21:31 +0800 | [diff] [blame] | 262 | } else if (!strcmp(cmd, "--list-opts")) { |
| 263 | unsigned int i; |
| 264 | |
| 265 | for (i = 0; i < ARRAY_SIZE(options)-1; i++) { |
| 266 | struct option *p = options+i; |
| 267 | printf("--%s ", p->long_name); |
| 268 | } |
| 269 | putchar('\n'); |
| 270 | exit(0); |
Jiri Olsa | bbb2cea | 2014-07-17 12:55:00 +0200 | [diff] [blame] | 271 | } else if (!strcmp(cmd, "--debug")) { |
| 272 | if (*argc < 2) { |
| 273 | fprintf(stderr, "No variable specified for --debug.\n"); |
| 274 | usage(perf_usage_string); |
| 275 | } |
| 276 | if (perf_debug_option((*argv)[1])) |
| 277 | usage(perf_usage_string); |
| 278 | |
| 279 | (*argv)++; |
| 280 | (*argc)--; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 281 | } else { |
| 282 | fprintf(stderr, "Unknown option: %s\n", cmd); |
| 283 | usage(perf_usage_string); |
| 284 | } |
| 285 | |
| 286 | (*argv)++; |
| 287 | (*argc)--; |
| 288 | handled++; |
| 289 | } |
| 290 | return handled; |
| 291 | } |
| 292 | |
| 293 | static int handle_alias(int *argcp, const char ***argv) |
| 294 | { |
| 295 | int envchanged = 0, ret = 0, saved_errno = errno; |
| 296 | int count, option_count; |
Thiago Farina | 4c57415 | 2010-01-27 21:05:55 -0200 | [diff] [blame] | 297 | const char **new_argv; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 298 | const char *alias_command; |
| 299 | char *alias_string; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 300 | |
| 301 | alias_command = (*argv)[0]; |
| 302 | alias_string = alias_lookup(alias_command); |
| 303 | if (alias_string) { |
| 304 | if (alias_string[0] == '!') { |
| 305 | if (*argcp > 1) { |
| 306 | struct strbuf buf; |
| 307 | |
| 308 | strbuf_init(&buf, PATH_MAX); |
| 309 | strbuf_addstr(&buf, alias_string); |
| 310 | sq_quote_argv(&buf, (*argv) + 1, PATH_MAX); |
| 311 | free(alias_string); |
| 312 | alias_string = buf.buf; |
| 313 | } |
| 314 | ret = system(alias_string + 1); |
| 315 | if (ret >= 0 && WIFEXITED(ret) && |
| 316 | WEXITSTATUS(ret) != 127) |
| 317 | exit(WEXITSTATUS(ret)); |
| 318 | die("Failed to run '%s' when expanding alias '%s'", |
| 319 | alias_string + 1, alias_command); |
| 320 | } |
| 321 | count = split_cmdline(alias_string, &new_argv); |
| 322 | if (count < 0) |
| 323 | die("Bad alias.%s string", alias_command); |
| 324 | option_count = handle_options(&new_argv, &count, &envchanged); |
| 325 | if (envchanged) |
| 326 | die("alias '%s' changes environment variables\n" |
| 327 | "You can use '!perf' in the alias to do this.", |
| 328 | alias_command); |
| 329 | memmove(new_argv - option_count, new_argv, |
| 330 | count * sizeof(char *)); |
| 331 | new_argv -= option_count; |
| 332 | |
| 333 | if (count < 1) |
| 334 | die("empty alias for %s", alias_command); |
| 335 | |
| 336 | if (!strcmp(alias_command, new_argv[0])) |
| 337 | die("recursive alias: %s", alias_command); |
| 338 | |
Thiago Farina | 4c57415 | 2010-01-27 21:05:55 -0200 | [diff] [blame] | 339 | new_argv = realloc(new_argv, sizeof(char *) * |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 340 | (count + *argcp + 1)); |
| 341 | /* insert after command name */ |
Thiago Farina | 4c57415 | 2010-01-27 21:05:55 -0200 | [diff] [blame] | 342 | memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp); |
| 343 | new_argv[count + *argcp] = NULL; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 344 | |
| 345 | *argv = new_argv; |
| 346 | *argcp += count - 1; |
| 347 | |
| 348 | ret = 1; |
| 349 | } |
| 350 | |
| 351 | errno = saved_errno; |
| 352 | |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | const char perf_version_string[] = PERF_VERSION; |
| 357 | |
| 358 | #define RUN_SETUP (1<<0) |
| 359 | #define USE_PAGER (1<<1) |
| 360 | /* |
| 361 | * require working tree to be present -- anything uses this needs |
| 362 | * RUN_SETUP for reading from the configuration file. |
| 363 | */ |
| 364 | #define NEED_WORK_TREE (1<<2) |
| 365 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 366 | static int run_builtin(struct cmd_struct *p, int argc, const char **argv) |
| 367 | { |
| 368 | int status; |
| 369 | struct stat st; |
| 370 | const char *prefix; |
Masami Hiramatsu | b2348e1 | 2014-08-14 02:22:32 +0000 | [diff] [blame] | 371 | char sbuf[STRERR_BUFSIZE]; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 372 | |
| 373 | prefix = NULL; |
| 374 | if (p->option & RUN_SETUP) |
| 375 | prefix = NULL; /* setup_perf_directory(); */ |
| 376 | |
Arnaldo Carvalho de Melo | 5d06e69 | 2010-05-20 22:01:10 -0300 | [diff] [blame] | 377 | if (use_browser == -1) |
Namhyung Kim | 0020ce2 | 2012-11-12 11:50:17 +0900 | [diff] [blame] | 378 | use_browser = check_browser_config(p->cmd); |
Arnaldo Carvalho de Melo | 5d06e69 | 2010-05-20 22:01:10 -0300 | [diff] [blame] | 379 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 380 | if (use_pager == -1 && p->option & RUN_SETUP) |
| 381 | use_pager = check_pager_config(p->cmd); |
| 382 | if (use_pager == -1 && p->option & USE_PAGER) |
| 383 | use_pager = 1; |
| 384 | commit_pager_choice(); |
| 385 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 386 | status = p->fn(argc, argv, prefix); |
Arnaldo Carvalho de Melo | f3a1f0e | 2010-03-22 13:10:25 -0300 | [diff] [blame] | 387 | exit_browser(status); |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 388 | perf_env__exit(&perf_env); |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 389 | bpf__clear(); |
Arnaldo Carvalho de Melo | f3a1f0e | 2010-03-22 13:10:25 -0300 | [diff] [blame] | 390 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 391 | if (status) |
| 392 | return status & 0xff; |
| 393 | |
| 394 | /* Somebody closed stdout? */ |
| 395 | if (fstat(fileno(stdout), &st)) |
| 396 | return 0; |
| 397 | /* Ignore write errors for pipes and sockets.. */ |
| 398 | if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) |
| 399 | return 0; |
| 400 | |
Arnaldo Carvalho de Melo | 2a16bf8 | 2013-01-24 15:18:54 -0300 | [diff] [blame] | 401 | status = 1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 402 | /* Check for ENOSPC and EIO errors.. */ |
Arnaldo Carvalho de Melo | 2a16bf8 | 2013-01-24 15:18:54 -0300 | [diff] [blame] | 403 | if (fflush(stdout)) { |
Masami Hiramatsu | b2348e1 | 2014-08-14 02:22:32 +0000 | [diff] [blame] | 404 | fprintf(stderr, "write failure on standard output: %s", |
| 405 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Arnaldo Carvalho de Melo | 2a16bf8 | 2013-01-24 15:18:54 -0300 | [diff] [blame] | 406 | goto out; |
| 407 | } |
| 408 | if (ferror(stdout)) { |
| 409 | fprintf(stderr, "unknown write failure on standard output"); |
| 410 | goto out; |
| 411 | } |
| 412 | if (fclose(stdout)) { |
Masami Hiramatsu | b2348e1 | 2014-08-14 02:22:32 +0000 | [diff] [blame] | 413 | fprintf(stderr, "close failed on standard output: %s", |
| 414 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Arnaldo Carvalho de Melo | 2a16bf8 | 2013-01-24 15:18:54 -0300 | [diff] [blame] | 415 | goto out; |
| 416 | } |
| 417 | status = 0; |
| 418 | out: |
| 419 | return status; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void handle_internal_command(int argc, const char **argv) |
| 423 | { |
| 424 | const char *cmd = argv[0]; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 425 | unsigned int i; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 426 | static const char ext[] = STRIP_EXTENSION; |
| 427 | |
| 428 | if (sizeof(ext) > 1) { |
| 429 | i = strlen(argv[0]) - strlen(ext); |
| 430 | if (i > 0 && !strcmp(argv[0] + i, ext)) { |
| 431 | char *argv0 = strdup(argv[0]); |
| 432 | argv[0] = cmd = argv0; |
| 433 | argv0[i] = '\0'; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /* Turn "perf cmd --help" into "perf help cmd" */ |
| 438 | if (argc > 1 && !strcmp(argv[1], "--help")) { |
| 439 | argv[1] = argv[0]; |
| 440 | argv[0] = cmd = "help"; |
| 441 | } |
| 442 | |
| 443 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
| 444 | struct cmd_struct *p = commands+i; |
| 445 | if (strcmp(p->cmd, cmd)) |
| 446 | continue; |
| 447 | exit(run_builtin(p, argc, argv)); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | static void execv_dashed_external(const char **argv) |
| 452 | { |
| 453 | struct strbuf cmd = STRBUF_INIT; |
| 454 | const char *tmp; |
| 455 | int status; |
| 456 | |
| 457 | strbuf_addf(&cmd, "perf-%s", argv[0]); |
| 458 | |
| 459 | /* |
| 460 | * argv[0] must be the perf command, but the argv array |
| 461 | * belongs to the caller, and may be reused in |
| 462 | * subsequent loop iterations. Save argv[0] and |
| 463 | * restore it on error. |
| 464 | */ |
| 465 | tmp = argv[0]; |
| 466 | argv[0] = cmd.buf; |
| 467 | |
| 468 | /* |
| 469 | * if we fail because the command is not found, it is |
| 470 | * OK to return. Otherwise, we just pass along the status code. |
| 471 | */ |
| 472 | status = run_command_v_opt(argv, 0); |
| 473 | if (status != -ERR_RUN_COMMAND_EXEC) { |
| 474 | if (IS_RUN_COMMAND_ERR(status)) |
| 475 | die("unable to run '%s'", argv[0]); |
| 476 | exit(-status); |
| 477 | } |
| 478 | errno = ENOENT; /* as if we called execvp */ |
| 479 | |
| 480 | argv[0] = tmp; |
| 481 | |
| 482 | strbuf_release(&cmd); |
| 483 | } |
| 484 | |
| 485 | static int run_argv(int *argcp, const char ***argv) |
| 486 | { |
| 487 | int done_alias = 0; |
| 488 | |
| 489 | while (1) { |
| 490 | /* See if it's an internal command */ |
| 491 | handle_internal_command(*argcp, *argv); |
| 492 | |
| 493 | /* .. then try the external ones */ |
| 494 | execv_dashed_external(*argv); |
| 495 | |
| 496 | /* It could be an alias -- this works around the insanity |
| 497 | * of overriding "perf log" with "perf show" by having |
| 498 | * alias.log = show |
| 499 | */ |
| 500 | if (done_alias || !handle_alias(argcp, argv)) |
| 501 | break; |
| 502 | done_alias = 1; |
| 503 | } |
| 504 | |
| 505 | return done_alias; |
| 506 | } |
| 507 | |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 508 | static void pthread__block_sigwinch(void) |
| 509 | { |
| 510 | sigset_t set; |
| 511 | |
| 512 | sigemptyset(&set); |
| 513 | sigaddset(&set, SIGWINCH); |
| 514 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 515 | } |
| 516 | |
| 517 | void pthread__unblock_sigwinch(void) |
| 518 | { |
| 519 | sigset_t set; |
| 520 | |
| 521 | sigemptyset(&set); |
| 522 | sigaddset(&set, SIGWINCH); |
| 523 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 524 | } |
| 525 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 526 | int main(int argc, const char **argv) |
| 527 | { |
| 528 | const char *cmd; |
Masami Hiramatsu | b2348e1 | 2014-08-14 02:22:32 +0000 | [diff] [blame] | 529 | char sbuf[STRERR_BUFSIZE]; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 530 | |
Jiri Olsa | 918512b | 2013-09-12 18:39:35 +0200 | [diff] [blame] | 531 | /* The page_size is placed in util object. */ |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 532 | page_size = sysconf(_SC_PAGE_SIZE); |
Don Zickus | 2b1b710 | 2014-05-30 16:10:05 -0400 | [diff] [blame] | 533 | cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 534 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 535 | cmd = perf_extract_argv0_path(argv[0]); |
| 536 | if (!cmd) |
| 537 | cmd = "perf-help"; |
Jiri Olsa | 65d4b26 | 2015-09-02 09:56:33 +0200 | [diff] [blame] | 538 | |
| 539 | /* get debugfs/tracefs mount point from /proc/mounts */ |
| 540 | tracing_path_mount(); |
| 541 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 542 | /* |
| 543 | * "perf-xxxx" is the same as "perf xxxx", but we obviously: |
| 544 | * |
| 545 | * - cannot take flags in between the "perf" and the "xxxx". |
| 546 | * - cannot execute it externally (since it would just do |
| 547 | * the same thing over again) |
| 548 | * |
| 549 | * So we just directly call the internal command handler, and |
| 550 | * die if that one cannot handle it. |
| 551 | */ |
| 552 | if (!prefixcmp(cmd, "perf-")) { |
Peter Zijlstra | 266dfb0 | 2009-05-25 14:45:24 +0200 | [diff] [blame] | 553 | cmd += 5; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 554 | argv[0] = cmd; |
| 555 | handle_internal_command(argc, argv); |
Arnaldo Carvalho de Melo | 2a16bf8 | 2013-01-24 15:18:54 -0300 | [diff] [blame] | 556 | fprintf(stderr, "cannot handle %s internally", cmd); |
| 557 | goto out; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 558 | } |
Arnaldo Carvalho de Melo | b52bc23 | 2013-09-24 11:56:36 -0300 | [diff] [blame] | 559 | if (!prefixcmp(cmd, "trace")) { |
Arnaldo Carvalho de Melo | 1b572622 | 2014-05-26 16:02:29 -0300 | [diff] [blame] | 560 | #ifdef HAVE_LIBAUDIT_SUPPORT |
Jiri Olsa | 99ce8e9 | 2014-12-01 20:06:24 +0100 | [diff] [blame] | 561 | set_buildid_dir(NULL); |
Arnaldo Carvalho de Melo | b52bc23 | 2013-09-24 11:56:36 -0300 | [diff] [blame] | 562 | setup_path(); |
| 563 | argv[0] = "trace"; |
| 564 | return cmd_trace(argc, argv, NULL); |
Arnaldo Carvalho de Melo | 1b572622 | 2014-05-26 16:02:29 -0300 | [diff] [blame] | 565 | #else |
| 566 | fprintf(stderr, |
| 567 | "trace command not available: missing audit-libs devel package at build time.\n"); |
| 568 | goto out; |
Arnaldo Carvalho de Melo | b52bc23 | 2013-09-24 11:56:36 -0300 | [diff] [blame] | 569 | #endif |
Arnaldo Carvalho de Melo | 1b572622 | 2014-05-26 16:02:29 -0300 | [diff] [blame] | 570 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 571 | /* Look for flags.. */ |
| 572 | argv++; |
| 573 | argc--; |
| 574 | handle_options(&argv, &argc, NULL); |
| 575 | commit_pager_choice(); |
Jiri Olsa | 99ce8e9 | 2014-12-01 20:06:24 +0100 | [diff] [blame] | 576 | set_buildid_dir(NULL); |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 577 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 578 | if (argc > 0) { |
| 579 | if (!prefixcmp(argv[0], "--")) |
| 580 | argv[0] += 2; |
| 581 | } else { |
| 582 | /* The user didn't specify a command; give them help */ |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 583 | printf("\n usage: %s\n\n", perf_usage_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 584 | list_common_cmds_help(); |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 585 | printf("\n %s\n\n", perf_more_info_string); |
Arnaldo Carvalho de Melo | 2a16bf8 | 2013-01-24 15:18:54 -0300 | [diff] [blame] | 586 | goto out; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 587 | } |
| 588 | cmd = argv[0]; |
| 589 | |
Jiri Olsa | 52502bf | 2012-10-31 15:52:47 +0100 | [diff] [blame] | 590 | test_attr__init(); |
| 591 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 592 | /* |
| 593 | * We use PATH to find perf commands, but we prepend some higher |
Uwe Kleine-König | 659431f | 2010-01-18 16:02:48 +0100 | [diff] [blame] | 594 | * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 595 | * environment, and the $(perfexecdir) from the Makefile at build |
| 596 | * time. |
| 597 | */ |
| 598 | setup_path(); |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 599 | /* |
| 600 | * Block SIGWINCH notifications so that the thread that wants it can |
| 601 | * unblock and get syscalls like select interrupted instead of waiting |
| 602 | * forever while the signal goes to some other non interested thread. |
| 603 | */ |
| 604 | pthread__block_sigwinch(); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 605 | |
| 606 | while (1) { |
Thiago Farina | 4c57415 | 2010-01-27 21:05:55 -0200 | [diff] [blame] | 607 | static int done_help; |
Arnaldo Carvalho de Melo | b5ded71 | 2013-03-27 11:00:07 -0300 | [diff] [blame] | 608 | int was_alias = run_argv(&argc, &argv); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 609 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 610 | if (errno != ENOENT) |
| 611 | break; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 612 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 613 | if (was_alias) { |
| 614 | fprintf(stderr, "Expansion of alias '%s' failed; " |
| 615 | "'%s' is not a perf-command\n", |
| 616 | cmd, argv[0]); |
Arnaldo Carvalho de Melo | 2a16bf8 | 2013-01-24 15:18:54 -0300 | [diff] [blame] | 617 | goto out; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 618 | } |
| 619 | if (!done_help) { |
| 620 | cmd = argv[0] = help_unknown_cmd(cmd); |
| 621 | done_help = 1; |
| 622 | } else |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | fprintf(stderr, "Failed to run command '%s': %s\n", |
Masami Hiramatsu | b2348e1 | 2014-08-14 02:22:32 +0000 | [diff] [blame] | 627 | cmd, strerror_r(errno, sbuf, sizeof(sbuf))); |
Arnaldo Carvalho de Melo | 2a16bf8 | 2013-01-24 15:18:54 -0300 | [diff] [blame] | 628 | out: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 629 | return 1; |
| 630 | } |