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.
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/parse-options.h"
17 #include "util/debug.h"
18 #include <api/fs/debugfs.h>
21 const char perf_usage_string
[] =
22 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
24 const char perf_more_info_string
[] =
25 "See 'perf help COMMAND' for more information on a specific command.";
28 static int use_pager
= -1;
29 const char *input_name
;
33 int (*fn
)(int, const char **, const char *);
37 static struct cmd_struct commands
[] = {
38 { "buildid-cache", cmd_buildid_cache
, 0 },
39 { "buildid-list", cmd_buildid_list
, 0 },
40 { "diff", cmd_diff
, 0 },
41 { "evlist", cmd_evlist
, 0 },
42 { "help", cmd_help
, 0 },
43 { "list", cmd_list
, 0 },
44 { "record", cmd_record
, 0 },
45 { "report", cmd_report
, 0 },
46 { "bench", cmd_bench
, 0 },
47 { "stat", cmd_stat
, 0 },
48 { "timechart", cmd_timechart
, 0 },
49 { "top", cmd_top
, 0 },
50 { "annotate", cmd_annotate
, 0 },
51 { "version", cmd_version
, 0 },
52 { "script", cmd_script
, 0 },
53 { "sched", cmd_sched
, 0 },
54 #ifdef HAVE_LIBELF_SUPPORT
55 { "probe", cmd_probe
, 0 },
57 { "kmem", cmd_kmem
, 0 },
58 { "lock", cmd_lock
, 0 },
59 { "kvm", cmd_kvm
, 0 },
60 { "test", cmd_test
, 0 },
61 #ifdef HAVE_LIBAUDIT_SUPPORT
62 { "trace", cmd_trace
, 0 },
64 { "inject", cmd_inject
, 0 },
65 { "mem", cmd_mem
, 0 },
66 { "data", cmd_data
, 0 },
74 static int pager_command_config(const char *var
, const char *value
, void *data
)
76 struct pager_config
*c
= data
;
77 if (!prefixcmp(var
, "pager.") && !strcmp(var
+ 6, c
->cmd
))
78 c
->val
= perf_config_bool(var
, value
);
82 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
83 int check_pager_config(const char *cmd
)
85 struct pager_config c
;
88 perf_config(pager_command_config
, &c
);
92 static int browser_command_config(const char *var
, const char *value
, void *data
)
94 struct pager_config
*c
= data
;
95 if (!prefixcmp(var
, "tui.") && !strcmp(var
+ 4, c
->cmd
))
96 c
->val
= perf_config_bool(var
, value
);
97 if (!prefixcmp(var
, "gtk.") && !strcmp(var
+ 4, c
->cmd
))
98 c
->val
= perf_config_bool(var
, value
) ? 2 : 0;
103 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
104 * and -1 for "not specified"
106 static int check_browser_config(const char *cmd
)
108 struct pager_config c
;
111 perf_config(browser_command_config
, &c
);
115 static void commit_pager_choice(void)
119 setenv("PERF_PAGER", "cat", 1);
129 struct option options
[] = {
130 OPT_ARGUMENT("help", "help"),
131 OPT_ARGUMENT("version", "version"),
132 OPT_ARGUMENT("exec-path", "exec-path"),
133 OPT_ARGUMENT("html-path", "html-path"),
134 OPT_ARGUMENT("paginate", "paginate"),
135 OPT_ARGUMENT("no-pager", "no-pager"),
136 OPT_ARGUMENT("perf-dir", "perf-dir"),
137 OPT_ARGUMENT("work-tree", "work-tree"),
138 OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
139 OPT_ARGUMENT("buildid-dir", "buildid-dir"),
140 OPT_ARGUMENT("list-cmds", "list-cmds"),
141 OPT_ARGUMENT("list-opts", "list-opts"),
142 OPT_ARGUMENT("debug", "debug"),
146 static int handle_options(const char ***argv
, int *argc
, int *envchanged
)
151 const char *cmd
= (*argv
)[0];
156 * For legacy reasons, the "version" and "help"
157 * commands can be written with "--" prepended
158 * to make them look like flags.
160 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
164 * Check remaining flags.
166 if (!prefixcmp(cmd
, CMD_EXEC_PATH
)) {
167 cmd
+= strlen(CMD_EXEC_PATH
);
169 perf_set_argv_exec_path(cmd
+ 1);
171 puts(perf_exec_path());
174 } else if (!strcmp(cmd
, "--html-path")) {
175 puts(system_path(PERF_HTML_PATH
));
177 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
179 } else if (!strcmp(cmd
, "--no-pager")) {
183 } else if (!strcmp(cmd
, "--perf-dir")) {
185 fprintf(stderr
, "No directory given for --perf-dir.\n");
186 usage(perf_usage_string
);
188 setenv(PERF_DIR_ENVIRONMENT
, (*argv
)[1], 1);
194 } else if (!prefixcmp(cmd
, CMD_PERF_DIR
)) {
195 setenv(PERF_DIR_ENVIRONMENT
, cmd
+ strlen(CMD_PERF_DIR
), 1);
198 } else if (!strcmp(cmd
, "--work-tree")) {
200 fprintf(stderr
, "No directory given for --work-tree.\n");
201 usage(perf_usage_string
);
203 setenv(PERF_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
208 } else if (!prefixcmp(cmd
, CMD_WORK_TREE
)) {
209 setenv(PERF_WORK_TREE_ENVIRONMENT
, cmd
+ strlen(CMD_WORK_TREE
), 1);
212 } else if (!strcmp(cmd
, "--debugfs-dir")) {
214 fprintf(stderr
, "No directory given for --debugfs-dir.\n");
215 usage(perf_usage_string
);
217 perf_debugfs_set_path((*argv
)[1]);
222 } else if (!strcmp(cmd
, "--buildid-dir")) {
224 fprintf(stderr
, "No directory given for --buildid-dir.\n");
225 usage(perf_usage_string
);
227 set_buildid_dir((*argv
)[1]);
232 } else if (!prefixcmp(cmd
, CMD_DEBUGFS_DIR
)) {
233 perf_debugfs_set_path(cmd
+ strlen(CMD_DEBUGFS_DIR
));
234 fprintf(stderr
, "dir: %s\n", debugfs_mountpoint
);
237 } else if (!strcmp(cmd
, "--list-cmds")) {
240 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
241 struct cmd_struct
*p
= commands
+i
;
242 printf("%s ", p
->cmd
);
246 } else if (!strcmp(cmd
, "--list-opts")) {
249 for (i
= 0; i
< ARRAY_SIZE(options
)-1; i
++) {
250 struct option
*p
= options
+i
;
251 printf("--%s ", p
->long_name
);
255 } else if (!strcmp(cmd
, "--debug")) {
257 fprintf(stderr
, "No variable specified for --debug.\n");
258 usage(perf_usage_string
);
260 if (perf_debug_option((*argv
)[1]))
261 usage(perf_usage_string
);
266 fprintf(stderr
, "Unknown option: %s\n", cmd
);
267 usage(perf_usage_string
);
277 static int handle_alias(int *argcp
, const char ***argv
)
279 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
280 int count
, option_count
;
281 const char **new_argv
;
282 const char *alias_command
;
285 alias_command
= (*argv
)[0];
286 alias_string
= alias_lookup(alias_command
);
288 if (alias_string
[0] == '!') {
292 strbuf_init(&buf
, PATH_MAX
);
293 strbuf_addstr(&buf
, alias_string
);
294 sq_quote_argv(&buf
, (*argv
) + 1, PATH_MAX
);
296 alias_string
= buf
.buf
;
298 ret
= system(alias_string
+ 1);
299 if (ret
>= 0 && WIFEXITED(ret
) &&
300 WEXITSTATUS(ret
) != 127)
301 exit(WEXITSTATUS(ret
));
302 die("Failed to run '%s' when expanding alias '%s'",
303 alias_string
+ 1, alias_command
);
305 count
= split_cmdline(alias_string
, &new_argv
);
307 die("Bad alias.%s string", alias_command
);
308 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
310 die("alias '%s' changes environment variables\n"
311 "You can use '!perf' in the alias to do this.",
313 memmove(new_argv
- option_count
, new_argv
,
314 count
* sizeof(char *));
315 new_argv
-= option_count
;
318 die("empty alias for %s", alias_command
);
320 if (!strcmp(alias_command
, new_argv
[0]))
321 die("recursive alias: %s", alias_command
);
323 new_argv
= realloc(new_argv
, sizeof(char *) *
324 (count
+ *argcp
+ 1));
325 /* insert after command name */
326 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char *) * *argcp
);
327 new_argv
[count
+ *argcp
] = NULL
;
340 const char perf_version_string
[] = PERF_VERSION
;
342 #define RUN_SETUP (1<<0)
343 #define USE_PAGER (1<<1)
345 * require working tree to be present -- anything uses this needs
346 * RUN_SETUP for reading from the configuration file.
348 #define NEED_WORK_TREE (1<<2)
350 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
355 char sbuf
[STRERR_BUFSIZE
];
358 if (p
->option
& RUN_SETUP
)
359 prefix
= NULL
; /* setup_perf_directory(); */
361 if (use_browser
== -1)
362 use_browser
= check_browser_config(p
->cmd
);
364 if (use_pager
== -1 && p
->option
& RUN_SETUP
)
365 use_pager
= check_pager_config(p
->cmd
);
366 if (use_pager
== -1 && p
->option
& USE_PAGER
)
368 commit_pager_choice();
370 status
= p
->fn(argc
, argv
, prefix
);
371 exit_browser(status
);
374 return status
& 0xff;
376 /* Somebody closed stdout? */
377 if (fstat(fileno(stdout
), &st
))
379 /* Ignore write errors for pipes and sockets.. */
380 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
384 /* Check for ENOSPC and EIO errors.. */
385 if (fflush(stdout
)) {
386 fprintf(stderr
, "write failure on standard output: %s",
387 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
390 if (ferror(stdout
)) {
391 fprintf(stderr
, "unknown write failure on standard output");
394 if (fclose(stdout
)) {
395 fprintf(stderr
, "close failed on standard output: %s",
396 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
404 static void handle_internal_command(int argc
, const char **argv
)
406 const char *cmd
= argv
[0];
408 static const char ext
[] = STRIP_EXTENSION
;
410 if (sizeof(ext
) > 1) {
411 i
= strlen(argv
[0]) - strlen(ext
);
412 if (i
> 0 && !strcmp(argv
[0] + i
, ext
)) {
413 char *argv0
= strdup(argv
[0]);
414 argv
[0] = cmd
= argv0
;
419 /* Turn "perf cmd --help" into "perf help cmd" */
420 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
422 argv
[0] = cmd
= "help";
425 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
426 struct cmd_struct
*p
= commands
+i
;
427 if (strcmp(p
->cmd
, cmd
))
429 exit(run_builtin(p
, argc
, argv
));
433 static void execv_dashed_external(const char **argv
)
435 struct strbuf cmd
= STRBUF_INIT
;
439 strbuf_addf(&cmd
, "perf-%s", argv
[0]);
442 * argv[0] must be the perf command, but the argv array
443 * belongs to the caller, and may be reused in
444 * subsequent loop iterations. Save argv[0] and
445 * restore it on error.
451 * if we fail because the command is not found, it is
452 * OK to return. Otherwise, we just pass along the status code.
454 status
= run_command_v_opt(argv
, 0);
455 if (status
!= -ERR_RUN_COMMAND_EXEC
) {
456 if (IS_RUN_COMMAND_ERR(status
))
457 die("unable to run '%s'", argv
[0]);
460 errno
= ENOENT
; /* as if we called execvp */
464 strbuf_release(&cmd
);
467 static int run_argv(int *argcp
, const char ***argv
)
472 /* See if it's an internal command */
473 handle_internal_command(*argcp
, *argv
);
475 /* .. then try the external ones */
476 execv_dashed_external(*argv
);
478 /* It could be an alias -- this works around the insanity
479 * of overriding "perf log" with "perf show" by having
482 if (done_alias
|| !handle_alias(argcp
, argv
))
490 static void pthread__block_sigwinch(void)
495 sigaddset(&set
, SIGWINCH
);
496 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
499 void pthread__unblock_sigwinch(void)
504 sigaddset(&set
, SIGWINCH
);
505 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
508 int main(int argc
, const char **argv
)
511 char sbuf
[STRERR_BUFSIZE
];
513 /* The page_size is placed in util object. */
514 page_size
= sysconf(_SC_PAGE_SIZE
);
515 cacheline_size
= sysconf(_SC_LEVEL1_DCACHE_LINESIZE
);
517 cmd
= perf_extract_argv0_path(argv
[0]);
520 /* get debugfs mount point from /proc/mounts */
521 perf_debugfs_mount(NULL
);
523 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
525 * - cannot take flags in between the "perf" and the "xxxx".
526 * - cannot execute it externally (since it would just do
527 * the same thing over again)
529 * So we just directly call the internal command handler, and
530 * die if that one cannot handle it.
532 if (!prefixcmp(cmd
, "perf-")) {
535 handle_internal_command(argc
, argv
);
536 fprintf(stderr
, "cannot handle %s internally", cmd
);
539 if (!prefixcmp(cmd
, "trace")) {
540 #ifdef HAVE_LIBAUDIT_SUPPORT
541 set_buildid_dir(NULL
);
544 return cmd_trace(argc
, argv
, NULL
);
547 "trace command not available: missing audit-libs devel package at build time.\n");
551 /* Look for flags.. */
554 handle_options(&argv
, &argc
, NULL
);
555 commit_pager_choice();
556 set_buildid_dir(NULL
);
559 if (!prefixcmp(argv
[0], "--"))
562 /* The user didn't specify a command; give them help */
563 printf("\n usage: %s\n\n", perf_usage_string
);
564 list_common_cmds_help();
565 printf("\n %s\n\n", perf_more_info_string
);
573 * We use PATH to find perf commands, but we prepend some higher
574 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
575 * environment, and the $(perfexecdir) from the Makefile at build
580 * Block SIGWINCH notifications so that the thread that wants it can
581 * unblock and get syscalls like select interrupted instead of waiting
582 * forever while the signal goes to some other non interested thread.
584 pthread__block_sigwinch();
587 static int done_help
;
588 int was_alias
= run_argv(&argc
, &argv
);
594 fprintf(stderr
, "Expansion of alias '%s' failed; "
595 "'%s' is not a perf-command\n",
600 cmd
= argv
[0] = help_unknown_cmd(cmd
);
606 fprintf(stderr
, "Failed to run command '%s': %s\n",
607 cmd
, strerror_r(errno
, sbuf
, sizeof(sbuf
)));