c580891a1ac93471f544fce2ddb66c31ffa622b2
[lttng-ust.git] / liblttng-ust / lttng-context-provider.c
1 /*
2 * lttng-context-provider.c
3 *
4 * LTTng UST application context provider.
5 *
6 * Copyright (C) 2016 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 #define _LGPL_SOURCE
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <lttng/ust-context-provider.h>
27 #include "lttng-tracer-core.h"
28 #include "jhash.h"
29 #include <helper.h>
30
31 #define CONTEXT_PROVIDER_HT_BITS 12
32 #define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS)
33 struct context_provider_ht {
34 struct cds_hlist_head table[CONTEXT_PROVIDER_HT_SIZE];
35 };
36
37 static struct context_provider_ht context_provider_ht;
38
39 static struct lttng_ust_context_provider *
40 lookup_provider_by_name(const char *name)
41 {
42 struct cds_hlist_head *head;
43 struct cds_hlist_node *node;
44 struct lttng_ust_context_provider *provider;
45 uint32_t hash;
46 const char *end;
47 size_t len;
48
49 /* Lookup using everything before first ':' as key. */
50 end = strchr(name, ':');
51 if (end)
52 len = end - name;
53 else
54 len = strlen(name);
55 hash = jhash(name, len, 0);
56 head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
57 cds_hlist_for_each_entry(provider, node, head, node) {
58 if (!strncmp(provider->name, name, len))
59 return provider;
60 }
61 return NULL;
62 }
63
64 int lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider)
65 {
66 struct cds_hlist_head *head;
67 size_t name_len = strlen(provider->name);
68 uint32_t hash;
69 int ret = 0;
70
71 /* Provider name starts with "$app.". */
72 if (strncmp("$app.", provider->name, strlen("$app.") != 0))
73 return -EINVAL;
74 /* Provider name cannot contain a column character. */
75 if (strchr(provider->name, ':'))
76 return -EINVAL;
77 if (ust_lock()) {
78 ret = -EBUSY;
79 goto end;
80 }
81 if (lookup_provider_by_name(provider->name)) {
82 ret = -EBUSY;
83 goto end;
84 }
85 hash = jhash(provider->name, name_len, 0);
86 head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
87 cds_hlist_add_head(&provider->node, head);
88 lttng_ust_context_set_session_provider(provider->name,
89 provider->get_size, provider->record,
90 provider->get_value);
91 end:
92 ust_unlock();
93 return ret;
94 }
95
96 void lttng_ust_context_provider_unregister(struct lttng_ust_context_provider *provider)
97 {
98 if (ust_lock())
99 goto end;
100 lttng_ust_context_set_session_provider(provider->name,
101 lttng_ust_dummy_get_size, lttng_ust_dummy_record,
102 lttng_ust_dummy_get_value);
103 cds_hlist_del(&provider->node);
104 end:
105 ust_unlock();
106 }
107
108 /*
109 * Called with ust mutex held.
110 * Add application context to array of context, even if the application
111 * context is not currently loaded by application. It will then use the
112 * dummy callbacks in that case.
113 * Always performed before tracing is started, since it modifies
114 * metadata describing the context.
115 */
116 int lttng_ust_add_app_context_to_ctx_rcu(const char *name,
117 struct lttng_ctx **ctx)
118 {
119 struct lttng_ust_context_provider *provider;
120 struct lttng_ctx_field new_field;
121 int ret;
122
123 if (*ctx && lttng_find_context(*ctx, name))
124 return -EEXIST;
125 /*
126 * For application context, add it by expanding
127 * ctx array.
128 */
129 memset(&new_field, 0, sizeof(new_field));
130 new_field.field_name = strdup(name);
131 if (!new_field.field_name)
132 return -ENOMEM;
133 new_field.event_field.name = new_field.field_name;
134 new_field.event_field.type.atype = atype_dynamic;
135 /*
136 * If provider is not found, we add the context anyway, but
137 * it will provide a dummy context.
138 */
139 provider = lookup_provider_by_name(name);
140 if (provider) {
141 new_field.get_size = provider->get_size;
142 new_field.record = provider->record;
143 new_field.get_value = provider->get_value;
144 } else {
145 new_field.get_size = lttng_ust_dummy_get_size;
146 new_field.record = lttng_ust_dummy_record;
147 new_field.get_value = lttng_ust_dummy_get_value;
148 }
149 ret = lttng_context_add_rcu(ctx, &new_field);
150 if (ret) {
151 free(new_field.field_name);
152 return ret;
153 }
154 return 0;
155 }
This page took 0.035487 seconds and 3 git commands to generate.