From 4ba92f185fb1d0b112cbc804a261939f5f81dc34 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Tue, 17 Nov 2015 01:20:34 -0500 Subject: [PATCH] lttng: show man page when using command's --help MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- src/bin/lttng/command.h | 10 +++++++ src/bin/lttng/commands/add_context.c | 2 +- src/bin/lttng/commands/calibrate.c | 2 +- src/bin/lttng/commands/create.c | 2 +- src/bin/lttng/commands/destroy.c | 2 +- src/bin/lttng/commands/disable_channels.c | 2 +- src/bin/lttng/commands/disable_events.c | 2 +- src/bin/lttng/commands/enable_channels.c | 2 +- src/bin/lttng/commands/enable_events.c | 2 +- src/bin/lttng/commands/list.c | 2 +- src/bin/lttng/commands/load.c | 2 +- src/bin/lttng/commands/save.c | 2 +- src/bin/lttng/commands/set_session.c | 2 +- src/bin/lttng/commands/snapshot.c | 2 +- src/bin/lttng/commands/start.c | 2 +- src/bin/lttng/commands/status.c | 2 +- src/bin/lttng/commands/stop.c | 2 +- src/bin/lttng/commands/track-untrack.c | 2 +- src/bin/lttng/commands/version.c | 2 +- src/bin/lttng/commands/view.c | 2 +- src/bin/lttng/utils.c | 13 +++++++++ src/bin/lttng/utils.h | 1 + src/common/Makefile.am | 3 +- src/common/defaults.h | 6 ++++ src/common/utils.c | 35 ++++++++++++++++++++++- src/common/utils.h | 1 + 26 files changed, 86 insertions(+), 21 deletions(-) diff --git a/src/bin/lttng/command.h b/src/bin/lttng/command.h index 451e30f79..05c51663b 100644 --- a/src/bin/lttng/command.h +++ b/src/bin/lttng/command.h @@ -28,6 +28,16 @@ #define DECL_COMMAND(_name) \ extern int cmd_##_name(int, const char **) +#define SHOW_HELP() \ + do { \ + ret = show_cmd_man_page(argv[0]); \ + \ + if (ret) { \ + ERR("Cannot view man page lttng-%s(1)", argv[0]); \ + perror("exec"); \ + } \ + } while (0) + enum cmd_error_code { CMD_SUCCESS = 0, CMD_ERROR, diff --git a/src/bin/lttng/commands/add_context.c b/src/bin/lttng/commands/add_context.c index 6f7c23c01..56ee2aca4 100644 --- a/src/bin/lttng/commands/add_context.c +++ b/src/bin/lttng/commands/add_context.c @@ -841,7 +841,7 @@ int cmd_add_context(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST: print_ctx_type(stdout); diff --git a/src/bin/lttng/commands/calibrate.c b/src/bin/lttng/commands/calibrate.c index b15ce40cf..ba3350fb6 100644 --- a/src/bin/lttng/commands/calibrate.c +++ b/src/bin/lttng/commands/calibrate.c @@ -172,7 +172,7 @@ int cmd_calibrate(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_TRACEPOINT: ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/create.c b/src/bin/lttng/commands/create.c index 5027fce90..efc9e60c1 100644 --- a/src/bin/lttng/commands/create.c +++ b/src/bin/lttng/commands/create.c @@ -699,7 +699,7 @@ int cmd_create(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); diff --git a/src/bin/lttng/commands/destroy.c b/src/bin/lttng/commands/destroy.c index ad181d7c4..661c0a235 100644 --- a/src/bin/lttng/commands/destroy.c +++ b/src/bin/lttng/commands/destroy.c @@ -189,7 +189,7 @@ int cmd_destroy(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); break; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); diff --git a/src/bin/lttng/commands/disable_channels.c b/src/bin/lttng/commands/disable_channels.c index f1f2e51b9..a8bfa5dea 100644 --- a/src/bin/lttng/commands/disable_channels.c +++ b/src/bin/lttng/commands/disable_channels.c @@ -233,7 +233,7 @@ int cmd_disable_channels(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_USERSPACE: opt_userspace = 1; diff --git a/src/bin/lttng/commands/disable_events.c b/src/bin/lttng/commands/disable_events.c index cc2711565..b853c57f7 100644 --- a/src/bin/lttng/commands/disable_events.c +++ b/src/bin/lttng/commands/disable_events.c @@ -361,7 +361,7 @@ int cmd_disable_events(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_TYPE_SYSCALL: opt_event_type = LTTNG_EVENT_SYSCALL; diff --git a/src/bin/lttng/commands/enable_channels.c b/src/bin/lttng/commands/enable_channels.c index 696b8e094..93d81d735 100644 --- a/src/bin/lttng/commands/enable_channels.c +++ b/src/bin/lttng/commands/enable_channels.c @@ -401,7 +401,7 @@ int cmd_enable_channels(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_DISCARD: chan.attr.overwrite = 0; diff --git a/src/bin/lttng/commands/enable_events.c b/src/bin/lttng/commands/enable_events.c index 6a5369766..2c366c6d2 100644 --- a/src/bin/lttng/commands/enable_events.c +++ b/src/bin/lttng/commands/enable_events.c @@ -1401,7 +1401,7 @@ int cmd_enable_events(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_TRACEPOINT: opt_event_type = LTTNG_EVENT_TRACEPOINT; diff --git a/src/bin/lttng/commands/list.c b/src/bin/lttng/commands/list.c index b122b5b78..7a35258b4 100644 --- a/src/bin/lttng/commands/list.c +++ b/src/bin/lttng/commands/list.c @@ -1737,7 +1737,7 @@ int cmd_list(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_USERSPACE: opt_userspace = 1; diff --git a/src/bin/lttng/commands/load.c b/src/bin/lttng/commands/load.c index cb3eb75aa..7a29da52d 100644 --- a/src/bin/lttng/commands/load.c +++ b/src/bin/lttng/commands/load.c @@ -151,7 +151,7 @@ int cmd_load(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_ALL: opt_load_all = 1; diff --git a/src/bin/lttng/commands/save.c b/src/bin/lttng/commands/save.c index 6bd873226..66ec5b6eb 100644 --- a/src/bin/lttng/commands/save.c +++ b/src/bin/lttng/commands/save.c @@ -146,7 +146,7 @@ int cmd_save(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_ALL: opt_save_all = 1; diff --git a/src/bin/lttng/commands/set_session.c b/src/bin/lttng/commands/set_session.c index c0572e127..d41dc1d19 100644 --- a/src/bin/lttng/commands/set_session.c +++ b/src/bin/lttng/commands/set_session.c @@ -174,7 +174,7 @@ int cmd_set_session(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); diff --git a/src/bin/lttng/commands/snapshot.c b/src/bin/lttng/commands/snapshot.c index 465d864de..1d7843954 100644 --- a/src/bin/lttng/commands/snapshot.c +++ b/src/bin/lttng/commands/snapshot.c @@ -715,7 +715,7 @@ int cmd_snapshot(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, snapshot_opts); diff --git a/src/bin/lttng/commands/start.c b/src/bin/lttng/commands/start.c index b353eb6e3..90794c669 100644 --- a/src/bin/lttng/commands/start.c +++ b/src/bin/lttng/commands/start.c @@ -161,7 +161,7 @@ int cmd_start(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); diff --git a/src/bin/lttng/commands/status.c b/src/bin/lttng/commands/status.c index d9b6c4f27..693527535 100644 --- a/src/bin/lttng/commands/status.c +++ b/src/bin/lttng/commands/status.c @@ -83,7 +83,7 @@ int cmd_status(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); diff --git a/src/bin/lttng/commands/stop.c b/src/bin/lttng/commands/stop.c index b3fff0753..e946d4fdb 100644 --- a/src/bin/lttng/commands/stop.c +++ b/src/bin/lttng/commands/stop.c @@ -189,7 +189,7 @@ int cmd_stop(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); diff --git a/src/bin/lttng/commands/track-untrack.c b/src/bin/lttng/commands/track-untrack.c index a4f4689e7..6d8d91ffd 100644 --- a/src/bin/lttng/commands/track-untrack.c +++ b/src/bin/lttng/commands/track-untrack.c @@ -364,7 +364,7 @@ int cmd_track_untrack(enum cmd_type cmd_type, const char *cmd_str, while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout, cmd_str); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); diff --git a/src/bin/lttng/commands/version.c b/src/bin/lttng/commands/version.c index 7634f5871..3630950bb 100644 --- a/src/bin/lttng/commands/version.c +++ b/src/bin/lttng/commands/version.c @@ -148,7 +148,7 @@ int cmd_version(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); diff --git a/src/bin/lttng/commands/view.c b/src/bin/lttng/commands/view.c index 44e0cb015..dac2c3e24 100644 --- a/src/bin/lttng/commands/view.c +++ b/src/bin/lttng/commands/view.c @@ -444,7 +444,7 @@ int cmd_view(int argc, const char **argv) while ((opt = poptGetNextOpt(pc)) != -1) { switch (opt) { case OPT_HELP: - usage(stdout); + SHOW_HELP(); goto end; case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); diff --git a/src/bin/lttng/utils.c b/src/bin/lttng/utils.c index 802e0971b..912cbd577 100644 --- a/src/bin/lttng/utils.c +++ b/src/bin/lttng/utils.c @@ -26,9 +26,11 @@ #include #include #include +#include #include #include +#include #include "conf.h" #include "utils.h" @@ -479,3 +481,14 @@ void print_session_stats(const char *session_name) end: return; } + +int show_cmd_man_page(const char *cmd_name) +{ + int ret; + char page_name[32]; + + ret = sprintf(page_name, "lttng-%s", cmd_name); + assert(ret > 0 && ret < 32); + + return utils_show_man_page(1, page_name); +} diff --git a/src/bin/lttng/utils.h b/src/bin/lttng/utils.h index 0d70cf751..7f656df13 100644 --- a/src/bin/lttng/utils.h +++ b/src/bin/lttng/utils.h @@ -60,5 +60,6 @@ int print_missing_or_multiple_domains(unsigned int sum); int spawn_relayd(const char *pathname, int port); int check_relayd(void); void print_session_stats(const char *session_name); +int show_cmd_man_page(const char *cmd_name); #endif /* _LTTNG_UTILS_H */ diff --git a/src/common/Makefile.am b/src/common/Makefile.am index c1b9d4e7a..5096676e0 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -1,4 +1,5 @@ -AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src \ + -DMANPATH=\""$(mandir)"\" AUTOMAKE_OPTIONS = subdir-objects diff --git a/src/common/defaults.h b/src/common/defaults.h index f45c8f6f0..6e343b50c 100644 --- a/src/common/defaults.h +++ b/src/common/defaults.h @@ -35,6 +35,12 @@ /* Environment variable to set session daemon binary path. */ #define DEFAULT_SESSIOND_PATH_ENV "LTTNG_SESSIOND_PATH" +/* Environment variable to set man pager binary path. */ +#define DEFAULT_MAN_BIN_PATH_ENV "LTTNG_MAN_BIN_PATH" + +/* Default man pager binary path. */ +#define DEFAULT_MAN_BIN_PATH "/usr/bin/man" + /* Default trace output directory name */ #define DEFAULT_TRACE_DIR_NAME "lttng-traces" diff --git a/src/common/utils.c b/src/common/utils.c index d71d92446..df55dc9fb 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -1341,7 +1341,40 @@ int utils_truncate_stream_file(int fd, off_t length) PERROR("lseek"); goto end; } - end: return ret; } + +static const char *get_man_bin_path(void) +{ + char *env_man_path = getenv(DEFAULT_MAN_BIN_PATH_ENV); + + if (env_man_path) { + return env_man_path; + } + + return DEFAULT_MAN_BIN_PATH; +} + +LTTNG_HIDDEN +int utils_show_man_page(int section, const char *page_name) +{ + char section_string[8]; + const char *man_bin_path = get_man_bin_path(); + int ret; + + /* Section integer -> section string */ + ret = sprintf(section_string, "%d", section); + assert(ret > 0 && ret < 8); + + /* + * Execute man pager. + * + * We provide --manpath to man here because LTTng-tools can + * be installed outside /usr, in which case its man pages are + * not located in the default /usr/share/man directory. + */ + ret = execlp(man_bin_path, "man", "--manpath", MANPATH, + section_string, page_name, NULL); + return ret; +} diff --git a/src/common/utils.h b/src/common/utils.h index 5272d54a6..7285f5c30 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -59,5 +59,6 @@ char *utils_generate_optstring(const struct option *long_options, int utils_create_lock_file(const char *filepath); int utils_recursive_rmdir(const char *path); int utils_truncate_stream_file(int fd, off_t length); +int utils_show_man_page(int section, const char *page_name); #endif /* _COMMON_UTILS_H */ -- 2.34.1