| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * LTTng UST trace/channel/event context management. |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <lttng/ust-events.h> |
| 11 | #include <lttng/ust-tracer.h> |
| 12 | #include <ust-context-provider.h> |
| 13 | #include <lttng/urcu/pointer.h> |
| 14 | #include <lttng/urcu/urcu-ust.h> |
| 15 | #include <usterr-signal-safe.h> |
| 16 | #include <ust-helper.h> |
| 17 | #include <stddef.h> |
| 18 | #include <string.h> |
| 19 | #include <assert.h> |
| 20 | #include "tracepoint-internal.h" |
| 21 | |
| 22 | #include "context-internal.h" |
| 23 | |
| 24 | /* |
| 25 | * The filter implementation requires that two consecutive "get" for the |
| 26 | * same context performed by the same thread return the same result. |
| 27 | */ |
| 28 | |
| 29 | int lttng_find_context(struct lttng_ust_ctx *ctx, const char *name) |
| 30 | { |
| 31 | unsigned int i; |
| 32 | const char *subname; |
| 33 | |
| 34 | if (strncmp(name, "$ctx.", strlen("$ctx.")) == 0) { |
| 35 | subname = name + strlen("$ctx."); |
| 36 | } else { |
| 37 | subname = name; |
| 38 | } |
| 39 | for (i = 0; i < ctx->nr_fields; i++) { |
| 40 | /* Skip allocated (but non-initialized) contexts */ |
| 41 | if (!ctx->fields[i]->event_field->name) |
| 42 | continue; |
| 43 | if (!strcmp(ctx->fields[i]->event_field->name, subname)) |
| 44 | return 1; |
| 45 | } |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | int lttng_get_context_index(struct lttng_ust_ctx *ctx, const char *name) |
| 50 | { |
| 51 | unsigned int i; |
| 52 | const char *subname; |
| 53 | |
| 54 | if (!ctx) |
| 55 | return -1; |
| 56 | if (strncmp(name, "$ctx.", strlen("$ctx.")) == 0) { |
| 57 | subname = name + strlen("$ctx."); |
| 58 | } else { |
| 59 | subname = name; |
| 60 | } |
| 61 | for (i = 0; i < ctx->nr_fields; i++) { |
| 62 | /* Skip allocated (but non-initialized) contexts */ |
| 63 | if (!ctx->fields[i]->event_field->name) |
| 64 | continue; |
| 65 | if (!strcmp(ctx->fields[i]->event_field->name, subname)) |
| 66 | return i; |
| 67 | } |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | static int lttng_find_context_provider(struct lttng_ust_ctx *ctx, const char *name) |
| 72 | { |
| 73 | unsigned int i; |
| 74 | |
| 75 | for (i = 0; i < ctx->nr_fields; i++) { |
| 76 | /* Skip allocated (but non-initialized) contexts */ |
| 77 | if (!ctx->fields[i]->event_field->name) |
| 78 | continue; |
| 79 | if (!strncmp(ctx->fields[i]->event_field->name, name, |
| 80 | strlen(name))) |
| 81 | return 1; |
| 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Note: as we append context information, the pointer location may change. |
| 88 | * lttng_add_context leaves the new last context initialized to NULL. |
| 89 | */ |
| 90 | static |
| 91 | int lttng_add_context(struct lttng_ust_ctx **ctx_p) |
| 92 | { |
| 93 | struct lttng_ust_ctx *ctx; |
| 94 | |
| 95 | if (!*ctx_p) { |
| 96 | *ctx_p = zmalloc(sizeof(struct lttng_ust_ctx)); |
| 97 | if (!*ctx_p) |
| 98 | return -ENOMEM; |
| 99 | (*ctx_p)->struct_size = sizeof(struct lttng_ust_ctx); |
| 100 | (*ctx_p)->largest_align = 1; |
| 101 | } |
| 102 | ctx = *ctx_p; |
| 103 | if (ctx->nr_fields + 1 > ctx->allocated_fields) { |
| 104 | struct lttng_ust_ctx_field **new_fields; |
| 105 | |
| 106 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); |
| 107 | new_fields = zmalloc(ctx->allocated_fields * sizeof(*new_fields)); |
| 108 | if (!new_fields) |
| 109 | return -ENOMEM; |
| 110 | /* Copy pointers */ |
| 111 | if (ctx->fields) |
| 112 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); |
| 113 | free(ctx->fields); |
| 114 | ctx->fields = new_fields; |
| 115 | } |
| 116 | ctx->nr_fields++; |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | struct lttng_ust_ctx_field *lttng_append_context(struct lttng_ust_ctx **ctx_p) |
| 121 | { |
| 122 | struct lttng_ust_ctx_field *field; |
| 123 | int ret; |
| 124 | |
| 125 | field = zmalloc(sizeof(struct lttng_ust_ctx_field)); |
| 126 | if (!field) |
| 127 | goto error_alloc_field; |
| 128 | field->struct_size = sizeof(struct lttng_ust_ctx_field); |
| 129 | field->event_field = zmalloc(sizeof(struct lttng_ust_event_field)); |
| 130 | if (!field->event_field) |
| 131 | goto error_alloc_event_field; |
| 132 | field->event_field->struct_size = sizeof(struct lttng_ust_event_field); |
| 133 | |
| 134 | ret = lttng_add_context(ctx_p); |
| 135 | if (ret) |
| 136 | goto error_add_context; |
| 137 | (*ctx_p)->fields[(*ctx_p)->nr_fields - 1] = field; |
| 138 | return field; |
| 139 | |
| 140 | error_add_context: |
| 141 | free(field->event_field); |
| 142 | error_alloc_event_field: |
| 143 | free(field); |
| 144 | error_alloc_field: |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Takes ownership of @f on success. |
| 150 | */ |
| 151 | int lttng_context_add_rcu(struct lttng_ust_ctx **ctx_p, |
| 152 | struct lttng_ust_ctx_field *f) |
| 153 | { |
| 154 | struct lttng_ust_ctx *old_ctx = *ctx_p, *new_ctx = NULL; |
| 155 | struct lttng_ust_ctx_field **new_fields = NULL; |
| 156 | int ret; |
| 157 | |
| 158 | if (old_ctx) { |
| 159 | new_ctx = zmalloc(sizeof(struct lttng_ust_ctx)); |
| 160 | if (!new_ctx) |
| 161 | return -ENOMEM; |
| 162 | new_ctx->struct_size = sizeof(struct lttng_ust_ctx); |
| 163 | *new_ctx = *old_ctx; |
| 164 | new_fields = zmalloc(new_ctx->allocated_fields * sizeof(*new_fields)); |
| 165 | if (!new_fields) { |
| 166 | free(new_ctx); |
| 167 | return -ENOMEM; |
| 168 | } |
| 169 | /* Copy pointers */ |
| 170 | memcpy(new_fields, old_ctx->fields, |
| 171 | sizeof(*old_ctx->fields) * old_ctx->nr_fields); |
| 172 | new_ctx->fields = new_fields; |
| 173 | } |
| 174 | ret = lttng_add_context(&new_ctx); |
| 175 | if (ret) { |
| 176 | free(new_fields); |
| 177 | free(new_ctx); |
| 178 | return ret; |
| 179 | } |
| 180 | /* Taking ownership of f. */ |
| 181 | (*ctx_p)->fields[(*ctx_p)->nr_fields - 1] = f; |
| 182 | lttng_context_update(new_ctx); |
| 183 | lttng_ust_rcu_assign_pointer(*ctx_p, new_ctx); |
| 184 | lttng_ust_urcu_synchronize_rcu(); |
| 185 | if (old_ctx) { |
| 186 | free(old_ctx->fields); |
| 187 | free(old_ctx); |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static size_t get_type_max_align(struct lttng_ust_type_common *type) |
| 193 | { |
| 194 | switch (type->type) { |
| 195 | case lttng_ust_type_integer: |
| 196 | return lttng_ust_get_type_integer(type)->alignment; |
| 197 | case lttng_ust_type_string: |
| 198 | return CHAR_BIT; |
| 199 | case lttng_ust_type_dynamic: |
| 200 | return 0; |
| 201 | case lttng_ust_type_enum: |
| 202 | return get_type_max_align(lttng_ust_get_type_enum(type)->container_type); |
| 203 | case lttng_ust_type_array: |
| 204 | return max_t(size_t, get_type_max_align(lttng_ust_get_type_array(type)->elem_type), |
| 205 | lttng_ust_get_type_array(type)->alignment); |
| 206 | case lttng_ust_type_sequence: |
| 207 | return max_t(size_t, get_type_max_align(lttng_ust_get_type_sequence(type)->elem_type), |
| 208 | lttng_ust_get_type_sequence(type)->alignment); |
| 209 | case lttng_ust_type_struct: |
| 210 | { |
| 211 | unsigned int i; |
| 212 | size_t field_align = 0; |
| 213 | struct lttng_ust_type_struct *struct_type = lttng_ust_get_type_struct(type); |
| 214 | |
| 215 | for (i = 0; i < struct_type->nr_fields; i++) { |
| 216 | field_align = max_t(size_t, |
| 217 | get_type_max_align(struct_type->fields[i]->type), |
| 218 | field_align); |
| 219 | } |
| 220 | return field_align; |
| 221 | } |
| 222 | default: |
| 223 | WARN_ON_ONCE(1); |
| 224 | return 0; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * lttng_context_update() should be called at least once between context |
| 230 | * modification and trace start. |
| 231 | */ |
| 232 | void lttng_context_update(struct lttng_ust_ctx *ctx) |
| 233 | { |
| 234 | int i; |
| 235 | size_t largest_align = 8; /* in bits */ |
| 236 | |
| 237 | for (i = 0; i < ctx->nr_fields; i++) { |
| 238 | size_t field_align = 8; |
| 239 | |
| 240 | field_align = get_type_max_align(ctx->fields[i]->event_field->type); |
| 241 | largest_align = max_t(size_t, largest_align, field_align); |
| 242 | } |
| 243 | ctx->largest_align = largest_align >> 3; /* bits to bytes */ |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Remove last context field. |
| 248 | */ |
| 249 | void lttng_remove_context_field(struct lttng_ust_ctx **ctx_p, |
| 250 | struct lttng_ust_ctx_field *field) |
| 251 | { |
| 252 | struct lttng_ust_ctx *ctx; |
| 253 | |
| 254 | ctx = *ctx_p; |
| 255 | ctx->nr_fields--; |
| 256 | assert(ctx->fields[ctx->nr_fields] == field); |
| 257 | lttng_ust_destroy_type(field->event_field->type); |
| 258 | free((char *) field->event_field->name); |
| 259 | free(field->event_field); |
| 260 | free(field); |
| 261 | ctx->fields[ctx->nr_fields] = NULL; |
| 262 | } |
| 263 | |
| 264 | void lttng_destroy_context(struct lttng_ust_ctx *ctx) |
| 265 | { |
| 266 | int i; |
| 267 | |
| 268 | if (!ctx) |
| 269 | return; |
| 270 | for (i = 0; i < ctx->nr_fields; i++) { |
| 271 | if (ctx->fields[i]->destroy) |
| 272 | ctx->fields[i]->destroy(ctx->fields[i]); |
| 273 | lttng_ust_destroy_type(ctx->fields[i]->event_field->type); |
| 274 | free((char *) ctx->fields[i]->event_field->name); |
| 275 | free(ctx->fields[i]->event_field); |
| 276 | free(ctx->fields[i]); |
| 277 | } |
| 278 | free(ctx->fields); |
| 279 | free(ctx); |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Can be safely performed concurrently with tracing using the struct |
| 284 | * lttng_ctx. Using RCU update. Needs to match RCU read-side handling of |
| 285 | * contexts. |
| 286 | * |
| 287 | * This does not allow adding, removing, or changing typing of the |
| 288 | * contexts, since this needs to stay invariant for metadata. However, |
| 289 | * it allows updating the handlers associated with all contexts matching |
| 290 | * a provider (by name) while tracing is using it, in a way that ensures |
| 291 | * a single RCU read-side critical section see either all old, or all |
| 292 | * new handlers. |
| 293 | */ |
| 294 | int lttng_ust_context_set_provider_rcu(struct lttng_ust_ctx **_ctx, |
| 295 | const char *name, |
| 296 | size_t (*get_size)(struct lttng_ust_ctx_field *field, size_t offset), |
| 297 | void (*record)(struct lttng_ust_ctx_field *field, |
| 298 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 299 | struct lttng_channel *chan), |
| 300 | void (*get_value)(struct lttng_ust_ctx_field *field, |
| 301 | struct lttng_ust_ctx_value *value)) |
| 302 | { |
| 303 | int i, ret; |
| 304 | struct lttng_ust_ctx *ctx = *_ctx, *new_ctx; |
| 305 | struct lttng_ust_ctx_field **new_fields; |
| 306 | |
| 307 | if (!ctx || !lttng_find_context_provider(ctx, name)) |
| 308 | return 0; |
| 309 | /* |
| 310 | * We have at least one instance of context for the provider. |
| 311 | */ |
| 312 | new_ctx = zmalloc(sizeof(*new_ctx)); |
| 313 | if (!new_ctx) |
| 314 | return -ENOMEM; |
| 315 | new_ctx->struct_size = sizeof(*new_ctx); |
| 316 | *new_ctx = *ctx; |
| 317 | new_fields = zmalloc(sizeof(*new_fields) * ctx->allocated_fields); |
| 318 | if (!new_fields) { |
| 319 | ret = -ENOMEM; |
| 320 | goto field_error; |
| 321 | } |
| 322 | /* Copy pointers */ |
| 323 | memcpy(new_fields, ctx->fields, |
| 324 | sizeof(*new_fields) * ctx->allocated_fields); |
| 325 | for (i = 0; i < ctx->nr_fields; i++) { |
| 326 | if (strncmp(new_fields[i]->event_field->name, |
| 327 | name, strlen(name)) != 0) |
| 328 | continue; |
| 329 | new_fields[i]->get_size = get_size; |
| 330 | new_fields[i]->record = record; |
| 331 | new_fields[i]->get_value = get_value; |
| 332 | } |
| 333 | new_ctx->fields = new_fields; |
| 334 | lttng_ust_rcu_assign_pointer(*_ctx, new_ctx); |
| 335 | lttng_ust_urcu_synchronize_rcu(); |
| 336 | free(ctx->fields); |
| 337 | free(ctx); |
| 338 | return 0; |
| 339 | |
| 340 | field_error: |
| 341 | free(new_ctx); |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | int lttng_context_init_all(struct lttng_ust_ctx **ctx) |
| 346 | { |
| 347 | int ret; |
| 348 | |
| 349 | ret = lttng_add_pthread_id_to_ctx(ctx); |
| 350 | if (ret) { |
| 351 | WARN("Cannot add context lttng_add_pthread_id_to_ctx"); |
| 352 | goto error; |
| 353 | } |
| 354 | ret = lttng_add_vtid_to_ctx(ctx); |
| 355 | if (ret) { |
| 356 | WARN("Cannot add context lttng_add_vtid_to_ctx"); |
| 357 | goto error; |
| 358 | } |
| 359 | ret = lttng_add_vpid_to_ctx(ctx); |
| 360 | if (ret) { |
| 361 | WARN("Cannot add context lttng_add_vpid_to_ctx"); |
| 362 | goto error; |
| 363 | } |
| 364 | ret = lttng_add_procname_to_ctx(ctx); |
| 365 | if (ret) { |
| 366 | WARN("Cannot add context lttng_add_procname_to_ctx"); |
| 367 | goto error; |
| 368 | } |
| 369 | ret = lttng_add_cpu_id_to_ctx(ctx); |
| 370 | if (ret) { |
| 371 | WARN("Cannot add context lttng_add_cpu_id_to_ctx"); |
| 372 | goto error; |
| 373 | } |
| 374 | ret = lttng_add_cgroup_ns_to_ctx(ctx); |
| 375 | if (ret) { |
| 376 | WARN("Cannot add context lttng_add_cgroup_ns_to_ctx"); |
| 377 | goto error; |
| 378 | } |
| 379 | ret = lttng_add_ipc_ns_to_ctx(ctx); |
| 380 | if (ret) { |
| 381 | WARN("Cannot add context lttng_add_ipc_ns_to_ctx"); |
| 382 | goto error; |
| 383 | } |
| 384 | ret = lttng_add_mnt_ns_to_ctx(ctx); |
| 385 | if (ret) { |
| 386 | WARN("Cannot add context lttng_add_mnt_ns_to_ctx"); |
| 387 | goto error; |
| 388 | } |
| 389 | ret = lttng_add_net_ns_to_ctx(ctx); |
| 390 | if (ret) { |
| 391 | WARN("Cannot add context lttng_add_net_ns_to_ctx"); |
| 392 | goto error; |
| 393 | } |
| 394 | ret = lttng_add_pid_ns_to_ctx(ctx); |
| 395 | if (ret) { |
| 396 | WARN("Cannot add context lttng_add_pid_ns_to_ctx"); |
| 397 | goto error; |
| 398 | } |
| 399 | ret = lttng_add_time_ns_to_ctx(ctx); |
| 400 | if (ret) { |
| 401 | WARN("Cannot add context lttng_add_time_ns_to_ctx"); |
| 402 | goto error; |
| 403 | } |
| 404 | ret = lttng_add_user_ns_to_ctx(ctx); |
| 405 | if (ret) { |
| 406 | WARN("Cannot add context lttng_add_user_ns_to_ctx"); |
| 407 | goto error; |
| 408 | } |
| 409 | ret = lttng_add_uts_ns_to_ctx(ctx); |
| 410 | if (ret) { |
| 411 | WARN("Cannot add context lttng_add_uts_ns_to_ctx"); |
| 412 | goto error; |
| 413 | } |
| 414 | ret = lttng_add_vuid_to_ctx(ctx); |
| 415 | if (ret) { |
| 416 | WARN("Cannot add context lttng_add_vuid_to_ctx"); |
| 417 | goto error; |
| 418 | } |
| 419 | ret = lttng_add_veuid_to_ctx(ctx); |
| 420 | if (ret) { |
| 421 | WARN("Cannot add context lttng_add_veuid_to_ctx"); |
| 422 | goto error; |
| 423 | } |
| 424 | ret = lttng_add_vsuid_to_ctx(ctx); |
| 425 | if (ret) { |
| 426 | WARN("Cannot add context lttng_add_vsuid_to_ctx"); |
| 427 | goto error; |
| 428 | } |
| 429 | ret = lttng_add_vgid_to_ctx(ctx); |
| 430 | if (ret) { |
| 431 | WARN("Cannot add context lttng_add_vgid_to_ctx"); |
| 432 | goto error; |
| 433 | } |
| 434 | ret = lttng_add_vegid_to_ctx(ctx); |
| 435 | if (ret) { |
| 436 | WARN("Cannot add context lttng_add_vegid_to_ctx"); |
| 437 | goto error; |
| 438 | } |
| 439 | ret = lttng_add_vsgid_to_ctx(ctx); |
| 440 | if (ret) { |
| 441 | WARN("Cannot add context lttng_add_vsgid_to_ctx"); |
| 442 | goto error; |
| 443 | } |
| 444 | lttng_context_update(*ctx); |
| 445 | return 0; |
| 446 | |
| 447 | error: |
| 448 | lttng_destroy_context(*ctx); |
| 449 | return ret; |
| 450 | } |