Add liblttng-ctl destructor to cleanup memory
[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) {
f73fabfd 81 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
b579acd9
DG
82 goto error;
83 }
84 } else {
85 ret = add_kctx_to_event(kctx, kchan, event_name);
86 if (ret < 0) {
f73fabfd 87 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
b579acd9
DG
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) {
f73fabfd 98 ret = LTTNG_ERR_NO_EVENT;
b579acd9
DG
99 goto error;
100 }
101
f73fabfd 102 ret = LTTNG_OK;
b579acd9
DG
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) {
f73fabfd 128 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
b579acd9
DG
129 goto error;
130 }
131 } else {
132 ret = add_kctx_to_event(kctx, kchan, event_name);
133 if (ret < 0) {
f73fabfd 134 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
b579acd9
DG
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) {
f73fabfd 143 ret = LTTNG_ERR_NO_EVENT;
b579acd9
DG
144 goto error;
145 }
146
f73fabfd 147 ret = LTTNG_OK;
b579acd9
DG
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;
54773d68
JD
298 case LTTNG_EVENT_CONTEXT_HOSTNAME:
299 kctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
300 break;
9197c5c4 301 default:
f73fabfd 302 return LTTNG_ERR_KERN_CONTEXT_FAIL;
9197c5c4
MD
303 }
304
54d01ffb
DG
305 kctx.u.perf_counter.type = ctx->u.perf_counter.type;
306 kctx.u.perf_counter.config = ctx->u.perf_counter.config;
307 strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name,
308 LTTNG_SYMBOL_NAME_LEN);
309 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
b579acd9
DG
310
311 if (strlen(channel_name) == 0) {
54d01ffb 312 ret = add_kctx_all_channels(ksession, &kctx, event_name);
f73fabfd 313 if (ret != LTTNG_OK) {
b579acd9
DG
314 goto error;
315 }
316 } else {
317 /* Get kernel channel */
62499ad6 318 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
b579acd9 319 if (kchan == NULL) {
f73fabfd 320 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
b579acd9
DG
321 goto error;
322 }
323
54d01ffb 324 ret = add_kctx_to_channel(&kctx, kchan, event_name);
f73fabfd 325 if (ret != LTTNG_OK) {
b579acd9
DG
326 goto error;
327 }
328 }
329
f73fabfd 330 ret = LTTNG_OK;
b579acd9
DG
331
332error:
333 return ret;
334}
2bdd86d4
MD
335
336/*
55cc08a6 337 * Add UST context to tracer.
2bdd86d4 338 */
55cc08a6
DG
339int context_ust_add(struct ltt_ust_session *usess, int domain,
340 struct lttng_event_context *ctx, char *event_name,
341 char *channel_name)
2bdd86d4 342{
f73fabfd 343 int ret = LTTNG_OK, have_event = 0;
442359c0 344 struct lttng_ht_iter iter;
bec39940 345 struct lttng_ht *chan_ht;
55cc08a6
DG
346 struct ltt_ust_channel *uchan = NULL;
347 struct ltt_ust_event *uevent = NULL;
2bdd86d4 348
6b79ed20
DG
349 /*
350 * Define which channel's hashtable to use from the domain or quit if
351 * unknown domain.
352 */
55cc08a6
DG
353 switch (domain) {
354 case LTTNG_DOMAIN_UST:
355 chan_ht = usess->domain_global.channels;
356 break;
d78d6610 357#if 0
55cc08a6
DG
358 case LTTNG_DOMAIN_UST_EXEC_NAME:
359 case LTTNG_DOMAIN_UST_PID:
360 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 361#endif
55cc08a6 362 default:
f73fabfd 363 ret = LTTNG_ERR_UND;
2bdd86d4
MD
364 goto error;
365 }
2bdd86d4 366
6b79ed20 367 /* Do we have an event name */
55cc08a6
DG
368 if (strlen(event_name) != 0) {
369 have_event = 1;
2bdd86d4
MD
370 }
371
55cc08a6
DG
372 /* Get UST channel if defined */
373 if (strlen(channel_name) != 0) {
374 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
375 if (uchan == NULL) {
f73fabfd 376 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2bdd86d4
MD
377 goto error;
378 }
55cc08a6
DG
379 }
380
381 /* If UST channel specified and event name, get UST event ref */
382 if (uchan && have_event) {
383 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
384 if (uevent == NULL) {
f73fabfd 385 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
2bdd86d4 386 goto error;
2bdd86d4
MD
387 }
388 }
389
55cc08a6
DG
390 /* At this point, we have 4 possibilities */
391
6b79ed20
DG
392 if (uchan && uevent) { /* Add ctx to event in channel */
393 ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
55cc08a6 394 } else if (uchan && !have_event) { /* Add ctx to channel */
6b79ed20 395 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
55cc08a6 396 } else if (!uchan && have_event) { /* Add ctx to event */
6b79ed20 397 /* Add context to event without having the channel name */
bec39940 398 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
55cc08a6
DG
399 uevent = trace_ust_find_event_by_name(uchan->events, event_name);
400 if (uevent != NULL) {
6b79ed20 401 ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
55cc08a6
DG
402 /*
403 * LTTng UST does not allowed the same event to be registered
404 * multiple time in different or the same channel. So, if we
6b79ed20 405 * found our event, we stop.
55cc08a6
DG
406 */
407 goto end;
408 }
2bdd86d4 409 }
f73fabfd 410 ret = LTTNG_ERR_UST_EVENT_NOT_FOUND;
6b79ed20
DG
411 goto error;
412 } else if (!uchan && !have_event) { /* Add ctx all events, all channels */
413 /* For all channels */
bec39940 414 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
6b79ed20 415 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
55cc08a6 416 if (ret < 0) {
ae856491 417 ERR("Context failed for channel %s", uchan->name);
55cc08a6
DG
418 continue;
419 }
2bdd86d4 420 }
55cc08a6 421 }
2bdd86d4 422
55cc08a6
DG
423end:
424 switch (ret) {
425 case -EEXIST:
f73fabfd 426 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
727d5404 427 break;
55cc08a6 428 case -ENOMEM:
f73fabfd 429 ret = LTTNG_ERR_FATAL;
727d5404
DG
430 break;
431 case -EINVAL:
f73fabfd 432 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
727d5404
DG
433 break;
434 case -ENOSYS:
f73fabfd 435 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
727d5404
DG
436 break;
437 default:
f73fabfd 438 ret = LTTNG_OK;
727d5404 439 break;
2bdd86d4
MD
440 }
441
2bdd86d4
MD
442error:
443 return ret;
444}
This page took 0.047609 seconds and 4 git commands to generate.