dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / tools / perf / perf.c
blob3d4c7c09adeae9afeeccf69161882b2fc65f9e41
1 /*
2 * perf.c
4 * Performance analysis utility.
6 * This is the main hub from which the sub-commands (perf stat,
7 * perf top, perf record, perf report, etc.) are started.
8 */
9 #include "builtin.h"
11 #include "util/env.h"
12 #include "util/exec_cmd.h"
13 #include "util/cache.h"
14 #include "util/quote.h"
15 #include "util/run-command.h"
16 #include "util/parse-events.h"
17 #include "util/parse-options.h"
18 #include "util/bpf-loader.h"
19 #include "util/debug.h"
20 #include <api/fs/tracing_path.h>
21 #include <pthread.h>
23 const char perf_usage_string[] =
24 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
26 const char perf_more_info_string[] =
27 "See 'perf help COMMAND' for more information on a specific command.";
29 int use_browser = -1;
30 static int use_pager = -1;
31 const char *input_name;
33 struct cmd_struct {
34 const char *cmd;
35 int (*fn)(int, const char **, const char *);
36 int option;
39 static struct cmd_struct commands[] = {
40 { "buildid-cache", cmd_buildid_cache, 0 },
41 { "buildid-list", cmd_buildid_list, 0 },
42 { "diff", cmd_diff, 0 },
43 { "evlist", cmd_evlist, 0 },
44 { "help", cmd_help, 0 },
45 { "list", cmd_list, 0 },
46 { "record", cmd_record, 0 },
47 { "report", cmd_report, 0 },
48 { "bench", cmd_bench, 0 },
49 { "stat", cmd_stat, 0 },
50 { "timechart", cmd_timechart, 0 },
51 { "top", cmd_top, 0 },
52 { "annotate", cmd_annotate, 0 },
53 { "version", cmd_version, 0 },
54 { "script", cmd_script, 0 },
55 { "sched", cmd_sched, 0 },
56 #ifdef HAVE_LIBELF_SUPPORT
57 { "probe", cmd_probe, 0 },
58 #endif
59 { "kmem", cmd_kmem, 0 },
60 { "lock", cmd_lock, 0 },
61 { "kvm", cmd_kvm, 0 },
62 { "test", cmd_test, 0 },
63 #ifdef HAVE_LIBAUDIT_SUPPORT
64 { "trace", cmd_trace, 0 },
65 #endif
66 { "inject", cmd_inject, 0 },
67 { "mem", cmd_mem, 0 },
68 { "data", cmd_data, 0 },
71 struct pager_config {
72 const char *cmd;
73 int val;
76 static int pager_command_config(const char *var, const char *value, void *data)
78 struct pager_config *c = data;
79 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
80 c->val = perf_config_bool(var, value);
81 return 0;
84 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
85 int check_pager_config(const char *cmd)
87 struct pager_config c;
88 c.cmd = cmd;
89 c.val = -1;
90 perf_config(pager_command_config, &c);
91 return c.val;
94 static int browser_command_config(const char *var, const char *value, void *data)
96 struct pager_config *c = data;
97 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
98 c->val = perf_config_bool(var, value);
99 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
100 c->val = perf_config_bool(var, value) ? 2 : 0;
101 return 0;
105 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
106 * and -1 for "not specified"
108 static int check_browser_config(const char *cmd)
110 struct pager_config c;
111 c.cmd = cmd;
112 c.val = -1;
113 perf_config(browser_command_config, &c);
114 return c.val;
117 static void commit_pager_choice(void)
119 switch (use_pager) {
120 case 0:
121 setenv("PERF_PAGER", "cat", 1);
122 break;
123 case 1:
124 /* setup_pager(); */
125 break;
126 default:
127 break;
131 struct option options[] = {
132 OPT_ARGUMENT("help", "help"),
133 OPT_ARGUMENT("version", "version"),
134 OPT_ARGUMENT("exec-path", "exec-path"),
135 OPT_ARGUMENT("html-path", "html-path"),
136 OPT_ARGUMENT("paginate", "paginate"),
137 OPT_ARGUMENT("no-pager", "no-pager"),
138 OPT_ARGUMENT("perf-dir", "perf-dir"),
139 OPT_ARGUMENT("work-tree", "work-tree"),
140 OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
141 OPT_ARGUMENT("buildid-dir", "buildid-dir"),
142 OPT_ARGUMENT("list-cmds", "list-cmds"),
143 OPT_ARGUMENT("list-opts", "list-opts"),
144 OPT_ARGUMENT("debug", "debug"),
145 OPT_END()
148 static int handle_options(const char ***argv, int *argc, int *envchanged)
150 int handled = 0;
152 while (*argc > 0) {
153 const char *cmd = (*argv)[0];
154 if (cmd[0] != '-')
155 break;
158 * For legacy reasons, the "version" and "help"
159 * commands can be written with "--" prepended
160 * to make them look like flags.
162 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
163 break;
166 * Shortcut for '-h' and '-v' options to invoke help
167 * and version command.
169 if (!strcmp(cmd, "-h")) {
170 (*argv)[0] = "--help";
171 break;
174 if (!strcmp(cmd, "-v")) {
175 (*argv)[0] = "--version";
176 break;
180 * Check remaining flags.
182 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
183 cmd += strlen(CMD_EXEC_PATH);
184 if (*cmd == '=')
185 perf_set_argv_exec_path(cmd + 1);
186 else {
187 puts(perf_exec_path());
188 exit(0);
190 } else if (!strcmp(cmd, "--html-path")) {
191 puts(system_path(PERF_HTML_PATH));
192 exit(0);
193 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
194 use_pager = 1;
195 } else if (!strcmp(cmd, "--no-pager")) {
196 use_pager = 0;
197 if (envchanged)
198 *envchanged = 1;
199 } else if (!strcmp(cmd, "--perf-dir")) {
200 if (*argc < 2) {
201 fprintf(stderr, "No directory given for --perf-dir.\n");
202 usage(perf_usage_string);
204 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
205 if (envchanged)
206 *envchanged = 1;
207 (*argv)++;
208 (*argc)--;
209 handled++;
210 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
211 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
212 if (envchanged)
213 *envchanged = 1;
214 } else if (!strcmp(cmd, "--work-tree")) {
215 if (*argc < 2) {
216 fprintf(stderr, "No directory given for --work-tree.\n");
217 usage(perf_usage_string);
219 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
220 if (envchanged)
221 *envchanged = 1;
222 (*argv)++;
223 (*argc)--;
224 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
225 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
226 if (envchanged)
227 *envchanged = 1;
228 } else if (!strcmp(cmd, "--debugfs-dir")) {
229 if (*argc < 2) {
230 fprintf(stderr, "No directory given for --debugfs-dir.\n");
231 usage(perf_usage_string);
233 tracing_path_set((*argv)[1]);
234 if (envchanged)
235 *envchanged = 1;
236 (*argv)++;
237 (*argc)--;
238 } else if (!strcmp(cmd, "--buildid-dir")) {
239 if (*argc < 2) {
240 fprintf(stderr, "No directory given for --buildid-dir.\n");
241 usage(perf_usage_string);
243 set_buildid_dir((*argv)[1]);
244 if (envchanged)
245 *envchanged = 1;
246 (*argv)++;
247 (*argc)--;
248 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
249 tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
250 fprintf(stderr, "dir: %s\n", tracing_path);
251 if (envchanged)
252 *envchanged = 1;
253 } else if (!strcmp(cmd, "--list-cmds")) {
254 unsigned int i;
256 for (i = 0; i < ARRAY_SIZE(commands); i++) {
257 struct cmd_struct *p = commands+i;
258 printf("%s ", p->cmd);
260 putchar('\n');
261 exit(0);
262 } else if (!strcmp(cmd, "--list-opts")) {
263 unsigned int i;
265 for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
266 struct option *p = options+i;
267 printf("--%s ", p->long_name);
269 putchar('\n');
270 exit(0);
271 } else if (!strcmp(cmd, "--debug")) {
272 if (*argc < 2) {
273 fprintf(stderr, "No variable specified for --debug.\n");
274 usage(perf_usage_string);
276 if (perf_debug_option((*argv)[1]))
277 usage(perf_usage_string);
279 (*argv)++;
280 (*argc)--;
281 } else {
282 fprintf(stderr, "Unknown option: %s\n", cmd);
283 usage(perf_usage_string);
286 (*argv)++;
287 (*argc)--;
288 handled++;
290 return handled;
293 static int handle_alias(int *argcp, const char ***argv)
295 int envchanged = 0, ret = 0, saved_errno = errno;
296 int count, option_count;
297 const char **new_argv;
298 const char *alias_command;
299 char *alias_string;
301 alias_command = (*argv)[0];
302 alias_string = alias_lookup(alias_command);
303 if (alias_string) {
304 if (alias_string[0] == '!') {
305 if (*argcp > 1) {
306 struct strbuf buf;
308 strbuf_init(&buf, PATH_MAX);
309 strbuf_addstr(&buf, alias_string);
310 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
311 free(alias_string);
312 alias_string = buf.buf;
314 ret = system(alias_string + 1);
315 if (ret >= 0 && WIFEXITED(ret) &&
316 WEXITSTATUS(ret) != 127)
317 exit(WEXITSTATUS(ret));
318 die("Failed to run '%s' when expanding alias '%s'",
319 alias_string + 1, alias_command);
321 count = split_cmdline(alias_string, &new_argv);
322 if (count < 0)
323 die("Bad alias.%s string", alias_command);
324 option_count = handle_options(&new_argv, &count, &envchanged);
325 if (envchanged)
326 die("alias '%s' changes environment variables\n"
327 "You can use '!perf' in the alias to do this.",
328 alias_command);
329 memmove(new_argv - option_count, new_argv,
330 count * sizeof(char *));
331 new_argv -= option_count;
333 if (count < 1)
334 die("empty alias for %s", alias_command);
336 if (!strcmp(alias_command, new_argv[0]))
337 die("recursive alias: %s", alias_command);
339 new_argv = realloc(new_argv, sizeof(char *) *
340 (count + *argcp + 1));
341 /* insert after command name */
342 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
343 new_argv[count + *argcp] = NULL;
345 *argv = new_argv;
346 *argcp += count - 1;
348 ret = 1;
351 errno = saved_errno;
353 return ret;
356 const char perf_version_string[] = PERF_VERSION;
358 #define RUN_SETUP (1<<0)
359 #define USE_PAGER (1<<1)
361 * require working tree to be present -- anything uses this needs
362 * RUN_SETUP for reading from the configuration file.
364 #define NEED_WORK_TREE (1<<2)
366 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
368 int status;
369 struct stat st;
370 const char *prefix;
371 char sbuf[STRERR_BUFSIZE];
373 prefix = NULL;
374 if (p->option & RUN_SETUP)
375 prefix = NULL; /* setup_perf_directory(); */
377 if (use_browser == -1)
378 use_browser = check_browser_config(p->cmd);
380 if (use_pager == -1 && p->option & RUN_SETUP)
381 use_pager = check_pager_config(p->cmd);
382 if (use_pager == -1 && p->option & USE_PAGER)
383 use_pager = 1;
384 commit_pager_choice();
386 status = p->fn(argc, argv, prefix);
387 exit_browser(status);
388 perf_env__exit(&perf_env);
389 bpf__clear();
391 if (status)
392 return status & 0xff;
394 /* Somebody closed stdout? */
395 if (fstat(fileno(stdout), &st))
396 return 0;
397 /* Ignore write errors for pipes and sockets.. */
398 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
399 return 0;
401 status = 1;
402 /* Check for ENOSPC and EIO errors.. */
403 if (fflush(stdout)) {
404 fprintf(stderr, "write failure on standard output: %s",
405 strerror_r(errno, sbuf, sizeof(sbuf)));
406 goto out;
408 if (ferror(stdout)) {
409 fprintf(stderr, "unknown write failure on standard output");
410 goto out;
412 if (fclose(stdout)) {
413 fprintf(stderr, "close failed on standard output: %s",
414 strerror_r(errno, sbuf, sizeof(sbuf)));
415 goto out;
417 status = 0;
418 out:
419 return status;
422 static void handle_internal_command(int argc, const char **argv)
424 const char *cmd = argv[0];
425 unsigned int i;
426 static const char ext[] = STRIP_EXTENSION;
428 if (sizeof(ext) > 1) {
429 i = strlen(argv[0]) - strlen(ext);
430 if (i > 0 && !strcmp(argv[0] + i, ext)) {
431 char *argv0 = strdup(argv[0]);
432 argv[0] = cmd = argv0;
433 argv0[i] = '\0';
437 /* Turn "perf cmd --help" into "perf help cmd" */
438 if (argc > 1 && !strcmp(argv[1], "--help")) {
439 argv[1] = argv[0];
440 argv[0] = cmd = "help";
443 for (i = 0; i < ARRAY_SIZE(commands); i++) {
444 struct cmd_struct *p = commands+i;
445 if (strcmp(p->cmd, cmd))
446 continue;
447 exit(run_builtin(p, argc, argv));
451 static void execv_dashed_external(const char **argv)
453 struct strbuf cmd = STRBUF_INIT;
454 const char *tmp;
455 int status;
457 strbuf_addf(&cmd, "perf-%s", argv[0]);
460 * argv[0] must be the perf command, but the argv array
461 * belongs to the caller, and may be reused in
462 * subsequent loop iterations. Save argv[0] and
463 * restore it on error.
465 tmp = argv[0];
466 argv[0] = cmd.buf;
469 * if we fail because the command is not found, it is
470 * OK to return. Otherwise, we just pass along the status code.
472 status = run_command_v_opt(argv, 0);
473 if (status != -ERR_RUN_COMMAND_EXEC) {
474 if (IS_RUN_COMMAND_ERR(status))
475 die("unable to run '%s'", argv[0]);
476 exit(-status);
478 errno = ENOENT; /* as if we called execvp */
480 argv[0] = tmp;
482 strbuf_release(&cmd);
485 static int run_argv(int *argcp, const char ***argv)
487 int done_alias = 0;
489 while (1) {
490 /* See if it's an internal command */
491 handle_internal_command(*argcp, *argv);
493 /* .. then try the external ones */
494 execv_dashed_external(*argv);
496 /* It could be an alias -- this works around the insanity
497 * of overriding "perf log" with "perf show" by having
498 * alias.log = show
500 if (done_alias || !handle_alias(argcp, argv))
501 break;
502 done_alias = 1;
505 return done_alias;
508 static void pthread__block_sigwinch(void)
510 sigset_t set;
512 sigemptyset(&set);
513 sigaddset(&set, SIGWINCH);
514 pthread_sigmask(SIG_BLOCK, &set, NULL);
517 void pthread__unblock_sigwinch(void)
519 sigset_t set;
521 sigemptyset(&set);
522 sigaddset(&set, SIGWINCH);
523 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
526 int main(int argc, const char **argv)
528 const char *cmd;
529 char sbuf[STRERR_BUFSIZE];
531 /* The page_size is placed in util object. */
532 page_size = sysconf(_SC_PAGE_SIZE);
533 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
535 cmd = perf_extract_argv0_path(argv[0]);
536 if (!cmd)
537 cmd = "perf-help";
539 /* get debugfs/tracefs mount point from /proc/mounts */
540 tracing_path_mount();
543 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
545 * - cannot take flags in between the "perf" and the "xxxx".
546 * - cannot execute it externally (since it would just do
547 * the same thing over again)
549 * So we just directly call the internal command handler, and
550 * die if that one cannot handle it.
552 if (!prefixcmp(cmd, "perf-")) {
553 cmd += 5;
554 argv[0] = cmd;
555 handle_internal_command(argc, argv);
556 fprintf(stderr, "cannot handle %s internally", cmd);
557 goto out;
559 if (!prefixcmp(cmd, "trace")) {
560 #ifdef HAVE_LIBAUDIT_SUPPORT
561 set_buildid_dir(NULL);
562 setup_path();
563 argv[0] = "trace";
564 return cmd_trace(argc, argv, NULL);
565 #else
566 fprintf(stderr,
567 "trace command not available: missing audit-libs devel package at build time.\n");
568 goto out;
569 #endif
571 /* Look for flags.. */
572 argv++;
573 argc--;
574 handle_options(&argv, &argc, NULL);
575 commit_pager_choice();
576 set_buildid_dir(NULL);
578 if (argc > 0) {
579 if (!prefixcmp(argv[0], "--"))
580 argv[0] += 2;
581 } else {
582 /* The user didn't specify a command; give them help */
583 printf("\n usage: %s\n\n", perf_usage_string);
584 list_common_cmds_help();
585 printf("\n %s\n\n", perf_more_info_string);
586 goto out;
588 cmd = argv[0];
590 test_attr__init();
593 * We use PATH to find perf commands, but we prepend some higher
594 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
595 * environment, and the $(perfexecdir) from the Makefile at build
596 * time.
598 setup_path();
600 * Block SIGWINCH notifications so that the thread that wants it can
601 * unblock and get syscalls like select interrupted instead of waiting
602 * forever while the signal goes to some other non interested thread.
604 pthread__block_sigwinch();
606 while (1) {
607 static int done_help;
608 int was_alias = run_argv(&argc, &argv);
610 if (errno != ENOENT)
611 break;
613 if (was_alias) {
614 fprintf(stderr, "Expansion of alias '%s' failed; "
615 "'%s' is not a perf-command\n",
616 cmd, argv[0]);
617 goto out;
619 if (!done_help) {
620 cmd = argv[0] = help_unknown_cmd(cmd);
621 done_help = 1;
622 } else
623 break;
626 fprintf(stderr, "Failed to run command '%s': %s\n",
627 cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
628 out:
629 return 1;