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 * The filter implementation requires that two consecutive "get" for the
35 * same context performed by the same thread return the same result.
38 int lttng_find_context(struct lttng_ctx
*ctx
, const char *name
)
43 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
44 subname
= name
+ strlen("$ctx.");
48 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
49 /* Skip allocated (but non-initialized) contexts */
50 if (!ctx
->fields
[i
].event_field
.name
)
52 if (!strcmp(ctx
->fields
[i
].event_field
.name
, subname
))
58 int lttng_context_is_app(const char *name
)
60 if (strncmp(name
, "$app.", strlen("$app.")) != 0) {
66 int lttng_get_context_index(struct lttng_ctx
*ctx
, const char *name
)
73 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
74 subname
= name
+ strlen("$ctx.");
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 (!strcmp(ctx
->fields
[i
].event_field
.name
, subname
))
88 static int lttng_find_context_provider(struct lttng_ctx
*ctx
, const char *name
)
92 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
93 /* Skip allocated (but non-initialized) contexts */
94 if (!ctx
->fields
[i
].event_field
.name
)
96 if (!strncmp(ctx
->fields
[i
].event_field
.name
, name
,
104 * Note: as we append context information, the pointer location may change.
106 struct lttng_ctx_field
*lttng_append_context(struct lttng_ctx
**ctx_p
)
108 struct lttng_ctx_field
*field
;
109 struct lttng_ctx
*ctx
;
112 *ctx_p
= zmalloc(sizeof(struct lttng_ctx
));
115 (*ctx_p
)->largest_align
= 1;
118 if (ctx
->nr_fields
+ 1 > ctx
->allocated_fields
) {
119 struct lttng_ctx_field
*new_fields
;
121 ctx
->allocated_fields
= max_t(size_t, 1, 2 * ctx
->allocated_fields
);
122 new_fields
= zmalloc(ctx
->allocated_fields
* sizeof(struct lttng_ctx_field
));
126 memcpy(new_fields
, ctx
->fields
, sizeof(*ctx
->fields
) * ctx
->nr_fields
);
128 ctx
->fields
= new_fields
;
130 field
= &ctx
->fields
[ctx
->nr_fields
];
135 int lttng_context_add_rcu(struct lttng_ctx
**ctx_p
,
136 const struct lttng_ctx_field
*f
)
138 struct lttng_ctx
*old_ctx
= *ctx_p
, *new_ctx
= NULL
;
139 struct lttng_ctx_field
*new_fields
= NULL
;
140 struct lttng_ctx_field
*nf
;
143 new_ctx
= zmalloc(sizeof(struct lttng_ctx
));
147 new_fields
= zmalloc(new_ctx
->allocated_fields
148 * sizeof(struct lttng_ctx_field
));
153 memcpy(new_fields
, old_ctx
->fields
,
154 sizeof(*old_ctx
->fields
) * old_ctx
->nr_fields
);
155 new_ctx
->fields
= new_fields
;
157 nf
= lttng_append_context(&new_ctx
);
164 lttng_context_update(new_ctx
);
165 rcu_assign_pointer(*ctx_p
, new_ctx
);
168 free(old_ctx
->fields
);
175 * lttng_context_update() should be called at least once between context
176 * modification and trace start.
178 void lttng_context_update(struct lttng_ctx
*ctx
)
181 size_t largest_align
= 8; /* in bits */
183 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
184 struct lttng_type
*type
;
185 size_t field_align
= 8;
187 type
= &ctx
->fields
[i
].event_field
.type
;
188 switch (type
->atype
) {
190 field_align
= type
->u
.basic
.integer
.alignment
;
194 struct lttng_basic_type
*btype
;
196 btype
= &type
->u
.array
.elem_type
;
197 switch (btype
->atype
) {
199 field_align
= btype
->u
.basic
.integer
.alignment
;
214 struct lttng_basic_type
*btype
;
216 btype
= &type
->u
.sequence
.length_type
;
217 switch (btype
->atype
) {
219 field_align
= btype
->u
.basic
.integer
.alignment
;
230 btype
= &type
->u
.sequence
.elem_type
;
231 switch (btype
->atype
) {
233 field_align
= max_t(size_t,
235 btype
->u
.basic
.integer
.alignment
);
258 largest_align
= max_t(size_t, largest_align
, field_align
);
260 ctx
->largest_align
= largest_align
>> 3; /* bits to bytes */
264 * Remove last context field.
266 void lttng_remove_context_field(struct lttng_ctx
**ctx_p
,
267 struct lttng_ctx_field
*field
)
269 struct lttng_ctx
*ctx
;
273 assert(&ctx
->fields
[ctx
->nr_fields
] == field
);
274 assert(field
->field_name
== NULL
);
275 memset(&ctx
->fields
[ctx
->nr_fields
], 0, sizeof(struct lttng_ctx_field
));
278 void lttng_destroy_context(struct lttng_ctx
*ctx
)
284 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
285 if (ctx
->fields
[i
].destroy
)
286 ctx
->fields
[i
].destroy(&ctx
->fields
[i
]);
287 free(ctx
->fields
[i
].field_name
);
294 * Can be safely performed concurrently with tracing using the struct
295 * lttng_ctx. Using RCU update. Needs to match RCU read-side handling of
298 * This does not allow adding, removing, or changing typing of the
299 * contexts, since this needs to stay invariant for metadata. However,
300 * it allows updating the handlers associated with all contexts matching
301 * a provider (by name) while tracing is using it, in a way that ensures
302 * a single RCU read-side critical section see either all old, or all
305 int lttng_ust_context_set_provider_rcu(struct lttng_ctx
**_ctx
,
307 size_t (*get_size
)(struct lttng_ctx_field
*field
, size_t offset
),
308 void (*record
)(struct lttng_ctx_field
*field
,
309 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
310 struct lttng_channel
*chan
),
311 void (*get_value
)(struct lttng_ctx_field
*field
,
312 struct lttng_ctx_value
*value
))
315 struct lttng_ctx
*ctx
= *_ctx
, *new_ctx
;
316 struct lttng_ctx_field
*new_fields
;
318 if (!ctx
|| !lttng_find_context_provider(ctx
, name
))
321 * We have at least one instance of context for the provider.
323 new_ctx
= zmalloc(sizeof(*new_ctx
));
327 new_fields
= zmalloc(sizeof(*new_fields
) * ctx
->allocated_fields
);
332 memcpy(new_fields
, ctx
->fields
,
333 sizeof(*new_fields
) * ctx
->allocated_fields
);
334 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
335 if (strncmp(new_fields
[i
].event_field
.name
,
336 name
, strlen(name
)) != 0)
338 new_fields
[i
].get_size
= get_size
;
339 new_fields
[i
].record
= record
;
340 new_fields
[i
].get_value
= get_value
;
342 new_ctx
->fields
= new_fields
;
343 rcu_assign_pointer(*_ctx
, new_ctx
);
354 int lttng_session_context_init(struct lttng_ctx
**ctx
)
358 ret
= lttng_add_pthread_id_to_ctx(ctx
);
360 WARN("Cannot add context lttng_add_pthread_id_to_ctx");
363 ret
= lttng_add_vtid_to_ctx(ctx
);
365 WARN("Cannot add context lttng_add_vtid_to_ctx");
368 ret
= lttng_add_vpid_to_ctx(ctx
);
370 WARN("Cannot add context lttng_add_vpid_to_ctx");
373 ret
= lttng_add_procname_to_ctx(ctx
);
375 WARN("Cannot add context lttng_add_procname_to_ctx");
378 ret
= lttng_add_cpu_id_to_ctx(ctx
);
380 WARN("Cannot add context lttng_add_cpu_id_to_ctx");
383 lttng_context_update(*ctx
);
387 lttng_destroy_context(*ctx
);