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