blob: fc2f770e3027e5f10939df7b17f3f805ce2f2107 [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"
Clark Williams549104f2009-11-08 09:03:07 -060016#include "util/debugfs.h"
Irina Tirdea27683dc2012-09-08 03:43:19 +030017#include <pthread.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
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030025int use_browser = -1;
Ingo Molnar07800602009-04-20 15:00:56 +020026static int use_pager = -1;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030027
Frederic Weisbecker98a41792012-08-09 16:31:51 +020028struct cmd_struct {
29 const char *cmd;
30 int (*fn)(int, const char **, const char *);
31 int option;
32};
33
34static struct cmd_struct commands[] = {
35 { "buildid-cache", cmd_buildid_cache, 0 },
36 { "buildid-list", cmd_buildid_list, 0 },
37 { "diff", cmd_diff, 0 },
38 { "evlist", cmd_evlist, 0 },
39 { "help", cmd_help, 0 },
40 { "list", cmd_list, 0 },
41 { "record", cmd_record, 0 },
42 { "report", cmd_report, 0 },
43 { "bench", cmd_bench, 0 },
44 { "stat", cmd_stat, 0 },
45 { "timechart", cmd_timechart, 0 },
46 { "top", cmd_top, 0 },
47 { "annotate", cmd_annotate, 0 },
48 { "version", cmd_version, 0 },
49 { "script", cmd_script, 0 },
50 { "sched", cmd_sched, 0 },
Namhyung Kim393be2e2012-08-06 13:41:21 +090051#ifndef NO_LIBELF_SUPPORT
Frederic Weisbecker98a41792012-08-09 16:31:51 +020052 { "probe", cmd_probe, 0 },
Namhyung Kim393be2e2012-08-06 13:41:21 +090053#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020054 { "kmem", cmd_kmem, 0 },
55 { "lock", cmd_lock, 0 },
56 { "kvm", cmd_kvm, 0 },
57 { "test", cmd_test, 0 },
Namhyung Kim4d290892012-09-27 20:23:38 +090058#ifndef NO_LIBAUDIT_SUPPORT
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030059 { "trace", cmd_trace, 0 },
Namhyung Kim4d290892012-09-27 20:23:38 +090060#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020061 { "inject", cmd_inject, 0 },
62};
63
Ingo Molnar07800602009-04-20 15:00:56 +020064struct pager_config {
65 const char *cmd;
66 int val;
67};
68
69static int pager_command_config(const char *var, const char *value, void *data)
70{
71 struct pager_config *c = data;
72 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
73 c->val = perf_config_bool(var, value);
74 return 0;
75}
76
77/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
78int check_pager_config(const char *cmd)
79{
80 struct pager_config c;
81 c.cmd = cmd;
82 c.val = -1;
83 perf_config(pager_command_config, &c);
84 return c.val;
85}
86
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030087static int tui_command_config(const char *var, const char *value, void *data)
88{
89 struct pager_config *c = data;
90 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
91 c->val = perf_config_bool(var, value);
92 return 0;
93}
94
95/* returns 0 for "no tui", 1 for "use tui", and -1 for "not specified" */
96static int check_tui_config(const char *cmd)
97{
98 struct pager_config c;
99 c.cmd = cmd;
100 c.val = -1;
101 perf_config(tui_command_config, &c);
102 return c.val;
103}
104
Thiago Farina4c574152010-01-27 21:05:55 -0200105static void commit_pager_choice(void)
106{
Ingo Molnar07800602009-04-20 15:00:56 +0200107 switch (use_pager) {
108 case 0:
109 setenv("PERF_PAGER", "cat", 1);
110 break;
111 case 1:
112 /* setup_pager(); */
113 break;
114 default:
115 break;
116 }
117}
118
Thiago Farina4c574152010-01-27 21:05:55 -0200119static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +0200120{
121 int handled = 0;
122
123 while (*argc > 0) {
124 const char *cmd = (*argv)[0];
125 if (cmd[0] != '-')
126 break;
127
128 /*
129 * For legacy reasons, the "version" and "help"
130 * commands can be written with "--" prepended
131 * to make them look like flags.
132 */
133 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
134 break;
135
136 /*
137 * Check remaining flags.
138 */
Vincent Legollcfed95a2009-10-13 10:18:16 +0200139 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
140 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +0200141 if (*cmd == '=')
142 perf_set_argv_exec_path(cmd + 1);
143 else {
144 puts(perf_exec_path());
145 exit(0);
146 }
147 } else if (!strcmp(cmd, "--html-path")) {
148 puts(system_path(PERF_HTML_PATH));
149 exit(0);
150 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
151 use_pager = 1;
152 } else if (!strcmp(cmd, "--no-pager")) {
153 use_pager = 0;
154 if (envchanged)
155 *envchanged = 1;
156 } else if (!strcmp(cmd, "--perf-dir")) {
157 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200158 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200159 usage(perf_usage_string);
160 }
161 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
162 if (envchanged)
163 *envchanged = 1;
164 (*argv)++;
165 (*argc)--;
166 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200167 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
168 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200169 if (envchanged)
170 *envchanged = 1;
171 } else if (!strcmp(cmd, "--work-tree")) {
172 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200173 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200174 usage(perf_usage_string);
175 }
176 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
177 if (envchanged)
178 *envchanged = 1;
179 (*argv)++;
180 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200181 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
182 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200183 if (envchanged)
184 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400185 } else if (!strcmp(cmd, "--debugfs-dir")) {
186 if (*argc < 2) {
187 fprintf(stderr, "No directory given for --debugfs-dir.\n");
188 usage(perf_usage_string);
189 }
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200190 debugfs_set_path((*argv)[1]);
Jason Baron5beeded2009-07-21 14:16:29 -0400191 if (envchanged)
192 *envchanged = 1;
193 (*argv)++;
194 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200195 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200196 debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
197 fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
Jason Baron5beeded2009-07-21 14:16:29 -0400198 if (envchanged)
199 *envchanged = 1;
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200200 } else if (!strcmp(cmd, "--list-cmds")) {
201 unsigned int i;
202
203 for (i = 0; i < ARRAY_SIZE(commands); i++) {
204 struct cmd_struct *p = commands+i;
205 printf("%s ", p->cmd);
206 }
207 exit(0);
Ingo Molnar07800602009-04-20 15:00:56 +0200208 } else {
209 fprintf(stderr, "Unknown option: %s\n", cmd);
210 usage(perf_usage_string);
211 }
212
213 (*argv)++;
214 (*argc)--;
215 handled++;
216 }
217 return handled;
218}
219
220static int handle_alias(int *argcp, const char ***argv)
221{
222 int envchanged = 0, ret = 0, saved_errno = errno;
223 int count, option_count;
Thiago Farina4c574152010-01-27 21:05:55 -0200224 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200225 const char *alias_command;
226 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200227
228 alias_command = (*argv)[0];
229 alias_string = alias_lookup(alias_command);
230 if (alias_string) {
231 if (alias_string[0] == '!') {
232 if (*argcp > 1) {
233 struct strbuf buf;
234
235 strbuf_init(&buf, PATH_MAX);
236 strbuf_addstr(&buf, alias_string);
237 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
238 free(alias_string);
239 alias_string = buf.buf;
240 }
241 ret = system(alias_string + 1);
242 if (ret >= 0 && WIFEXITED(ret) &&
243 WEXITSTATUS(ret) != 127)
244 exit(WEXITSTATUS(ret));
245 die("Failed to run '%s' when expanding alias '%s'",
246 alias_string + 1, alias_command);
247 }
248 count = split_cmdline(alias_string, &new_argv);
249 if (count < 0)
250 die("Bad alias.%s string", alias_command);
251 option_count = handle_options(&new_argv, &count, &envchanged);
252 if (envchanged)
253 die("alias '%s' changes environment variables\n"
254 "You can use '!perf' in the alias to do this.",
255 alias_command);
256 memmove(new_argv - option_count, new_argv,
257 count * sizeof(char *));
258 new_argv -= option_count;
259
260 if (count < 1)
261 die("empty alias for %s", alias_command);
262
263 if (!strcmp(alias_command, new_argv[0]))
264 die("recursive alias: %s", alias_command);
265
Thiago Farina4c574152010-01-27 21:05:55 -0200266 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200267 (count + *argcp + 1));
268 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200269 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
270 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200271
272 *argv = new_argv;
273 *argcp += count - 1;
274
275 ret = 1;
276 }
277
278 errno = saved_errno;
279
280 return ret;
281}
282
283const char perf_version_string[] = PERF_VERSION;
284
285#define RUN_SETUP (1<<0)
286#define USE_PAGER (1<<1)
287/*
288 * require working tree to be present -- anything uses this needs
289 * RUN_SETUP for reading from the configuration file.
290 */
291#define NEED_WORK_TREE (1<<2)
292
Ingo Molnar07800602009-04-20 15:00:56 +0200293static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
294{
295 int status;
296 struct stat st;
297 const char *prefix;
298
299 prefix = NULL;
300 if (p->option & RUN_SETUP)
301 prefix = NULL; /* setup_perf_directory(); */
302
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300303 if (use_browser == -1)
304 use_browser = check_tui_config(p->cmd);
305
Ingo Molnar07800602009-04-20 15:00:56 +0200306 if (use_pager == -1 && p->option & RUN_SETUP)
307 use_pager = check_pager_config(p->cmd);
308 if (use_pager == -1 && p->option & USE_PAGER)
309 use_pager = 1;
310 commit_pager_choice();
311
Ingo Molnar07800602009-04-20 15:00:56 +0200312 status = p->fn(argc, argv, prefix);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300313 exit_browser(status);
314
Ingo Molnar07800602009-04-20 15:00:56 +0200315 if (status)
316 return status & 0xff;
317
318 /* Somebody closed stdout? */
319 if (fstat(fileno(stdout), &st))
320 return 0;
321 /* Ignore write errors for pipes and sockets.. */
322 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
323 return 0;
324
325 /* Check for ENOSPC and EIO errors.. */
326 if (fflush(stdout))
327 die("write failure on standard output: %s", strerror(errno));
328 if (ferror(stdout))
329 die("unknown write failure on standard output");
330 if (fclose(stdout))
331 die("close failed on standard output: %s", strerror(errno));
332 return 0;
333}
334
335static void handle_internal_command(int argc, const char **argv)
336{
337 const char *cmd = argv[0];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200338 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200339 static const char ext[] = STRIP_EXTENSION;
340
341 if (sizeof(ext) > 1) {
342 i = strlen(argv[0]) - strlen(ext);
343 if (i > 0 && !strcmp(argv[0] + i, ext)) {
344 char *argv0 = strdup(argv[0]);
345 argv[0] = cmd = argv0;
346 argv0[i] = '\0';
347 }
348 }
349
350 /* Turn "perf cmd --help" into "perf help cmd" */
351 if (argc > 1 && !strcmp(argv[1], "--help")) {
352 argv[1] = argv[0];
353 argv[0] = cmd = "help";
354 }
355
356 for (i = 0; i < ARRAY_SIZE(commands); i++) {
357 struct cmd_struct *p = commands+i;
358 if (strcmp(p->cmd, cmd))
359 continue;
360 exit(run_builtin(p, argc, argv));
361 }
362}
363
364static void execv_dashed_external(const char **argv)
365{
366 struct strbuf cmd = STRBUF_INIT;
367 const char *tmp;
368 int status;
369
370 strbuf_addf(&cmd, "perf-%s", argv[0]);
371
372 /*
373 * argv[0] must be the perf command, but the argv array
374 * belongs to the caller, and may be reused in
375 * subsequent loop iterations. Save argv[0] and
376 * restore it on error.
377 */
378 tmp = argv[0];
379 argv[0] = cmd.buf;
380
381 /*
382 * if we fail because the command is not found, it is
383 * OK to return. Otherwise, we just pass along the status code.
384 */
385 status = run_command_v_opt(argv, 0);
386 if (status != -ERR_RUN_COMMAND_EXEC) {
387 if (IS_RUN_COMMAND_ERR(status))
388 die("unable to run '%s'", argv[0]);
389 exit(-status);
390 }
391 errno = ENOENT; /* as if we called execvp */
392
393 argv[0] = tmp;
394
395 strbuf_release(&cmd);
396}
397
398static int run_argv(int *argcp, const char ***argv)
399{
400 int done_alias = 0;
401
402 while (1) {
403 /* See if it's an internal command */
404 handle_internal_command(*argcp, *argv);
405
406 /* .. then try the external ones */
407 execv_dashed_external(*argv);
408
409 /* It could be an alias -- this works around the insanity
410 * of overriding "perf log" with "perf show" by having
411 * alias.log = show
412 */
413 if (done_alias || !handle_alias(argcp, argv))
414 break;
415 done_alias = 1;
416 }
417
418 return done_alias;
419}
420
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300421static void pthread__block_sigwinch(void)
422{
423 sigset_t set;
424
425 sigemptyset(&set);
426 sigaddset(&set, SIGWINCH);
427 pthread_sigmask(SIG_BLOCK, &set, NULL);
428}
429
430void pthread__unblock_sigwinch(void)
431{
432 sigset_t set;
433
434 sigemptyset(&set);
435 sigaddset(&set, SIGWINCH);
436 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
437}
438
Ingo Molnar07800602009-04-20 15:00:56 +0200439int main(int argc, const char **argv)
440{
441 const char *cmd;
442
443 cmd = perf_extract_argv0_path(argv[0]);
444 if (!cmd)
445 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400446 /* get debugfs mount point from /proc/mounts */
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200447 debugfs_mount(NULL);
Ingo Molnar07800602009-04-20 15:00:56 +0200448 /*
449 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
450 *
451 * - cannot take flags in between the "perf" and the "xxxx".
452 * - cannot execute it externally (since it would just do
453 * the same thing over again)
454 *
455 * So we just directly call the internal command handler, and
456 * die if that one cannot handle it.
457 */
458 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200459 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200460 argv[0] = cmd;
461 handle_internal_command(argc, argv);
462 die("cannot handle %s internally", cmd);
463 }
464
465 /* Look for flags.. */
466 argv++;
467 argc--;
468 handle_options(&argv, &argc, NULL);
469 commit_pager_choice();
Stephane Eranian45de34b2010-06-01 21:25:01 +0200470 set_buildid_dir();
471
Ingo Molnar07800602009-04-20 15:00:56 +0200472 if (argc > 0) {
473 if (!prefixcmp(argv[0], "--"))
474 argv[0] += 2;
475 } else {
476 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100477 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200478 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100479 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200480 exit(1);
481 }
482 cmd = argv[0];
483
484 /*
485 * We use PATH to find perf commands, but we prepend some higher
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100486 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
Ingo Molnar07800602009-04-20 15:00:56 +0200487 * environment, and the $(perfexecdir) from the Makefile at build
488 * time.
489 */
490 setup_path();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300491 /*
492 * Block SIGWINCH notifications so that the thread that wants it can
493 * unblock and get syscalls like select interrupted instead of waiting
494 * forever while the signal goes to some other non interested thread.
495 */
496 pthread__block_sigwinch();
Ingo Molnar07800602009-04-20 15:00:56 +0200497
498 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200499 static int done_help;
500 static int was_alias;
Ingo Molnar8035e422009-06-06 15:19:13 +0200501
Ingo Molnar07800602009-04-20 15:00:56 +0200502 was_alias = run_argv(&argc, &argv);
503 if (errno != ENOENT)
504 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200505
Ingo Molnar07800602009-04-20 15:00:56 +0200506 if (was_alias) {
507 fprintf(stderr, "Expansion of alias '%s' failed; "
508 "'%s' is not a perf-command\n",
509 cmd, argv[0]);
510 exit(1);
511 }
512 if (!done_help) {
513 cmd = argv[0] = help_unknown_cmd(cmd);
514 done_help = 1;
515 } else
516 break;
517 }
518
519 fprintf(stderr, "Failed to run command '%s': %s\n",
520 cmd, strerror(errno));
521
522 return 1;
523}