blob: 2826e702986e0da3c6fd4ce118a14454a9b2fe00 [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
Arnaldo Carvalho de Melo567e5472010-03-12 21:05:10 -030019bool use_browser;
20
Ingo Molnar07800602009-04-20 15:00:56 +020021const char perf_usage_string[] =
Ingo Molnarcc13a592009-04-20 16:01:30 +020022 "perf [--version] [--help] 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
27static int use_pager = -1;
28struct pager_config {
29 const char *cmd;
30 int val;
31};
32
Jason Baron5beeded2009-07-21 14:16:29 -040033static char debugfs_mntpt[MAXPATHLEN];
34
Ingo Molnar07800602009-04-20 15:00:56 +020035static int pager_command_config(const char *var, const char *value, void *data)
36{
37 struct pager_config *c = data;
38 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
39 c->val = perf_config_bool(var, value);
40 return 0;
41}
42
43/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
44int check_pager_config(const char *cmd)
45{
46 struct pager_config c;
47 c.cmd = cmd;
48 c.val = -1;
49 perf_config(pager_command_config, &c);
50 return c.val;
51}
52
Thiago Farina4c574152010-01-27 21:05:55 -020053static void commit_pager_choice(void)
54{
Ingo Molnar07800602009-04-20 15:00:56 +020055 switch (use_pager) {
56 case 0:
57 setenv("PERF_PAGER", "cat", 1);
58 break;
59 case 1:
60 /* setup_pager(); */
61 break;
62 default:
63 break;
64 }
65}
66
Jason Baron5beeded2009-07-21 14:16:29 -040067static void set_debugfs_path(void)
68{
69 char *path;
70
71 path = getenv(PERF_DEBUGFS_ENVIRONMENT);
72 snprintf(debugfs_path, MAXPATHLEN, "%s/%s", path ?: debugfs_mntpt,
73 "tracing/events");
74}
75
Thiago Farina4c574152010-01-27 21:05:55 -020076static int handle_options(const char ***argv, int *argc, int *envchanged)
Ingo Molnar07800602009-04-20 15:00:56 +020077{
78 int handled = 0;
79
80 while (*argc > 0) {
81 const char *cmd = (*argv)[0];
82 if (cmd[0] != '-')
83 break;
84
85 /*
86 * For legacy reasons, the "version" and "help"
87 * commands can be written with "--" prepended
88 * to make them look like flags.
89 */
90 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
91 break;
92
93 /*
94 * Check remaining flags.
95 */
Vincent Legollcfed95a2009-10-13 10:18:16 +020096 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
97 cmd += strlen(CMD_EXEC_PATH);
Ingo Molnar07800602009-04-20 15:00:56 +020098 if (*cmd == '=')
99 perf_set_argv_exec_path(cmd + 1);
100 else {
101 puts(perf_exec_path());
102 exit(0);
103 }
104 } else if (!strcmp(cmd, "--html-path")) {
105 puts(system_path(PERF_HTML_PATH));
106 exit(0);
107 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
108 use_pager = 1;
109 } else if (!strcmp(cmd, "--no-pager")) {
110 use_pager = 0;
111 if (envchanged)
112 *envchanged = 1;
113 } else if (!strcmp(cmd, "--perf-dir")) {
114 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200115 fprintf(stderr, "No directory given for --perf-dir.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200116 usage(perf_usage_string);
117 }
118 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
119 if (envchanged)
120 *envchanged = 1;
121 (*argv)++;
122 (*argc)--;
123 handled++;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200124 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
125 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200126 if (envchanged)
127 *envchanged = 1;
128 } else if (!strcmp(cmd, "--work-tree")) {
129 if (*argc < 2) {
Thiago Farina4c574152010-01-27 21:05:55 -0200130 fprintf(stderr, "No directory given for --work-tree.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200131 usage(perf_usage_string);
132 }
133 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
134 if (envchanged)
135 *envchanged = 1;
136 (*argv)++;
137 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200138 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
139 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200140 if (envchanged)
141 *envchanged = 1;
Jason Baron5beeded2009-07-21 14:16:29 -0400142 } else if (!strcmp(cmd, "--debugfs-dir")) {
143 if (*argc < 2) {
144 fprintf(stderr, "No directory given for --debugfs-dir.\n");
145 usage(perf_usage_string);
146 }
147 strncpy(debugfs_mntpt, (*argv)[1], MAXPATHLEN);
148 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
149 if (envchanged)
150 *envchanged = 1;
151 (*argv)++;
152 (*argc)--;
Vincent Legollcfed95a2009-10-13 10:18:16 +0200153 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
154 strncpy(debugfs_mntpt, cmd + strlen(CMD_DEBUGFS_DIR), MAXPATHLEN);
Jason Baron5beeded2009-07-21 14:16:29 -0400155 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
156 if (envchanged)
157 *envchanged = 1;
Ingo Molnar07800602009-04-20 15:00:56 +0200158 } else {
159 fprintf(stderr, "Unknown option: %s\n", cmd);
160 usage(perf_usage_string);
161 }
162
163 (*argv)++;
164 (*argc)--;
165 handled++;
166 }
167 return handled;
168}
169
170static int handle_alias(int *argcp, const char ***argv)
171{
172 int envchanged = 0, ret = 0, saved_errno = errno;
173 int count, option_count;
Thiago Farina4c574152010-01-27 21:05:55 -0200174 const char **new_argv;
Ingo Molnar07800602009-04-20 15:00:56 +0200175 const char *alias_command;
176 char *alias_string;
Ingo Molnar07800602009-04-20 15:00:56 +0200177
178 alias_command = (*argv)[0];
179 alias_string = alias_lookup(alias_command);
180 if (alias_string) {
181 if (alias_string[0] == '!') {
182 if (*argcp > 1) {
183 struct strbuf buf;
184
185 strbuf_init(&buf, PATH_MAX);
186 strbuf_addstr(&buf, alias_string);
187 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
188 free(alias_string);
189 alias_string = buf.buf;
190 }
191 ret = system(alias_string + 1);
192 if (ret >= 0 && WIFEXITED(ret) &&
193 WEXITSTATUS(ret) != 127)
194 exit(WEXITSTATUS(ret));
195 die("Failed to run '%s' when expanding alias '%s'",
196 alias_string + 1, alias_command);
197 }
198 count = split_cmdline(alias_string, &new_argv);
199 if (count < 0)
200 die("Bad alias.%s string", alias_command);
201 option_count = handle_options(&new_argv, &count, &envchanged);
202 if (envchanged)
203 die("alias '%s' changes environment variables\n"
204 "You can use '!perf' in the alias to do this.",
205 alias_command);
206 memmove(new_argv - option_count, new_argv,
207 count * sizeof(char *));
208 new_argv -= option_count;
209
210 if (count < 1)
211 die("empty alias for %s", alias_command);
212
213 if (!strcmp(alias_command, new_argv[0]))
214 die("recursive alias: %s", alias_command);
215
Thiago Farina4c574152010-01-27 21:05:55 -0200216 new_argv = realloc(new_argv, sizeof(char *) *
Ingo Molnar07800602009-04-20 15:00:56 +0200217 (count + *argcp + 1));
218 /* insert after command name */
Thiago Farina4c574152010-01-27 21:05:55 -0200219 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
220 new_argv[count + *argcp] = NULL;
Ingo Molnar07800602009-04-20 15:00:56 +0200221
222 *argv = new_argv;
223 *argcp += count - 1;
224
225 ret = 1;
226 }
227
228 errno = saved_errno;
229
230 return ret;
231}
232
233const char perf_version_string[] = PERF_VERSION;
234
235#define RUN_SETUP (1<<0)
236#define USE_PAGER (1<<1)
237/*
238 * require working tree to be present -- anything uses this needs
239 * RUN_SETUP for reading from the configuration file.
240 */
241#define NEED_WORK_TREE (1<<2)
242
243struct cmd_struct {
244 const char *cmd;
245 int (*fn)(int, const char **, const char *);
246 int option;
247};
248
249static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
250{
251 int status;
252 struct stat st;
253 const char *prefix;
254
255 prefix = NULL;
256 if (p->option & RUN_SETUP)
257 prefix = NULL; /* setup_perf_directory(); */
258
259 if (use_pager == -1 && p->option & RUN_SETUP)
260 use_pager = check_pager_config(p->cmd);
261 if (use_pager == -1 && p->option & USE_PAGER)
262 use_pager = 1;
263 commit_pager_choice();
Jason Baron5beeded2009-07-21 14:16:29 -0400264 set_debugfs_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200265
Ingo Molnar07800602009-04-20 15:00:56 +0200266 status = p->fn(argc, argv, prefix);
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300267 exit_browser(status);
268
Ingo Molnar07800602009-04-20 15:00:56 +0200269 if (status)
270 return status & 0xff;
271
272 /* Somebody closed stdout? */
273 if (fstat(fileno(stdout), &st))
274 return 0;
275 /* Ignore write errors for pipes and sockets.. */
276 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
277 return 0;
278
279 /* Check for ENOSPC and EIO errors.. */
280 if (fflush(stdout))
281 die("write failure on standard output: %s", strerror(errno));
282 if (ferror(stdout))
283 die("unknown write failure on standard output");
284 if (fclose(stdout))
285 die("close failed on standard output: %s", strerror(errno));
286 return 0;
287}
288
289static void handle_internal_command(int argc, const char **argv)
290{
291 const char *cmd = argv[0];
292 static struct cmd_struct commands[] = {
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200293 { "buildid-cache", cmd_buildid_cache, 0 },
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -0200294 { "buildid-list", cmd_buildid_list, 0 },
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200295 { "diff", cmd_diff, 0 },
Li Zefanba77c9e2009-11-20 15:53:25 +0800296 { "help", cmd_help, 0 },
297 { "list", cmd_list, 0 },
298 { "record", cmd_record, 0 },
299 { "report", cmd_report, 0 },
300 { "bench", cmd_bench, 0 },
301 { "stat", cmd_stat, 0 },
302 { "timechart", cmd_timechart, 0 },
303 { "top", cmd_top, 0 },
304 { "annotate", cmd_annotate, 0 },
305 { "version", cmd_version, 0 },
306 { "trace", cmd_trace, 0 },
307 { "sched", cmd_sched, 0 },
308 { "probe", cmd_probe, 0 },
309 { "kmem", cmd_kmem, 0 },
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900310 { "lock", cmd_lock, 0 },
Ingo Molnar07800602009-04-20 15:00:56 +0200311 };
Ingo Molnarf37a2912009-07-01 12:37:06 +0200312 unsigned int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200313 static const char ext[] = STRIP_EXTENSION;
314
315 if (sizeof(ext) > 1) {
316 i = strlen(argv[0]) - strlen(ext);
317 if (i > 0 && !strcmp(argv[0] + i, ext)) {
318 char *argv0 = strdup(argv[0]);
319 argv[0] = cmd = argv0;
320 argv0[i] = '\0';
321 }
322 }
323
324 /* Turn "perf cmd --help" into "perf help cmd" */
325 if (argc > 1 && !strcmp(argv[1], "--help")) {
326 argv[1] = argv[0];
327 argv[0] = cmd = "help";
328 }
329
330 for (i = 0; i < ARRAY_SIZE(commands); i++) {
331 struct cmd_struct *p = commands+i;
332 if (strcmp(p->cmd, cmd))
333 continue;
334 exit(run_builtin(p, argc, argv));
335 }
336}
337
338static void execv_dashed_external(const char **argv)
339{
340 struct strbuf cmd = STRBUF_INIT;
341 const char *tmp;
342 int status;
343
344 strbuf_addf(&cmd, "perf-%s", argv[0]);
345
346 /*
347 * argv[0] must be the perf command, but the argv array
348 * belongs to the caller, and may be reused in
349 * subsequent loop iterations. Save argv[0] and
350 * restore it on error.
351 */
352 tmp = argv[0];
353 argv[0] = cmd.buf;
354
355 /*
356 * if we fail because the command is not found, it is
357 * OK to return. Otherwise, we just pass along the status code.
358 */
359 status = run_command_v_opt(argv, 0);
360 if (status != -ERR_RUN_COMMAND_EXEC) {
361 if (IS_RUN_COMMAND_ERR(status))
362 die("unable to run '%s'", argv[0]);
363 exit(-status);
364 }
365 errno = ENOENT; /* as if we called execvp */
366
367 argv[0] = tmp;
368
369 strbuf_release(&cmd);
370}
371
372static int run_argv(int *argcp, const char ***argv)
373{
374 int done_alias = 0;
375
376 while (1) {
377 /* See if it's an internal command */
378 handle_internal_command(*argcp, *argv);
379
380 /* .. then try the external ones */
381 execv_dashed_external(*argv);
382
383 /* It could be an alias -- this works around the insanity
384 * of overriding "perf log" with "perf show" by having
385 * alias.log = show
386 */
387 if (done_alias || !handle_alias(argcp, argv))
388 break;
389 done_alias = 1;
390 }
391
392 return done_alias;
393}
394
Jason Baron5beeded2009-07-21 14:16:29 -0400395/* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
396static void get_debugfs_mntpt(void)
397{
Xiao Guangrong29c52aa2009-12-28 16:47:12 +0800398 const char *path = debugfs_mount(NULL);
Jason Baron5beeded2009-07-21 14:16:29 -0400399
Clark Williams549104f2009-11-08 09:03:07 -0600400 if (path)
401 strncpy(debugfs_mntpt, path, sizeof(debugfs_mntpt));
402 else
403 debugfs_mntpt[0] = '\0';
Jason Baron5beeded2009-07-21 14:16:29 -0400404}
Ingo Molnar07800602009-04-20 15:00:56 +0200405
406int main(int argc, const char **argv)
407{
408 const char *cmd;
409
410 cmd = perf_extract_argv0_path(argv[0]);
411 if (!cmd)
412 cmd = "perf-help";
Jason Baron5beeded2009-07-21 14:16:29 -0400413 /* get debugfs mount point from /proc/mounts */
414 get_debugfs_mntpt();
Ingo Molnar07800602009-04-20 15:00:56 +0200415 /*
416 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
417 *
418 * - cannot take flags in between the "perf" and the "xxxx".
419 * - cannot execute it externally (since it would just do
420 * the same thing over again)
421 *
422 * So we just directly call the internal command handler, and
423 * die if that one cannot handle it.
424 */
425 if (!prefixcmp(cmd, "perf-")) {
Peter Zijlstra266dfb02009-05-25 14:45:24 +0200426 cmd += 5;
Ingo Molnar07800602009-04-20 15:00:56 +0200427 argv[0] = cmd;
428 handle_internal_command(argc, argv);
429 die("cannot handle %s internally", cmd);
430 }
431
432 /* Look for flags.. */
433 argv++;
434 argc--;
435 handle_options(&argv, &argc, NULL);
436 commit_pager_choice();
Jason Baron5beeded2009-07-21 14:16:29 -0400437 set_debugfs_path();
Ingo Molnar07800602009-04-20 15:00:56 +0200438 if (argc > 0) {
439 if (!prefixcmp(argv[0], "--"))
440 argv[0] += 2;
441 } else {
442 /* The user didn't specify a command; give them help */
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100443 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200444 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100445 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200446 exit(1);
447 }
448 cmd = argv[0];
449
450 /*
451 * We use PATH to find perf commands, but we prepend some higher
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100452 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
Ingo Molnar07800602009-04-20 15:00:56 +0200453 * environment, and the $(perfexecdir) from the Makefile at build
454 * time.
455 */
456 setup_path();
457
458 while (1) {
Thiago Farina4c574152010-01-27 21:05:55 -0200459 static int done_help;
460 static int was_alias;
Ingo Molnar8035e422009-06-06 15:19:13 +0200461
Ingo Molnar07800602009-04-20 15:00:56 +0200462 was_alias = run_argv(&argc, &argv);
463 if (errno != ENOENT)
464 break;
Ingo Molnar8035e422009-06-06 15:19:13 +0200465
Ingo Molnar07800602009-04-20 15:00:56 +0200466 if (was_alias) {
467 fprintf(stderr, "Expansion of alias '%s' failed; "
468 "'%s' is not a perf-command\n",
469 cmd, argv[0]);
470 exit(1);
471 }
472 if (!done_help) {
473 cmd = argv[0] = help_unknown_cmd(cmd);
474 done_help = 1;
475 } else
476 break;
477 }
478
479 fprintf(stderr, "Failed to run command '%s': %s\n",
480 cmd, strerror(errno));
481
482 return 1;
483}