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/config.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/fs.h>
21 #include <api/fs/tracing_path.h>
25 #include <linux/kernel.h>
27 const char perf_usage_string[] =
28 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
30 const char perf_more_info_string[] =
31 "See 'perf help COMMAND' for more information on a specific command.";
33 static int use_pager = -1;
34 const char *input_name;
38 int (*fn)(int, const char **);
42 static struct cmd_struct commands[] = {
43 { "buildid-cache", cmd_buildid_cache, 0 },
44 { "buildid-list", cmd_buildid_list, 0 },
45 { "config", cmd_config, 0 },
46 { "c2c", cmd_c2c, 0 },
47 { "diff", cmd_diff, 0 },
48 { "evlist", cmd_evlist, 0 },
49 { "help", cmd_help, 0 },
50 { "kallsyms", cmd_kallsyms, 0 },
51 { "list", cmd_list, 0 },
52 { "record", cmd_record, 0 },
53 { "report", cmd_report, 0 },
54 { "bench", cmd_bench, 0 },
55 { "stat", cmd_stat, 0 },
56 { "timechart", cmd_timechart, 0 },
57 { "top", cmd_top, 0 },
58 { "annotate", cmd_annotate, 0 },
59 { "version", cmd_version, 0 },
60 { "script", cmd_script, 0 },
61 { "sched", cmd_sched, 0 },
62 #ifdef HAVE_LIBELF_SUPPORT
63 { "probe", cmd_probe, 0 },
65 { "kmem", cmd_kmem, 0 },
66 { "lock", cmd_lock, 0 },
67 { "kvm", cmd_kvm, 0 },
68 { "test", cmd_test, 0 },
69 #ifdef HAVE_LIBAUDIT_SUPPORT
70 { "trace", cmd_trace, 0 },
72 { "inject", cmd_inject, 0 },
73 { "mem", cmd_mem, 0 },
74 { "data", cmd_data, 0 },
75 { "ftrace", cmd_ftrace, 0 },
83 static int pager_command_config(const char *var, const char *value, void *data)
85 struct pager_config *c = data;
86 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
87 c->val = perf_config_bool(var, value);
91 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
92 int check_pager_config(const char *cmd)
95 struct pager_config c;
98 err = perf_config(pager_command_config, &c);
102 static int browser_command_config(const char *var, const char *value, void *data)
104 struct pager_config *c = data;
105 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
106 c->val = perf_config_bool(var, value);
107 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
108 c->val = perf_config_bool(var, value) ? 2 : 0;
113 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
114 * and -1 for "not specified"
116 static int check_browser_config(const char *cmd)
119 struct pager_config c;
122 err = perf_config(browser_command_config, &c);
126 static void commit_pager_choice(void)
130 setenv(PERF_PAGER_ENVIRONMENT, "cat", 1);
140 struct option options[] = {
141 OPT_ARGUMENT("help", "help"),
142 OPT_ARGUMENT("version", "version"),
143 OPT_ARGUMENT("exec-path", "exec-path"),
144 OPT_ARGUMENT("html-path", "html-path"),
145 OPT_ARGUMENT("paginate", "paginate"),
146 OPT_ARGUMENT("no-pager", "no-pager"),
147 OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
148 OPT_ARGUMENT("buildid-dir", "buildid-dir"),
149 OPT_ARGUMENT("list-cmds", "list-cmds"),
150 OPT_ARGUMENT("list-opts", "list-opts"),
151 OPT_ARGUMENT("debug", "debug"),
155 static int handle_options(const char ***argv, int *argc, int *envchanged)
160 const char *cmd = (*argv)[0];
165 * For legacy reasons, the "version" and "help"
166 * commands can be written with "--" prepended
167 * to make them look like flags.
169 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
173 * Shortcut for '-h' and '-v' options to invoke help
174 * and version command.
176 if (!strcmp(cmd, "-h")) {
177 (*argv)[0] = "--help";
181 if (!strcmp(cmd, "-v")) {
182 (*argv)[0] = "--version";
187 * Check remaining flags.
189 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
190 cmd += strlen(CMD_EXEC_PATH);
192 set_argv_exec_path(cmd + 1);
194 puts(get_argv_exec_path());
197 } else if (!strcmp(cmd, "--html-path")) {
198 puts(system_path(PERF_HTML_PATH));
200 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
202 } else if (!strcmp(cmd, "--no-pager")) {
206 } else if (!strcmp(cmd, "--debugfs-dir")) {
208 fprintf(stderr, "No directory given for --debugfs-dir.\n");
209 usage(perf_usage_string);
211 tracing_path_set((*argv)[1]);
216 } else if (!strcmp(cmd, "--buildid-dir")) {
218 fprintf(stderr, "No directory given for --buildid-dir.\n");
219 usage(perf_usage_string);
221 set_buildid_dir((*argv)[1]);
226 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
227 tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
228 fprintf(stderr, "dir: %s\n", tracing_path);
231 } else if (!strcmp(cmd, "--list-cmds")) {
234 for (i = 0; i < ARRAY_SIZE(commands); i++) {
235 struct cmd_struct *p = commands+i;
236 printf("%s ", p->cmd);
240 } else if (!strcmp(cmd, "--list-opts")) {
243 for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
244 struct option *p = options+i;
245 printf("--%s ", p->long_name);
249 } else if (!strcmp(cmd, "--debug")) {
251 fprintf(stderr, "No variable specified for --debug.\n");
252 usage(perf_usage_string);
254 if (perf_debug_option((*argv)[1]))
255 usage(perf_usage_string);
260 fprintf(stderr, "Unknown option: %s\n", cmd);
261 usage(perf_usage_string);
271 #define RUN_SETUP (1<<0)
272 #define USE_PAGER (1<<1)
274 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
278 char sbuf[STRERR_BUFSIZE];
280 if (use_browser == -1)
281 use_browser = check_browser_config(p->cmd);
283 if (use_pager == -1 && p->option & RUN_SETUP)
284 use_pager = check_pager_config(p->cmd);
285 if (use_pager == -1 && p->option & USE_PAGER)
287 commit_pager_choice();
289 perf_env__set_cmdline(&perf_env, argc, argv);
290 status = p->fn(argc, argv);
292 exit_browser(status);
293 perf_env__exit(&perf_env);
297 return status & 0xff;
299 /* Somebody closed stdout? */
300 if (fstat(fileno(stdout), &st))
302 /* Ignore write errors for pipes and sockets.. */
303 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
307 /* Check for ENOSPC and EIO errors.. */
308 if (fflush(stdout)) {
309 fprintf(stderr, "write failure on standard output: %s",
310 str_error_r(errno, sbuf, sizeof(sbuf)));
313 if (ferror(stdout)) {
314 fprintf(stderr, "unknown write failure on standard output");
317 if (fclose(stdout)) {
318 fprintf(stderr, "close failed on standard output: %s",
319 str_error_r(errno, sbuf, sizeof(sbuf)));
327 static void handle_internal_command(int argc, const char **argv)
329 const char *cmd = argv[0];
331 static const char ext[] = STRIP_EXTENSION;
333 if (sizeof(ext) > 1) {
334 i = strlen(argv[0]) - strlen(ext);
335 if (i > 0 && !strcmp(argv[0] + i, ext)) {
336 char *argv0 = strdup(argv[0]);
337 argv[0] = cmd = argv0;
342 /* Turn "perf cmd --help" into "perf help cmd" */
343 if (argc > 1 && !strcmp(argv[1], "--help")) {
345 argv[0] = cmd = "help";
348 for (i = 0; i < ARRAY_SIZE(commands); i++) {
349 struct cmd_struct *p = commands+i;
350 if (strcmp(p->cmd, cmd))
352 exit(run_builtin(p, argc, argv));
356 static void execv_dashed_external(const char **argv)
362 if (asprintf(&cmd, "perf-%s", argv[0]) < 0)
366 * argv[0] must be the perf command, but the argv array
367 * belongs to the caller, and may be reused in
368 * subsequent loop iterations. Save argv[0] and
369 * restore it on error.
375 * if we fail because the command is not found, it is
376 * OK to return. Otherwise, we just pass along the status code.
378 status = run_command_v_opt(argv, 0);
379 if (status != -ERR_RUN_COMMAND_EXEC) {
380 if (IS_RUN_COMMAND_ERR(status)) {
382 pr_err("FATAL: unable to run '%s'", argv[0]);
387 errno = ENOENT; /* as if we called execvp */
393 static int run_argv(int *argcp, const char ***argv)
395 /* See if it's an internal command */
396 handle_internal_command(*argcp, *argv);
398 /* .. then try the external ones */
399 execv_dashed_external(*argv);
403 static void pthread__block_sigwinch(void)
408 sigaddset(&set, SIGWINCH);
409 pthread_sigmask(SIG_BLOCK, &set, NULL);
412 void pthread__unblock_sigwinch(void)
417 sigaddset(&set, SIGWINCH);
418 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
421 #ifdef _SC_LEVEL1_DCACHE_LINESIZE
422 #define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
424 static void cache_line_size(int *cacheline_sizep)
426 if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
427 pr_debug("cannot determine cache line size");
431 int main(int argc, const char **argv)
435 char sbuf[STRERR_BUFSIZE];
439 exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT);
440 pager_init(PERF_PAGER_ENVIRONMENT);
442 /* The page_size is placed in util object. */
443 page_size = sysconf(_SC_PAGE_SIZE);
444 cache_line_size(&cacheline_size);
446 if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
447 sysctl_perf_event_max_stack = value;
449 if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
450 sysctl_perf_event_max_contexts_per_stack = value;
452 cmd = extract_argv0_path(argv[0]);
459 err = perf_config(perf_default_config, NULL);
462 set_buildid_dir(NULL);
464 /* get debugfs/tracefs mount point from /proc/mounts */
465 tracing_path_mount();
468 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
470 * - cannot take flags in between the "perf" and the "xxxx".
471 * - cannot execute it externally (since it would just do
472 * the same thing over again)
474 * So we just directly call the internal command handler, and
475 * die if that one cannot handle it.
477 if (!prefixcmp(cmd, "perf-")) {
480 handle_internal_command(argc, argv);
481 fprintf(stderr, "cannot handle %s internally", cmd);
484 if (!prefixcmp(cmd, "trace")) {
485 #ifdef HAVE_LIBAUDIT_SUPPORT
488 return cmd_trace(argc, argv);
491 "trace command not available: missing audit-libs devel package at build time.\n");
495 /* Look for flags.. */
498 handle_options(&argv, &argc, NULL);
499 commit_pager_choice();
502 if (!prefixcmp(argv[0], "--"))
505 /* The user didn't specify a command; give them help */
506 printf("\n usage: %s\n\n", perf_usage_string);
507 list_common_cmds_help();
508 printf("\n %s\n\n", perf_more_info_string);
516 * We use PATH to find perf commands, but we prepend some higher
517 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
518 * environment, and the $(perfexecdir) from the Makefile at build
523 * Block SIGWINCH notifications so that the thread that wants it can
524 * unblock and get syscalls like select interrupted instead of waiting
525 * forever while the signal goes to some other non interested thread.
527 pthread__block_sigwinch();
532 static int done_help;
534 run_argv(&argc, &argv);
540 cmd = argv[0] = help_unknown_cmd(cmd);
546 fprintf(stderr, "Failed to run command '%s': %s\n",
547 cmd, str_error_r(errno, sbuf, sizeof(sbuf)));