lttng add-trigger: parse condition / action name as option arguments
[lttng-tools.git] / src / bin / lttng / commands / add_trigger.c
index 401fa762ece18648ab2fedd2b0a43f92292ee9f5..fdfbecfaa0afbb8e23d65747fce8d5c61db02c40 100644 (file)
@@ -19,6 +19,9 @@
 /* For lttng_event_rule_type_str(). */
 #include <lttng/event-rule/event-rule-internal.h>
 #include <lttng/lttng.h>
+#include "common/filter/filter-ast.h"
+#include "common/filter/filter-ir.h"
+#include "common/dynamic-array.h"
 
 #if (LTTNG_SYMBOL_NAME_LEN == 256)
 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
@@ -37,9 +40,8 @@ enum {
        OPT_CONDITION,
        OPT_ACTION,
        OPT_ID,
-       OPT_FIRE_ONCE_AFTER,
-       OPT_FIRE_EVERY,
        OPT_USER_ID,
+       OPT_RATE_POLICY,
 
        OPT_ALL,
        OPT_FILTER,
@@ -65,6 +67,8 @@ enum {
        OPT_CTRL_URL,
        OPT_URL,
        OPT_PATH,
+
+       OPT_CAPTURE,
 };
 
 static const struct argpar_opt_descr event_rule_opt_descrs[] = {
@@ -88,6 +92,9 @@ static const struct argpar_opt_descr event_rule_opt_descrs[] = {
        { OPT_SYSCALL, '\0', "syscall" },
        { OPT_TRACEPOINT, '\0', "tracepoint" },
 
+       /* Capture descriptor */
+       { OPT_CAPTURE, '\0', "capture", true },
+
        ARGPAR_OPT_DESCR_SENTINEL
 };
 
@@ -229,7 +236,7 @@ static int parse_kernel_probe_opts(const char *source,
 
                *location = lttng_kernel_probe_location_symbol_create(
                                symbol_name, offset);
-               if (!location) {
+               if (!*location) {
                        ERR("Failed to create symbol kernel probe location.");
                        goto error;
                }
@@ -252,7 +259,7 @@ static int parse_kernel_probe_opts(const char *source,
 
                        *location = lttng_kernel_probe_location_symbol_create(
                                        symbol_name, 0);
-                       if (!location) {
+                       if (!*location) {
                                ERR("Failed to create symbol kernel probe location.");
                                goto error;
                        }
@@ -273,7 +280,7 @@ static int parse_kernel_probe_opts(const char *source,
 
                address = strtoul(s_hex, NULL, 0);
                *location = lttng_kernel_probe_location_address_create(address);
-               if (!location) {
+               if (!*location) {
                        ERR("Failed to create symbol kernel probe location.");
                        goto error;
                }
@@ -291,9 +298,236 @@ end:
        return ret;
 }
 
-static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
+static
+struct lttng_event_expr *ir_op_load_expr_to_event_expr(
+               const struct ir_load_expression *load_expr,
+               const char *capture_str)
+{
+       char *provider_name = NULL;
+       struct lttng_event_expr *event_expr = NULL;
+       const struct ir_load_expression_op *load_expr_op = load_expr->child;
+       const enum ir_load_expression_type load_expr_child_type =
+                       load_expr_op->type;
+
+       switch (load_expr_child_type) {
+       case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT:
+       case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT:
+       {
+               const char *field_name;
+
+               load_expr_op = load_expr_op->next;
+               assert(load_expr_op);
+               assert(load_expr_op->type == IR_LOAD_EXPRESSION_GET_SYMBOL);
+               field_name = load_expr_op->u.symbol;
+               assert(field_name);
+
+               event_expr = load_expr_child_type == IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT ?
+                               lttng_event_expr_event_payload_field_create(field_name) :
+                               lttng_event_expr_channel_context_field_create(field_name);
+               if (!event_expr) {
+                       ERR("Failed to create %s event expression: field name = `%s`.",
+                                       load_expr_child_type == IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT ?
+                                                       "payload field" : "channel context",
+                                                       field_name);
+                       goto error;
+               }
+
+               break;
+       }
+       case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT:
+       {
+               const char *colon;
+               const char *type_name;
+               const char *field_name;
+
+               load_expr_op = load_expr_op->next;
+               assert(load_expr_op);
+               assert(load_expr_op->type == IR_LOAD_EXPRESSION_GET_SYMBOL);
+               field_name = load_expr_op->u.symbol;
+               assert(field_name);
+
+               /*
+                * The field name needs to be of the form PROVIDER:TYPE. We
+                * split it here.
+                */
+               colon = strchr(field_name, ':');
+               if (!colon) {
+                       ERR("Invalid app-specific context field name: missing colon in `%s`.",
+                                       field_name);
+                       goto error;
+               }
+
+               type_name = colon + 1;
+               if (*type_name == '\0') {
+                       ERR("Invalid app-specific context field name: missing type name after colon in `%s`.",
+                                       field_name);
+                       goto error;
+               }
+
+               provider_name = strndup(field_name, colon - field_name);
+               if (!provider_name) {
+                       PERROR("Failed to allocate field name string");
+                       goto error;
+               }
+
+               event_expr = lttng_event_expr_app_specific_context_field_create(
+                               provider_name, type_name);
+               if (!event_expr) {
+                       ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`",
+                                       provider_name, type_name);
+                       goto error;
+               }
+
+               break;
+       }
+       default:
+               ERR("%s: unexpected load expr type %d.", __func__,
+                               load_expr_op->type);
+               abort();
+       }
+
+       load_expr_op = load_expr_op->next;
+
+       /* There may be a single array index after that. */
+       if (load_expr_op->type == IR_LOAD_EXPRESSION_GET_INDEX) {
+               struct lttng_event_expr *index_event_expr;
+               const uint64_t index = load_expr_op->u.index;
+
+               index_event_expr = lttng_event_expr_array_field_element_create(event_expr, index);
+               if (!index_event_expr) {
+                       ERR("Failed to create array field element event expression.");
+                       goto error;
+               }
+
+               event_expr = index_event_expr;
+               load_expr_op = load_expr_op->next;
+       }
+
+       switch (load_expr_op->type) {
+       case IR_LOAD_EXPRESSION_LOAD_FIELD:
+               /*
+                * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is
+                * always found at the end of the chain.
+                */
+               break;
+       case IR_LOAD_EXPRESSION_GET_SYMBOL:
+               ERR("While parsing expression `%s`: Capturing subfields is not supported.",
+                               capture_str);
+               goto error;
+
+       default:
+               ERR("%s: unexpected load expression operator %s.", __func__,
+                               ir_load_expression_type_str(load_expr_op->type));
+               abort();
+       }
+
+       goto end;
+
+error:
+       lttng_event_expr_destroy(event_expr);
+       event_expr = NULL;
+
+end:
+       free(provider_name);
+
+       return event_expr;
+}
+
+static
+struct lttng_event_expr *ir_op_load_to_event_expr(
+               const struct ir_op *ir, const char *capture_str)
+{
+       struct lttng_event_expr *event_expr = NULL;
+
+       assert(ir->op == IR_OP_LOAD);
+
+       switch (ir->data_type) {
+       case IR_DATA_EXPRESSION:
+       {
+               const struct ir_load_expression *ir_load_expr =
+                               ir->u.load.u.expression;
+
+               event_expr = ir_op_load_expr_to_event_expr(
+                               ir_load_expr, capture_str);
+               break;
+       }
+       default:
+               ERR("%s: unexpected data type: %s.", __func__,
+                               ir_data_type_str(ir->data_type));
+               abort();
+       }
+
+       return event_expr;
+}
+
+static
+const char *ir_operator_type_human_str(enum ir_op_type op)
+{
+       const char *name;
+
+       switch (op) {
+       case IR_OP_BINARY:
+               name = "Binary";
+               break;
+       case IR_OP_UNARY:
+               name = "Unary";
+               break;
+       case IR_OP_LOGICAL:
+               name = "Logical";
+               break;
+       default:
+               abort();
+       }
+
+       return name;
+}
+
+static
+struct lttng_event_expr *ir_op_root_to_event_expr(const struct ir_op *ir,
+               const char *capture_str)
+{
+       struct lttng_event_expr *event_expr = NULL;
+
+       assert(ir->op == IR_OP_ROOT);
+       ir = ir->u.root.child;
+
+       switch (ir->op) {
+       case IR_OP_LOAD:
+               event_expr = ir_op_load_to_event_expr(ir, capture_str);
+               break;
+       case IR_OP_BINARY:
+       case IR_OP_UNARY:
+       case IR_OP_LOGICAL:
+               ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.",
+                               capture_str,
+                               ir_operator_type_human_str(ir->op));
+               break;
+       default:
+               ERR("%s: unexpected IR op type: %s.", __func__,
+                               ir_op_type_str(ir->op));
+               abort();
+       }
+
+       return event_expr;
+}
+
+static
+void destroy_event_expr(void *ptr)
+{
+       lttng_event_expr_destroy(ptr);
+}
+
+struct parse_event_rule_res {
+       /* Owned by this. */
+       struct lttng_event_rule *er;
+
+       /* Array of `struct lttng_event_expr *` */
+       struct lttng_dynamic_pointer_array capture_descriptors;
+};
+
+static
+struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv)
 {
-       struct lttng_event_rule *er = NULL;
        enum lttng_domain_type domain_type = LTTNG_DOMAIN_NONE;
        enum lttng_event_rule_type event_rule_type =
                        LTTNG_EVENT_RULE_TYPE_UNKNOWN;
@@ -303,6 +537,10 @@ static struct lttng_event_rule *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 lttng_event_expr *event_expr = NULL;
+       struct filter_parser_ctx *parser_ctx = NULL;
+       struct lttng_log_level_rule *log_level_rule = NULL;
 
        /* Was the -a/--all flag provided? */
        bool all_events = false;
@@ -324,6 +562,8 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
        char *loglevel_str = NULL;
        bool loglevel_only = false;
 
+       lttng_dynamic_pointer_array_init(&res.capture_descriptors,
+                               destroy_event_expr);
        state = argpar_state_create(*argc, *argv, event_rule_opt_descrs);
        if (!state) {
                ERR("Failed to allocate an argpar state.");
@@ -387,14 +627,14 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
                        /* Event rule types */
                        case OPT_FUNCTION:
                                if (!assign_event_rule_type(&event_rule_type,
-                                               LTTNG_EVENT_RULE_TYPE_KRETPROBE)) {
+                                               LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION)) {
                                        goto error;
                                }
 
                                break;
                        case OPT_PROBE:
                                if (!assign_event_rule_type(&event_rule_type,
-                                               LTTNG_EVENT_RULE_TYPE_KPROBE)) {
+                                               LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE)) {
                                        goto error;
                                }
 
@@ -405,7 +645,7 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
                                break;
                        case OPT_USERSPACE_PROBE:
                                if (!assign_event_rule_type(&event_rule_type,
-                                               LTTNG_EVENT_RULE_TYPE_UPROBE)) {
+                                               LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE)) {
                                        goto error;
                                }
 
@@ -455,6 +695,45 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
                                loglevel_only = item_opt->descr->id ==
                                                OPT_LOGLEVEL_ONLY;
                                break;
+                       case OPT_CAPTURE:
+                       {
+                               int ret;
+                               const char *capture_str = item_opt->arg;
+
+                               ret = filter_parser_ctx_create_from_filter_expression(
+                                               capture_str, &parser_ctx);
+                               if (ret) {
+                                       ERR("Failed to parse capture expression `%s`.",
+                                                       capture_str);
+                                       goto error;
+                               }
+
+                               event_expr = ir_op_root_to_event_expr(
+                                               parser_ctx->ir_root,
+                                               capture_str);
+                               if (!event_expr) {
+                                       /*
+                                        * ir_op_root_to_event_expr has printed
+                                        * an error message.
+                                        */
+                                       goto error;
+                               }
+
+                               ret = lttng_dynamic_pointer_array_add_pointer(
+                                               &res.capture_descriptors,
+                                               event_expr);
+                               if (ret) {
+                                       goto error;
+                               }
+
+                               /*
+                                * The ownership of event expression was
+                                * transferred to the dynamic array.
+                                */
+                               event_expr = NULL;
+
+                               break;
+                       }
                        default:
                                abort();
                        }
@@ -538,9 +817,9 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
 
        /* Validate event rule type against domain. */
        switch (event_rule_type) {
-       case LTTNG_EVENT_RULE_TYPE_KPROBE:
-       case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
-       case LTTNG_EVENT_RULE_TYPE_UPROBE:
+       case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE:
+       case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION:
+       case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE:
        case LTTNG_EVENT_RULE_TYPE_SYSCALL:
                if (domain_type != LTTNG_DOMAIN_KERNEL) {
                        ERR("Event type not available for user-space tracing.");
@@ -599,15 +878,15 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
        {
                enum lttng_event_rule_status event_rule_status;
 
-               er = lttng_event_rule_tracepoint_create(domain_type);
-               if (!er) {
+               res.er = lttng_event_rule_tracepoint_create(domain_type);
+               if (!res.er) {
                        ERR("Failed to create tracepoint event rule.");
                        goto error;
                }
 
                /* Set pattern. */
                event_rule_status = lttng_event_rule_tracepoint_set_pattern(
-                               er, tracepoint_name);
+                               res.er, tracepoint_name);
                if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
                        ERR("Failed to set tracepoint event rule's pattern to '%s'.",
                                        tracepoint_name);
@@ -617,7 +896,7 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
                /* Set filter. */
                if (filter) {
                        event_rule_status = lttng_event_rule_tracepoint_set_filter(
-                                       er, filter);
+                                       res.er, filter);
                        if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
                                ERR("Failed to set tracepoint event rule's filter to '%s'.",
                                                filter);
@@ -631,7 +910,7 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
 
                        for (n = 0; exclusion_list[n]; n++) {
                                event_rule_status = lttng_event_rule_tracepoint_add_exclusion(
-                                               er,
+                                               res.er,
                                                exclusion_list[n]);
                                if (event_rule_status !=
                                                LTTNG_EVENT_RULE_STATUS_OK) {
@@ -659,13 +938,20 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
                        }
 
                        if (loglevel_only) {
-                               event_rule_status = lttng_event_rule_tracepoint_set_log_level(
-                                               er, loglevel);
+                               log_level_rule = lttng_log_level_rule_exactly_create(loglevel);
                        } else {
-                               event_rule_status = lttng_event_rule_tracepoint_set_log_level_range_lower_bound(
-                                               er, loglevel);
+                               log_level_rule = lttng_log_level_rule_at_least_as_severe_as_create(loglevel);
+                       }
+
+                       if (log_level_rule == NULL) {
+                               ERR("Failed to create log level rule object.");
+                               goto error;
                        }
 
+                       event_rule_status =
+                                       lttng_event_rule_tracepoint_set_log_level_rule(
+                                                       res.er, log_level_rule);
+
                        if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
                                ERR("Failed to set log level on event fule.");
                                goto error;
@@ -674,16 +960,11 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
 
                break;
        }
-       case LTTNG_EVENT_RULE_TYPE_KPROBE:
+       case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE:
        {
                int ret;
                enum lttng_event_rule_status event_rule_status;
 
-               er = lttng_event_rule_kprobe_create();
-               if (!er) {
-                       ERR("Failed to create kprobe event rule.");
-                       goto error;
-               }
 
                ret = parse_kernel_probe_opts(source, &kernel_probe_location);
                if (ret) {
@@ -691,22 +972,22 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
                        goto error;
                }
 
-               event_rule_status = lttng_event_rule_kprobe_set_name(er, tracepoint_name);
-               if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
-                       ERR("Failed to set kprobe event rule's name to '%s'.", tracepoint_name);
+               assert(kernel_probe_location);
+               res.er = lttng_event_rule_kernel_probe_create(kernel_probe_location);
+               if (!res.er) {
+                       ERR("Failed to create kprobe event rule.");
                        goto error;
                }
 
-               assert(kernel_probe_location);
-               event_rule_status = lttng_event_rule_kprobe_set_location(er, kernel_probe_location);
+               event_rule_status = lttng_event_rule_kernel_probe_set_event_name(res.er, tracepoint_name);
                if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
-                       ERR("Failed to set kprobe event rule's location.");
+                       ERR("Failed to set kprobe event rule's name to '%s'.", tracepoint_name);
                        goto error;
                }
 
                break;
        }
-       case LTTNG_EVENT_RULE_TYPE_UPROBE:
+       case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE:
        {
                int ret;
                enum lttng_event_rule_status event_rule_status;
@@ -718,21 +999,14 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
                        goto error;
                }
 
-               er = lttng_event_rule_uprobe_create();
-               if (!er) {
-                       ERR("Failed to create user space probe event rule.");
+               res.er = lttng_event_rule_userspace_probe_create(userspace_probe_location);
+               if (!res.er) {
+                       ERR("Failed to create userspace probe event rule.");
                        goto error;
                }
 
-               event_rule_status = lttng_event_rule_uprobe_set_location(
-                               er, userspace_probe_location);
-               if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
-                       ERR("Failed to set user space probe event rule's location.");
-                       goto error;
-               }
-
-               event_rule_status = lttng_event_rule_uprobe_set_name(
-                               er, tracepoint_name);
+               event_rule_status = lttng_event_rule_userspace_probe_set_event_name(
+                               res.er, tracepoint_name);
                if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
                        ERR("Failed to set user space probe event rule's name to '%s'.",
                                        tracepoint_name);
@@ -745,14 +1019,14 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
        {
                enum lttng_event_rule_status event_rule_status;
 
-               er = lttng_event_rule_syscall_create();
-               if (!er) {
+               res.er = lttng_event_rule_syscall_create();
+               if (!res.er) {
                        ERR("Failed to create syscall event rule.");
                        goto error;
                }
 
                event_rule_status = lttng_event_rule_syscall_set_pattern(
-                               er, tracepoint_name);
+                               res.er, tracepoint_name);
                if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
                        ERR("Failed to set syscall event rule's pattern to '%s'.",
                                        tracepoint_name);
@@ -761,7 +1035,7 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
 
                if (filter) {
                        event_rule_status = lttng_event_rule_syscall_set_filter(
-                                       er, filter);
+                                       res.er, filter);
                        if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
                                ERR("Failed to set syscall event rule's filter to '%s'.",
                                                filter);
@@ -779,41 +1053,82 @@ static struct lttng_event_rule *parse_event_rule(int *argc, const char ***argv)
        goto end;
 
 error:
-       lttng_event_rule_destroy(er);
-       er = NULL;
+       lttng_event_rule_destroy(res.er);
+       res.er = NULL;
+       lttng_dynamic_pointer_array_reset(&res.capture_descriptors);
 
 end:
+       if (parser_ctx) {
+               filter_parser_ctx_free(parser_ctx);
+       }
+
+       lttng_event_expr_destroy(event_expr);
        argpar_item_destroy(item);
        free(error);
        argpar_state_destroy(state);
        free(filter);
        free(exclude);
        free(loglevel_str);
+       free(source);
        strutils_free_null_terminated_array_of_strings(exclusion_list);
        lttng_kernel_probe_location_destroy(kernel_probe_location);
        lttng_userspace_probe_location_destroy(userspace_probe_location);
-       return er;
+       lttng_log_level_rule_destroy(log_level_rule);
+       return res;
 }
 
 static
 struct lttng_condition *handle_condition_event(int *argc, const char ***argv)
 {
-       struct lttng_event_rule *er;
+       struct parse_event_rule_res res;
        struct lttng_condition *c;
+       size_t i;
 
-       er = parse_event_rule(argc, argv);
-       if (!er) {
+       res = parse_event_rule(argc, argv);
+       if (!res.er) {
                c = NULL;
-               goto end;
+               goto error;
        }
 
-       c = lttng_condition_event_rule_create(er);
-       lttng_event_rule_destroy(er);
+       c = lttng_condition_on_event_create(res.er);
+       lttng_event_rule_destroy(res.er);
+       res.er = NULL;
        if (!c) {
-               goto end;
+               goto error;
        }
 
+       for (i = 0; i < lttng_dynamic_pointer_array_get_count(&res.capture_descriptors);
+                       i++) {
+               enum lttng_condition_status status;
+               struct lttng_event_expr **expr =
+                               lttng_dynamic_array_get_element(
+                                       &res.capture_descriptors.array, i);
+
+               assert(expr);
+               assert(*expr);
+               status = lttng_condition_on_event_append_capture_descriptor(
+                               c, *expr);
+               if (status != LTTNG_CONDITION_STATUS_OK) {
+                       if (status == LTTNG_CONDITION_STATUS_UNSUPPORTED) {
+                               ERR("The capture feature is unsupported by the event-rule condition type");
+                       }
+
+                       goto error;
+               }
+
+               /* Ownership of event expression moved to `c` */
+               *expr = NULL;
+       }
+
+       goto end;
+
+error:
+       lttng_condition_destroy(c);
+       c = NULL;
+
 end:
+       lttng_dynamic_pointer_array_reset(&res.capture_descriptors);
+       lttng_event_rule_destroy(res.er);
        return c;
 }
 
@@ -978,23 +1293,13 @@ struct condition_descr condition_descrs[] = {
 };
 
 static
-struct lttng_condition *parse_condition(int *argc, const char ***argv)
+struct lttng_condition *parse_condition(const char *condition_name, int *argc,
+               const char ***argv)
 {
        int i;
        struct lttng_condition *cond;
-       const char *condition_name;
        const struct condition_descr *descr = NULL;
 
-       if (*argc == 0) {
-               ERR("Missing condition name.");
-               goto error;
-       }
-
-       condition_name = (*argv)[0];
-
-       (*argc)--;
-       (*argv)++;
-
        for (i = 0; i < ARRAY_SIZE(condition_descrs); i++) {
                if (strcmp(condition_name, condition_descrs[i].name) == 0) {
                        descr = &condition_descrs[i];
@@ -1020,27 +1325,191 @@ end:
        return cond;
 }
 
-
-static
-struct lttng_action *handle_action_notify(int *argc, const char ***argv)
+static struct lttng_rate_policy *parse_rate_policy(const char *policy_str)
 {
-       return lttng_action_notify_create();
+       int num_token;
+       char **tokens = NULL;
+       struct lttng_rate_policy *policy = NULL;
+       enum lttng_rate_policy_type policy_type;
+       unsigned long long value;
+       char *policy_type_str;
+       char *policy_value_str;
+
+       assert(policy_str);
+
+       /*
+        * rate policy fields are separated by ':'.
+        */
+       tokens = strutils_split(policy_str, ':', 1);
+       num_token = strutils_array_of_strings_len(tokens);
+
+       /*
+        * Early sanity check that the number of parameter is exactly 2.
+        * i.e : type:value
+        */
+       if (num_token != 2) {
+               ERR("Rate policy format is invalid.");
+               goto end;
+       }
+
+       policy_type_str = tokens[0];
+       policy_value_str = tokens[1];
+
+       /* Parse the type. */
+       if (strcmp(policy_type_str, "once-after") == 0) {
+               policy_type = LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N;
+       } else if (strcmp(policy_type_str, "every") == 0) {
+               policy_type = LTTNG_RATE_POLICY_TYPE_EVERY_N;
+       } else {
+               ERR("Rate policy type `%s` unknown.", policy_type_str);
+               goto end;
+       }
+
+       /* Parse the value. */
+       if (utils_parse_unsigned_long_long(policy_value_str, &value) != 0) {
+               ERR("Failed to parse rate policy value `%s` as an integer.",
+                               policy_value_str);
+               goto end;
+       }
+
+       if (value == 0) {
+               ERR("Rate policy value `%s` must be > 0.", policy_value_str);
+               goto end;
+       }
+
+       switch (policy_type) {
+       case LTTNG_RATE_POLICY_TYPE_EVERY_N:
+               policy = lttng_rate_policy_every_n_create(value);
+               break;
+       case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N:
+               policy = lttng_rate_policy_once_after_n_create(value);
+               break;
+       default:
+               abort();
+       }
+
+       if (policy == NULL) {
+               ERR("Failed to create rate policy `%s`.", policy_str);
+       }
+
+end:
+       strutils_free_null_terminated_array_of_strings(tokens);
+       return policy;
 }
 
-static const struct argpar_opt_descr no_opt_descrs[] = {
+static const struct argpar_opt_descr notify_action_opt_descrs[] = {
+       { OPT_RATE_POLICY, '\0', "rate-policy", true },
        ARGPAR_OPT_DESCR_SENTINEL
 };
 
+static
+struct lttng_action *handle_action_notify(int *argc, const char ***argv)
+{
+       struct lttng_action *action = NULL;
+       struct argpar_state *state = NULL;
+       struct argpar_item *item = NULL;
+       char *error = 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.");
+               goto error;
+       }
+
+       while (true) {
+               enum argpar_state_parse_next_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);
+                       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) {
+                       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;
+
+                       switch (item_opt->descr->id) {
+                       case OPT_RATE_POLICY:
+                       {
+                               policy = parse_rate_policy(item_opt->arg);
+                               if (!policy) {
+                                       goto error;
+                               }
+                               break;
+                       }
+                       default:
+                               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;
+
+                       switch (item_non_opt->non_opt_index) {
+                       default:
+                               ERR("Unexpected argument `%s`.",
+                                               item_non_opt->arg);
+                               goto error;
+                       }
+               }
+       }
+
+       *argc -= argpar_state_get_ingested_orig_args(state);
+       *argv += argpar_state_get_ingested_orig_args(state);
+
+       action = lttng_action_notify_create();
+       if (!action) {
+               ERR("Failed to create notify action");
+               goto error;
+       }
+
+       if (policy) {
+               enum lttng_action_status status;
+               status = lttng_action_notify_set_rate_policy(action, policy);
+               if (status != LTTNG_ACTION_STATUS_OK) {
+                       ERR("Failed to set rate policy");
+                       goto error;
+               }
+       }
+
+       goto end;
+
+error:
+       lttng_action_destroy(action);
+       action = NULL;
+end:
+       free(error);
+       lttng_rate_policy_destroy(policy);
+       argpar_state_destroy(state);
+       argpar_item_destroy(item);
+       return action;
+}
+
 /*
- * Generic handler for a kind of action that takes a session name as its sole
- * argument.
+ * Generic handler for a kind of action that takes a session name and an
+ * optional rate policy.
  */
 
-static
-struct lttng_action *handle_action_simple_session(
-               int *argc, const char ***argv,
+static struct lttng_action *handle_action_simple_session_with_policy(int *argc,
+               const char ***argv,
                struct lttng_action *(*create_action_cb)(void),
-               enum lttng_action_status (*set_session_name_cb)(struct lttng_action *, const char *),
+               enum lttng_action_status (*set_session_name_cb)(
+                               struct lttng_action *, const char *),
+               enum lttng_action_status (*set_rate_policy_cb)(
+                               struct lttng_action *,
+                               const struct lttng_rate_policy *),
                const char *action_name)
 {
        struct lttng_action *action = NULL;
@@ -1049,8 +1518,17 @@ struct lttng_action *handle_action_simple_session(
        const char *session_name_arg = NULL;
        char *error = NULL;
        enum lttng_action_status action_status;
+       struct lttng_rate_policy *policy = NULL;
+
+       assert(set_session_name_cb);
+       assert(set_rate_policy_cb);
 
-       state = argpar_state_create(*argc, *argv, no_opt_descrs);
+       const struct argpar_opt_descr rate_policy_opt_descrs[] = {
+               { OPT_RATE_POLICY, '\0', "rate-policy", true },
+               ARGPAR_OPT_DESCR_SENTINEL
+       };
+       
+       state = argpar_state_create(*argc, *argv, rate_policy_opt_descrs);
        if (!state) {
                ERR("Failed to allocate an argpar state.");
                goto error;
@@ -1058,14 +1536,14 @@ struct lttng_action *handle_action_simple_session(
 
        while (true) {
                enum argpar_state_parse_next_status status;
-               const struct argpar_item_non_opt *item_non_opt;
 
                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);
                        goto error;
-               } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) {
+               } 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) {
@@ -1073,17 +1551,35 @@ struct lttng_action *handle_action_simple_session(
                }
 
                assert(status == ARGPAR_STATE_PARSE_NEXT_STATUS_OK);
-               assert(item->type == ARGPAR_ITEM_TYPE_NON_OPT);
+               if (item->type == ARGPAR_ITEM_TYPE_OPT) {
+                       const struct argpar_item_opt *item_opt =
+                                       (const struct argpar_item_opt *) item;
 
-               item_non_opt = (const struct argpar_item_non_opt *) item;
+                       switch (item_opt->descr->id) {
+                       case OPT_RATE_POLICY:
+                       {
+                               policy = parse_rate_policy(item_opt->arg);
+                               if (!policy) {
+                                       goto error;
+                               }
+                               break;
+                       }
+                       default:
+                               abort();
+                       }
+               } else {
+                       const struct argpar_item_non_opt *item_non_opt;
+                       item_non_opt = (const struct argpar_item_non_opt *) item;
 
-               switch (item_non_opt->non_opt_index) {
-               case 0:
-                       session_name_arg = item_non_opt->arg;
-                       break;
-               default:
-                       ERR("Unexpected argument `%s`.", item_non_opt->arg);
-                       goto error;
+                       switch (item_non_opt->non_opt_index) {
+                       case 0:
+                               session_name_arg = item_non_opt->arg;
+                               break;
+                       default:
+                               ERR("Unexpected argument `%s`.",
+                                               item_non_opt->arg);
+                               goto error;
+                       }
                }
        }
 
@@ -1108,13 +1604,24 @@ struct lttng_action *handle_action_simple_session(
                goto error;
        }
 
+       if (policy) {
+               action_status = set_rate_policy_cb(action, policy);
+               if (action_status != LTTNG_ACTION_STATUS_OK) {
+                       ERR("Failed to set rate policy");
+                       goto error;
+               }
+       }
+
        goto end;
 
 error:
        lttng_action_destroy(action);
        action = NULL;
-       free(error);
+       argpar_item_destroy(item);
 end:
+       lttng_rate_policy_destroy(policy);
+       free(error);
+       argpar_state_destroy(state);
        return action;
 }
 
@@ -1122,29 +1629,30 @@ static
 struct lttng_action *handle_action_start_session(int *argc,
                const char ***argv)
 {
-       return handle_action_simple_session(argc, argv,
-               lttng_action_start_session_create,
-               lttng_action_start_session_set_session_name,
-               "start");
+       return handle_action_simple_session_with_policy(argc, argv,
+                       lttng_action_start_session_create,
+                       lttng_action_start_session_set_session_name,
+                       lttng_action_start_session_set_rate_policy, "start");
 }
 
 static
 struct lttng_action *handle_action_stop_session(int *argc,
                const char ***argv)
 {
-       return handle_action_simple_session(argc, argv,
-               lttng_action_stop_session_create,
-               lttng_action_stop_session_set_session_name,
-               "stop");
+       return handle_action_simple_session_with_policy(argc, argv,
+                       lttng_action_stop_session_create,
+                       lttng_action_stop_session_set_session_name,
+                       lttng_action_stop_session_set_rate_policy, "stop");
 }
 
 static
 struct lttng_action *handle_action_rotate_session(int *argc,
                const char ***argv)
 {
-       return handle_action_simple_session(argc, argv,
+       return handle_action_simple_session_with_policy(argc, argv,
                lttng_action_rotate_session_create,
                lttng_action_rotate_session_set_session_name,
+               lttng_action_rotate_session_set_rate_policy,
                "rotate");
 }
 
@@ -1155,6 +1663,7 @@ static const struct argpar_opt_descr snapshot_action_opt_descrs[] = {
        { OPT_DATA_URL, '\0', "data-url", true },
        { OPT_URL, '\0', "url", true },
        { OPT_PATH, '\0', "path", true },
+       { OPT_RATE_POLICY, '\0', "rate-policy", true },
        ARGPAR_OPT_DESCR_SENTINEL
 };
 
@@ -1175,6 +1684,7 @@ struct lttng_action *handle_action_snapshot_session(int *argc,
        char *error = NULL;
        enum lttng_action_status action_status;
        struct lttng_snapshot_output *snapshot_output = NULL;
+       struct lttng_rate_policy *policy = NULL;
        int ret;
        unsigned int locations_specified = 0;
 
@@ -1242,6 +1752,14 @@ struct lttng_action *handle_action_snapshot_session(int *argc,
                                }
 
                                break;
+                       case OPT_RATE_POLICY:
+                       {
+                               policy = parse_rate_policy(item_opt->arg);
+                               if (!policy) {
+                                       goto error;
+                               }
+                               break;
+                       }
                        default:
                                abort();
                        }
@@ -1426,6 +1944,16 @@ struct lttng_action *handle_action_snapshot_session(int *argc,
                snapshot_output = NULL;
        }
 
+       if (policy) {
+               enum lttng_action_status status;
+               status = lttng_action_snapshot_session_set_rate_policy(
+                               action, policy);
+               if (status != LTTNG_ACTION_STATUS_OK) {
+                       ERR("Failed to set rate policy");
+                       goto error;
+               }
+       }
+
        goto end;
 
 error:
@@ -1439,7 +1967,10 @@ end:
        free(ctrl_url_arg);
        free(data_url_arg);
        free(snapshot_output);
+       free(max_size_arg);
+       lttng_rate_policy_destroy(policy);
        argpar_state_destroy(state);
+       argpar_item_destroy(item);
        return action;
 }
 
@@ -1458,23 +1989,12 @@ struct action_descr action_descrs[] = {
 };
 
 static
-struct lttng_action *parse_action(int *argc, const char ***argv)
+struct lttng_action *parse_action(const char *action_name, int *argc, const char ***argv)
 {
        int i;
        struct lttng_action *action;
-       const char *action_name;
        const struct action_descr *descr = NULL;
 
-       if (*argc == 0) {
-               ERR("Missing action name.");
-               goto error;
-       }
-
-       action_name = (*argv)[0];
-
-       (*argc)--;
-       (*argv)++;
-
        for (i = 0; i < ARRAY_SIZE(action_descrs); i++) {
                if (strcmp(action_name, action_descrs[i].name) == 0) {
                        descr = &action_descrs[i];
@@ -1504,11 +2024,9 @@ static const
 struct argpar_opt_descr add_trigger_options[] = {
        { OPT_HELP, 'h', "help", false },
        { OPT_LIST_OPTIONS, '\0', "list-options", false },
-       { OPT_CONDITION, '\0', "condition", false },
-       { OPT_ACTION, '\0', "action", false },
+       { OPT_CONDITION, '\0', "condition", true },
+       { OPT_ACTION, '\0', "action", true },
        { OPT_ID, '\0', "id", true },
-       { OPT_FIRE_ONCE_AFTER, '\0', "fire-once-after", true },
-       { OPT_FIRE_EVERY, '\0', "fire-every", true },
        { OPT_USER_ID, '\0', "user-id", true },
        ARGPAR_OPT_DESCR_SENTINEL,
 };
@@ -1536,8 +2054,6 @@ int cmd_add_trigger(int argc, const char **argv)
        char *error = NULL;
        char *id = NULL;
        int i;
-       char *fire_once_after_str = NULL;
-       char *fire_every_str = NULL;
        char *user_id = NULL;
 
        lttng_dynamic_pointer_array_init(&actions, lttng_actions_destructor);
@@ -1602,7 +2118,7 @@ int cmd_add_trigger(int argc, const char **argv)
                                goto error;
                        }
 
-                       condition = parse_condition(&my_argc, &my_argv);
+                       condition = parse_condition(item_opt->arg, &my_argc, &my_argv);
                        if (!condition) {
                                /*
                                 * An error message was already printed by
@@ -1615,7 +2131,7 @@ int cmd_add_trigger(int argc, const char **argv)
                }
                case OPT_ACTION:
                {
-                       action = parse_action(&my_argc, &my_argv);
+                       action = parse_action(item_opt->arg, &my_argc, &my_argv);
                        if (!action) {
                                /*
                                 * An error message was already printed by
@@ -1644,24 +2160,6 @@ int cmd_add_trigger(int argc, const char **argv)
 
                        break;
                }
-               case OPT_FIRE_ONCE_AFTER:
-               {
-                       if (!assign_string(&fire_once_after_str, item_opt->arg,
-                                       "--fire-once-after")) {
-                               goto error;
-                       }
-
-                       break;
-               }
-               case OPT_FIRE_EVERY:
-               {
-                       if (!assign_string(&fire_every_str, item_opt->arg,
-                                       "--fire-every")) {
-                               goto error;
-                       }
-
-                       break;
-               }
                case OPT_USER_ID:
                {
                        if (!assign_string(&user_id, item_opt->arg,
@@ -1686,11 +2184,6 @@ int cmd_add_trigger(int argc, const char **argv)
                goto error;
        }
 
-       if (fire_every_str && fire_once_after_str) {
-               ERR("Can't specify both --fire-once-after and --fire-every.");
-               goto error;
-       }
-
        action_group = lttng_action_group_create();
        if (!action_group) {
                goto error;
@@ -1729,41 +2222,6 @@ int cmd_add_trigger(int argc, const char **argv)
                }
        }
 
-       if (fire_once_after_str) {
-               unsigned long long threshold;
-               enum lttng_trigger_status trigger_status;
-
-               if (utils_parse_unsigned_long_long(fire_once_after_str, &threshold) != 0) {
-                       ERR("Failed to parse `%s` as an integer.", fire_once_after_str);
-                       goto error;
-               }
-
-               trigger_status = lttng_trigger_set_firing_policy(trigger,
-                               LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N,
-                               threshold);
-               if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
-                       ERR("Failed to set trigger's policy to `fire once after N`.");
-                       goto error;
-               }
-       }
-
-       if (fire_every_str) {
-               unsigned long long threshold;
-               enum lttng_trigger_status trigger_status;
-
-               if (utils_parse_unsigned_long_long(fire_every_str, &threshold) != 0) {
-                       ERR("Failed to parse `%s` as an integer.", fire_every_str);
-                       goto error;
-               }
-
-               trigger_status = lttng_trigger_set_firing_policy(trigger,
-                               LTTNG_TRIGGER_FIRING_POLICY_EVERY_N, threshold);
-               if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
-                       ERR("Failed to set trigger's policy to `fire every N`.");
-                       goto error;
-               }
-       }
-
        if (user_id) {
                enum lttng_trigger_status trigger_status;
                char *end;
@@ -1805,8 +2263,6 @@ end:
        lttng_trigger_destroy(trigger);
        free(error);
        free(id);
-       free(fire_once_after_str);
-       free(fire_every_str);
        free(user_id);
        return ret;
 }
This page took 0.038915 seconds and 4 git commands to generate.