2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng UST trace/channel/event context management.
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>
20 #include "tracepoint-internal.h"
22 #include "context-internal.h"
25 * The filter implementation requires that two consecutive "get" for the
26 * same context performed by the same thread return the same result.
29 int lttng_find_context(struct lttng_ust_ctx
*ctx
, const char *name
)
34 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
35 subname
= name
+ strlen("$ctx.");
39 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
40 /* Skip allocated (but non-initialized) contexts */
41 if (!ctx
->fields
[i
]->event_field
->name
)
43 if (!strcmp(ctx
->fields
[i
]->event_field
->name
, subname
))
49 int lttng_get_context_index(struct lttng_ust_ctx
*ctx
, const char *name
)
56 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
57 subname
= name
+ strlen("$ctx.");
61 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
62 /* Skip allocated (but non-initialized) contexts */
63 if (!ctx
->fields
[i
]->event_field
->name
)
65 if (!strcmp(ctx
->fields
[i
]->event_field
->name
, subname
))
71 static int lttng_find_context_provider(struct lttng_ust_ctx
*ctx
, const char *name
)
75 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
76 /* Skip allocated (but non-initialized) contexts */
77 if (!ctx
->fields
[i
]->event_field
->name
)
79 if (!strncmp(ctx
->fields
[i
]->event_field
->name
, name
,
87 * Note: as we append context information, the pointer location may change.
88 * lttng_add_context leaves the new last context initialized to NULL.
91 int lttng_add_context(struct lttng_ust_ctx
**ctx_p
)
93 struct lttng_ust_ctx
*ctx
;
96 *ctx_p
= zmalloc(sizeof(struct lttng_ust_ctx
));
99 (*ctx_p
)->struct_size
= sizeof(struct lttng_ust_ctx
);
100 (*ctx_p
)->largest_align
= 1;
103 if (ctx
->nr_fields
+ 1 > ctx
->allocated_fields
) {
104 struct lttng_ust_ctx_field
**new_fields
;
106 ctx
->allocated_fields
= max_t(size_t, 1, 2 * ctx
->allocated_fields
);
107 new_fields
= zmalloc(ctx
->allocated_fields
* sizeof(*new_fields
));
112 memcpy(new_fields
, ctx
->fields
, sizeof(*ctx
->fields
) * ctx
->nr_fields
);
114 ctx
->fields
= new_fields
;
120 struct lttng_ust_ctx_field
*lttng_append_context(struct lttng_ust_ctx
**ctx_p
)
122 struct lttng_ust_ctx_field
*field
;
125 field
= zmalloc(sizeof(struct lttng_ust_ctx_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
);
134 ret
= lttng_add_context(ctx_p
);
136 goto error_add_context
;
137 (*ctx_p
)->fields
[(*ctx_p
)->nr_fields
- 1] = field
;
141 free(field
->event_field
);
142 error_alloc_event_field
:
149 * Takes ownership of @f on success.
151 int lttng_context_add_rcu(struct lttng_ust_ctx
**ctx_p
,
152 struct lttng_ust_ctx_field
*f
)
154 struct lttng_ust_ctx
*old_ctx
= *ctx_p
, *new_ctx
= NULL
;
155 struct lttng_ust_ctx_field
**new_fields
= NULL
;
159 new_ctx
= zmalloc(sizeof(struct lttng_ust_ctx
));
162 new_ctx
->struct_size
= sizeof(struct lttng_ust_ctx
);
164 new_fields
= zmalloc(new_ctx
->allocated_fields
* sizeof(*new_fields
));
170 memcpy(new_fields
, old_ctx
->fields
,
171 sizeof(*old_ctx
->fields
) * old_ctx
->nr_fields
);
172 new_ctx
->fields
= new_fields
;
174 ret
= lttng_add_context(&new_ctx
);
180 /* Taking ownership of f. */
181 (*ctx_p
)->fields
[(*ctx_p
)->nr_fields
- 1] = f
;
182 lttng_context_update(new_ctx
);
183 lttng_ust_rcu_assign_pointer(*ctx_p
, new_ctx
);
184 lttng_ust_urcu_synchronize_rcu();
186 free(old_ctx
->fields
);
192 static size_t get_type_max_align(struct lttng_ust_type_common
*type
)
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
:
199 case lttng_ust_type_dynamic
:
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
:
212 size_t field_align
= 0;
213 struct lttng_ust_type_struct
*struct_type
= lttng_ust_get_type_struct(type
);
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
),
229 * lttng_context_update() should be called at least once between context
230 * modification and trace start.
232 void lttng_context_update(struct lttng_ust_ctx
*ctx
)
235 size_t largest_align
= 8; /* in bits */
237 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
238 size_t field_align
= 8;
240 field_align
= get_type_max_align(ctx
->fields
[i
]->event_field
->type
);
241 largest_align
= max_t(size_t, largest_align
, field_align
);
243 ctx
->largest_align
= largest_align
>> 3; /* bits to bytes */
247 * Remove last context field.
249 void lttng_remove_context_field(struct lttng_ust_ctx
**ctx_p
,
250 struct lttng_ust_ctx_field
*field
)
252 struct lttng_ust_ctx
*ctx
;
256 assert(ctx
->fields
[ctx
->nr_fields
] == field
);
257 lttng_ust_destroy_type(field
->event_field
->type
);
258 free((char *) field
->event_field
->name
);
259 free(field
->event_field
);
261 ctx
->fields
[ctx
->nr_fields
] = NULL
;
264 void lttng_destroy_context(struct lttng_ust_ctx
*ctx
)
270 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
271 if (ctx
->fields
[i
]->destroy
)
272 ctx
->fields
[i
]->destroy(ctx
->fields
[i
]);
273 lttng_ust_destroy_type(ctx
->fields
[i
]->event_field
->type
);
274 free((char *) ctx
->fields
[i
]->event_field
->name
);
275 free(ctx
->fields
[i
]->event_field
);
276 free(ctx
->fields
[i
]);
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
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
294 int lttng_ust_context_set_provider_rcu(struct lttng_ust_ctx
**_ctx
,
296 size_t (*get_size
)(struct lttng_ust_ctx_field
*field
, size_t offset
),
297 void (*record
)(struct lttng_ust_ctx_field
*field
,
298 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
299 struct lttng_ust_channel_buffer
*chan
),
300 void (*get_value
)(struct lttng_ust_ctx_field
*field
,
301 struct lttng_ust_ctx_value
*value
))
304 struct lttng_ust_ctx
*ctx
= *_ctx
, *new_ctx
;
305 struct lttng_ust_ctx_field
**new_fields
;
307 if (!ctx
|| !lttng_find_context_provider(ctx
, name
))
310 * We have at least one instance of context for the provider.
312 new_ctx
= zmalloc(sizeof(*new_ctx
));
315 new_ctx
->struct_size
= sizeof(*new_ctx
);
317 new_fields
= zmalloc(sizeof(*new_fields
) * ctx
->allocated_fields
);
323 memcpy(new_fields
, ctx
->fields
,
324 sizeof(*new_fields
) * ctx
->allocated_fields
);
325 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
326 if (strncmp(new_fields
[i
]->event_field
->name
,
327 name
, strlen(name
)) != 0)
329 new_fields
[i
]->get_size
= get_size
;
330 new_fields
[i
]->record
= record
;
331 new_fields
[i
]->get_value
= get_value
;
333 new_ctx
->fields
= new_fields
;
334 lttng_ust_rcu_assign_pointer(*_ctx
, new_ctx
);
335 lttng_ust_urcu_synchronize_rcu();
345 int lttng_context_init_all(struct lttng_ust_ctx
**ctx
)
349 ret
= lttng_add_pthread_id_to_ctx(ctx
);
351 WARN("Cannot add context lttng_add_pthread_id_to_ctx");
354 ret
= lttng_add_vtid_to_ctx(ctx
);
356 WARN("Cannot add context lttng_add_vtid_to_ctx");
359 ret
= lttng_add_vpid_to_ctx(ctx
);
361 WARN("Cannot add context lttng_add_vpid_to_ctx");
364 ret
= lttng_add_procname_to_ctx(ctx
);
366 WARN("Cannot add context lttng_add_procname_to_ctx");
369 ret
= lttng_add_cpu_id_to_ctx(ctx
);
371 WARN("Cannot add context lttng_add_cpu_id_to_ctx");
374 ret
= lttng_add_cgroup_ns_to_ctx(ctx
);
376 WARN("Cannot add context lttng_add_cgroup_ns_to_ctx");
379 ret
= lttng_add_ipc_ns_to_ctx(ctx
);
381 WARN("Cannot add context lttng_add_ipc_ns_to_ctx");
384 ret
= lttng_add_mnt_ns_to_ctx(ctx
);
386 WARN("Cannot add context lttng_add_mnt_ns_to_ctx");
389 ret
= lttng_add_net_ns_to_ctx(ctx
);
391 WARN("Cannot add context lttng_add_net_ns_to_ctx");
394 ret
= lttng_add_pid_ns_to_ctx(ctx
);
396 WARN("Cannot add context lttng_add_pid_ns_to_ctx");
399 ret
= lttng_add_time_ns_to_ctx(ctx
);
401 WARN("Cannot add context lttng_add_time_ns_to_ctx");
404 ret
= lttng_add_user_ns_to_ctx(ctx
);
406 WARN("Cannot add context lttng_add_user_ns_to_ctx");
409 ret
= lttng_add_uts_ns_to_ctx(ctx
);
411 WARN("Cannot add context lttng_add_uts_ns_to_ctx");
414 ret
= lttng_add_vuid_to_ctx(ctx
);
416 WARN("Cannot add context lttng_add_vuid_to_ctx");
419 ret
= lttng_add_veuid_to_ctx(ctx
);
421 WARN("Cannot add context lttng_add_veuid_to_ctx");
424 ret
= lttng_add_vsuid_to_ctx(ctx
);
426 WARN("Cannot add context lttng_add_vsuid_to_ctx");
429 ret
= lttng_add_vgid_to_ctx(ctx
);
431 WARN("Cannot add context lttng_add_vgid_to_ctx");
434 ret
= lttng_add_vegid_to_ctx(ctx
);
436 WARN("Cannot add context lttng_add_vegid_to_ctx");
439 ret
= lttng_add_vsgid_to_ctx(ctx
);
441 WARN("Cannot add context lttng_add_vsgid_to_ctx");
444 lttng_context_update(*ctx
);
448 lttng_destroy_context(*ctx
);