| 1 | /* |
| 2 | * lttng-context.c |
| 3 | * |
| 4 | * LTTng UST trace/channel/event context management. |
| 5 | * |
| 6 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; only |
| 11 | * version 2.1 of the License. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #define _LGPL_SOURCE |
| 24 | #include <lttng/ust-events.h> |
| 25 | #include <lttng/ust-tracer.h> |
| 26 | #include <lttng/ust-context-provider.h> |
| 27 | #include <urcu-pointer.h> |
| 28 | #include <usterr-signal-safe.h> |
| 29 | #include <helper.h> |
| 30 | #include <stddef.h> |
| 31 | #include <string.h> |
| 32 | #include <assert.h> |
| 33 | |
| 34 | #include "context-internal.h" |
| 35 | |
| 36 | /* |
| 37 | * The filter implementation requires that two consecutive "get" for the |
| 38 | * same context performed by the same thread return the same result. |
| 39 | */ |
| 40 | |
| 41 | int lttng_find_context(struct lttng_ctx *ctx, const char *name) |
| 42 | { |
| 43 | unsigned int i; |
| 44 | const char *subname; |
| 45 | |
| 46 | if (strncmp(name, "$ctx.", strlen("$ctx.")) == 0) { |
| 47 | subname = name + strlen("$ctx."); |
| 48 | } else { |
| 49 | subname = name; |
| 50 | } |
| 51 | for (i = 0; i < ctx->nr_fields; i++) { |
| 52 | /* Skip allocated (but non-initialized) contexts */ |
| 53 | if (!ctx->fields[i].event_field.name) |
| 54 | continue; |
| 55 | if (!strcmp(ctx->fields[i].event_field.name, subname)) |
| 56 | return 1; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int lttng_get_context_index(struct lttng_ctx *ctx, const char *name) |
| 62 | { |
| 63 | unsigned int i; |
| 64 | const char *subname; |
| 65 | |
| 66 | if (!ctx) |
| 67 | return -1; |
| 68 | if (strncmp(name, "$ctx.", strlen("$ctx.")) == 0) { |
| 69 | subname = name + strlen("$ctx."); |
| 70 | } else { |
| 71 | subname = name; |
| 72 | } |
| 73 | for (i = 0; i < ctx->nr_fields; i++) { |
| 74 | /* Skip allocated (but non-initialized) contexts */ |
| 75 | if (!ctx->fields[i].event_field.name) |
| 76 | continue; |
| 77 | if (!strcmp(ctx->fields[i].event_field.name, subname)) |
| 78 | return i; |
| 79 | } |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | static int lttng_find_context_provider(struct lttng_ctx *ctx, const char *name) |
| 84 | { |
| 85 | unsigned int i; |
| 86 | |
| 87 | for (i = 0; i < ctx->nr_fields; i++) { |
| 88 | /* Skip allocated (but non-initialized) contexts */ |
| 89 | if (!ctx->fields[i].event_field.name) |
| 90 | continue; |
| 91 | if (!strncmp(ctx->fields[i].event_field.name, name, |
| 92 | strlen(name))) |
| 93 | return 1; |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Note: as we append context information, the pointer location may change. |
| 100 | */ |
| 101 | struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p) |
| 102 | { |
| 103 | struct lttng_ctx_field *field; |
| 104 | struct lttng_ctx *ctx; |
| 105 | |
| 106 | if (!*ctx_p) { |
| 107 | *ctx_p = zmalloc(sizeof(struct lttng_ctx)); |
| 108 | if (!*ctx_p) |
| 109 | return NULL; |
| 110 | (*ctx_p)->largest_align = 1; |
| 111 | } |
| 112 | ctx = *ctx_p; |
| 113 | if (ctx->nr_fields + 1 > ctx->allocated_fields) { |
| 114 | struct lttng_ctx_field *new_fields; |
| 115 | |
| 116 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); |
| 117 | new_fields = zmalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field)); |
| 118 | if (!new_fields) |
| 119 | return NULL; |
| 120 | if (ctx->fields) |
| 121 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); |
| 122 | free(ctx->fields); |
| 123 | ctx->fields = new_fields; |
| 124 | } |
| 125 | field = &ctx->fields[ctx->nr_fields]; |
| 126 | ctx->nr_fields++; |
| 127 | return field; |
| 128 | } |
| 129 | |
| 130 | int lttng_context_add_rcu(struct lttng_ctx **ctx_p, |
| 131 | const struct lttng_ctx_field *f) |
| 132 | { |
| 133 | struct lttng_ctx *old_ctx = *ctx_p, *new_ctx = NULL; |
| 134 | struct lttng_ctx_field *new_fields = NULL; |
| 135 | struct lttng_ctx_field *nf; |
| 136 | |
| 137 | if (old_ctx) { |
| 138 | new_ctx = zmalloc(sizeof(struct lttng_ctx)); |
| 139 | if (!new_ctx) |
| 140 | return -ENOMEM; |
| 141 | *new_ctx = *old_ctx; |
| 142 | new_fields = zmalloc(new_ctx->allocated_fields |
| 143 | * sizeof(struct lttng_ctx_field)); |
| 144 | if (!new_fields) { |
| 145 | free(new_ctx); |
| 146 | return -ENOMEM; |
| 147 | } |
| 148 | memcpy(new_fields, old_ctx->fields, |
| 149 | sizeof(*old_ctx->fields) * old_ctx->nr_fields); |
| 150 | new_ctx->fields = new_fields; |
| 151 | } |
| 152 | nf = lttng_append_context(&new_ctx); |
| 153 | if (!nf) { |
| 154 | free(new_fields); |
| 155 | free(new_ctx); |
| 156 | return -ENOMEM; |
| 157 | } |
| 158 | *nf = *f; |
| 159 | lttng_context_update(new_ctx); |
| 160 | rcu_assign_pointer(*ctx_p, new_ctx); |
| 161 | synchronize_trace(); |
| 162 | if (old_ctx) { |
| 163 | free(old_ctx->fields); |
| 164 | free(old_ctx); |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * lttng_context_update() should be called at least once between context |
| 171 | * modification and trace start. |
| 172 | */ |
| 173 | void lttng_context_update(struct lttng_ctx *ctx) |
| 174 | { |
| 175 | int i; |
| 176 | size_t largest_align = 8; /* in bits */ |
| 177 | |
| 178 | for (i = 0; i < ctx->nr_fields; i++) { |
| 179 | struct lttng_type *type; |
| 180 | size_t field_align = 8; |
| 181 | |
| 182 | type = &ctx->fields[i].event_field.type; |
| 183 | switch (type->atype) { |
| 184 | case atype_integer: |
| 185 | field_align = type->u.integer.alignment; |
| 186 | break; |
| 187 | case atype_array: |
| 188 | { |
| 189 | struct lttng_basic_type *btype; |
| 190 | |
| 191 | btype = &type->u.legacy.array.elem_type; |
| 192 | switch (btype->atype) { |
| 193 | case atype_integer: |
| 194 | field_align = btype->u.basic.integer.alignment; |
| 195 | break; |
| 196 | case atype_string: |
| 197 | break; |
| 198 | |
| 199 | case atype_array: |
| 200 | case atype_array_nestable: |
| 201 | case atype_sequence: |
| 202 | case atype_sequence_nestable: |
| 203 | default: |
| 204 | WARN_ON_ONCE(1); |
| 205 | break; |
| 206 | } |
| 207 | break; |
| 208 | } |
| 209 | case atype_array_nestable: |
| 210 | { |
| 211 | const struct lttng_type *nested_type; |
| 212 | |
| 213 | nested_type = type->u.array_nestable.elem_type; |
| 214 | switch (nested_type->atype) { |
| 215 | case atype_integer: |
| 216 | field_align = nested_type->u.integer.alignment; |
| 217 | break; |
| 218 | case atype_string: |
| 219 | break; |
| 220 | |
| 221 | case atype_array: |
| 222 | case atype_array_nestable: |
| 223 | case atype_sequence: |
| 224 | case atype_sequence_nestable: |
| 225 | default: |
| 226 | WARN_ON_ONCE(1); |
| 227 | break; |
| 228 | } |
| 229 | field_align = max_t(size_t, field_align, |
| 230 | type->u.array_nestable.alignment); |
| 231 | break; |
| 232 | } |
| 233 | case atype_sequence: |
| 234 | { |
| 235 | struct lttng_basic_type *btype; |
| 236 | |
| 237 | btype = &type->u.legacy.sequence.length_type; |
| 238 | switch (btype->atype) { |
| 239 | case atype_integer: |
| 240 | field_align = btype->u.basic.integer.alignment; |
| 241 | break; |
| 242 | |
| 243 | case atype_string: |
| 244 | case atype_array: |
| 245 | case atype_array_nestable: |
| 246 | case atype_sequence: |
| 247 | case atype_sequence_nestable: |
| 248 | default: |
| 249 | WARN_ON_ONCE(1); |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | btype = &type->u.legacy.sequence.elem_type; |
| 254 | switch (btype->atype) { |
| 255 | case atype_integer: |
| 256 | field_align = max_t(size_t, |
| 257 | field_align, |
| 258 | btype->u.basic.integer.alignment); |
| 259 | break; |
| 260 | |
| 261 | case atype_string: |
| 262 | break; |
| 263 | |
| 264 | case atype_array: |
| 265 | case atype_array_nestable: |
| 266 | case atype_sequence: |
| 267 | case atype_sequence_nestable: |
| 268 | default: |
| 269 | WARN_ON_ONCE(1); |
| 270 | break; |
| 271 | } |
| 272 | break; |
| 273 | } |
| 274 | case atype_sequence_nestable: |
| 275 | { |
| 276 | const struct lttng_type *nested_type; |
| 277 | |
| 278 | nested_type = type->u.sequence_nestable.elem_type; |
| 279 | switch (nested_type->atype) { |
| 280 | case atype_integer: |
| 281 | field_align = nested_type->u.integer.alignment; |
| 282 | break; |
| 283 | |
| 284 | case atype_string: |
| 285 | break; |
| 286 | |
| 287 | case atype_array: |
| 288 | case atype_array_nestable: |
| 289 | case atype_sequence: |
| 290 | case atype_sequence_nestable: |
| 291 | default: |
| 292 | WARN_ON_ONCE(1); |
| 293 | break; |
| 294 | } |
| 295 | field_align = max_t(size_t, field_align, |
| 296 | type->u.sequence_nestable.alignment); |
| 297 | break; |
| 298 | } |
| 299 | case atype_string: |
| 300 | break; |
| 301 | case atype_dynamic: |
| 302 | break; |
| 303 | case atype_enum: |
| 304 | case atype_enum_nestable: |
| 305 | default: |
| 306 | WARN_ON_ONCE(1); |
| 307 | break; |
| 308 | } |
| 309 | largest_align = max_t(size_t, largest_align, field_align); |
| 310 | } |
| 311 | ctx->largest_align = largest_align >> 3; /* bits to bytes */ |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Remove last context field. |
| 316 | */ |
| 317 | void lttng_remove_context_field(struct lttng_ctx **ctx_p, |
| 318 | struct lttng_ctx_field *field) |
| 319 | { |
| 320 | struct lttng_ctx *ctx; |
| 321 | |
| 322 | ctx = *ctx_p; |
| 323 | ctx->nr_fields--; |
| 324 | assert(&ctx->fields[ctx->nr_fields] == field); |
| 325 | assert(field->field_name == NULL); |
| 326 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); |
| 327 | } |
| 328 | |
| 329 | void lttng_destroy_context(struct lttng_ctx *ctx) |
| 330 | { |
| 331 | int i; |
| 332 | |
| 333 | if (!ctx) |
| 334 | return; |
| 335 | for (i = 0; i < ctx->nr_fields; i++) { |
| 336 | if (ctx->fields[i].destroy) |
| 337 | ctx->fields[i].destroy(&ctx->fields[i]); |
| 338 | free(ctx->fields[i].field_name); |
| 339 | } |
| 340 | free(ctx->fields); |
| 341 | free(ctx); |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Can be safely performed concurrently with tracing using the struct |
| 346 | * lttng_ctx. Using RCU update. Needs to match RCU read-side handling of |
| 347 | * contexts. |
| 348 | * |
| 349 | * This does not allow adding, removing, or changing typing of the |
| 350 | * contexts, since this needs to stay invariant for metadata. However, |
| 351 | * it allows updating the handlers associated with all contexts matching |
| 352 | * a provider (by name) while tracing is using it, in a way that ensures |
| 353 | * a single RCU read-side critical section see either all old, or all |
| 354 | * new handlers. |
| 355 | */ |
| 356 | int lttng_ust_context_set_provider_rcu(struct lttng_ctx **_ctx, |
| 357 | const char *name, |
| 358 | size_t (*get_size)(struct lttng_ctx_field *field, size_t offset), |
| 359 | void (*record)(struct lttng_ctx_field *field, |
| 360 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 361 | struct lttng_channel *chan), |
| 362 | void (*get_value)(struct lttng_ctx_field *field, |
| 363 | struct lttng_ctx_value *value)) |
| 364 | { |
| 365 | int i, ret; |
| 366 | struct lttng_ctx *ctx = *_ctx, *new_ctx; |
| 367 | struct lttng_ctx_field *new_fields; |
| 368 | |
| 369 | if (!ctx || !lttng_find_context_provider(ctx, name)) |
| 370 | return 0; |
| 371 | /* |
| 372 | * We have at least one instance of context for the provider. |
| 373 | */ |
| 374 | new_ctx = zmalloc(sizeof(*new_ctx)); |
| 375 | if (!new_ctx) |
| 376 | return -ENOMEM; |
| 377 | *new_ctx = *ctx; |
| 378 | new_fields = zmalloc(sizeof(*new_fields) * ctx->allocated_fields); |
| 379 | if (!new_fields) { |
| 380 | ret = -ENOMEM; |
| 381 | goto field_error; |
| 382 | } |
| 383 | memcpy(new_fields, ctx->fields, |
| 384 | sizeof(*new_fields) * ctx->allocated_fields); |
| 385 | for (i = 0; i < ctx->nr_fields; i++) { |
| 386 | if (strncmp(new_fields[i].event_field.name, |
| 387 | name, strlen(name)) != 0) |
| 388 | continue; |
| 389 | new_fields[i].get_size = get_size; |
| 390 | new_fields[i].record = record; |
| 391 | new_fields[i].get_value = get_value; |
| 392 | } |
| 393 | new_ctx->fields = new_fields; |
| 394 | rcu_assign_pointer(*_ctx, new_ctx); |
| 395 | synchronize_trace(); |
| 396 | free(ctx->fields); |
| 397 | free(ctx); |
| 398 | return 0; |
| 399 | |
| 400 | field_error: |
| 401 | free(new_ctx); |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | int lttng_context_init_all(struct lttng_ctx **ctx) |
| 406 | { |
| 407 | int ret; |
| 408 | |
| 409 | ret = lttng_add_pthread_id_to_ctx(ctx); |
| 410 | if (ret) { |
| 411 | WARN("Cannot add context lttng_add_pthread_id_to_ctx"); |
| 412 | goto error; |
| 413 | } |
| 414 | ret = lttng_add_vtid_to_ctx(ctx); |
| 415 | if (ret) { |
| 416 | WARN("Cannot add context lttng_add_vtid_to_ctx"); |
| 417 | goto error; |
| 418 | } |
| 419 | ret = lttng_add_vpid_to_ctx(ctx); |
| 420 | if (ret) { |
| 421 | WARN("Cannot add context lttng_add_vpid_to_ctx"); |
| 422 | goto error; |
| 423 | } |
| 424 | ret = lttng_add_procname_to_ctx(ctx); |
| 425 | if (ret) { |
| 426 | WARN("Cannot add context lttng_add_procname_to_ctx"); |
| 427 | goto error; |
| 428 | } |
| 429 | ret = lttng_add_cpu_id_to_ctx(ctx); |
| 430 | if (ret) { |
| 431 | WARN("Cannot add context lttng_add_cpu_id_to_ctx"); |
| 432 | goto error; |
| 433 | } |
| 434 | ret = lttng_add_cgroup_ns_to_ctx(ctx); |
| 435 | if (ret) { |
| 436 | WARN("Cannot add context lttng_add_cgroup_ns_to_ctx"); |
| 437 | goto error; |
| 438 | } |
| 439 | ret = lttng_add_ipc_ns_to_ctx(ctx); |
| 440 | if (ret) { |
| 441 | WARN("Cannot add context lttng_add_ipc_ns_to_ctx"); |
| 442 | goto error; |
| 443 | } |
| 444 | ret = lttng_add_mnt_ns_to_ctx(ctx); |
| 445 | if (ret) { |
| 446 | WARN("Cannot add context lttng_add_mnt_ns_to_ctx"); |
| 447 | goto error; |
| 448 | } |
| 449 | ret = lttng_add_net_ns_to_ctx(ctx); |
| 450 | if (ret) { |
| 451 | WARN("Cannot add context lttng_add_net_ns_to_ctx"); |
| 452 | goto error; |
| 453 | } |
| 454 | ret = lttng_add_pid_ns_to_ctx(ctx); |
| 455 | if (ret) { |
| 456 | WARN("Cannot add context lttng_add_pid_ns_to_ctx"); |
| 457 | goto error; |
| 458 | } |
| 459 | ret = lttng_add_time_ns_to_ctx(ctx); |
| 460 | if (ret) { |
| 461 | WARN("Cannot add context lttng_add_time_ns_to_ctx"); |
| 462 | goto error; |
| 463 | } |
| 464 | ret = lttng_add_user_ns_to_ctx(ctx); |
| 465 | if (ret) { |
| 466 | WARN("Cannot add context lttng_add_user_ns_to_ctx"); |
| 467 | goto error; |
| 468 | } |
| 469 | ret = lttng_add_uts_ns_to_ctx(ctx); |
| 470 | if (ret) { |
| 471 | WARN("Cannot add context lttng_add_uts_ns_to_ctx"); |
| 472 | goto error; |
| 473 | } |
| 474 | ret = lttng_add_vuid_to_ctx(ctx); |
| 475 | if (ret) { |
| 476 | WARN("Cannot add context lttng_add_vuid_to_ctx"); |
| 477 | goto error; |
| 478 | } |
| 479 | ret = lttng_add_veuid_to_ctx(ctx); |
| 480 | if (ret) { |
| 481 | WARN("Cannot add context lttng_add_veuid_to_ctx"); |
| 482 | goto error; |
| 483 | } |
| 484 | ret = lttng_add_vsuid_to_ctx(ctx); |
| 485 | if (ret) { |
| 486 | WARN("Cannot add context lttng_add_vsuid_to_ctx"); |
| 487 | goto error; |
| 488 | } |
| 489 | ret = lttng_add_vgid_to_ctx(ctx); |
| 490 | if (ret) { |
| 491 | WARN("Cannot add context lttng_add_vgid_to_ctx"); |
| 492 | goto error; |
| 493 | } |
| 494 | ret = lttng_add_vegid_to_ctx(ctx); |
| 495 | if (ret) { |
| 496 | WARN("Cannot add context lttng_add_vegid_to_ctx"); |
| 497 | goto error; |
| 498 | } |
| 499 | ret = lttng_add_vsgid_to_ctx(ctx); |
| 500 | if (ret) { |
| 501 | WARN("Cannot add context lttng_add_vsgid_to_ctx"); |
| 502 | goto error; |
| 503 | } |
| 504 | lttng_context_update(*ctx); |
| 505 | return 0; |
| 506 | |
| 507 | error: |
| 508 | lttng_destroy_context(*ctx); |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | /* For backward compatibility. Leave those exported symbols in place. */ |
| 513 | struct lttng_ctx *lttng_static_ctx; |
| 514 | |
| 515 | void lttng_context_init(void) |
| 516 | { |
| 517 | } |
| 518 | |
| 519 | void lttng_context_exit(void) |
| 520 | { |
| 521 | } |
| 522 | |
| 523 | int lttng_session_context_init(struct lttng_ctx **ctx) |
| 524 | { |
| 525 | return 0; |
| 526 | } |