tools/perf/perf.c: Clean up trivial style issues
[linux-2.6/x86.git] / tools / perf / perf.c
blob109b89b30cedd34204a8d9054235d4f4543cd382
1 /*
2 * perf.c
4 * Performance analysis utility.
6 * This is the main hub from which the sub-commands (perf stat,
7 * perf top, perf record, perf report, etc.) are started.
8 */
9 #include "builtin.h"
11 #include "util/exec_cmd.h"
12 #include "util/cache.h"
13 #include "util/quote.h"
14 #include "util/run-command.h"
15 #include "util/parse-events.h"
16 #include "util/string.h"
17 #include "util/debugfs.h"
19 const char perf_usage_string[] =
20 "perf [--version] [--help] COMMAND [ARGS]";
22 const char perf_more_info_string[] =
23 "See 'perf help COMMAND' for more information on a specific command.";
25 static int use_pager = -1;
26 struct pager_config {
27 const char *cmd;
28 int val;
31 static char debugfs_mntpt[MAXPATHLEN];
33 static int pager_command_config(const char *var, const char *value, void *data)
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;
41 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
42 int check_pager_config(const char *cmd)
44 struct pager_config c;
45 c.cmd = cmd;
46 c.val = -1;
47 perf_config(pager_command_config, &c);
48 return c.val;
51 static void commit_pager_choice(void)
53 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;
65 static void set_debugfs_path(void)
67 char *path;
69 path = getenv(PERF_DEBUGFS_ENVIRONMENT);
70 snprintf(debugfs_path, MAXPATHLEN, "%s/%s", path ?: debugfs_mntpt,
71 "tracing/events");
74 static int handle_options(const char ***argv, int *argc, int *envchanged)
76 int handled = 0;
78 while (*argc > 0) {
79 const char *cmd = (*argv)[0];
80 if (cmd[0] != '-')
81 break;
84 * For legacy reasons, the "version" and "help"
85 * commands can be written with "--" prepended
86 * to make them look like flags.
88 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
89 break;
92 * Check remaining flags.
94 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
95 cmd += strlen(CMD_EXEC_PATH);
96 if (*cmd == '=')
97 perf_set_argv_exec_path(cmd + 1);
98 else {
99 puts(perf_exec_path());
100 exit(0);
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) {
113 fprintf(stderr, "No directory given for --perf-dir.\n");
114 usage(perf_usage_string);
116 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
117 if (envchanged)
118 *envchanged = 1;
119 (*argv)++;
120 (*argc)--;
121 handled++;
122 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
123 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
124 if (envchanged)
125 *envchanged = 1;
126 } else if (!strcmp(cmd, "--work-tree")) {
127 if (*argc < 2) {
128 fprintf(stderr, "No directory given for --work-tree.\n");
129 usage(perf_usage_string);
131 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
132 if (envchanged)
133 *envchanged = 1;
134 (*argv)++;
135 (*argc)--;
136 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
137 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
138 if (envchanged)
139 *envchanged = 1;
140 } 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);
145 strncpy(debugfs_mntpt, (*argv)[1], MAXPATHLEN);
146 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
147 if (envchanged)
148 *envchanged = 1;
149 (*argv)++;
150 (*argc)--;
151 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
152 strncpy(debugfs_mntpt, cmd + strlen(CMD_DEBUGFS_DIR), MAXPATHLEN);
153 debugfs_mntpt[MAXPATHLEN - 1] = '\0';
154 if (envchanged)
155 *envchanged = 1;
156 } else {
157 fprintf(stderr, "Unknown option: %s\n", cmd);
158 usage(perf_usage_string);
161 (*argv)++;
162 (*argc)--;
163 handled++;
165 return handled;
168 static int handle_alias(int *argcp, const char ***argv)
170 int envchanged = 0, ret = 0, saved_errno = errno;
171 int count, option_count;
172 const char **new_argv;
173 const char *alias_command;
174 char *alias_string;
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;
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;
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);
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;
208 if (count < 1)
209 die("empty alias for %s", alias_command);
211 if (!strcmp(alias_command, new_argv[0]))
212 die("recursive alias: %s", alias_command);
214 new_argv = realloc(new_argv, sizeof(char *) *
215 (count + *argcp + 1));
216 /* insert after command name */
217 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
218 new_argv[count + *argcp] = NULL;
220 *argv = new_argv;
221 *argcp += count - 1;
223 ret = 1;
226 errno = saved_errno;
228 return ret;
231 const char perf_version_string[] = PERF_VERSION;
233 #define RUN_SETUP (1<<0)
234 #define USE_PAGER (1<<1)
236 * require working tree to be present -- anything uses this needs
237 * RUN_SETUP for reading from the configuration file.
239 #define NEED_WORK_TREE (1<<2)
241 struct cmd_struct {
242 const char *cmd;
243 int (*fn)(int, const char **, const char *);
244 int option;
247 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
249 int status;
250 struct stat st;
251 const char *prefix;
253 prefix = NULL;
254 if (p->option & RUN_SETUP)
255 prefix = NULL; /* setup_perf_directory(); */
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();
262 set_debugfs_path();
264 status = p->fn(argc, argv, prefix);
265 if (status)
266 return status & 0xff;
268 /* Somebody closed stdout? */
269 if (fstat(fileno(stdout), &st))
270 return 0;
271 /* Ignore write errors for pipes and sockets.. */
272 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
273 return 0;
275 /* Check for ENOSPC and EIO errors.. */
276 if (fflush(stdout))
277 die("write failure on standard output: %s", strerror(errno));
278 if (ferror(stdout))
279 die("unknown write failure on standard output");
280 if (fclose(stdout))
281 die("close failed on standard output: %s", strerror(errno));
282 return 0;
285 static void handle_internal_command(int argc, const char **argv)
287 const char *cmd = argv[0];
288 static struct cmd_struct commands[] = {
289 { "buildid-cache", cmd_buildid_cache, 0 },
290 { "buildid-list", cmd_buildid_list, 0 },
291 { "diff", cmd_diff, 0 },
292 { "help", cmd_help, 0 },
293 { "list", cmd_list, 0 },
294 { "record", cmd_record, 0 },
295 { "report", cmd_report, 0 },
296 { "bench", cmd_bench, 0 },
297 { "stat", cmd_stat, 0 },
298 { "timechart", cmd_timechart, 0 },
299 { "top", cmd_top, 0 },
300 { "annotate", cmd_annotate, 0 },
301 { "version", cmd_version, 0 },
302 { "trace", cmd_trace, 0 },
303 { "sched", cmd_sched, 0 },
304 { "probe", cmd_probe, 0 },
305 { "kmem", cmd_kmem, 0 },
307 unsigned int i;
308 static const char ext[] = STRIP_EXTENSION;
310 if (sizeof(ext) > 1) {
311 i = strlen(argv[0]) - strlen(ext);
312 if (i > 0 && !strcmp(argv[0] + i, ext)) {
313 char *argv0 = strdup(argv[0]);
314 argv[0] = cmd = argv0;
315 argv0[i] = '\0';
319 /* Turn "perf cmd --help" into "perf help cmd" */
320 if (argc > 1 && !strcmp(argv[1], "--help")) {
321 argv[1] = argv[0];
322 argv[0] = cmd = "help";
325 for (i = 0; i < ARRAY_SIZE(commands); i++) {
326 struct cmd_struct *p = commands+i;
327 if (strcmp(p->cmd, cmd))
328 continue;
329 exit(run_builtin(p, argc, argv));
333 static void execv_dashed_external(const char **argv)
335 struct strbuf cmd = STRBUF_INIT;
336 const char *tmp;
337 int status;
339 strbuf_addf(&cmd, "perf-%s", argv[0]);
342 * argv[0] must be the perf command, but the argv array
343 * belongs to the caller, and may be reused in
344 * subsequent loop iterations. Save argv[0] and
345 * restore it on error.
347 tmp = argv[0];
348 argv[0] = cmd.buf;
351 * if we fail because the command is not found, it is
352 * OK to return. Otherwise, we just pass along the status code.
354 status = run_command_v_opt(argv, 0);
355 if (status != -ERR_RUN_COMMAND_EXEC) {
356 if (IS_RUN_COMMAND_ERR(status))
357 die("unable to run '%s'", argv[0]);
358 exit(-status);
360 errno = ENOENT; /* as if we called execvp */
362 argv[0] = tmp;
364 strbuf_release(&cmd);
367 static int run_argv(int *argcp, const char ***argv)
369 int done_alias = 0;
371 while (1) {
372 /* See if it's an internal command */
373 handle_internal_command(*argcp, *argv);
375 /* .. then try the external ones */
376 execv_dashed_external(*argv);
378 /* It could be an alias -- this works around the insanity
379 * of overriding "perf log" with "perf show" by having
380 * alias.log = show
382 if (done_alias || !handle_alias(argcp, argv))
383 break;
384 done_alias = 1;
387 return done_alias;
390 /* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
391 static void get_debugfs_mntpt(void)
393 const char *path = debugfs_mount(NULL);
395 if (path)
396 strncpy(debugfs_mntpt, path, sizeof(debugfs_mntpt));
397 else
398 debugfs_mntpt[0] = '\0';
401 int main(int argc, const char **argv)
403 const char *cmd;
405 cmd = perf_extract_argv0_path(argv[0]);
406 if (!cmd)
407 cmd = "perf-help";
408 /* get debugfs mount point from /proc/mounts */
409 get_debugfs_mntpt();
411 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
413 * - cannot take flags in between the "perf" and the "xxxx".
414 * - cannot execute it externally (since it would just do
415 * the same thing over again)
417 * So we just directly call the internal command handler, and
418 * die if that one cannot handle it.
420 if (!prefixcmp(cmd, "perf-")) {
421 cmd += 5;
422 argv[0] = cmd;
423 handle_internal_command(argc, argv);
424 die("cannot handle %s internally", cmd);
427 /* Look for flags.. */
428 argv++;
429 argc--;
430 handle_options(&argv, &argc, NULL);
431 commit_pager_choice();
432 set_debugfs_path();
433 if (argc > 0) {
434 if (!prefixcmp(argv[0], "--"))
435 argv[0] += 2;
436 } else {
437 /* The user didn't specify a command; give them help */
438 printf("\n usage: %s\n\n", perf_usage_string);
439 list_common_cmds_help();
440 printf("\n %s\n\n", perf_more_info_string);
441 exit(1);
443 cmd = argv[0];
446 * We use PATH to find perf commands, but we prepend some higher
447 * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
448 * environment, and the $(perfexecdir) from the Makefile at build
449 * time.
451 setup_path();
453 while (1) {
454 static int done_help;
455 static int was_alias;
457 was_alias = run_argv(&argc, &argv);
458 if (errno != ENOENT)
459 break;
461 if (was_alias) {
462 fprintf(stderr, "Expansion of alias '%s' failed; "
463 "'%s' is not a perf-command\n",
464 cmd, argv[0]);
465 exit(1);
467 if (!done_help) {
468 cmd = argv[0] = help_unknown_cmd(cmd);
469 done_help = 1;
470 } else
471 break;
474 fprintf(stderr, "Failed to run command '%s': %s\n",
475 cmd, strerror(errno));
477 return 1;