sessiond: implement clear command
[deliverable/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,
111 const struct lttng_event_context *ctx)
112 {
113 int ret;
114 struct ltt_ust_context *uctx = NULL;
115
116 assert(usess);
117 assert(uchan);
118 assert(ctx);
119
120 /* Check if context is duplicate */
121 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
122 if (trace_ust_match_context(uctx, ctx)) {
123 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
124 goto duplicate;
125 }
126 }
127 uctx = NULL;
128
129 switch (domain) {
130 case LTTNG_DOMAIN_JUL:
131 case LTTNG_DOMAIN_LOG4J:
132 {
133 struct agent *agt;
134
135 if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
136 /* Other contexts are not needed by the agent. */
137 break;
138 }
139 agt = trace_ust_find_agent(usess, domain);
140
141 if (!agt) {
142 agt = agent_create(domain);
143 if (!agt) {
144 ret = -LTTNG_ERR_NOMEM;
145 goto error;
146 }
147 agent_add(agt, usess->agents);
148 }
149 ret = agent_add_context(ctx, agt);
150 if (ret != LTTNG_OK) {
151 goto error;
152 }
153
154 ret = agent_enable_context(ctx, domain);
155 if (ret != LTTNG_OK) {
156 goto error;
157 }
158 break;
159 }
160 case LTTNG_DOMAIN_UST:
161 break;
162 default:
163 assert(0);
164 }
165
166 /* Create ltt UST context */
167 uctx = trace_ust_create_context(ctx);
168 if (uctx == NULL) {
169 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
170 goto error;
171 }
172
173 /* Add ltt UST context node to ltt UST channel */
174 lttng_ht_add_ulong(uchan->ctx, &uctx->node);
175 cds_list_add_tail(&uctx->list, &uchan->ctx_list);
176
177 if (!usess->active) {
178 goto end;
179 }
180
181 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
182 if (ret < 0) {
183 goto error;
184 }
185 end:
186 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
187
188 return 0;
189
190 error:
191 free(uctx);
192 duplicate:
193 return ret;
194 }
195
196 /*
197 * Add kernel context to tracer.
198 */
199 int context_kernel_add(struct ltt_kernel_session *ksession,
200 const struct lttng_event_context *ctx, char *channel_name)
201 {
202 int ret;
203 struct ltt_kernel_channel *kchan;
204 struct ltt_kernel_context *kctx;
205
206 assert(ksession);
207 assert(ctx);
208 assert(channel_name);
209
210 kctx = trace_kernel_create_context(NULL);
211 if (!kctx) {
212 ret = -LTTNG_ERR_NOMEM;
213 goto error;
214 }
215
216 /* Setup kernel context structure */
217 switch (ctx->ctx) {
218 case LTTNG_EVENT_CONTEXT_PID:
219 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
220 break;
221 case LTTNG_EVENT_CONTEXT_PROCNAME:
222 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
223 break;
224 case LTTNG_EVENT_CONTEXT_PRIO:
225 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
226 break;
227 case LTTNG_EVENT_CONTEXT_NICE:
228 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
229 break;
230 case LTTNG_EVENT_CONTEXT_VPID:
231 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
232 break;
233 case LTTNG_EVENT_CONTEXT_TID:
234 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
235 break;
236 case LTTNG_EVENT_CONTEXT_VTID:
237 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
238 break;
239 case LTTNG_EVENT_CONTEXT_PPID:
240 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
241 break;
242 case LTTNG_EVENT_CONTEXT_VPPID:
243 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
244 break;
245 case LTTNG_EVENT_CONTEXT_HOSTNAME:
246 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
247 break;
248 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
249 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
250 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER;
251 break;
252 case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE:
253 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE;
254 break;
255 case LTTNG_EVENT_CONTEXT_PREEMPTIBLE:
256 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PREEMPTIBLE;
257 break;
258 case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE:
259 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE;
260 break;
261 case LTTNG_EVENT_CONTEXT_MIGRATABLE:
262 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_MIGRATABLE;
263 break;
264 case LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL:
265 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL;
266 break;
267 case LTTNG_EVENT_CONTEXT_CALLSTACK_USER:
268 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_CALLSTACK_USER;
269 break;
270 case LTTNG_EVENT_CONTEXT_CGROUP_NS:
271 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_CGROUP_NS;
272 break;
273 case LTTNG_EVENT_CONTEXT_IPC_NS:
274 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_IPC_NS;
275 break;
276 case LTTNG_EVENT_CONTEXT_MNT_NS:
277 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_MNT_NS;
278 break;
279 case LTTNG_EVENT_CONTEXT_NET_NS:
280 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NET_NS;
281 break;
282 case LTTNG_EVENT_CONTEXT_PID_NS:
283 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID_NS;
284 break;
285 case LTTNG_EVENT_CONTEXT_USER_NS:
286 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_USER_NS;
287 break;
288 case LTTNG_EVENT_CONTEXT_UTS_NS:
289 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_UTS_NS;
290 break;
291 case LTTNG_EVENT_CONTEXT_UID:
292 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_UID;
293 break;
294 case LTTNG_EVENT_CONTEXT_EUID:
295 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_EUID;
296 break;
297 case LTTNG_EVENT_CONTEXT_SUID:
298 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_SUID;
299 break;
300 case LTTNG_EVENT_CONTEXT_GID:
301 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_GID;
302 break;
303 case LTTNG_EVENT_CONTEXT_EGID:
304 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_EGID;
305 break;
306 case LTTNG_EVENT_CONTEXT_SGID:
307 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_SGID;
308 break;
309 case LTTNG_EVENT_CONTEXT_VUID:
310 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VUID;
311 break;
312 case LTTNG_EVENT_CONTEXT_VEUID:
313 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VEUID;
314 break;
315 case LTTNG_EVENT_CONTEXT_VSUID:
316 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VSUID;
317 break;
318 case LTTNG_EVENT_CONTEXT_VGID:
319 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VGID;
320 break;
321 case LTTNG_EVENT_CONTEXT_VEGID:
322 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VEGID;
323 break;
324 case LTTNG_EVENT_CONTEXT_VSGID:
325 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VSGID;
326 break;
327 default:
328 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
329 goto error;
330 }
331
332 kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
333 kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
334 strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
335 LTTNG_SYMBOL_NAME_LEN);
336 kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
337
338 if (*channel_name == '\0') {
339 ret = add_kctx_all_channels(ksession, kctx);
340 /* Ownership of kctx is transferred to the callee. */
341 kctx = NULL;
342 if (ret != LTTNG_OK) {
343 goto error;
344 }
345 } else {
346 /* Get kernel channel */
347 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
348 if (kchan == NULL) {
349 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
350 goto error;
351 }
352
353 ret = add_kctx_to_channel(kctx, kchan);
354 /* Ownership of kctx is transferred to the callee. */
355 kctx = NULL;
356 if (ret != LTTNG_OK) {
357 goto error;
358 }
359 }
360
361 ret = LTTNG_OK;
362
363 error:
364 if (kctx) {
365 trace_kernel_destroy_context(kctx);
366 }
367 return ret;
368 }
369
370 /*
371 * Add UST context to tracer.
372 */
373 int context_ust_add(struct ltt_ust_session *usess,
374 enum lttng_domain_type domain,
375 const struct lttng_event_context *ctx,
376 char *channel_name)
377 {
378 int ret = LTTNG_OK;
379 struct lttng_ht_iter iter;
380 struct lttng_ht *chan_ht;
381 struct ltt_ust_channel *uchan = NULL;
382
383 assert(usess);
384 assert(ctx);
385 assert(channel_name);
386
387 rcu_read_lock();
388
389 chan_ht = usess->domain_global.channels;
390
391 /* Get UST channel if defined */
392 if (channel_name[0] != '\0') {
393 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
394 if (uchan == NULL) {
395 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
396 goto error;
397 }
398 }
399
400 if (uchan) {
401 /* Add ctx to channel */
402 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
403 } else {
404 rcu_read_lock();
405 /* Add ctx all events, all channels */
406 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
407 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
408 if (ret) {
409 ERR("Failed to add context to channel %s",
410 uchan->name);
411 continue;
412 }
413 }
414 rcu_read_unlock();
415 }
416
417 switch (ret) {
418 case LTTNG_ERR_UST_CONTEXT_EXIST:
419 break;
420 case -ENOMEM:
421 case -LTTNG_ERR_NOMEM:
422 ret = LTTNG_ERR_FATAL;
423 break;
424 case -EINVAL:
425 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
426 break;
427 case -ENOSYS:
428 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
429 break;
430 default:
431 if (ret != 0 && ret != LTTNG_OK) {
432 ret = ret > 0 ? ret : LTTNG_ERR_UNK;
433 } else {
434 ret = LTTNG_OK;
435 }
436 break;
437 }
438
439 error:
440 rcu_read_unlock();
441 return ret;
442 }
This page took 0.041295 seconds and 5 git commands to generate.