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 <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/zalloc.h>
24 #include <sys/types.h>
28 static struct man_viewer_list
{
29 struct man_viewer_list
*next
;
33 static struct man_viewer_info_list
{
34 struct man_viewer_info_list
*next
;
37 } *man_viewer_info_list
;
46 static enum help_format
parse_help_format(const char *format
)
48 if (!strcmp(format
, "man"))
49 return HELP_FORMAT_MAN
;
50 if (!strcmp(format
, "info"))
51 return HELP_FORMAT_INFO
;
52 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
53 return HELP_FORMAT_WEB
;
55 pr_err("unrecognized help format '%s'", format
);
56 return HELP_FORMAT_NONE
;
59 static const char *get_man_viewer_info(const char *name
)
61 struct man_viewer_info_list
*viewer
;
63 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
) {
64 if (!strcasecmp(name
, viewer
->name
))
70 static int check_emacsclient_version(void)
72 struct strbuf buffer
= STRBUF_INIT
;
73 struct child_process ec_process
;
74 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
78 /* emacsclient prints its version number on stderr */
79 memset(&ec_process
, 0, sizeof(ec_process
));
80 ec_process
.argv
= argv_ec
;
82 ec_process
.stdout_to_stderr
= 1;
83 if (start_command(&ec_process
)) {
84 fprintf(stderr
, "Failed to start emacsclient.\n");
87 if (strbuf_read(&buffer
, ec_process
.err
, 20) < 0) {
88 fprintf(stderr
, "Failed to read emacsclient version\n");
91 close(ec_process
.err
);
94 * Don't bother checking return value, because "emacsclient --version"
95 * seems to always exits with code 1.
97 finish_command(&ec_process
);
99 if (!strstarts(buffer
.buf
, "emacsclient")) {
100 fprintf(stderr
, "Failed to parse emacsclient version.\n");
104 version
= atoi(buffer
.buf
+ strlen("emacsclient"));
108 "emacsclient version '%d' too old (< 22).\n",
113 strbuf_release(&buffer
);
117 static void exec_failed(const char *cmd
)
119 char sbuf
[STRERR_BUFSIZE
];
120 pr_warning("failed to exec '%s': %s", cmd
, str_error_r(errno
, sbuf
, sizeof(sbuf
)));
123 static void exec_woman_emacs(const char *path
, const char *page
)
125 if (!check_emacsclient_version()) {
126 /* This works only with emacsclient version >= 22. */
130 path
= "emacsclient";
131 if (asprintf(&man_page
, "(woman \"%s\")", page
) > 0) {
132 execlp(path
, "emacsclient", "-e", man_page
, NULL
);
139 static void exec_man_konqueror(const char *path
, const char *page
)
141 const char *display
= getenv("DISPLAY");
143 if (display
&& *display
) {
145 const char *filename
= "kfmclient";
147 /* It's simpler to launch konqueror using kfmclient. */
149 const char *file
= strrchr(path
, '/');
150 if (file
&& !strcmp(file
+ 1, "konqueror")) {
151 char *new = strdup(path
);
152 char *dest
= strrchr(new, '/');
154 /* strlen("konqueror") == strlen("kfmclient") */
155 strcpy(dest
+ 1, "kfmclient");
162 if (asprintf(&man_page
, "man:%s(1)", page
) > 0) {
163 execlp(path
, filename
, "newTab", man_page
, NULL
);
170 static void exec_man_man(const char *path
, const char *page
)
174 execlp(path
, "man", page
, NULL
);
178 static void exec_man_cmd(const char *cmd
, const char *page
)
182 if (asprintf(&shell_cmd
, "%s %s", cmd
, page
) > 0) {
183 execl("/bin/sh", "sh", "-c", shell_cmd
, NULL
);
189 static void add_man_viewer(const char *name
)
191 struct man_viewer_list
**p
= &man_viewer_list
;
192 size_t len
= strlen(name
);
196 *p
= zalloc(sizeof(**p
) + len
+ 1);
197 strcpy((*p
)->name
, name
);
200 static int supported_man_viewer(const char *name
, size_t len
)
202 return (!strncasecmp("man", name
, len
) ||
203 !strncasecmp("woman", name
, len
) ||
204 !strncasecmp("konqueror", name
, len
));
207 static void do_add_man_viewer_info(const char *name
,
211 struct man_viewer_info_list
*new = zalloc(sizeof(*new) + len
+ 1);
213 strncpy(new->name
, name
, len
);
214 new->info
= strdup(value
);
215 new->next
= man_viewer_info_list
;
216 man_viewer_info_list
= new;
219 static void unsupported_man_viewer(const char *name
, const char *var
)
221 pr_warning("'%s': path for unsupported man viewer.\n"
222 "Please consider using 'man.<tool>.%s' instead.", name
, var
);
225 static int add_man_viewer_path(const char *name
,
229 if (supported_man_viewer(name
, len
))
230 do_add_man_viewer_info(name
, len
, value
);
232 unsupported_man_viewer(name
, "cmd");
237 static int add_man_viewer_cmd(const char *name
,
241 if (supported_man_viewer(name
, len
))
242 unsupported_man_viewer(name
, "path");
244 do_add_man_viewer_info(name
, len
, value
);
249 static int add_man_viewer_info(const char *var
, const char *value
)
251 const char *name
= var
+ 4;
252 const char *subkey
= strrchr(name
, '.');
255 pr_err("Config with no key for man viewer: %s", name
);
259 if (!strcmp(subkey
, ".path")) {
261 return config_error_nonbool(var
);
262 return add_man_viewer_path(name
, subkey
- name
, value
);
264 if (!strcmp(subkey
, ".cmd")) {
266 return config_error_nonbool(var
);
267 return add_man_viewer_cmd(name
, subkey
- name
, value
);
270 pr_warning("'%s': unsupported man viewer sub key.", subkey
);
274 static int perf_help_config(const char *var
, const char *value
, void *cb
)
276 enum help_format
*help_formatp
= cb
;
278 if (!strcmp(var
, "help.format")) {
280 return config_error_nonbool(var
);
281 *help_formatp
= parse_help_format(value
);
282 if (*help_formatp
== HELP_FORMAT_NONE
)
286 if (!strcmp(var
, "man.viewer")) {
288 return config_error_nonbool(var
);
289 add_man_viewer(value
);
292 if (strstarts(var
, "man."))
293 return add_man_viewer_info(var
, value
);
298 static struct cmdnames main_cmds
, other_cmds
;
300 void list_common_cmds_help(void)
302 unsigned int i
, longest
= 0;
304 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
305 if (longest
< strlen(common_cmds
[i
].name
))
306 longest
= strlen(common_cmds
[i
].name
);
309 puts(" The most commonly used perf commands are:");
310 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
311 printf(" %-*s ", longest
, common_cmds
[i
].name
);
312 puts(common_cmds
[i
].help
);
316 static const char *cmd_to_page(const char *perf_cmd
)
322 else if (strstarts(perf_cmd
, "perf"))
325 return asprintf(&s
, "perf-%s", perf_cmd
) < 0 ? NULL
: s
;
328 static void setup_man_path(void)
331 const char *old_path
= getenv("MANPATH");
333 /* We should always put ':' after our path. If there is no
334 * old_path, the ':' at the end will let 'man' to try
335 * system-wide paths after ours to find the manual page. If
336 * there is old_path, we need ':' as delimiter. */
337 if (asprintf(&new_path
, "%s:%s", system_path(PERF_MAN_PATH
), old_path
?: "") > 0) {
338 setenv("MANPATH", new_path
, 1);
341 pr_err("Unable to setup man path");
345 static void exec_viewer(const char *name
, const char *page
)
347 const char *info
= get_man_viewer_info(name
);
349 if (!strcasecmp(name
, "man"))
350 exec_man_man(info
, page
);
351 else if (!strcasecmp(name
, "woman"))
352 exec_woman_emacs(info
, page
);
353 else if (!strcasecmp(name
, "konqueror"))
354 exec_man_konqueror(info
, page
);
356 exec_man_cmd(info
, page
);
358 pr_warning("'%s': unknown man viewer.", name
);
361 static int show_man_page(const char *perf_cmd
)
363 struct man_viewer_list
*viewer
;
364 const char *page
= cmd_to_page(perf_cmd
);
365 const char *fallback
= getenv("PERF_MAN_VIEWER");
368 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
369 exec_viewer(viewer
->name
, page
); /* will return when unable */
372 exec_viewer(fallback
, page
);
373 exec_viewer("man", page
);
375 pr_err("no man viewer handled the request");
379 static int show_info_page(const char *perf_cmd
)
381 const char *page
= cmd_to_page(perf_cmd
);
382 setenv("INFOPATH", system_path(PERF_INFO_PATH
), 1);
383 execlp("info", "info", "perfman", page
, NULL
);
387 static int get_html_page_path(char **page_path
, const char *page
)
390 const char *html_path
= system_path(PERF_HTML_PATH
);
392 /* Check that we have a perf documentation directory. */
393 if (stat(mkpath("%s/perf.html", html_path
), &st
)
394 || !S_ISREG(st
.st_mode
)) {
395 pr_err("'%s': not a documentation directory.", html_path
);
399 return asprintf(page_path
, "%s/%s.html", html_path
, page
);
403 * If open_html is not defined in a platform-specific way (see for
404 * example compat/mingw.h), we use the script web--browse to display
408 static void open_html(const char *path
)
410 execl_cmd("web--browse", "-c", "help.browser", path
, NULL
);
414 static int show_html_page(const char *perf_cmd
)
416 const char *page
= cmd_to_page(perf_cmd
);
417 char *page_path
; /* it leaks but we exec bellow */
419 if (get_html_page_path(&page_path
, page
) < 0)
422 open_html(page_path
);
427 int cmd_help(int argc
, const char **argv
)
429 bool show_all
= false;
430 enum help_format help_format
= HELP_FORMAT_MAN
;
431 struct option builtin_help_options
[] = {
432 OPT_BOOLEAN('a', "all", &show_all
, "print all available commands"),
433 OPT_SET_UINT('m', "man", &help_format
, "show man page", HELP_FORMAT_MAN
),
434 OPT_SET_UINT('w', "web", &help_format
, "show manual in web browser",
436 OPT_SET_UINT('i', "info", &help_format
, "show info page",
440 const char * const builtin_help_subcommands
[] = {
441 "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
442 "record", "report", "bench", "stat", "timechart", "top", "annotate",
443 "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
444 #ifdef HAVE_LIBELF_SUPPORT
447 #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
451 const char *builtin_help_usage
[] = {
452 "perf help [--all] [--man|--web|--info] [command]",
457 load_command_list("perf-", &main_cmds
, &other_cmds
);
459 rc
= perf_config(perf_help_config
, &help_format
);
463 argc
= parse_options_subcommand(argc
, argv
, builtin_help_options
,
464 builtin_help_subcommands
, builtin_help_usage
, 0);
467 printf("\n Usage: %s\n\n", perf_usage_string
);
468 list_commands("perf commands", &main_cmds
, &other_cmds
);
469 printf(" %s\n\n", perf_more_info_string
);
474 printf("\n usage: %s\n\n", perf_usage_string
);
475 list_common_cmds_help();
476 printf("\n %s\n\n", perf_more_info_string
);
480 switch (help_format
) {
481 case HELP_FORMAT_MAN
:
482 rc
= show_man_page(argv
[0]);
484 case HELP_FORMAT_INFO
:
485 rc
= show_info_page(argv
[0]);
487 case HELP_FORMAT_WEB
:
488 rc
= show_html_page(argv
[0]);
490 case HELP_FORMAT_NONE
: