builtin-help.c 14 KB

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