Fix: file-descriptor: missing include guards
[lttng-tools.git] / src / bin / lttng-sessiond / context.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include "agent.hpp"
11 #include "context.hpp"
12 #include "kernel.hpp"
13 #include "trace-ust.hpp"
14 #include "ust-app.hpp"
15
16 #include <common/error.hpp>
17 #include <common/sessiond-comm/sessiond-comm.hpp>
18 #include <common/urcu.hpp>
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <urcu/list.h>
23
24 /*
25 * Add kernel context to all channel.
26 *
27 * Assumes the ownership of kctx.
28 */
29 static int add_kctx_all_channels(struct ltt_kernel_session *ksession,
30 struct ltt_kernel_context *kctx)
31 {
32 int ret;
33 struct ltt_kernel_channel *kchan;
34
35 LTTNG_ASSERT(ksession);
36 LTTNG_ASSERT(kctx);
37
38 DBG("Adding kernel context to all channels");
39
40 /* Go over all channels */
41 cds_list_for_each_entry (kchan, &ksession->channel_list.head, list) {
42 struct ltt_kernel_context *kctx_copy;
43
44 kctx_copy = trace_kernel_copy_context(kctx);
45 if (!kctx_copy) {
46 PERROR("zmalloc ltt_kernel_context");
47 ret = -LTTNG_ERR_NOMEM;
48 goto error;
49 }
50
51 /* Ownership of kctx_copy is transferred to the callee. */
52 ret = kernel_add_channel_context(kchan, kctx_copy);
53 kctx_copy = nullptr;
54 if (ret != 0) {
55 goto error;
56 }
57 }
58
59 ret = LTTNG_OK;
60
61 error:
62 trace_kernel_destroy_context(kctx);
63 return ret;
64 }
65
66 /*
67 * Add kernel context to a specific channel.
68 *
69 * Assumes the ownership of kctx.
70 */
71 static int add_kctx_to_channel(struct ltt_kernel_context *kctx, struct ltt_kernel_channel *kchan)
72 {
73 int ret;
74
75 LTTNG_ASSERT(kchan);
76 LTTNG_ASSERT(kctx);
77
78 DBG("Add kernel context to channel '%s'", kchan->channel->name);
79
80 /* Ownership of kctx is transferred to the callee. */
81 ret = kernel_add_channel_context(kchan, kctx);
82 kctx = nullptr;
83 if (ret != 0) {
84 goto error;
85 }
86
87 ret = LTTNG_OK;
88
89 error:
90 return ret;
91 }
92
93 /*
94 * Add UST context to channel.
95 */
96 static int add_uctx_to_channel(struct ltt_ust_session *usess,
97 enum lttng_domain_type domain,
98 struct ltt_ust_channel *uchan,
99 const struct lttng_event_context *ctx)
100 {
101 int ret;
102 struct ltt_ust_context *uctx = nullptr;
103
104 LTTNG_ASSERT(usess);
105 LTTNG_ASSERT(uchan);
106 LTTNG_ASSERT(ctx);
107
108 /* Check if context is duplicate */
109 cds_list_for_each_entry (uctx, &uchan->ctx_list, list) {
110 if (trace_ust_match_context(uctx, ctx)) {
111 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
112 goto duplicate;
113 }
114 }
115 uctx = nullptr;
116
117 switch (domain) {
118 case LTTNG_DOMAIN_JUL:
119 case LTTNG_DOMAIN_LOG4J:
120 {
121 struct agent *agt;
122
123 if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
124 /* Other contexts are not needed by the agent. */
125 break;
126 }
127 agt = trace_ust_find_agent(usess, domain);
128
129 if (!agt) {
130 agt = agent_create(domain);
131 if (!agt) {
132 ret = -LTTNG_ERR_NOMEM;
133 goto error;
134 }
135 agent_add(agt, usess->agents);
136 }
137 ret = agent_add_context(ctx, agt);
138 if (ret != LTTNG_OK) {
139 goto error;
140 }
141
142 ret = agent_enable_context(ctx, domain);
143 if (ret != LTTNG_OK) {
144 goto error;
145 }
146 break;
147 }
148 case LTTNG_DOMAIN_UST:
149 break;
150 default:
151 abort();
152 }
153
154 /* Create ltt UST context */
155 uctx = trace_ust_create_context(ctx);
156 if (uctx == nullptr) {
157 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
158 goto error;
159 }
160
161 /* Add ltt UST context node to ltt UST channel */
162 lttng_ht_add_ulong(uchan->ctx, &uctx->node);
163 cds_list_add_tail(&uctx->list, &uchan->ctx_list);
164
165 if (!usess->active) {
166 goto end;
167 }
168
169 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
170 if (ret < 0) {
171 goto error;
172 }
173 end:
174 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
175
176 return 0;
177
178 error:
179 free(uctx);
180 duplicate:
181 return ret;
182 }
183
184 /*
185 * Add kernel context to tracer.
186 */
187 int context_kernel_add(struct ltt_kernel_session *ksession,
188 const struct lttng_event_context *ctx,
189 const char *channel_name)
190 {
191 int ret;
192 struct ltt_kernel_channel *kchan;
193 struct ltt_kernel_context *kctx;
194
195 LTTNG_ASSERT(ksession);
196 LTTNG_ASSERT(ctx);
197 LTTNG_ASSERT(channel_name);
198
199 kctx = trace_kernel_create_context(nullptr);
200 if (!kctx) {
201 ret = -LTTNG_ERR_NOMEM;
202 goto error;
203 }
204
205 /* Setup kernel context structure */
206 switch (ctx->ctx) {
207 case LTTNG_EVENT_CONTEXT_PID:
208 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PID;
209 break;
210 case LTTNG_EVENT_CONTEXT_PROCNAME:
211 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PROCNAME;
212 break;
213 case LTTNG_EVENT_CONTEXT_PRIO:
214 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PRIO;
215 break;
216 case LTTNG_EVENT_CONTEXT_NICE:
217 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_NICE;
218 break;
219 case LTTNG_EVENT_CONTEXT_VPID:
220 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VPID;
221 break;
222 case LTTNG_EVENT_CONTEXT_TID:
223 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_TID;
224 break;
225 case LTTNG_EVENT_CONTEXT_VTID:
226 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VTID;
227 break;
228 case LTTNG_EVENT_CONTEXT_PPID:
229 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PPID;
230 break;
231 case LTTNG_EVENT_CONTEXT_VPPID:
232 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VPPID;
233 break;
234 case LTTNG_EVENT_CONTEXT_HOSTNAME:
235 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_HOSTNAME;
236 break;
237 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
238 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
239 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PERF_CPU_COUNTER;
240 break;
241 case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE:
242 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_INTERRUPTIBLE;
243 break;
244 case LTTNG_EVENT_CONTEXT_PREEMPTIBLE:
245 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PREEMPTIBLE;
246 break;
247 case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE:
248 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_NEED_RESCHEDULE;
249 break;
250 case LTTNG_EVENT_CONTEXT_MIGRATABLE:
251 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_MIGRATABLE;
252 break;
253 case LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL:
254 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL;
255 break;
256 case LTTNG_EVENT_CONTEXT_CALLSTACK_USER:
257 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER;
258 break;
259 case LTTNG_EVENT_CONTEXT_CGROUP_NS:
260 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_CGROUP_NS;
261 break;
262 case LTTNG_EVENT_CONTEXT_IPC_NS:
263 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_IPC_NS;
264 break;
265 case LTTNG_EVENT_CONTEXT_MNT_NS:
266 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_MNT_NS;
267 break;
268 case LTTNG_EVENT_CONTEXT_NET_NS:
269 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_NET_NS;
270 break;
271 case LTTNG_EVENT_CONTEXT_PID_NS:
272 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PID_NS;
273 break;
274 case LTTNG_EVENT_CONTEXT_TIME_NS:
275 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_TIME_NS;
276 break;
277 case LTTNG_EVENT_CONTEXT_USER_NS:
278 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_USER_NS;
279 break;
280 case LTTNG_EVENT_CONTEXT_UTS_NS:
281 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_UTS_NS;
282 break;
283 case LTTNG_EVENT_CONTEXT_UID:
284 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_UID;
285 break;
286 case LTTNG_EVENT_CONTEXT_EUID:
287 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_EUID;
288 break;
289 case LTTNG_EVENT_CONTEXT_SUID:
290 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_SUID;
291 break;
292 case LTTNG_EVENT_CONTEXT_GID:
293 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_GID;
294 break;
295 case LTTNG_EVENT_CONTEXT_EGID:
296 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_EGID;
297 break;
298 case LTTNG_EVENT_CONTEXT_SGID:
299 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_SGID;
300 break;
301 case LTTNG_EVENT_CONTEXT_VUID:
302 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VUID;
303 break;
304 case LTTNG_EVENT_CONTEXT_VEUID:
305 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VEUID;
306 break;
307 case LTTNG_EVENT_CONTEXT_VSUID:
308 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VSUID;
309 break;
310 case LTTNG_EVENT_CONTEXT_VGID:
311 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VGID;
312 break;
313 case LTTNG_EVENT_CONTEXT_VEGID:
314 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VEGID;
315 break;
316 case LTTNG_EVENT_CONTEXT_VSGID:
317 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VSGID;
318 break;
319 default:
320 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
321 goto error;
322 }
323
324 kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
325 kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
326 strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name, LTTNG_SYMBOL_NAME_LEN);
327 kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
328
329 if (*channel_name == '\0') {
330 ret = add_kctx_all_channels(ksession, kctx);
331 /* Ownership of kctx is transferred to the callee. */
332 kctx = nullptr;
333 if (ret != LTTNG_OK) {
334 goto error;
335 }
336 } else {
337 /* Get kernel channel */
338 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
339 if (kchan == nullptr) {
340 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
341 goto error;
342 }
343
344 ret = add_kctx_to_channel(kctx, kchan);
345 /* Ownership of kctx is transferred to the callee. */
346 kctx = nullptr;
347 if (ret != LTTNG_OK) {
348 goto error;
349 }
350 }
351
352 ret = LTTNG_OK;
353
354 error:
355 if (kctx) {
356 trace_kernel_destroy_context(kctx);
357 }
358 return ret;
359 }
360
361 /*
362 * Add UST context to tracer.
363 */
364 int context_ust_add(struct ltt_ust_session *usess,
365 enum lttng_domain_type domain,
366 const struct lttng_event_context *ctx,
367 const char *channel_name)
368 {
369 int ret = LTTNG_OK;
370 struct lttng_ht_iter iter;
371 struct lttng_ht *chan_ht;
372 struct ltt_ust_channel *uchan = nullptr;
373
374 LTTNG_ASSERT(usess);
375 LTTNG_ASSERT(ctx);
376 LTTNG_ASSERT(channel_name);
377
378 lttng::urcu::read_lock_guard read_lock;
379
380 chan_ht = usess->domain_global.channels;
381
382 /* Get UST channel if defined */
383 if (channel_name[0] != '\0') {
384 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
385 if (uchan == nullptr) {
386 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
387 goto error;
388 }
389 }
390
391 if (uchan) {
392 /* Add ctx to channel */
393 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
394 } else {
395 /* Add ctx all events, all channels */
396 cds_lfht_for_each_entry (chan_ht->ht, &iter.iter, uchan, node.node) {
397 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
398 if (ret) {
399 ERR("Failed to add context to channel %s", uchan->name);
400 continue;
401 }
402 }
403 }
404
405 switch (ret) {
406 case LTTNG_ERR_UST_CONTEXT_EXIST:
407 break;
408 case -ENOMEM:
409 case -LTTNG_ERR_NOMEM:
410 ret = LTTNG_ERR_FATAL;
411 break;
412 case -EINVAL:
413 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
414 break;
415 case -ENOSYS:
416 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
417 break;
418 default:
419 if (ret != 0 && ret != LTTNG_OK) {
420 ret = ret > 0 ? ret : LTTNG_ERR_UNK;
421 } else {
422 ret = LTTNG_OK;
423 }
424 break;
425 }
426
427 error:
428 return ret;
429 }
This page took 0.038396 seconds and 4 git commands to generate.