Create agent on channel creation
[lttng-tools.git] / src / bin / lttng-sessiond / context.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _LGPL_SOURCE
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <urcu/list.h>
25
26 #include <common/error.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28
29 #include "context.h"
30 #include "kernel.h"
31 #include "ust-app.h"
32 #include "trace-ust.h"
33 #include "agent.h"
34
35 /*
36 * Add kernel context to all channel.
37 */
38 static int add_kctx_all_channels(struct ltt_kernel_session *ksession,
39 struct ltt_kernel_context *kctx)
40 {
41 int ret;
42 struct ltt_kernel_channel *kchan;
43
44 assert(ksession);
45 assert(kctx);
46
47 DBG("Adding kernel context to all channels");
48
49 /* Go over all channels */
50 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
51 ret = kernel_add_channel_context(kchan, kctx);
52 if (ret < 0) {
53 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
54 goto error;
55 }
56 }
57
58 ret = LTTNG_OK;
59
60 error:
61 return ret;
62 }
63
64 /*
65 * Add kernel context to a specific channel.
66 */
67 static int add_kctx_to_channel(struct ltt_kernel_context *kctx,
68 struct ltt_kernel_channel *kchan)
69 {
70 int ret;
71
72 assert(kchan);
73 assert(kctx);
74
75 DBG("Add kernel context to channel '%s'", kchan->channel->name);
76
77 ret = kernel_add_channel_context(kchan, kctx);
78 if (ret < 0) {
79 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
80 goto error;
81 }
82
83 ret = LTTNG_OK;
84
85 error:
86 return ret;
87 }
88
89 /*
90 * Add UST context to channel.
91 */
92 static int add_uctx_to_channel(struct ltt_ust_session *usess,
93 enum lttng_domain_type domain,
94 struct ltt_ust_channel *uchan, struct lttng_event_context *ctx)
95 {
96 int ret;
97 struct ltt_ust_context *uctx = NULL;
98
99 assert(usess);
100 assert(uchan);
101 assert(ctx);
102
103 /* Check if context is duplicate */
104 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
105 if (trace_ust_match_context(uctx, ctx)) {
106 ret = -EEXIST;
107 goto duplicate;
108 }
109 }
110 uctx = NULL;
111
112 switch (domain) {
113 case LTTNG_DOMAIN_JUL:
114 case LTTNG_DOMAIN_LOG4J:
115 {
116 struct agent *agt = trace_ust_find_agent(usess, domain);
117
118 if (!agt) {
119 agt = agent_create(domain);
120 if (!agt) {
121 ret = LTTNG_ERR_NOMEM;
122 goto error;
123 }
124 agent_add(agt, usess->agents);
125 }
126 ret = agent_add_context(ctx, agt);
127 if (ret != LTTNG_OK) {
128 goto error;
129 }
130
131 ret = agent_enable_context(ctx, domain);
132 if (ret != LTTNG_OK) {
133 goto error;
134 }
135 break;
136 }
137 case LTTNG_DOMAIN_UST:
138 break;
139 default:
140 assert(0);
141 }
142
143 /* Create ltt UST context */
144 uctx = trace_ust_create_context(ctx);
145 if (uctx == NULL) {
146 ret = -EINVAL;
147 goto error;
148 }
149
150 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
151 if (ret < 0) {
152 goto error;
153 }
154
155 rcu_read_lock();
156
157 /* Add ltt UST context node to ltt UST channel */
158 lttng_ht_add_ulong(uchan->ctx, &uctx->node);
159 rcu_read_unlock();
160 cds_list_add_tail(&uctx->list, &uchan->ctx_list);
161
162 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
163
164 return 0;
165
166 error:
167 free(uctx);
168 duplicate:
169 return ret;
170 }
171
172 /*
173 * Add kernel context to tracer.
174 */
175 int context_kernel_add(struct ltt_kernel_session *ksession,
176 struct lttng_event_context *ctx, char *channel_name)
177 {
178 int ret;
179 struct ltt_kernel_channel *kchan;
180 struct ltt_kernel_context *kctx;
181
182 assert(ksession);
183 assert(ctx);
184 assert(channel_name);
185
186 kctx = trace_kernel_create_context(NULL);
187 if (!kctx) {
188 ret = LTTNG_ERR_NOMEM;
189 goto error;
190 }
191
192 /* Setup kernel context structure */
193 switch (ctx->ctx) {
194 case LTTNG_EVENT_CONTEXT_PID:
195 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
196 break;
197 case LTTNG_EVENT_CONTEXT_PROCNAME:
198 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
199 break;
200 case LTTNG_EVENT_CONTEXT_PRIO:
201 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
202 break;
203 case LTTNG_EVENT_CONTEXT_NICE:
204 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
205 break;
206 case LTTNG_EVENT_CONTEXT_VPID:
207 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
208 break;
209 case LTTNG_EVENT_CONTEXT_TID:
210 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
211 break;
212 case LTTNG_EVENT_CONTEXT_VTID:
213 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
214 break;
215 case LTTNG_EVENT_CONTEXT_PPID:
216 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
217 break;
218 case LTTNG_EVENT_CONTEXT_VPPID:
219 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
220 break;
221 case LTTNG_EVENT_CONTEXT_HOSTNAME:
222 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
223 break;
224 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
225 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
226 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER;
227 break;
228 default:
229 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
230 goto error;
231 }
232
233 kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
234 kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
235 strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
236 LTTNG_SYMBOL_NAME_LEN);
237 kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
238
239 if (*channel_name == '\0') {
240 ret = add_kctx_all_channels(ksession, kctx);
241 if (ret != LTTNG_OK) {
242 goto error;
243 }
244 } else {
245 /* Get kernel channel */
246 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
247 if (kchan == NULL) {
248 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
249 goto error;
250 }
251
252 ret = add_kctx_to_channel(kctx, kchan);
253 if (ret != LTTNG_OK) {
254 goto error;
255 }
256 }
257
258 return LTTNG_OK;
259
260 error:
261 if (kctx) {
262 trace_kernel_destroy_context(kctx);
263 }
264 return ret;
265 }
266
267 /*
268 * Add UST context to tracer.
269 */
270 int context_ust_add(struct ltt_ust_session *usess,
271 enum lttng_domain_type domain, struct lttng_event_context *ctx,
272 char *channel_name)
273 {
274 int ret = LTTNG_OK;
275 struct lttng_ht_iter iter;
276 struct lttng_ht *chan_ht;
277 struct ltt_ust_channel *uchan = NULL;
278
279 assert(usess);
280 assert(ctx);
281 assert(channel_name);
282
283 rcu_read_lock();
284
285 chan_ht = usess->domain_global.channels;
286
287 /* Get UST channel if defined */
288 if (channel_name[0] != '\0') {
289 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
290 if (uchan == NULL) {
291 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
292 goto error;
293 }
294 }
295
296 if (uchan) {
297 /* Add ctx to channel */
298 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
299 } else {
300 rcu_read_lock();
301 /* Add ctx all events, all channels */
302 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
303 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
304 if (ret < 0) {
305 ERR("Failed to add context to channel %s",
306 uchan->name);
307 continue;
308 }
309 }
310 rcu_read_unlock();
311 }
312
313 switch (ret) {
314 case -EEXIST:
315 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
316 break;
317 case -ENOMEM:
318 ret = LTTNG_ERR_FATAL;
319 break;
320 case -EINVAL:
321 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
322 break;
323 case -ENOSYS:
324 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
325 break;
326 default:
327 ret = LTTNG_OK;
328 break;
329 }
330
331 error:
332 rcu_read_unlock();
333 return ret;
334 }
This page took 0.036378 seconds and 5 git commands to generate.