Implement per-context filtering
[lttng-ust.git] / liblttng-ust / lttng-context.c
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 /*
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
35 int 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 }
48
49 int 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
65 /*
66 * Note: as we append context information, the pointer location may change.
67 */
68 struct 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) {
74 *ctx_p = zmalloc(sizeof(struct lttng_ctx));
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);
83 new_fields = zmalloc(ctx->allocated_fields * sizeof(struct lttng_ctx_field));
84 if (!new_fields)
85 return NULL;
86 if (ctx->fields)
87 memcpy(new_fields, ctx->fields, sizeof(*ctx->fields) * ctx->nr_fields);
88 free(ctx->fields);
89 ctx->fields = new_fields;
90 }
91 field = &ctx->fields[ctx->nr_fields];
92 ctx->nr_fields++;
93 return field;
94 }
95
96 /*
97 * Remove last context field.
98 */
99 void 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--;
106 assert(&ctx->fields[ctx->nr_fields] == field);
107 memset(&ctx->fields[ctx->nr_fields], 0, sizeof(struct lttng_ctx_field));
108 }
109
110 void 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 }
120 free(ctx->fields);
121 free(ctx);
122 }
This page took 0.031914 seconds and 5 git commands to generate.