blob: 89b82acac7d9d9800f63088a530029e95c1e601b [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"
Jason Baron5beeded2009-07-21 14:16:29 -040015#include "util/parse-events.h"
16#include "util/string.h"
Clark Williams549104f2009-11-08 09:03:07 -060017#include "util/debugfs.h"
Ingo Molnar07800602009-04-20 15:00:56 +020018
19const char perf_usage_string[] =
Ingo Molnarcc13a592009-04-20 16:01:30 +020020 "perf [--version] [--help] COMMAND [ARGS]";
Ingo Molnar07800602009-04-20 15:00:56 +020021
22const char perf_more_info_string[] =
23 "See 'perf help COMMAND' for more information on a specific command.";
24
25static int use_pager = -1;
26struct pager_config {
27 const char *cmd;
28 int val;
29};
30
Jason Baron5beeded2009-07-21 14:16:29 -040031static char debugfs_mntpt[MAXPATHLEN];
32
Ingo Molnar07800602009-04-20 15:00:56 +020033static int pager_command_config(const char *var, const char *value, void *data)
34{
35 struct pager_config *c = data;
36 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
37 c->val = perf_config_bool(var, value);
38 return 0;
39}
40
41/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
42int check_pager_config(const char *cmd)
43{
44 struct pager_config c;
45 c.cmd = cmd;
46 c.val = -1;
47 perf_config(pager_command_config, &c);
48 return c.val;
49}
50
51static void commit_pager_choice(void) {
52 switch (use_pager) {
53 case 0:
54 setenv("PERF_PAGER", "cat", 1);
55 break;
56 case 1:
57 /* setup_pager(); */
58 break;
59 default:
60 break;
61 }
62}
63
Jason Baron5beeded2009-07-21 14:16:29 -040064static void set_debugfs_path(void)
65{
66 char *path;
67
68 path = getenv(PERF_DEBUGFS_ENVIRONMENT);
69 snprintf(debugfs_path, MAXPATHLEN, "%s/%s", path ?: debugfs_mntpt,
70 "tracing/events");
71}
72
Ingo Molnar07800602009-04-20 15:00:56 +020073static int handle_options(const char*** argv, int* argc, int* envchanged)
74{
75 int handled = 0;
76
77 while (*argc > 0) {
78 const char *cmd = (*argv)[0];
79 if (cmd[0] != '-')
80 break;
81
82 /*
83 * For legacy reasons, the "version" and "help"
84 * commands can be written with "--" prepended
85 * to make them look like flags.
86 */
87 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
88 break;
89
90 /*
91 * Check remaining flags.
92 */
Vincent Legollcfed95a2009-10-13 10:18:16 +020093 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
94 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +020095 if (*cmd == '=')
96 perf_set_argv_exec_path(cmd + 1);
97 else {
98 puts(perf_exec_path());
99 exit(0);
100 }
101 } else if (!strcmp(cmd, "--html-path")) {
102 puts(system_path(PERF_HTML_PATH));
103 exit(0);
104 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
105 use_pager = 1;
106 } else if (!strcmp(cmd, "--no-pager")) {
107 use_pager = 0;
108 if (envchanged)
109 *envchanged = 1;
110 } else if (!strcmp(cmd, "--perf-dir")) {
111 if (*argc < 2) {
112 fprintf(stderr, "No directory given for --perf-dir.\n" );
113 usage(perf_usage_string);
114 }
115 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
116 if (envchanged)
117 *envchanged = 1;
118 (*argv)++;
119 (*argc)--;
120 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200121 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
122 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200123 if (envchanged)
124 *envchanged = 1;
125 } else if (!strcmp(cmd, "--work-tree")) {
126 if (*argc < 2) {
127 fprintf(stderr, "No directory given for --work-tree.\n" );
128 usage(perf_usage_string);
129 }
130 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
131 if (envchanged)
132 *envchanged = 1;
133 (*argv)++;
134 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200135 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
136 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200137 if (envchanged)
138 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400139 } else if (!strcmp(cmd, "--debugfs-dir")) {
140 if (*argc < 2) {
141 fprintf(stderr, "No directory given for --debugfs-dir.\n");
142 usage(perf_usage_string);
143 }
144 strncpy(debugfs_mntpt, (*argv)[1], MAXPATHLEN);
145 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
146 if (envchanged)
147 *envchanged = 1;
148 (*argv)++;
149 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200150 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
151 strncpy(debugfs_mntpt, cmd + strlen(CMD_DEBUGFS_DIR), MAXPATHLEN);
Jason Baron5beeded2009-07-21 14:16:29 -0400152 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
153 if (envchanged)
154 *envchanged = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200155 } else {
156 fprintf(stderr, "Unknown option: %s\n", cmd);
157 usage(perf_usage_string);
158 }
159
160 (*argv)++;
161 (*argc)--;
162 handled++;
163 }
164 return handled;
165}
166
167static int handle_alias(int *argcp, const char ***argv)
168{
169 int envchanged = 0, ret = 0, saved_errno = errno;
170 int count, option_count;
171 const char** new_argv;
172 const char *alias_command;
173 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200174
175 alias_command = (*argv)[0];
176 alias_string = alias_lookup(alias_command);
177 if (alias_string) {
178 if (alias_string[0] == '!') {
179 if (*argcp > 1) {
180 struct strbuf buf;
181
182 strbuf_init(&buf, PATH_MAX);
183 strbuf_addstr(&buf, alias_string);
184 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
185 free(alias_string);
186 alias_string = buf.buf;
187 }
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);
194 }
195 count = split_cmdline(alias_string, &new_argv);
196 if (count < 0)
197 die("Bad alias.%s string", alias_command);
198 option_count = handle_options(&new_argv, &count, &envchanged);
199 if (envchanged)
200 die("alias '%s' changes environment variables\n"
201 "You can use '!perf' in the alias to do this.",
202 alias_command);
203 memmove(new_argv - option_count, new_argv,
204 count * sizeof(char *));
205 new_argv -= option_count;
206
207 if (count < 1)
208 die("empty alias for %s", alias_command);
209
210 if (!strcmp(alias_command, new_argv[0]))
211 die("recursive alias: %s", alias_command);
212
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;
218
219 *argv = new_argv;
220 *argcp += count - 1;
221
222 ret = 1;
223 }
224
225 errno = saved_errno;
226
227 return ret;
228}
229
230const char perf_version_string[] = PERF_VERSION;
231
232#define RUN_SETUP (1<<0)
233#define USE_PAGER (1<<1)
234/*
235 * require working tree to be present -- anything uses this needs
236 * RUN_SETUP for reading from the configuration file.
237 */
238#define NEED_WORK_TREE (1<<2)
239
240struct cmd_struct {
241 const char *cmd;
242 int (*fn)(int, const char **, const char *);
243 int option;
244};
245
246static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
247{
248 int status;
249 struct stat st;
250 const char *prefix;
251
252 prefix = NULL;
253 if (p->option & RUN_SETUP)
254 prefix = NULL; /* setup_perf_directory(); */
255
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)
259 use_pager = 1;
260 commit_pager_choice();
Jason Baron5beeded2009-07-21 14:16:29 -0400261 set_debugfs_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200262
Ingo Molnar07800602009-04-20 15:00:56 +0200263 status = p->fn(argc, argv, prefix);
264 if (status)
265 return status & 0xff;
266
267 /* Somebody closed stdout? */
268 if (fstat(fileno(stdout), &st))
269 return 0;
270 /* Ignore write errors for pipes and sockets.. */
271 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
272 return 0;
273
274 /* Check for ENOSPC and EIO errors.. */
275 if (fflush(stdout))
276 die("write failure on standard output: %s", strerror(errno));
277 if (ferror(stdout))
278 die("unknown write failure on standard output");
279 if (fclose(stdout))
280 die("close failed on standard output: %s", strerror(errno));
281 return 0;
282}
283
284static void handle_internal_command(int argc, const char **argv)
285{
286 const char *cmd = argv[0];
287 static struct cmd_struct commands[] = {
Ingo Molnar6142fdd2009-04-20 16:05:55 +0200288 { "help", cmd_help, 0 },
Thomas Gleixner86847b62009-06-06 12:24:17 +0200289 { "list", cmd_list, 0 },
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -0200290 { "buildid-list", cmd_buildid_list, 0 },
Ingo Molnare33e0a42009-04-20 15:58:01 +0200291 { "record", cmd_record, 0 },
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300292 { "report", cmd_report, 0 },
Hitoshi Mitakedcba8842009-11-05 09:31:36 +0900293 { "bench", cmd_bench, 0 },
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200294 { "stat", cmd_stat, 0 },
Arjan van de Ven10274982009-09-12 07:53:05 +0200295 { "timechart", cmd_timechart, 0 },
Ingo Molnare33e0a42009-04-20 15:58:01 +0200296 { "top", cmd_top, 0 },
Ingo Molnar8035e422009-06-06 15:19:13 +0200297 { "annotate", cmd_annotate, 0 },
Ingo Molnarcc13a592009-04-20 16:01:30 +0200298 { "version", cmd_version, 0 },
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200299 { "trace", cmd_trace, 0 },
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200300 { "sched", cmd_sched, 0 },
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400301 { "probe", cmd_probe, 0 },
Ingo Molnar07800602009-04-20 15:00:56 +0200302 };
Ingo Molnarf37a2912009-07-01 12:37:06 +0200303 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200304 static const char ext[] = STRIP_EXTENSION;
305
306 if (sizeof(ext) > 1) {
307 i = strlen(argv[0]) - strlen(ext);
308 if (i > 0 && !strcmp(argv[0] + i, ext)) {
309 char *argv0 = strdup(argv[0]);
310 argv[0] = cmd = argv0;
311 argv0[i] = '\0';
312 }
313 }
314
315 /* Turn "perf cmd --help" into "perf help cmd" */
316 if (argc > 1 && !strcmp(argv[1], "--help")) {
317 argv[1] = argv[0];
318 argv[0] = cmd = "help";
319 }
320
321 for (i = 0; i < ARRAY_SIZE(commands); i++) {
322 struct cmd_struct *p = commands+i;
323 if (strcmp(p->cmd, cmd))
324 continue;
325 exit(run_builtin(p, argc, argv));
326 }
327}
328
329static void execv_dashed_external(const char **argv)
330{
331 struct strbuf cmd = STRBUF_INIT;
332 const char *tmp;
333 int status;
334
335 strbuf_addf(&cmd, "perf-%s", argv[0]);
336
337 /*
338 * argv[0] must be the perf command, but the argv array
339 * belongs to the caller, and may be reused in
340 * subsequent loop iterations. Save argv[0] and
341 * restore it on error.
342 */
343 tmp = argv[0];
344 argv[0] = cmd.buf;
345
346 /*
347 * if we fail because the command is not found, it is
348 * OK to return. Otherwise, we just pass along the status code.
349 */
350 status = run_command_v_opt(argv, 0);
351 if (status != -ERR_RUN_COMMAND_EXEC) {
352 if (IS_RUN_COMMAND_ERR(status))
353 die("unable to run '%s'", argv[0]);
354 exit(-status);
355 }
356 errno = ENOENT; /* as if we called execvp */
357
358 argv[0] = tmp;
359
360 strbuf_release(&cmd);
361}
362
363static int run_argv(int *argcp, const char ***argv)
364{
365 int done_alias = 0;
366
367 while (1) {
368 /* See if it's an internal command */
369 handle_internal_command(*argcp, *argv);
370
371 /* .. then try the external ones */
372 execv_dashed_external(*argv);
373
374 /* It could be an alias -- this works around the insanity
375 * of overriding "perf log" with "perf show" by having
376 * alias.log = show
377 */
378 if (done_alias || !handle_alias(argcp, argv))
379 break;
380 done_alias = 1;
381 }
382
383 return done_alias;
384}
385
Jason Baron5beeded2009-07-21 14:16:29 -0400386/* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
387static void get_debugfs_mntpt(void)
388{
Clark Williams549104f2009-11-08 09:03:07 -0600389 const char *path = debugfs_find_mountpoint();
Jason Baron5beeded2009-07-21 14:16:29 -0400390
Clark Williams549104f2009-11-08 09:03:07 -0600391 if (path)
392 strncpy(debugfs_mntpt, path, sizeof(debugfs_mntpt));
393 else
394 debugfs_mntpt[0] = '\0';
Jason Baron5beeded2009-07-21 14:16:29 -0400395}
Ingo Molnar07800602009-04-20 15:00:56 +0200396
397int main(int argc, const char **argv)
398{
399 const char *cmd;
400
401 cmd = perf_extract_argv0_path(argv[0]);
402 if (!cmd)
403 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400404 /* get debugfs mount point from /proc/mounts */
405 get_debugfs_mntpt();
Ingo Molnar07800602009-04-20 15:00:56 +0200406 /*
407 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
408 *
409 * - cannot take flags in between the "perf" and the "xxxx".
410 * - cannot execute it externally (since it would just do
411 * the same thing over again)
412 *
413 * So we just directly call the internal command handler, and
414 * die if that one cannot handle it.
415 */
416 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200417 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200418 argv[0] = cmd;
419 handle_internal_command(argc, argv);
420 die("cannot handle %s internally", cmd);
421 }
422
423 /* Look for flags.. */
424 argv++;
425 argc--;
426 handle_options(&argv, &argc, NULL);
427 commit_pager_choice();
Jason Baron5beeded2009-07-21 14:16:29 -0400428 set_debugfs_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200429 if (argc > 0) {
430 if (!prefixcmp(argv[0], "--"))
431 argv[0] += 2;
432 } else {
433 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100434 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200435 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100436 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200437 exit(1);
438 }
439 cmd = argv[0];
440
441 /*
442 * We use PATH to find perf commands, but we prepend some higher
443 * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
444 * environment, and the $(perfexecdir) from the Makefile at build
445 * time.
446 */
447 setup_path();
448
449 while (1) {
450 static int done_help = 0;
451 static int was_alias = 0;
Ingo Molnar8035e422009-06-06 15:19:13 +0200452
Ingo Molnar07800602009-04-20 15:00:56 +0200453 was_alias = run_argv(&argc, &argv);
454 if (errno != ENOENT)
455 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200456
Ingo Molnar07800602009-04-20 15:00:56 +0200457 if (was_alias) {
458 fprintf(stderr, "Expansion of alias '%s' failed; "
459 "'%s' is not a perf-command\n",
460 cmd, argv[0]);
461 exit(1);
462 }
463 if (!done_help) {
464 cmd = argv[0] = help_unknown_cmd(cmd);
465 done_help = 1;
466 } else
467 break;
468 }
469
470 fprintf(stderr, "Failed to run command '%s': %s\n",
471 cmd, strerror(errno));
472
473 return 1;
474}