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"
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.";
25 static int use_pager
= -1;
31 static char debugfs_mntpt
[MAXPATHLEN
];
33 static int pager_command_config(const char *var
, const char *value
, void *data
)
35 struct pager_config
*c
= data
;
36 if (!prefixcmp(var
, "pager.") && !strcmp(var
+ 6, c
->cmd
))
37 c
->val
= perf_config_bool(var
, value
);
41 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
42 int check_pager_config(const char *cmd
)
44 struct pager_config c
;
47 perf_config(pager_command_config
, &c
);
51 static void commit_pager_choice(void) {
54 setenv("PERF_PAGER", "cat", 1);
64 static void set_debugfs_path(void)
68 path
= getenv(PERF_DEBUGFS_ENVIRONMENT
);
69 snprintf(debugfs_path
, MAXPATHLEN
, "%s/%s", path
?: debugfs_mntpt
,
73 static int handle_options(const char*** argv
, int* argc
, int* envchanged
)
78 const char *cmd
= (*argv
)[0];
83 * For legacy reasons, the "version" and "help"
84 * commands can be written with "--" prepended
85 * to make them look like flags.
87 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
91 * Check remaining flags.
93 if (!prefixcmp(cmd
, CMD_EXEC_PATH
)) {
94 cmd
+= strlen(CMD_EXEC_PATH
);
96 perf_set_argv_exec_path(cmd
+ 1);
98 puts(perf_exec_path());
101 } else if (!strcmp(cmd
, "--html-path")) {
102 puts(system_path(PERF_HTML_PATH
));
104 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
106 } else if (!strcmp(cmd
, "--no-pager")) {
110 } else if (!strcmp(cmd
, "--perf-dir")) {
112 fprintf(stderr
, "No directory given for --perf-dir.\n" );
113 usage(perf_usage_string
);
115 setenv(PERF_DIR_ENVIRONMENT
, (*argv
)[1], 1);
121 } else if (!prefixcmp(cmd
, CMD_PERF_DIR
)) {
122 setenv(PERF_DIR_ENVIRONMENT
, cmd
+ strlen(CMD_PERF_DIR
), 1);
125 } else if (!strcmp(cmd
, "--work-tree")) {
127 fprintf(stderr
, "No directory given for --work-tree.\n" );
128 usage(perf_usage_string
);
130 setenv(PERF_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
135 } else if (!prefixcmp(cmd
, CMD_WORK_TREE
)) {
136 setenv(PERF_WORK_TREE_ENVIRONMENT
, cmd
+ strlen(CMD_WORK_TREE
), 1);
139 } else if (!strcmp(cmd
, "--debugfs-dir")) {
141 fprintf(stderr
, "No directory given for --debugfs-dir.\n");
142 usage(perf_usage_string
);
144 strncpy(debugfs_mntpt
, (*argv
)[1], MAXPATHLEN
);
145 debugfs_mntpt
[MAXPATHLEN
- 1] = '\0';
150 } else if (!prefixcmp(cmd
, CMD_DEBUGFS_DIR
)) {
151 strncpy(debugfs_mntpt
, cmd
+ strlen(CMD_DEBUGFS_DIR
), MAXPATHLEN
);
152 debugfs_mntpt
[MAXPATHLEN
- 1] = '\0';
156 fprintf(stderr
, "Unknown option: %s\n", cmd
);
157 usage(perf_usage_string
);
167 static int handle_alias(int *argcp
, const char ***argv
)
169 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
170 int count
, option_count
;
171 const char** new_argv
;
172 const char *alias_command
;
175 alias_command
= (*argv
)[0];
176 alias_string
= alias_lookup(alias_command
);
178 if (alias_string
[0] == '!') {
182 strbuf_init(&buf
, PATH_MAX
);
183 strbuf_addstr(&buf
, alias_string
);
184 sq_quote_argv(&buf
, (*argv
) + 1, PATH_MAX
);
186 alias_string
= buf
.buf
;
188 ret
= system(alias_string
+ 1);
189 if (ret
>= 0 && WIFEXITED(ret
) &&
190 WEXITSTATUS(ret
) != 127)
191 exit(WEXITSTATUS(ret
));
192 die("Failed to run '%s' when expanding alias '%s'",
193 alias_string
+ 1, alias_command
);
195 count
= split_cmdline(alias_string
, &new_argv
);
197 die("Bad alias.%s string", alias_command
);
198 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
200 die("alias '%s' changes environment variables\n"
201 "You can use '!perf' in the alias to do this.",
203 memmove(new_argv
- option_count
, new_argv
,
204 count
* sizeof(char *));
205 new_argv
-= option_count
;
208 die("empty alias for %s", alias_command
);
210 if (!strcmp(alias_command
, new_argv
[0]))
211 die("recursive alias: %s", alias_command
);
213 new_argv
= realloc(new_argv
, sizeof(char*) *
214 (count
+ *argcp
+ 1));
215 /* insert after command name */
216 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char*) * *argcp
);
217 new_argv
[count
+*argcp
] = NULL
;
230 const char perf_version_string
[] = PERF_VERSION
;
232 #define RUN_SETUP (1<<0)
233 #define USE_PAGER (1<<1)
235 * require working tree to be present -- anything uses this needs
236 * RUN_SETUP for reading from the configuration file.
238 #define NEED_WORK_TREE (1<<2)
242 int (*fn
)(int, const char **, const char *);
246 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
253 if (p
->option
& RUN_SETUP
)
254 prefix
= NULL
; /* setup_perf_directory(); */
256 if (use_pager
== -1 && p
->option
& RUN_SETUP
)
257 use_pager
= check_pager_config(p
->cmd
);
258 if (use_pager
== -1 && p
->option
& USE_PAGER
)
260 commit_pager_choice();
263 status
= p
->fn(argc
, argv
, prefix
);
265 return status
& 0xff;
267 /* Somebody closed stdout? */
268 if (fstat(fileno(stdout
), &st
))
270 /* Ignore write errors for pipes and sockets.. */
271 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
274 /* Check for ENOSPC and EIO errors.. */
276 die("write failure on standard output: %s", strerror(errno
));
278 die("unknown write failure on standard output");
280 die("close failed on standard output: %s", strerror(errno
));
284 static void handle_internal_command(int argc
, const char **argv
)
286 const char *cmd
= argv
[0];
287 static struct cmd_struct commands
[] = {
288 { "buildid-list", cmd_buildid_list
, 0 },
289 { "diff", cmd_diff
, 0 },
290 { "help", cmd_help
, 0 },
291 { "list", cmd_list
, 0 },
292 { "record", cmd_record
, 0 },
293 { "report", cmd_report
, 0 },
294 { "bench", cmd_bench
, 0 },
295 { "stat", cmd_stat
, 0 },
296 { "timechart", cmd_timechart
, 0 },
297 { "top", cmd_top
, 0 },
298 { "annotate", cmd_annotate
, 0 },
299 { "version", cmd_version
, 0 },
300 { "trace", cmd_trace
, 0 },
301 { "sched", cmd_sched
, 0 },
302 { "probe", cmd_probe
, 0 },
303 { "kmem", cmd_kmem
, 0 },
306 static const char ext
[] = STRIP_EXTENSION
;
308 if (sizeof(ext
) > 1) {
309 i
= strlen(argv
[0]) - strlen(ext
);
310 if (i
> 0 && !strcmp(argv
[0] + i
, ext
)) {
311 char *argv0
= strdup(argv
[0]);
312 argv
[0] = cmd
= argv0
;
317 /* Turn "perf cmd --help" into "perf help cmd" */
318 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
320 argv
[0] = cmd
= "help";
323 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
324 struct cmd_struct
*p
= commands
+i
;
325 if (strcmp(p
->cmd
, cmd
))
327 exit(run_builtin(p
, argc
, argv
));
331 static void execv_dashed_external(const char **argv
)
333 struct strbuf cmd
= STRBUF_INIT
;
337 strbuf_addf(&cmd
, "perf-%s", argv
[0]);
340 * argv[0] must be the perf command, but the argv array
341 * belongs to the caller, and may be reused in
342 * subsequent loop iterations. Save argv[0] and
343 * restore it on error.
349 * if we fail because the command is not found, it is
350 * OK to return. Otherwise, we just pass along the status code.
352 status
= run_command_v_opt(argv
, 0);
353 if (status
!= -ERR_RUN_COMMAND_EXEC
) {
354 if (IS_RUN_COMMAND_ERR(status
))
355 die("unable to run '%s'", argv
[0]);
358 errno
= ENOENT
; /* as if we called execvp */
362 strbuf_release(&cmd
);
365 static int run_argv(int *argcp
, const char ***argv
)
370 /* See if it's an internal command */
371 handle_internal_command(*argcp
, *argv
);
373 /* .. then try the external ones */
374 execv_dashed_external(*argv
);
376 /* It could be an alias -- this works around the insanity
377 * of overriding "perf log" with "perf show" by having
380 if (done_alias
|| !handle_alias(argcp
, argv
))
388 /* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
389 static void get_debugfs_mntpt(void)
391 const char *path
= debugfs_find_mountpoint();
394 strncpy(debugfs_mntpt
, path
, sizeof(debugfs_mntpt
));
396 debugfs_mntpt
[0] = '\0';
399 int main(int argc
, const char **argv
)
403 cmd
= perf_extract_argv0_path(argv
[0]);
406 /* get debugfs mount point from /proc/mounts */
409 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
411 * - cannot take flags in between the "perf" and the "xxxx".
412 * - cannot execute it externally (since it would just do
413 * the same thing over again)
415 * So we just directly call the internal command handler, and
416 * die if that one cannot handle it.
418 if (!prefixcmp(cmd
, "perf-")) {
421 handle_internal_command(argc
, argv
);
422 die("cannot handle %s internally", cmd
);
425 /* Look for flags.. */
428 handle_options(&argv
, &argc
, NULL
);
429 commit_pager_choice();
432 if (!prefixcmp(argv
[0], "--"))
435 /* The user didn't specify a command; give them help */
436 printf("\n usage: %s\n\n", perf_usage_string
);
437 list_common_cmds_help();
438 printf("\n %s\n\n", perf_more_info_string
);
444 * We use PATH to find perf commands, but we prepend some higher
445 * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
446 * environment, and the $(perfexecdir) from the Makefile at build
452 static int done_help
= 0;
453 static int was_alias
= 0;
455 was_alias
= run_argv(&argc
, &argv
);
460 fprintf(stderr
, "Expansion of alias '%s' failed; "
461 "'%s' is not a perf-command\n",
466 cmd
= argv
[0] = help_unknown_cmd(cmd
);
472 fprintf(stderr
, "Failed to run command '%s': %s\n",
473 cmd
, strerror(errno
));