xref: /openbmc/linux/tools/perf/builtin-help.c (revision 0adea51ab2e6a0d9c8336673c7c07abb7a8b858a)
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  */
78520a98dSArnaldo Carvalho de Melo #include "util/cache.h"
841840d21STaeung Song #include "util/config.h"
98520a98dSArnaldo 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"
17*0adea51aSIan Rogers #include "util/util.h"
18877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
198520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
207f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
21a43783aeSArnaldo Carvalho de Melo #include <errno.h>
227a8ef4c4SArnaldo Carvalho de Melo #include <stdio.h>
23215a0d30SArnaldo Carvalho de Melo #include <stdlib.h>
248520a98dSArnaldo Carvalho de Melo #include <string.h>
257a8ef4c4SArnaldo Carvalho de Melo #include <sys/types.h>
267a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h>
277a8ef4c4SArnaldo Carvalho de Melo #include <unistd.h>
2886470930SIngo Molnar 
2986470930SIngo Molnar static struct man_viewer_list {
3086470930SIngo Molnar 	struct man_viewer_list *next;
317909675dSArnaldo Carvalho de Melo 	char name[0];
3286470930SIngo Molnar } *man_viewer_list;
3386470930SIngo Molnar 
3486470930SIngo Molnar static struct man_viewer_info_list {
3586470930SIngo Molnar 	struct man_viewer_info_list *next;
3686470930SIngo Molnar 	const char *info;
377909675dSArnaldo Carvalho de Melo 	char name[0];
3886470930SIngo Molnar } *man_viewer_info_list;
3986470930SIngo Molnar 
4086470930SIngo Molnar enum help_format {
41cc584821SDavid Ahern 	HELP_FORMAT_NONE,
4286470930SIngo Molnar 	HELP_FORMAT_MAN,
4386470930SIngo Molnar 	HELP_FORMAT_INFO,
4486470930SIngo Molnar 	HELP_FORMAT_WEB,
4586470930SIngo Molnar };
4686470930SIngo Molnar 
4786470930SIngo Molnar static enum help_format parse_help_format(const char *format)
4886470930SIngo Molnar {
4986470930SIngo Molnar 	if (!strcmp(format, "man"))
5086470930SIngo Molnar 		return HELP_FORMAT_MAN;
5186470930SIngo Molnar 	if (!strcmp(format, "info"))
5286470930SIngo Molnar 		return HELP_FORMAT_INFO;
5386470930SIngo Molnar 	if (!strcmp(format, "web") || !strcmp(format, "html"))
5486470930SIngo Molnar 		return HELP_FORMAT_WEB;
55cc584821SDavid Ahern 
56cc584821SDavid Ahern 	pr_err("unrecognized help format '%s'", format);
57cc584821SDavid Ahern 	return HELP_FORMAT_NONE;
5886470930SIngo Molnar }
5986470930SIngo Molnar 
6086470930SIngo Molnar static const char *get_man_viewer_info(const char *name)
6186470930SIngo Molnar {
6286470930SIngo Molnar 	struct man_viewer_info_list *viewer;
6386470930SIngo Molnar 
64c1e53017SThiago Farina 	for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
6586470930SIngo Molnar 		if (!strcasecmp(name, viewer->name))
6686470930SIngo Molnar 			return viewer->info;
6786470930SIngo Molnar 	}
6886470930SIngo Molnar 	return NULL;
6986470930SIngo Molnar }
7086470930SIngo Molnar 
7186470930SIngo Molnar static int check_emacsclient_version(void)
7286470930SIngo Molnar {
7386470930SIngo Molnar 	struct strbuf buffer = STRBUF_INIT;
7486470930SIngo Molnar 	struct child_process ec_process;
7586470930SIngo Molnar 	const char *argv_ec[] = { "emacsclient", "--version", NULL };
7686470930SIngo Molnar 	int version;
77b72ca403SMasami Hiramatsu 	int ret = -1;
7886470930SIngo Molnar 
7986470930SIngo Molnar 	/* emacsclient prints its version number on stderr */
8086470930SIngo Molnar 	memset(&ec_process, 0, sizeof(ec_process));
8186470930SIngo Molnar 	ec_process.argv = argv_ec;
8286470930SIngo Molnar 	ec_process.err = -1;
8386470930SIngo Molnar 	ec_process.stdout_to_stderr = 1;
8486470930SIngo Molnar 	if (start_command(&ec_process)) {
8586470930SIngo Molnar 		fprintf(stderr, "Failed to start emacsclient.\n");
8686470930SIngo Molnar 		return -1;
8786470930SIngo Molnar 	}
88b72ca403SMasami Hiramatsu 	if (strbuf_read(&buffer, ec_process.err, 20) < 0) {
89b72ca403SMasami Hiramatsu 		fprintf(stderr, "Failed to read emacsclient version\n");
90b72ca403SMasami Hiramatsu 		goto out;
91b72ca403SMasami Hiramatsu 	}
9286470930SIngo Molnar 	close(ec_process.err);
9386470930SIngo Molnar 
9486470930SIngo Molnar 	/*
9586470930SIngo Molnar 	 * Don't bother checking return value, because "emacsclient --version"
9686470930SIngo Molnar 	 * seems to always exits with code 1.
9786470930SIngo Molnar 	 */
9886470930SIngo Molnar 	finish_command(&ec_process);
9986470930SIngo Molnar 
1008e99b6d4SArnaldo Carvalho de Melo 	if (!strstarts(buffer.buf, "emacsclient")) {
10186470930SIngo Molnar 		fprintf(stderr, "Failed to parse emacsclient version.\n");
102b72ca403SMasami Hiramatsu 		goto out;
10386470930SIngo Molnar 	}
10486470930SIngo Molnar 
105d1130686SArnaldo Carvalho de Melo 	version = atoi(buffer.buf + strlen("emacsclient"));
10686470930SIngo Molnar 
10786470930SIngo Molnar 	if (version < 22) {
10886470930SIngo Molnar 		fprintf(stderr,
10986470930SIngo Molnar 			"emacsclient version '%d' too old (< 22).\n",
11086470930SIngo Molnar 			version);
111b72ca403SMasami Hiramatsu 	} else
112b72ca403SMasami Hiramatsu 		ret = 0;
113b72ca403SMasami Hiramatsu out:
11486470930SIngo Molnar 	strbuf_release(&buffer);
115b72ca403SMasami Hiramatsu 	return ret;
11686470930SIngo Molnar }
11786470930SIngo Molnar 
118881c362dSArnaldo Carvalho de Melo static void exec_failed(const char *cmd)
11986470930SIngo Molnar {
120ede395d2SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
121881c362dSArnaldo Carvalho de Melo 	pr_warning("failed to exec '%s': %s", cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
122881c362dSArnaldo Carvalho de Melo }
123ede395d2SMasami Hiramatsu 
124881c362dSArnaldo Carvalho de Melo static void exec_woman_emacs(const char *path, const char *page)
125881c362dSArnaldo Carvalho de Melo {
12686470930SIngo Molnar 	if (!check_emacsclient_version()) {
12786470930SIngo Molnar 		/* This works only with emacsclient version >= 22. */
128a610f5cbSArnaldo Carvalho de Melo 		char *man_page;
12986470930SIngo Molnar 
13086470930SIngo Molnar 		if (!path)
13186470930SIngo Molnar 			path = "emacsclient";
132a610f5cbSArnaldo Carvalho de Melo 		if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
133a610f5cbSArnaldo Carvalho de Melo 			execlp(path, "emacsclient", "-e", man_page, NULL);
134a610f5cbSArnaldo Carvalho de Melo 			free(man_page);
135a610f5cbSArnaldo Carvalho de Melo 		}
136881c362dSArnaldo Carvalho de Melo 		exec_failed(path);
13786470930SIngo Molnar 	}
13886470930SIngo Molnar }
13986470930SIngo Molnar 
14086470930SIngo Molnar static void exec_man_konqueror(const char *path, const char *page)
14186470930SIngo Molnar {
14286470930SIngo Molnar 	const char *display = getenv("DISPLAY");
143ede395d2SMasami Hiramatsu 
14486470930SIngo Molnar 	if (display && *display) {
145a610f5cbSArnaldo Carvalho de Melo 		char *man_page;
14686470930SIngo Molnar 		const char *filename = "kfmclient";
14786470930SIngo Molnar 
14886470930SIngo Molnar 		/* It's simpler to launch konqueror using kfmclient. */
14986470930SIngo Molnar 		if (path) {
15086470930SIngo Molnar 			const char *file = strrchr(path, '/');
15186470930SIngo Molnar 			if (file && !strcmp(file + 1, "konqueror")) {
15286470930SIngo Molnar 				char *new = strdup(path);
15386470930SIngo Molnar 				char *dest = strrchr(new, '/');
15486470930SIngo Molnar 
15586470930SIngo Molnar 				/* strlen("konqueror") == strlen("kfmclient") */
15686470930SIngo Molnar 				strcpy(dest + 1, "kfmclient");
15786470930SIngo Molnar 				path = new;
15886470930SIngo Molnar 			}
15986470930SIngo Molnar 			if (file)
16086470930SIngo Molnar 				filename = file;
16186470930SIngo Molnar 		} else
16286470930SIngo Molnar 			path = "kfmclient";
163a610f5cbSArnaldo Carvalho de Melo 		if (asprintf(&man_page, "man:%s(1)", page) > 0) {
164a610f5cbSArnaldo Carvalho de Melo 			execlp(path, filename, "newTab", man_page, NULL);
165a610f5cbSArnaldo Carvalho de Melo 			free(man_page);
166a610f5cbSArnaldo Carvalho de Melo 		}
167881c362dSArnaldo Carvalho de Melo 		exec_failed(path);
16886470930SIngo Molnar 	}
16986470930SIngo Molnar }
17086470930SIngo Molnar 
17186470930SIngo Molnar static void exec_man_man(const char *path, const char *page)
17286470930SIngo Molnar {
17386470930SIngo Molnar 	if (!path)
17486470930SIngo Molnar 		path = "man";
17586470930SIngo Molnar 	execlp(path, "man", page, NULL);
176881c362dSArnaldo Carvalho de Melo 	exec_failed(path);
17786470930SIngo Molnar }
17886470930SIngo Molnar 
17986470930SIngo Molnar static void exec_man_cmd(const char *cmd, const char *page)
18086470930SIngo Molnar {
181a610f5cbSArnaldo Carvalho de Melo 	char *shell_cmd;
182ede395d2SMasami Hiramatsu 
183a610f5cbSArnaldo Carvalho de Melo 	if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
184a610f5cbSArnaldo Carvalho de Melo 		execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
185a610f5cbSArnaldo Carvalho de Melo 		free(shell_cmd);
186a610f5cbSArnaldo Carvalho de Melo 	}
187881c362dSArnaldo Carvalho de Melo 	exec_failed(cmd);
18886470930SIngo Molnar }
18986470930SIngo Molnar 
19086470930SIngo Molnar static void add_man_viewer(const char *name)
19186470930SIngo Molnar {
19286470930SIngo Molnar 	struct man_viewer_list **p = &man_viewer_list;
19386470930SIngo Molnar 	size_t len = strlen(name);
19486470930SIngo Molnar 
19586470930SIngo Molnar 	while (*p)
19686470930SIngo Molnar 		p = &((*p)->next);
19736479484SArnaldo Carvalho de Melo 	*p = zalloc(sizeof(**p) + len + 1);
198b6313899SArnaldo Carvalho de Melo 	strcpy((*p)->name, name);
19986470930SIngo Molnar }
20086470930SIngo Molnar 
20186470930SIngo Molnar static int supported_man_viewer(const char *name, size_t len)
20286470930SIngo Molnar {
20386470930SIngo Molnar 	return (!strncasecmp("man", name, len) ||
20486470930SIngo Molnar 		!strncasecmp("woman", name, len) ||
20586470930SIngo Molnar 		!strncasecmp("konqueror", name, len));
20686470930SIngo Molnar }
20786470930SIngo Molnar 
20886470930SIngo Molnar static void do_add_man_viewer_info(const char *name,
20986470930SIngo Molnar 				   size_t len,
21086470930SIngo Molnar 				   const char *value)
21186470930SIngo Molnar {
21236479484SArnaldo Carvalho de Melo 	struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
21386470930SIngo Molnar 
21486470930SIngo Molnar 	strncpy(new->name, name, len);
21586470930SIngo Molnar 	new->info = strdup(value);
21686470930SIngo Molnar 	new->next = man_viewer_info_list;
21786470930SIngo Molnar 	man_viewer_info_list = new;
21886470930SIngo Molnar }
21986470930SIngo Molnar 
22086e474ffSArnaldo Carvalho de Melo static void unsupported_man_viewer(const char *name, const char *var)
22186e474ffSArnaldo Carvalho de Melo {
22286e474ffSArnaldo Carvalho de Melo 	pr_warning("'%s': path for unsupported man viewer.\n"
22386e474ffSArnaldo Carvalho de Melo 		   "Please consider using 'man.<tool>.%s' instead.", name, var);
22486e474ffSArnaldo Carvalho de Melo }
22586e474ffSArnaldo Carvalho de Melo 
22686470930SIngo Molnar static int add_man_viewer_path(const char *name,
22786470930SIngo Molnar 			       size_t len,
22886470930SIngo Molnar 			       const char *value)
22986470930SIngo Molnar {
23086470930SIngo Molnar 	if (supported_man_viewer(name, len))
23186470930SIngo Molnar 		do_add_man_viewer_info(name, len, value);
23286470930SIngo Molnar 	else
23386e474ffSArnaldo Carvalho de Melo 		unsupported_man_viewer(name, "cmd");
23486470930SIngo Molnar 
23586470930SIngo Molnar 	return 0;
23686470930SIngo Molnar }
23786470930SIngo Molnar 
23886470930SIngo Molnar static int add_man_viewer_cmd(const char *name,
23986470930SIngo Molnar 			      size_t len,
24086470930SIngo Molnar 			      const char *value)
24186470930SIngo Molnar {
24286470930SIngo Molnar 	if (supported_man_viewer(name, len))
24386e474ffSArnaldo Carvalho de Melo 		unsupported_man_viewer(name, "path");
24486470930SIngo Molnar 	else
24586470930SIngo Molnar 		do_add_man_viewer_info(name, len, value);
24686470930SIngo Molnar 
24786470930SIngo Molnar 	return 0;
24886470930SIngo Molnar }
24986470930SIngo Molnar 
25086470930SIngo Molnar static int add_man_viewer_info(const char *var, const char *value)
25186470930SIngo Molnar {
25286470930SIngo Molnar 	const char *name = var + 4;
25386470930SIngo Molnar 	const char *subkey = strrchr(name, '.');
25486470930SIngo Molnar 
25562d94b00SArnaldo Carvalho de Melo 	if (!subkey) {
25662d94b00SArnaldo Carvalho de Melo 		pr_err("Config with no key for man viewer: %s", name);
25762d94b00SArnaldo Carvalho de Melo 		return -1;
25862d94b00SArnaldo Carvalho de Melo 	}
25986470930SIngo Molnar 
26086470930SIngo Molnar 	if (!strcmp(subkey, ".path")) {
26186470930SIngo Molnar 		if (!value)
26286470930SIngo Molnar 			return config_error_nonbool(var);
26386470930SIngo Molnar 		return add_man_viewer_path(name, subkey - name, value);
26486470930SIngo Molnar 	}
26586470930SIngo Molnar 	if (!strcmp(subkey, ".cmd")) {
26686470930SIngo Molnar 		if (!value)
26786470930SIngo Molnar 			return config_error_nonbool(var);
26886470930SIngo Molnar 		return add_man_viewer_cmd(name, subkey - name, value);
26986470930SIngo Molnar 	}
27086470930SIngo Molnar 
27159913aabSArnaldo Carvalho de Melo 	pr_warning("'%s': unsupported man viewer sub key.", subkey);
27286470930SIngo Molnar 	return 0;
27386470930SIngo Molnar }
27486470930SIngo Molnar 
27586470930SIngo Molnar static int perf_help_config(const char *var, const char *value, void *cb)
27686470930SIngo Molnar {
2772bae1d1bSArnaldo Carvalho de Melo 	enum help_format *help_formatp = cb;
2782bae1d1bSArnaldo Carvalho de Melo 
27986470930SIngo Molnar 	if (!strcmp(var, "help.format")) {
28086470930SIngo Molnar 		if (!value)
28186470930SIngo Molnar 			return config_error_nonbool(var);
2822bae1d1bSArnaldo Carvalho de Melo 		*help_formatp = parse_help_format(value);
2832bae1d1bSArnaldo Carvalho de Melo 		if (*help_formatp == HELP_FORMAT_NONE)
284cc584821SDavid Ahern 			return -1;
28586470930SIngo Molnar 		return 0;
28686470930SIngo Molnar 	}
28786470930SIngo Molnar 	if (!strcmp(var, "man.viewer")) {
28886470930SIngo Molnar 		if (!value)
28986470930SIngo Molnar 			return config_error_nonbool(var);
29086470930SIngo Molnar 		add_man_viewer(value);
29186470930SIngo Molnar 		return 0;
29286470930SIngo Molnar 	}
2932f0af860SNamhyung Kim 	if (strstarts(var, "man."))
29486470930SIngo Molnar 		return add_man_viewer_info(var, value);
29586470930SIngo Molnar 
296b8cbb349SWang Nan 	return 0;
29786470930SIngo Molnar }
29886470930SIngo Molnar 
29986470930SIngo Molnar static struct cmdnames main_cmds, other_cmds;
30086470930SIngo Molnar 
30186470930SIngo Molnar void list_common_cmds_help(void)
30286470930SIngo Molnar {
303f37a291cSIngo Molnar 	unsigned int i, longest = 0;
30486470930SIngo Molnar 
30586470930SIngo Molnar 	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
30686470930SIngo Molnar 		if (longest < strlen(common_cmds[i].name))
30786470930SIngo Molnar 			longest = strlen(common_cmds[i].name);
30886470930SIngo Molnar 	}
30986470930SIngo Molnar 
31086470930SIngo Molnar 	puts(" The most commonly used perf commands are:");
31186470930SIngo Molnar 	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
3123912f2abSAmerigo Wang 		printf("   %-*s   ", longest, common_cmds[i].name);
31386470930SIngo Molnar 		puts(common_cmds[i].help);
31486470930SIngo Molnar 	}
31586470930SIngo Molnar }
31686470930SIngo Molnar 
31786470930SIngo Molnar static const char *cmd_to_page(const char *perf_cmd)
31886470930SIngo Molnar {
319a610f5cbSArnaldo Carvalho de Melo 	char *s;
320a610f5cbSArnaldo Carvalho de Melo 
32186470930SIngo Molnar 	if (!perf_cmd)
32286470930SIngo Molnar 		return "perf";
3232f0af860SNamhyung Kim 	else if (strstarts(perf_cmd, "perf"))
32486470930SIngo Molnar 		return perf_cmd;
325a610f5cbSArnaldo Carvalho de Melo 
326a610f5cbSArnaldo Carvalho de Melo 	return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
32786470930SIngo Molnar }
32886470930SIngo Molnar 
32986470930SIngo Molnar static void setup_man_path(void)
33086470930SIngo Molnar {
331a610f5cbSArnaldo Carvalho de Melo 	char *new_path;
33286470930SIngo Molnar 	const char *old_path = getenv("MANPATH");
33386470930SIngo Molnar 
33486470930SIngo Molnar 	/* We should always put ':' after our path. If there is no
33586470930SIngo Molnar 	 * old_path, the ':' at the end will let 'man' to try
33686470930SIngo Molnar 	 * system-wide paths after ours to find the manual page. If
33786470930SIngo Molnar 	 * there is old_path, we need ':' as delimiter. */
338a610f5cbSArnaldo Carvalho de Melo 	if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
339a610f5cbSArnaldo Carvalho de Melo 		setenv("MANPATH", new_path, 1);
340a610f5cbSArnaldo Carvalho de Melo 		free(new_path);
341a610f5cbSArnaldo Carvalho de Melo 	} else {
34262d94b00SArnaldo Carvalho de Melo 		pr_err("Unable to setup man path");
343a610f5cbSArnaldo Carvalho de Melo 	}
34486470930SIngo Molnar }
34586470930SIngo Molnar 
34686470930SIngo Molnar static void exec_viewer(const char *name, const char *page)
34786470930SIngo Molnar {
34886470930SIngo Molnar 	const char *info = get_man_viewer_info(name);
34986470930SIngo Molnar 
35086470930SIngo Molnar 	if (!strcasecmp(name, "man"))
35186470930SIngo Molnar 		exec_man_man(info, page);
35286470930SIngo Molnar 	else if (!strcasecmp(name, "woman"))
35386470930SIngo Molnar 		exec_woman_emacs(info, page);
35486470930SIngo Molnar 	else if (!strcasecmp(name, "konqueror"))
35586470930SIngo Molnar 		exec_man_konqueror(info, page);
35686470930SIngo Molnar 	else if (info)
35786470930SIngo Molnar 		exec_man_cmd(info, page);
35886470930SIngo Molnar 	else
35959913aabSArnaldo Carvalho de Melo 		pr_warning("'%s': unknown man viewer.", name);
36086470930SIngo Molnar }
36186470930SIngo Molnar 
362cc584821SDavid Ahern static int show_man_page(const char *perf_cmd)
36386470930SIngo Molnar {
36486470930SIngo Molnar 	struct man_viewer_list *viewer;
36586470930SIngo Molnar 	const char *page = cmd_to_page(perf_cmd);
36686470930SIngo Molnar 	const char *fallback = getenv("PERF_MAN_VIEWER");
36786470930SIngo Molnar 
36886470930SIngo Molnar 	setup_man_path();
36986470930SIngo Molnar 	for (viewer = man_viewer_list; viewer; viewer = viewer->next)
37086470930SIngo Molnar 		exec_viewer(viewer->name, page); /* will return when unable */
371c1e53017SThiago Farina 
37286470930SIngo Molnar 	if (fallback)
37386470930SIngo Molnar 		exec_viewer(fallback, page);
37486470930SIngo Molnar 	exec_viewer("man", page);
375cc584821SDavid Ahern 
376cc584821SDavid Ahern 	pr_err("no man viewer handled the request");
377cc584821SDavid Ahern 	return -1;
37886470930SIngo Molnar }
37986470930SIngo Molnar 
380cc584821SDavid Ahern static int show_info_page(const char *perf_cmd)
38186470930SIngo Molnar {
38286470930SIngo Molnar 	const char *page = cmd_to_page(perf_cmd);
38386470930SIngo Molnar 	setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
38486470930SIngo Molnar 	execlp("info", "info", "perfman", page, NULL);
385cc584821SDavid Ahern 	return -1;
38686470930SIngo Molnar }
38786470930SIngo Molnar 
388a610f5cbSArnaldo Carvalho de Melo static int get_html_page_path(char **page_path, const char *page)
38986470930SIngo Molnar {
39086470930SIngo Molnar 	struct stat st;
39186470930SIngo Molnar 	const char *html_path = system_path(PERF_HTML_PATH);
39286470930SIngo Molnar 
39386470930SIngo Molnar 	/* Check that we have a perf documentation directory. */
39486470930SIngo Molnar 	if (stat(mkpath("%s/perf.html", html_path), &st)
395cc584821SDavid Ahern 	    || !S_ISREG(st.st_mode)) {
396cc584821SDavid Ahern 		pr_err("'%s': not a documentation directory.", html_path);
397cc584821SDavid Ahern 		return -1;
398cc584821SDavid Ahern 	}
39986470930SIngo Molnar 
400a610f5cbSArnaldo Carvalho de Melo 	return asprintf(page_path, "%s/%s.html", html_path, page);
40186470930SIngo Molnar }
40286470930SIngo Molnar 
40386470930SIngo Molnar /*
40486470930SIngo Molnar  * If open_html is not defined in a platform-specific way (see for
40586470930SIngo Molnar  * example compat/mingw.h), we use the script web--browse to display
40686470930SIngo Molnar  * HTML.
40786470930SIngo Molnar  */
40886470930SIngo Molnar #ifndef open_html
40986470930SIngo Molnar static void open_html(const char *path)
41086470930SIngo Molnar {
41146113a54SJosh Poimboeuf 	execl_cmd("web--browse", "-c", "help.browser", path, NULL);
41286470930SIngo Molnar }
41386470930SIngo Molnar #endif
41486470930SIngo Molnar 
415cc584821SDavid Ahern static int show_html_page(const char *perf_cmd)
41686470930SIngo Molnar {
41786470930SIngo Molnar 	const char *page = cmd_to_page(perf_cmd);
418a610f5cbSArnaldo Carvalho de Melo 	char *page_path; /* it leaks but we exec bellow */
41986470930SIngo Molnar 
420a610f5cbSArnaldo Carvalho de Melo 	if (get_html_page_path(&page_path, page) < 0)
421cc584821SDavid Ahern 		return -1;
42286470930SIngo Molnar 
423a610f5cbSArnaldo Carvalho de Melo 	open_html(page_path);
424cc584821SDavid Ahern 
425cc584821SDavid Ahern 	return 0;
42686470930SIngo Molnar }
42786470930SIngo Molnar 
428b0ad8ea6SArnaldo Carvalho de Melo int cmd_help(int argc, const char **argv)
42986470930SIngo Molnar {
4302bae1d1bSArnaldo Carvalho de Melo 	bool show_all = false;
431670ab5d2SNamhyung Kim 	enum help_format help_format = HELP_FORMAT_MAN;
4322bae1d1bSArnaldo Carvalho de Melo 	struct option builtin_help_options[] = {
4332bae1d1bSArnaldo Carvalho de Melo 	OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
4342bae1d1bSArnaldo Carvalho de Melo 	OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
4352bae1d1bSArnaldo Carvalho de Melo 	OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
4362bae1d1bSArnaldo Carvalho de Melo 			HELP_FORMAT_WEB),
4372bae1d1bSArnaldo Carvalho de Melo 	OPT_SET_UINT('i', "info", &help_format, "show info page",
4382bae1d1bSArnaldo Carvalho de Melo 			HELP_FORMAT_INFO),
4392bae1d1bSArnaldo Carvalho de Melo 	OPT_END(),
4402bae1d1bSArnaldo Carvalho de Melo 	};
441e24a1108SYunlong Song 	const char * const builtin_help_subcommands[] = {
442e24a1108SYunlong Song 		"buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
443e24a1108SYunlong Song 		"record", "report", "bench", "stat", "timechart", "top", "annotate",
44435563771SArnaldo Carvalho de Melo 		"script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
445e24a1108SYunlong Song #ifdef HAVE_LIBELF_SUPPORT
446e24a1108SYunlong Song 		"probe",
447e24a1108SYunlong Song #endif
44822e9af4eSJin Yao #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
449e24a1108SYunlong Song 		"trace",
450e24a1108SYunlong Song #endif
451e24a1108SYunlong Song 	NULL };
452e24a1108SYunlong Song 	const char *builtin_help_usage[] = {
4532bae1d1bSArnaldo Carvalho de Melo 		"perf help [--all] [--man|--web|--info] [command]",
4542bae1d1bSArnaldo Carvalho de Melo 		NULL
4552bae1d1bSArnaldo Carvalho de Melo 	};
456ecc4c561SArnaldo Carvalho de Melo 	int rc;
457f37a291cSIngo Molnar 
45886470930SIngo Molnar 	load_command_list("perf-", &main_cmds, &other_cmds);
45986470930SIngo Molnar 
460ecc4c561SArnaldo Carvalho de Melo 	rc = perf_config(perf_help_config, &help_format);
461ecc4c561SArnaldo Carvalho de Melo 	if (rc)
462ecc4c561SArnaldo Carvalho de Melo 		return rc;
46386470930SIngo Molnar 
464e24a1108SYunlong Song 	argc = parse_options_subcommand(argc, argv, builtin_help_options,
465e24a1108SYunlong Song 			builtin_help_subcommands, builtin_help_usage, 0);
46686470930SIngo Molnar 
46786470930SIngo Molnar 	if (show_all) {
4683a134ae9SYunlong Song 		printf("\n Usage: %s\n\n", perf_usage_string);
46986470930SIngo Molnar 		list_commands("perf commands", &main_cmds, &other_cmds);
47086470930SIngo Molnar 		printf(" %s\n\n", perf_more_info_string);
47186470930SIngo Molnar 		return 0;
47286470930SIngo Molnar 	}
47386470930SIngo Molnar 
47486470930SIngo Molnar 	if (!argv[0]) {
47586470930SIngo Molnar 		printf("\n usage: %s\n\n", perf_usage_string);
47686470930SIngo Molnar 		list_common_cmds_help();
47786470930SIngo Molnar 		printf("\n %s\n\n", perf_more_info_string);
47886470930SIngo Molnar 		return 0;
47986470930SIngo Molnar 	}
48086470930SIngo Molnar 
48186470930SIngo Molnar 	switch (help_format) {
48286470930SIngo Molnar 	case HELP_FORMAT_MAN:
483cc584821SDavid Ahern 		rc = show_man_page(argv[0]);
48486470930SIngo Molnar 		break;
48586470930SIngo Molnar 	case HELP_FORMAT_INFO:
486cc584821SDavid Ahern 		rc = show_info_page(argv[0]);
48786470930SIngo Molnar 		break;
48886470930SIngo Molnar 	case HELP_FORMAT_WEB:
489cc584821SDavid Ahern 		rc = show_html_page(argv[0]);
490cc584821SDavid Ahern 		break;
491cc584821SDavid Ahern 	case HELP_FORMAT_NONE:
492cc584821SDavid Ahern 		/* fall-through */
49383a0944fSIngo Molnar 	default:
494cc584821SDavid Ahern 		rc = -1;
49586470930SIngo Molnar 		break;
49686470930SIngo Molnar 	}
49786470930SIngo Molnar 
498cc584821SDavid Ahern 	return rc;
49986470930SIngo Molnar }
500