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