1 // SPDX-License-Identifier: GPL-2.0
8 #include "util/config.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>
19 #include <sys/types.h>
23 static struct man_viewer_list
{
24 struct man_viewer_list
*next
;
28 static struct man_viewer_info_list
{
29 struct man_viewer_info_list
*next
;
32 } *man_viewer_info_list
;
41 static enum help_format
parse_help_format(const char *format
)
43 if (!strcmp(format
, "man"))
44 return HELP_FORMAT_MAN
;
45 if (!strcmp(format
, "info"))
46 return HELP_FORMAT_INFO
;
47 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
48 return HELP_FORMAT_WEB
;
50 pr_err("unrecognized help format '%s'", format
);
51 return HELP_FORMAT_NONE
;
54 static const char *get_man_viewer_info(const char *name
)
56 struct man_viewer_info_list
*viewer
;
58 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
) {
59 if (!strcasecmp(name
, viewer
->name
))
65 static int check_emacsclient_version(void)
67 struct strbuf buffer
= STRBUF_INIT
;
68 struct child_process ec_process
;
69 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
73 /* emacsclient prints its version number on stderr */
74 memset(&ec_process
, 0, sizeof(ec_process
));
75 ec_process
.argv
= argv_ec
;
77 ec_process
.stdout_to_stderr
= 1;
78 if (start_command(&ec_process
)) {
79 fprintf(stderr
, "Failed to start emacsclient.\n");
82 if (strbuf_read(&buffer
, ec_process
.err
, 20) < 0) {
83 fprintf(stderr
, "Failed to read emacsclient version\n");
86 close(ec_process
.err
);
89 * Don't bother checking return value, because "emacsclient --version"
90 * seems to always exits with code 1.
92 finish_command(&ec_process
);
94 if (!strstarts(buffer
.buf
, "emacsclient")) {
95 fprintf(stderr
, "Failed to parse emacsclient version.\n");
99 version
= atoi(buffer
.buf
+ strlen("emacsclient"));
103 "emacsclient version '%d' too old (< 22).\n",
108 strbuf_release(&buffer
);
112 static void exec_failed(const char *cmd
)
114 char sbuf
[STRERR_BUFSIZE
];
115 pr_warning("failed to exec '%s': %s", cmd
, str_error_r(errno
, sbuf
, sizeof(sbuf
)));
118 static void exec_woman_emacs(const char *path
, const char *page
)
120 if (!check_emacsclient_version()) {
121 /* This works only with emacsclient version >= 22. */
125 path
= "emacsclient";
126 if (asprintf(&man_page
, "(woman \"%s\")", page
) > 0) {
127 execlp(path
, "emacsclient", "-e", man_page
, NULL
);
134 static void exec_man_konqueror(const char *path
, const char *page
)
136 const char *display
= getenv("DISPLAY");
138 if (display
&& *display
) {
140 const char *filename
= "kfmclient";
142 /* It's simpler to launch konqueror using kfmclient. */
144 const char *file
= strrchr(path
, '/');
145 if (file
&& !strcmp(file
+ 1, "konqueror")) {
146 char *new = strdup(path
);
147 char *dest
= strrchr(new, '/');
149 /* strlen("konqueror") == strlen("kfmclient") */
150 strcpy(dest
+ 1, "kfmclient");
157 if (asprintf(&man_page
, "man:%s(1)", page
) > 0) {
158 execlp(path
, filename
, "newTab", man_page
, NULL
);
165 static void exec_man_man(const char *path
, const char *page
)
169 execlp(path
, "man", page
, NULL
);
173 static void exec_man_cmd(const char *cmd
, const char *page
)
177 if (asprintf(&shell_cmd
, "%s %s", cmd
, page
) > 0) {
178 execl("/bin/sh", "sh", "-c", shell_cmd
, NULL
);
184 static void add_man_viewer(const char *name
)
186 struct man_viewer_list
**p
= &man_viewer_list
;
187 size_t len
= strlen(name
);
191 *p
= zalloc(sizeof(**p
) + len
+ 1);
192 strncpy((*p
)->name
, name
, len
);
195 static int supported_man_viewer(const char *name
, size_t len
)
197 return (!strncasecmp("man", name
, len
) ||
198 !strncasecmp("woman", name
, len
) ||
199 !strncasecmp("konqueror", name
, len
));
202 static void do_add_man_viewer_info(const char *name
,
206 struct man_viewer_info_list
*new = zalloc(sizeof(*new) + len
+ 1);
208 strncpy(new->name
, name
, len
);
209 new->info
= strdup(value
);
210 new->next
= man_viewer_info_list
;
211 man_viewer_info_list
= new;
214 static void unsupported_man_viewer(const char *name
, const char *var
)
216 pr_warning("'%s': path for unsupported man viewer.\n"
217 "Please consider using 'man.<tool>.%s' instead.", name
, var
);
220 static int add_man_viewer_path(const char *name
,
224 if (supported_man_viewer(name
, len
))
225 do_add_man_viewer_info(name
, len
, value
);
227 unsupported_man_viewer(name
, "cmd");
232 static int add_man_viewer_cmd(const char *name
,
236 if (supported_man_viewer(name
, len
))
237 unsupported_man_viewer(name
, "path");
239 do_add_man_viewer_info(name
, len
, value
);
244 static int add_man_viewer_info(const char *var
, const char *value
)
246 const char *name
= var
+ 4;
247 const char *subkey
= strrchr(name
, '.');
250 pr_err("Config with no key for man viewer: %s", name
);
254 if (!strcmp(subkey
, ".path")) {
256 return config_error_nonbool(var
);
257 return add_man_viewer_path(name
, subkey
- name
, value
);
259 if (!strcmp(subkey
, ".cmd")) {
261 return config_error_nonbool(var
);
262 return add_man_viewer_cmd(name
, subkey
- name
, value
);
265 pr_warning("'%s': unsupported man viewer sub key.", subkey
);
269 static int perf_help_config(const char *var
, const char *value
, void *cb
)
271 enum help_format
*help_formatp
= cb
;
273 if (!strcmp(var
, "help.format")) {
275 return config_error_nonbool(var
);
276 *help_formatp
= parse_help_format(value
);
277 if (*help_formatp
== HELP_FORMAT_NONE
)
281 if (!strcmp(var
, "man.viewer")) {
283 return config_error_nonbool(var
);
284 add_man_viewer(value
);
287 if (strstarts(var
, "man."))
288 return add_man_viewer_info(var
, value
);
293 static struct cmdnames main_cmds
, other_cmds
;
295 void list_common_cmds_help(void)
297 unsigned int i
, longest
= 0;
299 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
300 if (longest
< strlen(common_cmds
[i
].name
))
301 longest
= strlen(common_cmds
[i
].name
);
304 puts(" The most commonly used perf commands are:");
305 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
306 printf(" %-*s ", longest
, common_cmds
[i
].name
);
307 puts(common_cmds
[i
].help
);
311 static const char *cmd_to_page(const char *perf_cmd
)
317 else if (strstarts(perf_cmd
, "perf"))
320 return asprintf(&s
, "perf-%s", perf_cmd
) < 0 ? NULL
: s
;
323 static void setup_man_path(void)
326 const char *old_path
= getenv("MANPATH");
328 /* We should always put ':' after our path. If there is no
329 * old_path, the ':' at the end will let 'man' to try
330 * system-wide paths after ours to find the manual page. If
331 * there is old_path, we need ':' as delimiter. */
332 if (asprintf(&new_path
, "%s:%s", system_path(PERF_MAN_PATH
), old_path
?: "") > 0) {
333 setenv("MANPATH", new_path
, 1);
336 pr_err("Unable to setup man path");
340 static void exec_viewer(const char *name
, const char *page
)
342 const char *info
= get_man_viewer_info(name
);
344 if (!strcasecmp(name
, "man"))
345 exec_man_man(info
, page
);
346 else if (!strcasecmp(name
, "woman"))
347 exec_woman_emacs(info
, page
);
348 else if (!strcasecmp(name
, "konqueror"))
349 exec_man_konqueror(info
, page
);
351 exec_man_cmd(info
, page
);
353 pr_warning("'%s': unknown man viewer.", name
);
356 static int show_man_page(const char *perf_cmd
)
358 struct man_viewer_list
*viewer
;
359 const char *page
= cmd_to_page(perf_cmd
);
360 const char *fallback
= getenv("PERF_MAN_VIEWER");
363 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
364 exec_viewer(viewer
->name
, page
); /* will return when unable */
367 exec_viewer(fallback
, page
);
368 exec_viewer("man", page
);
370 pr_err("no man viewer handled the request");
374 static int show_info_page(const char *perf_cmd
)
376 const char *page
= cmd_to_page(perf_cmd
);
377 setenv("INFOPATH", system_path(PERF_INFO_PATH
), 1);
378 execlp("info", "info", "perfman", page
, NULL
);
382 static int get_html_page_path(char **page_path
, const char *page
)
385 const char *html_path
= system_path(PERF_HTML_PATH
);
387 /* Check that we have a perf documentation directory. */
388 if (stat(mkpath("%s/perf.html", html_path
), &st
)
389 || !S_ISREG(st
.st_mode
)) {
390 pr_err("'%s': not a documentation directory.", html_path
);
394 return asprintf(page_path
, "%s/%s.html", html_path
, page
);
398 * If open_html is not defined in a platform-specific way (see for
399 * example compat/mingw.h), we use the script web--browse to display
403 static void open_html(const char *path
)
405 execl_cmd("web--browse", "-c", "help.browser", path
, NULL
);
409 static int show_html_page(const char *perf_cmd
)
411 const char *page
= cmd_to_page(perf_cmd
);
412 char *page_path
; /* it leaks but we exec bellow */
414 if (get_html_page_path(&page_path
, page
) < 0)
417 open_html(page_path
);
422 int cmd_help(int argc
, const char **argv
)
424 bool show_all
= false;
425 enum help_format help_format
= HELP_FORMAT_MAN
;
426 struct option builtin_help_options
[] = {
427 OPT_BOOLEAN('a', "all", &show_all
, "print all available commands"),
428 OPT_SET_UINT('m', "man", &help_format
, "show man page", HELP_FORMAT_MAN
),
429 OPT_SET_UINT('w', "web", &help_format
, "show manual in web browser",
431 OPT_SET_UINT('i', "info", &help_format
, "show info page",
435 const char * const builtin_help_subcommands
[] = {
436 "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
437 "record", "report", "bench", "stat", "timechart", "top", "annotate",
438 "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
439 #ifdef HAVE_LIBELF_SUPPORT
442 #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
446 const char *builtin_help_usage
[] = {
447 "perf help [--all] [--man|--web|--info] [command]",
452 load_command_list("perf-", &main_cmds
, &other_cmds
);
454 rc
= perf_config(perf_help_config
, &help_format
);
458 argc
= parse_options_subcommand(argc
, argv
, builtin_help_options
,
459 builtin_help_subcommands
, builtin_help_usage
, 0);
462 printf("\n Usage: %s\n\n", perf_usage_string
);
463 list_commands("perf commands", &main_cmds
, &other_cmds
);
464 printf(" %s\n\n", perf_more_info_string
);
469 printf("\n usage: %s\n\n", perf_usage_string
);
470 list_common_cmds_help();
471 printf("\n %s\n\n", perf_more_info_string
);
475 switch (help_format
) {
476 case HELP_FORMAT_MAN
:
477 rc
= show_man_page(argv
[0]);
479 case HELP_FORMAT_INFO
:
480 rc
= show_info_page(argv
[0]);
482 case HELP_FORMAT_WEB
:
483 rc
= show_html_page(argv
[0]);
485 case HELP_FORMAT_NONE
: