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
)
62 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
63 /* Skip allocated (but non-initialized) contexts */
64 if (!ctx
->fields
[i
].event_field
.name
)
66 if (!strcmp(ctx
->fields
[i
].event_field
.name
, name
))
71 EXPORT_SYMBOL_GPL(lttng_get_context_index
);
74 * Note: as we append context information, the pointer location may change.
76 struct lttng_ctx_field
*lttng_append_context(struct lttng_ctx
**ctx_p
)
78 struct lttng_ctx_field
*field
;
79 struct lttng_ctx
*ctx
;
82 *ctx_p
= kzalloc(sizeof(struct lttng_ctx
), GFP_KERNEL
);
85 (*ctx_p
)->largest_align
= 1;
88 if (ctx
->nr_fields
+ 1 > ctx
->allocated_fields
) {
89 struct lttng_ctx_field
*new_fields
;
91 ctx
->allocated_fields
= max_t(size_t, 1, 2 * ctx
->allocated_fields
);
92 new_fields
= kzalloc(ctx
->allocated_fields
* sizeof(struct lttng_ctx_field
), GFP_KERNEL
);
96 memcpy(new_fields
, ctx
->fields
, sizeof(*ctx
->fields
) * ctx
->nr_fields
);
98 ctx
->fields
= new_fields
;
100 field
= &ctx
->fields
[ctx
->nr_fields
];
104 EXPORT_SYMBOL_GPL(lttng_append_context
);
107 * lttng_context_update() should be called at least once between context
108 * modification and trace start.
110 void lttng_context_update(struct lttng_ctx
*ctx
)
113 size_t largest_align
= 8; /* in bits */
115 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
116 struct lttng_type
*type
;
117 size_t field_align
= 8;
119 type
= &ctx
->fields
[i
].event_field
.type
;
120 switch (type
->atype
) {
122 field_align
= type
->u
.basic
.integer
.alignment
;
126 struct lttng_basic_type
*btype
;
128 btype
= &type
->u
.array
.elem_type
;
129 switch (btype
->atype
) {
131 field_align
= btype
->u
.basic
.integer
.alignment
;
146 struct lttng_basic_type
*btype
;
148 btype
= &type
->u
.sequence
.length_type
;
149 switch (btype
->atype
) {
151 field_align
= btype
->u
.basic
.integer
.alignment
;
162 btype
= &type
->u
.sequence
.elem_type
;
163 switch (btype
->atype
) {
165 field_align
= max_t(size_t,
167 btype
->u
.basic
.integer
.alignment
);
189 largest_align
= max_t(size_t, largest_align
, field_align
);
191 ctx
->largest_align
= largest_align
>> 3; /* bits to bytes */
195 * Remove last context field.
197 void lttng_remove_context_field(struct lttng_ctx
**ctx_p
,
198 struct lttng_ctx_field
*field
)
200 struct lttng_ctx
*ctx
;
204 WARN_ON_ONCE(&ctx
->fields
[ctx
->nr_fields
] != field
);
205 memset(&ctx
->fields
[ctx
->nr_fields
], 0, sizeof(struct lttng_ctx_field
));
207 EXPORT_SYMBOL_GPL(lttng_remove_context_field
);
209 void lttng_destroy_context(struct lttng_ctx
*ctx
)
215 for (i
= 0; i
< ctx
->nr_fields
; i
++) {
216 if (ctx
->fields
[i
].destroy
)
217 ctx
->fields
[i
].destroy(&ctx
->fields
[i
]);
223 int lttng_context_init(void)
227 ret
= lttng_add_hostname_to_ctx(<tng_static_ctx
);
229 printk(KERN_WARNING
"Cannot add context lttng_add_hostname_to_ctx");
231 ret
= lttng_add_nice_to_ctx(<tng_static_ctx
);
233 printk(KERN_WARNING
"Cannot add context lttng_add_nice_to_ctx");
235 ret
= lttng_add_pid_to_ctx(<tng_static_ctx
);
237 printk(KERN_WARNING
"Cannot add context lttng_add_pid_to_ctx");
239 ret
= lttng_add_ppid_to_ctx(<tng_static_ctx
);
241 printk(KERN_WARNING
"Cannot add context lttng_add_ppid_to_ctx");
243 ret
= lttng_add_prio_to_ctx(<tng_static_ctx
);
245 printk(KERN_WARNING
"Cannot add context lttng_add_prio_to_ctx");
247 ret
= lttng_add_procname_to_ctx(<tng_static_ctx
);
249 printk(KERN_WARNING
"Cannot add context lttng_add_procname_to_ctx");
251 ret
= lttng_add_tid_to_ctx(<tng_static_ctx
);
253 printk(KERN_WARNING
"Cannot add context lttng_add_tid_to_ctx");
255 ret
= lttng_add_vppid_to_ctx(<tng_static_ctx
);
257 printk(KERN_WARNING
"Cannot add context lttng_add_vppid_to_ctx");
259 ret
= lttng_add_vtid_to_ctx(<tng_static_ctx
);
261 printk(KERN_WARNING
"Cannot add context lttng_add_vtid_to_ctx");
263 ret
= lttng_add_vpid_to_ctx(<tng_static_ctx
);
265 printk(KERN_WARNING
"Cannot add context lttng_add_vpid_to_ctx");
267 ret
= lttng_add_cpu_id_to_ctx(<tng_static_ctx
);
269 printk(KERN_WARNING
"Cannot add context lttng_add_cpu_id_to_ctx");
271 /* TODO: perf counters for filtering */
275 void lttng_context_exit(void)
277 lttng_destroy_context(lttng_static_ctx
);
278 lttng_static_ctx
= NULL
;