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