Fix: poll: show the correct number of fds
[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,
601d5acf 37 struct lttng_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
DG
64 */
65static int add_kctx_to_channel(struct lttng_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;
727d5404
DG
95 struct lttng_ht_iter iter;
96 struct lttng_ht_node_ulong *uctx_node;
6b79ed20 97
0525e9ae
DG
98 assert(usess);
99 assert(uchan);
100 assert(ctx);
101
6b79ed20
DG
102 /* Create ltt UST context */
103 uctx = trace_ust_create_context(ctx);
104 if (uctx == NULL) {
727d5404 105 ret = -EINVAL;
6b79ed20
DG
106 goto error;
107 }
108
109 switch (domain) {
110 case LTTNG_DOMAIN_UST:
111 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
112 if (ret < 0) {
113 goto error;
114 }
115 break;
116 default:
727d5404
DG
117 ret = -ENOSYS;
118 goto error;
119 }
120
e7fe706f
DG
121 rcu_read_lock();
122
727d5404
DG
123 /* Lookup context before adding it */
124 lttng_ht_lookup(uchan->ctx, (void *)((unsigned long)uctx->ctx.ctx), &iter);
125 uctx_node = lttng_ht_iter_get_node_ulong(&iter);
126 if (uctx_node != NULL) {
127 ret = -EEXIST;
e7fe706f 128 rcu_read_unlock();
6b79ed20
DG
129 goto error;
130 }
131
132 /* Add ltt UST context node to ltt UST channel */
bec39940 133 lttng_ht_add_unique_ulong(uchan->ctx, &uctx->node);
e7fe706f 134 rcu_read_unlock();
31746f93 135 cds_list_add_tail(&uctx->list, &uchan->ctx_list);
6b79ed20 136
727d5404
DG
137 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
138
139 return 0;
6b79ed20
DG
140
141error:
142 free(uctx);
143 return ret;
144}
145
b579acd9
DG
146/*
147 * Add kernel context to tracer.
148 */
54d01ffb 149int context_kernel_add(struct ltt_kernel_session *ksession,
601d5acf 150 struct lttng_event_context *ctx, char *channel_name)
b579acd9
DG
151{
152 int ret;
153 struct ltt_kernel_channel *kchan;
54d01ffb
DG
154 struct lttng_kernel_context kctx;
155
0525e9ae
DG
156 assert(ksession);
157 assert(ctx);
158 assert(channel_name);
159
54d01ffb 160 /* Setup kernel context structure */
9197c5c4
MD
161 switch (ctx->ctx) {
162 case LTTNG_EVENT_CONTEXT_PID:
163 kctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
164 break;
165 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
166 kctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_COUNTER;
167 break;
168 case LTTNG_EVENT_CONTEXT_PROCNAME:
169 kctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
170 break;
171 case LTTNG_EVENT_CONTEXT_PRIO:
172 kctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
173 break;
174 case LTTNG_EVENT_CONTEXT_NICE:
175 kctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
176 break;
177 case LTTNG_EVENT_CONTEXT_VPID:
178 kctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
179 break;
180 case LTTNG_EVENT_CONTEXT_TID:
181 kctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
182 break;
183 case LTTNG_EVENT_CONTEXT_VTID:
184 kctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
185 break;
186 case LTTNG_EVENT_CONTEXT_PPID:
187 kctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
188 break;
189 case LTTNG_EVENT_CONTEXT_VPPID:
190 kctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
191 break;
54773d68
JD
192 case LTTNG_EVENT_CONTEXT_HOSTNAME:
193 kctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
194 break;
9197c5c4 195 default:
f73fabfd 196 return LTTNG_ERR_KERN_CONTEXT_FAIL;
9197c5c4
MD
197 }
198
54d01ffb
DG
199 kctx.u.perf_counter.type = ctx->u.perf_counter.type;
200 kctx.u.perf_counter.config = ctx->u.perf_counter.config;
201 strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name,
202 LTTNG_SYMBOL_NAME_LEN);
203 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
b579acd9 204
9d035200 205 if (*channel_name == '\0') {
601d5acf 206 ret = add_kctx_all_channels(ksession, &kctx);
f73fabfd 207 if (ret != LTTNG_OK) {
b579acd9
DG
208 goto error;
209 }
210 } else {
211 /* Get kernel channel */
62499ad6 212 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
b579acd9 213 if (kchan == NULL) {
f73fabfd 214 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
b579acd9
DG
215 goto error;
216 }
217
601d5acf 218 ret = add_kctx_to_channel(&kctx, kchan);
f73fabfd 219 if (ret != LTTNG_OK) {
b579acd9
DG
220 goto error;
221 }
222 }
223
f73fabfd 224 ret = LTTNG_OK;
b579acd9
DG
225
226error:
227 return ret;
228}
2bdd86d4
MD
229
230/*
55cc08a6 231 * Add UST context to tracer.
2bdd86d4 232 */
55cc08a6 233int context_ust_add(struct ltt_ust_session *usess, int domain,
601d5acf 234 struct lttng_event_context *ctx, char *channel_name)
2bdd86d4 235{
601d5acf 236 int ret = LTTNG_OK;
442359c0 237 struct lttng_ht_iter iter;
bec39940 238 struct lttng_ht *chan_ht;
55cc08a6 239 struct ltt_ust_channel *uchan = NULL;
2bdd86d4 240
0525e9ae
DG
241 assert(usess);
242 assert(ctx);
243 assert(channel_name);
244
2223c96f
DG
245 rcu_read_lock();
246
6b79ed20
DG
247 /*
248 * Define which channel's hashtable to use from the domain or quit if
249 * unknown domain.
250 */
55cc08a6
DG
251 switch (domain) {
252 case LTTNG_DOMAIN_UST:
253 chan_ht = usess->domain_global.channels;
254 break;
d78d6610 255#if 0
55cc08a6
DG
256 case LTTNG_DOMAIN_UST_EXEC_NAME:
257 case LTTNG_DOMAIN_UST_PID:
258 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 259#endif
55cc08a6 260 default:
f73fabfd 261 ret = LTTNG_ERR_UND;
2bdd86d4
MD
262 goto error;
263 }
2bdd86d4 264
55cc08a6 265 /* Get UST channel if defined */
9f9ee9c9 266 if (channel_name[0] != '\0') {
55cc08a6
DG
267 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
268 if (uchan == NULL) {
f73fabfd 269 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2bdd86d4
MD
270 goto error;
271 }
55cc08a6
DG
272 }
273
601d5acf
DG
274 if (uchan) {
275 /* Add ctx to channel */
6b79ed20 276 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
601d5acf 277 } else {
e7fe706f 278 rcu_read_lock();
601d5acf 279 /* Add ctx all events, all channels */
bec39940 280 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
6b79ed20 281 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
55cc08a6 282 if (ret < 0) {
ae856491 283 ERR("Context failed for channel %s", uchan->name);
55cc08a6
DG
284 continue;
285 }
2bdd86d4 286 }
e7fe706f 287 rcu_read_unlock();
55cc08a6 288 }
2bdd86d4 289
55cc08a6
DG
290 switch (ret) {
291 case -EEXIST:
f73fabfd 292 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
727d5404 293 break;
55cc08a6 294 case -ENOMEM:
f73fabfd 295 ret = LTTNG_ERR_FATAL;
727d5404
DG
296 break;
297 case -EINVAL:
f73fabfd 298 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
727d5404
DG
299 break;
300 case -ENOSYS:
f73fabfd 301 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
727d5404
DG
302 break;
303 default:
f73fabfd 304 ret = LTTNG_OK;
727d5404 305 break;
2bdd86d4
MD
306 }
307
2bdd86d4 308error:
2223c96f 309 rcu_read_unlock();
2bdd86d4
MD
310 return ret;
311}
This page took 0.056495 seconds and 4 git commands to generate.