blob: db37ee3f29b8425c3a1d38157f5a23741a507284 [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"
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
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030024int use_browser = -1;
Ingo Molnar07800602009-04-20 15:00:56 +020025static int use_pager = -1;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030026
Frederic Weisbecker98a41792012-08-09 16:31:51 +020027struct cmd_struct {
28 const char *cmd;
29 int (*fn)(int, const char **, const char *);
30 int option;
31};
32
33static struct cmd_struct commands[] = {
34 { "buildid-cache", cmd_buildid_cache, 0 },
35 { "buildid-list", cmd_buildid_list, 0 },
36 { "diff", cmd_diff, 0 },
37 { "evlist", cmd_evlist, 0 },
38 { "help", cmd_help, 0 },
39 { "list", cmd_list, 0 },
40 { "record", cmd_record, 0 },
41 { "report", cmd_report, 0 },
42 { "bench", cmd_bench, 0 },
43 { "stat", cmd_stat, 0 },
44 { "timechart", cmd_timechart, 0 },
45 { "top", cmd_top, 0 },
46 { "annotate", cmd_annotate, 0 },
47 { "version", cmd_version, 0 },
48 { "script", cmd_script, 0 },
49 { "sched", cmd_sched, 0 },
50 { "probe", cmd_probe, 0 },
51 { "kmem", cmd_kmem, 0 },
52 { "lock", cmd_lock, 0 },
53 { "kvm", cmd_kvm, 0 },
54 { "test", cmd_test, 0 },
55 { "inject", cmd_inject, 0 },
56};
57
Ingo Molnar07800602009-04-20 15:00:56 +020058struct pager_config {
59 const char *cmd;
60 int val;
61};
62
63static int pager_command_config(const char *var, const char *value, void *data)
64{
65 struct pager_config *c = data;
66 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
67 c->val = perf_config_bool(var, value);
68 return 0;
69}
70
71/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
72int check_pager_config(const char *cmd)
73{
74 struct pager_config c;
75 c.cmd = cmd;
76 c.val = -1;
77 perf_config(pager_command_config, &c);
78 return c.val;
79}
80
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030081static int tui_command_config(const char *var, const char *value, void *data)
82{
83 struct pager_config *c = data;
84 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
85 c->val = perf_config_bool(var, value);
86 return 0;
87}
88
89/* returns 0 for "no tui", 1 for "use tui", and -1 for "not specified" */
90static int check_tui_config(const char *cmd)
91{
92 struct pager_config c;
93 c.cmd = cmd;
94 c.val = -1;
95 perf_config(tui_command_config, &c);
96 return c.val;
97}
98
Thiago Farina4c574152010-01-27 21:05:55 -020099static void commit_pager_choice(void)
100{
Ingo Molnar07800602009-04-20 15:00:56 +0200101 switch (use_pager) {
102 case 0:
103 setenv("PERF_PAGER", "cat", 1);
104 break;
105 case 1:
106 /* setup_pager(); */
107 break;
108 default:
109 break;
110 }
111}
112
Thiago Farina4c574152010-01-27 21:05:55 -0200113static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +0200114{
115 int handled = 0;
116
117 while (*argc > 0) {
118 const char *cmd = (*argv)[0];
119 if (cmd[0] != '-')
120 break;
121
122 /*
123 * For legacy reasons, the "version" and "help"
124 * commands can be written with "--" prepended
125 * to make them look like flags.
126 */
127 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
128 break;
129
130 /*
131 * Check remaining flags.
132 */
Vincent Legollcfed95a2009-10-13 10:18:16 +0200133 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
134 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +0200135 if (*cmd == '=')
136 perf_set_argv_exec_path(cmd + 1);
137 else {
138 puts(perf_exec_path());
139 exit(0);
140 }
141 } else if (!strcmp(cmd, "--html-path")) {
142 puts(system_path(PERF_HTML_PATH));
143 exit(0);
144 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
145 use_pager = 1;
146 } else if (!strcmp(cmd, "--no-pager")) {
147 use_pager = 0;
148 if (envchanged)
149 *envchanged = 1;
150 } else if (!strcmp(cmd, "--perf-dir")) {
151 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200152 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200153 usage(perf_usage_string);
154 }
155 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
156 if (envchanged)
157 *envchanged = 1;
158 (*argv)++;
159 (*argc)--;
160 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200161 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
162 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200163 if (envchanged)
164 *envchanged = 1;
165 } else if (!strcmp(cmd, "--work-tree")) {
166 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200167 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200168 usage(perf_usage_string);
169 }
170 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
171 if (envchanged)
172 *envchanged = 1;
173 (*argv)++;
174 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200175 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
176 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200177 if (envchanged)
178 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400179 } else if (!strcmp(cmd, "--debugfs-dir")) {
180 if (*argc < 2) {
181 fprintf(stderr, "No directory given for --debugfs-dir.\n");
182 usage(perf_usage_string);
183 }
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200184 debugfs_set_path((*argv)[1]);
Jason Baron5beeded2009-07-21 14:16:29 -0400185 if (envchanged)
186 *envchanged = 1;
187 (*argv)++;
188 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200189 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200190 debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
191 fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
Jason Baron5beeded2009-07-21 14:16:29 -0400192 if (envchanged)
193 *envchanged = 1;
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200194 } else if (!strcmp(cmd, "--list-cmds")) {
195 unsigned int i;
196
197 for (i = 0; i < ARRAY_SIZE(commands); i++) {
198 struct cmd_struct *p = commands+i;
199 printf("%s ", p->cmd);
200 }
201 exit(0);
Ingo Molnar07800602009-04-20 15:00:56 +0200202 } else {
203 fprintf(stderr, "Unknown option: %s\n", cmd);
204 usage(perf_usage_string);
205 }
206
207 (*argv)++;
208 (*argc)--;
209 handled++;
210 }
211 return handled;
212}
213
214static int handle_alias(int *argcp, const char ***argv)
215{
216 int envchanged = 0, ret = 0, saved_errno = errno;
217 int count, option_count;
Thiago Farina4c574152010-01-27 21:05:55 -0200218 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200219 const char *alias_command;
220 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200221
222 alias_command = (*argv)[0];
223 alias_string = alias_lookup(alias_command);
224 if (alias_string) {
225 if (alias_string[0] == '!') {
226 if (*argcp > 1) {
227 struct strbuf buf;
228
229 strbuf_init(&buf, PATH_MAX);
230 strbuf_addstr(&buf, alias_string);
231 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
232 free(alias_string);
233 alias_string = buf.buf;
234 }
235 ret = system(alias_string + 1);
236 if (ret >= 0 && WIFEXITED(ret) &&
237 WEXITSTATUS(ret) != 127)
238 exit(WEXITSTATUS(ret));
239 die("Failed to run '%s' when expanding alias '%s'",
240 alias_string + 1, alias_command);
241 }
242 count = split_cmdline(alias_string, &new_argv);
243 if (count < 0)
244 die("Bad alias.%s string", alias_command);
245 option_count = handle_options(&new_argv, &count, &envchanged);
246 if (envchanged)
247 die("alias '%s' changes environment variables\n"
248 "You can use '!perf' in the alias to do this.",
249 alias_command);
250 memmove(new_argv - option_count, new_argv,
251 count * sizeof(char *));
252 new_argv -= option_count;
253
254 if (count < 1)
255 die("empty alias for %s", alias_command);
256
257 if (!strcmp(alias_command, new_argv[0]))
258 die("recursive alias: %s", alias_command);
259
Thiago Farina4c574152010-01-27 21:05:55 -0200260 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200261 (count + *argcp + 1));
262 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200263 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
264 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200265
266 *argv = new_argv;
267 *argcp += count - 1;
268
269 ret = 1;
270 }
271
272 errno = saved_errno;
273
274 return ret;
275}
276
277const char perf_version_string[] = PERF_VERSION;
278
279#define RUN_SETUP (1<<0)
280#define USE_PAGER (1<<1)
281/*
282 * require working tree to be present -- anything uses this needs
283 * RUN_SETUP for reading from the configuration file.
284 */
285#define NEED_WORK_TREE (1<<2)
286
Ingo Molnar07800602009-04-20 15:00:56 +0200287static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
288{
289 int status;
290 struct stat st;
291 const char *prefix;
292
293 prefix = NULL;
294 if (p->option & RUN_SETUP)
295 prefix = NULL; /* setup_perf_directory(); */
296
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300297 if (use_browser == -1)
298 use_browser = check_tui_config(p->cmd);
299
Ingo Molnar07800602009-04-20 15:00:56 +0200300 if (use_pager == -1 && p->option & RUN_SETUP)
301 use_pager = check_pager_config(p->cmd);
302 if (use_pager == -1 && p->option & USE_PAGER)
303 use_pager = 1;
304 commit_pager_choice();
305
Ingo Molnar07800602009-04-20 15:00:56 +0200306 status = p->fn(argc, argv, prefix);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300307 exit_browser(status);
308
Ingo Molnar07800602009-04-20 15:00:56 +0200309 if (status)
310 return status & 0xff;
311
312 /* Somebody closed stdout? */
313 if (fstat(fileno(stdout), &st))
314 return 0;
315 /* Ignore write errors for pipes and sockets.. */
316 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
317 return 0;
318
319 /* Check for ENOSPC and EIO errors.. */
320 if (fflush(stdout))
321 die("write failure on standard output: %s", strerror(errno));
322 if (ferror(stdout))
323 die("unknown write failure on standard output");
324 if (fclose(stdout))
325 die("close failed on standard output: %s", strerror(errno));
326 return 0;
327}
328
329static void handle_internal_command(int argc, const char **argv)
330{
331 const char *cmd = argv[0];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200332 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200333 static const char ext[] = STRIP_EXTENSION;
334
335 if (sizeof(ext) > 1) {
336 i = strlen(argv[0]) - strlen(ext);
337 if (i > 0 && !strcmp(argv[0] + i, ext)) {
338 char *argv0 = strdup(argv[0]);
339 argv[0] = cmd = argv0;
340 argv0[i] = '\0';
341 }
342 }
343
344 /* Turn "perf cmd --help" into "perf help cmd" */
345 if (argc > 1 && !strcmp(argv[1], "--help")) {
346 argv[1] = argv[0];
347 argv[0] = cmd = "help";
348 }
349
350 for (i = 0; i < ARRAY_SIZE(commands); i++) {
351 struct cmd_struct *p = commands+i;
352 if (strcmp(p->cmd, cmd))
353 continue;
354 exit(run_builtin(p, argc, argv));
355 }
356}
357
358static void execv_dashed_external(const char **argv)
359{
360 struct strbuf cmd = STRBUF_INIT;
361 const char *tmp;
362 int status;
363
364 strbuf_addf(&cmd, "perf-%s", argv[0]);
365
366 /*
367 * argv[0] must be the perf command, but the argv array
368 * belongs to the caller, and may be reused in
369 * subsequent loop iterations. Save argv[0] and
370 * restore it on error.
371 */
372 tmp = argv[0];
373 argv[0] = cmd.buf;
374
375 /*
376 * if we fail because the command is not found, it is
377 * OK to return. Otherwise, we just pass along the status code.
378 */
379 status = run_command_v_opt(argv, 0);
380 if (status != -ERR_RUN_COMMAND_EXEC) {
381 if (IS_RUN_COMMAND_ERR(status))
382 die("unable to run '%s'", argv[0]);
383 exit(-status);
384 }
385 errno = ENOENT; /* as if we called execvp */
386
387 argv[0] = tmp;
388
389 strbuf_release(&cmd);
390}
391
392static int run_argv(int *argcp, const char ***argv)
393{
394 int done_alias = 0;
395
396 while (1) {
397 /* See if it's an internal command */
398 handle_internal_command(*argcp, *argv);
399
400 /* .. then try the external ones */
401 execv_dashed_external(*argv);
402
403 /* It could be an alias -- this works around the insanity
404 * of overriding "perf log" with "perf show" by having
405 * alias.log = show
406 */
407 if (done_alias || !handle_alias(argcp, argv))
408 break;
409 done_alias = 1;
410 }
411
412 return done_alias;
413}
414
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300415static void pthread__block_sigwinch(void)
416{
417 sigset_t set;
418
419 sigemptyset(&set);
420 sigaddset(&set, SIGWINCH);
421 pthread_sigmask(SIG_BLOCK, &set, NULL);
422}
423
424void pthread__unblock_sigwinch(void)
425{
426 sigset_t set;
427
428 sigemptyset(&set);
429 sigaddset(&set, SIGWINCH);
430 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
431}
432
Ingo Molnar07800602009-04-20 15:00:56 +0200433int main(int argc, const char **argv)
434{
435 const char *cmd;
436
437 cmd = perf_extract_argv0_path(argv[0]);
438 if (!cmd)
439 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400440 /* get debugfs mount point from /proc/mounts */
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200441 debugfs_mount(NULL);
Ingo Molnar07800602009-04-20 15:00:56 +0200442 /*
443 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
444 *
445 * - cannot take flags in between the "perf" and the "xxxx".
446 * - cannot execute it externally (since it would just do
447 * the same thing over again)
448 *
449 * So we just directly call the internal command handler, and
450 * die if that one cannot handle it.
451 */
452 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200453 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200454 argv[0] = cmd;
455 handle_internal_command(argc, argv);
456 die("cannot handle %s internally", cmd);
457 }
458
459 /* Look for flags.. */
460 argv++;
461 argc--;
462 handle_options(&argv, &argc, NULL);
463 commit_pager_choice();
Stephane Eranian45de34b2010-06-01 21:25:01 +0200464 set_buildid_dir();
465
Ingo Molnar07800602009-04-20 15:00:56 +0200466 if (argc > 0) {
467 if (!prefixcmp(argv[0], "--"))
468 argv[0] += 2;
469 } else {
470 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100471 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200472 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100473 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200474 exit(1);
475 }
476 cmd = argv[0];
477
478 /*
479 * We use PATH to find perf commands, but we prepend some higher
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100480 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
Ingo Molnar07800602009-04-20 15:00:56 +0200481 * environment, and the $(perfexecdir) from the Makefile at build
482 * time.
483 */
484 setup_path();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300485 /*
486 * Block SIGWINCH notifications so that the thread that wants it can
487 * unblock and get syscalls like select interrupted instead of waiting
488 * forever while the signal goes to some other non interested thread.
489 */
490 pthread__block_sigwinch();
Ingo Molnar07800602009-04-20 15:00:56 +0200491
492 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200493 static int done_help;
494 static int was_alias;
Ingo Molnar8035e422009-06-06 15:19:13 +0200495
Ingo Molnar07800602009-04-20 15:00:56 +0200496 was_alias = run_argv(&argc, &argv);
497 if (errno != ENOENT)
498 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200499
Ingo Molnar07800602009-04-20 15:00:56 +0200500 if (was_alias) {
501 fprintf(stderr, "Expansion of alias '%s' failed; "
502 "'%s' is not a perf-command\n",
503 cmd, argv[0]);
504 exit(1);
505 }
506 if (!done_help) {
507 cmd = argv[0] = help_unknown_cmd(cmd);
508 done_help = 1;
509 } else
510 break;
511 }
512
513 fprintf(stderr, "Failed to run command '%s': %s\n",
514 cmd, strerror(errno));
515
516 return 1;
517}