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>
17 #include <linux/zalloc.h>
21 #include <sys/types.h>
25 static struct man_viewer_list {
26 struct man_viewer_list *next;
30 static struct man_viewer_info_list {
31 struct man_viewer_info_list *next;
34 } *man_viewer_info_list;
43 static enum help_format parse_help_format(const char *format)
45 if (!strcmp(format, "man"))
46 return HELP_FORMAT_MAN;
47 if (!strcmp(format, "info"))
48 return HELP_FORMAT_INFO;
49 if (!strcmp(format, "web") || !strcmp(format, "html"))
50 return HELP_FORMAT_WEB;
52 pr_err("unrecognized help format '%s'", format);
53 return HELP_FORMAT_NONE;
56 static const char *get_man_viewer_info(const char *name)
58 struct man_viewer_info_list *viewer;
60 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
61 if (!strcasecmp(name, viewer->name))
67 static int check_emacsclient_version(void)
69 struct strbuf buffer = STRBUF_INIT;
70 struct child_process ec_process;
71 const char *argv_ec[] = { "emacsclient", "--version", NULL };
75 /* emacsclient prints its version number on stderr */
76 memset(&ec_process, 0, sizeof(ec_process));
77 ec_process.argv = argv_ec;
79 ec_process.stdout_to_stderr = 1;
80 if (start_command(&ec_process)) {
81 fprintf(stderr, "Failed to start emacsclient.\n");
84 if (strbuf_read(&buffer, ec_process.err, 20) < 0) {
85 fprintf(stderr, "Failed to read emacsclient version\n");
88 close(ec_process.err);
91 * Don't bother checking return value, because "emacsclient --version"
92 * seems to always exits with code 1.
94 finish_command(&ec_process);
96 if (!strstarts(buffer.buf, "emacsclient")) {
97 fprintf(stderr, "Failed to parse emacsclient version.\n");
101 version = atoi(buffer.buf + strlen("emacsclient"));
105 "emacsclient version '%d' too old (< 22).\n",
110 strbuf_release(&buffer);
114 static void exec_failed(const char *cmd)
116 char sbuf[STRERR_BUFSIZE];
117 pr_warning("failed to exec '%s': %s", cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
120 static void exec_woman_emacs(const char *path, const char *page)
122 if (!check_emacsclient_version()) {
123 /* This works only with emacsclient version >= 22. */
127 path = "emacsclient";
128 if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
129 execlp(path, "emacsclient", "-e", man_page, NULL);
136 static void exec_man_konqueror(const char *path, const char *page)
138 const char *display = getenv("DISPLAY");
140 if (display && *display) {
142 const char *filename = "kfmclient";
144 /* It's simpler to launch konqueror using kfmclient. */
146 const char *file = strrchr(path, '/');
147 if (file && !strcmp(file + 1, "konqueror")) {
148 char *new = strdup(path);
149 char *dest = strrchr(new, '/');
151 /* strlen("konqueror") == strlen("kfmclient") */
152 strcpy(dest + 1, "kfmclient");
159 if (asprintf(&man_page, "man:%s(1)", page) > 0) {
160 execlp(path, filename, "newTab", man_page, NULL);
167 static void exec_man_man(const char *path, const char *page)
171 execlp(path, "man", page, NULL);
175 static void exec_man_cmd(const char *cmd, const char *page)
179 if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
180 execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
186 static void add_man_viewer(const char *name)
188 struct man_viewer_list **p = &man_viewer_list;
189 size_t len = strlen(name);
193 *p = zalloc(sizeof(**p) + len + 1);
194 strcpy((*p)->name, name);
197 static int supported_man_viewer(const char *name, size_t len)
199 return (!strncasecmp("man", name, len) ||
200 !strncasecmp("woman", name, len) ||
201 !strncasecmp("konqueror", name, len));
204 static void do_add_man_viewer_info(const char *name,
208 struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
210 strncpy(new->name, name, len);
211 new->info = strdup(value);
212 new->next = man_viewer_info_list;
213 man_viewer_info_list = new;
216 static void unsupported_man_viewer(const char *name, const char *var)
218 pr_warning("'%s': path for unsupported man viewer.\n"
219 "Please consider using 'man.<tool>.%s' instead.", name, var);
222 static int add_man_viewer_path(const char *name,
226 if (supported_man_viewer(name, len))
227 do_add_man_viewer_info(name, len, value);
229 unsupported_man_viewer(name, "cmd");
234 static int add_man_viewer_cmd(const char *name,
238 if (supported_man_viewer(name, len))
239 unsupported_man_viewer(name, "path");
241 do_add_man_viewer_info(name, len, value);
246 static int add_man_viewer_info(const char *var, const char *value)
248 const char *name = var + 4;
249 const char *subkey = strrchr(name, '.');
252 pr_err("Config with no key for man viewer: %s", name);
256 if (!strcmp(subkey, ".path")) {
258 return config_error_nonbool(var);
259 return add_man_viewer_path(name, subkey - name, value);
261 if (!strcmp(subkey, ".cmd")) {
263 return config_error_nonbool(var);
264 return add_man_viewer_cmd(name, subkey - name, value);
267 pr_warning("'%s': unsupported man viewer sub key.", subkey);
271 static int perf_help_config(const char *var, const char *value, void *cb)
273 enum help_format *help_formatp = cb;
275 if (!strcmp(var, "help.format")) {
277 return config_error_nonbool(var);
278 *help_formatp = parse_help_format(value);
279 if (*help_formatp == HELP_FORMAT_NONE)
283 if (!strcmp(var, "man.viewer")) {
285 return config_error_nonbool(var);
286 add_man_viewer(value);
289 if (strstarts(var, "man."))
290 return add_man_viewer_info(var, value);
295 static struct cmdnames main_cmds, other_cmds;
297 void list_common_cmds_help(void)
299 unsigned int i, longest = 0;
301 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
302 if (longest < strlen(common_cmds[i].name))
303 longest = strlen(common_cmds[i].name);
306 puts(" The most commonly used perf commands are:");
307 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
308 printf(" %-*s ", longest, common_cmds[i].name);
309 puts(common_cmds[i].help);
313 static const char *cmd_to_page(const char *perf_cmd)
319 else if (strstarts(perf_cmd, "perf"))
322 return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
325 static void setup_man_path(void)
328 const char *old_path = getenv("MANPATH");
330 /* We should always put ':' after our path. If there is no
331 * old_path, the ':' at the end will let 'man' to try
332 * system-wide paths after ours to find the manual page. If
333 * there is old_path, we need ':' as delimiter. */
334 if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
335 setenv("MANPATH", new_path, 1);
338 pr_err("Unable to setup man path");
342 static void exec_viewer(const char *name, const char *page)
344 const char *info = get_man_viewer_info(name);
346 if (!strcasecmp(name, "man"))
347 exec_man_man(info, page);
348 else if (!strcasecmp(name, "woman"))
349 exec_woman_emacs(info, page);
350 else if (!strcasecmp(name, "konqueror"))
351 exec_man_konqueror(info, page);
353 exec_man_cmd(info, page);
355 pr_warning("'%s': unknown man viewer.", name);
358 static int show_man_page(const char *perf_cmd)
360 struct man_viewer_list *viewer;
361 const char *page = cmd_to_page(perf_cmd);
362 const char *fallback = getenv("PERF_MAN_VIEWER");
365 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
366 exec_viewer(viewer->name, page); /* will return when unable */
369 exec_viewer(fallback, page);
370 exec_viewer("man", page);
372 pr_err("no man viewer handled the request");
376 static int show_info_page(const char *perf_cmd)
378 const char *page = cmd_to_page(perf_cmd);
379 setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
380 execlp("info", "info", "perfman", page, NULL);
384 static int get_html_page_path(char **page_path, const char *page)
387 const char *html_path = system_path(PERF_HTML_PATH);
389 /* Check that we have a perf documentation directory. */
390 if (stat(mkpath("%s/perf.html", html_path), &st)
391 || !S_ISREG(st.st_mode)) {
392 pr_err("'%s': not a documentation directory.", html_path);
396 return asprintf(page_path, "%s/%s.html", html_path, page);
400 * If open_html is not defined in a platform-specific way (see for
401 * example compat/mingw.h), we use the script web--browse to display
405 static void open_html(const char *path)
407 execl_cmd("web--browse", "-c", "help.browser", path, NULL);
411 static int show_html_page(const char *perf_cmd)
413 const char *page = cmd_to_page(perf_cmd);
414 char *page_path; /* it leaks but we exec bellow */
416 if (get_html_page_path(&page_path, page) < 0)
419 open_html(page_path);
424 int cmd_help(int argc, const char **argv)
426 bool show_all = false;
427 enum help_format help_format = HELP_FORMAT_MAN;
428 struct option builtin_help_options[] = {
429 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
430 OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
431 OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
433 OPT_SET_UINT('i', "info", &help_format, "show info page",
437 const char * const builtin_help_subcommands[] = {
438 "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
439 "record", "report", "bench", "stat", "timechart", "top", "annotate",
440 "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
441 #ifdef HAVE_LIBELF_SUPPORT
444 #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
448 const char *builtin_help_usage[] = {
449 "perf help [--all] [--man|--web|--info] [command]",
454 load_command_list("perf-", &main_cmds, &other_cmds);
456 rc = perf_config(perf_help_config, &help_format);
460 argc = parse_options_subcommand(argc, argv, builtin_help_options,
461 builtin_help_subcommands, builtin_help_usage, 0);
464 printf("\n Usage: %s\n\n", perf_usage_string);
465 list_commands("perf commands", &main_cmds, &other_cmds);
466 printf(" %s\n\n", perf_more_info_string);
471 printf("\n usage: %s\n\n", perf_usage_string);
472 list_common_cmds_help();
473 printf("\n %s\n\n", perf_more_info_string);
477 switch (help_format) {
478 case HELP_FORMAT_MAN:
479 rc = show_man_page(argv[0]);
481 case HELP_FORMAT_INFO:
482 rc = show_info_page(argv[0]);
484 case HELP_FORMAT_WEB:
485 rc = show_html_page(argv[0]);
487 case HELP_FORMAT_NONE: