Commit | Line | Data |
---|---|---|
8020ceb5 | 1 | /* |
e92f3e28 | 2 | * lttng-context.c |
8020ceb5 | 3 | * |
8173ec7c | 4 | * LTTng UST trace/channel/event context management. |
8020ceb5 | 5 | * |
e92f3e28 MD |
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 | |
8020ceb5 MD |
21 | */ |
22 | ||
e92f3e28 | 23 | |
4318ae1b MD |
24 | #include <lttng/ust-events.h> |
25 | #include <lttng/ust-tracer.h> | |
35897f8b | 26 | #include <helper.h> |
8d8a24c8 | 27 | #include <string.h> |
8173ec7c MD |
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 | } | |
8020ceb5 | 43 | |
a0a748b8 MD |
44 | /* |
45 | * Note: as we append context information, the pointer location may change. | |
46 | */ | |
8020ceb5 MD |
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) { | |
8d8a24c8 | 53 | *ctx_p = zmalloc(sizeof(struct lttng_ctx)); |
8020ceb5 MD |
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); | |
8d8a24c8 | 62 | new_fields = zmalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field)); |
8020ceb5 MD |
63 | if (!new_fields) |
64 | return NULL; | |
65 | if (ctx->fields) | |
66 | memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields); | |
8d8a24c8 | 67 | free(ctx->fields); |
8020ceb5 MD |
68 | ctx->fields = new_fields; |
69 | } | |
70 | field = &ctx->fields[ctx->nr_fields]; | |
71 | ctx->nr_fields++; | |
72 | return field; | |
73 | } | |
8020ceb5 | 74 | |
b13d13b1 MD |
75 | /* |
76 | * Remove last context field. | |
77 | */ | |
8020ceb5 MD |
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--; | |
8173ec7c | 85 | assert(&ctx->fields[ctx->nr_fields] == field); |
8020ceb5 MD |
86 | memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field)); |
87 | } | |
8020ceb5 MD |
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 | } | |
8d8a24c8 MD |
99 | free(ctx->fields); |
100 | free(ctx); | |
8020ceb5 | 101 | } |