Implement per-context filtering
[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>
35897f8b 26#include <helper.h>
8d8a24c8 27#include <string.h>
8173ec7c
MD
28#include <assert.h>
29
77aa5901
MD
30/*
31 * The filter implementation requires that two consecutive "get" for the
32 * same context performed by the same thread return the same result.
33 */
34
8173ec7c
MD
35int lttng_find_context(struct lttng_ctx *ctx, const char *name)
36{
37 unsigned int i;
38
39 for (i = 0; i < ctx->nr_fields; i++) {
40 /* Skip allocated (but non-initialized) contexts */
41 if (!ctx->fields[i].event_field.name)
42 continue;
43 if (!strcmp(ctx->fields[i].event_field.name, name))
44 return 1;
45 }
46 return 0;
47}
8020ceb5 48
77aa5901
MD
49int lttng_get_context_index(struct lttng_ctx *ctx, const char *name)
50{
51 unsigned int i;
52
53 if (!ctx)
54 return -1;
55 for (i = 0; i < ctx->nr_fields; i++) {
56 /* Skip allocated (but non-initialized) contexts */
57 if (!ctx->fields[i].event_field.name)
58 continue;
59 if (!strcmp(ctx->fields[i].event_field.name, name))
60 return i;
61 }
62 return -1;
63}
64
a0a748b8
MD
65/*
66 * Note: as we append context information, the pointer location may change.
67 */
8020ceb5
MD
68struct lttng_ctx_field *lttng_append_context(struct lttng_ctx **ctx_p)
69{
70 struct lttng_ctx_field *field;
71 struct lttng_ctx *ctx;
72
73 if (!*ctx_p) {
8d8a24c8 74 *ctx_p = zmalloc(sizeof(struct lttng_ctx));
8020ceb5
MD
75 if (!*ctx_p)
76 return NULL;
77 }
78 ctx = *ctx_p;
79 if (ctx->nr_fields + 1 > ctx->allocated_fields) {
80 struct lttng_ctx_field *new_fields;
81
82 ctx->allocated_fields = max_t(size_t, 1, 2 * ctx->allocated_fields);
8d8a24c8 83 new_fields = zmalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field));
8020ceb5
MD
84 if (!new_fields)
85 return NULL;
86 if (ctx->fields)
87 memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields);
8d8a24c8 88 free(ctx->fields);
8020ceb5
MD
89 ctx->fields = new_fields;
90 }
91 field = &ctx->fields[ctx->nr_fields];
92 ctx->nr_fields++;
93 return field;
94}
8020ceb5 95
b13d13b1
MD
96/*
97 * Remove last context field.
98 */
8020ceb5
MD
99void lttng_remove_context_field(struct lttng_ctx **ctx_p,
100 struct lttng_ctx_field *field)
101{
102 struct lttng_ctx *ctx;
103
104 ctx = *ctx_p;
105 ctx->nr_fields--;
8173ec7c 106 assert(&ctx->fields[ctx->nr_fields] == field);
8020ceb5
MD
107 memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field));
108}
8020ceb5
MD
109
110void lttng_destroy_context(struct lttng_ctx *ctx)
111{
112 int i;
113
114 if (!ctx)
115 return;
116 for (i = 0; i < ctx->nr_fields; i++) {
117 if (ctx->fields[i].destroy)
118 ctx->fields[i].destroy(&ctx->fields[i]);
119 }
8d8a24c8
MD
120 free(ctx->fields);
121 free(ctx);
8020ceb5 122}
This page took 0.030323 seconds and 4 git commands to generate.