All the commands currently ignore leftover arguments, this can lead to
wrong usage of the commands and waste of time debugging. For example,
this command enables the vpid context on all channels instead of only on
the "mychan" channel:
$ lttng add-context -u mychan -t vpid

The correct usage is:
$ lttng add-context -u -c mychan -t vpid

We now output an error on leftover arguments:
$ lttng add-context -u bbb -t vpid
Error: Unknown argument: bbb
Error: Command error

Some commands accept one leftover argument (create, start, stop,
destroy), so we check if there are other leftovers:
$ lttng create mysess allo
Error: Unknown argument: allo
Error: Command error

Signed-off-by: Julien Desfossez <jdesfos...@efficios.com>
---
 src/bin/lttng/commands/add_context.c      | 8 ++++++++
 src/bin/lttng/commands/create.c           | 8 ++++++++
 src/bin/lttng/commands/destroy.c          | 9 +++++++++
 src/bin/lttng/commands/disable_channels.c | 8 ++++++++
 src/bin/lttng/commands/disable_events.c   | 8 ++++++++
 src/bin/lttng/commands/enable_channels.c  | 9 +++++++++
 src/bin/lttng/commands/enable_events.c    | 8 ++++++++
 src/bin/lttng/commands/list.c             | 9 ++++++++-
 src/bin/lttng/commands/load.c             | 8 ++++++++
 src/bin/lttng/commands/save.c             | 9 ++++++++-
 src/bin/lttng/commands/snapshot.c         | 8 ++++++++
 src/bin/lttng/commands/start.c            | 8 ++++++++
 src/bin/lttng/commands/stop.c             | 8 ++++++++
 src/bin/lttng/commands/view.c             | 8 ++++++++
 14 files changed, 114 insertions(+), 2 deletions(-)

diff --git a/src/bin/lttng/commands/add_context.c 
b/src/bin/lttng/commands/add_context.c
index 209a9f4..d9121b0 100644
--- a/src/bin/lttng/commands/add_context.c
+++ b/src/bin/lttng/commands/add_context.c
@@ -898,6 +898,7 @@ int cmd_add_context(int argc, const char **argv)
        static poptContext pc;
        struct ctx_type *type, *tmptype;
        char *session_name = NULL;
+       const char *leftover = NULL;
 
        if (argc < 2) {
                ret = CMD_ERROR;
@@ -944,6 +945,13 @@ int cmd_add_context(int argc, const char **argv)
                }
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace +
                        opt_jul + opt_log4j);
        if (ret) {
diff --git a/src/bin/lttng/commands/create.c b/src/bin/lttng/commands/create.c
index d075f64..faf9f3e 100644
--- a/src/bin/lttng/commands/create.c
+++ b/src/bin/lttng/commands/create.c
@@ -625,6 +625,7 @@ int cmd_create(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        char *opt_arg = NULL;
+       const char *leftover = NULL;
        static poptContext pc;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
@@ -719,6 +720,13 @@ int cmd_create(int argc, const char **argv)
        }
        opt_session_name = (char*) poptGetArg(pc);
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        command_ret = create_session();
        if (command_ret) {
                success = 0;
diff --git a/src/bin/lttng/commands/destroy.c b/src/bin/lttng/commands/destroy.c
index 02c7139..6878aaa 100644
--- a/src/bin/lttng/commands/destroy.c
+++ b/src/bin/lttng/commands/destroy.c
@@ -174,6 +174,7 @@ int cmd_destroy(int argc, const char **argv)
        int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       const char *leftover = NULL;
 
        struct lttng_session *sessions;
        int count;
@@ -280,6 +281,14 @@ int cmd_destroy(int argc, const char **argv)
                }
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               success = 0;
+               goto mi_closing;
+       }
+
 mi_closing:
        /* Mi closing */
        if (lttng_opt_mi) {
diff --git a/src/bin/lttng/commands/disable_channels.c 
b/src/bin/lttng/commands/disable_channels.c
index 775ff89..936884e 100644
--- a/src/bin/lttng/commands/disable_channels.c
+++ b/src/bin/lttng/commands/disable_channels.c
@@ -216,6 +216,7 @@ int cmd_disable_channels(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
@@ -250,6 +251,13 @@ int cmd_disable_channels(int argc, const char **argv)
                goto end;
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        if (!opt_session_name) {
                session_name = get_session_name();
                if (session_name == NULL) {
diff --git a/src/bin/lttng/commands/disable_events.c 
b/src/bin/lttng/commands/disable_events.c
index 2696201..290e727 100644
--- a/src/bin/lttng/commands/disable_events.c
+++ b/src/bin/lttng/commands/disable_events.c
@@ -328,6 +328,7 @@ int cmd_disable_events(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       const char *leftover = NULL;
        int event_type = -1;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
@@ -398,6 +399,13 @@ int cmd_disable_events(int argc, const char **argv)
                goto end;
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        if (!opt_session_name) {
                session_name = get_session_name();
                if (session_name == NULL) {
diff --git a/src/bin/lttng/commands/enable_channels.c 
b/src/bin/lttng/commands/enable_channels.c
index cb9f44a..9c84d02 100644
--- a/src/bin/lttng/commands/enable_channels.c
+++ b/src/bin/lttng/commands/enable_channels.c
@@ -400,6 +400,7 @@ int cmd_enable_channels(int argc, const char **argv)
        static poptContext pc;
        char *session_name = NULL;
        char *opt_arg = NULL;
+       const char *leftover = NULL;
 
        init_channel_config();
 
@@ -693,6 +694,14 @@ int cmd_enable_channels(int argc, const char **argv)
                goto mi_closing;
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               success = 0;
+               goto mi_closing;
+       }
+
        if (!opt_session_name) {
                session_name = get_session_name();
                if (session_name == NULL) {
diff --git a/src/bin/lttng/commands/enable_events.c 
b/src/bin/lttng/commands/enable_events.c
index 97a36b3..05f20f7 100644
--- a/src/bin/lttng/commands/enable_events.c
+++ b/src/bin/lttng/commands/enable_events.c
@@ -1264,6 +1264,7 @@ int cmd_enable_events(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       const char *leftover = NULL;
        int event_type = -1;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
@@ -1363,6 +1364,13 @@ int cmd_enable_events(int argc, const char **argv)
                goto end;
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        if (!opt_session_name) {
                session_name = get_session_name();
                if (session_name == NULL) {
diff --git a/src/bin/lttng/commands/list.c b/src/bin/lttng/commands/list.c
index a166fd2..898c0de 100644
--- a/src/bin/lttng/commands/list.c
+++ b/src/bin/lttng/commands/list.c
@@ -1776,7 +1776,7 @@ end:
 int cmd_list(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS;
-       const char *session_name;
+       const char *session_name, *leftover = NULL;
        static poptContext pc;
        struct lttng_domain domain;
        struct lttng_domain *domains = NULL;
@@ -1837,6 +1837,13 @@ int cmd_list(int argc, const char **argv)
        session_name = poptGetArg(pc);
        DBG2("Session name: %s", session_name);
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        if (opt_kernel) {
                domain.type = LTTNG_DOMAIN_KERNEL;
        } else if (opt_userspace) {
diff --git a/src/bin/lttng/commands/load.c b/src/bin/lttng/commands/load.c
index ad61849..07ef121 100644
--- a/src/bin/lttng/commands/load.c
+++ b/src/bin/lttng/commands/load.c
@@ -163,6 +163,7 @@ int cmd_load(int argc, const char **argv)
        poptContext pc;
        struct lttng_load_session_attr *session_attr = NULL;
        char *input_path = NULL;
+       const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, load_opts, 0);
        poptReadDefaultConfig(pc, 0);
@@ -206,6 +207,13 @@ int cmd_load(int argc, const char **argv)
                }
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        /* Mi check */
        if (lttng_opt_mi) {
                writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
diff --git a/src/bin/lttng/commands/save.c b/src/bin/lttng/commands/save.c
index 4ba9024..924ddcb 100644
--- a/src/bin/lttng/commands/save.c
+++ b/src/bin/lttng/commands/save.c
@@ -127,7 +127,7 @@ int cmd_save(int argc, const char **argv)
 {
        int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
        int opt;
-       const char *session_name = NULL;
+       const char *session_name = NULL, *leftover = NULL;
        poptContext pc;
        struct lttng_save_session_attr *attr;
 
@@ -164,6 +164,13 @@ int cmd_save(int argc, const char **argv)
                }
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        attr = lttng_save_session_attr_create();
        if (!attr) {
                ret = CMD_FATAL;
diff --git a/src/bin/lttng/commands/snapshot.c 
b/src/bin/lttng/commands/snapshot.c
index d8a6b81..dc51628 100644
--- a/src/bin/lttng/commands/snapshot.c
+++ b/src/bin/lttng/commands/snapshot.c
@@ -510,6 +510,7 @@ int cmd_snapshot(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        char *session_name = NULL;
        static poptContext pc;
+       const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, snapshot_opts, 0);
        poptReadDefaultConfig(pc, 0);
@@ -571,6 +572,13 @@ int cmd_snapshot(int argc, const char **argv)
                }
        }
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        if (!opt_session_name) {
                session_name = get_session_name();
                if (session_name == NULL) {
diff --git a/src/bin/lttng/commands/start.c b/src/bin/lttng/commands/start.c
index 0287fc7..65b2229 100644
--- a/src/bin/lttng/commands/start.c
+++ b/src/bin/lttng/commands/start.c
@@ -144,6 +144,7 @@ int cmd_start(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
+       const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
@@ -164,6 +165,13 @@ int cmd_start(int argc, const char **argv)
 
        opt_session_name = (char*) poptGetArg(pc);
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        /* Mi check */
        if (lttng_opt_mi) {
                writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
diff --git a/src/bin/lttng/commands/stop.c b/src/bin/lttng/commands/stop.c
index 336d887..9d211d6 100644
--- a/src/bin/lttng/commands/stop.c
+++ b/src/bin/lttng/commands/stop.c
@@ -172,6 +172,7 @@ int cmd_stop(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
+       const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
@@ -228,6 +229,13 @@ int cmd_stop(int argc, const char **argv)
 
        opt_session_name = (char*) poptGetArg(pc);
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        command_ret = stop_tracing();
        if (command_ret) {
                success = 0;
diff --git a/src/bin/lttng/commands/view.c b/src/bin/lttng/commands/view.c
index c9a5fdc..faca60d 100644
--- a/src/bin/lttng/commands/view.c
+++ b/src/bin/lttng/commands/view.c
@@ -415,6 +415,7 @@ int cmd_view(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS;
        static poptContext pc;
+       const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
@@ -439,6 +440,13 @@ int cmd_view(int argc, const char **argv)
 
        opt_session_name = (char*) poptGetArg(pc);
 
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        ret = view_trace();
 
 end:
-- 
2.7.4

_______________________________________________
lttng-dev mailing list
lttng-dev@lists.lttng.org
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Reply via email to