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
26 #include <sys/types.h>
29 #include <lttng/ust-context-provider.h>
31 #include "lttng-tracer-core.h"
33 #include "context-provider-internal.h"
36 #define CONTEXT_PROVIDER_HT_BITS 12
37 #define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS)
38 struct context_provider_ht
{
39 struct cds_hlist_head table
[CONTEXT_PROVIDER_HT_SIZE
];
42 static struct context_provider_ht context_provider_ht
;
44 static struct lttng_ust_context_provider
*
45 lookup_provider_by_name(const char *name
)
47 struct cds_hlist_head
*head
;
48 struct cds_hlist_node
*node
;
49 struct lttng_ust_context_provider
*provider
;
54 /* Lookup using everything before first ':' as key. */
55 end
= strchr(name
, ':');
60 hash
= jhash(name
, len
, 0);
61 head
= &context_provider_ht
.table
[hash
& (CONTEXT_PROVIDER_HT_SIZE
- 1)];
62 cds_hlist_for_each_entry(provider
, node
, head
, node
) {
63 if (!strncmp(provider
->name
, name
, len
))
69 int lttng_ust_context_provider_register(struct lttng_ust_context_provider
*provider
)
71 struct cds_hlist_head
*head
;
72 size_t name_len
= strlen(provider
->name
);
76 lttng_ust_fixup_tls();
78 /* Provider name starts with "$app.". */
79 if (strncmp("$app.", provider
->name
, strlen("$app.")) != 0)
81 /* Provider name cannot contain a colon character. */
82 if (strchr(provider
->name
, ':'))
88 if (lookup_provider_by_name(provider
->name
)) {
92 hash
= jhash(provider
->name
, name_len
, 0);
93 head
= &context_provider_ht
.table
[hash
& (CONTEXT_PROVIDER_HT_SIZE
- 1)];
94 cds_hlist_add_head(&provider
->node
, head
);
96 lttng_ust_context_set_session_provider(provider
->name
,
97 provider
->get_size
, provider
->record
,
100 lttng_ust_context_set_event_notifier_group_provider(provider
->name
,
101 provider
->get_size
, provider
->record
,
102 provider
->get_value
);
108 void lttng_ust_context_provider_unregister(struct lttng_ust_context_provider
*provider
)
110 lttng_ust_fixup_tls();
114 lttng_ust_context_set_session_provider(provider
->name
,
115 lttng_ust_dummy_get_size
, lttng_ust_dummy_record
,
116 lttng_ust_dummy_get_value
);
118 lttng_ust_context_set_event_notifier_group_provider(provider
->name
,
119 lttng_ust_dummy_get_size
, lttng_ust_dummy_record
,
120 lttng_ust_dummy_get_value
);
122 cds_hlist_del(&provider
->node
);
128 * Called with ust mutex held.
129 * Add application context to array of context, even if the application
130 * context is not currently loaded by application. It will then use the
131 * dummy callbacks in that case.
132 * Always performed before tracing is started, since it modifies
133 * metadata describing the context.
135 int lttng_ust_add_app_context_to_ctx_rcu(const char *name
,
136 struct lttng_ctx
**ctx
)
138 struct lttng_ust_context_provider
*provider
;
139 struct lttng_ctx_field new_field
;
142 if (*ctx
&& lttng_find_context(*ctx
, name
))
145 * For application context, add it by expanding
148 memset(&new_field
, 0, sizeof(new_field
));
149 new_field
.field_name
= strdup(name
);
150 if (!new_field
.field_name
)
152 new_field
.event_field
.name
= new_field
.field_name
;
153 new_field
.event_field
.type
.atype
= atype_dynamic
;
155 * If provider is not found, we add the context anyway, but
156 * it will provide a dummy context.
158 provider
= lookup_provider_by_name(name
);
160 new_field
.get_size
= provider
->get_size
;
161 new_field
.record
= provider
->record
;
162 new_field
.get_value
= provider
->get_value
;
164 new_field
.get_size
= lttng_ust_dummy_get_size
;
165 new_field
.record
= lttng_ust_dummy_record
;
166 new_field
.get_value
= lttng_ust_dummy_get_value
;
168 ret
= lttng_context_add_rcu(ctx
, &new_field
);
170 free(new_field
.field_name
);