builtin-help.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * builtin-help.c
  4. *
  5. * Builtin help command
  6. */
  7. #include "util/cache.h"
  8. #include "util/config.h"
  9. #include "util/strbuf.h"
  10. #include "builtin.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>
  21. #include <errno.h>
  22. #include <limits.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. static struct man_viewer_list {
  30. struct man_viewer_list *next;
  31. char name[0];
  32. } *man_viewer_list;
  33. static struct man_viewer_info_list {
  34. struct man_viewer_info_list *next;
  35. const char *info;
  36. char name[0];
  37. } *man_viewer_info_list;
  38. enum help_format {
  39. HELP_FORMAT_NONE,
  40. HELP_FORMAT_MAN,
  41. HELP_FORMAT_INFO,
  42. HELP_FORMAT_WEB,
  43. };
  44. static enum help_format parse_help_format(const char *format)
  45. {
  46. if (!strcmp(format, "man"))
  47. return HELP_FORMAT_MAN;
  48. if (!strcmp(format, "info"))
  49. return HELP_FORMAT_INFO;
  50. if (!strcmp(format, "web") || !strcmp(format, "html"))
  51. return HELP_FORMAT_WEB;
  52. pr_err("unrecognized help format '%s'", format);
  53. return HELP_FORMAT_NONE;
  54. }
  55. static const char *get_man_viewer_info(const char *name)
  56. {
  57. struct man_viewer_info_list *viewer;
  58. for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
  59. if (!strcasecmp(name, viewer->name))
  60. return viewer->info;
  61. }
  62. return NULL;
  63. }
  64. static int check_emacsclient_version(void)
  65. {
  66. struct strbuf buffer = STRBUF_INIT;
  67. struct child_process ec_process;
  68. const char *argv_ec[] = { "emacsclient", "--version", NULL };
  69. int version;
  70. int ret = -1;
  71. /* emacsclient prints its version number on stderr */
  72. memset(&ec_process, 0, sizeof(ec_process));
  73. ec_process.argv = argv_ec;
  74. ec_process.err = -1;
  75. ec_process.stdout_to_stderr = 1;
  76. if (start_command(&ec_process)) {
  77. fprintf(stderr, "Failed to start emacsclient.\n");
  78. return -1;
  79. }
  80. if (strbuf_read(&buffer, ec_process.err, 20) < 0) {
  81. fprintf(stderr, "Failed to read emacsclient version\n");
  82. goto out;
  83. }
  84. close(ec_process.err);
  85. /*
  86. * Don't bother checking return value, because "emacsclient --version"
  87. * seems to always exits with code 1.
  88. */
  89. finish_command(&ec_process);
  90. if (!strstarts(buffer.buf, "emacsclient")) {
  91. fprintf(stderr, "Failed to parse emacsclient version.\n");
  92. goto out;
  93. }
  94. version = atoi(buffer.buf + strlen("emacsclient"));
  95. if (version < 22) {
  96. fprintf(stderr,
  97. "emacsclient version '%d' too old (< 22).\n",
  98. version);
  99. } else
  100. ret = 0;
  101. out:
  102. strbuf_release(&buffer);
  103. return ret;
  104. }
  105. static void exec_failed(const char *cmd)
  106. {
  107. char sbuf[STRERR_BUFSIZE];
  108. pr_warning("failed to exec '%s': %s", cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
  109. }
  110. static void exec_woman_emacs(const char *path, const char *page)
  111. {
  112. if (!check_emacsclient_version()) {
  113. /* This works only with emacsclient version >= 22. */
  114. char *man_page;
  115. if (!path)
  116. path = "emacsclient";
  117. if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
  118. execlp(path, "emacsclient", "-e", man_page, NULL);
  119. free(man_page);
  120. }
  121. exec_failed(path);
  122. }
  123. }
  124. static void exec_man_konqueror(const char *path, const char *page)
  125. {
  126. const char *display = getenv("DISPLAY");
  127. if (display && *display) {
  128. char *man_page;
  129. const char *filename = "kfmclient";
  130. /* It's simpler to launch konqueror using kfmclient. */
  131. if (path) {
  132. const char *file = strrchr(path, '/');
  133. if (file && !strcmp(file + 1, "konqueror")) {
  134. char *new = strdup(path);
  135. char *dest = strrchr(new, '/');
  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. if (asprintf(&man_page, "man:%s(1)", page) > 0) {
  145. execlp(path, filename, "newTab", man_page, NULL);
  146. free(man_page);
  147. }
  148. exec_failed(path);
  149. }
  150. }
  151. static void exec_man_man(const char *path, const char *page)
  152. {
  153. if (!path)
  154. path = "man";
  155. execlp(path, "man", page, NULL);
  156. exec_failed(path);
  157. }
  158. static void exec_man_cmd(const char *cmd, const char *page)
  159. {
  160. char *shell_cmd;
  161. if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
  162. execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
  163. free(shell_cmd);
  164. }
  165. exec_failed(cmd);
  166. }
  167. static void add_man_viewer(const char *name)
  168. {
  169. struct man_viewer_list **p = &man_viewer_list;
  170. size_t len = strlen(name);
  171. while (*p)
  172. p = &((*p)->next);
  173. *p = zalloc(sizeof(**p) + len + 1);
  174. strcpy((*p)->name, name);
  175. }
  176. static int supported_man_viewer(const char *name, size_t len)
  177. {
  178. return (!strncasecmp("man", name, len) ||
  179. !strncasecmp("woman", name, len) ||
  180. !strncasecmp("konqueror", name, len));
  181. }
  182. static void do_add_man_viewer_info(const char *name,
  183. size_t len,
  184. const char *value)
  185. {
  186. struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
  187. strncpy(new->name, name, len);
  188. new->info = strdup(value);
  189. new->next = man_viewer_info_list;
  190. man_viewer_info_list = new;
  191. }
  192. static void unsupported_man_viewer(const char *name, const char *var)
  193. {
  194. pr_warning("'%s': path for unsupported man viewer.\n"
  195. "Please consider using 'man.<tool>.%s' instead.", name, var);
  196. }
  197. static int add_man_viewer_path(const char *name,
  198. size_t len,
  199. const char *value)
  200. {
  201. if (supported_man_viewer(name, len))
  202. do_add_man_viewer_info(name, len, value);
  203. else
  204. unsupported_man_viewer(name, "cmd");
  205. return 0;
  206. }
  207. static int add_man_viewer_cmd(const char *name,
  208. size_t len,
  209. const char *value)
  210. {
  211. if (supported_man_viewer(name, len))
  212. unsupported_man_viewer(name, "path");
  213. else
  214. do_add_man_viewer_info(name, len, value);
  215. return 0;
  216. }
  217. static int add_man_viewer_info(const char *var, const char *value)
  218. {
  219. const char *name = var + 4;
  220. const char *subkey = strrchr(name, '.');
  221. if (!subkey) {
  222. pr_err("Config with no key for man viewer: %s", name);
  223. return -1;
  224. }
  225. if (!strcmp(subkey, ".path")) {
  226. if (!value)
  227. return config_error_nonbool(var);
  228. return add_man_viewer_path(name, subkey - name, value);
  229. }
  230. if (!strcmp(subkey, ".cmd")) {
  231. if (!value)
  232. return config_error_nonbool(var);
  233. return add_man_viewer_cmd(name, subkey - name, value);
  234. }
  235. pr_warning("'%s': unsupported man viewer sub key.", subkey);
  236. return 0;
  237. }
  238. static int perf_help_config(const char *var, const char *value, void *cb)
  239. {
  240. enum help_format *help_formatp = cb;
  241. if (!strcmp(var, "help.format")) {
  242. if (!value)
  243. return config_error_nonbool(var);
  244. *help_formatp = parse_help_format(value);
  245. if (*help_formatp == HELP_FORMAT_NONE)
  246. return -1;
  247. return 0;
  248. }
  249. if (!strcmp(var, "man.viewer")) {
  250. if (!value)
  251. return config_error_nonbool(var);
  252. add_man_viewer(value);
  253. return 0;
  254. }
  255. if (strstarts(var, "man."))
  256. return add_man_viewer_info(var, value);
  257. return 0;
  258. }
  259. static struct cmdnames main_cmds, other_cmds;
  260. void list_common_cmds_help(void)
  261. {
  262. unsigned int i, longest = 0;
  263. for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
  264. if (longest < strlen(common_cmds[i].name))
  265. longest = strlen(common_cmds[i].name);
  266. }
  267. puts(" The most commonly used perf commands are:");
  268. for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
  269. printf(" %-*s ", longest, common_cmds[i].name);
  270. puts(common_cmds[i].help);
  271. }
  272. }
  273. static const char *cmd_to_page(const char *perf_cmd)
  274. {
  275. char *s;
  276. if (!perf_cmd)
  277. return "perf";
  278. else if (strstarts(perf_cmd, "perf"))
  279. return perf_cmd;
  280. return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
  281. }
  282. static void setup_man_path(void)
  283. {
  284. char *new_path;
  285. const char *old_path = getenv("MANPATH");
  286. /* We should always put ':' after our path. If there is no
  287. * old_path, the ':' at the end will let 'man' to try
  288. * system-wide paths after ours to find the manual page. If
  289. * there is old_path, we need ':' as delimiter. */
  290. if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
  291. setenv("MANPATH", new_path, 1);
  292. free(new_path);
  293. } else {
  294. pr_err("Unable to setup man path");
  295. }
  296. }
  297. static void exec_viewer(const char *name, const char *page)
  298. {
  299. const char *info = get_man_viewer_info(name);
  300. if (!strcasecmp(name, "man"))
  301. exec_man_man(info, page);
  302. else if (!strcasecmp(name, "woman"))
  303. exec_woman_emacs(info, page);
  304. else if (!strcasecmp(name, "konqueror"))
  305. exec_man_konqueror(info, page);
  306. else if (info)
  307. exec_man_cmd(info, page);
  308. else
  309. pr_warning("'%s': unknown man viewer.", name);
  310. }
  311. static int show_man_page(const char *perf_cmd)
  312. {
  313. struct man_viewer_list *viewer;
  314. const char *page = cmd_to_page(perf_cmd);
  315. const char *fallback = getenv("PERF_MAN_VIEWER");
  316. setup_man_path();
  317. for (viewer = man_viewer_list; viewer; viewer = viewer->next)
  318. exec_viewer(viewer->name, page); /* will return when unable */
  319. if (fallback)
  320. exec_viewer(fallback, page);
  321. exec_viewer("man", page);
  322. pr_err("no man viewer handled the request");
  323. return -1;
  324. }
  325. static int show_info_page(const char *perf_cmd)
  326. {
  327. const char *page = cmd_to_page(perf_cmd);
  328. setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
  329. execlp("info", "info", "perfman", page, NULL);
  330. return -1;
  331. }
  332. static int get_html_page_path(char **page_path, const char *page)
  333. {
  334. struct stat st;
  335. const char *html_path = system_path(PERF_HTML_PATH);
  336. char path[PATH_MAX];
  337. /* Check that we have a perf documentation directory. */
  338. if (stat(mkpath(path, sizeof(path), "%s/perf.html", html_path), &st)
  339. || !S_ISREG(st.st_mode)) {
  340. pr_err("'%s': not a documentation directory.", html_path);
  341. return -1;
  342. }
  343. return asprintf(page_path, "%s/%s.html", html_path, page);
  344. }
  345. /*
  346. * If open_html is not defined in a platform-specific way (see for
  347. * example compat/mingw.h), we use the script web--browse to display
  348. * HTML.
  349. */
  350. #ifndef open_html
  351. static void open_html(const char *path)
  352. {
  353. execl_cmd("web--browse", "-c", "help.browser", path, NULL);
  354. }
  355. #endif
  356. static int show_html_page(const char *perf_cmd)
  357. {
  358. const char *page = cmd_to_page(perf_cmd);
  359. char *page_path; /* it leaks but we exec below */
  360. if (get_html_page_path(&page_path, page) < 0)
  361. return -1;
  362. open_html(page_path);
  363. return 0;
  364. }
  365. int cmd_help(int argc, const char **argv)
  366. {
  367. bool show_all = false;
  368. enum help_format help_format = HELP_FORMAT_MAN;
  369. struct option builtin_help_options[] = {
  370. OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
  371. OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
  372. OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
  373. HELP_FORMAT_WEB),
  374. OPT_SET_UINT('i', "info", &help_format, "show info page",
  375. HELP_FORMAT_INFO),
  376. OPT_END(),
  377. };
  378. const char * const builtin_help_subcommands[] = {
  379. "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
  380. "record", "report", "bench", "stat", "timechart", "top", "annotate",
  381. "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
  382. #ifdef HAVE_LIBELF_SUPPORT
  383. "probe",
  384. #endif
  385. #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
  386. "trace",
  387. #endif
  388. NULL };
  389. const char *builtin_help_usage[] = {
  390. "perf help [--all] [--man|--web|--info] [command]",
  391. NULL
  392. };
  393. int rc;
  394. load_command_list("perf-", &main_cmds, &other_cmds);
  395. rc = perf_config(perf_help_config, &help_format);
  396. if (rc)
  397. return rc;
  398. argc = parse_options_subcommand(argc, argv, builtin_help_options,
  399. builtin_help_subcommands, builtin_help_usage, 0);
  400. if (show_all) {
  401. printf("\n Usage: %s\n\n", perf_usage_string);
  402. list_commands("perf commands", &main_cmds, &other_cmds);
  403. printf(" %s\n\n", perf_more_info_string);
  404. return 0;
  405. }
  406. if (!argv[0]) {
  407. printf("\n usage: %s\n\n", perf_usage_string);
  408. list_common_cmds_help();
  409. printf("\n %s\n\n", perf_more_info_string);
  410. return 0;
  411. }
  412. switch (help_format) {
  413. case HELP_FORMAT_MAN:
  414. rc = show_man_page(argv[0]);
  415. break;
  416. case HELP_FORMAT_INFO:
  417. rc = show_info_page(argv[0]);
  418. break;
  419. case HELP_FORMAT_WEB:
  420. rc = show_html_page(argv[0]);
  421. break;
  422. case HELP_FORMAT_NONE:
  423. /* fall-through */
  424. default:
  425. rc = -1;
  426. break;
  427. }
  428. return rc;
  429. }