blob: 3d4c7c09adeae9afeeccf69161882b2fc65f9e41 [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
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -030011#include "util/env.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020012#include "util/exec_cmd.h"
13#include "util/cache.h"
14#include "util/quote.h"
15#include "util/run-command.h"
Jason Baron5beeded2009-07-21 14:16:29 -040016#include "util/parse-events.h"
Yunlong Song73353992015-02-27 18:21:31 +080017#include "util/parse-options.h"
Wang Nan84c86ca2015-10-14 12:41:14 +000018#include "util/bpf-loader.h"
Jiri Olsabbb2cea2014-07-17 12:55:00 +020019#include "util/debug.h"
Jiri Olsa592d5a62015-09-02 09:56:34 +020020#include <api/fs/tracing_path.h>
Irina Tirdea27683dc2012-09-08 03:43:19 +030021#include <pthread.h>
Ingo Molnar07800602009-04-20 15:00:56 +020022
23const char perf_usage_string[] =
Jiri Olsa78a1b502014-07-18 09:11:30 +020024 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
Ingo Molnar07800602009-04-20 15:00:56 +020025
26const char perf_more_info_string[] =
27 "See 'perf help COMMAND' for more information on a specific command.";
28
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030029int use_browser = -1;
Ingo Molnar07800602009-04-20 15:00:56 +020030static int use_pager = -1;
Feng Tang70cb4e92012-10-30 11:56:02 +080031const char *input_name;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030032
Frederic Weisbecker98a41792012-08-09 16:31:51 +020033struct cmd_struct {
34 const char *cmd;
35 int (*fn)(int, const char **, const char *);
36 int option;
37};
38
39static 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 },
Ingo Molnar89fe8082013-09-30 12:07:11 +020056#ifdef HAVE_LIBELF_SUPPORT
Frederic Weisbecker98a41792012-08-09 16:31:51 +020057 { "probe", cmd_probe, 0 },
Namhyung Kim393be2e2012-08-06 13:41:21 +090058#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020059 { "kmem", cmd_kmem, 0 },
60 { "lock", cmd_lock, 0 },
61 { "kvm", cmd_kvm, 0 },
62 { "test", cmd_test, 0 },
Ingo Molnar89fe8082013-09-30 12:07:11 +020063#ifdef HAVE_LIBAUDIT_SUPPORT
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030064 { "trace", cmd_trace, 0 },
Namhyung Kim4d290892012-09-27 20:23:38 +090065#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020066 { "inject", cmd_inject, 0 },
Stephane Eranian028f12e2013-01-24 16:10:38 +010067 { "mem", cmd_mem, 0 },
Jiri Olsa2245bf12015-02-20 23:16:59 +010068 { "data", cmd_data, 0 },
Frederic Weisbecker98a41792012-08-09 16:31:51 +020069};
70
Ingo Molnar07800602009-04-20 15:00:56 +020071struct pager_config {
72 const char *cmd;
73 int val;
74};
75
76static int pager_command_config(const char *var, const char *value, void *data)
77{
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;
82}
83
84/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
85int check_pager_config(const char *cmd)
86{
87 struct pager_config c;
88 c.cmd = cmd;
89 c.val = -1;
90 perf_config(pager_command_config, &c);
91 return c.val;
92}
93
Namhyung Kim0020ce22012-11-12 11:50:17 +090094static int browser_command_config(const char *var, const char *value, void *data)
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030095{
96 struct pager_config *c = data;
97 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
98 c->val = perf_config_bool(var, value);
Namhyung Kim0020ce22012-11-12 11:50:17 +090099 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
100 c->val = perf_config_bool(var, value) ? 2 : 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300101 return 0;
102}
103
Namhyung Kim0020ce22012-11-12 11:50:17 +0900104/*
105 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
106 * and -1 for "not specified"
107 */
108static int check_browser_config(const char *cmd)
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300109{
110 struct pager_config c;
111 c.cmd = cmd;
112 c.val = -1;
Namhyung Kim0020ce22012-11-12 11:50:17 +0900113 perf_config(browser_command_config, &c);
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300114 return c.val;
115}
116
Thiago Farina4c574152010-01-27 21:05:55 -0200117static void commit_pager_choice(void)
118{
Ingo Molnar07800602009-04-20 15:00:56 +0200119 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;
128 }
129}
130
Yunlong Song73353992015-02-27 18:21:31 +0800131struct 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()
146};
147
Thiago Farina4c574152010-01-27 21:05:55 -0200148static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +0200149{
150 int handled = 0;
151
152 while (*argc > 0) {
153 const char *cmd = (*argv)[0];
154 if (cmd[0] != '-')
155 break;
156
157 /*
158 * For legacy reasons, the "version" and "help"
159 * commands can be written with "--" prepended
160 * to make them look like flags.
161 */
162 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
163 break;
164
165 /*
Jiri Olsaa1853e22015-10-05 20:06:09 +0200166 * Shortcut for '-h' and '-v' options to invoke help
167 * and version command.
168 */
169 if (!strcmp(cmd, "-h")) {
170 (*argv)[0] = "--help";
171 break;
172 }
173
174 if (!strcmp(cmd, "-v")) {
175 (*argv)[0] = "--version";
176 break;
177 }
178
179 /*
Ingo Molnar07800602009-04-20 15:00:56 +0200180 * Check remaining flags.
181 */
Vincent Legollcfed95a2009-10-13 10:18:16 +0200182 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
183 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +0200184 if (*cmd == '=')
185 perf_set_argv_exec_path(cmd + 1);
186 else {
187 puts(perf_exec_path());
188 exit(0);
189 }
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) {
Thiago Farina4c574152010-01-27 21:05:55 -0200201 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200202 usage(perf_usage_string);
203 }
204 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
205 if (envchanged)
206 *envchanged = 1;
207 (*argv)++;
208 (*argc)--;
209 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200210 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
211 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200212 if (envchanged)
213 *envchanged = 1;
214 } else if (!strcmp(cmd, "--work-tree")) {
215 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200216 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200217 usage(perf_usage_string);
218 }
219 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
220 if (envchanged)
221 *envchanged = 1;
222 (*argv)++;
223 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200224 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
225 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200226 if (envchanged)
227 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400228 } 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);
232 }
Jiri Olsa65d4b262015-09-02 09:56:33 +0200233 tracing_path_set((*argv)[1]);
Jason Baron5beeded2009-07-21 14:16:29 -0400234 if (envchanged)
235 *envchanged = 1;
236 (*argv)++;
237 (*argc)--;
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100238 } 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);
242 }
243 set_buildid_dir((*argv)[1]);
244 if (envchanged)
245 *envchanged = 1;
246 (*argv)++;
247 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200248 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
Jiri Olsa65d4b262015-09-02 09:56:33 +0200249 tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
Jiri Olsa9f30fff2015-08-26 15:46:45 +0200250 fprintf(stderr, "dir: %s\n", tracing_path);
Jason Baron5beeded2009-07-21 14:16:29 -0400251 if (envchanged)
252 *envchanged = 1;
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200253 } else if (!strcmp(cmd, "--list-cmds")) {
254 unsigned int i;
255
256 for (i = 0; i < ARRAY_SIZE(commands); i++) {
257 struct cmd_struct *p = commands+i;
258 printf("%s ", p->cmd);
259 }
Yunlong Songed457522015-02-27 18:21:29 +0800260 putchar('\n');
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200261 exit(0);
Yunlong Song73353992015-02-27 18:21:31 +0800262 } else if (!strcmp(cmd, "--list-opts")) {
263 unsigned int i;
264
265 for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
266 struct option *p = options+i;
267 printf("--%s ", p->long_name);
268 }
269 putchar('\n');
270 exit(0);
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200271 } else if (!strcmp(cmd, "--debug")) {
272 if (*argc < 2) {
273 fprintf(stderr, "No variable specified for --debug.\n");
274 usage(perf_usage_string);
275 }
276 if (perf_debug_option((*argv)[1]))
277 usage(perf_usage_string);
278
279 (*argv)++;
280 (*argc)--;
Ingo Molnar07800602009-04-20 15:00:56 +0200281 } else {
282 fprintf(stderr, "Unknown option: %s\n", cmd);
283 usage(perf_usage_string);
284 }
285
286 (*argv)++;
287 (*argc)--;
288 handled++;
289 }
290 return handled;
291}
292
293static int handle_alias(int *argcp, const char ***argv)
294{
295 int envchanged = 0, ret = 0, saved_errno = errno;
296 int count, option_count;
Thiago Farina4c574152010-01-27 21:05:55 -0200297 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200298 const char *alias_command;
299 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200300
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;
307
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;
313 }
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);
320 }
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;
332
333 if (count < 1)
334 die("empty alias for %s", alias_command);
335
336 if (!strcmp(alias_command, new_argv[0]))
337 die("recursive alias: %s", alias_command);
338
Thiago Farina4c574152010-01-27 21:05:55 -0200339 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200340 (count + *argcp + 1));
341 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200342 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
343 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200344
345 *argv = new_argv;
346 *argcp += count - 1;
347
348 ret = 1;
349 }
350
351 errno = saved_errno;
352
353 return ret;
354}
355
356const char perf_version_string[] = PERF_VERSION;
357
358#define RUN_SETUP (1<<0)
359#define USE_PAGER (1<<1)
360/*
361 * require working tree to be present -- anything uses this needs
362 * RUN_SETUP for reading from the configuration file.
363 */
364#define NEED_WORK_TREE (1<<2)
365
Ingo Molnar07800602009-04-20 15:00:56 +0200366static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
367{
368 int status;
369 struct stat st;
370 const char *prefix;
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000371 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200372
373 prefix = NULL;
374 if (p->option & RUN_SETUP)
375 prefix = NULL; /* setup_perf_directory(); */
376
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300377 if (use_browser == -1)
Namhyung Kim0020ce22012-11-12 11:50:17 +0900378 use_browser = check_browser_config(p->cmd);
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300379
Ingo Molnar07800602009-04-20 15:00:56 +0200380 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();
385
Ingo Molnar07800602009-04-20 15:00:56 +0200386 status = p->fn(argc, argv, prefix);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300387 exit_browser(status);
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300388 perf_env__exit(&perf_env);
Wang Nan84c86ca2015-10-14 12:41:14 +0000389 bpf__clear();
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300390
Ingo Molnar07800602009-04-20 15:00:56 +0200391 if (status)
392 return status & 0xff;
393
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;
400
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300401 status = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200402 /* Check for ENOSPC and EIO errors.. */
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300403 if (fflush(stdout)) {
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000404 fprintf(stderr, "write failure on standard output: %s",
405 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300406 goto out;
407 }
408 if (ferror(stdout)) {
409 fprintf(stderr, "unknown write failure on standard output");
410 goto out;
411 }
412 if (fclose(stdout)) {
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000413 fprintf(stderr, "close failed on standard output: %s",
414 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300415 goto out;
416 }
417 status = 0;
418out:
419 return status;
Ingo Molnar07800602009-04-20 15:00:56 +0200420}
421
422static void handle_internal_command(int argc, const char **argv)
423{
424 const char *cmd = argv[0];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200425 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200426 static const char ext[] = STRIP_EXTENSION;
427
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';
434 }
435 }
436
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";
441 }
442
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));
448 }
449}
450
451static void execv_dashed_external(const char **argv)
452{
453 struct strbuf cmd = STRBUF_INIT;
454 const char *tmp;
455 int status;
456
457 strbuf_addf(&cmd, "perf-%s", argv[0]);
458
459 /*
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.
464 */
465 tmp = argv[0];
466 argv[0] = cmd.buf;
467
468 /*
469 * if we fail because the command is not found, it is
470 * OK to return. Otherwise, we just pass along the status code.
471 */
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);
477 }
478 errno = ENOENT; /* as if we called execvp */
479
480 argv[0] = tmp;
481
482 strbuf_release(&cmd);
483}
484
485static int run_argv(int *argcp, const char ***argv)
486{
487 int done_alias = 0;
488
489 while (1) {
490 /* See if it's an internal command */
491 handle_internal_command(*argcp, *argv);
492
493 /* .. then try the external ones */
494 execv_dashed_external(*argv);
495
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
499 */
500 if (done_alias || !handle_alias(argcp, argv))
501 break;
502 done_alias = 1;
503 }
504
505 return done_alias;
506}
507
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300508static void pthread__block_sigwinch(void)
509{
510 sigset_t set;
511
512 sigemptyset(&set);
513 sigaddset(&set, SIGWINCH);
514 pthread_sigmask(SIG_BLOCK, &set, NULL);
515}
516
517void pthread__unblock_sigwinch(void)
518{
519 sigset_t set;
520
521 sigemptyset(&set);
522 sigaddset(&set, SIGWINCH);
523 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
524}
525
Ingo Molnar07800602009-04-20 15:00:56 +0200526int main(int argc, const char **argv)
527{
528 const char *cmd;
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000529 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200530
Jiri Olsa918512b2013-09-12 18:39:35 +0200531 /* The page_size is placed in util object. */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300532 page_size = sysconf(_SC_PAGE_SIZE);
Don Zickus2b1b7102014-05-30 16:10:05 -0400533 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300534
Ingo Molnar07800602009-04-20 15:00:56 +0200535 cmd = perf_extract_argv0_path(argv[0]);
536 if (!cmd)
537 cmd = "perf-help";
Jiri Olsa65d4b262015-09-02 09:56:33 +0200538
539 /* get debugfs/tracefs mount point from /proc/mounts */
540 tracing_path_mount();
541
Ingo Molnar07800602009-04-20 15:00:56 +0200542 /*
543 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
544 *
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)
548 *
549 * So we just directly call the internal command handler, and
550 * die if that one cannot handle it.
551 */
552 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200553 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200554 argv[0] = cmd;
555 handle_internal_command(argc, argv);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300556 fprintf(stderr, "cannot handle %s internally", cmd);
557 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200558 }
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300559 if (!prefixcmp(cmd, "trace")) {
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300560#ifdef HAVE_LIBAUDIT_SUPPORT
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100561 set_buildid_dir(NULL);
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300562 setup_path();
563 argv[0] = "trace";
564 return cmd_trace(argc, argv, NULL);
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300565#else
566 fprintf(stderr,
567 "trace command not available: missing audit-libs devel package at build time.\n");
568 goto out;
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300569#endif
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300570 }
Ingo Molnar07800602009-04-20 15:00:56 +0200571 /* Look for flags.. */
572 argv++;
573 argc--;
574 handle_options(&argv, &argc, NULL);
575 commit_pager_choice();
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100576 set_buildid_dir(NULL);
Stephane Eranian45de34b2010-06-01 21:25:01 +0200577
Ingo Molnar07800602009-04-20 15:00:56 +0200578 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 */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100583 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200584 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100585 printf("\n %s\n\n", perf_more_info_string);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300586 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200587 }
588 cmd = argv[0];
589
Jiri Olsa52502bf2012-10-31 15:52:47 +0100590 test_attr__init();
591
Ingo Molnar07800602009-04-20 15:00:56 +0200592 /*
593 * We use PATH to find perf commands, but we prepend some higher
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100594 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
Ingo Molnar07800602009-04-20 15:00:56 +0200595 * environment, and the $(perfexecdir) from the Makefile at build
596 * time.
597 */
598 setup_path();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300599 /*
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.
603 */
604 pthread__block_sigwinch();
Ingo Molnar07800602009-04-20 15:00:56 +0200605
606 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200607 static int done_help;
Arnaldo Carvalho de Melob5ded712013-03-27 11:00:07 -0300608 int was_alias = run_argv(&argc, &argv);
Ingo Molnar8035e422009-06-06 15:19:13 +0200609
Ingo Molnar07800602009-04-20 15:00:56 +0200610 if (errno != ENOENT)
611 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200612
Ingo Molnar07800602009-04-20 15:00:56 +0200613 if (was_alias) {
614 fprintf(stderr, "Expansion of alias '%s' failed; "
615 "'%s' is not a perf-command\n",
616 cmd, argv[0]);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300617 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200618 }
619 if (!done_help) {
620 cmd = argv[0] = help_unknown_cmd(cmd);
621 done_help = 1;
622 } else
623 break;
624 }
625
626 fprintf(stderr, "Failed to run command '%s': %s\n",
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000627 cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300628out:
Ingo Molnar07800602009-04-20 15:00:56 +0200629 return 1;
630}