5 #include "run-command.h"
7 const char perf_usage_string[] =
8 "perf [--version] [--exec-path[=PERF_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--perf-dir=PERF_DIR] [--work-tree=PERF_WORK_TREE] [--help] COMMAND [ARGS]";
10 const char perf_more_info_string[] =
11 "See 'perf help COMMAND' for more information on a specific command.";
13 static int use_pager = -1;
19 static int pager_command_config(const char *var, const char *value, void *data)
21 struct pager_config *c = data;
22 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
23 c->val = perf_config_bool(var, value);
27 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
28 int check_pager_config(const char *cmd)
30 struct pager_config c;
33 perf_config(pager_command_config, &c);
37 static void commit_pager_choice(void) {
40 setenv("PERF_PAGER", "cat", 1);
50 static int handle_options(const char*** argv, int* argc, int* envchanged)
55 const char *cmd = (*argv)[0];
60 * For legacy reasons, the "version" and "help"
61 * commands can be written with "--" prepended
62 * to make them look like flags.
64 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
68 * Check remaining flags.
70 if (!prefixcmp(cmd, "--exec-path")) {
73 perf_set_argv_exec_path(cmd + 1);
75 puts(perf_exec_path());
78 } else if (!strcmp(cmd, "--html-path")) {
79 puts(system_path(PERF_HTML_PATH));
81 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
83 } else if (!strcmp(cmd, "--no-pager")) {
87 } else if (!strcmp(cmd, "--perf-dir")) {
89 fprintf(stderr, "No directory given for --perf-dir.\n" );
90 usage(perf_usage_string);
92 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
98 } else if (!prefixcmp(cmd, "--perf-dir=")) {
99 setenv(PERF_DIR_ENVIRONMENT, cmd + 10, 1);
102 } else if (!strcmp(cmd, "--work-tree")) {
104 fprintf(stderr, "No directory given for --work-tree.\n" );
105 usage(perf_usage_string);
107 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
112 } else if (!prefixcmp(cmd, "--work-tree=")) {
113 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
117 fprintf(stderr, "Unknown option: %s\n", cmd);
118 usage(perf_usage_string);
128 static int handle_alias(int *argcp, const char ***argv)
130 int envchanged = 0, ret = 0, saved_errno = errno;
131 int count, option_count;
132 const char** new_argv;
133 const char *alias_command;
136 alias_command = (*argv)[0];
137 alias_string = alias_lookup(alias_command);
139 if (alias_string[0] == '!') {
143 strbuf_init(&buf, PATH_MAX);
144 strbuf_addstr(&buf, alias_string);
145 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
147 alias_string = buf.buf;
149 ret = system(alias_string + 1);
150 if (ret >= 0 && WIFEXITED(ret) &&
151 WEXITSTATUS(ret) != 127)
152 exit(WEXITSTATUS(ret));
153 die("Failed to run '%s' when expanding alias '%s'",
154 alias_string + 1, alias_command);
156 count = split_cmdline(alias_string, &new_argv);
158 die("Bad alias.%s string", alias_command);
159 option_count = handle_options(&new_argv, &count, &envchanged);
161 die("alias '%s' changes environment variables\n"
162 "You can use '!perf' in the alias to do this.",
164 memmove(new_argv - option_count, new_argv,
165 count * sizeof(char *));
166 new_argv -= option_count;
169 die("empty alias for %s", alias_command);
171 if (!strcmp(alias_command, new_argv[0]))
172 die("recursive alias: %s", alias_command);
174 new_argv = realloc(new_argv, sizeof(char*) *
175 (count + *argcp + 1));
176 /* insert after command name */
177 memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);
178 new_argv[count+*argcp] = NULL;
191 const char perf_version_string[] = PERF_VERSION;
193 #define RUN_SETUP (1<<0)
194 #define USE_PAGER (1<<1)
196 * require working tree to be present -- anything uses this needs
197 * RUN_SETUP for reading from the configuration file.
199 #define NEED_WORK_TREE (1<<2)
203 int (*fn)(int, const char **, const char *);
207 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
214 if (p->option & RUN_SETUP)
215 prefix = NULL; /* setup_perf_directory(); */
217 if (use_pager == -1 && p->option & RUN_SETUP)
218 use_pager = check_pager_config(p->cmd);
219 if (use_pager == -1 && p->option & USE_PAGER)
221 commit_pager_choice();
223 if (p->option & NEED_WORK_TREE)
224 /* setup_work_tree() */;
226 status = p->fn(argc, argv, prefix);
228 return status & 0xff;
230 /* Somebody closed stdout? */
231 if (fstat(fileno(stdout), &st))
233 /* Ignore write errors for pipes and sockets.. */
234 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
237 /* Check for ENOSPC and EIO errors.. */
239 die("write failure on standard output: %s", strerror(errno));
241 die("unknown write failure on standard output");
243 die("close failed on standard output: %s", strerror(errno));
247 static void handle_internal_command(int argc, const char **argv)
249 const char *cmd = argv[0];
250 static struct cmd_struct commands[] = {
251 { "top", cmd_top, 0 },
254 static const char ext[] = STRIP_EXTENSION;
256 if (sizeof(ext) > 1) {
257 i = strlen(argv[0]) - strlen(ext);
258 if (i > 0 && !strcmp(argv[0] + i, ext)) {
259 char *argv0 = strdup(argv[0]);
260 argv[0] = cmd = argv0;
265 /* Turn "perf cmd --help" into "perf help cmd" */
266 if (argc > 1 && !strcmp(argv[1], "--help")) {
268 argv[0] = cmd = "help";
271 for (i = 0; i < ARRAY_SIZE(commands); i++) {
272 struct cmd_struct *p = commands+i;
273 if (strcmp(p->cmd, cmd))
275 exit(run_builtin(p, argc, argv));
279 static void execv_dashed_external(const char **argv)
281 struct strbuf cmd = STRBUF_INIT;
285 strbuf_addf(&cmd, "perf-%s", argv[0]);
288 * argv[0] must be the perf command, but the argv array
289 * belongs to the caller, and may be reused in
290 * subsequent loop iterations. Save argv[0] and
291 * restore it on error.
297 * if we fail because the command is not found, it is
298 * OK to return. Otherwise, we just pass along the status code.
300 status = run_command_v_opt(argv, 0);
301 if (status != -ERR_RUN_COMMAND_EXEC) {
302 if (IS_RUN_COMMAND_ERR(status))
303 die("unable to run '%s'", argv[0]);
306 errno = ENOENT; /* as if we called execvp */
310 strbuf_release(&cmd);
313 static int run_argv(int *argcp, const char ***argv)
318 /* See if it's an internal command */
319 handle_internal_command(*argcp, *argv);
321 /* .. then try the external ones */
322 execv_dashed_external(*argv);
324 /* It could be an alias -- this works around the insanity
325 * of overriding "perf log" with "perf show" by having
328 if (done_alias || !handle_alias(argcp, argv))
337 int main(int argc, const char **argv)
341 cmd = perf_extract_argv0_path(argv[0]);
346 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
348 * - cannot take flags in between the "perf" and the "xxxx".
349 * - cannot execute it externally (since it would just do
350 * the same thing over again)
352 * So we just directly call the internal command handler, and
353 * die if that one cannot handle it.
355 if (!prefixcmp(cmd, "perf-")) {
358 handle_internal_command(argc, argv);
359 die("cannot handle %s internally", cmd);
362 /* Look for flags.. */
365 handle_options(&argv, &argc, NULL);
366 commit_pager_choice();
368 if (!prefixcmp(argv[0], "--"))
371 /* The user didn't specify a command; give them help */
372 printf("usage: %s\n\n", perf_usage_string);
373 list_common_cmds_help();
374 printf("\n%s\n", perf_more_info_string);
380 * We use PATH to find perf commands, but we prepend some higher
381 * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
382 * environment, and the $(perfexecdir) from the Makefile at build
388 static int done_help = 0;
389 static int was_alias = 0;
390 was_alias = run_argv(&argc, &argv);
394 fprintf(stderr, "Expansion of alias '%s' failed; "
395 "'%s' is not a perf-command\n",
400 cmd = argv[0] = help_unknown_cmd(cmd);
406 fprintf(stderr, "Failed to run command '%s': %s\n",
407 cmd, strerror(errno));