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 <ust-context-provider.h>
17 #include "context-internal.h"
18 #include "lttng-tracer-core.h"
20 #include "context-provider-internal.h"
21 #include <ust-helper.h>
23 struct lttng_ust_registered_context_provider
{
24 const struct lttng_ust_context_provider
*provider
;
26 struct cds_hlist_node node
;
29 #define CONTEXT_PROVIDER_HT_BITS 12
30 #define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS)
31 struct context_provider_ht
{
32 struct cds_hlist_head table
[CONTEXT_PROVIDER_HT_SIZE
];
35 static struct context_provider_ht context_provider_ht
;
37 static const struct lttng_ust_context_provider
*
38 lookup_provider_by_name(const char *name
)
40 struct cds_hlist_head
*head
;
41 struct cds_hlist_node
*node
;
42 struct lttng_ust_registered_context_provider
*reg_provider
;
47 /* Lookup using everything before first ':' as key. */
48 end
= strchr(name
, ':');
53 hash
= jhash(name
, len
, 0);
54 head
= &context_provider_ht
.table
[hash
& (CONTEXT_PROVIDER_HT_SIZE
- 1)];
55 cds_hlist_for_each_entry(reg_provider
, node
, head
, node
) {
56 if (!strncmp(reg_provider
->provider
->name
, name
, len
))
57 return reg_provider
->provider
;
62 struct lttng_ust_registered_context_provider
*lttng_ust_context_provider_register(struct lttng_ust_context_provider
*provider
)
64 struct lttng_ust_registered_context_provider
*reg_provider
= NULL
;
65 struct cds_hlist_head
*head
;
66 size_t name_len
= strlen(provider
->name
);
69 lttng_ust_fixup_tls();
71 /* Provider name starts with "$app.". */
72 if (strncmp("$app.", provider
->name
, strlen("$app.")) != 0)
74 /* Provider name cannot contain a colon character. */
75 if (strchr(provider
->name
, ':'))
79 if (lookup_provider_by_name(provider
->name
))
81 reg_provider
= zmalloc(sizeof(struct lttng_ust_registered_context_provider
));
84 reg_provider
->provider
= provider
;
85 hash
= jhash(provider
->name
, name_len
, 0);
86 head
= &context_provider_ht
.table
[hash
& (CONTEXT_PROVIDER_HT_SIZE
- 1)];
87 cds_hlist_add_head(®_provider
->node
, head
);
89 lttng_ust_context_set_session_provider(provider
->name
,
90 provider
->get_size
, provider
->record
,
91 provider
->get_value
, provider
->priv
);
93 lttng_ust_context_set_event_notifier_group_provider(provider
->name
,
94 provider
->get_size
, provider
->record
,
95 provider
->get_value
, provider
->priv
);
101 void lttng_ust_context_provider_unregister(struct lttng_ust_registered_context_provider
*reg_provider
)
103 lttng_ust_fixup_tls();
107 lttng_ust_context_set_session_provider(reg_provider
->provider
->name
,
108 lttng_ust_dummy_get_size
, lttng_ust_dummy_record
,
109 lttng_ust_dummy_get_value
, NULL
);
111 lttng_ust_context_set_event_notifier_group_provider(reg_provider
->provider
->name
,
112 lttng_ust_dummy_get_size
, lttng_ust_dummy_record
,
113 lttng_ust_dummy_get_value
, NULL
);
115 cds_hlist_del(®_provider
->node
);
122 * Called with ust mutex held.
123 * Add application context to array of context, even if the application
124 * context is not currently loaded by application. It will then use the
125 * dummy callbacks in that case.
126 * Always performed before tracing is started, since it modifies
127 * metadata describing the context.
129 int lttng_ust_add_app_context_to_ctx_rcu(const char *name
,
130 struct lttng_ust_ctx
**ctx
)
132 const struct lttng_ust_context_provider
*provider
;
133 struct lttng_ust_ctx_field
*new_field
= NULL
;
134 struct lttng_ust_event_field
*event_field
= NULL
;
135 struct lttng_ust_type_common
*type
= NULL
;
138 if (*ctx
&& lttng_find_context(*ctx
, name
))
140 new_field
= zmalloc(sizeof(struct lttng_ust_ctx_field
));
143 goto error_field_alloc
;
145 event_field
= zmalloc(sizeof(struct lttng_ust_event_field
));
148 goto error_event_field_alloc
;
150 event_field
->name
= strdup(name
);
151 if (!event_field
->name
) {
153 goto error_field_name_alloc
;
155 type
= zmalloc(sizeof(struct lttng_ust_type_common
));
158 goto error_field_type_alloc
;
160 type
->type
= lttng_ust_type_dynamic
;
161 event_field
->type
= type
;
162 new_field
->event_field
= event_field
;
164 * If provider is not found, we add the context anyway, but
165 * it will provide a dummy context.
167 provider
= lookup_provider_by_name(name
);
169 new_field
->get_size
= provider
->get_size
;
170 new_field
->record
= provider
->record
;
171 new_field
->get_value
= provider
->get_value
;
173 new_field
->get_size
= lttng_ust_dummy_get_size
;
174 new_field
->record
= lttng_ust_dummy_record
;
175 new_field
->get_value
= lttng_ust_dummy_get_value
;
178 * For application context, add it by expanding
179 * ctx array. Ownership of new_field is passed to the callee on
182 ret
= lttng_ust_context_append_rcu(ctx
, new_field
);
185 free((char *) new_field
->event_field
->name
);
192 error_field_type_alloc
:
193 free((char *) new_field
->event_field
->name
);
194 error_field_name_alloc
:
196 error_event_field_alloc
: