d4203bed8c4b35246ad3b76cc30c852df9d0f86c
[lttng-tools.git] / 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 it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, 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 <lttng-sessiond-comm.h>
26 #include <lttngerr.h>
27
28 #include "context.h"
29 #include "hashtable.h"
30 #include "kernel.h"
31 #include "ust-app.h"
32 #include "trace-ust.h"
33
34 /*
35 * Add kernel context to an event of a specific channel.
36 */
37 static int add_kctx_to_event(struct lttng_kernel_context *kctx,
38 struct ltt_kernel_channel *kchan, char *event_name)
39 {
40 int ret, found = 0;
41 struct ltt_kernel_event *kevent;
42
43 DBG("Add kernel context to event %s", event_name);
44
45 kevent = trace_kernel_get_event_by_name(event_name, kchan);
46 if (kevent != NULL) {
47 ret = kernel_add_event_context(kevent, kctx);
48 if (ret < 0) {
49 goto error;
50 }
51 found = 1;
52 }
53
54 ret = found;
55
56 error:
57 return ret;
58 }
59
60 /*
61 * Add kernel context to all channel.
62 *
63 * If event_name is specified, add context to event instead.
64 */
65 static int add_kctx_all_channels(struct ltt_kernel_session *ksession,
66 struct lttng_kernel_context *kctx, char *event_name)
67 {
68 int ret, no_event = 0, found = 0;
69 struct ltt_kernel_channel *kchan;
70
71 if (strlen(event_name) == 0) {
72 no_event = 1;
73 }
74
75 DBG("Adding kernel context to all channels (event: %s)", event_name);
76
77 /* Go over all channels */
78 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
79 if (no_event) {
80 ret = kernel_add_channel_context(kchan, kctx);
81 if (ret < 0) {
82 ret = LTTCOMM_KERN_CONTEXT_FAIL;
83 goto error;
84 }
85 } else {
86 ret = add_kctx_to_event(kctx, kchan, event_name);
87 if (ret < 0) {
88 ret = LTTCOMM_KERN_CONTEXT_FAIL;
89 goto error;
90 } else if (ret == 1) {
91 /* Event found and context added */
92 found = 1;
93 break;
94 }
95 }
96 }
97
98 if (!found && !no_event) {
99 ret = LTTCOMM_NO_EVENT;
100 goto error;
101 }
102
103 ret = LTTCOMM_OK;
104
105 error:
106 return ret;
107 }
108
109 /*
110 * Add kernel context to a specific channel.
111 *
112 * If event_name is specified, add context to that event.
113 */
114 static int add_kctx_to_channel(struct lttng_kernel_context *kctx,
115 struct ltt_kernel_channel *kchan, char *event_name)
116 {
117 int ret, no_event = 0, found = 0;
118
119 if (strlen(event_name) == 0) {
120 no_event = 1;
121 }
122
123 DBG("Add kernel context to channel '%s', event '%s'",
124 kchan->channel->name, event_name);
125
126 if (no_event) {
127 ret = kernel_add_channel_context(kchan, kctx);
128 if (ret < 0) {
129 ret = LTTCOMM_KERN_CONTEXT_FAIL;
130 goto error;
131 }
132 } else {
133 ret = add_kctx_to_event(kctx, kchan, event_name);
134 if (ret < 0) {
135 ret = LTTCOMM_KERN_CONTEXT_FAIL;
136 goto error;
137 } else if (ret == 1) {
138 /* Event found and context added */
139 found = 1;
140 }
141 }
142
143 if (!found && !no_event) {
144 ret = LTTCOMM_NO_EVENT;
145 goto error;
146 }
147
148 ret = LTTCOMM_OK;
149
150 error:
151 return ret;
152 }
153
154 /*
155 * Add UST context to channel.
156 */
157 static int add_uctx_to_channel(struct ltt_ust_session *usess, int domain,
158 struct ltt_ust_channel *uchan, struct lttng_event_context *ctx)
159 {
160 int ret;
161 struct ltt_ust_context *uctx;
162
163 /* Create ltt UST context */
164 uctx = trace_ust_create_context(ctx);
165 if (uctx == NULL) {
166 ret = LTTCOMM_FATAL;
167 goto error;
168 }
169
170 switch (domain) {
171 case LTTNG_DOMAIN_UST:
172 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
173 if (ret < 0) {
174 goto error;
175 }
176 break;
177 default:
178 ret = LTTCOMM_NOT_IMPLEMENTED;
179 goto error;
180 }
181
182 /* Add ltt UST context node to ltt UST channel */
183 hashtable_add_unique(uchan->ctx, &uctx->node);
184
185 return LTTCOMM_OK;
186
187 error:
188 free(uctx);
189 return ret;
190 }
191
192 /*
193 * Add UST context to event.
194 */
195 static int add_uctx_to_event(struct ltt_ust_session *usess, int domain,
196 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
197 struct lttng_event_context *ctx)
198 {
199 int ret;
200 struct ltt_ust_context *uctx;
201
202 /* Create ltt UST context */
203 uctx = trace_ust_create_context(ctx);
204 if (uctx == NULL) {
205 ret = LTTCOMM_FATAL;
206 goto error;
207 }
208
209 switch (domain) {
210 case LTTNG_DOMAIN_UST:
211 ret = ust_app_add_ctx_event_glb(usess, uchan, uevent, uctx);
212 if (ret < 0) {
213 goto error;
214 }
215 break;
216 default:
217 ret = LTTCOMM_NOT_IMPLEMENTED;
218 goto error;
219 }
220
221 /* Add ltt UST context node to ltt UST event */
222 hashtable_add_unique(uevent->ctx, &uctx->node);
223
224 return LTTCOMM_OK;
225
226 error:
227 free(uctx);
228 return ret;
229 }
230
231 /*
232 * Add kernel context to tracer.
233 */
234 int context_kernel_add(struct ltt_kernel_session *ksession,
235 struct lttng_event_context *ctx, char *event_name,
236 char *channel_name)
237 {
238 int ret;
239 struct ltt_kernel_channel *kchan;
240 struct lttng_kernel_context kctx;
241
242 /* Setup kernel context structure */
243 kctx.ctx = ctx->ctx;
244 kctx.u.perf_counter.type = ctx->u.perf_counter.type;
245 kctx.u.perf_counter.config = ctx->u.perf_counter.config;
246 strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name,
247 LTTNG_SYMBOL_NAME_LEN);
248 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
249
250 if (strlen(channel_name) == 0) {
251 ret = add_kctx_all_channels(ksession, &kctx, event_name);
252 if (ret != LTTCOMM_OK) {
253 goto error;
254 }
255 } else {
256 /* Get kernel channel */
257 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
258 if (kchan == NULL) {
259 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
260 goto error;
261 }
262
263 ret = add_kctx_to_channel(&kctx, kchan, event_name);
264 if (ret != LTTCOMM_OK) {
265 goto error;
266 }
267 }
268
269 ret = LTTCOMM_OK;
270
271 error:
272 return ret;
273 }
274
275 /*
276 * Add UST context to tracer.
277 */
278 int context_ust_add(struct ltt_ust_session *usess, int domain,
279 struct lttng_event_context *ctx, char *event_name,
280 char *channel_name)
281 {
282 int ret = LTTCOMM_OK, have_event = 0;
283 struct cds_lfht_iter iter;
284 struct cds_lfht *chan_ht;
285 struct ltt_ust_channel *uchan = NULL;
286 struct ltt_ust_event *uevent = NULL;
287
288 /*
289 * Define which channel's hashtable to use from the domain or quit if
290 * unknown domain.
291 */
292 switch (domain) {
293 case LTTNG_DOMAIN_UST:
294 chan_ht = usess->domain_global.channels;
295 break;
296 case LTTNG_DOMAIN_UST_EXEC_NAME:
297 case LTTNG_DOMAIN_UST_PID:
298 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
299 default:
300 ret = LTTCOMM_NOT_IMPLEMENTED;
301 goto error;
302 }
303
304 /* Do we have an event name */
305 if (strlen(event_name) != 0) {
306 have_event = 1;
307 }
308
309 /* Get UST channel if defined */
310 if (strlen(channel_name) != 0) {
311 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
312 if (uchan == NULL) {
313 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
314 goto error;
315 }
316 }
317
318 /* If UST channel specified and event name, get UST event ref */
319 if (uchan && have_event) {
320 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
321 if (uevent == NULL) {
322 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
323 goto error;
324 }
325 }
326
327 /* At this point, we have 4 possibilities */
328
329 if (uchan && uevent) { /* Add ctx to event in channel */
330 ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
331 } else if (uchan && !have_event) { /* Add ctx to channel */
332 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
333 } else if (!uchan && have_event) { /* Add ctx to event */
334 /* Add context to event without having the channel name */
335 cds_lfht_for_each_entry(chan_ht, &iter, uchan, node) {
336 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
337 if (uevent != NULL) {
338 ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
339 /*
340 * LTTng UST does not allowed the same event to be registered
341 * multiple time in different or the same channel. So, if we
342 * found our event, we stop.
343 */
344 goto end;
345 }
346 }
347 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
348 goto error;
349 } else if (!uchan && !have_event) { /* Add ctx all events, all channels */
350 /* For all channels */
351 cds_lfht_for_each_entry(chan_ht, &iter, uchan, node) {
352 struct cds_lfht_iter uiter;
353
354 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
355 if (ret < 0) {
356 ERR("Context added to channel %s failed", uchan->name);
357 continue;
358 }
359
360 /* For all events in channel */
361 cds_lfht_for_each_entry(uchan->events, &uiter, uevent, node) {
362 ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
363 if (ret < 0) {
364 ERR("Context add to event %s in channel %s failed",
365 uevent->attr.name, uchan->name);
366 continue;
367 }
368 }
369 }
370 }
371
372 end:
373 switch (ret) {
374 case -EEXIST:
375 ret = LTTCOMM_UST_CONTEXT_EXIST;
376 goto error;
377 case -ENOMEM:
378 ret = LTTCOMM_FATAL;
379 goto error;
380 }
381
382 return LTTCOMM_OK;
383
384 error:
385 return ret;
386 }
This page took 0.035731 seconds and 3 git commands to generate.