blob: f3c66b81c6bee9b2cb57e2c36939df8bf533d090 [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 },
Jiri Olsa2245bf12015-02-20 23:16:59 +010065 { "data", cmd_data, 0 },
Frederic Weisbecker98a41792012-08-09 16:31:51 +020066};
67
Ingo Molnar07800602009-04-20 15:00:56 +020068struct pager_config {
69 const char *cmd;
70 int val;
71};
72
73static int pager_command_config(const char *var, const char *value, void *data)
74{
75 struct pager_config *c = data;
76 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
77 c->val = perf_config_bool(var, value);
78 return 0;
79}
80
81/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
82int check_pager_config(const char *cmd)
83{
84 struct pager_config c;
85 c.cmd = cmd;
86 c.val = -1;
87 perf_config(pager_command_config, &c);
88 return c.val;
89}
90
Namhyung Kim0020ce22012-11-12 11:50:17 +090091static int browser_command_config(const char *var, const char *value, void *data)
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030092{
93 struct pager_config *c = data;
94 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
95 c->val = perf_config_bool(var, value);
Namhyung Kim0020ce22012-11-12 11:50:17 +090096 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
97 c->val = perf_config_bool(var, value) ? 2 : 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030098 return 0;
99}
100
Namhyung Kim0020ce22012-11-12 11:50:17 +0900101/*
102 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
103 * and -1 for "not specified"
104 */
105static int check_browser_config(const char *cmd)
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300106{
107 struct pager_config c;
108 c.cmd = cmd;
109 c.val = -1;
Namhyung Kim0020ce22012-11-12 11:50:17 +0900110 perf_config(browser_command_config, &c);
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300111 return c.val;
112}
113
Thiago Farina4c574152010-01-27 21:05:55 -0200114static void commit_pager_choice(void)
115{
Ingo Molnar07800602009-04-20 15:00:56 +0200116 switch (use_pager) {
117 case 0:
118 setenv("PERF_PAGER", "cat", 1);
119 break;
120 case 1:
121 /* setup_pager(); */
122 break;
123 default:
124 break;
125 }
126}
127
Thiago Farina4c574152010-01-27 21:05:55 -0200128static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +0200129{
130 int handled = 0;
131
132 while (*argc > 0) {
133 const char *cmd = (*argv)[0];
134 if (cmd[0] != '-')
135 break;
136
137 /*
138 * For legacy reasons, the "version" and "help"
139 * commands can be written with "--" prepended
140 * to make them look like flags.
141 */
142 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
143 break;
144
145 /*
146 * Check remaining flags.
147 */
Vincent Legollcfed95a2009-10-13 10:18:16 +0200148 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
149 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +0200150 if (*cmd == '=')
151 perf_set_argv_exec_path(cmd + 1);
152 else {
153 puts(perf_exec_path());
154 exit(0);
155 }
156 } else if (!strcmp(cmd, "--html-path")) {
157 puts(system_path(PERF_HTML_PATH));
158 exit(0);
159 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
160 use_pager = 1;
161 } else if (!strcmp(cmd, "--no-pager")) {
162 use_pager = 0;
163 if (envchanged)
164 *envchanged = 1;
165 } else if (!strcmp(cmd, "--perf-dir")) {
166 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200167 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200168 usage(perf_usage_string);
169 }
170 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
171 if (envchanged)
172 *envchanged = 1;
173 (*argv)++;
174 (*argc)--;
175 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200176 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
177 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200178 if (envchanged)
179 *envchanged = 1;
180 } else if (!strcmp(cmd, "--work-tree")) {
181 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200182 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200183 usage(perf_usage_string);
184 }
185 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
186 if (envchanged)
187 *envchanged = 1;
188 (*argv)++;
189 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200190 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
191 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200192 if (envchanged)
193 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400194 } else if (!strcmp(cmd, "--debugfs-dir")) {
195 if (*argc < 2) {
196 fprintf(stderr, "No directory given for --debugfs-dir.\n");
197 usage(perf_usage_string);
198 }
Borislav Petkov13559152013-02-20 16:32:31 +0100199 perf_debugfs_set_path((*argv)[1]);
Jason Baron5beeded2009-07-21 14:16:29 -0400200 if (envchanged)
201 *envchanged = 1;
202 (*argv)++;
203 (*argc)--;
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100204 } else if (!strcmp(cmd, "--buildid-dir")) {
205 if (*argc < 2) {
206 fprintf(stderr, "No directory given for --buildid-dir.\n");
207 usage(perf_usage_string);
208 }
209 set_buildid_dir((*argv)[1]);
210 if (envchanged)
211 *envchanged = 1;
212 (*argv)++;
213 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200214 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
Borislav Petkov13559152013-02-20 16:32:31 +0100215 perf_debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200216 fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
Jason Baron5beeded2009-07-21 14:16:29 -0400217 if (envchanged)
218 *envchanged = 1;
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200219 } else if (!strcmp(cmd, "--list-cmds")) {
220 unsigned int i;
221
222 for (i = 0; i < ARRAY_SIZE(commands); i++) {
223 struct cmd_struct *p = commands+i;
224 printf("%s ", p->cmd);
225 }
226 exit(0);
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200227 } else if (!strcmp(cmd, "--debug")) {
228 if (*argc < 2) {
229 fprintf(stderr, "No variable specified for --debug.\n");
230 usage(perf_usage_string);
231 }
232 if (perf_debug_option((*argv)[1]))
233 usage(perf_usage_string);
234
235 (*argv)++;
236 (*argc)--;
Ingo Molnar07800602009-04-20 15:00:56 +0200237 } else {
238 fprintf(stderr, "Unknown option: %s\n", cmd);
239 usage(perf_usage_string);
240 }
241
242 (*argv)++;
243 (*argc)--;
244 handled++;
245 }
246 return handled;
247}
248
249static int handle_alias(int *argcp, const char ***argv)
250{
251 int envchanged = 0, ret = 0, saved_errno = errno;
252 int count, option_count;
Thiago Farina4c574152010-01-27 21:05:55 -0200253 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200254 const char *alias_command;
255 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200256
257 alias_command = (*argv)[0];
258 alias_string = alias_lookup(alias_command);
259 if (alias_string) {
260 if (alias_string[0] == '!') {
261 if (*argcp > 1) {
262 struct strbuf buf;
263
264 strbuf_init(&buf, PATH_MAX);
265 strbuf_addstr(&buf, alias_string);
266 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
267 free(alias_string);
268 alias_string = buf.buf;
269 }
270 ret = system(alias_string + 1);
271 if (ret >= 0 && WIFEXITED(ret) &&
272 WEXITSTATUS(ret) != 127)
273 exit(WEXITSTATUS(ret));
274 die("Failed to run '%s' when expanding alias '%s'",
275 alias_string + 1, alias_command);
276 }
277 count = split_cmdline(alias_string, &new_argv);
278 if (count < 0)
279 die("Bad alias.%s string", alias_command);
280 option_count = handle_options(&new_argv, &count, &envchanged);
281 if (envchanged)
282 die("alias '%s' changes environment variables\n"
283 "You can use '!perf' in the alias to do this.",
284 alias_command);
285 memmove(new_argv - option_count, new_argv,
286 count * sizeof(char *));
287 new_argv -= option_count;
288
289 if (count < 1)
290 die("empty alias for %s", alias_command);
291
292 if (!strcmp(alias_command, new_argv[0]))
293 die("recursive alias: %s", alias_command);
294
Thiago Farina4c574152010-01-27 21:05:55 -0200295 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200296 (count + *argcp + 1));
297 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200298 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
299 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200300
301 *argv = new_argv;
302 *argcp += count - 1;
303
304 ret = 1;
305 }
306
307 errno = saved_errno;
308
309 return ret;
310}
311
312const char perf_version_string[] = PERF_VERSION;
313
314#define RUN_SETUP (1<<0)
315#define USE_PAGER (1<<1)
316/*
317 * require working tree to be present -- anything uses this needs
318 * RUN_SETUP for reading from the configuration file.
319 */
320#define NEED_WORK_TREE (1<<2)
321
Ingo Molnar07800602009-04-20 15:00:56 +0200322static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
323{
324 int status;
325 struct stat st;
326 const char *prefix;
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000327 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200328
329 prefix = NULL;
330 if (p->option & RUN_SETUP)
331 prefix = NULL; /* setup_perf_directory(); */
332
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300333 if (use_browser == -1)
Namhyung Kim0020ce22012-11-12 11:50:17 +0900334 use_browser = check_browser_config(p->cmd);
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300335
Ingo Molnar07800602009-04-20 15:00:56 +0200336 if (use_pager == -1 && p->option & RUN_SETUP)
337 use_pager = check_pager_config(p->cmd);
338 if (use_pager == -1 && p->option & USE_PAGER)
339 use_pager = 1;
340 commit_pager_choice();
341
Ingo Molnar07800602009-04-20 15:00:56 +0200342 status = p->fn(argc, argv, prefix);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300343 exit_browser(status);
344
Ingo Molnar07800602009-04-20 15:00:56 +0200345 if (status)
346 return status & 0xff;
347
348 /* Somebody closed stdout? */
349 if (fstat(fileno(stdout), &st))
350 return 0;
351 /* Ignore write errors for pipes and sockets.. */
352 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
353 return 0;
354
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300355 status = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200356 /* Check for ENOSPC and EIO errors.. */
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300357 if (fflush(stdout)) {
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000358 fprintf(stderr, "write failure on standard output: %s",
359 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300360 goto out;
361 }
362 if (ferror(stdout)) {
363 fprintf(stderr, "unknown write failure on standard output");
364 goto out;
365 }
366 if (fclose(stdout)) {
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000367 fprintf(stderr, "close failed on standard output: %s",
368 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300369 goto out;
370 }
371 status = 0;
372out:
373 return status;
Ingo Molnar07800602009-04-20 15:00:56 +0200374}
375
376static void handle_internal_command(int argc, const char **argv)
377{
378 const char *cmd = argv[0];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200379 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200380 static const char ext[] = STRIP_EXTENSION;
381
382 if (sizeof(ext) > 1) {
383 i = strlen(argv[0]) - strlen(ext);
384 if (i > 0 && !strcmp(argv[0] + i, ext)) {
385 char *argv0 = strdup(argv[0]);
386 argv[0] = cmd = argv0;
387 argv0[i] = '\0';
388 }
389 }
390
391 /* Turn "perf cmd --help" into "perf help cmd" */
392 if (argc > 1 && !strcmp(argv[1], "--help")) {
393 argv[1] = argv[0];
394 argv[0] = cmd = "help";
395 }
396
397 for (i = 0; i < ARRAY_SIZE(commands); i++) {
398 struct cmd_struct *p = commands+i;
399 if (strcmp(p->cmd, cmd))
400 continue;
401 exit(run_builtin(p, argc, argv));
402 }
403}
404
405static void execv_dashed_external(const char **argv)
406{
407 struct strbuf cmd = STRBUF_INIT;
408 const char *tmp;
409 int status;
410
411 strbuf_addf(&cmd, "perf-%s", argv[0]);
412
413 /*
414 * argv[0] must be the perf command, but the argv array
415 * belongs to the caller, and may be reused in
416 * subsequent loop iterations. Save argv[0] and
417 * restore it on error.
418 */
419 tmp = argv[0];
420 argv[0] = cmd.buf;
421
422 /*
423 * if we fail because the command is not found, it is
424 * OK to return. Otherwise, we just pass along the status code.
425 */
426 status = run_command_v_opt(argv, 0);
427 if (status != -ERR_RUN_COMMAND_EXEC) {
428 if (IS_RUN_COMMAND_ERR(status))
429 die("unable to run '%s'", argv[0]);
430 exit(-status);
431 }
432 errno = ENOENT; /* as if we called execvp */
433
434 argv[0] = tmp;
435
436 strbuf_release(&cmd);
437}
438
439static int run_argv(int *argcp, const char ***argv)
440{
441 int done_alias = 0;
442
443 while (1) {
444 /* See if it's an internal command */
445 handle_internal_command(*argcp, *argv);
446
447 /* .. then try the external ones */
448 execv_dashed_external(*argv);
449
450 /* It could be an alias -- this works around the insanity
451 * of overriding "perf log" with "perf show" by having
452 * alias.log = show
453 */
454 if (done_alias || !handle_alias(argcp, argv))
455 break;
456 done_alias = 1;
457 }
458
459 return done_alias;
460}
461
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300462static void pthread__block_sigwinch(void)
463{
464 sigset_t set;
465
466 sigemptyset(&set);
467 sigaddset(&set, SIGWINCH);
468 pthread_sigmask(SIG_BLOCK, &set, NULL);
469}
470
471void pthread__unblock_sigwinch(void)
472{
473 sigset_t set;
474
475 sigemptyset(&set);
476 sigaddset(&set, SIGWINCH);
477 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
478}
479
Ingo Molnar07800602009-04-20 15:00:56 +0200480int main(int argc, const char **argv)
481{
482 const char *cmd;
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000483 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200484
Jiri Olsa918512b2013-09-12 18:39:35 +0200485 /* The page_size is placed in util object. */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300486 page_size = sysconf(_SC_PAGE_SIZE);
Don Zickus2b1b7102014-05-30 16:10:05 -0400487 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300488
Ingo Molnar07800602009-04-20 15:00:56 +0200489 cmd = perf_extract_argv0_path(argv[0]);
490 if (!cmd)
491 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400492 /* get debugfs mount point from /proc/mounts */
Borislav Petkov13559152013-02-20 16:32:31 +0100493 perf_debugfs_mount(NULL);
Ingo Molnar07800602009-04-20 15:00:56 +0200494 /*
495 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
496 *
497 * - cannot take flags in between the "perf" and the "xxxx".
498 * - cannot execute it externally (since it would just do
499 * the same thing over again)
500 *
501 * So we just directly call the internal command handler, and
502 * die if that one cannot handle it.
503 */
504 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200505 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200506 argv[0] = cmd;
507 handle_internal_command(argc, argv);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300508 fprintf(stderr, "cannot handle %s internally", cmd);
509 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200510 }
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300511 if (!prefixcmp(cmd, "trace")) {
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300512#ifdef HAVE_LIBAUDIT_SUPPORT
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100513 set_buildid_dir(NULL);
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300514 setup_path();
515 argv[0] = "trace";
516 return cmd_trace(argc, argv, NULL);
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300517#else
518 fprintf(stderr,
519 "trace command not available: missing audit-libs devel package at build time.\n");
520 goto out;
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300521#endif
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300522 }
Ingo Molnar07800602009-04-20 15:00:56 +0200523 /* Look for flags.. */
524 argv++;
525 argc--;
526 handle_options(&argv, &argc, NULL);
527 commit_pager_choice();
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100528 set_buildid_dir(NULL);
Stephane Eranian45de34b2010-06-01 21:25:01 +0200529
Ingo Molnar07800602009-04-20 15:00:56 +0200530 if (argc > 0) {
531 if (!prefixcmp(argv[0], "--"))
532 argv[0] += 2;
533 } else {
534 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100535 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200536 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100537 printf("\n %s\n\n", perf_more_info_string);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300538 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200539 }
540 cmd = argv[0];
541
Jiri Olsa52502bf2012-10-31 15:52:47 +0100542 test_attr__init();
543
Ingo Molnar07800602009-04-20 15:00:56 +0200544 /*
545 * We use PATH to find perf commands, but we prepend some higher
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100546 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
Ingo Molnar07800602009-04-20 15:00:56 +0200547 * environment, and the $(perfexecdir) from the Makefile at build
548 * time.
549 */
550 setup_path();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300551 /*
552 * Block SIGWINCH notifications so that the thread that wants it can
553 * unblock and get syscalls like select interrupted instead of waiting
554 * forever while the signal goes to some other non interested thread.
555 */
556 pthread__block_sigwinch();
Ingo Molnar07800602009-04-20 15:00:56 +0200557
558 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200559 static int done_help;
Arnaldo Carvalho de Melob5ded712013-03-27 11:00:07 -0300560 int was_alias = run_argv(&argc, &argv);
Ingo Molnar8035e422009-06-06 15:19:13 +0200561
Ingo Molnar07800602009-04-20 15:00:56 +0200562 if (errno != ENOENT)
563 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200564
Ingo Molnar07800602009-04-20 15:00:56 +0200565 if (was_alias) {
566 fprintf(stderr, "Expansion of alias '%s' failed; "
567 "'%s' is not a perf-command\n",
568 cmd, argv[0]);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300569 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200570 }
571 if (!done_help) {
572 cmd = argv[0] = help_unknown_cmd(cmd);
573 done_help = 1;
574 } else
575 break;
576 }
577
578 fprintf(stderr, "Failed to run command '%s': %s\n",
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000579 cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300580out:
Ingo Molnar07800602009-04-20 15:00:56 +0200581 return 1;
582}