X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fadd_trigger.c;h=d2880b347867bbaa7722521c45e388fabef1cbd6;hb=9d28679a3d23e2404d42fd9f8abe0002797a6213;hp=e7809dc521362408cc2df6b3ed58dddc5f55d2a8;hpb=27692bb8c119de1fb7943d39fc98824c49fa8562;p=lttng-tools.git diff --git a/src/bin/lttng/commands/add_trigger.c b/src/bin/lttng/commands/add_trigger.c index e7809dc52..d2880b347 100644 --- a/src/bin/lttng/commands/add_trigger.c +++ b/src/bin/lttng/commands/add_trigger.c @@ -8,16 +8,19 @@ #include #include #include +#include #include "../command.h" #include "../loglevel.h" #include "../uprobe.h" #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 /* For lttng_event_rule_type_str(). */ #include #include @@ -646,13 +649,13 @@ 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; - struct argpar_state *state; - struct argpar_item *item = NULL; - char *error = NULL; + struct argpar_iter *argpar_iter = NULL; + const struct argpar_item *argpar_item = NULL; int consumed_args = -1; struct lttng_kernel_probe_location *kernel_probe_location = NULL; struct lttng_userspace_probe_location *userspace_probe_location = NULL; @@ -684,74 +687,67 @@ struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv) lttng_dynamic_pointer_array_init(&exclude_names, free); - state = argpar_state_create(*argc, *argv, event_rule_opt_descrs); - if (!state) { - ERR("Failed to allocate an argpar state."); + argpar_iter = argpar_iter_create(*argc, *argv, event_rule_opt_descrs); + if (!argpar_iter) { + ERR("Failed to allocate an argpar iter."); goto error; } while (true) { - enum argpar_state_parse_next_status status; + enum parse_next_item_status status; - ARGPAR_ITEM_DESTROY_AND_RESET(item); - status = argpar_state_parse_next(state, &item, &error); - if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR) { - ERR("%s", error); + status = parse_next_item(argpar_iter, &argpar_item, + argc_offset, *argv, false, NULL); + if (status == PARSE_NEXT_ITEM_STATUS_ERROR) { goto error; - } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) { - /* Just stop parsing here. */ - break; - } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_END) { + } else if (status == PARSE_NEXT_ITEM_STATUS_END) { break; } - assert(status == ARGPAR_STATE_PARSE_NEXT_STATUS_OK); + assert(status == PARSE_NEXT_ITEM_STATUS_OK); - if (item->type == ARGPAR_ITEM_TYPE_OPT) { - const struct argpar_item_opt *item_opt = - (const struct argpar_item_opt *) item; + if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) { + const struct argpar_opt_descr *descr = + argpar_item_opt_descr(argpar_item); + const char *arg = argpar_item_opt_arg(argpar_item); - switch (item_opt->descr->id) { + switch (descr->id) { case OPT_TYPE: - if (!assign_event_rule_type(&event_rule_type, - item_opt->arg)) { + if (!assign_event_rule_type(&event_rule_type, arg)) { goto error; } /* Save the string for later use. */ - if (!assign_string(&event_rule_type_str, - item_opt->arg, - "--type/-t")) { + if (!assign_string(&event_rule_type_str, arg, + "--type/-t")) { goto error; } break; case OPT_LOCATION: - if (!assign_string(&location, - item_opt->arg, + if (!assign_string(&location, arg, "--location/-L")) { goto error; } break; case OPT_EVENT_NAME: - if (!assign_string(&event_name, - item_opt->arg, - "--event-name/-E")) { + if (!assign_string(&event_name, arg, + "--event-name/-E")) { goto error; } break; case OPT_FILTER: - if (!assign_string(&filter, item_opt->arg, - "--filter/-f")) { + if (!assign_string(&filter, arg, + "--filter/-f")) { goto error; } break; case OPT_NAME: - if (!assign_string(&name, item_opt->arg, - "--name/-n")) { + if (!assign_string(&name, arg, + "--name/-n")) { goto error; } @@ -762,7 +758,7 @@ struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv) ret = lttng_dynamic_pointer_array_add_pointer( &exclude_names, - strdup(item_opt->arg)); + strdup(arg)); if (ret != 0) { ERR("Failed to add pointer to dynamic pointer array."); goto error; @@ -771,8 +767,8 @@ struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv) break; } case OPT_LOG_LEVEL: - if (!assign_string(&log_level_str, - item_opt->arg, "--log-level/-l")) { + if (!assign_string(&log_level_str, arg, + "--log-level/-l")) { goto error; } @@ -780,19 +776,16 @@ struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv) case OPT_CAPTURE: { int ret; - const char *capture_str = item_opt->arg; ret = filter_parser_ctx_create_from_filter_expression( - capture_str, &parser_ctx); + arg, &parser_ctx); if (ret) { - ERR("Failed to parse capture expression `%s`.", - capture_str); + ERR("Failed to parse capture expression `%s`.", arg); goto error; } event_expr = ir_op_root_to_event_expr( - parser_ctx->ir_root, - capture_str); + parser_ctx->ir_root, arg); if (!event_expr) { /* * ir_op_root_to_event_expr has printed @@ -820,18 +813,17 @@ struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv) abort(); } } else { - const struct argpar_item_non_opt *item_non_opt = - (const struct argpar_item_non_opt *) - item; + const char *arg = argpar_item_non_opt_arg(argpar_item); /* Don't accept non-option arguments. */ - ERR("Unexpected argument '%s'", item_non_opt->arg); + ERR("Unexpected argument '%s'", arg); goto error; } } if (event_rule_type == LTTNG_EVENT_RULE_TYPE_UNKNOWN) { - event_rule_type = LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT; + ERR("Event rule requires a --type."); + goto error; } /* @@ -907,7 +899,7 @@ struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv) /* * Update *argc and *argv so our caller can keep parsing what follows. */ - consumed_args = argpar_state_get_ingested_orig_args(state); + consumed_args = argpar_iter_ingested_orig_args(argpar_iter); assert(consumed_args >= 0); *argc -= consumed_args; *argv += consumed_args; @@ -1339,9 +1331,8 @@ end: } lttng_event_expr_destroy(event_expr); - argpar_item_destroy(item); - free(error); - argpar_state_destroy(state); + argpar_item_destroy(argpar_item); + argpar_iter_destroy(argpar_iter); free(filter); free(name); lttng_dynamic_pointer_array_reset(&exclude_names); @@ -1357,13 +1348,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; @@ -1413,7 +1405,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 @@ -1423,7 +1416,8 @@ struct condition_descr condition_descrs[] = { 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; @@ -1437,11 +1431,12 @@ 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); 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; @@ -1534,45 +1529,43 @@ 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_state *state = NULL; - struct argpar_item *item = NULL; - char *error = NULL; + struct argpar_iter *argpar_iter = NULL; + const struct argpar_item *argpar_item = NULL; struct lttng_rate_policy *policy = NULL; - state = argpar_state_create(*argc, *argv, notify_action_opt_descrs); - if (!state) { - ERR("Failed to allocate an argpar state."); + argpar_iter = argpar_iter_create(*argc, *argv, notify_action_opt_descrs); + if (!argpar_iter) { + ERR("Failed to allocate an argpar iter."); goto error; } while (true) { - enum argpar_state_parse_next_status status; + enum parse_next_item_status status; - ARGPAR_ITEM_DESTROY_AND_RESET(item); - status = argpar_state_parse_next(state, &item, &error); - if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR) { - ERR("%s", error); + status = parse_next_item(argpar_iter, &argpar_item, + argc_offset, *argv, false, + "While parsing `notify` action:"); + if (status == PARSE_NEXT_ITEM_STATUS_ERROR) { goto error; - } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) { - /* Just stop parsing here. */ - break; - } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_END) { + } else if (status == PARSE_NEXT_ITEM_STATUS_END) { break; } - assert(status == ARGPAR_STATE_PARSE_NEXT_STATUS_OK); + assert(status == PARSE_NEXT_ITEM_STATUS_OK); - if (item->type == ARGPAR_ITEM_TYPE_OPT) { - const struct argpar_item_opt *item_opt = - (const struct argpar_item_opt *) item; + if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) { + const struct argpar_opt_descr *descr = + argpar_item_opt_descr(argpar_item); + const char *arg = argpar_item_opt_arg(argpar_item); - switch (item_opt->descr->id) { + switch (descr->id) { case OPT_RATE_POLICY: { - policy = parse_rate_policy(item_opt->arg); + policy = parse_rate_policy(arg); if (!policy) { goto error; } @@ -1582,23 +1575,15 @@ struct lttng_action *handle_action_notify(int *argc, const char ***argv) abort(); } } else { - const struct argpar_item_non_opt *item_non_opt; - - assert(item->type == ARGPAR_ITEM_TYPE_NON_OPT); + const char *arg = argpar_item_non_opt_arg(argpar_item); - item_non_opt = (const struct argpar_item_non_opt *) item; - - switch (item_non_opt->non_opt_index) { - default: - ERR("Unexpected argument `%s`.", - item_non_opt->arg); - goto error; - } + ERR("Unexpected argument `%s`.", arg); + goto error; } } - *argc -= argpar_state_get_ingested_orig_args(state); - *argv += argpar_state_get_ingested_orig_args(state); + *argc -= argpar_iter_ingested_orig_args(argpar_iter); + *argv += argpar_iter_ingested_orig_args(argpar_iter); action = lttng_action_notify_create(); if (!action) { @@ -1621,10 +1606,9 @@ error: lttng_action_destroy(action); action = NULL; end: - free(error); lttng_rate_policy_destroy(policy); - argpar_state_destroy(state); - argpar_item_destroy(item); + argpar_item_destroy(argpar_item); + argpar_iter_destroy(argpar_iter); return action; } @@ -1635,6 +1619,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,10 +1629,9 @@ static struct lttng_action *handle_action_simple_session_with_policy(int *argc, const char *action_name) { struct lttng_action *action = NULL; - struct argpar_state *state = NULL; - struct argpar_item *item = NULL; + struct argpar_iter *argpar_iter = NULL; + const struct argpar_item *argpar_item = NULL; const char *session_name_arg = NULL; - char *error = NULL; enum lttng_action_status action_status; struct lttng_rate_policy *policy = NULL; @@ -1659,37 +1643,35 @@ static struct lttng_action *handle_action_simple_session_with_policy(int *argc, ARGPAR_OPT_DESCR_SENTINEL }; - state = argpar_state_create(*argc, *argv, rate_policy_opt_descrs); - if (!state) { - ERR("Failed to allocate an argpar state."); + argpar_iter = argpar_iter_create(*argc, *argv, rate_policy_opt_descrs); + if (!argpar_iter) { + ERR("Failed to allocate an argpar iter."); goto error; } while (true) { - enum argpar_state_parse_next_status status; + enum parse_next_item_status status; - ARGPAR_ITEM_DESTROY_AND_RESET(item); - status = argpar_state_parse_next(state, &item, &error); - if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR) { - ERR("%s", error); + status = parse_next_item(argpar_iter, &argpar_item, argc_offset, + *argv, false, + "While parsing `%s` action:", action_name); + if (status == PARSE_NEXT_ITEM_STATUS_ERROR) { goto error; - } else if (status == - ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) { - /* Just stop parsing here. */ - break; - } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_END) { + } else if (status == PARSE_NEXT_ITEM_STATUS_END) { break; } - assert(status == ARGPAR_STATE_PARSE_NEXT_STATUS_OK); - if (item->type == ARGPAR_ITEM_TYPE_OPT) { - const struct argpar_item_opt *item_opt = - (const struct argpar_item_opt *) item; + assert(status == PARSE_NEXT_ITEM_STATUS_OK); - switch (item_opt->descr->id) { + if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) { + const struct argpar_opt_descr *descr = + argpar_item_opt_descr(argpar_item); + const char *arg = argpar_item_opt_arg(argpar_item); + + switch (descr->id) { case OPT_RATE_POLICY: { - policy = parse_rate_policy(item_opt->arg); + policy = parse_rate_policy(arg); if (!policy) { goto error; } @@ -1699,23 +1681,22 @@ static struct lttng_action *handle_action_simple_session_with_policy(int *argc, abort(); } } else { - const struct argpar_item_non_opt *item_non_opt; - item_non_opt = (const struct argpar_item_non_opt *) item; + const char *arg = argpar_item_non_opt_arg(argpar_item); + unsigned int idx = argpar_item_non_opt_non_opt_index(argpar_item); - switch (item_non_opt->non_opt_index) { + switch (idx) { case 0: - session_name_arg = item_non_opt->arg; + session_name_arg = arg; break; default: - ERR("Unexpected argument `%s`.", - item_non_opt->arg); + ERR("Unexpected argument `%s`.", arg); goto error; } } } - *argc -= argpar_state_get_ingested_orig_args(state); - *argv += argpar_state_get_ingested_orig_args(state); + *argc -= argpar_iter_ingested_orig_args(argpar_iter); + *argv += argpar_iter_ingested_orig_args(argpar_iter); if (!session_name_arg) { ERR("Missing session name."); @@ -1748,19 +1729,20 @@ static struct lttng_action *handle_action_simple_session_with_policy(int *argc, error: lttng_action_destroy(action); action = NULL; - argpar_item_destroy(item); + end: lttng_rate_policy_destroy(policy); - free(error); - argpar_state_destroy(state); + argpar_item_destroy(argpar_item); + argpar_iter_destroy(argpar_iter); return action; } 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"); @@ -1768,9 +1750,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"); @@ -1778,9 +1761,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, @@ -1800,11 +1784,11 @@ 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_state *state = NULL; - struct argpar_item *item = NULL; + struct argpar_iter *argpar_iter = NULL; + const struct argpar_item *argpar_item = NULL; const char *session_name_arg = NULL; char *snapshot_name_arg = NULL; char *ctrl_url_arg = NULL; @@ -1819,73 +1803,70 @@ struct lttng_action *handle_action_snapshot_session(int *argc, int ret; unsigned int locations_specified = 0; - state = argpar_state_create(*argc, *argv, snapshot_action_opt_descrs); - if (!state) { - ERR("Failed to allocate an argpar state."); + argpar_iter = argpar_iter_create(*argc, *argv, snapshot_action_opt_descrs); + if (!argpar_iter) { + ERR("Failed to allocate an argpar iter."); goto error; } while (true) { - enum argpar_state_parse_next_status status; + enum parse_next_item_status status; - ARGPAR_ITEM_DESTROY_AND_RESET(item); - status = argpar_state_parse_next(state, &item, &error); - if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR) { - ERR("%s", error); + status = parse_next_item(argpar_iter, &argpar_item, argc_offset, + *argv, false, "While parsing `snapshot` action:"); + if (status == PARSE_NEXT_ITEM_STATUS_ERROR) { goto error; - } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) { - /* Just stop parsing here. */ - break; - } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_END) { + } else if (status == PARSE_NEXT_ITEM_STATUS_END) { break; } - assert(status == ARGPAR_STATE_PARSE_NEXT_STATUS_OK); + assert(status == PARSE_NEXT_ITEM_STATUS_OK); - if (item->type == ARGPAR_ITEM_TYPE_OPT) { - const struct argpar_item_opt *item_opt = - (const struct argpar_item_opt *) item; + if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) { + const struct argpar_opt_descr *descr = + argpar_item_opt_descr(argpar_item); + const char *arg = argpar_item_opt_arg(argpar_item); - switch (item_opt->descr->id) { + switch (descr->id) { case OPT_NAME: - if (!assign_string(&snapshot_name_arg, item_opt->arg, "--name/-n")) { + if (!assign_string(&snapshot_name_arg, arg, "--name/-n")) { goto error; } break; case OPT_MAX_SIZE: - if (!assign_string(&max_size_arg, item_opt->arg, "--max-size/-m")) { + if (!assign_string(&max_size_arg, arg, "--max-size/-m")) { goto error; } break; case OPT_CTRL_URL: - if (!assign_string(&ctrl_url_arg, item_opt->arg, "--ctrl-url")) { + if (!assign_string(&ctrl_url_arg, arg, "--ctrl-url")) { goto error; } break; case OPT_DATA_URL: - if (!assign_string(&data_url_arg, item_opt->arg, "--data-url")) { + if (!assign_string(&data_url_arg, arg, "--data-url")) { goto error; } break; case OPT_URL: - if (!assign_string(&url_arg, item_opt->arg, "--url")) { + if (!assign_string(&url_arg, arg, "--url")) { goto error; } break; case OPT_PATH: - if (!assign_string(&path_arg, item_opt->arg, "--path")) { + if (!assign_string(&path_arg, arg, "--path")) { goto error; } break; case OPT_RATE_POLICY: { - policy = parse_rate_policy(item_opt->arg); + policy = parse_rate_policy(arg); if (!policy) { goto error; } @@ -1895,26 +1876,22 @@ struct lttng_action *handle_action_snapshot_session(int *argc, abort(); } } else { - const struct argpar_item_non_opt *item_non_opt; - - assert(item->type == ARGPAR_ITEM_TYPE_NON_OPT); - - item_non_opt = (const struct argpar_item_non_opt *) item; + const char *arg = argpar_item_non_opt_arg(argpar_item); + unsigned int idx = argpar_item_non_opt_non_opt_index(argpar_item); - switch (item_non_opt->non_opt_index) { + switch (idx) { case 0: - session_name_arg = item_non_opt->arg; + session_name_arg = arg; break; default: - ERR("Unexpected argument `%s`.", - item_non_opt->arg); + ERR("Unexpected argument `%s`.", arg); goto error; } } } - *argc -= argpar_state_get_ingested_orig_args(state); - *argv += argpar_state_get_ingested_orig_args(state); + *argc -= argpar_iter_ingested_orig_args(argpar_iter); + *argv += argpar_iter_ingested_orig_args(argpar_iter); if (!session_name_arg) { ERR("Missing session name."); @@ -2100,14 +2077,15 @@ end: free(snapshot_output); free(max_size_arg); lttng_rate_policy_destroy(policy); - argpar_state_destroy(state); - argpar_item_destroy(item); + argpar_item_destroy(argpar_item); + argpar_iter_destroy(argpar_iter); return action; } 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 @@ -2120,7 +2098,9 @@ struct action_descr action_descrs[] = { }; static -struct lttng_action *parse_action(const char *action_name, int *argc, const char ***argv) +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; @@ -2134,11 +2114,12 @@ 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); 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; @@ -2177,12 +2158,11 @@ int cmd_add_trigger(int argc, const char **argv) const char **my_argv = argv + 1; struct lttng_condition *condition = NULL; struct lttng_dynamic_pointer_array actions; - struct argpar_state *argpar_state = NULL; - struct argpar_item *argpar_item = NULL; + struct argpar_iter *argpar_iter = NULL; + const struct argpar_item *argpar_item = NULL; struct lttng_action *action_list = NULL; struct lttng_action *action = NULL; struct lttng_trigger *trigger = NULL; - char *error = NULL; char *name = NULL; int i; char *owner_uid = NULL; @@ -2217,50 +2197,44 @@ int cmd_add_trigger(int argc, const char **argv) } while (true) { - enum argpar_state_parse_next_status status; - const struct argpar_item_opt *item_opt; + enum parse_next_item_status status; int ingested_args; + const struct argpar_opt_descr *descr; + const char *arg; - argpar_state_destroy(argpar_state); - argpar_state = argpar_state_create(my_argc, my_argv, + argpar_iter_destroy(argpar_iter); + argpar_iter = argpar_iter_create(my_argc, my_argv, add_trigger_options); - if (!argpar_state) { - ERR("Failed to create argpar state."); + if (!argpar_iter) { + ERR("Failed to create argpar iter."); goto error; } - ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item); - status = argpar_state_parse_next(argpar_state, &argpar_item, &error); - if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR) { - ERR("%s", error); + status = parse_next_item(argpar_iter, &argpar_item, + argc - my_argc, my_argv, true, NULL); + if (status == PARSE_NEXT_ITEM_STATUS_ERROR) { goto error; - } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) { - ERR("%s", error); - goto error; - } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_END) { + } else if (status == PARSE_NEXT_ITEM_STATUS_END) { break; } - assert(status == ARGPAR_STATE_PARSE_NEXT_STATUS_OK); - - if (argpar_item->type == ARGPAR_ITEM_TYPE_NON_OPT) { - const struct argpar_item_non_opt *item_non_opt = - (const struct argpar_item_non_opt *) - argpar_item; + assert(status == PARSE_NEXT_ITEM_STATUS_OK); - ERR("Unexpected argument `%s`.", item_non_opt->arg); + if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_NON_OPT) { + ERR("Unexpected argument `%s`.", + argpar_item_non_opt_arg(argpar_item)); goto error; } - item_opt = (const struct argpar_item_opt *) argpar_item; - - ingested_args = argpar_state_get_ingested_orig_args( - argpar_state); + ingested_args = argpar_iter_ingested_orig_args(argpar_iter); my_argc -= ingested_args; my_argv += ingested_args; - switch (item_opt->descr->id) { + descr = argpar_item_opt_descr(argpar_item); + arg = argpar_item_opt_arg(argpar_item); + + switch (descr->id) { case OPT_HELP: SHOW_HELP(); ret = 0; @@ -2276,7 +2250,9 @@ int cmd_add_trigger(int argc, const char **argv) goto error; } - condition = parse_condition(item_opt->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 @@ -2289,7 +2265,9 @@ int cmd_add_trigger(int argc, const char **argv) } case OPT_ACTION: { - action = parse_action(item_opt->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 @@ -2312,7 +2290,7 @@ int cmd_add_trigger(int argc, const char **argv) } case OPT_NAME: { - if (!assign_string(&name, item_opt->arg, "--name")) { + if (!assign_string(&name, arg, "--name")) { goto error; } @@ -2320,7 +2298,7 @@ int cmd_add_trigger(int argc, const char **argv) } case OPT_OWNER_UID: { - if (!assign_string(&owner_uid, item_opt->arg, + if (!assign_string(&owner_uid, arg, "--owner-uid")) { goto error; } @@ -2406,9 +2384,19 @@ int cmd_add_trigger(int argc, const char **argv) if (ret_code != LTTNG_OK) { goto error; } + } else { + const char *returned_trigger_name; + const enum lttng_trigger_status trigger_status = + lttng_trigger_get_name(trigger, + &returned_trigger_name); + + if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { + WARN("Failed to retrieve the added trigger's name."); + } else { + MSG("Added trigger `%s`.", returned_trigger_name); + } } - MSG("Trigger registered successfully."); ret = 0; goto end; @@ -2418,7 +2406,7 @@ error: end: /* Mi closing. */ - if (lttng_opt_mi) { + if (lttng_opt_mi && mi_writer) { int mi_ret; /* Close output element. */ @@ -2444,14 +2432,13 @@ end: } cleanup: - argpar_state_destroy(argpar_state); + argpar_iter_destroy(argpar_iter); argpar_item_destroy(argpar_item); lttng_dynamic_pointer_array_reset(&actions); lttng_condition_destroy(condition); lttng_action_destroy(action_list); lttng_action_destroy(action); lttng_trigger_destroy(trigger); - free(error); free(name); free(owner_uid); if (mi_writer && mi_lttng_writer_destroy(mi_writer)) {