Update lttng-ust-abi.h for event field listing
[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 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 UST context to channel.
155 */
156 static int add_uctx_to_channel(struct ltt_ust_session *usess, int domain,
157 struct ltt_ust_channel *uchan, struct lttng_event_context *ctx)
158 {
159 int ret;
160 struct ltt_ust_context *uctx;
161 struct lttng_ht_iter iter;
162 struct lttng_ht_node_ulong *uctx_node;
163
164 /* Create ltt UST context */
165 uctx = trace_ust_create_context(ctx);
166 if (uctx == NULL) {
167 ret = -EINVAL;
168 goto error;
169 }
170
171 switch (domain) {
172 case LTTNG_DOMAIN_UST:
173 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
174 if (ret < 0) {
175 goto error;
176 }
177 break;
178 default:
179 ret = -ENOSYS;
180 goto error;
181 }
182
183 /* Lookup context before adding it */
184 lttng_ht_lookup(uchan->ctx, (void *)((unsigned long)uctx->ctx.ctx), &iter);
185 uctx_node = lttng_ht_iter_get_node_ulong(&iter);
186 if (uctx_node != NULL) {
187 ret = -EEXIST;
188 goto error;
189 }
190
191 /* Add ltt UST context node to ltt UST channel */
192 lttng_ht_add_unique_ulong(uchan->ctx, &uctx->node);
193
194 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
195
196 return 0;
197
198 error:
199 free(uctx);
200 return ret;
201 }
202
203 /*
204 * Add UST context to event.
205 */
206 static int add_uctx_to_event(struct ltt_ust_session *usess, int domain,
207 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
208 struct lttng_event_context *ctx)
209 {
210 int ret;
211 struct ltt_ust_context *uctx;
212 struct lttng_ht_iter iter;
213 struct lttng_ht_node_ulong *uctx_node;
214
215 /* Create ltt UST context */
216 uctx = trace_ust_create_context(ctx);
217 if (uctx == NULL) {
218 /* Context values are invalid. */
219 ret = -EINVAL;
220 goto error;
221 }
222
223 switch (domain) {
224 case LTTNG_DOMAIN_UST:
225 ret = ust_app_add_ctx_event_glb(usess, uchan, uevent, uctx);
226 if (ret < 0) {
227 goto error;
228 }
229 break;
230 default:
231 ret = -ENOSYS;
232 goto error;
233 }
234
235 /* Lookup context before adding it */
236 lttng_ht_lookup(uevent->ctx, (void *)((unsigned long)uctx->ctx.ctx), &iter);
237 uctx_node = lttng_ht_iter_get_node_ulong(&iter);
238 if (uctx_node != NULL) {
239 ret = -EEXIST;
240 goto error;
241 }
242
243 /* Add ltt UST context node to ltt UST event */
244 lttng_ht_add_unique_ulong(uevent->ctx, &uctx->node);
245
246 DBG("Context UST %d added to event %s", uctx->ctx.ctx, uevent->attr.name);
247
248 return 0;
249
250 error:
251 free(uctx);
252 return ret;
253 }
254
255 /*
256 * Add kernel context to tracer.
257 */
258 int context_kernel_add(struct ltt_kernel_session *ksession,
259 struct lttng_event_context *ctx, char *event_name,
260 char *channel_name)
261 {
262 int ret;
263 struct ltt_kernel_channel *kchan;
264 struct lttng_kernel_context kctx;
265
266 /* Setup kernel context structure */
267 switch (ctx->ctx) {
268 case LTTNG_EVENT_CONTEXT_PID:
269 kctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
270 break;
271 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
272 kctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_COUNTER;
273 break;
274 case LTTNG_EVENT_CONTEXT_PROCNAME:
275 kctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
276 break;
277 case LTTNG_EVENT_CONTEXT_PRIO:
278 kctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
279 break;
280 case LTTNG_EVENT_CONTEXT_NICE:
281 kctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
282 break;
283 case LTTNG_EVENT_CONTEXT_VPID:
284 kctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
285 break;
286 case LTTNG_EVENT_CONTEXT_TID:
287 kctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
288 break;
289 case LTTNG_EVENT_CONTEXT_VTID:
290 kctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
291 break;
292 case LTTNG_EVENT_CONTEXT_PPID:
293 kctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
294 break;
295 case LTTNG_EVENT_CONTEXT_VPPID:
296 kctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
297 break;
298 default:
299 return LTTCOMM_KERN_CONTEXT_FAIL;
300 }
301
302 kctx.u.perf_counter.type = ctx->u.perf_counter.type;
303 kctx.u.perf_counter.config = ctx->u.perf_counter.config;
304 strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name,
305 LTTNG_SYMBOL_NAME_LEN);
306 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
307
308 if (strlen(channel_name) == 0) {
309 ret = add_kctx_all_channels(ksession, &kctx, event_name);
310 if (ret != LTTCOMM_OK) {
311 goto error;
312 }
313 } else {
314 /* Get kernel channel */
315 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
316 if (kchan == NULL) {
317 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
318 goto error;
319 }
320
321 ret = add_kctx_to_channel(&kctx, kchan, event_name);
322 if (ret != LTTCOMM_OK) {
323 goto error;
324 }
325 }
326
327 ret = LTTCOMM_OK;
328
329 error:
330 return ret;
331 }
332
333 /*
334 * Add UST context to tracer.
335 */
336 int context_ust_add(struct ltt_ust_session *usess, int domain,
337 struct lttng_event_context *ctx, char *event_name,
338 char *channel_name)
339 {
340 int ret = LTTCOMM_OK, have_event = 0;
341 struct lttng_ht_iter iter;
342 struct lttng_ht *chan_ht;
343 struct ltt_ust_channel *uchan = NULL;
344 struct ltt_ust_event *uevent = NULL;
345
346 /*
347 * Define which channel's hashtable to use from the domain or quit if
348 * unknown domain.
349 */
350 switch (domain) {
351 case LTTNG_DOMAIN_UST:
352 chan_ht = usess->domain_global.channels;
353 break;
354 #if 0
355 case LTTNG_DOMAIN_UST_EXEC_NAME:
356 case LTTNG_DOMAIN_UST_PID:
357 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
358 #endif
359 default:
360 ret = LTTCOMM_UND;
361 goto error;
362 }
363
364 /* Do we have an event name */
365 if (strlen(event_name) != 0) {
366 have_event = 1;
367 }
368
369 /* Get UST channel if defined */
370 if (strlen(channel_name) != 0) {
371 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
372 if (uchan == NULL) {
373 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
374 goto error;
375 }
376 }
377
378 /* If UST channel specified and event name, get UST event ref */
379 if (uchan && have_event) {
380 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
381 if (uevent == NULL) {
382 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
383 goto error;
384 }
385 }
386
387 /* At this point, we have 4 possibilities */
388
389 if (uchan && uevent) { /* Add ctx to event in channel */
390 ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
391 } else if (uchan && !have_event) { /* Add ctx to channel */
392 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
393 } else if (!uchan && have_event) { /* Add ctx to event */
394 /* Add context to event without having the channel name */
395 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
396 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
397 if (uevent != NULL) {
398 ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
399 /*
400 * LTTng UST does not allowed the same event to be registered
401 * multiple time in different or the same channel. So, if we
402 * found our event, we stop.
403 */
404 goto end;
405 }
406 }
407 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
408 goto error;
409 } else if (!uchan && !have_event) { /* Add ctx all events, all channels */
410 /* For all channels */
411 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
412 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
413 if (ret < 0) {
414 ERR("Context failed for channel %s", uchan->name);
415 continue;
416 }
417 }
418 }
419
420 end:
421 switch (ret) {
422 case -EEXIST:
423 ret = LTTCOMM_UST_CONTEXT_EXIST;
424 break;
425 case -ENOMEM:
426 ret = LTTCOMM_FATAL;
427 break;
428 case -EINVAL:
429 ret = LTTCOMM_UST_CONTEXT_INVAL;
430 break;
431 case -ENOSYS:
432 ret = LTTCOMM_UNKNOWN_DOMAIN;
433 break;
434 default:
435 ret = LTTCOMM_OK;
436 break;
437 }
438
439 error:
440 return ret;
441 }
This page took 0.03881 seconds and 4 git commands to generate.