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