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 <common/ust-context-provider.h>
13 #include <lttng/urcu/pointer.h>
14 #include <lttng/urcu/urcu-ust.h>
15 #include "common/logging.h"
16 #include "common/macros.h"
21 #include "common/tracepoint.h"
23 #include "context-internal.h"
26 * The filter implementation requires that two consecutive "get" for the
27 * same context performed by the same thread return the same result.
30 int lttng_find_context(struct lttng_ust_ctx
*ctx
, const char *name
)
37 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
38 subname
= name
+ strlen("$ctx.");
42 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
43 /* Skip allocated (but non-initialized) contexts */
44 if (!ctx
->fields
[i
].event_field
->name
)
46 if (!strcmp(ctx
->fields
[i
].event_field
->name
, subname
))
52 int lttng_get_context_index(struct lttng_ust_ctx
*ctx
, const char *name
)
59 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
60 subname
= name
+ strlen("$ctx.");
64 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
65 /* Skip allocated (but non-initialized) contexts */
66 if (!ctx
->fields
[i
].event_field
->name
)
68 if (!strcmp(ctx
->fields
[i
].event_field
->name
, subname
))
74 static int lttng_find_context_provider(struct lttng_ust_ctx
*ctx
, const char *name
)
78 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
79 /* Skip allocated (but non-initialized) contexts */
80 if (!ctx
->fields
[i
].event_field
->name
)
82 if (!strncmp(ctx
->fields
[i
].event_field
->name
, name
,
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.
94 int lttng_ust_context_add_field(struct lttng_ust_ctx
**ctx_p
)
96 struct lttng_ust_ctx
*ctx
;
99 *ctx_p
= zmalloc(sizeof(struct lttng_ust_ctx
));
102 (*ctx_p
)->largest_align
= 1;
105 if (ctx
->nr_fields
+ 1 > ctx
->allocated_fields
) {
106 struct lttng_ust_ctx_field
*new_fields
;
108 ctx
->allocated_fields
= max_t(size_t, 1, 2 * ctx
->allocated_fields
);
109 new_fields
= zmalloc(ctx
->allocated_fields
* sizeof(*new_fields
));
114 memcpy(new_fields
, ctx
->fields
, sizeof(*ctx
->fields
) * ctx
->nr_fields
);
116 ctx
->fields
= new_fields
;
122 static size_t get_type_max_align(const struct lttng_ust_type_common
*type
)
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
:
129 case lttng_ust_type_dynamic
:
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
:
142 size_t field_align
= 0;
143 const struct lttng_ust_type_struct
*struct_type
= lttng_ust_get_type_struct(type
);
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
),
159 * lttng_context_update() should be called at least once between context
160 * modification and trace start.
163 void lttng_context_update(struct lttng_ust_ctx
*ctx
)
166 size_t largest_align
= 8; /* in bits */
168 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
169 size_t field_align
= 8;
171 field_align
= get_type_max_align(ctx
->fields
[i
].event_field
->type
);
172 largest_align
= max_t(size_t, largest_align
, field_align
);
174 ctx
->largest_align
= largest_align
>> 3; /* bits to bytes */
177 int lttng_ust_context_append_rcu(struct lttng_ust_ctx
**ctx_p
,
178 const struct lttng_ust_ctx_field
*f
)
180 struct lttng_ust_ctx
*old_ctx
= *ctx_p
, *new_ctx
= NULL
;
181 struct lttng_ust_ctx_field
*new_fields
= NULL
;
185 new_ctx
= zmalloc(sizeof(struct lttng_ust_ctx
));
189 new_fields
= zmalloc(new_ctx
->allocated_fields
* sizeof(*new_fields
));
195 memcpy(new_fields
, old_ctx
->fields
,
196 sizeof(*old_ctx
->fields
) * old_ctx
->nr_fields
);
197 new_ctx
->fields
= new_fields
;
199 ret
= lttng_ust_context_add_field(&new_ctx
);
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();
210 free(old_ctx
->fields
);
216 int lttng_ust_context_append(struct lttng_ust_ctx
**ctx_p
,
217 const struct lttng_ust_ctx_field
*f
)
221 ret
= lttng_ust_context_add_field(ctx_p
);
224 (*ctx_p
)->fields
[(*ctx_p
)->nr_fields
- 1] = *f
;
225 lttng_context_update(*ctx_p
);
229 void lttng_destroy_context(struct lttng_ust_ctx
*ctx
)
235 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
236 if (ctx
->fields
[i
].destroy
)
237 ctx
->fields
[i
].destroy(ctx
->fields
[i
].priv
);
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
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
255 int lttng_ust_context_set_provider_rcu(struct lttng_ust_ctx
**_ctx
,
257 size_t (*get_size
)(void *priv
, struct lttng_ust_probe_ctx
*probe_ctx
,
259 void (*record
)(void *priv
, struct lttng_ust_probe_ctx
*probe_ctx
,
260 struct lttng_ust_ring_buffer_ctx
*ctx
,
261 struct lttng_ust_channel_buffer
*chan
),
262 void (*get_value
)(void *priv
, struct lttng_ust_probe_ctx
*probe_ctx
,
263 struct lttng_ust_ctx_value
*value
))
266 struct lttng_ust_ctx
*ctx
= *_ctx
, *new_ctx
;
267 struct lttng_ust_ctx_field
*new_fields
;
269 if (!ctx
|| !lttng_find_context_provider(ctx
, name
))
272 * We have at least one instance of context for the provider.
274 new_ctx
= zmalloc(sizeof(*new_ctx
));
278 new_fields
= zmalloc(sizeof(*new_fields
) * ctx
->allocated_fields
);
284 memcpy(new_fields
, ctx
->fields
,
285 sizeof(*new_fields
) * ctx
->allocated_fields
);
286 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
287 if (strncmp(new_fields
[i
].event_field
->name
,
288 name
, strlen(name
)) != 0)
290 new_fields
[i
].get_size
= get_size
;
291 new_fields
[i
].record
= record
;
292 new_fields
[i
].get_value
= get_value
;
294 new_ctx
->fields
= new_fields
;
295 lttng_ust_rcu_assign_pointer(*_ctx
, new_ctx
);
296 lttng_ust_urcu_synchronize_rcu();
306 int lttng_context_init_all(struct lttng_ust_ctx
**ctx
)
310 ret
= lttng_add_pthread_id_to_ctx(ctx
);
312 WARN("Cannot add context lttng_add_pthread_id_to_ctx");
315 ret
= lttng_add_vtid_to_ctx(ctx
);
317 WARN("Cannot add context lttng_add_vtid_to_ctx");
320 ret
= lttng_add_vpid_to_ctx(ctx
);
322 WARN("Cannot add context lttng_add_vpid_to_ctx");
325 ret
= lttng_add_procname_to_ctx(ctx
);
327 WARN("Cannot add context lttng_add_procname_to_ctx");
330 ret
= lttng_add_cpu_id_to_ctx(ctx
);
332 WARN("Cannot add context lttng_add_cpu_id_to_ctx");
335 ret
= lttng_add_cgroup_ns_to_ctx(ctx
);
337 WARN("Cannot add context lttng_add_cgroup_ns_to_ctx");
340 ret
= lttng_add_ipc_ns_to_ctx(ctx
);
342 WARN("Cannot add context lttng_add_ipc_ns_to_ctx");
345 ret
= lttng_add_mnt_ns_to_ctx(ctx
);
347 WARN("Cannot add context lttng_add_mnt_ns_to_ctx");
350 ret
= lttng_add_net_ns_to_ctx(ctx
);
352 WARN("Cannot add context lttng_add_net_ns_to_ctx");
355 ret
= lttng_add_pid_ns_to_ctx(ctx
);
357 WARN("Cannot add context lttng_add_pid_ns_to_ctx");
360 ret
= lttng_add_time_ns_to_ctx(ctx
);
362 WARN("Cannot add context lttng_add_time_ns_to_ctx");
365 ret
= lttng_add_user_ns_to_ctx(ctx
);
367 WARN("Cannot add context lttng_add_user_ns_to_ctx");
370 ret
= lttng_add_uts_ns_to_ctx(ctx
);
372 WARN("Cannot add context lttng_add_uts_ns_to_ctx");
375 ret
= lttng_add_vuid_to_ctx(ctx
);
377 WARN("Cannot add context lttng_add_vuid_to_ctx");
380 ret
= lttng_add_veuid_to_ctx(ctx
);
382 WARN("Cannot add context lttng_add_veuid_to_ctx");
385 ret
= lttng_add_vsuid_to_ctx(ctx
);
387 WARN("Cannot add context lttng_add_vsuid_to_ctx");
390 ret
= lttng_add_vgid_to_ctx(ctx
);
392 WARN("Cannot add context lttng_add_vgid_to_ctx");
395 ret
= lttng_add_vegid_to_ctx(ctx
);
397 WARN("Cannot add context lttng_add_vegid_to_ctx");
400 ret
= lttng_add_vsgid_to_ctx(ctx
);
402 WARN("Cannot add context lttng_add_vsgid_to_ctx");
405 lttng_context_update(*ctx
);
409 lttng_destroy_context(*ctx
);