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