Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * builtin-help.c |
| 3 | * |
| 4 | * Builtin help command |
| 5 | */ |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 6 | #include "perf.h" |
Taeung Song | 41840d2 | 2016-06-23 17:55:17 +0900 | [diff] [blame] | 7 | #include "util/config.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 8 | #include "builtin.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 9 | #include <subcmd/exec-cmd.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 10 | #include "common-cmds.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 11 | #include <subcmd/parse-options.h> |
| 12 | #include <subcmd/run-command.h> |
| 13 | #include <subcmd/help.h> |
Jiri Olsa | 84f5d36 | 2014-07-14 23:46:48 +0200 | [diff] [blame] | 14 | #include "util/debug.h" |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 16 | #include <errno.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <unistd.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 21 | |
| 22 | static struct man_viewer_list { |
| 23 | struct man_viewer_list *next; |
Arnaldo Carvalho de Melo | 7909675 | 2017-04-17 11:08:20 -0300 | [diff] [blame] | 24 | char name[0]; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 25 | } *man_viewer_list; |
| 26 | |
| 27 | static struct man_viewer_info_list { |
| 28 | struct man_viewer_info_list *next; |
| 29 | const char *info; |
Arnaldo Carvalho de Melo | 7909675 | 2017-04-17 11:08:20 -0300 | [diff] [blame] | 30 | char name[0]; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 31 | } *man_viewer_info_list; |
| 32 | |
| 33 | enum help_format { |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 34 | HELP_FORMAT_NONE, |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 35 | HELP_FORMAT_MAN, |
| 36 | HELP_FORMAT_INFO, |
| 37 | HELP_FORMAT_WEB, |
| 38 | }; |
| 39 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 40 | static enum help_format parse_help_format(const char *format) |
| 41 | { |
| 42 | if (!strcmp(format, "man")) |
| 43 | return HELP_FORMAT_MAN; |
| 44 | if (!strcmp(format, "info")) |
| 45 | return HELP_FORMAT_INFO; |
| 46 | if (!strcmp(format, "web") || !strcmp(format, "html")) |
| 47 | return HELP_FORMAT_WEB; |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 48 | |
| 49 | pr_err("unrecognized help format '%s'", format); |
| 50 | return HELP_FORMAT_NONE; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static const char *get_man_viewer_info(const char *name) |
| 54 | { |
| 55 | struct man_viewer_info_list *viewer; |
| 56 | |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 57 | for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 58 | if (!strcasecmp(name, viewer->name)) |
| 59 | return viewer->info; |
| 60 | } |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | static int check_emacsclient_version(void) |
| 65 | { |
| 66 | struct strbuf buffer = STRBUF_INIT; |
| 67 | struct child_process ec_process; |
| 68 | const char *argv_ec[] = { "emacsclient", "--version", NULL }; |
| 69 | int version; |
Masami Hiramatsu | b72ca40 | 2016-05-10 14:47:17 +0900 | [diff] [blame] | 70 | int ret = -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 71 | |
| 72 | /* emacsclient prints its version number on stderr */ |
| 73 | memset(&ec_process, 0, sizeof(ec_process)); |
| 74 | ec_process.argv = argv_ec; |
| 75 | ec_process.err = -1; |
| 76 | ec_process.stdout_to_stderr = 1; |
| 77 | if (start_command(&ec_process)) { |
| 78 | fprintf(stderr, "Failed to start emacsclient.\n"); |
| 79 | return -1; |
| 80 | } |
Masami Hiramatsu | b72ca40 | 2016-05-10 14:47:17 +0900 | [diff] [blame] | 81 | if (strbuf_read(&buffer, ec_process.err, 20) < 0) { |
| 82 | fprintf(stderr, "Failed to read emacsclient version\n"); |
| 83 | goto out; |
| 84 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 85 | close(ec_process.err); |
| 86 | |
| 87 | /* |
| 88 | * Don't bother checking return value, because "emacsclient --version" |
| 89 | * seems to always exits with code 1. |
| 90 | */ |
| 91 | finish_command(&ec_process); |
| 92 | |
| 93 | if (prefixcmp(buffer.buf, "emacsclient")) { |
| 94 | fprintf(stderr, "Failed to parse emacsclient version.\n"); |
Masami Hiramatsu | b72ca40 | 2016-05-10 14:47:17 +0900 | [diff] [blame] | 95 | goto out; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Arnaldo Carvalho de Melo | d1130686 | 2016-02-23 16:18:37 -0300 | [diff] [blame] | 98 | version = atoi(buffer.buf + strlen("emacsclient")); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 99 | |
| 100 | if (version < 22) { |
| 101 | fprintf(stderr, |
| 102 | "emacsclient version '%d' too old (< 22).\n", |
| 103 | version); |
Masami Hiramatsu | b72ca40 | 2016-05-10 14:47:17 +0900 | [diff] [blame] | 104 | } else |
| 105 | ret = 0; |
| 106 | out: |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 107 | strbuf_release(&buffer); |
Masami Hiramatsu | b72ca40 | 2016-05-10 14:47:17 +0900 | [diff] [blame] | 108 | return ret; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 111 | static void exec_woman_emacs(const char *path, const char *page) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 112 | { |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 113 | char sbuf[STRERR_BUFSIZE]; |
| 114 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 115 | if (!check_emacsclient_version()) { |
| 116 | /* This works only with emacsclient version >= 22. */ |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 117 | char *man_page; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 118 | |
| 119 | if (!path) |
| 120 | path = "emacsclient"; |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 121 | if (asprintf(&man_page, "(woman \"%s\")", page) > 0) { |
| 122 | execlp(path, "emacsclient", "-e", man_page, NULL); |
| 123 | free(man_page); |
| 124 | } |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 125 | warning("failed to exec '%s': %s", path, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 126 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 130 | static void exec_man_konqueror(const char *path, const char *page) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 131 | { |
| 132 | const char *display = getenv("DISPLAY"); |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 133 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 134 | if (display && *display) { |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 135 | char *man_page; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 136 | const char *filename = "kfmclient"; |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 137 | char sbuf[STRERR_BUFSIZE]; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 138 | |
| 139 | /* It's simpler to launch konqueror using kfmclient. */ |
| 140 | if (path) { |
| 141 | const char *file = strrchr(path, '/'); |
| 142 | if (file && !strcmp(file + 1, "konqueror")) { |
| 143 | char *new = strdup(path); |
| 144 | char *dest = strrchr(new, '/'); |
| 145 | |
| 146 | /* strlen("konqueror") == strlen("kfmclient") */ |
| 147 | strcpy(dest + 1, "kfmclient"); |
| 148 | path = new; |
| 149 | } |
| 150 | if (file) |
| 151 | filename = file; |
| 152 | } else |
| 153 | path = "kfmclient"; |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 154 | if (asprintf(&man_page, "man:%s(1)", page) > 0) { |
| 155 | execlp(path, filename, "newTab", man_page, NULL); |
| 156 | free(man_page); |
| 157 | } |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 158 | warning("failed to exec '%s': %s", path, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 159 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 163 | static void exec_man_man(const char *path, const char *page) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 164 | { |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 165 | char sbuf[STRERR_BUFSIZE]; |
| 166 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 167 | if (!path) |
| 168 | path = "man"; |
| 169 | execlp(path, "man", page, NULL); |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 170 | warning("failed to exec '%s': %s", path, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 171 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static void exec_man_cmd(const char *cmd, const char *page) |
| 175 | { |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 176 | char sbuf[STRERR_BUFSIZE]; |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 177 | char *shell_cmd; |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 178 | |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 179 | if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) { |
| 180 | execl("/bin/sh", "sh", "-c", shell_cmd, NULL); |
| 181 | free(shell_cmd); |
| 182 | } |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 183 | warning("failed to exec '%s': %s", cmd, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 184 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void add_man_viewer(const char *name) |
| 188 | { |
| 189 | struct man_viewer_list **p = &man_viewer_list; |
| 190 | size_t len = strlen(name); |
| 191 | |
| 192 | while (*p) |
| 193 | p = &((*p)->next); |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 194 | *p = zalloc(sizeof(**p) + len + 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 195 | strncpy((*p)->name, name, len); |
| 196 | } |
| 197 | |
| 198 | static int supported_man_viewer(const char *name, size_t len) |
| 199 | { |
| 200 | return (!strncasecmp("man", name, len) || |
| 201 | !strncasecmp("woman", name, len) || |
| 202 | !strncasecmp("konqueror", name, len)); |
| 203 | } |
| 204 | |
| 205 | static void do_add_man_viewer_info(const char *name, |
| 206 | size_t len, |
| 207 | const char *value) |
| 208 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 209 | struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 210 | |
| 211 | strncpy(new->name, name, len); |
| 212 | new->info = strdup(value); |
| 213 | new->next = man_viewer_info_list; |
| 214 | man_viewer_info_list = new; |
| 215 | } |
| 216 | |
| 217 | static int add_man_viewer_path(const char *name, |
| 218 | size_t len, |
| 219 | const char *value) |
| 220 | { |
| 221 | if (supported_man_viewer(name, len)) |
| 222 | do_add_man_viewer_info(name, len, value); |
| 223 | else |
| 224 | warning("'%s': path for unsupported man viewer.\n" |
| 225 | "Please consider using 'man.<tool>.cmd' instead.", |
| 226 | name); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int add_man_viewer_cmd(const char *name, |
| 232 | size_t len, |
| 233 | const char *value) |
| 234 | { |
| 235 | if (supported_man_viewer(name, len)) |
| 236 | warning("'%s': cmd for supported man viewer.\n" |
| 237 | "Please consider using 'man.<tool>.path' instead.", |
| 238 | name); |
| 239 | else |
| 240 | do_add_man_viewer_info(name, len, value); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int add_man_viewer_info(const char *var, const char *value) |
| 246 | { |
| 247 | const char *name = var + 4; |
| 248 | const char *subkey = strrchr(name, '.'); |
| 249 | |
| 250 | if (!subkey) |
| 251 | return error("Config with no key for man viewer: %s", name); |
| 252 | |
| 253 | if (!strcmp(subkey, ".path")) { |
| 254 | if (!value) |
| 255 | return config_error_nonbool(var); |
| 256 | return add_man_viewer_path(name, subkey - name, value); |
| 257 | } |
| 258 | if (!strcmp(subkey, ".cmd")) { |
| 259 | if (!value) |
| 260 | return config_error_nonbool(var); |
| 261 | return add_man_viewer_cmd(name, subkey - name, value); |
| 262 | } |
| 263 | |
| 264 | warning("'%s': unsupported man viewer sub key.", subkey); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int perf_help_config(const char *var, const char *value, void *cb) |
| 269 | { |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 270 | enum help_format *help_formatp = cb; |
| 271 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 272 | if (!strcmp(var, "help.format")) { |
| 273 | if (!value) |
| 274 | return config_error_nonbool(var); |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 275 | *help_formatp = parse_help_format(value); |
| 276 | if (*help_formatp == HELP_FORMAT_NONE) |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 277 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | if (!strcmp(var, "man.viewer")) { |
| 281 | if (!value) |
| 282 | return config_error_nonbool(var); |
| 283 | add_man_viewer(value); |
| 284 | return 0; |
| 285 | } |
| 286 | if (!prefixcmp(var, "man.")) |
| 287 | return add_man_viewer_info(var, value); |
| 288 | |
Wang Nan | b8cbb34 | 2016-02-26 09:31:51 +0000 | [diff] [blame] | 289 | return 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static struct cmdnames main_cmds, other_cmds; |
| 293 | |
| 294 | void list_common_cmds_help(void) |
| 295 | { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 296 | unsigned int i, longest = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 297 | |
| 298 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { |
| 299 | if (longest < strlen(common_cmds[i].name)) |
| 300 | longest = strlen(common_cmds[i].name); |
| 301 | } |
| 302 | |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 303 | puts(" The most commonly used perf commands are:"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 304 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { |
Amerigo Wang | 3912f2a | 2009-12-14 03:23:56 -0500 | [diff] [blame] | 305 | printf(" %-*s ", longest, common_cmds[i].name); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 306 | puts(common_cmds[i].help); |
| 307 | } |
| 308 | } |
| 309 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 310 | static const char *cmd_to_page(const char *perf_cmd) |
| 311 | { |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 312 | char *s; |
| 313 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 314 | if (!perf_cmd) |
| 315 | return "perf"; |
| 316 | else if (!prefixcmp(perf_cmd, "perf")) |
| 317 | return perf_cmd; |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 318 | |
| 319 | return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void setup_man_path(void) |
| 323 | { |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 324 | char *new_path; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 325 | const char *old_path = getenv("MANPATH"); |
| 326 | |
| 327 | /* We should always put ':' after our path. If there is no |
| 328 | * old_path, the ':' at the end will let 'man' to try |
| 329 | * system-wide paths after ours to find the manual page. If |
| 330 | * there is old_path, we need ':' as delimiter. */ |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 331 | if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) { |
| 332 | setenv("MANPATH", new_path, 1); |
| 333 | free(new_path); |
| 334 | } else { |
| 335 | error("Unable to setup man path"); |
| 336 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static void exec_viewer(const char *name, const char *page) |
| 340 | { |
| 341 | const char *info = get_man_viewer_info(name); |
| 342 | |
| 343 | if (!strcasecmp(name, "man")) |
| 344 | exec_man_man(info, page); |
| 345 | else if (!strcasecmp(name, "woman")) |
| 346 | exec_woman_emacs(info, page); |
| 347 | else if (!strcasecmp(name, "konqueror")) |
| 348 | exec_man_konqueror(info, page); |
| 349 | else if (info) |
| 350 | exec_man_cmd(info, page); |
| 351 | else |
| 352 | warning("'%s': unknown man viewer.", name); |
| 353 | } |
| 354 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 355 | static int show_man_page(const char *perf_cmd) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 356 | { |
| 357 | struct man_viewer_list *viewer; |
| 358 | const char *page = cmd_to_page(perf_cmd); |
| 359 | const char *fallback = getenv("PERF_MAN_VIEWER"); |
| 360 | |
| 361 | setup_man_path(); |
| 362 | for (viewer = man_viewer_list; viewer; viewer = viewer->next) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 363 | exec_viewer(viewer->name, page); /* will return when unable */ |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 364 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 365 | if (fallback) |
| 366 | exec_viewer(fallback, page); |
| 367 | exec_viewer("man", page); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 368 | |
| 369 | pr_err("no man viewer handled the request"); |
| 370 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 371 | } |
| 372 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 373 | static int show_info_page(const char *perf_cmd) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 374 | { |
| 375 | const char *page = cmd_to_page(perf_cmd); |
| 376 | setenv("INFOPATH", system_path(PERF_INFO_PATH), 1); |
| 377 | execlp("info", "info", "perfman", page, NULL); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 378 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 381 | static int get_html_page_path(char **page_path, const char *page) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 382 | { |
| 383 | struct stat st; |
| 384 | const char *html_path = system_path(PERF_HTML_PATH); |
| 385 | |
| 386 | /* Check that we have a perf documentation directory. */ |
| 387 | if (stat(mkpath("%s/perf.html", html_path), &st) |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 388 | || !S_ISREG(st.st_mode)) { |
| 389 | pr_err("'%s': not a documentation directory.", html_path); |
| 390 | return -1; |
| 391 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 392 | |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 393 | return asprintf(page_path, "%s/%s.html", html_path, page); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* |
| 397 | * If open_html is not defined in a platform-specific way (see for |
| 398 | * example compat/mingw.h), we use the script web--browse to display |
| 399 | * HTML. |
| 400 | */ |
| 401 | #ifndef open_html |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 402 | static void open_html(const char *path) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 403 | { |
Josh Poimboeuf | 46113a5 | 2015-12-15 09:39:37 -0600 | [diff] [blame] | 404 | execl_cmd("web--browse", "-c", "help.browser", path, NULL); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 405 | } |
| 406 | #endif |
| 407 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 408 | static int show_html_page(const char *perf_cmd) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 409 | { |
| 410 | const char *page = cmd_to_page(perf_cmd); |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 411 | char *page_path; /* it leaks but we exec bellow */ |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 412 | |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 413 | if (get_html_page_path(&page_path, page) < 0) |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 414 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 415 | |
Arnaldo Carvalho de Melo | a610f5c | 2016-03-23 15:44:09 -0300 | [diff] [blame] | 416 | open_html(page_path); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 417 | |
| 418 | return 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 421 | int cmd_help(int argc, const char **argv) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 422 | { |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 423 | bool show_all = false; |
Namhyung Kim | 670ab5d | 2012-10-22 16:12:23 +0900 | [diff] [blame] | 424 | enum help_format help_format = HELP_FORMAT_MAN; |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 425 | struct option builtin_help_options[] = { |
| 426 | OPT_BOOLEAN('a', "all", &show_all, "print all available commands"), |
| 427 | OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN), |
| 428 | OPT_SET_UINT('w', "web", &help_format, "show manual in web browser", |
| 429 | HELP_FORMAT_WEB), |
| 430 | OPT_SET_UINT('i', "info", &help_format, "show info page", |
| 431 | HELP_FORMAT_INFO), |
| 432 | OPT_END(), |
| 433 | }; |
Yunlong Song | e24a110 | 2015-03-18 21:35:53 +0800 | [diff] [blame] | 434 | const char * const builtin_help_subcommands[] = { |
| 435 | "buildid-cache", "buildid-list", "diff", "evlist", "help", "list", |
| 436 | "record", "report", "bench", "stat", "timechart", "top", "annotate", |
Arnaldo Carvalho de Melo | 3556377 | 2017-01-05 15:33:32 -0300 | [diff] [blame] | 437 | "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data", |
Yunlong Song | e24a110 | 2015-03-18 21:35:53 +0800 | [diff] [blame] | 438 | #ifdef HAVE_LIBELF_SUPPORT |
| 439 | "probe", |
| 440 | #endif |
| 441 | #ifdef HAVE_LIBAUDIT_SUPPORT |
| 442 | "trace", |
| 443 | #endif |
| 444 | NULL }; |
| 445 | const char *builtin_help_usage[] = { |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 446 | "perf help [--all] [--man|--web|--info] [command]", |
| 447 | NULL |
| 448 | }; |
Arnaldo Carvalho de Melo | ecc4c56 | 2017-01-24 13:44:10 -0300 | [diff] [blame] | 449 | int rc; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 450 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 451 | load_command_list("perf-", &main_cmds, &other_cmds); |
| 452 | |
Arnaldo Carvalho de Melo | ecc4c56 | 2017-01-24 13:44:10 -0300 | [diff] [blame] | 453 | rc = perf_config(perf_help_config, &help_format); |
| 454 | if (rc) |
| 455 | return rc; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 456 | |
Yunlong Song | e24a110 | 2015-03-18 21:35:53 +0800 | [diff] [blame] | 457 | argc = parse_options_subcommand(argc, argv, builtin_help_options, |
| 458 | builtin_help_subcommands, builtin_help_usage, 0); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 459 | |
| 460 | if (show_all) { |
Yunlong Song | 3a134ae | 2015-10-15 15:39:51 +0800 | [diff] [blame] | 461 | printf("\n Usage: %s\n\n", perf_usage_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 462 | list_commands("perf commands", &main_cmds, &other_cmds); |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 463 | printf(" %s\n\n", perf_more_info_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | if (!argv[0]) { |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 468 | printf("\n usage: %s\n\n", perf_usage_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 469 | list_common_cmds_help(); |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 470 | printf("\n %s\n\n", perf_more_info_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 474 | switch (help_format) { |
| 475 | case HELP_FORMAT_MAN: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 476 | rc = show_man_page(argv[0]); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 477 | break; |
| 478 | case HELP_FORMAT_INFO: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 479 | rc = show_info_page(argv[0]); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 480 | break; |
| 481 | case HELP_FORMAT_WEB: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 482 | rc = show_html_page(argv[0]); |
| 483 | break; |
| 484 | case HELP_FORMAT_NONE: |
| 485 | /* fall-through */ |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 486 | default: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 487 | rc = -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 488 | break; |
| 489 | } |
| 490 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 491 | return rc; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 492 | } |