blob: 9ff186b57cb7715f5d9cfdae8e7590543038ffce [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"
16#include "util/string.h"
Clark Williams549104f2009-11-08 09:03:07 -060017#include "util/debugfs.h"
Ingo Molnar07800602009-04-20 15:00:56 +020018
19const char perf_usage_string[] =
Ingo Molnarcc13a592009-04-20 16:01:30 +020020 "perf [--version] [--help] COMMAND [ARGS]";
Ingo Molnar07800602009-04-20 15:00:56 +020021
22const char perf_more_info_string[] =
23 "See 'perf help COMMAND' for more information on a specific command.";
24
25static int use_pager = -1;
26struct pager_config {
27 const char *cmd;
28 int val;
29};
30
Jason Baron5beeded2009-07-21 14:16:29 -040031static char debugfs_mntpt[MAXPATHLEN];
32
Ingo Molnar07800602009-04-20 15:00:56 +020033static int pager_command_config(const char *var, const char *value, void *data)
34{
35 struct pager_config *c = data;
36 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
37 c->val = perf_config_bool(var, value);
38 return 0;
39}
40
41/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
42int check_pager_config(const char *cmd)
43{
44 struct pager_config c;
45 c.cmd = cmd;
46 c.val = -1;
47 perf_config(pager_command_config, &c);
48 return c.val;
49}
50
Thiago Farina4c574152010-01-27 21:05:55 -020051static void commit_pager_choice(void)
52{
Ingo Molnar07800602009-04-20 15:00:56 +020053 switch (use_pager) {
54 case 0:
55 setenv("PERF_PAGER", "cat", 1);
56 break;
57 case 1:
58 /* setup_pager(); */
59 break;
60 default:
61 break;
62 }
63}
64
Jason Baron5beeded2009-07-21 14:16:29 -040065static void set_debugfs_path(void)
66{
67 char *path;
68
69 path = getenv(PERF_DEBUGFS_ENVIRONMENT);
70 snprintf(debugfs_path, MAXPATHLEN, "%s/%s", path ?: debugfs_mntpt,
71 "tracing/events");
72}
73
Thiago Farina4c574152010-01-27 21:05:55 -020074static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +020075{
76 int handled = 0;
77
78 while (*argc > 0) {
79 const char *cmd = (*argv)[0];
80 if (cmd[0] != '-')
81 break;
82
83 /*
84 * For legacy reasons, the "version" and "help"
85 * commands can be written with "--" prepended
86 * to make them look like flags.
87 */
88 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
89 break;
90
91 /*
92 * Check remaining flags.
93 */
Vincent Legollcfed95a2009-10-13 10:18:16 +020094 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
95 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +020096 if (*cmd == '=')
97 perf_set_argv_exec_path(cmd + 1);
98 else {
99 puts(perf_exec_path());
100 exit(0);
101 }
102 } else if (!strcmp(cmd, "--html-path")) {
103 puts(system_path(PERF_HTML_PATH));
104 exit(0);
105 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
106 use_pager = 1;
107 } else if (!strcmp(cmd, "--no-pager")) {
108 use_pager = 0;
109 if (envchanged)
110 *envchanged = 1;
111 } else if (!strcmp(cmd, "--perf-dir")) {
112 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200113 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200114 usage(perf_usage_string);
115 }
116 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
117 if (envchanged)
118 *envchanged = 1;
119 (*argv)++;
120 (*argc)--;
121 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200122 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
123 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200124 if (envchanged)
125 *envchanged = 1;
126 } else if (!strcmp(cmd, "--work-tree")) {
127 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200128 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200129 usage(perf_usage_string);
130 }
131 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
132 if (envchanged)
133 *envchanged = 1;
134 (*argv)++;
135 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200136 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
137 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200138 if (envchanged)
139 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400140 } else if (!strcmp(cmd, "--debugfs-dir")) {
141 if (*argc < 2) {
142 fprintf(stderr, "No directory given for --debugfs-dir.\n");
143 usage(perf_usage_string);
144 }
145 strncpy(debugfs_mntpt, (*argv)[1], MAXPATHLEN);
146 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
147 if (envchanged)
148 *envchanged = 1;
149 (*argv)++;
150 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200151 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
152 strncpy(debugfs_mntpt, cmd + strlen(CMD_DEBUGFS_DIR), MAXPATHLEN);
Jason Baron5beeded2009-07-21 14:16:29 -0400153 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
154 if (envchanged)
155 *envchanged = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200156 } else {
157 fprintf(stderr, "Unknown option: %s\n", cmd);
158 usage(perf_usage_string);
159 }
160
161 (*argv)++;
162 (*argc)--;
163 handled++;
164 }
165 return handled;
166}
167
168static int handle_alias(int *argcp, const char ***argv)
169{
170 int envchanged = 0, ret = 0, saved_errno = errno;
171 int count, option_count;
Thiago Farina4c574152010-01-27 21:05:55 -0200172 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200173 const char *alias_command;
174 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200175
176 alias_command = (*argv)[0];
177 alias_string = alias_lookup(alias_command);
178 if (alias_string) {
179 if (alias_string[0] == '!') {
180 if (*argcp > 1) {
181 struct strbuf buf;
182
183 strbuf_init(&buf, PATH_MAX);
184 strbuf_addstr(&buf, alias_string);
185 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
186 free(alias_string);
187 alias_string = buf.buf;
188 }
189 ret = system(alias_string + 1);
190 if (ret >= 0 && WIFEXITED(ret) &&
191 WEXITSTATUS(ret) != 127)
192 exit(WEXITSTATUS(ret));
193 die("Failed to run '%s' when expanding alias '%s'",
194 alias_string + 1, alias_command);
195 }
196 count = split_cmdline(alias_string, &new_argv);
197 if (count < 0)
198 die("Bad alias.%s string", alias_command);
199 option_count = handle_options(&new_argv, &count, &envchanged);
200 if (envchanged)
201 die("alias '%s' changes environment variables\n"
202 "You can use '!perf' in the alias to do this.",
203 alias_command);
204 memmove(new_argv - option_count, new_argv,
205 count * sizeof(char *));
206 new_argv -= option_count;
207
208 if (count < 1)
209 die("empty alias for %s", alias_command);
210
211 if (!strcmp(alias_command, new_argv[0]))
212 die("recursive alias: %s", alias_command);
213
Thiago Farina4c574152010-01-27 21:05:55 -0200214 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200215 (count + *argcp + 1));
216 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200217 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
218 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200219
220 *argv = new_argv;
221 *argcp += count - 1;
222
223 ret = 1;
224 }
225
226 errno = saved_errno;
227
228 return ret;
229}
230
231const char perf_version_string[] = PERF_VERSION;
232
233#define RUN_SETUP (1<<0)
234#define USE_PAGER (1<<1)
235/*
236 * require working tree to be present -- anything uses this needs
237 * RUN_SETUP for reading from the configuration file.
238 */
239#define NEED_WORK_TREE (1<<2)
240
241struct cmd_struct {
242 const char *cmd;
243 int (*fn)(int, const char **, const char *);
244 int option;
245};
246
247static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
248{
249 int status;
250 struct stat st;
251 const char *prefix;
252
253 prefix = NULL;
254 if (p->option & RUN_SETUP)
255 prefix = NULL; /* setup_perf_directory(); */
256
257 if (use_pager == -1 && p->option & RUN_SETUP)
258 use_pager = check_pager_config(p->cmd);
259 if (use_pager == -1 && p->option & USE_PAGER)
260 use_pager = 1;
261 commit_pager_choice();
Jason Baron5beeded2009-07-21 14:16:29 -0400262 set_debugfs_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200263
Ingo Molnar07800602009-04-20 15:00:56 +0200264 status = p->fn(argc, argv, prefix);
265 if (status)
266 return status & 0xff;
267
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300268 exit_browser();
269
Ingo Molnar07800602009-04-20 15:00:56 +0200270 /* Somebody closed stdout? */
271 if (fstat(fileno(stdout), &st))
272 return 0;
273 /* Ignore write errors for pipes and sockets.. */
274 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
275 return 0;
276
277 /* Check for ENOSPC and EIO errors.. */
278 if (fflush(stdout))
279 die("write failure on standard output: %s", strerror(errno));
280 if (ferror(stdout))
281 die("unknown write failure on standard output");
282 if (fclose(stdout))
283 die("close failed on standard output: %s", strerror(errno));
284 return 0;
285}
286
287static void handle_internal_command(int argc, const char **argv)
288{
289 const char *cmd = argv[0];
290 static struct cmd_struct commands[] = {
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200291 { "buildid-cache", cmd_buildid_cache, 0 },
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -0200292 { "buildid-list", cmd_buildid_list, 0 },
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200293 { "diff", cmd_diff, 0 },
Li Zefanba77c9e2009-11-20 15:53:25 +0800294 { "help", cmd_help, 0 },
295 { "list", cmd_list, 0 },
296 { "record", cmd_record, 0 },
297 { "report", cmd_report, 0 },
298 { "bench", cmd_bench, 0 },
299 { "stat", cmd_stat, 0 },
300 { "timechart", cmd_timechart, 0 },
301 { "top", cmd_top, 0 },
302 { "annotate", cmd_annotate, 0 },
303 { "version", cmd_version, 0 },
304 { "trace", cmd_trace, 0 },
305 { "sched", cmd_sched, 0 },
306 { "probe", cmd_probe, 0 },
307 { "kmem", cmd_kmem, 0 },
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900308 { "lock", cmd_lock, 0 },
Ingo Molnar07800602009-04-20 15:00:56 +0200309 };
Ingo Molnarf37a2912009-07-01 12:37:06 +0200310 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200311 static const char ext[] = STRIP_EXTENSION;
312
313 if (sizeof(ext) > 1) {
314 i = strlen(argv[0]) - strlen(ext);
315 if (i > 0 && !strcmp(argv[0] + i, ext)) {
316 char *argv0 = strdup(argv[0]);
317 argv[0] = cmd = argv0;
318 argv0[i] = '\0';
319 }
320 }
321
322 /* Turn "perf cmd --help" into "perf help cmd" */
323 if (argc > 1 && !strcmp(argv[1], "--help")) {
324 argv[1] = argv[0];
325 argv[0] = cmd = "help";
326 }
327
328 for (i = 0; i < ARRAY_SIZE(commands); i++) {
329 struct cmd_struct *p = commands+i;
330 if (strcmp(p->cmd, cmd))
331 continue;
332 exit(run_builtin(p, argc, argv));
333 }
334}
335
336static void execv_dashed_external(const char **argv)
337{
338 struct strbuf cmd = STRBUF_INIT;
339 const char *tmp;
340 int status;
341
342 strbuf_addf(&cmd, "perf-%s", argv[0]);
343
344 /*
345 * argv[0] must be the perf command, but the argv array
346 * belongs to the caller, and may be reused in
347 * subsequent loop iterations. Save argv[0] and
348 * restore it on error.
349 */
350 tmp = argv[0];
351 argv[0] = cmd.buf;
352
353 /*
354 * if we fail because the command is not found, it is
355 * OK to return. Otherwise, we just pass along the status code.
356 */
357 status = run_command_v_opt(argv, 0);
358 if (status != -ERR_RUN_COMMAND_EXEC) {
359 if (IS_RUN_COMMAND_ERR(status))
360 die("unable to run '%s'", argv[0]);
361 exit(-status);
362 }
363 errno = ENOENT; /* as if we called execvp */
364
365 argv[0] = tmp;
366
367 strbuf_release(&cmd);
368}
369
370static int run_argv(int *argcp, const char ***argv)
371{
372 int done_alias = 0;
373
374 while (1) {
375 /* See if it's an internal command */
376 handle_internal_command(*argcp, *argv);
377
378 /* .. then try the external ones */
379 execv_dashed_external(*argv);
380
381 /* It could be an alias -- this works around the insanity
382 * of overriding "perf log" with "perf show" by having
383 * alias.log = show
384 */
385 if (done_alias || !handle_alias(argcp, argv))
386 break;
387 done_alias = 1;
388 }
389
390 return done_alias;
391}
392
Jason Baron5beeded2009-07-21 14:16:29 -0400393/* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
394static void get_debugfs_mntpt(void)
395{
Xiao Guangrong29c52aa2009-12-28 16:47:12 +0800396 const char *path = debugfs_mount(NULL);
Jason Baron5beeded2009-07-21 14:16:29 -0400397
Clark Williams549104f2009-11-08 09:03:07 -0600398 if (path)
399 strncpy(debugfs_mntpt, path, sizeof(debugfs_mntpt));
400 else
401 debugfs_mntpt[0] = '\0';
Jason Baron5beeded2009-07-21 14:16:29 -0400402}
Ingo Molnar07800602009-04-20 15:00:56 +0200403
404int main(int argc, const char **argv)
405{
406 const char *cmd;
407
408 cmd = perf_extract_argv0_path(argv[0]);
409 if (!cmd)
410 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400411 /* get debugfs mount point from /proc/mounts */
412 get_debugfs_mntpt();
Ingo Molnar07800602009-04-20 15:00:56 +0200413 /*
414 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
415 *
416 * - cannot take flags in between the "perf" and the "xxxx".
417 * - cannot execute it externally (since it would just do
418 * the same thing over again)
419 *
420 * So we just directly call the internal command handler, and
421 * die if that one cannot handle it.
422 */
423 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200424 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200425 argv[0] = cmd;
426 handle_internal_command(argc, argv);
427 die("cannot handle %s internally", cmd);
428 }
429
430 /* Look for flags.. */
431 argv++;
432 argc--;
433 handle_options(&argv, &argc, NULL);
434 commit_pager_choice();
Jason Baron5beeded2009-07-21 14:16:29 -0400435 set_debugfs_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200436 if (argc > 0) {
437 if (!prefixcmp(argv[0], "--"))
438 argv[0] += 2;
439 } else {
440 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100441 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200442 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100443 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200444 exit(1);
445 }
446 cmd = argv[0];
447
448 /*
449 * We use PATH to find perf commands, but we prepend some higher
450 * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
451 * environment, and the $(perfexecdir) from the Makefile at build
452 * time.
453 */
454 setup_path();
455
456 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200457 static int done_help;
458 static int was_alias;
Ingo Molnar8035e422009-06-06 15:19:13 +0200459
Ingo Molnar07800602009-04-20 15:00:56 +0200460 was_alias = run_argv(&argc, &argv);
461 if (errno != ENOENT)
462 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200463
Ingo Molnar07800602009-04-20 15:00:56 +0200464 if (was_alias) {
465 fprintf(stderr, "Expansion of alias '%s' failed; "
466 "'%s' is not a perf-command\n",
467 cmd, argv[0]);
468 exit(1);
469 }
470 if (!done_help) {
471 cmd = argv[0] = help_unknown_cmd(cmd);
472 done_help = 1;
473 } else
474 break;
475 }
476
477 fprintf(stderr, "Failed to run command '%s': %s\n",
478 cmd, strerror(errno));
479
480 return 1;
481}