ARM: sa1100: remove unused RTC register definitions
[deliverable/linux.git] / tools / perf / perf.c
CommitLineData
bf9e1876
IM
1/*
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 */
07800601 9#include "builtin.h"
bf9e1876 10
148be2c1
IM
11#include "util/exec_cmd.h"
12#include "util/cache.h"
13#include "util/quote.h"
14#include "util/run-command.h"
5beeded1 15#include "util/parse-events.h"
7335399a 16#include "util/parse-options.h"
bbb2cea7 17#include "util/debug.h"
553873e1 18#include <api/fs/debugfs.h>
27683dc5 19#include <pthread.h>
07800601
IM
20
21const char perf_usage_string[] =
78a1b503 22 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
07800601
IM
23
24const char perf_more_info_string[] =
25 "See 'perf help COMMAND' for more information on a specific command.";
26
5d06e691 27int use_browser = -1;
07800601 28static int use_pager = -1;
70cb4e96 29const char *input_name;
5d06e691 30
98a4179c
FW
31struct cmd_struct {
32 const char *cmd;
33 int (*fn)(int, const char **, const char *);
34 int option;
35};
36
37static struct cmd_struct commands[] = {
38 { "buildid-cache", cmd_buildid_cache, 0 },
39 { "buildid-list", cmd_buildid_list, 0 },
40 { "diff", cmd_diff, 0 },
41 { "evlist", cmd_evlist, 0 },
42 { "help", cmd_help, 0 },
43 { "list", cmd_list, 0 },
44 { "record", cmd_record, 0 },
45 { "report", cmd_report, 0 },
46 { "bench", cmd_bench, 0 },
47 { "stat", cmd_stat, 0 },
48 { "timechart", cmd_timechart, 0 },
49 { "top", cmd_top, 0 },
50 { "annotate", cmd_annotate, 0 },
51 { "version", cmd_version, 0 },
52 { "script", cmd_script, 0 },
53 { "sched", cmd_sched, 0 },
89fe808a 54#ifdef HAVE_LIBELF_SUPPORT
98a4179c 55 { "probe", cmd_probe, 0 },
393be2e3 56#endif
98a4179c
FW
57 { "kmem", cmd_kmem, 0 },
58 { "lock", cmd_lock, 0 },
59 { "kvm", cmd_kvm, 0 },
60 { "test", cmd_test, 0 },
89fe808a 61#ifdef HAVE_LIBAUDIT_SUPPORT
514f1c67 62 { "trace", cmd_trace, 0 },
4d29089c 63#endif
98a4179c 64 { "inject", cmd_inject, 0 },
028f12ee 65 { "mem", cmd_mem, 0 },
2245bf14 66 { "data", cmd_data, 0 },
98a4179c
FW
67};
68
07800601
IM
69struct pager_config {
70 const char *cmd;
71 int val;
72};
73
74static int pager_command_config(const char *var, const char *value, void *data)
75{
76 struct pager_config *c = data;
77 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
78 c->val = perf_config_bool(var, value);
79 return 0;
80}
81
82/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
83int check_pager_config(const char *cmd)
84{
85 struct pager_config c;
86 c.cmd = cmd;
87 c.val = -1;
88 perf_config(pager_command_config, &c);
89 return c.val;
90}
91
0020ce23 92static int browser_command_config(const char *var, const char *value, void *data)
5d06e691
ACM
93{
94 struct pager_config *c = data;
95 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
96 c->val = perf_config_bool(var, value);
0020ce23
NK
97 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
98 c->val = perf_config_bool(var, value) ? 2 : 0;
5d06e691
ACM
99 return 0;
100}
101
0020ce23
NK
102/*
103 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
104 * and -1 for "not specified"
105 */
106static int check_browser_config(const char *cmd)
5d06e691
ACM
107{
108 struct pager_config c;
109 c.cmd = cmd;
110 c.val = -1;
0020ce23 111 perf_config(browser_command_config, &c);
5d06e691
ACM
112 return c.val;
113}
114
4c574159
TF
115static void commit_pager_choice(void)
116{
07800601
IM
117 switch (use_pager) {
118 case 0:
119 setenv("PERF_PAGER", "cat", 1);
120 break;
121 case 1:
122 /* setup_pager(); */
123 break;
124 default:
125 break;
126 }
127}
128
7335399a
YS
129struct option options[] = {
130 OPT_ARGUMENT("help", "help"),
131 OPT_ARGUMENT("version", "version"),
132 OPT_ARGUMENT("exec-path", "exec-path"),
133 OPT_ARGUMENT("html-path", "html-path"),
134 OPT_ARGUMENT("paginate", "paginate"),
135 OPT_ARGUMENT("no-pager", "no-pager"),
136 OPT_ARGUMENT("perf-dir", "perf-dir"),
137 OPT_ARGUMENT("work-tree", "work-tree"),
138 OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
139 OPT_ARGUMENT("buildid-dir", "buildid-dir"),
140 OPT_ARGUMENT("list-cmds", "list-cmds"),
141 OPT_ARGUMENT("list-opts", "list-opts"),
142 OPT_ARGUMENT("debug", "debug"),
143 OPT_END()
144};
145
4c574159 146static int handle_options(const char ***argv, int *argc, int *envchanged)
07800601
IM
147{
148 int handled = 0;
149
150 while (*argc > 0) {
151 const char *cmd = (*argv)[0];
152 if (cmd[0] != '-')
153 break;
154
155 /*
156 * For legacy reasons, the "version" and "help"
157 * commands can be written with "--" prepended
158 * to make them look like flags.
159 */
160 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
161 break;
162
163 /*
164 * Check remaining flags.
165 */
cfed95a6
VL
166 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
167 cmd += strlen(CMD_EXEC_PATH);
07800601
IM
168 if (*cmd == '=')
169 perf_set_argv_exec_path(cmd + 1);
170 else {
171 puts(perf_exec_path());
172 exit(0);
173 }
174 } else if (!strcmp(cmd, "--html-path")) {
175 puts(system_path(PERF_HTML_PATH));
176 exit(0);
177 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
178 use_pager = 1;
179 } else if (!strcmp(cmd, "--no-pager")) {
180 use_pager = 0;
181 if (envchanged)
182 *envchanged = 1;
183 } else if (!strcmp(cmd, "--perf-dir")) {
184 if (*argc < 2) {
4c574159 185 fprintf(stderr, "No directory given for --perf-dir.\n");
07800601
IM
186 usage(perf_usage_string);
187 }
188 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
189 if (envchanged)
190 *envchanged = 1;
191 (*argv)++;
192 (*argc)--;
193 handled++;
cfed95a6
VL
194 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
195 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
07800601
IM
196 if (envchanged)
197 *envchanged = 1;
198 } else if (!strcmp(cmd, "--work-tree")) {
199 if (*argc < 2) {
4c574159 200 fprintf(stderr, "No directory given for --work-tree.\n");
07800601
IM
201 usage(perf_usage_string);
202 }
203 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
204 if (envchanged)
205 *envchanged = 1;
206 (*argv)++;
207 (*argc)--;
cfed95a6
VL
208 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
209 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
07800601
IM
210 if (envchanged)
211 *envchanged = 1;
5beeded1
JB
212 } else if (!strcmp(cmd, "--debugfs-dir")) {
213 if (*argc < 2) {
214 fprintf(stderr, "No directory given for --debugfs-dir.\n");
215 usage(perf_usage_string);
216 }
1355915a 217 perf_debugfs_set_path((*argv)[1]);
5beeded1
JB
218 if (envchanged)
219 *envchanged = 1;
220 (*argv)++;
221 (*argc)--;
99ce8e9f
JO
222 } else if (!strcmp(cmd, "--buildid-dir")) {
223 if (*argc < 2) {
224 fprintf(stderr, "No directory given for --buildid-dir.\n");
225 usage(perf_usage_string);
226 }
227 set_buildid_dir((*argv)[1]);
228 if (envchanged)
229 *envchanged = 1;
230 (*argv)++;
231 (*argc)--;
cfed95a6 232 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
1355915a 233 perf_debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
9f30fffc 234 fprintf(stderr, "dir: %s\n", tracing_path);
5beeded1
JB
235 if (envchanged)
236 *envchanged = 1;
98a4179c
FW
237 } else if (!strcmp(cmd, "--list-cmds")) {
238 unsigned int i;
239
240 for (i = 0; i < ARRAY_SIZE(commands); i++) {
241 struct cmd_struct *p = commands+i;
242 printf("%s ", p->cmd);
243 }
ed457520 244 putchar('\n');
98a4179c 245 exit(0);
7335399a
YS
246 } else if (!strcmp(cmd, "--list-opts")) {
247 unsigned int i;
248
249 for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
250 struct option *p = options+i;
251 printf("--%s ", p->long_name);
252 }
253 putchar('\n');
254 exit(0);
bbb2cea7
JO
255 } else if (!strcmp(cmd, "--debug")) {
256 if (*argc < 2) {
257 fprintf(stderr, "No variable specified for --debug.\n");
258 usage(perf_usage_string);
259 }
260 if (perf_debug_option((*argv)[1]))
261 usage(perf_usage_string);
262
263 (*argv)++;
264 (*argc)--;
07800601
IM
265 } else {
266 fprintf(stderr, "Unknown option: %s\n", cmd);
267 usage(perf_usage_string);
268 }
269
270 (*argv)++;
271 (*argc)--;
272 handled++;
273 }
274 return handled;
275}
276
277static int handle_alias(int *argcp, const char ***argv)
278{
279 int envchanged = 0, ret = 0, saved_errno = errno;
280 int count, option_count;
4c574159 281 const char **new_argv;
07800601
IM
282 const char *alias_command;
283 char *alias_string;
07800601
IM
284
285 alias_command = (*argv)[0];
286 alias_string = alias_lookup(alias_command);
287 if (alias_string) {
288 if (alias_string[0] == '!') {
289 if (*argcp > 1) {
290 struct strbuf buf;
291
292 strbuf_init(&buf, PATH_MAX);
293 strbuf_addstr(&buf, alias_string);
294 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
295 free(alias_string);
296 alias_string = buf.buf;
297 }
298 ret = system(alias_string + 1);
299 if (ret >= 0 && WIFEXITED(ret) &&
300 WEXITSTATUS(ret) != 127)
301 exit(WEXITSTATUS(ret));
302 die("Failed to run '%s' when expanding alias '%s'",
303 alias_string + 1, alias_command);
304 }
305 count = split_cmdline(alias_string, &new_argv);
306 if (count < 0)
307 die("Bad alias.%s string", alias_command);
308 option_count = handle_options(&new_argv, &count, &envchanged);
309 if (envchanged)
310 die("alias '%s' changes environment variables\n"
311 "You can use '!perf' in the alias to do this.",
312 alias_command);
313 memmove(new_argv - option_count, new_argv,
314 count * sizeof(char *));
315 new_argv -= option_count;
316
317 if (count < 1)
318 die("empty alias for %s", alias_command);
319
320 if (!strcmp(alias_command, new_argv[0]))
321 die("recursive alias: %s", alias_command);
322
4c574159 323 new_argv = realloc(new_argv, sizeof(char *) *
07800601
IM
324 (count + *argcp + 1));
325 /* insert after command name */
4c574159
TF
326 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
327 new_argv[count + *argcp] = NULL;
07800601
IM
328
329 *argv = new_argv;
330 *argcp += count - 1;
331
332 ret = 1;
333 }
334
335 errno = saved_errno;
336
337 return ret;
338}
339
340const char perf_version_string[] = PERF_VERSION;
341
342#define RUN_SETUP (1<<0)
343#define USE_PAGER (1<<1)
344/*
345 * require working tree to be present -- anything uses this needs
346 * RUN_SETUP for reading from the configuration file.
347 */
348#define NEED_WORK_TREE (1<<2)
349
07800601
IM
350static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
351{
352 int status;
353 struct stat st;
354 const char *prefix;
b2348e1d 355 char sbuf[STRERR_BUFSIZE];
07800601
IM
356
357 prefix = NULL;
358 if (p->option & RUN_SETUP)
359 prefix = NULL; /* setup_perf_directory(); */
360
5d06e691 361 if (use_browser == -1)
0020ce23 362 use_browser = check_browser_config(p->cmd);
5d06e691 363
07800601
IM
364 if (use_pager == -1 && p->option & RUN_SETUP)
365 use_pager = check_pager_config(p->cmd);
366 if (use_pager == -1 && p->option & USE_PAGER)
367 use_pager = 1;
368 commit_pager_choice();
369
07800601 370 status = p->fn(argc, argv, prefix);
f3a1f0ea
ACM
371 exit_browser(status);
372
07800601
IM
373 if (status)
374 return status & 0xff;
375
376 /* Somebody closed stdout? */
377 if (fstat(fileno(stdout), &st))
378 return 0;
379 /* Ignore write errors for pipes and sockets.. */
380 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
381 return 0;
382
2a16bf8c 383 status = 1;
07800601 384 /* Check for ENOSPC and EIO errors.. */
2a16bf8c 385 if (fflush(stdout)) {
b2348e1d
MH
386 fprintf(stderr, "write failure on standard output: %s",
387 strerror_r(errno, sbuf, sizeof(sbuf)));
2a16bf8c
ACM
388 goto out;
389 }
390 if (ferror(stdout)) {
391 fprintf(stderr, "unknown write failure on standard output");
392 goto out;
393 }
394 if (fclose(stdout)) {
b2348e1d
MH
395 fprintf(stderr, "close failed on standard output: %s",
396 strerror_r(errno, sbuf, sizeof(sbuf)));
2a16bf8c
ACM
397 goto out;
398 }
399 status = 0;
400out:
401 return status;
07800601
IM
402}
403
404static void handle_internal_command(int argc, const char **argv)
405{
406 const char *cmd = argv[0];
f37a291c 407 unsigned int i;
07800601
IM
408 static const char ext[] = STRIP_EXTENSION;
409
410 if (sizeof(ext) > 1) {
411 i = strlen(argv[0]) - strlen(ext);
412 if (i > 0 && !strcmp(argv[0] + i, ext)) {
413 char *argv0 = strdup(argv[0]);
414 argv[0] = cmd = argv0;
415 argv0[i] = '\0';
416 }
417 }
418
419 /* Turn "perf cmd --help" into "perf help cmd" */
420 if (argc > 1 && !strcmp(argv[1], "--help")) {
421 argv[1] = argv[0];
422 argv[0] = cmd = "help";
423 }
424
425 for (i = 0; i < ARRAY_SIZE(commands); i++) {
426 struct cmd_struct *p = commands+i;
427 if (strcmp(p->cmd, cmd))
428 continue;
429 exit(run_builtin(p, argc, argv));
430 }
431}
432
433static void execv_dashed_external(const char **argv)
434{
435 struct strbuf cmd = STRBUF_INIT;
436 const char *tmp;
437 int status;
438
439 strbuf_addf(&cmd, "perf-%s", argv[0]);
440
441 /*
442 * argv[0] must be the perf command, but the argv array
443 * belongs to the caller, and may be reused in
444 * subsequent loop iterations. Save argv[0] and
445 * restore it on error.
446 */
447 tmp = argv[0];
448 argv[0] = cmd.buf;
449
450 /*
451 * if we fail because the command is not found, it is
452 * OK to return. Otherwise, we just pass along the status code.
453 */
454 status = run_command_v_opt(argv, 0);
455 if (status != -ERR_RUN_COMMAND_EXEC) {
456 if (IS_RUN_COMMAND_ERR(status))
457 die("unable to run '%s'", argv[0]);
458 exit(-status);
459 }
460 errno = ENOENT; /* as if we called execvp */
461
462 argv[0] = tmp;
463
464 strbuf_release(&cmd);
465}
466
467static int run_argv(int *argcp, const char ***argv)
468{
469 int done_alias = 0;
470
471 while (1) {
472 /* See if it's an internal command */
473 handle_internal_command(*argcp, *argv);
474
475 /* .. then try the external ones */
476 execv_dashed_external(*argv);
477
478 /* It could be an alias -- this works around the insanity
479 * of overriding "perf log" with "perf show" by having
480 * alias.log = show
481 */
482 if (done_alias || !handle_alias(argcp, argv))
483 break;
484 done_alias = 1;
485 }
486
487 return done_alias;
488}
489
3af6e338
ACM
490static void pthread__block_sigwinch(void)
491{
492 sigset_t set;
493
494 sigemptyset(&set);
495 sigaddset(&set, SIGWINCH);
496 pthread_sigmask(SIG_BLOCK, &set, NULL);
497}
498
499void pthread__unblock_sigwinch(void)
500{
501 sigset_t set;
502
503 sigemptyset(&set);
504 sigaddset(&set, SIGWINCH);
505 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
506}
507
07800601
IM
508int main(int argc, const char **argv)
509{
510 const char *cmd;
b2348e1d 511 char sbuf[STRERR_BUFSIZE];
07800601 512
918512b4 513 /* The page_size is placed in util object. */
0c1fe6b2 514 page_size = sysconf(_SC_PAGE_SIZE);
2b1b7100 515 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
0c1fe6b2 516
07800601
IM
517 cmd = perf_extract_argv0_path(argv[0]);
518 if (!cmd)
519 cmd = "perf-help";
5beeded1 520 /* get debugfs mount point from /proc/mounts */
1355915a 521 perf_debugfs_mount(NULL);
07800601
IM
522 /*
523 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
524 *
525 * - cannot take flags in between the "perf" and the "xxxx".
526 * - cannot execute it externally (since it would just do
527 * the same thing over again)
528 *
529 * So we just directly call the internal command handler, and
530 * die if that one cannot handle it.
531 */
532 if (!prefixcmp(cmd, "perf-")) {
266dfb0b 533 cmd += 5;
07800601
IM
534 argv[0] = cmd;
535 handle_internal_command(argc, argv);
2a16bf8c
ACM
536 fprintf(stderr, "cannot handle %s internally", cmd);
537 goto out;
07800601 538 }
b52bc234 539 if (!prefixcmp(cmd, "trace")) {
1b572622 540#ifdef HAVE_LIBAUDIT_SUPPORT
99ce8e9f 541 set_buildid_dir(NULL);
b52bc234
ACM
542 setup_path();
543 argv[0] = "trace";
544 return cmd_trace(argc, argv, NULL);
1b572622
ACM
545#else
546 fprintf(stderr,
547 "trace command not available: missing audit-libs devel package at build time.\n");
548 goto out;
b52bc234 549#endif
1b572622 550 }
07800601
IM
551 /* Look for flags.. */
552 argv++;
553 argc--;
554 handle_options(&argv, &argc, NULL);
555 commit_pager_choice();
99ce8e9f 556 set_buildid_dir(NULL);
45de34bb 557
07800601
IM
558 if (argc > 0) {
559 if (!prefixcmp(argv[0], "--"))
560 argv[0] += 2;
561 } else {
562 /* The user didn't specify a command; give them help */
502fc5c7 563 printf("\n usage: %s\n\n", perf_usage_string);
07800601 564 list_common_cmds_help();
502fc5c7 565 printf("\n %s\n\n", perf_more_info_string);
2a16bf8c 566 goto out;
07800601
IM
567 }
568 cmd = argv[0];
569
52502bf2
JO
570 test_attr__init();
571
07800601
IM
572 /*
573 * We use PATH to find perf commands, but we prepend some higher
659431fc 574 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
07800601
IM
575 * environment, and the $(perfexecdir) from the Makefile at build
576 * time.
577 */
578 setup_path();
3af6e338
ACM
579 /*
580 * Block SIGWINCH notifications so that the thread that wants it can
581 * unblock and get syscalls like select interrupted instead of waiting
582 * forever while the signal goes to some other non interested thread.
583 */
584 pthread__block_sigwinch();
07800601
IM
585
586 while (1) {
4c574159 587 static int done_help;
b5ded713 588 int was_alias = run_argv(&argc, &argv);
8035e428 589
07800601
IM
590 if (errno != ENOENT)
591 break;
8035e428 592
07800601
IM
593 if (was_alias) {
594 fprintf(stderr, "Expansion of alias '%s' failed; "
595 "'%s' is not a perf-command\n",
596 cmd, argv[0]);
2a16bf8c 597 goto out;
07800601
IM
598 }
599 if (!done_help) {
600 cmd = argv[0] = help_unknown_cmd(cmd);
601 done_help = 1;
602 } else
603 break;
604 }
605
606 fprintf(stderr, "Failed to run command '%s': %s\n",
b2348e1d 607 cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
2a16bf8c 608out:
07800601
IM
609 return 1;
610}
This page took 0.254555 seconds and 5 git commands to generate.