1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
5 * LTTng trace/channel/event context management.
7 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/module.h>
11 #include <linux/list.h>
12 #include <linux/mutex.h>
13 #include <linux/slab.h>
14 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */
15 #include <lttng-events.h>
16 #include <lttng-tracer.h>
19 * The filter implementation requires that two consecutive "get" for the
20 * same context performed by the same thread return the same result.
24 * Static array of contexts, for $ctx filters.
26 struct lttng_ctx
*lttng_static_ctx
;
28 int lttng_find_context(struct lttng_ctx
*ctx
, const char *name
)
32 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
33 /* Skip allocated (but non-initialized) contexts */
34 if (!ctx
->fields
[i
].event_field
.name
)
36 if (!strcmp(ctx
->fields
[i
].event_field
.name
, name
))
41 EXPORT_SYMBOL_GPL(lttng_find_context
);
43 int lttng_get_context_index(struct lttng_ctx
*ctx
, const char *name
)
50 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
51 subname
= name
+ strlen("$ctx.");
55 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
56 /* Skip allocated (but non-initialized) contexts */
57 if (!ctx
->fields
[i
].event_field
.name
)
59 if (!strcmp(ctx
->fields
[i
].event_field
.name
, subname
))
64 EXPORT_SYMBOL_GPL(lttng_get_context_index
);
67 * Note: as we append context information, the pointer location may change.
69 struct lttng_ctx_field
*lttng_append_context(struct lttng_ctx
**ctx_p
)
71 struct lttng_ctx_field
*field
;
72 struct lttng_ctx
*ctx
;
75 *ctx_p
= kzalloc(sizeof(struct lttng_ctx
), GFP_KERNEL
);
78 (*ctx_p
)->largest_align
= 1;
81 if (ctx
->nr_fields
+ 1 > ctx
->allocated_fields
) {
82 struct lttng_ctx_field
*new_fields
;
84 ctx
->allocated_fields
= max_t(size_t, 1, 2 * ctx
->allocated_fields
);
85 new_fields
= lttng_kvzalloc(ctx
->allocated_fields
* sizeof(struct lttng_ctx_field
), GFP_KERNEL
);
89 memcpy(new_fields
, ctx
->fields
, sizeof(*ctx
->fields
) * ctx
->nr_fields
);
90 lttng_kvfree(ctx
->fields
);
91 ctx
->fields
= new_fields
;
93 field
= &ctx
->fields
[ctx
->nr_fields
];
97 EXPORT_SYMBOL_GPL(lttng_append_context
);
100 * lttng_context_update() should be called at least once between context
101 * modification and trace start.
103 void lttng_context_update(struct lttng_ctx
*ctx
)
106 size_t largest_align
= 8; /* in bits */
108 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
109 struct lttng_type
*type
;
110 size_t field_align
= 8;
112 type
= &ctx
->fields
[i
].event_field
.type
;
113 switch (type
->atype
) {
115 field_align
= type
->u
.basic
.integer
.alignment
;
118 case atype_array_bitfield
:
120 struct lttng_basic_type
*btype
;
122 btype
= &type
->u
.array
.elem_type
;
123 switch (btype
->atype
) {
125 field_align
= btype
->u
.basic
.integer
.alignment
;
132 case atype_array_bitfield
:
133 case atype_sequence_bitfield
:
135 case atype_array_compound
:
136 case atype_sequence_compound
:
145 case atype_sequence_bitfield
:
147 struct lttng_basic_type
*btype
;
149 btype
= &type
->u
.sequence
.length_type
;
150 switch (btype
->atype
) {
152 field_align
= btype
->u
.basic
.integer
.alignment
;
158 case atype_array_bitfield
:
159 case atype_sequence_bitfield
:
161 case atype_array_compound
:
162 case atype_sequence_compound
:
169 btype
= &type
->u
.sequence
.elem_type
;
170 switch (btype
->atype
) {
172 field_align
= max_t(size_t,
174 btype
->u
.basic
.integer
.alignment
);
182 case atype_array_bitfield
:
183 case atype_sequence_bitfield
:
185 case atype_array_compound
:
186 case atype_sequence_compound
:
198 case atype_array_compound
:
199 case atype_sequence_compound
:
208 largest_align
= max_t(size_t, largest_align
, field_align
);
210 ctx
->largest_align
= largest_align
>> 3; /* bits to bytes */
214 * Remove last context field.
216 void lttng_remove_context_field(struct lttng_ctx
**ctx_p
,
217 struct lttng_ctx_field
*field
)
219 struct lttng_ctx
*ctx
;
223 WARN_ON_ONCE(&ctx
->fields
[ctx
->nr_fields
] != field
);
224 memset(&ctx
->fields
[ctx
->nr_fields
], 0, sizeof(struct lttng_ctx_field
));
226 EXPORT_SYMBOL_GPL(lttng_remove_context_field
);
228 void lttng_destroy_context(struct lttng_ctx
*ctx
)
234 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
235 if (ctx
->fields
[i
].destroy
)
236 ctx
->fields
[i
].destroy(&ctx
->fields
[i
]);
238 lttng_kvfree(ctx
->fields
);
242 int lttng_context_init(void)
246 ret
= lttng_add_hostname_to_ctx(<tng_static_ctx
);
248 printk(KERN_WARNING
"Cannot add context lttng_add_hostname_to_ctx");
250 ret
= lttng_add_nice_to_ctx(<tng_static_ctx
);
252 printk(KERN_WARNING
"Cannot add context lttng_add_nice_to_ctx");
254 ret
= lttng_add_pid_to_ctx(<tng_static_ctx
);
256 printk(KERN_WARNING
"Cannot add context lttng_add_pid_to_ctx");
258 ret
= lttng_add_ppid_to_ctx(<tng_static_ctx
);
260 printk(KERN_WARNING
"Cannot add context lttng_add_ppid_to_ctx");
262 ret
= lttng_add_prio_to_ctx(<tng_static_ctx
);
264 printk(KERN_WARNING
"Cannot add context lttng_add_prio_to_ctx");
266 ret
= lttng_add_procname_to_ctx(<tng_static_ctx
);
268 printk(KERN_WARNING
"Cannot add context lttng_add_procname_to_ctx");
270 ret
= lttng_add_tid_to_ctx(<tng_static_ctx
);
272 printk(KERN_WARNING
"Cannot add context lttng_add_tid_to_ctx");
274 ret
= lttng_add_vppid_to_ctx(<tng_static_ctx
);
276 printk(KERN_WARNING
"Cannot add context lttng_add_vppid_to_ctx");
278 ret
= lttng_add_vtid_to_ctx(<tng_static_ctx
);
280 printk(KERN_WARNING
"Cannot add context lttng_add_vtid_to_ctx");
282 ret
= lttng_add_vpid_to_ctx(<tng_static_ctx
);
284 printk(KERN_WARNING
"Cannot add context lttng_add_vpid_to_ctx");
286 ret
= lttng_add_cpu_id_to_ctx(<tng_static_ctx
);
288 printk(KERN_WARNING
"Cannot add context lttng_add_cpu_id_to_ctx");
290 ret
= lttng_add_interruptible_to_ctx(<tng_static_ctx
);
292 printk(KERN_WARNING
"Cannot add context lttng_add_interruptible_to_ctx");
294 ret
= lttng_add_need_reschedule_to_ctx(<tng_static_ctx
);
296 printk(KERN_WARNING
"Cannot add context lttng_add_need_reschedule_to_ctx");
298 ret
= lttng_add_preemptible_to_ctx(<tng_static_ctx
);
299 if (ret
&& ret
!= -ENOSYS
) {
300 printk(KERN_WARNING
"Cannot add context lttng_add_preemptible_to_ctx");
302 ret
= lttng_add_migratable_to_ctx(<tng_static_ctx
);
303 if (ret
&& ret
!= -ENOSYS
) {
304 printk(KERN_WARNING
"Cannot add context lttng_add_migratable_to_ctx");
306 /* TODO: perf counters for filtering */
310 void lttng_context_exit(void)
312 lttng_destroy_context(lttng_static_ctx
);
313 lttng_static_ctx
= NULL
;