License header fixes
[lttng-tools.git] / src / bin / lttng-sessiond / context.c
CommitLineData
b579acd9
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
b579acd9 7 *
d14d33bf
AM
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.
b579acd9 12 *
d14d33bf
AM
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.
b579acd9
DG
16 */
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
54d01ffb 23#include <urcu/list.h>
1e307fab 24
db758600 25#include <common/error.h>
10a8a223 26#include <common/sessiond-comm/sessiond-comm.h>
b579acd9 27
b579acd9 28#include "context.h"
4771f025 29#include "kernel.h"
55cc08a6 30#include "ust-app.h"
34378f76 31#include "trace-ust.h"
b579acd9
DG
32
33/*
34 * Add kernel context to an event of a specific channel.
35 */
36static 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
62499ad6 44 kevent = trace_kernel_get_event_by_name(event_name, kchan);
b579acd9
DG
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
55error:
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 */
64static 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
104error:
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 */
113static 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
149error:
150 return ret;
151}
152
6b79ed20
DG
153/*
154 * Add UST context to channel.
155 */
156static 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;
727d5404
DG
161 struct lttng_ht_iter iter;
162 struct lttng_ht_node_ulong *uctx_node;
6b79ed20
DG
163
164 /* Create ltt UST context */
165 uctx = trace_ust_create_context(ctx);
166 if (uctx == NULL) {
727d5404 167 ret = -EINVAL;
6b79ed20
DG
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:
727d5404
DG
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;
6b79ed20
DG
188 goto error;
189 }
190
191 /* Add ltt UST context node to ltt UST channel */
bec39940 192 lttng_ht_add_unique_ulong(uchan->ctx, &uctx->node);
6b79ed20 193
727d5404
DG
194 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
195
196 return 0;
6b79ed20
DG
197
198error:
199 free(uctx);
200 return ret;
201}
202
203/*
204 * Add UST context to event.
205 */
206static 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;
727d5404
DG
212 struct lttng_ht_iter iter;
213 struct lttng_ht_node_ulong *uctx_node;
6b79ed20
DG
214
215 /* Create ltt UST context */
216 uctx = trace_ust_create_context(ctx);
217 if (uctx == NULL) {
727d5404
DG
218 /* Context values are invalid. */
219 ret = -EINVAL;
6b79ed20
DG
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:
727d5404
DG
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;
6b79ed20
DG
240 goto error;
241 }
242
243 /* Add ltt UST context node to ltt UST event */
bec39940 244 lttng_ht_add_unique_ulong(uevent->ctx, &uctx->node);
6b79ed20 245
727d5404
DG
246 DBG("Context UST %d added to event %s", uctx->ctx.ctx, uevent->attr.name);
247
248 return 0;
6b79ed20
DG
249
250error:
251 free(uctx);
252 return ret;
253}
254
b579acd9
DG
255/*
256 * Add kernel context to tracer.
257 */
54d01ffb
DG
258int context_kernel_add(struct ltt_kernel_session *ksession,
259 struct lttng_event_context *ctx, char *event_name,
b579acd9
DG
260 char *channel_name)
261{
262 int ret;
263 struct ltt_kernel_channel *kchan;
54d01ffb
DG
264 struct lttng_kernel_context kctx;
265
266 /* Setup kernel context structure */
9197c5c4
MD
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
54d01ffb
DG
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';
b579acd9
DG
307
308 if (strlen(channel_name) == 0) {
54d01ffb 309 ret = add_kctx_all_channels(ksession, &kctx, event_name);
b579acd9
DG
310 if (ret != LTTCOMM_OK) {
311 goto error;
312 }
313 } else {
314 /* Get kernel channel */
62499ad6 315 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
b579acd9
DG
316 if (kchan == NULL) {
317 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
318 goto error;
319 }
320
54d01ffb 321 ret = add_kctx_to_channel(&kctx, kchan, event_name);
b579acd9
DG
322 if (ret != LTTCOMM_OK) {
323 goto error;
324 }
325 }
326
327 ret = LTTCOMM_OK;
328
329error:
330 return ret;
331}
2bdd86d4
MD
332
333/*
55cc08a6 334 * Add UST context to tracer.
2bdd86d4 335 */
55cc08a6
DG
336int context_ust_add(struct ltt_ust_session *usess, int domain,
337 struct lttng_event_context *ctx, char *event_name,
338 char *channel_name)
2bdd86d4 339{
dae5ce8d 340 int ret = LTTCOMM_OK, have_event = 0;
442359c0 341 struct lttng_ht_iter iter;
bec39940 342 struct lttng_ht *chan_ht;
55cc08a6
DG
343 struct ltt_ust_channel *uchan = NULL;
344 struct ltt_ust_event *uevent = NULL;
2bdd86d4 345
6b79ed20
DG
346 /*
347 * Define which channel's hashtable to use from the domain or quit if
348 * unknown domain.
349 */
55cc08a6
DG
350 switch (domain) {
351 case LTTNG_DOMAIN_UST:
352 chan_ht = usess->domain_global.channels;
353 break;
d78d6610 354#if 0
55cc08a6
DG
355 case LTTNG_DOMAIN_UST_EXEC_NAME:
356 case LTTNG_DOMAIN_UST_PID:
357 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 358#endif
55cc08a6 359 default:
1ab1ea0b 360 ret = LTTCOMM_UND;
2bdd86d4
MD
361 goto error;
362 }
2bdd86d4 363
6b79ed20 364 /* Do we have an event name */
55cc08a6
DG
365 if (strlen(event_name) != 0) {
366 have_event = 1;
2bdd86d4
MD
367 }
368
55cc08a6
DG
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;
2bdd86d4
MD
374 goto error;
375 }
55cc08a6
DG
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;
2bdd86d4 383 goto error;
2bdd86d4
MD
384 }
385 }
386
55cc08a6
DG
387 /* At this point, we have 4 possibilities */
388
6b79ed20
DG
389 if (uchan && uevent) { /* Add ctx to event in channel */
390 ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
55cc08a6 391 } else if (uchan && !have_event) { /* Add ctx to channel */
6b79ed20 392 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
55cc08a6 393 } else if (!uchan && have_event) { /* Add ctx to event */
6b79ed20 394 /* Add context to event without having the channel name */
bec39940 395 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
55cc08a6
DG
396 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
397 if (uevent != NULL) {
6b79ed20 398 ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
55cc08a6
DG
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
6b79ed20 402 * found our event, we stop.
55cc08a6
DG
403 */
404 goto end;
405 }
2bdd86d4 406 }
55cc08a6 407 ret = LTTCOMM_UST_EVENT_NOT_FOUND;
6b79ed20
DG
408 goto error;
409 } else if (!uchan && !have_event) { /* Add ctx all events, all channels */
410 /* For all channels */
bec39940 411 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
6b79ed20 412 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
55cc08a6 413 if (ret < 0) {
ae856491 414 ERR("Context failed for channel %s", uchan->name);
55cc08a6
DG
415 continue;
416 }
2bdd86d4 417 }
55cc08a6 418 }
2bdd86d4 419
55cc08a6
DG
420end:
421 switch (ret) {
422 case -EEXIST:
423 ret = LTTCOMM_UST_CONTEXT_EXIST;
727d5404 424 break;
55cc08a6
DG
425 case -ENOMEM:
426 ret = LTTCOMM_FATAL;
727d5404
DG
427 break;
428 case -EINVAL:
ae856491 429 ret = LTTCOMM_UST_CONTEXT_INVAL;
727d5404
DG
430 break;
431 case -ENOSYS:
432 ret = LTTCOMM_UNKNOWN_DOMAIN;
433 break;
434 default:
435 ret = LTTCOMM_OK;
436 break;
2bdd86d4
MD
437 }
438
2bdd86d4
MD
439error:
440 return ret;
441}
This page took 0.045011 seconds and 4 git commands to generate.