4 * LTTng UST trace/channel/event context management.
6 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <lttng/ust-events.h>
25 #include <lttng/ust-tracer.h>
26 #include <lttng/ust-context-provider.h>
27 #include <urcu-pointer.h>
28 #include <usterr-signal-safe.h>
35 * The filter implementation requires that two consecutive "get" for the
36 * same context performed by the same thread return the same result.
39 int lttng_find_context(struct lttng_ctx
*ctx
, const char *name
)
44 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
45 subname
= name
+ strlen("$ctx.");
49 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
50 /* Skip allocated (but non-initialized) contexts */
51 if (!ctx
->fields
[i
].event_field
.name
)
53 if (!strcmp(ctx
->fields
[i
].event_field
.name
, subname
))
59 int lttng_get_context_index(struct lttng_ctx
*ctx
, const char *name
)
66 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
67 subname
= name
+ strlen("$ctx.");
71 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
72 /* Skip allocated (but non-initialized) contexts */
73 if (!ctx
->fields
[i
].event_field
.name
)
75 if (!strcmp(ctx
->fields
[i
].event_field
.name
, subname
))
81 static int lttng_find_context_provider(struct lttng_ctx
*ctx
, const char *name
)
85 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
86 /* Skip allocated (but non-initialized) contexts */
87 if (!ctx
->fields
[i
].event_field
.name
)
89 if (!strncmp(ctx
->fields
[i
].event_field
.name
, name
,
97 * Note: as we append context information, the pointer location may change.
99 struct lttng_ctx_field
*lttng_append_context(struct lttng_ctx
**ctx_p
)
101 struct lttng_ctx_field
*field
;
102 struct lttng_ctx
*ctx
;
105 *ctx_p
= zmalloc(sizeof(struct lttng_ctx
));
108 (*ctx_p
)->largest_align
= 1;
111 if (ctx
->nr_fields
+ 1 > ctx
->allocated_fields
) {
112 struct lttng_ctx_field
*new_fields
;
114 ctx
->allocated_fields
= max_t(size_t, 1, 2 * ctx
->allocated_fields
);
115 new_fields
= zmalloc(ctx
->allocated_fields
* sizeof(struct lttng_ctx_field
));
119 memcpy(new_fields
, ctx
->fields
, sizeof(*ctx
->fields
) * ctx
->nr_fields
);
121 ctx
->fields
= new_fields
;
123 field
= &ctx
->fields
[ctx
->nr_fields
];
128 int lttng_context_add_rcu(struct lttng_ctx
**ctx_p
,
129 const struct lttng_ctx_field
*f
)
131 struct lttng_ctx
*old_ctx
= *ctx_p
, *new_ctx
= NULL
;
132 struct lttng_ctx_field
*new_fields
= NULL
;
133 struct lttng_ctx_field
*nf
;
136 new_ctx
= zmalloc(sizeof(struct lttng_ctx
));
140 new_fields
= zmalloc(new_ctx
->allocated_fields
141 * sizeof(struct lttng_ctx_field
));
146 memcpy(new_fields
, old_ctx
->fields
,
147 sizeof(*old_ctx
->fields
) * old_ctx
->nr_fields
);
148 new_ctx
->fields
= new_fields
;
150 nf
= lttng_append_context(&new_ctx
);
157 lttng_context_update(new_ctx
);
158 rcu_assign_pointer(*ctx_p
, new_ctx
);
161 free(old_ctx
->fields
);
168 * lttng_context_update() should be called at least once between context
169 * modification and trace start.
171 void lttng_context_update(struct lttng_ctx
*ctx
)
174 size_t largest_align
= 8; /* in bits */
176 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
177 struct lttng_type
*type
;
178 size_t field_align
= 8;
180 type
= &ctx
->fields
[i
].event_field
.type
;
181 switch (type
->atype
) {
183 field_align
= type
->u
.basic
.integer
.alignment
;
187 struct lttng_basic_type
*btype
;
189 btype
= &type
->u
.array
.elem_type
;
190 switch (btype
->atype
) {
192 field_align
= btype
->u
.basic
.integer
.alignment
;
207 struct lttng_basic_type
*btype
;
209 btype
= &type
->u
.sequence
.length_type
;
210 switch (btype
->atype
) {
212 field_align
= btype
->u
.basic
.integer
.alignment
;
223 btype
= &type
->u
.sequence
.elem_type
;
224 switch (btype
->atype
) {
226 field_align
= max_t(size_t,
228 btype
->u
.basic
.integer
.alignment
);
251 largest_align
= max_t(size_t, largest_align
, field_align
);
253 ctx
->largest_align
= largest_align
>> 3; /* bits to bytes */
257 * Remove last context field.
259 void lttng_remove_context_field(struct lttng_ctx
**ctx_p
,
260 struct lttng_ctx_field
*field
)
262 struct lttng_ctx
*ctx
;
266 assert(&ctx
->fields
[ctx
->nr_fields
] == field
);
267 assert(field
->field_name
== NULL
);
268 memset(&ctx
->fields
[ctx
->nr_fields
], 0, sizeof(struct lttng_ctx_field
));
271 void lttng_destroy_context(struct lttng_ctx
*ctx
)
277 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
278 if (ctx
->fields
[i
].destroy
)
279 ctx
->fields
[i
].destroy(&ctx
->fields
[i
]);
280 free(ctx
->fields
[i
].field_name
);
287 * Can be safely performed concurrently with tracing using the struct
288 * lttng_ctx. Using RCU update. Needs to match RCU read-side handling of
291 * This does not allow adding, removing, or changing typing of the
292 * contexts, since this needs to stay invariant for metadata. However,
293 * it allows updating the handlers associated with all contexts matching
294 * a provider (by name) while tracing is using it, in a way that ensures
295 * a single RCU read-side critical section see either all old, or all
298 int lttng_ust_context_set_provider_rcu(struct lttng_ctx
**_ctx
,
300 size_t (*get_size
)(struct lttng_ctx_field
*field
, size_t offset
),
301 void (*record
)(struct lttng_ctx_field
*field
,
302 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
303 struct lttng_channel
*chan
),
304 void (*get_value
)(struct lttng_ctx_field
*field
,
305 struct lttng_ctx_value
*value
))
308 struct lttng_ctx
*ctx
= *_ctx
, *new_ctx
;
309 struct lttng_ctx_field
*new_fields
;
311 if (!ctx
|| !lttng_find_context_provider(ctx
, name
))
314 * We have at least one instance of context for the provider.
316 new_ctx
= zmalloc(sizeof(*new_ctx
));
320 new_fields
= zmalloc(sizeof(*new_fields
) * ctx
->allocated_fields
);
325 memcpy(new_fields
, ctx
->fields
,
326 sizeof(*new_fields
) * ctx
->allocated_fields
);
327 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
328 if (strncmp(new_fields
[i
].event_field
.name
,
329 name
, strlen(name
)) != 0)
331 new_fields
[i
].get_size
= get_size
;
332 new_fields
[i
].record
= record
;
333 new_fields
[i
].get_value
= get_value
;
335 new_ctx
->fields
= new_fields
;
336 rcu_assign_pointer(*_ctx
, new_ctx
);
347 int lttng_session_context_init(struct lttng_ctx
**ctx
)
351 ret
= lttng_add_pthread_id_to_ctx(ctx
);
353 WARN("Cannot add context lttng_add_pthread_id_to_ctx");
356 ret
= lttng_add_vtid_to_ctx(ctx
);
358 WARN("Cannot add context lttng_add_vtid_to_ctx");
361 ret
= lttng_add_vpid_to_ctx(ctx
);
363 WARN("Cannot add context lttng_add_vpid_to_ctx");
366 ret
= lttng_add_procname_to_ctx(ctx
);
368 WARN("Cannot add context lttng_add_procname_to_ctx");
371 ret
= lttng_add_cpu_id_to_ctx(ctx
);
373 WARN("Cannot add context lttng_add_cpu_id_to_ctx");
376 ret
= lttng_add_cgroup_ns_to_ctx(ctx
);
378 WARN("Cannot add context lttng_add_cgroup_ns_to_ctx");
381 ret
= lttng_add_ipc_ns_to_ctx(ctx
);
383 WARN("Cannot add context lttng_add_ipc_ns_to_ctx");
386 ret
= lttng_add_mnt_ns_to_ctx(ctx
);
388 WARN("Cannot add context lttng_add_mnt_ns_to_ctx");
391 ret
= lttng_add_net_ns_to_ctx(ctx
);
393 WARN("Cannot add context lttng_add_net_ns_to_ctx");
396 ret
= lttng_add_pid_ns_to_ctx(ctx
);
398 WARN("Cannot add context lttng_add_pid_ns_to_ctx");
401 ret
= lttng_add_user_ns_to_ctx(ctx
);
403 WARN("Cannot add context lttng_add_user_ns_to_ctx");
406 ret
= lttng_add_uts_ns_to_ctx(ctx
);
408 WARN("Cannot add context lttng_add_uts_ns_to_ctx");
411 ret
= lttng_add_vuid_to_ctx(ctx
);
413 WARN("Cannot add context lttng_add_vuid_to_ctx");
416 ret
= lttng_add_veuid_to_ctx(ctx
);
418 WARN("Cannot add context lttng_add_veuid_to_ctx");
421 ret
= lttng_add_vsuid_to_ctx(ctx
);
423 WARN("Cannot add context lttng_add_vsuid_to_ctx");
426 ret
= lttng_add_vgid_to_ctx(ctx
);
428 WARN("Cannot add context lttng_add_vgid_to_ctx");
431 ret
= lttng_add_vegid_to_ctx(ctx
);
433 WARN("Cannot add context lttng_add_vegid_to_ctx");
436 ret
= lttng_add_vsgid_to_ctx(ctx
);
438 WARN("Cannot add context lttng_add_vsgid_to_ctx");
441 lttng_context_update(*ctx
);
445 lttng_destroy_context(*ctx
);
449 /* For backward compatibility. Leave those exported symbols in place. */
450 struct lttng_ctx
*lttng_static_ctx
;
452 void lttng_context_init(void)
456 void lttng_context_exit(void)