blob: f90ca5ec83f3674fcde7b1aedd5b490a9907646d [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"
Ingo Molnar07800602009-04-20 15:00:56 +020017
18const char perf_usage_string[] =
Ingo Molnarcc13a592009-04-20 16:01:30 +020019 "perf [--version] [--help] COMMAND [ARGS]";
Ingo Molnar07800602009-04-20 15:00:56 +020020
21const char perf_more_info_string[] =
22 "See 'perf help COMMAND' for more information on a specific command.";
23
24static int use_pager = -1;
25struct pager_config {
26 const char *cmd;
27 int val;
28};
29
Jason Baron5beeded2009-07-21 14:16:29 -040030static char debugfs_mntpt[MAXPATHLEN];
31
Ingo Molnar07800602009-04-20 15:00:56 +020032static int pager_command_config(const char *var, const char *value, void *data)
33{
34 struct pager_config *c = data;
35 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
36 c->val = perf_config_bool(var, value);
37 return 0;
38}
39
40/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
41int check_pager_config(const char *cmd)
42{
43 struct pager_config c;
44 c.cmd = cmd;
45 c.val = -1;
46 perf_config(pager_command_config, &c);
47 return c.val;
48}
49
50static void commit_pager_choice(void) {
51 switch (use_pager) {
52 case 0:
53 setenv("PERF_PAGER", "cat", 1);
54 break;
55 case 1:
56 /* setup_pager(); */
57 break;
58 default:
59 break;
60 }
61}
62
Jason Baron5beeded2009-07-21 14:16:29 -040063static void set_debugfs_path(void)
64{
65 char *path;
66
67 path = getenv(PERF_DEBUGFS_ENVIRONMENT);
68 snprintf(debugfs_path, MAXPATHLEN, "%s/%s", path ?: debugfs_mntpt,
69 "tracing/events");
70}
71
Ingo Molnar07800602009-04-20 15:00:56 +020072static int handle_options(const char*** argv, int* argc, int* envchanged)
73{
74 int handled = 0;
75
76 while (*argc > 0) {
77 const char *cmd = (*argv)[0];
78 if (cmd[0] != '-')
79 break;
80
81 /*
82 * For legacy reasons, the "version" and "help"
83 * commands can be written with "--" prepended
84 * to make them look like flags.
85 */
86 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
87 break;
88
89 /*
90 * Check remaining flags.
91 */
Vincent Legollcfed95a2009-10-13 10:18:16 +020092 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
93 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +020094 if (*cmd == '=')
95 perf_set_argv_exec_path(cmd + 1);
96 else {
97 puts(perf_exec_path());
98 exit(0);
99 }
100 } else if (!strcmp(cmd, "--html-path")) {
101 puts(system_path(PERF_HTML_PATH));
102 exit(0);
103 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
104 use_pager = 1;
105 } else if (!strcmp(cmd, "--no-pager")) {
106 use_pager = 0;
107 if (envchanged)
108 *envchanged = 1;
109 } else if (!strcmp(cmd, "--perf-dir")) {
110 if (*argc < 2) {
111 fprintf(stderr, "No directory given for --perf-dir.\n" );
112 usage(perf_usage_string);
113 }
114 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
115 if (envchanged)
116 *envchanged = 1;
117 (*argv)++;
118 (*argc)--;
119 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200120 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
121 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200122 if (envchanged)
123 *envchanged = 1;
124 } else if (!strcmp(cmd, "--work-tree")) {
125 if (*argc < 2) {
126 fprintf(stderr, "No directory given for --work-tree.\n" );
127 usage(perf_usage_string);
128 }
129 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
130 if (envchanged)
131 *envchanged = 1;
132 (*argv)++;
133 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200134 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
135 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200136 if (envchanged)
137 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400138 } else if (!strcmp(cmd, "--debugfs-dir")) {
139 if (*argc < 2) {
140 fprintf(stderr, "No directory given for --debugfs-dir.\n");
141 usage(perf_usage_string);
142 }
143 strncpy(debugfs_mntpt, (*argv)[1], MAXPATHLEN);
144 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
145 if (envchanged)
146 *envchanged = 1;
147 (*argv)++;
148 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200149 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
150 strncpy(debugfs_mntpt, cmd + strlen(CMD_DEBUGFS_DIR), MAXPATHLEN);
Jason Baron5beeded2009-07-21 14:16:29 -0400151 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
152 if (envchanged)
153 *envchanged = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200154 } else {
155 fprintf(stderr, "Unknown option: %s\n", cmd);
156 usage(perf_usage_string);
157 }
158
159 (*argv)++;
160 (*argc)--;
161 handled++;
162 }
163 return handled;
164}
165
166static int handle_alias(int *argcp, const char ***argv)
167{
168 int envchanged = 0, ret = 0, saved_errno = errno;
169 int count, option_count;
170 const char** new_argv;
171 const char *alias_command;
172 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200173
174 alias_command = (*argv)[0];
175 alias_string = alias_lookup(alias_command);
176 if (alias_string) {
177 if (alias_string[0] == '!') {
178 if (*argcp > 1) {
179 struct strbuf buf;
180
181 strbuf_init(&buf, PATH_MAX);
182 strbuf_addstr(&buf, alias_string);
183 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
184 free(alias_string);
185 alias_string = buf.buf;
186 }
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);
193 }
194 count = split_cmdline(alias_string, &new_argv);
195 if (count < 0)
196 die("Bad alias.%s string", alias_command);
197 option_count = handle_options(&new_argv, &count, &envchanged);
198 if (envchanged)
199 die("alias '%s' changes environment variables\n"
200 "You can use '!perf' in the alias to do this.",
201 alias_command);
202 memmove(new_argv - option_count, new_argv,
203 count * sizeof(char *));
204 new_argv -= option_count;
205
206 if (count < 1)
207 die("empty alias for %s", alias_command);
208
209 if (!strcmp(alias_command, new_argv[0]))
210 die("recursive alias: %s", alias_command);
211
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;
217
218 *argv = new_argv;
219 *argcp += count - 1;
220
221 ret = 1;
222 }
223
224 errno = saved_errno;
225
226 return ret;
227}
228
229const char perf_version_string[] = PERF_VERSION;
230
231#define RUN_SETUP (1<<0)
232#define USE_PAGER (1<<1)
233/*
234 * require working tree to be present -- anything uses this needs
235 * RUN_SETUP for reading from the configuration file.
236 */
237#define NEED_WORK_TREE (1<<2)
238
239struct cmd_struct {
240 const char *cmd;
241 int (*fn)(int, const char **, const char *);
242 int option;
243};
244
245static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
246{
247 int status;
248 struct stat st;
249 const char *prefix;
250
251 prefix = NULL;
252 if (p->option & RUN_SETUP)
253 prefix = NULL; /* setup_perf_directory(); */
254
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)
258 use_pager = 1;
259 commit_pager_choice();
Jason Baron5beeded2009-07-21 14:16:29 -0400260 set_debugfs_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200261
Ingo Molnar07800602009-04-20 15:00:56 +0200262 status = p->fn(argc, argv, prefix);
263 if (status)
264 return status & 0xff;
265
266 /* Somebody closed stdout? */
267 if (fstat(fileno(stdout), &st))
268 return 0;
269 /* Ignore write errors for pipes and sockets.. */
270 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
271 return 0;
272
273 /* Check for ENOSPC and EIO errors.. */
274 if (fflush(stdout))
275 die("write failure on standard output: %s", strerror(errno));
276 if (ferror(stdout))
277 die("unknown write failure on standard output");
278 if (fclose(stdout))
279 die("close failed on standard output: %s", strerror(errno));
280 return 0;
281}
282
283static void handle_internal_command(int argc, const char **argv)
284{
285 const char *cmd = argv[0];
286 static struct cmd_struct commands[] = {
Ingo Molnar6142fdd2009-04-20 16:05:55 +0200287 { "help", cmd_help, 0 },
Thomas Gleixner86847b62009-06-06 12:24:17 +0200288 { "list", cmd_list, 0 },
Ingo Molnare33e0a42009-04-20 15:58:01 +0200289 { "record", cmd_record, 0 },
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300290 { "report", cmd_report, 0 },
Hitoshi Mitakedcba8842009-11-05 09:31:36 +0900291 { "bench", cmd_bench, 0 },
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200292 { "stat", cmd_stat, 0 },
Arjan van de Ven10274982009-09-12 07:53:05 +0200293 { "timechart", cmd_timechart, 0 },
Ingo Molnare33e0a42009-04-20 15:58:01 +0200294 { "top", cmd_top, 0 },
Ingo Molnar8035e422009-06-06 15:19:13 +0200295 { "annotate", cmd_annotate, 0 },
Ingo Molnarcc13a592009-04-20 16:01:30 +0200296 { "version", cmd_version, 0 },
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200297 { "trace", cmd_trace, 0 },
Ingo Molnar0a02ad92009-09-11 12:12:54 +0200298 { "sched", cmd_sched, 0 },
Ingo Molnar07800602009-04-20 15:00:56 +0200299 };
Ingo Molnarf37a2912009-07-01 12:37:06 +0200300 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200301 static const char ext[] = STRIP_EXTENSION;
302
303 if (sizeof(ext) > 1) {
304 i = strlen(argv[0]) - strlen(ext);
305 if (i > 0 && !strcmp(argv[0] + i, ext)) {
306 char *argv0 = strdup(argv[0]);
307 argv[0] = cmd = argv0;
308 argv0[i] = '\0';
309 }
310 }
311
312 /* Turn "perf cmd --help" into "perf help cmd" */
313 if (argc > 1 && !strcmp(argv[1], "--help")) {
314 argv[1] = argv[0];
315 argv[0] = cmd = "help";
316 }
317
318 for (i = 0; i < ARRAY_SIZE(commands); i++) {
319 struct cmd_struct *p = commands+i;
320 if (strcmp(p->cmd, cmd))
321 continue;
322 exit(run_builtin(p, argc, argv));
323 }
324}
325
326static void execv_dashed_external(const char **argv)
327{
328 struct strbuf cmd = STRBUF_INIT;
329 const char *tmp;
330 int status;
331
332 strbuf_addf(&cmd, "perf-%s", argv[0]);
333
334 /*
335 * argv[0] must be the perf command, but the argv array
336 * belongs to the caller, and may be reused in
337 * subsequent loop iterations. Save argv[0] and
338 * restore it on error.
339 */
340 tmp = argv[0];
341 argv[0] = cmd.buf;
342
343 /*
344 * if we fail because the command is not found, it is
345 * OK to return. Otherwise, we just pass along the status code.
346 */
347 status = run_command_v_opt(argv, 0);
348 if (status != -ERR_RUN_COMMAND_EXEC) {
349 if (IS_RUN_COMMAND_ERR(status))
350 die("unable to run '%s'", argv[0]);
351 exit(-status);
352 }
353 errno = ENOENT; /* as if we called execvp */
354
355 argv[0] = tmp;
356
357 strbuf_release(&cmd);
358}
359
360static int run_argv(int *argcp, const char ***argv)
361{
362 int done_alias = 0;
363
364 while (1) {
365 /* See if it's an internal command */
366 handle_internal_command(*argcp, *argv);
367
368 /* .. then try the external ones */
369 execv_dashed_external(*argv);
370
371 /* It could be an alias -- this works around the insanity
372 * of overriding "perf log" with "perf show" by having
373 * alias.log = show
374 */
375 if (done_alias || !handle_alias(argcp, argv))
376 break;
377 done_alias = 1;
378 }
379
380 return done_alias;
381}
382
Jason Baron5beeded2009-07-21 14:16:29 -0400383/* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
384static void get_debugfs_mntpt(void)
385{
386 FILE *file;
387 char fs_type[100];
388 char debugfs[MAXPATHLEN];
389
390 /*
391 * try the standard location
392 */
393 if (valid_debugfs_mount("/sys/kernel/debug/") == 0) {
394 strcpy(debugfs_mntpt, "/sys/kernel/debug/");
395 return;
396 }
397
398 /*
399 * try the sane location
400 */
401 if (valid_debugfs_mount("/debug/") == 0) {
402 strcpy(debugfs_mntpt, "/debug/");
403 return;
404 }
405
406 /*
407 * give up and parse /proc/mounts
408 */
409 file = fopen("/proc/mounts", "r");
410 if (file == NULL)
411 return;
412
413 while (fscanf(file, "%*s %"
414 STR(MAXPATHLEN)
415 "s %99s %*s %*d %*d\n",
416 debugfs, fs_type) == 2) {
417 if (strcmp(fs_type, "debugfs") == 0)
418 break;
419 }
420 fclose(file);
421 if (strcmp(fs_type, "debugfs") == 0) {
422 strncpy(debugfs_mntpt, debugfs, MAXPATHLEN);
423 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
424 }
425}
Ingo Molnar07800602009-04-20 15:00:56 +0200426
427int main(int argc, const char **argv)
428{
429 const char *cmd;
430
431 cmd = perf_extract_argv0_path(argv[0]);
432 if (!cmd)
433 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400434 /* get debugfs mount point from /proc/mounts */
435 get_debugfs_mntpt();
Ingo Molnar07800602009-04-20 15:00:56 +0200436 /*
437 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
438 *
439 * - cannot take flags in between the "perf" and the "xxxx".
440 * - cannot execute it externally (since it would just do
441 * the same thing over again)
442 *
443 * So we just directly call the internal command handler, and
444 * die if that one cannot handle it.
445 */
446 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200447 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200448 argv[0] = cmd;
449 handle_internal_command(argc, argv);
450 die("cannot handle %s internally", cmd);
451 }
452
453 /* Look for flags.. */
454 argv++;
455 argc--;
456 handle_options(&argv, &argc, NULL);
457 commit_pager_choice();
Jason Baron5beeded2009-07-21 14:16:29 -0400458 set_debugfs_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200459 if (argc > 0) {
460 if (!prefixcmp(argv[0], "--"))
461 argv[0] += 2;
462 } else {
463 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100464 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200465 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100466 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200467 exit(1);
468 }
469 cmd = argv[0];
470
471 /*
472 * We use PATH to find perf commands, but we prepend some higher
473 * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
474 * environment, and the $(perfexecdir) from the Makefile at build
475 * time.
476 */
477 setup_path();
478
479 while (1) {
480 static int done_help = 0;
481 static int was_alias = 0;
Ingo Molnar8035e422009-06-06 15:19:13 +0200482
Ingo Molnar07800602009-04-20 15:00:56 +0200483 was_alias = run_argv(&argc, &argv);
484 if (errno != ENOENT)
485 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200486
Ingo Molnar07800602009-04-20 15:00:56 +0200487 if (was_alias) {
488 fprintf(stderr, "Expansion of alias '%s' failed; "
489 "'%s' is not a perf-command\n",
490 cmd, argv[0]);
491 exit(1);
492 }
493 if (!done_help) {
494 cmd = argv[0] = help_unknown_cmd(cmd);
495 done_help = 1;
496 } else
497 break;
498 }
499
500 fprintf(stderr, "Failed to run command '%s': %s\n",
501 cmd, strerror(errno));
502
503 return 1;
504}