builtin-help.c 12 KB

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