| 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 | |
| 24 | #include <lttng/ust-events.h> |
| 25 | #include <lttng/ust-tracer.h> |
| 26 | #include <helper.h> |
| 27 | #include <string.h> |
| 28 | #include <assert.h> |
| 29 | |
| 30 | int lttng_find_context(struct lttng_ctx *ctx, const char *name) |
| 31 | { |
| 32 | unsigned int i; |
| 33 | |
| 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 | |
| 44 | /* |
| 45 | * Note: as we append context information, the pointer location may change. |
| 46 | */ |
| 47 | struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p) |
| 48 | { |
| 49 | struct lttng_ctx_field *field; |
| 50 | struct lttng_ctx *ctx; |
| 51 | |
| 52 | if (!*ctx_p) { |
| 53 | *ctx_p = zmalloc(sizeof(struct lttng_ctx)); |
| 54 | if (!*ctx_p) |
| 55 | return NULL; |
| 56 | } |
| 57 | ctx = *ctx_p; |
| 58 | if (ctx->nr_fields + 1 > ctx->allocated_fields) { |
| 59 | struct lttng_ctx_field *new_fields; |
| 60 | |
| 61 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); |
| 62 | new_fields = zmalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field)); |
| 63 | if (!new_fields) |
| 64 | return NULL; |
| 65 | if (ctx->fields) |
| 66 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); |
| 67 | free(ctx->fields); |
| 68 | ctx->fields = new_fields; |
| 69 | } |
| 70 | field = &ctx->fields[ctx->nr_fields]; |
| 71 | ctx->nr_fields++; |
| 72 | return field; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Remove last context field. |
| 77 | */ |
| 78 | void lttng_remove_context_field(struct lttng_ctx **ctx_p, |
| 79 | struct lttng_ctx_field *field) |
| 80 | { |
| 81 | struct lttng_ctx *ctx; |
| 82 | |
| 83 | ctx = *ctx_p; |
| 84 | ctx->nr_fields--; |
| 85 | assert(&ctx->fields[ctx->nr_fields] == field); |
| 86 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); |
| 87 | } |
| 88 | |
| 89 | void lttng_destroy_context(struct lttng_ctx *ctx) |
| 90 | { |
| 91 | int i; |
| 92 | |
| 93 | if (!ctx) |
| 94 | return; |
| 95 | for (i = 0; i < ctx->nr_fields; i++) { |
| 96 | if (ctx->fields[i].destroy) |
| 97 | ctx->fields[i].destroy(&ctx->fields[i]); |
| 98 | } |
| 99 | free(ctx->fields); |
| 100 | free(ctx); |
| 101 | } |