builtin-help.c 12 KB

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