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