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"
18 const char perf_usage_string
[] =
19 "perf [--version] [--help] COMMAND [ARGS]";
21 const char perf_more_info_string
[] =
22 "See 'perf help COMMAND' for more information on a specific command.";
24 static int use_pager
= -1;
30 static char debugfs_mntpt
[MAXPATHLEN
];
32 static int pager_command_config(const char *var
, const char *value
, void *data
)
34 struct pager_config
*c
= data
;
35 if (!prefixcmp(var
, "pager.") && !strcmp(var
+ 6, c
->cmd
))
36 c
->val
= perf_config_bool(var
, value
);
40 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
41 int check_pager_config(const char *cmd
)
43 struct pager_config c
;
46 perf_config(pager_command_config
, &c
);
50 static void commit_pager_choice(void) {
53 setenv("PERF_PAGER", "cat", 1);
63 static void set_debugfs_path(void)
67 path
= getenv(PERF_DEBUGFS_ENVIRONMENT
);
68 snprintf(debugfs_path
, MAXPATHLEN
, "%s/%s", path
?: debugfs_mntpt
,
72 static int handle_options(const char*** argv
, int* argc
, int* envchanged
)
77 const char *cmd
= (*argv
)[0];
82 * For legacy reasons, the "version" and "help"
83 * commands can be written with "--" prepended
84 * to make them look like flags.
86 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
90 * Check remaining flags.
92 if (!prefixcmp(cmd
, "--exec-path")) {
95 perf_set_argv_exec_path(cmd
+ 1);
97 puts(perf_exec_path());
100 } else if (!strcmp(cmd
, "--html-path")) {
101 puts(system_path(PERF_HTML_PATH
));
103 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
105 } else if (!strcmp(cmd
, "--no-pager")) {
109 } else if (!strcmp(cmd
, "--perf-dir")) {
111 fprintf(stderr
, "No directory given for --perf-dir.\n" );
112 usage(perf_usage_string
);
114 setenv(PERF_DIR_ENVIRONMENT
, (*argv
)[1], 1);
120 } else if (!prefixcmp(cmd
, "--perf-dir=")) {
121 setenv(PERF_DIR_ENVIRONMENT
, cmd
+ 10, 1);
124 } else if (!strcmp(cmd
, "--work-tree")) {
126 fprintf(stderr
, "No directory given for --work-tree.\n" );
127 usage(perf_usage_string
);
129 setenv(PERF_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
134 } else if (!prefixcmp(cmd
, "--work-tree=")) {
135 setenv(PERF_WORK_TREE_ENVIRONMENT
, cmd
+ 12, 1);
138 } else if (!strcmp(cmd
, "--debugfs-dir")) {
140 fprintf(stderr
, "No directory given for --debugfs-dir.\n");
141 usage(perf_usage_string
);
143 strncpy(debugfs_mntpt
, (*argv
)[1], MAXPATHLEN
);
144 debugfs_mntpt
[MAXPATHLEN
- 1] = '\0';
149 } else if (!prefixcmp(cmd
, "--debugfs-dir=")) {
150 strncpy(debugfs_mntpt
, cmd
+ 14, MAXPATHLEN
);
151 debugfs_mntpt
[MAXPATHLEN
- 1] = '\0';
155 fprintf(stderr
, "Unknown option: %s\n", cmd
);
156 usage(perf_usage_string
);
166 static int handle_alias(int *argcp
, const char ***argv
)
168 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
169 int count
, option_count
;
170 const char** new_argv
;
171 const char *alias_command
;
174 alias_command
= (*argv
)[0];
175 alias_string
= alias_lookup(alias_command
);
177 if (alias_string
[0] == '!') {
181 strbuf_init(&buf
, PATH_MAX
);
182 strbuf_addstr(&buf
, alias_string
);
183 sq_quote_argv(&buf
, (*argv
) + 1, PATH_MAX
);
185 alias_string
= buf
.buf
;
187 ret
= system(alias_string
+ 1);
188 if (ret
>= 0 && WIFEXITED(ret
) &&
189 WEXITSTATUS(ret
) != 127)
190 exit(WEXITSTATUS(ret
));
191 die("Failed to run '%s' when expanding alias '%s'",
192 alias_string
+ 1, alias_command
);
194 count
= split_cmdline(alias_string
, &new_argv
);
196 die("Bad alias.%s string", alias_command
);
197 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
199 die("alias '%s' changes environment variables\n"
200 "You can use '!perf' in the alias to do this.",
202 memmove(new_argv
- option_count
, new_argv
,
203 count
* sizeof(char *));
204 new_argv
-= option_count
;
207 die("empty alias for %s", alias_command
);
209 if (!strcmp(alias_command
, new_argv
[0]))
210 die("recursive alias: %s", alias_command
);
212 new_argv
= realloc(new_argv
, sizeof(char*) *
213 (count
+ *argcp
+ 1));
214 /* insert after command name */
215 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char*) * *argcp
);
216 new_argv
[count
+*argcp
] = NULL
;
229 const char perf_version_string
[] = PERF_VERSION
;
231 #define RUN_SETUP (1<<0)
232 #define USE_PAGER (1<<1)
234 * require working tree to be present -- anything uses this needs
235 * RUN_SETUP for reading from the configuration file.
237 #define NEED_WORK_TREE (1<<2)
241 int (*fn
)(int, const char **, const char *);
245 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
252 if (p
->option
& RUN_SETUP
)
253 prefix
= NULL
; /* setup_perf_directory(); */
255 if (use_pager
== -1 && p
->option
& RUN_SETUP
)
256 use_pager
= check_pager_config(p
->cmd
);
257 if (use_pager
== -1 && p
->option
& USE_PAGER
)
259 commit_pager_choice();
262 status
= p
->fn(argc
, argv
, prefix
);
264 return status
& 0xff;
266 /* Somebody closed stdout? */
267 if (fstat(fileno(stdout
), &st
))
269 /* Ignore write errors for pipes and sockets.. */
270 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
273 /* Check for ENOSPC and EIO errors.. */
275 die("write failure on standard output: %s", strerror(errno
));
277 die("unknown write failure on standard output");
279 die("close failed on standard output: %s", strerror(errno
));
283 static void handle_internal_command(int argc
, const char **argv
)
285 const char *cmd
= argv
[0];
286 static struct cmd_struct commands
[] = {
287 { "help", cmd_help
, 0 },
288 { "list", cmd_list
, 0 },
289 { "record", cmd_record
, 0 },
290 { "report", cmd_report
, 0 },
291 { "stat", cmd_stat
, 0 },
292 { "top", cmd_top
, 0 },
293 { "annotate", cmd_annotate
, 0 },
294 { "version", cmd_version
, 0 },
297 static const char ext
[] = STRIP_EXTENSION
;
299 if (sizeof(ext
) > 1) {
300 i
= strlen(argv
[0]) - strlen(ext
);
301 if (i
> 0 && !strcmp(argv
[0] + i
, ext
)) {
302 char *argv0
= strdup(argv
[0]);
303 argv
[0] = cmd
= argv0
;
308 /* Turn "perf cmd --help" into "perf help cmd" */
309 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
311 argv
[0] = cmd
= "help";
314 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
315 struct cmd_struct
*p
= commands
+i
;
316 if (strcmp(p
->cmd
, cmd
))
318 exit(run_builtin(p
, argc
, argv
));
322 static void execv_dashed_external(const char **argv
)
324 struct strbuf cmd
= STRBUF_INIT
;
328 strbuf_addf(&cmd
, "perf-%s", argv
[0]);
331 * argv[0] must be the perf command, but the argv array
332 * belongs to the caller, and may be reused in
333 * subsequent loop iterations. Save argv[0] and
334 * restore it on error.
340 * if we fail because the command is not found, it is
341 * OK to return. Otherwise, we just pass along the status code.
343 status
= run_command_v_opt(argv
, 0);
344 if (status
!= -ERR_RUN_COMMAND_EXEC
) {
345 if (IS_RUN_COMMAND_ERR(status
))
346 die("unable to run '%s'", argv
[0]);
349 errno
= ENOENT
; /* as if we called execvp */
353 strbuf_release(&cmd
);
356 static int run_argv(int *argcp
, const char ***argv
)
361 /* See if it's an internal command */
362 handle_internal_command(*argcp
, *argv
);
364 /* .. then try the external ones */
365 execv_dashed_external(*argv
);
367 /* It could be an alias -- this works around the insanity
368 * of overriding "perf log" with "perf show" by having
371 if (done_alias
|| !handle_alias(argcp
, argv
))
379 /* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
380 static void get_debugfs_mntpt(void)
384 char debugfs
[MAXPATHLEN
];
387 * try the standard location
389 if (valid_debugfs_mount("/sys/kernel/debug/") == 0) {
390 strcpy(debugfs_mntpt
, "/sys/kernel/debug/");
395 * try the sane location
397 if (valid_debugfs_mount("/debug/") == 0) {
398 strcpy(debugfs_mntpt
, "/debug/");
403 * give up and parse /proc/mounts
405 file
= fopen("/proc/mounts", "r");
409 while (fscanf(file
, "%*s %"
411 "s %99s %*s %*d %*d\n",
412 debugfs
, fs_type
) == 2) {
413 if (strcmp(fs_type
, "debugfs") == 0)
417 if (strcmp(fs_type
, "debugfs") == 0) {
418 strncpy(debugfs_mntpt
, debugfs
, MAXPATHLEN
);
419 debugfs_mntpt
[MAXPATHLEN
- 1] = '\0';
423 int main(int argc
, const char **argv
)
427 cmd
= perf_extract_argv0_path(argv
[0]);
430 /* get debugfs mount point from /proc/mounts */
433 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
435 * - cannot take flags in between the "perf" and the "xxxx".
436 * - cannot execute it externally (since it would just do
437 * the same thing over again)
439 * So we just directly call the internal command handler, and
440 * die if that one cannot handle it.
442 if (!prefixcmp(cmd
, "perf-")) {
445 handle_internal_command(argc
, argv
);
446 die("cannot handle %s internally", cmd
);
449 /* Look for flags.. */
452 handle_options(&argv
, &argc
, NULL
);
453 commit_pager_choice();
456 if (!prefixcmp(argv
[0], "--"))
459 /* The user didn't specify a command; give them help */
460 printf("\n usage: %s\n\n", perf_usage_string
);
461 list_common_cmds_help();
462 printf("\n %s\n\n", perf_more_info_string
);
468 * We use PATH to find perf commands, but we prepend some higher
469 * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
470 * environment, and the $(perfexecdir) from the Makefile at build
476 static int done_help
= 0;
477 static int was_alias
= 0;
479 was_alias
= run_argv(&argc
, &argv
);
484 fprintf(stderr
, "Expansion of alias '%s' failed; "
485 "'%s' is not a perf-command\n",
490 cmd
= argv
[0] = help_unknown_cmd(cmd
);
496 fprintf(stderr
, "Failed to run command '%s': %s\n",
497 cmd
, strerror(errno
));