2 * lttng-context-provider.c
4 * LTTng UST application context provider.
6 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <sys/types.h>
26 #include <lttng/ust-context-provider.h>
27 #include "lttng-tracer-core.h"
31 #define CONTEXT_PROVIDER_HT_BITS 12
32 #define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS)
33 struct context_provider_ht
{
34 struct cds_hlist_head table
[CONTEXT_PROVIDER_HT_SIZE
];
37 static struct context_provider_ht context_provider_ht
;
39 static struct lttng_ust_context_provider
*
40 lookup_provider_by_name(const char *name
)
42 struct cds_hlist_head
*head
;
43 struct cds_hlist_node
*node
;
44 struct lttng_ust_context_provider
*provider
;
49 /* Lookup using everything before first ':' as key. */
50 end
= strchr(name
, ':');
55 hash
= jhash(name
, len
, 0);
56 head
= &context_provider_ht
.table
[hash
& (CONTEXT_PROVIDER_HT_SIZE
- 1)];
57 cds_hlist_for_each_entry(provider
, node
, head
, node
) {
58 if (!strncmp(provider
->name
, name
, len
))
64 int lttng_ust_context_provider_register(struct lttng_ust_context_provider
*provider
)
66 struct cds_hlist_head
*head
;
67 size_t name_len
= strlen(provider
->name
);
71 lttng_ust_fixup_tls();
73 /* Provider name starts with "$app.". */
74 if (strncmp("$app.", provider
->name
, strlen("$app.") != 0))
76 /* Provider name cannot contain a column character. */
77 if (strchr(provider
->name
, ':'))
83 if (lookup_provider_by_name(provider
->name
)) {
87 hash
= jhash(provider
->name
, name_len
, 0);
88 head
= &context_provider_ht
.table
[hash
& (CONTEXT_PROVIDER_HT_SIZE
- 1)];
89 cds_hlist_add_head(&provider
->node
, head
);
90 lttng_ust_context_set_session_provider(provider
->name
,
91 provider
->get_size
, provider
->record
,
98 void lttng_ust_context_provider_unregister(struct lttng_ust_context_provider
*provider
)
100 lttng_ust_fixup_tls();
104 lttng_ust_context_set_session_provider(provider
->name
,
105 lttng_ust_dummy_get_size
, lttng_ust_dummy_record
,
106 lttng_ust_dummy_get_value
);
107 cds_hlist_del(&provider
->node
);
113 * Called with ust mutex held.
114 * Add application context to array of context, even if the application
115 * context is not currently loaded by application. It will then use the
116 * dummy callbacks in that case.
117 * Always performed before tracing is started, since it modifies
118 * metadata describing the context.
120 int lttng_ust_add_app_context_to_ctx_rcu(const char *name
,
121 struct lttng_ctx
**ctx
)
123 struct lttng_ust_context_provider
*provider
;
124 struct lttng_ctx_field new_field
;
127 if (*ctx
&& lttng_find_context(*ctx
, name
))
130 * For application context, add it by expanding
133 memset(&new_field
, 0, sizeof(new_field
));
134 new_field
.field_name
= strdup(name
);
135 if (!new_field
.field_name
)
137 new_field
.event_field
.name
= new_field
.field_name
;
138 new_field
.event_field
.type
.atype
= atype_dynamic
;
140 * If provider is not found, we add the context anyway, but
141 * it will provide a dummy context.
143 provider
= lookup_provider_by_name(name
);
145 new_field
.get_size
= provider
->get_size
;
146 new_field
.record
= provider
->record
;
147 new_field
.get_value
= provider
->get_value
;
149 new_field
.get_size
= lttng_ust_dummy_get_size
;
150 new_field
.record
= lttng_ust_dummy_record
;
151 new_field
.get_value
= lttng_ust_dummy_get_value
;
153 ret
= lttng_context_add_rcu(ctx
, &new_field
);
155 free(new_field
.field_name
);