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/debugfs.h"
19 const char perf_usage_string
[] =
20 "perf [--version] [--help] COMMAND [ARGS]";
22 const char perf_more_info_string
[] =
23 "See 'perf help COMMAND' for more information on a specific command.";
26 static int use_pager
= -1;
30 int (*fn
)(int, const char **, const char *);
34 static struct cmd_struct commands
[] = {
35 { "buildid-cache", cmd_buildid_cache
, 0 },
36 { "buildid-list", cmd_buildid_list
, 0 },
37 { "diff", cmd_diff
, 0 },
38 { "evlist", cmd_evlist
, 0 },
39 { "help", cmd_help
, 0 },
40 { "list", cmd_list
, 0 },
41 { "record", cmd_record
, 0 },
42 { "report", cmd_report
, 0 },
43 { "bench", cmd_bench
, 0 },
44 { "stat", cmd_stat
, 0 },
45 { "timechart", cmd_timechart
, 0 },
46 { "top", cmd_top
, 0 },
47 { "annotate", cmd_annotate
, 0 },
48 { "version", cmd_version
, 0 },
49 { "script", cmd_script
, 0 },
50 { "sched", cmd_sched
, 0 },
52 { "probe", cmd_probe
, 0 },
54 { "kmem", cmd_kmem
, 0 },
55 { "lock", cmd_lock
, 0 },
56 { "kvm", cmd_kvm
, 0 },
57 { "test", cmd_test
, 0 },
58 #ifdef LIBAUDIT_SUPPORT
59 { "trace", cmd_trace
, 0 },
61 { "inject", cmd_inject
, 0 },
69 static int pager_command_config(const char *var
, const char *value
, void *data
)
71 struct pager_config
*c
= data
;
72 if (!prefixcmp(var
, "pager.") && !strcmp(var
+ 6, c
->cmd
))
73 c
->val
= perf_config_bool(var
, value
);
77 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
78 int check_pager_config(const char *cmd
)
80 struct pager_config c
;
83 perf_config(pager_command_config
, &c
);
87 static int tui_command_config(const char *var
, const char *value
, void *data
)
89 struct pager_config
*c
= data
;
90 if (!prefixcmp(var
, "tui.") && !strcmp(var
+ 4, c
->cmd
))
91 c
->val
= perf_config_bool(var
, value
);
95 /* returns 0 for "no tui", 1 for "use tui", and -1 for "not specified" */
96 static int check_tui_config(const char *cmd
)
98 struct pager_config c
;
101 perf_config(tui_command_config
, &c
);
105 static void commit_pager_choice(void)
109 setenv("PERF_PAGER", "cat", 1);
119 static int handle_options(const char ***argv
, int *argc
, int *envchanged
)
124 const char *cmd
= (*argv
)[0];
129 * For legacy reasons, the "version" and "help"
130 * commands can be written with "--" prepended
131 * to make them look like flags.
133 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
137 * Check remaining flags.
139 if (!prefixcmp(cmd
, CMD_EXEC_PATH
)) {
140 cmd
+= strlen(CMD_EXEC_PATH
);
142 perf_set_argv_exec_path(cmd
+ 1);
144 puts(perf_exec_path());
147 } else if (!strcmp(cmd
, "--html-path")) {
148 puts(system_path(PERF_HTML_PATH
));
150 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
152 } else if (!strcmp(cmd
, "--no-pager")) {
156 } else if (!strcmp(cmd
, "--perf-dir")) {
158 fprintf(stderr
, "No directory given for --perf-dir.\n");
159 usage(perf_usage_string
);
161 setenv(PERF_DIR_ENVIRONMENT
, (*argv
)[1], 1);
167 } else if (!prefixcmp(cmd
, CMD_PERF_DIR
)) {
168 setenv(PERF_DIR_ENVIRONMENT
, cmd
+ strlen(CMD_PERF_DIR
), 1);
171 } else if (!strcmp(cmd
, "--work-tree")) {
173 fprintf(stderr
, "No directory given for --work-tree.\n");
174 usage(perf_usage_string
);
176 setenv(PERF_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
181 } else if (!prefixcmp(cmd
, CMD_WORK_TREE
)) {
182 setenv(PERF_WORK_TREE_ENVIRONMENT
, cmd
+ strlen(CMD_WORK_TREE
), 1);
185 } else if (!strcmp(cmd
, "--debugfs-dir")) {
187 fprintf(stderr
, "No directory given for --debugfs-dir.\n");
188 usage(perf_usage_string
);
190 debugfs_set_path((*argv
)[1]);
195 } else if (!prefixcmp(cmd
, CMD_DEBUGFS_DIR
)) {
196 debugfs_set_path(cmd
+ strlen(CMD_DEBUGFS_DIR
));
197 fprintf(stderr
, "dir: %s\n", debugfs_mountpoint
);
200 } else if (!strcmp(cmd
, "--list-cmds")) {
203 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
204 struct cmd_struct
*p
= commands
+i
;
205 printf("%s ", p
->cmd
);
209 fprintf(stderr
, "Unknown option: %s\n", cmd
);
210 usage(perf_usage_string
);
220 static int handle_alias(int *argcp
, const char ***argv
)
222 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
223 int count
, option_count
;
224 const char **new_argv
;
225 const char *alias_command
;
228 alias_command
= (*argv
)[0];
229 alias_string
= alias_lookup(alias_command
);
231 if (alias_string
[0] == '!') {
235 strbuf_init(&buf
, PATH_MAX
);
236 strbuf_addstr(&buf
, alias_string
);
237 sq_quote_argv(&buf
, (*argv
) + 1, PATH_MAX
);
239 alias_string
= buf
.buf
;
241 ret
= system(alias_string
+ 1);
242 if (ret
>= 0 && WIFEXITED(ret
) &&
243 WEXITSTATUS(ret
) != 127)
244 exit(WEXITSTATUS(ret
));
245 die("Failed to run '%s' when expanding alias '%s'",
246 alias_string
+ 1, alias_command
);
248 count
= split_cmdline(alias_string
, &new_argv
);
250 die("Bad alias.%s string", alias_command
);
251 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
253 die("alias '%s' changes environment variables\n"
254 "You can use '!perf' in the alias to do this.",
256 memmove(new_argv
- option_count
, new_argv
,
257 count
* sizeof(char *));
258 new_argv
-= option_count
;
261 die("empty alias for %s", alias_command
);
263 if (!strcmp(alias_command
, new_argv
[0]))
264 die("recursive alias: %s", alias_command
);
266 new_argv
= realloc(new_argv
, sizeof(char *) *
267 (count
+ *argcp
+ 1));
268 /* insert after command name */
269 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char *) * *argcp
);
270 new_argv
[count
+ *argcp
] = NULL
;
283 const char perf_version_string
[] = PERF_VERSION
;
285 #define RUN_SETUP (1<<0)
286 #define USE_PAGER (1<<1)
288 * require working tree to be present -- anything uses this needs
289 * RUN_SETUP for reading from the configuration file.
291 #define NEED_WORK_TREE (1<<2)
293 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
300 if (p
->option
& RUN_SETUP
)
301 prefix
= NULL
; /* setup_perf_directory(); */
303 if (use_browser
== -1)
304 use_browser
= check_tui_config(p
->cmd
);
306 if (use_pager
== -1 && p
->option
& RUN_SETUP
)
307 use_pager
= check_pager_config(p
->cmd
);
308 if (use_pager
== -1 && p
->option
& USE_PAGER
)
310 commit_pager_choice();
312 status
= p
->fn(argc
, argv
, prefix
);
313 exit_browser(status
);
316 return status
& 0xff;
318 /* Somebody closed stdout? */
319 if (fstat(fileno(stdout
), &st
))
321 /* Ignore write errors for pipes and sockets.. */
322 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
325 /* Check for ENOSPC and EIO errors.. */
327 die("write failure on standard output: %s", strerror(errno
));
329 die("unknown write failure on standard output");
331 die("close failed on standard output: %s", strerror(errno
));
335 static void handle_internal_command(int argc
, const char **argv
)
337 const char *cmd
= argv
[0];
339 static const char ext
[] = STRIP_EXTENSION
;
341 if (sizeof(ext
) > 1) {
342 i
= strlen(argv
[0]) - strlen(ext
);
343 if (i
> 0 && !strcmp(argv
[0] + i
, ext
)) {
344 char *argv0
= strdup(argv
[0]);
345 argv
[0] = cmd
= argv0
;
350 /* Turn "perf cmd --help" into "perf help cmd" */
351 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
353 argv
[0] = cmd
= "help";
356 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
357 struct cmd_struct
*p
= commands
+i
;
358 if (strcmp(p
->cmd
, cmd
))
360 exit(run_builtin(p
, argc
, argv
));
364 static void execv_dashed_external(const char **argv
)
366 struct strbuf cmd
= STRBUF_INIT
;
370 strbuf_addf(&cmd
, "perf-%s", argv
[0]);
373 * argv[0] must be the perf command, but the argv array
374 * belongs to the caller, and may be reused in
375 * subsequent loop iterations. Save argv[0] and
376 * restore it on error.
382 * if we fail because the command is not found, it is
383 * OK to return. Otherwise, we just pass along the status code.
385 status
= run_command_v_opt(argv
, 0);
386 if (status
!= -ERR_RUN_COMMAND_EXEC
) {
387 if (IS_RUN_COMMAND_ERR(status
))
388 die("unable to run '%s'", argv
[0]);
391 errno
= ENOENT
; /* as if we called execvp */
395 strbuf_release(&cmd
);
398 static int run_argv(int *argcp
, const char ***argv
)
403 /* See if it's an internal command */
404 handle_internal_command(*argcp
, *argv
);
406 /* .. then try the external ones */
407 execv_dashed_external(*argv
);
409 /* It could be an alias -- this works around the insanity
410 * of overriding "perf log" with "perf show" by having
413 if (done_alias
|| !handle_alias(argcp
, argv
))
421 static void pthread__block_sigwinch(void)
426 sigaddset(&set
, SIGWINCH
);
427 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
430 void pthread__unblock_sigwinch(void)
435 sigaddset(&set
, SIGWINCH
);
436 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
439 int main(int argc
, const char **argv
)
443 cmd
= perf_extract_argv0_path(argv
[0]);
446 /* get debugfs mount point from /proc/mounts */
449 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
451 * - cannot take flags in between the "perf" and the "xxxx".
452 * - cannot execute it externally (since it would just do
453 * the same thing over again)
455 * So we just directly call the internal command handler, and
456 * die if that one cannot handle it.
458 if (!prefixcmp(cmd
, "perf-")) {
461 handle_internal_command(argc
, argv
);
462 die("cannot handle %s internally", cmd
);
465 /* Look for flags.. */
468 handle_options(&argv
, &argc
, NULL
);
469 commit_pager_choice();
473 if (!prefixcmp(argv
[0], "--"))
476 /* The user didn't specify a command; give them help */
477 printf("\n usage: %s\n\n", perf_usage_string
);
478 list_common_cmds_help();
479 printf("\n %s\n\n", perf_more_info_string
);
485 * We use PATH to find perf commands, but we prepend some higher
486 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
487 * environment, and the $(perfexecdir) from the Makefile at build
492 * Block SIGWINCH notifications so that the thread that wants it can
493 * unblock and get syscalls like select interrupted instead of waiting
494 * forever while the signal goes to some other non interested thread.
496 pthread__block_sigwinch();
499 static int done_help
;
500 static int was_alias
;
502 was_alias
= run_argv(&argc
, &argv
);
507 fprintf(stderr
, "Expansion of alias '%s' failed; "
508 "'%s' is not a perf-command\n",
513 cmd
= argv
[0] = help_unknown_cmd(cmd
);
519 fprintf(stderr
, "Failed to run command '%s': %s\n",
520 cmd
, strerror(errno
));