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 <subcmd/exec-cmd.h>
13 #include "util/cache.h"
14 #include "util/quote.h"
15 #include <subcmd/run-command.h>
16 #include "util/parse-events.h"
17 #include <subcmd/parse-options.h>
18 #include "util/bpf-loader.h"
19 #include "util/debug.h"
20 #include <api/fs/tracing_path.h>
25 const char perf_usage_string
[] =
26 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
28 const char perf_more_info_string
[] =
29 "See 'perf help COMMAND' for more information on a specific command.";
32 static int use_pager
= -1;
33 const char *input_name
;
37 int (*fn
)(int, const char **, const char *);
41 static struct cmd_struct commands
[] = {
42 { "buildid-cache", cmd_buildid_cache
, 0 },
43 { "buildid-list", cmd_buildid_list
, 0 },
44 { "config", cmd_config
, 0 },
45 { "diff", cmd_diff
, 0 },
46 { "evlist", cmd_evlist
, 0 },
47 { "help", cmd_help
, 0 },
48 { "list", cmd_list
, 0 },
49 { "record", cmd_record
, 0 },
50 { "report", cmd_report
, 0 },
51 { "bench", cmd_bench
, 0 },
52 { "stat", cmd_stat
, 0 },
53 { "timechart", cmd_timechart
, 0 },
54 { "top", cmd_top
, 0 },
55 { "annotate", cmd_annotate
, 0 },
56 { "version", cmd_version
, 0 },
57 { "script", cmd_script
, 0 },
58 { "sched", cmd_sched
, 0 },
59 #ifdef HAVE_LIBELF_SUPPORT
60 { "probe", cmd_probe
, 0 },
62 { "kmem", cmd_kmem
, 0 },
63 { "lock", cmd_lock
, 0 },
64 { "kvm", cmd_kvm
, 0 },
65 { "test", cmd_test
, 0 },
66 #ifdef HAVE_LIBAUDIT_SUPPORT
67 { "trace", cmd_trace
, 0 },
69 { "inject", cmd_inject
, 0 },
70 { "mem", cmd_mem
, 0 },
71 { "data", cmd_data
, 0 },
79 static int pager_command_config(const char *var
, const char *value
, void *data
)
81 struct pager_config
*c
= data
;
82 if (!prefixcmp(var
, "pager.") && !strcmp(var
+ 6, c
->cmd
))
83 c
->val
= perf_config_bool(var
, value
);
87 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
88 int check_pager_config(const char *cmd
)
90 struct pager_config c
;
93 perf_config(pager_command_config
, &c
);
97 static int browser_command_config(const char *var
, const char *value
, void *data
)
99 struct pager_config
*c
= data
;
100 if (!prefixcmp(var
, "tui.") && !strcmp(var
+ 4, c
->cmd
))
101 c
->val
= perf_config_bool(var
, value
);
102 if (!prefixcmp(var
, "gtk.") && !strcmp(var
+ 4, c
->cmd
))
103 c
->val
= perf_config_bool(var
, value
) ? 2 : 0;
108 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
109 * and -1 for "not specified"
111 static int check_browser_config(const char *cmd
)
113 struct pager_config c
;
116 perf_config(browser_command_config
, &c
);
120 static void commit_pager_choice(void)
124 setenv(PERF_PAGER_ENVIRONMENT
, "cat", 1);
134 struct option options
[] = {
135 OPT_ARGUMENT("help", "help"),
136 OPT_ARGUMENT("version", "version"),
137 OPT_ARGUMENT("exec-path", "exec-path"),
138 OPT_ARGUMENT("html-path", "html-path"),
139 OPT_ARGUMENT("paginate", "paginate"),
140 OPT_ARGUMENT("no-pager", "no-pager"),
141 OPT_ARGUMENT("perf-dir", "perf-dir"),
142 OPT_ARGUMENT("work-tree", "work-tree"),
143 OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
144 OPT_ARGUMENT("buildid-dir", "buildid-dir"),
145 OPT_ARGUMENT("list-cmds", "list-cmds"),
146 OPT_ARGUMENT("list-opts", "list-opts"),
147 OPT_ARGUMENT("debug", "debug"),
151 static int handle_options(const char ***argv
, int *argc
, int *envchanged
)
156 const char *cmd
= (*argv
)[0];
161 * For legacy reasons, the "version" and "help"
162 * commands can be written with "--" prepended
163 * to make them look like flags.
165 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
169 * Shortcut for '-h' and '-v' options to invoke help
170 * and version command.
172 if (!strcmp(cmd
, "-h")) {
173 (*argv
)[0] = "--help";
177 if (!strcmp(cmd
, "-v")) {
178 (*argv
)[0] = "--version";
183 * Check remaining flags.
185 if (!prefixcmp(cmd
, CMD_EXEC_PATH
)) {
186 cmd
+= strlen(CMD_EXEC_PATH
);
188 set_argv_exec_path(cmd
+ 1);
190 puts(get_argv_exec_path());
193 } else if (!strcmp(cmd
, "--html-path")) {
194 puts(system_path(PERF_HTML_PATH
));
196 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
198 } else if (!strcmp(cmd
, "--no-pager")) {
202 } else if (!strcmp(cmd
, "--perf-dir")) {
204 fprintf(stderr
, "No directory given for --perf-dir.\n");
205 usage(perf_usage_string
);
207 setenv(PERF_DIR_ENVIRONMENT
, (*argv
)[1], 1);
213 } else if (!prefixcmp(cmd
, CMD_PERF_DIR
)) {
214 setenv(PERF_DIR_ENVIRONMENT
, cmd
+ strlen(CMD_PERF_DIR
), 1);
217 } else if (!strcmp(cmd
, "--work-tree")) {
219 fprintf(stderr
, "No directory given for --work-tree.\n");
220 usage(perf_usage_string
);
222 setenv(PERF_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
227 } else if (!prefixcmp(cmd
, CMD_WORK_TREE
)) {
228 setenv(PERF_WORK_TREE_ENVIRONMENT
, cmd
+ strlen(CMD_WORK_TREE
), 1);
231 } else if (!strcmp(cmd
, "--debugfs-dir")) {
233 fprintf(stderr
, "No directory given for --debugfs-dir.\n");
234 usage(perf_usage_string
);
236 tracing_path_set((*argv
)[1]);
241 } else if (!strcmp(cmd
, "--buildid-dir")) {
243 fprintf(stderr
, "No directory given for --buildid-dir.\n");
244 usage(perf_usage_string
);
246 set_buildid_dir((*argv
)[1]);
251 } else if (!prefixcmp(cmd
, CMD_DEBUGFS_DIR
)) {
252 tracing_path_set(cmd
+ strlen(CMD_DEBUGFS_DIR
));
253 fprintf(stderr
, "dir: %s\n", tracing_path
);
256 } else if (!strcmp(cmd
, "--list-cmds")) {
259 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
260 struct cmd_struct
*p
= commands
+i
;
261 printf("%s ", p
->cmd
);
265 } else if (!strcmp(cmd
, "--list-opts")) {
268 for (i
= 0; i
< ARRAY_SIZE(options
)-1; i
++) {
269 struct option
*p
= options
+i
;
270 printf("--%s ", p
->long_name
);
274 } else if (!strcmp(cmd
, "--debug")) {
276 fprintf(stderr
, "No variable specified for --debug.\n");
277 usage(perf_usage_string
);
279 if (perf_debug_option((*argv
)[1]))
280 usage(perf_usage_string
);
285 fprintf(stderr
, "Unknown option: %s\n", cmd
);
286 usage(perf_usage_string
);
296 static int handle_alias(int *argcp
, const char ***argv
)
298 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
299 int count
, option_count
;
300 const char **new_argv
;
301 const char *alias_command
;
304 alias_command
= (*argv
)[0];
305 alias_string
= alias_lookup(alias_command
);
307 if (alias_string
[0] == '!') {
311 strbuf_init(&buf
, PATH_MAX
);
312 strbuf_addstr(&buf
, alias_string
);
313 sq_quote_argv(&buf
, (*argv
) + 1, PATH_MAX
);
315 alias_string
= buf
.buf
;
317 ret
= system(alias_string
+ 1);
318 if (ret
>= 0 && WIFEXITED(ret
) &&
319 WEXITSTATUS(ret
) != 127)
320 exit(WEXITSTATUS(ret
));
321 die("Failed to run '%s' when expanding alias '%s'",
322 alias_string
+ 1, alias_command
);
324 count
= split_cmdline(alias_string
, &new_argv
);
326 die("Bad alias.%s string", alias_command
);
327 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
329 die("alias '%s' changes environment variables\n"
330 "You can use '!perf' in the alias to do this.",
332 memmove(new_argv
- option_count
, new_argv
,
333 count
* sizeof(char *));
334 new_argv
-= option_count
;
337 die("empty alias for %s", alias_command
);
339 if (!strcmp(alias_command
, new_argv
[0]))
340 die("recursive alias: %s", alias_command
);
342 new_argv
= realloc(new_argv
, sizeof(char *) *
343 (count
+ *argcp
+ 1));
344 /* insert after command name */
345 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char *) * *argcp
);
346 new_argv
[count
+ *argcp
] = NULL
;
359 const char perf_version_string
[] = PERF_VERSION
;
361 #define RUN_SETUP (1<<0)
362 #define USE_PAGER (1<<1)
364 * require working tree to be present -- anything uses this needs
365 * RUN_SETUP for reading from the configuration file.
367 #define NEED_WORK_TREE (1<<2)
369 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
374 char sbuf
[STRERR_BUFSIZE
];
377 if (p
->option
& RUN_SETUP
)
378 prefix
= NULL
; /* setup_perf_directory(); */
380 if (use_browser
== -1)
381 use_browser
= check_browser_config(p
->cmd
);
383 if (use_pager
== -1 && p
->option
& RUN_SETUP
)
384 use_pager
= check_pager_config(p
->cmd
);
385 if (use_pager
== -1 && p
->option
& USE_PAGER
)
387 commit_pager_choice();
389 perf_env__set_cmdline(&perf_env
, argc
, argv
);
390 status
= p
->fn(argc
, argv
, prefix
);
391 exit_browser(status
);
392 perf_env__exit(&perf_env
);
396 return status
& 0xff;
398 /* Somebody closed stdout? */
399 if (fstat(fileno(stdout
), &st
))
401 /* Ignore write errors for pipes and sockets.. */
402 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
406 /* Check for ENOSPC and EIO errors.. */
407 if (fflush(stdout
)) {
408 fprintf(stderr
, "write failure on standard output: %s",
409 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
412 if (ferror(stdout
)) {
413 fprintf(stderr
, "unknown write failure on standard output");
416 if (fclose(stdout
)) {
417 fprintf(stderr
, "close failed on standard output: %s",
418 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
426 static void handle_internal_command(int argc
, const char **argv
)
428 const char *cmd
= argv
[0];
430 static const char ext
[] = STRIP_EXTENSION
;
432 if (sizeof(ext
) > 1) {
433 i
= strlen(argv
[0]) - strlen(ext
);
434 if (i
> 0 && !strcmp(argv
[0] + i
, ext
)) {
435 char *argv0
= strdup(argv
[0]);
436 argv
[0] = cmd
= argv0
;
441 /* Turn "perf cmd --help" into "perf help cmd" */
442 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
444 argv
[0] = cmd
= "help";
447 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
448 struct cmd_struct
*p
= commands
+i
;
449 if (strcmp(p
->cmd
, cmd
))
451 exit(run_builtin(p
, argc
, argv
));
455 static void execv_dashed_external(const char **argv
)
457 struct strbuf cmd
= STRBUF_INIT
;
461 strbuf_addf(&cmd
, "perf-%s", argv
[0]);
464 * argv[0] must be the perf command, but the argv array
465 * belongs to the caller, and may be reused in
466 * subsequent loop iterations. Save argv[0] and
467 * restore it on error.
473 * if we fail because the command is not found, it is
474 * OK to return. Otherwise, we just pass along the status code.
476 status
= run_command_v_opt(argv
, 0);
477 if (status
!= -ERR_RUN_COMMAND_EXEC
) {
478 if (IS_RUN_COMMAND_ERR(status
))
479 die("unable to run '%s'", argv
[0]);
482 errno
= ENOENT
; /* as if we called execvp */
486 strbuf_release(&cmd
);
489 static int run_argv(int *argcp
, const char ***argv
)
494 /* See if it's an internal command */
495 handle_internal_command(*argcp
, *argv
);
497 /* .. then try the external ones */
498 execv_dashed_external(*argv
);
500 /* It could be an alias -- this works around the insanity
501 * of overriding "perf log" with "perf show" by having
504 if (done_alias
|| !handle_alias(argcp
, argv
))
512 static void pthread__block_sigwinch(void)
517 sigaddset(&set
, SIGWINCH
);
518 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
521 void pthread__unblock_sigwinch(void)
526 sigaddset(&set
, SIGWINCH
);
527 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
530 int main(int argc
, const char **argv
)
533 char sbuf
[STRERR_BUFSIZE
];
536 exec_cmd_init("perf", PREFIX
, PERF_EXEC_PATH
, EXEC_PATH_ENVIRONMENT
);
537 pager_init(PERF_PAGER_ENVIRONMENT
);
539 /* The page_size is placed in util object. */
540 page_size
= sysconf(_SC_PAGE_SIZE
);
541 cacheline_size
= sysconf(_SC_LEVEL1_DCACHE_LINESIZE
);
543 cmd
= extract_argv0_path(argv
[0]);
549 /* get debugfs/tracefs mount point from /proc/mounts */
550 tracing_path_mount();
553 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
555 * - cannot take flags in between the "perf" and the "xxxx".
556 * - cannot execute it externally (since it would just do
557 * the same thing over again)
559 * So we just directly call the internal command handler, and
560 * die if that one cannot handle it.
562 if (!prefixcmp(cmd
, "perf-")) {
565 handle_internal_command(argc
, argv
);
566 fprintf(stderr
, "cannot handle %s internally", cmd
);
569 if (!prefixcmp(cmd
, "trace")) {
570 #ifdef HAVE_LIBAUDIT_SUPPORT
571 set_buildid_dir(NULL
);
574 return cmd_trace(argc
, argv
, NULL
);
577 "trace command not available: missing audit-libs devel package at build time.\n");
581 /* Look for flags.. */
584 handle_options(&argv
, &argc
, NULL
);
585 commit_pager_choice();
586 set_buildid_dir(NULL
);
589 if (!prefixcmp(argv
[0], "--"))
592 /* The user didn't specify a command; give them help */
593 printf("\n usage: %s\n\n", perf_usage_string
);
594 list_common_cmds_help();
595 printf("\n %s\n\n", perf_more_info_string
);
603 * We use PATH to find perf commands, but we prepend some higher
604 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
605 * environment, and the $(perfexecdir) from the Makefile at build
610 * Block SIGWINCH notifications so that the thread that wants it can
611 * unblock and get syscalls like select interrupted instead of waiting
612 * forever while the signal goes to some other non interested thread.
614 pthread__block_sigwinch();
617 static int done_help
;
618 int was_alias
= run_argv(&argc
, &argv
);
624 fprintf(stderr
, "Expansion of alias '%s' failed; "
625 "'%s' is not a perf-command\n",
630 cmd
= argv
[0] = help_unknown_cmd(cmd
);
636 fprintf(stderr
, "Failed to run command '%s': %s\n",
637 cmd
, strerror_r(errno
, sbuf
, sizeof(sbuf
)));