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/debug.h"
17 #include <api/fs/debugfs.h>
20 const char perf_usage_string
[] =
21 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
23 const char perf_more_info_string
[] =
24 "See 'perf help COMMAND' for more information on a specific command.";
27 static int use_pager
= -1;
28 const char *input_name
;
32 int (*fn
)(int, const char **, const char *);
36 static struct cmd_struct commands
[] = {
37 { "buildid-cache", cmd_buildid_cache
, 0 },
38 { "buildid-list", cmd_buildid_list
, 0 },
39 { "diff", cmd_diff
, 0 },
40 { "evlist", cmd_evlist
, 0 },
41 { "help", cmd_help
, 0 },
42 { "list", cmd_list
, 0 },
43 { "record", cmd_record
, 0 },
44 { "report", cmd_report
, 0 },
45 { "bench", cmd_bench
, 0 },
46 { "stat", cmd_stat
, 0 },
47 { "timechart", cmd_timechart
, 0 },
48 { "top", cmd_top
, 0 },
49 { "annotate", cmd_annotate
, 0 },
50 { "version", cmd_version
, 0 },
51 { "script", cmd_script
, 0 },
52 { "sched", cmd_sched
, 0 },
53 #ifdef HAVE_LIBELF_SUPPORT
54 { "probe", cmd_probe
, 0 },
56 { "kmem", cmd_kmem
, 0 },
57 { "lock", cmd_lock
, 0 },
58 { "kvm", cmd_kvm
, 0 },
59 { "test", cmd_test
, 0 },
60 #ifdef HAVE_LIBAUDIT_SUPPORT
61 { "trace", cmd_trace
, 0 },
63 { "inject", cmd_inject
, 0 },
64 { "mem", cmd_mem
, 0 },
72 static int pager_command_config(const char *var
, const char *value
, void *data
)
74 struct pager_config
*c
= data
;
75 if (!prefixcmp(var
, "pager.") && !strcmp(var
+ 6, c
->cmd
))
76 c
->val
= perf_config_bool(var
, value
);
80 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
81 int check_pager_config(const char *cmd
)
83 struct pager_config c
;
86 perf_config(pager_command_config
, &c
);
90 static int browser_command_config(const char *var
, const char *value
, void *data
)
92 struct pager_config
*c
= data
;
93 if (!prefixcmp(var
, "tui.") && !strcmp(var
+ 4, c
->cmd
))
94 c
->val
= perf_config_bool(var
, value
);
95 if (!prefixcmp(var
, "gtk.") && !strcmp(var
+ 4, c
->cmd
))
96 c
->val
= perf_config_bool(var
, value
) ? 2 : 0;
101 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
102 * and -1 for "not specified"
104 static int check_browser_config(const char *cmd
)
106 struct pager_config c
;
109 perf_config(browser_command_config
, &c
);
113 static void commit_pager_choice(void)
117 setenv("PERF_PAGER", "cat", 1);
127 static int handle_options(const char ***argv
, int *argc
, int *envchanged
)
132 const char *cmd
= (*argv
)[0];
137 * For legacy reasons, the "version" and "help"
138 * commands can be written with "--" prepended
139 * to make them look like flags.
141 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
145 * Check remaining flags.
147 if (!prefixcmp(cmd
, CMD_EXEC_PATH
)) {
148 cmd
+= strlen(CMD_EXEC_PATH
);
150 perf_set_argv_exec_path(cmd
+ 1);
152 puts(perf_exec_path());
155 } else if (!strcmp(cmd
, "--html-path")) {
156 puts(system_path(PERF_HTML_PATH
));
158 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
160 } else if (!strcmp(cmd
, "--no-pager")) {
164 } else if (!strcmp(cmd
, "--perf-dir")) {
166 fprintf(stderr
, "No directory given for --perf-dir.\n");
167 usage(perf_usage_string
);
169 setenv(PERF_DIR_ENVIRONMENT
, (*argv
)[1], 1);
175 } else if (!prefixcmp(cmd
, CMD_PERF_DIR
)) {
176 setenv(PERF_DIR_ENVIRONMENT
, cmd
+ strlen(CMD_PERF_DIR
), 1);
179 } else if (!strcmp(cmd
, "--work-tree")) {
181 fprintf(stderr
, "No directory given for --work-tree.\n");
182 usage(perf_usage_string
);
184 setenv(PERF_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
189 } else if (!prefixcmp(cmd
, CMD_WORK_TREE
)) {
190 setenv(PERF_WORK_TREE_ENVIRONMENT
, cmd
+ strlen(CMD_WORK_TREE
), 1);
193 } else if (!strcmp(cmd
, "--debugfs-dir")) {
195 fprintf(stderr
, "No directory given for --debugfs-dir.\n");
196 usage(perf_usage_string
);
198 perf_debugfs_set_path((*argv
)[1]);
203 } else if (!strcmp(cmd
, "--buildid-dir")) {
205 fprintf(stderr
, "No directory given for --buildid-dir.\n");
206 usage(perf_usage_string
);
208 set_buildid_dir((*argv
)[1]);
213 } else if (!prefixcmp(cmd
, CMD_DEBUGFS_DIR
)) {
214 perf_debugfs_set_path(cmd
+ strlen(CMD_DEBUGFS_DIR
));
215 fprintf(stderr
, "dir: %s\n", debugfs_mountpoint
);
218 } else if (!strcmp(cmd
, "--list-cmds")) {
221 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
222 struct cmd_struct
*p
= commands
+i
;
223 printf("%s ", p
->cmd
);
226 } else if (!strcmp(cmd
, "--debug")) {
228 fprintf(stderr
, "No variable specified for --debug.\n");
229 usage(perf_usage_string
);
231 if (perf_debug_option((*argv
)[1]))
232 usage(perf_usage_string
);
237 fprintf(stderr
, "Unknown option: %s\n", cmd
);
238 usage(perf_usage_string
);
248 static int handle_alias(int *argcp
, const char ***argv
)
250 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
251 int count
, option_count
;
252 const char **new_argv
;
253 const char *alias_command
;
256 alias_command
= (*argv
)[0];
257 alias_string
= alias_lookup(alias_command
);
259 if (alias_string
[0] == '!') {
263 strbuf_init(&buf
, PATH_MAX
);
264 strbuf_addstr(&buf
, alias_string
);
265 sq_quote_argv(&buf
, (*argv
) + 1, PATH_MAX
);
267 alias_string
= buf
.buf
;
269 ret
= system(alias_string
+ 1);
270 if (ret
>= 0 && WIFEXITED(ret
) &&
271 WEXITSTATUS(ret
) != 127)
272 exit(WEXITSTATUS(ret
));
273 die("Failed to run '%s' when expanding alias '%s'",
274 alias_string
+ 1, alias_command
);
276 count
= split_cmdline(alias_string
, &new_argv
);
278 die("Bad alias.%s string", alias_command
);
279 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
281 die("alias '%s' changes environment variables\n"
282 "You can use '!perf' in the alias to do this.",
284 memmove(new_argv
- option_count
, new_argv
,
285 count
* sizeof(char *));
286 new_argv
-= option_count
;
289 die("empty alias for %s", alias_command
);
291 if (!strcmp(alias_command
, new_argv
[0]))
292 die("recursive alias: %s", alias_command
);
294 new_argv
= realloc(new_argv
, sizeof(char *) *
295 (count
+ *argcp
+ 1));
296 /* insert after command name */
297 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char *) * *argcp
);
298 new_argv
[count
+ *argcp
] = NULL
;
311 const char perf_version_string
[] = PERF_VERSION
;
313 #define RUN_SETUP (1<<0)
314 #define USE_PAGER (1<<1)
316 * require working tree to be present -- anything uses this needs
317 * RUN_SETUP for reading from the configuration file.
319 #define NEED_WORK_TREE (1<<2)
321 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
326 char sbuf
[STRERR_BUFSIZE
];
329 if (p
->option
& RUN_SETUP
)
330 prefix
= NULL
; /* setup_perf_directory(); */
332 if (use_browser
== -1)
333 use_browser
= check_browser_config(p
->cmd
);
335 if (use_pager
== -1 && p
->option
& RUN_SETUP
)
336 use_pager
= check_pager_config(p
->cmd
);
337 if (use_pager
== -1 && p
->option
& USE_PAGER
)
339 commit_pager_choice();
341 status
= p
->fn(argc
, argv
, prefix
);
342 exit_browser(status
);
345 return status
& 0xff;
347 /* Somebody closed stdout? */
348 if (fstat(fileno(stdout
), &st
))
350 /* Ignore write errors for pipes and sockets.. */
351 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
355 /* Check for ENOSPC and EIO errors.. */
356 if (fflush(stdout
)) {
357 fprintf(stderr
, "write failure on standard output: %s",
358 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
361 if (ferror(stdout
)) {
362 fprintf(stderr
, "unknown write failure on standard output");
365 if (fclose(stdout
)) {
366 fprintf(stderr
, "close failed on standard output: %s",
367 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
375 static void handle_internal_command(int argc
, const char **argv
)
377 const char *cmd
= argv
[0];
379 static const char ext
[] = STRIP_EXTENSION
;
381 if (sizeof(ext
) > 1) {
382 i
= strlen(argv
[0]) - strlen(ext
);
383 if (i
> 0 && !strcmp(argv
[0] + i
, ext
)) {
384 char *argv0
= strdup(argv
[0]);
385 argv
[0] = cmd
= argv0
;
390 /* Turn "perf cmd --help" into "perf help cmd" */
391 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
393 argv
[0] = cmd
= "help";
396 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
397 struct cmd_struct
*p
= commands
+i
;
398 if (strcmp(p
->cmd
, cmd
))
400 exit(run_builtin(p
, argc
, argv
));
404 static void execv_dashed_external(const char **argv
)
406 struct strbuf cmd
= STRBUF_INIT
;
410 strbuf_addf(&cmd
, "perf-%s", argv
[0]);
413 * argv[0] must be the perf command, but the argv array
414 * belongs to the caller, and may be reused in
415 * subsequent loop iterations. Save argv[0] and
416 * restore it on error.
422 * if we fail because the command is not found, it is
423 * OK to return. Otherwise, we just pass along the status code.
425 status
= run_command_v_opt(argv
, 0);
426 if (status
!= -ERR_RUN_COMMAND_EXEC
) {
427 if (IS_RUN_COMMAND_ERR(status
))
428 die("unable to run '%s'", argv
[0]);
431 errno
= ENOENT
; /* as if we called execvp */
435 strbuf_release(&cmd
);
438 static int run_argv(int *argcp
, const char ***argv
)
443 /* See if it's an internal command */
444 handle_internal_command(*argcp
, *argv
);
446 /* .. then try the external ones */
447 execv_dashed_external(*argv
);
449 /* It could be an alias -- this works around the insanity
450 * of overriding "perf log" with "perf show" by having
453 if (done_alias
|| !handle_alias(argcp
, argv
))
461 static void pthread__block_sigwinch(void)
466 sigaddset(&set
, SIGWINCH
);
467 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
470 void pthread__unblock_sigwinch(void)
475 sigaddset(&set
, SIGWINCH
);
476 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
479 int main(int argc
, const char **argv
)
482 char sbuf
[STRERR_BUFSIZE
];
484 /* The page_size is placed in util object. */
485 page_size
= sysconf(_SC_PAGE_SIZE
);
486 cacheline_size
= sysconf(_SC_LEVEL1_DCACHE_LINESIZE
);
488 cmd
= perf_extract_argv0_path(argv
[0]);
491 /* get debugfs mount point from /proc/mounts */
492 perf_debugfs_mount(NULL
);
494 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
496 * - cannot take flags in between the "perf" and the "xxxx".
497 * - cannot execute it externally (since it would just do
498 * the same thing over again)
500 * So we just directly call the internal command handler, and
501 * die if that one cannot handle it.
503 if (!prefixcmp(cmd
, "perf-")) {
506 handle_internal_command(argc
, argv
);
507 fprintf(stderr
, "cannot handle %s internally", cmd
);
510 if (!prefixcmp(cmd
, "trace")) {
511 #ifdef HAVE_LIBAUDIT_SUPPORT
512 set_buildid_dir(NULL
);
515 return cmd_trace(argc
, argv
, NULL
);
518 "trace command not available: missing audit-libs devel package at build time.\n");
522 /* Look for flags.. */
525 handle_options(&argv
, &argc
, NULL
);
526 commit_pager_choice();
527 set_buildid_dir(NULL
);
530 if (!prefixcmp(argv
[0], "--"))
533 /* The user didn't specify a command; give them help */
534 printf("\n usage: %s\n\n", perf_usage_string
);
535 list_common_cmds_help();
536 printf("\n %s\n\n", perf_more_info_string
);
544 * We use PATH to find perf commands, but we prepend some higher
545 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
546 * environment, and the $(perfexecdir) from the Makefile at build
551 * Block SIGWINCH notifications so that the thread that wants it can
552 * unblock and get syscalls like select interrupted instead of waiting
553 * forever while the signal goes to some other non interested thread.
555 pthread__block_sigwinch();
558 static int done_help
;
559 int was_alias
= run_argv(&argc
, &argv
);
565 fprintf(stderr
, "Expansion of alias '%s' failed; "
566 "'%s' is not a perf-command\n",
571 cmd
= argv
[0] = help_unknown_cmd(cmd
);
577 fprintf(stderr
, "Failed to run command '%s': %s\n",
578 cmd
, strerror_r(errno
, sbuf
, sizeof(sbuf
)));