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