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