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