Implement structure, compound array/sequence types
[lttng-modules.git] / lttng-context.c
1 /*
2 * lttng-context.c
3 *
4 * LTTng trace/channel/event context management.
5 *
6 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
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>
30
31 /*
32 * The filter implementation requires that two consecutive "get" for the
33 * same context performed by the same thread return the same result.
34 */
35
36 /*
37 * Static array of contexts, for $ctx filters.
38 */
39 struct lttng_ctx *lttng_static_ctx;
40
41 int lttng_find_context(struct lttng_ctx *ctx, const char *name)
42 {
43 unsigned int i;
44
45 for (i = 0; i < ctx->nr_fields; i++) {
46 /* Skip allocated (but non-initialized) contexts */
47 if (!ctx->fields[i].event_field.name)
48 continue;
49 if (!strcmp(ctx->fields[i].event_field.name, name))
50 return 1;
51 }
52 return 0;
53 }
54 EXPORT_SYMBOL_GPL(lttng_find_context);
55
56 int lttng_get_context_index(struct lttng_ctx *ctx, const char *name)
57 {
58 unsigned int i;
59 const char *subname;
60
61 if (!ctx)
62 return -1;
63 if (strncmp(name, "$ctx.", strlen("$ctx.")) == 0) {
64 subname = name + strlen("$ctx.");
65 } else {
66 subname = name;
67 }
68 for (i = 0; i < ctx->nr_fields; i++) {
69 /* Skip allocated (but non-initialized) contexts */
70 if (!ctx->fields[i].event_field.name)
71 continue;
72 if (!strcmp(ctx->fields[i].event_field.name, subname))
73 return i;
74 }
75 return -1;
76 }
77 EXPORT_SYMBOL_GPL(lttng_get_context_index);
78
79 /*
80 * Note: as we append context information, the pointer location may change.
81 */
82 struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p)
83 {
84 struct lttng_ctx_field *field;
85 struct lttng_ctx *ctx;
86
87 if (!*ctx_p) {
88 *ctx_p = kzalloc(sizeof(struct lttng_ctx), GFP_KERNEL);
89 if (!*ctx_p)
90 return NULL;
91 (*ctx_p)->largest_align = 1;
92 }
93 ctx = *ctx_p;
94 if (ctx->nr_fields + 1 > ctx->allocated_fields) {
95 struct lttng_ctx_field *new_fields;
96
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);
99 if (!new_fields)
100 return NULL;
101 if (ctx->fields)
102 memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields);
103 kfree(ctx->fields);
104 ctx->fields = new_fields;
105 }
106 field = &ctx->fields[ctx->nr_fields];
107 ctx->nr_fields++;
108 return field;
109 }
110 EXPORT_SYMBOL_GPL(lttng_append_context);
111
112 /*
113 * lttng_context_update() should be called at least once between context
114 * modification and trace start.
115 */
116 void lttng_context_update(struct lttng_ctx *ctx)
117 {
118 int i;
119 size_t largest_align = 8; /* in bits */
120
121 for (i = 0; i < ctx->nr_fields; i++) {
122 struct lttng_type *type;
123 size_t field_align = 8;
124
125 type = &ctx->fields[i].event_field.type;
126 switch (type->atype) {
127 case atype_integer:
128 field_align = type->u.basic.integer.alignment;
129 break;
130 case atype_array:
131 {
132 struct lttng_basic_type *btype;
133
134 btype = &type->u.array.elem_type;
135 switch (btype->atype) {
136 case atype_integer:
137 field_align = btype->u.basic.integer.alignment;
138 break;
139 case atype_string:
140 break;
141
142 case atype_array:
143 case atype_sequence:
144 case atype_struct:
145 case atype_array_compound:
146 case atype_sequence_compound:
147 default:
148 WARN_ON_ONCE(1);
149 break;
150 }
151 break;
152 }
153 case atype_sequence:
154 {
155 struct lttng_basic_type *btype;
156
157 btype = &type->u.sequence.length_type;
158 switch (btype->atype) {
159 case atype_integer:
160 field_align = btype->u.basic.integer.alignment;
161 break;
162
163 case atype_string:
164 case atype_array:
165 case atype_sequence:
166 case atype_struct:
167 case atype_array_compound:
168 case atype_sequence_compound:
169 default:
170 WARN_ON_ONCE(1);
171 break;
172 }
173
174 btype = &type->u.sequence.elem_type;
175 switch (btype->atype) {
176 case atype_integer:
177 field_align = max_t(size_t,
178 field_align,
179 btype->u.basic.integer.alignment);
180 break;
181
182 case atype_string:
183 break;
184
185 case atype_array:
186 case atype_sequence:
187 case atype_struct:
188 case atype_array_compound:
189 case atype_sequence_compound:
190 default:
191 WARN_ON_ONCE(1);
192 break;
193 }
194 break;
195 }
196 case atype_string:
197 break;
198
199 case atype_struct:
200 case atype_array_compound:
201 case atype_sequence_compound:
202 break;
203
204 case atype_enum:
205 default:
206 WARN_ON_ONCE(1);
207 break;
208 }
209 largest_align = max_t(size_t, largest_align, field_align);
210 }
211 ctx->largest_align = largest_align >> 3; /* bits to bytes */
212 }
213
214 /*
215 * Remove last context field.
216 */
217 void lttng_remove_context_field(struct lttng_ctx **ctx_p,
218 struct lttng_ctx_field *field)
219 {
220 struct lttng_ctx *ctx;
221
222 ctx = *ctx_p;
223 ctx->nr_fields--;
224 WARN_ON_ONCE(&ctx->fields[ctx->nr_fields] != field);
225 memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field));
226 }
227 EXPORT_SYMBOL_GPL(lttng_remove_context_field);
228
229 void lttng_destroy_context(struct lttng_ctx *ctx)
230 {
231 int i;
232
233 if (!ctx)
234 return;
235 for (i = 0; i < ctx->nr_fields; i++) {
236 if (ctx->fields[i].destroy)
237 ctx->fields[i].destroy(&ctx->fields[i]);
238 }
239 kfree(ctx->fields);
240 kfree(ctx);
241 }
242
243 int lttng_context_init(void)
244 {
245 int ret;
246
247 ret = lttng_add_hostname_to_ctx(&lttng_static_ctx);
248 if (ret) {
249 printk(KERN_WARNING "Cannot add context lttng_add_hostname_to_ctx");
250 }
251 ret = lttng_add_nice_to_ctx(&lttng_static_ctx);
252 if (ret) {
253 printk(KERN_WARNING "Cannot add context lttng_add_nice_to_ctx");
254 }
255 ret = lttng_add_pid_to_ctx(&lttng_static_ctx);
256 if (ret) {
257 printk(KERN_WARNING "Cannot add context lttng_add_pid_to_ctx");
258 }
259 ret = lttng_add_ppid_to_ctx(&lttng_static_ctx);
260 if (ret) {
261 printk(KERN_WARNING "Cannot add context lttng_add_ppid_to_ctx");
262 }
263 ret = lttng_add_prio_to_ctx(&lttng_static_ctx);
264 if (ret) {
265 printk(KERN_WARNING "Cannot add context lttng_add_prio_to_ctx");
266 }
267 ret = lttng_add_procname_to_ctx(&lttng_static_ctx);
268 if (ret) {
269 printk(KERN_WARNING "Cannot add context lttng_add_procname_to_ctx");
270 }
271 ret = lttng_add_tid_to_ctx(&lttng_static_ctx);
272 if (ret) {
273 printk(KERN_WARNING "Cannot add context lttng_add_tid_to_ctx");
274 }
275 ret = lttng_add_vppid_to_ctx(&lttng_static_ctx);
276 if (ret) {
277 printk(KERN_WARNING "Cannot add context lttng_add_vppid_to_ctx");
278 }
279 ret = lttng_add_vtid_to_ctx(&lttng_static_ctx);
280 if (ret) {
281 printk(KERN_WARNING "Cannot add context lttng_add_vtid_to_ctx");
282 }
283 ret = lttng_add_vpid_to_ctx(&lttng_static_ctx);
284 if (ret) {
285 printk(KERN_WARNING "Cannot add context lttng_add_vpid_to_ctx");
286 }
287 ret = lttng_add_cpu_id_to_ctx(&lttng_static_ctx);
288 if (ret) {
289 printk(KERN_WARNING "Cannot add context lttng_add_cpu_id_to_ctx");
290 }
291 ret = lttng_add_interruptible_to_ctx(&lttng_static_ctx);
292 if (ret) {
293 printk(KERN_WARNING "Cannot add context lttng_add_interruptible_to_ctx");
294 }
295 ret = lttng_add_need_reschedule_to_ctx(&lttng_static_ctx);
296 if (ret) {
297 printk(KERN_WARNING "Cannot add context lttng_add_need_reschedule_to_ctx");
298 }
299 #if defined(CONFIG_PREEMPT_RT_FULL) || defined(CONFIG_PREEMPT)
300 ret = lttng_add_preemptible_to_ctx(&lttng_static_ctx);
301 if (ret != -ENOSYS) {
302 printk(KERN_WARNING "Cannot add context lttng_add_preemptible_to_ctx");
303 }
304 #endif
305 #ifdef CONFIG_PREEMPT_RT_FULL
306 ret = lttng_add_migratable_to_ctx(&lttng_static_ctx);
307 if (ret != -ENOSYS) {
308 printk(KERN_WARNING "Cannot add context lttng_add_migratable_to_ctx");
309 }
310 #endif
311 /* TODO: perf counters for filtering */
312 return 0;
313 }
314
315 void lttng_context_exit(void)
316 {
317 lttng_destroy_context(lttng_static_ctx);
318 lttng_static_ctx = NULL;
319 }
This page took 0.038561 seconds and 5 git commands to generate.