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