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