blob: ec7edb7fbe2982a3ccdd31b3ed92df31867c104f [file] [log] [blame]
Ingo Molnarbf9e1872009-06-02 23:37:05 +02001/*
2 * perf.c
3 *
4 * Performance analysis utility.
5 *
6 * This is the main hub from which the sub-commands (perf stat,
7 * perf top, perf record, perf report, etc.) are started.
8 */
Ingo Molnar07800602009-04-20 15:00:56 +02009#include "builtin.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020010
Ingo Molnar148be2c2009-04-27 08:02:14 +020011#include "util/exec_cmd.h"
12#include "util/cache.h"
13#include "util/quote.h"
14#include "util/run-command.h"
Ingo Molnar07800602009-04-20 15:00:56 +020015
16const char perf_usage_string[] =
Ingo Molnarcc13a592009-04-20 16:01:30 +020017 "perf [--version] [--help] COMMAND [ARGS]";
Ingo Molnar07800602009-04-20 15:00:56 +020018
19const char perf_more_info_string[] =
20 "See 'perf help COMMAND' for more information on a specific command.";
21
22static int use_pager = -1;
23struct pager_config {
24 const char *cmd;
25 int val;
26};
27
28static int pager_command_config(const char *var, const char *value, void *data)
29{
30 struct pager_config *c = data;
31 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
32 c->val = perf_config_bool(var, value);
33 return 0;
34}
35
36/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
37int check_pager_config(const char *cmd)
38{
39 struct pager_config c;
40 c.cmd = cmd;
41 c.val = -1;
42 perf_config(pager_command_config, &c);
43 return c.val;
44}
45
46static void commit_pager_choice(void) {
47 switch (use_pager) {
48 case 0:
49 setenv("PERF_PAGER", "cat", 1);
50 break;
51 case 1:
52 /* setup_pager(); */
53 break;
54 default:
55 break;
56 }
57}
58
59static int handle_options(const char*** argv, int* argc, int* envchanged)
60{
61 int handled = 0;
62
63 while (*argc > 0) {
64 const char *cmd = (*argv)[0];
65 if (cmd[0] != '-')
66 break;
67
68 /*
69 * For legacy reasons, the "version" and "help"
70 * commands can be written with "--" prepended
71 * to make them look like flags.
72 */
73 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
74 break;
75
76 /*
77 * Check remaining flags.
78 */
79 if (!prefixcmp(cmd, "--exec-path")) {
80 cmd += 11;
81 if (*cmd == '=')
82 perf_set_argv_exec_path(cmd + 1);
83 else {
84 puts(perf_exec_path());
85 exit(0);
86 }
87 } else if (!strcmp(cmd, "--html-path")) {
88 puts(system_path(PERF_HTML_PATH));
89 exit(0);
90 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
91 use_pager = 1;
92 } else if (!strcmp(cmd, "--no-pager")) {
93 use_pager = 0;
94 if (envchanged)
95 *envchanged = 1;
96 } else if (!strcmp(cmd, "--perf-dir")) {
97 if (*argc < 2) {
98 fprintf(stderr, "No directory given for --perf-dir.\n" );
99 usage(perf_usage_string);
100 }
101 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
102 if (envchanged)
103 *envchanged = 1;
104 (*argv)++;
105 (*argc)--;
106 handled++;
107 } else if (!prefixcmp(cmd, "--perf-dir=")) {
108 setenv(PERF_DIR_ENVIRONMENT, cmd + 10, 1);
109 if (envchanged)
110 *envchanged = 1;
111 } else if (!strcmp(cmd, "--work-tree")) {
112 if (*argc < 2) {
113 fprintf(stderr, "No directory given for --work-tree.\n" );
114 usage(perf_usage_string);
115 }
116 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
117 if (envchanged)
118 *envchanged = 1;
119 (*argv)++;
120 (*argc)--;
121 } else if (!prefixcmp(cmd, "--work-tree=")) {
122 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
123 if (envchanged)
124 *envchanged = 1;
125 } else {
126 fprintf(stderr, "Unknown option: %s\n", cmd);
127 usage(perf_usage_string);
128 }
129
130 (*argv)++;
131 (*argc)--;
132 handled++;
133 }
134 return handled;
135}
136
137static int handle_alias(int *argcp, const char ***argv)
138{
139 int envchanged = 0, ret = 0, saved_errno = errno;
140 int count, option_count;
141 const char** new_argv;
142 const char *alias_command;
143 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200144
145 alias_command = (*argv)[0];
146 alias_string = alias_lookup(alias_command);
147 if (alias_string) {
148 if (alias_string[0] == '!') {
149 if (*argcp > 1) {
150 struct strbuf buf;
151
152 strbuf_init(&buf, PATH_MAX);
153 strbuf_addstr(&buf, alias_string);
154 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
155 free(alias_string);
156 alias_string = buf.buf;
157 }
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);
164 }
165 count = split_cmdline(alias_string, &new_argv);
166 if (count < 0)
167 die("Bad alias.%s string", alias_command);
168 option_count = handle_options(&new_argv, &count, &envchanged);
169 if (envchanged)
170 die("alias '%s' changes environment variables\n"
171 "You can use '!perf' in the alias to do this.",
172 alias_command);
173 memmove(new_argv - option_count, new_argv,
174 count * sizeof(char *));
175 new_argv -= option_count;
176
177 if (count < 1)
178 die("empty alias for %s", alias_command);
179
180 if (!strcmp(alias_command, new_argv[0]))
181 die("recursive alias: %s", alias_command);
182
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;
188
189 *argv = new_argv;
190 *argcp += count - 1;
191
192 ret = 1;
193 }
194
195 errno = saved_errno;
196
197 return ret;
198}
199
200const char perf_version_string[] = PERF_VERSION;
201
202#define RUN_SETUP (1<<0)
203#define USE_PAGER (1<<1)
204/*
205 * require working tree to be present -- anything uses this needs
206 * RUN_SETUP for reading from the configuration file.
207 */
208#define NEED_WORK_TREE (1<<2)
209
210struct cmd_struct {
211 const char *cmd;
212 int (*fn)(int, const char **, const char *);
213 int option;
214};
215
216static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
217{
218 int status;
219 struct stat st;
220 const char *prefix;
221
222 prefix = NULL;
223 if (p->option & RUN_SETUP)
224 prefix = NULL; /* setup_perf_directory(); */
225
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)
229 use_pager = 1;
230 commit_pager_choice();
231
232 if (p->option & NEED_WORK_TREE)
233 /* setup_work_tree() */;
234
235 status = p->fn(argc, argv, prefix);
236 if (status)
237 return status & 0xff;
238
239 /* Somebody closed stdout? */
240 if (fstat(fileno(stdout), &st))
241 return 0;
242 /* Ignore write errors for pipes and sockets.. */
243 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
244 return 0;
245
246 /* Check for ENOSPC and EIO errors.. */
247 if (fflush(stdout))
248 die("write failure on standard output: %s", strerror(errno));
249 if (ferror(stdout))
250 die("unknown write failure on standard output");
251 if (fclose(stdout))
252 die("close failed on standard output: %s", strerror(errno));
253 return 0;
254}
255
256static void handle_internal_command(int argc, const char **argv)
257{
258 const char *cmd = argv[0];
259 static struct cmd_struct commands[] = {
Ingo Molnar6142fdd2009-04-20 16:05:55 +0200260 { "help", cmd_help, 0 },
Ingo Molnare33e0a42009-04-20 15:58:01 +0200261 { "record", cmd_record, 0 },
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300262 { "report", cmd_report, 0 },
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200263 { "stat", cmd_stat, 0 },
Ingo Molnare33e0a42009-04-20 15:58:01 +0200264 { "top", cmd_top, 0 },
Ingo Molnarcc13a592009-04-20 16:01:30 +0200265 { "version", cmd_version, 0 },
Ingo Molnar07800602009-04-20 15:00:56 +0200266 };
267 int i;
268 static const char ext[] = STRIP_EXTENSION;
269
270 if (sizeof(ext) > 1) {
271 i = strlen(argv[0]) - strlen(ext);
272 if (i > 0 && !strcmp(argv[0] + i, ext)) {
273 char *argv0 = strdup(argv[0]);
274 argv[0] = cmd = argv0;
275 argv0[i] = '\0';
276 }
277 }
278
279 /* Turn "perf cmd --help" into "perf help cmd" */
280 if (argc > 1 && !strcmp(argv[1], "--help")) {
281 argv[1] = argv[0];
282 argv[0] = cmd = "help";
283 }
284
285 for (i = 0; i < ARRAY_SIZE(commands); i++) {
286 struct cmd_struct *p = commands+i;
287 if (strcmp(p->cmd, cmd))
288 continue;
289 exit(run_builtin(p, argc, argv));
290 }
291}
292
293static void execv_dashed_external(const char **argv)
294{
295 struct strbuf cmd = STRBUF_INIT;
296 const char *tmp;
297 int status;
298
299 strbuf_addf(&cmd, "perf-%s", argv[0]);
300
301 /*
302 * argv[0] must be the perf command, but the argv array
303 * belongs to the caller, and may be reused in
304 * subsequent loop iterations. Save argv[0] and
305 * restore it on error.
306 */
307 tmp = argv[0];
308 argv[0] = cmd.buf;
309
310 /*
311 * if we fail because the command is not found, it is
312 * OK to return. Otherwise, we just pass along the status code.
313 */
314 status = run_command_v_opt(argv, 0);
315 if (status != -ERR_RUN_COMMAND_EXEC) {
316 if (IS_RUN_COMMAND_ERR(status))
317 die("unable to run '%s'", argv[0]);
318 exit(-status);
319 }
320 errno = ENOENT; /* as if we called execvp */
321
322 argv[0] = tmp;
323
324 strbuf_release(&cmd);
325}
326
327static int run_argv(int *argcp, const char ***argv)
328{
329 int done_alias = 0;
330
331 while (1) {
332 /* See if it's an internal command */
333 handle_internal_command(*argcp, *argv);
334
335 /* .. then try the external ones */
336 execv_dashed_external(*argv);
337
338 /* It could be an alias -- this works around the insanity
339 * of overriding "perf log" with "perf show" by having
340 * alias.log = show
341 */
342 if (done_alias || !handle_alias(argcp, argv))
343 break;
344 done_alias = 1;
345 }
346
347 return done_alias;
348}
349
350
351int main(int argc, const char **argv)
352{
353 const char *cmd;
354
355 cmd = perf_extract_argv0_path(argv[0]);
356 if (!cmd)
357 cmd = "perf-help";
358
359 /*
360 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
361 *
362 * - cannot take flags in between the "perf" and the "xxxx".
363 * - cannot execute it externally (since it would just do
364 * the same thing over again)
365 *
366 * So we just directly call the internal command handler, and
367 * die if that one cannot handle it.
368 */
369 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200370 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200371 argv[0] = cmd;
372 handle_internal_command(argc, argv);
373 die("cannot handle %s internally", cmd);
374 }
375
376 /* Look for flags.. */
377 argv++;
378 argc--;
379 handle_options(&argv, &argc, NULL);
380 commit_pager_choice();
381 if (argc > 0) {
382 if (!prefixcmp(argv[0], "--"))
383 argv[0] += 2;
384 } else {
385 /* The user didn't specify a command; give them help */
386 printf("usage: %s\n\n", perf_usage_string);
387 list_common_cmds_help();
388 printf("\n%s\n", perf_more_info_string);
389 exit(1);
390 }
391 cmd = argv[0];
392
393 /*
394 * We use PATH to find perf commands, but we prepend some higher
395 * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
396 * environment, and the $(perfexecdir) from the Makefile at build
397 * time.
398 */
399 setup_path();
400
401 while (1) {
402 static int done_help = 0;
403 static int was_alias = 0;
404 was_alias = run_argv(&argc, &argv);
405 if (errno != ENOENT)
406 break;
407 if (was_alias) {
408 fprintf(stderr, "Expansion of alias '%s' failed; "
409 "'%s' is not a perf-command\n",
410 cmd, argv[0]);
411 exit(1);
412 }
413 if (!done_help) {
414 cmd = argv[0] = help_unknown_cmd(cmd);
415 done_help = 1;
416 } else
417 break;
418 }
419
420 fprintf(stderr, "Failed to run command '%s': %s\n",
421 cmd, strerror(errno));
422
423 return 1;
424}