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
23 #include <sys/types.h>
25 #include <lttng/ust-context-provider.h>
26 #include "lttng-tracer-core.h"
30 #define CONTEXT_PROVIDER_HT_BITS 12
31 #define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS)
32 struct context_provider_ht
{
33 struct cds_hlist_head table
[CONTEXT_PROVIDER_HT_SIZE
];
36 static struct context_provider_ht context_provider_ht
;
38 static struct lttng_ust_context_provider
*
39 lookup_provider_by_name(const char *name
)
41 struct cds_hlist_head
*head
;
42 struct cds_hlist_node
*node
;
43 struct lttng_ust_context_provider
*provider
;
48 /* Lookup using everything before first ':' as key. */
49 end
= strchr(name
, ':');
54 hash
= jhash(name
, len
, 0);
55 head
= &context_provider_ht
.table
[hash
& (CONTEXT_PROVIDER_HT_SIZE
- 1)];
56 cds_hlist_for_each_entry(provider
, node
, head
, node
) {
57 if (!strncmp(provider
->name
, name
, len
))
63 int lttng_ust_context_provider_register(struct lttng_ust_context_provider
*provider
)
65 struct cds_hlist_head
*head
;
66 size_t name_len
= strlen(provider
->name
);
70 /* Provider name starts with "$app.". */
71 if (strncmp("$app.", provider
->name
, strlen("$app.") != 0))
73 /* Provider name cannot contain a column character. */
74 if (strchr(provider
->name
, ':'))
80 if (lookup_provider_by_name(provider
->name
)) {
84 hash
= jhash(provider
->name
, name_len
, 0);
85 head
= &context_provider_ht
.table
[hash
& (CONTEXT_PROVIDER_HT_SIZE
- 1)];
86 cds_hlist_add_head(&provider
->node
, head
);
87 lttng_ust_context_set_session_provider(provider
->name
,
88 provider
->get_size
, provider
->record
,
95 void lttng_ust_context_provider_unregister(struct lttng_ust_context_provider
*provider
)
99 lttng_ust_context_set_session_provider(provider
->name
,
100 lttng_ust_dummy_get_size
, lttng_ust_dummy_record
,
101 lttng_ust_dummy_get_value
);
102 cds_hlist_del(&provider
->node
);
108 * Called with ust mutex held.
109 * Add application context to array of context, even if the application
110 * context is not currently loaded by application. It will then use the
111 * dummy callbacks in that case.
112 * Always performed before tracing is started, since it modifies
113 * metadata describing the context.
115 int lttng_ust_add_app_context_to_ctx_rcu(const char *name
,
116 struct lttng_ctx
**ctx
)
118 struct lttng_ust_context_provider
*provider
;
119 struct lttng_ctx_field new_field
;
122 if (*ctx
&& lttng_find_context(*ctx
, name
))
125 * For application context, add it by expanding
128 memset(&new_field
, 0, sizeof(new_field
));
129 new_field
.field_name
= strdup(name
);
130 if (!new_field
.field_name
)
132 new_field
.event_field
.name
= new_field
.field_name
;
133 new_field
.event_field
.type
.atype
= atype_dynamic
;
135 * If provider is not found, we add the context anyway, but
136 * it will provide a dummy context.
138 provider
= lookup_provider_by_name(name
);
140 new_field
.get_size
= provider
->get_size
;
141 new_field
.record
= provider
->record
;
142 new_field
.get_value
= provider
->get_value
;
144 new_field
.get_size
= lttng_ust_dummy_get_size
;
145 new_field
.record
= lttng_ust_dummy_record
;
146 new_field
.get_value
= lttng_ust_dummy_get_value
;
148 ret
= lttng_context_add_rcu(ctx
, &new_field
);
150 free(new_field
.field_name
);