blob: b857fcbd00cf291f9dc7d8473e36cdadeac81987 [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"
Yunlong Song73353992015-02-27 18:21:31 +080016#include "util/parse-options.h"
Jiri Olsabbb2cea2014-07-17 12:55:00 +020017#include "util/debug.h"
Borislav Petkov553873e2013-12-09 17:14:23 +010018#include <api/fs/debugfs.h>
Irina Tirdea27683dc2012-09-08 03:43:19 +030019#include <pthread.h>
Ingo Molnar07800602009-04-20 15:00:56 +020020
21const char perf_usage_string[] =
Jiri Olsa78a1b502014-07-18 09:11:30 +020022 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
Ingo Molnar07800602009-04-20 15:00:56 +020023
24const char perf_more_info_string[] =
25 "See 'perf help COMMAND' for more information on a specific command.";
26
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030027int use_browser = -1;
Ingo Molnar07800602009-04-20 15:00:56 +020028static int use_pager = -1;
Feng Tang70cb4e92012-10-30 11:56:02 +080029const char *input_name;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030030
Frederic Weisbecker98a41792012-08-09 16:31:51 +020031struct 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 },
Ingo Molnar89fe8082013-09-30 12:07:11 +020054#ifdef HAVE_LIBELF_SUPPORT
Frederic Weisbecker98a41792012-08-09 16:31:51 +020055 { "probe", cmd_probe, 0 },
Namhyung Kim393be2e2012-08-06 13:41:21 +090056#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020057 { "kmem", cmd_kmem, 0 },
58 { "lock", cmd_lock, 0 },
59 { "kvm", cmd_kvm, 0 },
60 { "test", cmd_test, 0 },
Ingo Molnar89fe8082013-09-30 12:07:11 +020061#ifdef HAVE_LIBAUDIT_SUPPORT
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030062 { "trace", cmd_trace, 0 },
Namhyung Kim4d290892012-09-27 20:23:38 +090063#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020064 { "inject", cmd_inject, 0 },
Stephane Eranian028f12e2013-01-24 16:10:38 +010065 { "mem", cmd_mem, 0 },
Jiri Olsa2245bf12015-02-20 23:16:59 +010066 { "data", cmd_data, 0 },
Frederic Weisbecker98a41792012-08-09 16:31:51 +020067};
68
Ingo Molnar07800602009-04-20 15:00:56 +020069struct 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
Namhyung Kim0020ce22012-11-12 11:50:17 +090092static int browser_command_config(const char *var, const char *value, void *data)
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030093{
94 struct pager_config *c = data;
95 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
96 c->val = perf_config_bool(var, value);
Namhyung Kim0020ce22012-11-12 11:50:17 +090097 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
98 c->val = perf_config_bool(var, value) ? 2 : 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030099 return 0;
100}
101
Namhyung Kim0020ce22012-11-12 11:50:17 +0900102/*
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)
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300107{
108 struct pager_config c;
109 c.cmd = cmd;
110 c.val = -1;
Namhyung Kim0020ce22012-11-12 11:50:17 +0900111 perf_config(browser_command_config, &c);
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300112 return c.val;
113}
114
Thiago Farina4c574152010-01-27 21:05:55 -0200115static void commit_pager_choice(void)
116{
Ingo Molnar07800602009-04-20 15:00:56 +0200117 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
Yunlong Song73353992015-02-27 18:21:31 +0800129struct 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
Thiago Farina4c574152010-01-27 21:05:55 -0200146static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +0200147{
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 */
Vincent Legollcfed95a2009-10-13 10:18:16 +0200166 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
167 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +0200168 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) {
Thiago Farina4c574152010-01-27 21:05:55 -0200185 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200186 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++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200194 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
195 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200196 if (envchanged)
197 *envchanged = 1;
198 } else if (!strcmp(cmd, "--work-tree")) {
199 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200200 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200201 usage(perf_usage_string);
202 }
203 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
204 if (envchanged)
205 *envchanged = 1;
206 (*argv)++;
207 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200208 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
209 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200210 if (envchanged)
211 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400212 } 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 }
Borislav Petkov13559152013-02-20 16:32:31 +0100217 perf_debugfs_set_path((*argv)[1]);
Jason Baron5beeded2009-07-21 14:16:29 -0400218 if (envchanged)
219 *envchanged = 1;
220 (*argv)++;
221 (*argc)--;
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100222 } 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)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200232 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
Borislav Petkov13559152013-02-20 16:32:31 +0100233 perf_debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200234 fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
Jason Baron5beeded2009-07-21 14:16:29 -0400235 if (envchanged)
236 *envchanged = 1;
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200237 } 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 }
Yunlong Songed457522015-02-27 18:21:29 +0800244 putchar('\n');
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200245 exit(0);
Yunlong Song73353992015-02-27 18:21:31 +0800246 } 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);
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200255 } 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)--;
Ingo Molnar07800602009-04-20 15:00:56 +0200265 } 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;
Thiago Farina4c574152010-01-27 21:05:55 -0200281 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200282 const char *alias_command;
283 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200284
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
Thiago Farina4c574152010-01-27 21:05:55 -0200323 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200324 (count + *argcp + 1));
325 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200326 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
327 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200328
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
Ingo Molnar07800602009-04-20 15:00:56 +0200350static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
351{
352 int status;
353 struct stat st;
354 const char *prefix;
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000355 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200356
357 prefix = NULL;
358 if (p->option & RUN_SETUP)
359 prefix = NULL; /* setup_perf_directory(); */
360
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300361 if (use_browser == -1)
Namhyung Kim0020ce22012-11-12 11:50:17 +0900362 use_browser = check_browser_config(p->cmd);
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300363
Ingo Molnar07800602009-04-20 15:00:56 +0200364 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
Ingo Molnar07800602009-04-20 15:00:56 +0200370 status = p->fn(argc, argv, prefix);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300371 exit_browser(status);
372
Ingo Molnar07800602009-04-20 15:00:56 +0200373 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
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300383 status = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200384 /* Check for ENOSPC and EIO errors.. */
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300385 if (fflush(stdout)) {
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000386 fprintf(stderr, "write failure on standard output: %s",
387 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300388 goto out;
389 }
390 if (ferror(stdout)) {
391 fprintf(stderr, "unknown write failure on standard output");
392 goto out;
393 }
394 if (fclose(stdout)) {
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000395 fprintf(stderr, "close failed on standard output: %s",
396 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300397 goto out;
398 }
399 status = 0;
400out:
401 return status;
Ingo Molnar07800602009-04-20 15:00:56 +0200402}
403
404static void handle_internal_command(int argc, const char **argv)
405{
406 const char *cmd = argv[0];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200407 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200408 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
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300490static 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
Ingo Molnar07800602009-04-20 15:00:56 +0200508int main(int argc, const char **argv)
509{
510 const char *cmd;
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000511 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200512
Jiri Olsa918512b2013-09-12 18:39:35 +0200513 /* The page_size is placed in util object. */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300514 page_size = sysconf(_SC_PAGE_SIZE);
Don Zickus2b1b7102014-05-30 16:10:05 -0400515 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300516
Ingo Molnar07800602009-04-20 15:00:56 +0200517 cmd = perf_extract_argv0_path(argv[0]);
518 if (!cmd)
519 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400520 /* get debugfs mount point from /proc/mounts */
Borislav Petkov13559152013-02-20 16:32:31 +0100521 perf_debugfs_mount(NULL);
Ingo Molnar07800602009-04-20 15:00:56 +0200522 /*
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-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200533 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200534 argv[0] = cmd;
535 handle_internal_command(argc, argv);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300536 fprintf(stderr, "cannot handle %s internally", cmd);
537 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200538 }
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300539 if (!prefixcmp(cmd, "trace")) {
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300540#ifdef HAVE_LIBAUDIT_SUPPORT
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100541 set_buildid_dir(NULL);
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300542 setup_path();
543 argv[0] = "trace";
544 return cmd_trace(argc, argv, NULL);
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300545#else
546 fprintf(stderr,
547 "trace command not available: missing audit-libs devel package at build time.\n");
548 goto out;
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300549#endif
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300550 }
Ingo Molnar07800602009-04-20 15:00:56 +0200551 /* Look for flags.. */
552 argv++;
553 argc--;
554 handle_options(&argv, &argc, NULL);
555 commit_pager_choice();
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100556 set_buildid_dir(NULL);
Stephane Eranian45de34b2010-06-01 21:25:01 +0200557
Ingo Molnar07800602009-04-20 15:00:56 +0200558 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 */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100563 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200564 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100565 printf("\n %s\n\n", perf_more_info_string);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300566 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200567 }
568 cmd = argv[0];
569
Jiri Olsa52502bf2012-10-31 15:52:47 +0100570 test_attr__init();
571
Ingo Molnar07800602009-04-20 15:00:56 +0200572 /*
573 * We use PATH to find perf commands, but we prepend some higher
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100574 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
Ingo Molnar07800602009-04-20 15:00:56 +0200575 * environment, and the $(perfexecdir) from the Makefile at build
576 * time.
577 */
578 setup_path();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300579 /*
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();
Ingo Molnar07800602009-04-20 15:00:56 +0200585
586 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200587 static int done_help;
Arnaldo Carvalho de Melob5ded712013-03-27 11:00:07 -0300588 int was_alias = run_argv(&argc, &argv);
Ingo Molnar8035e422009-06-06 15:19:13 +0200589
Ingo Molnar07800602009-04-20 15:00:56 +0200590 if (errno != ENOENT)
591 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200592
Ingo Molnar07800602009-04-20 15:00:56 +0200593 if (was_alias) {
594 fprintf(stderr, "Expansion of alias '%s' failed; "
595 "'%s' is not a perf-command\n",
596 cmd, argv[0]);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300597 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200598 }
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",
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000607 cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300608out:
Ingo Molnar07800602009-04-20 15:00:56 +0200609 return 1;
610}