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 <lttng/urcu/pointer.h>
28 #include <usterr-signal-safe.h>
33 #include "tracepoint-internal.h"
35 #include "context-internal.h"
38 * The filter implementation requires that two consecutive "get" for the
39 * same context performed by the same thread return the same result.
42 int lttng_find_context(struct lttng_ctx
*ctx
, const char *name
)
47 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
48 subname
= name
+ strlen("$ctx.");
52 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
53 /* Skip allocated (but non-initialized) contexts */
54 if (!ctx
->fields
[i
].event_field
.name
)
56 if (!strcmp(ctx
->fields
[i
].event_field
.name
, subname
))
62 int lttng_get_context_index(struct lttng_ctx
*ctx
, const char *name
)
69 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
70 subname
= name
+ strlen("$ctx.");
74 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
75 /* Skip allocated (but non-initialized) contexts */
76 if (!ctx
->fields
[i
].event_field
.name
)
78 if (!strcmp(ctx
->fields
[i
].event_field
.name
, subname
))
84 static int lttng_find_context_provider(struct lttng_ctx
*ctx
, const char *name
)
88 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
89 /* Skip allocated (but non-initialized) contexts */
90 if (!ctx
->fields
[i
].event_field
.name
)
92 if (!strncmp(ctx
->fields
[i
].event_field
.name
, name
,
100 * Note: as we append context information, the pointer location may change.
102 struct lttng_ctx_field
*lttng_append_context(struct lttng_ctx
**ctx_p
)
104 struct lttng_ctx_field
*field
;
105 struct lttng_ctx
*ctx
;
108 *ctx_p
= zmalloc(sizeof(struct lttng_ctx
));
111 (*ctx_p
)->largest_align
= 1;
114 if (ctx
->nr_fields
+ 1 > ctx
->allocated_fields
) {
115 struct lttng_ctx_field
*new_fields
;
117 ctx
->allocated_fields
= max_t(size_t, 1, 2 * ctx
->allocated_fields
);
118 new_fields
= zmalloc(ctx
->allocated_fields
* sizeof(struct lttng_ctx_field
));
122 memcpy(new_fields
, ctx
->fields
, sizeof(*ctx
->fields
) * ctx
->nr_fields
);
124 ctx
->fields
= new_fields
;
126 field
= &ctx
->fields
[ctx
->nr_fields
];
131 int lttng_context_add_rcu(struct lttng_ctx
**ctx_p
,
132 const struct lttng_ctx_field
*f
)
134 struct lttng_ctx
*old_ctx
= *ctx_p
, *new_ctx
= NULL
;
135 struct lttng_ctx_field
*new_fields
= NULL
;
136 struct lttng_ctx_field
*nf
;
139 new_ctx
= zmalloc(sizeof(struct lttng_ctx
));
143 new_fields
= zmalloc(new_ctx
->allocated_fields
144 * sizeof(struct lttng_ctx_field
));
149 memcpy(new_fields
, old_ctx
->fields
,
150 sizeof(*old_ctx
->fields
) * old_ctx
->nr_fields
);
151 new_ctx
->fields
= new_fields
;
153 nf
= lttng_append_context(&new_ctx
);
160 lttng_context_update(new_ctx
);
161 lttng_ust_rcu_assign_pointer(*ctx_p
, new_ctx
);
162 lttng_ust_synchronize_trace();
164 free(old_ctx
->fields
);
171 * lttng_context_update() should be called at least once between context
172 * modification and trace start.
174 void lttng_context_update(struct lttng_ctx
*ctx
)
177 size_t largest_align
= 8; /* in bits */
179 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
180 struct lttng_type
*type
;
181 size_t field_align
= 8;
183 type
= &ctx
->fields
[i
].event_field
.type
;
184 switch (type
->atype
) {
186 field_align
= type
->u
.integer
.alignment
;
190 struct lttng_basic_type
*btype
;
192 btype
= &type
->u
.legacy
.array
.elem_type
;
193 switch (btype
->atype
) {
195 field_align
= btype
->u
.basic
.integer
.alignment
;
201 case atype_array_nestable
:
203 case atype_sequence_nestable
:
210 case atype_array_nestable
:
212 const struct lttng_type
*nested_type
;
214 nested_type
= type
->u
.array_nestable
.elem_type
;
215 switch (nested_type
->atype
) {
217 field_align
= nested_type
->u
.integer
.alignment
;
223 case atype_array_nestable
:
225 case atype_sequence_nestable
:
230 field_align
= max_t(size_t, field_align
,
231 type
->u
.array_nestable
.alignment
);
236 struct lttng_basic_type
*btype
;
238 btype
= &type
->u
.legacy
.sequence
.length_type
;
239 switch (btype
->atype
) {
241 field_align
= btype
->u
.basic
.integer
.alignment
;
246 case atype_array_nestable
:
248 case atype_sequence_nestable
:
254 btype
= &type
->u
.legacy
.sequence
.elem_type
;
255 switch (btype
->atype
) {
257 field_align
= max_t(size_t,
259 btype
->u
.basic
.integer
.alignment
);
266 case atype_array_nestable
:
268 case atype_sequence_nestable
:
275 case atype_sequence_nestable
:
277 const struct lttng_type
*nested_type
;
279 nested_type
= type
->u
.sequence_nestable
.elem_type
;
280 switch (nested_type
->atype
) {
282 field_align
= nested_type
->u
.integer
.alignment
;
289 case atype_array_nestable
:
291 case atype_sequence_nestable
:
296 field_align
= max_t(size_t, field_align
,
297 type
->u
.sequence_nestable
.alignment
);
305 case atype_enum_nestable
:
310 largest_align
= max_t(size_t, largest_align
, field_align
);
312 ctx
->largest_align
= largest_align
>> 3; /* bits to bytes */
316 * Remove last context field.
318 void lttng_remove_context_field(struct lttng_ctx
**ctx_p
,
319 struct lttng_ctx_field
*field
)
321 struct lttng_ctx
*ctx
;
325 assert(&ctx
->fields
[ctx
->nr_fields
] == field
);
326 assert(field
->field_name
== NULL
);
327 memset(&ctx
->fields
[ctx
->nr_fields
], 0, sizeof(struct lttng_ctx_field
));
330 void lttng_destroy_context(struct lttng_ctx
*ctx
)
336 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
337 if (ctx
->fields
[i
].destroy
)
338 ctx
->fields
[i
].destroy(&ctx
->fields
[i
]);
339 free(ctx
->fields
[i
].field_name
);
346 * Can be safely performed concurrently with tracing using the struct
347 * lttng_ctx. Using RCU update. Needs to match RCU read-side handling of
350 * This does not allow adding, removing, or changing typing of the
351 * contexts, since this needs to stay invariant for metadata. However,
352 * it allows updating the handlers associated with all contexts matching
353 * a provider (by name) while tracing is using it, in a way that ensures
354 * a single RCU read-side critical section see either all old, or all
357 int lttng_ust_context_set_provider_rcu(struct lttng_ctx
**_ctx
,
359 size_t (*get_size
)(struct lttng_ctx_field
*field
, size_t offset
),
360 void (*record
)(struct lttng_ctx_field
*field
,
361 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
362 struct lttng_channel
*chan
),
363 void (*get_value
)(struct lttng_ctx_field
*field
,
364 struct lttng_ctx_value
*value
))
367 struct lttng_ctx
*ctx
= *_ctx
, *new_ctx
;
368 struct lttng_ctx_field
*new_fields
;
370 if (!ctx
|| !lttng_find_context_provider(ctx
, name
))
373 * We have at least one instance of context for the provider.
375 new_ctx
= zmalloc(sizeof(*new_ctx
));
379 new_fields
= zmalloc(sizeof(*new_fields
) * ctx
->allocated_fields
);
384 memcpy(new_fields
, ctx
->fields
,
385 sizeof(*new_fields
) * ctx
->allocated_fields
);
386 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
387 if (strncmp(new_fields
[i
].event_field
.name
,
388 name
, strlen(name
)) != 0)
390 new_fields
[i
].get_size
= get_size
;
391 new_fields
[i
].record
= record
;
392 new_fields
[i
].get_value
= get_value
;
394 new_ctx
->fields
= new_fields
;
395 lttng_ust_rcu_assign_pointer(*_ctx
, new_ctx
);
396 lttng_ust_synchronize_trace();
406 int lttng_context_init_all(struct lttng_ctx
**ctx
)
410 ret
= lttng_add_pthread_id_to_ctx(ctx
);
412 WARN("Cannot add context lttng_add_pthread_id_to_ctx");
415 ret
= lttng_add_vtid_to_ctx(ctx
);
417 WARN("Cannot add context lttng_add_vtid_to_ctx");
420 ret
= lttng_add_vpid_to_ctx(ctx
);
422 WARN("Cannot add context lttng_add_vpid_to_ctx");
425 ret
= lttng_add_procname_to_ctx(ctx
);
427 WARN("Cannot add context lttng_add_procname_to_ctx");
430 ret
= lttng_add_cpu_id_to_ctx(ctx
);
432 WARN("Cannot add context lttng_add_cpu_id_to_ctx");
435 ret
= lttng_add_cgroup_ns_to_ctx(ctx
);
437 WARN("Cannot add context lttng_add_cgroup_ns_to_ctx");
440 ret
= lttng_add_ipc_ns_to_ctx(ctx
);
442 WARN("Cannot add context lttng_add_ipc_ns_to_ctx");
445 ret
= lttng_add_mnt_ns_to_ctx(ctx
);
447 WARN("Cannot add context lttng_add_mnt_ns_to_ctx");
450 ret
= lttng_add_net_ns_to_ctx(ctx
);
452 WARN("Cannot add context lttng_add_net_ns_to_ctx");
455 ret
= lttng_add_pid_ns_to_ctx(ctx
);
457 WARN("Cannot add context lttng_add_pid_ns_to_ctx");
460 ret
= lttng_add_time_ns_to_ctx(ctx
);
462 WARN("Cannot add context lttng_add_time_ns_to_ctx");
465 ret
= lttng_add_user_ns_to_ctx(ctx
);
467 WARN("Cannot add context lttng_add_user_ns_to_ctx");
470 ret
= lttng_add_uts_ns_to_ctx(ctx
);
472 WARN("Cannot add context lttng_add_uts_ns_to_ctx");
475 ret
= lttng_add_vuid_to_ctx(ctx
);
477 WARN("Cannot add context lttng_add_vuid_to_ctx");
480 ret
= lttng_add_veuid_to_ctx(ctx
);
482 WARN("Cannot add context lttng_add_veuid_to_ctx");
485 ret
= lttng_add_vsuid_to_ctx(ctx
);
487 WARN("Cannot add context lttng_add_vsuid_to_ctx");
490 ret
= lttng_add_vgid_to_ctx(ctx
);
492 WARN("Cannot add context lttng_add_vgid_to_ctx");
495 ret
= lttng_add_vegid_to_ctx(ctx
);
497 WARN("Cannot add context lttng_add_vegid_to_ctx");
500 ret
= lttng_add_vsgid_to_ctx(ctx
);
502 WARN("Cannot add context lttng_add_vsgid_to_ctx");
505 lttng_context_update(*ctx
);
509 lttng_destroy_context(*ctx
);
513 /* For backward compatibility. Leave those exported symbols in place. */
514 struct lttng_ctx
*lttng_static_ctx
;
516 void lttng_context_init(void)
520 void lttng_context_exit(void)
524 int lttng_session_context_init(struct lttng_ctx
**ctx
)