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