| 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_mappings() */ |
| 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 | if (!ctx) |
| 33 | return 0; |
| 34 | for (i = 0; i < ctx->nr_fields; i++) { |
| 35 | /* Skip allocated (but non-initialized) contexts */ |
| 36 | if (!ctx->fields[i].event_field.name) |
| 37 | continue; |
| 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 | |
| 45 | int lttng_get_context_index(struct lttng_ctx *ctx, const char *name) |
| 46 | { |
| 47 | unsigned int i; |
| 48 | const char *subname; |
| 49 | |
| 50 | if (!ctx) |
| 51 | return -1; |
| 52 | if (strncmp(name, "$ctx.", strlen("$ctx.")) == 0) { |
| 53 | subname = name + strlen("$ctx."); |
| 54 | } else { |
| 55 | subname = name; |
| 56 | } |
| 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; |
| 61 | if (!strcmp(ctx->fields[i].event_field.name, subname)) |
| 62 | return i; |
| 63 | } |
| 64 | return -1; |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(lttng_get_context_index); |
| 67 | |
| 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 | |
| 77 | /* |
| 78 | * Note: as we append context information, the pointer location may change. |
| 79 | */ |
| 80 | ssize_t lttng_append_context_index(struct lttng_ctx **ctx_p) |
| 81 | { |
| 82 | struct lttng_ctx *ctx; |
| 83 | ssize_t pos = -1; |
| 84 | |
| 85 | if (!*ctx_p) { |
| 86 | *ctx_p = kzalloc(sizeof(struct lttng_ctx), GFP_KERNEL); |
| 87 | if (!*ctx_p) |
| 88 | goto end; |
| 89 | (*ctx_p)->largest_align = 1; |
| 90 | } |
| 91 | ctx = *ctx_p; |
| 92 | if (ctx->nr_fields + 1 > ctx->allocated_fields) { |
| 93 | struct lttng_ctx_field *new_fields; |
| 94 | |
| 95 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); |
| 96 | new_fields = lttng_kvzalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field), GFP_KERNEL); |
| 97 | if (!new_fields) |
| 98 | goto end; |
| 99 | if (ctx->fields) |
| 100 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); |
| 101 | lttng_kvfree(ctx->fields); |
| 102 | ctx->fields = new_fields; |
| 103 | } |
| 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]; |
| 121 | } |
| 122 | EXPORT_SYMBOL_GPL(lttng_append_context); |
| 123 | |
| 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: |
| 140 | field_align = type->u.integer.alignment; |
| 141 | break; |
| 142 | case atype_array_nestable: |
| 143 | { |
| 144 | const struct lttng_type *nested_type; |
| 145 | |
| 146 | nested_type = type->u.array_nestable.elem_type; |
| 147 | switch (nested_type->atype) { |
| 148 | case atype_integer: |
| 149 | field_align = nested_type->u.integer.alignment; |
| 150 | break; |
| 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.array_nestable.alignment); |
| 164 | break; |
| 165 | } |
| 166 | case atype_sequence_nestable: |
| 167 | { |
| 168 | const struct lttng_type *nested_type; |
| 169 | |
| 170 | nested_type = type->u.sequence_nestable.elem_type; |
| 171 | switch (nested_type->atype) { |
| 172 | case atype_integer: |
| 173 | field_align = nested_type->u.integer.alignment; |
| 174 | break; |
| 175 | |
| 176 | case atype_string: |
| 177 | break; |
| 178 | |
| 179 | case atype_array_nestable: |
| 180 | case atype_sequence_nestable: |
| 181 | case atype_struct_nestable: |
| 182 | case atype_variant_nestable: |
| 183 | default: |
| 184 | WARN_ON_ONCE(1); |
| 185 | break; |
| 186 | } |
| 187 | field_align = max_t(size_t, field_align, |
| 188 | type->u.sequence_nestable.alignment); |
| 189 | break; |
| 190 | } |
| 191 | case atype_string: |
| 192 | break; |
| 193 | |
| 194 | case atype_struct_nestable: |
| 195 | case atype_variant_nestable: |
| 196 | break; |
| 197 | |
| 198 | case atype_enum_nestable: |
| 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 | |
| 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 | |
| 224 | /* |
| 225 | * Remove last context field. |
| 226 | */ |
| 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--; |
| 234 | WARN_ON_ONCE(&ctx->fields[ctx->nr_fields] != field); |
| 235 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(lttng_remove_context_field); |
| 238 | |
| 239 | void lttng_destroy_context(struct lttng_ctx *ctx) |
| 240 | { |
| 241 | int i; |
| 242 | |
| 243 | if (!ctx) |
| 244 | return; |
| 245 | for (i = 0; i < ctx->nr_fields; i++) { |
| 246 | if (ctx->fields[i].destroy) |
| 247 | ctx->fields[i].destroy(&ctx->fields[i]); |
| 248 | } |
| 249 | lttng_kvfree(ctx->fields); |
| 250 | kfree(ctx); |
| 251 | } |
| 252 | |
| 253 | int lttng_context_init(void) |
| 254 | { |
| 255 | int ret; |
| 256 | |
| 257 | ret = lttng_add_hostname_to_ctx(<tng_static_ctx); |
| 258 | if (ret) { |
| 259 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_hostname_to_ctx"); |
| 260 | } |
| 261 | ret = lttng_add_nice_to_ctx(<tng_static_ctx); |
| 262 | if (ret) { |
| 263 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_nice_to_ctx"); |
| 264 | } |
| 265 | ret = lttng_add_pid_to_ctx(<tng_static_ctx); |
| 266 | if (ret) { |
| 267 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_pid_to_ctx"); |
| 268 | } |
| 269 | ret = lttng_add_ppid_to_ctx(<tng_static_ctx); |
| 270 | if (ret) { |
| 271 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_ppid_to_ctx"); |
| 272 | } |
| 273 | ret = lttng_add_prio_to_ctx(<tng_static_ctx); |
| 274 | if (ret) { |
| 275 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_prio_to_ctx"); |
| 276 | } |
| 277 | ret = lttng_add_procname_to_ctx(<tng_static_ctx); |
| 278 | if (ret) { |
| 279 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_procname_to_ctx"); |
| 280 | } |
| 281 | ret = lttng_add_tid_to_ctx(<tng_static_ctx); |
| 282 | if (ret) { |
| 283 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_tid_to_ctx"); |
| 284 | } |
| 285 | ret = lttng_add_vppid_to_ctx(<tng_static_ctx); |
| 286 | if (ret) { |
| 287 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_vppid_to_ctx"); |
| 288 | } |
| 289 | ret = lttng_add_vtid_to_ctx(<tng_static_ctx); |
| 290 | if (ret) { |
| 291 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_vtid_to_ctx"); |
| 292 | } |
| 293 | ret = lttng_add_vpid_to_ctx(<tng_static_ctx); |
| 294 | if (ret) { |
| 295 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_vpid_to_ctx"); |
| 296 | } |
| 297 | ret = lttng_add_cpu_id_to_ctx(<tng_static_ctx); |
| 298 | if (ret) { |
| 299 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_cpu_id_to_ctx"); |
| 300 | } |
| 301 | ret = lttng_add_interruptible_to_ctx(<tng_static_ctx); |
| 302 | if (ret) { |
| 303 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_interruptible_to_ctx"); |
| 304 | } |
| 305 | ret = lttng_add_need_reschedule_to_ctx(<tng_static_ctx); |
| 306 | if (ret) { |
| 307 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_need_reschedule_to_ctx"); |
| 308 | } |
| 309 | ret = lttng_add_preemptible_to_ctx(<tng_static_ctx); |
| 310 | if (ret && ret != -ENOSYS) { |
| 311 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_preemptible_to_ctx"); |
| 312 | } |
| 313 | ret = lttng_add_migratable_to_ctx(<tng_static_ctx); |
| 314 | if (ret && ret != -ENOSYS) { |
| 315 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_migratable_to_ctx"); |
| 316 | } |
| 317 | ret = lttng_add_cgroup_ns_to_ctx(<tng_static_ctx); |
| 318 | if (ret && ret != -ENOSYS) { |
| 319 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_cgroup_ns_to_ctx"); |
| 320 | } |
| 321 | ret = lttng_add_ipc_ns_to_ctx(<tng_static_ctx); |
| 322 | if (ret && ret != -ENOSYS) { |
| 323 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_ipc_ns_to_ctx"); |
| 324 | } |
| 325 | ret = lttng_add_mnt_ns_to_ctx(<tng_static_ctx); |
| 326 | if (ret && ret != -ENOSYS) { |
| 327 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_mnt_ns_to_ctx"); |
| 328 | } |
| 329 | ret = lttng_add_net_ns_to_ctx(<tng_static_ctx); |
| 330 | if (ret && ret != -ENOSYS) { |
| 331 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_net_ns_to_ctx"); |
| 332 | } |
| 333 | ret = lttng_add_pid_ns_to_ctx(<tng_static_ctx); |
| 334 | if (ret && ret != -ENOSYS) { |
| 335 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_pid_ns_to_ctx"); |
| 336 | } |
| 337 | ret = lttng_add_user_ns_to_ctx(<tng_static_ctx); |
| 338 | if (ret && ret != -ENOSYS) { |
| 339 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_user_ns_to_ctx"); |
| 340 | } |
| 341 | ret = lttng_add_uts_ns_to_ctx(<tng_static_ctx); |
| 342 | if (ret && ret != -ENOSYS) { |
| 343 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_uts_ns_to_ctx"); |
| 344 | } |
| 345 | ret = lttng_add_time_ns_to_ctx(<tng_static_ctx); |
| 346 | if (ret && ret != -ENOSYS) { |
| 347 | printk(KERN_WARNING "LTTng: Cannot add context lttng_add_time_ns_to_ctx"); |
| 348 | } |
| 349 | /* TODO: perf counters for filtering */ |
| 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 | } |