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
.integer
.alignment
;
187 struct lttng_basic_type
*btype
;
189 btype
= &type
->u
.legacy
.array
.elem_type
;
190 switch (btype
->atype
) {
192 field_align
= btype
->u
.basic
.integer
.alignment
;
198 case atype_array_nestable
:
200 case atype_sequence_nestable
:
207 case atype_array_nestable
:
209 const struct lttng_type
*nested_type
;
211 nested_type
= type
->u
.array_nestable
.elem_type
;
212 switch (nested_type
->atype
) {
214 field_align
= nested_type
->u
.integer
.alignment
;
220 case atype_array_nestable
:
222 case atype_sequence_nestable
:
227 field_align
= max_t(size_t, field_align
,
228 type
->u
.array_nestable
.alignment
);
233 struct lttng_basic_type
*btype
;
235 btype
= &type
->u
.legacy
.sequence
.length_type
;
236 switch (btype
->atype
) {
238 field_align
= btype
->u
.basic
.integer
.alignment
;
243 case atype_array_nestable
:
245 case atype_sequence_nestable
:
251 btype
= &type
->u
.legacy
.sequence
.elem_type
;
252 switch (btype
->atype
) {
254 field_align
= max_t(size_t,
256 btype
->u
.basic
.integer
.alignment
);
263 case atype_array_nestable
:
265 case atype_sequence_nestable
:
272 case atype_sequence_nestable
:
274 const struct lttng_type
*nested_type
;
276 nested_type
= type
->u
.sequence_nestable
.elem_type
;
277 switch (nested_type
->atype
) {
279 field_align
= nested_type
->u
.integer
.alignment
;
286 case atype_array_nestable
:
288 case atype_sequence_nestable
:
293 field_align
= max_t(size_t, field_align
,
294 type
->u
.sequence_nestable
.alignment
);
302 case atype_enum_nestable
:
307 largest_align
= max_t(size_t, largest_align
, field_align
);
309 ctx
->largest_align
= largest_align
>> 3; /* bits to bytes */
313 * Remove last context field.
315 void lttng_remove_context_field(struct lttng_ctx
**ctx_p
,
316 struct lttng_ctx_field
*field
)
318 struct lttng_ctx
*ctx
;
322 assert(&ctx
->fields
[ctx
->nr_fields
] == field
);
323 assert(field
->field_name
== NULL
);
324 memset(&ctx
->fields
[ctx
->nr_fields
], 0, sizeof(struct lttng_ctx_field
));
327 void lttng_destroy_context(struct lttng_ctx
*ctx
)
333 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
334 if (ctx
->fields
[i
].destroy
)
335 ctx
->fields
[i
].destroy(&ctx
->fields
[i
]);
336 free(ctx
->fields
[i
].field_name
);
343 * Can be safely performed concurrently with tracing using the struct
344 * lttng_ctx. Using RCU update. Needs to match RCU read-side handling of
347 * This does not allow adding, removing, or changing typing of the
348 * contexts, since this needs to stay invariant for metadata. However,
349 * it allows updating the handlers associated with all contexts matching
350 * a provider (by name) while tracing is using it, in a way that ensures
351 * a single RCU read-side critical section see either all old, or all
354 int lttng_ust_context_set_provider_rcu(struct lttng_ctx
**_ctx
,
356 size_t (*get_size
)(struct lttng_ctx_field
*field
, size_t offset
),
357 void (*record
)(struct lttng_ctx_field
*field
,
358 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
359 struct lttng_channel
*chan
),
360 void (*get_value
)(struct lttng_ctx_field
*field
,
361 struct lttng_ctx_value
*value
))
364 struct lttng_ctx
*ctx
= *_ctx
, *new_ctx
;
365 struct lttng_ctx_field
*new_fields
;
367 if (!ctx
|| !lttng_find_context_provider(ctx
, name
))
370 * We have at least one instance of context for the provider.
372 new_ctx
= zmalloc(sizeof(*new_ctx
));
376 new_fields
= zmalloc(sizeof(*new_fields
) * ctx
->allocated_fields
);
381 memcpy(new_fields
, ctx
->fields
,
382 sizeof(*new_fields
) * ctx
->allocated_fields
);
383 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
384 if (strncmp(new_fields
[i
].event_field
.name
,
385 name
, strlen(name
)) != 0)
387 new_fields
[i
].get_size
= get_size
;
388 new_fields
[i
].record
= record
;
389 new_fields
[i
].get_value
= get_value
;
391 new_ctx
->fields
= new_fields
;
392 rcu_assign_pointer(*_ctx
, new_ctx
);
403 int lttng_session_context_init(struct lttng_ctx
**ctx
)
407 ret
= lttng_add_pthread_id_to_ctx(ctx
);
409 WARN("Cannot add context lttng_add_pthread_id_to_ctx");
412 ret
= lttng_add_vtid_to_ctx(ctx
);
414 WARN("Cannot add context lttng_add_vtid_to_ctx");
417 ret
= lttng_add_vpid_to_ctx(ctx
);
419 WARN("Cannot add context lttng_add_vpid_to_ctx");
422 ret
= lttng_add_procname_to_ctx(ctx
);
424 WARN("Cannot add context lttng_add_procname_to_ctx");
427 ret
= lttng_add_cpu_id_to_ctx(ctx
);
429 WARN("Cannot add context lttng_add_cpu_id_to_ctx");
432 ret
= lttng_add_cgroup_ns_to_ctx(ctx
);
434 WARN("Cannot add context lttng_add_cgroup_ns_to_ctx");
437 ret
= lttng_add_ipc_ns_to_ctx(ctx
);
439 WARN("Cannot add context lttng_add_ipc_ns_to_ctx");
442 ret
= lttng_add_mnt_ns_to_ctx(ctx
);
444 WARN("Cannot add context lttng_add_mnt_ns_to_ctx");
447 ret
= lttng_add_net_ns_to_ctx(ctx
);
449 WARN("Cannot add context lttng_add_net_ns_to_ctx");
452 ret
= lttng_add_pid_ns_to_ctx(ctx
);
454 WARN("Cannot add context lttng_add_pid_ns_to_ctx");
457 ret
= lttng_add_user_ns_to_ctx(ctx
);
459 WARN("Cannot add context lttng_add_user_ns_to_ctx");
462 ret
= lttng_add_uts_ns_to_ctx(ctx
);
464 WARN("Cannot add context lttng_add_uts_ns_to_ctx");
467 ret
= lttng_add_vuid_to_ctx(ctx
);
469 WARN("Cannot add context lttng_add_vuid_to_ctx");
472 ret
= lttng_add_veuid_to_ctx(ctx
);
474 WARN("Cannot add context lttng_add_veuid_to_ctx");
477 ret
= lttng_add_vsuid_to_ctx(ctx
);
479 WARN("Cannot add context lttng_add_vsuid_to_ctx");
482 ret
= lttng_add_vgid_to_ctx(ctx
);
484 WARN("Cannot add context lttng_add_vgid_to_ctx");
487 ret
= lttng_add_vegid_to_ctx(ctx
);
489 WARN("Cannot add context lttng_add_vegid_to_ctx");
492 ret
= lttng_add_vsgid_to_ctx(ctx
);
494 WARN("Cannot add context lttng_add_vsgid_to_ctx");
497 lttng_context_update(*ctx
);
501 lttng_destroy_context(*ctx
);
505 /* For backward compatibility. Leave those exported symbols in place. */
506 struct lttng_ctx
*lttng_static_ctx
;
508 void lttng_context_init(void)
512 void lttng_context_exit(void)