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