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> | |
241ae9a8 MD |
14 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */ |
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 | ||
32 | for (i = 0; i < ctx->nr_fields; i++) { | |
4b58a8e4 MD |
33 | /* Skip allocated (but non-initialized) contexts */ |
34 | if (!ctx->fields[i].event_field.name) | |
35 | continue; | |
44252f0f MD |
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 | ||
07dfc1d0 MD |
43 | int lttng_get_context_index(struct lttng_ctx *ctx, const char *name) |
44 | { | |
45 | unsigned int i; | |
0451bcec | 46 | const char *subname; |
07dfc1d0 MD |
47 | |
48 | if (!ctx) | |
49 | return -1; | |
0451bcec MD |
50 | if (strncmp(name, "$ctx.", strlen("$ctx.")) == 0) { |
51 | subname = name + strlen("$ctx."); | |
52 | } else { | |
53 | subname = name; | |
54 | } | |
07dfc1d0 MD |
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; | |
0451bcec | 59 | if (!strcmp(ctx->fields[i].event_field.name, subname)) |
07dfc1d0 MD |
60 | return i; |
61 | } | |
62 | return -1; | |
63 | } | |
64 | EXPORT_SYMBOL_GPL(lttng_get_context_index); | |
65 | ||
2001023e MD |
66 | /* |
67 | * Note: as we append context information, the pointer location may change. | |
68 | */ | |
2dccf128 MD |
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; | |
a9dd15da | 78 | (*ctx_p)->largest_align = 1; |
2dccf128 MD |
79 | } |
80 | ctx = *ctx_p; | |
81 | if (ctx->nr_fields + 1 > ctx->allocated_fields) { | |
82 | struct lttng_ctx_field *new_fields; | |
83 | ||
0f034e0f | 84 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); |
48f5e0b5 | 85 | new_fields = lttng_kvzalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field), GFP_KERNEL); |
2dccf128 MD |
86 | if (!new_fields) |
87 | return NULL; | |
88 | if (ctx->fields) | |
77aefe99 | 89 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); |
48f5e0b5 | 90 | lttng_kvfree(ctx->fields); |
2dccf128 MD |
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 | ||
a9dd15da MD |
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: | |
ceabb767 | 115 | field_align = type->u.integer.alignment; |
a9dd15da | 116 | break; |
ceabb767 | 117 | case atype_array_nestable: |
a9dd15da | 118 | { |
ceabb767 | 119 | const struct lttng_type *nested_type; |
a9dd15da | 120 | |
ceabb767 MD |
121 | nested_type = type->u.array_nestable.elem_type; |
122 | switch (nested_type->atype) { | |
a9dd15da | 123 | case atype_integer: |
ceabb767 | 124 | field_align = nested_type->u.integer.alignment; |
a9dd15da MD |
125 | break; |
126 | case atype_string: | |
127 | break; | |
128 | ||
ceabb767 MD |
129 | case atype_array_nestable: |
130 | case atype_sequence_nestable: | |
131 | case atype_struct_nestable: | |
132 | case atype_variant_nestable: | |
a9dd15da MD |
133 | default: |
134 | WARN_ON_ONCE(1); | |
135 | break; | |
136 | } | |
ceabb767 MD |
137 | field_align = max_t(size_t, field_align, |
138 | type->u.array_nestable.alignment); | |
a9dd15da MD |
139 | break; |
140 | } | |
ceabb767 | 141 | case atype_sequence_nestable: |
a9dd15da | 142 | { |
ceabb767 | 143 | const struct lttng_type *nested_type; |
a9dd15da | 144 | |
ceabb767 MD |
145 | nested_type = type->u.sequence_nestable.elem_type; |
146 | switch (nested_type->atype) { | |
a9dd15da | 147 | case atype_integer: |
ceabb767 | 148 | field_align = nested_type->u.integer.alignment; |
a9dd15da MD |
149 | break; |
150 | ||
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.sequence_nestable.alignment); | |
a9dd15da MD |
164 | break; |
165 | } | |
166 | case atype_string: | |
167 | break; | |
168 | ||
ceabb767 MD |
169 | case atype_struct_nestable: |
170 | case atype_variant_nestable: | |
f513b2bf MD |
171 | break; |
172 | ||
ceabb767 | 173 | case atype_enum_nestable: |
a9dd15da MD |
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 | ||
4cae220c MD |
183 | /* |
184 | * Remove last context field. | |
185 | */ | |
8289661d MD |
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--; | |
4cae220c | 193 | WARN_ON_ONCE(&ctx->fields[ctx->nr_fields] != field); |
8289661d MD |
194 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); |
195 | } | |
196 | EXPORT_SYMBOL_GPL(lttng_remove_context_field); | |
197 | ||
2dccf128 MD |
198 | void lttng_destroy_context(struct lttng_ctx *ctx) |
199 | { | |
200 | int i; | |
201 | ||
8070f5c0 MD |
202 | if (!ctx) |
203 | return; | |
9e7e4892 MD |
204 | for (i = 0; i < ctx->nr_fields; i++) { |
205 | if (ctx->fields[i].destroy) | |
206 | ctx->fields[i].destroy(&ctx->fields[i]); | |
207 | } | |
48f5e0b5 | 208 | lttng_kvfree(ctx->fields); |
2dccf128 MD |
209 | kfree(ctx); |
210 | } | |
07dfc1d0 MD |
211 | |
212 | int lttng_context_init(void) | |
213 | { | |
214 | int ret; | |
215 | ||
f127e61e MD |
216 | ret = lttng_add_hostname_to_ctx(<tng_static_ctx); |
217 | if (ret) { | |
38c4a82c | 218 | printk(KERN_WARNING "Cannot add context lttng_add_hostname_to_ctx"); |
f127e61e MD |
219 | } |
220 | ret = lttng_add_nice_to_ctx(<tng_static_ctx); | |
07dfc1d0 | 221 | if (ret) { |
38c4a82c | 222 | printk(KERN_WARNING "Cannot add context lttng_add_nice_to_ctx"); |
07dfc1d0 | 223 | } |
07dfc1d0 MD |
224 | ret = lttng_add_pid_to_ctx(<tng_static_ctx); |
225 | if (ret) { | |
38c4a82c | 226 | printk(KERN_WARNING "Cannot add context lttng_add_pid_to_ctx"); |
07dfc1d0 | 227 | } |
f127e61e | 228 | ret = lttng_add_ppid_to_ctx(<tng_static_ctx); |
07dfc1d0 | 229 | if (ret) { |
38c4a82c | 230 | printk(KERN_WARNING "Cannot add context lttng_add_ppid_to_ctx"); |
07dfc1d0 MD |
231 | } |
232 | ret = lttng_add_prio_to_ctx(<tng_static_ctx); | |
233 | if (ret) { | |
38c4a82c | 234 | printk(KERN_WARNING "Cannot add context lttng_add_prio_to_ctx"); |
07dfc1d0 | 235 | } |
f127e61e | 236 | ret = lttng_add_procname_to_ctx(<tng_static_ctx); |
07dfc1d0 | 237 | if (ret) { |
38c4a82c | 238 | printk(KERN_WARNING "Cannot add context lttng_add_procname_to_ctx"); |
07dfc1d0 MD |
239 | } |
240 | ret = lttng_add_tid_to_ctx(<tng_static_ctx); | |
241 | if (ret) { | |
38c4a82c | 242 | printk(KERN_WARNING "Cannot add context lttng_add_tid_to_ctx"); |
07dfc1d0 | 243 | } |
f127e61e | 244 | ret = lttng_add_vppid_to_ctx(<tng_static_ctx); |
07dfc1d0 | 245 | if (ret) { |
38c4a82c | 246 | printk(KERN_WARNING "Cannot add context lttng_add_vppid_to_ctx"); |
07dfc1d0 | 247 | } |
f127e61e | 248 | ret = lttng_add_vtid_to_ctx(<tng_static_ctx); |
07dfc1d0 | 249 | if (ret) { |
38c4a82c | 250 | printk(KERN_WARNING "Cannot add context lttng_add_vtid_to_ctx"); |
07dfc1d0 | 251 | } |
f127e61e | 252 | ret = lttng_add_vpid_to_ctx(<tng_static_ctx); |
07dfc1d0 | 253 | if (ret) { |
38c4a82c | 254 | printk(KERN_WARNING "Cannot add context lttng_add_vpid_to_ctx"); |
07dfc1d0 | 255 | } |
b3699d90 MD |
256 | ret = lttng_add_cpu_id_to_ctx(<tng_static_ctx); |
257 | if (ret) { | |
258 | printk(KERN_WARNING "Cannot add context lttng_add_cpu_id_to_ctx"); | |
259 | } | |
79150a49 JD |
260 | ret = lttng_add_interruptible_to_ctx(<tng_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(<tng_static_ctx); | |
265 | if (ret) { | |
266 | printk(KERN_WARNING "Cannot add context lttng_add_need_reschedule_to_ctx"); | |
267 | } | |
79150a49 | 268 | ret = lttng_add_preemptible_to_ctx(<tng_static_ctx); |
d0e59d6f | 269 | if (ret && ret != -ENOSYS) { |
79150a49 JD |
270 | printk(KERN_WARNING "Cannot add context lttng_add_preemptible_to_ctx"); |
271 | } | |
79150a49 | 272 | ret = lttng_add_migratable_to_ctx(<tng_static_ctx); |
d0e59d6f | 273 | if (ret && ret != -ENOSYS) { |
79150a49 JD |
274 | printk(KERN_WARNING "Cannot add context lttng_add_migratable_to_ctx"); |
275 | } | |
a6cf40a4 MJ |
276 | ret = lttng_add_cgroup_ns_to_ctx(<tng_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(<tng_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(<tng_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(<tng_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(<tng_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(<tng_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(<tng_static_ctx); | |
301 | if (ret && ret != -ENOSYS) { | |
302 | printk(KERN_WARNING "Cannot add context lttng_add_uts_ns_to_ctx"); | |
303 | } | |
f127e61e | 304 | /* TODO: perf counters for filtering */ |
07dfc1d0 MD |
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 | } |