2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
11 #include "context.hpp"
13 #include "trace-ust.hpp"
14 #include "ust-app.hpp"
16 #include <common/error.hpp>
17 #include <common/sessiond-comm/sessiond-comm.hpp>
21 #include <urcu/list.h>
24 * Add kernel context to all channel.
26 * Assumes the ownership of kctx.
28 static int add_kctx_all_channels(struct ltt_kernel_session
*ksession
,
29 struct ltt_kernel_context
*kctx
)
32 struct ltt_kernel_channel
*kchan
;
34 LTTNG_ASSERT(ksession
);
37 DBG("Adding kernel context to all channels");
39 /* Go over all channels */
40 cds_list_for_each_entry (kchan
, &ksession
->channel_list
.head
, list
) {
41 struct ltt_kernel_context
*kctx_copy
;
43 kctx_copy
= trace_kernel_copy_context(kctx
);
45 PERROR("zmalloc ltt_kernel_context");
46 ret
= -LTTNG_ERR_NOMEM
;
50 /* Ownership of kctx_copy is transferred to the callee. */
51 ret
= kernel_add_channel_context(kchan
, kctx_copy
);
61 trace_kernel_destroy_context(kctx
);
66 * Add kernel context to a specific channel.
68 * Assumes the ownership of kctx.
70 static int add_kctx_to_channel(struct ltt_kernel_context
*kctx
, struct ltt_kernel_channel
*kchan
)
77 DBG("Add kernel context to channel '%s'", kchan
->channel
->name
);
79 /* Ownership of kctx is transferred to the callee. */
80 ret
= kernel_add_channel_context(kchan
, kctx
);
93 * Add UST context to channel.
95 static int add_uctx_to_channel(struct ltt_ust_session
*usess
,
96 enum lttng_domain_type domain
,
97 struct ltt_ust_channel
*uchan
,
98 const struct lttng_event_context
*ctx
)
101 struct ltt_ust_context
*uctx
= nullptr;
107 /* Check if context is duplicate */
108 cds_list_for_each_entry (uctx
, &uchan
->ctx_list
, list
) {
109 if (trace_ust_match_context(uctx
, ctx
)) {
110 ret
= LTTNG_ERR_UST_CONTEXT_EXIST
;
117 case LTTNG_DOMAIN_JUL
:
118 case LTTNG_DOMAIN_LOG4J
:
122 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
123 /* Other contexts are not needed by the agent. */
126 agt
= trace_ust_find_agent(usess
, domain
);
129 agt
= agent_create(domain
);
131 ret
= -LTTNG_ERR_NOMEM
;
134 agent_add(agt
, usess
->agents
);
136 ret
= agent_add_context(ctx
, agt
);
137 if (ret
!= LTTNG_OK
) {
141 ret
= agent_enable_context(ctx
, domain
);
142 if (ret
!= LTTNG_OK
) {
147 case LTTNG_DOMAIN_UST
:
153 /* Create ltt UST context */
154 uctx
= trace_ust_create_context(ctx
);
155 if (uctx
== nullptr) {
156 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
160 /* Add ltt UST context node to ltt UST channel */
161 lttng_ht_add_ulong(uchan
->ctx
, &uctx
->node
);
162 cds_list_add_tail(&uctx
->list
, &uchan
->ctx_list
);
164 if (!usess
->active
) {
168 ret
= ust_app_add_ctx_channel_glb(usess
, uchan
, uctx
);
173 DBG("Context UST %d added to channel %s", uctx
->ctx
.ctx
, uchan
->name
);
184 * Add kernel context to tracer.
186 int context_kernel_add(struct ltt_kernel_session
*ksession
,
187 const struct lttng_event_context
*ctx
,
188 const char *channel_name
)
191 struct ltt_kernel_channel
*kchan
;
192 struct ltt_kernel_context
*kctx
;
194 LTTNG_ASSERT(ksession
);
196 LTTNG_ASSERT(channel_name
);
198 kctx
= trace_kernel_create_context(nullptr);
200 ret
= -LTTNG_ERR_NOMEM
;
204 /* Setup kernel context structure */
206 case LTTNG_EVENT_CONTEXT_PID
:
207 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PID
;
209 case LTTNG_EVENT_CONTEXT_PROCNAME
:
210 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PROCNAME
;
212 case LTTNG_EVENT_CONTEXT_PRIO
:
213 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PRIO
;
215 case LTTNG_EVENT_CONTEXT_NICE
:
216 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_NICE
;
218 case LTTNG_EVENT_CONTEXT_VPID
:
219 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VPID
;
221 case LTTNG_EVENT_CONTEXT_TID
:
222 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_TID
;
224 case LTTNG_EVENT_CONTEXT_VTID
:
225 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VTID
;
227 case LTTNG_EVENT_CONTEXT_PPID
:
228 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PPID
;
230 case LTTNG_EVENT_CONTEXT_VPPID
:
231 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VPPID
;
233 case LTTNG_EVENT_CONTEXT_HOSTNAME
:
234 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_HOSTNAME
;
236 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
237 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
238 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PERF_CPU_COUNTER
;
240 case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
:
241 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_INTERRUPTIBLE
;
243 case LTTNG_EVENT_CONTEXT_PREEMPTIBLE
:
244 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PREEMPTIBLE
;
246 case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
:
247 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_NEED_RESCHEDULE
;
249 case LTTNG_EVENT_CONTEXT_MIGRATABLE
:
250 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_MIGRATABLE
;
252 case LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL
:
253 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL
;
255 case LTTNG_EVENT_CONTEXT_CALLSTACK_USER
:
256 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER
;
258 case LTTNG_EVENT_CONTEXT_CGROUP_NS
:
259 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_CGROUP_NS
;
261 case LTTNG_EVENT_CONTEXT_IPC_NS
:
262 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_IPC_NS
;
264 case LTTNG_EVENT_CONTEXT_MNT_NS
:
265 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_MNT_NS
;
267 case LTTNG_EVENT_CONTEXT_NET_NS
:
268 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_NET_NS
;
270 case LTTNG_EVENT_CONTEXT_PID_NS
:
271 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PID_NS
;
273 case LTTNG_EVENT_CONTEXT_TIME_NS
:
274 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_TIME_NS
;
276 case LTTNG_EVENT_CONTEXT_USER_NS
:
277 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_USER_NS
;
279 case LTTNG_EVENT_CONTEXT_UTS_NS
:
280 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_UTS_NS
;
282 case LTTNG_EVENT_CONTEXT_UID
:
283 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_UID
;
285 case LTTNG_EVENT_CONTEXT_EUID
:
286 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_EUID
;
288 case LTTNG_EVENT_CONTEXT_SUID
:
289 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_SUID
;
291 case LTTNG_EVENT_CONTEXT_GID
:
292 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_GID
;
294 case LTTNG_EVENT_CONTEXT_EGID
:
295 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_EGID
;
297 case LTTNG_EVENT_CONTEXT_SGID
:
298 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_SGID
;
300 case LTTNG_EVENT_CONTEXT_VUID
:
301 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VUID
;
303 case LTTNG_EVENT_CONTEXT_VEUID
:
304 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VEUID
;
306 case LTTNG_EVENT_CONTEXT_VSUID
:
307 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VSUID
;
309 case LTTNG_EVENT_CONTEXT_VGID
:
310 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VGID
;
312 case LTTNG_EVENT_CONTEXT_VEGID
:
313 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VEGID
;
315 case LTTNG_EVENT_CONTEXT_VSGID
:
316 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VSGID
;
319 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
323 kctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
324 kctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
325 strncpy(kctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
, LTTNG_SYMBOL_NAME_LEN
);
326 kctx
->ctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
328 if (*channel_name
== '\0') {
329 ret
= add_kctx_all_channels(ksession
, kctx
);
330 /* Ownership of kctx is transferred to the callee. */
332 if (ret
!= LTTNG_OK
) {
336 /* Get kernel channel */
337 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
338 if (kchan
== nullptr) {
339 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
343 ret
= add_kctx_to_channel(kctx
, kchan
);
344 /* Ownership of kctx is transferred to the callee. */
346 if (ret
!= LTTNG_OK
) {
355 trace_kernel_destroy_context(kctx
);
361 * Add UST context to tracer.
363 int context_ust_add(struct ltt_ust_session
*usess
,
364 enum lttng_domain_type domain
,
365 const struct lttng_event_context
*ctx
,
366 const char *channel_name
)
369 struct lttng_ht_iter iter
;
370 struct lttng_ht
*chan_ht
;
371 struct ltt_ust_channel
*uchan
= nullptr;
375 LTTNG_ASSERT(channel_name
);
379 chan_ht
= usess
->domain_global
.channels
;
381 /* Get UST channel if defined */
382 if (channel_name
[0] != '\0') {
383 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
384 if (uchan
== nullptr) {
385 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
391 /* Add ctx to channel */
392 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
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
);
399 ERR("Failed to add context to channel %s", uchan
->name
);
407 case LTTNG_ERR_UST_CONTEXT_EXIST
:
410 case -LTTNG_ERR_NOMEM
:
411 ret
= LTTNG_ERR_FATAL
;
414 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
417 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
420 if (ret
!= 0 && ret
!= LTTNG_OK
) {
421 ret
= ret
> 0 ? ret
: LTTNG_ERR_UNK
;