4 * LTTng trace/channel/event context management.
6 * Copyright (C) 2011-2012 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
23 #include <linux/module.h>
24 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/slab.h>
27 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */
28 #include <lttng-events.h>
29 #include <lttng-tracer.h>
32 * The filter implementation requires that two consecutive "get" for the
33 * same context performed by the same thread return the same result.
37 * Static array of contexts, for $ctx filters.
39 struct lttng_ctx
*lttng_static_ctx
;
41 int lttng_find_context(struct lttng_ctx
*ctx
, const char *name
)
45 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
46 /* Skip allocated (but non-initialized) contexts */
47 if (!ctx
->fields
[i
].event_field
.name
)
49 if (!strcmp(ctx
->fields
[i
].event_field
.name
, name
))
54 EXPORT_SYMBOL_GPL(lttng_find_context
);
56 int lttng_get_context_index(struct lttng_ctx
*ctx
, const char *name
)
63 if (strncmp(name
, "$ctx.", strlen("$ctx.")) == 0) {
64 subname
= name
+ strlen("$ctx.");
68 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
69 /* Skip allocated (but non-initialized) contexts */
70 if (!ctx
->fields
[i
].event_field
.name
)
72 if (!strcmp(ctx
->fields
[i
].event_field
.name
, subname
))
77 EXPORT_SYMBOL_GPL(lttng_get_context_index
);
80 * Note: as we append context information, the pointer location may change.
82 struct lttng_ctx_field
*lttng_append_context(struct lttng_ctx
**ctx_p
)
84 struct lttng_ctx_field
*field
;
85 struct lttng_ctx
*ctx
;
88 *ctx_p
= kzalloc(sizeof(struct lttng_ctx
), GFP_KERNEL
);
91 (*ctx_p
)->largest_align
= 1;
94 if (ctx
->nr_fields
+ 1 > ctx
->allocated_fields
) {
95 struct lttng_ctx_field
*new_fields
;
97 ctx
->allocated_fields
= max_t(size_t, 1, 2 * ctx
->allocated_fields
);
98 new_fields
= kzalloc(ctx
->allocated_fields
* sizeof(struct lttng_ctx_field
), GFP_KERNEL
);
102 memcpy(new_fields
, ctx
->fields
, sizeof(*ctx
->fields
) * ctx
->nr_fields
);
104 ctx
->fields
= new_fields
;
106 field
= &ctx
->fields
[ctx
->nr_fields
];
110 EXPORT_SYMBOL_GPL(lttng_append_context
);
113 * lttng_context_update() should be called at least once between context
114 * modification and trace start.
116 void lttng_context_update(struct lttng_ctx
*ctx
)
119 size_t largest_align
= 8; /* in bits */
121 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
122 struct lttng_type
*type
;
123 size_t field_align
= 8;
125 type
= &ctx
->fields
[i
].event_field
.type
;
126 switch (type
->atype
) {
128 field_align
= type
->u
.basic
.integer
.alignment
;
132 struct lttng_basic_type
*btype
;
134 btype
= &type
->u
.array
.elem_type
;
135 switch (btype
->atype
) {
137 field_align
= btype
->u
.basic
.integer
.alignment
;
145 case atype_array_compound
:
146 case atype_sequence_compound
:
156 struct lttng_basic_type
*btype
;
158 btype
= &type
->u
.sequence
.length_type
;
159 switch (btype
->atype
) {
161 field_align
= btype
->u
.basic
.integer
.alignment
;
168 case atype_array_compound
:
169 case atype_sequence_compound
:
176 btype
= &type
->u
.sequence
.elem_type
;
177 switch (btype
->atype
) {
179 field_align
= max_t(size_t,
181 btype
->u
.basic
.integer
.alignment
);
190 case atype_array_compound
:
191 case atype_sequence_compound
:
203 case atype_array_compound
:
204 case atype_sequence_compound
:
213 largest_align
= max_t(size_t, largest_align
, field_align
);
215 ctx
->largest_align
= largest_align
>> 3; /* bits to bytes */
219 * Remove last context field.
221 void lttng_remove_context_field(struct lttng_ctx
**ctx_p
,
222 struct lttng_ctx_field
*field
)
224 struct lttng_ctx
*ctx
;
228 WARN_ON_ONCE(&ctx
->fields
[ctx
->nr_fields
] != field
);
229 memset(&ctx
->fields
[ctx
->nr_fields
], 0, sizeof(struct lttng_ctx_field
));
231 EXPORT_SYMBOL_GPL(lttng_remove_context_field
);
233 void lttng_destroy_context(struct lttng_ctx
*ctx
)
239 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
240 if (ctx
->fields
[i
].destroy
)
241 ctx
->fields
[i
].destroy(&ctx
->fields
[i
]);
247 int lttng_context_init(void)
251 ret
= lttng_add_hostname_to_ctx(<tng_static_ctx
);
253 printk(KERN_WARNING
"Cannot add context lttng_add_hostname_to_ctx");
255 ret
= lttng_add_nice_to_ctx(<tng_static_ctx
);
257 printk(KERN_WARNING
"Cannot add context lttng_add_nice_to_ctx");
259 ret
= lttng_add_pid_to_ctx(<tng_static_ctx
);
261 printk(KERN_WARNING
"Cannot add context lttng_add_pid_to_ctx");
263 ret
= lttng_add_ppid_to_ctx(<tng_static_ctx
);
265 printk(KERN_WARNING
"Cannot add context lttng_add_ppid_to_ctx");
267 ret
= lttng_add_prio_to_ctx(<tng_static_ctx
);
269 printk(KERN_WARNING
"Cannot add context lttng_add_prio_to_ctx");
271 ret
= lttng_add_procname_to_ctx(<tng_static_ctx
);
273 printk(KERN_WARNING
"Cannot add context lttng_add_procname_to_ctx");
275 ret
= lttng_add_tid_to_ctx(<tng_static_ctx
);
277 printk(KERN_WARNING
"Cannot add context lttng_add_tid_to_ctx");
279 ret
= lttng_add_vppid_to_ctx(<tng_static_ctx
);
281 printk(KERN_WARNING
"Cannot add context lttng_add_vppid_to_ctx");
283 ret
= lttng_add_vtid_to_ctx(<tng_static_ctx
);
285 printk(KERN_WARNING
"Cannot add context lttng_add_vtid_to_ctx");
287 ret
= lttng_add_vpid_to_ctx(<tng_static_ctx
);
289 printk(KERN_WARNING
"Cannot add context lttng_add_vpid_to_ctx");
291 ret
= lttng_add_cpu_id_to_ctx(<tng_static_ctx
);
293 printk(KERN_WARNING
"Cannot add context lttng_add_cpu_id_to_ctx");
295 ret
= lttng_add_interruptible_to_ctx(<tng_static_ctx
);
297 printk(KERN_WARNING
"Cannot add context lttng_add_interruptible_to_ctx");
299 ret
= lttng_add_need_reschedule_to_ctx(<tng_static_ctx
);
301 printk(KERN_WARNING
"Cannot add context lttng_add_need_reschedule_to_ctx");
303 ret
= lttng_add_preemptible_to_ctx(<tng_static_ctx
);
304 if (ret
&& ret
!= -ENOSYS
) {
305 printk(KERN_WARNING
"Cannot add context lttng_add_preemptible_to_ctx");
307 ret
= lttng_add_migratable_to_ctx(<tng_static_ctx
);
308 if (ret
&& ret
!= -ENOSYS
) {
309 printk(KERN_WARNING
"Cannot add context lttng_add_migratable_to_ctx");
311 /* TODO: perf counters for filtering */
315 void lttng_context_exit(void)
317 lttng_destroy_context(lttng_static_ctx
);
318 lttng_static_ctx
= NULL
;