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