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>
34 #include "context-internal.h"
37 * The filter implementation requires that two consecutive "get" for the
38 * same context performed by the same thread return the same result.
41 int lttng_find_context(struct lttng_ctx
*ctx
, const char *name
)
46 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
47 subname
= name
+ strlen("$ctx.");
51 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
52 /* Skip allocated (but non-initialized) contexts */
53 if (!ctx
->fields
[i
].event_field
.name
)
55 if (!strcmp(ctx
->fields
[i
].event_field
.name
, subname
))
61 int lttng_get_context_index(struct lttng_ctx
*ctx
, const char *name
)
68 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
69 subname
= name
+ strlen("$ctx.");
73 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
74 /* Skip allocated (but non-initialized) contexts */
75 if (!ctx
->fields
[i
].event_field
.name
)
77 if (!strcmp(ctx
->fields
[i
].event_field
.name
, subname
))
83 static int lttng_find_context_provider(struct lttng_ctx
*ctx
, const char *name
)
87 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
88 /* Skip allocated (but non-initialized) contexts */
89 if (!ctx
->fields
[i
].event_field
.name
)
91 if (!strncmp(ctx
->fields
[i
].event_field
.name
, name
,
99 * Note: as we append context information, the pointer location may change.
101 struct lttng_ctx_field
*lttng_append_context(struct lttng_ctx
**ctx_p
)
103 struct lttng_ctx_field
*field
;
104 struct lttng_ctx
*ctx
;
107 *ctx_p
= zmalloc(sizeof(struct lttng_ctx
));
110 (*ctx_p
)->largest_align
= 1;
113 if (ctx
->nr_fields
+ 1 > ctx
->allocated_fields
) {
114 struct lttng_ctx_field
*new_fields
;
116 ctx
->allocated_fields
= max_t(size_t, 1, 2 * ctx
->allocated_fields
);
117 new_fields
= zmalloc(ctx
->allocated_fields
* sizeof(struct lttng_ctx_field
));
121 memcpy(new_fields
, ctx
->fields
, sizeof(*ctx
->fields
) * ctx
->nr_fields
);
123 ctx
->fields
= new_fields
;
125 field
= &ctx
->fields
[ctx
->nr_fields
];
130 int lttng_context_add_rcu(struct lttng_ctx
**ctx_p
,
131 const struct lttng_ctx_field
*f
)
133 struct lttng_ctx
*old_ctx
= *ctx_p
, *new_ctx
= NULL
;
134 struct lttng_ctx_field
*new_fields
= NULL
;
135 struct lttng_ctx_field
*nf
;
138 new_ctx
= zmalloc(sizeof(struct lttng_ctx
));
142 new_fields
= zmalloc(new_ctx
->allocated_fields
143 * sizeof(struct lttng_ctx_field
));
148 memcpy(new_fields
, old_ctx
->fields
,
149 sizeof(*old_ctx
->fields
) * old_ctx
->nr_fields
);
150 new_ctx
->fields
= new_fields
;
152 nf
= lttng_append_context(&new_ctx
);
159 lttng_context_update(new_ctx
);
160 rcu_assign_pointer(*ctx_p
, new_ctx
);
163 free(old_ctx
->fields
);
170 * lttng_context_update() should be called at least once between context
171 * modification and trace start.
173 void lttng_context_update(struct lttng_ctx
*ctx
)
176 size_t largest_align
= 8; /* in bits */
178 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
179 struct lttng_type
*type
;
180 size_t field_align
= 8;
182 type
= &ctx
->fields
[i
].event_field
.type
;
183 switch (type
->atype
) {
185 field_align
= type
->u
.integer
.alignment
;
189 struct lttng_basic_type
*btype
;
191 btype
= &type
->u
.legacy
.array
.elem_type
;
192 switch (btype
->atype
) {
194 field_align
= btype
->u
.basic
.integer
.alignment
;
200 case atype_array_nestable
:
202 case atype_sequence_nestable
:
209 case atype_array_nestable
:
211 const struct lttng_type
*nested_type
;
213 nested_type
= type
->u
.array_nestable
.elem_type
;
214 switch (nested_type
->atype
) {
216 field_align
= nested_type
->u
.integer
.alignment
;
222 case atype_array_nestable
:
224 case atype_sequence_nestable
:
229 field_align
= max_t(size_t, field_align
,
230 type
->u
.array_nestable
.alignment
);
235 struct lttng_basic_type
*btype
;
237 btype
= &type
->u
.legacy
.sequence
.length_type
;
238 switch (btype
->atype
) {
240 field_align
= btype
->u
.basic
.integer
.alignment
;
245 case atype_array_nestable
:
247 case atype_sequence_nestable
:
253 btype
= &type
->u
.legacy
.sequence
.elem_type
;
254 switch (btype
->atype
) {
256 field_align
= max_t(size_t,
258 btype
->u
.basic
.integer
.alignment
);
265 case atype_array_nestable
:
267 case atype_sequence_nestable
:
274 case atype_sequence_nestable
:
276 const struct lttng_type
*nested_type
;
278 nested_type
= type
->u
.sequence_nestable
.elem_type
;
279 switch (nested_type
->atype
) {
281 field_align
= nested_type
->u
.integer
.alignment
;
288 case atype_array_nestable
:
290 case atype_sequence_nestable
:
295 field_align
= max_t(size_t, field_align
,
296 type
->u
.sequence_nestable
.alignment
);
304 case atype_enum_nestable
:
309 largest_align
= max_t(size_t, largest_align
, field_align
);
311 ctx
->largest_align
= largest_align
>> 3; /* bits to bytes */
315 * Remove last context field.
317 void lttng_remove_context_field(struct lttng_ctx
**ctx_p
,
318 struct lttng_ctx_field
*field
)
320 struct lttng_ctx
*ctx
;
324 assert(&ctx
->fields
[ctx
->nr_fields
] == field
);
325 assert(field
->field_name
== NULL
);
326 memset(&ctx
->fields
[ctx
->nr_fields
], 0, sizeof(struct lttng_ctx_field
));
329 void lttng_destroy_context(struct lttng_ctx
*ctx
)
335 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
336 if (ctx
->fields
[i
].destroy
)
337 ctx
->fields
[i
].destroy(&ctx
->fields
[i
]);
338 free(ctx
->fields
[i
].field_name
);
345 * Can be safely performed concurrently with tracing using the struct
346 * lttng_ctx. Using RCU update. Needs to match RCU read-side handling of
349 * This does not allow adding, removing, or changing typing of the
350 * contexts, since this needs to stay invariant for metadata. However,
351 * it allows updating the handlers associated with all contexts matching
352 * a provider (by name) while tracing is using it, in a way that ensures
353 * a single RCU read-side critical section see either all old, or all
356 int lttng_ust_context_set_provider_rcu(struct lttng_ctx
**_ctx
,
358 size_t (*get_size
)(struct lttng_ctx_field
*field
, size_t offset
),
359 void (*record
)(struct lttng_ctx_field
*field
,
360 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
361 struct lttng_channel
*chan
),
362 void (*get_value
)(struct lttng_ctx_field
*field
,
363 struct lttng_ctx_value
*value
))
366 struct lttng_ctx
*ctx
= *_ctx
, *new_ctx
;
367 struct lttng_ctx_field
*new_fields
;
369 if (!ctx
|| !lttng_find_context_provider(ctx
, name
))
372 * We have at least one instance of context for the provider.
374 new_ctx
= zmalloc(sizeof(*new_ctx
));
378 new_fields
= zmalloc(sizeof(*new_fields
) * ctx
->allocated_fields
);
383 memcpy(new_fields
, ctx
->fields
,
384 sizeof(*new_fields
) * ctx
->allocated_fields
);
385 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
386 if (strncmp(new_fields
[i
].event_field
.name
,
387 name
, strlen(name
)) != 0)
389 new_fields
[i
].get_size
= get_size
;
390 new_fields
[i
].record
= record
;
391 new_fields
[i
].get_value
= get_value
;
393 new_ctx
->fields
= new_fields
;
394 rcu_assign_pointer(*_ctx
, new_ctx
);
405 int lttng_context_init_all(struct lttng_ctx
**ctx
)
409 ret
= lttng_add_pthread_id_to_ctx(ctx
);
411 WARN("Cannot add context lttng_add_pthread_id_to_ctx");
414 ret
= lttng_add_vtid_to_ctx(ctx
);
416 WARN("Cannot add context lttng_add_vtid_to_ctx");
419 ret
= lttng_add_vpid_to_ctx(ctx
);
421 WARN("Cannot add context lttng_add_vpid_to_ctx");
424 ret
= lttng_add_procname_to_ctx(ctx
);
426 WARN("Cannot add context lttng_add_procname_to_ctx");
429 ret
= lttng_add_cpu_id_to_ctx(ctx
);
431 WARN("Cannot add context lttng_add_cpu_id_to_ctx");
434 ret
= lttng_add_cgroup_ns_to_ctx(ctx
);
436 WARN("Cannot add context lttng_add_cgroup_ns_to_ctx");
439 ret
= lttng_add_ipc_ns_to_ctx(ctx
);
441 WARN("Cannot add context lttng_add_ipc_ns_to_ctx");
444 ret
= lttng_add_mnt_ns_to_ctx(ctx
);
446 WARN("Cannot add context lttng_add_mnt_ns_to_ctx");
449 ret
= lttng_add_net_ns_to_ctx(ctx
);
451 WARN("Cannot add context lttng_add_net_ns_to_ctx");
454 ret
= lttng_add_pid_ns_to_ctx(ctx
);
456 WARN("Cannot add context lttng_add_pid_ns_to_ctx");
459 ret
= lttng_add_time_ns_to_ctx(ctx
);
461 WARN("Cannot add context lttng_add_time_ns_to_ctx");
464 ret
= lttng_add_user_ns_to_ctx(ctx
);
466 WARN("Cannot add context lttng_add_user_ns_to_ctx");
469 ret
= lttng_add_uts_ns_to_ctx(ctx
);
471 WARN("Cannot add context lttng_add_uts_ns_to_ctx");
474 ret
= lttng_add_vuid_to_ctx(ctx
);
476 WARN("Cannot add context lttng_add_vuid_to_ctx");
479 ret
= lttng_add_veuid_to_ctx(ctx
);
481 WARN("Cannot add context lttng_add_veuid_to_ctx");
484 ret
= lttng_add_vsuid_to_ctx(ctx
);
486 WARN("Cannot add context lttng_add_vsuid_to_ctx");
489 ret
= lttng_add_vgid_to_ctx(ctx
);
491 WARN("Cannot add context lttng_add_vgid_to_ctx");
494 ret
= lttng_add_vegid_to_ctx(ctx
);
496 WARN("Cannot add context lttng_add_vegid_to_ctx");
499 ret
= lttng_add_vsgid_to_ctx(ctx
);
501 WARN("Cannot add context lttng_add_vsgid_to_ctx");
504 lttng_context_update(*ctx
);
508 lttng_destroy_context(*ctx
);
512 /* For backward compatibility. Leave those exported symbols in place. */
513 struct lttng_ctx
*lttng_static_ctx
;
515 void lttng_context_init(void)
519 void lttng_context_exit(void)
523 int lttng_session_context_init(struct lttng_ctx
**ctx
)