builtin-help.c 12 KB

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