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