Fix: pass private data to context callbacks
[lttng-ust.git] / liblttng-ust / lttng-context-provider.c
CommitLineData
53569322 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
53569322
MD
3 *
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
c0c0989a 6 * LTTng UST application context provider.
53569322
MD
7 */
8
3fbec7dc 9#define _LGPL_SOURCE
b4051ad8 10#include <stddef.h>
fb31eb73 11#include <stdint.h>
53569322
MD
12#include <sys/types.h>
13#include <unistd.h>
fb31eb73 14
ae4b659d 15#include <ust-context-provider.h>
d8d2416d 16
fc80554e 17#include "context-internal.h"
53569322
MD
18#include "lttng-tracer-core.h"
19#include "jhash.h"
d8d2416d 20#include "context-provider-internal.h"
864a1eda 21#include <ust-helper.h>
53569322 22
4e48b5d2
MD
23struct lttng_ust_registered_context_provider {
24 const struct lttng_ust_context_provider *provider;
25
26 struct cds_hlist_node node;
27};
28
f26f2f9b
MD
29struct lttng_ust_app_ctx {
30 char *name;
31 struct lttng_ust_event_field *event_field;
32 struct lttng_ust_type_common *type;
33};
34
53569322
MD
35#define CONTEXT_PROVIDER_HT_BITS 12
36#define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS)
37struct context_provider_ht {
38 struct cds_hlist_head table[CONTEXT_PROVIDER_HT_SIZE];
39};
40
41static struct context_provider_ht context_provider_ht;
42
4e48b5d2 43static const struct lttng_ust_context_provider *
53569322
MD
44 lookup_provider_by_name(const char *name)
45{
46 struct cds_hlist_head *head;
47 struct cds_hlist_node *node;
4e48b5d2 48 struct lttng_ust_registered_context_provider *reg_provider;
53569322
MD
49 uint32_t hash;
50 const char *end;
51 size_t len;
52
53 /* Lookup using everything before first ':' as key. */
54 end = strchr(name, ':');
55 if (end)
56 len = end - name;
57 else
58 len = strlen(name);
59 hash = jhash(name, len, 0);
60 head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
4e48b5d2
MD
61 cds_hlist_for_each_entry(reg_provider, node, head, node) {
62 if (!strncmp(reg_provider->provider->name, name, len))
63 return reg_provider->provider;
53569322
MD
64 }
65 return NULL;
66}
67
4e48b5d2 68struct lttng_ust_registered_context_provider *lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider)
53569322 69{
4e48b5d2 70 struct lttng_ust_registered_context_provider *reg_provider = NULL;
53569322
MD
71 struct cds_hlist_head *head;
72 size_t name_len = strlen(provider->name);
73 uint32_t hash;
53569322 74
c362addf
MD
75 lttng_ust_fixup_tls();
76
53569322 77 /* Provider name starts with "$app.". */
fe94775b 78 if (strncmp("$app.", provider->name, strlen("$app.")) != 0)
4e48b5d2 79 return NULL;
8b05d0d4 80 /* Provider name cannot contain a colon character. */
53569322 81 if (strchr(provider->name, ':'))
4e48b5d2
MD
82 return NULL;
83 if (ust_lock())
53569322 84 goto end;
4e48b5d2 85 if (lookup_provider_by_name(provider->name))
53569322 86 goto end;
4e48b5d2
MD
87 reg_provider = zmalloc(sizeof(struct lttng_ust_registered_context_provider));
88 if (!reg_provider)
89 goto end;
90 reg_provider->provider = provider;
53569322
MD
91 hash = jhash(provider->name, name_len, 0);
92 head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
4e48b5d2 93 cds_hlist_add_head(&reg_provider->node, head);
d8d2416d 94
53569322
MD
95 lttng_ust_context_set_session_provider(provider->name,
96 provider->get_size, provider->record,
4e48b5d2 97 provider->get_value, provider->priv);
d8d2416d
FD
98
99 lttng_ust_context_set_event_notifier_group_provider(provider->name,
100 provider->get_size, provider->record,
4e48b5d2 101 provider->get_value, provider->priv);
53569322
MD
102end:
103 ust_unlock();
4e48b5d2 104 return reg_provider;
53569322
MD
105}
106
4e48b5d2 107void lttng_ust_context_provider_unregister(struct lttng_ust_registered_context_provider *reg_provider)
53569322 108{
c362addf
MD
109 lttng_ust_fixup_tls();
110
53569322
MD
111 if (ust_lock())
112 goto end;
4e48b5d2 113 lttng_ust_context_set_session_provider(reg_provider->provider->name,
ce7352a2 114 lttng_ust_dummy_get_size, lttng_ust_dummy_record,
4e48b5d2 115 lttng_ust_dummy_get_value, NULL);
d8d2416d 116
4e48b5d2 117 lttng_ust_context_set_event_notifier_group_provider(reg_provider->provider->name,
d8d2416d 118 lttng_ust_dummy_get_size, lttng_ust_dummy_record,
4e48b5d2 119 lttng_ust_dummy_get_value, NULL);
d8d2416d 120
4e48b5d2 121 cds_hlist_del(&reg_provider->node);
53569322
MD
122end:
123 ust_unlock();
4e48b5d2 124 free(reg_provider);
53569322
MD
125}
126
f26f2f9b
MD
127static void destroy_app_ctx(void *priv)
128{
129 struct lttng_ust_app_ctx *app_ctx = (struct lttng_ust_app_ctx *) priv;
130
131 free(app_ctx->name);
132 free(app_ctx->event_field);
133 free(app_ctx->type);
134 free(app_ctx);
135}
136
53569322
MD
137/*
138 * Called with ust mutex held.
139 * Add application context to array of context, even if the application
140 * context is not currently loaded by application. It will then use the
141 * dummy callbacks in that case.
142 * Always performed before tracing is started, since it modifies
143 * metadata describing the context.
144 */
145int lttng_ust_add_app_context_to_ctx_rcu(const char *name,
daacdbfc 146 struct lttng_ust_ctx **ctx)
53569322 147{
4e48b5d2 148 const struct lttng_ust_context_provider *provider;
f26f2f9b 149 struct lttng_ust_ctx_field new_field = { 0 };
4e48b5d2
MD
150 struct lttng_ust_event_field *event_field = NULL;
151 struct lttng_ust_type_common *type = NULL;
f26f2f9b
MD
152 struct lttng_ust_app_ctx *app_ctx = NULL;
153 char *ctx_name;
53569322
MD
154 int ret;
155
156 if (*ctx && lttng_find_context(*ctx, name))
157 return -EEXIST;
4e48b5d2
MD
158 event_field = zmalloc(sizeof(struct lttng_ust_event_field));
159 if (!event_field) {
daacdbfc
MD
160 ret = -ENOMEM;
161 goto error_event_field_alloc;
162 }
f26f2f9b
MD
163 ctx_name = strdup(name);
164 if (!ctx_name) {
daacdbfc
MD
165 ret = -ENOMEM;
166 goto error_field_name_alloc;
167 }
4e48b5d2
MD
168 type = zmalloc(sizeof(struct lttng_ust_type_common));
169 if (!type) {
a084756d
MD
170 ret = -ENOMEM;
171 goto error_field_type_alloc;
172 }
f26f2f9b
MD
173 app_ctx = zmalloc(sizeof(struct lttng_ust_app_ctx));
174 if (!app_ctx) {
175 ret = -ENOMEM;
176 goto error_app_ctx_alloc;
177 }
178 event_field->name = ctx_name;
4e48b5d2
MD
179 type->type = lttng_ust_type_dynamic;
180 event_field->type = type;
f26f2f9b 181 new_field.event_field = event_field;
53569322
MD
182 /*
183 * If provider is not found, we add the context anyway, but
184 * it will provide a dummy context.
185 */
186 provider = lookup_provider_by_name(name);
187 if (provider) {
f26f2f9b
MD
188 new_field.get_size = provider->get_size;
189 new_field.record = provider->record;
190 new_field.get_value = provider->get_value;
53569322 191 } else {
f26f2f9b
MD
192 new_field.get_size = lttng_ust_dummy_get_size;
193 new_field.record = lttng_ust_dummy_record;
194 new_field.get_value = lttng_ust_dummy_get_value;
53569322 195 }
f26f2f9b
MD
196 new_field.destroy = destroy_app_ctx;
197 new_field.priv = app_ctx;
daacdbfc
MD
198 /*
199 * For application context, add it by expanding
f26f2f9b 200 * ctx array.
daacdbfc 201 */
f26f2f9b 202 ret = lttng_ust_context_append_rcu(ctx, &new_field);
53569322 203 if (ret) {
f26f2f9b 204 destroy_app_ctx(app_ctx);
53569322
MD
205 return ret;
206 }
207 return 0;
daacdbfc 208
f26f2f9b
MD
209error_app_ctx_alloc:
210 free(type);
a084756d 211error_field_type_alloc:
f26f2f9b 212 free(ctx_name);
daacdbfc 213error_field_name_alloc:
4e48b5d2 214 free(event_field);
daacdbfc 215error_event_field_alloc:
daacdbfc 216 return ret;
53569322 217}
This page took 0.037289 seconds and 4 git commands to generate.