blob: 3fb052c9a27f4b801ac234e544774502205255fb [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 },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030058 { "trace", cmd_trace, 0 },
Frederic Weisbecker98a41792012-08-09 16:31:51 +020059 { "inject", cmd_inject, 0 },
60};
61
Ingo Molnar07800602009-04-20 15:00:56 +020062struct pager_config {
63 const char *cmd;
64 int val;
65};
66
67static int pager_command_config(const char *var, const char *value, void *data)
68{
69 struct pager_config *c = data;
70 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
71 c->val = perf_config_bool(var, value);
72 return 0;
73}
74
75/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
76int check_pager_config(const char *cmd)
77{
78 struct pager_config c;
79 c.cmd = cmd;
80 c.val = -1;
81 perf_config(pager_command_config, &c);
82 return c.val;
83}
84
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030085static int tui_command_config(const char *var, const char *value, void *data)
86{
87 struct pager_config *c = data;
88 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
89 c->val = perf_config_bool(var, value);
90 return 0;
91}
92
93/* returns 0 for "no tui", 1 for "use tui", and -1 for "not specified" */
94static int check_tui_config(const char *cmd)
95{
96 struct pager_config c;
97 c.cmd = cmd;
98 c.val = -1;
99 perf_config(tui_command_config, &c);
100 return c.val;
101}
102
Thiago Farina4c574152010-01-27 21:05:55 -0200103static void commit_pager_choice(void)
104{
Ingo Molnar07800602009-04-20 15:00:56 +0200105 switch (use_pager) {
106 case 0:
107 setenv("PERF_PAGER", "cat", 1);
108 break;
109 case 1:
110 /* setup_pager(); */
111 break;
112 default:
113 break;
114 }
115}
116
Thiago Farina4c574152010-01-27 21:05:55 -0200117static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +0200118{
119 int handled = 0;
120
121 while (*argc > 0) {
122 const char *cmd = (*argv)[0];
123 if (cmd[0] != '-')
124 break;
125
126 /*
127 * For legacy reasons, the "version" and "help"
128 * commands can be written with "--" prepended
129 * to make them look like flags.
130 */
131 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
132 break;
133
134 /*
135 * Check remaining flags.
136 */
Vincent Legollcfed95a2009-10-13 10:18:16 +0200137 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
138 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +0200139 if (*cmd == '=')
140 perf_set_argv_exec_path(cmd + 1);
141 else {
142 puts(perf_exec_path());
143 exit(0);
144 }
145 } else if (!strcmp(cmd, "--html-path")) {
146 puts(system_path(PERF_HTML_PATH));
147 exit(0);
148 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
149 use_pager = 1;
150 } else if (!strcmp(cmd, "--no-pager")) {
151 use_pager = 0;
152 if (envchanged)
153 *envchanged = 1;
154 } else if (!strcmp(cmd, "--perf-dir")) {
155 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200156 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200157 usage(perf_usage_string);
158 }
159 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
160 if (envchanged)
161 *envchanged = 1;
162 (*argv)++;
163 (*argc)--;
164 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200165 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
166 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200167 if (envchanged)
168 *envchanged = 1;
169 } else if (!strcmp(cmd, "--work-tree")) {
170 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200171 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200172 usage(perf_usage_string);
173 }
174 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
175 if (envchanged)
176 *envchanged = 1;
177 (*argv)++;
178 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200179 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
180 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200181 if (envchanged)
182 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400183 } else if (!strcmp(cmd, "--debugfs-dir")) {
184 if (*argc < 2) {
185 fprintf(stderr, "No directory given for --debugfs-dir.\n");
186 usage(perf_usage_string);
187 }
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200188 debugfs_set_path((*argv)[1]);
Jason Baron5beeded2009-07-21 14:16:29 -0400189 if (envchanged)
190 *envchanged = 1;
191 (*argv)++;
192 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200193 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200194 debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
195 fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
Jason Baron5beeded2009-07-21 14:16:29 -0400196 if (envchanged)
197 *envchanged = 1;
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200198 } else if (!strcmp(cmd, "--list-cmds")) {
199 unsigned int i;
200
201 for (i = 0; i < ARRAY_SIZE(commands); i++) {
202 struct cmd_struct *p = commands+i;
203 printf("%s ", p->cmd);
204 }
205 exit(0);
Ingo Molnar07800602009-04-20 15:00:56 +0200206 } else {
207 fprintf(stderr, "Unknown option: %s\n", cmd);
208 usage(perf_usage_string);
209 }
210
211 (*argv)++;
212 (*argc)--;
213 handled++;
214 }
215 return handled;
216}
217
218static int handle_alias(int *argcp, const char ***argv)
219{
220 int envchanged = 0, ret = 0, saved_errno = errno;
221 int count, option_count;
Thiago Farina4c574152010-01-27 21:05:55 -0200222 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200223 const char *alias_command;
224 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200225
226 alias_command = (*argv)[0];
227 alias_string = alias_lookup(alias_command);
228 if (alias_string) {
229 if (alias_string[0] == '!') {
230 if (*argcp > 1) {
231 struct strbuf buf;
232
233 strbuf_init(&buf, PATH_MAX);
234 strbuf_addstr(&buf, alias_string);
235 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
236 free(alias_string);
237 alias_string = buf.buf;
238 }
239 ret = system(alias_string + 1);
240 if (ret >= 0 && WIFEXITED(ret) &&
241 WEXITSTATUS(ret) != 127)
242 exit(WEXITSTATUS(ret));
243 die("Failed to run '%s' when expanding alias '%s'",
244 alias_string + 1, alias_command);
245 }
246 count = split_cmdline(alias_string, &new_argv);
247 if (count < 0)
248 die("Bad alias.%s string", alias_command);
249 option_count = handle_options(&new_argv, &count, &envchanged);
250 if (envchanged)
251 die("alias '%s' changes environment variables\n"
252 "You can use '!perf' in the alias to do this.",
253 alias_command);
254 memmove(new_argv - option_count, new_argv,
255 count * sizeof(char *));
256 new_argv -= option_count;
257
258 if (count < 1)
259 die("empty alias for %s", alias_command);
260
261 if (!strcmp(alias_command, new_argv[0]))
262 die("recursive alias: %s", alias_command);
263
Thiago Farina4c574152010-01-27 21:05:55 -0200264 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200265 (count + *argcp + 1));
266 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200267 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
268 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200269
270 *argv = new_argv;
271 *argcp += count - 1;
272
273 ret = 1;
274 }
275
276 errno = saved_errno;
277
278 return ret;
279}
280
281const char perf_version_string[] = PERF_VERSION;
282
283#define RUN_SETUP (1<<0)
284#define USE_PAGER (1<<1)
285/*
286 * require working tree to be present -- anything uses this needs
287 * RUN_SETUP for reading from the configuration file.
288 */
289#define NEED_WORK_TREE (1<<2)
290
Ingo Molnar07800602009-04-20 15:00:56 +0200291static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
292{
293 int status;
294 struct stat st;
295 const char *prefix;
296
297 prefix = NULL;
298 if (p->option & RUN_SETUP)
299 prefix = NULL; /* setup_perf_directory(); */
300
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300301 if (use_browser == -1)
302 use_browser = check_tui_config(p->cmd);
303
Ingo Molnar07800602009-04-20 15:00:56 +0200304 if (use_pager == -1 && p->option & RUN_SETUP)
305 use_pager = check_pager_config(p->cmd);
306 if (use_pager == -1 && p->option & USE_PAGER)
307 use_pager = 1;
308 commit_pager_choice();
309
Ingo Molnar07800602009-04-20 15:00:56 +0200310 status = p->fn(argc, argv, prefix);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300311 exit_browser(status);
312
Ingo Molnar07800602009-04-20 15:00:56 +0200313 if (status)
314 return status & 0xff;
315
316 /* Somebody closed stdout? */
317 if (fstat(fileno(stdout), &st))
318 return 0;
319 /* Ignore write errors for pipes and sockets.. */
320 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
321 return 0;
322
323 /* Check for ENOSPC and EIO errors.. */
324 if (fflush(stdout))
325 die("write failure on standard output: %s", strerror(errno));
326 if (ferror(stdout))
327 die("unknown write failure on standard output");
328 if (fclose(stdout))
329 die("close failed on standard output: %s", strerror(errno));
330 return 0;
331}
332
333static void handle_internal_command(int argc, const char **argv)
334{
335 const char *cmd = argv[0];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200336 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200337 static const char ext[] = STRIP_EXTENSION;
338
339 if (sizeof(ext) > 1) {
340 i = strlen(argv[0]) - strlen(ext);
341 if (i > 0 && !strcmp(argv[0] + i, ext)) {
342 char *argv0 = strdup(argv[0]);
343 argv[0] = cmd = argv0;
344 argv0[i] = '\0';
345 }
346 }
347
348 /* Turn "perf cmd --help" into "perf help cmd" */
349 if (argc > 1 && !strcmp(argv[1], "--help")) {
350 argv[1] = argv[0];
351 argv[0] = cmd = "help";
352 }
353
354 for (i = 0; i < ARRAY_SIZE(commands); i++) {
355 struct cmd_struct *p = commands+i;
356 if (strcmp(p->cmd, cmd))
357 continue;
358 exit(run_builtin(p, argc, argv));
359 }
360}
361
362static void execv_dashed_external(const char **argv)
363{
364 struct strbuf cmd = STRBUF_INIT;
365 const char *tmp;
366 int status;
367
368 strbuf_addf(&cmd, "perf-%s", argv[0]);
369
370 /*
371 * argv[0] must be the perf command, but the argv array
372 * belongs to the caller, and may be reused in
373 * subsequent loop iterations. Save argv[0] and
374 * restore it on error.
375 */
376 tmp = argv[0];
377 argv[0] = cmd.buf;
378
379 /*
380 * if we fail because the command is not found, it is
381 * OK to return. Otherwise, we just pass along the status code.
382 */
383 status = run_command_v_opt(argv, 0);
384 if (status != -ERR_RUN_COMMAND_EXEC) {
385 if (IS_RUN_COMMAND_ERR(status))
386 die("unable to run '%s'", argv[0]);
387 exit(-status);
388 }
389 errno = ENOENT; /* as if we called execvp */
390
391 argv[0] = tmp;
392
393 strbuf_release(&cmd);
394}
395
396static int run_argv(int *argcp, const char ***argv)
397{
398 int done_alias = 0;
399
400 while (1) {
401 /* See if it's an internal command */
402 handle_internal_command(*argcp, *argv);
403
404 /* .. then try the external ones */
405 execv_dashed_external(*argv);
406
407 /* It could be an alias -- this works around the insanity
408 * of overriding "perf log" with "perf show" by having
409 * alias.log = show
410 */
411 if (done_alias || !handle_alias(argcp, argv))
412 break;
413 done_alias = 1;
414 }
415
416 return done_alias;
417}
418
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300419static void pthread__block_sigwinch(void)
420{
421 sigset_t set;
422
423 sigemptyset(&set);
424 sigaddset(&set, SIGWINCH);
425 pthread_sigmask(SIG_BLOCK, &set, NULL);
426}
427
428void pthread__unblock_sigwinch(void)
429{
430 sigset_t set;
431
432 sigemptyset(&set);
433 sigaddset(&set, SIGWINCH);
434 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
435}
436
Ingo Molnar07800602009-04-20 15:00:56 +0200437int main(int argc, const char **argv)
438{
439 const char *cmd;
440
441 cmd = perf_extract_argv0_path(argv[0]);
442 if (!cmd)
443 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400444 /* get debugfs mount point from /proc/mounts */
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200445 debugfs_mount(NULL);
Ingo Molnar07800602009-04-20 15:00:56 +0200446 /*
447 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
448 *
449 * - cannot take flags in between the "perf" and the "xxxx".
450 * - cannot execute it externally (since it would just do
451 * the same thing over again)
452 *
453 * So we just directly call the internal command handler, and
454 * die if that one cannot handle it.
455 */
456 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200457 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200458 argv[0] = cmd;
459 handle_internal_command(argc, argv);
460 die("cannot handle %s internally", cmd);
461 }
462
463 /* Look for flags.. */
464 argv++;
465 argc--;
466 handle_options(&argv, &argc, NULL);
467 commit_pager_choice();
Stephane Eranian45de34b2010-06-01 21:25:01 +0200468 set_buildid_dir();
469
Ingo Molnar07800602009-04-20 15:00:56 +0200470 if (argc > 0) {
471 if (!prefixcmp(argv[0], "--"))
472 argv[0] += 2;
473 } else {
474 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100475 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200476 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100477 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200478 exit(1);
479 }
480 cmd = argv[0];
481
482 /*
483 * We use PATH to find perf commands, but we prepend some higher
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100484 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
Ingo Molnar07800602009-04-20 15:00:56 +0200485 * environment, and the $(perfexecdir) from the Makefile at build
486 * time.
487 */
488 setup_path();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300489 /*
490 * Block SIGWINCH notifications so that the thread that wants it can
491 * unblock and get syscalls like select interrupted instead of waiting
492 * forever while the signal goes to some other non interested thread.
493 */
494 pthread__block_sigwinch();
Ingo Molnar07800602009-04-20 15:00:56 +0200495
496 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200497 static int done_help;
498 static int was_alias;
Ingo Molnar8035e422009-06-06 15:19:13 +0200499
Ingo Molnar07800602009-04-20 15:00:56 +0200500 was_alias = run_argv(&argc, &argv);
501 if (errno != ENOENT)
502 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200503
Ingo Molnar07800602009-04-20 15:00:56 +0200504 if (was_alias) {
505 fprintf(stderr, "Expansion of alias '%s' failed; "
506 "'%s' is not a perf-command\n",
507 cmd, argv[0]);
508 exit(1);
509 }
510 if (!done_help) {
511 cmd = argv[0] = help_unknown_cmd(cmd);
512 done_help = 1;
513 } else
514 break;
515 }
516
517 fprintf(stderr, "Failed to run command '%s': %s\n",
518 cmd, strerror(errno));
519
520 return 1;
521}