Add kernel namespace contexts
[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 * Assumes the ownership of kctx.
39 */
40 static int add_kctx_all_channels(struct ltt_kernel_session *ksession,
41 struct ltt_kernel_context *kctx)
42 {
43 int ret;
44 struct ltt_kernel_channel *kchan;
45
46 assert(ksession);
47 assert(kctx);
48
49 DBG("Adding kernel context to all channels");
50
51 /* Go over all channels */
52 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
53 struct ltt_kernel_context *kctx_copy;
54
55 kctx_copy = trace_kernel_copy_context(kctx);
56 if (!kctx_copy) {
57 PERROR("zmalloc ltt_kernel_context");
58 ret = -LTTNG_ERR_NOMEM;
59 goto error;
60 }
61
62 /* Ownership of kctx_copy is transferred to the callee. */
63 ret = kernel_add_channel_context(kchan, kctx_copy);
64 kctx_copy = NULL;
65 if (ret != 0) {
66 goto error;
67 }
68 }
69
70 ret = LTTNG_OK;
71
72 error:
73 trace_kernel_destroy_context(kctx);
74 return ret;
75 }
76
77 /*
78 * Add kernel context to a specific channel.
79 *
80 * Assumes the ownership of kctx.
81 */
82 static int add_kctx_to_channel(struct ltt_kernel_context *kctx,
83 struct ltt_kernel_channel *kchan)
84 {
85 int ret;
86
87 assert(kchan);
88 assert(kctx);
89
90 DBG("Add kernel context to channel '%s'", kchan->channel->name);
91
92 /* Ownership of kctx is transferred to the callee. */
93 ret = kernel_add_channel_context(kchan, kctx);
94 kctx = NULL;
95 if (ret != 0) {
96 goto error;
97 }
98
99 ret = LTTNG_OK;
100
101 error:
102 return ret;
103 }
104
105 /*
106 * Add UST context to channel.
107 */
108 static int add_uctx_to_channel(struct ltt_ust_session *usess,
109 enum lttng_domain_type domain,
110 struct ltt_ust_channel *uchan, struct lttng_event_context *ctx)
111 {
112 int ret;
113 struct ltt_ust_context *uctx = NULL;
114
115 assert(usess);
116 assert(uchan);
117 assert(ctx);
118
119 /* Check if context is duplicate */
120 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
121 if (trace_ust_match_context(uctx, ctx)) {
122 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
123 goto duplicate;
124 }
125 }
126 uctx = NULL;
127
128 switch (domain) {
129 case LTTNG_DOMAIN_JUL:
130 case LTTNG_DOMAIN_LOG4J:
131 {
132 struct agent *agt;
133
134 if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
135 /* Other contexts are not needed by the agent. */
136 break;
137 }
138 agt = trace_ust_find_agent(usess, domain);
139
140 if (!agt) {
141 agt = agent_create(domain);
142 if (!agt) {
143 ret = -LTTNG_ERR_NOMEM;
144 goto error;
145 }
146 agent_add(agt, usess->agents);
147 }
148 ret = agent_add_context(ctx, agt);
149 if (ret != LTTNG_OK) {
150 goto error;
151 }
152
153 ret = agent_enable_context(ctx, domain);
154 if (ret != LTTNG_OK) {
155 goto error;
156 }
157 break;
158 }
159 case LTTNG_DOMAIN_UST:
160 break;
161 default:
162 assert(0);
163 }
164
165 /* Create ltt UST context */
166 uctx = trace_ust_create_context(ctx);
167 if (uctx == NULL) {
168 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
169 goto error;
170 }
171
172 /* Add ltt UST context node to ltt UST channel */
173 lttng_ht_add_ulong(uchan->ctx, &uctx->node);
174 cds_list_add_tail(&uctx->list, &uchan->ctx_list);
175
176 if (!usess->active) {
177 goto end;
178 }
179
180 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
181 if (ret < 0) {
182 goto error;
183 }
184 end:
185 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
186
187 return 0;
188
189 error:
190 free(uctx);
191 duplicate:
192 return ret;
193 }
194
195 /*
196 * Add kernel context to tracer.
197 */
198 int context_kernel_add(struct ltt_kernel_session *ksession,
199 struct lttng_event_context *ctx, char *channel_name)
200 {
201 int ret;
202 struct ltt_kernel_channel *kchan;
203 struct ltt_kernel_context *kctx;
204
205 assert(ksession);
206 assert(ctx);
207 assert(channel_name);
208
209 kctx = trace_kernel_create_context(NULL);
210 if (!kctx) {
211 ret = -LTTNG_ERR_NOMEM;
212 goto error;
213 }
214
215 /* Setup kernel context structure */
216 switch (ctx->ctx) {
217 case LTTNG_EVENT_CONTEXT_PID:
218 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
219 break;
220 case LTTNG_EVENT_CONTEXT_PROCNAME:
221 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
222 break;
223 case LTTNG_EVENT_CONTEXT_PRIO:
224 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
225 break;
226 case LTTNG_EVENT_CONTEXT_NICE:
227 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
228 break;
229 case LTTNG_EVENT_CONTEXT_VPID:
230 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
231 break;
232 case LTTNG_EVENT_CONTEXT_TID:
233 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
234 break;
235 case LTTNG_EVENT_CONTEXT_VTID:
236 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
237 break;
238 case LTTNG_EVENT_CONTEXT_PPID:
239 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
240 break;
241 case LTTNG_EVENT_CONTEXT_VPPID:
242 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
243 break;
244 case LTTNG_EVENT_CONTEXT_HOSTNAME:
245 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
246 break;
247 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
248 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
249 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER;
250 break;
251 case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE:
252 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE;
253 break;
254 case LTTNG_EVENT_CONTEXT_PREEMPTIBLE:
255 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PREEMPTIBLE;
256 break;
257 case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE:
258 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE;
259 break;
260 case LTTNG_EVENT_CONTEXT_MIGRATABLE:
261 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_MIGRATABLE;
262 break;
263 case LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL:
264 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL;
265 break;
266 case LTTNG_EVENT_CONTEXT_CALLSTACK_USER:
267 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_CALLSTACK_USER;
268 break;
269 case LTTNG_EVENT_CONTEXT_CGROUP_NS:
270 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_CGROUP_NS;
271 break;
272 case LTTNG_EVENT_CONTEXT_IPC_NS:
273 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_IPC_NS;
274 break;
275 case LTTNG_EVENT_CONTEXT_MNT_NS:
276 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_MNT_NS;
277 break;
278 case LTTNG_EVENT_CONTEXT_NET_NS:
279 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NET_NS;
280 break;
281 case LTTNG_EVENT_CONTEXT_PID_NS:
282 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID_NS;
283 break;
284 case LTTNG_EVENT_CONTEXT_USER_NS:
285 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_USER_NS;
286 break;
287 case LTTNG_EVENT_CONTEXT_UTS_NS:
288 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_UTS_NS;
289 break;
290 default:
291 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
292 goto error;
293 }
294
295 kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
296 kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
297 strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
298 LTTNG_SYMBOL_NAME_LEN);
299 kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
300
301 if (*channel_name == '\0') {
302 ret = add_kctx_all_channels(ksession, kctx);
303 /* Ownership of kctx is transferred to the callee. */
304 kctx = NULL;
305 if (ret != LTTNG_OK) {
306 goto error;
307 }
308 } else {
309 /* Get kernel channel */
310 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
311 if (kchan == NULL) {
312 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
313 goto error;
314 }
315
316 ret = add_kctx_to_channel(kctx, kchan);
317 /* Ownership of kctx is transferred to the callee. */
318 kctx = NULL;
319 if (ret != LTTNG_OK) {
320 goto error;
321 }
322 }
323
324 ret = LTTNG_OK;
325
326 error:
327 if (kctx) {
328 trace_kernel_destroy_context(kctx);
329 }
330 return ret;
331 }
332
333 /*
334 * Add UST context to tracer.
335 */
336 int context_ust_add(struct ltt_ust_session *usess,
337 enum lttng_domain_type domain, struct lttng_event_context *ctx,
338 char *channel_name)
339 {
340 int ret = LTTNG_OK;
341 struct lttng_ht_iter iter;
342 struct lttng_ht *chan_ht;
343 struct ltt_ust_channel *uchan = NULL;
344
345 assert(usess);
346 assert(ctx);
347 assert(channel_name);
348
349 rcu_read_lock();
350
351 chan_ht = usess->domain_global.channels;
352
353 /* Get UST channel if defined */
354 if (channel_name[0] != '\0') {
355 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
356 if (uchan == NULL) {
357 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
358 goto error;
359 }
360 }
361
362 if (uchan) {
363 /* Add ctx to channel */
364 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
365 } else {
366 rcu_read_lock();
367 /* Add ctx all events, all channels */
368 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
369 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
370 if (ret) {
371 ERR("Failed to add context to channel %s",
372 uchan->name);
373 continue;
374 }
375 }
376 rcu_read_unlock();
377 }
378
379 switch (ret) {
380 case LTTNG_ERR_UST_CONTEXT_EXIST:
381 break;
382 case -ENOMEM:
383 case -LTTNG_ERR_NOMEM:
384 ret = LTTNG_ERR_FATAL;
385 break;
386 case -EINVAL:
387 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
388 break;
389 case -ENOSYS:
390 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
391 break;
392 default:
393 if (ret != 0 && ret != LTTNG_OK) {
394 ret = ret > 0 ? ret : LTTNG_ERR_UNK;
395 } else {
396 ret = LTTNG_OK;
397 }
398 break;
399 }
400
401 error:
402 rcu_read_unlock();
403 return ret;
404 }
This page took 0.038837 seconds and 5 git commands to generate.