Fix: pass private data to context callbacks
[lttng-ust.git] / liblttng-ust / lttng-context-provider.c
index 6c067e2fecae06548a3d4495ef9477b2495a6a5b..c70da2089992a71a102d5faf3d6438039b044e7e 100644 (file)
@@ -1,31 +1,36 @@
 /*
- * lttng-context-provider.c
- *
- * LTTng UST application context provider.
+ * SPDX-License-Identifier: LGPL-2.1-only
  *
  * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; only
- * version 2.1 of the License.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * LTTng UST application context provider.
  */
 
+#define _LGPL_SOURCE
+#include <stddef.h>
+#include <stdint.h>
 #include <sys/types.h>
 #include <unistd.h>
-#include <lttng/ust-context-provider.h>
+
+#include <ust-context-provider.h>
+
+#include "context-internal.h"
 #include "lttng-tracer-core.h"
 #include "jhash.h"
-#include <helper.h>
+#include "context-provider-internal.h"
+#include <ust-helper.h>
+
+struct lttng_ust_registered_context_provider {
+       const struct lttng_ust_context_provider *provider;
+
+       struct cds_hlist_node node;
+};
+
+struct lttng_ust_app_ctx {
+       char *name;
+       struct lttng_ust_event_field *event_field;
+       struct lttng_ust_type_common *type;
+};
 
 #define CONTEXT_PROVIDER_HT_BITS       12
 #define CONTEXT_PROVIDER_HT_SIZE       (1U << CONTEXT_PROVIDER_HT_BITS)
@@ -35,12 +40,12 @@ struct context_provider_ht {
 
 static struct context_provider_ht context_provider_ht;
 
-static struct lttng_ust_context_provider *
+static const struct lttng_ust_context_provider *
                lookup_provider_by_name(const char *name)
 {
        struct cds_hlist_head *head;
        struct cds_hlist_node *node;
-       struct lttng_ust_context_provider *provider;
+       struct lttng_ust_registered_context_provider *reg_provider;
        uint32_t hash;
        const char *end;
        size_t len;
@@ -53,55 +58,80 @@ static struct lttng_ust_context_provider *
                len = strlen(name);
        hash = jhash(name, len, 0);
        head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
-       cds_hlist_for_each_entry(provider, node, head, node) {
-               if (!strncmp(provider->name, name, len))
-                       return provider;
+       cds_hlist_for_each_entry(reg_provider, node, head, node) {
+               if (!strncmp(reg_provider->provider->name, name, len))
+                       return reg_provider->provider;
        }
        return NULL;
 }
 
-int lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider)
+struct lttng_ust_registered_context_provider *lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider)
 {
+       struct lttng_ust_registered_context_provider *reg_provider = NULL;
        struct cds_hlist_head *head;
        size_t name_len = strlen(provider->name);
        uint32_t hash;
-       int ret = 0;
+
+       lttng_ust_fixup_tls();
 
        /* Provider name starts with "$app.". */
-       if (strncmp("$app.", provider->name, strlen("$app.") != 0))
-               return -EINVAL;
-       /* Provider name cannot contain a column character. */
+       if (strncmp("$app.", provider->name, strlen("$app.")) != 0)
+               return NULL;
+       /* Provider name cannot contain a colon character. */
        if (strchr(provider->name, ':'))
-               return -EINVAL;
-       if (ust_lock()) {
-               ret = -EBUSY;
+               return NULL;
+       if (ust_lock())
                goto end;
-       }
-       if (lookup_provider_by_name(provider->name)) {
-               ret = -EBUSY;
+       if (lookup_provider_by_name(provider->name))
                goto end;
-       }
+       reg_provider = zmalloc(sizeof(struct lttng_ust_registered_context_provider));
+       if (!reg_provider)
+               goto end;
+       reg_provider->provider = provider;
        hash = jhash(provider->name, name_len, 0);
        head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
-       cds_hlist_add_head(&provider->node, head);
+       cds_hlist_add_head(&reg_provider->node, head);
+
        lttng_ust_context_set_session_provider(provider->name,
                provider->get_size, provider->record,
-               provider->get_value);
+               provider->get_value, provider->priv);
+
+       lttng_ust_context_set_event_notifier_group_provider(provider->name,
+               provider->get_size, provider->record,
+               provider->get_value, provider->priv);
 end:
        ust_unlock();
-       return ret;
+       return reg_provider;
 }
 
-void lttng_ust_context_provider_unregister(struct lttng_ust_context_provider *provider)
+void lttng_ust_context_provider_unregister(struct lttng_ust_registered_context_provider *reg_provider)
 {
+       lttng_ust_fixup_tls();
+
        if (ust_lock())
                goto end;
-       lttng_ust_context_set_session_provider(provider->name,
+       lttng_ust_context_set_session_provider(reg_provider->provider->name,
+               lttng_ust_dummy_get_size, lttng_ust_dummy_record,
+               lttng_ust_dummy_get_value, NULL);
+
+       lttng_ust_context_set_event_notifier_group_provider(reg_provider->provider->name,
                lttng_ust_dummy_get_size, lttng_ust_dummy_record,
-               lttng_ust_dummy_get_value);
-       cds_hlist_del(&provider->node);
+               lttng_ust_dummy_get_value, NULL);
+
+       cds_hlist_del(&reg_provider->node);
 end:
        ust_unlock();
+       free(reg_provider);
+}
+
+static void destroy_app_ctx(void *priv)
+{
+       struct lttng_ust_app_ctx *app_ctx = (struct lttng_ust_app_ctx *) priv;
+
+       free(app_ctx->name);
+       free(app_ctx->event_field);
+       free(app_ctx->type);
+       free(app_ctx);
 }
 
 /*
@@ -113,24 +143,42 @@ end:
  * metadata describing the context.
  */
 int lttng_ust_add_app_context_to_ctx_rcu(const char *name,
-               struct lttng_ctx **ctx)
+               struct lttng_ust_ctx **ctx)
 {
-       struct lttng_ust_context_provider *provider;
-       struct lttng_ctx_field new_field;
+       const struct lttng_ust_context_provider *provider;
+       struct lttng_ust_ctx_field new_field = { 0 };
+       struct lttng_ust_event_field *event_field = NULL;
+       struct lttng_ust_type_common *type = NULL;
+       struct lttng_ust_app_ctx *app_ctx = NULL;
+       char *ctx_name;
        int ret;
 
        if (*ctx && lttng_find_context(*ctx, name))
                return -EEXIST;
-       /*
-        * For application context, add it by expanding
-        * ctx array.
-        */
-       memset(&new_field, 0, sizeof(new_field));
-       new_field.field_name = strdup(name);
-       if (!new_field.field_name)
-               return -ENOMEM;
-       new_field.event_field.name = new_field.field_name;
-       new_field.event_field.type.atype = atype_dynamic;
+       event_field = zmalloc(sizeof(struct lttng_ust_event_field));
+       if (!event_field) {
+               ret = -ENOMEM;
+               goto error_event_field_alloc;
+       }
+       ctx_name = strdup(name);
+       if (!ctx_name) {
+               ret = -ENOMEM;
+               goto error_field_name_alloc;
+       }
+       type = zmalloc(sizeof(struct lttng_ust_type_common));
+       if (!type) {
+               ret = -ENOMEM;
+               goto error_field_type_alloc;
+       }
+       app_ctx = zmalloc(sizeof(struct lttng_ust_app_ctx));
+       if (!app_ctx) {
+               ret = -ENOMEM;
+               goto error_app_ctx_alloc;
+       }
+       event_field->name = ctx_name;
+       type->type = lttng_ust_type_dynamic;
+       event_field->type = type;
+       new_field.event_field = event_field;
        /*
         * If provider is not found, we add the context anyway, but
         * it will provide a dummy context.
@@ -145,10 +193,25 @@ int lttng_ust_add_app_context_to_ctx_rcu(const char *name,
                new_field.record = lttng_ust_dummy_record;
                new_field.get_value = lttng_ust_dummy_get_value;
        }
-       ret = lttng_context_add_rcu(ctx, &new_field);
+       new_field.destroy = destroy_app_ctx;
+       new_field.priv = app_ctx;
+       /*
+        * For application context, add it by expanding
+        * ctx array.
+        */
+       ret = lttng_ust_context_append_rcu(ctx, &new_field);
        if (ret) {
-               free(new_field.field_name);
+               destroy_app_ctx(app_ctx);
                return ret;
        }
        return 0;
+
+error_app_ctx_alloc:
+       free(type);
+error_field_type_alloc:
+       free(ctx_name);
+error_field_name_alloc:
+       free(event_field);
+error_event_field_alloc:
+       return ret;
 }
This page took 0.025189 seconds and 4 git commands to generate.