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"
20 const char perf_usage_string
[] =
21 "perf [--version] [--help] COMMAND [ARGS]";
23 const char perf_more_info_string
[] =
24 "See 'perf help COMMAND' for more information on a specific command.";
26 static int use_pager
= -1;
32 static char debugfs_mntpt
[MAXPATHLEN
];
34 static int pager_command_config(const char *var
, const char *value
, void *data
)
36 struct pager_config
*c
= data
;
37 if (!prefixcmp(var
, "pager.") && !strcmp(var
+ 6, c
->cmd
))
38 c
->val
= perf_config_bool(var
, value
);
42 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
43 int check_pager_config(const char *cmd
)
45 struct pager_config c
;
48 perf_config(pager_command_config
, &c
);
52 static void commit_pager_choice(void)
56 setenv("PERF_PAGER", "cat", 1);
66 static void set_debugfs_path(void)
70 path
= getenv(PERF_DEBUGFS_ENVIRONMENT
);
71 snprintf(debugfs_path
, MAXPATHLEN
, "%s/%s", path
?: debugfs_mntpt
,
75 static int handle_options(const char ***argv
, int *argc
, int *envchanged
)
80 const char *cmd
= (*argv
)[0];
85 * For legacy reasons, the "version" and "help"
86 * commands can be written with "--" prepended
87 * to make them look like flags.
89 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
93 * Check remaining flags.
95 if (!prefixcmp(cmd
, CMD_EXEC_PATH
)) {
96 cmd
+= strlen(CMD_EXEC_PATH
);
98 perf_set_argv_exec_path(cmd
+ 1);
100 puts(perf_exec_path());
103 } else if (!strcmp(cmd
, "--html-path")) {
104 puts(system_path(PERF_HTML_PATH
));
106 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
108 } else if (!strcmp(cmd
, "--no-pager")) {
112 } else if (!strcmp(cmd
, "--perf-dir")) {
114 fprintf(stderr
, "No directory given for --perf-dir.\n");
115 usage(perf_usage_string
);
117 setenv(PERF_DIR_ENVIRONMENT
, (*argv
)[1], 1);
123 } else if (!prefixcmp(cmd
, CMD_PERF_DIR
)) {
124 setenv(PERF_DIR_ENVIRONMENT
, cmd
+ strlen(CMD_PERF_DIR
), 1);
127 } else if (!strcmp(cmd
, "--work-tree")) {
129 fprintf(stderr
, "No directory given for --work-tree.\n");
130 usage(perf_usage_string
);
132 setenv(PERF_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
137 } else if (!prefixcmp(cmd
, CMD_WORK_TREE
)) {
138 setenv(PERF_WORK_TREE_ENVIRONMENT
, cmd
+ strlen(CMD_WORK_TREE
), 1);
141 } else if (!strcmp(cmd
, "--debugfs-dir")) {
143 fprintf(stderr
, "No directory given for --debugfs-dir.\n");
144 usage(perf_usage_string
);
146 strncpy(debugfs_mntpt
, (*argv
)[1], MAXPATHLEN
);
147 debugfs_mntpt
[MAXPATHLEN
- 1] = '\0';
152 } else if (!prefixcmp(cmd
, CMD_DEBUGFS_DIR
)) {
153 strncpy(debugfs_mntpt
, cmd
+ strlen(CMD_DEBUGFS_DIR
), MAXPATHLEN
);
154 debugfs_mntpt
[MAXPATHLEN
- 1] = '\0';
158 fprintf(stderr
, "Unknown option: %s\n", cmd
);
159 usage(perf_usage_string
);
169 static int handle_alias(int *argcp
, const char ***argv
)
171 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
172 int count
, option_count
;
173 const char **new_argv
;
174 const char *alias_command
;
177 alias_command
= (*argv
)[0];
178 alias_string
= alias_lookup(alias_command
);
180 if (alias_string
[0] == '!') {
184 strbuf_init(&buf
, PATH_MAX
);
185 strbuf_addstr(&buf
, alias_string
);
186 sq_quote_argv(&buf
, (*argv
) + 1, PATH_MAX
);
188 alias_string
= buf
.buf
;
190 ret
= system(alias_string
+ 1);
191 if (ret
>= 0 && WIFEXITED(ret
) &&
192 WEXITSTATUS(ret
) != 127)
193 exit(WEXITSTATUS(ret
));
194 die("Failed to run '%s' when expanding alias '%s'",
195 alias_string
+ 1, alias_command
);
197 count
= split_cmdline(alias_string
, &new_argv
);
199 die("Bad alias.%s string", alias_command
);
200 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
202 die("alias '%s' changes environment variables\n"
203 "You can use '!perf' in the alias to do this.",
205 memmove(new_argv
- option_count
, new_argv
,
206 count
* sizeof(char *));
207 new_argv
-= option_count
;
210 die("empty alias for %s", alias_command
);
212 if (!strcmp(alias_command
, new_argv
[0]))
213 die("recursive alias: %s", alias_command
);
215 new_argv
= realloc(new_argv
, sizeof(char *) *
216 (count
+ *argcp
+ 1));
217 /* insert after command name */
218 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char *) * *argcp
);
219 new_argv
[count
+ *argcp
] = NULL
;
232 const char perf_version_string
[] = PERF_VERSION
;
234 #define RUN_SETUP (1<<0)
235 #define USE_PAGER (1<<1)
237 * require working tree to be present -- anything uses this needs
238 * RUN_SETUP for reading from the configuration file.
240 #define NEED_WORK_TREE (1<<2)
244 int (*fn
)(int, const char **, const char *);
248 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
255 if (p
->option
& RUN_SETUP
)
256 prefix
= NULL
; /* setup_perf_directory(); */
258 if (use_pager
== -1 && p
->option
& RUN_SETUP
)
259 use_pager
= check_pager_config(p
->cmd
);
260 if (use_pager
== -1 && p
->option
& USE_PAGER
)
262 commit_pager_choice();
265 status
= p
->fn(argc
, argv
, prefix
);
266 exit_browser(status
);
269 return status
& 0xff;
271 /* Somebody closed stdout? */
272 if (fstat(fileno(stdout
), &st
))
274 /* Ignore write errors for pipes and sockets.. */
275 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
278 /* Check for ENOSPC and EIO errors.. */
280 die("write failure on standard output: %s", strerror(errno
));
282 die("unknown write failure on standard output");
284 die("close failed on standard output: %s", strerror(errno
));
288 static void handle_internal_command(int argc
, const char **argv
)
290 const char *cmd
= argv
[0];
291 static struct cmd_struct commands
[] = {
292 { "buildid-cache", cmd_buildid_cache
, 0 },
293 { "buildid-list", cmd_buildid_list
, 0 },
294 { "diff", cmd_diff
, 0 },
295 { "help", cmd_help
, 0 },
296 { "list", cmd_list
, 0 },
297 { "record", cmd_record
, 0 },
298 { "report", cmd_report
, 0 },
299 { "bench", cmd_bench
, 0 },
300 { "stat", cmd_stat
, 0 },
301 { "timechart", cmd_timechart
, 0 },
302 { "top", cmd_top
, 0 },
303 { "annotate", cmd_annotate
, 0 },
304 { "version", cmd_version
, 0 },
305 { "trace", cmd_trace
, 0 },
306 { "sched", cmd_sched
, 0 },
307 { "probe", cmd_probe
, 0 },
308 { "kmem", cmd_kmem
, 0 },
309 { "lock", cmd_lock
, 0 },
310 { "kvm", cmd_kvm
, 0 },
311 { "test", cmd_test
, 0 },
312 { "inject", cmd_inject
, 0 },
315 static const char ext
[] = STRIP_EXTENSION
;
317 if (sizeof(ext
) > 1) {
318 i
= strlen(argv
[0]) - strlen(ext
);
319 if (i
> 0 && !strcmp(argv
[0] + i
, ext
)) {
320 char *argv0
= strdup(argv
[0]);
321 argv
[0] = cmd
= argv0
;
326 /* Turn "perf cmd --help" into "perf help cmd" */
327 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
329 argv
[0] = cmd
= "help";
332 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
333 struct cmd_struct
*p
= commands
+i
;
334 if (strcmp(p
->cmd
, cmd
))
336 exit(run_builtin(p
, argc
, argv
));
340 static void execv_dashed_external(const char **argv
)
342 struct strbuf cmd
= STRBUF_INIT
;
346 strbuf_addf(&cmd
, "perf-%s", argv
[0]);
349 * argv[0] must be the perf command, but the argv array
350 * belongs to the caller, and may be reused in
351 * subsequent loop iterations. Save argv[0] and
352 * restore it on error.
358 * if we fail because the command is not found, it is
359 * OK to return. Otherwise, we just pass along the status code.
361 status
= run_command_v_opt(argv
, 0);
362 if (status
!= -ERR_RUN_COMMAND_EXEC
) {
363 if (IS_RUN_COMMAND_ERR(status
))
364 die("unable to run '%s'", argv
[0]);
367 errno
= ENOENT
; /* as if we called execvp */
371 strbuf_release(&cmd
);
374 static int run_argv(int *argcp
, const char ***argv
)
379 /* See if it's an internal command */
380 handle_internal_command(*argcp
, *argv
);
382 /* .. then try the external ones */
383 execv_dashed_external(*argv
);
385 /* It could be an alias -- this works around the insanity
386 * of overriding "perf log" with "perf show" by having
389 if (done_alias
|| !handle_alias(argcp
, argv
))
397 /* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
398 static void get_debugfs_mntpt(void)
400 const char *path
= debugfs_mount(NULL
);
403 strncpy(debugfs_mntpt
, path
, sizeof(debugfs_mntpt
));
405 debugfs_mntpt
[0] = '\0';
408 int main(int argc
, const char **argv
)
412 cmd
= perf_extract_argv0_path(argv
[0]);
415 /* get debugfs mount point from /proc/mounts */
418 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
420 * - cannot take flags in between the "perf" and the "xxxx".
421 * - cannot execute it externally (since it would just do
422 * the same thing over again)
424 * So we just directly call the internal command handler, and
425 * die if that one cannot handle it.
427 if (!prefixcmp(cmd
, "perf-")) {
430 handle_internal_command(argc
, argv
);
431 die("cannot handle %s internally", cmd
);
434 /* Look for flags.. */
437 handle_options(&argv
, &argc
, NULL
);
438 commit_pager_choice();
441 if (!prefixcmp(argv
[0], "--"))
444 /* The user didn't specify a command; give them help */
445 printf("\n usage: %s\n\n", perf_usage_string
);
446 list_common_cmds_help();
447 printf("\n %s\n\n", perf_more_info_string
);
453 * We use PATH to find perf commands, but we prepend some higher
454 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
455 * environment, and the $(perfexecdir) from the Makefile at build
461 static int done_help
;
462 static int was_alias
;
464 was_alias
= run_argv(&argc
, &argv
);
469 fprintf(stderr
, "Expansion of alias '%s' failed; "
470 "'%s' is not a perf-command\n",
475 cmd
= argv
[0] = help_unknown_cmd(cmd
);
481 fprintf(stderr
, "Failed to run command '%s': %s\n",
482 cmd
, strerror(errno
));