2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng UST application context provider.
12 #include <sys/types.h>
15 #include <common/ust-context-provider.h>
17 #include "context-internal.h"
18 #include "lttng-tracer-core.h"
20 #include "context-provider-internal.h"
21 #include "common/macros.h"
23 struct lttng_ust_registered_context_provider
{
24 const struct lttng_ust_context_provider
*provider
;
26 struct cds_hlist_node node
;
29 struct lttng_ust_app_ctx
{
31 struct lttng_ust_event_field
*event_field
;
32 struct lttng_ust_type_common
*type
;
35 #define CONTEXT_PROVIDER_HT_BITS 12
36 #define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS)
37 struct context_provider_ht
{
38 struct cds_hlist_head table
[CONTEXT_PROVIDER_HT_SIZE
];
41 static struct context_provider_ht context_provider_ht
;
43 static const struct lttng_ust_context_provider
*
44 lookup_provider_by_name(const char *name
)
46 struct cds_hlist_head
*head
;
47 struct cds_hlist_node
*node
;
48 struct lttng_ust_registered_context_provider
*reg_provider
;
53 /* Lookup using everything before first ':' as key. */
54 end
= strchr(name
, ':');
59 hash
= jhash(name
, len
, 0);
60 head
= &context_provider_ht
.table
[hash
& (CONTEXT_PROVIDER_HT_SIZE
- 1)];
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
;
68 struct lttng_ust_registered_context_provider
*lttng_ust_context_provider_register(struct lttng_ust_context_provider
*provider
)
70 struct lttng_ust_registered_context_provider
*reg_provider
= NULL
;
71 struct cds_hlist_head
*head
;
72 size_t name_len
= strlen(provider
->name
);
75 lttng_ust_fixup_tls();
77 /* Provider name starts with "$app.". */
78 if (strncmp("$app.", provider
->name
, strlen("$app.")) != 0)
80 /* Provider name cannot contain a colon character. */
81 if (strchr(provider
->name
, ':'))
85 if (lookup_provider_by_name(provider
->name
))
87 reg_provider
= zmalloc(sizeof(struct lttng_ust_registered_context_provider
));
90 reg_provider
->provider
= provider
;
91 hash
= jhash(provider
->name
, name_len
, 0);
92 head
= &context_provider_ht
.table
[hash
& (CONTEXT_PROVIDER_HT_SIZE
- 1)];
93 cds_hlist_add_head(®_provider
->node
, head
);
95 lttng_ust_context_set_session_provider(provider
->name
,
96 provider
->get_size
, provider
->record
,
97 provider
->get_value
, provider
->priv
);
99 lttng_ust_context_set_event_notifier_group_provider(provider
->name
,
100 provider
->get_size
, provider
->record
,
101 provider
->get_value
, provider
->priv
);
107 void lttng_ust_context_provider_unregister(struct lttng_ust_registered_context_provider
*reg_provider
)
109 lttng_ust_fixup_tls();
113 lttng_ust_context_set_session_provider(reg_provider
->provider
->name
,
114 lttng_ust_dummy_get_size
, lttng_ust_dummy_record
,
115 lttng_ust_dummy_get_value
, NULL
);
117 lttng_ust_context_set_event_notifier_group_provider(reg_provider
->provider
->name
,
118 lttng_ust_dummy_get_size
, lttng_ust_dummy_record
,
119 lttng_ust_dummy_get_value
, NULL
);
121 cds_hlist_del(®_provider
->node
);
127 static void destroy_app_ctx(void *priv
)
129 struct lttng_ust_app_ctx
*app_ctx
= (struct lttng_ust_app_ctx
*) priv
;
132 free(app_ctx
->event_field
);
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.
145 int lttng_ust_add_app_context_to_ctx_rcu(const char *name
,
146 struct lttng_ust_ctx
**ctx
)
148 const struct lttng_ust_context_provider
*provider
;
149 struct lttng_ust_ctx_field new_field
= { 0 };
150 struct lttng_ust_event_field
*event_field
= NULL
;
151 struct lttng_ust_type_common
*type
= NULL
;
152 struct lttng_ust_app_ctx
*app_ctx
= NULL
;
156 if (*ctx
&& lttng_find_context(*ctx
, name
))
158 event_field
= zmalloc(sizeof(struct lttng_ust_event_field
));
161 goto error_event_field_alloc
;
163 ctx_name
= strdup(name
);
166 goto error_field_name_alloc
;
168 type
= zmalloc(sizeof(struct lttng_ust_type_common
));
171 goto error_field_type_alloc
;
173 app_ctx
= zmalloc(sizeof(struct lttng_ust_app_ctx
));
176 goto error_app_ctx_alloc
;
178 event_field
->name
= ctx_name
;
179 type
->type
= lttng_ust_type_dynamic
;
180 event_field
->type
= type
;
181 new_field
.event_field
= event_field
;
183 * If provider is not found, we add the context anyway, but
184 * it will provide a dummy context.
186 provider
= lookup_provider_by_name(name
);
188 new_field
.get_size
= provider
->get_size
;
189 new_field
.record
= provider
->record
;
190 new_field
.get_value
= provider
->get_value
;
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
;
196 new_field
.destroy
= destroy_app_ctx
;
197 new_field
.priv
= app_ctx
;
199 * For application context, add it by expanding
202 ret
= lttng_ust_context_append_rcu(ctx
, &new_field
);
204 destroy_app_ctx(app_ctx
);
211 error_field_type_alloc
:
213 error_field_name_alloc
:
215 error_event_field_alloc
: