builtin-help.c 11 KB

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