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