Move all sources to 'src/'
[lttng-ust.git] / src / liblttng-ust / lttng-context.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng UST trace/channel/event context management.
7 */
8
9 #define _LGPL_SOURCE
10 #include <lttng/ust-events.h>
11 #include <lttng/ust-tracer.h>
12 #include <ust-context-provider.h>
13 #include <lttng/urcu/pointer.h>
14 #include <lttng/urcu/urcu-ust.h>
15 #include <usterr-signal-safe.h>
16 #include <ust-helper.h>
17 #include <stddef.h>
18 #include <string.h>
19 #include <assert.h>
20 #include <limits.h>
21 #include "tracepoint-internal.h"
22
23 #include "context-internal.h"
24
25 /*
26 * The filter implementation requires that two consecutive "get" for the
27 * same context performed by the same thread return the same result.
28 */
29
30 int lttng_find_context(struct lttng_ust_ctx *ctx, const char *name)
31 {
32 unsigned int i;
33 const char *subname;
34
35 if (!ctx)
36 return 0;
37 if (strncmp(name, "$ctx.", strlen("$ctx.")) == 0) {
38 subname = name + strlen("$ctx.");
39 } else {
40 subname = name;
41 }
42 for (i = 0; i < ctx->nr_fields; i++) {
43 /* Skip allocated (but non-initialized) contexts */
44 if (!ctx->fields[i].event_field->name)
45 continue;
46 if (!strcmp(ctx->fields[i].event_field->name, subname))
47 return 1;
48 }
49 return 0;
50 }
51
52 int lttng_get_context_index(struct lttng_ust_ctx *ctx, const char *name)
53 {
54 unsigned int i;
55 const char *subname;
56
57 if (!ctx)
58 return -1;
59 if (strncmp(name, "$ctx.", strlen("$ctx.")) == 0) {
60 subname = name + strlen("$ctx.");
61 } else {
62 subname = name;
63 }
64 for (i = 0; i < ctx->nr_fields; i++) {
65 /* Skip allocated (but non-initialized) contexts */
66 if (!ctx->fields[i].event_field->name)
67 continue;
68 if (!strcmp(ctx->fields[i].event_field->name, subname))
69 return i;
70 }
71 return -1;
72 }
73
74 static int lttng_find_context_provider(struct lttng_ust_ctx *ctx, const char *name)
75 {
76 unsigned int i;
77
78 for (i = 0; i < ctx->nr_fields; i++) {
79 /* Skip allocated (but non-initialized) contexts */
80 if (!ctx->fields[i].event_field->name)
81 continue;
82 if (!strncmp(ctx->fields[i].event_field->name, name,
83 strlen(name)))
84 return 1;
85 }
86 return 0;
87 }
88
89 /*
90 * Note: as we append context information, the pointer location may change.
91 * lttng_ust_context_add_field leaves the new last context initialized to NULL.
92 */
93 static
94 int lttng_ust_context_add_field(struct lttng_ust_ctx **ctx_p)
95 {
96 struct lttng_ust_ctx *ctx;
97
98 if (!*ctx_p) {
99 *ctx_p = zmalloc(sizeof(struct lttng_ust_ctx));
100 if (!*ctx_p)
101 return -ENOMEM;
102 (*ctx_p)->largest_align = 1;
103 }
104 ctx = *ctx_p;
105 if (ctx->nr_fields + 1 > ctx->allocated_fields) {
106 struct lttng_ust_ctx_field *new_fields;
107
108 ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields);
109 new_fields = zmalloc(ctx->allocated_fields * sizeof(*new_fields));
110 if (!new_fields)
111 return -ENOMEM;
112 /* Copy elements */
113 if (ctx->fields)
114 memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields);
115 free(ctx->fields);
116 ctx->fields = new_fields;
117 }
118 ctx->nr_fields++;
119 return 0;
120 }
121
122 static size_t get_type_max_align(const struct lttng_ust_type_common *type)
123 {
124 switch (type->type) {
125 case lttng_ust_type_integer:
126 return lttng_ust_get_type_integer(type)->alignment;
127 case lttng_ust_type_string:
128 return CHAR_BIT;
129 case lttng_ust_type_dynamic:
130 return 0;
131 case lttng_ust_type_enum:
132 return get_type_max_align(lttng_ust_get_type_enum(type)->container_type);
133 case lttng_ust_type_array:
134 return max_t(size_t, get_type_max_align(lttng_ust_get_type_array(type)->elem_type),
135 lttng_ust_get_type_array(type)->alignment);
136 case lttng_ust_type_sequence:
137 return max_t(size_t, get_type_max_align(lttng_ust_get_type_sequence(type)->elem_type),
138 lttng_ust_get_type_sequence(type)->alignment);
139 case lttng_ust_type_struct:
140 {
141 unsigned int i;
142 size_t field_align = 0;
143 const struct lttng_ust_type_struct *struct_type = lttng_ust_get_type_struct(type);
144
145 for (i = 0; i < struct_type->nr_fields; i++) {
146 field_align = max_t(size_t,
147 get_type_max_align(struct_type->fields[i]->type),
148 field_align);
149 }
150 return field_align;
151 }
152 default:
153 WARN_ON_ONCE(1);
154 return 0;
155 }
156 }
157
158 /*
159 * lttng_context_update() should be called at least once between context
160 * modification and trace start.
161 */
162 static
163 void lttng_context_update(struct lttng_ust_ctx *ctx)
164 {
165 int i;
166 size_t largest_align = 8; /* in bits */
167
168 for (i = 0; i < ctx->nr_fields; i++) {
169 size_t field_align = 8;
170
171 field_align = get_type_max_align(ctx->fields[i].event_field->type);
172 largest_align = max_t(size_t, largest_align, field_align);
173 }
174 ctx->largest_align = largest_align >> 3; /* bits to bytes */
175 }
176
177 int lttng_ust_context_append_rcu(struct lttng_ust_ctx **ctx_p,
178 const struct lttng_ust_ctx_field *f)
179 {
180 struct lttng_ust_ctx *old_ctx = *ctx_p, *new_ctx = NULL;
181 struct lttng_ust_ctx_field *new_fields = NULL;
182 int ret;
183
184 if (old_ctx) {
185 new_ctx = zmalloc(sizeof(struct lttng_ust_ctx));
186 if (!new_ctx)
187 return -ENOMEM;
188 *new_ctx = *old_ctx;
189 new_fields = zmalloc(new_ctx->allocated_fields * sizeof(*new_fields));
190 if (!new_fields) {
191 free(new_ctx);
192 return -ENOMEM;
193 }
194 /* Copy elements */
195 memcpy(new_fields, old_ctx->fields,
196 sizeof(*old_ctx->fields) * old_ctx->nr_fields);
197 new_ctx->fields = new_fields;
198 }
199 ret = lttng_ust_context_add_field(&new_ctx);
200 if (ret) {
201 free(new_fields);
202 free(new_ctx);
203 return ret;
204 }
205 new_ctx->fields[new_ctx->nr_fields - 1] = *f;
206 lttng_context_update(new_ctx);
207 lttng_ust_rcu_assign_pointer(*ctx_p, new_ctx);
208 lttng_ust_urcu_synchronize_rcu();
209 if (old_ctx) {
210 free(old_ctx->fields);
211 free(old_ctx);
212 }
213 return 0;
214 }
215
216 int lttng_ust_context_append(struct lttng_ust_ctx **ctx_p,
217 const struct lttng_ust_ctx_field *f)
218 {
219 int ret;
220
221 ret = lttng_ust_context_add_field(ctx_p);
222 if (ret)
223 return ret;
224 (*ctx_p)->fields[(*ctx_p)->nr_fields - 1] = *f;
225 lttng_context_update(*ctx_p);
226 return 0;
227 }
228
229 void lttng_destroy_context(struct lttng_ust_ctx *ctx)
230 {
231 int i;
232
233 if (!ctx)
234 return;
235 for (i = 0; i < ctx->nr_fields; i++) {
236 if (ctx->fields[i].destroy)
237 ctx->fields[i].destroy(ctx->fields[i].priv);
238 }
239 free(ctx->fields);
240 free(ctx);
241 }
242
243 /*
244 * Can be safely performed concurrently with tracing using the struct
245 * lttng_ctx. Using RCU update. Needs to match RCU read-side handling of
246 * contexts.
247 *
248 * This does not allow adding, removing, or changing typing of the
249 * contexts, since this needs to stay invariant for metadata. However,
250 * it allows updating the handlers associated with all contexts matching
251 * a provider (by name) while tracing is using it, in a way that ensures
252 * a single RCU read-side critical section see either all old, or all
253 * new handlers.
254 */
255 int lttng_ust_context_set_provider_rcu(struct lttng_ust_ctx **_ctx,
256 const char *name,
257 size_t (*get_size)(void *priv, size_t offset),
258 void (*record)(void *priv, struct lttng_ust_lib_ring_buffer_ctx *ctx,
259 struct lttng_ust_channel_buffer *chan),
260 void (*get_value)(void *priv, struct lttng_ust_ctx_value *value),
261 void *priv)
262 {
263 int i, ret;
264 struct lttng_ust_ctx *ctx = *_ctx, *new_ctx;
265 struct lttng_ust_ctx_field *new_fields;
266
267 if (!ctx || !lttng_find_context_provider(ctx, name))
268 return 0;
269 /*
270 * We have at least one instance of context for the provider.
271 */
272 new_ctx = zmalloc(sizeof(*new_ctx));
273 if (!new_ctx)
274 return -ENOMEM;
275 *new_ctx = *ctx;
276 new_fields = zmalloc(sizeof(*new_fields) * ctx->allocated_fields);
277 if (!new_fields) {
278 ret = -ENOMEM;
279 goto field_error;
280 }
281 /* Copy elements */
282 memcpy(new_fields, ctx->fields,
283 sizeof(*new_fields) * ctx->allocated_fields);
284 for (i = 0; i < ctx->nr_fields; i++) {
285 if (strncmp(new_fields[i].event_field->name,
286 name, strlen(name)) != 0)
287 continue;
288 new_fields[i].get_size = get_size;
289 new_fields[i].record = record;
290 new_fields[i].get_value = get_value;
291 new_fields[i].priv = priv;
292 }
293 new_ctx->fields = new_fields;
294 lttng_ust_rcu_assign_pointer(*_ctx, new_ctx);
295 lttng_ust_urcu_synchronize_rcu();
296 free(ctx->fields);
297 free(ctx);
298 return 0;
299
300 field_error:
301 free(new_ctx);
302 return ret;
303 }
304
305 int lttng_context_init_all(struct lttng_ust_ctx **ctx)
306 {
307 int ret;
308
309 ret = lttng_add_pthread_id_to_ctx(ctx);
310 if (ret) {
311 WARN("Cannot add context lttng_add_pthread_id_to_ctx");
312 goto error;
313 }
314 ret = lttng_add_vtid_to_ctx(ctx);
315 if (ret) {
316 WARN("Cannot add context lttng_add_vtid_to_ctx");
317 goto error;
318 }
319 ret = lttng_add_vpid_to_ctx(ctx);
320 if (ret) {
321 WARN("Cannot add context lttng_add_vpid_to_ctx");
322 goto error;
323 }
324 ret = lttng_add_procname_to_ctx(ctx);
325 if (ret) {
326 WARN("Cannot add context lttng_add_procname_to_ctx");
327 goto error;
328 }
329 ret = lttng_add_cpu_id_to_ctx(ctx);
330 if (ret) {
331 WARN("Cannot add context lttng_add_cpu_id_to_ctx");
332 goto error;
333 }
334 ret = lttng_add_cgroup_ns_to_ctx(ctx);
335 if (ret) {
336 WARN("Cannot add context lttng_add_cgroup_ns_to_ctx");
337 goto error;
338 }
339 ret = lttng_add_ipc_ns_to_ctx(ctx);
340 if (ret) {
341 WARN("Cannot add context lttng_add_ipc_ns_to_ctx");
342 goto error;
343 }
344 ret = lttng_add_mnt_ns_to_ctx(ctx);
345 if (ret) {
346 WARN("Cannot add context lttng_add_mnt_ns_to_ctx");
347 goto error;
348 }
349 ret = lttng_add_net_ns_to_ctx(ctx);
350 if (ret) {
351 WARN("Cannot add context lttng_add_net_ns_to_ctx");
352 goto error;
353 }
354 ret = lttng_add_pid_ns_to_ctx(ctx);
355 if (ret) {
356 WARN("Cannot add context lttng_add_pid_ns_to_ctx");
357 goto error;
358 }
359 ret = lttng_add_time_ns_to_ctx(ctx);
360 if (ret) {
361 WARN("Cannot add context lttng_add_time_ns_to_ctx");
362 goto error;
363 }
364 ret = lttng_add_user_ns_to_ctx(ctx);
365 if (ret) {
366 WARN("Cannot add context lttng_add_user_ns_to_ctx");
367 goto error;
368 }
369 ret = lttng_add_uts_ns_to_ctx(ctx);
370 if (ret) {
371 WARN("Cannot add context lttng_add_uts_ns_to_ctx");
372 goto error;
373 }
374 ret = lttng_add_vuid_to_ctx(ctx);
375 if (ret) {
376 WARN("Cannot add context lttng_add_vuid_to_ctx");
377 goto error;
378 }
379 ret = lttng_add_veuid_to_ctx(ctx);
380 if (ret) {
381 WARN("Cannot add context lttng_add_veuid_to_ctx");
382 goto error;
383 }
384 ret = lttng_add_vsuid_to_ctx(ctx);
385 if (ret) {
386 WARN("Cannot add context lttng_add_vsuid_to_ctx");
387 goto error;
388 }
389 ret = lttng_add_vgid_to_ctx(ctx);
390 if (ret) {
391 WARN("Cannot add context lttng_add_vgid_to_ctx");
392 goto error;
393 }
394 ret = lttng_add_vegid_to_ctx(ctx);
395 if (ret) {
396 WARN("Cannot add context lttng_add_vegid_to_ctx");
397 goto error;
398 }
399 ret = lttng_add_vsgid_to_ctx(ctx);
400 if (ret) {
401 WARN("Cannot add context lttng_add_vsgid_to_ctx");
402 goto error;
403 }
404 lttng_context_update(*ctx);
405 return 0;
406
407 error:
408 lttng_destroy_context(*ctx);
409 return ret;
410 }
This page took 0.03714 seconds and 4 git commands to generate.