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;
27 const char *input_name;
31 int (*fn)(int, const char **, const char *);
35 static struct cmd_struct commands[] = {
36 { "buildid-cache", cmd_buildid_cache, 0 },
37 { "buildid-list", cmd_buildid_list, 0 },
38 { "diff", cmd_diff, 0 },
39 { "evlist", cmd_evlist, 0 },
40 { "help", cmd_help, 0 },
41 { "list", cmd_list, 0 },
42 { "record", cmd_record, 0 },
43 { "report", cmd_report, 0 },
44 { "bench", cmd_bench, 0 },
45 { "stat", cmd_stat, 0 },
46 { "timechart", cmd_timechart, 0 },
47 { "top", cmd_top, 0 },
48 { "annotate", cmd_annotate, 0 },
49 { "version", cmd_version, 0 },
50 { "script", cmd_script, 0 },
51 { "sched", cmd_sched, 0 },
53 { "probe", cmd_probe, 0 },
55 { "kmem", cmd_kmem, 0 },
56 { "lock", cmd_lock, 0 },
57 { "kvm", cmd_kvm, 0 },
58 { "test", cmd_test, 0 },
59 #ifdef LIBAUDIT_SUPPORT
60 { "trace", cmd_trace, 0 },
62 { "inject", cmd_inject, 0 },
70 static int pager_command_config(const char *var, const char *value, void *data)
72 struct pager_config *c = data;
73 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
74 c->val = perf_config_bool(var, value);
78 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
79 int check_pager_config(const char *cmd)
81 struct pager_config c;
84 perf_config(pager_command_config, &c);
88 static int tui_command_config(const char *var, const char *value, void *data)
90 struct pager_config *c = data;
91 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
92 c->val = perf_config_bool(var, value);
96 /* returns 0 for "no tui", 1 for "use tui", and -1 for "not specified" */
97 static int check_tui_config(const char *cmd)
99 struct pager_config c;
102 perf_config(tui_command_config, &c);
106 static void commit_pager_choice(void)
110 setenv("PERF_PAGER", "cat", 1);
120 static int handle_options(const char ***argv, int *argc, int *envchanged)
125 const char *cmd = (*argv)[0];
130 * For legacy reasons, the "version" and "help"
131 * commands can be written with "--" prepended
132 * to make them look like flags.
134 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
138 * Check remaining flags.
140 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
141 cmd += strlen(CMD_EXEC_PATH);
143 perf_set_argv_exec_path(cmd + 1);
145 puts(perf_exec_path());
148 } else if (!strcmp(cmd, "--html-path")) {
149 puts(system_path(PERF_HTML_PATH));
151 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
153 } else if (!strcmp(cmd, "--no-pager")) {
157 } else if (!strcmp(cmd, "--perf-dir")) {
159 fprintf(stderr, "No directory given for --perf-dir.\n");
160 usage(perf_usage_string);
162 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
168 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
169 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
172 } else if (!strcmp(cmd, "--work-tree")) {
174 fprintf(stderr, "No directory given for --work-tree.\n");
175 usage(perf_usage_string);
177 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
182 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
183 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
186 } else if (!strcmp(cmd, "--debugfs-dir")) {
188 fprintf(stderr, "No directory given for --debugfs-dir.\n");
189 usage(perf_usage_string);
191 debugfs_set_path((*argv)[1]);
196 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
197 debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
198 fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
201 } else if (!strcmp(cmd, "--list-cmds")) {
204 for (i = 0; i < ARRAY_SIZE(commands); i++) {
205 struct cmd_struct *p = commands+i;
206 printf("%s ", p->cmd);
210 fprintf(stderr, "Unknown option: %s\n", cmd);
211 usage(perf_usage_string);
221 static int handle_alias(int *argcp, const char ***argv)
223 int envchanged = 0, ret = 0, saved_errno = errno;
224 int count, option_count;
225 const char **new_argv;
226 const char *alias_command;
229 alias_command = (*argv)[0];
230 alias_string = alias_lookup(alias_command);
232 if (alias_string[0] == '!') {
236 strbuf_init(&buf, PATH_MAX);
237 strbuf_addstr(&buf, alias_string);
238 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
240 alias_string = buf.buf;
242 ret = system(alias_string + 1);
243 if (ret >= 0 && WIFEXITED(ret) &&
244 WEXITSTATUS(ret) != 127)
245 exit(WEXITSTATUS(ret));
246 die("Failed to run '%s' when expanding alias '%s'",
247 alias_string + 1, alias_command);
249 count = split_cmdline(alias_string, &new_argv);
251 die("Bad alias.%s string", alias_command);
252 option_count = handle_options(&new_argv, &count, &envchanged);
254 die("alias '%s' changes environment variables\n"
255 "You can use '!perf' in the alias to do this.",
257 memmove(new_argv - option_count, new_argv,
258 count * sizeof(char *));
259 new_argv -= option_count;
262 die("empty alias for %s", alias_command);
264 if (!strcmp(alias_command, new_argv[0]))
265 die("recursive alias: %s", alias_command);
267 new_argv = realloc(new_argv, sizeof(char *) *
268 (count + *argcp + 1));
269 /* insert after command name */
270 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
271 new_argv[count + *argcp] = NULL;
284 const char perf_version_string[] = PERF_VERSION;
286 #define RUN_SETUP (1<<0)
287 #define USE_PAGER (1<<1)
289 * require working tree to be present -- anything uses this needs
290 * RUN_SETUP for reading from the configuration file.
292 #define NEED_WORK_TREE (1<<2)
294 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
301 if (p->option & RUN_SETUP)
302 prefix = NULL; /* setup_perf_directory(); */
304 if (use_browser == -1)
305 use_browser = check_tui_config(p->cmd);
307 if (use_pager == -1 && p->option & RUN_SETUP)
308 use_pager = check_pager_config(p->cmd);
309 if (use_pager == -1 && p->option & USE_PAGER)
311 commit_pager_choice();
313 status = p->fn(argc, argv, prefix);
314 exit_browser(status);
317 return status & 0xff;
319 /* Somebody closed stdout? */
320 if (fstat(fileno(stdout), &st))
322 /* Ignore write errors for pipes and sockets.. */
323 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
326 /* Check for ENOSPC and EIO errors.. */
328 die("write failure on standard output: %s", strerror(errno));
330 die("unknown write failure on standard output");
332 die("close failed on standard output: %s", strerror(errno));
336 static void handle_internal_command(int argc, const char **argv)
338 const char *cmd = argv[0];
340 static const char ext[] = STRIP_EXTENSION;
342 if (sizeof(ext) > 1) {
343 i = strlen(argv[0]) - strlen(ext);
344 if (i > 0 && !strcmp(argv[0] + i, ext)) {
345 char *argv0 = strdup(argv[0]);
346 argv[0] = cmd = argv0;
351 /* Turn "perf cmd --help" into "perf help cmd" */
352 if (argc > 1 && !strcmp(argv[1], "--help")) {
354 argv[0] = cmd = "help";
357 for (i = 0; i < ARRAY_SIZE(commands); i++) {
358 struct cmd_struct *p = commands+i;
359 if (strcmp(p->cmd, cmd))
361 exit(run_builtin(p, argc, argv));
365 static void execv_dashed_external(const char **argv)
367 struct strbuf cmd = STRBUF_INIT;
371 strbuf_addf(&cmd, "perf-%s", argv[0]);
374 * argv[0] must be the perf command, but the argv array
375 * belongs to the caller, and may be reused in
376 * subsequent loop iterations. Save argv[0] and
377 * restore it on error.
383 * if we fail because the command is not found, it is
384 * OK to return. Otherwise, we just pass along the status code.
386 status = run_command_v_opt(argv, 0);
387 if (status != -ERR_RUN_COMMAND_EXEC) {
388 if (IS_RUN_COMMAND_ERR(status))
389 die("unable to run '%s'", argv[0]);
392 errno = ENOENT; /* as if we called execvp */
396 strbuf_release(&cmd);
399 static int run_argv(int *argcp, const char ***argv)
404 /* See if it's an internal command */
405 handle_internal_command(*argcp, *argv);
407 /* .. then try the external ones */
408 execv_dashed_external(*argv);
410 /* It could be an alias -- this works around the insanity
411 * of overriding "perf log" with "perf show" by having
414 if (done_alias || !handle_alias(argcp, argv))
422 static void pthread__block_sigwinch(void)
427 sigaddset(&set, SIGWINCH);
428 pthread_sigmask(SIG_BLOCK, &set, NULL);
431 void pthread__unblock_sigwinch(void)
436 sigaddset(&set, SIGWINCH);
437 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
440 int main(int argc, const char **argv)
444 page_size = sysconf(_SC_PAGE_SIZE);
446 cmd = perf_extract_argv0_path(argv[0]);
449 /* get debugfs mount point from /proc/mounts */
452 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
454 * - cannot take flags in between the "perf" and the "xxxx".
455 * - cannot execute it externally (since it would just do
456 * the same thing over again)
458 * So we just directly call the internal command handler, and
459 * die if that one cannot handle it.
461 if (!prefixcmp(cmd, "perf-")) {
464 handle_internal_command(argc, argv);
465 die("cannot handle %s internally", cmd);
468 /* Look for flags.. */
471 handle_options(&argv, &argc, NULL);
472 commit_pager_choice();
476 if (!prefixcmp(argv[0], "--"))
479 /* The user didn't specify a command; give them help */
480 printf("\n usage: %s\n\n", perf_usage_string);
481 list_common_cmds_help();
482 printf("\n %s\n\n", perf_more_info_string);
488 * We use PATH to find perf commands, but we prepend some higher
489 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
490 * environment, and the $(perfexecdir) from the Makefile at build
495 * Block SIGWINCH notifications so that the thread that wants it can
496 * unblock and get syscalls like select interrupted instead of waiting
497 * forever while the signal goes to some other non interested thread.
499 pthread__block_sigwinch();
502 static int done_help;
503 static int was_alias;
505 was_alias = run_argv(&argc, &argv);
510 fprintf(stderr, "Expansion of alias '%s' failed; "
511 "'%s' is not a perf-command\n",
516 cmd = argv[0] = help_unknown_cmd(cmd);
522 fprintf(stderr, "Failed to run command '%s': %s\n",
523 cmd, strerror(errno));