From: Simon Marchi Date: Tue, 24 Jan 2012 16:28:04 +0000 (-0500) Subject: Add a '--list-options' option to each command. X-Git-Tag: v2.0-pre19~60 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=679b4943c3b0f451e7f4fbcd804dd8a7a679e253;p=lttng-tools.git Add a '--list-options' option to each command. This is intended to be used for programmable Bash completion. Modified from previous version: - Changed '\0' to 0 to match current coding style. Changes made by David Goulet: - Check args for --list-options in lttng cli to stop auto execution of the session daemon. - Adds --list-options to all help. (Hidding command is bad :P) - Fix uninitialized ret value in enable channel Signed-off-by: Simon Marchi Acked-by: Mathieu Desnoyers Signed-off-by: David Goulet --- diff --git a/src/bin/lttng/commands/add_context.c b/src/bin/lttng/commands/add_context.c index 260c300be..82abc7333 100644 --- a/src/bin/lttng/commands/add_context.c +++ b/src/bin/lttng/commands/add_context.c @@ -48,6 +48,7 @@ enum { OPT_HELP = 1, OPT_TYPE, OPT_USERSPACE, + OPT_LIST_OPTIONS, }; static struct lttng_handle *handle; @@ -149,6 +150,7 @@ static struct poptOption long_options[] = { {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, #endif {"type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -307,6 +309,7 @@ static void usage(FILE *ofp) fprintf(ofp, "\n"); fprintf(ofp, "Options:\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, " -s, --session Apply on session name\n"); fprintf(ofp, " -c, --channel NAME Apply on channel\n"); fprintf(ofp, " -e, --event NAME Apply on event\n"); @@ -463,6 +466,10 @@ int cmd_add_context(int argc, const char **argv) opt_cmd_name = poptGetOptArg(pc); #endif break; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/calibrate.c b/src/bin/lttng/commands/calibrate.c index a0e852f8b..81eadbfd2 100644 --- a/src/bin/lttng/commands/calibrate.c +++ b/src/bin/lttng/commands/calibrate.c @@ -48,6 +48,7 @@ enum { OPT_FUNCTION_ENTRY, OPT_SYSCALL, OPT_USERSPACE, + OPT_LIST_OPTIONS, }; static struct lttng_handle *handle; @@ -75,6 +76,7 @@ static struct poptOption long_options[] = { {"function:entry", 0, POPT_ARG_NONE, 0, OPT_FUNCTION_ENTRY, 0, 0}, #endif {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -86,6 +88,7 @@ static void usage(FILE *ofp) fprintf(ofp, "usage: lttng calibrate [options] [calibrate_options]\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n"); #if 0 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n"); @@ -217,6 +220,10 @@ int cmd_calibrate(int argc, const char **argv) case OPT_USERSPACE: opt_userspace = 1; break; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/create.c b/src/bin/lttng/commands/create.c index 835d82a4f..a791fbd4d 100644 --- a/src/bin/lttng/commands/create.c +++ b/src/bin/lttng/commands/create.c @@ -27,18 +27,21 @@ #include #include "../command.h" +#include "../utils.h" static char *opt_output_path; static char *opt_session_name; enum { OPT_HELP = 1, + OPT_LIST_OPTIONS, }; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ - {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, - {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, 0, 0}, + {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL}, + {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -50,6 +53,7 @@ static void usage(FILE *ofp) fprintf(ofp, "usage: lttng create [options] [NAME]\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, " -o, --output PATH Specify output path for traces\n"); fprintf(ofp, "\n"); } @@ -160,6 +164,10 @@ int cmd_create(int argc, const char **argv) case OPT_HELP: usage(stderr); goto end; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/destroy.c b/src/bin/lttng/commands/destroy.c index b79315958..39b4e9a2f 100644 --- a/src/bin/lttng/commands/destroy.c +++ b/src/bin/lttng/commands/destroy.c @@ -31,11 +31,13 @@ static char *opt_session_name; enum { OPT_HELP = 1, + OPT_LIST_OPTIONS, }; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -50,6 +52,7 @@ static void usage(FILE *ofp) fprintf(ofp, "get it from the configuration directory (.lttng).\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, "\n"); } @@ -116,6 +119,10 @@ int cmd_destroy(int argc, const char **argv) case OPT_HELP: usage(stderr); goto end; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/disable_channels.c b/src/bin/lttng/commands/disable_channels.c index f8ed51759..55564bc3d 100644 --- a/src/bin/lttng/commands/disable_channels.c +++ b/src/bin/lttng/commands/disable_channels.c @@ -40,6 +40,7 @@ static pid_t opt_pid; enum { OPT_HELP = 1, OPT_USERSPACE, + OPT_LIST_OPTIONS, }; static struct lttng_handle *handle; @@ -56,6 +57,7 @@ static struct poptOption long_options[] = { #else {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, #endif + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -67,6 +69,7 @@ static void usage(FILE *ofp) fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] [options]\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, " -s, --session Apply on session name\n"); fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n"); #if 0 @@ -153,6 +156,10 @@ int cmd_disable_channels(int argc, const char **argv) case OPT_USERSPACE: opt_userspace = 1; break; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/disable_events.c b/src/bin/lttng/commands/disable_events.c index 7446168d2..ff42da0e0 100644 --- a/src/bin/lttng/commands/disable_events.c +++ b/src/bin/lttng/commands/disable_events.c @@ -42,6 +42,7 @@ static pid_t opt_pid; enum { OPT_HELP = 1, OPT_USERSPACE, + OPT_LIST_OPTIONS, }; static struct lttng_handle *handle; @@ -60,6 +61,7 @@ static struct poptOption long_options[] = { #else {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, #endif + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -71,6 +73,7 @@ static void usage(FILE *ofp) fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] [options]\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, " -s, --session Apply on session name\n"); fprintf(ofp, " -c, --channel Apply on this channel\n"); fprintf(ofp, " -a, --all-events Disable all tracepoints\n"); @@ -211,6 +214,10 @@ int cmd_disable_events(int argc, const char **argv) case OPT_USERSPACE: opt_userspace = 1; break; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/enable_channels.c b/src/bin/lttng/commands/enable_channels.c index 5443d78a3..e27fe88ce 100644 --- a/src/bin/lttng/commands/enable_channels.c +++ b/src/bin/lttng/commands/enable_channels.c @@ -48,6 +48,7 @@ enum { OPT_SWITCH_TIMER, OPT_READ_TIMER, OPT_USERSPACE, + OPT_LIST_OPTIONS, }; static struct lttng_handle *handle; @@ -70,6 +71,7 @@ static struct poptOption long_options[] = { {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0}, {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0}, {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -81,6 +83,7 @@ static void usage(FILE *ofp) fprintf(ofp, "usage: lttng enable-channel NAME[,NAME2,...] [options] [channel_options]\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, " -s, --session Apply on session name\n"); fprintf(ofp, " -k, --kernel Apply on the kernel tracer\n"); #if 0 @@ -258,6 +261,10 @@ int cmd_enable_channels(int argc, const char **argv) case OPT_USERSPACE: opt_userspace = 1; break; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/enable_events.c b/src/bin/lttng/commands/enable_events.c index c5a969f20..41a25730c 100644 --- a/src/bin/lttng/commands/enable_events.c +++ b/src/bin/lttng/commands/enable_events.c @@ -54,6 +54,7 @@ enum { OPT_SYSCALL, OPT_USERSPACE, OPT_TRACEPOINT_LOGLEVEL, + OPT_LIST_OPTIONS, }; static struct lttng_handle *handle; @@ -84,6 +85,7 @@ static struct poptOption long_options[] = { #endif {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0}, {"loglevel", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT_LOGLEVEL, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -95,6 +97,7 @@ static void usage(FILE *ofp) fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, " -s, --session Apply on session name\n"); fprintf(ofp, " -c, --channel Apply on this channel\n"); fprintf(ofp, " -a, --all Enable all tracepoints\n"); @@ -432,6 +435,10 @@ int cmd_enable_events(int argc, const char **argv) case OPT_TRACEPOINT_LOGLEVEL: opt_event_type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL; break; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/list.c b/src/bin/lttng/commands/list.c index f09edaef4..743b79ff2 100644 --- a/src/bin/lttng/commands/list.c +++ b/src/bin/lttng/commands/list.c @@ -43,6 +43,7 @@ const char *indent8 = " "; enum { OPT_HELP = 1, OPT_USERSPACE, + OPT_LIST_OPTIONS, }; static struct lttng_handle *handle; @@ -60,6 +61,7 @@ static struct poptOption long_options[] = { #endif {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0}, {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -76,6 +78,7 @@ static void usage(FILE *ofp) fprintf(ofp, "With -u alone, list available userspace events\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, " -k, --kernel Select kernel domain\n"); fprintf(ofp, " -u, --userspace Select user-space domain.\n"); #if 0 @@ -556,6 +559,10 @@ int cmd_list(int argc, const char **argv) case OPT_USERSPACE: opt_userspace = 1; break; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/set_session.c b/src/bin/lttng/commands/set_session.c index bc25b8d1e..f91935e04 100644 --- a/src/bin/lttng/commands/set_session.c +++ b/src/bin/lttng/commands/set_session.c @@ -31,11 +31,13 @@ static char *opt_session_name; enum { OPT_HELP = 1, + OPT_LIST_OPTIONS, }; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -48,6 +50,7 @@ static void usage(FILE *ofp) fprintf(ofp, "\n"); fprintf(ofp, "Options:\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, "\n"); } @@ -89,6 +92,10 @@ int cmd_set_session(int argc, const char **argv) usage(stderr); ret = CMD_SUCCESS; goto end; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/start.c b/src/bin/lttng/commands/start.c index 829278773..b2a498005 100644 --- a/src/bin/lttng/commands/start.c +++ b/src/bin/lttng/commands/start.c @@ -31,11 +31,13 @@ static char *opt_session_name; enum { OPT_HELP = 1, + OPT_LIST_OPTIONS, }; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -50,6 +52,7 @@ static void usage(FILE *ofp) fprintf(ofp, "get it from the configuration directory (.lttng).\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, "\n"); } @@ -109,6 +112,10 @@ int cmd_start(int argc, const char **argv) usage(stderr); ret = CMD_SUCCESS; goto end; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/stop.c b/src/bin/lttng/commands/stop.c index 0a942645d..82c9b4e13 100644 --- a/src/bin/lttng/commands/stop.c +++ b/src/bin/lttng/commands/stop.c @@ -31,11 +31,13 @@ static char *opt_session_name; enum { OPT_HELP = 1, + OPT_LIST_OPTIONS, }; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -50,6 +52,7 @@ static void usage(FILE *ofp) fprintf(ofp, "get it from the configuration directory (.lttng).\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, "\n"); } @@ -106,6 +109,10 @@ int cmd_stop(int argc, const char **argv) usage(stderr); ret = CMD_SUCCESS; goto end; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/commands/version.c b/src/bin/lttng/commands/version.c index c9b547bce..f288ba4aa 100644 --- a/src/bin/lttng/commands/version.c +++ b/src/bin/lttng/commands/version.c @@ -30,11 +30,13 @@ enum { OPT_HELP = 1, + OPT_LIST_OPTIONS, }; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, + {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, {0, 0, 0, 0, 0, 0, 0} }; @@ -46,6 +48,7 @@ static void usage(FILE *ofp) fprintf(ofp, "usage: lttng version\n"); fprintf(ofp, "\n"); fprintf(ofp, " -h, --help Show this help\n"); + fprintf(ofp, " --list-options Simple listing of options\n"); fprintf(ofp, "\n"); } @@ -66,6 +69,10 @@ int cmd_version(int argc, const char **argv) usage(stderr); ret = CMD_SUCCESS; goto end; + case OPT_LIST_OPTIONS: + list_cmd_options(stdout, long_options); + ret = CMD_SUCCESS; + goto end; default: usage(stderr); ret = CMD_UNDEFINED; diff --git a/src/bin/lttng/lttng.c b/src/bin/lttng/lttng.c index 031b015ef..64f3efa44 100644 --- a/src/bin/lttng/lttng.c +++ b/src/bin/lttng/lttng.c @@ -377,15 +377,19 @@ end: } /* - * Check for the "help" option in the argv. If found, return 1 else return 0. + * Check args for specific options that *must* not trigger a session daemon + * execution. + * + * Return 1 if match else 0. */ -static int check_help_command(int argc, char **argv) +static int check_args_no_sessiond(int argc, char **argv) { int i; for (i = 0; i < argc; i++) { if ((strncmp(argv[i], "-h", 2) == 0) || - strncmp(argv[i], "--h", 3) == 0) { + strncmp(argv[i], "--h", 3) == 0 || + strncmp(argv[i], "--list-options", 14)) { return 1; } } @@ -448,7 +452,7 @@ static int parse_args(int argc, char **argv) } /* Spawn session daemon if needed */ - if (opt_no_sessiond == 0 && check_help_command(argc, argv) == 0 && + if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 && (check_sessiond() < 0)) { goto error; } diff --git a/src/bin/lttng/utils.c b/src/bin/lttng/utils.c index e993f0f72..635bf7734 100644 --- a/src/bin/lttng/utils.c +++ b/src/bin/lttng/utils.c @@ -17,10 +17,12 @@ */ #include +#include #include #include "conf.h" +#include "utils.h" /* * get_session_name @@ -48,3 +50,26 @@ error: DBG("Session name found: %s", session_name); return session_name; } + + +/* + * list_cmd_options + * + * Prints a simple list of the options available to a command. This is intended + * to be easily parsed for bash completion. + */ +void list_cmd_options(FILE *ofp, struct poptOption *options) +{ + int i; + struct poptOption *option = NULL; + + for (i = 0; options[i].longName != NULL; i++) { + option = &options[i]; + + fprintf(ofp, "--%s\n", option->longName); + + if (isprint(option->shortName)) { + fprintf(ofp, "-%c\n", option->shortName); + } + } +} diff --git a/src/bin/lttng/utils.h b/src/bin/lttng/utils.h index 5492d5e67..e609b7086 100644 --- a/src/bin/lttng/utils.h +++ b/src/bin/lttng/utils.h @@ -19,8 +19,11 @@ #ifndef _LTTNG_UTILS_H #define _LTTNG_UTILS_H +#include + char *get_config_file_path(void); char *get_session_name(void); int set_session_name(char *name); +void list_cmd_options(FILE *ofp, struct poptOption *options); #endif /* _LTTNG_UTILS_H */