blob: e9683738d89f04e3e0446bca1e5e185893da236f [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;
Feng Tang70cb4e92012-10-30 11:56:02 +080027const char *input_name;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030028
Frederic Weisbecker98a41792012-08-09 16:31:51 +020029struct cmd_struct {
30 const char *cmd;
31 int (*fn)(int, const char **, const char *);
32 int option;
33};
34
35static struct cmd_struct commands[] = {
36 { "buildid-cache", cmd_buildid_cache, 0 },
37 { "buildid-list", cmd_buildid_list, 0 },
38 { "diff", cmd_diff, 0 },
39 { "evlist", cmd_evlist, 0 },
40 { "help", cmd_help, 0 },
41 { "list", cmd_list, 0 },
42 { "record", cmd_record, 0 },
43 { "report", cmd_report, 0 },
44 { "bench", cmd_bench, 0 },
45 { "stat", cmd_stat, 0 },
46 { "timechart", cmd_timechart, 0 },
47 { "top", cmd_top, 0 },
48 { "annotate", cmd_annotate, 0 },
49 { "version", cmd_version, 0 },
50 { "script", cmd_script, 0 },
51 { "sched", cmd_sched, 0 },
Namhyung Kim29a0fc92012-09-28 18:31:59 +090052#ifdef LIBELF_SUPPORT
Frederic Weisbecker98a41792012-08-09 16:31:51 +020053 { "probe", cmd_probe, 0 },
Namhyung Kim393be2e2012-08-06 13:41:21 +090054#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020055 { "kmem", cmd_kmem, 0 },
56 { "lock", cmd_lock, 0 },
57 { "kvm", cmd_kvm, 0 },
58 { "test", cmd_test, 0 },
Namhyung Kimf315e162012-09-28 18:32:01 +090059#ifdef LIBAUDIT_SUPPORT
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030060 { "trace", cmd_trace, 0 },
Namhyung Kim4d290892012-09-27 20:23:38 +090061#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020062 { "inject", cmd_inject, 0 },
63};
64
Ingo Molnar07800602009-04-20 15:00:56 +020065struct pager_config {
66 const char *cmd;
67 int val;
68};
69
70static int pager_command_config(const char *var, const char *value, void *data)
71{
72 struct pager_config *c = data;
73 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
74 c->val = perf_config_bool(var, value);
75 return 0;
76}
77
78/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
79int check_pager_config(const char *cmd)
80{
81 struct pager_config c;
82 c.cmd = cmd;
83 c.val = -1;
84 perf_config(pager_command_config, &c);
85 return c.val;
86}
87
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030088static int tui_command_config(const char *var, const char *value, void *data)
89{
90 struct pager_config *c = data;
91 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
92 c->val = perf_config_bool(var, value);
93 return 0;
94}
95
96/* returns 0 for "no tui", 1 for "use tui", and -1 for "not specified" */
97static int check_tui_config(const char *cmd)
98{
99 struct pager_config c;
100 c.cmd = cmd;
101 c.val = -1;
102 perf_config(tui_command_config, &c);
103 return c.val;
104}
105
Thiago Farina4c574152010-01-27 21:05:55 -0200106static void commit_pager_choice(void)
107{
Ingo Molnar07800602009-04-20 15:00:56 +0200108 switch (use_pager) {
109 case 0:
110 setenv("PERF_PAGER", "cat", 1);
111 break;
112 case 1:
113 /* setup_pager(); */
114 break;
115 default:
116 break;
117 }
118}
119
Thiago Farina4c574152010-01-27 21:05:55 -0200120static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +0200121{
122 int handled = 0;
123
124 while (*argc > 0) {
125 const char *cmd = (*argv)[0];
126 if (cmd[0] != '-')
127 break;
128
129 /*
130 * For legacy reasons, the "version" and "help"
131 * commands can be written with "--" prepended
132 * to make them look like flags.
133 */
134 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
135 break;
136
137 /*
138 * Check remaining flags.
139 */
Vincent Legollcfed95a2009-10-13 10:18:16 +0200140 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
141 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +0200142 if (*cmd == '=')
143 perf_set_argv_exec_path(cmd + 1);
144 else {
145 puts(perf_exec_path());
146 exit(0);
147 }
148 } else if (!strcmp(cmd, "--html-path")) {
149 puts(system_path(PERF_HTML_PATH));
150 exit(0);
151 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
152 use_pager = 1;
153 } else if (!strcmp(cmd, "--no-pager")) {
154 use_pager = 0;
155 if (envchanged)
156 *envchanged = 1;
157 } else if (!strcmp(cmd, "--perf-dir")) {
158 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200159 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200160 usage(perf_usage_string);
161 }
162 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
163 if (envchanged)
164 *envchanged = 1;
165 (*argv)++;
166 (*argc)--;
167 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200168 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
169 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200170 if (envchanged)
171 *envchanged = 1;
172 } else if (!strcmp(cmd, "--work-tree")) {
173 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200174 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200175 usage(perf_usage_string);
176 }
177 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
178 if (envchanged)
179 *envchanged = 1;
180 (*argv)++;
181 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200182 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
183 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200184 if (envchanged)
185 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400186 } else if (!strcmp(cmd, "--debugfs-dir")) {
187 if (*argc < 2) {
188 fprintf(stderr, "No directory given for --debugfs-dir.\n");
189 usage(perf_usage_string);
190 }
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200191 debugfs_set_path((*argv)[1]);
Jason Baron5beeded2009-07-21 14:16:29 -0400192 if (envchanged)
193 *envchanged = 1;
194 (*argv)++;
195 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200196 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200197 debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
198 fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
Jason Baron5beeded2009-07-21 14:16:29 -0400199 if (envchanged)
200 *envchanged = 1;
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200201 } else if (!strcmp(cmd, "--list-cmds")) {
202 unsigned int i;
203
204 for (i = 0; i < ARRAY_SIZE(commands); i++) {
205 struct cmd_struct *p = commands+i;
206 printf("%s ", p->cmd);
207 }
208 exit(0);
Ingo Molnar07800602009-04-20 15:00:56 +0200209 } else {
210 fprintf(stderr, "Unknown option: %s\n", cmd);
211 usage(perf_usage_string);
212 }
213
214 (*argv)++;
215 (*argc)--;
216 handled++;
217 }
218 return handled;
219}
220
221static int handle_alias(int *argcp, const char ***argv)
222{
223 int envchanged = 0, ret = 0, saved_errno = errno;
224 int count, option_count;
Thiago Farina4c574152010-01-27 21:05:55 -0200225 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200226 const char *alias_command;
227 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200228
229 alias_command = (*argv)[0];
230 alias_string = alias_lookup(alias_command);
231 if (alias_string) {
232 if (alias_string[0] == '!') {
233 if (*argcp > 1) {
234 struct strbuf buf;
235
236 strbuf_init(&buf, PATH_MAX);
237 strbuf_addstr(&buf, alias_string);
238 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
239 free(alias_string);
240 alias_string = buf.buf;
241 }
242 ret = system(alias_string + 1);
243 if (ret >= 0 && WIFEXITED(ret) &&
244 WEXITSTATUS(ret) != 127)
245 exit(WEXITSTATUS(ret));
246 die("Failed to run '%s' when expanding alias '%s'",
247 alias_string + 1, alias_command);
248 }
249 count = split_cmdline(alias_string, &new_argv);
250 if (count < 0)
251 die("Bad alias.%s string", alias_command);
252 option_count = handle_options(&new_argv, &count, &envchanged);
253 if (envchanged)
254 die("alias '%s' changes environment variables\n"
255 "You can use '!perf' in the alias to do this.",
256 alias_command);
257 memmove(new_argv - option_count, new_argv,
258 count * sizeof(char *));
259 new_argv -= option_count;
260
261 if (count < 1)
262 die("empty alias for %s", alias_command);
263
264 if (!strcmp(alias_command, new_argv[0]))
265 die("recursive alias: %s", alias_command);
266
Thiago Farina4c574152010-01-27 21:05:55 -0200267 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200268 (count + *argcp + 1));
269 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200270 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
271 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200272
273 *argv = new_argv;
274 *argcp += count - 1;
275
276 ret = 1;
277 }
278
279 errno = saved_errno;
280
281 return ret;
282}
283
284const char perf_version_string[] = PERF_VERSION;
285
286#define RUN_SETUP (1<<0)
287#define USE_PAGER (1<<1)
288/*
289 * require working tree to be present -- anything uses this needs
290 * RUN_SETUP for reading from the configuration file.
291 */
292#define NEED_WORK_TREE (1<<2)
293
Ingo Molnar07800602009-04-20 15:00:56 +0200294static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
295{
296 int status;
297 struct stat st;
298 const char *prefix;
299
300 prefix = NULL;
301 if (p->option & RUN_SETUP)
302 prefix = NULL; /* setup_perf_directory(); */
303
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300304 if (use_browser == -1)
305 use_browser = check_tui_config(p->cmd);
306
Ingo Molnar07800602009-04-20 15:00:56 +0200307 if (use_pager == -1 && p->option & RUN_SETUP)
308 use_pager = check_pager_config(p->cmd);
309 if (use_pager == -1 && p->option & USE_PAGER)
310 use_pager = 1;
311 commit_pager_choice();
312
Ingo Molnar07800602009-04-20 15:00:56 +0200313 status = p->fn(argc, argv, prefix);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300314 exit_browser(status);
315
Ingo Molnar07800602009-04-20 15:00:56 +0200316 if (status)
317 return status & 0xff;
318
319 /* Somebody closed stdout? */
320 if (fstat(fileno(stdout), &st))
321 return 0;
322 /* Ignore write errors for pipes and sockets.. */
323 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
324 return 0;
325
326 /* Check for ENOSPC and EIO errors.. */
327 if (fflush(stdout))
328 die("write failure on standard output: %s", strerror(errno));
329 if (ferror(stdout))
330 die("unknown write failure on standard output");
331 if (fclose(stdout))
332 die("close failed on standard output: %s", strerror(errno));
333 return 0;
334}
335
336static void handle_internal_command(int argc, const char **argv)
337{
338 const char *cmd = argv[0];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200339 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200340 static const char ext[] = STRIP_EXTENSION;
341
342 if (sizeof(ext) > 1) {
343 i = strlen(argv[0]) - strlen(ext);
344 if (i > 0 && !strcmp(argv[0] + i, ext)) {
345 char *argv0 = strdup(argv[0]);
346 argv[0] = cmd = argv0;
347 argv0[i] = '\0';
348 }
349 }
350
351 /* Turn "perf cmd --help" into "perf help cmd" */
352 if (argc > 1 && !strcmp(argv[1], "--help")) {
353 argv[1] = argv[0];
354 argv[0] = cmd = "help";
355 }
356
357 for (i = 0; i < ARRAY_SIZE(commands); i++) {
358 struct cmd_struct *p = commands+i;
359 if (strcmp(p->cmd, cmd))
360 continue;
361 exit(run_builtin(p, argc, argv));
362 }
363}
364
365static void execv_dashed_external(const char **argv)
366{
367 struct strbuf cmd = STRBUF_INIT;
368 const char *tmp;
369 int status;
370
371 strbuf_addf(&cmd, "perf-%s", argv[0]);
372
373 /*
374 * argv[0] must be the perf command, but the argv array
375 * belongs to the caller, and may be reused in
376 * subsequent loop iterations. Save argv[0] and
377 * restore it on error.
378 */
379 tmp = argv[0];
380 argv[0] = cmd.buf;
381
382 /*
383 * if we fail because the command is not found, it is
384 * OK to return. Otherwise, we just pass along the status code.
385 */
386 status = run_command_v_opt(argv, 0);
387 if (status != -ERR_RUN_COMMAND_EXEC) {
388 if (IS_RUN_COMMAND_ERR(status))
389 die("unable to run '%s'", argv[0]);
390 exit(-status);
391 }
392 errno = ENOENT; /* as if we called execvp */
393
394 argv[0] = tmp;
395
396 strbuf_release(&cmd);
397}
398
399static int run_argv(int *argcp, const char ***argv)
400{
401 int done_alias = 0;
402
403 while (1) {
404 /* See if it's an internal command */
405 handle_internal_command(*argcp, *argv);
406
407 /* .. then try the external ones */
408 execv_dashed_external(*argv);
409
410 /* It could be an alias -- this works around the insanity
411 * of overriding "perf log" with "perf show" by having
412 * alias.log = show
413 */
414 if (done_alias || !handle_alias(argcp, argv))
415 break;
416 done_alias = 1;
417 }
418
419 return done_alias;
420}
421
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300422static void pthread__block_sigwinch(void)
423{
424 sigset_t set;
425
426 sigemptyset(&set);
427 sigaddset(&set, SIGWINCH);
428 pthread_sigmask(SIG_BLOCK, &set, NULL);
429}
430
431void pthread__unblock_sigwinch(void)
432{
433 sigset_t set;
434
435 sigemptyset(&set);
436 sigaddset(&set, SIGWINCH);
437 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
438}
439
Ingo Molnar07800602009-04-20 15:00:56 +0200440int main(int argc, const char **argv)
441{
442 const char *cmd;
443
Arnaldo Carvalho de Melo0c1fe6b22012-10-06 14:57:10 -0300444 page_size = sysconf(_SC_PAGE_SIZE);
445
Ingo Molnar07800602009-04-20 15:00:56 +0200446 cmd = perf_extract_argv0_path(argv[0]);
447 if (!cmd)
448 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400449 /* get debugfs mount point from /proc/mounts */
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200450 debugfs_mount(NULL);
Ingo Molnar07800602009-04-20 15:00:56 +0200451 /*
452 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
453 *
454 * - cannot take flags in between the "perf" and the "xxxx".
455 * - cannot execute it externally (since it would just do
456 * the same thing over again)
457 *
458 * So we just directly call the internal command handler, and
459 * die if that one cannot handle it.
460 */
461 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200462 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200463 argv[0] = cmd;
464 handle_internal_command(argc, argv);
465 die("cannot handle %s internally", cmd);
466 }
467
468 /* Look for flags.. */
469 argv++;
470 argc--;
471 handle_options(&argv, &argc, NULL);
472 commit_pager_choice();
Stephane Eranian45de34b2010-06-01 21:25:01 +0200473 set_buildid_dir();
474
Ingo Molnar07800602009-04-20 15:00:56 +0200475 if (argc > 0) {
476 if (!prefixcmp(argv[0], "--"))
477 argv[0] += 2;
478 } else {
479 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100480 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200481 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100482 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200483 exit(1);
484 }
485 cmd = argv[0];
486
487 /*
488 * We use PATH to find perf commands, but we prepend some higher
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100489 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
Ingo Molnar07800602009-04-20 15:00:56 +0200490 * environment, and the $(perfexecdir) from the Makefile at build
491 * time.
492 */
493 setup_path();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300494 /*
495 * Block SIGWINCH notifications so that the thread that wants it can
496 * unblock and get syscalls like select interrupted instead of waiting
497 * forever while the signal goes to some other non interested thread.
498 */
499 pthread__block_sigwinch();
Ingo Molnar07800602009-04-20 15:00:56 +0200500
501 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200502 static int done_help;
503 static int was_alias;
Ingo Molnar8035e422009-06-06 15:19:13 +0200504
Ingo Molnar07800602009-04-20 15:00:56 +0200505 was_alias = run_argv(&argc, &argv);
506 if (errno != ENOENT)
507 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200508
Ingo Molnar07800602009-04-20 15:00:56 +0200509 if (was_alias) {
510 fprintf(stderr, "Expansion of alias '%s' failed; "
511 "'%s' is not a perf-command\n",
512 cmd, argv[0]);
513 exit(1);
514 }
515 if (!done_help) {
516 cmd = argv[0] = help_unknown_cmd(cmd);
517 done_help = 1;
518 } else
519 break;
520 }
521
522 fprintf(stderr, "Failed to run command '%s': %s\n",
523 cmd, strerror(errno));
524
525 return 1;
526}