Fix: Don't notify agent of non-app context addition
[lttng-tools.git] / src / bin / lttng-sessiond / context.c
... / ...
CommitLineData
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 */
38static 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
60error:
61 return ret;
62}
63
64/*
65 * Add kernel context to a specific channel.
66 */
67static 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
85error:
86 return ret;
87}
88
89/*
90 * Add UST context to channel.
91 */
92static 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;
117
118 if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
119 /* Other contexts are not needed by the agent. */
120 break;
121 }
122 agt = trace_ust_find_agent(usess, domain);
123
124 if (!agt) {
125 agt = agent_create(domain);
126 if (!agt) {
127 ret = LTTNG_ERR_NOMEM;
128 goto error;
129 }
130 agent_add(agt, usess->agents);
131 }
132 ret = agent_add_context(ctx, agt);
133 if (ret != LTTNG_OK) {
134 goto error;
135 }
136
137 ret = agent_enable_context(ctx, domain);
138 if (ret != LTTNG_OK) {
139 goto error;
140 }
141 break;
142 }
143 case LTTNG_DOMAIN_UST:
144 break;
145 default:
146 assert(0);
147 }
148
149 /* Create ltt UST context */
150 uctx = trace_ust_create_context(ctx);
151 if (uctx == NULL) {
152 ret = -EINVAL;
153 goto error;
154 }
155
156 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
157 if (ret < 0) {
158 goto error;
159 }
160
161 rcu_read_lock();
162
163 /* Add ltt UST context node to ltt UST channel */
164 lttng_ht_add_ulong(uchan->ctx, &uctx->node);
165 rcu_read_unlock();
166 cds_list_add_tail(&uctx->list, &uchan->ctx_list);
167
168 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
169
170 return 0;
171
172error:
173 free(uctx);
174duplicate:
175 return ret;
176}
177
178/*
179 * Add kernel context to tracer.
180 */
181int context_kernel_add(struct ltt_kernel_session *ksession,
182 struct lttng_event_context *ctx, char *channel_name)
183{
184 int ret;
185 struct ltt_kernel_channel *kchan;
186 struct ltt_kernel_context *kctx;
187
188 assert(ksession);
189 assert(ctx);
190 assert(channel_name);
191
192 kctx = trace_kernel_create_context(NULL);
193 if (!kctx) {
194 ret = LTTNG_ERR_NOMEM;
195 goto error;
196 }
197
198 /* Setup kernel context structure */
199 switch (ctx->ctx) {
200 case LTTNG_EVENT_CONTEXT_PID:
201 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
202 break;
203 case LTTNG_EVENT_CONTEXT_PROCNAME:
204 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
205 break;
206 case LTTNG_EVENT_CONTEXT_PRIO:
207 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
208 break;
209 case LTTNG_EVENT_CONTEXT_NICE:
210 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
211 break;
212 case LTTNG_EVENT_CONTEXT_VPID:
213 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
214 break;
215 case LTTNG_EVENT_CONTEXT_TID:
216 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
217 break;
218 case LTTNG_EVENT_CONTEXT_VTID:
219 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
220 break;
221 case LTTNG_EVENT_CONTEXT_PPID:
222 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
223 break;
224 case LTTNG_EVENT_CONTEXT_VPPID:
225 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
226 break;
227 case LTTNG_EVENT_CONTEXT_HOSTNAME:
228 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
229 break;
230 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
231 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
232 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER;
233 break;
234 default:
235 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
236 goto error;
237 }
238
239 kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
240 kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
241 strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
242 LTTNG_SYMBOL_NAME_LEN);
243 kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
244
245 if (*channel_name == '\0') {
246 ret = add_kctx_all_channels(ksession, kctx);
247 if (ret != LTTNG_OK) {
248 goto error;
249 }
250 } else {
251 /* Get kernel channel */
252 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
253 if (kchan == NULL) {
254 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
255 goto error;
256 }
257
258 ret = add_kctx_to_channel(kctx, kchan);
259 if (ret != LTTNG_OK) {
260 goto error;
261 }
262 }
263
264 return LTTNG_OK;
265
266error:
267 if (kctx) {
268 trace_kernel_destroy_context(kctx);
269 }
270 return ret;
271}
272
273/*
274 * Add UST context to tracer.
275 */
276int context_ust_add(struct ltt_ust_session *usess,
277 enum lttng_domain_type domain, struct lttng_event_context *ctx,
278 char *channel_name)
279{
280 int ret = LTTNG_OK;
281 struct lttng_ht_iter iter;
282 struct lttng_ht *chan_ht;
283 struct ltt_ust_channel *uchan = NULL;
284
285 assert(usess);
286 assert(ctx);
287 assert(channel_name);
288
289 rcu_read_lock();
290
291 chan_ht = usess->domain_global.channels;
292
293 /* Get UST channel if defined */
294 if (channel_name[0] != '\0') {
295 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
296 if (uchan == NULL) {
297 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
298 goto error;
299 }
300 }
301
302 if (uchan) {
303 /* Add ctx to channel */
304 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
305 } else {
306 rcu_read_lock();
307 /* Add ctx all events, all channels */
308 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
309 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
310 if (ret < 0) {
311 ERR("Failed to add context to channel %s",
312 uchan->name);
313 continue;
314 }
315 }
316 rcu_read_unlock();
317 }
318
319 switch (ret) {
320 case -EEXIST:
321 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
322 break;
323 case -ENOMEM:
324 ret = LTTNG_ERR_FATAL;
325 break;
326 case -EINVAL:
327 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
328 break;
329 case -ENOSYS:
330 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
331 break;
332 default:
333 ret = LTTNG_OK;
334 break;
335 }
336
337error:
338 rcu_read_unlock();
339 return ret;
340}
This page took 0.023039 seconds and 4 git commands to generate.