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 },
65 { "data", cmd_data
, 0 },
73 static int pager_command_config(const char *var
, const char *value
, void *data
)
75 struct pager_config
*c
= data
;
76 if (!prefixcmp(var
, "pager.") && !strcmp(var
+ 6, c
->cmd
))
77 c
->val
= perf_config_bool(var
, value
);
81 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
82 int check_pager_config(const char *cmd
)
84 struct pager_config c
;
87 perf_config(pager_command_config
, &c
);
91 static int browser_command_config(const char *var
, const char *value
, void *data
)
93 struct pager_config
*c
= data
;
94 if (!prefixcmp(var
, "tui.") && !strcmp(var
+ 4, c
->cmd
))
95 c
->val
= perf_config_bool(var
, value
);
96 if (!prefixcmp(var
, "gtk.") && !strcmp(var
+ 4, c
->cmd
))
97 c
->val
= perf_config_bool(var
, value
) ? 2 : 0;
102 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
103 * and -1 for "not specified"
105 static int check_browser_config(const char *cmd
)
107 struct pager_config c
;
110 perf_config(browser_command_config
, &c
);
114 static void commit_pager_choice(void)
118 setenv("PERF_PAGER", "cat", 1);
128 static int handle_options(const char ***argv
, int *argc
, int *envchanged
)
133 const char *cmd
= (*argv
)[0];
138 * For legacy reasons, the "version" and "help"
139 * commands can be written with "--" prepended
140 * to make them look like flags.
142 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
146 * Check remaining flags.
148 if (!prefixcmp(cmd
, CMD_EXEC_PATH
)) {
149 cmd
+= strlen(CMD_EXEC_PATH
);
151 perf_set_argv_exec_path(cmd
+ 1);
153 puts(perf_exec_path());
156 } else if (!strcmp(cmd
, "--html-path")) {
157 puts(system_path(PERF_HTML_PATH
));
159 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
161 } else if (!strcmp(cmd
, "--no-pager")) {
165 } else if (!strcmp(cmd
, "--perf-dir")) {
167 fprintf(stderr
, "No directory given for --perf-dir.\n");
168 usage(perf_usage_string
);
170 setenv(PERF_DIR_ENVIRONMENT
, (*argv
)[1], 1);
176 } else if (!prefixcmp(cmd
, CMD_PERF_DIR
)) {
177 setenv(PERF_DIR_ENVIRONMENT
, cmd
+ strlen(CMD_PERF_DIR
), 1);
180 } else if (!strcmp(cmd
, "--work-tree")) {
182 fprintf(stderr
, "No directory given for --work-tree.\n");
183 usage(perf_usage_string
);
185 setenv(PERF_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
190 } else if (!prefixcmp(cmd
, CMD_WORK_TREE
)) {
191 setenv(PERF_WORK_TREE_ENVIRONMENT
, cmd
+ strlen(CMD_WORK_TREE
), 1);
194 } else if (!strcmp(cmd
, "--debugfs-dir")) {
196 fprintf(stderr
, "No directory given for --debugfs-dir.\n");
197 usage(perf_usage_string
);
199 perf_debugfs_set_path((*argv
)[1]);
204 } else if (!strcmp(cmd
, "--buildid-dir")) {
206 fprintf(stderr
, "No directory given for --buildid-dir.\n");
207 usage(perf_usage_string
);
209 set_buildid_dir((*argv
)[1]);
214 } else if (!prefixcmp(cmd
, CMD_DEBUGFS_DIR
)) {
215 perf_debugfs_set_path(cmd
+ strlen(CMD_DEBUGFS_DIR
));
216 fprintf(stderr
, "dir: %s\n", debugfs_mountpoint
);
219 } else if (!strcmp(cmd
, "--list-cmds")) {
222 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
223 struct cmd_struct
*p
= commands
+i
;
224 printf("%s ", p
->cmd
);
227 } else if (!strcmp(cmd
, "--debug")) {
229 fprintf(stderr
, "No variable specified for --debug.\n");
230 usage(perf_usage_string
);
232 if (perf_debug_option((*argv
)[1]))
233 usage(perf_usage_string
);
238 fprintf(stderr
, "Unknown option: %s\n", cmd
);
239 usage(perf_usage_string
);
249 static int handle_alias(int *argcp
, const char ***argv
)
251 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
252 int count
, option_count
;
253 const char **new_argv
;
254 const char *alias_command
;
257 alias_command
= (*argv
)[0];
258 alias_string
= alias_lookup(alias_command
);
260 if (alias_string
[0] == '!') {
264 strbuf_init(&buf
, PATH_MAX
);
265 strbuf_addstr(&buf
, alias_string
);
266 sq_quote_argv(&buf
, (*argv
) + 1, PATH_MAX
);
268 alias_string
= buf
.buf
;
270 ret
= system(alias_string
+ 1);
271 if (ret
>= 0 && WIFEXITED(ret
) &&
272 WEXITSTATUS(ret
) != 127)
273 exit(WEXITSTATUS(ret
));
274 die("Failed to run '%s' when expanding alias '%s'",
275 alias_string
+ 1, alias_command
);
277 count
= split_cmdline(alias_string
, &new_argv
);
279 die("Bad alias.%s string", alias_command
);
280 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
282 die("alias '%s' changes environment variables\n"
283 "You can use '!perf' in the alias to do this.",
285 memmove(new_argv
- option_count
, new_argv
,
286 count
* sizeof(char *));
287 new_argv
-= option_count
;
290 die("empty alias for %s", alias_command
);
292 if (!strcmp(alias_command
, new_argv
[0]))
293 die("recursive alias: %s", alias_command
);
295 new_argv
= realloc(new_argv
, sizeof(char *) *
296 (count
+ *argcp
+ 1));
297 /* insert after command name */
298 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char *) * *argcp
);
299 new_argv
[count
+ *argcp
] = NULL
;
312 const char perf_version_string
[] = PERF_VERSION
;
314 #define RUN_SETUP (1<<0)
315 #define USE_PAGER (1<<1)
317 * require working tree to be present -- anything uses this needs
318 * RUN_SETUP for reading from the configuration file.
320 #define NEED_WORK_TREE (1<<2)
322 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
327 char sbuf
[STRERR_BUFSIZE
];
330 if (p
->option
& RUN_SETUP
)
331 prefix
= NULL
; /* setup_perf_directory(); */
333 if (use_browser
== -1)
334 use_browser
= check_browser_config(p
->cmd
);
336 if (use_pager
== -1 && p
->option
& RUN_SETUP
)
337 use_pager
= check_pager_config(p
->cmd
);
338 if (use_pager
== -1 && p
->option
& USE_PAGER
)
340 commit_pager_choice();
342 status
= p
->fn(argc
, argv
, prefix
);
343 exit_browser(status
);
346 return status
& 0xff;
348 /* Somebody closed stdout? */
349 if (fstat(fileno(stdout
), &st
))
351 /* Ignore write errors for pipes and sockets.. */
352 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
356 /* Check for ENOSPC and EIO errors.. */
357 if (fflush(stdout
)) {
358 fprintf(stderr
, "write failure on standard output: %s",
359 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
362 if (ferror(stdout
)) {
363 fprintf(stderr
, "unknown write failure on standard output");
366 if (fclose(stdout
)) {
367 fprintf(stderr
, "close failed on standard output: %s",
368 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
376 static void handle_internal_command(int argc
, const char **argv
)
378 const char *cmd
= argv
[0];
380 static const char ext
[] = STRIP_EXTENSION
;
382 if (sizeof(ext
) > 1) {
383 i
= strlen(argv
[0]) - strlen(ext
);
384 if (i
> 0 && !strcmp(argv
[0] + i
, ext
)) {
385 char *argv0
= strdup(argv
[0]);
386 argv
[0] = cmd
= argv0
;
391 /* Turn "perf cmd --help" into "perf help cmd" */
392 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
394 argv
[0] = cmd
= "help";
397 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
398 struct cmd_struct
*p
= commands
+i
;
399 if (strcmp(p
->cmd
, cmd
))
401 exit(run_builtin(p
, argc
, argv
));
405 static void execv_dashed_external(const char **argv
)
407 struct strbuf cmd
= STRBUF_INIT
;
411 strbuf_addf(&cmd
, "perf-%s", argv
[0]);
414 * argv[0] must be the perf command, but the argv array
415 * belongs to the caller, and may be reused in
416 * subsequent loop iterations. Save argv[0] and
417 * restore it on error.
423 * if we fail because the command is not found, it is
424 * OK to return. Otherwise, we just pass along the status code.
426 status
= run_command_v_opt(argv
, 0);
427 if (status
!= -ERR_RUN_COMMAND_EXEC
) {
428 if (IS_RUN_COMMAND_ERR(status
))
429 die("unable to run '%s'", argv
[0]);
432 errno
= ENOENT
; /* as if we called execvp */
436 strbuf_release(&cmd
);
439 static int run_argv(int *argcp
, const char ***argv
)
444 /* See if it's an internal command */
445 handle_internal_command(*argcp
, *argv
);
447 /* .. then try the external ones */
448 execv_dashed_external(*argv
);
450 /* It could be an alias -- this works around the insanity
451 * of overriding "perf log" with "perf show" by having
454 if (done_alias
|| !handle_alias(argcp
, argv
))
462 static void pthread__block_sigwinch(void)
467 sigaddset(&set
, SIGWINCH
);
468 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
471 void pthread__unblock_sigwinch(void)
476 sigaddset(&set
, SIGWINCH
);
477 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
480 int main(int argc
, const char **argv
)
483 char sbuf
[STRERR_BUFSIZE
];
485 /* The page_size is placed in util object. */
486 page_size
= sysconf(_SC_PAGE_SIZE
);
487 cacheline_size
= sysconf(_SC_LEVEL1_DCACHE_LINESIZE
);
489 cmd
= perf_extract_argv0_path(argv
[0]);
492 /* get debugfs mount point from /proc/mounts */
493 perf_debugfs_mount(NULL
);
495 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
497 * - cannot take flags in between the "perf" and the "xxxx".
498 * - cannot execute it externally (since it would just do
499 * the same thing over again)
501 * So we just directly call the internal command handler, and
502 * die if that one cannot handle it.
504 if (!prefixcmp(cmd
, "perf-")) {
507 handle_internal_command(argc
, argv
);
508 fprintf(stderr
, "cannot handle %s internally", cmd
);
511 if (!prefixcmp(cmd
, "trace")) {
512 #ifdef HAVE_LIBAUDIT_SUPPORT
513 set_buildid_dir(NULL
);
516 return cmd_trace(argc
, argv
, NULL
);
519 "trace command not available: missing audit-libs devel package at build time.\n");
523 /* Look for flags.. */
526 handle_options(&argv
, &argc
, NULL
);
527 commit_pager_choice();
528 set_buildid_dir(NULL
);
531 if (!prefixcmp(argv
[0], "--"))
534 /* The user didn't specify a command; give them help */
535 printf("\n usage: %s\n\n", perf_usage_string
);
536 list_common_cmds_help();
537 printf("\n %s\n\n", perf_more_info_string
);
545 * We use PATH to find perf commands, but we prepend some higher
546 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
547 * environment, and the $(perfexecdir) from the Makefile at build
552 * Block SIGWINCH notifications so that the thread that wants it can
553 * unblock and get syscalls like select interrupted instead of waiting
554 * forever while the signal goes to some other non interested thread.
556 pthread__block_sigwinch();
559 static int done_help
;
560 int was_alias
= run_argv(&argc
, &argv
);
566 fprintf(stderr
, "Expansion of alias '%s' failed; "
567 "'%s' is not a perf-command\n",
572 cmd
= argv
[0] = help_unknown_cmd(cmd
);
578 fprintf(stderr
, "Failed to run command '%s': %s\n",
579 cmd
, strerror_r(errno
, sbuf
, sizeof(sbuf
)));