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 | ||
23 | #include <sys/types.h> | |
24 | #include <unistd.h> | |
25 | #include <lttng/ust-context-provider.h> | |
26 | #include "lttng-tracer-core.h" | |
27 | #include "jhash.h" | |
28 | #include <helper.h> | |
29 | ||
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]; | |
34 | }; | |
35 | ||
36 | static struct context_provider_ht context_provider_ht; | |
37 | ||
38 | static struct lttng_ust_context_provider * | |
39 | lookup_provider_by_name(const char *name) | |
40 | { | |
41 | struct cds_hlist_head *head; | |
42 | struct cds_hlist_node *node; | |
43 | struct lttng_ust_context_provider *provider; | |
44 | uint32_t hash; | |
45 | const char *end; | |
46 | size_t len; | |
47 | ||
48 | /* Lookup using everything before first ':' as key. */ | |
49 | end = strchr(name, ':'); | |
50 | if (end) | |
51 | len = end - name; | |
52 | else | |
53 | len = strlen(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)) | |
58 | return provider; | |
59 | } | |
60 | return NULL; | |
61 | } | |
62 | ||
63 | int lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider) | |
64 | { | |
65 | struct cds_hlist_head *head; | |
66 | size_t name_len = strlen(provider->name); | |
67 | uint32_t hash; | |
68 | int ret = 0; | |
69 | ||
70 | /* Provider name starts with "$app.". */ | |
71 | if (strncmp("$app.", provider->name, strlen("$app.") != 0)) | |
72 | return -EINVAL; | |
73 | /* Provider name cannot contain a column character. */ | |
74 | if (strchr(provider->name, ':')) | |
75 | return -EINVAL; | |
76 | if (ust_lock()) { | |
77 | ret = -EBUSY; | |
78 | goto end; | |
79 | } | |
80 | if (lookup_provider_by_name(provider->name)) { | |
81 | ret = -EBUSY; | |
82 | goto end; | |
83 | } | |
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, | |
89 | provider->get_value); | |
90 | end: | |
91 | ust_unlock(); | |
92 | return ret; | |
93 | } | |
94 | ||
95 | static | |
96 | size_t dummy_get_size(struct lttng_ctx_field *field, size_t offset) | |
97 | { | |
98 | size_t size = 0; | |
99 | ||
100 | size += lib_ring_buffer_align(offset, lttng_alignof(char)); | |
101 | size += sizeof(char); /* tag */ | |
102 | return size; | |
103 | } | |
104 | ||
105 | static | |
106 | void dummy_record(struct lttng_ctx_field *field, | |
107 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
108 | struct lttng_channel *chan) | |
109 | { | |
110 | char sel_char = (char) LTTNG_UST_DYNAMIC_TYPE_NONE; | |
111 | ||
112 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(sel_char)); | |
113 | chan->ops->event_write(ctx, &sel_char, sizeof(sel_char)); | |
114 | } | |
115 | ||
116 | static | |
117 | void dummy_get_value(struct lttng_ctx_field *field, | |
118 | struct lttng_ctx_value *value) | |
119 | { | |
120 | value->sel = LTTNG_UST_DYNAMIC_TYPE_NONE; | |
121 | } | |
122 | ||
123 | void lttng_ust_context_provider_unregister(struct lttng_ust_context_provider *provider) | |
124 | { | |
125 | if (ust_lock()) | |
126 | goto end; | |
127 | lttng_ust_context_set_session_provider(provider->name, | |
128 | dummy_get_size, dummy_record, dummy_get_value); | |
129 | cds_hlist_del(&provider->node); | |
130 | end: | |
131 | ust_unlock(); | |
132 | } | |
133 | ||
134 | /* | |
135 | * Called with ust mutex held. | |
136 | * Add application context to array of context, even if the application | |
137 | * context is not currently loaded by application. It will then use the | |
138 | * dummy callbacks in that case. | |
139 | * Always performed before tracing is started, since it modifies | |
140 | * metadata describing the context. | |
141 | */ | |
142 | int lttng_ust_add_app_context_to_ctx_rcu(const char *name, | |
143 | struct lttng_ctx **ctx) | |
144 | { | |
145 | struct lttng_ust_context_provider *provider; | |
146 | struct lttng_ctx_field new_field; | |
147 | int ret; | |
148 | ||
149 | if (*ctx && lttng_find_context(*ctx, name)) | |
150 | return -EEXIST; | |
151 | /* | |
152 | * For application context, add it by expanding | |
153 | * ctx array. | |
154 | */ | |
155 | memset(&new_field, 0, sizeof(new_field)); | |
156 | new_field.field_name = strdup(name); | |
157 | if (!new_field.field_name) | |
158 | return -ENOMEM; | |
159 | new_field.event_field.name = new_field.field_name; | |
160 | new_field.event_field.type.atype = atype_dynamic; | |
161 | /* | |
162 | * If provider is not found, we add the context anyway, but | |
163 | * it will provide a dummy context. | |
164 | */ | |
165 | provider = lookup_provider_by_name(name); | |
166 | if (provider) { | |
167 | new_field.get_size = provider->get_size; | |
168 | new_field.record = provider->record; | |
169 | new_field.get_value = provider->get_value; | |
170 | } else { | |
171 | new_field.get_size = dummy_get_size; | |
172 | new_field.record = dummy_record; | |
173 | new_field.get_value = dummy_get_value; | |
174 | } | |
175 | ret = lttng_context_add_rcu(ctx, &new_field); | |
176 | if (ret) { | |
177 | free(new_field.field_name); | |
178 | return ret; | |
179 | } | |
180 | return 0; | |
181 | } |