xref: /openbmc/linux/tools/perf/builtin-help.c (revision 8520a98dbab61e9e340cdfb72dd17ccc8a98961e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
286470930SIngo Molnar /*
386470930SIngo Molnar  * builtin-help.c
486470930SIngo Molnar  *
586470930SIngo Molnar  * Builtin help command
686470930SIngo Molnar  */
7*8520a98dSArnaldo Carvalho de Melo #include "util/cache.h"
841840d21STaeung Song #include "util/config.h"
9*8520a98dSArnaldo Carvalho de Melo #include "util/strbuf.h"
1086470930SIngo Molnar #include "builtin.h"
114b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h>
1286470930SIngo Molnar #include "common-cmds.h"
134b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
144b6ab94eSJosh Poimboeuf #include <subcmd/run-command.h>
154b6ab94eSJosh Poimboeuf #include <subcmd/help.h>
1684f5d36fSJiri Olsa #include "util/debug.h"
17877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
18*8520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
197f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
20a43783aeSArnaldo Carvalho de Melo #include <errno.h>
217a8ef4c4SArnaldo Carvalho de Melo #include <stdio.h>
22215a0d30SArnaldo Carvalho de Melo #include <stdlib.h>
23*8520a98dSArnaldo Carvalho de Melo #include <string.h>
247a8ef4c4SArnaldo Carvalho de Melo #include <sys/types.h>
257a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h>
267a8ef4c4SArnaldo Carvalho de Melo #include <unistd.h>
2786470930SIngo Molnar 
2886470930SIngo Molnar static struct man_viewer_list {
2986470930SIngo Molnar 	struct man_viewer_list *next;
307909675dSArnaldo Carvalho de Melo 	char name[0];
3186470930SIngo Molnar } *man_viewer_list;
3286470930SIngo Molnar 
3386470930SIngo Molnar static struct man_viewer_info_list {
3486470930SIngo Molnar 	struct man_viewer_info_list *next;
3586470930SIngo Molnar 	const char *info;
367909675dSArnaldo Carvalho de Melo 	char name[0];
3786470930SIngo Molnar } *man_viewer_info_list;
3886470930SIngo Molnar 
3986470930SIngo Molnar enum help_format {
40cc584821SDavid Ahern 	HELP_FORMAT_NONE,
4186470930SIngo Molnar 	HELP_FORMAT_MAN,
4286470930SIngo Molnar 	HELP_FORMAT_INFO,
4386470930SIngo Molnar 	HELP_FORMAT_WEB,
4486470930SIngo Molnar };
4586470930SIngo Molnar 
4686470930SIngo Molnar static enum help_format parse_help_format(const char *format)
4786470930SIngo Molnar {
4886470930SIngo Molnar 	if (!strcmp(format, "man"))
4986470930SIngo Molnar 		return HELP_FORMAT_MAN;
5086470930SIngo Molnar 	if (!strcmp(format, "info"))
5186470930SIngo Molnar 		return HELP_FORMAT_INFO;
5286470930SIngo Molnar 	if (!strcmp(format, "web") || !strcmp(format, "html"))
5386470930SIngo Molnar 		return HELP_FORMAT_WEB;
54cc584821SDavid Ahern 
55cc584821SDavid Ahern 	pr_err("unrecognized help format '%s'", format);
56cc584821SDavid Ahern 	return HELP_FORMAT_NONE;
5786470930SIngo Molnar }
5886470930SIngo Molnar 
5986470930SIngo Molnar static const char *get_man_viewer_info(const char *name)
6086470930SIngo Molnar {
6186470930SIngo Molnar 	struct man_viewer_info_list *viewer;
6286470930SIngo Molnar 
63c1e53017SThiago Farina 	for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
6486470930SIngo Molnar 		if (!strcasecmp(name, viewer->name))
6586470930SIngo Molnar 			return viewer->info;
6686470930SIngo Molnar 	}
6786470930SIngo Molnar 	return NULL;
6886470930SIngo Molnar }
6986470930SIngo Molnar 
7086470930SIngo Molnar static int check_emacsclient_version(void)
7186470930SIngo Molnar {
7286470930SIngo Molnar 	struct strbuf buffer = STRBUF_INIT;
7386470930SIngo Molnar 	struct child_process ec_process;
7486470930SIngo Molnar 	const char *argv_ec[] = { "emacsclient", "--version", NULL };
7586470930SIngo Molnar 	int version;
76b72ca403SMasami Hiramatsu 	int ret = -1;
7786470930SIngo Molnar 
7886470930SIngo Molnar 	/* emacsclient prints its version number on stderr */
7986470930SIngo Molnar 	memset(&ec_process, 0, sizeof(ec_process));
8086470930SIngo Molnar 	ec_process.argv = argv_ec;
8186470930SIngo Molnar 	ec_process.err = -1;
8286470930SIngo Molnar 	ec_process.stdout_to_stderr = 1;
8386470930SIngo Molnar 	if (start_command(&ec_process)) {
8486470930SIngo Molnar 		fprintf(stderr, "Failed to start emacsclient.\n");
8586470930SIngo Molnar 		return -1;
8686470930SIngo Molnar 	}
87b72ca403SMasami Hiramatsu 	if (strbuf_read(&buffer, ec_process.err, 20) < 0) {
88b72ca403SMasami Hiramatsu 		fprintf(stderr, "Failed to read emacsclient version\n");
89b72ca403SMasami Hiramatsu 		goto out;
90b72ca403SMasami Hiramatsu 	}
9186470930SIngo Molnar 	close(ec_process.err);
9286470930SIngo Molnar 
9386470930SIngo Molnar 	/*
9486470930SIngo Molnar 	 * Don't bother checking return value, because "emacsclient --version"
9586470930SIngo Molnar 	 * seems to always exits with code 1.
9686470930SIngo Molnar 	 */
9786470930SIngo Molnar 	finish_command(&ec_process);
9886470930SIngo Molnar 
998e99b6d4SArnaldo Carvalho de Melo 	if (!strstarts(buffer.buf, "emacsclient")) {
10086470930SIngo Molnar 		fprintf(stderr, "Failed to parse emacsclient version.\n");
101b72ca403SMasami Hiramatsu 		goto out;
10286470930SIngo Molnar 	}
10386470930SIngo Molnar 
104d1130686SArnaldo Carvalho de Melo 	version = atoi(buffer.buf + strlen("emacsclient"));
10586470930SIngo Molnar 
10686470930SIngo Molnar 	if (version < 22) {
10786470930SIngo Molnar 		fprintf(stderr,
10886470930SIngo Molnar 			"emacsclient version '%d' too old (< 22).\n",
10986470930SIngo Molnar 			version);
110b72ca403SMasami Hiramatsu 	} else
111b72ca403SMasami Hiramatsu 		ret = 0;
112b72ca403SMasami Hiramatsu out:
11386470930SIngo Molnar 	strbuf_release(&buffer);
114b72ca403SMasami Hiramatsu 	return ret;
11586470930SIngo Molnar }
11686470930SIngo Molnar 
117881c362dSArnaldo Carvalho de Melo static void exec_failed(const char *cmd)
11886470930SIngo Molnar {
119ede395d2SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
120881c362dSArnaldo Carvalho de Melo 	pr_warning("failed to exec '%s': %s", cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
121881c362dSArnaldo Carvalho de Melo }
122ede395d2SMasami Hiramatsu 
123881c362dSArnaldo Carvalho de Melo static void exec_woman_emacs(const char *path, const char *page)
124881c362dSArnaldo Carvalho de Melo {
12586470930SIngo Molnar 	if (!check_emacsclient_version()) {
12686470930SIngo Molnar 		/* This works only with emacsclient version >= 22. */
127a610f5cbSArnaldo Carvalho de Melo 		char *man_page;
12886470930SIngo Molnar 
12986470930SIngo Molnar 		if (!path)
13086470930SIngo Molnar 			path = "emacsclient";
131a610f5cbSArnaldo Carvalho de Melo 		if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
132a610f5cbSArnaldo Carvalho de Melo 			execlp(path, "emacsclient", "-e", man_page, NULL);
133a610f5cbSArnaldo Carvalho de Melo 			free(man_page);
134a610f5cbSArnaldo Carvalho de Melo 		}
135881c362dSArnaldo Carvalho de Melo 		exec_failed(path);
13686470930SIngo Molnar 	}
13786470930SIngo Molnar }
13886470930SIngo Molnar 
13986470930SIngo Molnar static void exec_man_konqueror(const char *path, const char *page)
14086470930SIngo Molnar {
14186470930SIngo Molnar 	const char *display = getenv("DISPLAY");
142ede395d2SMasami Hiramatsu 
14386470930SIngo Molnar 	if (display && *display) {
144a610f5cbSArnaldo Carvalho de Melo 		char *man_page;
14586470930SIngo Molnar 		const char *filename = "kfmclient";
14686470930SIngo Molnar 
14786470930SIngo Molnar 		/* It's simpler to launch konqueror using kfmclient. */
14886470930SIngo Molnar 		if (path) {
14986470930SIngo Molnar 			const char *file = strrchr(path, '/');
15086470930SIngo Molnar 			if (file && !strcmp(file + 1, "konqueror")) {
15186470930SIngo Molnar 				char *new = strdup(path);
15286470930SIngo Molnar 				char *dest = strrchr(new, '/');
15386470930SIngo Molnar 
15486470930SIngo Molnar 				/* strlen("konqueror") == strlen("kfmclient") */
15586470930SIngo Molnar 				strcpy(dest + 1, "kfmclient");
15686470930SIngo Molnar 				path = new;
15786470930SIngo Molnar 			}
15886470930SIngo Molnar 			if (file)
15986470930SIngo Molnar 				filename = file;
16086470930SIngo Molnar 		} else
16186470930SIngo Molnar 			path = "kfmclient";
162a610f5cbSArnaldo Carvalho de Melo 		if (asprintf(&man_page, "man:%s(1)", page) > 0) {
163a610f5cbSArnaldo Carvalho de Melo 			execlp(path, filename, "newTab", man_page, NULL);
164a610f5cbSArnaldo Carvalho de Melo 			free(man_page);
165a610f5cbSArnaldo Carvalho de Melo 		}
166881c362dSArnaldo Carvalho de Melo 		exec_failed(path);
16786470930SIngo Molnar 	}
16886470930SIngo Molnar }
16986470930SIngo Molnar 
17086470930SIngo Molnar static void exec_man_man(const char *path, const char *page)
17186470930SIngo Molnar {
17286470930SIngo Molnar 	if (!path)
17386470930SIngo Molnar 		path = "man";
17486470930SIngo Molnar 	execlp(path, "man", page, NULL);
175881c362dSArnaldo Carvalho de Melo 	exec_failed(path);
17686470930SIngo Molnar }
17786470930SIngo Molnar 
17886470930SIngo Molnar static void exec_man_cmd(const char *cmd, const char *page)
17986470930SIngo Molnar {
180a610f5cbSArnaldo Carvalho de Melo 	char *shell_cmd;
181ede395d2SMasami Hiramatsu 
182a610f5cbSArnaldo Carvalho de Melo 	if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
183a610f5cbSArnaldo Carvalho de Melo 		execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
184a610f5cbSArnaldo Carvalho de Melo 		free(shell_cmd);
185a610f5cbSArnaldo Carvalho de Melo 	}
186881c362dSArnaldo Carvalho de Melo 	exec_failed(cmd);
18786470930SIngo Molnar }
18886470930SIngo Molnar 
18986470930SIngo Molnar static void add_man_viewer(const char *name)
19086470930SIngo Molnar {
19186470930SIngo Molnar 	struct man_viewer_list **p = &man_viewer_list;
19286470930SIngo Molnar 	size_t len = strlen(name);
19386470930SIngo Molnar 
19486470930SIngo Molnar 	while (*p)
19586470930SIngo Molnar 		p = &((*p)->next);
19636479484SArnaldo Carvalho de Melo 	*p = zalloc(sizeof(**p) + len + 1);
197b6313899SArnaldo Carvalho de Melo 	strcpy((*p)->name, name);
19886470930SIngo Molnar }
19986470930SIngo Molnar 
20086470930SIngo Molnar static int supported_man_viewer(const char *name, size_t len)
20186470930SIngo Molnar {
20286470930SIngo Molnar 	return (!strncasecmp("man", name, len) ||
20386470930SIngo Molnar 		!strncasecmp("woman", name, len) ||
20486470930SIngo Molnar 		!strncasecmp("konqueror", name, len));
20586470930SIngo Molnar }
20686470930SIngo Molnar 
20786470930SIngo Molnar static void do_add_man_viewer_info(const char *name,
20886470930SIngo Molnar 				   size_t len,
20986470930SIngo Molnar 				   const char *value)
21086470930SIngo Molnar {
21136479484SArnaldo Carvalho de Melo 	struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
21286470930SIngo Molnar 
21386470930SIngo Molnar 	strncpy(new->name, name, len);
21486470930SIngo Molnar 	new->info = strdup(value);
21586470930SIngo Molnar 	new->next = man_viewer_info_list;
21686470930SIngo Molnar 	man_viewer_info_list = new;
21786470930SIngo Molnar }
21886470930SIngo Molnar 
21986e474ffSArnaldo Carvalho de Melo static void unsupported_man_viewer(const char *name, const char *var)
22086e474ffSArnaldo Carvalho de Melo {
22186e474ffSArnaldo Carvalho de Melo 	pr_warning("'%s': path for unsupported man viewer.\n"
22286e474ffSArnaldo Carvalho de Melo 		   "Please consider using 'man.<tool>.%s' instead.", name, var);
22386e474ffSArnaldo Carvalho de Melo }
22486e474ffSArnaldo Carvalho de Melo 
22586470930SIngo Molnar static int add_man_viewer_path(const char *name,
22686470930SIngo Molnar 			       size_t len,
22786470930SIngo Molnar 			       const char *value)
22886470930SIngo Molnar {
22986470930SIngo Molnar 	if (supported_man_viewer(name, len))
23086470930SIngo Molnar 		do_add_man_viewer_info(name, len, value);
23186470930SIngo Molnar 	else
23286e474ffSArnaldo Carvalho de Melo 		unsupported_man_viewer(name, "cmd");
23386470930SIngo Molnar 
23486470930SIngo Molnar 	return 0;
23586470930SIngo Molnar }
23686470930SIngo Molnar 
23786470930SIngo Molnar static int add_man_viewer_cmd(const char *name,
23886470930SIngo Molnar 			      size_t len,
23986470930SIngo Molnar 			      const char *value)
24086470930SIngo Molnar {
24186470930SIngo Molnar 	if (supported_man_viewer(name, len))
24286e474ffSArnaldo Carvalho de Melo 		unsupported_man_viewer(name, "path");
24386470930SIngo Molnar 	else
24486470930SIngo Molnar 		do_add_man_viewer_info(name, len, value);
24586470930SIngo Molnar 
24686470930SIngo Molnar 	return 0;
24786470930SIngo Molnar }
24886470930SIngo Molnar 
24986470930SIngo Molnar static int add_man_viewer_info(const char *var, const char *value)
25086470930SIngo Molnar {
25186470930SIngo Molnar 	const char *name = var + 4;
25286470930SIngo Molnar 	const char *subkey = strrchr(name, '.');
25386470930SIngo Molnar 
25462d94b00SArnaldo Carvalho de Melo 	if (!subkey) {
25562d94b00SArnaldo Carvalho de Melo 		pr_err("Config with no key for man viewer: %s", name);
25662d94b00SArnaldo Carvalho de Melo 		return -1;
25762d94b00SArnaldo Carvalho de Melo 	}
25886470930SIngo Molnar 
25986470930SIngo Molnar 	if (!strcmp(subkey, ".path")) {
26086470930SIngo Molnar 		if (!value)
26186470930SIngo Molnar 			return config_error_nonbool(var);
26286470930SIngo Molnar 		return add_man_viewer_path(name, subkey - name, value);
26386470930SIngo Molnar 	}
26486470930SIngo Molnar 	if (!strcmp(subkey, ".cmd")) {
26586470930SIngo Molnar 		if (!value)
26686470930SIngo Molnar 			return config_error_nonbool(var);
26786470930SIngo Molnar 		return add_man_viewer_cmd(name, subkey - name, value);
26886470930SIngo Molnar 	}
26986470930SIngo Molnar 
27059913aabSArnaldo Carvalho de Melo 	pr_warning("'%s': unsupported man viewer sub key.", subkey);
27186470930SIngo Molnar 	return 0;
27286470930SIngo Molnar }
27386470930SIngo Molnar 
27486470930SIngo Molnar static int perf_help_config(const char *var, const char *value, void *cb)
27586470930SIngo Molnar {
2762bae1d1bSArnaldo Carvalho de Melo 	enum help_format *help_formatp = cb;
2772bae1d1bSArnaldo Carvalho de Melo 
27886470930SIngo Molnar 	if (!strcmp(var, "help.format")) {
27986470930SIngo Molnar 		if (!value)
28086470930SIngo Molnar 			return config_error_nonbool(var);
2812bae1d1bSArnaldo Carvalho de Melo 		*help_formatp = parse_help_format(value);
2822bae1d1bSArnaldo Carvalho de Melo 		if (*help_formatp == HELP_FORMAT_NONE)
283cc584821SDavid Ahern 			return -1;
28486470930SIngo Molnar 		return 0;
28586470930SIngo Molnar 	}
28686470930SIngo Molnar 	if (!strcmp(var, "man.viewer")) {
28786470930SIngo Molnar 		if (!value)
28886470930SIngo Molnar 			return config_error_nonbool(var);
28986470930SIngo Molnar 		add_man_viewer(value);
29086470930SIngo Molnar 		return 0;
29186470930SIngo Molnar 	}
2922f0af860SNamhyung Kim 	if (strstarts(var, "man."))
29386470930SIngo Molnar 		return add_man_viewer_info(var, value);
29486470930SIngo Molnar 
295b8cbb349SWang Nan 	return 0;
29686470930SIngo Molnar }
29786470930SIngo Molnar 
29886470930SIngo Molnar static struct cmdnames main_cmds, other_cmds;
29986470930SIngo Molnar 
30086470930SIngo Molnar void list_common_cmds_help(void)
30186470930SIngo Molnar {
302f37a291cSIngo Molnar 	unsigned int i, longest = 0;
30386470930SIngo Molnar 
30486470930SIngo Molnar 	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
30586470930SIngo Molnar 		if (longest < strlen(common_cmds[i].name))
30686470930SIngo Molnar 			longest = strlen(common_cmds[i].name);
30786470930SIngo Molnar 	}
30886470930SIngo Molnar 
30986470930SIngo Molnar 	puts(" The most commonly used perf commands are:");
31086470930SIngo Molnar 	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
3113912f2abSAmerigo Wang 		printf("   %-*s   ", longest, common_cmds[i].name);
31286470930SIngo Molnar 		puts(common_cmds[i].help);
31386470930SIngo Molnar 	}
31486470930SIngo Molnar }
31586470930SIngo Molnar 
31686470930SIngo Molnar static const char *cmd_to_page(const char *perf_cmd)
31786470930SIngo Molnar {
318a610f5cbSArnaldo Carvalho de Melo 	char *s;
319a610f5cbSArnaldo Carvalho de Melo 
32086470930SIngo Molnar 	if (!perf_cmd)
32186470930SIngo Molnar 		return "perf";
3222f0af860SNamhyung Kim 	else if (strstarts(perf_cmd, "perf"))
32386470930SIngo Molnar 		return perf_cmd;
324a610f5cbSArnaldo Carvalho de Melo 
325a610f5cbSArnaldo Carvalho de Melo 	return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
32686470930SIngo Molnar }
32786470930SIngo Molnar 
32886470930SIngo Molnar static void setup_man_path(void)
32986470930SIngo Molnar {
330a610f5cbSArnaldo Carvalho de Melo 	char *new_path;
33186470930SIngo Molnar 	const char *old_path = getenv("MANPATH");
33286470930SIngo Molnar 
33386470930SIngo Molnar 	/* We should always put ':' after our path. If there is no
33486470930SIngo Molnar 	 * old_path, the ':' at the end will let 'man' to try
33586470930SIngo Molnar 	 * system-wide paths after ours to find the manual page. If
33686470930SIngo Molnar 	 * there is old_path, we need ':' as delimiter. */
337a610f5cbSArnaldo Carvalho de Melo 	if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
338a610f5cbSArnaldo Carvalho de Melo 		setenv("MANPATH", new_path, 1);
339a610f5cbSArnaldo Carvalho de Melo 		free(new_path);
340a610f5cbSArnaldo Carvalho de Melo 	} else {
34162d94b00SArnaldo Carvalho de Melo 		pr_err("Unable to setup man path");
342a610f5cbSArnaldo Carvalho de Melo 	}
34386470930SIngo Molnar }
34486470930SIngo Molnar 
34586470930SIngo Molnar static void exec_viewer(const char *name, const char *page)
34686470930SIngo Molnar {
34786470930SIngo Molnar 	const char *info = get_man_viewer_info(name);
34886470930SIngo Molnar 
34986470930SIngo Molnar 	if (!strcasecmp(name, "man"))
35086470930SIngo Molnar 		exec_man_man(info, page);
35186470930SIngo Molnar 	else if (!strcasecmp(name, "woman"))
35286470930SIngo Molnar 		exec_woman_emacs(info, page);
35386470930SIngo Molnar 	else if (!strcasecmp(name, "konqueror"))
35486470930SIngo Molnar 		exec_man_konqueror(info, page);
35586470930SIngo Molnar 	else if (info)
35686470930SIngo Molnar 		exec_man_cmd(info, page);
35786470930SIngo Molnar 	else
35859913aabSArnaldo Carvalho de Melo 		pr_warning("'%s': unknown man viewer.", name);
35986470930SIngo Molnar }
36086470930SIngo Molnar 
361cc584821SDavid Ahern static int show_man_page(const char *perf_cmd)
36286470930SIngo Molnar {
36386470930SIngo Molnar 	struct man_viewer_list *viewer;
36486470930SIngo Molnar 	const char *page = cmd_to_page(perf_cmd);
36586470930SIngo Molnar 	const char *fallback = getenv("PERF_MAN_VIEWER");
36686470930SIngo Molnar 
36786470930SIngo Molnar 	setup_man_path();
36886470930SIngo Molnar 	for (viewer = man_viewer_list; viewer; viewer = viewer->next)
36986470930SIngo Molnar 		exec_viewer(viewer->name, page); /* will return when unable */
370c1e53017SThiago Farina 
37186470930SIngo Molnar 	if (fallback)
37286470930SIngo Molnar 		exec_viewer(fallback, page);
37386470930SIngo Molnar 	exec_viewer("man", page);
374cc584821SDavid Ahern 
375cc584821SDavid Ahern 	pr_err("no man viewer handled the request");
376cc584821SDavid Ahern 	return -1;
37786470930SIngo Molnar }
37886470930SIngo Molnar 
379cc584821SDavid Ahern static int show_info_page(const char *perf_cmd)
38086470930SIngo Molnar {
38186470930SIngo Molnar 	const char *page = cmd_to_page(perf_cmd);
38286470930SIngo Molnar 	setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
38386470930SIngo Molnar 	execlp("info", "info", "perfman", page, NULL);
384cc584821SDavid Ahern 	return -1;
38586470930SIngo Molnar }
38686470930SIngo Molnar 
387a610f5cbSArnaldo Carvalho de Melo static int get_html_page_path(char **page_path, const char *page)
38886470930SIngo Molnar {
38986470930SIngo Molnar 	struct stat st;
39086470930SIngo Molnar 	const char *html_path = system_path(PERF_HTML_PATH);
39186470930SIngo Molnar 
39286470930SIngo Molnar 	/* Check that we have a perf documentation directory. */
39386470930SIngo Molnar 	if (stat(mkpath("%s/perf.html", html_path), &st)
394cc584821SDavid Ahern 	    || !S_ISREG(st.st_mode)) {
395cc584821SDavid Ahern 		pr_err("'%s': not a documentation directory.", html_path);
396cc584821SDavid Ahern 		return -1;
397cc584821SDavid Ahern 	}
39886470930SIngo Molnar 
399a610f5cbSArnaldo Carvalho de Melo 	return asprintf(page_path, "%s/%s.html", html_path, page);
40086470930SIngo Molnar }
40186470930SIngo Molnar 
40286470930SIngo Molnar /*
40386470930SIngo Molnar  * If open_html is not defined in a platform-specific way (see for
40486470930SIngo Molnar  * example compat/mingw.h), we use the script web--browse to display
40586470930SIngo Molnar  * HTML.
40686470930SIngo Molnar  */
40786470930SIngo Molnar #ifndef open_html
40886470930SIngo Molnar static void open_html(const char *path)
40986470930SIngo Molnar {
41046113a54SJosh Poimboeuf 	execl_cmd("web--browse", "-c", "help.browser", path, NULL);
41186470930SIngo Molnar }
41286470930SIngo Molnar #endif
41386470930SIngo Molnar 
414cc584821SDavid Ahern static int show_html_page(const char *perf_cmd)
41586470930SIngo Molnar {
41686470930SIngo Molnar 	const char *page = cmd_to_page(perf_cmd);
417a610f5cbSArnaldo Carvalho de Melo 	char *page_path; /* it leaks but we exec bellow */
41886470930SIngo Molnar 
419a610f5cbSArnaldo Carvalho de Melo 	if (get_html_page_path(&page_path, page) < 0)
420cc584821SDavid Ahern 		return -1;
42186470930SIngo Molnar 
422a610f5cbSArnaldo Carvalho de Melo 	open_html(page_path);
423cc584821SDavid Ahern 
424cc584821SDavid Ahern 	return 0;
42586470930SIngo Molnar }
42686470930SIngo Molnar 
427b0ad8ea6SArnaldo Carvalho de Melo int cmd_help(int argc, const char **argv)
42886470930SIngo Molnar {
4292bae1d1bSArnaldo Carvalho de Melo 	bool show_all = false;
430670ab5d2SNamhyung Kim 	enum help_format help_format = HELP_FORMAT_MAN;
4312bae1d1bSArnaldo Carvalho de Melo 	struct option builtin_help_options[] = {
4322bae1d1bSArnaldo Carvalho de Melo 	OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
4332bae1d1bSArnaldo Carvalho de Melo 	OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
4342bae1d1bSArnaldo Carvalho de Melo 	OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
4352bae1d1bSArnaldo Carvalho de Melo 			HELP_FORMAT_WEB),
4362bae1d1bSArnaldo Carvalho de Melo 	OPT_SET_UINT('i', "info", &help_format, "show info page",
4372bae1d1bSArnaldo Carvalho de Melo 			HELP_FORMAT_INFO),
4382bae1d1bSArnaldo Carvalho de Melo 	OPT_END(),
4392bae1d1bSArnaldo Carvalho de Melo 	};
440e24a1108SYunlong Song 	const char * const builtin_help_subcommands[] = {
441e24a1108SYunlong Song 		"buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
442e24a1108SYunlong Song 		"record", "report", "bench", "stat", "timechart", "top", "annotate",
44335563771SArnaldo Carvalho de Melo 		"script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
444e24a1108SYunlong Song #ifdef HAVE_LIBELF_SUPPORT
445e24a1108SYunlong Song 		"probe",
446e24a1108SYunlong Song #endif
44722e9af4eSJin Yao #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
448e24a1108SYunlong Song 		"trace",
449e24a1108SYunlong Song #endif
450e24a1108SYunlong Song 	NULL };
451e24a1108SYunlong Song 	const char *builtin_help_usage[] = {
4522bae1d1bSArnaldo Carvalho de Melo 		"perf help [--all] [--man|--web|--info] [command]",
4532bae1d1bSArnaldo Carvalho de Melo 		NULL
4542bae1d1bSArnaldo Carvalho de Melo 	};
455ecc4c561SArnaldo Carvalho de Melo 	int rc;
456f37a291cSIngo Molnar 
45786470930SIngo Molnar 	load_command_list("perf-", &main_cmds, &other_cmds);
45886470930SIngo Molnar 
459ecc4c561SArnaldo Carvalho de Melo 	rc = perf_config(perf_help_config, &help_format);
460ecc4c561SArnaldo Carvalho de Melo 	if (rc)
461ecc4c561SArnaldo Carvalho de Melo 		return rc;
46286470930SIngo Molnar 
463e24a1108SYunlong Song 	argc = parse_options_subcommand(argc, argv, builtin_help_options,
464e24a1108SYunlong Song 			builtin_help_subcommands, builtin_help_usage, 0);
46586470930SIngo Molnar 
46686470930SIngo Molnar 	if (show_all) {
4673a134ae9SYunlong Song 		printf("\n Usage: %s\n\n", perf_usage_string);
46886470930SIngo Molnar 		list_commands("perf commands", &main_cmds, &other_cmds);
46986470930SIngo Molnar 		printf(" %s\n\n", perf_more_info_string);
47086470930SIngo Molnar 		return 0;
47186470930SIngo Molnar 	}
47286470930SIngo Molnar 
47386470930SIngo Molnar 	if (!argv[0]) {
47486470930SIngo Molnar 		printf("\n usage: %s\n\n", perf_usage_string);
47586470930SIngo Molnar 		list_common_cmds_help();
47686470930SIngo Molnar 		printf("\n %s\n\n", perf_more_info_string);
47786470930SIngo Molnar 		return 0;
47886470930SIngo Molnar 	}
47986470930SIngo Molnar 
48086470930SIngo Molnar 	switch (help_format) {
48186470930SIngo Molnar 	case HELP_FORMAT_MAN:
482cc584821SDavid Ahern 		rc = show_man_page(argv[0]);
48386470930SIngo Molnar 		break;
48486470930SIngo Molnar 	case HELP_FORMAT_INFO:
485cc584821SDavid Ahern 		rc = show_info_page(argv[0]);
48686470930SIngo Molnar 		break;
48786470930SIngo Molnar 	case HELP_FORMAT_WEB:
488cc584821SDavid Ahern 		rc = show_html_page(argv[0]);
489cc584821SDavid Ahern 		break;
490cc584821SDavid Ahern 	case HELP_FORMAT_NONE:
491cc584821SDavid Ahern 		/* fall-through */
49283a0944fSIngo Molnar 	default:
493cc584821SDavid Ahern 		rc = -1;
49486470930SIngo Molnar 		break;
49586470930SIngo Molnar 	}
49686470930SIngo Molnar 
497cc584821SDavid Ahern 	return rc;
49886470930SIngo Molnar }
499