+/*
+ * Depending on the event, return a newly allocated agent filter expression or
+ * NULL if not applicable.
+ *
+ * An event with NO loglevel and the name is * will return NULL.
+ */
+static char *set_agent_filter(const char *filter, struct lttng_event *ev)
+{
+ int err;
+ char *agent_filter = NULL;
+
+ assert(ev);
+
+ /* Don't add filter for the '*' event. */
+ if (ev->name[0] != '*') {
+ if (filter) {
+ err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
+ ev->name);
+ } else {
+ err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name);
+ }
+ if (err < 0) {
+ PERROR("asprintf");
+ goto error;
+ }
+ }
+
+ /* Add loglevel filtering if any for the JUL domain. */
+ if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
+ char *op;
+
+ if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
+ op = ">=";
+ } else {
+ op = "==";
+ }
+
+ if (filter || agent_filter) {
+ char *new_filter;
+
+ err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)",
+ agent_filter ? agent_filter : filter, op,
+ ev->loglevel);
+ if (agent_filter) {
+ free(agent_filter);
+ }
+ agent_filter = new_filter;
+ } else {
+ err = asprintf(&agent_filter, "int_loglevel %s %d", op,
+ ev->loglevel);
+ }
+ if (err < 0) {
+ PERROR("asprintf");
+ goto error;
+ }
+ }
+
+ return agent_filter;
+error:
+ free(agent_filter);
+ return NULL;
+}
+
+/*
+ * Generate the filter bytecode from a given filter expression string. Put the
+ * newly allocated parser context in ctxp and populate the lsm object with the
+ * expression len.
+ *
+ * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched.
+ */
+static int generate_filter(char *filter_expression,
+ struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp)
+{
+ int ret;
+ struct filter_parser_ctx *ctx = NULL;
+ FILE *fmem = NULL;
+
+ assert(filter_expression);
+ assert(lsm);
+ assert(ctxp);
+
+ /*
+ * Casting const to non-const, as the underlying function will use it in
+ * read-only mode.
+ */
+ fmem = lttng_fmemopen((void *) filter_expression,
+ strlen(filter_expression), "r");
+ if (!fmem) {
+ fprintf(stderr, "Error opening memory as stream\n");
+ ret = -LTTNG_ERR_FILTER_NOMEM;
+ goto error;
+ }
+ ctx = filter_parser_ctx_alloc(fmem);
+ if (!ctx) {
+ fprintf(stderr, "Error allocating parser\n");
+ ret = -LTTNG_ERR_FILTER_NOMEM;
+ goto filter_alloc_error;
+ }
+ ret = filter_parser_ctx_append_ast(ctx);
+ if (ret) {
+ fprintf(stderr, "Parse error\n");
+ ret = -LTTNG_ERR_FILTER_INVAL;
+ goto parse_error;
+ }
+ ret = filter_visitor_set_parent(ctx);
+ if (ret) {
+ fprintf(stderr, "Set parent error\n");
+ ret = -LTTNG_ERR_FILTER_INVAL;
+ goto parse_error;
+ }
+ if (print_xml) {
+ ret = filter_visitor_print_xml(ctx, stdout, 0);
+ if (ret) {
+ fflush(stdout);
+ fprintf(stderr, "XML print error\n");
+ ret = -LTTNG_ERR_FILTER_INVAL;
+ goto parse_error;
+ }
+ }
+
+ dbg_printf("Generating IR... ");
+ fflush(stdout);
+ ret = filter_visitor_ir_generate(ctx);
+ if (ret) {
+ fprintf(stderr, "Generate IR error\n");
+ ret = -LTTNG_ERR_FILTER_INVAL;
+ goto parse_error;
+ }
+ dbg_printf("done\n");
+
+ dbg_printf("Validating IR... ");
+ fflush(stdout);
+ ret = filter_visitor_ir_check_binary_op_nesting(ctx);
+ if (ret) {
+ ret = -LTTNG_ERR_FILTER_INVAL;
+ goto parse_error;
+ }
+ /* Validate strings used as literals in the expression */
+ ret = filter_visitor_ir_validate_string(ctx);
+ if (ret) {
+ ret = -LTTNG_ERR_FILTER_INVAL;
+ goto parse_error;
+ }
+ dbg_printf("done\n");
+
+ dbg_printf("Generating bytecode... ");
+ fflush(stdout);
+ ret = filter_visitor_bytecode_generate(ctx);
+ if (ret) {
+ fprintf(stderr, "Generate bytecode error\n");
+ ret = -LTTNG_ERR_FILTER_INVAL;
+ goto parse_error;
+ }
+ dbg_printf("done\n");
+ dbg_printf("Size of bytecode generated: %u bytes.\n",
+ bytecode_get_len(&ctx->bytecode->b));
+
+ lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b)
+ + bytecode_get_len(&ctx->bytecode->b);
+ lsm->u.enable.expression_len = strlen(filter_expression) + 1;
+
+ /* No need to keep the memory stream. */
+ if (fclose(fmem) != 0) {
+ PERROR("fclose");
+ }
+
+ *ctxp = ctx;
+ return 0;
+
+parse_error:
+ filter_ir_free(ctx);
+ filter_parser_ctx_free(ctx);
+filter_alloc_error:
+ if (fclose(fmem) != 0) {
+ PERROR("fclose");
+ }
+error:
+ return ret;
+}
+