Rename C++ header files to .hpp
[lttng-tools.git] / src / bin / lttng / commands / add_trigger.cpp
index 79303f6944087188f0935ae09119514ddf22953c..acaa50c7dfff3bfb183651e3f33c5c6e33f325bc 100644 (file)
 #include <string.h>
 #include <stdarg.h>
 
-#include "../command.h"
-#include "../loglevel.h"
-#include "../uprobe.h"
+#include "../command.hpp"
+#include "../loglevel.hpp"
+#include "../uprobe.hpp"
 
 #include "common/argpar/argpar.h"
-#include "common/argpar-utils/argpar-utils.h"
-#include "common/dynamic-array.h"
-#include "common/mi-lttng.h"
-#include "common/string-utils/string-utils.h"
-#include "common/utils.h"
-#include <lttng/domain-internal.h>
+#include "common/argpar-utils/argpar-utils.hpp"
+#include "common/dynamic-array.hpp"
+#include "common/mi-lttng.hpp"
+#include "common/string-utils/string-utils.hpp"
+#include "common/utils.hpp"
+#include <lttng/domain-internal.hpp>
 /* For lttng_event_rule_type_str(). */
-#include <lttng/event-rule/event-rule-internal.h>
+#include <lttng/event-rule/event-rule-internal.hpp>
 #include <lttng/lttng.h>
-#include "common/filter/filter-ast.h"
-#include "common/filter/filter-ir.h"
-#include "common/dynamic-array.h"
+#include "common/filter/filter-ast.hpp"
+#include "common/filter/filter-ir.hpp"
+#include "common/dynamic-array.hpp"
 
 #if (LTTNG_SYMBOL_NAME_LEN == 256)
 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
@@ -355,7 +355,7 @@ static int parse_kernel_probe_opts(const char *source,
                        PERROR("Failed to copy kernel probe location symbol name.");
                        goto error;
                }
-               offset = strtoul(s_hex, NULL, 0);
+               offset = strtoull(s_hex, NULL, 0);
 
                *location = lttng_kernel_probe_location_symbol_create(
                                symbol_name, offset);
@@ -401,7 +401,7 @@ static int parse_kernel_probe_opts(const char *source,
                        goto error;
                }
 
-               address = strtoul(s_hex, NULL, 0);
+               address = strtoull(s_hex, NULL, 0);
                *location = lttng_kernel_probe_location_address_create(address);
                if (!*location) {
                        ERR("Failed to create symbol kernel probe location.");
@@ -649,7 +649,8 @@ struct parse_event_rule_res {
 };
 
 static
-struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv)
+struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv,
+               int argc_offset)
 {
        enum lttng_event_rule_type event_rule_type =
                        LTTNG_EVENT_RULE_TYPE_UNKNOWN;
@@ -658,7 +659,7 @@ struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv)
        int consumed_args = -1;
        struct lttng_kernel_probe_location *kernel_probe_location = NULL;
        struct lttng_userspace_probe_location *userspace_probe_location = NULL;
-       struct parse_event_rule_res res = { 0 };
+       struct parse_event_rule_res res = {};
        struct lttng_event_expr *event_expr = NULL;
        struct filter_parser_ctx *parser_ctx = NULL;
        struct lttng_log_level_rule *log_level_rule = NULL;
@@ -695,9 +696,10 @@ struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv)
        while (true) {
                enum parse_next_item_status status;
 
-               status = parse_next_item(argpar_iter, &argpar_item, *argv,
-                       false, NULL);
-               if (status == PARSE_NEXT_ITEM_STATUS_ERROR) {
+               status = parse_next_item(argpar_iter, &argpar_item,
+                       argc_offset, *argv, false, NULL, NULL);
+               if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
+                               status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
                        goto error;
                } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
                        break;
@@ -1347,13 +1349,14 @@ end:
 }
 
 static
-struct lttng_condition *handle_condition_event(int *argc, const char ***argv)
+struct lttng_condition *handle_condition_event(int *argc, const char ***argv,
+               int argc_offset)
 {
        struct parse_event_rule_res res;
        struct lttng_condition *c;
        size_t i;
 
-       res = parse_event_rule(argc, argv);
+       res = parse_event_rule(argc, argv, argc_offset);
        if (!res.er) {
                c = NULL;
                goto error;
@@ -1403,7 +1406,8 @@ end:
 
 struct condition_descr {
        const char *name;
-       struct lttng_condition *(*handler) (int *argc, const char ***argv);
+       struct lttng_condition *(*handler) (int *argc, const char ***argv,
+               int argc_offset);
 };
 
 static const
@@ -1411,9 +1415,22 @@ struct condition_descr condition_descrs[] = {
        { "event-rule-matches", handle_condition_event },
 };
 
+static
+void print_valid_condition_names(void)
+{
+       unsigned int i;
+
+       ERR("Valid condition names are:");
+
+       for (i = 0; i < ARRAY_SIZE(condition_descrs); ++i) {
+               ERR("  %s", condition_descrs[i].name);
+       }
+}
+
 static
 struct lttng_condition *parse_condition(const char *condition_name, int *argc,
-               const char ***argv)
+               const char ***argv, int argc_offset, int orig_arg_index,
+               const char *orig_arg)
 {
        int i;
        struct lttng_condition *cond;
@@ -1427,11 +1444,13 @@ struct lttng_condition *parse_condition(const char *condition_name, int *argc,
        }
 
        if (!descr) {
-               ERR("Unknown condition name '%s'", condition_name);
+               ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown condition name '%s'",
+                       orig_arg_index + 1, orig_arg, condition_name);
+               print_valid_condition_names();
                goto error;
        }
 
-       cond = descr->handler(argc, argv);
+       cond = descr->handler(argc, argv, argc_offset);
        if (!cond) {
                /* The handler has already printed an error message. */
                goto error;
@@ -1524,7 +1543,8 @@ static const struct argpar_opt_descr notify_action_opt_descrs[] = {
 };
 
 static
-struct lttng_action *handle_action_notify(int *argc, const char ***argv)
+struct lttng_action *handle_action_notify(int *argc, const char ***argv,
+               int argc_offset)
 {
        struct lttng_action *action = NULL;
        struct argpar_iter *argpar_iter = NULL;
@@ -1540,9 +1560,11 @@ struct lttng_action *handle_action_notify(int *argc, const char ***argv)
        while (true) {
                enum parse_next_item_status status;
 
-               status = parse_next_item(argpar_iter, &argpar_item, *argv,
-                       false, "While parsing `notify` action:");
-               if (status == PARSE_NEXT_ITEM_STATUS_ERROR) {
+               status = parse_next_item(argpar_iter, &argpar_item,
+                       argc_offset, *argv, false, NULL,
+                       "While parsing `notify` action:");
+               if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
+                               status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
                        goto error;
                } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
                        break;
@@ -1612,6 +1634,7 @@ end:
 
 static struct lttng_action *handle_action_simple_session_with_policy(int *argc,
                const char ***argv,
+               int argc_offset,
                struct lttng_action *(*create_action_cb)(void),
                enum lttng_action_status (*set_session_name_cb)(
                                struct lttng_action *, const char *),
@@ -1644,9 +1667,11 @@ static struct lttng_action *handle_action_simple_session_with_policy(int *argc,
        while (true) {
                enum parse_next_item_status status;
 
-               status = parse_next_item(argpar_iter, &argpar_item, *argv,
-                       false, "While parsing `%s` action:", action_name);
-               if (status == PARSE_NEXT_ITEM_STATUS_ERROR) {
+               status = parse_next_item(argpar_iter, &argpar_item, argc_offset,
+                       *argv, false, NULL,
+                       "While parsing `%s` action:", action_name);
+               if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
+                               status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
                        goto error;
                } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
                        break;
@@ -1730,9 +1755,10 @@ end:
 
 static
 struct lttng_action *handle_action_start_session(int *argc,
-               const char ***argv)
+               const char ***argv, int argc_offset)
 {
        return handle_action_simple_session_with_policy(argc, argv,
+                       argc_offset,
                        lttng_action_start_session_create,
                        lttng_action_start_session_set_session_name,
                        lttng_action_start_session_set_rate_policy, "start");
@@ -1740,9 +1766,10 @@ struct lttng_action *handle_action_start_session(int *argc,
 
 static
 struct lttng_action *handle_action_stop_session(int *argc,
-               const char ***argv)
+               const char ***argv, int argc_offset)
 {
        return handle_action_simple_session_with_policy(argc, argv,
+                       argc_offset,
                        lttng_action_stop_session_create,
                        lttng_action_stop_session_set_session_name,
                        lttng_action_stop_session_set_rate_policy, "stop");
@@ -1750,9 +1777,10 @@ struct lttng_action *handle_action_stop_session(int *argc,
 
 static
 struct lttng_action *handle_action_rotate_session(int *argc,
-               const char ***argv)
+               const char ***argv, int argc_offset)
 {
        return handle_action_simple_session_with_policy(argc, argv,
+               argc_offset,
                lttng_action_rotate_session_create,
                lttng_action_rotate_session_set_session_name,
                lttng_action_rotate_session_set_rate_policy,
@@ -1772,7 +1800,7 @@ static const struct argpar_opt_descr snapshot_action_opt_descrs[] = {
 
 static
 struct lttng_action *handle_action_snapshot_session(int *argc,
-               const char ***argv)
+               const char ***argv, int argc_offset)
 {
        struct lttng_action *action = NULL;
        struct argpar_iter *argpar_iter = NULL;
@@ -1800,9 +1828,10 @@ struct lttng_action *handle_action_snapshot_session(int *argc,
        while (true) {
                enum parse_next_item_status status;
 
-               status = parse_next_item(argpar_iter, &argpar_item, *argv,
-                       false, "While parsing `snapshot` action:");
-               if (status == PARSE_NEXT_ITEM_STATUS_ERROR) {
+               status = parse_next_item(argpar_iter, &argpar_item, argc_offset,
+                       *argv, false, NULL, "While parsing `snapshot` action:");
+               if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
+                               status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
                        goto error;
                } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
                        break;
@@ -2072,7 +2101,8 @@ end:
 
 struct action_descr {
        const char *name;
-       struct lttng_action *(*handler) (int *argc, const char ***argv);
+       struct lttng_action *(*handler) (int *argc, const char ***argv,
+               int argc_offset);
 };
 
 static const
@@ -2085,7 +2115,21 @@ struct action_descr action_descrs[] = {
 };
 
 static
-struct lttng_action *parse_action(const char *action_name, int *argc, const char ***argv)
+void print_valid_action_names(void)
+{
+       unsigned int i;
+
+       ERR("Valid action names are:");
+
+       for (i = 0; i < ARRAY_SIZE(condition_descrs); ++i) {
+               ERR("  %s", action_descrs[i].name);
+       }
+}
+
+static
+struct lttng_action *parse_action(const char *action_name, int *argc,
+               const char ***argv, int argc_offset, int orig_arg_index,
+               const char *orig_arg)
 {
        int i;
        struct lttng_action *action;
@@ -2099,11 +2143,13 @@ struct lttng_action *parse_action(const char *action_name, int *argc, const char
        }
 
        if (!descr) {
-               ERR("Unknown action name: %s", action_name);
+               ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown action name '%s'",
+                       orig_arg_index + 1, orig_arg, action_name);
+               print_valid_action_names();
                goto error;
        }
 
-       action = descr->handler(argc, argv);
+       action = descr->handler(argc, argv, argc_offset);
        if (!action) {
                /* The handler has already printed an error message. */
                goto error;
@@ -2144,6 +2190,7 @@ int cmd_add_trigger(int argc, const char **argv)
        struct lttng_dynamic_pointer_array actions;
        struct argpar_iter *argpar_iter = NULL;
        const struct argpar_item *argpar_item = NULL;
+       const struct argpar_error *argpar_error = NULL;
        struct lttng_action *action_list = NULL;
        struct lttng_action *action = NULL;
        struct lttng_trigger *trigger = NULL;
@@ -2194,9 +2241,23 @@ int cmd_add_trigger(int argc, const char **argv)
                        goto error;
                }
 
-               status = parse_next_item(argpar_iter, &argpar_item, my_argv,
-                       true, NULL);
+               status = parse_next_item(argpar_iter, &argpar_item,
+                       argc - my_argc, my_argv, true, &argpar_error, NULL);
                if (status == PARSE_NEXT_ITEM_STATUS_ERROR) {
+
+                       if (argpar_error_type(argpar_error) ==
+                                       ARGPAR_ERROR_TYPE_MISSING_OPT_ARG) {
+                               int opt_id = argpar_error_opt_descr(argpar_error, NULL)->id;
+
+                               if (opt_id == OPT_CONDITION) {
+                                       print_valid_condition_names();
+                               } else if (opt_id == OPT_ACTION) {
+                                       print_valid_action_names();
+                               }
+                       }
+
+                       goto error;
+               } else if (status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
                        goto error;
                } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
                        break;
@@ -2234,7 +2295,9 @@ int cmd_add_trigger(int argc, const char **argv)
                                goto error;
                        }
 
-                       condition = parse_condition(arg, &my_argc, &my_argv);
+                       condition = parse_condition(arg, &my_argc, &my_argv,
+                               argc - my_argc, argc - my_argc - ingested_args,
+                               my_argv[-ingested_args]);
                        if (!condition) {
                                /*
                                 * An error message was already printed by
@@ -2247,7 +2310,9 @@ int cmd_add_trigger(int argc, const char **argv)
                }
                case OPT_ACTION:
                {
-                       action = parse_action(arg, &my_argc, &my_argv);
+                       action = parse_action(arg, &my_argc, &my_argv,
+                               argc - my_argc,  argc - my_argc - ingested_args,
+                               my_argv[-ingested_args]);
                        if (!action) {
                                /*
                                 * An error message was already printed by
@@ -2412,6 +2477,7 @@ end:
        }
 
 cleanup:
+       argpar_error_destroy(argpar_error);
        argpar_iter_destroy(argpar_iter);
        argpar_item_destroy(argpar_item);
        lttng_dynamic_pointer_array_reset(&actions);
This page took 0.031211 seconds and 4 git commands to generate.