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