1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * perf.c 4 * 5 * Performance analysis utility. 6 * 7 * This is the main hub from which the sub-commands (perf stat, 8 * perf top, perf record, perf report, etc.) are started. 9 */ 10 #include "builtin.h" 11 #include "perf.h" 12 13 #include "util/env.h" 14 #include <subcmd/exec-cmd.h> 15 #include "util/config.h" 16 #include <subcmd/run-command.h> 17 #include "util/parse-events.h" 18 #include <subcmd/parse-options.h> 19 #include "util/bpf-loader.h" 20 #include "util/debug.h" 21 #include "util/event.h" 22 #include "util/util.h" 23 #include "perf-sys.h" 24 #include <api/fs/fs.h> 25 #include <api/fs/tracing_path.h> 26 #include <errno.h> 27 #include <pthread.h> 28 #include <signal.h> 29 #include <stdlib.h> 30 #include <time.h> 31 #include <sys/types.h> 32 #include <sys/stat.h> 33 #include <unistd.h> 34 #include <linux/kernel.h> 35 #include <linux/zalloc.h> 36 37 const char perf_usage_string[] = 38 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]"; 39 40 const char perf_more_info_string[] = 41 "See 'perf help COMMAND' for more information on a specific command."; 42 43 static int use_pager = -1; 44 const char *input_name; 45 46 struct cmd_struct { 47 const char *cmd; 48 int (*fn)(int, const char **); 49 int option; 50 }; 51 52 static struct cmd_struct commands[] = { 53 { "buildid-cache", cmd_buildid_cache, 0 }, 54 { "buildid-list", cmd_buildid_list, 0 }, 55 { "config", cmd_config, 0 }, 56 { "c2c", cmd_c2c, 0 }, 57 { "diff", cmd_diff, 0 }, 58 { "evlist", cmd_evlist, 0 }, 59 { "help", cmd_help, 0 }, 60 { "kallsyms", cmd_kallsyms, 0 }, 61 { "list", cmd_list, 0 }, 62 { "record", cmd_record, 0 }, 63 { "report", cmd_report, 0 }, 64 { "bench", cmd_bench, 0 }, 65 { "stat", cmd_stat, 0 }, 66 { "timechart", cmd_timechart, 0 }, 67 { "top", cmd_top, 0 }, 68 { "annotate", cmd_annotate, 0 }, 69 { "version", cmd_version, 0 }, 70 { "script", cmd_script, 0 }, 71 { "sched", cmd_sched, 0 }, 72 #ifdef HAVE_LIBELF_SUPPORT 73 { "probe", cmd_probe, 0 }, 74 #endif 75 { "kmem", cmd_kmem, 0 }, 76 { "lock", cmd_lock, 0 }, 77 { "kvm", cmd_kvm, 0 }, 78 { "test", cmd_test, 0 }, 79 #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT) 80 { "trace", cmd_trace, 0 }, 81 #endif 82 { "inject", cmd_inject, 0 }, 83 { "mem", cmd_mem, 0 }, 84 { "data", cmd_data, 0 }, 85 { "ftrace", cmd_ftrace, 0 }, 86 }; 87 88 struct pager_config { 89 const char *cmd; 90 int val; 91 }; 92 93 static int pager_command_config(const char *var, const char *value, void *data) 94 { 95 struct pager_config *c = data; 96 if (strstarts(var, "pager.") && !strcmp(var + 6, c->cmd)) 97 c->val = perf_config_bool(var, value); 98 return 0; 99 } 100 101 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */ 102 static int check_pager_config(const char *cmd) 103 { 104 int err; 105 struct pager_config c; 106 c.cmd = cmd; 107 c.val = -1; 108 err = perf_config(pager_command_config, &c); 109 return err ?: c.val; 110 } 111 112 static int browser_command_config(const char *var, const char *value, void *data) 113 { 114 struct pager_config *c = data; 115 if (strstarts(var, "tui.") && !strcmp(var + 4, c->cmd)) 116 c->val = perf_config_bool(var, value); 117 if (strstarts(var, "gtk.") && !strcmp(var + 4, c->cmd)) 118 c->val = perf_config_bool(var, value) ? 2 : 0; 119 return 0; 120 } 121 122 /* 123 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk", 124 * and -1 for "not specified" 125 */ 126 static int check_browser_config(const char *cmd) 127 { 128 int err; 129 struct pager_config c; 130 c.cmd = cmd; 131 c.val = -1; 132 err = perf_config(browser_command_config, &c); 133 return err ?: c.val; 134 } 135 136 static void commit_pager_choice(void) 137 { 138 switch (use_pager) { 139 case 0: 140 setenv(PERF_PAGER_ENVIRONMENT, "cat", 1); 141 break; 142 case 1: 143 /* setup_pager(); */ 144 break; 145 default: 146 break; 147 } 148 } 149 150 struct option options[] = { 151 OPT_ARGUMENT("help", "help"), 152 OPT_ARGUMENT("version", "version"), 153 OPT_ARGUMENT("exec-path", "exec-path"), 154 OPT_ARGUMENT("html-path", "html-path"), 155 OPT_ARGUMENT("paginate", "paginate"), 156 OPT_ARGUMENT("no-pager", "no-pager"), 157 OPT_ARGUMENT("debugfs-dir", "debugfs-dir"), 158 OPT_ARGUMENT("buildid-dir", "buildid-dir"), 159 OPT_ARGUMENT("list-cmds", "list-cmds"), 160 OPT_ARGUMENT("list-opts", "list-opts"), 161 OPT_ARGUMENT("debug", "debug"), 162 OPT_END() 163 }; 164 165 static int handle_options(const char ***argv, int *argc, int *envchanged) 166 { 167 int handled = 0; 168 169 while (*argc > 0) { 170 const char *cmd = (*argv)[0]; 171 if (cmd[0] != '-') 172 break; 173 174 /* 175 * For legacy reasons, the "version" and "help" 176 * commands can be written with "--" prepended 177 * to make them look like flags. 178 */ 179 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) 180 break; 181 182 /* 183 * Shortcut for '-h' and '-v' options to invoke help 184 * and version command. 185 */ 186 if (!strcmp(cmd, "-h")) { 187 (*argv)[0] = "--help"; 188 break; 189 } 190 191 if (!strcmp(cmd, "-v")) { 192 (*argv)[0] = "--version"; 193 break; 194 } 195 196 if (!strcmp(cmd, "-vv")) { 197 (*argv)[0] = "version"; 198 version_verbose = 1; 199 break; 200 } 201 202 /* 203 * Check remaining flags. 204 */ 205 if (strstarts(cmd, CMD_EXEC_PATH)) { 206 cmd += strlen(CMD_EXEC_PATH); 207 if (*cmd == '=') 208 set_argv_exec_path(cmd + 1); 209 else { 210 puts(get_argv_exec_path()); 211 exit(0); 212 } 213 } else if (!strcmp(cmd, "--html-path")) { 214 puts(system_path(PERF_HTML_PATH)); 215 exit(0); 216 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { 217 use_pager = 1; 218 } else if (!strcmp(cmd, "--no-pager")) { 219 use_pager = 0; 220 if (envchanged) 221 *envchanged = 1; 222 } else if (!strcmp(cmd, "--debugfs-dir")) { 223 if (*argc < 2) { 224 fprintf(stderr, "No directory given for --debugfs-dir.\n"); 225 usage(perf_usage_string); 226 } 227 tracing_path_set((*argv)[1]); 228 if (envchanged) 229 *envchanged = 1; 230 (*argv)++; 231 (*argc)--; 232 } else if (!strcmp(cmd, "--buildid-dir")) { 233 if (*argc < 2) { 234 fprintf(stderr, "No directory given for --buildid-dir.\n"); 235 usage(perf_usage_string); 236 } 237 set_buildid_dir((*argv)[1]); 238 if (envchanged) 239 *envchanged = 1; 240 (*argv)++; 241 (*argc)--; 242 } else if (strstarts(cmd, CMD_DEBUGFS_DIR)) { 243 tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR)); 244 fprintf(stderr, "dir: %s\n", tracing_path_mount()); 245 if (envchanged) 246 *envchanged = 1; 247 } else if (!strcmp(cmd, "--list-cmds")) { 248 unsigned int i; 249 250 for (i = 0; i < ARRAY_SIZE(commands); i++) { 251 struct cmd_struct *p = commands+i; 252 printf("%s ", p->cmd); 253 } 254 putchar('\n'); 255 exit(0); 256 } else if (!strcmp(cmd, "--list-opts")) { 257 unsigned int i; 258 259 for (i = 0; i < ARRAY_SIZE(options)-1; i++) { 260 struct option *p = options+i; 261 printf("--%s ", p->long_name); 262 } 263 putchar('\n'); 264 exit(0); 265 } else if (!strcmp(cmd, "--debug")) { 266 if (*argc < 2) { 267 fprintf(stderr, "No variable specified for --debug.\n"); 268 usage(perf_usage_string); 269 } 270 if (perf_debug_option((*argv)[1])) 271 usage(perf_usage_string); 272 273 (*argv)++; 274 (*argc)--; 275 } else { 276 fprintf(stderr, "Unknown option: %s\n", cmd); 277 usage(perf_usage_string); 278 } 279 280 (*argv)++; 281 (*argc)--; 282 handled++; 283 } 284 return handled; 285 } 286 287 #define RUN_SETUP (1<<0) 288 #define USE_PAGER (1<<1) 289 290 static int run_builtin(struct cmd_struct *p, int argc, const char **argv) 291 { 292 int status; 293 struct stat st; 294 char sbuf[STRERR_BUFSIZE]; 295 296 if (use_browser == -1) 297 use_browser = check_browser_config(p->cmd); 298 299 if (use_pager == -1 && p->option & RUN_SETUP) 300 use_pager = check_pager_config(p->cmd); 301 if (use_pager == -1 && p->option & USE_PAGER) 302 use_pager = 1; 303 commit_pager_choice(); 304 305 perf_env__init(&perf_env); 306 perf_env__set_cmdline(&perf_env, argc, argv); 307 status = p->fn(argc, argv); 308 perf_config__exit(); 309 exit_browser(status); 310 perf_env__exit(&perf_env); 311 bpf__clear(); 312 313 if (status) 314 return status & 0xff; 315 316 /* Somebody closed stdout? */ 317 if (fstat(fileno(stdout), &st)) 318 return 0; 319 /* Ignore write errors for pipes and sockets.. */ 320 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) 321 return 0; 322 323 status = 1; 324 /* Check for ENOSPC and EIO errors.. */ 325 if (fflush(stdout)) { 326 fprintf(stderr, "write failure on standard output: %s", 327 str_error_r(errno, sbuf, sizeof(sbuf))); 328 goto out; 329 } 330 if (ferror(stdout)) { 331 fprintf(stderr, "unknown write failure on standard output"); 332 goto out; 333 } 334 if (fclose(stdout)) { 335 fprintf(stderr, "close failed on standard output: %s", 336 str_error_r(errno, sbuf, sizeof(sbuf))); 337 goto out; 338 } 339 status = 0; 340 out: 341 return status; 342 } 343 344 static void handle_internal_command(int argc, const char **argv) 345 { 346 const char *cmd = argv[0]; 347 unsigned int i; 348 349 /* Turn "perf cmd --help" into "perf help cmd" */ 350 if (argc > 1 && !strcmp(argv[1], "--help")) { 351 argv[1] = argv[0]; 352 argv[0] = cmd = "help"; 353 } 354 355 for (i = 0; i < ARRAY_SIZE(commands); i++) { 356 struct cmd_struct *p = commands+i; 357 if (strcmp(p->cmd, cmd)) 358 continue; 359 exit(run_builtin(p, argc, argv)); 360 } 361 } 362 363 static void execv_dashed_external(const char **argv) 364 { 365 char *cmd; 366 const char *tmp; 367 int status; 368 369 if (asprintf(&cmd, "perf-%s", argv[0]) < 0) 370 goto do_die; 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; 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 do_die: 389 pr_err("FATAL: unable to run '%s'", argv[0]); 390 status = -128; 391 } 392 exit(-status); 393 } 394 errno = ENOENT; /* as if we called execvp */ 395 396 argv[0] = tmp; 397 zfree(&cmd); 398 } 399 400 static int run_argv(int *argcp, const char ***argv) 401 { 402 /* See if it's an internal command */ 403 handle_internal_command(*argcp, *argv); 404 405 /* .. then try the external ones */ 406 execv_dashed_external(*argv); 407 return 0; 408 } 409 410 static void pthread__block_sigwinch(void) 411 { 412 sigset_t set; 413 414 sigemptyset(&set); 415 sigaddset(&set, SIGWINCH); 416 pthread_sigmask(SIG_BLOCK, &set, NULL); 417 } 418 419 void pthread__unblock_sigwinch(void) 420 { 421 sigset_t set; 422 423 sigemptyset(&set); 424 sigaddset(&set, SIGWINCH); 425 pthread_sigmask(SIG_UNBLOCK, &set, NULL); 426 } 427 428 int main(int argc, const char **argv) 429 { 430 int err; 431 const char *cmd; 432 char sbuf[STRERR_BUFSIZE]; 433 434 /* libsubcmd init */ 435 exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT); 436 pager_init(PERF_PAGER_ENVIRONMENT); 437 438 /* The page_size is placed in util object. */ 439 page_size = sysconf(_SC_PAGE_SIZE); 440 441 cmd = extract_argv0_path(argv[0]); 442 if (!cmd) 443 cmd = "perf-help"; 444 445 srandom(time(NULL)); 446 447 /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */ 448 config_exclusive_filename = getenv("PERF_CONFIG"); 449 450 err = perf_config(perf_default_config, NULL); 451 if (err) 452 return err; 453 set_buildid_dir(NULL); 454 455 /* 456 * "perf-xxxx" is the same as "perf xxxx", but we obviously: 457 * 458 * - cannot take flags in between the "perf" and the "xxxx". 459 * - cannot execute it externally (since it would just do 460 * the same thing over again) 461 * 462 * So we just directly call the internal command handler. If that one 463 * fails to handle this, then maybe we just run a renamed perf binary 464 * that contains a dash in its name. To handle this scenario, we just 465 * fall through and ignore the "xxxx" part of the command string. 466 */ 467 if (strstarts(cmd, "perf-")) { 468 cmd += 5; 469 argv[0] = cmd; 470 handle_internal_command(argc, argv); 471 /* 472 * If the command is handled, the above function does not 473 * return undo changes and fall through in such a case. 474 */ 475 cmd -= 5; 476 argv[0] = cmd; 477 } 478 if (strstarts(cmd, "trace")) { 479 #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT) 480 setup_path(); 481 argv[0] = "trace"; 482 return cmd_trace(argc, argv); 483 #else 484 fprintf(stderr, 485 "trace command not available: missing audit-libs devel package at build time.\n"); 486 goto out; 487 #endif 488 } 489 /* Look for flags.. */ 490 argv++; 491 argc--; 492 handle_options(&argv, &argc, NULL); 493 commit_pager_choice(); 494 495 if (argc > 0) { 496 if (strstarts(argv[0], "--")) 497 argv[0] += 2; 498 } else { 499 /* The user didn't specify a command; give them help */ 500 printf("\n usage: %s\n\n", perf_usage_string); 501 list_common_cmds_help(); 502 printf("\n %s\n\n", perf_more_info_string); 503 goto out; 504 } 505 cmd = argv[0]; 506 507 test_attr__init(); 508 509 /* 510 * We use PATH to find perf commands, but we prepend some higher 511 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH 512 * environment, and the $(perfexecdir) from the Makefile at build 513 * time. 514 */ 515 setup_path(); 516 /* 517 * Block SIGWINCH notifications so that the thread that wants it can 518 * unblock and get syscalls like select interrupted instead of waiting 519 * forever while the signal goes to some other non interested thread. 520 */ 521 pthread__block_sigwinch(); 522 523 perf_debug_setup(); 524 525 while (1) { 526 static int done_help; 527 528 run_argv(&argc, &argv); 529 530 if (errno != ENOENT) 531 break; 532 533 if (!done_help) { 534 cmd = argv[0] = help_unknown_cmd(cmd); 535 done_help = 1; 536 } else 537 break; 538 } 539 540 fprintf(stderr, "Failed to run command '%s': %s\n", 541 cmd, str_error_r(errno, sbuf, sizeof(sbuf))); 542 out: 543 return 1; 544 } 545