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/string.h"
17 #include "util/debugfs.h"
21 const char perf_usage_string[] =
22 "perf [--version] [--help] COMMAND [ARGS]";
24 const char perf_more_info_string[] =
25 "See 'perf help COMMAND' for more information on a specific command.";
27 static int use_pager = -1;
33 static char debugfs_mntpt[MAXPATHLEN];
35 static int pager_command_config(const char *var, const char *value, void *data)
37 struct pager_config *c = data;
38 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
39 c->val = perf_config_bool(var, value);
43 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
44 int check_pager_config(const char *cmd)
46 struct pager_config c;
49 perf_config(pager_command_config, &c);
53 static void commit_pager_choice(void)
57 setenv("PERF_PAGER", "cat", 1);
67 static void set_debugfs_path(void)
71 path = getenv(PERF_DEBUGFS_ENVIRONMENT);
72 snprintf(debugfs_path, MAXPATHLEN, "%s/%s", path ?: debugfs_mntpt,
76 static int handle_options(const char ***argv, int *argc, int *envchanged)
81 const char *cmd = (*argv)[0];
86 * For legacy reasons, the "version" and "help"
87 * commands can be written with "--" prepended
88 * to make them look like flags.
90 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
94 * Check remaining flags.
96 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
97 cmd += strlen(CMD_EXEC_PATH);
99 perf_set_argv_exec_path(cmd + 1);
101 puts(perf_exec_path());
104 } else if (!strcmp(cmd, "--html-path")) {
105 puts(system_path(PERF_HTML_PATH));
107 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
109 } else if (!strcmp(cmd, "--no-pager")) {
113 } else if (!strcmp(cmd, "--perf-dir")) {
115 fprintf(stderr, "No directory given for --perf-dir.\n");
116 usage(perf_usage_string);
118 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
124 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
125 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
128 } else if (!strcmp(cmd, "--work-tree")) {
130 fprintf(stderr, "No directory given for --work-tree.\n");
131 usage(perf_usage_string);
133 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
138 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
139 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
142 } else if (!strcmp(cmd, "--debugfs-dir")) {
144 fprintf(stderr, "No directory given for --debugfs-dir.\n");
145 usage(perf_usage_string);
147 strncpy(debugfs_mntpt, (*argv)[1], MAXPATHLEN);
148 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
153 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
154 strncpy(debugfs_mntpt, cmd + strlen(CMD_DEBUGFS_DIR), MAXPATHLEN);
155 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
159 fprintf(stderr, "Unknown option: %s\n", cmd);
160 usage(perf_usage_string);
170 static int handle_alias(int *argcp, const char ***argv)
172 int envchanged = 0, ret = 0, saved_errno = errno;
173 int count, option_count;
174 const char **new_argv;
175 const char *alias_command;
178 alias_command = (*argv)[0];
179 alias_string = alias_lookup(alias_command);
181 if (alias_string[0] == '!') {
185 strbuf_init(&buf, PATH_MAX);
186 strbuf_addstr(&buf, alias_string);
187 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
189 alias_string = buf.buf;
191 ret = system(alias_string + 1);
192 if (ret >= 0 && WIFEXITED(ret) &&
193 WEXITSTATUS(ret) != 127)
194 exit(WEXITSTATUS(ret));
195 die("Failed to run '%s' when expanding alias '%s'",
196 alias_string + 1, alias_command);
198 count = split_cmdline(alias_string, &new_argv);
200 die("Bad alias.%s string", alias_command);
201 option_count = handle_options(&new_argv, &count, &envchanged);
203 die("alias '%s' changes environment variables\n"
204 "You can use '!perf' in the alias to do this.",
206 memmove(new_argv - option_count, new_argv,
207 count * sizeof(char *));
208 new_argv -= option_count;
211 die("empty alias for %s", alias_command);
213 if (!strcmp(alias_command, new_argv[0]))
214 die("recursive alias: %s", alias_command);
216 new_argv = realloc(new_argv, sizeof(char *) *
217 (count + *argcp + 1));
218 /* insert after command name */
219 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
220 new_argv[count + *argcp] = NULL;
233 const char perf_version_string[] = PERF_VERSION;
235 #define RUN_SETUP (1<<0)
236 #define USE_PAGER (1<<1)
238 * require working tree to be present -- anything uses this needs
239 * RUN_SETUP for reading from the configuration file.
241 #define NEED_WORK_TREE (1<<2)
245 int (*fn)(int, const char **, const char *);
249 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
256 if (p->option & RUN_SETUP)
257 prefix = NULL; /* setup_perf_directory(); */
259 if (use_pager == -1 && p->option & RUN_SETUP)
260 use_pager = check_pager_config(p->cmd);
261 if (use_pager == -1 && p->option & USE_PAGER)
263 commit_pager_choice();
266 status = p->fn(argc, argv, prefix);
268 return status & 0xff;
272 /* Somebody closed stdout? */
273 if (fstat(fileno(stdout), &st))
275 /* Ignore write errors for pipes and sockets.. */
276 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
279 /* Check for ENOSPC and EIO errors.. */
281 die("write failure on standard output: %s", strerror(errno));
283 die("unknown write failure on standard output");
285 die("close failed on standard output: %s", strerror(errno));
289 static void handle_internal_command(int argc, const char **argv)
291 const char *cmd = argv[0];
292 static struct cmd_struct commands[] = {
293 { "buildid-cache", cmd_buildid_cache, 0 },
294 { "buildid-list", cmd_buildid_list, 0 },
295 { "diff", cmd_diff, 0 },
296 { "help", cmd_help, 0 },
297 { "list", cmd_list, 0 },
298 { "record", cmd_record, 0 },
299 { "report", cmd_report, 0 },
300 { "bench", cmd_bench, 0 },
301 { "stat", cmd_stat, 0 },
302 { "timechart", cmd_timechart, 0 },
303 { "top", cmd_top, 0 },
304 { "annotate", cmd_annotate, 0 },
305 { "version", cmd_version, 0 },
306 { "trace", cmd_trace, 0 },
307 { "sched", cmd_sched, 0 },
308 { "probe", cmd_probe, 0 },
309 { "kmem", cmd_kmem, 0 },
310 { "lock", cmd_lock, 0 },
313 static const char ext[] = STRIP_EXTENSION;
315 if (sizeof(ext) > 1) {
316 i = strlen(argv[0]) - strlen(ext);
317 if (i > 0 && !strcmp(argv[0] + i, ext)) {
318 char *argv0 = strdup(argv[0]);
319 argv[0] = cmd = argv0;
324 /* Turn "perf cmd --help" into "perf help cmd" */
325 if (argc > 1 && !strcmp(argv[1], "--help")) {
327 argv[0] = cmd = "help";
330 for (i = 0; i < ARRAY_SIZE(commands); i++) {
331 struct cmd_struct *p = commands+i;
332 if (strcmp(p->cmd, cmd))
334 exit(run_builtin(p, argc, argv));
338 static void execv_dashed_external(const char **argv)
340 struct strbuf cmd = STRBUF_INIT;
344 strbuf_addf(&cmd, "perf-%s", argv[0]);
347 * argv[0] must be the perf command, but the argv array
348 * belongs to the caller, and may be reused in
349 * subsequent loop iterations. Save argv[0] and
350 * restore it on error.
356 * if we fail because the command is not found, it is
357 * OK to return. Otherwise, we just pass along the status code.
359 status = run_command_v_opt(argv, 0);
360 if (status != -ERR_RUN_COMMAND_EXEC) {
361 if (IS_RUN_COMMAND_ERR(status))
362 die("unable to run '%s'", argv[0]);
365 errno = ENOENT; /* as if we called execvp */
369 strbuf_release(&cmd);
372 static int run_argv(int *argcp, const char ***argv)
377 /* See if it's an internal command */
378 handle_internal_command(*argcp, *argv);
380 /* .. then try the external ones */
381 execv_dashed_external(*argv);
383 /* It could be an alias -- this works around the insanity
384 * of overriding "perf log" with "perf show" by having
387 if (done_alias || !handle_alias(argcp, argv))
395 /* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
396 static void get_debugfs_mntpt(void)
398 const char *path = debugfs_mount(NULL);
401 strncpy(debugfs_mntpt, path, sizeof(debugfs_mntpt));
403 debugfs_mntpt[0] = '\0';
406 int main(int argc, const char **argv)
410 cmd = perf_extract_argv0_path(argv[0]);
413 /* get debugfs mount point from /proc/mounts */
416 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
418 * - cannot take flags in between the "perf" and the "xxxx".
419 * - cannot execute it externally (since it would just do
420 * the same thing over again)
422 * So we just directly call the internal command handler, and
423 * die if that one cannot handle it.
425 if (!prefixcmp(cmd, "perf-")) {
428 handle_internal_command(argc, argv);
429 die("cannot handle %s internally", cmd);
432 /* Look for flags.. */
435 handle_options(&argv, &argc, NULL);
436 commit_pager_choice();
439 if (!prefixcmp(argv[0], "--"))
442 /* The user didn't specify a command; give them help */
443 printf("\n usage: %s\n\n", perf_usage_string);
444 list_common_cmds_help();
445 printf("\n %s\n\n", perf_more_info_string);
451 * We use PATH to find perf commands, but we prepend some higher
452 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
453 * environment, and the $(perfexecdir) from the Makefile at build
459 static int done_help;
460 static int was_alias;
462 was_alias = run_argv(&argc, &argv);
467 fprintf(stderr, "Expansion of alias '%s' failed; "
468 "'%s' is not a perf-command\n",
473 cmd = argv[0] = help_unknown_cmd(cmd);
479 fprintf(stderr, "Failed to run command '%s': %s\n",
480 cmd, strerror(errno));