1 // SPDX-License-Identifier: GPL-2.0
7 #include "util/cache.h"
8 #include "util/config.h"
9 #include "util/strbuf.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>
26 #include <sys/types.h>
30 static struct man_viewer_list
{
31 struct man_viewer_list
*next
;
35 static struct man_viewer_info_list
{
36 struct man_viewer_info_list
*next
;
39 } *man_viewer_info_list
;
48 static enum help_format
parse_help_format(const char *format
)
50 if (!strcmp(format
, "man"))
51 return HELP_FORMAT_MAN
;
52 if (!strcmp(format
, "info"))
53 return HELP_FORMAT_INFO
;
54 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
55 return HELP_FORMAT_WEB
;
57 pr_err("unrecognized help format '%s'", format
);
58 return HELP_FORMAT_NONE
;
61 static const char *get_man_viewer_info(const char *name
)
63 struct man_viewer_info_list
*viewer
;
65 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
) {
66 if (!strcasecmp(name
, viewer
->name
))
72 static int check_emacsclient_version(void)
74 struct strbuf buffer
= STRBUF_INIT
;
75 struct child_process ec_process
;
76 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
80 /* emacsclient prints its version number on stderr */
81 memset(&ec_process
, 0, sizeof(ec_process
));
82 ec_process
.argv
= argv_ec
;
84 ec_process
.stdout_to_stderr
= 1;
85 if (start_command(&ec_process
)) {
86 fprintf(stderr
, "Failed to start emacsclient.\n");
89 if (strbuf_read(&buffer
, ec_process
.err
, 20) < 0) {
90 fprintf(stderr
, "Failed to read emacsclient version\n");
93 close(ec_process
.err
);
96 * Don't bother checking return value, because "emacsclient --version"
97 * seems to always exits with code 1.
99 finish_command(&ec_process
);
101 if (!strstarts(buffer
.buf
, "emacsclient")) {
102 fprintf(stderr
, "Failed to parse emacsclient version.\n");
106 version
= atoi(buffer
.buf
+ strlen("emacsclient"));
110 "emacsclient version '%d' too old (< 22).\n",
115 strbuf_release(&buffer
);
119 static void exec_failed(const char *cmd
)
121 char sbuf
[STRERR_BUFSIZE
];
122 pr_warning("failed to exec '%s': %s", cmd
, str_error_r(errno
, sbuf
, sizeof(sbuf
)));
125 static void exec_woman_emacs(const char *path
, const char *page
)
127 if (!check_emacsclient_version()) {
128 /* This works only with emacsclient version >= 22. */
132 path
= "emacsclient";
133 if (asprintf(&man_page
, "(woman \"%s\")", page
) > 0) {
134 execlp(path
, "emacsclient", "-e", man_page
, NULL
);
141 static void exec_man_konqueror(const char *path
, const char *page
)
143 const char *display
= getenv("DISPLAY");
145 if (display
&& *display
) {
147 const char *filename
= "kfmclient";
149 /* It's simpler to launch konqueror using kfmclient. */
151 const char *file
= strrchr(path
, '/');
152 if (file
&& !strcmp(file
+ 1, "konqueror")) {
153 char *new = strdup(path
);
154 char *dest
= strrchr(new, '/');
156 /* strlen("konqueror") == strlen("kfmclient") */
157 strcpy(dest
+ 1, "kfmclient");
164 if (asprintf(&man_page
, "man:%s(1)", page
) > 0) {
165 execlp(path
, filename
, "newTab", man_page
, NULL
);
172 static void exec_man_man(const char *path
, const char *page
)
176 execlp(path
, "man", page
, NULL
);
180 static void exec_man_cmd(const char *cmd
, const char *page
)
184 if (asprintf(&shell_cmd
, "%s %s", cmd
, page
) > 0) {
185 execl("/bin/sh", "sh", "-c", shell_cmd
, NULL
);
191 static void add_man_viewer(const char *name
)
193 struct man_viewer_list
**p
= &man_viewer_list
;
194 size_t len
= strlen(name
);
198 *p
= zalloc(sizeof(**p
) + len
+ 1);
199 strcpy((*p
)->name
, name
);
202 static int supported_man_viewer(const char *name
, size_t len
)
204 return (!strncasecmp("man", name
, len
) ||
205 !strncasecmp("woman", name
, len
) ||
206 !strncasecmp("konqueror", name
, len
));
209 static void do_add_man_viewer_info(const char *name
,
213 struct man_viewer_info_list
*new = zalloc(sizeof(*new) + len
+ 1);
215 strncpy(new->name
, name
, len
);
216 new->info
= strdup(value
);
217 new->next
= man_viewer_info_list
;
218 man_viewer_info_list
= new;
221 static void unsupported_man_viewer(const char *name
, const char *var
)
223 pr_warning("'%s': path for unsupported man viewer.\n"
224 "Please consider using 'man.<tool>.%s' instead.", name
, var
);
227 static int add_man_viewer_path(const char *name
,
231 if (supported_man_viewer(name
, len
))
232 do_add_man_viewer_info(name
, len
, value
);
234 unsupported_man_viewer(name
, "cmd");
239 static int add_man_viewer_cmd(const char *name
,
243 if (supported_man_viewer(name
, len
))
244 unsupported_man_viewer(name
, "path");
246 do_add_man_viewer_info(name
, len
, value
);
251 static int add_man_viewer_info(const char *var
, const char *value
)
253 const char *name
= var
+ 4;
254 const char *subkey
= strrchr(name
, '.');
257 pr_err("Config with no key for man viewer: %s", name
);
261 if (!strcmp(subkey
, ".path")) {
263 return config_error_nonbool(var
);
264 return add_man_viewer_path(name
, subkey
- name
, value
);
266 if (!strcmp(subkey
, ".cmd")) {
268 return config_error_nonbool(var
);
269 return add_man_viewer_cmd(name
, subkey
- name
, value
);
272 pr_warning("'%s': unsupported man viewer sub key.", subkey
);
276 static int perf_help_config(const char *var
, const char *value
, void *cb
)
278 enum help_format
*help_formatp
= cb
;
280 if (!strcmp(var
, "help.format")) {
282 return config_error_nonbool(var
);
283 *help_formatp
= parse_help_format(value
);
284 if (*help_formatp
== HELP_FORMAT_NONE
)
288 if (!strcmp(var
, "man.viewer")) {
290 return config_error_nonbool(var
);
291 add_man_viewer(value
);
294 if (strstarts(var
, "man."))
295 return add_man_viewer_info(var
, value
);
300 static struct cmdnames main_cmds
, other_cmds
;
302 void list_common_cmds_help(void)
304 unsigned int i
, longest
= 0;
306 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
307 if (longest
< strlen(common_cmds
[i
].name
))
308 longest
= strlen(common_cmds
[i
].name
);
311 puts(" The most commonly used perf commands are:");
312 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
313 printf(" %-*s ", longest
, common_cmds
[i
].name
);
314 puts(common_cmds
[i
].help
);
318 static const char *cmd_to_page(const char *perf_cmd
)
324 else if (strstarts(perf_cmd
, "perf"))
327 return asprintf(&s
, "perf-%s", perf_cmd
) < 0 ? NULL
: s
;
330 static void setup_man_path(void)
333 const char *old_path
= getenv("MANPATH");
335 /* We should always put ':' after our path. If there is no
336 * old_path, the ':' at the end will let 'man' to try
337 * system-wide paths after ours to find the manual page. If
338 * there is old_path, we need ':' as delimiter. */
339 if (asprintf(&new_path
, "%s:%s", system_path(PERF_MAN_PATH
), old_path
?: "") > 0) {
340 setenv("MANPATH", new_path
, 1);
343 pr_err("Unable to setup man path");
347 static void exec_viewer(const char *name
, const char *page
)
349 const char *info
= get_man_viewer_info(name
);
351 if (!strcasecmp(name
, "man"))
352 exec_man_man(info
, page
);
353 else if (!strcasecmp(name
, "woman"))
354 exec_woman_emacs(info
, page
);
355 else if (!strcasecmp(name
, "konqueror"))
356 exec_man_konqueror(info
, page
);
358 exec_man_cmd(info
, page
);
360 pr_warning("'%s': unknown man viewer.", name
);
363 static int show_man_page(const char *perf_cmd
)
365 struct man_viewer_list
*viewer
;
366 const char *page
= cmd_to_page(perf_cmd
);
367 const char *fallback
= getenv("PERF_MAN_VIEWER");
370 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
371 exec_viewer(viewer
->name
, page
); /* will return when unable */
374 exec_viewer(fallback
, page
);
375 exec_viewer("man", page
);
377 pr_err("no man viewer handled the request");
381 static int show_info_page(const char *perf_cmd
)
383 const char *page
= cmd_to_page(perf_cmd
);
384 setenv("INFOPATH", system_path(PERF_INFO_PATH
), 1);
385 execlp("info", "info", "perfman", page
, NULL
);
389 static int get_html_page_path(char **page_path
, const char *page
)
392 const char *html_path
= system_path(PERF_HTML_PATH
);
395 /* Check that we have a perf documentation directory. */
396 if (stat(mkpath(path
, sizeof(path
), "%s/perf.html", html_path
), &st
)
397 || !S_ISREG(st
.st_mode
)) {
398 pr_err("'%s': not a documentation directory.", html_path
);
402 return asprintf(page_path
, "%s/%s.html", html_path
, page
);
406 * If open_html is not defined in a platform-specific way (see for
407 * example compat/mingw.h), we use the script web--browse to display
411 static void open_html(const char *path
)
413 execl_cmd("web--browse", "-c", "help.browser", path
, NULL
);
417 static int show_html_page(const char *perf_cmd
)
419 const char *page
= cmd_to_page(perf_cmd
);
420 char *page_path
; /* it leaks but we exec below */
422 if (get_html_page_path(&page_path
, page
) < 0)
425 open_html(page_path
);
430 int cmd_help(int argc
, const char **argv
)
432 bool show_all
= false;
433 enum help_format help_format
= HELP_FORMAT_MAN
;
434 struct option builtin_help_options
[] = {
435 OPT_BOOLEAN('a', "all", &show_all
, "print all available commands"),
436 OPT_SET_UINT('m', "man", &help_format
, "show man page", HELP_FORMAT_MAN
),
437 OPT_SET_UINT('w', "web", &help_format
, "show manual in web browser",
439 OPT_SET_UINT('i', "info", &help_format
, "show info page",
443 const char * const builtin_help_subcommands
[] = {
444 "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
445 "record", "report", "bench", "stat", "timechart", "top", "annotate",
446 "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
447 #ifdef HAVE_LIBELF_SUPPORT
450 #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
454 const char *builtin_help_usage
[] = {
455 "perf help [--all] [--man|--web|--info] [command]",
460 load_command_list("perf-", &main_cmds
, &other_cmds
);
462 rc
= perf_config(perf_help_config
, &help_format
);
466 argc
= parse_options_subcommand(argc
, argv
, builtin_help_options
,
467 builtin_help_subcommands
, builtin_help_usage
, 0);
470 printf("\n Usage: %s\n\n", perf_usage_string
);
471 list_commands("perf commands", &main_cmds
, &other_cmds
);
472 printf(" %s\n\n", perf_more_info_string
);
477 printf("\n usage: %s\n\n", perf_usage_string
);
478 list_common_cmds_help();
479 printf("\n %s\n\n", perf_more_info_string
);
483 switch (help_format
) {
484 case HELP_FORMAT_MAN
:
485 rc
= show_man_page(argv
[0]);
487 case HELP_FORMAT_INFO
:
488 rc
= show_info_page(argv
[0]);
490 case HELP_FORMAT_WEB
:
491 rc
= show_html_page(argv
[0]);
493 case HELP_FORMAT_NONE
: