Support for UST context
[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
33 /*
34 * Add kernel context to an event of a specific channel.
35 */
36 static int add_kctx_to_event(struct lttng_kernel_context *kctx,
37 struct ltt_kernel_channel *kchan, char *event_name)
38 {
39 int ret, found = 0;
40 struct ltt_kernel_event *kevent;
41
42 DBG("Add kernel context to event %s", event_name);
43
44 kevent = trace_kernel_get_event_by_name(event_name, kchan);
45 if (kevent != NULL) {
46 ret = kernel_add_event_context(kevent, kctx);
47 if (ret < 0) {
48 goto error;
49 }
50 found = 1;
51 }
52
53 ret = found;
54
55 error:
56 return ret;
57 }
58
59 /*
60 * Add kernel context to all channel.
61 *
62 * If event_name is specified, add context to event instead.
63 */
64 static int add_kctx_all_channels(struct ltt_kernel_session *ksession,
65 struct lttng_kernel_context *kctx, char *event_name)
66 {
67 int ret, no_event = 0, found = 0;
68 struct ltt_kernel_channel *kchan;
69
70 if (strlen(event_name) == 0) {
71 no_event = 1;
72 }
73
74 DBG("Adding kernel context to all channels (event: %s)", event_name);
75
76 /* Go over all channels */
77 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
78 if (no_event) {
79 ret = kernel_add_channel_context(kchan, kctx);
80 if (ret < 0) {
81 ret = LTTCOMM_KERN_CONTEXT_FAIL;
82 goto error;
83 }
84 } else {
85 ret = add_kctx_to_event(kctx, kchan, event_name);
86 if (ret < 0) {
87 ret = LTTCOMM_KERN_CONTEXT_FAIL;
88 goto error;
89 } else if (ret == 1) {
90 /* Event found and context added */
91 found = 1;
92 break;
93 }
94 }
95 }
96
97 if (!found && !no_event) {
98 ret = LTTCOMM_NO_EVENT;
99 goto error;
100 }
101
102 ret = LTTCOMM_OK;
103
104 error:
105 return ret;
106 }
107
108 /*
109 * Add kernel context to a specific channel.
110 *
111 * If event_name is specified, add context to that event.
112 */
113 static int add_kctx_to_channel(struct lttng_kernel_context *kctx,
114 struct ltt_kernel_channel *kchan, char *event_name)
115 {
116 int ret, no_event = 0, found = 0;
117
118 if (strlen(event_name) == 0) {
119 no_event = 1;
120 }
121
122 DBG("Add kernel context to channel '%s', event '%s'",
123 kchan->channel->name, event_name);
124
125 if (no_event) {
126 ret = kernel_add_channel_context(kchan, kctx);
127 if (ret < 0) {
128 ret = LTTCOMM_KERN_CONTEXT_FAIL;
129 goto error;
130 }
131 } else {
132 ret = add_kctx_to_event(kctx, kchan, event_name);
133 if (ret < 0) {
134 ret = LTTCOMM_KERN_CONTEXT_FAIL;
135 goto error;
136 } else if (ret == 1) {
137 /* Event found and context added */
138 found = 1;
139 }
140 }
141
142 if (!found && !no_event) {
143 ret = LTTCOMM_NO_EVENT;
144 goto error;
145 }
146
147 ret = LTTCOMM_OK;
148
149 error:
150 return ret;
151 }
152
153 /*
154 * Add kernel context to tracer.
155 */
156 int context_kernel_add(struct ltt_kernel_session *ksession,
157 struct lttng_event_context *ctx, char *event_name,
158 char *channel_name)
159 {
160 int ret;
161 struct ltt_kernel_channel *kchan;
162 struct lttng_kernel_context kctx;
163
164 /* Setup kernel context structure */
165 kctx.ctx = ctx->ctx;
166 kctx.u.perf_counter.type = ctx->u.perf_counter.type;
167 kctx.u.perf_counter.config = ctx->u.perf_counter.config;
168 strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name,
169 LTTNG_SYMBOL_NAME_LEN);
170 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
171
172 if (strlen(channel_name) == 0) {
173 ret = add_kctx_all_channels(ksession, &kctx, event_name);
174 if (ret != LTTCOMM_OK) {
175 goto error;
176 }
177 } else {
178 /* Get kernel channel */
179 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
180 if (kchan == NULL) {
181 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
182 goto error;
183 }
184
185 ret = add_kctx_to_channel(&kctx, kchan, event_name);
186 if (ret != LTTCOMM_OK) {
187 goto error;
188 }
189 }
190
191 ret = LTTCOMM_OK;
192
193 error:
194 return ret;
195 }
196
197 /*
198 * Add UST context to tracer.
199 */
200 int context_ust_add(struct ltt_ust_session *usess, int domain,
201 struct lttng_event_context *ctx, char *event_name,
202 char *channel_name)
203 {
204 int ret = LTTCOMM_OK, have_event = 0;
205 struct cds_lfht_iter iter, uiter;
206 struct cds_lfht *chan_ht;
207 struct ltt_ust_channel *uchan = NULL;
208 struct ltt_ust_event *uevent = NULL;
209 struct ltt_ust_context *uctx;
210
211 switch (domain) {
212 case LTTNG_DOMAIN_UST:
213 chan_ht = usess->domain_global.channels;
214 break;
215 case LTTNG_DOMAIN_UST_EXEC_NAME:
216 case LTTNG_DOMAIN_UST_PID:
217 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
218 default:
219 ret = LTTCOMM_NOT_IMPLEMENTED;
220 goto error;
221 }
222
223 if (strlen(event_name) != 0) {
224 have_event = 1;
225 }
226
227 /* Get UST channel if defined */
228 if (strlen(channel_name) != 0) {
229 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
230 if (uchan == NULL) {
231 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
232 goto error;
233 }
234 }
235
236 /* If UST channel specified and event name, get UST event ref */
237 if (uchan && have_event) {
238 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
239 if (uevent == NULL) {
240 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
241 goto error;
242 }
243 }
244
245 /* Create ltt UST context */
246 uctx = trace_ust_create_context(ctx);
247 if (uctx == NULL) {
248 ret = LTTCOMM_FATAL;
249 goto error;
250 }
251
252 /* At this point, we have 4 possibilities */
253
254 if (uchan && uevent) { /* Add ctx to event in channel */
255 ret = ust_app_add_ctx_event_glb(usess, uchan, uevent, uctx);
256 } else if (uchan && !have_event) { /* Add ctx to channel */
257 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
258 } else if (!uchan && have_event) { /* Add ctx to event */
259 cds_lfht_for_each_entry(chan_ht, &iter, uchan, node) {
260 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
261 if (uevent != NULL) {
262 ret = ust_app_add_ctx_event_glb(usess, uchan, uevent, uctx);
263 /*
264 * LTTng UST does not allowed the same event to be registered
265 * multiple time in different or the same channel. So, if we
266 * found our event in the first place, no need to continue.
267 */
268 goto end;
269 }
270 }
271 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
272 goto error_free_ctx;
273 } else if (!uchan && !have_event) { /* Add ctx to all events, all channels */
274 cds_lfht_for_each_entry(chan_ht, &iter, uchan, node) {
275 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
276 if (ret < 0) {
277 /* Add failed so uctx was not added. We can keep it. */
278 continue;
279 }
280 cds_lfht_for_each_entry(uchan->events, &uiter, uevent, node) {
281 ret = ust_app_add_ctx_event_glb(usess, uchan, uevent, uctx);
282 if (ret < 0) {
283 /* Add failed so uctx was not added. We can keep it. */
284 continue;
285 }
286 /* Create ltt UST context */
287 uctx = trace_ust_create_context(ctx);
288 if (uctx == NULL) {
289 ret = LTTCOMM_FATAL;
290 goto error;
291 }
292 }
293 /* Create ltt UST context */
294 uctx = trace_ust_create_context(ctx);
295 if (uctx == NULL) {
296 ret = LTTCOMM_FATAL;
297 goto error;
298 }
299 }
300 }
301
302 end:
303 switch (ret) {
304 case -EEXIST:
305 ret = LTTCOMM_UST_CONTEXT_EXIST;
306 goto error_free_ctx;
307 case -ENOMEM:
308 ret = LTTCOMM_FATAL;
309 goto error_free_ctx;
310 }
311
312 return LTTCOMM_OK;
313
314 error_free_ctx:
315 free(uctx);
316 error:
317 return ret;
318 }
This page took 0.036368 seconds and 5 git commands to generate.