Fix: add missing rcu read side lock
[lttng-tools.git] / src / bin / lttng-sessiond / context.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <urcu/list.h>
24
25 #include <common/error.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27
28 #include "context.h"
29 #include "kernel.h"
30 #include "ust-app.h"
31 #include "trace-ust.h"
32
33 /*
34 * Add kernel context to all channel.
35 */
36 static int add_kctx_all_channels(struct ltt_kernel_session *ksession,
37 struct lttng_kernel_context *kctx)
38 {
39 int ret;
40 struct ltt_kernel_channel *kchan;
41
42 DBG("Adding kernel context to all channels");
43
44 /* Go over all channels */
45 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
46 ret = kernel_add_channel_context(kchan, kctx);
47 if (ret < 0) {
48 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
49 goto error;
50 }
51 }
52
53 ret = LTTNG_OK;
54
55 error:
56 return ret;
57 }
58
59 /*
60 * Add kernel context to a specific channel.
61 */
62 static int add_kctx_to_channel(struct lttng_kernel_context *kctx,
63 struct ltt_kernel_channel *kchan)
64 {
65 int ret;
66
67 DBG("Add kernel context to channel '%s'", kchan->channel->name);
68
69 ret = kernel_add_channel_context(kchan, kctx);
70 if (ret < 0) {
71 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
72 goto error;
73 }
74
75 ret = LTTNG_OK;
76
77 error:
78 return ret;
79 }
80
81 /*
82 * Add UST context to channel.
83 */
84 static int add_uctx_to_channel(struct ltt_ust_session *usess, int domain,
85 struct ltt_ust_channel *uchan, struct lttng_event_context *ctx)
86 {
87 int ret;
88 struct ltt_ust_context *uctx;
89 struct lttng_ht_iter iter;
90 struct lttng_ht_node_ulong *uctx_node;
91
92 /* Create ltt UST context */
93 uctx = trace_ust_create_context(ctx);
94 if (uctx == NULL) {
95 ret = -EINVAL;
96 goto error;
97 }
98
99 switch (domain) {
100 case LTTNG_DOMAIN_UST:
101 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
102 if (ret < 0) {
103 goto error;
104 }
105 break;
106 default:
107 ret = -ENOSYS;
108 goto error;
109 }
110
111 rcu_read_lock();
112
113 /* Lookup context before adding it */
114 lttng_ht_lookup(uchan->ctx, (void *)((unsigned long)uctx->ctx.ctx), &iter);
115 uctx_node = lttng_ht_iter_get_node_ulong(&iter);
116 if (uctx_node != NULL) {
117 ret = -EEXIST;
118 rcu_read_unlock();
119 goto error;
120 }
121
122 /* Add ltt UST context node to ltt UST channel */
123 lttng_ht_add_unique_ulong(uchan->ctx, &uctx->node);
124 rcu_read_unlock();
125
126 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
127
128 return 0;
129
130 error:
131 free(uctx);
132 return ret;
133 }
134
135 /*
136 * Add kernel context to tracer.
137 */
138 int context_kernel_add(struct ltt_kernel_session *ksession,
139 struct lttng_event_context *ctx, char *channel_name)
140 {
141 int ret;
142 struct ltt_kernel_channel *kchan;
143 struct lttng_kernel_context kctx;
144
145 /* Setup kernel context structure */
146 switch (ctx->ctx) {
147 case LTTNG_EVENT_CONTEXT_PID:
148 kctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
149 break;
150 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
151 kctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_COUNTER;
152 break;
153 case LTTNG_EVENT_CONTEXT_PROCNAME:
154 kctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
155 break;
156 case LTTNG_EVENT_CONTEXT_PRIO:
157 kctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
158 break;
159 case LTTNG_EVENT_CONTEXT_NICE:
160 kctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
161 break;
162 case LTTNG_EVENT_CONTEXT_VPID:
163 kctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
164 break;
165 case LTTNG_EVENT_CONTEXT_TID:
166 kctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
167 break;
168 case LTTNG_EVENT_CONTEXT_VTID:
169 kctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
170 break;
171 case LTTNG_EVENT_CONTEXT_PPID:
172 kctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
173 break;
174 case LTTNG_EVENT_CONTEXT_VPPID:
175 kctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
176 break;
177 case LTTNG_EVENT_CONTEXT_HOSTNAME:
178 kctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
179 break;
180 default:
181 return LTTNG_ERR_KERN_CONTEXT_FAIL;
182 }
183
184 kctx.u.perf_counter.type = ctx->u.perf_counter.type;
185 kctx.u.perf_counter.config = ctx->u.perf_counter.config;
186 strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name,
187 LTTNG_SYMBOL_NAME_LEN);
188 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
189
190 if (strlen(channel_name) == 0) {
191 ret = add_kctx_all_channels(ksession, &kctx);
192 if (ret != LTTNG_OK) {
193 goto error;
194 }
195 } else {
196 /* Get kernel channel */
197 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
198 if (kchan == NULL) {
199 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
200 goto error;
201 }
202
203 ret = add_kctx_to_channel(&kctx, kchan);
204 if (ret != LTTNG_OK) {
205 goto error;
206 }
207 }
208
209 ret = LTTNG_OK;
210
211 error:
212 return ret;
213 }
214
215 /*
216 * Add UST context to tracer.
217 */
218 int context_ust_add(struct ltt_ust_session *usess, int domain,
219 struct lttng_event_context *ctx, char *channel_name)
220 {
221 int ret = LTTNG_OK;
222 struct lttng_ht_iter iter;
223 struct lttng_ht *chan_ht;
224 struct ltt_ust_channel *uchan = NULL;
225
226 /*
227 * Define which channel's hashtable to use from the domain or quit if
228 * unknown domain.
229 */
230 switch (domain) {
231 case LTTNG_DOMAIN_UST:
232 chan_ht = usess->domain_global.channels;
233 break;
234 #if 0
235 case LTTNG_DOMAIN_UST_EXEC_NAME:
236 case LTTNG_DOMAIN_UST_PID:
237 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
238 #endif
239 default:
240 ret = LTTNG_ERR_UND;
241 goto error;
242 }
243
244 /* Get UST channel if defined */
245 if (strlen(channel_name) != 0) {
246 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
247 if (uchan == NULL) {
248 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
249 goto error;
250 }
251 }
252
253 if (uchan) {
254 /* Add ctx to channel */
255 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
256 } else {
257 rcu_read_lock();
258 /* Add ctx all events, all channels */
259 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
260 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
261 if (ret < 0) {
262 ERR("Context failed for channel %s", uchan->name);
263 continue;
264 }
265 }
266 rcu_read_unlock();
267 }
268
269 switch (ret) {
270 case -EEXIST:
271 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
272 break;
273 case -ENOMEM:
274 ret = LTTNG_ERR_FATAL;
275 break;
276 case -EINVAL:
277 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
278 break;
279 case -ENOSYS:
280 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
281 break;
282 default:
283 ret = LTTNG_OK;
284 break;
285 }
286
287 error:
288 return ret;
289 }
This page took 0.034117 seconds and 4 git commands to generate.