Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Ingo Molnar | 148be2c | 2009-04-27 08:02:14 +0200 | [diff] [blame] | 2 | #include "util/exec_cmd.h" |
| 3 | #include "util/cache.h" |
| 4 | #include "util/quote.h" |
| 5 | #include "util/run-command.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 6 | |
| 7 | const char perf_usage_string[] = |
Ingo Molnar | cc13a59 | 2009-04-20 16:01:30 +0200 | [diff] [blame] | 8 | "perf [--version] [--help] COMMAND [ARGS]"; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 9 | |
| 10 | const char perf_more_info_string[] = |
| 11 | "See 'perf help COMMAND' for more information on a specific command."; |
| 12 | |
| 13 | static int use_pager = -1; |
| 14 | struct pager_config { |
| 15 | const char *cmd; |
| 16 | int val; |
| 17 | }; |
| 18 | |
| 19 | static int pager_command_config(const char *var, const char *value, void *data) |
| 20 | { |
| 21 | struct pager_config *c = data; |
| 22 | if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd)) |
| 23 | c->val = perf_config_bool(var, value); |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */ |
| 28 | int check_pager_config(const char *cmd) |
| 29 | { |
| 30 | struct pager_config c; |
| 31 | c.cmd = cmd; |
| 32 | c.val = -1; |
| 33 | perf_config(pager_command_config, &c); |
| 34 | return c.val; |
| 35 | } |
| 36 | |
| 37 | static void commit_pager_choice(void) { |
| 38 | switch (use_pager) { |
| 39 | case 0: |
| 40 | setenv("PERF_PAGER", "cat", 1); |
| 41 | break; |
| 42 | case 1: |
| 43 | /* setup_pager(); */ |
| 44 | break; |
| 45 | default: |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static int handle_options(const char*** argv, int* argc, int* envchanged) |
| 51 | { |
| 52 | int handled = 0; |
| 53 | |
| 54 | while (*argc > 0) { |
| 55 | const char *cmd = (*argv)[0]; |
| 56 | if (cmd[0] != '-') |
| 57 | break; |
| 58 | |
| 59 | /* |
| 60 | * For legacy reasons, the "version" and "help" |
| 61 | * commands can be written with "--" prepended |
| 62 | * to make them look like flags. |
| 63 | */ |
| 64 | if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) |
| 65 | break; |
| 66 | |
| 67 | /* |
| 68 | * Check remaining flags. |
| 69 | */ |
| 70 | if (!prefixcmp(cmd, "--exec-path")) { |
| 71 | cmd += 11; |
| 72 | if (*cmd == '=') |
| 73 | perf_set_argv_exec_path(cmd + 1); |
| 74 | else { |
| 75 | puts(perf_exec_path()); |
| 76 | exit(0); |
| 77 | } |
| 78 | } else if (!strcmp(cmd, "--html-path")) { |
| 79 | puts(system_path(PERF_HTML_PATH)); |
| 80 | exit(0); |
| 81 | } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { |
| 82 | use_pager = 1; |
| 83 | } else if (!strcmp(cmd, "--no-pager")) { |
| 84 | use_pager = 0; |
| 85 | if (envchanged) |
| 86 | *envchanged = 1; |
| 87 | } else if (!strcmp(cmd, "--perf-dir")) { |
| 88 | if (*argc < 2) { |
| 89 | fprintf(stderr, "No directory given for --perf-dir.\n" ); |
| 90 | usage(perf_usage_string); |
| 91 | } |
| 92 | setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1); |
| 93 | if (envchanged) |
| 94 | *envchanged = 1; |
| 95 | (*argv)++; |
| 96 | (*argc)--; |
| 97 | handled++; |
| 98 | } else if (!prefixcmp(cmd, "--perf-dir=")) { |
| 99 | setenv(PERF_DIR_ENVIRONMENT, cmd + 10, 1); |
| 100 | if (envchanged) |
| 101 | *envchanged = 1; |
| 102 | } else if (!strcmp(cmd, "--work-tree")) { |
| 103 | if (*argc < 2) { |
| 104 | fprintf(stderr, "No directory given for --work-tree.\n" ); |
| 105 | usage(perf_usage_string); |
| 106 | } |
| 107 | setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1); |
| 108 | if (envchanged) |
| 109 | *envchanged = 1; |
| 110 | (*argv)++; |
| 111 | (*argc)--; |
| 112 | } else if (!prefixcmp(cmd, "--work-tree=")) { |
| 113 | setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + 12, 1); |
| 114 | if (envchanged) |
| 115 | *envchanged = 1; |
| 116 | } else { |
| 117 | fprintf(stderr, "Unknown option: %s\n", cmd); |
| 118 | usage(perf_usage_string); |
| 119 | } |
| 120 | |
| 121 | (*argv)++; |
| 122 | (*argc)--; |
| 123 | handled++; |
| 124 | } |
| 125 | return handled; |
| 126 | } |
| 127 | |
| 128 | static int handle_alias(int *argcp, const char ***argv) |
| 129 | { |
| 130 | int envchanged = 0, ret = 0, saved_errno = errno; |
| 131 | int count, option_count; |
| 132 | const char** new_argv; |
| 133 | const char *alias_command; |
| 134 | char *alias_string; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 135 | |
| 136 | alias_command = (*argv)[0]; |
| 137 | alias_string = alias_lookup(alias_command); |
| 138 | if (alias_string) { |
| 139 | if (alias_string[0] == '!') { |
| 140 | if (*argcp > 1) { |
| 141 | struct strbuf buf; |
| 142 | |
| 143 | strbuf_init(&buf, PATH_MAX); |
| 144 | strbuf_addstr(&buf, alias_string); |
| 145 | sq_quote_argv(&buf, (*argv) + 1, PATH_MAX); |
| 146 | free(alias_string); |
| 147 | alias_string = buf.buf; |
| 148 | } |
| 149 | ret = system(alias_string + 1); |
| 150 | if (ret >= 0 && WIFEXITED(ret) && |
| 151 | WEXITSTATUS(ret) != 127) |
| 152 | exit(WEXITSTATUS(ret)); |
| 153 | die("Failed to run '%s' when expanding alias '%s'", |
| 154 | alias_string + 1, alias_command); |
| 155 | } |
| 156 | count = split_cmdline(alias_string, &new_argv); |
| 157 | if (count < 0) |
| 158 | die("Bad alias.%s string", alias_command); |
| 159 | option_count = handle_options(&new_argv, &count, &envchanged); |
| 160 | if (envchanged) |
| 161 | die("alias '%s' changes environment variables\n" |
| 162 | "You can use '!perf' in the alias to do this.", |
| 163 | alias_command); |
| 164 | memmove(new_argv - option_count, new_argv, |
| 165 | count * sizeof(char *)); |
| 166 | new_argv -= option_count; |
| 167 | |
| 168 | if (count < 1) |
| 169 | die("empty alias for %s", alias_command); |
| 170 | |
| 171 | if (!strcmp(alias_command, new_argv[0])) |
| 172 | die("recursive alias: %s", alias_command); |
| 173 | |
| 174 | new_argv = realloc(new_argv, sizeof(char*) * |
| 175 | (count + *argcp + 1)); |
| 176 | /* insert after command name */ |
| 177 | memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp); |
| 178 | new_argv[count+*argcp] = NULL; |
| 179 | |
| 180 | *argv = new_argv; |
| 181 | *argcp += count - 1; |
| 182 | |
| 183 | ret = 1; |
| 184 | } |
| 185 | |
| 186 | errno = saved_errno; |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | const char perf_version_string[] = PERF_VERSION; |
| 192 | |
| 193 | #define RUN_SETUP (1<<0) |
| 194 | #define USE_PAGER (1<<1) |
| 195 | /* |
| 196 | * require working tree to be present -- anything uses this needs |
| 197 | * RUN_SETUP for reading from the configuration file. |
| 198 | */ |
| 199 | #define NEED_WORK_TREE (1<<2) |
| 200 | |
| 201 | struct cmd_struct { |
| 202 | const char *cmd; |
| 203 | int (*fn)(int, const char **, const char *); |
| 204 | int option; |
| 205 | }; |
| 206 | |
| 207 | static int run_builtin(struct cmd_struct *p, int argc, const char **argv) |
| 208 | { |
| 209 | int status; |
| 210 | struct stat st; |
| 211 | const char *prefix; |
| 212 | |
| 213 | prefix = NULL; |
| 214 | if (p->option & RUN_SETUP) |
| 215 | prefix = NULL; /* setup_perf_directory(); */ |
| 216 | |
| 217 | if (use_pager == -1 && p->option & RUN_SETUP) |
| 218 | use_pager = check_pager_config(p->cmd); |
| 219 | if (use_pager == -1 && p->option & USE_PAGER) |
| 220 | use_pager = 1; |
| 221 | commit_pager_choice(); |
| 222 | |
| 223 | if (p->option & NEED_WORK_TREE) |
| 224 | /* setup_work_tree() */; |
| 225 | |
| 226 | status = p->fn(argc, argv, prefix); |
| 227 | if (status) |
| 228 | return status & 0xff; |
| 229 | |
| 230 | /* Somebody closed stdout? */ |
| 231 | if (fstat(fileno(stdout), &st)) |
| 232 | return 0; |
| 233 | /* Ignore write errors for pipes and sockets.. */ |
| 234 | if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) |
| 235 | return 0; |
| 236 | |
| 237 | /* Check for ENOSPC and EIO errors.. */ |
| 238 | if (fflush(stdout)) |
| 239 | die("write failure on standard output: %s", strerror(errno)); |
| 240 | if (ferror(stdout)) |
| 241 | die("unknown write failure on standard output"); |
| 242 | if (fclose(stdout)) |
| 243 | die("close failed on standard output: %s", strerror(errno)); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static void handle_internal_command(int argc, const char **argv) |
| 248 | { |
| 249 | const char *cmd = argv[0]; |
| 250 | static struct cmd_struct commands[] = { |
Ingo Molnar | 6142fdd | 2009-04-20 16:05:55 +0200 | [diff] [blame] | 251 | { "help", cmd_help, 0 }, |
Ingo Molnar | e33e0a4 | 2009-04-20 15:58:01 +0200 | [diff] [blame] | 252 | { "record", cmd_record, 0 }, |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame^] | 253 | { "report", cmd_report, 0 }, |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 254 | { "stat", cmd_stat, 0 }, |
Ingo Molnar | e33e0a4 | 2009-04-20 15:58:01 +0200 | [diff] [blame] | 255 | { "top", cmd_top, 0 }, |
Ingo Molnar | cc13a59 | 2009-04-20 16:01:30 +0200 | [diff] [blame] | 256 | { "version", cmd_version, 0 }, |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 257 | }; |
| 258 | int i; |
| 259 | static const char ext[] = STRIP_EXTENSION; |
| 260 | |
| 261 | if (sizeof(ext) > 1) { |
| 262 | i = strlen(argv[0]) - strlen(ext); |
| 263 | if (i > 0 && !strcmp(argv[0] + i, ext)) { |
| 264 | char *argv0 = strdup(argv[0]); |
| 265 | argv[0] = cmd = argv0; |
| 266 | argv0[i] = '\0'; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /* Turn "perf cmd --help" into "perf help cmd" */ |
| 271 | if (argc > 1 && !strcmp(argv[1], "--help")) { |
| 272 | argv[1] = argv[0]; |
| 273 | argv[0] = cmd = "help"; |
| 274 | } |
| 275 | |
| 276 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
| 277 | struct cmd_struct *p = commands+i; |
| 278 | if (strcmp(p->cmd, cmd)) |
| 279 | continue; |
| 280 | exit(run_builtin(p, argc, argv)); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | static void execv_dashed_external(const char **argv) |
| 285 | { |
| 286 | struct strbuf cmd = STRBUF_INIT; |
| 287 | const char *tmp; |
| 288 | int status; |
| 289 | |
| 290 | strbuf_addf(&cmd, "perf-%s", argv[0]); |
| 291 | |
| 292 | /* |
| 293 | * argv[0] must be the perf command, but the argv array |
| 294 | * belongs to the caller, and may be reused in |
| 295 | * subsequent loop iterations. Save argv[0] and |
| 296 | * restore it on error. |
| 297 | */ |
| 298 | tmp = argv[0]; |
| 299 | argv[0] = cmd.buf; |
| 300 | |
| 301 | /* |
| 302 | * if we fail because the command is not found, it is |
| 303 | * OK to return. Otherwise, we just pass along the status code. |
| 304 | */ |
| 305 | status = run_command_v_opt(argv, 0); |
| 306 | if (status != -ERR_RUN_COMMAND_EXEC) { |
| 307 | if (IS_RUN_COMMAND_ERR(status)) |
| 308 | die("unable to run '%s'", argv[0]); |
| 309 | exit(-status); |
| 310 | } |
| 311 | errno = ENOENT; /* as if we called execvp */ |
| 312 | |
| 313 | argv[0] = tmp; |
| 314 | |
| 315 | strbuf_release(&cmd); |
| 316 | } |
| 317 | |
| 318 | static int run_argv(int *argcp, const char ***argv) |
| 319 | { |
| 320 | int done_alias = 0; |
| 321 | |
| 322 | while (1) { |
| 323 | /* See if it's an internal command */ |
| 324 | handle_internal_command(*argcp, *argv); |
| 325 | |
| 326 | /* .. then try the external ones */ |
| 327 | execv_dashed_external(*argv); |
| 328 | |
| 329 | /* It could be an alias -- this works around the insanity |
| 330 | * of overriding "perf log" with "perf show" by having |
| 331 | * alias.log = show |
| 332 | */ |
| 333 | if (done_alias || !handle_alias(argcp, argv)) |
| 334 | break; |
| 335 | done_alias = 1; |
| 336 | } |
| 337 | |
| 338 | return done_alias; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | int main(int argc, const char **argv) |
| 343 | { |
| 344 | const char *cmd; |
| 345 | |
| 346 | cmd = perf_extract_argv0_path(argv[0]); |
| 347 | if (!cmd) |
| 348 | cmd = "perf-help"; |
| 349 | |
| 350 | /* |
| 351 | * "perf-xxxx" is the same as "perf xxxx", but we obviously: |
| 352 | * |
| 353 | * - cannot take flags in between the "perf" and the "xxxx". |
| 354 | * - cannot execute it externally (since it would just do |
| 355 | * the same thing over again) |
| 356 | * |
| 357 | * So we just directly call the internal command handler, and |
| 358 | * die if that one cannot handle it. |
| 359 | */ |
| 360 | if (!prefixcmp(cmd, "perf-")) { |
Peter Zijlstra | 266dfb0 | 2009-05-25 14:45:24 +0200 | [diff] [blame] | 361 | cmd += 5; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 362 | argv[0] = cmd; |
| 363 | handle_internal_command(argc, argv); |
| 364 | die("cannot handle %s internally", cmd); |
| 365 | } |
| 366 | |
| 367 | /* Look for flags.. */ |
| 368 | argv++; |
| 369 | argc--; |
| 370 | handle_options(&argv, &argc, NULL); |
| 371 | commit_pager_choice(); |
| 372 | if (argc > 0) { |
| 373 | if (!prefixcmp(argv[0], "--")) |
| 374 | argv[0] += 2; |
| 375 | } else { |
| 376 | /* The user didn't specify a command; give them help */ |
| 377 | printf("usage: %s\n\n", perf_usage_string); |
| 378 | list_common_cmds_help(); |
| 379 | printf("\n%s\n", perf_more_info_string); |
| 380 | exit(1); |
| 381 | } |
| 382 | cmd = argv[0]; |
| 383 | |
| 384 | /* |
| 385 | * We use PATH to find perf commands, but we prepend some higher |
| 386 | * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH |
| 387 | * environment, and the $(perfexecdir) from the Makefile at build |
| 388 | * time. |
| 389 | */ |
| 390 | setup_path(); |
| 391 | |
| 392 | while (1) { |
| 393 | static int done_help = 0; |
| 394 | static int was_alias = 0; |
| 395 | was_alias = run_argv(&argc, &argv); |
| 396 | if (errno != ENOENT) |
| 397 | break; |
| 398 | if (was_alias) { |
| 399 | fprintf(stderr, "Expansion of alias '%s' failed; " |
| 400 | "'%s' is not a perf-command\n", |
| 401 | cmd, argv[0]); |
| 402 | exit(1); |
| 403 | } |
| 404 | if (!done_help) { |
| 405 | cmd = argv[0] = help_unknown_cmd(cmd); |
| 406 | done_help = 1; |
| 407 | } else |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | fprintf(stderr, "Failed to run command '%s': %s\n", |
| 412 | cmd, strerror(errno)); |
| 413 | |
| 414 | return 1; |
| 415 | } |