blob: 49d55e21b1b06dbecd9c6935a2e0f33550585514 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
2 * builtin-help.c
3 *
4 * Builtin help command
5 */
Ingo Molnarf37a2912009-07-01 12:37:06 +02006#include "perf.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +02007#include "util/cache.h"
Ingo Molnar07800602009-04-20 15:00:56 +02008#include "builtin.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/exec-cmd.h>
Ingo Molnar07800602009-04-20 15:00:56 +020010#include "common-cmds.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060011#include <subcmd/parse-options.h>
12#include <subcmd/run-command.h>
13#include <subcmd/help.h>
Jiri Olsa84f5d362014-07-14 23:46:48 +020014#include "util/debug.h"
Ingo Molnar07800602009-04-20 15:00:56 +020015
16static struct man_viewer_list {
17 struct man_viewer_list *next;
18 char name[FLEX_ARRAY];
19} *man_viewer_list;
20
21static struct man_viewer_info_list {
22 struct man_viewer_info_list *next;
23 const char *info;
24 char name[FLEX_ARRAY];
25} *man_viewer_info_list;
26
27enum help_format {
David Aherncc584822012-08-26 12:24:45 -060028 HELP_FORMAT_NONE,
Ingo Molnar07800602009-04-20 15:00:56 +020029 HELP_FORMAT_MAN,
30 HELP_FORMAT_INFO,
31 HELP_FORMAT_WEB,
32};
33
Ingo Molnar07800602009-04-20 15:00:56 +020034static enum help_format parse_help_format(const char *format)
35{
36 if (!strcmp(format, "man"))
37 return HELP_FORMAT_MAN;
38 if (!strcmp(format, "info"))
39 return HELP_FORMAT_INFO;
40 if (!strcmp(format, "web") || !strcmp(format, "html"))
41 return HELP_FORMAT_WEB;
David Aherncc584822012-08-26 12:24:45 -060042
43 pr_err("unrecognized help format '%s'", format);
44 return HELP_FORMAT_NONE;
Ingo Molnar07800602009-04-20 15:00:56 +020045}
46
47static const char *get_man_viewer_info(const char *name)
48{
49 struct man_viewer_info_list *viewer;
50
Thiago Farinac1e53012009-11-03 08:28:45 -050051 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
Ingo Molnar07800602009-04-20 15:00:56 +020052 if (!strcasecmp(name, viewer->name))
53 return viewer->info;
54 }
55 return NULL;
56}
57
58static int check_emacsclient_version(void)
59{
60 struct strbuf buffer = STRBUF_INIT;
61 struct child_process ec_process;
62 const char *argv_ec[] = { "emacsclient", "--version", NULL };
63 int version;
64
65 /* emacsclient prints its version number on stderr */
66 memset(&ec_process, 0, sizeof(ec_process));
67 ec_process.argv = argv_ec;
68 ec_process.err = -1;
69 ec_process.stdout_to_stderr = 1;
70 if (start_command(&ec_process)) {
71 fprintf(stderr, "Failed to start emacsclient.\n");
72 return -1;
73 }
74 strbuf_read(&buffer, ec_process.err, 20);
75 close(ec_process.err);
76
77 /*
78 * Don't bother checking return value, because "emacsclient --version"
79 * seems to always exits with code 1.
80 */
81 finish_command(&ec_process);
82
83 if (prefixcmp(buffer.buf, "emacsclient")) {
84 fprintf(stderr, "Failed to parse emacsclient version.\n");
85 strbuf_release(&buffer);
86 return -1;
87 }
88
Arnaldo Carvalho de Melod11306862016-02-23 16:18:37 -030089 version = atoi(buffer.buf + strlen("emacsclient"));
Ingo Molnar07800602009-04-20 15:00:56 +020090
91 if (version < 22) {
92 fprintf(stderr,
93 "emacsclient version '%d' too old (< 22).\n",
94 version);
95 strbuf_release(&buffer);
96 return -1;
97 }
98
99 strbuf_release(&buffer);
100 return 0;
101}
102
Thiago Farinac1e53012009-11-03 08:28:45 -0500103static void exec_woman_emacs(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200104{
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000105 char sbuf[STRERR_BUFSIZE];
106
Ingo Molnar07800602009-04-20 15:00:56 +0200107 if (!check_emacsclient_version()) {
108 /* This works only with emacsclient version >= 22. */
109 struct strbuf man_page = STRBUF_INIT;
110
111 if (!path)
112 path = "emacsclient";
113 strbuf_addf(&man_page, "(woman \"%s\")", page);
114 execlp(path, "emacsclient", "-e", man_page.buf, NULL);
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000115 warning("failed to exec '%s': %s", path,
116 strerror_r(errno, sbuf, sizeof(sbuf)));
Ingo Molnar07800602009-04-20 15:00:56 +0200117 }
118}
119
Thiago Farinac1e53012009-11-03 08:28:45 -0500120static void exec_man_konqueror(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200121{
122 const char *display = getenv("DISPLAY");
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000123
Ingo Molnar07800602009-04-20 15:00:56 +0200124 if (display && *display) {
125 struct strbuf man_page = STRBUF_INIT;
126 const char *filename = "kfmclient";
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000127 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200128
129 /* It's simpler to launch konqueror using kfmclient. */
130 if (path) {
131 const char *file = strrchr(path, '/');
132 if (file && !strcmp(file + 1, "konqueror")) {
133 char *new = strdup(path);
134 char *dest = strrchr(new, '/');
135
136 /* strlen("konqueror") == strlen("kfmclient") */
137 strcpy(dest + 1, "kfmclient");
138 path = new;
139 }
140 if (file)
141 filename = file;
142 } else
143 path = "kfmclient";
144 strbuf_addf(&man_page, "man:%s(1)", page);
145 execlp(path, filename, "newTab", man_page.buf, NULL);
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000146 warning("failed to exec '%s': %s", path,
147 strerror_r(errno, sbuf, sizeof(sbuf)));
Ingo Molnar07800602009-04-20 15:00:56 +0200148 }
149}
150
Thiago Farinac1e53012009-11-03 08:28:45 -0500151static void exec_man_man(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200152{
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000153 char sbuf[STRERR_BUFSIZE];
154
Ingo Molnar07800602009-04-20 15:00:56 +0200155 if (!path)
156 path = "man";
157 execlp(path, "man", page, NULL);
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000158 warning("failed to exec '%s': %s", path,
159 strerror_r(errno, sbuf, sizeof(sbuf)));
Ingo Molnar07800602009-04-20 15:00:56 +0200160}
161
162static void exec_man_cmd(const char *cmd, const char *page)
163{
164 struct strbuf shell_cmd = STRBUF_INIT;
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000165 char sbuf[STRERR_BUFSIZE];
166
Ingo Molnar07800602009-04-20 15:00:56 +0200167 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
168 execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000169 warning("failed to exec '%s': %s", cmd,
170 strerror_r(errno, sbuf, sizeof(sbuf)));
Ingo Molnar07800602009-04-20 15:00:56 +0200171}
172
173static void add_man_viewer(const char *name)
174{
175 struct man_viewer_list **p = &man_viewer_list;
176 size_t len = strlen(name);
177
178 while (*p)
179 p = &((*p)->next);
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200180 *p = zalloc(sizeof(**p) + len + 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200181 strncpy((*p)->name, name, len);
182}
183
184static int supported_man_viewer(const char *name, size_t len)
185{
186 return (!strncasecmp("man", name, len) ||
187 !strncasecmp("woman", name, len) ||
188 !strncasecmp("konqueror", name, len));
189}
190
191static void do_add_man_viewer_info(const char *name,
192 size_t len,
193 const char *value)
194{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200195 struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200196
197 strncpy(new->name, name, len);
198 new->info = strdup(value);
199 new->next = man_viewer_info_list;
200 man_viewer_info_list = new;
201}
202
203static int add_man_viewer_path(const char *name,
204 size_t len,
205 const char *value)
206{
207 if (supported_man_viewer(name, len))
208 do_add_man_viewer_info(name, len, value);
209 else
210 warning("'%s': path for unsupported man viewer.\n"
211 "Please consider using 'man.<tool>.cmd' instead.",
212 name);
213
214 return 0;
215}
216
217static int add_man_viewer_cmd(const char *name,
218 size_t len,
219 const char *value)
220{
221 if (supported_man_viewer(name, len))
222 warning("'%s': cmd for supported man viewer.\n"
223 "Please consider using 'man.<tool>.path' instead.",
224 name);
225 else
226 do_add_man_viewer_info(name, len, value);
227
228 return 0;
229}
230
231static int add_man_viewer_info(const char *var, const char *value)
232{
233 const char *name = var + 4;
234 const char *subkey = strrchr(name, '.');
235
236 if (!subkey)
237 return error("Config with no key for man viewer: %s", name);
238
239 if (!strcmp(subkey, ".path")) {
240 if (!value)
241 return config_error_nonbool(var);
242 return add_man_viewer_path(name, subkey - name, value);
243 }
244 if (!strcmp(subkey, ".cmd")) {
245 if (!value)
246 return config_error_nonbool(var);
247 return add_man_viewer_cmd(name, subkey - name, value);
248 }
249
250 warning("'%s': unsupported man viewer sub key.", subkey);
251 return 0;
252}
253
254static int perf_help_config(const char *var, const char *value, void *cb)
255{
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300256 enum help_format *help_formatp = cb;
257
Ingo Molnar07800602009-04-20 15:00:56 +0200258 if (!strcmp(var, "help.format")) {
259 if (!value)
260 return config_error_nonbool(var);
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300261 *help_formatp = parse_help_format(value);
262 if (*help_formatp == HELP_FORMAT_NONE)
David Aherncc584822012-08-26 12:24:45 -0600263 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200264 return 0;
265 }
266 if (!strcmp(var, "man.viewer")) {
267 if (!value)
268 return config_error_nonbool(var);
269 add_man_viewer(value);
270 return 0;
271 }
272 if (!prefixcmp(var, "man."))
273 return add_man_viewer_info(var, value);
274
Wang Nanb8cbb342016-02-26 09:31:51 +0000275 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200276}
277
278static struct cmdnames main_cmds, other_cmds;
279
280void list_common_cmds_help(void)
281{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200282 unsigned int i, longest = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200283
284 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
285 if (longest < strlen(common_cmds[i].name))
286 longest = strlen(common_cmds[i].name);
287 }
288
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100289 puts(" The most commonly used perf commands are:");
Ingo Molnar07800602009-04-20 15:00:56 +0200290 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
Amerigo Wang3912f2a2009-12-14 03:23:56 -0500291 printf(" %-*s ", longest, common_cmds[i].name);
Ingo Molnar07800602009-04-20 15:00:56 +0200292 puts(common_cmds[i].help);
293 }
294}
295
296static int is_perf_command(const char *s)
297{
298 return is_in_cmdlist(&main_cmds, s) ||
299 is_in_cmdlist(&other_cmds, s);
300}
301
302static const char *prepend(const char *prefix, const char *cmd)
303{
304 size_t pre_len = strlen(prefix);
305 size_t cmd_len = strlen(cmd);
306 char *p = malloc(pre_len + cmd_len + 1);
307 memcpy(p, prefix, pre_len);
308 strcpy(p + pre_len, cmd);
309 return p;
310}
311
312static const char *cmd_to_page(const char *perf_cmd)
313{
314 if (!perf_cmd)
315 return "perf";
316 else if (!prefixcmp(perf_cmd, "perf"))
317 return perf_cmd;
Ingo Molnar07800602009-04-20 15:00:56 +0200318 else
Ingo Molnarc1c23652009-05-30 12:38:51 +0200319 return prepend("perf-", perf_cmd);
Ingo Molnar07800602009-04-20 15:00:56 +0200320}
321
322static void setup_man_path(void)
323{
324 struct strbuf new_path = STRBUF_INIT;
325 const char *old_path = getenv("MANPATH");
326
327 /* We should always put ':' after our path. If there is no
328 * old_path, the ':' at the end will let 'man' to try
329 * system-wide paths after ours to find the manual page. If
330 * there is old_path, we need ':' as delimiter. */
331 strbuf_addstr(&new_path, system_path(PERF_MAN_PATH));
332 strbuf_addch(&new_path, ':');
333 if (old_path)
334 strbuf_addstr(&new_path, old_path);
335
336 setenv("MANPATH", new_path.buf, 1);
337
338 strbuf_release(&new_path);
339}
340
341static void exec_viewer(const char *name, const char *page)
342{
343 const char *info = get_man_viewer_info(name);
344
345 if (!strcasecmp(name, "man"))
346 exec_man_man(info, page);
347 else if (!strcasecmp(name, "woman"))
348 exec_woman_emacs(info, page);
349 else if (!strcasecmp(name, "konqueror"))
350 exec_man_konqueror(info, page);
351 else if (info)
352 exec_man_cmd(info, page);
353 else
354 warning("'%s': unknown man viewer.", name);
355}
356
David Aherncc584822012-08-26 12:24:45 -0600357static int show_man_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200358{
359 struct man_viewer_list *viewer;
360 const char *page = cmd_to_page(perf_cmd);
361 const char *fallback = getenv("PERF_MAN_VIEWER");
362
363 setup_man_path();
364 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
Ingo Molnar07800602009-04-20 15:00:56 +0200365 exec_viewer(viewer->name, page); /* will return when unable */
Thiago Farinac1e53012009-11-03 08:28:45 -0500366
Ingo Molnar07800602009-04-20 15:00:56 +0200367 if (fallback)
368 exec_viewer(fallback, page);
369 exec_viewer("man", page);
David Aherncc584822012-08-26 12:24:45 -0600370
371 pr_err("no man viewer handled the request");
372 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200373}
374
David Aherncc584822012-08-26 12:24:45 -0600375static int show_info_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200376{
377 const char *page = cmd_to_page(perf_cmd);
378 setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
379 execlp("info", "info", "perfman", page, NULL);
David Aherncc584822012-08-26 12:24:45 -0600380 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200381}
382
David Aherncc584822012-08-26 12:24:45 -0600383static int get_html_page_path(struct strbuf *page_path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200384{
385 struct stat st;
386 const char *html_path = system_path(PERF_HTML_PATH);
387
388 /* Check that we have a perf documentation directory. */
389 if (stat(mkpath("%s/perf.html", html_path), &st)
David Aherncc584822012-08-26 12:24:45 -0600390 || !S_ISREG(st.st_mode)) {
391 pr_err("'%s': not a documentation directory.", html_path);
392 return -1;
393 }
Ingo Molnar07800602009-04-20 15:00:56 +0200394
395 strbuf_init(page_path, 0);
396 strbuf_addf(page_path, "%s/%s.html", html_path, page);
David Aherncc584822012-08-26 12:24:45 -0600397
398 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200399}
400
401/*
402 * If open_html is not defined in a platform-specific way (see for
403 * example compat/mingw.h), we use the script web--browse to display
404 * HTML.
405 */
406#ifndef open_html
Ingo Molnar16f762a2009-05-27 09:10:38 +0200407static void open_html(const char *path)
Ingo Molnar07800602009-04-20 15:00:56 +0200408{
Josh Poimboeuf46113a52015-12-15 09:39:37 -0600409 execl_cmd("web--browse", "-c", "help.browser", path, NULL);
Ingo Molnar07800602009-04-20 15:00:56 +0200410}
411#endif
412
David Aherncc584822012-08-26 12:24:45 -0600413static int show_html_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200414{
415 const char *page = cmd_to_page(perf_cmd);
416 struct strbuf page_path; /* it leaks but we exec bellow */
417
David Aherncc584822012-08-26 12:24:45 -0600418 if (get_html_page_path(&page_path, page) != 0)
419 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200420
421 open_html(page_path.buf);
David Aherncc584822012-08-26 12:24:45 -0600422
423 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200424}
425
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300426int cmd_help(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar07800602009-04-20 15:00:56 +0200427{
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300428 bool show_all = false;
Namhyung Kim670ab5d2012-10-22 16:12:23 +0900429 enum help_format help_format = HELP_FORMAT_MAN;
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300430 struct option builtin_help_options[] = {
431 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
432 OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
433 OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
434 HELP_FORMAT_WEB),
435 OPT_SET_UINT('i', "info", &help_format, "show info page",
436 HELP_FORMAT_INFO),
437 OPT_END(),
438 };
Yunlong Songe24a1102015-03-18 21:35:53 +0800439 const char * const builtin_help_subcommands[] = {
440 "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
441 "record", "report", "bench", "stat", "timechart", "top", "annotate",
442 "script", "sched", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
443#ifdef HAVE_LIBELF_SUPPORT
444 "probe",
445#endif
446#ifdef HAVE_LIBAUDIT_SUPPORT
447 "trace",
448#endif
449 NULL };
450 const char *builtin_help_usage[] = {
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300451 "perf help [--all] [--man|--web|--info] [command]",
452 NULL
453 };
Ingo Molnar07800602009-04-20 15:00:56 +0200454 const char *alias;
David Aherncc584822012-08-26 12:24:45 -0600455 int rc = 0;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200456
Ingo Molnar07800602009-04-20 15:00:56 +0200457 load_command_list("perf-", &main_cmds, &other_cmds);
458
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300459 perf_config(perf_help_config, &help_format);
Ingo Molnar07800602009-04-20 15:00:56 +0200460
Yunlong Songe24a1102015-03-18 21:35:53 +0800461 argc = parse_options_subcommand(argc, argv, builtin_help_options,
462 builtin_help_subcommands, builtin_help_usage, 0);
Ingo Molnar07800602009-04-20 15:00:56 +0200463
464 if (show_all) {
Yunlong Song3a134ae2015-10-15 15:39:51 +0800465 printf("\n Usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200466 list_commands("perf commands", &main_cmds, &other_cmds);
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100467 printf(" %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200468 return 0;
469 }
470
471 if (!argv[0]) {
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100472 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200473 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100474 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200475 return 0;
476 }
477
478 alias = alias_lookup(argv[0]);
479 if (alias && !is_perf_command(argv[0])) {
480 printf("`perf %s' is aliased to `%s'\n", argv[0], alias);
481 return 0;
482 }
483
484 switch (help_format) {
485 case HELP_FORMAT_MAN:
David Aherncc584822012-08-26 12:24:45 -0600486 rc = show_man_page(argv[0]);
Ingo Molnar07800602009-04-20 15:00:56 +0200487 break;
488 case HELP_FORMAT_INFO:
David Aherncc584822012-08-26 12:24:45 -0600489 rc = show_info_page(argv[0]);
Ingo Molnar07800602009-04-20 15:00:56 +0200490 break;
491 case HELP_FORMAT_WEB:
David Aherncc584822012-08-26 12:24:45 -0600492 rc = show_html_page(argv[0]);
493 break;
494 case HELP_FORMAT_NONE:
495 /* fall-through */
Ingo Molnar83a09442009-08-15 12:26:57 +0200496 default:
David Aherncc584822012-08-26 12:24:45 -0600497 rc = -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200498 break;
499 }
500
David Aherncc584822012-08-26 12:24:45 -0600501 return rc;
Ingo Molnar07800602009-04-20 15:00:56 +0200502}