blob: 543713422d14568a8801a08f56a8938f4bc51564 [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"
Jiri Olsabbb2cea2014-07-17 12:55:00 +020018#include "util/debug.h"
Jiri Olsa592d5a62015-09-02 09:56:34 +020019#include <api/fs/tracing_path.h>
Irina Tirdea27683dc2012-09-08 03:43:19 +030020#include <pthread.h>
Ingo Molnar07800602009-04-20 15:00:56 +020021
22const char perf_usage_string[] =
Jiri Olsa78a1b502014-07-18 09:11:30 +020023 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
Ingo Molnar07800602009-04-20 15:00:56 +020024
25const char perf_more_info_string[] =
26 "See 'perf help COMMAND' for more information on a specific command.";
27
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030028int use_browser = -1;
Ingo Molnar07800602009-04-20 15:00:56 +020029static int use_pager = -1;
Feng Tang70cb4e92012-10-30 11:56:02 +080030const char *input_name;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030031
Frederic Weisbecker98a41792012-08-09 16:31:51 +020032struct cmd_struct {
33 const char *cmd;
34 int (*fn)(int, const char **, const char *);
35 int option;
36};
37
38static struct cmd_struct commands[] = {
39 { "buildid-cache", cmd_buildid_cache, 0 },
40 { "buildid-list", cmd_buildid_list, 0 },
41 { "diff", cmd_diff, 0 },
42 { "evlist", cmd_evlist, 0 },
43 { "help", cmd_help, 0 },
44 { "list", cmd_list, 0 },
45 { "record", cmd_record, 0 },
46 { "report", cmd_report, 0 },
47 { "bench", cmd_bench, 0 },
48 { "stat", cmd_stat, 0 },
49 { "timechart", cmd_timechart, 0 },
50 { "top", cmd_top, 0 },
51 { "annotate", cmd_annotate, 0 },
52 { "version", cmd_version, 0 },
53 { "script", cmd_script, 0 },
54 { "sched", cmd_sched, 0 },
Ingo Molnar89fe8082013-09-30 12:07:11 +020055#ifdef HAVE_LIBELF_SUPPORT
Frederic Weisbecker98a41792012-08-09 16:31:51 +020056 { "probe", cmd_probe, 0 },
Namhyung Kim393be2e2012-08-06 13:41:21 +090057#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020058 { "kmem", cmd_kmem, 0 },
59 { "lock", cmd_lock, 0 },
60 { "kvm", cmd_kvm, 0 },
61 { "test", cmd_test, 0 },
Ingo Molnar89fe8082013-09-30 12:07:11 +020062#ifdef HAVE_LIBAUDIT_SUPPORT
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030063 { "trace", cmd_trace, 0 },
Namhyung Kim4d290892012-09-27 20:23:38 +090064#endif
Frederic Weisbecker98a41792012-08-09 16:31:51 +020065 { "inject", cmd_inject, 0 },
Stephane Eranian028f12e2013-01-24 16:10:38 +010066 { "mem", cmd_mem, 0 },
Jiri Olsa2245bf12015-02-20 23:16:59 +010067 { "data", cmd_data, 0 },
Frederic Weisbecker98a41792012-08-09 16:31:51 +020068};
69
Ingo Molnar07800602009-04-20 15:00:56 +020070struct pager_config {
71 const char *cmd;
72 int val;
73};
74
75static int pager_command_config(const char *var, const char *value, void *data)
76{
77 struct pager_config *c = data;
78 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
79 c->val = perf_config_bool(var, value);
80 return 0;
81}
82
83/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
84int check_pager_config(const char *cmd)
85{
86 struct pager_config c;
87 c.cmd = cmd;
88 c.val = -1;
89 perf_config(pager_command_config, &c);
90 return c.val;
91}
92
Namhyung Kim0020ce22012-11-12 11:50:17 +090093static int browser_command_config(const char *var, const char *value, void *data)
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -030094{
95 struct pager_config *c = data;
96 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
97 c->val = perf_config_bool(var, value);
Namhyung Kim0020ce22012-11-12 11:50:17 +090098 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
99 c->val = perf_config_bool(var, value) ? 2 : 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300100 return 0;
101}
102
Namhyung Kim0020ce22012-11-12 11:50:17 +0900103/*
104 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
105 * and -1 for "not specified"
106 */
107static int check_browser_config(const char *cmd)
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300108{
109 struct pager_config c;
110 c.cmd = cmd;
111 c.val = -1;
Namhyung Kim0020ce22012-11-12 11:50:17 +0900112 perf_config(browser_command_config, &c);
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300113 return c.val;
114}
115
Thiago Farina4c574152010-01-27 21:05:55 -0200116static void commit_pager_choice(void)
117{
Ingo Molnar07800602009-04-20 15:00:56 +0200118 switch (use_pager) {
119 case 0:
120 setenv("PERF_PAGER", "cat", 1);
121 break;
122 case 1:
123 /* setup_pager(); */
124 break;
125 default:
126 break;
127 }
128}
129
Yunlong Song73353992015-02-27 18:21:31 +0800130struct option options[] = {
131 OPT_ARGUMENT("help", "help"),
132 OPT_ARGUMENT("version", "version"),
133 OPT_ARGUMENT("exec-path", "exec-path"),
134 OPT_ARGUMENT("html-path", "html-path"),
135 OPT_ARGUMENT("paginate", "paginate"),
136 OPT_ARGUMENT("no-pager", "no-pager"),
137 OPT_ARGUMENT("perf-dir", "perf-dir"),
138 OPT_ARGUMENT("work-tree", "work-tree"),
139 OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
140 OPT_ARGUMENT("buildid-dir", "buildid-dir"),
141 OPT_ARGUMENT("list-cmds", "list-cmds"),
142 OPT_ARGUMENT("list-opts", "list-opts"),
143 OPT_ARGUMENT("debug", "debug"),
144 OPT_END()
145};
146
Thiago Farina4c574152010-01-27 21:05:55 -0200147static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +0200148{
149 int handled = 0;
150
151 while (*argc > 0) {
152 const char *cmd = (*argv)[0];
153 if (cmd[0] != '-')
154 break;
155
156 /*
157 * For legacy reasons, the "version" and "help"
158 * commands can be written with "--" prepended
159 * to make them look like flags.
160 */
161 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
162 break;
163
164 /*
Jiri Olsaa1853e22015-10-05 20:06:09 +0200165 * Shortcut for '-h' and '-v' options to invoke help
166 * and version command.
167 */
168 if (!strcmp(cmd, "-h")) {
169 (*argv)[0] = "--help";
170 break;
171 }
172
173 if (!strcmp(cmd, "-v")) {
174 (*argv)[0] = "--version";
175 break;
176 }
177
178 /*
Ingo Molnar07800602009-04-20 15:00:56 +0200179 * Check remaining flags.
180 */
Vincent Legollcfed95a2009-10-13 10:18:16 +0200181 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
182 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +0200183 if (*cmd == '=')
184 perf_set_argv_exec_path(cmd + 1);
185 else {
186 puts(perf_exec_path());
187 exit(0);
188 }
189 } else if (!strcmp(cmd, "--html-path")) {
190 puts(system_path(PERF_HTML_PATH));
191 exit(0);
192 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
193 use_pager = 1;
194 } else if (!strcmp(cmd, "--no-pager")) {
195 use_pager = 0;
196 if (envchanged)
197 *envchanged = 1;
198 } else if (!strcmp(cmd, "--perf-dir")) {
199 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200200 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200201 usage(perf_usage_string);
202 }
203 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
204 if (envchanged)
205 *envchanged = 1;
206 (*argv)++;
207 (*argc)--;
208 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200209 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
210 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200211 if (envchanged)
212 *envchanged = 1;
213 } else if (!strcmp(cmd, "--work-tree")) {
214 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200215 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200216 usage(perf_usage_string);
217 }
218 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
219 if (envchanged)
220 *envchanged = 1;
221 (*argv)++;
222 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200223 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
224 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200225 if (envchanged)
226 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400227 } else if (!strcmp(cmd, "--debugfs-dir")) {
228 if (*argc < 2) {
229 fprintf(stderr, "No directory given for --debugfs-dir.\n");
230 usage(perf_usage_string);
231 }
Jiri Olsa65d4b262015-09-02 09:56:33 +0200232 tracing_path_set((*argv)[1]);
Jason Baron5beeded2009-07-21 14:16:29 -0400233 if (envchanged)
234 *envchanged = 1;
235 (*argv)++;
236 (*argc)--;
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100237 } else if (!strcmp(cmd, "--buildid-dir")) {
238 if (*argc < 2) {
239 fprintf(stderr, "No directory given for --buildid-dir.\n");
240 usage(perf_usage_string);
241 }
242 set_buildid_dir((*argv)[1]);
243 if (envchanged)
244 *envchanged = 1;
245 (*argv)++;
246 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200247 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
Jiri Olsa65d4b262015-09-02 09:56:33 +0200248 tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
Jiri Olsa9f30fff2015-08-26 15:46:45 +0200249 fprintf(stderr, "dir: %s\n", tracing_path);
Jason Baron5beeded2009-07-21 14:16:29 -0400250 if (envchanged)
251 *envchanged = 1;
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200252 } else if (!strcmp(cmd, "--list-cmds")) {
253 unsigned int i;
254
255 for (i = 0; i < ARRAY_SIZE(commands); i++) {
256 struct cmd_struct *p = commands+i;
257 printf("%s ", p->cmd);
258 }
Yunlong Songed457522015-02-27 18:21:29 +0800259 putchar('\n');
Frederic Weisbecker98a41792012-08-09 16:31:51 +0200260 exit(0);
Yunlong Song73353992015-02-27 18:21:31 +0800261 } else if (!strcmp(cmd, "--list-opts")) {
262 unsigned int i;
263
264 for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
265 struct option *p = options+i;
266 printf("--%s ", p->long_name);
267 }
268 putchar('\n');
269 exit(0);
Jiri Olsabbb2cea2014-07-17 12:55:00 +0200270 } else if (!strcmp(cmd, "--debug")) {
271 if (*argc < 2) {
272 fprintf(stderr, "No variable specified for --debug.\n");
273 usage(perf_usage_string);
274 }
275 if (perf_debug_option((*argv)[1]))
276 usage(perf_usage_string);
277
278 (*argv)++;
279 (*argc)--;
Ingo Molnar07800602009-04-20 15:00:56 +0200280 } else {
281 fprintf(stderr, "Unknown option: %s\n", cmd);
282 usage(perf_usage_string);
283 }
284
285 (*argv)++;
286 (*argc)--;
287 handled++;
288 }
289 return handled;
290}
291
292static int handle_alias(int *argcp, const char ***argv)
293{
294 int envchanged = 0, ret = 0, saved_errno = errno;
295 int count, option_count;
Thiago Farina4c574152010-01-27 21:05:55 -0200296 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200297 const char *alias_command;
298 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200299
300 alias_command = (*argv)[0];
301 alias_string = alias_lookup(alias_command);
302 if (alias_string) {
303 if (alias_string[0] == '!') {
304 if (*argcp > 1) {
305 struct strbuf buf;
306
307 strbuf_init(&buf, PATH_MAX);
308 strbuf_addstr(&buf, alias_string);
309 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
310 free(alias_string);
311 alias_string = buf.buf;
312 }
313 ret = system(alias_string + 1);
314 if (ret >= 0 && WIFEXITED(ret) &&
315 WEXITSTATUS(ret) != 127)
316 exit(WEXITSTATUS(ret));
317 die("Failed to run '%s' when expanding alias '%s'",
318 alias_string + 1, alias_command);
319 }
320 count = split_cmdline(alias_string, &new_argv);
321 if (count < 0)
322 die("Bad alias.%s string", alias_command);
323 option_count = handle_options(&new_argv, &count, &envchanged);
324 if (envchanged)
325 die("alias '%s' changes environment variables\n"
326 "You can use '!perf' in the alias to do this.",
327 alias_command);
328 memmove(new_argv - option_count, new_argv,
329 count * sizeof(char *));
330 new_argv -= option_count;
331
332 if (count < 1)
333 die("empty alias for %s", alias_command);
334
335 if (!strcmp(alias_command, new_argv[0]))
336 die("recursive alias: %s", alias_command);
337
Thiago Farina4c574152010-01-27 21:05:55 -0200338 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200339 (count + *argcp + 1));
340 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200341 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
342 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200343
344 *argv = new_argv;
345 *argcp += count - 1;
346
347 ret = 1;
348 }
349
350 errno = saved_errno;
351
352 return ret;
353}
354
355const char perf_version_string[] = PERF_VERSION;
356
357#define RUN_SETUP (1<<0)
358#define USE_PAGER (1<<1)
359/*
360 * require working tree to be present -- anything uses this needs
361 * RUN_SETUP for reading from the configuration file.
362 */
363#define NEED_WORK_TREE (1<<2)
364
Ingo Molnar07800602009-04-20 15:00:56 +0200365static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
366{
367 int status;
368 struct stat st;
369 const char *prefix;
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000370 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200371
372 prefix = NULL;
373 if (p->option & RUN_SETUP)
374 prefix = NULL; /* setup_perf_directory(); */
375
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300376 if (use_browser == -1)
Namhyung Kim0020ce22012-11-12 11:50:17 +0900377 use_browser = check_browser_config(p->cmd);
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300378
Ingo Molnar07800602009-04-20 15:00:56 +0200379 if (use_pager == -1 && p->option & RUN_SETUP)
380 use_pager = check_pager_config(p->cmd);
381 if (use_pager == -1 && p->option & USE_PAGER)
382 use_pager = 1;
383 commit_pager_choice();
384
Ingo Molnar07800602009-04-20 15:00:56 +0200385 status = p->fn(argc, argv, prefix);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300386 exit_browser(status);
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300387 perf_env__exit(&perf_env);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300388
Ingo Molnar07800602009-04-20 15:00:56 +0200389 if (status)
390 return status & 0xff;
391
392 /* Somebody closed stdout? */
393 if (fstat(fileno(stdout), &st))
394 return 0;
395 /* Ignore write errors for pipes and sockets.. */
396 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
397 return 0;
398
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300399 status = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200400 /* Check for ENOSPC and EIO errors.. */
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300401 if (fflush(stdout)) {
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000402 fprintf(stderr, "write failure on standard output: %s",
403 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300404 goto out;
405 }
406 if (ferror(stdout)) {
407 fprintf(stderr, "unknown write failure on standard output");
408 goto out;
409 }
410 if (fclose(stdout)) {
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000411 fprintf(stderr, "close failed on standard output: %s",
412 strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300413 goto out;
414 }
415 status = 0;
416out:
417 return status;
Ingo Molnar07800602009-04-20 15:00:56 +0200418}
419
420static void handle_internal_command(int argc, const char **argv)
421{
422 const char *cmd = argv[0];
Ingo Molnarf37a2912009-07-01 12:37:06 +0200423 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200424 static const char ext[] = STRIP_EXTENSION;
425
426 if (sizeof(ext) > 1) {
427 i = strlen(argv[0]) - strlen(ext);
428 if (i > 0 && !strcmp(argv[0] + i, ext)) {
429 char *argv0 = strdup(argv[0]);
430 argv[0] = cmd = argv0;
431 argv0[i] = '\0';
432 }
433 }
434
435 /* Turn "perf cmd --help" into "perf help cmd" */
436 if (argc > 1 && !strcmp(argv[1], "--help")) {
437 argv[1] = argv[0];
438 argv[0] = cmd = "help";
439 }
440
441 for (i = 0; i < ARRAY_SIZE(commands); i++) {
442 struct cmd_struct *p = commands+i;
443 if (strcmp(p->cmd, cmd))
444 continue;
445 exit(run_builtin(p, argc, argv));
446 }
447}
448
449static void execv_dashed_external(const char **argv)
450{
451 struct strbuf cmd = STRBUF_INIT;
452 const char *tmp;
453 int status;
454
455 strbuf_addf(&cmd, "perf-%s", argv[0]);
456
457 /*
458 * argv[0] must be the perf command, but the argv array
459 * belongs to the caller, and may be reused in
460 * subsequent loop iterations. Save argv[0] and
461 * restore it on error.
462 */
463 tmp = argv[0];
464 argv[0] = cmd.buf;
465
466 /*
467 * if we fail because the command is not found, it is
468 * OK to return. Otherwise, we just pass along the status code.
469 */
470 status = run_command_v_opt(argv, 0);
471 if (status != -ERR_RUN_COMMAND_EXEC) {
472 if (IS_RUN_COMMAND_ERR(status))
473 die("unable to run '%s'", argv[0]);
474 exit(-status);
475 }
476 errno = ENOENT; /* as if we called execvp */
477
478 argv[0] = tmp;
479
480 strbuf_release(&cmd);
481}
482
483static int run_argv(int *argcp, const char ***argv)
484{
485 int done_alias = 0;
486
487 while (1) {
488 /* See if it's an internal command */
489 handle_internal_command(*argcp, *argv);
490
491 /* .. then try the external ones */
492 execv_dashed_external(*argv);
493
494 /* It could be an alias -- this works around the insanity
495 * of overriding "perf log" with "perf show" by having
496 * alias.log = show
497 */
498 if (done_alias || !handle_alias(argcp, argv))
499 break;
500 done_alias = 1;
501 }
502
503 return done_alias;
504}
505
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300506static void pthread__block_sigwinch(void)
507{
508 sigset_t set;
509
510 sigemptyset(&set);
511 sigaddset(&set, SIGWINCH);
512 pthread_sigmask(SIG_BLOCK, &set, NULL);
513}
514
515void pthread__unblock_sigwinch(void)
516{
517 sigset_t set;
518
519 sigemptyset(&set);
520 sigaddset(&set, SIGWINCH);
521 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
522}
523
Ingo Molnar07800602009-04-20 15:00:56 +0200524int main(int argc, const char **argv)
525{
526 const char *cmd;
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000527 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200528
Jiri Olsa918512b2013-09-12 18:39:35 +0200529 /* The page_size is placed in util object. */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300530 page_size = sysconf(_SC_PAGE_SIZE);
Don Zickus2b1b7102014-05-30 16:10:05 -0400531 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -0300532
Ingo Molnar07800602009-04-20 15:00:56 +0200533 cmd = perf_extract_argv0_path(argv[0]);
534 if (!cmd)
535 cmd = "perf-help";
Jiri Olsa65d4b262015-09-02 09:56:33 +0200536
537 /* get debugfs/tracefs mount point from /proc/mounts */
538 tracing_path_mount();
539
Ingo Molnar07800602009-04-20 15:00:56 +0200540 /*
541 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
542 *
543 * - cannot take flags in between the "perf" and the "xxxx".
544 * - cannot execute it externally (since it would just do
545 * the same thing over again)
546 *
547 * So we just directly call the internal command handler, and
548 * die if that one cannot handle it.
549 */
550 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200551 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200552 argv[0] = cmd;
553 handle_internal_command(argc, argv);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300554 fprintf(stderr, "cannot handle %s internally", cmd);
555 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200556 }
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300557 if (!prefixcmp(cmd, "trace")) {
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300558#ifdef HAVE_LIBAUDIT_SUPPORT
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100559 set_buildid_dir(NULL);
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300560 setup_path();
561 argv[0] = "trace";
562 return cmd_trace(argc, argv, NULL);
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300563#else
564 fprintf(stderr,
565 "trace command not available: missing audit-libs devel package at build time.\n");
566 goto out;
Arnaldo Carvalho de Melob52bc232013-09-24 11:56:36 -0300567#endif
Arnaldo Carvalho de Melo1b5726222014-05-26 16:02:29 -0300568 }
Ingo Molnar07800602009-04-20 15:00:56 +0200569 /* Look for flags.. */
570 argv++;
571 argc--;
572 handle_options(&argv, &argc, NULL);
573 commit_pager_choice();
Jiri Olsa99ce8e92014-12-01 20:06:24 +0100574 set_buildid_dir(NULL);
Stephane Eranian45de34b2010-06-01 21:25:01 +0200575
Ingo Molnar07800602009-04-20 15:00:56 +0200576 if (argc > 0) {
577 if (!prefixcmp(argv[0], "--"))
578 argv[0] += 2;
579 } else {
580 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100581 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200582 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100583 printf("\n %s\n\n", perf_more_info_string);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300584 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200585 }
586 cmd = argv[0];
587
Jiri Olsa52502bf2012-10-31 15:52:47 +0100588 test_attr__init();
589
Ingo Molnar07800602009-04-20 15:00:56 +0200590 /*
591 * We use PATH to find perf commands, but we prepend some higher
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100592 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
Ingo Molnar07800602009-04-20 15:00:56 +0200593 * environment, and the $(perfexecdir) from the Makefile at build
594 * time.
595 */
596 setup_path();
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300597 /*
598 * Block SIGWINCH notifications so that the thread that wants it can
599 * unblock and get syscalls like select interrupted instead of waiting
600 * forever while the signal goes to some other non interested thread.
601 */
602 pthread__block_sigwinch();
Ingo Molnar07800602009-04-20 15:00:56 +0200603
604 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200605 static int done_help;
Arnaldo Carvalho de Melob5ded712013-03-27 11:00:07 -0300606 int was_alias = run_argv(&argc, &argv);
Ingo Molnar8035e422009-06-06 15:19:13 +0200607
Ingo Molnar07800602009-04-20 15:00:56 +0200608 if (errno != ENOENT)
609 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200610
Ingo Molnar07800602009-04-20 15:00:56 +0200611 if (was_alias) {
612 fprintf(stderr, "Expansion of alias '%s' failed; "
613 "'%s' is not a perf-command\n",
614 cmd, argv[0]);
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300615 goto out;
Ingo Molnar07800602009-04-20 15:00:56 +0200616 }
617 if (!done_help) {
618 cmd = argv[0] = help_unknown_cmd(cmd);
619 done_help = 1;
620 } else
621 break;
622 }
623
624 fprintf(stderr, "Failed to run command '%s': %s\n",
Masami Hiramatsub2348e12014-08-14 02:22:32 +0000625 cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo2a16bf82013-01-24 15:18:54 -0300626out:
Ingo Molnar07800602009-04-20 15:00:56 +0200627 return 1;
628}