Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
9f36eaed | 2 | * |
a90917c3 | 3 | * lttng-context.c |
2dccf128 | 4 | * |
2dccf128 | 5 | * LTTng trace/channel/event context management. |
17baffe2 | 6 | * |
886d51a3 | 7 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2dccf128 MD |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/list.h> | |
12 | #include <linux/mutex.h> | |
13 | #include <linux/slab.h> | |
263b6c88 | 14 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */ |
2df37e95 MD |
15 | #include <lttng/events.h> |
16 | #include <lttng/tracer.h> | |
2dccf128 | 17 | |
07dfc1d0 MD |
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 | ||
44252f0f MD |
28 | int lttng_find_context(struct lttng_ctx *ctx, const char *name) |
29 | { | |
30 | unsigned int i; | |
31 | ||
c02eb859 MD |
32 | if (!ctx) |
33 | return 0; | |
44252f0f | 34 | for (i = 0; i < ctx->nr_fields; i++) { |
4b58a8e4 MD |
35 | /* Skip allocated (but non-initialized) contexts */ |
36 | if (!ctx->fields[i].event_field.name) | |
37 | continue; | |
44252f0f MD |
38 | if (!strcmp(ctx->fields[i].event_field.name, name)) |
39 | return 1; | |
40 | } | |
41 | return 0; | |
42 | } | |
43 | EXPORT_SYMBOL_GPL(lttng_find_context); | |
44 | ||
07dfc1d0 MD |
45 | int lttng_get_context_index(struct lttng_ctx *ctx, const char *name) |
46 | { | |
47 | unsigned int i; | |
0451bcec | 48 | const char *subname; |
07dfc1d0 MD |
49 | |
50 | if (!ctx) | |
51 | return -1; | |
0451bcec MD |
52 | if (strncmp(name, "$ctx.", strlen("$ctx.")) == 0) { |
53 | subname = name + strlen("$ctx."); | |
54 | } else { | |
55 | subname = name; | |
56 | } | |
07dfc1d0 MD |
57 | for (i = 0; i < ctx->nr_fields; i++) { |
58 | /* Skip allocated (but non-initialized) contexts */ | |
59 | if (!ctx->fields[i].event_field.name) | |
60 | continue; | |
0451bcec | 61 | if (!strcmp(ctx->fields[i].event_field.name, subname)) |
07dfc1d0 MD |
62 | return i; |
63 | } | |
64 | return -1; | |
65 | } | |
66 | EXPORT_SYMBOL_GPL(lttng_get_context_index); | |
67 | ||
c02eb859 MD |
68 | struct lttng_ctx_field *lttng_get_context_field_from_index(struct lttng_ctx *ctx, |
69 | size_t index) | |
70 | { | |
71 | if (index >= ctx->nr_fields) | |
72 | return NULL; | |
73 | return &ctx->fields[index]; | |
74 | } | |
75 | EXPORT_SYMBOL_GPL(lttng_get_context_field_from_index); | |
76 | ||
2001023e MD |
77 | /* |
78 | * Note: as we append context information, the pointer location may change. | |
79 | */ | |
c02eb859 | 80 | ssize_t lttng_append_context_index(struct lttng_ctx **ctx_p) |
2dccf128 | 81 | { |
2dccf128 | 82 | struct lttng_ctx *ctx; |
c02eb859 | 83 | ssize_t pos = -1; |
2dccf128 MD |
84 | |
85 | if (!*ctx_p) { | |
86 | *ctx_p = kzalloc(sizeof(struct lttng_ctx), GFP_KERNEL); | |
87 | if (!*ctx_p) | |
c02eb859 | 88 | goto end; |
a9dd15da | 89 | (*ctx_p)->largest_align = 1; |
2dccf128 MD |
90 | } |
91 | ctx = *ctx_p; | |
92 | if (ctx->nr_fields + 1 > ctx->allocated_fields) { | |
93 | struct lttng_ctx_field *new_fields; | |
94 | ||
0f034e0f | 95 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); |
48f5e0b5 | 96 | new_fields = lttng_kvzalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field), GFP_KERNEL); |
2dccf128 | 97 | if (!new_fields) |
c02eb859 | 98 | goto end; |
2dccf128 | 99 | if (ctx->fields) |
77aefe99 | 100 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); |
48f5e0b5 | 101 | lttng_kvfree(ctx->fields); |
2dccf128 MD |
102 | ctx->fields = new_fields; |
103 | } | |
c02eb859 MD |
104 | pos = ctx->nr_fields++; |
105 | end: | |
106 | return pos; | |
107 | } | |
108 | EXPORT_SYMBOL_GPL(lttng_append_context_index); | |
109 | ||
110 | /* | |
111 | * Note: as we append context information, the pointer location may change. | |
112 | */ | |
113 | struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p) | |
114 | { | |
115 | ssize_t pos; | |
116 | ||
117 | pos = lttng_append_context_index(ctx_p); | |
118 | if (pos < 0) | |
119 | return NULL; | |
120 | return &(*ctx_p)->fields[pos]; | |
2dccf128 MD |
121 | } |
122 | EXPORT_SYMBOL_GPL(lttng_append_context); | |
123 | ||
a9dd15da MD |
124 | /* |
125 | * lttng_context_update() should be called at least once between context | |
126 | * modification and trace start. | |
127 | */ | |
128 | void lttng_context_update(struct lttng_ctx *ctx) | |
129 | { | |
130 | int i; | |
131 | size_t largest_align = 8; /* in bits */ | |
132 | ||
133 | for (i = 0; i < ctx->nr_fields; i++) { | |
134 | struct lttng_type *type; | |
135 | size_t field_align = 8; | |
136 | ||
137 | type = &ctx->fields[i].event_field.type; | |
138 | switch (type->atype) { | |
139 | case atype_integer: | |
ceabb767 | 140 | field_align = type->u.integer.alignment; |
a9dd15da | 141 | break; |
ceabb767 | 142 | case atype_array_nestable: |
a9dd15da | 143 | { |
ceabb767 | 144 | const struct lttng_type *nested_type; |
a9dd15da | 145 | |
ceabb767 MD |
146 | nested_type = type->u.array_nestable.elem_type; |
147 | switch (nested_type->atype) { | |
a9dd15da | 148 | case atype_integer: |
ceabb767 | 149 | field_align = nested_type->u.integer.alignment; |
a9dd15da MD |
150 | break; |
151 | case atype_string: | |
152 | break; | |
153 | ||
ceabb767 MD |
154 | case atype_array_nestable: |
155 | case atype_sequence_nestable: | |
156 | case atype_struct_nestable: | |
157 | case atype_variant_nestable: | |
a9dd15da MD |
158 | default: |
159 | WARN_ON_ONCE(1); | |
160 | break; | |
161 | } | |
ceabb767 MD |
162 | field_align = max_t(size_t, field_align, |
163 | type->u.array_nestable.alignment); | |
a9dd15da MD |
164 | break; |
165 | } | |
ceabb767 | 166 | case atype_sequence_nestable: |
a9dd15da | 167 | { |
ceabb767 | 168 | const struct lttng_type *nested_type; |
a9dd15da | 169 | |
ceabb767 MD |
170 | nested_type = type->u.sequence_nestable.elem_type; |
171 | switch (nested_type->atype) { | |
a9dd15da | 172 | case atype_integer: |
ceabb767 | 173 | field_align = nested_type->u.integer.alignment; |
a9dd15da MD |
174 | break; |
175 | ||
176 | case atype_string: | |
177 | break; | |
178 | ||
ceabb767 MD |
179 | case atype_array_nestable: |
180 | case atype_sequence_nestable: | |
181 | case atype_struct_nestable: | |
182 | case atype_variant_nestable: | |
a9dd15da MD |
183 | default: |
184 | WARN_ON_ONCE(1); | |
185 | break; | |
186 | } | |
ceabb767 MD |
187 | field_align = max_t(size_t, field_align, |
188 | type->u.sequence_nestable.alignment); | |
a9dd15da MD |
189 | break; |
190 | } | |
191 | case atype_string: | |
192 | break; | |
193 | ||
ceabb767 MD |
194 | case atype_struct_nestable: |
195 | case atype_variant_nestable: | |
f513b2bf MD |
196 | break; |
197 | ||
ceabb767 | 198 | case atype_enum_nestable: |
a9dd15da MD |
199 | default: |
200 | WARN_ON_ONCE(1); | |
201 | break; | |
202 | } | |
203 | largest_align = max_t(size_t, largest_align, field_align); | |
204 | } | |
205 | ctx->largest_align = largest_align >> 3; /* bits to bytes */ | |
206 | } | |
207 | ||
c02eb859 MD |
208 | /* Keep same order. */ |
209 | void lttng_remove_context_field_index(struct lttng_ctx **ctx_p, size_t index) | |
210 | { | |
211 | struct lttng_ctx *ctx = *ctx_p; | |
212 | ||
213 | WARN_ON_ONCE(ctx->nr_fields >= index); | |
214 | if (index != ctx->nr_fields - 1) { | |
215 | memmove(&ctx->fields[index], &ctx->fields[index + 1], | |
216 | (ctx->nr_fields - index - 1) * sizeof(struct lttng_ctx_field)); | |
217 | } | |
218 | /* Clear last item. */ | |
219 | memset(&ctx->fields[ctx->nr_fields - 1], 0, sizeof(struct lttng_ctx_field)); | |
220 | ctx->nr_fields--; | |
221 | } | |
222 | EXPORT_SYMBOL_GPL(lttng_remove_context_field_index); | |
223 | ||
4cae220c MD |
224 | /* |
225 | * Remove last context field. | |
226 | */ | |
8289661d MD |
227 | void lttng_remove_context_field(struct lttng_ctx **ctx_p, |
228 | struct lttng_ctx_field *field) | |
229 | { | |
230 | struct lttng_ctx *ctx; | |
231 | ||
232 | ctx = *ctx_p; | |
233 | ctx->nr_fields--; | |
4cae220c | 234 | WARN_ON_ONCE(&ctx->fields[ctx->nr_fields] != field); |
8289661d MD |
235 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); |
236 | } | |
237 | EXPORT_SYMBOL_GPL(lttng_remove_context_field); | |
238 | ||
2dccf128 MD |
239 | void lttng_destroy_context(struct lttng_ctx *ctx) |
240 | { | |
241 | int i; | |
242 | ||
8070f5c0 MD |
243 | if (!ctx) |
244 | return; | |
9e7e4892 MD |
245 | for (i = 0; i < ctx->nr_fields; i++) { |
246 | if (ctx->fields[i].destroy) | |
247 | ctx->fields[i].destroy(&ctx->fields[i]); | |
248 | } | |
48f5e0b5 | 249 | lttng_kvfree(ctx->fields); |
2dccf128 MD |
250 | kfree(ctx); |
251 | } | |
07dfc1d0 MD |
252 | |
253 | int lttng_context_init(void) | |
254 | { | |
255 | int ret; | |
256 | ||
f127e61e MD |
257 | ret = lttng_add_hostname_to_ctx(<tng_static_ctx); |
258 | if (ret) { | |
5a15f70c | 259 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_hostname_to_ctx"); |
f127e61e MD |
260 | } |
261 | ret = lttng_add_nice_to_ctx(<tng_static_ctx); | |
07dfc1d0 | 262 | if (ret) { |
5a15f70c | 263 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_nice_to_ctx"); |
07dfc1d0 | 264 | } |
07dfc1d0 MD |
265 | ret = lttng_add_pid_to_ctx(<tng_static_ctx); |
266 | if (ret) { | |
5a15f70c | 267 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_pid_to_ctx"); |
07dfc1d0 | 268 | } |
f127e61e | 269 | ret = lttng_add_ppid_to_ctx(<tng_static_ctx); |
07dfc1d0 | 270 | if (ret) { |
5a15f70c | 271 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_ppid_to_ctx"); |
07dfc1d0 MD |
272 | } |
273 | ret = lttng_add_prio_to_ctx(<tng_static_ctx); | |
274 | if (ret) { | |
5a15f70c | 275 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_prio_to_ctx"); |
07dfc1d0 | 276 | } |
f127e61e | 277 | ret = lttng_add_procname_to_ctx(<tng_static_ctx); |
07dfc1d0 | 278 | if (ret) { |
5a15f70c | 279 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_procname_to_ctx"); |
07dfc1d0 MD |
280 | } |
281 | ret = lttng_add_tid_to_ctx(<tng_static_ctx); | |
282 | if (ret) { | |
5a15f70c | 283 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_tid_to_ctx"); |
07dfc1d0 | 284 | } |
f127e61e | 285 | ret = lttng_add_vppid_to_ctx(<tng_static_ctx); |
07dfc1d0 | 286 | if (ret) { |
5a15f70c | 287 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_vppid_to_ctx"); |
07dfc1d0 | 288 | } |
f127e61e | 289 | ret = lttng_add_vtid_to_ctx(<tng_static_ctx); |
07dfc1d0 | 290 | if (ret) { |
5a15f70c | 291 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_vtid_to_ctx"); |
07dfc1d0 | 292 | } |
f127e61e | 293 | ret = lttng_add_vpid_to_ctx(<tng_static_ctx); |
07dfc1d0 | 294 | if (ret) { |
5a15f70c | 295 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_vpid_to_ctx"); |
07dfc1d0 | 296 | } |
b3699d90 MD |
297 | ret = lttng_add_cpu_id_to_ctx(<tng_static_ctx); |
298 | if (ret) { | |
5a15f70c | 299 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_cpu_id_to_ctx"); |
b3699d90 | 300 | } |
79150a49 JD |
301 | ret = lttng_add_interruptible_to_ctx(<tng_static_ctx); |
302 | if (ret) { | |
5a15f70c | 303 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_interruptible_to_ctx"); |
79150a49 JD |
304 | } |
305 | ret = lttng_add_need_reschedule_to_ctx(<tng_static_ctx); | |
306 | if (ret) { | |
5a15f70c | 307 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_need_reschedule_to_ctx"); |
79150a49 | 308 | } |
79150a49 | 309 | ret = lttng_add_preemptible_to_ctx(<tng_static_ctx); |
d0e59d6f | 310 | if (ret && ret != -ENOSYS) { |
5a15f70c | 311 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_preemptible_to_ctx"); |
79150a49 | 312 | } |
79150a49 | 313 | ret = lttng_add_migratable_to_ctx(<tng_static_ctx); |
d0e59d6f | 314 | if (ret && ret != -ENOSYS) { |
5a15f70c | 315 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_migratable_to_ctx"); |
79150a49 | 316 | } |
a6cf40a4 MJ |
317 | ret = lttng_add_cgroup_ns_to_ctx(<tng_static_ctx); |
318 | if (ret && ret != -ENOSYS) { | |
5a15f70c | 319 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_cgroup_ns_to_ctx"); |
a6cf40a4 MJ |
320 | } |
321 | ret = lttng_add_ipc_ns_to_ctx(<tng_static_ctx); | |
322 | if (ret && ret != -ENOSYS) { | |
5a15f70c | 323 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_ipc_ns_to_ctx"); |
a6cf40a4 MJ |
324 | } |
325 | ret = lttng_add_mnt_ns_to_ctx(<tng_static_ctx); | |
326 | if (ret && ret != -ENOSYS) { | |
5a15f70c | 327 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_mnt_ns_to_ctx"); |
a6cf40a4 MJ |
328 | } |
329 | ret = lttng_add_net_ns_to_ctx(<tng_static_ctx); | |
330 | if (ret && ret != -ENOSYS) { | |
5a15f70c | 331 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_net_ns_to_ctx"); |
a6cf40a4 MJ |
332 | } |
333 | ret = lttng_add_pid_ns_to_ctx(<tng_static_ctx); | |
334 | if (ret && ret != -ENOSYS) { | |
5a15f70c | 335 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_pid_ns_to_ctx"); |
a6cf40a4 MJ |
336 | } |
337 | ret = lttng_add_user_ns_to_ctx(<tng_static_ctx); | |
338 | if (ret && ret != -ENOSYS) { | |
5a15f70c | 339 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_user_ns_to_ctx"); |
a6cf40a4 MJ |
340 | } |
341 | ret = lttng_add_uts_ns_to_ctx(<tng_static_ctx); | |
342 | if (ret && ret != -ENOSYS) { | |
5a15f70c | 343 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_uts_ns_to_ctx"); |
a6cf40a4 | 344 | } |
876e2e92 MJ |
345 | ret = lttng_add_time_ns_to_ctx(<tng_static_ctx); |
346 | if (ret && ret != -ENOSYS) { | |
5a15f70c | 347 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_time_ns_to_ctx"); |
876e2e92 | 348 | } |
f127e61e | 349 | /* TODO: perf counters for filtering */ |
07dfc1d0 MD |
350 | return 0; |
351 | } | |
352 | ||
353 | void lttng_context_exit(void) | |
354 | { | |
355 | lttng_destroy_context(lttng_static_ctx); | |
356 | lttng_static_ctx = NULL; | |
357 | } |