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"
16 const char perf_usage_string
[] =
17 "perf [--version] [--help] COMMAND [ARGS]";
19 const char perf_more_info_string
[] =
20 "See 'perf help COMMAND' for more information on a specific command.";
22 static int use_pager
= -1;
28 static int pager_command_config(const char *var
, const char *value
, void *data
)
30 struct pager_config
*c
= data
;
31 if (!prefixcmp(var
, "pager.") && !strcmp(var
+ 6, c
->cmd
))
32 c
->val
= perf_config_bool(var
, value
);
36 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
37 int check_pager_config(const char *cmd
)
39 struct pager_config c
;
42 perf_config(pager_command_config
, &c
);
46 static void commit_pager_choice(void) {
49 setenv("PERF_PAGER", "cat", 1);
59 static int handle_options(const char*** argv
, int* argc
, int* envchanged
)
64 const char *cmd
= (*argv
)[0];
69 * For legacy reasons, the "version" and "help"
70 * commands can be written with "--" prepended
71 * to make them look like flags.
73 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
77 * Check remaining flags.
79 if (!prefixcmp(cmd
, "--exec-path")) {
82 perf_set_argv_exec_path(cmd
+ 1);
84 puts(perf_exec_path());
87 } else if (!strcmp(cmd
, "--html-path")) {
88 puts(system_path(PERF_HTML_PATH
));
90 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
92 } else if (!strcmp(cmd
, "--no-pager")) {
96 } else if (!strcmp(cmd
, "--perf-dir")) {
98 fprintf(stderr
, "No directory given for --perf-dir.\n" );
99 usage(perf_usage_string
);
101 setenv(PERF_DIR_ENVIRONMENT
, (*argv
)[1], 1);
107 } else if (!prefixcmp(cmd
, "--perf-dir=")) {
108 setenv(PERF_DIR_ENVIRONMENT
, cmd
+ 10, 1);
111 } else if (!strcmp(cmd
, "--work-tree")) {
113 fprintf(stderr
, "No directory given for --work-tree.\n" );
114 usage(perf_usage_string
);
116 setenv(PERF_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
121 } else if (!prefixcmp(cmd
, "--work-tree=")) {
122 setenv(PERF_WORK_TREE_ENVIRONMENT
, cmd
+ 12, 1);
126 fprintf(stderr
, "Unknown option: %s\n", cmd
);
127 usage(perf_usage_string
);
137 static int handle_alias(int *argcp
, const char ***argv
)
139 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
140 int count
, option_count
;
141 const char** new_argv
;
142 const char *alias_command
;
145 alias_command
= (*argv
)[0];
146 alias_string
= alias_lookup(alias_command
);
148 if (alias_string
[0] == '!') {
152 strbuf_init(&buf
, PATH_MAX
);
153 strbuf_addstr(&buf
, alias_string
);
154 sq_quote_argv(&buf
, (*argv
) + 1, PATH_MAX
);
156 alias_string
= buf
.buf
;
158 ret
= system(alias_string
+ 1);
159 if (ret
>= 0 && WIFEXITED(ret
) &&
160 WEXITSTATUS(ret
) != 127)
161 exit(WEXITSTATUS(ret
));
162 die("Failed to run '%s' when expanding alias '%s'",
163 alias_string
+ 1, alias_command
);
165 count
= split_cmdline(alias_string
, &new_argv
);
167 die("Bad alias.%s string", alias_command
);
168 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
170 die("alias '%s' changes environment variables\n"
171 "You can use '!perf' in the alias to do this.",
173 memmove(new_argv
- option_count
, new_argv
,
174 count
* sizeof(char *));
175 new_argv
-= option_count
;
178 die("empty alias for %s", alias_command
);
180 if (!strcmp(alias_command
, new_argv
[0]))
181 die("recursive alias: %s", alias_command
);
183 new_argv
= realloc(new_argv
, sizeof(char*) *
184 (count
+ *argcp
+ 1));
185 /* insert after command name */
186 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char*) * *argcp
);
187 new_argv
[count
+*argcp
] = NULL
;
200 const char perf_version_string
[] = PERF_VERSION
;
202 #define RUN_SETUP (1<<0)
203 #define USE_PAGER (1<<1)
205 * require working tree to be present -- anything uses this needs
206 * RUN_SETUP for reading from the configuration file.
208 #define NEED_WORK_TREE (1<<2)
212 int (*fn
)(int, const char **, const char *);
216 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
223 if (p
->option
& RUN_SETUP
)
224 prefix
= NULL
; /* setup_perf_directory(); */
226 if (use_pager
== -1 && p
->option
& RUN_SETUP
)
227 use_pager
= check_pager_config(p
->cmd
);
228 if (use_pager
== -1 && p
->option
& USE_PAGER
)
230 commit_pager_choice();
232 status
= p
->fn(argc
, argv
, prefix
);
234 return status
& 0xff;
236 /* Somebody closed stdout? */
237 if (fstat(fileno(stdout
), &st
))
239 /* Ignore write errors for pipes and sockets.. */
240 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
243 /* Check for ENOSPC and EIO errors.. */
245 die("write failure on standard output: %s", strerror(errno
));
247 die("unknown write failure on standard output");
249 die("close failed on standard output: %s", strerror(errno
));
253 static void handle_internal_command(int argc
, const char **argv
)
255 const char *cmd
= argv
[0];
256 static struct cmd_struct commands
[] = {
257 { "help", cmd_help
, 0 },
258 { "list", cmd_list
, 0 },
259 { "record", cmd_record
, 0 },
260 { "report", cmd_report
, 0 },
261 { "stat", cmd_stat
, 0 },
262 { "top", cmd_top
, 0 },
263 { "annotate", cmd_annotate
, 0 },
264 { "version", cmd_version
, 0 },
267 static const char ext
[] = STRIP_EXTENSION
;
269 if (sizeof(ext
) > 1) {
270 i
= strlen(argv
[0]) - strlen(ext
);
271 if (i
> 0 && !strcmp(argv
[0] + i
, ext
)) {
272 char *argv0
= strdup(argv
[0]);
273 argv
[0] = cmd
= argv0
;
278 /* Turn "perf cmd --help" into "perf help cmd" */
279 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
281 argv
[0] = cmd
= "help";
284 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
285 struct cmd_struct
*p
= commands
+i
;
286 if (strcmp(p
->cmd
, cmd
))
288 exit(run_builtin(p
, argc
, argv
));
292 static void execv_dashed_external(const char **argv
)
294 struct strbuf cmd
= STRBUF_INIT
;
298 strbuf_addf(&cmd
, "perf-%s", argv
[0]);
301 * argv[0] must be the perf command, but the argv array
302 * belongs to the caller, and may be reused in
303 * subsequent loop iterations. Save argv[0] and
304 * restore it on error.
310 * if we fail because the command is not found, it is
311 * OK to return. Otherwise, we just pass along the status code.
313 status
= run_command_v_opt(argv
, 0);
314 if (status
!= -ERR_RUN_COMMAND_EXEC
) {
315 if (IS_RUN_COMMAND_ERR(status
))
316 die("unable to run '%s'", argv
[0]);
319 errno
= ENOENT
; /* as if we called execvp */
323 strbuf_release(&cmd
);
326 static int run_argv(int *argcp
, const char ***argv
)
331 /* See if it's an internal command */
332 handle_internal_command(*argcp
, *argv
);
334 /* .. then try the external ones */
335 execv_dashed_external(*argv
);
337 /* It could be an alias -- this works around the insanity
338 * of overriding "perf log" with "perf show" by having
341 if (done_alias
|| !handle_alias(argcp
, argv
))
350 int main(int argc
, const char **argv
)
354 cmd
= perf_extract_argv0_path(argv
[0]);
359 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
361 * - cannot take flags in between the "perf" and the "xxxx".
362 * - cannot execute it externally (since it would just do
363 * the same thing over again)
365 * So we just directly call the internal command handler, and
366 * die if that one cannot handle it.
368 if (!prefixcmp(cmd
, "perf-")) {
371 handle_internal_command(argc
, argv
);
372 die("cannot handle %s internally", cmd
);
375 /* Look for flags.. */
378 handle_options(&argv
, &argc
, NULL
);
379 commit_pager_choice();
381 if (!prefixcmp(argv
[0], "--"))
384 /* The user didn't specify a command; give them help */
385 printf("\n usage: %s\n\n", perf_usage_string
);
386 list_common_cmds_help();
387 printf("\n %s\n\n", perf_more_info_string
);
393 * We use PATH to find perf commands, but we prepend some higher
394 * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
395 * environment, and the $(perfexecdir) from the Makefile at build
401 static int done_help
= 0;
402 static int was_alias
= 0;
404 was_alias
= run_argv(&argc
, &argv
);
409 fprintf(stderr
, "Expansion of alias '%s' failed; "
410 "'%s' is not a perf-command\n",
415 cmd
= argv
[0] = help_unknown_cmd(cmd
);
421 fprintf(stderr
, "Failed to run command '%s': %s\n",
422 cmd
, strerror(errno
));