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