4 * Performance analysis utility.
6 * This is the main hub from which the sub-commands (perf stat,
7 * perf top, perf record, perf report, etc.) are started.
12 #include "util/exec_cmd.h"
13 #include "util/cache.h"
14 #include "util/quote.h"
15 #include "util/run-command.h"
16 #include "util/parse-events.h"
17 #include "util/parse-options.h"
18 #include "util/bpf-loader.h"
19 #include "util/debug.h"
20 #include <api/fs/tracing_path.h>
23 const char perf_usage_string[] =
24 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
26 const char perf_more_info_string[] =
27 "See 'perf help COMMAND' for more information on a specific command.";
30 static int use_pager = -1;
31 const char *input_name;
35 int (*fn)(int, const char **, const char *);
39 static struct cmd_struct commands[] = {
40 { "buildid-cache", cmd_buildid_cache, 0 },
41 { "buildid-list", cmd_buildid_list, 0 },
42 { "diff", cmd_diff, 0 },
43 { "evlist", cmd_evlist, 0 },
44 { "help", cmd_help, 0 },
45 { "list", cmd_list, 0 },
46 { "record", cmd_record, 0 },
47 { "report", cmd_report, 0 },
48 { "bench", cmd_bench, 0 },
49 { "stat", cmd_stat, 0 },
50 { "timechart", cmd_timechart, 0 },
51 { "top", cmd_top, 0 },
52 { "annotate", cmd_annotate, 0 },
53 { "version", cmd_version, 0 },
54 { "script", cmd_script, 0 },
55 { "sched", cmd_sched, 0 },
56 #ifdef HAVE_LIBELF_SUPPORT
57 { "probe", cmd_probe, 0 },
59 { "kmem", cmd_kmem, 0 },
60 { "lock", cmd_lock, 0 },
61 { "kvm", cmd_kvm, 0 },
62 { "test", cmd_test, 0 },
63 #ifdef HAVE_LIBAUDIT_SUPPORT
64 { "trace", cmd_trace, 0 },
66 { "inject", cmd_inject, 0 },
67 { "mem", cmd_mem, 0 },
68 { "data", cmd_data, 0 },
76 static int pager_command_config(const char *var, const char *value, void *data)
78 struct pager_config *c = data;
79 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
80 c->val = perf_config_bool(var, value);
84 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
85 int check_pager_config(const char *cmd)
87 struct pager_config c;
90 perf_config(pager_command_config, &c);
94 static int browser_command_config(const char *var, const char *value, void *data)
96 struct pager_config *c = data;
97 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
98 c->val = perf_config_bool(var, value);
99 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
100 c->val = perf_config_bool(var, value) ? 2 : 0;
105 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
106 * and -1 for "not specified"
108 static int check_browser_config(const char *cmd)
110 struct pager_config c;
113 perf_config(browser_command_config, &c);
117 static void commit_pager_choice(void)
121 setenv("PERF_PAGER", "cat", 1);
131 struct option options[] = {
132 OPT_ARGUMENT("help", "help"),
133 OPT_ARGUMENT("version", "version"),
134 OPT_ARGUMENT("exec-path", "exec-path"),
135 OPT_ARGUMENT("html-path", "html-path"),
136 OPT_ARGUMENT("paginate", "paginate"),
137 OPT_ARGUMENT("no-pager", "no-pager"),
138 OPT_ARGUMENT("perf-dir", "perf-dir"),
139 OPT_ARGUMENT("work-tree", "work-tree"),
140 OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
141 OPT_ARGUMENT("buildid-dir", "buildid-dir"),
142 OPT_ARGUMENT("list-cmds", "list-cmds"),
143 OPT_ARGUMENT("list-opts", "list-opts"),
144 OPT_ARGUMENT("debug", "debug"),
148 static int handle_options(const char ***argv, int *argc, int *envchanged)
153 const char *cmd = (*argv)[0];
158 * For legacy reasons, the "version" and "help"
159 * commands can be written with "--" prepended
160 * to make them look like flags.
162 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
166 * Shortcut for '-h' and '-v' options to invoke help
167 * and version command.
169 if (!strcmp(cmd, "-h")) {
170 (*argv)[0] = "--help";
174 if (!strcmp(cmd, "-v")) {
175 (*argv)[0] = "--version";
180 * Check remaining flags.
182 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
183 cmd += strlen(CMD_EXEC_PATH);
185 perf_set_argv_exec_path(cmd + 1);
187 puts(perf_exec_path());
190 } else if (!strcmp(cmd, "--html-path")) {
191 puts(system_path(PERF_HTML_PATH));
193 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
195 } else if (!strcmp(cmd, "--no-pager")) {
199 } else if (!strcmp(cmd, "--perf-dir")) {
201 fprintf(stderr, "No directory given for --perf-dir.\n");
202 usage(perf_usage_string);
204 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
210 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
211 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
214 } else if (!strcmp(cmd, "--work-tree")) {
216 fprintf(stderr, "No directory given for --work-tree.\n");
217 usage(perf_usage_string);
219 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
224 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
225 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
228 } else if (!strcmp(cmd, "--debugfs-dir")) {
230 fprintf(stderr, "No directory given for --debugfs-dir.\n");
231 usage(perf_usage_string);
233 tracing_path_set((*argv)[1]);
238 } else if (!strcmp(cmd, "--buildid-dir")) {
240 fprintf(stderr, "No directory given for --buildid-dir.\n");
241 usage(perf_usage_string);
243 set_buildid_dir((*argv)[1]);
248 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
249 tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
250 fprintf(stderr, "dir: %s\n", tracing_path);
253 } else if (!strcmp(cmd, "--list-cmds")) {
256 for (i = 0; i < ARRAY_SIZE(commands); i++) {
257 struct cmd_struct *p = commands+i;
258 printf("%s ", p->cmd);
262 } else if (!strcmp(cmd, "--list-opts")) {
265 for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
266 struct option *p = options+i;
267 printf("--%s ", p->long_name);
271 } else if (!strcmp(cmd, "--debug")) {
273 fprintf(stderr, "No variable specified for --debug.\n");
274 usage(perf_usage_string);
276 if (perf_debug_option((*argv)[1]))
277 usage(perf_usage_string);
282 fprintf(stderr, "Unknown option: %s\n", cmd);
283 usage(perf_usage_string);
293 static int handle_alias(int *argcp, const char ***argv)
295 int envchanged = 0, ret = 0, saved_errno = errno;
296 int count, option_count;
297 const char **new_argv;
298 const char *alias_command;
301 alias_command = (*argv)[0];
302 alias_string = alias_lookup(alias_command);
304 if (alias_string[0] == '!') {
308 strbuf_init(&buf, PATH_MAX);
309 strbuf_addstr(&buf, alias_string);
310 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
312 alias_string = buf.buf;
314 ret = system(alias_string + 1);
315 if (ret >= 0 && WIFEXITED(ret) &&
316 WEXITSTATUS(ret) != 127)
317 exit(WEXITSTATUS(ret));
318 die("Failed to run '%s' when expanding alias '%s'",
319 alias_string + 1, alias_command);
321 count = split_cmdline(alias_string, &new_argv);
323 die("Bad alias.%s string", alias_command);
324 option_count = handle_options(&new_argv, &count, &envchanged);
326 die("alias '%s' changes environment variables\n"
327 "You can use '!perf' in the alias to do this.",
329 memmove(new_argv - option_count, new_argv,
330 count * sizeof(char *));
331 new_argv -= option_count;
334 die("empty alias for %s", alias_command);
336 if (!strcmp(alias_command, new_argv[0]))
337 die("recursive alias: %s", alias_command);
339 new_argv = realloc(new_argv, sizeof(char *) *
340 (count + *argcp + 1));
341 /* insert after command name */
342 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
343 new_argv[count + *argcp] = NULL;
356 const char perf_version_string[] = PERF_VERSION;
358 #define RUN_SETUP (1<<0)
359 #define USE_PAGER (1<<1)
361 * require working tree to be present -- anything uses this needs
362 * RUN_SETUP for reading from the configuration file.
364 #define NEED_WORK_TREE (1<<2)
366 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
371 char sbuf[STRERR_BUFSIZE];
374 if (p->option & RUN_SETUP)
375 prefix = NULL; /* setup_perf_directory(); */
377 if (use_browser == -1)
378 use_browser = check_browser_config(p->cmd);
380 if (use_pager == -1 && p->option & RUN_SETUP)
381 use_pager = check_pager_config(p->cmd);
382 if (use_pager == -1 && p->option & USE_PAGER)
384 commit_pager_choice();
386 status = p->fn(argc, argv, prefix);
387 exit_browser(status);
388 perf_env__exit(&perf_env);
392 return status & 0xff;
394 /* Somebody closed stdout? */
395 if (fstat(fileno(stdout), &st))
397 /* Ignore write errors for pipes and sockets.. */
398 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
402 /* Check for ENOSPC and EIO errors.. */
403 if (fflush(stdout)) {
404 fprintf(stderr, "write failure on standard output: %s",
405 strerror_r(errno, sbuf, sizeof(sbuf)));
408 if (ferror(stdout)) {
409 fprintf(stderr, "unknown write failure on standard output");
412 if (fclose(stdout)) {
413 fprintf(stderr, "close failed on standard output: %s",
414 strerror_r(errno, sbuf, sizeof(sbuf)));
422 static void handle_internal_command(int argc, const char **argv)
424 const char *cmd = argv[0];
426 static const char ext[] = STRIP_EXTENSION;
428 if (sizeof(ext) > 1) {
429 i = strlen(argv[0]) - strlen(ext);
430 if (i > 0 && !strcmp(argv[0] + i, ext)) {
431 char *argv0 = strdup(argv[0]);
432 argv[0] = cmd = argv0;
437 /* Turn "perf cmd --help" into "perf help cmd" */
438 if (argc > 1 && !strcmp(argv[1], "--help")) {
440 argv[0] = cmd = "help";
443 for (i = 0; i < ARRAY_SIZE(commands); i++) {
444 struct cmd_struct *p = commands+i;
445 if (strcmp(p->cmd, cmd))
447 exit(run_builtin(p, argc, argv));
451 static void execv_dashed_external(const char **argv)
453 struct strbuf cmd = STRBUF_INIT;
457 strbuf_addf(&cmd, "perf-%s", argv[0]);
460 * argv[0] must be the perf command, but the argv array
461 * belongs to the caller, and may be reused in
462 * subsequent loop iterations. Save argv[0] and
463 * restore it on error.
469 * if we fail because the command is not found, it is
470 * OK to return. Otherwise, we just pass along the status code.
472 status = run_command_v_opt(argv, 0);
473 if (status != -ERR_RUN_COMMAND_EXEC) {
474 if (IS_RUN_COMMAND_ERR(status))
475 die("unable to run '%s'", argv[0]);
478 errno = ENOENT; /* as if we called execvp */
482 strbuf_release(&cmd);
485 static int run_argv(int *argcp, const char ***argv)
490 /* See if it's an internal command */
491 handle_internal_command(*argcp, *argv);
493 /* .. then try the external ones */
494 execv_dashed_external(*argv);
496 /* It could be an alias -- this works around the insanity
497 * of overriding "perf log" with "perf show" by having
500 if (done_alias || !handle_alias(argcp, argv))
508 static void pthread__block_sigwinch(void)
513 sigaddset(&set, SIGWINCH);
514 pthread_sigmask(SIG_BLOCK, &set, NULL);
517 void pthread__unblock_sigwinch(void)
522 sigaddset(&set, SIGWINCH);
523 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
526 int main(int argc, const char **argv)
529 char sbuf[STRERR_BUFSIZE];
531 /* The page_size is placed in util object. */
532 page_size = sysconf(_SC_PAGE_SIZE);
533 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
535 cmd = perf_extract_argv0_path(argv[0]);
539 /* get debugfs/tracefs mount point from /proc/mounts */
540 tracing_path_mount();
543 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
545 * - cannot take flags in between the "perf" and the "xxxx".
546 * - cannot execute it externally (since it would just do
547 * the same thing over again)
549 * So we just directly call the internal command handler, and
550 * die if that one cannot handle it.
552 if (!prefixcmp(cmd, "perf-")) {
555 handle_internal_command(argc, argv);
556 fprintf(stderr, "cannot handle %s internally", cmd);
559 if (!prefixcmp(cmd, "trace")) {
560 #ifdef HAVE_LIBAUDIT_SUPPORT
561 set_buildid_dir(NULL);
564 return cmd_trace(argc, argv, NULL);
567 "trace command not available: missing audit-libs devel package at build time.\n");
571 /* Look for flags.. */
574 handle_options(&argv, &argc, NULL);
575 commit_pager_choice();
576 set_buildid_dir(NULL);
579 if (!prefixcmp(argv[0], "--"))
582 /* The user didn't specify a command; give them help */
583 printf("\n usage: %s\n\n", perf_usage_string);
584 list_common_cmds_help();
585 printf("\n %s\n\n", perf_more_info_string);
593 * We use PATH to find perf commands, but we prepend some higher
594 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
595 * environment, and the $(perfexecdir) from the Makefile at build
600 * Block SIGWINCH notifications so that the thread that wants it can
601 * unblock and get syscalls like select interrupted instead of waiting
602 * forever while the signal goes to some other non interested thread.
604 pthread__block_sigwinch();
607 static int done_help;
608 int was_alias = run_argv(&argc, &argv);
614 fprintf(stderr, "Expansion of alias '%s' failed; "
615 "'%s' is not a perf-command\n",
620 cmd = argv[0] = help_unknown_cmd(cmd);
626 fprintf(stderr, "Failed to run command '%s': %s\n",
627 cmd, strerror_r(errno, sbuf, sizeof(sbuf)));