blob: 452a8474d29d8cedbed3e47f46a4ebc728663907 [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"
Jiri Olsabbb2cea2014-07-17 12:55:00 +020016#include "util/debug.h"
Borislav Petkov553873e2013-12-09 17:14:23 +010017#include <api/fs/debugfs.h>
Irina Tirdea27683dc2012-09-08 03:43:19 +030018#include <pthread.h>
Ingo Molnar07800602009-04-20 15:00:56 +020019
20const char perf_usage_string[] =
Jiri Olsa78a1b502014-07-18 09:11:30 +020021 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
Ingo Molnar07800602009-04-20 15:00:56 +020022
23const char perf_more_info_string[] =
24 "See 'perf help COMMAND' for more information on a specific command.";
25
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030026int use_browser = -1;
Ingo Molnar07800602009-04-20 15:00:56 +020027static int use_pager = -1;
Feng Tang70cb4e92012-10-30 11:56:02 +080028const char *input_name;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030029
Frederic Weisbecker98a41792012-08-09 16:31:51 +020030struct 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 },
Ingo Molnar89fe8082013-09-30 12:07:11 +020053#ifdef HAVE_LIBELF_SUPPORT
Frederic Weisbecker98a41792012-08-09 16:31:51 +020054 { "probe", cmd_probe, 0 },
Namhyung Kim393be2e2012-08-06 13:41:21 +090055#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020056 { "kmem", cmd_kmem, 0 },
57 { "lock", cmd_lock, 0 },
58 { "kvm", cmd_kvm, 0 },
59 { "test", cmd_test, 0 },
Ingo Molnar89fe8082013-09-30 12:07:11 +020060#ifdef HAVE_LIBAUDIT_SUPPORT
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030061 { "trace", cmd_trace, 0 },
Namhyung Kim4d290892012-09-27 20:23:38 +090062#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020063 { "inject", cmd_inject, 0 },
Stephane Eranian028f12e2013-01-24 16:10:38 +010064 { "mem", cmd_mem, 0 },
Frederic Weisbecker98a41792012-08-09 16:31:51 +020065};
66
Ingo Molnar07800602009-04-20 15:00:56 +020067struct 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
Namhyung Kim0020ce22012-11-12 11:50:17 +090090static int browser_command_config(const char *var, const char *value, void *data)
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030091{
92 struct pager_config *c = data;
93 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
94 c->val = perf_config_bool(var, value);
Namhyung Kim0020ce22012-11-12 11:50:17 +090095 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
96 c->val = perf_config_bool(var, value) ? 2 : 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030097 return 0;
98}
99
Namhyung Kim0020ce22012-11-12 11:50:17 +0900100/*
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)
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300105{
106 struct pager_config c;
107 c.cmd = cmd;
108 c.val = -1;
Namhyung Kim0020ce22012-11-12 11:50:17 +0900109 perf_config(browser_command_config, &c);
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300110 return c.val;
111}
112
Thiago Farina4c574152010-01-27 21:05:55 -0200113static void commit_pager_choice(void)
114{
Ingo Molnar07800602009-04-20 15:00:56 +0200115 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
Thiago Farina4c574152010-01-27 21:05:55 -0200127static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +0200128{
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 */
Vincent Legollcfed95a2009-10-13 10:18:16 +0200147 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
148 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +0200149 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) {
Thiago Farina4c574152010-01-27 21:05:55 -0200166 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200167 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++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200175 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
176 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200177 if (envchanged)
178 *envchanged = 1;
179 } else if (!strcmp(cmd, "--work-tree")) {
180 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200181 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200182 usage(perf_usage_string);
183 }
184 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
185 if (envchanged)
186 *envchanged = 1;
187 (*argv)++;
188 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200189 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
190 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200191 if (envchanged)
192 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400193 } 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 }
Borislav Petkov13559152013-02-20 16:32:31 +0100198 perf_debugfs_set_path((*argv)[1]);
Jason Baron5beeded2009-07-21 14:16:29 -0400199 if (envchanged)
200 *envchanged = 1;
201 (*argv)++;
202 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200203 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
Borislav Petkov13559152013-02-20 16:32:31 +0100204 perf_debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200205 fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
Jason Baron5beeded2009-07-21 14:16:29 -0400206 if (envchanged)
207 *envchanged = 1;
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200208 } 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);
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200216 } 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)--;
Ingo Molnar07800602009-04-20 15:00:56 +0200226 } 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;
Thiago Farina4c574152010-01-27 21:05:55 -0200242 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200243 const char *alias_command;
244 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200245
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
Thiago Farina4c574152010-01-27 21:05:55 -0200284 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200285 (count + *argcp + 1));
286 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200287 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
288 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200289
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
Ingo Molnar07800602009-04-20 15:00:56 +0200311static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
312{
313 int status;
314 struct stat st;
315 const char *prefix;
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000316 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200317
318 prefix = NULL;
319 if (p->option & RUN_SETUP)
320 prefix = NULL; /* setup_perf_directory(); */
321
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300322 if (use_browser == -1)
Namhyung Kim0020ce22012-11-12 11:50:17 +0900323 use_browser = check_browser_config(p->cmd);
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300324
Ingo Molnar07800602009-04-20 15:00:56 +0200325 if (use_pager == -1 && p->option & RUN_SETUP)
326 use_pager = check_pager_config(p->cmd);
327 if (use_pager == -1 && p->option & USE_PAGER)
328 use_pager = 1;
329 commit_pager_choice();
330
Ingo Molnar07800602009-04-20 15:00:56 +0200331 status = p->fn(argc, argv, prefix);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300332 exit_browser(status);
333
Ingo Molnar07800602009-04-20 15:00:56 +0200334 if (status)
335 return status & 0xff;
336
337 /* Somebody closed stdout? */
338 if (fstat(fileno(stdout), &st))
339 return 0;
340 /* Ignore write errors for pipes and sockets.. */
341 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
342 return 0;
343
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300344 status = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200345 /* Check for ENOSPC and EIO errors.. */
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300346 if (fflush(stdout)) {
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000347 fprintf(stderr, "write failure on standard output: %s",
348 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300349 goto out;
350 }
351 if (ferror(stdout)) {
352 fprintf(stderr, "unknown write failure on standard output");
353 goto out;
354 }
355 if (fclose(stdout)) {
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000356 fprintf(stderr, "close failed on standard output: %s",
357 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300358 goto out;
359 }
360 status = 0;
361out:
362 return status;
Ingo Molnar07800602009-04-20 15:00:56 +0200363}
364
365static void handle_internal_command(int argc, const char **argv)
366{
367 const char *cmd = argv[0];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200368 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200369 static const char ext[] = STRIP_EXTENSION;
370
371 if (sizeof(ext) > 1) {
372 i = strlen(argv[0]) - strlen(ext);
373 if (i > 0 && !strcmp(argv[0] + i, ext)) {
374 char *argv0 = strdup(argv[0]);
375 argv[0] = cmd = argv0;
376 argv0[i] = '\0';
377 }
378 }
379
380 /* Turn "perf cmd --help" into "perf help cmd" */
381 if (argc > 1 && !strcmp(argv[1], "--help")) {
382 argv[1] = argv[0];
383 argv[0] = cmd = "help";
384 }
385
386 for (i = 0; i < ARRAY_SIZE(commands); i++) {
387 struct cmd_struct *p = commands+i;
388 if (strcmp(p->cmd, cmd))
389 continue;
390 exit(run_builtin(p, argc, argv));
391 }
392}
393
394static void execv_dashed_external(const char **argv)
395{
396 struct strbuf cmd = STRBUF_INIT;
397 const char *tmp;
398 int status;
399
400 strbuf_addf(&cmd, "perf-%s", argv[0]);
401
402 /*
403 * argv[0] must be the perf command, but the argv array
404 * belongs to the caller, and may be reused in
405 * subsequent loop iterations. Save argv[0] and
406 * restore it on error.
407 */
408 tmp = argv[0];
409 argv[0] = cmd.buf;
410
411 /*
412 * if we fail because the command is not found, it is
413 * OK to return. Otherwise, we just pass along the status code.
414 */
415 status = run_command_v_opt(argv, 0);
416 if (status != -ERR_RUN_COMMAND_EXEC) {
417 if (IS_RUN_COMMAND_ERR(status))
418 die("unable to run '%s'", argv[0]);
419 exit(-status);
420 }
421 errno = ENOENT; /* as if we called execvp */
422
423 argv[0] = tmp;
424
425 strbuf_release(&cmd);
426}
427
428static int run_argv(int *argcp, const char ***argv)
429{
430 int done_alias = 0;
431
432 while (1) {
433 /* See if it's an internal command */
434 handle_internal_command(*argcp, *argv);
435
436 /* .. then try the external ones */
437 execv_dashed_external(*argv);
438
439 /* It could be an alias -- this works around the insanity
440 * of overriding "perf log" with "perf show" by having
441 * alias.log = show
442 */
443 if (done_alias || !handle_alias(argcp, argv))
444 break;
445 done_alias = 1;
446 }
447
448 return done_alias;
449}
450
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300451static void pthread__block_sigwinch(void)
452{
453 sigset_t set;
454
455 sigemptyset(&set);
456 sigaddset(&set, SIGWINCH);
457 pthread_sigmask(SIG_BLOCK, &set, NULL);
458}
459
460void pthread__unblock_sigwinch(void)
461{
462 sigset_t set;
463
464 sigemptyset(&set);
465 sigaddset(&set, SIGWINCH);
466 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
467}
468
Ingo Molnar07800602009-04-20 15:00:56 +0200469int main(int argc, const char **argv)
470{
471 const char *cmd;
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000472 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200473
Jiri Olsa918512b2013-09-12 18:39:35 +0200474 /* The page_size is placed in util object. */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300475 page_size = sysconf(_SC_PAGE_SIZE);
Don Zickus2b1b7102014-05-30 16:10:05 -0400476 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300477
Ingo Molnar07800602009-04-20 15:00:56 +0200478 cmd = perf_extract_argv0_path(argv[0]);
479 if (!cmd)
480 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400481 /* get debugfs mount point from /proc/mounts */
Borislav Petkov13559152013-02-20 16:32:31 +0100482 perf_debugfs_mount(NULL);
Ingo Molnar07800602009-04-20 15:00:56 +0200483 /*
484 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
485 *
486 * - cannot take flags in between the "perf" and the "xxxx".
487 * - cannot execute it externally (since it would just do
488 * the same thing over again)
489 *
490 * So we just directly call the internal command handler, and
491 * die if that one cannot handle it.
492 */
493 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200494 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200495 argv[0] = cmd;
496 handle_internal_command(argc, argv);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300497 fprintf(stderr, "cannot handle %s internally", cmd);
498 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200499 }
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300500 if (!prefixcmp(cmd, "trace")) {
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300501#ifdef HAVE_LIBAUDIT_SUPPORT
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300502 set_buildid_dir();
503 setup_path();
504 argv[0] = "trace";
505 return cmd_trace(argc, argv, NULL);
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300506#else
507 fprintf(stderr,
508 "trace command not available: missing audit-libs devel package at build time.\n");
509 goto out;
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300510#endif
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300511 }
Ingo Molnar07800602009-04-20 15:00:56 +0200512 /* Look for flags.. */
513 argv++;
514 argc--;
515 handle_options(&argv, &argc, NULL);
516 commit_pager_choice();
Stephane Eranian45de34b2010-06-01 21:25:01 +0200517 set_buildid_dir();
518
Ingo Molnar07800602009-04-20 15:00:56 +0200519 if (argc > 0) {
520 if (!prefixcmp(argv[0], "--"))
521 argv[0] += 2;
522 } else {
523 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100524 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200525 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100526 printf("\n %s\n\n", perf_more_info_string);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300527 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200528 }
529 cmd = argv[0];
530
Jiri Olsa52502bf2012-10-31 15:52:47 +0100531 test_attr__init();
532
Ingo Molnar07800602009-04-20 15:00:56 +0200533 /*
534 * We use PATH to find perf commands, but we prepend some higher
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100535 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
Ingo Molnar07800602009-04-20 15:00:56 +0200536 * environment, and the $(perfexecdir) from the Makefile at build
537 * time.
538 */
539 setup_path();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300540 /*
541 * Block SIGWINCH notifications so that the thread that wants it can
542 * unblock and get syscalls like select interrupted instead of waiting
543 * forever while the signal goes to some other non interested thread.
544 */
545 pthread__block_sigwinch();
Ingo Molnar07800602009-04-20 15:00:56 +0200546
547 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200548 static int done_help;
Arnaldo Carvalho de Melob5ded712013-03-27 11:00:07 -0300549 int was_alias = run_argv(&argc, &argv);
Ingo Molnar8035e422009-06-06 15:19:13 +0200550
Ingo Molnar07800602009-04-20 15:00:56 +0200551 if (errno != ENOENT)
552 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200553
Ingo Molnar07800602009-04-20 15:00:56 +0200554 if (was_alias) {
555 fprintf(stderr, "Expansion of alias '%s' failed; "
556 "'%s' is not a perf-command\n",
557 cmd, argv[0]);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300558 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200559 }
560 if (!done_help) {
561 cmd = argv[0] = help_unknown_cmd(cmd);
562 done_help = 1;
563 } else
564 break;
565 }
566
567 fprintf(stderr, "Failed to run command '%s': %s\n",
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000568 cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300569out:
Ingo Molnar07800602009-04-20 15:00:56 +0200570 return 1;
571}