1 // SPDX-License-Identifier: GPL-2.0
8 #include "util/config.h"
10 #include <subcmd/exec-cmd.h>
11 #include "common-cmds.h"
12 #include <subcmd/parse-options.h>
13 #include <subcmd/run-command.h>
14 #include <subcmd/help.h>
15 #include "util/debug.h"
16 #include <linux/kernel.h>
20 #include <sys/types.h>
24 static struct man_viewer_list {
25 struct man_viewer_list *next;
29 static struct man_viewer_info_list {
30 struct man_viewer_info_list *next;
33 } *man_viewer_info_list;
42 static enum help_format parse_help_format(const char *format)
44 if (!strcmp(format, "man"))
45 return HELP_FORMAT_MAN;
46 if (!strcmp(format, "info"))
47 return HELP_FORMAT_INFO;
48 if (!strcmp(format, "web") || !strcmp(format, "html"))
49 return HELP_FORMAT_WEB;
51 pr_err("unrecognized help format '%s'", format);
52 return HELP_FORMAT_NONE;
55 static const char *get_man_viewer_info(const char *name)
57 struct man_viewer_info_list *viewer;
59 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
60 if (!strcasecmp(name, viewer->name))
66 static int check_emacsclient_version(void)
68 struct strbuf buffer = STRBUF_INIT;
69 struct child_process ec_process;
70 const char *argv_ec[] = { "emacsclient", "--version", NULL };
74 /* emacsclient prints its version number on stderr */
75 memset(&ec_process, 0, sizeof(ec_process));
76 ec_process.argv = argv_ec;
78 ec_process.stdout_to_stderr = 1;
79 if (start_command(&ec_process)) {
80 fprintf(stderr, "Failed to start emacsclient.\n");
83 if (strbuf_read(&buffer, ec_process.err, 20) < 0) {
84 fprintf(stderr, "Failed to read emacsclient version\n");
87 close(ec_process.err);
90 * Don't bother checking return value, because "emacsclient --version"
91 * seems to always exits with code 1.
93 finish_command(&ec_process);
95 if (!strstarts(buffer.buf, "emacsclient")) {
96 fprintf(stderr, "Failed to parse emacsclient version.\n");
100 version = atoi(buffer.buf + strlen("emacsclient"));
104 "emacsclient version '%d' too old (< 22).\n",
109 strbuf_release(&buffer);
113 static void exec_failed(const char *cmd)
115 char sbuf[STRERR_BUFSIZE];
116 pr_warning("failed to exec '%s': %s", cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
119 static void exec_woman_emacs(const char *path, const char *page)
121 if (!check_emacsclient_version()) {
122 /* This works only with emacsclient version >= 22. */
126 path = "emacsclient";
127 if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
128 execlp(path, "emacsclient", "-e", man_page, NULL);
135 static void exec_man_konqueror(const char *path, const char *page)
137 const char *display = getenv("DISPLAY");
139 if (display && *display) {
141 const char *filename = "kfmclient";
143 /* It's simpler to launch konqueror using kfmclient. */
145 const char *file = strrchr(path, '/');
146 if (file && !strcmp(file + 1, "konqueror")) {
147 char *new = strdup(path);
148 char *dest = strrchr(new, '/');
150 /* strlen("konqueror") == strlen("kfmclient") */
151 strcpy(dest + 1, "kfmclient");
158 if (asprintf(&man_page, "man:%s(1)", page) > 0) {
159 execlp(path, filename, "newTab", man_page, NULL);
166 static void exec_man_man(const char *path, const char *page)
170 execlp(path, "man", page, NULL);
174 static void exec_man_cmd(const char *cmd, const char *page)
178 if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
179 execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
185 static void add_man_viewer(const char *name)
187 struct man_viewer_list **p = &man_viewer_list;
188 size_t len = strlen(name);
192 *p = zalloc(sizeof(**p) + len + 1);
193 strcpy((*p)->name, name);
196 static int supported_man_viewer(const char *name, size_t len)
198 return (!strncasecmp("man", name, len) ||
199 !strncasecmp("woman", name, len) ||
200 !strncasecmp("konqueror", name, len));
203 static void do_add_man_viewer_info(const char *name,
207 struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
209 strncpy(new->name, name, len);
210 new->info = strdup(value);
211 new->next = man_viewer_info_list;
212 man_viewer_info_list = new;
215 static void unsupported_man_viewer(const char *name, const char *var)
217 pr_warning("'%s': path for unsupported man viewer.\n"
218 "Please consider using 'man.<tool>.%s' instead.", name, var);
221 static int add_man_viewer_path(const char *name,
225 if (supported_man_viewer(name, len))
226 do_add_man_viewer_info(name, len, value);
228 unsupported_man_viewer(name, "cmd");
233 static int add_man_viewer_cmd(const char *name,
237 if (supported_man_viewer(name, len))
238 unsupported_man_viewer(name, "path");
240 do_add_man_viewer_info(name, len, value);
245 static int add_man_viewer_info(const char *var, const char *value)
247 const char *name = var + 4;
248 const char *subkey = strrchr(name, '.');
251 pr_err("Config with no key for man viewer: %s", name);
255 if (!strcmp(subkey, ".path")) {
257 return config_error_nonbool(var);
258 return add_man_viewer_path(name, subkey - name, value);
260 if (!strcmp(subkey, ".cmd")) {
262 return config_error_nonbool(var);
263 return add_man_viewer_cmd(name, subkey - name, value);
266 pr_warning("'%s': unsupported man viewer sub key.", subkey);
270 static int perf_help_config(const char *var, const char *value, void *cb)
272 enum help_format *help_formatp = cb;
274 if (!strcmp(var, "help.format")) {
276 return config_error_nonbool(var);
277 *help_formatp = parse_help_format(value);
278 if (*help_formatp == HELP_FORMAT_NONE)
282 if (!strcmp(var, "man.viewer")) {
284 return config_error_nonbool(var);
285 add_man_viewer(value);
288 if (strstarts(var, "man."))
289 return add_man_viewer_info(var, value);
294 static struct cmdnames main_cmds, other_cmds;
296 void list_common_cmds_help(void)
298 unsigned int i, longest = 0;
300 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
301 if (longest < strlen(common_cmds[i].name))
302 longest = strlen(common_cmds[i].name);
305 puts(" The most commonly used perf commands are:");
306 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
307 printf(" %-*s ", longest, common_cmds[i].name);
308 puts(common_cmds[i].help);
312 static const char *cmd_to_page(const char *perf_cmd)
318 else if (strstarts(perf_cmd, "perf"))
321 return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
324 static void setup_man_path(void)
327 const char *old_path = getenv("MANPATH");
329 /* We should always put ':' after our path. If there is no
330 * old_path, the ':' at the end will let 'man' to try
331 * system-wide paths after ours to find the manual page. If
332 * there is old_path, we need ':' as delimiter. */
333 if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
334 setenv("MANPATH", new_path, 1);
337 pr_err("Unable to setup man path");
341 static void exec_viewer(const char *name, const char *page)
343 const char *info = get_man_viewer_info(name);
345 if (!strcasecmp(name, "man"))
346 exec_man_man(info, page);
347 else if (!strcasecmp(name, "woman"))
348 exec_woman_emacs(info, page);
349 else if (!strcasecmp(name, "konqueror"))
350 exec_man_konqueror(info, page);
352 exec_man_cmd(info, page);
354 pr_warning("'%s': unknown man viewer.", name);
357 static int show_man_page(const char *perf_cmd)
359 struct man_viewer_list *viewer;
360 const char *page = cmd_to_page(perf_cmd);
361 const char *fallback = getenv("PERF_MAN_VIEWER");
364 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
365 exec_viewer(viewer->name, page); /* will return when unable */
368 exec_viewer(fallback, page);
369 exec_viewer("man", page);
371 pr_err("no man viewer handled the request");
375 static int show_info_page(const char *perf_cmd)
377 const char *page = cmd_to_page(perf_cmd);
378 setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
379 execlp("info", "info", "perfman", page, NULL);
383 static int get_html_page_path(char **page_path, const char *page)
386 const char *html_path = system_path(PERF_HTML_PATH);
388 /* Check that we have a perf documentation directory. */
389 if (stat(mkpath("%s/perf.html", html_path), &st)
390 || !S_ISREG(st.st_mode)) {
391 pr_err("'%s': not a documentation directory.", html_path);
395 return asprintf(page_path, "%s/%s.html", html_path, page);
399 * If open_html is not defined in a platform-specific way (see for
400 * example compat/mingw.h), we use the script web--browse to display
404 static void open_html(const char *path)
406 execl_cmd("web--browse", "-c", "help.browser", path, NULL);
410 static int show_html_page(const char *perf_cmd)
412 const char *page = cmd_to_page(perf_cmd);
413 char *page_path; /* it leaks but we exec bellow */
415 if (get_html_page_path(&page_path, page) < 0)
418 open_html(page_path);
423 int cmd_help(int argc, const char **argv)
425 bool show_all = false;
426 enum help_format help_format = HELP_FORMAT_MAN;
427 struct option builtin_help_options[] = {
428 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
429 OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
430 OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
432 OPT_SET_UINT('i', "info", &help_format, "show info page",
436 const char * const builtin_help_subcommands[] = {
437 "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
438 "record", "report", "bench", "stat", "timechart", "top", "annotate",
439 "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
440 #ifdef HAVE_LIBELF_SUPPORT
443 #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
447 const char *builtin_help_usage[] = {
448 "perf help [--all] [--man|--web|--info] [command]",
453 load_command_list("perf-", &main_cmds, &other_cmds);
455 rc = perf_config(perf_help_config, &help_format);
459 argc = parse_options_subcommand(argc, argv, builtin_help_options,
460 builtin_help_subcommands, builtin_help_usage, 0);
463 printf("\n Usage: %s\n\n", perf_usage_string);
464 list_commands("perf commands", &main_cmds, &other_cmds);
465 printf(" %s\n\n", perf_more_info_string);
470 printf("\n usage: %s\n\n", perf_usage_string);
471 list_common_cmds_help();
472 printf("\n %s\n\n", perf_more_info_string);
476 switch (help_format) {
477 case HELP_FORMAT_MAN:
478 rc = show_man_page(argv[0]);
480 case HELP_FORMAT_INFO:
481 rc = show_info_page(argv[0]);
483 case HELP_FORMAT_WEB:
484 rc = show_html_page(argv[0]);
486 case HELP_FORMAT_NONE: