Rename public structure to avoid collisions
[lttng-ust.git] / liblttng-ust / lttng-context.c
CommitLineData
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>
a0a3bef9 26#include <usterr-signal-safe.h>
35897f8b 27#include <helper.h>
8d8a24c8 28#include <string.h>
8173ec7c
MD
29#include <assert.h>
30
77aa5901
MD
31/*
32 * The filter implementation requires that two consecutive "get" for the
33 * same context performed by the same thread return the same result.
34 */
35
a0a3bef9
MD
36/*
37 * Static array of contexts, for $ctx filters.
38 */
39struct lttng_ctx *lttng_static_ctx;
40
8173ec7c
MD
41int lttng_find_context(struct lttng_ctx *ctx, const char *name)
42{
43 unsigned int i;
44
45 for (i = 0; i < ctx->nr_fields; i++) {
46 /* Skip allocated (but non-initialized) contexts */
47 if (!ctx->fields[i].event_field.name)
48 continue;
49 if (!strcmp(ctx->fields[i].event_field.name, name))
50 return 1;
51 }
52 return 0;
53}
8020ceb5 54
77aa5901
MD
55int lttng_get_context_index(struct lttng_ctx *ctx, const char *name)
56{
57 unsigned int i;
58
59 if (!ctx)
60 return -1;
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, name))
66 return i;
67 }
68 return -1;
69}
70
a0a748b8
MD
71/*
72 * Note: as we append context information, the pointer location may change.
73 */
8020ceb5
MD
74struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p)
75{
76 struct lttng_ctx_field *field;
77 struct lttng_ctx *ctx;
78
79 if (!*ctx_p) {
8d8a24c8 80 *ctx_p = zmalloc(sizeof(struct lttng_ctx));
8020ceb5
MD
81 if (!*ctx_p)
82 return NULL;
83 }
84 ctx = *ctx_p;
85 if (ctx->nr_fields + 1 > ctx->allocated_fields) {
86 struct lttng_ctx_field *new_fields;
87
88 ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields);
8d8a24c8 89 new_fields = zmalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field));
8020ceb5
MD
90 if (!new_fields)
91 return NULL;
92 if (ctx->fields)
93 memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields);
8d8a24c8 94 free(ctx->fields);
8020ceb5
MD
95 ctx->fields = new_fields;
96 }
97 field = &ctx->fields[ctx->nr_fields];
98 ctx->nr_fields++;
99 return field;
100}
8020ceb5 101
b13d13b1
MD
102/*
103 * Remove last context field.
104 */
8020ceb5
MD
105void lttng_remove_context_field(struct lttng_ctx **ctx_p,
106 struct lttng_ctx_field *field)
107{
108 struct lttng_ctx *ctx;
109
110 ctx = *ctx_p;
111 ctx->nr_fields--;
8173ec7c 112 assert(&ctx->fields[ctx->nr_fields] == field);
8020ceb5
MD
113 memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field));
114}
8020ceb5
MD
115
116void lttng_destroy_context(struct lttng_ctx *ctx)
117{
118 int i;
119
120 if (!ctx)
121 return;
122 for (i = 0; i < ctx->nr_fields; i++) {
123 if (ctx->fields[i].destroy)
124 ctx->fields[i].destroy(&ctx->fields[i]);
125 }
8d8a24c8
MD
126 free(ctx->fields);
127 free(ctx);
8020ceb5 128}
a0a3bef9
MD
129
130void lttng_context_init(void)
131{
132 int ret;
133
134 ret = lttng_add_pthread_id_to_ctx(&lttng_static_ctx);
135 if (ret) {
136 WARN("Cannot add context lttng_add_pthread_id_to_ctx");
137 }
138 ret = lttng_add_vtid_to_ctx(&lttng_static_ctx);
139 if (ret) {
140 WARN("Cannot add context lttng_add_vtid_to_ctx");
141 }
142 ret = lttng_add_vpid_to_ctx(&lttng_static_ctx);
143 if (ret) {
144 WARN("Cannot add context lttng_add_vpid_to_ctx");
145 }
146 ret = lttng_add_procname_to_ctx(&lttng_static_ctx);
147 if (ret) {
148 WARN("Cannot add context lttng_add_procname_to_ctx");
149 }
150}
151
152void lttng_context_exit(void)
153{
154 lttng_destroy_context(lttng_static_ctx);
155 lttng_static_ctx = NULL;
156}
This page took 0.039749 seconds and 4 git commands to generate.