Fix: Don't report an error when listing a session with no channels
[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 32
b579acd9
DG
33/*
34 * Add kernel context to all channel.
b579acd9
DG
35 */
36static int add_kctx_all_channels(struct ltt_kernel_session *ksession,
645328ae 37 struct ltt_kernel_context *kctx)
b579acd9 38{
601d5acf 39 int ret;
b579acd9
DG
40 struct ltt_kernel_channel *kchan;
41
0525e9ae
DG
42 assert(ksession);
43 assert(kctx);
44
601d5acf 45 DBG("Adding kernel context to all channels");
b579acd9
DG
46
47 /* Go over all channels */
48 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
601d5acf
DG
49 ret = kernel_add_channel_context(kchan, kctx);
50 if (ret < 0) {
51 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
52 goto error;
b579acd9
DG
53 }
54 }
55
f73fabfd 56 ret = LTTNG_OK;
b579acd9
DG
57
58error:
59 return ret;
60}
61
62/*
63 * Add kernel context to a specific channel.
b579acd9 64 */
645328ae 65static int add_kctx_to_channel(struct ltt_kernel_context *kctx,
601d5acf 66 struct ltt_kernel_channel *kchan)
b579acd9 67{
601d5acf 68 int ret;
b579acd9 69
0525e9ae
DG
70 assert(kchan);
71 assert(kctx);
72
601d5acf 73 DBG("Add kernel context to channel '%s'", kchan->channel->name);
b579acd9 74
601d5acf
DG
75 ret = kernel_add_channel_context(kchan, kctx);
76 if (ret < 0) {
77 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
b579acd9
DG
78 goto error;
79 }
80
f73fabfd 81 ret = LTTNG_OK;
b579acd9
DG
82
83error:
84 return ret;
85}
86
6b79ed20
DG
87/*
88 * Add UST context to channel.
89 */
90static int add_uctx_to_channel(struct ltt_ust_session *usess, int domain,
91 struct ltt_ust_channel *uchan, struct lttng_event_context *ctx)
92{
93 int ret;
94 struct ltt_ust_context *uctx;
95
0525e9ae
DG
96 assert(usess);
97 assert(uchan);
98 assert(ctx);
99
aa3514e9
MD
100 /* Check if context is duplicate */
101 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
102 if (trace_ust_match_context(uctx, ctx)) {
103 ret = -EEXIST;
104 goto duplicate;
105 }
106 }
107
6b79ed20
DG
108 /* Create ltt UST context */
109 uctx = trace_ust_create_context(ctx);
110 if (uctx == NULL) {
727d5404 111 ret = -EINVAL;
6b79ed20
DG
112 goto error;
113 }
114
115 switch (domain) {
116 case LTTNG_DOMAIN_UST:
117 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
118 if (ret < 0) {
119 goto error;
120 }
121 break;
122 default:
727d5404
DG
123 ret = -ENOSYS;
124 goto error;
125 }
126
e7fe706f
DG
127 rcu_read_lock();
128
6b79ed20 129 /* Add ltt UST context node to ltt UST channel */
aa3514e9 130 lttng_ht_add_ulong(uchan->ctx, &uctx->node);
e7fe706f 131 rcu_read_unlock();
31746f93 132 cds_list_add_tail(&uctx->list, &uchan->ctx_list);
6b79ed20 133
727d5404
DG
134 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
135
136 return 0;
6b79ed20
DG
137
138error:
139 free(uctx);
aa3514e9 140duplicate:
6b79ed20
DG
141 return ret;
142}
143
b579acd9
DG
144/*
145 * Add kernel context to tracer.
146 */
54d01ffb 147int context_kernel_add(struct ltt_kernel_session *ksession,
601d5acf 148 struct lttng_event_context *ctx, char *channel_name)
b579acd9
DG
149{
150 int ret;
151 struct ltt_kernel_channel *kchan;
645328ae 152 struct ltt_kernel_context *kctx;
54d01ffb 153
0525e9ae
DG
154 assert(ksession);
155 assert(ctx);
156 assert(channel_name);
157
645328ae
DG
158 kctx = trace_kernel_create_context(NULL);
159 if (!kctx) {
160 ret = LTTNG_ERR_NOMEM;
161 goto error;
162 }
163
54d01ffb 164 /* Setup kernel context structure */
9197c5c4
MD
165 switch (ctx->ctx) {
166 case LTTNG_EVENT_CONTEXT_PID:
645328ae 167 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
9197c5c4 168 break;
9197c5c4 169 case LTTNG_EVENT_CONTEXT_PROCNAME:
645328ae 170 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
9197c5c4
MD
171 break;
172 case LTTNG_EVENT_CONTEXT_PRIO:
645328ae 173 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
9197c5c4
MD
174 break;
175 case LTTNG_EVENT_CONTEXT_NICE:
645328ae 176 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
9197c5c4
MD
177 break;
178 case LTTNG_EVENT_CONTEXT_VPID:
645328ae 179 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
9197c5c4
MD
180 break;
181 case LTTNG_EVENT_CONTEXT_TID:
645328ae 182 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
9197c5c4
MD
183 break;
184 case LTTNG_EVENT_CONTEXT_VTID:
645328ae 185 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
9197c5c4
MD
186 break;
187 case LTTNG_EVENT_CONTEXT_PPID:
645328ae 188 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
9197c5c4
MD
189 break;
190 case LTTNG_EVENT_CONTEXT_VPPID:
645328ae 191 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
9197c5c4 192 break;
54773d68 193 case LTTNG_EVENT_CONTEXT_HOSTNAME:
645328ae 194 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
54773d68 195 break;
aa3514e9
MD
196 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
197 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
645328ae 198 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER;
aa3514e9 199 break;
9197c5c4 200 default:
f73fabfd 201 return LTTNG_ERR_KERN_CONTEXT_FAIL;
9197c5c4
MD
202 }
203
645328ae
DG
204 kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
205 kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
206 strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
54d01ffb 207 LTTNG_SYMBOL_NAME_LEN);
645328ae 208 kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
b579acd9 209
9d035200 210 if (*channel_name == '\0') {
645328ae 211 ret = add_kctx_all_channels(ksession, kctx);
f73fabfd 212 if (ret != LTTNG_OK) {
b579acd9
DG
213 goto error;
214 }
215 } else {
216 /* Get kernel channel */
62499ad6 217 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
b579acd9 218 if (kchan == NULL) {
f73fabfd 219 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
b579acd9
DG
220 goto error;
221 }
222
645328ae 223 ret = add_kctx_to_channel(kctx, kchan);
f73fabfd 224 if (ret != LTTNG_OK) {
b579acd9
DG
225 goto error;
226 }
227 }
228
f73fabfd 229 ret = LTTNG_OK;
b579acd9
DG
230
231error:
232 return ret;
233}
2bdd86d4
MD
234
235/*
55cc08a6 236 * Add UST context to tracer.
2bdd86d4 237 */
55cc08a6 238int context_ust_add(struct ltt_ust_session *usess, int domain,
601d5acf 239 struct lttng_event_context *ctx, char *channel_name)
2bdd86d4 240{
601d5acf 241 int ret = LTTNG_OK;
442359c0 242 struct lttng_ht_iter iter;
bec39940 243 struct lttng_ht *chan_ht;
55cc08a6 244 struct ltt_ust_channel *uchan = NULL;
2bdd86d4 245
0525e9ae
DG
246 assert(usess);
247 assert(ctx);
248 assert(channel_name);
249
2223c96f
DG
250 rcu_read_lock();
251
6b79ed20
DG
252 /*
253 * Define which channel's hashtable to use from the domain or quit if
254 * unknown domain.
255 */
55cc08a6
DG
256 switch (domain) {
257 case LTTNG_DOMAIN_UST:
258 chan_ht = usess->domain_global.channels;
259 break;
d78d6610 260#if 0
55cc08a6
DG
261 case LTTNG_DOMAIN_UST_EXEC_NAME:
262 case LTTNG_DOMAIN_UST_PID:
263 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 264#endif
55cc08a6 265 default:
f73fabfd 266 ret = LTTNG_ERR_UND;
2bdd86d4
MD
267 goto error;
268 }
2bdd86d4 269
55cc08a6 270 /* Get UST channel if defined */
9f9ee9c9 271 if (channel_name[0] != '\0') {
55cc08a6
DG
272 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
273 if (uchan == NULL) {
f73fabfd 274 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2bdd86d4
MD
275 goto error;
276 }
55cc08a6
DG
277 }
278
601d5acf
DG
279 if (uchan) {
280 /* Add ctx to channel */
6b79ed20 281 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
601d5acf 282 } else {
e7fe706f 283 rcu_read_lock();
601d5acf 284 /* Add ctx all events, all channels */
bec39940 285 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
6b79ed20 286 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
55cc08a6 287 if (ret < 0) {
ae856491 288 ERR("Context failed for channel %s", uchan->name);
55cc08a6
DG
289 continue;
290 }
2bdd86d4 291 }
e7fe706f 292 rcu_read_unlock();
55cc08a6 293 }
2bdd86d4 294
55cc08a6
DG
295 switch (ret) {
296 case -EEXIST:
f73fabfd 297 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
727d5404 298 break;
55cc08a6 299 case -ENOMEM:
f73fabfd 300 ret = LTTNG_ERR_FATAL;
727d5404
DG
301 break;
302 case -EINVAL:
f73fabfd 303 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
727d5404
DG
304 break;
305 case -ENOSYS:
f73fabfd 306 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
727d5404
DG
307 break;
308 default:
f73fabfd 309 ret = LTTNG_OK;
727d5404 310 break;
2bdd86d4
MD
311 }
312
2bdd86d4 313error:
2223c96f 314 rcu_read_unlock();
2bdd86d4
MD
315 return ret;
316}
This page took 0.071165 seconds and 4 git commands to generate.