Commit | Line | Data |
---|---|---|
53569322 MD |
1 | /* |
2 | * lttng-context-provider.c | |
3 | * | |
4 | * LTTng UST application context provider. | |
5 | * | |
6 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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 | |
21 | */ | |
22 | ||
3fbec7dc | 23 | #define _LGPL_SOURCE |
b4051ad8 | 24 | #include <stddef.h> |
fb31eb73 | 25 | #include <stdint.h> |
53569322 MD |
26 | #include <sys/types.h> |
27 | #include <unistd.h> | |
fb31eb73 | 28 | |
53569322 MD |
29 | #include <lttng/ust-context-provider.h> |
30 | #include "lttng-tracer-core.h" | |
31 | #include "jhash.h" | |
32 | #include <helper.h> | |
33 | ||
34 | #define CONTEXT_PROVIDER_HT_BITS 12 | |
35 | #define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS) | |
36 | struct context_provider_ht { | |
37 | struct cds_hlist_head table[CONTEXT_PROVIDER_HT_SIZE]; | |
38 | }; | |
39 | ||
40 | static struct context_provider_ht context_provider_ht; | |
41 | ||
42 | static struct lttng_ust_context_provider * | |
43 | lookup_provider_by_name(const char *name) | |
44 | { | |
45 | struct cds_hlist_head *head; | |
46 | struct cds_hlist_node *node; | |
47 | struct lttng_ust_context_provider *provider; | |
48 | uint32_t hash; | |
49 | const char *end; | |
50 | size_t len; | |
51 | ||
52 | /* Lookup using everything before first ':' as key. */ | |
53 | end = strchr(name, ':'); | |
54 | if (end) | |
55 | len = end - name; | |
56 | else | |
57 | len = strlen(name); | |
58 | hash = jhash(name, len, 0); | |
59 | head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)]; | |
60 | cds_hlist_for_each_entry(provider, node, head, node) { | |
61 | if (!strncmp(provider->name, name, len)) | |
62 | return provider; | |
63 | } | |
64 | return NULL; | |
65 | } | |
66 | ||
67 | int lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider) | |
68 | { | |
69 | struct cds_hlist_head *head; | |
70 | size_t name_len = strlen(provider->name); | |
71 | uint32_t hash; | |
72 | int ret = 0; | |
73 | ||
c362addf MD |
74 | lttng_ust_fixup_tls(); |
75 | ||
53569322 | 76 | /* Provider name starts with "$app.". */ |
fe94775b | 77 | if (strncmp("$app.", provider->name, strlen("$app.")) != 0) |
53569322 | 78 | return -EINVAL; |
8b05d0d4 | 79 | /* Provider name cannot contain a colon character. */ |
53569322 MD |
80 | if (strchr(provider->name, ':')) |
81 | return -EINVAL; | |
82 | if (ust_lock()) { | |
83 | ret = -EBUSY; | |
84 | goto end; | |
85 | } | |
86 | if (lookup_provider_by_name(provider->name)) { | |
87 | ret = -EBUSY; | |
88 | goto end; | |
89 | } | |
90 | hash = jhash(provider->name, name_len, 0); | |
91 | head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)]; | |
92 | cds_hlist_add_head(&provider->node, head); | |
93 | lttng_ust_context_set_session_provider(provider->name, | |
94 | provider->get_size, provider->record, | |
95 | provider->get_value); | |
96 | end: | |
97 | ust_unlock(); | |
98 | return ret; | |
99 | } | |
100 | ||
53569322 MD |
101 | void lttng_ust_context_provider_unregister(struct lttng_ust_context_provider *provider) |
102 | { | |
c362addf MD |
103 | lttng_ust_fixup_tls(); |
104 | ||
53569322 MD |
105 | if (ust_lock()) |
106 | goto end; | |
107 | lttng_ust_context_set_session_provider(provider->name, | |
ce7352a2 MD |
108 | lttng_ust_dummy_get_size, lttng_ust_dummy_record, |
109 | lttng_ust_dummy_get_value); | |
53569322 MD |
110 | cds_hlist_del(&provider->node); |
111 | end: | |
112 | ust_unlock(); | |
113 | } | |
114 | ||
115 | /* | |
116 | * Called with ust mutex held. | |
117 | * Add application context to array of context, even if the application | |
118 | * context is not currently loaded by application. It will then use the | |
119 | * dummy callbacks in that case. | |
120 | * Always performed before tracing is started, since it modifies | |
121 | * metadata describing the context. | |
122 | */ | |
123 | int lttng_ust_add_app_context_to_ctx_rcu(const char *name, | |
124 | struct lttng_ctx **ctx) | |
125 | { | |
126 | struct lttng_ust_context_provider *provider; | |
127 | struct lttng_ctx_field new_field; | |
128 | int ret; | |
129 | ||
130 | if (*ctx && lttng_find_context(*ctx, name)) | |
131 | return -EEXIST; | |
132 | /* | |
133 | * For application context, add it by expanding | |
134 | * ctx array. | |
135 | */ | |
136 | memset(&new_field, 0, sizeof(new_field)); | |
137 | new_field.field_name = strdup(name); | |
138 | if (!new_field.field_name) | |
139 | return -ENOMEM; | |
140 | new_field.event_field.name = new_field.field_name; | |
141 | new_field.event_field.type.atype = atype_dynamic; | |
142 | /* | |
143 | * If provider is not found, we add the context anyway, but | |
144 | * it will provide a dummy context. | |
145 | */ | |
146 | provider = lookup_provider_by_name(name); | |
147 | if (provider) { | |
148 | new_field.get_size = provider->get_size; | |
149 | new_field.record = provider->record; | |
150 | new_field.get_value = provider->get_value; | |
151 | } else { | |
ce7352a2 MD |
152 | new_field.get_size = lttng_ust_dummy_get_size; |
153 | new_field.record = lttng_ust_dummy_record; | |
154 | new_field.get_value = lttng_ust_dummy_get_value; | |
53569322 MD |
155 | } |
156 | ret = lttng_context_add_rcu(ctx, &new_field); | |
157 | if (ret) { | |
158 | free(new_field.field_name); | |
159 | return ret; | |
160 | } | |
161 | return 0; | |
162 | } |