| 1 | /* |
| 2 | * lttng-context.c |
| 3 | * |
| 4 | * LTTng trace/channel/event context management. |
| 5 | * |
| 6 | * Copyright (C) 2011-2012 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 | #include <linux/module.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
| 28 | #include "lttng-events.h" |
| 29 | #include "lttng-tracer.h" |
| 30 | |
| 31 | int lttng_find_context(struct lttng_ctx *ctx, const char *name) |
| 32 | { |
| 33 | unsigned int i; |
| 34 | |
| 35 | for (i = 0; i < ctx->nr_fields; i++) { |
| 36 | /* Skip allocated (but non-initialized) contexts */ |
| 37 | if (!ctx->fields[i].event_field.name) |
| 38 | continue; |
| 39 | if (!strcmp(ctx->fields[i].event_field.name, name)) |
| 40 | return 1; |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | EXPORT_SYMBOL_GPL(lttng_find_context); |
| 45 | |
| 46 | /* |
| 47 | * Note: as we append context information, the pointer location may change. |
| 48 | */ |
| 49 | struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p) |
| 50 | { |
| 51 | struct lttng_ctx_field *field; |
| 52 | struct lttng_ctx *ctx; |
| 53 | |
| 54 | if (!*ctx_p) { |
| 55 | *ctx_p = kzalloc(sizeof(struct lttng_ctx), GFP_KERNEL); |
| 56 | if (!*ctx_p) |
| 57 | return NULL; |
| 58 | } |
| 59 | ctx = *ctx_p; |
| 60 | if (ctx->nr_fields + 1 > ctx->allocated_fields) { |
| 61 | struct lttng_ctx_field *new_fields; |
| 62 | |
| 63 | ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields); |
| 64 | new_fields = kzalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field), GFP_KERNEL); |
| 65 | if (!new_fields) |
| 66 | return NULL; |
| 67 | if (ctx->fields) |
| 68 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); |
| 69 | kfree(ctx->fields); |
| 70 | ctx->fields = new_fields; |
| 71 | } |
| 72 | field = &ctx->fields[ctx->nr_fields]; |
| 73 | ctx->nr_fields++; |
| 74 | return field; |
| 75 | } |
| 76 | EXPORT_SYMBOL_GPL(lttng_append_context); |
| 77 | |
| 78 | /* |
| 79 | * Remove last context field. |
| 80 | */ |
| 81 | void lttng_remove_context_field(struct lttng_ctx **ctx_p, |
| 82 | struct lttng_ctx_field *field) |
| 83 | { |
| 84 | struct lttng_ctx *ctx; |
| 85 | |
| 86 | ctx = *ctx_p; |
| 87 | ctx->nr_fields--; |
| 88 | WARN_ON_ONCE(&ctx->fields[ctx->nr_fields] != field); |
| 89 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(lttng_remove_context_field); |
| 92 | |
| 93 | void lttng_destroy_context(struct lttng_ctx *ctx) |
| 94 | { |
| 95 | int i; |
| 96 | |
| 97 | if (!ctx) |
| 98 | return; |
| 99 | for (i = 0; i < ctx->nr_fields; i++) { |
| 100 | if (ctx->fields[i].destroy) |
| 101 | ctx->fields[i].destroy(&ctx->fields[i]); |
| 102 | } |
| 103 | kfree(ctx->fields); |
| 104 | kfree(ctx); |
| 105 | } |