Fix: consumer add channel return value was positive on error
[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 assert(ksession);
43 assert(kctx);
44
45 DBG("Adding kernel context to all channels");
46
47 /* Go over all channels */
48 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
49 ret = kernel_add_channel_context(kchan, kctx);
50 if (ret < 0) {
51 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
52 goto error;
53 }
54 }
55
56 ret = LTTNG_OK;
57
58 error:
59 return ret;
60 }
61
62 /*
63 * Add kernel context to a specific channel.
64 */
65 static int add_kctx_to_channel(struct lttng_kernel_context *kctx,
66 struct ltt_kernel_channel *kchan)
67 {
68 int ret;
69
70 assert(kchan);
71 assert(kctx);
72
73 DBG("Add kernel context to channel '%s'", kchan->channel->name);
74
75 ret = kernel_add_channel_context(kchan, kctx);
76 if (ret < 0) {
77 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
78 goto error;
79 }
80
81 ret = LTTNG_OK;
82
83 error:
84 return ret;
85 }
86
87 /*
88 * Add UST context to channel.
89 */
90 static int add_uctx_to_channel(struct ltt_ust_session *usess, int domain,
91 struct ltt_ust_channel *uchan, struct lttng_event_context *ctx)
92 {
93 int ret;
94 struct ltt_ust_context *uctx;
95 struct lttng_ht_iter iter;
96 struct lttng_ht_node_ulong *uctx_node;
97
98 assert(usess);
99 assert(uchan);
100 assert(ctx);
101
102 /* Create ltt UST context */
103 uctx = trace_ust_create_context(ctx);
104 if (uctx == NULL) {
105 ret = -EINVAL;
106 goto error;
107 }
108
109 switch (domain) {
110 case LTTNG_DOMAIN_UST:
111 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
112 if (ret < 0) {
113 goto error;
114 }
115 break;
116 default:
117 ret = -ENOSYS;
118 goto error;
119 }
120
121 rcu_read_lock();
122
123 /* Lookup context before adding it */
124 lttng_ht_lookup(uchan->ctx, (void *)((unsigned long)uctx->ctx.ctx), &iter);
125 uctx_node = lttng_ht_iter_get_node_ulong(&iter);
126 if (uctx_node != NULL) {
127 ret = -EEXIST;
128 rcu_read_unlock();
129 goto error;
130 }
131
132 /* Add ltt UST context node to ltt UST channel */
133 lttng_ht_add_unique_ulong(uchan->ctx, &uctx->node);
134 rcu_read_unlock();
135
136 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
137
138 return 0;
139
140 error:
141 free(uctx);
142 return ret;
143 }
144
145 /*
146 * Add kernel context to tracer.
147 */
148 int context_kernel_add(struct ltt_kernel_session *ksession,
149 struct lttng_event_context *ctx, char *channel_name)
150 {
151 int ret;
152 struct ltt_kernel_channel *kchan;
153 struct lttng_kernel_context kctx;
154
155 assert(ksession);
156 assert(ctx);
157 assert(channel_name);
158
159 /* Setup kernel context structure */
160 switch (ctx->ctx) {
161 case LTTNG_EVENT_CONTEXT_PID:
162 kctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
163 break;
164 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
165 kctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_COUNTER;
166 break;
167 case LTTNG_EVENT_CONTEXT_PROCNAME:
168 kctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
169 break;
170 case LTTNG_EVENT_CONTEXT_PRIO:
171 kctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
172 break;
173 case LTTNG_EVENT_CONTEXT_NICE:
174 kctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
175 break;
176 case LTTNG_EVENT_CONTEXT_VPID:
177 kctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
178 break;
179 case LTTNG_EVENT_CONTEXT_TID:
180 kctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
181 break;
182 case LTTNG_EVENT_CONTEXT_VTID:
183 kctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
184 break;
185 case LTTNG_EVENT_CONTEXT_PPID:
186 kctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
187 break;
188 case LTTNG_EVENT_CONTEXT_VPPID:
189 kctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
190 break;
191 case LTTNG_EVENT_CONTEXT_HOSTNAME:
192 kctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
193 break;
194 default:
195 return LTTNG_ERR_KERN_CONTEXT_FAIL;
196 }
197
198 kctx.u.perf_counter.type = ctx->u.perf_counter.type;
199 kctx.u.perf_counter.config = ctx->u.perf_counter.config;
200 strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name,
201 LTTNG_SYMBOL_NAME_LEN);
202 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
203
204 if (*channel_name == '\0') {
205 ret = add_kctx_all_channels(ksession, &kctx);
206 if (ret != LTTNG_OK) {
207 goto error;
208 }
209 } else {
210 /* Get kernel channel */
211 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
212 if (kchan == NULL) {
213 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
214 goto error;
215 }
216
217 ret = add_kctx_to_channel(&kctx, kchan);
218 if (ret != LTTNG_OK) {
219 goto error;
220 }
221 }
222
223 ret = LTTNG_OK;
224
225 error:
226 return ret;
227 }
228
229 /*
230 * Add UST context to tracer.
231 */
232 int context_ust_add(struct ltt_ust_session *usess, int domain,
233 struct lttng_event_context *ctx, char *channel_name)
234 {
235 int ret = LTTNG_OK;
236 struct lttng_ht_iter iter;
237 struct lttng_ht *chan_ht;
238 struct ltt_ust_channel *uchan = NULL;
239
240 assert(usess);
241 assert(ctx);
242 assert(channel_name);
243
244 rcu_read_lock();
245
246 /*
247 * Define which channel's hashtable to use from the domain or quit if
248 * unknown domain.
249 */
250 switch (domain) {
251 case LTTNG_DOMAIN_UST:
252 chan_ht = usess->domain_global.channels;
253 break;
254 #if 0
255 case LTTNG_DOMAIN_UST_EXEC_NAME:
256 case LTTNG_DOMAIN_UST_PID:
257 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
258 #endif
259 default:
260 ret = LTTNG_ERR_UND;
261 goto error;
262 }
263
264 /* Get UST channel if defined */
265 if (channel_name[0] != '\0') {
266 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
267 if (uchan == NULL) {
268 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
269 goto error;
270 }
271 }
272
273 if (uchan) {
274 /* Add ctx to channel */
275 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
276 } else {
277 rcu_read_lock();
278 /* Add ctx all events, all channels */
279 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
280 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
281 if (ret < 0) {
282 ERR("Context failed for channel %s", uchan->name);
283 continue;
284 }
285 }
286 rcu_read_unlock();
287 }
288
289 switch (ret) {
290 case -EEXIST:
291 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
292 break;
293 case -ENOMEM:
294 ret = LTTNG_ERR_FATAL;
295 break;
296 case -EINVAL:
297 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
298 break;
299 case -ENOSYS:
300 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
301 break;
302 default:
303 ret = LTTNG_OK;
304 break;
305 }
306
307 error:
308 rcu_read_unlock();
309 return ret;
310 }
This page took 0.035965 seconds and 4 git commands to generate.