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