Commit | Line | Data |
---|---|---|
bf9e1876 IM |
1 | /* |
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 | */ | |
07800601 | 9 | #include "builtin.h" |
bf9e1876 | 10 | |
148be2c1 IM |
11 | #include "util/exec_cmd.h" |
12 | #include "util/cache.h" | |
13 | #include "util/quote.h" | |
14 | #include "util/run-command.h" | |
5beeded1 | 15 | #include "util/parse-events.h" |
549104f2 | 16 | #include "util/debugfs.h" |
07800601 IM |
17 | |
18 | const char perf_usage_string[] = | |
cc13a591 | 19 | "perf [--version] [--help] COMMAND [ARGS]"; |
07800601 IM |
20 | |
21 | const char perf_more_info_string[] = | |
22 | "See 'perf help COMMAND' for more information on a specific command."; | |
23 | ||
5d06e691 | 24 | int use_browser = -1; |
07800601 | 25 | static int use_pager = -1; |
5d06e691 | 26 | |
98a4179c FW |
27 | struct cmd_struct { |
28 | const char *cmd; | |
29 | int (*fn)(int, const char **, const char *); | |
30 | int option; | |
31 | }; | |
32 | ||
33 | static struct cmd_struct commands[] = { | |
34 | { "buildid-cache", cmd_buildid_cache, 0 }, | |
35 | { "buildid-list", cmd_buildid_list, 0 }, | |
36 | { "diff", cmd_diff, 0 }, | |
37 | { "evlist", cmd_evlist, 0 }, | |
38 | { "help", cmd_help, 0 }, | |
39 | { "list", cmd_list, 0 }, | |
40 | { "record", cmd_record, 0 }, | |
41 | { "report", cmd_report, 0 }, | |
42 | { "bench", cmd_bench, 0 }, | |
43 | { "stat", cmd_stat, 0 }, | |
44 | { "timechart", cmd_timechart, 0 }, | |
45 | { "top", cmd_top, 0 }, | |
46 | { "annotate", cmd_annotate, 0 }, | |
47 | { "version", cmd_version, 0 }, | |
48 | { "script", cmd_script, 0 }, | |
49 | { "sched", cmd_sched, 0 }, | |
393be2e3 | 50 | #ifndef NO_LIBELF_SUPPORT |
98a4179c | 51 | { "probe", cmd_probe, 0 }, |
393be2e3 | 52 | #endif |
98a4179c FW |
53 | { "kmem", cmd_kmem, 0 }, |
54 | { "lock", cmd_lock, 0 }, | |
55 | { "kvm", cmd_kvm, 0 }, | |
56 | { "test", cmd_test, 0 }, | |
57 | { "inject", cmd_inject, 0 }, | |
58 | }; | |
59 | ||
07800601 IM |
60 | struct pager_config { |
61 | const char *cmd; | |
62 | int val; | |
63 | }; | |
64 | ||
65 | static int pager_command_config(const char *var, const char *value, void *data) | |
66 | { | |
67 | struct pager_config *c = data; | |
68 | if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd)) | |
69 | c->val = perf_config_bool(var, value); | |
70 | return 0; | |
71 | } | |
72 | ||
73 | /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */ | |
74 | int check_pager_config(const char *cmd) | |
75 | { | |
76 | struct pager_config c; | |
77 | c.cmd = cmd; | |
78 | c.val = -1; | |
79 | perf_config(pager_command_config, &c); | |
80 | return c.val; | |
81 | } | |
82 | ||
5d06e691 ACM |
83 | static int tui_command_config(const char *var, const char *value, void *data) |
84 | { | |
85 | struct pager_config *c = data; | |
86 | if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd)) | |
87 | c->val = perf_config_bool(var, value); | |
88 | return 0; | |
89 | } | |
90 | ||
91 | /* returns 0 for "no tui", 1 for "use tui", and -1 for "not specified" */ | |
92 | static int check_tui_config(const char *cmd) | |
93 | { | |
94 | struct pager_config c; | |
95 | c.cmd = cmd; | |
96 | c.val = -1; | |
97 | perf_config(tui_command_config, &c); | |
98 | return c.val; | |
99 | } | |
100 | ||
4c574159 TF |
101 | static void commit_pager_choice(void) |
102 | { | |
07800601 IM |
103 | switch (use_pager) { |
104 | case 0: | |
105 | setenv("PERF_PAGER", "cat", 1); | |
106 | break; | |
107 | case 1: | |
108 | /* setup_pager(); */ | |
109 | break; | |
110 | default: | |
111 | break; | |
112 | } | |
113 | } | |
114 | ||
4c574159 | 115 | static int handle_options(const char ***argv, int *argc, int *envchanged) |
07800601 IM |
116 | { |
117 | int handled = 0; | |
118 | ||
119 | while (*argc > 0) { | |
120 | const char *cmd = (*argv)[0]; | |
121 | if (cmd[0] != '-') | |
122 | break; | |
123 | ||
124 | /* | |
125 | * For legacy reasons, the "version" and "help" | |
126 | * commands can be written with "--" prepended | |
127 | * to make them look like flags. | |
128 | */ | |
129 | if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) | |
130 | break; | |
131 | ||
132 | /* | |
133 | * Check remaining flags. | |
134 | */ | |
cfed95a6 VL |
135 | if (!prefixcmp(cmd, CMD_EXEC_PATH)) { |
136 | cmd += strlen(CMD_EXEC_PATH); | |
07800601 IM |
137 | if (*cmd == '=') |
138 | perf_set_argv_exec_path(cmd + 1); | |
139 | else { | |
140 | puts(perf_exec_path()); | |
141 | exit(0); | |
142 | } | |
143 | } else if (!strcmp(cmd, "--html-path")) { | |
144 | puts(system_path(PERF_HTML_PATH)); | |
145 | exit(0); | |
146 | } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { | |
147 | use_pager = 1; | |
148 | } else if (!strcmp(cmd, "--no-pager")) { | |
149 | use_pager = 0; | |
150 | if (envchanged) | |
151 | *envchanged = 1; | |
152 | } else if (!strcmp(cmd, "--perf-dir")) { | |
153 | if (*argc < 2) { | |
4c574159 | 154 | fprintf(stderr, "No directory given for --perf-dir.\n"); |
07800601 IM |
155 | usage(perf_usage_string); |
156 | } | |
157 | setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1); | |
158 | if (envchanged) | |
159 | *envchanged = 1; | |
160 | (*argv)++; | |
161 | (*argc)--; | |
162 | handled++; | |
cfed95a6 VL |
163 | } else if (!prefixcmp(cmd, CMD_PERF_DIR)) { |
164 | setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1); | |
07800601 IM |
165 | if (envchanged) |
166 | *envchanged = 1; | |
167 | } else if (!strcmp(cmd, "--work-tree")) { | |
168 | if (*argc < 2) { | |
4c574159 | 169 | fprintf(stderr, "No directory given for --work-tree.\n"); |
07800601 IM |
170 | usage(perf_usage_string); |
171 | } | |
172 | setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1); | |
173 | if (envchanged) | |
174 | *envchanged = 1; | |
175 | (*argv)++; | |
176 | (*argc)--; | |
cfed95a6 VL |
177 | } else if (!prefixcmp(cmd, CMD_WORK_TREE)) { |
178 | setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1); | |
07800601 IM |
179 | if (envchanged) |
180 | *envchanged = 1; | |
5beeded1 JB |
181 | } else if (!strcmp(cmd, "--debugfs-dir")) { |
182 | if (*argc < 2) { | |
183 | fprintf(stderr, "No directory given for --debugfs-dir.\n"); | |
184 | usage(perf_usage_string); | |
185 | } | |
ebf294bf | 186 | debugfs_set_path((*argv)[1]); |
5beeded1 JB |
187 | if (envchanged) |
188 | *envchanged = 1; | |
189 | (*argv)++; | |
190 | (*argc)--; | |
cfed95a6 | 191 | } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) { |
ebf294bf ACM |
192 | debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR)); |
193 | fprintf(stderr, "dir: %s\n", debugfs_mountpoint); | |
5beeded1 JB |
194 | if (envchanged) |
195 | *envchanged = 1; | |
98a4179c FW |
196 | } else if (!strcmp(cmd, "--list-cmds")) { |
197 | unsigned int i; | |
198 | ||
199 | for (i = 0; i < ARRAY_SIZE(commands); i++) { | |
200 | struct cmd_struct *p = commands+i; | |
201 | printf("%s ", p->cmd); | |
202 | } | |
203 | exit(0); | |
07800601 IM |
204 | } else { |
205 | fprintf(stderr, "Unknown option: %s\n", cmd); | |
206 | usage(perf_usage_string); | |
207 | } | |
208 | ||
209 | (*argv)++; | |
210 | (*argc)--; | |
211 | handled++; | |
212 | } | |
213 | return handled; | |
214 | } | |
215 | ||
216 | static int handle_alias(int *argcp, const char ***argv) | |
217 | { | |
218 | int envchanged = 0, ret = 0, saved_errno = errno; | |
219 | int count, option_count; | |
4c574159 | 220 | const char **new_argv; |
07800601 IM |
221 | const char *alias_command; |
222 | char *alias_string; | |
07800601 IM |
223 | |
224 | alias_command = (*argv)[0]; | |
225 | alias_string = alias_lookup(alias_command); | |
226 | if (alias_string) { | |
227 | if (alias_string[0] == '!') { | |
228 | if (*argcp > 1) { | |
229 | struct strbuf buf; | |
230 | ||
231 | strbuf_init(&buf, PATH_MAX); | |
232 | strbuf_addstr(&buf, alias_string); | |
233 | sq_quote_argv(&buf, (*argv) + 1, PATH_MAX); | |
234 | free(alias_string); | |
235 | alias_string = buf.buf; | |
236 | } | |
237 | ret = system(alias_string + 1); | |
238 | if (ret >= 0 && WIFEXITED(ret) && | |
239 | WEXITSTATUS(ret) != 127) | |
240 | exit(WEXITSTATUS(ret)); | |
241 | die("Failed to run '%s' when expanding alias '%s'", | |
242 | alias_string + 1, alias_command); | |
243 | } | |
244 | count = split_cmdline(alias_string, &new_argv); | |
245 | if (count < 0) | |
246 | die("Bad alias.%s string", alias_command); | |
247 | option_count = handle_options(&new_argv, &count, &envchanged); | |
248 | if (envchanged) | |
249 | die("alias '%s' changes environment variables\n" | |
250 | "You can use '!perf' in the alias to do this.", | |
251 | alias_command); | |
252 | memmove(new_argv - option_count, new_argv, | |
253 | count * sizeof(char *)); | |
254 | new_argv -= option_count; | |
255 | ||
256 | if (count < 1) | |
257 | die("empty alias for %s", alias_command); | |
258 | ||
259 | if (!strcmp(alias_command, new_argv[0])) | |
260 | die("recursive alias: %s", alias_command); | |
261 | ||
4c574159 | 262 | new_argv = realloc(new_argv, sizeof(char *) * |
07800601 IM |
263 | (count + *argcp + 1)); |
264 | /* insert after command name */ | |
4c574159 TF |
265 | memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp); |
266 | new_argv[count + *argcp] = NULL; | |
07800601 IM |
267 | |
268 | *argv = new_argv; | |
269 | *argcp += count - 1; | |
270 | ||
271 | ret = 1; | |
272 | } | |
273 | ||
274 | errno = saved_errno; | |
275 | ||
276 | return ret; | |
277 | } | |
278 | ||
279 | const char perf_version_string[] = PERF_VERSION; | |
280 | ||
281 | #define RUN_SETUP (1<<0) | |
282 | #define USE_PAGER (1<<1) | |
283 | /* | |
284 | * require working tree to be present -- anything uses this needs | |
285 | * RUN_SETUP for reading from the configuration file. | |
286 | */ | |
287 | #define NEED_WORK_TREE (1<<2) | |
288 | ||
07800601 IM |
289 | static int run_builtin(struct cmd_struct *p, int argc, const char **argv) |
290 | { | |
291 | int status; | |
292 | struct stat st; | |
293 | const char *prefix; | |
294 | ||
295 | prefix = NULL; | |
296 | if (p->option & RUN_SETUP) | |
297 | prefix = NULL; /* setup_perf_directory(); */ | |
298 | ||
5d06e691 ACM |
299 | if (use_browser == -1) |
300 | use_browser = check_tui_config(p->cmd); | |
301 | ||
07800601 IM |
302 | if (use_pager == -1 && p->option & RUN_SETUP) |
303 | use_pager = check_pager_config(p->cmd); | |
304 | if (use_pager == -1 && p->option & USE_PAGER) | |
305 | use_pager = 1; | |
306 | commit_pager_choice(); | |
307 | ||
07800601 | 308 | status = p->fn(argc, argv, prefix); |
f3a1f0ea ACM |
309 | exit_browser(status); |
310 | ||
07800601 IM |
311 | if (status) |
312 | return status & 0xff; | |
313 | ||
314 | /* Somebody closed stdout? */ | |
315 | if (fstat(fileno(stdout), &st)) | |
316 | return 0; | |
317 | /* Ignore write errors for pipes and sockets.. */ | |
318 | if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) | |
319 | return 0; | |
320 | ||
321 | /* Check for ENOSPC and EIO errors.. */ | |
322 | if (fflush(stdout)) | |
323 | die("write failure on standard output: %s", strerror(errno)); | |
324 | if (ferror(stdout)) | |
325 | die("unknown write failure on standard output"); | |
326 | if (fclose(stdout)) | |
327 | die("close failed on standard output: %s", strerror(errno)); | |
328 | return 0; | |
329 | } | |
330 | ||
331 | static void handle_internal_command(int argc, const char **argv) | |
332 | { | |
333 | const char *cmd = argv[0]; | |
f37a291c | 334 | unsigned int i; |
07800601 IM |
335 | static const char ext[] = STRIP_EXTENSION; |
336 | ||
337 | if (sizeof(ext) > 1) { | |
338 | i = strlen(argv[0]) - strlen(ext); | |
339 | if (i > 0 && !strcmp(argv[0] + i, ext)) { | |
340 | char *argv0 = strdup(argv[0]); | |
341 | argv[0] = cmd = argv0; | |
342 | argv0[i] = '\0'; | |
343 | } | |
344 | } | |
345 | ||
346 | /* Turn "perf cmd --help" into "perf help cmd" */ | |
347 | if (argc > 1 && !strcmp(argv[1], "--help")) { | |
348 | argv[1] = argv[0]; | |
349 | argv[0] = cmd = "help"; | |
350 | } | |
351 | ||
352 | for (i = 0; i < ARRAY_SIZE(commands); i++) { | |
353 | struct cmd_struct *p = commands+i; | |
354 | if (strcmp(p->cmd, cmd)) | |
355 | continue; | |
356 | exit(run_builtin(p, argc, argv)); | |
357 | } | |
358 | } | |
359 | ||
360 | static void execv_dashed_external(const char **argv) | |
361 | { | |
362 | struct strbuf cmd = STRBUF_INIT; | |
363 | const char *tmp; | |
364 | int status; | |
365 | ||
366 | strbuf_addf(&cmd, "perf-%s", argv[0]); | |
367 | ||
368 | /* | |
369 | * argv[0] must be the perf command, but the argv array | |
370 | * belongs to the caller, and may be reused in | |
371 | * subsequent loop iterations. Save argv[0] and | |
372 | * restore it on error. | |
373 | */ | |
374 | tmp = argv[0]; | |
375 | argv[0] = cmd.buf; | |
376 | ||
377 | /* | |
378 | * if we fail because the command is not found, it is | |
379 | * OK to return. Otherwise, we just pass along the status code. | |
380 | */ | |
381 | status = run_command_v_opt(argv, 0); | |
382 | if (status != -ERR_RUN_COMMAND_EXEC) { | |
383 | if (IS_RUN_COMMAND_ERR(status)) | |
384 | die("unable to run '%s'", argv[0]); | |
385 | exit(-status); | |
386 | } | |
387 | errno = ENOENT; /* as if we called execvp */ | |
388 | ||
389 | argv[0] = tmp; | |
390 | ||
391 | strbuf_release(&cmd); | |
392 | } | |
393 | ||
394 | static int run_argv(int *argcp, const char ***argv) | |
395 | { | |
396 | int done_alias = 0; | |
397 | ||
398 | while (1) { | |
399 | /* See if it's an internal command */ | |
400 | handle_internal_command(*argcp, *argv); | |
401 | ||
402 | /* .. then try the external ones */ | |
403 | execv_dashed_external(*argv); | |
404 | ||
405 | /* It could be an alias -- this works around the insanity | |
406 | * of overriding "perf log" with "perf show" by having | |
407 | * alias.log = show | |
408 | */ | |
409 | if (done_alias || !handle_alias(argcp, argv)) | |
410 | break; | |
411 | done_alias = 1; | |
412 | } | |
413 | ||
414 | return done_alias; | |
415 | } | |
416 | ||
3af6e338 ACM |
417 | static void pthread__block_sigwinch(void) |
418 | { | |
419 | sigset_t set; | |
420 | ||
421 | sigemptyset(&set); | |
422 | sigaddset(&set, SIGWINCH); | |
423 | pthread_sigmask(SIG_BLOCK, &set, NULL); | |
424 | } | |
425 | ||
426 | void pthread__unblock_sigwinch(void) | |
427 | { | |
428 | sigset_t set; | |
429 | ||
430 | sigemptyset(&set); | |
431 | sigaddset(&set, SIGWINCH); | |
432 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
433 | } | |
434 | ||
07800601 IM |
435 | int main(int argc, const char **argv) |
436 | { | |
437 | const char *cmd; | |
438 | ||
439 | cmd = perf_extract_argv0_path(argv[0]); | |
440 | if (!cmd) | |
441 | cmd = "perf-help"; | |
5beeded1 | 442 | /* get debugfs mount point from /proc/mounts */ |
ebf294bf | 443 | debugfs_mount(NULL); |
07800601 IM |
444 | /* |
445 | * "perf-xxxx" is the same as "perf xxxx", but we obviously: | |
446 | * | |
447 | * - cannot take flags in between the "perf" and the "xxxx". | |
448 | * - cannot execute it externally (since it would just do | |
449 | * the same thing over again) | |
450 | * | |
451 | * So we just directly call the internal command handler, and | |
452 | * die if that one cannot handle it. | |
453 | */ | |
454 | if (!prefixcmp(cmd, "perf-")) { | |
266dfb0b | 455 | cmd += 5; |
07800601 IM |
456 | argv[0] = cmd; |
457 | handle_internal_command(argc, argv); | |
458 | die("cannot handle %s internally", cmd); | |
459 | } | |
460 | ||
461 | /* Look for flags.. */ | |
462 | argv++; | |
463 | argc--; | |
464 | handle_options(&argv, &argc, NULL); | |
465 | commit_pager_choice(); | |
45de34bb SE |
466 | set_buildid_dir(); |
467 | ||
07800601 IM |
468 | if (argc > 0) { |
469 | if (!prefixcmp(argv[0], "--")) | |
470 | argv[0] += 2; | |
471 | } else { | |
472 | /* The user didn't specify a command; give them help */ | |
502fc5c7 | 473 | printf("\n usage: %s\n\n", perf_usage_string); |
07800601 | 474 | list_common_cmds_help(); |
502fc5c7 | 475 | printf("\n %s\n\n", perf_more_info_string); |
07800601 IM |
476 | exit(1); |
477 | } | |
478 | cmd = argv[0]; | |
479 | ||
480 | /* | |
481 | * We use PATH to find perf commands, but we prepend some higher | |
659431fc | 482 | * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH |
07800601 IM |
483 | * environment, and the $(perfexecdir) from the Makefile at build |
484 | * time. | |
485 | */ | |
486 | setup_path(); | |
3af6e338 ACM |
487 | /* |
488 | * Block SIGWINCH notifications so that the thread that wants it can | |
489 | * unblock and get syscalls like select interrupted instead of waiting | |
490 | * forever while the signal goes to some other non interested thread. | |
491 | */ | |
492 | pthread__block_sigwinch(); | |
07800601 IM |
493 | |
494 | while (1) { | |
4c574159 TF |
495 | static int done_help; |
496 | static int was_alias; | |
8035e428 | 497 | |
07800601 IM |
498 | was_alias = run_argv(&argc, &argv); |
499 | if (errno != ENOENT) | |
500 | break; | |
8035e428 | 501 | |
07800601 IM |
502 | if (was_alias) { |
503 | fprintf(stderr, "Expansion of alias '%s' failed; " | |
504 | "'%s' is not a perf-command\n", | |
505 | cmd, argv[0]); | |
506 | exit(1); | |
507 | } | |
508 | if (!done_help) { | |
509 | cmd = argv[0] = help_unknown_cmd(cmd); | |
510 | done_help = 1; | |
511 | } else | |
512 | break; | |
513 | } | |
514 | ||
515 | fprintf(stderr, "Failed to run command '%s': %s\n", | |
516 | cmd, strerror(errno)); | |
517 | ||
518 | return 1; | |
519 | } |